Esempio n. 1
0
 static function saveNew(Profile $profile, $url, $options = array())
 {
     $vid = new Video();
     $vid->id = UUID::gen();
     $vid->profile_id = $profile->id;
     $vid->url = $url;
     $options['object_type'] = Video::OBJECT_TYPE;
     if (!array_key_exists('uri', $options)) {
         $options['uri'] = common_local_url('showvideo', array('id' => $vid->id));
     }
     if (!array_key_exists('rendered', $options)) {
         $options['rendered'] = sprintf("<video src=\"%s\">Sorry, your browser doesn't support the video tag.</video>", $url);
     }
     $vid->uri = $options['uri'];
     $vid->insert();
     return Notice::saveNew($profile->id, '', 'web', $options);
 }
Esempio n. 2
0
 function save_notice(&$req, &$consumer, &$token)
 {
     $version = $req->get_parameter('omb_version');
     if ($version != OMB_VERSION_01) {
         $this->clientError(_('Unsupported OMB version'), 400);
         return false;
     }
     # First, check to see
     $listenee = $req->get_parameter('omb_listenee');
     $remote_profile = Remote_profile::staticGet('uri', $listenee);
     if (!$remote_profile) {
         $this->clientError(_('Profile unknown'), 403);
         return false;
     }
     $sub = Subscription::staticGet('token', $token->key);
     if (!$sub) {
         $this->clientError(_('No such subscription'), 403);
         return false;
     }
     $content = $req->get_parameter('omb_notice_content');
     $content_shortened = common_shorten_links($content);
     if (mb_strlen($content_shortened) > 140) {
         $this->clientError(_('Invalid notice content'), 400);
         return false;
     }
     $notice_uri = $req->get_parameter('omb_notice');
     if (!Validate::uri($notice_uri) && !common_valid_tag($notice_uri)) {
         $this->clientError(_('Invalid notice uri'), 400);
         return false;
     }
     $notice_url = $req->get_parameter('omb_notice_url');
     if ($notice_url && !common_valid_http_url($notice_url)) {
         $this->clientError(_('Invalid notice url'), 400);
         return false;
     }
     $notice = Notice::staticGet('uri', $notice_uri);
     if (!$notice) {
         $notice = Notice::saveNew($remote_profile->id, $content, 'omb', false, null, $notice_uri);
         if (is_string($notice)) {
             common_server_serror($notice, 500);
             return false;
         }
         common_broadcast_notice($notice, true);
     }
     return true;
 }
Esempio n. 3
0
 static function saveNew(Profile $profile, $photo_uri, $thumb_uri, $title, $description, $options = array())
 {
     $photo = new Photo();
     $photo->id = UUID::gen();
     $photo->profile_id = $profile->id;
     $photo->photo_uri = $photo_uri;
     $photo->thumb_uri = $thumb_uri;
     $options['object_type'] = Photo::OBJECT_TYPE;
     if (!array_key_exists('uri', $options)) {
         $options['uri'] = common_local_url('showphoto', array('id' => $photo->id));
     }
     if (!array_key_exists('rendered', $options)) {
         $options['rendered'] = sprintf("<img src=\"%s\" alt=\"%s\"></img>", $photo_uri, $title);
     }
     $photo->uri = $options['uri'];
     $photo->insert();
     return Notice::saveNew($profile->id, '', 'web', $options);
 }
function importActivityStream($user, $doc)
{
    $feed = $doc->documentElement;
    $entries = $feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
    for ($i = $entries->length - 1; $i >= 0; $i--) {
        $entry = $entries->item($i);
        $activity = new Activity($entry, $feed);
        $object = $activity->objects[0];
        if (!have_option('q', 'quiet')) {
            print $activity->content . "\n";
        }
        $html = getTweetHtml($object->link);
        $config = array('safe' => 1, 'deny_attribute' => 'class,rel,id,style,on*');
        $html = htmLawed($html, $config);
        $content = html_entity_decode(strip_tags($html), ENT_QUOTES, 'UTF-8');
        $notice = Notice::saveNew($user->id, $content, 'importtwitter', array('uri' => $object->id, 'url' => $object->link, 'rendered' => $html, 'created' => common_sql_date($activity->time), 'replies' => array(), 'groups' => array()));
    }
}
Esempio n. 5
0
function newNotice($i, $tagmax)
{
    global $userprefix;
    $n = rand(0, $i - 1);
    $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
    $is_reply = rand(0, 4);
    $content = 'Test notice content';
    if ($is_reply == 0) {
        $n = rand(0, $i - 1);
        $content = "@{$userprefix}{$n} " . $content;
    }
    $has_hash = rand(0, 2);
    if ($has_hash == 0) {
        $hashcount = rand(0, 2);
        for ($j = 0; $j < $hashcount; $j++) {
            $h = rand(0, $tagmax);
            $content .= " #tag{$h}";
        }
    }
    $notice = Notice::saveNew($user->id, $content, 'system');
    $user->free();
    $notice->free();
}
Esempio n. 6
0
 /**
  * Save passed notice
  *
  * Stores the OMB notice $notice. The datastore may change the passed notice.
  * This might by neccessary for URIs depending on a database key. Note that
  * it is the user’s duty to present a mechanism for his OMB_Datastore to
  * appropriately change his OMB_Notice.
  * Throws exceptions in case of error.
  *
  * @param OMB_Notice $notice The OMB notice which should be saved
  *
  * @access public
  **/
 public function saveNotice(&$omb_notice)
 {
     if (Notice::staticGet('uri', $omb_notice->getIdentifierURI())) {
         // TRANS: Exception thrown when a notice is denied because it has been sent before.
         throw new Exception(_('Duplicate notice.'));
     }
     $author_uri = $omb_notice->getAuthor()->getIdentifierURI();
     common_log(LOG_DEBUG, $author_uri, __FILE__);
     $author = Remote_profile::staticGet('uri', $author_uri);
     if (!$author) {
         $author = User::staticGet('uri', $author_uri);
     }
     if (!$author) {
         throw new Exception('No such user.');
     }
     common_log(LOG_DEBUG, print_r($author, true), __FILE__);
     $notice = Notice::saveNew($author->id, $omb_notice->getContent(), 'omb', array('is_local' => Notice::REMOTE_OMB, 'uri' => $omb_notice->getIdentifierURI()));
 }
 function postNote($user, $author, $activity)
 {
     $note = $activity->objects[0];
     $sourceUri = $note->id;
     $notice = Notice::getKV('uri', $sourceUri);
     if ($notice instanceof Notice) {
         common_log(LOG_INFO, "Notice {$sourceUri} already exists.");
         if ($this->trusted) {
             $profile = $notice->getProfile();
             $uri = $profile->getUri();
             if ($uri === $author->id) {
                 common_log(LOG_INFO, sprintf('Updating notice author from %s to %s', $author->id, $user->getUri()));
                 $orig = clone $notice;
                 $notice->profile_id = $user->id;
                 $notice->update($orig);
                 return;
             } else {
                 // TRANS: Client exception thrown when trying to import a notice by another user.
                 // TRANS: %1$s is the source URI of the notice, %2$s is the URI of the author.
                 throw new ClientException(sprintf(_('Already know about notice %1$s and ' . ' it has a different author %2$s.'), $sourceUri, $uri));
             }
         } else {
             // TRANS: Client exception thrown when trying to overwrite the author information for a non-trusted user during import.
             throw new ClientException(_('Not overwriting author info for non-trusted user.'));
         }
     }
     // Use summary as fallback for content
     if (!empty($note->content)) {
         $sourceContent = $note->content;
     } else {
         if (!empty($note->summary)) {
             $sourceContent = $note->summary;
         } else {
             if (!empty($note->title)) {
                 $sourceContent = $note->title;
             } else {
                 // @fixme fetch from $sourceUrl?
                 // TRANS: Client exception thrown when trying to import a notice without content.
                 // TRANS: %s is the notice URI.
                 throw new ClientException(sprintf(_('No content for notice %s.'), $sourceUri));
             }
         }
     }
     // Get (safe!) HTML and text versions of the content
     $rendered = common_purify($sourceContent);
     $content = common_strip_html($rendered);
     $shortened = $user->shortenLinks($content);
     $options = array('is_local' => Notice::LOCAL_PUBLIC, 'uri' => $sourceUri, 'rendered' => $rendered, 'replies' => array(), 'groups' => array(), 'tags' => array(), 'urls' => array(), 'distribute' => false);
     // Check for optional attributes...
     if (!empty($activity->time)) {
         $options['created'] = common_sql_date($activity->time);
     }
     if ($activity->context) {
         // Any individual or group attn: targets?
         list($options['groups'], $options['replies']) = $this->filterAttention($activity->context->attention);
         // Maintain direct reply associations
         // @fixme what about conversation ID?
         if (!empty($activity->context->replyToID)) {
             $orig = Notice::getKV('uri', $activity->context->replyToID);
             if ($orig instanceof Notice) {
                 $options['reply_to'] = $orig->id;
             }
         }
         $location = $activity->context->location;
         if ($location) {
             $options['lat'] = $location->lat;
             $options['lon'] = $location->lon;
             if ($location->location_id) {
                 $options['location_ns'] = $location->location_ns;
                 $options['location_id'] = $location->location_id;
             }
         }
     }
     // Atom categories <-> hashtags
     foreach ($activity->categories as $cat) {
         if ($cat->term) {
             $term = common_canonical_tag($cat->term);
             if ($term) {
                 $options['tags'][] = $term;
             }
         }
     }
     // Atom enclosures -> attachment URLs
     foreach ($activity->enclosures as $href) {
         // @fixme save these locally or....?
         $options['urls'][] = $href;
     }
     common_log(LOG_INFO, "Saving notice {$options['uri']}");
     $saved = Notice::saveNew($user->id, $content, 'restore', $options);
     return $saved;
 }
 /**
  * Handle the request
  *
  * Make a new notice for the update, save it, and show it
  *
  * @return void
  */
 protected function handle()
 {
     parent::handle();
     // Workaround for PHP returning empty $_POST and $_FILES when POST
     // length > post_max_size in php.ini
     if (empty($_FILES) && empty($_POST) && $_SERVER['CONTENT_LENGTH'] > 0) {
         // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
         // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
         $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.', 'The server was unable to handle that much POST data (%s bytes) due to its current configuration.', intval($_SERVER['CONTENT_LENGTH']));
         $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
     }
     if (empty($this->status)) {
         // TRANS: Client error displayed when the parameter "status" is missing.
         $this->clientError(_('Client must provide a \'status\' parameter with a value.'));
     }
     if (is_null($this->scoped)) {
         // TRANS: Client error displayed when updating a status for a non-existing user.
         $this->clientError(_('No such user.'), 404);
     }
     /* Do not call shortenlinks until the whole notice has been build */
     // Check for commands
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($this->auth_user, $this->status);
     if ($cmd) {
         if ($this->supported($cmd)) {
             $cmd->execute(new Channel());
         }
         // Cmd not supported?  Twitter just returns your latest status.
         // And, it returns your last status whether the cmd was successful
         // or not!
         $this->notice = $this->auth_user->getCurrentNotice();
     } else {
         $reply_to = null;
         if (!empty($this->in_reply_to_status_id)) {
             // Check whether notice actually exists
             $reply = Notice::getKV($this->in_reply_to_status_id);
             if ($reply) {
                 $reply_to = $this->in_reply_to_status_id;
             } else {
                 // TRANS: Client error displayed when replying to a non-existing notice.
                 $this->clientError(_('Parent notice not found.'), 404);
             }
         }
         $upload = null;
         try {
             $upload = MediaFile::fromUpload('media', $this->scoped);
         } catch (NoUploadedMediaException $e) {
             // There was no uploaded media for us today.
         }
         if (isset($upload)) {
             $this->status .= ' ' . $upload->shortUrl();
             /* Do not call shortenlinks until the whole notice has been build */
         }
         // in Qvitter we shorten _before_ posting, so disble shortening here
         $status_shortened = $this->status;
         if (Notice::contentTooLong($status_shortened)) {
             if ($upload instanceof MediaFile) {
                 $upload->delete();
             }
             // TRANS: Client error displayed exceeding the maximum notice length.
             // TRANS: %d is the maximum lenth for a notice.
             $msg = _m('Maximum notice size is %d character, including attachment URL.', 'Maximum notice size is %d characters, including attachment URL.', Notice::maxContent());
             /* Use HTTP 413 error code (Request Entity Too Large)
              * instead of basic 400 for better understanding
              */
             $this->clientError(sprintf($msg, Notice::maxContent()), 413);
         }
         $content = html_entity_decode($status_shortened, ENT_NOQUOTES, 'UTF-8');
         $options = array('reply_to' => $reply_to);
         // -------------------------------------------------------------
         // -------- Qvitter's post-to-the-right-group stuff! -----------
         // -------------------------------------------------------------
         // guess the groups by the content first, if we don't have group id:s as meta data
         $profile = Profile::getKV('id', $this->scoped->id);
         $guessed_groups = User_group::groupsFromText($content, $profile);
         // if the user has specified any group id:s, correct the guesswork
         if (strlen($this->post_to_groups) > 0) {
             // get the groups that the user wants to post to
             $group_ids = explode(':', $this->post_to_groups);
             $correct_groups = array();
             foreach ($group_ids as $group_id) {
                 $correct_groups[] = User_group::getKV('id', $group_id);
             }
             // correct the guesses
             $corrected_group_ids = array();
             foreach ($guessed_groups as $guessed_group) {
                 $id_to_keep = $guessed_group->id;
                 foreach ($correct_groups as $k => $correct_group) {
                     if ($correct_group->nickname == $guessed_group->nickname) {
                         $id_to_keep = $correct_group->id;
                         unset($correct_groups[$k]);
                         break;
                     }
                 }
                 $corrected_group_ids[$id_to_keep] = true;
             }
             // but we still want to post to all of the groups that the user specified by id
             // even if we couldn't use it to correct a bad guess
             foreach ($correct_groups as $correct_group) {
                 $corrected_group_ids[$correct_group->id] = true;
             }
             $options['groups'] = array_keys($corrected_group_ids);
         } else {
             $guessed_ids = array();
             foreach ($guessed_groups as $guessed_group) {
                 $guessed_ids[$guessed_group->id] = true;
             }
             $options['groups'] = array_keys($guessed_ids);
         }
         // -------------------------------------------------------------
         // ------ End of Qvitter's post-to-the-right-group stuff! ------
         // -------------------------------------------------------------
         if ($this->scoped->shareLocation()) {
             $locOptions = Notice::locationOptions($this->lat, $this->lon, null, null, $this->scoped);
             $options = array_merge($options, $locOptions);
         }
         try {
             $this->notice = Notice::saveNew($this->scoped->id, $content, $this->source, $options);
         } catch (Exception $e) {
             $this->clientError($e->getMessage(), $e->getCode());
         }
         if (isset($upload)) {
             $upload->attachToNotice($this->notice);
         }
     }
     $this->showNotice();
 }
 function handle($channel)
 {
     $notice = $this->getNotice($this->other);
     $recipient = $notice->getProfile();
     $len = mb_strlen($this->text);
     if ($len == 0) {
         $channel->error($this->user, _('No content!'));
         return;
     }
     $this->text = common_shorten_links($this->text);
     if (Notice::contentTooLong($this->text)) {
         $channel->error($this->user, sprintf(_('Notice too long - maximum is %d characters, you sent %d'), Notice::maxContent(), mb_strlen($this->text)));
         return;
     }
     $notice = Notice::saveNew($this->user->id, $this->text, $channel->source(), array('reply_to' => $notice->id));
     if ($notice) {
         $channel->output($this->user, sprintf(_('Reply to %s sent'), $recipient->nickname));
     } else {
         $channel->error($this->user, _('Error saving notice.'));
     }
 }
Esempio n. 10
0
 function postNote($activity)
 {
     $note = $activity->objects[0];
     // Use summary as fallback for content
     if (!empty($note->content)) {
         $sourceContent = $note->content;
     } else {
         if (!empty($note->summary)) {
             $sourceContent = $note->summary;
         } else {
             if (!empty($note->title)) {
                 $sourceContent = $note->title;
             } else {
                 // @fixme fetch from $sourceUrl?
                 // TRANS: Client error displayed when posting a notice without content through the API.
                 // TRANS: %d is the notice ID (number).
                 $this->clientError(sprintf(_('No content for notice %d.'), $note->id));
             }
         }
     }
     // Get (safe!) HTML and text versions of the content
     $rendered = common_purify($sourceContent);
     $content = common_strip_html($rendered);
     $shortened = $this->auth_user->shortenLinks($content);
     $options = array('is_local' => Notice::LOCAL_PUBLIC, 'rendered' => $rendered, 'replies' => array(), 'groups' => array(), 'tags' => array(), 'urls' => array());
     // accept remote URI (not necessarily a good idea)
     common_debug("Note ID is {$note->id}");
     if (!empty($note->id)) {
         $notice = Notice::getKV('uri', trim($note->id));
         if (!empty($notice)) {
             // TRANS: Client error displayed when using another format than AtomPub.
             // TRANS: %s is the notice URI.
             $this->clientError(sprintf(_('Notice with URI "%s" already exists.'), $note->id));
         }
         common_log(LOG_NOTICE, "Saving client-supplied notice URI '{$note->id}'");
         $options['uri'] = $note->id;
     }
     // accept remote create time (also maybe not such a good idea)
     if (!empty($activity->time)) {
         common_log(LOG_NOTICE, "Saving client-supplied create time {$activity->time}");
         $options['created'] = common_sql_date($activity->time);
     }
     // Check for optional attributes...
     if ($activity->context instanceof ActivityContext) {
         foreach ($activity->context->attention as $uri => $type) {
             try {
                 $profile = Profile::fromUri($uri);
                 if ($profile->isGroup()) {
                     $options['groups'][] = $profile->id;
                 } else {
                     $options['replies'][] = $uri;
                 }
             } catch (UnknownUriException $e) {
                 common_log(LOG_WARNING, sprintf('AtomPub post with unknown attention URI %s', $uri));
             }
         }
         // Maintain direct reply associations
         // @fixme what about conversation ID?
         if (!empty($activity->context->replyToID)) {
             $orig = Notice::getKV('uri', $activity->context->replyToID);
             if (!empty($orig)) {
                 $options['reply_to'] = $orig->id;
             }
         }
         $location = $activity->context->location;
         if ($location) {
             $options['lat'] = $location->lat;
             $options['lon'] = $location->lon;
             if ($location->location_id) {
                 $options['location_ns'] = $location->location_ns;
                 $options['location_id'] = $location->location_id;
             }
         }
     }
     // Atom categories <-> hashtags
     foreach ($activity->categories as $cat) {
         if ($cat->term) {
             $term = common_canonical_tag($cat->term);
             if ($term) {
                 $options['tags'][] = $term;
             }
         }
     }
     // Atom enclosures -> attachment URLs
     foreach ($activity->enclosures as $href) {
         // @fixme save these locally or....?
         $options['urls'][] = $href;
     }
     $saved = Notice::saveNew($this->target->id, $content, 'atompub', $options);
     return $saved;
 }
Esempio n. 11
0
 function saveNewNotice()
 {
     $user = $this->flink->getUser();
     $content = $this->trimmed('status_textarea');
     if (!$content) {
         $this->showPage(_m('No notice content!'));
         return;
     } else {
         $content_shortened = common_shorten_links($content);
         if (Notice::contentTooLong($content_shortened)) {
             $this->showPage(sprintf(_m('That\'s too long. Max notice size is %d chars.'), Notice::maxContent()));
             return;
         }
     }
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($user, $content_shortened);
     if ($cmd) {
         // XXX fix this
         $cmd->execute(new WebChannel());
         return;
     }
     $replyto = $this->trimmed('inreplyto');
     try {
         $notice = Notice::saveNew($user->id, $content, 'web', array('reply_to' => $replyto == 'false' ? null : $replyto));
     } catch (Exception $e) {
         $this->showPage($e->getMessage());
         return;
     }
 }
 /**
  * Process an incoming post activity from this remote feed.
  * @param Activity $activity
  * @param string $method 'push' or 'salmon'
  * @return mixed saved Notice or false
  * @fixme break up this function, it's getting nasty long
  */
 public function processPost($activity, $method)
 {
     if ($this->isGroup()) {
         // A group feed will contain posts from multiple authors.
         // @fixme validate these profiles in some way!
         $oprofile = self::ensureActorProfile($activity);
         if ($oprofile->isGroup()) {
             // Groups can't post notices in StatusNet.
             common_log(LOG_WARNING, "OStatus: skipping post with group listed as author: {$oprofile->uri} in feed from {$this->uri}");
             return false;
         }
     } else {
         $actor = $activity->actor;
         if (empty($actor)) {
             // OK here! assume the default
         } else {
             if ($actor->id == $this->uri || $actor->link == $this->uri) {
                 $this->updateFromActivityObject($actor);
             } else {
                 throw new Exception("Got an actor '{$actor->title}' ({$actor->id}) on single-user feed for {$this->uri}");
             }
         }
         $oprofile = $this;
     }
     // It's not always an ActivityObject::NOTE, but... let's just say it is.
     $note = $activity->objects[0];
     // The id URI will be used as a unique identifier for for the notice,
     // protecting against duplicate saves. It isn't required to be a URL;
     // tag: URIs for instance are found in Google Buzz feeds.
     $sourceUri = $note->id;
     $dupe = Notice::staticGet('uri', $sourceUri);
     if ($dupe) {
         common_log(LOG_INFO, "OStatus: ignoring duplicate post: {$sourceUri}");
         return false;
     }
     // We'll also want to save a web link to the original notice, if provided.
     $sourceUrl = null;
     if ($note->link) {
         $sourceUrl = $note->link;
     } else {
         if ($activity->link) {
             $sourceUrl = $activity->link;
         } else {
             if (preg_match('!^https?://!', $note->id)) {
                 $sourceUrl = $note->id;
             }
         }
     }
     // Use summary as fallback for content
     if (!empty($note->content)) {
         $sourceContent = $note->content;
     } else {
         if (!empty($note->summary)) {
             $sourceContent = $note->summary;
         } else {
             if (!empty($note->title)) {
                 $sourceContent = $note->title;
             } else {
                 // @fixme fetch from $sourceUrl?
                 throw new ClientException("No content for notice {$sourceUri}");
             }
         }
     }
     // Get (safe!) HTML and text versions of the content
     $rendered = $this->purify($sourceContent);
     $content = html_entity_decode(strip_tags($rendered));
     $shortened = common_shorten_links($content);
     // If it's too long, try using the summary, and make the
     // HTML an attachment.
     $attachment = null;
     if (Notice::contentTooLong($shortened)) {
         $attachment = $this->saveHTMLFile($note->title, $rendered);
         $summary = html_entity_decode(strip_tags($note->summary));
         if (empty($summary)) {
             $summary = $content;
         }
         $shortSummary = common_shorten_links($summary);
         if (Notice::contentTooLong($shortSummary)) {
             $url = common_shorten_url($sourceUrl);
             $shortSummary = substr($shortSummary, 0, Notice::maxContent() - (mb_strlen($url) + 2));
             $content = $shortSummary . ' ' . $url;
             // We mark up the attachment link specially for the HTML output
             // so we can fold-out the full version inline.
             $attachUrl = common_local_url('attachment', array('attachment' => $attachment->id));
             $rendered = common_render_text($shortSummary) . '<a href="' . htmlspecialchars($attachUrl) . '"' . ' class="attachment more"' . ' title="' . htmlspecialchars(_m('Show more')) . '">' . '&#8230;' . '</a>';
         }
     }
     $options = array('is_local' => Notice::REMOTE_OMB, 'url' => $sourceUrl, 'uri' => $sourceUri, 'rendered' => $rendered, 'replies' => array(), 'groups' => array(), 'tags' => array(), 'urls' => array());
     // Check for optional attributes...
     if (!empty($activity->time)) {
         $options['created'] = common_sql_date($activity->time);
     }
     if ($activity->context) {
         // Any individual or group attn: targets?
         $replies = $activity->context->attention;
         $options['groups'] = $this->filterReplies($oprofile, $replies);
         $options['replies'] = $replies;
         // Maintain direct reply associations
         // @fixme what about conversation ID?
         if (!empty($activity->context->replyToID)) {
             $orig = Notice::staticGet('uri', $activity->context->replyToID);
             if (!empty($orig)) {
                 $options['reply_to'] = $orig->id;
             }
         }
         $location = $activity->context->location;
         if ($location) {
             $options['lat'] = $location->lat;
             $options['lon'] = $location->lon;
             if ($location->location_id) {
                 $options['location_ns'] = $location->location_ns;
                 $options['location_id'] = $location->location_id;
             }
         }
     }
     // Atom categories <-> hashtags
     foreach ($activity->categories as $cat) {
         if ($cat->term) {
             $term = common_canonical_tag($cat->term);
             if ($term) {
                 $options['tags'][] = $term;
             }
         }
     }
     // Atom enclosures -> attachment URLs
     foreach ($activity->enclosures as $href) {
         // @fixme save these locally or....?
         $options['urls'][] = $href;
     }
     try {
         $saved = Notice::saveNew($oprofile->profile_id, $content, 'ostatus', $options);
         if ($saved) {
             Ostatus_source::saveNew($saved, $this, $method);
             if (!empty($attachment)) {
                 File_to_post::processNew($attachment->id, $saved->id);
             }
         }
     } catch (Exception $e) {
         common_log(LOG_ERR, "OStatus save of remote message {$sourceUri} failed: " . $e->getMessage());
         throw $e;
     }
     common_log(LOG_INFO, "OStatus saved remote message {$sourceUri} as notice id {$saved->id}");
     return $saved;
 }
 /**
  * Save a new notice, based on arguments
  *
  * If successful, will show the notice, or return an Ajax-y result.
  * If not, it will show an error message -- possibly Ajax-y.
  *
  * Also, if the notice input looks like a command, it will run the
  * command and show the results -- again, possibly ajaxy.
  *
  * @return void
  */
 function saveNewNotice()
 {
     $user = common_current_user();
     assert($user);
     // XXX: maybe an error instead...
     $content = $this->trimmed('status_textarea');
     if (!$content) {
         $this->clientError(_('No content!'));
         return;
     }
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($user, $content);
     if ($cmd) {
         if ($this->boolean('ajax')) {
             $cmd->execute(new AjaxWebChannel($this));
         } else {
             $cmd->execute(new WebChannel($this));
         }
         return;
     }
     $content_shortened = common_shorten_links($content);
     if (Notice::contentTooLong($content_shortened)) {
         $this->clientError(sprintf(_('That\'s too long. ' . 'Max notice size is %d chars.'), Notice::maxContent()));
     }
     $replyto = $this->trimmed('inreplyto');
     #If an ID of 0 is wrongly passed here, it will cause a database error,
     #so override it...
     if ($replyto == 0) {
         $replyto = 'false';
     }
     $upload = null;
     $upload = MediaFile::fromUpload('attach');
     if (isset($upload)) {
         $content_shortened .= ' ' . $upload->shortUrl();
         if (Notice::contentTooLong($content_shortened)) {
             $upload->delete();
             $this->clientError(sprintf(_('Max notice size is %d chars, including attachment URL.'), Notice::maxContent()));
         }
     }
     $options = array('reply_to' => $replyto == 'false' ? null : $replyto);
     if ($user->shareLocation()) {
         // use browser data if checked; otherwise profile data
         if ($this->arg('notice_data-geo')) {
             $locOptions = Notice::locationOptions($this->trimmed('lat'), $this->trimmed('lon'), $this->trimmed('location_id'), $this->trimmed('location_ns'), $user->getProfile());
         } else {
             $locOptions = Notice::locationOptions(null, null, null, null, $user->getProfile());
         }
         $options = array_merge($options, $locOptions);
     }
     $notice = Notice::saveNew($user->id, $content_shortened, 'web', $options);
     if (isset($upload)) {
         $upload->attachToNotice($notice);
     }
     if ($this->boolean('ajax')) {
         header('Content-Type: text/xml;charset=utf-8');
         $this->xw->startDocument('1.0', 'UTF-8');
         $this->elementStart('html');
         $this->elementStart('head');
         $this->element('title', null, _('Notice posted'));
         $this->elementEnd('head');
         $this->elementStart('body');
         $this->showNotice($notice);
         $this->elementEnd('body');
         $this->elementEnd('html');
     } else {
         $returnto = $this->trimmed('returnto');
         if ($returnto) {
             $url = common_local_url($returnto, array('nickname' => $user->nickname));
         } else {
             $url = common_local_url('shownotice', array('notice' => $notice->id));
         }
         common_redirect($url, 303);
     }
 }
Esempio n. 14
0
 function add_notice(&$user, &$pl)
 {
     $body = trim($pl['body']);
     $content_shortened = $user->shortenLinks($body);
     if (Notice::contentTooLong($content_shortened)) {
         $from = jabber_normalize_jid($pl['from']);
         // TRANS: Response to XMPP source when it sent too long a message.
         // TRANS: %1$d the maximum number of allowed characters (used for plural), %2$d is the sent number.
         $this->from_site($from, sprintf(_m('Message too long. Maximum is %1$d character, you sent %2$d.', 'Message too long. Maximum is %1$d characters, you sent %2$d.', Notice::maxContent()), Notice::maxContent(), mb_strlen($content_shortened)));
         return;
     }
     try {
         $notice = Notice::saveNew($user->id, $content_shortened, 'xmpp');
     } catch (Exception $e) {
         $this->log(LOG_ERR, $e->getMessage());
         $this->from_site($user->jabber, $e->getMessage());
         return;
     }
     common_broadcast_notice($notice);
     $this->log(LOG_INFO, 'Added notice ' . $notice->id . ' from user ' . $user->nickname);
     $notice->free();
     unset($notice);
 }
Esempio n. 15
0
 /**
  * Register a new user account and profile and set up default subscriptions.
  * If a new-user welcome message is configured, this will be sent.
  *
  * @param array $fields associative array of optional properties
  *              string 'bio'
  *              string 'email'
  *              bool 'email_confirmed' pass true to mark email as pre-confirmed
  *              string 'fullname'
  *              string 'homepage'
  *              string 'location' informal string description of geolocation
  *              float 'lat' decimal latitude for geolocation
  *              float 'lon' decimal longitude for geolocation
  *              int 'location_id' geoname identifier
  *              int 'location_ns' geoname namespace to interpret location_id
  *              string 'nickname' REQUIRED
  *              string 'password' (may be missing for eg OpenID registrations)
  *              string 'code' invite code
  *              ?string 'uri' permalink to notice; defaults to local notice URL
  * @return mixed User object or false on failure
  */
 static function register($fields)
 {
     // MAGICALLY put fields into current scope
     extract($fields);
     $profile = new Profile();
     if (!empty($email)) {
         $email = common_canonical_email($email);
     }
     $nickname = common_canonical_nickname($nickname);
     $profile->nickname = $nickname;
     if (!User::allowed_nickname($nickname)) {
         common_log(LOG_WARNING, sprintf("Attempted to register a nickname that is not allowed: %s", $profile->nickname), __FILE__);
         return false;
     }
     $profile->profileurl = common_profile_url($nickname);
     if (!empty($fullname)) {
         $profile->fullname = $fullname;
     }
     if (!empty($homepage)) {
         $profile->homepage = $homepage;
     }
     if (!empty($bio)) {
         $profile->bio = $bio;
     }
     if (!empty($location)) {
         $profile->location = $location;
         $loc = Location::fromName($location);
         if (!empty($loc)) {
             $profile->lat = $loc->lat;
             $profile->lon = $loc->lon;
             $profile->location_id = $loc->location_id;
             $profile->location_ns = $loc->location_ns;
         }
     }
     $profile->created = common_sql_now();
     $user = new User();
     $user->nickname = $nickname;
     // Users who respond to invite email have proven their ownership of that address
     if (!empty($code)) {
         $invite = Invitation::staticGet($code);
         if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
             $user->email = $invite->address;
         }
     }
     if (isset($email_confirmed) && $email_confirmed) {
         $user->email = $email;
     }
     // This flag is ignored but still set to 1
     $user->inboxed = 1;
     // Set default-on options here, otherwise they'll be disabled
     // initially for sites using caching, since the initial encache
     // doesn't know about the defaults in the database.
     $user->emailnotifysub = 1;
     $user->emailnotifyfav = 1;
     $user->emailnotifynudge = 1;
     $user->emailnotifymsg = 1;
     $user->emailnotifyattn = 1;
     $user->emailmicroid = 1;
     $user->emailpost = 1;
     $user->jabbermicroid = 1;
     $user->viewdesigns = 1;
     $user->created = common_sql_now();
     if (Event::handle('StartUserRegister', array(&$user, &$profile))) {
         $profile->query('BEGIN');
         $id = $profile->insert();
         if (empty($id)) {
             common_log_db_error($profile, 'INSERT', __FILE__);
             return false;
         }
         $user->id = $id;
         if (!empty($uri)) {
             $user->uri = $uri;
         } else {
             $user->uri = common_user_uri($user);
         }
         if (!empty($password)) {
             // may not have a password for OpenID users
             $user->password = common_munge_password($password, $id);
         }
         $result = $user->insert();
         if (!$result) {
             common_log_db_error($user, 'INSERT', __FILE__);
             return false;
         }
         // Everyone gets an inbox
         $inbox = new Inbox();
         $inbox->user_id = $user->id;
         $inbox->notice_ids = '';
         $result = $inbox->insert();
         if (!$result) {
             common_log_db_error($inbox, 'INSERT', __FILE__);
             return false;
         }
         // Everyone is subscribed to themself
         $subscription = new Subscription();
         $subscription->subscriber = $user->id;
         $subscription->subscribed = $user->id;
         $subscription->created = $user->created;
         $result = $subscription->insert();
         if (!$result) {
             common_log_db_error($subscription, 'INSERT', __FILE__);
             return false;
         }
         if (!empty($email) && !$user->email) {
             $confirm = new Confirm_address();
             $confirm->code = common_confirmation_code(128);
             $confirm->user_id = $user->id;
             $confirm->address = $email;
             $confirm->address_type = 'email';
             $result = $confirm->insert();
             if (!$result) {
                 common_log_db_error($confirm, 'INSERT', __FILE__);
                 return false;
             }
         }
         if (!empty($code) && $user->email) {
             $user->emailChanged();
         }
         // Default system subscription
         $defnick = common_config('newuser', 'default');
         if (!empty($defnick)) {
             $defuser = User::staticGet('nickname', $defnick);
             if (empty($defuser)) {
                 common_log(LOG_WARNING, sprintf("Default user %s does not exist.", $defnick), __FILE__);
             } else {
                 Subscription::start($user, $defuser);
             }
         }
         $profile->query('COMMIT');
         if (!empty($email) && !$user->email) {
             mail_confirm_address($user, $confirm->code, $profile->nickname, $email);
         }
         // Welcome message
         $welcome = common_config('newuser', 'welcome');
         if (!empty($welcome)) {
             $welcomeuser = User::staticGet('nickname', $welcome);
             if (empty($welcomeuser)) {
                 common_log(LOG_WARNING, sprintf("Welcome user %s does not exist.", $defnick), __FILE__);
             } else {
                 $notice = Notice::saveNew($welcomeuser->id, sprintf(_('Welcome to %1$s, @%2$s!'), common_config('site', 'name'), $user->nickname), 'system');
             }
         }
         Event::handle('EndUserRegister', array(&$profile, &$user));
     }
     return $user;
 }
Esempio n. 16
0
 /**
  * Process an incoming post activity from this remote feed.
  * @param Activity $activity
  * @param string $method 'push' or 'salmon'
  * @return mixed saved Notice or false
  * @todo FIXME: Break up this function, it's getting nasty long
  */
 public function processPost($activity, $method)
 {
     $notice = null;
     $oprofile = $this->checkAuthorship($activity);
     if (empty($oprofile)) {
         return null;
     }
     // It's not always an ActivityObject::NOTE, but... let's just say it is.
     $note = $activity->objects[0];
     // The id URI will be used as a unique identifier for for the notice,
     // protecting against duplicate saves. It isn't required to be a URL;
     // tag: URIs for instance are found in Google Buzz feeds.
     $sourceUri = $note->id;
     $dupe = Notice::staticGet('uri', $sourceUri);
     if ($dupe) {
         common_log(LOG_INFO, "OStatus: ignoring duplicate post: {$sourceUri}");
         return $dupe;
     }
     // We'll also want to save a web link to the original notice, if provided.
     $sourceUrl = null;
     if ($note->link) {
         $sourceUrl = $note->link;
     } else {
         if ($activity->link) {
             $sourceUrl = $activity->link;
         } else {
             if (preg_match('!^https?://!', $note->id)) {
                 $sourceUrl = $note->id;
             }
         }
     }
     // Use summary as fallback for content
     if (!empty($note->content)) {
         $sourceContent = $note->content;
     } else {
         if (!empty($note->summary)) {
             $sourceContent = $note->summary;
         } else {
             if (!empty($note->title)) {
                 $sourceContent = $note->title;
             } else {
                 // @todo FIXME: Fetch from $sourceUrl?
                 // TRANS: Client exception. %s is a source URI.
                 throw new ClientException(sprintf(_m('No content for notice %s.'), $sourceUri));
             }
         }
     }
     // Get (safe!) HTML and text versions of the content
     $rendered = $this->purify($sourceContent);
     $content = html_entity_decode(strip_tags($rendered), ENT_QUOTES, 'UTF-8');
     $shortened = common_shorten_links($content);
     // If it's too long, try using the summary, and make the
     // HTML an attachment.
     $attachment = null;
     if (Notice::contentTooLong($shortened)) {
         $attachment = $this->saveHTMLFile($note->title, $rendered);
         $summary = html_entity_decode(strip_tags($note->summary), ENT_QUOTES, 'UTF-8');
         if (empty($summary)) {
             $summary = $content;
         }
         $shortSummary = common_shorten_links($summary);
         if (Notice::contentTooLong($shortSummary)) {
             $url = common_shorten_url($sourceUrl);
             $shortSummary = substr($shortSummary, 0, Notice::maxContent() - (mb_strlen($url) + 2));
             $content = $shortSummary . ' ' . $url;
             // We mark up the attachment link specially for the HTML output
             // so we can fold-out the full version inline.
             // @todo FIXME i18n: This tooltip will be saved with the site's default language
             // TRANS: Shown when a notice is longer than supported and/or when attachments are present. At runtime
             // TRANS: this will usually be replaced with localised text from StatusNet core messages.
             $showMoreText = _m('Show more');
             $attachUrl = common_local_url('attachment', array('attachment' => $attachment->id));
             $rendered = common_render_text($shortSummary) . '<a href="' . htmlspecialchars($attachUrl) . '"' . ' class="attachment more"' . ' title="' . htmlspecialchars($showMoreText) . '">' . '&#8230;' . '</a>';
         }
     }
     $options = array('is_local' => Notice::REMOTE, 'url' => $sourceUrl, 'uri' => $sourceUri, 'rendered' => $rendered, 'replies' => array(), 'groups' => array(), 'peopletags' => array(), 'tags' => array(), 'urls' => array());
     // Check for optional attributes...
     if (!empty($activity->time)) {
         $options['created'] = common_sql_date($activity->time);
     }
     if ($activity->context) {
         // Any individual or group attn: targets?
         $replies = $activity->context->attention;
         $options['groups'] = $this->filterReplies($oprofile, $replies);
         $options['replies'] = $replies;
         // Maintain direct reply associations
         // @todo FIXME: What about conversation ID?
         if (!empty($activity->context->replyToID)) {
             $orig = Notice::staticGet('uri', $activity->context->replyToID);
             if (!empty($orig)) {
                 $options['reply_to'] = $orig->id;
             }
         }
         $location = $activity->context->location;
         if ($location) {
             $options['lat'] = $location->lat;
             $options['lon'] = $location->lon;
             if ($location->location_id) {
                 $options['location_ns'] = $location->location_ns;
                 $options['location_id'] = $location->location_id;
             }
         }
     }
     if ($this->isPeopletag()) {
         $options['peopletags'][] = $this->localPeopletag();
     }
     // Atom categories <-> hashtags
     foreach ($activity->categories as $cat) {
         if ($cat->term) {
             $term = common_canonical_tag($cat->term);
             if ($term) {
                 $options['tags'][] = $term;
             }
         }
     }
     // Atom enclosures -> attachment URLs
     foreach ($activity->enclosures as $href) {
         // @todo FIXME: Save these locally or....?
         $options['urls'][] = $href;
     }
     try {
         $saved = Notice::saveNew($oprofile->profile_id, $content, 'ostatus', $options);
         if ($saved) {
             Ostatus_source::saveNew($saved, $this, $method);
             if (!empty($attachment)) {
                 File_to_post::processNew($attachment->id, $saved->id);
             }
         }
     } catch (Exception $e) {
         common_log(LOG_ERR, "OStatus save of remote message {$sourceUri} failed: " . $e->getMessage());
         throw $e;
     }
     common_log(LOG_INFO, "OStatus saved remote message {$sourceUri} as notice id {$saved->id}");
     return $saved;
 }
Esempio n. 17
0
 function saveStatus($status)
 {
     $profile = $this->ensureProfile($status->user);
     if (empty($profile)) {
         common_log(LOG_ERR, $this->name() . ' - Problem saving notice. No associated Profile.');
         return null;
     }
     $statusId = twitter_id($status);
     $statusUri = $this->makeStatusURI($status->user->screen_name, $statusId);
     // check to see if we've already imported the status
     $n2s = Notice_to_status::staticGet('status_id', $statusId);
     if (!empty($n2s)) {
         common_log(LOG_INFO, $this->name() . " - Ignoring duplicate import: {$statusId}");
         return Notice::staticGet('id', $n2s->notice_id);
     }
     // If it's a retweet, save it as a repeat!
     if (!empty($status->retweeted_status)) {
         common_log(LOG_INFO, "Status {$statusId} is a retweet of " . twitter_id($status->retweeted_status) . ".");
         $original = $this->saveStatus($status->retweeted_status);
         if (empty($original)) {
             return null;
         } else {
             $author = $original->getProfile();
             // TRANS: Message used to repeat a notice. RT is the abbreviation of 'retweet'.
             // TRANS: %1$s is the repeated user's name, %2$s is the repeated notice.
             $content = sprintf(_m('RT @%1$s %2$s'), $author->nickname, $original->content);
             if (Notice::contentTooLong($content)) {
                 $contentlimit = Notice::maxContent();
                 $content = mb_substr($content, 0, $contentlimit - 4) . ' ...';
             }
             $repeat = Notice::saveNew($profile->id, $content, 'twitter', array('repeat_of' => $original->id, 'uri' => $statusUri, 'is_local' => Notice::GATEWAY));
             common_log(LOG_INFO, "Saved {$repeat->id} as a repeat of {$original->id}");
             Notice_to_status::saveNew($repeat->id, $statusId);
             return $repeat;
         }
     }
     $notice = new Notice();
     $notice->profile_id = $profile->id;
     $notice->uri = $statusUri;
     $notice->url = $statusUri;
     $notice->created = strftime('%Y-%m-%d %H:%M:%S', strtotime($status->created_at));
     $notice->source = 'twitter';
     $notice->reply_to = null;
     $replyTo = twitter_id($status, 'in_reply_to_status_id');
     if (!empty($replyTo)) {
         common_log(LOG_INFO, "Status {$statusId} is a reply to status {$replyTo}");
         $n2s = Notice_to_status::staticGet('status_id', $replyTo);
         if (empty($n2s)) {
             common_log(LOG_INFO, "Couldn't find local notice for status {$replyTo}");
         } else {
             $reply = Notice::staticGet('id', $n2s->notice_id);
             if (empty($reply)) {
                 common_log(LOG_INFO, "Couldn't find local notice for status {$replyTo}");
             } else {
                 common_log(LOG_INFO, "Found local notice {$reply->id} for status {$replyTo}");
                 $notice->reply_to = $reply->id;
                 $notice->conversation = $reply->conversation;
             }
         }
     }
     if (empty($notice->conversation)) {
         $conv = Conversation::create();
         $notice->conversation = $conv->id;
         common_log(LOG_INFO, "No known conversation for status {$statusId} so making a new one {$conv->id}.");
     }
     $notice->is_local = Notice::GATEWAY;
     $notice->content = html_entity_decode($status->text, ENT_QUOTES, 'UTF-8');
     $notice->rendered = $this->linkify($status);
     if (Event::handle('StartNoticeSave', array(&$notice))) {
         $id = $notice->insert();
         if (!$id) {
             common_log_db_error($notice, 'INSERT', __FILE__);
             common_log(LOG_ERR, $this->name() . ' - Problem saving notice.');
         }
         Event::handle('EndNoticeSave', array($notice));
     }
     Notice_to_status::saveNew($notice->id, $statusId);
     $this->saveStatusMentions($notice, $status);
     $this->saveStatusAttachments($notice, $status);
     $notice->blowOnInsert();
     return $notice;
 }
Esempio n. 18
0
 function add_notice(&$user, &$pl)
 {
     $body = trim($pl['body']);
     $content_shortened = common_shorten_links($body);
     if (Notice::contentTooLong($content_shortened)) {
         $from = jabber_normalize_jid($pl['from']);
         $this->from_site($from, sprintf(_('Message too long - maximum is %1$d characters, you sent %2$d.'), Notice::maxContent(), mb_strlen($content_shortened)));
         return;
     }
     try {
         $notice = Notice::saveNew($user->id, $content_shortened, 'xmpp');
     } catch (Exception $e) {
         $this->log(LOG_ERR, $e->getMessage());
         $this->from_site($user->jabber, $e->getMessage());
         return;
     }
     common_broadcast_notice($notice);
     $this->log(LOG_INFO, 'Added notice ' . $notice->id . ' from user ' . $user->nickname);
     $notice->free();
     unset($notice);
 }
Esempio n. 19
0
 /**
  * Helper for handling incoming messages
  * Your incoming message handler will probably want to call this function
  *
  * @param string $from screenname the message was sent from
  * @param string $message message contents
  *
  * @param boolean success
  */
 protected function addNotice($screenname, $user, $body)
 {
     $body = trim(strip_tags($body));
     $content_shortened = common_shorten_links($body);
     if (Notice::contentTooLong($content_shortened)) {
         $this->sendFromSite($screenname, sprintf(_m('Message too long - maximum is %1$d character, you sent %2$d.', 'Message too long - maximum is %1$d characters, you sent %2$d.', Notice::maxContent()), Notice::maxContent(), mb_strlen($content_shortened)));
         return;
     }
     try {
         $notice = Notice::saveNew($user->id, $content_shortened, $this->transport);
     } catch (Exception $e) {
         common_log(LOG_ERR, $e->getMessage());
         $this->sendFromSite($from, $e->getMessage());
         return;
     }
     common_log(LOG_INFO, 'Added notice ' . $notice->id . ' from user ' . $user->nickname);
     $notice->free();
     unset($notice);
 }
 /**
  * Handle the request
  *
  * Make a new notice for the update, save it, and show it
  *
  * @param array $args $_REQUEST data (unused)
  *
  * @return void
  */
 function handle($args)
 {
     parent::handle($args);
     if ($_SERVER['REQUEST_METHOD'] != 'POST') {
         $this->clientError(_('This method requires a POST.'), 400, $this->format);
         return;
     }
     // Workaround for PHP returning empty $_POST and $_FILES when POST
     // length > post_max_size in php.ini
     if (empty($_FILES) && empty($_POST) && $_SERVER['CONTENT_LENGTH'] > 0) {
         $msg = _('The server was unable to handle that much POST ' . 'data (%s bytes) due to its current configuration.');
         $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
         return;
     }
     if (empty($this->status)) {
         $this->clientError('Client must provide a \'status\' parameter with a value.', 400, $this->format);
         return;
     }
     if (empty($this->auth_user)) {
         $this->clientError(_('No such user.'), 404, $this->format);
         return;
     }
     $status_shortened = common_shorten_links($this->status);
     if (Notice::contentTooLong($status_shortened)) {
         // Note: Twitter truncates anything over 140, flags the status
         // as "truncated."
         $this->clientError(sprintf(_('That\'s too long. Max notice size is %d chars.'), Notice::maxContent()), 406, $this->format);
         return;
     }
     // Check for commands
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($this->auth_user, $status_shortened);
     if ($cmd) {
         if ($this->supported($cmd)) {
             $cmd->execute(new Channel());
         }
         // Cmd not supported?  Twitter just returns your latest status.
         // And, it returns your last status whether the cmd was successful
         // or not!
         $this->notice = $this->auth_user->getCurrentNotice();
     } else {
         $reply_to = null;
         if (!empty($this->in_reply_to_status_id)) {
             // Check whether notice actually exists
             $reply = Notice::staticGet($this->in_reply_to_status_id);
             if ($reply) {
                 $reply_to = $this->in_reply_to_status_id;
             } else {
                 $this->clientError(_('Not found.'), $code = 404, $this->format);
                 return;
             }
         }
         $upload = null;
         try {
             $upload = MediaFile::fromUpload('media', $this->auth_user);
         } catch (ClientException $ce) {
             $this->clientError($ce->getMessage());
             return;
         }
         if (isset($upload)) {
             $status_shortened .= ' ' . $upload->shortUrl();
             if (Notice::contentTooLong($status_shortened)) {
                 $upload->delete();
                 $msg = _('Max notice size is %d chars, ' . 'including attachment URL.');
                 $this->clientError(sprintf($msg, Notice::maxContent()));
             }
         }
         $content = html_entity_decode($status_shortened, ENT_NOQUOTES, 'UTF-8');
         $options = array('reply_to' => $reply_to);
         if ($this->auth_user->shareLocation()) {
             $locOptions = Notice::locationOptions($this->lat, $this->lon, null, null, $this->auth_user->getProfile());
             $options = array_merge($options, $locOptions);
         }
         try {
             $this->notice = Notice::saveNew($this->auth_user->id, $content, $this->source, $options);
         } catch (Exception $e) {
             $this->clientError($e->getMessage());
             return;
         }
         if (isset($upload)) {
             $upload->attachToNotice($this->notice);
         }
     }
     $this->showNotice();
 }
 /**
  * Handle the request
  *
  * Make a new notice for the update, save it, and show it
  *
  * @return void
  */
 protected function handle()
 {
     parent::handle();
     // Workaround for PHP returning empty $_POST and $_FILES when POST
     // length > post_max_size in php.ini
     if (empty($_FILES) && empty($_POST) && $_SERVER['CONTENT_LENGTH'] > 0) {
         // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
         // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
         $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.', 'The server was unable to handle that much POST data (%s bytes) due to its current configuration.', intval($_SERVER['CONTENT_LENGTH']));
         $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
     }
     if (empty($this->status)) {
         // TRANS: Client error displayed when the parameter "status" is missing.
         $this->clientError(_('Client must provide a \'status\' parameter with a value.'));
     }
     if (is_null($this->scoped)) {
         // TRANS: Client error displayed when updating a status for a non-existing user.
         $this->clientError(_('No such user.'), 404);
     }
     /* Do not call shortenLinks until the whole notice has been build */
     // Check for commands
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($this->auth_user, $this->status);
     if ($cmd) {
         if ($this->supported($cmd)) {
             $cmd->execute(new Channel());
         }
         // Cmd not supported?  Twitter just returns your latest status.
         // And, it returns your last status whether the cmd was successful
         // or not!
         $this->notice = $this->auth_user->getCurrentNotice();
     } else {
         $reply_to = null;
         if (!empty($this->in_reply_to_status_id)) {
             // Check whether notice actually exists
             $reply = Notice::getKV($this->in_reply_to_status_id);
             if ($reply) {
                 $reply_to = $this->in_reply_to_status_id;
             } else {
                 // TRANS: Client error displayed when replying to a non-existing notice.
                 $this->clientError(_('Parent notice not found.'), 404);
             }
         }
         $upload = null;
         try {
             $upload = MediaFile::fromUpload('media', $this->scoped);
             $this->status .= ' ' . $upload->shortUrl();
             /* Do not call shortenLinks until the whole notice has been build */
         } catch (NoUploadedMediaException $e) {
             // There was no uploaded media for us today.
         }
         /* Do call shortenlinks here & check notice length since notice is about to be saved & sent */
         $status_shortened = $this->auth_user->shortenLinks($this->status);
         if (Notice::contentTooLong($status_shortened)) {
             if ($upload instanceof MediaFile) {
                 $upload->delete();
             }
             // TRANS: Client error displayed exceeding the maximum notice length.
             // TRANS: %d is the maximum lenth for a notice.
             $msg = _m('Maximum notice size is %d character, including attachment URL.', 'Maximum notice size is %d characters, including attachment URL.', Notice::maxContent());
             /* Use HTTP 413 error code (Request Entity Too Large)
              * instead of basic 400 for better understanding
              */
             $this->clientError(sprintf($msg, Notice::maxContent()), 413);
         }
         $content = html_entity_decode($status_shortened, ENT_NOQUOTES, 'UTF-8');
         $options = array('reply_to' => $reply_to);
         if ($this->scoped->shareLocation()) {
             $locOptions = Notice::locationOptions($this->lat, $this->lon, null, null, $this->scoped);
             $options = array_merge($options, $locOptions);
         }
         try {
             $this->notice = Notice::saveNew($this->scoped->id, $content, $this->source, $options);
         } catch (Exception $e) {
             $this->clientError($e->getMessage(), $e->getCode());
         }
         if (isset($upload)) {
             $upload->attachToNotice($this->notice);
         }
     }
     $this->showNotice();
 }
Esempio n. 22
0
 function handle($channel)
 {
     $notice = $this->getNotice($this->other);
     $recipient = $notice->getProfile();
     $len = mb_strlen($this->text);
     if ($len == 0) {
         // TRANS: Command exception text shown when trying to reply to a notice without providing content for the reply.
         $channel->error($this->user, _('No content!'));
         return;
     }
     $this->text = common_shorten_links($this->text);
     if (Notice::contentTooLong($this->text)) {
         // XXX: i18n. Needs plural support.
         // TRANS: Message given if content of a notice for a reply is too long.
         // TRANS: %1$d is the maximum number of characters, %2$d is the number of submitted characters.
         $channel->error($this->user, sprintf(_('Notice too long - maximum is %1$d characters, you sent %2$d.'), Notice::maxContent(), mb_strlen($this->text)));
         return;
     }
     $notice = Notice::saveNew($this->user->id, $this->text, $channel->source(), array('reply_to' => $notice->id));
     if ($notice) {
         // TRANS: Text shown having sent a reply to a notice successfully.
         // TRANS: %s is the nickname of the user of the notice the reply was sent to.
         $channel->output($this->user, sprintf(_('Reply to %s sent.'), $recipient->nickname));
     } else {
         // TRANS: Error text shown when a reply to a notice fails with an unknown reason.
         $channel->error($this->user, _('Error saving notice.'));
     }
 }
Esempio n. 23
0
 /**
  * Delete any notifications tied to deleted notices and un-repeats
  *
  * @return boolean hook flag
  */
 public function onNoticeDeleteRelated($notice)
 {
     $notif = new QvitterNotification();
     // unrepeats
     if ($notice->isRepeat()) {
         $repeated_notice = Notice::getKV('id', $notice->repeat_of);
         $notif->notice_id = $repeated_notice->id;
         $notif->from_profile_id = $notice->profile_id;
     } else {
         $notif->notice_id = $notice->id;
     }
     $notif->delete();
     // outputs an activity notice that this notice was deleted
     $profile = $notice->getProfile();
     // don't delete if this is a user is being deleted
     // because that creates an infinite loop of deleting and creating notices...
     $user_is_deleted = false;
     $user = User::getKV('id', $profile->id);
     if ($user instanceof User && $user->hasRole(Profile_role::DELETED)) {
         $user_is_deleted = true;
     }
     if (!$user_is_deleted && class_exists('StatusNet') && !array_key_exists('ActivityModeration', StatusNet::getActivePlugins())) {
         $rendered = sprintf(_m('<a href="%1$s">%2$s</a> deleted notice <a href="%3$s">{{%4$s}}</a>.'), htmlspecialchars($profile->getUrl()), htmlspecialchars($profile->getBestName()), htmlspecialchars($notice->getUrl()), htmlspecialchars($notice->uri));
         $text = sprintf(_m('%1$s deleted notice {{%2$s}}.'), $profile->getBestName(), $notice->uri);
         $uri = TagURI::mint('delete-notice:%d:%d:%s', $notice->profile_id, $notice->id, common_date_iso8601(common_sql_now()));
         $notice = Notice::saveNew($notice->profile_id, $text, ActivityPlugin::SOURCE, array('rendered' => $rendered, 'urls' => array(), 'uri' => $uri, 'verb' => 'qvitter-delete-notice', 'object_type' => ActivityObject::ACTIVITY));
     }
     return true;
 }
Esempio n. 24
0
 function onEndLeaveGroup($group, $profile)
 {
     // Only do this if config is enabled
     if (!$this->LeaveGroup) {
         return true;
     }
     if (!$profile->isLocal()) {
         return true;
     }
     // TRANS: Text for "left group" item in activity plugin.
     // TRANS: %1$s is a profile URL, %2$s is a profile name,
     // TRANS: %3$s is a group URL, %4$s is a group name.
     $rendered = sprintf(_m('<a href="%1$s">%2$s</a> left the group <a href="%3$s">%4$s</a>.'), $profile->getUrl(), $profile->getBestName(), $group->homeUrl(), $group->getBestName());
     // TRANS: Text for "left group" item in activity plugin.
     // TRANS: %1$s is a profile name, %2$s is a profile URL,
     // TRANS: %3$s is a group name, %4$s is a group URL.
     $content = sprintf(_m('%1$s (%2$s) left the group %3$s (%4$s).'), $profile->getBestName(), $profile->getUrl(), $group->getBestName(), $group->homeUrl());
     $uri = TagURI::mint('leave:%d:%d:%s', $profile->id, $group->id, common_date_iso8601(common_sql_now()));
     $notice = Notice::saveNew($profile->id, $content, ActivityPlugin::SOURCE, array('rendered' => $rendered, 'urls' => array(), 'groups' => array($group->id), 'uri' => $uri, 'verb' => ActivityVerb::LEAVE, 'object_type' => ActivityObject::GROUP));
     return true;
 }
Esempio n. 25
0
 function add_notice($user, $msg, $mediafiles)
 {
     try {
         $notice = Notice::saveNew($user->id, $msg, 'mail');
     } catch (Exception $e) {
         $this->log(LOG_ERR, $e->getMessage());
         return $e->getMessage();
     }
     foreach ($mediafiles as $mf) {
         $mf->attachToNotice($notice);
     }
     $this->log(LOG_INFO, 'Added notice ' . $notice->id . ' from user ' . $user->nickname);
     return true;
 }
Esempio n. 26
0
 /**
  * Load or create an imported notice from Yammer data.
  *
  * @param object $item loaded JSON data for Yammer importer
  * @return Notice
  */
 function importNotice($item)
 {
     $data = $this->prepNotice($item);
     $noticeId = $this->findImportedNotice($data['orig_id']);
     if ($noticeId) {
         return Notice::staticGet('id', $noticeId);
     } else {
         $notice = Notice::staticGet('uri', $data['options']['uri']);
         $content = $data['content'];
         $user = User::staticGet($data['profile']);
         // Fetch file attachments and add the URLs...
         $uploads = array();
         foreach ($data['attachments'] as $url) {
             try {
                 $upload = $this->saveAttachment($url, $user);
                 $content .= ' ' . $upload->shortUrl();
                 $uploads[] = $upload;
             } catch (Exception $e) {
                 common_log(LOG_ERR, "Error importing Yammer attachment: " . $e->getMessage());
             }
         }
         // Here's the meat! Actually save the dang ol' notice.
         $notice = Notice::saveNew($user->id, $content, $data['source'], $data['options']);
         // Save "likes" as favorites...
         foreach ($data['faves'] as $nickname) {
             $user = User::staticGet('nickname', $nickname);
             if ($user) {
                 Fave::addNew($user->getProfile(), $notice);
             }
         }
         // And finally attach the upload records...
         foreach ($uploads as $upload) {
             $upload->attachToNotice($notice);
         }
         $this->recordImportedNotice($data['orig_id'], $notice->id);
         return $notice;
     }
 }
Esempio n. 27
0
 /**
  * Save a new poll notice
  *
  * @param Profile $profile
  * @param string  $question
  * @param array   $opts (poll responses)
  *
  * @return Notice saved notice
  */
 static function saveNew($profile, $question, $opts, $options = null)
 {
     if (empty($options)) {
         $options = array();
     }
     $p = new Poll();
     $p->id = UUID::gen();
     $p->profile_id = $profile->id;
     $p->question = $question;
     $p->options = implode("\n", $opts);
     if (array_key_exists('created', $options)) {
         $p->created = $options['created'];
     } else {
         $p->created = common_sql_now();
     }
     if (array_key_exists('uri', $options)) {
         $p->uri = $options['uri'];
     } else {
         $p->uri = common_local_url('showpoll', array('id' => $p->id));
     }
     common_log(LOG_DEBUG, "Saving poll: {$p->id} {$p->uri}");
     $p->insert();
     // TRANS: Notice content creating a poll.
     // TRANS: %1$s is the poll question, %2$s is a link to the poll.
     $content = sprintf(_m('Poll: %1$s %2$s'), $question, $p->uri);
     $link = '<a href="' . htmlspecialchars($p->uri) . '">' . htmlspecialchars($question) . '</a>';
     // TRANS: Rendered version of the notice content creating a poll.
     // TRANS: %s is a link to the poll with the question as link description.
     $rendered = sprintf(_m('Poll: %s'), $link);
     $tags = array('poll');
     $replies = array();
     $options = array_merge(array('urls' => array(), 'rendered' => $rendered, 'tags' => $tags, 'replies' => $replies, 'object_type' => PollPlugin::POLL_OBJECT), $options);
     if (!array_key_exists('uri', $options)) {
         $options['uri'] = $p->uri;
     }
     $saved = Notice::saveNew($profile->id, $content, array_key_exists('source', $options) ? $options['source'] : 'web', $options);
     return $saved;
 }
Esempio n. 28
0
 /**
  * Save passed notice
  *
  * Stores the OMB notice $notice. The datastore may change the passed notice.
  * This might by neccessary for URIs depending on a database key. Note that
  * it is the user’s duty to present a mechanism for his OMB_Datastore to
  * appropriately change his OMB_Notice.
  * Throws exceptions in case of error.
  *
  * @param OMB_Notice $notice The OMB notice which should be saved
  *
  * @access public
  **/
 public function saveNotice(&$omb_notice)
 {
     if (Notice::staticGet('uri', $omb_notice->getIdentifierURI())) {
         throw new Exception(_('Duplicate notice'));
     }
     $author_uri = $omb_notice->getAuthor()->getIdentifierURI();
     common_log(LOG_DEBUG, $author_uri, __FILE__);
     $author = Remote_profile::staticGet('uri', $author_uri);
     if (!$author) {
         $author = User::staticGet('uri', $author_uri);
     }
     if (!$author) {
         throw new Exception('用户不存在');
     }
     common_log(LOG_DEBUG, print_r($author, true), __FILE__);
     $notice = Notice::saveNew($author->id, $omb_notice->getContent(), 'omb', array('is_local' => Notice::REMOTE_OMB, 'uri' => $omb_notice->getIdentifierURI()));
 }
Esempio n. 29
0
 /**
  * Save a new notice bookmark
  *
  * @param Profile $profile     To save the bookmark for
  * @param string  $title       Title of the bookmark
  * @param string  $url         URL of the bookmark
  * @param mixed   $rawtags     array of tags or string
  * @param string  $description Description of the bookmark
  * @param array   $options     Options for the Notice::saveNew()
  *
  * @return Notice saved notice
  */
 static function saveNew($profile, $title, $url, $rawtags, $description, $options = null)
 {
     if (!common_valid_http_url($url)) {
         throw new ClientException(_m('Only web bookmarks can be posted (HTTP or HTTPS).'));
     }
     $nb = self::getByURL($profile, $url);
     if (!empty($nb)) {
         // TRANS: Client exception thrown when trying to save a new bookmark that already exists.
         throw new ClientException(_m('Bookmark already exists.'));
     }
     if (empty($options)) {
         $options = array();
     }
     if (array_key_exists('uri', $options)) {
         $other = Bookmark::getKV('uri', $options['uri']);
         if (!empty($other)) {
             // TRANS: Client exception thrown when trying to save a new bookmark that already exists.
             throw new ClientException(_m('Bookmark already exists.'));
         }
     }
     if (is_string($rawtags)) {
         if (empty($rawtags)) {
             $rawtags = array();
         } else {
             $rawtags = preg_split('/[\\s,]+/', $rawtags);
         }
     }
     $nb = new Bookmark();
     $nb->id = UUID::gen();
     $nb->profile_id = $profile->id;
     $nb->url = $url;
     $nb->title = $title;
     $nb->description = $description;
     if (array_key_exists('created', $options)) {
         $nb->created = $options['created'];
     } else {
         $nb->created = common_sql_now();
     }
     if (array_key_exists('uri', $options)) {
         $nb->uri = $options['uri'];
     } else {
         // FIXME: hacks to work around router bugs in
         // queue daemons
         $r = Router::get();
         $path = $r->build('showbookmark', array('id' => $nb->id));
         if (empty($path)) {
             $nb->uri = common_path('bookmark/' . $nb->id, false, false);
         } else {
             $nb->uri = common_local_url('showbookmark', array('id' => $nb->id), null, null, false);
         }
     }
     $nb->insert();
     $tags = array();
     $replies = array();
     // filter "for:nickname" tags
     foreach ($rawtags as $tag) {
         if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
             // skip if done by caller
             if (!array_key_exists('replies', $options)) {
                 $nickname = mb_substr($tag, 4);
                 $other = common_relative_profile($profile, $nickname);
                 if (!empty($other)) {
                     $replies[] = $other->getUri();
                 }
             }
         } else {
             $tags[] = common_canonical_tag($tag);
         }
     }
     $hashtags = array();
     $taglinks = array();
     foreach ($tags as $tag) {
         $hashtags[] = '#' . $tag;
         $attrs = array('href' => Notice_tag::url($tag), 'rel' => $tag, 'class' => 'tag');
         $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
     }
     // Use user's preferences for short URLs, if possible
     try {
         $user = User::getKV('id', $profile->id);
         $shortUrl = File_redirection::makeShort($url, empty($user) ? null : $user);
     } catch (Exception $e) {
         // Don't let this stop us.
         $shortUrl = $url;
     }
     // TRANS: Bookmark content.
     // TRANS: %1$s is a title, %2$s is a short URL, %3$s is the bookmark description,
     // TRANS: %4$s is space separated list of hash tags.
     $content = sprintf(_m('"%1$s" %2$s %3$s %4$s'), $title, $shortUrl, $description, implode(' ', $hashtags));
     // TRANS: Rendered bookmark content.
     // TRANS: %1$s is a URL, %2$s the bookmark title, %3$s is the bookmark description,
     // TRANS: %4$s is space separated list of hash tags.
     $rendered = sprintf(_m('<span class="xfolkentry">' . '<a class="taggedlink" href="%1$s">%2$s</a> ' . '<span class="description">%3$s</span> ' . '<span class="meta">%4$s</span>' . '</span>'), htmlspecialchars($url), htmlspecialchars($title), htmlspecialchars($description), implode(' ', $taglinks));
     $options = array_merge(array('urls' => array($url), 'rendered' => $rendered, 'tags' => $tags, 'replies' => $replies, 'object_type' => ActivityObject::BOOKMARK), $options);
     if (!array_key_exists('uri', $options)) {
         $options['uri'] = $nb->uri;
     }
     try {
         $saved = Notice::saveNew($profile->id, $content, array_key_exists('source', $options) ? $options['source'] : 'web', $options);
     } catch (Exception $e) {
         $nb->delete();
         throw $e;
     }
     if (empty($saved)) {
         $nb->delete();
     }
     return $saved;
 }
Esempio n. 30
0
 /**
  * Save a new poll notice
  *
  * @param Profile $profile
  * @param Poll    $poll the poll being responded to
  * @param int     $selection (1-based)
  * @param array   $opts (poll responses)
  *
  * @return Notice saved notice
  */
 static function saveNew($profile, $poll, $selection, $options = null)
 {
     if (empty($options)) {
         $options = array();
     }
     if (!$poll->isValidSelection($selection)) {
         // TRANS: Client exception thrown when responding to a poll with an invalid option.
         throw new ClientException(_m('Invalid poll selection.'));
     }
     $opts = $poll->getOptions();
     $answer = $opts[$selection - 1];
     $pr = new Poll_response();
     $pr->id = UUID::gen();
     $pr->profile_id = $profile->id;
     $pr->poll_id = $poll->id;
     $pr->selection = $selection;
     if (array_key_exists('created', $options)) {
         $pr->created = $options['created'];
     } else {
         $pr->created = common_sql_now();
     }
     if (array_key_exists('uri', $options)) {
         $pr->uri = $options['uri'];
     } else {
         $pr->uri = common_local_url('showpollresponse', array('id' => $pr->id));
     }
     common_log(LOG_DEBUG, "Saving poll response: {$pr->id} {$pr->uri}");
     $pr->insert();
     // TRANS: Notice content voting for a poll.
     // TRANS: %s is the chosen option in the poll.
     $content = sprintf(_m('voted for "%s"'), $answer);
     $link = '<a href="' . htmlspecialchars($poll->uri) . '">' . htmlspecialchars($answer) . '</a>';
     // TRANS: Rendered version of the notice content voting for a poll.
     // TRANS: %s a link to the poll with the chosen option as link description.
     $rendered = sprintf(_m('voted for "%s"'), $link);
     $tags = array();
     $options = array_merge(array('urls' => array(), 'rendered' => $rendered, 'tags' => $tags, 'reply_to' => $poll->getNotice()->id, 'object_type' => PollPlugin::POLL_RESPONSE_OBJECT), $options);
     if (!array_key_exists('uri', $options)) {
         $options['uri'] = $pr->uri;
     }
     $saved = Notice::saveNew($profile->id, $content, array_key_exists('source', $options) ? $options['source'] : 'web', $options);
     return $saved;
 }