|
|
@ -1,92 +1,110 @@ |
|
|
|
<?php |
|
|
|
<?php |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Default response |
|
|
|
$response = [ |
|
|
|
$response = [ |
|
|
|
'success' => false, |
|
|
|
'success' => false, |
|
|
|
'message' => _('Internal server error'), |
|
|
|
'message' => _('Internal server error'), |
|
|
|
'profile' => [] |
|
|
|
'profile' => [] |
|
|
|
]; |
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Access allowed for authorized users only |
|
|
|
if (isset($_SESSION['userName'])) { |
|
|
|
if (isset($_SESSION['userName'])) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Prepare user request, authorized user by default |
|
|
|
$userName = isset($_GET['userName']) ? Filter::userName($_GET['userName']) : $_SESSION['userName']; |
|
|
|
$userName = isset($_GET['userName']) ? Filter::userName($_GET['userName']) : $_SESSION['userName']; |
|
|
|
|
|
|
|
|
|
|
|
if ($profile = $_memcache->get('api.user.profile.' . $userName)) { |
|
|
|
// Check user exists in the database |
|
|
|
|
|
|
|
if ($userId = $_modelUser->getUserId($userName)) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
|
|
* Step 1: try to obtain profile from cache |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* */ |
|
|
|
|
|
|
|
if ($mcProfile = $_memcache->get('api.user.profile.' . $userName)) { |
|
|
|
|
|
|
|
|
|
|
|
$response = [ |
|
|
|
$response = [ |
|
|
|
'success' => true, |
|
|
|
'success' => true, |
|
|
|
'message' => _('Profile successfully received from Cache'), |
|
|
|
'message' => _('Profile successfully received from Cache'), |
|
|
|
'profile' => $profile |
|
|
|
'profile' => $mcProfile |
|
|
|
]; |
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
} else if ($userProfileVersions = $_twister->getDHT($userName, 'profile', 's')) { |
|
|
|
/* |
|
|
|
|
|
|
|
* Step 2: try to obtain profile from DHT |
|
|
|
// Check user exists |
|
|
|
* |
|
|
|
if ($userId = $_modelUser->getUserId($userName)) { |
|
|
|
* */ |
|
|
|
|
|
|
|
} else if ($dhtProfileRevisions = $_twister->getDHTProfileRevisions($userName)) { |
|
|
|
|
|
|
|
|
|
|
|
// Add DHT version if not exists |
|
|
|
// Add DHT version if not exists |
|
|
|
foreach ($userProfileVersions as $userProfileVersion) { |
|
|
|
foreach ((array) $dhtProfileRevisions as $dhtProfileRevision) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Save revision into the database if not exists |
|
|
|
if (!$_modelProfile->versionExists($userId, |
|
|
|
if (!$_modelProfile->versionExists($userId, |
|
|
|
Filter::int($userProfileVersion['p']['height']), |
|
|
|
$dhtProfileRevision['height'], |
|
|
|
Filter::int($userProfileVersion['p']['seq']))) { |
|
|
|
$dhtProfileRevision['seq'])) { |
|
|
|
|
|
|
|
|
|
|
|
$profile = $userProfileVersion['p']['v']; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$_modelProfile->add($userId, |
|
|
|
$_modelProfile->add($userId, |
|
|
|
Filter::int($userProfileVersion['p']['height']), |
|
|
|
$dhtProfileRevision['height'], |
|
|
|
Filter::int($userProfileVersion['p']['seq']), |
|
|
|
$dhtProfileRevision['seq'], |
|
|
|
Filter::int($userProfileVersion['p']['time']), |
|
|
|
$dhtProfileRevision['time'], |
|
|
|
|
|
|
|
|
|
|
|
isset($profile['fullname']) ? Filter::string($profile['fullname']) : '', |
|
|
|
$dhtProfileRevision['fullName'], |
|
|
|
isset($profile['bio']) ? Filter::string($profile['bio']) : '', |
|
|
|
$dhtProfileRevision['bio'], |
|
|
|
isset($profile['location']) ? Filter::string($profile['location']) : '', |
|
|
|
$dhtProfileRevision['location'], |
|
|
|
isset($profile['url']) ? Filter::string($profile['url']) : '', |
|
|
|
$dhtProfileRevision['url'], |
|
|
|
isset($profile['bitmessage']) ? Filter::string($profile['bitmessage']) : '', |
|
|
|
$dhtProfileRevision['bitMessage'], |
|
|
|
isset($profile['tox']) ? Filter::string($profile['tox']) : ''); |
|
|
|
$dhtProfileRevision['tox']); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Get latest version available |
|
|
|
/* |
|
|
|
if ($profileInfo = $_modelProfile->get($userId)) { |
|
|
|
* Step 3: Select latest version available from DB revisions |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* */ |
|
|
|
|
|
|
|
if ($dbProfileRevision = $_modelProfile->get($userId)) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Format output |
|
|
|
$profile = [ |
|
|
|
$profile = [ |
|
|
|
'userName' => $userName, |
|
|
|
'userName' => $userName, |
|
|
|
'fullName' => Filter::string($profileInfo['fullName']), |
|
|
|
'fullName' => Format::text($dbProfileRevision['fullName']), |
|
|
|
'location' => Filter::string($profileInfo['location']), |
|
|
|
'location' => Format::text($dbProfileRevision['location']), |
|
|
|
'url' => Filter::string($profileInfo['url']), |
|
|
|
'url' => Format::text($dbProfileRevision['url']), |
|
|
|
'bitMessage' => Filter::string($profileInfo['bitMessage']), |
|
|
|
'bitMessage' => Format::text($dbProfileRevision['bitMessage']), |
|
|
|
'tox' => Filter::string($profileInfo['tox']), |
|
|
|
'tox' => Format::text($dbProfileRevision['tox']), |
|
|
|
'bio' => nl2br(Filter::string($profileInfo['bio'])), |
|
|
|
'bio' => Format::text($dbProfileRevision['bio']), |
|
|
|
]; |
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Save request into the cache pool |
|
|
|
|
|
|
|
$_memcache->set('api.user.profile.' . $userName, $profile, MEMCACHE_COMPRESS, MEMCACHE_DHT_PROFILE_TIMEOUT); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Response |
|
|
|
$response = [ |
|
|
|
$response = [ |
|
|
|
'success' => true, |
|
|
|
'success' => true, |
|
|
|
'message' => _('Profile successfully received from DHT'), |
|
|
|
'message' => _('Profile successfully received from DHT/DB'), |
|
|
|
'profile' => $profile |
|
|
|
'profile' => $profile |
|
|
|
]; |
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
$_memcache->set('api.user.profile.' . $userName, $profile, MEMCACHE_COMPRESS, MEMCACHE_DHT_PROFILE_TIMEOUT); |
|
|
|
// Cache, DHT, DB not contain any the details about user requested |
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
$response = [ |
|
|
|
$response = [ |
|
|
|
'success' => false, |
|
|
|
'success' => false, |
|
|
|
'message' => _('Profile data not available'), |
|
|
|
'message' => _('Could not receive any profile details'), |
|
|
|
'profile' => [] |
|
|
|
'profile' => [] |
|
|
|
]; |
|
|
|
]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// User not found in the local database registry |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
$response = [ |
|
|
|
$response = [ |
|
|
|
'success' => false, |
|
|
|
'success' => false, |
|
|
|
'message' => _('Could not receive profile details'), |
|
|
|
'message' => _('Requested user not found'), |
|
|
|
'profile' => [] |
|
|
|
'profile' => [] |
|
|
|
]; |
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Session expired response |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
$response = [ |
|
|
|
$response = [ |
|
|
|