diff --git a/app/autoload.php b/app/autoload.php index 878f0f1d3ca..8240d31eb5b 100644 --- a/app/autoload.php +++ b/app/autoload.php @@ -5,20 +5,6 @@ use Doctrine\Common\Annotations\AnnotationRegistry; use Composer\Autoload\ClassLoader; -/** @deprecated When 5.6 is PHP minimum */ -if(! function_exists('hash_equals')) { - function hash_equals($str1, $str2) { - if(strlen($str1) != strlen($str2)) { - return false; - } else { - $res = $str1 ^ $str2; - $ret = 0; - for($i = strlen($res) - 1; $i >= 0; $i--) $ret |= ord($res[$i]); - return ! $ret; - } - } -} - /** * @var ClassLoader $loader */ diff --git a/app/middlewares/CatchExceptionMiddleware.php b/app/middlewares/CatchExceptionMiddleware.php index f9597646da0..269f20e6ecd 100644 --- a/app/middlewares/CatchExceptionMiddleware.php +++ b/app/middlewares/CatchExceptionMiddleware.php @@ -17,7 +17,7 @@ class CatchExceptionMiddleware implements HttpKernelInterface, PrioritizedMiddlewareInterface { - const PRIORITY = 1; + const PRIORITY = 100; /** * @var HttpKernelInterface diff --git a/app/middlewares/Dev/IpRestrictMiddleware.php b/app/middlewares/Dev/IpRestrictMiddleware.php new file mode 100644 index 00000000000..dfd414f7b88 --- /dev/null +++ b/app/middlewares/Dev/IpRestrictMiddleware.php @@ -0,0 +1,70 @@ +app = $app; + $this->allowedIps = ['127.0.0.1', 'fe80::1', '::1']; + + if (isset($_SERVER['MAUTIC_DEV_HOSTS'])) { + $localIps = explode(' ', $_SERVER['MAUTIC_DEV_HOSTS']); + $this->allowedIps = array_merge($this->allowedIps, $localIps); + } + } + + /** + * This check prevents access to debug front controllers + * that are deployed by accident to production servers. + * + * {@inheritdoc} + */ + public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true) + { + if (in_array($request->getClientIp(), $this->allowedIps)) { + return $this->app->handle($request, $type, $catch); + } + + return new Response('You are not allowed to access this file.', 403); + } + + /** + * {@inheritdoc} + */ + public function getPriority() + { + return self::PRIORITY; + } +} diff --git a/app/middlewares/MiddlewareBuilder.php b/app/middlewares/MiddlewareBuilder.php index f62a1dab6ff..6c437dd26a6 100644 --- a/app/middlewares/MiddlewareBuilder.php +++ b/app/middlewares/MiddlewareBuilder.php @@ -17,14 +17,29 @@ class MiddlewareBuilder { protected $specs; - public function __construct() + public function __construct($env = 'prod') { $this->specs = new \SplPriorityQueue(); $middlewares = glob(__DIR__ . '/*Middleware.php'); - + + $this->addMiddlewares($middlewares); + + if ($envMiddlewares = glob(__DIR__ . '/' . ucfirst($env) . '/*Middleware.php')) { + $this->addMiddlewares($envMiddlewares, $env); + } + } + + public function addMiddlewares(array $middlewares, $env = null) + { + $prefix = 'Mautic\\Middleware\\'; + + if ($env) { + $prefix .= ucfirst($env) . '\\'; + } + foreach ($middlewares as $middleware) { - $this->push('Mautic\\Middleware\\' . basename(substr($middleware, 0, -4))); + $this->push($prefix . basename(substr($middleware, 0, -4))); } } diff --git a/app/middlewares/SetMauticHeaderMiddleware.php b/app/middlewares/Prod/SetMauticHeaderMiddleware.php similarity index 91% rename from app/middlewares/SetMauticHeaderMiddleware.php rename to app/middlewares/Prod/SetMauticHeaderMiddleware.php index ddb854a2353..b4654281122 100644 --- a/app/middlewares/SetMauticHeaderMiddleware.php +++ b/app/middlewares/Prod/SetMauticHeaderMiddleware.php @@ -8,14 +8,15 @@ * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html */ -namespace Mautic\Middleware; +namespace Mautic\Middleware\Prod; +use Mautic\Middleware\PrioritizedMiddlewareInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\HttpKernelInterface; class SetMauticHeaderMiddleware implements HttpKernelInterface, PrioritizedMiddlewareInterface { - const PRIORITY = 2; + const PRIORITY = 20; /** * @var HttpKernelInterface diff --git a/app/middlewares/VersionCheckMiddleware.php b/app/middlewares/VersionCheckMiddleware.php new file mode 100644 index 00000000000..16620a354a6 --- /dev/null +++ b/app/middlewares/VersionCheckMiddleware.php @@ -0,0 +1,66 @@ +app = $app; + } + + /** + * Check Minimum / Maximum PHP versions + * + * {@inheritdoc} + */ + public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true) + { + // Are we running the minimum version? + if (version_compare(PHP_VERSION, self::MAUTIC_MINIMUM_PHP, 'lt')) { + return new Response('Your server does not meet the minimum PHP requirements. Mautic requires PHP version '.self::MAUTIC_MINIMUM_PHP.' while your server has '.PHP_VERSION.'. Please contact your host to update your PHP installation.', 500); + } + + // Are we running a version newer than what Mautic supports? + if (version_compare(PHP_VERSION, self::MAUTIC_MAXIMUM_PHP, 'gt')) { + return new Response('Mautic does not support PHP version '.PHP_VERSION.' at this time. To use Mautic, you will need to downgrade to an earlier version.', 500); + } + + return $this->app->handle($request, $type, $catch); + } + + /** + * {@inheritdoc} + */ + public function getPriority() + { + return self::PRIORITY; + } +} diff --git a/composer.json b/composer.json index 6b893c18c4f..090d64c08c2 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ ] }, "require": { - "php": "~5.4.9", + "php": "~5.6.19", "symfony/console": "~2.8", "symfony/debug": "~2.8", @@ -30,6 +30,7 @@ "symfony/yaml": "~2.8", "symfony/property-access": "~2.8", "symfony/dom-crawler": "~2.8", + "symfony/browser-kit": "~2.8", "symfony/asset": "~2.7", "symfony/class-loader": "~2.1", diff --git a/composer.lock b/composer.lock index 7ce8b49ac27..7045735fa7a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "28bb3028862d81c7961efe46765610bb", - "content-hash": "3410db9f9aea70d37e46555fb0035945", + "hash": "93ea990ffe0891415466af8d4fef977b", + "content-hash": "1f194860265fb0777112651a3e4181c4", "packages": [ { "name": "aws/aws-sdk-php", @@ -3302,6 +3302,63 @@ "homepage": "https://symfony.com", "time": "2016-03-07 14:04:32" }, + { + "name": "symfony/browser-kit", + "version": "v2.8.6", + "source": { + "type": "git", + "url": "https://github.com/symfony/browser-kit.git", + "reference": "745c19467255cf32eaf311f000eecafd83ca5586" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/browser-kit/zipball/745c19467255cf32eaf311f000eecafd83ca5586", + "reference": "745c19467255cf32eaf311f000eecafd83ca5586", + "shasum": "" + }, + "require": { + "php": ">=5.3.9", + "symfony/dom-crawler": "~2.0,>=2.0.5|~3.0.0" + }, + "require-dev": { + "symfony/css-selector": "~2.0,>=2.0.5|~3.0.0", + "symfony/process": "~2.3.34|~2.7,>=2.7.6|~3.0.0" + }, + "suggest": { + "symfony/process": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.8-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\BrowserKit\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony BrowserKit Component", + "homepage": "https://symfony.com", + "time": "2016-03-04 07:54:35" + }, { "name": "symfony/class-loader", "version": "v2.8.6", @@ -6745,16 +6802,16 @@ }, { "name": "sebastian/environment", - "version": "1.3.6", + "version": "1.3.7", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/environment.git", - "reference": "2292b116f43c272ff4328083096114f84ea46a56" + "reference": "4e8f0da10ac5802913afc151413bc8c53b6c2716" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/2292b116f43c272ff4328083096114f84ea46a56", - "reference": "2292b116f43c272ff4328083096114f84ea46a56", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/4e8f0da10ac5802913afc151413bc8c53b6c2716", + "reference": "4e8f0da10ac5802913afc151413bc8c53b6c2716", "shasum": "" }, "require": { @@ -6791,7 +6848,7 @@ "environment", "hhvm" ], - "time": "2016-05-04 07:59:13" + "time": "2016-05-17 03:18:57" }, { "name": "sebastian/exporter", @@ -7046,63 +7103,6 @@ "description": "This bundle generates code for you", "time": "2015-03-17 06:36:52" }, - { - "name": "symfony/browser-kit", - "version": "v2.8.6", - "source": { - "type": "git", - "url": "https://github.com/symfony/browser-kit.git", - "reference": "745c19467255cf32eaf311f000eecafd83ca5586" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/browser-kit/zipball/745c19467255cf32eaf311f000eecafd83ca5586", - "reference": "745c19467255cf32eaf311f000eecafd83ca5586", - "shasum": "" - }, - "require": { - "php": ">=5.3.9", - "symfony/dom-crawler": "~2.0,>=2.0.5|~3.0.0" - }, - "require-dev": { - "symfony/css-selector": "~2.0,>=2.0.5|~3.0.0", - "symfony/process": "~2.3.34|~2.7,>=2.7.6|~3.0.0" - }, - "suggest": { - "symfony/process": "" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.8-dev" - } - }, - "autoload": { - "psr-4": { - "Symfony\\Component\\BrowserKit\\": "" - }, - "exclude-from-classmap": [ - "/Tests/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Fabien Potencier", - "email": "fabien@symfony.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony BrowserKit Component", - "homepage": "https://symfony.com", - "time": "2016-03-04 07:54:35" - }, { "name": "symfony/web-profiler-bundle", "version": "v2.5.12", @@ -7217,7 +7217,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": "~5.4.9" + "php": "~5.6.19" }, "platform-dev": [] } diff --git a/index.php b/index.php index a5ddaa25b06..d7c847bde23 100644 --- a/index.php +++ b/index.php @@ -6,44 +6,25 @@ * @link http://mautic.org * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html */ - -// Define Mautic's supported PHP versions -define('MAUTIC_MINIMUM_PHP', '5.3.7'); -define('MAUTIC_MAXIMUM_PHP', '5.6.999'); define('MAUTIC_ROOT_DIR', __DIR__); -// Are we running the minimum version? -if (version_compare(PHP_VERSION, MAUTIC_MINIMUM_PHP, '<')) { - echo 'Your server does not meet the minimum PHP requirements. Mautic requires PHP version '.MAUTIC_MINIMUM_PHP.' while your server has '.PHP_VERSION.'. Please contact your host to update your PHP installation.'; - - exit; -} - -// Are we running a version newer than what Mautic supports? -if (version_compare(PHP_VERSION, MAUTIC_MAXIMUM_PHP, '>')) { - echo 'Mautic does not support PHP version '.PHP_VERSION.' at this time. To use Mautic, you will need to downgrade to an earlier version.'; - - exit; -} - // Fix for hosts that do not have date.timezone set, it will be reset based on users settings -date_default_timezone_set ('UTC'); +date_default_timezone_set('UTC'); use Symfony\Component\ClassLoader\ApcClassLoader; -use Symfony\Component\HttpFoundation\Request; +use Mautic\Middleware\MiddlewareBuilder; $loader = require_once __DIR__ . '/app/autoload.php'; -// Use APC for autoloading to improve performance. -// Change 'sf2' to a unique prefix in order to prevent cache key conflicts -// with other applications also using APC. -/* -$apcLoader = new ApcClassLoader('sf2', $loader); -$loader->unregister(); -$apcLoader->register(true); -*/ +/** + * Use APC for autoloading to improve performance. Change 'sf2' to a unique prefix + * in order to prevent cache key conflicts with other applications also using APC. + */ +//$apcLoader = new ApcClassLoader('sf2', $loader); +//$loader->unregister(); +//$apcLoader->register(true); $kernel = new AppKernel('prod', false); $kernel->loadClassCache(); -Stack\run((new \Mautic\Middleware\MiddlewareBuilder)->resolve($kernel)); +Stack\run((new MiddlewareBuilder('prod'))->resolve($kernel)); diff --git a/index_dev.php b/index_dev.php index ce9c0e627a5..abf1a31950d 100644 --- a/index_dev.php +++ b/index_dev.php @@ -5,55 +5,25 @@ * @author Mautic * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html */ - -// Define Mautic's supported PHP versions -define('MAUTIC_MINIMUM_PHP', '5.3.7'); -define('MAUTIC_MAXIMUM_PHP', '5.6.999'); define('MAUTIC_ROOT_DIR', __DIR__); -// Are we running the minimum version? -if (version_compare(PHP_VERSION, MAUTIC_MINIMUM_PHP, '<')) { - echo 'Your server does not meet the minimum PHP requirements. Mautic requires PHP version '.MAUTIC_MINIMUM_PHP.' while your server has '.PHP_VERSION.'. Please contact your host to update your PHP installation.'; - - exit; -} - -// Are we running a version newer than what Mautic supports? -if (version_compare(PHP_VERSION, MAUTIC_MAXIMUM_PHP, '>')) { - echo 'Mautic does not support PHP version '.PHP_VERSION.' at this time. To use Mautic, you will need to downgrade to an earlier version.'; - - exit; -} - // Fix for hosts that do not have date.timezone set, it will be reset based on users settings -date_default_timezone_set ('UTC'); +date_default_timezone_set('UTC'); use Symfony\Component\Debug\Debug; +use Mautic\Middleware\MiddlewareBuilder; -// If you don't want to setup permissions the proper way, just uncomment the following PHP line -// read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information -//umask(0000); - -// This check prevents access to debug front controllers that are deployed by accident to production servers. -// Feel free to remove this, extend it, or make something more sophisticated. -$allowedIps = array('127.0.0.1', 'fe80::1', '::1'); -if (isset($_SERVER['MAUTIC_DEV_HOSTS'])) { - $localIps = explode(' ', $_SERVER['MAUTIC_DEV_HOSTS']); - $allowedIps = array_merge($allowedIps, $localIps); -} +$loader = require_once __DIR__ . '/vendor/autoload.php'; -if (isset($_SERVER['HTTP_CLIENT_IP']) - || isset($_SERVER['HTTP_X_FORWARDED_FOR']) - || !in_array(@$_SERVER['REMOTE_ADDR'], $allowedIps) -) { - header('HTTP/1.0 403 Forbidden'); - exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.'); -} +/** + * If you don't want to setup permissions the proper way, just uncomment the following PHP line + * read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information + */ +//umask(0000); -$loader = require_once __DIR__ . '/vendor/autoload.php'; Debug::enable(); $kernel = new AppKernel('dev', false); $kernel->loadClassCache(); -Stack\run((new \Mautic\Middleware\MiddlewareBuilder)->resolve($kernel)); +Stack\run((new MiddlewareBuilder('dev'))->resolve($kernel));