-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathtxplib_misc.php
More file actions
6749 lines (5653 loc) · 184 KB
/
Copy pathtxplib_misc.php
File metadata and controls
6749 lines (5653 loc) · 184 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
/*
* Textpattern Content Management System
* https://textpattern.com/
*
* Copyright (C) 2026 The Textpattern Development Team
*
* This file is part of Textpattern.
*
* Textpattern is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, version 2.
*
* Textpattern is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Textpattern. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Collection of miscellaneous tools.
*
* @package Misc
*/
/**
* Strips NULL bytes.
*
* @param string|array $in The input value
* @return mixed
*/
function deNull($in)
{
return is_array($in) ? doArray($in, 'deNull') : strtr((string)$in, array("\0" => ''));
}
/**
* Strips carriage returns and linefeeds.
*
* @param string|array $in The input value
* @return mixed
*/
function deCRLF($in)
{
return is_array($in) ? doArray($in, 'deCRLF') : strtr((string)$in, array(
"\n" => '',
"\r" => '',
));
}
/**
* Applies a callback to a given string or an array.
*
* @param string|array $in An array or a string to run through the callback function
* @param callback $function The callback function
* @return mixed
* @example
* echo doArray(array('value1', 'value2'), 'intval');
*/
function doArray($in, $function)
{
if (is_array($in)) {
return array_map($function, $in);
}
if (is_array($function)) {
return call_user_func($function, $in);
}
return $function($in);
}
/**
* Un-quotes a quoted string or an array of values.
*
* @param string|array $in The input value
* @return mixed
*/
function doStrip($in)
{
return is_array($in) ? doArray($in, 'doStrip') : doArray($in, 'stripslashes');
}
/**
* Strips HTML and PHP tags from a string or an array.
*
* @param string|array $in The input value
* @return mixed
* @example
* echo doStripTags('<p>Hello world!</p>');
*/
function doStripTags($in)
{
return is_array($in) ? doArray($in, 'doStripTags') : doArray($in, 'strip_tags');
}
/**
* Converts entity escaped brackets back to characters.
*
* @param string|array $in The input value
* @return mixed
*/
function doDeEnt($in)
{
return doArray($in, 'deEntBrackets');
}
/**
* Converts entity escaped brackets back to characters.
*
* @param string $in The input value
* @return string
*/
function deEntBrackets($in)
{
$array = array(
'<' => '<',
'<' => '<',
'<' => '<',
'>' => '>',
'>' => '>',
'>' => '>',
);
foreach ($array as $k => $v) {
$in = preg_replace("/".preg_quote($k)."/i", $v, $in);
}
return $in;
}
/**
* Escapes special characters for use in an SQL statement.
*
* Always use this function when dealing with user-defined values in SQL
* statements. If this function is not used to escape user-defined data in a
* statement, the query is vulnerable to SQL injection attacks.
*
* @param string|array $in The input value
* @return mixed An array of escaped values or a string depending on $in
* @package DB
* @example
* echo safe_field('column', 'table', "color = '" . doSlash(gps('color')) . "'");
*/
function doSlash($in)
{
return doArray($in, 'safe_escape');
}
/**
* Escape SQL LIKE pattern's wildcards for use in an SQL statement.
*
* @param string|array $in The input value
* @return mixed An array of escaped values or a string depending on $in
* @since 4.6.0
* @package DB
* @example
* echo safe_field('column', 'table', "color LIKE '" . doLike(gps('color')) . "'");
*/
function doLike($in)
{
return doArray($in, 'safe_escape_like');
}
/**
* A shell for htmlspecialchars() with $flags defaulting to ENT_QUOTES.
*
* @param string $string The string being converted
* @param int $flags A bitmask of one or more flags. The default is ENT_QUOTES
* @param string $encoding Defines encoding used in conversion. The default is UTF-8
* @param bool $double_encode When double_encode is turned off PHP will not encode existing HTML entities, the default is to convert everything
* @return string
* @see https://www.php.net/manual/en/function.htmlspecialchars.php
* @since 4.5.0
* @package Filter
*/
function txpspecialchars($string, $flags = ENT_QUOTES, $encoding = 'UTF-8', $double_encode = true)
{
// Ignore ENT_HTML5 and ENT_XHTML for now.
// ENT_HTML5 and ENT_XHTML are defined in PHP 5.4+ but we consistently encode single quotes as ' in any doctype.
// global $prefs;
// static $h5 = null;
//
// if (defined(ENT_HTML5)) {
// if ($h5 === null) {
// $h5 = ($prefs['doctype'] == 'html5' && txpinterface == 'public');
// }
//
// if ($h5) {
// $flags = ($flags | ENT_HTML5) & ~ENT_HTML401;
// }
// }
//
return htmlspecialchars((string)$string, $flags, $encoding, $double_encode);
}
/**
* Converts special characters to HTML entities.
*
* @param array|string $in The input value
* @return mixed The array or string with HTML syntax characters escaped
* @package Filter
*/
function doSpecial($in)
{
return doArray($in, 'txpspecialchars');
}
/**
* Converts the given value to NULL.
*
* @param mixed $a The input value
* @return null
* @package Filter
* @access private
*/
function _null($a)
{
return null;
}
/**
* Converts an array of values to NULL.
*
* @param array $in The array
* @return array
* @package Filter
*/
function array_null($in)
{
return array_map('_null', $in);
}
/**
* Escapes a page title. Converts <, >, ', " characters to HTML entities.
*
* @param string $title The input string
* @return string The string escaped
* @package Filter
*/
function escape_title($title)
{
return strtr((string)$title, array(
'<' => '<',
'>' => '>',
"'" => ''',
'"' => '"',
));
}
/**
* Sanitises a string for use in a JavaScript string.
*
* Escapes \, \n, \r, " and ' characters. It removes 'PARAGRAPH SEPARATOR'
* (U+2029) and 'LINE SEPARATOR' (U+2028). When you need to pass a string
* from PHP to JavaScript, use this function to sanitise the value to avoid
* XSS attempts.
*
* @param string $js JavaScript input
* @return string Escaped JavaScript
* @since 4.4.0
* @package Filter
*/
function escape_js($js)
{
$js = isset($js) ? preg_replace('/[\x{2028}\x{2029}]/u', '', $js) : '';
return addcslashes($js, "\\\'\"\n\r");
}
/**
* Escapes CDATA section for an XML document.
*
* @param string $str The string
* @return string XML representation wrapped in CDATA tags
* @package XML
*/
function escape_cdata($str)
{
return '<![CDATA['.str_replace(']]>', ']]]><![CDATA[]>', $str).']]>';
}
/**
* Returns a localisation string.
*
* @param string $var String name
* @param array $atts Replacement pairs
* @param string $escape Convert special characters to HTML entities. Either "html" or ""
* @return string A localisation string
* @package L10n
*/
function gTxt($var, $atts = array(), $escape = 'html')
{
global $event, $plugin, $txp_current_plugin;
static $txpLang = null;
if ($txpLang === null) {
$txpLang = Txp::get('\Textpattern\L10n\Lang');
$lang = txpinterface == 'admin' ? get_pref('language_ui', gps('lang', LANG)) : LANG;
$loaded = $txpLang->load($lang, true);
$evt = isset($event) ? trim($event) : '';
if (empty($loaded) || !in_array($evt, $loaded)) {
load_lang($lang, $evt);
}
}
// Hackish
if (isset($txp_current_plugin) && isset($plugin['textpack'])) {
$txpLang->loadTextpack($plugin['textpack']);
unset($plugin['textpack']);
}
return $txpLang->txt($var, $atts, $escape);
}
/**
* Returns given timestamp in a format of 01 Jan 2001 15:19:16.
*
* @param int $timestamp The UNIX timestamp
* @return string A formatted date
* @access private
* @see safe_stftime()
* @package DateTime
* @example
* echo gTime();
*/
function gTime($timestamp = 0)
{
return safe_strftime('%d %b %Y %X', $timestamp);
}
/**
* Creates a dumpfile from a backtrace and outputs given parameters.
*
* @package Debug
*/
function dmp()
{
static $f = false;
if (defined('txpdmpfile')) {
global $prefs;
if (!$f) {
$f = fopen($prefs['tempdir'].'/'.txpdmpfile, 'a');
}
$stack = get_caller();
fwrite($f, "\n[".$stack[0].t.safe_strftime('iso8601')."]\n");
}
$a = func_get_args();
if (!$f) {
echo "<pre dir=\"auto\">".n;
}
foreach ($a as $thing) {
$out = is_scalar($thing) ? strval($thing) : var_export($thing, true);
if ($f) {
fwrite($f, $out.n);
} else {
echo txpspecialchars($out).n;
}
}
if (!$f) {
echo "</pre>".n;
}
}
/**
* Gets the given language's strings from the database.
*
* Fetches the given language from the database and returns the strings
* as an array.
*
* If no $events is specified, only appropriate strings for the current context
* are returned. If 'txpinterface' constant equals 'admin' all strings are
* returned. Otherwise, only strings from events 'common' and 'public'.
*
* If $events is FALSE, returns all strings.
*
* @param string $lang The language code
* @param array|string|bool $events An array of loaded events
* @return array
* @package L10n
* @example
* print_r(
* load_lang('en-gb', false)
* );
*/
function load_lang($lang, $events = null)
{
global $production_status, $event, $textarray;
isset($textarray) or $textarray = array();
$textarray = array_merge($textarray, Txp::get('\Textpattern\L10n\Lang')->load($lang, $events));
if (($production_status !== 'live' || $event === 'diag')
&& $debug = parse_ini_file(txpath.DS.'mode.ini')
) {
$textarray += (array)$debug;
Txp::get('\Textpattern\L10n\Lang')->setPack($textarray);
}
return $textarray;
}
/**
* Gets a list of user groups.
*
* @param bool $keys Whether to fetch the array as keys (true) or gTxt() strings (false)
* @return array
* @package User
* @example
* print_r(
* get_groups()
* );
*/
function get_groups($keys = false)
{
global $txp_groups;
callback_event_ref('user', 'groups', 0, $txp_groups);
return $keys ? $txp_groups : doArray($txp_groups, 'gTxt');
}
/**
* Checks if a user has privileges to the given resource.
*
* @param string $res The resource
* @param mixed $user The user. If no user name is supplied, assume the current logged in user
* @return bool
* @package User
* @example
* add_privs('my_privilege_resource', '1,2,3');
* if (has_privs('my_privilege_resource', 'username'))
* {
* echo "'username' has privileges to 'my_privilege_resource'.";
* }
*/
function has_privs($res = null, $user = '')
{
global $txp_user, $txp_permissions;
static $privs;
if (is_array($user)) {
$level = isset($user['privs']) ? $user['privs'] : null;
$user = isset($user['name']) ? $user['name'] : '';
}
$user = (string) $user;
if ($user === '') {
$user = (string) $txp_user;
}
if ($user !== '') {
if (!isset($privs[$user])) {
$privs[$user] = isset($level) ?
$level :
safe_field("privs", 'txp_users', "name = '".doSlash($user)."'");
}
if (!isset($res)) {
return $privs[$user];
} elseif (isset($txp_permissions[$res]) && $privs[$user] && $txp_permissions[$res]) {
return in_list($privs[$user], $txp_permissions[$res]);
}
}
return false;
}
/**
* Adds dynamic privileges.
*
* @param array $pluggable The array, see global $txp_options
* @since 4.7.2
* @package User
*/
function plug_privs($pluggable = null, $user = null)
{
global $txp_options;
isset($pluggable) or $pluggable = $txp_options;
$level = isset($user['privs']) ? $user['privs'] : has_privs();
foreach ((array)$pluggable as $pref => $pane) {
if (is_array($pane)) {
if (isset($pane[0])) {
if (!in_list($level, $pane[0])) {
break;
}
unset($pane[0]);
}
} else {
$pane = array('prefs.'.$pref => $pane);
}
if (get_pref($pref)) {
array_walk($pane, function (&$item) use ($level) {
if ($item === true) {
$item = $level;
}
});
add_privs($pane);
} else {
add_privs(array_fill_keys(array_keys($pane), null));
}
}
}
/**
* Grants privileges to user-groups.
*
* Will not let you override existing privs.
*
* @param mixed $res The resource
* @param string $perm List of user-groups, e.g. '1,2,3'
* @package User
* @example
* add_privs('my_admin_side_panel_event', '1,2,3,4,5');
*/
function add_privs($res, $perm = '1')
{
global $txp_permissions;
if (!is_array($res)) {
$res = array($res => $perm);
}
foreach ($res as $priv => $group) {
if ($group === null) {
$txp_permissions[$priv] = null;
} else {
$group .= (empty($txp_permissions[$priv]) ? '' : ','.$txp_permissions[$priv]);
$group = join(',', do_list_unique($group));
$txp_permissions[$priv] = $group;
}
}
}
/**
* Require privileges from a user to the given resource.
*
* Terminates the script if user doesn't have required privileges.
*
* @param string|null $res The resource, or NULL
* @param string $user The user. If no user name is supplied, assume the current logged in user
* @package User
* @example
* require_privs('article.edit');
*/
function require_privs($res = null, $user = '')
{
if ($res === null || !has_privs($res, $user)) {
pagetop(gTxt('restricted_area'));
echo graf(gTxt('restricted_area'), array('class' => 'restricted-area'));
end_page();
exit;
}
}
/**
* Gets a list of users having access to a resource.
*
* @param string $res The resource, e.g. 'article.edit.published'
* @return array A list of usernames
* @since 4.5.0
* @package User
*/
function the_privileged($res, $real = false)
{
global $txp_permissions;
$out = array();
if (isset($txp_permissions[$res])) {
foreach (safe_rows("name, RealName", 'txp_users', "FIND_IN_SET(privs, '".$txp_permissions[$res]."') ORDER BY ".($real ? "RealName" : "name")." ASC") as $user) {
extract($user);
$out[$name] = $real ? $RealName : $name;
}
}
return $out;
}
/**
* Check whether a user can modify the article.
*
* Probably more suitable for Validator class?
*
* @param array $rs The article data
* @param string $user The user name
* @return bool
* @since 4.9.0
* @package User
*/
function can_modify($rs, $user = null) {
global $txp_user;
isset($user) or $user = $txp_user;
$published = $rs['Status'] >= STATUS_LIVE ? '.published' : '';
return has_privs('article.edit'.$published, $user) ||
$rs['AuthorID'] === $user && has_privs('article.edit.own'.$published, $user);
}
/**
* Check whether a user can preview the article.
*
* @param array $rs The article data
* @param string $user The user name
* @return bool
* @since 4.9.1
* @package User
*/
function can_preview($rs, $user = null) {
return has_privs('article.preview', $user) &&
(empty($rs['ID']) || can_modify($rs, $user));
}
/**
* Convert SVG size to pixel size
*
* @param char SVG size
* @return int size in px
* @package Image
*/
function txpsvgtopx($svgsize)
{
if (empty($svgsize)) {
return($svgsize);
}
preg_match('/([0-9\.]*)([A-Za-z]*)/', $svgsize, $matches);
switch (substr($matches[2], 0, 2)) {
case '':
return($matches[1]);
break;
case 'pt':
return($matches[1] * 96 / 72);
break;
case 'pc':
return($matches[1] * 16);
break;
case 'mm':
return($matches[1] * 96 / 25.4);
break;
case 'cm':
return($matches[1] * 96 / 2.54);
break;
case 'in':
return($matches[1] * 96);
break;
default:
return($matches[1]);
break;
}
}
/**
* Lists image types that can be safely uploaded.
*
* Returns different results based on the logged in user's privileges.
*
* @param int $type If set, validates the given value
* @return mixed
* @package Image
* @since 4.6.0
* @example
* list($width, $height, $extension) = getimagesize('image');
* if ($type = get_safe_image_types($extension))
* {
* echo "Valid image of {$type}.";
* }
*/
function get_safe_image_types($type = null)
{
$extensions = array(IMAGETYPE_GIF => '.gif', 0 => '.jpeg', IMAGETYPE_JPEG => '.jpg', IMAGETYPE_PNG => '.png') +
(has_privs('image.create.trusted') ? array(IMAGETYPE_SVG => '.svg') : array()) +
(defined('IMAGETYPE_WEBP') ? array(IMAGETYPE_WEBP => '.webp') : array()) +
(defined('IMAGETYPE_AVIF') ? array(IMAGETYPE_AVIF => '.avif') : array());
callback_event_ref('txp.image', 'types', 0, $extensions);
if (isset($type)) {
return !empty($extensions[$type]) ? $extensions[$type] : false;
}
return $extensions;
}
/**
* Gets the dimensions of an image for a HTML <img> tag.
*
* @param string $name The filename
* @return string|bool height="100" width="40", or FALSE on failure
* @package Image
* @example
* if ($size = sizeImage('/path/to/image.png'))
* {
* echo "<img src='image.png' {$size} />";
* }
*/
function sizeImage($name)
{
$size = getimagesize($name);
return is_array($size) ? $size[3] : false;
}
/**
* Gets an image as an array.
*
* @param int $id image ID
* @param string $name image name
* @return array|bool An image data array, or FALSE on failure
* @package Image
* @example
* if ($image = imageFetchInfo($id))
* {
* print_r($image);
* }
*/
function imageFetchInfo($id = "", $name = "")
{
global $thisimage, $p;
static $cache = array(), $fields = null;
if ($id) {
if (isset($cache['i'][$id])) {
return $cache['i'][$id];
} elseif (preg_match('/^\d+$/', trim($id))) {
$where = 'id = '.intval($id).' LIMIT 1';
} else {
if (!isset($fields)) {
$fields = array_column(safe_show('columns', 'txp_image'), 'Default', 'Field');
$fields['date'] = date('c');
}
$pathinfo = pathinfo($id);
$fields['name'] = isset($pathinfo['filename']) ? $pathinfo['filename'] : '';
$fields['ext'] = isset($pathinfo['extension']) ? '.'.$pathinfo['extension'] : '';
$fields['id'] = $id;
return $fields;
}
} elseif ($name) {
if (isset($cache['n'][$name])) {
return $cache['n'][$name];
} else {
$where = "name = '".doSlash($name)."' LIMIT 1";
}
} elseif ($thisimage) {
$id = (int) $thisimage['id'];
return $cache['i'][$id] = $thisimage;
} elseif ($p) {
if (isset($cache['i'][$p])) {
return $cache['i'][$p];
} else {
$where = 'id = '.intval($p).' LIMIT 1';
}
} else {
assert_image();
return false;
}
$rs = safe_row("*", 'txp_image', $where);
if ($rs) {
$id = (int) $rs['id'];
return $cache['i'][$id] = image_format_info($rs);
} else {
trigger_error(gTxt('unknown_image'));
}
return false;
}
/**
* Private implementation of PHP image_type_to_mime_type().
*
* Takes an image data array generated by imageFetchInfo() and formats the contents.
*
* @param integer $image_type IMAGETYPE_XXX
* @return string
* @access private
* @package Image
*/
function txp_image_type_to_mime_type($image_type)
{
if ($image_type == IMAGETYPE_SVG) {
return 'image/svg+xml';
}
return image_type_to_mime_type($image_type);
}
/**
* Formats image info.
*
* Takes an image data array generated by imageFetchInfo() and formats the contents.
*
* @param array $image The image
* @return array
* @see imageFetchInfo()
* @access private
* @package Image
*/
function image_format_info($image)
{
static $mimetypes;
if (($unix_ts = strtotime($image['date'])) > 0) {
$image['date'] = $unix_ts;
}
if (!isset($mimetypes)) {
$mimetypes = get_safe_image_types();
}
$image['mime'] = ($mime = array_search($image['ext'], $mimetypes)) !== false ? txp_image_type_to_mime_type($mime) : '';
return $image;
}
/**
* Formats link info.
*
* @param array $link The link to format
* @return array Formatted link data
* @access private
* @package Link
*/
function link_format_info($link)
{
if (($unix_ts = strtotime($link['date'])) > 0) {
$link['date'] = $unix_ts;
}
return $link;
}
/**
* Gets a HTTP GET or POST parameter.
*
* Internally strips CRLF from GET parameters and removes NULL bytes.
*
* @param string $thing The parameter to get
* @return string|array The value of $thing, or an empty string
* @package Network
* @example
* if (gps('sky') == 'blue' && gps('roses') == 'red')
* {
* echo 'Roses are red, sky is blue.';
* }
*/
function gps($thing, $default = '')
{
global $pretext;
if (isset($_GET[$thing])) {
$out = $_GET[$thing];
$out = doArray($out, 'deCRLF');
} elseif (isset($_POST[$thing])) {
$out = $_POST[$thing];
} elseif (is_numeric($thing) && isset($pretext[abs($thing)])) {
$thing >= 0 or $thing += $pretext[0] + 1;
$out = $pretext[$thing];
} else {
return $default;
}
$out = doArray($out, 'deNull');
return $out;
}
/**
* Gets an array of HTTP GET or POST parameters.
*
* @param array $array The parameters to extract
* @return array
* @package Network
* @example
* extract(gpsa(array('sky', 'roses'));
* if ($sky == 'blue' && $roses == 'red')
* {
* echo 'Roses are red, sky is blue.';
* }
*/
function gpsa($array)
{
if (is_array($array)) {
$out = array();
foreach ($array as $a) {
$out[$a] = gps($a);
}
return $out;
}
return false;
}
/**
* Gets a HTTP POST parameter.
*
* Internally removes NULL bytes.
*
* @param string $thing The parameter to get
* @return string|array The value of $thing, or an empty string
* @package Network
* @example
* if (ps('sky') == 'blue' && ps('roses') == 'red')
* {
* echo 'Roses are red, sky is blue.';
* }
*/
function ps($thing, $default = '')
{
return isset($_POST[$thing]) ? doArray($_POST[$thing], 'deNull') : $default;
}
/**
* Gets an array of HTTP POST parameters.
*
* @param array $array The parameters to extract
* @return array
* @package Network
* @example
* extract(psa(array('sky', 'roses'));
* if ($sky == 'blue' && $roses == 'red')
* {
* echo 'Roses are red, sky is blue.';
* }
*/
function psa($array)
{
foreach ($array as $a) {
$out[$a] = ps($a);
}
return $out;
}
/**