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