- SyncMarks $version $userName
Last login: $userOldLogin
$admenu
0) { e_log(1,"JSON error: ".$jerrmsg); $filename = "import_".substr($client,0,8)."_".time().".json"; file_put_contents($filename,urldecode($_POST['bookmark']),true); die(json_encode($jerrmsg)); } $client = $_POST['client']; $ctype = getClientType($_SERVER['HTTP_USER_AGENT']); $ctime = round(microtime(true) * 1000); delUsermarks($userData['userID']); $armarks = parseJSON($jmarks); updateClient($client, $ctype, $userData, $ctime, true); die(json_encode(importMarks($armarks,$userData['userID']))); break; case "export": e_log(8,"Browser requested bookmark import..."); $client = filter_var($_POST['client'], FILTER_SANITIZE_STRING); $ctype = getClientType($_SERVER['HTTP_USER_AGENT']); $ctime = round(microtime(true) * 1000); $bookmarks = json_encode(getBookmarks($userData)); if($loglevel = 9 && $cexpjson == true) { $filename = "export_".substr($client,0,8)."_".time().".json"; file_put_contents($filename,$bookmarks,true); } echo $bookmarks; e_log(8,count(json_decode($bookmarks))." bookmarks send to client."); updateClient($client, $ctype, $userData, $ctime, true); die(); break; case "getpurl": $client = filter_var($_POST['client'], FILTER_SANITIZE_STRING); $url = validate_url($_POST['url']); $target = (isset($_POST['tg'])) ? filter_var($_POST['tg'], FILTER_SANITIZE_STRING) : '0'; $ctime = time(); $title = getSiteTitle($url); e_log(8,"Received new pushed URL: ".$url); $uidd = $userData['userID']; $query = "INSERT INTO `notifications` (`title`,`message`,`ntime`,`client`,`nloop`,`publish_date`,`userID`) VALUES ('$title', '$url', $ctime, '$target', 1, $ctime, $uidd)"; $erg = db_query($query); if($erg !== 0) echo("URL successfully pushed."); break; case "lsnc": e_log(8,"Get clients lastseen date."); $query = "SELECT MAX(`lastseen`) as lastseen FROM `clients` WHERE `uid` = ".$userData['userID'].";"; $lastSeen = db_query($query)[0]['lastseen']; die($lastSeen); break; case "rmessage": $message = filter_var($_POST['message'], FILTER_VALIDATE_INT); $loop = filter_var($_POST['lp'], FILTER_SANITIZE_STRING) == 'aNoti' ? 1 : 0; e_log(8,"Try to delete notification $message"); $query = "DELETE FROM `notifications` WHERE `userID` = ".$userData['userID']." AND `id` = $message;"; $count = db_query($query); ($count === 1) ? e_log(8,"Notification successfully removed") : e_log(9,"Error, removing notification"); die(notiList($userData['userID'], $loop)); break; case "soption": $option = filter_var($_POST['option'], FILTER_SANITIZE_STRING); $value = filter_var(filter_var($_POST['value'], FILTER_SANITIZE_NUMBER_INT), FILTER_VALIDATE_INT); e_log(8,"Option received: ".$option.":".$value); $oOptionsA = json_decode($userData['uOptions'],true); $oOptionsA[$option] = $value; $query = "UPDATE `users` SET `uOptions`='".json_encode($oOptionsA)."' WHERE `userID`=".$userData['userID'].";"; $count = db_query($query); ($count === 1) ? e_log(8,"Option saved") : e_log(9,"Error, saving option"); echo $count; break; case "getclients": e_log(8,"Try to get list of clients"); $client = filter_var($_POST['client'], FILTER_SANITIZE_STRING); $query = "SELECT `cid`, IFNULL(`cname`, `cid`) `cname`, `ctype`, `lastseen` FROM `clients` WHERE `uid` = ".$userData['userID']." AND NOT `cid` = '$client' ORDER BY 2 COLLATE NOCASE ASC;"; $clientList = db_query($query); e_log(8,"Found ".count($clientList)." clients. Send list to requesting client."); if (!empty($clientList)) { foreach($clientList as $key => $client) { $myObj[$key]['id'] = $client['cid']; $myObj[$key]['name'] = $client['cname']; $myObj[$key]['type'] = $client['ctype']; $myObj[$key]['date'] = $client['lastseen']; } die(json_encode($myObj)); } else { $myObj[0]['id'] = '0'; $myObj[0]['name'] = 'All Clients'; $myObj[0]['type'] = ''; $myObj[0]['date'] = ''; die(json_encode($myObj)); } break; case "tl": e_log(8,"Get testrequest from addon options page"); $client = filter_var($_POST['client'], FILTER_SANITIZE_STRING); $type = getClientType($_SERVER['HTTP_USER_AGENT']); $time = round(microtime(true) * 1000); die(updateClient($client, $type, $userData, $time)); break; case "gname": e_log(8,"Request clientname"); $client = filter_var($_POST['cl'], FILTER_SANITIZE_STRING); $query = "SELECT cname, ctype FROM clients WHERE cid = '$client' and uid = ".$userData['userID'].";"; $clientData = db_query($query)[0]; e_log(8,"Send name '".$clientData['cname']."' back to client"); die(json_encode($clientData)); break; case "gurls": $client = (isset($_POST['client'])) ? filter_var($_POST['client'], FILTER_SANITIZE_STRING) : '0'; e_log(8,"Request pushed sites for client $client"); $query = "SELECT * FROM `notifications` WHERE `nloop` = 1 AND `userID` = ".$userData['userID']." AND `client` IN ('".$client."','0');"; $uOptions = json_decode($userData['uOptions'],true); $notificationData = db_query($query); e_log(8,"Found ".count($notificationData)." links. Will push them to the client."); if (!empty($notificationData)) { foreach($notificationData as $key => $notification) { $myObj[$key]['title'] = html_entity_decode($notification['title'],ENT_QUOTES,'UTF-8'); $myObj[$key]['url'] = $notification['message']; $myObj[$key]['nkey'] = $notification['id']; $myObj[$key]['nOption'] = $uOptions['notifications']; } die(json_encode($myObj)); } else { die(); } break; case "durl": e_log(8,"Hide notification"); $notification = filter_var($_POST['durl'], FILTER_VALIDATE_INT); $query = "UPDATE `notifications` SET `nloop`= 0, `ntime`= '".time()."' WHERE `id` = $notification AND `userID` = ".$userData['userID']; die(db_query($query)); break; case "bmedt": $title = filter_var($_POST['title'], FILTER_SANITIZE_STRING); $id = filter_var($_POST['id'], FILTER_SANITIZE_STRING); e_log(8,"Edit entry '$title'"); $url = strlen($_POST['url']) > 4 ? '\''.validate_url($_POST['url']).'\'' : 'NULL'; $query = "UPDATE `bookmarks` SET `bmTitle` = '$title', `bmURL` = $url, `bmAdded` = '".round(microtime(true) * 1000)."' WHERE `bmID` = '$id' AND `userID` = ".$userData['userID'].";"; $count = db_query($query); ($count > 0) ? die(true) : die(false); break; case "bmmv": $id = filter_var($_POST['id'], FILTER_SANITIZE_STRING); e_log(8,"Move bookmark $id"); $folder = filter_var($_POST['folder'], FILTER_SANITIZE_STRING); $query = "SELECT MAX(bmIndex)+1 AS 'index' FROM `bookmarks` WHERE `bmParentID` = '$folder';"; $folderData = db_query($query); $query = "UPDATE `bookmarks` SET `bmIndex` = ".$folderData[0]['index'].", `bmParentID` = '$folder', `bmAction` = 2, `bmAdded` = '".round(microtime(true) * 1000)."' WHERE `bmID` = '$id' AND `userID` = ".$userData['userID'].";"; $count = db_query($query); ($count > 0) ? die(true) : die(false); break; case "arename": $client = filter_var($_POST['cido'], FILTER_SANITIZE_STRING); $name = filter_var($_POST['nname'], FILTER_SANITIZE_STRING); e_log(8,"Rename client $client to $name"); $query = "UPDATE `clients` SET `cname` = '".$name."' WHERE `uid` = ".$userData['userID']." AND `cid` = '".$client."';"; $count = db_query($query); ($count > 0) ? die(bClientlist($userData['userID'])) : die(false); break; case "adel": $client = filter_var($_POST['cido'], FILTER_SANITIZE_STRING); e_log(8,"Delete client $client"); $query = "DELETE FROM `clients` WHERE `uid` = ".$userData['userID']." AND `cid` = '$client';"; $count = db_query($query); ($count > 0) ? die(bClientlist($userData['userID'])) : die(false); break; case "muedt": $del = false; $headers = "From: PHPMarks <$sender>"; $url = $_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']; $variant = filter_var($_POST['muedt'], FILTER_SANITIZE_STRING); $password = filter_var($_POST['npwd'], FILTER_SANITIZE_STRING); $userLevel = filter_var($_POST['userLevel'], FILTER_VALIDATE_INT) + 1; $user = filter_var($_POST['nuser'], FILTER_SANITIZE_STRING); $uID = filter_var($_POST['userSelect'], FILTER_VALIDATE_INT); switch($variant) { case "Add User": $pwd = password_hash($password,PASSWORD_DEFAULT); e_log(8,"Adding new user $user"); $query = "INSERT INTO `users` (`userName`,`userType`,`userHash`) VALUES ('$user', '$userLevel', '".$pwd."')"; if(db_query($query) == 1) { $message = "Hello,\r\n\r\na account with the following credentials is created and stored encrypted on the database:\r\nE-Mail: $user\r\nPassword: $password\r\n\r\nYou can login at $url"; if(!mail ($user, "Account created",$message,$headers)) e_log(1,"Error sending data for created user account to user"); } break; case "Edit User": $pwd = password_hash($password,PASSWORD_DEFAULT); e_log(8,"Updating user $user"); $query = "UPDATE `users` SET `userName`= '$user', `userType`= '$userLevel', `userHash`= '".$pwd."' WHERE `userID` = $uID;"; if(db_query($query) == 1) { $message = "Hello,\r\n\r\nyour account is changed and stored encrypted on the database. Your new credentials are:\r\nE-Mail: $user\r\nPassword: $password\r\n\r\nYou can login at $url"; if(!mail ($user, "Account changed",$message,$headers)) e_log(1,"Error sending email for changed user account"); } break; case "Delete User": e_log(8,"Delete user $user"); $query = "DELETE FROM `users` WHERE `userID` = $uID;"; if(db_query($query) == 1) { $message = "Hello,\r\n\r\nyour account '$user' and all it's data is removed from $url."; if(!mail ($_POST['nuser'], "Account removed",$message,$headers)) e_log(1,"Error sending data for created user account to user"); } break; default: $message = "Unknown action for managing users"; e_log(1,$message); die($message); } break; case "mlog": e_log(8,"Try to show logfile"); if($userData['userType'] > 1) { die(file_get_contents($logfile)); } else { $message = "Not allowed to read server logfile."; e_log(2,$message); die($message); } break; case "mclear": e_log(8,"Clear logfile"); if($userData['userType'] > 1) file_put_contents($logfile,""); die(); break; case "madd": $bmParentID = filter_var($_POST['folder'], FILTER_SANITIZE_STRING); $bmURL = validate_url(trim($_POST['url'])); e_log(8,"Try to add manually new bookmark ".$bmURL); $bmID = unique_code(12); $bmIndex = getIndex($bmParentID); if(strpos($bmURL,'http') != 0) { e_log(1,"Given string is not a real URL, cant add this."); exit; } $bmTitle = getSiteTitle($bmURL); $bmAdded = round(microtime(true) * 1000); $userID = $userData['userID']; if($bmTitle === "") { $message = "Titel is missing, add bookmark failed"; e_log(1,$message); die($message); } else { $query = "INSERT INTO `bookmarks` (`bmID`,`bmParentID`,`bmIndex`,`bmTitle`,`bmType`,`bmURL`,`bmAdded`,`userID`) VALUES ('".$bmID."', '".$bmParentID."', ".$bmIndex.", '".$bmTitle."', 'bookmark', '".$bmURL."', ".$bmAdded.", ".$userID.")"; db_query($query); } if(!isset($_POST['rc'])) { e_log(8,"Manually added bookmark."); die(bmTree($userData)); } else { die(e_log(8,"Roundcube added bookmark.")); } break; case "mdel": $bmID = filter_var($_POST['id'], FILTER_SANITIZE_STRING); $delMark = delMark($bmID); if($delMark != 0) { if(!isset($_POST['rc'])) { e_log(8,"Deleted bookmark $bmID"); die(); } else { die(e_log(8,"Bookmark $bmID deleted by Roundcube")); } } else { die(e_log(2,"There was an problem removing the bookmark, please check the logfile")); } break; case "pupdate": e_log(8,"Userchange: Updating user password started"); $opassword = filter_var($_POST['opassword'], FILTER_SANITIZE_STRING); $npassword = filter_var($_POST['npassword'], FILTER_SANITIZE_STRING); $cpassword = filter_var($_POST['cpassword'], FILTER_SANITIZE_STRING); if($opassword != "" && $npassword !="" && $cpassword !="") { e_log(8,"Userchange: Data complete entered"); if(password_verify($opassword,$userData['userHash'])) { e_log(8,"Userchange: Verify original password"); if($npassword === $cpassword) { e_log(8,"Userchange: New and confirmed password"); if($npassword != $opassword) { e_log(2,"Userchange: Old and new password NOT identical"); $password = password_hash($npassword,PASSWORD_DEFAULT); $query = "UPDATE `users` SET `userHash`='$password' WHERE `userID`=".$userData['userID'].";"; db_query($query); e_log(8,"Userchange: Password changed"); unset($_SESSION['fauth']); } else { e_log(2,"Userchange: Old and new password identical, user not changed"); } } else { e_log(2,"Userchange: Old and new password are different"); } } else { e_log(2,"Userchange: Old password missmatch"); } } else { e_log(2,"Userchange: Data missing, process failed"); } die(); break; case "pbupdate": e_log(8,"Pushbullet: Updating Pushbullet information."); $password = filter_var($_POST['password'], FILTER_SANITIZE_STRING); $ptoken = filter_var($_POST['ptoken'], FILTER_SANITIZE_STRING); $pdevice = filter_var($_POST['pdevice'], FILTER_SANITIZE_STRING); $pbe = filter_var($_POST['pbe'], FILTER_SANITIZE_STRING); if(password_verify($password,$userData['userHash'])) { $token = edcrpt('en', $ptoken); $device = edcrpt('en', $pdevice); $pbEnable = filter_var($pbe,FILTER_VALIDATE_BOOLEAN) ? '1' : '0'; $oOptionsA = json_decode($userData['uOptions'],true); $oOptionsA['pAPI'] = $token; $oOptionsA['pDevice'] = $device; $oOptionsA['pbEnable'] = $pbEnable; $query = "UPDATE `users` SET `uOptions`='".json_encode($oOptionsA)."' WHERE `userID`=".$userData['userID'].";"; $count = db_query($query); ($count === 1) ? e_log(8,"Option saved") : e_log(9,"Error, saving option"); header("location:".$_SERVER['PHP_SELF']); die(); } else { e_log(1,"Password missmatch. Pushbullet not updated."); die("Password missmatch. Pushbullet not updated."); } break; case "uupdate": e_log(8,"Userchange: Updating user name started"); $opassword = filter_var($_POST['opassword'], FILTER_SANITIZE_STRING); $username = filter_var($_POST['username'], FILTER_SANITIZE_STRING); if($opassword != "") { e_log(8,"Userchange: Data complete entered"); if(password_verify($opassword,$userData['userHash'])) { e_log(8,"Userchange: Verify original password"); $query = "UPDATE `users` SET `userName`='$username' WHERE `userID`=".$userData['userID'].";"; db_query($query); e_log(8,"Userchange: Username changed"); unset($_SESSION['fauth']); } else { e_log(2,"Userchange: Failed to verify original password"); } } else { e_log(2,"Userchange: Data missing"); } die(); break; case "fexport": $format = filter_var($_POST['type'], FILTER_SANITIZE_STRING); switch($format) { case "html": e_log(2,"Exporting in html format for download"); die(html_export($userData)); break; default: die(e_log(2,"Unknown export format, exit process")); } exit; break; case "checkdups": e_log(8,"Checking for duplicated bookmarks by url"); $query = "SELECT `bmID`, `bmTitle`, `bmURL` FROM `bookmarks` WHERE `userID` = ".$userData['userID']." AND `bmAction` ISNULL OR `bmAction` = 2 GROUP BY `bmURL` HAVING COUNT(`bmURL`) > 1;"; $dubData = db_query($query); foreach($dubData as $key => $dub) { $query = "SELECT `bmID`, `bmParentID`, `bmTitle`, `bmAdded` FROM `bookmarks` WHERE `bmURL` = '".$dub['bmURL']."' AND `userID` = ".$userData['userID']." AND `bmAction` ISNULL OR `bmAction` = 2 ORDER BY `bmParentID`, `bmIndex`;"; $subData = db_query($query); foreach($subData as $index => $entry) { $subData[$index]['fway'] = fWay($entry['bmParentID'], $userData['userID'],''); } $dubData[$key]['subs'] = $subData; } die(json_encode($dubData)); break; case "logout": e_log(8,"Logout user ".$_SESSION['sauth']); unset($_SESSION['sauth']); $_SESSION['fauth'] = true; e_log(8,"User logged out"); echo htmlHeader(); echo "
"; return $content; } function editFolder($bm, $ud) { e_log(8,"Edit folder request, try to find the folder..."); $query = "SELECT * FROM `bookmarks` WHERE `bmIndex` >= ".$bm['index']." AND `bmType` = 'folder' AND `bmParentID` = '".$bm['parentId']."' AND `userID` = ".$ud['userID'].";"; $fData = db_query($query); if(count($fData) == 1) { e_log(8,"Unique folder found, edit the folder"); $query = "UPDATE `bookmarks` SET `bmAction` = NULL, `bmTitle` = '".$bm['title']."' WHERE `bmID` = '".$fData[0]['bmID']."' AND userID = ".$ud["userID"].";"; $count = db_query($query); } else { e_log(8,"Folder not found, chancel operation and send error to client."); $count = 0; } return $count; } function editBookmark($bm, $ud) { e_log(8,"Edit bookmark request, try to find the bookmark first by url..."); $query = "SELECT `bmID` FROM `bookmarks` WHERE `bmURL` = '".$bm['url']."' AND `userID` = ".$ud['userID']; $bmData = db_query($query); if(count($bmData) == 1) { e_log(8,"Unique entry found, edit the title of the bookmark."); $query = "UPDATE `bookmarks` SET `bmTitle` = '".$bm['title']."' WHERE `bmID` = '".$bmData[0]['bmID']."' AND userID = ".$ud["userID"].";"; $count = db_query($query); } else { e_log(8,"No unique bookmark found, try to find now by title..."); $query = "SELECT `bmID` FROM `bookmarks` WHERE `bmTitle` = '".$bm['title']."' AND `userID` = ".$ud['userID']; $bmData = db_query($query); if(count($bmData) == 1) { e_log(8,"Unique entry found, edit the url of the bookmark."); $query = "UPDATE `bookmarks` SET `bmURL` = '".$bm['url']."' WHERE `bmID` = '".$bmData[0]['bmID']."' AND userID = ".$ud["userID"].";"; $count = db_query($query); } else { e_log(8,"No Unique entry found, chancel operation and send error to client."); $count = 0; } } return $count; } function moveBookmark($ud, $bm) { e_log(8,"Bookmark seems to be moved, checking current folder data"); $query = "SELECT `bmID`, `bmParentID` FROM `bookmarks` WHERE `bmType` = 'folder' AND `bmTitle` = '".$bm['nfolder']."' AND `userID` = ".$ud['userID'].";"; $folderData = db_query($query)[0]; if(is_null($folderData['bmID'])) { e_log(2,"Folder not found, can`t move bookmark."); return "Folder not found, bookmark not moved."; } if(array_key_exists("url", $bm)) { e_log(8,"Checking bookmark data before moving it"); $query = "SELECT * FROM `bookmarks` WHERE `userID`= ".$ud["userID"]." AND `bmURL` = '".$bm["url"]."';"; $oldData = db_query($query)[0]; if (!empty($folderData) && !empty($oldData)) { if(($folderData['bmParentID'] != $oldData['bmParentID']) || ($oldData['bmIndex'] != $bm['index'])) { e_log(8,"Folder or Position changed, moving bookmark"); $query = "DELETE FROM `bookmarks` WHERE `bmID` = '".$oldData["bmID"]."'"; db_query($query); e_log(8,"Re-Add bookmark on new position"); $query = "INSERT INTO `bookmarks` (`bmID`,`bmParentID`,`bmIndex`,`bmTitle`,`bmType`,`bmURL`,`bmAdded`,`userID`,`bmAction`) VALUES ('".$oldData["bmID"]."', '".$bm['folder']."', ".$bm['index'].", '".$oldData['bmTitle']."', '".$oldData['bmType']."', '".$oldData['bmURL']."', ".$oldData['bmAdded'].", ".$ud["userID"].",2)"; db_query($query); return true; } else { e_log(2,"Bookmark not moved, exiting"); return "Bookmark not moved, exiting"; } } else { return "Cant move bookmark, data not found."; } } else { e_log(8,"url key not found"); } } function addFolder($ud, $bm) { $count = 0; e_log(8,"Try to find if this folder exists already"); $query = "SELECT COUNT(*) AS bmCount, bmAction, bmID FROM `bookmarks` WHERE `bmTitle` = '".$bm['title']."' AND `bmParentID` = '".$bm['folder']."' AND `userID` = ".$ud['userID'].";"; $res = db_query($query)[0]; if($res["bmAction"]) { e_log(8,"Remove temporary entry ".$res["bmID"]); $query = "DELETE FROM `bookmarks` WHERE `bmID` = '".$res["bmID"]."' AND `userID` = ".$ud['userID'].";"; $count = db_query($query); } if($res["bmCount"] > 0 && $count != 1) { e_log(8,"Folder not added, it exists already for this user, exit request"); return false; } e_log(8,"Get folder data for adding folder"); $query = "SELECT IFNULL(MAX(`bmIndex`),-1) + 1 AS `nindex`, `bmParentId` FROM `bookmarks` WHERE `bmParentId` = '".$bm['folder']."' AND `userID` = ".$ud['userID'].";"; $folderData = db_query($query); if (!empty($folderData)) { $query = "INSERT INTO `bookmarks` (`bmID`,`bmParentID`,`bmIndex`,`bmTitle`,`bmType`,`bmAdded`,`userID`) VALUES ('".$bm['id']."', '".$bm['folder']."', ".$folderData[0]['nindex'].", '".$bm['title']."', '".$bm['type']."', ".$bm['added'].", ".$ud["userID"].")"; db_query($query); return true; } else { e_log(1,"Couldn't add folder"); return false; } } function addBookmark($ud, $bm) { e_log(8,"Check if bookmark already exists for user."); $query = "SELECT `bmID`, COUNT(*) AS `bmcount`, MAX(`bmAction`) AS `bmaction` FROM `bookmarks` WHERE `bmUrl` = '".$bm['url']."' AND `bmParentID` = '".$bm["nfolder"]."' AND `userID` = ".$ud["userID"].";"; $bmExistData = db_query($query); if($bmExistData[0]["bmcount"] > 0) { if($bmExistData[0]["bmaction"] == 1) { e_log(8,"Undelete removed bookmark."); $query = "UPDATE `bookmarks` SET `bmAction` = NULL WHERE `bmID` = '".$bmExistData[0]["bmID"]."' AND userID = ".$ud["userID"].";"; $count = db_query($query); $message = "Bookmark not added at server, it already exists for this user, bookmark undeleted now."; e_log(8,$message); return $count; } else { $message = "Bookmark not added at server, it already exists"; e_log(8,$message); return $message; } } e_log(8,"Get folder for adding bookmark"); $query = "SELECT `bmID` FROM `bookmarks` WHERE `bmID` = '".$bm["folder"]."' AND `userID` = ".$ud['userID']." UNION ALL SELECT 'unfiled_____' WHERE NOT EXISTS (SELECT 1 FROM `bookmarks` WHERE `bmID` = '".$bm["folder"]."');"; $folderID = db_query($query)[0]['bmID']; e_log(8,"Get new index for bookmark"); $query = "SELECT IFNULL(MAX(`bmIndex`),-1) + 1 AS `nindex` FROM `bookmarks` WHERE `userID` = ".$ud['userID']." AND `bmParentID` = '$folderID';"; $nindex = db_query($query)[0]['nindex']; $title = htmlspecialchars($bm['title'],ENT_QUOTES,'UTF-8'); e_log(8,"Add bookmark '".$title."'"); $query = "INSERT INTO `bookmarks` (`bmID`,`bmParentID`,`bmIndex`,`bmTitle`,`bmType`,`bmURL`,`bmAdded`,`userID`) VALUES ('".$bm['id']."', '$folderID', $nindex, '".$title."', '".$bm['type']."', '".$bm['url']."', ".$bm['added'].", ".$ud["userID"].");"; if(db_query($query) != 1 ) { $message = "Adding bookmark failed"; e_log(1,$message); return $message; } else { return 1; } } function getChanges($cl, $ct, $ud, $time) { $uid = $ud["userID"]; e_log(8,"Browser startup sync started, get client data"); $query = "SELECT `lastseen` FROM `clients` WHERE `cid` = '".$cl."' AND `uid` = $uid AND `ctype` = '".$ct."';"; $clientData = db_query($query)[0]; if($clientData) { $lastseen = $clientData["lastseen"]; e_log(8,"Get changed bookmarks for client $cl"); $query = "SELECT a.`bmParentID` as fdID, (SELECT `bmTitle` FROM `bookmarks` WHERE `bmID` = a.`bmParentID`) as fdName, (SELECT `bmIndex` FROM `bookmarks` WHERE `bmID` = a.`bmParentID`) as fdIndex, `bmID`, `bmIndex`, `bmTitle`, `bmType`, `bmURL`, `bmAdded`, `bmModified`, `bmAction` FROM `bookmarks` a WHERE (bmAdded >= $lastseen AND userID = $uid) OR (bmAction = 1 AND bmAdded >= $lastseen AND userID = $uid);"; $bookmarkData = db_query($query); foreach($bookmarkData as $key => $entry) { $bookmarkData[$key]['bmTitle'] = html_entity_decode($entry['bmTitle'],ENT_QUOTES,'UTF-8'); } } else { e_log(2,"Client not found in database, registering now"); updateClient($cl, $ct, $ud, $time, true); return "New client registered for user."; } if (!empty($bookmarkData)) { global $cexpjson; updateClient($cl, $ct, $ud, $time, true); e_log(8,"Try to find bookmarks, which could be completely deleted"); $query = "SELECT `bmID` FROM `bookmarks` WHERE `bmAdded` <= (SELECT MIN(`lastseen`) FROM `clients` WHERE `uid` = $uid AND `lastseen` > 1) AND `bmAction` = 1;"; $removeMarks = db_query($query); if (!empty($removeMarks)) { e_log(8,count($removeMarks)." are deletable from the database"); foreach($removeMarks as $bookmark) { $query = "DELETE FROM `bookmarks` WHERE `bmID` = '".$bookmark["bmID"]."';"; db_query($query); } e_log(8,"Try to compacting database"); db_query("VACUUM"); } else { e_log(8,"No bookmarks found to delete from the database"); } $filename = "changes_".substr($cl,0,8)."_".time().".json"; if($cexpjson && $loglevel = 9) file_put_contents($filename,json_encode($bookmarkData),true); e_log(8,"Found ".count($bookmarkData)." changes. Sending them to the client"); return $bookmarkData; } else { e_log(8,"No bookmarks changed since last sync"); return "No bookmarks added, removed or changed since the client was last seen."; } } function updateClient($cl, $ct, $ud, $time, $sync = false) { $uid = $ud["userID"]; $query = "SELECT * FROM `clients` WHERE `cid` = '".$cl."' AND uid = ".$uid.";"; $clientData = db_query($query); if (!empty($clientData) && $sync) { e_log(8,"Updating lastlogin for client $cl."); $query = "UPDATE `clients` SET `lastseen`= '".$time."' WHERE `cid` = '".$cl."';"; db_query($query); } else if(empty($clientData)) { e_log(8,"New client detected. Register client $cl for user ".$ud["userName"]); $query = "INSERT INTO `clients` (`cid`,`cname`,`ctype`,`uid`,`lastseen`) VALUES ('".$cl."','".$cl."', '".$ct."', ".$uid.", '0')"; db_query($query); } return "Client updated."; } function bmTree($userData) { e_log(8,"Build HTML tree from bookmarks"); $bmTree = makeHTMLTree(getBookmarks($userData)); do { $start = strpos($bmTree,"%ID"); $end = strpos($bmTree,"\n",$start); $len = $end - $start; $bmTree = substr_replace($bmTree, "", $start, $len); } while (strpos($bmTree,"%ID") > 0); $bmTree = preg_replace("/[\r\n]\s*[\r\n]/",' ',$bmTree); return $bmTree; } function getIndex($folder) { e_log(8,"Get new bookmark ID"); $query = "SELECT MAX(`bmIndex`) AS OIndex FROM `bookmarks` WHERE `bmParentID` = '".$folder."'"; $IndexArr = db_query($query); $maxIndex = $IndexArr[0]['OIndex'] + 1; return $maxIndex; } function getSiteTitle($url) { e_log(8,"Get titel from site ".$url); $src = file_get_contents($url); if(strlen($src) > 0) { preg_match("/\
'; break; case 'unfiled_____': $sfolder = ' UNFILED_BOOKMARKS_FOLDER="true"'; $fclose = '
'; break; case 'menu________': $fclose = ''; break; default: $sfolder = ''; $fclose = '
'; } $flvls = ($bm['bmID'] == 'menu________') ? "\r\n
%ID".$bm['bmID']."\r\n\t$fclose"; if(strpos($bookmarks, "%ID".$bm['bmParentID']) > 0) { $nFolder = "\r\n\t".$nFolder."\n%ID".$bm['bmParentID']; $bookmarks = str_replace("%ID".$bm['bmParentID'], $nFolder, $bookmarks); } else { $bookmarks.= $nFolder; } } } return $bookmarks; } function makeHTMLTree($arr) { $bookmarks = ""; foreach($arr as $bm) { if($bm['bmType'] == "bookmark") { $title = html_entity_decode($bm['bmTitle'],ENT_QUOTES,'UTF-8'); $bookmark = "\n