Merge pull request #8315 from MrPetovan/task/8310-improve-manifest
[friendica.git/.git] / view / theme / frio / theme.php
1 <?php
2 /*
3  * Name: frio
4  * Description: Bootstrap V3 theme. The theme is currently under construction, so it is far from finished. For further information have a look at the <a href="https://github.com/friendica/friendica/tree/develop/view/theme/frio/README.md">ReadMe</a>.
5  * Version: V.0.8.5
6  * Author: Rabuzarus <https://friendica.kommune4.de/profile/rabuzarus>
7  *
8  */
9
10 use Friendica\App;
11 use Friendica\Content\Text\Plaintext;
12 use Friendica\Content\Widget;
13 use Friendica\Core\Hook;
14 use Friendica\Core\Logger;
15 use Friendica\Core\Renderer;
16 use Friendica\Core\Session;
17 use Friendica\Database\DBA;
18 use Friendica\DI;
19 use Friendica\Model;
20 use Friendica\Module;
21 use Friendica\Util\Strings;
22
23 function frio_init(App $a)
24 {
25         global $frio;
26         $frio = 'view/theme/frio';
27
28         // disable the events module link in the profile tab
29         $a->theme_events_in_profile = false;
30         $a->videowidth = 622;
31
32         Renderer::setActiveTemplateEngine('smarty3');
33
34         // if the device is a mobile device set js is_mobile
35         // variable so the js scripts can use this information
36         if (DI::mode()->isMobile() || DI::mode()->isMobile()) {
37                 DI::page()['htmlhead'] .= <<< EOT
38                         <script type="text/javascript">
39                                 var is_mobile = 1;
40                         </script>
41 EOT;
42         }
43
44         $enable_compose = DI::pConfig()->get(local_user(), 'frio', 'enable_compose');
45         $compose = $enable_compose === '1' || $enable_compose === null && DI::config()->get('frio', 'enable_compose') ? 1 : 0;
46         DI::page()['htmlhead'] .= <<< HTML
47                 <script type="text/javascript">
48                         var compose = $compose;
49                 </script>
50 HTML;
51 }
52
53 function frio_install()
54 {
55         Hook::register('prepare_body_final', 'view/theme/frio/theme.php', 'frio_item_photo_links');
56         Hook::register('item_photo_menu', 'view/theme/frio/theme.php', 'frio_item_photo_menu');
57         Hook::register('contact_photo_menu', 'view/theme/frio/theme.php', 'frio_contact_photo_menu');
58         Hook::register('nav_info', 'view/theme/frio/theme.php', 'frio_remote_nav');
59         Hook::register('acl_lookup_end', 'view/theme/frio/theme.php', 'frio_acl_lookup');
60         Hook::register('display_item', 'view/theme/frio/theme.php', 'frio_display_item');
61
62         Logger::log('installed theme frio');
63 }
64
65 function frio_uninstall()
66 {
67         Hook::unregister('prepare_body_final', 'view/theme/frio/theme.php', 'frio_item_photo_links');
68         Hook::unregister('item_photo_menu', 'view/theme/frio/theme.php', 'frio_item_photo_menu');
69         Hook::unregister('contact_photo_menu', 'view/theme/frio/theme.php', 'frio_contact_photo_menu');
70         Hook::unregister('nav_info', 'view/theme/frio/theme.php', 'frio_remote_nav');
71         Hook::unregister('acl_lookup_end', 'view/theme/frio/theme.php', 'frio_acl_lookup');
72         Hook::unregister('display_item', 'view/theme/frio/theme.php', 'frio_display_item');
73
74         Logger::log('uninstalled theme frio');
75 }
76
77 /**
78  * Replace friendica photo links hook
79  *
80  *  This function does replace the links to photos
81  *  of other friendica users. Original the photos are
82  *  linked to the photo page. Now they will linked directly
83  *  to the photo file. This function is nessesary to use colorbox
84  *  in the network stream
85  *
86  * @param App $a Unused but required by hook definition
87  * @param array $body_info The item and its html output
88  */
89 function frio_item_photo_links(App $a, &$body_info)
90 {
91         $occurence = 0;
92         $p = Plaintext::getBoundariesPosition($body_info['html'], '<a', '>');
93         while ($p !== false && ($occurence++ < 500)) {
94                 $link = substr($body_info['html'], $p['start'], $p['end'] - $p['start']);
95                 $matches = [];
96
97                 preg_match('/\/photos\/[\w]+\/image\/([\w]+)/', $link, $matches);
98                 if ($matches) {
99                         // Replace the link for the photo's page with a direct link to the photo itself
100                         $newlink = str_replace($matches[0], "/photo/{$matches[1]}", $link);
101
102                         // Add a "quiet" parameter to any redir links to prevent the "XX welcomes YY" info boxes
103                         $newlink = preg_replace('/href="([^"]+)\/redir\/([^"]+)&url=([^"]+)"/', 'href="$1/redir/$2&quiet=1&url=$3"', $newlink);
104
105                         // Having any arguments to the link for Colorbox causes it to fetch base64 code instead of the image
106                         $newlink = preg_replace('/\/[?&]zrl=([^&"]+)/', '', $newlink);
107
108                         $body_info['html'] = str_replace($link, $newlink, $body_info['html']);
109                 }
110
111                 $p = Plaintext::getBoundariesPosition($body_info['html'], '<a', '>', $occurence);
112         }
113 }
114
115 /**
116  * Replace links of the item_photo_menu hook
117  *
118  *  This function replaces the original poke and the message links
119  *  to call the addToModal javascript function so this pages can
120  *  be loaded in a bootstrap modal
121  *
122  * @param App $a Unused but required by the hook definition
123  * @param array $arr Contains item data and the original photo_menu
124  */
125 function frio_item_photo_menu(App $a, &$arr)
126 {
127         foreach ($arr['menu'] as $k => $v) {
128                 if (strpos($v, 'poke?c=') === 0 || strpos($v, 'message/new/') === 0) {
129                         $v = 'javascript:addToModal(\'' . $v . '\'); return false;';
130                         $arr['menu'][$k] = $v;
131                 }
132         }
133 }
134
135 /**
136  * Replace links of the contact_photo_menu
137  *
138  *  This function replaces the original poke and the message links
139  *  to call the addToModal javascript function so this pages can
140  *  be loaded in a bootstrap modal
141  *  Additionally the profile, status and photo page links  will be changed
142  *  to don't open in a new tab if the contact is a friendica contact.
143  *
144  * @param App $a The app data
145  * @param array $args Contains contact data and the original photo_menu
146  */
147 function frio_contact_photo_menu(App $a, &$args)
148 {
149         $cid = $args['contact']['id'];
150
151         if (!empty($args['menu']['poke'])) {
152                 $pokelink = $args['menu']['poke'][1];
153         } else {
154                 $pokelink = '';
155         }
156
157         if (!empty($args['menu']['poke'])) {
158                 $pmlink = $args['menu']['pm'][1];
159         } else {
160                 $pmlink = '';
161         }
162
163         // Set the the indicator for opening the status, profile and photo pages
164         // in a new tab to false if the contact a dfrn (friendica) contact
165         // We do this because we can go back on foreign friendica pages throuhg
166         // friendicas "magic-link" which indicates a friendica user on froreign
167         // friendica servers as remote user or visitor
168         //
169         // The value for opening in a new tab is e.g. when
170         // $args['menu']['status'][2] is true. If the value of the [2] key is true
171         // and if it's a friendica contact we set it to false
172         foreach ($args['menu'] as $k => $v) {
173                 if ($k === 'status' || $k === 'profile' || $k === 'photos') {
174                         $v[2] = (($args['contact']['network'] === 'dfrn') ? false : true);
175                         $args['menu'][$k][2] = $v[2];
176                 }
177         }
178
179         // Add to pm and poke links a new key with the value 'modal'.
180         // Later we can make conditions in the corresponing templates (e.g.
181         // contact_template.tpl)
182         if (strpos($pokelink, 'poke?c=' . $cid) !== false) {
183                 $args['menu']['poke'][3] = 'modal';
184         }
185
186         if (strpos($pmlink, 'message/new/' . $cid) !== false) {
187                 $args['menu']['pm'][3] = 'modal';
188         }
189 }
190
191 /**
192  * Construct remote nav menu
193  *
194  *  It creates a remote baseurl form $_SESSION for remote users and friendica
195  *  visitors. This url will be added to some of the nav links. With this behaviour
196  *  the user will come back to her/his own pages on his/her friendica server.
197  *  Not all possible links are available (notifications, administrator, manage,
198  *  notes aren't available because we have no way the check remote permissions)..
199  *  Some links will point to the local pages because the user would expect
200  *  local page (these pages are: search, community, help, apps, directory).
201  *
202  * @param App $a The App class
203  * @param array $nav The original nav menu
204  */
205 function frio_remote_nav($a, &$nav)
206 {
207         // get the homelink from $_XSESSION
208         $homelink = Model\Profile::getMyURL();
209         if (!$homelink) {
210                 $homelink = Session::get('visitor_home', '');
211         }
212
213         // split up the url in it's parts (protocol,domain/directory, /profile/, nickname
214         // I'm not familiar with regex, so someone might find a better solutionen
215         //
216         // E.g $homelink = 'https://friendica.domain.com/profile/mickey' should result in an array
217         // with 0 => 'https://friendica.domain.com/profile/mickey' 1 => 'https://',
218         // 2 => 'friendica.domain.com' 3 => '/profile/' 4 => 'mickey'
219         //
220         //$server_url = preg_match('/^(https?:\/\/.*?)\/profile\//2', $homelink);
221         preg_match('/^(https?:\/\/)?(.*?)(\/profile\/)(.*)/', $homelink, $url_parts);
222
223         // Construct the server url of the visitor. So we could link back to his/her own menu.
224         // And construct a webbie (e.g. mickey@friendica.domain.com for the search in gcontact
225         // We use the webbie for search in gcontact because we don't know if gcontact table stores
226         // the right value if its http or https protocol
227         $webbie = '';
228         if (count($url_parts)) {
229                 $server_url = $url_parts[1] . $url_parts[2];
230                 $webbie = $url_parts[4] . '@' . $url_parts[2];
231         }
232
233         // since $userinfo isn't available for the hook we write it to the nav array
234         // this isn't optimal because the contact query will be done now twice
235         if (local_user()) {
236                 // empty the server url for local user because we won't need it
237                 $server_url = '';
238                 // user info
239                 $r = q("SELECT `micro` FROM `contact` WHERE `uid` = %d AND `self`", intval($a->user['uid']));
240
241                 $r[0]['photo'] = (DBA::isResult($r) ? DI::baseUrl()->remove($r[0]['micro']) : 'images/person-48.jpg');
242                 $r[0]['name'] = $a->user['username'];
243         } elseif (!local_user() && remote_user()) {
244                 $r = q("SELECT `name`, `nick`, `micro` AS `photo` FROM `contact` WHERE `id` = %d", intval(remote_user()));
245                 $nav['remote'] = DI::l10n()->t('Guest');
246         } elseif (Model\Profile::getMyURL()) {
247                 $r = q("SELECT `name`, `nick`, `photo` FROM `gcontact`
248                                 WHERE `addr` = '%s' AND `network` = 'dfrn'",
249                         DBA::escape($webbie));
250                 $nav['remote'] = DI::l10n()->t('Visitor');
251         } else {
252                 $r = false;
253         }
254
255         $remoteUser = null;
256         if (DBA::isResult($r)) {
257                 $nav['userinfo'] = [
258                         'icon' => (DBA::isResult($r) ? $r[0]['photo'] : 'images/person-48.jpg'),
259                         'name' => $r[0]['name'],
260                 ];
261                 $remoteUser = $r[0];
262         }
263
264         if (!local_user() && !empty($server_url) && !is_null($remoteUser)) {
265                 // user menu
266                 $nav['usermenu'][] = [$server_url . '/profile/' . $remoteUser['nick'], DI::l10n()->t('Status'), '', DI::l10n()->t('Your posts and conversations')];
267                 $nav['usermenu'][] = [$server_url . '/profile/' . $remoteUser['nick'] . '/profile', DI::l10n()->t('Profile'), '', DI::l10n()->t('Your profile page')];
268                 $nav['usermenu'][] = [$server_url . '/photos/' . $remoteUser['nick'], DI::l10n()->t('Photos'), '', DI::l10n()->t('Your photos')];
269                 $nav['usermenu'][] = [$server_url . '/videos/' . $remoteUser['nick'], DI::l10n()->t('Videos'), '', DI::l10n()->t('Your videos')];
270                 $nav['usermenu'][] = [$server_url . '/events/', DI::l10n()->t('Events'), '', DI::l10n()->t('Your events')];
271
272                 // navbar links
273                 $nav['network'] = [$server_url . '/network', DI::l10n()->t('Network'), '', DI::l10n()->t('Conversations from your friends')];
274                 $nav['events'] = [$server_url . '/events', DI::l10n()->t('Events'), '', DI::l10n()->t('Events and Calendar')];
275                 $nav['messages'] = [$server_url . '/message', DI::l10n()->t('Messages'), '', DI::l10n()->t('Private mail')];
276                 $nav['settings'] = [$server_url . '/settings', DI::l10n()->t('Settings'), '', DI::l10n()->t('Account settings')];
277                 $nav['contacts'] = [$server_url . '/contact', DI::l10n()->t('Contacts'), '', DI::l10n()->t('Manage/edit friends and contacts')];
278                 $nav['sitename'] = DI::config()->get('config', 'sitename');
279         }
280 }
281
282 /**
283  * Search for contacts
284  *
285  * This function search for a users contacts. The code is copied from contact search
286  * in /src/Module/Contact.php. With this function the contacts will permitted to acl_lookup()
287  * and can grabbed as json. For this we use the type="r". This is usful to to let js
288  * grab the contact data.
289  * We use this to give the data to textcomplete and have a filter function at the
290  * contact page.
291  *
292  * @param App $a The app data @TODO Unused
293  * @param array $results The array with the originals from acl_lookup()
294  */
295 function frio_acl_lookup(App $a, &$results)
296 {
297         $nets = !empty($_GET['nets']) ? Strings::escapeTags(trim($_GET['nets'])) : '';
298
299         // we introduce a new search type, r should do the same query like it's
300         // done in /src/Module/Contact.php for connections
301         if ($results['type'] !== 'r') {
302                 return;
303         }
304
305         $sql_extra = '';
306         if ($results['search']) {
307                 $search_txt = DBA::escape(Strings::protectSprintf(preg_quote($results['search'])));
308                 $sql_extra .= " AND (`attag` LIKE '%%" . $search_txt . "%%' OR `name` LIKE '%%" . $search_txt . "%%' OR `nick` LIKE '%%" . $search_txt . "%%') ";
309         }
310
311         if ($nets) {
312                 $sql_extra .= sprintf(" AND network = '%s' ", DBA::escape($nets));
313         }
314
315         $total = 0;
316         $r = q("SELECT COUNT(*) AS `total` FROM `contact`
317                 WHERE `uid` = %d AND NOT `self` AND NOT `deleted` AND NOT `pending` $sql_extra ", intval($_SESSION['uid']));
318         if (DBA::isResult($r)) {
319                 $total = $r[0]['total'];
320         }
321
322         $sql_extra3 = Widget::unavailableNetworks();
323
324         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `self` AND NOT `deleted` AND NOT `pending` $sql_extra $sql_extra3 ORDER BY `name` ASC LIMIT %d, %d ",
325                 intval($_SESSION['uid']), intval($results['start']), intval($results['count'])
326         );
327
328         $contacts = [];
329
330         if (DBA::isResult($r)) {
331                 foreach ($r as $rr) {
332                         $contacts[] = Module\Contact::getContactTemplateVars($rr);
333                 }
334         }
335
336         $results['items'] = $contacts;
337         $results['tot'] = $total;
338 }
339
340 /**
341  * Manipulate the data of the item
342  *
343  * At the moment we use this function to add some own stuff to the item menu
344  *
345  * @param App $a App $a The app data
346  * @param array $arr Array with the item and the item actions<br>
347  *     'item' => Array with item data<br>
348  *     'output' => Array with item actions<br>
349  */
350 function frio_display_item(App $a, &$arr)
351 {
352         // Add subthread to the item menu
353         $subthread = [];
354         if (
355                 local_user()
356                 && local_user() == $arr['item']['uid']
357                 && $arr['item']['parent'] == $arr['item']['id']
358                 && !$arr['item']['self'])
359         {
360                 $subthread = [
361                         'menu'   => 'follow_thread',
362                         'title'  => DI::l10n()->t('Follow Thread'),
363                         'action' => 'dosubthread(' . $arr['item']['id'] . '); return false;',
364                         'href'   => '#'
365                 ];
366         }
367         $arr['output']['subthread'] = $subthread;
368 }
369
370 /**
371  * @param int|null $uid
372  * @return string
373  * @see \Friendica\Core\Theme::getBackgroundColor()
374  */
375 function frio_get_background_color(int $uid = null)
376 {
377         $background_color = DI::config()->get('frio', 'background_color') ?: '#ededed';
378
379         if ($uid) {
380                 $background_color = DI::pConfig()->get($uid, 'frio', 'background_color') ?: $background_color;
381         }
382
383         $scheme = DI::config()->get('frio', 'scheme', DI::config()->get('frio', 'schema'));
384         $scheme = Strings::sanitizeFilePathItem($scheme);
385
386         if ($scheme && ($scheme != '---') && file_exists('view/theme/frio/scheme/' . $scheme . '.php')) {
387                 $schemefile = 'view/theme/frio/scheme/' . $scheme . '.php';
388                 require_once $schemefile;
389         }
390
391         return $background_color;
392 }
393
394 /**
395  * @param int|null $uid
396  * @return string
397  * @see \Friendica\Core\Theme::getThemeColor()
398  */
399 function frio_get_theme_color(int $uid = null)
400 {
401         $nav_bg = DI::config()->get('frio', 'nav_bg') ?: '#708fa0';
402
403         if ($uid) {
404                 $nav_bg = DI::pConfig()->get($uid, 'frio', 'background_color') ?: $nav_bg;
405         }
406
407         $scheme = DI::config()->get('frio', 'scheme', DI::config()->get('frio', 'schema'));
408         $scheme = Strings::sanitizeFilePathItem($scheme);
409
410         if ($scheme && ($scheme != '---') && file_exists('view/theme/frio/scheme/' . $scheme . '.php')) {
411                 $schemefile = 'view/theme/frio/scheme/' . $scheme . '.php';
412                 require_once $schemefile;
413         }
414
415         return $nav_bg;
416 }