/fsn-site-central/mediatheque/include/ws_functions/pwg.permissions.php

https://gitlab.com/team_fsn/fsn-php · PHP · 245 lines · 168 code · 27 blank · 50 comment · 25 complexity · 77bb37019f0047057b228ecf3465def1 MD5 · raw file

  1. <?php
  2. // +-----------------------------------------------------------------------+
  3. // | Piwigo - a PHP based photo gallery |
  4. // +-----------------------------------------------------------------------+
  5. // | Copyright(C) 2008-2016 Piwigo Team http://piwigo.org |
  6. // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net |
  7. // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick |
  8. // +-----------------------------------------------------------------------+
  9. // | This program is free software; you can redistribute it and/or modify |
  10. // | it under the terms of the GNU General Public License as published by |
  11. // | the Free Software Foundation |
  12. // | |
  13. // | This program is distributed in the hope that it will be useful, but |
  14. // | WITHOUT ANY WARRANTY; without even the implied warranty of |
  15. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
  16. // | General Public License for more details. |
  17. // | |
  18. // | You should have received a copy of the GNU General Public License |
  19. // | along with this program; if not, write to the Free Software |
  20. // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
  21. // | USA. |
  22. // +-----------------------------------------------------------------------+
  23. /**
  24. * API method
  25. * Returns permissions
  26. * @param mixed[] $params
  27. * @option int[] cat_id (optional)
  28. * @option int[] group_id (optional)
  29. * @option int[] user_id (optional)
  30. */
  31. function ws_permissions_getList($params, &$service)
  32. {
  33. $my_params = array_intersect(array_keys($params), array('cat_id','group_id','user_id'));
  34. if (count($my_params) > 1)
  35. {
  36. return new PwgError(WS_ERR_INVALID_PARAM, 'Too many parameters, provide cat_id OR user_id OR group_id');
  37. }
  38. $cat_filter = '';
  39. if (!empty($params['cat_id']))
  40. {
  41. $cat_filter = 'WHERE cat_id IN('. implode(',', $params['cat_id']) .')';
  42. }
  43. $perms = array();
  44. // direct users
  45. $query = '
  46. SELECT user_id, cat_id
  47. FROM '. USER_ACCESS_TABLE .'
  48. '. $cat_filter .'
  49. ;';
  50. $result = pwg_query($query);
  51. while ($row = pwg_db_fetch_assoc($result))
  52. {
  53. if (!isset($perms[ $row['cat_id'] ]))
  54. {
  55. $perms[ $row['cat_id'] ]['id'] = intval($row['cat_id']);
  56. }
  57. $perms[ $row['cat_id'] ]['users'][] = intval($row['user_id']);
  58. }
  59. // indirect users
  60. $query = '
  61. SELECT ug.user_id, ga.cat_id
  62. FROM '. USER_GROUP_TABLE .' AS ug
  63. INNER JOIN '. GROUP_ACCESS_TABLE .' AS ga
  64. ON ug.group_id = ga.group_id
  65. '. $cat_filter .'
  66. ;';
  67. $result = pwg_query($query);
  68. while ($row = pwg_db_fetch_assoc($result))
  69. {
  70. if (!isset($perms[ $row['cat_id'] ]))
  71. {
  72. $perms[ $row['cat_id'] ]['id'] = intval($row['cat_id']);
  73. }
  74. $perms[ $row['cat_id'] ]['users_indirect'][] = intval($row['user_id']);
  75. }
  76. // groups
  77. $query = '
  78. SELECT group_id, cat_id
  79. FROM '. GROUP_ACCESS_TABLE .'
  80. '. $cat_filter .'
  81. ;';
  82. $result = pwg_query($query);
  83. while ($row = pwg_db_fetch_assoc($result))
  84. {
  85. if (!isset($perms[ $row['cat_id'] ]))
  86. {
  87. $perms[ $row['cat_id'] ]['id'] = intval($row['cat_id']);
  88. }
  89. $perms[ $row['cat_id'] ]['groups'][] = intval($row['group_id']);
  90. }
  91. // filter by group and user
  92. foreach ($perms as $cat_id => &$cat)
  93. {
  94. if (isset($filters['group_id']))
  95. {
  96. if (empty($cat['groups']) or count(array_intersect($cat['groups'], $params['group_id'])) == 0)
  97. {
  98. unset($perms[$cat_id]);
  99. continue;
  100. }
  101. }
  102. if (isset($filters['user_id']))
  103. {
  104. if (
  105. (empty($cat['users_indirect']) or count(array_intersect($cat['users_indirect'], $params['user_id'])) == 0)
  106. and (empty($cat['users']) or count(array_intersect($cat['users'], $params['user_id'])) == 0)
  107. ) {
  108. unset($perms[$cat_id]);
  109. continue;
  110. }
  111. }
  112. $cat['groups'] = !empty($cat['groups']) ? array_values(array_unique($cat['groups'])) : array();
  113. $cat['users'] = !empty($cat['users']) ? array_values(array_unique($cat['users'])) : array();
  114. $cat['users_indirect'] = !empty($cat['users_indirect']) ? array_values(array_unique($cat['users_indirect'])) : array();
  115. }
  116. unset($cat);
  117. return array(
  118. 'categories' => new PwgNamedArray(
  119. array_values($perms),
  120. 'category',
  121. array('id')
  122. )
  123. );
  124. }
  125. /**
  126. * API method
  127. * Add permissions
  128. * @param mixed[] $params
  129. * @option int[] cat_id
  130. * @option int[] group_id (optional)
  131. * @option int[] user_id (optional)
  132. * @option bool recursive
  133. */
  134. function ws_permissions_add($params, &$service)
  135. {
  136. if (get_pwg_token() != $params['pwg_token'])
  137. {
  138. return new PwgError(403, 'Invalid security token');
  139. }
  140. include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
  141. if (!empty($params['group_id']))
  142. {
  143. $cat_ids = get_uppercat_ids($params['cat_id']);
  144. if ($params['recursive'])
  145. {
  146. $cat_ids = array_merge($cat_ids, get_subcat_ids($params['cat_id']));
  147. }
  148. $query = '
  149. SELECT id
  150. FROM '. CATEGORIES_TABLE .'
  151. WHERE id IN ('. implode(',', $cat_ids) .')
  152. AND status = \'private\'
  153. ;';
  154. $private_cats = array_from_query($query, 'id');
  155. $inserts = array();
  156. foreach ($private_cats as $cat_id)
  157. {
  158. foreach ($params['group_id'] as $group_id)
  159. {
  160. $inserts[] = array(
  161. 'group_id' => $group_id,
  162. 'cat_id' => $cat_id
  163. );
  164. }
  165. }
  166. mass_inserts(
  167. GROUP_ACCESS_TABLE,
  168. array('group_id','cat_id'),
  169. $inserts,
  170. array('ignore'=>true)
  171. );
  172. }
  173. if (!empty($params['user_id']))
  174. {
  175. if ($params['recursive']) $_POST['apply_on_sub'] = true;
  176. add_permission_on_category($params['cat_id'], $params['user_id']);
  177. }
  178. return $service->invoke('pwg.permissions.getList', array('cat_id'=>$params['cat_id']));
  179. }
  180. /**
  181. * API method
  182. * Removes permissions
  183. * @param mixed[] $params
  184. * @option int[] cat_id
  185. * @option int[] group_id (optional)
  186. * @option int[] user_id (optional)
  187. */
  188. function ws_permissions_remove($params, &$service)
  189. {
  190. if (get_pwg_token() != $params['pwg_token'])
  191. {
  192. return new PwgError(403, 'Invalid security token');
  193. }
  194. include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
  195. $cat_ids = get_subcat_ids($params['cat_id']);
  196. if (!empty($params['group_id']))
  197. {
  198. $query = '
  199. DELETE
  200. FROM '. GROUP_ACCESS_TABLE .'
  201. WHERE group_id IN ('. implode(',', $params['group_id']).')
  202. AND cat_id IN ('. implode(',', $cat_ids).')
  203. ;';
  204. pwg_query($query);
  205. }
  206. if (!empty($params['user_id']))
  207. {
  208. $query = '
  209. DELETE
  210. FROM '. USER_ACCESS_TABLE .'
  211. WHERE user_id IN ('. implode(',', $params['user_id']) .')
  212. AND cat_id IN ('. implode(',', $cat_ids) .')
  213. ;';
  214. pwg_query($query);
  215. }
  216. return $service->invoke('pwg.permissions.getList', array('cat_id'=>$params['cat_id']));
  217. }
  218. ?>