示例#1
0
 /**
  * Given a URL, returns the page title.
  * @param $Url
  * @return mixed
  */
 function getTitleFromURL($Url)
 {
     $str = \Idno\Core\Webservice::file_get_contents($Url);
     if (strlen($str) > 0) {
         preg_match("/\\<title\\>(.*)\\<\\/title\\>/siu", $str, $title);
         return htmlspecialchars_decode($title[1]);
     }
     return '';
 }
示例#2
0
文件: Like.php 项目: avewrigley/idno
 /**
  * Given a URL, returns the page title.
  * @param $Url
  * @return mixed
  */
 function getTitleFromURL($Url)
 {
     $str = \Idno\Core\Webservice::file_get_contents($Url);
     //@file_get_contents($Url);
     if (strlen($str) > 0) {
         preg_match("/\\<title\\>(.*)\\<\\/title\\>/i", $str, $title);
         return $title[1];
     }
     return '';
 }
示例#3
0
 /**
  * Find the self resource.
  * This method will find a link self on a feed, finding the feed first
  * @param type $url
  */
 private function findSelf($url)
 {
     $self = null;
     $feed = null;
     // Find RSS
     $feed = $this->findFeed($url);
     // Find self
     if ($feed) {
         $data = \Idno\Core\Webservice::file_get_contents($feed);
         if (preg_match('/<atom:link[^>]+href="([^"]+)"[^>]*rel="self"[^>]*>/i', $data, $match)) {
             $self = $match[1];
         }
         if (preg_match('/<atom:link[^>]+rel="self"[^>]*href="([^"]+)"[^>]*>/i', $data, $match)) {
             $self = $match[1];
         }
     }
     return $self;
 }
示例#4
0
 /**
  * Recursive helper function for parsing webmentions.
  *
  * @param $item
  * @param $mentions
  * @return array
  */
 function addWebmentionItem($item, $mentions, $source, $target, $title = '')
 {
     if (!empty($item['properties']['author'])) {
         foreach ($item['properties']['author'] as $author) {
             if (!empty($author['type'])) {
                 foreach ($author['type'] as $type) {
                     if ($type == 'h-card') {
                         if (!empty($author['properties']['name'])) {
                             $mentions['owner']['name'] = $author['properties']['name'][0];
                         }
                         if (!empty($author['properties']['url'])) {
                             $mentions['owner']['url'] = $author['properties']['url'][0];
                         }
                         if (!empty($author['properties']['photo'])) {
                             //$mentions['owner']['photo'] = $author['properties']['photo'][0];
                             $tmpfname = tempnam(sys_get_temp_dir(), 'webmention_avatar');
                             file_put_contents($tmpfname, \Idno\Core\Webservice::file_get_contents($author['properties']['photo'][0]));
                             $name = md5($author['properties']['url'][0]);
                             // TODO: Don't update the cache image for every webmention
                             if ($icon = \Idno\Entities\File::createThumbnailFromFile($tmpfname, $name, 300)) {
                                 $mentions['owner']['photo'] = \Idno\Core\Idno::site()->config()->url . 'file/' . (string) $icon;
                             } else {
                                 if ($icon = \Idno\Entities\File::createFromFile($tmpfname, $name)) {
                                     $mentions['owner']['photo'] = \Idno\Core\Idno::site()->config()->url . 'file/' . (string) $icon;
                                 }
                             }
                             unlink($tmpfname);
                         }
                     }
                 }
             }
         }
     }
     if (!empty($item['type'])) {
         if (in_array('h-entry', $item['type'])) {
             $mention = array();
             if (!empty($item['properties'])) {
                 if (!empty($item['properties']['content'])) {
                     $mention['content'] = '';
                     if (is_array($item['properties']['content'])) {
                         foreach ($item['properties']['content'] as $content) {
                             if (!empty($content['value'])) {
                                 $parsed_content = \Idno\Core\Idno::site()->template()->sanitize_html($content['value']);
                                 if (!substr_count($mention['content'], $parsed_content)) {
                                     $mention['content'] .= $parsed_content;
                                 }
                             }
                         }
                     } else {
                         $mention['content'] = $item['properties']['content'];
                     }
                 } else {
                     if (!empty($item['properties']['summary'])) {
                         if (is_array($item['properties']['summary'])) {
                             $mention['content'] = \Idno\Core\Idno::site()->template()->sanitize_html(implode(' ', $item['properties']['summary']));
                         } else {
                             $mention['content'] = $item['properties']['summary'];
                         }
                     } else {
                         if (!empty($item['properties']['name'])) {
                             if (is_array($item['properties']['name'])) {
                                 $mention['content'] = \Idno\Core\Idno::site()->template()->sanitize_html(implode(' ', $item['properties']['name']));
                             } else {
                                 $mention['content'] = $item['properties']['name'];
                             }
                         }
                     }
                 }
                 if (!empty($item['properties']['published'])) {
                     if (is_array($item['properties']['published'])) {
                         $mention['created'] = @strtotime(array_shift(array_pop($item['properties']['published'])));
                     } else {
                         $mention['created'] = @strtotime($item['properties']['content']);
                     }
                 }
                 if (empty($mention['created'])) {
                     $mention['created'] = time();
                 }
                 if (!empty($item['properties']['url'])) {
                     if (!empty($item['properties']['uid'])) {
                         $mention['url'] = array_intersect($item['properties']['uid'], $item['properties']['url']);
                     }
                     if (empty($mention['url'])) {
                         $mention['url'] = $item['properties']['url'];
                     }
                 }
                 if (!empty($item['properties']['like']) && is_array($item['properties']['like'])) {
                     if (in_array($target, static::getStringURLs($item['properties']['like']))) {
                         $mention['type'] = 'like';
                     }
                 }
                 if (!empty($item['properties']['like-of']) && is_array($item['properties']['like-of'])) {
                     if (in_array($target, static::getStringURLs($item['properties']['like-of']))) {
                         $mention['type'] = 'like';
                     }
                 }
                 if (!empty($item['properties']['rsvp']) && is_array($item['properties']['rsvp'])) {
                     $mention['type'] = 'rsvp';
                     $mention['content'] = implode(' ', $item['properties']['rsvp']);
                 }
                 foreach (array('share', 'repost', 'repost-of') as $verb) {
                     if (!empty($item['properties'][$verb]) && is_array($item['properties'][$verb])) {
                         if (in_array($target, static::getStringURLs($item['properties'][$verb]))) {
                             $mention['type'] = 'share';
                         }
                     }
                 }
                 if (!empty($item['properties']['in-reply-to']) && is_array($item['properties']['in-reply-to'])) {
                     if (in_array($target, static::getStringURLs($item['properties']['in-reply-to']))) {
                         $mention['type'] = 'reply';
                     }
                 }
                 if (empty($mention['type'])) {
                     $mention['type'] = 'mention';
                 }
             }
             if (empty($mention['content'])) {
                 $mention['content'] = '';
             }
             $mention['title'] = $title;
             if (!empty($mention['type'])) {
                 $mentions['mentions'][] = $mention;
             }
         }
     }
     if (in_array('h-feed', $item['type'])) {
         if (!empty($item['children'])) {
             foreach ($item['children'] as $child) {
                 $mentions = $this->addWebmentionItem($child, $mentions, $source, $target, $title);
             }
         }
     }
     return $mentions;
 }
示例#5
0
 /**
  * Retrieve a user's icon from a given homepage
  * @param $content The content of the page
  * @param $url The URL of the page
  * @return $icon_url
  */
 static function getIconFromWebsiteContent($content, $url)
 {
     if ($mf2 = self::parseContent($content, $url)) {
         $mf2 = (array) $mf2;
         foreach ($mf2['items'] as $item) {
             // Figure out what kind of Microformats 2 item we have
             if (!empty($item['type']) && is_array($item['type'])) {
                 foreach ($item['type'] as $type) {
                     switch ($type) {
                         case 'h-card':
                             if (!empty($item['properties'])) {
                                 if (!empty($item['properties']['name'])) {
                                     $mentions['owner']['name'] = $item['properties']['name'][0];
                                 }
                                 if (!empty($item['properties']['url'])) {
                                     $mentions['owner']['url'] = $item['properties']['url'][0];
                                 }
                                 if (!empty($item['properties']['photo'])) {
                                     //$mentions['owner']['photo'] = $item['properties']['photo'][0];
                                     $tmpfname = tempnam(sys_get_temp_dir(), 'webmention_avatar');
                                     file_put_contents($tmpfname, \Idno\Core\Webservice::file_get_contents($item['properties']['photo'][0]));
                                     $name = md5($item['properties']['url'][0]);
                                     // TODO: Don't update the cache image for every webmention
                                     if ($icon = \Idno\Entities\File::createThumbnailFromFile($tmpfname, $name, 300)) {
                                         return \Idno\Core\Idno::site()->config()->url . 'file/' . (string) $icon;
                                     } else {
                                         if ($icon = \Idno\Entities\File::createFromFile($tmpfname, $name)) {
                                             return \Idno\Core\Idno::site()->config()->url . 'file/' . (string) $icon;
                                         }
                                     }
                                     unlink($tmpfname);
                                 }
                             }
                             break;
                     }
                 }
             }
         }
     }
     return false;
 }
示例#6
0
 private function parseHCard($hcard)
 {
     $owner = [];
     if (!empty($hcard['properties']['name'])) {
         $owner['name'] = $hcard['properties']['name'][0];
     }
     if (!empty($hcard['properties']['url'])) {
         $owner['url'] = $hcard['properties']['url'][0];
     }
     if (!empty($hcard['properties']['photo'])) {
         //$owner['photo'] = $hcard['properties']['photo'][0];
         $tmpfname = tempnam(sys_get_temp_dir(), 'webmention_avatar');
         file_put_contents($tmpfname, \Idno\Core\Webservice::file_get_contents($hcard['properties']['photo'][0]));
         $name = md5($hcard['properties']['url'][0]);
         // TODO: Don't update the cache image for every webmention
         if ($icon = \Idno\Entities\File::createThumbnailFromFile($tmpfname, $name, 300)) {
             $owner['photo'] = \Idno\Core\Idno::site()->config()->url . 'file/' . (string) $icon;
         } else {
             if ($icon = \Idno\Entities\File::createFromFile($tmpfname, $name)) {
                 $owner['photo'] = \Idno\Core\Idno::site()->config()->url . 'file/' . (string) $icon;
             }
         }
         unlink($tmpfname);
     }
     return $owner;
 }