| 1 |
<?php // $Id: movino_feeds.inc,v 1.4 2007/09/19 15:12:38 tomsun Exp $
|
| 2 |
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Figures out the URL to a Movino feed
|
| 6 |
*/
|
| 7 |
function movino_feed_url($server, $type) {
|
| 8 |
// Allow for full paths.
|
| 9 |
if (substr($server['feed_' . $type . '_path'], 0, 7) == 'http://') {
|
| 10 |
return $server['feed_' . $type . '_path'];
|
| 11 |
}
|
| 12 |
// Default to path constructed from host and port.
|
| 13 |
return 'http://' . $server['host'] . ':' . $server['feed_' . $type . '_port'] . '/' . $server['feed_' . $type . '_path'];
|
| 14 |
}
|
| 15 |
|
| 16 |
|
| 17 |
/**
|
| 18 |
* Takes an XML feed as a string and returns an array.
|
| 19 |
*
|
| 20 |
* @param string $xmlData
|
| 21 |
* The contents of the XML feed.
|
| 22 |
*
|
| 23 |
* @return array
|
| 24 |
*/
|
| 25 |
function _movino_feed_parse($url) {
|
| 26 |
|
| 27 |
// Experimentally removed in favor of Curl.
|
| 28 |
// $data = file_get_contents($url);
|
| 29 |
|
| 30 |
$c = curl_init();
|
| 31 |
$timeout = 5;
|
| 32 |
curl_setopt ($c, CURLOPT_URL, $url);
|
| 33 |
curl_setopt ($c, CURLOPT_RETURNTRANSFER, 1);
|
| 34 |
curl_setopt ($c, CURLOPT_CONNECTTIMEOUT, $timeout);
|
| 35 |
$data = curl_exec($c);
|
| 36 |
curl_close($c);
|
| 37 |
|
| 38 |
// Verify that the contents of the data is a Movino feed.
|
| 39 |
if (substr($data, 0, 1) != '<') {
|
| 40 |
if (MOVINO_DEBUG) {
|
| 41 |
drupal_set_message('Feed fetching failed: ' . $url);
|
| 42 |
}
|
| 43 |
return FALSE;
|
| 44 |
}
|
| 45 |
|
| 46 |
// Create a parser object.
|
| 47 |
$parser = new XML_Unserializer();
|
| 48 |
|
| 49 |
// Parse and return an array.
|
| 50 |
return $parser->unserialize($data);
|
| 51 |
}
|
| 52 |
|
| 53 |
|
| 54 |
/**
|
| 55 |
* Feed parser.
|
| 56 |
*/
|
| 57 |
class XML_Unserializer {
|
| 58 |
var $stack;
|
| 59 |
var $arr_output;
|
| 60 |
var $null_token = "null";
|
| 61 |
|
| 62 |
function unserialize($str_input_xml) {
|
| 63 |
$p = xml_parser_create("");
|
| 64 |
xml_parser_set_option($p,XML_OPTION_SKIP_WHITE,1);
|
| 65 |
xml_set_element_handler($p, array(&$this, 'start_handler'), array(&$this, 'end_handler'));
|
| 66 |
xml_set_character_data_handler($p, array(&$this, 'data_handler'));
|
| 67 |
$this->stack = array(
|
| 68 |
array(
|
| 69 |
'name' => 'document',
|
| 70 |
'attributes' => array(),
|
| 71 |
'children' => array(),
|
| 72 |
'data' => ''
|
| 73 |
)
|
| 74 |
);
|
| 75 |
if (!xml_parse($p, $str_input_xml)) {
|
| 76 |
trigger_error(xml_error_string(xml_get_error_code($p)) ."\n". $str_input_xml, E_USER_NOTICE);
|
| 77 |
xml_parser_free($p);
|
| 78 |
return;
|
| 79 |
}
|
| 80 |
xml_parser_free($p);
|
| 81 |
|
| 82 |
$tmp = $this->build_array($this->stack[0]);
|
| 83 |
if (is_array($tmp) && count($tmp) == 1) {
|
| 84 |
$this->arr_output = array_pop($tmp);
|
| 85 |
}
|
| 86 |
else {
|
| 87 |
$this->arr_output = array();
|
| 88 |
}
|
| 89 |
unset($this->stack);
|
| 90 |
return $this->arr_output;
|
| 91 |
}
|
| 92 |
|
| 93 |
function get_unserialized_data() {
|
| 94 |
return $this->arr_output;
|
| 95 |
}
|
| 96 |
|
| 97 |
function build_array($stack) {
|
| 98 |
$result = array();
|
| 99 |
if (count($stack['attributes']) > 0) {
|
| 100 |
$result = array_merge($result, $stack['attributes']);
|
| 101 |
}
|
| 102 |
|
| 103 |
if (count($stack['children']) > 0) {
|
| 104 |
if (count($stack['children']) == 1) {
|
| 105 |
$key = array_keys($stack['children']);
|
| 106 |
if ($stack['children'][$key[0]]['name'] === $this->null_token) {
|
| 107 |
return NULL;
|
| 108 |
}
|
| 109 |
}
|
| 110 |
$keycount = array();
|
| 111 |
foreach ($stack['children'] as $child) {
|
| 112 |
$keycount[] = $child['name'];
|
| 113 |
}
|
| 114 |
if (count(array_unique($keycount)) != count($keycount)) {
|
| 115 |
// enumerated array
|
| 116 |
$children = array();
|
| 117 |
foreach ($stack['children'] as $child) {
|
| 118 |
$children[] = $this->build_array($child);
|
| 119 |
}
|
| 120 |
}
|
| 121 |
else {
|
| 122 |
// indexed array
|
| 123 |
$children = array();
|
| 124 |
foreach ($stack['children'] as $child) {
|
| 125 |
$children[$child['name']] = $this->build_array($child);
|
| 126 |
}
|
| 127 |
}
|
| 128 |
$result = array_merge($result, $children);
|
| 129 |
}
|
| 130 |
|
| 131 |
if (count($result) == 0) {
|
| 132 |
return trim($stack['data']);
|
| 133 |
}
|
| 134 |
else {
|
| 135 |
return $result;
|
| 136 |
}
|
| 137 |
}
|
| 138 |
|
| 139 |
|
| 140 |
function start_handler($parser, $name, $attribs = array()) {
|
| 141 |
$token = array();
|
| 142 |
$token['name'] = strtolower($name);
|
| 143 |
$token['attributes'] = $attribs;
|
| 144 |
$token['data'] = '';
|
| 145 |
$token['children'] = array();
|
| 146 |
$this->stack[] = $token;
|
| 147 |
}
|
| 148 |
|
| 149 |
|
| 150 |
function end_handler($parser, $name, $attribs = array()) {
|
| 151 |
$token = array_pop($this->stack);
|
| 152 |
$this->stack[count($this->stack) - 1]['children'][] = $token;
|
| 153 |
}
|
| 154 |
|
| 155 |
|
| 156 |
function data_handler($parser, $data) {
|
| 157 |
$this->stack[count($this->stack) - 1]['data'] .= $data;
|
| 158 |
}
|
| 159 |
}
|
| 160 |
|
| 161 |
|
| 162 |
function xml_serialize($tagname, $data) {
|
| 163 |
$xml = "<$tagname>";
|
| 164 |
if (is_array($data)) {
|
| 165 |
foreach ($data as $key => $value) {
|
| 166 |
$xml .= xml_serialize($key, $value);
|
| 167 |
}
|
| 168 |
}
|
| 169 |
else {
|
| 170 |
$xml .= "<![CDATA[".$data."]]>";
|
| 171 |
}
|
| 172 |
$xml .= "</$tagname>\n";
|
| 173 |
return $xml;
|
| 174 |
}
|