Edit file File name : class.MemcacheLite.php Content :<?php /** * A2 Optimized: MemcacheLite Class * * class for controlling a mamcached server * * @author Benjamin Cool * @package A2 Optimized cPanel * * */ class MemcacheLite { protected $connection; protected $instance; public function __construct($instance) { global $cpanel; if (php_sapi_name() != 'cli') { $user_info = $cpanel->uapi( 'Variables', 'get_user_information', [ 'name-1' => 'user', ] ); $this->username = $user_info['cpanelresult']['result']['data']['user']; } else { $this->username = get_current_user(); } if (!is_null($instance)) { $this->instance = new MemcachedInstance($instance->id, $instance->user); } $this->connection = null; } public function error($msg, $code = 400) { header("HTTP/1.1 {$code} Bad Request"); echo json_encode((object) ['error' => $msg]); error_log($msg); exit(1); } public function connect($error = true) { error_reporting(E_ERROR); $this->check_instance(); if ($this->is_cagefs()) { if ($this->connection = socket_create(AF_UNIX, SOCK_STREAM, 0)) { if (!socket_connect($this->connection, $this->instance->socket_path)) { if ($error === true) { $this->error("could not connect to the socket {$this->instance->socket_path}"); } return false; } } else { return false; } } else { // Cannot connect via socket_connect to TCP instance. Instead we'll shell out, ask for stats and exit $connection = shell_exec('exec 3<>/dev/tcp/localhost/11211; printf "stats\nquit\n" >&3; cat <&3'); if (!$connection) { return false; } } return true; } public function close() { socket_close($this->connection); } public function flush_all() { $this->check_instance(); if ($this->connection == null) { $this->connect(); } $stats1 = $this->get_stats(); socket_write($this->connection, "flush_all\n"); $line = socket_read($this->connection, 2048, PHP_BINARY_READ); $stats2 = $this->get_stats(); if ($stats2['total_items'] > $stats1['total_items']) { $this->error('Could not flush the Memcached cache'); } return $stats2; } public function reset_stats() { $this->check_instance(); if ($this->connection == null) { $this->connect(); } $stats1 = $this->get_stats(); socket_write($this->connection, "stats reset\n"); $line = socket_read($this->connection, 2048, PHP_BINARY_READ); $stats2 = $this->get_stats(); if ($stats2['cache_hits'] > $stats1['cache_hits']) { $this->error('Could not reset the Memcached stats.'); } return $stats2; } public function start_server() { $this->check_instance(); $memcache_binaries = [ '/usr/bin/memcached', // Salted shared servers '/bin/memcached', // Possible MVPS '/opt/memcached/bin/a2memcached', // Shared servers ]; $success = false; if (is_dir("/opt/memcached/run/{$this->username}")) { foreach ($memcache_binaries as $binary) { if (is_executable($binary) && !$success) { $command = escapeshellcmd("$binary -P {$this->instance->pidfile} -u {$this->username} -a {$this->instance->perms} -m {$this->instance->size} -t {$this->instance->threads} -c {$this->instance->max_connections} -R {$this->instance->sequential_requests} -s {$this->instance->socket_path} -d"); $res = shell_exec($command); $success = true; } } } if (!$this->is_cagefs()) { // Not on cage_fs, check for localhost memcached $running_processes = shell_exec('ps -ax'); $process_array = explode("\n", $running_processes); foreach ($process_array as $process) { if (strpos('memcached -p 11211', $process)) { $success = true; } } } if (!$success) { $this->error('Cannot start Memcached'); } } public function restart_server() { $this->stop_server(); $this->start_server(); } public function stop_server() { $this->check_instance(); if ($this->connect(false)) { $stats = $this->get_stats(false); if (isset($stats['pid']) && is_numeric($stats['pid'])) { $res = shell_exec("kill -9 {$stats['pid']}"); } } } public function get_stats($error = true) { $stats = []; $this->check_instance(); if ($this->is_cagefs()) { $this->connect($error); socket_write($this->connection, "stats\n"); $data = socket_read($this->connection, 2048, PHP_BINARY_READ); } else { $data = shell_exec('exec 3<>/dev/tcp/localhost/11211; printf "stats\nquit\n" >&3; cat <&3'); } $lines = explode("\n", $data); foreach ($lines as $linenum => $line) { if (trim($line) != '') { $arr = explode(' ', $line); if (count($arr) == 3) { $stats[$arr[1]] = trim($arr[2]); } } } return $stats; } public function check_instance() { if (isset($this->instance) && $this->instance != null) { $this->instance->check(); } else { $this->error('A Memcached instance was not given.'); } return false; } public function is_cagefs() { if (empty($this->username)) { $this->username = $this->get_username(); } if (file_exists('/usr/sbin/cagefsctl')) { // Lets check if CageFS is enabled for this user @exec('/usr/bin/selectorctl --user-current --user=' . $this->username, $cout, $cret); if (preg_match('/ERROR/is', implode('', $cout))) { //not cageFS return false; } //is cageFS return true; } //not cageFS return false; } } Save