| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace CodeBlog\ToWebP\Convert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Class Make | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @author Whallysson Avelino <https://github.com/whallysson> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @package CodeBlog\ToWebP\Convert | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | class Make | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     /** @var string */ | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     protected $path; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     /** @var string */ | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     protected $name; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * Make constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * @param string $uploadDir | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * @param string $fileTypeDir | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     public function __construct(string $uploadDir, string $fileTypeDir) | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         $this->dir($uploadDir); | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         $this->dir("{$uploadDir}/{$fileTypeDir}"); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         $this->path("{$uploadDir}/{$fileTypeDir}"); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * @param string $string | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     protected function name(string $string) | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $info = pathinfo(filter_var(mb_strtolower($string), FILTER_SANITIZE_STRIPPED)); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $formats = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜüÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿRr"!@#$%&*()_-+={[}]/?;:.,\\\'<>°ºª¹²³'; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $replace = 'aaaaaaaceeeeiiiidnoooooouuuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyRr                                    '; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $name = str_replace(["-----", "----", "---", "--"], "-", | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |             str_replace(" ", "-", trim(strtr(utf8_decode($info['filename']), utf8_decode($formats), $replace)))); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $this->name = "{$name}.{$info['extension']}"; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         return $this->name; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * @param string $dir | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * @param int $mode | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     protected function dir(string $dir, int $mode = 0775) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         if (!file_exists($dir)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             // Trying to create the given folder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             if (!mkdir($dir, $mode, true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 throw new \Exception('Failed creating folder: ' . $dir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             chmod($dir, $mode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         // Checks if there's a file in $filePath & if writing permissions are correct | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if (file_exists($dir) && !is_writable($dir)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             throw new \Exception('Cannot overwrite ' . basename($dir) . ' - check file permissions.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  |         return; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * @param string $path | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     protected function path(string $path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         list($yearPath, $mothPath) = explode("/", date("Y/m")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $this->dir("{$path}/{$yearPath}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $this->dir("{$path}/{$yearPath}/{$mothPath}"); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |         $this->path = "{$path}/{$yearPath}/{$mothPath}"; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |  |