user_login || $password != $this->user_password) return false; @session_start(); $_SESSION['logged'] = true; return true; } public function logout() { @session_start(); $_SESSION = array(); } public function isLogged() { @session_start(); if (!empty($_SESSION['logged'])) return true; return false; } public function requireLogin() { if (!$this->isLogged()) redirect('include/login.php'); } private function getThumbPath($hash) { $dir = CACHE_DIR . '/img/' . $hash[0]; if (!file_exists($dir)) mkdir($dir, 0700); return $dir . '/' . $hash . '_thumb.jpg'; } private function getSmallPath($hash) { $dir = CACHE_DIR . '/img/' . $hash[0]; if (!file_exists($dir)) mkdir($dir, 0700); return $dir . '/' . $hash . '_small.jpg'; } public function __construct() { $init = false; if (!file_exists(CACHE_DIR)) { mkdir(CACHE_DIR, 0700); mkdir(CACHE_DIR . '/img', 0700); } if (!file_exists(CACHE_DIR . '/photos.db')) $init = true; $this->db = new SQLiteDatabase(CACHE_DIR . '/photos.db', 600); if ($init) $this->initDB(); } private function initDB() { $this->db->queryExec(' CREATE TABLE photos ( id INTEGER UNSIGNED PRIMARY KEY NOT NULL, filename VARCHAR(255) NOT NULL, path VARCHAR(255) NOT NULL, width SMALLINT UNSIGNED NOT NULL, height SMALLINT UNSIGNED NOT NULL, year SMALLINT UNSIGNED NOT NULL, month TINYINT UNSIGNED NOT NULL, day TINYINT UNSIGNED NOT NULL, time INTEGER UNSIGNED NOT NULL, comment VARCHAR(255) NOT NULL, hash VARCHAR(50) NOT NULL ); CREATE UNIQUE INDEX hash ON photos (hash); CREATE INDEX file ON photos (filename, path); CREATE INDEX date ON photos (year, month, day); CREATE TABLE tags ( name VARCHAR(255) NOT NULL, photo INTEGER UNSIGNED, PRIMARY KEY (name, photo) );'); } public function getInfos($filename, $path) { $res = $this->db->arrayQuery("SELECT * FROM photos WHERE filename='".sqlite_escape_string($filename)."' AND path='".sqlite_escape_string($path)."'", SQLITE_ASSOC); if (empty($res[0])) return false; $tags = $this->db->arrayQuery('SELECT name FROM tags WHERE photo="'.(int)$res[0]['id'].'";', SQLITE_NUM); $res[0]['tags'] = array(); foreach ($tags as $row) { $res[0]['tags'][] = $row[0]; } return $res[0]; } private function getHash($file, $path, $size, $time) { return md5($file . $path . $size . $time); } private function updateInfos($id, $filename, $path, $hash=false) { $file = BASE_DIR . '/' . ($path ? $path . '/' : '') . $filename; $new_hash = $this->getHash($filename, $path, filesize($file), filemtime($file)); if ($new_hash == $hash) { return false; } $this->db->queryExec('DELETE FROM photos WHERE id="'.(int)$id.'";'); $this->db->queryExec('DELETE FROM tags WHERE photos="'.(int)$id.'";'); $thumb = $this->getThumbPath($hash); if (file_exists($thumb)) unlink($thumb); $small = $this->getSmallPath($hash); if (file_exists($small)) unlink($small); $this->db->addInfos($filename, $path); return true; } public function addInfos($filename, $path) { $file = BASE_DIR . '/' . ($path ? $path . '/' : '') . $filename; $size = getimagesize($file, $infos); $width = $size[1]; $height = $size[2]; $comment = ''; $tags = array(); if (!empty($infos['APP13'])) { $iptc = iptcparse($infos['APP13']); if (!empty($iptc['2#025'])) { foreach ($iptc['2#025'] as $tag) { $tags[] = utf8_decode($tag); } } unset($iptc); } unset($infos, $size); $exif = exif_read_data($file, 'COMMENT,THUMBNAIL', true, true); if (!empty($exif)) { if (!empty($exif['DateTime'])) { list($d, $h) = explode(' ', $exif['DateTime']); $d = explode(':', $d); $h = explode(':', $h); $date = mktime($h[0], $h[1], 0, $d[1], $d[2], $d[0]); } if (!empty($exif['COMMENT'])) { $comment = implode("\n", $exif['COMMENT']); $comment = trim($comment); } if (!empty($exif['THUMBNAIL']['THUMBNAIL'])) { $thumb = $exif['THUMBNAIL']['THUMBNAIL']; } } unset($exif); $file_time = filemtime($file); $hash = $this->getHash($filename, $path, filesize($file), $file_time); if (empty($date)) $date = $file_time; if (isset($thumb)) { file_put_contents($this->getThumbPath($hash), $thumb); } $this->db->unbufferedQuery("INSERT INTO photos (id, filename, path, width, height, year, month, day, time, comment, hash) VALUES (NULL, '".sqlite_escape_string($filename)."', '".sqlite_escape_string($path)."', '".(int)$width."', '".(int)$height."', '".date('Y', $date)."', '".date('m', $date)."', '".date('d', $date)."', '".(int)$date."', '".sqlite_escape_string($comment)."', '".sqlite_escape_string($hash)."');"); $id = $this->db->lastInsertRowid(); foreach ($tags as $tag) { $this->db->unbufferedQuery("INSERT INTO tags (name, photo) VALUES ('".sqlite_escape_string($tag)."', '".(int)$id."');"); } return array( 'id' => $id, 'filename' => $filename, 'path' => $path, 'width' => $width, 'height'=> $height, 'time' => $date, 'comment'=> $comment, 'hash' => $hash, ); } public function getDateIndex() { //$res = $this->db->arrayQuery(" } public function getDirectory($path='') { $path = preg_replace('!(^/+|/+$)!', '', $path); if ($path == '.' || empty($path)) $dir_path = BASE_DIR . '/'; else $dir_path = BASE_DIR . '/' . $path . '/'; $dir = dir($dir_path); if (!$dir) return false; $dirs = array(); $pictures = array(); $to_update = array(); while ($file = $dir->read()) { $file_path = $dir_path . $file; if ($file[0] == '.' || $file_path == CACHE_DIR || $file_path == BASE_DIR . '/include') continue; if (is_dir($file_path)) { $dirs[] = $file; } elseif (!preg_match('!\.jpe?g$!i', $file)) { continue; } elseif ($pic = $this->getInfos($file, $path)) { $pictures[$file] = $pic; } else { $to_update[] = $file; } } $dir->close(); sort($dirs); ksort($pictures); return array($dirs, $pictures, $to_update); } public function debug() { print_r($this->db->arrayQuery('SELECT * FROM photos;')); print_r($this->db->arrayQuery('SELECT * FROM tags;')); } } ?>