function Streams_after_Q_objects() { $user = Users::loggedInUser(); if (!$user) { return; } $invite = Streams::$followedInvite; if (!$invite) { return; } $displayName = $user->displayName(); if ($displayName) { return; } $stream = new Streams_Stream(); $stream->publisherId = $invite->publisherId; $stream->name = $invite->streamName; if (!$stream->retrieve()) { throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => 'with that name'), 'streamName'); } // Prepare the complete invite dialog $invitingUser = Users_User::fetch($invite->invitingUserId); list($relations, $related) = Streams::related($user->id, $stream->publisherId, $stream->name, false); $params = array('displayName' => null, 'action' => 'Streams/basic', 'icon' => $user->iconUrl(), 'token' => $invite->token, 'user' => array('icon' => $invitingUser->iconUrl(), 'displayName' => $invitingUser->displayName(array('fullAccess' => true))), 'stream' => $stream->exportArray(), 'relations' => Db::exportArray($relations), 'related' => Db::exportArray($related)); $config = Streams_Stream::getConfigField($stream->type, 'invite', array()); $defaults = Q::ifset($config, 'dialog', array()); $tree = new Q_Tree($defaults); if ($tree->merge($params)) { $dialogData = $tree->getAll(); if ($dialogData) { Q_Response::setScriptData('Q.plugins.Streams.invite.dialog', $dialogData); Q_Response::addTemplate('Streams/invite/complete'); } } }
function Streams_interests_response() { // serve a javascript file and tell client to cache it $app = Q_Config::expect('Q', 'app'); $communityId = Q::ifset($_REQUEST, 'communityId', $app); $tree = new Q_Tree(); $tree->load("files/Streams/interests/{$communityId}.json"); $categories = $tree->getAll(); foreach ($categories as $category => &$v1) { foreach ($v1 as $k2 => &$v2) { if (!Q::isAssociative($v2)) { ksort($v1); break; } ksort($v2); } } header('Content-Type: text/javascript'); header("Pragma: ", true); // 1 day header("Cache-Control: public, max-age=86400"); // 1 day $expires = date("D, d M Y H:i:s T", time() + 86400); header("Expires: {$expires}"); // 1 day $json = Q::json_encode($categories, true); echo "Q.setObject(['Q', 'Streams', 'Interests', 'all', '{$communityId}'], {$json});"; return false; }
function Streams_after_Users_User_saveExecute($params) { // If the username or icon was somehow modified, // update all the avatars for this publisher $modifiedFields = $params['modifiedFields']; $user = $params['row']; $updates = array(); if (isset($modifiedFields['username'])) { $updates['username'] = $modifiedFields['username']; } if (isset($modifiedFields['icon'])) { $updates['icon'] = $modifiedFields['icon']; } if ($user->id === Users::communityId()) { $firstName = Users::communityName(); $lastName = Users::communitySuffix(); $firstName = $firstName ? $firstName : ""; $lastName = $lastName ? $lastName : ""; } else { $firstName = Q::ifset(Streams::$cache, 'register', 'first', ''); $lastName = Q::ifset(Streams::$cache, 'register', 'last', ''); } if ($params['inserted']) { // create some standard streams for them $onInsert = Q_Config::get('Streams', 'onInsert', 'Users_User', array()); if (!$onInsert) { return; } $p = new Q_Tree(); $p->load(STREAMS_PLUGIN_CONFIG_DIR . DS . 'streams.json'); $p->load(APP_CONFIG_DIR . DS . 'streams.json'); $values = array('Streams/user/firstName' => $firstName, 'Streams/user/lastName' => $lastName); // Check for user data from facebook if (!empty(Users::$cache['facebookUserData'])) { $userData = Users::$cache['facebookUserData']; foreach ($userData as $name_fb => $value) { foreach ($p->getAll() as $name => $info) { if (isset($info['name_fb']) and $info['name_fb'] === $name_fb) { $onInsert[] = $name; $values[$name] = $value; } } } } foreach ($onInsert as $name) { $stream = Streams::fetchOne($user->id, $user->id, $name); if (!$stream) { // it shouldn't really be in the db yet $stream = new Streams_Stream(); $stream->publisherId = $user->id; $stream->name = $name; } $stream->type = $p->expect($name, "type"); $stream->title = $p->expect($name, "title"); $stream->content = $p->get($name, "content", ''); // usually empty $stream->readLevel = $p->get($name, 'readLevel', Streams_Stream::$DEFAULTS['readLevel']); $stream->writeLevel = $p->get($name, 'writeLevel', Streams_Stream::$DEFAULTS['writeLevel']); $stream->adminLevel = $p->get($name, 'adminLevel', Streams_Stream::$DEFAULTS['adminLevel']); if ($name === "Streams/user/icon") { $sizes = Q_Config::expect('Users', 'icon', 'sizes'); sort($sizes); $stream->setAttribute('sizes', $sizes); $stream->icon = $user->iconUrl(); } if (isset($values[$name])) { $stream->content = $values[$name]; } $stream->save(); // this also inserts avatars $o = array('userId' => $user->id, 'skipAccess' => true); $so = $p->get($name, "subscribe", array()); if ($so === false) { $stream->join($o); } else { $stream->subscribe(array_merge($o, $so)); } } // Save a greeting stream, to be edited $communityId = Users::communityId(); Streams::create($user->id, $user->id, "Streams/greeting", array('name' => "Streams/greeting/{$communityId}")); // Create some standard labels $label = new Users_Label(); $label->userId = $user->id; $label->label = 'Streams/invited'; $label->icon = 'labels/Streams/invited'; $label->title = 'People I invited'; $label->save(true); $label2 = new Users_Label(); $label2->userId = $user->id; $label2->label = 'Streams/invitedMe'; $label2->icon = 'labels/Streams/invitedMe'; $label2->title = 'Who invited me'; $label2->save(true); // By default, users they invite should see their full name $access = new Streams_Access(); $access->publisherId = $user->id; $access->streamName = 'Streams/user/firstName'; $access->ofUserId = ''; $access->ofContactLabel = 'Streams/invited'; $access->grantedByUserId = $user->id; $access->readLevel = Streams::$READ_LEVEL['content']; $access->writeLevel = -1; $access->adminLevel = -1; $access->save(); $access = new Streams_Access(); $access->publisherId = $user->id; $access->streamName = 'Streams/user/lastName'; $access->ofUserId = ''; $access->ofContactLabel = 'Streams/invited'; $access->grantedByUserId = $user->id; $access->readLevel = Streams::$READ_LEVEL['content']; $access->writeLevel = -1; $access->adminLevel = -1; $access->save(); // NOTE: the above saving of access caused Streams::updateAvatar to run, // insert a Streams_Avatar row for the new user, and properly configure it. } else { if ($modifiedFields) { if ($updates) { Streams_Avatar::update()->set($updates)->where(array('publisherId' => $user->id))->execute(); } foreach ($modifiedFields as $field => $value) { $name = Q_Config::get('Streams', 'onUpdate', 'Users_User', $field, null); if (!$name) { continue; } $stream = isset(Streams::$beingSaved[$field]) ? Streams::$beingSaved[$field] : Streams::fetchOne($user->id, $user->id, $name); if (!$stream) { // it should probably already be in the db continue; } $stream->content = $value; if ($name === "Streams/user/icon") { $sizes = Q_Config::expect('Users', 'icon', 'sizes'); sort($sizes); $attributes = $stream->attributes; $stream->setAttribute('sizes', $sizes); $stream->icon = $changes['icon'] = $user->iconUrl(); } Streams::$beingSavedQuery = $stream->changed($user->id); } } } }
/** * Modify a config file by clearing some data * Config file is searched in APP_DIR/files forder. If config server url is defined * the filename is searched on config server * @method clearOnServer * @static * @param {string} $filename The name of the config file. If config server is defined, file is changed there * @param {string|array} [$args=null] OA key or an array of keys for traversing the tree. * If keys are not supplied the file is cleared * If all-but-last keys point to plain array, last key is interpreted as a member * of that array and only this array member is removed * If all-but-last keys point to associative array (A) and last key is plain array (B) * all keys from array A which are in array B are unset * @param {boolean} [$noSave=false] Weather result shall be returned or saved. Shall be of type boolean * @return {boolean} Wheather data was successfuly cleared. If some key does not exist still true * @throws {Q_Exception} */ static function clearOnServer($filename, $args = null, $noSave = false) { if (!isset($args) || $args === 'null') { $args = array(); } if (is_string($args)) { $args = array($args); } if (is_string($noSave)) { $noSave = json_decode($noSave); } $noSave = !!$noSave; if ($cs = self::serverInfo()) { // request config server if (!empty($cs['url'])) { if (!empty($cs['internal'])) { // query "internal" Qbix server return Q_Utils::queryInternal('Q/Config', array('Q/method' => 'clear', 'filename' => $filename, 'args' => $args, 'noSave' => $noSave), $cs['url']); } else { // query "external" Qbix server return Q_Utils::queryExternal('Q/Config', array('Q/method' => 'clear', 'filename' => $filename, 'args' => $args, 'noSave' => $noSave), $cs['url']); } } } // modify local file if (defined('APP_DIR')) { $filename = Q::realPath(APP_DIR . DS . 'files' . DS . $filename); } else { throw new Q_Exception("'APP_DIR' is not defined"); } if (!$filename) { return true; } $tree = new Q_Tree(); if (count($args)) { $tree->load($filename); // if not loaded we consider three empty if (count($args) > 1) { $last = call_user_func_array(array($tree, "get"), $args); } else { $last = $tree->getAll(); } if (is_array($last)) { if (array_keys($last) === range(0, count($last) - 1)) { // it's plain array and we remove it's member $search = array_pop($args); if (!is_array($search)) { $search = array($search); } foreach ($search as $value) { $keys = array_keys($last, $value); for ($deleted = 0, $i = 0; $i < count($keys); $i++) { array_splice($last, $keys[$i] - $deleted++, 1); } } call_user_func_array(array($tree, "clear"), $args); if (count($last)) { array_push($args, $last); call_user_func_array(array($tree, "set"), $args); } } else { // $last is associative array $search = array_pop($args); if (!is_array($search)) { $search = array($search); } foreach ($search as $value) { call_user_func_array(array($tree, "clear"), array_merge($args, array($value))); } } } } else { $tree = new Q_Tree(); } return $noSave ? $tree->getAll() : $tree->save($filename); }
/** * Get a structured, sorted array with all the interests in a community * @method interests * @static * @param {string} [$communityId=Users::communityId()] the id of the community * @return {array} an array of $category => ($subcategory =>) $interest */ static function interests($communityId = null) { if (!isset($communityId)) { $communityId = Users::communityId(); } $tree = new Q_Tree(); $tree->load("files/Streams/interests/{$communityId}.json"); $interests = $tree->getAll(); foreach ($interests as $category => &$v1) { foreach ($v1 as $k2 => &$v2) { if (!Q::isAssociative($v2)) { ksort($v1); break; } ksort($v2); } } return $interests; }
/** * @method writeHandler * @static * @param {string} $id * @param {string} $sess_data * @return {boolean} */ static function writeHandler($id, $sess_data) { try { // if the request is AJAX request that came without session cookie, then do not write session, ignore it if (Q_Request::isAjax() && !isset($_COOKIE[self::name()])) { return false; } // don't save sessions when running from command-line (cli) if (php_sapi_name() == 'cli') { return false; } $our_SESSION = $_SESSION; $old_data = self::$sess_data; $changed = $sess_data !== $old_data; $result = false; /** * @event Q/session/write {before} * @param {string} id * @param {string} sess_data * @param {string} old_data * @param {boolean} changed * @return {boolean} */ if (false === Q::event('Q/session/write', compact('id', 'sess_data', 'old_data', 'changed'), 'before')) { return false; } if (empty(self::$session_save_path)) { self::$session_save_path = self::savePath(); } if (!empty(self::$session_db_connection)) { // Create a new row to be saved in the session table $db_row_class = self::$session_db_row_class; // Make sure it has a primary key! if (count(self::$session_db_row->getPrimaryKey()) != 1) { throw new Q_Exception("The primary key of " . self::$session_db_row_class . " has to consist of exactly 1 field!"); } $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; $row = self::$session_db_row; $row->{$id_field} = $id; } else { $duration_name = self::durationName(); $id1 = substr($id, 0, 4); $id2 = substr($id, 4); $ssp = self::$session_save_path; $sess_file = $ssp . DS . "{$duration_name}/{$id1}/{$id2}"; $dir = $ssp . DS . "{$duration_name}/{$id1}/"; } if ($changed) { // Apparently, we want to save some changes. // The convention to avoid locking is that everything // stored in sessions must be mergeable using the // Q_Tree merge algorithm. // So we will retrieve the latest session data again, // merge our changes over it, and save. $params = array('id_field' => $id_field, 'data_field' => $data_field, 'updated_field' => $updated_field, 'duration_field' => $duration_field, 'changed' => $changed, 'sess_data' => $sess_data, 'old_data' => $old_data); if (!empty(self::$session_db_connection)) { $row->retrieve(); $existing_data = Q::ifset($row, $data_field, ""); $params['row'] = $row; } else { if (!is_dir($dir)) { mkdir($dir, fileperms($ssp), true); } if (!is_writable($dir)) { // alert the developer to this problem Q::log("{$sess_file} is not writable", 'fatal'); die("{$sess_file} is not writable"); } $file = fopen($sess_file, "w"); if (!$file) { return false; } $params['row'] = $row; $maxlength = Q_Config::get('Q', 'session', 'maxlength', 4095); $existing_data = fread($file, $maxlength); } $_SESSION = session_decode($existing_data); if (!$_SESSION) { $_SESSION = array(); } $t = new Q_Tree($_SESSION); $t->merge($our_SESSION); $_SESSION = $t->getAll(); $params['existing_data'] = $existing_data; $params['merged_data'] = $merged_data = session_encode(); /** * @event Q/session/save {before} * @param {string} sess_data * @param {string} old_data * @param {string} existing_data * @param {string} merged_data * @param {boolean} changed * @param {Db_Row} row * @return {boolean} */ if (false === Q::event('Q/session/save', $params, 'before')) { return false; } if (!empty(self::$session_db_connection)) { $row->{$data_field} = $merged_data; $row->{$duration_field} = Q_Config::get('Q', 'session', 'durations', Q_Request::formFactor(), Q_Config::expect('Q', 'session', 'durations', 'session')); $row->save(); $result = true; } else { $result = fwrite($file, $merged_data); fclose($file); } } /** * @event Q/session/write {after} * @param {string} id * @param {boolean} changed * @param {string} sess_data * @param {string} old_data * @param {string} existing_data * @param {string} merged_data * @param {string} data_field * @param {string} updated_field * @param {string} duration_field * @param {string} sess_file * @param {integer} row * @return {mixed} */ $result = Q::event('Q/session/write', compact('id', 'data_field', 'updated_field', 'duration_field', 'sess_file', 'row', 'changed', 'sess_data', 'old_data', 'existing_data', 'merged_data'), 'after'); return $result; } catch (Exception $e) { Q::log("Exception when writing session {$id}: " . $e->getMessage()); throw $e; } }