');
// Sanitize attributes and remove JavaScript code
if (!empty($pageDescription)) {
$document = new DOMDocument();
$document->loadHTML($pageDescription, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
// Find all elements that have attributes
$elements = $document->getElementsByTagName('*');
foreach ($elements as $element) {
// Check each attribute of the element
foreach ($element->attributes as $attribute) {
$attributeName = $attribute->nodeName;
// Remove attributes that contain "on" followed by an event name
if (strpos($attributeName, 'on') === 0) {
$element->removeAttribute($attributeName);
}
}
}
// Get the sanitized HTML back
$pageDescription = $document->saveHTML();
}
$name = $request->name;
$checkmark = $request->checkmark;
$sharebtn = $request->sharebtn;
User::where('id', $userId)->update(['littlelink_name' => $pageName, 'littlelink_description' => $pageDescription, 'name' => $name]);
if ($request->hasFile('image')) {
$profilePhoto->move(base_path('assets/img'), $userId . ".png");
}
if($checkmark == "on"){
UserData::saveData($userId, 'checkmark', true);
} else {
UserData::saveData($userId, 'checkmark', false);
}
if($sharebtn == "on"){
UserData::saveData($userId, 'disable-sharebtn', false);
} else {
UserData::saveData($userId, 'disable-sharebtn', true);
}
return Redirect('/studio/page');
}
//Upload custom theme background image
public function themeBackground(request $request)
{
$userId = Auth::user()->id;
$littlelink_name = Auth::user()->littlelink_name;
$customBackground = $request->file('image');
if (!empty($customBackground)) {
$directory = base_path('assets/img/background-img/');
$files = scandir($directory);
$pathinfo = "error.error";
foreach($files as $file) {
if (strpos($file, $userId.'.') !== false) {
$pathinfo = $userId. "." . pathinfo($file, PATHINFO_EXTENSION);
}}
if(file_exists(base_path('assets/img/background-img/').$pathinfo)){File::delete(base_path('assets/img/background-img/').$pathinfo);}
$customBackground->move(base_path('assets/img/background-img/'), $userId.".".$request->file('image')->extension());
}
return Redirect('/studio/theme');
}
//Delete custom background image
public function removeBackground()
{
function findBackground($name){
$directory = base_path('assets/img/background-img/');
$files = scandir($directory);
$pathinfo = "error.error";
foreach($files as $file) {
if (strpos($file, $name.'.') !== false) {
$pathinfo = $name. "." . pathinfo($file, PATHINFO_EXTENSION);
}}
return $pathinfo;
}
$user_id = Auth::user()->id;
$path = findBackground($user_id);
$path = base_path('assets/img/background-img/'.$path);
if (File::exists($path)) {
File::delete($path);
}
return back();
}
//Show custom theme
public function showTheme(request $request)
{
$userId = Auth::user()->id;
$data['pages'] = User::where('id', $userId)->select('littlelink_name', 'theme')->get();
return view('/studio/theme', $data);
}
//Save custom theme
public function editTheme(request $request)
{
$request->validate([
'zip' => 'sometimes|mimes:zip',
]);
$userId = Auth::user()->id;
$zipfile = $request->file('zip');
$theme = $request->theme;
$message = "";
User::where('id', $userId)->update(['theme' => $theme]);
if (!empty($zipfile)) {
$zipfile->move(base_path('/themes'), "temp.zip");
$zip = new ZipArchive;
$zip->open(base_path() . '/themes/temp.zip');
$zip->extractTo(base_path() . '/themes');
$zip->close();
unlink(base_path() . '/themes/temp.zip');
// Removes version numbers from folder.
$folder = base_path('themes');
$regex = '/[0-9.-]/';
$files = scandir($folder);
foreach ($files as $file) {
if ($file !== '.' && $file !== '..') {
if (preg_match($regex, $file)) {
$new_file = preg_replace($regex, '', $file);
File::copyDirectory($folder . '/' . $file, $folder . '/' . $new_file);
$dirname = $folder . '/' . $file;
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
system('rmdir ' . escapeshellarg($dirname) . ' /s /q');
} else {
system("rm -rf " . escapeshellarg($dirname));
}
}
}
}
}
return Redirect('/studio/theme')->with("success", $message);
}
//Show user (name, email, password)
public function showProfile(request $request)
{
$userId = Auth::user()->id;
$data['profile'] = User::where('id', $userId)->select('name', 'email', 'role')->get();
return view('/studio/profile', $data);
}
//Save user (name, email, password)
public function editProfile(request $request)
{
$request->validate([
'name' => 'sometimes|required|unique:users',
'email' => 'sometimes|required|email|unique:users',
'password' => 'sometimes|min:8',
]);
$userId = Auth::user()->id;
$name = $request->name;
$email = $request->email;
$password = Hash::make($request->password);
if ($request->name != '') {
User::where('id', $userId)->update(['name' => $name]);
} elseif ($request->email != '') {
User::where('id', $userId)->update(['email' => $email]);
} elseif ($request->password != '') {
User::where('id', $userId)->update(['password' => $password]);
}
return back();
}
//Show user theme credit page
public function theme(request $request)
{
$littlelink_name = $request->littlelink;
$id = User::select('id')->where('littlelink_name', $littlelink_name)->value('id');
if (empty($id)) {
return abort(404);
}
$userinfo = User::select('name', 'littlelink_name', 'littlelink_description', 'theme')->where('id', $id)->first();
$information = User::select('name', 'littlelink_name', 'littlelink_description', 'theme')->where('id', $id)->get();
$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();
return view('components/theme', ['userinfo' => $userinfo, 'information' => $information, 'links' => $links, 'littlelink_name' => $littlelink_name]);
}
//Delete existing user
public function deleteUser(request $request)
{
// echo $request->id;
// echo "
";
// echo Auth::id();
$id = $request->id;
if($id == Auth::id() and $id != "1") {
$user = User::find($id);
Schema::disableForeignKeyConstraints();
$user->forceDelete();
Schema::enableForeignKeyConstraints();
}
return redirect('/');
}
//Delete profile picture
public function delProfilePicture()
{
$user_id = Auth::user()->id;
$path = base_path(findAvatar($user_id));
if (File::exists($path)) {
File::delete($path);
}
return back();
}
//Export user links
public function exportLinks(request $request)
{
$userId = Auth::id();
$user = User::find($userId);
$links = Link::where('user_id', $userId)->get();
if (!$user) {
// handle the case where the user is null
return response()->json(['message' => 'User not found'], 404);
}
$userData['links'] = $links->toArray();
$domain = $_SERVER['HTTP_HOST'];
$date = date('Y-m-d_H-i-s');
$fileName = "links-$domain-$date.json";
$headers = [
'Content-Type' => 'application/json',
'Content-Disposition' => 'attachment; filename="'.$fileName.'"',
];
return response()->json($userData, 200, $headers);
return back();
}
//Export all user data
public function exportAll(Request $request)
{
$userId = Auth::id();
$user = User::find($userId);
$links = Link::where('user_id', $userId)->get();
if (!$user) {
// handle the case where the user is null
return response()->json(['message' => 'User not found'], 404);
}
$userData = $user->toArray();
$userData['links'] = $links->toArray();
function findAvatar($name){
$directory = base_path('assets/img');
$files = scandir($directory);
$pathinfo = "error.error";
foreach($files as $file) {
if (strpos($file, $name.'.') !== false) {
$pathinfo = "/img/" . $name. "." . pathinfo($file, PATHINFO_EXTENSION);
}}
return $pathinfo;
}
if (file_exists(base_path(findAvatar($userId)))){
$imagePath = base_path(findAvatar($userId));
$imageData = base64_encode(file_get_contents($imagePath));
$userData['image_data'] = $imageData;
$imageExtension = pathinfo($imagePath, PATHINFO_EXTENSION);
$userData['image_extension'] = $imageExtension;
}
$domain = $_SERVER['HTTP_HOST'];
$date = date('Y-m-d_H-i-s');
$fileName = "user_data-$domain-$date.json";
$headers = [
'Content-Type' => 'application/json',
'Content-Disposition' => 'attachment; filename="'.$fileName.'"',
];
return response()->json($userData, 200, $headers);
return back();
}
public function importData(Request $request)
{
try {
// Get the JSON data from the uploaded file
if (!$request->hasFile('import') || !$request->file('import')->isValid()) {
throw new \Exception('File not uploaded or is faulty');
}
$file = $request->file('import');
$jsonString = $file->get();
$userData = json_decode($jsonString, true);
// Update the authenticated user's profile data if defined in the JSON file
$user = auth()->user();
if (isset($userData['name'])) {
$user->name = $userData['name'];
}
if (isset($userData['littlelink_name'])) {
$user->littlelink_name = $userData['littlelink_name'];
}
if (isset($userData['littlelink_description'])) {
$user->littlelink_description = $userData['littlelink_description'];
}
if (isset($userData['image_data'])) {
// Decode the image data from Base64
$imageData = base64_decode($userData['image_data']);
// Save the image to the correct path with the correct file name and extension
$filename = $user->id . '.' . $userData['image_extension'];
file_put_contents(base_path('img/' . $filename), $imageData);
// Update the user's image field with the correct file name
$user->image = $filename;
}
$user->save();
// Delete all links for the authenticated user
Link::where('user_id', $user->id)->delete();
// Loop through each link in $userData and create a new link for the user
foreach ($userData['links'] as $linkData) {
$newLink = new Link();
// Copy over the link data from $linkData to $newLink
$newLink->button_id = $linkData['button_id'];
$newLink->link = $linkData['link'];
$newLink->title = $linkData['title'];
$newLink->order = $linkData['order'];
$newLink->click_number = $linkData['click_number'];
$newLink->up_link = $linkData['up_link'];
$newLink->custom_css = $linkData['custom_css'];
$newLink->custom_icon = $linkData['custom_icon'];
// Set the user ID to the current user's ID
$newLink->user_id = $user->id;
// Save the new link to the database
$newLink->save();
}
return redirect('studio/profile')->with('success', __('messages.Profile updated successfully!'));
} catch (\Exception $e) {
return redirect('studio/profile')->with('error', __('messages.An error occurred while updating your profile.'));
}
}
//Edit/save page icons
public function editIcons(request $request)
{
function searchIcon($icon)
{
$iconId = DB::table('links')
->where('user_id', Auth::id())
->where('title', $icon)
->where('button_id', 94)
->value('id');
if (is_null($iconId)){
return false;
} else {
return $iconId;
}
}
function addIcon($icon, $link){
$userId = Auth::user()->id;
$links = new Link;
$links->link = $link;
$links->user_id = $userId;
$links->title = $icon;
$links->button_id = '94';
$links->save();
$links->order = ($links->id - 1);
$links->save();
}
function updateIcon($icon, $link){
Link::where('id', searchIcon($icon))->update([
'button_id' => 94,
'link' => $link,
'title' => $icon
]);
}
function saveIcon($icon, $link){
if(isset($link)){
if(searchIcon($icon) != NULL){
updateIcon($icon, $link);
}else{
addIcon($icon, $link);}
}
}
saveIcon('mastodon', $request->mastodon);
saveIcon('instagram', $request->instagram);
saveIcon('twitter', $request->twitter);
saveIcon('facebook', $request->facebook);
saveIcon('github', $request->github);
saveIcon('linkedin', $request->linkedin);
saveIcon('tiktok', $request->tiktok);
saveIcon('discord', $request->discord);
saveIcon('youtube', $request->youtube);
saveIcon('snapchat', $request->snapchat);
saveIcon('reddit', $request->reddit);
saveIcon('pinterest', $request->pinterest);
saveIcon('telegram', $request->telegram);
saveIcon('whatsapp', $request->whatsapp);
saveIcon('twitch', $request->twitch);
return Redirect('studio/links#icons');
}
}