summaryrefslogtreecommitdiff
path: root/modules/rest/helpers/rest.php
blob: 676c10c3b1c1f065117896d35842e2de99d8ceac (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php defined("SYSPATH") or die("No direct script access.");
/**
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2010 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 {
  const API_VERSION = "3.0";

  static function reply($data=array()) {
    Session::instance()->abort_save();

    header("X-Gallery-API-Version: " . rest::API_VERSION);
    switch (Input::instance()->get("output", "json")) {
    case "json":
      json::reply($data);
      break;

    case "jsonp":
      if (!($callback = Input::instance()->get("callback", ""))) {
        throw new Rest_Exception(
          "Bad Request", 400, array("errors" => array("callback" => "missing")));
      }

      if (preg_match('/^[$A-Za-z_][0-9A-Za-z_]*$/', $callback) == 1) {
        header("Content-type: application/javascript; charset=UTF-8");
        print "$callback(" . json_encode($data) . ")";
      } else {
        throw new Rest_Exception(
          "Bad Request", 400, array("errors" => array("callback" => "invalid")));
      }
      break;

    case "html":
      header("Content-type: text/html; charset=UTF-8");
      if ($data) {
        $html = preg_replace(
          "#([\w]+?://[\w]+[^ \'\"\n\r\t<]*)#ise", "'<a href=\"\\1\" >\\1</a>'",
          var_export($data, 1));
      } else {
        $html = t("Empty response");
      }
      print "<pre>$html</pre>";
      if (Session::instance()->get("profiler", false)) {
        Profiler::enable();
        $profiler = new Profiler();
        $profiler->render();
      }
      break;

    default:
      throw new Rest_Exception("Bad Request", 400);
    }
  }

  static function set_active_user($access_key) {
    if (empty($access_key)) {
      if (module::get_var("rest", "allow_guest_access")) {
        identity::set_active_user(identity::guest());
        return;
      } else {
        throw new Rest_Exception("Forbidden", 403);
      }
    }

    $key = ORM::factory("user_access_key")
      ->where("access_key", "=", $access_key)
      ->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 reset_access_key() {
    $key = ORM::factory("user_access_key")
      ->where("user_id", "=", identity::active_user()->id)
      ->find();
    if ($key->loaded()) {
      $key->delete();
    }
    return rest::access_key();
  }

  static function access_key() {
    $key = ORM::factory("user_access_key")
      ->where("user_id", "=", identity::active_user()->id)
      ->find();

    if (!$key->loaded()) {
      $key->user_id = identity::active_user()->id;
      $key->access_key = md5(random::hash() . access::private_key());
      $key->save();
    }

    return $key->access_key;
  }

  /**
   * Convert a REST url into an object.
   * Eg:
   *   http://example.com/gallery3/index.php/rest/item/35          -> Item_Model
   *   http://example.com/gallery3/index.php/rest/tag/16           -> Tag_Model
   *   http://example.com/gallery3/index.php/rest/tagged_item/1,16 -> [Tag_Model, Item_Model]
   *
   * @param string  the fully qualified REST url
   * @return mixed  the corresponding object (usually a model of some kind)
   */
  static function resolve($url) {
    if ($suffix = Kohana::config('core.url_suffix')) {
      $relative_url = substr($url, strlen(url::abs_site("rest")) - strlen($suffix));
    } else {
      $relative_url = substr($url, strlen(url::abs_site("rest")));
    }

    $path = parse_url($relative_url, PHP_URL_PATH);
    $components = explode("/", $path, 3);

    if (count($components) != 3) {
      throw new Kohana_404_Exception($url);
    }

    $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  resource type (eg, "item", "tag")
   * @param  object  resource
   */
  static function url() {
    $args = func_get_args();
    $resource_type = array_shift($args);

    $class = "{$resource_type}_rest";
    if (!method_exists($class, "url")) {
      throw new Rest_Exception("Bad Request", 400);
    }

    $url = call_user_func_array(array($class, "url"), $args);
    if (Input::instance()->get("output") == "html") {
      if (strpos($url, "?") === false) {
        $url .= "?output=html";
      } else {
        $url .= "&output=html";
      }
    }
    return $url;
  }

  static function relationships($resource_type, $resource) {
    $results = array();
    foreach (module::active() as $module) {
      foreach (glob(MODPATH . "{$module->name}/helpers/*_rest.php") as $filename) {
        $class = str_replace(".php", "", basename($filename));
        if (method_exists($class, "relationships")) {
          if ($tmp = call_user_func(array($class, "relationships"), $resource_type, $resource)) {
            $results = array_merge($results, $tmp);
          }
        }
      }
    }

    return $results;
  }
}