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