Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.54 KB | None | 0 0
  1. <?php
  2.  
  3. use Fleshgrinder\Validator\URL as URLValidator;
  4.  
  5. use App\Exceptions\APIException;
  6.  
  7. // ...
  8.  
  9.     private function validate_url($url_from_request) {
  10.         if (is_null($url_from_request)) {
  11.             throw new APIException('URL is empty');
  12.         }
  13.  
  14.         $url = \filter_url($url_from_request);
  15.         if(empty($url)) {
  16.             throw new APIException('After sanitization URL is empty');
  17.         }
  18.         if (\mb_strlen($url) > 2049) {
  19.             throw new APIException('URL Too Long');
  20.         }
  21.  
  22.         if (!filter_var($url, FILTER_VALIDATE_URL)) {
  23.             throw new APIException('URL is invalid (check #1)');
  24.         }
  25.  
  26.         try {
  27.             $url_validation = new URLValidator($url);
  28.         } catch (\Exception $e) {
  29.             $message = $e->getMessage();
  30.             $message = str_replace('[' . $url . '] ', '', $message);
  31.             throw new APIException($message . ' (check #2)');
  32.         }
  33.  
  34.         $url_lower = \mb_strtolower($url);
  35.         $url_lower = str_replace('www.', '', $url_lower);
  36.  
  37.         foreach($this->settings['blocked_domains'] as $domain) {
  38.             $domain = '://' . $domain . '/';
  39.             if (\mb_strpos($url_lower, $domain) !== false) {
  40.                 throw new APIException('This domain blocked');
  41.             }
  42.         }
  43.  
  44.         foreach ($this->settings['blocked_pieces'] as $piece) {
  45.             if (\mb_strpos($url_lower, $piece) !== false) {
  46.                 throw new APIException('This URL is blocked');
  47.             }
  48.         }
  49.  
  50.         return $url;
  51.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement