/** * Search the given url for a fead or a link to it * @param $url String the url the fead should be included * @return Array|null */ public function searchFead($url) { $content = $this->getContent($url); if (!$content) { return null; } $data = $content['data']; $url = $content['url']; // rss-fead if (strpos($data, '<rss')) { $titleStart = strpos($data, '>', strpos($data, '<title')) + 1; return [['url' => $content['url'], 'title' => substr($data, $titleStart, strpos($data, '</title>', $titleStart) - $titleStart), 'type' => 'rss']]; } else { if (strpos($data, '<feed')) { $titleStart = strpos($data, '>', strpos($data, '<title')) + 1; return [['url' => $content['url'], 'title' => substr($data, $titleStart, strpos($data, '</title>', $titleStart) - $titleStart), 'type' => 'atom']]; } else { if (strpos(strtolower($data), '<!doctype') || strpos($data, '<html')) { // take only head $data = substr($data, strpos($data, '<head'), strpos($data, '</head>') + 7); $document = new DOMDocument(); $document->loadHTML($data); $feads = []; foreach ($document->getElementsByTagName('link') as $element) { $type = $element->getAttributeNode('rel')->nodeValue; if ('alternate' === $type) { if ('application/rss+xml' === $element->getAttributeNode('type')->nodeValue) { $type = 'rss'; } else { if ('application/atom+xml' === $element->getAttributeNode('type')->nodeValue) { $type = 'atom'; } } $linkUrl = $element->getAttributeNode('href')->nodeValue; if (0 === strpos($linkUrl, '/')) { $realUrl = parse_url($url); if (is_array($realUrl)) { $scheme = $realUrl['scheme']; if (is_null($scheme)) { $scheme = 'http'; } $linkUrl = $scheme . '://' . $realUrl['host'] . $linkUrl; } } $title = $element->getAttributeNode('title')->nodeValue; if (is_null($title) || 0 === strlen($title)) { $title = $type . ' fead'; } $fead = new Application_Model_Entity_Fead(); $fead->setUrl($linkUrl); $fead->setType($type); $fead->setTitle($title); array_push($feads, ['url' => $linkUrl, 'title' => $title, 'type' => $type]); } } if ([] === $feads) { return null; } return $feads; } } } }