Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

ryxeo-glpi-git / inc / mailgate.class.php @ b67d8923

Historique | Voir | Annoter | Télécharger (21,5 ko)

1
<?php
2
/*
3
 * @version $Id: mailgate.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
/// Mailgate class
41
class Mailgate  extends CommonDBTM {
42

    
43

    
44
        /**
45
         * Constructor
46
        **/
47
        function Mailgate () {
48
                $this->table="glpi_mailgate";
49
                $this->type=MAILGATE_TYPE;
50
        }
51
        function prepareInputForUpdate($input) {
52

    
53
                if (isset($input['password'])&&empty($input['password'])){
54
                        unset($input['password']);
55
                }
56
                if (isset ($input['mail_server']) && !empty ($input['mail_server']))
57
                        $input["host"] = constructMailServerConfig($input);
58
                return $input;
59
        }
60

    
61
        function prepareInputForAdd($input) {
62

    
63
                if (isset ($input['mail_server']) && !empty ($input['mail_server']))
64
                        $input["host"] = constructMailServerConfig($input);
65
                return $input;
66
        }
67

    
68
        /**
69
         * Print the mailgate form
70
         *
71
         *@param $target filename : where to go when done.
72
         *@param $ID Integer : Id of the item to print
73
         *
74
         *@return boolean item found
75
         **/
76
        function showForm ($target,$ID) {
77

    
78
                global $CFG_GLPI, $LANG;
79

    
80
                if (!haveRight("config","r")) return false;
81

    
82
                $spotted = false;
83

    
84
                if(empty($ID)) {
85
                        if($this->getEmpty()) $spotted = true;
86
                } else {
87
                        if($this->getFromDB($ID)) $spotted = true;
88
                }
89

    
90
                if ($spotted){
91

    
92
                        echo "<div class='center'><form method='post' name=form action=\"$target\">";
93

    
94
                        echo "<table class='tab_cadre' cellpadding='2'>";
95

    
96
                        echo "<tr><th align='center' colspan='2'>";
97
                        if (empty($ID)){
98
                                echo $LANG["mailgate"][1];
99
                        } else {
100
                                echo $LANG["mailgate"][0].": ".$this->fields["ID"];
101
                        }
102

    
103
                        echo "</th></tr>";
104
                        if (!function_exists('mb_list_encodings')||!function_exists('mb_convert_encoding')){
105
                                echo "<tr class='tab_bg_1'><td align='center' colspan='2'>";
106
                                echo $LANG["mailgate"][4];
107
                                echo "</td></tr>";
108
                        }
109
                        echo "<tr class='tab_bg_2'><td>".$LANG["common"][16].":        </td><td>";
110
                        autocompletionTextField("name","glpi_mailgate","name",$this->fields["name"],20);
111
                        echo "</td></tr>";
112

    
113
                        echo "<tr class='tab_bg_2'><td>".$LANG["entity"][0].":        </td><td>";
114
                        dropdownValue("glpi_entities", "FK_entities",$this->fields["FK_entities"],1,$_SESSION['glpiactiveentities']);
115
                        echo "</td></tr>";
116

    
117
                        showMailServerConfig($this->fields["host"]);
118

    
119
                        echo "<tr class='tab_bg_2'><td>".$LANG["login"][6].":        </td><td>";
120
                        autocompletionTextField("login","glpi_mailgate","login",$this->fields["login"],20);
121
                        echo "</td></tr>";
122

    
123
                        echo "<tr class='tab_bg_2'><td>".$LANG["login"][7].":        </td><td>";
124
                        echo "<input type='password' name='password' value='' size='20'>";
125
                        echo "</td></tr>";
126

    
127

    
128
                        if (haveRight("config","w")) {
129

    
130
                                echo "<tr class='tab_bg_1'>";
131
                                if(empty($ID)){
132

    
133
                                        echo "<td valign='top' colspan='2'>";
134
                                        echo "<div class='center'><input type='submit' name='add' value=\"".$LANG["buttons"][8]."\" class='submit'></div>";
135
                                        echo "</td>";
136
                                        echo "</tr>";
137
                                } else {
138

    
139
                                        echo "<td valign='top' align='center'>";
140
                                        echo "<input type='hidden' name='ID' value=\"$ID\">\n";
141
                                        echo "<input type='submit' name='update' value=\"".$LANG["buttons"][7]."\" class='submit'>";
142
                                        echo "</td>";
143
                                        echo "<td valign='top'>\n";
144
                                        echo "<div class='center'>";
145
                                        echo "<input type='submit' name='delete' value=\"".$LANG["buttons"][6]."\" class='submit'>";
146
                                        echo "</div>";
147
                                        echo "</td>";
148
                                        echo "</tr>";
149
                                        echo "<tr class='tab_bg_1'><td colspan='2' align='center'><input type='submit' name='get_mails' value=\"".$LANG["mailgate"][2]."\" class='submit'>";
150
                                        echo "</td></tr>";
151
                                }
152

    
153

    
154
                        }
155

    
156
                        echo "</table></form></div>";
157

    
158
                        return true;        
159
                }
160
                else {
161
                        echo "<div class='center'><strong>".$LANG["common"][54]."</strong></div>";
162
                        return false;
163
                }
164

    
165
        }
166
        
167
}
168

    
169

    
170
// modif and debug by  INDEPNET Development Team.
171
// Merge with collect GLPI system after big modification in it
172
/* Original class ReceiveMail 1.0 by Mitul Koradia Created: 01-03-2006
173
 * Description: Reciving mail With Attechment
174
 * Email: mitulkoradia@gmail.com
175
 */
176
/// Mailcollect class
177
class MailCollect {
178
        /// working entity
179
        var $entity;
180
        /// working charset of the mail
181
        var $charset="";
182

    
183
        /// IMAP / POP server
184
        var $server='';
185
        /// IMAP / POP login
186
        var $username='';
187
        /// IMAP / POP password
188
        var $password='';
189
        
190
        /// IMAP / POP connection
191
        var $marubox='';                                        
192
        
193
        /// ID of the current message
194
        var $mid = -1;
195
        /// structure used to store the mail structure
196
        var $structure = false;
197
        /// structure used to store files attached to a mail
198
        var $files;         
199
        /// Message to add to body to build ticket
200
        var $addtobody; 
201
        /// Number of ferchs emails
202
        var $fetch_emails=0;
203

    
204
        /**
205
        * Constructor
206
        * @param $server IMAP/POP server address
207
        * @param $username IMAP/POP user name
208
        * @param $password IMAP/POP password
209
        * @param $entity entity ID used
210
        * @param $display display messages in MessageAfterRedirect or just return error
211
        * @return if $display = false return messages result string
212
        */
213
        function collect($server,$username,$password,$entity,$display=0){
214
                global $LANG;
215
                $this->entity=$entity;
216

    
217
                $this->server        =        $server;
218
                $this->username        =        $username;
219
                $this->password        =        $password;
220
                $this->mid        = -1;
221

    
222
                $this->fetch_emails = 0;
223
                //Connect to the Mail Box
224
                $this->connect();
225

    
226
                if ($this->marubox){
227
                        // Get Total Number of Unread Email in mail box
228
                        $tot=$this->getTotalMails(); //Total Mails in Inbox Return integer value
229
                        $error=0;
230

    
231
                        for($i=1;$i<=$tot && $i<= MAX_MAILS_RETRIEVED;$i++){
232
                                $tkt= $this->buildTicket($i);
233
                                $this->deleteMails($i); // Delete Mail from Mail box
234
                                $result=imap_fetchheader($this->marubox,$i);
235

    
236
                                // Is a mail responding of an already existgin ticket ?
237
                                if (array_key_exists('tracking',$tkt) ) {
238
                                        // Deletion of message with sucess
239
                                        if (false === is_array($result)){
240
                                                $fup=new Followup();
241
                                                $fup->add($tkt);
242
                                        } else {
243
                                                $error++;
244
                                        }
245
                                } else { // New ticket
246
                                        // Deletion of message with sucess
247
                                        if (false === is_array($result)){
248
                                                $track=new job;
249
                                                $track->add($tkt);
250
                                        } else {
251
                                                $error++;
252
                                        }
253
                                }
254
                                $this->fetch_emails++;
255
                        }
256
                        imap_expunge($this->marubox);
257
                        $this->close_mailbox();   //Close Mail Box
258

    
259
                        if ($display){
260
                                addMessageAfterRedirect($LANG["mailgate"][3].": ".$this->fetch_emails." ".($error>0?"($error ".$LANG["common"][63].")":""));
261
                        } else {
262
                                return $LANG["mailgate"][3].": ".$this->fetch_emails." ".($error>0?"($error ".$LANG["common"][63].")":"");
263
                        }
264
                        
265
                }else{
266
                        if ($display){
267
                                addMessageAfterRedirect($LANG["log"][41]);
268
                        } else {
269
                                return $LANG["log"][41];
270
                        }
271
//                        return 0;
272
                }
273
        } // end function MailCollect
274
        
275
        
276
        
277
        /** function buildTicket - Builds,and returns, the major structure of the ticket to be entered . 
278
        * @param $i mail ID
279
        * @return ticket fields array
280
        */
281
        function buildTicket($i){
282
                global $DB,$LANG,$CFG_GLPI;
283
        
284
                $head=$this->getHeaders($i);  // Get Header Info Return Array Of Headers **Key Are (subject,to,toOth,toNameOth,from,fromName)
285
        
286
                $tkt= array ();
287

    
288
                // max size = 0 : no import attachments
289
                if ($CFG_GLPI['mailgate_filesize_max']>0){
290
                        if (is_writable(GLPI_DOC_DIR."/_tmp/")){
291
                                $_FILES=$this->getAttached($i,GLPI_DOC_DIR."/_tmp/",$CFG_GLPI['mailgate_filesize_max']);
292
                        } else {
293
                                logInFile('mailgate',GLPI_DOC_DIR."/_tmp/ is not writable");
294
                        }
295
                }
296

    
297
                //  Who is the user ?
298
                $tkt['author']=0;
299
                $query="SELECT ID from glpi_users WHERE email='".$head['from']."'";
300
                $result=$DB->query($query);
301
                if ($result&&$DB->numrows($result)){
302
                        $tkt['author']=$DB->result($result,0,"ID");
303
                }
304
                //RYXEO hack no spam please, utilisateur inconnu ? passe ta route
305
                else {
306
                  return ;
307
                }
308

    
309
                // AUto_import
310
                $tkt['_auto_import']=1;
311
                $tkt['_do_not_check_author']=1;
312
                $body=$this->getBody($i);
313

    
314
                // Do it before using charset variable
315
                $head['subject']=$this->decodeMimeString($head['subject']);
316

    
317
                if (!empty($this->charset)&&function_exists('mb_convert_encoding')){
318
                        $body=mb_convert_encoding($body, 'utf-8',$this->charset);
319
                }
320
                if (!seems_utf8($body)){
321
                        $tkt['contents']= utf8_encode($body);        
322
                }else{
323
                        $tkt['contents']= $body;
324
                }
325
                // Add message from getAttached
326
                if ($this->addtobody) {
327
                        $tkt['contents'] .= $this->addtobody;
328
                }
329

    
330
                $exists = false;
331
                //RYXEO special a cause des loopback to myself
332
                if ( preg_match('/Nouveau ticket/',$head['subject'],$match) ) {
333
                  return;
334
                }
335
                if ( preg_match('/\[SUPPORT RYXEO #(\d+)/',$head['subject'],$match) ) {
336
                        // it's a reply to a previous ticket
337
                        $ID = (int)$match[1];
338
                        $tkt['tracking'] = $ID;
339
                        $job=new Job();
340
                        $job->getfromDB($ID);
341
                        // the job exists and the author is the same
342
                        if ( $job->getFromDB($ID) && ($job->fields["author"] == $tkt['author']) ) {
343
                
344
                                $exists = true;
345
                                $content=explode("\n",$tkt['contents']);
346
                                $tkt['contents']="";
347
                                $first_comment=true;
348
                                $to_keep=array();
349
                                foreach($content as $ID => $val){
350
                                        if (isset($val[0])&&$val[0]=='>'){
351
                                                // Delete line at the top of the first comment
352
                                                if ($first_comment){
353
                                                        $first_comment=false;
354
                                                        if (isset($to_keep[$ID-1])){
355
                                                                unset($to_keep[$ID-1]);
356
                                                        }
357
                                                }
358
                                        } else {
359
                                                // Detect a signature if already keep lines
360
                                                if (isset($val[0])&&$val[0]=='-'
361
                                                        &&isset($val[1])&&$val[1]=='-' 
362
                                                        &&count($tokeep)){
363
                                                        
364
                                                        break;
365
                                                } else {
366

    
367
                                                        $to_keep[$ID]=$ID;
368
                                                }
369
                                        }
370
                                }
371
                                foreach($to_keep as $ID ){
372
                                        $tkt['contents'].=$content[$ID]."\n";
373
                                }
374
                        }
375
                }
376
                //RYXEO: do not create tickets on mail ... due to spam
377
                //ou evite de creer un ticket a la racine quand on recoit un mail
378
                //de supportteam ... ie moi-meme !
379
                else {
380
                  return;
381
                }
382

    
383
//RYXEO: do not create tickets on mail ... due to spam
384
                if ( ! $exists ) {
385
                  unset($tkt['tracking']);
386
                  // Mail followup
387
                  /*
388
                    $tkt['uemail']=$head['from'];
389
                    $tkt['emailupdates']=1;
390
                    // Which entity ?
391
                    $tkt['FK_entities']=$this->entity;
392
                    
393
                    //$tkt['Subject']= $head['subject'];   // not use for the moment
394
                    $tkt['name']=$this->textCleaner($head['subject']);
395
                    // Medium
396
                    $tkt['priority']= "3";
397
                    // No hardware associated
398
                    $tkt['device_type']="0";
399
                    // Mail request type
400
                    $tkt['request_type']="2";
401
                  */
402
                }
403
                $tkt['contents']=clean_cross_side_scripting_deep(html_clean($tkt['contents']));
404
                
405
                $tkt=addslashes_deep($tkt);
406
                
407
                return $tkt;
408
        }
409

    
410

    
411
        /** function textCleaner - Strip out unwanted/unprintable characters from the subject. 
412
        * @param $text text to clean
413
        * @return clean text
414
        */
415
        function textCleaner($text)
416
        {
417
                //$text= str_replace("'", "", $text);
418
                $text= str_replace("=20", "\n", $text);
419
                return $text;
420
        }
421

    
422

    
423
        ///return supported encodings in lowercase.
424
        function mb_list_lowerencodings() { 
425
                $r=mb_list_encodings();
426
                for ($n=sizeOf($r); $n--; ) { 
427
                        $r[$n]=strtolower($r[$n]); 
428
                } 
429
                return $r;
430
        }
431
        
432
        /**  Receive a string with a mail header and returns it
433
        // decoded to a specified charset.
434
        // If the charset specified into a piece of text from header
435
        // isn't supported by "mb", the "fallbackCharset" will be
436
        // used to try to decode it.
437
        * @param $mimeStr mime header string
438
        * @param $inputCharset input charset
439
        * @param $targetCharset target charset
440
        * @param $fallbackCharset charset used if input charset not supported by mb
441
        * @return decoded string
442
        */
443
        function decodeMimeString($mimeStr, $inputCharset='utf-8', $targetCharset='utf-8', $fallbackCharset='iso-8859-1') {
444
                if (function_exists('mb_list_encodings')&&function_exists('mb_convert_encoding')){
445
                        $encodings=$this->mb_list_lowerencodings();
446
                        $inputCharset=strtolower($inputCharset);
447
                        $targetCharset=strtolower($targetCharset);
448
                        $fallbackCharset=strtolower($fallbackCharset);
449
                        
450
                        $decodedStr='';
451
                        $mimeStrs=imap_mime_header_decode($mimeStr);
452
                        for ($n=sizeOf($mimeStrs), $i=0; $i<$n; $i++) {
453
                                $mimeStr=$mimeStrs[$i];
454
                                $mimeStr->charset=strtolower($mimeStr->charset);
455
                                if (($mimeStr == 'default' && $inputCharset == $targetCharset)
456
                                || $mimeStr->charset == $targetCharset) {
457
                                        $decodedStr.=$mimeStr->text;
458
                                } else {
459
                                        if (in_array($mimeStr->charset, $encodings)){
460
                                                $this->charset=        $mimeStr->charset;
461
                                        }
462
                                
463
                                        $decodedStr.=mb_convert_encoding(
464
                                                $mimeStr->text, $targetCharset,
465
                                                (in_array($mimeStr->charset, $encodings) ?
466
                                                $mimeStr->charset : $fallbackCharset)
467
                                                );
468
                                }
469
                        } return $decodedStr;
470
                } else {
471
                        return $mimeStr;
472
                }
473
                
474
        }
475

    
476
         ///Connect To the Mail Box
477
        function connect()
478
        {
479
                $this->marubox=imap_open($this->server,$this->username,$this->password);
480
        }
481

    
482
        /**
483
         * get the message structure if not already retrieved
484
         * 
485
         * @param $mid : Message ID.
486
         * 
487
         */
488
         function getStructure ($mid)
489
         {
490
                 if ($mid != $this->mid || !$this->structure) {
491
                        $this->structure = imap_fetchstructure($this->marubox,$mid);
492
                        if ($this->structure) {
493
                                $this->mid = $mid;
494
                        }
495
                 }
496
         }
497
        /**
498
        *This function is use full to Get Header info from particular mail
499
        *
500
        * @param $mid               = Mail Id of a Mailbox
501
        *
502
        * @return Return Associative array with following keys
503
        *        subject   => Subject of Mail
504
        *        to        => To Address of that mail
505
        *        toOth     => Other To address of mail
506
        *        toNameOth => To Name of Mail
507
        *        from      => From address of mail
508
        *        fromName  => Form Name of Mail
509
        */
510
        function getHeaders($mid) // Get Header info
511
        {        
512
                $mail_header=imap_header($this->marubox,$mid);
513
                $sender=$mail_header->from[0];
514
                $sender_replyto=$mail_header->reply_to[0];
515
                if(strtolower($sender->mailbox)!='mailer-daemon' && strtolower($sender->mailbox)!='postmaster')
516
                {
517
                        $mail_details=array(
518
                                        'from'=>strtolower($sender->mailbox).'@'.$sender->host,
519
                                        //'fromName'=>$sender->personal,
520
                                        //'toOth'=>strtolower($sender_replyto->mailbox).'@'.$sender_replyto->host,
521
                                        //'toNameOth'=>$sender_replyto->personal,
522
                                        'subject'=>$mail_header->subject,
523
                                        //'to'=>strtolower($mail_header->toaddress)
524
                                );
525
                }
526
                return $mail_details;
527
        }
528

    
529
        /**Get Mime type Internal Private Use
530
        * @param $structure mail structure
531
        * @return mime type
532
        */
533
        function get_mime_type(&$structure) { 
534
                $primary_mime_type = array("TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER"); 
535
                
536
                if($structure->subtype) { 
537
                        return $primary_mime_type[(int) $structure->type] . '/' . $structure->subtype; 
538
                } 
539
                return "TEXT/PLAIN"; 
540
        }
541
        
542
        
543
        /**Get Part Of Message Internal Private Use
544
        * @param $stream An IMAP stream returned by imap_open
545
        * @param $msg_number The message number
546
        * @param $mime_type mime type of the mail
547
        * @param $structure struture of the mail
548
        * @param $part_number The part number.
549
        * @return data of false if error
550
        */
551
        function get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false) { 
552
                if($structure) {                 
553
                        if($mime_type == $this->get_mime_type($structure)){ 
554
                                if(!$part_number) { 
555
                                        $part_number = "1"; 
556
                                } 
557
                                $text = imap_fetchbody($stream, $msg_number, $part_number); 
558
                                if($structure->encoding == 3) { 
559
                                        return imap_base64($text); 
560
                                } else if($structure->encoding == 4) { 
561
                                        return imap_qprint($text); 
562
                                } else { 
563
                                        return $text; 
564
                                } 
565
                        } 
566
                        if($structure->type == 1){ /* multipart */ 
567
                                $prefix="";
568
                                reset($structure->parts);
569
                                while(list($index, $sub_structure) = each($structure->parts)){ 
570
                                        if($part_number){ 
571
                                                $prefix = $part_number . '.'; 
572
                                        } 
573
                                        $data = $this->get_part($stream, $msg_number, $mime_type, $sub_structure, $prefix . ($index + 1)); 
574
                                        if($data){ 
575
                                                return $data; 
576
                                        } 
577
                                } 
578
                        } 
579
                } 
580
                return false; 
581
        } 
582
        
583
        /**
584
         * used to get total unread mail from That mailbox
585
         *
586
         * Return : 
587
         * Int Total Mail
588
         */        
589
        function getTotalMails() //Get Total Number off Unread Email In Mailbox
590
        {
591
                $headers=imap_headers($this->marubox);
592
                return count($headers);
593
        }
594

    
595
        /**
596
        *GetAttech($mid,$path) / Prefer use getAttached
597
        *Save attached file from mail to given path of a particular location
598
        *
599
        * @param $mid mail id
600
        * @param $path path where to save
601
        *
602
        * @return  String of filename with coma separated
603
        *like a.gif,pio.jpg etc
604
        */        
605
        function GetAttech($mid,$path) {
606
                $struckture = imap_fetchstructure($this->marubox,$mid);
607
                $ar="";
608
                if (isset($struckture->parts)&&count($struckture->parts)>0){
609
                        foreach($struckture->parts as $key => $value)
610
                        {
611
                                $enc=$struckture->parts[$key]->encoding;
612
                                if($struckture->parts[$key]->ifdparameters)
613
                                {
614
                                        $name=$struckture->parts[$key]->dparameters[0]->value;
615
                                        $message = imap_fetchbody($this->marubox,$mid,$key+1);
616
                                        if ($enc == 0)
617
                                                $message = imap_7bit($message);
618
                                        if ($enc == 1)
619
                                                $message = imap_8bit ($message);
620
                                        if ($enc == 2)
621
                                                $message = imap_binary ($message);
622
                                        if ($enc == 3)
623
                                                $message = imap_base64 ($message); 
624
                                        if ($enc == 4)
625
                                                $message = quoted_printable_decode($message);
626
                                        if ($enc == 5)
627
                                                $message = $message;
628
                                        $fp=fopen($path.$name,"w");
629
                                        fwrite($fp,$message);
630
                                        fclose($fp);
631
                                        $ar=$ar.$name.",";
632
                                }
633
                        }
634
                }
635
                $ar=substr($ar,0,(strlen($ar)-1));
636
                return $ar;
637
        }
638
        
639
        /**
640
         * Private function : Recursivly get attached documents
641
         * 
642
         * @param $mid : message id
643
         * @param $path : temporary path
644
         * @param $maxsize : of document to be retrieved
645
         * @param $structure : of the message or part
646
         * @param $part : part for recursive
647
         * 
648
         * Result is stored in $this->files
649
         *  
650
         */
651
        function getRecursiveAttached ($mid, $path, $maxsize, $structure, $part="")
652
        {
653
                global $LANG;
654
                
655
                if ($structure->type == 1) { // multipart
656
                        reset($structure->parts);
657
                        while(list($index, $sub) = each($structure->parts)) {
658
                                $this->getRecursiveAttached($mid, $path, $maxsize, $sub, ($part ? $part.".".($index+1) : ($index+1)));
659
                        }
660
                } else if ($structure->ifdparameters) {
661

    
662
                        //get filename of attachment if present
663
                        $filename='';
664
                        // if there are any dparameters present in this part
665
                        if (count($structure->dparameters)>0){
666
                        foreach ($structure->dparameters as $dparam){
667
                                if ((strtoupper($dparam->attribute)=='NAME') ||(strtoupper($dparam->attribute)=='FILENAME')) $filename=$dparam->value;
668
                                }
669
                        }
670
                        //if no filename found
671
                        if ($filename==''){
672
                                // if there are any parameters present in this part
673
                                if (count($structure->parameters)>0){
674
                                        foreach ($structure->parameters as $param){
675
                                        if ((strtoupper($param->attribute)=='NAME') ||(strtoupper($param->attribute)=='FILENAME')) $filename=$param->value;
676
                                        }
677
                                }
678
                        }
679
                        $filename=$this->decodeMimeString($filename);
680

    
681
                        if ($structure->bytes > $maxsize) {
682
                                $this->addtobody .= "<br>".$LANG["mailgate"][6]." (" . getSize($structure->bytes) . "): ".$filename;
683
                                return false;
684
                        }
685
                        if (!isValidDoc($filename)){
686
                                $this->addtobody .= "<br>".$LANG["mailgate"][5]." (" . $this->get_mime_type($structure) . "): ".$filename;
687
                                return false;
688
                        }
689
                        if ($message=imap_fetchbody($this->marubox, $mid, $part)) {
690
                                switch ($structure->encoding)
691
                                {
692
                                        case 0:        $message = imap_7bit($message); break;
693
                                        case 1:        $message = imap_8bit($message); break;
694
                                        case 2: $message = imap_binary($message); break;
695
                                        case 3: $message = imap_base64($message); break;
696
                                        case 4: $message = quoted_printable_decode($message); break;
697
                                }        
698
                                $fp=fopen($path.$filename,"w");
699
                                if ($fp) {
700
                                        fwrite($fp,$message);
701
                                        fclose($fp);        
702
                                        
703
                                        $this->files['multiple'] = true;
704
                                        $j = count($this->files)-1;
705
                                        $this->files[$j]['filename']['size'] = $structure->bytes;
706
                                        $this->files[$j]['filename']['name'] = $filename;
707
                                        $this->files[$j]['filename']['tmp_name'] = $path.$filename;
708
                                        $this->files[$j]['filename']['type'] = $this->get_mime_type($structure);        
709
                                }
710
                        } // fetchbody
711
                } // ifdparameters 
712
        }
713

    
714
        /**
715
         * Public function : get attached documents in a mail
716
         * 
717
         * @param $mid : message id
718
         * @param $path : temporary path
719
         * @param $maxsize : of document to be retrieved
720
         * 
721
         * @return array like $_FILES
722
         *  
723
         */
724
        function getAttached ($mid, $path, $maxsize)
725
        {
726
                $this->getStructure($mid);
727
                $this->files = array();
728
                $this->addtobody="";
729
                $this->getRecursiveAttached($mid, $path, $maxsize, $this->structure);
730
                
731
                return ($this->files);
732
        }
733
        /**
734
         * Get The actual mail content from this mail
735
         *
736
         * @param $mid : mail Id
737
         */
738
        function getBody($mid) // Get Message Body
739
        {
740
                $this->getStructure($mid);
741

    
742
                $body = $this->get_part($this->marubox, $mid, "TEXT/HTML", $this->structure);
743
                if ($body == "") {
744
                        $body = $this->get_part($this->marubox, $mid, "TEXT/PLAIN", $this->structure);                        
745
                }
746
                if ($body == "") { 
747
                        return "";
748
                }
749
                return $body;
750
        }
751
        
752
        /**
753
         * Delete mail from that mail box
754
         *
755
         * @param $mid : mail Id
756
         */
757
        function deleteMails($mid) {
758
                imap_delete($this->marubox,$mid);
759
        }
760
        
761
        /**
762
         * Close The Mail Box
763
         *  
764
          */        
765
        function close_mailbox() {
766
                imap_close($this->marubox,CL_EXPUNGE);
767
        }
768

    
769
}
770

    
771
?>
Redmine Appliance - Powered by TurnKey Linux