/** * 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; }
/** * 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 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 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 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; } }
/** * Checks if an ini file exists, and creates an ini file if it not exists. * @return boolean $res true on success and false on failure. */ public function createIniFile() { $module = $this->installInfo['NAME']; $module_path = conf::pathModules(); $ini_file = "{$module_path}/{$module}/{$module}.ini"; $ini_file_dist = "{$module_path}/{$module}/{$module}.ini-dist"; if (!file_exists($ini_file) && file_exists($ini_file_dist)) { if (!copy($ini_file_dist, $ini_file)) { $this->error = "Error: Could not copy {$ini_file} to {$ini_file_dist}" . PHP_EOL; return false; } } $ini_file_php = "{$module_path}/{$module}/config.php"; $ini_file_dist_php = "{$module_path}/{$module}/config.php-dist"; if (!file_exists($ini_file_php) && file_exists($ini_file_dist_php)) { if (!copy($ini_file_dist_php, $ini_file_php)) { $this->error = "Error: Could not copy {$ini_file_php} to {$ini_file_dist_php}" . PHP_EOL; return false; } } return true; }
/** * loads all modules in database */ public static function loadDbModules() { if (!self::tablesExists()) { common::echoMessage('No tables exists. We can not load all modules'); return; } $mod_loader = new moduleloader(); $modules = moduleloader::getAllModules(); foreach ($modules as $val) { if (isset($val['is_shell']) && $val['is_shell'] == 1) { moduleloader::includeModule($val['module_name']); $path = conf::pathModules() . "/{$val['module_name']}/{$val['module_name']}.inc"; if (file_exists($path)) { include_once $path; } } } }
/** * method for getting all parsed blocks * @todo clearify what is going on * @param string $block * @return array blocks containing strings with html to display */ public static function parseBlock($block) { $blocks = array(); if (isset(conf::$vars['coscms_main'][$block], conf::$vars['coscms_main']['module'][$block])) { $blocks = array_merge(conf::$vars['coscms_main'][$block], conf::$vars['coscms_main']['module'][$block]); } else { if (isset(conf::$vars['coscms_main'][$block])) { $blocks = conf::$vars['coscms_main'][$block]; } else { if (isset(conf::$vars['coscms_main']['module'][$block])) { $blocks = conf::$vars['coscms_main']['module'][$block]; } else { return $blocks; } } } $ret_blocks = array(); foreach ($blocks as $val) { // numeric is custom block added to database if (is_numeric($val)) { moduleloader::includeModule('blocks'); $row = blocks::getOne($val); $row['content_block'] = moduleloader::getFilteredContent(conf::getModuleIni('blocks_filters'), $row['content_block']); $row['title'] = htmlspecialchars($row['title']); $content = view::get('blocks', 'block_html', $row); $ret_blocks[] = $content; continue; } if ($val == 'module_menu') { $ret_blocks[] = self::getMainMenu(); continue; } $func = explode('/', $val); $num = count($func) - 1; $func = explode('.', $func[$num]); $func = 'block_' . $func[0]; $path_to_function = conf::pathModules() . "/{$val}"; include_once $path_to_function; ob_start(); $ret = $func(); if ($ret) { $ret_blocks[] = $ret; } else { $ret_blocks[] = ob_get_contents(); ob_end_clean(); } } return $ret_blocks; }
/** * Method for loading all module's configuration, contained in a profile * @param string $profile name of the profile */ public function loadProfileFiles($profile) { $profile_dir = conf::pathBase() . "/profiles/{$profile}"; foreach ($this->profileModules as $key => $val) { $source = $profile_dir . "/{$val['module_name']}.ini-dist"; $dest = conf::pathModules() . "/{$val['module_name']}/{$val['module_name']}.ini"; $path_module = conf::pathModules() . "/{$val['module_name']}"; if (!file_exists($path_module)) { continue; } if (file_exists($source)) { copy($source, $dest); $this->confirm[] = "Copy {$source} to {$dest}"; } // If a PHP config.php file exists, then copy that too. $source = $profile_dir . "/{$val['module_name']}.php-dist"; $dest = conf::pathModules() . "/{$val['module_name']}/config.php"; if (file_exists($source)) { copy($source, $dest); } } foreach ($this->profileTemplates as $key => $val) { $source = $profile_dir . "/{$val['module_name']}.ini-dist"; $dest = conf::pathHtdocs() . "/templates/{$val['module_name']}/{$val['module_name']}.ini"; $path_module = conf::pathHtdocs() . "/templates/{$val['module_name']}"; if (!file_exists($path_module)) { continue; } if (file_exists($source)) { copy($source, $dest); $this->confirm[] = "Copy {$source} to {$dest}"; } // If a PHP config.php file exists, then copy that too. $source = $profile_dir . "/{$val['module_name']}.php-dist"; $dest = conf::pathHtdocs() . "/templates/{$val['module_name']}/config.php"; if (file_exists($source)) { copy($source, $dest); } } }
/** * include a module. This will include the module file * and load language and configuration * @param string $module * @retur boolean true on success and false on failure */ public static function includeModule($module) { if (isset(self::$loadedModules['loaded'][$module])) { return true; } // find base module. // only in base modules does ini settings exists $ary = explode('/', $module); $base_module = $ary[0]; // lang and ini only exists in base module self::setModuleIniSettings($base_module); // include module file $module_file = conf::pathModules() . "/{$module}/module.php"; if (file_exists($module_file)) { self::$loadedModules['loaded'][$module] = true; require_once $module_file; } return true; }
/** * Set CSS from a module name and a CSS file. This can then be overridded * in a template * @param string $module the module in context, e.g. account * @param string $css the CSS file in context, e.g. /assets/style.css * @param int $order the loading order of css 0 is first > 0 is * later. */ public static function setModuleInlineCss($module, $css, $order = null, $options = array()) { $module_css = conf::pathModules() . "/{$module}/{$css}"; $template_name = layout::getTemplateName(); $template_override = "/templates/{$template_name}/{$module}{$css}"; if (file_exists(conf::pathHtdocs() . $template_override)) { self::setCss($template_override); return; } self::setInlineCss($module_css); }
/** * function for adding and commiting all modules and templates * @param array options from cli env */ function cos_git_commit_module_single($options) { $path = conf::pathModules() . "/{$options['repo']}"; if (!cos_git_is_repo($path)) { common::abort("Module: {$options['repo']} is not a git repo. Specify installed module name (e.g. 'settings') when commiting"); } $p = new profile(); $mod = $p->getModule($options['repo']); cos_git_commit($mod, 'module'); }
/** * Include a set of module function used for a module. These * functions will be overridden in template if they exists in a template * @param string $module * @param string $file * @return void */ public static function includeOverrideFunctions($module, $file) { // Check for view in template $template = layout::getTemplateName(); if ($template) { $override = conf::pathHtdocs() . "/templates/{$template}/{$module}/{$file}"; if (is_file($override)) { include_once $override; return; } } include_once conf::pathModules() . "/{$module}/{$file}"; }
/** * function for updating a modules .ini file with new settings * from updated ini-dist file. * * @param array $options */ function update_ini_file($options) { if (!isset($options['module'])) { echo "Specify module\n"; exit(1); } $ini_file_path = conf::pathModules() . "/{$options['module']}/{$options['module']}.ini"; $ini_dist_path = $ini_file_path . "-dist"; $ini_file = conf::getIniFileArray($ini_file_path); $ini_dist = conf::getIniFileArray($ini_dist_path); $new_settings = array(); foreach ($ini_dist as $key => $val) { if (!isset($ini_file[$key])) { $ini_file[$key] = $val; // used for displaying which settings were updated. $new_settings[$key] = $val; } } // write it to ini file $content = conf::arrayToIniFile($ini_file); file_put_contents($ini_file_path, $content); // install profile. if (!empty($new_settings)) { $new_settings_str = conf::arrayToIniFile($new_settings); common::echoMessage("New ini file written with updated settings: {$ini_file_path}"); common::echoMessage("These are the new ini settings for module {$options['module']}:"); common::echoMessage(trim($new_settings_str)); } }
/** * include a set of module function used for e.g. templates. These * functions can be overridden in template if they exists in a template * @param string $module * @param string $file * @return void */ public static function includeOverrideFunctions($module, $file) { // only template who has set name will be able to override this way // templage_name = 'clean' $template = layout::getTemplateName(); if ($template) { $override = conf::pathHtdocs() . "/templates/{$template}/{$module}/{$file}"; if (is_file($override)) { include_once $override; return; } } include_once conf::pathModules() . "/{$module}/{$file}"; }