]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/theme.php
Adjust for templates
[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
6  * Author: Rabuzarus <https://friendica.kommune4.de/profile/rabuzarus>
7  *
8  */
9 use Friendica\App;
10 use Friendica\Content\Widget;
11 use Friendica\Core\Addon;
12 use Friendica\Core\Config;
13 use Friendica\Core\L10n;
14 use Friendica\Core\PConfig;
15 use Friendica\Core\System;
16 use Friendica\Database\DBM;
17 use Friendica\Model\Profile;
18 use Friendica\Object\Image;
19
20 $frio = "view/theme/frio";
21
22 global $frio;
23
24 function frio_init(App $a)
25 {
26         // disable the events module link in the profile tab
27         $a->theme_events_in_profile = false;
28         $a->videowidth = 622;
29
30         $a->set_template_engine('smarty3');
31
32         $baseurl = System::baseUrl();
33
34         $style = PConfig::get(local_user(), 'frio', 'style');
35
36         $frio = "view/theme/frio";
37
38         global $frio;
39
40         // if the device is a mobile device set js is_mobile
41         // variable so the js scripts can use this information
42         if ($a->is_mobile || $a->is_tablet) {
43                 $a->page["htmlhead"] .= <<< EOT
44                         <script type="text/javascript">
45                                 var is_mobile = 1;
46                         </script>
47 EOT;
48         }
49
50         if ($style == "") {
51                 $style = Config::get('frio', 'style');
52         }
53 }
54
55 function frio_install()
56 {
57         Addon::registerHook('prepare_body_final', 'view/theme/frio/theme.php', 'frio_item_photo_links');
58         Addon::registerHook('item_photo_menu', 'view/theme/frio/theme.php', 'frio_item_photo_menu');
59         Addon::registerHook('contact_photo_menu', 'view/theme/frio/theme.php', 'frio_contact_photo_menu');
60         Addon::registerHook('nav_info', 'view/theme/frio/theme.php', 'frio_remote_nav');
61         Addon::registerHook('acl_lookup_end', 'view/theme/frio/theme.php', 'frio_acl_lookup');
62         Addon::registerHook('display_item', 'view/theme/frio/theme.php', 'frio_display_item');
63
64         logger("installed theme frio");
65 }
66
67 function frio_uninstall()
68 {
69         Addon::unregisterHook('prepare_body_final', 'view/theme/frio/theme.php', 'frio_item_photo_links');
70         Addon::unregisterHook('item_photo_menu', 'view/theme/frio/theme.php', 'frio_item_photo_menu');
71         Addon::unregisterHook('contact_photo_menu', 'view/theme/frio/theme.php', 'frio_contact_photo_menu');
72         Addon::unregisterHook('nav_info', 'view/theme/frio/theme.php', 'frio_remote_nav');
73         Addon::unregisterHook('acl_lookup_end', 'view/theme/frio/theme.php', 'frio_acl_lookup');
74         Addon::unregisterHook('display_item', 'view/theme/frio/theme.php', 'frio_display_item');
75
76         logger("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         $phototypes = Image::supportedTypes();
94         $occurence = 1;
95         $p = bb_find_open_close($body_info['html'], "<a", ">");
96
97         while ($p !== false && ($occurence++ < 500)) {
98                 $link = substr($body_info['html'], $p['start'], $p['end'] - $p['start']);
99                 $matches = [];
100
101                 preg_match("/\/photos\/[\w]+\/image\/([\w]+)/", $link, $matches);
102                 if ($matches) {
103                         // Replace the link for the photo's page with a direct link to the photo itself
104                         $newlink = str_replace($matches[0], "/photo/{$matches[1]}", $link);
105
106                         // Add a "quiet" parameter to any redir links to prevent the "XX welcomes YY" info boxes
107                         $newlink = preg_replace("/href=\"([^\"]+)\/redir\/([^\"]+)&url=([^\"]+)\"/", 'href="$1/redir/$2&quiet=1&url=$3"', $newlink);
108
109                         // Having any arguments to the link for Colorbox causes it to fetch base64 code instead of the image
110                         $newlink = preg_replace("/\/[?&]zrl=([^&\"]+)/", '', $newlink);
111
112                         $body_info['html'] = str_replace($link, $newlink, $body_info['html']);
113                 }
114
115                 $p = bb_find_open_close($body_info['html'], "<a", ">", $occurence);
116         }
117 }
118
119 /**
120  * @brief Replace links of the item_photo_menu hook
121  *
122  *  This function replaces the original poke and the message links
123  *  to call the addToModal javascript function so this pages can
124  *  be loaded in a bootstrap modal
125  *
126  * @param App $a Unused but required by the hook definition
127  * @param array $arr Contains item data and the original photo_menu
128  */
129 function frio_item_photo_menu(App $a, &$arr)
130 {
131         foreach ($arr["menu"] as $k => $v) {
132                 if (strpos($v, 'poke/?f=&c=') === 0 || strpos($v, 'message/new/') === 0) {
133                         $v = "javascript:addToModal('" . $v . "'); return false;";
134                         $arr["menu"][$k] = $v;
135                 }
136         }
137 }
138
139 /**
140  * @brief Replace links of the contact_photo_menu
141  *
142  *  This function replaces the original poke and the message links
143  *  to call the addToModal javascript function so this pages can
144  *  be loaded in a bootstrap modal
145  *  Additionally the profile, status and photo page links  will be changed
146  *  to don't open in a new tab if the contact is a friendica contact.
147  *
148  * @param App $a The app data
149  * @param array $args Contains contact data and the original photo_menu
150  */
151 function frio_contact_photo_menu(App $a, &$args)
152 {
153         $cid = $args["contact"]["id"];
154         $pokelink = $args["menu"]["poke"][1];
155         $pmlink = $args["menu"]["pm"][1];
156
157         // Set the the indicator for opening the status, profile and photo pages
158         // in a new tab to false if the contact a dfrn (friendica) contact
159         // We do this because we can go back on foreign friendica pages throuhg
160         // friendicas "magic-link" which indicates a friendica user on froreign
161         // friendica servers as remote user or visitor
162         //
163         // The value for opening in a new tab is e.g. when
164         // $args["menu"]["status"][2] is true. If the value of the [2] key is true
165         // and if it's a friendica contact we set it to false
166         foreach ($args["menu"] as $k => $v) {
167                 if ($k === "status" || $k === "profile" || $k === "photos") {
168                         $v[2] = (($args["contact"]["network"] === "dfrn") ? false : true);
169                         $args["menu"][$k][2] = $v[2];
170                 }
171         }
172
173         // Add to pm and poke links a new key with the value 'modal'.
174         // Later we can make conditions in the corresponing templates (e.g.
175         // contact_template.tpl)
176         if (strpos($pokelink, 'poke/?f=&c=' . $cid) !== false) {
177                 $args["menu"]["poke"][3] = "modal";
178         }
179
180         if (strpos($pmlink, 'message/new/' . $cid) !== false) {
181                 $args["menu"]["pm"][3] = "modal";
182         }
183 }
184
185 /**
186  * @brief Construct remote nav menu
187  *
188  *  It creates a remote baseurl form $_SESSION for remote users and friendica
189  *  visitors. This url will be added to some of the nav links. With this behaviour
190  *  the user will come back to her/his own pages on his/her friendica server.
191  *  Not all possible links are available (notifications, administrator, manage,
192  *  notes aren't available because we have no way the check remote permissions)..
193  *  Some links will point to the local pages because the user would expect
194  *  local page (these pages are: search, community, help, apps, directory).
195  *
196  * @param App $a The App class
197  * @param array $nav The original nav menu
198  */
199 function frio_remote_nav($a, &$nav)
200 {
201         // get the homelink from $_XSESSION
202         $homelink = Profile::getMyURL();
203         if (!$homelink) {
204                 $homelink = defaults($_SESSION, 'visitor_home', '');
205         }
206
207         // split up the url in it's parts (protocol,domain/directory, /profile/, nickname
208         // I'm not familiar with regex, so someone might find a better solutionen
209         //
210         // E.g $homelink = 'https://friendica.domain.com/profile/mickey' should result in an array
211         // with 0 => 'https://friendica.domain.com/profile/mickey' 1 => 'https://',
212         // 2 => 'friendica.domain.com' 3 => '/profile/' 4 => 'mickey'
213         //
214         //$server_url = preg_match('/^(https?:\/\/.*?)\/profile\//2', $homelink);
215         preg_match('/^(https?:\/\/)?(.*?)(\/profile\/)(.*)/', $homelink, $url_parts);
216
217         // Construct the server url of the visitor. So we could link back to his/her own menu.
218         // And construct a webbie (e.g. mickey@friendica.domain.com for the search in gcontact
219         // We use the webbie for search in gcontact because we don't know if gcontact table stores
220         // the right value if its http or https protocol
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'] = (DBM::is_result($r) ? $a->remove_baseurl($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 (Profile::getMyURL()) {
240                 $r = q("SELECT `name`, `nick`, `photo` FROM `gcontact`
241                                 WHERE `addr` = '%s' AND `network` = 'dfrn'",
242                         dbesc($webbie));
243                 $nav['remote'] = L10n::t("Visitor");
244         } else {
245                 $r = false;
246         }
247
248         if (DBM::is_result($r)) {
249                 $nav['userinfo'] = [
250                         'icon' => (DBM::is_result($r) ? $r[0]['photo'] : "images/person-48.jpg"),
251                         'name' => $r[0]['name'],
252                 ];
253         }
254
255         if (!local_user() && !empty($server_url)) {
256                 $nav['logout'] = [$server_url . '/logout', L10n::t('Logout'), "", L10n::t('End this session')];
257
258                 // user menu
259                 $nav['usermenu'][] = [$server_url . '/profile/' . $a->user['nickname'], L10n::t('Status'), "", L10n::t('Your posts and conversations')];
260                 $nav['usermenu'][] = [$server_url . '/profile/' . $a->user['nickname'] . '?tab=profile', L10n::t('Profile'), "", L10n::t('Your profile page')];
261                 $nav['usermenu'][] = [$server_url . '/photos/' . $a->user['nickname'], L10n::t('Photos'), "", L10n::t('Your photos')];
262                 $nav['usermenu'][] = [$server_url . '/videos/' . $a->user['nickname'], 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 . '/contacts', L10n::t('Contacts'), "", L10n::t('Manage/edit friends and contacts')];
271                 $nav['sitename'] = $a->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 /mod/contacts.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         require_once 'mod/contacts.php';
291
292         $nets = x($_GET, "nets") ? notags(trim($_GET["nets"])) : "";
293
294         // we introduce a new search type, r should do the same query like it's
295         // done in /mod/contacts for connections
296         if ($results["type"] !== "r") {
297                 return;
298         }
299
300         $searching = false;
301         if ($results["search"]) {
302                 $search_txt = dbesc(protect_sprintf(preg_quote($results["search"])));
303                 $searching = true;
304         }
305         
306         $sql_extra = '';
307         if ($searching) {
308                 $sql_extra .= " AND (`attag` LIKE '%%" . dbesc($search_txt) . "%%' OR `name` LIKE '%%" . dbesc($search_txt) . "%%' OR `nick` LIKE '%%" . dbesc($search_txt) . "%%') ";
309         }
310
311         if ($nets) {
312                 $sql_extra .= sprintf(" AND network = '%s' ", dbesc($nets));
313         }
314
315         $r = q("SELECT COUNT(*) AS `total` FROM `contact`
316                 WHERE `uid` = %d AND NOT `self` AND NOT `pending` $sql_extra ", intval($_SESSION['uid']));
317         if (DBM::is_result($r)) {
318                 $total = $r[0]["total"];
319         }
320
321         $sql_extra3 = Widget::unavailableNetworks();
322
323         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `self` AND NOT `pending` $sql_extra $sql_extra3 ORDER BY `name` ASC LIMIT %d, %d ",
324                 intval($_SESSION['uid']), intval($results['start']), intval($results['count'])
325         );
326
327         $contacts = [];
328
329         if (DBM::is_result($r)) {
330                 foreach ($r as $rr) {
331                         $contacts[] = _contact_detail_for_template($rr);
332                 }
333         }
334
335         $results["items"] = $contacts;
336         $results["tot"] = $total;
337 }
338
339 /**
340  * @brief Manipulate the data of the item
341  *
342  * At the moment we use this function to add some own stuff to the item menu
343  *
344  * @param App $a App $a The app data
345  * @param array $arr Array with the item and the item actions<br>
346  *     'item' => Array with item data<br>
347  *     'output' => Array with item actions<br>
348  */
349 function frio_display_item(App $a, &$arr)
350 {
351         // Add subthread to the item menu
352         $subthread = [];
353         if (local_user() == $arr['item']['uid'] && $arr['item']['parent'] == $arr['item']['id'] && !$arr['item']['self']) {
354                 $subthread = [
355                         'menu'   => 'follow_thread',
356                         'title'  => L10n::t('Follow Thread'),
357                         'action' => 'dosubthread(' . $arr['item']['id'] . '); return false;',
358                         'href'   => '#'
359                 ];
360         }
361         $arr['output']['subthread'] = $subthread;
362 }