File indexing completed on 2025-03-02 05:29:09
0001 <?php 0002 /** 0003 * ocs-apiserver 0004 * 0005 * Copyright 2016 by pling GmbH. 0006 * 0007 * This file is part of ocs-apiserver. 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 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 * @return bool 0044 */ 0045 public function testForAuthCodeExist($url, $authCode) 0046 { 0047 if (true == empty($url)) { 0048 return false; 0049 } 0050 0051 $url = $this->addDefaultScheme($url); 0052 0053 $httpClient = $this->getHttpClient(); 0054 0055 $uri = $this->getAuthFileUri($url); 0056 0057 $httpClient->setUri($uri); 0058 $response = $this->retrieveBody($httpClient); 0059 0060 if (false === $response) { 0061 $httpClient->setUri($url); 0062 $response = $this->retrieveBody($httpClient); 0063 if (false === $response) { 0064 Zend_Registry::get('logger')->err(__METHOD__ . " - Error while validate AuthCode for Website: " . $url . ".\n Server replay was: " . $httpClient->getLastResponse()->getStatus() . ". " . $httpClient->getLastResponse()->getMessage() . PHP_EOL); 0065 return false; 0066 } 0067 } 0068 return (strpos($response, $authCode) !== false) ? true : false; 0069 } 0070 0071 /** 0072 * @param string $url 0073 * @param Zend_Db_Table_Row_Abstract $dataRow 0074 * @return bool 0075 */ 0076 public function validateAuthCode($url, $dataRow) 0077 { 0078 if (true == empty($url)) { 0079 return false; 0080 } 0081 0082 if (false == $this->validateUrlMemberData($url, $dataRow)) { 0083 return false; 0084 } 0085 0086 $url = $this->addDefaultScheme($url); 0087 0088 $httpClient = $this->getHttpClient(); 0089 $httpClient->setUri($this->getAuthFileUri($url)); 0090 0091 $response = $this->retrieveBody($httpClient); 0092 0093 if (false === $response) { 0094 $httpClient->setUri($url); 0095 $response = $this->retrieveBody($httpClient); 0096 if (false === $response) { 0097 Zend_Registry::get('logger')->err(__METHOD__ . " - Error while validate AuthCode for Website: " . $url . ".\n Server replay was: " . $httpClient->getLastResponse()->getStatus() . ". " . $httpClient->getLastResponse()->getMessage() . PHP_EOL); 0098 return false; 0099 } 0100 } 0101 return (strpos($response, $this->generateAuthCode($url)) !== false) ? true : false; 0102 } 0103 0104 public function validateUrlMemberData($url, $dataRow) 0105 { 0106 $result = false; 0107 $memberTable = new Application_Model_Member(); 0108 /** @var Zend_Db_Table_Row $rowMember */ 0109 $rowMember = $memberTable->find($dataRow)->current(); 0110 if ($rowMember->link_website == $url) { 0111 $result = true; 0112 } 0113 return $result; 0114 } 0115 0116 /** 0117 * @return Zend_Http_Client 0118 */ 0119 public function getHttpClient() 0120 { 0121 $httpClient = new Zend_Http_Client(); 0122 $httpClient->setConfig($this->_config); 0123 return $httpClient; 0124 } 0125 0126 /** 0127 * @param string $domain 0128 * @return string 0129 */ 0130 public function getAuthFileUri($domain) 0131 { 0132 return $domain . '/' . $this->getAuthFileName($domain); 0133 } 0134 0135 /** 0136 * @param string $url 0137 * @param string $scheme 0138 * @return string 0139 */ 0140 public function addDefaultScheme($url, $scheme = 'http://') 0141 { 0142 if (false == preg_match("~^(?:f|ht)tps?://~i", $url)) { 0143 $url = $scheme . $url; 0144 } 0145 return $url; 0146 } 0147 0148 /** 0149 * @param string $domain 0150 * @return string 0151 */ 0152 public function getAuthFileName($domain) 0153 { 0154 return self::FILE_PREFIX . $this->generateAuthCode($domain) . self::FILE_POSTFIX; 0155 } 0156 0157 /** 0158 * @param string $domain 0159 * @return null|string 0160 */ 0161 public function generateAuthCode($domain) 0162 { 0163 if (empty($domain)) { 0164 return null; 0165 } 0166 return md5($this->_parseDomain($domain) . self::SALT_KEY); 0167 } 0168 0169 protected function _parseDomain($domain) 0170 { 0171 $count = preg_match_all("/^(?:(?:http|https):\/\/)?([\da-zA-ZäüöÄÖÜ\.-]+\.[a-z\.]{2,6})[\/\w \.-]*\/?$/", $domain, $matches); 0172 if ($count > 0) { 0173 return current($matches[1]); 0174 } else { 0175 Zend_Registry::get('logger')->err(__METHOD__ . ' - Error while parsing the domain = ' . $domain); 0176 return ''; 0177 } 0178 } 0179 0180 /** 0181 * @param Zend_Http_Client $httpClient 0182 * @return bool 0183 */ 0184 public function retrieveBody($httpClient) 0185 { 0186 $response = $httpClient->request(); 0187 0188 if ($response->isError()) { 0189 return false; 0190 } else { 0191 return $response->getBody(); 0192 } 0193 } 0194 0195 public function parseDomain($domain) 0196 { 0197 return $this->_parseDomain($domain); 0198 } 0199 0200 /** 0201 * @return array 0202 */ 0203 public function getConfig() 0204 { 0205 return $this->_config; 0206 } 0207 0208 /** 0209 * @param $config 0210 */ 0211 public function setConfig($config) 0212 { 0213 $this->_config = $config; 0214 } 0215 0216 /** 0217 * @param $memberId 0218 * @param $verificationResult 0219 */ 0220 public function updateData($memberId, $verificationResult) 0221 { 0222 $modelMember = new Application_Model_Member(); 0223 /** @var Zend_Db_Table_Row $rowMember */ 0224 $rowMember = $modelMember->find($memberId)->current(); 0225 if (count($rowMember->toArray()) == 0) { 0226 return; 0227 } 0228 $rowMember->validated_at = new Zend_Db_Expr('NOW()'); 0229 $rowMember->validated = (int)$verificationResult; 0230 $rowMember->save(); 0231 } 0232 0233 }