maintenance.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. //set the domain and user
  44. $domain_uuid = $_SESSION['domain_uuid'] ?? '';
  45. $user_uuid = $_SESSION['user_uuid'] ?? '';
  46. //internationalization
  47. $language = new text;
  48. $text = $language->get();
  49. //create a database object
  50. $database = database::new();
  51. //process registering maintenance applications
  52. if (!empty($_REQUEST['action'])) {
  53. //validate the token
  54. $token = new token;
  55. if (!$token->validate($_SERVER['PHP_SELF'])) {
  56. message::add($text['message-invalid_token'], 'negative');
  57. header('Location: maintenance.php');
  58. exit;
  59. }
  60. $action = $_REQUEST['action'];
  61. $checked_apps = $_REQUEST['maintenance_apps'] ?? [];
  62. switch($action) {
  63. case 'toggle':
  64. if (permission_exists('maintenance_edit')) {
  65. if (maintenance::register_applications($database, $checked_apps)) {
  66. message::add($text['message-toggle']);
  67. } else {
  68. message::add($text['message-register_failed'], 'negative');
  69. }
  70. } else {
  71. message::add($text['message-action_prohibited'], 'negative');
  72. }
  73. break;
  74. }
  75. $toggle_maintenance_apps = $_REQUEST['toggle'];
  76. unset($token);
  77. }
  78. //create a boolean value to represent if show_all is enabled
  79. $show_all = !empty($_REQUEST['show']) && $_REQUEST['show'] === 'all' && permission_exists('maintenance_show_all');
  80. //order by
  81. if (!empty($_REQUEST['order_by'])) {
  82. $order_by = $_REQUEST['order_by'];
  83. } else {
  84. $order_by = '';
  85. }
  86. //paging
  87. $rows_per_page = $_SESSION['domain']['paging']['numeric'] ?? 50;
  88. if (!empty($_REQUEST['page'])) {
  89. $page = $_REQUEST['page'];
  90. $offset = $rows_per_page * $page;
  91. } else {
  92. $page = '';
  93. }
  94. //load the global settings only
  95. $default_settings = new settings(['database' => $database]);
  96. //get the list in the default settings
  97. $classes = $default_settings->get('maintenance', 'application', []);
  98. //get the display array
  99. $maintenance_apps = [];
  100. if ($show_all) {
  101. //get a list of domain names
  102. $domains = maintenance::get_domains($database);
  103. //get maintainers
  104. foreach ($classes as $maintainer) {
  105. $tasks = ['database', 'filesystem'];
  106. foreach ($tasks as $task) {
  107. $has_task = "has_{$task}_maintenance";
  108. if (maintenance::$has_task($maintainer)) {
  109. $category_method = "get_{$task}_category";
  110. $subcategory_method = "get_{$task}_subcategory";
  111. $category = maintenance::$category_method($maintainer);
  112. $subcategory = maintenance::$subcategory_method($maintainer);
  113. //get all UUIDs in the database associated with this setting
  114. $uuids = maintenance::find_all_uuids($database, $category, $subcategory);
  115. foreach ($uuids as $match) {
  116. $uuid = $match['uuid'];
  117. $status = $match['status'];
  118. $table = $match['table'];
  119. $domain_uuid = $match['domain_uuid'] ?? 'global';
  120. $value = $match['value'];
  121. $maintenance_apps[$category]["{$task}_maintenance"][$domain_uuid]["{$table}_setting_enabled"] = $match['status'];
  122. $maintenance_apps[$category]["{$task}_maintenance"][$domain_uuid]["{$table}_setting_value"] = $value;
  123. $maintenance_apps[$category]["{$task}_maintenance"][$domain_uuid]["{$table}_setting_uuid"] = $uuid;
  124. }
  125. }
  126. }
  127. }
  128. }
  129. else {
  130. //use the settings object to get the maintenance apps and their values
  131. foreach ($classes as $maintainer) {
  132. $domain_settings = new settings(['database' => $database, 'domain_uuid' => $_SESSION['domain_uuid'] ?? $domain_uuid]);
  133. //database retention days
  134. $database_retention_days = maintenance::get_database_retention_days($domain_settings, $maintainer);
  135. if (!empty($database_retention_days)) {
  136. $category = maintenance::get_database_category($maintainer);
  137. $maintenance_apps[$category]['database_maintenance'][$domain_uuid]['domain_setting_value'] = $database_retention_days;
  138. $maintenance_apps[$category]['database_maintenance'][$domain_uuid]['domain_setting_enabled'] = 'true';
  139. }
  140. //filesystem retention days
  141. $filesystem_retention_days = maintenance::get_filesystem_retention_days($domain_settings, $maintainer);
  142. if (!empty($filesystem_retention_days)) {
  143. $category = maintenance::get_filesystem_category($maintainer);
  144. $maintenance_apps[$category]['filesystem_maintenance'][$domain_uuid]['domain_setting_value'] = $filesystem_retention_days;
  145. $maintenance_apps[$category]['filesystem_maintenance'][$domain_uuid]['domain_setting_enabled'] = 'true';
  146. }
  147. }
  148. }
  149. //sort the result
  150. ksort($maintenance_apps);
  151. //set URL parameters
  152. $url_params = '';
  153. if ($show_all) {
  154. $url_params = '?show=all';
  155. }
  156. if (!empty($page)) {
  157. $url_params .= (empty($url_params) ? '?' : '&') . 'page=' . $page;
  158. }
  159. if (!empty($search)) {
  160. $url_params .= (empty($url_params) ? '?' : '&') . 'search=' . urlencode($search);
  161. }
  162. //get the list of domains
  163. $domain_names = maintenance::get_domains($database);
  164. //create the token
  165. $object = new token;
  166. $token = $object->create($_SERVER['PHP_SELF']);
  167. //show the content
  168. require_once dirname(__DIR__, 2) . '/resources/header.php';
  169. $document['title'] = $text['title-maintenance'];
  170. echo "<div class='action_bar' id='action_bar'>";
  171. echo " <div class='heading'><b>Maintenance</b></div>";
  172. echo " <div class='actions'>";
  173. echo button::create(['type'=>'button','label'=>$text['button-logs'],'icon'=>'fas fa-scroll fa-fw','id'=>'btn_logs', 'link'=>'maintenance_logs.php']);
  174. //show all
  175. if (!$show_all) {
  176. 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')]);
  177. }
  178. //search form
  179. echo " <form id='form_search' class='inline' method='get'>";
  180. if (!empty($page)) {
  181. echo " <input name='page' type=hidden value='$page'>";
  182. }
  183. if ($show_all) {
  184. echo " <input name='show' type=hidden value='all'>";
  185. }
  186. echo " <input type='text' class='txt list-search' name='search' id='search' value=\"".escape($search)."\" placeholder=\"".$text['label-search']."\" onkeydown=''>";
  187. echo button::create(['label'=>$text['button-search'],'icon'=>$_SESSION['theme']['button_icon_search'],'type'=>'submit','id'=>'btn_search']);
  188. echo " </form>";
  189. echo " </div>";
  190. //javascript modal boxes
  191. 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');"])]);
  192. 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');"])]);
  193. 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');"])]);
  194. echo " <div style='clear: both;'></div>";
  195. echo " <br/><br/>";
  196. echo " <form id='form_list' method='post'>";
  197. echo " <input type='hidden' id='action' name='action' value=''>";
  198. echo " <input type='hidden' name='search' value=\"".escape($search)."\">";
  199. echo " <div class='card'>\n";
  200. echo " <table class='list'>";
  201. echo " <tr class='list-header'>";
  202. echo " <th>Name</th>";
  203. if ($show_all) {
  204. echo " <th>Domain</th>";
  205. }
  206. echo " <th>Database Enabled</th>";
  207. echo " <th>Retention Days</th>";
  208. echo " <th>File System Enabled</th>";
  209. echo " <th>Retention Days</th>";
  210. echo " </tr>";
  211. //list all maintenance applications from the defaults settings for global and each domain and show if they are enabled or disabled
  212. foreach ($maintenance_apps as $class => $app_settings) {
  213. //make the class name more user-friendly
  214. if ($class === 'cdr') {
  215. $display_name = strtoupper(str_replace('_', ' ', $class));
  216. } else {
  217. $display_name = ucwords(str_replace('_', ' ', $class));
  218. }
  219. //display global first
  220. if ((isset($app_settings['database_maintenance']['global']) || isset($app_settings['filesystem_maintenance']['global'])) && $show_all) {
  221. echo "<tr class='list-row' style=''>";
  222. echo " <td>$display_name</td>";
  223. echo " <td>".$text['label-global']."</td>";
  224. if (isset($app_settings['database_maintenance']['global'])) {
  225. $enabled = $app_settings['database_maintenance']['global']['default_setting_enabled'] ? $text['label-yes'] : $text['label-no'];
  226. $value = $app_settings['database_maintenance']['global']['default_setting_value'];
  227. echo "<td>$enabled</td>";
  228. echo "<td>$value</td>";
  229. } else {
  230. echo "<td>&nbsp;</td>";
  231. echo "<td>&nbsp;</td>";
  232. }
  233. if (isset($app_settings['filesystem_maintenance']['global'])) {
  234. $enabled = $app_settings['filesystem_maintenance']['global']['default_setting_enabled'] ? $text['label-yes'] : $text['label-no'];
  235. $value = $app_settings['filesystem_maintenance']['global']['default_setting_value'];
  236. echo "<td>$enabled</td>";
  237. echo "<td>$value</td>";
  238. } else {
  239. echo "<td>&nbsp;</td>";
  240. echo "<td>&nbsp;</td>";
  241. }
  242. echo "</tr>";
  243. }
  244. if (isset($app_settings['database_maintenance']) || isset($app_settings['filesystem_maintenance'])) {
  245. //get all domains with database traits
  246. $database_domain_uuids = array_keys($app_settings['database_maintenance'] ?? []);
  247. //get all domains with filesystem traits
  248. $filesystem_domain_uuids = array_keys($app_settings['filesystem_maintenance'] ?? []);
  249. //combine database and filesystem domain_uuids without duplicates
  250. $domain_uuids = $database_domain_uuids + $filesystem_domain_uuids;
  251. //loop through domains that have the database and filesystem traits
  252. foreach ($domain_uuids as $domain_uuid) {
  253. //skip global it has already been done
  254. if ($domain_uuid === 'global') {
  255. continue;
  256. }
  257. echo "<tr class='list-row' style=''>";
  258. echo " <td>$display_name</td>";
  259. if ($show_all) {
  260. echo "<td>".$domain_names[$domain_uuid]."</td>";
  261. }
  262. if (isset($app_settings['database_maintenance'][$domain_uuid])) {
  263. $enabled = $app_settings['database_maintenance'][$domain_uuid]['domain_setting_enabled'] ? $text['label-yes'] : $text['label-no'];
  264. $value = $app_settings['database_maintenance'][$domain_uuid]['domain_setting_value'];
  265. echo "<td>$enabled</td>";
  266. echo "<td>$value</td>";
  267. } else {
  268. echo "<td>&nbsp;</td>";
  269. echo "<td>&nbsp;</td>";
  270. }
  271. if (isset($app_settings['filesystem_maintenance'][$domain_uuid])) {
  272. $enabled = $app_settings['filesystem_maintenance'][$domain_uuid]['domain_setting_enabled'] ? $text['label-yes'] : $text['label-no'];
  273. $value = $app_settings['filesystem_maintenance'][$domain_uuid]['domain_setting_value'];
  274. echo "<td>$enabled</td>";
  275. echo "<td>$value</td>";
  276. } else {
  277. echo "<td>&nbsp;</td>";
  278. echo "<td>&nbsp;</td>";
  279. }
  280. echo "</tr>";
  281. }
  282. }
  283. }
  284. echo " </table>";
  285. echo " </div>";
  286. echo " <input type='hidden' name='".$token['name']."' value='".$token['hash']."'>";
  287. echo " </form>";
  288. echo "</div>";
  289. //include the footer
  290. require_once dirname(__DIR__, 2) . '/resources/footer.php';