Esempio n. 1
0
 /**
  * makes a plain-text formatted version of a notice, suitable for IM distribution
  *
  * @param Notice  $notice  notice being sent
  *
  * @return string plain-text version of the notice, with user nickname prefixed
  */
 protected function formatNotice(Notice $notice)
 {
     $profile = $notice->getProfile();
     try {
         $parent = $notice->getParent();
         $orig_profile = $parent->getProfile();
         $nicknames = sprintf('%1$s => %2$s', $profile->nickname, $orig_profile->nickname);
     } catch (NoParentNoticeException $e) {
         $nicknames = $profile->nickname;
     }
     return sprintf('%1$s: %2$s [%3$u]', $nicknames, $notice->content, $notice->id);
 }
Esempio n. 2
0
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
// Abort if called from a web server
if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
    print "This script must be run from the command line\n";
    exit;
}
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
define('GNUSOCIAL', true);
define('STATUSNET', true);
// compatibility
require_once INSTALLDIR . '/lib/common.php';
common_log(LOG_INFO, 'Starting to do old notices.');
$notice = new Notice();
$cnt = $notice->find();
while ($notice->fetch()) {
    common_log(LOG_INFO, 'Getting tags for notice #' . $notice->id);
    $notice->saveTags();
    $original = clone $notice;
    $notice->rendered = common_render_content($notice->content, $notice->getProfile(), $notice->hasParent() ? $notice->getParent() : null);
    $result = $notice->update($original);
    if (!$result) {
        common_log_db_error($notice, 'UPDATE', __FILE__);
    }
}
Esempio n. 3
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. 4
0
 /**
  * extra information for XMPP messages, as defined by Twitter
  *
  * @param Profile $profile Profile of the sending user
  * @param Notice  $notice  Notice being sent
  *
  * @return string Extra information (Atom, HTML, addresses) in string format
  */
 protected function format_entry(Notice $notice)
 {
     $profile = $notice->getProfile();
     $entry = $notice->asAtomEntry(true, true);
     $xs = new XMLStringer();
     $xs->elementStart('html', array('xmlns' => 'http://jabber.org/protocol/xhtml-im'));
     $xs->elementStart('body', array('xmlns' => 'http://www.w3.org/1999/xhtml'));
     $xs->element('a', array('href' => $profile->profileurl), $profile->nickname);
     try {
         $parent = $notice->getParent();
         $orig_profile = $parent->getProfile();
         $orig_profurl = $orig_profile->getUrl();
         $xs->text(" => ");
         $xs->element('a', array('href' => $orig_profurl), $orig_profile->nickname);
         $xs->text(": ");
     } catch (InvalidUrlException $e) {
         $xs->text(sprintf(' => %s', $orig_profile->nickname));
     } catch (NoParentNoticeException $e) {
         $xs->text(": ");
     }
     if (!empty($notice->rendered)) {
         $notice->rendered = str_replace("\t", "", $notice->rendered);
         $xs->raw($notice->rendered);
     } else {
         $xs->raw(common_render_content($notice->content, $notice));
     }
     $xs->text(" ");
     $xs->element('a', array('href' => common_local_url('conversation', array('id' => $notice->conversation)) . '#notice-' . $notice->id), sprintf(_m('[%u]'), $notice->id));
     $xs->elementEnd('body');
     $xs->elementEnd('html');
     $html = $xs->getString();
     return $html . ' ' . $entry;
 }
Esempio n. 5
0
 /**
  * extra information for XMPP messages, as defined by Twitter
  *
  * @param Profile $profile Profile of the sending user
  * @param Notice  $notice  Notice being sent
  *
  * @return string Extra information (Atom, HTML, addresses) in string format
  */
 protected function format_entry(Notice $notice)
 {
     $profile = $notice->getProfile();
     $entry = $notice->asAtomEntry(true, true);
     $xs = new XMLStringer();
     $xs->elementStart('html', array('xmlns' => 'http://jabber.org/protocol/xhtml-im'));
     $xs->elementStart('body', array('xmlns' => 'http://www.w3.org/1999/xhtml'));
     $xs->element('a', array('href' => $profile->profileurl), $profile->nickname);
     try {
         $parent = $notice->getParent();
         $orig_profile = $parent->getProfile();
         $orig_profurl = $orig_profile->getUrl();
         $xs->text(" => ");
         $xs->element('a', array('href' => $orig_profurl), $orig_profile->nickname);
         $xs->text(": ");
     } catch (InvalidUrlException $e) {
         $xs->text(sprintf(' => %s', $orig_profile->nickname));
     } catch (NoParentNoticeException $e) {
         $xs->text(": ");
     } catch (NoResultException $e) {
         // Parent notice was probably deleted.
         $xs->text(": ");
     }
     // FIXME: Why do we replace \t with ''? is it just to make it pretty? shouldn't whitespace be handled well...?
     $xs->raw(str_replace("\t", "", $notice->getRendered()));
     $xs->text(" ");
     $xs->element('a', array('href' => common_local_url('conversation', array('id' => $notice->conversation)) . '#notice-' . $notice->id), sprintf(_m('[%u]'), $notice->id));
     $xs->elementEnd('body');
     $xs->elementEnd('html');
     $html = $xs->getString();
     return $html . ' ' . $entry;
 }
Esempio n. 6
0
/**
 * Find @-mentions in the given text, using the given notice object as context.
 * References will be resolved with common_relative_profile() against the user
 * who posted the notice.
 *
 * Note the return data format is internal, to be used for building links and
 * such. Should not be used directly; rather, call common_linkify_mentions().
 *
 * @param string $text
 * @param Notice $notice notice in whose context we're building links
 *
 * @return array
 *
 * @access private
 */
function common_find_mentions($text, Notice $notice)
{
    // The getProfile call throws NoProfileException on failure
    $sender = $notice->getProfile();
    $mentions = array();
    if (Event::handle('StartFindMentions', array($sender, $text, &$mentions))) {
        // Get the context of the original notice, if any
        $origAuthor = null;
        $origNotice = null;
        $origMentions = array();
        // Is it a reply?
        if ($notice instanceof Notice) {
            try {
                $origNotice = $notice->getParent();
                $origAuthor = $origNotice->getProfile();
                $ids = $origNotice->getReplies();
                foreach ($ids as $id) {
                    $repliedTo = Profile::getKV('id', $id);
                    if ($repliedTo instanceof Profile) {
                        $origMentions[$repliedTo->nickname] = $repliedTo;
                    }
                }
            } catch (NoProfileException $e) {
                common_log(LOG_WARNING, sprintf('Notice %d author profile id %d does not exist', $origNotice->id, $origNotice->profile_id));
            } catch (NoParentNoticeException $e) {
                // This notice is not in reply to anything
            } catch (Exception $e) {
                common_log(LOG_WARNING, __METHOD__ . ' got exception ' . get_class($e) . ' : ' . $e->getMessage());
            }
        }
        $matches = common_find_mentions_raw($text);
        foreach ($matches as $match) {
            try {
                $nickname = Nickname::normalize($match[0]);
            } catch (NicknameException $e) {
                // Bogus match? Drop it.
                continue;
            }
            // Try to get a profile for this nickname.
            // Start with conversation context, then go to
            // sender context.
            if ($origAuthor instanceof Profile && $origAuthor->nickname == $nickname) {
                $mentioned = $origAuthor;
            } else {
                if (!empty($origMentions) && array_key_exists($nickname, $origMentions)) {
                    $mentioned = $origMentions[$nickname];
                } else {
                    $mentioned = common_relative_profile($sender, $nickname);
                }
            }
            if ($mentioned instanceof Profile) {
                $user = User::getKV('id', $mentioned->id);
                if ($user instanceof User) {
                    $url = common_local_url('userbyid', array('id' => $user->id));
                } else {
                    $url = $mentioned->profileurl;
                }
                $mention = array('mentioned' => array($mentioned), 'type' => 'mention', 'text' => $match[0], 'position' => $match[1], 'url' => $url);
                if (!empty($mentioned->fullname)) {
                    $mention['title'] = $mentioned->fullname;
                }
                $mentions[] = $mention;
            }
        }
        // @#tag => mention of all subscriptions tagged 'tag'
        preg_match_all('/(?:^|[\\s\\.\\,\\:\\;]+)@#([\\pL\\pN_\\-\\.]{1,64})/', $text, $hmatches, PREG_OFFSET_CAPTURE);
        foreach ($hmatches[1] as $hmatch) {
            $tag = common_canonical_tag($hmatch[0]);
            $plist = Profile_list::getByTaggerAndTag($sender->id, $tag);
            if (!$plist instanceof Profile_list || $plist->private) {
                continue;
            }
            $tagged = $sender->getTaggedSubscribers($tag);
            $url = common_local_url('showprofiletag', array('tagger' => $sender->nickname, 'tag' => $tag));
            $mentions[] = array('mentioned' => $tagged, 'type' => 'list', 'text' => $hmatch[0], 'position' => $hmatch[1], 'url' => $url);
        }
        preg_match_all('/(?:^|[\\s\\.\\,\\:\\;]+)!(' . Nickname::DISPLAY_FMT . ')/', $text, $hmatches, PREG_OFFSET_CAPTURE);
        foreach ($hmatches[1] as $hmatch) {
            $nickname = Nickname::normalize($hmatch[0]);
            $group = User_group::getForNickname($nickname, $sender);
            if (!$group instanceof User_group || !$sender->isMember($group)) {
                continue;
            }
            $profile = $group->getProfile();
            $mentions[] = array('mentioned' => array($profile), 'type' => 'group', 'text' => $hmatch[0], 'position' => $hmatch[1], 'url' => $group->permalink(), 'title' => $group->getFancyName());
        }
        Event::handle('EndFindMentions', array($sender, $text, &$mentions));
    }
    return $mentions;
}
Esempio n. 7
0
 /**
  * makes a plain-text formatted version of a notice, suitable for IM distribution
  *
  * @param Notice  $notice  notice being sent
  *
  * @return string plain-text version of the notice, with user nickname prefixed
  */
 protected function formatNotice(Notice $notice)
 {
     $profile = $notice->getProfile();
     $nicknames = $profile->getNickname();
     try {
         $parent = $notice->getParent();
         $orig_profile = $parent->getProfile();
         $nicknames = sprintf('%1$s => %2$s', $profile->getNickname(), $orig_profile->getNickname());
     } catch (NoParentNoticeException $e) {
         // Not a reply, no parent notice stored
     } catch (NoResultException $e) {
         // Parent notice was probably deleted
     }
     return sprintf('%1$s: %2$s [%3$u]', $nicknames, $notice->content, $notice->id);
 }