This blob has been accessed 420 times via Git panel.
- <?php
- function pass2_extract($mbox, $msgno, $p, $partno, $path) {
- $data = ($partno) ? imap_fetchbody($mbox, $msgno, $partno, FT_UID) : imap_body($mbox, $msgno, FT_UID);
- if ($p->encoding == 4)
- $data = quoted_printable_decode($data);
- else if ($p->encoding == 3)
- $data = base64_decode($data);
- $params = array();
- if ($p->parameters)
- foreach ($p->parameters as $x)
- $params[strtolower($x->attribute)] = $x->value;
- if ($p->dparameters)
- foreach ($p->dparameters as $x)
- $params[strtolower($x->attribute)] = $x->value;
- if ($params['filename'] || $params['name']) {
- $partid = htmlentities($p->id, ENT_QUOTES, "UTF-8");
- $filename = ($params['filename']) ? $params['filename'] : $params['name'];
- $attachments[$filename] = $data;
- $image[$key] = $filename;
- }
- if (count($attachments) > 0) {
- if (!file_exists($path)) {
- mkdir($path, 0700, 1);
- }
- foreach ($attachments as $key => $val) {
- $files = glob("$path/*", GLOB_NOSORT);
- if (count($files) > 0) {
- $dup = "0";
- foreach ($files as $file) {
- if (file_get_contents($file) == $val) {
- $dup++;
- }
- }
- if (($dup == 0) and (mb_strlen($val,"8bit") > 0)) {
- file_put_contents("$path/$key", $val);
- }
- }
- else {
- if (mb_strlen($val,"8bit") > 0) {
- file_put_contents("$path/$key", $val);
- }
- }
- unset($files);
- }
- }
- }
- function pass2_process($mbox, $msgno, $path) {
- $attachments = array();
- $s = imap_fetchstructure($mbox, $msgno, FT_UID);
- if (!$s->parts) {
- pass2_extract($mbox, $msgno, $s, 0, $path);
- }
- else {
- foreach ($s->parts as $partno0 => $p) {
- pass2_extract($mbox, $msgno, $p, $partno0 + 1, $path);
- }
- }
- }
- pass2_process($mbox, $msgno, $filePath);
- ?>