-
Notifications
You must be signed in to change notification settings - Fork 119
/
Url.php
222 lines (190 loc) · 5.11 KB
/
Url.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
namespace App;
use App\Http\Traits\Hashidable;
use Hidehalo\Nanoid\Client;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class Url extends Model
{
use Hashidable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'user_id',
'url_key',
'is_custom',
'long_url',
'meta_title',
'clicks',
'ip',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'is_custom' => 'boolean',
];
// Relations
public function user()
{
return $this->belongsTo('App\User')->withDefault([
'name' => 'Guest',
]);
}
public function urlStat()
{
return $this->hasMany('App\UrlStat');
}
// Mutator
public function setUserIdAttribute($value)
{
if ($value == 0) {
$this->attributes['user_id'] = null;
} else {
$this->attributes['user_id'] = $value;
}
}
public function setLongUrlAttribute($value)
{
$this->attributes['long_url'] = rtrim($value, '/');
}
public function setMetaTitleAttribute($value)
{
$this->attributes['meta_title'] = $this->getTitle($value);
}
// Accessor
public function getShortUrlAttribute()
{
return url('/'.$this->attributes['url_key']);
}
/*
|
|
*/
public function totalShortUrl()
{
return self::count('url_key');
}
/**
* @param int $id
*/
public function totalShortUrlById($id = null)
{
return self::whereUserId($id)->count('url_key');
}
public function totalClicks():int
{
return self::sum('clicks');
}
/**
* @param int $id
*/
public function totalClicksById($id = null):int
{
return self::whereUserId($id)->sum('clicks');
}
/*
|
|
*/
/**
* Generate an unique short URL using Nanoid.
*
* @return string
*/
public function key_generator()
{
$generateId = new Client();
$alphabet = config('urlhub.hash_alphabet');
$size1 = (int) config('urlhub.hash_size_1');
$size2 = (int) config('urlhub.hash_size_2');
// @codeCoverageIgnoreStart
if (($size1 == $size2) || $size2 == 0) {
$size2 = $size1;
}
// @codeCoverageIgnoreEnd
$urlKey = $generateId->formatedId($alphabet, $size1);
// If it is already used (not available), find the next available
// ending.
// @codeCoverageIgnoreStart
$link = self::whereUrlKey($urlKey)->first();
while ($link) {
$urlKey = $generateId->formatedId($alphabet, $size2);
$link = self::whereUrlKey($urlKey)->first();
}
// @codeCoverageIgnoreEnd
return $urlKey;
}
/**
* @return int
*/
public function url_key_capacity()
{
$alphabet = strlen(config('urlhub.hash_alphabet'));
$size1 = (int) config('urlhub.hash_size_1');
$size2 = (int) config('urlhub.hash_size_2');
// If the hash size is filled with integers that do not match
// the rules, change the variable's value to 0.
$size1 = ! ($size1 < 1) ? $size1 : 0;
$size2 = ! ($size2 < 0) ? $size2 : 0;
if ($size1 == 0 || ($size1 == 0 && $size2 == 0)) {
return 0;
} elseif ($size1 == $size2 || $size2 == 0) {
return pow($alphabet, $size1);
} else {
return pow($alphabet, $size1) + pow($alphabet, $size2);
}
}
/**
* @return int
*/
public function url_key_remaining()
{
$totalShortUrl = self::whereIsCustom(false)->count();
if ($this->url_key_capacity() < $totalShortUrl) {
return 0;
}
return $this->url_key_capacity() - $totalShortUrl;
}
/**
* Gets the title of page from its url.
*
* @param string $url
* @return string
*/
public function getTitle($url)
{
if ($title = preg_match('/<title[^>]*>(.*?)<\/title>/ims', @file_get_contents($url), $matches)) {
return $matches[1];
} elseif ($domain = $this->getDomain($url)) {
// @codeCoverageIgnoreStart
return Str::title($domain).' - '.__('No Title'); // @codeCoverageIgnoreEnd
} else {
return __('No Title');
}
}
/**
* Get Domain from external url.
*
* Extract the domain name using the classic parse_url() and then look
* for a valid domain without any subdomain (www being a subdomain).
* Won't work on things like 'localhost'.
*
* @param string $url
* @return mixed
*/
public function getDomain($url)
{
// https://stackoverflow.com/a/399316
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
return $regs['domain'];
}
}
}