/
item_rest.php
197 lines (175 loc) · 6.13 KB
/
item_rest.php
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
192
193
194
195
196
197
<?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 item_rest_Core {
/**
* For items that are collections, you can specify the following additional query parameters to
* query the collection. You can specify them in any combination.
*
* scope=direct
* only return items that are immediately under this one
* scope=all
* return items anywhere under this one
*
* name=<substring>
* only return items where the name contains this substring
*
* random=true
* return a single random item
*
* type=<comma separate list of photo, movie or album>
* limit the type to types in this list. eg, "type=photo,movie"
*/
static function get($request) {
$item = rest::resolve($request->url);
access::required("view", $item);
$p = $request->params;
if (isset($p->random)) {
$orm = item::random_query()->offset(0)->limit(1);
} else {
$orm = ORM::factory("item")->viewable();
}
if (empty($p->scope)) {
$p->scope = "direct";
}
if (!in_array($p->scope, array("direct", "all"))) {
throw new Rest_Exception("Bad Request", 400);
}
if ($p->scope == "direct") {
$orm->where("parent_id", "=", $item->id);
} else {
$orm->where("left_ptr", ">", $item->left_ptr);
$orm->where("right_ptr", "<", $item->right_ptr);
}
if (isset($p->name)) {
$orm->where("name", "LIKE", "%{$p->name}%");
}
if (isset($p->type)) {
$orm->where("type", "IN", explode(",", $p->type));
}
// Apply the item's sort order, using id as the tie breaker.
// See Item_Model::children()
$order_by = array($item->sort_column => $item->sort_order);
if ($item->sort_column != "id") {
$order_by["id"] = "ASC";
}
$orm->order_by($order_by);
$result = array(
"url" => $request->url,
"entity" => $item->as_restful_array(),
"relationships" => rest::relationships("item", $item));
if ($item->is_album()) {
$result["members"] = array();
foreach ($orm->find_all() as $child) {
$result["members"][] = rest::url("item", $child);
}
}
return $result;
}
static function put($request) {
$item = rest::resolve($request->url);
access::required("edit", $item);
if ($entity = $request->params->entity) {
// Only change fields from a whitelist.
foreach (array("album_cover", "captured", "description",
"height", "mime_type", "name", "parent", "rand_key", "resize_dirty",
"resize_height", "resize_width", "slug", "sort_column", "sort_order",
"thumb_dirty", "thumb_height", "thumb_width", "title", "view_count",
"width") as $key) {
switch ($key) {
case "album_cover":
if (property_exists($entity, "album_cover")) {
$album_cover_item = rest::resolve($entity->album_cover);
access::required("view", $album_cover_item);
$item->album_cover_item_id = $album_cover_item->id;
}
break;
case "parent":
if (property_exists($entity, "parent")) {
$parent = rest::resolve($entity->parent);
access::required("edit", $parent);
$item->parent_id = $parent->id;
}
break;
default:
if (property_exists($entity, $key)) {
$item->$key = $entity->$key;
}
}
}
}
$weight = 0;
if (isset($request->params->members)) {
foreach ($request->params->members as $url) {
$child = rest::resolve($url);
if ($child->parent_id == $item->id && $child->weight != $weight) {
$child->weight = $weight++;
$child->save();
}
}
}
$item->save();
}
static function post($request) {
$parent = rest::resolve($request->url);
access::required("edit", $parent);
$entity = $request->params->entity;
$item = ORM::factory("item");
switch ($entity->type) {
case "album":
$item->type = "album";
$item->parent_id = $parent->id;
$item->name = $entity->name;
$item->title = isset($entity->title) ? $entity->title : $name;
$item->description = isset($entity->description) ? $entity->description : null;
$item->slug = isset($entity->slug) ? $entity->slug : null;
$item->save();
break;
case "photo":
case "movie":
$item->type = $entity->type;
$item->parent_id = $parent->id;
$item->set_data_file($request->file);
$item->name = $entity->name;
$item->title = isset($entity->title) ? $entity->title : $entity->name;
$item->description = isset($entity->description) ? $entity->description : null;
$item->slug = isset($entity->slug) ? $entity->slug : null;
$item->save();
break;
default:
throw new Rest_Exception("Invalid type: $entity->type", 400);
}
return array("url" => rest::url("item", $item));
}
static function delete($request) {
$item = rest::resolve($request->url);
access::required("edit", $item);
$item->delete();
}
static function resolve($id) {
$item = ORM::factory("item", $id);
if (!access::can("view", $item)) {
throw new Kohana_404_Exception();
}
return $item;
}
static function url($item) {
return url::abs_site("rest/item/{$item->id}");
}
}