This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Spatie\Sluggable; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Model; |
||
6 | use Illuminate\Support\Str; |
||
7 | |||
8 | trait HasSlug |
||
9 | { |
||
10 | protected SlugOptions $slugOptions; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
11 | |||
12 | abstract public function getSlugOptions(): SlugOptions; |
||
13 | |||
14 | protected static function bootHasSlug() |
||
15 | { |
||
16 | static::creating(function (Model $model) { |
||
17 | $model->generateSlugOnCreate(); |
||
18 | }); |
||
19 | |||
20 | static::updating(function (Model $model) { |
||
21 | $model->generateSlugOnUpdate(); |
||
22 | }); |
||
23 | } |
||
24 | |||
25 | protected function generateSlugOnCreate() |
||
26 | { |
||
27 | $this->slugOptions = $this->getSlugOptions(); |
||
28 | |||
29 | if (! $this->slugOptions->generateSlugsOnCreate) { |
||
30 | return; |
||
31 | } |
||
32 | |||
33 | $this->addSlug(); |
||
34 | } |
||
35 | |||
36 | protected function generateSlugOnUpdate() |
||
37 | { |
||
38 | $this->slugOptions = $this->getSlugOptions(); |
||
39 | |||
40 | if (! $this->slugOptions->generateSlugsOnUpdate) { |
||
41 | return; |
||
42 | } |
||
43 | |||
44 | $this->addSlug(); |
||
45 | } |
||
46 | |||
47 | public function generateSlug() |
||
48 | { |
||
49 | $this->slugOptions = $this->getSlugOptions(); |
||
50 | |||
51 | $this->addSlug(); |
||
52 | } |
||
53 | |||
54 | protected function addSlug() |
||
55 | { |
||
56 | $this->ensureValidSlugOptions(); |
||
57 | |||
58 | $slug = $this->generateNonUniqueSlug(); |
||
59 | |||
60 | if ($this->slugOptions->generateUniqueSlugs) { |
||
61 | $slug = $this->makeSlugUnique($slug); |
||
62 | } |
||
63 | |||
64 | $slugField = $this->slugOptions->slugField; |
||
65 | |||
66 | $this->$slugField = $slug; |
||
67 | } |
||
68 | |||
69 | protected function generateNonUniqueSlug(): string |
||
70 | { |
||
71 | $slugField = $this->slugOptions->slugField; |
||
72 | |||
73 | if ($this->hasCustomSlugBeenUsed() && ! empty($this->$slugField)) { |
||
74 | return $this->$slugField; |
||
75 | } |
||
76 | |||
77 | return Str::slug($this->getSlugSourceString(), $this->slugOptions->slugSeparator, $this->slugOptions->slugLanguage); |
||
78 | } |
||
79 | |||
80 | protected function hasCustomSlugBeenUsed(): bool |
||
81 | { |
||
82 | $slugField = $this->slugOptions->slugField; |
||
83 | |||
84 | return $this->getOriginal($slugField) != $this->$slugField; |
||
85 | } |
||
86 | |||
87 | protected function getSlugSourceString(): string |
||
88 | { |
||
89 | if (is_callable($this->slugOptions->generateSlugFrom)) { |
||
90 | $slugSourceString = $this->getSlugSourceStringFromCallable(); |
||
91 | |||
92 | return $this->generateSubstring($slugSourceString); |
||
93 | } |
||
94 | |||
95 | $slugSourceString = collect($this->slugOptions->generateSlugFrom) |
||
96 | ->map(fn (string $fieldName): string => data_get($this, $fieldName, '')) |
||
97 | ->implode($this->slugOptions->slugSeparator); |
||
98 | |||
99 | return $this->generateSubstring($slugSourceString); |
||
100 | } |
||
101 | |||
102 | protected function getSlugSourceStringFromCallable(): string |
||
103 | { |
||
104 | return call_user_func($this->slugOptions->generateSlugFrom, $this); |
||
105 | } |
||
106 | |||
107 | protected function makeSlugUnique(string $slug): string |
||
108 | { |
||
109 | $originalSlug = $slug; |
||
110 | $i = 1; |
||
111 | |||
112 | while ($this->otherRecordExistsWithSlug($slug) || $slug === '') { |
||
113 | $slug = $originalSlug.$this->slugOptions->slugSeparator.$i++; |
||
114 | } |
||
115 | |||
116 | return $slug; |
||
117 | } |
||
118 | |||
119 | protected function otherRecordExistsWithSlug(string $slug): bool |
||
120 | { |
||
121 | $key = $this->getKey(); |
||
122 | |||
123 | if ($this->getIncrementing()) { |
||
124 | $key ??= '0'; |
||
125 | } |
||
126 | |||
127 | $query = static::where($this->slugOptions->slugField, $slug) |
||
128 | ->where($this->getKeyName(), '!=', $key) |
||
129 | ->withoutGlobalScopes(); |
||
130 | |||
131 | if ($this->usesSoftDeletes()) { |
||
132 | $query->withTrashed(); |
||
133 | } |
||
134 | |||
135 | return $query->exists(); |
||
136 | } |
||
137 | |||
138 | protected function usesSoftDeletes(): bool |
||
139 | { |
||
140 | return (bool) in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this)); |
||
141 | } |
||
142 | |||
143 | protected function ensureValidSlugOptions() |
||
144 | { |
||
145 | if (is_array($this->slugOptions->generateSlugFrom) && ! count($this->slugOptions->generateSlugFrom)) { |
||
146 | throw InvalidOption::missingFromField(); |
||
147 | } |
||
148 | |||
149 | if (! strlen($this->slugOptions->slugField)) { |
||
150 | throw InvalidOption::missingSlugField(); |
||
151 | } |
||
152 | |||
153 | if ($this->slugOptions->maximumLength <= 0) { |
||
154 | throw InvalidOption::invalidMaximumLength(); |
||
155 | } |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Helper function to handle multi-bytes strings if the module mb_substr is present, |
||
160 | * default to substr otherwise. |
||
161 | */ |
||
162 | protected function generateSubstring($slugSourceString) |
||
163 | { |
||
164 | if (function_exists('mb_substr')) { |
||
165 | return mb_substr($slugSourceString, 0, $this->slugOptions->maximumLength); |
||
166 | } |
||
167 | |||
168 | return substr($slugSourceString, 0, $this->slugOptions->maximumLength); |
||
169 | } |
||
170 | } |
||
171 |