/**
  * Create a new translatable email
  * 
  * @param DBObject $context
  * @param string $translation_id
  * @param array $variables
  * 
  * @return TranslatableEmail
  */
 public static function create($translation_id, $variables)
 {
     $email = new self();
     // Get translation data and variables
     $email->translation_id = $translation_id;
     $email->variables = array();
     if ($variables) {
         foreach ($variables as $k => $v) {
             // Convert DBObject types to type/id pairs for saving
             if ($v instanceof DBObject) {
                 $v = array('dbobject_type' => get_class($v), 'dbobject_id' => $v->id);
             }
             $email->variables[$k] = $v;
         }
     }
     // Add meta
     $email->created = time();
     // Generate token until it is indeed unique
     $email->token = Utilities::generateUID(function ($token) {
         $statement = DBI::prepare('SELECT * FROM ' . TranslatableEmail::getDBTable() . ' WHERE token = :token');
         $statement->execute(array(':token' => $token));
         $data = $statement->fetch();
         return !$data;
     });
     $email->save();
     return $email;
 }
Exemplo n.º 2
0
 /**
  * Constructor
  * 
  * @param integer $id identifier of user to load from database (null if loading not wanted)
  * @param array $data data to create the notes_log from (if already fetched from database)
  * 
  * @throws NotesLogNotFoundException
  */
 protected function __construct($id = null, $data = null)
 {
     if (!is_null($id)) {
         $statement = DBI::prepare('SELECT * FROM ' . self::getDBTable() . ' WHERE id = :id');
         $statement->execute(array(':id' => $id));
         $data = $statement->fetch();
         if (!$data) {
             throw new NotesLogNotFoundException('id = ' . $id);
         }
     }
     if ($data) {
         $this->fillFromDBData($data);
     }
 }
Exemplo n.º 3
0
 /**
  * Constructor
  * 
  * @param integer $id identifier of user to load from database (null if loading not wanted)
  * @param array $data data to create the user from (if already fetched from database)
  * 
  * @throws UserNotFoundException
  */
 protected function __construct($id = null, $data = null)
 {
     if (!is_null($id)) {
         // Load from database if id given
         $statement = DBI::prepare('SELECT * FROM ' . self::getDBTable() . ' WHERE id = :id');
         $statement->execute(array(':id' => $id));
         $data = $statement->fetch();
     }
     if ($data) {
         // Fill properties from provided data
         $this->fillFromDBData($data);
         $this->hasPreferences = true;
     } else {
         // New user, set base data
         $this->id = $id;
         $this->created = time();
     }
     // Generate user remote auth secret
     if (Config::get('auth_remote_user_autogenerate_secret') && !$this->auth_secret) {
         $this->auth_secret = hash('sha256', $this->id . '|' . time() . '|' . Utilities::generateUID());
         $this->save();
     }
 }
 public function customDelete()
 {
     // Remove from database
     $s = DBI::prepare('DELETE FROM ' . static::getDBTable() . ' WHERE pad_id = :pad_id AND guest_id = :guest_id');
     $s->execute(array('pad_id' => $this->pad_id, 'guest_id' => $this->guest_id));
     // Remove from object cache
     self::purgeCache(get_called_class(), $this->pad_id);
 }
Exemplo n.º 5
0
 public static function getByID($guestID)
 {
     $query = 'SELECT * FROM ' . static::getDBTable() . " where id = :id";
     $statement = DBI::prepare($query);
     $statement->execute(array('id' => $guestID));
     $objects = array();
     return $statement->fetch();
 }
Exemplo n.º 6
0
 /**
  * This function allwso to know if current pad has guest passed in parameters
  * 
  * @param GuestComponent $guest
  * @return boolean true if has,n false otherwhise
  */
 public function hasGuest($guest)
 {
     $tmpGuest = GuestComponent::getByEmail($guest->email);
     if ($tmpGuest) {
         $query = 'SELECT * FROM ' . PadGuestComponent::getDBTable() . " where pad_id = :pad_id AND guest_id = :guest_id ";
         $statement = DBI::prepare($query);
         $statement->execute(array('pad_id' => $this->id, 'guest_id' => $guest->id));
         $datas = $statement->fetch();
         if (is_array($datas)) {
             return true;
         }
     }
     return false;
 }
Exemplo n.º 7
0
 /**
  * Generate a new survey id
  * 
  * @return string
  * 
  * @throws SurveyUIDGeneratorTriedTooMuchException
  */
 private static function generateUID()
 {
     return self::generateKey(self::UID_LENGTH, function ($uid) {
         $check = DBI::prepare('SELECT id FROM ' . self::getDBTable() . ' WHERE id = :id');
         $check->execute(array(':id' => $uid));
         return !$check->fetch();
     });
 }
Exemplo n.º 8
0
 /**
  * Table columns format update.
  * 
  * @param string $table table name
  * @param string $column column name
  * @param array $definition column definition
  * @param array $problems problematic options
  */
 public static function updateTableColumnFormat($table, $column, $definition, $problems)
 {
     if (in_array('type', $problems)) {
         // Cahnge data type
         $type = '';
         switch ($definition['type']) {
             case 'int':
             case 'uint':
                 $size = array_key_exists('size', $definition) ? $definition['size'] : 'medium';
                 if (!$size) {
                     $size = 'medium';
                 }
                 $s2s = array('small' => 'smallint', 'medium' => 'integer', 'big' => 'bigint');
                 $type .= $s2s[$size];
                 break;
             case 'string':
                 $type .= 'character varying(' . $definition['size'] . ')';
                 break;
             case 'bool':
                 $type .= 'boolean';
                 break;
             case 'text':
                 $type .= 'text';
                 break;
             case 'date':
                 $type .= 'date';
                 break;
             case 'datetime':
                 $type .= 'timestamp';
                 break;
             case 'time':
                 $type .= 'time';
                 break;
         }
         if ($type) {
             DBI::exec('ALTER TABLE ' . $table . ' ALTER COLUMN ' . $column . ' TYPE ' . $type);
         }
     }
     // Options defaults
     foreach (array('null', 'primary', 'unique', 'autoinc') as $k) {
         if (!array_key_exists($k, $definition)) {
             $definition[$k] = false;
         }
     }
     // Change nullable
     if (in_array('null', $problems)) {
         if ($definition['null']) {
             DBI::exec('ALTER TABLE ' . $table . ' ALTER COLUMN ' . $column . ' DROP NOT NULL');
         } else {
             DBI::exec('ALTER TABLE ' . $table . ' ALTER COLUMN ' . $column . ' SET NOT NULL');
         }
     }
     // Change default
     if (in_array('default', $problems)) {
         if (array_key_exists('default', $definition)) {
             if (is_null($definition['default'])) {
                 DBI::exec('ALTER TABLE ' . $table . ' ALTER COLUMN ' . $column . ' SET DEFAULT NULL');
             } else {
                 if (is_bool($definition['default'])) {
                     DBI::exec('ALTER TABLE ' . $table . ' ALTER COLUMN ' . $column . ' SET DEFAULT ' . ($definition['default'] ? '1' : '0'));
                 } else {
                     DBI::prepare('ALTER TABLE ' . $table . ' ALTER COLUMN ' . $column . ' SET DEFAULT :default') - execute(array(':default' => $definition['default']));
                 }
             }
         } else {
             DBI::exec('ALTER TABLE ' . $table . ' ALTER COLUMN ' . $column . ' DROP DEFAULT');
         }
     }
     // Change primary
     if (in_array('primary', $problems)) {
         if ($definition['primary']) {
             DBI::exec('ALTER TABLE ' . $table . ' ADD CONSTRAINT primary_' . $column . ' PRIMARY KEY (' . $column . ')');
         } else {
             DBI::exec('ALTER TABLE ' . $table . ' DROP CONSTRAINT primary_' . $column);
         }
     }
     // Change unique
     if (in_array('unique', $problems)) {
         if ($definition['unique']) {
             DBI::exec('ALTER TABLE ' . $table . ' ADD CONSTRAINT unique_' . $column . ' UNIQUE (' . $column . ')');
         } else {
             DBI::exec('ALTER TABLE ' . $table . ' DROP CONSTRAINT unique_' . $column);
         }
     }
     // Change autoinc
     if (in_array('autoinc', $problems)) {
         if ($definition['autoinc']) {
             self::createSequence($table, $column);
         } else {
             DBI::exec('ALTER TABLE ' . $table . ' ALTER COLUMN ' . $column . ' DROP DEFAULT');
             // Should we drop the sequence as well ?
         }
     }
 }
Exemplo n.º 9
0
 /**
  * Update object record
  * 
  * @param array $data database compliant data such as returned by toDBData
  * @param string $key_name field_name to use as primary key (string or array of strings)
  * @param string $where where clause extension (optionnal)
  */
 public static function updateRecord($data, $key_name, $where = null)
 {
     $event = new Event('dbobject_update', static::getClassName(), $data, $key_name, $where);
     $table = static::getDBTable();
     $event->trigger(function ($class, $data, $key_name, $where) use($table) {
         $placeholders = array();
         $values = array();
         // Filter
         $key_names = is_array($key_name) ? $key_name : array($key_name);
         $key_names = array_filter($key_names);
         // Build update pairs
         foreach ($data as $field_name => $value) {
             if (!in_array($field_name, $key_names)) {
                 $placeholders[] = $field_name . ' = :' . $field_name;
             }
             $values[':' . $field_name] = $value;
         }
         // Build filter
         $where_parts = array();
         foreach ($key_names as $key_name) {
             $where_parts[] = $key_name . ' = :' . $key_name;
         }
         if ($where) {
             $where_parts[] = $where;
         }
         // Run the query
         $s = DBI::prepare('UPDATE ' . $table . ' SET ' . implode(', ', $placeholders) . (count($where_parts) ? ' WHERE (' . implode(') AND (', $where_parts) . ')' : ''));
         $s->execute($values);
     });
 }
Exemplo n.º 10
0
 /**
  * Table columns format checking.
  * 
  * @param string $table table name
  * @param string $column column name
  * @param string $definition column definition
  * 
  * @return array of non respected options or false if no problems
  */
 public static function checkTableColumnFormat($table, $column, $definition, $logger = null)
 {
     if (!$logger || !is_callable($logger)) {
         $logger = function () {
         };
     }
     // Get current definition
     $s = DBI::prepare('SHOW COLUMNS FROM ' . $table . ' LIKE :column');
     $s->execute(array(':column' => $column));
     $column_dfn = $s->fetch();
     $non_respected = array();
     $typematcher = '';
     // Build type matcher
     switch ($definition['type']) {
         case 'int':
         case 'uint':
             $size = array_key_exists('size', $definition) ? $definition['size'] : 'medium';
             if (!$size) {
                 $size = 'medium';
             }
             $typematcher = $size . 'int(?:\\([0-9]+\\))';
             if ($definition['type'] == 'uint') {
                 $typematcher .= ' unsigned';
             }
             break;
         case 'string':
             $typematcher = 'varchar\\(' . $definition['size'] . '\\)';
             break;
         case 'bool':
             $typematcher = 'tinyint\\([0-9]+\\) unsigned';
             break;
         case 'enum':
             $typematcher = 'enum(' . implode(',', $definition['values']) . ')';
             break;
         case 'text':
             $typematcher = 'text';
             break;
         case 'date':
             $typematcher = 'date';
             break;
         case 'datetime':
             $typematcher = 'datetime';
             break;
         case 'time':
             $typematcher = 'time';
             break;
     }
     // Check type
     if (!preg_match('`' . $typematcher . '`i', $column_dfn['Type'])) {
         $logger($column . ' type does not match ' . $typematcher);
         $non_respected[] = 'type';
     }
     // Check default
     if (array_key_exists('default', $definition)) {
         if (is_null($definition['default'])) {
             if (!is_null($column_dfn['Default'])) {
                 $logger($column . ' default is not null');
                 $non_respected[] = 'default';
             }
         } else {
             if (is_bool($definition['default'])) {
                 if ((bool) $column_dfn['Default'] != $definition['default']) {
                     $logger($column . ' default is not ' . ($definition['default'] ? '1' : '0'));
                     $non_respected[] = 'default';
                 }
             } else {
                 if ($column_dfn['Default'] != $definition['default']) {
                     $logger($column . ' default is not "' . $definition['default'] . '"');
                     $non_respected[] = 'default';
                 }
             }
         }
     }
     // Options defaults
     foreach (array('null', 'primary', 'unique', 'autoinc') as $k) {
         if (!array_key_exists($k, $definition)) {
             $definition[$k] = false;
         }
     }
     // Check nullable
     $is_null = $column_dfn['Null'] == 'YES';
     if ($definition['null'] && !$is_null) {
         $logger($column . ' is not nullable');
         $non_respected[] = 'null';
     } else {
         if (!$definition['null'] && $is_null) {
             $logger($column . ' should not be nullable');
             $non_respected[] = 'null';
         }
     }
     // Check primary
     $is_primary = $column_dfn['Key'] == 'PRI';
     if ($definition['primary'] && !$is_primary) {
         $logger($column . ' is not primary');
         $non_respected[] = 'primary';
     } else {
         if (!$definition['primary'] && $is_primary) {
             $logger($column . ' should not be primary');
             $non_respected[] = 'primary';
         }
     }
     // Check unique
     $is_unique = $column_dfn['Key'] == 'UNI';
     if ($definition['unique'] && !$is_unique) {
         $logger($column . ' is not unique');
         $non_respected[] = 'unique';
     } else {
         if (!$definition['unique'] && $is_unique) {
             $logger($column . ' should not be unique');
             $non_respected[] = 'unique';
         }
     }
     // Check autoinc
     $is_autoinc = preg_match('`auto_increment`', $column_dfn['Extra']);
     if ($definition['autoinc'] && !$is_autoinc) {
         $logger($column . ' is not autoinc');
         $non_respected[] = 'autoinc';
     } else {
         if (!$definition['autoinc'] && $is_autoinc) {
             $logger($column . ' should not be autoinc');
             $non_respected[] = 'autoinc';
         }
     }
     // Return any errors
     return count($non_respected) ? $non_respected : false;
 }
 /**
  * Allows to get config by api key
  * 
  * @param String $key
  * @return boolean|\self
  */
 private static function getByApiKey($key)
 {
     $query = 'SELECT * FROM ' . static::getDBTable() . " where api_key = :api_key";
     $statement = DBI::prepare($query);
     $statement->execute(array('api_key' => $key));
     $datas = $statement->fetch();
     if (is_array($datas)) {
         $self = new self(null, $datas);
         return $self;
     } else {
         return false;
     }
 }