root / phpfspot.class.php

View | Annotate | Download (118.3 KB)

1 61f2970e Andreas
 <?php
2 61f2970e Andreas
3 6e2d319e Andreas
 /***************************************************************************
4 6e2d319e Andreas
  *
5 7617d447 Andreas
  * phpfspot, presents your F-Spot photo collection in Web browsers.
6 7617d447 Andreas
  *
7 7617d447 Andreas
  * Copyright (c) by Andreas Unterkircher
8 6e2d319e Andreas
  *
9 6e2d319e Andreas
  *  This program is free software; you can redistribute it and/or modify
10 6e2d319e Andreas
  *  it under the terms of the GNU General Public License as published by
11 6e2d319e Andreas
  *  the Free Software Foundation; either version 2 of the License, or
12 6e2d319e Andreas
  *  any later version.
13 6e2d319e Andreas
  *
14 6e2d319e Andreas
  *  This program is distributed in the hope that it will be useful,
15 6e2d319e Andreas
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 6e2d319e Andreas
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 6e2d319e Andreas
  *  GNU General Public License for more details.
18 6e2d319e Andreas
  *
19 6e2d319e Andreas
  *  You should have received a copy of the GNU General Public License
20 6e2d319e Andreas
  *  along with this program; if not, write to the Free Software
21 6e2d319e Andreas
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 6e2d319e Andreas
  *
23 6e2d319e Andreas
  ***************************************************************************/
24 6e2d319e Andreas
25 61f2970e Andreas
 require_once "phpfspot_cfg.php";
26 61f2970e Andreas
 require_once "phpfspot_db.php";
27 61f2970e Andreas
28 2765fc08 Andreas
 /**
29 2765fc08 Andreas
  * PHPFSPOT main class
30 2765fc08 Andreas
  *
31 55677896 Andreas
  * this class contains the most functions which will to the major
32 55677896 Andreas
  * work for phpfspot.
33 55677896 Andreas
  *
34 2765fc08 Andreas
  * @package phpfspot
35 2765fc08 Andreas
  */
36 61f2970e Andreas
 class PHPFSPOT {
37 61f2970e Andreas
38 2765fc08 Andreas
    /**
39 2765fc08 Andreas
      * phpfspot configuration
40 2765fc08 Andreas
      * @access public
41 2765fc08 Andreas
      * @see PHPFSPOT_CFG()
42 2765fc08 Andreas
      * @var PHPFSPOT_CFG
43 2765fc08 Andreas
      */
44 61f2970e Andreas
    var $cfg;
45 2765fc08 Andreas
46 2765fc08 Andreas
    /**
47 2765fc08 Andreas
      * SQLite database handle to f-spot database
48 2765fc08 Andreas
      * @see PHPFSPOT_DB()
49 2765fc08 Andreas
      * @access public
50 2765fc08 Andreas
      * @var PHPFSPOT_DB
51 2765fc08 Andreas
      */
52 61f2970e Andreas
    var $db;
53 2765fc08 Andreas
54 2765fc08 Andreas
    /**
55 2765fc08 Andreas
      * SQLite database handle to phpfspot database
56 2765fc08 Andreas
      * @see PHPFSPOT_DB()
57 2765fc08 Andreas
      * @access public
58 2765fc08 Andreas
      * @var PHPFSPOT_DB
59 2765fc08 Andreas
      */
60 abc9aff6 Andreas
    var $cfg_db;
61 2765fc08 Andreas
62 2765fc08 Andreas
    /**
63 2765fc08 Andreas
     * Smarty template engine
64 2765fc08 Andreas
     * @link http://smarty.php.net smarty.php.net
65 2765fc08 Andreas
     * @see PHPFSPOT_TMPL()
66 2765fc08 Andreas
     * @access public
67 2765fc08 Andreas
     * @var PHPFSPOT_TMPL
68 2765fc08 Andreas
     */
69 61f2970e Andreas
    var $tmpl;
70 2765fc08 Andreas
71 2765fc08 Andreas
    /**
72 2765fc08 Andreas
     * full tag - list
73 2765fc08 Andreas
     * @access public
74 2765fc08 Andreas
     * @var array
75 2765fc08 Andreas
     */
76 61f2970e Andreas
    var $tags;
77 2765fc08 Andreas
78 2765fc08 Andreas
    /**
79 2765fc08 Andreas
     * list of available, not-selected, tags
80 2765fc08 Andreas
     * @access public
81 2765fc08 Andreas
     * @var array
82 2765fc08 Andreas
     */
83 193a2982 Andreas
    var $avail_tags;
84 42d16009 Andreas
85 2765fc08 Andreas
    /**
86 2765fc08 Andreas
     * true if runtime error occued
87 2765fc08 Andreas
     * @access private
88 2765fc08 Andreas
     * @var boolean
89 2765fc08 Andreas
     */
90 42d16009 Andreas
    private $runtime_error = false;
91 2765fc08 Andreas
92 2765fc08 Andreas
    /**
93 2765fc08 Andreas
     * F-Spot database version
94 2765fc08 Andreas
     * @access private
95 2765fc08 Andreas
     * @var integer
96 2765fc08 Andreas
     */
97 e8bc8f7d Andreas
    private $dbver;
98 61f2970e Andreas
99 7f50c5e3 Andreas
    /**
100 2765fc08 Andreas
     * class constructor ($cfg, $db, $cfg_db, $tmpl, $db_ver)
101 7f50c5e3 Andreas
     *
102 7f50c5e3 Andreas
     * this function will be called on class construct
103 7f50c5e3 Andreas
     * and will check requirements, loads configuration,
104 7f50c5e3 Andreas
     * open databases and start the user session
105 7f50c5e3 Andreas
     */
106 61f2970e Andreas
    public function __construct()
107 61f2970e Andreas
    {
108 ae99d62d Andreas
       /**
109 ae99d62d Andreas
        * register PHPFSPOT class global
110 ae99d62d Andreas
        *
111 ae99d62d Andreas
        * @global PHPFSPOT $GLOBALS['phpfspot']
112 ae99d62d Andreas
        * @name $phpfspot
113 ae99d62d Andreas
        */
114 ae99d62d Andreas
       $GLOBALS['phpfspot'] =& $this;
115 ae99d62d Andreas
116 c6c2fa93 Andreas
       $this->cfg = new PHPFSPOT_CFG;
117 c6c2fa93 Andreas
118 42d16009 Andreas
       /* verify config settings */
119 42d16009 Andreas
       if($this->check_config_options()) {
120 42d16009 Andreas
          exit(1);
121 42d16009 Andreas
       }
122 42d16009 Andreas
123 3e95994d Andreas
       /* set application name and version information */
124 3e95994d Andreas
       $this->cfg->product = "phpfspot";
125 88e26f8f Andreas
       $this->cfg->version = "1.7";
126 6198d8ac Andreas
       $this->cfg->db_version = 2;
127 3e95994d Andreas
128 5bc35081 Andreas
       $this->sort_orders= array(
129 5bc35081 Andreas
          'date_asc' => 'Date &uarr;',
130 5bc35081 Andreas
          'date_desc' => 'Date &darr;',
131 5bc35081 Andreas
          'name_asc' => 'Name &uarr;',
132 811a18a5 Andreas
          'name_desc' => 'Name &darr;',
133 811a18a5 Andreas
          'tags_asc' => 'Tags &uarr;',
134 811a18a5 Andreas
          'tags_desc' => 'Tags &darr;',
135 5bc35081 Andreas
       );
136 5bc35081 Andreas
137 605439cc Andreas
       /* Check necessary requirements */
138 ef48f193 Andreas
       if(!$this->check_requirements()) {
139 605439cc Andreas
          exit(1);
140 605439cc Andreas
       }
141 605439cc Andreas
142 1532cb59 Andreas
       /******* Opening F-Spot's sqlite database *********/
143 1532cb59 Andreas
144 79362554 Andreas
       /* Check if database file exists and is readable */
145 79362554 Andreas
       if(!file_exists($this->cfg->fspot_db) || !is_readable($this->cfg->fspot_db)) {
146 79362554 Andreas
          print "Error: ". $this->cfg->fspot_db ." does not exist or is not readable for user ". $this->getuid() .".\n";
147 79362554 Andreas
          exit(1);
148 79362554 Andreas
       }
149 79362554 Andreas
150 1532cb59 Andreas
       /* Check if database file is writeable */
151 39507eea Andreas
       if(!is_writeable($this->cfg->fspot_db)) {
152 c74587e3 Andreas
          print "Error: ". $this->cfg->fspot_db ." is not writeable for user ". $this->getuid() .".\n";
153 c74587e3 Andreas
          print "Please fix permissions so phpfspot can create indices within the F-Spot database to"
154 c74587e3 Andreas
             ." speed up some database operations.\n";
155 39507eea Andreas
          exit(1);
156 39507eea Andreas
       }
157 e8bc8f7d Andreas
158 1532cb59 Andreas
       /* open the database */
159 1532cb59 Andreas
       $this->db  = new PHPFSPOT_DB($this, $this->cfg->fspot_db);
160 e8bc8f7d Andreas
161 1532cb59 Andreas
       /* change sqlite temp directory, if requested */
162 1532cb59 Andreas
       if(isset($this->cfg->sqlite_temp_dir)) {
163 1532cb59 Andreas
          $this->db->db_exec("
164 1532cb59 Andreas
             PRAGMA
165 1532cb59 Andreas
                temp_store_directory = '". $this->cfg->sqlite_temp_dir ."'
166 1532cb59 Andreas
          ");
167 22ac9327 Andreas
       }
168 e2471df5 Andreas
169 aab7d4d0 Andreas
       /* get F-Spot database version */
170 1532cb59 Andreas
       $this->dbver = $this->getFspotDBVersion();
171 1532cb59 Andreas
172 e2471df5 Andreas
       if(!is_writeable($this->cfg->base_path ."/templates_c")) {
173 e2471df5 Andreas
          print $this->cfg->base_path ."/templates_c: directory is not writeable for user ". $this->getuid() ."\n";
174 e2471df5 Andreas
          exit(1);
175 e2471df5 Andreas
       }
176 e2471df5 Andreas
177 af297e16 Andreas
       if(!is_writeable($this->cfg->thumb_path)) {
178 af297e16 Andreas
          print $this->cfg->thumb_path .": directory is not writeable for user ". $this->getuid() ."\n";
179 af297e16 Andreas
          exit(1);
180 af297e16 Andreas
       }
181 af297e16 Andreas
182 1532cb59 Andreas
       /******* Opening phpfspot's sqlite database *********/
183 1532cb59 Andreas
184 1532cb59 Andreas
       /* Check if directory where the database file is stored is writeable  */
185 1532cb59 Andreas
       if(!is_writeable(dirname($this->cfg->phpfspot_db))) {
186 c74587e3 Andreas
          print "Error: ". dirname($this->cfg->phpfspot_db) .": directory is not writeable for user ". $this->getuid() .".\n";
187 c74587e3 Andreas
          print "Please fix permissions so phpfspot can create its own sqlite database to store some settings.\n";
188 1532cb59 Andreas
          exit(1);
189 1532cb59 Andreas
       }
190 1532cb59 Andreas
191 1532cb59 Andreas
       /* Check if database file is writeable */
192 f79d6ba1 Andreas
       if(file_exists($this->cfg->phpfspot_db) && !is_writeable($this->cfg->phpfspot_db)) {
193 c74587e3 Andreas
          print "Error: ". $this->cfg->phpfspot_db ." is not writeable for user ". $this->getuid() .".\n";
194 c74587e3 Andreas
          print "Please fix permissions so phpfspot can create its own sqlite database to store some settings.\n";
195 5369d689 Andreas
          exit(1);
196 5369d689 Andreas
       }
197 e2471df5 Andreas
198 1532cb59 Andreas
       /* open the database */
199 1532cb59 Andreas
       $this->cfg_db = new PHPFSPOT_DB($this, $this->cfg->phpfspot_db);
200 dac334b4 Andreas
201 1532cb59 Andreas
       /* change sqlite temp directory, if requested */
202 1532cb59 Andreas
       if(isset($this->cfg->sqlite_temp_dir)) {
203 1532cb59 Andreas
          $this->cfg_db->db_exec("
204 1532cb59 Andreas
             PRAGMA
205 1532cb59 Andreas
                temp_store_directory = '". $this->cfg->sqlite_temp_dir ."'
206 1532cb59 Andreas
          ");
207 7f4f4e74 Andreas
       }
208 1532cb59 Andreas
209 1532cb59 Andreas
       /* Check if some tables need to be created */
210 6198d8ac Andreas
       $this->check_phpfspot_db();
211 dac334b4 Andreas
212 ef48f193 Andreas
       /* overload Smarty class with our own template handler */
213 d1882698 Andreas
       require_once "phpfspot_tmpl.php";
214 ae99d62d Andreas
       $this->tmpl = new PHPFSPOT_TMPL();
215 61f2970e Andreas
216 ea36e81b Andreas
       /* pre-set some template variables */
217 8a050d43 Andreas
       $this->tmpl->assign('web_path', $this->cfg->web_path);
218 8a050d43 Andreas
219 aab7d4d0 Andreas
       /* Starting with F-Spot 0.4.2, the rating-feature was available */
220 aab7d4d0 Andreas
       if($this->dbver > 10) {
221 f36faab3 Andreas
          $this->tmpl->assign('has_rating', true);
222 aab7d4d0 Andreas
          $this->sort_orders = array_merge($this->sort_orders, array(
223 aab7d4d0 Andreas
             'rate_asc' => 'Rate &uarr;',
224 aab7d4d0 Andreas
             'rate_desc' => 'Rate &darr;',
225 aab7d4d0 Andreas
          ));
226 aab7d4d0 Andreas
       }
227 aab7d4d0 Andreas
228 4dc79076 Andreas
       /* check if all necessary indices exist */
229 4dc79076 Andreas
       $this->checkDbIndices();
230 4dc79076 Andreas
231 8a634d64 Andreas
       /* if session is not yet started, do it now */
232 8a634d64 Andreas
       if(session_id() == "")
233 8a634d64 Andreas
          session_start();
234 ad475b7b Andreas
235 ad475b7b Andreas
       if(!isset($_SESSION['tag_condition']))
236 ad475b7b Andreas
          $_SESSION['tag_condition'] = 'or';
237 ad475b7b Andreas
238 f6b2d643 Andreas
       /* if sort-order has not been set yet, get the one specified in the config */
239 2f381082 Andreas
       if(!isset($_SESSION['sort_order']))
240 f6b2d643 Andreas
          $_SESSION['sort_order'] = $this->cfg->sort_order;
241 2f381082 Andreas
242 213a12db Andreas
       if(!isset($_SESSION['searchfor_tag']))
243 213a12db Andreas
          $_SESSION['searchfor_tag'] = '';
244 c48fe9f8 Andreas
245 e54e7297 Andreas
       // if begin_with is still set but thumbs_per_page is now 0, unset it
246 e54e7297 Andreas
       if(isset($_SESSION['begin_with']) && $this->cfg->thumbs_per_page == 0)
247 5d397db3 Andreas
          unset($_SESSION['begin_with']);
248 5d397db3 Andreas
249 8e14a6b9 Andreas
       // if user-friendly-url's are enabled, set also a flag for the template handler
250 8e14a6b9 Andreas
       if($this->is_user_friendly_url()) {
251 8e14a6b9 Andreas
          $this->tmpl->assign('user_friendly_url', 'true');
252 8e14a6b9 Andreas
       }
253 8e14a6b9 Andreas
254 61f2970e Andreas
    } // __construct()
255 61f2970e Andreas
256 61f2970e Andreas
    public function __destruct()
257 61f2970e Andreas
    {
258 61f2970e Andreas
259 61f2970e Andreas
    } // __destruct()
260 61f2970e Andreas
261 7f50c5e3 Andreas
    /**
262 7f50c5e3 Andreas
     * show - generate html output
263 7f50c5e3 Andreas
     *
264 7f50c5e3 Andreas
     * this function can be called after the constructor has
265 7f50c5e3 Andreas
     * prepared everyhing. it will load the index.tpl smarty
266 7f50c5e3 Andreas
     * template. if necessary it will registere pre-selects
267 7f50c5e3 Andreas
     * (photo index, photo, tag search, date search) into
268 7f50c5e3 Andreas
     * users session.
269 7f50c5e3 Andreas
     */
270 61f2970e Andreas
    public function show()
271 61f2970e Andreas
    {
272 213a12db Andreas
       $this->tmpl->assign('searchfor_tag', $_SESSION['searchfor_tag']);
273 de34b624 Andreas
       $this->tmpl->assign('page_title', $this->cfg->page_title);
274 ad475b7b Andreas
       $this->tmpl->assign('current_condition', $_SESSION['tag_condition']);
275 a36bd6f4 Andreas
       $this->tmpl->assign('template_path', 'themes/'. $this->cfg->theme_name);
276 3d567f23 Andreas
277 9471faab Andreas
       /* parse URL */
278 867cf65e Andreas
       if($this->is_user_friendly_url()) {
279 9471faab Andreas
          $content = $this->parse_user_friendly_url($_SERVER['REQUEST_URI']);
280 9471faab Andreas
       }
281 9471faab Andreas
282 cd94f8e4 Andreas
       if(isset($_GET['mode'])) {
283 1bf32ff1 Andreas
284 cd94f8e4 Andreas
          $_SESSION['start_action'] = $_GET['mode'];
285 cd94f8e4 Andreas
286 cd94f8e4 Andreas
          switch($_GET['mode']) {
287 cd94f8e4 Andreas
             case 'showpi':
288 cd94f8e4 Andreas
                if(isset($_GET['tags'])) {
289 cd94f8e4 Andreas
                   $_SESSION['selected_tags'] = $this->extractTags($_GET['tags']);
290 cd94f8e4 Andreas
                }
291 cd94f8e4 Andreas
                if(isset($_GET['from_date']) && $this->isValidDate($_GET['from_date'])) {
292 cd94f8e4 Andreas
                   $_SESSION['from_date'] = strtotime($_GET['from_date'] ." 00:00:00");
293 cd94f8e4 Andreas
                }
294 cd94f8e4 Andreas
                if(isset($_GET['to_date']) && $this->isValidDate($_GET['to_date'])) {
295 cd94f8e4 Andreas
                   $_SESSION['to_date'] = strtotime($_GET['to_date'] ." 23:59:59");
296 cd94f8e4 Andreas
                }
297 cd94f8e4 Andreas
                break;
298 cd94f8e4 Andreas
             case 'showp':
299 cd94f8e4 Andreas
                if(isset($_GET['tags'])) {
300 cd94f8e4 Andreas
                   $_SESSION['selected_tags'] = $this->extractTags($_GET['tags']);
301 cd94f8e4 Andreas
                   $_SESSION['start_action'] = 'showp';
302 cd94f8e4 Andreas
                }
303 cd94f8e4 Andreas
                if(isset($_GET['id']) && is_numeric($_GET['id'])) {
304 6198d8ac Andreas
                   if($_SESSION['current_photo'] != $_GET['id'])
305 6198d8ac Andreas
                      unset($_SESSION['current_version']);
306 cd94f8e4 Andreas
                   $_SESSION['current_photo'] = $_GET['id'];
307 cd94f8e4 Andreas
                   $_SESSION['start_action'] = 'showp';
308 cd94f8e4 Andreas
                }
309 cd94f8e4 Andreas
                if(isset($_GET['from_date']) && $this->isValidDate($_GET['from_date'])) {
310 3face190 Andreas
                   $_SESSION['from_date'] = strtotime($_GET['from_date'] ." 00:00:00");
311 cd94f8e4 Andreas
                }
312 cd94f8e4 Andreas
                if(isset($_GET['to_date']) && $this->isValidDate($_GET['to_date'])) {
313 3face190 Andreas
                   $_SESSION['to_date'] = strtotime($_GET['to_date'] ." 23:59:59");
314 cd94f8e4 Andreas
                }
315 cd94f8e4 Andreas
                break;
316 cd94f8e4 Andreas
             case 'export':
317 dcc26041 Andreas
                /* fetch export template */
318 dcc26041 Andreas
                print $this->tmpl->fetch("export.tpl");
319 dcc26041 Andreas
                /* no further execution necessary. */
320 cd94f8e4 Andreas
                return;
321 cd94f8e4 Andreas
                break;
322 cd94f8e4 Andreas
             case 'slideshow':
323 9ebbaf7d Andreas
                /* fetch slideshow template */
324 9ebbaf7d Andreas
                print $this->tmpl->show("slideshow.tpl");
325 9ebbaf7d Andreas
                /* no further execution necessary. */
326 cd94f8e4 Andreas
                return;
327 cd94f8e4 Andreas
                break;
328 cd94f8e4 Andreas
             case 'rss':
329 cd94f8e4 Andreas
                if(isset($_GET['tags'])) {
330 cd94f8e4 Andreas
                   $_SESSION['selected_tags'] = $this->extractTags($_GET['tags']);
331 cd94f8e4 Andreas
                }
332 cd94f8e4 Andreas
                if(isset($_GET['from_date']) && $this->isValidDate($_GET['from_date'])) {
333 cd94f8e4 Andreas
                   $_SESSION['from_date'] = strtotime($_GET['from_date'] ." 00:00:00");
334 cd94f8e4 Andreas
                }
335 cd94f8e4 Andreas
                if(isset($_GET['to_date']) && $this->isValidDate($_GET['to_date'])) {
336 cd94f8e4 Andreas
                   $_SESSION['to_date'] = strtotime($_GET['to_date'] ." 23:59:59");
337 cd94f8e4 Andreas
                }
338 cd94f8e4 Andreas
                $this->getRSSFeed();
339 cd94f8e4 Andreas
                return;
340 cd94f8e4 Andreas
                break;
341 cd94f8e4 Andreas
          }
342 1bf32ff1 Andreas
       }
343 1bf32ff1 Andreas
344 f36faab3 Andreas
       /* if date-search variables are registered in the session, set the check
345 f36faab3 Andreas
          for "consider date-range" in the html output
346 f36faab3 Andreas
       */
347 d017dda4 Andreas
       if(isset($_SESSION['from_date']) && isset($_SESSION['to_date']))
348 8223eed3 Andreas
          $this->tmpl->assign('date_search_enabled', true);
349 d017dda4 Andreas
350 f36faab3 Andreas
       /* if rate-search variables are registered in the session, set the check
351 f36faab3 Andreas
          for "consider rate-range" in the html output
352 f36faab3 Andreas
       */
353 f36faab3 Andreas
       if(isset($_SESSION['rate_from']) && isset($_SESSION['rate_to'])) {
354 f36faab3 Andreas
          $this->tmpl->assign('rate_search_enabled', true);
355 f36faab3 Andreas
       }
356 f36faab3 Andreas
357 bb3bcfbb Andreas
       $this->tmpl->register_function("sort_select_list", array(&$this, "smarty_sort_select_list"), false);
358 8996f568 Andreas
       $this->tmpl->assign('search_from_date', $this->get_date_text_field('from'));
359 8996f568 Andreas
       $this->tmpl->assign('search_to_date', $this->get_date_text_field('to'));
360 9471faab Andreas
361 50e93956 Andreas
       $this->tmpl->assign('preset_selected_tags', $this->getSelectedTags());
362 50e93956 Andreas
       $this->tmpl->assign('preset_available_tags', $this->getAvailableTags());
363 f36faab3 Andreas
       $this->tmpl->assign('rate_search', $this->get_rate_search());
364 50e93956 Andreas
365 fa213e56 Andreas
       /* if no site-content has been set yet... */
366 ca733ba2 Andreas
       if(!isset($content)) {
367 fa213e56 Andreas
          /* if tags are already selected, we can immediately display photo-index */
368 fa213e56 Andreas
          if((isset($_SESSION['selected_tags']) && !empty($_SESSION['selected_tags']) &&
369 b03bd448 Andreas
              isset($_SESSION['start_action']) && $_SESSION['start_action'] != 'showp') ||
370 b03bd448 Andreas
             (isset($_SESSION['start_action']) && $_SESSION['start_action'] == 'showpi'))
371 50e93956 Andreas
             $this->tmpl->assign('initial_content', $this->showPhotoIndex());
372 fa213e56 Andreas
          else {
373 fa213e56 Andreas
             /* if a photo is already selected, we can immediately display single-photo */
374 fa213e56 Andreas
             if(isset($_SESSION['current_photo']) && !empty($_SESSION['current_photo']))
375 fa213e56 Andreas
                $this->tmpl->assign('initial_content', $this->showPhoto($_SESSION['current_photo']));
376 fa213e56 Andreas
             else {
377 fa213e56 Andreas
                /* ok, then let us show the welcome page... */
378 fa213e56 Andreas
                $this->tmpl->assign('initial_content', $this->tmpl->fetch('welcome.tpl'));
379 fa213e56 Andreas
             }
380 fa213e56 Andreas
          }
381 ca733ba2 Andreas
       }
382 9471faab Andreas
       else
383 50e93956 Andreas
          $this->tmpl->assign('initial_content', $content);
384 9471faab Andreas
385 61f2970e Andreas
       $this->tmpl->show("index.tpl");
386 61f2970e Andreas
387 61f2970e Andreas
    } // show()
388 61f2970e Andreas
389 7f50c5e3 Andreas
    /**
390 7f50c5e3 Andreas
     * get_tags - grab all tags of f-spot's database
391 7f50c5e3 Andreas
     *
392 7f50c5e3 Andreas
     * this function will get all available tags from
393 7f50c5e3 Andreas
     * the f-spot database and store them within two
394 ba2bd867 Andreas
     * arrays within this class for later usage. in
395 7f50c5e3 Andreas
     * fact, if the user requests (hide_tags) it will
396 7f50c5e3 Andreas
     * opt-out some of them.
397 7f50c5e3 Andreas
     *
398 7f50c5e3 Andreas
     * this function is getting called once by show()
399 7f50c5e3 Andreas
     */
400 61f2970e Andreas
    private function get_tags()
401 61f2970e Andreas
    {
402 193a2982 Andreas
       $this->avail_tags = Array();
403 193a2982 Andreas
       $count = 0;
404 193a2982 Andreas
405 a3917de4 Andreas
       /* if show_tags has been set in the configuration (only show photos
406 a3917de4 Andreas
          which are tagged by these tags) they following will take care,
407 a3917de4 Andreas
          that only these other tags are displayed where the photo is also
408 a3917de4 Andreas
          tagged with one of show_tags.
409 a3917de4 Andreas
       */
410 33dba06c Andreas
       if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
411 33dba06c Andreas
          $query_str="
412 33dba06c Andreas
             SELECT
413 4dc79076 Andreas
                DISTINCT t1.id as id, t1.name as name
414 ad8f72f7 Andreas
             FROM
415 33dba06c Andreas
                photo_tags pt1
416 33dba06c Andreas
             INNER JOIN photo_tags
417 33dba06c Andreas
                pt2 ON pt1.photo_id=pt2.photo_id
418 33dba06c Andreas
             INNER JOIN tags t1
419 33dba06c Andreas
                ON t1.id=pt1.tag_id
420 33dba06c Andreas
             INNER JOIN tags t2
421 33dba06c Andreas
                ON t2.id=pt2.tag_id
422 33dba06c Andreas
             WHERE
423 4dc79076 Andreas
                t2.name IN  ('".implode("','",$this->cfg->show_tags)."')
424 33dba06c Andreas
             ORDER BY
425 33dba06c Andreas
                t1.sort_priority ASC";
426 4dc79076 Andreas
427 33dba06c Andreas
          $result = $this->db->db_query($query_str);
428 33dba06c Andreas
       }
429 33dba06c Andreas
       else
430 33dba06c Andreas
       {
431 33dba06c Andreas
          $result = $this->db->db_query("
432 6216473e Andreas
             SELECT id as id,name as name
433 33dba06c Andreas
             FROM tags
434 33dba06c Andreas
             ORDER BY sort_priority ASC
435 33dba06c Andreas
          ");
436 33dba06c Andreas
       }
437 61f2970e Andreas
438 61f2970e Andreas
       while($row = $this->db->db_fetch_object($result)) {
439 61f2970e Andreas
440 61f2970e Andreas
          $tag_id = $row['id'];
441 61f2970e Andreas
          $tag_name = $row['name'];
442 61f2970e Andreas
443 ba2bd867 Andreas
          /* if the user has specified to ignore this tag in phpfspot's
444 ba2bd867 Andreas
             configuration, ignore it here so it does not get added to
445 ba2bd867 Andreas
             the tag list.
446 ba2bd867 Andreas
          */
447 7ca20d2b Andreas
          if(in_array($row['name'], $this->cfg->hide_tags))
448 7ca20d2b Andreas
             continue;
449 7ca20d2b Andreas
450 33dba06c Andreas
          /* if you include the following if-clause and the user has specified
451 33dba06c Andreas
             to only show certain tags which are specified in phpfspot's
452 33dba06c Andreas
             configuration, ignore all others so they will not be added to the
453 33dba06c Andreas
             tag list.
454 ba2bd867 Andreas
          if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags) &&
455 ba2bd867 Andreas
             !in_array($row['name'], $this->cfg->show_tags))
456 ba2bd867 Andreas
             continue;
457 33dba06c Andreas
          */
458 ba2bd867 Andreas
459 61f2970e Andreas
          $this->tags[$tag_id] = $tag_name;
460 193a2982 Andreas
          $this->avail_tags[$count] = $tag_id;
461 193a2982 Andreas
          $count++;
462 61f2970e Andreas
463 61f2970e Andreas
       }
464 193a2982 Andreas
465 61f2970e Andreas
    } // get_tags()
466 61f2970e Andreas
467 7f50c5e3 Andreas
    /**
468 6198d8ac Andreas
     * get all photo details from F-Spot database
469 7f50c5e3 Andreas
     *
470 6198d8ac Andreas
     * this function queries the F-Spot database for all available
471 6198d8ac Andreas
     * details of the requested photo. It returns them as a object.
472 6198d8ac Andreas
     *
473 6198d8ac Andreas
     * Furthermore it takes care of the photo version to be requested.
474 6198d8ac Andreas
     * If photo version is not yet, it queries information for the
475 6198d8ac Andreas
     * original photo.
476 6198d8ac Andreas
     *
477 2765fc08 Andreas
     * @param integer $idx
478 2765fc08 Andreas
     * @return object|null
479 7f50c5e3 Andreas
     */
480 6198d8ac Andreas
    public function get_photo_details($idx, $version_idx = NULL)
481 61f2970e Andreas
    {
482 a3917de4 Andreas
       /* ~ F-Spot version 0.3.x */
483 18ff7e04 Andreas
       if($this->dbver < 9) {
484 18ff7e04 Andreas
          $query_str = "
485 6216473e Andreas
             SELECT
486 6216473e Andreas
                p.id as id,
487 6216473e Andreas
                p.name as name,
488 6216473e Andreas
                p.time as time,
489 6216473e Andreas
                p.directory_path as directory_path,
490 6216473e Andreas
                p.description as description
491 6216473e Andreas
             FROM
492 6216473e Andreas
                photos p
493 18ff7e04 Andreas
          ";
494 18ff7e04 Andreas
       }
495 18ff7e04 Andreas
       else {
496 aab7d4d0 Andreas
          /* till F-Spot version 0.4.1 */
497 aab7d4d0 Andreas
          if($this->dbver < 11) {
498 aab7d4d0 Andreas
             $query_str = "
499 6216473e Andreas
                SELECT
500 6216473e Andreas
                   p.id as id,
501 6216473e Andreas
                   p.uri as uri,
502 6216473e Andreas
                   p.time as time,
503 6216473e Andreas
                   p.description as description
504 6216473e Andreas
                FROM
505 6216473e Andreas
                   photos p
506 aab7d4d0 Andreas
             ";
507 aab7d4d0 Andreas
          }
508 0b7778c4 Andreas
          elseif($this->dbver < 17) {
509 0b7778c4 Andreas
              /* rating value got introduced */
510 aab7d4d0 Andreas
             $query_str = "
511 6216473e Andreas
                SELECT
512 6216473e Andreas
                   p.id as id,
513 6216473e Andreas
                   p.uri as uri,
514 6216473e Andreas
                   p.time as time,
515 6216473e Andreas
                   p.description as description,
516 6216473e Andreas
                   p.rating as rating
517 6216473e Andreas
                FROM
518 6216473e Andreas
                   photos p
519 aab7d4d0 Andreas
             ";
520 aab7d4d0 Andreas
          }
521 0b7778c4 Andreas
          else {
522 0b7778c4 Andreas
             /* path & filename now splited in base_uri & filename */
523 0b7778c4 Andreas
             $query_str = "
524 0b7778c4 Andreas
                SELECT
525 0b7778c4 Andreas
                   p.id as id,
526 0b7778c4 Andreas
                   p.base_uri || p.filename as uri,
527 0b7778c4 Andreas
                   p.time as time,
528 0b7778c4 Andreas
                   p.description as description,
529 0b7778c4 Andreas
                   p.rating as rating
530 0b7778c4 Andreas
                FROM
531 0b7778c4 Andreas
                   photos p
532 0b7778c4 Andreas
             ";
533 0b7778c4 Andreas
          }
534 18ff7e04 Andreas
       }
535 ba2bd867 Andreas
536 a3917de4 Andreas
       /* if show_tags is set, only return details of photos which are
537 a3917de4 Andreas
          tagged with a tag that has been specified to be shown.
538 ba2bd867 Andreas
       */
539 ba2bd867 Andreas
       if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
540 ba2bd867 Andreas
          $query_str.= "
541 ba2bd867 Andreas
             INNER JOIN photo_tags pt
542 ba2bd867 Andreas
                ON p.id=pt.photo_id
543 ba2bd867 Andreas
             INNER JOIN tags t
544 ba2bd867 Andreas
                ON pt.tag_id=t.id
545 ba2bd867 Andreas
             WHERE p.id='". $idx ."'
546 5ea211de Andreas
             AND t.name IN ('".implode("','",$this->cfg->show_tags)."')";
547 ba2bd867 Andreas
       }
548 ba2bd867 Andreas
       else {
549 ba2bd867 Andreas
          $query_str.= "
550 ba2bd867 Andreas
             WHERE p.id='". $idx ."'
551 ba2bd867 Andreas
          ";
552 ba2bd867 Andreas
       }
553 ba2bd867 Andreas
554 d30a91ea Andreas
       if(!$row = $this->db->db_fetchSingleRow($query_str))
555 d30a91ea Andreas
          return null;
556 18ff7e04 Andreas
557 d30a91ea Andreas
       /* before F-Spot db version 9 there was no uri column but
558 d30a91ea Andreas
          seperated fields for directory_path and name (= filename).
559 d30a91ea Andreas
       */
560 d30a91ea Andreas
       if($this->dbver < 9) {
561 d30a91ea Andreas
          $row['uri'] = "file://". $row['directory_path'] ."/". $row['name'];
562 d30a91ea Andreas
       }
563 d30a91ea Andreas
       /* starting with dbversion >= 17 we need to rawurldecode() uri */
564 d30a91ea Andreas
       elseif($this->dbver >= 17) {
565 d30a91ea Andreas
          $row['uri'] = rawurldecode($row['uri']);
566 d30a91ea Andreas
       }
567 18ff7e04 Andreas
568 d30a91ea Andreas
       /* if version-idx has not yet been set, get the latest photo version */
569 d30a91ea Andreas
       if(!isset($version_idx) || !$this->is_valid_version($idx, $version_idx))
570 d30a91ea Andreas
          $version_idx = $this->get_latest_version($idx);
571 6198d8ac Andreas
572 d30a91ea Andreas
       /* if an alternative version has been requested. But we
573 d30a91ea Andreas
          support this only for F-Spot database versions from
574 d30a91ea Andreas
          v9.
575 d30a91ea Andreas
       */
576 d30a91ea Andreas
       if($version_idx > 0 && $this->dbver >= 9) {
577 d30a91ea Andreas
          if ($this->dbver < 17) {
578 d30a91ea Andreas
             /* check for alternative versions */
579 d30a91ea Andreas
             if($version = $this->db->db_fetchSingleRow("
580 d30a91ea Andreas
                SELECT
581 d30a91ea Andreas
                   version_id, name, uri
582 d30a91ea Andreas
                FROM
583 d30a91ea Andreas
                   photo_versions
584 d30a91ea Andreas
                WHERE
585 d30a91ea Andreas
                   photo_id LIKE '". $idx ."'
586 d30a91ea Andreas
                AND
587 d30a91ea Andreas
                   version_id LIKE '". $version_idx ."'")) {
588 d30a91ea Andreas
589 d30a91ea Andreas
                $row['name'] = $version['name'];
590 d30a91ea Andreas
                $row['uri'] = $version['uri'];
591 0b7778c4 Andreas
             }
592 d30a91ea Andreas
          }
593 d30a91ea Andreas
          else {
594 d30a91ea Andreas
             /* path & filename now splited in base_uri & filename */
595 d30a91ea Andreas
             if($version = $this->db->db_fetchSingleRow("
596 d30a91ea Andreas
                SELECT
597 d30a91ea Andreas
                   version_id,
598 d30a91ea Andreas
                   name,
599 d30a91ea Andreas
                   base_uri || filename as uri
600 d30a91ea Andreas
                FROM
601 d30a91ea Andreas
                   photo_versions
602 d30a91ea Andreas
                WHERE
603 d30a91ea Andreas
                   photo_id LIKE '". $idx ."'
604 d30a91ea Andreas
                AND
605 d30a91ea Andreas
                   version_id LIKE '". $version_idx ."'")) {
606 d30a91ea Andreas
607 d30a91ea Andreas
                $row['name'] = $version['name'];
608 d30a91ea Andreas
                $row['uri'] = rawurldecode($version['uri']);
609 6198d8ac Andreas
             }
610 6198d8ac Andreas
          }
611 b6feb281 Andreas
       }
612 6198d8ac Andreas
613 d30a91ea Andreas
       return $row;
614 61f2970e Andreas
615 6198d8ac Andreas
    } // get_photo_details()
616 61f2970e Andreas
617 7f50c5e3 Andreas
    /**
618 7f50c5e3 Andreas
     * returns aligned photo names
619 7f50c5e3 Andreas
     *
620 a3917de4 Andreas
     * this function returns aligned (length) names for a specific photo.
621 a3917de4 Andreas
     * If the length of the name exceeds $limit the name will bei
622 a3917de4 Andreas
     * shrinked (...).
623 a3917de4 Andreas
     *
624 2765fc08 Andreas
     * @param integer $idx
625 2765fc08 Andreas
     * @param integer $limit
626 2765fc08 Andreas
     * @return string|null
627 7f50c5e3 Andreas
     */
628 105d5d13 Andreas
    public function getPhotoName($idx, $limit = 0)
629 8f3e27ca Andreas
    {
630 74295f1b Andreas
       if($details = $this->get_photo_details($idx)) {
631 18ff7e04 Andreas
          if($long_name = $this->parse_uri($details['uri'], 'filename')) {
632 18ff7e04 Andreas
             $name = $this->shrink_text($long_name, $limit);
633 18ff7e04 Andreas
             return $name;
634 18ff7e04 Andreas
          }
635 74295f1b Andreas
       }
636 8f3e27ca Andreas
637 18ff7e04 Andreas
       return null;
638 18ff7e04 Andreas
639 8f3e27ca Andreas
    } // getPhotoName()
640 8f3e27ca Andreas
641 7f50c5e3 Andreas
    /**
642 2ea665ef Andreas
     * get photo rating level
643 2ea665ef Andreas
     *
644 a3917de4 Andreas
     * this function will return the integer-based rating level of a
645 a3917de4 Andreas
     * photo. This can only be done, if the F-Spot database is at a
646 a3917de4 Andreas
     * specific version. If rating value can not be found, zero will
647 a3917de4 Andreas
     * be returned indicating no rating value is available.
648 a3917de4 Andreas
     *
649 2ea665ef Andreas
     * @param integer idx
650 2ea665ef Andreas
     * @return integer
651 2ea665ef Andreas
     */
652 2ea665ef Andreas
    public function get_photo_rating($idx)
653 2ea665ef Andreas
    {
654 2ea665ef Andreas
       if($detail = $this->get_photo_details($idx)) {
655 2ea665ef Andreas
          if(isset($detail['rating']))
656 2ea665ef Andreas
             return $detail['rating'];
657 2ea665ef Andreas
       }
658 2ea665ef Andreas
659 2ea665ef Andreas
       return 0;
660 2ea665ef Andreas
661 2ea665ef Andreas
    } // get_photo_rating()
662 2ea665ef Andreas
663 2ea665ef Andreas
    /**
664 f36faab3 Andreas
     * get rate-search bars
665 f36faab3 Andreas
     *
666 a3917de4 Andreas
     * this function will return the rating-bars for the search field.
667 a3917de4 Andreas
     *
668 f36faab3 Andreas
     * @return string
669 f36faab3 Andreas
     */
670 f36faab3 Andreas
    public function get_rate_search()
671 f36faab3 Andreas
    {
672 f36faab3 Andreas
       $bar = "";
673 f36faab3 Andreas
674 f36faab3 Andreas
       for($i = 1; $i <= 5; $i++) {
675 f36faab3 Andreas
676 f36faab3 Andreas
          $bar.= "<img id=\"rate_from_". $i ."\" src=\"";
677 f36faab3 Andreas
678 f36faab3 Andreas
          if(isset($_SESSION['rate_from']) && $i <= $_SESSION['rate_from'])
679 f36faab3 Andreas
             $bar.= $this->cfg->web_path ."/resources/star.png";
680 f36faab3 Andreas
          else
681 f36faab3 Andreas
             $bar.= $this->cfg->web_path ."/resources/empty_rate.png";
682 f36faab3 Andreas
683 f36faab3 Andreas
          $bar.= "\"
684 f36faab3 Andreas
             onmouseover=\"show_rate('from', ". $i .");\"
685 f36faab3 Andreas
             onmouseout=\"reset_rate('from');\"
686 f36faab3 Andreas
             onclick=\"set_rate('from', ". $i .")\" />";
687 f36faab3 Andreas
       }
688 f36faab3 Andreas
689 f36faab3 Andreas
       $bar.= "<br />\n";
690 f36faab3 Andreas
691 f36faab3 Andreas
        for($i = 1; $i <= 5; $i++) {
692 f36faab3 Andreas
693 f36faab3 Andreas
          $bar.= "<img id=\"rate_to_". $i ."\" src=\"";
694 f36faab3 Andreas
695 f36faab3 Andreas
          if(isset($_SESSION['rate_to']) && $i <= $_SESSION['rate_to'])
696 f36faab3 Andreas
             $bar.= $this->cfg->web_path ."/resources/star.png";
697 f36faab3 Andreas
          else
698 f36faab3 Andreas
             $bar.= $this->cfg->web_path ."/resources/empty_rate.png";
699 f36faab3 Andreas
700 f36faab3 Andreas
          $bar.= "\"
701 f36faab3 Andreas
             onmouseover=\"show_rate('to', ". $i .");\"
702 f36faab3 Andreas
             onmouseout=\"reset_rate('to');\"
703 f36faab3 Andreas
             onclick=\"set_rate('to', ". $i .");\" />";
704 f36faab3 Andreas
       }
705 f36faab3 Andreas
706 f36faab3 Andreas
       return $bar;
707 f36faab3 Andreas
708 f36faab3 Andreas
    } // get_rate_search()
709 f36faab3 Andreas
710 f36faab3 Andreas
    /**
711 e9903870 Andreas
     * shrink text according provided limit
712 e9903870 Andreas
     *
713 a3917de4 Andreas
     * If the length of the name exceeds $limit, text will be shortend
714 a3917de4 Andreas
     * and inner content will be replaced with "...".
715 a3917de4 Andreas
     *
716 2765fc08 Andreas
     * @param string $ext
717 2765fc08 Andreas
     * @param integer $limit
718 2765fc08 Andreas
     * @return string
719 e9903870 Andreas
     */
720 e9903870 Andreas
    private function shrink_text($text, $limit)
721 e9903870 Andreas
    {
722 e9903870 Andreas
       if($limit != 0 && strlen($text) > $limit) {
723 e9903870 Andreas
          $text = substr($text, 0, $limit-5) ."...". substr($text, -($limit-5));
724 e9903870 Andreas
       }
725 e9903870 Andreas
726 e9903870 Andreas
       return $text;
727 e9903870 Andreas
728 e9903870 Andreas
    } // shrink_text();
729 e9903870 Andreas
730 e9903870 Andreas
    /**
731 7f50c5e3 Andreas
     * translate f-spoth photo path
732 7f50c5e3 Andreas
     *
733 7f50c5e3 Andreas
     * as the full-qualified path recorded in the f-spot database
734 7f50c5e3 Andreas
     * is usally not the same as on the webserver, this function
735 7f50c5e3 Andreas
     * will replace the path with that one specified in the cfg
736 2765fc08 Andreas
     * @param string $path
737 2765fc08 Andreas
     * @return string
738 7f50c5e3 Andreas
     */
739 14eb0419 Andreas
    public function translate_path($path)
740 1a7ed49f Andreas
    {
741 1a7ed49f Andreas
       if($this->cfg->enable_replace_path == true)
742 1a7ed49f Andreas
          return str_replace(
743 1a7ed49f Andreas
             $this->cfg->path_replace_from,
744 1a7ed49f Andreas
             $this->cfg->path_replace_to, $path);
745 1a7ed49f Andreas
746 1a7ed49f Andreas
       return $path;
747 61f2970e Andreas
748 61f2970e Andreas
    } // translate_path
749 61f2970e Andreas
750 7f50c5e3 Andreas
    /**
751 7f50c5e3 Andreas
     * control HTML ouput for a single photo
752 7f50c5e3 Andreas
     *
753 7f50c5e3 Andreas
     * this function provides all the necessary information
754 7f50c5e3 Andreas
     * for the single photo template.
755 2765fc08 Andreas
     * @param integer photo
756 7f50c5e3 Andreas
     */
757 ca33ffdc Andreas
    public function showPhoto($photo)
758 7a8bacbb Andreas
    {
759 7f50c5e3 Andreas
       /* get all photos from the current photo selection */
760 4b115eb0 Andreas
       $all_photos = $this->getPhotoSelection();
761 5d397db3 Andreas
       $count = count($all_photos);
762 bbf672d2 Andreas
763 5d397db3 Andreas
       for($i = 0; $i < $count; $i++) {
764 bbf672d2 Andreas
765 7f50c5e3 Andreas
          // $get_next will be set, when the photo which has to
766 7f50c5e3 Andreas
          // be displayed has been found - this means that the
767 7f50c5e3 Andreas
          // next available is in fact the NEXT image (for the
768 7f50c5e3 Andreas
          // navigation icons)
769 7f50c5e3 Andreas
          if(isset($get_next)) {
770 5d397db3 Andreas
             $next_img = $all_photos[$i];
771 bbf672d2 Andreas
             break;
772 bbf672d2 Andreas
          }
773 bbf672d2 Andreas
774 7f50c5e3 Andreas
          /* the next photo is our NEXT photo */
775 5d397db3 Andreas
          if($all_photos[$i] == $photo) {
776 bbf672d2 Andreas
             $get_next = 1;
777 bbf672d2 Andreas
          }
778 bbf672d2 Andreas
          else {
779 5d397db3 Andreas
             $previous_img = $all_photos[$i];
780 5d397db3 Andreas
          }
781 5d397db3 Andreas
782 5d397db3 Andreas
          if($photo == $all_photos[$i]) {
783 5d397db3 Andreas
                $current = $i;
784 bbf672d2 Andreas
          }
785 bbf672d2 Andreas
       }
786 bbf672d2 Andreas
787 588923cb Andreas
       $details = $this->get_photo_details($photo);
788 a552181a Andreas
789 a552181a Andreas
       if(!$details) {
790 a552181a Andreas
          print "error";
791 a552181a Andreas
          return;
792 a552181a Andreas
       }
793 a552181a Andreas
794 18ff7e04 Andreas
       $orig_path = $this->translate_path($this->parse_uri($details['uri'], 'fullpath'));
795 6198d8ac Andreas
796 6198d8ac Andreas
       /* if current version is already set, use it */
797 6198d8ac Andreas
       if($this->get_current_version() !== false)
798 6198d8ac Andreas
          $version = $this->get_current_version();
799 6198d8ac Andreas
800 6198d8ac Andreas
       /* if version not set yet, we assume to display the latest version */
801 6198d8ac Andreas
       if(!isset($version) || !$this->is_valid_version($photo, $version))
802 6198d8ac Andreas
          $version = $this->get_latest_version($photo);
803 6198d8ac Andreas
804 6198d8ac Andreas
       $thumb_path = $this->get_thumb_path($this->cfg->photo_width, $photo, $version);
805 322fc502 Andreas
806 cb4b2907 Andreas
       if(!file_exists($orig_path)) {
807 a276fb26 Andreas
          $this->_error("Photo ". $orig_path ." does not exist!<br />\n");
808 0501e39d Andreas
          return;
809 cb4b2907 Andreas
       }
810 cb4b2907 Andreas
811 cb4b2907 Andreas
       if(!is_readable($orig_path)) {
812 a276fb26 Andreas
          $this->_error("Photo ". $orig_path ." is not readable for user ". $this->getuid() ."<br />\n");
813 0501e39d Andreas
          return;
814 cb4b2907 Andreas
       }
815 cb4b2907 Andreas
816 322fc502 Andreas
       /* If the thumbnail doesn't exist yet, try to create it */
817 322fc502 Andreas
       if(!file_exists($thumb_path)) {
818 6a21b6f0 Andreas
          $this->gen_thumb($photo, true);
819 6198d8ac Andreas
          $thumb_path = $this->get_thumb_path($this->cfg->photo_width, $photo, $version);
820 322fc502 Andreas
       }
821 322fc502 Andreas
822 10509074 Andreas
       /* get mime-type, height and width from the original photo */
823 10509074 Andreas
       $info = getimagesize($orig_path);
824 10509074 Andreas
825 8ab8f2cd Andreas
       /* get EXIF information if JPEG */
826 9609cbda Andreas
       if(isset($info['mime']) && $info['mime'] == "image/jpeg") {
827 8ab8f2cd Andreas
          $meta = $this->get_meta_informations($orig_path);
828 8ab8f2cd Andreas
       }
829 64aff8ea Andreas
830 169c44be Andreas
       /* If EXIF data are available, use them */
831 169c44be Andreas
       if(isset($meta['ExifImageWidth'])) {
832 169c44be Andreas
          $meta_res = $meta['ExifImageWidth'] ."x". $meta['ExifImageLength'];
833 169c44be Andreas
       } else {
834 169c44be Andreas
          $meta_res = $info[0] ."x". $info[1];
835 169c44be Andreas
       }
836 169c44be Andreas
837 7f3aad92 Andreas
       $meta_make = isset($meta['Make']) ? $meta['Make'] ." / ". $meta['Model'] : "n/a";
838 f02f5af7 Andreas
       $meta_size = isset($meta['FileSize']) ? round($meta['FileSize']/1024, 1) ."kbyte" : "n/a";
839 f02f5af7 Andreas
840 15a6c1b6 Andreas
       $extern_link = "index.php?mode=showp&id=". $photo;
841 7f50c5e3 Andreas
       $current_tags = $this->getCurrentTags();
842 15a6c1b6 Andreas
       if($current_tags != "") {
843 15a6c1b6 Andreas
          $extern_link.= "&tags=". $current_tags;
844 15a6c1b6 Andreas
       }
845 3d567f23 Andreas
       if(isset($_SESSION['from_date']) && isset($_SESSION['to_date'])) {
846 8223eed3 Andreas
          $extern_link.= "&from_date=". $this->ts2str($_SESSION['from_date']) ."&to_date=". $this->ts2str($_SESSION['to_date']);
847 3d567f23 Andreas
       }
848 15a6c1b6 Andreas
849 15a6c1b6 Andreas
       $this->tmpl->assign('extern_link', $extern_link);
850 15a6c1b6 Andreas
851 1a39ed73 Andreas
       if(!file_exists($thumb_path)) {
852 1a39ed73 Andreas
          $this->_error("Can't open file ". $thumb_path ."\n");
853 1a39ed73 Andreas
          return;
854 1a39ed73 Andreas
       }
855 64aff8ea Andreas
856 10509074 Andreas
       $info_thumb = getimagesize($thumb_path);
857 7a8bacbb Andreas
858 1a39ed73 Andreas
       $this->tmpl->assign('description', $details['description']);
859 1a39ed73 Andreas
       $this->tmpl->assign('image_name', $this->parse_uri($details['uri'], 'filename'));
860 2ea665ef Andreas
       $this->tmpl->assign('image_rating', $this->get_photo_rating($photo));
861 322fc502 Andreas
862 10509074 Andreas
       $this->tmpl->assign('width', $info_thumb[0]);
863 10509074 Andreas
       $this->tmpl->assign('height', $info_thumb[1]);
864 c338e2fd Andreas
       $this->tmpl->assign('ExifMadeOn', strftime("%a %x %X", $details['time']));
865 1a39ed73 Andreas
       $this->tmpl->assign('ExifMadeWith', $meta_make);
866 1a39ed73 Andreas
       $this->tmpl->assign('ExifOrigResolution', $meta_res);
867 1a39ed73 Andreas
       $this->tmpl->assign('ExifFileSize', $meta_size);
868 1a39ed73 Andreas
869 867cf65e Andreas
       if($this->is_user_friendly_url()) {
870 6198d8ac Andreas
          $this->tmpl->assign('image_url', '/photo/'. $photo ."/". $this->cfg->photo_width .'/'. $version);
871 867cf65e Andreas
          $this->tmpl->assign('image_url_full', '/photo/'. $photo);
872 867cf65e Andreas
       }
873 867cf65e Andreas
       else {
874 6198d8ac Andreas
          $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&amp;width=". $this->cfg->photo_width ."&amp;version=". $version);
875 867cf65e Andreas
          $this->tmpl->assign('image_url_full', 'phpfspot_img.php?idx='. $photo);
876 867cf65e Andreas
       }
877 9471faab Andreas
878 1a39ed73 Andreas
       $this->tmpl->assign('image_filename', $this->parse_uri($details['uri'], 'filename'));
879 322fc502 Andreas
880 1a39ed73 Andreas
       $this->tmpl->assign('tags', $this->get_photo_tags($photo));
881 addc9889 Andreas
       $this->tmpl->assign('current_page', $this->getCurrentPage($current, $count));
882 addc9889 Andreas
       $this->tmpl->assign('current_img', $photo);
883 64aff8ea Andreas
884 9609cbda Andreas
       if(isset($previous_img)) {
885 ba600e17 Andreas
          $this->tmpl->assign('previous_url', "javascript:showPhoto(". $previous_img .");");
886 bcf2be54 Andreas
          $this->tmpl->assign('prev_img', $previous_img);
887 7a8bacbb Andreas
       }
888 7a8bacbb Andreas
889 9609cbda Andreas
       if(isset($next_img)) {
890 ba600e17 Andreas
          $this->tmpl->assign('next_url', "javascript:showPhoto(". $next_img .");");
891 bcf2be54 Andreas
          $this->tmpl->assign('next_img', $next_img);
892 7a8bacbb Andreas
       }
893 9609cbda Andreas
894 bcf2be54 Andreas
       $this->tmpl->assign('mini_width', $this->cfg->mini_width);
895 072aed3d Andreas
       $this->tmpl->assign('photo_width', $this->cfg->photo_width);
896 4079ac52 Andreas
       $this->tmpl->assign('photo_number', $i);
897 4079ac52 Andreas
       $this->tmpl->assign('photo_count', count($all_photos));
898 6198d8ac Andreas
       $this->tmpl->assign('photo', $photo);
899 6198d8ac Andreas
       $this->tmpl->assign('version', $version);
900 6198d8ac Andreas
901 6198d8ac Andreas
       /* if the photo as alternative versions, set a flag for the template */
902 6198d8ac Andreas
       if($this->get_photo_versions($photo))
903 6198d8ac Andreas
          $this->tmpl->assign('has_versions', true);
904 6198d8ac Andreas
905 6198d8ac Andreas
       $this->tmpl->register_function("photo_version_select_list", array(&$this, "smarty_photo_version_select_list"), false);
906 7a8bacbb Andreas
907 9471faab Andreas
       return $this->tmpl->fetch("single_photo.tpl");
908 ca33ffdc Andreas
909 ca33ffdc Andreas
    } // showPhoto()
910 7a8bacbb Andreas
911 7f50c5e3 Andreas
    /**
912 7f50c5e3 Andreas
     * all available tags and tag cloud
913 7f50c5e3 Andreas
     *
914 7f50c5e3 Andreas
     * this function outputs all available tags (time ordered)
915 7f50c5e3 Andreas
     * and in addition output them as tag cloud (tags which have
916 7f50c5e3 Andreas
     * many photos will appears more then others)
917 7f50c5e3 Andreas
     */
918 193a2982 Andreas
    public function getAvailableTags()
919 193a2982 Andreas
    {
920 2c33b25c Andreas
       /* retrive tags from database */
921 b44a5c35 Andreas
       $this->get_tags();
922 b44a5c35 Andreas
923 350ffa92 Andreas
       $output = "";
924 350ffa92 Andreas
925 83cde026 Andreas
       $result = $this->db->db_query("
926 83cde026 Andreas
          SELECT tag_id as id, count(tag_id) as quantity
927 83cde026 Andreas
          FROM photo_tags
928 8bd24a35 Andreas
          INNER JOIN tags t
929 8bd24a35 Andreas
             ON t.id = tag_id
930 83cde026 Andreas
          GROUP BY tag_id
931 8bd24a35 Andreas
          ORDER BY t.name ASC
932 83cde026 Andreas
       ");
933 83cde026 Andreas
934 83cde026 Andreas
       $tags = Array();
935 83cde026 Andreas
936 83cde026 Andreas
       while($row = $this->db->db_fetch_object($result)) {
937 83cde026 Andreas
          $tags[$row['id']] = $row['quantity'];
938 83cde026 Andreas
       }
939 83cde026 Andreas
940 83cde026 Andreas
       // change these font sizes if you will
941 83cde026 Andreas
       $max_size = 125; // max font size in %
942 83cde026 Andreas
       $min_size = 75; // min font size in %
943 83cde026 Andreas
944 67f0eb35 Andreas
       // color
945 67f0eb35 Andreas
       $max_sat = hexdec('cc');
946 67f0eb35 Andreas
       $min_sat = hexdec('44');
947 67f0eb35 Andreas
948 83cde026 Andreas
       // get the largest and smallest array values
949 83cde026 Andreas
       $max_qty = max(array_values($tags));
950 83cde026 Andreas
       $min_qty = min(array_values($tags));
951 83cde026 Andreas
952 83cde026 Andreas
       // find the range of values
953 83cde026 Andreas
       $spread = $max_qty - $min_qty;
954 83cde026 Andreas
       if (0 == $spread) { // we don't want to divide by zero
955 83cde026 Andreas
          $spread = 1;
956 83cde026 Andreas
       }
957 83cde026 Andreas
958 83cde026 Andreas
       // determine the font-size increment
959 83cde026 Andreas
       // this is the increase per tag quantity (times used)
960 83cde026 Andreas
       $step = ($max_size - $min_size)/($spread);
961 67f0eb35 Andreas
       $step_sat = ($max_sat - $min_sat)/($spread);
962 83cde026 Andreas
963 83cde026 Andreas
       // loop through our tag array
964 83cde026 Andreas
       foreach ($tags as $key => $value) {
965 83cde026 Andreas
966 aaa8698f Andreas
          /* has the currently processed tag already been added to
967 aaa8698f Andreas
             the selected tag list? if so, ignore it here...
968 aaa8698f Andreas
          */
969 83cde026 Andreas
          if(isset($_SESSION['selected_tags']) && in_array($key, $_SESSION['selected_tags']))
970 193a2982 Andreas
             continue;
971 193a2982 Andreas
972 2c33b25c Andreas
          // calculate CSS font-size
973 2c33b25c Andreas
          // find the $value in excess of $min_qty
974 2c33b25c Andreas
          // multiply by the font-size increment ($size)
975 2c33b25c Andreas
          // and add the $min_size set above
976 83cde026 Andreas
          $size = $min_size + (($value - $min_qty) * $step);
977 aaa8698f Andreas
          // uncomment if you want sizes in whole %:
978 c7e18357 Andreas
          $size = ceil($size);
979 83cde026 Andreas
980 67f0eb35 Andreas
          $color = $min_sat + ($value - $min_qty) * $step_sat;
981 67f0eb35 Andreas
982 67f0eb35 Andreas
          $r = '44';
983 67f0eb35 Andreas
          $g = dechex($color);
984 67f0eb35 Andreas
          $b = '88';
985 67f0eb35 Andreas
986 ba2bd867 Andreas
          if(isset($this->tags[$key])) {
987 aaa8698f Andreas
             if($this->is_user_friendly_url()) {
988 aaa8698f Andreas
                $output.= "<a href=\"". $this->cfg->web_path ."/tag/". $key ."\"
989 aaa8698f Andreas
                   onclick=\"Tags('add', ". $key ."); return false;\"
990 aaa8698f Andreas
                   class=\"tag\"
991 aaa8698f Andreas
                   style=\"font-size: ". $size ."%; color: #". $r.$g.$b .";\"
992 aaa8698f Andreas
                   title=\"Tag ". $this->tags[$key] .", ". $tags[$key] ." picture(s)\">". $this->tags[$key] ."</a>, ";
993 aaa8698f Andreas
             }
994 aaa8698f Andreas
             else {
995 aaa8698f Andreas
                $output.= "<a href=\"". $this->cfg->web_path ."/index.php?mode=showpi\"
996 aaa8698f Andreas
                   onclick=\"Tags('add', ". $key ."); return false;\"
997 aaa8698f Andreas
                   class=\"tag\"
998 aaa8698f Andreas
                   style=\"font-size: ". $size ."%; color: #". $r.$g.$b .";\"
999 aaa8698f Andreas
                   title=\"Tag ". $this->tags[$key] .", ". $tags[$key] ." picture(s)\">". $this->tags[$key] ."</a>, ";
1000 aaa8698f Andreas
             }
1001 ba2bd867 Andreas
          }
1002 193a2982 Andreas
       }
1003 193a2982 Andreas
1004 350ffa92 Andreas
       $output = substr($output, 0, strlen($output)-2);
1005 ca733ba2 Andreas
       return $output;
1006 350ffa92 Andreas
1007 193a2982 Andreas
    } // getAvailableTags()
1008 193a2982 Andreas
1009 7f50c5e3 Andreas
    /**
1010 7f50c5e3 Andreas
     * output all selected tags
1011 7f50c5e3 Andreas
     *
1012 7f50c5e3 Andreas
     * this function output all tags which have been selected
1013 7f50c5e3 Andreas
     * by the user. the selected tags are stored in the
1014 7f50c5e3 Andreas
     * session-variable $_SESSION['selected_tags']
1015 2765fc08 Andreas
     * @return string
1016 7f50c5e3 Andreas
     */
1017 4552617d Andreas
    public function getSelectedTags($type = 'link')
1018 193a2982 Andreas
    {
1019 2c33b25c Andreas
       /* retrive tags from database */
1020 b44a5c35 Andreas
       $this->get_tags();
1021 b44a5c35 Andreas
1022 3567b899 Andreas
       $output = "";
1023 2c33b25c Andreas
1024 193a2982 Andreas
       foreach($this->avail_tags as $tag)
1025 193a2982 Andreas
       {
1026 193a2982 Andreas
          // return all selected tags
1027 193a2982 Andreas
          if(isset($_SESSION['selected_tags']) && in_array($tag, $_SESSION['selected_tags'])) {
1028 4552617d Andreas
1029 4552617d Andreas
             switch($type) {
1030 4552617d Andreas
                default:
1031 4552617d Andreas
                case 'link':
1032 4552617d Andreas
                   $output.= "<a href=\"javascript:Tags('del', ". $tag .");\" class=\"tag\">". $this->tags[$tag] ."</a>, ";
1033 4552617d Andreas
                   break;
1034 4552617d Andreas
                case 'img':
1035 4552617d Andreas
                   $output.= "
1036 f5e9c6d9 Andreas
                   <div class=\"tagresulttag\">
1037 f5e9c6d9 Andreas
                    <a href=\"javascript:Tags('del', ". $tag .");\" title=\"". $this->tags[$tag] ."\">
1038 f5e9c6d9 Andreas
                     <img src=\"". $this->cfg->web_path ."/phpfspot_img.php?tagidx=". $tag ."\" />
1039 f5e9c6d9 Andreas
                    </a>
1040 4552617d Andreas
                   </div>
1041 4552617d Andreas
                   ";
1042 4552617d Andreas
                   break;
1043 4552617d Andreas
             }
1044 193a2982 Andreas
          }
1045 193a2982 Andreas
       }
1046 193a2982 Andreas
1047 2c33b25c Andreas
       if($output != "") {
1048 2c33b25c Andreas
          $output = substr($output, 0, strlen($output)-2);
1049 2c33b25c Andreas
          return $output;
1050 2c33b25c Andreas
       }
1051 2c33b25c Andreas
       else {
1052 2c33b25c Andreas
          return "no tags selected";
1053 2c33b25c Andreas
       }
1054 3567b899 Andreas
1055 193a2982 Andreas
    } // getSelectedTags()
1056 193a2982 Andreas
1057 7f50c5e3 Andreas
    /**
1058 7f50c5e3 Andreas
     * add tag to users session variable
1059 7f50c5e3 Andreas
     *
1060 7f50c5e3 Andreas
     * this function will add the specified to users current
1061 7f50c5e3 Andreas
     * tag selection. if a date search has been made before
1062 7f50c5e3 Andreas
     * it will be now cleared
1063 2765fc08 Andreas
     * @return string
1064 7f50c5e3 Andreas
     */
1065 193a2982 Andreas
    public function addTag($tag)
1066 193a2982 Andreas
    {
1067 193a2982 Andreas
       if(!isset($_SESSION['selected_tags']))
1068 193a2982 Andreas
          $_SESSION['selected_tags'] = Array();
1069 193a2982 Andreas
1070 213a12db Andreas
       if(isset($_SESSION['searchfor_tag']))
1071 213a12db Andreas
          unset($_SESSION['searchfor_tag']);
1072 e1853119 Andreas
1073 8b5da769 Andreas
       // has the user requested to hide this tag, and still someone,
1074 8b5da769 Andreas
       // somehow tries to add it, don't allow this.
1075 8b5da769 Andreas
       if(!isset($this->cfg->hide_tags) &&
1076 8b5da769 Andreas
          in_array($this->get_tag_name($tag), $this->cfg->hide_tags))
1077 8b5da769 Andreas
          return "ok";
1078 8b5da769 Andreas
1079 64aff8ea Andreas
       if(!in_array($tag, $_SESSION['selected_tags']))
1080 64aff8ea Andreas
          array_push($_SESSION['selected_tags'], $tag);
1081 d9ee4599 Andreas
1082 d9ee4599 Andreas
       return "ok";
1083 193a2982 Andreas
1084 193a2982 Andreas
    } // addTag()
1085 193a2982 Andreas
1086 7f50c5e3 Andreas
    /**
1087 7f50c5e3 Andreas
     * remove tag to users session variable
1088 7f50c5e3 Andreas
     *
1089 7f50c5e3 Andreas
     * this function removes the specified tag from
1090 7f50c5e3 Andreas
     * users current tag selection
1091 2765fc08 Andreas
     * @param string $tag
1092 2765fc08 Andreas
     * @return string
1093 7f50c5e3 Andreas
     */
1094 193a2982 Andreas
    public function delTag($tag)
1095 193a2982 Andreas
    {
1096 213a12db Andreas
       if(isset($_SESSION['searchfor_tag']))
1097 213a12db Andreas
          unset($_SESSION['searchfor_tag']);
1098 e1853119 Andreas
1099 193a2982 Andreas
       if(isset($_SESSION['selected_tags'])) {
1100 193a2982 Andreas
          $key = array_search($tag, $_SESSION['selected_tags']);
1101 193a2982 Andreas
          unset($_SESSION['selected_tags'][$key]);
1102 d2108af3 Andreas
          sort($_SESSION['selected_tags']);
1103 193a2982 Andreas
       }
1104 193a2982 Andreas
1105 d9ee4599 Andreas
       return "ok";
1106 d9ee4599 Andreas
1107 193a2982 Andreas
    } // delTag()
1108 193a2982 Andreas
1109 7f50c5e3 Andreas
    /**
1110 7f50c5e3 Andreas
     * reset tag selection
1111 7f50c5e3 Andreas
     *
1112 7f50c5e3 Andreas
     * if there is any tag selection, it will be
1113 7f50c5e3 Andreas
     * deleted now
1114 7f50c5e3 Andreas
     */
1115 193a2982 Andreas
    public function resetTags()
1116 193a2982 Andreas
    {
1117 4a0b6f61 Andreas
       if(isset($_SESSION['selected_tags']))
1118 4a0b6f61 Andreas
          unset($_SESSION['selected_tags']);
1119 193a2982 Andreas
1120 193a2982 Andreas
    } // resetTags()
1121 193a2982 Andreas
1122 7f50c5e3 Andreas
    /**
1123 f3f12303 Andreas
     * returns the value for the autocomplete tag-search
1124 2765fc08 Andreas
     * @return string
1125 af0be8f9 Andreas
     */
1126 af0be8f9 Andreas
    public function get_xml_tag_list()
1127 af0be8f9 Andreas
    {
1128 af0be8f9 Andreas
       if(!isset($_GET['search']) || !is_string($_GET['search']))
1129 af0be8f9 Andreas
          $_GET['search'] = '';
1130 af0be8f9 Andreas
1131 144f16a7 Andreas
       $length = 15;
1132 144f16a7 Andreas
       $i = 1;
1133 af0be8f9 Andreas
1134 af0be8f9 Andreas
       /* retrive tags from database */
1135 af0be8f9 Andreas
       $this->get_tags();
1136 af0be8f9 Andreas
1137 af0be8f9 Andreas
       $matched_tags = Array();
1138 af0be8f9 Andreas
1139 144f16a7 Andreas
       header("Content-Type: text/xml");
1140 144f16a7 Andreas
1141 144f16a7 Andreas
       $string = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
1142 144f16a7 Andreas
       $string.= "<results>\n";
1143 144f16a7 Andreas
1144 af0be8f9 Andreas
       foreach($this->avail_tags as $tag)
1145 af0be8f9 Andreas
       {
1146 af0be8f9 Andreas
          if(!empty($_GET['search']) &&
1147 af0be8f9 Andreas
             preg_match("/". $_GET['search'] ."/i", $this->tags[$tag]) &&
1148 144f16a7 Andreas
             count($matched_tags) < $length) {
1149 144f16a7 Andreas
1150 144f16a7 Andreas
             $count = $this->get_num_photos($tag);
1151 144f16a7 Andreas
1152 144f16a7 Andreas
             if($count == 1) {
1153 144f16a7 Andreas
                $string.= " <rs id=\"". $i ."\" info=\"". $count ." photo\">". $this->tags[$tag] ."</rs>\n";
1154 144f16a7 Andreas
             }
1155 144f16a7 Andreas
             else {
1156 144f16a7 Andreas
                $string.= " <rs id=\"". $i ."\" info=\"". $count ." photos\">". $this->tags[$tag] ."</rs>\n";
1157 af0be8f9 Andreas
1158 144f16a7 Andreas
             }
1159 144f16a7 Andreas
             $i++;
1160 af0be8f9 Andreas
          }
1161 af0be8f9 Andreas
1162 af0be8f9 Andreas
          /* if we have collected enough items, break out */
1163 144f16a7 Andreas
          if(count($matched_tags) >= $length)
1164 af0be8f9 Andreas
             break;
1165 af0be8f9 Andreas
       }
1166 af0be8f9 Andreas
1167 144f16a7 Andreas
       $string.= "</results>\n";
1168 af0be8f9 Andreas
1169 144f16a7 Andreas
       return $string;
1170 af0be8f9 Andreas
1171 af0be8f9 Andreas
    } // get_xml_tag_list()
1172 af0be8f9 Andreas
1173 af0be8f9 Andreas
1174 af0be8f9 Andreas
    /**
1175 7f50c5e3 Andreas
     * reset single photo
1176 7f50c5e3 Andreas
     *
1177 7f50c5e3 Andreas
     * if a specific photo was requested (external link)
1178 7f50c5e3 Andreas
     * unset the session variable now
1179 7f50c5e3 Andreas
     */
1180 15a6c1b6 Andreas
    public function resetPhotoView()
1181 15a6c1b6 Andreas
    {
1182 4a0b6f61 Andreas
       if(isset($_SESSION['current_photo']))
1183 4a0b6f61 Andreas
          unset($_SESSION['current_photo']);
1184 15a6c1b6 Andreas
1185 6198d8ac Andreas
       if(isset($_SESSION['current_version']))
1186 6198d8ac Andreas
          unset($_SESSION['current_version']);
1187 6198d8ac Andreas
1188 15a6c1b6 Andreas
    } // resetPhotoView();
1189 15a6c1b6 Andreas
1190 7f50c5e3 Andreas
    /**
1191 7f50c5e3 Andreas
     * reset tag search
1192 7f50c5e3 Andreas
     *
1193 213a12db Andreas
     * if any tag search has taken place, reset it now
1194 7f50c5e3 Andreas
     */
1195 6f67421b Andreas
    public function resetTagSearch()
1196 6f67421b Andreas
    {
1197 213a12db Andreas
       if(isset($_SESSION['searchfor_tag']))
1198 213a12db Andreas
          unset($_SESSION['searchfor_tag']);
1199 6f67421b Andreas
1200 6f67421b Andreas
    } // resetTagSearch()
1201 6f67421b Andreas
1202 213a12db Andreas
    /**
1203 213a12db Andreas
     * reset name search
1204 213a12db Andreas
     *
1205 213a12db Andreas
     * if any name search has taken place, reset it now
1206 213a12db Andreas
     */
1207 213a12db Andreas
    public function resetNameSearch()
1208 213a12db Andreas
    {
1209 213a12db Andreas
       if(isset($_SESSION['searchfor_name']))
1210 213a12db Andreas
          unset($_SESSION['searchfor_name']);
1211 213a12db Andreas
1212 213a12db Andreas
    } // resetNameSearch()
1213 213a12db Andreas
1214 213a12db Andreas
    /**
1215 7f50c5e3 Andreas
     * reset date search
1216 7f50c5e3 Andreas
     *
1217 f36faab3 Andreas
     * if any date search has taken place, reset it now.
1218 7f50c5e3 Andreas
     */
1219 4a0b6f61 Andreas
    public function resetDateSearch()
1220 4a0b6f61 Andreas
    {
1221 4a0b6f61 Andreas
       if(isset($_SESSION['from_date']))
1222 4a0b6f61 Andreas
          unset($_SESSION['from_date']);
1223 4a0b6f61 Andreas
       if(isset($_SESSION['to_date']))
1224 4a0b6f61 Andreas
          unset($_SESSION['to_date']);
1225 4a0b6f61 Andreas
1226 4a0b6f61 Andreas
    } // resetDateSearch();
1227 4a0b6f61 Andreas
1228 7f50c5e3 Andreas
    /**
1229 f36faab3 Andreas
     * reset rate search
1230 f36faab3 Andreas
     *
1231 f36faab3 Andreas
     * if any rate search has taken place, reset it now.
1232 f36faab3 Andreas
     */
1233 f36faab3 Andreas
    public function resetRateSearch()
1234 f36faab3 Andreas
    {
1235 f36faab3 Andreas
       if(isset($_SESSION['rate_from']))
1236 f36faab3 Andreas
          unset($_SESSION['rate_from']);
1237 f36faab3 Andreas
       if(isset($_SESSION['rate_to']))
1238 f36faab3 Andreas
          unset($_SESSION['rate_to']);
1239 f36faab3 Andreas
1240 f36faab3 Andreas
    } // resetRateSearch();
1241 f36faab3 Andreas
1242 f36faab3 Andreas
    /**
1243 7f50c5e3 Andreas
     * return all photo according selection
1244 7f50c5e3 Andreas
     *
1245 7f50c5e3 Andreas
     * this function returns all photos based on
1246 7f50c5e3 Andreas
     * the tag-selection, tag- or date-search.
1247 7f50c5e3 Andreas
     * the tag-search also has to take care of AND
1248 7f50c5e3 Andreas
     * and OR conjunctions
1249 2765fc08 Andreas
     * @return array
1250 7f50c5e3 Andreas
     */
1251 4b115eb0 Andreas
    public function getPhotoSelection()
1252 6216473e Andreas
    {
1253 4a0b6f61 Andreas
       $matched_photos = Array();
1254 8f46e095 Andreas
       $additional_where_cond = "";
1255 495fee6e Andreas
1256 984b068b Andreas
       if(isset($_SESSION['from_date']) && isset($_SESSION['to_date'])) {
1257 8223eed3 Andreas
          $from_date = $_SESSION['from_date'];
1258 8223eed3 Andreas
          $to_date = $_SESSION['to_date'];
1259 8f46e095 Andreas
          $additional_where_cond.= "
1260 984b068b Andreas
                p.time>='". $from_date ."'
1261 984b068b Andreas
             AND
1262 984b068b Andreas
                p.time<='". $to_date ."'
1263 984b068b Andreas
          ";
1264 984b068b Andreas
       }
1265 984b068b Andreas
1266 213a12db Andreas
       if(isset($_SESSION['searchfor_name'])) {
1267 f36faab3 Andreas
1268 f36faab3 Andreas
          /* check for previous conditions. if so add 'AND' */
1269 f36faab3 Andreas
          if(!empty($additional_where_cond)) {
1270 f36faab3 Andreas
             $additional_where_cond.= " AND ";
1271 f36faab3 Andreas
          }
1272 f36faab3 Andreas
1273 213a12db Andreas
          if($this->dbver < 9) {
1274 213a12db Andreas
             $additional_where_cond.= "
1275 213a12db Andreas
                   (
1276 213a12db Andreas
                         p.name LIKE '%". $_SESSION['searchfor_name'] ."%'
1277 213a12db Andreas
                      OR
1278 213a12db Andreas
                         p.description LIKE '%". $_SESSION['searchfor_name'] ."%'
1279 213a12db Andreas
                   )
1280 213a12db Andreas
             ";
1281 213a12db Andreas
          }
1282 0b7778c4 Andreas
          if($this->dbver < 17) {
1283 213a12db Andreas
             $additional_where_cond.= "
1284 213a12db Andreas
                   (
1285 213a12db Andreas
                         basename(p.uri) LIKE '%". $_SESSION['searchfor_name'] ."%'
1286 213a12db Andreas
                      OR
1287 213a12db Andreas
                         p.description LIKE '%". $_SESSION['searchfor_name'] ."%'
1288 213a12db Andreas
                   )
1289 213a12db Andreas
             ";
1290 213a12db Andreas
          }
1291 0b7778c4 Andreas
          else {
1292 0b7778c4 Andreas
             $additional_where_cond.= "
1293 0b7778c4 Andreas
                   (
1294 0b7778c4 Andreas
                         p.filename LIKE '%". $_SESSION['searchfor_name'] ."%'
1295 0b7778c4 Andreas
                      OR
1296 0b7778c4 Andreas
                         p.description LIKE '%". $_SESSION['searchfor_name'] ."%'
1297 0b7778c4 Andreas
                   )
1298 0b7778c4 Andreas
             ";
1299 0b7778c4 Andreas
          }
1300 213a12db Andreas
       }
1301 213a12db Andreas
1302 f36faab3 Andreas
       /* limit result based on rate-search */
1303 f36faab3 Andreas
       if(isset($_SESSION['rate_from']) && isset($_SESSION['rate_to'])) {
1304 f36faab3 Andreas
1305 f36faab3 Andreas
          if($this->dbver > 10) {
1306 f36faab3 Andreas
1307 f36faab3 Andreas
             /* check for previous conditions. if so add 'AND' */
1308 f36faab3 Andreas
             if(!empty($additional_where_cond)) {
1309 f36faab3 Andreas
                $additional_where_cond.= " AND ";
1310 f36faab3 Andreas
             }
1311 f36faab3 Andreas
1312 f36faab3 Andreas
             $additional_where_cond.= "
1313 f36faab3 Andreas
                      p.rating >= ". $_SESSION['rate_from'] ."
1314 f36faab3 Andreas
                   AND
1315 f36faab3 Andreas
                      p.rating <= ". $_SESSION['rate_to'] ."
1316 f36faab3 Andreas
             ";
1317 f36faab3 Andreas
          }
1318 f36faab3 Andreas
       }
1319 f36faab3 Andreas
1320 2f381082 Andreas
       if(isset($_SESSION['sort_order'])) {
1321 2f381082 Andreas
          $order_str = $this->get_sort_order();
1322 2f381082 Andreas
       }
1323 2f381082 Andreas
1324 09ab990f Andreas
       /* return a search result */
1325 213a12db Andreas
       if(isset($_SESSION['searchfor_tag']) && $_SESSION['searchfor_tag'] != '') {
1326 984b068b Andreas
          $query_str = "
1327 6216473e Andreas
             SELECT DISTINCT
1328 6216473e Andreas
                pt1.photo_id as photo_id
1329 6216473e Andreas
             FROM
1330 6216473e Andreas
                photo_tags pt1
1331 1a8de008 Andreas
             INNER JOIN photo_tags pt2
1332 1a8de008 Andreas
                ON pt1.photo_id=pt2.photo_id
1333 757e336f Andreas
             INNER JOIN tags t
1334 757e336f Andreas
                ON pt1.tag_id=t.id
1335 09ab990f Andreas
             INNER JOIN photos p
1336 1a8de008 Andreas
                ON pt1.photo_id=p.id
1337 1a8de008 Andreas
             INNER JOIN tags t2
1338 1a8de008 Andreas
                ON pt2.tag_id=t2.id
1339 213a12db Andreas
             WHERE t.name LIKE '%". $_SESSION['searchfor_tag'] ."%' ";
1340 984b068b Andreas
1341 f36faab3 Andreas
          if(!empty($additional_where_cond))
1342 984b068b Andreas
             $query_str.= "AND ". $additional_where_cond ." ";
1343 ba2bd867 Andreas
1344 ba2bd867 Andreas
          if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
1345 1a8de008 Andreas
             $query_str.= "AND t2.name IN ('".implode("','",$this->cfg->show_tags)."')";
1346 ba2bd867 Andreas
          }
1347 ba2bd867 Andreas
1348 2f381082 Andreas
          if(isset($order_str))
1349 2f381082 Andreas
             $query_str.= $order_str;
1350 2f381082 Andreas
1351 984b068b Andreas
          $result = $this->db->db_query($query_str);
1352 09ab990f Andreas
          while($row = $this->db->db_fetch_object($result)) {
1353 4a0b6f61 Andreas
             array_push($matched_photos, $row['photo_id']);
1354 09ab990f Andreas
          }
1355 4a0b6f61 Andreas
          return $matched_photos;
1356 09ab990f Andreas
       }
1357 09ab990f Andreas
1358 09ab990f Andreas
       /* return according the selected tags */
1359 dc815d80 Andreas
       if(isset($_SESSION['selected_tags']) && !empty($_SESSION['selected_tags'])) {
1360 4e4feabf Andreas
          $selected = "";
1361 4e4feabf Andreas
          foreach($_SESSION['selected_tags'] as $tag)
1362 4e4feabf Andreas
             $selected.= $tag .",";
1363 4e4feabf Andreas
          $selected = substr($selected, 0, strlen($selected)-1);
1364 ad475b7b Andreas
1365 1a8de008 Andreas
          /* photo has to match at least on of the selected tags */
1366 ad475b7b Andreas
          if($_SESSION['tag_condition'] == 'or') {
1367 984b068b Andreas
             $query_str = "
1368 6216473e Andreas
                SELECT DISTINCT
1369 6216473e Andreas
                   pt1.photo_id as photo_id
1370 6216473e Andreas
                FROM
1371 6216473e Andreas
                   photo_tags pt1
1372 1a8de008 Andreas
                INNER JOIN photo_tags pt2
1373 1a8de008 Andreas
                   ON pt1.photo_id=pt2.photo_id
1374 ba2bd867 Andreas
                INNER JOIN tags t
1375 1a8de008 Andreas
                   ON pt2.tag_id=t.id
1376 1a8de008 Andreas
                INNER JOIN photos p
1377 1a8de008 Andreas
                   ON pt1.photo_id=p.id
1378 1a8de008 Andreas
                WHERE pt1.tag_id IN (". $selected .")
1379 984b068b Andreas
             ";
1380 f36faab3 Andreas
             if(!empty($additional_where_cond))
1381 984b068b Andreas
                $query_str.= "AND ". $additional_where_cond ." ";
1382 ba2bd867 Andreas
1383 ba2bd867 Andreas
             if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
1384 5ea211de Andreas
                $query_str.= "AND t.name IN ('".implode("','",$this->cfg->show_tags)."')";
1385 ba2bd867 Andreas
             }
1386 ba2bd867 Andreas
1387 2f381082 Andreas
             if(isset($order_str))
1388 2f381082 Andreas
                $query_str.= $order_str;
1389 ad475b7b Andreas
          }
1390 1a8de008 Andreas
          /* photo has to match all selected tags */
1391 ad475b7b Andreas
          elseif($_SESSION['tag_condition'] == 'and') {
1392 ad475b7b Andreas
1393 086cf84b Andreas
             if(count($_SESSION['selected_tags']) >= 32) {
1394 086cf84b Andreas
                print "A SQLite limit of 32 tables within a JOIN SELECT avoids to<br />\n";
1395 086cf84b Andreas
                print "evaluate your tag selection. Please remove some tags from your selection.\n";
1396 086cf84b Andreas
                return Array();
1397 086cf84b Andreas
             }
1398 086cf84b Andreas
1399 d2108af3 Andreas
             /* Join together a table looking like
1400 d2108af3 Andreas
1401 d2108af3 Andreas
                pt1.photo_id pt1.tag_id pt2.photo_id pt2.tag_id ...
1402 d2108af3 Andreas
1403 d2108af3 Andreas
                so the query can quickly return all images matching the
1404 d2108af3 Andreas
                selected tags in an AND condition
1405 d2108af3 Andreas
1406 ad475b7b Andreas
             */
1407 d2108af3 Andreas
1408 d2108af3 Andreas
             $query_str = "
1409 6216473e Andreas
                SELECT DISTINCT
1410 6216473e Andreas
                   pt1.photo_id as photo_id
1411 6216473e Andreas
                FROM
1412 6216473e Andreas
                   photo_tags pt1
1413 d2108af3 Andreas
             ";
1414 d2108af3 Andreas
1415 ba2bd867 Andreas
             if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
1416 ba2bd867 Andreas
                $query_str.= "
1417 ba2bd867 Andreas
                   INNER JOIN tags t
1418 ba2bd867 Andreas
                      ON pt1.tag_id=t.id
1419 ba2bd867 Andreas
                ";
1420 ba2bd867 Andreas
             }
1421 ba2bd867 Andreas
1422 d2108af3 Andreas
             for($i = 0; $i < count($_SESSION['selected_tags']); $i++) {
1423 d2108af3 Andreas
                $query_str.= "
1424 d2108af3 Andreas
                   INNER JOIN photo_tags pt". ($i+2) ."
1425 d2108af3 Andreas
                      ON pt1.photo_id=pt". ($i+2) .".photo_id
1426 d2108af3 Andreas
                ";
1427 d2108af3 Andreas
             }
1428 51ab51f6 Andreas
             $query_str.= "
1429 51ab51f6 Andreas
                INNER JOIN photos p
1430 51ab51f6 Andreas
                   ON pt1.photo_id=p.id
1431 51ab51f6 Andreas
             ";
1432 33dba06c Andreas
             $query_str.= "WHERE pt2.tag_id=". $_SESSION['selected_tags'][0]." ";
1433 d2108af3 Andreas
             for($i = 1; $i < count($_SESSION['selected_tags']); $i++) {
1434 d2108af3 Andreas
                $query_str.= "
1435 33dba06c Andreas
                   AND pt". ($i+2) .".tag_id=". $_SESSION['selected_tags'][$i] ."
1436 d2108af3 Andreas
                ";
1437 ad475b7b Andreas
             }
1438 f36faab3 Andreas
             if(!empty($additional_where_cond))
1439 984b068b Andreas
                $query_str.= "AND ". $additional_where_cond;
1440 ba2bd867 Andreas
1441 ba2bd867 Andreas
             if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
1442 5ea211de Andreas
                $query_str.= "AND t.name IN ('".implode("','",$this->cfg->show_tags). "')";
1443 ba2bd867 Andreas
             }
1444 ba2bd867 Andreas
1445 2f381082 Andreas
             if(isset($order_str))
1446 2f381082 Andreas
                $query_str.= $order_str;
1447 ba2bd867 Andreas
1448 d2108af3 Andreas
          }
1449 d2108af3 Andreas
1450 984b068b Andreas
          $result = $this->db->db_query($query_str);
1451 d2108af3 Andreas
          while($row = $this->db->db_fetch_object($result)) {
1452 4a0b6f61 Andreas
             array_push($matched_photos, $row['photo_id']);
1453 ad475b7b Andreas
          }
1454 4a0b6f61 Andreas
          return $matched_photos;
1455 495fee6e Andreas
       }
1456 495fee6e Andreas
1457 09ab990f Andreas
       /* return all available photos */
1458 984b068b Andreas
       $query_str = "
1459 6216473e Andreas
          SELECT DISTINCT
1460 6216473e Andreas
             p.id as id
1461 6216473e Andreas
          FROM
1462 6216473e Andreas
             photos p
1463 66fc274f Andreas
          LEFT JOIN photo_tags pt
1464 09ab990f Andreas
             ON p.id=pt.photo_id
1465 66fc274f Andreas
          LEFT JOIN tags t
1466 ba2bd867 Andreas
             ON pt.tag_id=t.id
1467 984b068b Andreas
       ";
1468 66fc274f Andreas
1469 f36faab3 Andreas
       if(!empty($additional_where_cond))
1470 984b068b Andreas
          $query_str.= "WHERE ". $additional_where_cond ." ";
1471 ba2bd867 Andreas
1472 ba2bd867 Andreas
       if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
1473 f36faab3 Andreas
          if(!empty($additional_where_cond))
1474 2283995d Andreas
             $query_str.= "AND t.name IN ('".implode("','",$this->cfg->show_tags). "')";
1475 2283995d Andreas
          else
1476 2283995d Andreas
             $query_str.= "WHERE t.name IN ('".implode("','",$this->cfg->show_tags). "')";
1477 ba2bd867 Andreas
       }
1478 ba2bd867 Andreas
1479 2f381082 Andreas
       if(isset($order_str))
1480 2f381082 Andreas
          $query_str.= $order_str;
1481 2f381082 Andreas
1482 984b068b Andreas
       $result = $this->db->db_query($query_str);
1483 09ab990f Andreas
       while($row = $this->db->db_fetch_object($result)) {
1484 66fc274f Andreas
          array_push($matched_photos, $row['id']);
1485 09ab990f Andreas
       }
1486 4a0b6f61 Andreas
       return $matched_photos;
1487 495fee6e Andreas
1488 4b115eb0 Andreas
    } // getPhotoSelection()
1489 495fee6e Andreas
1490 7f50c5e3 Andreas
     /**
1491 7f50c5e3 Andreas
     * control HTML ouput for photo index
1492 7f50c5e3 Andreas
     *
1493 7f50c5e3 Andreas
     * this function provides all the necessary information
1494 7f50c5e3 Andreas
     * for the photo index template.
1495 50e93956 Andreas
     * @return string
1496 7f50c5e3 Andreas
     */
1497 60b6594f Andreas
    public function showPhotoIndex()
1498 60b6594f Andreas
    {
1499 09ab990f Andreas
       $photos = $this->getPhotoSelection();
1500 546c7957 Andreas
       $current_tags = $this->getCurrentTags();
1501 c48fe9f8 Andreas
1502 ca33ffdc Andreas
       $count = count($photos);
1503 ca33ffdc Andreas
1504 1b98418f Andreas
       /* if all thumbnails should be shown on one page */
1505 e54e7297 Andreas
       if(!isset($this->cfg->thumbs_per_page) || $this->cfg->thumbs_per_page == 0) {
1506 5d397db3 Andreas
          $begin_with = 0;
1507 dc815d80 Andreas
          $end_with = $count;
1508 5d397db3 Andreas
       }
1509 1b98418f Andreas
       /* thumbnails should be splitted up in several pages */
1510 e54e7297 Andreas
       elseif($this->cfg->thumbs_per_page > 0) {
1511 5d397db3 Andreas
1512 0b99d3d1 Andreas
          if(!isset($_SESSION['begin_with']) || $_SESSION['begin_with'] == 0) {
1513 5d397db3 Andreas
             $begin_with = 0;
1514 0b99d3d1 Andreas
          }
1515 5d397db3 Andreas
          else {
1516 5d397db3 Andreas
             $begin_with = $_SESSION['begin_with'];
1517 5d397db3 Andreas
          }
1518 5d397db3 Andreas
1519 e54e7297 Andreas
          $end_with = $begin_with + $this->cfg->thumbs_per_page;
1520 5d397db3 Andreas
       }
1521 dc815d80 Andreas
1522 e54e7297 Andreas
       $thumbs = 0;
1523 ca33ffdc Andreas
1524 dc815d80 Andreas
       for($i = $begin_with; $i < $end_with; $i++) {
1525 ca33ffdc Andreas
1526 ea36e81b Andreas
          if(!isset($photos[$i]))
1527 ea36e81b Andreas
             continue;
1528 ea36e81b Andreas
1529 ea36e81b Andreas
          /* on first run, initalize all used variables */
1530 ea36e81b Andreas
          if($thumbs == 0) {
1531 ea36e81b Andreas
             $images = Array();
1532 ea36e81b Andreas
             $images[$thumbs]        = Array();
1533 ea36e81b Andreas
             $img_height[$thumbs]    = Array();
1534 ea36e81b Andreas
             $img_width[$thumbs]     = Array();
1535 ea36e81b Andreas
             $img_id[$thumbs]        = Array();
1536 ea36e81b Andreas
             $img_name[$thumbs]      = Array();
1537 ea36e81b Andreas
             $img_fullname[$thumbs]  = Array();
1538 ea36e81b Andreas
             $img_title              = Array();
1539 ea36e81b Andreas
             $img_rating             = Array();
1540 ea36e81b Andreas
          }
1541 ca33ffdc Andreas
1542 ea36e81b Andreas
          $images[$thumbs] = $photos[$i];
1543 ea36e81b Andreas
          $img_id[$thumbs] = $i;
1544 ea36e81b Andreas
          $img_name[$thumbs] = htmlspecialchars($this->getPhotoName($photos[$i], 15));
1545 ea36e81b Andreas
          $img_fullname[$thumbs] = htmlspecialchars($this->getPhotoName($photos[$i], 0));
1546 ea36e81b Andreas
          $img_title[$thumbs] = "Click to view photo ". htmlspecialchars($this->getPhotoName($photos[$i], 0));
1547 ea36e81b Andreas
          $img_rating[$thumbs] = $this->get_photo_rating($photos[$i]);
1548 dc815d80 Andreas
1549 ea36e81b Andreas
          /* get local path of the thumbnail image to be displayed */
1550 ea36e81b Andreas
          $thumb_path = $this->get_thumb_path($this->cfg->thumb_width, $photos[$i], $this->get_latest_version($photos[$i]));
1551 dc815d80 Andreas
1552 ea36e81b Andreas
          /* if the image exist and is readable, extract some details */
1553 ea36e81b Andreas
          if(file_exists($thumb_path) && is_readable($thumb_path)) {
1554 ea36e81b Andreas
             if($info = getimagesize($thumb_path) !== false) {
1555 e54e7297 Andreas
                $img_width[$thumbs] = $info[0];
1556 e54e7297 Andreas
                $img_height[$thumbs] = $info[1];
1557 d4e39726 Andreas
             }
1558 ea36e81b Andreas
          }
1559 ea36e81b Andreas
          $thumbs++;
1560 d4e39726 Andreas
       }
1561 ca33ffdc Andreas
1562 213a12db Andreas
       if(isset($_SESSION['searchfor_tag']) && $_SESSION['searchfor_tag'] != '')
1563 213a12db Andreas
          $this->tmpl->assign('searchfor_tag', $_SESSION['searchfor_tag']);
1564 60b6594f Andreas
1565 5bf0159a Andreas
       if(isset($_SESSION['from_date']) && isset($_SESSION['to_date'])) {
1566 8223eed3 Andreas
          $this->tmpl->assign('from_date', $this->ts2str($_SESSION['from_date']));
1567 8223eed3 Andreas
          $this->tmpl->assign('to_date', $this->ts2str($_SESSION['to_date']));
1568 5bf0159a Andreas
       }
1569 5bf0159a Andreas
1570 5bf0159a Andreas
       if(isset($_SESSION['selected_tags']) && !empty($_SESSION['selected_tags'])) {
1571 5bf0159a Andreas
          $this->tmpl->assign('tag_result', 1);
1572 5bf0159a Andreas
       }
1573 5bf0159a Andreas
1574 7d9e5791 Andreas
       /* do we have to display the page selector ? */
1575 e54e7297 Andreas
       if($this->cfg->thumbs_per_page != 0) {
1576 0b99d3d1 Andreas
1577 0b99d3d1 Andreas
          $page_select = "";
1578 7258a413 Andreas
1579 7258a413 Andreas
          /* calculate the page switchers */
1580 e54e7297 Andreas
          $previous_start = $begin_with - $this->cfg->thumbs_per_page;
1581 e54e7297 Andreas
          $next_start = $begin_with + $this->cfg->thumbs_per_page;
1582 dc815d80 Andreas
1583 dc815d80 Andreas
          if($begin_with != 0)
1584 dc815d80 Andreas
             $this->tmpl->assign("previous_url", "javascript:showPhotoIndex(". $previous_start .");");
1585 dc815d80 Andreas
          if($end_with < $count)
1586 dc815d80 Andreas
             $this->tmpl->assign("next_url", "javascript:showPhotoIndex(". $next_start .");");
1587 7258a413 Andreas
1588 e54e7297 Andreas
          $photo_per_page  = $this->cfg->thumbs_per_page;
1589 7258a413 Andreas
          $last_page = ceil($count / $photo_per_page);
1590 7258a413 Andreas
1591 7d9e5791 Andreas
          /* get the current selected page */
1592 7258a413 Andreas
          if($begin_with == 0) {
1593 7258a413 Andreas
             $current_page = 1;
1594 7d9e5791 Andreas
          } else {
1595 7258a413 Andreas
             $current_page = 0;
1596 7258a413 Andreas
             for($i = $begin_with; $i >= 0; $i-=$photo_per_page) {
1597 7258a413 Andreas
                $current_page++;
1598 7258a413 Andreas
             }
1599 7258a413 Andreas
          }
1600 7258a413 Andreas
1601 d999f868 Andreas
          $dotdot_made = 0;
1602 d999f868 Andreas
1603 7258a413 Andreas
          for($i = 1; $i <= $last_page; $i++) {
1604 7258a413 Andreas
1605 7258a413 Andreas
             if($current_page == $i)
1606 1990884e Andreas
                $style = "style=\"font-size: 125%; text-decoration: underline;\"";
1607 7258a413 Andreas
             elseif($current_page-1 == $i || $current_page+1 == $i)
1608 7258a413 Andreas
                $style = "style=\"font-size: 105%;\"";
1609 7258a413 Andreas
             elseif(($current_page-5 >= $i) && ($i != 1) ||
1610 7258a413 Andreas
                ($current_page+5 <= $i) && ($i != $last_page))
1611 7258a413 Andreas
                $style = "style=\"font-size: 75%;\"";
1612 7258a413 Andreas
             else
1613 7258a413 Andreas
                $style = "";
1614 7258a413 Andreas
1615 8a63e413 Andreas
             $start_with = ($i*$photo_per_page)-$photo_per_page;
1616 8a63e413 Andreas
1617 8a63e413 Andreas
             if($this->is_user_friendly_url()) {
1618 8a63e413 Andreas
                $select = "<a href=\"". $this->cfg->web_path ."/tag/205/". $start_with ."\"";
1619 8a63e413 Andreas
             }
1620 8a63e413 Andreas
             else {
1621 8a63e413 Andreas
                $select = "<a href=\"". $this->cfg->web_path ."/index.php?mode=showpi&nbsp;tags=". $current_tags ."&nbsp;begin_with=". $begin_with ."\"";
1622 8a63e413 Andreas
             }
1623 8a63e413 Andreas
             $select.= " onclick=\"showPhotoIndex(". $start_with ."); return false;\"";
1624 8a63e413 Andreas
1625 7d9e5791 Andreas
                if($style != "")
1626 7d9e5791 Andreas
                   $select.= $style;
1627 7d9e5791 Andreas
             $select.= ">". $i ."</a>&nbsp;";
1628 7d9e5791 Andreas
1629 7d9e5791 Andreas
             // until 9 pages we show the selector from 1-9
1630 7d9e5791 Andreas
             if($last_page <= 9) {
1631 7d9e5791 Andreas
                $page_select.= $select;
1632 7d9e5791 Andreas
                continue;
1633 7d9e5791 Andreas
             } else {
1634 7d9e5791 Andreas
                if($i == 1 /* first page */ ||
1635 7d9e5791 Andreas
                   $i == $last_page /* last page */ ||
1636 7d9e5791 Andreas
                   $i == $current_page /* current page */ ||
1637 7d9e5791 Andreas
                   $i == ceil($last_page * 0.25) /* first quater */ ||
1638 7d9e5791 Andreas
                   $i == ceil($last_page * 0.5) /* half */ ||
1639 7d9e5791 Andreas
                   $i == ceil($last_page * 0.75) /* third quater */ ||
1640 7d9e5791 Andreas
                   (in_array($i, array(1,2,3,4,5,6)) && $current_page <= 4) /* the first 6 */ ||
1641 7d9e5791 Andreas
                   (in_array($i, array($last_page, $last_page-1, $last_page-2, $last_page-3, $last_page-4, $last_page-5)) && $current_page >= $last_page-4) /* the last 6 */ ||
1642 7d9e5791 Andreas
                   $i == $current_page-3 || $i == $current_page-2 || $i == $current_page-1 /* three before */ ||
1643 7d9e5791 Andreas
                   $i == $current_page+3 || $i == $current_page+2 || $i == $current_page+1 /* three after */) {
1644 7d9e5791 Andreas
1645 7d9e5791 Andreas
                   $page_select.= $select;
1646 d999f868 Andreas
                   $dotdot_made = 0;
1647 7d9e5791 Andreas
                   continue;
1648 7d9e5791 Andreas
1649 7d9e5791 Andreas
                }
1650 7d9e5791 Andreas
             }
1651 7258a413 Andreas
1652 d999f868 Andreas
             if(!$dotdot_made) {
1653 d999f868 Andreas
                $page_select.= ".........&nbsp;";
1654 d999f868 Andreas
                $dotdot_made = 1;
1655 d999f868 Andreas
             }
1656 7258a413 Andreas
          }
1657 7258a413 Andreas
1658 583a68ff Andreas
          /* only show the page selector if we have more then one page */
1659 583a68ff Andreas
          if($last_page > 1)
1660 583a68ff Andreas
             $this->tmpl->assign('page_selector', $page_select);
1661 dc815d80 Andreas
       }
1662 15a6c1b6 Andreas
1663 cac16f33 Andreas
       $extern_link = "index.php?mode=showpi";
1664 7ff7dfb9 Andreas
       $rss_link = "index.php?mode=rss";
1665 8f56c03f Andreas
       if($current_tags != "") {
1666 8f56c03f Andreas
          $extern_link.= "&tags=". $current_tags;
1667 7ff7dfb9 Andreas
          $rss_link.= "&tags=". $current_tags;
1668 8f56c03f Andreas
       }
1669 3d567f23 Andreas
       if(isset($_SESSION['from_date']) && isset($_SESSION['to_date'])) {
1670 8223eed3 Andreas
          $extern_link.= "&from_date=". $this->ts2str($_SESSION['from_date']) ."&to_date=". $this->ts2str($_SESSION['to_date']);
1671 7ff7dfb9 Andreas
          $rss_link.= "&from_date=". $this->ts2str($_SESSION['from_date']) ."&to_date=". $this->ts2str($_SESSION['to_date']);
1672 3d567f23 Andreas
       }
1673 1bf32ff1 Andreas
1674 e0ff14d4 Andreas
       $export_link = "index.php?mode=export";
1675 005562e4 Andreas
       $slideshow_link = "index.php?mode=slideshow";
1676 e0ff14d4 Andreas
1677 1bf32ff1 Andreas
       $this->tmpl->assign('extern_link', $extern_link);
1678 005562e4 Andreas
       $this->tmpl->assign('slideshow_link', $slideshow_link);
1679 e0ff14d4 Andreas
       $this->tmpl->assign('export_link', $export_link);
1680 7ff7dfb9 Andreas
       $this->tmpl->assign('rss_link', $rss_link);
1681 ca33ffdc Andreas
       $this->tmpl->assign('count', $count);
1682 ca33ffdc Andreas
       $this->tmpl->assign('width', $this->cfg->thumb_width);
1683 33c1f8bc Andreas
       $this->tmpl->assign('preview_width', $this->cfg->photo_width);
1684 7eaf3d14 Andreas
       $this->tmpl->assign('thumb_container_width', $this->cfg->thumb_width);
1685 7eaf3d14 Andreas
       $this->tmpl->assign('thumb_container_height', $this->cfg->thumb_height+20);
1686 4552617d Andreas
       $this->tmpl->assign('selected_tags', $this->getSelectedTags('img'));
1687 ea36e81b Andreas
       // +1 for for smarty's selection iteration
1688 ea36e81b Andreas
       $this->tmpl->assign('thumbs', $thumbs+1);
1689 ea36e81b Andreas
1690 ea36e81b Andreas
       if($thumbs > 0) {
1691 ea36e81b Andreas
          $this->tmpl->assign('images', $images);
1692 ea36e81b Andreas
          $this->tmpl->assign('img_width', $img_width);
1693 ea36e81b Andreas
          $this->tmpl->assign('img_height', $img_height);
1694 ea36e81b Andreas
          $this->tmpl->assign('img_id', $img_id);
1695 ea36e81b Andreas
          $this->tmpl->assign('img_name', $img_name);
1696 ea36e81b Andreas
          $this->tmpl->assign('img_fullname', $img_fullname);
1697 ea36e81b Andreas
          $this->tmpl->assign('img_title', $img_title);
1698 ea36e81b Andreas
          $this->tmpl->assign('img_rating', $img_rating);
1699 ea36e81b Andreas
       }
1700 4c5aa671 Andreas
1701 867cf65e Andreas
       $result = $this->tmpl->fetch("photo_index.tpl");
1702 5dd7a5cd Andreas
1703 addc9889 Andreas
       /* if we are returning to photo index from an photo-view,
1704 addc9889 Andreas
          scroll the window to the last shown photo-thumbnail.
1705 addc9889 Andreas
          after this, unset the last_photo session variable.
1706 addc9889 Andreas
       */
1707 addc9889 Andreas
       if(isset($_SESSION['last_photo'])) {
1708 867cf65e Andreas
          $result.= "<script language=\"JavaScript\">moveToThumb(". $_SESSION['last_photo'] .");</script>\n";
1709 addc9889 Andreas
          unset($_SESSION['last_photo']);
1710 addc9889 Andreas
       }
1711 5dd7a5cd Andreas
1712 867cf65e Andreas
       return $result;
1713 867cf65e Andreas
1714 60b6594f Andreas
    } // showPhotoIndex()
1715 60b6594f Andreas
1716 7f50c5e3 Andreas
    /**
1717 7f50c5e3 Andreas
     * show credit template
1718 7f50c5e3 Andreas
     */
1719 588923cb Andreas
    public function showCredits()
1720 588923cb Andreas
    {
1721 588923cb Andreas
       $this->tmpl->assign('version', $this->cfg->version);
1722 588923cb Andreas
       $this->tmpl->assign('product', $this->cfg->product);
1723 e8bc8f7d Andreas
       $this->tmpl->assign('db_version', $this->dbver);
1724 588923cb Andreas
       $this->tmpl->show("credits.tpl");
1725 588923cb Andreas
1726 588923cb Andreas
    } // showCredits()
1727 d902cdfd Andreas
1728 7f50c5e3 Andreas
    /**
1729 e1730519 Andreas
     * create thumbnails for the requested width
1730 7f50c5e3 Andreas
     *
1731 7f50c5e3 Andreas
     * this function creates image thumbnails of $orig_image
1732 7f50c5e3 Andreas
     * stored as $thumb_image. It will check if the image is
1733 7f50c5e3 Andreas
     * in a supported format, if necessary rotate the image
1734 7f50c5e3 Andreas
     * (based on EXIF orientation meta headers) and re-sizing.
1735 2765fc08 Andreas
     * @param string $orig_image
1736 2765fc08 Andreas
     * @param string $thumb_image
1737 2765fc08 Andreas
     * @param integer $width
1738 2765fc08 Andreas
     * @return boolean
1739 7f50c5e3 Andreas
     */
1740 3b285de3 Andreas
    public function create_thumbnail($orig_image, $thumb_image, $width)
1741 abc9aff6 Andreas
    {
1742 0a5ca001 Andreas
       if(!file_exists($orig_image)) {
1743 474ad4bf Andreas
          return false;
1744 0a5ca001 Andreas
       }
1745 474ad4bf Andreas
1746 e1730519 Andreas
       $mime = $this->get_mime_info($orig_image);
1747 e1730519 Andreas
1748 eda7f279 Andreas
       /* check if original photo is a support image type */
1749 e1730519 Andreas
       if(!$this->checkifImageSupported($mime))
1750 eda7f279 Andreas
          return false;
1751 eda7f279 Andreas
1752 e1730519 Andreas
       switch($mime) {
1753 8ab8f2cd Andreas
1754 8ab8f2cd Andreas
          case 'image/jpeg':
1755 8ab8f2cd Andreas
1756 8ab8f2cd Andreas
             $meta = $this->get_meta_informations($orig_image);
1757 8ab8f2cd Andreas
1758 8ab8f2cd Andreas
             $rotate = 0;
1759 8ab8f2cd Andreas
             $flip_hori = false;
1760 8ab8f2cd Andreas
             $flip_vert = false;
1761 8ab8f2cd Andreas
1762 9609cbda Andreas
             if(isset($meta['Orientation'])) {
1763 9609cbda Andreas
                switch($meta['Orientation']) {
1764 9609cbda Andreas
                   case 1: /* top, left */
1765 9609cbda Andreas
                      /* nothing to do */ break;
1766 9609cbda Andreas
                   case 2: /* top, right */
1767 9609cbda Andreas
                      $rotate = 0; $flip_hori = true; break;
1768 9609cbda Andreas
                   case 3: /* bottom, left */
1769 9609cbda Andreas
                      $rotate = 180; break;
1770 9609cbda Andreas
                   case 4: /* bottom, right */
1771 9609cbda Andreas
                      $flip_vert = true; break;
1772 9609cbda Andreas
                   case 5: /* left side, top */
1773 9609cbda Andreas
                      $rotate = 90; $flip_vert = true; break;
1774 9609cbda Andreas
                   case 6: /* right side, top */
1775 9609cbda Andreas
                      $rotate = 90; break;
1776 9609cbda Andreas
                   case 7: /* left side, bottom */
1777 9609cbda Andreas
                      $rotate = 270; $flip_vert = true; break;
1778 9609cbda Andreas
                   case 8: /* right side, bottom */
1779 9609cbda Andreas
                      $rotate = 270; break;
1780 9609cbda Andreas
                }
1781 8ab8f2cd Andreas
             }
1782 8ab8f2cd Andreas
1783 8ab8f2cd Andreas
             $src_img = @imagecreatefromjpeg($orig_image);
1784 375de618 Andreas
             $handler = "gd";
1785 8ab8f2cd Andreas
             break;
1786 8ab8f2cd Andreas
1787 8ab8f2cd Andreas
          case 'image/png':
1788 8ab8f2cd Andreas
1789 8ab8f2cd Andreas
             $src_img = @imagecreatefrompng($orig_image);
1790 375de618 Andreas
             $handler = "gd";
1791 8ab8f2cd Andreas
             break;
1792 8ab8f2cd Andreas
1793 e1730519 Andreas
          case 'image/x-portable-pixmap':
1794 375de618 Andreas
1795 375de618 Andreas
             $src_img = new Imagick($orig_image);
1796 375de618 Andreas
             $handler = "imagick";
1797 375de618 Andreas
             break;
1798 abc9aff6 Andreas
1799 474ad4bf Andreas
       }
1800 abc9aff6 Andreas
1801 e1730519 Andreas
       if(!isset($src_img) || empty($src_img)) {
1802 e1730519 Andreas
          print "Can't load image from ". $orig_image ."\n";
1803 e1730519 Andreas
          return false;
1804 e1730519 Andreas
       }
1805 abc9aff6 Andreas
1806 375de618 Andreas
       switch($handler) {
1807 abc9aff6 Andreas
1808 375de618 Andreas
          case 'gd':
1809 64f41bc1 Andreas
1810 375de618 Andreas
             /* grabs the height and width */
1811 375de618 Andreas
             $cur_width = imagesx($src_img);
1812 375de618 Andreas
             $cur_height = imagesy($src_img);
1813 375de618 Andreas
1814 375de618 Andreas
             // If requested width is more then the actual image width,
1815 375de618 Andreas
             // do not generate a thumbnail, instead safe the original
1816 375de618 Andreas
             // as thumbnail but with lower quality. But if the image
1817 375de618 Andreas
             // is to heigh too, then we still have to resize it.
1818 375de618 Andreas
             if($width >= $cur_width && $cur_height < $this->cfg->thumb_height) {
1819 375de618 Andreas
                $result = imagejpeg($src_img, $thumb_image, 75);
1820 375de618 Andreas
                imagedestroy($src_img);
1821 375de618 Andreas
                return true;
1822 375de618 Andreas
             }
1823 e1730519 Andreas
             break;
1824 abc9aff6 Andreas
1825 e1730519 Andreas
          case 'imagick':
1826 abc9aff6 Andreas
1827 e1730519 Andreas
             $cur_width = $src_img->getImageWidth();
1828 e1730519 Andreas
             $cur_height = $src_img->getImageHeight();
1829 abc9aff6 Andreas
1830 e1730519 Andreas
             // If requested width is more then the actual image width,
1831 e1730519 Andreas
             // do not generate a thumbnail, instead safe the original
1832 e1730519 Andreas
             // as thumbnail but with lower quality. But if the image
1833 e1730519 Andreas
             // is to heigh too, then we still have to resize it.
1834 e1730519 Andreas
             if($width >= $cur_width && $cur_height < $this->cfg->thumb_height) {
1835 e1730519 Andreas
                $src_img->setCompressionQuality(75);
1836 e1730519 Andreas
                $src_img->setImageFormat('jpeg');
1837 e1730519 Andreas
                $src_img->writeImage($thumb_image);
1838 e1730519 Andreas
                $src_img->clear();
1839 e1730519 Andreas
                $src_img->destroy();
1840 e1730519 Andreas
                return true;
1841 375de618 Andreas
             }
1842 e1730519 Andreas
             break;
1843 e1730519 Andreas
1844 e1730519 Andreas
       }
1845 e1730519 Andreas
1846 e1730519 Andreas
       // If the image will be rotate because EXIF orientation said so
1847 e1730519 Andreas
       // 'virtually rotate' the image for further calculations
1848 e1730519 Andreas
       if($rotate == 90 || $rotate == 270) {
1849 e1730519 Andreas
          $tmp = $cur_width;
1850 e1730519 Andreas
          $cur_width = $cur_height;
1851 e1730519 Andreas
          $cur_height = $tmp;
1852 e1730519 Andreas
       }
1853 e1730519 Andreas
1854 e1730519 Andreas
       /* calculates aspect ratio */
1855 e1730519 Andreas
       $aspect_ratio = $cur_height / $cur_width;
1856 e1730519 Andreas
1857 e1730519 Andreas
       /* sets new size */
1858 e1730519 Andreas
       if($aspect_ratio < 1) {
1859 e1730519 Andreas
          $new_w = $width;
1860 e1730519 Andreas
          $new_h = abs($new_w * $aspect_ratio);
1861 e1730519 Andreas
       } else {
1862 e1730519 Andreas
          /* 'virtually' rotate the image and calculate it's ratio */
1863 e1730519 Andreas
          $tmp_w = $cur_height;
1864 e1730519 Andreas
          $tmp_h = $cur_width;
1865 e1730519 Andreas
          /* now get the ratio from the 'rotated' image */
1866 e1730519 Andreas
          $tmp_ratio = $tmp_h/$tmp_w;
1867 e1730519 Andreas
          /* now calculate the new dimensions */
1868 e1730519 Andreas
          $tmp_w = $width;
1869 e1730519 Andreas
          $tmp_h = abs($tmp_w * $tmp_ratio);
1870 e1730519 Andreas
1871 e1730519 Andreas
          // now that we know, how high they photo should be, if it
1872 e1730519 Andreas
          // gets rotated, use this high to scale the image
1873 e1730519 Andreas
          $new_h = $tmp_h;
1874 e1730519 Andreas
          $new_w = abs($new_h / $aspect_ratio);
1875 e1730519 Andreas
1876 e1730519 Andreas
          // If the image will be rotate because EXIF orientation said so
1877 e1730519 Andreas
          // now 'virtually rotate' back the image for the image manipulation
1878 e1730519 Andreas
          if($rotate == 90 || $rotate == 270) {
1879 e1730519 Andreas
             $tmp = $new_w;
1880 e1730519 Andreas
             $new_w = $new_h;
1881 e1730519 Andreas
             $new_h = $tmp;
1882 e1730519 Andreas
          }
1883 e1730519 Andreas
       }
1884 e1730519 Andreas
1885 e1730519 Andreas
       switch($handler) {
1886 e1730519 Andreas
1887 e1730519 Andreas
          case 'gd':
1888 eaf7260e Andreas
1889 375de618 Andreas
             /* creates new image of that size */
1890 375de618 Andreas
             $dst_img = imagecreatetruecolor($new_w, $new_h);
1891 eaf7260e Andreas
1892 375de618 Andreas
             imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
1893 abc9aff6 Andreas
1894 375de618 Andreas
             /* copies resized portion of original image into new image */
1895 375de618 Andreas
             imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, imagesx($src_img), imagesy($src_img));
1896 474ad4bf Andreas
1897 375de618 Andreas
             /* needs the image to be flipped horizontal? */
1898 375de618 Andreas
             if($flip_hori) {
1899 375de618 Andreas
                $this->_debug("(FLIP)");
1900 375de618 Andreas
                $dst_img = $this->flipImage($dst_img, 'hori');
1901 375de618 Andreas
             }
1902 375de618 Andreas
             /* needs the image to be flipped vertical? */
1903 375de618 Andreas
             if($flip_vert) {
1904 375de618 Andreas
                $this->_debug("(FLIP)");
1905 375de618 Andreas
                $dst_img = $this->flipImage($dst_img, 'vert');
1906 375de618 Andreas
             }
1907 474ad4bf Andreas
1908 375de618 Andreas
             if($rotate) {
1909 375de618 Andreas
                $this->_debug("(ROTATE)");
1910 375de618 Andreas
                $dst_img = $this->rotateImage($dst_img, $rotate);
1911 375de618 Andreas
             }
1912 abc9aff6 Andreas
1913 375de618 Andreas
             /* write down new generated file */
1914 375de618 Andreas
             $result = imagejpeg($dst_img, $thumb_image, 75);
1915 375de618 Andreas
1916 375de618 Andreas
             /* free your mind */
1917 375de618 Andreas
             imagedestroy($dst_img);
1918 375de618 Andreas
             imagedestroy($src_img);
1919 375de618 Andreas
1920 375de618 Andreas
             if($result === false) {
1921 375de618 Andreas
                print "Can't write thumbnail ". $thumb_image ."\n";
1922 375de618 Andreas
                return false;
1923 375de618 Andreas
             }
1924 375de618 Andreas
1925 375de618 Andreas
             return true;
1926 375de618 Andreas
1927 375de618 Andreas
             break;
1928 375de618 Andreas
1929 375de618 Andreas
          case 'imagick':
1930 375de618 Andreas
1931 e1730519 Andreas
             $src_img->resizeImage($new_w, $new_h, Imagick::FILTER_LANCZOS, 1);
1932 e1730519 Andreas
1933 e1730519 Andreas
             /* needs the image to be flipped horizontal? */
1934 e1730519 Andreas
             if($flip_hori) {
1935 e1730519 Andreas
                $this->_debug("(FLIP)");
1936 e1730519 Andreas
                $src_img->rotateImage(new ImagickPixel(), 90);
1937 e1730519 Andreas
                $src_img->flipImage();
1938 e1730519 Andreas
                $src_img->rotateImage(new ImagickPixel(), -90);
1939 e1730519 Andreas
             }
1940 e1730519 Andreas
             /* needs the image to be flipped vertical? */
1941 e1730519 Andreas
             if($flip_vert) {
1942 e1730519 Andreas
                $this->_debug("(FLIP)");
1943 e1730519 Andreas
                $src_img->flipImage();
1944 e1730519 Andreas
             }
1945 e1730519 Andreas
1946 e1730519 Andreas
             if($rotate) {
1947 e1730519 Andreas
                $this->_debug("(ROTATE)");
1948 e1730519 Andreas
                $src_img->rotateImage(new ImagickPixel(), $rotate);
1949 e1730519 Andreas
             }
1950 e1730519 Andreas
1951 e1730519 Andreas
             $src_img->setCompressionQuality(75);
1952 e1730519 Andreas
             $src_img->setImageFormat('jpeg');
1953 e1730519 Andreas
1954 e1730519 Andreas
             if(!$src_img->writeImage($thumb_image)) {
1955 e1730519 Andreas
                print "Can't write thumbnail ". $thumb_image ."\n";
1956 e1730519 Andreas
                return false;
1957 e1730519 Andreas
             }
1958 e1730519 Andreas
1959 e1730519 Andreas
             $src_img->clear();
1960 e1730519 Andreas
             $src_img->destroy();
1961 e1730519 Andreas
             return true;
1962 e1730519 Andreas
1963 375de618 Andreas
             break;
1964 375de618 Andreas
1965 375de618 Andreas
       }
1966 474ad4bf Andreas
1967 a8f48692 Andreas
    } // create_thumbnail()
1968 abc9aff6 Andreas
1969 7f50c5e3 Andreas
    /**
1970 7f50c5e3 Andreas
     * return all exif meta data from the file
1971 2765fc08 Andreas
     * @param string $file
1972 2765fc08 Andreas
     * @return array
1973 7f50c5e3 Andreas
     */
1974 37a17e20 Andreas
    public function get_meta_informations($file)
1975 37a17e20 Andreas
    {
1976 24720217 Andreas
       return exif_read_data($file);
1977 37a17e20 Andreas
1978 37a17e20 Andreas
    } // get_meta_informations()
1979 37a17e20 Andreas
1980 7f50c5e3 Andreas
    /**
1981 7f50c5e3 Andreas
     * create phpfspot own sqlite database
1982 7f50c5e3 Andreas
     *
1983 7f50c5e3 Andreas
     * this function creates phpfspots own sqlite database
1984 7f50c5e3 Andreas
     * if it does not exist yet. this own is used to store
1985 7f50c5e3 Andreas
     * some necessary informations (md5 sum's, ...).
1986 7f50c5e3 Andreas
     */
1987 6198d8ac Andreas
    public function check_phpfspot_db()
1988 dac334b4 Andreas
    {
1989 dac334b4 Andreas
       // if the config table doesn't exist yet, create it
1990 dac334b4 Andreas
       if(!$this->cfg_db->db_check_table_exists("images")) {
1991 dac334b4 Andreas
          $this->cfg_db->db_exec("
1992 dac334b4 Andreas
             CREATE TABLE images (
1993 6198d8ac Andreas
                img_idx int,
1994 6198d8ac Andreas
                img_version_idx int,
1995 6198d8ac Andreas
                img_md5 varchar(32),
1996 6198d8ac Andreas
                UNIQUE(img_idx, img_version_idx)
1997 6198d8ac Andreas
             )
1998 6198d8ac Andreas
          ");
1999 6198d8ac Andreas
       }
2000 6198d8ac Andreas
2001 6198d8ac Andreas
       if(!$this->cfg_db->db_check_table_exists("meta")) {
2002 6198d8ac Andreas
          $this->cfg_db->db_exec("
2003 6198d8ac Andreas
             CREATE TABLE meta (
2004 6198d8ac Andreas
                meta_key varchar(255),
2005 6198d8ac Andreas
                meta_value varchar(255)
2006 6198d8ac Andreas
             )
2007 6198d8ac Andreas
          ");
2008 6198d8ac Andreas
2009 6198d8ac Andreas
          /* db_version was added with phpfspot 1.7, before changes
2010 6198d8ac Andreas
             on the phpfspot database where not necessary.
2011 6198d8ac Andreas
          */
2012 6198d8ac Andreas
2013 6198d8ac Andreas
          $this->cfg_db->db_exec("
2014 6198d8ac Andreas
             INSERT INTO meta (
2015 6198d8ac Andreas
                meta_key, meta_value
2016 6198d8ac Andreas
             ) VALUES (
2017 6198d8ac Andreas
                'phpfspot Database Version',
2018 6198d8ac Andreas
                '". $this->cfg->db_version ."'
2019 dac334b4 Andreas
             )
2020 6198d8ac Andreas
          ");
2021 dac334b4 Andreas
       }
2022 dac334b4 Andreas
2023 6198d8ac Andreas
       /* if version <= 2 and column img_version_idx does not exist yet */
2024 6198d8ac Andreas
       if($this->get_db_version() <= 2 &&
2025 6198d8ac Andreas
          !$this->cfg_db->db_check_column_exists("images", "img_version_idx")) {
2026 6198d8ac Andreas
2027 6198d8ac Andreas
          if(!$this->cfg_db->db_start_transaction())
2028 6198d8ac Andreas
             die("Can not start database transaction");
2029 6198d8ac Andreas
2030 6198d8ac Andreas
          $result = $this->cfg_db->db_exec("
2031 6198d8ac Andreas
             CREATE TEMPORARY TABLE images_temp (
2032 6198d8ac Andreas
                img_idx int,
2033 6198d8ac Andreas
                img_version_idx int,
2034 6198d8ac Andreas
                img_md5 varchar(32),
2035 6198d8ac Andreas
                UNIQUE(img_idx, img_version_idx)
2036 6198d8ac Andreas
             )
2037 6198d8ac Andreas
          ");
2038 6198d8ac Andreas
2039 6198d8ac Andreas
          if(!$result) {
2040 6198d8ac Andreas
             $this->cfg_db->db_rollback_transaction();
2041 6198d8ac Andreas
             die("Upgrade failed - transaction rollback");
2042 6198d8ac Andreas
          }
2043 6198d8ac Andreas
2044 6198d8ac Andreas
          $result = $this->cfg_db->db_exec("
2045 6198d8ac Andreas
            INSERT INTO images_temp
2046 6198d8ac Andreas
                SELECT
2047 6198d8ac Andreas
                   img_idx,
2048 6198d8ac Andreas
                   0,
2049 6198d8ac Andreas
                   img_md5
2050 6198d8ac Andreas
                FROM images
2051 6198d8ac Andreas
          ");
2052 6198d8ac Andreas
2053 6198d8ac Andreas
          if(!$result) {
2054 6198d8ac Andreas
             $this->cfg_db->db_rollback_transaction();
2055 6198d8ac Andreas
             die("Upgrade failed - transaction rollback");
2056 6198d8ac Andreas
          }
2057 6198d8ac Andreas
2058 6198d8ac Andreas
          $result = $this->cfg_db->db_exec("
2059 6198d8ac Andreas
             DROP TABLE images
2060 6198d8ac Andreas
          ");
2061 6198d8ac Andreas
2062 6198d8ac Andreas
          if(!$result) {
2063 6198d8ac Andreas
             $this->cfg_db->db_rollback_transaction();
2064 6198d8ac Andreas
             die("Upgrade failed - transaction rollback");
2065 6198d8ac Andreas
          }
2066 6198d8ac Andreas
2067 6198d8ac Andreas
          $result = $this->cfg_db->db_exec("
2068 6198d8ac Andreas
             CREATE TABLE images (
2069 6198d8ac Andreas
                img_idx int,
2070 6198d8ac Andreas
                img_version_idx int,
2071 6198d8ac Andreas
                img_md5 varchar(32),
2072 6198d8ac Andreas
                UNIQUE(img_idx, img_version_idx)
2073 6198d8ac Andreas
             )
2074 6198d8ac Andreas
          ");
2075 6198d8ac Andreas
2076 6198d8ac Andreas
          if(!$result) {
2077 6198d8ac Andreas
             $this->cfg_db->db_rollback_transaction();
2078 6198d8ac Andreas
             die("Upgrade failed - transaction rollback");
2079 6198d8ac Andreas
          }
2080 6198d8ac Andreas
2081 6198d8ac Andreas
          $result = $this->cfg_db->db_exec("
2082 6198d8ac Andreas
             INSERT INTO images
2083 6198d8ac Andreas
                SELECT *
2084 6198d8ac Andreas
                FROM images_temp
2085 6198d8ac Andreas
          ");
2086 6198d8ac Andreas
2087 6198d8ac Andreas
          if(!$result) {
2088 6198d8ac Andreas
             $this->cfg_db->db_rollback_transaction();
2089 6198d8ac Andreas
             die("Upgrade failed - transaction rollback");
2090 6198d8ac Andreas
          }
2091 6198d8ac Andreas
2092 6198d8ac Andreas
          $result = $this->cfg_db->db_exec("
2093 6198d8ac Andreas
             DROP TABLE images_temp
2094 6198d8ac Andreas
          ");
2095 6198d8ac Andreas
2096 6198d8ac Andreas
          if(!$result) {
2097 6198d8ac Andreas
             $this->cfg_db->db_rollback_transaction();
2098 6198d8ac Andreas
             die("Upgrade failed - transaction rollback");
2099 6198d8ac Andreas
          }
2100 6198d8ac Andreas
2101 6198d8ac Andreas
          if(!$this->cfg_db->db_commit_transaction())
2102 6198d8ac Andreas
             die("Can not commit database transaction");
2103 6198d8ac Andreas
2104 6198d8ac Andreas
       }
2105 6198d8ac Andreas
2106 6198d8ac Andreas
    } // check_phpfspot_db
2107 dac334b4 Andreas
2108 322fc502 Andreas
    /**
2109 6198d8ac Andreas
     * generates thumbnails
2110 322fc502 Andreas
     *
2111 6198d8ac Andreas
     * This function generates JPEG thumbnails from
2112 6198d8ac Andreas
     * provided F-Spot photo indize and its alternative
2113 6198d8ac Andreas
     * versions.
2114 322fc502 Andreas
     *
2115 322fc502 Andreas
     * 1. Check if all thumbnail generations (width) are already in place and
2116 322fc502 Andreas
     *    readable
2117 322fc502 Andreas
     * 2. Check if the md5sum of the original file has changed
2118 322fc502 Andreas
     * 3. Generate the thumbnails if needed
2119 2765fc08 Andreas
     * @param integer $idx
2120 2765fc08 Andreas
     * @param integer $force
2121 2765fc08 Andreas
     * @param boolean $overwrite
2122 322fc502 Andreas
     */
2123 aed571db Andreas
    public function gen_thumb($idx = 0, $force = 0, $overwrite = false)
2124 e1117268 Andreas
    {
2125 6a21b6f0 Andreas
       $error = 0;
2126 6198d8ac Andreas
       $versions = Array(0);
2127 6a21b6f0 Andreas
2128 322fc502 Andreas
       $resolutions = Array(
2129 322fc502 Andreas
          $this->cfg->thumb_width,
2130 322fc502 Andreas
          $this->cfg->photo_width,
2131 bcf2be54 Andreas
          $this->cfg->mini_width,
2132 4552617d Andreas
          30,
2133 322fc502 Andreas
       );
2134 e1117268 Andreas
2135 781c4674 Andreas
       /* in newer f-spot versions, also the original photos is listed
2136 781c4674 Andreas
          in photo_versions with version_id 1, name 'Original'. We have
2137 781c4674 Andreas
          to skip that one.
2138 781c4674 Andreas
       */
2139 781c4674 Andreas
       if($alt_versions = $this->get_photo_versions($idx)) {
2140 781c4674 Andreas
2141 781c4674 Andreas
          if($this->getFspotDBVersion() < 18) {
2142 781c4674 Andreas
             $versions = array_merge($versions, $alt_versions);
2143 781c4674 Andreas
          }
2144 781c4674 Andreas
          else {
2145 781c4674 Andreas
             unset($alt_versions[0]);
2146 781c4674 Andreas
             if(!empty($alt_versions))
2147 781c4674 Andreas
                $versions = array_merge($versions, $alt_versions);
2148 781c4674 Andreas
          }
2149 781c4674 Andreas
       }
2150 6a21b6f0 Andreas
2151 6198d8ac Andreas
       foreach($versions as $version) {
2152 4e9f06a1 Andreas
2153 6198d8ac Andreas
          /* get details from F-Spot's database */
2154 6198d8ac Andreas
          $details = $this->get_photo_details($idx, $version);
2155 4e9f06a1 Andreas
2156 6198d8ac Andreas
          /* calculate file MD5 sum */
2157 6198d8ac Andreas
          $full_path = $this->translate_path($this->parse_uri($details['uri'], 'fullpath'));
2158 6198d8ac Andreas
2159 6198d8ac Andreas
          if(!file_exists($full_path)) {
2160 aba6c296 Andreas
             $this->_error("File ". $full_path ." does not exist");
2161 6198d8ac Andreas
             return;
2162 6198d8ac Andreas
          }
2163 6198d8ac Andreas
2164 6198d8ac Andreas
          if(!is_readable($full_path)) {
2165 6198d8ac Andreas
             $this->_error("File ". $full_path ." is not readable for ". $this->getuid() ."\n");
2166 6198d8ac Andreas
             return;
2167 6198d8ac Andreas
          }
2168 4e9f06a1 Andreas
2169 6198d8ac Andreas
          $this->_debug("Image [". $idx ."] ". $this->shrink_text($this->parse_uri($details['uri'], 'filename'), 20) ." Thumbnails:");
2170 e1117268 Andreas
2171 6198d8ac Andreas
          /* If Nikon NEF format, we need to treat it another way */
2172 6198d8ac Andreas
          if(isset($this->cfg->dcraw_bin) &&
2173 6198d8ac Andreas
             file_exists($this->cfg->dcraw_bin) &&
2174 6198d8ac Andreas
             is_executable($this->cfg->dcraw_bin) &&
2175 6198d8ac Andreas
             preg_match('/\.nef$/i', $details['uri'])) {
2176 e1730519 Andreas
2177 6198d8ac Andreas
             $ppm_path = preg_replace('/\.nef$/i', '.ppm', $full_path);
2178 6198d8ac Andreas
2179 6198d8ac Andreas
             /* if PPM file does not exist, let dcraw convert it from NEF */
2180 6198d8ac Andreas
             if(!file_exists($ppm_path)) {
2181 6198d8ac Andreas
                system($this->cfg->dcraw_bin ." -a ". $full_path);
2182 6198d8ac Andreas
             }
2183 6198d8ac Andreas
2184 6198d8ac Andreas
             /* for now we handle the PPM instead of the NEF */
2185 6198d8ac Andreas
             $full_path = $ppm_path;
2186 e1730519 Andreas
2187 e1730519 Andreas
          }
2188 e1730519 Andreas
2189 6198d8ac Andreas
          $file_md5 = md5_file($full_path);
2190 6198d8ac Andreas
          $changes = false;
2191 e1730519 Andreas
2192 6198d8ac Andreas
          foreach($resolutions as $resolution) {
2193 e1730519 Andreas
2194 6198d8ac Andreas
             $generate_it = false;
2195 431b2c5f Andreas
2196 6198d8ac Andreas
             $thumb_sub_path = substr($file_md5, 0, 2);
2197 6198d8ac Andreas
             $thumb_path = $this->cfg->thumb_path ."/". $thumb_sub_path ."/". $resolution ."_". $file_md5;
2198 3b285de3 Andreas
2199 6198d8ac Andreas
             /* if thumbnail-subdirectory does not exist yet, create it */
2200 6198d8ac Andreas
             if(!file_exists(dirname($thumb_path))) {
2201 6198d8ac Andreas
                mkdir(dirname($thumb_path), 0755);
2202 6198d8ac Andreas
             }
2203 4ff4a0e7 Andreas
2204 6198d8ac Andreas
             /* if the thumbnail file doesn't exist, create it */
2205 6198d8ac Andreas
             if(!file_exists($thumb_path) || $force) {
2206 6198d8ac Andreas
                $generate_it = true;
2207 6198d8ac Andreas
             }
2208 6198d8ac Andreas
             elseif($file_md5 != $this->getMD5($idx, $version)) {
2209 6198d8ac Andreas
                $generate_it = true;
2210 6198d8ac Andreas
             }
2211 3b285de3 Andreas
2212 6198d8ac Andreas
             if($generate_it || $overwrite) {
2213 6198d8ac Andreas
2214 6198d8ac Andreas
                $this->_debug(" ". $resolution ."px");
2215 6198d8ac Andreas
                if(!$this->create_thumbnail($full_path, $thumb_path, $resolution))
2216 6198d8ac Andreas
                   $error = 1;
2217 aed571db Andreas
2218 6198d8ac Andreas
                $changes = true;
2219 6198d8ac Andreas
             }
2220 6198d8ac Andreas
          }
2221 322fc502 Andreas
2222 781c4674 Andreas
          if(empty($changes)) {
2223 6198d8ac Andreas
             $this->_debug(" already exist");
2224 6198d8ac Andreas
          }
2225 322fc502 Andreas
2226 6198d8ac Andreas
          /* set the new/changed MD5 sum for the current photo */
2227 6198d8ac Andreas
          if(!$error) {
2228 6198d8ac Andreas
             $this->setMD5($idx, $file_md5, $version);
2229 322fc502 Andreas
          }
2230 e1117268 Andreas
2231 6198d8ac Andreas
          $this->_debug("\n");
2232 431b2c5f Andreas
2233 6a21b6f0 Andreas
       }
2234 474ad4bf Andreas
2235 764ad0eb Andreas
    } // gen_thumb()
2236 e1117268 Andreas
2237 7f50c5e3 Andreas
    /**
2238 7f50c5e3 Andreas
     * returns stored md5 sum for a specific photo
2239 7f50c5e3 Andreas
     *
2240 6198d8ac Andreas
     * this function queries the phpfspot database for a stored MD5
2241 6198d8ac Andreas
     * checksum of the specified photo. It also takes care of the
2242 6198d8ac Andreas
     * requested photo version - original or alternative photo.
2243 6198d8ac Andreas
     *
2244 2765fc08 Andreas
     * @param integer $idx
2245 2765fc08 Andreas
     * @return string|null
2246 7f50c5e3 Andreas
     */
2247 6198d8ac Andreas
    public function getMD5($idx, $version_idx = 0)
2248 e1117268 Andreas
    {
2249 e1117268 Andreas
       $result = $this->cfg_db->db_query("
2250 6198d8ac Andreas
          SELECT
2251 6198d8ac Andreas
             img_md5
2252 6198d8ac Andreas
          FROM
2253 6198d8ac Andreas
             images
2254 6198d8ac Andreas
          WHERE
2255 6198d8ac Andreas
             img_idx='". $idx ."'
2256 6198d8ac Andreas
          AND
2257 6198d8ac Andreas
             img_version_idx='". $version_idx ."'
2258 e1117268 Andreas
       ");
2259 e1117268 Andreas
2260 e1117268 Andreas
       if(!$result)
2261 6198d8ac Andreas
          return NULL;
2262 e1117268 Andreas
2263 6198d8ac Andreas
       if($img = $this->cfg_db->db_fetch_object($result))
2264 6198d8ac Andreas
          return $img['img_md5'];
2265 6198d8ac Andreas
2266 6198d8ac Andreas
       return NULL;
2267 e1117268 Andreas
2268 e1117268 Andreas
    } // getMD5()
2269 e1117268 Andreas
2270 7f50c5e3 Andreas
    /**
2271 7f50c5e3 Andreas
     * set MD5 sum for the specific photo
2272 2765fc08 Andreas
     * @param integer $idx
2273 2765fc08 Andreas
     * @param string $md5
2274 7f50c5e3 Andreas
     */
2275 6198d8ac Andreas
    private function setMD5($idx, $md5, $version_idx = 0)
2276 e1117268 Andreas
    {
2277 e1117268 Andreas
       $result = $this->cfg_db->db_exec("
2278 6198d8ac Andreas
          INSERT OR REPLACE INTO images (
2279 6198d8ac Andreas
             img_idx, img_version_idx, img_md5
2280 6198d8ac Andreas
          ) VALUES (
2281 6198d8ac Andreas
             '". $idx ."',
2282 6198d8ac Andreas
             '". $version_idx ."',
2283 6198d8ac Andreas
             '". $md5 ."'
2284 6198d8ac Andreas
          )
2285 e1117268 Andreas
       ");
2286 e1117268 Andreas
2287 e1117268 Andreas
    } // setMD5()
2288 e1117268 Andreas
2289 7f50c5e3 Andreas
    /**
2290 7f50c5e3 Andreas
     * store current tag condition
2291 7f50c5e3 Andreas
     *
2292 7f50c5e3 Andreas
     * this function stores the current tag condition
2293 7f50c5e3 Andreas
     * (AND or OR) in the users session variables
2294 2765fc08 Andreas
     * @param string $mode
2295 2765fc08 Andreas
     * @return string
2296 7f50c5e3 Andreas
     */
2297 ad475b7b Andreas
    public function setTagCondition($mode)
2298 ad475b7b Andreas
    {
2299 ad475b7b Andreas
       $_SESSION['tag_condition'] = $mode;
2300 ad475b7b Andreas
2301 d9ee4599 Andreas
       return "ok";
2302 d9ee4599 Andreas
2303 ad475b7b Andreas
    } // setTagCondition()
2304 ad475b7b Andreas
2305 7f50c5e3 Andreas
    /**
2306 984b068b Andreas
     * invoke tag & date search
2307 7f50c5e3 Andreas
     *
2308 7f50c5e3 Andreas
     * this function will return all matching tags and store
2309 984b068b Andreas
     * them in the session variable selected_tags. furthermore
2310 984b068b Andreas
     * it also handles the date search.
2311 7f50c5e3 Andreas
     * getPhotoSelection() will then only return the matching
2312 7f50c5e3 Andreas
     * photos.
2313 2765fc08 Andreas
     * @return string
2314 7f50c5e3 Andreas
     */
2315 8f46e095 Andreas
    public function startSearch()
2316 c48fe9f8 Andreas
    {
2317 f36faab3 Andreas
       /* date search */
2318 8996f568 Andreas
       if(isset($_POST['date_from']) && $this->isValidDate($_POST['date_from'])) {
2319 8996f568 Andreas
          $date_from = $_POST['date_from'];
2320 213a12db Andreas
       }
2321 8996f568 Andreas
       if(isset($_POST['date_to']) && $this->isValidDate($_POST['date_to'])) {
2322 8996f568 Andreas
          $date_to = $_POST['date_to'];
2323 213a12db Andreas
       }
2324 213a12db Andreas
2325 f36faab3 Andreas
       /* tag-name search */
2326 f95833e6 Andreas
       if(isset($_POST['for_tag']) && is_string($_POST['for_tag'])) {
2327 f95833e6 Andreas
          $searchfor_tag = $_POST['for_tag'];
2328 8f46e095 Andreas
          $_SESSION['searchfor_tag'] = $_POST['for_tag'];
2329 213a12db Andreas
       }
2330 f36faab3 Andreas
       else {
2331 f36faab3 Andreas
          unset($_SESSION['searchfor_tag']);
2332 f36faab3 Andreas
       }
2333 213a12db Andreas
2334 f36faab3 Andreas
       /* file-name search */
2335 f95833e6 Andreas
       if(isset($_POST['for_name']) && is_string($_POST['for_name'])) {
2336 8f46e095 Andreas
          $_SESSION['searchfor_name'] = $_POST['for_name'];
2337 213a12db Andreas
       }
2338 f36faab3 Andreas
       else {
2339 f36faab3 Andreas
          unset($_SESSION['searchfor_name']);
2340 f36faab3 Andreas
       }
2341 f36faab3 Andreas
2342 f36faab3 Andreas
       /* rate-search */
2343 f36faab3 Andreas
       if(isset($_POST['rate_from']) && is_numeric($_POST['rate_from'])) {
2344 f36faab3 Andreas
2345 f36faab3 Andreas
          $_SESSION['rate_from'] = $_POST['rate_from'];
2346 f36faab3 Andreas
2347 f36faab3 Andreas
          if(isset($_POST['rate_to']) && is_numeric($_POST['rate_to'])) {
2348 f36faab3 Andreas
             $_SESSION['rate_to'] = $_POST['rate_to'];
2349 f36faab3 Andreas
          }
2350 f36faab3 Andreas
       }
2351 f36faab3 Andreas
       else {
2352 f36faab3 Andreas
          /* delete any previously set value */
2353 f36faab3 Andreas
          unset($_SESSION['rate_to'], $_SESSION['rate_from']);
2354 f36faab3 Andreas
       }
2355 213a12db Andreas
2356 b44a5c35 Andreas
       $this->get_tags();
2357 b44a5c35 Andreas
2358 8996f568 Andreas
       if(isset($date_from) && !empty($date_from))
2359 8996f568 Andreas
          $_SESSION['from_date'] = strtotime($date_from ." 00:00:00");
2360 8223eed3 Andreas
       else
2361 8223eed3 Andreas
          unset($_SESSION['from_date']);
2362 915550ab Andreas
2363 8996f568 Andreas
       if(isset($date_to) && !empty($date_to))
2364 8996f568 Andreas
          $_SESSION['to_date'] = strtotime($date_to ." 23:59:59");
2365 8223eed3 Andreas
       else
2366 8223eed3 Andreas
          unset($_SESSION['to_date']);
2367 984b068b Andreas
2368 8f46e095 Andreas
       if(isset($searchfor_tag) && !empty($searchfor_tag)) {
2369 984b068b Andreas
          /* new search, reset the current selected tags */
2370 984b068b Andreas
          $_SESSION['selected_tags'] = Array();
2371 984b068b Andreas
          foreach($this->avail_tags as $tag) {
2372 213a12db Andreas
             if(preg_match('/'. $searchfor_tag .'/i', $this->tags[$tag]))
2373 984b068b Andreas
                array_push($_SESSION['selected_tags'], $tag);
2374 984b068b Andreas
          }
2375 984b068b Andreas
       }
2376 8223eed3 Andreas
2377 f95833e6 Andreas
       return "ok";
2378 f95833e6 Andreas
2379 984b068b Andreas
    } // startSearch()
2380 4a0b6f61 Andreas
2381 7f50c5e3 Andreas
    /**
2382 5bc35081 Andreas
     * updates sort order in session variable
2383 5bc35081 Andreas
     *
2384 5bc35081 Andreas
     * this function is invoked by RPC and will sort the requested
2385 5bc35081 Andreas
     * sort order in the session variable.
2386 2765fc08 Andreas
     * @param string $sort_order
2387 2765fc08 Andreas
     * @return string
2388 5bc35081 Andreas
     */
2389 5bc35081 Andreas
    public function updateSortOrder($order)
2390 5bc35081 Andreas
    {
2391 5bc35081 Andreas
       if(isset($this->sort_orders[$order])) {
2392 5bc35081 Andreas
          $_SESSION['sort_order'] = $order;
2393 5bc35081 Andreas
          return "ok";
2394 5bc35081 Andreas
       }
2395 5bc35081 Andreas
2396 5bc35081 Andreas
       return "unkown error";
2397 5bc35081 Andreas
2398 5bc35081 Andreas
    } // updateSortOrder()
2399 5bc35081 Andreas
2400 5bc35081 Andreas
    /**
2401 6198d8ac Andreas
     * update photo version in session variable
2402 6198d8ac Andreas
     *
2403 6198d8ac Andreas
     * this function is invoked by RPC and will set the requested
2404 6198d8ac Andreas
     * photo version in the session variable.
2405 6198d8ac Andreas
     * @param string $photo_version
2406 6198d8ac Andreas
     * @return string
2407 6198d8ac Andreas
     */
2408 6198d8ac Andreas
    public function update_photo_version($photo_idx, $photo_version)
2409 6198d8ac Andreas
    {
2410 6198d8ac Andreas
       if($this->is_valid_version($photo_idx, $photo_version)) {
2411 6198d8ac Andreas
          $_SESSION['current_version'] = $photo_version;
2412 6198d8ac Andreas
          return "ok";
2413 6198d8ac Andreas
       }
2414 6198d8ac Andreas
2415 6198d8ac Andreas
       return "incorrect photo version provided";
2416 6198d8ac Andreas
2417 6198d8ac Andreas
    } // update_photo_version()
2418 6198d8ac Andreas
2419 6198d8ac Andreas
    /**
2420 7f50c5e3 Andreas
     * rotate image
2421 7f50c5e3 Andreas
     *
2422 7f50c5e3 Andreas
     * this function rotates the image according the
2423 7f50c5e3 Andreas
     * specified angel.
2424 2765fc08 Andreas
     * @param string $img
2425 2765fc08 Andreas
     * @param integer $degress
2426 2765fc08 Andreas
     * @return image
2427 7f50c5e3 Andreas
     */
2428 eaf7260e Andreas
    private function rotateImage($img, $degrees)
2429 eaf7260e Andreas
    {
2430 7f50c5e3 Andreas
       if(function_exists("imagerotate")) {
2431 eaf7260e Andreas
          $img = imagerotate($img, $degrees, 0);
2432 7f50c5e3 Andreas
       } else {
2433 eaf7260e Andreas
          function imagerotate($src_img, $angle)
2434 eaf7260e Andreas
          {
2435 eaf7260e Andreas
             $src_x = imagesx($src_img);
2436 eaf7260e Andreas
             $src_y = imagesy($src_img);
2437 eaf7260e Andreas
             if ($angle == 180) {
2438 eaf7260e Andreas
                $dest_x = $src_x;
2439 eaf7260e Andreas
                $dest_y = $src_y;
2440 eaf7260e Andreas
             }
2441 eaf7260e Andreas
             elseif ($src_x <= $src_y) {
2442 eaf7260e Andreas
                $dest_x = $src_y;
2443 eaf7260e Andreas
                $dest_y = $src_x;
2444 eaf7260e Andreas
             }
2445 eaf7260e Andreas
             elseif ($src_x >= $src_y) {
2446 eaf7260e Andreas
                $dest_x = $src_y;
2447 eaf7260e Andreas
                $dest_y = $src_x;
2448 eaf7260e Andreas
             }
2449 eaf7260e Andreas
2450 eaf7260e Andreas
             $rotate=imagecreatetruecolor($dest_x,$dest_y);
2451 eaf7260e Andreas
             imagealphablending($rotate, false);
2452 eaf7260e Andreas
2453 eaf7260e Andreas
             switch ($angle) {
2454 eaf7260e Andreas
2455 eaf7260e Andreas
                case 90:
2456 eaf7260e Andreas
                   for ($y = 0; $y < ($src_y); $y++) {
2457 eaf7260e Andreas
                      for ($x = 0; $x < ($src_x); $x++) {
2458 eaf7260e Andreas
                         $color = imagecolorat($src_img, $x, $y);
2459 eaf7260e Andreas
                         imagesetpixel($rotate, $dest_x - $y - 1, $x, $color);
2460 eaf7260e Andreas
                      }
2461 eaf7260e Andreas
                   }
2462 eaf7260e Andreas
                   break;
2463 eaf7260e Andreas
2464 eaf7260e Andreas
                case 270:
2465 eaf7260e Andreas
                   for ($y = 0; $y < ($src_y); $y++) {
2466 eaf7260e Andreas
                      for ($x = 0; $x < ($src_x); $x++) {
2467 eaf7260e Andreas
                         $color = imagecolorat($src_img, $x, $y);
2468 eaf7260e Andreas
                         imagesetpixel($rotate, $y, $dest_y - $x - 1, $color);
2469 eaf7260e Andreas
                      }
2470 eaf7260e Andreas
                   }
2471 eaf7260e Andreas
                   break;
2472 eaf7260e Andreas
2473 eaf7260e Andreas
                case 180:
2474 eaf7260e Andreas
                   for ($y = 0; $y < ($src_y); $y++) {
2475 eaf7260e Andreas
                      for ($x = 0; $x < ($src_x); $x++) {
2476 eaf7260e Andreas
                         $color = imagecolorat($src_img, $x, $y);
2477 eaf7260e Andreas
                         imagesetpixel($rotate, $dest_x - $x - 1, $dest_y - $y - 1, $color);
2478 eaf7260e Andreas
                      }
2479 eaf7260e Andreas
                   }
2480 eaf7260e Andreas
                   break;
2481 eaf7260e Andreas
2482 7f50c5e3 Andreas
                default:
2483 7f50c5e3 Andreas
                   $rotate = $src_img;
2484 7f50c5e3 Andreas
                   break;
2485 eaf7260e Andreas
             };
2486 eaf7260e Andreas
2487 eaf7260e Andreas
             return $rotate;
2488 eaf7260e Andreas
2489 eaf7260e Andreas
          }
2490 eaf7260e Andreas
2491 eaf7260e Andreas
          $img = imagerotate($img, $degrees);
2492 eaf7260e Andreas
2493 eaf7260e Andreas
       }
2494 eaf7260e Andreas
2495 eaf7260e Andreas
       return $img;
2496 eaf7260e Andreas
2497 eaf7260e Andreas
    } // rotateImage()
2498 eaf7260e Andreas
2499 7f50c5e3 Andreas
    /**
2500 c9a7f083 Andreas
     * returns flipped image
2501 c9a7f083 Andreas
     *
2502 c9a7f083 Andreas
     * this function will return an either horizontal or
2503 c9a7f083 Andreas
     * vertical flipped truecolor image.
2504 2765fc08 Andreas
     * @param string $image
2505 2765fc08 Andreas
     * @param string $mode
2506 2765fc08 Andreas
     * @return image
2507 c9a7f083 Andreas
     */
2508 c9a7f083 Andreas
    private function flipImage($image, $mode)
2509 c9a7f083 Andreas
    {
2510 c9a7f083 Andreas
       $w = imagesx($image);
2511 c9a7f083 Andreas
       $h = imagesy($image);
2512 c9a7f083 Andreas
       $flipped = imagecreatetruecolor($w, $h);
2513 c9a7f083 Andreas
2514 c9a7f083 Andreas
       switch($mode) {
2515 c9a7f083 Andreas
          case 'vert':
2516 c9a7f083 Andreas
             for ($y = 0; $y < $h; $y++) {
2517 c9a7f083 Andreas
                imagecopy($flipped, $image, 0, $y, 0, $h - $y - 1, $w, 1);
2518 c9a7f083 Andreas
             }
2519 c9a7f083 Andreas
             break;
2520 c9a7f083 Andreas
          case 'hori':
2521 c9a7f083 Andreas
             for ($x = 0; $x < $w; $x++) {
2522 c9a7f083 Andreas
                imagecopy($flipped, $image, $x, 0, $w - $x - 1, 0, 1, $h);
2523 c9a7f083 Andreas
             }
2524 c9a7f083 Andreas
             break;
2525 c9a7f083 Andreas
       }
2526 c9a7f083 Andreas
2527 c9a7f083 Andreas
       return $flipped;
2528 c9a7f083 Andreas
2529 c9a7f083 Andreas
    } // flipImage()
2530 c9a7f083 Andreas
2531 c9a7f083 Andreas
    /**
2532 7f50c5e3 Andreas
     * return all assigned tags for the specified photo
2533 2765fc08 Andreas
     * @param integer $idx
2534 2765fc08 Andreas
     * @return array
2535 7f50c5e3 Andreas
     */
2536 64aff8ea Andreas
    private function get_photo_tags($idx)
2537 64aff8ea Andreas
    {
2538 64aff8ea Andreas
       $result = $this->db->db_query("
2539 6216473e Andreas
          SELECT t.id as id, t.name as name
2540 64aff8ea Andreas
          FROM tags t
2541 64aff8ea Andreas
          INNER JOIN photo_tags pt
2542 64aff8ea Andreas
             ON t.id=pt.tag_id
2543 64aff8ea Andreas
          WHERE pt.photo_id='". $idx ."'
2544 64aff8ea Andreas
       ");
2545 64aff8ea Andreas
2546 64aff8ea Andreas
       $tags = Array();
2547 64aff8ea Andreas
2548 3f6b85d5 Andreas
       while($row = $this->db->db_fetch_object($result)) {
2549 3f6b85d5 Andreas
          if(isset($this->cfg->hide_tags) && in_array($row['name'], $this->cfg->hide_tags))
2550 3f6b85d5 Andreas
             continue;
2551 64aff8ea Andreas
          $tags[$row['id']] = $row['name'];
2552 3f6b85d5 Andreas
       }
2553 64aff8ea Andreas
2554 64aff8ea Andreas
       return $tags;
2555 64aff8ea Andreas
2556 64aff8ea Andreas
    } // get_photo_tags()
2557 64aff8ea Andreas
2558 7f50c5e3 Andreas
    /**
2559 7f50c5e3 Andreas
     * create on-the-fly images with text within
2560 2765fc08 Andreas
     * @param string $txt
2561 2765fc08 Andreas
     * @param string $color
2562 2765fc08 Andreas
     * @param integer $space
2563 2765fc08 Andreas
     * @param integer $font
2564 2765fc08 Andreas
     * @param integer $w
2565 7f50c5e3 Andreas
     */
2566 605439cc Andreas
    public function showTextImage($txt, $color=000000, $space=4, $font=4, $w=300)
2567 cb4b2907 Andreas
    {
2568 cb4b2907 Andreas
       if (strlen($color) != 6)
2569 cb4b2907 Andreas
          $color = 000000;
2570 cb4b2907 Andreas
2571 cb4b2907 Andreas
       $int = hexdec($color);
2572 cb4b2907 Andreas
       $h = imagefontheight($font);
2573 cb4b2907 Andreas
       $fw = imagefontwidth($font);
2574 cb4b2907 Andreas
       $txt = explode("\n", wordwrap($txt, ($w / $fw), "\n"));
2575 cb4b2907 Andreas
       $lines = count($txt);
2576 cb4b2907 Andreas
       $im = imagecreate($w, (($h * $lines) + ($lines * $space)));
2577 cb4b2907 Andreas
       $bg = imagecolorallocate($im, 255, 255, 255);
2578 cb4b2907 Andreas
       $color = imagecolorallocate($im, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
2579 cb4b2907 Andreas
       $y = 0;
2580 cb4b2907 Andreas
2581 cb4b2907 Andreas
       foreach ($txt as $text) {
2582 cb4b2907 Andreas
          $x = (($w - ($fw * strlen($text))) / 2);
2583 cb4b2907 Andreas
          imagestring($im, $font, $x, $y, $text, $color);
2584 cb4b2907 Andreas
          $y += ($h + $space);
2585 cb4b2907 Andreas
       }
2586 cb4b2907 Andreas
2587 cb4b2907 Andreas
       Header("Content-type: image/png");
2588 cb4b2907 Andreas
       ImagePng($im);
2589 cb4b2907 Andreas
2590 cb4b2907 Andreas
    } // showTextImage()
2591 cb4b2907 Andreas
2592 7f50c5e3 Andreas
    /**
2593 7f50c5e3 Andreas
     * check if all requirements are met
2594 2765fc08 Andreas
     * @return boolean
2595 7f50c5e3 Andreas
     */
2596 ef48f193 Andreas
    private function check_requirements()
2597 605439cc Andreas
    {
2598 605439cc Andreas
       if(!function_exists("imagecreatefromjpeg")) {
2599 605439cc Andreas
          print "PHP GD library extension is missing<br />\n";
2600 605439cc Andreas
          $missing = true;
2601 605439cc Andreas
       }
2602 605439cc Andreas
2603 36082151 Andreas
       if($this->cfg->db_access == "native" && !function_exists("sqlite3_open")) {
2604 605439cc Andreas
          print "PHP SQLite3 library extension is missing<br />\n";
2605 605439cc Andreas
          $missing = true;
2606 605439cc Andreas
       }
2607 605439cc Andreas
2608 1a7ed49f Andreas
       if($this->cfg->db_access == "pdo") {
2609 1a7ed49f Andreas
          if(array_search("sqlite", PDO::getAvailableDrivers()) === false) {
2610 1a7ed49f Andreas
             print "PDO SQLite3 driver is missing<br />\n";
2611 1a7ed49f Andreas
             $missing = true;
2612 1a7ed49f Andreas
          }
2613 1a7ed49f Andreas
       }
2614 1a7ed49f Andreas
2615 605439cc Andreas
       /* Check for HTML_AJAX PEAR package, lent from Horde project */
2616 605439cc Andreas
       ini_set('track_errors', 1);
2617 6c6ffd19 Andreas
       @include_once 'HTML/AJAX/Server.php';
2618 873c2c27 Andreas
       if(isset($php_errormsg) && preg_match('/Failed opening.*for inclusion/i', $php_errormsg)) {
2619 605439cc Andreas
          print "PEAR HTML_AJAX package is missing<br />\n";
2620 605439cc Andreas
          $missing = true;
2621 8a6ae2a6 Andreas
          unset($php_errormsg);
2622 605439cc Andreas
       }
2623 873c2c27 Andreas
       @include_once 'Calendar/Calendar.php';
2624 873c2c27 Andreas
       if(isset($php_errormsg) && preg_match('/Failed opening.*for inclusion/i', $php_errormsg)) {
2625 873c2c27 Andreas
          print "PEAR Calendar package is missing<br />\n";
2626 873c2c27 Andreas
          $missing = true;
2627 8a6ae2a6 Andreas
          unset($php_errormsg);
2628 873c2c27 Andreas
       }
2629 aed571db Andreas
       @include_once 'Console/Getopt.php';
2630 aed571db Andreas
       if(isset($php_errormsg) && preg_match('/Failed opening.*for inclusion/i', $php_errormsg)) {
2631 aed571db Andreas
          print "PEAR Console_Getopt package is missing<br />\n";
2632 aed571db Andreas
          $missing = true;
2633 8a6ae2a6 Andreas
          unset($php_errormsg);
2634 aed571db Andreas
       }
2635 5bc4f098 Andreas
       @include_once 'Date.php';
2636 5bc4f098 Andreas
       if(isset($php_errormsg) && preg_match('/Failed opening.*for inclusion/i', $php_errormsg)) {
2637 5bc4f098 Andreas
          print "PEAR Date package is missing<br />\n";
2638 5bc4f098 Andreas
          $missing = true;
2639 8a6ae2a6 Andreas
          unset($php_errormsg);
2640 5bc4f098 Andreas
       }
2641 ef48f193 Andreas
       @include_once $this->cfg->smarty_path .'/libs/Smarty.class.php';
2642 ef48f193 Andreas
       if(isset($php_errormsg) && preg_match('/Failed opening.*for inclusion/i', $php_errormsg)) {
2643 ef48f193 Andreas
          print "Smarty template engine can not be found in ". $this->cfg->smarty_path ."/libs/Smarty.class.php<br />\n";
2644 ef48f193 Andreas
          $missing = true;
2645 8a6ae2a6 Andreas
          unset($php_errormsg);
2646 ef48f193 Andreas
       }
2647 605439cc Andreas
       ini_restore('track_errors');
2648 605439cc Andreas
2649 605439cc Andreas
       if(isset($missing))
2650 605439cc Andreas
          return false;
2651 605439cc Andreas
2652 605439cc Andreas
       return true;
2653 605439cc Andreas
2654 ef48f193 Andreas
    } // check_requirements()
2655 6a21b6f0 Andreas
2656 6a21b6f0 Andreas
    private function _debug($text)
2657 6a21b6f0 Andreas
    {
2658 9609cbda Andreas
       if(isset($this->fromcmd)) {
2659 6a21b6f0 Andreas
          print $text;
2660 6a21b6f0 Andreas
       }
2661 6a21b6f0 Andreas
2662 6a21b6f0 Andreas
    } // _debug()
2663 605439cc Andreas
2664 7f50c5e3 Andreas
    /**
2665 7f50c5e3 Andreas
     * check if specified MIME type is supported
2666 2765fc08 Andreas
     * @param string $mime
2667 2765fc08 Andreas
     * @return boolean
2668 7f50c5e3 Andreas
     */
2669 eda7f279 Andreas
    public function checkifImageSupported($mime)
2670 eda7f279 Andreas
    {
2671 e1730519 Andreas
       $supported_types =  Array(
2672 e1730519 Andreas
          "image/jpeg",
2673 e1730519 Andreas
          "image/png",
2674 e1730519 Andreas
          "image/x-portable-pixmap",
2675 e1730519 Andreas
          "image/tiff"
2676 e1730519 Andreas
       );
2677 e1730519 Andreas
2678 e1730519 Andreas
       if(in_array($mime, $supported_types))
2679 eda7f279 Andreas
          return true;
2680 eda7f279 Andreas
2681 eda7f279 Andreas
       return false;
2682 eda7f279 Andreas
2683 eda7f279 Andreas
    } // checkifImageSupported()
2684 eda7f279 Andreas
2685 2765fc08 Andreas
    /**
2686 2765fc08 Andreas
     * output error text
2687 2765fc08 Andreas
     * @param string $text
2688 2765fc08 Andreas
     */
2689 a276fb26 Andreas
    public function _error($text)
2690 27bcdc62 Andreas
    {
2691 50786675 Andreas
       switch($this->cfg->logging) {
2692 42d16009 Andreas
          default:
2693 50786675 Andreas
          case 'display':
2694 aba6c296 Andreas
             if(isset($this->fromcmd))
2695 aba6c296 Andreas
                print $text ."\n";
2696 aba6c296 Andreas
             else {
2697 aba6c296 Andreas
                print "<img src=\"resources/green_info.png\" alt=\"warning\" />\n";
2698 aba6c296 Andreas
                print $text ."<br />\n";
2699 aba6c296 Andreas
             }
2700 50786675 Andreas
             break;
2701 50786675 Andreas
          case 'errorlog':
2702 50786675 Andreas
             error_log($text);
2703 50786675 Andreas
             break;
2704 50786675 Andreas
          case 'logfile':
2705 7a065517 Andreas
             error_log($text, 3, $this->cfg->log_file);
2706 50786675 Andreas
             break;
2707 50786675 Andreas
       }
2708 27bcdc62 Andreas
2709 42d16009 Andreas
       $this->runtime_error = true;
2710 42d16009 Andreas
2711 a276fb26 Andreas
    } // _error()
2712 27bcdc62 Andreas
2713 7f50c5e3 Andreas
    /**
2714 8996f568 Andreas
     * get calendar input-text fields
2715 8996f568 Andreas
     *
2716 8996f568 Andreas
     * this function returns a text-field used for the data selection.
2717 8996f568 Andreas
     * Either it will be filled with the current date or, if available,
2718 8996f568 Andreas
     * filled with the date user entered previously.
2719 8996f568 Andreas
     *
2720 2765fc08 Andreas
     * @param string $mode
2721 2765fc08 Andreas
     * @return string
2722 7f50c5e3 Andreas
     */
2723 8996f568 Andreas
    private function get_date_text_field($mode)
2724 873c2c27 Andreas
    {
2725 8996f568 Andreas
       $date = isset($_SESSION[$mode .'_date']) ? date("Y", $_SESSION[$mode .'_date']) : date("Y");
2726 8996f568 Andreas
       $date.= "-";
2727 8996f568 Andreas
       $date.= isset($_SESSION[$mode .'_date']) ? date("m", $_SESSION[$mode .'_date']) : date("m");
2728 8996f568 Andreas
       $date.= "-";
2729 8996f568 Andreas
       $date.= isset($_SESSION[$mode .'_date']) ? date("d", $_SESSION[$mode .'_date']) : date("d");
2730 3d567f23 Andreas
2731 8996f568 Andreas
       $output = "<input type=\"text\" size=\"15\" id=\"date_". $mode ."\" value=\"". $date ."\"";
2732 cd94f8e4 Andreas
       if(!isset($_SESSION[$mode .'_date']))
2733 cd94f8e4 Andreas
          $output.= " disabled=\"disabled\"";
2734 984b068b Andreas
       $output.= " />\n";
2735 cd94f8e4 Andreas
2736 4a0b6f61 Andreas
       return $output;
2737 873c2c27 Andreas
2738 8996f568 Andreas
    } // get_date_text_field()
2739 873c2c27 Andreas
2740 7f50c5e3 Andreas
    /**
2741 7f50c5e3 Andreas
     * output calendar matrix
2742 2765fc08 Andreas
     * @param integer $year
2743 2765fc08 Andreas
     * @param integer $month
2744 2765fc08 Andreas
     * @param integer $day
2745 7f50c5e3 Andreas
     */
2746 5bc4f098 Andreas
    public function get_calendar_matrix($userdate)
2747 4a0b6f61 Andreas
    {
2748 5bc4f098 Andreas
       if(($userdate = strtotime($userdate)) === false) {
2749 5bc4f098 Andreas
          $year = date('Y');
2750 5bc4f098 Andreas
          $month = date('m');
2751 5bc4f098 Andreas
          $day = date('d');
2752 5bc4f098 Andreas
       }
2753 5bc4f098 Andreas
       else {
2754 5bc4f098 Andreas
          $date = new Date();
2755 5bc4f098 Andreas
          $date->setDate($userdate);
2756 5bc4f098 Andreas
2757 5bc4f098 Andreas
          $year  = $date->getYear();
2758 5bc4f098 Andreas
          $month = $date->getMonth();
2759 5bc4f098 Andreas
          $day   = $date->getDay();
2760 5bc4f098 Andreas
       }
2761 5bc4f098 Andreas
2762 4a0b6f61 Andreas
       $rows = 1;
2763 4a0b6f61 Andreas
       $cols = 1;
2764 4a0b6f61 Andreas
       $matrix = Array();
2765 4a0b6f61 Andreas
2766 4a0b6f61 Andreas
       require_once CALENDAR_ROOT.'Month/Weekdays.php';
2767 4a0b6f61 Andreas
       require_once CALENDAR_ROOT.'Day.php';
2768 873c2c27 Andreas
2769 4a0b6f61 Andreas
       // Build the month
2770 5bc4f098 Andreas
       $month_cal = new Calendar_Month_Weekdays($year,$month);
2771 4a0b6f61 Andreas
2772 4a0b6f61 Andreas
       // Create links
2773 5bc4f098 Andreas
       $prevStamp = $month_cal->prevMonth(true);
2774 4a0b6f61 Andreas
       $prev = "javascript:setMonth(". date('Y',$prevStamp) .", ". date('n',$prevStamp) .", ". date('j',$prevStamp) .");";
2775 5bc4f098 Andreas
       $nextStamp = $month_cal->nextMonth(true);
2776 4a0b6f61 Andreas
       $next = "javascript:setMonth(". date('Y',$nextStamp) .", ". date('n',$nextStamp) .", ". date('j',$nextStamp) .");";
2777 4a0b6f61 Andreas
2778 4a0b6f61 Andreas
       $selectedDays = array (
2779 4a0b6f61 Andreas
          new Calendar_Day($year,$month,$day),
2780 4a0b6f61 Andreas
       );
2781 4a0b6f61 Andreas
2782 4a0b6f61 Andreas
       // Build the days in the month
2783 5bc4f098 Andreas
       $month_cal->build($selectedDays);
2784 4a0b6f61 Andreas
2785 5bc4f098 Andreas
       $this->tmpl->assign('current_month', date('F Y',$month_cal->getTimeStamp()));
2786 4a0b6f61 Andreas
       $this->tmpl->assign('prev_month', $prev);
2787 4a0b6f61 Andreas
       $this->tmpl->assign('next_month', $next);
2788 4a0b6f61 Andreas
2789 5bc4f098 Andreas
       while ( $day = $month_cal->fetch() ) {
2790 4a0b6f61 Andreas
2791 4a0b6f61 Andreas
          if(!isset($matrix[$rows]))
2792 4a0b6f61 Andreas
             $matrix[$rows] = Array();
2793 4a0b6f61 Andreas
2794 4a0b6f61 Andreas
          $string = "";
2795 4a0b6f61 Andreas
2796 4a0b6f61 Andreas
          $dayStamp = $day->thisDay(true);
2797 5bc4f098 Andreas
          $link = "javascript:setCalendarDate('"
2798 5bc4f098 Andreas
             . date('Y',$dayStamp)
2799 5bc4f098 Andreas
             . "-"
2800 5bc4f098 Andreas
             . date('m',$dayStamp)
2801 5bc4f098 Andreas
             . "-"
2802 5bc4f098 Andreas
             . date('d',$dayStamp)
2803 5bc4f098 Andreas
             ."');";
2804 4a0b6f61 Andreas
2805 4a0b6f61 Andreas
          // isFirst() to find start of week
2806 4a0b6f61 Andreas
          if ( $day->isFirst() )
2807 4a0b6f61 Andreas
             $string.= "<tr>\n";
2808 4a0b6f61 Andreas
2809 4a0b6f61 Andreas
          if ( $day->isSelected() ) {
2810 4a0b6f61 Andreas
             $string.= "<td class=\"selected\">".$day->thisDay()."</td>\n";
2811 4a0b6f61 Andreas
          } else if ( $day->isEmpty() ) {
2812 4a0b6f61 Andreas
             $string.= "<td>&nbsp;</td>\n";
2813 4a0b6f61 Andreas
          } else {
2814 4a0b6f61 Andreas
             $string.= "<td><a class=\"calendar\" href=\"".$link."\">".$day->thisDay()."</a></td>\n";
2815 4a0b6f61 Andreas
          }
2816 4a0b6f61 Andreas
2817 4a0b6f61 Andreas
          // isLast() to find end of week
2818 4a0b6f61 Andreas
          if ( $day->isLast() )
2819 4a0b6f61 Andreas
             $string.= "</tr>\n";
2820 4a0b6f61 Andreas
2821 4a0b6f61 Andreas
          $matrix[$rows][$cols] = $string;
2822 4a0b6f61 Andreas
2823 4a0b6f61 Andreas
          $cols++;
2824 4a0b6f61 Andreas
2825 4a0b6f61 Andreas
          if($cols > 7) {
2826 4a0b6f61 Andreas
             $cols = 1;
2827 4a0b6f61 Andreas
             $rows++;
2828 4a0b6f61 Andreas
          }
2829 873c2c27 Andreas
       }
2830 873c2c27 Andreas
2831 4a0b6f61 Andreas
       $this->tmpl->assign('matrix', $matrix);
2832 4a0b6f61 Andreas
       $this->tmpl->assign('rows', $rows);
2833 4a0b6f61 Andreas
       $this->tmpl->show("calendar.tpl");
2834 873c2c27 Andreas
2835 4a0b6f61 Andreas
    } // get_calendar_matrix()
2836 873c2c27 Andreas
2837 7f50c5e3 Andreas
    /**
2838 7f50c5e3 Andreas
     * output export page
2839 2765fc08 Andreas
     * @param string $mode
2840 7f50c5e3 Andreas
     */
2841 e0ff14d4 Andreas
    public function getExport($mode)
2842 e0ff14d4 Andreas
    {
2843 e0ff14d4 Andreas
       $pictures = $this->getPhotoSelection();
2844 99b313d1 Andreas
       $current_tags = $this->getCurrentTags();
2845 e0ff14d4 Andreas
2846 e0ff14d4 Andreas
       foreach($pictures as $picture) {
2847 e0ff14d4 Andreas
2848 867cf65e Andreas
          $orig_url = $this->get_phpfspot_url() ."/index.php?mode=showp&id=". $picture;
2849 99b313d1 Andreas
          if($current_tags != "") {
2850 99b313d1 Andreas
             $orig_url.= "&tags=". $current_tags;
2851 99b313d1 Andreas
          }
2852 ab99bf1b Andreas
          if(isset($_SESSION['from_date']) && isset($_SESSION['to_date'])) {
2853 ab99bf1b Andreas
             $orig_url.= "&from_date=". $_SESSION['from_date'] ."&to_date=". $_SESSION['to_date'];
2854 ab99bf1b Andreas
          }
2855 99b313d1 Andreas
2856 867cf65e Andreas
          if($this->is_user_friendly_url()) {
2857 867cf65e Andreas
             $thumb_url = $this->get_phpfspot_url() ."/photo/". $picture ."/". $this->cfg->thumb_width;
2858 867cf65e Andreas
          }
2859 867cf65e Andreas
          else {
2860 867cf65e Andreas
             $thumb_url = $this->get_phpfspot_url() ."/phpfspot_img.php?idx=". $picture ."&width=". $this->cfg->thumb_width;
2861 867cf65e Andreas
          }
2862 e0ff14d4 Andreas
2863 e0ff14d4 Andreas
          switch($mode) {
2864 e0ff14d4 Andreas
2865 e0ff14d4 Andreas
             case 'HTML':
2866 e0ff14d4 Andreas
                // <a href="%pictureurl%"><img src="%thumbnailurl%" ></a>
2867 e0ff14d4 Andreas
                print htmlspecialchars("<a href=\"". $orig_url ."\"><img src=\"". $thumb_url ."\" /></a>") ."<br />\n";
2868 e0ff14d4 Andreas
                break;
2869 e0ff14d4 Andreas
2870 e0ff14d4 Andreas
             case 'MoinMoin':
2871 af63ef61 Andreas
                // "[%pictureurl% %thumbnailurl%]"
2872 af63ef61 Andreas
                print htmlspecialchars("[".$orig_url." ".$thumb_url."&fake=1.jpg]") ."<br />\n";
2873 af63ef61 Andreas
                break;
2874 af63ef61 Andreas
2875 af63ef61 Andreas
             case 'MoinMoinList':
2876 af63ef61 Andreas
                // " * [%pictureurl% %thumbnailurl%]"
2877 af63ef61 Andreas
                print "&nbsp;" . htmlspecialchars("* [".$orig_url." ".$thumb_url."&fake=1.jpg]") ."<br />\n";
2878 e0ff14d4 Andreas
                break;
2879 e0ff14d4 Andreas
          }
2880 e0ff14d4 Andreas
2881 e0ff14d4 Andreas
       }
2882 e0ff14d4 Andreas
2883 e0ff14d4 Andreas
    } // getExport()
2884 e0ff14d4 Andreas
2885 7f50c5e3 Andreas
    /**
2886 7ff7dfb9 Andreas
     * output RSS feed
2887 7ff7dfb9 Andreas
     */
2888 7ff7dfb9 Andreas
    public function getRSSFeed()
2889 7ff7dfb9 Andreas
    {
2890 7ff7dfb9 Andreas
       Header("Content-type: text/xml; charset=utf-8");
2891 7ff7dfb9 Andreas
       print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
2892 7ff7dfb9 Andreas
 ?>
2893 7ff7dfb9 Andreas
 <rss version="2.0"
2894 7ff7dfb9 Andreas
    xmlns:media="http://search.yahoo.com/mrss/"
2895 7ff7dfb9 Andreas
    xmlns:dc="http://purl.org/dc/elements/1.1/"
2896 7ff7dfb9 Andreas
  >
2897 7ff7dfb9 Andreas
  <channel>
2898 7ff7dfb9 Andreas
   <title>phpfspot</title>
2899 7ff7dfb9 Andreas
   <description>phpfspot RSS feed</description>
2900 7ff7dfb9 Andreas
   <link><?php print htmlspecialchars($this->get_phpfspot_url()); ?></link>
2901 7ff7dfb9 Andreas
   <pubDate><?php print strftime("%a, %d %b %Y %T %z"); ?></pubDate>
2902 7ff7dfb9 Andreas
   <generator>phpfspot</generator>
2903 7ff7dfb9 Andreas
 <?php
2904 7ff7dfb9 Andreas
2905 7ff7dfb9 Andreas
       $pictures = $this->getPhotoSelection();
2906 7ff7dfb9 Andreas
       $current_tags = $this->getCurrentTags();
2907 7ff7dfb9 Andreas
2908 7ff7dfb9 Andreas
       foreach($pictures as $picture) {
2909 7ff7dfb9 Andreas
2910 867cf65e Andreas
          $orig_url = $this->get_phpfspot_url() ."/index.php?mode=showp&id=". $picture;
2911 7ff7dfb9 Andreas
          if($current_tags != "") {
2912 7ff7dfb9 Andreas
             $orig_url.= "&tags=". $current_tags;
2913 7ff7dfb9 Andreas
          }
2914 7ff7dfb9 Andreas
          if(isset($_SESSION['from_date']) && isset($_SESSION['to_date'])) {
2915 7ff7dfb9 Andreas
             $orig_url.= "&from_date=". $_SESSION['from_date'] ."&to_date=". $_SESSION['to_date'];
2916 7ff7dfb9 Andreas
          }
2917 7ff7dfb9 Andreas
2918 7ff7dfb9 Andreas
          $details = $this->get_photo_details($picture);
2919 7ff7dfb9 Andreas
2920 867cf65e Andreas
          if($this->is_user_friendly_url()) {
2921 867cf65e Andreas
             $thumb_url = $this->get_phpfspot_url() ."/photo/". $picture ."/". $this->cfg->thumb_width;
2922 867cf65e Andreas
          }
2923 867cf65e Andreas
          else {
2924 867cf65e Andreas
             $thumb_url = $this->get_phpfspot_url() ."/phpfspot_img.php?idx=". $picture ."&width=". $this->cfg->thumb_width;
2925 867cf65e Andreas
          }
2926 867cf65e Andreas
2927 7ff7dfb9 Andreas
          $thumb_html = htmlspecialchars("
2928 7ff7dfb9 Andreas
 <a href=\"". $orig_url ."\"><img src=\"". $thumb_url ."\" /></a>
2929 7ff7dfb9 Andreas
 <br>
2930 7ff7dfb9 Andreas
 ". $details['description']);
2931 7ff7dfb9 Andreas
2932 18ff7e04 Andreas
          $orig_path = $this->translate_path($this->parse_uri($details['uri'], 'fullpath'));
2933 8ab8f2cd Andreas
2934 8ab8f2cd Andreas
          /* get EXIF information if JPEG */
2935 9609cbda Andreas
          if(isset($details['mime']) && $details['mime'] == "image/jpeg") {
2936 8ab8f2cd Andreas
             $meta = $this->get_meta_informations($orig_path);
2937 8ab8f2cd Andreas
          }
2938 8ab8f2cd Andreas
2939 7ff7dfb9 Andreas
 ?>
2940 7ff7dfb9 Andreas
   <item>
2941 18ff7e04 Andreas
    <title><?php print htmlspecialchars($this->parse_uri($details['uri'], 'filename')); ?></title>
2942 7ff7dfb9 Andreas
    <link><?php print htmlspecialchars($orig_url); ?></link>
2943 7ff7dfb9 Andreas
    <guid><?php print htmlspecialchars($orig_url); ?></guid>
2944 c338e2fd Andreas
    <dc:date.Taken><?php print strftime("%Y-%m-%dT%H:%M:%S+00:00", $details['time']); ?></dc:date.Taken>
2945 7ff7dfb9 Andreas
    <description>
2946 7ff7dfb9 Andreas
     <?php print $thumb_html; ?>
2947 7ff7dfb9 Andreas
    </description>
2948 c338e2fd Andreas
    <pubDate><?php print strftime("%a, %d %b %Y %T %z", $details['time']); ?></pubDate>
2949 7ff7dfb9 Andreas
   </item>
2950 7ff7dfb9 Andreas
 <?php
2951 7ff7dfb9 Andreas
2952 7ff7dfb9 Andreas
       }
2953 7ff7dfb9 Andreas
 ?>
2954 7ff7dfb9 Andreas
  </channel>
2955 7ff7dfb9 Andreas
 </rss>
2956 7ff7dfb9 Andreas
 <?php
2957 7ff7dfb9 Andreas
2958 7ff7dfb9 Andreas
2959 7ff7dfb9 Andreas
    } // getExport()
2960 7ff7dfb9 Andreas
2961 7ff7dfb9 Andreas
2962 7ff7dfb9 Andreas
    /**
2963 546c7957 Andreas
     * get all selected tags
2964 546c7957 Andreas
     *
2965 546c7957 Andreas
     * This function will return all selected tags as one string, seperated
2966 546c7957 Andreas
     * by a comma.
2967 2765fc08 Andreas
     * @return array
2968 7f50c5e3 Andreas
     */
2969 15a6c1b6 Andreas
    private function getCurrentTags()
2970 15a6c1b6 Andreas
    {
2971 15a6c1b6 Andreas
       $current_tags = "";
2972 0b99d3d1 Andreas
       if(isset($_SESSION['selected_tags']) && $_SESSION['selected_tags'] != "") {
2973 15a6c1b6 Andreas
          foreach($_SESSION['selected_tags'] as $tag)
2974 15a6c1b6 Andreas
             $current_tags.= $tag .",";
2975 15a6c1b6 Andreas
          $current_tags = substr($current_tags, 0, strlen($current_tags)-1);
2976 15a6c1b6 Andreas
       }
2977 15a6c1b6 Andreas
       return $current_tags;
2978 15a6c1b6 Andreas
2979 15a6c1b6 Andreas
    } // getCurrentTags()
2980 15a6c1b6 Andreas
2981 7f50c5e3 Andreas
    /**
2982 7f50c5e3 Andreas
     * return the current photo
2983 7f50c5e3 Andreas
     */
2984 6198d8ac Andreas
    public function get_current_photo()
2985 15a6c1b6 Andreas
    {
2986 15a6c1b6 Andreas
       if(isset($_SESSION['current_photo'])) {
2987 6198d8ac Andreas
          return $_SESSION['current_photo'];
2988 15a6c1b6 Andreas
       }
2989 6198d8ac Andreas
2990 6198d8ac Andreas
       return NULL;
2991 6198d8ac Andreas
2992 6198d8ac Andreas
    } // get_current_photo()
2993 6198d8ac Andreas
2994 6198d8ac Andreas
    /**
2995 6198d8ac Andreas
     * current selected photo version
2996 6198d8ac Andreas
     *
2997 6198d8ac Andreas
     * this function returns the current selected photo version
2998 6198d8ac Andreas
     * from the session variables.
2999 6198d8ac Andreas
     *
3000 6198d8ac Andreas
     * @return int
3001 6198d8ac Andreas
     */
3002 6198d8ac Andreas
    public function get_current_version()
3003 6198d8ac Andreas
    {
3004 6198d8ac Andreas
       /* if current version is set, return it, if the photo really has that version */
3005 6198d8ac Andreas
       if(isset($_SESSION['current_version']) && is_numeric($_SESSION['current_version']))
3006 6198d8ac Andreas
          return $_SESSION['current_version'];
3007 6198d8ac Andreas
3008 6198d8ac Andreas
       return false;
3009 6198d8ac Andreas
3010 6198d8ac Andreas
    } // get_current_version()
3011 6198d8ac Andreas
3012 6198d8ac Andreas
    /**
3013 6198d8ac Andreas
     * returns latest available photo version
3014 6198d8ac Andreas
     *
3015 6198d8ac Andreas
     * this function returns the latested available version
3016 6198d8ac Andreas
     * for the requested photo.
3017 6198d8ac Andreas
     *
3018 6198d8ac Andreas
     * @return int
3019 6198d8ac Andreas
     */
3020 6198d8ac Andreas
    public function get_latest_version($photo_idx)
3021 6198d8ac Andreas
    {
3022 6198d8ac Andreas
       /* try to get the lasted version for the current photo */
3023 6198d8ac Andreas
       if($versions = $this->get_photo_versions($photo_idx))
3024 6198d8ac Andreas
          return $versions[count($versions)-1];
3025 6198d8ac Andreas
3026 6198d8ac Andreas
       /* if no alternative version were found, return original version */
3027 6198d8ac Andreas
       return 0;
3028 6198d8ac Andreas
3029 6198d8ac Andreas
    } // get_current_version()
3030 15a6c1b6 Andreas
3031 7f50c5e3 Andreas
    /**
3032 7f50c5e3 Andreas
     * tells the client browser what to do
3033 7f50c5e3 Andreas
     *
3034 7f50c5e3 Andreas
     * this function is getting called via AJAX by the
3035 7f50c5e3 Andreas
     * client browsers. it will tell them what they have
3036 7f50c5e3 Andreas
     * to do next. This is necessary for directly jumping
3037 7f50c5e3 Andreas
     * into photo index or single photo view when the are
3038 7f50c5e3 Andreas
     * requested with specific URLs
3039 2765fc08 Andreas
     * @return string
3040 7f50c5e3 Andreas
     */
3041 3d567f23 Andreas
    public function whatToDo()
3042 3d567f23 Andreas
    {
3043 845ed0ab Andreas
       if(isset($_SESSION['current_photo']) && $_SESSION['start_action'] == 'showp') {
3044 3d567f23 Andreas
       }
3045 e785e025 Andreas
       elseif(isset($_SESSION['selected_tags']) && !empty($_SESSION['selected_tags'])) {
3046 e785e025 Andreas
          return "showpi_tags";
3047 845ed0ab Andreas
       }
3048 3d567f23 Andreas
       elseif(isset($_SESSION['start_action']) && $_SESSION['start_action'] == 'showpi') {
3049 3d567f23 Andreas
          return "showpi";
3050 3d567f23 Andreas
       }
3051 3d567f23 Andreas
3052 3d567f23 Andreas
    } // whatToDo()
3053 3d567f23 Andreas
3054 7f50c5e3 Andreas
    /**
3055 7f50c5e3 Andreas
     * return the current process-user
3056 2765fc08 Andreas
     * @return string
3057 7f50c5e3 Andreas
     */
3058 a63f8995 Andreas
    private function getuid()
3059 a63f8995 Andreas
    {
3060 a63f8995 Andreas
       if($uid = posix_getuid()) {
3061 a63f8995 Andreas
          if($user = posix_getpwuid($uid)) {
3062 a63f8995 Andreas
             return $user['name'];
3063 a63f8995 Andreas
          }
3064 a63f8995 Andreas
       }
3065 a63f8995 Andreas
3066 a63f8995 Andreas
       return 'n/a';
3067 a63f8995 Andreas
3068 a63f8995 Andreas
    } // getuid()
3069 a63f8995 Andreas
3070 2f381082 Andreas
    /**
3071 6198d8ac Andreas
     * photo version select list
3072 6198d8ac Andreas
     *
3073 6198d8ac Andreas
     * this function returns a HTML select list (drop down)
3074 6198d8ac Andreas
     * to select a alternative photo version of the original photo.
3075 6198d8ac Andreas
     *
3076 6198d8ac Andreas
     * @param array $params
3077 6198d8ac Andreas
     * @param smarty $smarty
3078 6198d8ac Andreas
     * @return string
3079 6198d8ac Andreas
     */
3080 6198d8ac Andreas
    public function smarty_photo_version_select_list($params, &$smarty)
3081 6198d8ac Andreas
    {
3082 6198d8ac Andreas
       if(!isset($params['photo']) || !isset($params['current']))
3083 6198d8ac Andreas
          return NULL;
3084 6198d8ac Andreas
3085 6198d8ac Andreas
       $output = "<option value=\"0\">Original</option>";
3086 6198d8ac Andreas
       $versions = $this->get_photo_versions($params['photo']);
3087 6198d8ac Andreas
3088 6198d8ac Andreas
       foreach($versions as $version) {
3089 6198d8ac Andreas
3090 6198d8ac Andreas
          $output.= "<option value=\"". $version ."\"";
3091 6198d8ac Andreas
          if($version == $params['current']) {
3092 6198d8ac Andreas
             $output.= " selected=\"selected\"";
3093 6198d8ac Andreas
          }
3094 6198d8ac Andreas
          $output.= ">". $this->get_photo_version_name($params['photo'], $version) ."</option>";
3095 6198d8ac Andreas
       }
3096 6198d8ac Andreas
3097 6198d8ac Andreas
       return $output;
3098 6198d8ac Andreas
3099 6198d8ac Andreas
    } // smarty_photo_version_select_list()
3100 6198d8ac Andreas
3101 6198d8ac Andreas
    /**
3102 2f381082 Andreas
     * returns a select-dropdown box to select photo index sort parameters
3103 2765fc08 Andreas
     * @param array $params
3104 2765fc08 Andreas
     * @param smarty $smarty
3105 2765fc08 Andreas
     * @return string
3106 2f381082 Andreas
     */
3107 bb3bcfbb Andreas
    public function smarty_sort_select_list($params, &$smarty)
3108 2f381082 Andreas
    {
3109 5bc35081 Andreas
       $output = "";
3110 bb3bcfbb Andreas
3111 5bc35081 Andreas
       foreach($this->sort_orders as $key => $value) {
3112 bb3bcfbb Andreas
          $output.= "<option value=\"". $key ."\"";
3113 bb3bcfbb Andreas
          if($key == $_SESSION['sort_order']) {
3114 2f381082 Andreas
             $output.= " selected=\"selected\"";
3115 2f381082 Andreas
          }
3116 bb3bcfbb Andreas
          $output.= ">". $value ."</option>";
3117 2f381082 Andreas
       }
3118 bb3bcfbb Andreas
3119 2f381082 Andreas
       return $output;
3120 2f381082 Andreas
3121 bb3bcfbb Andreas
    } // smarty_sort_select_list()
3122 2f381082 Andreas
3123 2f381082 Andreas
    /**
3124 2f381082 Andreas
     * returns the currently selected sort order
3125 2765fc08 Andreas
     * @return string
3126 2f381082 Andreas
     */
3127 2f381082 Andreas
    private function get_sort_order()
3128 2f381082 Andreas
    {
3129 2f381082 Andreas
       switch($_SESSION['sort_order']) {
3130 2f381082 Andreas
          case 'date_asc':
3131 51ab51f6 Andreas
             return " ORDER BY p.time ASC";
3132 2f381082 Andreas
             break;
3133 2f381082 Andreas
          case 'date_desc':
3134 51ab51f6 Andreas
             return " ORDER BY p.time DESC";
3135 2f381082 Andreas
             break;
3136 2f381082 Andreas
          case 'name_asc':
3137 18ecb839 Andreas
             if($this->dbver < 9) {
3138 18ecb839 Andreas
                return " ORDER BY p.name ASC";
3139 18ecb839 Andreas
             }
3140 18ecb839 Andreas
             else {
3141 afadeef5 Andreas
                return " ORDER BY basename(p.uri) ASC";
3142 18ecb839 Andreas
             }
3143 2f381082 Andreas
             break;
3144 2f381082 Andreas
          case 'name_desc':
3145 18ecb839 Andreas
             if($this->dbver < 9) {
3146 18ecb839 Andreas
                return " ORDER BY p.name DESC";
3147 18ecb839 Andreas
             }
3148 18ecb839 Andreas
             else {
3149 afadeef5 Andreas
                return " ORDER BY basename(p.uri) DESC";
3150 18ecb839 Andreas
             }
3151 2f381082 Andreas
             break;
3152 811a18a5 Andreas
          case 'tags_asc':
3153 811a18a5 Andreas
             return " ORDER BY t.name ASC ,p.time ASC";
3154 811a18a5 Andreas
             break;
3155 811a18a5 Andreas
          case 'tags_desc':
3156 811a18a5 Andreas
             return " ORDER BY t.name DESC ,p.time ASC";
3157 811a18a5 Andreas
             break;
3158 aab7d4d0 Andreas
          case 'rate_asc':
3159 18efe997 Andreas
             return " ORDER BY p.rating ASC, t.name ASC";
3160 aab7d4d0 Andreas
             break;
3161 aab7d4d0 Andreas
          case 'rate_desc':
3162 5d95b196 Andreas
             return " ORDER BY p.rating DESC, t.name ASC";
3163 aab7d4d0 Andreas
             break;
3164 2f381082 Andreas
       }
3165 2f381082 Andreas
3166 2f381082 Andreas
    } // get_sort_order()
3167 005562e4 Andreas
3168 2765fc08 Andreas
    /**
3169 2765fc08 Andreas
     * return the next to be shown slide show image
3170 2765fc08 Andreas
     *
3171 2765fc08 Andreas
     * this function returns the URL of the next image
3172 2765fc08 Andreas
     * in the slideshow sequence.
3173 2765fc08 Andreas
     * @return string
3174 2765fc08 Andreas
     */
3175 005562e4 Andreas
    public function getNextSlideShowImage()
3176 005562e4 Andreas
    {
3177 005562e4 Andreas
       $all_photos = $this->getPhotoSelection();
3178 005562e4 Andreas
3179 005562e4 Andreas
       if(!isset($_SESSION['slideshow_img']) || $_SESSION['slideshow_img'] == count($all_photos)-1)
3180 005562e4 Andreas
          $_SESSION['slideshow_img'] = 0;
3181 005562e4 Andreas
       else
3182 005562e4 Andreas
          $_SESSION['slideshow_img']++;
3183 005562e4 Andreas
3184 867cf65e Andreas
       if($this->is_user_friendly_url()) {
3185 867cf65e Andreas
          return $this->get_phpfspot_url() ."/photo/". $all_photos[$_SESSION['slideshow_img']] ."/". $this->cfg->photo_width;
3186 867cf65e Andreas
       }
3187 867cf65e Andreas
3188 867cf65e Andreas
       return $this->get_phpfspot_url() ."/phpfspot_img.php?idx=". $all_photos[$_SESSION['slideshow_img']] ."&width=". $this->cfg->photo_width;
3189 005562e4 Andreas
3190 005562e4 Andreas
    } // getNextSlideShowImage()
3191 005562e4 Andreas
3192 2765fc08 Andreas
    /**
3193 2765fc08 Andreas
     * return the previous to be shown slide show image
3194 2765fc08 Andreas
     *
3195 2765fc08 Andreas
     * this function returns the URL of the previous image
3196 2765fc08 Andreas
     * in the slideshow sequence.
3197 2765fc08 Andreas
     * @return string
3198 2765fc08 Andreas
     */
3199 a36bd6f4 Andreas
    public function getPrevSlideShowImage()
3200 a36bd6f4 Andreas
    {
3201 a36bd6f4 Andreas
       $all_photos = $this->getPhotoSelection();
3202 a36bd6f4 Andreas
3203 a36bd6f4 Andreas
       if(!isset($_SESSION['slideshow_img']) || $_SESSION['slideshow_img'] == 0)
3204 a36bd6f4 Andreas
          $_SESSION['slideshow_img'] = 0;
3205 a36bd6f4 Andreas
       else
3206 a36bd6f4 Andreas
          $_SESSION['slideshow_img']--;
3207 a36bd6f4 Andreas
3208 867cf65e Andreas
       if($this->is_user_friendly_url()) {
3209 867cf65e Andreas
          return $this->get_phpfspot_url() ."/photo/". $all_photos[$_SESSION['slideshow_img']] ."/". $this->cfg->photo_width;
3210 867cf65e Andreas
       }
3211 867cf65e Andreas
3212 867cf65e Andreas
       return $this->get_phpfspot_url() ."/phpfspot_img.php?idx=". $all_photos[$_SESSION['slideshow_img']] ."&width=". $this->cfg->photo_width;
3213 a36bd6f4 Andreas
3214 a36bd6f4 Andreas
    } // getPrevSlideShowImage()
3215 a36bd6f4 Andreas
3216 005562e4 Andreas
    public function resetSlideShow()
3217 005562e4 Andreas
    {
3218 005562e4 Andreas
       if(isset($_SESSION['slideshow_img']))
3219 005562e4 Andreas
          unset($_SESSION['slideshow_img']);
3220 213a12db Andreas
3221 005562e4 Andreas
    } // resetSlideShow()
3222 758887b7 Andreas
3223 2765fc08 Andreas
    /**
3224 2765fc08 Andreas
     * get random photo
3225 2765fc08 Andreas
     *
3226 2765fc08 Andreas
     * this function will get all photos from the fspot
3227 2765fc08 Andreas
     * database and randomly return ONE entry
3228 2765fc08 Andreas
     *
3229 2765fc08 Andreas
     * saddly there is yet no sqlite3 function which returns
3230 2765fc08 Andreas
     * the bulk result in array, so we have to fill up our
3231 2765fc08 Andreas
     * own here.
3232 2765fc08 Andreas
     * @return array
3233 2765fc08 Andreas
     */
3234 758887b7 Andreas
    public function get_random_photo()
3235 758887b7 Andreas
    {
3236 758887b7 Andreas
       $all = Array();
3237 758887b7 Andreas
3238 66085a75 Andreas
       $query_str = "
3239 6216473e Andreas
          SELECT p.id as id
3240 66085a75 Andreas
          FROM photos p
3241 66085a75 Andreas
       ";
3242 66085a75 Andreas
3243 66085a75 Andreas
       /* if show_tags is set, only return details for photos which
3244 66085a75 Andreas
          are specified to be shown
3245 66085a75 Andreas
       */
3246 66085a75 Andreas
       if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
3247 66085a75 Andreas
          $query_str.= "
3248 66085a75 Andreas
             INNER JOIN photo_tags pt
3249 66085a75 Andreas
                ON p.id=pt.photo_id
3250 66085a75 Andreas
             INNER JOIN tags t
3251 66085a75 Andreas
                ON pt.tag_id=t.id
3252 66085a75 Andreas
             WHERE
3253 66085a75 Andreas
                t.name IN ('".implode("','",$this->cfg->show_tags)."')";
3254 66085a75 Andreas
       }
3255 66085a75 Andreas
3256 66085a75 Andreas
       $result = $this->db->db_query($query_str);
3257 66085a75 Andreas
3258 758887b7 Andreas
       while($row = $this->db->db_fetch_object($result)) {
3259 223a626a Andreas
          array_push($all, $row['id']);
3260 758887b7 Andreas
       }
3261 758887b7 Andreas
3262 223a626a Andreas
       return $all[array_rand($all)];
3263 758887b7 Andreas
3264 758887b7 Andreas
    } // get_random_photo()
3265 758887b7 Andreas
3266 8223eed3 Andreas
    /**
3267 4552617d Andreas
     * get random photo tag photo
3268 4552617d Andreas
     *
3269 4552617d Andreas
     * this function will get all photos tagged with the requested
3270 4552617d Andreas
     * tag from the fspot database and randomly return ONE entry
3271 4552617d Andreas
     *
3272 4552617d Andreas
     * saddly there is yet no sqlite3 function which returns
3273 4552617d Andreas
     * the bulk result in array, so we have to fill up our
3274 4552617d Andreas
     * own here.
3275 4552617d Andreas
     * @return array
3276 4552617d Andreas
     */
3277 4552617d Andreas
    public function get_random_tag_photo($tagidx)
3278 4552617d Andreas
    {
3279 4552617d Andreas
       $all = Array();
3280 4552617d Andreas
3281 4552617d Andreas
       if(isset($this->cfg->show_tags) && !empty($this->cfg->show_tags)) {
3282 4552617d Andreas
          $query_str.= "
3283 ad8f72f7 Andreas
             SELECT
3284 ad8f72f7 Andreas
                DISTINCT pt1.photo_id as id
3285 ad8f72f7 Andreas
             FROM
3286 ad8f72f7 Andreas
                photo_tags pt1
3287 ad8f72f7 Andreas
             INNER JOIN photo_tags
3288 ad8f72f7 Andreas
                pt2 ON pt1.photo_id=pt2.photo_id
3289 ad8f72f7 Andreas
             INNER JOIN tags t1
3290 ad8f72f7 Andreas
                ON t1.id=pt1.tag_id
3291 ad8f72f7 Andreas
             INNER JOIN tags t2
3292 ad8f72f7 Andreas
                ON t2.id=pt2.tag_id
3293 ad8f72f7 Andreas
             WHERE
3294 ad8f72f7 Andreas
                pt1.tag_id LIKE '". $tagidx ."'
3295 ad8f72f7 Andreas
             AND
3296 ad8f72f7 Andreas
                t2.name IN  ('".implode("','",$this->cfg->show_tags)."')
3297 ad8f72f7 Andreas
             ORDER BY
3298 ad8f72f7 Andreas
                t1.sort_priority ASC";
3299 ad8f72f7 Andreas
       }
3300 ad8f72f7 Andreas
       else {
3301 ad8f72f7 Andreas
          $query_str = "
3302 ad8f72f7 Andreas
             SELECT
3303 6216473e Andreas
                p.id as id
3304 ad8f72f7 Andreas
             FROM
3305 ad8f72f7 Andreas
                photos p
3306 ad8f72f7 Andreas
             INNER JOIN photo_tags pt
3307 ad8f72f7 Andreas
                ON p.id=pt.photo_id
3308 ad8f72f7 Andreas
             WHERE
3309 ad8f72f7 Andreas
                pt.tag_id LIKE '". $tagidx ."'";
3310 4552617d Andreas
       }
3311 4552617d Andreas
3312 4552617d Andreas
       $result = $this->db->db_query($query_str);
3313 4552617d Andreas
3314 4552617d Andreas
       while($row = $this->db->db_fetch_object($result)) {
3315 4552617d Andreas
          array_push($all, $row['id']);
3316 4552617d Andreas
       }
3317 4552617d Andreas
3318 4552617d Andreas
       return $all[array_rand($all)];
3319 4552617d Andreas
3320 4552617d Andreas
    } // get_random_tag_photo()
3321 4552617d Andreas
3322 4552617d Andreas
    /**
3323 8223eed3 Andreas
     * validates provided date
3324 8223eed3 Andreas
     *
3325 8223eed3 Andreas
     * this function validates if the provided date
3326 8223eed3 Andreas
     * contains a valid date and will return true
3327 8223eed3 Andreas
     * if it is.
3328 2765fc08 Andreas
     * @param string $date_str
3329 2765fc08 Andreas
     * @return boolean
3330 8223eed3 Andreas
     */
3331 8223eed3 Andreas
    public function isValidDate($date_str)
3332 8223eed3 Andreas
    {
3333 8223eed3 Andreas
       $timestamp = strtotime($date_str);
3334 8223eed3 Andreas
3335 8223eed3 Andreas
       if(is_numeric($timestamp))
3336 8223eed3 Andreas
          return true;
3337 8223eed3 Andreas
3338 8223eed3 Andreas
       return false;
3339 8223eed3 Andreas
3340 8223eed3 Andreas
    } // isValidDate()
3341 8223eed3 Andreas
3342 8223eed3 Andreas
    /**
3343 8223eed3 Andreas
     * timestamp to string conversion
3344 2765fc08 Andreas
     * @param integer $timestamp
3345 2765fc08 Andreas
     * @return string
3346 8223eed3 Andreas
     */
3347 8223eed3 Andreas
    private function ts2str($timestamp)
3348 8223eed3 Andreas
    {
3349 3d867f51 Andreas
       if(!empty($timestamp) && is_numeric($timestamp))
3350 3d867f51 Andreas
          return strftime("%Y-%m-%d", $timestamp);
3351 3d867f51 Andreas
3352 8223eed3 Andreas
    } // ts2str()
3353 8223eed3 Andreas
3354 2765fc08 Andreas
    /**
3355 2765fc08 Andreas
     * extract tag-names from $_GET['tags']
3356 2765fc08 Andreas
     * @param string $tags_str
3357 2765fc08 Andreas
     * @return string
3358 2765fc08 Andreas
     */
3359 7f6d9076 Andreas
    private function extractTags($tags_str)
3360 7f6d9076 Andreas
    {
3361 2765fc08 Andreas
       $not_validated = split(',', $tags_str);
3362 7f6d9076 Andreas
       $validated = array();
3363 7f6d9076 Andreas
3364 7f6d9076 Andreas
       foreach($not_validated as $tag) {
3365 7f6d9076 Andreas
          if(is_numeric($tag))
3366 7f6d9076 Andreas
             array_push($validated, $tag);
3367 7f6d9076 Andreas
       }
3368 7f6d9076 Andreas
3369 7f6d9076 Andreas
       return $validated;
3370 7f6d9076 Andreas
3371 7f6d9076 Andreas
    } // extractTags()
3372 7f6d9076 Andreas
3373 4ff4a0e7 Andreas
    /**
3374 4ff4a0e7 Andreas
     * returns the full path to a thumbnail
3375 2765fc08 Andreas
     * @param integer $width
3376 2765fc08 Andreas
     * @param integer $photo
3377 2765fc08 Andreas
     * @return string
3378 4ff4a0e7 Andreas
     */
3379 6198d8ac Andreas
    public function get_thumb_path($width, $photo_idx, $version_idx)
3380 4ff4a0e7 Andreas
    {
3381 6198d8ac Andreas
       $md5 = $this->getMD5($photo_idx, $version_idx);
3382 5ea211de Andreas
       $sub_path = substr($md5, 0, 2);
3383 6a54409e Andreas
       return $this->cfg->thumb_path
3384 6a54409e Andreas
          . "/"
3385 4ff4a0e7 Andreas
          . $sub_path
3386 4ff4a0e7 Andreas
          . "/"
3387 4ff4a0e7 Andreas
          . $width
3388 4ff4a0e7 Andreas
          . "_"
3389 5ea211de Andreas
          . $md5;
3390 4ff4a0e7 Andreas
3391 4ff4a0e7 Andreas
    } // get_thumb_path()
3392 4ff4a0e7 Andreas
3393 7ff7dfb9 Andreas
    /**
3394 7ff7dfb9 Andreas
     * returns server's virtual host name
3395 2765fc08 Andreas
     * @return string
3396 7ff7dfb9 Andreas
     */
3397 7ff7dfb9 Andreas
    private function get_server_name()
3398 7ff7dfb9 Andreas
    {
3399 7ff7dfb9 Andreas
       return $_SERVER['SERVER_NAME'];
3400 7ff7dfb9 Andreas
    } // get_server_name()
3401 7ff7dfb9 Andreas
3402 7ff7dfb9 Andreas
    /**
3403 2765fc08 Andreas
     * returns type of webprotocol which is currently used
3404 2765fc08 Andreas
     * @return string
3405 7ff7dfb9 Andreas
     */
3406 7ff7dfb9 Andreas
    private function get_web_protocol()
3407 7ff7dfb9 Andreas
    {
3408 7ff7dfb9 Andreas
       if(!isset($_SERVER['HTTPS']))
3409 7ff7dfb9 Andreas
          return "http";
3410 7ff7dfb9 Andreas
       else
3411 7ff7dfb9 Andreas
          return "https";
3412 7ff7dfb9 Andreas
    } // get_web_protocol()
3413 7ff7dfb9 Andreas
3414 7ff7dfb9 Andreas
    /**
3415 7ff7dfb9 Andreas
     * return url to this phpfspot installation
3416 2765fc08 Andreas
     * @return string
3417 7ff7dfb9 Andreas
     */
3418 7ff7dfb9 Andreas
    private function get_phpfspot_url()
3419 7ff7dfb9 Andreas
    {
3420 8a050d43 Andreas
       return $this->get_web_protocol() ."://". $this->get_server_name() . $this->cfg->web_path;
3421 867cf65e Andreas
3422 7ff7dfb9 Andreas
    } // get_phpfspot_url()
3423 144f16a7 Andreas
3424 144f16a7 Andreas
    /**
3425 144f16a7 Andreas
     * returns the number of photos which are tagged with $tag_id
3426 2765fc08 Andreas
     * @param integer $tag_id
3427 2765fc08 Andreas
     * @return integer
3428 144f16a7 Andreas
     */
3429 144f16a7 Andreas
    public function get_num_photos($tag_id)
3430 144f16a7 Andreas
    {
3431 144f16a7 Andreas
       if($result = $this->db->db_fetchSingleRow("
3432 144f16a7 Andreas
          SELECT count(*) as number
3433 144f16a7 Andreas
          FROM photo_tags
3434 144f16a7 Andreas
          WHERE
3435 144f16a7 Andreas
             tag_id LIKE '". $tag_id ."'")) {
3436 144f16a7 Andreas
3437 144f16a7 Andreas
          return $result['number'];
3438 144f16a7 Andreas
3439 144f16a7 Andreas
       }
3440 144f16a7 Andreas
3441 144f16a7 Andreas
       return 0;
3442 144f16a7 Andreas
3443 144f16a7 Andreas
    } // get_num_photos()
3444 7f4f4e74 Andreas
3445 7f4f4e74 Andreas
    /**
3446 7f4f4e74 Andreas
     * check file exists and is readable
3447 7f4f4e74 Andreas
     *
3448 7f4f4e74 Andreas
     * returns true, if everything is ok, otherwise false
3449 7f4f4e74 Andreas
     * if $silent is not set, this function will output and
3450 7f4f4e74 Andreas
     * error message
3451 2765fc08 Andreas
     * @param string $file
3452 2765fc08 Andreas
     * @param boolean $silent
3453 55677896 Andreas
     * @return boolean
3454 7f4f4e74 Andreas
     */
3455 7f4f4e74 Andreas
    private function check_readable($file, $silent = null)
3456 7f4f4e74 Andreas
    {
3457 7f4f4e74 Andreas
       if(!file_exists($file)) {
3458 7f4f4e74 Andreas
          if(!isset($silent))
3459 7f4f4e74 Andreas
             print "File \"". $file ."\" does not exist.\n";
3460 7f4f4e74 Andreas
          return false;
3461 7f4f4e74 Andreas
       }
3462 7f4f4e74 Andreas
3463 7f4f4e74 Andreas
       if(!is_readable($file)) {
3464 7f4f4e74 Andreas
          if(!isset($silent))
3465 7f4f4e74 Andreas
             print "File \"". $file ."\" is not reachable for user ". $this->getuid() ."\n";
3466 7f4f4e74 Andreas
          return false;
3467 7f4f4e74 Andreas
       }
3468 7f4f4e74 Andreas
3469 7f4f4e74 Andreas
       return true;
3470 7f4f4e74 Andreas
3471 7f4f4e74 Andreas
    } // check_readable()
3472 7ff7dfb9 Andreas
3473 4dc79076 Andreas
    /**
3474 4dc79076 Andreas
     * check if all needed indices are present
3475 4dc79076 Andreas
     *
3476 4dc79076 Andreas
     * this function checks, if some needed indices are already
3477 4dc79076 Andreas
     * present, or if not, create them on the fly. they are
3478 4dc79076 Andreas
     * necessary to speed up some queries like that one look for
3479 4dc79076 Andreas
     * all tags, when show_tags is specified in the configuration.
3480 4dc79076 Andreas
     */
3481 4dc79076 Andreas
    private function checkDbIndices()
3482 4dc79076 Andreas
    {
3483 4dc79076 Andreas
       $result = $this->db->db_exec("
3484 4dc79076 Andreas
          CREATE INDEX IF NOT EXISTS
3485 4dc79076 Andreas
             phototag
3486 4dc79076 Andreas
          ON
3487 4dc79076 Andreas
             photo_tags
3488 4dc79076 Andreas
                (photo_id, tag_id)
3489 4dc79076 Andreas
       ");
3490 4dc79076 Andreas
3491 4dc79076 Andreas
    } // checkDbIndices()
3492 4dc79076 Andreas
3493 e8bc8f7d Andreas
    /**
3494 e8bc8f7d Andreas
     * retrive F-Spot database version
3495 e8bc8f7d Andreas
     *
3496 e8bc8f7d Andreas
     * this function will return the F-Spot database version number
3497 e8bc8f7d Andreas
     * It is stored within the sqlite3 database in the table meta
3498 2765fc08 Andreas
     * @return string|null
3499 e8bc8f7d Andreas
     */
3500 e8bc8f7d Andreas
    public function getFspotDBVersion()
3501 e8bc8f7d Andreas
    {
3502 e8bc8f7d Andreas
       if($result = $this->db->db_fetchSingleRow("
3503 e8bc8f7d Andreas
          SELECT data as version
3504 e8bc8f7d Andreas
          FROM meta
3505 e8bc8f7d Andreas
          WHERE
3506 e8bc8f7d Andreas
             name LIKE 'F-Spot Database Version'
3507 e8bc8f7d Andreas
       "))
3508 e8bc8f7d Andreas
          return $result['version'];
3509 e8bc8f7d Andreas
3510 e8bc8f7d Andreas
       return null;
3511 e8bc8f7d Andreas
3512 e8bc8f7d Andreas
    } // getFspotDBVersion()
3513 e8bc8f7d Andreas
3514 18ff7e04 Andreas
    /**
3515 2765fc08 Andreas
     * parse the provided URI and will returned the requested chunk
3516 2765fc08 Andreas
     * @param string $uri
3517 2765fc08 Andreas
     * @param string $mode
3518 2765fc08 Andreas
     * @return string
3519 18ff7e04 Andreas
     */
3520 18ff7e04 Andreas
    public function parse_uri($uri, $mode)
3521 18ff7e04 Andreas
    {
3522 81b97707 Andreas
       if(($components = parse_url($uri)) === false)
3523 81b97707 Andreas
          return $uri;
3524 18ff7e04 Andreas
3525 81b97707 Andreas
       switch($mode) {
3526 81b97707 Andreas
          case 'filename':
3527 81b97707 Andreas
             return basename($components['path']);
3528 81b97707 Andreas
             break;
3529 81b97707 Andreas
          case 'dirname':
3530 81b97707 Andreas
             return dirname($components['path']);
3531 81b97707 Andreas
             break;
3532 81b97707 Andreas
          case 'fullpath':
3533 81b97707 Andreas
             return $components['path'];
3534 81b97707 Andreas
             break;
3535 81b97707 Andreas
          default:
3536 81b97707 Andreas
             $this->throwError("unknown mode ". $mode);
3537 81b97707 Andreas
             break;
3538 18ff7e04 Andreas
       }
3539 18ff7e04 Andreas
3540 18ff7e04 Andreas
    } // parse_uri()
3541 18ff7e04 Andreas
3542 42d16009 Andreas
    /**
3543 42d16009 Andreas
     * validate config options
3544 42d16009 Andreas
     *
3545 42d16009 Andreas
     * this function checks if all necessary configuration options are
3546 42d16009 Andreas
     * specified and set.
3547 2765fc08 Andreas
     * @return boolean
3548 42d16009 Andreas
     */
3549 42d16009 Andreas
    private function check_config_options()
3550 42d16009 Andreas
    {
3551 42d16009 Andreas
       if(!isset($this->cfg->page_title) || $this->cfg->page_title == "")
3552 42d16009 Andreas
          $this->_error("Please set \$page_title in phpfspot_cfg");
3553 42d16009 Andreas
3554 42d16009 Andreas
       if(!isset($this->cfg->base_path) || $this->cfg->base_path == "")
3555 42d16009 Andreas
          $this->_error("Please set \$base_path in phpfspot_cfg");
3556 42d16009 Andreas
3557 42d16009 Andreas
       if(!isset($this->cfg->web_path) || $this->cfg->web_path == "")
3558 42d16009 Andreas
          $this->_error("Please set \$web_path in phpfspot_cfg");
3559 42d16009 Andreas
3560 42d16009 Andreas
       if(!isset($this->cfg->thumb_path) || $this->cfg->thumb_path == "")
3561 42d16009 Andreas
          $this->_error("Please set \$thumb_path in phpfspot_cfg");
3562 42d16009 Andreas
3563 42d16009 Andreas
       if(!isset($this->cfg->smarty_path) || $this->cfg->smarty_path == "")
3564 42d16009 Andreas
          $this->_error("Please set \$smarty_path in phpfspot_cfg");
3565 42d16009 Andreas
3566 42d16009 Andreas
       if(!isset($this->cfg->fspot_db) || $this->cfg->fspot_db == "")
3567 42d16009 Andreas
          $this->_error("Please set \$fspot_db in phpfspot_cfg");
3568 42d16009 Andreas
3569 42d16009 Andreas
       if(!isset($this->cfg->db_access) || $this->cfg->db_access == "")
3570 42d16009 Andreas
          $this->_error("Please set \$db_access in phpfspot_cfg");
3571 42d16009 Andreas
3572 42d16009 Andreas
       if(!isset($this->cfg->phpfspot_db) || $this->cfg->phpfspot_db == "")
3573 42d16009 Andreas
          $this->_error("Please set \$phpfspot_db in phpfspot_cfg");
3574 42d16009 Andreas
3575 42d16009 Andreas
       if(!isset($this->cfg->thumb_width) || $this->cfg->thumb_width == "")
3576 42d16009 Andreas
          $this->_error("Please set \$thumb_width in phpfspot_cfg");
3577 42d16009 Andreas
3578 42d16009 Andreas
       if(!isset($this->cfg->thumb_height) || $this->cfg->thumb_height == "")
3579 42d16009 Andreas
          $this->_error("Please set \$thumb_height in phpfspot_cfg");
3580 42d16009 Andreas
3581 42d16009 Andreas
       if(!isset($this->cfg->photo_width) || $this->cfg->photo_width == "")
3582 42d16009 Andreas
          $this->_error("Please set \$photo_width in phpfspot_cfg");
3583 42d16009 Andreas
3584 42d16009 Andreas
       if(!isset($this->cfg->mini_width) || $this->cfg->mini_width == "")
3585 42d16009 Andreas
          $this->_error("Please set \$mini_width in phpfspot_cfg");
3586 42d16009 Andreas
3587 42d16009 Andreas
       if(!isset($this->cfg->thumbs_per_page))
3588 42d16009 Andreas
          $this->_error("Please set \$thumbs_per_page in phpfspot_cfg");
3589 42d16009 Andreas
3590 1a7ed49f Andreas
       if(!isset($this->cfg->enable_replace_path))
3591 1a7ed49f Andreas
          $this->_error("Please set \$enable_replace_path in phpfspot_cfg");
3592 42d16009 Andreas
3593 1a7ed49f Andreas
       if($this->cfg->enable_replace_path == true) {
3594 1a7ed49f Andreas
          if(!isset($this->cfg->path_replace_from) || $this->cfg->path_replace_from == "")
3595 1a7ed49f Andreas
             $this->_error("Please set \$path_replace_from in phpfspot_cfg");
3596 1a7ed49f Andreas
3597 1a7ed49f Andreas
          if(!isset($this->cfg->path_replace_to) || $this->cfg->path_replace_to == "")
3598 1a7ed49f Andreas
             $this->_error("Please set \$path_replace_to in phpfspot_cfg");
3599 1a7ed49f Andreas
       }
3600 42d16009 Andreas
3601 42d16009 Andreas
       if(!isset($this->cfg->hide_tags))
3602 42d16009 Andreas
          $this->_error("Please set \$hide_tags in phpfspot_cfg");
3603 42d16009 Andreas
3604 42d16009 Andreas
       if(!isset($this->cfg->theme_name))
3605 42d16009 Andreas
          $this->_error("Please set \$theme_name in phpfspot_cfg");
3606 42d16009 Andreas
3607 42d16009 Andreas
       if(!isset($this->cfg->logging))
3608 42d16009 Andreas
          $this->_error("Please set \$logging in phpfspot_cfg");
3609 42d16009 Andreas
3610 42d16009 Andreas
       if(isset($this->cfg->logging) && $this->cfg->logging == 'logfile') {
3611 42d16009 Andreas
3612 42d16009 Andreas
          if(!isset($this->cfg->log_file))
3613 42d16009 Andreas
             $this->_error("Please set \$log_file because you set logging = log_file in phpfspot_cfg");
3614 42d16009 Andreas
3615 42d16009 Andreas
          if(!is_writeable($this->cfg->log_file))
3616 42d16009 Andreas
             $this->_error("The specified \$log_file ". $log_file ." is not writeable!");
3617 42d16009 Andreas
3618 42d16009 Andreas
       }
3619 42d16009 Andreas
3620 8a050d43 Andreas
       /* remove trailing slash, if set */
3621 8a050d43 Andreas
       if($this->cfg->web_path == "/")
3622 8a050d43 Andreas
          $this->cfg->web_path = "";
3623 8a050d43 Andreas
       elseif(preg_match('/\/$/', $this->cfg->web_path))
3624 8a050d43 Andreas
          $this->cfg->web_path = preg_replace('/\/$/', '', $this->cfg->web_path);
3625 42d16009 Andreas
3626 42d16009 Andreas
       return $this->runtime_error;
3627 42d16009 Andreas
3628 42d16009 Andreas
    } // check_config_options()
3629 42d16009 Andreas
3630 1dc0e2e5 Andreas
    /**
3631 1dc0e2e5 Andreas
     * cleanup phpfspot own database
3632 1dc0e2e5 Andreas
     *
3633 1dc0e2e5 Andreas
     * When photos are getting delete from F-Spot, there will remain
3634 1dc0e2e5 Andreas
     * remain some residues in phpfspot own database. This function
3635 1dc0e2e5 Andreas
     * will try to wipe them out.
3636 1dc0e2e5 Andreas
     */
3637 1dc0e2e5 Andreas
    public function cleanup_phpfspot_db()
3638 1dc0e2e5 Andreas
    {
3639 1dc0e2e5 Andreas
       $to_delete = Array();
3640 1dc0e2e5 Andreas
3641 1dc0e2e5 Andreas
       $result = $this->cfg_db->db_query("
3642 6216473e Andreas
          SELECT img_idx as img_idx
3643 1dc0e2e5 Andreas
          FROM images
3644 1dc0e2e5 Andreas
          ORDER BY img_idx ASC
3645 1dc0e2e5 Andreas
       ");
3646 1dc0e2e5 Andreas
3647 1dc0e2e5 Andreas
       while($row = $this->cfg_db->db_fetch_object($result)) {
3648 1dc0e2e5 Andreas
          if(!$this->db->db_fetchSingleRow("
3649 6216473e Andreas
             SELECT id as id
3650 1dc0e2e5 Andreas
             FROM photos
3651 1dc0e2e5 Andreas
             WHERE id='". $row['img_idx'] ."'")) {
3652 1dc0e2e5 Andreas
3653 1dc0e2e5 Andreas
             array_push($to_delete, $row['img_idx'], ',');
3654 1dc0e2e5 Andreas
          }
3655 1dc0e2e5 Andreas
       }
3656 1dc0e2e5 Andreas
3657 71a8a6ed Andreas
       print count($to_delete) ." unnecessary objects will be removed from phpfspot's database.\n";
3658 71a8a6ed Andreas
3659 1dc0e2e5 Andreas
       $this->cfg_db->db_exec("
3660 1dc0e2e5 Andreas
          DELETE FROM images
3661 1dc0e2e5 Andreas
          WHERE img_idx IN (". implode($to_delete) .")
3662 1dc0e2e5 Andreas
       ");
3663 1dc0e2e5 Andreas
3664 1dc0e2e5 Andreas
    } // cleanup_phpfspot_db()
3665 1dc0e2e5 Andreas
3666 1b98418f Andreas
    /**
3667 1b98418f Andreas
     * return first image of the page, the $current photo
3668 1b98418f Andreas
     * lies in.
3669 1b98418f Andreas
     *
3670 1b98418f Andreas
     * this function is used to find out the first photo of the
3671 1b98418f Andreas
     * current page, in which the $current photo lies. this is
3672 1b98418f Andreas
     * used to display the correct photo, when calling showPhotoIndex()
3673 1b98418f Andreas
     * from showImage()
3674 2765fc08 Andreas
     * @param integer $current
3675 2765fc08 Andreas
     * @param integer $max
3676 2765fc08 Andreas
     * @return integer
3677 1b98418f Andreas
     */
3678 1b98418f Andreas
    private function getCurrentPage($current, $max)
3679 1b98418f Andreas
    {
3680 1b98418f Andreas
       if(isset($this->cfg->thumbs_per_page) && !empty($this->cfg->thumbs_per_page)) {
3681 1b98418f Andreas
          for($page_start = 0; $page_start <= $max; $page_start+=$this->cfg->thumbs_per_page) {
3682 1b98418f Andreas
             if($current >= $page_start && $current < ($page_start+$this->cfg->thumbs_per_page))
3683 1b98418f Andreas
                return $page_start;
3684 1b98418f Andreas
          }
3685 1b98418f Andreas
       }
3686 1b98418f Andreas
       return 0;
3687 1b98418f Andreas
3688 1b98418f Andreas
    } // getCurrentPage()
3689 1b98418f Andreas
3690 e1730519 Andreas
    /**
3691 e1730519 Andreas
     * return mime info
3692 e1730519 Andreas
     *
3693 e1730519 Andreas
     * this function tries to find out the correct mime-type
3694 e1730519 Andreas
     * for the provided file.
3695 e1730519 Andreas
     * @param string $file
3696 e1730519 Andreas
     * @return string
3697 e1730519 Andreas
     */
3698 e1730519 Andreas
    public function get_mime_info($file)
3699 e1730519 Andreas
    {
3700 4dbea8ec Arun
       $details = getimagesize($file);
3701 e1730519 Andreas
3702 e1730519 Andreas
       /* if getimagesize() returns empty, try at least to find out the
3703 e1730519 Andreas
          mime type.
3704 e1730519 Andreas
       */
3705 e1730519 Andreas
       if(empty($details) && function_exists('mime_content_type')) {
3706 e1730519 Andreas
3707 e1730519 Andreas
          // mime_content_type is marked as deprecated in the documentation,
3708 e1730519 Andreas
          // but is it really necessary to force users to install a PECL
3709 e1730519 Andreas
          // extension?
3710 e1730519 Andreas
          $details['mime'] = mime_content_type($file);
3711 e1730519 Andreas
       }
3712 e1730519 Andreas
3713 e1730519 Andreas
       return $details['mime'];
3714 e1730519 Andreas
3715 e1730519 Andreas
    } // get_mime_info()
3716 e1730519 Andreas
3717 8b5da769 Andreas
    /**
3718 8b5da769 Andreas
     * return tag-name by tag-idx
3719 8b5da769 Andreas
     *
3720 8b5da769 Andreas
     * this function returns the tag-name for the requested
3721 8b5da769 Andreas
     * tag specified by tag-idx.
3722 8b5da769 Andreas
     * @param integer $idx
3723 8b5da769 Andreas
     * @return string
3724 8b5da769 Andreas
     */
3725 8b5da769 Andreas
    public function get_tag_name($idx)
3726 8b5da769 Andreas
    {
3727 8b5da769 Andreas
        if($result = $this->db->db_fetchSingleRow("
3728 6216473e Andreas
          SELECT name as name
3729 8b5da769 Andreas
          FROM tags
3730 8b5da769 Andreas
          WHERE
3731 8b5da769 Andreas
             id LIKE '". $idx ."'")) {
3732 8b5da769 Andreas
3733 8b5da769 Andreas
          return $result['name'];
3734 8b5da769 Andreas
3735 8b5da769 Andreas
       }
3736 8b5da769 Andreas
3737 8b5da769 Andreas
       return 0;
3738 8b5da769 Andreas
3739 8b5da769 Andreas
    } // get_tag_name()
3740 8b5da769 Andreas
3741 8a050d43 Andreas
    /**
3742 8a050d43 Andreas
     * parse user friendly url which got rewritten by the websever
3743 8a050d43 Andreas
     * @param string $request_uri
3744 8a050d43 Andreas
     * @return string
3745 8a050d43 Andreas
     */
3746 9471faab Andreas
    private function parse_user_friendly_url($request_uri)
3747 9471faab Andreas
    {
3748 867cf65e Andreas
       if(preg_match('/\/photoview\/|\/photo\/|\/tag\//', $request_uri)) {
3749 867cf65e Andreas
3750 9471faab Andreas
          $options = explode('/', $request_uri);
3751 9471faab Andreas
3752 9471faab Andreas
          switch($options[1]) {
3753 9471faab Andreas
             case 'photoview':
3754 9471faab Andreas
                if(is_numeric($options[2])) {
3755 6a5756a7 Andreas
                   $this->session_cleanup();
3756 6a5756a7 Andreas
                   //unset($_SESSION['start_action']);
3757 6a5756a7 Andreas
                   //unset($_SESSION['selected_tags']);
3758 8a050d43 Andreas
                   $_GET['mode'] = 'showp';
3759 9471faab Andreas
                   return $this->showPhoto($options[2]);
3760 9471faab Andreas
                }
3761 9471faab Andreas
                break;
3762 9471faab Andreas
             case 'photo':
3763 9471faab Andreas
                if(is_numeric($options[2])) {
3764 6198d8ac Andreas
                   $width = NULL;
3765 6198d8ac Andreas
                   $version = NULL;
3766 6198d8ac Andreas
                   if(isset($options[3]) && is_numeric($options[3]))
3767 6198d8ac Andreas
                      $width = $options[3];
3768 6198d8ac Andreas
                   if(isset($options[4]) && is_numeric($options[4]))
3769 6198d8ac Andreas
                      $version = $options[4];
3770 9471faab Andreas
                   require_once "phpfspot_img.php";
3771 9471faab Andreas
                   $img = new PHPFSPOT_IMG;
3772 6198d8ac Andreas
                   $img->showImg($options[2], $width, $version);
3773 9471faab Andreas
                }
3774 9471faab Andreas
                exit;
3775 9471faab Andreas
                break;
3776 867cf65e Andreas
             case 'tag':
3777 867cf65e Andreas
                if(is_numeric($options[2])) {
3778 48b46c22 Andreas
                   $this->session_cleanup();
3779 867cf65e Andreas
                   $_GET['tags'] = $options[2];
3780 50e93956 Andreas
                   $_SESSION['selected_tags'] = Array($options[2]);
3781 8a63e413 Andreas
                   if(isset($options[3]) && is_numeric($options[3]))
3782 8a63e413 Andreas
                      $_SESSION['begin_with'] = $options[3];
3783 867cf65e Andreas
                   return $this->showPhotoIndex();
3784 867cf65e Andreas
                }
3785 867cf65e Andreas
                break;
3786 9471faab Andreas
          }
3787 9471faab Andreas
       }
3788 9471faab Andreas
3789 9471faab Andreas
    } // parse_user_friendly_url()
3790 9471faab Andreas
3791 867cf65e Andreas
    /**
3792 867cf65e Andreas
     * check if user-friendly-urls are enabled
3793 867cf65e Andreas
     *
3794 867cf65e Andreas
     * this function will return true, if the config option
3795 867cf65e Andreas
     * $user_friendly_url has been set. Otherwise false.
3796 867cf65e Andreas
     * @return boolean
3797 867cf65e Andreas
     */
3798 867cf65e Andreas
    private function is_user_friendly_url()
3799 867cf65e Andreas
    {
3800 867cf65e Andreas
       if(isset($this->cfg->user_friendly_url) && $this->cfg->user_friendly_url)
3801 867cf65e Andreas
          return true;
3802 867cf65e Andreas
3803 867cf65e Andreas
       return false;
3804 867cf65e Andreas
3805 867cf65e Andreas
    } // is_user_friendly_url()
3806 867cf65e Andreas
3807 48b46c22 Andreas
    /**
3808 48b46c22 Andreas
     * session cleanup
3809 48b46c22 Andreas
     *
3810 48b46c22 Andreas
     * this function will cleanup user's session information
3811 48b46c22 Andreas
     */
3812 48b46c22 Andreas
    private function session_cleanup()
3813 48b46c22 Andreas
    {
3814 48b46c22 Andreas
       unset($_SESSION['begin_with']);
3815 48b46c22 Andreas
       $this->resetDateSearch();
3816 48b46c22 Andreas
       $this->resetPhotoView();
3817 48b46c22 Andreas
       $this->resetTagSearch();
3818 48b46c22 Andreas
       $this->resetNameSearch();
3819 48b46c22 Andreas
       $this->resetDateSearch();
3820 6a5756a7 Andreas
       $this->resetTags();
3821 48b46c22 Andreas
3822 48b46c22 Andreas
    } // session_cleanup()
3823 9471faab Andreas
3824 6198d8ac Andreas
    /**
3825 6198d8ac Andreas
     * get database version
3826 6198d8ac Andreas
     *
3827 6198d8ac Andreas
     * this function queries the meta table
3828 6198d8ac Andreas
     * and returns the current database version.
3829 6198d8ac Andreas
     *
3830 6198d8ac Andreas
     * @return integer
3831 6198d8ac Andreas
     */
3832 6198d8ac Andreas
    public function get_db_version()
3833 6198d8ac Andreas
    {
3834 6198d8ac Andreas
       if($row = $this->cfg_db->db_fetchSingleRow("
3835 6216473e Andreas
          SELECT meta_value as meta_value
3836 6198d8ac Andreas
          FROM
3837 6198d8ac Andreas
             meta
3838 6198d8ac Andreas
          WHERE
3839 6198d8ac Andreas
             meta_key LIKE 'phpfspot Database Version'
3840 6198d8ac Andreas
          ")) {
3841 6198d8ac Andreas
3842 6198d8ac Andreas
          return $row['meta_value'];
3843 6198d8ac Andreas
3844 6198d8ac Andreas
       }
3845 6198d8ac Andreas
3846 6198d8ac Andreas
       return 0;
3847 6198d8ac Andreas
3848 6198d8ac Andreas
    } // get_db_version()
3849 6198d8ac Andreas
3850 6198d8ac Andreas
    /**
3851 6198d8ac Andreas
     * get photo versions
3852 6198d8ac Andreas
     *
3853 6198d8ac Andreas
     * this function returns an array of all available
3854 6198d8ac Andreas
     * alterntaive versions of the provided photo id.
3855 6198d8ac Andreas
     * has alternative photo versions available
3856 6198d8ac Andreas
     *
3857 6198d8ac Andreas
     * @param int $idx
3858 6198d8ac Andreas
     * @return array
3859 6198d8ac Andreas
     */
3860 6198d8ac Andreas
    public function get_photo_versions($idx)
3861 6198d8ac Andreas
    {
3862 6198d8ac Andreas
       $versions = Array();
3863 6198d8ac Andreas
3864 6198d8ac Andreas
       $result = $this->db->db_query("
3865 6198d8ac Andreas
          SELECT
3866 6198d8ac Andreas
             version_id
3867 6198d8ac Andreas
          FROM
3868 6198d8ac Andreas
             photo_versions
3869 6198d8ac Andreas
          WHERE
3870 781c4674 Andreas
             photo_id LIKE '". $idx ."'
3871 781c4674 Andreas
          ORDER BY
3872 781c4674 Andreas
             version_id ASC
3873 781c4674 Andreas
       ");
3874 6198d8ac Andreas
3875 6198d8ac Andreas
       while($row = $this->cfg_db->db_fetch_object($result)) {
3876 6198d8ac Andreas
          array_push($versions, $row['version_id']);
3877 6198d8ac Andreas
       }
3878 6198d8ac Andreas
3879 6198d8ac Andreas
       return $versions;
3880 6198d8ac Andreas
3881 6198d8ac Andreas
    } // get_photo_versions()
3882 6198d8ac Andreas
3883 6198d8ac Andreas
    /**
3884 6198d8ac Andreas
     * check for invalid version of photo
3885 6198d8ac Andreas
     *
3886 6198d8ac Andreas
     * this function validates the provided photo-id and version-id
3887 6198d8ac Andreas
     *
3888 6198d8ac Andreas
     * @param int $photo_idx
3889 6198d8ac Andreas
     * @param int $version_idx
3890 6198d8ac Andreas
     * @return bool
3891 6198d8ac Andreas
     */
3892 6198d8ac Andreas
    public function is_valid_version($photo_idx, $version_idx)
3893 6198d8ac Andreas
    {
3894 6198d8ac Andreas
       /* the original version is always valid */
3895 6198d8ac Andreas
       if($version_idx == 0)
3896 6198d8ac Andreas
          return true;
3897 6198d8ac Andreas
3898 6198d8ac Andreas
       if($versions = $this->get_photo_versions($photo_idx)) {
3899 6198d8ac Andreas
          if(in_array($version_idx, $versions))
3900 6198d8ac Andreas
             return true;
3901 6198d8ac Andreas
       }
3902 6198d8ac Andreas
3903 6198d8ac Andreas
       return false;
3904 6198d8ac Andreas
3905 6198d8ac Andreas
    } // is_valid_version()
3906 6198d8ac Andreas
3907 6198d8ac Andreas
    /**
3908 6198d8ac Andreas
     * get photo version name
3909 6198d8ac Andreas
     *
3910 6198d8ac Andreas
     * this function returns the name of the version
3911 6198d8ac Andreas
     * identified by the photo-id and version-id.
3912 6198d8ac Andreas
     *
3913 6198d8ac Andreas
     * @param int $photo_idx
3914 6198d8ac Andreas
     * @param int $version_idx
3915 6198d8ac Andreas
     * @return string
3916 6198d8ac Andreas
     */
3917 6198d8ac Andreas
    public function get_photo_version_name($photo_idx, $version_idx)
3918 6198d8ac Andreas
    {
3919 6198d8ac Andreas
       if($row = $this->db->db_fetchSingleRow("
3920 6198d8ac Andreas
          SELECT
3921 6198d8ac Andreas
             name
3922 6198d8ac Andreas
          FROM
3923 6198d8ac Andreas
             photo_versions
3924 6198d8ac Andreas
          WHERE
3925 6198d8ac Andreas
             photo_id LIKE '". $photo_idx ."'
3926 6198d8ac Andreas
          AND
3927 6198d8ac Andreas
             version_id LIKE '". $version_idx ."'")) {
3928 6198d8ac Andreas
3929 6198d8ac Andreas
          return $row['name'];
3930 6198d8ac Andreas
3931 6198d8ac Andreas
       }
3932 6198d8ac Andreas
3933 6198d8ac Andreas
       return false;
3934 6198d8ac Andreas
3935 6198d8ac Andreas
    } // get_photo_version_name()
3936 6198d8ac Andreas
3937 37d9c8bf Andreas
    /**
3938 37d9c8bf Andreas
     */
3939 37d9c8bf Andreas
    public function is_valid_width($image_width)
3940 37d9c8bf Andreas
    {
3941 37d9c8bf Andreas
       if(in_array($image_width,
3942 37d9c8bf Andreas
          Array($this->cfg->thumb_width,
3943 37d9c8bf Andreas
                $this->cfg->photo_width,
3944 37d9c8bf Andreas
                $this->cfg->mini_width,
3945 37d9c8bf Andreas
                30)))
3946 37d9c8bf Andreas
3947 37d9c8bf Andreas
          return true;
3948 37d9c8bf Andreas
3949 37d9c8bf Andreas
       return false;
3950 37d9c8bf Andreas
3951 37d9c8bf Andreas
    } // is_valid_width()
3952 37d9c8bf Andreas
3953 e8bc8f7d Andreas
 } // class PHPFSPOT
3954 61f2970e Andreas
3955 61f2970e Andreas
 ?>