/** * Method for enabling an apache2 site * The script does the following: * - create access.log and error.log in ./logs * - create virtual configuration and put it in sites-available * - enable new site * - create new /etc/hosts file * * @param array $options $options[sitename] */ public static function enableSite($options) { $hostname = trim($options['hostname']); self::createLogs(); common::needRoot(); // create apache2 conf and enable site $apache2_conf = self::getA2Conf($hostname); $tmp_file = conf::pathBase() . "/tmp/{$hostname}"; file_put_contents($tmp_file, $apache2_conf); $apache2_conf_file = "/etc/apache2/sites-available/{$hostname}"; // some changes in apache 2.4.x from apache 2.2.x // se http://httpd.apache.org/docs/current/upgrading.html $version = self::getVersion(); $version = version::getSemanticAry($version); if ($version['minor'] >= 4) { $apache2_conf_file .= ".conf"; } common::execCommand("cp -f tmp/{$hostname} {$apache2_conf_file}"); common::execCommand("a2ensite {$hostname}"); // create new hosts file and reload server // not very exact match $hosts_file_str = file_get_contents("/etc/hosts"); $host_str = "127.0.0.1\t{$hostname}\n"; if (!strstr($hosts_file_str, $host_str)) { $new_hosts_file_str = $host_str . $hosts_file_str; file_put_contents("tmp/hosts", $new_hosts_file_str); common::execCommand("cp -f tmp/hosts /etc/hosts"); } common::execCommand("/etc/init.d/apache2 reload"); }
/** * Sets a controller and a action */ private function setControllerAction() { // Parse url. 2 cases: // a) Default controller // b) Module controller // Get all modules $mod_path = conf::pathBase() . "/" . $this->modules; $mods = file::getDirsGlob($mod_path, array('basename' => true)); $base = direct::fragment(0); if (!in_array($base, $mods)) { // Could not find a controller / module $this->controller = $this->default; $action = direct::fragment(0); if ($action) { $this->action = $action; } } else { // Found a controller / module $this->controller = $base; $action = direct::fragment(1); if ($action) { $this->action = $action; } } }
/** * get tags for a module or template * @param string $module * @param string $type 'module' or 'template' * @return array|false a array or false */ public static function getTagsModule($module, $type = 'module') { if ($type == 'module') { $path = conf::pathModules() . "/{$module}"; } if ($type == 'template') { $path = conf::pathHtdocs() . "/templates/{$module}"; } $command = "cd {$path} && git tag -l"; exec($command, $ary, $ret); // ok if ($ret == 0) { $str = shell_exec($command); $ary = explode("\n", $str); $tags = array(); foreach ($ary as $line) { trim($line); if (empty($line)) { continue; } $tags[] = $line; } } else { return false; } return $tags; }
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); }
public function modulesAutoLoader($classname) { $class = str_replace('\\', '/', $classname) . ""; $class = conf::pathBase() . "/" . ($class .= ".php"); if (file_exists($class)) { require $class; } }
public static function assets($options = null) { $path = conf::pathBase() . "/htdocs/files/default/cached_assets"; if (file_exists($path)) { file::rrmdir($path); } return 1; }
/** * highlight a text using geshi. * in your text you will need, e.g. for PHP, * <code>[hl:php]<?php echo "hello world";?>[/hl:php]</code> * @param string $text string to filter. * @return string $text the filtered text */ public function filter($article) { if (conf::getMainIni('filters_allow_files')) { $article = self::filterGeshiFile($article); } $article = self::filterGeshiInline($article); return $article; }
/** * set default timezone (date_default_timezone) */ public static function setTimezone() { // if no user timezone if (!conf::getMainIni('date_default_timezone')) { conf::setMainIni('date_default_timezone', 'Europe/Copenhagen'); } date_default_timezone_set(conf::getMainIni('date_default_timezone')); }
function cache_clear_all($options = null) { if (conf::isCli()) { common::needRoot(); } clear::all(); return 0; }
/** * set SSL for mysql */ public static function setSsl() { $attr = conf::getMainIni('mysql_attr'); if (isset($attr['mysql_attr'])) { self::$dbh->setAttribute(PDO::MYSQL_ATTR_SSL_KEY, $attr['ssl_key']); self::$dbh->setAttribute(PDO::MYSQL_ATTR_SSL_CERT, $attr['ssl_cert']); self::$dbh->setAttribute(PDO::MYSQL_ATTR_SSL_CA, $attr['ssl_ca']); } }
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 for inserting user * @param array $values * @return boolean $res */ function useradd_db_insert($values) { $database = admin::getDbInfo(conf::getMainIni('url')); if (!$database) { return db_no_url(); } $db = new db(); $res = $db->insert('account', $values); return $res; }
/** * calculate a video ratio * @param int $default * @return int $ratio the ratio */ public static function videoRatio($default = 600) { $width = conf::getMainIni('media_width'); if ($width) { return $ratio = $width / $default; } else { // default ration is 1 return 1; } }
/** * Connect to existing database handle with RedBeans */ public static function connectExisting() { static $connected = null; if (!$connected) { R::setup(connect::$dbh); $connected = true; } $freeze = conf::getMainIni('rb_freeze'); if ($freeze == 1) { R::freeze(true); } }
/** * returns favicon html * @return string $html */ public static function getFaviconHTML() { $favicon = conf::getMainIni('favicon'); $domain = conf::getDomain(); $rel_path = "/files/{$domain}/favicon/{$favicon}"; $full_path = conf::pathHtdocs() . "/{$rel_path}"; if (!is_file($full_path)) { $rel_path = '/favicon.ico'; } $str = "<link rel=\"shortcut icon\" href=\"{$rel_path}\" type=\"image/x-icon\" />\n"; return $str; }
/** * Get HTML template for email * NOTE: Email needs to be set in config/config.ini when using CLI * @param string $path path to template * @return string $html */ public function getHtmlTemplate($path = null) { // Default path if (!$path) { $template = conf::getMainIni('template'); $path = conf::getTemplatePath($template) . '/mail/template.html'; } if (!file_exists($path)) { log::error('mailer/markdown: path does not exists: ' . $path); die; } $email = file_get_contents($path); return $email; }
/** * Set a log level based on env and debug */ public static function setLogLevel() { $env = conf::getEnv(); if ($env == 'development') { error_reporting(E_ALL); } // check if we are in debug mode and display errors if (conf::getMainIni('debug')) { ini_set('display_errors', 1); } if (conf::isCli()) { self::setErrorLog(); } }
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 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); }
/** * best guess for getting locales * If locale is set directly in configuration (or db override) we will * use this locale. * @return string */ public static function getLocale() { // Check in main config setting 'locale' $locale = conf::getMainIni('locale'); if ($locale) { return $locale; } // If locale is not set we use language .utf8 $language = conf::getMainIni('language'); if ($language) { return $language . ".utf8"; } // System locale return self::getSystemLocale(); }
/** * 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"); } } }
/** * setup a Redbean instance from CosCMS */ public static function connect() { static $connected = null; if (!$connected) { $url = conf::getMainIni('url'); $username = conf::getMainIni('username'); $password = conf::getMainIni('password'); R::setup($url, $username, $password); $freeze = conf::getMainIni('rb_freeze'); if ($freeze == 1) { R::freeze(true); } $connected = true; } }
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); }
/** * 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); } }
/** * 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'); }
protected function _doImages_inline_callback($matches) { $whole_match = $matches[1]; $alt_text = $matches[2]; $url = $matches[3] == '' ? $matches[4] : $matches[3]; $title =& $matches[7]; $alt_text = $this->encodeAttribute($alt_text); $url = $this->encodeAttribute($url); $type = $this->getType($url); if ($type == 'mp4') { $full_path = conf::pathHtdocs() . $url; return "![{$alt_text}]({$url})"; } if ($this->isImage($url)) { return "![{$alt_text}](" . $this->uploadImage($url) . ")"; } return; }
/** * gets database info from cinfuguration * @return array|false $ary false if no url is given. Array with db url info */ public static function getDbInfo($url = null) { if (!$url) { $url = conf::getMainIni('url'); } if (empty($url)) { return false; } $url = parse_url($url); $ary = explode(';', $url['path']); foreach ($ary as $val) { $a = explode("=", $val); if (isset($a[0], $a[1])) { $url[$a[0]] = $a[1]; } } return $url; }
/** * method for getting html for front page. If no logo has been * uploaded. You will get logo as html * @param type $options options to give to html::createHrefImage * @return string $str the html compsoing the logo or main title */ public static function getLogoHTML($options = array()) { $logo = conf::getMainIni('logo'); if (!$logo) { $title = $_SERVER['HTTP_HOST']; $link = html::createLink('/', $title); return $str = "<div id=\"logo_title\">{$link}</div>"; } else { $file = "/logo/" . conf::getMainIni('logo'); $src = conf::getWebFilesPath($file); if (!isset($options['alt'])) { $options['alt'] = $_SERVER['HTTP_HOST']; } $href = html::createHrefImage('/', $src, $options); $str = '<div id="logo_img">' . $href . '</div>' . "\n"; //die($str); return $str; } }