| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace kalanis\kw_user_paths; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use kalanis\kw_paths\ArrayPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use kalanis\kw_paths\Interfaces\IPaths; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use kalanis\kw_paths\PathsException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use kalanis\kw_paths\Stuff; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use kalanis\kw_user_paths\Traits\TLang; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use UnexpectedValueException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Class UserDir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Work with user dirs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class UserDir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     use TLang; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected ?string $userName = null; # obtained user's name (when need) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected ?string $userPath = null; # system path to user's home dir - as set by values (contains slashes as extra info) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected string $homeDir = ''; # relative path to user's home dir (from storage root) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected string $dataDir = ''; # relative path to user's work dir (from storage root) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected ?ArrayPath $fullPath = null; # full path as derived from user path or user settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected bool $hasHomeDir = true; # if use sub dirs or is it directly in user's home dir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected bool $hasDataDir = true; # if use user dir or is it anywhere else directly from web root | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 63 |  |     public function __construct(?Interfaces\IUPTranslations $lang = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 63 |  |         $this->setUpLang($lang); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 63 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * Return relative path to home dir for accessing special dirs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |     public function getHomeDir(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |         return $this->homeDir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * Return relative path to working dir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |     public function getDataDir(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |         return $this->dataDir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Return full path class with current user's dir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @throws PathsException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @return ArrayPath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 15 |  |     public function getFullPath(): ArrayPath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 15 |  |         if (empty($this->fullPath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |             throw new PathsException($this->getUpLang()->upCannotGetFullPaths()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 14 |  |         return $this->fullPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * Clear data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 47 |  |     public function clear(): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 47 |  |         $this->userName = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 47 |  |         $this->userPath = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 47 |  |         $this->homeDir = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 47 |  |         $this->dataDir = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 47 |  |         $this->fullPath = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 47 |  |         $this->hasHomeDir = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 47 |  |         $this->hasDataDir = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 47 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * Set username as base for generating user dir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 7 |  |     public function setUserName(string $name): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 7 |  |         if (empty($name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |             throw new InvalidArgumentException($this->getUpLang()->upUserNameIsShort()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 6 |  |         if (false !== strpbrk($name, '.: /~')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |             throw new InvalidArgumentException($this->getUpLang()->upUserNameContainsChars()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 5 |  |         $this->userName = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 5 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 1 |  |     public function getUserName(): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |         return $this->userName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * Use home as data dir? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @param bool $set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @return UserDir | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 111 | 5 |  |     public function wantHomeDir(bool $set): self | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 113 | 5 |  |         $this->hasHomeDir = $set; | 
            
                                                                        
                            
            
                                    
            
            
                | 114 | 5 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 32 |  |     public function hasHomeDir(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 32 |  |         return $this->hasHomeDir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * Use sub dirs with data? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @param bool $set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @return UserDir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 5 |  |     public function wantDataDir(bool $set): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 5 |  |         $this->hasDataDir = $set; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 5 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 39 |  |     public function hasDataDir(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 39 |  |         return $this->hasDataDir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * Set obtained path as basic user dir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * @param string|null $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 47 |  |     public function setUserPath(?string $path): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 47 |  |         $this->clear(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 47 |  |         if (is_null($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 4 |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 43 |  |         if (false !== strpbrk($path, ':')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 1 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 42 |  |         $this->hasHomeDir = IPaths::SPLITTER_SLASH != substr($path, 0, 1); # may use data dir - does not start with slash | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 42 |  |         $this->hasDataDir = IPaths::SPLITTER_SLASH != substr($path, -1, 1); # may use sub dirs - does not end with slash | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 42 |  |         $this->userPath = $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 42 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 35 |  |     public function getUserPath(): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 35 |  |         return $this->userPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * Fill user dir from obtained params, must run every time | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * @throws PathsException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 19 |  |     public function process(): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 19 |  |         if (empty($this->userPath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 8 |  |             $this->userPath = $this->makeFromUserName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 18 |  |         $this->homeDir = $this->hasHomeDir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 10 |  |             ? IPaths::DIR_USER | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 8 |  |             : '.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 18 |  |         $this->dataDir = $this->hasDataDir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 12 |  |             ? IPaths::DIR_DATA | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 6 |  |             : '.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 18 |  |         $this->fullPath = new ArrayPath(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 18 |  |         $this->fullPath->setArray(array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 | 18 |  |             [$this->homeDir], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 | 18 |  |             Stuff::pathToArray($this->userPath, IPaths::SPLITTER_SLASH), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 18 |  |             [$this->dataDir] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 | 18 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 | 5 |  |     protected function makeFromUserName(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 | 5 |  |         if (empty($this->userName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 | 1 |  |             throw new UnexpectedValueException($this->getUpLang()->upUserNameNotDefined()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 | 4 |  |         $userPath = $this->userName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 | 4 |  |         if (!$this->hasHomeDir) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 | 2 |  |             $userPath = IPaths::SPLITTER_SLASH . $userPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 | 4 |  |         if (!$this->hasDataDir) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 | 2 |  |             $userPath = $userPath . IPaths::SPLITTER_SLASH; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 | 4 |  |         return $userPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 205 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 206 |  |  |  |