phpDocumentor lodel
[ class tree: lodel ] [ index: lodel ] [ all elements ]

Source for file imapfunc.php

Documentation is available at imapfunc.php

  1. <?php
  2. /**
  3.  * Fichier de fonction IMAP
  4.  *
  5.  * PHP versions 4 et 5
  6.  *
  7.  * LODEL - Logiciel d'Edition ELectronique.
  8.  *
  9.  * Copyright (c) 2001-2002, Ghislain Picard, Marin Dacos
  10.  * Copyright (c) 2003, Ghislain Picard, Marin Dacos, Luc Santeramo, Nicolas Nutten, Anne Gentil-Beccot
  11.  * Copyright (c) 2004, Ghislain Picard, Marin Dacos, Luc Santeramo, Anne Gentil-Beccot, Bruno Cénou
  12.  * Copyright (c) 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
  13.  * Copyright (c) 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
  14.  * Copyright (c) 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
  15.  *
  16.  * Home page: http://www.lodel.org
  17.  *
  18.  * E-Mail: lodel@lodel.org
  19.  *
  20.  * All Rights Reserved
  21.  *
  22.  * This program is free software; you can redistribute it and/or modify
  23.  * it under the terms of the GNU General Public License as published by
  24.  * the Free Software Foundation; either version 2 of the License, or
  25.  * (at your option) any later version.
  26.  *
  27.  * This program is distributed in the hope that it will be useful,
  28.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  30.  * GNU General Public License for more details.
  31.  *
  32.  * You should have received a copy of the GNU General Public License
  33.  * along with this program; if not, write to the Free Software
  34.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  35.  *
  36.  * @author Ghislain Picard
  37.  * @author Jean Lamy
  38.  * @copyright 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
  39.  * @copyright 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
  40.  * @copyright 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
  41.  * @licence http://www.gnu.org/copyleft/gpl.html
  42.  * @version CVS:$Id:
  43.  * @package lodel
  44.  */
  45.  
  46. require_once "func.php";
  47.  
  48. /**
  49.  * Retire les pièces jointes des mails sur une boîte mail donnée
  50.  *
  51.  * Cette fonction utilise les options lodelmail.host, lodelmail.user, lodelmail.passwd.
  52.  *
  53.  * @return le nombre de pièces jointes
  54.  */
  55. {
  56.     $options getoption(array ("lodelmail.host""lodelmail.user""lodelmail.passwd")"");
  57.     if (count($options!= || !$options['lodelmail.host']{
  58.         die('ERROR: To use this feature, you must create and fill the options host, user and passwd in the group lodelmail. See in the administration interface ');
  59.     }
  60.  
  61.     list ($host$portexplode(":"$options['lodelmail.host']);
  62.     $mailserver "{".$host.":"($port $port "110")."/pop3}INBOX";
  63.     $passwd $options['lodelmail.passwd'];
  64.     $user $options['lodelmail.user'];
  65.  
  66.     $mbox imap_open($mailserver$user$passwd);
  67.  
  68.     if ($mbox === false{
  69.         die(imap_last_error());
  70.         return;
  71.     }
  72.  
  73.     $nbattachment 0;
  74.     $nbmsg imap_num_msg($mbox);
  75.  
  76.     for ($msgno 1$msgno <= $nbmsg$msgno ++{
  77.         $nbattachment += extractattachments($mbox$msgno"(je?pg|png|gif|tiff|sxw|doc|rtf|html?)");
  78.         imap_delete($mbox$msgno);
  79.     }
  80.     imap_expunge($mbox);
  81.  
  82.     return $nbattachment;
  83. }
  84.  
  85. /**
  86.  * Extrait les pièces jointes des mails d'une boîte donnée
  87.  *
  88.  * @param object $mbox la boîte mail
  89.  * @param integer $mnum le numéro du mail
  90.  * @param string $extre extension acceptées
  91.  * @param integer $struct par défaut 0.la structure des pièces jointes
  92.  * @param integer $pno par défaut vide la partie des attachements (cas des mails multiparts)
  93.  *
  94.  */
  95. function extractattachments($mbox$mnum$extre$struct 0$pno "")
  96. {
  97.     $nbattachment 0;
  98.     if ($struct === 0{
  99.         $struct imap_fetchstructure($mbox$mnum);
  100.     }
  101.     switch ($struct->type{
  102.     case // multipart
  103.         // look for the subpart
  104.         $partno 1;
  105.         if ($pno{
  106.             $pno .= ".";
  107.         }
  108.         while (list ($jeach($struct->parts)) {
  109.             $nbattachment += extractattachments($mbox$mnum$extre$struct->parts[$j]$pno.$partno);
  110.             $partno ++;
  111.         }
  112.         break;
  113.     case // message
  114.         // decode
  115.         $nbattachment += extractattachments($mbox$mnum$extre$struct->parts[0]$pno);
  116.         break;
  117.     case :
  118.     default // other
  119.         // fetch the body of the part
  120.         $body imap_fetchbody($mbox$mnum$pno);
  121.  
  122.         // dcode
  123.         if ($struct->encoding == 3{
  124.             $body imap_base64($body);
  125.         }
  126.         elseif ($struct->encoding == 4{
  127.             $body imap_qprint($body);
  128.         }
  129.  
  130.         // get the filename
  131.         if ($struct->parameters[0]->attribute == "NAME"{
  132.             $filename $struct->parameters[0]->value;
  133.         }    else {
  134.             return// no filename don't download
  135.         }
  136.         $filename preg_replace("/[^\w\.]/""_"$filename);
  137.         $extpos strrpos($filename".");
  138.         $ext substr($filename$extpos);
  139.  
  140.         // check the extension is valid
  141.         if (!preg_match("/^\.".$extre."$/i"$ext)) {
  142.             return;
  143.         }
  144.  
  145.         if (strlen($filename127// limit the length of the filename
  146.             $filename substr($filename0127 strlen($ext)).$ext;
  147.         }
  148.  
  149.         // save the attachment as $filename
  150.         writefile(SITEROOT."upload/".$filename$body);
  151.         $nbattachment ++;
  152.         break;
  153.     }
  154.     return $nbattachment;
  155. }
  156. ?>

Documentation generated on Thu, 19 Jun 2008 05:07:52 +0200 by phpDocumentor 1.4.0a2