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_WebsiteProject
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      * @throws Zend_Uri_Exception
0048      */
0049     public function testForAuthCodeExist($url, $authCode)
0050     {
0051         if (true == empty($url)) {
0052             return false;
0053         }
0054 
0055         $httpClient = $this->getHttpClient();
0056 
0057         $uri = $this->generateUri($url);
0058 
0059         $httpClient->setUri($uri);
0060         $response = $this->retrieveBody($httpClient);
0061 
0062         if (false === $response) {
0063             $httpClient->setUri($url);
0064             $response = $this->retrieveBody($httpClient);
0065             if (false === $response) {
0066                 Zend_Registry::get('logger')->err(__METHOD__ . " - Error while validate AuthCode for Website: " . $url
0067                     . ".\n Server replay was: " . $httpClient->getLastResponse()->getStatus() . ". " . $httpClient->getLastResponse()
0068                                                                                                                   ->getMessage()
0069                     . PHP_EOL)
0070                 ;
0071 
0072                 return false;
0073             }
0074         }
0075 
0076         return (strpos($response, $authCode) !== false) ? true : false;
0077     }
0078 
0079     /**
0080      * @return Zend_Http_Client
0081      * @throws Zend_Http_Client_Exception
0082      */
0083     public function getHttpClient()
0084     {
0085         $httpClient = new Zend_Http_Client();
0086         $httpClient->setConfig($this->_config);
0087 
0088         return $httpClient;
0089     }
0090 
0091     /**
0092      * @param $url
0093      *
0094      * @return Zend_Uri
0095      * @throws Zend_Uri_Exception
0096      */
0097     protected function generateUri($url)
0098     {
0099         $uri = Zend_Uri::factory($url);
0100 
0101         return $uri;
0102     }
0103 
0104     /**
0105      * @param Zend_Http_Client $httpClient
0106      *
0107      * @return bool
0108      * @throws Zend_Http_Client_Exception
0109      */
0110     public function retrieveBody($httpClient)
0111     {
0112         $response = $httpClient->request();
0113 
0114         if ($response->isError()) {
0115             return false;
0116         } else {
0117             return $response->getBody();
0118         }
0119     }
0120 
0121     /**
0122      * @param string $domain
0123      *
0124      * @return string
0125      */
0126     public function getAuthFileUri($domain)
0127     {
0128         return $domain . '/' . $this->getAuthFileName($domain);
0129     }
0130 
0131     /**
0132      * @param string $domain
0133      *
0134      * @return string
0135      */
0136     public function getAuthFileName($domain)
0137     {
0138         return self::FILE_PREFIX . $this->generateAuthCode($domain) . self::FILE_POSTFIX;
0139     }
0140 
0141     /**
0142      * @param string $domain
0143      *
0144      * @return null|string
0145      */
0146     public function generateAuthCode($domain)
0147     {
0148         if (empty($domain)) {
0149             return null;
0150         }
0151 
0152         return md5($domain . self::SALT_KEY);
0153     }
0154 
0155     /**
0156      * @return array
0157      */
0158     public function getConfig()
0159     {
0160         return $this->_config;
0161     }
0162 
0163     /**
0164      * @param $config
0165      */
0166     public function setConfig($config)
0167     {
0168         $this->_config = $config;
0169     }
0170 
0171     /**
0172      * @param $project_id
0173      * @param $verificationResult
0174      *
0175      * @throws Zend_Db_Table_Exception
0176      */
0177     public function updateData($project_id, $verificationResult)
0178     {
0179         $modelProject = new Default_Model_Project();
0180         /** @var Zend_Db_Table_Row $rowMember */
0181         $rowMember = $modelProject->find($project_id)->current();
0182         if (count($rowMember->toArray()) == 0) {
0183             return;
0184         }
0185         $rowMember->validated_at = new Zend_Db_Expr('NOW()');
0186         $rowMember->validated = (int)$verificationResult;
0187         $rowMember->save();
0188     }
0189 
0190     /**
0191      * @param $url
0192      *
0193      * @return mixed
0194      * @throws Exception
0195      */
0196     protected function _parseDomain($url)
0197     {
0198         $matches = array();
0199         $count = preg_match_all("/^(?:(?:http|https):\/\/)?([\da-zA-ZäüöÄÖÜ\.-]+\.[a-z\.]{2,6})[\/\w \.-]*\/?$/", $url, $matches);
0200         if ($count > 0) {
0201             return current($matches[1]);
0202         } else {
0203             throw new Exception(__FILE__ . '(' . __LINE__ . '): ' . 'Error while parsing url= ' . $url);
0204         }
0205     }
0206 
0207 }