PageRenderTime 63ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/s3db3.5.10/s3dbcore/S3QLaction1.php

https://code.google.com/p/s3db/
PHP | 1626 lines | 966 code | 410 blank | 250 comment | 217 complexity | 210e05ea8c9d60cdf21e5e74b414cc74 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. function S3QLaction($s3ql)
  3. {
  4. extract($s3ql);
  5. #grab a few relevant varuales
  6. $regexp = $GLOBALS['regexp'];
  7. $dbstruct = $GLOBALS['dbstruct'];
  8. #map a few vairables
  9. $s3map = $GLOBALS['s3map'];
  10. $format = $s3ql['format'];
  11. #Error messages
  12. extract($GLOBALS['messages']);
  13. #database and user identification
  14. if(!is_object($db))
  15. {$db = $_SESSION['db'];
  16. }
  17. $key=($_REQUEST['key'])?$_REQUEST['key']:$s3ql['key'];
  18. $user_id = ($user_id)?$user_id:$_SESSION['user']['account_id'];
  19. $user_info = s3info('users', $user_id, $db);
  20. if (!$user_id && !$db) {
  21. if (!$key) {
  22. return (formatReturn($GLOBALS['error_codes']['no_permission_message'], 'Please specify user_id and db or a key', $format,''));
  23. }
  24. #re-chekc if user provided is the same for key provided
  25. }
  26. $s3ql = array_diff_key($s3ql, array('db'=>'', 'user_id'=>'')); #take out from the array what needed to be included for wihitn S3DB queries
  27. if ($s3ql['update']!='') {
  28. $s3ql['edit'] = $s3ql['update'];#update is closer to SQL, although original was edit. Must keep edit to be backward compatible
  29. $s3ql=array_filter(array_diff_key($s3ql, array('update'=>1)));
  30. }
  31. #identify the action
  32. $possible_actions = array('insert', 'edit', 'delete', 'select', 'update', 'grant');
  33. foreach ($possible_actions as $someaction) {
  34. if ($s3ql[$someaction]!='') {
  35. $action = $someaction;
  36. }
  37. }
  38. #if there is nothing as action, assume a select
  39. if ($action=='') {
  40. $action = 'select';
  41. }
  42. #identify the target
  43. if (ereg('(insert|edit|update|delete|grant)', $action)) {
  44. $s3ql['from'] = ($s3ql[$action]=='')?$_REQUEST[$action]:$s3ql[$action];
  45. }
  46. elseif (ereg('(select)', $action)) {
  47. $s3ql['from'] = ($s3ql['from']=='')?$_REQUEST['from']:$s3ql['from'];
  48. }
  49. #if there is no target, assume projects
  50. if ($s3ql['from']=='') {
  51. $s3ql['from'] = 'projects';
  52. }
  53. if($s3ql['from']=='permission')
  54. $s3ql['from'] = 'user';
  55. if($s3ql['from']=='class')
  56. $s3ql['from']= 'collection';
  57. if($s3ql['from'] =='instance')
  58. $s3ql['from'] = 'item';
  59. #these are targets ONLY for insert/edit/delete. Select takes plurals... was a bad idea, I know :-( but is much more intuitive :-)
  60. $possible_targets = array('permission', 'user', 'group', 'key', 'project', 'collection', 'item', 'rule', 'statement', 'filekey');
  61. #start taking action
  62. switch ($action) {
  63. case 'select':
  64. {
  65. $data = selectQuery(compact('s3ql', 'db','user_id', 'format'));
  66. return ($data);
  67. break;
  68. } #Close select queries
  69. case 'insert':
  70. {
  71. #echo '<pre>';print_r($s3ql);exit;
  72. #map s3ql input to s3db structure requirements
  73. if($s3ql['insert']=='class')
  74. $s3ql['insert']='collection';
  75. if($s3ql['insert']=='instance')
  76. $s3ql['insert']='item';
  77. if($s3ql['where']['notes']!='')
  78. $s3ql['where']['notes'] = $s3ql['where']['notes'];
  79. if($s3ql['where']['value']!='')
  80. $s3ql['where']['value'] = $s3ql['where']['value'];
  81. ##build inputs and oldvalues for validation and insert functions
  82. $tranformed = S3QLselectTransform(compact('s3ql', 'db', 'user_id'));
  83. $s3ql= $tranformed['s3ql'];$element = $s3ql['insert'];
  84. $element_id = $s3ql['where'][$element.'_id'];
  85. $letter = strtoupper(substr($element,0,1));
  86. $uid = $letter.$element_id;
  87. $required = array(
  88. 'key'=>array(),
  89. 'project'=>array('project_name'),
  90. 'collection'=>array('project_id', 'entity'),
  91. 'rule'=>array('project_id', 'subject_id', 'verb', 'object'),
  92. 'item'=>array('collection_id'),
  93. 'statement'=>array('item_id', 'rule_id', 'value'),
  94. 'file' => array('item_id', 'rule_id', 'filekey'),
  95. 'user' => array('account_lid', 'account_email'),
  96. 'group'=>array('account_lid'));
  97. if(!in_array($element, array_keys($required)))
  98. {
  99. return (formatReturn($GLOBALS['error_codes']['wrong_input'], $element.' is not a valid S3DB element. Valid elements: key, project, collection, rule, item, statement, file',$format,''));
  100. }
  101. #if a subject is provided instead of a subject id in rule, dont break because of that. Find the subject
  102. #THIS PART NEEDS TO B HERE BECAUSE IT THE MANDATORY FIELDS ARE 'OR'
  103. if($element=='rule')
  104. {
  105. $s3ql=ruleInputsInfer($s3ql, $db, $user_id);
  106. }
  107. #echo '<pre>';print_r($s3ql);
  108. #translate some s3ql inputs into s3db names:
  109. #IS there anythi ng still missing? There are 2 types fo required inputs: thsoe from the user and those into the table. The firstare verified here, the rest are verified in "validation"
  110. $diff=array_diff($required[$element],array_keys($s3ql['where']));
  111. if($element_id=='' && !empty($diff))
  112. return formatReturn($GLOBALS['error_codes']['something_missing'],'Please provide all the necessary fields: '.rtrim(array_reduce($required[$element], "comma_split"), ", ").'. '.$syntax_message, $s3ql['format'], '');
  113. #echo '<pre>';print_r($required[$element]);exit;
  114. #if there is any sort of id, check if user has permissions on that. In case of statement, permission must be checked on both rule and instance
  115. $inserteable = array(
  116. #'deployment'=>'deployment_id',
  117. 'group'=>'group_id',
  118. 'user'=>'user_id',
  119. 'project'=>'project_id',
  120. 'rule'=>'rule_id',
  121. 'collection'=>'collection_id',
  122. 'item'=>'item_id',
  123. 'statement'=>'statement_id',
  124. );
  125. #insert overal view
  126. #element_id is not empty
  127. #upstream resource provided
  128. #if all permissions clear up, grant permission to upper on loewer score;
  129. #upstream resource not provided
  130. #infer deployment if user, group or project, else nothing to do
  131. #element_id is empty
  132. #upstream resources provided
  133. #all permissions clear up, create new entry.
  134. #scoreTable will allow us to score the elements according to their position in the inheritance model. To nisert an "inserteable" A into an "inserteable" B,
  135. $scoreTable=array_reverse($inserteable, 0);
  136. $scoreTable = array_combine(array_keys($scoreTable), range(1,count($inserteable)));
  137. $elementScore = $scoreTable[$element];#check the score of target. All other score will be chacked against this one
  138. #for user, group and project, inserts occur in deployment (local). Except when there is indication on group or any other Id.
  139. $input_ids = array_intersect($inserteable, array_keys($s3ql['where']));
  140. if(ereg('^(U|G|P)$', $letter) && (count($input_ids)<=1 || count(array_filter(array_diff_key($s3ql['where'], array($element.'_id'=>''))))==0))
  141. {
  142. $s3ql['where']['deployment_id']=($s3ql['where']['deployment_id']!='')?$s3ql['where']['deployment_id']:substr($GLOBALS['Did'], 1, strlen($GLOBALS['Did']));
  143. $info[$GLOBALS['Did']]=URI($GLOBALS['Did'], $user_id, $db);
  144. $permission2add[$GLOBALS['Did']] = $info[$GLOBALS['Did']]['add_data'];
  145. $core_score[$GLOBALS['Did']] = 8;
  146. }
  147. #echo '<pre>';print_r($input_ids);exit;
  148. #echo '<pre>';print_r($inserteable);
  149. #echo '<pre>';print_r($s3ql);exit;
  150. ############################
  151. #this next segment finds all the s3ids in the query, and checks permission of user/session on it (user/session beause user ccna be using a group)
  152. #echo '<pre>';print_r($s3ql);
  153. if (ereg('^(U|G|P|C|R|I|S|F)$', strtoupper(substr($element, 0,1)))) {
  154. foreach ($inserteable as $s3element=>$id) {
  155. if ($s3ql['where'][$id]!='') {
  156. $element_name = $s3element;
  157. $id_name = $id;
  158. $uid_info=uid(strtoupper(substr($element, 0,1)).$s3ql['where'][$id_name]);
  159. $element_info = retrieveUIDInfo($s3element, $id, $scoreTable, $s3ql, $letter, $input_ids, $user_id, $db);
  160. #echo '<pre>';print_r($element_info);
  161. $info[strtoupper(substr($element_name, 0,1)).$s3ql['where'][$id_name]] = $element_info;
  162. $permission2add[strtoupper(substr($element_name, 0,1)).$s3ql['where'][$id_name]] = $element_info['add_data'];
  163. $core_score[strtoupper(substr($element_name, 0,1)).$s3ql['where'][$id_name]] = $scoreTable[$element_name];
  164. #when element id is present (customized elemnt-id, and is the only ID, and id already exists, user cannot recreat it. To update it, he must go through update. That is the only ID that can "Not" exist
  165. if ($id==$GLOBALS['s3ids'][$element] && !is_array($element_info)){
  166. #if a particular id was not found and user is trying to customize a new element_id, then user will have permission to add to it.
  167. $permission2add[strtoupper(substr($element_name, 0,1)).$s3ql['where'][$id_name]] = '1';
  168. }
  169. else
  170. {
  171. if(!is_array($element_info) && $uid_info['Did']==$GLOBALS['Did'])#for remote resources, allow insert withour requiring validation.. for now. For inserting projects witha specific uid,
  172. {
  173. return (formatReturn($GLOBALS['error_codes']['no_results'], 'Resource '.strtoupper(substr($element_name, 0,1)).$s3ql['where'][$id_name].' was not found', $format,''));
  174. #if($s3ql['format']=='')
  175. #return ('<TABLE><TR><TD>error_code</TD><TD>message</TD></TR><TR><TD>'.ereg_replace('[^(0-9)]', '',$GLOBALS['messages']['something_does_not_exist']).'</TD><TD>>Resource '.strtoupper(substr($element_name, 0,1)).$s3ql['where'][$id_name].' was not found</TD></TR></TABLE>');
  176. #else
  177. #return ($GLOBALS['messages']['something_does_not_exist'].'<message>Resource '.strtoupper(substr($element_name, 0,1)).$s3ql['where'][$id_name].' was not found</message>');
  178. }
  179. }
  180. }
  181. }
  182. #echo 'ola<pre>';print_r($info);exit;
  183. #echo '<pre>';print_r($permission2add);
  184. #echo '<pre>';print_r($core_score);
  185. #exit;
  186. $result = array_combine($core_score, $permission2add);#score as index and permissions as values
  187. #a group and a user can be inserted in any one resource... as long as user does have permission on the resource
  188. if(ereg('^(U|G)$', $letter))
  189. {
  190. if($result[min(array_keys($result))] || ($user_info['account_type']=='a') && max(array_keys($result))==8)
  191. $result[max(array_keys($result))]='1';
  192. }
  193. $has_permission2add = $result[max(array_keys($result))];#this means the highest scored element does NOT have permission to add
  194. #echo '<pre>';print_r($result);exit;
  195. #how many IDS?Min ID is 1; if two, then it can be inserting a statement or adding remote resource on local resource
  196. #print $info
  197. ####If any s3ids were found, Variable $info was created, and variable $permission2add was created from the first.
  198. #now,interpret what was found.
  199. #Permissions need to be checek if any ID is supplied that already exists.
  200. #if (ereg('(group|user|project|collection|rule|item|statement|file)', $element)) {
  201. if (ereg('(G|U|P|C|R|I|S|F)', strtoupper(substr($element, 0,1)))) {
  202. if (count($info)=='1' || (count($info)=='2' && $info[$GLOBALS['Did']]!='') || (count($info)=='2' && ereg('^(statement|file)$', $element))) {
  203. #is this ID from the element we are trying to insert?
  204. #does it exist?
  205. if($s3ql['where'][$GLOBALS['COREids'][$element]]!='' && isLocal($uid, $db) && !$info[$uid]['is_remote']) {#cannot recreate id. Do nothing.
  206. return(formatReturn($GLOBALS['error_codes']['wrong_input'], $uid.' already exists. Could not recreate it.', $format,''));
  207. }
  208. elseif (count($info)=='1' && $element_id!='') {
  209. return (formatReturn($GLOBALS['error_codes']['something_missing'], 'Please provide the uid where this '.$element.' should be inserted.', $format,''));
  210. }
  211. else {
  212. #take inputs, validate them, check permission on ONE id, create resource. Do the switch cases here.
  213. if($has_permission2add) {
  214. #this means the highest value on permission2asd is 1.
  215. if($info[$uid]['to_create']=='1' || $element_id=='') {
  216. $create_info = $s3ql['where'];
  217. #echo '<pre>';print_r($create_info); exit;
  218. #echo 'ola';exit;
  219. $inputs = gatherInputs(array('element'=>$element, 'info'=>$info,'to_create'=>$create_info, 'user_id'=>$user_id, 'db'=>$db));
  220. $info=$inputs;
  221. #echo 'inputs<pre>';print_r($inputs);exit;
  222. if(!is_array($inputs))
  223. {
  224. return (formatReturn('3', $inputs, $format,''));
  225. }
  226. $validity = validateInputs(compact('element', 'inputs', 'oldvalues', 'info', 'db', 'action', 'key','user_id','format'));
  227. #echo 'validity<pre>';print_r($validity);exit;
  228. if($validity[0])
  229. {
  230. $key=$s3ql['key'];
  231. $inserted = insert_s3db(compact('element', 'inputs', 'user_id', 'db', 'key'));
  232. #echo '<pre>';print_r($inserted);exit;
  233. return (formatReturn('0',$inserted[4], $format, array($element.'_id'=>$inserted[$element.'_id'])));
  234. }
  235. else {
  236. #echo '<pre>';print_r($validity);
  237. return (formatReturn($validity['error_code'],$validity['message'], $format,''));
  238. }
  239. }
  240. elseif($info[$uid]['is_remote']=='1') {#insert the permission on local
  241. #remote users an dgroups are inserted ON TABLE
  242. if(ereg('user|group|project', $element))
  243. {
  244. #echo '<pre>';print_r($info[$uid]);exit;
  245. $create_info = $info[$uid];
  246. $create_info['account_email']=($info[$uid]['account_email']=='')?'s3db@s3db.org':$info[$uid]['account_email'];
  247. $create_info['account_lid']=($info[$uid]['account_lid']!='')?$info[$uid]['account_lid']:$info[$uid]['account_id'];
  248. $inputs = gatherInputs(array('element'=>$element, 'info'=>$info,'to_create'=>$create_info, 'user_id'=>$user_id, 'db'=>$db));
  249. #echo '<pre>';print_r($inputs);exit;
  250. if(!is_array($inputs))
  251. {return ($inputs);}
  252. $validity = validateInputs(compact('element', 'inputs', 'oldvalues', 'info', 'db', 'action', 'key'));
  253. #echo '<pre>';print_r($validity);exit;
  254. if($validity[0])
  255. { $key=$s3ql['key'];
  256. $inserted =insert_s3db(compact('element', 'inputs', 'user_id', 'db', 'key'));
  257. return (formatReturn('0', $element.' inserted.', array($element.'_id'=>$inserted[$element.'_id'], $s3ql['format'])));
  258. }
  259. else {
  260. return ($validity[1]);
  261. }
  262. }
  263. $permission_info = array('uid'=>$uid,'shared_with'=>'U'.$user_id,'permission_level'=>$info[$uid]['acl']);
  264. $permission_added = insert_permission(compact('permission_info', 'db', 'user_id', 'info'));
  265. if(!$permission_added)
  266. $permission_added = update_permission(compact('permission_info', 'db', 'user_id', 'info'));
  267. if($permission_added){
  268. return (formatReturn($GLOBALS['error_codes']['success'], $uid." shared_with in ".$permission_info['shared_with'], $format,''));
  269. #return $GLOBALS['messages']['success']."<message> ".$uid." shared_with in ".$permission_info['shared_with']."</message>";
  270. }
  271. else {
  272. return (formatReturn($GLOBALS['error_codes']['something_went_wrong'], "Could not share ".$uid." with ".$permission_info['shared_with'], $format,''));
  273. #return $GLOBALS['messages']['something_went_wrong']."<message>Could not share ".$uid." with ".$permission_info['shared_with']."</message>";
  274. }
  275. }
  276. }
  277. else {
  278. $no_permission_id = array_search('0', $permission2add);
  279. return (formatReturn($GLOBALS['error_codes']['no_permission_message'], 'User does not have permission to insert in '.$no_permission_id, $format,''));
  280. exit;
  281. #return ($GLOBALS['messages']['no_permission_message'].' Reason: <message>User does not have permission to insert in '.$no_permission_id.'</message>');
  282. }
  283. }
  284. }
  285. elseif(count($info)>=2) #NOT a physical insert, but a virtual insert in an existing resource
  286. {
  287. #echo '<pre>';print_r($info);exit;
  288. #2 or + ids in info.
  289. #these IDS can be entity_id OR membership
  290. if($element_id!='' && !$info[$uid]['to_create']) #this automatically means that the second id refers to membership.
  291. {
  292. #grant permissions
  293. $shared_with = array_diff(array_keys($permission2add), array($uid));#take uid from the keys of permission2add, that point to the uid we are sharing with
  294. $shared_with = $shared_with[0];
  295. $add_resource_on_resource = substr(has_permission(compact('uid', 'shared_with'), $db), 2,1);
  296. if(!$has_permission2add)#statement has rule_id and instance_id, user must have permission on both.
  297. return (formatReturn($GLOBALS['error_codes']['no_permission_message'], 'User does not have permission to insert in resource '.key($permission2add), $format,''));
  298. #return ($GLOBALS['messages']['no_permission_message'].'<message>User does not have permission to insert in resource '.key($permission2add).'</message>');
  299. if($result[max(array_keys($result))]=='0' && $result[min(array_keys($result))]=='1' && $add_resource_on_resource!='1' && $element!='user')
  300. return (formatReturn($GLOBALS['error_codes']['something_missing'], 'To share '.$uid.' owner of '.$shared_with.' must insert first '.$uid.' in '.$shared_with.'.', $s3ql['format'], ''));
  301. else {
  302. #if is remote and user cna insert in resource, must be inserted first
  303. if($info[$uid]['to_create'])
  304. {
  305. $create_info = $s3ql['where'];
  306. #echo '<pre>';print_r($create_info); exit;
  307. $inputs = gatherInputs(array('element'=>$element, 'info'=>$info,'to_create'=>$create_info, 'user_id'=>$user_id));
  308. if(!is_array($inputs))
  309. return ($inputs);
  310. $validity = validateInputs(compact('element', 'inputs', 'oldvalues', 'info', 'db', 'action', 'key'));
  311. if($validity[0])
  312. {
  313. $key=$s3ql['key'];
  314. $inserted = insert_s3db(compact('element', 'inputs', 'user_id', 'db', 'key'));
  315. return (formatReturn('0', $element.' inserted.', array($element.'_id'=>$inserted[$element.'_id'], $s3ql['format'])));
  316. }
  317. else {
  318. return ($validity[1]);
  319. }
  320. }
  321. if($info[$uid]['is_remote'])
  322. {
  323. #the other iD, non element id, should be the upper ID, where user shoulsd already have intert permission
  324. $diff=array_diff(array_keys($permission2add), array($uid));
  325. $shared_with = $diff[0];
  326. $permission_info = array('uid'=>$uid,'shared_with'=>$shared_with,'permission_level'=>$info[$uid]['acl']);
  327. $permission_added = insert_permission(compact('permission_info', 'db', 'user_id', 'info'));
  328. if(!$permission_added)
  329. $permission_added = update_permission(compact('permission_info', 'db', 'user_id', 'info'));
  330. if($permission_added){
  331. return formatReturn($GLOBALS['error_codes']['success'], $uid." inserted in ".$shared_with, $s3ql['format'], '');
  332. }
  333. else {
  334. return (formatReturn($GLOBALS['error_codes']['something_went_wrong'], "Could not share ".$uid." with ".$permission_info['shared_with'], $format,''));
  335. #return $GLOBALS['messages']['something_went_wrong']."<message>Could not share ".$uid." with ".$permission_info['shared_with']."</message>";
  336. }
  337. }
  338. if(!$info[$uid]['to_create'] && $s3ql['where']['permission_level']=='')
  339. {
  340. #does it exist already in upper resource?
  341. $diff=array_diff(array_keys($permission2add), array($uid));
  342. $shared_with = $diff[0];
  343. $sql = str_replace($GLOBALS['regexp'], '=', select(compact('uid', 'shared_with')));
  344. #echo $sql;exit;
  345. $db->query($sql, __LINE__, __FILE__);
  346. if($db->next_record())
  347. return (formatReturn($GLOBALS['error_codes']['repeating_action'], $uid.' already shared with '.$shared_with.'. You can change its level of permission by indicating permission_level.', $s3ql['format'],''));
  348. }
  349. }
  350. #share according to permissions
  351. $uid2share = array_search(min($core_score), $core_score);
  352. $shared_with = array_search(max($core_score), $core_score);
  353. $uid_info = uid($uid2share);
  354. if(($result[max(array_keys($result))]=='1') || ($add_resource_on_resource && $result[min(array_keys($result))]=='1')) #permission to add on upstream resource
  355. {
  356. #echo 'ola';exit;
  357. $case ='2';
  358. $uid_info = uid($uid2share);
  359. if($uid_info['Did']==$GLOBALS['Did'])
  360. $uid2share = $uid_info['uid'];
  361. #$uid2share = strtoupper(substr($uid_info['uid'],0,1)).$GLOBALS['Did'].'/'.$uid_info['uid'];
  362. $permission_info = array('uid'=>$uid2share,
  363. 'shared_with'=>$shared_with,
  364. 'permission_level'=>($s3ql['where']['permission_level']!='')?$s3ql['where']['permission_level']:'210',
  365. );
  366. #echo '<pre>';print_r($permission_info);exit;
  367. $validity = validate_permission(compact('permission_info', 'user_id', 'db', 'info'));#grant project_id permission on rule_id
  368. #echo $validity;exit;
  369. if($validity=='0')
  370. $permission_added = insert_permission(compact('permission_info', 'db', 'user_id', 'info'));#grant rule_id permission on project_id
  371. elseif($validity=='2')
  372. $permission_added = update_permission(compact('permission_info', 'db', 'user_id', 'info'));
  373. elseif($validity=='6' && ereg('^G', $shared_with) && ereg('^U', $uid))
  374. {
  375. $permission_added = insert_permission(compact('permission_info', 'db', 'user_id', 'info'));#grant rule_id permission on project_id
  376. $permission_added = update_permission(compact('permission_info', 'db', 'user_id', 'info'));
  377. }
  378. #can insert, special case, quick fix
  379. elseif($validity=='6')
  380. return (formatReturn($GLOBALS['error_codes']['no_permission_message'], 'User must have permission '.$permission_info['permission_level'].' or greater to grant permission '.$permission_info['permission_level'].' on '.$permission_info['shared_with'], $format,''));
  381. #return ($GLOBALS['messages']['no_permission_message'].'<message>User must have permission '.$permission_info['permission_level'].' or greater to grant permission '.$permission_info['permission_level'].' on '.$permission_info['shared_with'].'.</message>');
  382. }
  383. elseif($result[max(array_keys($result))]=='1' && $result[min(array_keys($result))]=='0') #permission to add on upstream resource
  384. {
  385. $case ='1';
  386. if($uid_info['Did']==$GLOBALS['Did'])
  387. $uid2share= strtoupper(substr($uid_info['uid'],0,1)).$GLOBALS['Did'].'/'.$uid_info['uid'];
  388. $permission_info = array('shared_with'=>$shared_with,
  389. 'uid'=>$uid2share,
  390. 'permission_level'=>'001');
  391. $permission_added = insert_permission(compact('permission_info', 'db', 'user_id', 'info'));
  392. if(!$permission_added)
  393. $permission_added = update_permission(compact('permission_info', 'db', 'user_id', 'info'));
  394. #This step will leave rule insert pending until owner of the rule comes by and inserts it in project
  395. }
  396. if($permission_added)
  397. {
  398. #Missing: Create an entry in access_rules with "Pending" statuss
  399. if($case =='1')
  400. return (formatReturn($GLOBALS['error_codes']['success'], "Permission on ".$permission_info['uid']." requested and pending.", $format,''));
  401. #return $GLOBALS['messages']['success']."<message> Permission on ".$permission_info['uid']." requested and pending.</message>";
  402. else {
  403. return (formatReturn($GLOBALS['error_codes']['success'],$permission_info['uid']." inserted in ".$permission_info['shared_with'], $s3ql['format'], ''));
  404. }
  405. }
  406. else {
  407. return (formatReturn($GLOBALS['error_codes']['something_went_wrong'], "Could not share ".$permission_info['uid']." with ".$permission_info['shared_with'], $s3ql['format'],''));
  408. }
  409. }
  410. elseif($info[$uid]['to_create'] || $info[$uid]['is_remote']) {#insert IF is remote or was asserted to be inserted
  411. if(is_array($info[$uid]) && $info[$uid]['is_remote'])
  412. $create_info = $info[$uid];
  413. else
  414. $create_info = $s3ql['where'];
  415. $inputs = gatherInputs(array('element'=>$element, 'to_create'=>$create_info, 'user_id'=>$user_id, 'info'=>$info));
  416. #echo '<pre>';print_r($inputs);exit;
  417. if(!is_array($inputs))
  418. return ($inputs);
  419. $validity = validateInputs(compact('element', 'inputs', 'oldvalues', 'info', 'db', 'action', 'key'));
  420. if($validity[0])
  421. { $key=$s3ql['key'];
  422. $inserted =insert_s3db(compact('element', 'inputs', 'user_id', 'db', 'key'));
  423. return (formatReturn('0', $element.' inserted.', array($element.'_id'=>$inserted[$element.'_id']), $s3ql['format']));
  424. }
  425. else {
  426. return ($validity[1]);
  427. }
  428. }
  429. }
  430. }
  431. }
  432. #permissions to add are stored in $permission2add, but when we are inserting an existing idA on an existing idB, we do not need permission to add_data on A, only on B. So the users does not need insert permission on idA, if idA is further down the graph then idB.
  433. #if there is only 1 id, and there is no insert permission, it can break
  434. #start some special cases
  435. switch ($element) {
  436. case 'key':
  437. {##INSERT KEY
  438. #when no key is given, generate a random one
  439. if ($s3ql['where']['key_id']=='')
  440. $s3ql['where']['key_id'] = random_string('15');
  441. if($s3ql['where']['expires']=='')
  442. $s3ql['where']['expires']=date('Y-m-d H:i:s',time() + (1 * 24 * 60 * 60));#expires in 24h
  443. #user can chose to insert a key for a specific ID, be it group, project, rule or statement (anywhere where permissions can be defined)
  444. $I['inputs'] = array_merge($s3ql['where'], array('account_id'=>$user_id));
  445. $validate = validate_access_key_inputs(array('inputs'=>$I['inputs'], 'db'=>$db, 'user_id'=>$user_id));
  446. switch ($validate)
  447. {
  448. case 0:
  449. {
  450. return (formatReturn($GLOBALS['error_codes']['something_missing'],'Expiration date is missing', $s3ql['format'], ''));
  451. break;
  452. }
  453. case 1:
  454. {return (formatReturn($GLOBALS['error_codes']['wrong_input'],'Key is too short. Please input a key longer than 10 char', $s3ql['format'], ''));
  455. break;
  456. }
  457. case 2:
  458. {return (formatReturn($GLOBALS['error_codes']['wrong_input'],'Invalid date format', $s3ql['format'], ''));
  459. break;
  460. }
  461. case 3:
  462. {return (formatReturn($GLOBALS['error_codes']['repeating_action'],'Key '.$s3ql['where']['key_id'].' is not valid. Please chose another key', $s3ql['format'], ''));
  463. break;
  464. }
  465. case 4:
  466. {return (formatReturn($GLOBALS['error_codes']['wrong_input'],'Expiration date must be bigger than present date.', $s3ql['format'], ''));
  467. break;
  468. }
  469. case 6:
  470. {return (formatReturn($GLOBALS['error_codes']['wrong_input'],'UID '.$s3ql['where']['UID'].' does not exist', $s3ql['format'], ''));
  471. break;
  472. }
  473. case 7:
  474. {return (formatReturn($GLOBALS['error_codes']['no_permission_message'],'UID '.$s3ql['where']['UID'].' does not belong to user.', $s3ql['format'], ''));
  475. break;
  476. }
  477. case 8:
  478. {return (formatReturn($GLOBALS['error_codes']['wrong_input'],'Please use only numbers and letter in your keys.', $s3ql['format'], ''));
  479. break;
  480. }
  481. case 5:
  482. {
  483. add_entry ('access_keys', $I['inputs'], $db);
  484. $output = formatReturn($GLOBALS['error_codes']['success'], 'Key created.',$s3ql['format'], array('key_id'=>$s3ql['where']['key_id']));
  485. return ($output);
  486. }
  487. }
  488. break;
  489. }
  490. case 'file':
  491. {
  492. $resource_id = ($s3ql['where']['item_id']!='')?$s3ql['where']['item_id']:$s3ql['where']['instance_id'];
  493. $rule_id = $s3ql['where']['rule_id'];
  494. $filekey = $s3ql['where']['filekey'];
  495. $notes = $s3ql['where']['notes'];
  496. if($resource_id=='' ||$rule_id=='' ||$filekey=='')
  497. {
  498. return (formatReturn($GLOBALS['error_codes']['something_missing'], 'Please provide all the necessary inputs: rule_id, item_id, filekey', $format,''));
  499. #return ($GLOBALS['messages']['something_missing'].'<message>Please provide all the necessary inputs: rule_id, item_id, filekey</message>');
  500. }
  501. #Check permission on inserting statements for specific projects
  502. #Check permission on inserting statements for specific projects
  503. $rule_info = $info['R'.$rule_id];
  504. $instance_info = $info['I'.$resource_id];
  505. #$instance_info = URIinfo('I'.$resource_id, $user_id, $key, $db);
  506. if($rule_info['object']=='UID')
  507. {
  508. return (formatReturn($GLOBALS['error_codes']['wrong_input'], 'Please use this query only for rules that do NOT enumerate classes. For inserting on other rules, use the query for insert instance', $format, ''));
  509. #return $wrong_input."<message>Please use this query only for rules that do NOT enumerate classes. For inserting on other rules, use the query for insert instance</message>";
  510. }
  511. elseif (!is_array($instance_info)) {
  512. return (formatReturn($GLOBALS['error_codes']['no_results'], 'Item '.$resource_id.' was not found', $format,''));
  513. #return ($something_does_not_exist.'<message>Instance '.$resource_id.' was not found</message>');
  514. }
  515. elseif ($instance_info['resource_class_id']!=$rule_info['subject_id']) {
  516. return (formatReturn($GLOBALS['error_codes']['wrong_input'],'Subject of rule does match Class of instance',$format,''));
  517. #return $wrong_input."<message>Subject of rule does match Class of instance</message>";
  518. }
  519. elseif($filekey=='')
  520. return (formatReturn($GLOBALS['error_codes']['something_missing'], 'Please indicate a filekey for this file',$format,''));
  521. #return $wrong_input."<message>Please indicate a filekey for this file</message>";
  522. #Find out if the file already exists in the tmp directory
  523. $fileFinalName = get_entry('file_transfer', 'filename', 'filekey', $filekey, $db);
  524. $file_id = get_entry('file_transfer', 'file_id', 'filekey', $filekey, $db);
  525. ereg('([A-Za-z0-9]+)\.*([A-Za-z0-9]*)$',$fileFinalName, $tokens);
  526. $name = $tokens[1];
  527. $extension= $tokens[2];
  528. #list($name, $extension) = explode('.', $fileFinalName);
  529. $maindir = $GLOBALS['s3db_info']['server']['db']['uploads_folder'].$GLOBALS['s3db_info']['server']['db']['uploads_file'].'/tmps3db';
  530. $old_file = $maindir.'/'.$file_id.'.'.$extension;
  531. if(!is_file($old_file))
  532. return (formatReturn($GLOBALS['error_codes']['something_does_not_exist'], 'File not found, please upload file first.', $format,''));
  533. #return $something_does_not_exist."<message>File not found, please upload file first.</message>";
  534. else
  535. {
  536. #project_id will be that of the rule, except if user does not have permission on it.
  537. $project_info = URI('P'.$rule_info['project_id'], $user_id, $db);
  538. $project_id = ($s3ql['where']['project_id']!='')?$s3ql['where']['project_id']:(($project_info['add_data'])?$class_info['project_id']:'');
  539. if($project_id =='')
  540. #find which of the user projects can insert instances in this class.
  541. {
  542. $project_id = $rule_info['project_id'];
  543. #$user_projects = findUserProjects($user_id, $db);
  544. // $user_projects = array_map('grab_project_id', $user_projects);
  545. //
  546. //
  547. // #find the projects that can access the rule
  548. // $allowed_projects = array_filter(explode('_', $rule_info['permission']));
  549. //
  550. // $both = array_intersect($allowed_projects, $user_projects);
  551. //
  552. // if (is_array($both)) {
  553. // foreach ($both as $key=>$allowed_project_id) {
  554. // if(substr(has_permission(array('uid'=>'R'.$rule_id, 'shared_with'=>'P'.$allowed_project_id), $db), 2,1))
  555. // $project_id = $allowed_project_id;
  556. // }
  557. // }
  558. }
  559. if($project_id=='')
  560. return (formatReturn($GLOBALS['error_codes']['something_went_wrong'], 'Failed to find a project_in for this intance', '', $s3ql['format']));
  561. $value = project_folder_name ($project_id, $db);
  562. $created_by = $user_id;
  563. $filesize = filesize($old_file);
  564. $filename = $fileFinalName;
  565. ##Create the row in the statements table
  566. $create_info = $s3ql['where'];
  567. #echo '<pre>';print_r($s3ql);
  568. $inputs = gatherInputs(array('element'=>'file', 'info'=>$info,'to_create'=>$create_info, 'user_id'=>$user_id, 'db'=>$db));
  569. $info=$inputs;
  570. if(!is_array($inputs))
  571. {
  572. return(formatReturn('3', $inputs, $s3ql['format'],''));
  573. }
  574. $validity = validateInputs(compact('element', 'inputs', 'oldvalues', 'info', 'db', 'action', 'key','user_id'));
  575. #echo '<pre>';print_r($validity);exit;
  576. if($validity[0])
  577. {
  578. $key=$s3ql['key'];
  579. $inserted = insert_s3db(compact('element', 'inputs', 'user_id', 'db', 'key'));
  580. ##Move the file
  581. $S = compact('user_id', 'project_id', 'resource_id', 'rule_id', 'value', 'notes', 'created_by', 'filename', 'filesize', 'extension', 'db');
  582. $S['statement_id']=$inserted['statement_id'];
  583. $S['uploadedfile'] = $old_file;
  584. $fileRelocated = movefile2folder($S);
  585. if(!$fileRelocated)#delete the statement
  586. {$sql = "delete from s3db_statement where statement_id = '".$S['statement_id']."'";
  587. $db->query($sql, __FILE__, __LINE__);
  588. #echo $sql;
  589. return (formatReturn('2', 'File could not be imported. Please try again.', '', $s3ql['format']));
  590. #unlink($old_file);
  591. }
  592. else{
  593. return (formatReturn($GLOBALS['error_codes']['success'], 'File inserted.', $s3ql['format'], array('file_id'=>$inserted['1'])));
  594. #if($s3ql['format']=='')
  595. # return ('<TABLE><TR><TD>error_code</TD><TD>message</TD><TD>'.$element.'_id</TD></TR><TR><TD>'.ereg_replace('[^(0-9)]', '', $inserted[3]).'</TD><TD>'.$inserted[4].'</TD><TD>'.$inserted[$element.'_id'].'</TD></TR></TABLE>');
  596. #else
  597. # return ($inserted[1]);
  598. }
  599. }
  600. else {
  601. #echo '<pre>';print_r($validity);
  602. return (formatReturn(ereg_replace('[^(0-9)]', '', $inserted[3]), $validity[1], $format,''));
  603. #if($s3ql['format']=='')
  604. # return ('<TABLE><TR><TD>error_code</TD><TD>message</TD></TR><TR><TD>'.ereg_replace('[^(0-9)]', '', $inserted[3]).'</TD><TD>'.$validity[1].'</TD></TR></TABLE>');
  605. #else
  606. #return ($validity[1]);
  607. }
  608. ##Move the file
  609. if($statement_inserted)
  610. {
  611. $S['statement_id']=find_latest_UID('statement', $db);
  612. $S['uploadedfile'] = $old_file;
  613. $fileRelocated = movefile2folder($S);
  614. if ($fileRelocated)
  615. {
  616. return (formatReturn($GLOBALS['error_codes']['success'], "File inserted", array('file_id'=>$S['file_id']), $s3ql['format']));
  617. }
  618. else {
  619. return (formatReturn($GLOBALS['error_codes']['something_went_wrong'], 'Failed to move file', $format,''));
  620. }
  621. #else return $something_went_wrong."<message>Failed to move file</message>";
  622. }
  623. }
  624. #This ends "is not a file"
  625. }#This ends insert file
  626. break;
  627. }#finish element switch
  628. break;
  629. } #Finish insert
  630. case 'edit':
  631. {
  632. if($s3ql['edit']=='class')
  633. $s3ql['edit']='collection';
  634. if($s3ql['edit']=='instance')
  635. $s3ql['edit']='item';
  636. if($s3ql['set']['notes']!='')
  637. $s3ql['set']['notes'] = utf8_encode($s3ql['set']['notes']);
  638. if($s3ql['set']['value']!='')
  639. $s3ql['set']['value'] = utf8_encode($s3ql['set']['value']);
  640. #$element = $s3ql[$action];
  641. $element = $s3ql['edit'];
  642. #echo 'ola<pre>';print_r($s3ql);exit;
  643. $set = array('project'=>array('project_name', 'project_description', 'project_owner', 'permission_level'),
  644. 'collection'=>array('project_id', 'entity', 'notes'),
  645. 'rule'=>array('project_id', 'subject', 'verb', 'object', 'subject_id', 'verb_id', 'object_id', 'notes', 'validation'),
  646. 'item'=>array('project_id', 'collection_id', 'notes'),
  647. 'statement'=>array('project_id', 'item_id', 'rule_id', 'value', 'notes'),
  648. 'user'=>array('account_lid','account_pwd', 'account_uname', 'account_email', 'account_phone', 'addr1', 'addr2', 'account_type', 'city', 'postal_code', 'state', 'country', 'account_status'),
  649. 'group'=>array('account_lid'));
  650. $E = compact('db', 'user_id', 's3ql');
  651. #first of all, is this a valid target?
  652. if(!in_array($s3ql['edit'], array_keys($set)))
  653. {
  654. return formatReturn($GLOBALS['error_codes']['wrong_input'], $s3ql['edit']." is not a valid S3DB element. Valid elements: project, collection, rule, item, statement", $s3ql['format'],'');
  655. }
  656. #is there an ID to locate the appropriate resource?
  657. if($s3ql['where'][$element.'_id'] == '')
  658. {
  659. return formatReturn($GLOBALS['error_codes']['something_missing'], 'ID of '.$element.' to edit is missing', $s3ql['format'],'');
  660. }
  661. if($s3ql['set']=='')
  662. {
  663. #is it in where?
  664. $s3ql['set']=array_diff_key($s3ql['where'], array($element.'_id'=>''));
  665. if($s3ql['set']=='')
  666. return formatReturn($GLOBALS['error_codes']['something_missing'], 'Please specify what you want to update.'.$syntax_message, $s3ql['format'],'');
  667. }
  668. #interpret input
  669. $s3map=$GLOBALS['s3map'];
  670. foreach ($s3map[$GLOBALS['plurals'][$element]] as $alter_name=>$name) {
  671. if($s3ql['set'][$alter_name]!='')
  672. $s3ql['set'][$name]=$s3ql['set'][$alter_name];
  673. }
  674. $s3ql['set'] = array_diff_key($s3ql['set'], $s3map[$GLOBALS['plurals'][$element]]);
  675. $s3ql['set'] = array_filter($s3ql['set']);
  676. #detect is something that is something in set that cannot be updated
  677. $test_set = array_intersect($set[$element], array_keys($s3ql['set']));
  678. $extra_fields = array_diff(array_keys($s3ql['set']), $test_set);
  679. if(count($s3ql['set'])>count($test_set))#this means that there are fields that don't exist
  680. foreach ($extra_fields as $field_name) {
  681. $output .= '<message>Warning: '.$field_name.' is not a valid property of '.$element.'. '.$field_name.' will not be updated. Valid properties: '.rtrim(array_reduce($set[$element], 'comma_split'), ', ').'</message>';
  682. }
  683. #retrieve information about resource
  684. $element_id = $s3ql['where'][$element.'_id'];
  685. $uid = strtoupper(substr($element,0,1)).$element_id;
  686. $e_info=URIinfo($uid, $user_id, $key, $db);
  687. #echo '<pre>';print_r($e_info);
  688. if(!is_array($e_info))
  689. return (formatReturn($GLOBALS['error_codes']['something_does_not_exist'], ''.$element.' '.$element_id.' was not found.'));
  690. elseif(!$e_info['change'])
  691. return (formatReturn($GLOBALS['error_codes']['no_permission_message'], 'User does not have permission to change this '.$element, $s3ql['format'],''));
  692. foreach ($e_info as $field=>$data) {
  693. if($s3ql['set'][$field]!='' || $field=='notes')
  694. if(in_array($field, $set[$element]))
  695. {
  696. $oldvalues[$field] = $e_info[$field];
  697. $e_info[$field] = $s3ql['set'][$field];
  698. $inputs[$field] = $s3ql['set'][$field];
  699. }
  700. }
  701. #echo '<pre>';print_r($inputs);
  702. switch ($element) {
  703. case 'user':{##EDIT USER
  704. $user_to_change_info = get_info('user', $element_id, $db);#this is necessary because password will not come in the $e_info var.
  705. #permission was checked before the switch
  706. #map values
  707. $s3map = array('login'=>'account_lid',
  708. 'password'=>'account_pwd',
  709. 'username'=>'account_uname',
  710. 'email'=>'account_email',
  711. 'phone'=>'account_phone',
  712. 'address'=>'addr1',
  713. 'address2'=>'addr2',
  714. 'city'=>'city',
  715. 'state'=>'state',
  716. 'postal_code'=>'postal_code',
  717. 'country'=>'country',
  718. 'account_type'=>'account_type');
  719. #encript the password
  720. #echo '<pre>';print_r($s3ql);exit;
  721. if ($s3ql['set']['password']!='' || $s3ql['set']['account_pwd']) {
  722. $s3ql['set']['password'] = ($s3ql['set']['account_pwd']!='')?md5($s3ql['set']['account_pwd']):md5($s3ql['set']['password']);
  723. }
  724. else {
  725. $s3ql['set']['password']=$user_to_change_info['account_pwd'];
  726. }
  727. #echo '<pre>';print_r($s3ql);
  728. #login, password and email cannot be deleted so if they come empty, fill them out with the old values
  729. $non_erasable = array('login', 'email', 'username', 'password');
  730. foreach ($non_erasable as $fieldname) {
  731. if (in_array($fieldname, array_keys($s3ql['set']))) {
  732. if ($s3ql['set'][$fieldname]=='') {
  733. return (formatReturn($GLOBALS['error_codes']['wrong_input'], 'login, email, username and password cannot be deleted', $s3ql['format'],''));
  734. }
  735. }
  736. elseif (!in_array($fieldname, array_keys($s3ql['set']))) {
  737. #then start filling out input with the old values
  738. $inputs[$s3map[$fieldname]] = $e_info[$s3map[$fieldname]];
  739. }
  740. }
  741. #now map the valid values
  742. foreach (array_keys($s3ql['set']) as $set) {
  743. if (in_array($set, array_keys($s3map))) {
  744. if($s3ql['set'][$set]!='') {
  745. $inputs[$s3map[$set]] =$s3ql['set'][$set];
  746. }
  747. }
  748. }
  749. #echo '<pre>';print_r($e_info);
  750. $inputs['account_type'] = ($s3ql['set']['account_type']!='')?$s3ql['set']['account_type']:$user_to_change_info['account_type'];
  751. $inputs['account_status'] = ($s3ql['set']['account_status']!='')?$s3ql['set']['account_status']:$user_to_change_info['account_status'];
  752. $inputs['account_group'] = $inputs['account_type'];
  753. #replace in $e_info the values with the inputs. First clean the existing one, then merge with the new one
  754. $user_info = array_diff_key($e_info, $inputs);
  755. $user_info = array_merge($user_info, $inputs);
  756. $validity = validate_user_inputs(array('inputs'=>$inputs, 'imp_user_id'=>$e_info['account_id'], 'db'=>$db, 'action'=>'update'));
  757. $info=$e_info;
  758. #echo '<pre>';print_r($inputs);
  759. #$validity = validateInputs(compact('element','info', 'inputs', 'oldvalues', 'user_id', 'db'));
  760. #echo '<pre>';print_r($validity);
  761. switch($validity)
  762. {
  763. case 0:
  764. #echo '<pre>';print_r($user_info); exit;
  765. if(!update_user(compact('user_info', 'db', 'user_id')))
  766. {
  767. #$output .= $something_went_wrong;
  768. return (formatReturn($GLOBALS['error_codes']['something_went_wrong'], 'User could not be updated. Undetermined reasons.', $s3ql['format'], ''));
  769. }
  770. else {
  771. #$output .= $GLOBALS['messages']['success'];
  772. #$output .= '<message> User updated</message>';
  773. return (formatReturn($GLOBALS['error_codes']['success'],'User updated', $s3ql['format'],''));
  774. }
  775. break;
  776. case 1:
  777. #$output .= $GLOBALS['messages']['something_missing'];
  778. #$output .= '<message> Login is missing.'.$syntax_message.'</message>';
  779. return (formatReturn($GLOBALS['error_codes']['something_missing'], 'Login is missing.'.$syntax_message, $s3ql['format'],''));
  780. break;
  781. case 2:
  782. #$output .= $GLOBALS['messages']['something_missing'];
  783. #$output .= '<message> Account_type is missing.'.$syntax_message.'</message>';
  784. return (formatReturn($GLOBALS['error_codes']['something_missing'], 'Account_type is missing.'.$syntax_message,$s3ql['format'],''));
  785. break;
  786. case 3:
  787. #$output .= $GLOBALS['messages']['something_missing'];
  788. #$output .= '<message> Username is missing.'.$syntax_message.'</message>';
  789. return (formatReturn($GLOBALS['error_codes']['something_missing'], 'Username is missing.'.$syntax_message, $s3ql['format'],''));
  790. break;
  791. case 4:
  792. #$output .= $GLOBALS['messages']['something_missing'];
  793. #$output .= '<message> Account status is missing.'.$syntax_message.'</message>';
  794. return (formatReturn($GLOBALS['error_codes']['something_missing'], 'Account status is missing.'.$syntax_message, $s3ql['format'],''));
  795. break;
  796. case 5:
  797. $output .= $GLOBALS['messages']['something_missing'];
  798. $output .= '<message> Password is missing.'.$syntax_message.'</message>';
  799. return (formatReturn($GLOBALS['error_codes']['something_missing'], 'Password is missing.'.$syntax_message, $s3ql['format'],''));
  800. break;
  801. case 6:
  802. #$output .= $GLOBALS['messages']['something_missing'];
  803. return (formatReturn($GLOBALS['error_codes']['something_missing'], '',$s3ql['format'],''));
  804. break;
  805. case 7:
  806. #$output .= $GLOBALS['messages']['something_missing'];
  807. return (formatReturn($GLOBALS['error_codes']['something_missing'], '',$s3ql['format'],''));
  808. break;
  809. case 8:
  810. #$output .=$GLOBALS['messages']['repeating_action'].'<message> User '.$inputs['account_lid'].' already exists</message>';
  811. return (formatReturn($GLOBALS['error_codes']['something_missing'], 'User '.$inputs['account_lid'].' already exist',$s3ql['format'],''));
  812. break;
  813. }
  814. break;
  815. }
  816. case 'group':{##EDIT GROUP
  817. $info = $e_info;
  818. $group_id = $info['group_id'];
  819. $validity = validateInputs(compact('element', 'inputs', 'oldvalues', 'info', 'db', 'action', 'key'));
  820. if($validity[0])
  821. {
  822. if (update_group(compact('inputs', 'group_id', 'user_id', 'db'))) {
  823. return (formatReturn($GLOBALS['error_codes']['success'], 'G'.$group_id.' successfully updated',$s3ql['format'],''));
  824. #return ($GLOBALS['messages']['success'].'<message>G'.$group_id.' successfully updated</message>');
  825. }
  826. else {
  827. return (formatReturn($GLOBALS['error_codes']['something_went_wrong'], 'G'.$group_id.' could not be updated. Reason undetermined.',$s3ql['format'],''));
  828. #return ($GLOBALS['messages']['something_went_wrong']);
  829. }
  830. break;
  831. }
  832. else {
  833. return ($validity[1]);
  834. }
  835. break;
  836. }
  837. case 'project': {##EDIT PROJECT
  838. $project_info = $e_info;
  839. $U = compact('project_info', 'db', 'user_id');
  840. #$validity = validate_project_inputs($U);
  841. $info = $e_info;
  842. $validity = validateInputs(compact('element', 'inputs', 'oldvalues', 'info', 'db', 'action', 'key'));
  843. if($validity[0])
  844. {
  845. if(update_project($U))
  846. {
  847. return formatReturn($GLOBALS['error_codes']['success'], $element." P".$element_id." updated.", $s3ql['format'],'');
  848. }
  849. else
  850. {
  851. return formatReturn($GLOBALS['error_codes']['something_went_wrong'], 'Failed to update project!',$s3ql['format'],'');
  852. }
  853. }
  854. else {
  855. #break validity in error and message
  856. ereg('<error>([0-9]+)</error>(.*)<message>(.*)</message>', $validity[1], $valOut);
  857. return (formatReturn($valOut[1],$valOut[3], $s3ql['format'],''));
  858. }
  859. break;
  860. }
  861. case 'collection':{##EDIT CLASS
  862. $resource_info = $e_info;
  863. $editresource = compact('db','user_id', 'resource_info', 'action', 'inputs', 'oldvalues');
  864. $info = $e_info;
  865. $validity = validateInputs(compact('element', 'inputs', 'oldvalues', 'info', 'db', 'action', 'key'));
  866. #echo '<pre>';print_r($validity);exit;
  867. #echo $validity = validate_resource_inputs($editresource);exit;
  868. if($validity[0]){
  869. if(update_resource($editresource))
  870. {#$validity[1].'<br><message>'.$element.' updated</message>';
  871. $output .= formatReturn('0', $element." C".$element_id.' updated', $format, '');
  872. return ($output);
  873. #return ($output);
  874. }
  875. }
  876. else {
  877. ereg('<error>([0-9]+)</error>(.*)<message>(.*)</message>', $validity[1], $valOut);
  878. return (formatReturn($valOut[1],$valOut[3], $s3ql['format'],''));
  879. #return ($validity[1]);
  880. }
  881. break;
  882. }
  883. case 'item':{##EDIT INSTANCE
  884. #echo '<pre>';print_r($oldvalues);exit;
  885. #Does this resource exist?
  886. $instance_id = $element_id;
  887. $info = $e_info;
  888. $notes = $s3ql['set']['notes'];
  889. $R = compact('info', 'inputs', 'oldvalues','db', 'user_id');
  890. $validity = validateInputs(compact('element', 'inputs', 'oldvalues', 'info', 'db', 'action', 'key'));
  891. if($validity[0]){
  892. if(update_resource_instance($R))
  893. {
  894. return (formatReturn('0',$element." I".$element_id." updated", $s3ql['format'],''));
  895. #$action = 'edit';
  896. #$statement_info = $info;
  897. #return ($output);
  898. }
  899. }
  900. else {
  901. ereg('<error>([0-9]+)</error>(.*)<message>(.*)</message>', $validity[1], $valOut);
  902. return (formatReturn($valOut[1],$valOut[3], $s3ql['format'],''));
  903. #return ($validity[1]);
  904. }
  905. break;
  906. }
  907. case 'rule':##EDIT RULE
  908. {
  909. $rule_id = $element_id;
  910. $info = $e_info;
  911. if($info['object']=='UID')
  912. {
  913. if($s3ql['where']['subject']!='')
  914. {#redirect to change class?
  915. $res3ql =array_diff_key($s3ql, array('edit'=>'', 'update'=>''));
  916. $res3ql['where'] = array_diff_key($res3ql['where'], array('rule_id'=>''));
  917. $res3ql = array_merge($res3ql, compact('db', 'user_id'));
  918. $res3ql['edit']='class';
  919. $res3ql['where']['class_id']=$info['subject_id'];
  920. $done = S3QLaction($res3ql);
  921. return ($done);
  922. }
  923. else {
  924. return (formatReturn($GLOBALS['error_codes']['wrong_input'], 'Rule '.$element_id.' cannot be edited. To change the subject of the relation please use edit class', $s3ql['format']));
  925. }
  926. }
  927. else
  928. {
  929. #permission was verified before switch
  930. if ($s3ql['set']['subject_id']!='') {
  931. #for log, need to keep track of old literal as well.
  932. $oldvalues['subject'] = $info['subject'];
  933. $class_info = s3info('class', $s3ql['set']['subject_id'], $db);
  934. if (!is_array($class_info)) {
  935. return (formatReturn($GLOBALS['error_codes']['something_does_not_exist'], 'Class '.$s3ql['set']['subject_id'].' does not exist', $format,''));
  936. #return ($something_does_not_exist.'<message>Class '.$s3ql['set']['subject_id'].' does not exist</message>');
  937. }
  938. $info['subject_id'] = $s3ql['set']['subject_id'];
  939. $info['subject']=$class_info['entity'];
  940. }
  941. else {
  942. if ($s3ql['set']['subject']!='') {

Large files files are truncated, but you can click here to view the full file