2010-01-04 01:03:33 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Gallery - a web based photo album viewer and editor
|
2010-03-03 18:16:08 +00:00
|
|
|
* Copyright (C) 2000-2010 Bharat Mediratta
|
2010-01-04 01:03:33 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
include("Mail.php");
|
|
|
|
include("Mail/mime.php");
|
|
|
|
include("HTTP/Request.php");
|
|
|
|
|
|
|
|
class Gallery3 {
|
|
|
|
var $url;
|
|
|
|
var $token;
|
|
|
|
var $data;
|
|
|
|
var $file;
|
|
|
|
|
2010-01-28 05:46:00 +00:00
|
|
|
protected $original_entity;
|
2010-01-18 01:37:19 +00:00
|
|
|
|
2010-01-04 01:03:33 +00:00
|
|
|
/**
|
|
|
|
* Connect to a remote Gallery3 instance
|
|
|
|
*
|
|
|
|
* @param string Gallery 3 API url, eg http://example.com/gallery3/index.php/rest
|
|
|
|
* @param string username
|
|
|
|
* @param string password
|
2010-01-26 08:15:35 +00:00
|
|
|
* @return string authentication token
|
2010-01-04 01:03:33 +00:00
|
|
|
*/
|
2010-01-26 08:15:35 +00:00
|
|
|
static function login($url, $user, $pass) {
|
2010-01-04 01:03:33 +00:00
|
|
|
$response = Gallery3_Helper::request(
|
|
|
|
"post", $url, null, array("user" => $user, "password" => $pass));
|
2010-01-26 08:15:35 +00:00
|
|
|
return $response;
|
2010-01-04 01:03:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-01-26 08:15:35 +00:00
|
|
|
* Construct a new Gallery3 instance associated with a remote resource
|
|
|
|
* @param string remote url
|
|
|
|
* @param string authentication token
|
|
|
|
* @return object Gallery3
|
2010-01-04 01:03:33 +00:00
|
|
|
*/
|
2010-02-11 01:54:56 +00:00
|
|
|
static function factory($url=null, $token=null) {
|
2010-01-26 08:15:35 +00:00
|
|
|
$obj = new Gallery3();
|
|
|
|
$obj->token = $token;
|
|
|
|
$obj->url = $url;
|
|
|
|
if ($url && $token) {
|
|
|
|
$obj->load();
|
|
|
|
}
|
|
|
|
return $obj;
|
2010-01-04 01:03:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-01-26 08:15:35 +00:00
|
|
|
* Constructor.
|
2010-01-04 01:03:33 +00:00
|
|
|
*/
|
2010-01-26 08:15:35 +00:00
|
|
|
public function __construct() {
|
|
|
|
$this->data = new stdClass();
|
|
|
|
$this->token = null;
|
|
|
|
$this->url = null;
|
2010-01-04 01:03:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-04-04 18:58:05 +00:00
|
|
|
* Set a value on the remote resource's entity. You must call save for it to take effect.
|
2010-01-04 01:03:33 +00:00
|
|
|
*
|
|
|
|
* @param string key
|
|
|
|
* @param string value
|
|
|
|
* @return object Gallery3
|
2010-01-26 08:15:35 +00:00
|
|
|
* @chainable
|
2010-01-04 01:03:33 +00:00
|
|
|
*/
|
2010-01-26 08:15:35 +00:00
|
|
|
public function set($key, $value) {
|
2010-01-28 05:46:00 +00:00
|
|
|
$this->data->entity->$key = $value;
|
2010-01-04 01:03:33 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2010-04-04 18:58:05 +00:00
|
|
|
/**
|
|
|
|
* Replace the members for the remote resource
|
|
|
|
*
|
|
|
|
* @param array members
|
|
|
|
* @return object Gallery3
|
|
|
|
* @chainable
|
|
|
|
*/
|
|
|
|
public function set_members($members) {
|
|
|
|
$this->data->members = $members;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2010-01-04 01:03:33 +00:00
|
|
|
/**
|
|
|
|
* Attach a file to the remote resource.
|
|
|
|
*
|
|
|
|
* @param string path to a local file (eg: /tmp/foo.jpg)
|
|
|
|
* @return object Gallery3
|
|
|
|
*/
|
|
|
|
public function set_file($file) {
|
|
|
|
$this->file = $file;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-01-26 08:15:35 +00:00
|
|
|
* Save any local changes made to this resource. If this is an existing resource, we'll return
|
|
|
|
* the resource itself. If we're creating a new resource, return the newly created resource.
|
2010-01-04 01:03:33 +00:00
|
|
|
*
|
|
|
|
* @return object Gallery3
|
|
|
|
*/
|
2010-01-26 08:15:35 +00:00
|
|
|
public function create($url, $token) {
|
2010-06-20 16:54:22 +00:00
|
|
|
if (!is_string($url)) {
|
|
|
|
throw new Gallery3_Exception("Invalid url: " . var_export($url));
|
|
|
|
}
|
|
|
|
|
2010-01-26 08:15:35 +00:00
|
|
|
$response = Gallery3_Helper::request(
|
2010-04-04 18:58:05 +00:00
|
|
|
"post", $url, $token, array("entity" => $this->data->entity), $this->file);
|
2010-01-26 08:15:35 +00:00
|
|
|
$this->url = $response->url;
|
|
|
|
$this->token = $token;
|
|
|
|
return $this->load();
|
2010-01-04 01:03:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-01-26 08:15:35 +00:00
|
|
|
* Save any local changes made to this resource. If this is an existing resource, we'll return
|
|
|
|
* the resource itself. If we're creating a new resource, return the newly created resource.
|
2010-01-04 01:03:33 +00:00
|
|
|
*
|
|
|
|
* @return object Gallery3
|
|
|
|
*/
|
|
|
|
public function save() {
|
2010-01-26 08:15:35 +00:00
|
|
|
$response = Gallery3_Helper::request(
|
|
|
|
"put", $this->url, $this->token,
|
2010-04-04 18:58:05 +00:00
|
|
|
array("entity" => array_diff((array)$this->data->entity, $this->original_entity),
|
|
|
|
"members" => $this->data->members));
|
2010-01-26 08:15:35 +00:00
|
|
|
return $this->load();
|
2010-01-04 01:03:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the remote resource.
|
|
|
|
*
|
|
|
|
* @return object Gallery3
|
|
|
|
*/
|
|
|
|
public function delete() {
|
|
|
|
Gallery3_Helper::request("delete", $this->url, $this->token);
|
2010-01-26 08:15:35 +00:00
|
|
|
$this->data = array();
|
|
|
|
$this->url = null;
|
|
|
|
return $this;
|
2010-01-05 21:42:30 +00:00
|
|
|
}
|
|
|
|
|
2010-01-04 01:03:33 +00:00
|
|
|
/**
|
|
|
|
* Reload the resource from a given url. This is useful after the remote resource has been
|
|
|
|
* modified.
|
|
|
|
*
|
|
|
|
* @return object Gallery3
|
|
|
|
*/
|
2010-01-26 08:15:35 +00:00
|
|
|
public function load() {
|
2010-01-05 21:42:30 +00:00
|
|
|
$response = Gallery3_Helper::request("get", $this->url, $this->token);
|
2010-01-26 08:15:35 +00:00
|
|
|
$this->data = $response;
|
2010-06-20 16:54:22 +00:00
|
|
|
$this->original_entity = isset($response->entity) ? (array)$response->entity : null;
|
2010-01-04 01:03:33 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Gallery3_Helper {
|
|
|
|
static function request($method, $url, $token=null, $params=array(), $file=null) {
|
|
|
|
$req = new HTTP_Request($url);
|
|
|
|
$req->setMethod($method == "get" ? HTTP_REQUEST_METHOD_GET : HTTP_REQUEST_METHOD_POST);
|
|
|
|
$req->addHeader("X-Gallery-Request-Method", $method);
|
|
|
|
if ($token) {
|
|
|
|
$req->addHeader("X-Gallery-Request-Key", $token);
|
|
|
|
}
|
|
|
|
foreach ($params as $key => $value) {
|
2010-04-04 18:58:05 +00:00
|
|
|
$req->addPostData($key, is_string($value) ? $value : json_encode($value));
|
2010-01-04 01:03:33 +00:00
|
|
|
}
|
|
|
|
if ($file) {
|
|
|
|
$req->addFile("file", $file, mime_content_type($file));
|
|
|
|
}
|
|
|
|
$req->sendRequest();
|
|
|
|
|
|
|
|
switch ($req->getResponseCode()) {
|
|
|
|
case 200:
|
2010-07-24 13:39:36 +00:00
|
|
|
case 201:
|
2010-01-04 01:03:33 +00:00
|
|
|
return json_decode($req->getResponseBody());
|
|
|
|
|
|
|
|
case 403:
|
|
|
|
throw new Gallery3_Forbidden_Exception($req->getResponseBody());
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Gallery3_Exception($req->getResponseBody());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Gallery3_Exception extends Exception {
|
|
|
|
}
|
|
|
|
|
|
|
|
class Gallery3_Forbidden_Exception extends Gallery3_Exception {
|
|
|
|
}
|