summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Almdal <tnalmdal@shaw.ca>2010-01-02 16:55:06 -0800
committerTim Almdal <tnalmdal@shaw.ca>2010-01-02 16:55:06 -0800
commit5b9801092b3c347161f9e3b8069e05945a5010d2 (patch)
tree170cf04d535d43a7d9180c821cdeca43d5520d08
parent07f8e256cf140c41e7eaf293c6c41b31aff8c661 (diff)
Remove the Rest_Exception::trigger method.
-rw-r--r--modules/gallery/helpers/gallery_rest.php9
-rw-r--r--modules/rest/controllers/rest.php51
-rw-r--r--modules/rest/helpers/rest.php6
-rw-r--r--modules/rest/libraries/Rest_Exception.php35
-rw-r--r--modules/tag/helpers/tag_rest.php6
5 files changed, 49 insertions, 58 deletions
diff --git a/modules/gallery/helpers/gallery_rest.php b/modules/gallery/helpers/gallery_rest.php
index 563a2c7c..a87ebb4e 100644
--- a/modules/gallery/helpers/gallery_rest.php
+++ b/modules/gallery/helpers/gallery_rest.php
@@ -50,7 +50,7 @@ class gallery_rest_Core {
static function put($request) {
if (empty($request->arguments)) {
- Rest_Exception::trigger(400, "Bad request");
+ throw new Rest_Exception(400, "Bad request");
}
$path = implode("/", $request->arguments);
$item = gallery_rest::_get_item($path, "edit");
@@ -78,7 +78,7 @@ class gallery_rest_Core {
static function post($request) {
if (empty($request->arguments)) {
- Rest_Exception::trigger(400, "Bad request");
+ throw new Rest_Exception(400, "Bad request");
}
$components = $request->arguments;
@@ -125,15 +125,14 @@ class gallery_rest_Core {
static function delete($request) {
if (empty($request->arguments)) {
- Rest_Exception::trigger(400, "Bad request", $log_message);
- return rest::invalid_request();
+ throw new Rest_Exception(400, "Bad request");
}
$path = implode("/", $request->arguments);
$item = gallery_rest::_get_item($path, "edit");
if ($item->id == 1) {
- Rest_Exception::trigger(400, "Bad request", "Attempt to delete the root album");
+ throw new Rest_Exception(400, "Bad request");
}
$parent = $item->parent();
diff --git a/modules/rest/controllers/rest.php b/modules/rest/controllers/rest.php
index 39ca4797..26e5b31a 100644
--- a/modules/rest/controllers/rest.php
+++ b/modules/rest/controllers/rest.php
@@ -19,32 +19,34 @@
*/
class Rest_Controller extends Controller {
public function access_key() {
- $request = (object)Input::instance()->get();
- if (empty($request->user) || empty($request->password)) {
- Rest_Exception::trigger(403, "Forbidden", "No user or password supplied");
- }
+ try {
+ $request = (object)Input::instance()->get();
+ if (empty($request->user) || empty($request->password)) {
+ throw new Rest_Exception(403, "Forbidden");
+ }
- $user = identity::lookup_user_by_name($request->user);
- if (empty($user)) {
- Rest_Exception::trigger(403, "Forbidden", "User '{$request->user}' not found");
- return;
- }
+ $user = identity::lookup_user_by_name($request->user);
+ if (empty($user)) {
+ throw new Rest_Exception(403, "Forbidden");
+ }
- if (!identity::is_correct_password($user, $request->password)) {
- Rest_Exception::trigger(403, "Forbidden", "Invalid password for '{$request->user}'.");
- return;
- }
+ if (!identity::is_correct_password($user, $request->password)) {
+ throw new Rest_Exception(403, "Forbidden");
+ }
- $key = ORM::factory("user_access_token")
- ->where("user_id", "=", $user->id)
- ->find();
- if (!$key->loaded()) {
- $key->user_id = $user->id;
- $key->access_key = md5($user->name . rand());
- $key->save();
+ $key = ORM::factory("user_access_token")
+ ->where("user_id", "=", $user->id)
+ ->find();
+ if (!$key->loaded()) {
+ $key->user_id = $user->id;
+ $key->access_key = md5($user->name . rand());
+ $key->save();
+ }
+ print rest::success(array("token" => $key->access_key));
+ } catch (Rest_Exception $e) {
+ $e->sendHeaders();
}
- print rest::success(array("token" => $key->access_key));
- }
+ }
public function __call($function, $args) {
$request = rest::normalize_request($args);
@@ -54,16 +56,13 @@ class Rest_Controller extends Controller {
$handler_method = $request->method;
if (!method_exists($handler_class, $handler_method)) {
- Rest_Exception::trigger(501, "Not implemented", "$handler_class::$handler_method");
+ throw new Rest_Exception(403, "Forbidden");
}
print call_user_func(array($handler_class, $handler_method), $request);
}
} catch (Rest_Exception $e) {
$e->sendHeaders();
- } catch (Exception $e) {
- Kohana_Log::add("error", $e->__toString());
- header("HTTP/1.1 500 Internal Error");
}
}
} \ No newline at end of file
diff --git a/modules/rest/helpers/rest.php b/modules/rest/helpers/rest.php
index 00790e6b..be0644f2 100644
--- a/modules/rest/helpers/rest.php
+++ b/modules/rest/helpers/rest.php
@@ -92,12 +92,10 @@ class rest_Core {
if ($key->loaded()) {
$user = identity::lookup_user($key->user_id);
if (empty($user)) {
- Rest_Exception::trigger(403, "Forbidden", $log_message,
- "User not found: {$key->user_id}");
+ throw new Rest_Exception(403, "Forbidden");
}
} else {
- Rest_Exception::trigger(403, "Forbidden", $log_message,
- "Invalid user access token supplied: {$key->user_id}");
+ throw new Rest_Exception(403, "Forbidden");
}
}
identity::set_active_user($user);
diff --git a/modules/rest/libraries/Rest_Exception.php b/modules/rest/libraries/Rest_Exception.php
index acdcb568..905b94a0 100644
--- a/modules/rest/libraries/Rest_Exception.php
+++ b/modules/rest/libraries/Rest_Exception.php
@@ -1,15 +1,22 @@
-<?php defined('SYSPATH') OR die('No direct access allowed.');
+<?php defined("SYSPATH") or die("No direct script access.");
/**
- * Creates a "Page Not Found" exception.
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
*
- * $Id: Kohana_404_Exception.php 4679 2009-11-10 01:45:52Z isaiah $
+ * 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.
*
- * @package Core
- * @author Kohana Team
- * @copyright (c) 2007-2009 Kohana Team
- * @license http://kohanaphp.com/license
+ * 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.
*/
-
class Rest_Exception_Core extends Exception {
/**
* Set internal properties.
@@ -19,18 +26,6 @@ class Rest_Exception_Core extends Exception {
}
/**
- * Throws a new Rest exception.
- *
- * @throws Rest_Exception
- * @return void
- */
- public static function trigger($code, $text, $log_message=null) {
- $message = "$code: $text" . (!empty($log_message) ? "\n$log_message" : "");
- Kohana_Log::add("info", $message);
- throw new Rest_Exception($code, $text);
- }
-
- /**
* Sends the headers, to emulate server behavior.
*
* @return void
diff --git a/modules/tag/helpers/tag_rest.php b/modules/tag/helpers/tag_rest.php
index 29b74510..cd1ca6c6 100644
--- a/modules/tag/helpers/tag_rest.php
+++ b/modules/tag/helpers/tag_rest.php
@@ -60,7 +60,7 @@ class tag_rest_Core {
static function post($request) {
if (empty($request->arguments) || count($request->arguments) != 1 || empty($request->path)) {
- Rest_Exception::trigger(400, "Bad request");
+ throw new Rest_Exception(400, "Bad request");
}
$path = $request->path;
$tags = explode(",", $request->arguments[0]);
@@ -85,7 +85,7 @@ class tag_rest_Core {
static function put($request) {
if (empty($request->arguments[0]) || empty($request->new_name)) {
- Rest_Exception::trigger(400, "Bad request");
+ throw new Rest_Exception(400, "Bad request");
}
$name = $request->arguments[0];
@@ -105,7 +105,7 @@ class tag_rest_Core {
static function delete($request) {
if (empty($request->arguments[0])) {
- Rest_Exception::trigger(400, "Bad request");
+ throw new Rest_Exception(400, "Bad request");
}
$tags = explode(",", $request->arguments[0]);
if (!empty($request->path)) {