| Total Complexity | 133 |
| Total Lines | 1036 |
| Duplicated Lines | 0 % |
| Changes | 24 | ||
| Bugs | 1 | Features | 0 |
Complex classes like UserController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UserController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class UserController extends Controller |
||
| 50 | { |
||
| 51 | |||
| 52 | //Statistics of the number of clicks and links |
||
| 53 | public function index() |
||
| 82 | } |
||
| 83 | |||
| 84 | //Show littlelink page. example => http://127.0.0.1:8000/+admin |
||
| 85 | public function littlelink(request $request) |
||
| 86 | { |
||
| 87 | if(isset($request->useif)){ |
||
| 88 | $littlelink_name = User::select('littlelink_name')->where('id', $request->littlelink)->value('littlelink_name'); |
||
| 89 | $id = $request->littlelink; |
||
| 90 | } else { |
||
| 91 | $littlelink_name = $request->littlelink; |
||
| 92 | $id = User::select('id')->where('littlelink_name', $littlelink_name)->value('id'); |
||
| 93 | } |
||
| 94 | |||
| 95 | if (empty($id)) { |
||
| 96 | return abort(404); |
||
| 97 | } |
||
| 98 | |||
| 99 | $userinfo = User::select('id', 'name', 'littlelink_name', 'littlelink_description', 'theme', 'role', 'block')->where('id', $id)->first(); |
||
| 100 | $information = User::select('name', 'littlelink_name', 'littlelink_description', 'theme')->where('id', $id)->get(); |
||
| 101 | |||
| 102 | if ($userinfo->block == 'yes') { |
||
| 103 | return abort(404); |
||
| 104 | } |
||
| 105 | |||
| 106 | $links = DB::table('links') |
||
| 107 | ->join('buttons', 'buttons.id', '=', 'links.button_id') |
||
| 108 | ->select('links.*', 'buttons.name') // Assuming 'links.*' to fetch all columns including 'type_params' |
||
| 109 | ->where('user_id', $id) |
||
| 110 | ->orderBy('up_link', 'asc') |
||
| 111 | ->orderBy('order', 'asc') |
||
| 112 | ->get(); |
||
| 113 | |||
| 114 | // Loop through each link to decode 'type_params' and merge it into the link object |
||
| 115 | foreach ($links as $link) { |
||
| 116 | if (!empty($link->type_params)) { |
||
| 117 | // Decode the JSON string into an associative array |
||
| 118 | $typeParams = json_decode($link->type_params, true); |
||
| 119 | if (is_array($typeParams)) { |
||
| 120 | // Merge the associative array into the link object |
||
| 121 | foreach ($typeParams as $key => $value) { |
||
| 122 | $link->$key = $value; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | return view('linkstack.linkstack', ['userinfo' => $userinfo, 'information' => $information, 'links' => $links, 'littlelink_name' => $littlelink_name]); |
||
| 129 | } |
||
| 130 | |||
| 131 | //Show littlelink page as home page if set in config |
||
| 132 | public function littlelinkhome(request $request) |
||
| 133 | { |
||
| 134 | $littlelink_name = env('HOME_URL'); |
||
| 135 | $id = User::select('id')->where('littlelink_name', $littlelink_name)->value('id'); |
||
| 136 | |||
| 137 | if (empty($id)) { |
||
| 138 | return abort(404); |
||
| 139 | } |
||
| 140 | |||
| 141 | $userinfo = User::select('id', 'name', 'littlelink_name', 'littlelink_description', 'theme', 'role', 'block')->where('id', $id)->first(); |
||
| 142 | $information = User::select('name', 'littlelink_name', 'littlelink_description', 'theme')->where('id', $id)->get(); |
||
| 143 | |||
| 144 | $links = DB::table('links')->join('buttons', 'buttons.id', '=', 'links.button_id')->select('links.link', 'links.id', 'links.button_id', 'links.title', 'links.custom_css', 'links.custom_icon', 'buttons.name')->where('user_id', $id)->orderBy('up_link', 'asc')->orderBy('order', 'asc')->get(); |
||
| 145 | |||
| 146 | return view('linkstack.linkstack', ['userinfo' => $userinfo, 'information' => $information, 'links' => $links, 'littlelink_name' => $littlelink_name]); |
||
| 147 | } |
||
| 148 | |||
| 149 | //Redirect to user page |
||
| 150 | public function userRedirect(request $request) |
||
| 151 | { |
||
| 152 | $id = $request->id; |
||
| 153 | $user = User::select('littlelink_name')->where('id', $id)->value('littlelink_name'); |
||
| 154 | |||
| 155 | if (empty($id)) { |
||
| 156 | return abort(404); |
||
| 157 | } |
||
| 158 | |||
| 159 | if (empty($user)) { |
||
| 160 | return abort(404); |
||
| 161 | } |
||
| 162 | |||
| 163 | return redirect(url('@'.$user)); |
||
| 164 | } |
||
| 165 | |||
| 166 | //Show add/update form |
||
| 167 | public function AddUpdateLink($id = 0) |
||
| 182 | } |
||
| 183 | |||
| 184 | //Save add link |
||
| 185 | public function saveLink(Request $request) |
||
| 186 | { |
||
| 187 | // Step 1: Validate Request |
||
| 188 | // $request->validate([ |
||
| 189 | // 'link' => 'sometimes|url', |
||
| 190 | // ]); |
||
| 191 | |||
| 192 | // Step 2: Determine Link Type and Title |
||
| 193 | $linkType = LinkType::findByTypename($request->typename); |
||
| 194 | $LinkTitle = $request->title; |
||
| 195 | $LinkURL = $request->link; |
||
| 196 | |||
| 197 | // Step 3: Load Link Type Logic |
||
| 198 | if($request->typename == 'predefined' || $request->typename == 'link') { |
||
| 199 | // Determine button id based on whether a custom or predefined button is used |
||
| 200 | $button_id = ($request->typename == 'link') ? ($request->GetSiteIcon == 1 ? 2 : 1) : null; |
||
| 201 | $button = ($request->typename != 'link') ? Button::where('name', $request->button)->first() : null; |
||
| 202 | |||
| 203 | $linkData = [ |
||
| 204 | 'link' => $LinkURL, |
||
| 205 | 'title' => $LinkTitle ?? $button?->alt, |
||
| 206 | 'user_id' => Auth::user()->id, |
||
| 207 | 'button_id' => $button?->id ?? $button_id, |
||
| 208 | 'type' => $request->typename // Save the link type |
||
| 209 | ]; |
||
| 210 | } else { |
||
| 211 | $linkTypePath = base_path("blocks/{$linkType->typename}/handler.php"); |
||
| 212 | if (file_exists($linkTypePath)) { |
||
| 213 | include $linkTypePath; |
||
| 214 | $linkData = handleLinkType($request, $linkType); |
||
| 215 | $linkData['button_id'] = $linkData['button_id'] ?? 1; // Set 'button_id' unless overwritten by handleLinkType |
||
| 216 | $linkData['type'] = $linkType->typename; // Ensure 'type' is included in $linkData |
||
| 217 | } else { |
||
| 218 | abort(404, "Link type logic not found."); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | // Step 4: Handle Custom Parameters |
||
| 223 | // (Same as before) |
||
| 224 | |||
| 225 | // Step 5: User and Button Information |
||
| 226 | $userId = Auth::user()->id; |
||
| 227 | $button = Button::where('name', $request->button)->first(); |
||
| 228 | if ($button && empty($LinkTitle)) $LinkTitle = $button->alt; |
||
| 229 | |||
| 230 | // Step 6: Prepare Link Data |
||
| 231 | // (Handled by the included file) |
||
| 232 | |||
| 233 | // Step 7: Save or Update Link |
||
| 234 | $OrigLink = Link::find($request->linkid); |
||
| 235 | $linkColumns = Schema::getColumnListing('links'); // Get all column names of links table |
||
| 236 | $filteredLinkData = array_intersect_key($linkData, array_flip($linkColumns)); // Filter $linkData to only include keys that are columns in the links table |
||
| 237 | |||
| 238 | // Combine remaining variables into one array and convert to JSON for the type_params column |
||
| 239 | $customParams = array_diff_key($linkData, $filteredLinkData); |
||
| 240 | |||
| 241 | // Check if $linkType->custom_html is defined and not null |
||
| 242 | if (isset($linkType->custom_html)) { |
||
| 243 | // Add $linkType->custom_html to the $customParams array |
||
| 244 | $customParams['custom_html'] = $linkType->custom_html; |
||
| 245 | } |
||
| 246 | |||
| 247 | // Check if $linkType->ignore_container is defined and not null |
||
| 248 | if (isset($linkType->ignore_container)) { |
||
| 249 | // Add $linkType->ignore_container to the $customParams array |
||
| 250 | $customParams['ignore_container'] = $linkType->ignore_container; |
||
| 251 | } |
||
| 252 | |||
| 253 | // Check if $linkType->include_libraries is defined and not null |
||
| 254 | if (isset($linkType->include_libraries)) { |
||
| 255 | // Add $linkType->include_libraries to the $customParams array |
||
| 256 | $customParams['include_libraries'] = $linkType->include_libraries; |
||
| 257 | } |
||
| 258 | |||
| 259 | $filteredLinkData['type_params'] = json_encode($customParams); |
||
| 260 | |||
| 261 | if ($OrigLink) { |
||
| 262 | $currentValues = $OrigLink->getAttributes(); |
||
| 263 | $nonNullFilteredLinkData = array_filter($filteredLinkData, function($value) {return !is_null($value);}); |
||
| 264 | $updatedValues = array_merge($currentValues, $nonNullFilteredLinkData); |
||
| 265 | $OrigLink->update($updatedValues); |
||
| 266 | $message = "Link updated"; |
||
| 267 | } else { |
||
| 268 | $link = new Link($filteredLinkData); |
||
| 269 | $link->user_id = $userId; |
||
| 270 | $link->save(); |
||
| 271 | $message = "Link added"; |
||
| 272 | } |
||
| 273 | |||
| 274 | // Step 8: Redirect |
||
| 275 | $redirectUrl = $request->input('param') == 'add_more' ? 'studio/add-link' : 'studio/links'; |
||
| 276 | return Redirect($redirectUrl)->with('success', $message); |
||
| 277 | } |
||
| 278 | |||
| 279 | public function sortLinks(Request $request) |
||
| 280 | { |
||
| 281 | $linkOrders = $request->input("linkOrders", []); |
||
| 282 | $currentPage = $request->input("currentPage", 1); |
||
| 283 | $perPage = $request->input("perPage", 0); |
||
| 284 | |||
| 285 | if ($perPage == 0) { |
||
| 286 | $currentPage = 1; |
||
| 287 | } |
||
| 288 | |||
| 289 | $linkOrders = array_unique(array_filter($linkOrders)); |
||
| 290 | if (!$linkOrders || $currentPage < 1) { |
||
| 291 | return response()->json([ |
||
| 292 | 'status' => 'ERROR', |
||
| 293 | ]); |
||
| 294 | } |
||
| 295 | |||
| 296 | $newOrder = $perPage * ($currentPage - 1); |
||
| 297 | $linkNewOrders = []; |
||
| 298 | foreach ($linkOrders as $linkId) { |
||
| 299 | if ($linkId < 0) { |
||
| 300 | continue; |
||
| 301 | } |
||
| 302 | |||
| 303 | $linkNewOrders[$linkId] = $newOrder; |
||
| 304 | Link::where("id", $linkId) |
||
| 305 | ->update([ |
||
| 306 | 'order' => $newOrder |
||
| 307 | ]); |
||
| 308 | $newOrder++; |
||
| 309 | } |
||
| 310 | |||
| 311 | return response()->json([ |
||
| 312 | 'status' => 'OK', |
||
| 313 | 'linkOrders' => $linkNewOrders, |
||
| 314 | ]); |
||
| 315 | } |
||
| 316 | |||
| 317 | |||
| 318 | //Count the number of clicks and redirect to link |
||
| 319 | public function clickNumber(request $request) |
||
| 320 | { |
||
| 321 | $linkId = $request->id; |
||
| 322 | |||
| 323 | if (substr($linkId, -1) == '+') { |
||
| 324 | $linkWithoutPlus = str_replace('+', '', $linkId); |
||
| 325 | return redirect(url('info/'.$linkWithoutPlus)); |
||
| 326 | } |
||
| 327 | |||
| 328 | $link = Link::find($linkId); |
||
| 329 | |||
| 330 | if (empty($link)) { |
||
| 331 | return abort(404); |
||
| 332 | } |
||
| 333 | |||
| 334 | $link = $link->link; |
||
| 335 | |||
| 336 | if (empty($linkId)) { |
||
| 337 | return abort(404); |
||
| 338 | } |
||
| 339 | |||
| 340 | Link::where('id', $linkId)->increment('click_number', 1); |
||
| 341 | |||
| 342 | $response = redirect()->away($link); |
||
| 343 | $response->header('X-Robots-Tag', 'noindex, nofollow'); |
||
| 344 | |||
| 345 | return $response; |
||
| 346 | } |
||
| 347 | |||
| 348 | //Download Vcard |
||
| 349 | public function vcard(request $request) |
||
| 350 | { |
||
| 351 | $linkId = $request->id; |
||
| 352 | |||
| 353 | // Find the link with the specified ID |
||
| 354 | $link = Link::findOrFail($linkId); |
||
| 355 | |||
| 356 | $json = $link->link; |
||
| 357 | |||
| 358 | // Decode the JSON to a PHP array |
||
| 359 | $data = json_decode($json, true); |
||
| 360 | |||
| 361 | // Create a new vCard object |
||
| 362 | $vcard = new VCard(); |
||
| 363 | |||
| 364 | // Set the vCard properties from the $data array |
||
| 365 | $vcard->addName($data['last_name'], $data['first_name'], $data['middle_name'], $data['prefix'], $data['suffix']); |
||
| 366 | $vcard->addCompany($data['organization']); |
||
| 367 | $vcard->addJobtitle($data['vtitle']); |
||
| 368 | $vcard->addRole($data['role']); |
||
| 369 | $vcard->addEmail($data['email']); |
||
| 370 | $vcard->addEmail($data['work_email'], 'WORK'); |
||
| 371 | $vcard->addURL($data['work_url'], 'WORK'); |
||
| 372 | $vcard->addPhoneNumber($data['home_phone'], 'HOME'); |
||
| 373 | $vcard->addPhoneNumber($data['work_phone'], 'WORK'); |
||
| 374 | $vcard->addPhoneNumber($data['cell_phone'], 'CELL'); |
||
| 375 | $vcard->addAddress($data['home_address_street'], '', $data['home_address_city'], $data['home_address_state'], $data['home_address_zip'], $data['home_address_country'], 'HOME'); |
||
| 376 | $vcard->addAddress($data['work_address_street'], '', $data['work_address_city'], $data['work_address_state'], $data['work_address_zip'], $data['work_address_country'], 'WORK'); |
||
| 377 | |||
| 378 | |||
| 379 | // $vcard->addPhoto(base_path('img/1.png')); |
||
| 380 | |||
| 381 | // Generate the vCard file contents |
||
| 382 | $file_contents = $vcard->getOutput(); |
||
| 383 | |||
| 384 | // Set the file headers for download |
||
| 385 | $headers = [ |
||
| 386 | 'Content-Type' => 'text/x-vcard', |
||
| 387 | 'Content-Disposition' => 'attachment; filename="contact.vcf"' |
||
| 388 | ]; |
||
| 389 | |||
| 390 | Link::where('id', $linkId)->increment('click_number', 1); |
||
| 391 | |||
| 392 | // Return the file download response |
||
| 393 | return response()->make($file_contents, 200, $headers); |
||
| 394 | |||
| 395 | } |
||
| 396 | |||
| 397 | //Show link, click number, up link in links page |
||
| 398 | public function showLinks() |
||
| 399 | { |
||
| 400 | $userId = Auth::user()->id; |
||
| 401 | $data['pagePage'] = 10; |
||
| 402 | |||
| 403 | $data['links'] = Link::select()->where('user_id', $userId)->orderBy('up_link', 'asc')->orderBy('order', 'asc')->paginate(99999); |
||
| 404 | return view('studio/links', $data); |
||
| 405 | } |
||
| 406 | |||
| 407 | //Delete link |
||
| 408 | public function deleteLink(request $request) |
||
| 409 | { |
||
| 410 | $linkId = $request->id; |
||
| 411 | |||
| 412 | Link::where('id', $linkId)->delete(); |
||
| 413 | |||
| 414 | $directory = base_path("assets/favicon/icons"); |
||
| 415 | $files = scandir($directory); |
||
| 416 | foreach($files as $file) { |
||
| 417 | if (strpos($file, $linkId.".") !== false) { |
||
| 418 | $pathinfo = pathinfo($file, PATHINFO_EXTENSION);}} |
||
| 419 | if (isset($pathinfo)) { |
||
| 420 | try{File::delete(base_path("assets/favicon/icons")."/".$linkId.".".$pathinfo);} catch (exception $e) {} |
||
| 421 | } |
||
| 422 | |||
| 423 | return redirect('/studio/links'); |
||
| 424 | } |
||
| 425 | |||
| 426 | //Delete icon |
||
| 427 | public function clearIcon(request $request) |
||
| 428 | { |
||
| 429 | $linkId = $request->id; |
||
| 430 | |||
| 431 | $directory = base_path("assets/favicon/icons"); |
||
| 432 | $files = scandir($directory); |
||
| 433 | foreach($files as $file) { |
||
| 434 | if (strpos($file, $linkId.".") !== false) { |
||
| 435 | $pathinfo = pathinfo($file, PATHINFO_EXTENSION);}} |
||
| 436 | if (isset($pathinfo)) { |
||
| 437 | try{File::delete(base_path("assets/favicon/icons")."/".$linkId.".".$pathinfo);} catch (exception $e) {} |
||
| 438 | } |
||
| 439 | |||
| 440 | return redirect('/studio/links'); |
||
| 441 | } |
||
| 442 | |||
| 443 | //Raise link on the littlelink page |
||
| 444 | public function upLink(request $request) |
||
| 445 | { |
||
| 446 | $linkId = $request->id; |
||
| 447 | $upLink = $request->up; |
||
| 448 | |||
| 449 | if ($upLink == 'yes') { |
||
| 450 | $up = 'no'; |
||
| 451 | } elseif ($upLink == 'no') { |
||
| 452 | $up = 'yes'; |
||
| 453 | } |
||
| 454 | |||
| 455 | Link::where('id', $linkId)->update(['up_link' => $up]); |
||
| 456 | |||
| 457 | return back(); |
||
| 458 | } |
||
| 459 | |||
| 460 | //Show link to edit |
||
| 461 | public function showLink(request $request) |
||
| 462 | { |
||
| 463 | $linkId = $request->id; |
||
| 464 | |||
| 465 | $link = Link::where('id', $linkId)->value('link'); |
||
| 466 | $title = Link::where('id', $linkId)->value('title'); |
||
| 467 | $order = Link::where('id', $linkId)->value('order'); |
||
| 468 | $custom_css = Link::where('id', $linkId)->value('custom_css'); |
||
| 469 | $buttonId = Link::where('id', $linkId)->value('button_id'); |
||
| 470 | $buttonName = Button::where('id', $buttonId)->value('name'); |
||
| 471 | |||
| 472 | $buttons = Button::select('id', 'name')->orderBy('name', 'asc')->get(); |
||
| 473 | |||
| 474 | return view('studio/edit-link', ['custom_css' => $custom_css, 'buttonId' => $buttonId, 'buttons' => $buttons, 'link' => $link, 'title' => $title, 'order' => $order, 'id' => $linkId, 'buttonName' => $buttonName]); |
||
| 475 | } |
||
| 476 | |||
| 477 | //Show custom CSS + custom icon |
||
| 478 | public function showCSS(request $request) |
||
| 479 | { |
||
| 480 | $linkId = $request->id; |
||
| 481 | |||
| 482 | $link = Link::where('id', $linkId)->value('link'); |
||
| 483 | $title = Link::where('id', $linkId)->value('title'); |
||
| 484 | $order = Link::where('id', $linkId)->value('order'); |
||
| 485 | $custom_css = Link::where('id', $linkId)->value('custom_css'); |
||
| 486 | $custom_icon = Link::where('id', $linkId)->value('custom_icon'); |
||
| 487 | $buttonId = Link::where('id', $linkId)->value('button_id'); |
||
| 488 | |||
| 489 | $buttons = Button::select('id', 'name')->get(); |
||
| 490 | |||
| 491 | return view('studio/button-editor', ['custom_icon' => $custom_icon, 'custom_css' => $custom_css, 'buttonId' => $buttonId, 'buttons' => $buttons, 'link' => $link, 'title' => $title, 'order' => $order, 'id' => $linkId]); |
||
| 492 | } |
||
| 493 | |||
| 494 | //Save edit link |
||
| 495 | public function editLink(request $request) |
||
| 496 | { |
||
| 497 | $request->validate([ |
||
| 498 | 'link' => 'required|exturl', |
||
| 499 | 'title' => 'required', |
||
| 500 | 'button' => 'required', |
||
| 501 | ]); |
||
| 502 | |||
| 503 | if (stringStartsWith($request->link, 'http://') == 'true' or stringStartsWith($request->link, 'https://') == 'true' or stringStartsWith($request->link, 'mailto:') == 'true') |
||
| 504 | $link1 = $request->link; |
||
| 505 | else |
||
| 506 | $link1 = 'https://' . $request->link; |
||
| 507 | if (stringEndsWith($request->link, '/') == 'true') |
||
| 508 | $link = rtrim($link1, "/ "); |
||
| 509 | else |
||
| 510 | $link = $link1; |
||
| 511 | $title = $request->title; |
||
| 512 | $order = $request->order; |
||
| 513 | $button = $request->button; |
||
| 514 | $linkId = $request->id; |
||
| 515 | |||
| 516 | $buttonId = Button::select('id')->where('name', $button)->value('id'); |
||
| 517 | |||
| 518 | Link::where('id', $linkId)->update(['link' => $link, 'title' => $title, 'order' => $order, 'button_id' => $buttonId]); |
||
| 519 | |||
| 520 | return redirect('/studio/links'); |
||
| 521 | } |
||
| 522 | |||
| 523 | //Save edit custom CSS + custom icon |
||
| 524 | public function editCSS(request $request) |
||
| 525 | { |
||
| 526 | $linkId = $request->id; |
||
| 527 | $custom_icon = $request->custom_icon; |
||
| 528 | $custom_css = $request->custom_css; |
||
| 529 | |||
| 530 | if ($request->custom_css == "" and $request->custom_icon = !"") { |
||
| 531 | Link::where('id', $linkId)->update(['custom_icon' => $custom_icon]); |
||
| 532 | } elseif ($request->custom_icon == "" and $request->custom_css = !"") { |
||
| 533 | Link::where('id', $linkId)->update(['custom_css' => $custom_css]); |
||
| 534 | } else { |
||
| 535 | Link::where('id', $linkId)->update([]); |
||
| 536 | } |
||
| 537 | return Redirect('#result'); |
||
| 538 | } |
||
| 539 | |||
| 540 | //Show littlelinke page for edit |
||
| 541 | public function showPage(request $request) |
||
| 542 | { |
||
| 543 | $userId = Auth::user()->id; |
||
| 544 | |||
| 545 | $data['pages'] = User::where('id', $userId)->select('littlelink_name', 'littlelink_description', 'image', 'name')->get(); |
||
| 546 | |||
| 547 | return view('/studio/page', $data); |
||
| 548 | } |
||
| 549 | |||
| 550 | //Save littlelink page (name, description, logo) |
||
| 551 | public function editPage(Request $request) |
||
| 627 | } |
||
| 628 | |||
| 629 | //Upload custom theme background image |
||
| 630 | public function themeBackground(Request $request) |
||
| 631 | { |
||
| 632 | $userId = Auth::user()->id; |
||
| 633 | $littlelink_name = Auth::user()->littlelink_name; |
||
| 634 | |||
| 635 | $request->validate([ |
||
| 636 | 'image' => 'required|image|mimes:jpeg,jpg,png,webp,gif|max:2048', // Max file size: 2MB |
||
| 637 | ], [ |
||
| 638 | 'image.required' => __('messages.Please select an image'), |
||
| 639 | 'image.image' => __('messages.The selected file must be an image'), |
||
| 640 | 'image.mimes' => __('messages.The image must be') . ' JPEG, JPG, PNG, webP, GIF.', |
||
| 641 | 'image.max' => __('messages.The image size should not exceed 2MB'), |
||
| 642 | ]); |
||
| 643 | |||
| 644 | $customBackground = $request->file('image'); |
||
| 645 | |||
| 646 | if ($customBackground) { |
||
| 647 | $directory = base_path('assets/img/background-img/'); |
||
| 648 | $files = scandir($directory); |
||
| 649 | $pathinfo = "error.error"; |
||
| 650 | foreach ($files as $file) { |
||
| 651 | if (strpos($file, $userId . '.') !== false) { |
||
| 652 | $pathinfo = $userId . "." . pathinfo($file, PATHINFO_EXTENSION); |
||
| 653 | } |
||
| 654 | } |
||
| 655 | |||
| 656 | // Delete the user's current background image if it exists |
||
| 657 | while (findBackground($userId) !== "error.error") { |
||
| 658 | $avatarName = "assets/img/background-img/" . findBackground(Auth::id()); |
||
| 659 | unlink(base_path($avatarName)); |
||
| 660 | } |
||
| 661 | |||
| 662 | $fileName = $userId . '_' . time() . "." . $customBackground->extension(); |
||
| 663 | $customBackground->move(base_path('assets/img/background-img/'), $fileName); |
||
| 664 | |||
| 665 | if (extension_loaded('imagick')) { |
||
| 666 | $imagePath = base_path('assets/img/background-img/') . $fileName; |
||
| 667 | $image = new \Imagick($imagePath); |
||
| 668 | $image->stripImage(); |
||
| 669 | $image->writeImage($imagePath); |
||
| 670 | } |
||
| 671 | |||
| 672 | return redirect('/studio/theme'); |
||
| 673 | } |
||
| 674 | |||
| 675 | return redirect('/studio/theme')->with('error', 'Please select a valid image file.'); |
||
| 676 | } |
||
| 677 | |||
| 678 | //Delete custom background image |
||
| 679 | public function removeBackground() |
||
| 680 | { |
||
| 681 | $userId = Auth::user()->id; |
||
| 682 | |||
| 683 | // Delete the user's current background image if it exists |
||
| 684 | while (findBackground($userId) !== "error.error") { |
||
| 685 | $avatarName = "assets/img/background-img/" . findBackground(Auth::id()); |
||
| 686 | unlink(base_path($avatarName)); |
||
| 687 | } |
||
| 688 | |||
| 689 | return back(); |
||
| 690 | } |
||
| 691 | |||
| 692 | |||
| 693 | //Show custom theme |
||
| 694 | public function showTheme(request $request) |
||
| 695 | { |
||
| 696 | $userId = Auth::user()->id; |
||
| 697 | |||
| 698 | $data['pages'] = User::where('id', $userId)->select('littlelink_name', 'theme')->get(); |
||
| 699 | |||
| 700 | return view('/studio/theme', $data); |
||
| 701 | } |
||
| 702 | |||
| 703 | //Save custom theme |
||
| 704 | public function editTheme(request $request) |
||
| 705 | { |
||
| 706 | $request->validate([ |
||
| 707 | 'zip' => 'sometimes|mimes:zip', |
||
| 708 | ]); |
||
| 709 | |||
| 710 | $userId = Auth::user()->id; |
||
| 711 | |||
| 712 | $zipfile = $request->file('zip'); |
||
| 713 | |||
| 714 | $theme = $request->theme; |
||
| 715 | $message = ""; |
||
| 716 | |||
| 717 | User::where('id', $userId)->update(['theme' => $theme]); |
||
| 718 | |||
| 719 | |||
| 720 | |||
| 721 | if (!empty($zipfile)) { |
||
| 722 | |||
| 723 | $zipfile->move(base_path('/themes'), "temp.zip"); |
||
| 724 | |||
| 725 | $zip = new ZipArchive; |
||
| 726 | $zip->open(base_path() . '/themes/temp.zip'); |
||
| 727 | $zip->extractTo(base_path() . '/themes'); |
||
| 728 | $zip->close(); |
||
| 729 | unlink(base_path() . '/themes/temp.zip'); |
||
| 730 | |||
| 731 | // Removes version numbers from folder. |
||
| 732 | |||
| 733 | $folder = base_path('themes'); |
||
| 734 | $regex = '/[0-9.-]/'; |
||
| 735 | $files = scandir($folder); |
||
| 736 | |||
| 737 | foreach ($files as $file) { |
||
| 738 | if ($file !== '.' && $file !== '..') { |
||
| 739 | if (preg_match($regex, $file)) { |
||
| 740 | $new_file = preg_replace($regex, '', $file); |
||
| 741 | File::copyDirectory($folder . '/' . $file, $folder . '/' . $new_file); |
||
| 742 | $dirname = $folder . '/' . $file; |
||
| 743 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
||
| 744 | system('rmdir ' . escapeshellarg($dirname) . ' /s /q'); |
||
| 745 | } else { |
||
| 746 | system("rm -rf " . escapeshellarg($dirname)); |
||
| 747 | } |
||
| 748 | } |
||
| 749 | } |
||
| 750 | } |
||
| 751 | } |
||
| 752 | |||
| 753 | |||
| 754 | return Redirect('/studio/theme')->with("success", $message); |
||
| 755 | } |
||
| 756 | |||
| 757 | //Show user (name, email, password) |
||
| 758 | public function showProfile(request $request) |
||
| 759 | { |
||
| 760 | $userId = Auth::user()->id; |
||
| 761 | |||
| 762 | $data['profile'] = User::where('id', $userId)->select('name', 'email', 'role')->get(); |
||
| 763 | |||
| 764 | return view('/studio/profile', $data); |
||
| 765 | } |
||
| 766 | |||
| 767 | //Save user (name, email, password) |
||
| 768 | public function editProfile(request $request) |
||
| 791 | } |
||
| 792 | |||
| 793 | //Show user theme credit page |
||
| 794 | public function theme(request $request) |
||
| 795 | { |
||
| 796 | $littlelink_name = $request->littlelink; |
||
| 797 | $id = User::select('id')->where('littlelink_name', $littlelink_name)->value('id'); |
||
| 798 | |||
| 799 | if (empty($id)) { |
||
| 800 | return abort(404); |
||
| 801 | } |
||
| 802 | |||
| 803 | $userinfo = User::select('name', 'littlelink_name', 'littlelink_description', 'theme')->where('id', $id)->first(); |
||
| 804 | $information = User::select('name', 'littlelink_name', 'littlelink_description', 'theme')->where('id', $id)->get(); |
||
| 805 | |||
| 806 | $links = DB::table('links')->join('buttons', 'buttons.id', '=', 'links.button_id')->select('links.link', 'links.id', 'links.button_id', 'links.title', 'links.custom_css', 'links.custom_icon', 'buttons.name')->where('user_id', $id)->orderBy('up_link', 'asc')->orderBy('order', 'asc')->get(); |
||
| 807 | |||
| 808 | return view('components/theme', ['userinfo' => $userinfo, 'information' => $information, 'links' => $links, 'littlelink_name' => $littlelink_name]); |
||
| 809 | } |
||
| 810 | |||
| 811 | //Delete existing user |
||
| 812 | public function deleteUser(request $request) |
||
| 813 | { |
||
| 814 | |||
| 815 | // echo $request->id; |
||
| 816 | // echo "<br>"; |
||
| 817 | // echo Auth::id(); |
||
| 818 | $id = $request->id; |
||
| 819 | |||
| 820 | if($id == Auth::id() and $id != "1") { |
||
| 821 | |||
| 822 | Link::where('user_id', $id)->delete(); |
||
| 823 | |||
| 824 | $user = User::find($id); |
||
| 825 | |||
| 826 | Schema::disableForeignKeyConstraints(); |
||
| 827 | $user->forceDelete(); |
||
| 828 | Schema::enableForeignKeyConstraints(); |
||
| 829 | } |
||
| 830 | |||
| 831 | return redirect('/'); |
||
| 832 | } |
||
| 833 | |||
| 834 | //Delete profile picture |
||
| 835 | public function delProfilePicture() |
||
| 836 | { |
||
| 837 | $userId = Auth::user()->id; |
||
| 838 | |||
| 839 | // Delete the user's current avatar if it exists |
||
| 840 | while (findAvatar($userId) !== "error.error") { |
||
| 841 | $avatarName = findAvatar($userId); |
||
| 842 | unlink(base_path($avatarName)); |
||
| 843 | } |
||
| 844 | |||
| 845 | return back(); |
||
| 846 | } |
||
| 847 | |||
| 848 | //Export user links |
||
| 849 | public function exportLinks(request $request) |
||
| 850 | { |
||
| 851 | $userId = Auth::id(); |
||
| 852 | $user = User::find($userId); |
||
| 853 | $links = Link::where('user_id', $userId)->get(); |
||
| 854 | |||
| 855 | if (!$user) { |
||
| 856 | // handle the case where the user is null |
||
| 857 | return response()->json(['message' => 'User not found'], 404); |
||
| 858 | } |
||
| 859 | |||
| 860 | $userData['links'] = $links->toArray(); |
||
| 861 | |||
| 862 | $domain = $_SERVER['HTTP_HOST']; |
||
| 863 | $date = date('Y-m-d_H-i-s'); |
||
| 864 | $fileName = "links-$domain-$date.json"; |
||
| 865 | $headers = [ |
||
| 866 | 'Content-Type' => 'application/json', |
||
| 867 | 'Content-Disposition' => 'attachment; filename="'.$fileName.'"', |
||
| 868 | ]; |
||
| 869 | return response()->json($userData, 200, $headers); |
||
| 870 | |||
| 871 | return back(); |
||
| 872 | } |
||
| 873 | |||
| 874 | //Export all user data |
||
| 875 | public function exportAll(Request $request) |
||
| 876 | { |
||
| 877 | $userId = Auth::id(); |
||
| 878 | $user = User::find($userId); |
||
| 879 | $links = Link::where('user_id', $userId)->get(); |
||
| 880 | |||
| 881 | if (!$user) { |
||
| 882 | // handle the case where the user is null |
||
| 883 | return response()->json(['message' => 'User not found'], 404); |
||
| 884 | } |
||
| 885 | |||
| 886 | $userData = $user->toArray(); |
||
| 887 | $userData['links'] = $links->toArray(); |
||
| 888 | |||
| 889 | if (file_exists(base_path(findAvatar($userId)))){ |
||
| 890 | $imagePath = base_path(findAvatar($userId)); |
||
| 891 | $imageData = base64_encode(file_get_contents($imagePath)); |
||
| 892 | $userData['image_data'] = $imageData; |
||
| 893 | |||
| 894 | $imageExtension = pathinfo($imagePath, PATHINFO_EXTENSION); |
||
| 895 | $userData['image_extension'] = $imageExtension; |
||
| 896 | } |
||
| 897 | |||
| 898 | $domain = $_SERVER['HTTP_HOST']; |
||
| 899 | $date = date('Y-m-d_H-i-s'); |
||
| 900 | $fileName = "user_data-$domain-$date.json"; |
||
| 901 | $headers = [ |
||
| 902 | 'Content-Type' => 'application/json', |
||
| 903 | 'Content-Disposition' => 'attachment; filename="'.$fileName.'"', |
||
| 904 | ]; |
||
| 905 | return response()->json($userData, 200, $headers); |
||
| 906 | |||
| 907 | return back(); |
||
| 908 | } |
||
| 909 | |||
| 910 | public function importData(Request $request) |
||
| 1009 | } |
||
| 1010 | } |
||
| 1011 | |||
| 1012 | |||
| 1013 | // Hanle reports |
||
| 1014 | function report(Request $request) |
||
| 1015 | { |
||
| 1016 | $formData = $request->all(); |
||
| 1017 | |||
| 1018 | try { |
||
| 1019 | Mail::to(env('ADMIN_EMAIL'))->send(new ReportSubmissionMail($formData)); |
||
| 1020 | |||
| 1021 | return redirect('report')->with('success', __('messages.report_success')); |
||
| 1022 | } catch (\Exception $e) { |
||
| 1023 | return redirect()->back()->with('error', __('messages.report_error')); |
||
| 1024 | } |
||
| 1025 | } |
||
| 1026 | |||
| 1027 | //Edit/save page icons |
||
| 1028 | public function editIcons(Request $request) |
||
| 1055 | } |
||
| 1056 | |||
| 1057 | private function searchIcon($icon) |
||
| 1058 | { |
||
| 1059 | return DB::table('links') |
||
| 1060 | ->where('user_id', Auth::id()) |
||
| 1061 | ->where('title', $icon) |
||
| 1062 | ->where('button_id', 94) |
||
| 1063 | ->value('id'); |
||
| 1064 | } |
||
| 1065 | |||
| 1066 | private function addIcon($icon, $link) |
||
| 1067 | { |
||
| 1068 | $userId = Auth::user()->id; |
||
| 1069 | $links = new Link; |
||
| 1070 | $links->link = $link; |
||
| 1071 | $links->user_id = $userId; |
||
| 1072 | $links->title = $icon; |
||
| 1073 | $links->button_id = '94'; |
||
| 1074 | $links->save(); |
||
| 1075 | $links->order = ($links->id - 1); |
||
| 1076 | $links->save(); |
||
| 1077 | } |
||
| 1078 | |||
| 1079 | private function updateIcon($icon, $link) |
||
| 1085 | ]); |
||
| 1086 | } |
||
| 1087 | } |