/
functions_smarty.inc.php
1279 lines (1089 loc) · 52.7 KB
/
functions_smarty.inc.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
# Copyright (c) 2003-2005, Jannis Hermanns (on behalf the Serendipity Developer Team)
# All rights reserved. See LICENSE file for licensing details
if (IN_serendipity !== true) {
die ("Don't hack!");
}
/**
* Fetch a list of trackbacks for an entry
*
* @access public
* @param int The ID of the entry
* @param string How many trackbacks to show
* @param boolean If true, also non-approved trackbacks will be shown
* @return
*/
function &serendipity_fetchTrackbacks($id, $limit = null, $showAll = false) {
global $serendipity;
if (!$showAll) {
$and = "AND status = 'approved'";
}
$query = "SELECT * FROM {$serendipity['dbPrefix']}comments WHERE entry_id = '". (int)$id ."' AND (type = 'TRACKBACK' OR type = 'PINGBACK') $and ORDER BY id";
if (isset($limit)) {
$limit = serendipity_db_limit_sql($limit);
$query .= " $limit";
}
$comments = serendipity_db_query($query);
if (!is_array($comments)) {
$temp = array();
return $temp;
}
return $comments;
}
/**
* Show trackbacks for an entry
*
* LONG
*
* @access public
* @param array The superarray of trackbacks to display
* @return
*/
function &serendipity_printTrackbacks($trackbacks) {
global $serendipity;
$serendipity['smarty']->assignByRef('trackbacks', $trackbacks);
return serendipity_smarty_fetch('TRACKBACKS', 'trackbacks.tpl');
}
/**
* Formats a HTML5 timestamp; our serendipity_formatTime handler uses strftime() which does not have this shortcut
*
* @access public
* @param int The unix timestamp to format
* @return timestamp in ISO-format
*/
function serendipity_smarty_html5time($timestamp) {
return date("c", $timestamp);
}
/**
* Smarty: Fetch a smarty block and pass it on to Serendipity Templates - use with Smarty > 3.0 only
*
* @access public
* @param string The name of the block to parse data into ("COMMENTS" - virtual variable like {$COMMENTS})
* @param string The name of the template file to fetch. Only filename, the path is auto-detected
* @param boolean If true, the output of the smarty parser will be echoed instead of invisibly treated
* @param boolean If true, always use a tpl file from the frontend fallback chain (used for the preview)
* @return string The parsed HTML code
*/
function &serendipity_smarty_fetch($block, $file, $echo = false, $force_frontend = false) {
global $serendipity;
$output = $serendipity['smarty']->fetch('file:'. serendipity_getTemplateFile($file, 'serendipityPath', $force_frontend), null, null, null, ($echo === true && ($serendipity['smarty_raw_mode'] ?? null)));
$serendipity['smarty']->assignByRef($block, $output);
return $output;
}
/**
* Smarty Modifier: Check if a string is not empty and prepend a prefix in that case. Else, leave empty
*
* @access public
* @param string The input string to check
* @param string The prefix to prepend, if $string is not empty
* @return string The return string
*/
function serendipity_emptyPrefix($string, $prefix = ': ') {
return (!empty($string) ? $prefix . serendipity_specialchars($string) : '');
}
/**
* Smarty Modifier: Return a remembered variable
*
* @access public
* @param string The variable name
* @param string The wanted value
* @param boolean Force default?
* @param string Returned attribute
* @return string The return string
*/
function serendipity_ifRemember($name, $value, $isDefault = false, $att = 'checked') {
global $serendipity;
if (!is_array($serendipity['COOKIE']) && !$isDefault) {
return false;
}
if ((!is_array($serendipity['COOKIE']) && $isDefault) ||
(!isset($serendipity['COOKIE']['serendipity_' . $name]) && $isDefault) ||
(isset($serendipity['COOKIE']['serendipity_' . $name]) && $serendipity['COOKIE']['serendipity_' . $name] == $value)) {
return " $att=\"$att\" ";
}
}
/**
* Smarty Function: Fetch and print a single or multiple entries
*
* @access public
* @param array Smarty parameter input array:
* [FETCHING]
* category: (int) The category ID (separate multiple with ";") to fetch entries from
* viewAuthor: (int) The author ID (separate multiple with ";") to fetch entries from
* page: (int) The number of the page for paginating entries
* id: (int) The ID of an entry. If given, only a single entry will be fetched. If left empty, multiple entries are fetched.
* range: (mixed) Restricts fetching entries to a specific timespan. Behaves differently depending on the type:
* Numeric:
* YYYYMMDD - Shows all entries from YYYY-MM-DD.
* If DD is "00", it will show all entries from that month.
* If DD is any other number, it will show entries of that specific day.
* 2-Dimensional Array:
* Key #0 - Specifies the start timestamp (unix seconds)
* Key #1 - Specifies the end timestamp (unix seconds)
* Other (null, 3-dimensional Array, ...):
* Entries newer than $modified_since will be fetched
* full (boolean) Indicates if the full entry will be fetched (body+extended: TRUE), or only the body (FALSE).
* limit (string) Holds a "Y" or "X, Y" string that tells which entries to fetch. X is the first entry offset, Y is number of entries. If not set, the global fetchLimit will be applied (15 entries by default)
* fetchDrafts (boolean) Indicates whether drafts should be fetched (TRUE) or not
* modified_since (int) Holds a unix timestamp to be used in conjunction with $range, to fetch all entries newer than this timestamp
* orderby (string) Holds the SQL "ORDER BY" statement.
* filter_sql (string) Can contain any SQL code to inject into the central SQL statement for fetching the entry
* noCache (boolean) If set to TRUE, all entries will be fetched from scratch and any caching is ignored
* noSticky (boolean) If set to TRUE, all sticky entries will NOT be fetched.
* select_key (string) Can contain a SQL statement on which keys to select. Plugins can also set this, pay attention!
* group_by (string) Can contain a SQL statement on how to group the query. Plugins can also set this, pay attention!
* returncode (string) If set to "array", the array of entries will be returned. "flat-array" will only return the articles without their entryproperties. "single" will only return a 1-dimensional array. "query" will only return the used SQL.
* joinauthors (bool) Should an SQL-join be made to the AUTHORS DB table?
* joincategories (bool) Should an SQL-join be made to the CATEGORIES DB table?
* joinown (string) SQL-Parts to add to the "JOIN" query
* entryprops (string) Condition list of commaseparated entryproperties that an entry must have to be displayed (example: "ep_CustomField='customVal',ep_CustomField2='customVal2'")
*
* [PRINTING]
* template: (string) Name of the template file to print entries with
* preview: (boolean) Indicates if this is a preview
* block (string The name of the SMARTY block that this gets parsed into
* use_hooks (boolean Indicates whether to apply footer/header event hooks
* use_footer (boolean Indicates whether the pagination footer should be displayed
* groupmode (string Indicates whether the input $entries array is already grouped in preparation for the smarty $entries output array [TRUE], or if it shall be grouped by date [FALSE]
* skip_smarty_hooks (boolean) If TRUE, no plugins will be executed at all
* skip_smarty_hook (mixed) Can be set to an array of plugin hooks to NOT execute
* prevent_reset (boolean) If set to TRUE, the smarty $entries array will NOT be cleared. (to prevent possible duplicate output of entries)
* @param object Smarty object
* @return string The Smarty HTML response
*/
function serendipity_smarty_fetchPrintEntries($params, $smarty) {
global $serendipity;
static $entrycount = 0;
static $restore_var_GET_keys = array('category', 'viewAuthor', 'page', 'hide_category');
// A counter variable to not assign template files multiple times
$entrycount++;
// Default values for function calls
if (empty($params['template'])) {
$params['template'] = 'entries.tpl';
}
if (empty($params['range'])) {
$params['range'] = null;
}
if (empty($params['full'])) {
$params['full'] = true;
}
if (empty($params['fetchDrafts'])) {
$params['fetchDrafts'] = false;
}
if (!empty($params['entryprops'])) {
if (preg_match_all('@(.*)(!)?=[\'"]*([^\'"]+)[\'"]*(,|$)@imsU', $params['entryprops'], $m)) {
foreach($m[0] AS $idx => $p) {
$params['joinown'] .= "\n JOIN {$serendipity['dbPrefix']}entryproperties
AS ep" . $idx . "
ON (ep" . $idx . ".entryid = e.id AND
ep" . $idx . ".property = '" . serendipity_db_escape_string($m[1][$idx]) . "' AND
ep" . $idx . ".value " . $m[2][$idx] . "= '" . serendipity_db_escape_string($m[3][$idx]) . "') \n";
}
}
}
if (empty($params['modified_since'])) {
$params['modified_since'] = false;
}
if (empty($params['orderby'])) {
$params['orderby'] = 'timestamp DESC';
}
if (empty($params['noCache'])) {
$params['noCache'] = false;
}
if (empty($params['noSticky'])) {
$params['noSticky'] = false;
}
if (empty($params['preview'])) {
$params['preview'] = false;
}
if (empty($params['block'])) {
$params['block'] = 'smarty_entries_' . $entrycount;
}
if (empty($params['use_hooks'])) {
$params['use_hooks'] = false;
}
if (empty($params['use_footer'])) {
$params['use_footer'] = false;
}
if (empty($params['groupmode'])) {
$params['groupmode'] = 'date';
}
if (empty($params['skip_smarty_hooks'])) {
$params['skip_smarty_hooks'] = true;
}
if (empty($params['skip_smarty_hook'])) {
$params['skip_smarty_hook'] = array();
}
if (empty($params['prevent_reset'])) {
$params['prevent_reset'] = false;
}
if (empty($params['select_key'])) {
$params['select_key'] = null;
}
if (empty($params['group_by'])) {
$params['group_by'] = null;
}
if (empty($params['returncode'])) {
$params['returncode'] = 'array';
}
if (empty($params['joinauthors'])) {
$params['joinauthors'] = true;
}
if (empty($params['joincategories'])) {
$params['joincategories'] = true;
}
// Some functions deal with the $serendipity array. To modify them, we need to store
// their original contents.
$old_var = array();
if (!empty($params['short_archives'])) {
$old_var['short_archives'] = $serendipity['short_archives'];
$serendipity['short_archives'] = $params['short_archives'];
}
$old_var['skip_smarty_hooks'] = $serendipity['skip_smarty_hooks'] ?? null;
$serendipity['skip_smarty_hooks'] = $params['skip_smarty_hooks'];
$old_var['skip_smarty_hook'] = $serendipity['skip_smarty_hook'] ?? null;
$serendipity['skip_smarty_hook'] = $params['skip_smarty_hook'];
foreach($restore_var_GET_keys AS $key) {
if (!empty($params[$key])) {
$old_var['GET'][$key] = $serendipity['GET'][$key] ?? null;
$serendipity['GET'][$key] = $params[$key];
}
}
$param_keys = array(
'full',
'limit',
'fetchDrafts',
'modified_since',
'orderby',
'filter_sql',
'noCache',
'noSticky',
'select_key',
'group_by',
'returncode',
'joinauthors',
'joincategories',
'joinown'
);
foreach ($param_keys as $param_key) {
if (!isset($params[$param_key])) {
$params[$param_key] = null;
}
}
if (!empty($params['id'])) {
$entry = serendipity_fetchEntry(
'id',
(int)$params['id'],
$params['full'],
$params['fetchDrafts']);
} else {
$entry = serendipity_fetchEntries(
$params['range'],
$params['full'],
$params['limit'],
$params['fetchDrafts'],
$params['modified_since'],
$params['orderby'],
$params['filter_sql'],
$params['noCache'],
$params['noSticky'],
$params['select_key'],
$params['group_by'],
$params['returncode'],
$params['joinauthors'],
$params['joincategories'],
$params['joinown']
);
// Check whether the returned entries shall be grouped specifically
switch ($params['groupmode']) {
case 'date':
// No regrouping required, printEntries() does it for us.
break;
case 'category':
// Regroup by primary category
$groupdata = array();
foreach($entry AS $k => $_entry) {
if (is_array($entry['categories'])) {
$groupkey = $entry['categories'][0];
} else {
$groupkey = 'none';
}
$groupdata[$groupkey]['entries'] =& $_entry;
}
$entry =& $groupdata;
break;
}
}
if ($params['returncode'] == 'query') {
return print_r($entry, true);
}
serendipity_printEntries(
$entry, // Entry data
(!empty($params['id']) ? true : false), // Extended data?
$params['preview'], // Entry preview?
'ENTRIES',
false, // Prevent Smarty parsing
$params['use_hooks'],
$params['use_footer'],
($params['groupmode'] == 'date' ? false : true) // Grouping of $entry
);
// Restore the $serendipity array after our modifications.
if (isset($old_var['short_archives'])) {
$serendipity['short_archives'] = $old_var['short_archives'];
}
if (is_array($old_var['GET'] ?? null)) {
foreach($old_var['GET'] AS $key => $val) {
$serendipity['GET'][$key] = $val;
}
}
$out = serendipity_smarty_fetch($params['block'], $params['template']);
// Reset array list, because we might be in a nested code call.
if ($params['prevent_reset'] == false) {
$serendipity['smarty']->assign('entries', array());
}
$serendipity['skip_smarty_hook'] = $old_var['skip_smarty_hook'];
$serendipity['skip_smarty_hooks'] = $old_var['skip_smarty_hooks'];
return $out;
}
/**
* Smarty Function: Shows a commentform
*
* @access public
* @param array Smarty parameter input array:
* id: An entryid to show the commentform for
* url: an optional HTML target link for the form
* comments: Optional array of containing comments
* data: possible pre-submitted values to the input values
* showToolbar: Toggle whether to show extended options of the comment form
* moderate_comments: Toggle whether comments to this entry are allowed
* @param object Smarty object
* @return void
*/
function serendipity_smarty_showCommentForm($params, $smarty) {
global $serendipity;
if (!isset($params['id']) || !isset($params['entry'])) {
trigger_error("Smarty Error: " . __FUNCTION__ .": missing 'id' or 'entry' parameter", E_USER_WARNING);
return;
}
if (empty($params['url'])) {
$params['url'] = $serendipity['serendipityHTTPPath'] . $serendipity['indexFile'] . '?url=' . $params['entry']['commURL'];
}
if (!isset($params['comments'])) {
$params['comments'] = NULL;
}
if (!isset($params['data'])) {
$params['data'] = $serendipity['POST'];
}
if (!isset($params['showToolbar'])) {
$params['showToolbar'] = true;
}
if (!isset($params['moderate_comments'])) {
$params['moderate_comments'] = serendipity_db_bool($params['entry']['moderate_comments']);
}
$comment_add_data = array(
'comments_messagestack' => (isset($serendipity['messagestack']['comments']) ? (array)$serendipity['messagestack']['comments'] : array()),
'is_comment_added' => (isset($serendipity['GET']['csuccess']) && $serendipity['GET']['csuccess'] == 'true' ? true: false),
'is_comment_moderate' => (isset($serendipity['GET']['csuccess']) && $serendipity['GET']['csuccess'] == 'moderate' ? true: false)
);
$smarty->assign($comment_add_data);
serendipity_displayCommentForm(
$params['id'],
$params['url'],
$params['comments'],
$params['data'],
$params['showToolbar'],
$params['moderate_comments'],
$params['entry']
);
return true;
}
/**
* Smarty Function: Be able to include the output of a sidebar plugin within a smarty template
*
* @access public
* @param array Smarty parameter input array:
* class: The classname of the plugin to show
* id: An ID of a plugin to show
* side: The side of a plugin to show (left|right|hidden)
* negate: Revert previous filters
* @param object Smarty object
* @return string The Smarty HTML response
*/
function serendipity_smarty_showPlugin($params, $smarty) {
global $serendipity;
if (!isset($params['class']) && !isset($params['id'])) {
trigger_error("Smarty Error: " . __FUNCTION__ .": missing 'class' or 'id' parameter", E_USER_WARNING);
return;
}
if (!isset($params['side'])) {
$params['side'] = '*';
}
if (!isset($params['negate'])) {
$params['negate'] = null;
}
if (empty($params['template'])) {
$params['template'] = 'sidebar.tpl';
}
$out = serendipity_plugin_api::generate_plugins($params['side'], $params['negate'], $params['class'], $params['id'], $params['template']);
if (empty($out) && !empty($params['empty'])) {
return $params['empty'];
}
return $out;
}
/**
* Smarty Function: Get total count for specific objects
*
* @access public
* @param array Smarty parameter input array:
* what: The type of count to show: "entries", "trackbacks", "comments"
* @param object Smarty object
* @return string The Smarty HTML response
*/
function serendipity_smarty_getTotalCount($params, $smarty) {
if (!isset($params['what'])) {
trigger_error("Smarty Error: " . __FUNCTION__ .": missing 'what' parameter", E_USER_WARNING);
return;
}
return serendipity_getTotalCount($params['what']);
}
/**
* Smarty Function: Be able to execute the hook of an event plugin and return its output
*
* Listens to specific serendipity global variables:
* $serendipity['skip_smarty_hooks'] - If TRUE, no plugins will be executed at all
* $serendipity['skip_smarty_hook'] - Can be set to an array of plugin hooks to NOT execute
*
* @access public
* @param array Smarty parameter input array:
* hook: The name of the event hook
* hookAll: boolean whether unknown hooks shall be executed
* data: The $eventData to an event plugin
* addData: the $addData to an event plugin
* @param object Smarty object
* @return null
*/
function serendipity_smarty_hookPlugin($params, $smarty) {
global $serendipity;
static $hookable = array('frontend_header',
'entries_header',
'entries_footer',
'frontend_comment',
'frontend_footer');
if (!isset($params['hook'])) {
trigger_error("Smarty Error: " . __FUNCTION__ .": missing 'hook' parameter", E_USER_WARNING);
return;
}
if (!in_array($params['hook'], $hookable) && $params['hookAll'] != 'true') {
trigger_error("Smarty Error: " . __FUNCTION__ .": illegal hook '". $params['hook'] ."' (" . $params['hookAll'] . ")", E_USER_WARNING);
return;
}
// Smarty hooks can be bypassed via an internal variable (temporarily)
if (isset($serendipity['skip_smarty_hooks']) && $serendipity['skip_smarty_hooks']) {
return;
}
// A specific hook can also be bypassed by creating an associative array like this:
// $serendipity['skip_smarty_hook'] = array('entries_header');
// That would only skip the entries_header event hook, but allow all others.
// Of course it cannot be used in conjunction with the all-blocking skip_smarty_hooks.
if (isset($serendipity['skip_smarty_hook']) && is_array($serendipity['skip_smarty_hook']) && isset($serendipity['skip_smarty_hook'][$params['hook']])) {
return;
}
if (!isset($params['data'])) {
if (isset($params['eventData'])) {
$params['data'] = &$params['eventData'];
} else {
$params['data'] = &$serendipity;
}
}
if (!isset($params['addData'])) {
$params['addData'] = null;
}
serendipity_plugin_api::hook_event($params['hook'], $params['data'], $params['addData']);
}
/**
* Smarty Modifier: Be able to execute the hook of an event plugin and return its output, uses a REFERENCED variable.
*
* Listens to specific serendipity global variables:
* $serendipity['skip_smarty_hooks'] - If TRUE, no plugins will be executed at all
* $serendipity['skip_smarty_hook'] - Can be set to an array of plugin hooks to NOT execute
*
* @access public
* @param mixed EventData (referenced)
* @param string Event hook name
* @param mixed Additional data
* @return null
*/
function serendipity_smarty_refhookPlugin($eventData, $hook, $addData = null) {
global $serendipity;
if (!isset($hook)) {
trigger_error("Smarty Error: " . __FUNCTION__ .": missing 'hook' parameter", E_USER_WARNING);
return;
}
// Smarty hooks can be bypassed via an internal variable (temporarily)
if (isset($serendipity['skip_smarty_hooks']) && $serendipity['skip_smarty_hooks']) {
return;
}
// A specific hook can also be bypassed by creating an associative array like this:
// $serendipity['skip_smarty_hook'] = array('entries_header');
// That would only skip the entries_header event hook, but allow all others.
// Of course it cannot be used in conjunction with the all-blocking skip_smarty_hooks.
if (isset($serendipity['skip_smarty_hook']) && is_array($serendipity['skip_smarty_hook']) && isset($serendipity['skip_smarty_hook'][$hook])) {
return;
}
serendipity_plugin_api::hook_event($hook, $eventData, $addData);
}
/**
* Smarty Function: Prints a list of sidebar plugins
*
* @access public
* @param array Smarty parameter input array:
* side: The plugin side to show (left|right|hidden)
* @param object Smarty object
* @return string The HTML code of a plugin's output
*/
function serendipity_smarty_printSidebar($params, $smarty) {
if ( !isset($params['side']) ) {
trigger_error("Smarty Error: " . __FUNCTION__ .": missing 'side' parameter", E_USER_WARNING);
return;
}
if (isset($params['template'])) {
return serendipity_plugin_api::generate_plugins($params['side'], false, null, null, $params['template']);
} else {
return serendipity_plugin_api::generate_plugins($params['side']);
}
}
/**
* Smarty Function: Get the full path to a template file
*
* @access public
* @param array Smarty parameter input array:
* file: The filename you want to include (any file within your template directory or the 'default' template if not found)
* @param object Smarty object
* @return string The requested filename with full path
*/
function serendipity_smarty_getFile($params, $smarty) {
if ( !isset($params['file']) ) {
trigger_error("Smarty Error: " . __FUNCTION__ .": missing 'file' parameter", E_USER_WARNING);
return;
}
return serendipity_getTemplateFile($params['file'], 'serendipityHTTPPath', $params['frontend'] ?? false);
}
function serendipity_smarty_getConfigVar($params, $smarty) {
if ( !isset($params['key']) ) {
trigger_error("Smarty Error: " . __FUNCTION__ .": missing 'key' parameter", E_USER_WARNING);
return;
}
return serendipity_get_config_var($params['key']);
}
function serendipity_smarty_setFormToken($params, $smarty) {
if ( isset($params['type']) ) {
return serendipity_setFormToken($params['type']);
}
return serendipity_setFormToken();
}
/**
* Smarty Function: Picks a specified key from an array and returns it
*
* @access public
* @param array Smarty parameter input array:
* array: The array you want to check
* key: The keyname
* default: What (string) to return when array does not contain the key.
* @param object Smarty object
* @return string The requested filename with full path
*/
function serendipity_smarty_pickKey($params, $smarty) {
if ( !isset($params['array']) ) {
trigger_error("Smarty Error: " . __FUNCTION__ .": missing 'array' parameter", E_USER_WARNING);
return;
}
if ( !isset($params['key']) ) {
trigger_error("Smarty Error: " . __FUNCTION__ .": missing 'key' parameter", E_USER_WARNING);
return;
}
return serendipity_pickKey($params['array'], $params['key'], $params['default']);
}
/**
* Smarty Function: Get a permalink for an entry
*
* @access public
* @param array Smarty parameter input array:
* entry: $entry data to pull title etc. out of
* is_comments: Whether a permalink for a comment feed should be embedded
* @param object Smarty object
* @return string The permalink
*/
function serendipity_smarty_rss_getguid($params, $smarty) {
if ( !isset($params['entry']) ) {
trigger_error("Smarty Error: " . __FUNCTION__ .": missing 'entry' parameter", E_USER_WARNING);
return;
}
if ( !isset($params['is_comments']) ) {
trigger_error("Smarty Error: " . __FUNCTION__ .": missing 'is_comments' parameter", E_USER_WARNING);
return;
}
return serendipity_rss_getguid($params['entry'], $params['is_comments']);
}
/**
* Smarty Modifier: Format a timestamp
*
* @access public
* @param int The timestamp to format (unix seconds)
* @param string The strftime() format options on how to format this string
* @param boolean Shall timezone conversions be applied?
* @param boolean Try to detect a valid timestamp?
* @param boolean Use strftime or date?
* @return
*/
function serendipity_smarty_formatTime($timestamp, $format, $useOffset = true, $detectTimestamp = false, $useDate = false) {
if ($detectTimestamp !== false && stristr($detectTimestamp, 'date') === false) {
return $timestamp;
}
if (defined($format)) {
return serendipity_formatTime(constant($format), $timestamp, $useOffset, $useDate);
} else {
return serendipity_formatTime($format, $timestamp, $useOffset, $useDate);
}
}
/**
* Smarty Function: Show comments to an entry
*
* @access public
* @param array Smarty parameter input array:
* entry: The $entry data
* mode: The default viewmode (threaded/linear)
* @param object Smarty object
* @return string The HTML code with the comments
*/
function &serendipity_smarty_printComments($params, $smarty) {
global $serendipity;
if (!isset($params['entry'])) {
trigger_error("Smarty Error: " . __FUNCTION__ .": missing 'entry' parameter", E_USER_WARNING);
return;
}
if (!isset($params['mode'])) {
$params['mode'] = VIEWMODE_THREADED;
}
if (isset($params['order']) && $params['order'] != 'DESC') {
$params['order'] = 'ASC';
}
$comments = serendipity_fetchComments($params['entry'], (int)($params['limit'] ?? null), 'co.id ' . ($params['order'] ?? null));
if (!empty($serendipity['POST']['preview'])) {
$comments[] =
array(
'email' => $serendipity['POST']['email'],
'author' => $serendipity['POST']['name'],
'body' => $serendipity['POST']['comment'],
'url' => $serendipity['POST']['url'],
'parent_id' => $serendipity['POST']['replyTo'],
'timestamp' => time()
);
}
if (empty($params['depth'])) {
$params['depth'] = 0;
}
if (empty($params['trace'])) {
$params['trace'] = null;
}
if (empty($params['block'])) {
$params['block'] = 'COMMENTS';
}
if (empty($params['template'])) {
$params['template'] = 'comments.tpl';
}
$out = serendipity_printComments($comments, $params['mode'], $params['depth'], $params['trace'], $params['block'], $params['template']);
return $out;
}
/**
* Smarty Function: Show Trackbacks
*
* @access public
* @param array Smarty parameter input array:
* entry: The $entry data for the trackbacks
* @param object Smarty object
* @return
*/
function &serendipity_smarty_printTrackbacks($params, $smarty) {
if ( !isset($params['entry']) ) {
trigger_error("Smarty Error: " . __FUNCTION__ .": missing 'entry' parameter", E_USER_WARNING);
return;
}
$trackbacks =& serendipity_fetchTrackbacks($params['entry']);
if (empty($params['depth'])) {
$params['depth'] = 0;
}
if (empty($params['trace'])) {
$params['trace'] = null;
}
if (empty($params['block'])) {
$params['block'] = 'TRACKBACKS';
}
if (empty($params['template'])) {
$params['template'] = 'trackbacks.tpl';
}
$out = serendipity_printComments($trackbacks, VIEWMODE_LINEAR, $params['depth'], $params['trace'], $params['block'], $params['template']);
return $out;
}
/**
* Get the Serendipity dimensions for an image
*
* @access public
* @param array Smarty parameter input array:
* file: The image file to get image data for
* assign: The variable to assign the image data array to
* @param object Smarty object
* @return string Empty
*/
function serendipity_smarty_getImageSize($params, $smarty) {
global $serendipity;
if (!isset($params['file'])) {
trigger_error("Smarty Error: " . __FUNCTION__ .": missing 'file' parameter", E_USER_WARNING);
return;
}
if (!isset($params['assign'])) {
trigger_error("Smarty Error: " . __FUNCTION__ .": missing 'assign' parameter", E_USER_WARNING);
return;
}
// Is it a correct filesystem absolute path?
$file = $params['file'];
// Most likely the user specified an HTTP path
if (!file_exists($file)) {
$file = $_SERVER['DOCUMENT_ROOT'] . $file;
}
// Maybe wants a template file (returns filesystem path)
if (!file_exists($file)) {
$file = serendipity_getTemplateFile($params['file']);
}
// If no file, trigger an error
if (!file_exists($file)) {
trigger_error("Smarty Error: " . __FUNCTION__ .': file ' . $params['file'] . NOT_FOUND . ' ', E_USER_WARNING);
return;
}
$smarty->assign($params['assign'], serendipity_getimagesize($file));
}
/**
* Smarty Prefilter: Replace constants to direkt $smarty.const. access
*
* @access public
* @param string Template source content
* @param object Smarty object
* @return
*/
function &serendipity_replaceSmartyVars($tpl_source, $smarty) {
$tpl_source = str_replace('$CONST.', '$smarty.const.', $tpl_source);
return $tpl_source;
}
function serendipity_smarty_sprintf($format, ...$values) {
return sprintf($format, ...$values);
}
function serendipity_smarty_print_r($value, $return = false) {
return print_r($value, $return);
}
function serendipity_smarty_array_key_exists($key, $array) {
return array_key_exists($key, $array);
}
function serendipity_smarty_count($value, $mode = COUNT_NORMAL) {
return count($value, $mode);
}
/**
* Initialize the Smarty framework for use in Serendipity
*
* @access public
* @return null
*/
function serendipity_smarty_init($vars = array()) {
global $serendipity, $template_config, $template_global_config, $template_config_groups;
if (!isset($serendipity['smarty'])) {
$template_dir = $serendipity['serendipityPath'] . $serendipity['templatePath'] . ($serendipity['template'] ?? null);
if (!defined('IN_serendipity_admin') && file_exists($template_dir . '/template.inc.php')) {
// If this file exists, a custom template engine will be loaded.
// Beware: Smarty is used in the Admin backend, despite of this.
include_once $template_dir . '/template.inc.php';
} else {
// Backend template overwritten here (NOT earlier due to frontend specific check)
if (defined('IN_serendipity_admin')) {
$template_dir = $serendipity['serendipityPath'] . $serendipity['templatePath'] . $serendipity['template_backend'];
}
// Set a session variable if Smarty fails:
$prev_smarty = $_SESSION['no_smarty'] ?? null;
$_SESSION['no_smarty'] = true;
if (LANG_CHARSET != 'UTF-8') {
@define('SMARTY_MBSTRING', false);
@define('SMARTY_RESOURCE_CHAR_SET', LANG_CHARSET);
}
// define cache resources to load with smarty - see smarty cache readme - needs enabled cache!
#@define('APC_EXTENSION_LOADED', extension_loaded('apc') && ini_get('apc.enabled'));
#@define('MEMCACHE_EXTENSION_LOADED', (class_exists('Memcached',false) || class_exists('Memcache',false)) && (extension_loaded("memcached") || extension_loaded("memcache")));
// Default Smarty Engine will be used
if (!defined('SMARTY_DIR')) {
@define('SMARTY_DIR', S9Y_PEAR_PATH . 'smarty/smarty/libs/');
}
if (!class_exists('Smarty')) {
include_once SMARTY_DIR . 'Smarty.class.php';
}
if (!class_exists('Smarty')) {
return false;
}
// include the serendipity smarty constructor
if (!class_exists('Serendipity_Smarty')) {
include_once S9Y_INCLUDE_PATH . '/include/serendipity_smarty_class.inc.php';
}
if (!class_exists('Serendipity_Smarty')) {
return false;
}
// set smarty instance
#$serendipity['smarty'] = new Serendipity_Smarty;
// initialize smarty object by instance
$serendipity['smarty'] = Serendipity_Smarty::getInstance();
// debug moved to class
// Hooray for Smarty:
$_SESSION['no_smarty'] = $prev_smarty;
// enable security policy by instance of the Smarty_Security class
$serendipity['smarty']->enableSecurity('Serendipity_Smarty_Security_Policy');
// debugging...
#echo '<pre>';print_r($serendipity['smarty']);echo '</pre>';#exit;
#$serendipity['smarty']->testInstall();exit;
// extreme debugging with undocumented internal flag which enables a trace output from the parser during debugging
#$serendipity['smarty']->_parserdebug = true; // be careful!
/**
* ToDo: Check for possible API changes in Smarty 3.2 [smarty_modifier_foobar, --> [smarty_modifier_foobar, smarty_function_foobar, smarty_block_foobar] (in class)]
* smarty_modifier_foobar(Smarty $smarty, $string, ...) vs. smarty_modifier_foobar($string, ...)
**/
$serendipity['smarty']->registerPlugin('modifier', 'makeFilename', 'serendipity_makeFilename');