]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/theme.php
c7d38baeb970edae0b865533fe8cae449d52e2a3
[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\Addon;
14 use Friendica\Core\Config;
15 use Friendica\Core\L10n;
16 use Friendica\Core\Logger;
17 use Friendica\Core\PConfig;
18 use Friendica\Core\Renderer;
19 use Friendica\Core\System;
20 use Friendica\Database\DBA;
21 use Friendica\Model;
22 use Friendica\Module;
23
24 $frio = 'view/theme/frio';
25
26 global $frio;
27
28 function frio_init(App $a)
29 {
30         // disable the events module link in the profile tab
31         $a->theme_events_in_profile = false;
32         $a->videowidth = 622;
33
34         Renderer::setActiveTemplateEngine('smarty3');
35
36         $baseurl = System::baseUrl();
37
38         $style = PConfig::get(local_user(), 'frio', 'style');
39
40         $frio = 'view/theme/frio';
41
42         global $frio;
43
44         // if the device is a mobile device set js is_mobile
45         // variable so the js scripts can use this information
46         if ($a->is_mobile || $a->is_tablet) {
47                 $a->page['htmlhead'] .= <<< EOT
48                         <script type="text/javascript">
49                                 var is_mobile = 1;
50                         </script>
51 EOT;
52         }
53
54         if ($style == '') {
55                 $style = Config::get('frio', 'style');
56         }
57 }
58
59 function frio_install()
60 {
61         Addon::registerHook('prepare_body_final', 'view/theme/frio/theme.php', 'frio_item_photo_links');
62         Addon::registerHook('item_photo_menu', 'view/theme/frio/theme.php', 'frio_item_photo_menu');
63         Addon::registerHook('contact_photo_menu', 'view/theme/frio/theme.php', 'frio_contact_photo_menu');
64         Addon::registerHook('nav_info', 'view/theme/frio/theme.php', 'frio_remote_nav');
65         Addon::registerHook('acl_lookup_end', 'view/theme/frio/theme.php', 'frio_acl_lookup');
66         Addon::registerHook('display_item', 'view/theme/frio/theme.php', 'frio_display_item');
67
68         Logger::log('installed theme frio');
69 }
70
71 function frio_uninstall()
72 {
73         Addon::unregisterHook('prepare_body_final', 'view/theme/frio/theme.php', 'frio_item_photo_links');
74         Addon::unregisterHook('item_photo_menu', 'view/theme/frio/theme.php', 'frio_item_photo_menu');
75         Addon::unregisterHook('contact_photo_menu', 'view/theme/frio/theme.php', 'frio_contact_photo_menu');
76         Addon::unregisterHook('nav_info', 'view/theme/frio/theme.php', 'frio_remote_nav');
77         Addon::unregisterHook('acl_lookup_end', 'view/theme/frio/theme.php', 'frio_acl_lookup');
78         Addon::unregisterHook('display_item', 'view/theme/frio/theme.php', 'frio_display_item');
79
80         Logger::log('uninstalled theme frio');
81 }
82
83 /**
84  * @brief 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="([^"]+)\/redir\/([^"]+)&url=([^"]+)"/', 'href="$1/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  * @brief Replace links of the item_photo_menu hook
123  *
124  *  This function replaces the original poke and the 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, 'poke/?f=&c=') === 0 || strpos($v, 'message/new/') === 0) {
135                         $v = 'javascript:addToModal(\'' . $v . '\'); return false;';
136                         $arr['menu'][$k] = $v;
137                 }
138         }
139 }
140
141 /**
142  * @brief Replace links of the contact_photo_menu
143  *
144  *  This function replaces the original poke and the message links
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']['poke'])) {
158                 $pokelink = $args['menu']['poke'][1];
159         } else {
160                 $pokelink = '';
161         }
162
163         if (!empty($args['menu']['poke'])) {
164                 $pmlink = $args['menu']['pm'][1];
165         } else {
166                 $pmlink = '';
167         }
168
169         // Set the the indicator for opening the status, profile and photo pages
170         // in a new tab to false if the contact a dfrn (friendica) contact
171         // We do this because we can go back on foreign friendica pages throuhg
172         // friendicas "magic-link" which indicates a friendica user on froreign
173         // friendica servers as remote user or visitor
174         //
175         // The value for opening in a new tab is e.g. when
176         // $args['menu']['status'][2] is true. If the value of the [2] key is true
177         // and if it's a friendica contact we set it to false
178         foreach ($args['menu'] as $k => $v) {
179                 if ($k === 'status' || $k === 'profile' || $k === 'photos') {
180                         $v[2] = (($args['contact']['network'] === 'dfrn') ? false : true);
181                         $args['menu'][$k][2] = $v[2];
182                 }
183         }
184
185         // Add to pm and poke links a new key with the value 'modal'.
186         // Later we can make conditions in the corresponing templates (e.g.
187         // contact_template.tpl)
188         if (strpos($pokelink, 'poke/?f=&c=' . $cid) !== false) {
189                 $args['menu']['poke'][3] = 'modal';
190         }
191
192         if (strpos($pmlink, 'message/new/' . $cid) !== false) {
193                 $args['menu']['pm'][3] = 'modal';
194         }
195 }
196
197 /**
198  * @brief Construct remote nav menu
199  *
200  *  It creates a remote baseurl form $_SESSION for remote users and friendica
201  *  visitors. This url will be added to some of the nav links. With this behaviour
202  *  the user will come back to her/his own pages on his/her friendica server.
203  *  Not all possible links are available (notifications, administrator, manage,
204  *  notes aren't available because we have no way the check remote permissions)..
205  *  Some links will point to the local pages because the user would expect
206  *  local page (these pages are: search, community, help, apps, directory).
207  *
208  * @param App $a The App class
209  * @param array $nav The original nav menu
210  */
211 function frio_remote_nav($a, &$nav)
212 {
213         // get the homelink from $_XSESSION
214         $homelink = Model\Profile::getMyURL();
215         if (!$homelink) {
216                 $homelink = defaults($_SESSION, 'visitor_home', '');
217         }
218
219         // split up the url in it's parts (protocol,domain/directory, /profile/, nickname
220         // I'm not familiar with regex, so someone might find a better solutionen
221         //
222         // E.g $homelink = 'https://friendica.domain.com/profile/mickey' should result in an array
223         // with 0 => 'https://friendica.domain.com/profile/mickey' 1 => 'https://',
224         // 2 => 'friendica.domain.com' 3 => '/profile/' 4 => 'mickey'
225         //
226         //$server_url = preg_match('/^(https?:\/\/.*?)\/profile\//2', $homelink);
227         preg_match('/^(https?:\/\/)?(.*?)(\/profile\/)(.*)/', $homelink, $url_parts);
228
229         // Construct the server url of the visitor. So we could link back to his/her own menu.
230         // And construct a webbie (e.g. mickey@friendica.domain.com for the search in gcontact
231         // We use the webbie for search in gcontact because we don't know if gcontact table stores
232         // the right value if its http or https protocol
233         $webbie = '';
234         if (count($url_parts)) {
235                 $server_url = $url_parts[1] . $url_parts[2];
236                 $webbie = $url_parts[4] . '@' . $url_parts[2];
237         }
238
239         // since $userinfo isn't available for the hook we write it to the nav array
240         // this isn't optimal because the contact query will be done now twice
241         if (local_user()) {
242                 // empty the server url for local user because we won't need it
243                 $server_url = '';
244                 // user info
245                 $r = q("SELECT `micro` FROM `contact` WHERE `uid` = %d AND `self`", intval($a->user['uid']));
246
247                 $r[0]['photo'] = (DBA::isResult($r) ? $a->removeBaseURL($r[0]['micro']) : 'images/person-48.jpg');
248                 $r[0]['name'] = $a->user['username'];
249         } elseif (!local_user() && remote_user()) {
250                 $r = q("SELECT `name`, `nick`, `micro` AS `photo` FROM `contact` WHERE `id` = %d", intval(remote_user()));
251                 $nav['remote'] = L10n::t('Guest');
252         } elseif (Model\Profile::getMyURL()) {
253                 $r = q("SELECT `name`, `nick`, `photo` FROM `gcontact`
254                                 WHERE `addr` = '%s' AND `network` = 'dfrn'",
255                         DBA::escape($webbie));
256                 $nav['remote'] = L10n::t('Visitor');
257         } else {
258                 $r = false;
259         }
260
261         if (DBA::isResult($r)) {
262                 $nav['userinfo'] = [
263                         'icon' => (DBA::isResult($r) ? $r[0]['photo'] : 'images/person-48.jpg'),
264                         'name' => $r[0]['name'],
265                 ];
266         }
267
268         if (!local_user() && !empty($server_url)) {
269                 $nav['logout'] = [$server_url . '/logout', L10n::t('Logout'), '', L10n::t('End this session')];
270
271                 // user menu
272                 $nav['usermenu'][] = [$server_url . '/profile/' . $a->user['nickname'], L10n::t('Status'), '', L10n::t('Your posts and conversations')];
273                 $nav['usermenu'][] = [$server_url . '/profile/' . $a->user['nickname'] . '?tab=profile', L10n::t('Profile'), '', L10n::t('Your profile page')];
274                 $nav['usermenu'][] = [$server_url . '/photos/' . $a->user['nickname'], L10n::t('Photos'), '', L10n::t('Your photos')];
275                 $nav['usermenu'][] = [$server_url . '/videos/' . $a->user['nickname'], L10n::t('Videos'), '', L10n::t('Your videos')];
276                 $nav['usermenu'][] = [$server_url . '/events/', L10n::t('Events'), '', L10n::t('Your events')];
277
278                 // navbar links
279                 $nav['network'] = [$server_url . '/network', L10n::t('Network'), '', L10n::t('Conversations from your friends')];
280                 $nav['events'] = [$server_url . '/events', L10n::t('Events'), '', L10n::t('Events and Calendar')];
281                 $nav['messages'] = [$server_url . '/message', L10n::t('Messages'), '', L10n::t('Private mail')];
282                 $nav['settings'] = [$server_url . '/settings', L10n::t('Settings'), '', L10n::t('Account settings')];
283                 $nav['contacts'] = [$server_url . '/contact', L10n::t('Contacts'), '', L10n::t('Manage/edit friends and contacts')];
284                 $nav['sitename'] = Config::get('config', 'sitename');
285         }
286 }
287
288 /**
289  * @brief: Search for contacts
290  *
291  * This function search for a users contacts. The code is copied from contact search
292  * in /src/Module/Contact.php. With this function the contacts will permitted to acl_lookup()
293  * and can grabbed as json. For this we use the type="r". This is usful to to let js
294  * grab the contact data.
295  * We use this to give the data to textcomplete and have a filter function at the
296  * contact page.
297  *
298  * @param App $a The app data @TODO Unused
299  * @param array $results The array with the originals from acl_lookup()
300  */
301 function frio_acl_lookup(App $a, &$results)
302 {
303         $nets = x($_GET, 'nets') ? notags(trim($_GET['nets'])) : '';
304
305         // we introduce a new search type, r should do the same query like it's
306         // done in /src/Module/Contact.php for connections
307         if ($results['type'] !== 'r') {
308                 return;
309         }
310
311         $sql_extra = '';
312         if ($results['search']) {
313                 $search_txt = DBA::escape(protect_sprintf(preg_quote($results['search'])));
314                 $sql_extra .= " AND (`attag` LIKE '%%" . $search_txt . "%%' OR `name` LIKE '%%" . $search_txt . "%%' OR `nick` LIKE '%%" . $search_txt . "%%') ";
315         }
316
317         if ($nets) {
318                 $sql_extra .= sprintf(" AND network = '%s' ", DBA::escape($nets));
319         }
320
321         $total = 0;
322         $r = q("SELECT COUNT(*) AS `total` FROM `contact`
323                 WHERE `uid` = %d AND NOT `self` AND NOT `pending` $sql_extra ", intval($_SESSION['uid']));
324         if (DBA::isResult($r)) {
325                 $total = $r[0]['total'];
326         }
327
328         $sql_extra3 = Widget::unavailableNetworks();
329
330         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `self` AND NOT `pending` $sql_extra $sql_extra3 ORDER BY `name` ASC LIMIT %d, %d ",
331                 intval($_SESSION['uid']), intval($results['start']), intval($results['count'])
332         );
333
334         $contacts = [];
335
336         if (DBA::isResult($r)) {
337                 foreach ($r as $rr) {
338                         $contacts[] = Module\Contact::getContactTemplateVars($rr);
339                 }
340         }
341
342         $results['items'] = $contacts;
343         $results['tot'] = $total;
344 }
345
346 /**
347  * @brief Manipulate the data of the item
348  *
349  * At the moment we use this function to add some own stuff to the item menu
350  *
351  * @param App $a App $a The app data
352  * @param array $arr Array with the item and the item actions<br>
353  *     'item' => Array with item data<br>
354  *     'output' => Array with item actions<br>
355  */
356 function frio_display_item(App $a, &$arr)
357 {
358         // Add subthread to the item menu
359         $subthread = [];
360         if (
361                 local_user()
362                 && local_user() == $arr['item']['uid']
363                 && $arr['item']['parent'] == $arr['item']['id']
364                 && !$arr['item']['self'])
365         {
366                 $subthread = [
367                         'menu'   => 'follow_thread',
368                         'title'  => L10n::t('Follow Thread'),
369                         'action' => 'dosubthread(' . $arr['item']['id'] . '); return false;',
370                         'href'   => '#'
371                 ];
372         }
373         $arr['output']['subthread'] = $subthread;
374 }