This is the API documentation for concrete5 version 5.6 and earlier. View Current API Documentation
  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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 
<?php defined('C5_EXECUTE') or die("Access Denied.");

/**
 * @package Core
 * @category Concrete
 * @author Andrew Embler <[email protected]>
 * @copyright  Copyright (c) 2003-2009 Concrete5. (http://www.concrete5.org)
 * @license    http://www.concrete5.org/license/     MIT License
 *
 */

class Concrete5_Library_MailImporter extends Object {

    /**
     * gets the text string that's used to identify the body of the message
     * @return string
     */
    public function getMessageBodyHeader() {
        return t('--- Reply ABOVE. Do not alter this line --- [' . $this->validationHash  . '] ---');
    }

    public function getValidationHash() {
        return $this->validationHash;
    }
    

    public function getMessageBodyHashRegularExpression() {
        return t('/\-\-\- Reply ABOVE\. Do not alter this line \-\-\- \[(.*)\] \-\-\-/i');
    }

    public function getList() {
        $db = Loader::db();
        $r = $db->Execute('select miID from MailImporters order by miID asc');
        $importers = array();
        while ($row = $r->FetchRow()) {
            $importers[] = MailImporter::getByID($row['miID']);
        }
        return $importers;
    }

    public function getEnabledList() {
        $db = Loader::db();
        $r = $db->Execute('select miID from MailImporters where miIsEnabled = 1 order by miID asc');
        $importers = array();
        while ($row = $r->FetchRow()) {
            $importers[] = MailImporter::getByID($row['miID']);
        }
        return $importers;
    }
    
    public static function getByID($miID) {
        $db = Loader::db();
        $row = $db->GetRow("select miID, miHandle, miServer, miUsername, miPassword, miEncryption, miIsEnabled, miEmail, miPort, miConnectionMethod, Packages.pkgID, pkgHandle from MailImporters left join Packages on MailImporters.pkgID = Packages.pkgID where miID = ?", array($miID));
        if (isset($row['miID'])) {
            Loader::library('mail/importers/' . $row['miHandle'], $row['pkgHandle']);
            $txt = Loader::helper('text');
            $className = $txt->camelcase($row['miHandle']) . 'MailImporter';
            $mi = new $className();
            $mi->setPropertiesFromArray($row);
            return $mi;
        }
    }

    public static function getByHandle($miHandle) {
        $db = Loader::db();
        $miID = $db->GetOne("select miID from MailImporters where miHandle = ?", $miHandle);
        return MailImporter::getByID($miID);
    }
    
    public function delete() {
        $db = Loader::db();
        $db->Execute('delete from MailImporters where miID = ?', array($this->miID));
    }
    
    public static function getListByPackage($pkg) {
        $db = Loader::db();
        $list = array();
        $r = $db->Execute('select miID from MailImporters where pkgID = ? order by miID asc', array($pkg->getPackageID()));
        while ($row = $r->FetchRow()) {
            $list[] = self::getByID($row['miID']);
        }
        $r->Close();
        return $list;
    }   
    
    public function getMailImporterID() {return $this->miID;}
    public function getMailImporterName() {
        $txt = Loader::helper('text');
        return $txt->unhandle($this->miHandle);
    }
    public function getMailImporterHandle() {return $this->miHandle;}
    public function getMailImporterServer() {return $this->miServer;}
    public function getMailImporterUsername() {return $this->miUsername;}
    public function getMailImporterPassword() {return $this->miPassword;}
    public function getMailImporterEncryption() {return $this->miEncryption;}
    public function getMailImporterEmail() {return $this->miEmail;}
    public function getMailImporterPort() {return $this->miPort;}
    public function isMailImporterEnabled() {return $this->miIsEnabled;}
    public function getMailImporterConnectionMethod() {return $this->miConnectionMethod;}
    public function getPackageID() { return $this->pkgID;}
    public function getPackageHandle() {
        return PackageList::getHandle($this->pkgID);
    }

    public function add($args, $pkg = null) {
        $db = Loader::db();
        extract($args);
        
        if ($miPort < 1) {
            $miPort = 0;
        }
        
        if ($miIsEnabled != 1) {
            $miIsEnabled = 0;
        }
        
        if ($miEncryption == '') {
            $miEncryption == null;
        }
        
        if (!$miConnectionMethod) {
            $miConnectionMethod = 'POP';
        }

        
        $pkgID = ($pkg == null) ? 0 : $pkg->getPackageID();
        
        $db->Execute('insert into MailImporters (miHandle, miServer, miUsername, miPassword, miEncryption, miIsEnabled, miEmail, miPort, miConnectionMethod, pkgID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', 
            array(
                $miHandle,
                Loader::helper('security')->sanitizeString($miServer),
                $miUsername,
                $miPassword,
                $miEncryption,
                $miIsEnabled,
                Loader::helper('security')->sanitizeString($miEmail),
                $miPort, 
                $miConnectionMethod, 
                $pkgID
            ));
        
        $miID = $db->Insert_ID();
        return MailImporter::getByID($miID);
    }
    
    public function update($args) {
        $db = Loader::db();
        extract($args);
        
        if ($miPort < 1) {
            $miPort = 0;
        }
        
        if ($miIsEnabled != 1) {
            $miIsEnabled = 0;
        }
        
        if ($miEncryption == '') {
            $miEncryption == null;
        }

        $db->Execute('update MailImporters set miServer = ?, miUsername = ?, miPassword = ?, miEncryption = ?, miIsEnabled = ?, miEmail = ?, miPort = ?, miConnectionMethod = ? where miID = ?', 
            array(
                Loader::helper('security')->sanitizeString($miServer),
                $miUsername,
                $miPassword,
                $miEncryption,
                $miIsEnabled,
                Loader::helper('security')->sanitizeString($miEmail),
                $miPort,
                $miConnectionMethod,
                $this->miID
            ));
    }
    
    public function setupBody($body) {
        return $this->getMessageBodyHeader() . "\n\n" . $body;
    }

    public function getValidationErrorMessage() {
        return t('Unable to process email. Check that your email contains the validation hash present in the original message.');
    }

    public function setupValidation($email, $dataObject) {
        $db = Loader::db();
        $h = Loader::helper('validation/identifier');
        $hash = $h->generate('MailValidationHashes', 'mHash');
        $args = array($email, $this->miID, $hash, time(), serialize($dataObject));
        $db->Execute("insert into MailValidationHashes (email, miID, mHash, mDateGenerated, data) values (?, ?, ?, ?, ?)", $args);
        $this->validationHash = $hash;
        return $hash;
    }
    
    public function getPendingMessages() {
        $messages = array();
        // connect to the server to grab all messages 
        Loader::library('3rdparty/Zend/Exception');
        if ($this->miConnectionMethod == 'IMAP') { 
            Loader::library('3rdparty/Zend/Mail/Storage/Imap');
        } else {
            Loader::library('3rdparty/Zend/Mail/Storage/Pop3');
        }
        
        $args = array('host' => $this->miServer, 'user' => $this->miUsername, 'password' => $this->miPassword);
        if ($this->miEncryption != '') {
            $args['ssl'] = $this->miEncryption;
        }
        if ($this->miPort > 0) {
            $args['port'] = $this->miPort;
        }
        
        if ($this->miConnectionMethod == 'IMAP') {
            $mail = new Zend_Mail_Storage_Imap($args);
        } else { 
            $mail = new Zend_Mail_Storage_Pop3($args);
        }
        $i = 1;
        foreach($mail as $m) {
            $mim = new MailImportedMessage($mail, $m, $i);
            $messages[] = $mim;
            $i++;
        }
        
        return $messages;
    }

    public function cleanup(MailImportedMessage $me) {
        
        $db = Loader::db();
        $db->query("update MailValidationHashes set mDateRedeemed = " . time() . " where mHash = ?", array($me->getValidationHash()));
        
        $m = $me->getOriginalMailObject();
        $msg = $me->getOriginalMessageObject();
        $m->removeMessage($me->getOriginalMessageCount());


    }
    
}

class MailImportedMessage {
    
    protected $body;
    protected $subject;
    protected $sender;
    protected $validationHash;
    protected $oMail;
    protected $oMailMessage;
    protected $oMailCnt;
    
    public function __construct($mail, Zend_Mail_Message $msg, $count) {
        
        try {
            $this->subject = $msg->subject;
        } catch(Exception $e) {
        }
        
        $this->oMail = $mail;
        $this->oMailMessage = $msg;
        $this->oMailCnt = $count;
        
        if (strpos($msg->from, ' ') === false) {
            $this->sender = str_replace(array('>','<'), '', $msg->from);
        } else {
            $this->sender = substr($msg->from, strpos($msg->from, '<') + 1, strpos($msg->from, '>') - strpos($msg->from, '<') - 1);
        }

        try {
        
            if (strpos($msg->contentType, 'text/plain') !== false) {
                $this->body = quoted_printable_decode($msg->getContent());
            } else {
                $foundPart = null;
                
                foreach (new RecursiveIteratorIterator($msg) as $part) {
                    try {
                        if (strtok($part->contentType, ';') == 'text/plain') {
                            $foundPart = $part;
                            break;
                        }
                    } catch (Zend_Mail_Exception $e) {
                        // ignore
                    }
                }
            
                if ($foundPart) {
                    $this->body = quoted_printable_decode($foundPart);
                }
            }
        } catch(Exception $e) {
        
        }
        
        // find the hash
        $r = preg_match(MailImporter::getMessageBodyHashRegularExpression(), $this->body, $matches);
        if ($r) {
            $this->validationHash = $matches[1];
            if ($this->validationHash != '') {
                $db = Loader::db();
                $r = $db->GetOne('select data from MailValidationHashes where mHash = ?', array($this->validationHash));
                if ($r) {
                    $this->dataObject = unserialize($r);
                }
            }
        }
    }
    
    public function getOriginalSender() {return $this->sender;}
    public function getOriginalMailObject() {return $this->oMail;}
    public function getOriginalMessageObject() {return $this->oMailMessage;}
    public function getOriginalMessageCount() {return $this->oMailCnt;}
    
    public function getSubject() {return $this->subject;}
    public function getBody() {return $this->body;}
    
    /** 
     * Returns the relevant content of the email message, minus any quotations, and the line that includes the validation hash
     */
    public function getProcessedBody() {
        $r = preg_split(MailImporter::getMessageBodyHashRegularExpression(), $this->body, $matches);        
        $message = $r[0];
        $r = preg_replace(array(
            '/^On (.*) at (.*), (.*) wrote:/sm',
            '/[\n\r\s\>]*\Z/i'
        ), '', $message);
        return $r;
    }
    
    public function getValidationHash() {return $this->validationHash;}
    
    /** 
     * Validates the email message - checks the validation hash found in the body with one in the database. Checks the from address as well
     */
    public function validate() {
        if (!$this->validationHash) {
            return false;
        }
        $db = Loader::db();
        $row = $db->GetRow("select * from MailValidationHashes where mHash = ? order by mDateGenerated desc limit 1", $this->validationHash);
        if ($row['mvhID'] > 0) {
            // Can't do this even though it's a good idea
            //if ($row['email'] != $this->sender) {
            //  return false;
            //} else {
                return $row['mDateRedeemed'] == 0;
            //}
        }
    }
    
    public function getDataObject() {return $this->dataObject;}
    
}