sql_query_result.php 7.8 KB

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