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