123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- /*
- * FusionPBX
- * Version: MPL 1.1
- *
- * The contents of this file are subject to the Mozilla Public License Version
- * 1.1 (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- * http://www.mozilla.org/MPL/
- *
- * Software distributed under the License is distributed on an "AS IS" basis,
- * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- * for the specific language governing rights and limitations under the
- * License.
- *
- * The Original Code is FusionPBX
- *
- * The Initial Developer of the Original Code is
- * Mark J Crane <[email protected]>
- * Portions created by the Initial Developer are Copyright (C) 2008-2024
- * the Initial Developer. All Rights Reserved.
- *
- * Contributor(s):
- * Mark J Crane <[email protected]>
- * Tim Fry <[email protected]>
- */
- /**
- * Description of maintenance_logs
- *
- * @author Tim Fry <[email protected]>
- */
- class maintenance_logs implements database_maintenance {
- const APP_NAME = 'maintenance_logs';
- const APP_UUID = '5be7b4c2-1a4f-4236-b91a-60e3c33904d7';
- const PERMISSION_PREFIX = 'maintenance_log_';
- const LIST_PAGE = 'maintenance_logs.php';
- const TABLE = 'maintenance_logs';
- const UUID_PREFIX = 'maintenance_log_';
- const TOGGLE_FIELD = 'maintenance_log_enable';
- const TOGGLE_VALUES = ['true', 'false'];
- private $database;
- private $settings;
- private $domain_uuid;
- private $user_uuid;
- /**
- * Called when a database maintenance is triggered from the maintenance service.
- * <p>This could be copied and pasted in to any other class that requires database maintenance
- * as long as the constant for TABLE exists. Currently most classes would need to be changed from using
- * $this->table (only available with an object) to be self::TABLE (available without an object).</p>
- */
- public static function database_maintenance(settings $settings): void {
- //set the table name for this class
- $table = self::TABLE;
- //get the database used
- $database = $settings->database();
- //get a list of all domains ignoring the domain_enabled field
- $domains = maintenance_service::get_domains($database, true);
- //run the maintenance per domain
- foreach ($domains as $domain_uuid => $domain_name) {
- //get the settings for this domain
- $domain_settings = new $settings(['database' => $database, 'domain_uuid' => $domain_uuid]);
- //get retention days using the class defined category and subcategory
- $retention_days = $domain_settings->get(self::database_maintenance_category(), self::database_maintenance_subcategory(), '');
- //ensure there is something to do
- if (!empty($retention_days) && is_numeric($retention_days)) {
- //delete old entries for this domain
- $database->execute("delete from v_{$table} where insert_date < NOW() - INTERVAL '{$retention_days} days' and domain_uuid = '{$domain_uuid}'");
- $code = $database->message['code'] ?? 0;
- //ensure the removal was successful
- if ($database->message['code'] == 200) {
- //log success
- maintenance_service::log_write(self::class, "Successfully removed entries older than $retention_days", $domain_uuid);
- } else {
- //log failure
- $message = $database->message['message'] ?? "An unknown error has occurred";
- maintenance_service::log_write(self::class, "Unable to remove old database records. Error message: $message ($code)", $domain_uuid, maintenance_service::LOG_ERROR);
- }
- } else {
- //database retention not set or not a valid number
- maintenance_service::log_write(self::class, 'Retention days not set', '', maintenance_service::LOG_ERROR);
- }
- }
- }
- public function __construct(database $database, settings $settings) {
- if ($database !== null) {
- $this->database = $database;
- } else {
- $this->database = new $database;
- }
- $this->domain_uuid = $_SESSION['domain_uuid'] ?? '';
- $this->user_uuid = $_SESSION['user_uuid'] ?? '';
- if ($settings !== null) {
- $this->settings = $settings;
- } else {
- $this->settings = new settings([
- 'database' => $database
- , 'domain_uuid' => $this->domain_uuid
- , 'user_uuid' => $this->user_uuid
- ]);
- }
- $database->app_name = self::APP_NAME;
- $database->app_uuid = self::APP_UUID;
- }
- /**
- * delete records
- */
- public function delete(array $records) {
- //add multi-lingual support
- $language = new text;
- $text = $language->get();
- if (!permission_exists(self::PERMISSION_PREFIX . 'delete') || empty($records)) {
- message::add($text['message-no_records'], 'negative');
- header('Location: ' . self::LIST_PAGE);
- exit;
- }
- //validate the token
- $token = new token;
- if (!$token->validate($_SERVER['PHP_SELF'])) {
- message::add($text['message-invalid_token'], 'negative');
- header('Location: ' . self::LIST_PAGE);
- exit;
- }
- //delete multiple records by building an array of records to remove
- $remove_array = [];
- foreach ($records as $x => $record) {
- if (!empty($record['checked']) && $record['checked'] == 'true' && is_uuid($record['uuid'])) {
- $remove_array[self::TABLE][$x][self::UUID_PREFIX . 'uuid'] = $record['uuid'];
- }
- }
- //delete the checked rows
- if (!empty($remove_array)) {
- //execute delete
- $this->database->delete($remove_array);
- //set message
- message::add($text['message-delete']);
- }
- }
- /**
- * toggle records
- */
- public function toggle(array $records) {
- //add multi-lingual support
- $language = new text;
- $text = $language->get();
- //check that we have something to do
- if (empty($records) || !permission_exists(self::PERMISSION_PREFIX . 'edit')) {
- message::add($text['message-no_records'], 'negative');
- header('Location: ' . self::LIST_PAGE);
- return;
- }
- //validate the token
- $token = new token;
- if (!$token->validate($_SERVER['PHP_SELF'])) {
- message::add($text['message-invalid_token'], 'negative');
- header('Location: ' . self::LIST_PAGE);
- exit;
- }
- //toggle the checked records to get current toggle state
- $uuids = [];
- foreach ($records as $x => $record) {
- if (!empty($record['checked']) && $record['checked'] == 'true' && is_uuid($record['uuid'])) {
- $uuids[] = "'" . $record['uuid'] . "'";
- }
- }
- if (!empty($uuids)) {
- $sql = "select " . self::UUID_PREFIX . "uuid as uuid, " . self::TOGGLE_FIELD . " as toggle from v_" . self::TABLE . " ";
- $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) ";
- $sql .= "and " . self::UUID_PREFIX . "uuid in (" . implode(', ', $uuids) . ") ";
- $parameters['domain_uuid'] = $this->domain_uuid;
- $rows = $this->database->select($sql, $parameters, 'all');
- if (!empty($rows)) {
- $states = [];
- foreach ($rows as $row) {
- $states[$row['uuid']] = $row['toggle'];
- }
- }
- }
- //build update array
- $x = 0;
- $array = [];
- foreach ($states as $uuid => $state) {
- $array[self::TABLE][$x][self::UUID_PREFIX . 'uuid'] = $uuid;
- $array[self::TABLE][$x][self::TOGGLE_FIELD] = $state == self::TOGGLE_VALUES[0] ? self::TOGGLE_VALUES[1] : self::TOGGLE_VALUES[0];
- $x++;
- }
- //save the changes
- if (!empty($array)) {
- //save the array
- $database->app_name = self::APP_NAME;
- $database->app_uuid = self::APP_UUID;
- $this->database->save($array);
- //set message
- message::add($text['message-toggle']);
- }
- }
- /**
- * copy records
- */
- public function copy(array $records) {
- //check that we have something to do
- if (empty($records) || !permission_exists(self::PERMISSION_PREFIX . 'add')) {
- return;
- }
- //add multi-lingual support
- $language = new text;
- $text = $language->get();
- //validate the token
- $token = new token;
- if (!$token->validate($_SERVER['PHP_SELF'])) {
- message::add($text['message-invalid_token'], 'negative');
- header('Location: ' . self::LIST_PAGE);
- exit;
- }
- //get checked records
- $uuids = [];
- foreach ($records as $record) {
- if (!empty($record['checked']) && $record['checked'] == 'true' && is_uuid($record['uuid'])) {
- $uuids[] = "'" . $record['uuid'] . "'";
- }
- }
- //create insert array from existing data
- if (!empty($uuids)) {
- $sql = "select * from v_" . self::TABLE
- . " where (domain_uuid = :domain_uuid or domain_uuid is null)"
- . " and " . self::UUID_PREFIX . "uuid in (" . implode(', ', $uuids) . ")";
- $parameters['domain_uuid'] = $this->domain_uuid;
- $rows = $this->database->select($sql, $parameters, 'all');
- if (!empty($rows)) {
- $array = [];
- foreach ($rows as $x => $row) {
- //copy data
- $array[self::TABLE][$x] = $row;
- //overwrite
- $array[self::TABLE][$x][self::UUID_PREFIX . 'uuid'] = uuid();
- $array[self::TABLE][$x]['bridge_description'] = trim($row['bridge_description'] . ' (' . $text['label-copy'] . ')');
- }
- $this->database->save($array);
- }
- //set message
- message::add($text['message-copy']);
- }
- }
- public static function database_maintenance_category(): string {
- return "maintenance_logs";
- }
- public static function database_maintenance_subcategory(): string {
- return "database_retention_days";
- }
- }
|