PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/adminer/inc/adminer/drivers/sqlite.inc.php

https://gitlab.com/Blueprint-Marketing/interoccupy.net
PHP | 673 lines | 583 code | 84 blank | 6 comment | 64 complexity | 35690e27cab0e96ad882dff90af5b0f5 MD5 | raw file
  1. <?php
  2. $drivers["sqlite"] = "SQLite 3";
  3. $drivers["sqlite2"] = "SQLite 2";
  4. if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
  5. $possible_drivers = array((isset($_GET["sqlite"]) ? "SQLite3" : "SQLite"), "PDO_SQLite");
  6. define("DRIVER", (isset($_GET["sqlite"]) ? "sqlite" : "sqlite2"));
  7. if (extension_loaded(isset($_GET["sqlite"]) ? "sqlite3" : "sqlite")) {
  8. if (isset($_GET["sqlite"])) {
  9. class Min_SQLite {
  10. var $extension = "SQLite3", $server_info, $affected_rows, $error, $_link;
  11. function Min_SQLite($filename) {
  12. $this->_link = new SQLite3($filename);
  13. $version = $this->_link->version();
  14. $this->server_info = $version["versionString"];
  15. }
  16. function query($query) {
  17. $result = @$this->_link->query($query);
  18. $this->error = "";
  19. if (!$result) {
  20. $this->error = $this->_link->lastErrorMsg();
  21. return false;
  22. } elseif ($result->numColumns()) {
  23. return new Min_Result($result);
  24. }
  25. $this->affected_rows = $this->_link->changes();
  26. return true;
  27. }
  28. function quote($string) {
  29. return (is_utf8($string)
  30. ? "'" . $this->_link->escapeString($string) . "'"
  31. : "x'" . reset(unpack('H*', $string)) . "'"
  32. );
  33. }
  34. function store_result() {
  35. return $this->_result;
  36. }
  37. function result($query, $field = 0) {
  38. $result = $this->query($query);
  39. if (!is_object($result)) {
  40. return false;
  41. }
  42. $row = $result->_result->fetchArray();
  43. return $row[$field];
  44. }
  45. }
  46. class Min_Result {
  47. var $_result, $_offset = 0, $num_rows;
  48. function Min_Result($result) {
  49. $this->_result = $result;
  50. }
  51. function fetch_assoc() {
  52. return $this->_result->fetchArray(SQLITE3_ASSOC);
  53. }
  54. function fetch_row() {
  55. return $this->_result->fetchArray(SQLITE3_NUM);
  56. }
  57. function fetch_field() {
  58. $column = $this->_offset++;
  59. $type = $this->_result->columnType($column);
  60. return (object) array(
  61. "name" => $this->_result->columnName($column),
  62. "type" => $type,
  63. "charsetnr" => ($type == SQLITE3_BLOB ? 63 : 0), // 63 - binary
  64. );
  65. }
  66. function __desctruct() {
  67. return $this->_result->finalize();
  68. }
  69. }
  70. } else {
  71. class Min_SQLite {
  72. var $extension = "SQLite", $server_info, $affected_rows, $error, $_link;
  73. function Min_SQLite($filename) {
  74. $this->server_info = sqlite_libversion();
  75. $this->_link = new SQLiteDatabase($filename);
  76. }
  77. function query($query, $unbuffered = false) {
  78. $method = ($unbuffered ? "unbufferedQuery" : "query");
  79. $result = @$this->_link->$method($query, SQLITE_BOTH, $error);
  80. $this->error = "";
  81. if (!$result) {
  82. $this->error = $error;
  83. return false;
  84. } elseif ($result === true) {
  85. $this->affected_rows = $this->changes();
  86. return true;
  87. }
  88. return new Min_Result($result);
  89. }
  90. function quote($string) {
  91. return "'" . sqlite_escape_string($string) . "'";
  92. }
  93. function store_result() {
  94. return $this->_result;
  95. }
  96. function result($query, $field = 0) {
  97. $result = $this->query($query);
  98. if (!is_object($result)) {
  99. return false;
  100. }
  101. $row = $result->_result->fetch();
  102. return $row[$field];
  103. }
  104. }
  105. class Min_Result {
  106. var $_result, $_offset = 0, $num_rows;
  107. function Min_Result($result) {
  108. $this->_result = $result;
  109. if (method_exists($result, 'numRows')) { // not available in unbuffered query
  110. $this->num_rows = $result->numRows();
  111. }
  112. }
  113. function fetch_assoc() {
  114. $row = $this->_result->fetch(SQLITE_ASSOC);
  115. if (!$row) {
  116. return false;
  117. }
  118. $return = array();
  119. foreach ($row as $key => $val) {
  120. $return[($key[0] == '"' ? idf_unescape($key) : $key)] = $val;
  121. }
  122. return $return;
  123. }
  124. function fetch_row() {
  125. return $this->_result->fetch(SQLITE_NUM);
  126. }
  127. function fetch_field() {
  128. $name = $this->_result->fieldName($this->_offset++);
  129. $pattern = '(\\[.*]|"(?:[^"]|"")*"|(.+))';
  130. if (preg_match("~^($pattern\\.)?$pattern\$~", $name, $match)) {
  131. $table = ($match[3] != "" ? $match[3] : idf_unescape($match[2]));
  132. $name = ($match[5] != "" ? $match[5] : idf_unescape($match[4]));
  133. }
  134. return (object) array(
  135. "name" => $name,
  136. "orgname" => $name,
  137. "orgtable" => $table,
  138. );
  139. }
  140. }
  141. }
  142. } elseif (extension_loaded("pdo_sqlite")) {
  143. class Min_SQLite extends Min_PDO {
  144. var $extension = "PDO_SQLite";
  145. function Min_SQLite($filename) {
  146. $this->dsn(DRIVER . ":$filename", "", "");
  147. }
  148. }
  149. }
  150. if (class_exists("Min_SQLite")) {
  151. class Min_DB extends Min_SQLite {
  152. function Min_DB() {
  153. $this->Min_SQLite(":memory:");
  154. }
  155. function select_db($filename) {
  156. if (is_readable($filename) && $this->query("ATTACH " . $this->quote(ereg("(^[/\\\\]|:)", $filename) ? $filename : dirname($_SERVER["SCRIPT_FILENAME"]) . "/$filename") . " AS a")) { // is_readable - SQLite 3
  157. $this->Min_SQLite($filename);
  158. return true;
  159. }
  160. return false;
  161. }
  162. function multi_query($query) {
  163. return $this->_result = $this->query($query);
  164. }
  165. function next_result() {
  166. return false;
  167. }
  168. }
  169. }
  170. function idf_escape($idf) {
  171. return '"' . str_replace('"', '""', $idf) . '"';
  172. }
  173. function table($idf) {
  174. return idf_escape($idf);
  175. }
  176. function connect() {
  177. return new Min_DB;
  178. }
  179. function get_databases() {
  180. return array();
  181. }
  182. function limit($query, $where, $limit, $offset = 0, $separator = " ") {
  183. return " $query$where" . ($limit !== null ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : "");
  184. }
  185. function limit1($query, $where) {
  186. global $connection;
  187. return ($connection->result("SELECT sqlite_compileoption_used('ENABLE_UPDATE_DELETE_LIMIT')") ? limit($query, $where, 1) : " $query$where");
  188. }
  189. function db_collation($db, $collations) {
  190. global $connection;
  191. return $connection->result("PRAGMA encoding"); // there is no database list so $db == DB
  192. }
  193. function engines() {
  194. return array();
  195. }
  196. function logged_user() {
  197. return get_current_user(); // should return effective user
  198. }
  199. function tables_list() {
  200. return get_key_vals("SELECT name, type FROM sqlite_master WHERE type IN ('table', 'view') ORDER BY (name = 'sqlite_sequence'), name", 1);
  201. }
  202. function count_tables($databases) {
  203. return array();
  204. }
  205. function table_status($name = "") {
  206. global $connection;
  207. $return = array();
  208. foreach (get_rows("SELECT name AS Name, type AS Engine FROM sqlite_master WHERE type IN ('table', 'view')" . ($name != "" ? " AND name = " . q($name) : "")) as $row) {
  209. $row["Oid"] = "t";
  210. $row["Auto_increment"] = "";
  211. $row["Rows"] = $connection->result("SELECT COUNT(*) FROM " . idf_escape($row["Name"]));
  212. $return[$row["Name"]] = $row;
  213. }
  214. foreach (get_rows("SELECT * FROM sqlite_sequence", null, "") as $row) {
  215. $return[$row["name"]]["Auto_increment"] = $row["seq"];
  216. }
  217. return ($name != "" ? $return[$name] : $return);
  218. }
  219. function is_view($table_status) {
  220. return $table_status["Engine"] == "view";
  221. }
  222. function fk_support($table_status) {
  223. global $connection;
  224. return !$connection->result("SELECT sqlite_compileoption_used('OMIT_FOREIGN_KEY')");
  225. }
  226. function fields($table) {
  227. $return = array();
  228. foreach (get_rows("PRAGMA table_info(" . table($table) . ")") as $row) {
  229. $type = strtolower($row["type"]);
  230. $default = $row["dflt_value"];
  231. $return[$row["name"]] = array(
  232. "field" => $row["name"],
  233. "type" => (eregi("int", $type) ? "integer" : (eregi("char|clob|text", $type) ? "text" : (eregi("blob", $type) ? "blob" : (eregi("real|floa|doub", $type) ? "real" : "numeric")))),
  234. "full_type" => $type,
  235. "default" => (ereg("'(.*)'", $default, $match) ? str_replace("''", "'", $match[1]) : ($default == "NULL" ? null : $default)),
  236. "null" => !$row["notnull"],
  237. "auto_increment" => eregi('^integer$', $type) && $row["pk"], //! possible false positive
  238. "privileges" => array("select" => 1, "insert" => 1, "update" => 1),
  239. "primary" => $row["pk"],
  240. );
  241. }
  242. return $return;
  243. }
  244. function indexes($table, $connection2 = null) {
  245. $return = array();
  246. $primary = array();
  247. foreach (fields($table) as $field) {
  248. if ($field["primary"]) {
  249. $primary[] = $field["field"];
  250. }
  251. }
  252. if ($primary) {
  253. $return[""] = array("type" => "PRIMARY", "columns" => $primary, "lengths" => array());
  254. }
  255. foreach (get_rows("PRAGMA index_list(" . table($table) . ")") as $row) {
  256. if (!ereg("^sqlite_", $row["name"])) {
  257. $return[$row["name"]]["type"] = ($row["unique"] ? "UNIQUE" : "INDEX");
  258. $return[$row["name"]]["lengths"] = array();
  259. foreach (get_rows("PRAGMA index_info(" . idf_escape($row["name"]) . ")") as $row1) {
  260. $return[$row["name"]]["columns"][] = $row1["name"];
  261. }
  262. }
  263. }
  264. return $return;
  265. }
  266. function foreign_keys($table) {
  267. $return = array();
  268. foreach (get_rows("PRAGMA foreign_key_list(" . table($table) . ")") as $row) {
  269. $foreign_key = &$return[$row["id"]];
  270. //! idf_unescape in SQLite2
  271. if (!$foreign_key) {
  272. $foreign_key = $row;
  273. }
  274. $foreign_key["source"][] = $row["from"];
  275. $foreign_key["target"][] = $row["to"];
  276. }
  277. return $return;
  278. }
  279. function view($name) {
  280. global $connection;
  281. return array("select" => preg_replace('~^(?:[^`"[]+|`[^`]*`|"[^"]*")* AS\\s+~iU', '', $connection->result("SELECT sql FROM sqlite_master WHERE name = " . q($name)))); //! identifiers may be inside []
  282. }
  283. function collations() {
  284. return (isset($_GET["create"]) ? get_vals("PRAGMA collation_list", 1) : array());
  285. }
  286. function information_schema($db) {
  287. return false;
  288. }
  289. function error() {
  290. global $connection;
  291. return h($connection->error);
  292. }
  293. function exact_value($val) {
  294. return q($val);
  295. }
  296. function check_sqlite_name($name) {
  297. // avoid creating PHP files on unsecured servers
  298. global $connection;
  299. $extensions = "db|sdb|sqlite";
  300. if (!preg_match("~^[^\\0]*\\.($extensions)\$~", $name)) {
  301. $connection->error = lang('Please use one of the extensions %s.', str_replace("|", ", ", $extensions));
  302. return false;
  303. }
  304. return true;
  305. }
  306. function create_database($db, $collation) {
  307. global $connection;
  308. if (file_exists($db)) {
  309. $connection->error = lang('File exists.');
  310. return false;
  311. }
  312. if (!check_sqlite_name($db)) {
  313. return false;
  314. }
  315. $link = new Min_SQLite($db); //! exception handler
  316. $link->query('PRAGMA encoding = "UTF-8"');
  317. $link->query('CREATE TABLE adminer (i)'); // otherwise creates empty file
  318. $link->query('DROP TABLE adminer');
  319. return true;
  320. }
  321. function drop_databases($databases) {
  322. global $connection;
  323. $connection->Min_SQLite(":memory:"); // to unlock file, doesn't work in PDO on Windows
  324. foreach ($databases as $db) {
  325. if (!@unlink($db)) {
  326. $connection->error = lang('File exists.');
  327. return false;
  328. }
  329. }
  330. return true;
  331. }
  332. function rename_database($name, $collation) {
  333. global $connection;
  334. if (!check_sqlite_name($name)) {
  335. return false;
  336. }
  337. $connection->Min_SQLite(":memory:");
  338. $connection->error = lang('File exists.');
  339. return @rename(DB, $name);
  340. }
  341. function auto_increment() {
  342. return " PRIMARY KEY" . (DRIVER == "sqlite" ? " AUTOINCREMENT" : "");
  343. }
  344. function alter_table($table, $name, $fields, $foreign, $comment, $engine, $collation, $auto_increment, $partitioning) {
  345. $use_all_fields = ($table == "" || $foreign);
  346. foreach ($fields as $field) {
  347. if ($field[0] != "" || !$field[1] || $field[2]) {
  348. $use_all_fields = true;
  349. break;
  350. }
  351. }
  352. $alter = array();
  353. $originals = array();
  354. $primary_key = false;
  355. foreach ($fields as $field) {
  356. if ($field[1]) {
  357. if ($field[1][6]) {
  358. $primary_key = true;
  359. }
  360. $alter[] = ($use_all_fields ? " " : "ADD ") . implode($field[1]);
  361. if ($field[0] != "") {
  362. $originals[$field[0]] = $field[1][0];
  363. }
  364. }
  365. }
  366. if ($use_all_fields) {
  367. if ($table != "") {
  368. queries("BEGIN");
  369. foreach (foreign_keys($table) as $foreign_key) {
  370. $columns = array();
  371. foreach ($foreign_key["source"] as $column) {
  372. if (!$originals[$column]) {
  373. continue 2;
  374. }
  375. $columns[] = $originals[$column];
  376. }
  377. $foreign[] = " FOREIGN KEY (" . implode(", ", $columns) . ") REFERENCES "
  378. . table($foreign_key["table"])
  379. . " (" . implode(", ", array_map('idf_escape', $foreign_key["target"]))
  380. . ") ON DELETE $foreign_key[on_delete] ON UPDATE $foreign_key[on_update]"
  381. ;
  382. }
  383. $indexes = array();
  384. foreach (indexes($table) as $key_name => $index) {
  385. $columns = array();
  386. foreach ($index["columns"] as $column) {
  387. if (!$originals[$column]) {
  388. continue 2;
  389. }
  390. $columns[] = $originals[$column];
  391. }
  392. $columns = "(" . implode(", ", $columns) . ")";
  393. if ($index["type"] != "PRIMARY") {
  394. $indexes[] = array($index["type"], $key_name, $columns);
  395. } elseif (!$primary_key) {
  396. $foreign[] = " PRIMARY KEY $columns";
  397. }
  398. }
  399. }
  400. $alter = array_merge($alter, $foreign);
  401. if (!queries("CREATE TABLE " . table($table != "" ? "adminer_$name" : $name) . " (\n" . implode(",\n", $alter) . "\n)")) {
  402. // implicit ROLLBACK to not overwrite $connection->error
  403. return false;
  404. }
  405. if ($table != "") {
  406. if ($originals && !queries("INSERT INTO " . table("adminer_$name") . " (" . implode(", ", $originals) . ") SELECT " . implode(", ", array_map('idf_escape', array_keys($originals))) . " FROM " . table($table))) {
  407. return false;
  408. }
  409. $triggers = array();
  410. foreach (triggers($table) as $trigger_name => $timing_event) {
  411. $trigger = trigger($trigger_name);
  412. $triggers[] = "CREATE TRIGGER " . idf_escape($trigger_name) . " " . implode(" ", $timing_event) . " ON " . table($name) . "\n$trigger[Statement]";
  413. }
  414. if (!queries("DROP TABLE " . table($table))) { // drop before creating indexes and triggers to allow using old names
  415. return false;
  416. }
  417. queries("ALTER TABLE " . table("adminer_$name") . " RENAME TO " . table($name));
  418. if (!alter_indexes($name, $indexes)) {
  419. return false;
  420. }
  421. foreach ($triggers as $trigger) {
  422. if (!queries($trigger)) {
  423. return false;
  424. }
  425. }
  426. queries("COMMIT");
  427. }
  428. } else {
  429. foreach ($alter as $val) {
  430. if (!queries("ALTER TABLE " . table($table) . " $val")) {
  431. return false;
  432. }
  433. }
  434. if ($table != $name && !queries("ALTER TABLE " . table($table) . " RENAME TO " . table($name))) {
  435. return false;
  436. }
  437. }
  438. if ($auto_increment) {
  439. queries("UPDATE sqlite_sequence SET seq = $auto_increment WHERE name = " . q($name)); // ignores error
  440. }
  441. return true;
  442. }
  443. function alter_indexes($table, $alter) {
  444. foreach ($alter as $val) {
  445. if (!queries($val[2] == "DROP"
  446. ? "DROP INDEX " . idf_escape($val[1])
  447. : "CREATE $val[0] " . ($val[0] != "INDEX" ? "INDEX " : "") . idf_escape($val[1] != "" ? $val[1] : uniqid($table . "_")) . " ON " . table($table) . " $val[2]"
  448. )) {
  449. return false;
  450. }
  451. }
  452. return true;
  453. }
  454. function truncate_tables($tables) {
  455. return apply_queries("DELETE FROM", $tables);
  456. }
  457. function drop_views($views) {
  458. return apply_queries("DROP VIEW", $views);
  459. }
  460. function drop_tables($tables) {
  461. return apply_queries("DROP TABLE", $tables);
  462. }
  463. function move_tables($tables, $views, $target) {
  464. return false;
  465. }
  466. function trigger($name) {
  467. global $connection;
  468. if ($name == "") {
  469. return array("Statement" => "BEGIN\n\t;\nEND");
  470. }
  471. preg_match('~^CREATE\\s+TRIGGER\\s*(?:[^`"\\s]+|`[^`]*`|"[^"]*")+\\s*([a-z]+)\\s+([a-z]+)\\s+ON\\s*(?:[^`"\\s]+|`[^`]*`|"[^"]*")+\\s*(?:FOR\\s*EACH\\s*ROW\\s)?(.*)~is', $connection->result("SELECT sql FROM sqlite_master WHERE name = " . q($name)), $match);
  472. return array("Timing" => strtoupper($match[1]), "Event" => strtoupper($match[2]), "Trigger" => $name, "Statement" => $match[3]);
  473. }
  474. function triggers($table) {
  475. $return = array();
  476. foreach (get_rows("SELECT * FROM sqlite_master WHERE type = 'trigger' AND tbl_name = " . q($table)) as $row) {
  477. preg_match('~^CREATE\\s+TRIGGER\\s*(?:[^`"\\s]+|`[^`]*`|"[^"]*")+\\s*([a-z]+)\\s*([a-z]+)~i', $row["sql"], $match);
  478. $return[$row["name"]] = array($match[1], $match[2]);
  479. }
  480. return $return;
  481. }
  482. function trigger_options() {
  483. return array(
  484. "Timing" => array("BEFORE", "AFTER", "INSTEAD OF"),
  485. "Type" => array("FOR EACH ROW"),
  486. );
  487. }
  488. function routine($name, $type) {
  489. // not supported by SQLite
  490. }
  491. function routines() {
  492. // not supported by SQLite
  493. }
  494. function routine_languages() {
  495. // not supported by SQLite
  496. }
  497. function begin() {
  498. return queries("BEGIN");
  499. }
  500. function insert_into($table, $set) {
  501. return queries("INSERT INTO " . table($table) . ($set ? " (" . implode(", ", array_keys($set)) . ")\nVALUES (" . implode(", ", $set) . ")" : "DEFAULT VALUES"));
  502. }
  503. function insert_update($table, $set, $primary) {
  504. return queries("REPLACE INTO " . table($table) . " (" . implode(", ", array_keys($set)) . ") VALUES (" . implode(", ", $set) . ")");
  505. }
  506. function last_id() {
  507. global $connection;
  508. return $connection->result("SELECT LAST_INSERT_ROWID()");
  509. }
  510. function explain($connection, $query) {
  511. return $connection->query("EXPLAIN $query");
  512. }
  513. function found_rows($table_status, $where) {
  514. }
  515. function types() {
  516. return array();
  517. }
  518. function schemas() {
  519. return array();
  520. }
  521. function get_schema() {
  522. return "";
  523. }
  524. function set_schema($scheme) {
  525. return true;
  526. }
  527. function create_sql($table, $auto_increment) {
  528. global $connection;
  529. return $connection->result("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = " . q($table));
  530. }
  531. function truncate_sql($table) {
  532. return "DELETE FROM " . table($table);
  533. }
  534. function use_sql($database) {
  535. }
  536. function trigger_sql($table, $style) {
  537. return implode(get_vals("SELECT sql || ';;\n' FROM sqlite_master WHERE type = 'trigger' AND tbl_name = " . q($table)));
  538. }
  539. function show_variables() {
  540. global $connection;
  541. $return = array();
  542. foreach (array("auto_vacuum", "cache_size", "count_changes", "default_cache_size", "empty_result_callbacks", "encoding", "foreign_keys", "full_column_names", "fullfsync", "journal_mode", "journal_size_limit", "legacy_file_format", "locking_mode", "page_size", "max_page_count", "read_uncommitted", "recursive_triggers", "reverse_unordered_selects", "secure_delete", "short_column_names", "synchronous", "temp_store", "temp_store_directory", "schema_version", "integrity_check", "quick_check") as $key) {
  543. $return[$key] = $connection->result("PRAGMA $key");
  544. }
  545. return $return;
  546. }
  547. function show_status() {
  548. $return = array();
  549. foreach (get_vals("PRAGMA compile_options") as $option) {
  550. list($key, $val) = explode("=", $option, 2);
  551. $return[$key] = $val;
  552. }
  553. return $return;
  554. }
  555. function convert_field($field) {
  556. }
  557. function unconvert_field($field, $return) {
  558. return $return;
  559. }
  560. function support($feature) {
  561. return ereg('^(view|trigger|variables|status|dump|move_col|drop_col)$', $feature);
  562. }
  563. $jush = "sqlite";
  564. $types = array("integer" => 0, "real" => 0, "numeric" => 0, "text" => 0, "blob" => 0);
  565. $structured_types = array_keys($types);
  566. $unsigned = array();
  567. $operators = array("=", "<", ">", "<=", ">=", "!=", "LIKE", "LIKE %%", "IN", "IS NULL", "NOT LIKE", "NOT IN", "IS NOT NULL", ""); // REGEXP can be user defined function
  568. $functions = array("hex", "length", "lower", "round", "unixepoch", "upper");
  569. $grouping = array("avg", "count", "count distinct", "group_concat", "max", "min", "sum");
  570. $edit_functions = array(
  571. array(
  572. // "text" => "date('now')/time('now')/datetime('now')",
  573. ), array(
  574. "integer|real|numeric" => "+/-",
  575. // "text" => "date/time/datetime",
  576. "text" => "||",
  577. )
  578. );
  579. }