View file File name : class.A2OptJoomla.php Content :<?php class A2OptJoomla extends A2OptBase { public $app; public $db_connection; public $db_server; public $db_prefix; public $db_name; public $db_user; public $db_passwd; public $memcached_socket; public $db_port; public $db_type; public $pdo; public $version; public $cache_handler; public function __construct($app) { $this->exit_on_error = false; $this->pdo = null; $this->db_connection = new MySQLi(); $this->db_name = ''; $this->db_server = ''; $this->db_prefix = ''; $this->db_user = ''; $this->db_type = ''; $this->db_port = '3306'; $this->db_password = ''; $this->app = $app; $this->path = $app->path; $this->domain = $app->domain; $this->type = 'Joomla'; $this->db_connect(); if (is_null($this->db_connection)) { $this->log_action('Unable to connect to DB ' . $this->path); $this->type = null; return; } $this->title = $this->check_app_title($this->get_variable('sitename'), $app); $this->version = $this->get_joomla_version(); $this->set_instance(); if (isset($this->instance->socket)) { if(version_compare($this->version, '2.5', '>=') && version_compare($this->version, '3.0', '<')){ $this->cache_handler = 'memcached'; $this->memcached_socket = $this->instance->socket_path; } if(version_compare($this->version, '3.0', '>=') && version_compare($this->version, '3.6', '<=')){ $this->cache_handler = 'memcache'; $this->memcached_socket = $this->instance->socket; } if(version_compare($this->version, '3.7', '>=') && version_compare($this->version, '4.0', '<')){ $this->cache_handler = 'memcached'; $this->memcached_socket = $this->instance->socket_path; } if(version_compare($this->version, '4.0', '>=')){ $this->cache_handler = 'memcached'; $this->memcached_socket = $this->instance->socket; } } $this->exit_on_error = true; } public function get_joomla_version() { $version = "2.5"; // default $possible_locations = [ '/libraries/cms/version/version.php', '/libraries/src/version/Version.php', '/libraries/src/Version.php', ]; foreach($possible_locations as $loc){ if(file_exists($this->path . $loc)){ $file_contents = file_get_contents($this->path . $loc); $lines = explode("\n", $file_contents); if($loc == '/libraries/cms/version/version.php'){ // Version 2.x foreach($lines as $line){ if(stripos($line, 'public $RELEASE =') !== false){ $version = str_replace(['public $RELEASE = ', ';', "'"], '', $line); $version = trim($version); } } } else { // Version 3.x / 4.x $major_version = ''; $minor_version = ''; foreach($lines as $line){ if(stripos($line, 'const MAJOR_VERSION =') !== false){ $major_version = str_replace(['const MAJOR_VERSION = ', ';'], '', $line); $major_version = trim($major_version); } if(stripos($line, 'const MINOR_VERSION =') !== false){ $minor_version = str_replace(['const MINOR_VERSION = ', ';'], '', $line); $minor_version = trim($minor_version); } } $version = $major_version . '.' . $minor_version; } } } return (string)$version; } public function enable_memcached() { //Joomla! $config_file = "{$this->path}/configuration.php"; if (!file_exists($config_file)) { $this->error('The Joomla! configuration.php file is not in the usual location.'); } if (!is_writable($config_file)) { chmod($config_file, 664); if (!is_writable($config_file)) { $this->error('The web server does not have write access to the Joomla! configuration.php file'); } } if(version_compare($this->version, '2.5', '>=') && version_compare($this->version, '3.0', '<')){ // 2.5 -> 2.9 $this->set_variable('caching', '2'); $this->set_variable('memcache_persist', '1'); $this->set_variable('memcache_compress', '1'); $this->set_variable('cachetime', '15'); $this->set_variable('cache_handler', 'memcached'); $this->set_variable('session_handler', 'memcached'); } if(version_compare($this->version, '3.0', '>=') && version_compare($this->version, '3.6', '<=')){ // 3.0 -> 3.6 $this->set_variable('caching', '2'); $this->set_variable('memcache_persist', '1'); $this->set_variable('memcache_compress', '1'); $this->set_variable('cachetime', '15'); $this->set_variable('cache_handler', 'memcache'); $this->set_variable('session_handler', 'memcache'); } if(version_compare($this->version, '3.7', '>=') && version_compare($this->version, '4.0', '<')){ // 3.7+ $this->set_variable('caching', '2'); $this->set_variable('memcache_persist', '1'); $this->set_variable('memcache_compress', '1'); $this->set_variable('cachetime', '15'); $this->set_variable('cache_handler', 'memcached'); $this->set_variable('session_handler', 'memcached'); $this->memcached_socket = $this->instance->socket_path; } if(version_compare($this->version, '4.0', '>=')){ // 4.0+ $this->set_variable('caching', 2, '', true); $this->set_variable('memcache_persist', 1, '', true); $this->set_variable('memcache_compress', 1, '', true); $this->set_variable('cachetime', 15, '', true); $this->set_variable('cache_handler', 'memcached'); $this->set_variable('session_handler', 'memcached'); } $this->set_variable('session_memcache_server_host', $this->memcached_socket, 'session_memcached_server_host'); $this->set_variable('session_memcache_server_port', '0', 'session_memcached_server_port'); $this->set_variable('memcache_server_host', $this->memcached_socket, 'memcached_server_host'); $this->set_variable('memcache_server_port', '0', 'memcached_server_port'); } public function disable_memcached() {//Joomla $config_file = "{$this->path}/configuration.php"; if (!file_exists($config_file)) { $this->error('The Joomla! configuration.php file is not in the usual location.'); } if (!is_writable($config_file)) { chmod($config_file, 664); if (!is_writable($config_file)) { $this->error('The web server does not have write access to the Joomla! configuration.php file'); } } $config = file_get_contents("{$this->path}/configuration.php"); $config = preg_replace(array( "/[^;]*session_memcached?_server_host[^;]*;\n?/", "/[^;]*session_memcached?_server_port[^;]*;\n?/", "/[^;]*cache_handler[^;]*;\n?/", "/[^;]*memcached?_server_host[^;]*;\n?/", "/[^;]*memcached?_server_port[^;]*;\n?/", "/[^;]*memcached?_persist[^;]*;\n?/", "/[^;]*memcached?_compress[^;]*;\n?/", "/[^;]*cachetime[^;]*;\n?/", "/[^;]*session_handler[^;]*;\n?/", "/[^;]*caching[^;]*;\n?/", ), "\n", $config); if(version_compare($this->version, '2.5', '>=') && version_compare($this->version, '4.0', '<')){ $new_params = <<<CODE ; public \$caching = '0'; public \$cache_handler = 'file'; public \$cachetime = '15'; public \$session_handler = 'none'; } CODE; } if(version_compare($this->version, '4.0', '>=')){ $new_params = <<<CODE ; public \$caching = 0; public \$cache_handler = 'file'; public \$cachetime = '15'; public \$session_handler = 'none'; } CODE; } $config = str_replace(array(";\n}",';}'), $new_params, $config); $fp = fopen($config_file, 'w'); fwrite($fp, trim($config)); fclose($fp); } public function is_memcached_enabled() { if ($cache_handler = $this->get_variable('cache_handler')) { if ($cache_handler == $this->cache_handler) { return true; } } return false; } public function is_extension_enabled($name) { $table = $this->get_table('extensions'); $pdo = $this->get_pdo(); $stmt = $pdo->prepare("select enabled from {$table} where name = ?"); $stmt->execute(array($name)); if ($stmt->rowCount() >= 1) { $extension = $stmt->fetchObject(); if ( isset( $extension->enabled ) ) { return $extension->enabled == 1; } } return false; } public function is_extension_installed($name) { $table = $this->get_table('extensions'); $pdo = $this->get_pdo(); $stmt = $pdo->prepare("select enabled from {$table} where name = ?"); $stmt->execute(array($name)); if ($stmt->rowCount() >= 1) { return true; } return false; } public function enable_extension($name) { $table = $this->get_table('extensions'); $pdo = $this->get_pdo(); $stmt = $pdo->prepare("update {$table} set enabled=1 where name = ?"); $stmt->execute(array($name)); if ($stmt->rowCount() >= 1) { return(true); } else { error_log('Extension query failed'); } return false; } public function disable_extension($name) { $table = $this->get_table('extensions'); $pdo = $this->get_pdo(); $stmt = $pdo->prepare("update {$table} set enabled=0 where name = ?"); $stmt->execute(array($name)); if ($stmt->rowCount() >= 1) { return(true); } else { error_log('Extension query failed'); } return false; } public function set_extension_params($name, $params) { $table = $this->get_table('extensions'); $pdo = $this->get_pdo(); $stmt = $pdo->prepare("select params from {$table} where name = ?"); $stmt->execute(array($name)); if ($stmt->rowCount() >= 1) { $extension = $stmt->fetchObject(); if ( isset( $extension->params ) ) { $current_params = json_decode($extension->params, true); $new_params = array_merge($current_params, $params); $new_json_params = json_encode($new_params); $stmt = $pdo->prepare("update {$table} set params=? where name = ?"); $stmt->execute(array($new_json_params,$name)); if ($stmt->rowCount() == 1) { return true; } } } else { error_log('Extension query failed'); } return false; } public function get_extension_params($name) { $table = $this->get_table('extensions'); $pdo = $this->get_pdo(); $stmt = $pdo->prepare("select params from {$table} where name = ?"); $stmt->execute(array($name)); if ($stmt->rowCount() >= 1) { $extension = $stmt->fetchObject(); if (isset($extension->params)) { return json_decode($extension->params, true); } } return array(); } public function install_extension($location, $name) { if ( !$this->is_extension_installed($name) ) { $cmd = __DIR__ . "/../../joomla/install-extension --package={$location}"; $this->su_exec($cmd, $this->path); } if ($res == 'Extension successfully installed') { return true; } else { return false; } } public function install_jch() { if(version_compare($this->version, '3.7', '>=')){ return $this->install_extension(__DIR__ . '/../../joomla/pkg_jchoptimize_1.7.0.zip', 'PLG_SYSTEM_JCH_OPTIMIZE'); } else { return $this->install_extension(__DIR__ . '/../../joomla/jch_optimize.zip', 'PLG_SYSTEM_JCH_OPTIMIZE'); } } public function is_jch_installed() { return $this->is_extension_installed('PLG_SYSTEM_JCH_OPTIMIZE'); } public function enable_jch() { if (!$this->is_jch_enabled()) { if ( !$this->is_jch_installed() ) { $this->install_jch(); } $this->enable_extension('PLG_SYSTEM_JCH_OPTIMIZE'); } } public function is_jch_enabled() { return $this->is_extension_enabled('PLG_SYSTEM_JCH_OPTIMIZE'); } public function disable_jch() { if ($this->is_jch_enabled()) { //Undo Minimum settings to no settings $this->set_extension_params('PLG_SYSTEM_JCH_OPTIMIZE', json_decode('{"css":"0","javascript":"0","gzip":"0","css_minify":"0","js_minify":"0","html_minify":"0","defer_js":"0","bottom_js":"0","html_minify_level":"0","lifetime":"30","excludeAllExtensions":"1","jchmenu":"101","htaccess":"2","debug":"0","log":"0","try_catch":"0","csg_enable":"0","csg_direction":"vertical","csg_wrap_images":"0"}', true)); $this->disable_extension('PLG_SYSTEM_JCH_OPTIMIZE'); } return true; } public function set_minimal_jch() { $this->enable_jch(); $this->set_extension_params('PLG_SYSTEM_JCH_OPTIMIZE', json_decode('{"css":"1","javascript":"1","gzip":"0","css_minify":"0","js_minify":"0","html_minify":"0","defer_js":"0","bottom_js":"0","html_minify_level":"0","lifetime":"30","excludeAllExtensions":"1","jchmenu":"101","htaccess":"2","debug":"0","log":"0","try_catch":"0","csg_enable":"0","csg_direction":"vertical","csg_wrap_images":"0"}', true)); } public function jch_level() { $level = 0; $params = $this->get_extension_params('PLG_SYSTEM_JCH_OPTIMIZE'); if ($params['css'] == '1' && $params['javascript'] == '1') { $level = 1; if ($params['gzip'] == '1' && $params['css_minify'] == '1' && $params['js_minify'] == '1' && $params['html_minify'] == '1') { $level = 2; if ($params['excludeAllExtensions'] == '0') { $level = 3; } } } //level 0 off //level 1 minimal //level 2 intermediate //level 3 average return $level; } public function set_intermediate_jch() { $this->enable_jch(); $this->set_extension_params('PLG_SYSTEM_JCH_OPTIMIZE', json_decode('{"css":"1","javascript":"1","gzip":"1","css_minify":"1","js_minify":"1","html_minify":"1","defer_js":"0","bottom_js":"0","html_minify_level":"0","lifetime":"30","excludeAllExtensions":"1","jchmenu":"101","htaccess":"0","debug":"0","log":"0","try_catch":"0","csg_enable":"0","csg_direction":"vertical","csg_wrap_images":"0"}', true)); } public function set_average_jch() { $this->enable_jch(); $this->set_extension_params('PLG_SYSTEM_JCH_OPTIMIZE', json_decode('{"css":"1","javascript":"1","gzip":"1","css_minify":"1","js_minify":"1","html_minify":"1","defer_js":"0","bottom_js":"0","html_minify_level":"0","lifetime":"30","excludeAllExtensions":"0","jchmenu":"101","htaccess":"0","debug":"0","log":"0","try_catch":"0","csg_enable":"0","csg_direction":"vertical","csg_wrap_images":"0"}', true)); } private function get_pdo() { if ( isset($this->db_server) && !empty($this->db_server) && isset($this->db_passwd) && !empty($this->db_passwd) && isset($this->db_user) && !empty($this->db_user) && isset($this->db_name) && !empty($this->db_name) ) { $this->db_connection = new MySQLi($this->db_server, $this->db_user, $this->db_passwd, $this->db_name); if ($this->db_connection->connect_errno) { return null; } else { try { return new A2OptPDO($this->db_server, $this->db_user, $this->db_passwd, $this->db_name); } catch ( PDOException $Exception ) { $this->error('Could not connect to the database.'); return null; } } } return null; } public function get_table($name) { //using spaces for ordering purposes. //need to replace spaces with proper ordering filter in AngularJS //$pdo = $this->get_pdo(); if ( !( is_null($this->pdo) ) ) { $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; } else { $this->error('Cannot connect to the Joomla! database'); } return false; } public function get_optimizations() { $this_class = $this; $optimizations = array( ' Conservative Caching' => array( 'description' => 'Static full page caching', 'optimized' => (intval($this->get_variable('caching')) >= 1), 'enable' => function () use (&$this_class) { $this_class->set_variable('caching', '1'); $this_class->set_variable('cache_handler', 'file'); }, 'disable' => function () use (&$this_class) { $this_class->set_variable('caching', '0'); }, 'automatic' => true, 'type' => 'variable', ), ' Store Sessions in File' => array( 'description' => 'Store user data in files instead of in the database. If using memcached this option will show as enabled: memcached sessions are stored in RAM, which is faster.', 'optimized' => ($this->get_variable('session_handler') == 'memcache' || $this->get_variable('session_handler') == 'none'), 'enable' => function () use (&$this_class) { if ($this->get_variable('session_handler') != 'memcache') { $this_class->set_variable('session_handler', 'none'); } }, 'disable' => function () use (&$this_class) { $this_class->set_variable('session_handler', 'database'); }, 'automatic' => true, 'type' => 'variable', ), ' Cache Time' => array( 'description' => 'How long should pages be cached. Longer is better for performance.', 'optimized' => (intval($this->get_variable('cachetime')) == 60), 'enable' => function () use (&$this_class) { $this_class->set_variable('cachetime', '60'); }, 'disable' => function () use (&$this_class) { $this_class->set_variable('cachetime', '15'); }, 'enabled_text' => '1 Hour', 'disabled_text' => '15 minutes', 'automatic' => true, 'type' => 'variable', ), ' File Compression' => array( 'description' => 'Enable Gzip compression to reduce the size of files sent to the user.', 'optimized' => ( intval($this->get_variable('gzip')) == 1 ), 'enable' => function () use (&$this_class) { $this_class->set_variable('gzip', '1'); }, 'disable' => function () use (&$this_class) { $this_class->set_variable('gzip', '0'); }, 'automatic' => true, 'type' => 'variable', ), ' Browser Caching' => array( 'description' => 'Enable browser caching of static resources.', 'optimized' => ( $this->is_browser_cache() === true ), 'enable' => function () use (&$this_class) { $this->enable_browser_cache(); }, 'disable' => function () use (&$this_class) { $this->disable_browser_cache(); }, 'type' => 'variable', 'automatic' => true, ), ' Combine JS/CSS Files' => array( 'description' => 'Uses the JCH Optimize Extension to automatically combine JS/CSS Files.', 'optimized' => ($this->jch_level() >= 1), 'enable' => function () use (&$this_class) { $this->set_minimal_jch(); }, 'disable' => function () use (&$this_class) { $this->disable_jch(); }, 'automatic' => true, 'type' => 'plugin', ), ' Minify JS/CSS/HTML Files' => array( 'description' => "Uses the JCH Optimize Extension to automatically minify JS/CSS Files. Requires 'Combine JS/CSS Files'", 'optimized' => ($this->jch_level() >= 2), 'enable' => function () use (&$this_class) { $this->set_intermediate_jch(); }, 'disable' => function () use (&$this_class) { $this->set_minimal_jch(); }, 'automatic' => false, 'type' => 'plugin', ), ' Include Extensions in JS/CSS/HTML Minify' => array( 'description' => "Uses the JCH Optimize Extension to automatically minify JS/CSS Files. Requires 'Combine JS/CSS Files'", 'optimized' => ($this->jch_level() >= 3), 'enable' => function () use (&$this_class) { $this->set_average_jch(); }, 'disable' => function () use (&$this_class) { $this->set_intermediate_jch(); }, 'automatic' => false, 'type' => 'plugin', ), 'Debugging' => array( 'description' => "Disable 'Debug System' and 'Debug Language'.", 'optimized' => (intval($this->get_variable('debug')) + intval($this->get_variable('debug_lang')) === 0), 'enable' => function () use (&$this_class) { $this_class->set_variable('debug', '0'); $this_class->set_variable('debug_lang', '0'); }, 'disable' => function () use (&$this_class) { $this_class->set_variable('debug', '1'); $this_class->set_variable('debug_lang', '1'); }, 'enabled_text' => 'Off', 'disabled_text' => 'On', 'automatic' => true, 'type' => 'variable', ), '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 ) ); if(version_compare($this->version, '4.0', '>=')){ unset($optimizations[' Combine JS/CSS Files']); unset($optimizations[' Minify JS/CSS/HTML Files']); unset($optimizations[' Include Extensions in JS/CSS/HTML Minify']); } return $optimizations; } public function set_optimization($name, $optimize) { $optimizations = $this->get_optimizations(); if (array_key_exists($name, $optimizations)) { if (isset($optimizations[$name]['type'])) { switch ($optimize) { case true: if (isset($optimizations[$name]['enable'])) { $optimizations[$name]['enable'](); } break; case false: if (isset($optimizations[$name]['disable'])) { $optimizations[$name]['disable'](); } break; } } } return $this->get_optimizations(); } 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 is_browser_cache() { $rules = array( 'ExpiresActive On', 'ExpiresByType image/gif "access plus 1 month"', 'ExpiresByType image/jpeg "access plus 1 month"', 'ExpiresByType image/png "access plus 1 month"', 'ExpiresByType text/css "access plus 1 week"', 'ExpiresByType text/javascript "access plus 1 week"', 'ExpiresByType application/javascript "access plus 1 week"', 'ExpiresDefault "access plus 1 week"', ); return $this->check_htaccess_rules($this->get_htaccess(), $rules); } public function enable_browser_cache() { $rules = array( 'ExpiresActive On', 'ExpiresByType image/gif "access plus 1 month"', 'ExpiresByType image/jpeg "access plus 1 month"', 'ExpiresByType image/png "access plus 1 month"', 'ExpiresByType text/css "access plus 1 week"', 'ExpiresByType text/javascript "access plus 1 week"', 'ExpiresByType application/javascript "access plus 1 week"', 'ExpiresDefault "access plus 1 week"', ); $this->add_htaccess_rules($this->get_htaccess_file(), $rules, 'mod_expires.c'); } public function disable_browser_cache() { $rules = array( 'ExpiresActive On', 'ExpiresByType image/gif "access plus 1 month"', 'ExpiresByType image/jpeg "access plus 1 month"', 'ExpiresByType image/png "access plus 1 month"', 'ExpiresByType text/css "access plus 1 week"', 'ExpiresByType text/javascript "access plus 1 week"', 'ExpiresByType application/javascript "access plus 1 week"', 'ExpiresDefault "access plus 1 week"', ); $this->comment_htaccess_rules($this->get_htaccess_file(), $rules); } /*public function get_percent_optimized(){ $count = 0; if($cache_handler = $this->get_variable('cache_handler')){ switch($cache_handler){ case 'memcache': $count += 2; case 'file': $count++; } } if($gzip = $this->get_variable('gzip')){ if($gzip == '1') $count++; } return ($count*100)/3; }*/ public function db_connect() { if (file_exists("{$this->path}/configuration.php")) { $config = file_get_contents("{$this->path}/configuration.php"); if (preg_match('/\$dbtype\s*=\s*[\'"](.*)[\'"]\s*;/', $config, $db_match)) { $this->db_type = array_pop($db_match); } if (preg_match('/\$db\s*=\s*[\'"](.*)[\'"]\s*;/', $config, $db_match)) { $this->db_name = array_pop($db_match); } if (preg_match('/\$user\s*=\s*[\'"](.*)[\'"]\s*;/', $config, $db_match)) { $this->db_user = array_pop($db_match); } if (preg_match('/\$password\s*=\s*[\'"](.*)[\'"]\s*;/', $config, $db_match)) { $this->db_passwd = array_pop($db_match); } if (preg_match('/\$host\s*=\s*[\'"](.*)[\'"]\s*;/', $config, $db_match)) { $this->db_server = array_pop($db_match); } if (preg_match('/\$dbprefix\s*=\s*[\'"](.*)[\'"]\s*;/', $config, $db_match)) { $this->db_prefix = array_pop($db_match); } $this->db_connection = new MySQLi($this->db_server, $this->db_user, $this->db_passwd, $this->db_name); if ($this->db_connection->connect_errno) { $this->error('Could not connect to the Joomla! database.'); $this->db_connection = null; return; } try { $this->pdo = new A2OptPDO($this->db_server, $this->db_user, $this->db_passwd, $this->db_name); } catch (PDOException $e) { $this->error('Could not connect to the Joomla! database.'); $this->db_connection = null; return; } return true; } else { $this->type = null; $this->db_connection = null; } return false; } public function get_variable($name = 'sitename') { $value = false; if (file_exists("{$this->path}/configuration.php")) { $config = file_get_contents("{$this->path}/configuration.php"); if (preg_match("/\\\${$name}\s*=\s*['\\\"](.*)['\\\"]\s*;/", $config, $match)) { $value = $match[1]; } } return $value; } public function set_variable($name, $value, $alternate_name = '', $use_bool = false) { $oldval = $this->get_variable($name); $alt = false; if ($oldval === false && $alternate_name != '') { $oldval = $this->get_variable($alternate_name); if ( !($oldval === false) ) { $alt = true; } } if (!($value === $oldval)) { $config_file = "{$this->path}/configuration.php"; if (file_exists($config_file)) { if (!is_writable($config_file)) { chmod($config_file, 0664); } $config = file_get_contents("{$this->path}/configuration.php"); if (!$alt) { if($use_bool){ $new_config = preg_replace('/\$'.$name.'.*;/', "\$".$name." = ".$value.";", $config); } else { $new_config = preg_replace('/\$'.$name.'.*;/', "\$".$name." = '".$value."';", $config); } } else { if($use_bool){ $new_config = preg_replace('/\$'.$alternate_name.'.*;/', "\$".$name." = ".$value.";", $config); } else { $new_config = preg_replace('/\$'.$alternate_name.'.*;/', "\$".$name." = '".$value."';", $config); } } if ($config == $new_config) { if($use_bool){ $new_config = preg_replace('/}$/', "\tpublic \${$name} = {$value};\n}", $config); } else { $new_config = preg_replace('/}$/', "\tpublic \${$name} = '{$value}';\n}", $config); } } else { $new_config = $this->uncomment_variable($name, $new_config); } if (is_writable($config_file) && $config != $new_config) { copy($config_file, $config_file . '.bak-' . floor(time() / 6) . '.php'); $fh = fopen($config_file, 'w'); fwrite($fh, $new_config); fclose($fh); } } else { return false; } } return true; } public function uncomment_variable($name, $old_config = null) { $config_file = "{$this->path}/configuration.php"; if (file_exists($config_file)) { if (!is_writable($config_file)) { chmod($config_file, 0664); } if ( $old_config === null ) { $config = file_get_contents($config_file); } else { $config = $old_config; } $new_config = preg_replace("/([\/]{2}[\s\/]*)([^\/]*\\\${$name}\s*=\s*['\\\"].*['\\\"]\s*;)/Ux", "\t$2", $config); if ( $old_config === null ) { if ($new_config != $old_config) { if (is_writable($config_file)) { copy($config_file, $config_file . '.bak-' . floor(time() / 6) . '.php'); $fh = fopen($config_file, 'w'); fwrite($fh, $new_config); fclose($fh); } else { $this->error('Cannot edit configuration.php. The file is not writable!'); } return true; } } else { return $new_config; } } return false; } public function comment_variable($name, $old_config = null) { $config_file = "{$this->path}/configuration.php"; if (file_exists($config_file)) { if (!is_writable($config_file)) { chmod($config_file, 0664); } if ( $old_config === null ) { $config = file_get_contents($config_file); } else { $config = $old_config; } $new_config = preg_replace("/(.*\\\${$name}\s*=\s*['\\\"].*['\\\"]\s*;)/", '//$1', $config); if ( $old_config === null ) { if ($new_config != $old_config) { if (is_writable($config_file)) { copy($config_file, $config_file . '.bak-' . floor(time() / 6) . '.php'); $fh = fopen($config_file, 'w'); fwrite($fh, $new_config); fclose($fh); } else { $this->error('Cannot edit configuration.php. The file is not writable!'); } return true; } } else { return $new_config; } } return false; } public function get_turbocache_defaults() { return array( 'reject_urls' => array( 'admin', 'login', 'register' ), 'reject_cookies' => array( 'logged' ), 'time' => '15 Minutes', 'ttl' => 900, 'enabled' => true ); } public function optimize() { $optimizations = $this->get_optimizations(); foreach ($optimizations as $name => $optimization) { if (!$optimization['optimized']) { $this->set_optimization($name, true); } } } }