Back to index

d-push  2.0
sendmail.php
Go to the documentation of this file.
00001 <?php
00002 /***********************************************
00003 * File      :   sendmail.php
00004 * Project   :   Z-Push
00005 * Descr     :   Provides the SENDMAIL, SMARTREPLY and SMARTFORWARD command
00006 *
00007 * Created   :   16.02.2012
00008 *
00009 * Copyright 2007 - 2012 Zarafa Deutschland GmbH
00010 *
00011 * This program is free software: you can redistribute it and/or modify
00012 * it under the terms of the GNU Affero General Public License, version 3,
00013 * as published by the Free Software Foundation with the following additional
00014 * term according to sec. 7:
00015 *
00016 * According to sec. 7 of the GNU Affero General Public License, version 3,
00017 * the terms of the AGPL are supplemented with the following terms:
00018 *
00019 * "Zarafa" is a registered trademark of Zarafa B.V.
00020 * "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
00021 * The licensing of the Program under the AGPL does not imply a trademark license.
00022 * Therefore any rights, title and interest in our trademarks remain entirely with us.
00023 *
00024 * However, if you propagate an unmodified version of the Program you are
00025 * allowed to use the term "Z-Push" to indicate that you distribute the Program.
00026 * Furthermore you may use our trademarks where it is necessary to indicate
00027 * the intended purpose of a product or service provided you use it in accordance
00028 * with honest practices in industrial or commercial matters.
00029 * If you want to propagate modified versions of the Program under the name "Z-Push",
00030 * you may only do so if you have a written permission by Zarafa Deutschland GmbH
00031 * (to acquire a permission please contact Zarafa at trademark@zarafa.com).
00032 *
00033 * This program is distributed in the hope that it will be useful,
00034 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00035 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00036 * GNU Affero General Public License for more details.
00037 *
00038 * You should have received a copy of the GNU Affero General Public License
00039 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00040 *
00041 * Consult LICENSE file for details
00042 ************************************************/
00043 
00044 class SendMail extends RequestProcessor {
00045 
00054     public function Handle($commandCode) {
00055         $status = SYNC_COMMONSTATUS_SUCCESS;
00056         $sm = new SyncSendMail();
00057 
00058         $reply = $forward = $parent = $sendmail = $smartreply = $smartforward = false;
00059         if (Request::GetGETCollectionId())
00060             $parent = Request::GetGETCollectionId();
00061         if ($commandCode == ZPush::COMMAND_SMARTFORWARD)
00062             $forward = Request::GetGETItemId();
00063         else if ($commandCode == ZPush::COMMAND_SMARTREPLY)
00064             $reply = Request::GetGETItemId();
00065 
00066         if (self::$decoder->IsWBXML()) {
00067             $el = self::$decoder->getElement();
00068 
00069             if($el[EN_TYPE] != EN_TYPE_STARTTAG)
00070                 return false;
00071 
00072 
00073             if($el[EN_TAG] == SYNC_COMPOSEMAIL_SENDMAIL)
00074                 $sendmail = true;
00075             else if($el[EN_TAG] == SYNC_COMPOSEMAIL_SMARTREPLY)
00076                 $smartreply = true;
00077             else if($el[EN_TAG] == SYNC_COMPOSEMAIL_SMARTFORWARD)
00078                 $smartforward = true;
00079 
00080             if(!$sendmail && !$smartreply && !$smartforward)
00081                 return false;
00082 
00083             $sm->Decode(self::$decoder);
00084         }
00085         else {
00086             $sm->mime = self::$decoder->GetPlainInputStream();
00087             // no wbxml output is provided, only a http OK
00088             $sm->saveinsent = Request::GetGETSaveInSent();
00089         }
00090         // Check if it is a reply or forward. Two cases are possible:
00091         // 1. Either $smartreply or $smartforward are set after reading WBXML
00092         // 2. Either $reply or $forward are set after geting the request parameters
00093         if ($reply || $smartreply || $forward || $smartforward) {
00094             // If the mobile sends an email in WBXML data the variables below
00095             // should be set. If it is a RFC822 message, get the reply/forward message id
00096             // from the request as they are always available there
00097             if (!isset($sm->source)) $sm->source = new SyncSendMailSource();
00098             if (!isset($sm->source->itemid)) $sm->source->itemid = Request::GetGETItemId();
00099             if (!isset($sm->source->folderid)) $sm->source->folderid = Request::GetGETCollectionId();
00100 
00101             // replyflag and forward flags are actually only for the correct icon.
00102             // Even if they are a part of SyncSendMail object, they won't be streamed.
00103             if ($smartreply || $reply)
00104                 $sm->replyflag = true;
00105             else
00106                 $sm->forwardflag = true;
00107 
00108             if (!isset($sm->source->folderid))
00109                 ZLog::Write(LOGLEVEL_ERROR, sprintf("No parent folder id while replying or forwarding message:'%s'", (($reply) ? $reply : $forward)));
00110         }
00111 
00112         self::$topCollector->AnnounceInformation(sprintf("Sending email with %d bytes", strlen($sm->mime)), true);
00113 
00114         try {
00115             $status = self::$backend->SendMail($sm);
00116         }
00117         catch (StatusException $se) {
00118             $status = $se->getCode();
00119             $statusMessage = $se->getMessage();
00120         }
00121 
00122         if ($status != SYNC_COMMONSTATUS_SUCCESS) {
00123             if (self::$decoder->IsWBXML()) {
00124                 // TODO check no WBXML on SmartReply and SmartForward
00125                 self::$encoder->StartWBXML();
00126                 self::$encoder->startTag(SYNC_COMPOSEMAIL_SENDMAIL);
00127                 self::$encoder->startTag(SYNC_COMPOSEMAIL_STATUS);
00128                 self::$encoder->content($status); //TODO return the correct status
00129                 self::$encoder->endTag();
00130                 self::$encoder->endTag();
00131             }
00132             else
00133                 throw new HTTPReturnCodeException($statusMessage, HTTP_CODE_500, null, LOGLEVEL_WARN);
00134         }
00135 
00136         return $status;
00137     }
00138 }
00139 ?>