sql_query_result.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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-2025
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //includes files
  22. require_once dirname(__DIR__, 2) . "/resources/require.php";
  23. require_once "resources/check_auth.php";
  24. //check permissions
  25. if (!permission_exists('sql_query')) {
  26. echo "access denied";
  27. exit;
  28. }
  29. //show blank iframe initially
  30. if (empty($_REQUEST)) {
  31. echo '&nbsp;';
  32. exit;
  33. }
  34. //add multi-lingual support
  35. $language = new text;
  36. $text = $language->get();
  37. //connect to the database
  38. $database = database::new();
  39. //pdo database connection
  40. if (strlen($_REQUEST['id']) > 0) {
  41. require_once "sql_query_pdo.php";
  42. }
  43. //validate the token
  44. $token = new token;
  45. if (!$token->validate('/app/sql_query/sql_query.php')) {
  46. message::add($text['message-invalid_token'],'negative');
  47. echo "invalid token\n";
  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' or table_type='VIEW') order by table_type, table_name"; break;
  54. case 'mysql': $sql = "show tables"; break;
  55. }
  56. $rows = $database->select($sql, null, 'all');
  57. if (is_array($rows) && @sizeof($rows) != 0) {
  58. foreach ($rows as $row) {
  59. $tables[] = $row['name'];
  60. }
  61. }
  62. unset($sql, $rows, $row);
  63. //show the content
  64. if (!empty($_REQUEST)) {
  65. //get the sql type, command and name
  66. $sql_type = trim($_REQUEST["sql_type"]);
  67. $sql_cmd = trim($_REQUEST["command"]);
  68. $table_name = trim($_REQUEST["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 (empty($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 (!empty($sql_cmd)) { $sql_array = array_filter(explode(";", $sql_cmd)); }
  131. if (!empty($table_name) && in_array($table_name, $tables)) { $sql_array[] = "select * from ".$table_name; }
  132. $show_query = (!empty($sql_array) && sizeof($sql_array) > 1) ? true : false;
  133. if (!empty($sql_array) && 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. //connect to the database and run the sql query
  140. $result = $database->execute($sql, null, 'all');
  141. $message = $database->message;
  142. //show the number of records in the result
  143. if (!empty($result) && is_array($result)) {
  144. echo "<b>".$text['label-records'].": ".count($result)."</b>";
  145. echo "<br /><br />\n";
  146. }
  147. else {
  148. echo "<b>".$text['label-error']."</b>";
  149. echo "<br /><br />\n";
  150. echo $message['message']."<br />\n";
  151. if (!empty($message['error'])) {
  152. foreach ($message['error'] as $error) {
  153. echo "<pre>".$error."</pre><br /><br />\n";
  154. }
  155. }
  156. }
  157. //show the results from the SQL query
  158. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  159. $x = 0;
  160. if (is_array($result[0])) {
  161. echo "<thead>\n";
  162. echo " <tr>\n";
  163. foreach ($result[0] as $key => $value) {
  164. echo "<th>".escape($key)."</th>\n";
  165. $column_array[$x++] = $key;
  166. }
  167. echo " </tr>\n";
  168. echo "</thead>\n";
  169. }
  170. $x = 1;
  171. if (is_array($result)) {
  172. echo "<tbody>\n";
  173. foreach ($result as &$row) {
  174. if ($x++ > 1000) { break; }
  175. echo "<tr>\n";
  176. if (is_array($column_array)) {
  177. foreach ($column_array as $column_index => $column) {
  178. echo "<td class='".$row_style[$c]."' ".(($column_index == 0) ? "style='border-left: none;'" : null).">".escape($row[$column])."&nbsp;</td>\n";
  179. }
  180. }
  181. echo "</tr>\n";
  182. $c = ($c == 0) ? 1 : 0;
  183. }
  184. echo "</tbody>\n";
  185. }
  186. echo "</table>\n";
  187. echo "<br>\n";
  188. unset($result, $column_array);
  189. }
  190. echo $footer;
  191. }
  192. if ($sql_type == "inserts") {
  193. echo $header;
  194. $sql = trim($sql ?? '');
  195. //get the table data
  196. $sql = (strlen($sql_cmd) == 0 && in_array($table_name, $tables)) ? "select * from ".$table_name : $sql_cmd;
  197. if (strlen($sql) > 0) {
  198. $result = $database->execute($sql);
  199. $message = $database->message;
  200. if ($message['message'] != 'OK' || $message['code'] != 200) {
  201. echo "<b>".$text['label-error']."</b>";
  202. echo "<br /><br />\n";
  203. echo $message['message'].' ['.$message['code']."]<br />\n";
  204. if (is_array($message['error']) && @sizeof($message['error']) != 0) {
  205. foreach ($message['error'] as $error) {
  206. echo "<pre>".$error."</pre><br /><br />\n";
  207. }
  208. }
  209. exit;
  210. }
  211. $x = 0;
  212. if (is_array($result[0])) {
  213. foreach ($result[0] as $key => $value) {
  214. $column_array[$x++] = $key;
  215. }
  216. }
  217. $column_array_count = count($column_array);
  218. if (is_array($result)) {
  219. foreach ($result as $index => &$row) {
  220. echo "<div style='font-family: monospace; border-bottom: 1px solid #ccc; padding-bottom: 8px; ".($index != 0 ? 'padding-top: 8px;' : null)."'>\n";
  221. echo "insert into ".$table_name." (";
  222. if (is_array($column_array)) {
  223. foreach ($column_array as $column) {
  224. if ($column != "menuid" && $column != "menuparentid") {
  225. $columns[] = $column;
  226. }
  227. }
  228. }
  229. if (is_array($columns) && sizeof($columns) > 0) {
  230. echo implode(', ', $columns);
  231. }
  232. echo ") values (";
  233. if (is_array($column_array)) {
  234. foreach ($column_array as $column) {
  235. if ($column != "menuid" && $column != "menuparentid") {
  236. $values[] = $row[$column] != '' ? "'".escape($row[$column])."'" : 'null';
  237. }
  238. }
  239. }
  240. if (is_array($values) && sizeof($values) > 0) {
  241. echo implode(', ', $values);
  242. }
  243. echo ");\n";
  244. echo "</div>\n";
  245. unset($columns, $values);
  246. }
  247. }
  248. }
  249. echo $footer;
  250. }
  251. if ($sql_type == "csv") {
  252. //set the headers
  253. header('Content-type: application/octet-binary');
  254. if (strlen($sql_cmd) > 0) {
  255. header('Content-Disposition: attachment; filename=data.csv');
  256. }
  257. else if (in_array($table_name, $tables)) {
  258. header('Content-Disposition: attachment; filename='.$table_name.'.csv');
  259. }
  260. //get the table data
  261. if (strlen($sql_cmd) > 0) {
  262. $sql = $sql_cmd;
  263. }
  264. else if (in_array($table_name, $tables)) {
  265. $sql = "select * from ".$table_name;
  266. }
  267. if (strlen($sql) > 0) {
  268. $database = new database;
  269. $result = $database->execute($sql);
  270. $message = $database->message;
  271. if ($message['message'] != 'OK' || $message['code'] != 200) {
  272. echo "<b>".$text['label-error']."</b>";
  273. echo "<br /><br />\n";
  274. echo $message['message'].' ['.$message['code']."]<br />\n";
  275. if (is_array($message['error']) && @sizeof($message['error']) != 0) {
  276. foreach ($message['error'] as $error) {
  277. echo "<pre>".$error."</pre><br /><br />\n";
  278. }
  279. }
  280. exit;
  281. }
  282. //build the column array
  283. $x = 0;
  284. if (is_array($result[0])) {
  285. foreach ($result[0] as $key => $value) {
  286. $column_array[$x] = $key;
  287. $x++;
  288. }
  289. }
  290. //column names
  291. echo '"'.implode('","', $column_array).'"'."\r\n";
  292. //column values
  293. if (is_array($result)) {
  294. foreach ($result as &$row) {
  295. $x = 1;
  296. foreach ($column_array as $column) {
  297. echo '"'.$row[$column].'"'.($x++ < count($column_array) ? ',' : null);
  298. }
  299. echo "\n";
  300. }
  301. }
  302. }
  303. }
  304. }