Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

ryxeo-glpi-git / inc / mailing.class.php @ f3b595b7

Historique | Voir | Annoter | Télécharger (36,7 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

    
432
                        }
433

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

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

    
441
                }else{ // text format
442

    
443
                        if ($CFG_GLPI["url_in_mail"]&&!empty($CFG_GLPI["url_base"])){
444
                          //ryxeo        
445
                          $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";
446

    
447
                        }
448

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

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

    
457
                return $body;
458
        }
459

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

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

    
477
                return $CFG_GLPI["admin_email"];
478
        }
479

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

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

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

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

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

    
536
                $replyto=$CFG_GLPI["admin_email"];
537

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

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

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

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

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

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

    
640
                                                        $mmail->AddAddress($email, "");
641

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

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

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

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

    
688
        }
689

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

    
701
                $emails=array();
702

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

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

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

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

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

    
831
                return $emails;
832
        }
833

    
834

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

    
842

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

    
863
                return $CFG_GLPI["admin_email"];
864
        }
865

    
866

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

    
875
                // Create message body from Job and type
876
                $body="";
877

    
878
                if($format=="html"){
879

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

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

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

    
907
                return $subject;
908
        }
909

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

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

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

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

    
948

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

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

    
970
                                        $mmail->Subject=$subjects[$lang];
971

    
972
                                        $mmail->Body=$bodys[$lang];
973
                                        $mmail->AltBody=$altbodys[$lang];
974

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

    
992
}
993

    
994

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

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

    
1021

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

    
1033
                $emails=array();
1034

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

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

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

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

    
1100
                return $emails;
1101
        }
1102

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

    
1111
                // Create message body from Job and type
1112
                $body="";
1113

    
1114
                if($format=="html"){
1115

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

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

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

    
1138
                // Create the message subject 
1139
                $subject="[SUPPORT RYXEO]";
1140

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

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

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

    
1178
                return $CFG_GLPI["admin_email"];
1179
        }
1180

    
1181

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

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

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

    
1207

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

    
1213
                        $mmail->isHTML(true);
1214

    
1215
                        // Send all mails
1216

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

    
1231
                                        $mmail->Subject=$subjects[$lang];
1232

    
1233
                                        $mmail->Body=$bodys[$lang];
1234
                                        $mmail->AltBody=$altbodys[$lang];
1235

    
1236
                                        $mmail->AddAddress($email, "");
1237

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

    
1254
?>
Redmine Appliance - Powered by TurnKey Linux