123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- <?php
- function aggregator_aggregator_parse_info() {
- return array(
- 'title' => t('Default parser'),
- 'description' => t('Parses RSS, Atom and RDF feeds.'),
- );
- }
- function aggregator_aggregator_parse($feed) {
- global $channel, $image;
-
- if (aggregator_parse_feed($feed->source_string, $feed)) {
- $modified = empty($feed->http_headers['last-modified']) ? 0 : strtotime($feed->http_headers['last-modified']);
-
- foreach ($channel as $key => $value) {
- $channel[$key] = trim($value);
- }
-
- foreach ($image as $key => $value) {
- $image[$key] = trim($value);
- }
- $etag = empty($feed->http_headers['etag']) ? '' : $feed->http_headers['etag'];
-
- $feed->link = !empty($channel['link']) ? $channel['link'] : '';
- $feed->description = !empty($channel['description']) ? $channel['description'] : '';
- $feed->image = !empty($image['url']) ? $image['url'] : '';
- $feed->etag = $etag;
- $feed->modified = $modified;
-
- cache_clear_all();
- return TRUE;
- }
- return FALSE;
- }
- function aggregator_parse_feed(&$data, $feed) {
- global $items, $image, $channel;
-
- unset($GLOBALS['element'], $GLOBALS['item'], $GLOBALS['tag']);
- $items = array();
- $image = array();
- $channel = array();
-
- $xml_parser = drupal_xml_parser_create($data);
- xml_set_element_handler($xml_parser, 'aggregator_element_start', 'aggregator_element_end');
- xml_set_character_data_handler($xml_parser, 'aggregator_element_data');
- if (!xml_parse($xml_parser, $data, 1)) {
- watchdog('aggregator', 'The feed from %site seems to be broken, due to an error "%error" on line %line.', array('%site' => $feed->title, '%error' => xml_error_string(xml_get_error_code($xml_parser)), '%line' => xml_get_current_line_number($xml_parser)), WATCHDOG_WARNING);
- drupal_set_message(t('The feed from %site seems to be broken, because of error "%error" on line %line.', array('%site' => $feed->title, '%error' => xml_error_string(xml_get_error_code($xml_parser)), '%line' => xml_get_current_line_number($xml_parser))), 'error');
- return FALSE;
- }
- xml_parser_free($xml_parser);
-
-
- $items = array_reverse($items);
-
- $feed->items = array();
- foreach ($items as $item) {
-
- foreach ($item as $key => $value) {
- $item[$key] = trim($value);
- }
-
-
-
- if (!empty($item['title'])) {
- $item['title'] = $item['title'];
- }
- elseif (!empty($item['description'])) {
- $item['title'] = preg_replace('/^(.*)[^\w;&].*?$/', "\\1", truncate_utf8($item['description'], 40));
- }
- else {
- $item['title'] = '';
- }
-
- if (!empty($item['link'])) {
- $item['link'] = $item['link'];
- }
- else {
- $item['link'] = $feed->link;
- }
-
- if (!isset($item['guid'])) {
- $item['guid'] = isset($item['id']) ? $item['id'] : '';
- }
-
- if (!empty($item['content:encoded'])) {
- $item['description'] = $item['content:encoded'];
- }
- elseif (!empty($item['summary'])) {
- $item['description'] = $item['summary'];
- }
- elseif (!empty($item['content'])) {
- $item['description'] = $item['content'];
- }
-
- $date = '';
- foreach (array('pubdate', 'dc:date', 'dcterms:issued', 'dcterms:created', 'dcterms:modified', 'issued', 'created', 'modified', 'published', 'updated') as $key) {
- if (!empty($item[$key])) {
- $date = $item[$key];
- break;
- }
- }
- $item['timestamp'] = strtotime($date);
- if ($item['timestamp'] === FALSE) {
- $item['timestamp'] = aggregator_parse_w3cdtf($date);
- }
-
- if (empty($item['author']) && !empty($item['dc:creator'])) {
- $item['author'] = $item['dc:creator'];
- }
- $item += array('author' => '', 'description' => '');
-
- $feed->items[] = $item;
- }
- return TRUE;
- }
- function aggregator_element_start($parser, $name, $attributes) {
- global $item, $element, $tag, $items, $channel;
- $name = strtolower($name);
- switch ($name) {
- case 'image':
- case 'textinput':
- case 'summary':
- case 'tagline':
- case 'subtitle':
- case 'logo':
- case 'info':
- $element = $name;
- break;
- case 'id':
- case 'content':
- if ($element != 'item') {
- $element = $name;
- }
- case 'link':
-
-
-
- if (!empty($attributes['HREF']) && (empty($attributes['REL']) || $attributes['REL'] == 'alternate')) {
- if ($element == 'item') {
- $items[$item]['link'] = $attributes['HREF'];
- }
- else {
- $channel['link'] = $attributes['HREF'];
- }
- }
- break;
- case 'item':
- $element = $name;
- $item += 1;
- break;
- case 'entry':
- $element = 'item';
- $item += 1;
- break;
- }
- $tag = $name;
- }
- function aggregator_element_end($parser, $name) {
- global $element;
- switch ($name) {
- case 'image':
- case 'textinput':
- case 'item':
- case 'entry':
- case 'info':
- $element = '';
- break;
- case 'id':
- case 'content':
- if ($element == $name) {
- $element = '';
- }
- }
- }
- function aggregator_element_data($parser, $data) {
- global $channel, $element, $items, $item, $image, $tag;
- $items += array($item => array());
- switch ($element) {
- case 'item':
- $items[$item] += array($tag => '');
- $items[$item][$tag] .= $data;
- break;
- case 'image':
- case 'logo':
- $image += array($tag => '');
- $image[$tag] .= $data;
- break;
- case 'link':
- if ($data) {
- $items[$item] += array($tag => '');
- $items[$item][$tag] .= $data;
- }
- break;
- case 'content':
- $items[$item] += array('content' => '');
- $items[$item]['content'] .= $data;
- break;
- case 'summary':
- $items[$item] += array('summary' => '');
- $items[$item]['summary'] .= $data;
- break;
- case 'tagline':
- case 'subtitle':
- $channel += array('description' => '');
- $channel['description'] .= $data;
- break;
- case 'info':
- case 'id':
- case 'textinput':
-
-
- break;
- default:
- $channel += array($tag => '');
- $channel[$tag] .= $data;
- }
- }
- function aggregator_parse_w3cdtf($date_str) {
- if (preg_match('/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(:(\d{2}))?(?:([-+])(\d{2}):?(\d{2})|(Z))?/', $date_str, $match)) {
- list($year, $month, $day, $hours, $minutes, $seconds) = array($match[1], $match[2], $match[3], $match[4], $match[5], $match[6]);
-
- $epoch = gmmktime($hours, $minutes, $seconds, $month, $day, $year);
- if ($match[10] != 'Z') {
- list($tz_mod, $tz_hour, $tz_min) = array($match[8], $match[9], $match[10]);
-
- if (!$tz_hour) {
- $tz_hour = 0;
- }
- if (!$tz_min) {
- $tz_min = 0;
- }
- $offset_secs = (($tz_hour * 60) + $tz_min) * 60;
-
- if ($tz_mod == '+') {
- $offset_secs *= -1;
- }
- $epoch += $offset_secs;
- }
- return $epoch;
- }
- else {
- return FALSE;
- }
- }
|