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