Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use Fleshgrinder\Validator\URL as URLValidator;
- use App\Exceptions\APIException;
- // ...
- private function validate_url($url_from_request) {
- if (is_null($url_from_request)) {
- throw new APIException('URL is empty');
- }
- $url = \filter_url($url_from_request);
- if(empty($url)) {
- throw new APIException('After sanitization URL is empty');
- }
- if (\mb_strlen($url) > 2049) {
- throw new APIException('URL Too Long');
- }
- if (!filter_var($url, FILTER_VALIDATE_URL)) {
- throw new APIException('URL is invalid (check #1)');
- }
- try {
- $url_validation = new URLValidator($url);
- } catch (\Exception $e) {
- $message = $e->getMessage();
- $message = str_replace('[' . $url . '] ', '', $message);
- throw new APIException($message . ' (check #2)');
- }
- $url_lower = \mb_strtolower($url);
- $url_lower = str_replace('www.', '', $url_lower);
- foreach($this->settings['blocked_domains'] as $domain) {
- $domain = '://' . $domain . '/';
- if (\mb_strpos($url_lower, $domain) !== false) {
- throw new APIException('This domain blocked');
- }
- }
- foreach ($this->settings['blocked_pieces'] as $piece) {
- if (\mb_strpos($url_lower, $piece) !== false) {
- throw new APIException('This URL is blocked');
- }
- }
- return $url;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement