Exemplo n.º 1
0
 /**
  * do the plugin action
  * @param array data
  * @param object list model
  * @return number of records updated
  */
 public function process(&$data, &$listModel)
 {
     $params = $this->getParams();
     $email = $params->get('plugin-options.email');
     $pw = $params->get('plugin-options.password');
     if ($email == '' || $pw == '') {
         return;
     }
     $server = $params->get('plugin-options.server', '{imap.gmail.com:993/imap/ssl}');
     $inboxes = explode(',', $params->get('plugin-options.inboxes', 'INBOX'));
     $deleteMail = false;
     //$storageType = 'filesystemstorage';
     //require_once(JPATH_BASE.DS.'components'.DS.'com_fabrik'.DS.'plugins'.DS.'element'.DS.'fabrikfileupload'.DS.'adaptors'.DS.$storageType.'.php');
     $p = new stdClass();
     $fromField = $params->get('plugin-options.from');
     $titleField = $params->get('plugin-options.title');
     $dateField = $params->get('plugin-options.date');
     $contentField = $params->get('plugin-options.content');
     //	$storage = new $storageType( $p);
     //$imageLib = FabimageHelper::loadLib('GD2');
     //	$imageLib->setStorage($storage);
     $storeData = array();
     $numProcessed = 0;
     foreach ($inboxes as $inbox) {
         $url = $server . $inbox;
         $mbox = imap_open($url, $email, $pw);
         if (!$mbox) {
             JError::raiseNotice(400, JText::_("PLG_CRON_GMAIL_ERROR_CONNECT") . imap_last_error());
             continue;
         }
         $MC = imap_check($mbox);
         $mailboxes = imap_list($mbox, $server, '*');
         echo "<pre>";
         print_r($mailboxes);
         $lastid = $params->get('plugin-options.lastid', 0);
         if ($lastid == 0) {
             $result = imap_fetch_overview($mbox, "1:{$MC->Nmsgs}");
             echo $lastid;
             $mode = 0;
             //retrieve emails by message number
         } else {
             // retrieve emails by message id;
             $result = imap_fetch_overview($mbox, "{$lastid}:*", FT_UID);
             if (count($result) > 0) {
                 unset($result[0]);
             }
         }
         // Fetch an overview for all messages in INBOX
         //$result = imap_fetch_overview($mbox, "1:$lastid", $mode);
         print_r($result);
         exit;
         $numProcessed += count($result);
         foreach ($result as $overview) {
             if ($overview->uid > $lastid) {
                 $lastid = $overview->uid;
             }
             $content = '';
             $thisData = array();
             preg_match("/<(.*)>/", $overview->from, $matches);
             $thisData[$fromField] = $overview->from;
             $thisData[$titleField] = $this->getTitle($overview);
             $thisData[$dateField] = JFactory::getDate($overview->date)->toMySQL();
             $thisData['imageFound'] = false;
             $thisData[$fromField] = empty($matches) ? $overview->from : "<a href=\"mailto:{$matches['1']}\">{$overview->from}</a>";
             //use server time for all incomming messages.
             $date = JFactory::getDate();
             $thisData['processed_date'] = $date->toMySQL();
             $struct = imap_fetchstructure($mbox, $overview->msgno);
             $parts = create_part_array($struct);
             foreach ($parts as $part) {
                 //type 5 is image - full list here http://algorytmy.pl/doc/php/function.imap-fetchstructure.php
                 if ($part['part_object']->type == 5) {
                     $filecontent = imap_fetchbody($mbox, $overview->msgno, $part['part_number']);
                     $attachmentName = '';
                     $pname = 'parameters';
                     if (is_object($part['part_object']->parameters)) {
                         //can be in dparamenters instead?
                         $pname = 'dparameters';
                     }
                     $attarray = $part['part_object']->{$pname};
                     if ($attarray[0]->value == "us-ascii" || $attarray[0]->value == "US-ASCII") {
                         if ($attarray[1]->value != "") {
                             $attachmentName = $attarray[1]->value;
                         }
                     } elseif ($attarray[0]->value != "iso-8859-1" && $attarray[0]->value != "ISO-8859-1" && $attarray[0]->value != 'utf-8') {
                         $attachmentName = $attarray[0]->value;
                     }
                     if ($attachmentName != '') {
                         //randomize file name
                         $ext = JFile::getExt($attachmentName);
                         $name = JFile::stripExt($attachmentName);
                         $name .= '-' . JUserHelper::genRandomPassword(5) . '.' . $ext;
                         $thisData['attachmentName'] = $name;
                         $thisData['imageFound'] = true;
                         $fileContent = imap_fetchbody($mbox, $overview->msgno, 2);
                         $thisData['imageBuffer'] = imap_base64($filecontent);
                     }
                 }
                 /*
                 * Message parts - third param in imap_fetchbody
                 * (empty) - Entire message
                 						0 - Message header
                 						1 - MULTIPART/ALTERNATIVE
                 						1.1 - TEXT/PLAIN
                 						1.2 - TEXT/HTML
                 						2 - file.ext
                 */
                 $content = @imap_fetchbody($mbox, $overview->msgno, 1.2);
                 //html
                 if (strip_tags($content) == '') {
                     $content = @imap_fetchbody($mbox, $overview->msgno, 1.1);
                     //plain text
                 }
                 //this encodes text with  =20 correctly i think
                 //may need to test that $part['encoding'] = 4	(QUOTED-PRINTABLE)
                 $content = imap_qprint($content);
                 // hmm this seemed to include encoded text which imap_base64 couldnt sort out
                 //as the encoding was too long for insert query - shouts were not getting through
                 // think it might be to do with $part being type 5 (image)
                 //
                 // now only adding if part type is 0
                 if (strip_tags($content) == '') {
                     if ($part['part_object']->type == 0) {
                         $content = @imap_fetchbody($mbox, $overview->msgno, 1);
                         //multipart alternative
                     }
                 }
             }
             $content = $this->removeReplyText($content);
             //remove any style sheets
             $content = preg_replace('/<\\s*style.+?<\\s*\\/\\s*style.*?>/si', ' ', $content);
             $thisData[$contentField] = $content;
             foreach ($thisData as $key => $val) {
                 JRequest::setVar($key, $val);
             }
             $formModel = $listModel->getForm();
             unset($listModel->getFormModel()->_formData);
             $listModel->getFormModel()->process();
             //// TEST!!!!!!!
             if ($deleteMail) {
                 imap_delete($mbox, $overview->msgno);
             }
         }
     }
     $params->set('plugin-options.lastid', $lastid);
     $this->_row->params = $params->toString();
     $this->_row->store();
     /*	foreach ($storeData as &$data) {
     			$relLargeImagePath = '';
     			$largeImagePath = '';
     			$smallImagePath = '';
     			if ($data['imageFound']) {
     			// @TODO process images to fileupload element
     			if (isset($data['imageBuffer'] )) {
     			$relLargeImagePath = DS.'media'.DS.'com_fabrik'.DS.$data['id'].DS.'galleries'.DS.'images'.DS.$data['attachmentName'];
     			$largeImagePath = JPATH_BASE.$relLargeImagePath;
     
     			$smallImagePath = JPATH_BASE.DS.'media'.DS.'com_fabrik'.DS.$data['id'].DS.'galleries'.DS.'thumbs'.DS.$data['attachmentName'];
     			JFile::write( $largeImagePath, $data['imageBuffer']);
     			$imageLib->resize(400, 400, $largeImagePath, $largeImagePath);
     			$imageLib->resize(125, 125, $largeImagePath, $smallImagePath);
     			$title = "<a href='".JURI::base()."/media/com_fabrik/".$data['id']."/galleries/images/".$data['attachmentName']."' rel='lightbox[]' title='".$data['attachmentName']."'>
     			<img class='fabrikLightBoxImage' src='".JURI::base()."/media/com_fabrik/".$data['id']."/galleries/thumbs/".$data['attachmentName']."' alt='media' /></a>" . $title;
     			}
     			}
     			}*/
     imap_expunge($mbox);
     imap_close($mbox);
     return $numProcessed;
 }
Exemplo n.º 2
0
function getOneMail($usr, $nr)
{
    $files = array();
    mb_internal_encoding($_SESSION["charset"]);
    $srv = getUsrMailData($usr);
    $mbox = mail_login($srv["msrv"], $srv["port"], $srv["postf"], $srv["mailuser"], $srv["kennw"], $srv["proto"], $srv["ssl"]);
    $head = mail_parse_headers(mail_retr($mbox, $nr));
    if (!$head) {
        return;
    }
    $info = mail_fetch_overview($mbox, $nr);
    $senderadr = $head["From"] . "\n" . $head["Date"] . "\n";
    $sender = getSenderMail($head["From"]);
    $mybody = $senderadr;
    $htmlbody = "Empty Message Body";
    $subject = $head["Subject"];
    $structure = imap_fetchstructure($mbox, $nr);
    if ($structure->parts) {
        $parts = create_part_array($structure);
        $body = mail_get_body($mbox, $nr, $parts[0]);
    } else {
        $head["encoding"] = $structure->encoding;
        $head["ifsubtype"] = $structure->ifsubtype;
        $head["subtype"] = $structure->subtype;
        $body = mail_getBody($mbox, $nr, $head);
    }
    if (!preg_match('/PLAIN/i', $structure->subtype)) {
        for ($p = 1; $p < count($parts); $p++) {
            $attach = mail_get_file($mbox, $nr, $parts[$p]);
            if ($attach) {
                $files[] = $attach;
            }
        }
    }
    $rc = mail_SetFlag($mbox, $nr, $_SESSION['MailFlag']);
    mail_close($mbox);
    $data["id"] = $nr;
    $data["muid"] = $info[0]->uid;
    $data['kontaktname'] = $sender['kontaktname'];
    $data['kontakttab'] = $sender['kontakttab'];
    $data['kontaktid'] = $sender['kontaktid'];
    $data["sendername"] = $sender["name"];
    $data["senderid"] = $sender["id"];
    $data["Initdate"] = $head["date"];
    $data["cause"] = $subject;
    $data["c_long"] = $mybody . $body;
    $data["Datei"] = $anhang;
    $data["status"] = "1";
    $data["InitCrm"] = $_SESSION["loginCRM"];
    //$head[""];
    $data["CRMUSER"] = $_SESSION["login"];
    //$head[""];
    $data["DCaption"] = $files ? $data["cause"] : "";
    $data["Anhang"] = $files;
    $data['flags'] = array("flagged" => $info[0]->flagged, 'answered' => $info[0]->answered, 'deleted' => $info[0]->deleted, 'seen' => $info[0]->seen, 'draft' => $info[0]->draft, 'recend' => $info[0]->recend);
    return $data;
}