ghost
3 years ago
6 changed files with 287 additions and 9 deletions
Binary file not shown.
@ -0,0 +1,89 @@
@@ -0,0 +1,89 @@
|
||||
<?php |
||||
|
||||
$response = [ |
||||
'success' => false, |
||||
'message' => _('Internal server error'), |
||||
'profile' => [] |
||||
]; |
||||
|
||||
if (isset($_SESSION['userName'])) { |
||||
|
||||
$userName = isset($_POST['userName']) ? Filter::userName($_POST['userName']) : $_SESSION['userName']; |
||||
|
||||
if ($userProfileVersions = $_twister->getDHT($userName, 'profile', 's')) { |
||||
|
||||
// Check user exists |
||||
if ($userId = $_modelUser->getUserId($userName)) { |
||||
|
||||
// Add DHT version if not exists |
||||
foreach ($userProfileVersions as $userProfileVersion) { |
||||
|
||||
if (!$_modelProfile->versionExists($userId, |
||||
$userProfileVersion['p']['height'], |
||||
$userProfileVersion['p']['seq'])) { |
||||
|
||||
$profile = $userProfileVersion['p']['v']; |
||||
|
||||
$_modelProfile->add($userId, |
||||
$userProfileVersion['p']['height'], |
||||
$userProfileVersion['p']['seq'], |
||||
$userProfileVersion['p']['time'], |
||||
|
||||
isset($profile['fullname']) ? $profile['fullname'] : '', |
||||
isset($profile['bio']) ? $profile['bio'] : '', |
||||
isset($profile['location']) ? $profile['location'] : '', |
||||
isset($profile['url']) ? $profile['url'] : '', |
||||
isset($profile['bitmessage']) ? $profile['bitmessage'] : '', |
||||
isset($profile['tox']) ? $profile['tox'] : ''); |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
// Get latest version available |
||||
if ($profileInfo = $_modelProfile->get($userId)) { |
||||
|
||||
$response = [ |
||||
'success' => true, |
||||
'message' => _('Profile successfully received'), |
||||
'profile' => [ |
||||
'userName' => $userName, |
||||
'fullName' => $profileInfo['fullName'], |
||||
'location' => $profileInfo['location'], |
||||
'url' => $profileInfo['url'], |
||||
'bitMessage' => $profileInfo['bitMessage'], |
||||
'tox' => $profileInfo['tox'], |
||||
'bio' => nl2br($profileInfo['bio']), |
||||
] |
||||
]; |
||||
|
||||
} else { |
||||
|
||||
$response = [ |
||||
'success' => false, |
||||
'message' => _('Profile data not available'), |
||||
'profile' => [] |
||||
]; |
||||
} |
||||
|
||||
} else { |
||||
|
||||
$response = [ |
||||
'success' => false, |
||||
'message' => _('Could not receive profile details'), |
||||
'profile' => [] |
||||
]; |
||||
|
||||
} |
||||
|
||||
} else { |
||||
|
||||
$response = [ |
||||
'success' => false, |
||||
'message' => _('Session expired. Please, reload the page.'), |
||||
'profile' => [] |
||||
]; |
||||
} |
||||
|
||||
header('Content-Type: application/json; charset=utf-8'); |
||||
echo json_encode($response); |
@ -0,0 +1,87 @@
@@ -0,0 +1,87 @@
|
||||
<?php |
||||
|
||||
class ModelProfile extends Model { |
||||
|
||||
public function get(int $userId) { |
||||
|
||||
try { |
||||
|
||||
$query = $this->_db->prepare("SELECT * FROM `profile` |
||||
WHERE `userId` = ? |
||||
|
||||
ORDER BY `seq` DESC |
||||
LIMIT 1"); |
||||
|
||||
$query->execute([$userId]); |
||||
|
||||
return $query->fetch(); |
||||
|
||||
} catch (PDOException $e) { |
||||
|
||||
trigger_error($e->getMessage()); |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
public function versionExists(int $userId, int $blockId, int $seq) { |
||||
|
||||
try { |
||||
|
||||
$query = $this->_db->prepare("SELECT COUNT(*) AS `total` FROM `profile` |
||||
WHERE `userId` = ? AND `blockId` = ? AND `seq` = ?"); |
||||
|
||||
$query->execute([$userId, $blockId, $seq]); |
||||
|
||||
return $query->fetch()['total']; |
||||
|
||||
} catch (PDOException $e) { |
||||
|
||||
trigger_error($e->getMessage()); |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
public function add(int $userId, |
||||
int $blockId, |
||||
int $seq, |
||||
int $time, |
||||
string $fullName, |
||||
string $bio, |
||||
string $location, |
||||
string $url, |
||||
string $bitMessage, |
||||
string $tox) { |
||||
|
||||
try { |
||||
|
||||
$query = $this->_db->prepare("INSERT INTO `profile` SET `userId` = ?, |
||||
`blockId` = ?, |
||||
`seq` = ?, |
||||
`time` = ?, |
||||
`fullName` = ?, |
||||
`bio` = ?, |
||||
`location` = ?, |
||||
`url` = ?, |
||||
`bitMessage` = ?, |
||||
`tox` = ?"); |
||||
|
||||
$query->execute([$userId, |
||||
$blockId, |
||||
$seq, |
||||
$time, |
||||
$fullName, |
||||
$bio, |
||||
$location, |
||||
$url, |
||||
$bitMessage, |
||||
$tox]); |
||||
|
||||
return $this->_db->lastInsertId(); |
||||
|
||||
} catch (PDOException $e) { |
||||
|
||||
trigger_error($e->getMessage()); |
||||
return false; |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue