Esempio n. 1
0
 function onHandleQueuedNotice($notice)
 {
     if (intval($notice->is_local) === Notice::LOCAL_PUBLIC) {
         // Try to avoid actually mucking with the
         // notice content
         $c = $notice->content;
         $this->notice = $notice;
         if (!$notice->getProfile()->getPref("linkbackplugin", "disable_linkbacks")) {
             // Ignoring results
             common_replace_urls_callback($c, array($this, 'linkbackUrl'));
         }
         if ($notice->isRepeat()) {
             $repeat = Notice::getByID($notice->repeat_of);
             $this->linkbackUrl($repeat->getUrl());
         } else {
             if (!empty($notice->reply_to)) {
                 $parent = $notice->getParent();
                 $this->linkbackUrl($parent->getUrl());
             }
         }
         $replyProfiles = Profile::multiGet('id', $notice->getReplies());
         foreach ($replyProfiles->fetchAll('profileurl') as $profileurl) {
             $this->linkbackUrl($profileurl);
         }
     }
     return true;
 }
Esempio n. 2
0
 protected function doPreparation()
 {
     $this->notice = Notice::getByID($this->trimmed('notice'));
     if (!$this->scoped->sameAs($this->notice->getProfile()) && !$this->scoped->hasRight(Right::DELETEOTHERSNOTICE)) {
         // TRANS: Error message displayed trying to delete a notice that was not made by the current user.
         $this->clientError(_('Cannot delete this notice.'));
     }
     $this->formOpts['notice'] = $this->notice;
 }
Esempio n. 3
0
 protected function doPreparation()
 {
     $this->verb = $this->trimmed('verb');
     if (empty($this->verb)) {
         throw new ServerException('A verb has not been specified.');
     }
     $this->notice = Notice::getByID($this->trimmed('id'));
     if (!$this->notice->inScope($this->scoped)) {
         // 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.'), $this->scoped->getNickname(), $this->notice->getID()), 403);
     }
     Event::handle('ActivityVerbDoPreparation', array($this, $this->verb, $this->notice, $this->scoped));
 }
Esempio n. 4
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. 5
0
 public function extendActivity(Notice $stored, Activity $act, Profile $scoped = null)
 {
     // TODO: How to handle repeats of deleted notices?
     $target = Notice::getByID($stored->repeat_of);
     // TRANS: A repeat activity's title. %1$s is repeater's nickname
     //        and %2$s is the repeated user's nickname.
     $act->title = sprintf(_('%1$s repeated a notice by %2$s'), $stored->getProfile()->getNickname(), $target->getProfile()->getNickname());
     $act->objects[] = $target->asActivity($scoped);
 }
Esempio n. 6
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!');
 }
Esempio n. 7
0
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
$shortoptions = 'i::n::u::y';
$longoptions = array('id=', 'nickname=', 'uri=', 'yes');
$helptext = <<<END_OF_HELP
delete_notice.php [options]
deletes a notice (but not related File objects) from the database

  -i --id       Local ID of the notice
  -u --uri      Notice URI
  -y --yes      do not wait for confirmation

END_OF_HELP;
require_once INSTALLDIR . '/scripts/commandline.inc';
if (have_option('i', 'id')) {
    $id = get_option_value('i', 'id');
    $notice = Notice::getByID($id);
    if (!$notice instanceof Notice) {
        print "Can't find notice with ID {$id}\n";
        exit(1);
    }
} else {
    if (have_option('u', 'uri')) {
        $uri = get_option_value('u', 'uri');
        $notice = Notice::getKV('uri', $uri);
        if (!$notice instanceof Notice) {
            print "Can't find notice with URI '{$uri}'\n";
            exit(1);
        }
    } else {
        print "You must provide either an ID, a URI or a nickname.\n";
        exit(1);
Esempio n. 8
0
 public function getTarget()
 {
     return Notice::getByID($this->notice_id);
 }