Laravel-Backpack /
CRUD
We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| 1 | <?php |
||||
| 2 | |||||
| 3 | use Backpack\Basset\Facades\Basset; |
||||
| 4 | use Creativeorange\Gravatar\Facades\Gravatar; |
||||
| 5 | use Illuminate\Support\Facades\Log; |
||||
| 6 | use Illuminate\Support\Str; |
||||
| 7 | |||||
| 8 | if (! function_exists('backpack_url')) { |
||||
| 9 | /** |
||||
| 10 | * Appends the configured backpack prefix and returns |
||||
| 11 | * the URL using the standard Laravel helpers. |
||||
| 12 | * |
||||
| 13 | * @param $path |
||||
| 14 | * @return string |
||||
| 15 | */ |
||||
| 16 | function backpack_url($path = null, $parameters = [], $secure = null) |
||||
| 17 | { |
||||
| 18 | $path = ! $path || (substr($path, 0, 1) == '/') ? $path : '/'.$path; |
||||
| 19 | |||||
| 20 | return url(config('backpack.base.route_prefix', 'admin').$path, $parameters, $secure); |
||||
| 21 | } |
||||
| 22 | } |
||||
| 23 | |||||
| 24 | if (! function_exists('backpack_authentication_column')) { |
||||
| 25 | /** |
||||
| 26 | * Return the username column name. |
||||
| 27 | * The Laravel default (and Backpack default) is 'email'. |
||||
| 28 | * |
||||
| 29 | * @return string |
||||
| 30 | */ |
||||
| 31 | function backpack_authentication_column() |
||||
| 32 | { |
||||
| 33 | return config('backpack.base.authentication_column', 'email'); |
||||
| 34 | } |
||||
| 35 | } |
||||
| 36 | |||||
| 37 | if (! function_exists('backpack_email_column')) { |
||||
| 38 | /** |
||||
| 39 | * Return the email column name. |
||||
| 40 | * The Laravel default (and Backpack default) is 'email'. |
||||
| 41 | * |
||||
| 42 | * @return string |
||||
| 43 | */ |
||||
| 44 | function backpack_email_column() |
||||
| 45 | { |
||||
| 46 | return config('backpack.base.email_column', 'email'); |
||||
| 47 | } |
||||
| 48 | } |
||||
| 49 | |||||
| 50 | if (! function_exists('backpack_form_input')) { |
||||
| 51 | /** |
||||
| 52 | * Parse the submitted input in request('form') to an usable array. |
||||
| 53 | * Joins the multiple[] fields in a single key and transform the dot notation fields into arrayed ones. |
||||
| 54 | * |
||||
| 55 | * |
||||
| 56 | * @return array |
||||
| 57 | */ |
||||
| 58 | function backpack_form_input() |
||||
| 59 | { |
||||
| 60 | $input = request('form') ?? []; |
||||
| 61 | $result = []; |
||||
| 62 | |||||
| 63 | foreach ($input as $row) { |
||||
| 64 | $repeatableRowKey = null; |
||||
| 65 | |||||
| 66 | // regular fields don't need any additional parsing |
||||
| 67 | if (strpos($row['name'], '[') === false) { |
||||
| 68 | $result[$row['name']] = $row['value']; |
||||
| 69 | |||||
| 70 | continue; |
||||
| 71 | } |
||||
| 72 | |||||
| 73 | $isMultiple = substr($row['name'], -2, 2) === '[]'; |
||||
| 74 | |||||
| 75 | if ($isMultiple && substr_count($row['name'], '[') === 1) { |
||||
| 76 | $result[substr($row['name'], 0, -2)][] = $row['value']; |
||||
| 77 | continue; |
||||
| 78 | } |
||||
| 79 | |||||
| 80 | // dot notation fields |
||||
| 81 | if (substr_count($row['name'], '[') === 1) { |
||||
| 82 | // start in the first occurrence since it's HasOne/MorphOne with dot notation (address[street] in request) to get the input name (address) |
||||
| 83 | $inputNameStart = strpos($row['name'], '[') + 1; |
||||
| 84 | } else { |
||||
| 85 | // repeatable fields, we need to get the input name and the row number |
||||
| 86 | // start on the second occurrence since it's a repeatable and we want to bypass the row number (repeatableName[rowNumber][inputName]) |
||||
| 87 | $inputNameStart = strpos($row['name'], '[', strpos($row['name'], '[') + 1) + 1; |
||||
| 88 | |||||
| 89 | // get the array key (aka repeatable row) from field name |
||||
| 90 | $startKey = strpos($row['name'], '[') + 1; |
||||
| 91 | $endKey = strpos($row['name'], ']', $startKey); |
||||
| 92 | $lengthKey = $endKey - $startKey; |
||||
| 93 | $repeatableRowKey = substr($row['name'], $startKey, $lengthKey); |
||||
| 94 | } |
||||
| 95 | |||||
| 96 | $inputNameEnd = strpos($row['name'], ']', $inputNameStart); |
||||
| 97 | $inputNameLength = $inputNameEnd - $inputNameStart; |
||||
| 98 | $inputName = substr($row['name'], $inputNameStart, $inputNameLength); |
||||
| 99 | $parentInputName = substr($row['name'], 0, strpos($row['name'], '[')); |
||||
| 100 | |||||
| 101 | if (isset($repeatableRowKey)) { |
||||
| 102 | if ($isMultiple) { |
||||
| 103 | $result[$parentInputName][$repeatableRowKey][$inputName][] = $row['value']; |
||||
| 104 | continue; |
||||
| 105 | } |
||||
| 106 | |||||
| 107 | $result[$parentInputName][$repeatableRowKey][$inputName] = $row['value']; |
||||
| 108 | |||||
| 109 | continue; |
||||
| 110 | } |
||||
| 111 | |||||
| 112 | if ($isMultiple) { |
||||
| 113 | $result[$parentInputName][$inputName][] = $row['value']; |
||||
| 114 | continue; |
||||
| 115 | } |
||||
| 116 | $result[$parentInputName][$inputName] = $row['value']; |
||||
| 117 | } |
||||
| 118 | |||||
| 119 | return $result; |
||||
| 120 | } |
||||
| 121 | } |
||||
| 122 | |||||
| 123 | if (! function_exists('backpack_users_have_email')) { |
||||
| 124 | /** |
||||
| 125 | * Check if the email column is present on the user table. |
||||
| 126 | * |
||||
| 127 | * @return string |
||||
| 128 | */ |
||||
| 129 | function backpack_users_have_email() |
||||
| 130 | { |
||||
| 131 | $user_model_fqn = config('backpack.base.user_model_fqn'); |
||||
| 132 | $user = new $user_model_fqn(); |
||||
| 133 | |||||
| 134 | return \Schema::hasColumn($user->getTable(), config('backpack.base.email_column') ?? 'email'); |
||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||
| 135 | } |
||||
| 136 | } |
||||
| 137 | |||||
| 138 | if (! function_exists('backpack_avatar_url')) { |
||||
| 139 | /** |
||||
| 140 | * Returns the avatar URL of a user. |
||||
| 141 | * |
||||
| 142 | * @param $user |
||||
| 143 | * @return string |
||||
| 144 | */ |
||||
| 145 | function backpack_avatar_url($user) |
||||
| 146 | { |
||||
| 147 | switch (config('backpack.base.avatar_type')) { |
||||
| 148 | case 'gravatar': |
||||
| 149 | if (backpack_users_have_email() && ! empty($user->email)) { |
||||
| 150 | $avatarLink = Gravatar::fallback(config('backpack.base.gravatar_fallback'))->get($user->email, ['size' => 80]); |
||||
| 151 | |||||
| 152 | // if we can save it locally, for safer loading, let's do it |
||||
| 153 | if (in_array(Basset::basset($avatarLink, false)->name, ['INTERNALIZED', 'IN_CACHE', 'LOADED'])) { |
||||
|
0 ignored issues
–
show
|
|||||
| 154 | return Basset::getUrl($avatarLink); |
||||
| 155 | } |
||||
| 156 | |||||
| 157 | return $avatarLink; |
||||
| 158 | } |
||||
| 159 | break; |
||||
| 160 | default: |
||||
| 161 | return method_exists($user, config('backpack.base.avatar_type')) ? $user->{config('backpack.base.avatar_type')}() : $user->{config('backpack.base.avatar_type')}; |
||||
| 162 | break; |
||||
|
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The switch ($x) {
case 1:
return 'foo';
break; // This break is not necessary and can be left off.
}
If you would like to keep this construct to be consistent with other Loading history...
|
|||||
| 163 | } |
||||
| 164 | } |
||||
| 165 | } |
||||
| 166 | |||||
| 167 | if (! function_exists('backpack_middleware')) { |
||||
| 168 | /** |
||||
| 169 | * Return the key of the middleware used across Backpack. |
||||
| 170 | * That middleware checks if the visitor is an admin. |
||||
| 171 | * |
||||
| 172 | * @param $path |
||||
| 173 | * @return string |
||||
| 174 | */ |
||||
| 175 | function backpack_middleware() |
||||
| 176 | { |
||||
| 177 | return config('backpack.base.middleware_key', 'admin'); |
||||
| 178 | } |
||||
| 179 | } |
||||
| 180 | |||||
| 181 | if (! function_exists('backpack_guard_name')) { |
||||
| 182 | /* |
||||
| 183 | * Returns the name of the guard defined |
||||
| 184 | * by the application config |
||||
| 185 | */ |
||||
| 186 | function backpack_guard_name() |
||||
| 187 | { |
||||
| 188 | return config('backpack.base.guard', config('auth.defaults.guard')); |
||||
| 189 | } |
||||
| 190 | } |
||||
| 191 | |||||
| 192 | if (! function_exists('backpack_auth')) { |
||||
| 193 | /* |
||||
| 194 | * Returns the user instance if it exists |
||||
| 195 | * of the currently authenticated admin |
||||
| 196 | * based off the defined guard. |
||||
| 197 | */ |
||||
| 198 | function backpack_auth() |
||||
| 199 | { |
||||
| 200 | return \Auth::guard(backpack_guard_name()); |
||||
| 201 | } |
||||
| 202 | } |
||||
| 203 | |||||
| 204 | if (! function_exists('backpack_user')) { |
||||
| 205 | /* |
||||
| 206 | * Returns back a user instance without |
||||
| 207 | * the admin guard, however allows you |
||||
| 208 | * to pass in a custom guard if you like. |
||||
| 209 | */ |
||||
| 210 | function backpack_user() |
||||
| 211 | { |
||||
| 212 | return backpack_auth()->user(); |
||||
| 213 | } |
||||
| 214 | } |
||||
| 215 | |||||
| 216 | if (! function_exists('mb_ucfirst')) { |
||||
| 217 | /** |
||||
| 218 | * Capitalize the first letter of a string, |
||||
| 219 | * even if that string is multi-byte (non-latin alphabet). |
||||
| 220 | * |
||||
| 221 | * @param string $string String to have its first letter capitalized. |
||||
| 222 | * @param encoding $encoding Character encoding |
||||
|
0 ignored issues
–
show
The type
encoding was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 223 | * @return string String with first letter capitalized. |
||||
| 224 | */ |
||||
| 225 | function mb_ucfirst($string, $encoding = false) |
||||
| 226 | { |
||||
| 227 | $encoding = $encoding ? $encoding : mb_internal_encoding(); |
||||
| 228 | |||||
| 229 | $strlen = mb_strlen($string, $encoding); |
||||
|
0 ignored issues
–
show
It seems like
$encoding can also be of type true; however, parameter $encoding of mb_strlen() does only seem to accept null|string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 230 | $firstChar = mb_substr($string, 0, 1, $encoding); |
||||
|
0 ignored issues
–
show
It seems like
$encoding can also be of type true; however, parameter $encoding of mb_substr() does only seem to accept null|string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 231 | $then = mb_substr($string, 1, $strlen - 1, $encoding); |
||||
| 232 | |||||
| 233 | return mb_strtoupper($firstChar, $encoding).$then; |
||||
|
0 ignored issues
–
show
It seems like
$encoding can also be of type true; however, parameter $encoding of mb_strtoupper() does only seem to accept null|string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 234 | } |
||||
| 235 | } |
||||
| 236 | |||||
| 237 | if (! function_exists('backpack_view')) { |
||||
| 238 | /** |
||||
| 239 | * Returns a new displayable view path, based on the configured backpack view namespace. |
||||
| 240 | * If that view doesn't exist, it falls back to the fallback namespace. |
||||
| 241 | * If that view doesn't exist, it falls back to the one from the Backpack UI directory. |
||||
| 242 | * |
||||
| 243 | * @param string (see config/backpack/base.php) |
||||
|
0 ignored issues
–
show
|
|||||
| 244 | * @return string |
||||
| 245 | */ |
||||
| 246 | function backpack_view($view) |
||||
| 247 | { |
||||
| 248 | $viewPaths = [ |
||||
| 249 | config('backpack.ui.view_namespace').$view, |
||||
| 250 | backpack_theme_config('view_namespace_fallback').$view, |
||||
| 251 | 'backpack.ui::'.$view, |
||||
| 252 | ]; |
||||
| 253 | |||||
| 254 | foreach ($viewPaths as $view) { |
||||
|
0 ignored issues
–
show
|
|||||
| 255 | if (view()->exists($view)) { |
||||
| 256 | return $view; |
||||
| 257 | } |
||||
| 258 | } |
||||
| 259 | |||||
| 260 | $errorMessage = 'The view: ['.$view.'] was not found in any of the following view paths: ['.implode(' ], [ ', $viewPaths).']'; |
||||
| 261 | |||||
| 262 | $errorDetails = (function () { |
||||
| 263 | if (env('APP_ENV') === 'production' || ! env('APP_DEBUG')) { |
||||
| 264 | return ''; |
||||
| 265 | } |
||||
| 266 | |||||
| 267 | $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2) ?? []; |
||||
| 268 | $functionCaller = $backtrace[1] ?? []; |
||||
| 269 | $functionLine = $functionCaller['line'] ?? 'N/A'; |
||||
| 270 | $functionFile = $functionCaller['file'] ?? 'N/A'; |
||||
| 271 | |||||
| 272 | return '- Called in: '.Str::after($functionFile, base_path()).' on line: '.$functionLine; |
||||
| 273 | })(); |
||||
| 274 | |||||
| 275 | abort(500, $errorMessage.$errorDetails, ['developer-error-exception']); |
||||
| 276 | } |
||||
| 277 | } |
||||
| 278 | |||||
| 279 | if (! function_exists('backpack_theme_config')) { |
||||
| 280 | /** |
||||
| 281 | * Returns a config value from the current theme's config file. |
||||
| 282 | * It assumes the theme's config namespace is the same as the view namespace. |
||||
| 283 | * |
||||
| 284 | * @param string |
||||
| 285 | * @return string |
||||
| 286 | */ |
||||
| 287 | function backpack_theme_config($key) |
||||
| 288 | { |
||||
| 289 | $namespacedKey = config('backpack.ui.view_namespace').$key; |
||||
| 290 | $namespacedKey = str_replace('::', '.', $namespacedKey); |
||||
| 291 | |||||
| 292 | // if the config exists in the theme config file, use it |
||||
| 293 | if (config()->has($namespacedKey)) { |
||||
| 294 | return config($namespacedKey); |
||||
| 295 | } |
||||
| 296 | |||||
| 297 | // if not, fall back to a general the config in the fallback theme |
||||
| 298 | $namespacedKey = config('backpack.ui.view_namespace_fallback').$key; |
||||
| 299 | $namespacedKey = str_replace('::', '.', $namespacedKey); |
||||
| 300 | |||||
| 301 | if (config()->has($namespacedKey)) { |
||||
| 302 | return config($namespacedKey); |
||||
| 303 | } |
||||
| 304 | |||||
| 305 | // if not, fall back to the config in ui |
||||
| 306 | $namespacedKey = 'backpack.ui.'.$key; |
||||
| 307 | |||||
| 308 | if (config()->has($namespacedKey)) { |
||||
| 309 | return config($namespacedKey); |
||||
| 310 | } |
||||
| 311 | |||||
| 312 | Log::error('Could not find config key: '.$key.'. Neither in the Backpack theme, nor in the fallback theme, nor in ui.'); |
||||
| 313 | |||||
| 314 | return null; |
||||
| 315 | } |
||||
| 316 | } |
||||
| 317 | |||||
| 318 | if (! function_exists('square_brackets_to_dots')) { |
||||
| 319 | /** |
||||
| 320 | * Turns a string from bracket-type array to dot-notation array. |
||||
| 321 | * Ex: array[0][property] turns into array.0.property. |
||||
| 322 | * |
||||
| 323 | * @param $path |
||||
| 324 | * @return string |
||||
| 325 | */ |
||||
| 326 | function square_brackets_to_dots($string) |
||||
| 327 | { |
||||
| 328 | $string = str_replace(['[', ']'], ['.', ''], $string); |
||||
| 329 | |||||
| 330 | return $string; |
||||
| 331 | } |
||||
| 332 | } |
||||
| 333 | |||||
| 334 | if (! function_exists('old_empty_or_null')) { |
||||
| 335 | /** |
||||
| 336 | * This method is an alternative to Laravel's old() helper, which mistakenly |
||||
| 337 | * returns NULL it two cases: |
||||
| 338 | * - if there is an old value, and it was empty or null |
||||
| 339 | * - if there is no old value |
||||
| 340 | * (this is because of the ConvertsEmptyStringsToNull middleware). |
||||
| 341 | * |
||||
| 342 | * In contrast, this method will return: |
||||
| 343 | * - the old value, if there actually is an old value for that key; |
||||
| 344 | * - the second parameter, if there is no old value for that key, but it was empty string or null; |
||||
| 345 | * - null, if there is no old value at all for that key; |
||||
| 346 | * |
||||
| 347 | * @param string $key |
||||
| 348 | * @param array|string $empty_value |
||||
| 349 | * @return mixed |
||||
| 350 | */ |
||||
| 351 | function old_empty_or_null($key, $empty_value = '') |
||||
| 352 | { |
||||
| 353 | $key = square_brackets_to_dots($key); |
||||
| 354 | $old_inputs = session()->getOldInput(); |
||||
| 355 | |||||
| 356 | // if the input name is present in the old inputs we need to return earlier and not in a coalescing chain |
||||
| 357 | // otherwise `null` aka empty will not pass the condition and the field value would be returned. |
||||
| 358 | if (\Arr::has($old_inputs, $key)) { |
||||
| 359 | return \Arr::get($old_inputs, $key) ?? $empty_value; |
||||
| 360 | } |
||||
| 361 | |||||
| 362 | return null; |
||||
| 363 | } |
||||
| 364 | } |
||||
| 365 | |||||
| 366 | if (! function_exists('is_multidimensional_array')) { |
||||
| 367 | /** |
||||
| 368 | * Check if the array is multidimensional. |
||||
| 369 | * |
||||
| 370 | * If $strict is enabled, the array is considered multidimensional only if all elements of the array are arrays. |
||||
| 371 | */ |
||||
| 372 | function is_multidimensional_array(array $array, bool $strict = false): bool |
||||
| 373 | { |
||||
| 374 | foreach ($array as $item) { |
||||
| 375 | if ($strict && ! is_array($item)) { |
||||
| 376 | return false; |
||||
| 377 | } |
||||
| 378 | if (! $strict && is_array($item)) { |
||||
| 379 | return true; |
||||
| 380 | } |
||||
| 381 | } |
||||
| 382 | |||||
| 383 | return $strict; |
||||
| 384 | } |
||||
| 385 | } |
||||
| 386 | |||||
| 387 | if (! function_exists('backpack_pro')) { |
||||
| 388 | /** |
||||
| 389 | * Check if the backpack/pro package is installed. |
||||
| 390 | * |
||||
| 391 | * @return bool |
||||
| 392 | */ |
||||
| 393 | function backpack_pro() |
||||
| 394 | { |
||||
| 395 | if (app()->runningUnitTests()) { |
||||
|
0 ignored issues
–
show
The method
runningUnitTests() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 396 | return true; |
||||
| 397 | } |
||||
| 398 | if (! \Composer\InstalledVersions::isInstalled('backpack/pro')) { |
||||
| 399 | return false; |
||||
| 400 | } |
||||
| 401 | |||||
| 402 | return \Composer\InstalledVersions::getVersion('backpack/pro'); |
||||
|
0 ignored issues
–
show
|
|||||
| 403 | } |
||||
| 404 | } |
||||
| 405 |