Fibinger Ádám
2020-07-31 7e1ea4d5fb16450ce81f9b7f39784e7a84b73f9b
commit | author | age
1dcdf6 1 <?php
2
3 namespace Wargasz;
4
5 class FileHelper
6 {
7     private static $ch;
8
a9e494 9     public static function downloadFiles($url, $targetFileName = '')
1dcdf6 10     {
11         $ch = self::createCURLObject($url);
12
13         $response = curl_exec($ch);
14         $errno = curl_errno($ch);
15
16         if (!$errno)
17         {
18             return (bool)file_put_contents($targetFileName, $response);
19         }
20
21         return false;
22     }
23
24     public static function normalizeString($source)
25     {
7e1ea4 26         $pattern = '/[^a-zA-Z0-9\ \.\-\_]/';
1dcdf6 27         return preg_replace($pattern, "", $source);
28     }
29
30     protected static function createCURLObject($url)
31     {
32         $options = [
33             CURLOPT_RETURNTRANSFER => 1,
34             CURLOPT_URL            => trim($url),
35             CURLOPT_SSL_VERIFYPEER => 0,
36             CURLOPT_SSL_VERIFYHOST => 0,
a9e494 37             CURLOPT_FOLLOWLOCATION => 0
1dcdf6 38         ];
39
40         if (self::$ch == null)
41         {
42             self::$ch = curl_init();
43         }
44
45         curl_setopt_array(self::$ch, $options);
46
47         return self::$ch;
48     }
a9e494 49
50     public static function getFiles($directory): array
51     {
52         $Directory = new \RecursiveDirectoryIterator($directory);
53         $Iterator = new \RecursiveIteratorIterator($Directory);
54         $Regex = new \RegexIterator($Iterator, '/^.+\.html$/i', \RecursiveRegexIterator::GET_MATCH);
55
56         $files = [];
57
58         foreach ($Regex as $file)
59         {
60             $files[] = $file;
61         }
62
63         return $files;
64     }
1dcdf6 65 }