forked from blazoncek/n3o_phpcms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_functions.php
executable file
·828 lines (735 loc) · 32.2 KB
/
_functions.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
<?php
/*~ _functions.php
.---------------------------------------------------------------------------.
| Software: N3O CMS (frontend and backend) |
| Version: 2.2.2 |
| Contact: contact author (also http://blaz.at/home) |
| ------------------------------------------------------------------------- |
| Author: Blaž Kristan ([email protected]) |
| Copyright (c) 2007-2014, Blaž Kristan. All Rights Reserved. |
| ------------------------------------------------------------------------- |
| License: Distributed under the Lesser General Public License (LGPL) |
| http://www.gnu.org/copyleft/lesser.html |
| ------------------------------------------------------------------------- |
| This file is part of N3O CMS (frontend and backend). |
| |
| N3O CMS is free software: you can redistribute it and/or |
| modify it under the terms of the GNU Lesser General Public License as |
| published by the Free Software Foundation, either version 3 of the |
| License, or (at your option) any later version. |
| |
| N3O CMS 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 Lesser General Public License for more details. |
'---------------------------------------------------------------------------'
*/
/**************************************
* fileExists - case (in)sensitive file_exists variant
*--------------------------------------
* @parameter string - filename with full path
* @parameter boolean - case sensitivity toggle
* @returns string/boolean - filename if found/false otherwise
**************************************/
function fileExists($fileName, $caseSensitive=false)
{
if ( left($fileName,5)=='data:' || left($fileName,4)=='http' || left($fileName,3)=='ftp' ) {
return false;
}
// handle UTF8 in filename on Windows systems
$fileName = ((strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? DecodeUTF8($fileName) : $fileName);
if ( file_exists($fileName) ) {
return basename($fileName);
}
if ( $caseSensitive ) return false;
// Handle case insensitive requests
$directoryName = dirname($fileName);
$fileArray = glob($directoryName . '/*', GLOB_NOSORT);
$fileNameLowerCase = strtolower($fileName);
if ( $fileArray ) foreach ($fileArray as $file) {
if (strtolower($file) == $fileNameLowerCase) {
return $file;
}
}
return false;
}
/**************************************
* retinaImgExists - check if retina image exists
*--------------------------------------
* @parameter string - filename with full path
* @parameter boolean - case sensitivity toggle
* @returns string/boolean - filename if found/false otherwise
**************************************/
function retinaImgExists($file, $caseSensitive=false)
{
$ext = strrchr($file, '.');
$name = left($file, strlen($file)-4);
if ( right($name, 3) === '@2x' ) return false;
return fileExists($name.'@2x'.$ext, $caseSensitive);
}
/**************************************
* AddLightboxLink - find <IMG> tag in string and surround it with <A HREF=... REL="fancybox">
*--------------------------------------
* @parameter string - input string
* @parameter string - additional ID for lightbox grouping
* @parameter string - (relative) folder with larger images
* @returns string - input string with inserted <A ...> tags
**************************************/
function AddLightboxLink($str, $ID="", $folder="large")
{
global $StoreRoot, $WebPath, $WebURL;
if ( $ID != "" ) $ID = "_" . $ID . "_";
preg_match_all("/<img[^>]*>/i", $str, $aRes); // find all instances of <IMG> in string
foreach ( $aRes[0] as $img ) { // $aRes[0] contains all instances, there is no $aRes[1] since we do not have () in search
preg_match("/ALT=\"([^\"]*)\"/i", $img, $aAlt); // find ALT= content
preg_match("/SRC=\"((?!(?:http|data))[^\"]*)\"/i", $img, $aSrc); // find SRC= content
$sSrc = $aSrc[1]; // SRC="" content (without SRC="")
$sAlt = $aAlt[1]; // ALT="" content
if ( $sSrc != "" ) {
$rPath = dirname($sSrc); // web relative path
if ( $WebPath!="" ) $sSrc = str_replace($WebPath, '', $sSrc); // remove base
$sPath = dirname($StoreRoot .'/'. $sSrc); // filesystem path
$sName = basename($sSrc); // filename
$sAlt = $sAlt=="" ? $sName : $sAlt;
// check if large file exists
if ( fileExists($sPath .'/'. $folder .'/'. $sName) ) {
// add "lightbox" link to large image
$str = str_replace($img, "<a href=\"". $rPath .'/'. $folder .'/'. $sName ."\" class=\"fancybox\" rel=\"lightbox$ID\" title=\"". $sAlt ."\">". $img ."</a>", $str);
}
}
}
return $str;
}
/**************************************
* AddImageLink - find <IMG> tag in string and surround it with <A HREF=...>
*--------------------------------------
* @parameter string - input string
* @parameter string - HREF location
* @parameter string - (relative) folder with larger images
* @returns string - input string with inserted <A ...> tags
**************************************/
function AddImageLink($str, $link="", $folder="large")
{
global $StoreRoot, $WebPath;
preg_match_all("/<img[^>]*>/i", $str, $aRes); // find all instances of <IMG> in string
foreach ( $aRes[0] as $img ) { // $aRes[0] contains all instances, there is no $aRes[1] since we do not have () in search
preg_match("/ALT=\"([^\"]*)\"/i", $img, $aAlt); // find ALT= content
preg_match("/SRC=\"((?!(?:http|data))[^\"]*)\"/i", $img, $aSrc); // find SRC= content
$sSrc = $aSrc[1]; // SRC="" content (without SRC="")
$sAlt = $aAlt[1]; // ALT="" content
if ( $sSrc != "" ) {
$rPath = dirname($sSrc); // web relative path
if ( $WebPath!="" ) $sSrc = str_replace($WebPath, '', $sSrc); // remove base
$sPath = dirname($StoreRoot .'/'. $sSrc); // filesystem path
$sName = basename($sSrc); // filename
$sAlt = $sAlt=="" ? $sName : $sAlt;
// check if large file exists
if ( fileExists($sPath .'/'. $folder .'/'. $sName) ) {
// add link to large image
$str = str_replace($img, "<a href=\"". $link . $rPath .'/'. $folder .'/'. $sName ."\" title=\"". $sAlt ."\">". $img ."</a>", $str);
}
}
}
return $str;
}
/**************************************
* PrependImagePath - find <IMG> tag in string and insert path at beginning
*--------------------------------------
* @parameter string - input string
* @parameter string - path to insert
* @returns string - input string with inserted paths
**************************************/
function PrependImagePath($str, $path="")
{
preg_match_all("/<img[^>]*>/i", $str, $aRes); // find all instances of <IMG> in string
foreach ( $aRes[0] as $img ) { // $aRes[0] contains all instances, there is no $aRes[1] since we do not have () in search
$imgABS = preg_replace( "/(SRC=\")(?!(?:http|data|\/))([^\"]*\")/i", '$1'. $path .'$2', $img ); // find SRC= content
$str = str_replace( $img, $imgABS, $str );
}
return $str;
}
/**************************************
* koncnica - return word ending (plural form) for literal numbers
*--------------------------------------
* @parameter integer - input number
* @parameter string - comma separated entries wor word endings
* @returns string - word ending corresponding to input number
* ex: 1 zmaj - echo $zmaji . " zmaj(" . koncnica($zmaji," ,a,i,ev") . ")"
* ex: 2 zmaj(a) - echo $zmaji . " zmaj(" . koncnica($zmaji," ,a,i,ev") . ")"
* ex: 3 zmaj(i) - echo $zmaji . " zmaj(" . koncnica($zmaji," ,a,i,ev") . ")"
* ex: 7 zmaj(ev) - echo $zmaji . " zmaj(" . koncnica($zmaji," ,a,i,ev") . ")"
**************************************/
function koncnica($kolicina=0, $koncnice=" ,a,e,ov")
{
$koncnice = explode(",", $koncnice);
switch ( $kolicina % 100 ) {
case 1: $Koncnica = $koncnice[0]; break;
case 2: $Koncnica = $koncnice[1]; break;
case 3:
case 4: $Koncnica = $koncnice[2]; break;
default: $Koncnica = $koncnice[3]; break;
}
return trim( $Koncnica );
}
function left($str, $count=1) { return substr($str, 0, $count); }
function right($str, $count=1) { return substr($str, -$count); }
function contains($haystack, $needle)
{
return strpos($haystack, $needle)!==false;
}
function rfind($c,$s)
{
while (right($s,1) != $c && strlen($s) > 2) {
$s = left($s,strlen($s)-1);
}
return strlen($s)-1;
}
function ArrayLen($arr)
{
return count($arr);
}
function now()
{
return date("Y-m-d H:i:s");
}
function isLeapYear($year)
{
return ((($year%4==0) && ($year%100)) || $year%400==0) ? (true):(false);
}
function isDate($date, $format='Y-m-d H:i:s')
{
$d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format)==$date;
}
function addDate($givendate, $day=0, $mth=0, $yr=0, $format='Y-m-d H:i:s')
{
$cd = strtotime($givendate);
$hr = abs($day)<1 && abs($day)>0 ? (int)($day * 24) : 0;
return date($format, mktime(date('H',$cd)+$hr,
date('i',$cd), date('s',$cd), date('m',$cd)+$mth,
date('d',$cd)+(int)$day, date('Y',$cd)+$yr));
}
function compareDate($date1, $date2, $format='Y-m-d H:i:s')
{
$datetime1 = DateTime::createFromFormat($format, $date1);
$datetime2 = DateTime::createFromFormat($format, $date2);
return round($datetime1->diff($datetime2)->format("%r%a.%H%I%S"),4);
}
function formatDate($date, $format='Y-m-d H:i:s')
{
$cd = strtotime($date);
return date($format, mktime((int)date('H',$cd), (int)date('i',$cd), (int)date('s',$cd),
(int)date('m',$cd), (int)date('d',$cd), (int)date('Y',$cd)));
}
function repeatstring($str, $count=1)
{
$out = "";
while ( $count-- > 0 )
$out .= $str;
return $out;
}
/**************************************
* EncodeUTF8 - convert Windows-1250 encoded string to UTF8 (slovenian letters only)
**************************************/
function EncodeUTF8($str)
{
$str = str_replace( "\xE8", "\xC4\x8D", $str );
$str = str_replace( "\xC8", "\xC4\x8C", $str );
$str = str_replace( "\x9A", "\xC5\xA1", $str );
$str = str_replace( "\x8A", "\xC5\xA0", $str );
$str = str_replace( "\x9E", "\xC5\xBE", $str );
$str = str_replace( "\x8E", "\xC5\xBD", $str );
$str = str_replace( "\xE6", "\xC4\x87", $str );
$str = str_replace( "\xC6", "\xC4\x86", $str );
$str = str_replace( "\xF0", "\xC4\x91", $str );
$str = str_replace( "\xD0", "\xC4\x90", $str );
return $str;
}
function DecodeUTF8($str)
{
$str = str_replace( "\xC4\x8D", "\xE8", $str );
$str = str_replace( "\xC4\x8C", "\xC8", $str );
$str = str_replace( "\xC5\xA1", "\x9A", $str );
$str = str_replace( "\xC5\xA0", "\x8A", $str );
$str = str_replace( "\xC5\xBE", "\x9E", $str );
$str = str_replace( "\xC5\xBD", "\x8E", $str );
$str = str_replace( "\xC4\x87", "\xE6", $str );
$str = str_replace( "\xC4\x86", "\xC6", $str );
$str = str_replace( "\xC4\x91", "\xF0", $str );
$str = str_replace( "\xC4\x90", "\xD0", $str );
return $str;
}
/**************************************
* Returns an string clean of UTF8, Win1252 & HTML characters.
* It will convert them to a similar ASCII character or strip them.
* (from www.unexpectedit.com)
**************************************/
function CleanString($text, $removepunct=false)
{
// 1) convert á ô => a o
$text = preg_replace("/[áàâãªä]/u","a",$text);
$text = preg_replace("/[ÁÀÂÃÄ]/u","A",$text);
$text = preg_replace("/[čćç]/u","c",$text);
$text = preg_replace("/[ČĆÇ]/u","C",$text);
$text = preg_replace("/[đ]/u","d",$text);
$text = preg_replace("/[Đ]/u","D",$text);
$text = preg_replace("/[éèêë]/u","e",$text);
$text = preg_replace("/[ÉÈÊË]/u","E",$text);
$text = preg_replace("/[ÍÌÎÏ]/u","I",$text);
$text = preg_replace("/[íìîï]/u","i",$text);
$text = preg_replace("/[óòôõºö]/u","o",$text);
$text = preg_replace("/[ÓÒÔÕÖ]/u","O",$text);
$text = preg_replace("/[úùûü]/u","u",$text);
$text = preg_replace("/[ÚÙÛÜ]/u","U",$text);
$text = preg_replace("/[šś]/u","s",$text);
$text = preg_replace("/[ŠŚ]/u","S",$text);
$text = preg_replace("/[žż]/u","z",$text);
$text = preg_replace("/[ŽŻ]/u","Z",$text);
$text = preg_replace("/[’‘‹›‚]/u","'",$text);
$text = preg_replace("/[“”«»„]/u",'"',$text);
$text = str_replace("–","-",$text);
$text = str_replace(" "," ",$text);
$text = str_replace("ł","l",$text);
$text = str_replace("Ł","L",$text);
$text = str_replace("ñ","n",$text);
$text = str_replace("Ñ","N",$text);
//2) Translation CP1252. – => -
$trans = get_html_translation_table(HTML_ENTITIES);
$trans[chr(130)] = '‚'; // Single Low-9 Quotation Mark
$trans[chr(131)] = 'ƒ'; // Latin Small Letter F With Hook
$trans[chr(132)] = '„'; // Double Low-9 Quotation Mark
$trans[chr(133)] = '…'; // Horizontal Ellipsis
$trans[chr(134)] = '†'; // Dagger
$trans[chr(135)] = '‡'; // Double Dagger
$trans[chr(136)] = 'ˆ'; // Modifier Letter Circumflex Accent
$trans[chr(137)] = '‰'; // Per Mille Sign
$trans[chr(138)] = 'Š'; // Latin Capital Letter S With Caron
$trans[chr(139)] = '‹'; // Single Left-Pointing Angle Quotation Mark
$trans[chr(140)] = 'Œ'; // Latin Capital Ligature OE
$trans[chr(145)] = '‘'; // Left Single Quotation Mark
$trans[chr(146)] = '’'; // Right Single Quotation Mark
$trans[chr(147)] = '“'; // Left Double Quotation Mark
$trans[chr(148)] = '”'; // Right Double Quotation Mark
$trans[chr(149)] = '•'; // Bullet
$trans[chr(150)] = '–'; // En Dash
$trans[chr(151)] = '—'; // Em Dash
$trans[chr(152)] = '˜'; // Small Tilde
$trans[chr(153)] = '™'; // Trade Mark Sign
$trans[chr(154)] = 'š'; // Latin Small Letter S With Caron
$trans[chr(155)] = '›'; // Single Right-Pointing Angle Quotation Mark
$trans[chr(156)] = 'œ'; // Latin Small Ligature OE
$trans[chr(159)] = 'Ÿ'; // Latin Capital Letter Y With Diaeresis
$trans['euro'] = '€'; // euro currency symbol
ksort($trans);
foreach ($trans as $k => $v) {
$text = str_replace($v, $k, $text);
}
// 3) remove <p>, <br/> ...
$text = strip_tags($text);
/*
$search = array('@<script[^>]*?>.*?</script>@si', // Strip out javascript
'@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
'@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
'@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments including CDATA
);
$text = preg_replace($search, '', $text);
*/
// 4) & => & " => '
$text = html_entity_decode($text);
// 5) remove Windows-1252 symbols like "TradeMark", "Euro"...
$text = preg_replace('/[^(\x20-\x7F)]*/','', $text);
// 6) remove ASCII punctuators (except -_@)
if ( $removepunct ) $text = preg_replace('/[\x21-\x2C\x2E-\x2F\x3A-\x3F\x5B-\x5E\x7B-\x7D]/','',$text);
$targets=array('\r\n','\n','\r','\t');
$results=array(" "," "," "," ");
$text = str_replace($targets,$results,$text);
return ($text);
}
/**************************************
* CleanupTinyMCE - cleanup TinyMCE edits
*--------------------------------------
* @parameter string - input string
* @returns string - converted input string
**************************************/
function CleanupTinyMCE($str)
{
$str = str_replace("š", "š", $str);
$str = str_replace("Š", "Š", $str);
$str = str_replace("č", "č", $str);
$str = str_replace("Č", "Č", $str);
$str = preg_replace("/(SRC=\")\.\.\//i", '$1', $str);
$str = preg_replace("/<([\/]*)EM>/i", '<$1I>', $str);
$str = preg_replace("/<([\/]*)STRONG>/i", '<$1B>', $str);
return $str;
}
/**************************************
* ReplaceSmileys - replace character smileys in string with images
*--------------------------------------
* @parameter string - input string containing character smileys
* @parameter string - optional: folder with images
* @returns string - converted input string
**************************************/
function ReplaceSmileys($str, $folder="./pic/")
{
$smileys = array();
$smileys[] = array('/([ >])(\:\-*\()([ <])/', 's01.png'); // :-(
$smileys[] = array('/([ >])(\:\-*\))([ <])/', 's02.png'); // :-)
$smileys[] = array('/([ >])(\:\-*D)([ <])/', 's03.png'); // :-D
$smileys[] = array('/([ >])(\;\-*\))([ <])/', 's04.png'); // ;-)
$smileys[] = array('/([ >])(\:\-*O)([ <])/i', 's05.png'); // :-O
$smileys[] = array('/([ >])(\:\-*S)([ <])/i', 's06.png'); // :-S
$smileys[] = array('/([ >])(\:\-*\$)([ <])/', 's07.png'); // :-$
$smileys[] = array('/([ >])(\:\'\()([ <])/', 's08.png'); // :'(
$smileys[] = array('/([ >])(\:\-*\|)([ <])/', 's09.png'); // :-|
$smileys[] = array('/([ >])(\:\-*P)([ <])/', 's10.png'); // :-P
$smileys[] = array('/([ >])(8\-*\))([ <])/', 's11.png'); // 8-)
$smileys[] = array('/([ >])(\:\-*\{\})([ <])/', 's12.png'); // :-{}
$smileys[] = array('/([ >])(\:\*\)*)([ <])/', 's12.png'); // :* :*)
$smileys[] = array('/([ >])(\<3)([ <])/i', 's13.png'); // <3
$smileys[] = array('/([ >])(\:\-*[Xx#!])([ <])/', 's14.png'); // :-X :-# :-!
$smileys[] = array('/([ >])(o\:\-*\))([ <])/i', 's15.png'); // o:-)
$smileys[] = array('/([ >])(>*\:\-*[>)])([ <])/', 's16.png'); // >:-> >:-)
$smileys[] = array('/([ >])(\-[_.]\-)([ <])/i', 's17.png'); // -_- -.-
$smileys[] = array('/([ >])(\(\@\))([ <])/', 's18.png'); // (@)
$smileys[] = array('/([ >])(\:\@)([ <])/', 's18.png'); // :@
$smileys[] = array('/([ >])(>*\:\-*>)([ <])/', 's16.png'); // >:->
$smileys[] = array('/([ >])(\:\-*[\/])([ <])/', 's19.png'); // :-/ :-\
$smileys[] = array('/([ >])([^ps]\:\-*[\/])([ <])/', 's19.png'); // :-/ :-\
$smileys[] = array('/([ >])(\;\-*\()([ <])/', 's20.png'); // ;-(
$smileys[] = array('/([ >])(\@\}\-[>;]\-+)([ <])/', 's21.png'); // @}->-- @}-;--
$smileys[] = array('/([ >])(\@\}\-(>)\-+)([ <])/','s21.png'); // @}->--
$smileys[] = array('/([ >])(\*FLOWER\*)([ <])/i', 's21.png'); // @}->--
$smileys[] = array('/([ >])(\:\.\()([ <])/', 's08.png'); // :.(
$smileys[] = array('/([ >])(B\-*\))([ <])/', 's11.png'); // B-)
$smileys[] = array('/([ >])(O\.o)([ <])/', 's22.png'); // O.o
$smileys[] = array('/([ >])(\*\.\*)([ <])/', 's20.png'); // *.*
for ( $i=0; $i < count($smileys); $i++ ) {
$str = preg_replace($smileys[$i][0], "$1<IMG CLASS=\"smiley\" ALT=\"$2\" SRC=\"" . $folder . $smileys[$i][1] . "\" BORDER=0>$3", $str);
}
return $str;
}
/**************************************
* SearchString
*--------------------------------------
* Attributes:
* DBFIELD - database field name
* STRING - a search string to be parsed
*
* Returns:
* SearchString
*
* A custom function to parse a complete search string in keywords (tokens)
* used by SQL LIKE statement. to search using
* OR technique. AND technique is achieved as a single OR token, composed of two
* or more words combined with '+' (plus) sign.
*
* Examples:
* STRING: good manners
* RESULT: DBFIELD like '%good%' and DBFIELD like '%manners%'
*
* STRING: good OR manners
* STRING: good ALI manners (slovenian implementation)
* STRING: good,manners
* STRING: good, manners
* RESULT: DBFIELD like '%good%' or DBFIELD like '%manners%'
*
* STRING: -good -manners
* RESULT: DBFIELD not like '%good%' and DBFIELD not like '%manners%'
*
* STRING: "good manners"
* RESULT: DBFIELD like '%good manners%'
* -------------------------------------
* Developed by Blaz Kristan ([email protected])
**************************************/
function SearchString($DBField, $String="")
{
// preoblikujem iskalno besedilo
$String = str_replace("[", "[[]", $String);
$String = str_replace("%", "[%]", $String);
$String = str_replace("_", "[_]", $String);
$String = str_replace("*", "", $String);
$String = str_replace("'", "", $String);
$String = str_replace("?", "_", $String);
$String = str_replace("-", " -", $String);
$String = str_replace("- ", " ", $String);
$String = str_replace("+ ", " ", $String);
$String = preg_replace("/[[:space:]]+/", " ", $String);
// loim iskane besede za iskanje z ALI
$String = str_replace(", " , " +", $String);
$String = str_replace("," , " +", $String);
$String = str_replace(" ALI ", " +", $String);
$String = str_replace(" OR " , " +", $String);
// loim iskane besede za iskanje z IN
$String = str_replace(" IN " , " ", $String);
$String = str_replace(" AND ", " ", $String);
// parsam iskalno besedilo v seznam iskanih besed
$SearchSubstrings = explode(" ", $String);
$SearchList = array();
$Nest = ""; // zaasno za zdruevanje citiranih stringov
foreach ( $SearchSubstrings as $SearchSubstring ) {
$Nest .= $SearchSubstring .' ';
// imamo citiran string (quote)
if ( left($SearchSubstring, 1)=='"' && right($SearchSubstring, 1) != '"' )
continue; // skok na naslednjo besedo
// zakljuek citiranega stringa
if ( trim($Nest)!=$SearchSubstring && right($SearchSubstring, 1) != '"' )
continue; // skok na naslednjo besedo
// dodam v seznam iskanih stringov
$SearchList[] = trim($Nest);
$Nest = ""; // pocistim zacasno variablo
}
// bildam SQL WHERE stavek, ki je zaradi varnosti v oklepajih
if ( count($SearchList)>0 ) {
$SearchString = "(";
foreach ( $SearchList as $Niz ) {
if ( left($Niz, 1) == "+" ) {
// e e nekaj imam v iskalnem stringu dodam oklepaje in OR
if ( strlen($SearchString) > 1 )
$SearchString = left($SearchString, strlen($SearchString)-5) . ") OR (";
if ( substr($Niz, 1, 1)=="-" ) // e imam "+-" dodam NOT LIKE
$SearchString .= "$DBField NOT LIKE '%" . preg_replace('/["+-]/', "", $Niz) . "%' AND "; // odstranim ["+-]
else // drugae dodam samo LIKE
$SearchString .= "$DBField LIKE '%" . preg_replace('/["+-]/', "", $Niz) . "%' AND "; // odstranim ["+-]
} elseif ( left( $Niz, 1 ) == "-" ) // - pomeni, da te besede noemo
$SearchString .= "$DBField NOT LIKE '%" . preg_replace('/["+-]/', "", $Niz) . "%' AND "; // odstranim ["+-]
else // zgolj dodamo besedo
$SearchString .= "$DBField LIKE '%" . preg_replace('/["]/', "", $Niz) . "%' AND "; // odstranim ["]
}
// e elim iskati samo cele besede moram v SQL string dodati [^a-z], npr:
//(FIELD like '%[^a-z]STRING[^a-z]%' or FIELD like '%[^a-z]STRING%' or FIELD like '%STRING[^a-z]%' or FIELD like '%STRING%')
// odstranim zadnji " AND " in zakljuim z ")"
return left($SearchString, strlen($SearchString)-5) .")";
}
return "";
}
/**************************************
* ImageResize
*--------------------------------------
* Attributes:
* filefield="[filefield]" %Name of form field where photo is uploaded or
* % actual filename prefixed by '->' (rename) or '=>' (keep name)
* imagepath="[photopath]" %Server path to the image directory
* thumbprefix="[prefix]" /optional %Server path to the thumbnail directory or thumb prefix
* largeprefix="[largeprefix]" /optional %Server path to the original image directory or large prefix
* maxsize=[number,array] /optional %Maximum width/height of image
* thumbsize=[number] /optional %Width/height of the generated thumbnail (<0 = square thumbnail (cropped))
* jpegquality=[number] /optional %JPEG quality % setting [0-100]
* nameconflict="[text]" /optional %Action to take if uploaded image's name is already on the server
*
* Returns:
* array(name, width, height, reduced width, reduced height, thumb width, thumb height, size)
*
* Upload a $_FILES file and resize and/or create thumbnail image.
* Requires PhpThumbFactory library by Ian Selby/Gen X Design <http://phpthumb.gxdlabs.com>
**************************************/
require_once(dirname(__FILE__) .'/inc/thumb/ThumbLib.inc.php');
function ImageResize(
$filefield,
$imagepath,
$thumbprefix='_t_',
$largeprefix='_l_',
$maxsize = 0,
$thumbsize = 0,
$jpegquality = 90,
$nameconflict = "makeunique"
) {
// Error Checking
$doit = true;
$message = "";
if ( !isset($imagepath) || !is_dir($imagepath) ) {
$doit = false;
$message = "You need to specify a valid image path!";
} else {}
if ( !isset($filefield) ) {
$doit = false;
$message = "You need to specify a file field!";
} else {}
if ( !isset($thumbprefix) || !isset($thumbsize) ) {
$doit = false;
$message = "Wrong thumbnail size and prefix.";
} else {}
if ( !isset($largeprefix) ) {
$doit = false;
$message = "Wrong large image prefix.";
} else {}
if ( !(strtolower($nameconflict)=="makeunique"
|| strtolower($nameconflict)=="overwrite"
|| strtolower($nameconflict)=="error") ) {
$nameconflict = "makeunique";
} else {}
// parse $filefield
if ( left($filefield,2)=='->' || left($filefield,2)=='=>' ) {
// '->' or '=>' in front of actual name means already uploaded file (existing in the app folder)
// -> change the name of file
// => do not change the name (except uppercase and space)
$tmpfile = substr($filefield,2);
$photo = (left($filefield,1)=='=' ? basename($tmpfile) : "photo". date("-Ymd-His") . strrchr(basename($tmpfile),'.'));
$photo = strtolower(str_replace(' ', '-', $photo));
if ( !contains(".gif,.jpg,.png", right($photo, 4)) ) {
$doit = false;
$message = "Wrong image type. Only GIF, JPEG and PNG allowed.";
}
} elseif ( !$_FILES[$filefield]['error'] ) {
// file being uploaded
$tmpfile = $_FILES[$filefield]['tmp_name'];
$photo = strtolower(str_replace(' ','-',CleanString(basename($_FILES[$filefield]['name']))));
if ( !contains(".gif,.jpg,.png", right($photo, 4)) ) {
$doit = false;
$message = "Wrong image type. Only GIF, JPG and PNG allowed.";
}
} else {
$doit = false;
$message = "File upload error.";
}
if ( $doit ) {
$ext = strrchr($photo, '.');
$name = left($photo, strlen($photo)-4);
$retina = (right($name, 3) === '@2x'); // check if 'retina' size/intent upload
if ( $retina ) {
$name = left($name, strlen($name)-3); // remove retina designator from name
}
switch ( strtolower($nameconflict) ) {
case "makeunique":
// if file exists make filename unique
for ( $i=1; fileExists($imagepath .'/'. $name . $ext); )
$name = "photo". date("-Ymd-His-") . $i++;
break;
case "error":
if ( fileExists($imagepath .'/'. $name . $ext) ) {
$doit = false;
$message = "File already exists and NAMECONFLICT=ERROR specified.";
}
break;
}
$uploadfile = $imagepath .'/'. $name . $ext;
$thumbfile = $imagepath .'/'. $thumbprefix . $name . $ext;
$largefile = $imagepath .'/'. ($maxsize > 0 ? $largeprefix : '') . $name . $ext;
}
if ( $doit ) {
// uploaded image limits
if ( is_array($maxsize) ) {
$limit = max(abs($maxsize[0]),abs($maxsize[1])) * ($retina ? 2 : 1);
$maxsize = min(abs($maxsize[0]),abs($maxsize[1]));
} else
$limit = $retina ? 2048 : 1024;
// move file and resize image
if ( (left($filefield,2)=='->' ? rename($tmpfile, $largefile) : @move_uploaded_file($tmpfile, $largefile)) ) {
// resize image
try {
$thumb = PhpThumbFactory::create($largefile, array('jpegQuality' => $jpegquality,'resizeUp' => false));
// get image dimensions
$size = $thumb->getCurrentDimensions();
// limit original image size
if ( $size['width'] > $limit || $size['height'] > $limit ) {
$thumb->resize($limit, $limit)->save($largefile);
$size = $thumb->getCurrentDimensions();
}
$r_width = $i_width = $size['width'];
$r_height = $i_height = $size['height'];
// resize image if larger than limits
if ( isset($maxsize) && $maxsize > 0 && ($i_width > $maxsize || $i_height > $maxsize) ) {
// resize retina image
if ( $retina ) $thumb->resize($maxsize*2, $maxsize*2)->save($imagepath .'/'. $name .'@2x'. $ext);
$thumb->resize($maxsize, $maxsize)->save($uploadfile);
// get resized image dimesions
$size = $thumb->getCurrentDimensions();
$r_width = $size['width'];
$r_height = $size['height'];
} else {
// if resizing not specified or image is smaller than limits
if ( $retina ) { // handle retina upload
@copy($largefile, $imagepath .'/'. $name .'@2x'. $ext); // copy retina original
$size = $thumb->getCurrentDimensions(); // $thumb == $largefile
$max = max($size['width'],$size['height']);
$thumb->resize($max/2, $max/2)->save($uploadfile); // resize non-retina
// get image dimensions
$size = $thumb->getCurrentDimensions();
$r_width = $i_width = $size['width'];
$r_height = $i_height = $size['height'];
} else {
if ( $largefile != $uploadfile )
@copy($largefile, $uploadfile);
}
// remove original image
if ( $largefile != $uploadfile )
@unlink($largefile);
}
// create thumbnail (<0 crop it square)
$square = $thumbsize < 0;
$thumbsize = abs($thumbsize);
if ( $thumbprefix != '' && isset($thumbsize) && $thumbsize!=0 ) {
// adaptiveResize=square (crop&resize), resize=regular
if ( $square ) {
// allways create retina thumbnail if original image large enough
if ( $retina || ($r_width>$thumbsize*2 || $r_height>$thumbsize*2) ) // resize retina image
$thumb->adaptiveResize($thumbsize*2, $thumbsize*2)->save($imagepath .'/'. $thumbprefix . $name .'@2x'. $ext);
$thumb->adaptiveResize($thumbsize, $thumbsize)->save($thumbfile);
} else {
// allways create retina thumbnail if original image large enough
if ( $retina || ($r_width>$thumbsize*2 || $r_height>$thumbsize*2) ) // resize retina image
$thumb->resize($thumbsize*2, $thumbsize*2)->save($imagepath .'/'. $thumbprefix . $name .'@2x'. $ext);
$thumb->resize($thumbsize, $thumbsize)->save($thumbfile);
}
$size = $thumb->getCurrentDimensions();
$t_width = $size['width'];
$t_height = $size['height'];
} else {
$t_width = $t_height = 0;
}
// get file size
$stat = stat($uploadfile);
$fileSize = (int)$stat['size'];
} catch (Exception $e) {
// cleanup
@unlink($imagepath .'/'. $thumbprefix . $name .'@2x'. $ext); // retina thumbnail
@unlink($imagepath .'/'. $thumbprefix . $name . $ext); // thumbnail
@unlink($imagepath .'/'. $name .'@2x'. $ext); // resized retina image
@unlink($uploadfile); // resized image
@unlink($largefile); //original image
trigger_error("Resize error!", E_USER_ERROR);
return false;
}
} else {
// cleanup
@unlink($largefile);
trigger_error("Move error!", E_USER_ERROR);
return false;
}
// return metadata
return array('name' => $name . $ext,
'iw' => $i_width, 'ih' => $i_height,
'rw' => $r_width, 'rh' => $r_height,
'tw' => $t_width, 'th' => $t_height,
'size' => $fileSize);
} else {
trigger_error($message, E_USER_NOTICE);
return false;
}
}
/**************************************
* ParseMetadata
*--------------------------------------
* Attributes:
* metafield="[metafield]" %string with metadata in the form: variable=value;variable=value;...
* separator=';' /OPTIONAL % metadata field separator
*
* Returns:
* array(variable, value)
**************************************/
function ParseMetadata($metafield, $separator=";")
{
if ( $metafield == "" ) return null;
$keys = array();
$values = array();
foreach ( explode($separator, $metafield) As $item ) {
if ( $item=="" ) continue;
$j = explode("=", $item);
$keys[] = $j[0];
$values[] = $j[1];
}
$Meta = array_combine($keys, $values);
unset($keys);
unset($values);
return $Meta;
}
?>