-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdatasource.remote.php
More file actions
1118 lines (935 loc) · 41 KB
/
datasource.remote.php
File metadata and controls
1118 lines (935 loc) · 41 KB
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
require_once TOOLKIT . '/class.datasource.php';
require_once FACE . '/interface.datasource.php';
class RemoteDatasource extends DataSource implements iDatasource
{
private static $transformer = array();
private static $url_result = null;
private static $cacheable = null;
public static function getName()
{
return __('Remote Datasource');
}
public static function getClass()
{
return __CLASS__;
}
public function getSource()
{
return self::getClass();
}
public static function getTemplate()
{
return EXTENSIONS . '/remote_datasource/templates/blueprints.datasource.tpl';
}
public function settings()
{
$settings = array();
$settings[self::getClass()]['namespaces'] = $this->dsParamNAMESPACES;
$settings[self::getClass()]['paramoutput'] = $this->dsParamOUTPUTPARAM;
$settings[self::getClass()]['url'] = $this->dsParamURL;
$settings[self::getClass()]['xpath'] = isset($this->dsParamXPATH) ? $this->dsParamXPATH : '/';
$settings[self::getClass()]['cache'] = isset($this->dsParamCACHE) ? $this->dsParamCACHE : 30;
$settings[self::getClass()]['format'] = isset($this->dsParamFORMAT) ? $this->dsParamFORMAT : 'xml';
$settings[self::getClass()]['timeout'] = isset($this->dsParamTIMEOUT) ? $this->dsParamTIMEOUT : 6;
return $settings;
}
/**
* This methods allows custom remote data source to set other
* properties on the HTTP gateway, like Authentication or other
* parameters. This method is call just before the `exec` method.
*
* @param Gateway $gateway
* the Gateway object that will be use for the current HTTP request
* passed by reference
*/
public static function prepareGateway(&$gateway)
{
}
/**
* This methods allows custom remote data source to read the returned
* data before it becomes only available in the XML.
*
* @since Remote Datasource 2.0
* @param string $data
* the parsed xml string data returned by the Gateway by reference
* @param array $param_pool
* the pool of parameters
*/
public function exposeData(&$data, array &$param_pool = null)
{
}
/**
* This method is called when their is an http error
* or when content type is unsupported
*
* @since Remote Datasource 2.0
* @param array $info
* info of the http request
*/
public function httpError(&$info)
{
}
/*-------------------------------------------------------------------------
Utilities
-------------------------------------------------------------------------*/
/**
* Returns the source value for display in the Datasources index
*
* @param string $file
* The path to the Datasource file
* @return string
*/
public static function getSourceColumn($handle)
{
$datasource = DatasourceManager::create($handle, array(), false);
if (isset($datasource->dsParamURL)) {
return Widget::Anchor(str_replace('http://www.', '', $datasource->dsParamURL), $datasource->dsParamURL);
} else {
return 'Remote Datasource';
}
}
/**
* Given a `$url` and `$timeout`, this function will use the `Gateway`
* class to determine that it is a valid URL and returns successfully
* before the `$timeout`. If it does not, an error message will be
* returned, otherwise true.
*
* @todo This function is a bit messy, could be revisited.
* @param string $url
* @param integer $timeout
* If not provided, this will default to 6 seconds
* @param boolean $fetch_URL
* Defaults to false, but when set to true, this function will use the
* `Gateway` class to attempt to validate the URL's existence and it
* returns before the `$timeout`
* @param string $format
* The format that the URL will return, either JSON or XML. Defaults
* to 'xml' which will send the appropriate ACCEPTs header.
* @return string|array
* Returns an array with the 'data' if it is a valid URL, otherwise a string
* containing an error message.
*/
public static function isValidURL($url, $timeout = 6, $format = 'xml', $fetch_URL = false)
{
if (trim($url) == '') {
return __('This is a required field');
} elseif ($fetch_URL === true) {
list($data, $info) = self::fetch($url, $format, $timeout);
// 28 is CURLE_OPERATION_TIMEOUTED
$isTimeout = (isset($info['curl_errno']) && $info['curl_errno'] === 28) ||
(isset($info['curl_error']) && $info['curl_error'] === 28);
if ($isTimeout) {
return __('Request timed out. %d second limit reached.', array($timeout));
} elseif ($data === false || $info['http_code'] != 200) {
return __('Failed to load URL, status code %d was returned.', array($info['http_code']));
}
}
return array('data' => $data);
}
/**
* Builds the namespaces out to be saved in the Datasource file
*
* @param array $namespaces
* An associative array of where the key is the namespace prefix
* and the value is the namespace URL.
* @param string $template
* The template file, as defined by `getTemplate()`
* @return string
* The template injected with the Namespaces (if any).
*/
public static function injectNamespaces(array $namespaces, &$template)
{
if (empty($namespaces)) {
return;
}
$placeholder = '<!-- NAMESPACES -->';
$string = 'public $dsParamNAMESPACES = array(' . PHP_EOL;
foreach ($namespaces as $key => $val) {
if (trim($val) == '') {
continue;
}
$string .= str_repeat(' ', 8) . "'$key' => '" . addslashes($val) . "'," . PHP_EOL;
}
$string .= str_repeat(' ', 4) . ");" . PHP_EOL . str_repeat(' ', 4) . $placeholder;
$template = str_replace($placeholder, trim($string), $template);
}
/**
* Builds the output parameters out to be saved in the Datasource file
*
* @param array $paramoutput
* An associative array of where the key is the parameter name
* and the value is the xpath
* @param string $template
* The template file, as defined by `getTemplate()`
* @return string
* The template injected with the Namespaces (if any).
*/
public static function injectOutputParameters(array $paramoutput, &$template)
{
if (empty($paramoutput)) {
return;
}
$placeholder = '<!-- PARAMOUTPUT -->';
$string = 'public $dsParamOUTPUTPARAM = array(' . PHP_EOL;
foreach ($paramoutput as $key => $val) {
if (trim($val) == '') {
continue;
}
$string .= str_repeat(' ', 8) . "'$key' => '" . addslashes($val) . "'," . PHP_EOL;
}
$string .= str_repeat(' ', 4) . ");" . PHP_EOL . str_repeat(' ', 4) . $placeholder;
$template = str_replace($placeholder, trim($string), $template);
}
/**
* Given either the Datasource object or an array of settings for a
* Remote Datasource, this function will return it's cache ID, which
* is stored in tbl_cache.
*
* @since 1.1
* @param array|object $settings
*/
public static function buildCacheID($settings)
{
$cache_id = null;
if (is_object($settings)) {
$cache_id = md5(
$settings->dsParamURL .
serialize($settings->dsParamNAMESPACES) .
serialize($settings->dsParamPARAMOUTPUT) .
$settings->dsParamXPATH .
$settings->dsParamFORMAT
);
} elseif (is_array($settings)) {
// Namespaces come through empty, or as an array, so normalise
// to ensure the cache key stays the same.
if (is_array($settings['namespaces']) && empty($settings['namespaces'])) {
$settings['namespaces'] = null;
}
// Same deal with output parameters
if (is_array($settings['paramoutput']) && empty($settings['paramoutput'])) {
$settings['paramoutput'] = null;
}
$cache_id = md5(
$settings['url'] .
serialize($settings['namespaces']) .
serialize($settings['paramoutput']) .
stripslashes($settings['xpath']) .
$settings['format']
);
}
return $cache_id;
}
/**
* Helper function to build Cache information block
*
* @param XMLElement $wrapper
* @param Cacheable $cache
* @param string $cache_id
*/
public static function buildCacheInformation(XMLElement $wrapper, Cacheable $cache, $cache_id)
{
$cachedData = $cache->read($cache_id);
if (is_array($cachedData) && !empty($cachedData) && (time() < $cachedData['expiry'])) {
$a = Widget::Anchor(__('Clear now'), SYMPHONY_URL . getCurrentPage() . 'clear_cache/');
$wrapper->appendChild(
new XMLElement('p', __('Cache expires in %d minutes. %s', array(
($cachedData['expiry'] - time()) / 60,
$a->generate(false)
)), array('class' => 'help'))
);
} else {
$wrapper->appendChild(
new XMLElement('p', __('Cache has expired or does not exist.'), array('class' => 'help'))
);
}
}
/**
* Returns an instance of the cache for the Remote Datasource
*
* @since Remote Datasource 2.4.0
* @return Cacheable
*/
public static function getCache() {
return Symphony::ExtensionManager()->getCacheProvider('remotedatasource');
}
/*-------------------------------------------------------------------------
Editor
-------------------------------------------------------------------------*/
public static function buildEditor(XMLElement $wrapper, array &$errors = array(), array $settings = null, $handle = null)
{
if (!is_null($handle) && isset($settings[self::getClass()])) {
$cache = self::getCache();
$cache_id = self::buildCacheID($settings[self::getClass()]);
}
// If `clear_cache` is set, clear it..
if (isset($cache_id) && in_array('clear_cache', Administration::instance()->Page->getContext())) {
$cache->delete($cache_id, $handle);
Administration::instance()->Page->pageAlert(
__('Data source cache cleared at %s.', array(Widget::Time()->generate()))
. '<a href="' . SYMPHONY_URL . '/blueprints/datasources/" accesskey="a">'
. __('View all Data sources')
. '</a>',
Alert::SUCCESS
);
}
$fieldset = new XMLElement('fieldset');
$fieldset->setAttribute('class', 'settings contextual ' . __CLASS__);
$fieldset->setAttribute('data-context', Lang::createHandle(self::getName()));
$fieldset->appendChild(new XMLElement('legend', self::getName()));
$p = new XMLElement(
'p',
__('Use %s syntax to specify dynamic portions of the URL.', array(
'<code>{' . __('$param') . '}</code>'
))
);
$p->setAttribute('class', 'help');
$fieldset->appendChild($p);
// URL
$label = Widget::Label(__('URL'));
$url = isset($settings[self::getClass()]['url'])
? General::sanitize($settings[self::getClass()]['url'])
: null;
$label->appendChild(Widget::Input('fields[' . self::getClass() . '][url]', $url, 'text', array('placeholder' => 'http://')));
if (isset($errors[self::getClass()]['url'])) {
$fieldset->appendChild(Widget::Error($label, $errors[self::getClass()]['url']));
} else {
$fieldset->appendChild($label);
}
// Namespaces
static::addNamespaces($fieldset, $errors, $settings, $handle);
// Included Elements
$label = Widget::Label(__('Included Elements'));
$help = new XMLElement('i', __('xPath expression'));
$label->appendChild($help);
$xpath = isset($settings[self::getClass()]['xpath'])
? stripslashes($settings[self::getClass()]['xpath'])
: null;
$label->appendChild(
Widget::Input('fields[' . self::getClass() . '][xpath]', $xpath, 'text', array('placeholder' => '/'))
);
if (isset($errors[self::getClass()]['xpath'])) {
$fieldset->appendChild(Widget::Error($label, $errors[self::getClass()]['xpath']));
} else {
$fieldset->appendChild($label);
}
$group = new XMLElement('div', null, array('class' => 'three columns'));
$fieldset->appendChild($group);
// Caching
$label = Widget::Label(__('Cache expiration'));
$label->setAttribute('class', 'column');
$help = new XMLElement('i', __('in minutes'));
$label->appendChild($help);
$cache_time = isset($settings[self::getClass()]['cache'])
? max(0, intval($settings[self::getClass()]['cache']))
: 5;
$input = Widget::Input('fields[' . self::getClass() . '][cache]', (string) $cache_time);
$label->appendChild($input);
if (isset($errors[self::getClass()]['cache'])) {
$group->appendChild(Widget::Error($label, $errors[self::getClass()]['cache']));
} else {
$group->appendChild($label);
}
// Timeout
$label = Widget::Label(__('Timeout'));
$label->setAttribute('class', 'column');
$help = new XMLElement('i', __('in seconds'));
$label->appendChild($help);
$timeout_time = isset($settings[self::getClass()]['timeout'])
? max(1, intval($settings[self::getClass()]['timeout']))
: 6;
$label->appendChild(
Widget::Input('fields[' . self::getClass() . '][timeout]', (string) $timeout_time, 'text')
);
if (isset($errors[self::getClass()]['timeout'])) {
$group->appendChild(Widget::Error($label, $errors[self::getClass()]['timeout']));
} else {
$group->appendChild($label);
}
// Format
$label = Widget::Label(__('Format'));
$label->setAttribute('class', 'column');
$format = isset($settings[self::getClass()]['format'])
? $settings[self::getClass()]['format']
: null;
$label->appendChild(
Widget::Select('fields[' . self::getClass() . '][format]', array(
array('xml', $settings[self::getClass()]['format'] == 'xml', 'XML'),
array('json', $settings[self::getClass()]['format'] == 'json', 'JSON'),
array('csv', $settings[self::getClass()]['format'] == 'csv', 'CSV'),
array('txt', $settings[self::getClass()]['format'] == 'txt', 'TEXT')
), array(
'class' => 'picker'
))
);
if (isset($errors[self::getClass()]['format'])) {
$group->appendChild(Widget::Error($label, $errors[self::getClass()]['format']));
} else {
$group->appendChild($label);
}
// Check for existing Cache objects
if (isset($cache_id)) {
static::buildCacheInformation($fieldset, $cache, $cache_id);
}
$wrapper->appendChild($fieldset);
// Parameters
$fieldset = new XMLElement('fieldset');
$fieldset->setAttribute('class', 'settings contextual ' . __CLASS__);
$fieldset->setAttribute('data-context', Lang::createHandle(self::getName()));
$fieldset->appendChild(new XMLElement('legend', 'Parameters'));
$p = new XMLElement(
'p',
__('Appended to datasource handle, eg. %s', array(
'<code>$ds-handle.parameter-name</code>'
))
);
$p->setAttribute('class', 'help');
$fieldset->appendChild($p);
// Output Parameters
static::addOutputParameters($fieldset, $errors, $settings, $handle);
$wrapper->appendChild($fieldset);
}
/**
* Creates the markup for a Namespaces duplicator in the Remote Datasource editor.
*
* @param XMLElement $wrapper
* @param array $errors
* @param array $settings
* @param string $handle
*/
protected static function addNamespaces(XMLElement $wrapper, array &$errors = array(), array $settings = null, $handle = null) {
$div = new XMLElement('div', false, array(
'id' => 'xml',
'class' => 'pickable'
));
$p = new XMLElement('p', __('Namespace Declarations'));
$p->appendChild(new XMLElement('i', __('optional')));
$p->setAttribute('class', 'label');
$div->appendChild($p);
$frame = new XMLElement('div', null, array('class' => 'frame filters-duplicator'));
$frame->setAttribute('data-interactive', 'data-interactive');
$ol = new XMLElement('ol');
$ol->setAttribute('data-add', __('Add namespace'));
$ol->setAttribute('data-remove', __('Remove namespace'));
if (isset($settings[self::getClass()], $settings[self::getClass()]['namespaces']) && is_array($settings[self::getClass()]['namespaces']) && !empty($settings[self::getClass()]['namespaces'])) {
$ii = 0;
foreach ($settings[self::getClass()]['namespaces'] as $name => $uri) {
// Namespaces get saved to the file as $name => $uri, however in
// the $_POST they are represented as $index => array. This loop
// patches the difference.
if (is_array($uri)) {
$name = $uri['name'];
$uri = $uri['uri'];
}
$name = General::sanitize($name);
$uri = General::sanitize($uri);
$li = static::addNamespaceDuplicatorItem(false, $ii, $name, $uri);
$ol->appendChild($li);
$ii++;
}
}
// Add the template
$li = static::addNamespaceDuplicatorItem();
$ol->appendChild($li);
$frame->appendChild($ol);
$div->appendChild($frame);
$wrapper->appendChild($div);
}
/**
* Creates the duplicator item, both the template or the actual instance.
*
* @param boolean $template
* If this is the template, set to `true` (which it is by default)
* @param string|integer $key
* Used with instances, helps maintain the position of the duplicator item
* @param string $name
* The namespace name (by default null)
* @param string $uri
* The matching URI for this namespace
* @return XMLElement
*/
protected static function addNamespaceDuplicatorItem($template = true, $key = '-1', $name = null, $uri = null) {
$li = new XMLElement('li');
// Is this a template or the real deal?
if ($template) {
$li->setAttribute('class', 'template');
$li->setAttribute('data-type', 'namespace');
}
else {
$li->setAttribute('class', 'instance');
}
// Header
$header = new XMLElement('header');
$header->appendChild(
new XMLElement('h4', __('Namespace'))
);
$li->appendChild($header);
// Actual duplicator content (visible only when expanded)
$group = new XMLElement('div');
$group->setAttribute('class', 'two columns');
$label = Widget::Label(__('Name'));
$label->setAttribute('class', 'column');
$label->appendChild(Widget::Input('fields[' . self::getClass() . '][namespaces][' . $key . '][name]', $name));
$group->appendChild($label);
$label = Widget::Label(__('URI'));
$label->setAttribute('class', 'column');
$label->appendChild(Widget::Input('fields[' . self::getClass() . '][namespaces][' . $key . '][uri]', $uri));
$group->appendChild($label);
$li->appendChild($group);
return $li;
}
/**
* Creates the markup for the Output Parameters duplicator in the Remote Datasource editor.
*
* @param XMLElement $wrapper
* @param array $errors
* @param array $settings
* @param string $handle
*/
protected static function addOutputParameters(XMLElement $wrapper, array &$errors = array(), array $settings = null, $handle = null) {
$div = new XMLElement('div', false, array(
'id' => 'xml',
'class' => 'pickable'
));
$frame = new XMLElement('div', null, array('class' => 'frame filters-duplicator'));
$frame->setAttribute('data-interactive', 'data-interactive');
$ol = new XMLElement('ol');
$ol->setAttribute('data-add', __('Add parameter'));
$ol->setAttribute('data-remove', __('Remove parameter'));
if (isset($settings[self::getClass()], $settings[self::getClass()]['paramoutput']) && is_array($settings[self::getClass()]['paramoutput']) && !empty($settings[self::getClass()]['paramoutput'])) {
$ii = 0;
foreach ($settings[self::getClass()]['paramoutput'] as $param => $xpath) {
if (is_array($param)) {
$param = $xpath['param'];
$xpath = $xpath['xpath'];
}
$param = General::sanitize($param);
$xpath = General::sanitize($xpath);
$li = static::addOutputParameterDuplicatorItem(false, $ii, $param, $xpath);
$ol->appendChild($li);
$ii++;
}
}
// Add the template
$li = static::addOutputParameterDuplicatorItem();
$ol->appendChild($li);
$frame->appendChild($ol);
$div->appendChild($frame);
$wrapper->appendChild($div);
}
/**
* Creates the duplicator item, both the template or the actual instance.
*
* @param boolean $template
* If this is the template, set to `true` (which it is by default)
* @param string|integer $key
* Used with instances, helps maintain the position of the duplicator item
* @param string $param
* The namespace name (by default null)
* @param string $xpath
* The matching URI for this namespace
* @return XMLElement
*/
protected static function addOutputParameterDuplicatorItem($template = true, $key = '-1', $param = null, $xpath = null) {
$li = new XMLElement('li');
// Is this a template or the real deal?
if ($template) {
$li->setAttribute('class', 'template');
$li->setAttribute('data-type', 'paramoutput');
}
else {
$li->setAttribute('class', 'instance');
}
// Header
$header = new XMLElement('header');
$header->appendChild(
new XMLElement('h4', __('Parameter'))
);
$li->appendChild($header);
// Actual duplicator content (visible only when expanded)
$group = new XMLElement('div');
$group->setAttribute('class', 'two columns');
$label = Widget::Label(__('Parameter name'));
$label->setAttribute('class', 'column');
$label->appendChild(Widget::Input('fields[' . self::getClass() . '][paramoutput][' . $key . '][name]', $param));
$group->appendChild($label);
$label = Widget::Label(__('XPath'));
$label->setAttribute('class', 'column');
$label->appendChild(Widget::Input('fields[' . self::getClass() . '][paramoutput][' . $key . '][xpath]', $xpath));
$group->appendChild($label);
$li->appendChild($group);
return $li;
}
public static function validate(array &$settings, array &$errors)
{
// Use the TIMEOUT that was specified by the user for a real world indication
$timeout = isset($settings[self::getClass()]['timeout'])
? (int) $settings[self::getClass()]['timeout']
: 6;
// Check cache value is numeric
if (!is_numeric($settings[self::getClass()]['cache'])) {
$errors[self::getClass()]['cache'] = __('Must be a valid number');
}
// Make sure that XPath has been filled out
if (trim($settings[self::getClass()]['xpath']) == '') {
$errors[self::getClass()]['xpath'] = __('This is a required field');
}
// Ensure we have a URL
if (trim($settings[self::getClass()]['url']) == '') {
$errors[self::getClass()]['url'] = __('This is a required field');
} elseif (!preg_match('@{([^}]+)}@i', $settings[self::getClass()]['url'])) {
// If there is a parameter in the URL, we can't validate the existence of the URL
// as we don't have the environment details of where this datasource is going
// to be executed.
$valid_url = self::isValidURL($settings[self::getClass()]['url'], $timeout, $settings[self::getClass()]['format'], true);
// If url was valid, `isValidURL` will return an array of data
// Otherwise it'll return a string, which is an error
if (is_array($valid_url)) {
self::$url_result = $valid_url['data'];
} else {
$errors[self::getClass()]['url'] = $valid_url;
}
}
return empty($errors[self::getClass()]);
}
public static function prepare(array $settings, array $params, $template)
{
$settings = $settings[self::getClass()];
// Timeout
$timeout = isset($settings['timeout'])
? (int) $settings['timeout']
: 6;
// Automatically detect namespaces
if (!is_null(self::$url_result)) {
preg_match_all('/xmlns:([a-z][a-z-0-9\-]*)="([^\"]+)"/i', self::$url_result, $matches);
if (!is_array($settings['namespaces'])) {
$settings['namespaces'] = array();
}
if (isset($matches[2][0])) {
$detected_namespaces = array();
foreach ($settings['namespaces'] as $index => $namespace) {
$detected_namespaces[] = $namespace['name'];
$detected_namespaces[] = $namespace['uri'];
}
foreach ($matches[2] as $index => $uri) {
$name = $matches[1][$index];
if (in_array($name, $detected_namespaces) || in_array($uri, $detected_namespaces)) {
continue;
}
$detected_namespaces[] = $name;
$detected_namespaces[] = $uri;
$settings['namespaces'][] = array(
'name' => $name,
'uri' => $uri
);
}
}
}
// Namespace
$namespaces = array();
if (is_array($settings['namespaces'])) {
foreach ($settings['namespaces'] as $index => $data) {
$namespaces[$data['name']] = $data['uri'];
}
}
self::injectNamespaces($namespaces, $template);
// Output params
$outputparams = array();
if (is_array($settings['paramoutput'])) {
foreach ($settings['paramoutput'] as $index => $data) {
$outputparams[$data['name']] = $data['xpath'];
}
}
self::injectOutputParameters($outputparams, $template);
// If there is valid data, save it to cache so that it is available
// immediately to the frontend
if (!is_null(self::$url_result)) {
$settings = array_merge($settings, array(
'namespaces' => $namespaces,
'paramoutput' => $outputparams
));
$data = self::transformResult(self::$url_result, $settings['format']);
$cache_id = self::buildCacheID($settings);
self::getCache()->write($cache_id, $data, $settings['cache'], $params['rootelement']);
}
return sprintf(
$template,
$params['rootelement'], // rootelement
$settings['url'], // url
$settings['format'], // format
addslashes($settings['xpath']), // xpath
$settings['cache'], // cache
$timeout// timeout
);
}
/*-------------------------------------------------------------------------
Execution
-------------------------------------------------------------------------*/
public function execute(array &$param_pool = null)
{
$result = new XMLElement($this->dsParamROOTELEMENT);
// When DS is called out of the Frontend context, this will enable
// {$root} and {$workspace} parameters to be evaluated
if (empty($this->_env)) {
$this->_env['env']['pool'] = array(
'root' => URL,
'workspace' => URL . '/workspace'
);
}
try {
require_once(TOOLKIT . '/class.gateway.php');
require_once(TOOLKIT . '/class.xsltprocess.php');
require_once(CORE . '/class.cacheable.php');
$this->dsParamURL = $this->parseParamURL($this->dsParamURL);
if (isset($this->dsParamXPATH)) {
$this->dsParamXPATH = $this->__processParametersInString(stripslashes($this->dsParamXPATH), $this->_env);
}
// Builds a Default Stylesheet to transform the resulting XML with
$stylesheet = new XMLElement('xsl:stylesheet');
$stylesheet->setAttributeArray(array('version' => '1.0', 'xmlns:xsl' => 'http://www.w3.org/1999/XSL/Transform'));
$output = new XMLElement('xsl:output');
$output->setAttributeArray(array('method' => 'xml', 'version' => '1.0', 'encoding' => 'utf-8', 'indent' => 'yes', 'omit-xml-declaration' => 'yes'));
$stylesheet->appendChild($output);
$template = new XMLElement('xsl:template');
$template->setAttribute('match', '/');
$instruction = new XMLElement('xsl:copy-of');
// Namespaces
if (isset($this->dsParamNAMESPACES) && is_array($this->dsParamNAMESPACES)) {
foreach ($this->dsParamNAMESPACES as $name => $uri) {
$instruction->setAttribute('xmlns' . ($name ? ":{$name}" : null), $uri);
}
}
// XPath
$instruction->setAttribute('select', $this->dsParamXPATH);
$template->appendChild($instruction);
$stylesheet->appendChild($template);
$stylesheet->setIncludeHeader(true);
$xsl = $stylesheet->generate(true);
// Check for an existing Cache for this Datasource
$cache_id = self::buildCacheID($this);
$cachedData = self::getCache()->read($cache_id, $this->dsParamROOTELEMENT);
$writeToCache = null;
$isCacheValid = true;
$creation = DateTimeObj::get('c');
// Execute if the cache doesn't exist, or if it is old.
if (
(!is_array($cachedData) || empty($cachedData)) // There's no cache.
|| (time() - $cachedData['creation']) > ($this->dsParamCACHE * 60) // The cache is old.
) {
if (Mutex::acquire($cache_id, $this->dsParamTIMEOUT, TMP)) {
list($data, $info) = self::fetch($this->dsParamURL, $this->dsParamFORMAT, $this->dsParamTIMEOUT);
Mutex::release($cache_id, TMP);
$writeToCache = true;
// Handle any response that is not a 200, or the content type does not include XML, JSON, plain or text
if ((int) $info['http_code'] != 200 || !preg_match('/(xml|json|csv|plain|text)/i', $info['content_type'])) {
$writeToCache = false;
$result->setAttribute('valid', 'false');
$this->httpError($info);
// 28 is CURLE_OPERATION_TIMEOUTED
if ($info['curl_error'] == 28) {
$result->appendChild(
new XMLElement(
'error',
sprintf('Request timed out. %d second limit reached.', $this->dsParamTIMEOUT)
)
);
} else {
$result->appendChild(
new XMLElement(
'error',
sprintf('Status code %d was returned. Content-type: %s', $info['http_code'], $info['content_type'])
)
);
}
return $result;
// Handle where there is `$data`
} else if (strlen($data) > 0) {
try {
$data = self::transformResult($data, $this->dsParamFORMAT);
}
// If the `$data` is invalid, return a result explaining why
catch (TransformException $ex) {
$writeToCache = false;
$error = new XMLElement('errors');
$error->setAttribute('valid', 'false');
$error->appendChild(new XMLElement('error', __('Data returned is invalid.')));
foreach ($ex->getErrors() as $e) {
if (strlen(trim($e['message'])) == 0) {
continue;
}
$error->appendChild(new XMLElement('item', General::sanitize($e['message'])));
}
$result->appendChild($error);
return $result;
}
// If `$data` is empty, set the `force_empty_result` to true.
} elseif (strlen($data) == 0) {
$this->_force_empty_result = true;
}
// Failed to acquire a lock
} else {
$result->appendChild(
new XMLElement('error', __('The %s class failed to acquire a lock.', array('<code>Mutex</code>')))
);
}
// The cache is good, use it!
} else {
$data = trim($cachedData['data']);
$creation = DateTimeObj::get('c', $cachedData['creation']);
}
// Visit the data
$this->exposeData($data, $param_pool);
// If `$writeToCache` is set to false, invalidate the old cache if it existed.
if (is_array($cachedData) && !empty($cachedData) && $writeToCache === false) {
$data = trim($cachedData['data']);
$isCacheValid = false;
$creation = DateTimeObj::get('c', $cachedData['creation']);
if (empty($data)) {
$this->_force_empty_result = true;
}
}
// If `force_empty_result` is false and `$result` is an instance of
// XMLElement, build the `$result`.
if (!$this->_force_empty_result && is_object($result)) {
$proc = new XsltProcess;
$ret = $proc->process($data, $xsl);
if ($proc->isErrors()) {
$result->setAttribute('valid', 'false');
$error = new XMLElement('error', __('Transformed XML is invalid.'));
$result->appendChild($error);
$errors = new XMLElement('errors');
foreach ($proc->getError() as $e) {
if (strlen(trim($e['message'])) == 0) {
continue;
}
$errors->appendChild(new XMLElement('item', General::sanitize($e['message'])));
}
$result->appendChild($errors);
$result->appendChild(
new XMLElement('raw-data', General::wrapInCDATA($data))
);
} elseif (strlen(trim($ret)) == 0) {
$this->_force_empty_result = true;
} else {
if ($this->dsParamCACHE > 0 && $writeToCache) {
self::getCache()->write($cache_id, $data, $this->dsParamCACHE, $this->dsParamROOTELEMENT);
}
if (!empty($this->dsParamOUTPUTPARAM)) {
$this->processOutputParameters($data, $param_pool);
}
$result->setValue(PHP_EOL . str_repeat("\t", 2) . preg_replace('/([\r\n]+)/', "$1\t", $ret));
$result->setAttribute('status', ($isCacheValid === true ? 'fresh' : 'stale'));
$result->setAttribute('cache-id', $cache_id);
$result->setAttribute('cache-namespace', $this->dsParamROOTELEMENT);
$result->setAttribute('creation', $creation);
}
}
} catch (FrontendPageNotFoundException $e) {
// Work around. This ensures the 404 page is displayed and
// is not picked up by the default catch() statement below
throw $e;
} catch (Exception $e) {
$result->appendChild(new XMLElement('error', $e->getMessage()));
}
if ($this->_force_empty_result) {
$result = $this->emptyXMLSet();