initial import
authorAndreas Unterkircher <unki@netshadow.at>
Mon, 4 Jun 2007 18:32:41 +0000 (18:32 +0000)
committerAndreas Unterkircher <unki@netshadow.at>
Mon, 4 Jun 2007 18:32:41 +0000 (18:32 +0000)
git-svn-id: file:///var/lib/svn/phpfspot/trunk@2 fa6a889d-dae6-447d-9e79-4ba9a3039384

phpfspot.class.php [new file with mode: 0644]
phpfspot_cfg.php [new file with mode: 0644]
phpfspot_db.php [new file with mode: 0644]
phpfspot_img.php [new file with mode: 0644]
phpfspot_tmpl.php [new file with mode: 0644]

diff --git a/phpfspot.class.php b/phpfspot.class.php
new file mode 100644 (file)
index 0000000..d040f13
--- /dev/null
@@ -0,0 +1,127 @@
+<?php
+
+require_once "phpfspot_cfg.php";
+require_once "phpfspot_db.php";
+require_once "phpfspot_tmpl.php";
+
+class PHPFSPOT {
+
+   var $cfg;
+   var $db;
+   var $tmpl;
+   var $tags;
+   var $avail_photos;
+   var $photos;
+   var $current_photo;
+   var $current_tags;
+
+   public function __construct()
+   {
+      $this->cfg = new PHPFSPOT_CFG;
+
+      $this->db = new PHPFSPOT_DB(&$this, $this->cfg->db);
+      $this->tmpl = new PHPFSPOT_TMPL(&$this);
+
+      $this->get_tags();
+      $this->get_photos();
+
+      if(isset($_GET['id']) && is_numeric($_GET['id']))
+         $this->current_photo = $_GET['id'];
+
+   } // __construct()
+
+   public function __destruct()
+   {
+
+   } // __destruct()
+
+   public function show()
+   {
+
+      if(isset($this->current_photo)) {
+         $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $this->avail_photos[$this->current_photo] ."&amp;width=". $this->cfg->photo_width);
+      }
+
+      if($this->current_photo > 0) {
+         $this->tmpl->assign('previous_url', $_SERVER['PHP_SELF'] ."?mode=showphoto&amp;id=". $this->avail_photos[$this->current_photo-1]);
+      }
+
+      if($this->current_photo < count($this->avail_photos)) {
+         $this->tmpl->assign('next_url', $_SERVER['PHP_SELF'] ."?mode=showphoto&amp;id=". $this->avail_photos[$this->current_photo+1]);
+      }
+
+      $this->tmpl->assign('tags', $this->tags);
+      $this->tmpl->show("index.tpl");
+
+
+   } // show()
+
+   private function get_tags()
+   {
+      $result = $this->db->db_query("
+         SELECT id,name
+         FROM tags
+         ORDER BY sort_priority ASC
+      ");
+      
+      while($row = $this->db->db_fetch_object($result)) {
+
+         $tag_id = $row['id'];
+         $tag_name = $row['name'];
+
+         $this->tags[$tag_id] = $tag_name; 
+
+      }
+   } // get_tags()
+
+   private function get_photos()
+   {
+      $this->avail_photos = Array();
+      $count = 0;
+
+      $result = $this->db->db_query("
+         SELECT id, name
+         FROM photos
+         ORDER BY time ASC
+      ");
+      
+      while($row = $this->db->db_fetch_object($result)) {
+
+         $photo_id = $row['id'];
+         $photo_name = $row['name'];
+
+         $this->photos[$photo_id] = $photo_name;
+         $this->avail_photos[$count] = $photo_id;
+
+         if(!isset($this->current_photo))
+            $this->current_photo = $count;
+
+         $count++;
+
+      }
+
+   } // get_photos()
+
+   public function get_photo_details($idx)
+   {
+      $result = $this->db->db_query("
+         SELECT *
+         FROM photos
+         WHERE id='". $idx ."'
+      ");
+      
+      return $this->db->db_fetch_object($result);
+
+   } // get_photo_details
+
+   public function translate_path($path, $width = 0)
+   {  
+      return str_replace($this->cfg->path_replace_from, $this->cfg->path_replace_to, $path);
+
+   } // translate_path
+
+   
+
+}
+
+?>
diff --git a/phpfspot_cfg.php b/phpfspot_cfg.php
new file mode 100644 (file)
index 0000000..b833b99
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+
+class PHPFSPOT_CFG {
+
+   var $page_title = "phpfspot - dynamic PHP gallery for F-Spot";
+
+   var $base_path = "/var/www/images.netshadow.at/htdocs/phpfspot";
+
+   var $web_path = "/phpfspot";
+
+   var $thumb_width = "200";
+   
+   var $photo_width = "800";
+
+   var $db = "/var/www/images.netshadow.at/htdocs/phpfspot/photos.db";
+
+   var $path_replace_from = "/home/unki";
+   var $path_replace_to = "/var/www/images.netshadow.at/htdocs/phpfspot";
+
+}
+
+?>
diff --git a/phpfspot_db.php b/phpfspot_db.php
new file mode 100644 (file)
index 0000000..96648be
--- /dev/null
@@ -0,0 +1,247 @@
+<?php
+
+/***************************************************************************
+ *
+ * Copyright (c) by Andreas Unterkircher, unki@netshadow.at
+ * All rights reserved
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ ***************************************************************************/
+
+/* from pear "MDB2" package. use "pear install MDB2" if you don't have this! */
+require_once('MDB2.php');
+
+class PHPFSPOT_DB {
+
+   private $db;
+   private $db_path;
+   private $parent;
+   private $is_connected;
+   private $last_error;
+
+   /**
+    * PHPFSPOT_DB class constructor
+    *
+    * This constructor initially connect to the database.
+    */
+   public function __construct($parent, $db_path)
+   {
+      $this->parent = $parent;
+      $this->db_path = $db_path;
+
+      /* We are starting disconnected */
+      $this->setConnStatus(false);
+
+      /* Connect to MySQL Database */
+      $this->db_connect();
+
+   } // __construct()
+        
+   /**
+    * PHPFSPOT_DB class deconstructor
+    *
+    * This destructor will close the current database connection.
+    */ 
+   public function __destruct()
+   {
+      $this->db_disconnect();
+
+   } // _destruct()
+
+   /**
+    * PHPFSPOT_DB database connect
+    *
+    * This function will connect to the database via MDB2
+    */
+   private function db_connect()
+   {
+      if(($this->db = sqlite3_open($this->db_path)) === false) {
+
+         $this->throwError("Unable to connect to database:" . $this->getLastError());
+         $this->setConnStatus(false);
+      }
+
+      $this->setConnStatus(true);
+
+   } // db_connect()
+
+   /**
+    * PHPFSPOT_DB database disconnect
+    *
+    * This function will disconnected an established database connection.
+    */
+   private function db_disconnect()
+   {
+      if($this->getConnStatus())
+         sqlite3_close($this->db);
+
+   } // db_disconnect()
+
+   /**
+    * PHPFSPOT_DB database query
+    *
+    * This function will execute a SQL query and return the result as
+    * object.
+    */
+   public function db_query($query = "")
+   {
+      if($this->getConnStatus()) {
+
+         if(($result = sqlite3_query($this->db, $query)) === false)
+            $this->trowError($this->getLastError());
+       
+         return $result;
+      }
+      else 
+         $this->ThrowError("Can't execute query - we are not connected!");
+
+   } // db_query()
+
+   public function db_fetch_object(&$resource)
+   {
+      return sqlite3_fetch_array($resource);
+
+   } // db_fetch_object
+
+   /**
+    * PHPFSPOT_DB fetch ONE row
+    *
+    * This function will execute the given but only return the
+    * first result.
+    */
+   public function db_fetchSingleRow($query = "") 
+   {
+      if($this->getConnStatus()) {
+
+         $result = $this->db_query($query);
+         $row = $result->fetchRow();
+         return $row;
+      }
+      else 
+         $this->ThrowError("Can't fetch row - we are not connected!");
+      
+   } // db_fetchSingleRow()
+
+   /**
+    * PHPFSPOT_DB number of affected rows
+    *
+    * This functions returns the number of affected rows but the
+    * given SQL query.
+    */
+   public function db_getNumRows($query = "")
+   {
+      /* Execute query */
+      $result = $this->db_query($query);
+
+      /* Errors? */
+      if(PEAR::isError($result)) 
+         $this->throwError($result->getMessage());
+
+      return $result->numRows();
+
+   } // db_getNumRows()
+
+   /**
+    * PHPFSPOT_DB get primary key
+    *
+    * This function returns the primary key of the last
+    * operated insert SQL query.
+    */
+   public function db_getid()
+   {
+      /* Get the last primary key ID from execute query */
+      return mysql_insert_id($this->db->connection);
+      
+   } // db_getid()
+
+   /**
+    * PHPFSPOT_DB check table exists
+    *
+    * This function checks if the given table exists in the
+    * database
+    * @param string, table name
+    * @return true if table found otherwise false
+    */
+   public function db_check_table_exists($table_name = "")
+   {
+      if($this->getConnStatus()) {
+         $result = $this->db_query("SHOW TABLES");
+         $tables_in = "Tables_in_". MYSQL_DB;
+       
+         while($row = $result->fetchRow()) {
+            if($row->$tables_in == $table_name)
+               return true;
+         }
+         return false;
+      }
+      else
+         $this->ThrowError("Can't check table - we are not connected!");
+        
+   } // db_check_table_exists()
+
+   /**
+    * PHPFSPOT_DB get connection status
+    *
+    * This function checks the internal state variable if already
+    * connected to database.
+    */
+   private function setConnStatus($status)
+   {
+      $this->is_connected = $status;
+      
+   } // setConnStatus()
+
+   /**
+    * PHPFSPOT_DB set connection status
+    * This function sets the internal state variable to indicate
+    * current database connection status.
+    */
+   private function getConnStatus()
+   {
+      return $this->is_connected;
+
+   } // getConnStatus()
+
+   /**
+    * PHPFSPOT_DB throw error
+    *
+    * This function shows up error messages and afterwards through exceptions.
+    */
+   private function ThrowError($string)
+   {
+      if(!defined('DB_NOERROR'))  {
+         print "<br /><br />". $string ."<br /><br />\n";
+         try {
+            throw new Exception;
+         }
+         catch(Exectpion $e) {
+         }
+      }
+
+      $this->last_error = $string;
+        
+   } // ThrowError()
+
+   private function getLastError()
+   {
+
+      return sqlite3_error_string(sqlite3_last_error($this->db));
+
+   } // getLastError()
+
+}
+
+?>
diff --git a/phpfspot_img.php b/phpfspot_img.php
new file mode 100644 (file)
index 0000000..3a2a322
--- /dev/null
@@ -0,0 +1,113 @@
+<?php
+
+require_once "phpfspot_db.php";
+require_once "phpfspot.class.php";
+
+class PHPFSPOT_IMG {
+   
+   var $db;
+   var $parent;
+
+   public function __construct()
+   {
+      $this->parent = new PHPFSPOT;
+      $this->db = $this->parent->db;
+
+   } // __construct()
+
+   public function __destruct()
+   {
+
+   } // __desctruct()
+
+   public function show($idx, $width = "")
+   {
+      $details = $this->parent->get_photo_details($idx);
+
+               foreach(Array($this->parent->cfg->thumb_width, $this->parent->cfg->photo_width) as $resolution)
+                 $this->resize_image($this->parent->translate_path($details['directory_path'])  ."/". $details['name'], $resolution);
+
+      if($width == "")
+         $fullpath = $this->parent->translate_path($details['directory_path'])  ."/". $details['name'];
+      else
+         $fullpath = $this->parent->translate_path($details['directory_path'])  ."/thumbs/". $width ."_". $details['name'];
+
+
+      $tmp = getimagesize($fullpath);
+      $mime = $tmp['mime'];
+
+      Header("Content-Type: ". $mime);
+      Header("Content-Length: ". filesize($fullpath));
+      Header("Content-Transfer-Encoding: binary\n");              
+      $user_agent = strtolower ($_SERVER["HTTP_USER_AGENT"]);     
+      Header("Content-Disposition: inline; filename=\"". $details['name'] ."\"");
+      Header("Accept-Ranges: bytes");                             
+      Header("Connection: close");                                
+   
+      $file = fopen($fullpath, "rb");
+      fpassthru($file);
+      @fclose($file);
+
+   } // show()
+
+   public function resize_image($image, $width)
+   {
+      $src_img = @imagecreatefromjpeg($image);
+        
+      if($src_img)
+      {
+         /* grabs the height and width */
+         $new_w = imagesx($src_img);
+         $new_h = imagesy($src_img);
+                
+         // If requested width is more then the actual image width,
+         // do not generate a thumbnail
+         
+         if($width >= $new_w) {
+            imagedestroy($src_img);
+            return;
+         }
+                
+         /* calculates aspect ratio */
+         $aspect_ratio = $new_h / $new_w;
+                
+         /* sets new size */
+         $new_w = $width;
+         $new_h = abs($new_w * $aspect_ratio);
+                
+         /* creates new image of that size */
+         $dst_img = imagecreatetruecolor($new_w,$new_h);
+
+         imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
+                
+         /* copies resized portion of original image into new image */
+         imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
+                
+         /* write down new generated file */
+         
+         if(!file_exists(dirname($image) ."/thumbs"))
+            mkdir(dirname($image) ."/thumbs");
+
+         $newfile = dirname($image) ."/thumbs/". $width ."_". basename($image);
+         imagejpeg($dst_img, $newfile, 75);
+                
+         /* free your mind */
+         imagedestroy($dst_img);
+         imagedestroy($src_img);
+      }
+
+   } // resize_image()
+}
+
+if(isset($_GET['idx']) && is_numeric($_GET['idx'])) {
+
+   if(isset($_GET['width']) && is_numeric($_GET['width'])) 
+      $width = $_GET['width'];
+   else
+      $width = "";
+
+   $img = new PHPFSPOT_IMG;
+   $img->show($_GET['idx'], $width);
+}
+
+?>
diff --git a/phpfspot_tmpl.php b/phpfspot_tmpl.php
new file mode 100644 (file)
index 0000000..4dd24e6
--- /dev/null
@@ -0,0 +1,27 @@
+<?php
+
+require 'smarty/libs/Smarty.class.php';
+
+class PHPFSPOT_TMPL extends Smarty {
+
+   var $parent;
+
+   public function __construct($parent)
+   {
+      $this->Smarty();
+      $this->template_dir = $parent->cfg->base_path .'/templates';
+      $this->config_dir   = $parent->cfg->base_path .'/smarty_config';
+      $this->compile_dir  = $parent->cfg->base_path .'/templates_c';
+      $this->cache_dir    = $parent->cfg->base_path .'/smarty_cache';
+
+   } // __construct()
+
+   public function show($template)
+   {
+      $this->display($template);
+
+   } // show()
+
+}
+
+?>