[ Index ] |
|
Code source de e107 0.7.8 |
1 <?php 2 /* 3 + ----------------------------------------------------------------------------+ 4 | e107 website system 5 | 6 | ©Steve Dunstan 2001-2002 7 | http://e107.org 8 | jalist@e107.org 9 | 10 | Released under the terms and conditions of the 11 | GNU General Public License (http://gnu.org). 12 | 13 | $Source: /cvsroot/e107/e107_0.7/e107_handlers/xml_class.php,v $ 14 | $Revision: 1.8 $ 15 | $Date: 2007/01/24 21:21:20 $ 16 | $Author: e107steved $ 17 +----------------------------------------------------------------------------+ 18 */ 19 20 if (!defined('e107_INIT')) { exit; } 21 22 class parseXml { 23 24 var $parser; 25 var $error; 26 var $current_tag; 27 var $start_tag; 28 var $xmlData = array(); 29 var $counterArray = array(); 30 var $data; 31 var $xmlFileContents; 32 33 34 function getRemoteXmlFile($address) 35 { 36 if(function_exists("curl_init")) 37 { 38 $cu = curl_init (); 39 curl_setopt($cu, CURLOPT_URL, $address); 40 curl_setopt($cu, CURLOPT_RETURNTRANSFER, 1); 41 curl_setopt ($cu, CURLOPT_HEADER, 0); 42 curl_setopt ($cu, CURLOPT_TIMEOUT, 10); 43 $this -> xmlFileContents = curl_exec($cu); 44 if (curl_error($cu)) 45 { 46 $this -> error = "Error: ".curl_errno($cu).", ".curl_error($cu); 47 return FALSE; 48 } 49 curl_close ($cu); 50 return $this -> xmlFileContents; 51 } 52 53 if(ini_get("allow_url_fopen")) 54 { 55 if(!$remote = @fopen ($address, "r")) 56 { 57 $this -> error = "Unable to open remote XML file."; 58 return FALSE; 59 } 60 } 61 else 62 { 63 $tmp = parse_url($address); 64 if(!$remote = fsockopen ($tmp['host'], 80 ,$errno, $errstr, 10)) 65 { 66 $this -> error = "Unable to open remote XML file."; 67 return FALSE; 68 } 69 else 70 { 71 socket_set_timeout($remote, 10); 72 fputs($remote, "GET ".$headline_url." HTTP/1.0\r\n\r\n"); 73 } 74 } 75 76 $this -> xmlFileContents = ""; 77 while (!feof($remote)) 78 { 79 $this -> xmlFileContents .= fgets ($remote, 4096); 80 } 81 fclose ($remote); 82 return $this -> xmlFileContents; 83 } 84 85 86 function parseXmlContents () 87 { 88 foreach($this -> xmlData as $key => $value) 89 { 90 unset($this -> xmlData[$key]); 91 } 92 foreach($this -> counterArray as $key => $value) 93 { 94 unset($this -> counterArray[$key]); 95 } 96 97 if(!function_exists('xml_parser_create')) 98 { 99 $this->error = "XML library not available."; 100 return FALSE; 101 } 102 103 if(!$this -> xmlFileContents) 104 { 105 $this->error = "No XML source specified"; 106 return FALSE; 107 } 108 109 $this->parser = xml_parser_create(''); 110 xml_set_object($this->parser, $this); 111 xml_set_element_handler($this->parser, 'startElement', 'endElement'); 112 xml_set_character_data_handler( $this->parser, 'characterData' ); 113 114 $array = explode("\n", $this -> xmlFileContents); 115 116 foreach($array as $data) 117 { 118 119 if(strlen($data == 4096)) 120 { 121 $this -> error = "The XML cannot be parsed as it is badly formed."; 122 return FALSE; 123 } 124 125 if (!xml_parse($this->parser, $data)) 126 { 127 $this->error = sprintf('XML error: %s at line %d, column %d', xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser),xml_get_current_column_number($this->parser)); 128 return FALSE; 129 } 130 } 131 xml_parser_free( $this->parser ); 132 return $this -> xmlData; 133 } 134 135 function startElement ($p, $element, &$attrs) 136 { 137 $this -> start_tag = $element; 138 $this -> current_tag = strtolower($element); 139 if(!array_key_exists($this -> current_tag, $this -> counterArray)) 140 { 141 $this -> counterArray[$this -> current_tag] = 0; 142 $this -> xmlData[$this -> current_tag][$this -> counterArray[$this -> current_tag]] = ""; 143 } 144 } 145 146 function endElement ($p, $element) 147 { 148 if($this -> start_tag == $element) 149 { 150 $this -> counterArray[$this -> current_tag] ++; 151 } 152 } 153 154 function characterData ($p, $data) 155 { 156 $data = trim ( chop ( $data )); 157 $data = preg_replace('/&(?!amp;)/', '&', $data); 158 if(!array_key_exists($this -> current_tag, $this -> xmlData)) 159 { 160 $this -> xmlData [$this -> current_tag] = array(); 161 } 162 if(array_key_exists($this -> counterArray[$this -> current_tag], $this -> xmlData [$this -> current_tag])) 163 { 164 $this -> xmlData [$this -> current_tag] [$this -> counterArray[$this -> current_tag]] .= $data; 165 } 166 else 167 { 168 $this -> xmlData [$this -> current_tag] [$this -> counterArray[$this -> current_tag]] = $data; 169 } 170 } 171 } 172 173 //CXml class code found on php.net 174 class CXml 175 { 176 var $xml_data; 177 var $obj_data; 178 var $pointer; 179 180 function CXml() { } 181 182 function Set_xml_data( &$xml_data ) 183 { 184 $this->index = 0; 185 $this->pointer[] = &$this->obj_data; 186 187 //strip white space between tags 188 $this->xml_data = preg_replace("/>[[:space:]]+</i", "><", $xml_data); 189 $this->xml_parser = xml_parser_create( "UTF-8" ); 190 191 xml_parser_set_option( $this->xml_parser, XML_OPTION_CASE_FOLDING, false ); 192 xml_set_object( $this->xml_parser, $this ); 193 xml_set_element_handler( $this->xml_parser, "_startElement", "_endElement"); 194 xml_set_character_data_handler( $this->xml_parser, "_cData" ); 195 196 xml_parse( $this->xml_parser, $this->xml_data, true ); 197 xml_parser_free( $this->xml_parser ); 198 } 199 200 function _startElement( $parser, $tag, $attributeList ) 201 { 202 foreach( $attributeList as $name => $value ) 203 { 204 $value = $this->_cleanString( $value ); 205 $object->$name = $value; 206 } 207 //replaces the special characters with the underscore (_) in tag name 208 $tag = preg_replace("/[:\-\. ]/", "_", $tag); 209 eval( "\$this->pointer[\$this->index]->" . $tag . "[] = \$object;" ); 210 eval( "\$size = sizeof( \$this->pointer[\$this->index]->" . $tag . " );" ); 211 eval( "\$this->pointer[] = &\$this->pointer[\$this->index]->" . $tag . "[\$size-1];" ); 212 213 $this->index++; 214 } 215 216 function _endElement( $parser, $tag ) 217 { 218 array_pop( $this->pointer ); 219 $this->index--; 220 } 221 222 function _cData( $parser, $data ) 223 { 224 if (empty($this->pointer[$this->index])) { 225 if (rtrim($data, "\n")) 226 $this->pointer[$this->index] = $data; 227 } else { 228 $this->pointer[$this->index] .= $data; 229 } 230 } 231 232 function _cleanString( $string ) 233 { 234 return utf8_decode( trim( $string ) ); 235 } 236 } 237 238 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Apr 1 01:23:32 2007 | par Balluche grâce à PHPXref 0.7 |