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