parse_url() public method

public parse_url ( $url )
 public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false)
 {
     if (class_exists('idna_convert')) {
         $idn = new idna_convert();
         $parsed = SimplePie_Misc::parse_url($url);
         $url = SimplePie_Misc::compress_parse_url($parsed['scheme'], $idn->encode($parsed['authority']), $parsed['path'], $parsed['query'], $parsed['fragment']);
     }
     $this->url = $url;
     $this->useragent = $useragent;
     if (preg_match('/^http(s)?:\\/\\//i', $url)) {
         if (!is_array($headers)) {
             $headers = array();
         }
         $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_CURL;
         $headers2 = array();
         foreach ($headers as $key => $value) {
             $headers2[] = "{$key}: {$value}";
         }
         //TODO: allow for HTTP headers
         // curl_setopt($fp, CURLOPT_HTTPHEADER, $headers2);
         $response = self::$agent->get($url);
         if ($response === false || !isset($response['status_code'])) {
             $this->error = 'failed to fetch URL';
             $this->success = false;
         } else {
             // The extra lines at the end are there to satisfy SimplePie's HTTP parser.
             // The class expects a full HTTP message, whereas we're giving it only
             // headers - the new lines indicate the start of the body.
             $parser = new SimplePie_HTTP_Parser($response['headers'] . "\r\n\r\n");
             if ($parser->parse()) {
                 $this->headers = $parser->headers;
                 //$this->body = $parser->body;
                 $this->body = $response['body'];
                 $this->status_code = $parser->status_code;
             }
         }
     } else {
         $this->error = 'invalid URL';
         $this->success = false;
     }
 }
示例#2
0
文件: File.php 项目: kosir/wp-pipes
 public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false)
 {
     if (class_exists('idna_convert')) {
         $idn = new idna_convert();
         $parsed = SimplePie_Misc::parse_url($url);
         $url = SimplePie_Misc::compress_parse_url($parsed['scheme'], $idn->encode($parsed['authority']), $parsed['path'], $parsed['query'], $parsed['fragment']);
     }
     $this->url = $url;
     $this->useragent = $useragent;
     if (preg_match('/^http(s)?:\\/\\//i', $url)) {
         if ($useragent === null) {
             $useragent = ini_get('user_agent');
             $this->useragent = $useragent;
         }
         if (!is_array($headers)) {
             $headers = array();
         }
         if (!$force_fsockopen && function_exists('curl_exec')) {
             $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_CURL;
             $fp = curl_init();
             $headers2 = array();
             foreach ($headers as $key => $value) {
                 $headers2[] = "{$key}: {$value}";
             }
             if (version_compare(SimplePie_Misc::get_curl_version(), '7.10.5', '>=')) {
                 curl_setopt($fp, CURLOPT_ENCODING, '');
             }
             curl_setopt($fp, CURLOPT_URL, $url);
             curl_setopt($fp, CURLOPT_HEADER, 1);
             curl_setopt($fp, CURLOPT_RETURNTRANSFER, 1);
             curl_setopt($fp, CURLOPT_TIMEOUT, $timeout);
             curl_setopt($fp, CURLOPT_CONNECTTIMEOUT, $timeout);
             curl_setopt($fp, CURLOPT_REFERER, $url);
             curl_setopt($fp, CURLOPT_USERAGENT, $useragent);
             curl_setopt($fp, CURLOPT_HTTPHEADER, $headers2);
             if (!ini_get('open_basedir') && !ini_get('safe_mode') && version_compare(SimplePie_Misc::get_curl_version(), '7.15.2', '>=')) {
                 curl_setopt($fp, CURLOPT_FOLLOWLOCATION, 1);
                 curl_setopt($fp, CURLOPT_MAXREDIRS, $redirects);
             }
             $this->headers = curl_exec($fp);
             if (curl_errno($fp) === 23 || curl_errno($fp) === 61) {
                 curl_setopt($fp, CURLOPT_ENCODING, 'none');
                 $this->headers = curl_exec($fp);
             }
             if (curl_errno($fp)) {
                 $this->error = 'cURL error ' . curl_errno($fp) . ': ' . curl_error($fp);
                 $this->success = false;
             } else {
                 $info = curl_getinfo($fp);
                 curl_close($fp);
                 $this->headers = explode("\r\n\r\n", $this->headers, $info['redirect_count'] + 1);
                 $this->headers = array_pop($this->headers);
                 $parser = new SimplePie_HTTP_Parser($this->headers);
                 if ($parser->parse()) {
                     $this->headers = $parser->headers;
                     $this->body = $parser->body;
                     $this->status_code = $parser->status_code;
                     if ((in_array($this->status_code, array(300, 301, 302, 303, 307)) || $this->status_code > 307 && $this->status_code < 400) && isset($this->headers['location']) && $this->redirects < $redirects) {
                         $this->redirects++;
                         $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url);
                         return $this->__construct($location, $timeout, $redirects, $headers, $useragent, $force_fsockopen);
                     }
                 }
             }
         } else {
             $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_FSOCKOPEN;
             $url_parts = parse_url($url);
             $socket_host = $url_parts['host'];
             if (isset($url_parts['scheme']) && strtolower($url_parts['scheme']) === 'https') {
                 $socket_host = "ssl://{$url_parts['host']}";
                 $url_parts['port'] = 443;
             }
             if (!isset($url_parts['port'])) {
                 $url_parts['port'] = 80;
             }
             $fp = @fsockopen($socket_host, $url_parts['port'], $errno, $errstr, $timeout);
             if (!$fp) {
                 $this->error = 'fsockopen error: ' . $errstr;
                 $this->success = false;
             } else {
                 stream_set_timeout($fp, $timeout);
                 if (isset($url_parts['path'])) {
                     if (isset($url_parts['query'])) {
                         $get = "{$url_parts['path']}?{$url_parts['query']}";
                     } else {
                         $get = $url_parts['path'];
                     }
                 } else {
                     $get = '/';
                 }
                 $out = "GET {$get} HTTP/1.1\r\n";
                 $out .= "Host: {$url_parts['host']}\r\n";
                 $out .= "User-Agent: {$useragent}\r\n";
                 if (extension_loaded('zlib')) {
                     $out .= "Accept-Encoding: x-gzip,gzip,deflate\r\n";
                 }
                 if (isset($url_parts['user']) && isset($url_parts['pass'])) {
                     $out .= "Authorization: Basic " . base64_encode("{$url_parts['user']}:{$url_parts['pass']}") . "\r\n";
                 }
                 foreach ($headers as $key => $value) {
                     $out .= "{$key}: {$value}\r\n";
                 }
                 $out .= "Connection: Close\r\n\r\n";
                 fwrite($fp, $out);
                 $info = stream_get_meta_data($fp);
                 $this->headers = '';
                 while (!$info['eof'] && !$info['timed_out']) {
                     $this->headers .= fread($fp, 1160);
                     $info = stream_get_meta_data($fp);
                 }
                 if (!$info['timed_out']) {
                     $parser = new SimplePie_HTTP_Parser($this->headers);
                     if ($parser->parse()) {
                         $this->headers = $parser->headers;
                         $this->body = $parser->body;
                         $this->status_code = $parser->status_code;
                         if ((in_array($this->status_code, array(300, 301, 302, 303, 307)) || $this->status_code > 307 && $this->status_code < 400) && isset($this->headers['location']) && $this->redirects < $redirects) {
                             $this->redirects++;
                             $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url);
                             return $this->__construct($location, $timeout, $redirects, $headers, $useragent, $force_fsockopen);
                         }
                         if (isset($this->headers['content-encoding'])) {
                             // Hey, we act dumb elsewhere, so let's do that here too
                             switch (strtolower(trim($this->headers['content-encoding'], "\t\n\r "))) {
                                 case 'gzip':
                                 case 'x-gzip':
                                     $decoder = new SimplePie_gzdecode($this->body);
                                     if (!$decoder->parse()) {
                                         $this->error = 'Unable to decode HTTP "gzip" stream';
                                         $this->success = false;
                                     } else {
                                         $this->body = $decoder->data;
                                     }
                                     break;
                                 case 'deflate':
                                     if (($decompressed = gzinflate($this->body)) !== false) {
                                         $this->body = $decompressed;
                                     } else {
                                         if (($decompressed = gzuncompress($this->body)) !== false) {
                                             $this->body = $decompressed;
                                         } else {
                                             if (function_exists('gzdecode') && ($decompressed = gzdecode($this->body)) !== false) {
                                                 $this->body = $decompressed;
                                             } else {
                                                 $this->error = 'Unable to decode HTTP "deflate" stream';
                                                 $this->success = false;
                                             }
                                         }
                                     }
                                     break;
                                 default:
                                     $this->error = 'Unknown content coding';
                                     $this->success = false;
                             }
                         }
                     }
                 } else {
                     $this->error = 'fsocket timed out';
                     $this->success = false;
                 }
                 fclose($fp);
             }
         }
     } else {
         $this->method = SIMPLEPIE_FILE_SOURCE_LOCAL | SIMPLEPIE_FILE_SOURCE_FILE_GET_CONTENTS;
         if (!($this->body = file_get_contents($url))) {
             $this->error = 'file_get_contents could not read the file';
             $this->success = false;
         }
     }
 }
示例#3
0
 public static function fix_protocol($url, $http = 1)
 {
     $url = SimplePie_Misc::normalize_url($url);
     $parsed = SimplePie_Misc::parse_url($url);
     if ($parsed['scheme'] !== '' && $parsed['scheme'] !== 'http' && $parsed['scheme'] !== 'https') {
         return SimplePie_Misc::fix_protocol(SimplePie_Misc::compress_parse_url('http', $parsed['authority'], $parsed['path'], $parsed['query'], $parsed['fragment']), $http);
     }
     if ($parsed['scheme'] === '' && $parsed['authority'] === '' && !file_exists($url)) {
         return SimplePie_Misc::fix_protocol(SimplePie_Misc::compress_parse_url('http', $parsed['path'], '', $parsed['query'], $parsed['fragment']), $http);
     }
     if ($http === 2 && $parsed['scheme'] !== '') {
         return "feed:{$url}";
     } elseif ($http === 3 && strtolower($parsed['scheme']) === 'http') {
         return substr_replace($url, 'podcast', 0, 4);
     } elseif ($http === 4 && strtolower($parsed['scheme']) === 'http') {
         return substr_replace($url, 'itpc', 0, 4);
     } else {
         return $url;
     }
 }
示例#4
0
 function get_links()
 {
     $links = SimplePie_Misc::get_element('a', $this->file->body);
     foreach ($links as $link) {
         if (isset($link['attribs']['href']['data'])) {
             $href = trim($link['attribs']['href']['data']);
             $parsed = SimplePie_Misc::parse_url($href);
             if ($parsed['scheme'] === '' || preg_match('/^(http(s)|feed)?$/i', $parsed['scheme'])) {
                 if ($this->base_location < $link['offset']) {
                     $href = SimplePie_Misc::absolutize_url(trim($link['attribs']['href']['data']), $this->base);
                 } else {
                     $href = SimplePie_Misc::absolutize_url(trim($link['attribs']['href']['data']), $this->http_base);
                 }
                 $current = SimplePie_Misc::parse_url($this->file->url);
                 if ($parsed['authority'] === '' || $parsed['authority'] == $current['authority']) {
                     $this->local[] = $href;
                 } else {
                     $this->elsewhere[] = $href;
                 }
             }
         }
     }
     $this->local = array_unique($this->local);
     $this->elsewhere = array_unique($this->elsewhere);
     if (!empty($this->local) || !empty($this->elsewhere)) {
         return true;
     }
     return null;
 }
示例#5
0
 /**
  * Get the file extension
  *
  * @return string|null
  */
 public function get_extension()
 {
     if ($this->link !== null) {
         $url = SimplePie_Misc::parse_url($this->link);
         if ($url['path'] !== '') {
             return pathinfo($url['path'], PATHINFO_EXTENSION);
         }
     }
     return null;
 }
示例#6
0
 public function init()
 {
     // Check absolute bare minimum requirements.
     if (function_exists('version_compare') && version_compare(PHP_VERSION, '5.0', '<') || !extension_loaded('xml') || !extension_loaded('pcre')) {
         return false;
     } elseif (!extension_loaded('xmlreader')) {
         static $xml_is_sane = null;
         if ($xml_is_sane === null) {
             $parser_check = xml_parser_create();
             xml_parse_into_struct($parser_check, '<foo>&amp;</foo>', $values);
             xml_parser_free($parser_check);
             $xml_is_sane = isset($values[0]['value']);
         }
         if (!$xml_is_sane) {
             return false;
         }
     }
     // Pass whatever was set with config options over to the sanitizer.
     $this->sanitize->pass_cache_data($this->cache, $this->cache_location, $this->cache_name_function, $this->cache_class);
     $this->sanitize->pass_file_data($this->file_class, $this->timeout, $this->useragent, $this->force_fsockopen);
     if ($this->feed_url !== null || $this->raw_data !== null) {
         $this->error = null;
         $this->data = array();
         $this->multifeed_objects = array();
         $cache = false;
         if ($this->feed_url !== null) {
             $parsed_feed_url = SimplePie_Misc::parse_url($this->feed_url);
             // Decide whether to enable caching
             if ($this->cache && $parsed_feed_url['scheme'] !== '') {
                 $cache = call_user_func(array($this->cache_class, 'create'), $this->cache_location, call_user_func($this->cache_name_function, $this->feed_url), 'spc');
             }
             // If it's enabled and we don't want an XML dump, use the cache
             if ($cache && !$this->xml_dump) {
                 // Load the Cache
                 $this->data = $cache->load();
                 if (!empty($this->data)) {
                     // If the cache is for an outdated build of SimplePie
                     if (!isset($this->data['build']) || $this->data['build'] !== SIMPLEPIE_BUILD) {
                         $cache->unlink();
                         $this->data = array();
                     } elseif (isset($this->data['url']) && $this->data['url'] !== $this->feed_url) {
                         $cache = false;
                         $this->data = array();
                     } elseif (isset($this->data['feed_url'])) {
                         // If the autodiscovery cache is still valid use it.
                         if ($cache->mtime() + $this->autodiscovery_cache_duration > time()) {
                             // Do not need to do feed autodiscovery yet.
                             if ($this->data['feed_url'] === $this->data['url']) {
                                 $cache->unlink();
                                 $this->data = array();
                             } else {
                                 $this->set_feed_url($this->data['feed_url']);
                                 return $this->init();
                             }
                         }
                     } elseif ($cache->mtime() + $this->cache_duration < time()) {
                         // If we have last-modified and/or etag set
                         if (isset($this->data['headers']['last-modified']) || isset($this->data['headers']['etag'])) {
                             $headers = array('Accept' => 'application/atom+xml, application/rss+xml, application/rdf+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.8, text/html;q=0.7, unknown/unknown;q=0.1, application/unknown;q=0.1, */*;q=0.1');
                             if (isset($this->data['headers']['last-modified'])) {
                                 $headers['if-modified-since'] = $this->data['headers']['last-modified'];
                             }
                             if (isset($this->data['headers']['etag'])) {
                                 $headers['if-none-match'] = $this->data['headers']['etag'];
                             }
                             $file = new $this->file_class($this->feed_url, $this->timeout / 10, 5, $headers, $this->useragent, $this->force_fsockopen);
                             if ($file->success) {
                                 if ($file->status_code === 304) {
                                     $cache->touch();
                                     return true;
                                 } else {
                                     $headers = $file->headers;
                                 }
                             } else {
                                 unset($file);
                             }
                         }
                     } else {
                         return true;
                     }
                 } else {
                     $cache->unlink();
                     $this->data = array();
                 }
             }
             // If we don't already have the file (it'll only exist if we've opened it to check if the cache has been modified), open it.
             if (!isset($file)) {
                 if (is_a($this->file, 'SimplePie_File') && $this->file->url === $this->feed_url) {
                     $file =& $this->file;
                 } else {
                     $headers = array('Accept' => 'application/atom+xml, application/rss+xml, application/rdf+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.8, text/html;q=0.7, unknown/unknown;q=0.1, application/unknown;q=0.1, */*;q=0.1');
                     $file = new $this->file_class($this->feed_url, $this->timeout, 5, $headers, $this->useragent, $this->force_fsockopen);
                 }
             }
             // If the file connection has an error, set SimplePie::error to that and quit
             if (!$file->success && !($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($file->status_code === 200 || $file->status_code > 206 && $file->status_code < 300))) {
                 $this->error = $file->error;
                 if (!empty($this->data)) {
                     return true;
                 } else {
                     return false;
                 }
             }
             if (!$this->force_feed) {
                 // Check if the supplied URL is a feed, if it isn't, look for it.
                 $locate = new $this->locator_class($file, $this->timeout, $this->useragent, $this->file_class, $this->max_checked_feeds, $this->content_type_sniffer_class);
                 if (!$locate->is_feed($file)) {
                     // We need to unset this so that if SimplePie::set_file() has been called that object is untouched
                     unset($file);
                     if ($file = $locate->find($this->autodiscovery, $this->all_discovered_feeds)) {
                         if ($cache) {
                             $this->data = array('url' => $this->feed_url, 'feed_url' => $file->url, 'build' => SIMPLEPIE_BUILD);
                             if (!$cache->save($this)) {
                                 trigger_error("{$this->cache_location} is not writeable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING);
                             }
                             $cache = call_user_func(array($this->cache_class, 'create'), $this->cache_location, call_user_func($this->cache_name_function, $file->url), 'spc');
                         }
                         $this->feed_url = $file->url;
                     } else {
                         $this->error = "A feed could not be found at {$this->feed_url}. A feed with an invalid mime type may fall victim to this error, or " . SIMPLEPIE_NAME . " was unable to auto-discover it.. Use force_feed() if you are certain this URL is a real feed.";
                         SimplePie_Misc::error($this->error, E_USER_NOTICE, __FILE__, __LINE__);
                         return false;
                     }
                 }
                 $locate = null;
             }
             $headers = $file->headers;
             $data = $file->body;
             $sniffer = new $this->content_type_sniffer_class($file);
             $sniffed = $sniffer->get_type();
         } else {
             $data = $this->raw_data;
         }
         // Set up array of possible encodings
         $encodings = array();
         // First check to see if input has been overridden.
         if ($this->input_encoding !== false) {
             $encodings[] = $this->input_encoding;
         }
         $application_types = array('application/xml', 'application/xml-dtd', 'application/xml-external-parsed-entity');
         $text_types = array('text/xml', 'text/xml-external-parsed-entity');
         // RFC 3023 (only applies to sniffed content)
         if (isset($sniffed)) {
             if (in_array($sniffed, $application_types) || substr($sniffed, 0, 12) === 'application/' && substr($sniffed, -4) === '+xml') {
                 if (isset($headers['content-type']) && preg_match('/;\\x20?charset=([^;]*)/i', $headers['content-type'], $charset)) {
                     $encodings[] = strtoupper($charset[1]);
                 }
                 $encodings = array_merge($encodings, SimplePie_Misc::xml_encoding($data));
                 $encodings[] = 'UTF-8';
             } elseif (in_array($sniffed, $text_types) || substr($sniffed, 0, 5) === 'text/' && substr($sniffed, -4) === '+xml') {
                 if (isset($headers['content-type']) && preg_match('/;\\x20?charset=([^;]*)/i', $headers['content-type'], $charset)) {
                     $encodings[] = $charset[1];
                 }
                 $encodings[] = 'US-ASCII';
             } elseif (substr($sniffed, 0, 5) === 'text/') {
                 $encodings[] = 'US-ASCII';
             }
         }
         // Fallback to XML 1.0 Appendix F.1/UTF-8/ISO-8859-1
         $encodings = array_merge($encodings, SimplePie_Misc::xml_encoding($data));
         $encodings[] = 'UTF-8';
         $encodings[] = 'ISO-8859-1';
         // There's no point in trying an encoding twice
         $encodings = array_unique($encodings);
         // If we want the XML, just output that with the most likely encoding and quit
         if ($this->xml_dump) {
             header('Content-type: text/xml; charset=' . $encodings[0]);
             echo $data;
             exit;
         }
         // Loop through each possible encoding, till we return something, or run out of possibilities
         foreach ($encodings as $encoding) {
             // Change the encoding to UTF-8 (as we always use UTF-8 internally)
             if ($utf8_data = SimplePie_Misc::change_encoding($data, $encoding, 'UTF-8')) {
                 // Create new parser
                 $parser = new $this->parser_class();
                 // If it's parsed fine
                 if ($parser->parse($utf8_data, 'UTF-8')) {
                     $this->data = $parser->get_data();
                     if ($this->get_type() & ~SIMPLEPIE_TYPE_NONE) {
                         if (isset($headers)) {
                             $this->data['headers'] = $headers;
                         }
                         $this->data['build'] = SIMPLEPIE_BUILD;
                         // Cache the file if caching is enabled
                         if ($cache && !$cache->save($this)) {
                             trigger_error("{$this->cache_location} is not writeable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING);
                         }
                         return true;
                     } else {
                         $this->error = "A feed could not be found at {$this->feed_url}. This does not appear to be a valid RSS or Atom feed.";
                         SimplePie_Misc::error($this->error, E_USER_NOTICE, __FILE__, __LINE__);
                         return false;
                     }
                 }
             }
         }
         if (isset($parser)) {
             // We have an error, just set SimplePie_Misc::error to it and quit
             $this->error = sprintf('This XML document is invalid, likely due to invalid characters. XML error: %s at line %d, column %d', $parser->get_error_string(), $parser->get_current_line(), $parser->get_current_column());
         } else {
             $this->error = 'The data could not be converted to UTF-8. You MUST have either the iconv or mbstring extension installed. Upgrading to PHP 5.x (which includes iconv) is highly recommended.';
         }
         SimplePie_Misc::error($this->error, E_USER_NOTICE, __FILE__, __LINE__);
         return false;
     } elseif (!empty($this->multifeed_url)) {
         $i = 0;
         $success = 0;
         $this->multifeed_objects = array();
         foreach ($this->multifeed_url as $url) {
             $this->multifeed_objects[$i] = clone $this;
             $this->multifeed_objects[$i]->set_feed_url($url);
             $success |= $this->multifeed_objects[$i]->init();
             $i++;
         }
         return (bool) $success;
     } else {
         return false;
     }
 }
示例#7
0
 function parse_data_array(&$data, $url)
 {
     // Feed Info (Type and Version)
     if (!empty($data['feedinfo']['type'])) {
         $this->feedinfo = $data['feedinfo'];
     }
     // Feed level xml:base
     if (!empty($data['feeddata']['attribs']['XML:BASE'])) {
         $this->feed_xmlbase = $data['feeddata']['attribs']['XML:BASE'];
     } else {
         if (!empty($data['feeddata']['attribs']['HTTP://WWW.W3.ORG/XML/1998/NAMESPACE:BASE'])) {
             $this->feed_xmlbase = $data['feeddata']['attribs']['HTTP://WWW.W3.ORG/XML/1998/NAMESPACE:BASE'];
         } else {
             if (strpos($url, 'http://feeds.feedburner.com/') !== 0) {
                 $this->feed_xmlbase = SimplePie_Misc::parse_url($url);
                 if (empty($this->feed_xmlbase['authority'])) {
                     $this->feed_xmlbase = preg_replace('/^' . preg_quote(realpath($_SERVER['DOCUMENT_ROOT']), '/') . '/', '', realpath($url));
                 } else {
                     $this->feed_xmlbase = $url;
                 }
             }
         }
     }
     // Feed link(s)
     if (!empty($data['info']['link'])) {
         foreach ($data['info']['link'] as $link) {
             if (empty($link['attribs']['REL'])) {
                 $rel = 'alternate';
             } else {
                 $rel = strtolower($link['attribs']['REL']);
             }
             if ($rel == 'enclosure') {
                 $href = null;
                 $type = null;
                 $length = null;
                 if (!empty($link['data'])) {
                     $href = $this->sanitize($link['data'], $link['attribs'], true);
                 } else {
                     if (!empty($link['attribs']['HREF'])) {
                         $href = $this->sanitize($link['attribs']['HREF'], $link['attribs'], true);
                     }
                 }
                 if (!empty($link['attribs']['TYPE'])) {
                     $type = $this->sanitize($link['attribs']['TYPE'], $link['attribs']);
                 }
                 if (!empty($link['attribs']['LENGTH'])) {
                     $length = $this->sanitize($link['attribs']['LENGTH'], $link['attribs']);
                 }
                 $this->info['link']['enclosure'][] = new $this->enclosure_class($href, $type, $length);
             } else {
                 if (!empty($link['data'])) {
                     $this->info['link'][$rel][] = $this->sanitize($link['data'], $link['attribs'], true);
                 } else {
                     if (!empty($link['attribs']['HREF'])) {
                         $this->info['link'][$rel][] = $this->sanitize($link['attribs']['HREF'], $link['attribs'], true);
                     }
                 }
             }
         }
     }
     // Use the first alternate link if we don't have any feed xml:base
     if (empty($this->feed_xmlbase) && !empty($this->info['link']['alternate'][0])) {
         $this->feed_xmlbase = $this->info['link']['alternate'][0];
     }
     // Feed Title
     if (!empty($data['info']['title']['data'])) {
         $this->info['title'] = $this->sanitize($data['info']['title']['data'], $data['info']['title']['attribs']);
     }
     // Feed Descriptions
     if (!empty($data['info']['description']['data'])) {
         $this->info['description'] = $this->sanitize($data['info']['description']['data'], $data['info']['description']['attribs'], false, true);
     }
     if (!empty($data['info']['dc:description']['data'])) {
         $this->info['dc:description'] = $this->sanitize($data['info']['dc:description']['data'], $data['info']['dc:description']['attribs']);
     }
     if (!empty($data['info']['tagline']['data'])) {
         $this->info['tagline'] = $this->sanitize($data['info']['tagline']['data'], $data['info']['tagline']['attribs']);
     }
     if (!empty($data['info']['subtitle']['data'])) {
         $this->info['subtitle'] = $this->sanitize($data['info']['subtitle']['data'], $data['info']['subtitle']['attribs']);
     }
     // Feed Language
     if (!empty($data['info']['language']['data'])) {
         $this->info['language'] = $this->sanitize($data['info']['language']['data'], $data['info']['language']['attribs']);
     }
     if (!empty($data['feeddata']['attribs']['XML:LANG'])) {
         $this->info['xml:lang'] = $this->sanitize($data['feeddata']['attribs']['XML:LANG'], null);
     } else {
         if (!empty($data['feeddata']['attribs']['HTTP://WWW.W3.ORG/XML/1998/NAMESPACE:LANG'])) {
             $this->info['xml:lang'] = $this->sanitize($data['feeddata']['attribs']['HTTP://WWW.W3.ORG/XML/1998/NAMESPACE:LANG'], null);
         }
     }
     // Feed Copyright
     if (!empty($data['info']['copyright']['data'])) {
         $this->info['copyright'] = $this->sanitize($data['info']['copyright']['data'], $data['info']['copyright']['attribs']);
     }
     // Feed Image
     if (!empty($data['info']['image']['title']['data'])) {
         $this->info['image']['title'] = $this->sanitize($data['info']['image']['title']['data'], $data['info']['image']['title']['attribs']);
     }
     if (!empty($data['info']['image']['url']['data'])) {
         $this->info['image']['url'] = $this->sanitize($data['info']['image']['url']['data'], $data['info']['image']['url']['attribs'], true);
     }
     if (!empty($data['info']['logo']['data'])) {
         $this->info['image']['logo'] = $this->sanitize($data['info']['logo']['data'], $data['info']['logo']['attribs'], true);
     }
     if (!empty($data['info']['image']['link']['data'])) {
         $this->info['image']['link'] = $this->sanitize($data['info']['image']['link']['data'], $data['info']['image']['link']['attribs'], true);
     }
     if (!empty($data['info']['image']['width']['data'])) {
         $this->info['image']['width'] = $this->sanitize($data['info']['image']['width']['data'], $data['info']['image']['width']['attribs']);
     }
     if (!empty($data['info']['image']['height']['data'])) {
         $this->info['image']['height'] = $this->sanitize($data['info']['image']['height']['data'], $data['info']['image']['height']['attribs']);
     }
     // Items
     if (!empty($data['items'])) {
         foreach ($data['items'] as $key => $item) {
             $newitem = null;
             // Item level xml:base
             if (!empty($item['attribs']['XML:BASE'])) {
                 $this->item_xmlbase = SimplePie_Misc::absolutize_url($item['attribs']['XML:BASE'], $this->feed_xmlbase);
             } else {
                 if (!empty($item['attribs']['HTTP://WWW.W3.ORG/XML/1998/NAMESPACE:BASE'])) {
                     $this->item_xmlbase = SimplePie_Misc::absolutize_url($item['attribs']['HTTP://WWW.W3.ORG/XML/1998/NAMESPACE:BASE'], $this->feed_xmlbase);
                 } else {
                     $this->item_xmlbase = null;
                 }
             }
             // Title
             if (!empty($item['title']['data'])) {
                 $newitem['title'] = $this->sanitize($item['title']['data'], $item['title']['attribs']);
             }
             if (!empty($item['dc:title']['data'])) {
                 $newitem['dc:title'] = $this->sanitize($item['dc:title']['data'], $item['dc:title']['attribs']);
             }
             // Description
             if (!empty($item['content']['data'])) {
                 $newitem['content'] = $this->sanitize($item['content']['data'], $item['content']['attribs']);
             }
             if (!empty($item['encoded']['data'])) {
                 $newitem['encoded'] = $this->sanitize($item['encoded']['data'], $item['encoded']['attribs']);
             }
             if (!empty($item['summary']['data'])) {
                 $newitem['summary'] = $this->sanitize($item['summary']['data'], $item['summary']['attribs']);
             }
             if (!empty($item['description']['data'])) {
                 $newitem['description'] = $this->sanitize($item['description']['data'], $item['description']['attribs'], false, true);
             }
             if (!empty($item['dc:description']['data'])) {
                 $newitem['dc:description'] = $this->sanitize($item['dc:description']['data'], $item['dc:description']['attribs']);
             }
             if (!empty($item['longdesc']['data'])) {
                 $newitem['longdesc'] = $this->sanitize($item['longdesc']['data'], $item['longdesc']['attribs']);
             }
             // Link(s)
             if (!empty($item['link'])) {
                 foreach ($item['link'] as $link) {
                     if (empty($link['attribs']['REL'])) {
                         $rel = 'alternate';
                     } else {
                         $rel = strtolower($link['attribs']['REL']);
                     }
                     if ($rel == 'enclosure') {
                         $href = null;
                         $type = null;
                         $length = null;
                         if (!empty($link['data'])) {
                             $href = $this->sanitize($link['data'], $link['attribs'], true);
                         } else {
                             if (!empty($link['attribs']['HREF'])) {
                                 $href = $this->sanitize($link['attribs']['HREF'], $link['attribs'], true);
                             }
                         }
                         if (!empty($link['attribs']['TYPE'])) {
                             $type = $this->sanitize($link['attribs']['TYPE'], $link['attribs']);
                         }
                         if (!empty($link['attribs']['LENGTH'])) {
                             $length = $this->sanitize($link['attribs']['LENGTH'], $link['attribs']);
                         }
                         if (!empty($href)) {
                             $newitem['link'][$rel][] = new $this->enclosure_class($href, $type, $length);
                         }
                     } else {
                         if (!empty($link['data'])) {
                             $newitem['link'][$rel][] = $this->sanitize($link['data'], $link['attribs'], true);
                         } else {
                             if (!empty($link['attribs']['HREF'])) {
                                 $newitem['link'][$rel][] = $this->sanitize($link['attribs']['HREF'], $link['attribs'], true);
                             }
                         }
                     }
                 }
             }
             // Enclosure(s)
             if (!empty($item['enclosure'])) {
                 foreach ($item['enclosure'] as $enclosure) {
                     if (!empty($enclosure['attribs']['URL'])) {
                         $type = null;
                         $length = null;
                         $href = $this->sanitize($enclosure['attribs']['URL'], $enclosure['attribs'], true);
                         if (!empty($enclosure['attribs']['TYPE'])) {
                             $type = $this->sanitize($enclosure['attribs']['TYPE'], $enclosure['attribs']);
                         }
                         if (!empty($enclosure['attribs']['LENGTH'])) {
                             $length = $this->sanitize($enclosure['attribs']['LENGTH'], $enclosure['attribs']);
                         }
                         $newitem['enclosures'][] = new $this->enclosure_class($href, $type, $length);
                     }
                 }
             }
             // ID
             if (!empty($item['guid']['data'])) {
                 if (!empty($item['guid']['attribs']['ISPERMALINK']) && strtolower($item['guid']['attribs']['ISPERMALINK']) == 'false') {
                     $newitem['guid']['permalink'] = false;
                 } else {
                     $newitem['guid']['permalink'] = true;
                 }
                 $newitem['guid']['data'] = $this->sanitize($item['guid']['data'], $item['guid']['attribs']);
             }
             if (!empty($item['id']['data'])) {
                 $newitem['id'] = $this->sanitize($item['id']['data'], $item['id']['attribs']);
             }
             // Date
             if (!empty($item['pubdate']['data'])) {
                 $newitem['pubdate'] = $this->parse_date($this->sanitize($item['pubdate']['data'], $item['pubdate']['attribs']));
             }
             if (!empty($item['dc:date']['data'])) {
                 $newitem['dc:date'] = $this->parse_date($this->sanitize($item['dc:date']['data'], $item['dc:date']['attribs']));
             }
             if (!empty($item['issued']['data'])) {
                 $newitem['issued'] = $this->parse_date($this->sanitize($item['issued']['data'], $item['issued']['attribs']));
             }
             if (!empty($item['published']['data'])) {
                 $newitem['published'] = $this->parse_date($this->sanitize($item['published']['data'], $item['published']['attribs']));
             }
             if (!empty($item['modified']['data'])) {
                 $newitem['modified'] = $this->parse_date($this->sanitize($item['modified']['data'], $item['modified']['attribs']));
             }
             if (!empty($item['updated']['data'])) {
                 $newitem['updated'] = $this->parse_date($this->sanitize($item['updated']['data'], $item['updated']['attribs']));
             }
             // Categories
             if (!empty($item['category'])) {
                 foreach ($item['category'] as $category) {
                     if (!empty($category['data'])) {
                         $newitem['category'][] = $this->sanitize($category['data'], $category['attribs']);
                     } else {
                         if (!empty($category['attribs']['TERM'])) {
                             $newitem['term'][] = $this->sanitize($category['attribs']['TERM'], $category['attribs']);
                         }
                     }
                 }
             }
             if (!empty($item['subject'])) {
                 foreach ($item['subject'] as $category) {
                     if (!empty($category['data'])) {
                         $newitem['subject'][] = $this->sanitize($category['data'], $category['attribs']);
                     }
                 }
             }
             // Author
             if (!empty($item['creator'])) {
                 foreach ($item['creator'] as $creator) {
                     if (!empty($creator['data'])) {
                         $newitem['creator'][] = new $this->author_class($this->sanitize($creator['data'], $creator['attribs']), null, null);
                     }
                 }
             }
             if (!empty($item['author'])) {
                 foreach ($item['author'] as $author) {
                     $name = null;
                     $link = null;
                     $email = null;
                     if (!empty($author['rss'])) {
                         $sane = $this->sanitize($author['rss']['data'], $author['rss']['attribs']);
                         if (preg_match('/(.*)@(.*) \\((.*)\\)/msiU', $sane, $matches)) {
                             $name = trim($matches[3]);
                             $email = trim("{$matches['1']}@{$matches['2']}");
                         } else {
                             $email = $sane;
                         }
                     } else {
                         if (!empty($author['name'])) {
                             $name = $this->sanitize($author['name']['data'], $author['name']['attribs']);
                         }
                         if (!empty($author['url'])) {
                             $link = $this->sanitize($author['url']['data'], $author['url']['attribs'], true);
                         } else {
                             if (!empty($author['uri'])) {
                                 $link = $this->sanitize($author['uri']['data'], $author['uri']['attribs'], true);
                             } else {
                                 if (!empty($author['homepage'])) {
                                     $link = $this->sanitize($author['homepage']['data'], $author['homepage']['attribs'], true);
                                 }
                             }
                         }
                         if (!empty($author['email'])) {
                             $email = $this->sanitize($author['email']['data'], $author['email']['attribs']);
                         }
                     }
                     $newitem['author'][] = new $this->author_class($name, $link, $email);
                 }
             }
             unset($data['items'][$key]);
             $this->items[] = new $this->item_class($newitem);
         }
     }
 }