Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
rss_feed_writer.inc
1 <?php
30 {
31 
37  var $items = Array();
38 
44  var $image = Array();
45 
51  var $channel = Array();
52 
58  var $rss = '';
59 
65  var $type = '';
66 
72  var $version = '';
73 
74 
79  function RSS_Feed_Writer()
80  {
81 
82  }//end constructor
83 
84 
93  function addItems($items)
94  {
95  if (empty($items)) {
96  return TRUE;
97  }
98  if ($this->_isTypeVersionSet()) {
99  if (!is_array($items)) {
100  trigger_error('You can assign Associative Arrays only to the items', E_USER_ERROR);
101  return FALSE;
102  }
103  $this->items = $items;
104  return TRUE;
105  } else {
106  return FALSE;
107  }
108 
109  }//end addItems()
110 
111 
120  function addChannel($channel)
121  {
122  if (empty($channel)) {
123  trigger_error('You cannot set empty values to channel', E_USER_ERROR);
124  return FALSE;
125  }
126  if ($this->_isTypeVersionSet()) {
127  if (!is_array($channel)) {
128  trigger_error('You can assign Associative Arrays only to the items', E_USER_ERROR);
129  return FALSE;
130  }
131  $this->channel = $channel;
132  return TRUE;
133  } else {
134  return FALSE;
135  }
136 
137  }//end addChannel()
138 
139 
148  function addImage($image)
149  {
150  if (empty($image)) {
151  trigger_error('You cannot set empty values to image ', E_USER_ERROR);
152  return FALSE;
153  }
154  if ($this->_isTypeVersionSet()) {
155  if (!is_array($image)) {
156  trigger_error('You can assign Associative Arrays only to the items', E_USER_ERROR);
157  return FALSE;
158  }
159  $this->image = $image;
160  return TRUE;
161  } else {
162  return FALSE;
163  }
164 
165  }//end addImage()
166 
167 
177  function setTypeVersion($type='', $version='')
178  {
179  if (empty($type) || empty($version)) {
180  trigger_error('You Have To Set The Type And The Version Of RSS Before Adding the element', E_USER_WARNING);
181  return FALSE;
182  } else {
183  if (!is_string($type) || !is_string($version)) {
184  trigger_error('The Type And Version Of The RSS Feed To Be Generated Have To Be Strings', E_USER_WARNING);
185  return FALSE;
186  }
187  $type_version = $type.'_'.$version;
188  switch ($type_version) {
189  case 'rss_1.0':
190  break;
191  case 'rss_2.0':
192  break;
193  case 'atom_1.0':
194  break;
195  default:
196  trigger_error('The Type The RSS Feed Can Only Be Either rss Or atom', E_USER_ERROR);
197  return FALSE;
198  }
199  $this->type = $type;
200  $this->version = $version;
201  return TRUE;
202  }
203 
204  }//end setTypeVersion()
205 
206 
216  function getRSSString($header=TRUE, $encoding='iso-8859-1')
217  {
218  $channel = $this->channel;
219  $image = $this->image;
220  $items = $this->items;
221  $type = $this->type;
222  $version = $this->version;
223  // if no type or version is specified then trigger error
224  // we NEED TYPE AND VERSION as this writer can build rss1.0, rss2.0, atom 1.0 versions
225  if (empty($type) || empty($version)) {
226  trigger_error('There Is No Type Or Version Set.. Please Set the Type And Version And Try Again', E_USER_WARNING);
227  return;
228  }
229  // if the channel is empty ir the items is empoty then trigger error as there is no feed without items or channel
230  if (empty($channel)) {
231  trigger_error('Channel Info Required As Per The RSS Standards', E_USER_WARNING);
232  return;
233  }
234 
235  $name = 'item';
236  ob_start();
237  if ($header) header('Content-type: text/xml');
238  echo '<?xml version="1.0" encoding="'.$encoding.'"?>';
239 
240  // open tag for atom is feed
241  // for rss 1.0 it is rdf:RDF
242  // for rss 2.0 it is rss version 2.0
243  if (strcasecmp($type, 'atom') == 0) {
244  $type = 'feed';
245  $name = 'entry';
246  }
247  if (($type == 'rss') && ($version == '1.0')) {
248  echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/">';
249  } else {
250  echo '<'.$type;
251  if ($type == 'feed') {
252  echo ' xml:lang="en" xmlns="http://www.w3.org/2005/Atom"';
253  } else {
254  echo ' version="'.$version.'"';
255  }
256  echo '>';
257  }
258  // you do not have channel tag in atom 1.0 Instead we use feed tag itself
259  if ($type != 'feed') {
260  $type .= ' '.$version;
261 
262  if ($type == 'rss 2.0') {
263  echo '<channel>';
264  } else {
265  if (isset($channel['rdf:about'])) {
266  echo '<channel rdf:about="'.$channel['rdf:about'].'">';
267  } else {
268  trigger_error('A required attribute "rdf:about" is undefined for the Channel tag', E_USER_ERROR);
269  ob_end_clean();
270  return '';
271  }
272  }
273  }
274  // add data to the chaannel or the feed tags
275  foreach ($channel as $key => $info) {
276  if ($key == 'rdf:about') continue;
277  if (is_array($info)) {
278  if ($type != 'feed') {
279  trigger_error('Attributes And SubTags In Channel Not Allowed For RSS Versions', E_USER_ERROR);
280  ob_end_clean();
281  return '';
282  }
283  $keys = array_keys($info);
284  if (in_array('ATTRIBUTES', $keys)) {
285  echo '<'.$key.' ';
286  $attributes = $info['ATTRIBUTES'];
287  foreach ($attributes as $sub => $sub_info) {
288  // change the tags if any present in the info
289  $sub_info = str_replace('&', '&amp;', $sub_info);
290  $sub_info = str_replace('<', '&lt;', $sub_info);
291  $sub_info = str_replace('>', '&gt;', $sub_info);
292  echo $sub.'="'.$sub_info.'" ';
293  }
294  echo '>';
295  }
296  if (in_array('SUBTAGS', $keys)) {
297  echo '<'.$key.'>';
298  $sub_tags = $info['SUBTAGS'];
299  foreach ($sub_tags as $sub => $sub_info) {
300  // change the tags if any present in the info
301  $sub_info = str_replace('&', '&amp;', $sub_info);
302  $sub_info = str_replace('<', '&lt;', $sub_info);
303  $sub_info = str_replace('>', '&gt;', $sub_info);
304  echo '<'.$sub.'>'.$sub_info.'</'.$sub.'>';
305  }
306  }
307  echo '</'.$key.'>';
308  } else {
309  // change the tags if any present in the info
310  $info = str_replace('&', '&amp;', $info);
311  $info = str_replace('<', '&lt;', $info);
312  $info = str_replace('>', '&gt;', $info);
313  echo '<'.$key.'>'.$info.'</'.$key.'>';
314  }
315  }//end foreach channnel
316 
317  if ($type == 'rss 1.0') {
318  echo '<items><rdf:Seq>';
319  foreach ($items as $item) {
320  echo '<rdf:li rdf:resource="'.$item['rdf:about'].'"/>';
321  }
322  echo '</rdf:Seq></items></channel>';
323  }
324 
325  if (!empty($image)) {
326  if ($type == 'feed') {
327  echo '<logo>';
328  } else {
329  echo '<image>';
330  }
331  foreach ($image as $key => $info) {
332  // change the tags if any present in the info
333  $info = str_replace('&', '&amp;', $info);
334  $info = str_replace('<', '&lt;', $info);
335  $info = str_replace('>', '&gt;', $info);
336  if (is_array($info)) {
337  echo '<'.$key.'>';
338  foreach ($info as $sub => $sub_info) {
339  echo '<'.$sub.'>'.$sub_info.'</'.$sub.'>';
340  }
341  echo '</'.$key.'>';
342  } else {
343  echo '<'.$key.'>'.$info.'</'.$key.'>';
344  }
345  }
346  if ($type == 'feed') {
347  echo '</logo>';
348  } else {
349  echo '</image>';
350  }
351  }
352 
353  foreach ($items as $item) {
354  if ($type != 'rss 1.0') {
355  echo '<'.$name.'>';
356  } else {
357  echo '<item rdf:about="'.$item['rdf:about'].'">';
358  unset($item['rdf:about']);
359  }
360  foreach ($item as $key => $info) {
361  if (is_array($info)) {
362  $keys = array_keys($info);
363  if (in_array('ATTRIBUTES', $keys)) {
364  echo '<'.$key.' ';
365  $attributes = $info['ATTRIBUTES'];
366  foreach ($attributes as $sub => $sub_info) {
367  // change the tags if any present in the info
368  $sub_info = str_replace('&', '&amp;', $sub_info);
369  $sub_info = str_replace('<', '&lt;', $sub_info);
370  $sub_info = str_replace('>', '&gt;', $sub_info);
371  echo $sub.'="'.$sub_info.'" ';
372  }
373  echo '>';
374  }
375  if (in_array('SUBTAGS', $keys)) {
376  echo '<'.$key.'>';
377  $sub_tags = $info['SUBTAGS'];
378  foreach ($sub_tags as $sub => $sub_info) {
379  // change the tags if any present in the info
380  $sub_info = str_replace('&', '&amp;', $sub_info);
381  $sub_info = str_replace('<', '&lt;', $sub_info);
382  $sub_info = str_replace('>', '&gt;', $sub_info);
383  echo '<'.$sub.'>'.$sub_info.'</'.$sub.'>';
384  }
385  }
386  echo '</'.$key.'>';
387  } else {
388  // change the tags if any present in the info
389  $info = str_replace('&', '&amp;', $info);
390  $info = str_replace('<', '&lt;', $info);
391  $info = str_replace('>', '&gt;', $info);
392  echo '<'.$key.'>'.$info.'</'.$key.'>';
393  }
394  }//end foreach item
395  echo '</'.$name.'>';
396  }//end foreach items
397 
398  if ($type != 'rss 1.0') {
399  if ($type != 'feed') {
400  // rss 2.0
401  echo '</channel>';
402  echo '</'.$this->type.'>';
403  } else {
404  // atom 1.0
405  echo '</feed>';
406  }
407  } else {
408  // rss 1.0
409  echo '</rdf:RDF>';
410  }
411 
412  $rss = ob_get_contents();
413  ob_end_clean();
414  return $rss;
415 
416  }//end getRSSString()
417 
418 
419 //-- PRIVATE OR INTERNAL FUNCTIONS --//
420 
421 
428  function _isTypeVersionSet()
429  {
430  if (!empty($this->type) && (!empty($this->version))) {
431  return TRUE;
432  } else {
433  trigger_error('You Have To Set The Type And The Version Of RSS Before Adding the element', E_USER_WARNING);
434  return FALSE;
435  }
436 
437  }//end _isTypeVersionSet()
438 
439 
440 }//end class
441 
442 
443 ?>