Edit file File name : class.A2OptMagento.php Content :<?php class A2OptMagento extends A2OptBase { public $app; public $db_connection; public $title; public $db_server; public $db_prefix; public $db_name; public $db_user; public $db_passwd; public $memcached_socket; public $db_port; public $pdo; public $is_cagefs; public function __construct($app) { $this->exit_on_error = false; //$this->db_connection = new MySQLi(); //$this->pdo = new PDO(""); $this->db_name = ''; $this->db_server = ''; $this->db_prefix = ''; $this->db_user = ''; $this->db_port = '3306'; $this->db_password = ''; $this->app = $app; $this->path = $app->path; $this->htaccess_file = "{$this->path}/.htaccess"; $this->domain = $app->domain; $this->type = 'Magento'; $this->get_settings(); $this->db_connect(); if (is_null($this->db_connection)) { $this->log_action('Unable to connect to DB ' . $this->path); $this->type = null; return; } $this->get_title(); $this->set_instance(); if (isset($this->instance->socket_path)) { $this->memcached_socket = $this->instance->socket_path; } $this->is_cagefs = $this->is_cagefs(); $this->exit_on_error = true; } public function db_connect() { $this->db_connection = new MySQLi($this->db_server, $this->db_user, $this->db_passwd, $this->db_name); if ($this->db_connection->connect_errno) { $this->db_connection = null; $this->type = null; $this->error('Could not connect to the Magento database.'); return; } try { $this->pdo = new A2OptPDO($this->db_server, $this->db_user, $this->db_passwd, $this->db_name); } catch (PDOException $e) { $this->db_connection = null; $this->type = null; $this->error('Could not connect to the Magento database.'); return; } } public function enable_memcached() {//Magento $local_xml_file = "{$this->path}/app/etc/local.xml"; if (file_exists($local_xml_file)) { $local_xml = file_get_contents($local_xml_file); $local_xml = preg_replace("/<cache>.*<\/cache>/msiU", '', $local_xml); $memcache_config = <<<XML <global> <cache> <backend>memcached</backend> <memcached> <servers> <server> <host><![CDATA[{$this->memcached_socket}]]></host> <port><![CDATA[0]]></port> <persistent><![CDATA[1]]></persistent> </server> </servers> <compression><![CDATA[1]]></compression> <cache_dir><![CDATA[]]></cache_dir> <hashed_directory_level><![CDATA[]]></hashed_directory_level> <hashed_directory_umask><![CDATA[]]></hashed_directory_umask> <file_name_prefix><![CDATA[]]></file_name_prefix> </memcached> </cache> < XML; $local_xml = preg_replace('/<global>[^<]+</msiU', $memcache_config, $local_xml); $this->get_magento_clean_cache(); if ($fp = fopen($local_xml_file, 'w')) { fwrite($fp, $local_xml); fclose($fp); chdir($this->app->path); if ($this->is_cagefs()) { shell_exec('/bin/cagefs_enter.proxied php-cli ./shell/cleanCache.php all'); } else { shell_exec('php-cli ./shell/cleanCache.php all'); } } else { $this->error("{$local_xml_file} is not writable by the web server."); } } else { $this->error("{$local_xml_file} does not exist in your Magento Installation."); } } public function get_table($name = 'core_config_data') { if ($name == '') { return ''; } $var = "{$this->db_prefix}{$name}"; $table = ''; $stmt = $this->pdo->prepare('SHOW Tables LIKE ?'); $stmt->execute(array($var)); if ($res = $stmt->fetchAll()) { $table = $res[0][0]; } else { $var = "%{$name}"; $stmt = $this->pdo->prepare('SHOW Tables LIKE ?'); $stmt->execute(array($var)); if ($res = $stmt->fetchAll()) { $table = $res[0][0]; } } return $table; } public function disable_memcached() { $local_xml_file = "{$this->path}/app/etc/local.xml"; if (file_exists($local_xml_file)) { $local_xml = file_get_contents($local_xml_file); $local_xml = preg_replace("/<cache>.*<\/cache>/msiU", '', $local_xml); $this->get_magento_clean_cache(); if ($fp = fopen($local_xml_file, 'w')) { fwrite($fp, $local_xml); fclose($fp); chdir($this->app->path); if ($this->is_cagefs()) { shell_exec('/bin/cagefs_enter.proxied php-cli ./shell/cleanCache.php all'); } else { shell_exec('php-cli ./shell/cleanCache.php all'); } } else { $this->error("{$local_xml_file} is not writable by the web server."); } } else { $this->error("{$local_xml_file} does not exist in your Magento Installation."); } } public function get_magento_clean_cache() { if (!file_exists("{$this->path}/shell/cleanCache.php")) { if (is_dir($this->path)) { chdir($this->path); $res = file_get_contents('https://raw.githubusercontent.com/akira28/magento-utils/master/shell/cleanCache.php'); if ($fp = fopen("{$this->path}/shell/cleanCache.php", 'w+')) { fwrite($fp, $res); fclose($fp); } } if (!file_exists("{$this->path}/shell/cleanCache.php")) { $this->error("The web server does not have write access to {$this->path}/shell/"); } } } public function is_memcached_enabled() { //$cache_handler = 'memcache'; if (file_exists("{$this->path}/app/etc/local.xml")) { $config = simplexml_load_file("{$this->path}/app/etc/local.xml"); return isset($config->global->cache->memcached->servers->server->host); } return false; } public function get_settings() { if (file_exists("{$this->path}/app/etc/local.xml")) { $config = file_get_contents("{$this->path}/app/etc/local.xml"); $this->db_prefix = ''; $connection = preg_match('/<resources>.*<\/resources>/msiU', $config, $db_match) ? array_pop($db_match) : ''; if (preg_match('/<host><!\[CDATA\[(.*)\]\]><\/host>/', $connection, $db_match)) { $this->db_server = array_pop($db_match); } if (preg_match('/<dbname><!\[CDATA\[(.*)\]\]><\/dbname>/', $connection, $db_match)) { $this->db_name = array_pop($db_match); } if (preg_match('/<username><!\[CDATA\[(.*)\]\]><\/username>/', $connection, $db_match)) { $this->db_user = array_pop($db_match); } if (preg_match('/<password><!\[CDATA\[(.*)\]\]><\/password>/', $connection, $db_match)) { $this->db_passwd = array_pop($db_match); } if (preg_match('/<table_prefix><!\[CDATA\[(.*)\]\]><\/table_prefix>/', $connection, $db_match)) { $this->db_prefix = array_pop($db_match); } } } public function get_title() { $value = ($title = $this->get_variable('design/head/default_title')) ? $title : false; if (!$value && file_exists("{$this->path}/app/code/core/Mage/Page/etc/config.xml") && preg_match("/<default_title>(.*)<\/default_title>/", file_get_contents("{$this->path}/app/code/core/Mage/Page/etc/config.xml"), $match)) { $value = array_pop($match); } $this->title = $this->check_app_title($value, $this->app); } public function get_variable($path = 'design/head/default_title') { $value = null; if ($this->pdo != null) { $table_name = $this->get_table('core_config_data'); if ($table_name != '') { $stmt = $this->pdo->prepare("select value from $table_name where path=?"); $stmt->execute(array($path)); if ($res = $stmt->fetchAll()) { $value = $res[0][0]; } } } return $value; } public function set_variable($name, $value) { if ($this->pdo != null) { $table_name = $this->get_table('core_config_data'); if ($table_name != '') { $current_value = $this->get_variable($name); if ($current_value != null) { $stmt = $this->pdo->prepare("update {$table_name} set value=? where path=?"); $stmt->execute(array($value,$name)); if ($stmt->rowCount() > 0) { return true; } } else { $stmt = $this->pdo->prepare("insert into {$table_name} set value=?,path=?"); $stmt->execute(array($value,$name)); if ($stmt->rowCount() > 0) { return true; } } } } return false; } public function set_optimization($name, $optimize) { $optimizations = $this->get_optimizations(); if (array_key_exists($name, $optimizations)) { if (isset($optimizations[$name]['type'])) { $value = $optimize ? $optimizations[$name]['optimized_value']: $optimizations[$name]['deoptimized_value']; switch ($optimizations[$name]['type']) { case 'core_config': $this->set_variable($optimizations[$name]['variable'], $value); break; case 'advanced': break; case 'function': if ($optimize) { $optimizations[$name]['enable'](); } else { $optimizations[$name]['disable'](); } break; } } } return $this->get_optimizations(); } public function optimize() { $optimizations = $this->get_optimizations(); foreach ($optimizations as $name => $optimization) { if (!$optimization['optimized']) { $this->set_optimization($name, true); } } } public function get_optimizations() { $thisclass = $this; return array( 'Turn Off Logging' => array( 'optimized' => !$this->using_logs(), 'variable' => 'dev/log/active', 'optimized_value' => '0', 'deoptimized_value' => '1', 'type' => 'core_config' ), 'Merge JavaScript' => array( 'optimized' => $this->merging_js(), 'variable' => 'dev/js/merge_files', 'optimized_value' => '1', 'deoptimized_value' => '0', 'type' => 'core_config' ), 'Merge CSS' => array( 'optimized' => $this->merging_css(), 'variable' => 'dev/css/merge_css_files', 'optimized_value' => '1', 'deoptimized_value' => '0', 'type' => 'core_config' ), 'Optimize Cron' => array( 'optimized' => $this->is_cron_optimized(), 'enable' => function () use (&$thisclass) { $this->optimize_cron(); }, 'disable' => function () use (&$thisclass) { $this->deoptimize_cron(); }, 'type' => 'function', 'description' => 'Optimize magento by setting the cron to fire every 30 minutes instead of every 15.' ), 'Force Gzip Compression' => array( 'optimized' => $this->is_mod_deflate(), 'enable' => function () use (&$thisclass) { $thisclass->enable_mod_deflate(); }, 'disable' => function () use (&$thisclass) { $thisclass->disable_mod_deflate(); }, 'type' => 'function', 'description' => 'Enable Gzip Compression using apache mod_deflate' ), 'Add Expires Headers' => array( 'optimized' => $this->is_expires(), 'enable' => function () use (&$thisclass) { $thisclass->enable_expires(); }, 'disable' => function () use (&$thisclass) { $thisclass->disable_expires(); }, 'type' => 'function', 'description' => 'Enable ExpiresDefault for access + 1 week' ), 'Memcached' => array( 'optimized' => $this->is_memcached_enabled(), 'description' => 'Caching of database query results in memory', 'controller' => 'Memcached', 'advanced' => true ), 'TurboCache' => array( 'optimized' => $this->app->swiftcache->turbocache->enabled, 'description' => 'Static file caching by the web Server', 'controller' => 'TurboCache', 'advanced' => true ) ); } public function get_percent_optimized() { $optimizations = $this->get_optimizations(); $count = 0; foreach ($optimizations as $name => $optimization) { if ($optimization['optimized']) { $count ++; } } return round(($count / (count($optimizations))) * 100); } public function merging_js() { return (trim($this->get_variable('dev/js/merge_files')) === '1'); } public function merging_css() { return (trim($this->get_variable('dev/css/merge_css_files')) === '1'); } public function using_logs() { return (trim($this->get_variable('dev/log/active')) === '1'); } public function optimize_cron() { $this->disable_cron('php ' . rtrim($this->app->path, '/') . '/cron.php'); $command = 'php-cli ' . rtrim($this->app->path, '/') . '/cron.php 1>/dev/null 2>/dev/null'; if ($this->is_cagefs) { $command = "/bin/cagefs_enter.proxied $command"; } $minute = rand(0, 29); $minute2 = $minute + 30;//every 30 minutes $this->enable_cron("{$minute},{$minute2}", '*', '*', '*', '*', $command); $this->set_variable('system/cron/schedule_generate_every', 30); $this->set_variable('system/cron/schedule_ahead_for', 1); $this->set_variable('system/cron/schedule_lifetime', 30); $this->set_variable('system/cron/history_cleanup_every', 120); $this->set_variable('system/cron/history_success_lifetime', 120); $this->set_variable('system/cron/history_failure_lifetime', 120); } public function deoptimize_cron() { $this->disable_cron('php ' . rtrim($this->app->path, '/') . '/cron.php'); $command = 'php-cli ' . rtrim($this->app->path, '/') . '/cron.php 1>/dev/null 2>/dev/null'; if ($this->is_cagefs) { $command = "/bin/cagefs_enter.proxied $command"; } $minute = rand(0, 14); $minute2 = $minute + 15;//every 30 minutes $minute3 = $minute2 + 15;//every 30 minutes $minute4 = $minute3 + 15;//every 30 minutes $this->enable_cron("{$minute},{$minute2},{$minute3},{$minute4}", '*', '*', '*', '*', $command); $this->set_variable('system/cron/schedule_generate_every', 15); $this->set_variable('system/cron/schedule_ahead_for', 1); $this->set_variable('system/cron/schedule_lifetime', 15); $this->set_variable('system/cron/history_cleanup_every', 120); $this->set_variable('system/cron/history_success_lifetime', 120); $this->set_variable('system/cron/history_failure_lifetime', 120); } public function is_cron_optimized() { $command = $this->get_cron_command('php ' . rtrim($this->app->path, '/') . '/cron.php'); preg_match('/([0-9]{1,2}),([0-9]{1,2}) \\* /', $command, $matches); if (is_array($matches) && count($matches) > 1) { return (intval($matches[2]) - intval($matches[1]) == 30); } return false; } public function enable_mod_deflate() { $this->add_htaccess_rules($this->htaccess_file, array( 'SetOutputFilter DEFLATE', 'AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript', 'BrowserMatch ^Mozilla/4 gzip-only-text/html', "BrowserMatch ^Mozilla/4\.0[678] no-gzip", "BrowserMatch \bMSIE !no-gzip !gzip-only-text/html", "SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary", 'Header append Vary User-Agent env=!dont-vary' ), 'mod_deflate.c'); } public function disable_mod_deflate() { $this->comment_htaccess_rules($this->htaccess_file, array( 'SetOutputFilter DEFLATE', 'AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript', 'BrowserMatch ^Mozilla/4 gzip-only-text/html', "BrowserMatch ^Mozilla/4\.0[678] no-gzip", "BrowserMatch \bMSIE !no-gzip !gzip-only-text/html", "SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary", 'Header append Vary User-Agent env=!dont-vary')); } public function is_mod_deflate() { if (file_exists($this->htaccess_file)) { $htaccess = file_get_contents($this->htaccess_file); return $this->check_htaccess_rules($htaccess, array( 'SetOutputFilter DEFLATE', 'AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript', 'BrowserMatch ^Mozilla/4 gzip-only-text/html', "BrowserMatch ^Mozilla/4\.0[678] no-gzip", "BrowserMatch \bMSIE !no-gzip !gzip-only-text/html", "SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary", 'Header append Vary User-Agent env=!dont-vary')); } } public function enable_expires() { $this->add_htaccess_rules($this->htaccess_file, array( 'ExpiresActive On', 'ExpiresDefault "access plus 1 week"' ), 'mod_expires.c', array('ExpiresDefault')); } //TODO: make this smarter so that I can look for customer modifications public function disable_expires() { $this->comment_htaccess_rules($this->htaccess_file, array( 'ExpiresActive On', 'ExpiresDefault "access plus 1 week"' )); } public function is_expires() { if (file_exists($this->htaccess_file)) { $htaccess = file_get_contents($this->htaccess_file); return $this->check_htaccess_rules($htaccess, array( 'ExpiresActive On', 'ExpiresDefault "access plus 1 week"' ), true); } } public function get_turbocache_defaults() { return array( 'reject_urls' => array( 'admin', 'login', 'register', 'cart' ), 'reject_cookies' => array( 'logged', ), 'time' => '15 Minutes', 'ttl' => 900, 'enabled' => true ); } } Save