瀏覽代碼

Update settings using real boolean (#7165)

* update settings using real boolean
frytimo 10 月之前
父節點
當前提交
58bb08884c
共有 1 個文件被更改,包括 61 次插入31 次删除
  1. 61 31
      resources/classes/email.php

+ 61 - 31
resources/classes/email.php

@@ -45,10 +45,10 @@ if (!class_exists('email')) {
 		public $subject;
 		public $body;
 		public $from_address;
-		public $from_name; 
+		public $from_name;
 		public $priority;
-		public $debug_level; 
-		public $attachments; 
+		public $debug_level;
+		public $attachments;
 		public $read_confirmation;
 		public $error;
 		public $response;
@@ -56,7 +56,7 @@ if (!class_exists('email')) {
 		/**
 		 * called when the object is created
 		 */
-		public function __construct() {
+		public function __construct($params = []) {
 			//assign the variables
 			$this->app_name = 'email';
 			$this->name = 'email';
@@ -64,6 +64,29 @@ if (!class_exists('email')) {
 			$this->priority = 0;
 			$this->debug_level = 3;
 			$this->read_confirmation = false;
+
+			//set the domain_uuid
+			$this->domain_uuid = $params['domain_uuid'] ?? $_SESSION['domain_uuid'] ?? '';
+
+			if (isset($params['settings'])) {
+				$this->settings = $params['settings'];
+			}
+
+			//set the database from the settings object if available
+			if ($this->settings instanceof settings && !isset($this->database)) {
+				$this->database = $this->settings->database();
+			}
+
+			//ensure we have a valid database object
+			if (!($this->database instanceof database)) {
+				$this->database = $params['database'] ?? database::new();
+			}
+
+			//ensure we have a valid settings object
+			if (!($this->settings) instanceof settings) {
+				$this->settings = new settings(['database' => $this->database, 'domain_uuid' => $this->domain_uuid]);
+			}
+
 		}
 
 		/**
@@ -126,7 +149,7 @@ if (!class_exists('email')) {
 						if (substr($body_content_type, 0, 9) == "text/html") {
 							$this->body = $row["Body"];
 						}
-						if (substr($body_content_type, 0, 10) == "text/plain") { 
+						if (substr($body_content_type, 0, 10) == "text/plain") {
 							$body_plain = $row["Body"];
 							$this->body = $body_plain;
 						}
@@ -156,7 +179,7 @@ if (!class_exists('email')) {
 					//testfax.tif
 					$file = $parts_array["FileName"];
 
-					//inline	
+					//inline
 					$filedisposition = $parts_array["FileDisposition"];
 
 					$body_part = $parts_array["BodyPart"];
@@ -193,7 +216,7 @@ if (!class_exists('email')) {
 							$this->attachments[$x]['type'] = 'string';
 							$this->attachments[$x]['name'] = $file;
 							$this->attachments[$x]['value'] = $parts_array["Body"];
-						
+
 						//increment the id
 							$x++;
 					}
@@ -208,16 +231,18 @@ if (!class_exists('email')) {
 		public function send() {
 
 			//set the domain_uuid if not set
-			if (!isset($this->domain_uuid) && isset($_SESSION)) {
+			if (!isset($this->domain_uuid) && isset($_SESSION['domain_uuid'])) {
 				$this->domain_uuid = $_SESSION['domain_uuid'];
 			}
 
-			//get the email queue settings
-			$setting = new settings(["domain_uuid" => $this->domain_uuid]);
+			//ensure the settings object matches the domain uuid requested in this object
+			if ($this->settings->get_domain_uuid() !== $this->domain_uuid) {
+				$this->settings = new settings(["database" => $this->database, "domain_uuid" => $this->domain_uuid]);
+			}
 
 			//set the send_method if not already set
 			if (!isset($this->method)) {
-				if ($setting->get('email_queue','enabled') == 'true') {
+				if ($setting->get('email_queue','enabled', true)) {
 					$this->method = 'queue';
 				}
 				else {
@@ -324,17 +349,23 @@ if (!class_exists('email')) {
 				$p->add("email_queue_attachment_add", 'temp');
 
 				//save the dialplan
-				$database = new database;
-				$database->app_name = 'email';
-				$database->app_uuid = 'e24b5dab-3bcc-42e8-99c1-19b0c558c2d7';
-				$database->save($array);
-				//$dialplan_response = $database->message;
+				$this->database->app_name = 'email';
+				$this->database->app_uuid = 'e24b5dab-3bcc-42e8-99c1-19b0c558c2d7';
+				$this->database->save($array);
+				//$dialplan_response = $this->database->message;
 				unset($array);
 
 				//remove temporary permissions
 				$p->delete("dialplan_add", 'temp');
 				$p->delete("dialplan_detail_add", 'temp');
 
+				//return a human readable response for debugging
+				if ($this->database->message['message'] == 'OK') {
+					return "Added to queue";
+				} else {
+					//return the SQL server message
+					return $this->database->message['message'];
+				}
 			}
 
 			//send the email directly
@@ -407,24 +438,24 @@ if (!class_exists('email')) {
 					include_once("resources/phpmailer/class.smtp.php");
 
 					//use the email default settings
-					if (!empty($setting->get('email','smtp_hostname'))) {
-						$smtp['hostname'] = $setting->get('email','smtp_hostname');
+					if (!empty($this->settings->get('email','smtp_hostname'))) {
+						$smtp['hostname'] = $this->settings->get('email','smtp_hostname');
 					}
-					$smtp['host'] 		= (!empty($setting->get('email','smtp_host')) ? $setting->get('email','smtp_host'): '127.0.0.1');
-					if (!empty($setting->get('email','smtp_port'))) {
-						$smtp['port'] = (int)$setting->get('email','smtp_port');
+					$smtp['host'] 		= (!empty($this->settings->get('email','smtp_host')) ? $this->settings->get('email','smtp_host'): '127.0.0.1');
+					if (!empty($this->settings->get('email','smtp_port'))) {
+						$smtp['port'] = (int)$this->settings->get('email','smtp_port');
 					}
 					else {
 						$smtp['port'] = 0;
 					}
-					$smtp['secure'] 	= $setting->get('email','smtp_secure');
-					$smtp['auth'] 		= $setting->get('email','smtp_auth');
-					$smtp['username'] 	= $setting->get('email','smtp_username');
-					$smtp['password'] 	= $setting->get('email','smtp_password');
-					$smtp['from'] 		= $setting->get('voicemail','smtp_from') ?? $setting->get('email','smtp_from');
-					$smtp['from_name'] 	= $setting->get('voicemail','smtp_from_name') ?? $setting->get('email','smtp_from_name');
-					$smtp['validate_certificate'] = $setting->get('email','smtp_validate_certificate');
-					$smtp['crypto_method'] = $setting->get('email','smtp_crypto_method') ?? null;
+					$smtp['secure'] 	= $this->settings->get('email','smtp_secure');
+					$smtp['auth'] 		= $this->settings->get('email','smtp_auth');
+					$smtp['username'] 	= $this->settings->get('email','smtp_username');
+					$smtp['password'] 	= $this->settings->get('email','smtp_password');
+					$smtp['from'] 		= $this->settings->get('voicemail','smtp_from') ?? $this->settings->get('email','smtp_from');
+					$smtp['from_name'] 	= $this->settings->get('voicemail','smtp_from_name') ?? $this->settings->get('email','smtp_from_name');
+					$smtp['validate_certificate'] = $this->settings->get('email','smtp_validate_certificate');
+					$smtp['crypto_method'] = $this->settings->get('email','smtp_crypto_method') ?? null;
 
 					//override the domain-specific smtp server settings, if any
 					$sql = "select domain_setting_subcategory, domain_setting_value ";
@@ -433,8 +464,7 @@ if (!class_exists('email')) {
 					$sql .= "and (domain_setting_category = 'email' or domain_setting_category = 'voicemail') ";
 					$sql .= "and domain_setting_enabled = 'true' ";
 					$parameters['domain_uuid'] = $this->domain_uuid;
-					$database = new database;
-					$result = $database->select($sql, $parameters, 'all');
+					$result = $this->database->select($sql, $parameters, 'all');
 					if (is_array($result) && @sizeof($result) != 0) {
 						foreach ($result as $row) {
 							if ($row['domain_setting_value'] != '') {