fixed sql query for tag-search
[phpfspot.git] / phpfspot.class.php
index f68632184a730dd3b5b4ce90195dcc2187e9be06..826fafd2d1101b792306590de0cd032731443a7d 100644 (file)
@@ -32,6 +32,8 @@ class PHPFSPOT {
    var $tmpl;
    var $tags;
    var $avail_tags;
    var $tmpl;
    var $tags;
    var $avail_tags;
+
+   private $runtime_error = false;
    private $dbver;
 
    /**
    private $dbver;
 
    /**
@@ -45,15 +47,22 @@ class PHPFSPOT {
    {
       $this->cfg = new PHPFSPOT_CFG;
 
    {
       $this->cfg = new PHPFSPOT_CFG;
 
+      /* verify config settings */
+      if($this->check_config_options()) {
+         exit(1);
+      }
+
       /* set application name and version information */
       $this->cfg->product = "phpfspot";
       /* set application name and version information */
       $this->cfg->product = "phpfspot";
-      $this->cfg->version = "1.2";
+      $this->cfg->version = "1.3";
 
       $this->sort_orders= array(
          'date_asc' => 'Date ↑',
          'date_desc' => 'Date ↓',
          'name_asc' => 'Name ↑',
 
       $this->sort_orders= array(
          'date_asc' => 'Date ↑',
          'date_desc' => 'Date ↓',
          'name_asc' => 'Name ↑',
-         'name_desc' => 'Name ↓'
+         'name_desc' => 'Name ↓',
+         'tags_asc' => 'Tags ↑',
+         'tags_desc' => 'Tags ↓',
       );
 
       /* Check necessary requirements */
       );
 
       /* Check necessary requirements */
@@ -112,8 +121,8 @@ class PHPFSPOT {
       if(!isset($_SESSION['searchfor']))
          $_SESSION['searchfor'] = '';
 
       if(!isset($_SESSION['searchfor']))
          $_SESSION['searchfor'] = '';
 
-      // if begin_with is still set but rows_per_page is now 0, unset it
-      if(isset($_SESSION['begin_with']) && $this->cfg->rows_per_page == 0)
+      // if begin_with is still set but thumbs_per_page is now 0, unset it
+      if(isset($_SESSION['begin_with']) && $this->cfg->thumbs_per_page == 0)
          unset($_SESSION['begin_with']);
 
    } // __construct()
          unset($_SESSION['begin_with']);
 
    } // __construct()
@@ -743,13 +752,13 @@ class PHPFSPOT {
                FROM photo_tags pt1
             INNER JOIN photo_tags pt2
                ON pt1.photo_id=pt2.photo_id
                FROM photo_tags pt1
             INNER JOIN photo_tags pt2
                ON pt1.photo_id=pt2.photo_id
-            INNER JOIN tags t1
-               ON pt1.tag_id=t1.id
+            INNER JOIN tags t
+               ON pt1.tag_id=t.id
             INNER JOIN photos p
                ON pt1.photo_id=p.id
             INNER JOIN tags t2
                ON pt2.tag_id=t2.id
             INNER JOIN photos p
                ON pt1.photo_id=p.id
             INNER JOIN tags t2
                ON pt2.tag_id=t2.id
-            WHERE t1.name LIKE '%". $_SESSION['searchfor'] ."%' ";
+            WHERE t.name LIKE '%". $_SESSION['searchfor'] ."%' ";
 
          if(isset($additional_where_cond))
             $query_str.= "AND ". $additional_where_cond ." ";
 
          if(isset($additional_where_cond))
             $query_str.= "AND ". $additional_where_cond ." ";
@@ -865,7 +874,7 @@ class PHPFSPOT {
 
       /* return all available photos */
       $query_str = "
 
       /* return all available photos */
       $query_str = "
-         SELECT p.id
+         SELECT DISTINCT p.id
          FROM photos p
          LEFT JOIN photo_tags pt
             ON p.id=pt.photo_id
          FROM photos p
          LEFT JOIN photo_tags pt
             ON p.id=pt.photo_id
@@ -877,7 +886,10 @@ class PHPFSPOT {
          $query_str.= "WHERE ". $additional_where_cond ." ";
 
       if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
          $query_str.= "WHERE ". $additional_where_cond ." ";
 
       if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
-         $query_str.= "AND t.name IN ('".implode("','",$this->cfg->show_tags). "')";
+         if(isset($additional_where_cond))
+            $query_str.= "AND t.name IN ('".implode("','",$this->cfg->show_tags). "')";
+         else
+            $query_str.= "WHERE t.name IN ('".implode("','",$this->cfg->show_tags). "')";
       }
  
       if(isset($order_str))
       }
  
       if(isset($order_str))
@@ -906,79 +918,54 @@ class PHPFSPOT {
       if(isset($_SESSION['begin_with']) && $_SESSION['begin_with'] != "")
          $anchor = $_SESSION['begin_with'];
 
       if(isset($_SESSION['begin_with']) && $_SESSION['begin_with'] != "")
          $anchor = $_SESSION['begin_with'];
 
-      if(!isset($this->cfg->rows_per_page) || $this->cfg->rows_per_page == 0) {
+      if(!isset($this->cfg->thumbs_per_page) || $this->cfg->thumbs_per_page == 0) {
 
          $begin_with = 0;
          $end_with = $count;
 
       }
 
          $begin_with = 0;
          $end_with = $count;
 
       }
-      elseif($this->cfg->rows_per_page > 0) {
+      elseif($this->cfg->thumbs_per_page > 0) {
 
          if(!isset($_SESSION['begin_with']) || $_SESSION['begin_with'] == 0) {
             $begin_with = 0;
          }
          else {
 
          if(!isset($_SESSION['begin_with']) || $_SESSION['begin_with'] == 0) {
             $begin_with = 0;
          }
          else {
-
             $begin_with = $_SESSION['begin_with'];
             $begin_with = $_SESSION['begin_with'];
-
-            // verify $begin_with - perhaps the thumbs-per-rows or
-            // rows-per-page variables have changed or the jump back
-            // from a photo wasn't exact - so calculate the real new
-            // starting point
-            $multiplicator = $this->cfg->rows_per_page * $this->cfg->thumbs_per_row;
-            for($i = 0; $i <= $count; $i+=$multiplicator) {
-               if($begin_with >= $i && $begin_with < $i+$multiplicator) {
-                  $begin_with = $i;
-                  break;
-               }
-            }
          }
 
          }
 
-         $end_with = $begin_with + ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
+         $end_with = $begin_with + $this->cfg->thumbs_per_page;
       }
 
       }
 
-   
-      $rows = 0;
-      $cols = 0;
-      $images[$rows] = Array();
-      $img_height[$rows] = Array();
-      $img_width[$rows] = Array();
-      $img_id[$rows] = Array();
-      $img_name[$rows] = Array();
+      $thumbs = 0;
+      $images[$thumbs] = Array();
+      $img_height[$thumbs] = Array();
+      $img_width[$thumbs] = Array();
+      $img_id[$thumbs] = Array();
+      $img_name[$thumbs] = Array();
       $img_title = Array();
 
       for($i = $begin_with; $i < $end_with; $i++) {
 
          if(isset($photos[$i])) {
 
       $img_title = Array();
 
       for($i = $begin_with; $i < $end_with; $i++) {
 
          if(isset($photos[$i])) {
 
-            $images[$rows][$cols] = $photos[$i];
-            $img_id[$rows][$cols] = $i;
-            $img_name[$rows][$cols] = htmlspecialchars($this->getPhotoName($photos[$i], 15));
-            $img_title[$rows][$cols] = "Click to view photo ". htmlspecialchars($this->getPhotoName($photos[$i], 0));
+            $images[$thumbs] = $photos[$i];
+            $img_id[$thumbs] = $i;
+            $img_name[$thumbs] = htmlspecialchars($this->getPhotoName($photos[$i], 15));
+            $img_title[$thumbs] = "Click to view photo ". htmlspecialchars($this->getPhotoName($photos[$i], 0));
 
             $thumb_path = $this->get_thumb_path($this->cfg->thumb_width, $photos[$i]);
 
             if(file_exists($thumb_path)) {
                $info = getimagesize($thumb_path); 
 
             $thumb_path = $this->get_thumb_path($this->cfg->thumb_width, $photos[$i]);
 
             if(file_exists($thumb_path)) {
                $info = getimagesize($thumb_path); 
-               $img_width[$rows][$cols] = $info[0];
-               $img_height[$rows][$cols] = $info[1];
-            }
-
-            if($cols == $this->cfg->thumbs_per_row-1) {
-               $cols = 0;
-               $rows++;
-               $images[$rows] = Array();
-               $img_width[$rows] = Array();
-               $img_height[$rows] = Array();
-            }
-            else {
-               $cols++;
+               $img_width[$thumbs] = $info[0];
+               $img_height[$thumbs] = $info[1];
             }
             }
+            $thumbs++;
          } 
       }
 
       // +1 for for smarty's selection iteration
          } 
       }
 
       // +1 for for smarty's selection iteration
-      $rows++;
+      $thumbs++;
 
       if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '')
          $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
 
       if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '')
          $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
@@ -993,20 +980,20 @@ class PHPFSPOT {
       }
 
       /* do we have to display the page selector ? */
       }
 
       /* do we have to display the page selector ? */
-      if($this->cfg->rows_per_page != 0) {
+      if($this->cfg->thumbs_per_page != 0) {
 
          $page_select = "";
       
          /* calculate the page switchers */
 
          $page_select = "";
       
          /* calculate the page switchers */
-         $previous_start = $begin_with - ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
-         $next_start = $begin_with + ($this->cfg->rows_per_page * $this->cfg->thumbs_per_row);
+         $previous_start = $begin_with - $this->cfg->thumbs_per_page;
+         $next_start = $begin_with + $this->cfg->thumbs_per_page;
 
          if($begin_with != 0) 
             $this->tmpl->assign("previous_url", "javascript:showPhotoIndex(". $previous_start .");"); 
          if($end_with < $count)
             $this->tmpl->assign("next_url", "javascript:showPhotoIndex(". $next_start .");"); 
 
 
          if($begin_with != 0) 
             $this->tmpl->assign("previous_url", "javascript:showPhotoIndex(". $previous_start .");"); 
          if($end_with < $count)
             $this->tmpl->assign("next_url", "javascript:showPhotoIndex(". $next_start .");"); 
 
-         $photo_per_page  = $this->cfg->rows_per_page * $this->cfg->thumbs_per_row;
+         $photo_per_page  = $this->cfg->thumbs_per_page;
          $last_page = ceil($count / $photo_per_page);
 
          /* get the current selected page */
          $last_page = ceil($count / $photo_per_page);
 
          /* get the current selected page */
@@ -1102,8 +1089,7 @@ class PHPFSPOT {
       $this->tmpl->assign('img_id', $img_id);
       $this->tmpl->assign('img_name', $img_name);
       $this->tmpl->assign('img_title', $img_title);
       $this->tmpl->assign('img_id', $img_id);
       $this->tmpl->assign('img_name', $img_name);
       $this->tmpl->assign('img_title', $img_title);
-      $this->tmpl->assign('rows', $rows);
-      $this->tmpl->assign('columns', $this->cfg->thumbs_per_row);
+      $this->tmpl->assign('thumbs', $thumbs);
 
       $this->tmpl->show("photo_index.tpl");
 
 
       $this->tmpl->show("photo_index.tpl");
 
@@ -1147,26 +1133,26 @@ class PHPFSPOT {
       $meta = $this->get_meta_informations($orig_image);
 
       $rotate = 0;
       $meta = $this->get_meta_informations($orig_image);
 
       $rotate = 0;
-      $flip = false;
+      $flip_hori = false;
+      $flip_vert = false;
 
       switch($meta['Orientation']) {
 
       switch($meta['Orientation']) {
-
          case 1: /* top, left */
          case 1: /* top, left */
-            $rotate = 0; $flip = false; break;
+            /* nothing to do */ break;
          case 2: /* top, right */
          case 2: /* top, right */
-            $rotate = 0; $flip = true; break;
+            $rotate = 0; $flip_hori = true; break;
          case 3: /* bottom, left */
          case 3: /* bottom, left */
-            $rotate = 180; $flip = false; break;
+            $rotate = 180; break;
          case 4: /* bottom, right */
          case 4: /* bottom, right */
-            $rotate = 180; $flip = true; break;
+            $flip_vert = true; break;
          case 5: /* left side, top */
          case 5: /* left side, top */
-            $rotate = 90; $flip = true; break;
+            $rotate = 90; $flip_vert = true; break;
          case 6: /* right side, top */
          case 6: /* right side, top */
-            $rotate = 90; $flip = false; break;
+            $rotate = 90; break;
          case 7: /* left side, bottom */
          case 7: /* left side, bottom */
-            $rotate = 270; $flip = true; break;
+            $rotate = 270; $flip_vert = true; break;
          case 8: /* right side, bottom */
          case 8: /* right side, bottom */
-            $rotate = 270; $flip = false; break;
+            $rotate = 270; break;
       }
 
       $src_img = @imagecreatefromjpeg($orig_image);
       }
 
       $src_img = @imagecreatefromjpeg($orig_image);
@@ -1238,12 +1224,14 @@ class PHPFSPOT {
       imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, imagesx($src_img), imagesy($src_img));
 
       /* needs the image to be flipped horizontal? */
       imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, imagesx($src_img), imagesy($src_img));
 
       /* needs the image to be flipped horizontal? */
-      if($flip) {
-         print "(FLIP)";
-         $image = $dst_img;
-         for($x = 0; $x < $new_w; $x++) {
-            imagecopy($dst_img, $image, $x, 0, $w - $x - 1, 0, 1, $h);
-         }
+      if($flip_hori) {
+         $this->_debug("(FLIP)");
+         $dst_img = $this->flipImage($dst_img, 'hori');
+      }
+      /* needs the image to be flipped vertical? */
+      if($flip_vert) {
+         $this->_debug("(FLIP)");
+         $dst_img = $this->flipImage($dst_img, 'vert');
       }
 
       if($rotate) {
       }
 
       if($rotate) {
@@ -1559,6 +1547,35 @@ class PHPFSPOT {
 
    } // rotateImage()
 
 
    } // rotateImage()
 
+   /**
+    * returns flipped image
+    *
+    * this function will return an either horizontal or
+    * vertical flipped truecolor image.
+    */
+   private function flipImage($image, $mode)
+   {
+      $w = imagesx($image);
+      $h = imagesy($image);
+      $flipped = imagecreatetruecolor($w, $h);
+
+      switch($mode) {
+         case 'vert':
+            for ($y = 0; $y < $h; $y++) {
+               imagecopy($flipped, $image, 0, $y, 0, $h - $y - 1, $w, 1);
+            }
+            break;
+         case 'hori':
+            for ($x = 0; $x < $w; $x++) {
+               imagecopy($flipped, $image, $x, 0, $w - $x - 1, 0, 1, $h);
+            }
+            break;
+      }
+
+      return $flipped;
+
+   } // flipImage()
+
    /**
     * return all assigned tags for the specified photo
     */
    /**
     * return all assigned tags for the specified photo
     */
@@ -1674,9 +1691,10 @@ class PHPFSPOT {
    public function _error($text)
    {
       switch($this->cfg->logging) {
    public function _error($text)
    {
       switch($this->cfg->logging) {
+         default:
          case 'display':
             print "<img src=\"resources/green_info.png\" alt=\"warning\" />\n";
          case 'display':
             print "<img src=\"resources/green_info.png\" alt=\"warning\" />\n";
-            print $text;
+            print $text ."<br />\n";
             break;
          case 'errorlog':  
             error_log($text);
             break;
          case 'errorlog':  
             error_log($text);
@@ -1686,6 +1704,8 @@ class PHPFSPOT {
             break;
       }
 
             break;
       }
 
+      $this->runtime_error = true;
+
    } // _error()
 
    /**
    } // _error()
 
    /**
@@ -2014,6 +2034,12 @@ class PHPFSPOT {
                return " ORDER BY basename(p.uri) DESC";
             }
             break;
                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()
       }
 
    } // get_sort_order()
@@ -2264,6 +2290,86 @@ class PHPFSPOT {
 
    } // parse_uri()
 
 
    } // parse_uri()
 
+   /**
+    * validate config options
+    *
+    * this function checks if all necessary configuration options are
+    * specified and set.
+    */
+   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->web_path))
+         $this->web_path.= "/";
+
+      return $this->runtime_error;
+
+   } // check_config_options()
+
 } // class PHPFSPOT
 
 ?>
 } // class PHPFSPOT
 
 ?>