| Total Complexity | 138 |
| Total Lines | 1081 |
| Duplicated Lines | 0 % |
| Changes | 29 | ||
| 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) |
||
| 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') |
||
| 145 | ->join('buttons', 'buttons.id', '=', 'links.button_id') |
||
| 146 | ->select('links.*', 'buttons.name') // Assuming 'links.*' to fetch all columns including 'type_params' |
||
| 147 | ->where('user_id', $id) |
||
| 148 | ->orderBy('up_link', 'asc') |
||
| 149 | ->orderBy('order', 'asc') |
||
| 150 | ->get(); |
||
| 151 | |||
| 152 | // Loop through each link to decode 'type_params' and merge it into the link object |
||
| 153 | foreach ($links as $link) { |
||
| 154 | if (!empty($link->type_params)) { |
||
| 155 | // Decode the JSON string into an associative array |
||
| 156 | $typeParams = json_decode($link->type_params, true); |
||
| 157 | if (is_array($typeParams)) { |
||
| 158 | // Merge the associative array into the link object |
||
| 159 | foreach ($typeParams as $key => $value) { |
||
| 160 | $link->$key = $value; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | return view('linkstack.linkstack', ['userinfo' => $userinfo, 'information' => $information, 'links' => $links, 'littlelink_name' => $littlelink_name]); |
||
| 167 | } |
||
| 168 | |||
| 169 | //Redirect to user page |
||
| 170 | public function userRedirect(request $request) |
||
| 171 | { |
||
| 172 | $id = $request->id; |
||
| 173 | $user = User::select('littlelink_name')->where('id', $id)->value('littlelink_name'); |
||
| 174 | |||
| 175 | if (empty($id)) { |
||
| 176 | return abort(404); |
||
| 177 | } |
||
| 178 | |||
| 179 | if (empty($user)) { |
||
| 180 | return abort(404); |
||
| 181 | } |
||
| 182 | |||
| 183 | return redirect(url('@'.$user)); |
||
| 184 | } |
||
| 185 | |||
| 186 | //Show add/update form |
||
| 187 | public function AddUpdateLink($id = 0) |
||
| 202 | } |
||
| 203 | |||
| 204 | //Save add link |
||
| 205 | public function saveLink(Request $request) |
||
| 206 | { |
||
| 207 | // Step 1: Validate Request |
||
| 208 | // $request->validate([ |
||
| 209 | // 'link' => 'sometimes|url', |
||
| 210 | // ]); |
||
| 211 | |||
| 212 | // Step 2: Determine Link Type and Title |
||
| 213 | $linkType = LinkType::findByTypename($request->typename); |
||
| 214 | $LinkTitle = $request->title; |
||
| 215 | $LinkURL = $request->link; |
||
| 216 | |||
| 217 | // Step 3: Load Link Type Logic |
||
| 218 | if($request->typename == 'predefined' || $request->typename == 'link') { |
||
| 219 | // Determine button id based on whether a custom or predefined button is used |
||
| 220 | $button_id = ($request->typename == 'link') ? ($request->GetSiteIcon == 1 ? 2 : 1) : null; |
||
| 221 | $button = ($request->typename != 'link') ? Button::where('name', $request->button)->first() : null; |
||
| 222 | |||
| 223 | $linkData = [ |
||
| 224 | 'link' => $LinkURL, |
||
| 225 | 'title' => $LinkTitle ?? $button?->alt, |
||
| 226 | 'user_id' => Auth::user()->id, |
||
| 227 | 'button_id' => $button?->id ?? $button_id, |
||
| 228 | 'type' => $request->typename // Save the link type |
||
| 229 | ]; |
||
| 230 | } else { |
||
| 231 | $linkTypePath = base_path("blocks/{$linkType->typename}/handler.php"); |
||
| 232 | if (file_exists($linkTypePath)) { |
||
| 233 | include $linkTypePath; |
||
| 234 | $result = handleLinkType($request, $linkType); |
||
| 235 | |||
| 236 | // Extract rules and linkData from the result |
||
| 237 | $rules = $result['rules']; |
||
| 238 | $linkData = $result['linkData']; |
||
| 239 | |||
| 240 | // Validate the request |
||
| 241 | $validator = Validator::make($request->all(), $rules); |
||
| 242 | |||
| 243 | // Check if validation fails |
||
| 244 | if ($validator->fails()) { |
||
| 245 | return back()->withErrors($validator)->withInput(); |
||
| 246 | } |
||
| 247 | |||
| 248 | $linkData['button_id'] = $linkData['button_id'] ?? 1; // Set 'button_id' unless overwritten by handleLinkType |
||
| 249 | $linkData['type'] = $linkType->typename; // Ensure 'type' is included in $linkData |
||
| 250 | } else { |
||
| 251 | abort(404, "Link type logic not found."); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | // Step 4: Handle Custom Parameters |
||
| 256 | // (Same as before) |
||
| 257 | |||
| 258 | // Step 5: User and Button Information |
||
| 259 | $userId = Auth::user()->id; |
||
| 260 | $button = Button::where('name', $request->button)->first(); |
||
| 261 | if ($button && empty($LinkTitle)) $LinkTitle = $button->alt; |
||
| 262 | |||
| 263 | // Step 6: Prepare Link Data |
||
| 264 | // (Handled by the included file) |
||
| 265 | |||
| 266 | // Step 7: Save or Update Link |
||
| 267 | $OrigLink = Link::find($request->linkid); |
||
| 268 | $linkColumns = Schema::getColumnListing('links'); // Get all column names of links table |
||
| 269 | $filteredLinkData = array_intersect_key($linkData, array_flip($linkColumns)); // Filter $linkData to only include keys that are columns in the links table |
||
| 270 | |||
| 271 | // Combine remaining variables into one array and convert to JSON for the type_params column |
||
| 272 | $customParams = array_diff_key($linkData, $filteredLinkData); |
||
| 273 | |||
| 274 | // Check if $linkType->custom_html is defined and not null |
||
| 275 | if (isset($linkType->custom_html)) { |
||
| 276 | // Add $linkType->custom_html to the $customParams array |
||
| 277 | $customParams['custom_html'] = $linkType->custom_html; |
||
| 278 | } |
||
| 279 | |||
| 280 | // Check if $linkType->ignore_container is defined and not null |
||
| 281 | if (isset($linkType->ignore_container)) { |
||
| 282 | // Add $linkType->ignore_container to the $customParams array |
||
| 283 | $customParams['ignore_container'] = $linkType->ignore_container; |
||
| 284 | } |
||
| 285 | |||
| 286 | // Check if $linkType->include_libraries is defined and not null |
||
| 287 | if (isset($linkType->include_libraries)) { |
||
| 288 | // Add $linkType->include_libraries to the $customParams array |
||
| 289 | $customParams['include_libraries'] = $linkType->include_libraries; |
||
| 290 | } |
||
| 291 | |||
| 292 | $filteredLinkData['type_params'] = json_encode($customParams); |
||
| 293 | |||
| 294 | if ($OrigLink) { |
||
| 295 | $currentValues = $OrigLink->getAttributes(); |
||
| 296 | $nonNullFilteredLinkData = array_filter($filteredLinkData, function($value) {return !is_null($value);}); |
||
| 297 | $updatedValues = array_merge($currentValues, $nonNullFilteredLinkData); |
||
| 298 | $OrigLink->update($updatedValues); |
||
| 299 | $message = "Link updated"; |
||
| 300 | } else { |
||
| 301 | $link = new Link($filteredLinkData); |
||
| 302 | $link->user_id = $userId; |
||
| 303 | $link->save(); |
||
| 304 | $message = "Link added"; |
||
| 305 | } |
||
| 306 | |||
| 307 | // Step 8: Redirect |
||
| 308 | $redirectUrl = $request->input('param') == 'add_more' ? 'studio/add-link' : 'studio/links'; |
||
| 309 | return Redirect($redirectUrl)->with('success', $message); |
||
| 310 | } |
||
| 311 | |||
| 312 | public function sortLinks(Request $request) |
||
| 313 | { |
||
| 314 | $linkOrders = $request->input("linkOrders", []); |
||
| 315 | $currentPage = $request->input("currentPage", 1); |
||
| 316 | $perPage = $request->input("perPage", 0); |
||
| 317 | |||
| 318 | if ($perPage == 0) { |
||
| 319 | $currentPage = 1; |
||
| 320 | } |
||
| 321 | |||
| 322 | $linkOrders = array_unique(array_filter($linkOrders)); |
||
| 323 | if (!$linkOrders || $currentPage < 1) { |
||
| 324 | return response()->json([ |
||
| 325 | 'status' => 'ERROR', |
||
| 326 | ]); |
||
| 327 | } |
||
| 328 | |||
| 329 | $newOrder = $perPage * ($currentPage - 1); |
||
| 330 | $linkNewOrders = []; |
||
| 331 | foreach ($linkOrders as $linkId) { |
||
| 332 | if ($linkId < 0) { |
||
| 333 | continue; |
||
| 334 | } |
||
| 335 | |||
| 336 | $linkNewOrders[$linkId] = $newOrder; |
||
| 337 | Link::where("id", $linkId) |
||
| 338 | ->update([ |
||
| 339 | 'order' => $newOrder |
||
| 340 | ]); |
||
| 341 | $newOrder++; |
||
| 342 | } |
||
| 343 | |||
| 344 | return response()->json([ |
||
| 345 | 'status' => 'OK', |
||
| 346 | 'linkOrders' => $linkNewOrders, |
||
| 347 | ]); |
||
| 348 | } |
||
| 349 | |||
| 350 | |||
| 351 | //Count the number of clicks and redirect to link |
||
| 352 | public function clickNumber(request $request) |
||
| 353 | { |
||
| 354 | $linkId = $request->id; |
||
| 355 | |||
| 356 | if (substr($linkId, -1) == '+') { |
||
| 357 | $linkWithoutPlus = str_replace('+', '', $linkId); |
||
| 358 | return redirect(url('info/'.$linkWithoutPlus)); |
||
| 359 | } |
||
| 360 | |||
| 361 | $link = Link::find($linkId); |
||
| 362 | |||
| 363 | if (empty($link)) { |
||
| 364 | return abort(404); |
||
| 365 | } |
||
| 366 | |||
| 367 | $link = $link->link; |
||
| 368 | |||
| 369 | if (empty($linkId)) { |
||
| 370 | return abort(404); |
||
| 371 | } |
||
| 372 | |||
| 373 | Link::where('id', $linkId)->increment('click_number', 1); |
||
| 374 | |||
| 375 | $response = redirect()->away($link); |
||
| 376 | $response->header('X-Robots-Tag', 'noindex, nofollow'); |
||
| 377 | |||
| 378 | return $response; |
||
| 379 | } |
||
| 380 | |||
| 381 | //Download Vcard |
||
| 382 | public function vcard(request $request) |
||
| 383 | { |
||
| 384 | $linkId = $request->id; |
||
| 385 | |||
| 386 | // Find the link with the specified ID |
||
| 387 | $link = Link::findOrFail($linkId); |
||
| 388 | |||
| 389 | $json = $link->link; |
||
| 390 | |||
| 391 | // Decode the JSON to a PHP array |
||
| 392 | $data = json_decode($json, true); |
||
| 393 | |||
| 394 | // Create a new vCard object |
||
| 395 | $vcard = new VCard(); |
||
| 396 | |||
| 397 | // Set the vCard properties from the $data array |
||
| 398 | $vcard->addName($data['last_name'], $data['first_name'], $data['middle_name'], $data['prefix'], $data['suffix']); |
||
| 399 | $vcard->addCompany($data['organization']); |
||
| 400 | $vcard->addJobtitle($data['vtitle']); |
||
| 401 | $vcard->addRole($data['role']); |
||
| 402 | $vcard->addEmail($data['email']); |
||
| 403 | $vcard->addEmail($data['work_email'], 'WORK'); |
||
| 404 | $vcard->addURL($data['work_url'], 'WORK'); |
||
| 405 | $vcard->addPhoneNumber($data['home_phone'], 'HOME'); |
||
| 406 | $vcard->addPhoneNumber($data['work_phone'], 'WORK'); |
||
| 407 | $vcard->addPhoneNumber($data['cell_phone'], 'CELL'); |
||
| 408 | $vcard->addAddress($data['home_address_street'], '', $data['home_address_city'], $data['home_address_state'], $data['home_address_zip'], $data['home_address_country'], 'HOME'); |
||
| 409 | $vcard->addAddress($data['work_address_street'], '', $data['work_address_city'], $data['work_address_state'], $data['work_address_zip'], $data['work_address_country'], 'WORK'); |
||
| 410 | |||
| 411 | |||
| 412 | // $vcard->addPhoto(base_path('img/1.png')); |
||
| 413 | |||
| 414 | // Generate the vCard file contents |
||
| 415 | $file_contents = $vcard->getOutput(); |
||
| 416 | |||
| 417 | // Set the file headers for download |
||
| 418 | $headers = [ |
||
| 419 | 'Content-Type' => 'text/x-vcard', |
||
| 420 | 'Content-Disposition' => 'attachment; filename="contact.vcf"' |
||
| 421 | ]; |
||
| 422 | |||
| 423 | Link::where('id', $linkId)->increment('click_number', 1); |
||
| 424 | |||
| 425 | // Return the file download response |
||
| 426 | return response()->make($file_contents, 200, $headers); |
||
| 427 | |||
| 428 | } |
||
| 429 | |||
| 430 | //Show link, click number, up link in links page |
||
| 431 | public function showLinks() |
||
| 432 | { |
||
| 433 | $userId = Auth::user()->id; |
||
| 434 | $data['pagePage'] = 10; |
||
| 435 | |||
| 436 | $data['links'] = Link::select()->where('user_id', $userId)->orderBy('up_link', 'asc')->orderBy('order', 'asc')->paginate(99999); |
||
| 437 | return view('studio/links', $data); |
||
| 438 | } |
||
| 439 | |||
| 440 | //Delete link |
||
| 441 | public function deleteLink(request $request) |
||
| 442 | { |
||
| 443 | $linkId = $request->id; |
||
| 444 | |||
| 445 | Link::where('id', $linkId)->delete(); |
||
| 446 | |||
| 447 | $directory = base_path("assets/favicon/icons"); |
||
| 448 | $files = scandir($directory); |
||
| 449 | foreach($files as $file) { |
||
| 450 | if (strpos($file, $linkId.".") !== false) { |
||
| 451 | $pathinfo = pathinfo($file, PATHINFO_EXTENSION);}} |
||
| 452 | if (isset($pathinfo)) { |
||
| 453 | try{File::delete(base_path("assets/favicon/icons")."/".$linkId.".".$pathinfo);} catch (exception $e) {} |
||
| 454 | } |
||
| 455 | |||
| 456 | return redirect('/studio/links'); |
||
| 457 | } |
||
| 458 | |||
| 459 | //Delete icon |
||
| 460 | public function clearIcon(request $request) |
||
| 461 | { |
||
| 462 | $linkId = $request->id; |
||
| 463 | |||
| 464 | $directory = base_path("assets/favicon/icons"); |
||
| 465 | $files = scandir($directory); |
||
| 466 | foreach($files as $file) { |
||
| 467 | if (strpos($file, $linkId.".") !== false) { |
||
| 468 | $pathinfo = pathinfo($file, PATHINFO_EXTENSION);}} |
||
| 469 | if (isset($pathinfo)) { |
||
| 470 | try{File::delete(base_path("assets/favicon/icons")."/".$linkId.".".$pathinfo);} catch (exception $e) {} |
||
| 471 | } |
||
| 472 | |||
| 473 | return redirect('/studio/links'); |
||
| 474 | } |
||
| 475 | |||
| 476 | //Raise link on the littlelink page |
||
| 477 | public function upLink(request $request) |
||
| 478 | { |
||
| 479 | $linkId = $request->id; |
||
| 480 | $upLink = $request->up; |
||
| 481 | |||
| 482 | if ($upLink == 'yes') { |
||
| 483 | $up = 'no'; |
||
| 484 | } elseif ($upLink == 'no') { |
||
| 485 | $up = 'yes'; |
||
| 486 | } |
||
| 487 | |||
| 488 | Link::where('id', $linkId)->update(['up_link' => $up]); |
||
| 489 | |||
| 490 | return back(); |
||
| 491 | } |
||
| 492 | |||
| 493 | //Show link to edit |
||
| 494 | public function showLink(request $request) |
||
| 495 | { |
||
| 496 | $linkId = $request->id; |
||
| 497 | |||
| 498 | $link = Link::where('id', $linkId)->value('link'); |
||
| 499 | $title = Link::where('id', $linkId)->value('title'); |
||
| 500 | $order = Link::where('id', $linkId)->value('order'); |
||
| 501 | $custom_css = Link::where('id', $linkId)->value('custom_css'); |
||
| 502 | $buttonId = Link::where('id', $linkId)->value('button_id'); |
||
| 503 | $buttonName = Button::where('id', $buttonId)->value('name'); |
||
| 504 | |||
| 505 | $buttons = Button::select('id', 'name')->orderBy('name', 'asc')->get(); |
||
| 506 | |||
| 507 | return view('studio/edit-link', ['custom_css' => $custom_css, 'buttonId' => $buttonId, 'buttons' => $buttons, 'link' => $link, 'title' => $title, 'order' => $order, 'id' => $linkId, 'buttonName' => $buttonName]); |
||
| 508 | } |
||
| 509 | |||
| 510 | //Show custom CSS + custom icon |
||
| 511 | public function showCSS(request $request) |
||
| 512 | { |
||
| 513 | $linkId = $request->id; |
||
| 514 | |||
| 515 | $link = Link::where('id', $linkId)->value('link'); |
||
| 516 | $title = Link::where('id', $linkId)->value('title'); |
||
| 517 | $order = Link::where('id', $linkId)->value('order'); |
||
| 518 | $custom_css = Link::where('id', $linkId)->value('custom_css'); |
||
| 519 | $custom_icon = Link::where('id', $linkId)->value('custom_icon'); |
||
| 520 | $buttonId = Link::where('id', $linkId)->value('button_id'); |
||
| 521 | |||
| 522 | $buttons = Button::select('id', 'name')->get(); |
||
| 523 | |||
| 524 | 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]); |
||
| 525 | } |
||
| 526 | |||
| 527 | //Save edit link |
||
| 528 | public function editLink(request $request) |
||
| 529 | { |
||
| 530 | $request->validate([ |
||
| 531 | 'link' => 'required|exturl', |
||
| 532 | 'title' => 'required', |
||
| 533 | 'button' => 'required', |
||
| 534 | ]); |
||
| 535 | |||
| 536 | if (stringStartsWith($request->link, 'http://') == 'true' or stringStartsWith($request->link, 'https://') == 'true' or stringStartsWith($request->link, 'mailto:') == 'true') |
||
| 537 | $link1 = $request->link; |
||
| 538 | else |
||
| 539 | $link1 = 'https://' . $request->link; |
||
| 540 | if (stringEndsWith($request->link, '/') == 'true') |
||
| 541 | $link = rtrim($link1, "/ "); |
||
| 542 | else |
||
| 543 | $link = $link1; |
||
| 544 | $title = $request->title; |
||
| 545 | $order = $request->order; |
||
| 546 | $button = $request->button; |
||
| 547 | $linkId = $request->id; |
||
| 548 | |||
| 549 | $buttonId = Button::select('id')->where('name', $button)->value('id'); |
||
| 550 | |||
| 551 | Link::where('id', $linkId)->update(['link' => $link, 'title' => $title, 'order' => $order, 'button_id' => $buttonId]); |
||
| 552 | |||
| 553 | return redirect('/studio/links'); |
||
| 554 | } |
||
| 555 | |||
| 556 | //Save edit custom CSS + custom icon |
||
| 557 | public function editCSS(request $request) |
||
| 558 | { |
||
| 559 | $linkId = $request->id; |
||
| 560 | $custom_icon = $request->custom_icon; |
||
| 561 | $custom_css = $request->custom_css; |
||
| 562 | |||
| 563 | if ($request->custom_css == "" and $request->custom_icon = !"") { |
||
| 564 | Link::where('id', $linkId)->update(['custom_icon' => $custom_icon]); |
||
| 565 | } elseif ($request->custom_icon == "" and $request->custom_css = !"") { |
||
| 566 | Link::where('id', $linkId)->update(['custom_css' => $custom_css]); |
||
| 567 | } else { |
||
| 568 | Link::where('id', $linkId)->update([]); |
||
| 569 | } |
||
| 570 | return Redirect('#result'); |
||
| 571 | } |
||
| 572 | |||
| 573 | //Show littlelinke page for edit |
||
| 574 | public function showPage(request $request) |
||
| 575 | { |
||
| 576 | $userId = Auth::user()->id; |
||
| 577 | |||
| 578 | $data['pages'] = User::where('id', $userId)->select('littlelink_name', 'littlelink_description', 'image', 'name')->get(); |
||
| 579 | |||
| 580 | return view('/studio/page', $data); |
||
| 581 | } |
||
| 582 | |||
| 583 | //Save littlelink page (name, description, logo) |
||
| 584 | public function editPage(Request $request) |
||
| 660 | } |
||
| 661 | |||
| 662 | //Upload custom theme background image |
||
| 663 | public function themeBackground(Request $request) |
||
| 664 | { |
||
| 665 | $userId = Auth::user()->id; |
||
| 666 | $littlelink_name = Auth::user()->littlelink_name; |
||
| 667 | |||
| 668 | $request->validate([ |
||
| 669 | 'image' => 'required|image|mimes:jpeg,jpg,png,webp,gif|max:2048', // Max file size: 2MB |
||
| 670 | ], [ |
||
| 671 | 'image.required' => __('messages.Please select an image'), |
||
| 672 | 'image.image' => __('messages.The selected file must be an image'), |
||
| 673 | 'image.mimes' => __('messages.The image must be') . ' JPEG, JPG, PNG, webP, GIF.', |
||
| 674 | 'image.max' => __('messages.The image size should not exceed 2MB'), |
||
| 675 | ]); |
||
| 676 | |||
| 677 | $customBackground = $request->file('image'); |
||
| 678 | |||
| 679 | if ($customBackground) { |
||
| 680 | $directory = base_path('assets/img/background-img/'); |
||
| 681 | $files = scandir($directory); |
||
| 682 | $pathinfo = "error.error"; |
||
| 683 | foreach ($files as $file) { |
||
| 684 | if (strpos($file, $userId . '.') !== false) { |
||
| 685 | $pathinfo = $userId . "." . pathinfo($file, PATHINFO_EXTENSION); |
||
| 686 | } |
||
| 687 | } |
||
| 688 | |||
| 689 | // Delete the user's current background image if it exists |
||
| 690 | while (findBackground($userId) !== "error.error") { |
||
| 691 | $avatarName = "assets/img/background-img/" . findBackground(Auth::id()); |
||
| 692 | unlink(base_path($avatarName)); |
||
| 693 | } |
||
| 694 | |||
| 695 | $fileName = $userId . '_' . time() . "." . $customBackground->extension(); |
||
| 696 | $customBackground->move(base_path('assets/img/background-img/'), $fileName); |
||
| 697 | |||
| 698 | if (extension_loaded('imagick')) { |
||
| 699 | $imagePath = base_path('assets/img/background-img/') . $fileName; |
||
| 700 | $image = new \Imagick($imagePath); |
||
| 701 | $image->stripImage(); |
||
| 702 | $image->writeImage($imagePath); |
||
| 703 | } |
||
| 704 | |||
| 705 | return redirect('/studio/theme'); |
||
| 706 | } |
||
| 707 | |||
| 708 | return redirect('/studio/theme')->with('error', 'Please select a valid image file.'); |
||
| 709 | } |
||
| 710 | |||
| 711 | //Delete custom background image |
||
| 712 | public function removeBackground() |
||
| 723 | } |
||
| 724 | |||
| 725 | |||
| 726 | //Show custom theme |
||
| 727 | public function showTheme(request $request) |
||
| 734 | } |
||
| 735 | |||
| 736 | //Save custom theme |
||
| 737 | public function editTheme(request $request) |
||
| 738 | { |
||
| 739 | $request->validate([ |
||
| 740 | 'zip' => 'sometimes|mimes:zip', |
||
| 741 | ]); |
||
| 742 | |||
| 743 | $userId = Auth::user()->id; |
||
| 744 | |||
| 745 | $zipfile = $request->file('zip'); |
||
| 746 | |||
| 747 | $theme = $request->theme; |
||
| 748 | $message = ""; |
||
| 749 | |||
| 750 | User::where('id', $userId)->update(['theme' => $theme]); |
||
| 751 | |||
| 752 | |||
| 753 | |||
| 754 | if (!empty($zipfile) && Auth::user()->role == 'admin') { |
||
| 755 | |||
| 756 | $themesPath = base_path('themes'); |
||
| 757 | $tmpPath = base_path() . '/themes/temp.zip'; |
||
| 758 | $zipfile->move($themesPath, "temp.zip"); |
||
| 759 | |||
| 760 | $zip = new ZipArchive; |
||
| 761 | $zip->open($tmpPath); |
||
| 762 | $zip->extractTo($themesPath); |
||
| 763 | $zip->close(); |
||
| 764 | unlink($tmpPath); |
||
| 765 | |||
| 766 | // Removes version numbers from folder. |
||
| 767 | |||
| 768 | $regex = '/[0-9.-]/'; |
||
| 769 | $files = scandir($themesPath); |
||
| 770 | $files = array_diff($files, array('.', '..')); |
||
| 771 | |||
| 772 | foreach ($files as $file) { |
||
| 773 | |||
| 774 | $basename = basename($file); |
||
| 775 | $filePath = $themesPath . '/' . $basename; |
||
| 776 | |||
| 777 | if (!is_dir($filePath)) { |
||
| 778 | |||
| 779 | try { |
||
| 780 | File::delete($filePath); |
||
| 781 | } catch (exception $e) {} |
||
| 782 | |||
| 783 | } |
||
| 784 | |||
| 785 | if (preg_match($regex, $basename)) { |
||
| 786 | |||
| 787 | $newBasename = preg_replace($regex, '', $basename); |
||
| 788 | $newPath = $themesPath . '/' . $newBasename; |
||
| 789 | File::copyDirectory($filePath, $newPath); |
||
| 790 | File::deleteDirectory($filePath); |
||
| 791 | |||
| 792 | } |
||
| 793 | |||
| 794 | } |
||
| 795 | |||
| 796 | } |
||
| 797 | |||
| 798 | |||
| 799 | return Redirect('/studio/theme')->with("success", $message); |
||
| 800 | } |
||
| 801 | |||
| 802 | //Show user (name, email, password) |
||
| 803 | public function showProfile(request $request) |
||
| 804 | { |
||
| 805 | $userId = Auth::user()->id; |
||
| 806 | |||
| 807 | $data['profile'] = User::where('id', $userId)->select('name', 'email', 'role')->get(); |
||
| 808 | |||
| 809 | return view('/studio/profile', $data); |
||
| 810 | } |
||
| 811 | |||
| 812 | //Save user (name, email, password) |
||
| 813 | public function editProfile(request $request) |
||
| 836 | } |
||
| 837 | |||
| 838 | //Show user theme credit page |
||
| 839 | public function theme(request $request) |
||
| 840 | { |
||
| 841 | $littlelink_name = $request->littlelink; |
||
| 842 | $id = User::select('id')->where('littlelink_name', $littlelink_name)->value('id'); |
||
| 843 | |||
| 844 | if (empty($id)) { |
||
| 845 | return abort(404); |
||
| 846 | } |
||
| 847 | |||
| 848 | $userinfo = User::select('name', 'littlelink_name', 'littlelink_description', 'theme')->where('id', $id)->first(); |
||
| 849 | $information = User::select('name', 'littlelink_name', 'littlelink_description', 'theme')->where('id', $id)->get(); |
||
| 850 | |||
| 851 | $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(); |
||
| 852 | |||
| 853 | return view('components/theme', ['userinfo' => $userinfo, 'information' => $information, 'links' => $links, 'littlelink_name' => $littlelink_name]); |
||
| 854 | } |
||
| 855 | |||
| 856 | //Delete existing user |
||
| 857 | public function deleteUser(request $request) |
||
| 858 | { |
||
| 859 | |||
| 860 | // echo $request->id; |
||
| 861 | // echo "<br>"; |
||
| 862 | // echo Auth::id(); |
||
| 863 | $id = $request->id; |
||
| 864 | |||
| 865 | if($id == Auth::id() and $id != "1") { |
||
| 866 | |||
| 867 | Link::where('user_id', $id)->delete(); |
||
| 868 | |||
| 869 | $user = User::find($id); |
||
| 870 | |||
| 871 | Schema::disableForeignKeyConstraints(); |
||
| 872 | $user->forceDelete(); |
||
| 873 | Schema::enableForeignKeyConstraints(); |
||
| 874 | } |
||
| 875 | |||
| 876 | return redirect('/'); |
||
| 877 | } |
||
| 878 | |||
| 879 | //Delete profile picture |
||
| 880 | public function delProfilePicture() |
||
| 881 | { |
||
| 882 | $userId = Auth::user()->id; |
||
| 883 | |||
| 884 | // Delete the user's current avatar if it exists |
||
| 885 | while (findAvatar($userId) !== "error.error") { |
||
| 886 | $avatarName = findAvatar($userId); |
||
| 887 | unlink(base_path($avatarName)); |
||
| 888 | } |
||
| 889 | |||
| 890 | return back(); |
||
| 891 | } |
||
| 892 | |||
| 893 | //Export user links |
||
| 894 | public function exportLinks(request $request) |
||
| 895 | { |
||
| 896 | $userId = Auth::id(); |
||
| 897 | $user = User::find($userId); |
||
| 898 | $links = Link::where('user_id', $userId)->get(); |
||
| 899 | |||
| 900 | if (!$user) { |
||
| 901 | // handle the case where the user is null |
||
| 902 | return response()->json(['message' => 'User not found'], 404); |
||
| 903 | } |
||
| 904 | |||
| 905 | $userData['links'] = $links->toArray(); |
||
| 906 | |||
| 907 | $domain = $_SERVER['HTTP_HOST']; |
||
| 908 | $date = date('Y-m-d_H-i-s'); |
||
| 909 | $fileName = "links-$domain-$date.json"; |
||
| 910 | $headers = [ |
||
| 911 | 'Content-Type' => 'application/json', |
||
| 912 | 'Content-Disposition' => 'attachment; filename="'.$fileName.'"', |
||
| 913 | ]; |
||
| 914 | return response()->json($userData, 200, $headers); |
||
| 915 | |||
| 916 | return back(); |
||
| 917 | } |
||
| 918 | |||
| 919 | //Export all user data |
||
| 920 | public function exportAll(Request $request) |
||
| 921 | { |
||
| 922 | $userId = Auth::id(); |
||
| 923 | $user = User::find($userId); |
||
| 924 | $links = Link::where('user_id', $userId)->get(); |
||
| 925 | |||
| 926 | if (!$user) { |
||
| 927 | // handle the case where the user is null |
||
| 928 | return response()->json(['message' => 'User not found'], 404); |
||
| 929 | } |
||
| 930 | |||
| 931 | $userData = $user->toArray(); |
||
| 932 | $userData['links'] = $links->toArray(); |
||
| 933 | |||
| 934 | if (file_exists(base_path(findAvatar($userId)))){ |
||
| 935 | $imagePath = base_path(findAvatar($userId)); |
||
| 936 | $imageData = base64_encode(file_get_contents($imagePath)); |
||
| 937 | $userData['image_data'] = $imageData; |
||
| 938 | |||
| 939 | $imageExtension = pathinfo($imagePath, PATHINFO_EXTENSION); |
||
| 940 | $userData['image_extension'] = $imageExtension; |
||
| 941 | } |
||
| 942 | |||
| 943 | $domain = $_SERVER['HTTP_HOST']; |
||
| 944 | $date = date('Y-m-d_H-i-s'); |
||
| 945 | $fileName = "user_data-$domain-$date.json"; |
||
| 946 | $headers = [ |
||
| 947 | 'Content-Type' => 'application/json', |
||
| 948 | 'Content-Disposition' => 'attachment; filename="'.$fileName.'"', |
||
| 949 | ]; |
||
| 950 | return response()->json($userData, 200, $headers); |
||
| 951 | |||
| 952 | return back(); |
||
| 953 | } |
||
| 954 | |||
| 955 | public function importData(Request $request) |
||
| 1054 | } |
||
| 1055 | } |
||
| 1056 | |||
| 1057 | |||
| 1058 | // Hanle reports |
||
| 1059 | function report(Request $request) |
||
| 1060 | { |
||
| 1061 | $formData = $request->all(); |
||
| 1062 | |||
| 1063 | try { |
||
| 1064 | Mail::to(env('ADMIN_EMAIL'))->send(new ReportSubmissionMail($formData)); |
||
| 1065 | |||
| 1066 | return redirect('report')->with('success', __('messages.report_success')); |
||
| 1067 | } catch (\Exception $e) { |
||
| 1068 | return redirect()->back()->with('error', __('messages.report_error')); |
||
| 1069 | } |
||
| 1070 | } |
||
| 1071 | |||
| 1072 | //Edit/save page icons |
||
| 1073 | public function editIcons(Request $request) |
||
| 1100 | } |
||
| 1101 | |||
| 1102 | private function searchIcon($icon) |
||
| 1103 | { |
||
| 1104 | return DB::table('links') |
||
| 1105 | ->where('user_id', Auth::id()) |
||
| 1106 | ->where('title', $icon) |
||
| 1107 | ->where('button_id', 94) |
||
| 1108 | ->value('id'); |
||
| 1109 | } |
||
| 1110 | |||
| 1111 | private function addIcon($icon, $link) |
||
| 1112 | { |
||
| 1113 | $userId = Auth::user()->id; |
||
| 1114 | $links = new Link; |
||
| 1115 | $links->link = $link; |
||
| 1116 | $links->user_id = $userId; |
||
| 1117 | $links->title = $icon; |
||
| 1118 | $links->button_id = '94'; |
||
| 1119 | $links->save(); |
||
| 1120 | $links->order = ($links->id - 1); |
||
| 1121 | $links->save(); |
||
| 1122 | } |
||
| 1123 | |||
| 1124 | private function updateIcon($icon, $link) |
||
| 1130 | ]); |
||
| 1131 | } |
||
| 1132 | } |