function cos_upgrade_to($version) { common::echoMessage("Will now pull source, and checkout latest tag", 'y'); $command = "git fetch --tags && git checkout master && git pull && git checkout {$version}"; $ret = common::execCommand($command); if ($ret) { common::abort('Aborting upgrade'); } common::echoMessage("Will upgrade vendor with composer according to version", 'y'); $command = "composer update"; $ret = common::systemCommand($command); if ($ret) { common::abort('Composer update failed.'); } common::echoMessage("Will upgrade all modules and templates the versions in the profile", 'y'); // Upgrade all modules and templates $profile = conf::getModuleIni('system_profile'); if (!$profile) { $profile = 'default'; } upgrade_from_profile(array('clone_only' => 1, 'profile' => $profile)); // reload any changes common::echoMessage("Reloading all configuration files", 'y'); $p = new profile(); $p->reloadProfile($profile); common::echoMessage("Load modules changes into database", 'y'); cos_config_reload(); }
/** * build source package with more simple form of install. * @param array $options */ function cos_build_simple($options = null) { $dir = getcwd(); $name = basename($dir); if (file_exists("./build/{$name}")) { common::execCommand("sudo rm -rf ./build/{$name}*"); } common::execCommand("mkdir ./build/{$name}"); $htdocs = "cp -rf htdocs/* ./build/{$name}"; common::execCommand($htdocs); $domain = conf::getMainIni('domain'); if (!$domain) { $domain = 'default'; } $files_rm = "sudo rm -rf ./build/{$name}/files/{$domain}/*"; common::execCommand($files_rm); $config = "mkdir ./build/{$name}/config"; common::execCommand($config); $tmp_dir = "mkdir ./build/{$name}/tmp"; common::execCommand($tmp_dir); $profiles = "cp -rf profiles ./build/{$name}"; common::execCommand($profiles); $sql_scripts = "cp -rf scripts ./build/{$name}"; common::execCommand($sql_scripts); $cli = "cp -rf coscli.sh ./build/{$name}"; common::execCommand($cli); $composer = "cp -rf composer.json ./build/{$name}"; common::execCommand($composer); // reset database password $ary = conf::getIniFileArray("./config/config.ini"); $profile = new profile(); $ary = $profile->iniArrayPrepare($ary); // clean ini settings for secrets $ini_settings = conf::arrayToIniFile($ary); // add ini dist file file_put_contents("./build/{$name}/config/config.ini-dist", $ini_settings); $index = "cp -rf htdocs/index.php ./build/{$name}/index.php"; common::execCommand($index); $phar_cli = "cp -rf phar-cli.php ./build/{$name}/"; common::execCommand($phar_cli); $phar_web = "cp -rf phar-web.php ./build/{$name}/"; common::execCommand($phar_web); $module_dir = conf::pathModules(); $modules = "cp -rf {$module_dir} ./build/{$name}"; common::execCommand($modules); $vendor = "cp -rf vendor ./build/{$name}"; common::execCommand($vendor); $rm_git = "rm `find ./build/{$name} -name '.git'` -rf"; common::execCommand($rm_git); $rm_ignore = "rm `find ./build/{$name} -name '.gitignore'` -rf"; common::execCommand($rm_ignore); $rm_doc = "rm -rf ./build/vendor/doc"; common::execCommand($rm_doc); $output = array(); exec('git tag -l', $output); $version = array_pop($output); $command = "cd ./build && tar -Pczf {$name}-{$version}.tar.gz {$name} "; common::execCommand($command); }
function miau_colors($options) { $ary = miau_get_colors(); foreach ($ary as $color) { common::echoMessage($color); $char = substr($color, 0, 1); echo common::colorOutput(miau_cat(), $char) . PHP_EOL; } }
function cron_install() { $mes = "Add the following line to your crontab"; common::echoMessage($mes); $command = diversen\conf::pathBase() . "/coscli.sh cron --run"; $command = "* * * * * {$command} 1>> /dev/null 2>&1"; common::echoMessage($command); return 0; }
function get_password() { $site_password = common::readSingleline('Enter system user password, and hit return: '); $site_password2 = common::readSingleline('Retype system user password, and hit return: '); if ($site_password == $site_password2) { return $site_password; } else { get_password(); } }
function cron_install() { $mes = "Add the following line to your crontab"; common::echoMessage($mes); $command = conf::pathBase() . "/coscli.sh cron --run"; $log = conf::pathBase() . "/logs/cron.log"; $command = "* * * * * {$command} >> {$log} 2>&1"; common::echoMessage($command); return 0; }
function google_translate_path($options) { if (!isset($options['path'])) { common::abort('You need to specify path to translate'); } if (!isset($options['target'])) { common::abort('You need to specify target language to translate into'); } $e = new google(); $key = conf::getMainIni('google_translate_key'); $e->key = $key; $e->setSingleDir($options['path']); $e->updateLang(); }
/** * function for upgrading a module * * @param array options the module to be upgraded */ function cos_menu_uninstall_menu($options) { // check if module exists in modules dir $module_path = conf::pathModules() . '/' . $options['module']; if (!file_exists($module_path)) { common::echoMessage("module {$options['module']} does not exists in modules dir. "); } $menu = new moduleinstaller($options); $res = $menu->deleteMenuItem($options['module']); if ($res) { if (conf::getMainIni('verbose')) { common::echoMessage("Main menu item for '{$options['module']}' deleted"); } } }
/** * function for purgeing a template * * @param array options */ function purge_template($options) { //uninstall_module($options); if (strlen($options['template']) == 0) { common::echoMessage("No such template: {$options['template']}"); common::abort(); } $template_path = conf::pathBase() . '/htdocs/templates/' . $options['template']; if (!file_exists($template_path)) { common::echoMessage("Template already purged: No such template path: {$template_path}"); common::abort(); } $command = "rm -rf {$template_path}"; common::execCommand($command); }
function multi_shared_exec_command($options = null) { $path = conf::pathBase() . "/config/multi/*"; if (!isset($options['command'])) { common::abort('Specify a command'); return 1; } $command = $options['command']; $dirs = file::getDirsGlob($path, array('basename' => 1)); foreach ($dirs as $domain) { $exec_command = "./coscli.sh --domain={$domain} {$command}"; common::echoMessage("Executing command: {$exec_command}"); passthru($exec_command, $return_var); } }
/** * wrapper function for creating documention with phpdoc * hi! I'am created with this function :) * * @return int value from cos_system command */ function create_docs() { // check if command exists $command = "whereis phpdoc"; $ret = common::execCommand($command); if ($ret) { common::echoMessage("Could not find command phpdoc on your system"); common::echoMessage("If the command phpdoc is not on your system we will not be able to create documentation."); common::echoMessage("One way to do this is to: pear install PhpDocumentor"); exit(127); } $command = "phpdoc run "; $command .= "-d coslib "; $command .= "--template abstract -t " . conf::pathBase() . "/htdocs/phpdocs "; common::systemCommand($command); }
/** * will update all translation files in specified language * @param array $options */ function translate_path($options) { if (!isset($options['path'])) { common::abort('Add a path'); } $path = conf::pathBase() . "/{$options['path']}"; if (!file_exists($path) or !is_dir($path)) { common::abort('Specify a dir as path'); } $e = new extractor(); if (!empty($options['language'])) { $e->defaultLanguage = $options['language']; } $e->setSingleDir($options['path']); $e->updateLang(); common::echoStatus('OK', 'g', 'Extraction done'); }
/** * adds an user by prompt * @param array $options * @return int $res */ function useradd_add($options = null) { $values['email'] = common::readSingleline("Enter Email of super user (you will use this as login): "); $values['password'] = common::readSingleline("Enter password: "); $values['password'] = md5($values['password']); $values['username'] = $values['email']; $values['verified'] = 1; $values['admin'] = 1; $values['super'] = 1; $values['type'] = 'email'; $res = useradd_db_insert($values); if ($res) { return 0; } else { return 1; } }
/** * executes a shell command on all coscms systems install in parent dir (../) from * current dir * @param string $command * @return int $ret */ function multi_exec_shell_command($options = array()) { $path = conf::pathBase() . "/../"; if (!isset($options['command'])) { common::abort('Specify a command'); return 1; } $command = $options['command']; $dirs = file::getDirsGlob($path, array('basename' => 1)); foreach ($dirs as $domain) { if (!file_exists("../{$domain}/config/config.ini")) { continue; } $exec_command = "cd ../{$domain} && {$command}"; multi_passthru($exec_command); } }
/** * function for checking if your are denying people * from e.g. admin areas of your module. */ function dev_test_access($options = null) { $files = file::getFileListRecursive(conf::pathModules(), "*module.php"); foreach ($files as $val) { $class_path = "modules" . str_replace(conf::pathModules(), '', $val); $class_path = str_replace('.php', '', $class_path); $class = str_replace('/', "\\", $class_path); $ary = get_class_methods($class); if (!is_array($ary)) { continue; } $call_paths = dev_get_actions($ary, $class_path); foreach ($call_paths as $path) { $url = conf::getSchemeWithServerName() . "{$path}"; $curl = new mycurl($url); $curl->createCurl(); echo $curl->getHttpStatus(); common::echoMessage(" Status code recieved on: {$url}"); } } }
function db_to_sqlite($options = array()) { $check = "which sequel"; if (common::execCommand($check)) { common::echoMessage('You need sequel. Install it like this, e.g.:'); common::echoMessage('sudo aptitude install ruby-sequel libsqlite3-ruby libmysql-ruby'); common::abort(); } else { common::echoStatus('OK', 'g', 'Sequel is installed'); } $ok = false; $info = admin::getDbInfo(); if (!$info) { return db_no_url(); } if ($info['scheme'] == 'mysql') { $ok = true; } if ($info['scheme'] == 'mysqli') { $ok = true; } if (!$ok) { common::echoStatus('ERROR', 'r', 'Driver needs to be mysql or mysqli'); } $fs = new Filesystem(); $fs->remove('sqlite/database.sql'); $username = conf::getMainIni('username'); $password = conf::getMainIni('password'); $command = "sequel "; $command .= "{$info['scheme']}://{$username}:{$password}@{$info['host']}/{$info['dbname']} "; $command .= "-C "; $command .= "sqlite://sqlite/database.sql"; $ret = common::systemCommand($command); $base = conf::pathBase(); if (!$ret) { $fs->chmod('sqlite/database.sql', 0777, 00, true); common::echoMessage('Sqlite database created. Edit config.ini and add:'); common::echoMessage("sqlite:/{$base}/sqlite/database.sql"); } }
/** * reads install info from modules/module_name/install.inc * @param array $options */ public function setInstallInfo($options) { // In profile all templates are also modules if (isset($options['module_name'])) { $template_name = $options['module_name']; } else { $template_name = $options['template']; } // Set dir and ini files $template_dir = conf::pathHtdocs() . "/templates/{$template_name}"; $ini_file = $template_dir . "/{$template_name}.ini"; $ini_file_dist = $template_dir . "/{$template_name}.ini-dist"; // Profile if (isset($options['profile'])) { $ini_file_dist = conf::pathBase() . "/profiles/{$options['profile']}/{$template_name}.ini-dist"; } // Generate ini file $this->generateInifile($ini_file, $ini_file_dist); if (file_exists($template_dir)) { $install_file = "{$template_dir}/install.inc"; if (!file_exists($install_file)) { common::echoMessage("Notice: No install file '{$install_file}' found in: '{$template_dir}'\n"); } $this->installInfo['NAME'] = $template_name; // if no version we check if this is a git repo if (!isset($this->installInfo['VERSION'])) { $this->setInstallInfoFromGit(); } if (file_exists($install_file)) { include $install_file; $this->installInfo = $_INSTALL; $this->installInfo['NAME'] = $template_name; $this->installInfo['RUN_LEVEL'] = 0; } } else { common::echoMessage("Notice: No template dir: {$template_dir}\n"); } }
/** * function for creating a skeleton for a module. We are just * using shell command touch for creating the files. * * @param array e.g: <code>array('module' => 'test')</code> This will create * the following folder /modules/test containing base files * used when writing a module * @return int result from shell operation 0 is success */ function create_module_skeleton($args) { $module_name = $args['module']; $ary = explode('/', $module_name); $module_path = conf::pathModules() . '/' . $module_name; common::execCommand('mkdir ' . $module_path); if (count($ary) == 1) { // create dirs for sql. Only need in a base module $mysql_dir = $module_path . "/mysql"; common::execCommand('mkdir ' . $mysql_dir); $mysql_up = $mysql_dir . "/up"; common::execCommand('mkdir ' . $mysql_up); $mysql_down = $mysql_dir . "/down"; common::execCommand('mkdir ' . $mysql_down); // create dirs for language. Only need in base module $lang_dir = $module_path . "/lang"; common::execCommand('mkdir ' . $lang_dir); $lang_dir_en = $module_path . "/lang/en_GB"; common::execCommand('mkdir ' . $lang_dir_en); $files = $module_path . "/menu.inc "; $files .= $module_path . "/views.php "; $files .= $module_path . "/README.md "; $files .= $module_path . "/install.inc "; $files .= $module_path . "/{$module_name}.ini "; $files .= $module_path . "/{$module_name}.ini-dist "; $files .= $module_path . "/module.php "; $files .= $lang_dir_en . "/system.inc "; $files .= $lang_dir_en . "/language.inc "; } common::echoMessage('Creating files: '); $res = common::execCommand('touch ' . $files); if (!$res) { // add a version $str = "<?php\n\n\$_INSTALL['VERSION'] = 1.71; \n"; $res = file_put_contents($module_path . "/install.inc", $str); $res; } }
function cos_phar_web_create() { // move sources to build dir. // e.g. build/coscms cos_build_simple(); // some base values $dir = getcwd(); $base = basename($dir); // dir we build phar from $build_from_dir = "{$dir}/build/{$base}"; // when creating a web phar we add // config.ini // this is done so that on first exectution of the // phar archive we will create this file and a .sqlite database. $build_phar_dir = "build/phar"; if (!file_exists($build_phar_dir)) { common::execCommand("mkdir {$build_phar_dir}"); } // hidden .config file // cos_exec("cp -f config/config.ini $build_from_dir/tmp/.config.ini"); // reset database password $ary = conf::getIniFileArray("./config/config.ini"); $profile = new profile(); // rm secrets $ary = $profile->iniArrayPrepare($ary); // add sqlite database to build phar dir if (conf::getMainIni('phar_sqlite')) { db_to_sqlite(); // mv sqlite database into hidden file common::execCommand("cp -R sqlite/database.sql {$build_from_dir}/tmp/.database.sql"); common::execCommand("chmod 777 {$build_from_dir}/tmp/.database.sql"); common::execCommand("mkdir {$build_from_dir}/sqlite"); unset($ary['db_init']); $ary['url'] = 'sqlite:.database.sql'; } // no caching of assets. Place css and js in file $ary['cached_assets'] = 0; $ary['cashed_assets_reload'] = 0; $ary['cached_assets_minify'] = 0; $ary['cached_assets_compress'] = 0; $ary['cached_assets_inline'] = 1; if (conf::getMainIni('phar_files')) { common::execCommand("cp -rf htdocs/files {$build_from_dir}"); common::execCommand("sudo chown -R 777 {$build_from_dir}/files"); } $ini_settings = conf::arrayToIniFile($ary); file_put_contents("{$build_from_dir}/tmp/.config.ini", $ini_settings); chdir($build_phar_dir); $output = "{$base}-web.phar"; $phar = new Phar($output); $phar->buildFromDirectory($build_from_dir); $stub = $phar->createDefaultStub('phar-web.php', 'phar-web.php'); $phar->setStub($stub); $phar->stopBuffering(); echo "Web phar executable file created from current source ({$output})\n"; echo "Serve it e.g. with the built-in server. Like this:\n"; echo "cd {$build_phar_dir}\n"; echo "php -S localhost:8080 {$base}-web.phar\n"; exit(0); }
/** * function for loading a database file into db specified in config.ini * * @param array options. You can specifiy a file to load in options. * e.g. <code>$options = array('File' => 'backup/sql/latest.sql')</code> * @return int the executed commands shell status 0 on success. */ function cos_db_load_table($options) { if (!isset($options['table'])) { common::abort('Specify a table to load with a backup'); } $dump_dir = "backup/sql/{$options['table']}"; if (!file_exists($dump_dir)) { common::abort('Yet no backups'); } $search = conf::pathBase() . "/backup/sql/{$options['table']}"; $latest = get_latest_db_dump($search); if ($latest == 0) { common::abort('Yet no database dumps'); } $latest = "backup/sql/{$options['table']}/" . $latest . ".sql"; $db = admin::getDbInfo(); if (!$db) { return db_no_url(); } $command = "mysql --default-character-set=utf8 -u" . conf::$vars['coscms_main']['username'] . " -p" . conf::$vars['coscms_main']['password'] . " {$db['dbname']} < {$latest}"; return $ret = common::execCommand($command); }
/** * Upgrade to a specific version of a module * @param float $specific, e.g. '5.06' * @return boolean $res */ public function upgrade($specific = null) { // Only upgrade if module is installed if (!moduleloader::isInstalledModule($this->installInfo['NAME'])) { common::echoMessage("Notice: Can not upgrade. You will need to install module first"); return; } // Get specific version if (!isset($specific)) { $specific = $this->installInfo['VERSION']; } // Get current module version from registry $row = $this->getModuleDbInfo(); $current_version = $row['module_version']; // Same version. Return if ($current_version == $specific) { $this->confirm = "Module '" . $this->installInfo['NAME'] . "'. Version is '{$specific}'. No upgrade to perform"; return true; } // Get a list of SQL updates to perform $updates = $this->getSqlFileListOrdered($this->installInfo['NAME'], 'up'); // perform sql upgrade if (!empty($updates)) { foreach ($updates as $key => $val) { $possible_versions = ''; $version = substr($val, 0, -4); if ($version == $specific) { $version_exists = true; } else { $possible_versions .= "{$version} "; } } if (!isset($version_exists)) { $this->error = 'Module SQL ' . $this->installInfo['NAME'] . " "; $this->error .= 'does not have such a version. Possible version are: '; $this->error .= $possible_versions; } // perform SQL updates found in .sql files foreach ($updates as $key => $val) { $version = substr($val, 0, -4); if ($current_version < $version) { $this->executeSqlUpgrade($version); } } } // update registry $this->updateRegistry($specific, $row['id']); if ($specific > $current_version) { $this->confirm = "Module '" . $this->installInfo['NAME'] . "'. "; $this->confirm .= "Version '" . $specific . "' installed. "; $this->confirm .= "Upgraded from {$current_version}"; return true; } else { $this->confirm = "Module '" . $this->installInfo['NAME'] . "'. Nothing to upgrade. Module version is still {$current_version}"; return true; } }
/** * function for showing git tags (just for testing) * @param array $options */ function cos_git_echo_remote_tags($options) { if (empty($options['repo'])) { common::abort('You need to specify a repo'); } $tags = git::getTagsRemote($options['repo']); if (empty($tags)) { common::abort('No tags'); } $latest = git::getTagsRemoteLatest($options['repo']); common::echoMessage("Latest is: {$latest}"); }
function clone_db($options = array()) { if (!isset($options['File'])) { common::abort('Specify new database name'); } $db = admin::getDbInfo(conf::getMainIni('url')); $old = $db['dbname']; $new_name = $options['File']; admin::cloneDB($old, $new_name); }
/** * Method that disable an apache2 site * * @param array $options only options is $options[sitename] */ public static function disableSite($options) { common::needRoot(); $hostname = trim($options['hostname']); $apache2_conf_file = "/etc/apache2/sites-available/{$hostname}"; $ret = common::execCommand("a2dissite {$hostname}"); if ($ret) { return false; } $version = self::getVersion(); $version = version::getSemanticAry($version); if ($version['minor'] >= 4) { $apache2_conf_file .= ".conf"; } $ret = common::execCommand("rm -f {$apache2_conf_file}"); // create new hosts file and reload server $host_file_str = ''; $hosts_file_str = file("/etc/hosts"); $host_search = "127.0.0.1\t{$hostname}\n"; $str = ""; foreach ($hosts_file_str as $key => $val) { if (strstr($val, $host_search)) { continue; } else { $host_file_str .= $val; } } file_put_contents("tmp/hosts", $host_file_str); common::execCommand("cp -f tmp/hosts /etc/hosts"); common::execCommand("/etc/init.d/apache2 reload"); }
/** * get tags for a system git repo * @return string $tags tags as a string */ public static function getTagsInstall() { $command = "git tag -l"; $ret = exec($command, $output); return common::parseShellArray($output); }
/** * CLI command. Function for restoring tar archive * All file settings are restored (if user is the owner of all files) * * @param array options to parser, e.g. * <code>array('File' => '/backup/full/latest.tar')</code> This will restore * the tar achive /backup/full/latest.tar * * Leave options empty if you * want to restore latest archive with highest timestamp, .e.g * backup/full/1264168904.tar * @return int the executed commands shell status 0 on success. */ function backup_files_restore($options) { // in order to easily preserve ownership we use need to run as root if (!isset($options['File'])) { $latest = backup_get_latest_backup('files'); if ($latest == 0) { die("Yet no backups\n"); } $backup_file = $latest = "backup/files/" . $latest . ".tar.gz"; } else { $backup_file = $options['File']; } common::needRoot("In order to restore: {$backup_file}. We need root"); $command = "tar -Pxzf {$backup_file} -v ./htdocs/files "; $ret = common::execCommand($command); }
/** * Checks if any table exist in database * @return boolean */ public static function tablesExists() { $db = new db(); $ret = $db->connect(array('dont_die' => 1)); if ($ret == 'NO_DB_CONN') { return false; } $info = admin::getDbInfo(); if (!$info) { common::echoMessage('No databse url in config.ini'); } if ($info['scheme'] == 'mysql' || $info['scheme'] == 'mysqli') { $rows = $db->selectQuery("SHOW TABLES"); if (empty($rows)) { return false; } return true; } if ($info['scheme'] == 'sqlite') { $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name='modules'"; $rows = $db->selectQuery($sql); if (empty($rows)) { return false; } return true; } }
/** * Method for setting info about profile * @param string $profile name of the profile */ public function setProfileInfo($profile) { $profile_dir = conf::pathBase() . "/profiles/{$profile}"; if (!file_exists($profile_dir)) { common::abort("No such path to profiles: {$profile_dir}"); } include $profile_dir . "/profile.inc"; $this->profileModules = $_PROFILE_MODULES; $this->profileTemplates = $_PROFILE_TEMPLATES; $this->profileTemplate = $_PROFILE_TEMPLATE; }
/** * function for updating a modules .ini file with new settings * from updated ini-dist file. * * @param array $options */ function upgrade_config_ini_file($options) { $ini_file_path = conf::pathBase() . "/config/config.ini"; $ini_dist_path = conf::pathBase() . "/profiles/{$options['profile']}/config.ini-dist"; $ini_file = conf::getIniFileArray($ini_file_path, true); $ini_dist = conf::getIniFileArray($ini_dist_path, true); $ary = array_merge($ini_dist, $ini_file); $ary_diff = array_diff($ary, $ini_file); $content = conf::arrayToIniFile($ary); file_put_contents($ini_file_path, $content); if (empty($ary_diff)) { common::echoMessage("No new ini file settings for config.ini"); } else { $new_settings_str = conf::arrayToIniFile($ary_diff); common::echoMessage("New ini file written to: {$ini_file_path}"); common::echoMessage("These are the new ini settings for config.ini"); common::echoMessage(trim($new_settings_str)); } }
/** * function for removing all files in htdocs/files/*, htdocs/logo/* * when doing an install * * @return int value from exec command */ function cos_create_files() { $files_path = conf::pathBase() . '/logs/coscms.log'; if (!file_exists($files_path)) { $command = "touch {$files_path}"; common::execCommand($command); } $files_path = conf::pathBase() . '/logs/cron.log'; if (!file_exists($files_path)) { $command = "touch {$files_path}"; common::execCommand($command); } $files_path = conf::pathFiles(); if (!file_exists($files_path)) { $command = "mkdir -p {$files_path}"; common::execCommand($command); } }