function HyperCacheScanBuildLocalPath($uri, $ID) { $path = HyperCacheRetranslation_get($uri); if ($path != null) { return $path; } $unix = new unix(); $Root = $GLOBALS["HyperCacheStoragePath"]; if (!is_dir($Root)) { @mkdir($Root, 0755, true); @chown($Root, "squid"); @chgrp($Root, "squid"); } $RulePath = "{$Root}/{$ID}"; if (!is_dir($RulePath)) { @mkdir($RulePath, 0755, true); @chown($RulePath, "squid"); @chgrp($RulePath, "squid"); } $H = parse_url($uri); $sitename = $H["host"]; $f = new familysite(); $Family = $f->GetFamilySites($sitename); $finalDir = "{$RulePath}/{$Family}"; if (!is_dir($finalDir)) { @mkdir($finalDir, 0755, true); @chown($finalDir, "squid"); @chgrp($finalDir, "squid"); } $path = $H["path"]; $filename = basename($path); $extension = $unix->file_ext($filename); return "{$ID}/{$Family}/" . md5($uri) . ".{$extension}"; }
function RestoreFromBackup($backuppath) { $unix = new unix(); $PidRestore = "/etc/artica-postfix/pids/zarafaRestore.pid"; $rm = $unix->find_program("rm"); $pid = $unix->get_pid_from_file($PidRestore); if ($unix->process_exists($pid, basename(__FILE__))) { $time = $unix->PROCCESS_TIME_MIN($pid); if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: Restore Task Already running PID {$pid} since {$time}mn\n"; } return; } $pid = $unix->PIDOF_PATTERN("exec.zarafa-db.php --restorefrom"); if ($pid != getmypid()) { if ($unix->process_exists($pid)) { $time = $unix->PROCCESS_TIME_MIN($pid); if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: Restore Task Already running PID {$pid} since {$time}mn\n"; } return; } } $mysql = $unix->find_program("mysql"); $pid = $unix->PIDOF_PATTERN("{$mysql}\\s+.*?--socket=/var/run/mysqld/zarafa-db.sock.*?database=zarafa"); if ($unix->process_exists($pid)) { $time = $unix->PROCCESS_TIME_MIN($pid); if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " [INIT]: Restore Task Already running PID {$pid} since {$time}mn\n"; } return; } @file_put_contents($PidRestore, getmypid()); $sock = new sockets(); $SourceDir = dirname($backuppath); $WORKDIR = $sock->GET_INFO("ZarafaDedicateMySQLWorkDir"); if ($WORKDIR == null) { $WORKDIR = "/home/zarafa-db"; } if (is_file("{$SourceDir}/ldap.ldif")) { RestoreFromBackup_progress("{restore_ldap_database}", 10); RestoreFromBackup_ldap("{$SourceDir}/ldap.ldif"); } $unix = new unix(); if (!is_file($backuppath)) { echo "Action: `{$backuppath}` no such file: ABORT!\n"; RestoreFromBackup_progress("{failed}", 100); return; } echo "Action: Removing Zarafa Database MySQL client `{$mysql}`....\n"; RestoreFromBackup_progress("Removing Zarafa Database", 30); $cmd = "{$mysql} --socket=/var/run/mysqld/zarafa-db.sock --protocol=socket --user=root --batch --debug-info --database=zarafa --execute=\"DROP DATABASE zarafa\" 2>&1"; $results = array(); exec("{$cmd}", $results); while (list($num, $ligne) = each($results)) { echo "MySQL: (Delete Database) {$ligne}\n"; } RestoreFromBackup_progress("Removing all content", 32); if (is_dir("{$WORKDIR}/data/zarafa")) { recursive_remove_directory("{$WORKDIR}"); } RestoreFromBackup_progress("Restarting MySQL service (recovery)", 40); echo "Action: Restarting MySQL service...\n"; echo "Action: Stopping MySQL service...\n"; stop(true); echo "Action: Starting MySQL service (InnoDB recovery mode)...\n"; start(true, true); while (list($num, $ligne) = each($results)) { echo "Service: {$ligne}\n"; } sleep(5); $ZARAFADB_PID = ZARAFADB_PID(); if (!$unix->process_exists($ZARAFADB_PID)) { RestoreFromBackup_progress("Failed to restart dedicated MySQL", 100); return; } RestoreFromBackup_progress("Stopping Zarafa server", 43); @unlink("/tmp/zarafa-upgrade-lock"); shell_exec("/etc/init.d/zarafa-server stop --force"); $pid = XZARAFA_SERVER_PID(); if ($unix->process_exists($pid)) { $kill = $unix->find_program("kill"); unix_system_kill_force($pid); } RestoreFromBackup_progress("Restarting MySQL service (normal)", 45); echo "Action: Restarting MySQL service...\n"; echo "Action: Stopping MySQL service...\n"; stop(true); echo "Action: Starting MySQL service (InnoDB normal mode)...\n"; start(true, false); while (list($num, $ligne) = each($results)) { echo "Service: {$ligne}\n"; } sleep(2); $ZARAFADB_PID = ZARAFADB_PID(); if (!$unix->process_exists($ZARAFADB_PID)) { RestoreFromBackup_progress("Failed to restart dedicated MySQL", 100); return; } if (!$unix->is_socket("/var/run/mysqld/zarafa-db.sock")) { echo "Action: /var/run/mysqld/zarafa-db.sock waiting socket\n"; for ($i = 0; $i < 5; $i++) { if ($unix->is_socket("/var/run/mysqld/zarafa-db.sock")) { break; } echo "Action: Waiting zarafa-db.sock {$i}/4\n"; sleep(1); } } if (!$unix->is_socket("/var/run/mysqld/zarafa-db.sock")) { echo "Action: /var/run/mysqld/zarafa-db.sock no such socket\n"; RestoreFromBackup_progress("zarafa-db.sock no such socket", 100); return; } echo "Action: /var/run/mysqld/zarafa-db.sock OK\n"; echo "Action: create a freshed Zarafa database\n"; $ZarafaIndexPath = $sock->GET_INFO("ZarafaIndexPath"); if ($ZarafaIndexPath == null) { $ZarafaIndexPath = "/var/lib/zarafa/index"; } RestoreFromBackup_progress("Cleaning/Stopping Zarafa search DBs", 50); if (is_dir($ZarafaIndexPath)) { recursive_remove_directory("{$ZarafaIndexPath}"); shell_exec("/etc/init.d/zarafa-search stop"); } RestoreFromBackup_progress("Create a freshed Zarafa database", 50); $results = array(); $cmd = "{$mysql} --socket=/var/run/mysqld/zarafa-db.sock --protocol=socket --user=root --batch --debug-info --execute=\"CREATE DATABASE zarafa\" 2>&1"; $results = array(); exec("{$cmd}", $results); while (list($num, $ligne) = each($results)) { echo "MySQL: (Create Database) {$ligne}\n"; } RestoreFromBackup_progress("Testing Database...", 51); if (!is_dir("{$WORKDIR}/data/zarafa")) { echo "Action: FAILED TO create a freshed Zarafa database: ABORT!!\n"; echo "Action: {$WORKDIR}/data/zarafa no such directory\n"; RestoreFromBackup_progress("FAILED to create a freshed Zarafa database", 100); return; } RestoreFromBackup_progress("Checks Database size", 53); databasesize(true); $gunzip = $unix->find_program("gunzip"); $SourceFileBase = basename($backuppath); $file_ext = $unix->file_ext($SourceFileBase); $tStart = time(); $nohup = $unix->find_program("nohup"); $backuppath1 = $unix->shellEscapeChars($backuppath); $cmd = "{$nohup} {$mysql} --show-warnings --socket=/var/run/mysqld/zarafa-db.sock --protocol=socket --user=root --batch --debug-info --database=zarafa < {$backuppath1} >/root/mysqllog.txt 2>&1 &"; echo "Action: {$SourceFileBase} extension {$file_ext}\n"; echo "Action: Restoring From {$backuppath1}\n"; if ($file_ext == "gz") { echo "Action: Restoring From {$backuppath1} with uncompress..\n"; $cmd = "{$nohup} {$gunzip} -c {$backuppath1} |{$mysql} --show-warnings --socket=/var/run/mysqld/zarafa-db.sock --protocol=socket --user=root --batch --debug-info --database=zarafa >/root/mysqllog.txt 2>&1 &"; } $size = @filesize($backuppath); $size = FormatBytes($size / 1024); echo "Action: Please wait, it should take time...\nAction: Do not shutdown the computer or restart the MySQL service!\n"; $results = array(); RestoreFromBackup_progress("{restoring_data} {$size} {please_wait} !", 70); $lastmd5 = null; $continue = true; shell_exec($cmd); $ALRDLO = array(); while ($continue) { $fileMD5 = @md5_file("/root/mysqllog.txt"); if ($fileMD5 != $lastmd5) { $LOGS = explode("\n", @file_get_contents("/root/mysqllog.txt")); while (list($num, $ligne) = each($LOGS)) { if (trim($ligne) == null) { continue; } if (isset($ALRDLO[md5($ligne)])) { continue; } $ALRDLO[md5($ligne)] = true; if (preg_match("#ERROR\\s+([0-9]+)\\s+\\(#", $ligne, $re)) { echo date("Y-m-d H:i:s") . " MySQL: FAILED !!! {$ligne}\n"; RestoreFromBackup_progress("{failed} {error} {$re[1]} ", 100); return; } echo date("Y-m-d H:i:s") . " MySQL: {$ligne}\n"; } $lastmd5 = $fileMD5; } $pid = $unix->PIDOF_PATTERN("{$mysql}\\s+.*?--socket=/var/run/mysqld/zarafa-db.sock.*?database=zarafa"); echo "Action: PID: {$pid}\n"; if (!$unix->process_exists($pid)) { echo "Action: injection stopped running since " . $unix->distanceOfTimeInWords($tStart, time(), true) . "\n"; $continue = false; break; } echo "Action: PID {$pid} running since " . $unix->distanceOfTimeInWords($tStart, time(), true) . ", please wait...\n"; RestoreFromBackup_progress($unix->distanceOfTimeInWords($tStart, time(), true) . " {please_wait} !", 71); $continue = true; sleep(30); continue; } echo "Action: Done, took: " . $unix->distanceOfTimeInWords($tStart, time(), true) . "\n"; echo "Action: Please wait, Checks Database size\n"; RestoreFromBackup_progress("Checks Database size", 75); databasesize(true); RestoreFromBackup_progress("{restoring_data} {success}", 80); echo "Action: restart_services\n"; RestoreFromBackup_progress("{restart_services}", 90); $unix->THREAD_COMMAND_SET("/etc/init.d/zarafa-server restart"); echo "Action: Restore task done...\n"; echo "Action: You can close the windows now...\n"; RestoreFromBackup_progress("{done}", 100); die; }
function ScanSpool($SQLIne) { $unix = new unix(); $GLOBALS["CLASS_UNIX"] = $unix; $php5 = $unix->LOCATE_PHP5_BIN(); $domainname = $SQLIne["domainname"]; $instance = $SQLIne["hostname"]; $nohup = $unix->find_program("nohup"); $params = unserialize(base64_decode($SQLIne["params"])); $directory = "{$GLOBALS["QUEUE_DIRECTORY"]}/{$instance}/{$domainname}"; //smtp::events("Scanning `$directory` ",__FUNCTION__,__FILE__,__LINE__); if (!is_dir($directory)) { return; } if ($GLOBALS["VERBOSE"]) { smtp::events("Scanning `{$directory}` ", __FUNCTION__, __FILE__, __LINE__); } if (!($handle = opendir($directory))) { smtp::events("glob failed {$directory} in Line: ", __FUNCTION__, __FILE__, __LINE__); return; } if (!isset($params["max_msg_per_connection"])) { ScanSpoolEvents("Warning max_msg_per_connection is not set..", __LINE__); $params["max_msg_per_connection"] = 5; } if (!isset($params["max_msg_rate"])) { $params["max_msg_rate"] = 600; } if (!isset($params["max_smtp_out"])) { $params["max_smtp_out"] = 5; } if (!is_numeric($params["max_msg_per_connection"])) { $params["max_msg_per_connection"] = 5; } if (!is_numeric($params["max_msg_rate"])) { $params["max_msg_rate"] = 600; } if (!is_numeric($params["max_msg_rate_timeout"])) { $params["max_msg_rate_timeout"] = 300; } $max_msg_per_connection = $params["max_msg_per_connection"]; $max_msg_rate = $params["max_msg_rate"]; $max_smtp_out = $params["max_smtp_out"]; //smtp::events("Scanning `$directory` Max messages per instance: $max_msg_per_connection,max_msg_rate: $max_msg_rate,Max instances:$max_smtp_out",__FUNCTION__,__FILE__,__LINE__); if (!aiguilleur_max_msg_rate($directory, $params)) { return; } $c = 0; $dMESS = 0; $smtpInstances = array(); $DetectedInstances = array(); ScanSpoolEvents("Starting loop in {$directory}", __LINE__); $CountDeScannedMessageFiles = 0; $INSTANCESEXEC = array(); while (false !== ($filename = readdir($handle))) { $CountDeScannedMessageFiles++; $fullpath = "{$directory}/{$filename}"; if (is_dir($fullpath)) { $DirName = basename($fullpath); if ($GLOBALS["DEBUG"]) { smtp::events("{$fullpath} is a directory `{$DirName}` -> next", __FUNCTION__, __FILE__, __LINE__); } if (is_numeric($DirName)) { $DetectedInstances[] = $DirName; } continue; } $ext = $unix->file_ext($fullpath); if ($ext != "routing") { if ($GLOBALS["DEBUG"]) { smtp::events("{$fullpath} <> routing -> next", __FUNCTION__, __FILE__, __LINE__); } continue; } if ($c >= $max_smtp_out) { $c = 0; } $dMESS++; if (count($smtpInstances[$c]) >= $max_msg_per_connection) { //smtp::events("Instance $c =". count($smtpInstances[$c])." >= $max_msg_per_connection -> Trying the next instance " .$c+1,__FUNCTION__,__FILE__,__LINE__); $c++; continue; } $InstanceQueueMessagesNumber = InstanceQueueMessagesNumber("{$directory}/{$c}"); if ($InstanceQueueMessagesNumber >= $max_msg_per_connection) { ScanSpoolExecuteProcess($domainname, $instance, $c); $c++; continue; } $smtpInstances[$c][] = $fullpath; if ($GLOBALS["DEBUG"]) { echo "Instance {$c}, add {$fullpath} " . count($smtpInstances[$c]) . " items/{$max_msg_per_connection}\n"; } $c++; if ($c >= $max_smtp_out) { $c = 0; } } $directoriesOfInstances = $GLOBALS["CLASS_UNIX"]->dirdir($directory); if (is_array($directoriesOfInstances)) { while (list($a, $b) = each($directoriesOfInstances)) { $InstanceNum = basename($a); if (!is_numeric($InstanceNum)) { ScanSpoolEvents("`{$InstanceNum}` is not a numeric and a part of {$a}", __LINE__); continue; } ScanSpoolExecuteProcess($domainname, $instance, $InstanceNum); } } ScanSpoolEvents("Stopping loop in {$directory} ({$CountDeScannedMessageFiles} messages) " . count($smtpInstances) . " SMTP instances /" . count($directoriesOfInstances) . " already created instances", __LINE__); while (list($InstanceNum, $messages) = each($smtpInstances)) { if (!is_dir("{$directory}/{$InstanceNum}")) { if ($GLOBALS["VERBOSE"]) { echo "Writting new mails in {$directory}/{$InstanceNum}\n"; } @mkdir("{$directory}/{$InstanceNum}"); $cmes = 0; while (list($messageIndex, $messagePath) = each($messages)) { $cmes++; @copy($messagePath, "{$directory}/{$InstanceNum}/" . basename($messagePath)); @unlink($messagePath); } ScanSpoolEvents("[{$domainname}]: Put {$cmes} messages in Instance Number {$InstanceNum} (Max Message per connection={$max_msg_per_connection})", __LINE__); if (!$GLOBALS["VERBOSE"]) { ScanSpoolExecuteProcess($domainname, $instance, $InstanceNum); } } else { if (!$GLOBALS["VERBOSE"]) { ScanSpoolExecuteProcess($domainname, $instance, $InstanceNum); } } } if ($dMESS == 0) { if (count($DetectedInstances) > 0) { if ($GLOBALS["VERBOSE"]) { echo count($DetectedInstances) . " Detected instances\n"; } while (list($index, $InstanceNum) = each($DetectedInstances)) { $cmd = "{$nohup} {$php5} " . __FILE__ . " --instance-send {$domainname} {$instance} {$InstanceNum} >/dev/null 2>&1 &"; if (!$GLOBALS["VERBOSE"]) { shell_exec($cmd); } } return; } } //smtp::events("[$instance/$domainname]: $dMESS messages file...",__FUNCTION__,__FILE__,__LINE__); if ($dMESS == 0) { if (is_file("{$directory}/TIMESTAMP")) { @unlink("{$directory}/TIMESTAMP"); } if (is_file("{$directory}/max_msg_rate")) { @unlink("{$directory}/max_msg_rate"); } } @rmdir($directory); }
function CheckOldCachesLog() { @mkdir("/home/squid/cache-logs", 0755, true); $unix = new unix(); foreach (glob("/var/log/squid/cache.log.*") as $filename) { if ($GLOBALS["VERBOSE"]) { echo "Move {$filename} to /home/squid/cache-logs\n"; } Events("Move {$filename} to /home/squid/cache-logs"); @copy($filename, "/home/squid/cache-logs/" . basename($filename)); @unlink($filename); } foreach (glob("/home/squid/cache-logs/*") as $filename) { $ext = $unix->file_ext($filename); if (is_numeric($ext)) { Events("Compress {$filename} to {$filename}.gz"); if ($unix->compress($filename, "{$filename}.gz")) { @unlink($filename); } continue; } if ($ext == "gz") { $time = $unix->file_time_min($filename); if ($GLOBALS["VERBOSE"]) { echo "{$filename} = {$time}Mn\n"; } if ($time > 4320) { Events("Remove {$filename} (exceed 3 days on disk...)"); @unlink($filename); continue; } } } if ($GLOBALS["VERBOSE"]) { echo "CheckOldCachesLog:: END\n"; } }
function analyze_single_file($filename) { $sock = new sockets(); $unix = new unix(); if (!isset($GLOBALS["squidtail"])) { $GLOBALS["squidtail"] = new squid_tail(); } if ($GLOBALS["VERBOSE"]) { echo $filename . " -> '/' = " . strpos($filename, '/') . " pos\n"; } if (!is_file($filename)) { if ($GLOBALS["VERBOSE"]) { echo $filename . " no such file\n"; } if (!tests_nas()) { return; } $SquidOldLogsNAS = unserialize(base64_decode($sock->GET_INFO("SquidOldLogsNAS"))); $mountPoint = "/mnt/SquidImportLogs"; $NasFolder = $SquidOldLogsNAS["folder"]; $NasFolder = str_replace('\\', '/', $NasFolder); $NasFolder = str_replace('//', '/', $NasFolder); if (strpos($NasFolder, "/") > 0) { $f = explode("/", $NasFolder); unset($f[0]); $NasFolder = @implode("/", $f); } $targetFile = "{$mountPoint}/{$NasFolder}/{$filename}"; $targetFile = str_replace("//", "/", $targetFile); } else { $targetFile = $filename; } $REMOVE = false; $ext = $unix->file_ext($targetFile); if ($GLOBALS["SIMULATE"]) { echo "Simulate enabled, no MySQL events will be injected\n"; } if (!$GLOBALS["SIMULATE"]) { $zmd5 = md5_file($targetFile); } @mkdir("/home/squid/wkdir", 0755, true); if ($ext == "gz") { $basename = $filename; if (count(explode('/', $basename)) > 0) { $basename = basename($basename); } if ($GLOBALS["VERBOSE"]) { echo "Uncompress {$targetFile}\n"; } $unix->uncompress($targetFile, "/home/squid/wkdir/{$basename}"); $targetFile = "/home/squid/wkdir/{$basename}"; $REMOVE = TRUE; } else { $basename = $targetFile; if (count(explode('/', $basename)) > 0) { $basename = basename($basename); } if ($GLOBALS["VERBOSE"]) { echo "Copy {$targetFile} -> /home/squid/wkdir/{$basename}\n"; } if (!@copy($targetFile, "/home/squid/wkdir/{$basename}")) { events("Unable to copy {$targetFile} to /home/squid/wkdir/{$basename}"); return false; } $targetFile = "/home/squid/wkdir/{$basename}"; $REMOVE = TRUE; } events("_analyze_file - {$targetFile}"); if (_analyze_file($targetFile, $zmd5)) { if ($REMOVE) { @unlink($targetFile); } } killNas(); }