/** * 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; }
/** * 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(); } }
/** * Get the security token, refreshing it in the process if needed * * @return string */ public static function getSecurityToken() { if (!is_null(self::$security_token)) { return self::$security_token['value']; } // Fetch existing token $token = array_key_exists('security_token', $_SESSION) ? $_SESSION['security_token'] : null; // Old token style, cancel it if (!is_array($token)) { $token = null; } if (!$token) { // First access $token = array('value' => Utilities::generateUID(), 'valid_until' => time() + 3600, 'old_value' => null); } else { if ($token['valid_until'] < time()) { // Must renew $token['old_value'] = $token['value']; $token['value'] = Utilities::generateUID(); $token['valid_until'] = time() + 3600; } else { // Still valid, scrape old value from any previous changes $token['old_value'] = null; } } if ($token['old_value']) { // Send new value as header if changed header('X-Application-Security-Token: ' . $token['value']); } // Store in session $_SESSION['security_token'] = $token; // Cache in class self::$security_token = $token; return $token['value']; }