2011-10-02 14:24:02 +04:00
|
|
|
<?php
|
2011-09-26 00:32:08 +04:00
|
|
|
|
|
|
|
require_once('base.php'); // base lib
|
2011-10-18 23:43:33 +04:00
|
|
|
require_once('lib/images_utils.php');
|
2011-09-26 00:32:08 +04:00
|
|
|
|
|
|
|
class OC_GALLERY_SCANNER {
|
|
|
|
|
|
|
|
public static function scan($root) {
|
|
|
|
$albums = array();
|
|
|
|
self::scanDir($root, $albums);
|
|
|
|
return $albums;
|
|
|
|
}
|
|
|
|
|
2011-10-18 23:43:33 +04:00
|
|
|
public static function cleanUp() {
|
|
|
|
$stmt = OC_DB::prepare('DELETE FROM *PREFIX*gallery_albums');
|
|
|
|
$stmt->execute(array());
|
|
|
|
$stmt = OC_DB::prepare('DELETE FROM *PREFIX*gallery_photos');
|
|
|
|
$stmt->execute(array());
|
|
|
|
}
|
|
|
|
|
2011-09-26 00:32:08 +04:00
|
|
|
public static function scanDir($path, &$albums) {
|
|
|
|
$current_album = array('name'=> $path, 'imagesCount' => 0, 'images' => array());
|
|
|
|
$current_album['name'] = str_replace('/', '.', str_replace(OC::$CONFIG_DATADIRECTORY, '', $current_album['name']));
|
|
|
|
$current_album['name'] = ($current_album['name']==='')?'main':$current_album['name'];
|
|
|
|
|
|
|
|
if ($dh = OC_Filesystem::opendir($path)) {
|
|
|
|
while (($filename = readdir($dh)) !== false) {
|
|
|
|
$filepath = $path.'/'.$filename;
|
|
|
|
if (substr($filename, 0, 1) == '.') continue;
|
|
|
|
if (OC_Filesystem::is_dir($filepath)) {
|
|
|
|
self::scanDir($filepath, $albums);
|
|
|
|
} elseif (self::isPhoto($path.'/'.$filename)) {
|
|
|
|
$current_album['images'][] = $filepath;
|
|
|
|
}
|
2011-10-18 23:43:33 +04:00
|
|
|
}
|
2011-09-26 00:32:08 +04:00
|
|
|
}
|
|
|
|
$current_album['imagesCount'] = count($current_album['images']);
|
|
|
|
$albums[] = $current_album;
|
2011-10-27 23:16:04 +04:00
|
|
|
$stmt = OC_DB::prepare('SELECT * FROM *PREFIX*gallery_albums WHERE uid_owner LIKE ? AND album_name LIKE ?');
|
2011-09-26 00:32:08 +04:00
|
|
|
$result = $stmt->execute(array(OC_User::getUser(), $current_album['name']));
|
2011-10-02 15:52:04 +04:00
|
|
|
if ($result->numRows() == 0 && count($current_album['images'])) {
|
2011-10-27 23:16:04 +04:00
|
|
|
$stmt = OC_DB::prepare('INSERT INTO *PREFIX*gallery_albums (uid_owner, album_name) VALUES (?, ?)');
|
2011-09-26 00:32:08 +04:00
|
|
|
$stmt->execute(array(OC_User::getUser(), $current_album['name']));
|
|
|
|
}
|
2011-10-27 23:16:04 +04:00
|
|
|
$stmt = OC_DB::prepare('SELECT * FROM *PREFIX*gallery_albums WHERE uid_owner LIKE ? AND album_name LIKE ?');
|
2011-09-26 00:32:08 +04:00
|
|
|
$result = $stmt->execute(array(OC_User::getUser(), $current_album['name']));
|
2011-10-27 23:16:04 +04:00
|
|
|
$albumId = $result->fetchAll();
|
|
|
|
$albumId = $albumId[0]['album_id'];
|
2011-09-26 00:32:08 +04:00
|
|
|
foreach ($current_album['images'] as $img) {
|
2011-10-27 23:16:04 +04:00
|
|
|
$stmt = OC_DB::prepare('SELECT * FROM *PREFIX*gallery_photos WHERE album_id = ? AND file_path LIKE ?');
|
2011-09-26 00:32:08 +04:00
|
|
|
$result = $stmt->execute(array($albumId, $img));
|
|
|
|
if ($result->numRows() == 0) {
|
2011-10-27 23:16:04 +04:00
|
|
|
$stmt = OC_DB::prepare('INSERT INTO *PREFIX*gallery_photos (album_id, file_path) VALUES (?, ?)');
|
2011-09-26 00:32:08 +04:00
|
|
|
$stmt->execute(array($albumId, $img));
|
|
|
|
}
|
|
|
|
}
|
2011-10-18 23:43:33 +04:00
|
|
|
if (count($current_album['images'])) {
|
|
|
|
self::createThumbnail($current_album['name'],$current_album['images']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function createThumbnail($albumName, $files) {
|
|
|
|
$file_count = min(count($files), 10);
|
|
|
|
$thumbnail = imagecreatetruecolor($file_count*200, 200);
|
|
|
|
for ($i = 0; $i < $file_count; $i++) {
|
|
|
|
CroppedThumbnail(OC_Config::getValue("datadirectory").'/'. OC_User::getUser() .'/files/'.$files[$i], 200, 200, $thumbnail, $i*200);
|
|
|
|
}
|
|
|
|
imagepng($thumbnail, OC_Config::getValue("datadirectory").'/'. OC_User::getUser() .'/gallery/' . $albumName.'.png');
|
2011-09-26 00:32:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function isPhoto($filename) {
|
|
|
|
if (substr(OC_Filesystem::getMimeType($filename), 0, 6) == "image/")
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|