sql_query_result.php 8.6 KB

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