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