sql_query_result.php 9.9 KB

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