예제 #1
0
 /**
  * Test on the regex matching used in common_find_mentions
  * (testing on the full notice rendering is difficult as it needs
  * to be able to pull from global state)
  *
  * @dataProvider provider
  */
 public function testAtReply($input, $expected, $expectedException = null)
 {
     if ($expected == false) {
         // nothing to do
     } else {
         $text = "@{$input} awesome! :)";
         $matches = common_find_mentions_raw($text);
         $this->assertEquals(1, count($matches));
         $this->assertEquals($expected, Nickname::normalize($matches[0][0]));
     }
 }
예제 #2
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)
{
    $mentions = array();
    $sender = Profile::staticGet('id', $notice->profile_id);
    if (empty($sender)) {
        return $mentions;
    }
    if (Event::handle('StartFindMentions', array($sender, $text, &$mentions))) {
        // Get the context of the original notice, if any
        $originalAuthor = null;
        $originalNotice = null;
        $originalMentions = array();
        // Is it a reply?
        if (!empty($notice) && !empty($notice->reply_to)) {
            $originalNotice = Notice::staticGet('id', $notice->reply_to);
            if (!empty($originalNotice)) {
                $originalAuthor = Profile::staticGet('id', $originalNotice->profile_id);
                $ids = $originalNotice->getReplies();
                foreach ($ids as $id) {
                    $repliedTo = Profile::staticGet('id', $id);
                    if (!empty($repliedTo)) {
                        $originalMentions[$repliedTo->nickname] = $repliedTo;
                    }
                }
            }
        }
        $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 (!empty($originalAuthor) && $originalAuthor->nickname == $nickname) {
                $mentioned = $originalAuthor;
            } else {
                if (!empty($originalMentions) && array_key_exists($nickname, $originalMentions)) {
                    $mentioned = $originalMentions[$nickname];
                } else {
                    $mentioned = common_relative_profile($sender, $nickname);
                }
            }
            if (!empty($mentioned)) {
                $user = User::staticGet('id', $mentioned->id);
                if ($user) {
                    $url = common_local_url('userbyid', array('id' => $user->id));
                } else {
                    $url = $mentioned->profileurl;
                }
                $mention = array('mentioned' => array($mentioned), '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 (!empty($plist) && !$plist->private) {
                $tagged = $sender->getTaggedSubscribers($tag);
                $url = common_local_url('showprofiletag', array('tagger' => $sender->nickname, 'tag' => $tag));
                $mentions[] = array('mentioned' => $tagged, 'text' => $hmatch[0], 'position' => $hmatch[1], 'url' => $url);
            }
        }
        Event::handle('EndFindMentions', array($sender, $text, &$mentions));
    }
    return $mentions;
}
예제 #3
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 Profile   $sender the Profile that is sending the current text
 * @param Notice    $parent the Notice this text is in reply to, if any
 *
 * @return array
 *
 * @access private
 */
function common_find_mentions($text, Profile $sender, Notice $parent = null)
{
    $mentions = array();
    if (Event::handle('StartFindMentions', array($sender, $text, &$mentions))) {
        // Get the context of the original notice, if any
        $origMentions = array();
        // Does it have a parent notice for context?
        if ($parent instanceof Notice) {
            $ids = $parent->getReplies();
            // replied-to _profile ids_
            foreach ($ids as $id) {
                try {
                    $repliedTo = Profile::getByID($id);
                    $origMentions[$repliedTo->getNickname()] = $repliedTo;
                } catch (NoResultException $e) {
                    // continue foreach
                }
            }
        }
        $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 ($parent instanceof Notice && $parent->getProfile()->getNickname() === $nickname) {
                $mentioned = $parent->getProfile();
            } else {
                if (!empty($origMentions) && array_key_exists($nickname, $origMentions)) {
                    $mentioned = $origMentions[$nickname];
                } else {
                    // sets to null if no match
                    $mentioned = common_relative_profile($sender, $nickname);
                }
            }
            if ($mentioned instanceof Profile) {
                $user = User::getKV('id', $mentioned->id);
                try {
                    $url = $mentioned->getUrl();
                } catch (InvalidUrlException $e) {
                    $url = common_local_url('userbyid', array('id' => $mentioned->getID()));
                }
                $mention = array('mentioned' => array($mentioned), 'type' => 'mention', 'text' => $match[0], 'position' => $match[1], 'length' => mb_strlen($match[0]), 'title' => $mentioned->getFullname(), 'url' => $url);
                $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->getID(), $tag);
            if (!$plist instanceof Profile_list || $plist->private) {
                continue;
            }
            $tagged = $sender->getTaggedSubscribers($tag);
            $url = common_local_url('showprofiletag', array('nickname' => $sender->getNickname(), 'tag' => $tag));
            $mentions[] = array('mentioned' => $tagged, 'type' => 'list', 'text' => $hmatch[0], 'position' => $hmatch[1], 'length' => mb_strlen($hmatch[0]), '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], 'length' => mb_strlen($hmatch[0]), 'url' => $group->permalink(), 'title' => $group->getFancyName());
        }
        Event::handle('EndFindMentions', array($sender, $text, &$mentions));
    }
    return $mentions;
}
예제 #4
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;
}
예제 #5
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 (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;
     $notice->content_type = NOTICE::CONTENT_TYPE_POST;
     // default value
     if (!empty($content_type)) {
         $notice->content_type = $content_type;
     }
     $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;
         $notice->content_type = NOTICE::CONTENT_TYPE_REPEAT;
     } 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;
             $notice->content_type = NOTICE::CONTENT_TYPE_COMMENT;
             // 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;
             }
         }
     }
     //dyg
     $matches = common_find_mentions_raw($notice->content);
     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.
         $mentioned = common_relative_profile($profile, $nickname);
         if ($mentioned) {
             $notice->content_type |= NOTICE::CONTENT_TYPE_MENTIONS;
             break;
         }
     }
     //end
     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 (!empty($notice->reply_to)) {
         $notice->addCommentCount($notice->reply_to);
     }
     if (!empty($repeat_of)) {
         $notice->addRepostCount($repeat_of);
     }
     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;
 }