1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
<?php
require_once('FileParser.bi.class.php');
/**
* Classe qui permet de parser un ou plusieurs fichier reçu par mail
*/
class MailParser{
/**
* Serveur pop
*/
private $serveur = '';
/**
* User
*/
private $user = '*****';
/**
* pass
*/
private $pass = '*****';
/**
* Connection au serbeur mail
*/
private $connexion;
/**
* Nombre de message
*/
private $nombreMessage;
/**
* Nombre d'itération
*/
private $iteration = 0;
/**
* nom du dossier temporaire des BI
*/
private $dir;
/**
* Nom du fichier temporaire
*/
private $tmpName = 'toto.txt';
/**
* header du mail
*/
private $head;
/**
* From du mail
*/
private $from;
/**
* Objet du mail
*/
private $subject;
/**
* Nom du fichier
*/
private $nomFichier;
//**************************************************************************
// CONSTRUCTEUR
//**************************************************************************
/**
* Constructeur
*/
function __construct(){
$this->connexion = imap_open('{****/***:***}', $this->user, $this->pass);
$this->nombreMessage = imap_num_msg($this->connexion);
$this->dir = getcwd() .'/tmp_bi/' . $this->tmpName;
}
//**************************************************************************
// METHODES DE CLASSES
//**************************************************************************
/**
* Retourne vrai s'il reste des mail à lire
* @return Boolean
*/
public function suivant(){
return(++$this->iteration <= $this->nombreMessage);
}
//-------------------------------------------------------------------------
/**
* Retourne la pièce jointe du mail en cours
* @return la pièce jointe
*/
public function getPieceJointe(){
//On ouvre le fichier temporaire
$tmp = fopen($this->dir, 'w');
//On récupère les headers
$this->head = imap_header($this->connexion, $this->iteration);
$this->subject = $this->subjectDecode($this->head->subject);
$this->from = $this->head->fromaddress;
//On récupère les propriété de la pièce jointe
//pour le type d'encodeage du fichier
$o = imap_fetchstructure($this->connexion, $this->iteration);
//var_dump($o->parts);
//Pour le $o->parts CF dylan Salmon
//----------------------------------
if(!isset($o->parts))
{
//On repère le type d'encodage
switch($o->encoding){
case 3:
$content = imap_base64(imap_fetchbody($this->connexion, $this->iteration,2));
break;
case 4:
$content = imap_qprint(imap_fetchbody($this->connexion,$this->iteration,2));
break;
default:
$content = imap_fetchbody($this->connexion, $this->iteration,2);
break;
}
}
else{
$w=1;
$retour = array();
foreach($o->parts as $part) {
$attach=true;
$this->nomFichier = '';
//echo '<h2> PARTS => ' . $part->encoding . '</h2>';
switch($part->encoding){
case 3:
$content = imap_base64(imap_fetchbody($this->connexion, $this->iteration,$w));
break;
case 4:
$content = imap_qprint(imap_fetchbody($this->connexion,$this->iteration,$w));
break;
default:
$content = imap_fetchbody($this->connexion, $this->iteration,$w);
break;
}
if ($part->ifdparameters == 1) {
$dparams = $part->dparameters;
foreach($dparams as $dparam) {
if ($dparam->attribute == 'FILENAME') {
$this->nomFichier = $dparam->value;
}
}
}
if ($this->nomFichier == '') {
if ($part->ifparameters == 1) {
$params = $part->parameters;
foreach($params as $param) {
if ($param->attribute == 'NAME') {
$this->nomFichier = $param->value;
}
}
}
}
$w++;
}
}
fwrite($tmp, $content);
fclose($tmp);
return $this->dir;
}
//-------------------------------------------------------------------------
private function subjectDecode($var){
if(ereg("=\?.{0,}\?[Bb]\?",$var)){
$var = split("=\?.{0,}\?[Bb]\?",$var);
while(list($key,$value)=each($var)){
if(ereg("\?=",$value)){
$arrTemp=split("\?=",$value);
$arrTemp[0]=base64_decode($arrTemp[0]);
$var[$key]=join("",$arrTemp);
}
}
$var=join("",$var);
}
if(ereg("=\?.{0,}\?Q\?",$var)){
$var = quoted_printable_decode($var);
$var = ereg_replace("=\?.{0,}\?[Qq]\?","",$var);
$var = ereg_replace("\?=","",$var);
}
return trim(str_replace('?', '', $var));
}
//**************************************************************************
// ACCESSEURS
//**************************************************************************
/**
* Retourne la connexion en cours
* @return la Connexion
*/
public function getConnexion(){
return $this->connexion;
}
//------------------------------------------------------------
/**
* Retourne le nombre de message dans la boite mail
* @return nombre de message
*/
public function getNombreMessage(){
return $this->nombreMessage;
}
//---------------------------------------------------------------
/**
* retourne le from du mail
* @return From du mail
*/
public function getFrom(){
return $this->from;
}
//---------------------------------------------------------------
/**
* Retourne l'objet du mail
* @return Objet du mail
*/
public function getSujet(){
return $this->subject;
}
}
?> |
Partager