File: NotificationMail.php

package info (click to toggle)
ocsinventory-server 2.5%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,088 kB
  • sloc: php: 27,462; perl: 8,241; sh: 1,680; sql: 1,355; xml: 1,041; makefile: 34
file content (344 lines) | stat: -rw-r--r-- 12,923 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/*
 * Copyright 2005-2016 OCSInventory-NG/OCSInventory-ocsreports contributors.
 * See the Contributors file for more details about them.
 *
 * This file is part of OCSInventory-NG/OCSInventory-ocsreports.
 *
 * OCSInventory-NG/OCSInventory-ocsreports is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 2 of the License,
 * or (at your option) any later version.
 *
 * OCSInventory-NG/OCSInventory-ocsreports is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OCSInventory-NG/OCSInventory-ocsreports. if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

 // Import PHPMailer classes into the global namespace
 // These must be at the top of your script, not inside a function
 use PHPMailer\PHPMailer\PHPMailer;
 use PHPMailer\PHPMailer\Exception;

 //Load libraries
 require PHPMAILER.'/Exception.php';
 require PHPMAILER.'/PHPMailer.php';
 require PHPMAILER.'/SMTP.php';
 require __DIR__.'/../softwares/SoftwareCategory.php';
 require __DIR__.'/../assets/AssetsCategory.php';

 /**
  * Class for the notification mail
  */
 class NotificationMail
 {
      public $info = [];
      public $notif;
      public $div = [];
      private $champs = array('NOTIF_FOLLOW'=>'NOTIF_FOLLOW','NOTIF_MAIL_ADMIN'=>'NOTIF_MAIL_ADMIN','NOTIF_NAME_ADMIN'=>'NOTIF_NAME_ADMIN','NOTIF_MAIL_REPLY'=>'NOTIF_MAIL_REPLY',
                          'NOTIF_NAME_REPLY'=>'NOTIF_NAME_REPLY','NOTIF_SEND_MODE'=>'NOTIF_SEND_MODE','NOTIF_SMTP_HOST'=>'NOTIF_SMTP_HOST',
                          'NOTIF_PORT_SMTP'=>'NOTIF_PORT_SMTP','NOTIF_USER_SMTP'=>'NOTIF_USER_SMTP','NOTIF_PASSWD_SMTP'=>'NOTIF_PASSWD_SMTP',
                          'NOTIF_PROG_TIME'=>'NOTIF_PROG_TIME','NOTIF_PROG_DAY'=>'NOTIF_PROG_DAY'
                        );
      private $week = array('MON' => 'MON', 'TUE' => 'TUE', 'WED' => 'WED', 'THURS' => 'THURS', 'FRI' => 'FRI', 'SAT' => 'SAT', 'SUN' => 'SUN');

      public function __construct($language){
        global $l;
        $l = new language($language);
      }

      /**
       * Get the notification selected
       * @return [type] [description]
       */
      public function get_notif_selected(){
          $sql = "SELECT `FILE` FROM notification WHERE `TYPE`= 'SELECTED'";
          $result = mysqli_query($_SESSION['OCS']["readServer"], $sql);
          $item_notif = mysqli_fetch_array($result);

          return $item_notif['FILE'];
      }

      /**
       * Update the selected notification
       * @param  string $selected [description]
       */
      public function update_perso($selected){
          $sql = "UPDATE notification SET `FILE` = '%s' WHERE `TYPE` = 'SELECTED'";
          $arg_sql = array($selected);
          mysql2_query_secure($sql, $_SESSION['OCS']["writeServer"], $arg_sql);
      }

      /**
       * Insert Info smtp in notification_config
       * @param  array $infos [description]
       * @return
       */
      public function insert_info_smtp($infos){
        $infos['NOTIF_PROG_DAY'] = $this->number_day($infos);
        $sql="SELECT * FROM notification_config";
        $result = mysql2_query_secure($sql, $_SESSION['OCS']["readServer"]);
        while($row = mysqli_fetch_array($result)){
           $verif[$row['NAME']]= $row['NAME'];
        }
        foreach($infos as $key => $value){
          if(array_key_exists($key, $this->champs) && !array_key_exists($key, $verif)){
            $sql = "INSERT INTO `notification_config`(`NAME`, `TVALUE`) VALUES('%s', '%s')";
            $arg = array($key, $value);
            mysql2_query_secure($sql, $_SESSION['OCS']["writeServer"], $arg);
          }elseif(array_key_exists($key, $verif)){
            $sql_update = "UPDATE `notification_config` SET `NAME` = '%s', `TVALUE` = '%s' WHERE `NAME` = '%s' ";
            $arg_update = array($key, $value, $key);
            mysql2_query_secure($sql_update, $_SESSION['OCS']["writeServer"], $arg_update);
          }
        }
      }

      /**
       * Concatenation of day for bdd
       * @param  array $infos [description]
       * @return string        [description]
       */
      private function number_day($infos){
        foreach($infos as $key => $value){
          if(array_key_exists($key, $this->week)){
              $day .= $this->week[$key] . ",";
          }
        }
        return $day;
      }

      /**
       * retrieve info smtp
       * @return [type] [description]
       */
      public function get_info_smtp(){
          $sql = "SELECT * FROM notification_config";
          $result = mysql2_query_secure($sql, $_SESSION['OCS']["readServer"]);
          while($row = mysqli_fetch_array($result)){
              if($row['NAME'] == 'NOTIF_PROG_DAY'){
                $day[] = explode(",", $row['TVALUE']);
                foreach($day as $key => $value){
                  foreach($value as $keys => $values){
                    if($values != ''){
                      $this->info[$values] = $values;
                    }
                  }
                }
              }else{
                $this->info[$row['NAME']]= $row['TVALUE'];
              }
          }
          return $this->info;
      }

      /**
       * configuration of phpmailer object
       * @return phpMailer $this->notif
       */
      public function config_mailer(){
          // Passing `true` enables exceptions
          $this->notif = new PHPMailer(true);
          // Server settings
          // Set mailer to use SMTP
          $this->notif->isSMTP();
          // Specify main and backup SMTP servers
          $this->notif->Host = $this->info['NOTIF_SMTP_HOST'];

          if($this->info['NOTIF_USER_SMTP'] != '' && $this->info['NOTIF_PASSWD_SMTP'] != ''){
              // Enable SMTP authentication
              $this->notif->SMTPAuth = true;
              // SMTP username
              $this->notif->Username = $this->info['NOTIF_USER_SMTP'];
              // SMTP password
              $this->notif->Password = $this->info['NOTIF_PASSWD_SMTP'];
          }else{
              $this->notif->SMTPAuth = false;
          }
          $this->notif->SMTPSecure = $this->info['NOTIF_SEND_MODE'];
          $this->notif->Port = $this->info['NOTIF_PORT_SMTP'];

          // Recipients
          $this->notif->setFrom($this->info['NOTIF_MAIL_ADMIN'], 'OCSInventory');
          $this->notif->addAddress($this->info['NOTIF_MAIL_ADMIN'], $this->info['NOTIF_NAME_ADMIN']);

          if($this->info['NOTIF_MAIL_REPLY'] != '' && $this->info['NOTIF_NAME_REPLY'] != ''){
             $this->notif->addReplyTo($this->info['NOTIF_MAIL_REPLY'], $this->info['NOTIF_NAME_REPLY']);
          }

      }


      /**
       * send notification with phpMailer
       * @return void
       */
     public function send_notification($subject, $body, $altBody = '', $selected, $isHtml = false ){
          $body = $this->replace_value($body, $selected);
          try{
             // Content
             $this->notif->isHTML(false);
             $this->notif->Subject = $subject;
             $this->notif->Body    = $body;
             $this->notif->AltBody = $altBody;

             $this->notif->send();
             error_log('Message has been sent');
         } catch (Exception $e) {
             $msg = 'Message could not be sent. Mailer Error: '. $mail->ErrorInfo;
             error_log($msg);
         }
      }

      /**
       * Replace value in template
       * @param  $_FILES $file [description]
       * @return [type]       [description]
       */
      public function replace_value($file, $selected){
          global $l;
          $soft = new SoftwareCategory();
          $asset = new AssetsCategory();

          $pattern[0] = "{{";
          $pattern[1] = "}}";
          $replacement[0] = "";
          $replacement[1] = "";

          if($selected == 'DEFAULT'){
            $template = file_get_contents(TEMPLATE.'OCS_template.html', true);
          }else{
            $template = file_get_contents($file, true);
          }

          if(strpos($template, "{{") !== false){
            $explode1 = explode("{{", $template);
              foreach($explode1 as $key => $value){
                if(strpos($value, "}}") !== false){
                    $explode2[] = explode("}}", $value);
                }
              }

              foreach ($explode2 as $key => $values){
                foreach ($values as $nb => $trad){
                  if(strpos($trad, "<") === false){
                      $explode3[] = $trad;
                  }
                }
              }

              foreach ($explode3 as $keys => $replace){
                if(strpos($replace, "g") !== false){
                    $traduction = explode(".", $replace);
                    $pattern[] = $replace;
                    $replacement[] = $l->g($traduction[1]);
                }elseif($replace == 'Report.Asset'){
                    $pattern[] = $replace;
                    $asset->get_assets();
                    $replacement[] = $asset->html;
                }elseif($replace == 'Report.Software'){
                    $pattern[] = $replace;
                    $replacement[] = $soft->get_table_html_soft();
                }
              }
              $output = str_replace($pattern, $replacement, $template);

              return $output;
          }else{
              return $template;
          }
      }

      /**
       * Save file in Templates
       * @param  $_FILE $file [description]
       * @return [type]       [description]
       */
      public function upload_file($file, $subject){
          global $l;
          $uploadFile = TEMPLATE . basename($file['template']['name']);

          if($file['template']['type'] == 'text/html'){
            if (move_uploaded_file($_FILES['template']['tmp_name'], $uploadFile)) {
              $sql = "UPDATE `notification` SET FILE='%s', SUBJECT='%s' WHERE TYPE='PERSO'";
              $arg = array(TEMPLATE . basename($file['template']['name']), $subject);
              mysql2_query_secure($sql, $_SESSION['OCS']["writeServer"], $arg);
              msg_success($l->g(8014));
              $this->get_template_perso();
            } else {
              msg_error($l->g(8015));
              return false;
            }
          }else{
            msg_error($l->g(8017));
            return false;
          }
      }

      /**
       * Get directory template perso
       * @return string [directory]
       */
      public function get_template_perso(){
          $sql = "SELECT FILE FROM `notification` WHERE TYPE='PERSO'";
          $result = mysql2_query_secure($sql, $_SESSION['OCS']["readServer"]);
          $row = mysqli_fetch_array($result);

          return $row['FILE'];
      }

      /**
       * [get_all_information description]
       * @param  string $value [description]
       * @return array        [description]
       */
      public function get_all_information($value){
        $sql = "SELECT * FROM `notification` WHERE TYPE='%s'";
        $arg = array($value);
        $result = mysql2_query_secure($sql, $_SESSION['OCS']["readServer"], $arg);
        while ($notif = mysqli_fetch_array($result)) {
            $info[$notif['TYPE']]['SUBJECT'] = $notif['SUBJECT'];
            $info[$notif['TYPE']]['FILE'] = $notif['FILE'];
            $info[$notif['TYPE']]['ALTBODY'] = $notif['ALTBODY'];
        }
        return $info;
      }

      /**
       * html div for form
       * @return [type] [description]
       */
      public function html_div(){
        global $l;
        $champs = array('NOTIF_FOLLOW' => $l->g(8001),
            'NOTIF_MAIL_ADMIN' => $l->g(8002),
            'NOTIF_NAME_ADMIN' => $l->g(8003),
            'NOTIF_MAIL_REPLY' => $l->g(8004),
            'NOTIF_NAME_REPLY' => $l->g(8005),
            'NOTIF_SEND_MODE' => $l->g(8009),
            'NOTIF_SMTP_HOST' => $l->g(8006),
            'NOTIF_PORT_SMTP' => $l->g(279),
            'NOTIF_USER_SMTP' => $l->g(8007),
            'NOTIF_PASSWD_SMTP' => $l->g(8008),
            'NOTIF_PROG_TIME' => '',
            'NOTIF_PROG_DAY' => ''
        );

        foreach ($champs as $key => $value){
          $this->div[$key] = "<hr/>
                        <div class='row config-row'>
                        <div class='col-md-6'>
                        <label for='".$key."'>".$key."</label>
                        <p class='help-block'>".$value."</p>
                        </div>
                        <div class='col-md-4'>
                        <div class='form-group'>";
        }
      }
 }