Ejemplo n.º 1
0
 /**
  * Connects to the database with configuration defined from the config.php and sets correct modes.
  */
 private function connectToDatabase()
 {
     $this->_pPDO = Database::getInstance();
     $this->_pPDO->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
 }
Ejemplo n.º 2
0
 /**
  * Management gets the added bonus of being able to restore deleted
  * messages, in case of abuse. That'll be dealt with in here.
  * 
  * @param string $sNickname The nickname we'll be restoring the message of.
  * @param string $sRestoredBy The nickname of the person who restored it.
  * @return boolean
  */
 public function restore($sNickname, $sRestoredBy)
 {
     if ($this->exists($sNickname)) {
         return false;
     }
     $pStatement = Database::getInstance()->prepare("UPDATE\n                                samp_welcome_message\n                        SET\n                                is_deleted = 0,\n                                last_edit_by = ?\n                        WHERE\n                                nickname = ?");
     if ($pStatement === false) {
         return false;
     }
     $pStatement->bind_param('ss', $sRestoredBy, $sNickname);
     if (!$pStatement->execute()) {
         return false;
     }
     $pStatement->close();
     $this->m_aWelcomeMessages[$sNickname] = '';
     $pStatement = Database::getInstance()->prepare("SELECT\n                                welcome_message\n                        FROM\n                                samp_welcome_message\n                        WHERE\n                                nickname = ?");
     if ($pStatement === false) {
         return false;
     }
     $pStatement->bind_param('s', $sNickname);
     $pStatement->bind_result($this->m_aWelcomeMessages[$sNickname]);
     $pStatement->execute();
     $pStatement->fetch();
     return true;
 }
Ejemplo n.º 3
0
 /**
  * This function will immediatly execute the evaluation that we're doing
  * right now. Evaluation delays are already handled by the parser.
  *
  * @param Bot $pBot Bot that should handle the evaluation.
  * @param string $sDestination Where should the output be redirected?
  * @param array $aOptions Array with the parsed options for this evaluation.
  */
 public function doEvaluation(Bot $pBot, $sDestination, $aOptions)
 {
     ob_start();
     ErrorExceptionHandler::$Source = $sDestination;
     switch ($aOptions['Type']) {
         case 'PHP':
             eval($aOptions['Operation']);
             break;
         case 'EXEC':
             $process = proc_open($aOptions['Operation'], array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), $pipes);
             if (is_resource($process)) {
                 fclose($pipes[0]);
                 $output = trim(stream_get_contents($pipes[1]));
                 fclose($pipes[1]);
                 $error = trim(stream_get_contents($pipes[2]));
                 fclose($pipes[2]);
                 $returnValue = proc_close($process);
                 if ($output != '') {
                     echo $output . ' ';
                 }
                 $haveNewLine = false;
                 if ($error != '') {
                     echo PHP_EOL . ModuleBase::COLOUR_RED . '* Error' . ModuleBase::CLEAR . ': ' . $error . ' ';
                     $haveNewLine = true;
                 }
                 if ($returnValue !== 0) {
                     if (!$haveNewLine) {
                         echo PHP_EOL;
                     } else {
                         echo '| ';
                     }
                     echo ModuleBase::COLOUR_TEAL;
                     if (!$haveNewLine) {
                         echo '* ';
                     }
                     echo 'Return value' . ModuleBase::CLEAR . ': ' . $returnValue;
                     $haveNewLine = true;
                 }
             }
             break;
         case 'SQL':
             $pDatabase = Database::getInstance();
             if ($pDatabase == null || $pDatabase->connect_error) {
                 echo '4* Database Error: Could not connect to database';
                 if ($pDatabase != null) {
                     echo ': "' . $pDatabase->connect_error . '"';
                 }
                 echo '.';
                 break;
             }
             $pResult = $pDatabase->query($aOptions['Operation']);
             if ($pResult === false) {
                 echo '4* Database Error: ' . $pDatabase->error;
             } else {
                 if ($pResult->num_rows == 0) {
                     echo '10* No rows.';
                 } else {
                     $aFields = $aFieldLen = array();
                     foreach ($pResult->fetch_fields() as $pField) {
                         $aFields[$pField->name] = array($pField->name);
                         $aFieldLen[$pField->name] = strlen($pField->name);
                     }
                     while (($aRow = $pResult->fetch_assoc()) != null) {
                         foreach ($aRow as $sField => $mValue) {
                             if ($mValue == null) {
                                 $mValue = 'NULL';
                             }
                             $aFields[$sField][] = $mValue;
                             $aFieldLen[$sField] = max($aFieldLen[$sField], strlen((string) $mValue));
                         }
                     }
                     // Output is imminent.
                     for ($i = 0; $i <= $pResult->num_rows; $i++) {
                         // 1 extra loop for the header.
                         foreach (array_keys($aFields) as $sField) {
                             echo sprintf('| ' . ($i == 0 ? '' : '') . '%\'' . chr(160) . '-' . $aFieldLen[$sField] . 's' . ($i == 0 ? '' : '') . ' ', $aFields[$sField][$i]);
                         }
                         echo '|' . PHP_EOL;
                     }
                 }
             }
             break;
     }
     $aOutput = explode("\n", trim(ob_get_clean()));
     if (count($aOutput) > $aOptions['MaxLines'] + 1 && $aOptions['Buffering'] == true) {
         $nSize = count($aOutput);
         $aOutput = array_slice($aOutput, 0, $aOptions['MaxLines']);
         $aOutput[] = '10* Displayed ' . $aOptions['MaxLines'] . ' out of ' . $nSize . ' lines.';
     }
     if (isset($pBot) && $pBot instanceof Bot) {
         foreach ($aOutput as $sLine) {
             $pBot->send('PRIVMSG ' . $sDestination . ' :' . trim($sLine), false);
         }
     }
 }