summaryrefslogtreecommitdiff
path: root/modules/rest/helpers/rest.php
blob: 93ad2bd301f0f41fe14d1c21d750e145cb43b41a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php defined("SYSPATH") or die("No direct script access.");
/**
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2009 Bharat Mediratta
 *
 * 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.
 */
class rest_Core {
  static function reply($data=array()) {
    Session::abort_save();

    if ($data) {
      print json_encode($data);
    }
  }

  static function set_active_user($access_token) {
    if (empty($access_token)) {
      identity::set_active_user(identity::guest());
      return;
    }

    $key = ORM::factory("user_access_token")
      ->where("access_key", "=", $access_token)
      ->find();

    if (!$key->loaded()) {
      throw new Rest_Exception("Forbidden", 403);
    }

    $user = identity::lookup_user($key->user_id);
    if (empty($user)) {
      throw new Rest_Exception("Forbidden", 403);
    }

    identity::set_active_user($user);
  }

  static function send_headers($exception) {
    header("HTTP/1.1 " . $exception->getCode() . " " . $exception->getMessage());
  }

  /**
   * Convert a REST url into an object.
   * Eg: "http://example.com/gallery3/index.php/rest/gallery/Family/Wedding" -> Item_Model
   *
   * @param string  the fully qualified REST url
   * @return mixed  the corresponding object (usually a model of some kind)
   */
  static function resolve($url) {
    $relative_url = substr($url, strlen(url::abs_site("rest")));
    $path = parse_url($relative_url, PHP_URL_PATH);
    $components = explode("/", $path, 3);

    $class = "$components[1]_rest";
    if (!method_exists($class, "resolve")) {
      throw new Kohana_404_Exception($url);
    }

    return call_user_func(array($class, "resolve"), !empty($components[2]) ? $components[2] : null);
  }

  /**
   * Return an absolute url used for REST resource location.
   * @param  string  module name (eg, "gallery", "tags")
   * @param  object  resource
   */
  static function url($module, $resource) {
    $class = "{$module}_rest";
    if (!method_exists($class, "url")) {
      throw new Exception("@todo MISSING REST CLASS: $class");
    }

    return call_user_func(array($class, "url"), $resource);
  }
}