Tim Fry 1 жил өмнө
parent
commit
0473316169

+ 9 - 4
resources/classes/maintenance_service.php

@@ -222,7 +222,7 @@ class maintenance_service extends service {
 		//go through the list of registered apps in default settings
 		foreach ($this->maintenance_apps as $class_name) {
 			//check the class implements database_maintenance
-			if (has_interface($class_name, 'database_maintenance')) {
+			if (has_trait($class_name, 'database_maintenance')) {
 				//call the method statically so it does not invoke the constructor
 				$class_name::database_maintenance($this->database, $this->settings);
 			}
@@ -237,7 +237,7 @@ class maintenance_service extends service {
 		//go through the list of registered apps in default settings
 		foreach ($this->maintenance_apps as $class_name) {
 			//check the class implements filesystem_maintenance
-			if (has_interface($class_name, 'filesystem_maintenance')) {
+			if (has_trait($class_name, 'filesystem_maintenance')) {
 				//call the method statically so it does not invoke the constructor
 				$class_name::filesystem_maintenance($this->database, $this->settings);
 			}
@@ -391,14 +391,19 @@ class maintenance_service extends service {
 		require_once dirname(__DIR__) . '/functions.php';
 		$classname = get_classname($worker_or_classname);
 		//protect against hijacking the log writer from a non maintenance worker
-		if (self::$logs !== null && (has_interface($classname, 'database_maintenance') || has_interface($classname, 'filesystem_maintenance'))) {
+		if (self::$logs !== null && (has_trait($classname, 'database_maintenance') || has_trait($classname, 'filesystem_maintenance'))) {
 			$row_index = count(self::$logs);
+			self::$logs[$row_index]['domain_uuid'] = $domain_uuid;
 			self::$logs[$row_index]['maintenance_log_uuid'] = uuid();
-			self::$logs[$row_index]['maintenance_log_domain_uuid'] = $domain_uuid;
 			self::$logs[$row_index]['maintenance_log_application'] = $classname;
 			self::$logs[$row_index]['maintenance_log_epoch'] = time();
 			self::$logs[$row_index]['maintenance_log_message'] = $message;
 			self::$logs[$row_index]['maintenance_log_status'] = $status;
+
+			//only allow up to 100 entries before flushing
+			if (count(self::$logs) > 100) {
+				self::log_flush();
+			}
 		}
 	}
 

+ 13 - 4
resources/classes/service.php

@@ -699,12 +699,21 @@ abstract class service {
 		defined('STDIN') or die('Unauthorized');
 
 		//force launching in a seperate process
-		if ($pid = pcntl_fork()) {
-			exit;
+//		if ($pid = pcntl_fork()) {
+//			exit;
+//		}
+//
+//		if ($cid = pcntl_fork()) {
+//			exit;
+//		}
+
+		//TODO remove updated settings object after merge
+		if (file_exists( __DIR__ . '/settings.php')) {
+			require_once __DIR__ . '/settings.php';
 		}
 
-		if ($cid = pcntl_fork()) {
-			exit;
+		if (file_exists(dirname(__DIR__).'/functions.php')) {
+			require_once dirname(__DIR__).'/functions.php';
 		}
 
 		//parse the cli options and store them statically

+ 264 - 0
resources/classes/settings.php

@@ -0,0 +1,264 @@
+<?php
+
+/**
+ * settings class
+ * 
+ */
+class settings {
+
+	private $domain_uuid;
+	private $user_uuid;
+	private $device_uuid;
+	private $device_profile_uuid;
+	private $category;
+	private $settings;
+	private $database;
+
+	/**
+	 * Called when the object is created
+	 * @param array setting_array
+	 * @depends database::new()
+	 */
+	public function __construct($setting_array = []) {
+
+		//open a database connection
+		$this->database = database::new();
+
+		//set the values from the array
+		$this->domain_uuid = $setting_array['domain_uuid'] ?? null;
+		$this->user_uuid = $setting_array['user_uuid'] ?? null;
+		$this->device_uuid = $setting_array['device_uuid'] ?? null;
+		$this->device_profile_uuid = $setting_array['device_profile_uuid'] ?? null;
+		$this->category = $setting_array['category'] ?? null;
+
+	}
+
+	public function reload(): void {
+		//set the default settings
+		$this->default_settings();
+
+		//set the domain settings
+		if (!empty($this->domain_uuid)) {
+			$this->domain_settings();
+		}
+
+		//set the user settings
+		if (!empty($this->user_uuid)) {
+			$this->user_settings();
+		}
+
+	}
+
+	/**
+	 * get the value
+	 * @param string category
+	 * @param string subcategory
+	 * @param mixed allows default value returned if category and subcategory not found
+	 */
+	public function get(string $category = null, string $subcategory = null, $default_value = null) {
+
+		if (empty($category)) {
+			return $this->settings;
+		}
+		elseif (empty($subcategory)) {
+			return $this->settings[$category];
+		}
+		else {
+			return $this->settings[$category][$subcategory] ?? $default_value;
+		}
+
+	}
+
+	/**
+	 * set the default, domain, user, device or device profile settings
+	 * @param string $table_prefix prefix for the table.
+	 * @param string $uuid uuid of the setting if available. If set to an empty string then a new uuid will be created.
+	 * @param string $category Category of the setting.
+	 * @param string $subcategory Subcategory of the setting.
+	 * @param string $type Type of the setting (array, numeric, text, etc)
+	 * @param string $value (optional) Value to set. Default is empty string.
+	 * @param bool $enabled (optional) True or False. Default is True.
+	 * @param string $description (optional) Description. Default is empty string.
+	 */
+	public function set(string $table_prefix, string $uuid, string $category, string $subcategory, string $type = 'text', string $value = "", bool $enabled = true, string $description = "") {
+		//set the table name
+		$table_name = $table_prefix.'_settings';
+
+		//init record as an array
+		$record = [];
+		if(!empty($this->domain_uuid)) {
+			$record[$table_name][0]['domain_uuid'] = $this->domain_uuid;
+		}
+		if(!empty($this->user_uuid)) {
+			$record[$table_name][0]['user_uuid'] = $this->user_uuid;
+		}
+		if(!empty($this->device_uuid)) {
+			$record[$table_name][0]['device_uuid'] = $this->device_uuid;
+		}
+		if(!empty($this->device_profile_uuid)) {
+			$record[$table_name][0]['device_profile_uuid'] = $this->device_profile_uuid;
+		}
+		if(!is_uuid($uuid)) {
+			$uuid = uuid();
+		}
+		//build the array
+		$record[$table_name][0][$table_prefix.'_setting_uuid'       ] = $uuid;
+		$record[$table_name][0][$table_prefix.'_setting_category'   ] = $category;
+		$record[$table_name][0][$table_prefix.'_setting_subcategory'] = $subcategory;
+		$record[$table_name][0][$table_prefix.'_setting_name'       ] = $type;
+		$record[$table_name][0][$table_prefix.'_setting_value'      ] = $value;
+		$record[$table_name][0][$table_prefix.'_setting_enabled'    ] = $enabled;
+		$record[$table_name][0][$table_prefix.'_setting_description'] = $description;
+
+		//grant temporary permissions
+		$p = new permissions;
+		$p->add($table_prefix.'_setting_add', 'temp');
+		$p->add($table_prefix.'_setting_edit', 'temp');
+
+		//execute insert
+		$this->database->app_name = $table_name;
+		$this->database->save($record);
+
+		//revoke temporary permissions
+		$p->delete($table_prefix.'_setting_add', 'temp');
+		$p->delete($table_prefix.'_setting_edit', 'temp');
+	}
+
+	/**
+	 * set the default settings
+	 * 
+	 */
+	private function default_settings() {
+
+		//get the default settings
+		$sql = "select * from v_default_settings ";
+		$sql .= "where default_setting_enabled = 'true' ";
+		if (!empty($this->category)) {
+			$sql .= "and default_setting_category = :default_setting_category ";
+			$parameters['default_setting_category'] = $this->category;
+		}
+		$sql .= "order by default_setting_order asc ";
+		$result = $this->database->select($sql, $parameters ?? null, 'all');
+		if (!empty($result)) {
+			foreach ($result as $row) {
+				$name = $row['default_setting_name'];
+				$category = $row['default_setting_category'];
+				$subcategory = $row['default_setting_subcategory'];
+				if (empty($subcategory)) {
+					if ($name == "array") {
+						if (!isset($this->settings[$category]) || !is_array($this->settings[$category])) {
+							$this->settings[$category] = array();
+						}
+						$this->settings[$category][] = $row['default_setting_value'];
+					}
+					else {
+						$this->settings[$category] = $row['default_setting_value'];
+					}
+				}
+				else {
+					if ($name == "array") {
+						if (!isset($this->settings[$category][$subcategory]) || !is_array($this->settings[$category][$subcategory])) {
+							$this->settings[$category][$subcategory] = array();
+						}
+						$this->settings[$category][$subcategory][] = $row['default_setting_value'];
+					}
+					else {
+						$this->settings[$category][$subcategory] = $row['default_setting_value'];
+					}
+				}
+			}
+		}
+		unset($sql, $result, $row);
+	}
+
+
+	/**
+	 * set the domain settings
+	 */
+	private function domain_settings() {
+
+		$sql = "select * from v_domain_settings ";
+		$sql .= "where domain_uuid = :domain_uuid ";
+		$sql .= "and domain_setting_enabled = 'true' ";
+		$parameters['domain_uuid'] = $this->domain_uuid;
+		$result = $this->database->select($sql, $parameters, 'all');
+		unset($sql, $parameters);
+		if (!empty($result)) {
+			foreach ($result as $row) {
+				$name = $row['domain_setting_name'];
+				$category = $row['domain_setting_category'];
+				$subcategory = $row['domain_setting_subcategory'];
+				if (empty($subcategory)) {
+					if ($name == "array") {
+						if (!isset($this->settings[$category]) || !is_array($this->settings[$category])) {
+						    $this->settings[$category] = array();
+						}
+						$this->settings[$category][] = $row['domain_setting_value'];
+					}
+					else {
+						$this->settings[$category] = $row['domain_setting_value'];
+					}
+				}
+				else {
+					if ($name == "array") {
+						if (!isset($this->settings[$category][$subcategory]) || !is_array($this->settings[$category][$subcategory])) {
+						    $this->settings[$category][$subcategory] = array();
+						}
+						$this->settings[$category][$subcategory][] = $row['domain_setting_value'];
+					}
+					else {
+						$this->settings[$category][$subcategory] = $row['domain_setting_value'];
+					}
+				}
+			}
+		}
+		unset($result, $row);
+
+	}
+
+
+	/**
+	 * set the user settings
+	 */
+	private function user_settings() {
+
+		$sql = "select * from v_user_settings ";
+		$sql .= "where domain_uuid = :domain_uuid ";
+		$sql .= "and user_uuid = :user_uuid ";
+		$sql .= " order by user_setting_order asc ";
+		$parameters['domain_uuid'] = $this->domain_uuid;
+		$parameters['user_uuid'] = $this->user_uuid;
+		$result = $this->database->select($sql, $parameters, 'all');
+		if (is_array($result)) {
+			foreach ($result as $row) {
+				if ($row['user_setting_enabled'] == 'true') {
+					$name = $row['user_setting_name'];
+					$category = $row['user_setting_category'];
+					$subcategory = $row['user_setting_subcategory'];
+					if (!empty($row['user_setting_value'])) {
+						if (empty($subcategory)) {
+							if ($name == "array") {
+								$this->settings[$category][] = $row['user_setting_value'];
+							}
+							else {
+								$this->settings[$category] = $row['user_setting_value'];
+							}
+						}
+						else {
+							if ($name == "array") {
+								$this->settings[$category][$subcategory][] = $row['user_setting_value'];
+							}
+							else {
+								$this->settings[$category][$subcategory] = $row['user_setting_value'];
+							}
+						}
+					}
+				}
+			}
+		}
+
+	}
+
+}
+
+?>

+ 2 - 2
resources/interfaces/database_maintenance.php

@@ -49,9 +49,9 @@ trait database_maintenance {
 		if (!empty($days) && is_numeric($days)) {
 			$domains = maintenance_service::get_domains($database);
 			foreach ($domains as $domain_uuid => $domain_name) {
-				$database->execute(self::get_database_maintenance_sql_statement($domain_uuid, $domain_name, $days));
+				$database->execute(self::database_maintenance_sql($domain_uuid, $days));
 				if ($database->message['code'] === '200') {
-					maintenance_service::log_write(self::$database_maintenance_application, "Removed maintenance log entries older than $days days for domain $domain_name.");
+					maintenance_service::log_write(self::$database_maintenance_application, "Removed maintenance log entries older than $days days for domain $domain_name");
 				} else {
 					maintenance_service::log_write(self::$database_maintenance_application, "Failed to clear entries", $domain_uuid, maintenance_service::LOG_ERROR);
 				}

+ 6 - 3
resources/interfaces/filesystem_maintenance.php

@@ -40,10 +40,10 @@ trait filesystem_maintenance {
 	public static $filesystem_retention_default_value = '30';
 
 	//class must implement this method
-	abstract public static function filesystem_maintenance_files(string $domain_uuid, string $domain_name, string $retention_days): array;
+	abstract public static function filesystem_maintenance_files(settings $settings, string $domain_uuid, string $domain_name, string $retention_days): array;
 
 	/**
-	 * A generic file remover
+	 * Removes old files on a per-domain basis
 	 * @param database $database
 	 * @param settings $settings
 	 * @return void
@@ -56,8 +56,11 @@ trait filesystem_maintenance {
 			$domains = maintenance_service::get_domains($database);
 			//loop over domains
 			foreach ($domains as $domain_uuid => $domain_name) {
+				//assign the settings object
+				$domain_settings = new settings(['database' => $database, 'domain_uuid' => $domain_uuid]);
+				$files = self::filesystem_maintenance_files($domain_settings, $domain_uuid, $domain_name, $days);
 				//loop over each file in the domain
-				foreach (self::filesystem_maintenance_files($domain_uuid, $domain_name, $days) as $file) {
+				foreach ($files as $file) {
 					//check if it is old enough
 					if (maintenance_service::days_since_created($file) > $days) {
 						if (unlink($file)) {