| 1 |
<?php |
| 2 |
namespace MailPoet\Config; |
| 3 |
|
| 4 |
use MailPoet\Util\Helpers; |
| 5 |
use MailPoet\WP\Notice as WPNotice; |
| 6 |
|
| 7 |
if(!defined('ABSPATH')) exit; |
| 8 |
|
| 9 |
class RequirementsChecker { |
| 10 |
const TEST_FOLDER_PERMISSIONS = 'TempAndCacheFolderCreation'; |
| 11 |
const TEST_PDO_EXTENSION = 'PDOExtension'; |
| 12 |
const TEST_MBSTRING_EXTENSION = 'MbstringExtension'; |
| 13 |
const TEST_XML_EXTENSION = 'XmlExtension'; |
| 14 |
const TEST_ZIP_EXTENSION = 'ZipExtension'; |
| 15 |
const TEST_VENDOR_SOURCE = 'VendorSource'; |
| 16 |
const TWIG_SUPPORTED_VERSIONS = '1.26.0-1.34.4'; |
| 17 |
|
| 18 |
public $display_error_notice; |
| 19 |
public $vendor_classes = array( |
| 20 |
'\ORM', |
| 21 |
'\Model', |
| 22 |
'\Twig_Environment', |
| 23 |
'\Twig_Loader_Filesystem', |
| 24 |
'\Twig_Lexer', |
| 25 |
'\Twig_Extension', |
| 26 |
'\Twig_SimpleFunction', |
| 27 |
'\Swift_Mailer', |
| 28 |
'\Swift_SmtpTransport', |
| 29 |
'\Swift_Message', |
| 30 |
'\Carbon\Carbon', |
| 31 |
'\Sudzy\ValidModel', |
| 32 |
'\Sudzy\ValidationException', |
| 33 |
'\Sudzy\Engine', |
| 34 |
'\pQuery', |
| 35 |
'\Cron\CronExpression', |
| 36 |
'\Html2Text\Html2Text', |
| 37 |
'\csstidy', |
| 38 |
'\Sabberworm\CSS\Parser' |
| 39 |
); |
| 40 |
|
| 41 |
function __construct($display_error_notice = true) { |
| 42 |
$this->display_error_notice = $display_error_notice; |
| 43 |
} |
| 44 |
|
| 45 |
function checkAllRequirements() { |
| 46 |
$available_tests = array( |
| 47 |
self::TEST_PDO_EXTENSION, |
| 48 |
self::TEST_FOLDER_PERMISSIONS, |
| 49 |
self::TEST_MBSTRING_EXTENSION, |
| 50 |
self::TEST_XML_EXTENSION, |
| 51 |
self::TEST_ZIP_EXTENSION, |
| 52 |
self::TEST_VENDOR_SOURCE |
| 53 |
); |
| 54 |
$results = array(); |
| 55 |
foreach($available_tests as $test) { |
| 56 |
$results[$test] = call_user_func(array($this, 'check' . $test)); |
| 57 |
} |
| 58 |
return $results; |
| 59 |
} |
| 60 |
|
| 61 |
function checkTempAndCacheFolderCreation() { |
| 62 |
$paths = array( |
| 63 |
'temp_path' => Env::$temp_path, |
| 64 |
'cache_path' => Env::$cache_path |
| 65 |
); |
| 66 |
if(!is_dir($paths['cache_path']) && !wp_mkdir_p($paths['cache_path'])) { |
| 67 |
$error = Helpers::replaceLinkTags( |
| 68 |
__('MailPoet requires write permissions inside the /wp-content/uploads folder. Please read our [link]instructions[/link] on how to resolve this issue.', 'mailpoet'), |
| 69 |
'//beta.docs.mailpoet.com/article/152-minimum-requirements-for-mailpoet-3#folder_permissions', |
| 70 |
array('target' => '_blank') |
| 71 |
); |
| 72 |
return $this->processError($error); |
| 73 |
} |
| 74 |
foreach($paths as $path) { |
| 75 |
$index_file = $path . '/index.php'; |
| 76 |
if(!file_exists($index_file)) { |
| 77 |
file_put_contents( |
| 78 |
$path . '/index.php', |
| 79 |
str_replace('\n', PHP_EOL, '<?php\n\n// Silence is golden') |
| 80 |
); |
| 81 |
} |
| 82 |
} |
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
function checkPDOExtension() { |
| 87 |
if(extension_loaded('pdo') && extension_loaded('pdo_mysql')) return true; |
| 88 |
$error = Helpers::replaceLinkTags( |
| 89 |
__('MailPoet requires a PDO_MYSQL PHP extension. Please read our [link]instructions[/link] on how to resolve this issue.', 'mailpoet'), |
| 90 |
'//beta.docs.mailpoet.com/article/152-minimum-requirements-for-mailpoet-3#php_extension', |
| 91 |
array('target' => '_blank') |
| 92 |
); |
| 93 |
return $this->processError($error); |
| 94 |
} |
| 95 |
|
| 96 |
function checkMbstringExtension() { |
| 97 |
if(!extension_loaded('mbstring')) { |
| 98 |
require_once Env::$util_path .'/Polyfills.php'; |
| 99 |
} |
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
function checkXmlExtension() { |
| 104 |
if(extension_loaded('xml')) return true; |
| 105 |
$error = Helpers::replaceLinkTags( |
| 106 |
__('MailPoet requires an XML PHP extension. Please read our [link]instructions[/link] on how to resolve this issue.', 'mailpoet'), |
| 107 |
'//beta.docs.mailpoet.com/article/152-minimum-requirements-for-mailpoet-3#php_extension', |
| 108 |
array('target' => '_blank') |
| 109 |
); |
| 110 |
return $this->processError($error); |
| 111 |
} |
| 112 |
|
| 113 |
function checkZipExtension() { |
| 114 |
if(extension_loaded('zip')) return true; |
| 115 |
$error = Helpers::replaceLinkTags( |
| 116 |
__('MailPoet requires a ZIP PHP extension. Please read our [link]instructions[/link] on how to resolve this issue.', 'mailpoet'), |
| 117 |
'//beta.docs.mailpoet.com/article/152-minimum-requirements-for-mailpoet-3#php_extension', |
| 118 |
array('target' => '_blank') |
| 119 |
); |
| 120 |
return $this->processError($error); |
| 121 |
} |
| 122 |
|
| 123 |
function checkVendorSource() { |
| 124 |
foreach($this->vendor_classes as $dependency) { |
| 125 |
$dependency_path = $this->getDependencyPath($dependency); |
| 126 |
if(!$dependency_path) { |
| 127 |
$error = sprintf( |
| 128 |
__('A MailPoet dependency (%s) does not appear to be loaded correctly, thus MailPoet will not work correctly. Please reinstall the plugin.', 'mailpoet'), |
| 129 |
$dependency |
| 130 |
); |
| 131 |
|
| 132 |
return $this->processError($error); |
| 133 |
} |
| 134 |
|
| 135 |
$pattern = '#' . preg_quote(Env::$path) . '[\\\/]#'; |
| 136 |
$is_loaded_by_plugin = preg_match($pattern, $dependency_path); |
| 137 |
if(!$is_loaded_by_plugin) { |
| 138 |
$error = sprintf( |
| 139 |
__('MailPoet has detected a dependency conflict (%s) with another plugin (%s), which may cause unexpected behavior. Please disable the offending plugin to fix this issue.', 'mailpoet'), |
| 140 |
$dependency, |
| 141 |
$dependency_path |
| 142 |
); |
| 143 |
|
| 144 |
$return_error = true; |
| 145 |
|
| 146 |
// if a Twig dependency is loaded by another plugin, check for valid version |
| 147 |
if(strpos($dependency, '\Twig_') === 0) { |
| 148 |
$return_error = ($this->isValidTwigVersion()) ? false : $return_error; |
| 149 |
} |
| 150 |
|
| 151 |
if($return_error) return $this->processError($error); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return true; |
| 156 |
} |
| 157 |
|
| 158 |
function isValidTwigVersion() { |
| 159 |
list($minimum_version, $maximum_version) = explode('-', self::TWIG_SUPPORTED_VERSIONS); |
| 160 |
return ( |
| 161 |
class_exists('\Twig_Environment') && |
| 162 |
defined('\Twig_Environment::VERSION') && |
| 163 |
version_compare(\Twig_Environment::VERSION, $minimum_version, '>=') && |
| 164 |
version_compare(\Twig_Environment::VERSION, $maximum_version, '<=') |
| 165 |
); |
| 166 |
} |
| 167 |
|
| 168 |
private function getDependencyPath($namespaced_class) { |
| 169 |
try { |
| 170 |
$reflector = new \ReflectionClass($namespaced_class); |
| 171 |
return $reflector->getFileName(); |
| 172 |
} catch(\ReflectionException $ex) { |
| 173 |
return false; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
function processError($error) { |
| 178 |
if($this->display_error_notice) { |
| 179 |
WPNotice::displayError($error); |
| 180 |
} |
| 181 |
return false; |
| 182 |
} |
| 183 |
} |