HEX
Server: Apache/2.4.67 (Debian)
System: Linux hosting103 6.1.0-49-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.174-1 (2026-05-26) x86_64
User: muzikaecom (1013)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: //usr/local/share/hostingplatform/scripts/Utils.class.php
<?php

class Utils {
	static $download = array(
		"http://",
		"https://",
		"ftp://"
	);

	public static function needToDownload($url) {
		foreach(self::$download as $proto) {
			if (
				strtolower(substr($url, strlen($proto)))
				== strtolower($proto)
			) return true;
		}

		return false;
	}

	public static function tempdir() {
		$base = sys_get_temp_dir();
		do {
			$path = $base . '/' . mt_rand();
		} while (file_exists($path));

		if (!mkdir($path, 0700)) throw new Exception(
			"Unable to create temp dir '$path'!"
		);
		return $path;
	}

	public static function tempfile() {
		$base = sys_get_temp_dir();
		do {
			$path = $base . '/' . mt_rand();
		} while (file_exists($path));

		if (!touch($path)) throw new Exception(
			"Unable to create temp file '$path'!"
		);

		@chmod($path, 0600);

		return $path;
	}

	public static function sameContent($one, $other) {
		$cmd = "diff "
			. "-dbwBNau0 "
			. "-I " . escapeshellarg('^-- Dump completed on.*$') . " "
			. escapeshellarg($one)
			. " "
			. escapeshellarg($other);

		exec($cmd, $output, $return_val);

		return $return_val == 0;
	}

	public static function chOwnModTree($path, $user, $group, $dirmod = 0750, $filemod = 0640) {
		if (!file_exists($path)) return;

		if (is_dir($path)) {
			$entries = scandir($path);
			foreach ($entries as $entry) {
				if ($entry == ".")  continue;
				if ($entry == "..") continue;

				self::chOwnModTree(
					$path.'/'.$entry,
					$user,
					$group,
					$dirmod,
					$filemod
				);
			}

			chmod($path, $dirmod);
		} else {
			chmod($path, $filemod);
		}

		chown($path, $user);
		chgrp($path, $group);
	}

	public static function downloadFile($url) {
		$path = self::tempfile();

		exec(
			"wget "
			. "-q "
			. "-c "
			. "-t3 "
			. "--no-check-certificate "
			. "-O " . escapeshellarg($path) . " "
			. escapeshellarg($url),
			$output,
			$return_val
		);

		if ($return_val !== 0) {
			unlink($path);
			throw new Exception("Failed to download '$url'!");
		}

		return $path;
	}

	public static function untar($archive, $path) {
		if (!is_file($archive))
			throw new Exception("tar: Archive file '$archive' does not exist!");

		if (!is_dir($path))
			throw new Exception("tar: Target dir '$path' does not exist!");

		exec(
			"tar "
			. "-C " . escapeshellarg($path) . " "
			. "-x "
			. "-z "
			. "-f " . escapeshellarg($archive),
			$output,
			$return_var
		);

		if ($return_var != 0) throw new Exception(
			"tar: Error while extracting '$archive' to '$path'!"
		);
	}

	public static function tarAddDir($archive, $path, $rename=false) {
		if ($rename === false)
			$rename = basename($path);

		if (file_exists($archive))
			$action = "-r ";
		else
			$action = "-c ";

		exec(
			"tar "
			. "-C " . escapeshellarg($path) . " "
			. "--transform='s|^.|". escapeshellarg($rename) . "|' "
			. $action 
			. "-f " . escapeshellarg($archive) . " " 
			. ".",
			$output,
			$return_var
		);

		if ($return_var != 0 || !file_exists($archive)) throw new Exception(
			"tar: Failed to add '$path' to '$archive'!"
		);
	}

	public static function tarAddFile($archive, $path, $rename=false) {
		if ($rename === false)
			$rename = basename($path);

		if (file_exists($archive))
			$action = "-r ";
		else
			$action = "-c ";

		exec(
			"tar "
			. "-C " . escapeshellarg(dirname($path)) . " "
			. $action
			. "-f " . escapeshellarg($archive) . " "
			. escapeshellarg(basename($path)),
			$output,
			$return_var
		);

		if ($return_var != 0 || !file_exists($archive)) throw new Exception(
			"tar: Failed to add '$path' to '$archive'!"
		);
	}

	public static function rsync($src, $dst, $previous = null) {
		$options = "";

		if (file_exists($previous)) {
			if (is_link($previous)) $previous = readlink($previous);

			$options .= " --link-dest=" . escapeshellarg($previous);
		}

		foreach (array("include", "exclude") as $inex)
			if (is_file($src . "/.backup-" . $inex))
				$options .= " --" . $inex . "-from=" . escapeshellarg($src . "/.backup-" . $inex);

		exec(
			"nice rsync-novanish "
			. "-a "       // archive: -rlptgoD
			. "-H "       // keep hard links (expensive)
			. "--delete " // remove deleted files from backup
			. $options . " " 
			. escapeshellarg($src) . " "
			. escapeshellarg($dst),
			$output,
			$return_var
		);

		if ($return_var != 0 || !file_exists($dst)) throw new Exception(
			"rsync: Failed to rsync '$src' to '$dst'!"
		);
	}

	public static function compress($path) {
		if (!file_exists($path)) throw new Exception(
			"gzip: Unable to compress nonexistent file '$path'!"
		);

		exec("gzip \"$path\"", $output, $return_var);

		if ($return_var != 0 || !is_file($path . '.gz')) throw new Exception(
			"gzip: Failed to compress file '$path'!"
		);

		return $path.'.gz';
	}

	public static function rm($path) {
		if (!file_exists($path) && !is_link($path)) return;

		if (is_dir($path) && !is_link($path)) {
			$entries = scandir($path);
			foreach ($entries as $entry) {
				if ($entry == ".")  continue;
				if ($entry == "..") continue;

				self::rm($path.'/'.$entry);
			}
			rmdir($path);
		} else {
			unlink($path);
		}
	}

	public static function databaseName($packagename, $database) {
		// package
		// package_db

		return preg_replace(
			array(
				"/^([^_]+)\$/",
				"/^[^_]+_(.*)\$/"
			),
			array(
				$packagename,
				$packagename."_\$1"
			),
			$database
		);
	}

	public static function databaseShortName($database) {
		return preg_replace(
			array(
				"/^([^_]+)\$/",
				"/^[^_]+_(.*)\$/"
			),
			array(
				"",
				"\$1"
			),
			$database
		);
	}

	public static function dumpPostgreSQLDatabase($database, $path) {
		exec(
			"su postgres -l -c \"pg_dump -c -x -O --column-inserts "
			. escapeshellarg($database)
			. "\"",
			$output,
			$return_var
		);

		// Check target dir existence...
		if (!is_dir(dirname($path))) {
			if (!mkdir(dirname($path), 0755, true)) throw new Exception(
				"Unable to create parent dir for PostgreSQL dump " .
				"'$path'"
			);
		}
		file_put_contents($path, implode("\n", $output));

		if ($return_var != 0 || !is_file($path)) throw new Exception(
			"Failed to dump PostgreSQL database '$database' to '$path'!"
		);
	}

	public static function restorePostgreSQLDatabase($database, $path) {
		exec(
			"su postgres -l -c \"psql "
			. escapeshellarg($database)
			. "\""
			. " < " . escapeshellarg($path),
			$output,
			$return_var
		);

		if ($return_var != 0) throw new Exception(
			"Failed to import PostgreSQL database '$database' from '$path'!"
		);
	}

	public static function dumpMySQLDatabase($database, $path) {
		$username = trim(`grep user /etc/mysql/debian.cnf |sort -u |awk -F '=' '{ print \$2 }'`);
		$password = trim(`grep pass /etc/mysql/debian.cnf |sort -u |awk -F '=' '{ print \$2 }'`);

		exec(
			"mysqldump "
			. "--user=" . escapeshellarg($username) . " "
			. "--password=" . escapeshellarg($password) . " "
			. escapeshellarg($database),
			$output,
			$return_var
		);

		// Check target dir existence...
		if (!is_dir(dirname($path))) {
			if (!mkdir(dirname($path), 0770, true)) throw new Exception(
				"Unable to create parent dir for MySQL dump " .
				"'$path'"
			);
		}
		file_put_contents($path, implode("\n", $output));

		if ($return_var != 0 || !is_file($path)) throw new Exception(
			"Failed to dump MySQL database '$database' to '$path'!"
		);
	}

	public static function dumpMySQLTable($database, $table, $path) {
		$username = trim(`grep user /etc/mysql/debian.cnf |sort -u |awk -F '=' '{ print \$2 }'`);
		$password = trim(`grep pass /etc/mysql/debian.cnf |sort -u |awk -F '=' '{ print \$2 }'`);

		exec(
			"mysqldump "
			. "--user=" . escapeshellarg($username) . " "
			. "--password=" . escapeshellarg($password) . " "
                        . "--no-create-db --skip-comments "
                        . "--add-drop-table "
			. escapeshellarg($database) . " "
                        . "--tables " . escapeshellarg($table),
			$output,
			$return_var
		);

		// Check target dir existence...
		if (!is_dir(dirname($path))) {
			if (!mkdir(dirname($path), 0770, true)) throw new Exception(
				"Unable to create parent dir for MySQL dump " .
				"'$path'"
			);
		}
		file_put_contents($path, implode("\n", $output));

		if ($return_var != 0 || !is_file($path)) throw new Exception(
			"Failed to dump MySQL database '$database' to '$path'!"
		);
	}


	public static function restoreMySQLDatabase($database, $path) {
		$username = trim(`grep user /etc/mysql/debian.cnf |sort -u |awk -F '=' '{ print \$2 }'`);
		$password = trim(`grep pass /etc/mysql/debian.cnf |sort -u |awk -F '=' '{ print \$2 }'`);

		exec(
			"mysql "
			. "--user=" . escapeshellarg($username) . " "
			. "--password=" . escapeshellarg($password) . " "
			. "--batch "
			. "--silent "
			. escapeshellarg($database)
			. " < " . escapeshellarg($path),
			$output,
			$return_var
		);

		if ($return_var != 0) throw new Exception(
			"Failed to import MySQL database '$database' from '$path'!"
		);
	}
}