/** * Adds a device to the system, after sending a test notification to it * @param {array} $device * @param {string} $device.userId * @param {string} $device.deviceId * @param {string} [$device.formFactor] * @param {string} [$device.platform] * @param {string} [$device.version] * @param {string} [$device.sessionId] * @param {boolean} [$device.sandbox] * @param {string} [$device.passphrase] * @param {boolean} [$skipNotification=false] if true, skips sending notification * @return {Users_Device} */ static function add($device, $skipNotification = false) { Q_Valid::requireFields(array('userId', 'deviceId'), $device, true); $userId = $device['userId']; $deviceId = $device['deviceId']; if (!$skipNotification) { $app = Q::app(); $sandbox = Q::ifset($device, 'sandbox', null); if (!isset($sandbox)) { $sandbox = Q_Config::get($app, "cordova", "ios", "sandbox", false); } $env = $sandbox ? ApnsPHP_Abstract::ENVIRONMENT_SANDBOX : ApnsPHP_Abstract::ENVIRONMENT_PRODUCTION; $s = $sandbox ? 'sandbox' : 'production'; $cert = APP_LOCAL_DIR . DS . 'Users' . DS . 'certs' . DS . $app . DS . $s . DS . 'bundle.pem'; $authority = USERS_PLUGIN_FILES_DIR . DS . 'Users' . DS . 'certs' . DS . 'EntrustRootCA.pem'; $logger = new Users_ApnsPHP_Logger(); $push = new ApnsPHP_Push($env, $cert); $push->setLogger($logger); $push->setRootCertificationAuthority($authority); if (isset($device['passphrase'])) { $push->setProviderCertificatePassphrase($device['passphrase']); } $push->connect(); $message = new ApnsPHP_Message($deviceId); $message->setCustomIdentifier('Users_Device-adding'); $message->setBadge(0); $message->setText(Q_Config::get($app, "cordova", "ios", "device", "text", "Notifications have been enabled")); $message->setCustomProperty('userId', $userId); $message->setExpiry(5); $push->add($message); $push->send(); $push->disconnect(); $errors = $push->getErrors(); if (!empty($errors)) { $result = reset($errors); throw new Users_Exception_DeviceNotification($result['ERRORS'][0]); } } $sessionId = Q_Session::id(); $user = Users::loggedInUser(); $info = array_merge(Q_Request::userAgentInfo(), array('sessionId' => $sessionId, 'userId' => $user ? $user->id : null, 'deviceId' => null)); $device2 = Q::take($device, $info); $d = new Users_Device($device2); $d->save(true); if ($sessionId) { $s = new Users_Session(); $s->id = $sessionId; if (!$s->retrieve()) { $s->deviceId = $deviceId; } } $_SESSION['Users']['deviceId'] = $deviceId; $device2['Q/method'] = 'Users/device'; Q_Utils::sendToNode($device2); return $d; }
/** * @method start * @static * @return {boolean} */ static function start() { if (self::id()) { // Session has already started return false; } /** * @event Q/session/start {before} * @return {false} * Return false to cancel session start */ if (false === Q::event('Q/session/start', array(), 'before')) { return false; } if (Q_Config::get('Q', 'session', 'custom', true)) { session_set_save_handler(array(__CLASS__, 'openHandler'), array(__CLASS__, 'closeHandler'), array(__CLASS__, 'readHandler'), array(__CLASS__, 'writeHandler'), array(__CLASS__, 'destroyHandler'), array(__CLASS__, 'gcHandler')); } if (!empty($_SESSION)) { $pre_SESSION = $_SESSION; } self::init(); $name = Q_Session::name(); $id = isset($_REQUEST[$name]) ? $_REQUEST[$name] : isset($_COOKIE[$name]) ? $_COOKIE[$name] : null; if (!self::isValidId($id)) { // The session id was probably not generated by us, generate a new one /** * @event Q/session/generate {before} * @param {string} id An invalid id, if any, that was passed by the client * @return {boolean} */ if (false === Q::event('Q/session/generate', compact('id'), 'before')) { return false; } $id = self::generateId(); } try { if ($id) { self::processDbInfo(); if (self::$session_db_connection) { $id_field = self::$session_db_id_field; $data_field = self::$session_db_data_field; $updated_field = self::$session_db_updated_field; $duration_field = self::$session_db_duration_field; $class = self::$session_db_row_class; $row = new $class(); $row->{$id_field} = $id; if ($row->retrieve()) { self::$session_db_row = $row; } else { // Start a new session with our own id $id = $row->{$id_field} = self::generateId(); $row->{$data_field} = ""; $row->{$updated_field} = date('Y-m-d H:i:s'); $row->{$duration_field} = Q_Config::get('Q', 'session', 'durations', Q_Request::formFactor(), Q_Config::expect('Q', 'session', 'durations', 'session')); if (false !== Q::event('Q/session/save', array('row' => $row, 'id_field' => $id_field, 'data_field' => $data_field, 'updated_field' => $updated_field, 'duration_field' => $duration_field, 'inserting' => true), 'before')) { $row->save(); self::id($row->{$id_field}); // this sets the session cookie as well self::$session_db_row = $row; } } } self::id($id); } if (!empty($_SERVER['HTTP_HOST'])) { $durationName = self::durationName(); $duration = Q_Config::get('Q', 'session', 'durations', $durationName, 0); Q_Response::setCookie(self::name(), $id, $duration ? time() + $duration : 0); } else { if (empty($_SESSION)) { $_SESSION = array(); } } ini_set('session.use_cookies', 0); // we are gonna handle the cookies, thanks session_cache_limiter(''); // don't send the cache limiter headers either session_start(); } catch (Exception $e) { $app = Q_Config::get('Q', 'app', null); $prefix = $app ? "{$app}/" : ''; if (empty($_SERVER['HTTP_HOST'])) { echo "Warning: Ignoring Q_Session::start() called before running {$prefix}scripts/Q/install.php --all" . PHP_EOL; $message = $e->getMessage(); $file = $e->getFile(); $line = $e->getLine(); if (is_callable(array($e, 'getTraceAsStringEx'))) { $trace_string = $e->getTraceAsStringEx(); } else { $trace_string = $e->getTraceAsString(); } echo "{$message}\n(in {$file} line {$line})\n{$trace_string}" . PHP_EOL; } else { if (is_callable('apc_clear_cache')) { apc_clear_cache('user'); } Q::log($e); throw new Q_Exception("Please run {$prefix}scripts/Q/install.php --all"); } } // merge in all the stuff that was added to $_SESSION // before we started it. if (isset($pre_SESSION)) { foreach ($pre_SESSION as $k => $v) { $_SESSION[$k] = $v; } } if (isset($_SESSION['Q']['notices'])) { foreach ($_SESSION['Q']['notices'] as $k => $v) { Q_Response::setNotice($k, $v); } } if (!empty($_SESSION['Q']['terminated'])) { throw new Q_Exception_SessionTerminated(array('id' => Q_Session::id())); } if (Q_Config::get('Q', 'session', 'userAgentInfo', null)) { $arr = isset($_SESSION['Q']) ? $_SESSION['Q'] : array(); $_SESSION['Q'] = array_merge($arr, Q_Request::userAgentInfo()); } /** * @event Q/session/start {after} */ Q::event('Q/session/start', array(), 'after'); return true; }