]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/theme.php
b135fa0d6604edc3ae56ec23f04ca4923fd9245e
[friendica.git] / view / theme / frio / theme.php
1 <?php
2
3 /*
4  * Name: frio
5  * 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>.
6  * Version: V.0.8
7  * Author: Rabuzarus <https://friendica.kommune4.de/profile/rabuzarus>
8  *
9  */
10
11 use Friendica\App;
12 use Friendica\Core\Config;
13 use Friendica\Core\PConfig;
14 use Friendica\Core\System;
15 use Friendica\Database\DBM;
16 use Friendica\Model\Profile;
17 use Friendica\Object\Image;
18
19 $frio = "view/theme/frio";
20
21 global $frio;
22
23 function frio_init(App $a)
24 {
25         // disable the events module link in the profile tab
26         $a->theme_events_in_profile = false;
27         $a->videowidth = 622;
28
29         $a->set_template_engine('smarty3');
30
31         $baseurl = System::baseUrl();
32
33         $style = PConfig::get(local_user(), 'frio', 'style');
34
35         $frio = "view/theme/frio";
36
37         global $frio;
38
39         // if the device is a mobile device set js is_mobile
40         // variable so the js scripts can use this information
41         if ($a->is_mobile || $a->is_tablet) {
42                 $a->page["htmlhead"] .= <<< EOT
43                         <script type="text/javascript">
44                                 var is_mobile = 1;
45                         </script>
46 EOT;
47         }
48
49         if ($style == "") {
50                 $style = Config::get('frio', 'style');
51         }
52 }
53
54 function frio_install()
55 {
56         register_hook('prepare_body_final', 'view/theme/frio/theme.php', 'frio_item_photo_links');
57         register_hook('item_photo_menu', 'view/theme/frio/theme.php', 'frio_item_photo_menu');
58         register_hook('contact_photo_menu', 'view/theme/frio/theme.php', 'frio_contact_photo_menu');
59         register_hook('nav_info', 'view/theme/frio/theme.php', 'frio_remote_nav');
60         register_hook('acl_lookup_end', 'view/theme/frio/theme.php', 'frio_acl_lookup');
61         register_hook('display_item', 'view/theme/frio/theme.php', 'frio_display_item');
62
63         logger("installed theme frio");
64 }
65
66 function frio_uninstall()
67 {
68         unregister_hook('prepare_body_final', 'view/theme/frio/theme.php', 'frio_item_photo_links');
69         unregister_hook('item_photo_menu', 'view/theme/frio/theme.php', 'frio_item_photo_menu');
70         unregister_hook('contact_photo_menu', 'view/theme/frio/theme.php', 'frio_contact_photo_menu');
71         unregister_hook('nav_info', 'view/theme/frio/theme.php', 'frio_remote_nav');
72         unregister_hook('acl_lookup_end', 'view/theme/frio/theme.php', 'frio_acl_lookup');
73         unregister_hook('display_item', 'view/theme/frio/theme.php', 'frio_display_item');
74
75         logger("uninstalled theme frio");
76 }
77
78 /**
79  * @brief Replace friendica photo links hook
80  *
81  *  This function does replace the links to photos
82  *  of other friendica users. Original the photos are
83  *  linked to the photo page. Now they will linked directly
84  *  to the photo file. This function is nessesary to use colorbox
85  *  in the network stream
86  *
87  * @param App $a Unused but required by hook definition
88  * @param array $body_info The item and its html output
89  */
90 function frio_item_photo_links(App $a, &$body_info)
91 {
92         $phototypes = Image::supportedTypes();
93         $occurence = 1;
94         $p = bb_find_open_close($body_info['html'], "<a", ">");
95
96         while ($p !== false && ($occurence++ < 500)) {
97                 $link = substr($body_info['html'], $p['start'], $p['end'] - $p['start']);
98                 $matches = array();
99
100                 preg_match("/\/photos\/[\w]+\/image\/([\w]+)/", $link, $matches);
101                 if ($matches) {
102                         // Replace the link for the photo's page with a direct link to the photo itself
103                         $newlink = str_replace($matches[0], "/photo/{$matches[1]}", $link);
104
105                         // Add a "quiet" parameter to any redir links to prevent the "XX welcomes YY" info boxes
106                         $newlink = preg_replace("/href=\"([^\"]+)\/redir\/([^\"]+)&url=([^\"]+)\"/", 'href="$1/redir/$2&quiet=1&url=$3"', $newlink);
107
108                         // Having any arguments to the link for Colorbox causes it to fetch base64 code instead of the image
109                         $newlink = preg_replace("/\/[?&]zrl=([^&\"]+)/", '', $newlink);
110
111                         $body_info['html'] = str_replace($link, $newlink, $body_info['html']);
112                 }
113
114                 $p = bb_find_open_close($body_info['html'], "<a", ">", $occurence);
115         }
116 }
117
118 /**
119  * @brief Replace links of the item_photo_menu hook
120  *
121  *  This function replaces the original poke and the message links
122  *  to call the addToModal javascript function so this pages can
123  *  be loaded in a bootstrap modal
124  *
125  * @param App $a Unused but required by the hook definition
126  * @param array $arr Contains item data and the original photo_menu
127  */
128 function frio_item_photo_menu(App $a, &$arr)
129 {
130         foreach ($arr["menu"] as $k => $v) {
131                 if (strpos($v, 'poke/?f=&c=') === 0 || strpos($v, 'message/new/') === 0) {
132                         $v = "javascript:addToModal('" . $v . "'); return false;";
133                         $arr["menu"][$k] = $v;
134                 }
135         }
136 }
137
138 /**
139  * @brief Replace links of the contact_photo_menu
140  *
141  *  This function replaces the original poke and the message links
142  *  to call the addToModal javascript function so this pages can
143  *  be loaded in a bootstrap modal
144  *  Additionally the profile, status and photo page links  will be changed
145  *  to don't open in a new tab if the contact is a friendica contact.
146  *
147  * @param App $a The app data
148  * @param array $args Contains contact data and the original photo_menu
149  */
150 function frio_contact_photo_menu(App $a, &$args)
151 {
152         $cid = $args["contact"]["id"];
153         $pokelink = $args["menu"]["poke"][1];
154         $pmlink = $args["menu"]["pm"][1];
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 = Profile::getMyURL();
202         if (!$homelink) {
203                 $homelink = defaults($_SESSION, '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         if (count($url_parts)) {
221                 $server_url = $url_parts[1] . $url_parts[2];
222                 $webbie = $url_parts[4] . '@' . $url_parts[2];
223         }
224
225         // since $userinfo isn't available for the hook we write it to the nav array
226         // this isn't optimal because the contact query will be done now twice
227         if (local_user()) {
228                 // empty the server url for local user because we won't need it
229                 $server_url = '';
230                 // user info
231                 $r = q("SELECT `micro` FROM `contact` WHERE `uid` = %d AND `self`", intval($a->user['uid']));
232
233                 $r[0]['photo'] = (DBM::is_result($r) ? $a->remove_baseurl($r[0]['micro']) : "images/person-48.jpg");
234                 $r[0]['name'] = $a->user['username'];
235         } elseif (!local_user() && remote_user()) {
236                 $r = q("SELECT `name`, `nick`, `micro` AS `photo` FROM `contact` WHERE `id` = %d", intval(remote_user()));
237                 $nav['remote'] = t("Guest");
238         } elseif (Profile::getMyURL()) {
239                 $r = q("SELECT `name`, `nick`, `photo` FROM `gcontact`
240                                 WHERE `addr` = '%s' AND `network` = 'dfrn'",
241                         dbesc($webbie));
242                 $nav['remote'] = t("Visitor");
243         } else {
244                 $r = false;
245         }
246
247         if (DBM::is_result($r)) {
248                 $nav['userinfo'] = array(
249                         'icon' => (DBM::is_result($r) ? $r[0]['photo'] : "images/person-48.jpg"),
250                         'name' => $r[0]['name'],
251                 );
252         }
253
254         if (!local_user() && !empty($server_url)) {
255                 $nav['logout'] = Array($server_url . '/logout', t('Logout'), "", t('End this session'));
256
257                 // user menu
258                 $nav['usermenu'][] = Array($server_url . '/profile/' . $a->user['nickname'], t('Status'), "", t('Your posts and conversations'));
259                 $nav['usermenu'][] = Array($server_url . '/profile/' . $a->user['nickname'] . '?tab=profile', t('Profile'), "", t('Your profile page'));
260                 $nav['usermenu'][] = Array($server_url . '/photos/' . $a->user['nickname'], t('Photos'), "", t('Your photos'));
261                 $nav['usermenu'][] = Array($server_url . '/videos/' . $a->user['nickname'], t('Videos'), "", t('Your videos'));
262                 $nav['usermenu'][] = Array($server_url . '/events/', t('Events'), "", t('Your events'));
263
264                 // navbar links
265                 $nav['network'] = array($server_url . '/network', t('Network'), "", t('Conversations from your friends'));
266                 $nav['events'] = Array($server_url . '/events', t('Events'), "", t('Events and Calendar'));
267                 $nav['messages'] = array($server_url . '/message', t('Messages'), "", t('Private mail'));
268                 $nav['settings'] = array($server_url . '/settings', t('Settings'), "", t('Account settings'));
269                 $nav['contacts'] = array($server_url . '/contacts', t('Contacts'), "", t('Manage/edit friends and contacts'));
270                 $nav['sitename'] = $a->config['sitename'];
271         }
272 }
273
274 /**
275  * @brief: Search for contacts
276  *
277  * This function search for a users contacts. The code is copied from contact search
278  * in /mod/contacts.php. With this function the contacts will permitted to acl_lookup()
279  * and can grabbed as json. For this we use the type="r". This is usful to to let js
280  * grab the contact data.
281  * We use this to give the data to textcomplete and have a filter function at the
282  * contact page.
283  *
284  * @param App $a The app data @TODO Unused
285  * @param array $results The array with the originals from acl_lookup()
286  */
287 function frio_acl_lookup(App $a, &$results)
288 {
289         require_once 'mod/contacts.php';
290
291         $nets = x($_GET, "nets") ? notags(trim($_GET["nets"])) : "";
292
293         // we introduce a new search type, r should do the same query like it's
294         // done in /mod/contacts for connections
295         if ($results["type"] == "r") {
296                 $searching = false;
297                 if ($search) {
298                         $search_hdr = $search;
299                         $search_txt = dbesc(protect_sprintf(preg_quote($search)));
300                         $searching = true;
301                 }
302                 $sql_extra = '';
303                 if ($searching) {
304                         $sql_extra .= " AND (`attag` LIKE '%%" . dbesc($search_txt) . "%%' OR `name` LIKE '%%" . dbesc($search_txt) . "%%' OR `nick` LIKE '%%" . dbesc($search_txt) . "%%') ";
305                 }
306
307                 if ($nets) {
308                         $sql_extra .= sprintf(" AND network = '%s' ", dbesc($nets));
309                 }
310
311                 $r = q("SELECT COUNT(*) AS `total` FROM `contact`
312                         WHERE `uid` = %d AND NOT `self` AND NOT `pending` $sql_extra ",
313                         intval($_SESSION['uid']));
314                 if (DBM::is_result($r)) {
315                         $total = $r[0]["total"];
316                 }
317
318                 $sql_extra3 = unavailable_networks();
319
320                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `self` AND NOT `pending` $sql_extra $sql_extra3 ORDER BY `name` ASC LIMIT 100 ",
321                         intval($_SESSION['uid'])
322                 );
323
324                 $contacts = array();
325
326                 if (DBM::is_result($r)) {
327                         foreach ($r as $rr) {
328                                 $contacts[] = _contact_detail_for_template($rr);
329                         }
330                 }
331
332                 $results["items"] = $contacts;
333                 $results["tot"] = $total;
334         }
335 }
336
337 /**
338  * @brief Manipulate the data of the item
339  *
340  * At the moment we use this function to add some own stuff to the item menu
341  *
342  * @param App $a App $a The app data
343  * @param array $arr Array with the item and the item actions<br>
344  *     'item' => Array with item data<br>
345  *     'output' => Array with item actions<br>
346  */
347 function frio_display_item(App $a, &$arr)
348 {
349         // Add subthread to the item menu
350         $subthread = array();
351         if (local_user() == $arr['item']['uid'] && $arr['item']['parent'] == $arr['item']['id'] && !$arr['item']['self']) {
352                 $subthread = array(
353                         'menu'   => 'follow_thread',
354                         'title'  => t('Follow Thread'),
355                         'action' => 'dosubthread(' . $arr['item']['id'] . '); return false;',
356                         'href'   => '#'
357                 );
358         }
359         $arr['output']['subthread'] = $subthread;
360 }