/s3db3.5.10/s3dbcore/element_info.php

https://github.com/drobbins/s3db · PHP · 439 lines · 321 code · 88 blank · 30 comment · 23 complexity · fd95c315c493c53c7f67e3ba678a998b MD5 · raw file

  1. <?php
  2. /**
  3. * @author Helena F Deus <helenadeus@gmail.com>
  4. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
  5. * @package S3DB http://www.s3db.org
  6. */
  7. function get_rule_info($element_id)
  8. {
  9. $db = $_SESSION['db'];
  10. $sql = "select * from s3db_rule where rule_id='".$element_id."'";
  11. $db->query($sql, __LINE__, __FILE__);
  12. if($db->next_record())
  13. {
  14. $rule = Array('entity_id'=>$db->f('entity_id'),
  15. 'rule_id'=>$db->f('rule_id'),
  16. 'project_id'=>$db->f('project_id'),
  17. 'subject'=>$db->f('subject'),
  18. 'verb'=>$db->f('verb'),
  19. 'object'=>$db->f('object'),
  20. 'notes'=>$db->f('notes'),
  21. 'created_on'=>substr($db->f('created_on'), 0, 19),
  22. 'created_by'=>$db->f('created_by'),
  23. 'modified_on'=>$db->f('modified_on'),
  24. 'modified_by'=>$db->f('modified_by'),
  25. 'permission'=>$db->f('permission'));
  26. }
  27. return $rule;
  28. }
  29. function get_group_info($account_id)
  30. {
  31. $db =$_SESSION['db'];
  32. $sql = "select account_id, account_lid from s3db_account where account_id ='".$account_id."'";
  33. $db->query($sql, __LINE__, __FILE__);
  34. if($db->next_record())
  35. {
  36. $group = Array('account_id'=>$db->f('account_id'),
  37. 'account_lid'=>$db->f('account_lid'));
  38. }
  39. return $group;
  40. }
  41. function get_resource_info($id, $iid)
  42. {
  43. $db= $_SESSION['db'];
  44. $sql ="select * from s3db_resource where resource_id='".$id."' and iid=".$iid."";
  45. $db->query($sql, __LINE__, __FILE__);
  46. if($db->next_record())
  47. {
  48. $resource = Array('resource_id'=>$db->f('resource_id'),
  49. 'project_id'=>$db->f('project_id'),
  50. //'owner'=>$db->f('owner'),
  51. 'uid'=>$db->f('uid'),
  52. 'entity'=>$db->f('entity'),
  53. 'resource_class_id'=>$db->f('resource_class_id'),
  54. 'notes'=>$db->f('notes'),
  55. 'created_on'=>substr($db->f('created_on'), 0, 19),
  56. 'created_by'=>$db->f('created_by'),
  57. 'modified_on'=>substr($db->f('modified_on'), 0, 19),
  58. 'modified_by'=>$db->f('modified_by'));
  59. }
  60. return $resource;
  61. }
  62. function find_rule_id($subject, $verb, $object, $project_id, $db)
  63. {
  64. if($db=='') $db = $_SESSION['db'];
  65. if ($GLOBALS['s3db_info']['server']['db']['db_type'] == 'mysql')
  66. $sql = "select rule_id from s3db_rule where subject = '".$subject."' and verb='".$verb."' and object='".$object."' and permission regexp '(".$project_id."$|".$project_id."_)'";
  67. else
  68. $sql = "select rule_id from s3db_rule where subject = '".$subject."' and verb='".$verb."' and object='".$object."' and permission ~~ '%".$project_id."\\\_%'";
  69. #echo $sql;
  70. $db->query($sql, __LINE__, __FILE__);
  71. if($db->next_record())
  72. return $db->f('rule_id');
  73. }
  74. function find_statement_id($rule_id, $UID, $project_id)
  75. {
  76. $db = $_SESSION['db'];
  77. $sql = "select statement_id from s3db_statement where rule_id='".$rule_id."' and resource_id='".$UID."'";
  78. #echo $sql.'<BR>';
  79. $db->query($sql, __LINE__, __FILE__);
  80. while($db->next_record())
  81. {
  82. $statement_id[] = $db->f('statement_id');
  83. }
  84. #echo $statement_id;
  85. return $statement_id;
  86. }
  87. function find_owner_project_id($element, $element_id, $db)
  88. {
  89. if ($db=='') $db = $_SESSION['db'];
  90. $sql = "select project_id from s3db_".$element." where ".$element."_id='".$element_id."'";
  91. $db->query($sql, __LINE__, __FILE__);
  92. if($db->next_record())
  93. return $db->f('project_id');
  94. }
  95. function get_statement_info_by_rule($rule_id, $UID, $project_id)
  96. {
  97. $db= $_SESSION['db'];
  98. $sql ="select * from s3db_statement where rule_id='".$rule_id."' and resource_id='".$UID."'";
  99. #$sql ="select * from s3db_statement where rule_id=".$rule_id." and resource_id=".$UID."";
  100. #echo $sql;
  101. $db->query($sql, __LINE__, __FILE__);
  102. while($db->next_record())
  103. {
  104. $statement = Array('statement_id'=>$db->f('statement_id'),
  105. 'project_id'=>$db->f('project_id'),
  106. 'resource_id'=>$db->f('resource_id'),
  107. 'rule_id'=>$db->f('rule_id'),
  108. 'value'=>$db->f('value'),
  109. 'notes'=>$db->f('notes'),
  110. 'created_on'=>$db->f('created_on'),
  111. 'created_by'=>$db->f('created_by'),
  112. 'modified_on'=>$db->f('modified_on'),
  113. 'modified_by'=>$db->f('modified_by'),
  114. 'mime_type'=>$db->f('mime_type'),
  115. 'file_name'=>$db->f('file_name'));
  116. }
  117. return $statement;
  118. }
  119. function get_statement_info($S)
  120. {
  121. extract($S);
  122. #echo '<pre>';print_r($S);
  123. #$db= $_SESSION['db'];
  124. $sql ="select r.subject, r.verb, r.object, s.statement_id, s.project_id, s.resource_id, s.rule_id, s.value, s.file_name, s.notes, s.created_on, s.created_by, s.modified_by, s.modified_on from s3db_rule as r, s3db_statement as s where r.rule_id = s.rule_id and s.statement_id='".$statement_id."'";
  125. $db->query($sql, __LINE__, __FILE__);
  126. if($db->next_record())
  127. {
  128. $statement = Array('statement_id'=>$db->f('statement_id'),
  129. 'project_id'=>$db->f('project_id'),
  130. 'resource_id'=>$db->f('resource_id'),
  131. 'rule_id'=>$db->f('rule_id'),
  132. //'owner'=>$db->f('owner'),
  133. 'subject'=>$db->f('subject'),
  134. 'verb'=>$db->f('verb'),
  135. 'object'=>$db->f('object'),
  136. 'value'=>$db->f('value'),
  137. 'file_name'=>$db->f('file_name'),
  138. 'notes'=>$db->f('notes'),
  139. 'created_on'=>$db->f('created_on'),
  140. 'created_by'=>$db->f('created_by'),
  141. 'modified_on'=>$db->f('modified_on'),
  142. 'modified_by'=>$db->f('modified_by'));
  143. }
  144. return $statement;
  145. }
  146. function get_statement_info_editstat($id)
  147. {
  148. $db= $_SESSION['db'];
  149. $sql ="select r.subject, r.verb, r.object, s.statement_id, s.project_id, s.resource_id, s.rule_id, s.value, s.file_name, s.notes, s.created_on, s.created_by, s.modified_by, s.modified_on from s3db_rule as r, s3db_statement as s where r.rule_id = s.rule_id and s.statement_id='".$id."'";
  150. //$sql ="select * from s3db_statement where statement_id='".$id."'";
  151. $db->query($sql, __LINE__, __FILE__);
  152. if($db->next_record())
  153. {
  154. $statement = Array('statement_id'=>$db->f('statement_id'),
  155. 'project_id'=>$db->f('project_id'),
  156. 'rule_id'=>$db->f('rule_id'),
  157. 'resource_id'=>$db->f('resource_id'),
  158. 'subject'=>$db->f('subject'),
  159. 'verb'=>$db->f('verb'),
  160. 'object'=>$db->f('object'),
  161. 'value'=>$db->f('value'),
  162. 'file_name'=>$db->f('file_name'),
  163. 'notes'=>$db->f('notes'),
  164. 'created_on'=>$db->f('created_on'),
  165. 'created_by'=>$db->f('created_by'),
  166. 'modified_on'=>$db->f('modified_on'),
  167. 'modified_by'=>$db->f('modified_by'));
  168. }
  169. return $statement;
  170. }
  171. function get_statement_info_from_ID($statement_id, $project_id)
  172. {
  173. $db= $_SESSION['db'];
  174. $sql ="select * from s3db_statement where rule_id='".$rule_id."' and resource_id='".$UID."'";
  175. #$sql ="select * from s3db_statement where rule_id=".$rule_id." and resource_id=".$UID."";
  176. $db->query($sql, __LINE__, __FILE__);
  177. while($db->next_record())
  178. {
  179. $statement = Array('statement_id'=>$db->f('statement_id'),
  180. 'project_id'=>$db->f('project_id'),
  181. 'resource_id'=>$db->f('resource_id'),
  182. 'rule_id'=>$db->f('rule_id'),
  183. 'value'=>$db->f('value'),
  184. 'notes'=>$db->f('notes'),
  185. 'created_on'=>$db->f('created_on'),
  186. 'created_by'=>$db->f('created_by'),
  187. 'modified_on'=>$db->f('modified_on'),
  188. 'modified_by'=>$db->f('modified_by'),
  189. 'mime_type'=>$db->f('mime_type'),
  190. 'file_name'=>$db->f('file_name'));
  191. }
  192. return $statement;
  193. }
  194. function find_object_info($object, $project_id)
  195. {
  196. $shared_rules = list_shared_rules ($project_id, '', $object);
  197. #echo '<pre>'.$object;print_r($shared_rules);
  198. #When looking at the shared rules, find which one hve UID as object, those are the ones that are reosurces
  199. if ($object == $shared_rules[0]['subject'])
  200. return $shared_rules[0];
  201. }
  202. function get_user_short_info($id)
  203. {
  204. $db= $_SESSION['db'];
  205. $sql ="select * from s3db_account where account_id='".$id."'";
  206. $db->query($sql, __LINE__, __FILE__);
  207. if($db->next_record())
  208. {
  209. $user = Array('account_id'=>$db->f('account_id'),
  210. 'account_lid'=>$db->f('account_lid'),
  211. 'account_uname'=>$db->f('account_uname'),
  212. 'account_group'=>$db->f('account_group'));
  213. }
  214. return $user;
  215. }
  216. function find_login_id($account_id, $db)
  217. {
  218. $sql = "select account_lid from s3db_account where s3db_account.account_id='".$account_id."'";
  219. if($db->next_record())
  220. {
  221. $login_id = $db->f('account_lid');
  222. }
  223. return $login_id;
  224. }
  225. function get_user_info($account_id)
  226. {
  227. $db = $_SESSION['db'];
  228. $sql = "select * from s3db_account where s3db_account.account_id='".$account_id."'";
  229. $db->query($sql, __LINE__, __FILE__);
  230. //$user = '';
  231. if($db->next_record())
  232. {
  233. $account = Array('account_id'=>$db->f('account_id'),
  234. 'account_lid'=>$db->f('account_lid'),
  235. 'account_pwd'=>$db->f('account_pwd'),
  236. 'account_uname'=>$db->f('account_uname'),
  237. 'account_group'=>$db->f('account_group'),
  238. 'account_email'=>$db->f('account_email'),
  239. 'account_phone'=>$db->f('account_phone'),
  240. 'account_type'=>$db->f('account_type'),
  241. 'account_addr_id'=>$db->f('account_addr_id'),
  242. 'account_status'=>$db->f('account_status'));
  243. if($account['account_addr_id'] > 0)
  244. {
  245. $sql = "select * from s3db_addr where addr_id='".$account['account_addr_id']."'";
  246. $db->query($sql, __LINE__, __FILE__);
  247. if($db->next_record())
  248. {
  249. $addr = Array ('addr1'=>$db->f('addr1'),
  250. 'addr2'=>$db->f('addr2'),
  251. 'city'=>$db->f('city'),
  252. 'state'=>$db->f('state'),
  253. 'postal_code'=>$db->f('postal_code'),
  254. 'country'=>$db->f('country'));
  255. }
  256. }
  257. if(!is_array($addr))
  258. {
  259. $addr = Array('addr1'=>'',
  260. 'addr2'=>'',
  261. 'city'=>'',
  262. 'state'=>'',
  263. 'postal_code'=>'',
  264. 'country'=>'');
  265. }
  266. }
  267. $user = array_merge($account, $addr);
  268. //print_r($user);
  269. return $user;
  270. }
  271. function get_project_info($project_id)
  272. {
  273. $db = $_SESSION['db'];
  274. $sql = "select * from s3db_project where project_id='".$project_id."'";
  275. $db->query($sql, __LINE__, __FILE__);
  276. if($db->next_record())
  277. {
  278. $project_info=Array('project_id'=>$db->f('project_id'),
  279. 'project_name'=>$db->f('project_name'),
  280. 'project_owner'=>$db->f('project_owner'),
  281. 'project_description'=>$db->f('project_description'),
  282. 'project_status'=>$db->f('project_status'),
  283. 'created_on'=>$db->f('created_on'),
  284. 'created_by'=>$db->f('created_by'),
  285. 'modified_on'=>$db->f('modified_on'),
  286. 'modified_by'=>$db->f('modified_by'));
  287. }
  288. return $project_info;
  289. }
  290. function get_project_resources($project_id)
  291. {
  292. $db = $_SESSION['db'];
  293. if ($GLOBALS['s3db_info']['server']['db']['db_type']=='mysql')
  294. $sql = "select distinct entity, resource_id from s3db_resource where iid='0' and project_id='".$project_id."' order by entity";
  295. else
  296. $sql = "select distinct on (entity) entity, resource_id from s3db_resource where iid='0' and project_id='".$project_id."' order by entity";
  297. $db->query($sql, __LINE__, __FILE__);
  298. while($db->next_record())
  299. {
  300. $resources[] = Array('resource_id'=>$db->f('resource_id'),
  301. 'entity'=>$db->f('entity'));
  302. }
  303. //echo $sql;
  304. //print_r($resources);
  305. return $resources;
  306. }
  307. function get_project_rules($P)
  308. {
  309. extract ($P);
  310. if ($GLOBALS['s3db_info']['server']['db']['db_type'] == 'mysql')
  311. $regexp = "regexp '(".$project_id."$|".$project_id."_)'";
  312. else
  313. $regexp = "~~ '%".$project_id."\\\_%'";
  314. #$db = $_SESSION['db'];
  315. //$sql = "select * from s3db_rule where object !='UID' and rule_id in (select distinct rule_id from s3db_statement) order by rule_id";
  316. //$sql = "select * from s3db_rule where rule_id in (select distinct rule_id from s3db_statement) order by rule_id";
  317. $sql = "select * from s3db_rule where permission ".$regexp."";
  318. //echo $sql;
  319. $db->query($sql, __LINE__, __FILE__);
  320. while($db->next_record())
  321. {
  322. $rules[] = Array('rule_id'=>$db->f('rule_id'),
  323. //'owner'=>$db->f('owner'),
  324. 'resource_id'=>$db->f('resource_id'),
  325. 'subject'=>$db->f('subject'),
  326. 'verb'=>$db->f('verb'),
  327. 'object'=>$db->f('object'),
  328. 'notes'=>$db->f('notes'),
  329. 'created_on'=>$db->f('created_on'),
  330. 'created_by'=>$db->f('created_by'),
  331. 'modified_on'=>$db->f('modified_on'),
  332. 'modified_by'=>$db->f('modified_by'));
  333. }
  334. //echo count($rules);
  335. return $rules;
  336. }
  337. function find_involved_projects()
  338. {
  339. $db = $_SESSION['db'];
  340. $sql = "select acl_project_id, acl_rights from s3db_project_acl where acl_account='".$_SESSION['user']['account_id']."' and acl_rights !=0";
  341. //echo $sql;
  342. $db->query($sql, __LINE__, __FILE__);
  343. while($db->next_record())
  344. {
  345. $involved_projects[] = Array('project_id'=>$db->f('acl_project_id'),
  346. 'rights'=>$db->f('acl_rights'));
  347. }
  348. return $involved_projects;
  349. }
  350. ?>