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\Strings;
38 * Shows the local directory of this node
40 class Directory extends BaseModule
42 public static function content(array $parameters = [])
45 $config = DI::config();
47 if (($config->get('system', 'block_public') && !Session::isAuthenticated()) ||
48 ($config->get('system', 'block_local_dir') && !Session::isAuthenticated())) {
49 throw new HTTPException\ForbiddenException(DI::l10n()->t('Public access denied.'));
53 DI::page()['aside'] .= Widget::findPeople();
54 DI::page()['aside'] .= Widget::follow();
60 Nav::setSelected('directory');
62 $search = (!empty($_REQUEST['search']) ?
63 Strings::escapeTags(trim(rawurldecode($_REQUEST['search']))) :
67 $dirURL = $config->get('system', 'directory');
68 if (strlen($dirURL)) {
69 $gDirPath = Profile::zrl($dirURL, true);
72 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 60);
74 $profiles = Profile::searchProfiles($pager->getStart(), $pager->getItemsPerPage(), $search);
76 if ($profiles['total'] === 0) {
77 notice(DI::l10n()->t('No entries (some entries may be hidden).'));
79 if (in_array('small', $app->argv)) {
85 foreach ($profiles['entries'] as $entry) {
86 $entries[] = self::formatEntry($entry, $photo);
90 $tpl = Renderer::getMarkupTemplate('directory_header.tpl');
92 $output .= Renderer::replaceMacros($tpl, [
94 '$globaldir' => DI::l10n()->t('Global Directory'),
95 '$gDirPath' => $gDirPath,
96 '$desc' => DI::l10n()->t('Find on this site'),
97 '$contacts' => $entries,
98 '$finding' => DI::l10n()->t('Results for:'),
99 '$findterm' => (strlen($search) ? $search : ""),
100 '$title' => DI::l10n()->t('Site Directory'),
101 '$search_mod' => 'directory',
102 '$submit' => DI::l10n()->t('Find'),
103 '$paginate' => $pager->renderFull($profiles['total']),
110 * Format contact/profile/user data from the database into an usable
111 * array for displaying directory entries.
113 * @param array $contact The directory entry from the database.
114 * @param string $photo_size Avatar size (thumb, photo or micro).
120 public static function formatEntry(array $contact, $photo_size = 'photo')
122 $itemurl = (($contact['addr'] != "") ? $contact['addr'] : $contact['url']);
124 $profile_link = $contact['url'];
126 $about = (($contact['about']) ? $contact['about'] . '<br />' : '');
129 if (strlen($contact['locality'])) {
130 $details .= $contact['locality'];
132 if (strlen($contact['region'])) {
133 if (strlen($contact['locality'])) {
136 $details .= $contact['region'];
138 if (strlen($contact['country-name'])) {
139 if (strlen($details)) {
142 $details .= $contact['country-name'];
147 if (!empty($profile['address'])
148 || !empty($profile['locality'])
149 || !empty($profile['region'])
150 || !empty($profile['postal-code'])
151 || !empty($profile['country-name'])
153 $location = DI::l10n()->t('Location:');
158 $homepage = (!empty($profile['homepage']) ? DI::l10n()->t('Homepage:') : false);
160 $location_e = $location;
163 'profile' => [DI::l10n()->t("View Profile"), Contact::magicLink($profile_link)]
167 'id' => $contact['id'],
168 'url' => Contact::magicLink($profile_link),
169 'itemurl' => $itemurl,
170 'thumb' => Contact::getThumb($contact),
171 'img_hover' => $contact['name'],
172 'name' => $contact['name'],
173 'details' => $details,
174 'account_type' => Contact::getAccountType($contact),
175 'profile' => $profile,
176 'location' => $location_e,
177 'tags' => $contact['pub_keywords'],
179 'homepage' => $homepage,
180 'photo_menu' => $photo_menu,
184 $hook = ['contact' => $contact, 'entry' => $entry];
186 Hook::callAll('directory_item', $hook);
191 return $hook['entry'];