issue43, also return a thumbnail if requested width is below image size
[phpfspot.git] / phpfspot.class.php
index 8fe0f77e40824092049fa1b7eb0864cd7a05ab71..00c84dbd9884e4d074972ea6fd8f5c589b192514 100644 (file)
@@ -23,6 +23,12 @@ class PHPFSPOT {
       $this->cfg = new PHPFSPOT_CFG;
 
       $this->db  = new PHPFSPOT_DB(&$this, $this->cfg->fspot_db);
+      
+      if(!is_writeable(dirname($this->cfg->phpfspot_db))) {
+         print dirname($this->cfg->phpfspot_db) .": directory is not writeable!";
+         exit(1);
+      }
+         
       $this->cfg_db = new PHPFSPOT_DB(&$this, $this->cfg->phpfspot_db);
       $this->check_config_table();
 
@@ -54,21 +60,35 @@ class PHPFSPOT {
       $this->tmpl->assign('searchfor', $_SESSION['searchfor']);
       $this->tmpl->assign('page_title', $this->cfg->page_title);
       $this->tmpl->assign('current_condition', $_SESSION['tag_condition']);
-      $this->tmpl->assign('from_date', $this->get_calendar());
-      $this->tmpl->assign('to_date', $this->get_calendar());
+
+      $_SESSION['start_action'] = $_GET['mode'];
 
       switch($_GET['mode']) {
          case 'showpi':
             if(isset($_GET['tags'])) {
                $_SESSION['selected_tags'] = split(',', $_GET['tags']);
             }
+            if(isset($_GET['from_date'])) {
+               $_SESSION['from_date'] = $_GET['from_date'];
+            }
+            if(isset($_GET['to_date'])) {
+               $_SESSION['to_date'] = $_GET['to_date'];
+            }
             break;
          case 'showp':
             if(isset($_GET['tags'])) {
                $_SESSION['selected_tags'] = split(',', $_GET['tags']);
+               $_SESSION['start_action'] = 'showp';
             }
             if(isset($_GET['id'])) {
                $_SESSION['current_photo'] = $_GET['id'];
+               $_SESSION['start_action'] = 'showp';
+            }
+            if(isset($_GET['from_date'])) {
+               $_SESSION['from_date'] = $_GET['from_date'];
+            }
+            if(isset($_GET['to_date'])) {
+               $_SESSION['to_date'] = $_GET['to_date'];
             }
             break;
          case 'export':
@@ -78,6 +98,8 @@ class PHPFSPOT {
 
       }
 
+      $this->tmpl->assign('from_date', $this->get_calendar('from'));
+      $this->tmpl->assign('to_date', $this->get_calendar('to'));
       $this->tmpl->assign('content_page', 'welcome.tpl');
       $this->tmpl->show("index.tpl");
 
@@ -169,6 +191,12 @@ class PHPFSPOT {
       }
 
       $details = $this->get_photo_details($photo);
+
+      if(!$details) {
+         print "error";
+         return;
+      }
+
       $orig_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
       $thumb_path = $this->cfg->base_path ."/thumbs/". $this->cfg->photo_width ."_". $this->getMD5($photo);
 
@@ -177,12 +205,13 @@ class PHPFSPOT {
       }
 
       if(!is_readable($orig_path)) {
-         $this->_warning("Photo ". $orig_path ." is not readable for user ". get_current_user() ."<br />\n");
+         $this->_warning("Photo ". $orig_path ." is not readable for user ". $this->getuid() ."<br />\n");
       }
 
       /* If the thumbnail doesn't exist yet, try to create it */
       if(!file_exists($thumb_path)) {
          $this->gen_thumb($photo, true);
+         $thumb_path = $this->cfg->base_path ."/thumbs/". $this->cfg->photo_width ."_". $this->getMD5($photo);
       }
 
       $meta = $this->get_meta_informations($orig_path);
@@ -204,6 +233,9 @@ class PHPFSPOT {
       if($current_tags != "") {
          $extern_link.= "&tags=". $current_tags;
       }
+      if(isset($_SESSION['from_date']) && isset($_SESSION['to_date'])) {
+         $extern_link.= "&from_date=". $_SESSION['from_date'] ."&to_date=". $_SESSION['to_date'];
+      }
 
       $this->tmpl->assign('extern_link', $extern_link);
 
@@ -229,6 +261,7 @@ class PHPFSPOT {
       }
       else {
          $this->_warning("Can't open file ". $thumb_path ."\n");
+         return;
       }
 
       if($previous_img) {
@@ -319,6 +352,9 @@ class PHPFSPOT {
 
    public function addTag($tag)
    {
+      // if the result of a date search are displayed, reset them
+      $this->resetDateSearch();
+
       if(!isset($_SESSION['selected_tags']))
          $_SESSION['selected_tags'] = Array();
 
@@ -339,25 +375,37 @@ class PHPFSPOT {
 
    public function resetTags()
    {
-      unset($_SESSION['selected_tags']);
+      if(isset($_SESSION['selected_tags']))
+         unset($_SESSION['selected_tags']);
 
    } // resetTags()
 
    public function resetPhotoView()
    {
-      unset($_SESSION['current_photo']);
+      if(isset($_SESSION['current_photo']))
+         unset($_SESSION['current_photo']);
 
    } // resetPhotoView();
 
    public function resetTagSearch()
    {
-      unset($_SESSION['searchfor']);
+      if(isset($_SESSION['searchfor']))
+         unset($_SESSION['searchfor']);
 
    } // resetTagSearch()
 
+   public function resetDateSearch()
+   {
+      if(isset($_SESSION['from_date']))
+         unset($_SESSION['from_date']);
+      if(isset($_SESSION['to_date']))
+         unset($_SESSION['to_date']);
+
+   } // resetDateSearch();
+
    public function getPhotoSelection()
    {  
-      $tagged_photos = Array();
+      $matched_photos = Array();
 
       /* return a search result */
       if(isset($_SESSION['searchfor']) && $_SESSION['searchfor'] != '') {
@@ -372,9 +420,9 @@ class PHPFSPOT {
                ORDER BY p.time ASC
          ");
          while($row = $this->db->db_fetch_object($result)) {
-            array_push($tagged_photos, $row['photo_id']);
+            array_push($matched_photos, $row['photo_id']);
          }
-         return $tagged_photos;
+         return $matched_photos;
       }
 
       /* return according the selected tags */
@@ -432,11 +480,31 @@ class PHPFSPOT {
          }
 
          while($row = $this->db->db_fetch_object($result)) {
-            array_push($tagged_photos, $row['photo_id']);
+            array_push($matched_photos, $row['photo_id']);
          }
-         return $tagged_photos;
+         return $matched_photos;
       }
 
+      if(isset($_SESSION['from_date']) && isset($_SESSION['to_date'])) {
+         $from_date = strtotime($_SESSION['from_date']);
+         $to_date = strtotime($_SESSION['to_date']);
+         $result = $this->db->db_query("
+            SELECT DISTINCT photo_id
+               FROM photo_tags pt
+            INNER JOIN photos p
+               ON p.id=pt.photo_id
+            WHERE 
+               time>='". $from_date ."'
+            AND
+               time<='". $to_date ."'
+            ORDER BY p.time ASC
+         ");
+         while($row = $this->db->db_fetch_object($result)) {
+            array_push($matched_photos, $row['photo_id']);
+         }
+         return $matched_photos;
+      } 
+
       /* return all available photos */
       $result = $this->db->db_query("
          SELECT DISTINCT photo_id
@@ -446,9 +514,9 @@ class PHPFSPOT {
          ORDER BY p.time ASC
       ");
       while($row = $this->db->db_fetch_object($result)) {
-         array_push($tagged_photos, $row['photo_id']);
+         array_push($matched_photos, $row['photo_id']);
       }
-      return $tagged_photos;
+      return $matched_photos;
 
    } // getPhotoSelection()
 
@@ -612,6 +680,9 @@ class PHPFSPOT {
       if($current_tags != "") {
          $extern_link.= "&tags=". $current_tags;
       }
+      if(isset($_SESSION['from_date']) && isset($_SESSION['to_date'])) {
+         $extern_link.= "&from_date=". $_SESSION['from_date'] ."&to_date=". $_SESSION['to_date'];
+      }
 
       $export_link = "index.php?mode=export";
 
@@ -645,8 +716,9 @@ class PHPFSPOT {
 
    public function create_thumbnail($orig_image, $thumb_image, $width)
    {  
-      if(!file_exists($orig_image))
+      if(!file_exists($orig_image)) {
          return false;
+      }
 
       $details = getimagesize($orig_image);
       
@@ -691,9 +763,11 @@ class PHPFSPOT {
       $cur_height = imagesy($src_img);
 
       // If requested width is more then the actual image width,
-      // do not generate a thumbnail
+      // do not generate a thumbnail, instead safe the original
+      // as thumbnail but with lower quality
 
       if($width >= $cur_width) {
+         $result = imagejpeg($src_img, $thumb_image, 75);
          imagedestroy($src_img);
          return true;
       }
@@ -828,7 +902,7 @@ class PHPFSPOT {
       }
 
       if(!is_readable($full_path)) {
-         $this->_warning("File ". $full_path ." is not readable for ". get_current_user() ."\n");
+         $this->_warning("File ". $full_path ." is not readable for ". $this->getuid() ."\n");
          return;
       }
 
@@ -847,7 +921,6 @@ class PHPFSPOT {
             if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
                $error = 1;
          }
-
          /* if the file hasn't changed there is no need to regen the thumb */
          elseif($file_md5 != $this->getMD5($idx) || $force) {
 
@@ -908,8 +981,16 @@ class PHPFSPOT {
             array_push($_SESSION['selected_tags'], $tag);
       }
 
+      $this->resetDateSearch();
+
    } // startTagSearch()
 
+   public function startDateSearch($from, $to)
+   {
+      $_SESSION['from_date'] = $from;
+      $_SESSION['to_date'] = $to;
+   }
+
    private function rotateImage($img, $degrees)
    {
       if(function_exists("imagerotate"))
@@ -1082,37 +1163,94 @@ class PHPFSPOT {
 
    } // _warning()
 
-   private function get_calendar()
+   private function get_calendar($mode)
    {
-//      require_once CALENDAR_ROOT.'Year.php';
-//      require_once CALENDAR_ROOT.'Month.php';
-//      require_once CALENDAR_ROOT.'Day.php';
+      $year = $_SESSION[$mode .'_date'] ? date("Y", strtotime($_SESSION[$mode .'_date'])) : date("Y");
+      $month = $_SESSION[$mode .'_date'] ? date("m", strtotime($_SESSION[$mode .'_date'])) : date("m");
+      $day = $_SESSION[$mode .'_date'] ? date("d", strtotime($_SESSION[$mode .'_date'])) : date("d");
 
-//      $Year = new Calendar_Year();
-//      $Month = new Calendar_Month();
-//      $Day = new Calendar_Day();
+      $output = "<input type=\"text\" size=\"3\" id=\"". $mode ."year\" value=\"". $year ."\" />\n";
+      $output.= "<input type=\"text\" size=\"1\" id=\"". $mode ."month\" value=\"". $month ."\" />\n";
+      $output.= "<input type=\"text\" size=\"1\" id=\"". $mode ."day\" value=\"". $day ."\" />\n";
+      return $output;
 
-      $output = "<select name=\"year\">\n";
-      for($year = 1990; $year <= date("Y"); $year++) {
-         $output.= "<option value=\"". $year ."\">". $year ."</option>\n";
-      }
-      $output.= "</select>\n";
+   } // get_calendar()
 
-      $output.= "<select name=\"month\">\n";
-      for($month = 1; $month <= 12; $month++) {
-         $output.= "<option value=\"". $month ."\">". $month ."</option>\n";
-      }
-      $output.= "</select>\n";
+   public function get_calendar_matrix($year = 0, $month = 0, $day = 0)
+   {
+      if (!isset($year)) $year = date('Y');
+      if (!isset($month)) $month = date('m');
+      if (!isset($day)) $day = date('d');
+      $rows = 1;
+      $cols = 1;
+      $matrix = Array();
 
-      $output.= "<select name=\"day\">\n";
-      for($day = 1; $day <= 31; $day++) {
-         $output.= "<option value=\"". $day ."\">". $day ."</option>\n";
+      require_once CALENDAR_ROOT.'Month/Weekdays.php';
+      require_once CALENDAR_ROOT.'Day.php';
+
+
+      // Build the month
+      $month = new Calendar_Month_Weekdays($year,$month);
+
+      // Create links
+      $prevStamp = $month->prevMonth(true);
+      $prev = "javascript:setMonth(". date('Y',$prevStamp) .", ". date('n',$prevStamp) .", ". date('j',$prevStamp) .");";
+      $nextStamp = $month->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);
+
+      $this->tmpl->assign('current_month', date('F Y',$month->getTimeStamp()));
+      $this->tmpl->assign('prev_month', $prev);
+      $this->tmpl->assign('next_month', $next);
+
+      while ( $day = $month->fetch() ) {
+   
+         if(!isset($matrix[$rows]))
+            $matrix[$rows] = Array();
+
+         $string = "";
+
+         $dayStamp = $day->thisDay(true);
+         $link = "javascript:setCalendarDate(". date('Y',$dayStamp) .", ". date('n',$dayStamp).", ". date('j',$dayStamp) .");";
+
+         // isFirst() to find start of week
+         if ( $day->isFirst() )
+            $string.= "<tr>\n";
+
+         if ( $day->isSelected() ) {
+            $string.= "<td class=\"selected\">".$day->thisDay()."</td>\n";
+         } else if ( $day->isEmpty() ) {
+            $string.= "<td>&nbsp;</td>\n";
+         } else {
+            $string.= "<td><a class=\"calendar\" href=\"".$link."\">".$day->thisDay()."</a></td>\n";
+         }
+
+         // isLast() to find end of week
+         if ( $day->isLast() )
+            $string.= "</tr>\n";
+
+         $matrix[$rows][$cols] = $string;
+
+         $cols++;
+
+         if($cols > 7) {
+            $cols = 1;
+            $rows++;
+         }
       }
-      $output.= "</select>\n";
 
-      return $output;
+      $this->tmpl->assign('matrix', $matrix);
+      $this->tmpl->assign('rows', $rows);
+      $this->tmpl->show("calendar.tpl");
 
-   } // get_calendar()
+   } // get_calendar_matrix()
 
    public function getExport($mode)
    {
@@ -1130,6 +1268,9 @@ class PHPFSPOT {
          if($current_tags != "") {
             $orig_url.= "&tags=". $current_tags;
          } 
+         if(isset($_SESSION['from_date']) && isset($_SESSION['to_date'])) {
+            $orig_url.= "&from_date=". $_SESSION['from_date'] ."&to_date=". $_SESSION['to_date'];
+         }
 
          $thumb_url = $protocol ."://". $server_name . $this->cfg->web_path ."phpfspot_img.php?idx=". $picture ."&width=". $this->cfg->thumb_width;
 
@@ -1169,6 +1310,37 @@ class PHPFSPOT {
       }
    } // getCurrentPhoto()
 
+   public function whatToDo()
+   {
+      if(isset($_SESSION['selected_tags']) && !empty($_SESSION['selected_tags'])) {
+         return "showpi_tags";
+      }
+      elseif(isset($_SESSION['from_date']) && isset($_SESSION['to_date'])) {
+         return "showpi_date";
+      }
+      elseif(isset($_SESSION['current_photo'])) {
+         return "show_photo";
+      }
+      elseif(isset($_SESSION['start_action']) && $_SESSION['start_action'] == 'showpi') {
+         return "showpi";
+      }
+
+      return "nothing special";
+
+   } // whatToDo()
+
+   private function getuid()
+   {
+      if($uid = posix_getuid()) {
+         if($user = posix_getpwuid($uid)) {
+            return $user['name'];
+         }
+      }
+   
+      return 'n/a';
+   
+   } // getuid()
+
 }
 
 ?>