Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

ryxeo-glpi-git / inc / mailing.class.php @ 1386d344

Historique | Voir | Annoter | Télécharger (37,1 ko)

1
<?php
2
/*
3
 * @version $Id: mailing.class.php 7763 2009-01-06 18:44:50Z moyo $
4
 -------------------------------------------------------------------------
5
 GLPI - Gestionnaire Libre de Parc Informatique
6
 Copyright (C) 2003-2009 by the INDEPNET Development Team.
7

8
 http://indepnet.net/   http://glpi-project.org
9
 -------------------------------------------------------------------------
10

11
 LICENSE
12

13
 This file is part of GLPI.
14

15
 GLPI is free software; you can redistribute it and/or modify
16
 it under the terms of the GNU General Public License as published by
17
 the Free Software Foundation; either version 2 of the License, or
18
 (at your option) any later version.
19

20
 GLPI is distributed in the hope that it will be useful,
21
 but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 GNU General Public License for more details.
24

25
 You should have received a copy of the GNU General Public License
26
 along with GLPI; if not, write to the Free Software
27
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
 --------------------------------------------------------------------------
29
 */
30

    
31
// ----------------------------------------------------------------------
32
// Original Author of file:
33
// Purpose of file:
34
// ----------------------------------------------------------------------
35

    
36
if (!defined('GLPI_ROOT')){
37
        die("Sorry. You can't access directly to this file");
38
        }
39

    
40
require_once(GLPI_ROOT . "/lib/phpmailer/class.phpmailer.php");
41

    
42
/**
43
 *  glpi_phpmailer class extends 
44
 */
45
class glpi_phpmailer extends phpmailer {
46

    
47
        /// Set default variables for all new objects
48
        var $WordWrap = 80;
49
        /// Defaut charset
50
        var $CharSet ="utf-8";
51

    
52

    
53
        /**
54
         * Constructor
55
        **/
56
        function glpi_phpmailer(){
57
                global $CFG_GLPI;
58

    
59
                // Comes from config
60
                $this->SetLanguage("en", GLPI_ROOT . "/lib/phpmailer/language/");
61
                if($CFG_GLPI['smtp_mode'] == '1') {
62
                        $this->Host = $CFG_GLPI['smtp_host'];
63
                        $this->Port = $CFG_GLPI['smtp_port'];
64

    
65
                        if($CFG_GLPI['smtp_username'] != '') {
66
                                $this->SMTPAuth  = true;
67
                                $this->Username  = $CFG_GLPI['smtp_username'];
68
                                $this->Password  =  $CFG_GLPI['smtp_password'];
69
                        }
70

    
71
                        /*if($CFG_GLPI['debug']=="2"){
72
                                $this->SMTPDebug    = TRUE;
73
                        }*/
74

    
75
                        $this->Mailer = "smtp";
76
                }
77

    
78
        } 
79

    
80

    
81

    
82

    
83
}
84

    
85

    
86
/**
87
 *  Mailing class for trackings
88
 */
89
class Mailing
90
{
91
        //! mailing type (new,attrib,followup,finish)
92
        var $type=NULL;
93
        /** Job class variable - job to be mailed
94
         * @see Job
95
         */
96
        var $job=NULL;
97
        /** User class variable - user who make changes
98
         * @see User
99
         */
100
        var $user=NULL;
101
        /// Is the followupadded private ? 
102
        var $followupisprivate=NULL;
103

    
104

    
105
        /**
106
         * Constructor
107
         * @param $type mailing type (new,attrib,followup,finish)
108
         * @param $job Job to mail
109
         * @param $user User who made change
110
         * @param $followupisprivate true if the currently added/modified followup is private
111
         * @return nothing 
112
         */
113
        function Mailing ($type="",$job=NULL,$user=NULL,$followupisprivate=false)
114
        {
115
                $this->type=$type;
116
                if (!isset($job->hardwaredatas)||!count($job->hardwaredatas)){
117
                        $job->getHardwareData();
118
                }
119
                $this->job=$job;
120
                $this->user=$user;
121
                $this->followupisprivate=$followupisprivate;
122
        }
123

    
124
        /**
125
         * Give mails to send the mail
126
         * 
127
         * Determine email to send mail using global config and Mailing type
128
         * @param $sendprivate false : all users; true : only users who have the right to see private followups 
129
         *
130
         * @return array containing email
131
         */
132
        function get_users_to_send_mail($sendprivate=false)
133
        {
134
                global $DB,$CFG_GLPI;
135

    
136
                $emails=array();
137

    
138
                $query="SELECT * FROM glpi_mailing WHERE type='".$this->type."'";
139
                $result=$DB->query($query);
140
                if ($DB->numrows($result)){
141

    
142
                        $select ="";
143
                        $join="";
144
                        $joinprofile="";
145
                        // If send private is the user can see private followups ?
146
                        if ($sendprivate){
147
                                $join=" INNER JOIN glpi_users_profiles 
148
                                        ON (glpi_users_profiles.FK_users = glpi_users.ID 
149
                                                ".getEntitiesRestrictRequest("AND","glpi_users_profiles","FK_entities",$this->job->fields['FK_entities'],true).")
150
                                        INNER JOIN glpi_profiles 
151
                                        ON (glpi_profiles.ID = glpi_users_profiles.FK_profiles AND glpi_profiles.interface='central' AND glpi_profiles.show_full_ticket = '1') ";
152
                                $joinprofile=        "INNER JOIN glpi_profiles 
153
                                        ON (glpi_profiles.ID = glpi_users_profiles.FK_profiles AND glpi_profiles.interface='central' AND glpi_profiles.show_full_ticket = '1') ";
154

    
155
                        }
156

    
157
                        while ($data=$DB->fetch_assoc($result)){
158
                                switch ($data["item_type"]){
159
                                        case USER_MAILING_TYPE :
160
                                                switch($data["FK_item"]){
161
                                                        // ADMIN SEND
162
                                                        case ADMIN_MAILING :
163
                                                                if (isValidEmail($CFG_GLPI["admin_email"])&&!isset($emails[$CFG_GLPI["admin_email"]])){
164
                                                                        $emails[$CFG_GLPI["admin_email"]]=$CFG_GLPI["default_language"];
165
                                                                }
166
                                                                break;
167
                                                        // ADMIN ENTITY SEND
168
                                                        case ADMIN_ENTITY_MAILING :
169
                                                                $query2 = "SELECT admin_email AS EMAIL 
170
                                                                        FROM glpi_entities_data 
171
                                                                        WHERE (FK_entities = '".$this->job->fields["FK_entities"]."')";
172
                                                                if ($result2 = $DB->query($query2)) {
173
                                                                        if ($DB->numrows($result2)==1){
174
                                                                                $row = $DB->fetch_array($result2);
175
                                                                                if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){
176
                                                                                        $emails[$row['EMAIL']]=$CFG_GLPI["default_language"];
177
                                                                                }
178
                                                                        }
179
                                                                }
180
                                                                break;
181
                                                        // ASSIGN SEND
182
                                                        case ASSIGN_MAILING :
183
                                                                if (isset($this->job->fields["assign"])&&$this->job->fields["assign"]>0){
184
                                                                        $query2 = "SELECT DISTINCT glpi_users.email AS EMAIL, glpi_users.language AS LANG 
185
                                                                                FROM glpi_users $join 
186
                                                                                WHERE (glpi_users.ID = '".$this->job->fields["assign"]."')";
187
                                                                        if ($result2 = $DB->query($query2)) {
188
                                                                                if ($DB->numrows($result2)==1){
189
                                                                                        $row = $DB->fetch_array($result2);
190
                                                                                        if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){
191
                                                                                                $emails[$row['EMAIL']]=$row['LANG'];
192
                                                                                        }
193
                                                                                }
194
                                                                        }
195
                                                                }
196
                                                                break;
197
                                                        // ASSIGN SEND
198
                                                        case ASSIGN_ENT_MAILING :
199
                                                                
200
                                                                if (!$sendprivate&&isset($this->job->fields["assign_ent"])&&$this->job->fields["assign_ent"]>0){
201
                                                                        $query2 = "SELECT DISTINCT glpi_enterprises.email AS EMAIL 
202
                                                                        FROM glpi_enterprises 
203
                                                                        WHERE (glpi_enterprises.ID = '".$this->job->fields["assign_ent"]."')";
204
                                                                        if ($result2 = $DB->query($query2)) {
205
                                                                                if ($DB->numrows($result2)==1){
206
                                                                                        $row = $DB->fetch_array($result2);
207
                                                                                        if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){
208
                                                                                                $emails[$row['EMAIL']]=$CFG_GLPI["default_language"];
209
                                                                                        }
210
                                                                                }
211
                                                                        }
212
                                                                }
213
                                                                break;
214
                                                        // ASSIGN GROUP SEND
215
                                                        case ASSIGN_GROUP_MAILING :
216
                                                                if (isset($this->job->fields["assign_group"])&&$this->job->fields["assign_group"]>0){
217
                                                                        $query="SELECT glpi_users.email AS EMAIL, glpi_users.language AS LANG 
218
                                                                        FROM glpi_users_groups 
219
                                                                        INNER JOIN glpi_users ON (glpi_users_groups.FK_users = glpi_users.ID) $join 
220
                                                                        WHERE glpi_users.deleted=0 AND glpi_users_groups.FK_groups='".$this->job->fields["assign_group"]."'";
221
                                
222
                                                                        if ($result2= $DB->query($query)){
223
                                                                                if ($DB->numrows($result2)){
224
                                                                                        while ($row=$DB->fetch_assoc($result2)){
225
                                                                                                if (isValidEmail($row["EMAIL"])&&!isset($emails[$row['EMAIL']])){
226
                                                                                                        $emails[$row['EMAIL']]=$row['LANG'];
227
                                                                                                }
228
                                                                                        }
229
                                                                                }
230
                                                                        }
231
                                                                }
232
                                                                break;
233
                                                        // SUPERVISOR ASSIGN GROUP SEND
234
                                                        case SUPERVISOR_ASSIGN_GROUP_MAILING :
235
                                                                if (isset($this->job->fields["assign_group"])&&$this->job->fields["assign_group"]>0){
236
                                                                        $query2 = "SELECT DISTINCT glpi_users.email AS EMAIL, glpi_users.language AS LANG 
237
                                                                        FROM glpi_groups 
238
                                                                        LEFT JOIN glpi_users ON (glpi_users.ID = glpi_groups.FK_users) $join 
239
                                                                        WHERE (glpi_groups.ID = '".$this->job->fields["assign_group"]."')";
240
                                                                        if ($result2 = $DB->query($query2)) {
241
                                                                                if ($DB->numrows($result2)==1){
242
                                                                                        $row = $DB->fetch_array($result2);
243
                                                                                        if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){
244
                                                                                                $emails[$row['EMAIL']]=$row['LANG'];
245
                                                                                        }
246
                                                                                }
247
                                                                        }
248
                                                                }
249
                                                                break;
250

    
251

    
252
                                                        // RECIPIENT SEND
253
                                                        case RECIPIENT_MAILING :
254
                                                                if (isset($this->job->fields["recipient"])&&$this->job->fields["recipient"]>0){
255
                                                                        $query2 = "SELECT DISTINCT glpi_users.email AS EMAIL, glpi_users.language AS LANG  
256
                                                                                FROM glpi_users $join 
257
                                                                                WHERE (glpi_users.ID = '".$this->job->fields["recipient"]."')";
258
                                                                        if ($result2 = $DB->query($query2)) {
259
                                                                                if ($DB->numrows($result2)==1){
260
                                                                                        $row = $DB->fetch_array($result2);
261
                                                                                        if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){
262
                                                                                                $emails[$row['EMAIL']]=$row['LANG'];
263
                                                                                        }
264
                                                                                }
265
                                                                        }
266
                                                                }
267
                                                                break;
268

    
269
                                                        // AUTHOR SEND
270
                                                        case AUTHOR_MAILING :
271
                                                                if ($this->job->fields["emailupdates"]&&isValidEmail($this->job->fields["uemail"])&&!isset($emails[$this->job->fields["uemail"]])){
272
                                                                        // Uemail = mail of the author ? -> use right of the author to see private followups
273
                                                                        // Else not see private
274
                                                                        $authorsend=false;
275
                                                                        $authorlang=$CFG_GLPI["default_language"];
276
                                                                        if (!$sendprivate){
277
                                                                                $authorsend=true;
278
                                                                        } 
279

    
280
                                                                        // Is the user have the same mail that uemail ?
281
                                                                        $query2 = "SELECT DISTINCT glpi_users.email AS EMAIL, glpi_users.language AS LANG   
282
                                                                        FROM glpi_users $join 
283
                                                                        WHERE (glpi_users.ID = '".$this->job->fields["author"]."')";
284
                                                                        if ($result2 = $DB->query($query2)) {
285
                                                                                if ($DB->numrows($result2)==1){
286
                                                                                        $row = $DB->fetch_array($result2);
287
                                                                                        if ($row['EMAIL']==$this->job->fields["uemail"]){
288
                                                                                                $authorsend=true;
289
                                                                                                $authorlang=$row['LANG'];
290
                                                                                        }
291
                                                                                }
292
                                                                        }
293

    
294
                                                                        if ($authorsend){
295
                                                                                $emails[$this->job->fields["uemail"]]=$authorlang;
296
                                                                        }
297
                                                                }
298
                                                                break;
299
                                                        // SUPERVISOR ASSIGN GROUP SEND
300
                                                        case SUPERVISOR_AUTHOR_GROUP_MAILING :
301
                                                                if (isset($this->job->fields["FK_group"])&&$this->job->fields["FK_group"]>0){
302
                                                                        $query2 = "SELECT DISTINCT glpi_users.email AS EMAIL, glpi_users.language AS LANG 
303
                                                                                FROM glpi_groups 
304
                                                                                LEFT JOIN glpi_users ON (glpi_users.ID = glpi_groups.FK_users) $join 
305
                                                                                WHERE (glpi_groups.ID = '".$this->job->fields["FK_group"]."')";
306
                                                                        if ($result2 = $DB->query($query2)) {
307
                                                                                if ($DB->numrows($result2)==1){
308
                                                                                        $row = $DB->fetch_array($result2);
309
                                                                                        if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){
310
                                                                                                $emails[$row['EMAIL']]=$row['LANG'];
311
                                                                                        }
312
                                                                                }
313
                                                                        }
314
                                                                }
315
                                                                break;
316

    
317
                                                        // OLD ASSIGN SEND
318
                                                        case OLD_ASSIGN_MAILING :
319
                                                                if (isset($this->job->fields["_old_assign"])&&$this->job->fields["_old_assign"]>0){
320
                                                                        $query2 = "SELECT DISTINCT glpi_users.email AS EMAIL, glpi_users.language AS LANG  
321
                                                                                FROM glpi_users $join 
322
                                                                                WHERE (glpi_users.ID = '".$this->job->fields["_old_assign"]."')";
323
                                                                        if ($result2 = $DB->query($query2)) {
324
                                                                                if ($DB->numrows($result2)==1){
325
                                                                                        $row = $DB->fetch_array($result2);
326
                                                                                        if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){
327
                                                                                                $emails[$row['EMAIL']]=$row['LANG'];
328
                                                                                        }
329
                                                                                }
330
                                                                        }
331
                                                                }
332
                                                                break;
333
                                                        // TECH SEND
334
                                                        case TECH_MAILING :
335
                                                                if (isset($this->job->fields["computer"])&&$this->job->fields["computer"]>0&&isset($this->job->fields["device_type"])&&$this->job->fields["device_type"]>0){
336
                                                                        $ci= new CommonItem();
337
                                                                        $ci->getFromDB($this->job->fields["device_type"],$this->job->fields["computer"]);
338
                                                                        if ($tmp=$ci->getField('tech_num')){
339
                                                                                $query2 = "SELECT DISTINCT glpi_users.email AS EMAIL, glpi_users.language AS LANG 
340
                                                                                        FROM glpi_users $join 
341
                                                                                        WHERE (glpi_users.ID = '".$tmp."')";
342
                                                                                if ($result2 = $DB->query($query2)) {
343
                                                                                        if ($DB->numrows($result2)==1){
344
                                                                                                $row = $DB->fetch_array($result2);
345
                                                                                                if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){
346
                                                                                                        $emails[$row['EMAIL']]=$row['LANG'];
347
                                                                                                }
348
                                                                                        }
349
                                                                                }
350
                                                                        }
351
                                                                }
352
                                                                break;
353
                                                        // USER SEND
354
                                                        case USER_MAILING :
355
                                                                if (isset($this->job->fields["computer"])&&$this->job->fields["computer"]>0&&isset($this->job->fields["device_type"])&&$this->job->fields["device_type"]>0){
356
                                                                        $ci= new CommonItem();
357
                                                                        $ci->getFromDB($this->job->fields["device_type"],$this->job->fields["computer"]);
358
                                                                        if ($tmp=$ci->getField('FK_users')){
359
                                                                                $query2 = "SELECT DISTINCT glpi_users.email AS EMAIL, glpi_users.language AS LANG 
360
                                                                                        FROM glpi_users $join 
361
                                                                                        WHERE (glpi_users.ID = '".$tmp."')";
362
                                                                                if ($result2 = $DB->query($query2)) {
363
                                                                                        if ($DB->numrows($result2)==1){
364
                                                                                                $row = $DB->fetch_array($result2);
365
                                                                                                if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){
366
                                                                                                        $emails[$row['EMAIL']]=$row['LANG'];
367
                                                                                                }
368
                                                                                        }
369
                                                                                }
370
                                                                        }
371
                                                                }
372
                                                                break;
373
                                                                
374
                                                }
375
                                                break;
376
                                        case PROFILE_MAILING_TYPE :
377
                                                $query="SELECT glpi_users.email AS EMAIL, glpi_users.language AS LANG 
378
                                                FROM glpi_users_profiles 
379
                                                INNER JOIN glpi_users ON (glpi_users_profiles.FK_users = glpi_users.ID) $joinprofile 
380
                                                WHERE glpi_users.deleted=0 AND glpi_users_profiles.FK_profiles='".$data["FK_item"]."' ".
381
                                                getEntitiesRestrictRequest("AND","glpi_users_profiles","FK_entities",$this->job->fields['FK_entities'],true);
382

    
383
                                                if ($result2= $DB->query($query)){
384
                                                        if ($DB->numrows($result2))
385
                                                                while ($row=$DB->fetch_assoc($result2)){
386
                                                                        if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){
387
                                                                                $emails[$row['EMAIL']]=$row['LANG'];
388
                                                                        }
389
                                                                }
390
                                                }
391
                                                break;
392
                                        case GROUP_MAILING_TYPE :
393
                                                $query="SELECT glpi_users.email AS EMAIL, glpi_users.language AS LANG 
394
                                                        FROM glpi_users_groups 
395
                                                        INNER JOIN glpi_users ON (glpi_users_groups.FK_users = glpi_users.ID) $join 
396
                                                        WHERE glpi_users.deleted=0 AND glpi_users_groups.FK_groups='".$data["FK_item"]."'";
397

    
398
                                                if ($result2= $DB->query($query)){
399
                                                        if ($DB->numrows($result2))
400
                                                                while ($row=$DB->fetch_assoc($result2)){
401
                                                                        if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){
402
                                                                                $emails[$row['EMAIL']]=$row['LANG'];
403
                                                                        }
404
                                                                }
405
                                                }
406
                                                break;
407
                                }
408
                        }
409
                }
410

    
411
                return $emails;
412
        }
413

    
414
        /**
415
         * Format the mail body to send
416
        * @param $format text or html
417
         * @param $sendprivate true if the email contains private followups
418
         * @return mail body string
419
         */
420
        function get_mail_body($format="text", $sendprivate=false)
421
        {
422
                global $CFG_GLPI, $LANG;
423

    
424
                // Create message body from Job and type
425
                $body="";
426

    
427
                if($format=="html"){
428
                        if ($CFG_GLPI["url_in_mail"]&&!empty($CFG_GLPI["url_base"])){
429
                          //ryxeo
430
                          //$body.="Pour répondre à cet échange, veuillez vous connecter sur la plate forme suivante :<a href=\"".$CFG_GLPI["url_base"]."/index.php?redirect=tracking_".$this->job->fields["ID"]."\">".$CFG_GLPI["url_base"]."/index.php?redirect=tracking_".$this->job->fields["ID"]." </a><br><br>";
431
                          $body.="Pour répondre à cet échange, veuillez vous connecter sur la plate forme suivante :<a href=\"".$CFG_GLPI["url_base"]."\">".$CFG_GLPI["url_base"]." </a><br><br>";
432

    
433
                        }
434

    
435
                        $body.=$this->job->textDescription($format);
436
                        $body.=$this->job->textFollowups($format, $sendprivate);
437

    
438
                        $body.="<br>-- <br>".$CFG_GLPI["mailing_signature"];
439
                        $body.="</body></html>";
440
                        $body=ereg_replace("\n","<br>\n",$body);
441

    
442
                }else{ // text format
443

    
444
                        if ($CFG_GLPI["url_in_mail"]&&!empty($CFG_GLPI["url_base"])){
445
                          //ryxeo        
446
                          //                          $body.=$LANG["mailing"][1]."\n"; $body.="Pour répondre à cet échange, veuillez vous connecter sur la plate forme suivante : : ".$CFG_GLPI["url_base"]."/index.php?redirect=tracking_".$this->job->fields["ID"]."\n";
447
                          $body.=$LANG["mailing"][1]."\n"; $body.="Pour répondre à cet échange, veuillez vous connecter sur la plate forme suivante : ".$CFG_GLPI["url_base"]."\n";
448

    
449
                        }
450

    
451
                        $body.=$this->job->textDescription($format);
452
                        $body.=$this->job->textFollowups($format, $sendprivate);
453

    
454
                        $body.="\n-- \n".$CFG_GLPI["mailing_signature"];
455
                        $body=ereg_replace("<br />","\n",$body);
456
                        $body=ereg_replace("<br>","\n",$body);
457
                }
458

    
459
                return $body;
460
        }
461

    
462
        /**
463
         * Format the mail sender to send
464
         * @return mail sender email string
465
         */
466
        function get_mail_sender(){
467
                global $CFG_GLPI,$DB;
468

    
469
                $query = "SELECT admin_email AS EMAIL FROM glpi_entities_data WHERE (FK_entities = '".$this->job->fields["FK_entities"]."')";
470
                if ($result=$DB->query($query)){
471
                        if ($DB->numrows($result)){
472
                                $data=$DB->fetch_assoc($result);
473
                                if (isValidEmail($data["EMAIL"])){
474
                                        return $data["EMAIL"];
475
                                }
476
                        }
477
                }
478

    
479
                return $CFG_GLPI["admin_email"];
480
        }
481

    
482
        /**
483
         * Format the mail subject to send
484
         * @return mail subject string
485
         */
486
        function get_mail_subject()
487
        {
488
                global $LANG;
489

    
490
                // Create the message subject 
491
                $subject=sprintf("%s%07d%s","[SUPPORT RYXEO #",$this->job->fields["ID"],"] ");
492

    
493
                if (isMultiEntitiesMode()){
494
                  //preg_replace pour virer toutes les entites parentes
495
                  $pattern = '/.*>/';
496
                  $replacement = '';
497
                  $subject .= preg_replace($pattern,$replacement, getDropdownName("glpi_entities",$this->job->fields['FK_entities'])) . " | ";
498
                }
499

    
500
                switch ($this->type){
501
                        case "new":
502
                                $subject.=$LANG["mailing"][9];
503
                        break;
504
                        case "attrib":
505
                                $subject.=$LANG["mailing"][12];
506
                        break;
507
                        case "followup":
508
                                $subject.=$LANG["mailing"][10];
509
                        break;
510
                        case "update":
511
                                $subject.=$LANG["mailing"][30];
512
                        break;
513
                        case "finish":
514
                                $subject.=$LANG["mailing"][11]." ".convDateTime($this->job->fields["closedate"]);                        
515
                        break;
516
                        default :
517
                        $subject.=$LANG["mailing"][13];
518
                        break;
519
                }
520
                
521
                if (strlen($this->job->fields['name'])>150){
522
                        $subject.=" - ".utf8_substr($this->job->fields['name'],0,150)." (...)";
523
                }else{
524
                        $subject.=" - ".$this->job->fields['name'];
525
                }
526
        
527
                return $subject;
528
        }
529

    
530
        /**
531
         * Get reply to address 
532
         * @param $sender sender address
533
         * @return return mail
534
         */
535
        function get_reply_to_address ($sender){
536
                global $CFG_GLPI,$DB;
537

    
538
                $replyto=$CFG_GLPI["admin_email"];
539

    
540
                // Entity  conf
541
                $query = "SELECT admin_email AS EMAIL, admin_reply AS REPLY FROM glpi_entities_data WHERE (FK_entities = '".$this->job->fields["FK_entities"]."')";
542
                if ($result=$DB->query($query)){
543
                        if ($DB->numrows($result)){
544
                                $data=$DB->fetch_assoc($result);
545
                                if (isValidEmail($data["REPLY"])){
546
                                        return $data["REPLY"];
547
                                } else if (isValidEmail($data["EMAIL"])){
548
                                        $replyto=$data["EMAIL"];
549
                                } 
550
                        }
551
                }
552
                // Global conf
553
                if (isValidEmail($CFG_GLPI["admin_reply"])){
554
                        return $CFG_GLPI["admin_reply"];
555
                }
556

    
557
                // No specific config
558
                switch ($this->type){
559
                        case "new":
560
                                if (isset($this->job->fields["uemail"]) && isValidEmail($this->job->fields["uemail"])) {
561
                                        $replyto=$this->job->fields["uemail"];
562
                                } else {
563
                                        $replyto=$sender;
564
                                }
565
                                break;
566
                        case "followup":
567
                        case "update":
568
                                if (isset($this->user->fields["email"]) && isValidEmail($this->user->fields["email"])) {
569
                                        $replyto=$this->user->fields["email"];
570
                                } else {
571
                                        $replyto=$sender;
572
                                }
573
                        break;
574
                }
575
                return $replyto;                
576
        }
577
        /**
578
         * Send mail function
579
         *
580
         * Construct email and send it
581
         *
582
         * @return mail subject string
583
         */
584
        function send()
585
        {
586
                global $CFG_GLPI,$LANG;
587
                if ($CFG_GLPI["mailing"])
588
                {        
589
                        if (!is_null($this->job)&&in_array($this->type,array("new","update","followup","finish")))
590
                        {
591
                                $senderror=false;
592
                                // get users to send mail
593
                                $users=array();
594
                                // All users
595
                                $users[0]=$this->get_users_to_send_mail(0);
596
                                // Users who could see private followups
597
                                $users[1]=$this->get_users_to_send_mail(1);
598
                                // Delete users who can see private followups to all users list
599
                                foreach ($users[1] as $email => $lang){
600
                                        if (isset($users[0][$email])){
601
                                                unset($users[0][$email]);
602
                                        }
603
                                }
604

    
605
                                // New Followup is private : do not send to common users
606
                                if ($this->followupisprivate){
607
                                        unset($users[0]);
608
                                }
609

    
610
                                $subjects=array();
611
                                // get sender
612
                                $sender= $this->get_mail_sender(); 
613
                                // get reply-to address : user->email ou job_email if not set OK
614
                                $replyto=$this->get_reply_to_address ($sender);
615

    
616
                                $messageerror=$LANG["mailing"][47];
617
                                // Send all mails
618
                                foreach ($users as $private=>$someusers) {
619
                                        if (count($someusers)){
620

    
621
                                                $mmail=new glpi_phpmailer();
622
                                                $mmail->From=$sender;
623
                                                $mmail->AddReplyTo("$replyto", ''); 
624
                                                $mmail->FromName=$sender;
625
                                                $mmail->isHTML(true);
626
                                                
627
                                                $bodys=array();
628
                                                $altbodys=array();
629
                                                foreach ($someusers as $email => $lang){
630
                                                        if (!isset($subjects[$lang])||!isset($bodys[$lang])||!isset($altbodys[$lang])){
631
                                                                loadLanguage($lang);
632
                                                                if (!isset($subjects[$lang])){
633
                                                                        $subjects[$lang]=$this->get_mail_subject();
634
                                                                }
635
                                                                $bodys[$lang]=$this->get_mail_body("html",$private);
636
                                                                $altbodys[$lang]=$this->get_mail_body("text",$private);
637
                                                        }
638
                                                        $mmail->Subject=$subjects[$lang];
639
                                                        $mmail->Body=$bodys[$lang];
640
                                                        $mmail->AltBody=$altbodys[$lang];
641

    
642
                                                        $mmail->AddAddress($email, "");
643

    
644
                                                        if(!$mmail->Send()){
645
                                                                $senderror=true;
646
                                                                addMessageAfterRedirect($messageerror."<br>".$mmail->ErrorInfo);
647
                                                        }else{
648
                                                                logInFile("mail",$LANG["tracking"][38]." ".$email.": ".$subjects[$lang]."\n");
649
                                                        } 
650

    
651
                                                        $mmail->ClearAddresses(); 
652
                                                }
653
                                        }
654
                                }
655
                                // Reinit language
656
                                loadLanguage();
657
                                if ($senderror){
658
                                        return false;
659
                                }
660
                        } else {
661
                                addMessageAfterRedirect($LANG["mailing"][112]);
662
                        }
663
                }
664
                return true;
665
        }
666
}
667

    
668
/**
669
 *  Mailing class for reservations
670
 */
671
class MailingResa{
672
        /** ReservationResa class variable
673
         * @see ReservationResa
674
         */
675
        var $resa;        
676
        //! type of mailing (new, update, delete)
677
        var $type;
678

    
679
        /**
680
         * Constructor
681
         * @param $type mailing type (new,attrib,followup,finish)
682
         * @param $resa ReservationResa to mail
683
         * @return nothing 
684
         */
685
        function MailingResa ($resa,$type="new")
686
        {
687
                $this->resa=$resa;
688
                $this->type=$type;
689

    
690
        }
691

    
692
        /**
693
         * Give mails to send the mail
694
         * 
695
         * Determine email to send mail using global config and Mailing type
696
         *
697
         * @return array containing email
698
         */
699
        function get_users_to_send_mail()
700
        {
701
                global $DB,$CFG_GLPI;
702

    
703
                $emails=array();
704

    
705
                $query="SELECT * FROM glpi_mailing WHERE type='resa'";
706
                $result=$DB->query($query);
707
                if ($DB->numrows($result)){
708
                        while ($data=$DB->fetch_assoc($result)){
709
                                switch ($data["item_type"]){
710
                                        case USER_MAILING_TYPE :
711
                                                switch ($data["FK_item"]){
712
                                                        // ADMIN SEND
713
                                                        case ADMIN_MAILING :
714
                                                                if (isValidEmail($CFG_GLPI["admin_email"])&&!isset($emails[$CFG_GLPI["admin_email"]]))
715
                                                                        $emails[$CFG_GLPI["admin_email"]]=$CFG_GLPI["default_language"];
716
                                                                break;
717
                                                        // ADMIN ENTITY SEND
718
                                                        case ADMIN_ENTITY_MAILING :
719

    
720
                                                                $ri=new ReservationItem();
721
                                                                $ci=new CommonItem();
722
                                                                $entity=-1;
723
                                                                if ($ri->getFromDB($this->resa->fields["id_item"])){
724
                                                                        if ($ci->getFromDB($ri->fields['device_type'],$ri->fields['id_device'])        ){
725
                                                                                $entity=$ci->getField('FK_entities');
726
                                                                        }
727
                                                                }
728
                                                                
729
                                                                if ($entity>=0){
730
                                                                        $query2 = "SELECT admin_email AS EMAIL FROM glpi_entities_data WHERE (FK_entities = '".$entity."')";
731
                                                                        if ($result2 = $DB->query($query2)) {
732
                                                                                if ($DB->numrows($result2)==1){
733
                                                                                        $row = $DB->fetch_array($result2);
734
                                                                                        if (isValidEmail($CFG_GLPI["admin_email"])&&!isset($emails[$CFG_GLPI["admin_email"]])){
735
                                                                                                $emails[$row['EMAIL']]=$CFG_GLPI["default_language"];
736
                                                                                        }
737
                                                                                }
738
                                                                        }
739
                                                                }
740
                                                                break;
741
                                                        // AUTHOR SEND
742
                                                        case AUTHOR_MAILING :
743
                                                                $user = new User;
744
                                                                if ($user->getFromDB($this->resa->fields["id_user"]))
745
                                                                        if (isValidEmail($user->fields["email"])&&!isset($emails[$user->fields["email"]])){
746
                                                                                $emails[$user->fields["email"]]=$user->fields['language'];
747
                                                                        }
748
                                                                break;
749
                                                        // TECH SEND
750
                                                        case TECH_MAILING :
751
                                                                $ri=new ReservationItem();
752
                                                                if ($ri->getFromDB($this->resa->fields["id_item"])){
753
                                                                        $ci=new CommonItem();
754
                                                                        $ci->getFromDB($ri->fields["device_type"],$ri->fields["id_device"]);
755

    
756
                                                                        if ($tmp=$ci->getField('tech_num')){
757
                                                                                $query2 = "SELECT glpi_users.email as EMAIL, glpi_users.language as LANG 
758
                                                                                FROM glpi_users WHERE (glpi_users.ID = '".$tmp."')";
759
                                                                                if ($result2 = $DB->query($query2)) {
760
                                                                                        if ($DB->numrows($result2)==1){
761
                                                                                                $row = $DB->fetch_row($result2);
762
                                                                                                if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){
763
                                                                                                        $emails[$row['EMAIL']]=$row['LANG'];
764
                                                                                                }
765
                                                                                        }
766
                                                                                }
767
                                                                        }
768
                                                                }
769
                                                                break;
770
                                                        // USER SEND
771
                                                        case USER_MAILING :
772
                                                                $ri=new ReservationItem();
773
                                                                if ($ri->getFromDB($this->resa->fields["id_item"])){
774
                                                                        $ci=new CommonItem();
775
                                                                        $ci->getFromDB($ri->fields["device_type"],$ri->fields["id_device"]);
776

    
777
                                                                        if ($tmp=$ci->getField('FK_users')){
778
                                                                                $query2 = "SELECT glpi_users.email AS EMAIL, glpi_users.language as LANG 
779
                                                                                FROM glpi_users WHERE (glpi_users.ID = '".$tmp."')";
780
                                                                                if ($result2 = $DB->query($query2)) {
781
                                                                                        if ($DB->numrows($result2)==1){
782
                                                                                                $row = $DB->fetch_row($result2);
783
                                                                                                if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){
784
                                                                                                        $emails[$row['EMAIL']]=$row['LANG'];
785
                                                                                                }
786
                                                                                        }
787
                                                                                }
788
                                                                        }
789
                                                                }
790
                                                                break;                                                                
791

    
792
                                                }
793
                                                break;
794
                                        case PROFILE_MAILING_TYPE :
795
                                                // Get entity
796
                                                $ri=new ReservationItem();
797
                                                $ri->getFromDB($this->resa->fields['id_item']);
798
                                                $ci = new CommonItem();
799
                                                $ci->getFromDB($ri->fields['device_type'],$ri->fields['id_device']);
800
                                                $FK_entities=$ci->getField('FK_entities');
801
                                                $query="SELECT glpi_users.email AS EMAIL, glpi_users.language as LANG 
802
                                                        FROM glpi_users_profiles 
803
                                                        INNER JOIN glpi_users ON (glpi_users_profiles.FK_users = glpi_users.ID) 
804
                                                        WHERE glpi_users_profiles.FK_profiles='".$data["FK_item"]."' 
805
                                                        ".getEntitiesRestrictRequest("AND","glpi_users_profiles","FK_entities",$FK_entities,true);
806
                                                if ($result2= $DB->query($query)){
807
                                                        if ($DB->numrows($result2))
808
                                                                while ($row=$DB->fetch_assoc($result2)){
809
                                                                        if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){
810
                                                                                $emails[$row['EMAIL']]=$row['LANG'];
811
                                                                        }
812
                                                        }
813
                                                }
814
                                                break;
815
                                        case GROUP_MAILING_TYPE :
816
                                                $query="SELECT glpi_users.email AS EMAIL, glpi_users.language as LANG  
817
                                                FROM glpi_users_groups 
818
                                                INNER JOIN glpi_users ON (glpi_users_groups.FK_users = glpi_users.ID) 
819
                                                WHERE glpi_users_groups.FK_groups='".$data["FK_item"]."'";
820
                                                if ($result2= $DB->query($query)){
821
                                                        if ($DB->numrows($result2))
822
                                                                while ($row=$DB->fetch_assoc($result2)){
823
                                                                        if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){
824
                                                                                $emails[$row['EMAIL']]=$row['LANG'];
825
                                                                        }
826
                                                                }
827
                                                }
828
                                                break;
829
                                }
830
                        }
831
                }
832

    
833
                return $emails;
834
        }
835

    
836

    
837
        /**
838
         * Format the mail sender to send
839
         * @return mail sender email string
840
         */
841
        function get_mail_sender(){
842
                global $CFG_GLPI,$DB;
843

    
844

    
845
                $ri=new ReservationItem();
846
                $ci=new CommonItem();
847
                $entity=-1;
848
                if ($ri->getFromDB($this->resa->fields["id_item"])){
849
                        if ($ci->getFromDB($ri->fields['device_type'],$ri->fields['id_device'])        ){
850
                                $entity=$ci->getField('FK_entities');
851
                        }
852
                }
853
                if ($entity>=0){
854
                        $query = "SELECT admin_email AS EMAIL FROM glpi_entities_data WHERE (FK_entities = '$entity')";
855
                        if ($result=$DB->query($query)){
856
                                if ($DB->numrows($result)){
857
                                        $data=$DB->fetch_assoc($result);
858
                                        if (isValidEmail($data["EMAIL"])){
859
                                                return $data["EMAIL"];
860
                                        }
861
                                }
862
                        }
863
                }
864

    
865
                return $CFG_GLPI["admin_email"];
866
        }
867

    
868

    
869
        /**
870
         * Format the mail body to send
871
        * @param $format text or html
872
         * @return mail body string
873
         */
874
        function get_mail_body($format="text"){
875
                global $CFG_GLPI;
876

    
877
                // Create message body from Job and type
878
                $body="";
879

    
880
                if($format=="html"){
881

    
882
                        $body.=$this->resa->textDescription("html");
883
                        $body.="<br>-- <br>".$CFG_GLPI["mailing_signature"];
884
                        $body.="</body></html>";
885
                        $body=ereg_replace("\n","<br>",$body);
886
                }else{ // text format
887

    
888
                        $body.=$this->resa->textDescription();
889
                        $body.="\n-- \n".$CFG_GLPI["mailing_signature"];
890
                        $body=ereg_replace("<br />","\n",$body);
891
                        $body=ereg_replace("<br>","\n",$body);
892
                }
893
                return $body;
894
        }
895
        /**
896
         * Format the mail subject to send
897
         * @return mail subject string
898
         */
899
        function get_mail_subject()
900
        {
901
                global $LANG;
902

    
903
                // Create the message subject 
904
                if ($this->type=="new")
905
                        $subject="[SUPPORT RYXEO] ".$LANG["mailing"][19];
906
                else if ($this->type=="update") $subject="[SUPPORT RYXEO] ".$LANG["mailing"][23];
907
                else if ($this->type=="delete") $subject="[SUPPORT RYXEO] ".$LANG["mailing"][29];
908

    
909
                return $subject;
910
        }
911

    
912
        /**
913
         * Get reply to address 
914
         * @param $sender sender address
915
         * @return return mail
916
         */
917
        function get_reply_to_address ($sender){
918
                global $CFG_GLPI;
919
                $replyto="";
920

    
921
                $user = new User;
922
                if ($user->getFromDB($this->resa->fields["id_user"])){
923
                        if (isValidEmail($user->fields["email"])) $replyto=$user->fields["email"];                
924
                        else $replyto=$sender;
925
                }
926
                else $replyto=$sender;                
927

    
928
                return $replyto;                
929
        }
930
        /**
931
         * Send mail function
932
         *
933
         * Construct email and send it
934
         *
935
         * @return mail subject string
936
         */
937
        function send()
938
        {
939
                global $CFG_GLPI,$LANG;
940
                if ($CFG_GLPI["mailing"]&&isValidEmail($CFG_GLPI["admin_email"]))
941
                {
942
                        // get users to send mail
943
                        $users=$this->get_users_to_send_mail();
944

    
945
                        // get sender
946
                        $sender= $this->get_mail_sender(); 
947
                        // get reply-to address : user->email ou job_email if not set OK
948
                        $replyto=$this->get_reply_to_address ($sender);
949

    
950

    
951
                        $mmail=new glpi_phpmailer();
952
                        $mmail->From=$sender;
953
                        $mmail->AddReplyTo("$replyto", ''); 
954
                        $mmail->FromName=$sender;
955
                        
956
                        $mmail->isHTML(true);
957

    
958
                        // get subject
959
                        $bodys=array();
960
                        $altbodys=array();
961
                        $subjects=array();
962
                        // Send all mails
963
                        if (count($users)){
964
                                foreach ($users as $email => $lang){
965
                                        if (!isset($subjects[$lang])||!isset($bodys[$lang])||!isset($altbodys[$lang])){
966
                                                loadLanguage($lang);
967
                                                $subjects[$lang]=$this->get_mail_subject();
968
                                                $bodys[$lang]=$this->get_mail_body("html");
969
                                                $altbodys[$lang]=$this->get_mail_body("text");
970
                                        }
971

    
972
                                        $mmail->Subject=$subjects[$lang];
973

    
974
                                        $mmail->Body=$bodys[$lang];
975
                                        $mmail->AltBody=$altbodys[$lang];
976

    
977
                                        $mmail->AddAddress($email, "");
978
                                        if(!$mmail->Send()){
979
                                                echo "<div class='center'>".$LANG["mailing"][47]."</div>";
980
                                                return false;
981
                                        }else{
982
                                                logInFile("mail",$LANG["reservation"][40]." ".$email.": ".$subjects[$lang]."\n");
983
                                        }
984
        
985
                                        $mmail->ClearAddresses(); 
986
                                }
987
                        } else {
988
                                return false;
989
                        }
990
                }
991
                return true;
992
        }
993

    
994
}
995

    
996

    
997
/**
998
 *  Mailing class for alerts
999
 */
1000
class MailingAlert
1001
{
1002
        /// mailing type (contract,infocom,cartridge,consumable)
1003
        var $type=NULL;
1004
        /// message to send
1005
        var $message="";
1006
        /// working entity
1007
        var $entity="";
1008

    
1009
        /**
1010
         * Constructor
1011
         * @param $type mailing type (new,attrib,followup,finish)
1012
         * @param $message Message to send
1013
         * @param $entity Restrict to a defined entity
1014
         * @return nothing 
1015
         */
1016
        function MailingAlert ($type,$message,$entity=-1)
1017
        {
1018
                $this->type=$type;
1019
                $this->message=$message;
1020
                $this->entity=$entity;
1021
        }
1022

    
1023

    
1024
        /**
1025
         * Give mails to send the mail
1026
         * 
1027
         * Determine email to send mail using global config and Mailing type
1028
         *
1029
         * @return array containing email
1030
         */
1031
        function get_users_to_send_mail()
1032
        {
1033
                global $DB,$CFG_GLPI;
1034

    
1035
                $emails=array();
1036

    
1037
                $query="SELECT * FROM glpi_mailing WHERE type='".$this->type."'";
1038
                $result=$DB->query($query);
1039
                if ($DB->numrows($result)){
1040
                        while ($data=$DB->fetch_assoc($result)){
1041
                                switch ($data["item_type"]){
1042
                                        case USER_MAILING_TYPE :
1043
                                                switch($data["FK_item"]){
1044
                                                        // ADMIN SEND
1045
                                                        case ADMIN_MAILING :
1046
                                                                if (isValidEmail($CFG_GLPI["admin_email"])&&!isset($emails[$CFG_GLPI["admin_email"]]))
1047
                                                                        $emails[$CFG_GLPI["admin_email"]]=$CFG_GLPI["default_language"];
1048
                                                                break;
1049
                                                        // ADMIN ENTITY SEND
1050
                                                        case ADMIN_ENTITY_MAILING :
1051
                                                                $query2 = "SELECT admin_email AS EMAIL 
1052
                                                                        FROM glpi_entities_data 
1053
                                                                        WHERE (FK_entities = '".$this->entity."')";
1054
                                                                if ($result2 = $DB->query($query2)) {
1055
                                                                        if ($DB->numrows($result2)==1){
1056
                                                                                $row = $DB->fetch_array($result2);
1057
                                                                                if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){
1058
                                                                                        $emails[$row['EMAIL']]=$CFG_GLPI["default_language"];
1059
                                                                                }
1060
                                                                        }
1061
                                                                }
1062
                                                                break;
1063

    
1064
                                                }
1065
                                                break;
1066
                                        case PROFILE_MAILING_TYPE :
1067
                                                $query="SELECT glpi_users.email AS EMAIL, glpi_users.language AS LANG 
1068
                                                        FROM glpi_users_profiles 
1069
                                                        INNER JOIN glpi_users ON (glpi_users_profiles.FK_users = glpi_users.ID) 
1070
                                                        WHERE glpi_users_profiles.FK_profiles='".$data["FK_item"]."'
1071
                                                        ".getEntitiesRestrictRequest("AND","glpi_users_profiles","FK_entities",$this->entity,true);
1072

    
1073
                                                if ($result2= $DB->query($query)){
1074
                                                        if ($DB->numrows($result2)){
1075
                                                                while ($row=$DB->fetch_assoc($result2)){
1076
                                                                        if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){
1077
                                                                                $emails[$row['EMAIL']]=$row['LANG'];
1078
                                                                        }
1079
                                                                }
1080
                                                        }
1081
                                                }
1082
                                                break;
1083
                                        case GROUP_MAILING_TYPE :
1084
                                                $query="SELECT glpi_users.email AS EMAIL, glpi_users.language AS LANG 
1085
                                                        FROM glpi_users_groups 
1086
                                                        INNER JOIN glpi_users ON (glpi_users_groups.FK_users = glpi_users.ID) 
1087
                                                        WHERE glpi_users_groups.FK_groups='".$data["FK_item"]."'";
1088

    
1089
                                                if ($result2= $DB->query($query)){
1090
                                                        if ($DB->numrows($result2))
1091
                                                                while ($row=$DB->fetch_assoc($result2)){
1092
                                                                        if (isValidEmail($row['EMAIL'])&&!isset($emails[$row['EMAIL']])){
1093
                                                                                $emails[$row['EMAIL']]=$row['LANG'];
1094
                                                                        }
1095
                                                                }
1096
                                                }
1097
                                                break;
1098
                                }
1099
                        }
1100
                }
1101

    
1102
                return $emails;
1103
        }
1104

    
1105
        /**
1106
         * Format the mail body to send
1107
         * @param $format text or html
1108
         * @return mail body string
1109
         */
1110
        function get_mail_body($format="text"){
1111
                global $CFG_GLPI, $LANG;
1112

    
1113
                // Create message body from Job and type
1114
                $body="";
1115

    
1116
                if($format=="html"){
1117

    
1118
                        $body.=$this->message;
1119
                        $body.="<br>-- <br>".$CFG_GLPI["mailing_signature"];
1120
                        $body.="</body></html>";
1121
                        $body=ereg_replace("\n","<br>",$body);
1122
                }else{ // text format
1123

    
1124
                        $body.=$this->message;
1125
                        $body.="\n-- \n".$CFG_GLPI["mailing_signature"];
1126
                        $body=ereg_replace("<br />","\n",$body);
1127
                        $body=ereg_replace("<br>","\n",$body);
1128
                }
1129
                return $body;
1130
        }
1131

    
1132
        /**
1133
         * Format the mail subject to send
1134
         * @return mail subject string
1135
         */
1136
        function get_mail_subject()
1137
        {
1138
                global $LANG;
1139

    
1140
                // Create the message subject 
1141
                $subject="[SUPPORT RYXEO]";
1142

    
1143
                switch ($this->type){
1144
                        case "alertcartridge" :
1145
                                $subject.=" ".$LANG["mailing"][33]. " - ".getDropdownName("glpi_entities",$this->entity);
1146
                        break;
1147
                        case "alertconsumable":
1148
                                $subject.=" ".$LANG["mailing"][36]. " - ".getDropdownName("glpi_entities",$this->entity);
1149
                        break;
1150
                        case "alertcontract":
1151
                                $subject.=" ".$LANG["mailing"][39]. " - ".getDropdownName("glpi_entities",$this->entity);
1152
                        break;
1153
                        case "alertinfocom":
1154
                                $subject.=" ".$LANG["mailing"][41]. " - ".getDropdownName("glpi_entities",$this->entity);
1155
                        break;
1156
                        case "alertlicense":
1157
                                $subject.=" ".$LANG["mailing"][52]. " - ".getDropdownName("glpi_entities",$this->entity);
1158
                        break;
1159
                }
1160
                return $subject;
1161
        }
1162

    
1163
        /**
1164
         * Format the mail sender to send
1165
         * @return mail sender email string
1166
         */
1167
        function get_mail_sender(){
1168
                global $CFG_GLPI,$DB;
1169

    
1170
                $query = "SELECT admin_email AS EMAIL FROM glpi_entities_data WHERE (FK_entities = '".$this->entity."')";
1171
                if ($result=$DB->query($query)){
1172
                        if ($DB->numrows($result)){
1173
                                $data=$DB->fetch_assoc($result);
1174
                                if (isValidEmail($data["EMAIL"])){
1175
                                        return $data["EMAIL"];
1176
                                }
1177
                        }
1178
                }
1179

    
1180
                return $CFG_GLPI["admin_email"];
1181
        }
1182

    
1183

    
1184
        /**
1185
         * Send mail function
1186
         *
1187
         * Construct email and send it
1188
         *
1189
         * @return mail subject string
1190
         */
1191
        function send()
1192
        {
1193
                global $CFG_GLPI,$LANG;
1194
                if ($CFG_GLPI["mailing"])
1195
                {
1196
                        // get users to send mail
1197
                        $users=$this->get_users_to_send_mail();
1198

    
1199
                        //ryxeo adds contact at least
1200
                        $users["contact@ryxeo.com"]="fr_FR";
1201

    
1202
                        // get subject OK
1203
                        $subject=$this->get_mail_subject();
1204
                        // get sender :  OK
1205
                        $sender= $this->get_mail_sender();
1206
                        // get reply-to address : user->email ou job_email if not set OK
1207
                        $replyto=$sender;
1208

    
1209

    
1210
                        $mmail=new glpi_phpmailer();
1211
                        $mmail->From=$sender;
1212
                        $mmail->AddReplyTo("$replyto", ''); 
1213
                        $mmail->FromName=$sender;
1214

    
1215
                        $mmail->isHTML(true);
1216

    
1217
                        // Send all mails
1218

    
1219
                        // get subject
1220
                        $bodys=array();
1221
                        $altbodys=array();
1222
                        $subjects=array();
1223
                        // Send all mails
1224
                        if (count($users)){
1225
                                foreach ($users as $email => $lang){
1226
                                        if (!isset($subjects[$lang])||!isset($bodys[$lang])||!isset($altbodys[$lang])){
1227
                                                loadLanguage($lang);
1228
                                                $subjects[$lang]=$this->get_mail_subject();
1229
                                                $bodys[$lang]=$this->get_mail_body("html");
1230
                                                $altbodys[$lang]=$this->get_mail_body("text");
1231
                                        }
1232

    
1233
                                        $mmail->Subject=$subjects[$lang];
1234

    
1235
                                        $mmail->Body=$bodys[$lang];
1236
                                        $mmail->AltBody=$altbodys[$lang];
1237

    
1238
                                        $mmail->AddAddress($email, "");
1239

    
1240
                                        if(!$mmail->Send()){
1241
                                                addMessageAfterRedirect($LANG["mailing"][47]);
1242
                                                return false;
1243
                                        }else{
1244
                                                logInFile("mail",$LANG["mailing"][111]." ".$email.": ".$subjects[$lang]."\n");
1245
                                        }
1246
                                        $mmail->ClearAddresses(); 
1247
                                }
1248
                        } else {
1249
                                return false;
1250
                        }
1251
                }
1252
                return true;
1253
        }
1254
}
1255

    
1256
?>
Redmine Appliance - Powered by TurnKey Linux