noalyss Version-10
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
mail_parameter.class.php
Go to the documentation of this file.
1<?php
2
3namespace Noalyss;
4
5/*
6 * This file is part of NOALYSS.
7 *
8 * NOALYSS is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * NOALYSS is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with NOALYSS; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21*/
22// Copyright Author Dany De Bontridder danydb@noalyss.eu 21.10.2025
23
24/**
25 * @file
26 * @brief configuration email and test PHPMAILER
27 */
28/*
29 * CREATE TABLE public.parm_mail_server (
30 pe_id int4 GENERATED ALWAYS AS IDENTITY( INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START 1 CACHE 1 NO CYCLE) NOT NULL, -- pk
31 pe_name varchar NULL, -- Config. name
32 pe_parameter text NOT NULL, -- key for json
33 pe_value text NULL, -- value of the key
34 CONSTRAINT param_email_pk PRIMARY KEY (pe_id),
35 CONSTRAINT parm_email_unique UNIQUE (pe_name, pe_parameter)
36 );
37 COMMENT ON TABLE public.parm_mail_server IS 'Parameters for email server';
38
39 -- Column comments
40
41 COMMENT ON COLUMN public.parm_mail_server.pe_id IS 'pk';
42 COMMENT ON COLUMN public.parm_mail_server.pe_name IS 'Config. name';
43 COMMENT ON COLUMN public.parm_mail_server.pe_parameter IS 'key for json';
44 COMMENT ON COLUMN public.parm_mail_server.pe_value IS 'value of the key';
45 */
46
47use PHPMailer\PHPMailer\PHPMailer;
48use PHPMailer\PHPMailer\SMTP;
49
50/**
51 * @class
52 * @brief configuration email and test PHPMAILER.
53 * Property :
54 * - $cn Database conx
55 * - $name name of the SMTP conx
56 * - $a_value array
57 * - smtp_user' username
58 - 'smtp_password' password
59 - 'smtp_host' host
60 - 'smtp_port' port
61 - 'smtp_replyto' email address to reply
62 - 'smtp_from' email address sender
63 - 'smtp_auth' smtp authorisation (always 1)
64 - 'smtp_type' smtp
65 - 'smtp_secure' kind of encryption PHPMailer::ENCRYPTION_STARTTLS, or PHPMailer::ENCRYPTION_SMTPS.
66 - 'smtp_auth_type'SMTP authentication type. Options are CRAM-MD5, LOGIN, PLAIN, XOAUTH2.
67 *
68 */
70{
71
72 const PARAMETER = [
73 'smtp_user'
74 , 'smtp_password'
75 , 'smtp_host'
76 , 'smtp_port'
77 , 'smtp_replyto'
78 , 'smtp_from'
79 , 'smtp_auth'
80 , 'smtp_type'
81 , 'smtp_secure'
82 , 'smtp_auth_type'
83 ];
84
85 protected $a_value; //!< $a_value array of PARAMETER=>Value
86
87// private $char_set; //!< string character set of message
88
89 function __construct(
90 protected \Database $cn
91 , protected $name
92 )
93 {
94
95 $this->load();
96 }
97
98 /**
99 * @brief set the value in the array $a_value thkx the key
100 * @param $key (string) key must be in Mail_Parameter::PARAMETER
101 * @param $value (string) value
102 * @return Mail_Parameter
103 * @throws \Exception if the key doesn't exist
104 */
105 function __set($key, $value)
106 {
107 if (in_array($key, Mail_Parameter::PARAMETER) == false)
108 {
109 throw new \Exception("MP72 : unknown $key");
110 }
111 $this->a_value[$key] = $value;
112 return $this;
113 }
114
115 /**
116 * @brief get the value in the array $a_value thkx the key
117 * @param $key (string) key must be in Mail_Parameter::PARAMETER
118 * @param $value (string) value
119 * @return a string or null if not found
120 * @throws \Exception if the key doesn't exist
121 */
122 function __get($key)
123 {
124 if (in_array($key, Mail_Parameter::PARAMETER) == false)
125 {
126 throw new \Exception("MP79 : unknown $key");
127 }
128 if (isset($this->a_value[$key]))
129 {
130 return $this->a_value[$key];
131 }
132 return null;
133 }
134
135 /**
136 * @brief save into the database
137 */
138 function save()
139 {
140 foreach (self::PARAMETER as $key)
141 {
142 $id = $this->cn->get_value("select pe_id from parm_mail_server
143 where pe_parameter=$1
144 and pe_name=$2",
145 [$key, $this->name]);
146 if ($id == "")
147 {
148 $record = new \Parm_Mail_Server_SQL($this->cn);
149 $record->set("pe_name", $this->name);
150 $record->set("pe_parameter", $key);
151 $record->set("pe_value", $this->a_value[$key]);
152 $record->insert();
153 } else
154 {
155 $record = new \Parm_Mail_Server_SQL($this->cn, $id);
156 $record->set("pe_value", $this->a_value[$key]);
157 $record->update();
158 }
159 }
160 }
161
162 /**
163 * @brief load from the database
164 */
165 function load()
166 {
167 $a_value = array_column(
168 $this->cn->get_array("select pe_parameter , pe_value from
169 public.parm_mail_server
170 where
171 pe_name=$1"
172 , [$this->name])
173 , "pe_value","pe_parameter"
174 );
175
176 foreach (self::PARAMETER as $key)
177 {
178 $this->a_value[$key] = $a_value[$key] ?? "";
179 }
180 }
181
182 function get($key)
183 {
184 return $this->$key;
185 }
186
187 function set($key, $value): Mail_Parameter
188 {
189 $this->$key = $value;
190 return $this;
191 }
192
193 /**
194 * @brief display a form to input the parameter
195 *
196 */
197 function input()
198 {
199
200 include NOALYSS_TEMPLATE . "/mail_parameter-input.php";
201 }
202 /**
203 * @brief retrieve information from GET
204 */
205 function from_get()
206 {
207 $http = new \HttpInput();
208 $this->smtp_user = $http->get('smtp_user');
209 $this->smtp_password = $http->get('smtp_password');
210 $this->smtp_host = $http->get('smtp_host');
211 $this->smtp_port = $http->get('smtp_port');
212 $this->smtp_replyto = $http->get('smtp_replyto');
213 $this->smtp_from = $http->get('smtp_from');
214 $this->smtp_type= $http->get('smtp_type');
215 $this->smtp_secure = $http->get('smtp_secure');
216 $this->smtp_auth_type = $http->get('smtp_auth_type');
217 $this->smtp_auth=1;
218
219 }
220 /**
221 * @brief retrieve information from POST
222 */
223 function from_post()
224 {
225 $http = new \HttpInput();
226 $this->smtp_user = $http->post('smtp_user');
227 $this->smtp_password = $http->post('smtp_password');
228 $this->smtp_host = $http->post('smtp_host');
229 $this->smtp_port = $http->post('smtp_port');
230 $this->smtp_replyto = $http->post('smtp_replyto');
231 $this->smtp_from = $http->post('smtp_from');
232 $this->smtp_type= $http->post('smtp_type');
233 $this->smtp_secure = $http->post('smtp_secure');
234 $this->smtp_auth_type = $http->post('smtp_auth_type');
235 $this->smtp_auth=1;
236
237 }
238 public static function Factory(\Database $cnx,$reply_to="", $blind_copy="")
239 {
240 $mail_parameter=new Mail_Parameter($cnx,MAIL_SETTING_NOALYSS);
241 if ( $mail_parameter->smtp_type=="sendmail") {
242 return new \Sendmail($mail_parameter->smtp_replyto,$mail_parameter->smtp_replyto);
243 } elseif ($mail_parameter->smtp_type=="smtp") {
244 $phpmail= new SMTPMail($mail_parameter);
245 return $phpmail;
246 }
247
248 throw new \Exception("MP212 MAIL NOT CONFIGURED",212);
249
250 }
251}
$input_from cn
$from_poste name
contains the class for connecting to Noalyss
configuration email and test PHPMAILER.
static Factory(\Database $cnx, $reply_to="", $blind_copy="")
__get($key)
get the value in the array $a_value thkx the key
__set($key, $value)
set the value in the array $a_value thkx the key
save()
save into the database
input()
display a form to input the parameter
load()
load from the database
$a_value
$a_value array of PARAMETER=>Value
from_post()
retrieve information from POST
__construct(protected \Database $cn, protected $name)
from_get()
retrieve information from GET
const MAIL_SETTING_NOALYSS
Definition constant.php:393
if( $delta< 0) elseif( $delta==0)