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