| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <contact@managewp.com> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
class MWP_ServiceContainer_Production extends MWP_ServiceContainer_Abstract |
| 12 |
{ |
| 13 |
/** |
| 14 |
* @return MWP_WordPress_Context |
| 15 |
*/ |
| 16 |
protected function createWordPressContext() |
| 17 |
{ |
| 18 |
return new MWP_WordPress_Context(); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* @return Symfony_EventDispatcher_EventDispatcherInterface |
| 23 |
*/ |
| 24 |
protected function createEventDispatcher() |
| 25 |
{ |
| 26 |
$dispatcher = new Symfony_EventDispatcher_EventDispatcher(); |
| 27 |
|
| 28 |
$dispatcher->addSubscriber(new MWP_EventListener_PublicRequest_BrandContactSupport($this->getWordPressContext(), $this->getBrand(), $this->getRequestStack())); |
| 29 |
$dispatcher->addSubscriber(new MWP_EventListener_PublicRequest_DisableEditor($this->getWordPressContext(), $this->getBrand())); |
| 30 |
$dispatcher->addSubscriber(new MWP_EventListener_PublicRequest_SetPluginInfo($this->getWordPressContext(), $this->getBrand())); |
| 31 |
$dispatcher->addSubscriber(new MWP_EventListener_PublicRequest_AddConnectionKeyInfo($this->getWordPressContext())); |
| 32 |
$dispatcher->addSubscriber(new MWP_EventListener_PublicRequest_SetHitCounter($this->getWordPressContext(), $this->getHitCounter(), $this->getRequestStack(), $this->getParameter('hit_counter_blacklisted_ips'), $this->getParameter('hit_counter_blacklisted_user_agents'))); |
| 33 |
$dispatcher->addSubscriber(new MWP_EventListener_PublicRequest_AutomaticLogin($this->getWordPressContext(), $this->getNonceManager(), $this->getSigner(), $this->getSigner256(), $this->getConfiguration(), $this->getSessionStore())); |
| 34 |
$dispatcher->addSubscriber(new MWP_EventListener_PublicRequest_CommandListener($this->getWordPressContext(), $this->getSigner(), $this->getConfiguration(), $this->getNonceManager())); |
| 35 |
|
| 36 |
$dispatcher->addSubscriber(new MWP_EventListener_MasterRequest_AuthenticateServiceRequest($this->getConfiguration(), $this->getSigner(),$this->getSigner256(), $this->getWordPressContext())); |
| 37 |
$dispatcher->addSubscriber(new MWP_EventListener_MasterRequest_VerifyConnectionInfo($this->getWordPressContext(), $this->getSigner(), $this->getSigner256())); |
| 38 |
$dispatcher->addSubscriber(new MWP_EventListener_MasterRequest_AuthenticateRequest($this->getConfiguration(), $this->getSigner(), $this->getWordPressContext())); |
| 39 |
$dispatcher->addSubscriber(new MWP_EventListener_MasterRequest_SetErrorHandler($this->getErrorLogger(), $this->getErrorHandler(), $this->getRequestStack(), $this->getResponseCallback(), $this, $this->getParameter('log_errors'), $this->getParameter('fatal_error_reserved_memory_size'))); |
| 40 |
$dispatcher->addSubscriber(new MWP_EventListener_MasterRequest_AttachJsonMessageHandler($this->getLogger(), $this->getJsonMessageHandler())); |
| 41 |
$dispatcher->addSubscriber(new MWP_EventListener_MasterRequest_RemoveUsernameParam()); |
| 42 |
$dispatcher->addSubscriber(new MWP_EventListener_MasterRequest_AuthenticateLegacyRequest($this->getConfiguration())); |
| 43 |
$dispatcher->addSubscriber(new MWP_EventListener_MasterRequest_SetRequestSettings($this->getWordPressContext())); |
| 44 |
$dispatcher->addSubscriber(new MWP_EventListener_MasterRequest_SetCurrentUser($this->getWordPressContext())); |
| 45 |
|
| 46 |
$dispatcher->addSubscriber(new MWP_EventListener_ActionRequest_VerifyNonce($this->getNonceManager())); |
| 47 |
$dispatcher->addSubscriber(new MWP_EventListener_ActionRequest_SetSettings($this->getWordPressContext(), $this->getSystemEnvironment(), $this->getMigration())); |
| 48 |
$dispatcher->addSubscriber(new MWP_EventListener_ActionRequest_LogRequest($this->getLogger())); |
| 49 |
|
| 50 |
$dispatcher->addSubscriber(new MWP_EventListener_ActionException_SetExceptionData($this->getConfiguration())); |
| 51 |
$dispatcher->addSubscriber(new MWP_EventListener_ActionException_MultipartException($this->getParameter('multipart_boundary'))); |
| 52 |
|
| 53 |
$dispatcher->addSubscriber(new MWP_EventListener_ActionResponse_SetActionData()); |
| 54 |
$dispatcher->addSubscriber(new MWP_EventListener_ActionResponse_SetLegacyWebsiteConnectionData($this->getWordPressContext())); |
| 55 |
$dispatcher->addSubscriber(new MWP_EventListener_ActionResponse_ChainState($this)); |
| 56 |
$dispatcher->addSubscriber(new MWP_EventListener_ActionResponse_SetUpdaterLog($this->getUpdaterSkin())); |
| 57 |
$dispatcher->addSubscriber(new MWP_EventListener_ActionResponse_SetLegacyPhpExecutionData()); |
| 58 |
$dispatcher->addSubscriber(new MWP_EventListener_ActionResponse_FetchFiles($this->getParameter('multipart_boundary'))); |
| 59 |
$dispatcher->addSubscriber(new MWP_EventListener_ActionResponse_DownloadFile($this->getParameter('multipart_boundary'))); |
| 60 |
|
| 61 |
$dispatcher->addSubscriber(new MWP_EventListener_MasterResponse_LogResponse($this->getLogger())); |
| 62 |
|
| 63 |
$dispatcher->addSubscriber(new MWP_EventListener_FixCompatibility($this->getWordPressContext())); |
| 64 |
|
| 65 |
$dispatcher->addSubscriber(new MWP_EventListener_EncodeMasterResponse()); |
| 66 |
|
| 67 |
return $dispatcher; |
| 68 |
} |
| 69 |
|
| 70 |
protected function createRequestStack() |
| 71 |
{ |
| 72 |
return new MWP_Worker_RequestStack(); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @return MWP_Action_Registry |
| 77 |
*/ |
| 78 |
protected function createActionRegistry() |
| 79 |
{ |
| 80 |
$mapper = new MWP_Action_Registry(); |
| 81 |
|
| 82 |
$mapper->addDefinition('do_upgrade', new MWP_Action_Definition('mmb_do_upgrade', array('hook_name' => 'wp_loaded', 'hook_priority' => MAX_PRIORITY_HOOK))); |
| 83 |
$mapper->addDefinition('remove_site', new MWP_Action_Definition('mmb_remove_site')); |
| 84 |
$mapper->addDefinition('update_worker', new MWP_Action_Definition('mmb_update_worker_plugin')); |
| 85 |
$mapper->addDefinition('install_addon', new MWP_Action_Definition('mmb_install_addon', array('hook_name' => 'wp_loaded', 'hook_priority' => MAX_PRIORITY_HOOK))); |
| 86 |
$mapper->addDefinition('bulk_action_comments', new MWP_Action_Definition('mmb_bulk_action_comments', array('hook_name' => 'init', 'hook_priority' => 9999))); |
| 87 |
$mapper->addDefinition('add_user', new MWP_Action_Definition('mmb_add_user', array('hook_name' => 'init', 'hook_priority' => 9999))); |
| 88 |
$mapper->addDefinition('execute_php_code', new MWP_Action_Definition('mmb_execute_php_code')); |
| 89 |
$mapper->addDefinition('edit_users', new MWP_Action_Definition('mmb_edit_users', array('hook_name' => 'init', 'hook_priority' => 9999))); |
| 90 |
$mapper->addDefinition('edit_plugins_themes', new MWP_Action_Definition('mmb_edit_plugins_themes', array('hook_name' => 'init', 'hook_priority' => 9999))); |
| 91 |
$mapper->addDefinition('worker_brand', new MWP_Action_Definition('mmb_worker_brand')); |
| 92 |
$mapper->addDefinition('maintenance', new MWP_Action_Definition('mmb_maintenance_mode')); |
| 93 |
$mapper->addDefinition('cleanup_delete', new MWP_Action_Definition('cleanup_delete_worker', array('hook_name' => 'init', 'hook_priority' => 9999))); |
| 94 |
$mapper->addDefinition('get_state', new MWP_Action_Definition(array('MWP_Action_GetState', 'execute'))); |
| 95 |
$mapper->addDefinition('add_site', new MWP_Action_Definition(array('MWP_Action_ConnectWebsite', 'execute'))); |
| 96 |
$mapper->addDefinition('destroy_sessions', new MWP_Action_Definition(array('MWP_Action_DestroySessions', 'execute'))); |
| 97 |
$mapper->addDefinition('clear_transient', new MWP_Action_Definition(array('MWP_Action_ClearTransient', 'execute'))); |
| 98 |
$mapper->addDefinition('get_stats', new MWP_Action_Definition('mwp_get_stats', array('hook_name' => 'wp_loaded', 'hook_priority' => MAX_PRIORITY_HOOK))); |
| 99 |
$mapper->addDefinition('get_components_stats', new MWP_Action_Definition(array('MWP_Action_GetComponentsStats', 'execute'))); |
| 100 |
$mapper->addDefinition('upload_file_action', new MWP_Action_Definition('mmb_upload_file_action')); |
| 101 |
$mapper->addDefinition('download_file_action', new MWP_Action_Definition(array('MWP_Action_DownloadFile', 'execute'))); |
| 102 |
|
| 103 |
// Incremental backup actions |
| 104 |
$mapper->addDefinition('list_files', new MWP_Action_Definition(array('MWP_Action_IncrementalBackup_ListFiles', 'queryFiles'))); |
| 105 |
$mapper->addDefinition('list_directories', new MWP_Action_Definition(array('MWP_Action_IncrementalBackup_ListFiles', 'listDirectories'))); |
| 106 |
$mapper->addDefinition('hash_files', new MWP_Action_Definition(array('MWP_Action_IncrementalBackup_HashFiles', 'execute'))); |
| 107 |
$mapper->addDefinition('fetch_files', new MWP_Action_Definition(array('MWP_Action_IncrementalBackup_FetchFiles', 'execute'))); |
| 108 |
$mapper->addDefinition('list_tables', new MWP_Action_Definition(array('MWP_Action_IncrementalBackup_ListTables', 'listTables'))); |
| 109 |
$mapper->addDefinition('checksum_tables', new MWP_Action_Definition(array('MWP_Action_IncrementalBackup_ChecksumTables', 'execute'))); |
| 110 |
|
| 111 |
// dump_tables is used before 4.1.8 for streaming |
| 112 |
// Note that dump_tables is used for the ActionResponse_FetchFiles listener |
| 113 |
$mapper->addDefinition('dump_tables', new MWP_Action_Definition(array('MWP_Action_IncrementalBackup_StreamTables', 'execute'))); |
| 114 |
|
| 115 |
$mapper->addDefinition('backup_stats', new MWP_Action_Definition(array('MWP_Action_IncrementalBackup_Stats', 'execute'))); |
| 116 |
$mapper->addDefinition('get_table_schema', new MWP_Action_Definition(array('MWP_Action_IncrementalBackup_GetTableSchema', 'execute'))); |
| 117 |
$mapper->addDefinition('get_view_schema', new MWP_Action_Definition(array('MWP_Action_IncrementalBackup_GetViewSchema', 'execute'))); |
| 118 |
|
| 119 |
return $mapper; |
| 120 |
} |
| 121 |
|
| 122 |
protected function createUserQuery() |
| 123 |
{ |
| 124 |
return new MWP_WordPress_Query_User($this->getWordPressContext()); |
| 125 |
} |
| 126 |
|
| 127 |
protected function createPostQuery() |
| 128 |
{ |
| 129 |
return new MWP_WordPress_Query_Post($this->getWordPressContext()); |
| 130 |
} |
| 131 |
|
| 132 |
protected function createCommentQuery() |
| 133 |
{ |
| 134 |
return new MWP_WordPress_Query_Comment($this->getWordPressContext()); |
| 135 |
} |
| 136 |
|
| 137 |
protected function createPluginProvider() |
| 138 |
{ |
| 139 |
return new MWP_WordPress_Provider_Plugin($this->getWordPressContext()); |
| 140 |
} |
| 141 |
|
| 142 |
protected function createThemeProvider() |
| 143 |
{ |
| 144 |
return new MWP_WordPress_Provider_Theme($this->getWordPressContext()); |
| 145 |
} |
| 146 |
|
| 147 |
protected function createWorkerBrand() |
| 148 |
{ |
| 149 |
return new MWP_Worker_Brand($this->getWordPressContext()); |
| 150 |
} |
| 151 |
|
| 152 |
protected function createAutoUpdateManager() |
| 153 |
{ |
| 154 |
return new MWP_Updater_AutoUpdateManager($this->getWordPressContext()); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* @return MWP_Updater_UpdateManager |
| 159 |
*/ |
| 160 |
protected function createUpdateManager() |
| 161 |
{ |
| 162 |
return new MWP_Updater_UpdateManager($this->getWordPressContext()); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* @return MWP_Signer_Interface |
| 167 |
*/ |
| 168 |
public function createSigner() |
| 169 |
{ |
| 170 |
if ($this->getSystemEnvironment()->isOpenSslLibraryEnabled()) { |
| 171 |
return MWP_Signer_Factory::createOpenSslSigner(); |
| 172 |
} |
| 173 |
|
| 174 |
return MWP_Signer_Factory::createPhpSecLibSigner(); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* @return MWP_Crypter_Interface |
| 179 |
*/ |
| 180 |
public function createCrypter() |
| 181 |
{ |
| 182 |
if ($this->getSystemEnvironment()->isOpenSslLibraryEnabled()) { |
| 183 |
return MWP_Crypter_Factory::createOpenSslCrypter(); |
| 184 |
} |
| 185 |
|
| 186 |
return MWP_Crypter_Factory::createPhpSecLibCrypter(); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* @return MWP_Security_NonceManager |
| 191 |
*/ |
| 192 |
protected function createNonceManager() |
| 193 |
{ |
| 194 |
return new MWP_Security_NonceManager($this->getWordPressContext()); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* @return MWP_Worker_Configuration |
| 199 |
*/ |
| 200 |
protected function createConfiguration() |
| 201 |
{ |
| 202 |
return new MWP_Worker_Configuration($this->getWordPressContext()); |
| 203 |
} |
| 204 |
|
| 205 |
private function createLogStream($file) |
| 206 |
{ |
| 207 |
$filePath = dirname(__FILE__) . '/../../../' . $file; |
| 208 |
|
| 209 |
if (!is_writable(dirname($filePath))) { |
| 210 |
return false; |
| 211 |
} |
| 212 |
|
| 213 |
$logFile = @fopen($filePath, 'a'); |
| 214 |
|
| 215 |
if ($logFile === false) { |
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
return $logFile; |
| 220 |
} |
| 221 |
|
| 222 |
protected function createLogger() |
| 223 |
{ |
| 224 |
$handlers = array(); |
| 225 |
|
| 226 |
$fileLogging = $this->getParameter('log_file'); |
| 227 |
$gelfLogging = $this->getParameter('gelf_server'); |
| 228 |
|
| 229 |
if ($fileLogging || $gelfLogging) { |
| 230 |
$logStart = $this->getParameter('log_start'); |
| 231 |
|
| 232 |
// Save when the first log started |
| 233 |
if (!$logStart) { |
| 234 |
$parameters = $this->getWordPressContext()->optionGet('mwp_container_parameters'); |
| 235 |
|
| 236 |
$parameters['log_start'] = time(); |
| 237 |
|
| 238 |
$this->getWordPressContext()->optionSet('mwp_container_parameters', $parameters); |
| 239 |
} |
| 240 |
|
| 241 |
// Logs can only go on for two days, always delete them after that |
| 242 |
if ($logStart && time() - $logStart > 172800) { |
| 243 |
// delete log file and disable logging |
| 244 |
if ($fileLogging && @is_file(dirname(__FILE__) . '/../../../' . $fileLogging)) { |
| 245 |
@unlink(dirname(__FILE__) . '/../../../' . $fileLogging); |
| 246 |
} |
| 247 |
$parameters = $this->getWordPressContext()->optionGet('mwp_container_parameters'); |
| 248 |
|
| 249 |
$fileLogging = null; |
| 250 |
$gelfLogging = null; |
| 251 |
|
| 252 |
$parameters['gelf_server'] = null; |
| 253 |
$parameters['log_file'] = null; |
| 254 |
$parameters['log_start'] = false; |
| 255 |
|
| 256 |
$this->getWordPressContext()->optionSet('mwp_container_parameters', $parameters); |
| 257 |
} |
| 258 |
} elseif ($fileLogging && @is_file(dirname(__FILE__) . '/../../../' . $fileLogging)) { |
| 259 |
@unlink(dirname(__FILE__) . '/../../../' . $fileLogging); |
| 260 |
} |
| 261 |
|
| 262 |
if ($fileLogging && ($logFile = $this->createLogStream($fileLogging))) { |
| 263 |
$fileHandler = new Monolog_Handler_StreamHandler($logFile); |
| 264 |
$fileHandler->setFormatter(new Monolog_Formatter_HtmlFormatter()); |
| 265 |
$handlers[] = $fileHandler; |
| 266 |
} |
| 267 |
|
| 268 |
if ($gelfLogging) { |
| 269 |
$publisher = $this->getGelfPublisher(); |
| 270 |
$handlers[] = new Monolog_Handler_LegacyGelfHandler($publisher); |
| 271 |
} |
| 272 |
|
| 273 |
$processors = array(); |
| 274 |
if (count($handlers) > 0) { |
| 275 |
// We do have some loggers set up. |
| 276 |
$processors += array( |
| 277 |
array(new Monolog_Processor_MemoryUsageProcessor(), 'callback'), |
| 278 |
array(new Monolog_Processor_MemoryPeakUsageProcessor(), 'callback'), |
| 279 |
array(new Monolog_Processor_IntrospectionProcessor(), 'callback'), |
| 280 |
array(new Monolog_Processor_PsrLogMessageProcessor(), 'callback'), |
| 281 |
array(new Monolog_Processor_UidProcessor(), 'callback'), |
| 282 |
array(new Monolog_Processor_WebProcessor(), 'callback'), |
| 283 |
array(new MWP_Monolog_Processor_TimeUsageProcessor(), 'callback'), |
| 284 |
array(new MWP_Monolog_Processor_ExceptionProcessor(), 'callback'), |
| 285 |
array(new MWP_Monolog_Processor_ProcessProcessor(), 'callback'), |
| 286 |
array(new MWP_Monolog_Processor_RequestIdProcessor($this->getParameter('request_id')), 'callback'), |
| 287 |
); |
| 288 |
} |
| 289 |
// Always push this handler, because Monolog attaches an STDERR handler if there's no other present. |
| 290 |
$handlers[] = new Monolog_Handler_NullHandler(1000); |
| 291 |
|
| 292 |
$logger = new Monolog_Logger('worker', $handlers, $processors); |
| 293 |
|
| 294 |
return $logger; |
| 295 |
} |
| 296 |
|
| 297 |
protected function createResponseCallback() |
| 298 |
{ |
| 299 |
return new MWP_Worker_ResponseCallback(); |
| 300 |
} |
| 301 |
|
| 302 |
protected function createErrorHandler() |
| 303 |
{ |
| 304 |
return new Monolog_ErrorHandler($this->getErrorLogger()); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* @return Symfony_Process_ExecutableFinder |
| 309 |
*/ |
| 310 |
protected function createExecutableFinder() |
| 311 |
{ |
| 312 |
$finder = new MWP_Process_ExecutableFinder(); |
| 313 |
|
| 314 |
$db = $this->getWordPressContext()->getDb(); |
| 315 |
|
| 316 |
if (is_callable(array($db, 'get_var'))) { |
| 317 |
$basePath = rtrim($db->get_var('select @@basedir'), '/\\'); |
| 318 |
if ($basePath) { |
| 319 |
$basePath .= '/bin'; |
| 320 |
$finder->addExtraDir($basePath); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
return $finder; |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* @return MWP_Extension_HitCounter |
| 329 |
*/ |
| 330 |
protected function createHitCounter() |
| 331 |
{ |
| 332 |
$counter = new MWP_Extension_HitCounter($this->getWordPressContext(), 60); |
| 333 |
|
| 334 |
return $counter; |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* @return MWP_Monolog_Handler_JsonMessageHandler |
| 339 |
*/ |
| 340 |
protected function createJsonMessageHandler() |
| 341 |
{ |
| 342 |
$handler = new MWP_Monolog_Handler_JsonMessageHandler($this->getParameter('message_minimum_level')); |
| 343 |
$handler->setPadLength($this->getParameter('message_pad_length')); |
| 344 |
|
| 345 |
return $handler; |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* @return Monolog_Logger |
| 350 |
*/ |
| 351 |
protected function createErrorLogger() |
| 352 |
{ |
| 353 |
return $this->getLogger(); |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* @return MWP_System_Environment |
| 358 |
*/ |
| 359 |
protected function createSystemEnvironment() |
| 360 |
{ |
| 361 |
return new MWP_System_Environment($this); |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* @return MWP_Updater_TraceableUpdaterSkin |
| 366 |
*/ |
| 367 |
protected function createUpdaterSkin() |
| 368 |
{ |
| 369 |
return new MWP_Updater_TraceableUpdaterSkin(); |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* @return MWP_WordPress_SessionStore |
| 374 |
*/ |
| 375 |
protected function createSessionStore() |
| 376 |
{ |
| 377 |
return new MWP_WordPress_SessionStore($this->getWordPressContext()); |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* @return MWP_Migration_Migration |
| 382 |
*/ |
| 383 |
protected function createMigration() |
| 384 |
{ |
| 385 |
return new MWP_Migration_Migration($this->getWordPressContext()); |
| 386 |
} |
| 387 |
|
| 388 |
protected function createGelfPublisher() |
| 389 |
{ |
| 390 |
$server = $this->getParameter('gelf_server'); |
| 391 |
$port = $this->getParameter('gelf_port'); |
| 392 |
$fallbackPort = $this->getParameter('gelf_port_fallback'); |
| 393 |
|
| 394 |
return new Gelf_Publisher($server, $port, $fallbackPort); |
| 395 |
} |
| 396 |
} |
| 397 |
|