maintenance.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. <?php
  2. /*
  3. * FusionPBX
  4. * Version: MPL 1.1
  5. *
  6. * The contents of this file are subject to the Mozilla Public License Version
  7. * 1.1 (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. * http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is FusionPBX
  17. *
  18. * The Initial Developer of the Original Code is
  19. * Mark J Crane <[email protected]>
  20. * Portions created by the Initial Developer are Copyright (C) 2008-2024
  21. * the Initial Developer. All Rights Reserved.
  22. *
  23. * Contributor(s):
  24. * Mark J Crane <[email protected]>
  25. * Tim Fry <[email protected]>
  26. */
  27. /**
  28. * Description of maintenance_app
  29. *
  30. * @author Tim Fry <[email protected]>
  31. */
  32. class maintenance {
  33. const APP_NAME = 'Maintenance';
  34. const APP_UUID = '8a209bac-3bba-46eb-828e-bd4087fb9cee';
  35. const PERMISSION_PREFIX = 'maintenance_';
  36. const LIST_PAGE = 'maintenance.php';
  37. const TABLE = 'maintenance';
  38. const UUID_PREFIX = 'maintenance_';
  39. const TOGGLE_FIELD = 'maintenance_enabled';
  40. const TOGGLE_VALUES = ['true', 'false'];
  41. const DATABASE_SUBCATEGORY = 'database_retention_days';
  42. const FILESYSTEM_SUBCATEGORY = 'filesystem_retention_days';
  43. private static $app_config_list = null;
  44. /**
  45. * Returns an array of domain names with their domain UUID as the array key
  46. * @param database $database
  47. * @param bool $ignore_domain_enabled Omit the SQL where clause for domain_enabled
  48. * @param bool $domain_status When the <code>$ignore_domain_enabled</code> is false, set the status to true or false
  49. * @return array
  50. */
  51. public static function get_domains(database $database, bool $ignore_domain_enabled = false, bool $domain_status = true): array {
  52. $domains = [];
  53. $status_string = $domain_status ? 'true' : 'false';
  54. $sql = "select domain_uuid, domain_name from v_domains";
  55. if (!$ignore_domain_enabled) {
  56. $sql .= " where domain_enabled='$status_string'";
  57. }
  58. $result = $database->select($sql);
  59. if (!empty($result)) {
  60. foreach ($result as $row) {
  61. $domains[$row['domain_uuid']] = $row['domain_name'];
  62. }
  63. }
  64. return $domains;
  65. }
  66. /**
  67. * Registers new applications by searching for files in the project that have the signature for a method called
  68. * <code>public static function database_maintenance</code> or the method <code>public static function
  69. * filesystem_maintenance</code>. When found, they are added to the <code>default_settings</code> category of
  70. * <b>maintenance</b> and put in to subcategory array <b>application</b> in default settings table.
  71. * This function is intended to be called by the upgrade method.
  72. * @param database $database
  73. */
  74. public static function app_defaults(database $database) {
  75. //get the maintenance apps
  76. $database_maintenance_apps = self::find_classes_by_method('database_maintenance');
  77. $filesystem_maintenance_apps = self::find_classes_by_method('filesystem_maintenance');
  78. $maintenance_apps = $database_maintenance_apps + $filesystem_maintenance_apps;
  79. if (!empty($maintenance_apps)) {
  80. self::register_applications($database, $maintenance_apps);
  81. }
  82. }
  83. /**
  84. * Registers the list of applications given in the $maintenance_apps array to the global default settings
  85. * @param database $database
  86. * @param array $maintenance_apps
  87. * @return bool
  88. * @access public
  89. */
  90. public static function register_applications(database $database, array $maintenance_apps): bool {
  91. //make sure there is something to do
  92. if (count($maintenance_apps) === 0) {
  93. return false;
  94. }
  95. //query the database for the already registered applications
  96. $registered_apps = self::get_registered_applications($database);
  97. //load the text for the description
  98. $text = (new text())->get(null, 'app/' . __CLASS__);
  99. //register each app
  100. $new_maintenance_apps = [];
  101. $index = 0;
  102. foreach ($maintenance_apps as $application => $file) {
  103. //format the array for what the database object needs for saving data in the global default settings
  104. self::add_maintenance_app_to_array($registered_apps, $application, $text['description-default_settings_app'], $new_maintenance_apps, $index);
  105. //get the application settings from the class for database maintenance
  106. //self::add_database_maintenance_to_array($database, $application, $text['description-retention_days'], $new_maintenance_apps, $index);
  107. //get the application settings from the class for filesystem maintenance
  108. //self::add_filesystem_maintenance_to_array($database, $application, $text['description-retention_days'], $new_maintenance_apps, $index);
  109. }
  110. if (count($new_maintenance_apps) > 0) {
  111. $database->app_name = self::APP_NAME;
  112. $database->app_uuid = self::APP_UUID;
  113. $database->save($new_maintenance_apps);
  114. return true;
  115. }
  116. return false;
  117. }
  118. /**
  119. * Returns the class specified category to be used for the maintenance service. If the class does not have a method that
  120. * returns a string with the category to use then the class name will be used as the category.
  121. * @param object|string $class_name
  122. * @return string
  123. */
  124. public static function get_database_category($class_name): string {
  125. if (method_exists($class_name, 'database_maintenance_category')) {
  126. $default_value = $class_name::database_maintenance_category();
  127. } else {
  128. $default_value = $class_name;
  129. }
  130. return $default_value;
  131. }
  132. /**
  133. * Returns the class specified subcategory to be used for the maintenance service. If the class does not have a method that
  134. * returns a string with the subcategory to use then the class name will be used as the subcategory.
  135. * @param object|string $class_name
  136. * @return string
  137. */
  138. public static function get_database_subcategory($class_name): string {
  139. if (method_exists($class_name, 'database_maintenance_subcategory')) {
  140. $default_value = $class_name::database_maintenance_subcategory();
  141. } else {
  142. $default_value = self::DATABASE_SUBCATEGORY;
  143. }
  144. return $default_value;
  145. }
  146. public static function get_database_retention_days(settings $settings, $class_name, $default_value = ''): string {
  147. return $settings->get(self::get_database_category($class_name), self::get_database_subcategory($class_name), $default_value);
  148. }
  149. /**
  150. * Returns the class specified category to be used for the maintenance service. If the class does not have a method that
  151. * returns a string with the category to use then the class name will be used as the category.
  152. * @param object|string $class_name
  153. * @return string
  154. */
  155. public static function get_filesystem_category($class_name): string {
  156. if (method_exists($class_name, 'filesystem_maintenance_category')) {
  157. $default_value = $class_name::filesystem_maintenance_category();
  158. } else {
  159. $default_value = $class_name;
  160. }
  161. return $default_value;
  162. }
  163. /**
  164. * Returns the class specified subcategory to be used for the maintenance service. If the class does not have a method that
  165. * returns a string with the subcategory to use then the class name will be used as the subcategory.
  166. * @param object|string $class_name
  167. * @return string
  168. */
  169. public static function get_filesystem_subcategory($class_name): string {
  170. if (method_exists($class_name, 'filesystem_maintenance_subcategory')) {
  171. $default_value = $class_name::filesystem_maintenance_subcategory();
  172. } else {
  173. $default_value = self::FILESYSTEM_SUBCATEGORY;
  174. }
  175. return $default_value;
  176. }
  177. public static function get_filesystem_retention_days(settings $settings, $class_name, $default_value = ''): string {
  178. return $settings->get(self::get_filesystem_category($class_name), self::get_filesystem_subcategory($class_name), $default_value);
  179. }
  180. /**
  181. * Returns a list of maintenance applications already in the default settings table ignoring default_setting_enabled
  182. * @param database $database
  183. * @return array
  184. * @access public
  185. */
  186. public static function get_registered_applications(database $database): array {
  187. //get the already registered applications from the global default settings table
  188. $sql = "select default_setting_value"
  189. . " from v_default_settings"
  190. . " where default_setting_category = 'maintenance'"
  191. . " and default_setting_subcategory = 'application'";
  192. $result = $database->select($sql);
  193. if (!empty($result)) {
  194. $registered_applications = array_map(function ($row) { return $row['default_setting_value']; }, $result);
  195. }
  196. else {
  197. $registered_applications = [];
  198. }
  199. return $registered_applications;
  200. }
  201. /**
  202. * Get a value from the app_config.php file default settings
  203. * This function will load all app_config.php files and then load them in a class array only once. The first call will
  204. * have a performance impact but subsequent calls will have minimal impact as no files will be loaded.
  205. * @param string $category
  206. * @param string $subcategory
  207. * @return array|string|null If no value is found then null will be returned
  208. */
  209. public static function get_app_config_value(string $category, string $subcategory) {
  210. $return_value = null;
  211. //check if this is the first time loading the files
  212. if (self::$app_config_list === null) {
  213. //load the app_config files once
  214. self::load_app_config_list();
  215. }
  216. if (!empty(self::$app_config_list[$category][$subcategory])) {
  217. $return_value = self::$app_config_list[$category][$subcategory];
  218. }
  219. return $return_value;
  220. }
  221. /**
  222. * updates the array with a maintenance app using a format the database object save method can use to save in the default settings
  223. * default settings category: maintenance, subcategory: application, value: name of new application
  224. * @param array $registered_applications List of already registered applications
  225. * @param string $application Application class name
  226. * @param array $array Array in a format ready to use for the database save method
  227. * @param int $index Index pointing to the location to save within $array
  228. * @access private
  229. */
  230. private static function add_maintenance_app_to_array(&$registered_applications, $application, $description, &$array, &$index) {
  231. //verify that the application we need to add is not already listed in the registered applications array
  232. if (!in_array($application, $registered_applications)) {
  233. $array['default_settings'][$index]['default_setting_uuid'] = uuid();
  234. $array['default_settings'][$index]['default_setting_category'] = 'maintenance';
  235. $array['default_settings'][$index]['default_setting_subcategory'] = 'application';
  236. $array['default_settings'][$index]['default_setting_name'] = 'array';
  237. $array['default_settings'][$index]['default_setting_value'] = $application;
  238. $array['default_settings'][$index]['default_setting_enabled'] = 'true';
  239. $array['default_settings'][$index]['default_setting_description'] = $description;
  240. $index++;
  241. }
  242. }
  243. /**
  244. * Updates the array with a database maintenance app using a format the database object save method can use in default settings table
  245. * <p><b>default setting category</b>: class name that has the <code>implements database_maintenance;</code> statement<br>
  246. * <b>default setting subcategory</b>: "database_retention_days" (The class can override this setting to a custom value)<br>
  247. * <b>default setting value</b>: "30" (The class can override this setting to a custom value)<br>
  248. * <b>description</b>: "Number of days the maintenance application will keep the information."<br>
  249. * </p>
  250. * @param database $database Database object
  251. * @param string $application Application class name
  252. * @param string $description Description to be added in to the default settings table
  253. * @param array $array Array formatted for use in the database save method
  254. * @param int $index Index pointer to the save location in $array
  255. * @access private
  256. */
  257. private static function add_database_maintenance_to_array($database, $application, $description, &$array, &$index) {
  258. //get the application settings from the object for database maintenance
  259. if (method_exists($application, 'database_maintenance')) {
  260. $category = $application;
  261. $subcategory = self::DATABASE_SUBCATEGORY;
  262. //check if the default setting already exists in global default settings table
  263. $uuid = self::default_setting_uuid($database, $category, $subcategory);
  264. if (empty($uuid)) {
  265. //does not exist so create it
  266. $array['default_settings'][$index]['default_setting_category'] = $category;
  267. $array['default_settings'][$index]['default_setting_subcategory'] = $subcategory;
  268. $array['default_settings'][$index]['default_setting_uuid'] = uuid();
  269. $array['default_settings'][$index]['default_setting_name'] = 'numeric';
  270. $array['default_settings'][$index]['default_setting_value'] = '30';
  271. $array['default_settings'][$index]['default_setting_enabled'] = 'false';
  272. $array['default_settings'][$index]['default_setting_description'] = $description;
  273. $index++;
  274. } else {
  275. //already exists
  276. }
  277. }
  278. }
  279. /**
  280. * Query the database for an existing UUID of a maintenance application
  281. * @param database $database Database object
  282. * @param string $category Category to look for in the database
  283. * @param string $subcategory Subcategory or name of the setting in the default settings table
  284. * @return string Empty string if not found or a UUID
  285. */
  286. public static function default_setting_uuid(database $database, string $category, string $subcategory): string {
  287. $sql = 'select default_setting_uuid'
  288. . ' from v_default_settings'
  289. . ' where default_setting_category = :category'
  290. . ' and default_setting_subcategory = :subcategory'
  291. ;
  292. $params = [];
  293. $params['category'] = $category;
  294. $params['subcategory'] = $subcategory;
  295. return $database->select($sql, $params, 'column');
  296. }
  297. /**
  298. * Updates the array with a file system maintenance app using a format the database object save method can use in default settings table
  299. * <p><b>default setting category:</b> class name that has the <code>use filesystem_maintenance;</code> statement<br>
  300. * <b>default setting subcategory:</b> "filesystem_retention_days" (The class can override this setting to a custom value)<br>
  301. * <b>default setting value:</b> "30" (The class can override this setting to a custom value)<br>
  302. * <b>description:</b> "Number of days the maintenance application will keep the information."<br>
  303. * </p>
  304. * @param database $database Database object
  305. * @param string $application Application class name
  306. * @param string $description Description to be added in to the default settings table
  307. * @param array $array Array formatted for use in the database save method
  308. * @param int $index Index pointer to the save location in $array
  309. * @access private
  310. */
  311. private static function add_filesystem_maintenance_to_array($database, $application, $description, &$array, &$index) {
  312. if (method_exists($application, 'filesystem_maintenance')) {
  313. //the trait has this value defined
  314. $category = $application;
  315. //the trait has this value defined
  316. $subcategory = 'filesystem_retention_days';
  317. //check if the default setting already exists in global settings
  318. $uuid = self::default_setting_uuid($database, $category, $subcategory);
  319. if (empty($uuid)) {
  320. $array['default_settings'][$index]['default_setting_category'] = $category;
  321. $array['default_settings'][$index]['default_setting_subcategory'] = $subcategory;
  322. $array['default_settings'][$index]['default_setting_uuid'] = uuid();
  323. $array['default_settings'][$index]['default_setting_name'] = 'numeric';
  324. $array['default_settings'][$index]['default_setting_value'] = '30';
  325. $array['default_settings'][$index]['default_setting_enabled'] = 'false';
  326. $array['default_settings'][$index]['default_setting_description'] = $description;
  327. $index++;
  328. }
  329. }
  330. }
  331. /**
  332. * Finds class names that match the given method name
  333. * @param string $method_name Name of method to match in the class
  334. * @return array Names of classes or empty array if none found
  335. */
  336. public static function find_classes_by_method(string $method_name): array {
  337. //set defaults
  338. $found_classes = [];
  339. $project_root = dirname(__DIR__, 4);
  340. //get the autoloader
  341. if (!class_exists('auto_loader')) {
  342. require_once $project_root . '/resources/classes/auto_loader.php';
  343. new auto_loader();
  344. }
  345. //get all php files
  346. $files = glob($project_root . '/*/*/resources/classes/*.php');
  347. //iterate over the files
  348. foreach ($files as $file) {
  349. include_once $file;
  350. $class = basename($file, '.php');
  351. //check for the existence of the method
  352. if (method_exists($class, $method_name)) {
  353. $found_classes[$class] = $file;
  354. }
  355. }
  356. return $found_classes;
  357. }
  358. private static function load_app_config_list() {
  359. //app_config files use the array $apps to define the default_settings
  360. global $apps;
  361. //initialize the config list
  362. self::$app_config_list = [];
  363. //get the list of app_config files
  364. $project_dir = dirname(__DIR__, 4);
  365. $app_config_files = glob($project_dir . '/app/*/app_config.php');
  366. $core_config_files = glob($project_dir . '/core/*/app_config.php');
  367. $config_files = array_merge($app_config_files, $core_config_files);
  368. //iterate over list
  369. foreach ($config_files as $x => $file) {
  370. //include the app_config file
  371. include_once $file;
  372. //create a classname
  373. //get the array from the included file
  374. if (!empty($apps[$x]['default_settings'])) {
  375. foreach ($apps[$x]['default_settings'] as $setting) {
  376. //get the subcategory
  377. $category = $setting['default_setting_category'] ?? '';
  378. $subcategory = $setting['default_setting_subcategory'] ?? '';
  379. $value = $setting['default_setting_value'] ?? '';
  380. $type = $setting['default_setting_name'] ?? '';
  381. //check for array type
  382. if ($type !== 'array') {
  383. //store the values
  384. self::$app_config_list[$category][$subcategory] = $value;
  385. } else {
  386. $order = intval($setting['default_setting_order'] ?? '0');
  387. self::$app_config_list[$category][$subcategory][$order] = $value;
  388. }
  389. }
  390. }
  391. }
  392. }
  393. /**
  394. * Finds the UUID of a maintenance setting searching in the default settings, domain settings, and user settings tables.
  395. * This is primarily used for the dashboard display as there was a need to detect a setting even if it was disabled.
  396. * <p>NOTE:<br>
  397. * This will not deal with an array of returned values (such as maintenance_application) appropriately at this time as it has a limited scope of detecting a
  398. * string value that is unique with the category and subcategory combination across the tables.</p>
  399. * @param database $database Already connected database object
  400. * @param string $category Main category
  401. * @param string $subcategory Subcategory or name of the setting
  402. * @param bool $status Used for internal use but could be used to find a setting that is currently disabled
  403. * @return array Two-dimensional array assigned but using key/value pairs. The keys are:<br>
  404. * <ul>
  405. * <li>uuid: Primary UUID that would be chosen by the settings object
  406. * <li>uuids: Array of all matching category and subcategory strings
  407. * <li>table: Table name that the primary UUID was found
  408. * <li>status: bool true/false
  409. * </ul>
  410. * @access public
  411. */
  412. public static function find_uuid(database $database, string $category, string $subcategory, bool $status = true): array {
  413. //first look for false setting then override with enabled setting
  414. if ($status) {
  415. $uuids = self::find_uuid($database, $category, $subcategory, false);
  416. } else {
  417. //set defaults to not found
  418. $uuids = [];
  419. $uuids['uuid'] = '';
  420. $uuids['uuids'] = [];
  421. $uuids['table'] = '';
  422. $uuids['status'] = false;
  423. }
  424. $status_string = ($status) ? 'true' : 'false';
  425. //
  426. // Get the settings for false first then override the 'false' setting with the setting that is set to 'true'
  427. //
  428. //get global setting
  429. $result = self::get_uuids($database, 'default', $category, $subcategory, $status_string);
  430. if (!empty($result)) {
  431. $uuids['uuid'] = $result[0];
  432. $uuids['count'] = count($result);
  433. if (count($result) > 1) {
  434. $uuids['uuids'] = $result;
  435. } else {
  436. $uuids['uuids'] = [];
  437. }
  438. $uuids['table'] = 'default';
  439. $uuids['status'] = $status;
  440. }
  441. //override default with domain setting
  442. $result = self::get_uuids($database, 'domain', $category, $subcategory, $status_string);
  443. if (!empty($result)) {
  444. if ($uuids['count'] > 0) {
  445. $uuids['count'] += count($result);
  446. if (count($uuids['uuids']) > 0) {
  447. array_merge($uuids['uuids'], $result);
  448. } else {
  449. $ids[] = $uuids['uuid'];
  450. $uuids['uuids'] = array_merge($result, $ids);
  451. }
  452. } else {
  453. $uuids['count'] = count($result);
  454. if (count($result) > 1) {
  455. $uuids['uuids'] = $result;
  456. } else {
  457. $uuids['uuids'] = [];
  458. }
  459. }
  460. $uuids['uuid'] = $result[0];
  461. $uuids['table'] = 'domain';
  462. $uuids['status'] = $status;
  463. }
  464. //override domain with user setting
  465. $result = self::get_uuids($database, 'user', $category, $subcategory, $status_string);
  466. if (!empty($result)) {
  467. $uuids['uuid'] = $result[0];
  468. $uuids['count'] = count($result);
  469. if (count($result) > 1) {
  470. $uuids['uuids'] = $result;
  471. } else {
  472. $uuids['uuids'] = [];
  473. }
  474. $uuids['table'] = 'user';
  475. $uuids['status'] = $status;
  476. }
  477. return $uuids;
  478. }
  479. /**
  480. * Finds all UUIDs of a maintenance setting searching in the default settings, domain settings, and user settings tables.
  481. * @param database $database Already connected database object
  482. * @param string $category Main category
  483. * @param string $subcategory Subcategory or name of the setting
  484. * @param bool $status Used for internal use but could be used to find a setting that is currently disabled
  485. * @return array Two-dimensional array of matching database records
  486. * @access public
  487. */
  488. public static function find_all_uuids(database $database, string $category, string $subcategory, bool $status = true): array {
  489. $matches = [];
  490. //first look for false settings
  491. if ($status) {
  492. $matches = self::find_all_uuids($database, $category, $subcategory, false);
  493. }
  494. $status_string = ($status) ? 'true' : 'false';
  495. $tables = ['default', 'domain', 'user'];
  496. foreach ($tables as $table) {
  497. $sql = "select {$table}_setting_uuid, {$table}_setting_value from v_{$table}_settings s";
  498. $sql .= " where s.{$table}_setting_category = :category";
  499. $sql .= " and s.{$table}_setting_subcategory = :subcategory";
  500. $sql .= " and s.{$table}_setting_enabled = '$status_string'";
  501. //set search params
  502. $params = [];
  503. $params['category'] = $category;
  504. $params['subcategory'] = $subcategory;
  505. $result = $database->select($sql, $params, 'all');
  506. if (!empty($result)) {
  507. foreach ($result as $record) {
  508. $uuid = $record["{$table}_setting_uuid"];
  509. $value = $record["{$table}_setting_value"];
  510. $domain_uuid = $database->select("select domain_uuid from v_{$table}_settings where {$table}_setting_uuid = '$uuid'", null, 'column');
  511. if ($domain_uuid == false) {
  512. $domain_uuid = null;
  513. }
  514. $matches[] = [
  515. 'uuid' => $uuid,
  516. 'table' => $table,
  517. 'category' => $category,
  518. 'subcategory' => $subcategory,
  519. 'status' => $status,
  520. 'value' => $value,
  521. 'domain_uuid' => $domain_uuid,
  522. ];
  523. }
  524. }
  525. }
  526. return $matches;
  527. }
  528. /**
  529. * Called by the find_uuid function to actually search database using prepared data structures
  530. * @param database $database Database object
  531. * @param string $table Either 'default' or 'domain'
  532. * @param string $category Category value to match
  533. * @param string $subcategory Subcategory value to match
  534. * @param string $status Either 'true' or 'false'
  535. * @return array
  536. */
  537. public static function get_uuids(database $database, string $table, string $category, string $subcategory, string $status): array {
  538. $uuid = [];
  539. $sql = "select {$table}_setting_uuid from v_{$table}_settings s";
  540. $sql .= " where s.{$table}_setting_category = :category";
  541. $sql .= " and s.{$table}_setting_subcategory = :subcategory";
  542. $sql .= " and s.{$table}_setting_enabled = '$status'";
  543. //set search params
  544. $params = [];
  545. $params['category'] = $category;
  546. $params['subcategory'] = $subcategory;
  547. if ($table === 'domain' && !empty($_SESSION['domain_uuid']) && is_uuid($_SESSION['domain_uuid'])) {
  548. $sql .= " and s.domain_uuid = :domain_uuid";
  549. $params['domain_uuid'] = $_SESSION['domain_uuid'];
  550. }
  551. if ($table === 'user' && !empty($_SESSION['user_uuid']) && is_uuid($_SESSION['user_uuid'])) {
  552. $sql .= " and s.user_uuid = :user_uuid";
  553. $params['user_uuid'] = $_SESSION['user_uuid'];
  554. }
  555. $result = $database->select($sql, $params);
  556. if (!empty($result)) {
  557. if (is_array($result)) {
  558. $uuids = array_map(function ($value) use ($table) {
  559. if (is_array($value)) {
  560. return $value["{$table}_setting_uuid"];
  561. } else {
  562. return $value;
  563. }
  564. }, $result);
  565. $uuid = $uuids;
  566. } else {
  567. $uuid[] = $result;
  568. }
  569. }
  570. return $uuid;
  571. }
  572. /**
  573. * Returns the record set of the UUID in the table or an empty array
  574. * @param database $database
  575. * @param string $table Either 'domain' or 'default'
  576. * @param string $uuid
  577. * @return array
  578. */
  579. public static function get_value_by_uuid(database $database, string $table, string $uuid): array {
  580. if ($table === 'domain' || $table === 'default') {
  581. $sql = "select * from v_{$table}_settings"
  582. . " where {$table}_setting_uuid = :uuid";
  583. $parameters = [];
  584. $parameters['uuid'] = $uuid;
  585. $result = $database->select($sql, $parameters, 'row');
  586. if (!empty($result)) {
  587. return $result;
  588. }
  589. }
  590. return [];
  591. }
  592. public static function has_database_maintenance($object_or_class): bool {
  593. return method_exists($object_or_class, 'database_maintenance');
  594. }
  595. public static function has_filesystem_maintenance($object_or_class): bool {
  596. return method_exists($object_or_class, 'filesystem_maintenance');
  597. }
  598. }