]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/atomlib.php
WordPress 4.7.2-scripts
[autoinstalls/wordpress.git] / wp-includes / atomlib.php
1 <?php
2 /**
3  * Atom Syndication Format PHP Library
4  *
5  * @package AtomLib
6  * @link http://code.google.com/p/phpatomlib/
7  *
8  * @author Elias Torres <elias@torrez.us>
9  * @version 0.4
10  * @since 2.3.0
11  */
12
13 /**
14  * Structure that store common Atom Feed Properties
15  *
16  * @package AtomLib
17  */
18 class AtomFeed {
19         /**
20          * Stores Links
21          * @var array
22          * @access public
23          */
24     var $links = array();
25     /**
26      * Stores Categories
27      * @var array
28      * @access public
29      */
30     var $categories = array();
31         /**
32          * Stores Entries
33          *
34          * @var array
35          * @access public
36          */
37     var $entries = array();
38 }
39
40 /**
41  * Structure that store Atom Entry Properties
42  *
43  * @package AtomLib
44  */
45 class AtomEntry {
46         /**
47          * Stores Links
48          * @var array
49          * @access public
50          */
51     var $links = array();
52     /**
53      * Stores Categories
54      * @var array
55          * @access public
56      */
57     var $categories = array();
58 }
59
60 /**
61  * AtomLib Atom Parser API
62  *
63  * @package AtomLib
64  */
65 class AtomParser {
66
67     var $NS = 'http://www.w3.org/2005/Atom';
68     var $ATOM_CONTENT_ELEMENTS = array('content','summary','title','subtitle','rights');
69     var $ATOM_SIMPLE_ELEMENTS = array('id','updated','published','draft');
70
71     var $debug = false;
72
73     var $depth = 0;
74     var $indent = 2;
75     var $in_content;
76     var $ns_contexts = array();
77     var $ns_decls = array();
78     var $content_ns_decls = array();
79     var $content_ns_contexts = array();
80     var $is_xhtml = false;
81     var $is_html = false;
82     var $is_text = true;
83     var $skipped_div = false;
84
85     var $FILE = "php://input";
86
87     var $feed;
88     var $current;
89
90         /**
91          * PHP5 constructor.
92          */
93     function __construct() {
94
95         $this->feed = new AtomFeed();
96         $this->current = null;
97         $this->map_attrs_func = create_function('$k,$v', 'return "$k=\"$v\"";');
98         $this->map_xmlns_func = create_function('$p,$n', '$xd = "xmlns"; if(strlen($n[0])>0) $xd .= ":{$n[0]}"; return "{$xd}=\"{$n[1]}\"";');
99     }
100
101         /**
102          * PHP4 constructor.
103          */
104         public function AtomParser() {
105                 self::__construct();
106         }
107
108     function _p($msg) {
109         if($this->debug) {
110             print str_repeat(" ", $this->depth * $this->indent) . $msg ."\n";
111         }
112     }
113
114     function error_handler($log_level, $log_text, $error_file, $error_line) {
115         $this->error = $log_text;
116     }
117
118     function parse() {
119
120         set_error_handler(array(&$this, 'error_handler'));
121
122         array_unshift($this->ns_contexts, array());
123
124         if ( ! function_exists( 'xml_parser_create_ns' ) ) {
125                 trigger_error( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) );
126                 return false;
127         }
128
129         $parser = xml_parser_create_ns();
130         xml_set_object($parser, $this);
131         xml_set_element_handler($parser, "start_element", "end_element");
132         xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
133         xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,0);
134         xml_set_character_data_handler($parser, "cdata");
135         xml_set_default_handler($parser, "_default");
136         xml_set_start_namespace_decl_handler($parser, "start_ns");
137         xml_set_end_namespace_decl_handler($parser, "end_ns");
138
139         $this->content = '';
140
141         $ret = true;
142
143         $fp = fopen($this->FILE, "r");
144         while ($data = fread($fp, 4096)) {
145             if($this->debug) $this->content .= $data;
146
147             if(!xml_parse($parser, $data, feof($fp))) {
148                 /* translators: 1: error message, 2: line number */
149                 trigger_error(sprintf(__('XML Error: %1$s at line %2$s')."\n",
150                     xml_error_string(xml_get_error_code($parser)),
151                     xml_get_current_line_number($parser)));
152                 $ret = false;
153                 break;
154             }
155         }
156         fclose($fp);
157
158         xml_parser_free($parser);
159
160         restore_error_handler();
161
162         return $ret;
163     }
164
165     function start_element($parser, $name, $attrs) {
166
167         $tag = array_pop(explode(":", $name));
168
169         switch($name) {
170             case $this->NS . ':feed':
171                 $this->current = $this->feed;
172                 break;
173             case $this->NS . ':entry':
174                 $this->current = new AtomEntry();
175                 break;
176         };
177
178         $this->_p("start_element('$name')");
179         #$this->_p(print_r($this->ns_contexts,true));
180         #$this->_p('current(' . $this->current . ')');
181
182         array_unshift($this->ns_contexts, $this->ns_decls);
183
184         $this->depth++;
185
186         if(!empty($this->in_content)) {
187
188             $this->content_ns_decls = array();
189
190             if($this->is_html || $this->is_text)
191                 trigger_error("Invalid content in element found. Content must not be of type text or html if it contains markup.");
192
193             $attrs_prefix = array();
194
195             // resolve prefixes for attributes
196             foreach($attrs as $key => $value) {
197                 $with_prefix = $this->ns_to_prefix($key, true);
198                 $attrs_prefix[$with_prefix[1]] = $this->xml_escape($value);
199             }
200
201             $attrs_str = join(' ', array_map($this->map_attrs_func, array_keys($attrs_prefix), array_values($attrs_prefix)));
202             if(strlen($attrs_str) > 0) {
203                 $attrs_str = " " . $attrs_str;
204             }
205
206             $with_prefix = $this->ns_to_prefix($name);
207
208             if(!$this->is_declared_content_ns($with_prefix[0])) {
209                 array_push($this->content_ns_decls, $with_prefix[0]);
210             }
211
212             $xmlns_str = '';
213             if(count($this->content_ns_decls) > 0) {
214                 array_unshift($this->content_ns_contexts, $this->content_ns_decls);
215                 $xmlns_str .= join(' ', array_map($this->map_xmlns_func, array_keys($this->content_ns_contexts[0]), array_values($this->content_ns_contexts[0])));
216                 if(strlen($xmlns_str) > 0) {
217                     $xmlns_str = " " . $xmlns_str;
218                 }
219             }
220
221             array_push($this->in_content, array($tag, $this->depth, "<". $with_prefix[1] ."{$xmlns_str}{$attrs_str}" . ">"));
222
223         } else if(in_array($tag, $this->ATOM_CONTENT_ELEMENTS) || in_array($tag, $this->ATOM_SIMPLE_ELEMENTS)) {
224             $this->in_content = array();
225             $this->is_xhtml = $attrs['type'] == 'xhtml';
226             $this->is_html = $attrs['type'] == 'html' || $attrs['type'] == 'text/html';
227             $this->is_text = !in_array('type',array_keys($attrs)) || $attrs['type'] == 'text';
228             $type = $this->is_xhtml ? 'XHTML' : ($this->is_html ? 'HTML' : ($this->is_text ? 'TEXT' : $attrs['type']));
229
230             if(in_array('src',array_keys($attrs))) {
231                 $this->current->$tag = $attrs;
232             } else {
233                 array_push($this->in_content, array($tag,$this->depth, $type));
234             }
235         } else if($tag == 'link') {
236             array_push($this->current->links, $attrs);
237         } else if($tag == 'category') {
238             array_push($this->current->categories, $attrs);
239         }
240
241         $this->ns_decls = array();
242     }
243
244     function end_element($parser, $name) {
245
246         $tag = array_pop(explode(":", $name));
247
248         $ccount = count($this->in_content);
249
250         # if we are *in* content, then let's proceed to serialize it
251         if(!empty($this->in_content)) {
252             # if we are ending the original content element
253             # then let's finalize the content
254             if($this->in_content[0][0] == $tag &&
255                 $this->in_content[0][1] == $this->depth) {
256                 $origtype = $this->in_content[0][2];
257                 array_shift($this->in_content);
258                 $newcontent = array();
259                 foreach($this->in_content as $c) {
260                     if(count($c) == 3) {
261                         array_push($newcontent, $c[2]);
262                     } else {
263                         if($this->is_xhtml || $this->is_text) {
264                             array_push($newcontent, $this->xml_escape($c));
265                         } else {
266                             array_push($newcontent, $c);
267                         }
268                     }
269                 }
270                 if(in_array($tag, $this->ATOM_CONTENT_ELEMENTS)) {
271                     $this->current->$tag = array($origtype, join('',$newcontent));
272                 } else {
273                     $this->current->$tag = join('',$newcontent);
274                 }
275                 $this->in_content = array();
276             } else if($this->in_content[$ccount-1][0] == $tag &&
277                 $this->in_content[$ccount-1][1] == $this->depth) {
278                 $this->in_content[$ccount-1][2] = substr($this->in_content[$ccount-1][2],0,-1) . "/>";
279             } else {
280                 # else, just finalize the current element's content
281                 $endtag = $this->ns_to_prefix($name);
282                 array_push($this->in_content, array($tag, $this->depth, "</$endtag[1]>"));
283             }
284         }
285
286         array_shift($this->ns_contexts);
287
288         $this->depth--;
289
290         if($name == ($this->NS . ':entry')) {
291             array_push($this->feed->entries, $this->current);
292             $this->current = null;
293         }
294
295         $this->_p("end_element('$name')");
296     }
297
298     function start_ns($parser, $prefix, $uri) {
299         $this->_p("starting: " . $prefix . ":" . $uri);
300         array_push($this->ns_decls, array($prefix,$uri));
301     }
302
303     function end_ns($parser, $prefix) {
304         $this->_p("ending: #" . $prefix . "#");
305     }
306
307     function cdata($parser, $data) {
308         $this->_p("data: #" . str_replace(array("\n"), array("\\n"), trim($data)) . "#");
309         if(!empty($this->in_content)) {
310             array_push($this->in_content, $data);
311         }
312     }
313
314     function _default($parser, $data) {
315         # when does this gets called?
316     }
317
318
319     function ns_to_prefix($qname, $attr=false) {
320         # split 'http://www.w3.org/1999/xhtml:div' into ('http','//www.w3.org/1999/xhtml','div')
321         $components = explode(":", $qname);
322
323         # grab the last one (e.g 'div')
324         $name = array_pop($components);
325
326         if(!empty($components)) {
327             # re-join back the namespace component
328             $ns = join(":",$components);
329             foreach($this->ns_contexts as $context) {
330                 foreach($context as $mapping) {
331                     if($mapping[1] == $ns && strlen($mapping[0]) > 0) {
332                         return array($mapping, "$mapping[0]:$name");
333                     }
334                 }
335             }
336         }
337
338         if($attr) {
339             return array(null, $name);
340         } else {
341             foreach($this->ns_contexts as $context) {
342                 foreach($context as $mapping) {
343                     if(strlen($mapping[0]) == 0) {
344                         return array($mapping, $name);
345                     }
346                 }
347             }
348         }
349     }
350
351     function is_declared_content_ns($new_mapping) {
352         foreach($this->content_ns_contexts as $context) {
353             foreach($context as $mapping) {
354                 if($new_mapping == $mapping) {
355                     return true;
356                 }
357             }
358         }
359         return false;
360     }
361
362     function xml_escape($string)
363     {
364              return str_replace(array('&','"',"'",'<','>'),
365                 array('&amp;','&quot;','&apos;','&lt;','&gt;'),
366                 $string );
367     }
368 }