]> git.mxchange.org Git - friendica.git/blob - src/Module/Directory.php
Fixed Forbidden message.
[friendica.git] / src / Module / Directory.php
1 <?php
2
3 namespace Friendica\Module;
4
5 use Friendica\BaseModule;
6 use Friendica\Content\Nav;
7 use Friendica\Content\Pager;
8 use Friendica\Content\Widget;
9 use Friendica\Core\Hook;
10 use Friendica\Core\L10n;
11 use Friendica\Core\Renderer;
12 use Friendica\Model\Contact;
13 use Friendica\Model\Profile;
14 use Friendica\Network\HTTPException;
15 use Friendica\Util\Proxy as ProxyUtils;
16 use Friendica\Util\Strings;
17
18 /**
19  * Shows the local directory of this node
20  */
21 class Directory extends BaseModule
22 {
23         public static function init()
24         {
25                 $app = self::getApp();
26
27                 if (local_user()) {
28                         $app->page['aside'] .= Widget::findPeople();
29                         $app->page['aside'] .= Widget::follow();
30                 } else {
31                         unset($_SESSION['theme']);
32                         unset($_SESSION['mobile-theme']);
33                 }
34         }
35
36         public static function content()
37         {
38                 $app = self::getApp();
39                 $config = $app->getConfig();
40
41                 if (($config->get('system', 'block_public') && !local_user() && !remote_user()) ||
42                         ($config->get('system', 'block_local_dir') && !local_user() && !remote_user())) {
43                         throw new HTTPException\ForbiddenException(L10n::t('Public access denied.'));
44                 }
45
46                 $output = '';
47                 $entries = [];
48
49                 Nav::setSelected('directory');
50
51                 $search = (!empty($_REQUEST['search']) ?
52                         Strings::escapeTags(trim(rawurldecode($_REQUEST['search']))) :
53                         '');
54
55                 $gDirPath = '';
56                 $dirURL = $config->get('system', 'directory');
57                 if (strlen($dirURL)) {
58                         $gDirPath = Profile::zrl($dirURL, true);
59                 }
60
61                 $pager = new Pager($app->query_string, 60);
62
63                 $profiles = Profile::searchProfiles($pager->getStart(), $pager->getItemsPerPage(), $search);
64
65                 if ($profiles['total'] === 0) {
66                         info(L10n::t('No entries (some entries may be hidden).') . EOL);
67                 } else {
68                         if (in_array('small', $app->argv)) {
69                                 $photo = 'thumb';
70                         } else {
71                                 $photo = 'photo';
72                         }
73
74                         foreach ($profiles['entries'] as $entry) {
75                                 $entries[] = self::formatEntry($entry, $photo);
76                         }
77                 }
78
79                 $tpl = Renderer::getMarkupTemplate('directory_header.tpl');
80
81                 $output .= Renderer::replaceMacros($tpl, [
82                         '$search'     => $search,
83                         '$globaldir'  => L10n::t('Global Directory'),
84                         '$gDirPath'   => $gDirPath,
85                         '$desc'       => L10n::t('Find on this site'),
86                         '$contacts'   => $profiles['entries'],
87                         '$finding'    => L10n::t('Results for:'),
88                         '$findterm'   => (strlen($search) ? $search : ""),
89                         '$title'      => L10n::t('Site Directory'),
90                         '$search_mod' => 'directory',
91                         '$submit'     => L10n::t('Find'),
92                         '$paginate'   => $pager->renderFull($profiles['total']),
93                 ]);
94
95                 return $output;
96         }
97
98         /**
99          * Format contact/profile/user data from the database into an usable
100          * array for displaying directory entries.
101          *
102          * @param array  $contact    The directory entry from the database.
103          * @param string $photo_size Avatar size (thumb, photo or micro).
104          *
105          * @return array
106          *
107          * @throws \Exception
108          */
109         public static function formatEntry(array $contact, $photo_size = 'photo')
110         {
111                 $itemurl = (($contact['addr'] != "") ? $contact['addr'] : $contact['profile_url']);
112
113                 $profile_link = $contact['profile_url'];
114
115                 $pdesc = (($contact['pdesc']) ? $contact['pdesc'] . '<br />' : '');
116
117                 $details = '';
118                 if (strlen($contact['locality'])) {
119                         $details .= $contact['locality'];
120                 }
121                 if (strlen($contact['region'])) {
122                         if (strlen($contact['locality'])) {
123                                 $details .= ', ';
124                         }
125                         $details .= $contact['region'];
126                 }
127                 if (strlen($contact['country-name'])) {
128                         if (strlen($details)) {
129                                 $details .= ', ';
130                         }
131                         $details .= $contact['country-name'];
132                 }
133
134                 $profile = $contact;
135
136                 if (!empty($profile['address'])
137                         || !empty($profile['locality'])
138                         || !empty($profile['region'])
139                         || !empty($profile['postal-code'])
140                         || !empty($profile['country-name'])
141                 ) {
142                         $location = L10n::t('Location:');
143                 } else {
144                         $location = '';
145                 }
146
147                 $gender =   (!empty($profile['gender'])   ? L10n::t('Gender:')   : false);
148                 $marital =  (!empty($profile['marital'])  ? L10n::t('Status:')   : false);
149                 $homepage = (!empty($profile['homepage']) ? L10n::t('Homepage:') : false);
150                 $about =    (!empty($profile['about'])    ? L10n::t('About:')    : false);
151
152                 $location_e = $location;
153
154                 $photo_menu = [
155                         'profile' => [L10n::t("View Profile"), Contact::magicLink($profile_link)]
156                 ];
157
158                 $entry = [
159                         'id'           => $contact['id'],
160                         'url'          => Contact::magicLInk($profile_link),
161                         'itemurl'      => $itemurl,
162                         'thumb'        => ProxyUtils::proxifyUrl($contact[$photo_size], false, ProxyUtils::SIZE_THUMB),
163                         'img_hover'    => $contact['name'],
164                         'name'         => $contact['name'],
165                         'details'      => $details,
166                         'account_type' => Contact::getAccountType($contact),
167                         'profile'      => $profile,
168                         'location'     => $location_e,
169                         'tags'         => $contact['pub_keywords'],
170                         'gender'       => $gender,
171                         'pdesc'        => $pdesc,
172                         'marital'      => $marital,
173                         'homepage'     => $homepage,
174                         'about'        => $about,
175                         'photo_menu'   => $photo_menu,
176
177                 ];
178
179                 $hook = ['contact' => $contact, 'entry' => $entry];
180
181                 Hook::callAll('directory_item', $hook);
182
183                 unset($profile);
184                 unset($location);
185
186                 return $hook['entry'];
187         }
188 }