sql_query_result.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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-2023
  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. //access granted
  27. }
  28. else {
  29. echo "access denied";
  30. exit;
  31. }
  32. //add multi-lingual support
  33. $language = new text;
  34. $text = $language->get();
  35. //pdo database connection
  36. if (strlen($_REQUEST['id']) > 0) {
  37. require_once "sql_query_pdo.php";
  38. }
  39. //check the captcha
  40. $code = trim($_REQUEST["code"]);
  41. $command_authorized = false;
  42. if (strtolower($_SESSION['captcha']) == strtolower($code)) {
  43. $command_authorized = true;
  44. }
  45. if (!$command_authorized) {
  46. //catpcha invalid
  47. exit;
  48. }
  49. //get allowed table names
  50. switch ($db_type) {
  51. case 'sqlite': $sql = "select name from sqlite_master where type='table' order by name;"; break;
  52. 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;
  53. case 'mysql': $sql = "show tables"; break;
  54. }
  55. $database = new database;
  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 (is_array($_POST)) {
  65. $sql_type = trim($_POST["sql_type"]);
  66. $sql_cmd = trim($_POST["command"]);
  67. $table_name = trim($_POST["table_name"]);
  68. $header = "<html>\n";
  69. $header .= "<head>\n";
  70. $header .= "<style type='text/css'>\n";
  71. $header .= "\n";
  72. $header .= "body {\n";
  73. $header .= " font-family: arial;\n";
  74. $header .= " font-size: 12px;\n";
  75. $header .= " color: #444;\n";
  76. $header .= "}\n";
  77. $header .= "\n";
  78. $header .= "th {\n";
  79. $header .= " border-top: 1px solid #444;\n";
  80. $header .= " border-bottom: 1px solid #444;\n";
  81. $header .= " color: #fff;\n";
  82. $header .= " font-size: 12px;\n";
  83. $header .= " font-family: arial;\n";
  84. $header .= " font-weight: bold;\n";
  85. $header .= " background-color: #777;\n";
  86. $header .= " padding: 4px 7px;\n";
  87. $header .= " text-align: left;\n";
  88. $header .= "}\n";
  89. $header .= "\n";
  90. $header .= ".row_style0 {\n";
  91. $header .= " background-color: #eee;\n";
  92. $header .= " border-bottom: 1px solid #999;\n";
  93. $header .= " border-left: 1px solid #fff;\n";
  94. $header .= " font-size: 12px;\n";
  95. $header .= " color: #444;\n";
  96. $header .= " text-align: left;\n";
  97. $header .= " padding: 4px 7px;\n";
  98. $header .= " text-align: left;\n";
  99. $header .= " vertical-align: top;\n";
  100. $header .= "}\n";
  101. $header .= "\n";
  102. $header .= ".row_style0 a:link{ color:#444; }\n";
  103. $header .= ".row_style0 a:visited{ color:#444; }\n";
  104. $header .= ".row_style0 a:hover{ color:#444; }\n";
  105. $header .= ".row_style0 a:active{ color:#444; }\n";
  106. $header .= "\n";
  107. $header .= ".row_style1 {\n";
  108. $header .= " border-bottom: 1px solid #999;\n";
  109. $header .= " border-left: 1px solid #eee;\n";
  110. $header .= " background-color: #fff;\n";
  111. $header .= " font-size: 12px;\n";
  112. $header .= " color: #444;\n";
  113. $header .= " text-align: left;\n";
  114. $header .= " padding: 4px 7px;\n";
  115. $header .= " text-align: left;\n";
  116. $header .= " vertical-align: top;\n";
  117. $header .= "}\n";
  118. $header .= "</style>";
  119. $header .= "</head>\n";
  120. $header .= "<body style='margin: 0; padding: 8;'>\n";
  121. $footer = "<body>\n";
  122. $footer .= "<html>\n";
  123. if (empty($sql_type)) {
  124. echo $header;
  125. $c = 0;
  126. $row_style["0"] = "row_style0";
  127. $row_style["1"] = "row_style1";
  128. //determine queries to run and show
  129. if ($sql_cmd != '') { $sql_array = array_filter(explode(";", $sql_cmd)); }
  130. if ($table_name != '' && in_array($table_name, $tables)) { $sql_array[] = "select * from ".$table_name; }
  131. $show_query = (sizeof($sql_array) > 1) ? true : false;
  132. if (is_array($sql_array)) foreach($sql_array as $sql_index => $sql) {
  133. $sql = trim($sql);
  134. if (sizeof($sql_array) > 1 || $show_query) {
  135. if ($sql_index > 0) { echo "<br /><br /><br />"; }
  136. echo "<span style='display: block; font-family: monospace; padding: 8px; color: green; background-color: #eefff0;'>".escape($sql).";</span><br />";
  137. }
  138. //connect to the database and run the sql query
  139. $database = new database;
  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. $database = new database;
  199. $result = $database->execute($sql);
  200. $message = $database->message;
  201. if ($message['message'] != 'OK' || $message['code'] != 200) {
  202. echo "<b>".$text['label-error']."</b>";
  203. echo "<br /><br />\n";
  204. echo $message['message'].' ['.$message['code']."]<br />\n";
  205. if (is_array($message['error']) && @sizeof($message['error']) != 0) {
  206. foreach ($message['error'] as $error) {
  207. echo "<pre>".$error."</pre><br /><br />\n";
  208. }
  209. }
  210. exit;
  211. }
  212. $x = 0;
  213. if (is_array($result[0])) {
  214. foreach ($result[0] as $key => $value) {
  215. $column_array[$x++] = $key;
  216. }
  217. }
  218. $column_array_count = count($column_array);
  219. if (is_array($result)) {
  220. foreach ($result as $index => &$row) {
  221. echo "<div style='font-family: monospace; border-bottom: 1px solid #ccc; padding-bottom: 8px; ".($index != 0 ? 'padding-top: 8px;' : null)."'>\n";
  222. echo "insert into ".$table_name." (";
  223. if (is_array($column_array)) {
  224. foreach ($column_array as $column) {
  225. if ($column != "menuid" && $column != "menuparentid") {
  226. $columns[] = $column;
  227. }
  228. }
  229. }
  230. if (is_array($columns) && sizeof($columns) > 0) {
  231. echo implode(', ', $columns);
  232. }
  233. echo ") values (";
  234. if (is_array($column_array)) {
  235. foreach ($column_array as $column) {
  236. if ($column != "menuid" && $column != "menuparentid") {
  237. $values[] = $row[$column] != '' ? "'".escape($row[$column])."'" : 'null';
  238. }
  239. }
  240. }
  241. if (is_array($values) && sizeof($values) > 0) {
  242. echo implode(', ', $values);
  243. }
  244. echo ");\n";
  245. echo "</div>\n";
  246. unset($columns, $values);
  247. }
  248. }
  249. }
  250. echo $footer;
  251. }
  252. if ($sql_type == "csv") {
  253. //set the headers
  254. header('Content-type: application/octet-binary');
  255. if (strlen($sql_cmd) > 0) {
  256. header('Content-Disposition: attachment; filename=data.csv');
  257. }
  258. else if (in_array($table_name, $tables)) {
  259. header('Content-Disposition: attachment; filename='.$table_name.'.csv');
  260. }
  261. //get the table data
  262. if (strlen($sql_cmd) > 0) {
  263. $sql = $sql_cmd;
  264. }
  265. else if (in_array($table_name, $tables)) {
  266. $sql = "select * from ".$table_name;
  267. }
  268. if (strlen($sql) > 0) {
  269. $database = new database;
  270. $result = $database->execute($sql);
  271. $message = $database->message;
  272. if ($message['message'] != 'OK' || $message['code'] != 200) {
  273. echo "<b>".$text['label-error']."</b>";
  274. echo "<br /><br />\n";
  275. echo $message['message'].' ['.$message['code']."]<br />\n";
  276. if (is_array($message['error']) && @sizeof($message['error']) != 0) {
  277. foreach ($message['error'] as $error) {
  278. echo "<pre>".$error."</pre><br /><br />\n";
  279. }
  280. }
  281. exit;
  282. }
  283. //build the column array
  284. $x = 0;
  285. if (is_array($result[0])) {
  286. foreach ($result[0] as $key => $value) {
  287. $column_array[$x] = $key;
  288. $x++;
  289. }
  290. }
  291. //column names
  292. echo '"'.implode('","', $column_array).'"'."\r\n";
  293. //column values
  294. if (is_array($result)) {
  295. foreach ($result as &$row) {
  296. $x = 1;
  297. foreach ($column_array as $column) {
  298. echo '"'.$row[$column].'"'.($x++ < count($column_array) ? ',' : null);
  299. }
  300. echo "\n";
  301. }
  302. }
  303. }
  304. }
  305. }
  306. ?>