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