3 * @copyright Copyright (C) 2020, Friendica
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\Session;
30 use Friendica\Core\Renderer;
32 use Friendica\Model\Contact;
33 use Friendica\Model\Profile;
34 use Friendica\Network\HTTPException;
35 use Friendica\Util\Proxy as ProxyUtils;
36 use Friendica\Util\Strings;
39 * Shows the local directory of this node
41 class Directory extends BaseModule
43 public static function content(array $parameters = [])
46 $config = DI::config();
48 if (($config->get('system', 'block_public') && !Session::isAuthenticated()) ||
49 ($config->get('system', 'block_local_dir') && !Session::isAuthenticated())) {
50 throw new HTTPException\ForbiddenException(DI::l10n()->t('Public access denied.'));
54 DI::page()['aside'] .= Widget::findPeople();
55 DI::page()['aside'] .= Widget::follow();
61 Nav::setSelected('directory');
63 $search = (!empty($_REQUEST['search']) ?
64 Strings::escapeTags(trim(rawurldecode($_REQUEST['search']))) :
68 $dirURL = $config->get('system', 'directory');
69 if (strlen($dirURL)) {
70 $gDirPath = Profile::zrl($dirURL, true);
73 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 60);
75 $profiles = Profile::searchProfiles($pager->getStart(), $pager->getItemsPerPage(), $search);
77 if ($profiles['total'] === 0) {
78 info(DI::l10n()->t('No entries (some entries may be hidden).') . EOL);
80 if (in_array('small', $app->argv)) {
86 foreach ($profiles['entries'] as $entry) {
87 $entries[] = self::formatEntry($entry, $photo);
91 $tpl = Renderer::getMarkupTemplate('directory_header.tpl');
93 $output .= Renderer::replaceMacros($tpl, [
95 '$globaldir' => DI::l10n()->t('Global Directory'),
96 '$gDirPath' => $gDirPath,
97 '$desc' => DI::l10n()->t('Find on this site'),
98 '$contacts' => $entries,
99 '$finding' => DI::l10n()->t('Results for:'),
100 '$findterm' => (strlen($search) ? $search : ""),
101 '$title' => DI::l10n()->t('Site Directory'),
102 '$search_mod' => 'directory',
103 '$submit' => DI::l10n()->t('Find'),
104 '$paginate' => $pager->renderFull($profiles['total']),
111 * Format contact/profile/user data from the database into an usable
112 * array for displaying directory entries.
114 * @param array $contact The directory entry from the database.
115 * @param string $photo_size Avatar size (thumb, photo or micro).
121 public static function formatEntry(array $contact, $photo_size = 'photo')
123 $itemurl = (($contact['addr'] != "") ? $contact['addr'] : $contact['profile_url']);
125 $profile_link = $contact['profile_url'];
127 $about = (($contact['about']) ? $contact['about'] . '<br />' : '');
130 if (strlen($contact['locality'])) {
131 $details .= $contact['locality'];
133 if (strlen($contact['region'])) {
134 if (strlen($contact['locality'])) {
137 $details .= $contact['region'];
139 if (strlen($contact['country-name'])) {
140 if (strlen($details)) {
143 $details .= $contact['country-name'];
148 if (!empty($profile['address'])
149 || !empty($profile['locality'])
150 || !empty($profile['region'])
151 || !empty($profile['postal-code'])
152 || !empty($profile['country-name'])
154 $location = DI::l10n()->t('Location:');
159 $homepage = (!empty($profile['homepage']) ? DI::l10n()->t('Homepage:') : false);
161 $location_e = $location;
164 'profile' => [DI::l10n()->t("View Profile"), Contact::magicLink($profile_link)]
168 'id' => $contact['id'],
169 'url' => Contact::magicLink($profile_link),
170 'itemurl' => $itemurl,
171 'thumb' => ProxyUtils::proxifyUrl($contact[$photo_size], false, ProxyUtils::SIZE_THUMB),
172 'img_hover' => $contact['name'],
173 'name' => $contact['name'],
174 'details' => $details,
175 'account_type' => Contact::getAccountType($contact),
176 'profile' => $profile,
177 'location' => $location_e,
178 'tags' => $contact['pub_keywords'],
180 'homepage' => $homepage,
181 'photo_menu' => $photo_menu,
185 $hook = ['contact' => $contact, 'entry' => $entry];
187 Hook::callAll('directory_item', $hook);
192 return $hook['entry'];