| Total Complexity | 131 |
| Total Lines | 1044 |
| Duplicated Lines | 0 % |
| Changes | 26 | ||
| 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 | $result = handleLinkType($request, $linkType); |
||
| 215 | |||
| 216 | // Extract rules and linkData from the result |
||
| 217 | $rules = $result['rules']; |
||
| 218 | $linkData = $result['linkData']; |
||
| 219 | |||
| 220 | // Validate the request |
||
| 221 | $validator = Validator::make($request->all(), $rules); |
||
| 222 | |||
| 223 | // Check if validation fails |
||
| 224 | if ($validator->fails()) { |
||
| 225 | return back()->withErrors($validator)->withInput(); |
||
| 226 | } |
||
| 227 | |||
| 228 | $linkData['button_id'] = $linkData['button_id'] ?? 1; // Set 'button_id' unless overwritten by handleLinkType |
||
| 229 | $linkData['type'] = $linkType->typename; // Ensure 'type' is included in $linkData |
||
| 230 | } else { |
||
| 231 | abort(404, "Link type logic not found."); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | // Step 4: Handle Custom Parameters |
||
| 236 | // (Same as before) |
||
| 237 | |||
| 238 | // Step 5: User and Button Information |
||
| 239 | $userId = Auth::user()->id; |
||
| 240 | $button = Button::where('name', $request->button)->first(); |
||
| 241 | if ($button && empty($LinkTitle)) $LinkTitle = $button->alt; |
||
| 242 | |||
| 243 | // Step 6: Prepare Link Data |
||
| 244 | // (Handled by the included file) |
||
| 245 | |||
| 246 | // Step 7: Save or Update Link |
||
| 247 | $OrigLink = Link::find($request->linkid); |
||
| 248 | $linkColumns = Schema::getColumnListing('links'); // Get all column names of links table |
||
| 249 | $filteredLinkData = array_intersect_key($linkData, array_flip($linkColumns)); // Filter $linkData to only include keys that are columns in the links table |
||
| 250 | |||
| 251 | // Combine remaining variables into one array and convert to JSON for the type_params column |
||
| 252 | $customParams = array_diff_key($linkData, $filteredLinkData); |
||
| 253 | |||
| 254 | // Check if $linkType->custom_html is defined and not null |
||
| 255 | if (isset($linkType->custom_html)) { |
||
| 256 | // Add $linkType->custom_html to the $customParams array |
||
| 257 | $customParams['custom_html'] = $linkType->custom_html; |
||
| 258 | } |
||
| 259 | |||
| 260 | // Check if $linkType->ignore_container is defined and not null |
||
| 261 | if (isset($linkType->ignore_container)) { |
||
| 262 | // Add $linkType->ignore_container to the $customParams array |
||
| 263 | $customParams['ignore_container'] = $linkType->ignore_container; |
||
| 264 | } |
||
| 265 | |||
| 266 | // Check if $linkType->include_libraries is defined and not null |
||
| 267 | if (isset($linkType->include_libraries)) { |
||
| 268 | // Add $linkType->include_libraries to the $customParams array |
||
| 269 | $customParams['include_libraries'] = $linkType->include_libraries; |
||
| 270 | } |
||
| 271 | |||
| 272 | $filteredLinkData['type_params'] = json_encode($customParams); |
||
| 273 | |||
| 274 | if ($OrigLink) { |
||
| 275 | $currentValues = $OrigLink->getAttributes(); |
||
| 276 | $nonNullFilteredLinkData = array_filter($filteredLinkData, function($value) {return !is_null($value);}); |
||
| 277 | $updatedValues = array_merge($currentValues, $nonNullFilteredLinkData); |
||
| 278 | $OrigLink->update($updatedValues); |
||
| 279 | $message = "Link updated"; |
||
| 280 | } else { |
||
| 281 | $link = new Link($filteredLinkData); |
||
| 282 | $link->user_id = $userId; |
||
| 283 | $link->save(); |
||
| 284 | $message = "Link added"; |
||
| 285 | } |
||
| 286 | |||
| 287 | // Step 8: Redirect |
||
| 288 | $redirectUrl = $request->input('param') == 'add_more' ? 'studio/add-link' : 'studio/links'; |
||
| 289 | return Redirect($redirectUrl)->with('success', $message); |
||
| 290 | } |
||
| 291 | |||
| 292 | public function sortLinks(Request $request) |
||
| 293 | { |
||
| 294 | $linkOrders = $request->input("linkOrders", []); |
||
| 295 | $currentPage = $request->input("currentPage", 1); |
||
| 296 | $perPage = $request->input("perPage", 0); |
||
| 297 | |||
| 298 | if ($perPage == 0) { |
||
| 299 | $currentPage = 1; |
||
| 300 | } |
||
| 301 | |||
| 302 | $linkOrders = array_unique(array_filter($linkOrders)); |
||
| 303 | if (!$linkOrders || $currentPage < 1) { |
||
| 304 | return response()->json([ |
||
| 305 | 'status' => 'ERROR', |
||
| 306 | ]); |
||
| 307 | } |
||
| 308 | |||
| 309 | $newOrder = $perPage * ($currentPage - 1); |
||
| 310 | $linkNewOrders = []; |
||
| 311 | foreach ($linkOrders as $linkId) { |
||
| 312 | if ($linkId < 0) { |
||
| 313 | continue; |
||
| 314 | } |
||
| 315 | |||
| 316 | $linkNewOrders[$linkId] = $newOrder; |
||
| 317 | Link::where("id", $linkId) |
||
| 318 | ->update([ |
||
| 319 | 'order' => $newOrder |
||
| 320 | ]); |
||
| 321 | $newOrder++; |
||
| 322 | } |
||
| 323 | |||
| 324 | return response()->json([ |
||
| 325 | 'status' => 'OK', |
||
| 326 | 'linkOrders' => $linkNewOrders, |
||
| 327 | ]); |
||
| 328 | } |
||
| 329 | |||
| 330 | |||
| 331 | //Count the number of clicks and redirect to link |
||
| 332 | public function clickNumber(request $request) |
||
| 333 | { |
||
| 334 | $linkId = $request->id; |
||
| 335 | |||
| 336 | if (substr($linkId, -1) == '+') { |
||
| 337 | $linkWithoutPlus = str_replace('+', '', $linkId); |
||
| 338 | return redirect(url('info/'.$linkWithoutPlus)); |
||
| 339 | } |
||
| 340 | |||
| 341 | $link = Link::find($linkId); |
||
| 342 | |||
| 343 | if (empty($link)) { |
||
| 344 | return abort(404); |
||
| 345 | } |
||
| 346 | |||
| 347 | $link = $link->link; |
||
| 348 | |||
| 349 | if (empty($linkId)) { |
||
| 350 | return abort(404); |
||
| 351 | } |
||
| 352 | |||
| 353 | Link::where('id', $linkId)->increment('click_number', 1); |
||
| 354 | |||
| 355 | $response = redirect()->away($link); |
||
| 356 | $response->header('X-Robots-Tag', 'noindex, nofollow'); |
||
| 357 | |||
| 358 | return $response; |
||
| 359 | } |
||
| 360 | |||
| 361 | //Download Vcard |
||
| 362 | public function vcard(request $request) |
||
| 363 | { |
||
| 364 | $linkId = $request->id; |
||
| 365 | |||
| 366 | // Find the link with the specified ID |
||
| 367 | $link = Link::findOrFail($linkId); |
||
| 368 | |||
| 369 | $json = $link->link; |
||
| 370 | |||
| 371 | // Decode the JSON to a PHP array |
||
| 372 | $data = json_decode($json, true); |
||
| 373 | |||
| 374 | // Create a new vCard object |
||
| 375 | $vcard = new VCard(); |
||
| 376 | |||
| 377 | // Set the vCard properties from the $data array |
||
| 378 | $vcard->addName($data['last_name'], $data['first_name'], $data['middle_name'], $data['prefix'], $data['suffix']); |
||
| 379 | $vcard->addCompany($data['organization']); |
||
| 380 | $vcard->addJobtitle($data['vtitle']); |
||
| 381 | $vcard->addRole($data['role']); |
||
| 382 | $vcard->addEmail($data['email']); |
||
| 383 | $vcard->addEmail($data['work_email'], 'WORK'); |
||
| 384 | $vcard->addURL($data['work_url'], 'WORK'); |
||
| 385 | $vcard->addPhoneNumber($data['home_phone'], 'HOME'); |
||
| 386 | $vcard->addPhoneNumber($data['work_phone'], 'WORK'); |
||
| 387 | $vcard->addPhoneNumber($data['cell_phone'], 'CELL'); |
||
| 388 | $vcard->addAddress($data['home_address_street'], '', $data['home_address_city'], $data['home_address_state'], $data['home_address_zip'], $data['home_address_country'], 'HOME'); |
||
| 389 | $vcard->addAddress($data['work_address_street'], '', $data['work_address_city'], $data['work_address_state'], $data['work_address_zip'], $data['work_address_country'], 'WORK'); |
||
| 390 | |||
| 391 | |||
| 392 | // $vcard->addPhoto(base_path('img/1.png')); |
||
| 393 | |||
| 394 | // Generate the vCard file contents |
||
| 395 | $file_contents = $vcard->getOutput(); |
||
| 396 | |||
| 397 | // Set the file headers for download |
||
| 398 | $headers = [ |
||
| 399 | 'Content-Type' => 'text/x-vcard', |
||
| 400 | 'Content-Disposition' => 'attachment; filename="contact.vcf"' |
||
| 401 | ]; |
||
| 402 | |||
| 403 | Link::where('id', $linkId)->increment('click_number', 1); |
||
| 404 | |||
| 405 | // Return the file download response |
||
| 406 | return response()->make($file_contents, 200, $headers); |
||
| 407 | |||
| 408 | } |
||
| 409 | |||
| 410 | //Show link, click number, up link in links page |
||
| 411 | public function showLinks() |
||
| 412 | { |
||
| 413 | $userId = Auth::user()->id; |
||
| 414 | $data['pagePage'] = 10; |
||
| 415 | |||
| 416 | $data['links'] = Link::select()->where('user_id', $userId)->orderBy('up_link', 'asc')->orderBy('order', 'asc')->paginate(99999); |
||
| 417 | return view('studio/links', $data); |
||
| 418 | } |
||
| 419 | |||
| 420 | //Delete link |
||
| 421 | public function deleteLink(request $request) |
||
| 422 | { |
||
| 423 | $linkId = $request->id; |
||
| 424 | |||
| 425 | Link::where('id', $linkId)->delete(); |
||
| 426 | |||
| 427 | $directory = base_path("assets/favicon/icons"); |
||
| 428 | $files = scandir($directory); |
||
| 429 | foreach($files as $file) { |
||
| 430 | if (strpos($file, $linkId.".") !== false) { |
||
| 431 | $pathinfo = pathinfo($file, PATHINFO_EXTENSION);}} |
||
| 432 | if (isset($pathinfo)) { |
||
| 433 | try{File::delete(base_path("assets/favicon/icons")."/".$linkId.".".$pathinfo);} catch (exception $e) {} |
||
| 434 | } |
||
| 435 | |||
| 436 | return redirect('/studio/links'); |
||
| 437 | } |
||
| 438 | |||
| 439 | //Delete icon |
||
| 440 | public function clearIcon(request $request) |
||
| 441 | { |
||
| 442 | $linkId = $request->id; |
||
| 443 | |||
| 444 | $directory = base_path("assets/favicon/icons"); |
||
| 445 | $files = scandir($directory); |
||
| 446 | foreach($files as $file) { |
||
| 447 | if (strpos($file, $linkId.".") !== false) { |
||
| 448 | $pathinfo = pathinfo($file, PATHINFO_EXTENSION);}} |
||
| 449 | if (isset($pathinfo)) { |
||
| 450 | try{File::delete(base_path("assets/favicon/icons")."/".$linkId.".".$pathinfo);} catch (exception $e) {} |
||
| 451 | } |
||
| 452 | |||
| 453 | return redirect('/studio/links'); |
||
| 454 | } |
||
| 455 | |||
| 456 | //Raise link on the littlelink page |
||
| 457 | public function upLink(request $request) |
||
| 458 | { |
||
| 459 | $linkId = $request->id; |
||
| 460 | $upLink = $request->up; |
||
| 461 | |||
| 462 | if ($upLink == 'yes') { |
||
| 463 | $up = 'no'; |
||
| 464 | } elseif ($upLink == 'no') { |
||
| 465 | $up = 'yes'; |
||
| 466 | } |
||
| 467 | |||
| 468 | Link::where('id', $linkId)->update(['up_link' => $up]); |
||
| 469 | |||
| 470 | return back(); |
||
| 471 | } |
||
| 472 | |||
| 473 | //Show link to edit |
||
| 474 | public function showLink(request $request) |
||
| 475 | { |
||
| 476 | $linkId = $request->id; |
||
| 477 | |||
| 478 | $link = Link::where('id', $linkId)->value('link'); |
||
| 479 | $title = Link::where('id', $linkId)->value('title'); |
||
| 480 | $order = Link::where('id', $linkId)->value('order'); |
||
| 481 | $custom_css = Link::where('id', $linkId)->value('custom_css'); |
||
| 482 | $buttonId = Link::where('id', $linkId)->value('button_id'); |
||
| 483 | $buttonName = Button::where('id', $buttonId)->value('name'); |
||
| 484 | |||
| 485 | $buttons = Button::select('id', 'name')->orderBy('name', 'asc')->get(); |
||
| 486 | |||
| 487 | return view('studio/edit-link', ['custom_css' => $custom_css, 'buttonId' => $buttonId, 'buttons' => $buttons, 'link' => $link, 'title' => $title, 'order' => $order, 'id' => $linkId, 'buttonName' => $buttonName]); |
||
| 488 | } |
||
| 489 | |||
| 490 | //Show custom CSS + custom icon |
||
| 491 | public function showCSS(request $request) |
||
| 492 | { |
||
| 493 | $linkId = $request->id; |
||
| 494 | |||
| 495 | $link = Link::where('id', $linkId)->value('link'); |
||
| 496 | $title = Link::where('id', $linkId)->value('title'); |
||
| 497 | $order = Link::where('id', $linkId)->value('order'); |
||
| 498 | $custom_css = Link::where('id', $linkId)->value('custom_css'); |
||
| 499 | $custom_icon = Link::where('id', $linkId)->value('custom_icon'); |
||
| 500 | $buttonId = Link::where('id', $linkId)->value('button_id'); |
||
| 501 | |||
| 502 | $buttons = Button::select('id', 'name')->get(); |
||
| 503 | |||
| 504 | 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]); |
||
| 505 | } |
||
| 506 | |||
| 507 | //Save edit link |
||
| 508 | public function editLink(request $request) |
||
| 509 | { |
||
| 510 | $request->validate([ |
||
| 511 | 'link' => 'required|exturl', |
||
| 512 | 'title' => 'required', |
||
| 513 | 'button' => 'required', |
||
| 514 | ]); |
||
| 515 | |||
| 516 | if (stringStartsWith($request->link, 'http://') == 'true' or stringStartsWith($request->link, 'https://') == 'true' or stringStartsWith($request->link, 'mailto:') == 'true') |
||
| 517 | $link1 = $request->link; |
||
| 518 | else |
||
| 519 | $link1 = 'https://' . $request->link; |
||
| 520 | if (stringEndsWith($request->link, '/') == 'true') |
||
| 521 | $link = rtrim($link1, "/ "); |
||
| 522 | else |
||
| 523 | $link = $link1; |
||
| 524 | $title = $request->title; |
||
| 525 | $order = $request->order; |
||
| 526 | $button = $request->button; |
||
| 527 | $linkId = $request->id; |
||
| 528 | |||
| 529 | $buttonId = Button::select('id')->where('name', $button)->value('id'); |
||
| 530 | |||
| 531 | Link::where('id', $linkId)->update(['link' => $link, 'title' => $title, 'order' => $order, 'button_id' => $buttonId]); |
||
| 532 | |||
| 533 | return redirect('/studio/links'); |
||
| 534 | } |
||
| 535 | |||
| 536 | //Save edit custom CSS + custom icon |
||
| 537 | public function editCSS(request $request) |
||
| 538 | { |
||
| 539 | $linkId = $request->id; |
||
| 540 | $custom_icon = $request->custom_icon; |
||
| 541 | $custom_css = $request->custom_css; |
||
| 542 | |||
| 543 | if ($request->custom_css == "" and $request->custom_icon = !"") { |
||
| 544 | Link::where('id', $linkId)->update(['custom_icon' => $custom_icon]); |
||
| 545 | } elseif ($request->custom_icon == "" and $request->custom_css = !"") { |
||
| 546 | Link::where('id', $linkId)->update(['custom_css' => $custom_css]); |
||
| 547 | } else { |
||
| 548 | Link::where('id', $linkId)->update([]); |
||
| 549 | } |
||
| 550 | return Redirect('#result'); |
||
| 551 | } |
||
| 552 | |||
| 553 | //Show littlelinke page for edit |
||
| 554 | public function showPage(request $request) |
||
| 555 | { |
||
| 556 | $userId = Auth::user()->id; |
||
| 557 | |||
| 558 | $data['pages'] = User::where('id', $userId)->select('littlelink_name', 'littlelink_description', 'image', 'name')->get(); |
||
| 559 | |||
| 560 | return view('/studio/page', $data); |
||
| 561 | } |
||
| 562 | |||
| 563 | //Save littlelink page (name, description, logo) |
||
| 564 | public function editPage(Request $request) |
||
| 640 | } |
||
| 641 | |||
| 642 | //Upload custom theme background image |
||
| 643 | public function themeBackground(Request $request) |
||
| 644 | { |
||
| 645 | $userId = Auth::user()->id; |
||
| 646 | $littlelink_name = Auth::user()->littlelink_name; |
||
| 647 | |||
| 648 | $request->validate([ |
||
| 649 | 'image' => 'required|image|mimes:jpeg,jpg,png,webp,gif|max:2048', // Max file size: 2MB |
||
| 650 | ], [ |
||
| 651 | 'image.required' => __('messages.Please select an image'), |
||
| 652 | 'image.image' => __('messages.The selected file must be an image'), |
||
| 653 | 'image.mimes' => __('messages.The image must be') . ' JPEG, JPG, PNG, webP, GIF.', |
||
| 654 | 'image.max' => __('messages.The image size should not exceed 2MB'), |
||
| 655 | ]); |
||
| 656 | |||
| 657 | $customBackground = $request->file('image'); |
||
| 658 | |||
| 659 | if ($customBackground) { |
||
| 660 | $directory = base_path('assets/img/background-img/'); |
||
| 661 | $files = scandir($directory); |
||
| 662 | $pathinfo = "error.error"; |
||
| 663 | foreach ($files as $file) { |
||
| 664 | if (strpos($file, $userId . '.') !== false) { |
||
| 665 | $pathinfo = $userId . "." . pathinfo($file, PATHINFO_EXTENSION); |
||
| 666 | } |
||
| 667 | } |
||
| 668 | |||
| 669 | // Delete the user's current background image if it exists |
||
| 670 | while (findBackground($userId) !== "error.error") { |
||
| 671 | $avatarName = "assets/img/background-img/" . findBackground(Auth::id()); |
||
| 672 | unlink(base_path($avatarName)); |
||
| 673 | } |
||
| 674 | |||
| 675 | $fileName = $userId . '_' . time() . "." . $customBackground->extension(); |
||
| 676 | $customBackground->move(base_path('assets/img/background-img/'), $fileName); |
||
| 677 | |||
| 678 | if (extension_loaded('imagick')) { |
||
| 679 | $imagePath = base_path('assets/img/background-img/') . $fileName; |
||
| 680 | $image = new \Imagick($imagePath); |
||
| 681 | $image->stripImage(); |
||
| 682 | $image->writeImage($imagePath); |
||
| 683 | } |
||
| 684 | |||
| 685 | return redirect('/studio/theme'); |
||
| 686 | } |
||
| 687 | |||
| 688 | return redirect('/studio/theme')->with('error', 'Please select a valid image file.'); |
||
| 689 | } |
||
| 690 | |||
| 691 | //Delete custom background image |
||
| 692 | public function removeBackground() |
||
| 703 | } |
||
| 704 | |||
| 705 | |||
| 706 | //Show custom theme |
||
| 707 | public function showTheme(request $request) |
||
| 714 | } |
||
| 715 | |||
| 716 | //Save custom theme |
||
| 717 | public function editTheme(request $request) |
||
| 718 | { |
||
| 719 | $request->validate([ |
||
| 720 | 'zip' => 'sometimes|mimes:zip', |
||
| 721 | ]); |
||
| 722 | |||
| 723 | $userId = Auth::user()->id; |
||
| 724 | |||
| 725 | $zipfile = $request->file('zip'); |
||
| 726 | |||
| 727 | $theme = $request->theme; |
||
| 728 | $message = ""; |
||
| 729 | |||
| 730 | User::where('id', $userId)->update(['theme' => $theme]); |
||
| 731 | |||
| 732 | |||
| 733 | |||
| 734 | if (!empty($zipfile)) { |
||
| 735 | |||
| 736 | $zipfile->move(base_path('/themes'), "temp.zip"); |
||
| 737 | |||
| 738 | $zip = new ZipArchive; |
||
| 739 | $zip->open(base_path() . '/themes/temp.zip'); |
||
| 740 | $zip->extractTo(base_path() . '/themes'); |
||
| 741 | $zip->close(); |
||
| 742 | unlink(base_path() . '/themes/temp.zip'); |
||
| 743 | |||
| 744 | // Removes version numbers from folder. |
||
| 745 | |||
| 746 | $folder = base_path('themes'); |
||
| 747 | $regex = '/[0-9.-]/'; |
||
| 748 | $files = scandir($folder); |
||
| 749 | |||
| 750 | foreach ($files as $file) { |
||
| 751 | $basename = basename($file); |
||
| 752 | if (preg_match($regex, $basename)) { |
||
| 753 | $newBasename = preg_replace($regex, '', $basename); |
||
| 754 | $newPath = $folder . '/' . $newBasename; |
||
| 755 | File::copyDirectory($file, $newPath); |
||
| 756 | File::deleteDirectory($file); |
||
| 757 | } |
||
| 758 | } |
||
| 759 | } |
||
| 760 | |||
| 761 | |||
| 762 | return Redirect('/studio/theme')->with("success", $message); |
||
| 763 | } |
||
| 764 | |||
| 765 | //Show user (name, email, password) |
||
| 766 | public function showProfile(request $request) |
||
| 767 | { |
||
| 768 | $userId = Auth::user()->id; |
||
| 769 | |||
| 770 | $data['profile'] = User::where('id', $userId)->select('name', 'email', 'role')->get(); |
||
| 771 | |||
| 772 | return view('/studio/profile', $data); |
||
| 773 | } |
||
| 774 | |||
| 775 | //Save user (name, email, password) |
||
| 776 | public function editProfile(request $request) |
||
| 799 | } |
||
| 800 | |||
| 801 | //Show user theme credit page |
||
| 802 | public function theme(request $request) |
||
| 803 | { |
||
| 804 | $littlelink_name = $request->littlelink; |
||
| 805 | $id = User::select('id')->where('littlelink_name', $littlelink_name)->value('id'); |
||
| 806 | |||
| 807 | if (empty($id)) { |
||
| 808 | return abort(404); |
||
| 809 | } |
||
| 810 | |||
| 811 | $userinfo = User::select('name', 'littlelink_name', 'littlelink_description', 'theme')->where('id', $id)->first(); |
||
| 812 | $information = User::select('name', 'littlelink_name', 'littlelink_description', 'theme')->where('id', $id)->get(); |
||
| 813 | |||
| 814 | $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(); |
||
| 815 | |||
| 816 | return view('components/theme', ['userinfo' => $userinfo, 'information' => $information, 'links' => $links, 'littlelink_name' => $littlelink_name]); |
||
| 817 | } |
||
| 818 | |||
| 819 | //Delete existing user |
||
| 820 | public function deleteUser(request $request) |
||
| 821 | { |
||
| 822 | |||
| 823 | // echo $request->id; |
||
| 824 | // echo "<br>"; |
||
| 825 | // echo Auth::id(); |
||
| 826 | $id = $request->id; |
||
| 827 | |||
| 828 | if($id == Auth::id() and $id != "1") { |
||
| 829 | |||
| 830 | Link::where('user_id', $id)->delete(); |
||
| 831 | |||
| 832 | $user = User::find($id); |
||
| 833 | |||
| 834 | Schema::disableForeignKeyConstraints(); |
||
| 835 | $user->forceDelete(); |
||
| 836 | Schema::enableForeignKeyConstraints(); |
||
| 837 | } |
||
| 838 | |||
| 839 | return redirect('/'); |
||
| 840 | } |
||
| 841 | |||
| 842 | //Delete profile picture |
||
| 843 | public function delProfilePicture() |
||
| 844 | { |
||
| 845 | $userId = Auth::user()->id; |
||
| 846 | |||
| 847 | // Delete the user's current avatar if it exists |
||
| 848 | while (findAvatar($userId) !== "error.error") { |
||
| 849 | $avatarName = findAvatar($userId); |
||
| 850 | unlink(base_path($avatarName)); |
||
| 851 | } |
||
| 852 | |||
| 853 | return back(); |
||
| 854 | } |
||
| 855 | |||
| 856 | //Export user links |
||
| 857 | public function exportLinks(request $request) |
||
| 858 | { |
||
| 859 | $userId = Auth::id(); |
||
| 860 | $user = User::find($userId); |
||
| 861 | $links = Link::where('user_id', $userId)->get(); |
||
| 862 | |||
| 863 | if (!$user) { |
||
| 864 | // handle the case where the user is null |
||
| 865 | return response()->json(['message' => 'User not found'], 404); |
||
| 866 | } |
||
| 867 | |||
| 868 | $userData['links'] = $links->toArray(); |
||
| 869 | |||
| 870 | $domain = $_SERVER['HTTP_HOST']; |
||
| 871 | $date = date('Y-m-d_H-i-s'); |
||
| 872 | $fileName = "links-$domain-$date.json"; |
||
| 873 | $headers = [ |
||
| 874 | 'Content-Type' => 'application/json', |
||
| 875 | 'Content-Disposition' => 'attachment; filename="'.$fileName.'"', |
||
| 876 | ]; |
||
| 877 | return response()->json($userData, 200, $headers); |
||
| 878 | |||
| 879 | return back(); |
||
| 880 | } |
||
| 881 | |||
| 882 | //Export all user data |
||
| 883 | public function exportAll(Request $request) |
||
| 884 | { |
||
| 885 | $userId = Auth::id(); |
||
| 886 | $user = User::find($userId); |
||
| 887 | $links = Link::where('user_id', $userId)->get(); |
||
| 888 | |||
| 889 | if (!$user) { |
||
| 890 | // handle the case where the user is null |
||
| 891 | return response()->json(['message' => 'User not found'], 404); |
||
| 892 | } |
||
| 893 | |||
| 894 | $userData = $user->toArray(); |
||
| 895 | $userData['links'] = $links->toArray(); |
||
| 896 | |||
| 897 | if (file_exists(base_path(findAvatar($userId)))){ |
||
| 898 | $imagePath = base_path(findAvatar($userId)); |
||
| 899 | $imageData = base64_encode(file_get_contents($imagePath)); |
||
| 900 | $userData['image_data'] = $imageData; |
||
| 901 | |||
| 902 | $imageExtension = pathinfo($imagePath, PATHINFO_EXTENSION); |
||
| 903 | $userData['image_extension'] = $imageExtension; |
||
| 904 | } |
||
| 905 | |||
| 906 | $domain = $_SERVER['HTTP_HOST']; |
||
| 907 | $date = date('Y-m-d_H-i-s'); |
||
| 908 | $fileName = "user_data-$domain-$date.json"; |
||
| 909 | $headers = [ |
||
| 910 | 'Content-Type' => 'application/json', |
||
| 911 | 'Content-Disposition' => 'attachment; filename="'.$fileName.'"', |
||
| 912 | ]; |
||
| 913 | return response()->json($userData, 200, $headers); |
||
| 914 | |||
| 915 | return back(); |
||
| 916 | } |
||
| 917 | |||
| 918 | public function importData(Request $request) |
||
| 1017 | } |
||
| 1018 | } |
||
| 1019 | |||
| 1020 | |||
| 1021 | // Hanle reports |
||
| 1022 | function report(Request $request) |
||
| 1023 | { |
||
| 1024 | $formData = $request->all(); |
||
| 1025 | |||
| 1026 | try { |
||
| 1027 | Mail::to(env('ADMIN_EMAIL'))->send(new ReportSubmissionMail($formData)); |
||
| 1028 | |||
| 1029 | return redirect('report')->with('success', __('messages.report_success')); |
||
| 1030 | } catch (\Exception $e) { |
||
| 1031 | return redirect()->back()->with('error', __('messages.report_error')); |
||
| 1032 | } |
||
| 1033 | } |
||
| 1034 | |||
| 1035 | //Edit/save page icons |
||
| 1036 | public function editIcons(Request $request) |
||
| 1063 | } |
||
| 1064 | |||
| 1065 | private function searchIcon($icon) |
||
| 1066 | { |
||
| 1067 | return DB::table('links') |
||
| 1068 | ->where('user_id', Auth::id()) |
||
| 1069 | ->where('title', $icon) |
||
| 1070 | ->where('button_id', 94) |
||
| 1071 | ->value('id'); |
||
| 1072 | } |
||
| 1073 | |||
| 1074 | private function addIcon($icon, $link) |
||
| 1075 | { |
||
| 1076 | $userId = Auth::user()->id; |
||
| 1077 | $links = new Link; |
||
| 1078 | $links->link = $link; |
||
| 1079 | $links->user_id = $userId; |
||
| 1080 | $links->title = $icon; |
||
| 1081 | $links->button_id = '94'; |
||
| 1082 | $links->save(); |
||
| 1083 | $links->order = ($links->id - 1); |
||
| 1084 | $links->save(); |
||
| 1085 | } |
||
| 1086 | |||
| 1087 | private function updateIcon($icon, $link) |
||
| 1093 | ]); |
||
| 1094 | } |
||
| 1095 | } |