3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module;
24 use Friendica\BaseModule;
25 use Friendica\Content\Nav;
26 use Friendica\Content\Pager;
27 use Friendica\Content\Widget;
28 use Friendica\Core\Hook;
29 use Friendica\Core\Renderer;
30 use Friendica\Core\Search;
33 use Friendica\Model\Profile;
34 use Friendica\Network\HTTPException;
37 * Shows the local directory of this node
39 class Directory extends BaseModule
41 protected function content(array $request = []): string
44 $config = DI::config();
46 if (($config->get('system', 'block_public') && !DI::userSession()->isAuthenticated()) ||
47 ($config->get('system', 'block_local_dir') && !DI::userSession()->isAuthenticated())) {
48 throw new HTTPException\ForbiddenException(DI::l10n()->t('Public access denied.'));
51 if (DI::userSession()->getLocalUserId()) {
52 DI::page()['aside'] .= Widget::findPeople();
53 DI::page()['aside'] .= Widget::follow();
59 Nav::setSelected('directory');
61 $search = trim(rawurldecode($_REQUEST['search'] ?? ''));
64 $dirURL = Search::getGlobalDirectory();
65 if (strlen($dirURL)) {
66 $gDirPath = Profile::zrl($dirURL, true);
69 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 60);
71 $profiles = Profile::searchProfiles($pager->getStart(), $pager->getItemsPerPage(), $search);
73 if ($profiles['total'] === 0) {
74 DI::sysmsg()->addNotice(DI::l10n()->t('No entries (some entries may be hidden).'));
76 foreach ($profiles['entries'] as $entry) {
77 $contact = Model\Contact::getByURLForUser($entry['url'], DI::userSession()->getLocalUserId());
78 if (!empty($contact)) {
79 $entries[] = Contact::getContactTemplateVars($contact);
84 $tpl = Renderer::getMarkupTemplate('directory_header.tpl');
86 $output .= Renderer::replaceMacros($tpl, [
88 '$globaldir' => DI::l10n()->t('Global Directory'),
89 '$gDirPath' => $gDirPath,
90 '$desc' => DI::l10n()->t('Find on this site'),
91 '$contacts' => $entries,
92 '$finding' => DI::l10n()->t('Results for:'),
93 '$findterm' => (strlen($search) ? $search : ""),
94 '$title' => DI::l10n()->t('Site Directory'),
95 '$search_mod' => 'directory',
96 '$submit' => DI::l10n()->t('Find'),
97 '$paginate' => $pager->renderFull($profiles['total']),
104 * Format contact/profile/user data from the database into an usable
105 * array for displaying directory entries.
107 * @param array $contact The directory entry from the database.
108 * @param string $photo_size Avatar size (thumb, photo or micro).
114 public static function formatEntry(array $contact, string $photo_size = 'photo'): array
116 $itemurl = (($contact['addr'] != "") ? $contact['addr'] : $contact['url']);
118 $profile_link = $contact['url'];
120 $about = (($contact['about']) ? $contact['about'] . '<br />' : '');
123 if (strlen($contact['locality'])) {
124 $details .= $contact['locality'];
126 if (strlen($contact['region'])) {
127 if (strlen($contact['locality'])) {
130 $details .= $contact['region'];
132 if (strlen($contact['country-name'])) {
133 if (strlen($details)) {
136 $details .= $contact['country-name'];
141 if (!empty($profile['address'])
142 || !empty($profile['locality'])
143 || !empty($profile['region'])
144 || !empty($profile['postal-code'])
145 || !empty($profile['country-name'])
147 $location = DI::l10n()->t('Location:');
152 $homepage = (!empty($profile['homepage']) ? DI::l10n()->t('Homepage:') : false);
154 $location_e = $location;
157 'profile' => [DI::l10n()->t("View Profile"), Model\Contact::magicLink($profile_link)]
161 'id' => $contact['id'],
162 'url' => Model\Contact::magicLink($profile_link),
163 'itemurl' => $itemurl,
164 'thumb' => Model\Contact::getThumb($contact),
165 'img_hover' => $contact['name'],
166 'name' => $contact['name'],
167 'details' => $details,
168 'account_type' => Model\Contact::getAccountType($contact['contact-type'] ?? 0),
169 'profile' => $profile,
170 'location' => $location_e,
171 'tags' => $contact['pub_keywords'],
173 'homepage' => $homepage,
174 'photo_menu' => $photo_menu,
178 $hook = ['contact' => $contact, 'entry' => $entry];
180 Hook::callAll('directory_item', $hook);
185 return $hook['entry'];