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