Cache
1 day ago
DBPermissions.php
1 day ago
DataEncoder.php
1 day ago
DatabaseOptions.php
1 day ago
Env.php
1 day ago
Escape.php
1 day ago
Glob.php
1 day ago
Hooks.php
1 day ago
Math.php
1 day ago
PluginInfo.php
1 day ago
Sanitize.php
1 day ago
ServerVars.php
1 day ago
SlashMode.php
1 day ago
Strings.php
1 day ago
Times.php
1 day ago
Urls.php
1 day ago
Version.php
1 day ago
WpDefaultDirectories.php
1 day ago
Strings.php
255 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Utils; |
| 4 | |
| 5 | use WPStaging\Framework\Traits\UrlTrait; |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | class Strings |
| 21 | { |
| 22 | use UrlTrait; |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | public function str_replace_first($search, $replace, $subject) |
| 35 | { |
| 36 | return $this->strReplaceFirst($search, $replace, $subject); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | public function strReplaceFirst($search, $replace, $subject) |
| 47 | { |
| 48 | if (empty($search)) { |
| 49 | return $subject; |
| 50 | } |
| 51 | |
| 52 | $pos = strpos($subject, $search); |
| 53 | if ($pos !== false) { |
| 54 | if ($replace === null) { |
| 55 | $replace = ''; |
| 56 | } |
| 57 | |
| 58 | return substr_replace($subject, $replace, $pos, strlen($search)); |
| 59 | } |
| 60 | |
| 61 | return $subject; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | public function getLastElemAfterString($needle, $haystack) |
| 72 | { |
| 73 | $pos = strrpos($haystack, $needle); |
| 74 | return $pos === false ? $haystack : substr($haystack, $pos + 1); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | public function sanitizeDirectorySeparator($path) |
| 85 | { |
| 86 | $string = preg_replace('/[\\\\]+/', '/', $path); |
| 87 | return str_replace('//', '/', $string); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | public function startsWith($haystack, $needle) |
| 97 | { |
| 98 | $length = strlen($needle); |
| 99 | return ($needle === substr($haystack, 0, $length)); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | public function startsWithAnyFromList(array $needlesList, string $haystack): bool |
| 109 | { |
| 110 | foreach ($needlesList as $needle) { |
| 111 | if ($this->startsWith($haystack, $needle)) { |
| 112 | return true; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | public function endsWith($haystack, $needle) |
| 126 | { |
| 127 | $haystack = strrev($haystack); |
| 128 | $needle = strrev($needle); |
| 129 | return strpos($haystack, $needle) === 0; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | public function replaceLastMatch($needle, $replace, $haystack) |
| 140 | { |
| 141 | $result = $haystack; |
| 142 | $pos = strrpos($haystack, $needle); |
| 143 | if ($pos !== false) { |
| 144 | $result = substr_replace($haystack, $replace, $pos, strlen($needle)); |
| 145 | } |
| 146 | |
| 147 | return $result; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | public function maybeAppendUnderscore(string $string): string |
| 156 | { |
| 157 | |
| 158 | if (substr($string, -1) === '_') { |
| 159 | return $string; |
| 160 | } |
| 161 | |
| 162 | return $string . '_'; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | public function replaceStartWith(string $needle, string $replace, string $haystack): string |
| 176 | { |
| 177 | if (strpos($haystack, $needle) === 0) { |
| 178 | return $replace . substr($haystack, strlen($needle)); |
| 179 | } |
| 180 | |
| 181 | return $haystack; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | public function maskEmail(string $email): string |
| 189 | { |
| 190 | if (empty($email)) { |
| 191 | return ''; |
| 192 | } |
| 193 | |
| 194 | list($username, $domain) = explode('@', $email); |
| 195 | $usernameLength = strlen($username); |
| 196 | $firstChar = substr($username, 0, 1); |
| 197 | $lastChar = substr($username, -1); |
| 198 | if ($usernameLength <= 2) { |
| 199 | $maskedUsername = $firstChar . str_repeat('*', max(0, $usernameLength - 1)); |
| 200 | } else { |
| 201 | $maskedUsername = $firstChar . str_repeat('*', $usernameLength - 2) . $lastChar; |
| 202 | } |
| 203 | |
| 204 | list($domainName, $topLevelDomain) = explode('.', $domain); |
| 205 | $maskedDomainName = substr($domainName, 0, 1) . str_repeat('*', max(0, strlen($domainName) - 1)); |
| 206 | return $maskedUsername . '@' . $maskedDomainName . '.' . $topLevelDomain; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | public function maskBackupFilename(string $filename): string |
| 221 | { |
| 222 | if ($filename === '') { |
| 223 | return ''; |
| 224 | } |
| 225 | |
| 226 | $basename = basename($filename); |
| 227 | |
| 228 | |
| 229 | if (preg_match('/^(.+?)_([0-9]{8}-[0-9]{6}_[a-f0-9]+)(\.(wpstgdb|otherfiles|rootfiles|themes|plugins|muplugins)\.\d+\.wpstg)$/', $basename, $matches)) { |
| 230 | $prefix = substr($matches[1], 0, 6); |
| 231 | $suffix = substr($matches[2], -6); |
| 232 | return $prefix . '*********' . $suffix . $matches[3]; |
| 233 | } |
| 234 | |
| 235 | |
| 236 | if (preg_match('/^(.+?)_([0-9]{8}-[0-9]{6}_[a-f0-9]+)(\.wpstg)$/', $basename, $matches)) { |
| 237 | $prefix = substr($matches[1], 0, 6); |
| 238 | $suffix = substr($matches[2], -6); |
| 239 | return $prefix . '*********' . $suffix . $matches[3]; |
| 240 | } |
| 241 | |
| 242 | |
| 243 | $nameWithoutExt = pathinfo($basename, PATHINFO_FILENAME); |
| 244 | $extension = pathinfo($basename, PATHINFO_EXTENSION); |
| 245 | $extension = $extension ? '.' . $extension : ''; |
| 246 | |
| 247 | $length = strlen($nameWithoutExt); |
| 248 | if ($length <= 2) { |
| 249 | return substr($nameWithoutExt, 0, 1) . '*********' . substr($nameWithoutExt, -1) . $extension; |
| 250 | } |
| 251 | |
| 252 | return substr($nameWithoutExt, 0, 6) . '*********' . substr($nameWithoutExt, -6) . $extension; |
| 253 | } |
| 254 | } |
| 255 |