Exemplo n.º 1
0
 /** Processes pre-parsing CLI arguments.
  * Processes the arguments who have to be processed after config loading (so they are prioritary on config, they overwrite it).
  * 
  * \returns TRUE if everything gone fine, FALSE if an error occured.
  */
 public function processCLIPostparsingArguments($CLIArguments, $logPos)
 {
     $logContent = '';
     foreach ($CLIArguments as $name => $value) {
         switch ($name) {
             //Change display language
             case 'l':
             case 'lang':
                 Leelabot::message('Changed locale by CLI : $0', array($value));
                 if (!$this->intl->setLocale($value)) {
                     Leelabot::message('Cannot change locale to "$0"', array($value), E_WARNING);
                 } else {
                     Leelabot::message('Locale successfully changed to "$0".', array($value));
                 }
                 break;
             case 'no-log':
                 //Don't use a log file
                 Leelabot::message('Disabling log (by CLI).');
             case 'log':
                 //Define the log file in another place than the default
                 if ($name == 'log') {
                     Leelabot::message('Changing log file to $0 (By CLI)', array($value));
                 }
                 //Save log content for later parameters (like --nolog -log file.log)
                 if (Leelabot::$_logFile) {
                     $logContent = '';
                     fseek(Leelabot::$_logFile, $logPos);
                     while (!feof(Leelabot::$_logFile)) {
                         $logContent .= fgets(Leelabot::$_logFile);
                     }
                     $logFileInfo = stream_get_meta_data(Leelabot::$_logFile);
                     fclose(Leelabot::$_logFile);
                     //If the file was empty before logging into it, delete it
                     if ($logPos == 0) {
                         unlink($logFileInfo['uri']);
                     }
                     Leelabot::$_logFile = FALSE;
                 }
                 if ($name == 'no-log') {
                     break;
                 }
                 //Load new file, and put the old log content into it (if opening has not failed, else we re-open the old log file)
                 if (!(Leelabot::$_logFile = fopen($value, 'a+'))) {
                     Leelabot::$_logFile = fopen($logFileInfo['uri'], 'a+');
                     Leelabot::message('Cannot open new log file ($0), reverting to old.', array($value), E_WARNING);
                     break;
                 }
                 fseek(Leelabot::$_logFile, 0, SEEK_END);
                 $logPos = ftell(Leelabot::$_logFile);
                 fputs(Leelabot::$_logFile, $logContent);
                 break;
             case 'erase-log':
                 $logFileInfo = stream_get_meta_data(Leelabot::$_logFile);
                 Leelabot::message('Erasing all previous log in $0...', array($logFileInfo['uri']));
                 //Rewinding log file to start of current session log
                 fseek(Leelabot::$_logFile, $logPos);
                 //Dumping new log data before wiping old log
                 $log = '';
                 while (!feof(Leelabot::$_logFile)) {
                     $log .= fgets(Leelabot::$_logFile);
                 }
                 //Closing file and reopening it in w+ mode, ensures that previous data is wiped, then write current log in the now-empty file
                 fclose(Leelabot::$_logFile);
                 Leelabot::$_logFile = fopen($logFileInfo['uri'], 'w+');
                 fputs(Leelabot::$_logFile, $log);
                 break;
         }
     }
 }