/** * Create a symlink * @method symlink * @static * @private * @param {string} $target * @param {string} $link */ private static function symlink($target, $link) { // Make sure destination directory exists if (!file_exists(dirname($link))) { $mask = umask(Q_Config::get('Q', 'internal', 'umask', 00)); mkdir(dirname($link), 0777, true); umask($mask); } $is_win = substr(strtolower(PHP_OS), 0, 3) === 'win'; if (is_dir($link) && !$is_win && !is_link($link)) { echo Q_Utils::colored("[WARN] Symlink '{$link}' (target: '{$target}') was not created" . PHP_EOL, 'red', 'yellow'); return; } if (file_exists($target)) { if ($is_win && is_dir($link)) { rmdir($link); } else { if (is_link($link)) { unlink($link); } } } if ($is_win) { exec('mklink /j "' . $link . '" "' . $target . '"'); } else { @symlink($target, $link); } if (!file_exists($link)) { echo Q_Utils::colored("[WARN] Symlink '{$link}' (target: '{$target}') was not created" . PHP_EOL, 'red', 'yellow'); return; } }
/** * Return colored text that you can output in logs or text mode * Pass an exception or * @param {string|Exception} $exception The exception or an exception message. If the later, you must pass three more arguments. * @param {string} [$file] * @param {string} [$line] * @param {string} [$trace] * @return {string} */ static function coloredString($message, $file = null, $line = null, $trace = null) { if ($message instanceof Exception) { $e = $message; $traceString = is_callable(array($e, 'getTraceAsStringEx')) ? $e->getTraceAsStringEx() : $e->getTraceAsString(); return self::coloredString($e->getMessage(), $e->getFile(), $e->getLine(), $traceString); } $colors = Q_Config::get('Q', 'exception', 'colors', array()); Q::autoload('Q_Utils'); $fields = array('message' => $message, 'fileAndLine' => "in {$file} ({$line})", 'trace' => $trace); foreach ($fields as $f => $v) { $c0 = isset($colors[$f][0]) ? $colors[$f][0] : null; $c1 = isset($colors[$f][1]) ? $colors[$f][1] : null; $fields[$f] = Q_Utils::colored($v, $c0, $c1); } $reset = Q_Utils::colored("", "", ""); return "{$fields['message']}\n\n{$fields['fileAndLine']}\n{$fields['trace']}\n"; }
/** * @method installPlugin * @static * @param {string} $plugin_name * @param {array} $options * @throws {Exception} */ static function installPlugin($plugin_name, $options) { set_time_limit(Q_Config::expect('Q', 'install', 'timeLimit')); // Connect Qbix platform if it's not already connected self::prepare(); $app_dir = APP_DIR; $plugin_dir = Q_PLUGINS_DIR . DS . $plugin_name; $app_web_plugins_dir = APP_WEB_DIR . DS . 'plugins'; echo "Installing plugin '{$plugin_name}' into '{$app_dir}'" . PHP_EOL; // Do we even have such a plugin? if (!is_dir($plugin_dir)) { throw new Exception("Plugin '{$plugin_name}' not found in " . Q_PLUGINS_DIR); } // Ensure that the plugin has config.json if (!file_exists($plugin_conf_file = $plugin_dir . DS . 'config' . DS . 'plugin.json')) { throw new Exception("Could not load plugin's config. Check {$plugin_conf_file}"); } $files_dir = $plugin_dir . DS . 'files'; $app_plugins_file = APP_LOCAL_DIR . DS . 'plugins.json'; // Check access to $app_web_plugins_dir if (!file_exists($app_web_plugins_dir)) { if (!@mkdir($app_web_plugins_dir, 0755, true)) { throw new Exception("Could not create {$app_web_plugins_dir}"); } } if (!is_dir($app_web_plugins_dir)) { throw new Exception("{$app_web_plugins_dir} exists, but is not a directory"); } elseif (!is_writable($app_web_plugins_dir)) { throw new Exception("Can not write to {$app_web_plugins_dir}"); } // Check access to $app_plugins_file if (file_exists($app_plugins_file) && !is_writable($app_plugins_file)) { throw new Exception("Can not write to {$app_plugins_file}"); } elseif (!file_exists($app_plugins_file) && !is_writable(dirname($app_plugins_file))) { throw new Exception("Can not write to " . dirname($app_plugins_file)); } // Check access to $files_dir if (!file_exists($files_dir)) { if (!@mkdir($files_dir, $options['dirmode'], true)) { throw new Exception("Could not create {$files_dir}"); } } // Do we now have plugin's config? if (Q_Config::get('Q', 'pluginInfo', $plugin_name, 'version', null) == null) { throw new Exception("Could not identify plugin version. Check {$plugin_conf_file}"); } $plugin_conf = Q_Config::get('Q', 'pluginInfo', $plugin_name, null); $plugin_version = $plugin_conf['version']; if (file_exists($app_plugins_file)) { Q_Config::load($app_plugins_file, true); } // Do we already have this plugin installed for this app? // Check requirements for plugin (will throw exceptions if they aren't met) if (!isset($options['noreq']) || !$options['noreq']) { echo "Checking requirements" . PHP_EOL; Q_Bootstrap::checkRequirements(array($plugin_name)); } // Checking LOCAL plugin version in plugins.json file if (($version_installed = Q_Config::get('Q', 'pluginLocal', $plugin_name, 'version', null)) != null) { //We have this plugin installed echo "Plugin '{$plugin_name}' (version: {$version_installed}) is already installed" . PHP_EOL; if (Q::compareVersion($version_installed, $plugin_version) < 0) { echo "Upgrading '{$plugin_name}' to version: {$plugin_version}" . PHP_EOL; } } // Check and fix permissions self::checkPermissions($files_dir, $options); if (isset($plugin_conf['permissions'])) { foreach ($plugin_conf['permissions'] as $perm) { self::checkPermissions($files_dir . DS . $perm, $options); } } // Symbolic links echo 'Creating symbolic links' . PHP_EOL; Q_Utils::symlink($plugin_dir . DS . 'web', $app_web_plugins_dir . DS . $plugin_name); // Checking if schema update is requested and updating database version $connections = Q_Config::get('Q', 'pluginInfo', $plugin_name, 'connections', array()); foreach ($connections as $connection) { self::installSchema(Q_PLUGINS_DIR . DS . $plugin_name, $plugin_name, 'plugin', $connection, $options); } // Push plugin name into Q/plugins array if (!in_array($plugin_name, $current_plugins = Q_Config::get('Q', 'plugins', array()))) { $current_plugins[] = $plugin_name; Q_Config::set('Q', 'plugins', $current_plugins); //TODO: When do we save Q/plugins to disk? } // Save info about plugin echo 'Registering plugin' . PHP_EOL; Q_Config::set('Q', 'pluginLocal', $plugin_name, $plugin_conf); Q_Config::save($app_plugins_file, array('Q', 'pluginLocal')); echo Q_Utils::colored("Plugin '{$plugin_name}' successfully installed" . PHP_EOL, 'green'); }