-
Notifications
You must be signed in to change notification settings - Fork 0
/
FeedWriter.php
executable file
·1194 lines (1031 loc) · 37.9 KB
/
FeedWriter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
//error_reporting(E_ALL); // DEBUG
if ( function_exists( 'mb_detect_order' ) )
mb_detect_order( "UTF-8,eucjp-win,sjis-win" );
require_once( "EssDTD.php" );
require_once( 'FeedValidator.php' );
require_once( 'EventFeed.php' );
/**
* Universal ESS Feed Writer class
* Generate ESS Feed v0.9
*
* @package ESSFeedWriter
* @author Brice Pissard
* @copyright No Copyright.
* @license GNU/GPLv2, see http://www.gnu.org/licenses/gpl-2.0.html
* @link http://essfeed.org
* @link https://github.com/essfeed
*/
final class FeedWriter
{
const LIBRARY_VERSION = '1.5'; // GitHub library versioning control.
const ESS_VERSION = '0.9'; // ESS Feed version.
const CHARSET = 'UTF-8'; // Defines the encoding Chartset for the whole document and the value inserted.
public $lang = 'en'; // Default 2 chars language (ISO 3166-1).
public $DEBUG = FALSE; // output debug information.
public $AUTO_PUSH = TRUE; // Defines feed changes have to be submited to ESS Aggregators.
public $IS_DOWNLOAD = FALSE; // Defines if the feed is to be downloaded (with Header: application/ess+xml).
const EMAIL_UP_TO_DATE = TRUE; // Defines if an email is sent to system administrator if the version is not up-to-date.
const REPLACE_ACCENT = FALSE; // if some problems occured durring encoding/decoding the data into UTF8, this parameter set to TRUE force the replacement of åççéñts by accents.
private $channel = array(); // Collection of Channel elements.
private $items = array(); // Collection of items as object of FeedItem class.
const TB = ' '; // Display a tabulation (for humans).
const LN = '
'; // Display breaklines (for humans).
/**
* FeedWriter Class Constructor
*
* @access public
* @param String [OPTIONAL] 2 chars language (ISO 3166-1) definition for the current feed.
* @param Array [OPTIONAL] array of event's feed tags definition.
* @return void
*/
function __construct( $lang='en', $data_=NULL )
{
if ( function_exists( 'set_error_handler' ) )
set_error_handler( array( 'FeedWriter', 'error_handler' ) );
$channelDTD = EssDTD::getChannelDTD(); // DTD Array of Channel first XML child elements.
$this->lang = ( strlen( $lang ) == 2 )? strtolower( $lang ) : $this->lang;
$this->setGenerator( 'ess:php:generator:version:' . self::LIBRARY_VERSION );
$mandatoryRequiredCount = 0;
$mandatoryCount = 0;
if ( $data_ != NULL )
{
if ( count( $data_ ) > 0 )
{
foreach ( $data_ as $key => $el )
{
switch ( $key )
{
case 'title': $this->setTitle( $el ); if ( $channelDTD[ $key ] == TRUE ) $mandatoryCount++; break;
case 'link': $this->setLink( $el ); if ( $channelDTD[ $key ] == TRUE ) $mandatoryCount++;$mandatoryCount++; break; // + element ID
case 'published': $this->setPublished( $el ); if ( $channelDTD[ $key ] == TRUE ) $mandatoryCount++; break;
case 'updated': $this->setUpdated( $el ); if ( $channelDTD[ $key ] == TRUE ) $mandatoryCount++; break;
case 'generator': $this->setGenerator( $el ); if ( $channelDTD[ $key ] == TRUE ) $mandatoryCount++; break;
case 'rights': $this->setRights( $el ); if ( $channelDTD[ $key ] == TRUE ) $mandatoryCount++; break;
default: throw new Exception("Error: XML Channel element < ".$key." > is not defined within ESS DTD." ); break;
}
}
foreach ( $channelDTD as $kk => $val )
{
if ( $val == TRUE && $kk != 'feed' )
$mandatoryRequiredCount++;
}
if ( $mandatoryRequiredCount != $mandatoryCount || $mandatoryCount == 0 )
{
$out = '';
foreach ( $channelDTD as $key => $m)
{
if ( $m == TRUE )
$out .= "< $key >, ";
}
throw new Exception( "Error: All XML Channel's mandatory elements are required: ". $out );
}
}
}
}
private function t( $num )
{
$text = "";
for ( $i=1; $i <= $num ; $i++ )
$text .= self::TB;
return $text;
}
/**
* Set a channel element
*
* @access public
* @see http://essfeed.org/index.php/ESS_structure
*
* @param String name of the channel tag.
* @param String content of the channel tag.
* @return void
*/
private function setChannelElement( $elementName, $content )
{
if ( is_string( $content ) )
$content = FeedValidator::xml_entities( $content, self::CHARSET );
$this->channel[ $elementName ] = $content;
}
/**
* Genarate the ESS Feed On-the-fly or create a file on local server disk.
* If the feed is generated and record on the server it consume less load on PHP and Database resources.
*
* @access public
* @return void
*/
public function genarateFeed( $filePath='', $displayResult=TRUE )
{
if ( function_exists( 'mb_internal_encoding' ) )
mb_internal_encoding( self::CHARSET );
if ( $this->DEBUG == FALSE && $displayResult == TRUE )
{
if ( function_exists( 'header_remove' ) )
header_remove();
header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header( "Cache-Control: no-cache" );
header( "Pragma: no-cache" );
header( "Keep-Alive: timeout=1, max=1" );
if ( $this->IS_DOWNLOAD ) { header( 'Content-Type: application/ess+xml; charset=' .self::CHARSET ); }
else { header( 'Content-Type: text/xml; charset=' .self::CHARSET ); }
}
$feedData = $this->getFeedData();
//var_dump( $feedData );
if ( FeedValidator::isNull( $filePath ) == FALSE && $this->DEBUG == FALSE )
$this->genarateFeedFile( $filePath, $feedData );
if ( function_exists( 'restore_error_handler' ) )
restore_error_handler();
if ( $displayResult == TRUE )
echo $feedData;
else
return $feedData;
}
/**
* Genarate the ESS File
*
* @access public
* @param String Local server path where the feed will be stored.
* @param URL URL of the same feed but available online. this URL will be used to broadcast your event to events search engines.
* @return void
*/
public function genarateFeedFile( $filePath='', $feedData=NULL )
{
if ( function_exists( 'mb_internal_encoding' ) )
mb_internal_encoding( self::CHARSET );
try
{
$fp = fopen( $filePath, 'w' );
if ( $fp !== FALSE )
{
fwrite( $fp, ( ( $feedData != NULL )? $feedData : $this->getFeedData() ) );
fclose( $fp );
}
}
catch( ErrorException $error )
{
throw new Exception( "Error: Impossible to generate the ESS file on local server disk: " . $error );
return;
}
}
/**
* Get ESS Feed data in String format.
*
* @access public
* @return String
*/
public function getFeedData()
{
$out = "";
$out .= $this->getHead();
$out .= $this->getChannel();
$out .= $this->getItems();
$out .= $this->getEndChannel();
$this->pushToAggregators( '', $out );
return $out;
}
/**
* Create a new EventFeed.
*
* @access public
* @return Object instance of EventFeed class
*/
public function newEventFeed( Array $arr_= NULL )
{
$newEvent = new EventFeed( NULL, self::CHARSET, self::REPLACE_ACCENT );
if ( $arr_ )
{
if ( count( $arr_ ) > 0 )
{
if ( isset( $arr_['title'] ) ) { if ( FeedValidator::isNull( $arr_['title'] ) == FALSE ) { $newEvent->setTitle( $arr_['title'] ); }}
if ( isset( $arr_['uri'] ) ) { if ( FeedValidator::isNull( $arr_['uri'] ) == FALSE ) { $newEvent->setUri( $arr_['uri'] ); }}
if ( isset( $arr_['published'] ) ) { if ( FeedValidator::isValidDate( $arr_['published'] ) == TRUE ) { $newEvent->setPublished( $arr_['published'] ); } else { $newEvent->setPublished( self::getISODate() ); }}
if ( isset( $arr_['updated'] ) ) { if ( FeedValidator::isValidDate( $arr_['updated'] ) == TRUE ) { $newEvent->setUpdated( $arr_['updated'] ); }}
if ( isset( $arr_['access'] ) ) { if ( FeedValidator::isNull( $arr_['access'] ) == FALSE ) { $newEvent->setAccess( $arr_['access'] ); } else { $newEvent->setAccess( EssDTD::ACCESS_PUBLIC ); }}
if ( isset( $arr_['description']) ) { if ( FeedValidator::isNull( $arr_['description'] ) == FALSE ) { $newEvent->setDescription( $arr_['description'] ); }}
if ( isset( $arr_['tags'] ) ) { if ( count( $arr_['tags'] ) > 0 ) { $newEvent->setTags( $arr_['tags'] ); }}
}
}
return $newEvent;
}
/**
* Add a EventFeed to the main class
*
* @access public
* @param Object instance of EventFeed class
* @return void
*/
public function addItem( $eventFeed )
{
$this->items[] = $eventFeed;
}
public static function getCurrentURL()
{
$protocol = ( ( isset( $_SERVER[ 'SERVER_PROTOCOL' ] ) )? ( ( stripos( $_SERVER[ 'SERVER_PROTOCOL' ], 'https' ) === TRUE )? 'https://' : 'http://' ) : 'http://' );
$host = ( ( isset( $_SERVER[ 'HTTP_HOST' ] ) )? $_SERVER[ 'HTTP_HOST' ] : '' );
$request = ( ( isset( $_SERVER[ 'REQUEST_URI' ] ) )? $_SERVER[ 'REQUEST_URI' ] : '' );
return $protocol . $host . $request;
}
// -------------------------------------
// -- Getter/Setter Wrapper Functions -------------------------------------------------------------------
// -------------------------------------
/**
* Set the 'title' channel element
*
* @access public
* @see http://essfeed.org/index.php/ESS_structure
*
* @param String value of 'title' channel tag.
* Define the language-sensitive feed title.
* Should not be longer then 128 characters.
* @return void
*/
public function setTitle( $el=NULL )
{
$elNane = 'title';
if ( self::controlChannelElements( $elNane, $el ) )
{
$this->setChannelElement( $elNane, ( self::REPLACE_ACCENT )? FeedValidator::noAccent( $el, $this->CHARSET ) : $el );
if ( !isset( $this->channel[ 'id' ] ) || FeedValidator::isNull( $this->channel[ 'id' ] ) )
$this->setId( $el );
}
else throw new Exception( "Error: '< channel >< $elNane>' XML element is mandatory and can not be empty." );
}
public function getTitle()
{
return $this->channel[ 'title' ];
}
/**
* Set the 'link' channel element
*
* @access public
* @see http://essfeed.org/index.php/ESS_structure
*
* @param String value of 'link' channel tag.
* Define the feed URL.
* @return void
*/
public function setLink( $el=NULL )
{
$elNane = 'link';
if ( self::controlChannelElements( $elNane, $el ) )
{
$this->setChannelElement( $elNane, ( self::REPLACE_ACCENT )? FeedValidator::noAccent( $el, $this->CHARSET ) : $el );
if ( !isset( $this->channel[ 'id' ] ) || FeedValidator::isNull( $this->channel[ 'id' ] ) )
$this->setId( $el );
}
else throw new Exception( "Error: '< channel >< $elNane>' XML element is mandatory and can not be empty, it must be a valid URL that specified the location of this feed." );
}
public function getLink()
{
return $this->channel[ 'link' ];
}
/**
* Set the 'id' channel element
*
* @access public
* @param String value of 'id' channel tag
* @return void
*/
public function setId( $el=NULL )
{
$elNane = 'id';
if ( self::controlChannelElements( $elNane, $el ) )
$this->setChannelElement( $elNane, $this->uuid( $el, 'ESSID:' ) );
else throw new Exception( "Error: '< channel >< $elNane>' XML element is mandatory and can not be empty." );
}
public function getId()
{
return $this->channel[ 'id' ];
}
/**
* Set the 'generator' channel element
*
* @access public
* @param String value of 'generator' channel tag
* @return void
*/
public function setGenerator( $el=NULL )
{
$elNane = 'generator';
if ( self::controlChannelElements( $elNane, $el ) )
$this->setChannelElement( $elNane, ( self::REPLACE_ACCENT )? FeedValidator::noAccent( $el, $this->CHARSET ) : $el );
else throw new Exception( "Error: '< channel >< $elNane>' XML element, if it is defined, can not be empty." );
}
public function getGenerator()
{
return $this->channel[ 'generator' ];
}
/**
* Set the 'published' channel element
*
* @access public
* @see http://essfeed.org/index.php/ESS_structure
*
* @param String Value of 'published' channel tag.
* Must be an UTC Date format (ISO 8601).
* e.g. 2013-10-31T15:30:59+02:00 in Paris or 2013-10-31T15:30:59-08:00 in San Francisco
*
* @return void
*/
public function setPublished( $el='now' )
{
$elNane = 'published';
if ( self::controlChannelElements( $elNane, $el ) )
$this->setChannelElement( $elNane, FeedWriter::getISODate( $el ) );
else throw new Exception( "Error: '< channel >< $elNane>' XML element is mandatory and can not be empty." );
}
public function getPublished()
{
return $this->channel[ 'published' ];
}
/**
* Set the 'updated' channel element
*
* @access public
* @see http://essfeed.org/index.php/ESS_structure
*
* @param String Value of 'updated' channel tag.
* Must be an UTC Date format (ISO 8601).
* e.g. 2013-10-31T15:30:59Z in Paris or 2013-10-31T15:30:59+0800 in San Francisco
* @return void
*/
public function setUpdated( $el='now' )
{
$elNane = 'updated';
if ( self::controlChannelElements( $elNane, $el ) )
$this->setChannelElement( 'updated', FeedWriter::getISODate( $el ) );
else throw new Exception( "Error: '< channel >< $elNane>' XML element, if it is defined, can not be empty." );
}
public function getUpdated()
{
return $this->channel[ 'updated' ];
}
/**
* Set the 'rights' channel element
*
* @access public
* @see http://essfeed.org/index.php/ESS_structure
*
* @param String value of 'rights' channel tag.
* Define the Feed proprietary rights.
* Should not be longer then 512 chars.
*
* @return void
*/
public function setRights( $el=NULL )
{
$elNane = 'rights';
if ( self::controlChannelElements( $elNane, $el ) )
$this->setChannelElement( $elNane, ( self::REPLACE_ACCENT )? FeedValidator::noAccent( $el, $this->CHARSET ) : $el );
else throw new Exception( "Error: '< channel >< $elNane>' XML element, if it is defined, can not be empty." );
}
public function getRights()
{
return $this->channel[ 'rights' ];
}
/**
* Generates an UUID
*
* @author Anis uddin Ahmad <[email protected]>
* @access public
* @param String [OPTIONAL] String prefix
* @return String the formated uuid
*/
public static function uuid( $key=NULL, $prefix='ESSID:' )
{
$key = ( FeedValidator::isNull( $key ) )? uniqid( rand() ) : $key;
$chars = md5( $key );
$uuid = substr( $chars, 0,8 ) . '-';
$uuid .= substr( $chars, 8,4 ) . '-';
$uuid .= substr( $chars, 12,4 ) . '-';
$uuid .= substr( $chars, 16,4 ) . '-';
$uuid .= substr( $chars, 20,12 );
return $prefix . $uuid;
}
/**
* Generate or convert a String or an Integer parameter into an ISO 8601 Date format.
*
* @access public
* @param Object date in seconds OR in convertible String Date (http://php.net/manual/en/function.strtotime.php)
* to convert in a ISO 8601 Date format: 'Y-m-d\TH:i:sZ'
* @return String
*/
public static function getISODate( $date=NULL )
{
if ( FeedValidator::isNull( $date ) == FALSE )
{
if ( strlen( $date ) >= 8 && !is_int( $date ) )
{
if ( FeedValidator::isValidDate( $date ) )
{
return $date;
}
else
{
return ( FeedValidator::isNull( strtotime( $date ) ) )?
self::getISODate()
:
date( DateTime::ATOM, strtotime( $date )
);
}
}
else if ( intval( $date ) > 0 && FeedValidator::isOnlyNumsChars( $date ) )
return date( DateTime::ATOM, $date );
}
else
{
$datetime_template = 'Y-m-d\TH:i:s';
// control if PHP is configured with the same timezone then the server
$timezone_server = exec( 'date +%:z' );
$timezone_php = date( 'P' );
if ( strlen( $timezone_server ) > 0 && $timezone_php != $timezone_server )
return date( $datetime_template, exec( "date --date='@" . date( 'U' ) . "'" ) ) . $timezone_server;
else
{
if ( date_default_timezone_get() == 'UTC' )
$offsetString = 'Z'; // No need to calculate offset, as default timezone is already UTC
else
{
$phpTime = date( $datetime_template );
$millis = strtotime( $phpTime ); // Convert time to milliseconds since 1970, using default timezone
$timezone = new DateTimeZone( date_default_timezone_get() ); // Get default system timezone to create a new DateTimeZone object
$offset = $timezone->getOffset( new DateTime( $phpTime ) ); // Offset in seconds to UTC
$offsetHours = round( abs( $offset ) / 3600 );
$offsetMinutes = round( ( abs( $offset ) - $offsetHours * 3600 ) / 60 );
$offsetString = ($offset < 0 ? '-' : '+' )
. ( $offsetHours < 10 ? '0' : '' ) . $offsetHours
. ':'
. ( $offsetMinutes < 10 ? '0' : '' ) . $offsetMinutes;
}
return date( $datetime_template, $millis ) . $offsetString;
}
}
return addslashes( date( DateTime::ATOM, date( 'U' ) ) );
}
/**
* Extract images URL from a blog HTML content.
*
* @access public
* @param String HTML content that can content fom <img /> XHTML element
* @return Array Return an array of the images URL founds.
*/
public static function getMediaURLfromHTML( $text=NULL )
{
$media_ = array();
if ( strlen( trim( $text ) ) > 0 )
{
$tt = preg_match_all( '/<(source|iframe|embed|param|img)[^>]+src=[\'"]([^\'"]+)[\'"].*>/i', str_replace( '><', '>
<', FeedValidator::removeBreaklines( $text,'
' ) ), $matches );
if ( $tt > 0 && count( $matches[ 2 ] ) > 0 )
{
foreach ( $matches[ 2 ] as $i => $value )
{
$sb1 = array();
$sb2 = array();
$sb3 = array();
$sb4 = array();
if ( FeedValidator::isValidURL( $value ) )
{
$simple_tag = str_replace( "'","\"",strtolower( stripcslashes( $matches[ 0 ][ $i ] ) ) );
$sb1 = explode( 'title="', $simple_tag );
if ( count( $sb1 ) > 1 )
$sb3 = explode( '"', $sb1[1] );
$sb2 = explode( 'alt="', $simple_tag );
if ( count( $sb2 ) > 1 )
$sb4 = explode( '"', $sb2[1] );
$media_type = FeedValidator::getMediaType( $value );
array_push(
$media_,
array(
'uri' => $value,
'type' => $media_type,
'name' => ( ( isset( $sb3[ 0 ] ) )? $sb3[ 0 ] : ( ( isset( $sb4[ 0 ] ) )? $sb4[ 0 ] : $media_type . " - " . $i ) )
)
);
}
}
}
// Strip HTML content and analyzed individual world to find URL in the text. (CF: MediaWiki content).
$text_split = explode( ' ', FeedValidator::getOnlyText( $text, self::CHARSET ) );
if ( count( $text_split ) > 0 )
{
foreach ( $text_split as $value )
{
foreach ( array( 'image', 'sound', 'video' ) as $media_type )
{
if ( FeedValidator::isValidURL( $value ) )
{
if ( FeedValidator::isValidMediaURL( $value, $media_type ) )
{
if ( !in_array( $value, $media_ ) )
{
array_push( $media_, array(
'uri' => $value,
'type' => $media_type,
'name' => $media_type
) );
}
}
}
}
}
}
}
return $media_;
}
private static function controlChannelElements( $elmName, $val=NULL )
{
switch ( $elmName )
{
default : return ( FeedValidator::isNull( $val ) )? FALSE : TRUE; break;
case 'link' : return ( FeedValidator::isValidURL( $val ) )? TRUE : FALSE; break;
case 'updated' :
case 'published' : return ( FeedValidator::isValidDate( $val ) )? TRUE : FeedValidator::isValidDate( self::getISODate( $val ) ); break;
}
}
/**
* Prints the xml and ESS namespace
*
* @access private
* @return String
*/
private function getHead()
{
$out = '<?xml version="1.0" encoding="'.self::CHARSET.'"?>' . self::LN;
$out .= '<!DOCTYPE ess PUBLIC "-//ESS//DTD" "http://essfeed.org/history/'.urlencode( self::ESS_VERSION ).'/index.dtd">' . self::LN;
$out .= $this->getComment();
$out .= '<ess xmlns="http://essfeed.org/history/'.urlencode( self::ESS_VERSION ).'/" version="'. urlencode( self::ESS_VERSION ) .'" lang="'. $this->lang .'">' . self::LN;
return $out;
}
private function getComment()
{
return '<!--' . self::LN .
$this->t(1) . 'ESS Feed (Event Standard Syndication)' . self::LN .
self::LN .
$this->t(1) . 'Your events are now available to any software that read ESS format, example:' . self::LN .
$this->t(2) . 'http://robby.ai '.$this->t(1).' (AI Calendar Assistant)' . self::LN .
$this->t(2) . 'http://wp-events-plugin.com '.$this->t(1).' (Wordpress Event Plugin)' . self::LN .
self::LN .
$this->t(1) . 'Standard info: '.$this->t(1).' http://essfeed.org/' . self::LN .
$this->t(1) . 'Other libraries: '.$this->t(1).' https://github.com/essfeed/' . self::LN .
self::LN .
'-->' . self::LN;
}
/**
* Closes the open tags at the end of file
*
* @access private
* @return String
*/
private function getEndChannel()
{
return $this->t(1) . "</channel>" . self::LN . "</ess>". self::LN;
}
/**
* Creates a single node as xml format
*
* @access private
* @param String name of the tag
* @param Mixed tag value as string or array of nested tags in 'tagName' => 'tagValue' format
* @param Array Attributes(if any) in 'attrName' => 'attrValue' format
* @return String formatted XML tag
*/
private function makeNode( $tagName, $tagContent, $attributes = NULL )
{
$CDATA = array( 'description' ); // Names of the tags to be displayed with <[CDATA[...]]>.
$nodeText = '';
$attrText = '';
if ( is_array( $attributes ) )
{
foreach ( $attributes as $key => $value )
{
if ( strlen( $value ) > 0 )
$attrText .= " $key=\"$value\" ";
}
}
$nodeText .= $this->t(2) . ( ( in_array( $tagName, $CDATA ) )? "<{$tagName}{$attrText}>" . self::LN . $this->t(3) . "<![CDATA[" . self::LN : "<{$tagName}{$attrText}>" );
if ( is_array( $tagContent ) ) // for tags
{
{
if ( isset( $value ) || $value == 0 )
{
$nodeText .= $this->t(4) . $this->makeNode( $key,
( ( self::REPLACE_ACCENT )?
FeedValidator::xml_entities(
FeedValidator::noAccent( $value, self::CHARSET ),
self::CHARSET
)
:
$value
)
);
}
}
}
else
{
if ( in_array( $tagName, $CDATA ) ||
$tagName == 'published' ||
$tagName == 'updated' ||
$tagName == 'value' ) { $nodeText .= self::utf8_for_xml( $tagContent ); }
else if ( $tagName == 'start' ) { $nodeText .= self::getISODate( $tagContent ); }
else if ( $tagName == 'link' ||
$tagName == 'uri' ) { $nodeText .= htmlspecialchars( $tagContent, ENT_QUOTES, self::CHARSET, FALSE ); }
else
{
$nodeText .= FeedValidator::xml_entities(
FeedValidator::noAccent( $tagContent, self::CHARSET ),
self::CHARSET
);
}
}
$nodeText .= ( ( in_array( $tagName, $CDATA ) )? self::LN . $this->t(3) . "]]>" . self::LN . $this->t(3) . "</$tagName>" : "</$tagName>" );
return $nodeText . self::LN;
}
/**
* convert unsuported UTF-8 chars within XML <[CDATA[...]]>.
*
* @access private
* @return String
*/
private static function utf8_for_xml($string)
{
if ( function_exists( 'mb_convert_encoding' ) )
{
$textORG = $string;
$string = mb_convert_encoding( $string, self::CHARSET, "auto" );
if ( strlen( $string ) <= 0 )
$string = $textORG;
}
return preg_replace ('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $string);
}
/**
* Get Channel XML content in String format
*
* @access private
* @return String
*/
private function getChannel()
{
$out = $this->t(1) .'<channel>' . self::LN;
foreach( $this->channel as $k => $v )
$out .= $this->makeNode( $k, $v );
return $out;
}
/**
* Get feed's items XML content in String format
*
* @access private
* @return String
*/
private function getItems()
{
$out = "";
foreach ( $this->items as $item )
{
$thisRoots = $item->getRoots();
$thisItems = $item->getElements();
$out .= $this->startFeed();
if ( is_array( $thisRoots ) )
{
if ( count( $thisRoots ) > 0 )
{
foreach ( $thisRoots as $elm => $val )
{
if ( $elm != 'tags' && is_string( $val ) )
{
if ( strlen( $elm ) > 0 && strlen( $val ) > 0 )
{
$out .= $this->t(1) .$this->makeNode( $elm, $val );
}
}
else
{
if ( is_array( $val ) )
{
if ( count( $val ) > 0 )
{
$out .= $this->t(3) . "<tags>" . self::LN;
foreach( $val as $tag )
$out .= $this->t(2) . $this->makeNode( 'tag', ( self::REPLACE_ACCENT )? FeedValidator::noAccent( $tag, self::CHARSET ) : $tag );
$out .= $this->t(3) . "</tags>" . self::LN;
}
}
}
}
}
}
if ( is_array( $thisItems ) )
{
if ( count( $thisItems ) > 0 )
{
foreach ( $thisItems as $key => $val )
{
if ( count( $thisItems[ $key ] ) > 0 && strlen( $key ) > 0 )
{
$out .= $this->t(3) . "<{$key}>" . self::LN;
foreach ( $val as $position => $feedItem )
{
$out .= $this->t(4) . "<item type='". strtolower( $feedItem[ 'type' ] ) ."'".
( ( isset( $feedItem[ 'unit' ] ) )? ( ( strlen( $feedItem[ 'unit' ] ) > 0 )? " unit='". strtolower( $feedItem[ 'unit' ] ) . "'" : '' ) : '' ) .
( ( isset( $feedItem[ 'mode' ] ) )? ( ( strlen( $feedItem[ 'mode' ] ) > 0 )? " mode='". strtolower( $feedItem[ 'mode' ] ) . "'" : '' ) : '' ) .
( ( isset( $feedItem[ 'selected_day' ] ) )? ( ( strlen( $feedItem[ 'selected_day' ] ) > 0 )? " selected_day='". strtolower( $feedItem[ 'selected_day' ] ) . "'" : '' ) : '' ) .
( ( isset( $feedItem[ 'selected_week' ] ) )? ( ( strlen( $feedItem[ 'selected_week' ] ) > 0 )? " selected_week='". strtolower( $feedItem[ 'selected_week' ] ) . "'" : '' ) : '' ) .
( ( isset( $feedItem[ 'interval' ] ) )? ( ( intval( $feedItem[ 'interval' ] ) > 1 )? " interval='". intval( $feedItem[ 'interval' ] ) . "'" : '' ) : '' ) .
( ( isset( $feedItem[ 'limit' ] ) )? ( ( intval( $feedItem[ 'limit' ] ) > 0 )? " limit='". intval( $feedItem[ 'limit' ] ) . "'" : '' ) : '' ) .
( ( isset( $feedItem[ 'moving_position' ] ) )? ( ( intval( $feedItem[ 'moving_position' ] ) > 0 )? " moving_position='". intval( $feedItem[ 'moving_position' ] ) . "'" : '' ) : '' ) .
( ( isset( $feedItem[ 'priority' ] ) )? ( ( intval( $feedItem[ 'priority' ] ) > 0 )? " priority='". intval( $feedItem[ 'priority' ] ) . "'" : " priority='".( $position + 1 ) . "'" ) : '' ) .
">" . self::LN;
if ( $key == 'prices' && ( $feedItem[ 'mode' ] == 'free' || $feedItem[ 'mode' ] == 'invitation' ) )
{
$out .= $this->t(3) . $this->makeNode( 'name', $feedItem['content']['name'] );
$out .= $this->t(3) . $this->makeNode( 'value', 0 );
}
else
{
foreach ( $feedItem['content'] as $elm => $feedElm )
{
$out .= $this->t(3) . $this->makeNode( $elm, $feedElm );
}
}
$out .= $this->t(4) . "</item>" . self::LN;
}
$out .= $this->t(3) . "</{$key}>" . self::LN;
}
}
}
}
$out .= $this->endFeed();
}
return $out;
}
/**
* Create the starting tag of feed
*
* @access private
* @return String
*/
private function startFeed()
{
return $this->t(2) . '<feed>' . self::LN;
}
/**
* Closes feed item tag
*
* @access private
* @return String
*/
private function endFeed()
{
return $this->t(2) . '</feed>' . self::LN;
}
/**
* Send email to server admin in case of specific problem.
*
* @access private
* @param String Receiver, or receivers of the mail.
* @param String Subject of the email to be sent.
* @param String Message to be sent in HTML format.
* @return Boolean bool TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
*/
private static function sendEmail( $email=NULL, $subject=NULL, $message=NULL )
{
if ( isset( $email ) || isset( $subject ) || isset( $message ) && function_exists( 'email' ) )
{
$headers = "From: " . strip_tags( $email ) . "\r\n";
$headers .= "Reply-To: ". strip_tags( $email ) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=" . self::CHARSET . "\r\n";
$msg = "<html><body>";
$msg .= $message;
$msg .= '</body></html>';
return mail( $email, $subject, $msg, $headers );
}
return FALSE;
}
public static function htmlvardump()
{
ob_start();
call_user_func_array( 'var_dump', func_get_args() );
return ob_get_clean();
}
/**
* Get a usable temp directory
*
* Adapted from Solar/Dir.php
* @author Paul M. Jones <[email protected]>
* @license http://opensource.org/licenses/bsd-license.php BSD
* @link http://solarphp.com/trac/core/browser/trunk/Solar/Dir.php
*
* @return string
*/
public static function tmp()
{
static $tmp = NULL;
if ( !$tmp )
{
$tmp = function_exists( 'sys_get_temp_dir' )? sys_get_temp_dir() : self::_tmp();
$tmp = rtrim( $tmp, DIRECTORY_SEPARATOR );
}
return $tmp;
}
/**
* Returns the OS-specific directory for temporary files
*
* @author Paul M. Jones <[email protected]>
* @license http://opensource.org/licenses/bsd-license.php BSD
* @link http://solarphp.com/trac/core/browser/trunk/Solar/Dir.php
*
* @return string
*/
protected static function _tmp()
{
// non-Windows system?
if ( strtolower( substr( PHP_OS, 0, 3 ) ) != 'win' )
{
$tmp = empty($_ENV['TMPDIR']) ? getenv( 'TMPDIR' ) : $_ENV['TMPDIR'];
return ($tmp)? $tmp : '/tmp';
}
// Windows 'TEMP'
$tmp = empty($_ENV['TEMP']) ? getenv('TEMP') : $_ENV['TEMP'];
if ($tmp) return $tmp;
// Windows 'TMP'
$tmp = empty($_ENV['TMP']) ? getenv('TMP') : $_ENV['TMP'];
if ($tmp) return $tmp;
// Windows 'windir'
$tmp = empty($_ENV['windir']) ? getenv('windir') : $_ENV['windir'];
if ($tmp) return $tmp;
// final fallback for Windows
return getenv('SystemRoot') . '\\temp';
}