]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/theme.php
d49ffaa6f50359647be9b955b9e452b55750149a
[friendica.git] / view / theme / frio / theme.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  * Name: frio
21  * Description: Responsive theme based on a modern HTML/CSS/JavaScript framework.
22  * Version: V.1.0
23  * Author: Rabuzarus <https://friendica.kommune4.de/profile/rabuzarus>
24  * Maintainer: Hypolite Petovan <https://friendica.mrpetovan.com/profile/hypolite>
25  */
26
27 use Friendica\App;
28 use Friendica\Content\Text\Plaintext;
29 use Friendica\Core\Hook;
30 use Friendica\Core\Logger;
31 use Friendica\Core\Renderer;
32 use Friendica\Database\DBA;
33 use Friendica\DI;
34 use Friendica\Model\Contact;
35 use Friendica\Model\Item;
36 use Friendica\Model\Profile;
37
38 const FRIO_SCHEME_ACCENT_BLUE   = '#1e87c2';
39 const FRIO_SCHEME_ACCENT_RED    = '#b50404';
40 const FRIO_SCHEME_ACCENT_PURPLE = '#a54bad';
41 const FRIO_SCHEME_ACCENT_GREEN  = '#218f39';
42 const FRIO_SCHEME_ACCENT_PINK   = '#d900a9';
43
44 /*
45  * This script can be included even when the app is in maintenance mode which requires us to avoid any config call
46  */
47
48 function frio_init(App $a)
49 {
50         global $frio;
51         $frio = 'view/theme/frio';
52
53         $a->setThemeInfoValue('videowidth', 622);
54
55         Renderer::setActiveTemplateEngine('smarty3');
56
57         // if the device is a mobile device set js is_mobile
58         // variable so the js scripts can use this information
59         if (DI::mode()->isMobile() || DI::mode()->isMobile()) {
60                 DI::page()['htmlhead'] .= <<< EOT
61                         <script type="text/javascript">
62                                 var is_mobile = 1;
63                         </script>
64 EOT;
65         }
66 }
67
68 function frio_install()
69 {
70         Hook::register('prepare_body_final', 'view/theme/frio/theme.php', 'frio_item_photo_links');
71         Hook::register('item_photo_menu', 'view/theme/frio/theme.php', 'frio_item_photo_menu');
72         Hook::register('contact_photo_menu', 'view/theme/frio/theme.php', 'frio_contact_photo_menu');
73         Hook::register('nav_info', 'view/theme/frio/theme.php', 'frio_remote_nav');
74         Hook::register('display_item', 'view/theme/frio/theme.php', 'frio_display_item');
75
76         Logger::info('installed theme frio');
77 }
78
79 /**
80  * Replace friendica photo links hook
81  *
82  *  This function does replace the links to photos
83  *  of other friendica users. Original the photos are
84  *  linked to the photo page. Now they will linked directly
85  *  to the photo file. This function is necessary to use colorbox
86  *  in the network stream
87  *
88  * @param array $body_info The item and its html output
89  */
90 function frio_item_photo_links(&$body_info)
91 {
92         $occurence = 0;
93         $p = Plaintext::getBoundariesPosition($body_info['html'], '<a', '>');
94         while ($p !== false && ($occurence++ < 500)) {
95                 $link = substr($body_info['html'], $p['start'], $p['end'] - $p['start']);
96                 $matches = [];
97
98                 preg_match('/\/photos\/[\w]+\/image\/([\w]+)/', $link, $matches);
99                 if ($matches) {
100                         // Replace the link for the photo's page with a direct link to the photo itself
101                         $newlink = str_replace($matches[0], "/photo/{$matches[1]}", $link);
102
103                         // Add a "quiet" parameter to any redir links to prevent the "XX welcomes YY" info boxes
104                         $newlink = preg_replace('#href="([^"]+)/contact/redir/(\d+)&url=([^"]+)"#', 'href="$1/contact/redir/$2&quiet=1&url=$3"', $newlink);
105
106                         // Having any arguments to the link for Colorbox causes it to fetch base64 code instead of the image
107                         $newlink = preg_replace('/\/[?&]zrl=([^&"]+)/', '', $newlink);
108
109                         $body_info['html'] = str_replace($link, $newlink, $body_info['html']);
110                 }
111
112                 $p = Plaintext::getBoundariesPosition($body_info['html'], '<a', '>', $occurence);
113         }
114 }
115
116 /**
117  * Replace links of the item_photo_menu hook
118  *
119  *  This function replaces the original message links
120  *  to call the addToModal javascript function so this pages can
121  *  be loaded in a bootstrap modal
122  *
123  * @param array $arr Contains item data and the original photo_menu
124  */
125 function frio_item_photo_menu(&$arr)
126 {
127         foreach ($arr['menu'] as $k => $v) {
128                 if (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 message link
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 array $args Contains contact data and the original photo_menu
145  */
146 function frio_contact_photo_menu(&$args)
147 {
148         $cid = $args['contact']['id'];
149
150         if (!empty($args['menu']['pm'])) {
151                 $pmlink = $args['menu']['pm'][1];
152         } else {
153                 $pmlink = '';
154         }
155
156         // Set the the indicator for opening the status, profile and photo pages
157         // in a new tab to false if the contact a dfrn (friendica) contact
158         // We do this because we can go back on foreign friendica pages throuhg
159         // friendicas "magic-link" which indicates a friendica user on foreign
160         // friendica servers as remote user or visitor
161         //
162         // The value for opening in a new tab is e.g. when
163         // $args['menu']['status'][2] is true. If the value of the [2] key is true
164         // and if it's a friendica contact we set it to false
165         foreach ($args['menu'] as $k => $v) {
166                 if ($k === 'status' || $k === 'profile' || $k === 'photos') {
167                         $v[2] = (($args['contact']['network'] === 'dfrn') ? false : true);
168                         $args['menu'][$k][2] = $v[2];
169                 }
170         }
171
172         // Add to pm link a new key with the value 'modal'.
173         // Later we can make conditions in the corresponding templates (e.g.
174         // contact/entry.tpl)
175         if (strpos($pmlink, 'message/new/' . $cid) !== false) {
176                 $args['menu']['pm'][3] = 'modal';
177         }
178 }
179
180 /**
181  * Construct remote nav menu
182  *
183  *  It creates a remote baseurl form $_SESSION for remote users and friendica
184  *  visitors. This url will be added to some of the nav links. With this behaviour
185  *  the user will come back to her/his own pages on his/her friendica server.
186  *  Not all possible links are available (notifications, administrator, manage,
187  *  notes aren't available because we have no way the check remote permissions)..
188  *  Some links will point to the local pages because the user would expect
189  *  local page (these pages are: search, community, help, apps, directory).
190  *
191  * @param App   $a        The App class
192  * @param array $nav_info The original nav info array: nav, banner, userinfo, sitelocation
193  * @throws Exception
194  */
195 function frio_remote_nav(array &$nav_info)
196 {
197         if (DI::mode()->has(App\Mode::MAINTENANCEDISABLED)) {
198                 // get the homelink from $_SESSION
199                 $homelink = Profile::getMyURL();
200                 if (!$homelink) {
201                         $homelink = DI::session()->get('visitor_home', '');
202                 }
203
204                 // since $userinfo isn't available for the hook we write it to the nav array
205                 // this isn't optimal because the contact query will be done now twice
206                 $fields = ['id', 'url', 'avatar', 'micro', 'name', 'nick', 'baseurl', 'updated'];
207                 if (DI::userSession()->isAuthenticated()) {
208                         $remoteUser = Contact::selectFirst($fields, ['uid' => DI::userSession()->getLocalUserId(), 'self' => true]);
209                 } elseif (!DI::userSession()->getLocalUserId() && DI::userSession()->getRemoteUserId()) {
210                         $remoteUser                = Contact::getById(DI::userSession()->getRemoteUserId(), $fields);
211                         $nav_info['nav']['remote'] = DI::l10n()->t('Guest');
212                 } elseif (Profile::getMyURL()) {
213                         $remoteUser                = Contact::getByURL($homelink, null, $fields);
214                         $nav_info['nav']['remote'] = DI::l10n()->t('Visitor');
215                 } else {
216                         $remoteUser = null;
217                 }
218
219                 if (DBA::isResult($remoteUser)) {
220                         $nav_info['userinfo'] = [
221                                 'icon' => Contact::getMicro($remoteUser),
222                                 'name' => $remoteUser['name'],
223                         ];
224                         $server_url           = $remoteUser['baseurl'];
225                 }
226
227                 if (!DI::userSession()->getLocalUserId() && !empty($server_url) && !is_null($remoteUser)) {
228                         // user menu
229                         $nav_info['nav']['usermenu'][] = [$server_url . '/profile/' . $remoteUser['nick'], DI::l10n()->t('Status'), '', DI::l10n()->t('Your posts and conversations')];
230                         $nav_info['nav']['usermenu'][] = [$server_url . '/profile/' . $remoteUser['nick'] . '/profile', DI::l10n()->t('Profile'), '', DI::l10n()->t('Your profile page')];
231                         // Kept for backwards-compatibility reasons, the remote server may not have updated to version 2022.12 yet
232                         // @TODO Switch with the new routes by version 2023.12
233                         //$nav_info['nav']['usermenu'][] = [$server_url . '/profile/' . $remoteUser['nick'] . '/photos', DI::l10n()->t('Photos'), '', DI::l10n()->t('Your photos')];
234                         $nav_info['nav']['usermenu'][] = [$server_url . '/photos/' . $remoteUser['nick'], DI::l10n()->t('Photos'), '', DI::l10n()->t('Your photos')];
235                         $nav_info['nav']['usermenu'][] = [$server_url . '/profile/' . $remoteUser['nick'] . '/media', DI::l10n()->t('Media'), '', DI::l10n()->t('Your postings with media')];
236                         $nav_info['nav']['usermenu'][] = [$server_url . '/calendar/', DI::l10n()->t('Calendar'), '', DI::l10n()->t('Your calendar')];
237
238                         // navbar links
239                         $nav_info['nav']['network']  = [$server_url . '/network', DI::l10n()->t('Network'), '', DI::l10n()->t('Conversations from your friends')];
240                         $nav_info['nav']['calendar'] = [$server_url . '/calendar', DI::l10n()->t('Calendar'), '', DI::l10n()->t('Calendar')];
241                         $nav_info['nav']['messages'] = [$server_url . '/message', DI::l10n()->t('Messages'), '', DI::l10n()->t('Private mail')];
242                         $nav_info['nav']['settings'] = [$server_url . '/settings', DI::l10n()->t('Settings'), '', DI::l10n()->t('Account settings')];
243                         $nav_info['nav']['contacts'] = [$server_url . '/contact', DI::l10n()->t('Contacts'), '', DI::l10n()->t('Manage/edit friends and contacts')];
244                         $nav_info['nav']['sitename'] = DI::config()->get('config', 'sitename');
245                 }
246         }
247 }
248
249 function frio_display_item(&$arr)
250 {
251         // Add follow to the item menu
252         $followThread = [];
253         if (
254                 DI::userSession()->getLocalUserId()
255                 && in_array($arr['item']['uid'], [0, DI::userSession()->getLocalUserId()])
256                 && $arr['item']['gravity'] == Item::GRAVITY_PARENT
257                 && !$arr['item']['self']
258                 && !$arr['item']['mention']
259         ) {
260                 $followThread = [
261                         'menu'   => 'follow_thread',
262                         'title'  => DI::l10n()->t('Follow Thread'),
263                         'action' => 'doFollowThread(' . $arr['item']['id'] . ');',
264                         'href'   => '#'
265                 ];
266         }
267         $arr['output']['follow_thread'] = $followThread;
268 }