Esempio n. 1
0
 function handle_message($rawmessage)
 {
     list($from, $to, $msg, $attachments) = $this->parse_message($rawmessage);
     if (!$from || !$to || !$msg) {
         // TRANS: Error message in incoming mail handler used when an incoming e-mail cannot be processed.
         $this->error(null, _('Could not parse message.'));
     }
     common_log(LOG_INFO, "Mail from {$from} to {$to} with " . count($attachments) . ' attachment(s): ' . substr($msg, 0, 20));
     $user = $this->user_from_header($from);
     if (!$user) {
         // TRANS: Error message in incoming mail handler used when an incoming e-mail is not from a registered user.
         $this->error($from, _('Not a registered user.'));
         return false;
     }
     if (!$this->user_match_to($user, $to)) {
         // TRANS: Error message in incoming mail handler used when an incoming e-mail is not from a user's incoming e-mail address.
         $this->error($from, _('Sorry, that is not your incoming email address.'));
         return false;
     }
     if (!$user->emailpost) {
         // TRANS: Error message in incoming mail handler used when no incoming e-mail is allowed.
         $this->error($from, _('Sorry, no incoming email allowed.'));
         return false;
     }
     $response = $this->handle_command($user, $from, $msg);
     if ($response) {
         return true;
     }
     $msg = $this->cleanup_msg($msg);
     $msg = $user->shortenLinks($msg);
     if (Notice::contentTooLong($msg)) {
         // TRANS: Error message in incoming mail handler used when an incoming e-mail contains too many characters.
         $this->error($from, sprintf(_m('That\'s too long. Maximum notice size is %d character.', 'That\'s too long. Maximum notice size is %d characters.', Notice::maxContent()), Notice::maxContent()));
     }
     $mediafiles = array();
     foreach ($attachments as $attachment) {
         $mf = null;
         try {
             $mf = MediaFile::fromFileHandle($attachment, $user);
         } catch (ClientException $ce) {
             $this->error($from, $ce->getMessage());
         }
         $msg .= ' ' . $mf->shortUrl();
         array_push($mediafiles, $mf);
         fclose($attachment);
     }
     $err = $this->add_notice($user, $msg, $mediafiles);
     if (is_string($err)) {
         $this->error($from, $err);
         return false;
     } else {
         return true;
     }
 }
Esempio n. 2
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;
     }
 }
Esempio n. 3
0
 /**
  * This doPost saves 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
  */
 protected function doPost()
 {
     assert($this->scoped instanceof Profile);
     // XXX: maybe an error instead...
     $user = $this->scoped->getUser();
     $content = $this->trimmed('status_textarea');
     $options = array('source' => 'web');
     Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
     if (empty($content)) {
         // TRANS: Client error displayed trying to send a notice without content.
         $this->clientError(_('No content!'));
     }
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($user, $content);
     if ($cmd) {
         if (GNUsocial::isAjax()) {
             $cmd->execute(new AjaxWebChannel($this));
         } else {
             $cmd->execute(new WebChannel($this));
         }
         return;
     }
     if ($this->int('inreplyto')) {
         // Throws exception if the inreplyto Notice is given but not found.
         $parent = Notice::getByID($this->int('inreplyto'));
     } else {
         $parent = null;
     }
     $act = new Activity();
     $act->verb = ActivityVerb::POST;
     $act->time = time();
     $act->actor = $this->scoped->asActivityObject();
     $upload = null;
     try {
         // throws exception on failure
         $upload = MediaFile::fromUpload('attach', $this->scoped);
         if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content, &$options))) {
             $content .= ' ' . $upload->shortUrl();
         }
         Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content, &$options));
         // We could check content length here if the URL was added, but I'll just let it slide for now...
         $act->enclosures[] = $upload->getEnclosure();
     } catch (NoUploadedMediaException $e) {
         // simply no attached media to the new notice
     }
     // Reject notice if it is too long (without the HTML)
     // This is done after MediaFile::fromUpload etc. just to act the same as the ApiStatusesUpdateAction
     if (Notice::contentTooLong($content)) {
         // TRANS: Client error displayed when the parameter "status" is missing.
         // TRANS: %d is the maximum number of character for a notice.
         throw new ClientException(sprintf(_m('That\'s too long. Maximum notice size is %d character.', 'That\'s too long. Maximum notice size is %d characters.', Notice::maxContent()), Notice::maxContent()));
     }
     $act->context = new ActivityContext();
     if ($parent instanceof Notice) {
         $act->context->replyToID = $parent->getUri();
         $act->context->replyToUrl = $parent->getUrl(true);
         // maybe we don't have to send true here to force a URL?
     }
     if ($this->scoped->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'), $this->scoped);
         } else {
             $locOptions = Notice::locationOptions(null, null, null, null, $this->scoped);
         }
         $act->context->location = Location::fromOptions($locOptions);
     }
     $content = $this->scoped->shortenLinks($content);
     // FIXME: Make sure NoticeTitle plugin gets a change to add the title to our activityobject!
     if (Event::handle('StartNoticeSaveWeb', array($this, $this->scoped, &$content, &$options))) {
         // FIXME: We should be able to get the attentions from common_render_content!
         // and maybe even directly save whether they're local or not!
         $act->context->attention = common_get_attentions($content, $this->scoped, $parent);
         $actobj = new ActivityObject();
         $actobj->type = ActivityObject::NOTE;
         $actobj->content = common_render_content($content, $this->scoped, $parent);
         // Finally add the activity object to our activity
         $act->objects[] = $actobj;
         $this->stored = Notice::saveActivity($act, $this->scoped, $options);
         if ($upload instanceof MediaFile) {
             $upload->attachToNotice($this->stored);
         }
         Event::handle('EndNoticeSaveWeb', array($this, $this->stored));
     }
     Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content, &$options));
     if (!GNUsocial::isAjax()) {
         $url = common_local_url('shownotice', array('notice' => $this->stored->id));
         common_redirect($url, 303);
     }
     return _('Saved the notice!');
 }
 /**
  * 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.'));
     }
 }
 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. 7
0
 function checkNotice()
 {
     $content = common_shorten_links($_POST['omb_notice_content']);
     if (Notice::contentTooLong($content)) {
         // TRANS: Client error displayed if the notice posted has too many characters.
         $this->clientError(_('Invalid notice content.'), 400);
         return false;
     }
     $license = $_POST['omb_notice_license'];
     $site_license = common_config('license', 'url');
     if ($license && !common_compatible_license($license, $site_license)) {
         // TRANS: Exception thrown if a notice's license is not compatible with the StatusNet site license.
         // TRANS: %1$s is the notice license, %2$s is the StatusNet site's license.
         throw new Exception(sprintf(_('Notice license "%1$s" is not ' . 'compatible with site license "%2$s".'), $license, $site_license));
     }
 }
Esempio n. 8
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. 9
0
 /**
  * Save a new notice and push it out to subscribers' inboxes.
  * Poster's permissions are checked before sending.
  *
  * @param int $profile_id Profile ID of the poster
  * @param string $content source message text; links may be shortened
  *                        per current user's preference
  * @param string $source source key ('web', 'api', etc)
  * @param array $options Associative array of optional properties:
  *              string 'created' timestamp of notice; defaults to now
  *              int 'is_local' source/gateway ID, one of:
  *                  Notice::LOCAL_PUBLIC    - Local, ok to appear in public timeline
  *                  Notice::REMOTE          - Sent from a remote service;
  *                                            hide from public timeline but show in
  *                                            local "and friends" timelines
  *                  Notice::LOCAL_NONPUBLIC - Local, but hide from public timeline
  *                  Notice::GATEWAY         - From another non-OStatus service;
  *                                            will not appear in public views
  *              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
  *              int 'reply_to'; notice ID this is a reply to
  *              int 'repeat_of'; notice ID this is a repeat of
  *              string 'uri' unique ID for notice; defaults to local notice URL
  *              string 'url' permalink to notice; defaults to local notice URL
  *              string 'rendered' rendered HTML version of content
  *              array 'replies' list of profile URIs for reply delivery in
  *                              place of extracting @-replies from content.
  *              array 'groups' list of group IDs to deliver to, in place of
  *                              extracting ! tags from content
  *              array 'tags' list of hashtag strings to save with the notice
  *                           in place of extracting # tags from content
  *              array 'urls' list of attached/referred URLs to save with the
  *                           notice in place of extracting links from content
  *              boolean 'distribute' whether to distribute the notice, default true
  *              string 'object_type' URL of the associated object type (default ActivityObject::NOTE)
  *              string 'verb' URL of the associated verb (default ActivityVerb::POST)
  *              int 'scope' Scope bitmask; default to SITE_SCOPE on private sites, 0 otherwise
  *
  * @fixme tag override
  *
  * @return Notice
  * @throws ClientException
  */
 static function saveNew($profile_id, $content, $source, $options = null)
 {
     $defaults = array('uri' => null, 'url' => null, 'reply_to' => null, 'repeat_of' => null, 'scope' => null, 'distribute' => true, 'object_type' => null, 'verb' => null);
     if (!empty($options) && is_array($options)) {
         $options = array_merge($defaults, $options);
         extract($options);
     } else {
         extract($defaults);
     }
     if (!isset($is_local)) {
         $is_local = Notice::LOCAL_PUBLIC;
     }
     $profile = Profile::staticGet('id', $profile_id);
     $user = User::staticGet('id', $profile_id);
     if ($user) {
         // Use the local user's shortening preferences, if applicable.
         $final = $user->shortenLinks($content);
     } else {
         $final = common_shorten_links($content);
     }
     if ($source != 'activity' && $source != 'event') {
         if (Notice::contentTooLong($final)) {
             // TRANS: Client exception thrown if a notice contains too many characters.
             throw new ClientException(_('Problem saving notice. Too long.'));
         }
     }
     if (empty($profile)) {
         // TRANS: Client exception thrown when trying to save a notice for an unknown user.
         throw new ClientException(_('Problem saving notice. Unknown user.'));
     }
     if (common_config('throttle', 'enabled') && !Notice::checkEditThrottle($profile_id)) {
         common_log(LOG_WARNING, 'Excessive posting by profile #' . $profile_id . '; throttled.');
         // TRANS: Client exception thrown when a user tries to post too many notices in a given time frame.
         throw new ClientException(_('Too many notices too fast; take a breather ' . 'and post again in a few minutes.'));
     }
     if (common_config('site', 'dupelimit') > 0 && !Notice::checkDupes($profile_id, $final)) {
         common_log(LOG_WARNING, 'Dupe posting by profile #' . $profile_id . '; throttled.');
         // TRANS: Client exception thrown when a user tries to post too many duplicate notices in a given time frame.
         throw new ClientException(_('Too many duplicate messages too quickly;' . ' take a breather and post again in a few minutes.'));
     }
     if (!$profile->hasRight(Right::NEWNOTICE)) {
         common_log(LOG_WARNING, "Attempted post from user disallowed to post: " . $profile->nickname);
         // TRANS: Client exception thrown when a user tries to post while being banned.
         throw new ClientException(_('You are banned from posting notices on this site.'), 403);
     }
     $notice = new Notice();
     $notice->profile_id = $profile_id;
     $autosource = common_config('public', 'autosource');
     // Sandboxed are non-false, but not 1, either
     if (!$profile->hasRight(Right::PUBLICNOTICE) || $source && $autosource && in_array($source, $autosource)) {
         $notice->is_local = Notice::LOCAL_NONPUBLIC;
     } else {
         $notice->is_local = $is_local;
     }
     if (!empty($created)) {
         $notice->created = $created;
     } else {
         $notice->created = common_sql_now();
     }
     $notice->content = $final;
     $notice->source = $source;
     $notice->uri = $uri;
     $notice->url = $url;
     // Get the groups here so we can figure out replies and such
     if (!isset($groups)) {
         $groups = self::groupsFromText($notice->content, $profile);
     }
     $reply = null;
     // Handle repeat case
     if (isset($repeat_of)) {
         // Check for a private one
         $repeat = Notice::staticGet('id', $repeat_of);
         if (empty($repeat)) {
             // TRANS: Client exception thrown in notice when trying to repeat a missing or deleted notice.
             throw new ClientException(_('Cannot repeat; original notice is missing or deleted.'));
         }
         if ($profile->id == $repeat->profile_id) {
             // TRANS: Client error displayed when trying to repeat an own notice.
             throw new ClientException(_('You cannot repeat your own notice.'));
         }
         if ($repeat->scope != Notice::SITE_SCOPE && $repeat->scope != Notice::PUBLIC_SCOPE) {
             // TRANS: Client error displayed when trying to repeat a non-public notice.
             throw new ClientException(_('Cannot repeat a private notice.'), 403);
         }
         if (!$repeat->inScope($profile)) {
             // The generic checks above should cover this, but let's be sure!
             // TRANS: Client error displayed when trying to repeat a notice you cannot access.
             throw new ClientException(_('Cannot repeat a notice you cannot read.'), 403);
         }
         if ($profile->hasRepeated($repeat->id)) {
             // TRANS: Client error displayed when trying to repeat an already repeated notice.
             throw new ClientException(_('You already repeated that notice.'));
         }
         $notice->repeat_of = $repeat_of;
     } else {
         $reply = self::getReplyTo($reply_to, $profile_id, $source, $final);
         if (!empty($reply)) {
             if (!$reply->inScope($profile)) {
                 // TRANS: Client error displayed when trying to reply to a notice a the target has no access to.
                 // TRANS: %1$s is a user nickname, %2$d is a notice ID (number).
                 throw new ClientException(sprintf(_('%1$s has no access to notice %2$d.'), $profile->nickname, $reply->id), 403);
             }
             $notice->reply_to = $reply->id;
             $notice->conversation = $reply->conversation;
             // If the original is private to a group, and notice has no group specified,
             // make it to the same group(s)
             if (empty($groups) && $reply->scope | Notice::GROUP_SCOPE) {
                 $groups = array();
                 $replyGroups = $reply->getGroups();
                 foreach ($replyGroups as $group) {
                     if ($profile->isMember($group)) {
                         $groups[] = $group->id;
                     }
                 }
             }
             // Scope set below
         }
     }
     if (!empty($lat) && !empty($lon)) {
         $notice->lat = $lat;
         $notice->lon = $lon;
     }
     if (!empty($location_ns) && !empty($location_id)) {
         $notice->location_id = $location_id;
         $notice->location_ns = $location_ns;
     }
     if (!empty($rendered)) {
         $notice->rendered = $rendered;
     } else {
         $notice->rendered = common_render_content($final, $notice);
     }
     if (empty($verb)) {
         if (!empty($notice->repeat_of)) {
             $notice->verb = ActivityVerb::SHARE;
             $notice->object_type = ActivityObject::ACTIVITY;
         } else {
             $notice->verb = ActivityVerb::POST;
         }
     } else {
         $notice->verb = $verb;
     }
     if (empty($object_type)) {
         $notice->object_type = empty($notice->reply_to) ? ActivityObject::NOTE : ActivityObject::COMMENT;
     } else {
         $notice->object_type = $object_type;
     }
     if (is_null($scope)) {
         // 0 is a valid value
         if (!empty($reply)) {
             $notice->scope = $reply->scope;
         } else {
             $notice->scope = self::defaultScope();
         }
     } else {
         $notice->scope = $scope;
     }
     // For private streams
     $user = $profile->getUser();
     if (!empty($user)) {
         if ($user->private_stream && ($notice->scope == Notice::PUBLIC_SCOPE || $notice->scope == Notice::SITE_SCOPE)) {
             $notice->scope |= Notice::FOLLOWER_SCOPE;
         }
     }
     // Force the scope for private groups
     foreach ($groups as $groupId) {
         $group = User_group::staticGet('id', $groupId);
         if (!empty($group)) {
             if ($group->force_scope) {
                 $notice->scope |= Notice::GROUP_SCOPE;
                 break;
             }
         }
     }
     if (Event::handle('StartNoticeSave', array(&$notice))) {
         // XXX: some of these functions write to the DB
         $id = $notice->insert();
         if (!$id) {
             common_log_db_error($notice, 'INSERT', __FILE__);
             // TRANS: Server exception thrown when a notice cannot be saved.
             throw new ServerException(_('Problem saving notice.'));
         }
         // Update ID-dependent columns: URI, conversation
         $orig = clone $notice;
         $changed = false;
         if (empty($uri)) {
             $notice->uri = common_notice_uri($notice);
             $changed = true;
         }
         // If it's not part of a conversation, it's
         // the beginning of a new conversation.
         if (empty($notice->conversation)) {
             $conv = Conversation::create();
             $notice->conversation = $conv->id;
             $changed = true;
         }
         if ($changed) {
             if (!$notice->update($orig)) {
                 common_log_db_error($notice, 'UPDATE', __FILE__);
                 // TRANS: Server exception thrown when a notice cannot be updated.
                 throw new ServerException(_('Problem saving notice.'));
             }
         }
     }
     // Clear the cache for subscribed users, so they'll update at next request
     // XXX: someone clever could prepend instead of clearing the cache
     $notice->blowOnInsert();
     // Save per-notice metadata...
     if (isset($replies)) {
         $notice->saveKnownReplies($replies);
     } else {
         $notice->saveReplies();
     }
     if (isset($tags)) {
         $notice->saveKnownTags($tags);
     } else {
         $notice->saveTags();
     }
     // Note: groups may save tags, so must be run after tags are saved
     // to avoid errors on duplicates.
     // Note: groups should always be set.
     $notice->saveKnownGroups($groups);
     if (isset($urls)) {
         $notice->saveKnownUrls($urls);
     } else {
         $notice->saveUrls();
     }
     if ($distribute) {
         // Prepare inbox delivery, may be queued to background.
         $notice->distribute();
     }
     return $notice;
 }
Esempio n. 10
0
 /**
  * Save a new question notice
  *
  * @param Profile $profile
  * @param string  $question
  * @param string  $title
  * @param string  $description
  * @param array   $option // and whatnot
  *
  * @return Notice saved notice
  */
 static function saveNew($profile, $title, $description, $options = array())
 {
     $q = new QnA_Question();
     $q->id = UUID::gen();
     $q->profile_id = $profile->id;
     $q->title = $title;
     $q->description = $description;
     if (array_key_exists('created', $options)) {
         $q->created = $options['created'];
     } else {
         $q->created = common_sql_now();
     }
     if (array_key_exists('uri', $options)) {
         $q->uri = $options['uri'];
     } else {
         $q->uri = common_local_url('qnashowquestion', array('id' => $q->id));
     }
     common_log(LOG_DEBUG, "Saving question: {$q->id} {$q->uri}");
     $q->insert();
     if (Notice::contentTooLong($q->title . ' ' . $q->uri)) {
         $max = Notice::maxContent();
         $uriLen = mb_strlen($q->uri);
         $targetLen = $max - ($uriLen + 15);
         $title = mb_substr($q->title, 0, $targetLen) . '…';
     }
     $content = $title . ' ' . $q->uri;
     $link = '<a href="' . htmlspecialchars($q->uri) . '">' . htmlspecialchars($q->title) . '</a>';
     // TRANS: Rendered version of the notice content creating a question.
     // TRANS: %s a link to the question as link description.
     $rendered = sprintf(_m('Question: %s'), $link);
     $tags = array('question');
     $replies = array();
     $options = array_merge(array('urls' => array(), 'rendered' => $rendered, 'tags' => $tags, 'replies' => $replies, 'object_type' => self::OBJECT_TYPE), $options);
     if (!array_key_exists('uri', $options)) {
         $options['uri'] = $q->uri;
     }
     $saved = Notice::saveNew($profile->id, $content, array_key_exists('source', $options) ? $options['source'] : 'web', $options);
     return $saved;
 }
Esempio n. 11
0
 static function shorten($content, $notice)
 {
     $short = null;
     if (Notice::contentTooLong($content)) {
         common_debug("content too long");
         $max = Notice::maxContent();
         // TRANS: Link description for link to full notice text if it is longer than
         // TRANS: what will be dispplayed.
         $ellipsis = _m('…');
         $short = mb_substr($content, 0, $max - 1);
         $short .= sprintf('<a href="%1$s" rel="more" title="%2$s">%3$s</a>', $notice->getUrl(), _m('more...'), $ellipsis);
     } else {
         $short = $content;
     }
     return $short;
 }
 /**
  * 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) {
         // 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']));
         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)) {
         // TRANS: Client error displayed when updating a status for a non-existing user.
         $this->clientError(_('No such user.'), 404, $this->format);
         return;
     }
     $status_shortened = $this->auth_user->shortenlinks($this->status);
     if (Notice::contentTooLong($status_shortened)) {
         // Note: Twitter truncates anything over 140, flags the status
         // as "truncated."
         $this->clientError(sprintf(_m('That\'s too long. Maximum notice size is %d character.', 'That\'s too long. Maximum notice size is %d characters.', Notice::maxContent()), 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(_('Parent notice not found.'), $code = 404, $this->format);
                 return;
             }
         }
         if (!empty($this->link)) {
             try {
                 $post_url_file = MediaFile::fromLink($this->link, $this->auth_user);
             } catch (Exception $e) {
                 $this->clientError($e->getMessage(), $e->getCode(), $this->format);
                 return;
             }
         }
         $upload2 = null;
         try {
             $upload2 = MediaFile::fromUpload('media2', $this->auth_user);
         } catch (Exception $e) {
             $this->clientError($e->getMessage(), $e->getCode(), $this->format);
             return;
         }
         $upload = null;
         try {
             $upload = MediaFile::fromUpload('media', $this->auth_user);
         } catch (Exception $e) {
             $this->clientError($e->getMessage(), $e->getCode(), $this->format);
             return;
         }
         if (isset($upload)) {
             if (Notice::contentTooLong($status_shortened)) {
                 $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());
                 $this->clientError(sprintf($msg, Notice::maxContent()), 400, $this->format);
             }
         }
         $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);
         }
         //dyg add to response to group_id request
         ToSelector::fillOptions($this, $options);
         //end
         if (!empty($this->html_status)) {
             $options['rendered'] = $this->html_status;
         }
         $created = $this->trimmed('created');
         if (!empty($created)) {
             $options['created'] = $created;
         }
         try {
             $this->notice = Notice::saveNew($this->auth_user->id, $content, $this->source, $options);
         } catch (Exception $e) {
             $this->clientError($e->getMessage(), $e->getCode(), $this->format);
             return;
         }
         if (isset($upload)) {
             $upload->attachToNotice($this->notice);
         }
         if (isset($upload2)) {
             $upload2->attachToNotice($this->notice);
         }
         if (isset($post_url_file)) {
             $post_url_file->attachToNotice($this->notice);
         }
     }
     $this->showNotice();
 }
Esempio n. 13
0
 /**
  * This doPost saves 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
  */
 protected function doPost()
 {
     assert($this->scoped instanceof Profile);
     // XXX: maybe an error instead...
     $user = $this->scoped->getUser();
     $content = $this->trimmed('status_textarea');
     $options = array();
     Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
     if (empty($content)) {
         // TRANS: Client error displayed trying to send a notice without content.
         $this->clientError(_('No content!'));
     }
     $inter = new CommandInterpreter();
     $cmd = $inter->handle_command($user, $content);
     if ($cmd) {
         if (GNUsocial::isAjax()) {
             $cmd->execute(new AjaxWebChannel($this));
         } else {
             $cmd->execute(new WebChannel($this));
         }
         return;
     }
     $content_shortened = $user->shortenLinks($content);
     if (Notice::contentTooLong($content_shortened)) {
         // TRANS: Client error displayed when the parameter "status" is missing.
         // TRANS: %d is the maximum number of character for a notice.
         $this->clientError(sprintf(_m('That\'s too long. Maximum notice size is %d character.', 'That\'s too long. Maximum notice size is %d characters.', Notice::maxContent()), Notice::maxContent()));
     }
     $replyto = $this->int('inreplyto');
     if ($replyto) {
         $options['reply_to'] = $replyto;
     }
     $upload = null;
     try {
         // throws exception on failure
         $upload = MediaFile::fromUpload('attach', $this->scoped);
         if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) {
             $content_shortened .= ' ' . $upload->shortUrl();
         }
         Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options));
         if (Notice::contentTooLong($content_shortened)) {
             $upload->delete();
             // TRANS: Client error displayed exceeding the maximum notice length.
             // TRANS: %d is the maximum length for a notice.
             $this->clientError(sprintf(_m('Maximum notice size is %d character, including attachment URL.', 'Maximum notice size is %d characters, including attachment URL.', Notice::maxContent()), Notice::maxContent()));
         }
     } catch (NoUploadedMediaException $e) {
         // simply no attached media to the new notice
     }
     if ($this->scoped->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'), $this->scoped);
         } else {
             $locOptions = Notice::locationOptions(null, null, null, null, $this->scoped);
         }
         $options = array_merge($options, $locOptions);
     }
     $author_id = $this->scoped->id;
     $text = $content_shortened;
     // Does the heavy-lifting for getting "To:" information
     ToSelector::fillOptions($this, $options);
     if (Event::handle('StartNoticeSaveWeb', array($this, &$author_id, &$text, &$options))) {
         $this->stored = Notice::saveNew($this->scoped->id, $content_shortened, 'web', $options);
         if ($upload instanceof MediaFile) {
             $upload->attachToNotice($this->stored);
         }
         Event::handle('EndNoticeSaveWeb', array($this, $this->stored));
     }
     Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content_shortened, &$options));
     if (!GNUsocial::isAjax()) {
         $url = common_local_url('shownotice', array('notice' => $this->stored->id));
         common_redirect($url, 303);
     }
     return _('Saved the notice!');
 }
Esempio n. 14
0
 function checkNotice()
 {
     $content = common_shorten_links($_POST['omb_notice_content']);
     if (Notice::contentTooLong($content)) {
         $this->clientError(_('Invalid notice content.'), 400);
         return false;
     }
     $license = $_POST['omb_notice_license'];
     $site_license = common_config('license', 'url');
     if ($license && !common_compatible_license($license, $site_license)) {
         throw new Exception(sprintf(_('Notice license ‘%1$s’ is not ' . 'compatible with site license ‘%2$s’.'), $license, $site_license));
     }
 }
Esempio n. 15
0
 /**
  * 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');
     $options = array();
     Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
     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 = $user->shortenLinks($content);
     if (Notice::contentTooLong($content_shortened)) {
         // TRANS: Client error displayed when the parameter "status" is missing.
         // TRANS: %d is the maximum number of character for a notice.
         $this->clientError(sprintf(_m('That\'s too long. Maximum notice size is %d character.', 'That\'s too long. Maximum notice size is %d characters.', Notice::maxContent()), Notice::maxContent()));
     }
     $replyto = intval($this->trimmed('inreplyto'));
     if ($replyto) {
         $options['reply_to'] = $replyto;
     }
     $upload = null;
     $upload = MediaFile::fromUpload('attach');
     if (isset($upload)) {
         if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) {
             $content_shortened .= ' ' . $upload->shortUrl();
         }
         Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options));
         if (Notice::contentTooLong($content_shortened)) {
             $upload->delete();
             $this->clientError(sprintf(_m('Maximum notice size is %d character, including attachment URL.', 'Maximum notice size is %d characters, including attachment URL.', Notice::maxContent()), Notice::maxContent()));
         }
     }
     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);
     }
     $author_id = $user->id;
     $text = $content_shortened;
     if (Event::handle('StartNoticeSaveWeb', array($this, &$author_id, &$text, &$options))) {
         $notice = Notice::saveNew($user->id, $content_shortened, 'web', $options);
         if (isset($upload)) {
             $upload->attachToNotice($notice);
         }
         Event::handle('EndNoticeSaveWeb', array($this, $notice));
     }
     Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content_shortened, &$options));
     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. 16
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);
 }
 /**
  * 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();
 }
Esempio n. 18
0
 static function saveNew($profile, $title, $content, $options = null)
 {
     if (is_null($options)) {
         $options = array();
     }
     $be = new Blog_entry();
     $be->id = (string) new UUID();
     $be->profile_id = $profile->id;
     $be->title = $title;
     // Note: not HTML-protected
     $be->content = self::purify($content);
     if (array_key_exists('summary', $options)) {
         $be->summary = self::purify($options['summary']);
     } else {
         // Already purified
         $be->summary = self::summarize($be->content);
     }
     // Don't save an identical summary
     if ($be->summary == $be->content) {
         $be->summary = null;
     }
     $url = common_local_url('showblogentry', array('id' => $be->id));
     if (!array_key_exists('uri', $options)) {
         $options['uri'] = $url;
     }
     $be->uri = $options['uri'];
     if (!array_key_exists('url', $options)) {
         $options['url'] = $url;
     }
     $be->url = $options['url'];
     if (!array_key_exists('created', $options)) {
         $be->created = common_sql_now();
     }
     $be->created = $options['created'];
     $be->modified = common_sql_now();
     $be->insert();
     // Use user's preferences for short URLs, if possible
     try {
         $user = $profile->getUser();
         $shortUrl = File_redirection::makeShort($url, empty($user) ? null : $user);
     } catch (Exception $e) {
         // Don't let this stop us.
         $shortUrl = $url;
     }
     // XXX: this might be too long.
     if (!empty($be->summary)) {
         $options['rendered'] = $be->summary . ' ' . XMLStringer::estring('a', array('href' => $url, 'class' => 'blog-entry'), _('More...'));
         $text = html_entity_decode(strip_tags($be->summary), ENT_QUOTES, 'UTF-8');
     } else {
         $options['rendered'] = $be->content;
         $text = html_entity_decode(strip_tags($be->content), ENT_QUOTES, 'UTF-8');
     }
     if (Notice::contentTooLong($text)) {
         $text = substr($text, 0, Notice::maxContent() - mb_strlen($shortUrl) - 2) . '… ' . $shortUrl;
     }
     // Override this no matter what.
     $options['object_type'] = self::TYPE;
     $source = array_key_exists('source', $options) ? $options['source'] : 'web';
     $saved = Notice::saveNew($profile->id, $text, $source, $options);
     return $saved;
 }
Esempio n. 19
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. 20
0
 /**
  * Save a new notice and push it out to subscribers' inboxes.
  * Poster's permissions are checked before sending.
  *
  * @param int $profile_id Profile ID of the poster
  * @param string $content source message text; links may be shortened
  *                        per current user's preference
  * @param string $source source key ('web', 'api', etc)
  * @param array $options Associative array of optional properties:
  *              string 'created' timestamp of notice; defaults to now
  *              int 'is_local' source/gateway ID, one of:
  *                  Notice::LOCAL_PUBLIC    - Local, ok to appear in public timeline
  *                  Notice::REMOTE_OMB      - Sent from a remote OMB service;
  *                                            hide from public timeline but show in
  *                                            local "and friends" timelines
  *                  Notice::LOCAL_NONPUBLIC - Local, but hide from public timeline
  *                  Notice::GATEWAY         - From another non-OMB service;
  *                                            will not appear in public views
  *              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
  *              int 'reply_to'; notice ID this is a reply to
  *              int 'repeat_of'; notice ID this is a repeat of
  *              string 'uri' unique ID for notice; defaults to local notice URL
  *              string 'url' permalink to notice; defaults to local notice URL
  *              string 'rendered' rendered HTML version of content
  *              array 'replies' list of profile URIs for reply delivery in
  *                              place of extracting @-replies from content.
  *              array 'groups' list of group IDs to deliver to, in place of
  *                              extracting ! tags from content
  *              array 'tags' list of hashtag strings to save with the notice
  *                           in place of extracting # tags from content
  *              array 'urls' list of attached/referred URLs to save with the
  *                           notice in place of extracting links from content
  * @fixme tag override
  *
  * @return Notice
  * @throws ClientException
  */
 static function saveNew($profile_id, $content, $source, $options = null)
 {
     $defaults = array('uri' => null, 'url' => null, 'reply_to' => null, 'repeat_of' => null);
     if (!empty($options)) {
         $options = $options + $defaults;
         extract($options);
     } else {
         extract($defaults);
     }
     if (!isset($is_local)) {
         $is_local = Notice::LOCAL_PUBLIC;
     }
     $profile = Profile::staticGet($profile_id);
     $final = common_shorten_links($content);
     if (Notice::contentTooLong($final)) {
         // TRANS: Client exception thrown if a notice contains too many characters.
         throw new ClientException(_('Problem saving notice. Too long.'));
     }
     if (empty($profile)) {
         // TRANS: Client exception thrown when trying to save a notice for an unknown user.
         throw new ClientException(_('Problem saving notice. Unknown user.'));
     }
     if (common_config('throttle', 'enabled') && !Notice::checkEditThrottle($profile_id)) {
         common_log(LOG_WARNING, 'Excessive posting by profile #' . $profile_id . '; throttled.');
         // TRANS: Client exception thrown when a user tries to post too many notices in a given time frame.
         throw new ClientException(_('Too many notices too fast; take a breather ' . 'and post again in a few minutes.'));
     }
     if (common_config('site', 'dupelimit') > 0 && !Notice::checkDupes($profile_id, $final)) {
         common_log(LOG_WARNING, 'Dupe posting by profile #' . $profile_id . '; throttled.');
         // TRANS: Client exception thrown when a user tries to post too many duplicate notices in a given time frame.
         throw new ClientException(_('Too many duplicate messages too quickly;' . ' take a breather and post again in a few minutes.'));
     }
     if (!$profile->hasRight(Right::NEWNOTICE)) {
         common_log(LOG_WARNING, "Attempted post from user disallowed to post: " . $profile->nickname);
         // TRANS: Client exception thrown when a user tries to post while being banned.
         throw new ClientException(_('You are banned from posting notices on this site.'), 403);
     }
     $notice = new Notice();
     $notice->profile_id = $profile_id;
     $autosource = common_config('public', 'autosource');
     # Sandboxed are non-false, but not 1, either
     if (!$profile->hasRight(Right::PUBLICNOTICE) || $source && $autosource && in_array($source, $autosource)) {
         $notice->is_local = Notice::LOCAL_NONPUBLIC;
     } else {
         $notice->is_local = $is_local;
     }
     if (!empty($created)) {
         $notice->created = $created;
     } else {
         $notice->created = common_sql_now();
     }
     $notice->content = $final;
     $notice->source = $source;
     $notice->uri = $uri;
     $notice->url = $url;
     // Handle repeat case
     if (isset($repeat_of)) {
         $notice->repeat_of = $repeat_of;
     } else {
         $notice->reply_to = self::getReplyTo($reply_to, $profile_id, $source, $final);
     }
     if (!empty($notice->reply_to)) {
         $reply = Notice::staticGet('id', $notice->reply_to);
         $notice->conversation = $reply->conversation;
     }
     if (!empty($lat) && !empty($lon)) {
         $notice->lat = $lat;
         $notice->lon = $lon;
     }
     if (!empty($location_ns) && !empty($location_id)) {
         $notice->location_id = $location_id;
         $notice->location_ns = $location_ns;
     }
     if (!empty($rendered)) {
         $notice->rendered = $rendered;
     } else {
         $notice->rendered = common_render_content($final, $notice);
     }
     if (Event::handle('StartNoticeSave', array(&$notice))) {
         // XXX: some of these functions write to the DB
         $id = $notice->insert();
         if (!$id) {
             common_log_db_error($notice, 'INSERT', __FILE__);
             // TRANS: Server exception thrown when a notice cannot be saved.
             throw new ServerException(_('Problem saving notice.'));
         }
         // Update ID-dependent columns: URI, conversation
         $orig = clone $notice;
         $changed = false;
         if (empty($uri)) {
             $notice->uri = common_notice_uri($notice);
             $changed = true;
         }
         // If it's not part of a conversation, it's
         // the beginning of a new conversation.
         if (empty($notice->conversation)) {
             $conv = Conversation::create();
             $notice->conversation = $conv->id;
             $changed = true;
         }
         if ($changed) {
             if (!$notice->update($orig)) {
                 common_log_db_error($notice, 'UPDATE', __FILE__);
                 // TRANS: Server exception thrown when a notice cannot be updated.
                 throw new ServerException(_('Problem saving notice.'));
             }
         }
     }
     # Clear the cache for subscribed users, so they'll update at next request
     # XXX: someone clever could prepend instead of clearing the cache
     $notice->blowOnInsert();
     // Save per-notice metadata...
     if (isset($replies)) {
         $notice->saveKnownReplies($replies);
     } else {
         $notice->saveReplies();
     }
     if (isset($tags)) {
         $notice->saveKnownTags($tags);
     } else {
         $notice->saveTags();
     }
     // Note: groups may save tags, so must be run after tags are saved
     // to avoid errors on duplicates.
     if (isset($groups)) {
         $notice->saveKnownGroups($groups);
     } else {
         $notice->saveGroups();
     }
     if (isset($urls)) {
         $notice->saveKnownUrls($urls);
     } else {
         $notice->saveUrls();
     }
     // Prepare inbox delivery, may be queued to background.
     $notice->distribute();
     return $notice;
 }
Esempio n. 21
0
 /**
  * Save a new notice and push it out to subscribers' inboxes.
  * Poster's permissions are checked before sending.
  *
  * @param int $profile_id Profile ID of the poster
  * @param string $content source message text; links may be shortened
  *                        per current user's preference
  * @param string $source source key ('web', 'api', etc)
  * @param array $options Associative array of optional properties:
  *              string 'created' timestamp of notice; defaults to now
  *              int 'is_local' source/gateway ID, one of:
  *                  Notice::LOCAL_PUBLIC    - Local, ok to appear in public timeline
  *                  Notice::REMOTE          - Sent from a remote service;
  *                                            hide from public timeline but show in
  *                                            local "and friends" timelines
  *                  Notice::LOCAL_NONPUBLIC - Local, but hide from public timeline
  *                  Notice::GATEWAY         - From another non-OStatus service;
  *                                            will not appear in public views
  *              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
  *              int 'reply_to'; notice ID this is a reply to
  *              int 'repeat_of'; notice ID this is a repeat of
  *              string 'uri' unique ID for notice; a unique tag uri (can be url or anything too)
  *              string 'url' permalink to notice; defaults to local notice URL
  *              string 'rendered' rendered HTML version of content
  *              array 'replies' list of profile URIs for reply delivery in
  *                              place of extracting @-replies from content.
  *              array 'groups' list of group IDs to deliver to, in place of
  *                              extracting ! tags from content
  *              array 'tags' list of hashtag strings to save with the notice
  *                           in place of extracting # tags from content
  *              array 'urls' list of attached/referred URLs to save with the
  *                           notice in place of extracting links from content
  *              boolean 'distribute' whether to distribute the notice, default true
  *              string 'object_type' URL of the associated object type (default ActivityObject::NOTE)
  *              string 'verb' URL of the associated verb (default ActivityVerb::POST)
  *              int 'scope' Scope bitmask; default to SITE_SCOPE on private sites, 0 otherwise
  *
  * @fixme tag override
  *
  * @return Notice
  * @throws ClientException
  */
 static function saveNew($profile_id, $content, $source, array $options = null)
 {
     $defaults = array('uri' => null, 'url' => null, 'conversation' => null, 'reply_to' => null, 'repeat_of' => null, 'scope' => null, 'distribute' => true, 'object_type' => null, 'verb' => null);
     if (!empty($options) && is_array($options)) {
         $options = array_merge($defaults, $options);
         extract($options);
     } else {
         extract($defaults);
     }
     if (!isset($is_local)) {
         $is_local = Notice::LOCAL_PUBLIC;
     }
     $profile = Profile::getKV('id', $profile_id);
     if (!$profile instanceof Profile) {
         // TRANS: Client exception thrown when trying to save a notice for an unknown user.
         throw new ClientException(_('Problem saving notice. Unknown user.'));
     }
     $user = User::getKV('id', $profile_id);
     if ($user instanceof User) {
         // Use the local user's shortening preferences, if applicable.
         $final = $user->shortenLinks($content);
     } else {
         $final = common_shorten_links($content);
     }
     if (Notice::contentTooLong($final)) {
         // TRANS: Client exception thrown if a notice contains too many characters.
         throw new ClientException(_('Problem saving notice. Too long.'));
     }
     if (common_config('throttle', 'enabled') && !Notice::checkEditThrottle($profile_id)) {
         common_log(LOG_WARNING, 'Excessive posting by profile #' . $profile_id . '; throttled.');
         // TRANS: Client exception thrown when a user tries to post too many notices in a given time frame.
         throw new ClientException(_('Too many notices too fast; take a breather ' . 'and post again in a few minutes.'));
     }
     if (common_config('site', 'dupelimit') > 0 && !Notice::checkDupes($profile_id, $final)) {
         common_log(LOG_WARNING, 'Dupe posting by profile #' . $profile_id . '; throttled.');
         // TRANS: Client exception thrown when a user tries to post too many duplicate notices in a given time frame.
         throw new ClientException(_('Too many duplicate messages too quickly;' . ' take a breather and post again in a few minutes.'));
     }
     if (!$profile->hasRight(Right::NEWNOTICE)) {
         common_log(LOG_WARNING, "Attempted post from user disallowed to post: " . $profile->nickname);
         // TRANS: Client exception thrown when a user tries to post while being banned.
         throw new ClientException(_('You are banned from posting notices on this site.'), 403);
     }
     $notice = new Notice();
     $notice->profile_id = $profile_id;
     $autosource = common_config('public', 'autosource');
     // Sandboxed are non-false, but not 1, either
     if (!$profile->hasRight(Right::PUBLICNOTICE) || $source && $autosource && in_array($source, $autosource)) {
         $notice->is_local = Notice::LOCAL_NONPUBLIC;
     } else {
         $notice->is_local = $is_local;
     }
     if (!empty($created)) {
         $notice->created = $created;
     } else {
         $notice->created = common_sql_now();
     }
     if (!$notice->isLocal()) {
         // Only do these checks for non-local notices. Local notices will generate these values later.
         if (!common_valid_http_url($url)) {
             common_debug('Bad notice URL: [' . $url . '], URI: [' . $uri . ']. Cannot link back to original! This is normal for shared notices etc.');
         }
         if (empty($uri)) {
             throw new ServerException('No URI for remote notice. Cannot accept that.');
         }
     }
     $notice->content = $final;
     $notice->source = $source;
     $notice->uri = $uri;
     $notice->url = $url;
     // Get the groups here so we can figure out replies and such
     if (!isset($groups)) {
         $groups = User_group::idsFromText($notice->content, $profile);
     }
     $reply = null;
     // Handle repeat case
     if (!empty($options['repeat_of'])) {
         // Check for a private one
         $repeat = Notice::getByID($options['repeat_of']);
         if ($profile->sameAs($repeat->getProfile())) {
             // TRANS: Client error displayed when trying to repeat an own notice.
             throw new ClientException(_('You cannot repeat your own notice.'));
         }
         if ($repeat->scope != Notice::SITE_SCOPE && $repeat->scope != Notice::PUBLIC_SCOPE) {
             // TRANS: Client error displayed when trying to repeat a non-public notice.
             throw new ClientException(_('Cannot repeat a private notice.'), 403);
         }
         if (!$repeat->inScope($profile)) {
             // The generic checks above should cover this, but let's be sure!
             // TRANS: Client error displayed when trying to repeat a notice you cannot access.
             throw new ClientException(_('Cannot repeat a notice you cannot read.'), 403);
         }
         if ($profile->hasRepeated($repeat)) {
             // TRANS: Client error displayed when trying to repeat an already repeated notice.
             throw new ClientException(_('You already repeated that notice.'));
         }
         $notice->repeat_of = $repeat->id;
         $notice->conversation = $repeat->conversation;
     } else {
         $reply = null;
         // If $reply_to is specified, we check that it exists, and then
         // return it if it does
         if (!empty($reply_to)) {
             $reply = Notice::getKV('id', $reply_to);
         } elseif (in_array($source, array('xmpp', 'mail', 'sms'))) {
             // If the source lacks capability of sending the "reply_to"
             // metadata, let's try to find an inline replyto-reference.
             $reply = self::getInlineReplyTo($profile, $final);
         }
         if ($reply instanceof Notice) {
             if (!$reply->inScope($profile)) {
                 // TRANS: Client error displayed when trying to reply to a notice a the target has no access to.
                 // TRANS: %1$s is a user nickname, %2$d is a notice ID (number).
                 throw new ClientException(sprintf(_('%1$s has no access to notice %2$d.'), $profile->nickname, $reply->id), 403);
             }
             // If it's a repeat, the reply_to should be to the original
             if ($reply->isRepeat()) {
                 $notice->reply_to = $reply->repeat_of;
             } else {
                 $notice->reply_to = $reply->id;
             }
             // But the conversation ought to be the same :)
             $notice->conversation = $reply->conversation;
             // If the original is private to a group, and notice has
             // no group specified, make it to the same group(s)
             if (empty($groups) && $reply->scope & Notice::GROUP_SCOPE) {
                 $groups = array();
                 $replyGroups = $reply->getGroups();
                 foreach ($replyGroups as $group) {
                     if ($profile->isMember($group)) {
                         $groups[] = $group->id;
                     }
                 }
             }
             // Scope set below
         }
         // If we don't know the reply, we might know the conversation!
         // This will happen if a known remote user replies to an
         // unknown remote user - within a known conversation.
         if (empty($notice->conversation) and !empty($options['conversation'])) {
             $conv = Conversation::getKV('uri', $options['conversation']);
             if ($conv instanceof Conversation) {
                 common_debug('Conversation stitched together from (probably) a reply to unknown remote user. Activity creation time (' . $notice->created . ') should maybe be compared to conversation creation time (' . $conv->created . ').');
             } else {
                 // Conversation entry with specified URI was not found, so we must create it.
                 common_debug('Conversation URI not found, so we will create it with the URI given in the options to Notice::saveNew: ' . $options['conversation']);
                 // The insert in Conversation::create throws exception on failure
                 $conv = Conversation::create($options['conversation'], $notice->created);
             }
             $notice->conversation = $conv->getID();
             unset($conv);
         }
     }
     // If it's not part of a conversation, it's the beginning of a new conversation.
     if (empty($notice->conversation)) {
         $conv = Conversation::create();
         $notice->conversation = $conv->getID();
         unset($conv);
     }
     $notloc = new Notice_location();
     if (!empty($lat) && !empty($lon)) {
         $notloc->lat = $lat;
         $notloc->lon = $lon;
     }
     if (!empty($location_ns) && !empty($location_id)) {
         $notloc->location_id = $location_id;
         $notloc->location_ns = $location_ns;
     }
     if (!empty($rendered)) {
         $notice->rendered = $rendered;
     } else {
         $notice->rendered = common_render_content($final, $notice->getProfile(), $notice->hasParent() ? $notice->getParent() : null);
     }
     if (empty($verb)) {
         if ($notice->isRepeat()) {
             $notice->verb = ActivityVerb::SHARE;
             $notice->object_type = ActivityObject::ACTIVITY;
         } else {
             $notice->verb = ActivityVerb::POST;
         }
     } else {
         $notice->verb = $verb;
     }
     if (empty($object_type)) {
         $notice->object_type = empty($notice->reply_to) ? ActivityObject::NOTE : ActivityObject::COMMENT;
     } else {
         $notice->object_type = $object_type;
     }
     if (is_null($scope) && $reply instanceof Notice) {
         $notice->scope = $reply->scope;
     } else {
         $notice->scope = $scope;
     }
     $notice->scope = self::figureOutScope($profile, $groups, $notice->scope);
     if (Event::handle('StartNoticeSave', array(&$notice))) {
         // XXX: some of these functions write to the DB
         try {
             $notice->insert();
             // throws exception on failure, if successful we have an ->id
             if ($notloc->lat && $notloc->lon || $notloc->location_id && $notloc->location_ns) {
                 $notloc->notice_id = $notice->getID();
                 $notloc->insert();
                 // store the notice location if it had any information
             }
         } catch (Exception $e) {
             // Let's test if we managed initial insert, which would imply
             // failing on some update-part (check 'insert()'). Delete if
             // something had been stored to the database.
             if (!empty($notice->id)) {
                 $notice->delete();
             }
             throw $e;
         }
     }
     // Only save 'attention' and metadata stuff (URLs, tags...) stuff if
     // the activityverb is a POST (since stuff like repeat, favorite etc.
     // reasonably handle notifications themselves.
     if (ActivityUtils::compareVerbs($notice->verb, array(ActivityVerb::POST))) {
         if (isset($replies)) {
             $notice->saveKnownReplies($replies);
         } else {
             $notice->saveReplies();
         }
         if (isset($tags)) {
             $notice->saveKnownTags($tags);
         } else {
             $notice->saveTags();
         }
         // Note: groups may save tags, so must be run after tags are saved
         // to avoid errors on duplicates.
         // Note: groups should always be set.
         $notice->saveKnownGroups($groups);
         if (isset($urls)) {
             $notice->saveKnownUrls($urls);
         } else {
             $notice->saveUrls();
         }
     }
     if ($distribute) {
         // Prepare inbox delivery, may be queued to background.
         $notice->distribute();
     }
     return $notice;
 }
Esempio n. 22
0
 /**
  * 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...
     $this->content = $this->trimmed('status_textarea');
     $groupid = $this->trimmed('inreplyto');
     $options = array();
     if (!$this->content) {
         // TRANS: Client error displayed trying to send a notice without content.
         $this->clientError(_('Mensaje vacío!!'));
         return;
     }
     $content_shortened = $user->shortenLinks($this->content);
     if (Notice::contentTooLong($content_shortened)) {
         // TRANS: Client error displayed when the parameter "status" is missing.
         // TRANS: %d is the maximum number of character for a notice.
         $this->clientError('Contenido demasiado largo! El tamaño máximo de caracteres es ' . Notice::maxContent() . '.');
     }
     $replyto = intval($this->trimmed('inreplyto'));
     if ($replyto) {
         $options['reply_to'] = $replyto;
     }
     $upload = null;
     $upload = MediaFile::fromUpload('attach');
     if (isset($upload)) {
         $content_shortened .= ' ' . $upload->shortUrl();
         if (Notice::contentTooLong($content_shortened)) {
             $upload->delete();
             // TRANS: Client error displayed exceeding the maximum notice length.
             // TRANS: %d is the maximum length for a notice.
             $this->clientError('Contenido demasiado largo! El tamaño máximo de caracteres ' . 'es ' . Notice::maxContent() . ', incluyendo la URL del adjunto.');
         }
     }
     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);
     }
     $author_id = $user->id;
     $text = $content_shortened;
     // Does the heavy-lifting for getting "To:" information
     $notice_to = $this->trimmed('notice_to');
     $options['groups'] = array($notice_to);
     $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');
         // TRANS: Page title after sending a notice.
         $this->element('title', null, _('Notice posted'));
         $this->elementEnd('head');
         $this->elementStart('body');
         $this->element('p', array('class' => 'notice-task-posted'), 'Tarea completada correctamente, mensaje publicado.');
         $this->elementEnd('body');
         $this->elementEnd('html');
     }
 }
Esempio n. 23
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);
 }
Esempio n. 24
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);
 }
 /**
  * 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();
 }
 /**
  * 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);
     }
 }
 /**
  * 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;
 }
Esempio n. 28
0
function linkback_notice($source, $notice_or_user, $entry, $author, $mf2)
{
    $content = $entry['content'] ? $entry['content'][0]['html'] : ($entry['summary'] ? $entry['sumary'][0] : $entry['name'][0]);
    $rendered = common_purify($content);
    if ($notice_or_user instanceof Notice && $entry['type'] == 'mention') {
        $name = $entry['name'] ? $entry['name'][0] : substr(common_strip_html($content), 0, 20) . '…';
        $rendered = _m('linked to this from <a href="' . htmlspecialchars($source) . '">' . htmlspecialchars($name) . '</a>');
    }
    $content = common_strip_html($rendered);
    $shortened = common_shorten_links($content);
    if (Notice::contentTooLong($shortened)) {
        $content = substr($content, 0, Notice::maxContent() - (mb_strlen($source) + 2));
        $rendered = $content . '<a href="' . htmlspecialchars($source) . '">…</a>';
        $content .= ' ' . $source;
    }
    $options = array('is_local' => Notice::REMOTE, 'url' => $entry['url'][0], 'uri' => $entry['url'][0], 'rendered' => $rendered, 'replies' => array(), 'groups' => array(), 'peopletags' => array(), 'tags' => array(), 'urls' => array());
    if ($notice_or_user instanceof User) {
        $options['replies'][] = $notice_or_user->getUri();
    } else {
        if ($entry['type'] == 'repost') {
            $options['repeat_of'] = $notice_or_user->id;
        } else {
            $options['reply_to'] = $notice_or_user->id;
        }
    }
    if ($entry['published'] || $entry['updated']) {
        $options['created'] = $entry['published'] ? common_sql_date($entry['published'][0]) : common_sql_date($entry['updated'][0]);
    }
    if ($entry['photo']) {
        $options['urls'][] = $entry['photo'][0];
    }
    foreach ((array) $entry['category'] as $tag) {
        $tag = common_canonical_tag($tag);
        if ($tag) {
            $options['tags'][] = $tag;
        }
    }
    if ($mf2['rels'] && $mf2['rels']['enclosure']) {
        foreach ($mf2['rels']['enclosure'] as $url) {
            $options['urls'][] = $url;
        }
    }
    if ($mf2['rels'] && $mf2['rels']['tag']) {
        foreach ($mf2['rels']['tag'] as $url) {
            preg_match('/\\/([^\\/]+)\\/*$/', $url, $match);
            $tag = common_canonical_tag($match[1]);
            if ($tag) {
                $options['tags'][] = $tag;
            }
        }
    }
    if ($entry['type'] != 'reply' && $entry['type'] != 'repost') {
        $options['urls'] = array();
    }
    return array($content, $options);
}