Example #1
0
 /**
  * Get postgres notifications
  * @return array
  */
 public function getNotification()
 {
     return pg_get_notify($this->resource->get());
 }
Example #2
0
 /**
  * getNotification
  *
  * Get pending notifications. If no notifications are waiting, NULL is
  * returned. Otherwise an associative array containing the optional data
  * and de backend's PID is returned.
  *
  * @access public
  * @return array|null
  */
 public function getNotification()
 {
     $data = pg_get_notify($this->handler, \PGSQL_ASSOC);
     return $data === false ? null : $data;
 }
Example #3
0
 /**
  * @return array|null
  */
 public function getNextNotification()
 {
     return pg_get_notify($this->handler, \PGSQL_ASSOC) ?: null;
 }
 if ($res === FALSE) {
     fail("Failed to listen for matchmaking events.");
 }
 pg_free_result($res);
 info("Listening on the matchmaking channel.");
 // Choose a random number of seconds to wait for matchmaking events, to get a
 // mix of bots.
 $wait = 1 + ord(openssl_random_pseudo_bytes(1)) % 10;
 info("Waiting {$wait} seconds for matchmaking events.");
 sleep($wait);
 info("Waiting completed.");
 // Collect all of the notifications that were sent during our nap, and any new
 // ones that crop up while we're at it.
 while (TRUE) {
     // Try to read a notification from the channel.
     $res = pg_get_notify($db, PGSQL_ASSOC);
     if ($res === FALSE) {
         break;
     }
     // Interpret the payload as a bot's ID.
     $bot = (int) $res["payload"];
     info("Received notification from bot with ID '{$bot}'.");
     array_push($bots, $bot);
 }
 // Ensure that there are no duplicate IDs in our array.
 $bots = array_unique($bots);
 // Don't go any further if there are no bots.
 if (count($bots) === 0) {
     continue;
 }
 pg_query("BEGIN;");
 /**
  * Calls a database function, such as mysql_connect or mysql_query, using lookup tables
  *
  * @return void
  * @author Joseph Todd Parsons <*****@*****.**>
  */
 private function functionMap($operation)
 {
     $args = func_get_args();
     /* TODO: consistent responses (e.g. FALSE on failure) */
     switch ($this->driver) {
         case 'mysql':
             switch ($operation) {
                 case 'connect':
                     $this->connection = mysql_connect("{$args['1']}:{$args['2']}", $args[3], $args[4]);
                     if ($this->getVersion) {
                         $this->version = $this->setDatabaseVersion(mysql_get_server_info($this->connection));
                     }
                     return $this->connection;
                     break;
                 case 'error':
                     return mysql_error(isset($this->connection) ? $this->connection : null);
                     break;
                 case 'close':
                     if ($this->connection) {
                         $function = mysql_close($this->connection);
                         unset($this->connection);
                         return $function;
                     } else {
                         return true;
                     }
                     break;
                 case 'selectdb':
                     return mysql_select_db($args[1], $this->connection);
                     break;
                 case 'escape':
                     return mysql_real_escape_string($args[1], $this->connection);
                     break;
                 case 'query':
                     return mysql_query($args[1], $this->connection);
                     break;
                 case 'insertId':
                     return mysql_insert_id($this->connection);
                     break;
                 case 'startTrans':
                     $this->rawQuery('START TRANSACTION');
                     break;
                 case 'endTrans':
                     $this->rawQuery('COMMIT');
                     break;
                 case 'rollbackTrans':
                     $this->rawQuery('ROLLBACK');
                     break;
                 default:
                     $this->triggerError("[Function Map] Unrecognised Operation", array('operation' => $operation), 'validation');
                     break;
             }
             break;
         case 'mysqli':
             switch ($operation) {
                 case 'connect':
                     $this->connection = new mysqli($args[1], $args[3], $args[4], $args[5] ? $args[5] : null, (int) $args[2]);
                     if ($this->connection->connect_error) {
                         $this->newError('Connect Error (' . $this->connection->connect_errno . ') ' . $this->connection->connect_error);
                     } else {
                         if ($this->getVersion) {
                             $this->version = $this->setDatabaseVersion($this->connection->server_info);
                         }
                         return $this->connection;
                     }
                     break;
                 case 'error':
                     if ($this->connection->connect_errno) {
                         return $this->connection->connect_errno;
                     } else {
                         return $this->connection->error;
                     }
                     break;
                 case 'selectdb':
                     return $this->connection->select_db($args[1]);
                     break;
                 case 'close':
                     /*return $this->connection->close();*/
                     break;
                 case 'escape':
                     return $this->connection->real_escape_string($args[1]);
                     break;
                 case 'query':
                     return $this->connection->query($args[1]);
                     break;
                 case 'insertId':
                     return $this->connection->insert_id;
                     break;
                 case 'startTrans':
                     $this->connection->autocommit(false);
                     break;
                     // Use start_transaction in PHP 5.5
                 // Use start_transaction in PHP 5.5
                 case 'endTrans':
                     $this->connection->commit();
                     $this->connection->autocommit(true);
                     break;
                 case 'rollbackTrans':
                     $this->connection->rollback();
                     $this->connection->autocommit(true);
                     break;
                 default:
                     $this->triggerError("[Function Map] Unrecognised Operation", array('operation' => $operation), 'validation');
                     break;
             }
             break;
         case 'pdo':
             switch ($operation) {
                 case 'connect':
                     // var_dump($args); die();
                     try {
                         $this->connection = new PDO("mysql:dbname={$args['5']};host={$args['1']}:{$args['2']}", $args[3], $args[4]);
                     } catch (PDOException $e) {
                         $this->connection->errorCode = $e->getFile();
                         die($this->connection->errorCode);
                         return false;
                     }
                     $this->version = $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
                     $this->activeDatabase = $args[5];
                     return $this->connection;
                     break;
                 case 'error':
                     return $this->connection->errorCode;
                     break;
                 case 'selectdb':
                     return $this->rawQuery("USE " . $this->formatValue("database", $args[1]));
                     break;
                     // TODO test
                 // TODO test
                 case 'close':
                     unset($this->connection);
                     return true;
                     break;
                 case 'escape':
                     switch ($args[2]) {
                         case 'string':
                         case 'search':
                             return $this->connection->quote($args[1], PDO::PARAM_STR);
                             break;
                         case 'integer':
                         case 'timestamp':
                             return $this->connection->quote($args[1], PDO::PARAM_STR);
                             break;
                         case 'column':
                         case 'columnA':
                         case 'table':
                         case 'tableA':
                         case 'database':
                             return $args[1];
                             break;
                         default:
                             $this->triggerError('Invalid context.', array('arguments' => $args), 'validation');
                             break;
                     }
                     break;
                     // Note: long-term, we should implement this using prepared statements.
                 // Note: long-term, we should implement this using prepared statements.
                 case 'query':
                     return $this->connection->query($args[1]);
                     break;
                 case 'insertId':
                     return $this->connection->lastInsertId();
                     break;
                 case 'startTrans':
                     $this->connection->beginTransaction();
                     break;
                     // Use start_transaction in PHP 5.5
                 // Use start_transaction in PHP 5.5
                 case 'endTrans':
                     $this->connection->commit();
                     break;
                 case 'rollbackTrans':
                     $this->connection->rollBack();
                     break;
                 default:
                     $this->triggerError("[Function Map] Unrecognised Operation", array('operation' => $operation), 'validation');
                     break;
             }
             break;
         case 'pgsql':
             switch ($operation) {
                 case 'connect':
                     return pg_connect("host={$args['1']} port={$args['2']} username={$args['3']} password={$args['4']} dbname={$args['5']}");
                     break;
                 case 'error':
                     return pg_last_error($this->dbLink);
                     break;
                 case 'close':
                     return pg_close($this->dbLink);
                     break;
                 case 'escape':
                     return pg_escape_string($this->dbLink, $args[1]);
                     break;
                 case 'query':
                     return pg_query($this->dbLink, $args[1]);
                     break;
                 case 'insertId':
                     return $this->rawQuery('SELECT LASTVAL()')->getAsArray(false, false, 0);
                     break;
                     // Note: Returning is by far the best solution, and should be considered in future versions. This would require defining the insertId column, which might be doable.
                 // Note: Returning is by far the best solution, and should be considered in future versions. This would require defining the insertId column, which might be doable.
                 case 'notify':
                     return pg_get_notify($this->dbLink);
                     break;
                 default:
                     $this->triggerError("[Function Map] Unrecognised Operation", array('operation' => $operation), 'validation');
                     break;
             }
             break;
     }
 }
Example #6
0
    while ($do_loop) {
        if ($verbose > 2) {
            echo "Mailer awaken.\n";
        }
        if (!Send_Mails($verbose, $dry_run)) {
            if ($verbose > 1) {
                echo "Mail sending failed.\n";
            }
        }
        if ($notify_mode) {
            if (pg_wait_notify($dbh->_connectionID, 300000) === false) {
                // TODO: check more thoroughly
                echo "Wait failed, db connection broken?\n";
                break;
            }
            if (($nots = pg_get_notify($dbh->_connectionID)) !== false) {
                if ($verbose > 2) {
                    print_r($nots);
                }
            }
            //break;
        } else {
            sleep(300);
        }
    }
} else {
    // just run once
    if (Send_Mails($verbose, $dry_run)) {
        exit(0);
    } else {
        exit(1);
Example #7
0
<?php

// optional functions
include 'config.inc';
$db = pg_connect($conn_str);
pg_query($db, 'LISTEN test_msg');
pg_query($db, 'NOTIFY test_msg');
$msg = pg_get_notify($db);
isset($msg['message'], $msg['pid']) ? print 'OK' : (print 'NG');
Example #8
0
 public function getWarning()
 {
     $e = "";
     if (pg_get_notify(self::$connection[self::$active])) {
         $e = pg_get_notify(self::$connection[self::$active]);
         //   $e = implode(" / ", $e);
     }
     return $e;
 }
Example #9
0
<?php

require_once 'pgproc/php/pgprocedures.php';
require_once 'config.inc.php';
$base = new PgProcedures2($pg_host, $pg_user, $pg_pass, $pg_database);
$base->execute_sql('LISTEN toto');
for ($i = 0; $i < 60; $i++) {
    $res = pg_get_notify($base->handler);
    if (isset($res['message'])) {
        print_r($res);
    }
    sleep(1);
}