Example #1
0
 /**
  * @param $APIKey
  * @return DIRT|mixed
  * @throws \Exception
  */
 public function init($APIKey)
 {
     if (!isset(self::$dirt['loaded']) || !self::$dirt['loaded']) {
         throw new \Exception('DIRT needs to be initialized');
     }
     try {
         // Run APIKey through a regex to prevent any SQL Injection
         $APIKey = preg_replace("/[^A-Fa-f0-9]/", '', $APIKey);
         // Check if the DIRT object has been cached in sessions[].
         $pdo = array_search($APIKey, self::$sessions, true);
         if ($pdo === false) {
             /* Not cached so let's grab it from the mysql metadata in the mysql.user table */
             /* TODO: Add support for different databases instead of just mySQL */
             $stmt = parent::query("SELECT user FROM mysql.user WHERE authentication_string IS NOT NULL AND authentication_string = '{$APIKey}'");
             $user = $stmt->fetchAll(self::FETCH_ASSOC);
             $user['User'] = '******';
             # uncomment for testing
             if (count($user) === 1) {
                 $actualPassword = $APIKey + self::$dirt['salt'];
                 $userName = $user['User'];
                 $actualPassword = '******';
                 #uncomment to test
                 $userName = '******';
                 #uncomment to test
                 // TODO: have method for optional parms instead of null
                 $pdo = new DIRT(self::$dirt['dsn'], $userName, $actualPassword, null);
                 if (self::$dirt['cacheSession'] === true) {
                     self::$sessions[] = $pdo;
                 }
                 $dbName = $pdo->query('select database()')->fetchColumn();
                 $pdo::$databaseName = $dbName;
                 return $pdo;
             } else {
                 die("Multiple API keys in mysql.user");
             }
         } else {
             return $pdo;
             # dirt object was cached.
         }
     } catch (\PDOException $pdoException) {
         throw $pdoException;
     }
 }
Example #2
0
 public function __construct(DIRT $dirt, $tableName, $baseWhere = '')
 {
     /* Keep the dirt object and table name as protected fields */
     $this->dirt = $dirt;
     $this->tableName = $tableName;
     $this->baseWhereClause = $baseWhere;
     /* We query the given table with no records returned so we can get the column info */
     $stmt = $dirt->query("SELECT * FROM {$tableName} LIMIT 0", \PDO::FETCH_ASSOC);
     /* Save the column count and iterate through each column and save the metadata for each column */
     $this->columnCount = $stmt->columnCount();
     for ($idx = 0; $idx < $this->columnCount; $idx++) {
         $columnMeta = $stmt->getColumnMeta($idx);
         # Get the meta data for this column
         $columnName = $columnMeta['name'];
         # Work around for warning array-->string
         $this->columnMeta[$columnName] = $columnMeta;
         # Keep the meta data in the protected array
         $this->row[$columnName] = null;
         # The initial vale for all columns is null
     }
 }