Exemplo n.º 1
0
/**
 * Used to post messages to EXISTING stream
 * $_REQUEST shall contain the content of the message. Also may include 'streamNames' 
 * field which is an array of additional names of the streams to post message to.
 *
 * @param string $params 
 *   publisher id and stream name of existing stream shall be supplied
 * @return {void}
 */
function Streams_message_post()
{
    $user = Users::loggedInUser(true);
    $publisherId = Streams::requestedPublisherId(true);
    $streamName = Streams::requestedName(true);
    // check if type is allowed
    $streams = Streams::fetch($user->id, $publisherId, $streamName);
    if (empty($streams)) {
        throw new Streams_Exception_NoSuchStream();
    }
    $stream = reset($streams);
    if (empty($_REQUEST['type'])) {
        throw new Q_Exception_RequiredField(array('field' => 'type'), 'type');
    }
    $type = $_REQUEST['type'];
    if (!Q_Config::get("Streams", "types", $stream->type, "messages", $type, 'post', false)) {
        throw new Q_Exception("This app doesn't support directly posting messages of type '{$type}' for streams of type '{$stream->type}'");
    }
    $result = Streams_Message::post($user->id, $publisherId, $streamName, $_REQUEST);
    if (is_array($result)) {
        Streams::$cache['messages'] = $result;
    } else {
        Streams::$cache['message'] = $result;
    }
}
Exemplo n.º 2
0
function Streams_stream_response_data()
{
    // happens only during non-GET requests
    if (isset(Streams::$cache['removed_count'])) {
        return array('removed_count' => Streams::$cache['removed_count']);
    }
    if (isset(Streams::$cache['result'])) {
        return Streams::$cache['result'];
    }
    if (isset(Streams::$cache['stream'])) {
        $user = Users::loggedInUser();
        $userId = $user ? $user->id : "";
        return Streams::$cache['stream']->exportArray(array('asUserId' => $userId));
    }
    $publisherId = Streams::requestedPublisherId(true);
    $name = Streams::requestedName(true);
    $fields = Streams::requestedFields();
    $user = Users::loggedInUser();
    $userId = $user ? $user->id : 0;
    $streams = array();
    foreach (Streams::fetch($userId, $publisherId, $name, $fields) as $key => $stream) {
        $streams[$key] = $stream->exportArray(array('asUserId' => $userId));
        if ($userId && !empty($_REQUEST['join'])) {
            $stream->join();
            // NOTE: one of the rare times we may change state in a response handler
        }
    }
    return Streams::$cache['result'] = array('stream' => empty($streams) ? null : reset($streams));
}
Exemplo n.º 3
0
/**
 * Renders chat tool.
 * @class Streams chat
 * @constructor
 * @param {array} $options Options for the tool
 * @param {string} $options.publisherId Publisher id of the stream to get messsages from.
 * @param {string} $options.streamName Required. Name of the stream to get messsages from.
 * @param {string} [$options.loadMore] May have one these values: 'scroll', 'click' or 'pull' which indicates what kind of algorithm will be used for loading new messages. 'scroll' means that new messages will be loaded when scrollbar of the chat cointainer reaches the top (for desktop) or whole document scrollbar reaches the top (for android). 'click' will show label with 'Click to see earlier messages' and when user clicks it, new messages will be loaded. Finally, 'pull' implements 'pull-to-refresh' behavior used in many modern applications today when new messages loaded by rubber-scrolling the container by more amount than it actually begins. Defaults to 'scroll' for desktop and Android devices and 'pull' for iOS devices.
*/
function Streams_chat_tool($options)
{
    $user = Users::loggedInUser();
    $userId = $user ? $user->id : '';
    /*
    $defaults = array(
    	'loadMore'         => (Q_Request::isTouchscreen() && Q_Request::platform() != 'android') ? 'click' : 'scroll',
    	'messagesToLoad'   => 5,
    	'messageMaxHeight' => 200
    );
    $options = array_merge($defaults, $options);
    */
    extract($options);
    if (!isset($publisherId)) {
        $publisherId = Streams::requestedPublisherId(true);
    }
    if (!isset($streamName)) {
        $streamName = Streams::requestedName();
    }
    $stream = Streams::fetchOne($userId, $publisherId, $streamName);
    if (!$stream) {
        throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => compact('publisherId', 'streamName')));
    }
    $options['userId'] = $userId;
    if (!isset($options['notLoggedIn'])) {
        $options['notLoggedIn'] = 'You are not logged in';
    }
    if (!isset($options['notAuthorized'])) {
        $options['notAuthorized'] = 'You are not authorized';
    }
    Q_Response::setToolOptions($options);
}
Exemplo n.º 4
0
/**
 * Provide player content to view the members of category listing
 * Uses Streams/$type/category.php view (Streams/$streamType/category/get.php can be used for viewing the category
 * stream itself if type of category is $streamType/category)
 * and Streams::related to retrieve streams data
 *
 **/
function Streams_category_response_player()
{
    $user = Users::loggedInUser();
    $userId = $user ? $user->id : 0;
    // These are PK of the category!
    $publisherId = Streams::requestedPublisherId(true);
    $name = Streams::requestedName(true);
    // need to know publisher and type of the streams to list
    $streamType = Streams::requestedType();
    if ($streamType) {
        $prefix = "{$streamType}/";
    }
    $stream_publisherId = Q::expect('Streams', $streamType, 'publisher');
    if (substr($name, -1) === '/') {
        throw new Q_Exception("Player cannot show listing for multiple categories", compact('publisherId', 'name'));
    }
    /*
     * Get shall return only streams which user is authorized to see.
     */
    $categories = Streams::fetch($userId, $publisherId, $name);
    if (empty($categories)) {
        throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => compact('publisherId', 'name')));
    }
    $category = reset($categories);
    // Are you authorized to see category content?
    if (!$category->testReadLevel('content')) {
        throw new Users_Exception_NotAuthorized();
    }
    // get all the streams which are members of this category
    // as Streams::get verifies access rights, it's safe to show all streams' content
    list($relations, $streams) = Streams::related($userId, $publisherId, $name, true, array('prefix' => $prefix, 'skipAccess' => true));
    Q::view("Stream/{$type}/category.php", compact('relations', 'streams', 'userId'));
}
Exemplo n.º 5
0
function Streams_participant_response_participant()
{
    if (isset(Streams::$cache['participant'])) {
        return Streams::$cache['participant'];
    }
    $publisherId = Streams::requestedPublisherId(true);
    $streamName = Streams::requestedName(true);
    if (empty($_REQUEST['userId'])) {
        throw new Q_Exception_RequiredField(array('field' => 'userId'));
    }
    $user = Users::loggedInUser();
    $userId = $user ? $user->id : "";
    $stream = Streams::fetch($userId, $publisherId, $streamName);
    if (empty($stream)) {
        throw new Q_Exception_MissingRow(array('table' => 'Stream', 'criteria' => "{publisherId: '{$publisherId}', name: '{$streamName}'}"));
    }
    $stream = reset($stream);
    if (!$stream->testReadLevel('participants')) {
        throw new Users_Exception_NotAuthorized();
    }
    $p = new Streams_Participant();
    $p->publisherId = $publisherId;
    $p->streamName = $streamName;
    $p->userId = $_REQUEST['userId'];
    if ($p->retrieve()) {
        return $p->exportArray();
    }
    return null;
}
Exemplo n.º 6
0
function Streams_message_response_messages()
{
    if (isset(Streams::$cache['message'])) {
        $message = Streams::$cache['message'];
        return Db::exportArray(array($message->ordinal => $message));
    }
    if (isset(Streams::$cache['messages'])) {
        return Db::exportArray(Streams::$cache['messages']);
    }
    $publisherId = Streams::requestedPublisherId(true);
    $streamName = Streams::requestedName(true);
    $type = Streams::requestedMessageType();
    $stream = Q::ifset(Streams::$cache, 'stream', Streams::fetchOne(null, $publisherId, $streamName, true));
    $maxLimit = Streams_Stream::getConfigField($type, 'getMessagesLimit', 100);
    $limit = min($maxLimit, Q::ifset($_REQUEST, 'limit', $maxLimit));
    if (isset($_REQUEST['ordinal'])) {
        $min = $_REQUEST['ordinal'];
        $limit = 1;
    }
    if (isset($_REQUEST['min'])) {
        $min = $_REQUEST['min'];
    }
    $max = isset($_REQUEST['max']) ? $_REQUEST['max'] : -1;
    if (isset($_REQUEST['ascending'])) {
        $ascending = $_REQUEST['ascending'];
    }
    if (!$stream->testReadLevel('messages')) {
        throw new Users_Exception_NotAuthorized();
    }
    $messages = $stream->getMessages(compact('type', 'min', 'max', 'limit', 'ascending'));
    return Db::exportArray($messages);
}
Exemplo n.º 7
0
/**
 * Used to get list of streams
 *
 * @param string $params 
 *   publisher id and stream name of existing stream shall be supplied
 * @return {void}
 *   array of streams indexed by names
 */
function Streams_publisher_get($params)
{
    // only logged in user can get stream
    $user = Users::loggedInUser(true);
    $publisherId = Streams::requestedPublisherId(true);
    $name = Streams::requestedName(true);
    $options = array_merge($_REQUEST, $params);
    return Streams::get($user->id, $publisherId, $name, $options);
}
Exemplo n.º 8
0
function Streams_access_response_data()
{
    $user = Users::loggedInUser(true);
    $publisherId = Streams::requestedPublisherId(true);
    $streamName = Streams::requestedName(true);
    $stream = Streams::fetchOne($user->id, $publisherId, $streamName);
    if (!$stream->testAdminLevel('own')) {
        throw new Users_Exception_NotAuthorized();
    }
    return array('access' => Q::ifset(Streams::$cache, 'access', null));
}
Exemplo n.º 9
0
function Streams_stream_response_streams()
{
    // happens only during non-GET requests
    $publisherId = Streams::requestedPublisherId(true);
    $name = Streams::requestedName(true);
    $fields = Streams::requestedFields();
    $limit = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : null;
    $user = Users::loggedInUser();
    $userId = $user ? $user->id : "";
    $streams = Streams::fetch($userId, $publisherId, $name, $fields ? $fields : '*', $limit ? compact('limit') : array());
    return Streams::$cache['streams'] = Db::exportArray($streams);
}
Exemplo n.º 10
0
function Streams_message_tool($options)
{
    extract($options);
    $user = Users::loggedInUser();
    if (!$user) {
        throw new Users_Exception_NotLoggedIn();
    }
    if (empty($publisherId)) {
        $publisherId = Streams::requestedPublisherId();
    }
    if (empty($publisherId)) {
        $publisherId = $_REQUEST['publisherId'] = $user->id;
    }
    if (empty($name)) {
        $name = Streams::requestedName(true);
    }
    $stream = Streams::fetch($user->id, $publisherId, $name);
    $stream = !empty($stream) ? reset($stream) : null;
    if (!$stream) {
        throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => 'with that name'), 'streamName');
    }
    if (!$stream->testReadLevel('messages') || !$stream->testWriteLevel('post')) {
        throw new Users_Exception_NotAuthorized();
    }
    $hidden = array('publisherId' => $publisherId, 'streamName' => $name);
    $fields = array('stream' => array('label' => 'Stream', 'type' => 'static', 'value' => $stream->title));
    $type = Streams::requestedType();
    // check if stream has messages
    $types = Q_Config::get('Streams', 'messages', $stream->type, array());
    if (count($types) === 0) {
        throw new Q_Exception("Stream of type '{$stream->type}' does not support messages");
    }
    if (!empty($type) && !in_array($type, $types)) {
        throw new Q_Exception("Requested message type '{$type}' is not alowed for streams of type '{$stream->type}'");
    }
    if (!empty($type)) {
        $hidden['type'] = $type;
        $fields['type'] = array('label' => 'Message type', 'type' => 'static', 'value' => $type);
    } else {
        $fields['type'] = array('label' => 'Message type', 'type' => 'select', 'options' => array_merge(array('' => 'Select type'), array_combine($types, $types)), 'value' => '');
    }
    $fields['content'] = array('label' => 'Content', 'type' => 'textarea');
    $fields['submit'] = array('label' => '', 'type' => 'submit_buttons', 'options' => array('submit' => 'Post'));
    return Q_Html::tag('h3', array(), 'Post a message') . Q_Html::form(Q_Request::baseUrl() . '/action.php/Streams/message', 'post', array(), Q_Html::hidden($hidden) . Q::tool('Q/form', array('fields' => $fields, 'onSuccess' => 'function (data) {
					if (data.errors) alert(data.errors);
					else {
						alert("Message posted");
						var message = Q.getObject(["slots", "form", "fields"], data);
						Q.handle(Q.info.baseUrl+"/plugins/Streams/message?publisherId="+message.publisherId+"&name="+message.streamName);
					}
				}')));
}
Exemplo n.º 11
0
function Streams_leave_post()
{
    $user = Users::loggedInUser(true);
    $publisherId = Streams::requestedPublisherId();
    $streamName = Streams::requestedName(true);
    $streams = Streams::fetch($user->id, $publisherId, $streamName);
    if (empty($streams)) {
        throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => "{publisherId: '{$publisherId}', name: '{$streamName}'}"));
    }
    $stream = reset($streams);
    $stream->leave(array(), $participant);
    Q_Response::setSlot('participant', $participant->exportArray());
}
Exemplo n.º 12
0
/**
 * This tool generates a category selector.
 *
 * @param {array} $options An associative array of parameters, containing:
 * @param {string} [$options.publisherId=Streams::requestedPublisherId()] The publisherId of the stream to present. If "stream" parameter is empty
 * @param {string} [$options.streamName=Streams::requestedName()] The streamName of the stream to present. If "stream" parameter is empty
 * @param {string} [options.relationType=null] Filter the relation type.
 */
function Streams_category_tool($options)
{
    extract($options);
    if (!$publisherId) {
        $options['publisherId'] = $publisherId = Streams::requestedPublisherId(true);
    }
    if (!$streamName) {
        $options['streamName'] = $streamName = Streams::requestedName(true);
    }
    Q_Response::setToolOptions($options);
    $stream = Streams::fetchOne(null, $publisherId, $streamName, true);
    $userId = Users::loggedInUser(true)->id;
    return Q::tool('Streams/related', $options);
}
Exemplo n.º 13
0
function Streams_stream_response_stream()
{
    // happens only during non-GET requests
    if (isset(Streams::$cache['stream'])) {
        return Streams::$cache['stream']->exportArray();
    }
    $publisherId = Streams::requestedPublisherId(true);
    $name = Streams::requestedName(true);
    $fields = Streams::requestedFields();
    $user = Users::loggedInUser();
    $userId = $user ? $user->id : "";
    Streams::$cache['stream'] = $stream = Streams::fetchOne($userId, $publisherId, $name, $fields, array('withParticipant' => true));
    return $stream ? $stream->exportArray() : null;
}
Exemplo n.º 14
0
/**
 * Used to close an existing stream. A cron job may delete this stream later.
 *
 * @module Streams
 * @class Streams_stream
 * @method delete
 * @static
 * @param {array} $_REQUEST
 * @param {string} $_REQUEST.publisherId The id of the stream publisher
 * @param {string} $_REQUEST.streamName The name of the stream the user will be invited to
 */
function Streams_stream_delete()
{
    $user = Users::loggedInUser(true);
    $publisherId = Streams::requestedPublisherId(true);
    $streamName = Streams::requestedName(true);
    Streams::$cache['result'] = Streams::close($user->id, $publisherId, $streamName);
    // NOTE: we did not delete the stream. That will have to be done in a cron job like this:
    // // Clean up access
    // $stream->delete();
    // Streams_Access::delete()->where(array(
    // 	'publisherId' => $stream->publisherId,
    // 	'streamName' => $stream->name
    // ))->execute();
    Q_Response::setSlot('result', Streams::$cache['result']);
}
Exemplo n.º 15
0
function Streams_invite_response_content()
{
    $user = Users::loggedInUser(true);
    $publisherId = Streams::requestedPublisherId();
    if (empty($publisherId)) {
        $publisherId = $user->id;
    }
    $streamName = Streams::requestedName(true);
    $stream = new Streams_Stream();
    $stream->publisherId = $publisherId;
    $stream->name = $streamName;
    if (!$stream->retrieve()) {
        throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => "{publisherId: '{$publisherId}', name: '{$streamName}'}"));
    }
    return Q::tool('Streams/invite', compact('stream'));
}
Exemplo n.º 16
0
 function beforeSave($modifiedFields)
 {
     $stream = null;
     $uri = Q_Uri::from($this->uri);
     if ($uri->module === 'Streams' and $uri->action === 'stream') {
         $publisherId = Streams::requestedPublisherId(false, $uri);
         $streamName = Streams::requestedName(false, 'original', $uri);
         $stream = Streams::fetchOne(null, $publisherId, $streamName);
     }
     Q::event('Websites/permalink', array('permalink' => $this, 'modifiedFields' => $modifiedFields, 'stream' => &$stream), 'before');
     if ($stream and $stream instanceof Streams_Stream) {
         $stream->setAttribute("Websites/url", $this->url);
         $stream->changed();
     }
     return parent::beforeSave($modifiedFields);
 }
Exemplo n.º 17
0
function Streams_stream_response_streams()
{
    // happens only during non-GET requests
    $publisherId = Streams::requestedPublisherId(true);
    $name = Streams::requestedName(true);
    $fields = Streams::requestedFields();
    $limit = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : null;
    $user = Users::loggedInUser();
    $userId = $user ? $user->id : "";
    $options = array('withParticipant' => true);
    if (isset($limit)) {
        $options['limit'] = $limit;
    }
    $streams = Streams::fetch($userId, $publisherId, $name, $fields ? $fields : '*', $options);
    return Streams::$cache['streams'] = Db::exportArray($streams);
}
Exemplo n.º 18
0
function Streams_join_post()
{
    $user = Users::loggedInUser(true);
    $publisherId = Streams::requestedPublisherId();
    $streamName = Streams::requestedName(true);
    $streams = Streams::fetch($user->id, $publisherId, $streamName);
    if (empty($streams)) {
        throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => "{publisherId: '{$publisherId}', name: '{$streamName}'}"));
    }
    $stream = reset($streams);
    $options = array();
    if (isset($_REQUEST['extra'])) {
        $options['extra'] = json_decode($_REQUEST['extra'], true);
    }
    $stream->join($options, $participant);
    Q_Response::setSlot('participant', $participant->exportArray());
}
Exemplo n.º 19
0
function Websites_article_put()
{
    // only a logged-in user can do this
    $user = Users::loggedInUser(true);
    $publisherId = Streams::requestedPublisherId();
    if (empty($publisherId)) {
        $publisherId = $_REQUEST['publisherId'] = $user->id;
    }
    $name = Streams::requestedName(true);
    $article = Streams::fetchOne($user->id, $publisherId, $name);
    if (!$article) {
        throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => "{publisherId: '{$publisherId}', name: '{$name}'}"));
    }
    $article->getintouch = isset($_REQUEST['getintouch']) ? $_REQUEST['getintouch'] : '';
    $article->save();
    Q_Response::setSlot('form', '');
}
Exemplo n.º 20
0
function Broadcast_stream_response_content()
{
    $publisherId = Streams::requestedPublisherId(true);
    $name = Streams::requestedName(true);
    $fields = Streams::requestedFields();
    $user = Users::loggedInUser();
    $userId = $user ? $user->id : 0;
    if (isset(Streams::$cache['stream'])) {
        $stream = Streams::$cache['stream'];
    } else {
        $streams = Streams::fetch($userId, $publisherId, $name, $fields, array('limit' => 30));
        if (empty($streams)) {
            throw new Q_Exception("No such stream", 'name');
        }
        $stream = reset($streams);
    }
    if ($publisherId != $userId and !$stream->testReadLevel('content')) {
        return "This belongs to someone else.";
    }
    if ($publisherId != $userId and !$stream->testReadLevel('content')) {
        throw new Users_Exception_NotAuthorized();
    }
    $userIds = array();
    $agreements = Broadcast_Agreement::select('userId')->where(array('publisherId' => $publisherId, 'streamName' => $name, 'platform' => 'facebook'))->fetchDbRows();
    foreach ($agreements as $a) {
        $userIds[] = $a->userId;
    }
    if ($userIds) {
        $agreed_users = Users_User::select('*')->where(array('id' => $userIds))->fetchDbRows();
    } else {
        $agreed_users = array();
    }
    $src = 'Broadcast/widget?';
    $q = array('publisherId' => $publisherId, 'streamName' => $name);
    foreach (array('css', 'button', 'checkmark', 'explanation') as $field) {
        if (isset($_REQUEST[$field])) {
            $q[$field] = $_REQUEST[$field];
        }
    }
    $src .= http_build_query($q, null, '&');
    $style = 'border: 0px;';
    $code = Q_Html::tag('iframe', compact('src', 'style'), '');
    Q_Response::addScript('plugins/Broadcast/js/Broadcast.js');
    return Q::view('Broadcast/content/stream.php', compact('publisherId', 'name', 'fields', 'user', 'stream', 'agreed_users', 'code'));
}
Exemplo n.º 21
0
function Streams_access_put($params)
{
    $user = Users::loggedInUser(true);
    Q_Valid::nonce(true);
    $publisherId = Streams::requestedPublisherId(true);
    $streamName = Streams::requestedName(true);
    $stream = Streams::fetchOne($user->id, $publisherId, $streamName);
    if (!$stream) {
        throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => 'with that name'));
    }
    if (!$stream->testAdminLevel('own')) {
        throw new Users_Exception_NotAuthorized();
    }
    $p = array_merge($_REQUEST, $params);
    $access = new Streams_Access();
    $access->publisherId = $stream->publisherId;
    $access->streamName = $stream->name;
    $access->ofUserId = Q::ifset($_REQUEST, 'ofUserId', '');
    $access->ofContactLabel = Q::ifset($_REQUEST, 'ofContactLabel', '');
    if (empty($access->ofUserId) and empty($access->ofContactLabel)) {
        $fields = array('grantedByUserId', 'filter', 'readLevel', 'writeLevel', 'adminLevel', 'permissions');
        foreach ($fields as $field) {
            if (isset($p[$field])) {
                $stream->{$field} = $p[$field];
            }
        }
        $stream->save();
        return;
    }
    $access->retrieve();
    $fields = array('grantedByUserId', 'filter', 'readLevel', 'writeLevel', 'adminLevel', 'permissions');
    foreach ($fields as $field) {
        if (isset($p[$field])) {
            $access->{$field} = $p[$field];
        }
    }
    $defaults = array('grantedByUserId' => $user->id, 'readLevel' => -1, 'writeLevel' => -1, 'adminLevel' => -1);
    foreach ($defaults as $k => $v) {
        if (!isset($access->{$k})) {
            $access->{$k} = $v;
        }
    }
    $access->save();
    Streams::$cache['access'] = $access;
}
Exemplo n.º 22
0
function Streams_stream_response_content()
{
    $publisherId = Streams::requestedPublisherId(true);
    $name = Streams::requestedName(true);
    $fields = Streams::requestedFields();
    $user = Users::loggedInUser();
    $userId = $user ? $user->id : 0;
    $stream = isset(Streams::$cache['stream']) ? Streams::$cache['stream'] : null;
    if (!isset($stream)) {
        throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => 'that name'));
    }
    if ($publisherId != $userId and !$stream->testReadLevel('content')) {
        Q_Response::setNotice('Streams/stream/response/content', 'This content is hidden from you.', true);
        return '';
    }
    // show stream as usual
    return Q::view('Streams/content/stream.php', compact('publisherId', 'name', 'fields', 'user', 'stream'));
}
Exemplo n.º 23
0
function Streams_access_response_content($options)
{
    $ajax = true;
    $user = Users::loggedInUser(true);
    $streamName = Streams::requestedName(true);
    $publisherId = Streams::requestedPublisherId();
    if (empty($publisherId)) {
        $publisherId = $user->id;
    }
    $stream = new Streams_Stream();
    $stream->publisherId = $publisherId;
    $stream->name = $streamName;
    if (!$stream->retrieve()) {
        throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => 'with that name'), 'name');
    }
    $controls = !empty($options['controls']);
    Q_Response::setSlot('title', "Access to: " . $stream->title);
    return Q::tool('Streams/access', compact('publisherId', 'streamName', 'ajax', 'controls'), $controls ? array('tag' => null) : array());
}
Exemplo n.º 24
0
/**
 * This tool generates a category selector.
 *
 * @param array $options
 *  An associative array of parameters, containing:
 *  "publisherId" => Optional. publisherId of the stream to present. If "stream" parameter is empty
 *    defaults to Streams::requestedPublisherId()
 *  "name" => Optional. the name of the stream to present. If "stream" parameter is empty
 *    defaults to Streams::requestedName()
 *  "stream" => Optional. Object. The stream objects to show categories.
 */
function Streams_category_tool($options)
{
    extract($options);
    $user = Users::loggedInUser(true);
    $userId = $user->id;
    // PK of stream to manage categories
    $publisherId = Streams::requestedPublisherId(true);
    $name = Streams::requestedName(true);
    $stream = Streams::get($userId, $publisherId, $name, null, true);
    if (!$stream) {
        throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => "{publisherId: {$publisherId}, name: {$name}}"));
    }
    // activate tool frontend
    $default = array('publisherId' => $stream->publisherId, 'name' => $stream->name);
    $options = array_merge($default, $options);
    Q_Response::setToolOptions($options);
    // get the list of categories
    list($relations, $categories) = Streams::related($userId, $publisherId, $name, true);
    return Q::view("Streams/tool/category.php", compact('relations', 'categories', 'stream'));
}
Exemplo n.º 25
0
function Broadcast_stream_response_form($params)
{
    extract($params);
    if (empty($publisherId)) {
        $publisherId = Streams::requestedPublisherId(true);
    }
    if (empty($name)) {
        $name = Streams::requestedName(true);
    }
    $name = is_array($name) ? implode('/', $name) : $name;
    $src = 'Broadcast/widget?';
    $q = array('publisherId' => $publisherId, 'streamName' => $name);
    foreach (array('css', 'button', 'checkbox', 'explanation') as $field) {
        if (isset($_REQUEST[$field])) {
            $q[$field] = $_REQUEST[$field];
        }
    }
    $src .= http_build_query($q, null, '&');
    $style = 'border: 0px;';
    $code = Q_Html::tag('iframe', compact('src', 'style'), '');
    return $code;
}
Exemplo n.º 26
0
/**
 * Provide player content to view the stream content
 * Uses Streams/$type/get.php view and Streams::get to retrieve stream data
 *
 **/
function Streams_get_response_player()
{
    $user = Users::loggedInUser();
    $userId = $user ? $user->id : 0;
    $publisherId = Streams::requestedPublisherId(true);
    $name = Streams::requestedName(true);
    if (substr($name, -1) === '/') {
        throw new Q_Exception("Player cannot show multiple streams", compact('publisherId', 'name'));
    }
    /*
     * Get shall return only streams which user is authorized to see.
     */
    if (!($stream = Streams::get($userId, $publisherId, $name, null, true))) {
        throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => compact('publisherId', 'name')));
    }
    // join the stream
    if ($userId !== 0 && $stream->testWriteLevel('join')) {
        Streams_Stream::join($userId, $stream->publisherId, $stream->name);
    }
    // Let's be nice to poor Windows users
    $type = join(DS, explode('/', $stream->type));
    return Q::view("Streams/{$type}/get.php", compact('stream', 'userId'));
}
Exemplo n.º 27
0
function Streams_participant_response_participants()
{
    if (isset(Streams::$cache['participant'])) {
        $participant = Streams::$cache['participant'];
        return Db::exportArray(array($participant->userId => $participant));
    }
    $publisherId = Streams::requestedPublisherId(true);
    $streamName = Streams::requestedName(true);
    $limit = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : 10;
    $offset = isset($_REQUEST['offset']) ? $_REQUEST['offset'] : -1;
    $state = isset($_REQUEST['state']) ? $_REQUEST['state'] : null;
    $user = Users::loggedInUser();
    $userId = $user ? $user->id : "";
    $stream = Streams::fetchOne($userId, $publisherId, $streamName);
    if (empty($stream)) {
        throw new Q_Exception_MissingRow(array('table' => 'Stream', 'criteria' => "{publisherId: '{$publisherId}', name: '{$streamName}'}"));
    }
    if (!$stream->testReadLevel('participants')) {
        throw new Users_Exception_NotAuthorized();
    }
    $participants = $stream->getParticipants(compact('limit', 'offset', 'state'));
    Db::exportArray($participants);
}
Exemplo n.º 28
0
function Streams_message_response_messages()
{
    if (isset(Streams::$cache['message'])) {
        $message = Streams::$cache['message'];
        return Db::exportArray(array($message->ordinal => $message));
    }
    if (isset(Streams::$cache['messages'])) {
        return Db::exportArray(Streams::$cache['messages']);
    }
    $publisherId = Streams::requestedPublisherId(true);
    $streamName = Streams::requestedName(true);
    $type = Streams::requestedMessageType();
    $maxLimit = Q_Config::get('Streams', 'defaults', 'getMessagesLimit', 100);
    $limit = min($maxLimit, Q::ifset($_REQUEST, 'limit', $maxLimit));
    if (isset($_REQUEST['ordinal'])) {
        $min = $_REQUEST['ordinal'];
        $limit = 1;
    }
    if (isset($_REQUEST['min'])) {
        $min = $_REQUEST['min'];
    }
    $max = isset($_REQUEST['max']) ? $_REQUEST['max'] : -1;
    if (isset($_REQUEST['ascending'])) {
        $ascending = $_REQUEST['ascending'];
    }
    $user = Users::loggedInUser();
    $userId = $user ? $user->id : "";
    $stream = isset(Streams::$cache['stream']) ? Streams::$cache['stream'] : Streams::fetchOne($userId, $publisherId, $streamName);
    if (!$stream) {
        throw new Q_Exception_MissingRow(array('table' => 'Stream', 'criteria' => "{publisherId: '{$publisherId}', name: '{$streamName}'}"));
    }
    if (!$stream->testReadLevel('messages')) {
        throw new Users_Exception_NotAuthorized();
    }
    $messages = $stream->getMessages(compact('type', 'min', 'max', 'limit', 'ascending'));
    return Db::exportArray($messages);
}
Exemplo n.º 29
0
function Streams_message_response_message()
{
    if (isset(Streams::$cache['message'])) {
        return Streams::$cache['message']->exportArray();
    }
    $publisherId = Streams::requestedPublisherId(true);
    $streamName = Streams::requestedName(true);
    if (empty($_REQUEST['ordinal'])) {
        throw new Q_Exception_RequiredField(array('field' => 'ordinal'));
    }
    $min = $_REQUEST['ordinal'];
    $limit = 1;
    $user = Users::loggedInUser();
    $userId = $user ? $user->id : "";
    $stream = isset(Streams::$cache['stream']) ? Streams::$cache['stream'] : Streams::fetchOne($userId, $publisherId, $streamName);
    if (!$stream) {
        throw new Q_Exception_MissingRow(array('table' => 'Stream', 'criteria' => "{publisherId: '{$publisherId}', name: '{$streamName}'}"));
    }
    if (!$stream->testReadLevel('messages')) {
        throw new Users_Exception_NotAuthorized();
    }
    $messages = $stream->getMessages(compact('type', 'min', 'limit'));
    return !empty($messages) ? reset($messages)->exportArray() : null;
}
Exemplo n.º 30
0
/**
 * Subscription tool
 * @param array $options
 *  "publisherId" => the id of the user who is publishing the stream
 *  "streamName" => the name of the stream for which to edit access levels
 */
function Streams_subscription_tool($options)
{
    $subscribed = 'no';
    extract($options);
    $user = Users::loggedInUser(true);
    if (!isset($publisherId)) {
        $publisherId = Streams::requestedPublisherId(true);
    }
    if (!isset($streamName)) {
        $streamName = Streams::requestedName();
    }
    $stream = Streams::fetchOne($user->id, $publisherId, $streamName);
    if (!$stream) {
        throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => compact('publisherId', 'streamName')));
    }
    $streams_participant = new Streams_Participant();
    $streams_participant->publisherId = $publisherId;
    $streams_participant->streamName = $streamName;
    $streams_participant->userId = $user->id;
    if ($streams_participant->retrieve()) {
        $subscribed = $streams_participant->subscribed;
    }
    $types = Q_Config::get('Streams', 'types', $stream->type, 'messages', array());
    $messageTypes = array();
    foreach ($types as $type => $msg) {
        $name = Q::ifset($msg, 'title', $type);
        /*
         * group by name
         */
        foreach ($messageTypes as $msgType) {
            if ($msgType['name'] == $name) {
                continue 2;
            }
        }
        $messageTypes[] = array('value' => $type, 'name' => $name);
    }
    $usersFetch = array('userId' => $user->id, 'state' => 'active');
    $devices = array();
    $emails = Users_Email::select('address')->where($usersFetch)->fetchAll(PDO::FETCH_COLUMN);
    $mobiles = Users_Mobile::select('number')->where($usersFetch)->fetchAll(PDO::FETCH_COLUMN);
    foreach ($emails as $email) {
        $devices[] = array('value' => Q::json_encode(array('email' => $email)), 'name' => 'my email');
    }
    foreach ($mobiles as $mobile) {
        $devices[] = array('value' => Q::json_encode(array('mobile' => $mobile)), 'name' => 'my mobile');
    }
    $items = array();
    $rules = Streams_Rule::select('deliver, filter')->where(array('ofUserId' => $user->id, 'publisherId' => $publisherId, 'streamName' => $streamName))->fetchAll(PDO::FETCH_ASSOC);
    while ($rule = array_pop($rules)) {
        $filter = json_decode($rule['filter']);
        /*
         * group by name
         */
        foreach ($rules as $val) {
            if (json_decode($val['filter'])->labels == $filter->labels) {
                continue 2;
            }
        }
        $items[] = array('deliver' => json_decode($rule['deliver']), 'filter' => $filter);
    }
    Q_Response::addScript("plugins/Streams/js/Streams.js");
    Q_Response::addScript("plugins/Streams/js/tools/subscription.js");
    Q_Response::setToolOptions(compact('items', 'subscribed', 'messageTypes', 'devices', 'publisherId', 'streamName'));
}