Changeset 562

Show
Ignore:
Timestamp:
07/18/08 17:44:34 (20 months ago)
Author:
sidtheduck
Message:

+ Fixed weird formatting indentations in some of plog-functions.php

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/plog-includes/plog-functions.php

    r561 r562  
    2828                        //if ($GLOBALS['plogger_mode'] == "slideshow") $breadcrumbs .= $sep . '<strong>' . plog_tr('Slideshow'); 
    2929 
    30                 break; 
     30                        break; 
    3131                case 'slideshow': 
    3232                case 'album': 
     
    5050                        } 
    5151 
    52                 break; 
     52                        break; 
    5353                case 'picture': 
    5454                        $row = get_picture_by_id($id); 
     
    7272                        //if ($GLOBALS['plogger_mode'] == "slideshow") $breadcrumbs .= $sep . '<strong>' . plog_tr('Slideshow'); 
    7373 
    74                 break; 
     74                        break; 
    7575                case 'search': 
    7676                        if ($config['truncate_breadcrumb'] == "album"){ 
     
    8585                                $breadcrumbs = $collections_link . $sep . '<strong>'.plog_tr('Search').'</strong>' . $sep . plog_tr('You searched for') . ' <strong>'.htmlspecialchars(SmartStripSlashes($_GET['searchterms'])).'</strong>.'; 
    8686                        } 
    87                 break; 
     87                        break; 
    8888                default: 
    89                 $breadcrumbs = $collections_name; 
    90                 break; 
     89                        $breadcrumbs = $collections_name; 
     90                        break; 
    9191        } 
    9292 
     
    102102                        if ($GLOBALS['plogger_mode'] == "slideshow") $breadcrumbs .= ' &raquo; ' . plog_tr('Slideshow'); 
    103103 
    104                 break; 
     104                        break; 
    105105                case 'slideshow': 
    106106                case 'album': 
     
    116116                        } 
    117117 
    118                 break; 
     118                        break; 
    119119                case 'picture': 
    120120                        $row = get_picture_by_id($GLOBALS['plogger_id']); 
     
    132132                        if ($GLOBALS['plogger_mode'] == "slideshow") $breadcrumbs .= ' &raquo; ' . plog_tr('Slideshow'); 
    133133 
    134                 break; 
     134                        break; 
    135135                default: 
    136                 $breadcrumbs = ' ' . plog_tr('Collections'); 
     136                        $breadcrumbs = ' ' . plog_tr('Collections'); 
    137137        } 
    138138 
     
    349349                        $output .= "\n\t\t\t\t\t\t\t\t<option value=\"$value\""; 
    350350                        if(strcasecmp($_SESSION["plogger_sortdir"], $okey) === 0) { 
    351                                         $output .= ' selected="selected"'; 
    352                                 } 
    353                                 $output .= ">$oval</option>"; 
     351                                $output .= ' selected="selected"'; 
    354352                        } 
    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].'" />'; 
     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 
     360function 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 
     383function getmicrotime($t) { 
     384        list($usec, $sec) = explode(" ",$t); 
     385        return ((float)$usec + (float)$sec); 
     386} 
     387 
     388// function for generating the slideshow javascript 
     389function 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 
     397function 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++; 
    375414                        } 
    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         } 
     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} 
    424424 
    425425        // function for generating the slideshow interface 
     
    459459} 
    460460 
    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"]); 
     461function 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 
     468function 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 
     481function 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\" />"; 
    524489                } else { 
    525                         return $result; 
    526                 } 
    527         } 
     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 
     512function 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} 
    528528 
    529529function get_active_collections_albums() { 
     
    544544} 
    545545 
    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; 
     546function 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 
     670function 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 
     714function 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 
     752function 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"]; 
    603783                        } 
    604784                } 
    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); 
     785        } else { 
     786                $album = false; 
     787        } 
     788 
     789        return $album; 
     790} 
     791 
     792function 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 
     807function 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"]; 
    699838                        } 
    700839                } 
    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  
     840        } 
     841 
     842        return $collection; 
     843} 
     844 
     845function 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 
     860function get_albums($collection_id = null, $sort = "alpha", $order = "DESC") { 
     861        global $config; 
     862 
     863        $albums = array(); 
     864 
     865        if ($sort == "mod"){ 
    717866                $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"){ 
     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"){ 
    734887                        $query .= " ASC "; 
    735888                } else { 
    736889                        $query .= " DESC "; 
    737890                } 
    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`, 
     891        } else { 
     892                $query = "SELECT 
     893                `a`.`id` AS `album_id`, 
     894                `a`.`name` AS `album_name`, 
     895                `c`.`id` AS `collection_id`, 
    759896                `c`.`name` AS `collection_name`, 
    760                 `a`.`name` AS `album_name` 
     897                `a`.`description`, 
     898                `a`.`thumbnail_id` 
    761899                FROM `".TABLE_PREFIX."albums` AS `a` 
    762900                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); 
     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 
     935function 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 
     992function SmartAddSlashes($str){ 
     993        if (get_magic_quotes_gpc()){ 
     994                return $str; 
     995        } else { 
     996                return addslashes($str); 
     997        } 
     998} 
     999 
     1000function 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 
     1010function 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"]."'"; 
    7651031                $result = run_query($sql); 
    7661032 
    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) { 
     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'])) { 
    7931046                $sql = "SELECT * 
    7941047                FROM `".TABLE_PREFIX."albums` 
    795                 WHERE `name` = '".mysql_real_escape_string($name)."'"; 
     1048                WHERE `path`='".$names["album"]."' 
     1049                AND `parent_id`=".intval($collection["id"]); 
    7961050                $result = run_query($sql); 
    7971051 
    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                                 } 
     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']; 
    8391068                        } 
    840                 } 
    841  
    842                 return $collection; 
    843         } 
    844  
    845         function get_collection_by_name($name) { 
     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"])) { 
    8461081                $sql = "SELECT * 
    847                 FROM `".TABLE_PREFIX."collections` 
    848                 WHERE name = '".mysql_real_escape_string($name)."'"; 
     1082                FROM `".TABLE_PREFIX."pictures` 
     1083                WHERE `caption`='".$names["picture"]."' 
     1084                AND `parent_album`=".intval($album["id"]); 
    8491085                $result = run_query($sql); 
    8501086 
    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"])) { 
     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); 
    10811095                        $sql = "SELECT * 
    10821096                        FROM `".TABLE_PREFIX."pictures` 
    1083                         WHERE `caption`='".$names["picture"]."' 
     1097                        WHERE `path` LIKE '".$filepath."____' 
    10841098                        AND `parent_album`=".intval($album["id"]); 
    10851099                        $result = run_query($sql); 
    1086  
    10871100                        $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 
     1114function 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> '; 
    11011145                        } 
    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){ 
     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 
     1157function 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 
     1163function 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){ 
    11701170                        case "collection": 
    11711171                                $query = "SELECT `path` FROM `".TABLE_PREFIX."collections` WHERE `id`=".intval($id); 
     
    11731173                                $row = mysql_fetch_assoc($result); 
    11741174                                $rv = $config['baseurl'].rawurlencode(SmartStripSlashes($row['path']))."/"; 
    1175                         break; 
     1175                                break; 
    11761176                        case "album": 
    11771177                                $query = "SELECT 
     
    11921192                                        } 
    11931193                                } 
    1194                         break; 
     1194                                break; 
    11951195                        case "picture": 
    11961196                                $pic = get_picture_by_id($id); 
    11971197                                //$album = $pic['parent_album']; 
    11981198                                $rv = $config['baseurl'].str_replace("%2F", "/", rawurlencode(substr(SmartStripSlashes($pic['path']), 0, -4)))."/"; 
    1199                         break; 
     1199                                break; 
    12001200                        case "search": 
    12011201                                $rv = $config['baseurl']."?level=search"; 
     
    12101210                                        } 
    12111211                                } 
    1212                         break; 
     1212                                break; 
    12131213                        case "collections": 
    12141214                                default: 
    12151215                                $rv = $config['baseurl']; 
    1216                         break; 
     1216                                break; 
    12171217                } 
    12181218        } else { 
     
    12231223                        case "collection": 
    12241224                                return $config['baseurl'].$query.'level=collection&amp;id='.$id; 
    1225                         break; 
     1225                                break; 
    12261226                        case "album": 
    12271227                                $rv = $config['baseurl'].$query.'level=album&amp;id='.$id; 
     
    12351235                                        } 
    12361236                                } 
    1237                         break; 
     1237                                break; 
    12381238                        case "picture": 
    12391239                                $rv = $config['baseurl'].$query.'level=picture&amp;id='.$id; 
    1240                         break; 
     1240                                break; 
    12411241                        case "search": 
    12421242                                $rv = $config['baseurl'].$query."level=search"; 
     
    12511251                                        } 
    12521252                                } 
    1253                         break; 
     1253                                break; 
    12541254                        case "collections": 
    12551255                                default: 
    12561256                                $query = (isset($config['query_args'])) ? "?".$config['query_args'] : $query = ""; 
    12571257                                $rv = $config['baseurl'].$query; 
    1258                         break; 
     1258                                break; 
    12591259                } 
    12601260        }