PageRenderTime 65ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/s3db3.5.10/s3dbcore/callback.php

https://code.google.com/p/s3db/
PHP | 2423 lines | 2139 code | 224 blank | 60 comment | 93 complexity | 61fae3ac9114fa5e9e4b16b4e109e1b0 MD5 | raw file

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

  1. <?php
  2. #Callbacks.php is for internal use only and fits the purpose of gathering all funcctions that are called as callback for an array
  3. function array_delete($ary,$key_to_be_deleted)
  4. {
  5. $new = array();
  6. if(is_string($key_to_be_deleted)) {
  7. if(!array_key_exists($key_to_be_deleted,$ary)) {
  8. return;
  9. }
  10. foreach($ary as $key => $value) {
  11. if($key != $key_to_be_deleted) {
  12. $new[$key] = $value;
  13. }
  14. }
  15. $ary = $new;
  16. }
  17. if(is_array($key_to_be_deleted)) {
  18. foreach($key_to_be_deleted as $del) {
  19. $ary=array_delete($ary,$del);
  20. }
  21. }
  22. return ($ary);
  23. }
  24. function remove_numeric_id($uid)
  25. {
  26. if(is_numeric($uid))
  27. return ('');
  28. }
  29. function uid_str($array, $field) #To be used with array_walk
  30. {
  31. #return $array;
  32. return $array[$field];
  33. }
  34. function grab_uid($array)
  35. {
  36. return $array['uid'];
  37. }
  38. function comma_split($v, $w)
  39. {
  40. $v .= $w.', ';
  41. return $v;
  42. }
  43. function bar_split($v, $w)
  44. {
  45. $v .= $w.'|';
  46. return $v;
  47. }
  48. function grab_delete($element)
  49. {
  50. return $element['delete'];
  51. }
  52. function grab_permission($user)
  53. {
  54. return ($user['permissionOnResource']);
  55. }
  56. function grab_verb($rule)
  57. {
  58. return $rule['verb'];
  59. }
  60. function grab_subject($rule)
  61. {
  62. return $rule['subject'];
  63. }
  64. function grab_object($rule, $verb)
  65. {
  66. if($rule['verb']==$verb)
  67. return $rule['object'];
  68. }
  69. function grab_value($statement)
  70. {
  71. if($statement['file_name']=='')
  72. return $statement['value'];
  73. else
  74. return $statement['file_name'];
  75. }
  76. function notes($instance_id, $db)
  77. {
  78. $db->query("select notes from s3db_resource where resource_id = '".$instance_id."'");
  79. if($db->next_record())
  80. return ($db->f('notes'));
  81. else {
  82. return ($instance_id);
  83. }
  84. }
  85. function letter($from)
  86. {
  87. return (strtoupper(substr($from,0,1)));
  88. }
  89. function permissionModelComp($permLevel, $permModel='nsy')
  90. {
  91. #Is permission defined using the new model?
  92. if(!ereg('^['.$permModel.'-]+$',$permLevel))
  93. {
  94. $permision2user = str_replace(array('0','1','2'), str_split($permModel), $permLevel);
  95. }
  96. else {
  97. $permision2user= $permLevel;
  98. }
  99. return ($permision2user);
  100. }
  101. function allowed($pL, $digit2check,$isOwner,$state=3,$model='nsy')
  102. {
  103. $literal = str_split($model);
  104. $numeric = range(0,2);
  105. $permissionOnSlot = substr($pL,$digit2check,1);
  106. #If digit corrsponds to highest, or digit is middle and user is owner, return true. Otherwise is false
  107. if(strtolower($permissionOnSlot)==substr($model,$state-1,1)) #if it is the same as the highest inthe model)
  108. {
  109. return (true);
  110. }
  111. elseif (strtolower($permissionOnSlot)==substr($model,$state-2,1) && $isOwner) {
  112. return (true);
  113. }
  114. else {
  115. return (False);
  116. }
  117. return (false);
  118. }
  119. function grab_resource_id($statement)
  120. {#call it statement but its the same for classes and instances
  121. if (is_array($statement)) {
  122. return $statement['resource_id'];
  123. }
  124. else {
  125. return ($statement);
  126. }
  127. }
  128. function grab_just_object($rule)
  129. {
  130. return $rule['object'];
  131. }
  132. function grab_nonUID_objects($R)
  133. {
  134. extract($R);
  135. foreach($rules as $rule_info)
  136. {
  137. #echo $rule_info['object'].'<br />';
  138. if(!resourceObject(array('regexp'=>$regexp, 'db'=>$db, 'rule_info'=>$rule_info, 'project_id'=>$project_id)))
  139. $objects[] = $rule_info['object'];
  140. }
  141. #echo '<pre>';print_r($objects);
  142. return $objects;
  143. }
  144. function grab_UID_objects($R)
  145. {
  146. extract($R);
  147. foreach($rules as $rule_info)
  148. {
  149. if(resourceObject(array('rule_id'=>$rule_info['rule_id'], 'db'=>$db, 'project_id'=>$project_id)))
  150. $rule_info['object_rule_id'] = ruleId4object(array('regexp'=>$regexp, 'db'=>$db, 'object'=>$rule_info['object'], 'project_id'=>$project_id));
  151. $rule_info['object_class_id'] = classID4object(array('db'=>$db, 'object'=>$rule_info['object'], 'project_id'=>$project_id));
  152. $data[] = $rule_info;
  153. }
  154. #echo '<pre>';print_r($data);
  155. return $data;
  156. }
  157. function grab_id($element, $list_of_elements)
  158. {
  159. if (!is_array($list_of_elements)) {
  160. $list_of_elements = array('sim'=>$list_of_elements);
  161. }
  162. foreach ($list_of_elements as $i=>$e_info) {
  163. $grabbed[$i] = $e_info[$element.'_id'];
  164. }
  165. if (!is_array($list_of_elements)) {
  166. $list_of_elements = $grabbed['sim'];
  167. }
  168. return ($grabbed);
  169. }
  170. function include_acl($elements, $user_id, $db)
  171. {
  172. if(is_array($elements))
  173. foreach ($elements as $project)
  174. {
  175. #$project['acl'] = find_final_acl($user_id, $project['project_id'], $db);
  176. $data[] = $project;
  177. }
  178. return $data;
  179. }
  180. function include_data_acl($D)
  181. {
  182. extract($D);
  183. if(!is_array($instances))
  184. $instances[0] = $instance_info;
  185. foreach ($instances as $instance_info)
  186. {
  187. $instance_info['dataAcl'] = dataAcl(compact('user_id', 'db', 'instance_info'));
  188. $data[] = $instance_info;
  189. }
  190. return $data;
  191. }
  192. function include_statement_acl($elements, $user_id, $db)
  193. {
  194. if(is_array($elements))
  195. foreach ($elements as $statement)
  196. {
  197. $statement_info = $statement;
  198. $statement['dataAcl'] = statementAcl(compact('user_id', 'db', 'statement_info'));
  199. $data[] = $statement;
  200. }
  201. return $data;
  202. }
  203. function include_class_id($rules, $db)
  204. {
  205. #echo '<pre>class';print_r($rules);
  206. if(is_array($rules))
  207. foreach ($rules as $rule_info)
  208. {
  209. $rule_id = $rule_info['rule_id'];
  210. $rule_info['subject_class_id'] = get_resource_id_from_rule(compact('rule_id', 'db'));
  211. $data[] = $rule_info;
  212. }
  213. #echo '<pre>class';print_r($data);
  214. return $data;
  215. }
  216. function include_subject_class_id($S)
  217. {
  218. extract($S);
  219. if(is_array($rules))
  220. foreach ($rules as $rule_info)
  221. {
  222. $rule_id = $rule_info['rule_id'];
  223. $entity = $rule_info['subject'];
  224. $rule_info['subject_class_id'] = classID4entity(compact('db', 'entity', 'project_id'));
  225. $data[] = $rule_info;
  226. }
  227. #echo '<pre>class';print_r($data);
  228. return $data;
  229. }
  230. function include_rule_info($statements, $project_id, $db)
  231. {
  232. foreach ($statements as $statement_info)
  233. {
  234. $rule_id = $statement_info['rule_id'];
  235. $rule_info = s3info('rule', $rule_id, $db);
  236. $statement_info['subject'] = $rule_info['subject'];
  237. $statement_info['verb'] = $rule_info['verb'];
  238. $statement_info['object'] = $rule_info['object'];
  239. $entity = $rule_info['subject'];#for the subject
  240. $statement_info['subject_class_id'] = fastClassID(compact('entity', 'project_id', 'db'));
  241. $entity = $rule_info['object']; #...and for the object
  242. $statement_info['object_class_id'] = fastClassID(compact('entity', 'project_id', 'db'));
  243. $data[] = $statement_info;
  244. }
  245. return $data;
  246. }
  247. function include_object_class_id($rules, $project_id, $db)
  248. {
  249. if (is_array($rules))
  250. foreach ($rules as $rule_info)
  251. {
  252. $rule_id = $rule_info['rule_id'];
  253. $entity = $rule_info['object'];
  254. $rule_info['object_class_id'] = fastClassID(compact('db', 'entity', 'project_id'));
  255. $data[] = $rule_info;
  256. }
  257. return $data;
  258. }
  259. function include_all_class_id($S)
  260. {
  261. extract($S);
  262. if(is_array($rules))
  263. foreach ($rules as $rule_info)
  264. {
  265. $rule_id = $rule_info['rule_id'];
  266. $entity = $rule_info['subject'];
  267. $rule_info['subject_class_id'] = fastClassID(compact('db', 'entity', 'project_id'));
  268. $entity = $rule_info['object'];
  269. $rule_info['object_class_id'] = fastClassID(compact('entity', 'project_id', 'db'));
  270. $data[] = $rule_info;
  271. }
  272. #echo '<pre>class';print_r($data);
  273. return $data;
  274. }
  275. function get_rule_drop_down_menu($D)
  276. {
  277. extract($D);
  278. if($rule_info=='')
  279. {
  280. $rule_info = s3info('rule', $rule_id, $db);
  281. #$rule_info = include_all_class_id(compact('rule_info', 'db', 'project_id', 'user_id'));
  282. }
  283. #if(is_array($_SESSION[$user_id]['instances'][$rule_info['object_class_id']])) {
  284. # $instances = $_SESSION[$user_id]['instances'][$class_id];
  285. #}
  286. #else {
  287. $s3ql = compact('db', 'user_id');
  288. $s3ql['select'] = '*';
  289. $s3ql['from'] = 'instances';
  290. $s3ql['where']['class_id'] = $rule_info['object_id'];
  291. #echo '<pre>';print_r($s3ql);exit;
  292. #$s3ql['where']['project_id'] = $project_id;
  293. #$instances = s3list($s3ql);
  294. $instances = S3QLAction($s3ql);
  295. #echo '<pre>';print_r($instances);
  296. #if(!is_array($instances))
  297. #{
  298. #$s3ql['where']='';
  299. #$s3ql['where']['project_id']=$project_id;
  300. #$s3ql['where']['entity']=$rule_info['object'];
  301. #$instances = S3QLaction($s3ql);
  302. #$_SESSION[$user_id]['instances'][$rule_info['object_class_id']] = $instances;
  303. #}
  304. #}
  305. $inputBox .= '<select name="'.$select_name.'" size="1" style="background-color: lightyellow; font-size: 8pt" size="1">';
  306. $inputBox .= '<option value="" selected></option>';
  307. if(is_array($instances))
  308. foreach($instances as $object_instance_info)
  309. {$inputBox .= '<option value ="'.$object_instance_info['resource_id'].'" >'.$object_instance_info['notes'].' <sub>(ID#'.$object_instance_info['resource_id'].')</sub></option>';
  310. }
  311. $inputBox .= '</select>';
  312. return $inputBox;
  313. }
  314. function include_object_drop_down_menu($S)
  315. {
  316. extract($S);
  317. if($rule_info['object_class_id']!='')
  318. {
  319. $s3ql = compact('db', 'user_id');
  320. $s3ql['select'] = 'resource_id,notes';
  321. $s3ql['from'] = 'instances';
  322. $s3ql['where']['resource_class_id'] = $rule_info['object_class_id'];
  323. $s3ql['where']['project_id'] = $project_id;
  324. $instances = S3QLaction($s3ql);
  325. $inputBox .= '<select name="input[]" size="1" style="background-color: lightyellow; font-size: 8pt" size="1">';
  326. $inputBox .= '<option value="" selected></option>';
  327. if(is_array($instances))
  328. foreach($instances as $object_instance_info)
  329. {$inputBox .= '<option value ="'.$object_instance_info['resource_id'].'" >'.$object_instance_info['notes'].' <sub>(ID#'.$object_instance_info['resource_id'].')</sub></option>';
  330. }
  331. $inputBox .= '</select>';
  332. $rule_info['object_drop_down_menu'] = $inputBox;
  333. }
  334. return ($rule_info);
  335. }
  336. function include_instance_class_id($instances, $project_id, $db)
  337. {
  338. foreach($instances as $instance_info)
  339. {if($instance_info['resource_class_id']=='')
  340. {
  341. $entity = $instance_info['entity'];
  342. if($project_id=='') $project_id = $instance_info['project_id'];
  343. $instance_info['resource_class_id'] = resourceClassID4Instance(compact('entity', 'project_id', 'db'));
  344. $instance_info['class_id'] = resourceClassID4Instance(compact('entity', 'project_id', 'db'));
  345. }
  346. else
  347. $instance_info['class_id'] = $instance_info['resource_class_id'];
  348. $data[] = $instance_info;
  349. }
  350. return $data;
  351. }
  352. function include_button_notes($statements, $project_id, $db)
  353. {$action = $GLOBALS['webaction'];
  354. foreach($statements as $statement_info)
  355. {$rule_id = $statement_info['rule_id'];
  356. if($statement_info['object_id']!='')
  357. {
  358. $instance_id = $statement_info['value'];
  359. $sql = "select notes from s3db_resource where resource_id = '".$instance_id."'";
  360. $db->query($sql, __LINE__, __FILE__);
  361. if($db->next_record())
  362. {
  363. $notes = $db->f('notes');
  364. if($notes!='')
  365. $statement_info['button_notes'] = $notes;
  366. else
  367. $statement_info['button_notes'] = $instance_id;
  368. $statement_info['instance_button'] = '<input type="button" value="'.$statement_info['button_notes'].'" onClick="window.open(\''.$action['instance'].'&instance_id='.$instance_id.'\')">';
  369. }
  370. }
  371. else
  372. $statement_info['instance_button'] = $statement_info['value'];
  373. $data[] = $statement_info;
  374. }
  375. return $data;
  376. }
  377. function button_notes($statement_info, $user_id, $db)
  378. {$action = $GLOBALS['webaction'];
  379. $rule_id = $statement_info['rule_id'];
  380. if($statement_info['object_id']!='')
  381. {
  382. $instance_id = $statement_info['value'];
  383. $sql = "select notes from s3db_resource where resource_id = '".$instance_id."'";
  384. $db->query($sql, __LINE__, __FILE__);
  385. if($db->next_record())
  386. {
  387. $notes = $db->f('notes');
  388. if($notes!='')
  389. $button_notes = $notes;
  390. else
  391. $button_notes = $instance_id;
  392. }
  393. }
  394. else
  395. $button_notes = $statement_info['value'];
  396. return $button_notes;
  397. }
  398. function value($statement_info, $user_id, $db)
  399. {$action = $GLOBALS['action'];
  400. $rule_id = $statement_info['rule_id'];
  401. $rule_info = s3info('rule', $rule_id, $db);
  402. if ($rule_info['object_id']!='') {
  403. $instance_id = $statement_info['value'];
  404. $intance_info = URI('I'.$instance_id, $user_id, $db);
  405. $notes = $intance_info['notes'];
  406. if($notes!='')
  407. $statement_info['button_notes'] = $notes;
  408. else
  409. $statement_info['button_notes'] = $instance_id;
  410. $value = '<input type="button" value="'.$statement_info['button_notes'].'" onClick="window.open(\''.$action['instance'].'&instance_id='.$instance_id.'\')">';
  411. }
  412. else {
  413. $value = include_fileLinks($statement_info, $db);
  414. }
  415. return ($value);
  416. }
  417. function include_button($statements, $user_id, $db)
  418. {
  419. $action = $GLOBALS['webaction'];
  420. foreach ($statements as $key=>$statement_info) {
  421. $statement_info['button'] = value($statement_info, $user_id, $db);
  422. $data[] = $statement_info;
  423. }
  424. return ($data);
  425. }
  426. function include_statements($S)
  427. {
  428. extract($S);
  429. if(is_array($instances)) {
  430. $class_instances = array();
  431. $statements = array();
  432. foreach ($instances as $instance_info)
  433. {
  434. $s3ql = compact('db', 'user_id');
  435. $s3ql['select']='*';
  436. $s3ql['from'] = 'statements';
  437. $s3ql['where']['item_id'] = $instance_info['resource_id'];
  438. #echo '<pre>';print_r($s3ql);exit;
  439. $statements = S3QLaction($s3ql);
  440. if(is_array($statements))
  441. {
  442. #figure out the classes of the subject and object of the rule_id
  443. if (!is_array($rules)) {
  444. $class_id = $instance_info['class_id'];
  445. $s3ql=compact('user_id','db');
  446. $s3ql['select']='*';
  447. $s3ql['from']='rules';
  448. $s3ql['where']['subject_id']=$class_id;
  449. #$s3ql['where']['object']='!="UID"';
  450. $rules = S3QLaction($s3ql);
  451. }
  452. #now add rule_info to the statements
  453. if (is_array($statements) && is_array($rules)) {
  454. #echo '<pre>';print_r($rule_ids);
  455. $rule_ids = array_map('grab_rule_id', $rules);
  456. $rules = array_combine($rule_ids, $rules);
  457. #echo '<pre>';print_r($rules);
  458. $instance_statements = array();
  459. foreach ($statements as $statement_info) {
  460. $statement_info['subject'] = $rules[$statement_info['rule_id']]['subject'];
  461. $statement_info['verb'] = $rules[$statement_info['rule_id']]['verb'];
  462. $statement_info['object'] = $rules[$statement_info['rule_id']]['object'];
  463. $statement_info['subject_id'] = $rules[$statement_info['rule_id']]['subject_id'];
  464. $statement_info['object_id'] = $rules[$statement_info['rule_id']]['object_id'];
  465. if(!$omit_button_notes)
  466. $statement_info['button_notes']=button_notes($statement_info, $user_id, $db);
  467. if($statement_info['subject']!='' && $statement_info['verb']!='' && $statement_info['object']!='')
  468. $instance_statements[] = $statement_info;
  469. else {
  470. $instance_statements[]=array();
  471. }
  472. #echo '<pre>';print_r($statement_info);
  473. }
  474. $statements = array_filter($instance_statements);
  475. #$statements = include_rule_info($statements, $project_id, $db);
  476. #$statements = include_button_notes($statements, $project_id, $db);
  477. $instance_info['stats'] = $statements;
  478. }
  479. }
  480. else {
  481. $instance_info['stats']='';
  482. }
  483. $class_instances[] = $instance_info;
  484. } #closes the foreach
  485. }
  486. return $class_instances;
  487. }
  488. function include_statements1($S)
  489. {
  490. extract($S);
  491. if(is_array($instances))
  492. foreach ($instances as $instance_info)
  493. {
  494. $s3ql = compact('db', 'user_id');
  495. $s3ql['select']='*';
  496. $s3ql['from'] = 'statements';
  497. $s3ql['where']['instance_id'] = $instance_info['resource_id'];
  498. #$s3ql['where']['project_id'] = $project_id;
  499. #$done = S3QLaction($s3ql);
  500. $done = s3list($s3ql);
  501. if(is_array($done))
  502. {
  503. $statements = $done;
  504. #figure out the classes of the subject and object of the rule_id
  505. #$statements = include_rule_info($statements, $project_id, $db);
  506. #$statements = include_button_notes($statements, $project_id, $db);
  507. $instance_info['stats'] = $done;
  508. }
  509. $data[] = $instance_info;
  510. }
  511. #echo '<pre>';print_r($data);
  512. return $data;
  513. }
  514. function replace_owner_acl($data, $db)
  515. {
  516. foreach($data as $D)
  517. {
  518. if($D['acl_project_id']!='')
  519. {
  520. #$acl = find_final_acl($D['account_id'], $D['acl_project_id'], $db);
  521. $D['acl_rights'] = $acl;
  522. }
  523. $Corrected_data[] = $D;
  524. }
  525. return $Corrected_data;
  526. }
  527. function account_id_as_key($users)
  528. {
  529. if(!is_array($users))
  530. $user_info = array('key_me'=>$users);
  531. else {
  532. $user_info = $users;
  533. }
  534. foreach($user_info as $user)
  535. if($user['account_id']!='')
  536. $data[$user['account_id']] = $user;
  537. if(!is_array($users))
  538. $data = $data['key_me'];
  539. return $data;
  540. }
  541. function number_as_key($users)
  542. {
  543. if(is_array($users))
  544. foreach($users as $account_id=>$user_info)
  545. $data[] = $user_info;
  546. return $data;
  547. }
  548. function grab_project_id($project)
  549. {
  550. if (is_array($project)) {
  551. return $project['project_id'];
  552. }
  553. else {
  554. return ($project);
  555. }
  556. }
  557. function grab_rule_id($rule)
  558. {
  559. if (is_array($rule)) {
  560. return $rule['rule_id'];
  561. }
  562. else {
  563. return ($rule);
  564. }
  565. }
  566. function grab_class_instance_id($resource)
  567. {
  568. global $element;
  569. if($element=='instance' || $element=='statement')
  570. {
  571. $resource['instance_id'] = $resource['resource_id'];
  572. if($resource['resource_class_id']!='')
  573. $resource['class_id'] = $resource['resource_class_id'];
  574. }
  575. elseif($element=='class')
  576. $resource['class_id'] = $resource['resource_id'];
  577. return $resource;
  578. }
  579. function delete_empty_statements($statement)
  580. {
  581. if($statement['value']!='')
  582. return $statement;
  583. }
  584. function replace_created_by($data, $db)
  585. {
  586. if(is_array($data))
  587. foreach($data as $x)
  588. {$x['account_name'] = find_user_loginID(array('account_id'=>$x['account_id'], 'db'=>$db));
  589. $x['created_byID'] = find_user_loginID(array('account_id'=>$x['created_by'], 'db'=>$db));
  590. $x['project_owner'] = find_user_loginID(array('account_id'=>$x['created_by'], 'db'=>$db));
  591. $z[] = $x;
  592. }
  593. return $z;
  594. }
  595. function replace_project_id($data)
  596. {
  597. global $projects;
  598. $data['project_name'] = $projects[$data['project_id']];
  599. return $data;
  600. }
  601. function replace_project_id_and_name($data)
  602. {
  603. global $idNames;
  604. $data['project_name'] = $idNames[$data['project_id']].' (ID:'.$data['project_id'].')';
  605. return $data;
  606. }
  607. function replace_id_with_name($data)
  608. {
  609. global $element, $idNames;
  610. $data[$element.'_name'] = $idNames[$data[$element.'_id']];
  611. return $data;
  612. }
  613. function random_string($length)
  614. {
  615. $acceptedChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYS0123456789';
  616. $max = strlen($acceptedChars)-1;
  617. $random = null;
  618. for($i=0; $i < $length; $i++) {
  619. $random .= $acceptedChars{mt_rand(0, $max)};
  620. }
  621. return $random;
  622. }
  623. function trimmit($something)
  624. {
  625. return trim($something);
  626. }
  627. function ValuesToFileLinks($statement)
  628. {global $key;
  629. if($statement['file_name']!='')
  630. {$filelink = '<a href="'.S3DB_URI_BASE.'/download.php?key='.$key.'&project_id='.$statement['project_id'].'&resource_id='.$statement['resource_id'].'&rule_id='.$statement['rule_id'].'&statement_id='.$statement['statement_id'].'">';
  631. $statement['value'] = $filelink.$statement['file_name'].'</a>';
  632. }
  633. return $statement;
  634. }
  635. function Values2Links($statement)
  636. {global $action;
  637. if(!is_array($statement)) $statement = array(0=>$statement);
  638. foreach($statement as $stat)
  639. {
  640. if($stat['file_name']!='')
  641. {$filelink = '<a href="'.$action['download'].'">';
  642. $stat['value'] = $filelink.$stat['file_name'].'</a>';
  643. }
  644. $data[] = $stat;
  645. }
  646. return $data;
  647. }
  648. function include_fileLinks($statement, $db)
  649. {
  650. $action = $GLOBALS['webaction'];
  651. #echo '<pre>';print_r($action);
  652. if($statement['file_name']!='')
  653. {
  654. #$filelink = '<a href="'.S3DB_URI_BASE.'/download.php?key='.$key.'&project_id='.$statement['project_id'].'&resource_id='.$statement['resource_id'].'&rule_id='.$statement['rule_id'].'&statement_id='.$statement['statement_id'].'">';
  655. #Find the file location, read the filesize, return that size to the user
  656. ereg('(.*)\.([a-zA-Z0-9]+)$', $statement['file_name'], $ext);
  657. if($ext){
  658. $name = $ext[1];
  659. $ext = $ext[2];
  660. }
  661. $fileLocated = $GLOBALS['uploads'].'/'.$statement['project_folder'].'/'.$name.'_'.urlencode($statement['project_id'].'_'.$statement['resource_id'].'_'.$statement['rule_id'].'_'.$statement['statement_id']).'.'.$ext;
  662. if(is_file($fileLocated)){
  663. $statement['file_size'] =filesize($fileLocated);
  664. }
  665. $filelink = '<a href = "'.$action['download'].'&statement_id='.$statement['statement_id'].'">';
  666. $statement['value'] = $filelink.$statement['file_name'].'</a>';
  667. }
  668. return $statement;
  669. }
  670. function ValuesToFileURLs($statement)
  671. {global $key;
  672. if($statement['file_name']!='')
  673. {$filelink = '<http://localhost/s3db/download.php?key='.$key.'&project_id='.$statement['project_id'].'&resource_id='.$statement['resource_id'].'&rule_id='.$statement['rule_id'].'&statement_id='.$statement['statement_id'].'">';
  674. $statement['value'] = $filelink;
  675. }
  676. return $statement;
  677. }
  678. function sqlcharacters($value)
  679. {$regexp = $GLOBALS['regexp'];
  680. #does the value have regular expressions?
  681. $cleanValue = preg_quote($value, '/');
  682. #echo '<BR>'.$cleanValue.'<BR>';
  683. if (strlen($cleanValue) > strlen($value))
  684. {
  685. $valueToQuery = stripslashes($value);
  686. #is this a "not"? if it's not, add the regexp part and the quotes
  687. #if(substr($valueToQuery,0,1)!='!')
  688. # $valueToQuery = $regexp." '".$valueToQuery."'";
  689. #echo $valueToQuery;
  690. }
  691. elseif(strlen($value) == strlen($cleanValue))
  692. $valueToQuery = "= '".$value."'";
  693. return $valueToQuery;
  694. }
  695. function parse_regexp($value)
  696. {
  697. $regexp = $GLOBALS['regexp'];
  698. $matchRegexp = substr($value, 0, strlen($regexp));
  699. $matchNeg = (substr($value,0,2)=='!=');
  700. $matchNeg = strstr($value, '!=');
  701. #does the value have regular expressions?
  702. $cleanValue = preg_quote($value, '/');
  703. #echo '<BR>'.$cleanValue.'<BR>';
  704. if($matchRegexp==$regexp)
  705. $valueToQuery = stripslashes($value);
  706. elseif(!$matchNeg)
  707. $valueToQuery = "= '".$value."'";
  708. else
  709. $valueToQuery = $value;
  710. return $valueToQuery;
  711. }
  712. function parse_regexp1($value)
  713. {$regexp = $GLOBALS['regexp'];
  714. #does the value have regular expressions?
  715. $cleanValue = preg_quote($value, '/');
  716. if (strlen($cleanValue) > strlen($value))
  717. {
  718. #echo substr($value, 0, strlen($regexp));
  719. if((substr($value,0, 2)!='!=') && (substr($value, 0, strlen($regexp))!=$regexp) && (substr($value, 0,1)!='='))
  720. $valueToQuery = $regexp." '".stripslashes($value)."'";
  721. else
  722. $valueToQuery = stripslashes($value);
  723. }
  724. elseif(strlen($value) == strlen($cleanValue) && (substr($value, 0, strlen($regexp))!=$regexp) && (substr($value, 0,1)!='='))
  725. $valueToQuery = "= '".$value."'";
  726. else {
  727. $valueToQuery = $value;
  728. }
  729. return $valueToQuery;
  730. }
  731. function S3QLSyntax($s3qlKeys)
  732. {
  733. if(!in_array($s3qlKeys, $syntax))
  734. return $s3qlKeys;
  735. }
  736. function grab_acl($x)
  737. {
  738. extract($x);
  739. #echo '<pre>';print_r($elements);
  740. foreach ($elements as $proj)
  741. {
  742. $proj['acl'] = $data['acl'];
  743. $data[] = $proj;
  744. }
  745. return $data;
  746. }
  747. function trim_quotes ($cell)
  748. { return $cell = trim ($cell, "\"'");
  749. }
  750. function remove_empty_lines($line)
  751. {
  752. if(!trim($line)=='')
  753. return ($line);
  754. }
  755. function clean_inputs($data)
  756. {
  757. if (!is_array($data)) { #this is for things that don't come in arrays, for example for using with array_map
  758. $data2clean = array('cleanme'=>$data);
  759. }
  760. else {
  761. $data2clean = $data;
  762. }
  763. #echo '<pre>';print_r($data);
  764. foreach($data2clean as $key=>$input) {
  765. if (ereg('(subject|verb|object|entity|cleanme)',$key)) {
  766. $clean_data[$key] = str_replace('(', '\\\(', $input);
  767. $clean_data[$key] = str_replace(')', '\\\)', $clean_data[$key]);
  768. }
  769. }
  770. if (!is_array($data)) {
  771. $clean_data = $clean_data['cleanme'];
  772. }
  773. return ($clean_data);
  774. }
  775. function create_element_list($element_ids)
  776. {
  777. #string create_something_list(array)
  778. #function requires an array with elements that have _id in one of the keys. Output is a string
  779. #Build a query for permissions
  780. reset($element_ids);
  781. $eles .= ''.current($element_ids).'$';
  782. while(each($element_ids))
  783. $eles .= '|'.current($element_ids).'$';
  784. #remove two extra chars that each throuw in for some reason. All queries should be performed specifically on these projects
  785. $eles = substr($eles, 0, strlen($eles)-2);
  786. return ($eles);
  787. }
  788. function create_list($element_ids)
  789. {
  790. #string create_something_list(array)
  791. #function requires an array with elements that have _id in one of the keys. Output is a string
  792. #Build a query for permissions
  793. if(!is_array($element_ids)) return '';#on exception return empty string
  794. reset($element_ids);
  795. $eles .= '^'.current($element_ids).'$';
  796. while(each($element_ids))
  797. $eles .= '|^'.current($element_ids).'$';
  798. #remove two extra chars that each throuw in for some reason. All queries should be performed specifically on these projects
  799. $eles = substr($eles, 0, strlen($eles)-3);
  800. return ($eles);
  801. }
  802. function createCharList($elements, $char)
  803. {
  804. for ($i=0; $i < count($elements); $i++) {
  805. if($str!='') $str .= $char;
  806. $str .= $elements[$i];
  807. }
  808. return ($str);
  809. }
  810. function create_project_id_list($projects)
  811. {
  812. #string create_project_id_list($array)
  813. #function reaquires an array with many projects. Output is a string
  814. #Build a query for permissions
  815. $projects = array_map('grab_project_id',$projects);
  816. reset($projects);
  817. $projs .= '^'.current($projects).'$';
  818. while(each($projects))
  819. $projs .= '|^'.current($projects).'$';
  820. #remove two extra chars that each throuw in for some reason. All queries should be performed specifically on these projects
  821. $projs = substr($projs, 0, strlen($projs)-3);
  822. return ($projs);
  823. }
  824. function create_class_id_list($classes)
  825. {
  826. #string create_project_id_list($array)
  827. #function reaquires an array with many projects. Output is a string
  828. #Build a query for permissions
  829. $classes = array_map('grab_resource_id',$classes);
  830. reset($classes);
  831. $clas .= '^'.current($classes).'$';
  832. while(each($classes))
  833. $clas .= '|^'.current($classes).'$';
  834. #remove two extra chars that each throuw in for some reason. All queries should be performed specifically on these projects
  835. $clas = substr($clas, 0, strlen($clas)-3);
  836. return ($clas);
  837. }
  838. function create_rule_id_list($rules) #i know, it's the previos function with a diff name, to abstract later
  839. {
  840. $rules = array_map('grab_rule_id',$rules);
  841. reset($rules);
  842. $rls .= '^'.current($rules).'$';
  843. while(each($rules))
  844. $rls .= '|^'.current($rules).'$';
  845. #remove two extra chars that each throuw in for some reason. All queries should be performed specifically on these projects
  846. $rls = substr($rls, 0, strlen($rls)-3);
  847. return ($rls);
  848. }
  849. function create_permission_list($projects)
  850. {
  851. #string create_project_id_list($array)
  852. #function reaquires an array with many projects. Output is a string
  853. #Build a query for permissions
  854. $projects = array_map('grab_project_id',$projects);
  855. reset($projects);
  856. $projs .= '(^|_)'.current($projects).'_';
  857. while(each($projects))
  858. $projs .= '|(^|_)'.current($projects).'_';
  859. #remove two extra chars that each throuw in for some reason. All queries should be performed specifically on these projects
  860. $projs = substr($projs, 0, strlen($projs)-7);
  861. return ($projs);
  862. }
  863. function get_show_me($resource_info, $rules, $db)
  864. {
  865. if($rules==''){
  866. $sql = "select rule_id, subject, verb, object from s3db_rule where project_id='".$_REQUEST['project_id']."' and subject='".$resource_info['entity']."' order by verb, object";
  867. $db->query($sql, __LINE__, __FILE__);
  868. while($db->next_record())
  869. {
  870. $rules[] = Array('rule_id'=>$db->f('rule_id'),
  871. 'subject'=>$db->f('subject'),
  872. 'verb'=>$db->f('verb'),
  873. 'object'=>$db->f('object'));
  874. }
  875. }
  876. $posted_show_me = $_POST['show_me'];
  877. #echo '<pre>';print_r($posted_show_me);exit;
  878. if(count($posted_show_me) > 0)
  879. {
  880. $show_me = Array();
  881. foreach($rules as $i => $value)
  882. {
  883. $show_me_id = 'show_me_'.$rules[$i]['rule_id'];
  884. $show_me_val = 'show_me_val_'.$rules[$i]['rule_id'];
  885. if(count($posted_show_me) > 0)
  886. {
  887. foreach($posted_show_me as $j =>$value)
  888. {
  889. if($posted_show_me[$j] == $show_me_val)
  890. {
  891. array_push($show_me, $rules[$i]);
  892. }
  893. }
  894. }
  895. }
  896. #echo '<pre>';print_r($show_me);exit;
  897. return $show_me;
  898. }
  899. }
  900. function get_rule_value_pairs($resource_info, $rules, $db)
  901. {
  902. $rule_value_pairs = array();
  903. foreach ($rules as $key=>$value) {
  904. if($_POST['rule_1_'.$value['rule_id']]!='')
  905. $rule_value_pairs['rule_1_'.$value['rule_id']]=$_POST['rule_1_'.$value['rule_id']];
  906. if($_POST['rule_2_'.$value['rule_id']]!='')
  907. $rule_value_pairs['rule_2_'.$value['rule_id']]=$_POST['rule_2_'.$value['rule_id']];
  908. }
  909. return ($rule_value_pairs);
  910. }
  911. function gatherInputs($g)
  912. {extract($g);
  913. $required = array(
  914. 'key'=>array(),
  915. 'project'=>array('project_name'),
  916. 'collection'=>array('project_id', 'entity'),
  917. 'rule'=>array('project_id', 'subject', 'subject_id', 'verb', 'object'),
  918. 'item'=>array('collection_id'),
  919. 'statement'=>array('item_id', 'rule_id', 'value'),
  920. 'file' => array('item_id', 'rule_id', 'filekey'),
  921. 'user' => array('account_lid', 'account_email'),
  922. 'group'=>array('account_lid'));
  923. #$map = $GLOBALS['s3map'];
  924. $optional = array(
  925. 'key'=>array('key_id', 'expires'),
  926. 'project'=>array('project_id', 'project_description', 'project_owner','status', 'created_on', 'created_by'),
  927. 'collection'=>array('collection_id', 'rule_id', 'notes','status', 'created_on', 'created_by'),
  928. 'rule'=>array('rule_id', 'verb_id', 'object_id','notes', 'validation','status', 'created_on', 'created_by'),
  929. 'item'=>array('item_id', 'notes', 'entity', 'project_id','status', 'created_on', 'created_by'),
  930. 'statement'=>array('statement_id', 'project_id', 'notes', 'status', 'created_on', 'created_by'),
  931. 'file' => array('statement_id', 'project_id', 'notes','status', 'created_on', 'created_by'),
  932. 'user' => array('user_id', 'account_pwd', 'account_uname', 'account_email', 'account_phone', 'addr1', 'addr2', 'account_type','account_status', 'city', 'postal_code', 'state', 'country', 'created_on', 'created_by','permission_level'),
  933. 'group'=>array('group_id', 'account_pwd', 'created_on', 'created_by'));
  934. $possible = array_merge_recursive($required,$optional);
  935. #echo '<pre>';print_r($info);exit;
  936. #echo '<pre>';print_r($to_create);exit;
  937. #echo '<pre>';print_r($possible[$element]);
  938. #echo '<pre>';print_r($to_create);
  939. $map[$element] = $GLOBALS['s3map'][$GLOBALS['plurals'][$element]];
  940. #echo '<pre>';print_r($map[$element]);
  941. foreach ($possible[$element] as $pot_input) {
  942. if ($to_create[$pot_input]!='') {
  943. if(ereg('owner|created_by',$pot_input)){
  944. //check if user exists; otherwise use user_id
  945. $sql = "select * from s3db_account where account_id = '".$to_create[$pot_input]."'";
  946. $db->query($sql);
  947. if(!$db->next_record()) {
  948. $to_create[$pot_input] = $user_id;
  949. }
  950. }
  951. $value = $to_create[$pot_input];
  952. $fieldName=(in_array($pot_input, array_keys($map[$element])))?$map[$element][$pot_input]:$pot_input;
  953. $inputs[$fieldName]=addslashes($value);
  954. #echo '<pre>';print_r($inputs);
  955. }
  956. elseif($to_create[$pot_input]=='') #if it is empty, look for something that might replace it
  957. {
  958. #infer from empty required
  959. if(in_array($pot_input, $required[$element]))
  960. {$inputs = formatReturn($GLOBALS['error_codes']['something_missing'], $pot_input.' cannot be empty. '.$GLOBALS['messages']['syntax_message'], $format,'');
  961. return ($inputs);
  962. }
  963. #$inputs= ($GLOBALS['messages']['something_missing'].'<message>'.$pot_input.' cannot be empty. '.$GLOBALS['messages']['syntax_message'].'</message>');
  964. elseif(in_array($pot_input, $optional[$element]))
  965. {
  966. if (ereg('owner|created_by|status', $pot_input))
  967. $inputs[$pot_input]=(ereg('owner|created_by', $pot_input)?$user_id:(ereg('status', $pot_input)?'A':''));
  968. if(ereg('account_type', $pot_input))
  969. {
  970. $inputs[$pot_input] = substr($element,0,1);
  971. }
  972. if(ereg('account_pwd', $pot_input))
  973. {
  974. $inputs[$pot_input] = random_string(12);
  975. }
  976. if(ereg('account_uname', $pot_input))
  977. {
  978. $inputs[$pot_input] = $inputs['account_lid'];
  979. }
  980. if(ereg('entity', $pot_input))
  981. {
  982. $inputs[$pot_input] = $info['C'.$to_create['collection_id']]['entity'];
  983. }
  984. if(ereg('project_id', $pot_input))
  985. {
  986. if(ereg('item', $element))
  987. {$inputs[$pot_input] = ($info['C'.$to_create['collection_id']]['project_id']!='')?$info['C'.$to_create['collection_id']]['project_id']:$info['R'.$to_create['rule_id']]['project_id'];
  988. }
  989. if(ereg('statement|file', $element)) #PROJECT_ID WILL BE THAT OF THE RULE
  990. {
  991. $inputs[$pot_input] = ($info['R'.$to_create['rule_id']]['project_id']!='')?$info['R'.$to_create['rule_id']]['project_id']:$info['I'.$to_create['rule_id']]['project_id'];
  992. }
  993. }
  994. }
  995. }
  996. #echo '<pre>';print_r($inputs);exit;
  997. }
  998. return ($inputs);
  999. }
  1000. #$uid=uid('somthingsomethig#http://123.456.6.3:80/s3db/Cxxx');
  1001. #echo '1<pre>';print_r($uid);
  1002. function uid($complete_uid)
  1003. {#some uid might come with the identifier as the very first letter. For uid to work, uid must come WITHOUT that first identifier even if they are remote or include mothership.
  1004. $complete_uid=trim($complete_uid);
  1005. $qnames = array('s3db'=>$GLOBALS['s3db_info']['deployment']['mothership']);
  1006. $did = str_replace(array('s3db:'),array($qnames['s3db']),$complete_uid);
  1007. if(!ereg('(.*)/(.*)', $complete_uid, $out))
  1008. {
  1009. $uid = $complete_uid;
  1010. #$Did=(($_SERVER['HTTPS']!='')?'https://':'http://'.$_SERVER['SERVER_NAME'].'/'.strtok($_SERVER['PHP_SELF'], '/'));
  1011. $Did = $GLOBALS['Did'];
  1012. $MS='';
  1013. }
  1014. else {
  1015. $uid = end($out);
  1016. #echo $complete_uid;exit;
  1017. #not as for deployment, grab everything between start of string and last backslash. Truncate the 'uid' part of the string
  1018. if(!ereg('(.*)#(.*)$', $complete_uid, $out1))
  1019. {$Did = substr($complete_uid, 0, strlen($complete_uid)-(strlen($uid)+1));
  1020. $MS = 'http://s3db.org';
  1021. #Added 27Mar08
  1022. $Did = ereg_replace('^'.substr($uid, 0,1), '', $Did);
  1023. $uid = $Did.'/'.$uid;
  1024. }
  1025. #...except when MS is identified. In this case, Did is everything between cardinal and Uid :-)
  1026. else {
  1027. $MS = $out1[1];
  1028. $Did = substr($complete_uid, strlen($MS)+1, strlen($complete_uid)-strlen($uid)-strlen($MS)-2);
  1029. }
  1030. }
  1031. return (compact('uid', 'Did', 'MS'));
  1032. }
  1033. function fileFound($F)
  1034. {extract($F);
  1035. #$F must contain filekey, instance_id/instance_info, rule_id/rule_info, db, user_id
  1036. #Find out if the file already exists in the tmp directory
  1037. $filedata = get_filekey_data($filekey, $db);
  1038. #echo '<pre>';print_r($filedata);exit;
  1039. if(!$filedata || !is_array($filedata))
  1040. return False;# '<message>Filekey is not valid.</message>');
  1041. else
  1042. extract($filedata);
  1043. #$file_id = get_entry('file_transfer', 'file_id', 'filekey', $filekey, $db);
  1044. #list($name, $extension) = explode('.', $filedata['filename']);
  1045. $a=fileNameAndExtension($filedata['filename']);
  1046. extract($a);
  1047. #ereg('.([A-Za-z0-9]+)$',$filedata['filename'], $tokens);
  1048. #$extension= $tokens[1];
  1049. #$name = ereg_replace('.([A-Za-z0-9]+)$', '', $filedata['filename']);
  1050. $maindir = $GLOBALS['s3db_info']['server']['db']['uploads_folder'].$GLOBALS['s3db_info']['server']['db']['uploads_file'].'/tmps3db';
  1051. $old_file = $maindir.'/'.$file_id.'.'.$extension;
  1052. if(!is_file($old_file))
  1053. return False; # $something_does_not_exist."<message>File not found, please upload file first.</message>");
  1054. else
  1055. return (True);
  1056. }
  1057. function fileFoundInfo($F)
  1058. {extract($F);
  1059. #$F must contain filekey, instance_id/instance_info, rule_id/rule_info, db, user_id
  1060. #Find out if the file already exists in the tmp directory
  1061. $filedata = get_filekey_data($filekey, $db);
  1062. #echo '<pre>';print_r($filedata);exit;
  1063. if(!$filedata || !is_array($filedata))
  1064. return array(False, '<message>Filekey is not valid.</message>');
  1065. else
  1066. extract($filedata);
  1067. #$file_id = get_entry('file_transfer', 'file_id', 'filekey', $filekey, $db);
  1068. #list($name, $extension) = explode('.', $filedata['filename']);
  1069. #separate file name from extensions
  1070. $a=fileNameAndExtension($filedata['filename']);
  1071. extract($a);
  1072. #$tmp=explode(".", $filedata['filename']);
  1073. #ereg('.([A-Za-z0-9]+)$',$filedata['filename'], $tokens);
  1074. #echo '<pre>';print_r($tokens);exit;
  1075. #$extension= $tokens[1];
  1076. $maindir = $GLOBALS['s3db_info']['server']['db']['uploads_folder'].$GLOBALS['s3db_info']['server']['db']['uploads_file'].'/tmps3db';
  1077. $old_file = $maindir.'/'.$file_id.'.'.$extension;
  1078. $old_file = ereg_replace('/.$','',$old_file);
  1079. if(!is_file($old_file))
  1080. return array(False, $something_does_not_exist."<message>File not found, please upload file first.</message>");
  1081. else
  1082. return array(True, 'old_file'=>$old_file, 'file_name'=>$filedata['filename'], 'file_size'=>filesize($old_file), 'mime_type'=>$extension);
  1083. }
  1084. //function captureIp()
  1085. //{#finds the IP address on the system call ipconfig
  1086. // exec('ipconfig', $output);
  1087. //
  1088. //$ip = trim($ip);
  1089. //
  1090. //$prot=($_SERVER['HTTPS']!='')?"https://":"http://";
  1091. //$myLocal = $prot.$myIp."/".strtok($_SERVER['PHP_SELF'], '/');
  1092. //if(ereg('localhost|127.0.0.1', $_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_ADDR']!=$myIp && $myIp!='')#is ip is valid and we are not there yet and this is a windows server..
  1093. //{
  1094. //header('Location: '.$myLocal, 1);exit;
  1095. //}
  1096. //
  1097. //
  1098. // if(is_array($output))
  1099. // foreach ($output as $value) {
  1100. // $value = trim($value);
  1101. // if(ereg('(IP Address)(.+[0-9])', $value, $ipdata))
  1102. // {
  1103. // $rightip =ltrim($ipdata[2], ". . . . . . . . . . . : ");
  1104. // $size[] = strlen($rightip);
  1105. // $ip[] = $rightip;
  1106. // }
  1107. // }
  1108. // #there may be + 1 IP. Return the longest...Lucky guess...
  1109. // if(is_array($size))
  1110. // $shortIp = $ip[array_search(max($size), $size)];
  1111. // else {
  1112. // $shortIp = $ip[0];
  1113. // }
  1114. //
  1115. // return ($shortIp);
  1116. //}
  1117. function removePermission($P)
  1118. {
  1119. extract($P);
  1120. $permission_info=array('uid'=>$uid, 'shared_with'=>$shared_with, 'permission_level'=>'000', 'info'=>$info);
  1121. #echo '<pre>';print_r($permission_info);
  1122. $has_permission = has_permission($permission_info, $db);
  1123. #echo $has_permission;
  1124. if($has_permission!='' && $has_permission!='000')
  1125. {
  1126. $done=delete_permission(compact('permission_info', 'db', 'user_id', 'info'));
  1127. }
  1128. elseif($has_permission=='')
  1129. {
  1130. $done=insert_permission(compact('permission_info', 'db', 'user_id', 'info'));
  1131. }
  1132. if($done|| $has_permission=='000')
  1133. $output .= formatReturn($GLOBALS['error_codes']['success'], $uid.' removed from '.$shared_with.'.', $format,'');
  1134. else {
  1135. $output .= formatReturn($GLOBALS['error_codes']['something_went_wrong'], $uid.' was NOT removed from resource '.$shared_with, $format,'');
  1136. }
  1137. return ($output);
  1138. }
  1139. function n3UID($uid)
  1140. {
  1141. $uid_info=uid($uid);
  1142. if($uid_info['Did']==$GLOBALS['Did']){$pre=':';$suf='';}
  1143. else{ $pre='<';$suf='>';}
  1144. $n3UID = $pre.$uid_info['uid'].$suf;
  1145. return ($n3UID);
  1146. }
  1147. function pushDownload2Header($Z)
  1148. {extract($Z);
  1149. $maindir = $GLOBALS['s3db_info']['server']['db']['uploads_folder'].$GLOBALS['s3db_info']['server']['db']['uploads_file'];
  1150. #retrieve the project information
  1151. #$project_id = $statement_info['project_id'];
  1152. #$project_info = URI('P'.$project_id, $user_id, $db);
  1153. $folder_code_name = $statement_info['project_folder'];
  1154. $file_name = urlencode($statement_info['file_name']);
  1155. #list ($realname, $extension) = explode('.', $file_name);
  1156. $tmp = fileNameAndExtension($file_name);
  1157. #echo '<pre>';print_r($tmp);exit;
  1158. extract($tmp);
  1159. //echo $realname;exit;
  1160. $file_new_name = $name.'_'.urlencode($statement_info['project_id'].'_'.$statement_info['resource_id'].'_'.$statement_info['rule_id'].'_'.$statement_info['statement_id']).'.'.$extension;
  1161. $file_location = $maindir."/".$folder_code_name."/".$file_new_name;
  1162. #echo $file_location.is_file($file_location);exit;
  1163. #echo $file_location;exit;
  1164. if(is_file($file_location))
  1165. $file_handle = fopen($file_location, "r");
  1166. else
  1167. {
  1168. echo formatReturn($GLOBALS['error_codes']['something_went_wrong'], 'This file does not exist', $format, '');
  1169. exit;
  1170. }
  1171. $file_name = urlencode($file_name);
  1172. #echo 'download in progress';
  1173. $size=(filesize($file_location))?filesize($file_location):'1';
  1174. $file_contents = fread($file_handle, $size);
  1175. if($_REQUEST['download']!='no' && $_REQUEST['download']!='0' && $_REQUEST['download']!='false'){
  1176. header("Pragma: public");
  1177. header("Expires: 0"); // set expiration time
  1178. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  1179. // browser must download file from server instead of cache
  1180. // force download dialog
  1181. header("Content-Type: application/force-download");
  1182. header("Content-Type: application/octet-stream");
  1183. header("Content-Type: application/download");
  1184. #header("Content-Type: ".$ext."");
  1185. // use the Content-Disposition header to supply a recommended filename and
  1186. // force the browser to display the save dialog.
  1187. header("Content-Disposition: attachment; filename=".$file_name."");
  1188. header("Content-Transfer-Encoding: binary");
  1189. echo $file_contents;
  1190. }
  1191. else {
  1192. //encode the string when the format output is json
  1193. if($_REQUEST['callback'] || $_REQUEST['format']=='json'){
  1194. $callback = ($_REQUEST['jsonp']=='')?(($_REQUEST['callback']=='')?'s3db_json':$_REQUEST['callback']):$_REQUEST['jsonp'];
  1195. $onLoad = ($_REQUEST['onload']=='')?'':'; '.stripslashes($_REQUEST['onload']).((ereg('\(.*\)',$_REQUEST['onload'])?'':'()'));
  1196. $jsonpp = ($_REQUEST['jsonpp']=='')?'':', "'.$_REQUEST['jsonpp'].'"';
  1197. $datatype = ($_REQUEST['datatype']!="")?$_REQUEST['datatype']:'string';
  1198. if($datatype=='string'){
  1199. $st = '"';
  1200. $en = '"';
  1201. }
  1202. elseif($datatype=='numeric'){
  1203. $st = '[';
  1204. $file_contents = str_replace(" ",",",$file_contents);
  1205. $en = ']';
  1206. }
  1207. if($datatype!='numeric' && $datatype!='var')
  1208. {$file_contents = urlencode($file_contents);}
  1209. $string2out = $callback.'('.$st.$file_contents.$en.$jsonpp.')'.$onLoad;
  1210. #$string2out = $callback.'('.'"'.urlencode($file_contents).'"'.$jsonpp.')'.$onLoad;
  1211. }
  1212. else {
  1213. $string2out = $file_contents;
  1214. }
  1215. echo $string2out;
  1216. }
  1217. fclose($file_handle);
  1218. }
  1219. function fileNameAndExtension($filename)
  1220. {
  1221. ereg('(.*)\.([A-Za-z0-9]*)$',$filename, $tokens);
  1222. $name = $tokens[1];
  1223. $extension= $tokens[2];
  1224. $a=compact('name','extension');
  1225. return $a;
  1226. }
  1227. function array2xml($array, $letter, $root=false)
  1228. {
  1229. static $indent = ''; $xml = '';
  1230. $res = $GLOBALS['s3codes'][$letter];
  1231. if($res=='')
  1232. if($root==false)
  1233. $res='error';
  1234. else
  1235. $res = $root;
  1236. if(is_array($array))
  1237. foreach($array as $key => $value)
  1238. {
  1239. if(!is_array($value))
  1240. $value = htmlentities($value);
  1241. if(!is_numeric($key))
  1242. $xml .= "$indent<$key>";
  1243. else
  1244. $xml .= "$indent<$res>";
  1245. if(is_array($value))
  1246. {
  1247. $indent .= "\t";
  1248. $value = "\n" . array2xml($value, $letter) . ($indent = substr($indent, 1));
  1249. }
  1250. if(!is_numeric($key))
  1251. $xml .= "$value</$key>\n";
  1252. else
  1253. $xml .= "$value</$res>\n";
  1254. }
  1255. return($xml);
  1256. }
  1257. function formatReturn($error_code,$message, $format, $id, $root=false)
  1258. {
  1259. if($_REQUEST['out']=='header' || ($format=='json' && $_SERVER['HTTPS']))
  1260. {
  1261. header("Pragma: public");
  1262. header("Expires: 0"); // set expiration time
  1263. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  1264. header("Content-Type: application/force-download");
  1265. header("Content-Type: application/octet-stream");
  1266. header("Content-Type: application/download");
  1267. #header("Content-Type: ".$ext."");
  1268. // use the Content-Disposition header to supply a recommended filename and
  1269. // force the browser to display the save dialog.
  1270. header("Content-Disposition: attachment; filename=".$format."");
  1271. header("Content-Transfer-Encoding: binary");
  1272. }
  1273. if($id=='')
  1274. {
  1275. $data[0] = array('error_code'=>$error_code, 'message'=>$message);
  1276. }
  1277. else {
  1278. if(count($id)==1){
  1279. $id_name = array_keys($id);
  1280. $id_name = $id_name[0];
  1281. $data[0] = array('error_code'=>$error_code, 'message'=>$message, $id_name=>$id[$id_name]);
  1282. }
  1283. else {
  1284. $data[0] = array('error_code'=>$error_code, 'message'=>$message);
  1285. foreach ($id as $k=>$v) {
  1286. $data[0][$k]=$v;
  1287. }
  1288. }
  1289. #echo '<pre>';print_r($data);
  1290. }
  1291. $letter = 'E';
  1292. $pack= compact('data','format', 'letter', 'root');
  1293. return(completeDisplay($pack));
  1294. }
  1295. function xml_encode($data, $letter, $root=false,$namespaces=false)
  1296. {
  1297. #echo '<pre>';print_r($data);
  1298. if($root==false)
  1299. $root='ans';
  1300. header("Content-type: application/xml");
  1301. $xml.= "<?xml version='1.0'?>\n";
  1302. $xml.= "<s3db_xml";
  1303. #include the namespaces
  1304. if(is_array($namespaces) && !empty($namespaces)){
  1305. $usedNS = array();
  1306. foreach ($namespaces as $nInfo) {
  1307. if($nInfo['qname'] && $nInfo['url']){
  1308. if(!in_array($nInfo['qname'], $usedNS)){
  1309. $nS .= ' xmlns:'.$nInfo['qname'].'="'.$nInfo['url'].'"';
  1310. array_push($usedNS,$nInfo['qname']);
  1311. }
  1312. }
  1313. }
  1314. }
  1315. $xml .= $nS.">\n".array2xml($data, $letter, $root)."</s3db_xml>";
  1316. return ($xml);
  1317. }
  1318. function SIF_encode($data, $letter)
  1319. {
  1320. foreach ($data as $key=>$res_data) {
  1321. $ID = $res_data[$GLOBALS['COREletterInv'][$letter]];
  1322. foreach ($res_data as $key=>$value) {
  1323. $sif .= $letter.$ID."\t".$key."\t".str_replace(' ','+', $value)."\n";
  1324. }
  1325. }
  1326. return ($sif);
  1327. }
  1328. function tab_encode($data, $cols)
  1329. {
  1330. if(empty($cols))
  1331. $cols = array_keys($data[0]);
  1332. foreach ($data as $key=>$data_info) {
  1333. $tabline='';
  1334. foreach ($cols as $returnCol) {
  1335. if($tabline!='') $tabline .= "\t";
  1336. $tabline .= $data_info[$returnCol];
  1337. }
  1338. $tab .= $tabline."\n";
  1339. }
  1340. return ($tab);
  1341. }
  1342. function get_parser_characters($format)
  1343. {
  1344. if ($format=='tab')
  1345. {
  1346. $tab = chr(9);
  1347. $newline = chr(13);
  1348. $middle = chr(9);
  1349. $end_td = chr(9);
  1350. $end_tr = chr(13);
  1351. $end_th =chr(13);
  1352. }
  1353. elseif($format=='html' || $format=='')
  1354. {
  1355. if($_REQUEST['table.class'])
  1356. $begin_table = '<TABLE class = "'.$_REQUEST['table.class'].'">';
  1357. else
  1358. $begin_table = '<TABLE>';
  1359. $end_table = '</TABLE>';
  1360. $th = '<TH>';
  1361. $end_th ='</TH>';
  1362. $tr = '<TR>';
  1363. $end_tr = '</TR>';
  1364. $td = '<TD>';
  1365. $end_td = '</TD>';
  1366. $middle = $end_td.$td;
  1367. }
  1368. elseif(ereg('html.', $format))
  1369. {
  1370. list($lixo, $style) = explode('.', $format);
  1371. echo $begin_table = '<TABLE class = "'.$style.'">';
  1372. $end_table = '</TABLE>';
  1373. $th = '<TH>';
  1374. $end_th ='</TH>';
  1375. $tr = '<TR>';
  1376. $end_tr = '</TR>';
  1377. $td = '<TD>';
  1378. $end_td = '</TD>';
  1379. $middle = $end_td.$td;
  1380. }
  1381. $parser_char = compact ('middle','begin_table','tr','end_tr','td','end_td','th','end_th','middle','end_table','tab','newline');
  1382. #$parser_char = array ('middle'=> $middle, 'begin_table'=>$begin_table, 'tr'=>$tr, 'end_tr'=>$end_tr,'td'=>$td, 'end_td'=>$end_td, 'middle'=>$middle, 'end_table'=>$end_table, 'tab'=>$tab, 'newline'=>$newline, 'th'=>$th,'end_th'=>$end_th);
  1383. return ($parser_char);
  1384. }
  1385. function rdf_encode_api($data, $letter, $format, $db)
  1386. {
  1387. $m = ModelFactory::getMemModel();
  1388. define('s3db', 'http://www.s3db.org/core#');
  1389. define('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
  1390. define('rdfs', 'http://www.w3.org/2000/01/rdf-schema#');
  1391. foreach ($data as $ind=>$res_data) {
  1392. $ID = $res_data[$GLOBALS['COREletterInv'][$letter]];
  1393. #echo $letter;exit;
  1394. #echo $GLOBALS['COREletterInv'][$letter];exit;
  1395. $D = S3DB_URI_BASE;
  1396. $URI = $D .'/'.$letter.$ID;
  1397. $mResource = new Resource($URI);
  1398. foreach ($res_data as $property=>$value) {
  1399. if(!empty($value)){
  1400. if(@in_array($property, @array_keys($GLOBALS['propertyURI'][$letter])))
  1401. {
  1402. $predicate = new Resource($GLOBALS['propertyURI'][$letter][$property]);
  1403. #else
  1404. #{
  1405. # $predicate = new Resource (s3db.$property);
  1406. #}
  1407. if(in_array($property, array_keys($GLOBALS['pointer'])))
  1408. {
  1409. $s = new Statement ($mResource, $predicate, new Resource ($D.'/'.letter($GLOBALS['pointer'][$property]).$value));
  1410. }
  1411. elseif(in_array($property, $GLOBALS['COREids']))
  1412. {
  1413. $s = new Statement ($mResource, $predicate, new Resource ($D.'/'.letter($property).$value));
  1414. }
  1415. elseif($property=='value' && RuleHasObjectId($ID, $db))
  1416. {
  1417. $s = new Statement ($mResource, $predicate, new Resource ($D.'/I'.$value));
  1418. }
  1419. else
  1420. {
  1421. $s = new Statement ($mResource, $predicate, new Literal ($value));
  1422. }
  1423. $m->add($s);
  1424. }
  1425. }
  1426. }
  1427. if($letter=='S'){
  1428. ##Outputalso a statment for the unserialized statement where subject is item_id, pred is rule, object is value
  1429. if(RuleHasObjectId($ID, $db))
  1430. $obj = new Resource ($D.'/I'.$data[$ind]['value']);
  1431. else
  1432. $obj = new Literal ($data[$ind]['value']);
  1433. $newS = new Statement (new Resource ($D.'/I'.$data[$ind]['item_id']), new Resource ($D.'/R'.$data[$ind]['rule_id']), $obj);
  1434. $m->add($newS);
  1435. }
  1436. if($letter=='R'){
  1437. ##Outputalso a statment for the unserialized statement where subject is item_id, pred is rule, object is value
  1438. if($data[$ind]['object_id']!='')
  1439. $obj = new Resource ($D.'/C'.$data[$ind]['object_id']);
  1440. else
  1441. $obj = new Literal ($data[$ind]['object']);
  1442. $newS = new Statement (new Resource ($D.'/C'.$data[$ind]['subject_id']), new Resource ($D.'/I'.$data[$ind]['verb_id']), $obj);
  1443. $m->add($newS);
  1444. }
  1445. #And for every element that is part of the core, output a statement that mentions where in the ontology they belong
  1446. $ontoS = new Statement ($mResource, new Resource (rdf.'type'), new Resource(s3db.$GLOBALS['N3Names'][$GLOBALS['s3codes'][$letter]]));
  1447. $m->add($ontoS);
  1448. }
  1449. $m->parsedNamespaces = array(rdf=>'rdf', rdfs=>'rdfs', s3db=>'s3db', 'http://purl.org/dc/terms/'=>'dc', 'http://xmlns.com/foaf/0.1/'=>'foaf',$D.'/'=>'D');
  1450. #echo '<pre>';print_r($m);
  1451. $filename = $GLOBALS['uploads'].'tmps3db/'.random_string('10').".rdf";
  1452. $m->saveAs($filename, $format);
  1453. return file_get_contents($filename);
  1454. }
  1455. function rdf_encode($data,$letter, $format, $db,$namespaces=false,$collected_data=array(), $dont_skip_serialized=true, $dont_skip_core_names=true)
  1456. { global $timer;
  1457. define('s3db', 'http://www.s3db.org/core');
  1458. define('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns');
  1459. define('rdfs', 'http://www.w3.org/2000/01/rdf-schema');

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