/** * Tests the server to determine an appropriate value for the * password hash_cost value. * * By default it will find a value based on a target of 50ms of * processing time, which is suitable for interactive logins. * * @param int $target_time The number of milliseconds to target. */ public function index($target_time = 50) { // Convert the milliseconds to seconds. $target_time = $target_time / 1000; CLI::write('Testing for password hash value with a target time of ' . $target_time . ' seconds...'); // Taken from the PHP manual $cost = 8; do { $cost++; $start = microtime(true); password_hash("test", PASSWORD_BCRYPT, ["cost" => $cost]); $end = microtime(true); } while ($end - $start < $target_time); CLI::write("Hash value should be set to: " . CLI::color($cost, 'green')); }
protected function sayDescriptions($descriptions) { $names = array_keys($descriptions); $syntaxes = array_column($descriptions, 0); $descs = array_column($descriptions, 1); // Pad each item to the same length $names = $this->padArray($names); $syntaxes = $this->padArray($syntaxes); for ($i = 0; $i < count($names); $i++) { $out = CLI::color($names[$i], 'yellow'); // The rest of the items stay default color. if (isset($syntaxes[$i])) { $out .= $syntaxes[$i]; } if (isset($descs[$i])) { $out .= CLI::wrap($descs[$i], 125, strlen($names[$i]) + strlen($syntaxes[$i])); } CLI::write($out); } }
/** * Creates a new migration file ready to be used. * * @param $name */ public function newMigration($name = null, $type = 'app') { if (empty($name)) { $name = CLI::prompt('Migration name? '); if (empty($name)) { return CLI::error("\tYou must provide a migration name.", 'red'); } } $this->load->library('migration'); $path = $this->migration->determine_migration_path($type); // Does the alias path exist in our config? if (!$path) { return CLI::error("\tThe migration path for '{$type}' does not exist.'"); } // Does the path really exist? if (!is_dir($path)) { return CLI::error("\tThe path for '{$type}' is not a directory."); } // Is the folder writeable? if (!is_writeable($path)) { return CLI::error("\tThe folder for '{$type}' migrations is not writeable."); } $file = $this->migration->make_name($name); $path = rtrim($path, '/') . '/' . $file; $contents = <<<EOT <?php /** * Migration: {clean_name} * * Created by: SprintPHP * Created on: {date} */ class Migration_{name} extends CI_Migration { public function up () { } //-------------------------------------------------------------------- public function down () { } //-------------------------------------------------------------------- } EOT; $contents = str_replace('{name}', $name, $contents); $contents = str_replace('{date}', date('Y-m-d H:i:s a'), $contents); $contents = str_replace('{clean_name}', ucwords(str_replace('_', ' ', $name)), $contents); $this->load->helper('file'); if (write_file($path, $contents)) { return CLI::write("\tNew migration created: " . CLI::color($file, 'yellow'), 'green'); } return CLI::error("\tUnkown error trying to create migration: {$file}", 'red'); }
/** * Resumes the running of tasks after the system has been disabled * with the `disable` command. */ public function enable() { if (!Settings::save('is_disabled', 'n', 'cron')) { return CLI::error('Unknown problem saving the setting. ' . CLI::color('Cron jobs will NOT run!', 'yellow')); } CLI::write('Done'); }
/** * Injects a block of code into an existing file. Using options * you can specify where the code should be inserted. Available options * are: * prepend - Place at beginning of file * append - Place at end of file * before => '' - Insert just prior to this line of text (don't forget the line ending "\n") * after => '' - Insert just after this line of text (don't forget the line ending "\n") * replace => '' - a simple string to be replaced. All locations will be replaced. * regex => '' - a pregex pattern to use to replace all locations. * * @param $path * @param $content * @param array $options * * @return $this */ public function injectIntoFile($path, $content, $options = 'append') { $kit = new FileKit(); if (is_string($options)) { $action = $options; } else { if (is_array($options) && count($options)) { $keys = array_keys($options); $action = array_shift($keys); $param = $options[$action]; } } switch (strtolower($action)) { case 'prepend': $success = $kit->prepend($path, $content); break; case 'before': $success = $kit->before($path, $param, $content); break; case 'after': $success = $kit->after($path, $param, $content); break; case 'replace': $success = $kit->replaceIn($path, $param, $content); break; case 'regex': $success = $kit->replaceWithRegex($path, $param, $content); break; case 'append': default: $success = $kit->append($path, $content); break; } if ($success) { CLI::write(CLI::color("\t" . strtolower(lang('modified')) . " ", 'cyan') . str_replace(APPPATH, '', $path)); } else { CLI::write(CLI::color("\t" . strtolower(lang('error')) . " ", 'light_red') . str_replace(APPPATH, '', $path)); } return $this; }
/** * Handles actually installing the tables and correct authentication * type. */ private function install() { CLI::write("Available Auth Types: " . CLI::color('basic, digest, none', 'yellow')); $this->auth_type = trim(CLI::prompt('Auth type')); switch ($this->auth_type) { case 'basic': $this->setupBasic(); $this->readme('readme_basic.txt'); break; case 'digest': $this->setupDigest(); $this->readme('readme_digest.txt'); break; } $this->setupLogging(); }
/** * Overrides CLIController's version to support searching our * collections for the help description. * * @param null $method */ public function longDescribeMethod($method = null) { $collections = config_item('forge.collections'); if (!is_array($collections) || !count($collections)) { return CLI::error('No generator collections found.'); } // We loop through each collection scanning // for any generator folders that have a // 'forge.php' file. For each one found // we build out another section in our help commands foreach ($collections as $alias => $path) { $path = rtrim($path, '/ ') . '/'; $folders = scandir($path); if (!($i = array_search(ucfirst($method), $folders))) { continue; } $dir = $path . $folders[$i] . '/'; if (!is_file($dir . '/forge.php')) { CLI::error("The {$method} command does not have any cli help available."); } include $dir . '/forge.php'; // Don't have valid arrays to work with? Move along... if (!isset($long_description)) { log_message('debug', '[Forge] Invalid forge.php file at: ' . $dir . '/forge.php'); continue; } if (empty($long_description)) { return CLI::error("The {$method} command does not have an cli help available."); } CLI::new_line(); CLI::write(CLI::color(ucfirst($method) . ' Help', 'yellow')); return CLI::write(CLI::wrap($long_description, CLI::getWidth())); } // Still here? CLI::error("No help found for command: {$method}"); }