source: trunk/plog-includes/plog-functions.php @ 560

Revision 560, 78.4 KB checked in by sidtheduck, 2 years ago (diff)

+ Fix for ticket #179
+ Fixes for miscellaneous minor bugs

Line 
1<?php
2
3if (basename($_SERVER['PHP_SELF']) == basename( __FILE__ )) {
4        // ignorance is bliss
5        exit();
6}
7
8function generate_breadcrumb($collections = 'Collections', $sep = ' &raquo; '){
9        global $config;
10
11        $id = $GLOBALS['plogger_id'];
12
13        $collections_link = '<a href="'.generate_url("collections").'">' . $collections . '</a>';
14        $collections_name = '<strong>' . $collections . '</strong>';
15
16        switch ($GLOBALS['plogger_level']) {
17                case 'collection':
18                        $row = get_collection_by_id($id);
19                        $collection_name = '<strong>' . SmartStripSlashes($row['name']) . '</strong>';
20
21                        if ($config['truncate_breadcrumb'] == "collection"){
22                                $breadcrumbs = $collection_name;
23                        } else {
24                                $breadcrumbs = $collections_link . $sep . $collection_name;
25                        }
26
27                        // Does this ever happen?  Collection level + slideshow mode ends in SQL error
28                        //if ($GLOBALS['plogger_mode'] == "slideshow") $breadcrumbs .= $sep . '<strong>' . plog_tr('Slideshow');
29
30                break;
31                case 'slideshow':
32                case 'album':
33                        $row = get_album_by_id($id);
34                        $album_name = '<strong>' . SmartStripSlashes($row['name']) . '</strong>';
35                        $album_link = '<a accesskey="/" href="' . generate_url("album", $row['id']) . '">' . SmartStripSlashes($row['name']) . '</a>';
36
37                        $row = get_collection_by_id($row['parent_id']);
38                        $collection_link = '<a accesskey="/" href="' . generate_url("collection", $row['id']) . '">' . SmartStripSlashes($row['name']) . '</a>';
39
40                        if ($config['truncate_breadcrumb'] == "album"){
41                                $breadcrumbs = $album_name;
42                        } else if ($config['truncate_breadcrumb'] == "collection"){
43                                $breadcrumbs = $collection_link . $sep . $album_name;
44                        } else {
45                                $breadcrumbs = $collections_link . $sep . $collection_link . $sep . $album_name;
46                        }
47
48                        if ($GLOBALS['plogger_mode'] == "slideshow") {
49                                $breadcrumbs = str_replace($album_name, $album_link . $sep . '<strong>' . plog_tr('Slideshow') . '</strong>', $breadcrumbs);
50                        }
51
52                break;
53                case 'picture':
54                        $row = get_picture_by_id($id);
55                        $picture_name = '<span id="image_name"><strong>' . SmartStripSlashes(get_caption_filename($row)) . '</strong></span>';
56
57                        $row = get_album_by_id($row['parent_album']);
58                        $album_link = '<a accesskey="/" href="' . generate_url("album", $row['id']) . '">' . SmartStripSlashes($row['name']) . '</a>';
59
60                        $row = get_collection_by_id($row['parent_id']);
61                        $collection_link = '<a accesskey="/" href="' . generate_url("collection", $row['id']) . '">' . SmartStripSlashes($row['name']) . '</a>';
62
63                        if ($config['truncate_breadcrumb'] == "album"){
64                                $breadcrumbs = $album_link . $sep . $picture_name;
65                        } else if ($config['truncate_breadcrumb'] == "collection") {
66                                $breadcrumbs = $collection_link . $sep . $album_link . $sep . $picture_name;
67                        } else {
68                                $breadcrumbs = $collections_link . $sep . $collection_link . $sep . $album_link . $sep . $picture_name;
69                        }
70
71                        // Does this ever happen?  Picture level + slideshow adds 'Slideshow' to breadcrumbs only
72                        //if ($GLOBALS['plogger_mode'] == "slideshow") $breadcrumbs .= $sep . '<strong>' . plog_tr('Slideshow');
73
74                break;
75                case 'search':
76                        if ($config['truncate_breadcrumb'] == "album"){
77                                $row = get_album_by_id($id);
78                                $album_link = '<a accesskey="/" href="' . generate_url("album", $row['id']) . '">' . SmartStripSlashes($row['name']) . '</a>';
79                                $breadcrumbs = $album_link . $sep . '<strong>'.plog_tr('Search').'</strong>' . $sep . plog_tr('You searched for') . ' <strong>'.htmlspecialchars(SmartStripSlashes($_GET['searchterms'])).'</strong>.';
80                        } else if ($config['truncate_breadcrumb'] == "collection") {
81                                $row = get_collection_by_id($id);
82                                $collection_link = '<a accesskey="/" href="' . generate_url("collection", $row['id']) . '">' . SmartStripSlashes($row['name']) . '</a>';
83                                $breadcrumbs = $collection_link . $sep . '<strong>'.plog_tr('Search').'</strong>' . $sep . plog_tr('You searched for') . ' <strong>'.htmlspecialchars(SmartStripSlashes($_GET['searchterms'])).'</strong>.';
84                        } else {
85                                $breadcrumbs = $collections_link . $sep . '<strong>'.plog_tr('Search').'</strong>' . $sep . plog_tr('You searched for') . ' <strong>'.htmlspecialchars(SmartStripSlashes($_GET['searchterms'])).'</strong>.';
86                        }
87                break;
88                default:
89                $breadcrumbs = $collections_name;
90                break;
91        }
92
93        return '<div id="breadcrumb_links">'.$breadcrumbs.'</div>';
94}
95
96function generate_title() {
97        switch ($GLOBALS['plogger_level']) {
98                case 'collection':
99                        $row = get_collection_by_id($GLOBALS['plogger_id']);
100
101                        $breadcrumbs = SmartStripSlashes($row["name"]);
102                        if ($GLOBALS['plogger_mode'] == "slideshow") $breadcrumbs .= ' &raquo; ' . plog_tr('Slideshow');
103
104                break;
105                case 'slideshow':
106                case 'album':
107                        $row = get_album_by_id($GLOBALS['plogger_id']);
108                        $album_name = SmartStripSlashes($row["name"]);
109
110                        $row = get_collection_by_id($row["parent_id"]);
111
112                        if ($GLOBALS['plogger_mode'] == "slideshow") {
113                                $breadcrumbs = SmartStripSlashes($row["name"]) . ' &raquo; ' . $album_name.' &raquo; ' . ' ' . plog_tr('Slideshow');
114                        } else {
115                                $breadcrumbs = SmartStripSlashes($row["name"]) . ' &raquo; ' . $album_name;
116                        }
117
118                break;
119                case 'picture':
120                        $row = get_picture_by_id($GLOBALS['plogger_id']);
121                        $picture_name = get_caption_filename($row);
122
123                        $row = get_album_by_id($row["parent_album"]);
124                        $album_name = SmartStripSlashes($row["name"]);
125
126                        $row = get_collection_by_id($row["parent_id"]);
127                        $collection_name = SmartStripSlashes($row["name"]);
128
129                        $breadcrumbs = $collection_name . ' &raquo; ' . $album_name . ' &raquo; ' . $picture_name;
130
131                        // Hmm, slideshow on picture level, how does that make sense?
132                        if ($GLOBALS['plogger_mode'] == "slideshow") $breadcrumbs .= ' &raquo; ' . plog_tr('Slideshow');
133
134                break;
135                default:
136                $breadcrumbs = ' ' . plog_tr('Collections');
137        }
138
139        return $breadcrumbs;
140}
141
142function get_caption_filename($row) {
143        // Check for caption, use this instead of filename if it exists!
144        if (!empty($row["caption"]) > 0) {
145                $picture_name = $row["caption"];
146        } else {
147                $picture_name = basename($row["path"]);
148        }
149
150        return $picture_name;
151}
152
153function generate_jump_menu() {
154        global $config;
155
156        $output = '';
157        $image_count = array();
158
159        $output .= "<form id=\"jump_menu\" action=\"#\" method=\"get\">\n\t\t\t\t\t<div>";
160        $output .= "\n\t\t\t\t\t\t" . '<select name="jump_menu" onchange="document.location.href = this.options[this.selectedIndex].value;">
161                                                        <option value="#">' . plog_tr('Jump to') . '...</option>';
162
163        // 1. create a list of all albums with at least one photo
164        $sql = "SELECT
165        `parent_album`,
166        COUNT(*) AS `imagecount`
167        FROM `".TABLE_PREFIX."pictures`
168        GROUP BY `parent_album`";
169        $result = run_query($sql);
170
171        while($row = mysql_fetch_assoc($result)) {
172                $image_count[$row["parent_album"]] = $row["imagecount"];
173        }
174
175        // 2. get a list of all albums and collections
176        $sqlCollection = "SELECT
177        `a`.id AS `album_id`,
178        `a`.name AS `album_name`,
179        `c`.id AS `collection_id`,
180        `c`.name AS `collection_name`
181        FROM `".TABLE_PREFIX."albums` AS `a`
182        LEFT JOIN `".TABLE_PREFIX."collections` AS `c` ON `a`.`parent_id`=`c`.`id`
183        ORDER BY `c`.`name` ASC, `a`.`name` ASC";
184        $result = run_query($sqlCollection);
185
186        $last_collection = "";
187
188        while ($row = mysql_fetch_assoc($result)){
189                // skip albums with no images
190                if (empty($image_count[$row["album_id"]])) {
191                        continue;
192                }
193
194                if ($row["collection_id"] != $last_collection) {
195                        $output .= "\n\t\t\t\t\t\t\t" . '<option value="'.generate_url("collection",$row["collection_id"]).'">'.$row["collection_name"].'</option>';
196                        $last_collection = $row["collection_id"];
197                }
198
199                $output .= "\n\t\t\t\t\t\t\t" . '<option value="'.generate_url("album",$row["album_id"]).'">'.SmartStripSlashes($row["collection_name"]).': '.SmartStripSlashes($row["album_name"]).'</option>';
200        }
201
202        $output .= "\n\t\t\t\t\t\t</select>\n\t\t\t\t\t</div>\n\t\t\t\t</form>\n";
203
204        return $output;
205}
206
207function generate_exif_table($id, $condensed = 0) {
208        global $config;
209
210        $query = "SELECT * FROM `".TABLE_PREFIX."pictures` WHERE `id`=".intval($id);
211        $result = run_query($query);
212
213        if (mysql_num_rows($result) > 0){
214                $row = mysql_fetch_assoc($result);
215
216                foreach($row as $key => $val) if (trim($row[$key]) == '') $row[$key] = '&nbsp;';
217
218                $table_data = "\t\t\t\t\t\t" . '<div id="exif_table">
219                                                        <table id="exif_data"';
220
221                if (!$_SESSION["plogger_details"]){
222                        $table_data .= ' style="display: none;"';
223                }
224
225                // get image size
226                $img = $config['basedir'] . 'plog-content/images/' . SmartStripSlashes($row['path']);
227                list($width, $height, $type, $attr) = getimagesize($img);
228                $size = round(filesize($img) / 1024, 2);
229
230                if (!$condensed) {
231                        $table_data .= '>
232                                                                <tr>
233                                                                        <td><strong>' . plog_tr('Dimensions') .'</strong></td>
234                                                                        <td>'.$width .' x ' .$height.'</td>
235                                                                </tr>
236                                                                <tr>
237                                                                        <td><strong>' . plog_tr('File size') . '</strong></td>
238                                                                        <td>'.$size.' kbytes</td>
239                                                                </tr>
240                                                                <tr>
241                                                                        <td><strong>' . plog_tr('Taken on') . '</strong></td>
242                                                                        <td>'.$row["EXIF_date_taken"].'</td>
243                                                                </tr>
244                                                                <tr>
245                                                                        <td><strong>' . plog_tr('Camera model') . '</strong></td>
246                                                                        <td>'.$row["EXIF_camera"].'</td>
247                                                                </tr>
248                                                                <tr>
249                                                                        <td><strong>' . plog_tr('Shutter speed') . '</strong></td>
250                                                                        <td>'.$row["EXIF_shutterspeed"].'</td>
251                                                                </tr>
252                                                                <tr>
253                                                                        <td><strong>' . plog_tr('Focal length') . '</strong></td>
254                                                                        <td>'.$row["EXIF_focallength"].'</td>
255                                                                </tr>
256                                                                <tr>
257                                                                        <td><strong>' . plog_tr('Aperture') . '</strong></td>
258                                                                        <td>'.$row["EXIF_aperture"].'</td>
259                                                                </tr>
260                                                        </table>
261                                                </div><!-- /exif_table -->' . "\n";
262                } else {
263                        $table_data .= '><tr><td><strong>' . plog_tr('Dimensions') . '</strong></td><td>'.$width .' x ' .$height.'</td></tr><tr><td><strong>' . plog_tr('File size') . '</strong></td><td>'.$size.' ' . plog_tr('kbytes') . '</td></tr><tr><td><strong>' . plog_tr('Taken on') . '</strong></td><td>'.$row["EXIF_date_taken"].'</td></tr><tr><td><strong>' . plog_tr('Camera model') . '</strong></td><td>'.$row["EXIF_camera"].'</td></tr><tr><td><strong>' . plog_tr('Shutter speed') . '</strong></td><td>'.$row["EXIF_shutterspeed"].'</td></tr><tr><td><strong>' . plog_tr('Focal length') . '</strong></td><td>'.$row["EXIF_focallength"].'</td></tr><tr><td><strong>' . plog_tr('Aperture') . '</strong></td><td>'.$row["EXIF_aperture"].'</td></tr></table></div><!-- /exif_table -->' . "\n";
264                }
265        }
266
267        return $table_data;
268}
269
270function plogger_display_comments() {
271        global $config;
272
273        if (file_exists(THEME_DIR . '/comments.php')) {
274                include(THEME_DIR . '/comments.php');
275        } else {
276                include($config['basedir'] . 'plog-content/themes/default/comments.php');
277        }
278}
279
280function plogger_require_captcha() {
281        $_SESSION['require_captcha'] = true;
282}
283
284// generate header produces the Gallery Name, The Jump Menu, and the Breadcrumb trail
285// at the top of the image
286
287function generate_header() {
288        global $config;
289
290        $output = '<h1 id="gallery-name">'.stripslashes($config["gallery_name"]).'</h1>';
291        return $output;
292}
293
294function generate_sortby($level,$id) {
295        global $config;
296
297        $output = '';
298
299        $id = $GLOBALS["plogger_id"];
300
301        $fields = array(
302        'date' => plog_tr('Date Submitted'),
303        'date_taken' => plog_tr('Date Taken'),
304        'caption' => plog_tr('Caption'),
305        'filename' => plog_tr('Filename'),
306        'number_of_comments' => plog_tr('Number of Comments'),
307        );
308
309        if ($level == "album"){
310                // I think this should be a single form and I really should move the javascript functions
311                // into a separate file
312
313                // I need to merge those 2 forms. and since I'm reloading stuff anyway, I can just
314                // create correct urls. oh yeah, baby.
315                $output .= "\n\t\t\t\t\t" . '<form action="#" method="get">
316                                                <div class="nomargin">
317                                                        <label for="change_sortby">' . plog_tr('Sort by') . ':</label>
318                                                        <select id="change_sortby" name="change_sortby" onchange="document.location.href = this.options[this.selectedIndex].value;">';
319                foreach($fields as$fkey => $fval) {
320                        $value = generate_url("album",$id,array(1 => 'sorted','sortby' => $fkey,'sortdir' => $_SESSION['plogger_sortdir']));
321                        $output .= "\n\t\t\t\t\t\t\t\t" . '<option value="'.$value.'"';
322                        if ($_SESSION["plogger_sortby"] == $fkey) {
323                                $output .= ' selected="selected"';
324                        }
325                        $output .= ">$fval</option>";
326                }
327                $output .= "\n\t\t\t\t\t\t\t</select>\n\t\t\t\t\t\t</div>\n\t\t\t\t\t</form><!-- /sort by -->";
328        }
329        return $output;
330}
331
332function generate_sortdir($level,$id) {
333        global $config;
334
335        $output = '';
336        $id = $GLOBALS["plogger_id"];
337
338        $orders = array(
339        'asc' => plog_tr('Ascending'),
340        'desc' => plog_tr('Descending'),
341        );
342
343        if ($level == "album") {
344                $output .= "\n\t\t\t\t\t" . '<form action="#" method="get">
345                                                <div class="nomargin">
346                                                        <select id="change_sortdir" name="change_sortdir" onchange="document.location.href = this.options[this.selectedIndex].value;">';
347                foreach($orders as $okey => $oval) {
348                        $value = generate_url("album",$id,array(1 => 'sorted','sortby' => $_SESSION['plogger_sortby'],'sortdir' => $okey));
349                        $output .= "\n\t\t\t\t\t\t\t\t<option value=\"$value\"";
350                        if(strcasecmp($_SESSION["plogger_sortdir"], $okey) === 0) {
351                                        $output .= ' selected="selected"';
352                                }
353                                $output .= ">$oval</option>";
354                        }
355                        $output .= "\n\t\t\t\t\t\t\t</select>\n\t\t\t\t\t\t</div>\n\t\t\t\t\t</form><!-- /sort asc/desc -->\n\t\t\t\t";
356                }
357                return $output;
358        }
359
360        function generate_search_box() {
361                global $config;
362
363                $output = '<form action="'.generate_url("collections").'" method="get">
364                                        <div id="search_box">
365                                                <input type="hidden" name="level" value="search" />
366                                                <input type="text" name="searchterms" />
367                                                <input class="submit" type="submit" value="' . plog_tr('Search') .'" />
368                                        </div>';
369                if (!empty($config['query_args'])){
370                        $query = array();
371                        $query_args = explode("&amp;", $config['query_args']);
372                        foreach ($query_args as $value) {
373                                $query = explode("=", $value);
374                                $output .= "\n\t\t\t\t\t\t" . '<input type="hidden" name="'.$query[0].'" value="'.$query[1].'" />';
375                        }
376                }
377                $output .= "\n\t\t\t\t" . '</form>' . "\n";
378
379                return $output;
380        }
381
382        // benchmark timing
383        function getmicrotime($t) {
384                list($usec, $sec) = explode(" ",$t);
385                return ((float)$usec + (float)$sec);
386        }
387
388        // function for generating the slideshow javascript
389        function generate_slideshow_js($id, $mode) {
390                global $config;
391
392                // output the link to the slideshow javascript
393                $output = "\n\t" . '<script type="text/javascript" src="'.$config['gallery_url'].'plog-includes/js/plog-slideshow.js"></script>';
394                return $output;
395        }
396
397        function preload_album_images() {
398                global $thumbnail_config;
399                $script = "\n\t\t<script type=\"text/javascript\" language=\"javascript\">
400                <!--
401                function preload_images() {
402                        if (document.images)
403                        {
404                                preload_image_object = new Image();
405                                // set image url
406                                image_url = new Array();";
407                                $pic_array = get_picture_by_id($GLOBALS["image_list"]);
408                                $i = 0;
409                                foreach($pic_array as $pic) {
410                                        unset($path);
411                                        $url = generate_thumb($pic['path'],$pic['id'],THUMB_LARGE);
412                                        $script .= "\t\timage_url[$i] = '$url'\n";
413                                        $i++;
414                                }
415                                $script .= "var i = 0;
416                                for(i=0; i<image_url.length; i++)
417                                preload_image_object.src = image_url[i];
418                        }
419                }
420                //-->
421                </script>";
422                return $script;
423        }
424
425        // function for generating the slideshow interface
426function generate_slideshow_interface() {
427        global $config;
428
429        $picture_caption = plogger_get_picture_caption();
430
431        $large_link = '<a accesskey="v" title="' . plog_tr('View Large Image') . '" href="javascript:slides.hotlink()"><img src="'.THEME_URL.'images/search.gif" width="16" height="16" alt="'. plog_tr('View Large Image') . '" /></a>&nbsp;&nbsp;';
432        $prev_url = '<a accesskey="," title="' .plog_tr('Previous Image') . '" href="javascript:slides.previous();"><img src="'.THEME_URL.'images/rewind.gif" width="16" height="16" alt="' . plog_tr('Previous Image') . '" /></a>&nbsp;&nbsp;';
433        $stop_url = '<a accesskey="x" title="'. plog_tr('Stop Slideshow') . '" href="javascript:slides.pause();"><img src="'.THEME_URL.'images/stop.gif" width="16" height="16" alt="' . plog_tr('Stop Slideshow') . '" /></a>&nbsp;&nbsp;';
434        $play_url = '<a accesskey="s" title="'. plog_tr('Start Slideshow') . '" href="javascript:slides.play();"><img src="'.THEME_URL.'images/play.gif" width="16" height="16" alt="' . plog_tr('Start Slideshow') . '" /></a>&nbsp;&nbsp;&nbsp;';
435        $next_url = '<a accesskey="." title="' . plog_tr('Next Image') . '" href="javascript:slides.next();"><img src="'.THEME_URL.'images/fforward.gif" width="16" height="16" alt="' . plog_tr('Next Image') . '" /></a>';
436
437        $output = "\t\t\t" . '<div class="large-thumb-toolbar" style="width: '.$config["max_display_size"].'px;">'.$large_link.$prev_url.$stop_url.$play_url.$next_url.'</div><!-- /large-thumb-toolbar -->';
438
439        $imgtag = '<img id="slideshow_image" class="photos-large" src="about:blank" title="'.$picture_caption.'" alt="'.$picture_caption.'" />';
440
441        $output .= "\n\n\t\t\t" . '<div id="picture-holder">
442                                <a href="javascript:slides.hotlink()">'.$imgtag.'</a>
443                        </div><!-- /picture-holder -->
444                        <br />';
445        // activate slideshow object using javascript block
446        $output .= "\n\t\t\t" . '<script type="text/javascript">
447                                <!--
448                                if (document.images)
449                                {
450                                        slides.set_image(document.images.slideshow_image);
451                                        slides.textid = "picture_caption"; // optional
452                                        slides.imagenameid = "image_name"; // optional
453                                        slides.update();
454                                        slides.play();
455                                }
456                                //-->
457                        </script>';
458        return $output;
459}
460
461        function get_head_title() {
462                global $config;
463
464                $title = generate_title($GLOBALS['plogger_level'], $GLOBALS['plogger_id']);
465                return (SmartStripSlashes($config['gallery_name']) . ": ". $title);
466        }
467
468        function plogger_head() {
469                global $config;
470
471                if ($config["embedded"] == 0) {
472                print "\t<title>" . get_head_title() . "</title>";
473                }
474                print "\n\t" . '<meta http-equiv="Content-Type" content="text/html;charset=' . $config['charset'] . '" />';
475                print "\n\t" . '<meta http-equiv="imagetoolbar" content="false" />';
476                print generate_slideshow_js($GLOBALS["plogger_id"], "album");
477                // Embed URL to RSS feed for proper level.
478                print "\n\t" . '<link rel="alternate" type="application/rss+xml" title="RSS Feed" href="'.plogger_rss_link().'" />' . "\n";
479        }
480
481        function connect_db() {
482                global $config;
483
484                if (!PLOGGER_DB_HOST) {
485                        if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], "plog-admin")) {
486                                $install_url = "_install.php";
487                                $upgrade_url = "_upgrade.php";
488                                $img = "<img src=\"images/plogger.gif\" alt=\"Plogger\" />";
489                        } else {
490                                $install_url = "plog-admin/_install.php";
491                                $upgrade_url = "plog-admin/_upgrade.php";
492                                $img = "<img src=\"plog-admin/images/plogger.gif\" alt=\"Plogger\" />";
493                        }
494                        die($img."\n<br /><br />Please run <a href=\"".$install_url."\">_install.php</a> to set up Plogger. If you are upgrading from a previous version, please run <a href=\"".$upgrade_url."\">_upgrade.php</a>.");
495                }
496
497                global $PLOGGER_DBH;
498
499                $PLOGGER_DBH = mysql_connect(PLOGGER_DB_HOST, PLOGGER_DB_USER, PLOGGER_DB_PW) or die ("Plogger cannot connect to the database because: " . mysql_error());
500
501                mysql_select_db(PLOGGER_DB_NAME);
502
503                $mysql_version = mysql_get_server_info();
504                $mysql_charset_support = "4.1";
505
506                if (1 == version_compare($mysql_version, $mysql_charset_support)) {
507                        mysql_query("SET NAMES utf8");
508                }
509
510        }
511
512        function run_query($query) {
513                global $PLOGGER_DBH;
514
515                $result = @mysql_query($query, $PLOGGER_DBH);
516
517                if (!$result){
518                        $trace = debug_backtrace();
519
520                        die(mysql_error($PLOGGER_DBH) . '<br /><br />' .
521                        $query . '<br /><br />
522                        In file: '.$_SERVER["PHP_SELF"] . '<br /><br />
523                        On line: ' . $trace[0]["line"]);
524                } else {
525                        return $result;
526                }
527        }
528
529function get_active_collections_albums() {
530        $return = array(
531                "collections" => '',
532                "albums" => '');
533
534        $sql = "SELECT parent_collection,parent_album,COUNT(*) AS imagecount
535        FROM `".TABLE_PREFIX."pictures` GROUP BY parent_collection,parent_album";
536        $result = run_query($sql);
537        while($row = mysql_fetch_assoc($result)) {
538                $image_collection_count[$row["parent_collection"]] = $row["imagecount"];
539                $image_album_count[$row["parent_album"]] = $row["imagecount"];
540        }
541        $return['collections'] = array_keys($image_collection_count);
542        $return['albums'] = array_keys($image_album_count);
543        return $return;
544}
545
546        function generate_thumb($path, $prefix, $type = THUMB_SMALL) {
547                global $config;
548                global $thumbnail_config;
549
550                // for relative paths assume that they are relative to 'plog-content/images/' directory,
551                // otherwise just use the given path
552                if (file_exists($path)) {
553                        $source_file_name = $path;
554                } else {
555                        $source_file_name = $config['basedir'] . 'plog-content/images/' . SmartStripSlashes($path);
556                }
557
558                // the file might have been deleted and since phpThumb dies in that case
559                // try to do something sensible so that the rest of the images can still be seen
560
561                // there is a problem in safe mode - if the script and picture file are owned by
562                // different users, then the file cannot be read.
563
564                if (!is_readable($source_file_name)) {
565                        return false;
566                }
567
568                $imgdata = @getimagesize($source_file_name);
569
570                if (!$imgdata) {
571                        // unknown image format, bail out
572                        // do we want to have video support in the Plogger core?
573                        //return 'plog-graphics/thumb-video.gif';
574                        return false;
575                }
576
577                // attributes of original image
578                $orig_width = $imgdata[0];
579                $orig_height = $imgdata[1];
580
581                // XXX: food for thought - maybe we can return URL to some kind of error image
582                // if this function fails?
583
584                $base_filename = sanitize_filename(basename($path));
585
586                $thumb_config = $thumbnail_config[$type];
587
588                if (1 == $thumb_config['disabled']) {
589                        return $config['gallery_url'] . 'plog-content/images/' . $path;
590                }
591
592                $prefix = $thumb_config['filename_prefix'] . $prefix . "-";
593
594                $thumbpath = $config['basedir'] . 'plog-content/thumbs/'.$prefix.$base_filename;
595                $thumburl = $config['gallery_url'] . 'plog-content/thumbs/'.$prefix.$base_filename;
596
597                // if thumbnail file already exists and is generated after data for a thumbnail type
598                // has been changed, then we assume that the thumbnail is valid.
599                if (file_exists($thumbpath)){
600                        $thumbnail_timestamp = @filemtime($thumbpath);
601                        if ($thumb_config['timestamp'] < $thumbnail_timestamp) {
602                                return $thumburl;
603                        }
604                }
605
606                // if dimensions of source image are smaller than those of the requested
607                // thumbnail, then use the original image as thumbnail
608                if ($orig_width <= $thumb_config['size'] && $orig_height <= $thumb_config['size']) {
609                        copy($source_file_name,$thumbpath);
610                        return $thumburl;
611                }
612
613                // no existing thumbnail found or thumbnail config has changed,
614                // generate new thumbnail file
615
616                list($width, $height, $thumb_type, $attr) = @getimagesize($thumbpath);
617
618                require_once($config['basedir'] . 'plog-includes/lib/phpthumb/phpthumb.class.php');
619
620                $phpThumb = new phpThumb();
621
622                // set data
623                $phpThumb->setSourceFileName($source_file_name);
624                if ($imgdata[0] > $imgdata[1]) {
625                        $phpThumb->w = $thumb_config['size'];
626                } else {
627                        $phpThumb->h = $thumb_config['size'];
628                }
629
630                $phpThumb->q = $config['compression'];
631
632                // set zoom crop flag to get image squared off
633                if ($type == THUMB_SMALL && $config['square_thumbs']) {
634                        $phpThumb->zc = 1;
635                        $phpThumb->h = $thumb_config['size'];
636                        $phpThumb->w = $thumb_config['size'];
637                }
638
639                $phpThumb->config_use_exif_thumbnail_for_speed = false;
640
641                // Set image height instead of width if not using square thumbs
642                if ($type == THUMB_SMALL && !$config['square_thumbs']) {
643                        $phpThumb->h = $thumb_config['size'];
644                        $phpThumb->w = '';
645                }
646
647                if($type == THUMB_NAV) {
648                        $phpThumb->zc = 1;
649                        $phpThumb->h = $thumb_config['size'];
650                        $phpThumb->w = $thumb_config['size'];
651                }
652
653                // set options (see phpThumb.config.php)
654                // here you must preface each option with "config_"
655
656                // Set error handling (optional)
657                $phpThumb->config_error_die_on_error = false;
658
659                // generate & output thumbnail
660                if ($phpThumb->GenerateThumbnail()) {
661                        $phpThumb->RenderToFile($thumbpath);
662                } else {
663                        // do something with debug/error messages
664                        die('Failed: '.implode("\n", $phpThumb->debugmessages));
665                }
666
667                return $thumburl;
668        }
669
670        function get_picture_by_id($id, $album_id = null) {
671                global $config;
672
673                if(is_array($id)) {
674                        $where_cond = "IN ('".implode("', '",$id)."');";
675                } else {
676                        $where_cond = "= ".intval($id).";";
677                }
678
679                $sql = "SELECT
680                `p`.*,
681                `a`.`path` AS `album_path`,
682                `c`.`path` AS `collection_path`
683                FROM `".TABLE_PREFIX."pictures` AS `p`
684                LEFT JOIN `".TABLE_PREFIX."albums` AS `a` ON `p`.`parent_album`=`a`.`id`
685                LEFT JOIN `".TABLE_PREFIX."collections` AS `c` ON `p`.`parent_collection`=`c`.`id`
686                WHERE `p`.`id` ".$where_cond;
687
688                if ($album_id){
689                        $sql .= " AND `p`.`parent_album`=".intval($album_id);
690                }
691
692                $resultPicture = run_query($sql);
693
694                if (is_array($id) && mysql_num_rows($resultPicture) > 0) {
695                        $picdata = array();
696                        while ($row = mysql_fetch_assoc($resultPicture)) {
697                                $row['url'] = $config['gallery_url'].'plog-content/images/'.$row['collection_path'].'/'.$row['album_path'].'/'.basename($row['path']);
698                                array_unshift($picdata,$row);
699                        }
700                }
701                elseif (!is_array($id) && mysql_num_rows($resultPicture) > 0){
702                        $picdata = mysql_fetch_assoc($resultPicture);
703
704                        // eventually I want to get rid of the full path in pictures tables to avoid useless data duplication
705                        // the following is a temporary solution so I don't have to break all the functionality at once
706                        $picdata['url'] = $config['gallery_url'].'plog-content/images/'.$picdata['collection_path'].'/'.$picdata['album_path'].'/'.basename($picdata['path']);
707                } else {
708                        $picdata = false;
709                }
710
711                return $picdata;
712        }
713
714        function get_pictures($album_id, $order = "alpha", $sort = "DESC"){
715                global $config;
716
717                $query = "SELECT
718                `p`.*,
719                `a`.`path` AS `album_path`,
720                `c`.`path` AS `collection_path`
721                FROM `".TABLE_PREFIX."pictures` AS `p`
722                LEFT JOIN `".TABLE_PREFIX."albums` AS `a` ON `p`.`parent_album`=`a`.`id`
723                LEFT JOIN `".TABLE_PREFIX."collections` AS `c` ON `p`.`parent_collection`=`c`.`id`
724                WHERE `a`.`id`=".intval($album_id);
725
726                if ($order == "mod"){
727                        $query .= " ORDER BY `p`.`date_submitted` ";
728                }
729                else {
730                        $query .= " ORDER BY `p`.`caption` ";
731                }
732
733                if ($sort == "ASC"){
734                        $query .= " ASC ";
735                } else {
736                        $query .= " DESC ";
737                }
738
739                $result = run_query($query);
740
741                $pictures = array();
742
743                while ($row = mysql_fetch_assoc($result)){
744                        // See comment in get_picture_by_id
745                        $row['url'] = $config['gallery_url'].'plog-content/images/'.$row['collection_path'].'/'.$row['album_path'].'/'.basename($row['path']);
746                        $pictures[$row["id"]] = $row;
747                }
748
749                return $pictures;
750        }
751
752        function get_album_by_id($id) {
753                global $config;
754
755                $sql = "SELECT
756                `a`.*,
757                `c`.`path` AS `collection_path`,
758                `a`.`path` AS `album_path`,
759                `c`.`name` AS `collection_name`,
760                `a`.`name` AS `album_name`
761                FROM `".TABLE_PREFIX."albums` AS `a`
762                LEFT JOIN `".TABLE_PREFIX."collections` AS `c` ON `a`.`parent_id`=`c`.`id`
763                LEFT JOIN `".TABLE_PREFIX."pictures` AS `i` ON `a`.`thumbnail_id`=`i`.`id`
764                WHERE `a`.`id` = ".intval($id);
765                $result = run_query($sql);
766
767                if (mysql_num_rows($result) > 0){
768                        $album = mysql_fetch_assoc($result);
769
770                        if ($album["thumbnail_id"] == 0){
771                                $query = "SELECT
772                                `id`,
773                                `path`
774                                FROM `".TABLE_PREFIX."pictures`
775                                WHERE `parent_album`=".intval($album["id"])."
776                                ORDER BY `date_submitted` DESC
777                                LIMIT 1";
778                                $result = run_query($query);
779
780                                if (mysql_num_rows($result) > 0){
781                                        $row = mysql_fetch_assoc($result);
782                                        $album["thumbnail_id"] = $row["id"];
783                                }
784                        }
785                } else {
786                        $album = false;
787                }
788
789                return $album;
790        }
791
792        function get_album_by_name($name) {
793                $sql = "SELECT *
794                FROM `".TABLE_PREFIX."albums`
795                WHERE `name` = '".mysql_real_escape_string($name)."'";
796                $result = run_query($sql);
797
798                if (mysql_num_rows($result) > 0){
799                        $album = mysql_fetch_assoc($result);
800                } else {
801                        $album = false;
802                }
803
804                return $album;
805        }
806
807        function get_collection_by_id($id) {
808                global $config;
809
810                $sqlCollection = "SELECT
811                `c`.*,
812                `c`.`path` AS `collection_path`
813                FROM `".TABLE_PREFIX."collections` AS `c`
814                LEFT JOIN `".TABLE_PREFIX."pictures` AS `i` ON `c`.`thumbnail_id`=`i`.`id`
815                WHERE `c`.`id`=".intval($id)."
816                ORDER BY `c`.`name` ASC";
817                $resultCollection = run_query($sqlCollection);
818
819                if (mysql_num_rows($resultCollection) == 0){
820                        $collection = false;
821                }
822                else {
823                        $collection = mysql_fetch_assoc($resultCollection);
824
825                        if ($collection["thumbnail_id"] == 0){
826                                $query = "SELECT
827                                `id`,
828                                `path`
829                                FROM `".TABLE_PREFIX."pictures`
830                                WHERE `parent_collection`=".intval($collection["id"])."
831                                ORDER BY `date_submitted` DESC
832                                LIMIT 1";
833                                $result = run_query($query);
834
835                                if (mysql_num_rows($result) > 0){
836                                        $row = mysql_fetch_assoc($result);
837                                        $collection["thumbnail_id"] = $row["id"];
838                                }
839                        }
840                }
841
842                return $collection;
843        }
844
845        function get_collection_by_name($name) {
846                $sql = "SELECT *
847                FROM `".TABLE_PREFIX."collections`
848                WHERE name = '".mysql_real_escape_string($name)."'";
849                $result = run_query($sql);
850
851                if (mysql_num_rows($result) > 0){
852                        $collection = mysql_fetch_assoc($result);
853                } else {
854                        $collection = false;
855                }
856
857                return $collection;
858        }
859
860        function get_albums($collection_id = null, $sort = "alpha", $order = "DESC") {
861                global $config;
862
863                $albums = array();
864
865                if ($sort == "mod"){
866                        $query = "SELECT
867                        `a`.`id` AS `album_id`,
868                        `a`.`name` AS `album_name`,
869                        `c`.`id` AS `collection_id`,
870                        `c`.`name` AS `collection_name`,
871                        `a`.`description`,
872                        `a`.`thumbnail_id`
873                        FROM `".TABLE_PREFIX."pictures` AS `i`
874                        LEFT JOIN `".TABLE_PREFIX."albums` AS `a` ON `i`.`parent_album`=`a`.`id`
875                        LEFT JOIN `".TABLE_PREFIX."collections` AS `c` ON `i`.`parent_collection`=`c`.`id`
876                        LEFT JOIN `".TABLE_PREFIX."pictures` AS `i2` ON `a`.`thumbnail_id`=`i2`.`id` ";
877
878                        if ($collection_id){
879                                $query .= " WHERE `i`.`parent_collection`=".intval($collection_id);
880                        }
881
882                        $query .= "
883                        GROUP BY `i`.`parent_album`
884                        ORDER BY `i`.`date_submitted` ";
885
886                        if ($order == "ASC"){
887                                $query .= " ASC ";
888                        } else {
889                                $query .= " DESC ";
890                        }
891                } else {
892                        $query = "SELECT
893                        `a`.`id` AS `album_id`,
894                        `a`.`name` AS `album_name`,
895                        `c`.`id` AS `collection_id`,
896                        `c`.`name` AS `collection_name`,
897                        `a`.`description`,
898                        `a`.`thumbnail_id`
899                        FROM `".TABLE_PREFIX."albums` AS `a`
900                        LEFT JOIN `".TABLE_PREFIX."collections` AS `c` ON `a`.`parent_id`=`c`.`id`
901                        LEFT JOIN `".TABLE_PREFIX."pictures` AS `i` ON `a`.`thumbnail_id`=`i`.`id`";
902
903                        if ($collection_id){
904                                $query .= " WHERE `c`.id=".intval($collection_id)." ";
905                        }
906
907                        $query .= " ORDER BY `c`.`name` ASC, `a`.`name` ASC";
908                }
909
910                $result = run_query($query);
911
912                while ($album = mysql_fetch_assoc($result)) {
913                        if ($album["thumbnail_id"] == 0){
914                                $query = "SELECT
915                                `id`,
916                                `path`
917                                FROM `".TABLE_PREFIX."pictures`
918                                WHERE `parent_album`=".intval($album["album_id"])."
919                                ORDER BY `date_submitted` DESC
920                                LIMIT 1";
921                                $thumb_result = run_query($query);
922
923                                if (mysql_num_rows($thumb_result) > 0){
924                                        $row = mysql_fetch_assoc($thumb_result);
925                                        $album["thumbnail_id"] = $row["id"];
926                                }
927                        }
928
929                        $albums[$album["album_id"]] = $album;
930                }
931
932                return $albums;
933        }
934
935        function get_collections($sort = "alpha", $order = "DESC") {
936                global $config;
937
938                if ($sort == "mod"){
939                        $query = "SELECT
940                        `c`.*
941                        FROM `".TABLE_PREFIX."pictures` AS `i`
942                        LEFT JOIN `".TABLE_PREFIX."collections` AS `c` ON `i`.`parent_collection`=`c`.`id`
943                        LEFT JOIN `".TABLE_PREFIX."pictures` AS `i2` ON `c`.`thumbnail_id`=`i2`.`id`
944                        GROUP BY `i`.`parent_collection`
945                        ORDER BY `i`.`date_submitted` ";
946
947                        if ($order == "ASC"){
948                                $query .= " ASC ";
949                        } else {
950                                $query .= " DESC ";
951                        }
952                } else {
953                        $query = "SELECT
954                        `c`.*
955                        FROM `".TABLE_PREFIX."collections` AS `c`
956                        ORDER BY `c`.`name` ";
957
958                        if ($order == "ASC"){
959                                $query .= " ASC ";
960                        } else {
961                                $query .= " DESC ";
962                        }
963                }
964
965                $resultCollection = run_query($query);
966
967                $collections = array();
968
969                while ($collection = mysql_fetch_assoc($resultCollection)){
970                        if ($collection["thumbnail_id"] == 0){
971                                $query = "SELECT
972                                `id`,
973                                `path`
974                                FROM `".TABLE_PREFIX."pictures`
975                                WHERE `parent_collection`=".intval($collection["id"])."
976                                ORDER BY `date_submitted` DESC
977                                LIMIT 1";
978                                $result = run_query($query);
979
980                                if (mysql_num_rows($result) > 0){
981                                        $row = mysql_fetch_assoc($result);
982                                        $collection["thumbnail_id"] = $row["id"];
983                                }
984                        }
985
986                        $collections[$collection["id"]] = $collection;
987                }
988
989                return $collections;
990        }
991
992        function SmartAddSlashes($str){
993                if (get_magic_quotes_gpc()){
994                        return $str;
995                } else {
996                        return addslashes($str);
997                }
998        }
999
1000        function SmartStripSlashes($str){
1001                if (get_magic_quotes_gpc()){
1002                        return stripslashes($str);
1003                } else {
1004                        return $str;
1005                }
1006        }
1007
1008        // this tries hard to figure out level and object id from textual path to a resource, used
1009        // mostly if mod_rewrite is in use
1010        function resolve_path($str = "") {
1011                $rv = array();
1012                $path_parts = explode("/",$str);
1013
1014                $levels = array("collection","album","picture","arg1","arg2");
1015
1016                $current_level = "";
1017
1018                $names = array();
1019
1020                foreach($levels as $key => $level) {
1021                        if (isset($path_parts[$key])) {
1022                                $names[$level] = mysql_real_escape_string(urldecode(SmartStripSlashes($path_parts[$key])));
1023                                $current_level = $level;
1024                        }
1025                }
1026
1027                if (!empty($names["collection"])) {
1028                        $sql = "SELECT *
1029                        FROM `".TABLE_PREFIX."collections`
1030                        WHERE `path`='".$names["collection"]."'";
1031                        $result = run_query($sql);
1032
1033                        // No such collection, fall back to main page
1034                        if (mysql_num_rows($result) == 0){
1035                                return $rv;
1036                        }
1037
1038                        $collection = mysql_fetch_assoc($result);
1039
1040                        // what if there are multiple collections with same names? I hope there aren't .. this would
1041                        // suck. But here is an idea, we shouldn't allow the user to enter similar names
1042                        $rv = array("level" => "collection","id" => $collection["id"]);
1043                }
1044
1045                if (!empty($names['album'])) {
1046                        $sql = "SELECT *
1047                        FROM `".TABLE_PREFIX."albums`
1048                        WHERE `path`='".$names["album"]."'
1049                        AND `parent_id`=".intval($collection["id"]);
1050                        $result = run_query($sql);
1051
1052                        // no such album, fall back to collection
1053                        if (mysql_num_rows($result) == 0){
1054                                return $rv;
1055                        }
1056
1057                        $album = mysql_fetch_assoc($result);
1058
1059                        // try to detect slideshow. Downside is that you cannot have a picture with that name
1060                        if (isset($names['picture']) && $names['picture'] == 'slideshow') {
1061                                return array('level' => 'album','mode' => 'slideshow','id' => $album['id']);
1062                        }
1063
1064                        // deal with http://plogger/collection/album/sorted/field/asc and friends
1065                        if (isset($names['picture']) && $names['picture'] == 'sorted') {
1066                                if (isset($names['arg1'])) {
1067                                        $_SESSION['plogger_sortby'] = $names['arg1'];
1068                                }
1069
1070                                if (isset($names['arg2'])) {
1071                                        $_SESSION['plogger_sortdir'] = $names['arg2'];
1072                                }
1073
1074                                return array('level' => 'album','id' => $album['id']);
1075                        }
1076
1077                        $rv = array('level' => 'album','id' => $album['id']);
1078                }
1079
1080                if (!empty($names["picture"])) {
1081                        $sql = "SELECT *
1082                        FROM `".TABLE_PREFIX."pictures`
1083                        WHERE `caption`='".$names["picture"]."'
1084                        AND `parent_album`=".intval($album["id"]);
1085                        $result = run_query($sql);
1086
1087                        $picture = mysql_fetch_assoc($result);
1088
1089                        // no such caption, perhaps we have better luck with path?
1090                        if (!$picture) {
1091                                $filepath = join("/",$names);
1092                                $like_match = array("_", "%");
1093                                $like_replace = array("\_", "\%");
1094                                $filepath = str_replace($like_match, $like_replace, $filepath);
1095                                $sql = "SELECT *
1096                                FROM `".TABLE_PREFIX."pictures`
1097                                WHERE `path` LIKE '".$filepath."____'
1098                                AND `parent_album`=".intval($album["id"]);
1099                                $result = run_query($sql);
1100                                $picture = mysql_fetch_assoc($result);
1101                        }
1102
1103                        // no such picture, fall back to album
1104                        if (!$picture) {
1105                                return $rv;
1106                        }
1107
1108                        $rv = array("level" => "picture", "id" => $picture["id"]);
1109                }
1110
1111                return $rv;
1112        }
1113
1114        function generate_pagination($url, $current_page, $items_total, $items_on_page, $extra_params = ''){
1115                $output = '';
1116
1117                if (!isset($GLOBALS["total_pictures"])) $GLOBALS["total_pictures"] = 0;
1118
1119                if (($items_total == 0) && ($GLOBALS["total_pictures"] > 0)) {
1120                        $items_total = $GLOBALS["total_pictures"];
1121                }
1122
1123                $num_pages = ceil($items_total / $items_on_page);
1124
1125                // if adding arguments to mod_rewritten urls, then I need ? (question mark) before the arguments
1126                // otherwise I want &amp;
1127                //$last = substr($url,-1);
1128
1129                if (!strpos($url,"?")) {
1130                        $separator = "?";
1131                } else {
1132                        $separator = "&amp;";
1133                }
1134
1135                if ($num_pages > 1){
1136                        if ($current_page > 1){
1137                                $output .= ' <a accesskey="," class="pagPrev" href="'.$url.$separator.'plog_page='.($current_page - 1).$extra_params.'"><span>&laquo;</span></a> ';
1138                        }
1139
1140                        for ($i = 1; $i <= $num_pages; $i++){
1141                                if ($i == $current_page){
1142                                        $output .= '<span class="page_link"> ['.$i.'] </span>';
1143                                } else{
1144                                        $output .= '<a href="'.$url.$separator.'plog_page='.$i.$extra_params.'" class="page_link">'.$i.'</a> ';
1145                                }
1146                        }
1147
1148                        if ($current_page != $num_pages){
1149                                $output .= ' <a accesskey="." class="pagNext" href="'.$url.$separator.'plog_page='.($current_page + 1).$extra_params.'"><span>&raquo;</span></a> ';
1150                        }
1151                }
1152
1153                return $output;
1154        }
1155
1156        // sanitize filename by replacing international characters with underscores
1157        function sanitize_filename($str) {
1158                // allow only alphanumeric characters, hyphen, [, ], dot and apostrophe in file names
1159                // the rest will be replaced
1160                return preg_replace("/[^\w|\.|'|\-|\[|\]]/","_",$str);
1161        }
1162
1163        function generate_url($level, $id = -1, $arg = array(), $plaintext = false){
1164                global $config;
1165
1166                $rv = '';
1167
1168                if ($config['use_mod_rewrite']){
1169                        switch($level){
1170                        case "collection":
1171                                $query = "SELECT `path` FROM `".TABLE_PREFIX."collections` WHERE `id`=".intval($id);
1172                                $result = run_query($query);
1173                                $row = mysql_fetch_assoc($result);
1174                                $rv = $config['baseurl'].rawurlencode(SmartStripSlashes($row['path']))."/";
1175                        break;
1176                        case "album":
1177                                $query = "SELECT
1178                                `c`.`path` AS `collection_path`,
1179                                `a`.`path` AS `album_path`
1180                                FROM `".TABLE_PREFIX."albums` AS `a`
1181                                LEFT JOIN `".TABLE_PREFIX."collections` AS `c` ON `a`.`parent_id`=`c`.`id`
1182                                WHERE `a`.`id`=".intval($id);
1183                                $result = run_query($query);
1184                                $row = mysql_fetch_assoc($result);
1185
1186                                $rv = $config['baseurl'].rawurlencode(SmartStripSlashes($row['collection_path'])) . '/' . rawurlencode(SmartStripSlashes($row['album_path']))."/";
1187
1188                                // I need to give additional arguments to the url-s
1189                                if (sizeof($arg) > 0) {
1190                                        foreach($arg as $aval) {
1191                                                $rv .= $aval."/";
1192                                        }
1193                                }
1194                        break;
1195                        case "picture":
1196                                $pic = get_picture_by_id($id);
1197                                //$album = $pic['parent_album'];
1198                                $rv = $config['baseurl'].str_replace("%2F", "/", rawurlencode(substr(SmartStripSlashes($pic['path']), 0, -4)))."/";
1199                        break;
1200                        case "search":
1201                                $rv = $config['baseurl']."?level=search";
1202                                // I need to give additional arguments to the url-s
1203                                if (sizeof($arg) > 0) {
1204                                        foreach($arg as $akey => $aval) {
1205                                                // mod_rewrite url-s need /sorted in them, the old style ones do not.
1206                                                // this temporary workaround removes the 'sorted' string
1207                                                if ($aval != 'sorted') {
1208                                                        $rv .= "&amp;".$akey."=".$aval;
1209                                                }
1210                                        }
1211                                }
1212                        break;
1213                        case "collections":
1214                                default:
1215                                $rv = $config['baseurl'];
1216                        break;
1217                }
1218        } else {
1219                // if there are non-Plogger query items, get them here to prepend to the URL query string
1220                $query = (isset($config['query_args'])) ? "?".$config['query_args']."&amp;" : $query = "?";
1221
1222                switch($level){
1223                        case "collection":
1224                                return $config['baseurl'].$query.'level=collection&amp;id='.$id;
1225                        break;
1226                        case "album":
1227                                $rv = $config['baseurl'].$query.'level=album&amp;id='.$id;
1228                                if (sizeof($arg) > 0) {
1229                                        foreach($arg as $akey => $aval) {
1230                                                // mod_rewrite url-s need /sorted in them, the old style ones do not.
1231                                                // this temporary workaround removes the 'sorted' string
1232                                                if ($aval != 'sorted') {
1233                                                        $rv .= "&amp;".$akey."=".$aval;
1234                                                }
1235                                        }
1236                                }
1237                        break;
1238                        case "picture":
1239                                $rv = $config['baseurl'].$query.'level=picture&amp;id='.$id;
1240                        break;
1241                        case "search":
1242                                $rv = $config['baseurl'].$query."level=search";
1243                                // I need to give additional arguments to the url-s
1244                                if (sizeof($arg) > 0) {
1245                                        foreach($arg as $akey => $aval) {
1246                                                // mod_rewrite url-s need /sorted in them, the old style ones do not.
1247                                                // this temporary workaround removes the 'sorted' string
1248                                                if ($aval != 'sorted') {
1249                                                        $rv .= "&amp;".$akey."=".$aval;
1250                                                }
1251                                        }
1252                                }
1253                        break;
1254                        case "collections":
1255                                default:
1256                                $query = (isset($config['query_args'])) ? "?".$config['query_args'] : $query = "";
1257                                $rv = $config['baseurl'].$query;
1258                        break;
1259                }
1260        }
1261
1262        // replace &amp; with & if outputting to email
1263        if ($plaintext !== false){
1264                $rv = str_replace("&amp;","&",$rv);
1265        }
1266
1267        return $rv;
1268}
1269
1270function add_comment($parent_id,$author,$email,$url,$comment) {
1271        global $config;
1272
1273        if (empty($config["allow_comments"])) {
1274                return array("errors" => plog_tr('Comments disabled'));
1275        }
1276
1277        if (empty($author) || empty($email)) {
1278                return array("errors" => plog_tr("Your comment did not post! Please fill the required fields."));
1279        }
1280
1281        $ip = $_SERVER["REMOTE_ADDR"];
1282        $host = gethostbyaddr($ip);
1283
1284        // I want to use the original unescaped values later - to send the email
1285        $sql_author = mysql_real_escape_string($author);
1286        $sql_email = mysql_real_escape_string($email);
1287        $sql_url = mysql_real_escape_string($url);
1288        $sql_comment = mysql_real_escape_string($comment);
1289        $sql_ip = mysql_real_escape_string($ip);
1290
1291        $parent_id = intval($parent_id);
1292
1293        $result = array();
1294
1295        $picdata = get_picture_by_id($parent_id);
1296
1297        if (empty($picdata)) {
1298                return array("errors" => plog_tr("Could not post comment - no such picture"));
1299        }
1300
1301        if (empty($picdata["allow_comments"])) {
1302                return array("errors" => plog_tr("Comments disabled"));
1303        }
1304
1305        if ($config["comments_moderate"] == 1) {
1306                $approved = 0;
1307                $notify_msg = " " . plog_tr('(awaiting your approval)');
1308        } else {
1309                $approved = 1;
1310                $notify_msg = '';
1311        }
1312
1313        // right now all comments will be approved, spam protection can be implemented later
1314        $query = "INSERT INTO `".TABLE_PREFIX."comments` SET
1315        `author`='$sql_author',
1316        `email`='$sql_email',
1317        `url`='$sql_url',
1318        `date`=NOW(),
1319        `comment`='$sql_comment',
1320        `parent_id`='$parent_id',
1321        `approved` = '$approved',
1322        `ip` = '$ip'";
1323        $result = mysql_query($query);
1324
1325        if (!$result) {
1326                return array("errors" => plog_tr("Could not post comment ") . mysql_error());
1327        }
1328
1329        // XXX: admin e-mail address should be validated
1330        if ($config["comments_notify"] && $config["admin_email"]) {
1331                // create and send notify mail message
1332                $msg = plog_tr("New comment posted for picture") . ": " . basename($picdata['path']) . " $notify_msg\n\n";
1333                $msg .= plog_tr("Author") . ": $author (IP: $ip, $host)\n";
1334                $msg .= plog_tr("E-mail") . ": $email\n";
1335                $msg .= plog_tr("URI") . ": $url\n\n";
1336                $msg .= plog_tr("Comment") . ":\n$comment\n\n";
1337                $msg .= plog_tr("You can see all the comments for this picture here") . ":\n";
1338                $picurl = generate_url("picture", $parent_id, NULL, true);
1339                $msg .= $picurl;
1340                $headers  = "From: $author <$email>\r\n";
1341                $headers .= 'Content-type: text/plain; charset=utf-8\r\n';
1342
1343                // Subject should be encoded for international characters too!
1344                mail(
1345                $config['admin_email'],
1346                '[' . SmartStripSlashes($config['gallery_name']) . '] ' . plog_tr('New Comment From') . ' ' . $author,
1347                SmartStripSlashes($msg),
1348                $headers);
1349        }
1350
1351        return array("result" => plog_tr("Comment added") . ".");
1352}
1353
1354// Begin basic Plogger API functions
1355
1356// plogger_list_categories()
1357// This function will create a list of nested categorical links
1358// for use in sidebars
1359
1360function plogger_list_categories($class) {
1361        // first select id and name for all collections
1362        $query = "SELECT * FROM ".TABLE_PREFIX."collections";
1363        $result = run_query($query);
1364
1365        $output = "\n<ul class=\"$class\">";
1366
1367        // loop through each collection, output child albums
1368        while ($row = mysql_fetch_assoc($result)) {
1369                // output collection name
1370                $collection_link = '<a href="'.generate_url("collection",$row['id']).'">'.$row['name'].'</a>';
1371                $output .= "\n\t<li>$collection_link</li>";
1372
1373                // loop through child albums
1374                $query = "SELECT * FROM ".TABLE_PREFIX."albums WHERE parent_id = '$row[id]' ORDER BY name DESC";
1375
1376                $output .= "\n<ul>";
1377                while ($albums = mysql_fetch_assoc($result)) {
1378                        $album_link = '<a href="'.generate_url("albums",$albums['id']).'">'.$albums['name'].'</a>';
1379                        $output .= "\n\t<li>$album_link</li>";
1380                }
1381
1382                $output .= "\n</ul>";
1383        }
1384
1385        $output .= "\n</ul>";
1386
1387        echo $output;
1388}
1389
1390function is_allowed_extension($ext) {
1391        // return in_array(strtolower($ext),array("jpg","gif","png","bmp"));
1392        return in_array(strtolower($ext),array("jpg","gif","png","bmp","mp4","wmv"));
1393}
1394
1395function plogger_init() {
1396        global $config;
1397
1398        $_SESSION['require_captcha'] = false;
1399
1400        $page = isset($_GET["plog_page"]) ? intval($_GET["plog_page"]) : 1;
1401        $from = ($page - 1) * $config["thumb_num"];
1402
1403        if ($from < 0) {
1404                $from = 0;
1405        }
1406
1407        // We shouldn't set a limit for the slideshow
1408        if( $GLOBALS["plogger_mode"] == 'slideshow')
1409        $lim = -1;
1410        else
1411        $lim = $config["thumb_num"];
1412
1413        $id = $GLOBALS["plogger_id"];
1414
1415        if ($GLOBALS["plogger_level"] == "search") {
1416                plogger_init_search(array(
1417                'searchterms' => $_GET['searchterms'],
1418                'from' => $from,
1419                'limit' => $lim));
1420        } else if ($GLOBALS["plogger_level"] == "album") {
1421                plogger_init_pictures(array(
1422                'type' => 'album',
1423                'value' => $id,
1424                'from' => $from,
1425                'limit' => $lim,
1426                'sortby' => isset($_SESSION['plogger_sortby']) ? $_SESSION['plogger_sortby'] : '',
1427                'sortdir' => isset($_SESSION['plogger_sortdir']) ? $_SESSION['plogger_sortdir'] : ''));
1428        } else if ($GLOBALS["plogger_level"] == "collection") {
1429                plogger_init_albums(array(
1430                'from' => $from,
1431                'limit' => $lim,
1432                'collection_id' => $id,
1433                'sortby' => !empty($config['album_sortby']) ? $config['album_sortby'] : 'id',
1434                'sortdir' => !empty($config['album_sortdir']) ? $config['album_sortdir'] : 'DESC'));
1435        } else if ($GLOBALS["plogger_level"] == "picture") {
1436                // first lets load the thumbnail of the picture at the correct size
1437                plogger_init_picture(array('id' => $id));
1438        } else {
1439                // Show all of the collections
1440                plogger_init_collections(array(
1441                'from' => $from,
1442                'limit' => $lim,
1443                'sortby' => !empty($config['collection_sortby']) ? $config['collection_sortby'] : 'id',
1444                'sortdir' => !empty($config['collection_sortdir']) ? $config['collection_sortdir'] : 'DESC'));
1445        }
1446}
1447
1448function plogger_init_picture($arr) {
1449        $id = intval($arr['id']);
1450        $sql = "SELECT `id`, `parent_album` FROM `".TABLE_PREFIX."pictures` WHERE `id`=".$id;
1451        $result = run_query($sql);
1452
1453        $row = mysql_fetch_assoc($result);
1454
1455        if (!$row) {
1456                return false;
1457        }
1458
1459        // generate a list of all image id-s so proper prev/next links can be created. This should be a
1460        // fast query, even for big albums.
1461        $image_list = array();
1462
1463        $sql = "SELECT `id` FROM `".TABLE_PREFIX."pictures` WHERE `parent_album`=".$row["parent_album"];
1464
1465        // determine sort ordering
1466        switch ($_SESSION['plogger_sortby']){
1467                case 'number_of_comments':
1468                        $sql = "SELECT
1469                        `p`.`id`,
1470                        COUNT(`comment`) AS `num_comments`
1471                        FROM `".TABLE_PREFIX."pictures` AS `p`
1472                        LEFT JOIN `".TABLE_PREFIX."comments` AS `c` ON `p`.`id`=`c`.`parent_id`
1473                        WHERE `parent_album`=".$row["parent_album"]."
1474                        GROUP BY `p`.`id`
1475                        ORDER BY `num_comments` ";
1476                break;
1477                case 'caption':
1478                        $sql .= " ORDER BY `caption` ";
1479                break;
1480                case 'date_taken':
1481                        $sql .= " ORDER BY `EXIF_date_taken` ";
1482                break;
1483                case 'filename':
1484                        $sql .= " ORDER BY `path` ";
1485                break;
1486                case 'date':
1487                        $sql .= " ORDER BY `date_submitted` ";
1488                break;
1489                default:
1490                $sql .= " ORDER BY `id` ";
1491                break;
1492        }
1493
1494        switch (strtoupper($_SESSION["plogger_sortdir"])){
1495                case 'ASC':
1496                        $sql .= " ASC";
1497                break;
1498                case 'DESC':
1499                        default:
1500                        $sql .= " DESC";
1501                break;
1502        }
1503
1504        // if sorting by number_of_comments, we sub-sort by 'id' DESC
1505        if ($_SESSION['plogger_sortby']=="number_of_comments"){
1506                $sql .= ",`p`.`id` DESC";
1507        }
1508
1509        $result = run_query($sql);
1510
1511        while ($image = mysql_fetch_assoc($result)) {
1512                $image_list[] = $image["id"];
1513        }
1514
1515        $GLOBALS["image_list"] = $image_list;
1516
1517        // first lets load the thumbnail of the picture at the correct size
1518        $sql = "SELECT *,
1519        UNIX_TIMESTAMP(`date_submitted`) AS `unix_date_submitted`,
1520        UNIX_TIMESTAMP(`EXIF_date_taken`) AS `unix_exif_date_taken`
1521        FROM `".TABLE_PREFIX."pictures`
1522        WHERE `id`=$id";
1523        $result = run_query($sql);
1524
1525        $GLOBALS["available_pictures"] = mysql_num_rows($result);
1526        $GLOBALS["picture_counter"] = 0;
1527        $GLOBALS["picture_dbh"] = $result;
1528
1529        // lets load up the comments for the current picture here as well
1530        $query = "SELECT *,
1531        UNIX_TIMESTAMP(`date`) AS `unix_date`
1532        FROM `".TABLE_PREFIX."comments`
1533        WHERE `parent_id`=".intval($id)."
1534        AND `approved`=1";
1535        $result = run_query($query) or die(mysql_error());
1536
1537        $GLOBALS["available_comments"] = mysql_num_rows($result);
1538        $GLOBALS["comment_counter"] = 0;
1539        $GLOBALS["comment_dbh"] = $result;
1540}
1541
1542// arr['type'] id|album|collection|latest
1543// arr['value'] - argument to
1544
1545// arr['sortby'] - what field is used for sorting
1546// arr['sortdir'] - asc|desc
1547
1548// arr['from'] - where to start in the result set- default to 0
1549// arr['limit'] - how many pictures to return
1550
1551function plogger_init_pictures($arr) {
1552        $sql = " FROM `".TABLE_PREFIX."pictures` AS `p`";
1553
1554        // if sorting by number_of_comments, we need to join the comments table
1555        if (isset($arr['sortby']) && $arr['sortby']=="number_of_comments"){
1556                $sql .= " LEFT JOIN `".TABLE_PREFIX."comments` AS `c` ON `p`.`id`=`c`.`parent_id`";
1557        }
1558
1559        // right now only single id is supported, maybe I want to specify multiple id-s as well
1560        $value = (isset($arr['value']) && $arr['value'] > 0) ? $arr['value'] : -1;
1561
1562        switch ($arr['type']) {
1563                case 'collection':
1564                        $sql .= " WHERE `p`.`parent_collection` = ".$value;
1565                break;
1566                case 'album':
1567                        $sql .= " WHERE `p`.`parent_album` = ".$value;
1568                break;
1569                case 'id':
1570                        $sql .= " WHERE `p`.`id` = ".$value;
1571                break;
1572                case 'latest':
1573                break;
1574                default:
1575                return 0;
1576        }
1577
1578        $result = run_query("SELECT COUNT(DISTINCT p.`id`) AS cnt " . $sql);
1579        $row = mysql_fetch_assoc($result);
1580
1581        $GLOBALS["total_pictures"] = $row["cnt"];
1582
1583        // if sorting by number_of_comments, we need group and count the results
1584        if (isset($arr['sortby']) && $arr['sortby']=="number_of_comments"){
1585                $sql = ",COUNT(`comment`) AS `num_comments` ".$sql." GROUP BY `p`.`id`";
1586        }
1587
1588        // query database and retrieve all pictures within selected album
1589        // and what about searching? well, what about it ..
1590        $sort_fields = array(
1591        'number_of_comments' => 'num_comments',
1592        'caption' => 'caption',
1593        'date_taken' => 'EXIF_date_taken',
1594        'filename' => 'path',
1595        'date' => 'date_submitted',
1596        'id' => 'id'
1597        );
1598
1599        if (isset($arr['sortby']) && isset($sort_fields[$arr['sortby']])) {
1600                $sortby = $arr['sortby'];
1601        } else {
1602                $sortby = "id";      // Default sort, is to sort by id
1603        }
1604
1605        $sortby = $sort_fields[$sortby];
1606        $sql .= " ORDER BY `".$sortby."`";
1607
1608        // determine sort direction
1609        if (isset($arr['sortdir']) && (strtoupper($arr['sortdir']) == 'ASC')) {
1610                $sortdir = " ASC";
1611        } else {
1612                $sortdir = " DESC";    // Default sort direction is descending
1613        }
1614        $sql .= $sortdir;
1615
1616        // if sorting by number_of_comments, we sub-sort by 'id' DESC
1617        if (isset($arr['sortby']) && $arr['sortby']=="number_of_comments"){
1618                $sql .= ",`p`.`id` DESC";
1619        }
1620
1621        // set up limits, if needed
1622        $from = 0;
1623        $limit = 20;        // Default limit if nothing is set
1624        $max_limit = 1000;  // Hard-coded max limit, no matter what you do you cannot
1625        // go beyond this limit for number of pictures in thumbnails,
1626        // slideshow etc.
1627
1628        if (isset($arr['from']) && $arr['from'] > 0) {
1629                $from = $arr['from'];
1630        }
1631
1632        // Enforce hard-coded max limit if the provided limit is extreme
1633        if (isset($arr['limit']) && $arr['limit'] > 0 && $arr['limit'] <= $max_limit) {
1634                $limit = $arr['limit'];
1635        } elseif (isset($arr['limit']) && $arr['limit'] == -1) {
1636                $limit = -1;      // Set to -1 for slideshow, so limit will not be used
1637        }
1638
1639        if (($from >= 0) && ($limit >= 0)) $sql .= " LIMIT ".$from.",".$limit;
1640
1641        $result = run_query("SELECT p.*,
1642        UNIX_TIMESTAMP(`date_submitted`) AS `unix_date_submitted`,
1643        UNIX_TIMESTAMP(`EXIF_date_taken`) AS `unix_exif_date_taken` " . $sql);
1644
1645        $GLOBALS["available_pictures"] = mysql_num_rows($result);
1646        $GLOBALS["picture_counter"] = 0;
1647        $GLOBALS["picture_dbh"] = $result;
1648}
1649
1650// arr['searchterms'] - what to search for, space separates different serach terms
1651// arr['from'] - where to start in the result set, default 0
1652// arr['limit'] - and how many items to return, default 20
1653// arr['sortby'] -
1654// arr['sortdir'] -
1655
1656function plogger_init_search($arr) {
1657        global $PLOGGER_DBH;
1658        $terms = explode(" ",$arr['searchterms']);
1659        $from = 0;
1660        $limit = 20;
1661
1662        if (isset($arr['from']) && $arr['from'] > 0) {
1663                $from = $arr['from'];
1664        }
1665
1666        // enforce hard-coded max limit
1667        if (isset($arr['limit']) && $arr['limit'] > 0 && $arr['limit'] <= 100) {
1668                $limit = $arr['limit'];
1669        }
1670
1671        $query = " FROM `".TABLE_PREFIX."pictures` p LEFT JOIN `".TABLE_PREFIX."comments` c
1672        ON p.`id` = c.`parent_id` ";
1673
1674        if ((count($terms) != 1) || ($terms[0] != '')){
1675                $query .= " WHERE ( ";
1676                foreach ($terms as $term) {
1677                        $term = mysql_real_escape_string($term);
1678                        $multi_term = explode("+",$term);
1679                        if (count($multi_term)>1) {
1680                                $path = implode("%' AND `path` LIKE '%",$multi_term);
1681                                $description = implode("%' AND `description` LIKE '%",$multi_term);
1682                                $comment = implode("%' AND `comment` LIKE '%",$multi_term);
1683                                $caption = implode("%' AND `caption` LIKE '%",$multi_term);
1684                        } else {
1685                                $path = $description = $comment = $caption = $term;
1686                        }
1687                        $query .= "
1688                        `path` LIKE '%$path%' OR
1689                        `description` LIKE '%$description%' OR
1690                        `comment` LIKE '%$comment%' OR
1691                        `caption` LIKE '%$caption%' OR ";
1692                }
1693
1694                $query = substr($query, 0, strlen($query) - 3) .") ";
1695        } else {
1696                // no search terms? no results either
1697                $query .= " WHERE 1 = 0";
1698        }
1699
1700        $sort_fields = array('date_submitted','id');
1701        $sortby = 'date_submitted';
1702
1703        if (isset($arr['sortby']) && in_array($arr['sortby'],$sort_fields)) {
1704                $sortby = $arr['sortby'];
1705        }
1706
1707        $sortdir = ' DESC';
1708
1709        if (isset($arr['sortdir']) && 'asc' == $arr['sortdir']) {
1710                $sortdir = ' ASC';
1711        }
1712
1713        $result = run_query("SELECT COUNT(DISTINCT p.`id`) AS cnt " . $query);
1714        $row = mysql_fetch_assoc($result);
1715
1716        $GLOBALS["total_pictures"] = $row["cnt"];
1717        // and I need sort order here as well
1718        // from and limit too
1719        $result = run_query("SELECT `caption`,`path`,p.`id`,c.`comment`,
1720        UNIX_TIMESTAMP(`date_submitted`) AS `unix_date_submitted` ".$query .
1721        " GROUP BY p.`id` ORDER BY `$sortby` $sortdir LIMIT $from,$limit");
1722
1723        $GLOBALS["available_pictures"] = mysql_num_rows($result);
1724        $GLOBALS["picture_counter"] = 0;
1725        $GLOBALS["picture_dbh"] = $result;
1726}
1727
1728function plogger_init_collections($arr) {
1729        $sql = "SELECT COUNT(DISTINCT `parent_collection`) AS `num_items`
1730        FROM `".TABLE_PREFIX."pictures`";
1731        $result = run_query($sql);
1732        $num_items = mysql_result($result, 'num_items');
1733        $GLOBALS["total_pictures"] = $num_items;
1734
1735        // create a list of all non-empty collections. Could be done with subqueries, but
1736        // MySQL 4.0 does not support those
1737
1738        // -1 is just for the case there are no images within collections or albums at all
1739        $image_collection_count = array(-1 => -1);
1740        $image_album_count = array(-1 => -1);
1741        $album_count = array();
1742
1743        // 1. create a list of all albums with at least one photo
1744        $sql = "SELECT parent_collection,parent_album,COUNT(*) AS imagecount
1745        FROM `".TABLE_PREFIX."pictures` GROUP BY parent_collection,parent_album";
1746        $result = run_query($sql);
1747        while($row = mysql_fetch_assoc($result)) {
1748                $image_collection_count[$row["parent_collection"]] = $row["imagecount"];
1749                $image_album_count[$row["parent_album"]] = $row["imagecount"];
1750        }
1751        $imlist = join(",",array_keys($image_collection_count));
1752        $albumlist = join(",",array_keys($image_album_count));
1753
1754        $cond = '';
1755
1756        if (empty($arr['all_collections'])) {
1757                $cond = " WHERE `parent_id` IN ($imlist) AND `id` IN ($albumlist) ";
1758        }
1759
1760        $sql = "SELECT parent_id,COUNT(*) AS albumcount
1761        FROM `".TABLE_PREFIX."albums`
1762        $cond
1763        GROUP BY parent_id";
1764
1765        $result = run_query($sql);
1766        while($row = mysql_fetch_assoc($result)) {
1767                $album_count[$row["parent_id"]] = $row["albumcount"];
1768        }
1769
1770        $GLOBALS["album_count"] = $album_count;
1771
1772        // I need to determine correct arguments for LIMIT from the given page number
1773        $from = $arr['from'];
1774        $lim = $arr['limit'];
1775
1776        $cond = "";
1777
1778        // by default only collections with pictures are returned
1779        // override that with passing all_collections as an argument to
1780        // this function
1781        if (empty($arr['all_collections'])) {
1782                $cond = " WHERE `id` IN ($imlist) ";
1783        }
1784
1785        $ordering = " ORDER BY $arr[sortby] $arr[sortdir] ";
1786
1787        $sql = "SELECT * FROM `".TABLE_PREFIX."collections`
1788        $cond $ordering LIMIT $from,$lim";
1789        $result = run_query($sql);
1790
1791        $GLOBALS["available_collections"] = mysql_num_rows($result);
1792        $GLOBALS["collection_counter"] = 0;
1793        $GLOBALS["collection_dbh"] = $result;
1794}
1795
1796function plogger_init_albums($arr) {
1797        $collection_id = intval($arr['collection_id']);
1798        $sql = "SELECT COUNT(DISTINCT `parent_album`) AS `num_items`
1799        FROM `".TABLE_PREFIX."pictures`
1800        WHERE `parent_collection` = '" . $collection_id . "'";
1801
1802        $result = run_query($sql);
1803        $num_items = mysql_result($result, 'num_items');
1804
1805        $GLOBALS["total_pictures"] = $num_items;
1806
1807        // create a list of all non-empty albums. Could be done with subqueries, but
1808        // MySQL 4.0 does not support those
1809
1810        // -1 is just for the case there are no albums at all. Shouldn't happen if user
1811        // follows links, but let's deal with it anyway
1812        $image_count = array(-1 => -1);
1813        // 1. create a list of all albums with at least one photo
1814        $sql = "SELECT parent_album,COUNT(*) AS imagecount FROM `".TABLE_PREFIX."pictures` GROUP BY parent_album";
1815        $result = run_query($sql);
1816        while($row = mysql_fetch_assoc($result)) {
1817                $image_count[$row["parent_album"]] = $row["imagecount"];
1818        }
1819
1820        $imlist = join(",",array_keys($image_count));
1821
1822        $from = intval($arr['from']);
1823        $lim = intval($arr['limit']);
1824
1825        $cond = '';
1826        if (empty($arr['all_albums'])) {
1827                $cond = " AND id IN ($imlist) ";
1828        }
1829
1830        $order = " ORDER BY $arr[sortby] $arr[sortdir] ";
1831
1832        $sql = "SELECT * FROM `".TABLE_PREFIX."albums`
1833        WHERE `parent_id` = '$collection_id' $cond $order LIMIT $from,$lim";
1834
1835        $result = run_query($sql);
1836        $GLOBALS["available_albums"] = mysql_num_rows($result);
1837        $GLOBALS["album_counter"] = 0;
1838        $GLOBALS["album_dbh"] = $result;
1839
1840}
1841
1842function plogger_get_header() {
1843        global $config;
1844        if (file_exists(THEME_DIR . '/header.php')) {
1845                include(THEME_DIR . '/header.php');
1846        } else {
1847                include($config['basedir'] . 'plog-content/themes/default/header.php');
1848        }
1849}
1850
1851function plogger_get_footer() {
1852        global $config;
1853        if (file_exists(THEME_DIR . '/footer.php')) {
1854                include(THEME_DIR . '/footer.php');
1855        } else {
1856                include($config['basedir'] . 'plog-content/themes/default/footer.php');
1857        }
1858}
1859
1860function plogger_download_selected_button() {
1861        global $config;
1862
1863        if ($GLOBALS['plogger_level'] != "picture" && $GLOBALS['plogger_mode'] != "slideshow" && $config["allow_dl"]) {
1864                return '<input id="download_selected_button" class="submit" type="submit" name="download_selected" value="' . plog_tr('Download Selected') . '" />';
1865        }
1866}
1867
1868function plogger_download_selected_form_start() {
1869        global $config;
1870
1871        if ($GLOBALS['plogger_level'] != "picture" && $GLOBALS['plogger_mode'] != "slideshow" && $config["allow_dl"]) {
1872                return "\n\t" . '<form action="'.$config['gallery_url'].'plog-download.php" method="post">
1873        <p><input type="hidden" name="dl_type" value="'.$GLOBALS['plogger_level'].'" /></p>' . "\n";
1874        }
1875
1876}
1877
1878function plogger_download_selected_form_end() {
1879        global $config;
1880
1881        if ($GLOBALS['plogger_level'] != "picture" && $GLOBALS['plogger_mode'] != "slideshow" && $config["allow_dl"]) {
1882                return "\n\t" . '</form><!-- /download form -->' . "\n";
1883        }
1884
1885}
1886
1887function plogger_print_button() {
1888        global $config;
1889        $id = $GLOBALS["plogger_id"];
1890
1891        if ($GLOBALS['plogger_level'] == "picture" && $config["allow_print"]) {
1892                return '<a class="print" href="' . $config["gallery_url"] . 'plog-print.php?id='.$id.'">' . plog_tr('Print Image') . '</a>';
1893        }
1894}
1895
1896function plogger_link_back() {
1897        return  "\t" . '<div id="link-back"><a title="' . plog_tr('Powered by Plogger') . '" href="http://www.plogger.org/">' . plog_tr('Powered by Plogger') . '</a></div>' . "\n";
1898}
1899
1900function plogger_rss_link() {
1901        global $config;
1902        $rss_link = '';
1903
1904        if ($config["use_mod_rewrite"]) {
1905                global $path;
1906                if (!empty($path))
1907                $rss_link .= "http://".$_SERVER['HTTP_HOST']."/".SmartStripSlashes(substr($path,1))."feed/";
1908                else
1909                $rss_link .= $config['gallery_url']."feed/";
1910
1911        }
1912        else {
1913                $rss_link .= $config["gallery_url"] . "plog-rss.php?level=".$GLOBALS['plogger_level']."&amp;id=".$GLOBALS['plogger_id'];
1914        }
1915
1916        // append the search terms
1917        if ($GLOBALS['plogger_level'] == "search") {
1918                $separator = $config["use_mod_rewrite"] ? "?" : "&amp;";
1919                $rss_link .= $separator . "searchterms=".htmlspecialchars($_GET["searchterms"]);
1920        }
1921
1922        return $rss_link;
1923}
1924
1925function plogger_rss_feed_button() {
1926        global $config;
1927
1928        if ($GLOBALS['plogger_mode'] != "slideshow" && $GLOBALS['plogger_level'] != "picture") {
1929                $rss_link = "";
1930                // change the tooltip message to reflect the nature of the RSS aggregate link.
1931                if ($GLOBALS['plogger_level'] != "")
1932                $rss_tooltip = plog_tr('RSS 2.0 subscribe to') . ': ' . $GLOBALS["plogger_level"];
1933                else
1934                $rss_tooltip = plog_tr('RSS 2.0 subscribe to all images');
1935
1936                $rss_link = plogger_rss_link();
1937
1938                $rss_tag = '<a href="'.$rss_link.'"><img id="rss-image" src="' . $config["gallery_url"] . 'plog-admin/images/rss.gif" title="'.$rss_tooltip.'" alt="' . plog_tr('RSS 2.0 Feed') . '" /></a>';
1939
1940                return $rss_tag;
1941        }
1942
1943}
1944
1945function plogger_slideshow_link() {
1946        global $config;
1947
1948        $id = $GLOBALS["plogger_id"];
1949        $ss_tag = "";
1950        if ($GLOBALS['plogger_mode'] != "slideshow") {
1951                if ($GLOBALS['plogger_level'] == "album") {
1952                        $ss_url = generate_url('album',$GLOBALS['plogger_id'],array('mode' => 'slideshow'));
1953                        $ss_tag = "<a href=\"".$ss_url."\">" . plog_tr('View as Slideshow') . "</a>";
1954                }
1955
1956                if ($GLOBALS['plogger_level'] == "search") {
1957                        $ss_url = generate_url("search", -1, array('searchterms'=>urlencode($_GET["searchterms"])));
1958                        $ss_url .= "&amp;mode=slideshow";
1959                        $ss_tag = "<a href=\"".$ss_url."\">" . plog_tr('View as Slideshow') . "</a>";
1960                }
1961
1962        }
1963        return $ss_tag;
1964}
1965
1966function plogger_sort_control() {
1967
1968        if ($GLOBALS['plogger_mode'] != 'slideshow')
1969        return generate_sortby($GLOBALS['plogger_level'],$GLOBALS['plogger_id']).generate_sortdir($GLOBALS['plogger_level'],$GLOBALS['plogger_id']);
1970
1971}
1972
1973function plogger_pagination_control() {
1974        global $config;
1975
1976        if ($GLOBALS['plogger_mode'] != 'slideshow') {
1977                $page = isset($_GET["plog_page"]) ? intval($_GET["plog_page"]) : 1;
1978
1979                if ($GLOBALS['plogger_level'] == "search") {
1980                        $p_url = generate_url("search", -1, array('searchterms'=>urlencode($_GET["searchterms"])));
1981                }
1982                else {
1983                        if ($GLOBALS['plogger_level']) {
1984                                $p_url = generate_url($GLOBALS['plogger_level'], $GLOBALS['plogger_id']);
1985                        } else {
1986                                $p_url = generate_url("collections");
1987                        }
1988                }
1989
1990                switch($GLOBALS['plogger_level']) {
1991                        case 'search':
1992                                $num_items = $GLOBALS["total_pictures"];
1993                        break;
1994
1995                        case 'album':
1996                                $num_items = plogger_album_picture_count();
1997                        break;
1998
1999                        case 'collection':
2000                                $num_items = plogger_collection_album_count();
2001                        break;
2002
2003                        default:
2004                        $num_items = plogger_count_collections();
2005                        break;
2006                }
2007
2008                return generate_pagination($p_url, $page, $num_items, $config["thumb_num"]);
2009        }
2010
2011}
2012
2013// generate meta tag keywords and descriptions for SEO
2014function plogger_generate_seo_meta_tags() {
2015        global $config;
2016
2017switch($GLOBALS['plogger_level']){
2018        case "collection":
2019                $collection_info = get_collection_by_id($GLOBALS['plogger_id']);
2020                $keyword = $collection_info['name'];
2021                $description = $collection_info['description'];
2022                break;
2023
2024        case "album":
2025                $album_info = get_album_by_id($GLOBALS['plogger_id']);
2026                $keyword = $album_info['name'];
2027                $description = $album_info['description'];
2028                break;
2029
2030        case "picture":
2031                $picture_info = get_picture_by_id($GLOBALS['plogger_id']);
2032                        if (!empty($picture_info['caption'])){
2033                        $keyword = $picture_info['caption'];
2034                } else {
2035                        $keyword = basename($picture_info['path']);
2036                }
2037                $description = $picture_info['description'];
2038                break;
2039
2040        case "search":
2041                $keyword = isset($_GET['searchterms']) ? str_replace(" ", ",", $_GET['searchterms']) : '';
2042                $description = "Search results from my Plogger gallery";
2043                break;
2044
2045        default:
2046                $keyword = SmartStripSlashes($config['gallery_name']); // used on gallery entry page
2047                $description = "This is my Plogger gallery"; // used on gallery entry page
2048                break;
2049        }
2050
2051        return "\t<meta name=\"keywords\" content=\"$keyword\" />
2052        <meta name=\"description\" content=\"$description\" />\n";
2053}
2054
2055/*** The following functions can only be used inside the Picture loop ***/
2056function plogger_has_pictures() {
2057        return $GLOBALS["picture_counter"] < $GLOBALS["available_pictures"];
2058}
2059
2060function plogger_comments_on() {
2061        global $config;
2062        return $config["allow_comments"];
2063}
2064
2065function plogger_picture_allows_comments() {
2066
2067        $picture = $GLOBALS["current_picture"];
2068        $id = $picture["id"];
2069        return $picture["allow_comments"];
2070}
2071
2072function plogger_load_picture() {
2073        $rv = mysql_fetch_assoc($GLOBALS["picture_dbh"]);
2074        $GLOBALS["picture_counter"]++;
2075        $GLOBALS["current_picture"] = $rv;
2076        return $rv;
2077}
2078
2079function plogger_picture_has_comments() {
2080        return $GLOBALS["comment_counter"] < $GLOBALS["available_comments"];
2081}
2082
2083function plogger_load_comment() {
2084        $rv = mysql_fetch_assoc($GLOBALS["comment_dbh"]);
2085        $GLOBALS["comment_counter"]++;
2086        $GLOBALS["current_comment"] = $rv;
2087        return $rv;
2088}
2089
2090function plogger_get_comment_date($format = false) {
2091        global $config;
2092        $comment = $GLOBALS["current_comment"];
2093        if (empty($format)) {
2094                $format = $config["date_format"];
2095        }
2096        return date($format,$comment["unix_date"]);
2097}
2098
2099function plogger_get_comment_id() {
2100        $comment = $GLOBALS["current_comment"];
2101        return $comment["id"];
2102}
2103
2104function plogger_get_comment_email() {
2105        $comment = $GLOBALS["current_comment"];
2106        return $comment["email"];
2107}
2108
2109function plogger_get_comment_url() {
2110        $comment = $GLOBALS["current_comment"];
2111        return htmlspecialchars($comment["url"]);
2112}
2113
2114function plogger_get_comment_author() {
2115        $comment = $GLOBALS["current_comment"];
2116        return htmlspecialchars(SmartStripSlashes($comment["author"]));
2117}
2118
2119function plogger_get_comment_text() {
2120        $comment = $GLOBALS["current_comment"];
2121        return htmlspecialchars(SmartStripSlashes($comment["comment"]));
2122}
2123
2124function plogger_comment_post_error() {
2125        if (isset($_SESSION['comment_post_error'])) {
2126                unset($_SESSION['comment_post_error']);
2127                return 1;
2128        }
2129        else
2130        return 0;
2131}
2132
2133function plogger_comment_moderated() {
2134        if (isset($_SESSION['comment_moderated'])) {
2135                unset($_SESSION['comment_moderated']);
2136                return 1;
2137        }
2138        else
2139        return 0;
2140}
2141
2142function plogger_get_picture_url() {
2143        $row = $GLOBALS["current_picture"];
2144        return generate_url("picture",$row["id"]);
2145}
2146
2147function plogger_get_picture_id() {
2148        $row = $GLOBALS["current_picture"];
2149        return $row["id"];
2150}
2151
2152function plogger_get_picture_thumb($type = THUMB_SMALL) {
2153        $pic = $GLOBALS["current_picture"];
2154        return generate_thumb($pic['path'], $pic['id'], $type);
2155}
2156
2157function plogger_get_picture_caption() {
2158        if (!empty($GLOBALS["current_picture"]["caption"]))
2159        return SmartStripSlashes($GLOBALS["current_picture"]["caption"]);
2160        else
2161        return "&nbsp;";
2162}
2163
2164function plogger_get_thumbnail_info() {
2165        global $thumbnail_config;
2166        global $config;
2167
2168        $thumb_config = $thumbnail_config[THUMB_SMALL];
2169
2170        $base_filename = sanitize_filename(basename($GLOBALS["current_picture"]["path"]));
2171        $prefix = $thumb_config['filename_prefix'] . $GLOBALS["current_picture"]["id"] . "-";
2172
2173        $thumbpath = $config['basedir'] . 'plog-content/thumbs/'.$prefix.$base_filename;
2174
2175        if (!is_readable($thumbpath)) {
2176                // again, do we want to support video in the Plogger core?
2177                // $image_info = getimagesize($config['basedir'] . 'plog-graphics/thumb-video.gif');
2178                return false;
2179        }       else {
2180                $image_info = getimagesize($thumbpath);
2181        }
2182
2183        // $image_info = getimagesize($thumbpath);
2184
2185        return $image_info;
2186}
2187
2188function plogger_get_source_picture_path() {
2189        global $config;
2190        return $config['basedir'].'plog-content/images/'.SmartStripSlashes($GLOBALS["current_picture"]["path"]);
2191}
2192
2193function plogger_get_picture_description() {
2194        if (isset($GLOBALS['current_picture']['description'])){
2195                return SmartStripSlashes($GLOBALS['current_picture']['description']);
2196        } else {
2197                return false;
2198        }
2199}
2200
2201function plogger_picture_comment_count() {
2202        $row = $GLOBALS["current_picture"];
2203        $comment_query = "SELECT COUNT(*) AS `num_comments` FROM `".TABLE_PREFIX."comments`
2204        WHERE approved = 1 AND `parent_id`='".$row["id"]."'";
2205
2206        $comment_result = run_query($comment_query);
2207        $num_comments = mysql_result($comment_result, 0, 'num_comments');
2208        return $num_comments;
2209}
2210
2211function plogger_get_picture_date($format = '',$submitted = 0) {
2212        global $config;
2213        $row = $GLOBALS['current_picture'];
2214        if ($submitted) {
2215                $date_taken = $row['unix_date_submitted'];
2216        }
2217        else {
2218                $date_taken = !empty($row['unix_exif_date_taken']) ? $row['unix_exif_date_taken'] : $row['unix_date_submitted'];
2219        }
2220        if (!$format) {
2221                $format = $config['date_format'];
2222        }
2223        return translate_date(date($format,$date_taken));
2224}
2225
2226function translate_date($date) {
2227
2228        $fields = array(
2229        'January' => plog_tr('January'),
2230        'February' => plog_tr('February'),
2231        'March' => plog_tr('March'),
2232        'April' => plog_tr('April'),
2233        'May' => plog_tr('May'),
2234        'June' => plog_tr('June'),
2235        'July' => plog_tr('July'),
2236        'August' => plog_tr('August'),
2237        'September' => plog_tr('September'),
2238        'October' => plog_tr('October'),
2239        'November' => plog_tr('November'),
2240        'December' => plog_tr('December'),
2241
2242        'Monday' => plog_tr('Monday'),
2243        'Tuesday' => plog_tr('Tuesday'),
2244        'Wednesday' => plog_tr('Wednesday'),
2245        'Thursday' => plog_tr('Thursday'),
2246        'Friday' => plog_tr('Friday'),
2247        'Saturday' => plog_tr('Saturday'),
2248        'Sunday' => plog_tr('Sunday'),
2249        );
2250
2251        // Replace english month and day
2252        foreach($fields as$fkey => $fval) {
2253                $date = str_replace($fkey, $fval, $date);
2254        }
2255
2256        // Replace english month and day using first three letters abbreviation
2257        foreach($fields as$fkey => $fval) {
2258                $date = str_replace(substr($fkey,0,3), substr($fval,0,3), $date);
2259        }
2260
2261        return plog_tr($date);
2262}
2263
2264function plogger_get_source_picture_url() {
2265        global $config;
2266        return (!empty($config['allow_fullpic'])) ? $config['gallery_url'].'plog-content/images/'.SmartStripSlashes($GLOBALS["current_picture"]["path"]) : "#";
2267}
2268
2269/**
2270* @author derek@plogger.org
2271* @return string html list of thumbnails
2272*/
2273function plogger_get_thumbnail_nav() {
2274        global $config;
2275        if(empty($config["enable_thumb_nav"])) return ''; // return if thumbnail nav turned off
2276        $image_list = $GLOBALS["image_list"];
2277        $array_length = count($image_list); // store array length
2278        $curr = $GLOBALS["current_picture"];
2279        $pos_array = array_keys($image_list,$curr["id"]);
2280        $curr_pos = $pos_array[0];
2281        $range = (isset($config["thumb_nav_range"])) ? $config["thumb_nav_range"] : 0;
2282        // if length is 0, use all thumbs
2283        if($range == 0) {
2284                // get_picture_by_id modified to take arrays, so pass the whole array
2285                $thumb_pic_array = get_picture_by_id($image_list);
2286                // else, add a thumb each side of current for each value of $config['thumb_nav_range']
2287        } else {
2288                $thumb_nav_array = array($curr['id']);
2289                for($i=1;$i<$range+1;$i++) {
2290                        // use unshift() to add to the beginning, push() to add to the end
2291                        // check that we haven't run out of images below: that (current image - offset) >= 0
2292                        if($curr_pos - $i >= 0) {
2293                                unset($newpic); // php has problems with reassigning via iteration
2294                                $newpic = $image_list[$curr_pos-$i];
2295                                if(!empty($newpic)) array_unshift($thumb_nav_array, $newpic);
2296                                // grab from the end of the array
2297                        } else {
2298                                unset($newpic); // php has problems with reassigning via iteration
2299                                $newpic = $image_list[$array_length+($curr_pos-$i)]; // adding a negative value, don't fret
2300                                if(!empty($newpic)) array_unshift($thumb_nav_array, $newpic);
2301                        }
2302                        // check that we haven't run out of images above: (current image + offset) <= (total images)
2303                        if($array_length-1 >= ($curr_pos+$i)) {
2304                                unset($newpic); // php has problems with reassigning via iteration
2305                                $newpic = $image_list[$curr_pos+$i];
2306                                if(!empty($newpic)) array_push($thumb_nav_array, $newpic);
2307                                // grab from the beginning of the array
2308                        } else {
2309                                unset($newpic); // php has problems with reassigning via iteration
2310                                $newpic = $image_list[($curr_pos + $i) - ($array_length)];
2311                                if(!empty($newpic)) array_push($thumb_nav_array, $newpic);
2312                        }
2313                }
2314                $thumb_pic_array = array();
2315                foreach($thumb_nav_array as $thumb_nav_value) {
2316
2317                        array_push($thumb_pic_array,get_picture_by_id($thumb_nav_value));
2318                }
2319        }
2320        return plogger_format_thumb_nav($thumb_pic_array);
2321}
2322
2323function plogger_format_thumb_nav($thumb_nav_array) {
2324        $thumb_nav_out = "\t\t\t<ul id=\"thumb-nav\">\n";
2325        foreach($thumb_nav_array as $current_thumb_nav) {
2326                unset($title);unset($img_path);unset($class);unset($link); // php has problems with reassigning via iteration
2327                $title = (!empty($current_thumb_nav["caption"])) ? $current_thumb_nav["caption"] : "";
2328                $img_path = generate_thumb($current_thumb_nav["path"], $current_thumb_nav['id'], THUMB_NAV);
2329                $class = ($current_thumb_nav["id"] == $GLOBALS["current_picture"]["id"]) ? "current" : "";
2330                $link = generate_url("picture",$current_thumb_nav["id"]);
2331                $thumb_nav_out .= plogger_thumb_nav_item($link, $title, $img_path, $class);
2332        }
2333        $thumb_nav_out .= "\t\t\t</ul>\n";
2334        return $thumb_nav_out;
2335}
2336
2337function plogger_thumb_nav_item($link, $title, $img_path, $class = '') {
2338        return "\t\t\t\t<li class=\"".$class."\"><a href=\"".$link."\" title=\"".$title."\"><img src=\"".$img_path."\" alt=\"".$title."\" /></a></li>\n";
2339}
2340
2341function plogger_get_next_picture_url() {
2342        $image_list = $GLOBALS["image_list"];
2343        $row = $GLOBALS["current_picture"];
2344        $current_picture = array_search($row['id'],$image_list);
2345        $next_link = '';
2346        if ($current_picture < sizeof($image_list)-1)
2347        {
2348                $next_link = generate_url("picture",$image_list[$current_picture+1]);
2349        }
2350        return $next_link;
2351}
2352
2353function plogger_get_prev_picture_url() {
2354        $image_list = $GLOBALS["image_list"];
2355        $row = $GLOBALS["current_picture"];
2356        $current_picture = array_search($row['id'],$image_list);
2357        $prev_link = '';
2358        if ($current_picture > 0) {
2359                $prev_link = generate_url("picture",$image_list[$current_picture-1]);
2360        }
2361        return $prev_link;
2362}
2363/*** End of Picture loop functions ***/
2364
2365/*** The following functions can only be used inside the Collections loop ***/
2366function plogger_load_collection() {
2367        $rv = mysql_fetch_assoc($GLOBALS["collection_dbh"]);
2368        $GLOBALS["collection_counter"]++;
2369        $GLOBALS["current_collection"] = $rv;
2370        return $rv;
2371}
2372
2373function plogger_has_collections() {
2374        return $GLOBALS["collection_counter"] < $GLOBALS["available_collections"];
2375}
2376
2377function plogger_get_collection_url() {
2378        $row = $GLOBALS["current_collection"];
2379        return generate_url("collection",$row["id"]);
2380}
2381
2382function plogger_get_collection_thumb() {
2383        $rv = $GLOBALS["current_collection"];
2384        // figure out the thumbnail as well
2385        $thumb_query = "SELECT * FROM `".TABLE_PREFIX."pictures` WHERE ";
2386        if ($rv["thumbnail_id"] > 0)
2387        $thumb_query .= " `id`=".$rv["thumbnail_id"];
2388        else
2389        $thumb_query .= " `parent_collection`='".$rv["id"]."' ORDER BY `id` DESC LIMIT 1";
2390
2391        $thumb_result = run_query($thumb_query);
2392        $thumb_data = mysql_fetch_assoc($thumb_result);
2393        if ($thumb_data) {
2394                $rv["thumbnail_id"] = $thumb_data["id"];
2395                $rv["thumbnail_path"] = $thumb_data["path"];
2396        }
2397        return generate_thumb($rv['thumbnail_path'], $rv['thumbnail_id'], THUMB_SMALL);
2398}
2399
2400function plogger_collection_album_count() {
2401        if (isset($GLOBALS['album_count']) && isset($GLOBALS['current_collection']['id']) && isset($GLOBALS['album_count'][$GLOBALS['current_collection']['id']])) {
2402                return $GLOBALS['album_count'][$GLOBALS['current_collection']['id']];
2403        } else {
2404                return 0;
2405        }
2406}
2407
2408function plogger_get_collection_description() {
2409        return htmlspecialchars(SmartStripSlashes($GLOBALS["current_collection"]["description"]));
2410}
2411
2412function plogger_get_collection_name() {
2413        return htmlspecialchars(SmartStripSlashes($GLOBALS["current_collection"]["name"]));
2414}
2415
2416function plogger_get_collection_id() {
2417        return $GLOBALS["current_collection"]["id"];
2418}
2419
2420function plogger_count_collections() {
2421
2422        $numquery = "SELECT COUNT(DISTINCT `parent_collection`) AS `num_collections` FROM `".TABLE_PREFIX."pictures`";
2423
2424        $numresult = run_query($numquery);
2425        $num_albums = mysql_result($numresult, 'num_collections');
2426        return $num_albums;
2427}
2428
2429/*** End of Collection loop functions ***/
2430
2431/*** The following functions can only be used inside the Albums loop ***/
2432function plogger_load_album() {
2433        $rv = mysql_fetch_assoc($GLOBALS["album_dbh"]);
2434        $GLOBALS["album_counter"]++;
2435        $GLOBALS["current_album"] = $rv;
2436        return $rv;
2437}
2438
2439function plogger_has_albums() {
2440        return $GLOBALS["album_counter"] < $GLOBALS["available_albums"];
2441}
2442
2443function plogger_get_album_url() {
2444        $row = $GLOBALS["current_album"];
2445        return generate_url("album",$row["id"]);
2446}
2447
2448function plogger_get_album_thumb() {
2449        $rv = $GLOBALS["current_album"];
2450        // figure out the thumbnail as well
2451        $thumb_query = "SELECT * FROM `".TABLE_PREFIX."pictures` WHERE ";
2452        if ($rv["thumbnail_id"] > 0)
2453        $thumb_query .= " `id`=".$rv["thumbnail_id"];
2454        else
2455        $thumb_query .= " `parent_album`='".$rv["id"]."' ORDER BY `date_submitted` DESC LIMIT 1";
2456
2457        $thumb_result = run_query($thumb_query);
2458        $thumb_data = mysql_fetch_assoc($thumb_result);
2459        if ($thumb_data) {
2460                $rv["thumbnail_id"] = $thumb_data["id"];
2461                $rv["thumbnail_path"] = $thumb_data["path"];
2462        }
2463        return generate_thumb($rv['thumbnail_path'], $rv['thumbnail_id'], THUMB_SMALL);
2464}
2465
2466function plogger_album_picture_count() {
2467        if (isset($GLOBALS['current_album'])) {
2468                $row = $GLOBALS['current_album'];
2469                // XXX: this may be faster?
2470                $numquery = "SELECT COUNT(DISTINCT `id`) AS `num_pictures` FROM `".TABLE_PREFIX."pictures` WHERE `parent_album`='".$row['id']."'";
2471                $numresult = run_query($numquery);
2472                return mysql_result($numresult, 'num_pictures');
2473        } else {
2474                return 0;
2475        }
2476}
2477
2478function plogger_get_album_description() {
2479        return htmlspecialchars(SmartStripSlashes($GLOBALS["current_album"]["description"]));
2480}
2481
2482function plogger_get_album_name() {
2483        return htmlspecialchars(SmartStripSlashes($GLOBALS["current_album"]["name"]));
2484}
2485
2486function plogger_get_album_id() {
2487        return $GLOBALS["current_album"]["id"];
2488}
2489
2490function plogger_get_detail_link() {
2491        if (!$_SESSION["plogger_details"]){
2492                return '<a id="show_details" accesskey="d" href="javascript:void(0);" style="display: inline;" onclick="show_details();">' . plog_tr('Show details') . '</a><a id="hide_details" accesskey="d" href="javascript:void(0);" style="display: none;" onclick="hide_details();">' . plog_tr('Hide details') . '</a>';
2493        }
2494        else{
2495                return '<a id="hide_details" accesskey="d" href="javascript:void(0);" style="display: inline;" onclick="hide_details();">' . plog_tr('Hide details') . '</a><a id="show_details" accesskey="d" href="javascript:void(0);" style="display: none;" onclick="show_details();">' . plog_tr('Show details') . '</a>';
2496        }
2497}
2498
2499function plogger_download_checkbox($id, $label = '') {
2500        global $config;
2501        if ($config["allow_dl"])
2502        return '<input type="checkbox" name="checked[]" value="'.$id.'" />'.$label;
2503        else
2504        return '';
2505}
2506
2507function plogger_get_next_picture_link() {
2508        global $config;
2509
2510        $next_url = plogger_get_next_picture_url();
2511
2512        if ($next_url)
2513        if ($config['embedded'] == 0) {
2514                $next_link = '<a id="next-button" accesskey="." href="'.$next_url.'">' . plog_tr('Next') . ' &raquo;</a>';
2515        } else {
2516                $next_link = '<a id="next-button" accesskey="." href="'.$next_url.'">' . plog_tr('Next') . ' &raquo;</a>';
2517        }
2518        else
2519        $next_link = '';
2520
2521        return $next_link;
2522}
2523
2524function plogger_get_prev_picture_link() {
2525        global $config;
2526
2527        $prev_url = plogger_get_prev_picture_url();
2528
2529        if ($prev_url)
2530        if ($config['embedded'] == 0) {
2531                $prev_link = '<a id="prev-button" accesskey="," href="'.$prev_url.'">&laquo; ' . plog_tr('Previous') . '</a>';
2532        } else {
2533                $prev_link = '<a id="prev-button" accesskey="," href="'.$prev_url.'">&laquo; ' . plog_tr('Previous') . '</a>';
2534        }
2535        else
2536        $prev_link = '';
2537
2538        return $prev_link;
2539
2540}
2541/*** End of Album loop functions ***/
2542function get_comments($picture_id){
2543        $query = "SELECT *
2544        FROM `".TABLE_PREFIX."comments`
2545        WHERE `parent_id`=".intval($picture_id)."
2546        ORDER BY `date` DESC";
2547        $result = run_query($query);
2548
2549        $comments = array();
2550
2551        while ($row = mysql_fetch_assoc($result)){
2552                $comments[$row["id"]] = $row;
2553        }
2554
2555        return $comments;
2556}
2557
2558?>
Note: See TracBrowser for help on using the repository browser.