PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/s3db3.5.10/S3QL.php

https://github.com/drobbins/s3db
PHP | 286 lines | 177 code | 76 blank | 33 comment | 43 complexity | fac9becc5cab8ff95ad6941bf129d538 MD5 | raw file
  1. <?php
  2. #S3QL reads query strings in XML and returns an output in html or tab
  3. #Syntax of the XML query: #S3QL.php?query=<select>collections</select><where><project_id>xxx</project_id><where>
  4. #S3QL.php?query=<select>rules</select><where><project_id>xxx</project_id><where>
  5. #S3QL.php?query=<select>items</select><where><project_id>xxx</project_id><lass_id>yyy</class_id><where>
  6. #http://s3db.org/documentation
  7. #Helena F Deus, November 8, 2006
  8. ini_set('display_errors',0);
  9. if($_REQUEST['su3d'])
  10. ini_set('display_errors',1);
  11. #Endpoint validation.
  12. if(file_exists('config.inc.php'))
  13. {
  14. include('config.inc.php');
  15. $s3ql['connection'] = 'successfull';
  16. }
  17. else
  18. {
  19. $s3ql['connection'] = 'unsuccessfull';
  20. exit;
  21. }
  22. #Get the key, send it to check validity
  23. #When the key goes in the header URL, no need to read the xml, go directly to the file
  24. include_once(S3DB_SERVER_ROOT.'/dbstruct.php');
  25. include_once(S3DB_SERVER_ROOT.'/s3dbcore/display.php');
  26. include_once(S3DB_SERVER_ROOT.'/s3dbcore/callback.php');
  27. #Profilling...
  28. require_once S3DB_SERVER_ROOT.'/pearlib/PEAR.php';
  29. if(is_file(S3DB_SERVER_ROOT.'/pearlib/Benchmark/Timer.php')){
  30. require_once S3DB_SERVER_ROOT.'/pearlib/Benchmark/Timer.php';
  31. $timer = new Benchmark_Timer();
  32. $timer->start();
  33. }
  34. ereg('query=(.*)(&amp;)*', $_SERVER['argv'][0], $tmp);
  35. $query = ($_REQUEST['query']!='')?$_REQUEST['query']:html_entity_decode($tmp[1]);
  36. #echo $query;exit;
  37. $s3ql = readInputMessage($query, $timer); #read the message from the URL input;
  38. #echo 'ola<pre>';print_r($s3ql);
  39. $key=$s3ql['key'];
  40. if(eregi('on|^t|true',$s3ql['graph'])) $complete = true;
  41. include_once('s3ql.header.php');#core.header.php manages all the user authentication
  42. $format = ($s3ql['format']!='')?$s3ql['format']:$_REQUEST['format'];
  43. if($format=='') $format='html';
  44. $s3ql['format'] = $format;
  45. #these represent all the queries that can be performed on the tables
  46. $table = $GLOBALS['s3tables'][$s3ql['from']];
  47. if ($s3ql['insert']=='user' && $s3ql['where']['password']!='') {
  48. echo formatReturn('6','Password cannot be inserted via the API. Please leave this field empty and a random password will be sent to the user\'s email',$format,'');
  49. }
  50. if (($s3ql['edit']=='user'|| $s3ql['update']=='user') && ($s3ql['set']['password']!='' || $s3ql['set']['email']!='')) {
  51. echo formatReturn('6','Password and email cannot be changed via the API.',$format,'');
  52. }
  53. #input the struct into the S3QLaction function
  54. $s3ql['db'] = $db;
  55. $s3ql['user_id'] = $user_id;
  56. $s3qlOut = S3QLaction($s3ql,$timer);
  57. if(!is_array($s3qlOut))
  58. { echo ($s3qlOut);
  59. exit;
  60. }
  61. elseif(ereg('rulelog|statementlog|accesslog|permission',$s3ql['from']))
  62. {
  63. $letter ='E';
  64. }
  65. else {
  66. $letter = letter($s3ql['from']);
  67. }
  68. #When the result is an array, rules or any other, run display to give values as tab delimited
  69. $data = $s3qlOut;
  70. $t=$GLOBALS['s3codes'][$letter];
  71. $t=$GLOBALS['plurals'][$t];
  72. $pack= compact('s3qlOut','data','s3ql','letter', 'returnFields','t','format', 'db','timer','complete');
  73. #echo '<pre>';print_r($pack);
  74. if($format=='json'){
  75. header("HTTP/1.1 200 OK ");
  76. header("Content-Type: text/javascript");
  77. }
  78. echo completeDisplay($pack);
  79. if($_REQUEST['su3d'])
  80. {
  81. echo "Total results: ".count($data);
  82. if($timer)
  83. $timer->display();
  84. }
  85. exit;
  86. #finish key valid
  87. function readInputMessage($query)
  88. {
  89. #echo '<pre>';print_r($$_SERVER[]);
  90. $xml = stripslashes($query);
  91. if($action=='')
  92. $action='select';
  93. #Determine if XML is a URL or a string
  94. if (ereg('^http.*', $xml))
  95. {
  96. $xmlFile= @file_get_contents($xml);
  97. if(empty($xmlFile))
  98. {
  99. echo (formatReturn($GLOBALS['error_codes']['something_missing'],'Query file is not valid', $_REQUEST['format'],''));
  100. exit;
  101. }
  102. else {
  103. $xml = $xmlFile;
  104. }
  105. }
  106. elseif(ereg('^(select|insert|edit|update|grant)', $query, $action)) {
  107. #it is text, read it frmo text
  108. $Q = explode(' ', $query);
  109. #if(ereg('^(projects|rules|classes|instances|statements|users|groups|keys)', $Q[1]))
  110. if(in_array($Q[1], array_keys($GLOBALS['s3input'])))
  111. {
  112. $s3ql['from']=$Q[1];
  113. if(array_search("in", $Q)){
  114. $where_ind = array_search("in", $Q);
  115. $s3ql['where'][$Q[$where_ind+1]] = $Q[$where_ind+2];}
  116. }
  117. else
  118. {
  119. $s3ql[$action[1]] = $Q[1];
  120. }
  121. if(array_search("in", $Q)){
  122. $where_ind = array_search("in", $Q);
  123. $s3ql['where'][$Q[$where_ind+1]] = $Q[$where_ind+2];
  124. }
  125. if(array_search("where", $Q)){
  126. $where_ind = array_search("where", $Q);
  127. if($Q[$where_ind+2]=='=')
  128. $s3ql['where'][$Q[$where_ind+1]] = $Q[$where_ind+3];
  129. else {
  130. $s3ql['where'][$Q[$where_ind+1]] = $Q[$where_ind+2].$Q[$where_ind+3];
  131. }
  132. }
  133. #find a "from". If there is one, then the from and the next for a key=>value pair
  134. if(array_search('from', $Q))
  135. $s3ql['from'] = $Q[array_search('from', $Q)];
  136. if(array_search('where', $Q)) #if there is a 'where' in the array, them capture the following field=>value pairs
  137. {$pairsA = range(array_search('where', $Q)+1, count($Q)+1, 4);
  138. $pairsB = range(array_search('where', $Q)+2, count($Q)+2, 4);
  139. $equality = range(array_search('where', $Q)+3, count($Q)+3, 4);
  140. $intersect = range(array_search('where', $Q)+2, count($Q)+4, 4);
  141. }
  142. }
  143. #clean up values that might affect parsing the xml
  144. ereg('<value>(.*)</value>', $xml, $val);
  145. if($val[1]!='')
  146. $xml = str_replace('<value>'.$val[1].'</value>', '<value>'.base64_encode($val[1]).'</value>', $xml);
  147. ereg('<notes>(.*)</notes>', $xml, $notes);
  148. if($notes[0]!='')
  149. {$xml = str_replace('<notes>'.$notes[1].'</notes>', '<notes>'.base64_encode($notes[1]).'</notes>', $xml);
  150. }
  151. if($xml!=''){
  152. try {
  153. $tmp = @simplexml_load_string($xml);
  154. if(!$tmp){
  155. $tmp = @simplexml_load_string(urldecode($xml));
  156. }
  157. if(!$tmp) {
  158. throw new Exception(formatReturn($GLOBALS['error_codes']['something_went_wrong'],'XML query is badly formatted. Please check your start/end tags', $_REQUEST['format'],''));
  159. }
  160. #$timer->setMarker('XML parsed');
  161. }
  162. catch(Exception $e) {
  163. print $e->getMessage();
  164. exit;
  165. }
  166. $xml = $tmp;
  167. #When there is no XML, rely on GET
  168. $s3ql = $xml;
  169. $s3ql = get_object_vars($s3ql);
  170. #echo '<pre>';print_r($s3ql);
  171. $s3ql['key'] = ($s3ql['key']!='')?$s3ql['key']:$_REQUEST['key'];
  172. if(get_object_vars($s3ql['where'])!='')
  173. $s3ql['where'] = get_object_vars($s3ql['where']);
  174. elseif($_REQUEST['where']!='')
  175. $s3ql['where'] = $_REQUEST['where'];
  176. if($s3ql['where']['value']!='')
  177. $s3ql['where']['value'] = base64_decode($s3ql['where']['value']);
  178. if($s3ql['where']['notes']!='')
  179. $s3ql['where']['notes'] = base64_decode($s3ql['where']['notes']);
  180. #echo '<pre>';print_r($s3ql);exit;
  181. #if(get_object_vars($s3ql['where']['or'])!='')
  182. #$s3ql['where']['or'] = get_object_vars($s3ql['where']['or']);
  183. #elseif($_REQUEST['where']['or']!='')
  184. #$s3ql['where']['or'] = $_REQUEST['where']['or'];
  185. if(get_object_vars($s3ql['set'])!='')
  186. {$s3ql['set'] = get_object_vars($s3ql['set']);
  187. if($s3ql['set']['value']!='')
  188. $s3ql['set']['value'] = base64_decode($s3ql['set']['value']);
  189. if($s3ql['set']['notes']!='')
  190. $s3ql['set']['notes'] = base64_decode($s3ql['set']['notes']);
  191. }
  192. elseif($_REQUEST['set']!='')
  193. $s3ql['set']=$_REQUEST['set'];
  194. $s3ql['select']=($s3ql['select']!='')?$s3ql['select']:(($_REQUEST['select']!='')?$_REQUEST['select']:(((is_array($s3ql) && in_array('from', array_keys($s3ql))))?'*':''));
  195. $s3ql['from']=($s3ql['from']!='')?$s3ql['from']:(($_REQUEST['from']!='')?$_REQUEST['from']:'projects');
  196. if($s3ql=='')
  197. {$s3ql = $_GET;
  198. }
  199. if($s3ql['format']=='' && $_REQUEST['format']!='')
  200. $s3ql['format'] = $_REQUEST['format'];
  201. #interpred the "or" and "and"
  202. }
  203. else {
  204. $s3ql['select']='*';
  205. $s3ql['from']='projects';
  206. }
  207. #echo '<pre>';print_r($s3ql);
  208. return $s3ql;
  209. }
  210. ?>