parser_common_syndication.module

Tracking 6.x-1.x branch
  1. drupal
    1. 5 contributions/feedapi/parser_common_syndication/parser_common_syndication.module
    2. 6 contributions/feedapi/parser_common_syndication/parser_common_syndication.module

Parse the incoming URL with SimpleXML then provide a data structure of the feed. Requires PHP5 because of SimpleXML.

Functions & methods

NameDescription
parser_common_syndication_feedapi_feedImplementation of hook_feedapi_feed().
parser_common_syndication_feedapi_settings_formImplementation of hook_feedapi_settings_form().
parser_common_syndication_helpImplementation of hook_help().
_parser_common_syndication_use_curlDecides if it's possible to use cURL or not

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Parse the incoming URL with SimpleXML then provide a data structure of the feed.
  5. * Requires PHP5 because of SimpleXML.
  6. */
  7. /**
  8. * Implementation of hook_help().
  9. */
  10. function parser_common_syndication_help($path, $arg) {
  11. switch ($path) {
  12. case 'admin/modules#description':
  13. return t('Provide a common syndication parser for FeedAPI-compatible modules. Only PHP5-compatible. Rather fast.');
  14. case 'feedapi/full_name':
  15. return t('Parser Common Syndication - only for PHP5');
  16. }
  17. }
  18. /**
  19. * Implementation of hook_feedapi_feed().
  20. */
  21. function parser_common_syndication_feedapi_feed($op) {
  22. $args = func_get_args();
  23. switch ($op) {
  24. case 'type':
  25. return array("XML feed");
  26. case 'compatible':
  27. if (!function_exists('simplexml_load_string')) {
  28. return FALSE;
  29. }
  30. require_once './'. drupal_get_path('module', 'parser_common_syndication') .'/parser_common_syndication.inc';
  31. $url = $args[1]->url;
  32. $settings = isset($args[2]) ? $args[2] : array();
  33. $downloaded_string = _parser_common_syndication_download($url, $settings);
  34. if (is_object($downloaded_string)) {
  35. return array_shift(parser_common_syndication_feedapi_feed('type'));
  36. }
  37. if (!defined('LIBXML_VERSION') || (version_compare(phpversion(), '5.1.0', '<'))) {
  38. @ $xml = simplexml_load_string($downloaded_string, NULL);
  39. }
  40. else {
  41. @ $xml = simplexml_load_string($downloaded_string, 'SimpleXMLElement', LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NOCDATA);
  42. }
  43. if (_parser_common_syndication_feed_format_detect($xml) != FALSE) {
  44. // The parser is compatible. Then has to parse the feed and cache it. Because in the download
  45. // part, the feed etag data be already saved perhaps (depends on the webserver).
  46. $parsed_feed = _parser_common_syndication_feedapi_parse($xml);
  47. if (is_object($parsed_feed) && empty($parsed_feed->from_cache)) {
  48. _parser_common_syndication_cache_set($url, $parsed_feed);
  49. }
  50. // We don't have to choose between the types, because this module is only able to parse one.
  51. return array_shift(parser_common_syndication_feedapi_feed('type'));
  52. }
  53. return FALSE;
  54. case 'parse':
  55. require_once './'. drupal_get_path('module', 'parser_common_syndication') .'/parser_common_syndication.inc';
  56. $feed = is_object($args[1]) ? $args[1] : FALSE;
  57. $parsed_feed = _parser_common_syndication_feedapi_parse($feed);
  58. if (is_object($parsed_feed) && empty($parsed_feed->from_cache)) {
  59. _parser_common_syndication_cache_set($feed->url, $parsed_feed);
  60. }
  61. return $parsed_feed;
  62. }
  63. }
  64. /**
  65. * Implementation of hook_feedapi_settings_form().
  66. */
  67. function parser_common_syndication_feedapi_settings_form($type) {
  68. if (_parser_common_syndication_use_curl()) {
  69. $form = array();
  70. switch ($type) {
  71. case 'parsers':
  72. $form['accept_invalid_cert'] = array(
  73. '#type' => 'checkbox',
  74. '#title' => t('Do not verify SSL peer'),
  75. '#description' => t('When the parser connects to an SSL protected resource it tries to validate the resource\'s SSL certificate. If this can\'t be done the feed download fails. Check this box to skip validation and accept any SSL certificate. Note that skipping validation can pose a security risk. See !curlopt.', array('!curlopt' => l('CURLOPT_SSL_VERIFYPEER', 'http://php.net/manual/en/function.curl-setopt.php'))),
  76. '#default_value' => FALSE,
  77. );
  78. break;
  79. }
  80. return $form;
  81. }
  82. }
  83. /**
  84. * Decides if it's possible to use cURL or not
  85. *
  86. * @return
  87. * TRUE or FALSE
  88. */
  89. function _parser_common_syndication_use_curl() {
  90. $basedir = ini_get("open_basedir");
  91. return function_exists('curl_init') && !ini_get('safe_mode') && empty($basedir);
  92. }