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