+ foreach($this->sort_orders as $key => $value) {
+ $output.= "<option value=\"". $key ."\"";
+ if($key == $_SESSION['sort_order']) {
+ $output.= " selected=\"selected\"";
+ }
+ $output.= ">". $value ."</option>";
+ }
+
+ return $output;
+
+ } // smarty_sort_select_list()
+
+ /**
+ * returns the currently selected sort order
+ * @return string
+ */
+ private function get_sort_order()
+ {
+ switch($_SESSION['sort_order']) {
+ case 'date_asc':
+ return " ORDER BY p.time ASC";
+ break;
+ case 'date_desc':
+ return " ORDER BY p.time DESC";
+ break;
+ case 'name_asc':
+ if($this->dbver < 9) {
+ return " ORDER BY p.name ASC";
+ }
+ else {
+ return " ORDER BY basename(p.uri) ASC";
+ }
+ break;
+ case 'name_desc':
+ if($this->dbver < 9) {
+ return " ORDER BY p.name DESC";
+ }
+ else {
+ return " ORDER BY basename(p.uri) DESC";
+ }
+ break;
+ case 'tags_asc':
+ return " ORDER BY t.name ASC ,p.time ASC";
+ break;
+ case 'tags_desc':
+ return " ORDER BY t.name DESC ,p.time ASC";
+ break;
+ }
+
+ } // get_sort_order()
+
+ /**
+ * return the next to be shown slide show image
+ *
+ * this function returns the URL of the next image
+ * in the slideshow sequence.
+ * @return string
+ */
+ public function getNextSlideShowImage()
+ {
+ $all_photos = $this->getPhotoSelection();
+
+ if(!isset($_SESSION['slideshow_img']) || $_SESSION['slideshow_img'] == count($all_photos)-1)
+ $_SESSION['slideshow_img'] = 0;
+ else
+ $_SESSION['slideshow_img']++;
+
+ return $this->get_phpfspot_url() ."phpfspot_img.php?idx=". $all_photos[$_SESSION['slideshow_img']] ."&width=". $this->cfg->photo_width;
+
+ } // getNextSlideShowImage()
+
+ /**
+ * return the previous to be shown slide show image
+ *
+ * this function returns the URL of the previous image
+ * in the slideshow sequence.
+ * @return string
+ */
+ public function getPrevSlideShowImage()
+ {
+ $all_photos = $this->getPhotoSelection();
+
+ if(!isset($_SESSION['slideshow_img']) || $_SESSION['slideshow_img'] == 0)
+ $_SESSION['slideshow_img'] = 0;
+ else
+ $_SESSION['slideshow_img']--;
+
+ return $this->get_phpfspot_url() ."phpfspot_img.php?idx=". $all_photos[$_SESSION['slideshow_img']] ."&width=". $this->cfg->photo_width;
+
+ } // getPrevSlideShowImage()
+
+ public function resetSlideShow()
+ {
+ if(isset($_SESSION['slideshow_img']))
+ unset($_SESSION['slideshow_img']);
+
+ } // resetSlideShow()
+
+ /**
+ * get random photo
+ *
+ * this function will get all photos from the fspot
+ * database and randomly return ONE entry
+ *
+ * saddly there is yet no sqlite3 function which returns
+ * the bulk result in array, so we have to fill up our
+ * own here.
+ * @return array
+ */
+ public function get_random_photo()
+ {
+ $all = Array();
+
+ $query_str = "
+ SELECT p.id
+ FROM photos p
+ ";
+
+ /* if show_tags is set, only return details for photos which
+ are specified to be shown
+ */
+ if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
+ $query_str.= "
+ INNER JOIN photo_tags pt
+ ON p.id=pt.photo_id
+ INNER JOIN tags t
+ ON pt.tag_id=t.id
+ WHERE
+ t.name IN ('".implode("','",$this->cfg->show_tags)."')";
+ }
+
+ $result = $this->db->db_query($query_str);
+
+ while($row = $this->db->db_fetch_object($result)) {
+ array_push($all, $row['id']);
+ }
+
+ return $all[array_rand($all)];
+
+ } // get_random_photo()
+
+ /**
+ * validates provided date
+ *
+ * this function validates if the provided date
+ * contains a valid date and will return true
+ * if it is.
+ * @param string $date_str
+ * @return boolean
+ */
+ public function isValidDate($date_str)
+ {
+ $timestamp = strtotime($date_str);
+
+ if(is_numeric($timestamp))
+ return true;
+
+ return false;
+
+ } // isValidDate()
+
+ /**
+ * timestamp to string conversion
+ * @param integer $timestamp
+ * @return string
+ */
+ private function ts2str($timestamp)
+ {
+ return strftime("%Y-%m-%d", $timestamp);
+ } // ts2str()
+
+ /**
+ * extract tag-names from $_GET['tags']
+ * @param string $tags_str
+ * @return string
+ */
+ private function extractTags($tags_str)
+ {
+ $not_validated = split(',', $tags_str);
+ $validated = array();
+
+ foreach($not_validated as $tag) {
+ if(is_numeric($tag))
+ array_push($validated, $tag);
+ }
+
+ return $validated;
+
+ } // extractTags()
+
+ /**
+ * returns the full path to a thumbnail
+ * @param integer $width
+ * @param integer $photo
+ * @return string
+ */
+ public function get_thumb_path($width, $photo)
+ {
+ $md5 = $this->getMD5($photo);
+ $sub_path = substr($md5, 0, 2);
+ return $this->cfg->thumb_path
+ . "/"
+ . $sub_path
+ . "/"
+ . $width
+ . "_"
+ . $md5;
+
+ } // get_thumb_path()
+
+ /**
+ * returns server's virtual host name
+ * @return string
+ */
+ private function get_server_name()
+ {
+ return $_SERVER['SERVER_NAME'];
+ } // get_server_name()
+
+ /**
+ * returns type of webprotocol which is currently used
+ * @return string
+ */
+ private function get_web_protocol()
+ {
+ if(!isset($_SERVER['HTTPS']))
+ return "http";
+ else
+ return "https";
+ } // get_web_protocol()
+
+ /**
+ * return url to this phpfspot installation
+ * @return string
+ */
+ private function get_phpfspot_url()
+ {
+ return $this->get_web_protocol() ."://". $this->get_server_name() . $this->cfg->web_path;
+ } // get_phpfspot_url()
+
+ /**
+ * returns the number of photos which are tagged with $tag_id
+ * @param integer $tag_id
+ * @return integer
+ */
+ public function get_num_photos($tag_id)
+ {
+ if($result = $this->db->db_fetchSingleRow("
+ SELECT count(*) as number
+ FROM photo_tags
+ WHERE
+ tag_id LIKE '". $tag_id ."'")) {
+
+ return $result['number'];
+
+ }
+
+ return 0;
+
+ } // get_num_photos()
+
+ /**
+ * check file exists and is readable
+ *
+ * returns true, if everything is ok, otherwise false
+ * if $silent is not set, this function will output and
+ * error message
+ * @param string $file
+ * @param boolean $silent
+ * @return boolean
+ */
+ private function check_readable($file, $silent = null)
+ {
+ if(!file_exists($file)) {
+ if(!isset($silent))
+ print "File \"". $file ."\" does not exist.\n";
+ return false;
+ }
+
+ if(!is_readable($file)) {
+ if(!isset($silent))
+ print "File \"". $file ."\" is not reachable for user ". $this->getuid() ."\n";
+ return false;
+ }
+
+ return true;
+
+ } // check_readable()
+
+ /**
+ * check if all needed indices are present
+ *
+ * this function checks, if some needed indices are already
+ * present, or if not, create them on the fly. they are
+ * necessary to speed up some queries like that one look for
+ * all tags, when show_tags is specified in the configuration.
+ */
+ private function checkDbIndices()
+ {
+ $result = $this->db->db_exec("
+ CREATE INDEX IF NOT EXISTS
+ phototag
+ ON
+ photo_tags
+ (photo_id, tag_id)
+ ");
+
+ } // checkDbIndices()
+
+ /**
+ * retrive F-Spot database version
+ *
+ * this function will return the F-Spot database version number
+ * It is stored within the sqlite3 database in the table meta
+ * @return string|null
+ */
+ public function getFspotDBVersion()
+ {
+ if($result = $this->db->db_fetchSingleRow("
+ SELECT data as version
+ FROM meta
+ WHERE
+ name LIKE 'F-Spot Database Version'
+ "))
+ return $result['version'];
+
+ return null;
+
+ } // getFspotDBVersion()
+
+ /**
+ * parse the provided URI and will returned the requested chunk
+ * @param string $uri
+ * @param string $mode
+ * @return string
+ */
+ public function parse_uri($uri, $mode)
+ {
+ if(($components = parse_url($uri)) !== false) {
+
+ switch($mode) {
+ case 'filename':
+ return basename($components['path']);
+ break;
+ case 'dirname':
+ return dirname($components['path']);
+ break;
+ case 'fullpath':
+ return $components['path'];
+ break;
+ }
+ }
+
+ return $uri;
+
+ } // parse_uri()
+
+ /**
+ * validate config options
+ *
+ * this function checks if all necessary configuration options are
+ * specified and set.
+ * @return boolean
+ */
+ private function check_config_options()
+ {
+ if(!isset($this->cfg->page_title) || $this->cfg->page_title == "")
+ $this->_error("Please set \$page_title in phpfspot_cfg");
+
+ if(!isset($this->cfg->base_path) || $this->cfg->base_path == "")
+ $this->_error("Please set \$base_path in phpfspot_cfg");
+
+ if(!isset($this->cfg->web_path) || $this->cfg->web_path == "")
+ $this->_error("Please set \$web_path in phpfspot_cfg");
+
+ if(!isset($this->cfg->thumb_path) || $this->cfg->thumb_path == "")
+ $this->_error("Please set \$thumb_path in phpfspot_cfg");
+
+ if(!isset($this->cfg->smarty_path) || $this->cfg->smarty_path == "")
+ $this->_error("Please set \$smarty_path in phpfspot_cfg");
+
+ if(!isset($this->cfg->fspot_db) || $this->cfg->fspot_db == "")
+ $this->_error("Please set \$fspot_db in phpfspot_cfg");
+
+ if(!isset($this->cfg->db_access) || $this->cfg->db_access == "")
+ $this->_error("Please set \$db_access in phpfspot_cfg");
+
+ if(!isset($this->cfg->phpfspot_db) || $this->cfg->phpfspot_db == "")
+ $this->_error("Please set \$phpfspot_db in phpfspot_cfg");
+
+ if(!isset($this->cfg->thumb_width) || $this->cfg->thumb_width == "")
+ $this->_error("Please set \$thumb_width in phpfspot_cfg");
+
+ if(!isset($this->cfg->thumb_height) || $this->cfg->thumb_height == "")
+ $this->_error("Please set \$thumb_height in phpfspot_cfg");
+
+ if(!isset($this->cfg->photo_width) || $this->cfg->photo_width == "")
+ $this->_error("Please set \$photo_width in phpfspot_cfg");
+
+ if(!isset($this->cfg->mini_width) || $this->cfg->mini_width == "")
+ $this->_error("Please set \$mini_width in phpfspot_cfg");
+
+ if(!isset($this->cfg->thumbs_per_page))
+ $this->_error("Please set \$thumbs_per_page in phpfspot_cfg");
+
+ if(!isset($this->cfg->path_replace_from) || $this->cfg->path_replace_from == "")
+ $this->_error("Please set \$path_replace_from in phpfspot_cfg");
+
+ if(!isset($this->cfg->path_replace_to) || $this->cfg->path_replace_to == "")
+ $this->_error("Please set \$path_replace_to in phpfspot_cfg");
+
+ if(!isset($this->cfg->hide_tags))
+ $this->_error("Please set \$hide_tags in phpfspot_cfg");
+
+ if(!isset($this->cfg->theme_name))
+ $this->_error("Please set \$theme_name in phpfspot_cfg");
+
+ if(!isset($this->cfg->logging))
+ $this->_error("Please set \$logging in phpfspot_cfg");
+
+ if(isset($this->cfg->logging) && $this->cfg->logging == 'logfile') {
+
+ if(!isset($this->cfg->log_file))
+ $this->_error("Please set \$log_file because you set logging = log_file in phpfspot_cfg");
+
+ if(!is_writeable($this->cfg->log_file))
+ $this->_error("The specified \$log_file ". $log_file ." is not writeable!");
+
+ }
+
+ /* check for pending slash on web_path */
+ if(!preg_match("/\/$/", $this->cfg->web_path))
+ $this->cfg->web_path.= "/";
+
+ return $this->runtime_error;
+
+ } // check_config_options()
+
+ /**
+ * cleanup phpfspot own database
+ *
+ * When photos are getting delete from F-Spot, there will remain
+ * remain some residues in phpfspot own database. This function
+ * will try to wipe them out.
+ */
+ public function cleanup_phpfspot_db()
+ {
+ $to_delete = Array();
+
+ $result = $this->cfg_db->db_query("
+ SELECT img_idx
+ FROM images
+ ORDER BY img_idx ASC
+ ");
+
+ while($row = $this->cfg_db->db_fetch_object($result)) {
+ if(!$this->db->db_fetchSingleRow("
+ SELECT id
+ FROM photos
+ WHERE id='". $row['img_idx'] ."'")) {
+
+ array_push($to_delete, $row['img_idx'], ',');
+ }
+ }
+
+ print count($to_delete) ." unnecessary objects will be removed from phpfspot's database.\n";
+
+ $this->cfg_db->db_exec("
+ DELETE FROM images
+ WHERE img_idx IN (". implode($to_delete) .")
+ ");
+
+ } // cleanup_phpfspot_db()
+
+ /**
+ * return first image of the page, the $current photo
+ * lies in.
+ *
+ * this function is used to find out the first photo of the
+ * current page, in which the $current photo lies. this is
+ * used to display the correct photo, when calling showPhotoIndex()
+ * from showImage()
+ * @param integer $current
+ * @param integer $max
+ * @return integer
+ */
+ private function getCurrentPage($current, $max)
+ {
+ if(isset($this->cfg->thumbs_per_page) && !empty($this->cfg->thumbs_per_page)) {
+ for($page_start = 0; $page_start <= $max; $page_start+=$this->cfg->thumbs_per_page) {
+ if($current >= $page_start && $current < ($page_start+$this->cfg->thumbs_per_page))
+ return $page_start;
+ }
+ }
+ return 0;
+
+ } // getCurrentPage()
+
+ /**
+ * return mime info
+ *
+ * this function tries to find out the correct mime-type
+ * for the provided file.
+ * @param string $file
+ * @return string
+ */
+ public function get_mime_info($file)
+ {
+ $details = getimagesize($orig_image);
+
+ /* if getimagesize() returns empty, try at least to find out the
+ mime type.
+ */
+ if(empty($details) && function_exists('mime_content_type')) {
+
+ // mime_content_type is marked as deprecated in the documentation,
+ // but is it really necessary to force users to install a PECL
+ // extension?
+ $details['mime'] = mime_content_type($file);
+ }
+
+ return $details['mime'];
+
+ } // get_mime_info()
+
+ /**
+ * return tag-name by tag-idx
+ *
+ * this function returns the tag-name for the requested
+ * tag specified by tag-idx.
+ * @param integer $idx
+ * @return string
+ */
+ public function get_tag_name($idx)
+ {
+ if($result = $this->db->db_fetchSingleRow("
+ SELECT name
+ FROM tags
+ WHERE
+ id LIKE '". $idx ."'")) {
+
+ return $result['name'];
+
+ }
+
+ return 0;
+
+ } // get_tag_name()