sql_query_result.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 ($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. $database = new database;
  139. $result = $database->execute($sql, null, 'all');
  140. $message = $database->message;
  141. if ($message['message'] == 'OK' && $message['code'] == 200) {
  142. echo "<b>".$text['label-records'].": ".count($result)."</b>";
  143. echo "<br /><br />\n";
  144. }
  145. else {
  146. echo "<b>".$text['label-error']."</b>";
  147. echo "<br /><br />\n";
  148. echo $message['message'].' ['.$message['code']."]<br />\n";
  149. if (is_array($message['error']) && @sizeof($message['error']) != 0) {
  150. foreach ($message['error'] as $error) {
  151. echo "<pre>".$error."</pre><br /><br />\n";
  152. }
  153. }
  154. }
  155. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  156. $x = 0;
  157. if (is_array($result[0])) {
  158. echo "<thead>\n";
  159. echo " <tr>\n";
  160. foreach ($result[0] as $key => $value) {
  161. echo "<th>".escape($key)."</th>\n";
  162. $column_array[$x++] = $key;
  163. }
  164. echo " </tr>\n";
  165. echo "</thead>\n";
  166. }
  167. $x = 1;
  168. if (is_array($result)) {
  169. echo "<tbody>\n";
  170. foreach ($result as &$row) {
  171. if ($x++ > 1000) { break; }
  172. echo "<tr>\n";
  173. if (is_array($column_array)) {
  174. foreach ($column_array as $column_index => $column) {
  175. echo "<td class='".$row_style[$c]."' ".(($column_index == 0) ? "style='border-left: none;'" : null).">".escape($row[$column])."&nbsp;</td>\n";
  176. }
  177. }
  178. echo "</tr>\n";
  179. $c = ($c == 0) ? 1 : 0;
  180. }
  181. echo "</tbody>\n";
  182. }
  183. echo "</table>\n";
  184. echo "<br>\n";
  185. unset($result, $column_array);
  186. }
  187. echo $footer;
  188. }
  189. if ($sql_type == "inserts") {
  190. echo $header;
  191. $sql = trim($sql);
  192. //get the table data
  193. $sql = (strlen($sql_cmd) == 0 && in_array($table_name, $tables)) ? "select * from ".$table_name : $sql_cmd;
  194. if (strlen($sql) > 0) {
  195. $database = new database;
  196. $result = $database->execute($sql);
  197. $message = $database->message;
  198. if ($message['message'] != 'OK' || $message['code'] != 200) {
  199. echo "<b>".$text['label-error']."</b>";
  200. echo "<br /><br />\n";
  201. echo $message['message'].' ['.$message['code']."]<br />\n";
  202. if (is_array($message['error']) && @sizeof($message['error']) != 0) {
  203. foreach ($message['error'] as $error) {
  204. echo "<pre>".$error."</pre><br /><br />\n";
  205. }
  206. }
  207. exit;
  208. }
  209. $x = 0;
  210. if (is_array($result[0])) {
  211. foreach ($result[0] as $key => $value) {
  212. $column_array[$x++] = $key;
  213. }
  214. }
  215. $column_array_count = count($column_array);
  216. if (is_array($result)) {
  217. foreach ($result as $index => &$row) {
  218. echo "<div style='font-family: monospace; border-bottom: 1px solid #ccc; padding-bottom: 8px; ".($index != 0 ? 'padding-top: 8px;' : null)."'>\n";
  219. echo "insert into ".$table_name." (";
  220. if (is_array($column_array)) {
  221. foreach ($column_array as $column) {
  222. if ($column != "menuid" && $column != "menuparentid") {
  223. $columns[] = $column;
  224. }
  225. }
  226. }
  227. if (is_array($columns) && sizeof($columns) > 0) {
  228. echo implode(', ', $columns);
  229. }
  230. echo ") values (";
  231. if (is_array($column_array)) {
  232. foreach ($column_array as $column) {
  233. if ($column != "menuid" && $column != "menuparentid") {
  234. $values[] = $row[$column] != '' ? "'".escape($row[$column])."'" : 'null';
  235. }
  236. }
  237. }
  238. if (is_array($values) && sizeof($values) > 0) {
  239. echo implode(', ', $values);
  240. }
  241. echo ");\n";
  242. echo "</div>\n";
  243. unset($columns, $values);
  244. }
  245. }
  246. }
  247. echo $footer;
  248. }
  249. if ($sql_type == "csv") {
  250. //set the headers
  251. header('Content-type: application/octet-binary');
  252. if (strlen($sql_cmd) > 0) {
  253. header('Content-Disposition: attachment; filename=data.csv');
  254. }
  255. else if (in_array($table_name, $tables)) {
  256. header('Content-Disposition: attachment; filename='.$table_name.'.csv');
  257. }
  258. //get the table data
  259. if (strlen($sql_cmd) > 0) {
  260. $sql = $sql_cmd;
  261. }
  262. else if (in_array($table_name, $tables)) {
  263. $sql = "select * from ".$table_name;
  264. }
  265. if (strlen($sql) > 0) {
  266. $database = new database;
  267. $result = $database->execute($sql);
  268. $message = $database->message;
  269. if ($message['message'] != 'OK' || $message['code'] != 200) {
  270. echo "<b>".$text['label-error']."</b>";
  271. echo "<br /><br />\n";
  272. echo $message['message'].' ['.$message['code']."]<br />\n";
  273. if (is_array($message['error']) && @sizeof($message['error']) != 0) {
  274. foreach ($message['error'] as $error) {
  275. echo "<pre>".$error."</pre><br /><br />\n";
  276. }
  277. }
  278. exit;
  279. }
  280. //build the column array
  281. $x = 0;
  282. if (is_array($result[0])) {
  283. foreach ($result[0] as $key => $value) {
  284. $column_array[$x] = $key;
  285. $x++;
  286. }
  287. }
  288. //column names
  289. echo '"'.implode('","', $column_array).'"'."\r\n";
  290. //column values
  291. if (is_array($result)) {
  292. foreach ($result as &$row) {
  293. $x = 1;
  294. foreach ($column_array as $column) {
  295. echo '"'.$row[$column].'"'.($x++ < count($column_array) ? ',' : null);
  296. }
  297. echo "\n";
  298. }
  299. }
  300. }
  301. }
  302. }
  303. ?>