| @@ -1,0 +1,66 @@ | ||
| 1 | +<?php | |
| 2 | +defined('ABSPATH') || die('Restricted Access'); | |
| 3 | + | |
| 4 | +function acym_getTables(bool $reload = false): array | |
| 5 | +{ | |
| 6 | + static $tables = null; | |
| 7 | + if (empty($tables) || $reload) { | |
| 8 | + $tables = acym_loadResultArray('SHOW TABLES'); | |
| 9 | + } | |
| 10 | + | |
| 11 | + return $tables; | |
| 12 | +} | |
| 13 | + | |
| 14 | +function acym_getColumns(string $table, bool $acyTable = true, bool $addPrefix = true): array | |
| 15 | +{ | |
| 16 | + if ($addPrefix) { | |
| 17 | + $prefix = $acyTable ? '#__acym_' : '#__'; | |
| 18 | + $table = $prefix.$table; | |
| 19 | + } | |
| 20 | + | |
| 21 | + static $columns = []; | |
| 22 | + if (empty($columns[$table])) { | |
| 23 | + $columns[$table] = acym_loadResultArray('SHOW COLUMNS FROM '.acym_secureDBColumn($table)); | |
| 24 | + } | |
| 25 | + | |
| 26 | + return $columns[$table] ?? []; | |
| 27 | +} | |
| 28 | + | |
| 29 | +function acym_secureDBColumn(string $fieldName): string | |
| 30 | +{ | |
| 31 | + if (preg_match('|[^a-z0-9#_.-]|i', $fieldName) !== 0) { | |
| 32 | + die('field, table or database "'.esc_html($fieldName).'" not secured'); | |
| 33 | + } | |
| 34 | + | |
| 35 | + return $fieldName; | |
| 36 | +} | |
| 37 | + | |
| 38 | +function acym_getDatabases(): array | |
| 39 | +{ | |
| 40 | + try { | |
| 41 | + $allDatabases = acym_loadResultArray('SHOW DATABASES'); | |
| 42 | + } catch (Exception $exception) { | |
| 43 | + $allDatabases = []; | |
| 44 | + $allDatabases[] = acym_loadResult('SELECT DATABASE();'); | |
| 45 | + } | |
| 46 | + | |
| 47 | + $databases = []; | |
| 48 | + foreach ($allDatabases as $database) { | |
| 49 | + $databases[$database] = $database; | |
| 50 | + } | |
| 51 | + | |
| 52 | + return $databases; | |
| 53 | +} | |
| 54 | + | |
| 55 | +function acym_addLimit(string &$query, int $limit = 1, ?int $offset = null): void | |
| 56 | +{ | |
| 57 | + if (strpos($query, 'LIMIT ') !== false) { | |
| 58 | + return; | |
| 59 | + } | |
| 60 | + | |
| 61 | + $query .= ' LIMIT '; | |
| 62 | + if (!empty($offset)) { | |
| 63 | + $query .= intval($offset).','; | |
| 64 | + } | |
| 65 | + $query .= intval($limit); | |
| 66 | +} | |