PageRenderTime 267ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/gulliver/methods/genericAjax.php

https://bitbucket.org/ferOnti/processmaker
PHP | 367 lines | 291 code | 45 blank | 31 comment | 57 complexity | 9cea15390f5e0d3e91656fe66af5ad49 MD5 | raw file
  1. <?php
  2. $request = isset($_POST['request'])? $_POST['request']: null;
  3. if( !isset($request) ){
  4. $request = isset($_GET['request'])? $_GET['request']: null;
  5. }
  6. if( isset($request) ){
  7. switch($request){
  8. case 'deleteGridRowOnDynaform':
  9. //This code is to update the SESSION variable for dependent fields in grids
  10. if (!defined("XMLFORM_AJAX_PATH")) {
  11. define("XMLFORM_AJAX_PATH", PATH_XMLFORM);
  12. }
  13. if (is_array($_SESSION[$_POST["formID"]][$_POST["gridname"]])) {
  14. if (!is_array($_SESSION[$_POST["formID"]][$_POST["gridname"]])) {
  15. $_SESSION[$_POST["formID"]][$_POST["gridname"]] = (array)$_SESSION[$_POST["formID"]][$_POST["gridname"]];
  16. }
  17. ksort($_SESSION[$_POST["formID"]][$_POST["gridname"]]);
  18. $oFields = array();
  19. $initialKey = 1;
  20. foreach ($_SESSION[$_POST["formID"]][$_POST["gridname"]] as $key => $value) {
  21. if ($key != $_POST["rowpos"]) {
  22. $oFields[$initialKey] = $value;
  23. $initialKey++;
  24. }
  25. }
  26. unset($_SESSION[$_POST["formID"]][$_POST["gridname"]]);
  27. $_SESSION[$_POST["formID"]][$_POST["gridname"]] = $oFields;
  28. }
  29. /* if( isset($_SESSION['APPLICATION']) ){
  30. G::LoadClass('case');
  31. $oApp= new Cases();
  32. $aFields = $oApp->loadCase($_SESSION['APPLICATION']);
  33. unset($aFields['APP_DATA'][$_POST['gridname']][$_POST['rowpos']]);
  34. $initialKey = 1;
  35. foreach ($aFields['APP_DATA'][$_POST['gridname']] as $key => $value) {
  36. $oFields[$initialKey] = $value;
  37. $initialKey++;
  38. $aFields['APP_DATA'][$_POST['gridname']] = $oFields;
  39. }
  40. $oApp->updateCase($_SESSION['APPLICATION'], $aFields);
  41. }
  42. */
  43. break;
  44. /** widgets **/
  45. case 'suggest':
  46. try {
  47. if(isset($_GET["inputEnconde64"])) {
  48. $_GET['input'] = base64_decode($_GET['input']);
  49. }
  50. $sData = base64_decode(str_rot13($_GET['hash']));
  51. list($SQL, $DB_UID) = explode('@|', $sData);
  52. // Remplace values for dependent fields
  53. $aDependentFieldsKeys = explode("|", base64_decode(str_rot13($_GET['dependentFieldsKeys'])));
  54. $aDependentFieldsValue = explode("|", $_GET['dependentFieldsValue']);
  55. if($aDependentFieldsKeys){
  56. $SQL = str_replace($aDependentFieldsKeys, $aDependentFieldsValue, $SQL);
  57. }
  58. // Parsed SQL Structure
  59. G::LoadClass('phpSqlParser');
  60. $parser = new PHPSQLParser($SQL);
  61. // Verif parsed array
  62. // print_r($parser->parsed);
  63. $SQL = queryModified($parser->parsed, $_GET['input']);
  64. $aRows = Array();
  65. try {
  66. $con = Propel::getConnection($DB_UID);
  67. $con->begin();
  68. $rs = $con->executeQuery($SQL);
  69. $con->commit();
  70. while ( $rs->next() ) {
  71. array_push($aRows, $rs->getRow());
  72. }
  73. } catch (SQLException $sqle) {
  74. $con->rollback();
  75. }
  76. $input = strtolower( $_GET['input'] );
  77. $len = strlen($input);
  78. $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 0;
  79. $aResults = array();
  80. $count = 0;
  81. $aRows = sortByChar($aRows, $input);
  82. if ($len){
  83. for ($i=0;$i<count($aRows);$i++){
  84. $aRow = $aRows[$i];
  85. $nCols = sizeof($aRow);
  86. $aRow = array_values($aRow);
  87. switch( $nCols ){
  88. case 1:
  89. $id = $aRow[0];
  90. $value = $aRow[0];
  91. $info = '';
  92. break;
  93. case 2:
  94. $id = $aRow[0];
  95. $value = $aRow[1];
  96. $info = '';
  97. break;
  98. case $nCols >= 3:
  99. $id = $aRow[0];
  100. $value = $aRow[1];
  101. $info = $aRow[2];
  102. break;
  103. }
  104. // had to use utf_decode, here
  105. // not necessary if the results are coming from mysql
  106. //
  107. $count++;
  108. $aResults[] = array( "id"=>$id ,"value"=>htmlspecialchars($value), "info"=>htmlspecialchars($info) );
  109. if ($limit && $count==$limit)
  110. break;
  111. }
  112. }
  113. header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
  114. header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
  115. header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  116. header ("Pragma: no-cache"); // HTTP/1.0
  117. if (isset($_REQUEST['json'])) {
  118. header("Content-Type: application/json");
  119. echo "{\"status\":0, \"results\": [";
  120. $arr = array();
  121. $aReplace = array("(\r\n)", "(\n\r)", "(\n)", "(\r)");
  122. for ($i=0;$i<count($aResults);$i++) {
  123. $arr[] = "{\"id\": \"".$aResults[$i]['id']."\", \"value\": \"". html_entity_decode(preg_replace($aReplace, "", $aResults[$i]['value']))."\", \"info\": \"".$aResults[$i]['info']."\"}";
  124. }
  125. echo implode(", ", $arr);
  126. echo "]}";
  127. } else {
  128. header("Content-Type: text/xml");
  129. echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?><results>";
  130. for ($i=0;$i<count($aResults);$i++)
  131. {
  132. echo "<rs id=\"".$aResults[$i]['id']."\" info=\"".$aResults[$i]['info']."\">".$aResults[$i]['value']."</rs>";
  133. }
  134. echo "</results>";
  135. }
  136. } catch(Exception $e){
  137. $err = $e->getMessage();
  138. //$err = eregi_replace("[\n|\r|\n\r]", ' ', $err);
  139. $err = preg_replace("[\n|\r|\n\r]", ' ', $err);//Made compatible to PHP 5.3
  140. echo '{"status":1, "message":"'.$err.'"}';
  141. }
  142. break;
  143. case 'storeInTmp':
  144. try {
  145. $con = Propel::getConnection($_GET['cnn']);
  146. if($_GET['pkt'] == 'int'){
  147. $rs = $con->executeQuery("SELECT MAX({$_GET['pk']}) as lastId FROM {$_GET['table']};");
  148. $rs->next();
  149. $row = $rs->getRow();
  150. $gKey = (int)$row['lastId'] + 1;
  151. } else {
  152. $gKey = md5(date('Y-m-d H:i:s').'@'.rand());
  153. }
  154. $rs = $con->executeQuery("INSERT INTO {$_GET['table']} ({$_GET['pk']}, {$_GET['fld']}) VALUES ('$gKey', '{$_GET['value']}');");
  155. echo "{status: 1, message: \"success\"}";
  156. } catch (Exception $e) {
  157. $err = $e->getMessage();
  158. //$err = eregi_replace("[\n|\r|\n\r]", ' ', $err);
  159. $err = preg_replace("[\n|\r|\n\r]", " ", $err); //Made compatible to PHP 5.3
  160. echo "{status: 0, message: \"" . $err . "\"}";
  161. }
  162. break;
  163. }
  164. }
  165. function sortByChar($aRows, $charSel)
  166. {
  167. $aIniChar = array();
  168. $aRest = array();
  169. for($i=0; $i<count($aRows) ;$i++){
  170. $aRow = $aRows[$i];
  171. $nCols = sizeof($aRow);
  172. $aRowOrder = array_values($aRow);
  173. switch( $nCols ){
  174. case 1:
  175. $value = $aRowOrder[0];
  176. break;
  177. case 2:
  178. $value = $aRowOrder[1];
  179. break;
  180. case $nCols >= 3:
  181. $value = $aRowOrder[1];
  182. break;
  183. }
  184. if(substr(strtolower($value), 0, 1) == substr( strtolower($charSel), 0, 1)){
  185. array_push($aIniChar, $aRow);
  186. } else {
  187. array_push($aRest, $aRow);
  188. }
  189. }
  190. return array_merge($aIniChar, $aRest);
  191. }
  192. /*
  193. * Converts a SQL array parsing to a SQL string.
  194. * @param string $sqlParsed
  195. * @param string $inputSel default value empty string
  196. * @return string
  197. */
  198. function queryModified($sqlParsed, $inputSel = "")
  199. {
  200. if(!empty($sqlParsed['SELECT'])) {
  201. $sqlSelect = "SELECT ";
  202. $aSelect = $sqlParsed['SELECT'];
  203. $sFieldSel = (count($aSelect)>1 ) ? $aSelect[1]['base_expr'] : $aSelect[0]['base_expr'];
  204. foreach($aSelect as $key => $value ) {
  205. if($key != 0)
  206. $sqlSelect .= ", ";
  207. $sAlias = str_replace("`","", $aSelect[$key]['alias']);
  208. $sBaseExpr = $aSelect[$key]['base_expr'];
  209. switch($aSelect[$key]['expr_type']){
  210. case 'colref' : if($sAlias === $sBaseExpr)
  211. $sqlSelect .= $sAlias;
  212. else
  213. $sqlSelect .= $sBaseExpr . ' AS ' . $sAlias;
  214. break;
  215. case 'expression' : if($sAlias === $sBaseExpr)
  216. $sqlSelect .= $sBaseExpr;
  217. else
  218. $sqlSelect .= $sBaseExpr . ' AS ' . $sAlias;
  219. break;
  220. case 'subquery' : if(strpos($sAlias, $sBaseExpr,0) != 0)
  221. $sqlSelect .= $sAlias;
  222. else
  223. $sqlSelect .= $sBaseExpr . " AS " . $sAlias;
  224. break;
  225. case 'operator' : $sqlSelect .= $sBaseExpr;
  226. break;
  227. default : $sqlSelect .= $sBaseExpr;
  228. break;
  229. }
  230. }
  231. $sqlFrom = " FROM ";
  232. if(!empty($sqlParsed['FROM'])){
  233. $aFrom = $sqlParsed['FROM'];
  234. if(count($aFrom) > 0){
  235. foreach($aFrom as $key => $value ){
  236. if($key == 0) {
  237. $sqlFrom .= $aFrom[$key]['table'] . (($aFrom[$key]['table'] == $aFrom[$key]['alias'])?"" : " " . $aFrom[$key]['alias']);
  238. } else {
  239. $sqlFrom .= " " . (($aFrom[$key]['join_type']=='JOIN')?"INNER": $aFrom[$key]['join_type']) . " JOIN " . $aFrom[$key]['table']
  240. . (($aFrom[$key]['table'] == $aFrom[$key]['alias'])?"" : " " . $aFrom[$key]['alias']) . " " . $aFrom[$key]['ref_type'] . " " . $aFrom[$key]['ref_clause'] ;
  241. }
  242. }
  243. }
  244. }
  245. if(!empty($sqlParsed['WHERE'])){
  246. $sqlWhere = " WHERE ";
  247. $aWhere = $sqlParsed['WHERE'];
  248. foreach($aWhere as $key => $value ){
  249. $sqlWhere .= $value['base_expr'] . " ";
  250. }
  251. $sqlWhere .= " AND " . $sFieldSel . " LIKE '%". $inputSel . "%'";
  252. }
  253. else {
  254. $sqlWhere = " WHERE " . $sFieldSel . " LIKE '%". $inputSel ."%' ";
  255. }
  256. $sqlGroupBy = "";
  257. if(!empty($sqlParsed['GROUP'])){
  258. $sqlGroupBy = "GROUP BY ";
  259. $aGroup = $sqlParsed['GROUP'];
  260. foreach($aGroup as $key => $value ){
  261. if($key != 0)
  262. $sqlGroupBy .= ", ";
  263. if($value['direction'] == 'ASC' )
  264. $sqlGroupBy .= $value['base_expr'];
  265. else
  266. $sqlGroupBy .= $value['base_expr'] . " " . $value['direction'];
  267. }
  268. }
  269. $sqlHaving = "";
  270. if(!empty($sqlParsed['HAVING'])){
  271. $sqlHaving = "HAVING ";
  272. $aHaving = $sqlParsed['HAVING'];
  273. foreach($aHaving as $key => $value ){
  274. $sqlHaving .= $value['base_expr'] . " ";
  275. }
  276. }
  277. $sqlOrderBy = "";
  278. if(!empty($sqlParsed['ORDER'])){
  279. $sqlOrderBy = "ORDER BY ";
  280. $aOrder = $sqlParsed['ORDER'];
  281. foreach($aOrder as $key => $value ){
  282. if($key != 0)
  283. $sqlOrderBy .= ", ";
  284. if($value['direction'] == 'ASC' )
  285. $sqlOrderBy .= $value['base_expr'];
  286. else
  287. $sqlOrderBy .= $value['base_expr'] . " " . $value['direction'];
  288. }
  289. } else {
  290. $sqlOrderBy = " ORDER BY " . $sFieldSel;
  291. }
  292. $sqlLimit = "";
  293. if(!empty($sqlParsed['LIMIT'])){
  294. $sqlLimit = "LIMIT ". $sqlParsed['LIMIT']['start'] . ", " . $sqlParsed['LIMIT']['end'];
  295. }
  296. return $sqlSelect . $sqlFrom . $sqlWhere . $sqlGroupBy . $sqlHaving . $sqlOrderBy . $sqlLimit;
  297. }
  298. if(!empty($sqlParsed['CALL'])){
  299. $sCall = "CALL ";
  300. $aCall = $sqlParsed['CALL'];
  301. foreach($aCall as $key => $value ){
  302. $sCall .= $value . " ";
  303. }
  304. return $sCall;
  305. }
  306. if(!empty($sqlParsed['EXECUTE'])){
  307. $sCall = "EXECUTE ";
  308. $aCall = $sqlParsed['EXECUTE'];
  309. foreach($aCall as $key => $value ){
  310. $sCall .= $value . " ";
  311. }
  312. return $sCall;
  313. }
  314. if(!empty($sqlParsed[''])){
  315. $sCall = "";
  316. $aCall = $sqlParsed[''];
  317. foreach($aCall as $key => $value ){
  318. $sCall .= $value . " ";
  319. }
  320. return $sCall;
  321. }
  322. }