maintenance.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * FusionPBX
  5. * Version: MPL 1.1
  6. *
  7. * The contents of this file are subject to the Mozilla Public License Version
  8. * 1.1 (the "License"); you may not use this file except in compliance with
  9. * the License. You may obtain a copy of the License at
  10. * http://www.mozilla.org/MPL/
  11. *
  12. * Software distributed under the License is distributed on an "AS IS" basis,
  13. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. * for the specific language governing rights and limitations under the
  15. * License.
  16. *
  17. * The Original Code is FusionPBX
  18. *
  19. * The Initial Developer of the Original Code is
  20. * Mark J Crane <[email protected]>
  21. * Portions created by the Initial Developer are Copyright (C) 2008-2024
  22. * the Initial Developer. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. * Mark J Crane <[email protected]>
  26. * Tim Fry <[email protected]>
  27. */
  28. //check permission
  29. require_once dirname(__DIR__, 2) . '/resources/require.php';
  30. require_once "resources/check_auth.php";
  31. require_once "resources/paging.php";
  32. require_once __DIR__ . '/resources/functions.php';
  33. if (permission_exists('maintenance_view')) {
  34. // permission granted
  35. } else {
  36. die('Unauthorized');
  37. }
  38. if (!empty($_REQUEST['search'])) {
  39. $search = urldecode($_REQUEST['search']);
  40. } else {
  41. $search = '';
  42. }
  43. //internationalization
  44. $language = new text;
  45. $text = $language->get();
  46. //create a database object
  47. $database = database::new();
  48. //process registering maintenance applications
  49. if (!empty($_REQUEST['action'])) {
  50. //validate the token
  51. $token = new token;
  52. if (!$token->validate($_SERVER['PHP_SELF'])) {
  53. message::add($text['message-invalid_token'], 'negative');
  54. header('Location: maintenance.php');
  55. exit;
  56. }
  57. $action = $_REQUEST['action'];
  58. $checked_apps = $_REQUEST['maintenance_apps'] ?? [];
  59. switch($action) {
  60. case 'toggle':
  61. if (permission_exists('maintenance_edit')) {
  62. if (maintenance::register_applications($database, $checked_apps)) {
  63. message::add($text['message-toggle']);
  64. } else {
  65. message::add($text['message-register_failed'], 'negative');
  66. }
  67. } else {
  68. message::add($text['message-action_prohibited'], 'negative');
  69. }
  70. break;
  71. }
  72. $toggle_maintenance_apps = $_REQUEST['toggle'];
  73. unset($token);
  74. }
  75. //create a boolean value to represent if show_all is enabled
  76. if (!empty($_REQUEST['show'])) {
  77. $show_all = ($_REQUEST['show'] == 'all' && permission_exists('maintenance_show_all')) ? true : false;
  78. } else {
  79. $show_all = false;
  80. }
  81. //order by
  82. if (!empty($_REQUEST['order_by'])) {
  83. $order_by = $_REQUEST['order_by'];
  84. } else {
  85. $order_by = '';
  86. }
  87. //paging
  88. $rows_per_page = $_SESSION['domain']['paging']['numeric'] ?? 50;
  89. if (!empty($_REQUEST['page'])) {
  90. $page = $_REQUEST['page'];
  91. $offset = $rows_per_page * $page;
  92. } else {
  93. $page = '';
  94. }
  95. //load the settings
  96. $default_settings = new settings(['database' => $database]);
  97. //get the list in the default settings
  98. $classes = $default_settings->get('maintenance', 'application', []);
  99. //get the display array
  100. $maintenance_apps = [];
  101. if ($show_all) {
  102. //get a list of domain names
  103. $domains = maintenance::get_domains($database);
  104. //get maintainers
  105. foreach ($classes as $maintainer) {
  106. $tasks = ['database', 'filesystem'];
  107. foreach ($tasks as $task) {
  108. $has_task = "has_{$task}_maintenance";
  109. if (maintenance::$has_task($maintainer)) {
  110. $category_method = "get_{$task}_category";
  111. $subcategory_method = "get_{$task}_subcategory";
  112. $category = maintenance::$category_method($maintainer);
  113. $subcategory = maintenance::$subcategory_method($maintainer);
  114. //get all UUIDs in the database associated with this setting
  115. $uuids = maintenance::find_all_uuids($database, $category, $subcategory);
  116. foreach ($uuids as $match) {
  117. $uuid = $match['uuid'];
  118. $status = $match['status'];
  119. $table = $match['table'];
  120. $domain_uuid = $match['domain_uuid'] ?? 'global';
  121. $value = $match['value'];
  122. $maintenance_apps[$category]["{$task}_maintenance"][$domain_uuid]["{$table}_setting_enabled"] = $match['status'];
  123. $maintenance_apps[$category]["{$task}_maintenance"][$domain_uuid]["{$table}_setting_value"] = $value;
  124. $maintenance_apps[$category]["{$task}_maintenance"][$domain_uuid]["{$table}_setting_uuid"] = $uuid;
  125. }
  126. }
  127. }
  128. }
  129. }
  130. else {
  131. //use the settings object to get the maintenance apps and their values
  132. foreach ($classes as $maintainer) {
  133. $domain_settings = new settings(['database' => $database, 'domain_uuid' => $_SESSION['domain_uuid'] ?? $domain_uuid]);
  134. //database retention days
  135. $database_retention_days = maintenance::get_database_retention_days($domain_settings, $maintainer);
  136. if (!empty($database_retention_days)) {
  137. $category = maintenance::get_database_category($maintainer);
  138. $maintenance_apps[$category]['database_maintenance'][$domain_uuid]['domain_setting_value'] = $database_retention_days;
  139. $maintenance_apps[$category]['database_maintenance'][$domain_uuid]['domain_setting_enabled'] = 'true';
  140. }
  141. //filesystem retention days
  142. $filesystem_retention_days = maintenance::get_filesystem_retention_days($domain_settings, $maintainer);
  143. if (!empty($filesystem_retention_days)) {
  144. $category = maintenance::get_filesystem_category($maintainer);
  145. $maintenance_apps[$category]['filesystem_maintenance'][$domain_uuid]['domain_setting_value'] = $filesystem_retention_days;
  146. $maintenance_apps[$category]['filesystem_maintenance'][$domain_uuid]['domain_setting_enabled'] = 'true';
  147. }
  148. }
  149. }
  150. //sort the result
  151. ksort($maintenance_apps);
  152. //set URL parameters
  153. $url_params = '';
  154. if ($show_all) {
  155. $url_params = (empty($url_params) ? '?' : '&') . 'show=all';
  156. }
  157. if (!empty($page)) {
  158. $url_params .= (empty($url_params) ? '?' : '&') . 'page=' . $page;
  159. }
  160. if (!empty($search)) {
  161. $url_params .= (empty($url_params) ? '?' : '&') . 'search=' . urlencode($search);
  162. }
  163. //get the list of domains
  164. $domain_names = maintenance::get_domains($database);
  165. //create the token
  166. $object = new token;
  167. $token = $object->create($_SERVER['PHP_SELF']);
  168. //show the content
  169. require_once dirname(__DIR__, 2) . '/resources/header.php';
  170. $document['title'] = $text['title-maintenance'];
  171. echo "<div class='action_bar' id='action_bar'>";
  172. echo " <div class='heading'><b>Maintenance</b></div>";
  173. echo " <div class='actions'>";
  174. echo button::create(['type'=>'button','label'=>$text['button-logs'],'icon'=>'fas fa-scroll fa-fw','id'=>'btn_logs', 'link'=>'maintenance_logs.php']);
  175. //show all
  176. if (!$show_all) {
  177. echo button::create(['type'=>'button','alt'=>$text['button-show_all']??'Show All','label'=>$text['button-show_all']??'Show All','class'=>'btn btn-default','icon'=>$_SESSION['theme']['button_icon_all']??'globe','link'=>(empty($url_params) ? '?show=all' : $url_params . '&show=all')]);
  178. }
  179. //search form
  180. echo " <form id='form_search' class='inline' method='get'>";
  181. if (!empty($page)) {
  182. echo " <input name='page' type=hidden value='$page'>";
  183. }
  184. if ($show_all) {
  185. echo " <input name='show' type=hidden value='all'>";
  186. }
  187. echo " <input type='text' class='txt list-search' name='search' id='search' value=\"".escape($search)."\" placeholder=\"".$text['label-search']."\" onkeydown=''>";
  188. echo button::create(['label'=>$text['button-search'],'icon'=>$_SESSION['theme']['button_icon_search'],'type'=>'submit','id'=>'btn_search']);
  189. echo " </form>";
  190. echo " </div>";
  191. //javascript modal boxes
  192. echo modal::create(['id'=>'modal-copy','type'=>'copy','actions'=> button::create(['type'=>'button','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_copy','style'=>'float: right; margin-left: 15px;','collapse'=>'never','onclick'=>"modal_close(); list_action_set('copy'); list_form_submit('form_list');"])]);
  193. echo modal::create(['id'=>'modal-delete','type'=>'delete','actions'=> button::create(['type'=>'button','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_delete','style'=>'float: right; margin-left: 15px;','collapse'=>'never','onclick'=>"modal_close(); list_action_set('delete'); list_form_submit('form_list');"])]);
  194. echo modal::create(['id'=>'modal-toggle','type'=>'toggle','actions'=> button::create(['type'=>'button','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_toggle','style'=>'float: right; margin-left: 15px;','collapse'=>'never','onclick'=>"modal_close(); list_action_set('toggle'); list_form_submit('form_list');"])]);
  195. echo " <div style='clear: both;'></div>";
  196. echo " <br/><br/>";
  197. echo " <form id='form_list' method='post'>";
  198. echo " <input type='hidden' id='action' name='action' value=''>";
  199. echo " <input type='hidden' name='search' value=\"".escape($search)."\">";
  200. echo " <div class='card'>\n";
  201. echo " <table class='list'>";
  202. echo " <tr class='list-header'>";
  203. echo " <th>Name</th>";
  204. if ($show_all) {
  205. echo " <th>Domain</th>";
  206. }
  207. echo " <th>Database Enabled</th>";
  208. echo " <th>Retention Days</th>";
  209. echo " <th>File System Enabled</th>";
  210. echo " <th>Retention Days</th>";
  211. echo " </tr>";
  212. //list all maintenance applications from the defaults settings for global and each domain and show if they are enabled or disabled
  213. foreach ($maintenance_apps as $class => $app_settings) {
  214. //make the class name more user friendly
  215. $display_name = ucwords(str_replace('_', ' ', $class));
  216. //display global first
  217. if ((isset($app_settings['database_maintenance']['global']) || isset($app_settings['filesystem_maintenance']['global'])) && $show_all) {
  218. echo "<tr class='list-row' style=''>";
  219. echo " <td>$display_name</td>";
  220. echo " <td>".$text['label-global']."</td>";
  221. if (isset($app_settings['database_maintenance']['global'])) {
  222. $enabled = $app_settings['database_maintenance']['global']['default_setting_enabled'] ? $text['label-yes'] : $text['label-no'];
  223. $value = $app_settings['database_maintenance']['global']['default_setting_value'];
  224. echo "<td>$enabled</td>";
  225. echo "<td>$value</td>";
  226. } else {
  227. echo "<td>&nbsp;</td>";
  228. echo "<td>&nbsp;</td>";
  229. }
  230. if (isset($app_settings['filesystem_maintenance']['global'])) {
  231. $enabled = $app_settings['filesystem_maintenance']['global']['default_setting_enabled'] ? $text['label-yes'] : $text['label-no'];
  232. $value = $app_settings['filesystem_maintenance']['global']['default_setting_value'];
  233. echo "<td>$enabled</td>";
  234. echo "<td>$value</td>";
  235. } else {
  236. echo "<td>&nbsp;</td>";
  237. echo "<td>&nbsp;</td>";
  238. }
  239. echo "</tr>";
  240. }
  241. if (isset($app_settings['database_maintenance']) || isset($app_settings['filesystem_maintenance'])) {
  242. //get all domains with database traits
  243. $database_domain_uuids = array_keys($app_settings['database_maintenance'] ?? []);
  244. //get all domains with filesystem traits
  245. $filesystem_domain_uuids = array_keys($app_settings['filesystem_maintenance'] ?? []);
  246. //combine database and filesystem domain_uuids without duplicates
  247. $domain_uuids = $database_domain_uuids + $filesystem_domain_uuids;
  248. //loop through domains that have the database and filesystem traits
  249. foreach ($domain_uuids as $domain_uuid) {
  250. //skip global it has already been done
  251. if ($domain_uuid === 'global') {
  252. continue;
  253. }
  254. echo "<tr class='list-row' style=''>";
  255. echo " <td>$display_name</td>";
  256. if ($show_all) {
  257. echo "<td>".$domain_names[$domain_uuid]."</td>";
  258. }
  259. if (isset($app_settings['database_maintenance'][$domain_uuid])) {
  260. $enabled = $app_settings['database_maintenance'][$domain_uuid]['domain_setting_enabled'] ? $text['label-yes'] : $text['label-no'];
  261. $value = $app_settings['database_maintenance'][$domain_uuid]['domain_setting_value'];
  262. echo "<td>$enabled</td>";
  263. echo "<td>$value</td>";
  264. } else {
  265. echo "<td>&nbsp;</td>";
  266. echo "<td>&nbsp;</td>";
  267. }
  268. if (isset($app_settings['filesystem_maintenance'][$domain_uuid])) {
  269. $enabled = $app_settings['filesystem_maintenance'][$domain_uuid]['domain_setting_enabled'] ? $text['label-yes'] : $text['label-no'];
  270. $value = $app_settings['filesystem_maintenance'][$domain_uuid]['domain_setting_value'];
  271. echo "<td>$enabled</td>";
  272. echo "<td>$value</td>";
  273. } else {
  274. echo "<td>&nbsp;</td>";
  275. echo "<td>&nbsp;</td>";
  276. }
  277. echo "</tr>";
  278. }
  279. }
  280. }
  281. echo " </table>";
  282. echo " </div>";
  283. echo " <input type='hidden' name='".$token['name']."' value='".$token['hash']."'>";
  284. echo " </form>";
  285. echo "</div>";
  286. //include the footer
  287. require_once dirname(__DIR__, 2) . '/resources/footer.php';