]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/theme.php
Merge pull request #10548 from annando/args
[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 function frio_init(App $a)
31 {
32         global $frio;
33         $frio = 'view/theme/frio';
34
35         // disable the events module link in the profile tab
36         $a->setThemeInfoValue('events_in_profile', false);
37         $a->setThemeInfoValue('videowidth', 622);
38
39         Renderer::setActiveTemplateEngine('smarty3');
40
41         // if the device is a mobile device set js is_mobile
42         // variable so the js scripts can use this information
43         if (DI::mode()->isMobile() || DI::mode()->isMobile()) {
44                 DI::page()['htmlhead'] .= <<< EOT
45                         <script type="text/javascript">
46                                 var is_mobile = 1;
47                         </script>
48 EOT;
49         }
50 }
51
52 function frio_install()
53 {
54         Hook::register('prepare_body_final', 'view/theme/frio/theme.php', 'frio_item_photo_links');
55         Hook::register('item_photo_menu', 'view/theme/frio/theme.php', 'frio_item_photo_menu');
56         Hook::register('contact_photo_menu', 'view/theme/frio/theme.php', 'frio_contact_photo_menu');
57         Hook::register('nav_info', 'view/theme/frio/theme.php', 'frio_remote_nav');
58         Hook::register('acl_lookup_end', 'view/theme/frio/theme.php', 'frio_acl_lookup');
59         Hook::register('display_item', 'view/theme/frio/theme.php', 'frio_display_item');
60
61         Logger::log('installed theme frio');
62 }
63
64 /**
65  * Replace friendica photo links hook
66  *
67  *  This function does replace the links to photos
68  *  of other friendica users. Original the photos are
69  *  linked to the photo page. Now they will linked directly
70  *  to the photo file. This function is nessesary to use colorbox
71  *  in the network stream
72  *
73  * @param App $a Unused but required by hook definition
74  * @param array $body_info The item and its html output
75  */
76 function frio_item_photo_links(App $a, &$body_info)
77 {
78         $occurence = 0;
79         $p = Plaintext::getBoundariesPosition($body_info['html'], '<a', '>');
80         while ($p !== false && ($occurence++ < 500)) {
81                 $link = substr($body_info['html'], $p['start'], $p['end'] - $p['start']);
82                 $matches = [];
83
84                 preg_match('/\/photos\/[\w]+\/image\/([\w]+)/', $link, $matches);
85                 if ($matches) {
86                         // Replace the link for the photo's page with a direct link to the photo itself
87                         $newlink = str_replace($matches[0], "/photo/{$matches[1]}", $link);
88
89                         // Add a "quiet" parameter to any redir links to prevent the "XX welcomes YY" info boxes
90                         $newlink = preg_replace('/href="([^"]+)\/redir\/([^"]+)&url=([^"]+)"/', 'href="$1/redir/$2&quiet=1&url=$3"', $newlink);
91
92                         // Having any arguments to the link for Colorbox causes it to fetch base64 code instead of the image
93                         $newlink = preg_replace('/\/[?&]zrl=([^&"]+)/', '', $newlink);
94
95                         $body_info['html'] = str_replace($link, $newlink, $body_info['html']);
96                 }
97
98                 $p = Plaintext::getBoundariesPosition($body_info['html'], '<a', '>', $occurence);
99         }
100 }
101
102 /**
103  * Replace links of the item_photo_menu hook
104  *
105  *  This function replaces the original poke and the message links
106  *  to call the addToModal javascript function so this pages can
107  *  be loaded in a bootstrap modal
108  *
109  * @param App $a Unused but required by the hook definition
110  * @param array $arr Contains item data and the original photo_menu
111  */
112 function frio_item_photo_menu(App $a, &$arr)
113 {
114         foreach ($arr['menu'] as $k => $v) {
115                 if (strpos($v, '/poke') === 0 || strpos($v, 'message/new/') === 0) {
116                         $v = 'javascript:addToModal(\'' . $v . '\'); return false;';
117                         $arr['menu'][$k] = $v;
118                 }
119         }
120 }
121
122 /**
123  * Replace links of the contact_photo_menu
124  *
125  *  This function replaces the original poke and the message links
126  *  to call the addToModal javascript function so this pages can
127  *  be loaded in a bootstrap modal
128  *  Additionally the profile, status and photo page links  will be changed
129  *  to don't open in a new tab if the contact is a friendica contact.
130  *
131  * @param App $a The app data
132  * @param array $args Contains contact data and the original photo_menu
133  */
134 function frio_contact_photo_menu(App $a, &$args)
135 {
136         $cid = $args['contact']['id'];
137
138         if (!empty($args['menu']['poke'])) {
139                 $pokelink = $args['menu']['poke'][1];
140         } else {
141                 $pokelink = '';
142         }
143
144         if (!empty($args['menu']['poke'])) {
145                 $pmlink = $args['menu']['pm'][1];
146         } else {
147                 $pmlink = '';
148         }
149
150         // Set the the indicator for opening the status, profile and photo pages
151         // in a new tab to false if the contact a dfrn (friendica) contact
152         // We do this because we can go back on foreign friendica pages throuhg
153         // friendicas "magic-link" which indicates a friendica user on froreign
154         // friendica servers as remote user or visitor
155         //
156         // The value for opening in a new tab is e.g. when
157         // $args['menu']['status'][2] is true. If the value of the [2] key is true
158         // and if it's a friendica contact we set it to false
159         foreach ($args['menu'] as $k => $v) {
160                 if ($k === 'status' || $k === 'profile' || $k === 'photos') {
161                         $v[2] = (($args['contact']['network'] === 'dfrn') ? false : true);
162                         $args['menu'][$k][2] = $v[2];
163                 }
164         }
165
166         // Add to pm and poke links a new key with the value 'modal'.
167         // Later we can make conditions in the corresponing templates (e.g.
168         // contact_template.tpl)
169         if (strpos($pokelink, $cid . '/poke') !== false) {
170                 $args['menu']['poke'][3] = 'modal';
171         }
172
173         if (strpos($pmlink, 'message/new/' . $cid) !== false) {
174                 $args['menu']['pm'][3] = 'modal';
175         }
176 }
177
178 /**
179  * Construct remote nav menu
180  *
181  *  It creates a remote baseurl form $_SESSION for remote users and friendica
182  *  visitors. This url will be added to some of the nav links. With this behaviour
183  *  the user will come back to her/his own pages on his/her friendica server.
184  *  Not all possible links are available (notifications, administrator, manage,
185  *  notes aren't available because we have no way the check remote permissions)..
186  *  Some links will point to the local pages because the user would expect
187  *  local page (these pages are: search, community, help, apps, directory).
188  *
189  * @param App   $a        The App class
190  * @param array $nav_info The original nav info array: nav, banner, userinfo, sitelocation
191  * @throws Exception
192  */
193 function frio_remote_nav(App $a, array &$nav_info)
194 {
195         // get the homelink from $_XSESSION
196         $homelink = Model\Profile::getMyURL();
197         if (!$homelink) {
198                 $homelink = Session::get('visitor_home', '');
199         }
200
201         // since $userinfo isn't available for the hook we write it to the nav array
202         // this isn't optimal because the contact query will be done now twice
203         $fields = ['id', 'url', 'avatar', 'micro', 'name', 'nick', 'baseurl'];
204         if (local_user() && !empty($a->user['uid'])) {
205                 $remoteUser = Contact::selectFirst($fields, ['uid' => $a->user['uid'], 'self' => true]);
206         } elseif (!local_user() && remote_user()) {
207                 $remoteUser = Contact::getById(remote_user(), $fields);
208                 $nav_info['nav']['remote'] = DI::l10n()->t('Guest');
209         } elseif (Model\Profile::getMyURL()) {
210                 $remoteUser = Contact::getByURL($homelink, null, $fields);
211                 $nav_info['nav']['remote'] = DI::l10n()->t('Visitor');
212         } else {
213                 $remoteUser = null;
214         }
215
216         if (DBA::isResult($remoteUser)) {
217                 $nav_info['userinfo'] = [
218                         'icon' => Contact::getMicro($remoteUser),
219                         'name' => $remoteUser['name'],
220                 ];
221                 $server_url = $remoteUser['baseurl'];
222         }
223
224         if (!local_user() && !empty($server_url) && !is_null($remoteUser)) {
225                 // user menu
226                 $nav_info['nav']['usermenu'][] = [$server_url . '/profile/' . $remoteUser['nick'], DI::l10n()->t('Status'), '', DI::l10n()->t('Your posts and conversations')];
227                 $nav_info['nav']['usermenu'][] = [$server_url . '/profile/' . $remoteUser['nick'] . '/profile', DI::l10n()->t('Profile'), '', DI::l10n()->t('Your profile page')];
228                 $nav_info['nav']['usermenu'][] = [$server_url . '/photos/' . $remoteUser['nick'], DI::l10n()->t('Photos'), '', DI::l10n()->t('Your photos')];
229                 $nav_info['nav']['usermenu'][] = [$server_url . '/videos/' . $remoteUser['nick'], DI::l10n()->t('Videos'), '', DI::l10n()->t('Your videos')];
230                 $nav_info['nav']['usermenu'][] = [$server_url . '/events/', DI::l10n()->t('Events'), '', DI::l10n()->t('Your events')];
231
232                 // navbar links
233                 $nav_info['nav']['network'] = [$server_url . '/network', DI::l10n()->t('Network'), '', DI::l10n()->t('Conversations from your friends')];
234                 $nav_info['nav']['events'] = [$server_url . '/events', DI::l10n()->t('Events'), '', DI::l10n()->t('Events and Calendar')];
235                 $nav_info['nav']['messages'] = [$server_url . '/message', DI::l10n()->t('Messages'), '', DI::l10n()->t('Private mail')];
236                 $nav_info['nav']['settings'] = [$server_url . '/settings', DI::l10n()->t('Settings'), '', DI::l10n()->t('Account settings')];
237                 $nav_info['nav']['contacts'] = [$server_url . '/contact', DI::l10n()->t('Contacts'), '', DI::l10n()->t('Manage/edit friends and contacts')];
238                 $nav_info['nav']['sitename'] = DI::config()->get('config', 'sitename');
239         }
240 }
241
242 /**
243  * Search for contacts
244  *
245  * This function search for a users contacts. The code is copied from contact search
246  * in /src/Module/Contact.php. With this function the contacts will permitted to acl_lookup()
247  * and can grabbed as json. For this we use the type="r". This is usful to to let js
248  * grab the contact data.
249  * We use this to give the data to textcomplete and have a filter function at the
250  * contact page.
251  *
252  * @param App $a The app data @TODO Unused
253  * @param array $results The array with the originals from acl_lookup()
254  */
255 function frio_acl_lookup(App $a, &$results)
256 {
257         $nets = !empty($_GET['nets']) ? Strings::escapeTags(trim($_GET['nets'])) : '';
258
259         // we introduce a new search type, r should do the same query like it's
260         // done in /src/Module/Contact.php for connections
261         if ($results['type'] !== 'r') {
262                 return;
263         }
264
265         $sql_extra = '';
266         if ($results['search']) {
267                 $search_txt = DBA::escape(Strings::protectSprintf(preg_quote($results['search'])));
268                 $sql_extra .= " AND (`attag` LIKE '%%" . $search_txt . "%%' OR `name` LIKE '%%" . $search_txt . "%%' OR `nick` LIKE '%%" . $search_txt . "%%') ";
269         }
270
271         if ($nets) {
272                 $sql_extra .= sprintf(" AND network = '%s' ", DBA::escape($nets));
273         }
274
275         $total = 0;
276         $r = q("SELECT COUNT(*) AS `total` FROM `contact`
277                 WHERE `uid` = %d AND NOT `self` AND NOT `deleted` AND NOT `pending` $sql_extra ", intval($_SESSION['uid']));
278         if (DBA::isResult($r)) {
279                 $total = $r[0]['total'];
280         }
281
282         $sql_extra3 = Widget::unavailableNetworks();
283
284         $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 ",
285                 intval($_SESSION['uid']), intval($results['start']), intval($results['count'])
286         );
287
288         $contacts = [];
289
290         if (DBA::isResult($r)) {
291                 foreach ($r as $rr) {
292                         $contacts[] = Module\Contact::getContactTemplateVars($rr);
293                 }
294         }
295
296         $results['items'] = $contacts;
297         $results['tot'] = $total;
298 }
299
300 /**
301  * Manipulate the data of the item
302  *
303  * At the moment we use this function to add some own stuff to the item menu
304  *
305  * @param App $a App $a The app data
306  * @param array $arr Array with the item and the item actions<br>
307  *     'item' => Array with item data<br>
308  *     'output' => Array with item actions<br>
309  */
310 function frio_display_item(App $a, &$arr)
311 {
312         // Add follow to the item menu
313         $followThread = [];
314         if (
315                 local_user()
316                 && local_user() == $arr['item']['uid']
317                 && $arr['item']['gravity'] == GRAVITY_PARENT
318                 && !$arr['item']['self']
319                 && !$arr['item']['mention']
320         ) {
321                 $followThread = [
322                         'menu'   => 'follow_thread',
323                         'title'  => DI::l10n()->t('Follow Thread'),
324                         'action' => 'doFollowThread(' . $arr['item']['id'] . ');',
325                         'href'   => '#'
326                 ];
327         }
328         $arr['output']['follow_thread'] = $followThread;
329 }