Beispiel #1
0
 /**
  * Saves the email and it's attachments. Uses the default db adapter as
  * configured by the application. If an exception occurs, the exception's
  * message will be stored in an array (together with other exceptions that
  * may have occured) and returned later on. Any db operation that failed will
  * be rolled back.
  *
  * @param array $emailItem An associative array with the data to insert into the
  * different tables. All attachments will be stored in the key/value pair "attachments",
  * which is itself a numeric array
  *
  * @return mixed Return the id of the last inserted email item, or an
  * error message if an error occured.
  */
 private function _saveEmail(array $emailItem)
 {
     $filterAttachment = $this->_filterAttachment;
     $filterFlag = $this->_filterFlag;
     $filterItem = $this->_filterItem;
     $filterInbox = $this->_filterInbox;
     $modelAttachment = $this->_modelAttachment;
     $modelFlag = $this->_modelFlag;
     $modelItem = $this->_modelItem;
     $modelInbox = $this->_modelInbox;
     $dbAdapter = Zend_Db_Table::getDefaultAdapter();
     if (!$this->_maxAllowedPacket) {
         $config = Zend_Registry::get(Conjoon_Keys::REGISTRY_CONFIG_OBJECT);
         $this->_maxAllowedPacket = $config->database->variables->max_allowed_packet;
         if (!$this->_maxAllowedPacket) {
             $this->_maxAllowedPacket = Conjoon_Db_Util::getMaxAllowedPacket($dbAdapter);
         }
     }
     $this->_setPlainFromHtml($emailItem);
     // filter and insert into groupware_email_items
     $filterItem->setData($emailItem);
     $itemData = $filterItem->getProcessedData();
     if ($this->_maxAllowedPacket < strlen($emailItem['rawBody'])) {
         return 'Could not save message with subject "' . $itemData['subject'] . '" - message is larger than available packet size (' . $this->_maxAllowedPacket . ' bytes).';
     }
     $dbAdapter->beginTransaction();
     $currFilter = null;
     try {
         Conjoon_Util_Array::underscoreKeys($itemData);
         try {
             $id = (int) $modelItem->insert($itemData);
         } catch (Zend_Db_Statement_Exception $zdse) {
             // in very rare cases, there are 4-byte characters in a utf-8
             // string, and mysql cannot handle them right since we use
             // utf-8 collations. We'll strip those 4-byte characters away.
             // see CN-619
             $content = $itemData['content_text_plain'];
             $i = 0;
             $len = strlen($content);
             $fourByteDetected = false;
             while ($i < $len) {
                 $ord = ord($content[$i]);
                 switch (true) {
                     case $ord <= 127:
                         $i += 1;
                         break;
                     case $ord < 224:
                         $i += 2;
                         break;
                     case $ord < 240:
                         $i += 3;
                         break;
                     default:
                         $fourByteDetected = true;
                         $content = substr($content, 0, $i) . substr($content, $i + 4);
                         $len -= 4;
                         break;
                 }
             }
             if ($fourByteDetected === true) {
                 $lastResortData = $emailItem;
                 $lastResortData['contentTextPlain'] = $content;
                 $filterItem->setData($lastResortData);
                 $lastResortItemData = $filterItem->getProcessedData();
                 Conjoon_Util_Array::underscoreKeys($lastResortItemData);
                 $id = (int) $modelItem->insert($lastResortItemData);
                 /**
                  * @see Conjoon_Log
                  */
                 require_once 'Conjoon/Log.php';
                 Conjoon_Log::log("Detected 4-byte character in content_text_plain for " . "Email message with id {$id}", Zend_Log::NOTICE);
             }
         }
         if ($id <= 0) {
             return null;
         }
         // assign needed (reference) keys
         $emailItem['isRead'] = 0;
         $emailItem['id'] = $id;
         $emailItem['groupwareEmailItemsId'] = $id;
         // filter and insert into groupware_email_items_inbox
         $currFilter = $filterInbox;
         $filterInbox->setData($emailItem);
         $itemData = $filterInbox->getProcessedData();
         Conjoon_Util_Array::underscoreKeys($itemData);
         $modelInbox->addInboxData($itemData);
         // filter and insert into groupware_email_items_flag
         $currFilter = $filterFlag;
         $filterFlag->setData($emailItem);
         $itemData = $filterFlag->getProcessedData();
         Conjoon_Util_Array::underscoreKeys($itemData);
         $modelFlag->insert($itemData);
         // loop through attachments and insert into groupware_email_items_attachments
         $attachmentCount = count($emailItem['attachments']);
         $currFilter = $filterAttachment;
         for ($i = 0; $i < $attachmentCount; $i++) {
             $emailItem['attachments'][$i]['groupwareEmailItemsId'] = $id;
             $filterAttachment->setData($emailItem['attachments'][$i]);
             $itemData = $filterAttachment->getProcessedData();
             Conjoon_Util_Array::underscoreKeys($itemData);
             $modelAttachment->addAttachmentForItem($itemData, $id);
         }
         $dbAdapter->commit();
         return $id;
     } catch (Exception $e) {
         if ($e instanceof Zend_Filter_Exception) {
             $error = Conjoon_Error::fromFilter($currFilter, $e);
             $error = $error->getMessage();
         } else {
             $error = $e->getMessage();
         }
         try {
             $dbAdapter->rollBack();
         } catch (Exception $m) {
             $error .= '; ' . $m->getMessage();
         }
         return $error;
     }
 }
Beispiel #2
0
 /**
  * Returns the number of bytes denoting the maximum file size for uploads.
  *
  * @return float
  */
 public function _getUploadMaxFileSize()
 {
     /**
      * @see Zend_Registry
      */
     require_once 'Zend/Registry.php';
     /**
      * @see Conjoon_Keys
      */
     require_once 'Conjoon/Keys.php';
     /**
      * @see Zend_File_Transfer
      */
     require_once 'Zend/File/Transfer/Adapter/Http.php';
     $config = Zend_Registry::get(Conjoon_Keys::REGISTRY_CONFIG_OBJECT);
     $maxAllowedPacket = $config->database->variables->max_allowed_packet;
     if (!$maxAllowedPacket) {
         /**
          * @see Conjoon_Db_Util
          */
         require_once 'Conjoon/Db/Util.php';
         $maxAllowedPacket = Conjoon_Db_Util::getMaxAllowedPacket(Zend_Db_Table::getDefaultAdapter());
     }
     $maxFileSize = min((double) $config->files->upload->max_size, (double) $maxAllowedPacket);
     // allowed filesize is max-filesize - 33-36 % of max filesize,
     // due to base64 encoding which might happen
     $maxFileSize = $maxFileSize - round($maxFileSize / 10 * 3.3);
     return $maxFileSize;
 }