/
singapore.class.php
982 lines (840 loc) · 32.1 KB
/
singapore.class.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
<?php
/**
* Main class.
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License
* @copyright (c)2003-2006 Tamlyn Rhodes
* @version $Id: singapore.class.php,v 1.77 2006/11/03 15:59:15 tamlyn Exp $
*/
//define constants for regular expressions
define('SG_REGEXP_PROTOCOLURL', '(?:http://|https://|ftp://|mailto:)(?:[a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,4}(?::[0-9]+)?(?:/[^ \n\r\"\'<]+)?');
define('SG_REGEXP_WWWURL', 'www\.(?:[a-zA-Z0-9\-]+\.)*[a-zA-Z]{2,4}(?:/[^ \n\r\"\'<]+)?');
define('SG_REGEXP_EMAILURL', '(?:[\w][\w\.\-]+)+@(?:[\w\-]+\.)+[a-zA-Z]{2,4}');
/**
* Provides functions for handling galleries and images
* @package singapore
* @author Tamlyn Rhodes <tam at zenology dot co dot uk>
*/
class Singapore
{
/**
* current script version
* @var string
*/
var $version = "0.10.1CVS";
/**
* instance of a {@link sgConfig} object representing the current
* script configuration
* @var sgConfig
*/
var $config;
/**
* instance of the currently selected IO handler object
* @var sgIO_csv
*/
var $io;
/**
* instance of a {@link Translator}
* @var Translator
*/
var $translator;
/**
* instance of a {@link sgGallery} representing the current gallery
* @var sgGallery
*/
var $gallery;
/**
* reference to the currently selected {@link sgImage} object in the
* $images array of {@link $gallery}
* @var sgImage
*/
var $image;
/**
* two character code of language currently in use
* @var string
*/
var $language = null;
/**
* name of template currently in use
* @var string
*/
var $template = null;
/**
* details of current user
* @var sgUser
*/
var $user = null;
/**
* name of action requested
* @var string
*/
var $action = null;
/**
* Constructor, does all init type stuff. This code is a total mess.
* @param string the path to the base singapore directory
*/
function Singapore($basePath = "")
{
//import class definitions
//io handler class included once config is loaded
require_once $basePath."includes/translator.class.php";
require_once $basePath."includes/thumbnail.class.php";
require_once $basePath."includes/gallery.class.php";
require_once $basePath."includes/config.class.php";
require_once $basePath."includes/image.class.php";
require_once $basePath."includes/user.class.php";
//start execution timer
$this->scriptStartTime = microtime();
//remove slashes
if(get_magic_quotes_gpc())
$_REQUEST = array_map(array("Singapore","arraystripslashes"), $_REQUEST);
//desanitize request
$_REQUEST = array_map("htmlentities", $_REQUEST);
//load config from singapore root directory
$this->config =& sgConfig::getInstance();
$this->config->loadConfig($basePath."singapore.ini");
$this->config->loadConfig($basePath."secret.ini.php");
//if instantiated remotely...
if(!empty($basePath)) {
//...try to guess base path and relative url
if(empty($this->config->base_path))
$this->config->base_path = $basePath;
if(empty($this->config->base_url))
$this->config->base_url = $basePath;
//...load local config if present
//may over-ride guessed values above
$this->config->loadConfig("singapore.local.ini");
}
//set current gallery to root if not specified in url
$galleryId = isset($_REQUEST[$this->config->url_gallery]) ? $_REQUEST[$this->config->url_gallery] : ".";
//load config from gallery ini file (gallery.ini) if present
$this->config->loadConfig($basePath.$this->config->pathto_galleries.$galleryId."/gallery.ini");
//set current template from request vars or config
//first, preset template to default one
$this->template = $this->config->default_template;
//then check if requested template exists
if(!empty($_REQUEST[$this->config->url_template])) {
$templates = Singapore::getListing($this->config->base_path.$this->config->pathto_templates);
foreach($templates->dirs as $single) {
if($single == $_REQUEST[$this->config->url_template]) {
$this->template = $single;
break;
}
}
}
$this->config->pathto_current_template = $this->config->pathto_templates.$this->template.'/';
//load config from template ini file (template.ini) if present
$this->config->loadConfig($basePath.$this->config->pathto_current_template."template.ini");
//set runtime values
$this->config->pathto_logs = $this->config->pathto_data_dir."logs/";
$this->config->pathto_cache = $this->config->pathto_data_dir."cache/";
$this->config->pathto_admin_template = $this->config->pathto_templates.$this->config->admin_template_name."/";
//set current language from request vars or config
if(!empty($_REQUEST[$this->config->url_lang]))
$this->language = $_REQUEST[$this->config->url_lang];
else {
$this->language = $this->config->default_language;
if($this->config->detect_language)
foreach($this->getBrowserLanguages() as $lang)
if($lang=="en" || file_exists($basePath.$this->config->pathto_locale."singapore.".$lang.".pmo")) {
$this->language = $lang;
break;
}
}
//read the language file
$this->translator =& Translator::getInstance($this->language);
$this->translator->readLanguageFile($this->config->base_path.$this->config->pathto_locale."singapore.".$this->language.".pmo");
//clear the UMASK
umask(0);
//include IO handler class and create instance
require_once $basePath."includes/io_".$this->config->io_handler.".class.php";
$ioClassName = "sgIO_".$this->config->io_handler;
$this->io = new $ioClassName($this->config);
//load gallery and image info
$this->selectGallery($galleryId);
//set character set
if(!empty($this->translator->languageStrings[0]["charset"]))
$this->character_set = $this->translator->languageStrings[0]["charset"];
else
$this->character_set = $this->config->default_charset;
//set action to perform
if(empty($_REQUEST["action"])) $this->action = "view";
else $this->action = $_REQUEST["action"];
}
/**
* Load gallery and image info
* @param string the id of the gallery to load (optional)
*/
function selectGallery($galleryId = "")
{
if(empty($galleryId)) $galleryId = isset($_REQUEST[$this->config->url_gallery]) ? $_REQUEST[$this->config->url_gallery] : ".";
//try to validate gallery id
if($galleryId != '.' && substr($galleryId, 0, 2) != './')
$galleryId = './'.$galleryId;
//detect back-references to avoid file-system walking
if(strpos($galleryId,"../")!==false) $galleryId = ".";
//find all ancestors to current gallery
$this->ancestors = array();
$ancestorNames = explode("/", $galleryId);
$numberOfAncestors = count($ancestorNames);
//construct fully qualified gallery ids
$ancestorIds[0] = ".";
for($i=1; $i<$numberOfAncestors; $i++)
$ancestorIds[$i] = $ancestorIds[$i-1]."/".$ancestorNames[$i];
//fetch galleries passing previous gallery as parent pointer
for($i=0; $i<$numberOfAncestors; $i++)
if(!$this->ancestors[$i] =& $this->io->getGallery(
$ancestorIds[$i], $this->ancestors[$i-1],
//only fetch children of bottom level gallery
($i==$numberOfAncestors-1) ? 1 : 0
)
)
break;
//need to remove bogus parent of root gallery created by previous step
unset($this->ancestors[-1]);
//set reference to current gallery
$this->gallery = &$this->ancestors[count($this->ancestors)-1];
//check if gallery was successfully fetched
if($this->gallery == null) {
$this->gallery = new sgGallery($galleryId, $this->ancestors[0]);
$this->gallery->name = $this->translator->_g("Gallery not found '%s'",htmlspecialchars($galleryId));
}
//sort galleries and images
$GLOBALS["sgSortOrder"] = $this->config->gallery_sort_order;
if($this->config->gallery_sort_order!="x") usort($this->gallery->galleries, array("Singapore","multiSort"));
$GLOBALS["sgSortOrder"] = $this->config->image_sort_order;
if($this->config->image_sort_order!="x") usort($this->gallery->images, array("Singapore","multiSort"));
unset($GLOBALS["sgSortOrder"]);
//if startat is set then cast to int otherwise startat 0
$this->gallery->startat = isset($_REQUEST[$this->config->url_startat]) ? (int)$_REQUEST[$this->config->url_startat] : 0;
$this->startat = $this->gallery->startat; //depreciated
//select the image (if any)
if(!empty($_REQUEST[$this->config->url_image]))
$this->selectImage($_REQUEST[$this->config->url_image]);
//load hit data
if($this->config->track_views || $this->config->show_views)
$this->io->getHits($this->gallery);
//update and save hit data
if($this->config->track_views) {
if($this->isImagePage()) {
$this->image->hits++;
$this->image->lasthit = time();
} elseif($this->gallery->startat == 0) {
$this->gallery->hits++;
$this->gallery->lasthit = time();
}
$this->io->putHits($this->gallery);
}
}
/**
* Selects an image from the current gallery
* @param mixed either the filename of the image to select or the integer
* index of its position in the images array
* @return boolean true on success; false otherwise
*/
function selectImage($image)
{
if(is_string($image)) {
foreach($this->gallery->images as $index => $img)
if($img->id == $image) {
$this->image =& $this->gallery->images[$index];
return true;
}
} elseif(is_int($image) && $image >= 0 && $image < count($this->gallery->images)) {
$this->image =& $this->gallery->images[$image];
return true;
}
$this->image =& new sgImage("", $this->gallery);
$this->image->name = $this->translator->_g("Image not found '%s'",htmlspecialchars($image));
return false;
}
/**
* Obfuscates the given email address by replacing "." with "dot" and "@" with "at"
* @param string email address to obfuscate
* @param boolean override the obfuscate_email config setting (optional)
* @return string obfuscated email address or HTML mailto link
*/
function formatEmail($email, $forceObfuscate = false)
{
if($this->config->obfuscate_email || $forceObfuscate)
return strtr($email,array("@" => ' <b>'.$this->translator->_g("email|at").'</b> ', "." => ' <b>'.$this->translator->_g("email|dot").'</b> '));
else
return "<a href=\"mailto:".$email."\">".$email."</a>";
}
/**
* Returns image name for image pages and gallery name for gallery pages.
* If either of these is empty, returns gallery_name config option.
*
* @return string Title of current page
*/
function pageTitle()
{
$crumbArray = $this->crumbLineArray();
$ret = "";
for($i=count($crumbArray)-1;$i>0;$i--)
$ret .= $crumbArray[$i]->nameForce()." < ";
$ret .= $crumbArray[$i]->nameForce();
return $ret;
}
/**
* @return bool true if this is an image page; false otherwise
*/
function isImagePage()
{
return !empty($this->image);
}
/**
* @return bool true if this is a non-album gallery page; false otherwise
*/
function isGalleryPage()
{
return !empty($this->gallery) && $this->gallery->galleryCount()>0;;
}
/**
* @return bool true if this is an album page; false otherwise
*/
function isAlbumPage()
{
return !$this->isGalleryPage() && !$this->isImagePage() && !empty($this->gallery);
}
/**
* @return int the script execution time in seconds rounded to two decimal places
*/
function scriptExecTime()
{
$scriptStartTime = $this->scriptStartTime;
$scriptEndTime = microtime();
list($usec, $sec) = explode(" ",$scriptStartTime);
$scriptStartTime = (float)$usec + (float)$sec;
list($usec, $sec) = explode(" ",$scriptEndTime);
$scriptEndTime = (float)$usec + (float)$sec;
$scriptExecTime = floor(($scriptEndTime - $scriptStartTime)*100)/100;
return $scriptExecTime;
}
/**
* Displays the script execution time if configured to do so
* @returns string the script execution time
*/
function scriptExecTimeText()
{
if($this->config->show_execution_time)
return $this->translator->_g("Page created in %s seconds",$this->scriptExecTime());
else
return "";
}
function poweredByText()
{
return $this->translator->_g("Powered by").' <a href="http://www.sgal.org/">singapore</a>';
}
function allRightsReserved()
{
return $this->translator->_g("All rights reserved.");
}
function licenseText()
{
return $this->translator->_g("Images may not be reproduced in any form without the express written permission of the copyright holder.");
}
function adminURL()
{
return '<a href="'.$this->config->base_url.'admin.php">';
}
function adminLink()
{
return $this->adminURL().$this->translator->_g("Log in")."</a>";
}
/**
* Checks to see if the user is currently logged in to admin mode. Also resets
* the login timeout to the current time.
* @returns boolean true if the user is logged in; false otherwise
* @static
*/
function isLoggedIn()
{
if(
isset($this->user) &&
$_SESSION["sgUser"]["ip"] == $_SERVER["REMOTE_ADDR"] &&
(time() - $_SESSION["sgUser"]["loginTime"] < 600)
) {
//reset loginTime to current time
$_SESSION["sgUser"]["loginTime"] = time();
return true;
}
return false;
}
function loadUser($username = null)
{
if($username == null)
if(isset($_SESSION["sgUser"]))
$username = $_SESSION["sgUser"]["username"];
else
return false;
$users = $this->io->getUsers();
foreach($users as $user)
if($user->username == $username) {
$this->user = $user;
return $user;
}
return false;
}
/**
* Creates an array of objects each representing an item in the crumb line.
* @return array the items of the crumb line
*/
function crumbLineArray()
{
$crumb = $this->ancestors;
if($this->isImagePage()) $crumb[] = $this->image;
return $crumb;
}
/**
* @return string the complete crumb line with links
*/
function crumbLineText()
{
$crumbArray = $this->crumbLineArray();
$ret = "";
for($i=0;$i<count($crumbArray)-1;$i++)
$ret .= $crumbArray[$i]->nameLink()." >\n";
$ret .= $crumbArray[$i]->nameForce();
return $ret;
}
function crumbLine()
{
return $this->translator->_g("crumb line|You are here:")." ".$this->crumbLineText();
}
/**
* Generates the HTML code for imagemap_navigation
* @return string imagemap HTML code
*/
function imageMap()
{
if(!$this->config->imagemap_navigation) return "";
$imageWidth = $this->image->width();
$imageHeight = $this->image->height();
$middleX = round($imageWidth/2);
$middleY = round($imageHeight/2);
$ret = "<map name=\"sgNavMap\" id=\"sgNavMap\">\n";
if($this->image->hasNext()) $ret .= '<area href="'.$this->image->nextURL().'" alt="'.$this->image->nextText().'" title="'.$this->image->nextText().'" shape="poly" ';
else $ret .= '<area href="'.$this->image->parentURL().'" alt="'.$this->image->parentText().'" title="'.$this->image->parentText().'" shape="poly" ';
$ret .= "coords=\"$middleX,$middleY,$imageWidth,$imageHeight,$imageWidth,0,$middleX,$middleY\" />\n";
if($this->image->hasPrev()) $ret .= '<area href="'.$this->image->prevURL().'" alt="'.$this->image->prevText().'" title="'.$this->image->prevText().'" shape="poly" ';
else $ret .= '<area href="'.$this->image->parentURL().'" alt="'.$this->image->parentText().'" title="'.$this->image->parentText().'" shape="poly" ';
$ret .= "coords=\"$middleX,$middleY,0,0,0,$imageHeight,$middleX,$middleY\" />\n";
$ret .= '<area href="'.$this->image->parentURL().'" alt="'.$this->image->parentText().'" title="'.$this->image->parentText().'" shape="poly" ';
$ret .= "coords=\"$middleX,$middleY,0,0,$imageWidth,0,$middleX,$middleY\" />\n";
$ret .= '</map>';
return $ret;
}
/**
* Generates the HTML code for the language select box
* @return string select box HTML code
*/
function languageFlipper()
{
if(!$this->config->language_flipper) return "";
$languageCache = $this->config->base_path.$this->config->pathto_data_dir."languages.cache";
// Look for the language file
if(!file_exists($languageCache))
return "";
// Open the file
$fp = @fopen($languageCache, "r");
if (!$fp) return "";
// Read contents
$str = '';
while (!feof($fp)) $str .= fread($fp, 1024);
// Unserialize
$availableLanguages = @unserialize($str);
$ret = '<div class="sgLanguageFlipper">';
$ret .= '<form method="get" action="'.$_SERVER["PHP_SELF"]."\">\n";
//carry over current get vars
foreach($_GET as $var => $val)
$ret .= '<input type="hidden" name="'.$var.'" value="'.htmlspecialchars($val)."\" />\n";
$ret .= '<select name="'.$this->config->url_lang."\">\n";
$ret .= ' <option value="'.$this->config->default_language.'">'.$this->translator->_g("Select language...")."</option>\n";
foreach($availableLanguages as $code => $name) {
$ret .= ' <option value="'.$code.'"';
if($code == $this->language && $this->language != $this->config->default_language)
$ret .= 'selected="true" ';
$ret .= '>'.htmlentities($name)."</option>\n";
}
$ret .= "</select>\n";
$ret .= '<input type="submit" class="button" value="'.$this->translator->_g("Go")."\" />\n";
$ret .= "</form></div>\n";
return $ret;
}
/**
* Generates the HTML code for the template select box
* @return string select box HTML code
*/
function templateFlipper()
{
if(!$this->config->template_flipper) return "";
//get list of installed templates
$templates = Singapore::getListing($this->config->base_path.$this->config->pathto_templates, "dirs");
$ret = '<div class="sgTemplateFlipper">';
$ret .= '<form method="get" action="'.$_SERVER["PHP_SELF"]."\">\n";
//carry over current get vars
foreach($_GET as $var => $val)
$ret .= '<input type="hidden" name="'.$var.'" value="'.htmlspecialchars($val)."\" />\n";
$ret .= '<select name="'.$this->config->url_template."\">\n";
$ret .= ' <option value="'.$this->config->default_template.'">'.$this->translator->_g("Select template...")."</option>\n";
foreach($templates->dirs as $name)
//do not list admin template(s)
if(strpos($name, "admin_")===false) {
$ret .= ' <option value="'.$name.'"';
if($name == $this->template && $this->template != $this->config->default_template)
$ret .= 'selected="true" ';
$ret .= '>'.$name."</option>\n";
}
$ret .= "</select>\n";
$ret .= '<input type="submit" class="button" value="'.$this->translator->_g("Go")."\" />\n";
$ret .= "</form></div>\n";
return $ret;
}
/**
* @param string $seperator optional string to seperate the Gallery Tab Links
* @return string
*/
function galleryTab($seperator = " | ")
{
$showing = $this->galleryTabShowing();
return Singapore::conditional($this->galleryTabLinks(), $showing.$seperator."%s", $showing);
}
/**
* @return string
*/
function galleryTabShowing()
{
if($this->isAlbumPage()) {
$total = $this->gallery->imageCount();
$perPage = $this->config->thumb_number_album;
} else {
$total = $this->gallery->galleryCount();
$perPage = $this->config->thumb_number_gallery;
}
if($this->gallery->startat+$perPage > $total)
$last = $total;
else
$last = $this->gallery->startat+$perPage;
return $this->translator->_g("Showing %s-%s of %s",($this->gallery->startat+1),$last,$total);
}
/**
* @return string
*/
function galleryTabLinks()
{
$ret = "";
if($this->hasPrevPage())
$ret .= $this->prevPageLink()." ";
//This is for compatibility with old templates
//it detects if compatibility mode is on using method_exists()
if(!$this->gallery->isRoot() && method_exists($this, 'galleryName'))
$ret .= $this->gallery->parentLink();
if($this->hasNextPage())
$ret .= " ".$this->nextPageLink();
return $ret;
}
function navigationLinks() {
$ret = "<link rel=\"Top\" title=\"".$this->ancestors[0]->name()."\" href=\"".$this->ancestors[0]->URL()."\" />\n";
if($this->isImagePage()) {
$ret .= "<link rel=\"Up\" title=\"".$this->image->parent->name()."\" href=\"".$this->image->parent->URL()."\" />\n";
if ($this->image->hasPrev()) {
$first= $this->image->firstImage();
$prev = $this->image->prevImage();
$ret .= "<link rel=\"First\" title=\"".$first->name()."\" href=\"".$first->URL()."\" />\n";
$ret .= "<link rel=\"Prev\" title=\"".$prev->name()."\" href=\"".$prev->URL()."\" />\n";
}
if ($this->image->hasNext()) {
$next = $this->image->nextImage();
$last = $this->image->lastImage();
$ret .= "<link rel=\"Next\" title=\"".$next->name()."\" href=\"".$next->URL()."\" />\n";
$ret .= "<link rel=\"Last\" title=\"".$last->name()."\" href=\"".$last->URL()."\" />\n";
//prefetch next image
$ret .= "<link rel=\"Prefetch\" href=\"".$next->imageURL()."\" />\n";
}
} else {
if(!$this->gallery->isRoot())
$ret .= "<link rel=\"Up\" title=\"".$this->gallery->parent->name()."\" href=\"".$this->gallery->parent->URL()."\" />\n";
if($this->hasPrevPage()) {
$ret .= "<link rel=\"Prev\" title=\"".$this->translator->_g("gallery|Previous")."\" href=\"".$this->prevPageURL()."\" />\n";
$ret .= "<link rel=\"First\" title=\"".$this->translator->_g("gallery|First")."\" href=\"".$this->firstPageURL()."\" />\n";
}
if($this->hasNextPage()) {
$ret .= "<link rel=\"Next\" title=\"".$this->translator->_g("gallery|Next")."\" href=\"".$this->nextPageURL()."\" />\n";
$ret .= "<link rel=\"Last\" title=\"".$this->translator->_g("gallery|Last")."\" href=\"".$this->lastPageURL()."\" />\n";
}
}
return $ret;
}
/**
* @return int the number of 'pages' or 'screen-fulls'
*/
function galleryPageCount() {
if($this->isAlbumPage())
return intval($this->gallery->imageCount()/$this->config->thumb_number_album)+1;
else
return intval($this->gallery->galleryCount()/$this->config->thumb_number_gallery)+1;
}
/**
* @return int
*/
function lastPageIndex() {
if($this->isAlbumPage())
return ($this->galleryPageCount()-1)*
($this->isAlbumPage()?$this->config->thumb_number_album:$this->config->thumb_number_gallery);
}
/**
* @return bool true if there is at least one more page
*/
function hasNextPage() {
if($this->isAlbumPage())
return count($this->gallery->images)>$this->startat+$this->config->thumb_number_album;
elseif($this->isGalleryPage())
return count($this->gallery->galleries)>$this->startat+$this->config->thumb_number_gallery;
elseif($this->isImagePage())
return isset($this->gallery->images[$this->image->index+1]);
}
/**
* @return bool true if there is at least one previous page
*/
function hasPrevPage() {
if($this->isAlbumPage() || $this->isGalleryPage())
return $this->startat > 0;
elseif($this->isImagePage())
return isset($this->gallery->images[$this->image->index-1]);
}
function firstPageURL() {
return $this->gallery->URL(0);
}
function firstPageLink() {
return "<a href=\"".$this->firstPageURL()."\">".$this->translator->_g("First %s", $this->itemsPerPage())."</a>";
}
/**
* @return string the URL of the previous page
*/
function prevPageURL() {
return $this->gallery->URL($this->startat - $this->itemsPerPage());
}
function prevPageLink() {
return "<a href=\"".$this->prevPageURL()."\">".$this->translator->_g("Previous %s", $this->itemsPerPage())."</a>";
}
/**
* @return string the URL of the next page
*/
function nextPageURL() {
return $this->gallery->URL($this->startat + $this->itemsPerPage());
}
function nextPageLink() {
return "<a href=\"".$this->nextPageURL()."\">".$this->translator->_g("Next %s", $this->itemsPerPage())."</a>";
}
function lastPageURL() {
$perpage = $this->isAlbumPage() ? $this->config->thumb_number_album : $this->config->thumb_number_gallery;
return $this->gallery->URL(floor($this->gallery->itemCount() / $perpage) * $perpage);
}
function lastPageLink() {
return "<a href=\"".$this->lastPageURL()."\">".$this->translator->_g("Last %s", $this->itemsPerPage())."</a>";
}
function itemsPerPage()
{
return $this->isAlbumPage() ? $this->config->thumb_number_album : $this->config->thumb_number_gallery;
}
/**
* @return string link for adding a comment to image
*/
function imageCommentLink()
{
return "<a href=\"".
$this->formatURL($this->gallery->idEncoded(), $this->image->id, null, "addcomment").
"\">".$this->translator->_g("Add a comment")."</a>";
}
/**
* @return array array of sgImage objects
*/
function &previewThumbnailsArray()
{
$ret = array();
//start at half way before current image or at 0, whichever is greatest
$start = max($this->image->index() - floor($this->config->thumb_number_preview/2), 0);
//end at start plus number of previews or the last image, whichever comes first
$end = min($start + $this->config->thumb_number_preview, $this->image->parent->imageCount());
//adjust start to end minus number of previews if that is less than current start
$start = min($start, max($end - $this->config->thumb_number_preview, 0));
//copy the images out of the array
for($i = $start; $i < $end; $i++)
$ret[$i] =& $this->image->parent->images[$i];
return $ret;
}
function previewThumbnails()
{
$thumbs =& $this->previewThumbnailsArray();
$index = $this->image->index();
$ret = "";
foreach($thumbs as $key => $thumb) {
$thumbClass = "sgThumbnailPreview";
if($key==$index-1) $thumbClass .= " sgThumbnailPreviewPrev";
elseif($key==$index) $thumbClass .= " sgThumbnailPreviewCurrent";
elseif($key==$index+1) $thumbClass .= " sgThumbnailPreviewNext";
$ret .= $thumb->thumbnailLink($thumbClass, "preview")."\n";
}
return $ret;
}
//////////////////////////////
//////ex-sgUtils methods//////
//////////////////////////////
/**
* Callback function for sorting things
* @static
*/
function multiSort($a, $b) {
switch($GLOBALS["sgSortOrder"]) {
case "f" :
case "p" : return strcmp($a->id, $b->id); //path
case "F" :
case "P" : return strcmp($b->id, $a->id); //path (reverse)
case "n" : return strcmp($a->name, $b->name); //name
case "N" : return strcmp($b->name, $a->name); //name (reverse)
case "i" : return strcasecmp($a->name, $b->name); //case-insensitive name
case "I" : return strcasecmp($b->name, $a->name); //case-insensitive name (reverse)
case "a" : return strcmp($a->artist, $b->artist); //artist
case "A" : return strcmp($b->artist, $a->artist); //artist (reverse)
case "d" : return strcmp($a->date, $b->date); //date
case "D" : return strcmp($b->date, $a->date); //date (reverse)
case "l" : return strcmp($a->location, $b->location); //location
case "L" : return strcmp($b->location, $a->location); //location (reverse)
}
}
/**
* Slightly pointless method
*/
function conditional($conditional, $iftrue, $iffalse = null)
{
if($conditional) return sprintf($iftrue, $conditional);
elseif($iffalse != null) return sprintf($iffalse, $conditional);
else return "";
}
/**
* Callback function for recursively stripping slashes
* @static
*/
function arraystripslashes($toStrip)
{
if(is_array($toStrip))
return array_map(array("Singapore","arraystripslashes"), $toStrip);
else
return stripslashes($toStrip);
}
function thumbnailPath($gallery, $image, $width, $height, $forceSize, $mode = 1)
{
$config =& sgConfig::getInstance();
switch($mode) {
case 0 :
return $config->pathto_data_dir."cache/".$width."x".$height.($forceSize?"f":"").strtr("-$gallery-$image",":/?\\","----");
case 1 :
return $config->pathto_galleries.$gallery."/_thumbs/".$width."x".$height.($forceSize?"f":"").strtr("-$image",":/?\\","----");
}
}
/**
* @param string relative or absolute path to directory
* @param string regular expression of files to return (optional)
* @param bool true to get hidden directories too
* @returns stdClass|false a data object representing the directory and its contents
* @static
*/
function getListing($wd, $mask = null, $getHidden = false)
{
$dir = new stdClass;
$dir->path = realpath($wd)."/";
$dir->files = array();
$dir->dirs = array();
$dp = opendir($dir->path);
if(!$dp) return false;
while(false !== ($entry = readdir($dp)))
if(is_dir($dir->path.$entry)) {
if(($entry{0} != '.' && $entry{0} != '_') || $getHidden)
$dir->dirs[] = $entry;
} else {
if($mask == null || preg_match("/\.($mask)$/i",$entry))
$dir->files[] = $entry;
}
sort($dir->files);
sort($dir->dirs);
closedir($dp);
return $dir;
}
/**
* Recursively deletes all directories and files in the specified directory.
* USE WITH EXTREME CAUTION!!
* @returns boolean true on success; false otherwise
* @static
*/
function rmdir_all($wd)
{
if(!$dp = opendir($wd)) return false;
$success = true;
while(false !== ($entry = readdir($dp))) {
if($entry == "." || $entry == "..") continue;
if(is_dir("$wd/$entry")) $success &= Singapore::rmdir_all("$wd/$entry");
else $success &= unlink("$wd/$entry");
}
closedir($dp);
$success &= rmdir($wd);
return $success;
}
/**
* Returns an array of language codes specified in the Accept-Language HHTP
* header field of the user's browser. q= components are ignored and removed.
* hyphens (-) are converted to underscores (_).
* @return array accepted language codes
* @static
*/
function getBrowserLanguages()
{
$langs = array();
foreach(explode(",",$_SERVER["HTTP_ACCEPT_LANGUAGE"]) as $bit)
if($pos = strpos($bit,";"))
$langs[] = strtr(substr($bit,0,$pos),"-","_");
else
$langs[] = strtr($bit,"-","_");
return $langs;
}
/**
* Wrapper for mkdir() implementing the safe-mode hack
*/
function mkdir($path)
{
$config =& sgConfig::getInstance();
if($config->safe_mode_hack) {
$connection = ftp_connect($config->ftp_server);
// login to ftp server
$result = ftp_login($connection, $config->ftp_user, $config->ftp_pass);
// check if connection was made
if ((!$connection) || (!$result))
return false;
ftp_chdir($connection, $config->ftp_base_path); // go to destination dir
if(!ftp_mkdir($connection, $path)) // create directory
return false;
ftp_site($connection, "CHMOD ".$config->directory_mode." ".$path);
ftp_close($connection); // close connection
return true;
} else
return mkdir($path, octdec($config->directory_mode));
}
/**
* Tests if $child is within or is the same path as $parent.
*
* @param string path to parent directory
* @param string path to child directory or file
* @param bool set false to prevent canonicalisation of paths (optional)
* @return bool true if $child is contained within or is $parent
*/
function isSubPath($parent, $child, $canonicalise = true)
{
$parentPath = $canonicalise ? realpath($parent) : $parent;
$childPath = $canonicalise ? realpath($child) : $child;
return $parentPath && $childPath && substr($childPath,0,strlen($parentPath)) == $parentPath;
}
function isInGroup($groups1,$groups2)
{
return (bool) array_intersect(explode(" ",$groups1),explode(" ",$groups2));
}
}
?>