1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models\Eloquent\Tool; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Support\Facades\DB; |
7
|
|
|
|
8
|
|
|
class Pastebin extends Model |
9
|
|
|
{ |
10
|
|
|
protected $table = 'pastebin'; |
11
|
|
|
protected $primaryKey = 'pbid'; |
12
|
|
|
|
13
|
|
|
protected $fillable=[ |
14
|
|
|
'lang', 'title', 'user_id', 'expired_at', 'content', 'code' |
15
|
|
|
]; |
16
|
|
|
|
17
|
|
|
public function user() |
18
|
|
|
{ |
19
|
|
|
return $this->belongsTo('App\Models\Eloquent\UserModel', 'user_id'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function generatePbCode($length=6) |
23
|
|
|
{ |
24
|
|
|
$chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789'; |
25
|
|
|
|
26
|
|
|
$code=''; |
27
|
|
|
for ($i=0; $i<$length; $i++) { |
28
|
|
|
$code.=$chars[mt_rand(0, strlen($chars)-1)]; |
29
|
|
|
} |
30
|
|
|
return $code; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public static function generate($all_data) |
34
|
|
|
{ |
35
|
|
|
$lang=$all_data["syntax"]; |
36
|
|
|
$expire=intval($all_data["expiration"]); |
37
|
|
|
$content=$all_data["content"]; |
38
|
|
|
$title=$all_data["title"]; |
39
|
|
|
$user_id=$all_data["uid"]; |
40
|
|
|
|
41
|
|
|
if ($expire==0) { |
42
|
|
|
$expire_time=null; |
43
|
|
|
} elseif ($expire==1) { |
44
|
|
|
$expire_time=date("Y-m-d H:i:s", strtotime('+1 days')); |
45
|
|
|
} elseif ($expire==7) { |
46
|
|
|
$expire_time=date("Y-m-d H:i:s", strtotime('+7 days')); |
47
|
|
|
} elseif ($expire==30) { |
48
|
|
|
$expire_time=date("Y-m-d H:i:s", strtotime('+30 days')); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$code=self::generatePbCode(6); |
52
|
|
|
$ret=self::where('code', $code)->first(); |
53
|
|
|
if (is_null($ret)) { |
54
|
|
|
self::create([ |
55
|
|
|
'lang' => $lang, |
56
|
|
|
'expired_at' => $expire_time, |
|
|
|
|
57
|
|
|
'user_id' => $user_id, |
58
|
|
|
'title' => $title, |
59
|
|
|
'content' => $content, |
60
|
|
|
'code' => $code, |
61
|
|
|
'created_at' => date("Y-m-d H:i:s"), |
62
|
|
|
])->save(); |
63
|
|
|
return $code; |
64
|
|
|
} else { |
65
|
|
|
return null; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|