sql_query_result.php 8.3 KB

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