sql_query_result.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 "includes/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. $sql = "select * from $table_name";
  164. if (strlen($sql) > 0) {
  165. $prep_statement = $db->prepare(check_sql($sql));
  166. if ($prep_statement) {
  167. $prep_statement->execute();
  168. $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  169. }
  170. else {
  171. echo "<b>".$text['label-error'].":</b>\n";
  172. echo "<pre>\n";
  173. print_r($db->errorInfo());
  174. echo "</pre>\n";
  175. }
  176. $x = 0;
  177. foreach ($result[0] as $key => $value) {
  178. $column_array[$x] = $key;
  179. $x++;
  180. }
  181. $column_array_count = count($column_array);
  182. foreach ($result as &$row) {
  183. echo "INSERT INTO $table_name (";
  184. $x = 1;
  185. foreach ($column_array as $column) {
  186. if ($x < $column_array_count) {
  187. if ($column != "menuid" && $column != "menuparentid") {
  188. echo "".$column.",";
  189. }
  190. }
  191. else {
  192. if ($column != "menuid" && $column != "menuparentid") {
  193. echo "".$column."";
  194. }
  195. }
  196. $x++;
  197. }
  198. echo ") ";
  199. echo "VALUES ( ";
  200. $x = 1;
  201. foreach ($column_array as $column) {
  202. if ($x < $column_array_count) {
  203. if ($column != "menuid" && $column != "menuparentid") {
  204. echo "'".check_str($row[$column])."',";
  205. }
  206. }
  207. else {
  208. if ($column != "menuid" && $column != "menuparentid") {
  209. echo "'".check_str($row[$column])."'";
  210. }
  211. }
  212. $x++;
  213. }
  214. echo ");<br />\n";
  215. }
  216. }
  217. echo $tmp_footer;
  218. }
  219. if ($sql_type == "csv") {
  220. //echo $tmp_header;
  221. //set the headers
  222. header('Content-type: application/octet-binary');
  223. header('Content-Disposition: attachment; filename='.$table_name.'.csv');
  224. //get the table data
  225. $sql = trim($sql);
  226. $sql = "select * from $table_name";
  227. if (strlen($sql) > 0) {
  228. $prep_statement = $db->prepare(check_sql($sql));
  229. if ($prep_statement) {
  230. $prep_statement->execute();
  231. $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  232. }
  233. else {
  234. echo "<b>".$text['label-error'].":</b>\n";
  235. echo "<pre>\n";
  236. print_r($db->errorInfo());
  237. echo "</pre>\n";
  238. }
  239. $x = 0;
  240. foreach ($result[0] as $key => $value) {
  241. $column_array[$x] = $key;
  242. $x++;
  243. }
  244. $column_array_count = count($column_array);
  245. $x = 1;
  246. foreach ($column_array as $column) {
  247. if ($x < $column_array_count) {
  248. echo "\"".$column."\",";
  249. }
  250. else {
  251. echo "\"".$column."\"";
  252. }
  253. $x++;
  254. }
  255. echo "\r\n";
  256. foreach ($result as &$row) {
  257. $x = 1;
  258. foreach ($column_array as $column) {
  259. if ($x < $column_array_count) {
  260. echo "\"".check_str($row[$column])."\",";
  261. }
  262. else {
  263. echo "\"".check_str($row[$column])."\"";
  264. }
  265. $x++;
  266. }
  267. echo "\n";
  268. }
  269. }
  270. }
  271. }
  272. ?>