]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/theme.php
App::get_baseurl is now replaced with System::baseUrl
[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
10 use Friendica\App;
11 use Friendica\Core\System;
12
13 $frio = "view/theme/frio";
14
15 global $frio;
16
17 function frio_init(App $a) {
18
19         // disable the events module link in the profile tab
20         $a->theme_events_in_profile = false;
21
22         set_template_engine($a, 'smarty3');
23
24         $baseurl = System::baseUrl();
25
26         $style = get_pconfig(local_user(), 'frio', 'style');
27
28         $frio = "view/theme/frio";
29
30         global $frio;
31
32         // if the device is a mobile device set js is_mobile
33         // variable so the js scripts can use this information
34         if($a->is_mobile || $a->is_tablet) {
35                 $a->page["htmlhead"] .= <<< EOT
36                         <script>
37                                 var is_mobile = 1;
38                         </script>
39 EOT;
40                         }
41
42         if ($style == "")
43                 $style = get_config('frio', 'style');
44 }
45
46 function frio_install() {
47         register_hook('prepare_body_final', 'view/theme/frio/theme.php', 'frio_item_photo_links');
48         register_hook('item_photo_menu', 'view/theme/frio/theme.php', 'frio_item_photo_menu');
49         register_hook('contact_photo_menu', 'view/theme/frio/theme.php', 'frio_contact_photo_menu');
50         register_hook('nav_info', 'view/theme/frio/theme.php', 'frio_remote_nav');
51         register_hook('acl_lookup_end', 'view/theme/frio/theme.php', 'frio_acl_lookup');
52         register_hook('display_item', 'view/theme/frio/theme.php', 'frio_display_item');
53
54         logger("installed theme frio");
55 }
56
57 function frio_uninstall() {
58         unregister_hook('prepare_body_final', 'view/theme/frio/theme.php', 'frio_item_photo_links');
59         unregister_hook('item_photo_menu', 'view/theme/frio/theme.php', 'frio_item_photo_menu');
60         unregister_hook('contact_photo_menu', 'view/theme/frio/theme.php', 'frio_contact_photo_menu');
61         unregister_hook('nav_info', 'view/theme/frio/theme.php', 'frio_remote_nav');
62         unregister_hook('acl_lookup_end', 'view/theme/frio/theme.php', 'frio_acl_lookup');
63         unregister_hook('display_item', 'view/theme/frio/theme.php', 'frio_display_item');
64
65         logger("uninstalled theme frio");
66 }
67 /**
68  * @brief Replace friendica photo links hook
69  *
70  *  This function does replace the links to photos
71  *  of other friendica users. Original the photos are
72  *  linked to the photo page. Now they will linked directly
73  *  to the photo file. This function is nessesary to use colorbox
74  *  in the network stream
75  *
76  * @param App $a Unused but required by hook definition
77  * @param array $body_info The item and its html output
78  */
79 function frio_item_photo_links(App $a, &$body_info) {
80         require_once('include/Photo.php');
81
82         $phototypes = Photo::supportedTypes();
83         $occurence = 1;
84         $p = bb_find_open_close($body_info['html'], "<a", ">");
85
86         while($p !== false && ($occurence++ < 500)) {
87                 $link = substr($body_info['html'], $p['start'], $p['end'] - $p['start']);
88                 $matches = array();
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 = bb_find_open_close($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         $args = array('item' => $item, 'menu' => $menu);
127 }
128
129 /**
130  * @brief Replace links of the contact_photo_menu
131  *
132  *  This function replaces the original poke and the message links
133  *  to call the addToModal javascript function so this pages can
134  *  be loaded in a bootstrap modal
135  *  Additionally the profile, status and photo page links  will be changed
136  *  to don't open in a new tab if the contact is a friendica contact.
137  *
138  * @param App $a The app data
139  * @param array $args Contains contact data and the original photo_menu
140  */
141 function frio_contact_photo_menu(App $a, &$args){
142
143         $pokelink = "";
144         $pmlink = "";
145         $cid = "";
146
147         $cid = $args["contact"]["id"];
148         $pokelink = $args["menu"]["poke"][1];
149         $pmlink = $args["menu"]["pm"][1];
150
151         // Set the the indicator for opening the status, profile and photo pages
152         // in a new tab to false if the contact a dfrn (friendica) contact
153         // We do this because we can go back on foreign friendica pages throuhg
154         // friendicas "magic-link" which indicates a friendica user on froreign
155         // friendica servers as remote user or visitor
156         //
157         // The value for opening in a new tab is e.g. when
158         // $args["menu"]["status"][2] is true. If the value of the [2] key is true
159         // and if it's a friendica contact we set it to false
160         foreach($args["menu"] as $k =>$v) {
161                 if($k === "status" || $k === "profile" || $k === "photos") {
162                         $v[2] = (($args["contact"]["network"] === "dfrn") ? false : true);
163                         $args["menu"][$k][2] = $v[2];
164                 }
165         }
166
167         // Add to pm and poke links a new key with the value 'modal'.
168         // Later we can make conditions in the corresponing templates (e.g.
169         // contact_template.tpl)
170         if(strpos($pokelink,'poke/?f=&c='. $cid) !== false)
171                 $args["menu"]["poke"][3] = "modal";
172
173         if(strpos($pmlink,'message/new/' . $cid) !== false)
174                 $args["menu"]["pm"][3] = "modal";
175
176         $args = array('contact' => $contact, 'menu' => &$menu);
177 }
178
179 /**
180  * @brief Construct remote nav menu
181  *
182  *  It creates a remote baseurl form $_SESSION for remote users and friendica
183  *  visitors. This url will be added to some of the nav links. With this behaviour
184  *  the user will come back to her/his own pages on his/her friendica server.
185  *  Not all possible links are available (notifications, administrator, manage,
186  *  notes aren't available because we have no way the check remote permissions)..
187  *  Some links will point to the local pages because the user would expect
188  *  local page (these pages are: search, community, help, apps, directory).
189  *
190  * @param App $a The App class
191  * @param array $nav The original nav menu
192  */
193 function frio_remote_nav($a,&$nav) {
194         // get the homelink from $_XSESSION
195         $homelink = get_my_url();
196         if(! $homelink)
197                 $homelink = ((x($_SESSION,'visitor_home')) ? $_SESSION['visitor_home'] : '');
198
199         // split up the url in it's parts (protocol,domain/directory, /profile/, nickname
200         // I'm not familiar with regex, so someone might find a better solutionen
201         //
202         // E.g $homelink = 'https://friendica.domain.com/profile/mickey' should result in an array
203         // with 0 => 'https://friendica.domain.com/profile/mickey' 1 => 'https://',
204         // 2 => 'friendica.domain.com' 3 => '/profile/' 4 => 'mickey'
205         //
206         //$server_url = preg_match('/^(https?:\/\/.*?)\/profile\//2', $homelink);
207         preg_match('/^(https?:\/\/)?(.*?)(\/profile\/)(.*)/', $homelink, $url_parts);
208
209         // Construct the server url of the visitor. So we could link back to his/her own menu.
210         // And construct a webbie (e.g. mickey@friendica.domain.com for the search in gcontact
211         // We use the webbie for search in gcontact because we don't know if gcontact table stores
212         // the right value if its http or https protocol
213         if(count($url_parts)) {
214                 $server_url = $url_parts[1] . $url_parts[2];
215                 $webbie = $url_parts[4] . '@' . $url_parts[2];
216         }
217
218         // since $userinfo isn't available for the hook we write it to the nav array
219         // this isn't optimal because the contact query will be done now twice
220         if (local_user()) {
221                 // empty the server url for local user because we won't need it
222                 $server_url = '';
223                 // user info
224                 $r = q("SELECT `micro` FROM `contact` WHERE `uid` = %d AND `self`", intval($a->user['uid']));
225
226                 $r[0]['photo'] = (dbm::is_result($r) ? $a->remove_baseurl($r[0]['micro']) : "images/person-48.jpg");
227                 $r[0]['name'] = $a->user['username'];
228
229         } elseif (!local_user() && remote_user()) {
230                 $r = q("SELECT `name`, `nick`, `micro` AS `photo` FROM `contact` WHERE `id` = %d", intval(remote_user()));
231                 $nav['remote'] = t("Guest");
232
233         } elseif (get_my_url()) {
234                 $r = q("SELECT `name`, `nick`, `photo` FROM `gcontact`
235                                 WHERE `addr` = '%s' AND `network` = 'dfrn'",
236                         dbesc($webbie));
237                 $nav['remote'] = t("Visitor");
238         } else {
239                 $r = false;
240         }
241
242         if (dbm::is_result($r)) {
243                         $nav['userinfo'] = array(
244                                 'icon' => (dbm::is_result($r) ? $r[0]['photo'] : "images/person-48.jpg"),
245                                 'name' => $r[0]['name'],
246                         );
247                 }
248
249         if (!local_user() && !empty($server_url)) {
250                 $nav['logout'] = Array($server_url . '/logout', t('Logout'), "", t('End this session'));
251
252                 // user menu
253                 $nav['usermenu'][] = Array($server_url . '/profile/' . $a->user['nickname'], t('Status'), "", t('Your posts and conversations'));
254                 $nav['usermenu'][] = Array($server_url . '/profile/' . $a->user['nickname']. '?tab=profile', t('Profile'), "", t('Your profile page'));
255                 $nav['usermenu'][] = Array($server_url . '/photos/' . $a->user['nickname'], t('Photos'), "", t('Your photos'));
256                 $nav['usermenu'][] = Array($server_url . '/videos/' . $a->user['nickname'], t('Videos'), "", t('Your videos'));
257                 $nav['usermenu'][] = Array($server_url . '/events/', t('Events'), "", t('Your events'));
258
259                 // navbar links
260                 $nav['network'] = array($server_url . '/network', t('Network'), "", t('Conversations from your friends'));
261                 $nav['events'] = Array($server_url . '/events', t('Events'), "", t('Events and Calendar'));
262                 $nav['messages'] = array($server_url . '/message', t('Messages'), "", t('Private mail'));
263                 $nav['settings'] = array($server_url . '/settings', t('Settings'),"", t('Account settings'));
264                 $nav['contacts'] = array($server_url . '/contacts', t('Contacts'),"", t('Manage/edit friends and contacts'));
265                 $nav['sitename'] = $a->config['sitename'];
266         }
267 }
268 /**
269  * @brief: Search for contacts
270  *
271  * This function search for a users contacts. The code is copied from contact search
272  * in /mod/contacts.php. With this function the contacts will permitted to acl_lookup()
273  * and can grabbed as json. For this we use the type="r". This is usful to to let js
274  * grab the contact data.
275  * We use this to give the data to textcomplete and have a filter function at the
276  * contact page.
277  *
278  * @param App $a The app data @TODO Unused
279  * @param array $results The array with the originals from acl_lookup()
280  */
281 function frio_acl_lookup(App $a, &$results) {
282         require_once("mod/contacts.php");
283
284         $nets = ((x($_GET,"nets")) ? notags(trim($_GET["nets"])) : "");
285
286         // we introduce a new search type, r should do the same query like it's
287         // done in /mod/contacts for connections
288         if ($results["type"] == "r") {
289                 $searching = false;
290                 if ($search) {
291                         $search_hdr = $search;
292                         $search_txt = dbesc(protect_sprintf(preg_quote($search)));
293                         $searching = true;
294                 }
295                 $sql_extra .= (($searching) ? " AND (`attag` LIKE '%%".dbesc($search_txt)."%%' OR `name` LIKE '%%".dbesc($search_txt)."%%' OR `nick` LIKE '%%".dbesc($search_txt)."%%') " : "");
296
297                 if ($nets) {
298                         $sql_extra .= sprintf(" AND network = '%s' ", dbesc($nets));
299                 }
300
301                 $sql_extra2 = ((($sort_type > 0) && ($sort_type <= CONTACT_IS_FRIEND)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : '');
302
303
304                 $r = q("SELECT COUNT(*) AS `total` FROM `contact`
305                         WHERE `uid` = %d AND NOT `self` AND NOT `pending` $sql_extra $sql_extra2 ",
306                         intval($_SESSION['uid']));
307                 if (dbm::is_result($r)) {
308                         $total = $r[0]["total"];
309                 }
310
311                 $sql_extra3 = unavailable_networks();
312
313                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `self` AND NOT `pending` $sql_extra $sql_extra2 $sql_extra3 ORDER BY `name` ASC LIMIT 100 ",
314                         intval($_SESSION['uid'])
315                 );
316
317                 $contacts = array();
318
319                 if (dbm::is_result($r)) {
320                         foreach ($r as $rr) {
321                                 $contacts[] = _contact_detail_for_template($rr);
322                         }
323                 }
324
325                 $results["items"] = $contacts;
326                 $results["tot"] = $total;
327         }
328 }
329
330 /**
331  * @brief Manipulate the data of the item
332  * 
333  * At the moment we use this function to add some own stuff to the item menu
334  * 
335  * @param App $a App $a The app data
336  * @param array $arr Array with the item and the item actions<br>
337  *     'item' => Array with item data<br>
338  *     'output' => Array with item actions<br>
339  */
340 function frio_display_item(App $a,&$arr) {
341
342         // Add subthread to the item menu
343         $subthread = array();
344         if ((local_user()) && local_user() == $arr['item']['uid'] && $arr['item']['parent'] == $arr['item']['id'] && (! $arr['item']['self'])) {
345                 $subthread = array(
346                         'menu'   => 'follow_thread',
347                         'title'  => t('Follow Thread'),
348                         'action' => 'dosubthread(' . $arr['item']['id'] . '); return false;',
349                         'href'   => '#'
350                 );
351         }
352         $arr['output']['subthread'] = $subthread;
353 }