|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Support\Facades\DB; |
|
7
|
|
|
use Illuminate\Support\Facades\Hash; |
|
8
|
|
|
use grubersjoe\BingPhoto; |
|
9
|
|
|
use Cache; |
|
10
|
|
|
use Storage; |
|
11
|
|
|
|
|
12
|
|
|
class AccountModel extends Model |
|
13
|
|
|
{ |
|
14
|
|
|
private $user_extra = [ |
|
15
|
|
|
0 => 'gender', |
|
16
|
|
|
1 => 'contact', |
|
17
|
|
|
2 => 'school', |
|
18
|
|
|
3 => 'country', |
|
19
|
|
|
4 => 'location', |
|
20
|
|
|
5 => 'editor_left_width', |
|
21
|
|
|
|
|
22
|
|
|
1000 => 'github_id', |
|
23
|
|
|
1001 => 'github_email', |
|
24
|
|
|
1002 => 'github_nickname', |
|
25
|
|
|
1003 => 'github_homepage', |
|
26
|
|
|
1004 => 'github_token', |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
private $socialite_support = [ |
|
30
|
|
|
//use the form "platform_id" for unique authentication |
|
31
|
|
|
//such as github_id |
|
32
|
|
|
'github' => [ |
|
33
|
|
|
'email','nickname','homepage','token' |
|
34
|
|
|
], |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
public function generatePassword($length=8) |
|
38
|
|
|
{ |
|
39
|
|
|
$chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789'; |
|
40
|
|
|
|
|
41
|
|
|
$password=''; |
|
42
|
|
|
for ($i=0; $i<$length; $i++) { |
|
43
|
|
|
$password.=$chars[mt_rand(0, strlen($chars)-1)]; |
|
44
|
|
|
} |
|
45
|
|
|
return $password; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function feed($uid=null) |
|
49
|
|
|
{ |
|
50
|
|
|
$ret=[]; |
|
51
|
|
|
$solution=DB::table("problem_solution")->join("problem","problem.pid","=","problem_solution.pid")->where(["uid"=>$uid,"audit"=>1])->select("problem.pid as pid","pcode","title","problem_solution.created_at as created_at")->orderBy("problem_solution.created_at","DESC")->get()->all(); |
|
52
|
|
|
foreach($solution as &$s){ |
|
53
|
|
|
$s["type"]="event"; |
|
54
|
|
|
$s["color"]="wemd-orange"; |
|
55
|
|
|
$s["icon"]="comment-check-outline"; |
|
56
|
|
|
$ret[]=$s; |
|
57
|
|
|
} |
|
58
|
|
|
return $ret; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function generateContestAccount($cid, $ccode, $num) |
|
62
|
|
|
{ |
|
63
|
|
|
$ret=[]; |
|
64
|
|
|
$starting=DB::table("users")->where(["contest_account"=>$cid])->count(); |
|
65
|
|
|
$contestModel=new ContestModel(); |
|
66
|
|
|
for ($i=1; $i<=$num; $i++) { |
|
67
|
|
|
$pass=$this->generatePassword(); |
|
68
|
|
|
$name=strtoupper($ccode).str_pad($starting+$i, 3, "0", STR_PAD_LEFT); |
|
69
|
|
|
$uid=$this->add([ |
|
70
|
|
|
'name' => $name, |
|
71
|
|
|
'email' => "[email protected]", |
|
72
|
|
|
'email_verified_at' => date("Y-m-d H:i:s"), |
|
73
|
|
|
'password' => $pass, |
|
74
|
|
|
'avatar' => "/static/img/avatar/default.png", |
|
75
|
|
|
'contest_account' => $cid |
|
76
|
|
|
]); |
|
77
|
|
|
$contestModel->grantAccess($uid, $cid, 1); |
|
78
|
|
|
$ret[]=[ |
|
79
|
|
|
"uid"=>$uid, |
|
80
|
|
|
"name"=>$name, |
|
81
|
|
|
"email"=>"[email protected]", |
|
82
|
|
|
"password"=>$pass |
|
83
|
|
|
]; |
|
84
|
|
|
} |
|
85
|
|
|
return $ret; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function add($data) |
|
89
|
|
|
{ |
|
90
|
|
|
return DB::table("users")->insertGetId([ |
|
91
|
|
|
'name' => $data['name'], |
|
92
|
|
|
'email' => $data['email'], |
|
93
|
|
|
'email_verified_at' => $data["email_verified_at"], |
|
94
|
|
|
'password' => Hash::make($data['password']), |
|
95
|
|
|
'avatar' => $data["avatar"], |
|
96
|
|
|
'contest_account' => $data["contest_account"], |
|
97
|
|
|
'remember_token'=>null, |
|
98
|
|
|
'created_at'=>date("Y-m-d H:i:s"), |
|
99
|
|
|
'updated_at'=>date("Y-m-d H:i:s") |
|
100
|
|
|
]); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function detail($uid) |
|
104
|
|
|
{ |
|
105
|
|
|
$ret=DB::table("users")->where(["id"=>$uid])->first(); |
|
106
|
|
|
$ret["submissionCount"]=DB::table("submission")->where([ |
|
107
|
|
|
"uid"=>$uid, |
|
108
|
|
|
])->whereNotIn('verdict', [ |
|
109
|
|
|
'System Error', |
|
110
|
|
|
'Submission Error' |
|
111
|
|
|
])->count(); |
|
112
|
|
|
$ret["solved"]=DB::table("submission")->where([ |
|
113
|
|
|
"uid"=>$uid, |
|
114
|
|
|
"verdict"=>"Accepted" |
|
115
|
|
|
])->join("problem", "problem.pid", "=", "submission.pid")->select('pcode')->distinct()->get()->all(); |
|
116
|
|
|
$ret["solvedCount"]=count($ret["solved"]); |
|
117
|
|
|
// Casual |
|
118
|
|
|
$ret["rank"]=Cache::tags(['rank',$ret["id"]])->get("rank", "N/A"); |
|
119
|
|
|
$ret["rankTitle"]=Cache::tags(['rank',$ret["id"]])->get("title"); |
|
120
|
|
|
$ret["rankTitleColor"]=RankModel::getColor($ret["rankTitle"]); |
|
121
|
|
|
// Professional |
|
122
|
|
|
$ret["professionalTitle"]=RankModel::getProfessionalTitle($ret["professional_rate"]); |
|
123
|
|
|
$ret["professionalTitleColor"]=RankModel::getProfessionalColor($ret["professionalTitle"]); |
|
124
|
|
|
// Administration Group |
|
125
|
|
|
$ret["admin"]=$uid==1?1:0; |
|
126
|
|
|
if (Cache::tags(['bing', 'pic'])->get(date("Y-m-d"))==null) { |
|
127
|
|
|
$bing=new BingPhoto([ |
|
128
|
|
|
'locale' => 'zh-CN', |
|
129
|
|
|
]); |
|
130
|
|
|
Storage::disk('NOJPublic')->put("static/img/bing/".date("Y-m-d").".jpg", file_get_contents($bing->getImage()['url']), 86400); |
|
131
|
|
|
Cache::tags(['bing', 'pic'])->put(date("Y-m-d"), "/static/img/bing/".date("Y-m-d").".jpg"); |
|
132
|
|
|
} |
|
133
|
|
|
$ret["image"]=Cache::tags(['bing', 'pic'])->get(date("Y-m-d")); |
|
134
|
|
|
return $ret; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* To get some extra info of a user. |
|
139
|
|
|
* |
|
140
|
|
|
* @param int $uid id of the user |
|
141
|
|
|
* @param string|array $need An array is returned when an array is passed in,Only one value is returned when a string is passed in. |
|
142
|
|
|
* @return string|array $result |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getExtra($uid,$need, $secret_level = 0){ |
|
145
|
|
|
$ret = DB::table('users_extra')->where('uid',$uid)->orderBy('key')->get()->all(); |
|
146
|
|
|
$result = []; |
|
147
|
|
|
if(!empty($ret)){ |
|
148
|
|
|
if(is_string($need)){ |
|
149
|
|
|
foreach ($ret as $value) { |
|
150
|
|
|
if(empty($value['secret_level']) || $value['secret_level'] <= $secret_level){ |
|
151
|
|
|
$key_name = $this->user_extra[$value['key']] ?? 'unknown'; |
|
152
|
|
|
if($key_name == $need){ |
|
153
|
|
|
return $value['value']; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
return null; |
|
158
|
|
|
}else{ |
|
159
|
|
|
foreach ($ret as $value) { |
|
160
|
|
|
if(empty($value['secret_level']) || $value['secret_level'] <= $secret_level){ |
|
161
|
|
|
$key_name = $this->user_extra[$value['key']] ?? 'unknown'; |
|
162
|
|
|
if(in_array($key_name,$need)){ |
|
163
|
|
|
$result[$key_name] = $value['value']; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
return $result; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* To set some extra info of a user. |
|
174
|
|
|
* |
|
175
|
|
|
* @param int $uid id of the user |
|
176
|
|
|
* @param string $key_name insert when key not found or update when key exists. Only values declared in the AccountModel are accepted |
|
177
|
|
|
* @param string|null $value the extra info will be delete when value is null |
|
178
|
|
|
* @return mixed $result |
|
179
|
|
|
*/ |
|
180
|
|
|
public function setExtra($uid,$key_name,$value = null,$secret_level = -1){ |
|
181
|
|
|
$key = array_search($key_name,$this->user_extra); |
|
182
|
|
|
if($key === false){ |
|
183
|
|
|
return false; |
|
184
|
|
|
} |
|
185
|
|
|
$ret = DB::table('users_extra')->where('uid',$uid)->where('key',$key)->first(); |
|
186
|
|
|
if(!empty($ret)){ |
|
187
|
|
|
unset($ret['id']); |
|
188
|
|
|
if(!is_null($value)){ |
|
189
|
|
|
$ret['value'] = $value; |
|
190
|
|
|
}else{ |
|
191
|
|
|
DB::table('users_extra')->where('uid',$uid)->where('key',$key)->delete(); |
|
192
|
|
|
return true; |
|
193
|
|
|
} |
|
194
|
|
|
if($secret_level != -1){ |
|
195
|
|
|
$ret['secret_level'] = $secret_level; |
|
196
|
|
|
} |
|
197
|
|
|
return DB::table('users_extra')->where('uid',$uid)->where('key',$key)->update($ret); |
|
198
|
|
|
}else{ |
|
199
|
|
|
if($value === null){ |
|
200
|
|
|
return true; |
|
201
|
|
|
} |
|
202
|
|
|
return DB::table('users_extra')->insertGetId( |
|
203
|
|
|
[ |
|
204
|
|
|
'uid' => $uid, |
|
205
|
|
|
'key' => $key, |
|
206
|
|
|
'value' => $value, |
|
207
|
|
|
'secret_level' => $secret_level == -1 ? 0 : $secret_level, |
|
208
|
|
|
] |
|
209
|
|
|
); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* find a extra info key-value pair |
|
215
|
|
|
* @param string $key_name the key |
|
216
|
|
|
* @param string $value the value |
|
217
|
|
|
* @return string $result |
|
218
|
|
|
*/ |
|
219
|
|
|
public function findExtra($key,$value){ |
|
220
|
|
|
$key = array_search($key,$this->user_extra); |
|
221
|
|
|
return DB::table('users_extra')->where('key',$key)->where('value',$value)->first(); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
public function getSocialiteInfo($uid,$secret_level = -1){ |
|
|
|
|
|
|
225
|
|
|
$socialites = []; |
|
226
|
|
|
foreach ($this->socialite_support as $key => $value) { |
|
227
|
|
|
$id_keyname = $key.'_id'; |
|
228
|
|
|
$id = $this->getExtra($uid,$id_keyname); |
|
229
|
|
|
if(!empty($id)){ |
|
230
|
|
|
$info = [ |
|
231
|
|
|
'id' => $id, |
|
232
|
|
|
]; |
|
233
|
|
|
foreach ($value as $info_name) { |
|
234
|
|
|
$info_temp = $this->getExtra($uid,$key.'_'.$info_name); |
|
235
|
|
|
if($info_temp !== null){ |
|
236
|
|
|
$info[$info_name] = $info_temp; |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
$socialites[$key] = $info; |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
return $socialites; |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.