X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=lib%2Futil.php;h=3b5bb2de08aa2dde81822e7fd27d2b49b52ab7a3;hb=3251ef3b518ef9db55fbab0693d8b0f654dcda58;hp=b842bbaa93e75d38f84b7af25bcf756109116f09;hpb=21979bb7d75ddbc0edfd529779a6f368bd0df7d5;p=quix0rs-gnu-social.git diff --git a/lib/util.php b/lib/util.php index b842bbaa93..3b5bb2de08 100644 --- a/lib/util.php +++ b/lib/util.php @@ -606,14 +606,15 @@ function common_remove_unicode_formatting($text) /** * Partial notice markup rendering step: build links to !group references. * - * @param string $text partially rendered HTML - * @param Notice $notice in whose context we're working + * @param string $text partially rendered HTML + * @param Profile $author the Profile that is composing the current notice + * @param Notice $parent the Notice this is sent in reply to, if any * @return string partially rendered HTML */ -function common_render_content($text, Notice $notice) +function common_render_content($text, Profile $author, Notice $parent=null) { $text = common_render_text($text); - $text = common_linkify_mentions($text, $notice); + $text = common_linkify_mentions($text, $author, $parent); return $text; } @@ -623,13 +624,14 @@ function common_render_content($text, Notice $notice) * * Should generally not be called except from common_render_content(). * - * @param string $text partially-rendered HTML - * @param Notice $notice in-progress or complete Notice object for context + * @param string $text partially-rendered HTML + * @param Profile $author the Profile that is composing the current notice + * @param Notice $parent the Notice this is sent in reply to, if any * @return string partially-rendered HTML */ -function common_linkify_mentions($text, Notice $notice) +function common_linkify_mentions($text, Profile $author, Notice $parent=null) { - $mentions = common_find_mentions($text, $notice); + $mentions = common_find_mentions($text, $author, $parent); // We need to go through in reverse order by position, // so our positions stay valid despite our fudging with the @@ -687,33 +689,25 @@ function common_linkify_mention(array $mention) * 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 + * @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, Notice $notice) +function common_find_mentions($text, Profile $sender, Notice $parent=null) { - // 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? - - try { - $origNotice = $notice->getParent(); - $origAuthor = $origNotice->getProfile(); - - $ids = $origNotice->getReplies(); + // Does it have a parent notice for context? + if ($parent instanceof Notice) { + $ids = $parent->getReplies(); // replied-to _profile ids_ foreach ($ids as $id) { try { @@ -723,62 +717,49 @@ function common_find_mentions($text, Notice $notice) // continue foreach } } - } catch (NoParentNoticeException $e) { - // It wasn't a reply to anything, so we can't harvest nickname-relations. } $matches = common_find_mentions_raw($text); foreach ($matches as $match) { - // Try to process it as @URL - $url = $match[0]; - if(!common_valid_http_url($url)) { $url = 'http://' . $url; } - if(common_valid_http_url($url)) { - $mentioned = Profile::ensureFromUrl($url); - $text = mb_strlen($mentioned->nickname) <= mb_strlen($match[0]) ? $mentioned->nickname : $match[0]; - } else { - try { - $nickname = Nickname::normalize($match[0]); - } catch (NicknameException $e) { - // Bogus match? Drop it. - continue; - } + 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. + // 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); - } - $text = $match[0]; + 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); - if ($user instanceof User) { - $url = common_local_url('userbyid', array('id' => $user->id)); - } else { - $url = $mentioned->profileurl; + try { + $url = $mentioned->getUrl(); + } catch (InvalidUrlException $e) { + $url = common_local_url('userbyid', array('id' => $mentioned->getID())); } $mention = array('mentioned' => array($mentioned), 'type' => 'mention', - 'text' => $text, + 'text' => $match[0], 'position' => $match[1], 'length' => mb_strlen($match[0]), + 'title' => $mentioned->getFullname(), 'url' => $url); - if (!empty($mentioned->fullname)) { - $mention['title'] = $mentioned->fullname; - } - $mentions[] = $mention; } } @@ -789,20 +770,21 @@ function common_find_mentions($text, Notice $notice) $text, $hmatches, PREG_OFFSET_CAPTURE); foreach ($hmatches[1] as $hmatch) { $tag = common_canonical_tag($hmatch[0]); - $plist = Profile_list::getByTaggerAndTag($sender->id, $tag); + $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('tagger' => $sender->nickname, + 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); } @@ -822,6 +804,7 @@ function common_find_mentions($text, Notice $notice) 'type' => 'group', 'text' => $hmatch[0], 'position' => $hmatch[1], + 'length' => mb_strlen($hmatch[0]), 'url' => $group->permalink(), 'title' => $group->getFancyName()); } @@ -848,7 +831,7 @@ function common_find_mentions_raw($text) PREG_OFFSET_CAPTURE); $atmatches = array(); - preg_match_all('/(?:^|\s+)@((?:[A-Za-z0-9_:\-\.\/%]+)|(?:' . Nickname::DISPLAY_FMT . '))\b/', + preg_match_all('/(?:^|\s+)@(' . Nickname::DISPLAY_FMT . ')\b/', $text, $atmatches, PREG_OFFSET_CAPTURE); @@ -885,7 +868,7 @@ function common_replace_urls_callback($text, $callback, $arg = null) { '(?:'. '(?:'. //Known protocols '(?:'. - '(?:(?:https?|ftps?|mms|rtsp|gopher|news|nntp|telnet|wais|file|prospero|webcal|irc)://)'. + '(?:(?:https?|ftps?|mms|rtsp|gopher|news|nntp|telnet|wais|file|prospero|webcal|ircs?)://)'. '|'. '(?:(?:mailto|aim|tel|xmpp):)'. ')'. @@ -995,20 +978,9 @@ function common_linkify($url) { $canon = "mailto:$url"; $longurl = "mailto:$url"; } else { - $canon = File_redirection::_canonUrl($url); - $longurl_data = File_redirection::where($canon, common_config('attachments', 'process_links')); - if (is_array($longurl_data)) { - $longurl = $longurl_data['url']; - } elseif (is_string($longurl_data)) { - $longurl = $longurl_data; - } else { - // Unable to reach the server to verify contents, etc - // Just pass the link on through for now. - common_log(LOG_ERR, "Can't linkify url '$url'"); - $longurl = $url; - } + $longurl = $longurl_data->url; } $attrs = array('href' => $canon, 'title' => $longurl); @@ -1565,14 +1537,24 @@ function common_root_url($ssl=false) } /** - * returns $bytes bytes of random data as a hexadecimal string + * returns $bytes bytes of raw random data */ -function common_random_hexstr($bytes) +function common_random_rawstr($bytes) { - $str = @file_exists('/dev/urandom') + $rawstr = @file_exists('/dev/urandom') ? common_urandom($bytes) : common_mtrand($bytes); + return $rawstr; +} + +/** + * returns $bytes bytes of random data as a hexadecimal string + */ +function common_random_hexstr($bytes) +{ + $str = common_random_rawstr($bytes); + $hexstr = ''; for ($i = 0; $i < $bytes; $i++) { $hexstr .= sprintf("%02x", ord($str[$i])); @@ -1876,6 +1858,7 @@ function common_get_mime_media($type) return strtolower($tmp[0]); } +// Get only the mimetype and not additional info (separated from bare mime with semi-colon) function common_bare_mime($mimetype) { $mimetype = mb_strtolower($mimetype); @@ -2440,62 +2423,6 @@ function common_strip_html($html, $trim=true, $save_whitespace=false) return $trim ? trim($text) : $text; } -function common_representative_hcard($url, $fn=null, $mf2=null) { - if(!$mf2) { - $request = HTTPClient::start(); - - try { - $response = $request->get($url); - } catch(Exception $ex) { - return null; - } - - $url = $response->getEffectiveUrl(); - $mf2 = new Mf2\Parser($response->getBody(), $url); - $mf2 = $mf2->parse(); - } - - $hcard = null; - - if(!empty($mf2['items'])) { - $hcards = array(); - foreach($mf2['items'] as $item) { - if(!in_array('h-card', $item['type'])) { - continue; - } - - // We found a match, return it immediately - if(isset($item['properties']['url']) && in_array($url, $item['properties']['url'])) { - $hcard = $item['properties']; - break; - } - - // Let's keep all the hcards for later, to return one of them at least - $hcards[] = $item['properties']; - } - - // No match immediately for the url we expected, but there were h-cards found - if (count($hcards) > 0) { - $hcard = $hcards[0]; - } - } - - if(!$hcard && $fn) { - $hcard = array('name' => array($fn)); - } - - if(!$hcard && $response) { - preg_match('/([^<]+)/', $response->getBody(), $match); - $hcard = array('name' => array($match[1])); - } - - if($hcard && !$hcard['url']) { - $hcard['url'] = array($url); - } - - return $hcard; -} - function html_sprintf() { $args = func_get_args();