From b1f6b3b21e42777497a89aed347873def8bfc016 Mon Sep 17 00:00:00 2001 From: "Thomas E. Horner" Date: Mon, 20 Jun 2011 09:08:09 +0200 Subject: [PATCH 01/10] remote protocol 2 tested with galleryremote 1.5.1 --- .../remote/controllers/gallery_remote.php | 517 +++++++++++++++++- .../remote/libraries/GalleryRemoteReply.php | 25 +- 2 files changed, 519 insertions(+), 23 deletions(-) diff --git a/3.0/modules/remote/controllers/gallery_remote.php b/3.0/modules/remote/controllers/gallery_remote.php index 0ef99d4f..cc65a53e 100644 --- a/3.0/modules/remote/controllers/gallery_remote.php +++ b/3.0/modules/remote/controllers/gallery_remote.php @@ -18,28 +18,507 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class Gallery_Remote_Controller extends Controller { + private static $thumb_size = 0; + private static $resize_size = 0; + + //XXX access::required("view", $item); + public function index() { - gallery_remote::check_protocol_version(); $input = Input::instance(); - // TODO: Validate protocol version here - switch($input->post("cmd")) { - case "login": - print "#__GR2PROTO__\n"; - $uname = $input->post("uname"); - if (empty($uname)) { - print "status=202\n"; - } else { - $user = user::lookup_by_name($uname); - $password = $input->post("password"); - if ($user && user::is_correct_password($user, $password)) { - print "status=0\n"; - user::login($user); - } else { - print "status=201\n"; - } - } - print "server_version=2.15\n"; + $reply = GalleryRemoteReply::factory(gallery_remote::GR_STAT_SUCCESS); + + if($this->_check_protocol($input, $reply)) { + $reply->set('debug_gallery_version', '3.0+'); //XXX + $reply->set('debug_user', identity::active_user()->name); + $reply->set('debug_user_type', 'Gallery_User'); + $reply->set('debug_user_already_logged_in', identity::active_user()->id != identity::guest()->id ? '1':''); + $reply->set('server_version', '2.15'); + + $cmd = trim($input->post('cmd')); + if($cmd == 'login') { + $this->_login($input, $reply); + } + else if( self::isloggedin() ) { + switch($cmd) { + case 'no-op': + $reply->set('status_text', 'Noop command successful.'); + $reply->send(); + break; + case 'fetch-albums': + case 'fetch-albums-prune': + $this->_fetch_albums_prune($input, $reply); + break; + case 'new-album': + $this->_new_album($input, $reply); + break; + case 'album-properties': + $this->_album_properties($input, $reply); + break; + case 'add-item': + $this->_add_item($input, $reply); + break; + case 'move-album': + $this->_move_album($input, $reply); + break; + case 'increment-view-count': + $this->_increment_view_count($input, $reply); + break; + case 'image-properties': + $this->_image_properties($input, $reply); + break; + case 'fetch-album-images': + $this->_fetch_album_images($input, $reply); + break; + default: + $reply->send(gallery_remote::UNKNOWN_CMD); + } + } + else { + $reply->send(gallery_remote::LOGIN_MISSING); + } + } + } + + private function _check_protocol(&$input, &$reply) { + $version = trim($input->post('protocol_version')); + if($version=='') { + $reply->send(gallery_remote::PROTO_VER_MISSING); + return false; + } + else if(!is_numeric($version)) { + $reply->send(gallery_remote::PROTO_MAJ_FMT_INVAL); + return false; + } + else if($version<'2') { + $reply->send(gallery_remote::PROTO_MAJ_VER_INVAL); + return false; + } + else if($version<'2.3') { + $reply->send(gallery_remote::PROTO_MIN_VER_INVAL); + return false; + } + + return true; + } + + private static function isloggedin() + { + return identity::active_user()->id != identity::guest()->id; + } + + private static function get_mime_type($filename, $mimePath = '/etc') { + $fileext = substr(strrchr($filename, '.'), 1); + if (empty($fileext)) return (false); + $regex = "/^([\w\+\-\.\/]+)\s+(\w+\s)*($fileext\s)/i"; + $lines = file("$mimePath/mime.types"); + foreach($lines as $line) { + if (substr($line, 0, 1) == '#') continue; // skip comments + $line = rtrim($line) . ' '; + if (!preg_match($regex, $line, $matches)) continue; // no match to the extension + return ($matches[1]); } + return (false); // no match at all } + + private function _login(&$input, &$reply) { + $uname = trim($input->post('uname')); + if (empty($uname)) { + $reply->send(gallery_remote::LOGIN_MISSING); + } else { + $user = user::lookup_by_name($uname); + $password = trim($input->post('password')); + if ($user && user::is_correct_password($user, $password)) { + auth::login($user); + Session::instance()->regenerate(); + + $reply->set('debug_user', $user->name); + $reply->set('status_text', 'Login successful.'); + $reply->send(); + + } else { + $reply->send(gallery_remote::PASSWD_WRONG); + } + } + } + + private function _fetch_albums_prune(&$input, &$reply) { + $root = item::root(); + $thumb_size = module::get_var('gallery', 'thumb_size'); + $resize_size = module::get_var('gallery', 'resize_size'); + $count = 0; + foreach( $root->descendants(null, null, array(array("type", "=", "album"))) as $item ) + { + $count++; + + $reply->set('album.name.'.$count, $item->slug); + $reply->set('album.title.'.$count, $item->title); + $reply->set('album.summary.'.$count, $item->description); + $reply->set('album.parent.'.$count, $item->parent()->id == $root->id ? '0' : $item->parent()->name); + $reply->set('album.resize_size.'.$count, $resize_size); + $reply->set('album.max_size.'.$count, '0'); + $reply->set('album.thumb_size.'.$count, $thumb_size); + $reply->set('album.perms.add.'.$count, 'true'); //XXX + $reply->set('album.perms.write.'.$count, 'true'); //XXX + $reply->set('album.perms.del_item.'.$count, 'true'); //XXX + $reply->set('album.perms.del_alb.'.$count, 'true'); //XXX + $reply->set('album.perms.create_sub.'.$count, 'true'); //XXX + $reply->set('album.info.extrafields.'.$count, ''); + } + $reply->set('album_count', $count); + $reply->set('can_create_root', 'yes'); //XXX + $reply->set('status_text', 'Fetch albums successful.'); + $reply->send(); + } + + private function _new_album(&$input, &$reply) { + $album = trim($input->post('set_albumName')); + $name = trim($input->post('newAlbumName')); + $title = trim($input->post('newAlbumTitle')); + $desc = trim($input->post('newAlbumDesc')); + + if($album=='0') $parent = item::root(); + else $parent = ORM::factory("item")->where("slug", "=", $album)->find(); + + if(isset($parent) && $parent->loaded() && $parent->id!='') { + $album = ORM::factory('item'); + $album->type = 'album'; + $album->parent_id = $parent->id; + + $album->name = $name; + $album->slug = $name; // <= verification fails if this property has not been set!!! + $album->title = $title; + $album->title or $album->title = $album->name; + $album->description = $desc; + //$album->owner_id = + $album->view_count = 0; + //$album->created = $fields['clicks_date']; + $album->sort_column = 'weight'; + $album->sort_order = 'ASC'; + + try { + $album->validate(); + + try { + $album->save(); + + $reply->set('album_name', $album->name); + $reply->set('status_text', 'New album created successfuly.'); + $reply->send(); + + } catch (Exception $e) { + $reply->set('status_text', t('Failed to save album with name %name.', array('name' => $name))); + $reply->send(gallery_remote::CREATE_ALBUM_FAILED); + } + + } catch (ORM_Validation_Exception $e) { + $reply->set('status_text', t('Failed to validate album with name %name.', array('name' => $name))); + $reply->send(gallery_remote::CREATE_ALBUM_FAILED); + } + } + else { + $reply->set('status_text', t('Failed to load album with name %name.', array('name' => $album))); + $reply->send(gallery_remote::CREATE_ALBUM_FAILED); + } + } + + private function _album_properties(&$input, &$reply) { + $album = trim($input->post('set_albumName')); + $resize_size = module::get_var('gallery', 'resize_size'); + + if($album=='0') $parent = item::root(); + else $parent = ORM::factory("item")->where("slug", "=", $album)->find(); + + if(isset($parent) && $parent->loaded() && $parent->id!='') { + $reply->set('auto_resize', $resize_size); //XXX + $reply->set('max_size', '0'); //XXX + $reply->set('add_to_beginning', 'no'); //XXX + $reply->set('extrafields', ''); + $reply->set('title', $parent->title); + $reply->set('status_text', 'Album properties queried successfuly.'); + $reply->send(); + } + else { + $reply->set('status_text', t('Failed to load album with name %name.', array('name' => $album))); + $reply->send(gallery_remote::NO_VIEW_PERMISSION); + } + } + + private function _add_item(&$input, &$reply) { + $album = trim($input->post('set_albumName')); + $userfilename = trim($input->post('userfile_name')); + $title = trim($input->post('caption')); + $forcefilename = trim($input->post('force_filename')); + $autorotate = trim($input->post('auto_rotate')); + //print_r($_FILES['userfile']); exit; + + if($album=='0') $parent = item::root(); + else $parent = ORM::factory("item")->where("slug", "=", $album)->find(); + + if(isset($parent) && $parent->loaded() && $parent->id!='') { + + //* + if(function_exists('mime_content_type')) + $type = mime_content_type($_FILES['userfile']['tmp_name']); + else + $type = self::get_mime_type($_FILES['userfile']['name']); + + if ($type!='' && !in_array($type, array('image/jpeg', 'image/gif', 'image/png'))) { + $reply->set('status_text', t("'%path' is an unsupported image type '%type'", array('path' => $_FILES['userfile']['tmp_name'], 'type' => $type))); + $reply->send(gallery_remote::UPLOAD_PHOTO_FAIL); + return; + } + + if($forcefilename!='') $filename = $forcefilename; + else if($userfilename!='') $filename = $userfilename; + else $filename = $_FILES['userfile']['name']; + + $slug = $filename; + $pos = strpos($slug, '.'); + if($pos!==false) + $slug = substr($slug, 0, $pos); + + try { + $item = ORM::factory('item'); + $item->type = 'photo'; + $item->parent_id = $parent->id; + $item->set_data_file($_FILES['userfile']['tmp_name']); + $item->name = $filename; + $item->slug = $slug; + $item->mime_type = $type; + $item->title = $title; + $item->title or $item->title = ' '; //don't use $item->name as this clutters up the UI + //$item->description = + //$item->owner_id = + $item->view_count = 0; + + try { + $item->validate(); + + try { + $item->save(); + + $reply->set('item_name', $item->name); + $reply->set('status_text', 'New item created successfuly.'); + $reply->send(); + + } catch (Exception $e) { + $reply->set('status_text', t('Failed to add item %item.', array('item' => $filename))); + $reply->send(gallery_remote::UPLOAD_PHOTO_FAIL); //XXX gallery remote doesn't accept this :( + } + + } catch (ORM_Validation_Exception $e) { + $validation = $e->validation; + //print_r($validation->errors()); exit; + $reply->set('status_text', t('Failed to validate item %item: %errors', array('item' => $filename, 'errors' => print_r($validation->errors(),true)) )); + $reply->send(gallery_remote::UPLOAD_PHOTO_FAIL); //XXX gallery remote doesn't accept this :( + } + + } catch (Exception $e) { + $reply->set('status_text', t("Corrupt image '%path'", array('path' => $_FILES['userfile']['tmp_name']))); + $reply->send(gallery_remote::UPLOAD_PHOTO_FAIL); //XXX gallery remote doesn't accept this :( + } + + } + else { + $reply->set('status_text', t('Failed to load album with name %name.', array('name' => $album))); + $reply->send(gallery_remote::UPLOAD_PHOTO_FAIL); //XXX gallery remote doesn't accept this :( + } + } + + private function _move_album(&$input, &$reply) { + $name = trim($input->post('set_albumName')); + $destination = trim($input->post('set_destalbumName')); + + $album = ORM::factory("item")->where("slug", "=", $name)->find(); + + if($destination=='0') $parent = item::root(); + else $parent = ORM::factory("item")->where("slug", "=", $destination)->find(); + + if(isset($parent) && $parent->loaded() && $parent->id!='' && isset($album) && $album->loaded() && $album->id!='') { + + $album->parent_id = $parent->id; + try { + $album->validate(); + + try { + $album->save(); + + $reply->set('status_text', 'Album moved successfuly.'); + $reply->send(); + + } catch (Exception $e) { + $reply->set('status_text', t('Failed to save album with name %name.', array('name' => $name))); + $reply->send(gallery_remote::MOVE_ALBUM_FAILED); + } + + } catch (ORM_Validation_Exception $e) { + $reply->set('status_text', t('Failed to validate album with name %name.', array('name' => $name))); + $reply->send(gallery_remote::MOVE_ALBUM_FAILED); + } + } + else { + $reply->set('status_text', t('Failed to load album with name %album or destination with name %dest.', array('name' => $name, 'dest' => $destination))); + $reply->send(gallery_remote::MOVE_ALBUM_FAILED); + } + } + + private function _increment_view_count(&$input, &$reply) { + $name = trim($input->post('itemId')); + + if($name=='0') $item = item::root(); + else $item = ORM::factory("item")->where("slug", "=", $name)->find(); + + if(isset($item) && $item->loaded() && $item->id!='') { + + $item->view_count = $item->view_count + 1; + + try { + $item->validate(); + + try { + $item->save(); + + $reply->set('item_name', $item->name); + $reply->set('status_text', 'Item view count incremented successfuly.'); + $reply->send(); + + } catch (Exception $e) { + $reply->set('status_text', t('Failed to save item %item.', array('item' => $name))); + $reply->send(gallery_remote::NO_WRITE_PERMISSION); + } + + } catch (ORM_Validation_Exception $e) { + $validation = $e->validation; + //print_r($validation->errors()); exit; + $reply->set('status_text', t('Failed to validate item %item.', array('item' => $name)).print_r($validation->errors(),true)); + $reply->send(gallery_remote::NO_WRITE_PERMISSION); + } + + } + else { + $reply->set('status_text', t('Failed to load album with name %name.', array('name' => $name))); + $reply->send(gallery_remote::NO_WRITE_PERMISSION); + } + } + + private function _image_properties(&$input, &$reply) { + $name = trim($input->post('itemId')); + + if($name=='0') $item = item::root(); + else $item = ORM::factory("item")->where("slug", "=", $name)->find(); + + if(isset($item) && $item->loaded() && $item->id!='') { + $reply->set('status_text', 'Item properties queried successfuly.'); + + $reply->set('image.name', $item->slug); + $reply->set('image.raw_width', $item->width); + $reply->set('image.raw_height', $item->height); + $reply->set('image.raw_filesize', filesize($item->file_path())); + $reply->set('image.resizedName', $item->name); //g3 stores resizes and thumbs different than g1 + $reply->set('image.resized_width', $item->resize_width); + $reply->set('image.resized_height', $item->resize_height); + $reply->set('image.thumbName', $item->name); //g3 stores resizes and thumbs different than g1 + $reply->set('image.thumb_width', $item->thumb_width); + $reply->set('image.thumb_height', $item->thumb_height); + $reply->set('image.caption', $item->title); + $reply->set('image.title', $item->title); + //XXX $reply->set('image.forceExtension', ''); + $reply->set('image.hidden', 'no'); //XXX + $reply->send(); + } + else { + $reply->set('status_text', t('Failed to load album with name %name.', array('name' => $name))); + $reply->send(gallery_remote::NO_VIEW_PERMISSION); + } + } + + private function _fetch_album_images(&$input, &$reply) { + $name = trim($input->post('set_albumName')); + $albums = trim($input->post('albums_too')); //yes/no [optional, since 2.13] + $random = trim($input->post('random')); //yes/no [optional, G2 since ***] + $limit = trim($input->post('limit')); //number-of-images [optional, G2 since ***] + $fields = trim($input->post('extrafields')); //yes/no [optional, G2 since 2.12] + $sizes = trim($input->post('all_sizes')); //yes/no [optional, G2 since 2.14] + + if($name=='0') $album = item::root(); + $album = ORM::factory("item")->where("slug", "=", $name)->find(); + + if(isset($album) && $album->loaded() && $album->id!='') { + + if($albums!='no') $iterator = ORM::factory("item")->where("parent_id", "=", $album->id)->find_all(); + else $iterator = ORM::factory("item")->where("parent_id", "=", $album->id)->where("type", "<>", "album")->find_all(); + + $reply->set('status_text', 'Album images query successful.'); + $reply->set('album.caption', $album->title); + $reply->set('album.extrafields', ''); + + /* + $reply->set('image_count', '0'); + $reply->send(); + return; + //*/ + + $count = 0; + foreach($iterator as $item) { + + $count++; + if($item->type != "album") { + $reply->set('image.name.'.$count, $item->name); + //$reply->set('image', print_r($item, true)); + $reply->set('image.raw_width.'.$count, $item->width); + $reply->set('image.raw_height.'.$count, $item->height); + $reply->set('image.raw_filesize.'.$count, filesize($item->file_path())); + $reply->set('image.resizedName.'.$count, $item->name); //g3 stores resizes and thumbs different than g1 + $reply->set('image.resized_width.'.$count, $item->resize_width); + $reply->set('image.resized_height.'.$count, $item->resize_height); + //$reply->set('image.resizedNum.'.$count, 'the number of resized versions for this image [since 2.14]'); + //$reply->set('image.resized.resized-num.name.'.$count, 'filename of the resized-numth resize [G2 since 2.14]'); + //$reply->set('image.resized.resized-num.width.'.$count, 'the width of the resized-numth resize [G2 since 2.14]'); + //$reply->set('image.resized.resized-num.height.'.$count, 'the height of the resized-numth resize [G2 since 2.14]'); + $reply->set('image.thumbName.'.$count, $item->name); //g3 stores resizes and thumbs different than g1 + $reply->set('image.thumb_width.'.$count, $item->thumb_width); + $reply->set('image.thumb_height.'.$count, $item->thumb_height); + + $reply->set('image.caption.'.$count, $item->title); + $reply->set('image.title.'.$count, $item->title); + //$reply->set('image.extrafield.fieldname.'.$count, 'value of the extra field of key fieldname'); + $reply->set('image.clicks.'.$count, $item->view_count); + //* + $reply->set('image.capturedate.year.'.$count, date("Y", $item->captured)); + $reply->set('image.capturedate.mon.'.$count, date("m", $item->captured)); + $reply->set('image.capturedate.mday.'.$count, date("d", $item->captured)); + $reply->set('image.capturedate.hours.'.$count, date("H", $item->captured)); + $reply->set('image.capturedate.minutes.'.$count, date("i", $item->captured)); + $reply->set('image.capturedate.seconds.'.$count, date("s", $item->captured)); + //*/ + //XXX $reply->set('image.forceExtension.'.$count, ''); + $reply->set('image.hidden.'.$count, 'no'); //XXX + } + else { + $reply->set('album.name.'.$count, $item->name); + } + } + + $reply->set('image_count', $count); + //* The baseurl contains a fully-qualified URL. A URL to each image + // can be obtained by appending the filename of the image to this. + if(isset($item) && $item->loaded()) { + $url = $item->file_url(true); + $pos = strrpos($url, '/'); + $reply->set('baseurl', ($pos!==false ? substr($url, 0, $pos+1) : $url) ); + } + else { + $reply->set('baseurl', $album->abs_url()); + } + //*/ + $reply->send(); + + } + else { + $reply->set('status_text', t('Failed to load album with name %name.', array('name' => $name))); + $reply->send(gallery_remote::NO_VIEW_PERMISSION); + } + } + } diff --git a/3.0/modules/remote/libraries/GalleryRemoteReply.php b/3.0/modules/remote/libraries/GalleryRemoteReply.php index 3896ed09..7ad5781f 100644 --- a/3.0/modules/remote/libraries/GalleryRemoteReply.php +++ b/3.0/modules/remote/libraries/GalleryRemoteReply.php @@ -19,22 +19,39 @@ */ class GalleryRemoteReply_Core { + private $values = array(); + private $nl = "\n"; /** * Constructor. * @param int $status a Gallery Remote status code */ - public static function factory($status) { + public static function factory($status='') { $reply = new GalleryRemoteReply(); - $reply->status = $status; + $reply->set('status', $status); + $reply->set('status_text', ''); return $reply; } + public function clear() { + $this->values = array(); + } + /** * Set a property on this reply * @chainable */ - public static function set($key, $value) { - $this->$key = $value; + public function set($key, $value) { + $this->values[$key] = $value; return $this; } + + public function send($status='') { + if($status!='') $reply->set('status', $status); + //ksort($this->values); + + echo '#__GR2PROTO__'.$this->nl; + foreach($this->values as $key => $value) { + echo $key.'='.$value.$this->nl; + } + } } From 3e59a5f933dc782476377a5e69bbd914829da282 Mon Sep 17 00:00:00 2001 From: "Thomas E. Horner" Date: Mon, 20 Jun 2011 11:09:39 +0200 Subject: [PATCH 02/10] now actually querying the access system for permissions --- .../remote/controllers/gallery_remote.php | 725 +++++++++--------- 1 file changed, 365 insertions(+), 360 deletions(-) diff --git a/3.0/modules/remote/controllers/gallery_remote.php b/3.0/modules/remote/controllers/gallery_remote.php index cc65a53e..fce7d88e 100644 --- a/3.0/modules/remote/controllers/gallery_remote.php +++ b/3.0/modules/remote/controllers/gallery_remote.php @@ -18,94 +18,89 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class Gallery_Remote_Controller extends Controller { - private static $thumb_size = 0; - private static $resize_size = 0; - - //XXX access::required("view", $item); - public function index() { $input = Input::instance(); $reply = GalleryRemoteReply::factory(gallery_remote::GR_STAT_SUCCESS); - if($this->_check_protocol($input, $reply)) { - $reply->set('debug_gallery_version', '3.0+'); //XXX - $reply->set('debug_user', identity::active_user()->name); + if($this->_check_protocol($input, $reply)) { + $reply->set('debug_gallery_version', gallery::version_string()); + $reply->set('debug_user', identity::active_user()->name); $reply->set('debug_user_type', 'Gallery_User'); $reply->set('debug_user_already_logged_in', identity::active_user()->id != identity::guest()->id ? '1':''); - $reply->set('server_version', '2.15'); + $reply->set('server_version', '2.15'); - $cmd = trim($input->post('cmd')); - if($cmd == 'login') { - $this->_login($input, $reply); - } - else if( self::isloggedin() ) { - switch($cmd) { - case 'no-op': - $reply->set('status_text', 'Noop command successful.'); - $reply->send(); - break; - case 'fetch-albums': - case 'fetch-albums-prune': - $this->_fetch_albums_prune($input, $reply); - break; - case 'new-album': - $this->_new_album($input, $reply); - break; - case 'album-properties': - $this->_album_properties($input, $reply); - break; - case 'add-item': - $this->_add_item($input, $reply); - break; - case 'move-album': - $this->_move_album($input, $reply); - break; - case 'increment-view-count': - $this->_increment_view_count($input, $reply); - break; - case 'image-properties': - $this->_image_properties($input, $reply); - break; - case 'fetch-album-images': - $this->_fetch_album_images($input, $reply); - break; - default: - $reply->send(gallery_remote::UNKNOWN_CMD); - } - } - else { - $reply->send(gallery_remote::LOGIN_MISSING); - } - } - } + $cmd = trim($input->post('cmd')); + if($cmd == 'login') { + $this->_login($input, $reply); + } + else if( self::isloggedin() ) { + switch($cmd) { + case 'no-op': + $reply->set('status_text', 'Noop command successful.'); + $reply->send(); + break; + case 'fetch-albums': + case 'fetch-albums-prune': + $this->_fetch_albums_prune($input, $reply); + break; + case 'new-album': + $this->_new_album($input, $reply); + break; + case 'album-properties': + $this->_album_properties($input, $reply); + break; + case 'add-item': + $this->_add_item($input, $reply); + break; + case 'move-album': + $this->_move_album($input, $reply); + break; + case 'increment-view-count': + $this->_increment_view_count($input, $reply); + break; + case 'image-properties': + $this->_image_properties($input, $reply); + break; + case 'fetch-album-images': + $this->_fetch_album_images($input, $reply); + break; + default: + $reply->send(gallery_remote::UNKNOWN_CMD); + } + } + else { + $reply->send(gallery_remote::LOGIN_MISSING); + } + } + } - private function _check_protocol(&$input, &$reply) { - $version = trim($input->post('protocol_version')); - if($version=='') { + private function _check_protocol(&$input, &$reply) { + $version = trim($input->post('protocol_version')); + if($version=='') { $reply->send(gallery_remote::PROTO_VER_MISSING); return false; - } - else if(!is_numeric($version)) { + } + else if(!is_numeric($version)) { $reply->send(gallery_remote::PROTO_MAJ_FMT_INVAL); return false; - } - else if($version<'2') { + } + else if($version<'2') { $reply->send(gallery_remote::PROTO_MAJ_VER_INVAL); return false; - } - else if($version<'2.3') { + } + else if($version<'2.3') { $reply->send(gallery_remote::PROTO_MIN_VER_INVAL); return false; - } + } - return true; - } + return true; + } - private static function isloggedin() - { - return identity::active_user()->id != identity::guest()->id; - } + private static function isloggedin() + { + return identity::active_user()->id != identity::guest()->id; + } private static function get_mime_type($filename, $mimePath = '/etc') { $fileext = substr(strrchr($filename, '.'), 1); @@ -121,162 +116,168 @@ class Gallery_Remote_Controller extends Controller { return (false); // no match at all } - private function _login(&$input, &$reply) { + private function _login(&$input, &$reply) { $uname = trim($input->post('uname')); if (empty($uname)) { - $reply->send(gallery_remote::LOGIN_MISSING); - } else { + $reply->send(gallery_remote::LOGIN_MISSING); + } else { $user = user::lookup_by_name($uname); $password = trim($input->post('password')); - if ($user && user::is_correct_password($user, $password)) { - auth::login($user); - Session::instance()->regenerate(); + if ($user && user::is_correct_password($user, $password)) { + auth::login($user); + Session::instance()->regenerate(); - $reply->set('debug_user', $user->name); - $reply->set('status_text', 'Login successful.'); - $reply->send(); - - } else { - $reply->send(gallery_remote::PASSWD_WRONG); - } - } - } + $reply->set('debug_user', $user->name); + $reply->set('status_text', 'Login successful.'); + $reply->send(); + + } else { + $reply->send(gallery_remote::PASSWD_WRONG); + } + } + } - private function _fetch_albums_prune(&$input, &$reply) { - $root = item::root(); - $thumb_size = module::get_var('gallery', 'thumb_size'); - $resize_size = module::get_var('gallery', 'resize_size'); - $count = 0; - foreach( $root->descendants(null, null, array(array("type", "=", "album"))) as $item ) - { - $count++; - - $reply->set('album.name.'.$count, $item->slug); - $reply->set('album.title.'.$count, $item->title); - $reply->set('album.summary.'.$count, $item->description); - $reply->set('album.parent.'.$count, $item->parent()->id == $root->id ? '0' : $item->parent()->name); - $reply->set('album.resize_size.'.$count, $resize_size); - $reply->set('album.max_size.'.$count, '0'); - $reply->set('album.thumb_size.'.$count, $thumb_size); - $reply->set('album.perms.add.'.$count, 'true'); //XXX - $reply->set('album.perms.write.'.$count, 'true'); //XXX - $reply->set('album.perms.del_item.'.$count, 'true'); //XXX - $reply->set('album.perms.del_alb.'.$count, 'true'); //XXX - $reply->set('album.perms.create_sub.'.$count, 'true'); //XXX - $reply->set('album.info.extrafields.'.$count, ''); - } - $reply->set('album_count', $count); - $reply->set('can_create_root', 'yes'); //XXX - $reply->set('status_text', 'Fetch albums successful.'); - $reply->send(); - } - - private function _new_album(&$input, &$reply) { - $album = trim($input->post('set_albumName')); - $name = trim($input->post('newAlbumName')); - $title = trim($input->post('newAlbumTitle')); - $desc = trim($input->post('newAlbumDesc')); + private function _fetch_albums_prune(&$input, &$reply) { + $root = item::root(); + $perms = trim($input->post('no_perms')); + $use_permissions = ($perms != 'no'); + + $thumb_size = module::get_var('gallery', 'thumb_size'); + $resize_size = module::get_var('gallery', 'resize_size'); + $count = 0; + foreach( $root->descendants(null, null, array(array("type", "=", "album"))) as $item ) + { + if(!$use_permissions || access::can('view', $item)) + { + $count++; + + $reply->set('album.name.'.$count, $item->slug); + $reply->set('album.title.'.$count, $item->title); + $reply->set('album.summary.'.$count, $item->description); + $reply->set('album.parent.'.$count, $item->parent()->id == $root->id ? '0' : $item->parent()->name); + $reply->set('album.resize_size.'.$count, $resize_size); + $reply->set('album.max_size.'.$count, '0'); + $reply->set('album.thumb_size.'.$count, $thumb_size); + if($use_permissions) { + $reply->set('album.perms.add.'.$count, access::can('add', $item) ? 'true':'false'); + $reply->set('album.perms.write.'.$count, access::can('add', $item) ? 'true':'false'); + $reply->set('album.perms.del_item.'.$count, access::can('edit', $item) ? 'true':'false'); + $reply->set('album.perms.del_alb.'.$count, access::can('edit', $item) ? 'true':'false'); + $reply->set('album.perms.create_sub.'.$count, access::can('add', $item) ? 'true':'false'); + } + $reply->set('album.info.extrafields.'.$count, ''); + } + } + $reply->set('album_count', $count); + if($use_permissions) { + $reply->set('can_create_root', access::can('add', $root) ? 'yes':'no'); + } + $reply->set('status_text', 'Fetch albums successful.'); + $reply->send(); + } + + private function _new_album(&$input, &$reply) { + $album = trim($input->post('set_albumName')); + $name = trim($input->post('newAlbumName')); + $title = trim($input->post('newAlbumTitle')); + $desc = trim($input->post('newAlbumDesc')); - if($album=='0') $parent = item::root(); + if($album=='0') $parent = item::root(); else $parent = ORM::factory("item")->where("slug", "=", $album)->find(); if(isset($parent) && $parent->loaded() && $parent->id!='') { - $album = ORM::factory('item'); - $album->type = 'album'; - $album->parent_id = $parent->id; + $album = ORM::factory('item'); + $album->type = 'album'; + $album->parent_id = $parent->id; - $album->name = $name; - $album->slug = $name; // <= verification fails if this property has not been set!!! - $album->title = $title; - $album->title or $album->title = $album->name; - $album->description = $desc; - //$album->owner_id = - $album->view_count = 0; - //$album->created = $fields['clicks_date']; - $album->sort_column = 'weight'; - $album->sort_order = 'ASC'; + $album->name = $name; + $album->slug = item::convert_filename_to_slug($name); // <= verification fails if this property has not been set!!! + $album->title = $title; + $album->title or $album->title = $album->name; + $album->description = $desc; + $album->view_count = 0; + $album->sort_column = 'weight'; + $album->sort_order = 'ASC'; - try { - $album->validate(); + try { + $album->validate(); - try { - $album->save(); + try { + $album->save(); - $reply->set('album_name', $album->name); - $reply->set('status_text', 'New album created successfuly.'); - $reply->send(); + $reply->set('album_name', $album->name); + $reply->set('status_text', 'New album created successfuly.'); + $reply->send(); - } catch (Exception $e) { - $reply->set('status_text', t('Failed to save album with name %name.', array('name' => $name))); - $reply->send(gallery_remote::CREATE_ALBUM_FAILED); - } + } catch (Exception $e) { + $reply->set('status_text', t('Failed to save album with name %name.', array('name' => $name))); + $reply->send(gallery_remote::CREATE_ALBUM_FAILED); + } - } catch (ORM_Validation_Exception $e) { - $reply->set('status_text', t('Failed to validate album with name %name.', array('name' => $name))); - $reply->send(gallery_remote::CREATE_ALBUM_FAILED); - } - } - else { - $reply->set('status_text', t('Failed to load album with name %name.', array('name' => $album))); - $reply->send(gallery_remote::CREATE_ALBUM_FAILED); - } - } - - private function _album_properties(&$input, &$reply) { - $album = trim($input->post('set_albumName')); - $resize_size = module::get_var('gallery', 'resize_size'); + } catch (ORM_Validation_Exception $e) { + $reply->set('status_text', t('Failed to validate album with name %name.', array('name' => $name))); + $reply->send(gallery_remote::CREATE_ALBUM_FAILED); + } + } + else { + $reply->set('status_text', t('Failed to load album with name %name.', array('name' => $album))); + $reply->send(gallery_remote::CREATE_ALBUM_FAILED); + } + } + + private function _album_properties(&$input, &$reply) { + $album = trim($input->post('set_albumName')); + $resize_size = module::get_var('gallery', 'resize_size'); - if($album=='0') $parent = item::root(); + if($album=='0') $parent = item::root(); else $parent = ORM::factory("item")->where("slug", "=", $album)->find(); - if(isset($parent) && $parent->loaded() && $parent->id!='') { - $reply->set('auto_resize', $resize_size); //XXX - $reply->set('max_size', '0'); //XXX - $reply->set('add_to_beginning', 'no'); //XXX - $reply->set('extrafields', ''); - $reply->set('title', $parent->title); - $reply->set('status_text', 'Album properties queried successfuly.'); - $reply->send(); - } - else { - $reply->set('status_text', t('Failed to load album with name %name.', array('name' => $album))); - $reply->send(gallery_remote::NO_VIEW_PERMISSION); - } - } - - private function _add_item(&$input, &$reply) { - $album = trim($input->post('set_albumName')); - $userfilename = trim($input->post('userfile_name')); - $title = trim($input->post('caption')); - $forcefilename = trim($input->post('force_filename')); - $autorotate = trim($input->post('auto_rotate')); - //print_r($_FILES['userfile']); exit; + if(isset($parent) && $parent->loaded() && $parent->id!='') { + $reply->set('auto_resize', $resize_size); //resize size is the same for all g3 albums + $reply->set('max_size', '0'); //not supported by g3 + $reply->set('add_to_beginning', 'no'); //g3 will add images to the end + $reply->set('extrafields', ''); + $reply->set('title', $parent->title); + $reply->set('status_text', 'Album properties queried successfuly.'); + $reply->send(); + } + else { + $reply->set('status_text', t('Failed to load album with name %name.', array('name' => $album))); + $reply->send(gallery_remote::NO_VIEW_PERMISSION); + } + } + + private function _add_item(&$input, &$reply) { + $album = trim($input->post('set_albumName')); + $userfilename = trim($input->post('userfile_name')); + $title = trim($input->post('caption')); + $forcefilename = trim($input->post('force_filename')); + $autorotate = trim($input->post('auto_rotate')); - if($album=='0') $parent = item::root(); + if($album=='0') $parent = item::root(); else $parent = ORM::factory("item")->where("slug", "=", $album)->find(); if(isset($parent) && $parent->loaded() && $parent->id!='') { - //* if(function_exists('mime_content_type')) $type = mime_content_type($_FILES['userfile']['tmp_name']); else $type = self::get_mime_type($_FILES['userfile']['name']); if ($type!='' && !in_array($type, array('image/jpeg', 'image/gif', 'image/png'))) { - $reply->set('status_text', t("'%path' is an unsupported image type '%type'", array('path' => $_FILES['userfile']['tmp_name'], 'type' => $type))); - $reply->send(gallery_remote::UPLOAD_PHOTO_FAIL); - return; + $reply->set('status_text', t("'%path' is an unsupported image type '%type'", array('path' => $_FILES['userfile']['tmp_name'], 'type' => $type))); + $reply->send(gallery_remote::UPLOAD_PHOTO_FAIL); + return; } - if($forcefilename!='') $filename = $forcefilename; - else if($userfilename!='') $filename = $userfilename; - else $filename = $_FILES['userfile']['name']; + if($forcefilename!='') $filename = $forcefilename; + else if($userfilename!='') $filename = $userfilename; + else $filename = $_FILES['userfile']['name']; - $slug = $filename; - $pos = strpos($slug, '.'); - if($pos!==false) - $slug = substr($slug, 0, $pos); + $slug = $filename; + $pos = strpos($slug, '.'); + if($pos!==false) + $slug = substr($slug, 0, $pos); try { $item = ORM::factory('item'); @@ -284,134 +285,134 @@ class Gallery_Remote_Controller extends Controller { $item->parent_id = $parent->id; $item->set_data_file($_FILES['userfile']['tmp_name']); $item->name = $filename; - $item->slug = $slug; + $item->slug = item::convert_filename_to_slug($slug); $item->mime_type = $type; $item->title = $title; $item->title or $item->title = ' '; //don't use $item->name as this clutters up the UI //$item->description = - //$item->owner_id = $item->view_count = 0; - try { - $item->validate(); + try { + $item->validate(); - try { - $item->save(); + try { + $item->save(); - $reply->set('item_name', $item->name); - $reply->set('status_text', 'New item created successfuly.'); - $reply->send(); + $reply->set('item_name', $item->name); + $reply->set('status_text', 'New item created successfuly.'); + $reply->send(); - } catch (Exception $e) { - $reply->set('status_text', t('Failed to add item %item.', array('item' => $filename))); - $reply->send(gallery_remote::UPLOAD_PHOTO_FAIL); //XXX gallery remote doesn't accept this :( - } + } catch (Exception $e) { + $reply->set('status_text', t('Failed to add item %item.', array('item' => $filename))); + $reply->send(gallery_remote::UPLOAD_PHOTO_FAIL); //FIXME gallery remote ignores this return value and continues to wait + } - } catch (ORM_Validation_Exception $e) { - $validation = $e->validation; - //print_r($validation->errors()); exit; - $reply->set('status_text', t('Failed to validate item %item: %errors', array('item' => $filename, 'errors' => print_r($validation->errors(),true)) )); - $reply->send(gallery_remote::UPLOAD_PHOTO_FAIL); //XXX gallery remote doesn't accept this :( - } + } catch (ORM_Validation_Exception $e) { + $validation = $e->validation; + //print_r($validation->errors()); exit; + $reply->set('status_text', t('Failed to validate item %item: %errors', array('item' => $filename, 'errors' => print_r($validation->errors(),true)) )); + $reply->send(gallery_remote::UPLOAD_PHOTO_FAIL); //FIXME gallery remote ignores this return value and continues to wait + } } catch (Exception $e) { - $reply->set('status_text', t("Corrupt image '%path'", array('path' => $_FILES['userfile']['tmp_name']))); - $reply->send(gallery_remote::UPLOAD_PHOTO_FAIL); //XXX gallery remote doesn't accept this :( + $reply->set('status_text', t("Corrupt image '%path'", array('path' => $_FILES['userfile']['tmp_name']))); + $reply->send(gallery_remote::UPLOAD_PHOTO_FAIL); //FIXME gallery remote ignores this return value and continues to wait } - } - else { - $reply->set('status_text', t('Failed to load album with name %name.', array('name' => $album))); - $reply->send(gallery_remote::UPLOAD_PHOTO_FAIL); //XXX gallery remote doesn't accept this :( - } - } + } + else { + $reply->set('status_text', t('Failed to load album with name %name.', array('name' => $album))); + $reply->send(gallery_remote::UPLOAD_PHOTO_FAIL); //FIXME gallery remote ignores this return value and continues to wait + } + } - private function _move_album(&$input, &$reply) { - $name = trim($input->post('set_albumName')); - $destination = trim($input->post('set_destalbumName')); + private function _move_album(&$input, &$reply) { + $name = trim($input->post('set_albumName')); + $destination = trim($input->post('set_destalbumName')); - $album = ORM::factory("item")->where("slug", "=", $name)->find(); + $album = ORM::factory("item")->where("slug", "=", $name)->find(); - if($destination=='0') $parent = item::root(); + if($destination=='0') $parent = item::root(); else $parent = ORM::factory("item")->where("slug", "=", $destination)->find(); if(isset($parent) && $parent->loaded() && $parent->id!='' && isset($album) && $album->loaded() && $album->id!='') { - - $album->parent_id = $parent->id; - try { - $album->validate(); + + $album->parent_id = $parent->id; + try { + $album->validate(); - try { - $album->save(); + try { + $album->save(); - $reply->set('status_text', 'Album moved successfuly.'); - $reply->send(); + $reply->set('status_text', 'Album moved successfuly.'); + $reply->send(); - } catch (Exception $e) { - $reply->set('status_text', t('Failed to save album with name %name.', array('name' => $name))); - $reply->send(gallery_remote::MOVE_ALBUM_FAILED); - } + } catch (Exception $e) { + $reply->set('status_text', t('Failed to save album with name %name.', array('name' => $name))); + $reply->send(gallery_remote::MOVE_ALBUM_FAILED); + } - } catch (ORM_Validation_Exception $e) { - $reply->set('status_text', t('Failed to validate album with name %name.', array('name' => $name))); - $reply->send(gallery_remote::MOVE_ALBUM_FAILED); - } - } - else { - $reply->set('status_text', t('Failed to load album with name %album or destination with name %dest.', array('name' => $name, 'dest' => $destination))); - $reply->send(gallery_remote::MOVE_ALBUM_FAILED); - } - } + } catch (ORM_Validation_Exception $e) { + $reply->set('status_text', t('Failed to validate album with name %name.', array('name' => $name))); + $reply->send(gallery_remote::MOVE_ALBUM_FAILED); + } + } + else { + $reply->set('status_text', t('Failed to load album with name %album or destination with name %dest.', array('name' => $name, 'dest' => $destination))); + $reply->send(gallery_remote::MOVE_ALBUM_FAILED); + } + } - private function _increment_view_count(&$input, &$reply) { - $name = trim($input->post('itemId')); + private function _increment_view_count(&$input, &$reply) { + $name = trim($input->post('itemId')); - if($name=='0') $item = item::root(); + if($name=='0') $item = item::root(); else $item = ORM::factory("item")->where("slug", "=", $name)->find(); - if(isset($item) && $item->loaded() && $item->id!='') { + if(isset($item) && $item->loaded() && $item->id!='') { $item->view_count = $item->view_count + 1; - try { - $item->validate(); + try { + $item->validate(); - try { - $item->save(); + try { + $item->save(); - $reply->set('item_name', $item->name); - $reply->set('status_text', 'Item view count incremented successfuly.'); - $reply->send(); + $reply->set('item_name', $item->name); + $reply->set('status_text', 'Item view count incremented successfuly.'); + $reply->send(); - } catch (Exception $e) { - $reply->set('status_text', t('Failed to save item %item.', array('item' => $name))); - $reply->send(gallery_remote::NO_WRITE_PERMISSION); - } + } catch (Exception $e) { + $reply->set('status_text', t('Failed to save item %item.', array('item' => $name))); + $reply->send(gallery_remote::NO_WRITE_PERMISSION); + } - } catch (ORM_Validation_Exception $e) { - $validation = $e->validation; - //print_r($validation->errors()); exit; - $reply->set('status_text', t('Failed to validate item %item.', array('item' => $name)).print_r($validation->errors(),true)); - $reply->send(gallery_remote::NO_WRITE_PERMISSION); - } + } catch (ORM_Validation_Exception $e) { + $validation = $e->validation; + //print_r($validation->errors()); exit; + $reply->set('status_text', t('Failed to validate item %item.', array('item' => $name)).print_r($validation->errors(),true)); + $reply->send(gallery_remote::NO_WRITE_PERMISSION); + } - } - else { - $reply->set('status_text', t('Failed to load album with name %name.', array('name' => $name))); - $reply->send(gallery_remote::NO_WRITE_PERMISSION); - } - } + } + else { + $reply->set('status_text', t('Failed to load album with name %name.', array('name' => $name))); + $reply->send(gallery_remote::NO_WRITE_PERMISSION); + } + } - private function _image_properties(&$input, &$reply) { - $name = trim($input->post('itemId')); + private function _image_properties(&$input, &$reply) { + $name = trim($input->post('itemId')); - if($name=='0') $item = item::root(); + if($name=='0') $item = item::root(); else $item = ORM::factory("item")->where("slug", "=", $name)->find(); - if(isset($item) && $item->loaded() && $item->id!='') { - $reply->set('status_text', 'Item properties queried successfuly.'); + if(isset($item) && $item->loaded() && $item->id!='') { + $info = pathinfo($item->file_path()); - $reply->set('image.name', $item->slug); + $reply->set('status_text', 'Item properties queried successfuly.'); + $reply->set('image.name', $item->slug); $reply->set('image.raw_width', $item->width); $reply->set('image.raw_height', $item->height); $reply->set('image.raw_filesize', filesize($item->file_path())); @@ -421,104 +422,108 @@ class Gallery_Remote_Controller extends Controller { $reply->set('image.thumbName', $item->name); //g3 stores resizes and thumbs different than g1 $reply->set('image.thumb_width', $item->thumb_width); $reply->set('image.thumb_height', $item->thumb_height); - $reply->set('image.caption', $item->title); - $reply->set('image.title', $item->title); - //XXX $reply->set('image.forceExtension', ''); - $reply->set('image.hidden', 'no'); //XXX - $reply->send(); - } - else { - $reply->set('status_text', t('Failed to load album with name %name.', array('name' => $name))); - $reply->send(gallery_remote::NO_VIEW_PERMISSION); - } - } + $reply->set('image.caption', $item->title); + $reply->set('image.title', $item->title); + $reply->set('image.forceExtension', $info['extension']); + $reply->set('image.hidden', access::user_can(identity::guest(), 'view', $item) ? 'no' : 'yes'); + $reply->send(); + } + else { + $reply->set('status_text', t('Failed to load album with name %name.', array('name' => $name))); + $reply->send(gallery_remote::NO_VIEW_PERMISSION); + } + } - private function _fetch_album_images(&$input, &$reply) { - $name = trim($input->post('set_albumName')); - $albums = trim($input->post('albums_too')); //yes/no [optional, since 2.13] - $random = trim($input->post('random')); //yes/no [optional, G2 since ***] - $limit = trim($input->post('limit')); //number-of-images [optional, G2 since ***] - $fields = trim($input->post('extrafields')); //yes/no [optional, G2 since 2.12] - $sizes = trim($input->post('all_sizes')); //yes/no [optional, G2 since 2.14] + private function _fetch_album_images(&$input, &$reply) { + $name = trim($input->post('set_albumName')); + $albums = trim($input->post('albums_too')); //yes/no [optional, since 2.13] + $random = trim($input->post('random')); //yes/no [optional, G2 since ***] + $limit = trim($input->post('limit')); //number-of-images [optional, G2 since ***] + $fields = trim($input->post('extrafields')); //yes/no [optional, G2 since 2.12] + $sizes = trim($input->post('all_sizes')); //yes/no [optional, G2 since 2.14] - if($name=='0') $album = item::root(); - $album = ORM::factory("item")->where("slug", "=", $name)->find(); + if($name=='0') $album = item::root(); + $album = ORM::factory("item")->where("slug", "=", $name)->find(); - if(isset($album) && $album->loaded() && $album->id!='') { - - if($albums!='no') $iterator = ORM::factory("item")->where("parent_id", "=", $album->id)->find_all(); - else $iterator = ORM::factory("item")->where("parent_id", "=", $album->id)->where("type", "<>", "album")->find_all(); + if(isset($album) && $album->loaded() && $album->id!='' && access::can('view', $album)) { + + if($albums!='no') $iterator = ORM::factory("item")->where("parent_id", "=", $album->id)->find_all(); + else $iterator = ORM::factory("item")->where("parent_id", "=", $album->id)->where("type", "<>", "album")->find_all(); $reply->set('status_text', 'Album images query successful.'); $reply->set('album.caption', $album->title); $reply->set('album.extrafields', ''); - /* + /* $reply->set('image_count', '0'); $reply->send(); return; //*/ - $count = 0; - foreach($iterator as $item) { + $count = 0; + foreach($iterator as $item) { - $count++; - if($item->type != "album") { - $reply->set('image.name.'.$count, $item->name); - //$reply->set('image', print_r($item, true)); - $reply->set('image.raw_width.'.$count, $item->width); - $reply->set('image.raw_height.'.$count, $item->height); - $reply->set('image.raw_filesize.'.$count, filesize($item->file_path())); - $reply->set('image.resizedName.'.$count, $item->name); //g3 stores resizes and thumbs different than g1 - $reply->set('image.resized_width.'.$count, $item->resize_width); - $reply->set('image.resized_height.'.$count, $item->resize_height); - //$reply->set('image.resizedNum.'.$count, 'the number of resized versions for this image [since 2.14]'); - //$reply->set('image.resized.resized-num.name.'.$count, 'filename of the resized-numth resize [G2 since 2.14]'); - //$reply->set('image.resized.resized-num.width.'.$count, 'the width of the resized-numth resize [G2 since 2.14]'); - //$reply->set('image.resized.resized-num.height.'.$count, 'the height of the resized-numth resize [G2 since 2.14]'); - $reply->set('image.thumbName.'.$count, $item->name); //g3 stores resizes and thumbs different than g1 - $reply->set('image.thumb_width.'.$count, $item->thumb_width); - $reply->set('image.thumb_height.'.$count, $item->thumb_height); - - $reply->set('image.caption.'.$count, $item->title); - $reply->set('image.title.'.$count, $item->title); - //$reply->set('image.extrafield.fieldname.'.$count, 'value of the extra field of key fieldname'); - $reply->set('image.clicks.'.$count, $item->view_count); - //* - $reply->set('image.capturedate.year.'.$count, date("Y", $item->captured)); - $reply->set('image.capturedate.mon.'.$count, date("m", $item->captured)); - $reply->set('image.capturedate.mday.'.$count, date("d", $item->captured)); - $reply->set('image.capturedate.hours.'.$count, date("H", $item->captured)); - $reply->set('image.capturedate.minutes.'.$count, date("i", $item->captured)); - $reply->set('image.capturedate.seconds.'.$count, date("s", $item->captured)); - //*/ - //XXX $reply->set('image.forceExtension.'.$count, ''); - $reply->set('image.hidden.'.$count, 'no'); //XXX - } - else { - $reply->set('album.name.'.$count, $item->name); + if(access::can('view', $item)) { + + $count++; + if($item->type != "album") { + $info = pathinfo($item->file_path()); + + $reply->set('image.name.'.$count, $item->name); + $reply->set('image.raw_width.'.$count, $item->width); + $reply->set('image.raw_height.'.$count, $item->height); + $reply->set('image.raw_filesize.'.$count, filesize($item->file_path())); + $reply->set('image.resizedName.'.$count, $item->name); //g3 stores resizes and thumbs different than g1 + $reply->set('image.resized_width.'.$count, $item->resize_width); + $reply->set('image.resized_height.'.$count, $item->resize_height); + /* + $reply->set('image.resizedNum.'.$count, 'the number of resized versions for this image [since 2.14]'); + $reply->set('image.resized.resized-num.name.'.$count, 'filename of the resized-numth resize [G2 since 2.14]'); + $reply->set('image.resized.resized-num.width.'.$count, 'the width of the resized-numth resize [G2 since 2.14]'); + $reply->set('image.resized.resized-num.height.'.$count, 'the height of the resized-numth resize [G2 since 2.14]'); + //*/ + $reply->set('image.thumbName.'.$count, $item->name); //g3 stores resizes and thumbs different than g1 + $reply->set('image.thumb_width.'.$count, $item->thumb_width); + $reply->set('image.thumb_height.'.$count, $item->thumb_height); + $reply->set('image.caption.'.$count, $item->title); + $reply->set('image.title.'.$count, $item->name); + //$reply->set('image.extrafield.fieldname.'.$count, 'value of the extra field of key fieldname'); + $reply->set('image.clicks.'.$count, $item->view_count); + $reply->set('image.capturedate.year.'.$count, date("Y", $item->captured)); + $reply->set('image.capturedate.mon.'.$count, date("m", $item->captured)); + $reply->set('image.capturedate.mday.'.$count, date("d", $item->captured)); + $reply->set('image.capturedate.hours.'.$count, date("H", $item->captured)); + $reply->set('image.capturedate.minutes.'.$count, date("i", $item->captured)); + $reply->set('image.capturedate.seconds.'.$count, date("s", $item->captured)); + $reply->set('image.forceExtension.'.$count, $info['extension']); + $reply->set('image.hidden.'.$count, access::user_can(identity::guest(), 'view', $item) ? 'no' : 'yes'); + } + else { + $reply->set('album.name.'.$count, $item->name); + } + } - } - + } + $reply->set('image_count', $count); - //* The baseurl contains a fully-qualified URL. A URL to each image - // can be obtained by appending the filename of the image to this. - if(isset($item) && $item->loaded()) { - $url = $item->file_url(true); - $pos = strrpos($url, '/'); - $reply->set('baseurl', ($pos!==false ? substr($url, 0, $pos+1) : $url) ); - } - else { - $reply->set('baseurl', $album->abs_url()); - } + //* The baseurl contains a fully-qualified URL. A URL to each image + // can be obtained by appending the filename of the image to this. + if(isset($item) && $item->loaded()) { + $url = $item->file_url(true); + $pos = strrpos($url, '/'); + $reply->set('baseurl', ($pos!==false ? substr($url, 0, $pos+1) : $url) ); + } + else { + $reply->set('baseurl', $album->abs_url()); + } //*/ $reply->send(); - } - else { - $reply->set('status_text', t('Failed to load album with name %name.', array('name' => $name))); - $reply->send(gallery_remote::NO_VIEW_PERMISSION); - } - } - + } + else { + $reply->set('status_text', t('Failed to load album with name %name.', array('name' => $name))); + $reply->send(gallery_remote::NO_VIEW_PERMISSION); + } + } + } From 0b0144d5444275f2e53e1e841bb1eff177f3c963 Mon Sep 17 00:00:00 2001 From: "Thomas E. Horner" Date: Mon, 20 Jun 2011 15:59:58 +0200 Subject: [PATCH 03/10] added a settings page with install notes --- 3.0/modules/remote/README | 4 +- .../remote/controllers/admin_remote.php | 28 ++++++++ 3.0/modules/remote/helpers/remote_event.php | 29 +++++++++ .../remote/libraries/GalleryRemoteReply.php | 16 ++--- 3.0/modules/remote/module.info | 8 +-- 3.0/modules/remote/patches/cookie.patch | 24 ++++--- .../remote/patches/gallery_remote2.php | 4 -- 3.0/modules/remote/patches/htaccess.patch | 28 ++++---- .../remote/views/admin_remote.html.php | 65 +++++++++++++++++++ 9 files changed, 163 insertions(+), 43 deletions(-) create mode 100644 3.0/modules/remote/controllers/admin_remote.php create mode 100644 3.0/modules/remote/helpers/remote_event.php delete mode 100644 3.0/modules/remote/patches/gallery_remote2.php create mode 100644 3.0/modules/remote/views/admin_remote.html.php diff --git a/3.0/modules/remote/README b/3.0/modules/remote/README index 456a2e4c..7e271ba5 100644 --- a/3.0/modules/remote/README +++ b/3.0/modules/remote/README @@ -1,3 +1 @@ -This is a preliminary work. To use it, you need to apply the changes -in the patches directory. It's got limited functionality and is only -the beginning of the effort. +To use this module you need to apply the changes in the patches directory. diff --git a/3.0/modules/remote/controllers/admin_remote.php b/3.0/modules/remote/controllers/admin_remote.php new file mode 100644 index 00000000..64fb22c1 --- /dev/null +++ b/3.0/modules/remote/controllers/admin_remote.php @@ -0,0 +1,28 @@ +page_title = t('Gallery Remote Protocol 2'); + $view->content = new View('admin_remote.html'); + print $view; + } +} \ No newline at end of file diff --git a/3.0/modules/remote/helpers/remote_event.php b/3.0/modules/remote/helpers/remote_event.php new file mode 100644 index 00000000..7abb70f0 --- /dev/null +++ b/3.0/modules/remote/helpers/remote_event.php @@ -0,0 +1,29 @@ +get("settings_menu") + ->append(Menu::factory("link") + ->id("remote") + ->label(t("Gallery Remote")) + ->url(url::site("admin/remote"))); + } +} diff --git a/3.0/modules/remote/libraries/GalleryRemoteReply.php b/3.0/modules/remote/libraries/GalleryRemoteReply.php index 7ad5781f..8dc8f4aa 100644 --- a/3.0/modules/remote/libraries/GalleryRemoteReply.php +++ b/3.0/modules/remote/libraries/GalleryRemoteReply.php @@ -19,8 +19,8 @@ */ class GalleryRemoteReply_Core { - private $values = array(); - private $nl = "\n"; + private $values = array(); + private $nl = "\n"; /** * Constructor. * @param int $status a Gallery Remote status code @@ -46,12 +46,12 @@ class GalleryRemoteReply_Core { } public function send($status='') { - if($status!='') $reply->set('status', $status); - //ksort($this->values); + if($status!='') $reply->set('status', $status); + //ksort($this->values); - echo '#__GR2PROTO__'.$this->nl; - foreach($this->values as $key => $value) { - echo $key.'='.$value.$this->nl; - } + echo '#__GR2PROTO__'.$this->nl; + foreach($this->values as $key => $value) { + echo $key.'='.$value.$this->nl; + } } } diff --git a/3.0/modules/remote/module.info b/3.0/modules/remote/module.info index 89b1d177..b0e74bbd 100644 --- a/3.0/modules/remote/module.info +++ b/3.0/modules/remote/module.info @@ -1,7 +1,7 @@ name = "Gallery Remote" -description = "Use Gallery Remote and other similar applications to control your Gallery" +description = "Use Gallery Remote and other similar applications to control Gallery 3" version = 1 -author_name = "" -author_url = "" -info_url = "http://codex.gallery2.org/Gallery3:Modules:remote" +author_name = "Thomas E. Horner" +author_url = "http://www.t-horner.com" +info_url = "http://www.t-horner.com/g3_remote" discuss_url = "http://gallery.menalto.com/forum_module_remote" diff --git a/3.0/modules/remote/patches/cookie.patch b/3.0/modules/remote/patches/cookie.patch index 95b16f69..4b9b7658 100644 --- a/3.0/modules/remote/patches/cookie.patch +++ b/3.0/modules/remote/patches/cookie.patch @@ -1,13 +1,11 @@ -diff --git a/system/helpers/cookie.php b/system/helpers/cookie.php -index 901b6d8..df276ee 100644 ---- a/system/helpers/cookie.php -+++ b/system/helpers/cookie.php -@@ -45,7 +45,7 @@ class cookie_Core { - // Expiration timestamp - $expire = ($expire == 0) ? 0 : time() + (int) $expire; - -- return setcookie($name, $value, $expire, $path, $domain, $secure, $httponly); -+ return setcookie($name, $value, $expire, $path, $domain, $secure, 0); - } - - /** +diff --git a/modules/gallery/config/cookie.php b/modules/gallery/config/cookie.php +index a865231..a2a0af7 100644 +--- a/modules/gallery/config/cookie.php ++++ b/modules/gallery/config/cookie.php +@@ -45,4 +45,4 @@ $config['secure'] = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on'; + * Enable this option to disable the cookie from being accessed when using a + * secure protocol. This option is only available in PHP 5.2 and above. + */ +-$config['httponly'] = true; +\ No newline at end of file ++$config['httponly'] = false; diff --git a/3.0/modules/remote/patches/gallery_remote2.php b/3.0/modules/remote/patches/gallery_remote2.php deleted file mode 100644 index db39124b..00000000 --- a/3.0/modules/remote/patches/gallery_remote2.php +++ /dev/null @@ -1,4 +0,0 @@ - diff --git a/3.0/modules/remote/patches/htaccess.patch b/3.0/modules/remote/patches/htaccess.patch index b2b33617..3875724c 100644 --- a/3.0/modules/remote/patches/htaccess.patch +++ b/3.0/modules/remote/patches/htaccess.patch @@ -1,15 +1,21 @@ diff --git a/.htaccess b/.htaccess -index 1d8bcb3..8229928 100644 +index d255efa..68b66bb 100644 --- a/.htaccess +++ b/.htaccess -@@ -56,3 +56,10 @@ - # RewriteRule ^(.*)$ index.php?kohana_uri=$1 [QSA,PT,L] - # RewriteRule ^$ index.php?kohana_uri=$1 [QSA,PT,L] - # -+ +@@ -22,6 +84,16 @@ + SecFilterScanPOST Off + + +# URL rewriting for Gallery Remote -+# -+# RewriteEngine On -+# RewriteBase /~bharat/gallery3/ -+# RewriteRule ^gallery_remote2.php$ index.php?kohana_uri=/remote [QSA,PT,L] -+# ++ ++ Options +FollowSymLinks ++ RewriteEngine On ++ RewriteBase /gallery/ ++ ErrorDocument 404 default ++ RewriteRule ^main.php(.*)$ / [QSA,L,R=404] ++ RewriteRule ^gallery_remote2.php(.*)$ index.php?kohana_uri=/gallery_remote$1 [QSA,PT,L] ++ ++ + # Increase security by uncommenting this block. It keeps browsers + # from seeing support files that they shouldn't have access to. We + # comment this out because Apache2 requires some minor configuration diff --git a/3.0/modules/remote/views/admin_remote.html.php b/3.0/modules/remote/views/admin_remote.html.php new file mode 100644 index 00000000..495cb641 --- /dev/null +++ b/3.0/modules/remote/views/admin_remote.html.php @@ -0,0 +1,65 @@ + +
+

+

+ +

+ + + +
From d87f5c8477e6d2c79ad3542d1d4179c1d5e887be Mon Sep 17 00:00:00 2001 From: "Thomas E. Horner" Date: Sat, 15 Oct 2011 08:19:14 +0200 Subject: [PATCH 04/10] fix for setting the correct return status (poggenpower) --- 3.0/modules/remote/libraries/GalleryRemoteReply.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3.0/modules/remote/libraries/GalleryRemoteReply.php b/3.0/modules/remote/libraries/GalleryRemoteReply.php index 8dc8f4aa..555cfa19 100644 --- a/3.0/modules/remote/libraries/GalleryRemoteReply.php +++ b/3.0/modules/remote/libraries/GalleryRemoteReply.php @@ -46,7 +46,7 @@ class GalleryRemoteReply_Core { } public function send($status='') { - if($status!='') $reply->set('status', $status); + if($status!='') $this->set('status', $status); //ksort($this->values); echo '#__GR2PROTO__'.$this->nl; From aa673efe9a0b493eae8a3c26a0455acc76f30adf Mon Sep 17 00:00:00 2001 From: "Thomas E. Horner" Date: Thu, 5 Jan 2012 14:37:53 +0100 Subject: [PATCH 05/10] improved non-ascii char handling and mime-type recognition --- .../remote/controllers/gallery_remote.php | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/3.0/modules/remote/controllers/gallery_remote.php b/3.0/modules/remote/controllers/gallery_remote.php index fce7d88e..6c708a74 100644 --- a/3.0/modules/remote/controllers/gallery_remote.php +++ b/3.0/modules/remote/controllers/gallery_remote.php @@ -154,7 +154,7 @@ class Gallery_Remote_Controller extends Controller { $reply->set('album.name.'.$count, $item->slug); $reply->set('album.title.'.$count, $item->title); $reply->set('album.summary.'.$count, $item->description); - $reply->set('album.parent.'.$count, $item->parent()->id == $root->id ? '0' : $item->parent()->name); + $reply->set('album.parent.'.$count, $item->parent()->id == $root->id ? '0' : $item->parent()->slug); $reply->set('album.resize_size.'.$count, $resize_size); $reply->set('album.max_size.'.$count, '0'); $reply->set('album.thumb_size.'.$count, $thumb_size); @@ -205,7 +205,7 @@ class Gallery_Remote_Controller extends Controller { try { $album->save(); - $reply->set('album_name', $album->name); + $reply->set('album_name', $album->slug); $reply->set('status_text', 'New album created successfuly.'); $reply->send(); @@ -264,6 +264,21 @@ class Gallery_Remote_Controller extends Controller { else $type = self::get_mime_type($_FILES['userfile']['name']); + + /* */ + if($type=='') + { + if(function_exists('getimagesize')) { + $size = getimagesize($_FILES['userfile']['tmp_name']); + $type = $size['mime']; + } + else if(function_exists('exif_imagetype') && function_exists('image_type_to_mime_type')) { + $type = image_type_to_mime_type(exif_imagetype($_FILES['userfile']['tmp_name'])); + } + } + /* */ + + if ($type!='' && !in_array($type, array('image/jpeg', 'image/gif', 'image/png'))) { $reply->set('status_text', t("'%path' is an unsupported image type '%type'", array('path' => $_FILES['userfile']['tmp_name'], 'type' => $type))); $reply->send(gallery_remote::UPLOAD_PHOTO_FAIL); @@ -310,7 +325,7 @@ class Gallery_Remote_Controller extends Controller { } catch (ORM_Validation_Exception $e) { $validation = $e->validation; //print_r($validation->errors()); exit; - $reply->set('status_text', t('Failed to validate item %item: %errors', array('item' => $filename, 'errors' => print_r($validation->errors(),true)) )); + $reply->set('status_text', t('Failed to validate item %item: %errors', array('item' => $filename, 'errors' => str_replace("\n", ' ', print_r($validation->errors(),true))) )); $reply->send(gallery_remote::UPLOAD_PHOTO_FAIL); //FIXME gallery remote ignores this return value and continues to wait } @@ -391,7 +406,7 @@ class Gallery_Remote_Controller extends Controller { } catch (ORM_Validation_Exception $e) { $validation = $e->validation; //print_r($validation->errors()); exit; - $reply->set('status_text', t('Failed to validate item %item.', array('item' => $name)).print_r($validation->errors(),true)); + $reply->set('status_text', t('Failed to validate item %item.', array('item' => $name)).str_replace("\n", ' ', print_r($validation->errors(),true)) ); $reply->send(gallery_remote::NO_WRITE_PERMISSION); } From ec815e799cc9e72c3eab333fa34da7a0f4e9ecdf Mon Sep 17 00:00:00 2001 From: "Thomas E. Horner" Date: Fri, 13 Jan 2012 09:14:43 +0100 Subject: [PATCH 06/10] changed from slug to id + added the gallery root folder as album --- .../remote/controllers/gallery_remote.php | 65 ++++++++++++------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/3.0/modules/remote/controllers/gallery_remote.php b/3.0/modules/remote/controllers/gallery_remote.php index 6c708a74..f51a6fd9 100644 --- a/3.0/modules/remote/controllers/gallery_remote.php +++ b/3.0/modules/remote/controllers/gallery_remote.php @@ -144,17 +144,37 @@ class Gallery_Remote_Controller extends Controller { $thumb_size = module::get_var('gallery', 'thumb_size'); $resize_size = module::get_var('gallery', 'resize_size'); - $count = 0; + + //* + $count = 1; + $item = &$root; + $reply->set('album.name.'.$count, $item->id); + $reply->set('album.title.'.$count, $item->title); + $reply->set('album.summary.'.$count, 'Gallery Remote Interface by Thomas E. Horner'); + $reply->set('album.parent.'.$count, '0'); + $reply->set('album.resize_size.'.$count, $resize_size); + $reply->set('album.max_size.'.$count, '0'); + $reply->set('album.thumb_size.'.$count, $thumb_size); + if($use_permissions) { + $reply->set('album.perms.add.'.$count, access::can('add', $item) ? 'true':'false'); + $reply->set('album.perms.write.'.$count, access::can('add', $item) ? 'true':'false'); + $reply->set('album.perms.del_item.'.$count, access::can('edit', $item) ? 'true':'false'); + $reply->set('album.perms.del_alb.'.$count, access::can('edit', $item) ? 'true':'false'); + $reply->set('album.perms.create_sub.'.$count, access::can('add', $item) ? 'true':'false'); + } + $reply->set('album.info.extrafields.'.$count, 'Summary'); + // */ + foreach( $root->descendants(null, null, array(array("type", "=", "album"))) as $item ) { if(!$use_permissions || access::can('view', $item)) { $count++; - $reply->set('album.name.'.$count, $item->slug); + $reply->set('album.name.'.$count, $item->id); $reply->set('album.title.'.$count, $item->title); $reply->set('album.summary.'.$count, $item->description); - $reply->set('album.parent.'.$count, $item->parent()->id == $root->id ? '0' : $item->parent()->slug); + $reply->set('album.parent.'.$count, $item->parent()->id == $root->id ? '0' : $item->parent()->id); $reply->set('album.resize_size.'.$count, $resize_size); $reply->set('album.max_size.'.$count, '0'); $reply->set('album.thumb_size.'.$count, $thumb_size); @@ -165,7 +185,7 @@ class Gallery_Remote_Controller extends Controller { $reply->set('album.perms.del_alb.'.$count, access::can('edit', $item) ? 'true':'false'); $reply->set('album.perms.create_sub.'.$count, access::can('add', $item) ? 'true':'false'); } - $reply->set('album.info.extrafields.'.$count, ''); + $reply->set('album.info.extrafields.'.$count, 'Summary'); } } $reply->set('album_count', $count); @@ -183,7 +203,7 @@ class Gallery_Remote_Controller extends Controller { $desc = trim($input->post('newAlbumDesc')); if($album=='0') $parent = item::root(); - else $parent = ORM::factory("item")->where("slug", "=", $album)->find(); + else $parent = ORM::factory("item")->where("id", "=", $album)->find(); if(isset($parent) && $parent->loaded() && $parent->id!='') { $album = ORM::factory('item'); @@ -205,7 +225,7 @@ class Gallery_Remote_Controller extends Controller { try { $album->save(); - $reply->set('album_name', $album->slug); + $reply->set('album_name', $album->id); $reply->set('status_text', 'New album created successfuly.'); $reply->send(); @@ -230,13 +250,13 @@ class Gallery_Remote_Controller extends Controller { $resize_size = module::get_var('gallery', 'resize_size'); if($album=='0') $parent = item::root(); - else $parent = ORM::factory("item")->where("slug", "=", $album)->find(); + else $parent = ORM::factory("item")->where("id", "=", $album)->find(); if(isset($parent) && $parent->loaded() && $parent->id!='') { $reply->set('auto_resize', $resize_size); //resize size is the same for all g3 albums $reply->set('max_size', '0'); //not supported by g3 $reply->set('add_to_beginning', 'no'); //g3 will add images to the end - $reply->set('extrafields', ''); + $reply->set('extrafields', 'Summary'); $reply->set('title', $parent->title); $reply->set('status_text', 'Album properties queried successfuly.'); $reply->send(); @@ -255,7 +275,7 @@ class Gallery_Remote_Controller extends Controller { $autorotate = trim($input->post('auto_rotate')); if($album=='0') $parent = item::root(); - else $parent = ORM::factory("item")->where("slug", "=", $album)->find(); + else $parent = ORM::factory("item")->where("id", "=", $album)->find(); if(isset($parent) && $parent->loaded() && $parent->id!='') { @@ -313,7 +333,7 @@ class Gallery_Remote_Controller extends Controller { try { $item->save(); - $reply->set('item_name', $item->name); + $reply->set('item_name', $item->id); $reply->set('status_text', 'New item created successfuly.'); $reply->send(); @@ -345,10 +365,10 @@ class Gallery_Remote_Controller extends Controller { $name = trim($input->post('set_albumName')); $destination = trim($input->post('set_destalbumName')); - $album = ORM::factory("item")->where("slug", "=", $name)->find(); + $album = ORM::factory("item")->where("id", "=", $name)->find(); if($destination=='0') $parent = item::root(); - else $parent = ORM::factory("item")->where("slug", "=", $destination)->find(); + else $parent = ORM::factory("item")->where("id", "=", $destination)->find(); if(isset($parent) && $parent->loaded() && $parent->id!='' && isset($album) && $album->loaded() && $album->id!='') { @@ -382,7 +402,7 @@ class Gallery_Remote_Controller extends Controller { $name = trim($input->post('itemId')); if($name=='0') $item = item::root(); - else $item = ORM::factory("item")->where("slug", "=", $name)->find(); + else $item = ORM::factory("item")->where("id", "=", $name)->find(); if(isset($item) && $item->loaded() && $item->id!='') { @@ -394,7 +414,7 @@ class Gallery_Remote_Controller extends Controller { try { $item->save(); - $reply->set('item_name', $item->name); + $reply->set('item_name', $item->id); $reply->set('status_text', 'Item view count incremented successfuly.'); $reply->send(); @@ -421,13 +441,13 @@ class Gallery_Remote_Controller extends Controller { $name = trim($input->post('itemId')); if($name=='0') $item = item::root(); - else $item = ORM::factory("item")->where("slug", "=", $name)->find(); + else $item = ORM::factory("item")->where("id", "=", $name)->find(); if(isset($item) && $item->loaded() && $item->id!='') { $info = pathinfo($item->file_path()); $reply->set('status_text', 'Item properties queried successfuly.'); - $reply->set('image.name', $item->slug); + $reply->set('image.name', $item->id); $reply->set('image.raw_width', $item->width); $reply->set('image.raw_height', $item->height); $reply->set('image.raw_filesize', filesize($item->file_path())); @@ -438,7 +458,7 @@ class Gallery_Remote_Controller extends Controller { $reply->set('image.thumb_width', $item->thumb_width); $reply->set('image.thumb_height', $item->thumb_height); $reply->set('image.caption', $item->title); - $reply->set('image.title', $item->title); + $reply->set('image.title', $item->name); $reply->set('image.forceExtension', $info['extension']); $reply->set('image.hidden', access::user_can(identity::guest(), 'view', $item) ? 'no' : 'yes'); $reply->send(); @@ -454,11 +474,11 @@ class Gallery_Remote_Controller extends Controller { $albums = trim($input->post('albums_too')); //yes/no [optional, since 2.13] $random = trim($input->post('random')); //yes/no [optional, G2 since ***] $limit = trim($input->post('limit')); //number-of-images [optional, G2 since ***] - $fields = trim($input->post('extrafields')); //yes/no [optional, G2 since 2.12] + $extra = trim($input->post('extrafields')); //yes/no [optional, G2 since 2.12] $sizes = trim($input->post('all_sizes')); //yes/no [optional, G2 since 2.14] if($name=='0') $album = item::root(); - $album = ORM::factory("item")->where("slug", "=", $name)->find(); + $album = ORM::factory("item")->where("id", "=", $name)->find(); if(isset($album) && $album->loaded() && $album->id!='' && access::can('view', $album)) { @@ -467,7 +487,7 @@ class Gallery_Remote_Controller extends Controller { $reply->set('status_text', 'Album images query successful.'); $reply->set('album.caption', $album->title); - $reply->set('album.extrafields', ''); + $reply->set('album.extrafields', 'Summary'); /* $reply->set('image_count', '0'); @@ -484,7 +504,7 @@ class Gallery_Remote_Controller extends Controller { if($item->type != "album") { $info = pathinfo($item->file_path()); - $reply->set('image.name.'.$count, $item->name); + $reply->set('image.name.'.$count, $item->id); $reply->set('image.raw_width.'.$count, $item->width); $reply->set('image.raw_height.'.$count, $item->height); $reply->set('image.raw_filesize.'.$count, filesize($item->file_path())); @@ -503,6 +523,7 @@ class Gallery_Remote_Controller extends Controller { $reply->set('image.caption.'.$count, $item->title); $reply->set('image.title.'.$count, $item->name); //$reply->set('image.extrafield.fieldname.'.$count, 'value of the extra field of key fieldname'); + $reply->set('image.extrafield.summary.'.$count, $item->description); $reply->set('image.clicks.'.$count, $item->view_count); $reply->set('image.capturedate.year.'.$count, date("Y", $item->captured)); $reply->set('image.capturedate.mon.'.$count, date("m", $item->captured)); @@ -514,7 +535,7 @@ class Gallery_Remote_Controller extends Controller { $reply->set('image.hidden.'.$count, access::user_can(identity::guest(), 'view', $item) ? 'no' : 'yes'); } else { - $reply->set('album.name.'.$count, $item->name); + $reply->set('album.name.'.$count, $item->id); } } From cd089cb36c862e4e0f07cefea41f1a77cd54e325 Mon Sep 17 00:00:00 2001 From: "Thomas E. Horner" Date: Wed, 8 Aug 2012 00:51:03 +0200 Subject: [PATCH 07/10] added bugfixes for latest gallery changes --- .../remote/controllers/gallery_remote.php | 53 +++++++++++++------ 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/3.0/modules/remote/controllers/gallery_remote.php b/3.0/modules/remote/controllers/gallery_remote.php index 4aa3ae17..5a3081bf 100644 --- a/3.0/modules/remote/controllers/gallery_remote.php +++ b/3.0/modules/remote/controllers/gallery_remote.php @@ -116,6 +116,10 @@ class Gallery_Remote_Controller extends Controller { return (false); // no match at all } + private static function decode($input) { + return html_entity_decode(trim($input), ENT_COMPAT, 'UTF-8'); + } + private function _login(&$input, &$reply) { $uname = trim($input->post('uname')); if (empty($uname)) { @@ -145,7 +149,7 @@ class Gallery_Remote_Controller extends Controller { $thumb_size = module::get_var('gallery', 'thumb_size'); $resize_size = module::get_var('gallery', 'resize_size'); - //* + //* $count = 1; $item = &$root; $reply->set('album.name.'.$count, $item->id); @@ -198,9 +202,9 @@ class Gallery_Remote_Controller extends Controller { private function _new_album(&$input, &$reply) { $album = trim($input->post('set_albumName')); - $name = trim($input->post('newAlbumName')); - $title = trim($input->post('newAlbumTitle')); - $desc = trim($input->post('newAlbumDesc')); + $name = $this->decode($input->post('newAlbumName')); + $title = $this->decode($input->post('newAlbumTitle')); + $desc = $this->decode($input->post('newAlbumDesc')); if($album=='0') $parent = item::root(); else $parent = ORM::factory("item")->where("id", "=", $album)->find(); @@ -269,9 +273,9 @@ class Gallery_Remote_Controller extends Controller { private function _add_item(&$input, &$reply) { $album = trim($input->post('set_albumName')); - $userfilename = trim($input->post('userfile_name')); - $title = trim($input->post('caption')); - $forcefilename = trim($input->post('force_filename')); + $userfilename = $this->decode($input->post('userfile_name')); + $title = $this->decode($input->post('caption')); + $forcefilename = $this->decode($input->post('force_filename')); $autorotate = trim($input->post('auto_rotate')); if($album=='0') $parent = item::root(); @@ -288,13 +292,13 @@ class Gallery_Remote_Controller extends Controller { /* */ if($type=='') { - if(function_exists('getimagesize')) { - $size = getimagesize($_FILES['userfile']['tmp_name']); - $type = $size['mime']; - } - else if(function_exists('exif_imagetype') && function_exists('image_type_to_mime_type')) { - $type = image_type_to_mime_type(exif_imagetype($_FILES['userfile']['tmp_name'])); - } + if(function_exists('getimagesize')) { + $size = getimagesize($_FILES['userfile']['tmp_name']); + $type = $size['mime']; + } + else if(function_exists('exif_imagetype') && function_exists('image_type_to_mime_type')) { + $type = image_type_to_mime_type(exif_imagetype($_FILES['userfile']['tmp_name'])); + } } /* */ @@ -314,6 +318,18 @@ class Gallery_Remote_Controller extends Controller { if($pos!==false) $slug = substr($slug, 0, $pos); + //*/ fix for a gallery remote bug... + $filename = str_replace('.JPG.jpeg', '.jpeg', $filename); + //*/ + + //*/ suddenly gallery fails because the uploaded(!) file (of cause!) doesn't contain a file extension + if(strpos($_FILES['userfile']['tmp_name'], '.')===false) { + $moveto = $_FILES['userfile']['tmp_name'].'.'.substr($type, strpos($type, '/')+1); + rename($_FILES['userfile']['tmp_name'], $moveto); + $_FILES['userfile']['tmp_name'] = $moveto; + } + //*/ + try { $item = ORM::factory('item'); $item->type = 'photo'; @@ -337,7 +353,14 @@ class Gallery_Remote_Controller extends Controller { $reply->set('status_text', 'New item created successfuly.'); $reply->send(); - } catch (Exception $e) { + } + catch (ORM_Validation_Exception $e) { + $validation = $e->validation; + //print_r($validation->errors()); exit; + $reply->set('status_text', t('Failed to validate item %item: %errors', array('item' => $filename, 'errors' => str_replace("\n", ' ', print_r($validation->errors(),true))) )); + $reply->send(gallery_remote::UPLOAD_PHOTO_FAIL); //FIXME gallery remote ignores this return value and continues to wait + } + catch (Exception $e) { $reply->set('status_text', t('Failed to add item %item.', array('item' => $filename))); $reply->send(gallery_remote::UPLOAD_PHOTO_FAIL); //FIXME gallery remote ignores this return value and continues to wait } From 6d3fa10a913f047e8adc7cf76fa1ad4ad75e22e6 Mon Sep 17 00:00:00 2001 From: "Thomas E. Horner" Date: Wed, 8 Aug 2012 00:55:18 +0200 Subject: [PATCH 08/10] minor text change --- 3.0/modules/remote/controllers/gallery_remote.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3.0/modules/remote/controllers/gallery_remote.php b/3.0/modules/remote/controllers/gallery_remote.php index 5a3081bf..bb28815d 100644 --- a/3.0/modules/remote/controllers/gallery_remote.php +++ b/3.0/modules/remote/controllers/gallery_remote.php @@ -239,7 +239,7 @@ class Gallery_Remote_Controller extends Controller { } } catch (ORM_Validation_Exception $e) { - $reply->set('status_text', t('Failed to validate album with name %name.', array('name' => $name))); + $reply->set('status_text', t('Failed to save album with name %name.', array('name' => $name))); $reply->send(gallery_remote::CREATE_ALBUM_FAILED); } } From b0887b2f97d2586b5e95fc0b70ca7baa6c484fc0 Mon Sep 17 00:00:00 2001 From: "Thomas E. Horner" Date: Wed, 8 Aug 2012 00:58:51 +0200 Subject: [PATCH 09/10] committing the correct minor text change --- 3.0/modules/remote/controllers/gallery_remote.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/3.0/modules/remote/controllers/gallery_remote.php b/3.0/modules/remote/controllers/gallery_remote.php index bb28815d..26170cc2 100644 --- a/3.0/modules/remote/controllers/gallery_remote.php +++ b/3.0/modules/remote/controllers/gallery_remote.php @@ -239,7 +239,7 @@ class Gallery_Remote_Controller extends Controller { } } catch (ORM_Validation_Exception $e) { - $reply->set('status_text', t('Failed to save album with name %name.', array('name' => $name))); + $reply->set('status_text', t('Failed to validate album with name %name.', array('name' => $name))); $reply->send(gallery_remote::CREATE_ALBUM_FAILED); } } @@ -357,7 +357,7 @@ class Gallery_Remote_Controller extends Controller { catch (ORM_Validation_Exception $e) { $validation = $e->validation; //print_r($validation->errors()); exit; - $reply->set('status_text', t('Failed to validate item %item: %errors', array('item' => $filename, 'errors' => str_replace("\n", ' ', print_r($validation->errors(),true))) )); + $reply->set('status_text', t('Failed to save item %item: %errors', array('item' => $filename, 'errors' => str_replace("\n", ' ', print_r($validation->errors(),true))) )); $reply->send(gallery_remote::UPLOAD_PHOTO_FAIL); //FIXME gallery remote ignores this return value and continues to wait } catch (Exception $e) { From 06bd5cd878ac99e5eeb18de44b98f56b76f69509 Mon Sep 17 00:00:00 2001 From: "Thomas E. Horner" Date: Sat, 25 Aug 2012 09:32:14 +0200 Subject: [PATCH 10/10] updated the protocol version check --- .../remote/controllers/gallery_remote.php | 16 ++++++++++++---- 3.0/modules/remote/helpers/gallery_remote.php | 5 ++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/3.0/modules/remote/controllers/gallery_remote.php b/3.0/modules/remote/controllers/gallery_remote.php index 26170cc2..6a41f657 100644 --- a/3.0/modules/remote/controllers/gallery_remote.php +++ b/3.0/modules/remote/controllers/gallery_remote.php @@ -77,6 +77,7 @@ class Gallery_Remote_Controller extends Controller { private function _check_protocol(&$input, &$reply) { $version = trim($input->post('protocol_version')); + $reply->set('status_text', 'Minimum protocol version required: '.gallery_remote::GR_PROT_MAJ.'.'.gallery_remote::GR_PROT_MIN.' - your client\'s protocol version: '.$version); if($version=='') { $reply->send(gallery_remote::PROTO_VER_MISSING); return false; @@ -85,14 +86,21 @@ class Gallery_Remote_Controller extends Controller { $reply->send(gallery_remote::PROTO_MAJ_FMT_INVAL); return false; } - else if($version<'2') { + else if($versionsend(gallery_remote::PROTO_MAJ_VER_INVAL); return false; } - else if($version<'2.3') { - $reply->send(gallery_remote::PROTO_MIN_VER_INVAL); + else if(strpos($version, '.')===false) { + $reply->send(gallery_remote::PROTO_MAJ_FMT_INVAL); return false; } + else { + $ver = explode('.', $version); + if($ver[0]==gallery_remote::GR_PROT_MAJ && $ver[1]send(gallery_remote::PROTO_MIN_VER_INVAL); + return false; + } + } return true; } @@ -357,7 +365,7 @@ class Gallery_Remote_Controller extends Controller { catch (ORM_Validation_Exception $e) { $validation = $e->validation; //print_r($validation->errors()); exit; - $reply->set('status_text', t('Failed to save item %item: %errors', array('item' => $filename, 'errors' => str_replace("\n", ' ', print_r($validation->errors(),true))) )); + $reply->set('status_text', t('Failed to validate item %item: %errors', array('item' => $filename, 'errors' => str_replace("\n", ' ', print_r($validation->errors(),true))) )); $reply->send(gallery_remote::UPLOAD_PHOTO_FAIL); //FIXME gallery remote ignores this return value and continues to wait } catch (Exception $e) { diff --git a/3.0/modules/remote/helpers/gallery_remote.php b/3.0/modules/remote/helpers/gallery_remote.php index 744b6b27..c0621ddf 100644 --- a/3.0/modules/remote/helpers/gallery_remote.php +++ b/3.0/modules/remote/helpers/gallery_remote.php @@ -18,6 +18,9 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class gallery_remote_Core { + const GR_PROT_MAJ = 2; + const GR_PROT_MIN = 3; + const GR_STAT_SUCCESS = 0; const PROTO_MAJ_VER_INVAL = 101; const PROTO_MIN_VER_INVAL= 102; @@ -35,4 +38,4 @@ class gallery_remote_Core { const CREATE_ALBUM_FAILED = 502; const MOVE_ALBUM_FAILED = 503; const ROTATE_IMAGE_FAILED = 504; -} \ No newline at end of file +}