File indexing completed on 2024-12-22 05:36:24
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 class Local_Verification_WebsiteOwner 0024 { 0025 0026 const SALT_KEY = 'MakeItAndPlingIt'; 0027 const FILE_PREFIX = 'pling'; 0028 const FILE_POSTFIX = '.html'; 0029 0030 /** 0031 * Configuration for HTTP-Client 0032 * 0033 * @var array 0034 */ 0035 protected $_config = array( 0036 'maxredirects' => 0, 0037 'timeout' => 30 0038 ); 0039 0040 /** 0041 * @param string $url 0042 * @param string $authCode 0043 * 0044 * @return bool 0045 * @throws Zend_Exception 0046 * @throws Zend_Http_Client_Exception 0047 */ 0048 public function testForAuthCodeExist($url, $authCode) 0049 { 0050 if (true == empty($url)) { 0051 return false; 0052 } 0053 0054 $url = $this->addDefaultScheme($url); 0055 0056 $httpClient = $this->getHttpClient(); 0057 0058 $uri = $this->getAuthFileUri($url); 0059 0060 $httpClient->setUri($uri); 0061 $response = $this->retrieveBody($httpClient); 0062 0063 if (false === $response) { 0064 $httpClient->setUri($url); 0065 $response = $this->retrieveBody($httpClient); 0066 if (false === $response) { 0067 Zend_Registry::get('logger')->err(__METHOD__ . " - Error while validate AuthCode for Website: " . $url 0068 . ".\n Server replay was: " . $httpClient->getLastResponse()->getStatus() . ". " . $httpClient->getLastResponse() 0069 ->getMessage() 0070 . PHP_EOL) 0071 ; 0072 0073 return false; 0074 } 0075 } 0076 0077 return (strpos($response, $authCode) !== false) ? true : false; 0078 } 0079 0080 /** 0081 * @param string $url 0082 * @param string $scheme 0083 * 0084 * @return string 0085 */ 0086 public function addDefaultScheme($url, $scheme = 'http://') 0087 { 0088 if (false == preg_match("~^(?:f|ht)tps?://~i", $url)) { 0089 $url = $scheme . $url; 0090 } 0091 0092 return $url; 0093 } 0094 0095 /** 0096 * @return Zend_Http_Client 0097 * @throws Zend_Http_Client_Exception 0098 */ 0099 public function getHttpClient() 0100 { 0101 $httpClient = new Zend_Http_Client(); 0102 $httpClient->setConfig($this->_config); 0103 0104 return $httpClient; 0105 } 0106 0107 /** 0108 * @param string $domain 0109 * 0110 * @return string 0111 * @throws Zend_Exception 0112 */ 0113 public function getAuthFileUri($domain) 0114 { 0115 return $domain . '/' . $this->getAuthFileName($domain); 0116 } 0117 0118 /** 0119 * @param string $domain 0120 * 0121 * @return string 0122 * @throws Zend_Exception 0123 */ 0124 public function getAuthFileName($domain) 0125 { 0126 return self::FILE_PREFIX . $this->generateAuthCode($domain) . self::FILE_POSTFIX; 0127 } 0128 0129 /** 0130 * @param string $domain 0131 * 0132 * @return null|string 0133 * @throws Zend_Exception 0134 */ 0135 public function generateAuthCode($domain) 0136 { 0137 if (empty($domain)) { 0138 return null; 0139 } 0140 0141 return md5($this->_parseDomain($domain) . self::SALT_KEY); 0142 } 0143 0144 /** 0145 * @param $domain 0146 * 0147 * @return mixed|string 0148 * @throws Zend_Exception 0149 */ 0150 protected function _parseDomain($domain) 0151 { 0152 $count = preg_match_all("/^(?:(?:http|https):\/\/)?([\da-zA-ZäüöÄÖÜ\.-]+\.[a-z\.]{2,6})[\/\w \.-]*\/?$/", $domain, $matches); 0153 if ($count > 0) { 0154 return current($matches[1]); 0155 } else { 0156 Zend_Registry::get('logger')->err(__METHOD__ . ' - Error while parsing the domain = ' . $domain); 0157 0158 return ''; 0159 } 0160 } 0161 0162 /** 0163 * @param Zend_Http_Client $httpClient 0164 * 0165 * @return bool 0166 * @throws Zend_Http_Client_Exception 0167 */ 0168 public function retrieveBody($httpClient) 0169 { 0170 $response = $httpClient->request(); 0171 0172 if ($response->isError()) { 0173 return false; 0174 } else { 0175 return $response->getBody(); 0176 } 0177 } 0178 0179 /** 0180 * @param string $url 0181 * @param Zend_Db_Table_Row_Abstract $dataRow 0182 * 0183 * @return bool 0184 * @throws Zend_Db_Table_Exception 0185 * @throws Zend_Exception 0186 * @throws Zend_Http_Client_Exception 0187 */ 0188 public function validateAuthCode($url, $dataRow) 0189 { 0190 if (true == empty($url)) { 0191 return false; 0192 } 0193 0194 if (false == $this->validateUrlMemberData($url, $dataRow)) { 0195 return false; 0196 } 0197 0198 $url = $this->addDefaultScheme($url); 0199 0200 $httpClient = $this->getHttpClient(); 0201 $httpClient->setUri($this->getAuthFileUri($url)); 0202 0203 $response = $this->retrieveBody($httpClient); 0204 0205 if (false === $response) { 0206 $httpClient->setUri($url); 0207 $response = $this->retrieveBody($httpClient); 0208 if (false === $response) { 0209 Zend_Registry::get('logger')->err(__METHOD__ . " - Error while validate AuthCode for Website: " . $url 0210 . ".\n Server replay was: " . $httpClient->getLastResponse()->getStatus() . ". " . $httpClient->getLastResponse() 0211 ->getMessage() 0212 . PHP_EOL) 0213 ; 0214 0215 return false; 0216 } 0217 } 0218 0219 return (strpos($response, $this->generateAuthCode($url)) !== false) ? true : false; 0220 } 0221 0222 /** 0223 * @param string $url 0224 * @param Zend_Db_Table_Row_Abstract $dataRow 0225 * 0226 * @return bool 0227 * @throws Zend_Db_Table_Exception 0228 */ 0229 public function validateUrlMemberData($url, $dataRow) 0230 { 0231 $result = false; 0232 $memberTable = new Default_Model_Member(); 0233 /** @var Zend_Db_Table_Row $rowMember */ 0234 $rowMember = $memberTable->find($dataRow)->current(); 0235 if ($rowMember->link_website == $url) { 0236 $result = true; 0237 } 0238 0239 return $result; 0240 } 0241 0242 /** 0243 * @param string $domain 0244 * 0245 * @return mixed|string 0246 * @throws Zend_Exception 0247 */ 0248 public function parseDomain($domain) 0249 { 0250 return $this->_parseDomain($domain); 0251 } 0252 0253 /** 0254 * @return array 0255 */ 0256 public function getConfig() 0257 { 0258 return $this->_config; 0259 } 0260 0261 /** 0262 * @param $config 0263 */ 0264 public function setConfig($config) 0265 { 0266 $this->_config = $config; 0267 } 0268 0269 /** 0270 * @param $memberId 0271 * @param $verificationResult 0272 * 0273 * @throws Zend_Db_Table_Exception 0274 */ 0275 public function updateData($memberId, $verificationResult) 0276 { 0277 $modelMember = new Default_Model_Member(); 0278 /** @var Zend_Db_Table_Row $rowMember */ 0279 $rowMember = $modelMember->find($memberId)->current(); 0280 if (count($rowMember->toArray()) == 0) { 0281 return; 0282 } 0283 $rowMember->validated_at = new Zend_Db_Expr('NOW()'); 0284 $rowMember->validated = (int)$verificationResult; 0285 $rowMember->save(); 0286 } 0287 0288 }