sql_query_result.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. $language = new text;
  33. $text = $language->get();
  34. //pdo database connection
  35. if (strlen($_REQUEST['id']) > 0) {
  36. require_once "sql_query_pdo.php";
  37. }
  38. if (count($_POST)>0) {
  39. $sql_type = trim($_POST["sql_type"]);
  40. $sql_cmd = trim($_POST["sql_cmd"]);
  41. $table_name = trim($_POST["table_name"]);
  42. if (strlen($sql_cmd) == 0) { $sql_cmd = "select * from ".$table_name; }
  43. }
  44. if (count($_POST)>0) {
  45. $tmp_header = "<html>\n";
  46. $tmp_header .= "<head>\n";
  47. $tmp_header .= "<style type='text/css'>\n";
  48. $tmp_header .= "\n";
  49. $tmp_header .= "body {\n";
  50. $tmp_header .= " font-family: arial;\n";
  51. $tmp_header .= " font-size: 12px;\n";
  52. $tmp_header .= " color: #444444;\n";
  53. $tmp_header .= "}\n";
  54. $tmp_header .= "\n";
  55. $tmp_header .= "th {\n";
  56. $tmp_header .= " border-top: 1px solid #444444;\n";
  57. $tmp_header .= " border-bottom: 1px solid #444444;\n";
  58. $tmp_header .= " color: #FFFFFF;\n";
  59. $tmp_header .= " font-size: 12px;\n";
  60. $tmp_header .= " font-family: arial;\n";
  61. $tmp_header .= " font-weight: bold;\n";
  62. $tmp_header .= " background-color: #777777;\n";
  63. $tmp_header .= " background-image: url(".PROJECT_PATH."'/themes/horizontal/background_th.png');\n";
  64. $tmp_header .= " padding-top: 4px;\n";
  65. $tmp_header .= " padding-bottom: 4px;\n";
  66. $tmp_header .= " padding-right: 7px;\n";
  67. $tmp_header .= " padding-left: 7px;\n";
  68. $tmp_header .= "}\n";
  69. $tmp_header .= "\n";
  70. $tmp_header .= ".row_style0 {\n";
  71. $tmp_header .= " background-color: #EEEEEE;\n";
  72. $tmp_header .= " background-image: url(".PROJECT_PATH."'/themes/horizontal/background_cell.gif');\n";
  73. $tmp_header .= " border-bottom: 1px solid #999999;\n";
  74. $tmp_header .= " font-size: 12px;\n";
  75. $tmp_header .= " color: #444444;\n";
  76. $tmp_header .= " text-align: left;\n";
  77. $tmp_header .= " padding-top: 4px;\n";
  78. $tmp_header .= " padding-bottom: 4px;\n";
  79. $tmp_header .= " padding-right: 7px;\n";
  80. $tmp_header .= " padding-left: 7px;\n";
  81. $tmp_header .= "}\n";
  82. $tmp_header .= "\n";
  83. $tmp_header .= ".row_style0 a:link{ color:#444444; }\n";
  84. $tmp_header .= ".row_style0 a:visited{ color:#444444; }\n";
  85. $tmp_header .= ".row_style0 a:hover{ color:#444444; }\n";
  86. $tmp_header .= ".row_style0 a:active{ color:#444444; }\n";
  87. $tmp_header .= "\n";
  88. $tmp_header .= ".row_style1 {\n";
  89. $tmp_header .= " border-bottom: 1px solid #999999;\n";
  90. $tmp_header .= " background-color: #FFFFFF;\n";
  91. $tmp_header .= " font-size: 12px;\n";
  92. $tmp_header .= " color: #444444;\n";
  93. $tmp_header .= " text-align: left;\n";
  94. $tmp_header .= " padding-top: 4px;\n";
  95. $tmp_header .= " padding-bottom: 4px;\n";
  96. $tmp_header .= " padding-right: 7px;\n";
  97. $tmp_header .= " padding-left: 7px;\n";
  98. $tmp_header .= "}\n";
  99. $tmp_header .= "\n";
  100. $tmp_header .= "</style>";
  101. $tmp_header .= "</head>\n";
  102. $tmp_header .= "<body>\n";
  103. $tmp_footer = "<body>\n";
  104. $tmp_footer .= "<html>\n";
  105. if ($sql_type == "default") {
  106. echo $tmp_header;
  107. $c = 0;
  108. $row_style["0"] = "row_style0";
  109. $row_style["1"] = "row_style1";
  110. $sql_array = explode(";", $sql_cmd);
  111. reset($sql_array);
  112. foreach($sql_array as $sql) {
  113. $sql = trim($sql);
  114. echo "<b>".$text['label-sql_query'].":</b><br>\n";
  115. echo "".$sql."<br /><br />";
  116. if (strlen($sql) > 0) {
  117. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  118. try {
  119. $prep_statement = $db->prepare(check_sql($sql));
  120. $prep_statement->execute();
  121. $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  122. echo "<b>".$text['label-results'].": ".count($result)."</b><br />";
  123. }
  124. catch(PDOException $e) {
  125. echo "<b>".$text['label-error'].":</b><br />\n";
  126. echo "<table>\n";
  127. echo "<tr>\n";
  128. echo "<td>\n";
  129. echo $e->getMessage();
  130. echo "</td>\n";
  131. echo "</tr>\n";
  132. echo "</table>\n";
  133. }
  134. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  135. $x = 0;
  136. foreach ($result[0] as $key => $value) {
  137. echo "<th>".$key."</th>";
  138. $column_array[$x] = $key;
  139. $x++;
  140. }
  141. $x = 1;
  142. foreach ($result as &$row) {
  143. if ($x > 1000) { break; }
  144. echo "<tr>\n";
  145. foreach ($column_array as $column) {
  146. echo "<td class='".$row_style[$c]."'>&nbsp;".$row[$column]."&nbsp;</td>";
  147. }
  148. echo "</tr>\n";
  149. if ($c==0) { $c=1; } else { $c=0; }
  150. $x++;
  151. }
  152. echo "</table>\n";
  153. echo "<br>\n";
  154. }
  155. } //foreach($sql_array as $sql)
  156. echo $tmp_footer;
  157. }
  158. if ($sql_type == "sql insert into") {
  159. echo $tmp_header;
  160. $sql = trim($sql);
  161. echo "<b>".$text['label-sql_query'].":</b><br>\n";
  162. echo "".$sql."<br /><br />";
  163. //get the table data
  164. if (strlen($sql_cmd) == 0) {
  165. $sql = "select * from $table_name";
  166. }
  167. else {
  168. $sql = $sql_cmd;
  169. }
  170. if (strlen($sql) > 0) {
  171. $prep_statement = $db->prepare(check_sql($sql));
  172. if ($prep_statement) {
  173. $prep_statement->execute();
  174. $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  175. }
  176. else {
  177. echo "<b>".$text['label-error'].":</b>\n";
  178. echo "<pre>\n";
  179. print_r($db->errorInfo());
  180. echo "</pre>\n";
  181. }
  182. $x = 0;
  183. foreach ($result[0] as $key => $value) {
  184. $column_array[$x] = $key;
  185. $x++;
  186. }
  187. $column_array_count = count($column_array);
  188. foreach ($result as &$row) {
  189. echo "INSERT INTO $table_name (";
  190. $x = 1;
  191. foreach ($column_array as $column) {
  192. if ($x < $column_array_count) {
  193. if ($column != "menuid" && $column != "menuparentid") {
  194. echo "".$column.",";
  195. }
  196. }
  197. else {
  198. if ($column != "menuid" && $column != "menuparentid") {
  199. echo "".$column."";
  200. }
  201. }
  202. $x++;
  203. }
  204. echo ") ";
  205. echo "VALUES ( ";
  206. $x = 1;
  207. foreach ($column_array as $column) {
  208. if ($x < $column_array_count) {
  209. if ($column != "menuid" && $column != "menuparentid") {
  210. if (is_null($row[$column])) {
  211. echo "null,";
  212. }
  213. else {
  214. echo "'".check_str($row[$column])."',";
  215. }
  216. }
  217. }
  218. else {
  219. if ($column != "menuid" && $column != "menuparentid") {
  220. if (is_null($row[$column])) {
  221. echo "null";
  222. }
  223. else {
  224. echo "'".check_str($row[$column])."'";
  225. }
  226. }
  227. }
  228. $x++;
  229. }
  230. echo ");<br />\n";
  231. }
  232. }
  233. echo $tmp_footer;
  234. }
  235. if ($sql_type == "csv") {
  236. //echo $tmp_header;
  237. //set the headers
  238. header('Content-type: application/octet-binary');
  239. header('Content-Disposition: attachment; filename='.$table_name.'.csv');
  240. //get the table data
  241. $sql = trim($sql);
  242. $sql = "select * from $table_name";
  243. if (strlen($sql) > 0) {
  244. $prep_statement = $db->prepare(check_sql($sql));
  245. if ($prep_statement) {
  246. $prep_statement->execute();
  247. $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  248. }
  249. else {
  250. echo "<b>".$text['label-error'].":</b>\n";
  251. echo "<pre>\n";
  252. print_r($db->errorInfo());
  253. echo "</pre>\n";
  254. }
  255. $x = 0;
  256. foreach ($result[0] as $key => $value) {
  257. $column_array[$x] = $key;
  258. $x++;
  259. }
  260. $column_array_count = count($column_array);
  261. $x = 1;
  262. foreach ($column_array as $column) {
  263. if ($x < $column_array_count) {
  264. echo "\"".$column."\",";
  265. }
  266. else {
  267. echo "\"".$column."\"";
  268. }
  269. $x++;
  270. }
  271. echo "\r\n";
  272. foreach ($result as &$row) {
  273. $x = 1;
  274. foreach ($column_array as $column) {
  275. if ($x < $column_array_count) {
  276. echo "\"".check_str($row[$column])."\",";
  277. }
  278. else {
  279. echo "\"".check_str($row[$column])."\"";
  280. }
  281. $x++;
  282. }
  283. echo "\n";
  284. }
  285. }
  286. }
  287. }
  288. ?>