File indexing completed on 2024-05-12 05:58:48

0001 <?php
0002 /**
0003  *  ocs-webserver
0004  *
0005  *  Copyright 2016 by pling GmbH.
0006  *
0007  *    This file is part of ocs-webserver.
0008  *
0009  *    This program is free software: you can redistribute it and/or modify
0010  *    it under the terms of the GNU Affero General Public License as
0011  *    published by the Free Software Foundation, either version 3 of the
0012  *    License, or (at your option) any later version.
0013  *
0014  *    This program is distributed in the hope that it will be useful,
0015  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017  *    GNU Affero General Public License for more details.
0018  *
0019  *    You should have received a copy of the GNU Affero General Public License
0020  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021  **/
0022 
0023 function tplVarMapFunc($n)
0024 {
0025     return '/%' . $n . '%/';
0026 }
0027 
0028 class Default_Plugin_SendMail extends Zend_Controller_Plugin_Abstract
0029 {
0030 
0031     private $_bodyText;
0032     private $_subject;
0033     private $_receiverMail;
0034     private $_receiverAlias;
0035     private $_fromAlias = "opendesktop.org";
0036     private $_fromMail = "contact@opendesktop.org";
0037     private $_tplVars = array();
0038 
0039     function __construct($tplName = null)
0040     {
0041         //    parent::__construct();
0042 
0043         $this->setTemplate($tplName);
0044         $this->setTemplateVar('username', '');
0045         $this->setTemplateVar('username_sender', 'the user');
0046         $this->setTemplateVar('verificationlink', '');
0047         $this->setTemplateVar('sender', '');
0048         $this->setTemplateVar('product_title', '');
0049     }
0050 
0051     public function setTemplate($tplName)
0052     {
0053         if ($tplName === null) {
0054             return;
0055         }
0056 
0057         $mailTplTable = new Default_Model_DbTable_MailTemplate();
0058         $mailTpl = $mailTplTable->findBy('name', $tplName);
0059         $mailTpl = $mailTpl->toArray();
0060 
0061         $this->setSubject($mailTpl['subject']);
0062         $this->setBodyText($mailTpl['text']);
0063     }
0064 
0065     public function setTemplateVar($key, $value)
0066     {
0067         $this->_tplVars[$key] = $value;
0068     }
0069 
0070     public function getBodyText()
0071     {
0072         return $this->_bodyText;
0073     }
0074 
0075     public function setBodyText($bodyText)
0076     {
0077         $this->_bodyText = $bodyText;
0078     }
0079 
0080     public function getReceiverAlias()
0081     {
0082         return $this->_receiverAlias;
0083     }
0084 
0085     public function setReceiverAlias($alias)
0086     {
0087         $this->_receiverAlias = $alias;
0088     }
0089 
0090     public function send()
0091     {
0092         try {
0093             $mail = new Zend_Mail('utf-8');
0094             $mail->setBodyHtml($this->buildBodyHtml());
0095             $mail->setFrom($this->getFromMail(), $this->getFromAlias());
0096 
0097             $mail->addTo($this->getReceiverMail());
0098             $mail->setSubject($this->getSubject());
0099             $mail->send();
0100             Zend_Registry::get('logger')->info(__METHOD__ . " - mail address: {$this->getReceiverMail()} - subject : {$this->getSubject()} - email sent.");
0101         } catch (Exception $e) {
0102             Zend_Registry::get('logger')->err(__METHOD__ . " - mail address: {$this->getReceiverMail()} " . $e->getMessage() . PHP_EOL);
0103         }
0104     }
0105 
0106     private function buildBodyHtml()
0107     {
0108         $textReplacedVars = $this->replaceVars();
0109         $returnBodyText = <<< EOT
0110     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
0111 <html xmlns="http://www.w3.org/1999/xhtml">
0112   <head>
0113         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
0114         <title>ocs. - open content store.</title>
0115         <style type="text/css">
0116       /* /\/\/\/\/\/\/\/\/ CLIENT-SPECIFIC STYLES /\/\/\/\/\/\/\/\/ */
0117       #outlook a{padding:0;} /* Force Outlook to provide a "view in browser" message */
0118       .ReadMsgBody{width:100%;} .ExternalClass{width:100%;} /* Force Hotmail to display emails at full width */
0119       .ExternalClass, .ExternalClass p, .ExternalClass span, .ExternalClass font, .ExternalClass td, .ExternalClass div {line-height: 100%;} /* Force Hotmail to display normal line spacing */
0120       body, table, td, p, a, li, blockquote{-webkit-text-size-adjust:100%; -ms-text-size-adjust:100%;} /* Prevent WebKit and Windows mobile changing default text sizes */
0121       table, td{mso-table-lspace:0pt; mso-table-rspace:0pt;} /* Remove spacing between tables in Outlook 2007 and up */
0122       img{-ms-interpolation-mode:bicubic;} /* Allow smoother rendering of resized image in Internet Explorer */
0123 
0124       /* /\/\/\/\/\/\/\/\/ RESET STYLES /\/\/\/\/\/\/\/\/ */
0125       body{margin:0; padding:0;}
0126       img{border:0; height:auto; line-height:100%; outline:none; text-decoration:none;}
0127       table{border-collapse:collapse !important;}
0128       body, #bodyTable, #bodyCell{height:100% !important; margin:0; padding:0; width:100% !important;}
0129 
0130       /* /\/\/\/\/\/\/\/\/ TEMPLATE STYLES /\/\/\/\/\/\/\/\/ */
0131 
0132       /* ========== Page Styles ========== */
0133 
0134       #bodyCell{padding:20px;}
0135       #templateContainer{width:600px;}
0136 
0137       /**
0138              * @tab Page
0139              * @section background style
0140              * @tip Set the background color and top border for your email. You may want to choose colors that match your company's branding.
0141              * @theme page
0142              */
0143       body, #bodyTable{
0144         /*@editable*/ background-color:#DEE0E2;
0145       }
0146 
0147     /**
0148      * @tab Page
0149      * @section background style
0150      * @tip Set the background color and top border for your email. You may want to choose colors that match your company's branding.
0151      * @theme page
0152      */
0153     #bodyCell{
0154     /*@editable*/ border-top:4px solid #BBBBBB;
0155 }
0156 
0157 /**
0158  * @tab Page
0159  * @section email border
0160  * @tip Set the border for your email.
0161  */
0162 #templateContainer{
0163 /*@editable*/ border:1px solid #BBBBBB;
0164       }
0165 
0166       /**
0167              * @tab Page
0168              * @section heading 1
0169              * @tip Set the styling for all first-level headings in your emails. These should be the largest of your headings.
0170              * @style heading 1
0171              */
0172       h1{
0173     /*@editable*/ color:#202020 !important;
0174     display:block;
0175     /*@editable*/ font-family:"Lato", Helvetica, Arial, sans-serif;
0176         /*@editable*/ font-size:26px;
0177         /*@editable*/ font-style:normal;
0178         /*@editable*/ font-weight:bold;
0179         /*@editable*/ line-height:100%;
0180         /*@editable*/ letter-spacing:normal;
0181         margin-top:0;
0182         margin-right:0;
0183         margin-bottom:10px;
0184         margin-left:0;
0185         /*@editable*/ text-align:left;
0186       }
0187 
0188       /**
0189              * @tab Page
0190              * @section heading 2
0191              * @tip Set the styling for all second-level headings in your emails.
0192              * @style heading 2
0193              */
0194       h2{
0195     /*@editable*/ color:#404040 !important;
0196     display:block;
0197     /*@editable*/ font-family:"Lato", Helvetica, Arial, sans-serif;
0198         /*@editable*/ font-size:20px;
0199         /*@editable*/ font-style:normal;
0200         /*@editable*/ font-weight:bold;
0201         /*@editable*/ line-height:100%;
0202         /*@editable*/ letter-spacing:normal;
0203         margin-top:0;
0204         margin-right:0;
0205         margin-bottom:10px;
0206         margin-left:0;
0207         /*@editable*/ text-align:left;
0208       }
0209 
0210       /**
0211              * @tab Page
0212              * @section heading 3
0213              * @tip Set the styling for all third-level headings in your emails.
0214              * @style heading 3
0215              */
0216       h3{
0217     /*@editable*/ color:#606060 !important;
0218     display:block;
0219     /*@editable*/ font-family:"Lato", Helvetica, Arial, sans-serif;
0220         /*@editable*/ font-size:16px;
0221         /*@editable*/ font-style:italic;
0222         /*@editable*/ font-weight:normal;
0223         /*@editable*/ line-height:100%;
0224         /*@editable*/ letter-spacing:normal;
0225         margin-top:0;
0226         margin-right:0;
0227         margin-bottom:10px;
0228         margin-left:0;
0229         /*@editable*/ text-align:left;
0230       }
0231 
0232       /**
0233              * @tab Page
0234              * @section heading 4
0235              * @tip Set the styling for all fourth-level headings in your emails. These should be the smallest of your headings.
0236              * @style heading 4
0237              */
0238       h4{
0239     /*@editable*/ color:#808080 !important;
0240     display:block;
0241     /*@editable*/ font-family:"Lato", Helvetica, Arial, sans-serif;
0242         /*@editable*/ font-size:14px;
0243         /*@editable*/ font-style:italic;
0244         /*@editable*/ font-weight:normal;
0245         /*@editable*/ line-height:100%;
0246         /*@editable*/ letter-spacing:normal;
0247         margin-top:0;
0248         margin-right:0;
0249         margin-bottom:10px;
0250         margin-left:0;
0251         /*@editable*/ text-align:left;
0252       }
0253 
0254       /* ========== Header Styles ========== */
0255 
0256       /**
0257              * @tab Header
0258              * @section preheader style
0259              * @tip Set the background color and bottom border for your email's preheader area.
0260              * @theme header
0261              */
0262       #templatePreheader{
0263         /*@editable*/ background-color:#34495c;
0264         /*@editable*/ border-bottom:1px solid #CCCCCC;
0265       }
0266 
0267       /**
0268              * @tab Header
0269              * @section preheader text
0270              * @tip Set the styling for your email's preheader text. Choose a size and color that is easy to read.
0271              */
0272       .preheaderContent{
0273     /*@editable*/ color:#ffffff;
0274     /*@editable*/ font-family:"Lato", Helvetica, Arial, sans-serif;
0275         /*@editable*/ font-size:12px;
0276         /*@editable*/ line-height:125%;
0277         /*@editable*/ text-align:right;
0278       }
0279 
0280       /**
0281              * @tab Header
0282              * @section preheader link
0283              * @tip Set the styling for your email's preheader links. Choose a color that helps them stand out from your text.
0284              */
0285       .preheaderContent a:link, .preheaderContent a:visited, /* Yahoo! Mail Override */ .preheaderContent a .yshortcuts /* Yahoo! Mail Override */{
0286     /*@editable*/ color:#606060;
0287     /*@editable*/ font-weight:normal;
0288         /*@editable*/ text-decoration:underline;
0289       }
0290 
0291       /**
0292              * @tab Header
0293              * @section header style
0294              * @tip Set the background color and borders for your email's header area.
0295              * @theme header
0296              */
0297       #templateHeader{
0298         /*@editable*/ background-color:#F4F4F4;
0299         /*@editable*/ border-top:1px solid #FFFFFF;
0300         /*@editable*/ border-bottom:1px solid #CCCCCC;
0301       }
0302 
0303       /**
0304              * @tab Header
0305              * @section header text
0306              * @tip Set the styling for your email's header text. Choose a size and color that is easy to read.
0307              */
0308       .headerContent{
0309     /*@editable*/ color:#505050;
0310     /*@editable*/ font-family:"Lato", Helvetica, Arial, sans-serif;
0311         /*@editable*/ font-size:20px;
0312         /*@editable*/ font-weight:bold;
0313         /*@editable*/ line-height:100%;
0314         /*@editable*/ padding-top:0;
0315         /*@editable*/ padding-right:0;
0316         /*@editable*/ padding-bottom:0;
0317         /*@editable*/ padding-left:0;
0318         /*@editable*/ text-align:left;
0319         /*@editable*/ vertical-align:middle;
0320       }
0321 
0322       /**
0323              * @tab Header
0324              * @section header link
0325              * @tip Set the styling for your email's header links. Choose a color that helps them stand out from your text.
0326              */
0327       .headerContent a:link, .headerContent a:visited, /* Yahoo! Mail Override */ .headerContent a .yshortcuts /* Yahoo! Mail Override */{
0328     /*@editable*/ color:#EB4102;
0329     /*@editable*/ font-weight:normal;
0330         /*@editable*/ text-decoration:underline;
0331       }
0332 
0333       #headerImage{
0334         height:auto;
0335         max-width:600px;
0336       }
0337 
0338       /* ========== Body Styles ========== */
0339 
0340       /**
0341              * @tab Body
0342              * @section body style
0343              * @tip Set the background color and borders for your email's body area.
0344              */
0345       #templateBody{
0346         /*@editable*/ background-color:#F4F4F4;
0347         /*@editable*/ border-top:1px solid #FFFFFF;
0348         /*@editable*/ border-bottom:1px solid #CCCCCC;
0349       }
0350 
0351       /**
0352              * @tab Body
0353              * @section body text
0354              * @tip Set the styling for your email's main content text. Choose a size and color that is easy to read.
0355              * @theme main
0356              */
0357       .bodyContent{
0358     /*@editable*/ color:#505050;
0359     /*@editable*/ font-family:"Lato", Helvetica, Arial, sans-serif;
0360         /*@editable*/ font-size:14px;
0361         /*@editable*/ line-height:150%;
0362         padding-top:20px;
0363         padding-right:20px;
0364         padding-bottom:20px;
0365         padding-left:20px;
0366         /*@editable*/ text-align:left;
0367       }
0368 
0369       /**
0370              * @tab Body
0371              * @section body link
0372              * @tip Set the styling for your email's main content links. Choose a color that helps them stand out from your text.
0373              */
0374       .bodyContent a:link, .bodyContent a:visited, /* Yahoo! Mail Override */ .bodyContent a .yshortcuts /* Yahoo! Mail Override */{
0375     /*@editable*/ color:#EB4102;
0376     /*@editable*/ font-weight:normal;
0377         /*@editable*/ text-decoration:underline;
0378       }
0379 
0380       .bodyContent img{
0381     display:inline;
0382     height:auto;
0383     max-width:560px;
0384       }
0385 
0386       /* ========== Footer Styles ========== */
0387 
0388       /**
0389              * @tab Footer
0390              * @section footer style
0391              * @tip Set the background color and borders for your email's footer area.
0392              * @theme footer
0393              */
0394       #templateFooter{
0395         /*@editable*/ background-color:#34495c;
0396         /*@editable*/ border-top:1px solid #FFFFFF;
0397       }
0398 
0399       /**
0400              * @tab Footer
0401              * @section footer text
0402              * @tip Set the styling for your email's footer text. Choose a size and color that is easy to read.
0403              * @theme footer
0404              */
0405       .footerContent{
0406     /*@editable*/ color:#ffffff;
0407     /*@editable*/ font-family:"Lato", Helvetica, Arial, sans-serif;
0408         /*@editable*/ font-size:10px;
0409         /*@editable*/ line-height:150%;
0410         padding-top:20px;
0411         padding-right:20px;
0412         padding-bottom:20px;
0413         padding-left:20px;
0414         /*@editable*/ text-align:left;
0415       }
0416 
0417       /**
0418              * @tab Footer
0419              * @section footer link
0420              * @tip Set the styling for your email's footer links. Choose a color that helps them stand out from your text.
0421              */
0422       .footerContent a:link, .footerContent a:visited, /* Yahoo! Mail Override */ .footerContent a .yshortcuts, .footerContent a span /* Yahoo! Mail Override */{
0423     /*@editable*/ color:#ffffff;
0424     /*@editable*/ font-weight:normal;
0425         /*@editable*/ text-decoration:underline;
0426       }
0427 
0428       /* /\/\/\/\/\/\/\/\/ MOBILE STYLES /\/\/\/\/\/\/\/\/ */
0429 
0430             @media only screen and (max-width: 480px){
0431     /* /\/\/\/\/\/\/ CLIENT-SPECIFIC MOBILE STYLES /\/\/\/\/\/\/ */
0432     body, table, td, p, a, li, blockquote{-webkit-text-size-adjust:none !important;} /* Prevent Webkit platforms from changing default text sizes */
0433                 body{width:100% !important; min-width:100% !important;} /* Prevent iOS Mail from adding padding to the body */
0434 
0435         /* /\/\/\/\/\/\/ MOBILE RESET STYLES /\/\/\/\/\/\/ */
0436         #bodyCell{padding:10px !important;}
0437 
0438         /* /\/\/\/\/\/\/ MOBILE TEMPLATE STYLES /\/\/\/\/\/\/ */
0439 
0440         /* ======== Page Styles ======== */
0441 
0442         /**
0443                  * @tab Mobile Styles
0444                  * @section template width
0445                  * @tip Make the template fluid for portrait or landscape view adaptability. If a fluid layout doesn't work for you, set the width to 300px instead.
0446                  */
0447         #templateContainer{
0448           max-width:600px !important;
0449           /*@editable*/ width:100% !important;
0450         }
0451 
0452         /**
0453                  * @tab Mobile Styles
0454                  * @section heading 1
0455                  * @tip Make the first-level headings larger in size for better readability on small screens.
0456                  */
0457         h1{
0458     /*@editable*/ font-size:24px !important;
0459           /*@editable*/ line-height:100% !important;
0460         }
0461 
0462         /**
0463                  * @tab Mobile Styles
0464                  * @section heading 2
0465                  * @tip Make the second-level headings larger in size for better readability on small screens.
0466                  */
0467         h2{
0468     /*@editable*/ font-size:20px !important;
0469           /*@editable*/ line-height:100% !important;
0470         }
0471 
0472         /**
0473                  * @tab Mobile Styles
0474                  * @section heading 3
0475                  * @tip Make the third-level headings larger in size for better readability on small screens.
0476                  */
0477         h3{
0478     /*@editable*/ font-size:18px !important;
0479           /*@editable*/ line-height:100% !important;
0480         }
0481 
0482         /**
0483                  * @tab Mobile Styles
0484                  * @section heading 4
0485                  * @tip Make the fourth-level headings larger in size for better readability on small screens.
0486                  */
0487         h4{
0488     /*@editable*/ font-size:16px !important;
0489           /*@editable*/ line-height:100% !important;
0490         }
0491 
0492         /* ======== Header Styles ======== */
0493 
0494         #templatePreheader{display:none !important;} /* Hide the template preheader to save space */
0495 
0496         /**
0497                  * @tab Mobile Styles
0498                  * @section header image
0499                  * @tip Make the main header image fluid for portrait or landscape view adaptability, and set the image's original width as the max-width. If a fluid setting doesn't work, set the image width to half its original size instead.
0500                  */
0501         #headerImage{
0502           height:auto !important;
0503           /*@editable*/ max-width:600px !important;
0504           /*@editable*/ width:100% !important;
0505         }
0506 
0507         /**
0508                  * @tab Mobile Styles
0509                  * @section header text
0510                  * @tip Make the header content text larger in size for better readability on small screens. We recommend a font size of at least 16px.
0511                  */
0512         .headerContent{
0513     /*@editable*/ font-size:20px !important;
0514           /*@editable*/ line-height:125% !important;
0515         }
0516 
0517         /* ======== Body Styles ======== */
0518 
0519         /**
0520                  * @tab Mobile Styles
0521                  * @section body text
0522                  * @tip Make the body content text larger in size for better readability on small screens. We recommend a font size of at least 16px.
0523                  */
0524         .bodyContent{
0525     /*@editable*/ font-size:18px !important;
0526           /*@editable*/ line-height:125% !important;
0527         }
0528 
0529         /* ======== Footer Styles ======== */
0530 
0531         /**
0532                  * @tab Mobile Styles
0533                  * @section footer text
0534                  * @tip Make the body content text larger in size for better readability on small screens.
0535                  */
0536         .footerContent{
0537     /*@editable*/ font-size:14px !important;
0538           /*@editable*/ line-height:115% !important;
0539         }
0540 
0541         .footerContent a{display:block !important;} /* Place footer social and utility links on their own lines, for easier access */
0542       }
0543     </style>
0544     </head>
0545     <body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0">
0546       <center>
0547           <table align="center" border="0" cellpadding="0" cellspacing="0" height="100%" width="100%" id="bodyTable">
0548               <tr>
0549                   <td align="center" valign="top" id="bodyCell">
0550                       <!-- BEGIN TEMPLATE // -->
0551                         <table border="0" cellpadding="0" cellspacing="0" id="templateContainer">
0552                           <tr>
0553                               <td align="center" valign="top">
0554                                 </td>
0555                             </tr>
0556                           <tr>
0557                               <td align="center" valign="top">
0558                                     <table border="0" cellpadding="0" cellspacing="0" width="100%" id="templateBody">
0559                                         <tr>
0560                                             <td valign="top" class="bodyContent">
0561 
0562                                             $textReplacedVars
0563 
0564                                             </td>
0565                                         </tr>
0566                                     </table>
0567                                 </td>
0568                             </tr>
0569                           <tr>
0570                               <td align="center" valign="top">
0571                                     <table border="0" cellpadding="0" cellspacing="0" width="100%" id="templateFooter">
0572                                         <tr>
0573                                             <td valign="top" class="footerContent">
0574                                                 <a href="https://twitter.com/opendesktop">Follow on Twitter</a>&nbsp;&nbsp;&nbsp;<a href="https://www.facebook.com/opendesktop.org">Follow on Facebook</a>&nbsp;
0575                                             </td>
0576                                         </tr>
0577                                         <tr>
0578                                             <td valign="top" class="footerContent" style="padding-top:0;">
0579                                             </td>
0580                                         </tr>
0581                                     </table>
0582                                 </td>
0583                             </tr>
0584                         </table>
0585                     </td>
0586                 </tr>
0587             </table>
0588         </center>
0589     </body>
0590 </html>
0591 EOT;
0592 
0593         return $returnBodyText;
0594     }
0595 
0596     private function replaceVars()
0597     {
0598         if (empty($this->_bodyText)) {
0599             return '';
0600         }
0601 
0602         $suchmuster = array_map('tplVarMapFunc', array_keys($this->_tplVars));
0603         $ersetzung = array_values($this->_tplVars);
0604 
0605         return preg_replace($suchmuster, $ersetzung, $this->_bodyText);
0606     }
0607 
0608     public function getFromMail()
0609     {
0610         return $this->_fromMail;
0611     }
0612 
0613     public function setFromMail($mail)
0614     {
0615         $this->_fromMail = $mail;
0616     }
0617 
0618     public function getFromAlias()
0619     {
0620         return $this->_fromAlias;
0621     }
0622 
0623     public function setFromAlias($alias)
0624     {
0625         $this->_fromAlias = $alias;
0626     }
0627 
0628     public function getReceiverMail()
0629     {
0630         return $this->_receiverMail;
0631     }
0632 
0633     public function setReceiverMail($mail)
0634     {
0635         $this->_receiverMail = $mail;
0636     }
0637 
0638     public function getSubject()
0639     {
0640         if (empty($this->_subject)) {
0641             return '';
0642         }
0643 
0644         $suchmuster = array_map('tplVarMapFunc', array_keys($this->_tplVars));
0645         $ersetzung = array_values($this->_tplVars);
0646 
0647         return preg_replace($suchmuster, $ersetzung, $this->_subject);
0648     }
0649 
0650     public function setSubject($subject)
0651     {
0652         $this->_subject = $subject;
0653     }
0654 
0655     /**
0656      * @param Zend_Mail_Transport_Abstract $transport
0657      *
0658      * @return string
0659      */
0660     public static function generateEmailFilename($transport)
0661     {
0662         return 'mail_' . time() . '_' . $transport->recipients  . '.eml';
0663     }
0664 
0665 }