sql_query_result.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2012
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. include "root.php";
  22. require_once "resources/require.php";
  23. require_once "resources/check_auth.php";
  24. if (permission_exists('sql_query_execute')) {
  25. //access granted
  26. }
  27. else {
  28. echo "access denied";
  29. exit;
  30. }
  31. //add multi-lingual support
  32. require_once "app_languages.php";
  33. foreach($text as $key => $value) {
  34. $text[$key] = $value[$_SESSION['domain']['language']['code']];
  35. }
  36. //pdo database connection
  37. if (strlen($_REQUEST['id']) > 0) {
  38. require_once "sql_query_pdo.php";
  39. }
  40. if (count($_POST)>0) {
  41. $sql_type = trim($_POST["sql_type"]);
  42. $sql_cmd = trim($_POST["sql_cmd"]);
  43. $table_name = trim($_POST["table_name"]);
  44. if (strlen($sql_cmd) == 0) { $sql_cmd = "select * from ".$table_name; }
  45. }
  46. if (count($_POST)>0) {
  47. $tmp_header = "<html>\n";
  48. $tmp_header .= "<head>\n";
  49. $tmp_header .= "<style type='text/css'>\n";
  50. $tmp_header .= "\n";
  51. $tmp_header .= "body {\n";
  52. $tmp_header .= " font-family: arial;\n";
  53. $tmp_header .= " font-size: 12px;\n";
  54. $tmp_header .= " color: #444444;\n";
  55. $tmp_header .= "}\n";
  56. $tmp_header .= "\n";
  57. $tmp_header .= "th {\n";
  58. $tmp_header .= " border-top: 1px solid #444444;\n";
  59. $tmp_header .= " border-bottom: 1px solid #444444;\n";
  60. $tmp_header .= " color: #FFFFFF;\n";
  61. $tmp_header .= " font-size: 12px;\n";
  62. $tmp_header .= " font-family: arial;\n";
  63. $tmp_header .= " font-weight: bold;\n";
  64. $tmp_header .= " background-color: #777777;\n";
  65. $tmp_header .= " background-image: url(".PROJECT_PATH."'/themes/horizontal/background_th.png');\n";
  66. $tmp_header .= " padding-top: 4px;\n";
  67. $tmp_header .= " padding-bottom: 4px;\n";
  68. $tmp_header .= " padding-right: 7px;\n";
  69. $tmp_header .= " padding-left: 7px;\n";
  70. $tmp_header .= "}\n";
  71. $tmp_header .= "\n";
  72. $tmp_header .= ".row_style0 {\n";
  73. $tmp_header .= " background-color: #EEEEEE;\n";
  74. $tmp_header .= " background-image: url(".PROJECT_PATH."'/themes/horizontal/background_cell.gif');\n";
  75. $tmp_header .= " border-bottom: 1px solid #999999;\n";
  76. $tmp_header .= " font-size: 12px;\n";
  77. $tmp_header .= " color: #444444;\n";
  78. $tmp_header .= " text-align: left;\n";
  79. $tmp_header .= " padding-top: 4px;\n";
  80. $tmp_header .= " padding-bottom: 4px;\n";
  81. $tmp_header .= " padding-right: 7px;\n";
  82. $tmp_header .= " padding-left: 7px;\n";
  83. $tmp_header .= "}\n";
  84. $tmp_header .= "\n";
  85. $tmp_header .= ".row_style0 a:link{ color:#444444; }\n";
  86. $tmp_header .= ".row_style0 a:visited{ color:#444444; }\n";
  87. $tmp_header .= ".row_style0 a:hover{ color:#444444; }\n";
  88. $tmp_header .= ".row_style0 a:active{ color:#444444; }\n";
  89. $tmp_header .= "\n";
  90. $tmp_header .= ".row_style1 {\n";
  91. $tmp_header .= " border-bottom: 1px solid #999999;\n";
  92. $tmp_header .= " background-color: #FFFFFF;\n";
  93. $tmp_header .= " font-size: 12px;\n";
  94. $tmp_header .= " color: #444444;\n";
  95. $tmp_header .= " text-align: left;\n";
  96. $tmp_header .= " padding-top: 4px;\n";
  97. $tmp_header .= " padding-bottom: 4px;\n";
  98. $tmp_header .= " padding-right: 7px;\n";
  99. $tmp_header .= " padding-left: 7px;\n";
  100. $tmp_header .= "}\n";
  101. $tmp_header .= "\n";
  102. $tmp_header .= "</style>";
  103. $tmp_header .= "</head>\n";
  104. $tmp_header .= "<body>\n";
  105. $tmp_footer = "<body>\n";
  106. $tmp_footer .= "<html>\n";
  107. if ($sql_type == "default") {
  108. echo $tmp_header;
  109. $c = 0;
  110. $row_style["0"] = "row_style0";
  111. $row_style["1"] = "row_style1";
  112. $sql_array = explode(";", $sql_cmd);
  113. reset($sql_array);
  114. foreach($sql_array as $sql) {
  115. $sql = trim($sql);
  116. echo "<b>".$text['label-sql_query'].":</b><br>\n";
  117. echo "".$sql."<br /><br />";
  118. if (strlen($sql) > 0) {
  119. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  120. try {
  121. $prep_statement = $db->prepare(check_sql($sql));
  122. $prep_statement->execute();
  123. $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  124. echo "<b>".$text['label-results'].": ".count($result)."</b><br />";
  125. }
  126. catch(PDOException $e) {
  127. echo "<b>".$text['label-error'].":</b><br />\n";
  128. echo "<table>\n";
  129. echo "<tr>\n";
  130. echo "<td>\n";
  131. echo $e->getMessage();
  132. echo "</td>\n";
  133. echo "</tr>\n";
  134. echo "</table>\n";
  135. }
  136. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  137. $x = 0;
  138. foreach ($result[0] as $key => $value) {
  139. echo "<th>".$key."</th>";
  140. $column_array[$x] = $key;
  141. $x++;
  142. }
  143. $x = 1;
  144. foreach ($result as &$row) {
  145. if ($x > 1000) { break; }
  146. echo "<tr>\n";
  147. foreach ($column_array as $column) {
  148. echo "<td class='".$row_style[$c]."'>&nbsp;".$row[$column]."&nbsp;</td>";
  149. }
  150. echo "</tr>\n";
  151. if ($c==0) { $c=1; } else { $c=0; }
  152. $x++;
  153. }
  154. echo "</table>\n";
  155. echo "<br>\n";
  156. }
  157. } //foreach($sql_array as $sql)
  158. echo $tmp_footer;
  159. }
  160. if ($sql_type == "sql insert into") {
  161. echo $tmp_header;
  162. $sql = trim($sql);
  163. echo "<b>".$text['label-sql_query'].":</b><br>\n";
  164. echo "".$sql."<br /><br />";
  165. //get the table data
  166. if (strlen($sql_cmd) == 0) {
  167. $sql = "select * from $table_name";
  168. }
  169. else {
  170. $sql = $sql_cmd;
  171. }
  172. if (strlen($sql) > 0) {
  173. $prep_statement = $db->prepare(check_sql($sql));
  174. if ($prep_statement) {
  175. $prep_statement->execute();
  176. $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  177. }
  178. else {
  179. echo "<b>".$text['label-error'].":</b>\n";
  180. echo "<pre>\n";
  181. print_r($db->errorInfo());
  182. echo "</pre>\n";
  183. }
  184. $x = 0;
  185. foreach ($result[0] as $key => $value) {
  186. $column_array[$x] = $key;
  187. $x++;
  188. }
  189. $column_array_count = count($column_array);
  190. foreach ($result as &$row) {
  191. echo "INSERT INTO $table_name (";
  192. $x = 1;
  193. foreach ($column_array as $column) {
  194. if ($x < $column_array_count) {
  195. if ($column != "menuid" && $column != "menuparentid") {
  196. echo "".$column.",";
  197. }
  198. }
  199. else {
  200. if ($column != "menuid" && $column != "menuparentid") {
  201. echo "".$column."";
  202. }
  203. }
  204. $x++;
  205. }
  206. echo ") ";
  207. echo "VALUES ( ";
  208. $x = 1;
  209. foreach ($column_array as $column) {
  210. if ($x < $column_array_count) {
  211. if ($column != "menuid" && $column != "menuparentid") {
  212. if (is_null($row[$column])) {
  213. echo "null,";
  214. }
  215. else {
  216. echo "'".check_str($row[$column])."',";
  217. }
  218. }
  219. }
  220. else {
  221. if ($column != "menuid" && $column != "menuparentid") {
  222. if (is_null($row[$column])) {
  223. echo "null";
  224. }
  225. else {
  226. echo "'".check_str($row[$column])."'";
  227. }
  228. }
  229. }
  230. $x++;
  231. }
  232. echo ");<br />\n";
  233. }
  234. }
  235. echo $tmp_footer;
  236. }
  237. if ($sql_type == "csv") {
  238. //echo $tmp_header;
  239. //set the headers
  240. header('Content-type: application/octet-binary');
  241. header('Content-Disposition: attachment; filename='.$table_name.'.csv');
  242. //get the table data
  243. $sql = trim($sql);
  244. $sql = "select * from $table_name";
  245. if (strlen($sql) > 0) {
  246. $prep_statement = $db->prepare(check_sql($sql));
  247. if ($prep_statement) {
  248. $prep_statement->execute();
  249. $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  250. }
  251. else {
  252. echo "<b>".$text['label-error'].":</b>\n";
  253. echo "<pre>\n";
  254. print_r($db->errorInfo());
  255. echo "</pre>\n";
  256. }
  257. $x = 0;
  258. foreach ($result[0] as $key => $value) {
  259. $column_array[$x] = $key;
  260. $x++;
  261. }
  262. $column_array_count = count($column_array);
  263. $x = 1;
  264. foreach ($column_array as $column) {
  265. if ($x < $column_array_count) {
  266. echo "\"".$column."\",";
  267. }
  268. else {
  269. echo "\"".$column."\"";
  270. }
  271. $x++;
  272. }
  273. echo "\r\n";
  274. foreach ($result as &$row) {
  275. $x = 1;
  276. foreach ($column_array as $column) {
  277. if ($x < $column_array_count) {
  278. echo "\"".check_str($row[$column])."\",";
  279. }
  280. else {
  281. echo "\"".check_str($row[$column])."\"";
  282. }
  283. $x++;
  284. }
  285. echo "\n";
  286. }
  287. }
  288. }
  289. }
  290. ?>