2012-01-08 03:14:34 +04:00
|
|
|
<?php
|
|
|
|
|
2012-01-08 14:01:25 +04:00
|
|
|
/**
|
|
|
|
* ownCloud - gallery application
|
|
|
|
*
|
|
|
|
* @author Bartek Przybylski
|
|
|
|
* @copyright 2012 Bartek Przybylski bart.p.pl@gmail.com
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-01-15 02:12:32 +04:00
|
|
|
OC_Hook::connect(OC_Filesystem::CLASSNAME, OC_Filesystem::signal_post_write, "OC_Gallery_Hooks_Handlers", "addPhotoFromPath");
|
|
|
|
OC_Hook::connect(OC_Filesystem::CLASSNAME, OC_Filesystem::signal_delete, "OC_Gallery_Hooks_Handlers", "removePhoto");
|
|
|
|
OC_Hook::connect(OC_Filesystem::CLASSNAME, OC_Filesystem::signal_post_rename, "OC_Gallery_Hooks_Handlers", "renamePhoto");
|
2012-01-08 03:14:34 +04:00
|
|
|
|
|
|
|
require_once(OC::$CLASSPATH['OC_Gallery_Album']);
|
|
|
|
require_once(OC::$CLASSPATH['OC_Gallery_Photo']);
|
|
|
|
|
|
|
|
class OC_Gallery_Hooks_Handlers {
|
|
|
|
private static $APP_TAG = "Gallery";
|
|
|
|
|
|
|
|
private static function isPhoto($filename) {
|
2012-01-21 02:26:39 +04:00
|
|
|
$ext = strtolower(substr($filename, strrpos($filename, '.')+1));
|
|
|
|
return $ext=='png' || $ext=='jpeg' || $ext=='jpg' || $ext=='gif';
|
2012-01-14 16:52:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private static function directoryContainsPhotos($dirpath) {
|
2012-03-07 20:31:01 +04:00
|
|
|
$dirhandle = OC_Filesystem::opendir($dirpath.'/');
|
2012-01-14 16:52:24 +04:00
|
|
|
if ($dirhandle != FALSE) {
|
|
|
|
while (($filename = readdir($dirhandle)) != FALSE) {
|
|
|
|
if ($filename[0] == '.') continue;
|
|
|
|
if (self::isPhoto($dirpath.'/'.$filename)) return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2012-01-08 03:14:34 +04:00
|
|
|
}
|
|
|
|
|
2012-01-08 04:32:11 +04:00
|
|
|
private static function createAlbum($path) {
|
|
|
|
$new_album_name = trim(str_replace('/', '.', $path), '.');
|
2012-01-08 14:01:25 +04:00
|
|
|
if ($new_album_name == '')
|
|
|
|
$new_album_name = 'main';
|
2012-01-08 04:32:11 +04:00
|
|
|
|
|
|
|
OC_Log::write(self::$APP_TAG, 'Creating new album '.$new_album_name, OC_Log::DEBUG);
|
|
|
|
OC_Gallery_Album::create(OC_User::getUser(), $new_album_name, $path);
|
|
|
|
|
|
|
|
return OC_Gallery_Album::find(OC_User::getUser(), null, $path);
|
|
|
|
}
|
|
|
|
|
2012-02-04 13:30:32 +04:00
|
|
|
public static function pathInRoot($path) {
|
2012-02-04 23:15:55 +04:00
|
|
|
$root = OC_Preferences::getValue(OC_User::getUser(), 'gallery', 'root', '/');
|
2012-02-04 13:30:32 +04:00
|
|
|
return substr($path, 0, strlen($path)>strlen($root)?strlen($root):strlen($path)) == $root;
|
|
|
|
}
|
|
|
|
|
2012-01-08 03:14:34 +04:00
|
|
|
public static function addPhotoFromPath($params) {
|
2012-01-15 02:12:32 +04:00
|
|
|
$fullpath = $params[OC_Filesystem::signal_param_path];
|
|
|
|
|
|
|
|
if (!self::isPhoto($fullpath)) return;
|
|
|
|
|
2012-03-08 18:47:49 +04:00
|
|
|
$path = dirname($fullpath);
|
2012-02-04 13:30:32 +04:00
|
|
|
if (!self::pathInRoot($path)) return;
|
2012-01-26 00:07:43 +04:00
|
|
|
OC_Gallery_Scanner::scanDir($path, $albums);
|
2012-01-08 03:14:34 +04:00
|
|
|
|
|
|
|
}
|
2012-01-08 04:32:11 +04:00
|
|
|
|
|
|
|
public static function removePhoto($params) {
|
2012-01-15 02:12:32 +04:00
|
|
|
$path = $params[OC_Filesystem::signal_param_path];
|
2012-03-07 20:31:01 +04:00
|
|
|
if (OC_Filesystem::is_dir($path.'/') && self::directoryContainsPhotos($path)) {
|
2012-02-04 13:30:32 +04:00
|
|
|
if(!self::pathInRoot($path)) return;
|
2012-03-07 20:31:01 +04:00
|
|
|
OC_Gallery_Album::removeByPath($path, OC_User::getUser());
|
2012-01-14 17:59:18 +04:00
|
|
|
} elseif (self::isPhoto($path)) {
|
|
|
|
OC_Gallery_Photo::removeByPath($path);
|
|
|
|
}
|
2012-01-08 04:32:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function renamePhoto($params) {
|
2012-01-15 02:12:32 +04:00
|
|
|
$oldpath = $params[OC_Filesystem::signal_param_oldpath];
|
|
|
|
$newpath = $params[OC_Filesystem::signal_param_newpath];
|
2012-03-07 20:31:01 +04:00
|
|
|
if (OC_Filesystem::is_dir($newpath.'/') && self::directoryContainsPhotos($newpath)) {
|
2012-01-15 02:12:32 +04:00
|
|
|
OC_Gallery_Album::changePath($oldpath, $newpath, OC_User::getUser());
|
2012-03-07 20:31:01 +04:00
|
|
|
} elseif (self::isPhoto($newpath)) {
|
|
|
|
$olddir = dirname($oldpath);
|
|
|
|
$newdir = dirname($newpath);
|
2012-01-14 16:52:24 +04:00
|
|
|
if ($olddir == '') $olddir = '/';
|
|
|
|
if ($newdir == '') $newdir = '/';
|
2012-01-15 02:12:32 +04:00
|
|
|
if (!self::isPhoto($newpath)) return;
|
|
|
|
OC_Log::write(self::$APP_TAG, 'Moving photo from '.$oldpath.' to '.$newpath, OC_Log::DEBUG);
|
2012-01-14 16:52:24 +04:00
|
|
|
$album;
|
|
|
|
$newAlbumId;
|
|
|
|
$oldAlbumId;
|
|
|
|
if ($olddir == $newdir) {
|
|
|
|
// album changing is not needed
|
2012-03-07 20:31:01 +04:00
|
|
|
$albums = OC_Gallery_Album::find(OC_User::getUser(), null, $olddir);
|
|
|
|
$album = $albums->fetchRow();
|
|
|
|
if (!$album) {
|
|
|
|
$albums = self::createAlbum($newdir);
|
|
|
|
$album = $albums->fetchRow();
|
2012-01-14 16:52:24 +04:00
|
|
|
}
|
|
|
|
$newAlbumId = $oldAlbumId = $album['album_id'];
|
|
|
|
} else {
|
|
|
|
$newalbum = OC_Gallery_Album::find(OC_User::getUser(), null, $newdir);
|
|
|
|
$oldalbum = OC_Gallery_Album::find(OC_User::getUser(), null, $olddir);
|
|
|
|
|
2012-03-07 20:31:01 +04:00
|
|
|
if (!($newalbum = $newalbum->fetchRow())) {
|
2012-01-14 16:52:24 +04:00
|
|
|
$newalbum = self::createAlbum($newdir);
|
2012-03-07 20:31:01 +04:00
|
|
|
$newalbum = $newalbum->fetchRow();
|
2012-01-14 16:52:24 +04:00
|
|
|
}
|
2012-03-07 20:31:01 +04:00
|
|
|
$oldalbum = $oldalbum->fetchRow();
|
|
|
|
if (!$oldalbum) {
|
2012-01-15 02:12:32 +04:00
|
|
|
OC_Gallery_Photo::create($newalbum['album_id'], $newpath);
|
2012-01-14 16:52:24 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$newAlbumId = $newalbum['album_id'];
|
|
|
|
$oldAlbumId = $oldalbum['album_id'];
|
2012-01-08 04:32:11 +04:00
|
|
|
|
2012-01-14 16:52:24 +04:00
|
|
|
}
|
2012-01-15 02:12:32 +04:00
|
|
|
OC_Gallery_Photo::changePath($oldAlbumId, $newAlbumId, $oldpath, $newpath);
|
2012-01-08 04:32:11 +04:00
|
|
|
}
|
|
|
|
}
|
2012-01-08 03:14:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|