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