/** * Get input from user * @param string $prompt text prompt, should include possible options * @param string $default default value when enter pressed * @param array $options list of allowed options, empty means any text * @param bool $casesensitive true if options are case sensitive * @return string entered text */ function cli_input($prompt, $default = '', array $options = null, $casesensitiveoptions = false) { cli_writeln($prompt); cli_write(': '); $input = fread(STDIN, 2048); $input = trim($input); if ($input === '') { $input = $default; } if ($options) { if (!$casesensitiveoptions) { $input = strtolower($input); } if (!in_array($input, $options)) { cli_writeln(get_string('cliincorrectvalueretry', 'admin')); return cli_input($prompt, $default, $options, $casesensitiveoptions); } } return $input; }
/** * Get input from user * @param string $prompt text prompt, should include possible options * @param string $default default value when enter pressed * @param array $options list of allowed options, empty means any text * @param bool $casesensitive true if options are case sensitive * @return string entered text */ function cli_input($prompt, $default = '', array $options = null, $casesensitiveoptions = false) { echo $prompt; echo "\n: "; $input = fread(STDIN, 2048); $input = trim($input); if ($input === '') { $input = $default; } if ($options) { if (!$casesensitiveoptions) { $input = strtolower($input); } if (!in_array($input, $options)) { echo "Incorrect value, please retry.\n"; // TODO: localize, mark as needed in install return cli_input($prompt, $default, $options, $casesensitiveoptions); } } return $input; }
/** * Asks for the next pair of users' id to merge. * It also detects when anything but a number is introduced, to re-ask for any user id. */ public function next() { $record = new stdClass(); //ask for the source user id. $record->fromid = 0; while ($record->fromid <= 0 && $record->fromid != -1) { $record->fromid = intval(cli_input(get_string('cligathering:fromid', 'tool_mergeusers'))); } //if we have to exit, do it just now ;-) if ($record->fromid == -1) { $this->end = true; return; } //otherwise, ask for the target user id. $record->toid = 0; while ($record->toid <= 0 && $record->toid != -1) { $record->toid = intval(cli_input(get_string('cligathering:toid', 'tool_mergeusers'))); } //updates related iterator fields. $this->end = $record->toid == -1; $this->current = $record; $this->index++; }
$options['adminemail'] = cli_input($prompt); } // Validate that the address provided was an e-mail address. if (!empty($options['adminemail']) && !validate_email($options['adminemail'])) { $a = (object) array('option' => 'adminemail', 'value' => $options['adminemail']); cli_error(get_string('cliincorrectvalueerror', 'admin', $a)); } if ($interactive) { if (!$options['agree-license']) { cli_separator(); cli_heading(get_string('copyrightnotice')); echo "Moodle - Modular Object-Oriented Dynamic Learning Environment\n"; echo get_string('gpl3') . "\n\n"; echo get_string('doyouagree') . "\n"; $prompt = get_string('cliyesnoprompt', 'admin'); $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin'))); if ($input == get_string('clianswerno', 'admin')) { exit(1); } } } else { if (!$options['agree-license']) { cli_error(get_string('climustagreelicense', 'install')); } } // Finally we have all info needed for config.php $configphp = install_generate_configphp($database, $CFG); umask(0137); if (($fh = fopen($configfile, 'w')) !== false) { fwrite($fh, $configphp); fclose($fh);
if (!isset($options['dbpass'])) { cli_heading(get_string('databasepass', 'install')); $options['dbpass'] = cli_input(get_string('clitypevalue', 'admin')); } if (!isset($options['prefix'])) { cli_heading(get_string('dbprefix', 'install')); $options['prefix'] = cli_input(get_string('clitypevalue', 'admin')); } if (!isset($options['dbport'])) { cli_heading(get_string('dbport', 'install')); $options['dbport'] = cli_input(get_string('clitypevalue', 'admin')); } if ($CFG->ostype !== 'WINDOWS') { if (!isset($options['dbsocket'])) { cli_heading(get_string('databasesocket', 'install')); $options['dbsocket'] = cli_input(get_string('clitypevalue', 'admin')); } } $a = (object) array('dbtypefrom' => $CFG->dbtype, 'dbtype' => $options['dbtype'], 'dbname' => $options['dbname'], 'dbhost' => $options['dbhost']); cli_heading(get_string('transferringdbto', 'tool_dbtransfer', $a)); // Try target DB connection. $problem = ''; $targetdb = moodle_database::get_driver_instance($options['dbtype'], $options['dblibrary']); $dboptions = array(); if ($options['dbport']) { $dboptions['dbport'] = $options['dbport']; } if ($options['dbsocket']) { $dboptions['dbsocket'] = $options['dbsocket']; } try {
// First, show categories list to the user to pick a category $list = coursecat::make_categories_list(); echo "\nAvailable categories:\n"; foreach ($list as $key => $value) { echo "{$value} (id:{$key})\n"; } $prompt = "\nEnter category id"; // TODO: localize $categoryid = cli_input($prompt); // Validate if category id exists if (!($category = $DB->get_record('course_categories', array('id' => $categoryid)))) { cli_error("Can not find category '{$categoryid}'"); } $prompt = "You will reset courses in category {$category->name}. Are you sure? (y/n)"; // TODO: localize $confirm = cli_input($prompt); $errmsg = ''; //prevent eclipse warning if ($confirm != 'y') { cli_error($errmsg); } echo "Reset process started...\n\n"; // Get all the courses in the category $courses = get_courses($category->id, 'id', 'c.id, c.shortname, c.fullname'); // Get all the roles, excluding Manager $rolesToDelete = array(); $roles = get_all_roles(); foreach ($roles as $role) { $rolesToDelete[] = $role->id; } // For each course, reset data
if ($unrecognized) { $unrecognized = implode("\n ", $unrecognized); cli_error(get_string('cliunknowoption', 'admin', $unrecognized)); } if ($options['help']) { $help = "Reset local user passwords, useful especially for admin acounts.\n\nThere are no security checks here because anybody who is able to\nexecute this file may execute any PHP too.\n\nOptions:\n-h, --help Print out this help\n\nExample:\n\$sudo -u www-data /usr/bin/php admin/cli/reset_password.php\n"; //TODO: localize - to be translated later when everything is finished echo $help; die; } cli_heading('Password reset'); // TODO: localize $prompt = "enter username (manual authentication only)"; // TODO: localize $username = cli_input($prompt); if (!($user = $DB->get_record('user', array('auth' => 'manual', 'username' => $username, 'mnethostid' => $CFG->mnet_localhost_id)))) { cli_error("Can not find user '{$username}'"); } $prompt = "Enter new password"; // TODO: localize $password = cli_input($prompt); $errmsg = ''; //prevent eclipse warning if (!check_password_policy($password, $errmsg)) { cli_error($errmsg); } $hashedpassword = hash_internal_user_password($password); $DB->set_field('user', 'password', $hashedpassword, array('id' => $user->id)); echo "Password changed\n"; exit(0); // 0 means success
} $component = mlang_component::from_phpfile($filepath, $options['lang'], $version, $options['timemodified'], $options['name'], (int) $options['format']); fputs(STDOUT, "{$component->name} {$component->version->label} {$component->lang}" . PHP_EOL); $stage = new mlang_stage(); $stage->add($component); $stage->rebase(null, true, $options['timemodified']); if (!$stage->has_component()) { echo 'No strings found (after rebase)' . PHP_EOL; exit(4); } foreach ($stage->get_iterator() as $component) { foreach ($component->get_iterator() as $string) { if ($string->deleted) { $sign = '-'; } else { $sign = '+'; } echo $sign . ' ' . $string->id . PHP_EOL; } } echo PHP_EOL; $continue = cli_input('Continue? [y/n]', 'n', array('y', 'n')); if ($continue !== 'y') { echo 'Import aborted' . PHP_EOL; exit(5); } $meta = array('source' => 'import', 'userinfo' => $options['userinfo']); if ($options['commithash']) { $meta['commithash'] = $commithash; } $stage->commit($options['message'], $meta, true);
$parallelrun = behat_config_manager::get_parallel_test_runs($options['fromrun']); // Default torun is maximum parallel runs. if (empty($options['torun'])) { $options['torun'] = $parallelrun; } // Capture signals and ensure we clean symlinks. if (extension_loaded('pcntl')) { $disabled = explode(',', ini_get('disable_functions')); if (!in_array('pcntl_signal', $disabled)) { pcntl_signal(SIGTERM, "signal_handler"); pcntl_signal(SIGINT, "signal_handler"); } } // If empty parallelrun then just check with user if it's a run single behat test. if (empty($parallelrun)) { if (cli_input("This is not a parallel site, do you want to run single behat run? (Y/N)", 'n', array('y', 'n')) == 'y') { $runtestscommand = behat_command::get_behat_command(); $runtestscommand .= ' --config ' . behat_config_manager::get_behat_cli_config_filepath(); exec("php {$runtestscommand}", $output, $code); echo implode(PHP_EOL, $output) . PHP_EOL; exit($code); } else { exit(1); } } // Create site symlink if necessary. if (!behat_config_manager::create_parallel_site_links($options['fromrun'], $options['torun'])) { echo "Check permissions. If on windows, make sure you are running this command as admin" . PHP_EOL; exit(1); } $time = microtime(true);
} if ($options['help']) { $help = "Command line SQL global executer.\n\n Options:\n -h, --help Print out this help\n -f, --sql-file Requiered, complete path to the sql file to execute\n -i, --inc-nodes If specified purge only given nodes (comma separated shortname)\n -e, --exc-nodes If specified purge all nodes except the given ones (comma separated shortname)\n -s, --simulate If not specified or true SQL requests into file will only be displayed\n -a, --interactive If false confirm prompt will be disabled\n\n "; echo $help; die; } if (!file_exists($options['sql-file'])) { mtrace("Path given does not match to an existing file"); die; } if (!empty($options['inc-nodes']) && !empty($options['exc-nodes'])) { mtrace("You cannot use args inc-nodes and exc-nodes both together !"); die; } if (!$options['simulate'] && $options['interactive']) { $input = cli_input("Simulation is off, requests will be executed, continue ? (y/n)", '', array('y', 'n')); if ($input == 'n') { mtrace("operation aborted by user."); exit(1); } } $incNodes = ""; $excNodes = ""; $where = ""; $patternRNE = '/^[0-9]{7}[a-zA-Z]$/'; $protocol = substr($CFG->wwwroot, 0, strpos($CFG->wwwroot, '://')); if (!empty($options['inc-nodes'])) { $incNodes = explode(',', $options['inc-nodes']); foreach ($incNodes as $node) { if (!preg_match($patternRNE, $node) && $node != 'template' && $node != 'commun') { echo "Node '{$node}' is not a valid identifier (ex: RNE) !";
} if ($options['help']) { $help = "Command line Dialogue module cron.\n\nPlease note you must execute this script with the same uid as apache!\n\nOptions:\n--non-interactive No interactive questions or confirmations\n-h, --help Print out this help\n\nExample:\n\$sudo -u www-data /usr/bin/php mod/dialogue/cli/cron.php\n"; //TODO: localize - to be translated later when everything is finished echo $help; die; } // Force a debugging mode regardless the settings in the site administration //@error_reporting(1023); // NOT FOR PRODUCTION SERVERS! //@ini_set('display_errors', '1'); // NOT FOR PRODUCTION SERVERS! //$CFG->debug = 38911; // DEBUG_DEVELOPER // NOT FOR PRODUCTION SERVERS! //$CFG->debugdisplay = true; // NOT FOR PRODUCTION SERVERS! $interactive = empty($options['non-interactive']); if ($interactive) { $prompt = "Run Dialogue module cron? type y (means yes) or n (means no)"; $input = cli_input($prompt, '', array('n', 'y')); if ($input == 'n') { mtrace('exited'); exit; } } set_time_limit(0); $starttime = microtime(); // Increase memory limit raise_memory_limit(MEMORY_EXTRA); // Emulate normal session - we use admin accoutn by default cron_setup_user(); // Start output log $timenow = time(); mtrace("Server Time: " . date('r', $timenow) . "\n\n"); mtrace("Processing Dialogue module cron ...", '');
$posttext = '<html>'; $posttext .= '<h3>Evaluación de tu revisión</h3>'; $posttext .= 'Envío que revisaste:<br>' . $feedback->title . '<br>'; $posttext .= 'Evaluación:<br>' . $feedback->feedback; $posttext .= '</html>'; $subject = "Evaluación de tu revisión en {$workshop->name}"; $headers = "From: {$workshop->name} {$course->shortname} \r\n" . "Reply-To: noreply@webcursos.cloudlab.cl\r\n" . 'Content-Type: text/html; charset="utf-8"' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); $emailAlumno = str_replace("@uai.cl", "@alumnos.uai.cl", $feedback->reviewer); if ($i == 0) { echo "\nEmail sample:\n\n"; echo "To: {$emailAlumno} \n"; echo $headers . "\n"; echo $subject . "\n"; echo $posttext . "\n"; $prompt = "\nDou you want to send the messages (y/n)?"; // TODO: localize $yesno = cli_input($prompt); if ($yesno != "y") { echo "Bye\n"; exit(1); } } echo "Sending mail to:{$emailAlumno} "; mail($emailAlumno, $subject, $posttext, $headers); echo " ... sent!\n"; $i++; } echo "\n\n"; echo "{$i} emails sent successfully\n"; exit(0); // 0 means success