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