Beispiel #1
0
		function getRemoteFeed($url, $depth=0) {
			global $db;
			if($depth>3) {
				return array(2, null, null);
			}
			requireComponent('LZ.PHP.HTTPRequest');
			$request = new HTTPRequest();
			$xml = $request->getPage($url);
			if (empty($xml)) {
				return array(2, null, null);
			}
			$feed = array('xmlURL' => $url);
			
			$encoding = '';
			if (preg_match('/^<\?xml[^<]*\s+encoding=["\']?([\w-]+)["\']?/', $xml, $matches))
				$encoding = $matches[1];
			if (strcasecmp($encoding, 'utf-8') != 0) {
				$xml = UTF8::bring($xml, $encoding);
				$xml = preg_replace('/^(<\?xml[^<]*\s+encoding=)["\']?[\w-]+["\']?/', '$1"utf-8"', $xml, 1);
			}

			if(preg_match_all('/<meta[ \t].*?http-equiv\s*=\s*[\'"]?refresh.*?>/i', $xml, $matches)) { // 야후코리아 때문 ..
				foreach($matches[0] as $link) {
					$attributes = func::getAttributesFromString($link);
					if (isset($attributes['content'])) {
						$attributes = explode(';', $attributes['content']);
						$contentURL = substr($attributes[1],4);
						if(substr($contentURL,0,7) == 'http://') {
							$url = $contentURL;
						} else {
							$url = func::unionAddress($url,$contentURL);
						}
						if(!empty($url)) {
							return Feed::getRemoteFeed($url, ++$depth);
						}
					}
				}
			}

			$xmls = new XMLStruct();
			if (!$xmls->open($xml)) {
				if(preg_match_all('/<link .*?rel\s*=\s*[\'"]?alternate.*?>/i', $xml, $matches)) {
					foreach($matches[0] as $link) {
						$attributes = func::getAttributesFromString($link);
						if (isset($attributes['href'])) {
							$urlInfo = parse_url($url);
							$rssInfo = parse_url($attributes['href']);
							$rssURL = false;
							if (isset($rssInfo['scheme']) && $rssInfo['scheme'] == 'http')
								$rssURL = $attributes['href'];
							else if (isset($rssInfo['path'])) {
								if ($rssInfo['path']{0} == '/')
									$rssURL = "{$urlInfo['scheme']}://{$urlInfo['host']}{$rssInfo['path']}";							
								else
									$rssURL = "{$urlInfo['scheme']}://{$urlInfo['host']}".(isset($urlInfo['path']) ? rtrim($urlInfo['path'], '/') : '').'/'.$rssInfo['path'];
							}
							if ($rssURL && $url != $rssURL)
								return Feed::getRemoteFeed($rssURL);
						}
					}
				}		
				return array(3, null, null);
			}

			$xmlType = '';
			$feed['blogTool'] = Func::isWhatBlog($url);
			if ($xmls->getAttribute('/rss', 'version')) {	
				$xmlType = 'rss';
				$feed['blogURL'] = $xmls->getValue('/rss/channel/link');
				$feed['title'] = $xmls->getValue('/rss/channel/title');
				$feed['description'] = $xmls->getValue('/rss/channel/description');
				if (Validator::language($xmls->getValue('/rss/channel/language')))
					$feed['language'] = $xmls->getValue('/rss/channel/language');
				else if (Validator::language($xmls->getValue('/rss/channel/dc:language')))
					$feed['language'] = $xmls->getValue('/rss/channel/dc:language');
				else
					$feed['language'] = 'en-US';
				$feed['modified'] = gmmktime();
				$feed['logo'] = $xmls->getValue('/rss/channel/image/url');
			} else if ($xmls->doesExist('/feed')) {
				$xmlType = 'atom';
				$feed['blogURL'] = $xmls->getAttribute('/feed/link', 'href');
				$feed['title'] = $xmls->getValue('/feed/title');
				if (!$feed['description'] = $xmls->getValue('/feed/tagline'))
					$feed['description'] = $xmls->getValue('/feed/subtitle');
				if (Validator::language($xmls->getAttribute('/feed', 'xml:lang')))
					$feed['language'] = $xmls->getAttribute('/feed', 'xml:lang');
				else
					$feed['language'] = 'en-US';
				$feed['modified'] = gmmktime();
			} else if ($xmls->getAttribute('/rdf:RDF', 'xmlns')) {
				$xmlType = 'rss';
				if ($xmls->getAttribute('/rdf:RDF/channel/link', 'href'))
					$feed['blogURL'] = $xmls->getAttribute('/rdf:RDF/channel/link', 'href');
				else if ($xmls->getValue('/rdf:RDF/channel/link'))
					$feed['blogURL'] = $xmls->getValue('/rdf:RDF/channel/link');
				else
					$feed['blogURL'] = '';
				$feed['title'] = $xmls->getValue('/rdf:RDF/channel/title');
				$feed['description'] = $xmls->getValue('/rdf:RDF/channel/description');
				if (Validator::language($xmls->getValue('/rdf:RDF/channel/dc:language')))
					$feed['language'] = $xmls->getValue('/rdf:RDF/channel/dc:language');
				else if (Validator::language($xmls->getAttribute('/rdf:RDF', 'xml:lang')))
					$feed['language'] = $xmls->getAttribute('/rdf:RDF', 'xml:lang');
				else
					$feed['language'] = 'en-US';
				$feed['modified'] = gmmktime();
			} else
				return array(3, null, null, null);
			
			$feed['xmlURL'] = $db->escape($db->lessen(UTF8::correct($feed['xmlURL'])));
			$feed['xmlType'] = $xmlType;
			$feed['blogURL'] = $db->escape($db->lessen(UTF8::correct($feed['blogURL'])));
			$feed['title'] = (empty($feed['title']))?_t('제목없음'):$db->escape($db->lessen(UTF8::correct($feed['title'])));
			$feed['description'] = $db->escape($db->lessen(UTF8::correct(func::stripHTML($feed['description']))));
			$feed['language'] = $db->escape($db->lessen(UTF8::correct($feed['language']), 255));

			return array(0, $feed, $xml, $xmlType);
		}