PageRenderTime 115ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/3.0/modules/unrest/helpers/unrest_rest.php

http://github.com/gallery/gallery3-contrib
PHP | 308 lines | 239 code | 50 blank | 19 comment | 34 complexity | b127b6a9a1a27311322f18cb6edfb550 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1
  1. <?php defined("SYSPATH") or die("No direct script access.");
  2. class unrest_rest_Core {
  3. private static function resolveLimitOption($string)
  4. {
  5. $items = split(',', $string);
  6. if (count($items) == 1) { return $string; }
  7. return $items;
  8. }
  9. private static function getFreetextLimiters($request, $limit = array())
  10. {
  11. $likeMapping = array(
  12. 'name' => 'name',
  13. 'description' => 'description'
  14. );
  15. foreach ($likeMapping as $key => $col)
  16. {
  17. if (isset($request->params->$key)) { $limit[$col] = array('op' => 'LIKE', 'value' => '%' . $request->params->$key . '%'); }
  18. }
  19. return $limit;
  20. }
  21. private static function getBasicLimiters($request, $limit = array())
  22. {
  23. $directMapping = array(
  24. 'type' => 'type',
  25. 'id' => 'items.id',
  26. 'parent' => 'parent_id',
  27. 'mime' => 'mime_type');
  28. foreach ($directMapping as $key => $col)
  29. {
  30. if (isset($request->params->$key)) { $limit[$col] = array('op' => '=', 'value' => unrest_rest::resolveLimitOption($request->params->$key)); }
  31. }
  32. return $limit;
  33. }
  34. private static function albumsICanAccess()
  35. {
  36. $db = db::build();
  37. $gids = identity::group_ids_for_active_user();
  38. $q = $db->select('id')->from('items');
  39. foreach ($gids as $gid) { $q->or_where('view_' . $gid, '=', 1); }
  40. $q->where('type', '=', 'album');
  41. $permitted = array();
  42. foreach($q->execute() as $row) { $permitted[] = $row->id; }
  43. return $permitted;
  44. }
  45. static function queryLimitByPermission(&$query, $permitted)
  46. {
  47. $query->and_open()->and_open()->where('type', '=', 'album')->and_where('items.id', 'IN', $permitted)->close();
  48. $query->or_open()->where('type', '!=', 'album')->and_where('parent_id', 'IN', $permitted)->close()->close();
  49. }
  50. static function baseItemQuery($db)
  51. {
  52. $fields = array(
  53. 'items.id', 'title', 'album_cover_item_id', 'description', 'height', 'width', 'left_ptr', 'right_ptr',
  54. 'level', 'mime_type', 'name', 'owner_id', 'parent_id', 'relative_path_cache', 'relative_url_cache',
  55. 'resize_dirty', 'slug', 'sort_column', 'sort_order', 'thumb_dirty','thumb_height', 'view_1', 'type',
  56. 'resize_height', 'resize_width', 'thumb_height', 'thumb_width', 'slug', 'name', 'relative_path_cache'
  57. );
  58. $permfields = array('view_', 'view_full_', 'edit_', 'add_');
  59. foreach (identity::group_ids_for_active_user() as $album)
  60. {
  61. foreach ($permfields as $field)
  62. {
  63. $fields[] = $field . $album;
  64. }
  65. }
  66. return($db->select($fields)->from('items')->join('access_caches', 'access_caches.item_id', 'items.id'));
  67. /*
  68. return($db->select(array(
  69. 'id', 'title', 'album_cover_item_id', 'description', 'height', 'width', 'left_ptr', 'right_ptr',
  70. 'level', 'mime_type', 'name', 'owner_id', 'parent_id', 'relative_path_cache', 'relative_url_cache',
  71. 'resize_dirty', 'slug', 'sort_column', 'sort_order', 'thumb_dirty','thumb_height', 'view_1', 'type',
  72. 'resize_height', 'resize_width', 'thumb_height', 'thumb_width', 'slug', 'name', 'relative_path_cache'
  73. ))->from('items'));
  74. */
  75. }
  76. static function queryLimitByLimiter(&$query, $limit)
  77. {
  78. foreach ($limit as $key => $block)
  79. {
  80. if (gettype($block['value']) == 'array') { $query->and_where($key, 'IN', $block['value']); }
  81. else { $query->and_where($key, $block['op'], $block['value']); }
  82. }
  83. }
  84. static function getDisplayOptions($request)
  85. {
  86. if (isset($request->params->display)) {
  87. return(split(',', $request->params->display));
  88. } else {
  89. return(array('uiimage','uitext','ownership','members'));
  90. };
  91. }
  92. static function queryOrder(&$query, $request)
  93. {
  94. if (isset($request->params->order)) {
  95. $order = $request->params->order;
  96. $direction = 'asc';
  97. if (isset($request->params->direction))
  98. {
  99. if ($request->params->direction == 'desc') { $direction = 'desc'; }
  100. }
  101. switch ($order)
  102. {
  103. case 'tree':
  104. $query->order_by(array('level' => 'ASC', 'left_ptr' => 'ASC'));
  105. break;
  106. case 'created':
  107. $query->order_by(array('created' => $direction));
  108. break;
  109. case 'updated':
  110. $query->order_by(array('updated' => $direction));
  111. break;
  112. case 'views':
  113. $query->order_by(array('view_count' => $direction));
  114. break;
  115. case 'type':
  116. $query->order_by(array('type' => $direction));
  117. break;
  118. }
  119. }
  120. }
  121. static function addChildren($request, $db, $filler, $permitted, $display, &$return, $rest_base)
  122. {
  123. $children = $db->select('parent_id', 'id')->from('items')->where('parent_id', 'IN', $filler['children_of']);
  124. if (isset($request->params->childtypes))
  125. {
  126. $types = split(',', $request->params->childtypes);
  127. $children->where('type', 'IN', $types);
  128. }
  129. /* We shouldn't have any albums we don't have access to by default in this query, but just in case.. */
  130. unrest_rest::queryLimitByPermission(&$children, $permitted);
  131. $childBlock = array();
  132. foreach($children->execute() as $item)
  133. {
  134. $childBlock[$item->parent_id][] = intval($item->id);
  135. }
  136. foreach ($return as &$data)
  137. {
  138. if (array_key_exists($data['entity']['id'], $childBlock))
  139. {
  140. if (in_array('terse', $display)) {
  141. $data['members'] = $childBlock[ $data['id'] ];
  142. }
  143. else {
  144. $members = array();
  145. foreach ($childBlock[ $data['entity']['id'] ] as $child) {
  146. $members[] = unrest_rest::makeRestURL('item', $child, $rest_base);
  147. }
  148. $data['members'] = $members;
  149. }
  150. }
  151. else
  152. {
  153. $data['members'] = array();
  154. }
  155. }
  156. }
  157. private static function makeRestURL($resource, $identifier, $base)
  158. {
  159. return $base . '/' . $resource . '/' . $identifier;
  160. }
  161. public static function size_url($size, $relative_path_cache, $type, $file_base) {
  162. $base = $file_base . 'var/' . $size . '/' . $relative_path_cache;
  163. if ($type == 'photo') {
  164. return $base;
  165. } else if ($type == 'album') {
  166. return $base . "/.album.jpg";
  167. } else if ($type == 'movie') {
  168. // Replace the extension with jpg
  169. return preg_replace("/...$/", "jpg", $base);
  170. }
  171. }
  172. static function get($request) {
  173. $db = db::build();
  174. $start = microtime(true);
  175. $rest_base = url::abs_site("rest");
  176. $file_base = url::abs_file(''); #'var/' . $size . '/'
  177. /* Build basic limiters */
  178. $limit = unrest_rest::getBasicLimiters($request);
  179. $limit = unrest_rest::getFreetextLimiters($request,$limit);
  180. /* Build numeric limiters */
  181. /* ...at some point. */
  182. /* Figure out an array of albums we got permissions to access */
  183. $permitted = unrest_rest::albumsICanAccess();
  184. $display = unrest_rest::getDisplayOptions($request);
  185. $items = unrest_rest::baseItemQuery($db);
  186. /*
  187. Introduce some WHERE statements that'll make sure that we don't get to see stuff we
  188. shouldn't be seeing.
  189. */
  190. unrest_rest::queryLimitByPermission(&$items, $permitted);
  191. unrest_rest::queryLimitByLimiter(&$items, $limit);
  192. unrest_rest::queryOrder(&$items, $request);
  193. $return = array();
  194. $filler = array();
  195. $relationshipCandidates = array();
  196. foreach($items->execute() as $item)
  197. {
  198. $data = array(
  199. 'id' => intval($item->id),
  200. 'parent' => intval($item->parent_id),
  201. 'owner_id' => intval($item->{'owner_id'}),
  202. 'public' => ($item->view_1)?true:false,
  203. 'type' => $item->type // Grmbl
  204. );
  205. if (in_array('uitext', $display)) {
  206. $ui = array(
  207. 'title' => $item->title,
  208. 'description' => $item->description,
  209. 'name' => $item->name,
  210. 'slug' => $item->slug
  211. );
  212. $data = array_merge($data, $ui);
  213. }
  214. if (in_array('uiimage', $display)) {
  215. $ui = array(
  216. 'height' => $item->height,
  217. 'width' => $item->width,
  218. 'resize_height' => $item->resize_height,
  219. 'resize_width' => $item->resize_width,
  220. 'thumb_height' => $item->resize_height,
  221. 'thumb_width' => $item->resize_width
  222. );
  223. $ui['thumb_url_public'] = unrest_rest::size_url('thumbs', $item->relative_path_cache, $item->type, $file_base);
  224. $public = $item->view_1?true:false;
  225. $fullPublic = $item->view_full_1?true:false;
  226. if ($item->type != 'album')
  227. {
  228. $ui['file_url'] = unrest_rest::makeRestURL('data', $item->id . '?size=full', $rest_base);
  229. $ui['thumb_url'] = unrest_rest::makeRestURL('data', $item->id . '?size=thumb', $rest_base);
  230. $ui['resize_url'] = unrest_rest::makeRestURL('data', $item->id . '?size=resize', $rest_base);
  231. if ($public) {
  232. $ui['resize_url_public'] = unrest_rest::size_url('resizes', $item->relative_path_cache, $item->type, $file_base);
  233. if ($fullPublic) {
  234. $ui['file_url_public'] = unrest_rest::size_url('albums', $item->relative_path_cache, $item->type, $file_base);
  235. }
  236. }
  237. }
  238. $data = array_merge($data, $ui);
  239. }
  240. if (in_array('members', $display)) {
  241. $filler['children_of'][] = $item->id;
  242. }
  243. $return[] = array(
  244. 'url' => unrest_rest::makeRestURL('item', $item->id, $rest_base ),
  245. 'entity' => $data
  246. );
  247. }
  248. /* Do we need to fetch children? */
  249. if (array_key_exists('children_of', $filler))
  250. {
  251. unrest_rest::addChildren($request, $db, $filler, $permitted, $display, &$return, $rest_base);
  252. }
  253. $end = microtime(true);
  254. error_log("Inner " . ($end - $start) . " seconds taken");
  255. return $return;
  256. }
  257. }
  258. ?>