Current File : //bin/zppy |
#!/usr/bin/php
<?php
/**
* @copyright 2014-2015 Sentora Project (http://www.sentora.org/)
* Sentora is a GPL fork of the ZPanel Project whose original header follows:
*
* ZPPY - The ZPanel Package Manager Tool.
* @package zpanelx
* @subpackage core -> zppy
* @author Bobby Allen (ballen@bobbyallen.me)
* @copyright ZPanel Project (http://www.zpanelcp.com/)
* @link http://www.zpanelcp.com/
* @license GPL (http://www.gnu.org/licenses/gpl.html)
*/
$rawPath = str_replace("\\", "/", dirname(__FILE__));
$rootPath = str_replace("/bin", "/", $rawPath);
chdir($rootPath);
set_time_limit(0);
//ini_set('memory_limit', '256M');
ini_set('error_reporting', E_ALL | E_STRICT);
ini_set('display_errors', 'On');
ini_set('log_errors', 'Off');
require_once 'dryden/loader.inc.php';
require_once 'cnf/db.php';
require_once 'inc/dbc.inc.php';
global $zdbh;
$repo_file = "etc/zppy-cache/repo.list";
$package_file = "etc/zppy-cache/package.list";
if (!runtime_controller::IsCLI())
exit(1);
if ((isset($argv[1])) && ($argv[1] == 'install')) {
if (!isset($argv[2])) {
fwrite(STDOUT, "No module specifed!\n");
exit(0);
}
fwrite(STDOUT, "Module to be installed " . $argv[2] . "\n");
$contents = file_get_contents($package_file);
$lines = preg_split('/\r\n|\r|\n/', $contents);
$newconf = "";
$isfound = false;
foreach ($lines as $line) {
$this_line = preg_split('/\t/', $line);
if ($this_line[0] == $argv[2]) {
$isfound = true;
if (fs_director::CheckFileExists('etc/zppy-cache/package-downloads/' . $argv[2] . '.zpp')) {
unlink('etc/zppy-cache/package-downloads/' . $argv[2] . '.zpp');
}
chdir('etc/zppy-cache/package-downloads/');
exec("wget -UZppyClient http://" . $this_line[1] . "/" . $argv[2] . ".zpp");
chdir($rootPath);
if ($isfound) {
if (!fs_director::CheckFolderExists(ctrl_options::GetSystemOption('sentora_root') . 'modules/' . $argv[2])) {
if (fs_director::CreateDirectory('modules/' . $argv[2])) {
if (sys_archive::Unzip('etc/zppy-cache/package-downloads/' . $argv[2] . '.zpp', 'modules/' . $argv[2] . '')) {
fs_director::SetFileSystemPermissions('modules/' . $argv[2] . '', 0777);
if (!fs_director::CheckFileExists('modules/' . $argv[2] . '/module.xml')) {
fwrite(STDOUT, "No module.xml file found in the unzipped archive.\n");
exit(1);
} else {
ui_module::ModuleInfoToDB($argv[2]);
$extra_config = ctrl_options::GetSystemOption('sentora_root') . "modules/" . $argv[2] . "/deploy/install.run";
if (fs_director::CheckFileExists($extra_config))
include($extra_config);
fwrite(STDOUT, "Module installed successfully!\n");
exit(0);
}
} else {
fs_director::RemoveDirectory('modules/' . $argv[2]);
fwrite(STDOUT, "An error occured, The package archive could not be extracted!\n");
exit(1);
}
exit(1);
} else {
fwrite(STDOUT, "Cannot create directory for module!\n");
exit(1);
}
} else {
fwrite(STDOUT, "Module already appears to be installed!\n");
exit(1);
}
}
}
}
if (!$isfound) {
fwrite(STDOUT, "No package match, check you have added the correct repo!\n");
exit(1);
}
}
if ((isset($argv[1])) && ($argv[1] == 'upgrade')) {
if (!isset($argv[2])) {
fwrite(STDOUT, "No module specifed!\n");
exit(0);
}
fwrite(STDOUT, "Module to be upgraded!\n");
$contents = file_get_contents($package_file);
$lines = preg_split('/\r\n|\r|\n/', $contents);
$newconf = "";
foreach ($lines as $line) {
$this_line = preg_split('/\t/', $line);
if ($this_line[0] == $argv[2]) {
if (fs_director::CheckFileExists('etc/zppy-cache/package-downloads/' . $argv[2] . '.zpp')) {
unlink('etc/zppy-cache/package-downloads/' . $argv[2] . '.zpp');
}
chdir('etc/zppy-cache/package-downloads/');
exec("wget -UZppyClient http://" . $this_line[1] . "/" . $argv[2] . ".zpp");
chdir($rootPath);
}
}
if (!fs_director::CheckFolderExists(ctrl_options::GetSystemOption('sentora_root') . 'modules/' . $argv[2])) {
fwrite(STDOUT, "The module '" . $argv[2] . "' is not installed, install it first before you can upgrade it!\n");
exit(1);
} else {
/**
* We need to overwrite any existing files here!!!
*/
if (sys_archive::Unzip('etc/zppy-cache/package-downloads/' . $argv[2] . '.zpp', 'modules/' . $argv[2] . '')) {
fs_director::SetFileSystemPermissions('modules/' . $argv[2] . '', 0777);
if (!fs_director::CheckFileExists('modules/' . $argv[2] . '/module.xml')) {
fwrite(STDOUT, "No module.xml file found in the unzipped archive.\n");
exit(1);
} else {
$extra_config = ctrl_options::GetSystemOption('sentora_root') . "modules/" . $argv[2] . "/deploy/upgrade.run";
if (fs_director::CheckFileExists($extra_config))
include($extra_config);
fwrite(STDOUT, "Module upgraded successfully!\n");
exit(0);
}
} else {
fwrite(STDOUT, "An error occured, The package archive could not be extracted!\n");
exit(1);
}
}
}
if ((isset($argv[1])) && ($argv[1] == 'remove')) {
if (!isset($argv[2])) {
fwrite(STDOUT, "No module specifed!\n");
exit(0);
}
if (fs_director::CheckFolderExists('modules/' . $argv[2])) {
$sql = $zdbh->prepare("SELECT mo_id_pk FROM x_modules WHERE mo_folder_vc = '" . $argv[2] . "'");
$sql->execute();
$moduleid = $sql->fetch();
if (isset($argv[3]) && $argv[3] == '-y') {
$confirm = 'y';
} else {
fwrite(STDOUT, "Removing \"" . $argv[2] . "\" will delete the module folder and drop any tables used by the module!\n");
fwrite(STDOUT, "Please confirm you want to remove this module? [y/N]");
$confirm = fgets(STDIN, 2);
}
if ($confirm == 'y') {
fwrite(STDOUT, "Removing " . $argv[2] . " module..\n");
$extra_config = ctrl_options::GetSystemOption('sentora_root') . "modules/" . $argv[2] . "/deploy/remove.run";
if (fs_director::CheckFileExists($extra_config))
include($extra_config);
ctrl_groups::DeleteGroupModulePermissions(0, $moduleid['mo_id_pk']);
$sql = $zdbh->prepare("DELETE FROM x_modules WHERE mo_folder_vc = '$argv[2]'");
$sql->execute();
$sql = $zdbh->prepare("DELETE FROM x_settings WHERE so_module_vc = '$argv[2]'");
$sql->execute();
if (!fs_director::RemoveDirectory('modules/' . $argv[2])) {
fwrite(STDOUT, "Could not removed the module directory!\n");
exit(1);
}
exit(0);
} else {
fwrite(STDOUT, "Cancelled module removal!\n");
exit(0);
}
} else {
fwrite(STDOUT, "The module '" . $argv[2] . "' doesn't appear to be installed!\n");
exit(1);
}
}
if ((isset($argv[1])) && (($argv[1] == 'version') || ($argv[1] == '-v') || ($argv[1] == '--version'))) {
fwrite(STDOUT, "Version: " . ctrl_options::GetSystemOption('dbversion') . "\n");
exit(0);
}
if (isset($argv[1]) && ($argv[1] == 'checkperms')) {
$checks = 0;
fwrite(STDOUT, "Checking file permissions: \n");
if (!is_writable($repo_file)) {
fwrite(STDOUT, "\trepo.list if not writable!\n");
$checks++;
}
if (!is_writable($package_file)) {
fwrite(STDOUT, "\tpackage.list if not writable!\n");
$checks++;
}
if ($checks == 0) {
fwrite(STDOUT, "All good!\n");
exit(0);
}
exit(1);
}
if ((isset($argv[1])) && ($argv[1] == 'repo')) {
if ((isset($argv[2])) && ($argv[2] == 'add')) {
$contents = file_get_contents($repo_file);
$lines = preg_split('/\r\n|\r|\n/', $contents);
if(!isset($argv[3])){
fwrite(STDOUT, "No repository has been specified!\n");
exit(1);
}
$exists = false;
foreach ($lines as $line) {
if ($line == $argv[3]) {
$exists = true;
}
}
if (isset($argv[3]) && $exists == false) {
$handle = fopen($repo_file, "r+");
$contents = fread($handle, filesize($repo_file));
$newrepo = "" . $argv[3] . "\n";
fwrite($handle, $newrepo);
fclose($handle);
fwrite(STDOUT, "Added '" . $argv[3] . "' to list of repositories!\nRemember to execute 'zppy update' to grab the latest list of packages.\n");
exit(0);
} else {
fwrite(STDOUT, "The repository '" . $argv[3] . "' already exists.\n");
exit(1);
}
}
if ((isset($argv[2])) && ($argv[2] == 'remove')) {
$contents = file_get_contents($repo_file);
$lines = preg_split('/\r\n|\r|\n/', $contents);
$newconf = "";
if(!isset($argv[3])){
fwrite(STDOUT, "No repository has been specified!\n");
exit(1);
}
$removed = false;
foreach ($lines as $line) {
if ($line != $argv[3]) {
$newconf .= $line . "\n";
} else {
$removed = true;
}
}
$newconf = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $newconf);
$handle = fopen($repo_file, "w");
fwrite($handle, $newconf);
fclose($handle);
if ($removed) {
fwrite(STDOUT, "Removed '" . $argv[3] . "' from the list of repositories! Remember to execute 'zppy update' to grab the latest list of packages.\n");
exit(0);
} else {
fwrite(STDOUT, "The repository '" . $argv[3] . "' was not found in your list of repositories!\n");
exit(1);
}
}
if ((isset($argv[2])) && ($argv[2] == 'list')) {
$handle = fopen($repo_file, "r+");
$contents = fread($handle, filesize($repo_file));
$lines = preg_split('/\r\n|\r|\n/', $contents);
fwrite(STDOUT, "\nCurrently configured repositories:\n\n");
foreach ($lines as $line) {
if (isset($line[0]) && ($line[0] == "#")) {
# Its a comment - lets ignore it.
} else {
if ($line != "") {
fwrite(STDOUT, " > " . $line . "\n");
}
}
}
exit(0);
}
}
if ((isset($argv[1])) && ($argv[1] == 'update')) {
$raw_repos = file_get_contents($repo_file);
$lines = preg_split('/\r\n|\r|\n/', $raw_repos);
$handle = fopen($package_file, "w+");
fwrite($handle, "");
fclose($handle);
fwrite(STDOUT, "Updating package list..\n");
foreach ($lines as $line) {
if (isset($line[0]) && ($line[0] == "#")) {
# Its a comment or empty line - lets ignore it.
} else {
if ($line != "") {
fwrite(STDOUT, " Connecting to " . $line . "\n");
$packagects = @file_get_contents("http://" . $line . "/packages.txt");
fwrite(STDOUT, " Getting list from " . $line . "\n");
if ($packagects) {
$currconf = file_get_contents($package_file);
$plines = preg_split('/\r\n|\r|\n/', $packagects);
$newconf = "";
foreach ($plines as $pline) {
if ((substr($line, 1, 1) != "#") || ($line != "")) {
$newconf .= $pline . "\t" . $line . "\n";
}
}
$handle = fopen($package_file, "w");
fwrite($handle, $currconf . $newconf);
fclose($handle);
} else {
fwrite(STDOUT, " Failed to contact the URL '" . $line . "'\n");
}
}
}
}
fwrite(STDOUT, "Local package cache is now up-to-date!\n");
exit(0);
}
if ((!isset($argv[1])) || ($argv[1] == 'help') || ($argv[1] == '-h') || ($argv[1] == '--help')) {
fwrite(STDOUT, "\nSentora Package Manager\n");
fwrite(STDOUT, "Copyright (c) 2014 Sentora Project (http://sentora.org/)\n");
fwrite(STDOUT, "Copyright (c) 2008 - 2014 ZPanel Project (http://www.zpanelcp.com/)\n\n");
fwrite(STDOUT, "Usage: zppy [action] [modulename]\n\n");
fwrite(STDOUT, "Actions:\n");
fwrite(STDOUT, " install [modulename]- Install a named module.\n");
fwrite(STDOUT, " upgrade [modulename]- Updates a named module.\n");
fwrite(STDOUT, " update - Updates local avaliable package list.\n");
fwrite(STDOUT, " remove [modulename] (-y) - Remove a named module. (Optional supress confirm)\n");
fwrite(STDOUT, " repo add [fqdn]- Adds a package repository.\n");
fwrite(STDOUT, " repo remove [fqdn]- Removes a package repository.\n");
fwrite(STDOUT, " repo list - Displays all your current avaliable package repositories.\n");
fwrite(STDOUT, " checkperms - Checks permissions on the cache files.\n");
fwrite(STDOUT, " --version - Displays the Sentora version\n");
fwrite(STDOUT, " --help - Displays this text.\n\n");
exit(0);
}
?>