sql_query_result.php 8.4 KB

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