]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Friendica/Profile/Show.php
Use central function to fetch the global directory
[friendica.git] / src / Module / Api / Friendica / Profile / Show.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Api\Friendica\Profile;
23
24 use Friendica\Profile\ProfileField\Collection\ProfileFields;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Core\Search;
27 use Friendica\DI;
28 use Friendica\Model\Profile;
29 use Friendica\Module\BaseApi;
30 use Friendica\Network\HTTPException;
31
32 /**
33  * API endpoint: /api/friendica/profile/show
34  */
35 class Show extends BaseApi
36 {
37         protected function rawContent(array $request = [])
38         {
39                 self::checkAllowedScope(self::SCOPE_READ);
40                 $uid = self::getCurrentUserID();
41
42                 // retrieve general information about profiles for user
43                 $directory = Search::getGlobalDirectory();
44
45                 $profile = Profile::getByUID($uid);
46                 
47                 $profileFields = DI::profileField()->selectPublicFieldsByUserId($uid);
48
49                 $profile = self::formatProfile($profile, $profileFields);
50
51                 $profiles = [];
52                 if (($this->parameters['extension'] ?? '') == 'xml') {
53                         $profiles['0:profile'] = $profile;
54                 } else {
55                         $profiles[] = $profile;
56                 }
57
58                 $result = [
59                         'multi_profiles' => false,
60                         'global_dir' => $directory,
61                         'friendica_owner' => DI::twitterUser()->createFromUserId($uid),
62                         'profiles' => $profiles
63                 ];
64
65                 $this->response->exit('friendica_profiles', ['$result' => $result], $this->parameters['extension'] ?? null);
66         }
67
68         /**
69          * @param array         $profile_row array containing data from db table 'profile'
70          * @param ProfileFields $profileFields
71          * @return array
72          * @throws HTTPException\InternalServerErrorException
73          */
74         private static function formatProfile($profile_row, ProfileFields $profileFields)
75         {
76                 $custom_fields = [];
77                 foreach ($profileFields as $profileField) {
78                         $custom_fields[] = [
79                                 'label' => $profileField->label,
80                                 'value' => BBCode::convert($profileField->value, false, BBCode::API),
81                         ];
82                 }
83
84                 return [
85                         'profile_id'       => $profile_row['id'],
86                         'profile_name'     => null,
87                         'is_default'       => null,
88                         'hide_friends'     => $profile_row['hide-friends'] ? true : false,
89                         'profile_photo'    => $profile_row['photo'],
90                         'profile_thumb'    => $profile_row['thumb'],
91                         'publish'          => $profile_row['publish'] ? true : false,
92                         'net_publish'      => $profile_row['net-publish'] ? true : false,
93                         'description'      => $profile_row['about'],
94                         'date_of_birth'    => $profile_row['dob'],
95                         'address'          => $profile_row['address'],
96                         'city'             => $profile_row['locality'],
97                         'region'           => $profile_row['region'],
98                         'postal_code'      => $profile_row['postal-code'],
99                         'country'          => $profile_row['country-name'],
100                         'hometown'         => null,
101                         'gender'           => null,
102                         'marital'          => null,
103                         'marital_with'     => null,
104                         'marital_since'    => null,
105                         'sexual'           => null,
106                         'politic'          => null,
107                         'religion'         => null,
108                         'public_keywords'  => $profile_row['pub_keywords'],
109                         'private_keywords' => $profile_row['prv_keywords'],
110                         'likes'            => null,
111                         'dislikes'         => null,
112                         'about'            => null,
113                         'music'            => null,
114                         'book'             => null,
115                         'tv'               => null,
116                         'film'             => null,
117                         'interest'         => null,
118                         'romance'          => null,
119                         'work'             => null,
120                         'education'        => null,
121                         'social_networks'  => null,
122                         'homepage'         => $profile_row['homepage'],
123                         'users'            => [],
124                         'custom_fields'    => $custom_fields,
125                 ];
126         }
127 }