check if parameter of ts2str is really numeric
[phpfspot.git] / phpfspot_img.php
1 <?php
2
3 /***************************************************************************
4  *
5  * phpfspot, presents your F-Spot photo collection in Web browsers.
6  *
7  * Copyright (c) by Andreas Unterkircher
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  ***************************************************************************/
24
25 require_once "phpfspot.class.php";
26
27 /**
28  * PHPFSPOT_IMG class
29  *
30  * handles phpfspot's photos. It will output either the photo binaries
31  * or can also show error messages as a on-the-fly generated picture.
32  * @package phpfspot
33  */
34 class PHPFSPOT_IMG {
35    
36    private $db;
37    private $parent;
38
39    /**
40     * PHPFSPOT_IMG class constructor
41     */
42    public function __construct()
43    {
44       $this->parent = new PHPFSPOT;
45       $this->db = $this->parent->db;
46
47    } // __construct()
48
49    /**
50     * PHPFSPOT_IMG class destructor
51     */
52    public function __destruct()
53    {
54
55    } // __desctruct()
56
57    /**
58     * sends the specified image to the browser
59     *
60     * this function will send the specified image to 
61     * the client - in the specified width. it also try's
62     * to create on-the-fly missing thumbnails via PHPFSPOT
63     * gen_thumbs function.
64     * @param integer $idx
65     * @param integer $width
66     */
67    public function showImg($idx, $width = 0)
68    {
69       if($idx == 'rand')
70          $idx = $this->parent->get_random_photo();
71
72       $details = $this->parent->get_photo_details($idx);
73    
74       if(!$details) {
75          $this->parent->showTextImage("The image (". $idx .") you requested is unknown");
76          return;
77       }
78
79       /* no width specified - show photo in its original size */
80       if($width == 0) {
81          $fullpath = $this->parent->translate_path($this->parent->parse_uri($details['uri'], 'fullpath'));
82       }
83       /* show thumbnail */
84       else {
85          /* if no entry for this photo is yet in the database, create thumb */
86          if(!$this->parent->getMD5($idx)) {
87             $this->parent->gen_thumb($idx);
88          }
89          $fullpath = $this->parent->get_thumb_path($width, $idx);
90          /* if the thumb file does not exist, create it */
91          if(!file_exists($fullpath)) {
92             $this->parent->gen_thumb($idx);
93          }
94       }
95
96       if(!file_exists($fullpath)) {
97          $this->parent->showTextImage("File ". basename($fullpath) ." does not exist");
98          return;
99       }
100       if(!is_readable($fullpath)) {
101          $this->parent->showTextImage("File ". basename($fullpath) ." is not readable. Check the permissions");
102          return;
103       } 
104       $mime = $this->parent->get_mime_info($fullpath);
105
106       if(!$this->parent->checkifImageSupported($mime)) {
107          $this->parent->showTextImage("Unsupported Image Type");
108          return;
109       }
110
111       Header("Content-Type: ". $mime);
112       Header("Content-Length: ". filesize($fullpath));
113       Header("Content-Transfer-Encoding: binary\n");
114       Header("Content-Disposition: inline; filename=\"". $this->parent->parse_uri($details['uri'], 'filename') ."\"");
115       Header("Content-Description: ". $this->parent->parse_uri($details['uri'], 'filename'));
116       Header("Accept-Ranges: bytes");
117       Header("Connection: close");
118       Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
119       Header("Cache-Control: no-cache");
120       Header("Pragma: no-cache");
121
122       $file = fopen($fullpath, "rb");
123       fpassthru($file);
124       @fclose($file);
125
126    } // showImg()
127
128    /**
129     * sends a random photo of the requested tag to the browser
130     *
131     * this function will send a random photo to the client.
132     * It is selected out by the provided $tagidx. It also try's
133     * to create on-the-fly missing thumbnails via PHPFSPOT
134     * gen_thumbs function.
135     * @param integer $idx
136     */
137    public function showTagImg($tagidx)
138    {
139       $idx = $this->parent->get_random_tag_photo($tagidx);
140       $width = 30;
141
142       $details = $this->parent->get_photo_details($idx);
143
144       if(!$details) {
145          $this->parent->showTextImage("The image (". $idx .") you requested is unknown");
146          return;
147       }
148
149       /* if no entry for this photo is yet in the database, create thumb */
150       if(!$this->parent->getMD5($idx)) {
151          $this->parent->gen_thumb($idx);
152       }
153       $fullpath = $this->parent->get_thumb_path($width, $idx);
154       /* if the thumb file does not exist, create it */
155       if(!file_exists($fullpath)) {
156          $this->parent->gen_thumb($idx);
157       }
158
159       if(!file_exists($fullpath)) {
160          $this->parent->showTextImage("File ". basename($fullpath) ." does not exist");
161          return;
162       }
163       if(!is_readable($fullpath)) {
164          $this->parent->showTextImage("File ". basename($fullpath) ." is not readable. Check the permissions");
165          return;
166       }
167
168       $mime = $this->parent->get_mime_info($fullpath);
169
170       if(!$this->parent->checkifImageSupported($mime)) {
171          $this->parent->showTextImage("Unsupported Image Type");
172          return;
173       }
174
175       Header("Content-Type: ". $mime);
176       Header("Content-Length: ". filesize($fullpath));
177       Header("Content-Transfer-Encoding: binary\n");
178       Header("Content-Disposition: inline; filename=\"". $this->parent->parse_uri($details['uri'], 'filename') ."\"");
179       Header("Content-Description: ". $this->parent->parse_uri($details['uri'], 'filename'));
180       Header("Accept-Ranges: bytes");
181       Header("Connection: close");
182       Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
183       Header("Cache-Control: no-cache");
184       Header("Pragma: no-cache");
185
186       $file = fopen($fullpath, "rb");
187       fpassthru($file);
188       @fclose($file);
189
190    } // showTagImg()
191
192 } // PHPFSPOT_IMG()
193
194 if(isset($_GET['idx']) && (is_numeric($_GET['idx']) || $_GET['idx'] == 'rand')) {
195
196    $img = new PHPFSPOT_IMG;
197
198    if(isset($_GET['width']) && is_numeric($_GET['width'])) 
199       $img->showImg($_GET['idx'], $_GET['width']);
200    else
201       $img->showImg($_GET['idx']);
202
203    exit(0);
204 }
205
206 if(isset($_GET['tagidx']) && is_numeric($_GET['tagidx'])) {
207
208    $img = new PHPFSPOT_IMG;
209    $img->showTagImg($_GET['tagidx']);
210
211    exit(0);
212
213 }
214
215
216 ?>