|
9 | 9 | use Illuminate\Support\Facades\Route; |
10 | 10 | use Illuminate\Support\Facades\Response; |
11 | 11 | use JeroenDesloovere\VCard\VCard; |
| 12 | +use Illuminate\Validation\Rule; |
| 13 | +use Illuminate\Support\Facades\Validator; |
12 | 14 |
|
13 | 15 | use Auth; |
14 | 16 | use DB; |
@@ -708,68 +710,110 @@ public function showPage(request $request) |
708 | 710 | } |
709 | 711 |
|
710 | 712 | //Save littlelink page (name, description, logo) |
711 | | - public function editPage(request $request) |
| 713 | + public function editPage(Request $request) |
712 | 714 | { |
713 | | - $request->validate([ |
714 | | - 'littlelink_name' => 'sometimes|max:255|string|isunique:users,id,'.Auth::id(), |
715 | | - 'name' => 'sometimes|max:255|string', |
716 | | - ]); |
717 | | - |
718 | 715 | $userId = Auth::user()->id; |
719 | 716 | $littlelink_name = Auth::user()->littlelink_name; |
720 | | - |
| 717 | + |
| 718 | + $validator = Validator::make($request->all(), [ |
| 719 | + 'littlelink_name' => [ |
| 720 | + 'sometimes', |
| 721 | + 'max:255', |
| 722 | + 'string', |
| 723 | + 'isunique:users,id,'.$userId, |
| 724 | + ], |
| 725 | + 'name' => 'sometimes|max:255|string', |
| 726 | + 'image' => 'sometimes|image|mimes:jpeg,jpg,png,webp|max:2048', // Max file size: 2MB |
| 727 | + ], [ |
| 728 | + 'littlelink_name.unique' => 'That handle has already been taken.', |
| 729 | + 'image.image' => 'The selected file must be an image.', |
| 730 | + 'image.mimes' => 'The image must be a: JPEG, JPG, PNG, webP.', |
| 731 | + 'image.max' => 'The image size should not exceed 2MB.', |
| 732 | + ]); |
| 733 | + |
| 734 | + if ($validator->fails()) { |
| 735 | + return redirect('/studio/page')->withErrors($validator)->withInput(); |
| 736 | + } |
| 737 | + |
721 | 738 | $profilePhoto = $request->file('image'); |
722 | 739 | $pageName = $request->littlelink_name; |
723 | | - $pageDescription = strip_tags($request->pageDescription,'<a><p><strong><i><ul><ol><li><blockquote><h2><h3><h4>'); |
| 740 | + $pageDescription = strip_tags($request->pageDescription, '<a><p><strong><i><ul><ol><li><blockquote><h2><h3><h4>'); |
724 | 741 | $pageDescription = preg_replace("/<a([^>]*)>/i", "<a $1 rel=\"noopener noreferrer nofollow\">", $pageDescription); |
725 | 742 | $name = $request->name; |
726 | 743 | $checkmark = $request->checkmark; |
727 | 744 | $sharebtn = $request->sharebtn; |
728 | | - |
729 | | - User::where('id', $userId)->update(['littlelink_name' => $pageName, 'littlelink_description' => $pageDescription, 'name' => $name]); |
730 | | - |
| 745 | + |
| 746 | + User::where('id', $userId)->update([ |
| 747 | + 'littlelink_name' => $pageName, |
| 748 | + 'littlelink_description' => $pageDescription, |
| 749 | + 'name' => $name |
| 750 | + ]); |
| 751 | + |
731 | 752 | if ($request->hasFile('image')) { |
732 | | - $profilePhoto->move(base_path('assets/img'), $userId . '_' . time() . ".png"); |
| 753 | + $fileName = $userId . '_' . time() . "." . $profilePhoto->extension(); |
| 754 | + $profilePhoto->move(base_path('assets/img'), $fileName); |
733 | 755 | } |
734 | | - |
735 | | - if($checkmark == "on"){ |
| 756 | + |
| 757 | + if ($checkmark == "on") { |
736 | 758 | UserData::saveData($userId, 'checkmark', true); |
737 | 759 | } else { |
738 | 760 | UserData::saveData($userId, 'checkmark', false); |
739 | 761 | } |
740 | | - |
741 | | - if($sharebtn == "on"){ |
| 762 | + |
| 763 | + if ($sharebtn == "on") { |
742 | 764 | UserData::saveData($userId, 'disable-sharebtn', false); |
743 | 765 | } else { |
744 | 766 | UserData::saveData($userId, 'disable-sharebtn', true); |
745 | 767 | } |
746 | | - |
| 768 | + |
747 | 769 | return Redirect('/studio/page'); |
748 | 770 | } |
749 | 771 |
|
750 | 772 | //Upload custom theme background image |
751 | | - public function themeBackground(request $request) |
| 773 | + public function themeBackground(Request $request) |
752 | 774 | { |
753 | | - |
754 | 775 | $userId = Auth::user()->id; |
755 | 776 | $littlelink_name = Auth::user()->littlelink_name; |
756 | | - |
| 777 | + |
| 778 | + $request->validate([ |
| 779 | + 'image' => 'required|image|mimes:jpeg,jpg,png,webp,gif|max:2048', // Max file size: 2MB |
| 780 | + ], [ |
| 781 | + 'image.required' => 'Please select an image file.', |
| 782 | + 'image.image' => 'The selected file must be an image.', |
| 783 | + 'image.mimes' => 'The image must be a: JPEG, JPG, PNG, webP, GIF.', |
| 784 | + 'image.max' => 'The image size should not exceed 2MB.', |
| 785 | + ]); |
| 786 | + |
757 | 787 | $customBackground = $request->file('image'); |
758 | | - |
759 | | - if (!empty($customBackground)) { |
| 788 | + |
| 789 | + if ($customBackground) { |
760 | 790 | $directory = base_path('assets/img/background-img/'); |
761 | 791 | $files = scandir($directory); |
762 | 792 | $pathinfo = "error.error"; |
763 | | - foreach($files as $file) { |
764 | | - if (strpos($file, $userId.'.') !== false) { |
765 | | - $pathinfo = $userId. "." . pathinfo($file, PATHINFO_EXTENSION); |
766 | | - }} |
767 | | - if(file_exists(base_path('assets/img/background-img/').$pathinfo)){File::delete(base_path('assets/img/background-img/').$pathinfo);} |
768 | | - |
769 | | - $customBackground->move(base_path('assets/img/background-img/'), $userId . '_' . time() . "." . $request->file('image')->extension()); |
| 793 | + foreach ($files as $file) { |
| 794 | + if (strpos($file, $userId . '.') !== false) { |
| 795 | + $pathinfo = $userId . "." . pathinfo($file, PATHINFO_EXTENSION); |
| 796 | + } |
| 797 | + } |
| 798 | + |
| 799 | + if (file_exists(base_path('assets/img/background-img/') . $pathinfo)) { |
| 800 | + File::delete(base_path('assets/img/background-img/') . $pathinfo); |
| 801 | + } |
| 802 | + |
| 803 | + $fileName = $userId . '_' . time() . "." . $customBackground->extension(); |
| 804 | + $customBackground->move(base_path('assets/img/background-img/'), $fileName); |
| 805 | + |
| 806 | + if (extension_loaded('imagick')) { |
| 807 | + $imagePath = base_path('assets/img/background-img/') . $fileName; |
| 808 | + $image = new \Imagick($imagePath); |
| 809 | + $image->stripImage(); |
| 810 | + $image->writeImage($imagePath); |
| 811 | + } |
| 812 | + |
| 813 | + return redirect('/studio/theme'); |
770 | 814 | } |
771 | | - |
772 | | - return Redirect('/studio/theme'); |
| 815 | + |
| 816 | + return redirect('/studio/theme')->with('error', 'Please select a valid image file.'); |
773 | 817 | } |
774 | 818 |
|
775 | 819 | //Delete custom background image |
|
0 commit comments