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