PageRenderTime 71ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/s3db3.5.10/s3dbcore/CORElist.php

https://code.google.com/p/s3db/
PHP | 349 lines | 223 code | 102 blank | 24 comment | 26 complexity | b28573c78a3118648295c55849ca94f6 MD5 | raw file
  1. <?php
  2. function CORElist($C)
  3. {#function CORElist lists all the resources in the element downstream of the "uid" in the s3core structure. For example, if element == rules, then s3list will list all the rules on a given project_id, provided project_id is specified. If element is statements, then s3list wil be expecting rule_id and resource_id or just one of them
  4. #Syntax CORElist(compact($child, array('rule_id'=>$rule_id, 'item_id'=>$item_id), $db)); where child is the name of the elements to retrieve; parante_ids is an array where the type of id is specified in the key
  5. $regexp = $GLOBALS['regexp'];
  6. $dbstruct = $GLOBALS['dbstruct'];
  7. $messages = $GLOBALS['messages'];
  8. extract($C);
  9. extract($parent_ids);
  10. $from = $child;
  11. if (!$from) {
  12. $from = 'projects';
  13. }
  14. if(!$select)
  15. $select = '*';
  16. $equality = '='; #by default, equality on query end be this, unless specified that equality should be a regular expression
  17. #Error messages
  18. $syntax_message = "Please provide all the necessary fields. For syntax instructions refer to <a href='http://www.s3db.org/documentation.html'>S3DB Documentation</a>";
  19. $success = '<error>0</error><message>'.$from.' '.$action.'ed '.$element_id.'</message>';
  20. $not_a_query = '<error>1</error><message>'.$from.' is not a valid S3element. Valid elements: groups, users, keys, projects, rules, statements, collections, items, rulelog";</message>';
  21. $something_went_wrong = '<error>2</error><message>Failed to '.$action.' '.$from.'</message>';
  22. $something_missing = '<error>3</error><message>'.$syntax_message.'</message>';
  23. $repeating_action = '<error>4</error>';
  24. $no_permission_message = '<error>5</error>';
  25. $something_does_not_exist = '<error>5</error>';
  26. $wrong_query_for_purpose = '<error>6</error>';
  27. $wrong_input = '<error>7</error>';
  28. $no_output = '<error>8</error>';
  29. #alternative IDs that can be used for the query
  30. $alt = array('keys'=>array('key_id'),
  31. 'rulelog'=>array('rule_id'),
  32. 'users'=>array('group_id', 'project_id'),
  33. 'groups'=>array('user_id'),
  34. 'projects'=>array('user_id'),
  35. 'collections'=>array('project_id', 'rule_id'),
  36. 'rules'=>array('project_id', 'collection_id', 'subject_id', 'object_id'),
  37. 'items'=>array('collection_id', 'project_id'),
  38. 'statements'=>array('rule_id', 'item_id', 'collection_id', 'project_id'),
  39. 'files'=>array('statement_id', 'rule_id', 'item_id', 'project_id'));
  40. #if from is not one of these elements, sent the user back, query is invalid!
  41. if (!in_array($from, array_keys($alt))) {
  42. #check if user is inputing a sigular of one of the alt plurals
  43. $plurals = array_keys($alt);
  44. $singulars = array('key', 'rulelog', 'user', 'group','project', 'collection', 'rule', 'item', 'statement', 'file');
  45. $from = str_replace($singulars, $plurals, $from);
  46. $cols = $dbstruct[$from];
  47. #if still not in array, definitelly exit;
  48. if (!in_array($from, array_keys($alt))) {
  49. return ($not_a_query);
  50. }
  51. }
  52. #now replace on "where" the correct s3db names
  53. $s3map = array('users'=>array('user_id'=>'account_id',
  54. 'login'=>'account_lid',
  55. 'password'=>'account_pwd',
  56. 'username'=>'account_uname',
  57. 'email'=>'account_email',
  58. 'phone'=>'account_phone',
  59. 'address'=>'addr1',
  60. 'address2'=>'addr2',
  61. 'city'=>'city',
  62. 'state'=>'state',
  63. 'postal_code'=>'postal_code',
  64. 'country'=>'country'),
  65. 'groups'=>array('group_id'=>'account_id',
  66. 'groupname'=>'account_lid'),
  67. 'keys'=>array(),
  68. 'accesslog'=>array('account_lid'=>'login_id', 'time'=>'login_timestamp',),
  69. 'projects'=>array(),
  70. 'project'=>array(),
  71. 'items'=>array('collection_id'=>'resource_class_id',
  72. 'item_id'=>'resource_id'),
  73. 'item'=>array('collection_id'=>'resource_class_id',
  74. 'item_id'=>'resource_id'),
  75. 'collections'=>array('collection_id'=>'resource_id'),
  76. 'collection'=>array('collection_id'=>'resource_id'),
  77. 'rules'=>array(),
  78. 'rule'=>array(),
  79. 'statements'=>array('item_id'=>'resource_id'),
  80. 'statement'=>array('item_id'=>'resource_id'),
  81. 'files'=>array());
  82. foreach ($alt[$from] as $s3id) {
  83. $s3dbId = $s3map[$from][$s3id];
  84. if ($s3dbId=='') {
  85. $s3dbId = $s3id;
  86. }
  87. if ($parent_ids[$s3id]!='') {
  88. #does it exist? What sort of resource is this? Type of id should be identified in the first letter (collection_id is C, rule_id is R...)
  89. $CRISP = strtoupper(substr($s3id, 0, 1));
  90. $id = $CRISP.$parent_ids[$s3id];
  91. $info[$parent_ids[$s3id]] = s3info(str_replace('_id', '', $s3id), $parent_ids[$s3id], $db);
  92. if (!is_array($info)) {
  93. return ($something_does_not_exist.'<message>'.$s3id.' '.$parent_ids[$s3id].' does not exist</message>');
  94. }
  95. #does user have permission on this/these resources?
  96. $query_end .= " and ".$s3dbId." ".$equality." '".$parent_ids[$s3id]."'";
  97. }
  98. }
  99. $toreplace = array_keys($s3map[$from]);
  100. $replacements = array_values($s3map[$from]);
  101. $s3ql['select'] = str_replace($toreplace, $replacements, $query_end);
  102. #all queries will run AS IF ADMIN WAS RUNNING THEM
  103. switch ($from) {
  104. case 'keys':{
  105. $table = 'access_keys';
  106. $required = "expires > '".date('Y-m-d')."'";
  107. if ($user_id!='1') {
  108. $required .= " and (account_id = '".$user_id."')";
  109. }
  110. break;
  111. }
  112. case 'rulelog':{
  113. $table = 'rule_change_log';
  114. $required = "rule_id !=''";
  115. break;
  116. }
  117. case 'users':{#expecting group_id or project_id
  118. #remove password from query fields
  119. $table = 'account';
  120. $required = "account_type = 'u' and account_status = 'A'";
  121. break;
  122. }
  123. case 'groups':{
  124. $table = 'account';
  125. $required = "account_type = 'g' and account_status = 'A'";
  126. break;
  127. }
  128. case 'projects':{
  129. $table = 'project';
  130. $required = "project_status = 'A'";
  131. #if user is not admin, retrict this query to the projects user can view by extending queryend
  132. if ($user_id!='1') {
  133. $required .= " and (project_owner = '".$user_id."' or project_id in (select acl_project_id from s3db_project_acl where acl_account = '".$user_id."' and acl_rights!='0'))";
  134. }
  135. break;
  136. }
  137. case 'collections':{
  138. #$table = 'resource';
  139. $table = 'resource, s3db_rule';
  140. $required = "iid = '0' and s3db_rule.status = 'A'";
  141. $select = str_replace('project_id', 's3db_rule.project_id', $select);
  142. $select = str_replace('notes', 's3db_resource.notes', $select);
  143. if ($parent_ids['project_id']!='') {
  144. $query_end = str_replace("and project_id = '".$project_id."'", "and (entity = subject and verb = 'has UID' and object = 'UID' and s3db_resource.project_id = s3db_rule.project_id and (s3db_rule.project_id = '".$project_id."' or s3db_rule.permission ".$regexp." '(_|^)".$project_id."_'))", $query_end);
  145. }
  146. #restrict the query to the rules where user is allowed
  147. $query_end = str_replace("and project_id ".$regexp." '".$user_project_list."'", "and subject = entity and object = 'UID' and s3db_rule.project_id = s3db_resource.project_id and (s3db_rule.project_id ".$regexp." '".$user_project_list."' or s3db_rule.permission ".$regexp." '".$user_permission_list."')", $query_end);
  148. break;
  149. }
  150. case 'items':{
  151. $table = 'resource';
  152. $required = "iid = '1' and status = 'A'";
  153. #to avoid having to call s3list again, created this function that simulates finding user collections
  154. $classes = findUserClasses($user_id, $db);
  155. if (!is_array($classes)) {
  156. return ($no_output.'<message>User does not have permission in any collections</message>');
  157. }
  158. $classes_list = create_class_id_list($classes);
  159. $query_end = str_replace("and project_id ".$regexp." '".$user_project_list."'", "and resource_class_id ".$regexp." '".$classes_list."'", $query_end);
  160. break;
  161. }
  162. case 'rules':{
  163. $table = 'rule';
  164. $required = "status ='A'";
  165. if ($parent_ids['project_id']!='') {
  166. $query_end = str_replace("and project_id = '".$project_id."'", "and (project_id ".$regexp." '^".$project_id."$' or permission ".$regexp." '(_|^)".$project_id."_')", $query_end);
  167. if ($parent_ids['collection_id']!='') {
  168. $class_info = s3info('collection', $parent_ids['collection_id'], $db);
  169. $query_end = str_replace("and collection_id = '".$parent_ids['collection_id']."'", "and (subject_id = '".$parent_ids['collection_id']."' or object_id = '".$parent_ids['collection_id']."')", $query_end);
  170. }
  171. }
  172. elseif ($parent_ids['collection_id']!='') { #no project_id but w/ collection_id. If no project_id is indicated, it will have to find the correct subjects (which can be repeated if queried on several projects)
  173. $class_info = s3info('collection', $parent_ids['collection_id'], $db);
  174. #$query_end = str_replace("and collection_id = '".$parent_ids['collection_id']."'", "and (subject_id = '".$parent_ids['collection_id']."' or (subject = '".$class_info['entity']."' and project_id = '".$class_info['project_id']."'))",$query_end); #all that don't belong to this project will have to be queried by collection_id.
  175. $query_end = str_replace("and collection_id = '".$parent_ids['collection_id']."'", "and (subject_id = '".$parent_ids['collection_id']."' or object_id = '".$parent_ids['collection_id']."')",$query_end);
  176. }
  177. else {
  178. $query_end = str_replace("and project_id ".$regexp." '".$user_project_list."'", " and (project_id ".$regexp." '".$user_project_list."' or permission ".$regexp." '".$user_permission_list."')", $query_end);
  179. }
  180. break;
  181. }
  182. case 'statements':{
  183. $table = 'statement';
  184. $required = "status = 'A'";
  185. if($parent_ids['collection_id']!='')
  186. {#find all the statements in items that belong to this collection.
  187. $instance_ids = findClassInstances($parent_ids['collection_id'], $db);
  188. $rule_ids = findClassRules($parent_ids['collection_id'], $db);#these would be all the rules that use the collection as either subject or object
  189. $instance_list = create_list($instance_ids);
  190. $rule_list = create_list($rule_ids);
  191. if(is_array($instance_ids) && is_array($rule_ids))
  192. {
  193. $query_end = str_replace("and collection_id = '".$parent_ids['collection_id']."'", "and (resource_id ".$regexp." '".$instance_list."' or rule_id ".$regexp." '".$rule_list."')", $query_end);
  194. }
  195. elseif(is_array($instance_ids) && !is_array($rule_ids))
  196. {
  197. $query_end = str_replace("and collection_id = '".$parent_ids['collection_id']."'", "and (resource_id ".$regexp." '".$instance_list."')", $query_end);
  198. }
  199. elseif(!is_array($instance_ids) && is_array($rule_ids))
  200. {
  201. $query_end = str_replace("and collection_id = '".$parent_ids['collection_id']."'", "and (rule_id ".$regexp." '".$rule_list."')", $query_end);
  202. }
  203. }
  204. }
  205. break;
  206. }
  207. #POSSIBLY MOVE THIS PART TO A SEPARATE FUNCTION!!
  208. $sql = "select ".$select." from s3db_".$table." where ".$required." ".$query_end.$order_by;
  209. #echo $sql.'<br>';
  210. #exit;
  211. $db->query($sql, __LINE__, __FILE__);
  212. $cols = $dbstruct[$from];
  213. while($db->next_record())
  214. {
  215. $resultStr .= "\$data[] = Array(";
  216. if ($extracol!='')
  217. $resultStr .= "'".$extracol."'=>'".$db->f($SQLfun)."',";
  218. foreach ($cols as $col)
  219. {
  220. #if($db->f($col)!='')
  221. {
  222. $resultStr .= "'".$col."'=>'".addslashes($db->f($col))."'";
  223. if($col != end($cols))
  224. $resultStr .= ",";
  225. }
  226. }
  227. $resultStr .= ");";
  228. }
  229. #evaluate the long string
  230. eval($resultStr);
  231. #echo '<pre>';print_r($data);
  232. if (is_array($data)) {
  233. if (!$nomap) {#include stuff relevant for each element
  234. foreach ($data as $element_info) {
  235. #$element_info['dataAcl'] = instanceAcl(array('instance_info'=>$element_info, 'user_id'=>$user_id, 'db'=>$db));
  236. $data1[] = include_all(array('elements'=>$from, 'element_info'=>$element_info, 'user_id'=>$user_id, 'db'=>$db));
  237. }
  238. $data = $data1;
  239. }
  240. }
  241. else {
  242. $data = $no_output.'<message>Your query returned no results</message>';
  243. }
  244. #echo '<pre>';print_r($data);
  245. return ($data);
  246. }
  247. ?>