mirror of
https://github.com/friendica/friendica
synced 2025-04-22 13:50:12 +00:00
Move api/profile/show to src/Module
- Update API documentation - Update BaseApi::format to include headers by format
This commit is contained in:
parent
608e634858
commit
dd24b3bac0
6 changed files with 175 additions and 234 deletions
114
src/Module/Api/Friendica/Profile/Show.php
Normal file
114
src/Module/Api/Friendica/Profile/Show.php
Normal file
|
@ -0,0 +1,114 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Module\Api\Friendica\Profile;
|
||||
|
||||
use Friendica\Collection\ProfileFields;
|
||||
use Friendica\Content\Text\BBCode;
|
||||
use Friendica\DI;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Model\Profile;
|
||||
use Friendica\Module\BaseApi;
|
||||
use Friendica\Network\HTTPException;
|
||||
use Friendica\Repository\PermissionSet;
|
||||
|
||||
/**
|
||||
* API endpoint: /api/friendica/profile/show
|
||||
*/
|
||||
class Show extends BaseApi
|
||||
{
|
||||
public static function rawContent(array $parameters = [])
|
||||
{
|
||||
if (self::login() === false) {
|
||||
throw new HTTPException\ForbiddenException();
|
||||
}
|
||||
|
||||
// retrieve general information about profiles for user
|
||||
$directory = DI::config()->get('system', 'directory');
|
||||
|
||||
$profile = Profile::getByUID(self::$current_user_id);
|
||||
|
||||
$profileFields = DI::profileField()->select(['uid' => self::$current_user_id, 'psid' => PermissionSet::PUBLIC]);
|
||||
|
||||
$profile = self::formatProfile($profile, $profileFields);
|
||||
|
||||
$profiles = [];
|
||||
if (self::$format == 'xml') {
|
||||
$profiles['0:profile'] = $profile;
|
||||
} else {
|
||||
$profiles[] = $profile;
|
||||
}
|
||||
|
||||
// return settings, authenticated user and profiles data
|
||||
$self = Contact::selectFirst(['nurl'], ['uid' => self::$current_user_id, 'self' => true]);
|
||||
|
||||
$result = [
|
||||
'multi_profiles' => false,
|
||||
'global_dir' => $directory,
|
||||
'friendica_owner' => self::getUser($self['nurl']),
|
||||
'profiles' => $profiles
|
||||
];
|
||||
|
||||
echo self::format('friendica_profiles', ['$result' => $result]);
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $profile_row array containing data from db table 'profile'
|
||||
* @param ProfileFields $profileFields
|
||||
* @return array
|
||||
* @throws HTTPException\InternalServerErrorException
|
||||
*/
|
||||
private static function formatProfile($profile_row, ProfileFields $profileFields)
|
||||
{
|
||||
$custom_fields = [];
|
||||
foreach ($profileFields as $profileField) {
|
||||
$custom_fields[] = [
|
||||
'label' => $profileField->label,
|
||||
'value' => BBCode::convert($profileField->value, false, 2),
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'profile_id' => $profile_row['id'],
|
||||
'profile_name' => null,
|
||||
'is_default' => null,
|
||||
'hide_friends' => $profile_row['hide-friends'] ? true : false,
|
||||
'profile_photo' => $profile_row['photo'],
|
||||
'profile_thumb' => $profile_row['thumb'],
|
||||
'publish' => $profile_row['publish'] ? true : false,
|
||||
'net_publish' => $profile_row['net-publish'] ? true : false,
|
||||
'description' => $profile_row['pdesc'],
|
||||
'date_of_birth' => $profile_row['dob'],
|
||||
'address' => $profile_row['address'],
|
||||
'city' => $profile_row['locality'],
|
||||
'region' => $profile_row['region'],
|
||||
'postal_code' => $profile_row['postal-code'],
|
||||
'country' => $profile_row['country-name'],
|
||||
'hometown' => null,
|
||||
'gender' => null,
|
||||
'marital' => null,
|
||||
'marital_with' => null,
|
||||
'marital_since' => null,
|
||||
'sexual' => null,
|
||||
'politic' => null,
|
||||
'religion' => null,
|
||||
'public_keywords' => $profile_row['pub_keywords'],
|
||||
'private_keywords' => $profile_row['prv_keywords'],
|
||||
'likes' => null,
|
||||
'dislikes' => null,
|
||||
'about' => null,
|
||||
'music' => null,
|
||||
'book' => null,
|
||||
'tv' => null,
|
||||
'film' => null,
|
||||
'interest' => null,
|
||||
'romance' => null,
|
||||
'work' => null,
|
||||
'education' => null,
|
||||
'social_networks' => null,
|
||||
'homepage' => $profile_row['homepage'],
|
||||
'users' => [],
|
||||
'custom_fields' => $custom_fields,
|
||||
];
|
||||
}
|
||||
}
|
|
@ -87,20 +87,53 @@ class BaseApi extends BaseModule
|
|||
return api_get_user(DI::app(), $contact_id);
|
||||
}
|
||||
|
||||
protected static function format($root_element, $data)
|
||||
/**
|
||||
* Formats the data according to the data type
|
||||
*
|
||||
* @param string $root_element
|
||||
* @param array $data An array with a single element containing the returned result
|
||||
* @return false|string
|
||||
*/
|
||||
protected static function format(string $root_element, array $data)
|
||||
{
|
||||
$return = api_format_data($root_element, self::$format, $data);
|
||||
|
||||
switch (self::$format) {
|
||||
case "atom":
|
||||
case "rss":
|
||||
case "xml":
|
||||
$ret = api_create_xml($data, $root_element);
|
||||
header("Content-Type: text/xml");
|
||||
break;
|
||||
case "json":
|
||||
default:
|
||||
$ret = $data;
|
||||
header("Content-Type: application/json");
|
||||
if (!empty($return)) {
|
||||
$json = json_encode(end($return));
|
||||
if (!empty($_GET['callback'])) {
|
||||
$json = $_GET['callback'] . "(" . $json . ")";
|
||||
}
|
||||
$return = $json;
|
||||
}
|
||||
break;
|
||||
case "rss":
|
||||
header("Content-Type: application/rss+xml");
|
||||
$return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
|
||||
break;
|
||||
case "atom":
|
||||
header("Content-Type: application/atom+xml");
|
||||
$return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
|
||||
break;
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
return $ret;
|
||||
/**
|
||||
* Creates the XML from a JSON style array
|
||||
*
|
||||
* @param $data
|
||||
* @param $root_element
|
||||
* @return string
|
||||
*/
|
||||
protected static function createXml($data, $root_element)
|
||||
{
|
||||
return api_create_xml($data, $root_element);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue