sql_query_result.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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('exec_sql')) {
  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["cmd"]);
  41. $table_name = trim($_POST["table_name"]);
  42. $header = "<html>\n";
  43. $header .= "<head>\n";
  44. $header .= "<style type='text/css'>\n";
  45. $header .= "\n";
  46. $header .= "body {\n";
  47. $header .= " font-family: arial;\n";
  48. $header .= " font-size: 12px;\n";
  49. $header .= " color: #444;\n";
  50. $header .= "}\n";
  51. $header .= "\n";
  52. $header .= "th {\n";
  53. $header .= " border-top: 1px solid #444;\n";
  54. $header .= " border-bottom: 1px solid #444;\n";
  55. $header .= " color: #fff;\n";
  56. $header .= " font-size: 12px;\n";
  57. $header .= " font-family: arial;\n";
  58. $header .= " font-weight: bold;\n";
  59. $header .= " background-color: #777;\n";
  60. $header .= " padding: 4px 7px;\n";
  61. $header .= " text-align: left;\n";
  62. $header .= "}\n";
  63. $header .= "\n";
  64. $header .= ".row_style0 {\n";
  65. $header .= " background-color: #eee;\n";
  66. $header .= " border-bottom: 1px solid #999;\n";
  67. $header .= " border-left: 1px solid #fff;\n";
  68. $header .= " font-size: 12px;\n";
  69. $header .= " color: #444;\n";
  70. $header .= " text-align: left;\n";
  71. $header .= " padding: 4px 7px;\n";
  72. $header .= " text-align: left;\n";
  73. $header .= " vertical-align: top;\n";
  74. $header .= "}\n";
  75. $header .= "\n";
  76. $header .= ".row_style0 a:link{ color:#444; }\n";
  77. $header .= ".row_style0 a:visited{ color:#444; }\n";
  78. $header .= ".row_style0 a:hover{ color:#444; }\n";
  79. $header .= ".row_style0 a:active{ color:#444; }\n";
  80. $header .= "\n";
  81. $header .= ".row_style1 {\n";
  82. $header .= " border-bottom: 1px solid #999;\n";
  83. $header .= " border-left: 1px solid #eee;\n";
  84. $header .= " background-color: #fff;\n";
  85. $header .= " font-size: 12px;\n";
  86. $header .= " color: #444;\n";
  87. $header .= " text-align: left;\n";
  88. $header .= " padding: 4px 7px;\n";
  89. $header .= " text-align: left;\n";
  90. $header .= " vertical-align: top;\n";
  91. $header .= "}\n";
  92. $header .= "\n";
  93. $header .= "</style>";
  94. $header .= "</head>\n";
  95. $header .= "<body style='margin: 0; padding: 8;'>\n";
  96. $footer = "<body>\n";
  97. $footer .= "<html>\n";
  98. if ($sql_type == '') {
  99. echo $header;
  100. $c = 0;
  101. $row_style["0"] = "row_style0";
  102. $row_style["1"] = "row_style1";
  103. //determine queries to run and show
  104. if ($sql_cmd != '') { $sql_array = array_filter(explode(";", $sql_cmd)); }
  105. if ($table_name != '') { $sql_array[] = "select * from ".$table_name; }
  106. $show_query = (sizeof($sql_array) > 1) ? true : false;
  107. reset($sql_array);
  108. foreach($sql_array as $sql_index => $sql) {
  109. $sql = trim($sql);
  110. if (sizeof($sql_array) > 1 || $show_query) {
  111. if ($sql_index > 0) { echo "<br /><br /><br />"; }
  112. echo "<span style='display: block; padding: 8px; color: green; background-color: #eefff0;'>".$sql.";</span><br />";
  113. }
  114. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  115. try {
  116. $prep_statement = $db->prepare(check_sql($sql));
  117. $prep_statement->execute();
  118. $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  119. echo "<b>".$text['label-records'].": ".count($result)."</b>";
  120. echo "<br /><br />\n";
  121. }
  122. catch(PDOException $e) {
  123. echo "<b>".$text['label-error']."</b>";
  124. echo "<br /><br />\n";
  125. echo $e->getMessage();
  126. echo "<br /><br />\n";
  127. exit;
  128. }
  129. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  130. $x = 0;
  131. foreach ($result[0] as $key => $value) {
  132. echo "<th>".$key."</th>";
  133. $column_array[$x++] = $key;
  134. }
  135. $x = 1;
  136. foreach ($result as &$row) {
  137. if ($x++ > 1000) { break; }
  138. echo "<tr>\n";
  139. foreach ($column_array as $column_index => $column) {
  140. echo "<td class='".$row_style[$c]."' ".(($column_index == 0) ? "style='border-left: none;'" : null).">".$row[$column]."&nbsp;</td>";
  141. }
  142. echo "</tr>\n";
  143. $c = ($c == 0) ? 1 : 0;
  144. }
  145. echo "</table>\n";
  146. echo "<br>\n";
  147. unset($result, $column_array);
  148. }
  149. echo $footer;
  150. }
  151. if ($sql_type == "inserts") {
  152. echo $header;
  153. $sql = trim($sql);
  154. //get the table data
  155. $sql = (strlen($sql_cmd) == 0) ? "select * from ".$table_name : $sql_cmd;
  156. if (strlen($sql) > 0) {
  157. $prep_statement = $db->prepare(check_sql($sql));
  158. if ($prep_statement) {
  159. $prep_statement->execute();
  160. $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  161. }
  162. else {
  163. echo "<b>".$text['label-error']."</b>\n";
  164. echo "<br /><br />\n";
  165. echo "<pre>".print_r($db->errorInfo(), true)."</pre>\n";
  166. echo "<br /><br />\n";
  167. exit;
  168. }
  169. $x = 0;
  170. foreach ($result[0] as $key => $value) {
  171. $column_array[$x++] = $key;
  172. }
  173. $column_array_count = count($column_array);
  174. foreach ($result as &$row) {
  175. echo "insert into ".$table_name." (";
  176. $x = 1;
  177. foreach ($column_array as $column) {
  178. if ($column != "menuid" && $column != "menuparentid") {
  179. echo $column.(($x < $column_array_count) ? "," : null);
  180. }
  181. $x++;
  182. }
  183. echo ") ";
  184. echo "values ( ";
  185. $x = 1;
  186. foreach ($column_array as $column) {
  187. if ($column != "menuid" && $column != "menuparentid") {
  188. echo (!is_null($row[$column])) ? "\"".check_str($row[$column])."\"".(($x < $column_array_count) ? ',' : null) : 'null';
  189. }
  190. $x++;
  191. }
  192. echo ");<br />\n";
  193. }
  194. }
  195. echo $footer;
  196. }
  197. if ($sql_type == "csv") {
  198. //set the headers
  199. header('Content-type: application/octet-binary');
  200. header('Content-Disposition: attachment; filename='.$table_name.'.csv');
  201. //get the table data
  202. $sql = trim($sql);
  203. $sql = "select * from ".$table_name;
  204. if (strlen($sql) > 0) {
  205. $prep_statement = $db->prepare(check_sql($sql));
  206. if ($prep_statement) {
  207. $prep_statement->execute();
  208. $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  209. }
  210. else {
  211. echo "<b>".$text['label-error']."</b>\n";
  212. echo "<br /><br />\n";
  213. echo "<pre>".print_r($db->errorInfo(), true)."</pre>\n";
  214. echo "<br /><br />\n";
  215. exit;
  216. }
  217. $x = 0;
  218. foreach ($result[0] as $key => $value) {
  219. $column_array[$x++] = $key;
  220. }
  221. //column names
  222. echo '"'.implode('","', $column_array).'"'."\r\n";
  223. //column values
  224. foreach ($result as &$row) {
  225. $x = 1;
  226. foreach ($column_array as $column) {
  227. echo '"'.check_str($row[$column]).'"'.(($x++ < count($column_array)) ? ',' : null);
  228. }
  229. echo "\n";
  230. }
  231. }
  232. }
  233. }
  234. ?>