File indexing completed on 2024-10-13 13:29:13

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *    GNU Affero General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Affero General Public License
0021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  *
0023  *    Created: 22.09.2016
0024  **/
0025 class Application_Model_MemberEmail
0026 {
0027     const CASE_INSENSITIVE = 1;
0028     /** @var string */
0029     protected $_dataTableName;
0030     /** @var  Application_Model_DbTable_MemberEmail */
0031     protected $_dataTable;
0032 
0033     /**
0034      * @inheritDoc
0035      */
0036     public function __construct($_dataTableName = 'Application_Model_DbTable_MemberEmail')
0037     {
0038         $this->_dataTableName = $_dataTableName;
0039         $this->_dataTable = new $this->_dataTableName;
0040     }
0041 
0042     /**
0043      * @param int  $member_id
0044      * @param bool $email_deleted
0045      *
0046      * @return array
0047      * @throws Zend_Db_Statement_Exception
0048      * @throws Zend_Db_Table_Exception
0049      */
0050     public function fetchAllMailAddresses($member_id, $email_deleted = false)
0051     {
0052         $deleted = $email_deleted === true ? Application_Model_DbTable_MemberEmail::EMAIL_DELETED
0053             : Application_Model_DbTable_MemberEmail::EMAIL_NOT_DELETED;
0054         $sql =
0055             "SELECT * FROM {$this->_dataTable->info('name')} WHERE `email_member_id` = :memberId AND `email_deleted` = :emailDeleted";
0056         $stmnt = $this->_dataTable->getAdapter()->query($sql, array('memberId' => $member_id, 'emailDeleted' => $deleted));
0057 
0058         return $stmnt->fetchAll();
0059     }
0060 
0061     /**
0062      * @param $emailId
0063      * @param $member_id
0064      *
0065      * @return bool
0066      * @throws Zend_Db_Statement_Exception
0067      * @throws Zend_Db_Table_Exception
0068      * @throws Zend_Exception
0069      */
0070     public function setDefaultEmail($emailId, $member_id)
0071     {
0072         $result = $this->resetDefaultMailAddress($member_id);
0073         $this->_dataTable->setPrimary($emailId);
0074         $this->updateMemberPrimaryMail($member_id); /* if we change the mail in member table, we change the login. */
0075 
0076         return true;
0077     }
0078 
0079     /**
0080      * @param $member_id
0081      *
0082      * @return bool
0083      * @throws Zend_Db_Statement_Exception
0084      */
0085     private function resetDefaultMailAddress($member_id)
0086     {
0087         $sql = "UPDATE `member_email` SET `email_primary` = 0 WHERE `email_member_id` = :member_id AND `email_primary` = 1";
0088 
0089         return $this->_dataTable->getAdapter()->query($sql, array('member_id' => $member_id))->execute();
0090     }
0091 
0092     /**
0093      * @param $member_id
0094      *
0095      * @return mixed
0096      * @throws Zend_Db_Statement_Exception
0097      * @throws Zend_Db_Table_Exception
0098      */
0099     private function updateMemberPrimaryMail($member_id)
0100     {
0101         $dataEmail = $this->fetchMemberPrimaryMail($member_id);
0102 
0103         return $this->saveMemberPrimaryMail($member_id, $dataEmail);
0104     }
0105 
0106     /**
0107      * @param $member_id
0108      *
0109      * @return mixed
0110      * @throws Zend_Db_Table_Exception
0111      */
0112     public function fetchMemberPrimaryMail($member_id)
0113     {
0114         $sql = "SELECT * FROM {$this->_dataTable->info('name')} WHERE email_member_id = :member_id AND email_primary = 1";
0115         $dataEmail = $this->_dataTable->getAdapter()->fetchRow($sql, array('member_id' => $member_id));
0116 
0117         return $dataEmail;
0118     }
0119 
0120     /**
0121      * @param $member_id
0122      * @param $dataEmail
0123      *
0124      * @return mixed
0125      * @throws Zend_Db_Statement_Exception
0126      */
0127     protected function saveMemberPrimaryMail($member_id, $dataEmail)
0128     {
0129         $modelMember = new Application_Model_Member();
0130         $dataMember = $modelMember->fetchMemberData($member_id);
0131         $dataMember->mail = $dataEmail['email_address'];
0132         $dataMember->mail_checked = isset($dataEmail['email_checked']) ? 1 : 0;
0133 
0134         return $dataMember->save();
0135     }
0136 
0137     /**
0138      * @param string $verification
0139      *
0140      * @return int count of updated rows
0141      * @throws Zend_Db_Statement_Exception
0142      */
0143     public function verificationEmail($verification)
0144     {
0145         $sql =
0146             "UPDATE `member_email` SET `email_checked` = NOW() WHERE `email_verification_value` = :verification AND `email_deleted` = 0 AND `email_checked` IS NULL";
0147         $stmnt = $this->_dataTable->getAdapter()->query($sql, array('verification' => $verification));
0148 
0149         return $stmnt->rowCount();
0150     }
0151 
0152     /**
0153      * @param int         $user_id
0154      * @param string      $user_mail
0155      * @param null|string $user_verification
0156      *
0157      * @return Zend_Db_Table_Row_Abstract
0158      * @throws Exception
0159      */
0160     public function saveEmail($user_id, $user_mail, $user_verification = null)
0161     {
0162         $data = array();
0163         $data['email_member_id'] = $user_id;
0164         $data['email_address'] = $user_mail;
0165         $data['email_hash'] = md5($user_mail);
0166         $data['email_verification_value'] =
0167             empty($user_verification) ? Application_Model_MemberEmail::getVerificationValue($user_id, $user_mail) : $user_verification;
0168 
0169         return $this->_dataTable->save($data);
0170     }
0171 
0172     /**
0173      * @param int    $user_name
0174      * @param string $member_email
0175      *
0176      * @return string
0177      */
0178     public static function getVerificationValue($user_name, $member_email)
0179     {
0180         return md5($user_name . $member_email . time());
0181     }
0182 
0183     public static function getHashForMailAddress($mail_address)
0184     {
0185         return md5($mail_address);
0186     }
0187 
0188     /**
0189      * @param int         $user_id
0190      * @param string      $user_mail
0191      * @param int         $user_mail_checked
0192      * @param null|string $user_verification
0193      *
0194      * @return Zend_Db_Table_Row_Abstract
0195      * @throws Zend_Db_Statement_Exception
0196      * @throws Zend_Db_Table_Exception
0197      * @throws Zend_Exception
0198      * @throws Exception
0199      */
0200     public function saveEmailAsPrimary($user_id, $user_mail, $user_mail_checked = 0, $user_verification = null)
0201     {
0202         if (empty($user_id) OR empty($user_mail)) {
0203             return false;
0204         }
0205 
0206         $data = array();
0207         $data['email_member_id'] = $user_id;
0208         $data['email_address'] = $user_mail;
0209         $data['email_hash'] = MD5($user_mail);
0210         $data['email_checked'] = $user_mail_checked == 1 ? new Zend_Db_Expr('Now()') : new Zend_Db_Expr('NULL');
0211         $data['email_verification_value'] =
0212             empty($user_verification) ? Application_Model_MemberEmail::getVerificationValue($user_id, $user_mail) : $user_verification;
0213         $data['email_primary'] = Application_Model_DbTable_MemberEmail::EMAIL_PRIMARY;
0214 
0215         $result = $this->_dataTable->save($data);
0216 
0217         $this->resetOtherPrimaryEmail($user_id, $user_mail);
0218 
0219         $this->updateMemberPrimaryMail($user_id);
0220 
0221         return $result;
0222     }
0223 
0224     private function resetOtherPrimaryEmail($user_id, $user_mail)
0225     {
0226         $sql = "
0227                 UPDATE `member_email`
0228                 SET `email_primary` = 0
0229                 WHERE `email_member_id` = :user_id AND `email_address` <> :user_mail; 
0230                 ";
0231         $result = $this->_dataTable->getAdapter()->query($sql, array('user_id' => $user_id, 'user_mail' => $user_mail));
0232 
0233     }
0234 
0235     /**
0236      * @param $member_id
0237      *
0238      * @return null
0239      * @throws Zend_Db_Table_Exception
0240      */
0241     public function getValidationValue($member_id)
0242     {
0243         $memberData = $this->fetchMemberPrimaryMail($member_id);
0244         if (count($memberData) == 0) {
0245             return null;
0246         }
0247 
0248         return $memberData['email_verification_value'];
0249     }
0250 
0251     /**
0252      * @param string $value
0253      * @param int    $test_case_sensitive
0254      *
0255      * @param array  $omitMember
0256      *
0257      * @return mixed
0258      */
0259     public function findMailAddress($value, $test_case_sensitive = self::CASE_INSENSITIVE, $omitMember = array())
0260     {
0261         $sql = "
0262             SELECT *
0263             FROM `member_email`
0264             WHERE
0265         ";
0266         if ($test_case_sensitive == self::CASE_INSENSITIVE) {
0267             $sql .= " LCASE(member_email.email_address) = LCASE(:mail_address)";
0268         } else {
0269             $sql .= " member_email.email_address = :mail_address";
0270         }
0271 
0272         if (count($omitMember) > 0) {
0273             $sql .= " AND member_email.email_member_id NOT IN (" . implode(',', $omitMember) . ")";
0274         }
0275 
0276         return $this->_dataTable->getAdapter()->fetchAll($sql, array('mail_address' => $value));
0277     }
0278 
0279     public function sendConfirmationMail($val, $verificationVal)
0280     {
0281         $confirmMail = new Default_Plugin_SendMail('tpl_verify_user');
0282         $confirmMail->setTemplateVar('servername', $this->getServerName());
0283         $confirmMail->setTemplateVar('username', $val['username']);
0284         $confirmMail->setTemplateVar('verificationlinktext',
0285             '<a href="https://' . $this->getServerName() . '/verification/' . $verificationVal
0286             . '">Click here to verify your email address</a>');
0287         $confirmMail->setTemplateVar('verificationlink',
0288             '<a href="https://' . $this->getServerName() . '/verification/' . $verificationVal . '">https://' . $this->getServerName()
0289             . '/verification/' . $verificationVal . '</a>');
0290         $confirmMail->setTemplateVar('verificationurl', 'https://' . $this->getServerName() . '/verification/' . $verificationVal);
0291         $confirmMail->setReceiverMail($val['mail']);
0292         $confirmMail->setFromMail('registration@opendesktop.org');
0293         $confirmMail->send();
0294     }
0295 
0296     private function getServerName()
0297     {
0298         /** @var Zend_Controller_Request_Http $request */
0299         $request = Zend_Controller_Front::getInstance()->getRequest();
0300 
0301         return $request->getHttpHost();
0302     }
0303 
0304 }