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