X-Git-Url: https://git.nubati.net/cgi-bin/gitweb.cgi?p=phpfspot.git;a=blobdiff_plain;f=phpfspot.class.php;h=f611391ae044ed2a234ef942ab622bd009c287b9;hp=f54a5d69c7a9a96c72d01409c64486b9d67f98f1;hb=52b668296eaa56c477cf81ef522ed1c7a24712a7;hpb=4712f548702a1a7f9ad86360ebd1fa44356791a3 diff --git a/phpfspot.class.php b/phpfspot.class.php index f54a5d6..f611391 100644 --- a/phpfspot.class.php +++ b/phpfspot.class.php @@ -141,6 +141,12 @@ class PHPFSPOT { /******* Opening F-Spot's sqlite database *********/ + /* Check if database file exists and is readable */ + if(!file_exists($this->cfg->fspot_db) || !is_readable($this->cfg->fspot_db)) { + print "Error: ". $this->cfg->fspot_db ." does not exist or is not readable for user ". $this->getuid() .".\n"; + exit(1); + } + /* Check if database file is writeable */ if(!is_writeable($this->cfg->fspot_db)) { print "Error: ". $this->cfg->fspot_db ." is not writeable for user ". $this->getuid() .".\n"; @@ -183,7 +189,7 @@ class PHPFSPOT { } /* Check if database file is writeable */ - if(!is_writeable($this->cfg->phpfspot_db)) { + if(file_exists($this->cfg->phpfspot_db) && !is_writeable($this->cfg->phpfspot_db)) { print "Error: ". $this->cfg->phpfspot_db ." is not writeable for user ". $this->getuid() .".\n"; print "Please fix permissions so phpfspot can create its own sqlite database to store some settings.\n"; exit(1); @@ -207,6 +213,7 @@ class PHPFSPOT { require_once "phpfspot_tmpl.php"; $this->tmpl = new PHPFSPOT_TMPL(); + /* pre-set some template variables */ $this->tmpl->assign('web_path', $this->cfg->web_path); /* Starting with F-Spot 0.4.2, the rating-feature was available */ @@ -404,7 +411,7 @@ class PHPFSPOT { $query_str=" SELECT DISTINCT t1.id as id, t1.name as name - FROM + FROM photo_tags pt1 INNER JOIN photo_tags pt2 ON pt1.photo_id=pt2.photo_id @@ -422,7 +429,7 @@ class PHPFSPOT { else { $result = $this->db->db_query(" - SELECT id,name + SELECT id as id,name as name FROM tags ORDER BY sort_priority ASC "); @@ -475,23 +482,53 @@ class PHPFSPOT { /* ~ F-Spot version 0.3.x */ if($this->dbver < 9) { $query_str = " - SELECT p.id, p.name, p.time, p.directory_path, p.description - FROM photos p + SELECT + p.id as id, + p.name as name, + p.time as time, + p.directory_path as directory_path, + p.description as description + FROM + photos p "; } else { /* till F-Spot version 0.4.1 */ if($this->dbver < 11) { $query_str = " - SELECT p.id, p.uri, p.time, p.description - FROM photos p + SELECT + p.id as id, + p.uri as uri, + p.time as time, + p.description as description + FROM + photos p + "; + } + elseif($this->dbver < 17) { + /* rating value got introduced */ + $query_str = " + SELECT + p.id as id, + p.uri as uri, + p.time as time, + p.description as description, + p.rating as rating + FROM + photos p "; } else { - /* rating value got introduced */ + /* path & filename now splited in base_uri & filename */ $query_str = " - SELECT p.id, p.uri, p.time, p.description, p.rating - FROM photos p + SELECT + p.id as id, + p.base_uri ||'/'|| p.filename as uri, + p.time as time, + p.description as description, + p.rating as rating + FROM + photos p "; } } @@ -514,45 +551,66 @@ class PHPFSPOT { "; } - if($row = $this->db->db_fetchSingleRow($query_str)) { - - /* before F-Spot db version 9 there was no uri column but - seperated fields for directory_path and name (= filename). - */ - if($this->dbver < 9) { - $row['uri'] = "file://". $row['directory_path'] ."/". $row['name']; - } + if(!$row = $this->db->db_fetchSingleRow($query_str)) + return null; - /* if version-idx has not yet been set, get the latest photo version */ - if(!isset($version_idx) || !$this->is_valid_version($idx, $version_idx)) - $version_idx = $this->get_latest_version($idx); + /* before F-Spot db version 9 there was no uri column but + seperated fields for directory_path and name (= filename). + */ + if($this->dbver < 9) { + $row['uri'] = "file://". $row['directory_path'] ."/". $row['name']; + } + /* starting with dbversion >= 17 we need to rawurldecode() uri */ + elseif($this->dbver >= 17) { + $row['uri'] = rawurldecode($row['uri']); + } - /* if an alternative version has been requested */ - if($version_idx > 0) { + /* if version-idx has not yet been set, get the latest photo version */ + if(!isset($version_idx) || !$this->is_valid_version($idx, $version_idx)) + $version_idx = $this->get_latest_version($idx); + /* if an alternative version has been requested. But we + support this only for F-Spot database versions from + v9. + */ + if($version_idx > 0 && $this->dbver >= 9) { + if ($this->dbver < 17) { /* check for alternative versions */ if($version = $this->db->db_fetchSingleRow(" - SELECT - version_id, name, uri - FROM - photo_versions - WHERE - photo_id LIKE '". $idx ."' - AND - version_id LIKE '". $version_idx ."' - ")) { + SELECT + version_id, name, uri + FROM + photo_versions + WHERE + photo_id LIKE '". $idx ."' + AND + version_id LIKE '". $version_idx ."'")) { $row['name'] = $version['name']; $row['uri'] = $version['uri']; - } } + else { + /* path & filename now splited in base_uri & filename */ + if($version = $this->db->db_fetchSingleRow(" + SELECT + version_id, + name, + base_uri || '/'||filename as uri + FROM + photo_versions + WHERE + photo_id LIKE '". $idx ."' + AND + version_id LIKE '". $version_idx ."'")) { - return $row; - + $row['name'] = $version['name']; + $row['uri'] = rawurldecode($version['uri']); + } + } } - return null; + return $row; } // get_photo_details() @@ -679,8 +737,13 @@ class PHPFSPOT { * @return string */ public function translate_path($path) - { - return str_replace($this->cfg->path_replace_from, $this->cfg->path_replace_to, $path); + { + if($this->cfg->enable_replace_path == true) + return str_replace( + $this->cfg->path_replace_from, + $this->cfg->path_replace_to, $path); + + return $path; } // translate_path @@ -1186,7 +1249,7 @@ class PHPFSPOT { * @return array */ public function getPhotoSelection() - { + { $matched_photos = Array(); $additional_where_cond = ""; @@ -1216,7 +1279,7 @@ class PHPFSPOT { ) "; } - else { + if($this->dbver < 17) { $additional_where_cond.= " ( basename(p.uri) LIKE '%". $_SESSION['searchfor_name'] ."%' @@ -1225,6 +1288,15 @@ class PHPFSPOT { ) "; } + else { + $additional_where_cond.= " + ( + p.filename LIKE '%". $_SESSION['searchfor_name'] ."%' + OR + p.description LIKE '%". $_SESSION['searchfor_name'] ."%' + ) + "; + } } /* limit result based on rate-search */ @@ -1252,8 +1324,10 @@ class PHPFSPOT { /* return a search result */ if(isset($_SESSION['searchfor_tag']) && $_SESSION['searchfor_tag'] != '') { $query_str = " - SELECT DISTINCT pt1.photo_id - FROM photo_tags pt1 + SELECT DISTINCT + pt1.photo_id as photo_id + FROM + photo_tags pt1 INNER JOIN photo_tags pt2 ON pt1.photo_id=pt2.photo_id INNER JOIN tags t @@ -1291,8 +1365,10 @@ class PHPFSPOT { /* photo has to match at least on of the selected tags */ if($_SESSION['tag_condition'] == 'or') { $query_str = " - SELECT DISTINCT pt1.photo_id - FROM photo_tags pt1 + SELECT DISTINCT + pt1.photo_id as photo_id + FROM + photo_tags pt1 INNER JOIN photo_tags pt2 ON pt1.photo_id=pt2.photo_id INNER JOIN tags t @@ -1330,8 +1406,10 @@ class PHPFSPOT { */ $query_str = " - SELECT DISTINCT pt1.photo_id - FROM photo_tags pt1 + SELECT DISTINCT + pt1.photo_id as photo_id + FROM + photo_tags pt1 "; if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) { @@ -1378,8 +1456,10 @@ class PHPFSPOT { /* return all available photos */ $query_str = " - SELECT DISTINCT p.id - FROM photos p + SELECT DISTINCT + p.id as id + FROM + photos p LEFT JOIN photo_tags pt ON p.id=pt.photo_id LEFT JOIN tags t @@ -1440,40 +1520,45 @@ class PHPFSPOT { } $thumbs = 0; - $images[$thumbs] = Array(); - $img_height[$thumbs] = Array(); - $img_width[$thumbs] = Array(); - $img_id[$thumbs] = Array(); - $img_name[$thumbs] = Array(); - $img_fullname[$thumbs] = Array(); - $img_title = Array(); - $img_rating = Array(); for($i = $begin_with; $i < $end_with; $i++) { - if(isset($photos[$i])) { + if(!isset($photos[$i])) + continue; + + /* on first run, initalize all used variables */ + if($thumbs == 0) { + $images = Array(); + $images[$thumbs] = Array(); + $img_height[$thumbs] = Array(); + $img_width[$thumbs] = Array(); + $img_id[$thumbs] = Array(); + $img_name[$thumbs] = Array(); + $img_fullname[$thumbs] = Array(); + $img_title = Array(); + $img_rating = Array(); + } - $images[$thumbs] = $photos[$i]; - $img_id[$thumbs] = $i; - $img_name[$thumbs] = htmlspecialchars($this->getPhotoName($photos[$i], 15)); - $img_fullname[$thumbs] = htmlspecialchars($this->getPhotoName($photos[$i], 0)); - $img_title[$thumbs] = "Click to view photo ". htmlspecialchars($this->getPhotoName($photos[$i], 0)); - $img_rating[$thumbs] = $this->get_photo_rating($photos[$i]); + $images[$thumbs] = $photos[$i]; + $img_id[$thumbs] = $i; + $img_name[$thumbs] = htmlspecialchars($this->getPhotoName($photos[$i], 15)); + $img_fullname[$thumbs] = htmlspecialchars($this->getPhotoName($photos[$i], 0)); + $img_title[$thumbs] = "Click to view photo ". htmlspecialchars($this->getPhotoName($photos[$i], 0)); + $img_rating[$thumbs] = $this->get_photo_rating($photos[$i]); - $thumb_path = $this->get_thumb_path($this->cfg->thumb_width, $photos[$i], $this->get_latest_version($photos[$i])); + /* get local path of the thumbnail image to be displayed */ + $thumb_path = $this->get_thumb_path($this->cfg->thumb_width, $photos[$i], $this->get_latest_version($photos[$i])); - if(file_exists($thumb_path)) { - $info = getimagesize($thumb_path); + /* if the image exist and is readable, extract some details */ + if(file_exists($thumb_path) && is_readable($thumb_path)) { + if($info = getimagesize($thumb_path) !== false) { $img_width[$thumbs] = $info[0]; $img_height[$thumbs] = $info[1]; } - $thumbs++; - } + } + $thumbs++; } - // +1 for for smarty's selection iteration - $thumbs++; - if(isset($_SESSION['searchfor_tag']) && $_SESSION['searchfor_tag'] != '') $this->tmpl->assign('searchfor_tag', $_SESSION['searchfor_tag']); @@ -1598,16 +1683,20 @@ class PHPFSPOT { $this->tmpl->assign('preview_width', $this->cfg->photo_width); $this->tmpl->assign('thumb_container_width', $this->cfg->thumb_width); $this->tmpl->assign('thumb_container_height', $this->cfg->thumb_height+20); - $this->tmpl->assign('images', $images); - $this->tmpl->assign('img_width', $img_width); - $this->tmpl->assign('img_height', $img_height); - $this->tmpl->assign('img_id', $img_id); - $this->tmpl->assign('img_name', $img_name); - $this->tmpl->assign('img_fullname', $img_fullname); - $this->tmpl->assign('img_title', $img_title); - $this->tmpl->assign('img_rating', $img_rating); - $this->tmpl->assign('thumbs', $thumbs); $this->tmpl->assign('selected_tags', $this->getSelectedTags('img')); + // +1 for for smarty's selection iteration + $this->tmpl->assign('thumbs', $thumbs+1); + + if($thumbs > 0) { + $this->tmpl->assign('images', $images); + $this->tmpl->assign('img_width', $img_width); + $this->tmpl->assign('img_height', $img_height); + $this->tmpl->assign('img_id', $img_id); + $this->tmpl->assign('img_name', $img_name); + $this->tmpl->assign('img_fullname', $img_fullname); + $this->tmpl->assign('img_title', $img_title); + $this->tmpl->assign('img_rating', $img_rating); + } $result = $this->tmpl->fetch("photo_index.tpl"); @@ -1681,13 +1770,13 @@ class PHPFSPOT { case 4: /* bottom, right */ $flip_vert = true; break; case 5: /* left side, top */ - $rotate = 90; $flip_vert = true; break; + $rotate = 270; $flip_vert = true; break; case 6: /* right side, top */ - $rotate = 90; break; + $rotate = 270; break; case 7: /* left side, bottom */ - $rotate = 270; $flip_vert = true; break; + $rotate = 90; $flip_vert = true; break; case 8: /* right side, bottom */ - $rotate = 270; break; + $rotate = 90; break; } } @@ -2055,7 +2144,7 @@ class PHPFSPOT { $full_path = $this->translate_path($this->parse_uri($details['uri'], 'fullpath')); if(!file_exists($full_path)) { - $this->_error("File ". $full_path ." does not exist\n"); + $this->_error("File ". $full_path ." does not exist"); return; } @@ -2434,7 +2523,7 @@ class PHPFSPOT { private function get_photo_tags($idx) { $result = $this->db->db_query(" - SELECT t.id, t.name + SELECT t.id as id, t.name as name FROM tags t INNER JOIN photo_tags pt ON t.id=pt.tag_id @@ -2503,6 +2592,13 @@ class PHPFSPOT { $missing = true; } + if($this->cfg->db_access == "pdo") { + if(array_search("sqlite", PDO::getAvailableDrivers()) === false) { + print "PDO SQLite3 driver is missing
\n"; + $missing = true; + } + } + /* Check for HTML_AJAX PEAR package, lent from Horde project */ ini_set('track_errors', 1); @include_once 'HTML/AJAX/Server.php'; @@ -2520,6 +2616,11 @@ class PHPFSPOT { print "PEAR Console_Getopt package is missing
\n"; $missing = true; } + @include_once 'Date.php'; + if(isset($php_errormsg) && preg_match('/Failed opening.*for inclusion/i', $php_errormsg)) { + print "PEAR Date package is missing
\n"; + $missing = true; + } @include_once $this->cfg->smarty_path .'/libs/Smarty.class.php'; if(isset($php_errormsg) && preg_match('/Failed opening.*for inclusion/i', $php_errormsg)) { print "Smarty template engine can not be found in ". $this->cfg->smarty_path ."/libs/Smarty.class.php
\n"; @@ -2572,14 +2673,18 @@ class PHPFSPOT { switch($this->cfg->logging) { default: case 'display': - print "\"warning\"\n"; - print $text ."
\n"; + if(isset($this->fromcmd)) + print $text ."\n"; + else { + print "\"warning\"\n"; + print $text ."
\n"; + } break; case 'errorlog': error_log($text); break; case 'logfile': - error_log($text, 3, $his->cfg->log_file); + error_log($text, 3, $this->cfg->log_file); break; } @@ -2620,11 +2725,22 @@ class PHPFSPOT { * @param integer $month * @param integer $day */ - public function get_calendar_matrix($year = 0, $month = 0, $day = 0) + public function get_calendar_matrix($userdate) { - if (!isset($year)) $year = date('Y'); - if (!isset($month)) $month = date('m'); - if (!isset($day)) $day = date('d'); + if(($userdate = strtotime($userdate)) === false) { + $year = date('Y'); + $month = date('m'); + $day = date('d'); + } + else { + $date = new Date(); + $date->setDate($userdate); + + $year = $date->getYear(); + $month = $date->getMonth(); + $day = $date->getDay(); + } + $rows = 1; $cols = 1; $matrix = Array(); @@ -2633,27 +2749,26 @@ class PHPFSPOT { require_once CALENDAR_ROOT.'Day.php'; // Build the month - $month = new Calendar_Month_Weekdays($year,$month); + $month_cal = new Calendar_Month_Weekdays($year,$month); // Create links - $prevStamp = $month->prevMonth(true); + $prevStamp = $month_cal->prevMonth(true); $prev = "javascript:setMonth(". date('Y',$prevStamp) .", ". date('n',$prevStamp) .", ". date('j',$prevStamp) .");"; - $nextStamp = $month->nextMonth(true); + $nextStamp = $month_cal->nextMonth(true); $next = "javascript:setMonth(". date('Y',$nextStamp) .", ". date('n',$nextStamp) .", ". date('j',$nextStamp) .");"; $selectedDays = array ( new Calendar_Day($year,$month,$day), - new Calendar_Day($year,12,25), ); // Build the days in the month - $month->build($selectedDays); + $month_cal->build($selectedDays); - $this->tmpl->assign('current_month', date('F Y',$month->getTimeStamp())); + $this->tmpl->assign('current_month', date('F Y',$month_cal->getTimeStamp())); $this->tmpl->assign('prev_month', $prev); $this->tmpl->assign('next_month', $next); - while ( $day = $month->fetch() ) { + while ( $day = $month_cal->fetch() ) { if(!isset($matrix[$rows])) $matrix[$rows] = Array(); @@ -2661,7 +2776,13 @@ class PHPFSPOT { $string = ""; $dayStamp = $day->thisDay(true); - $link = "javascript:setCalendarDate(". date('Y',$dayStamp) .", ". date('n',$dayStamp).", ". date('j',$dayStamp) .");"; + $link = "javascript:setCalendarDate('" + . date('Y',$dayStamp) + . "-" + . date('m',$dayStamp) + . "-" + . date('d',$dayStamp) + ."');"; // isFirst() to find start of week if ( $day->isFirst() ) @@ -3097,7 +3218,7 @@ class PHPFSPOT { $all = Array(); $query_str = " - SELECT p.id + SELECT p.id as id FROM photos p "; @@ -3139,30 +3260,36 @@ class PHPFSPOT { { $all = Array(); - $query_str = " - SELECT p.id - FROM photos p - INNER JOIN photo_tags pt - ON p.id=pt.photo_id - "; - if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) { $query_str.= " - INNER JOIN tags t - ON pt.tag_id=t.id - "; + SELECT + DISTINCT pt1.photo_id as id + FROM + photo_tags pt1 + INNER JOIN photo_tags + pt2 ON pt1.photo_id=pt2.photo_id + INNER JOIN tags t1 + ON t1.id=pt1.tag_id + INNER JOIN tags t2 + ON t2.id=pt2.tag_id + WHERE + pt1.tag_id LIKE '". $tagidx ."' + AND + t2.name IN ('".implode("','",$this->cfg->show_tags)."') + ORDER BY + t1.sort_priority ASC"; + } + else { + $query_str = " + SELECT + p.id as id + FROM + photos p + INNER JOIN photo_tags pt + ON p.id=pt.photo_id + WHERE + pt.tag_id LIKE '". $tagidx ."'"; } - $query_str.= " - WHERE - pt.tag_id LIKE '". $tagidx ."' - "; - - /*if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) { - $query_str.= " - AND - t.name IN ('".implode("','",$this->cfg->show_tags)."') - "; - }*/ $result = $this->db->db_query($query_str); @@ -3374,23 +3501,24 @@ class PHPFSPOT { */ public function parse_uri($uri, $mode) { - if(($components = parse_url($uri)) !== false) { + if(($components = parse_url($uri)) === false) + return $uri; - switch($mode) { - case 'filename': - return basename($components['path']); - break; - case 'dirname': - return dirname($components['path']); - break; - case 'fullpath': - return $components['path']; - break; - } + switch($mode) { + case 'filename': + return basename($components['path']); + break; + case 'dirname': + return dirname($components['path']); + break; + case 'fullpath': + return $components['path']; + break; + default: + $this->throwError("unknown mode ". $mode); + break; } - return $uri; - } // parse_uri() /** @@ -3441,11 +3569,16 @@ class PHPFSPOT { 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->enable_replace_path)) + $this->_error("Please set \$enable_replace_path in phpfspot_cfg"); + + if($this->cfg->enable_replace_path == true) { + 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->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"); @@ -3488,14 +3621,14 @@ class PHPFSPOT { $to_delete = Array(); $result = $this->cfg_db->db_query(" - SELECT img_idx + SELECT img_idx as 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 + SELECT id as id FROM photos WHERE id='". $row['img_idx'] ."'")) { @@ -3574,7 +3707,7 @@ class PHPFSPOT { public function get_tag_name($idx) { if($result = $this->db->db_fetchSingleRow(" - SELECT name + SELECT name as name FROM tags WHERE id LIKE '". $idx ."'")) { @@ -3681,7 +3814,7 @@ class PHPFSPOT { public function get_db_version() { if($row = $this->cfg_db->db_fetchSingleRow(" - SELECT meta_value + SELECT meta_value as meta_value FROM meta WHERE @@ -3780,6 +3913,22 @@ class PHPFSPOT { } // get_photo_version_name() + /** + */ + public function is_valid_width($image_width) + { + if(in_array($image_width, + Array($this->cfg->thumb_width, + $this->cfg->photo_width, + $this->cfg->mini_width, + 30))) + + return true; + + return false; + + } // is_valid_width() + } // class PHPFSPOT ?>