]> git.mxchange.org Git - friendica.git/blob - view/theme/frio/theme.php
Avoid memory issue in exception
[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.5
6  * Author: Rabuzarus <https://friendica.kommune4.de/profile/rabuzarus>
7  *
8  */
9
10 use Friendica\App;
11 use Friendica\Content\Text\Plaintext;
12 use Friendica\Content\Widget;
13 use Friendica\Core\Config;
14 use Friendica\Core\Hook;
15 use Friendica\Core\L10n;
16 use Friendica\Core\Logger;
17 use Friendica\Core\PConfig;
18 use Friendica\Core\Renderer;
19 use Friendica\Core\System;
20 use Friendica\Database\DBA;
21 use Friendica\Model;
22 use Friendica\Module;
23 use Friendica\Util\Strings;
24
25 function frio_init(App $a)
26 {
27         global $frio;
28         $frio = 'view/theme/frio';
29
30         // disable the events module link in the profile tab
31         $a->theme_events_in_profile = false;
32         $a->videowidth = 622;
33
34         Renderer::setActiveTemplateEngine('smarty3');
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
47 function frio_install()
48 {
49         Hook::register('prepare_body_final', 'view/theme/frio/theme.php', 'frio_item_photo_links');
50         Hook::register('item_photo_menu', 'view/theme/frio/theme.php', 'frio_item_photo_menu');
51         Hook::register('contact_photo_menu', 'view/theme/frio/theme.php', 'frio_contact_photo_menu');
52         Hook::register('nav_info', 'view/theme/frio/theme.php', 'frio_remote_nav');
53         Hook::register('acl_lookup_end', 'view/theme/frio/theme.php', 'frio_acl_lookup');
54         Hook::register('display_item', 'view/theme/frio/theme.php', 'frio_display_item');
55
56         Logger::log('installed theme frio');
57 }
58
59 function frio_uninstall()
60 {
61         Hook::unregister('prepare_body_final', 'view/theme/frio/theme.php', 'frio_item_photo_links');
62         Hook::unregister('item_photo_menu', 'view/theme/frio/theme.php', 'frio_item_photo_menu');
63         Hook::unregister('contact_photo_menu', 'view/theme/frio/theme.php', 'frio_contact_photo_menu');
64         Hook::unregister('nav_info', 'view/theme/frio/theme.php', 'frio_remote_nav');
65         Hook::unregister('acl_lookup_end', 'view/theme/frio/theme.php', 'frio_acl_lookup');
66         Hook::unregister('display_item', 'view/theme/frio/theme.php', 'frio_display_item');
67
68         Logger::log('uninstalled theme frio');
69 }
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         $occurence = 0;
86         $p = Plaintext::getBoundariesPosition($body_info['html'], '<a', '>');
87         while ($p !== false && ($occurence++ < 500)) {
88                 $link = substr($body_info['html'], $p['start'], $p['end'] - $p['start']);
89                 $matches = [];
90
91                 preg_match('/\/photos\/[\w]+\/image\/([\w]+)/', $link, $matches);
92                 if ($matches) {
93                         // Replace the link for the photo's page with a direct link to the photo itself
94                         $newlink = str_replace($matches[0], "/photo/{$matches[1]}", $link);
95
96                         // Add a "quiet" parameter to any redir links to prevent the "XX welcomes YY" info boxes
97                         $newlink = preg_replace('/href="([^"]+)\/redir\/([^"]+)&url=([^"]+)"/', 'href="$1/redir/$2&quiet=1&url=$3"', $newlink);
98
99                         // Having any arguments to the link for Colorbox causes it to fetch base64 code instead of the image
100                         $newlink = preg_replace('/\/[?&]zrl=([^&"]+)/', '', $newlink);
101
102                         $body_info['html'] = str_replace($link, $newlink, $body_info['html']);
103                 }
104
105                 $p = Plaintext::getBoundariesPosition($body_info['html'], '<a', '>', $occurence);
106         }
107 }
108
109 /**
110  * @brief Replace links of the item_photo_menu hook
111  *
112  *  This function replaces the original poke and the message links
113  *  to call the addToModal javascript function so this pages can
114  *  be loaded in a bootstrap modal
115  *
116  * @param App $a Unused but required by the hook definition
117  * @param array $arr Contains item data and the original photo_menu
118  */
119 function frio_item_photo_menu(App $a, &$arr)
120 {
121         foreach ($arr['menu'] as $k => $v) {
122                 if (strpos($v, 'poke/?f=&c=') === 0 || strpos($v, 'message/new/') === 0) {
123                         $v = 'javascript:addToModal(\'' . $v . '\'); return false;';
124                         $arr['menu'][$k] = $v;
125                 }
126         }
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         $cid = $args['contact']['id'];
144
145         if (!empty($args['menu']['poke'])) {
146                 $pokelink = $args['menu']['poke'][1];
147         } else {
148                 $pokelink = '';
149         }
150
151         if (!empty($args['menu']['poke'])) {
152                 $pmlink = $args['menu']['pm'][1];
153         } else {
154                 $pmlink = '';
155         }
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 = Model\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         $webbie = '';
222         if (count($url_parts)) {
223                 $server_url = $url_parts[1] . $url_parts[2];
224                 $webbie = $url_parts[4] . '@' . $url_parts[2];
225         }
226
227         // since $userinfo isn't available for the hook we write it to the nav array
228         // this isn't optimal because the contact query will be done now twice
229         if (local_user()) {
230                 // empty the server url for local user because we won't need it
231                 $server_url = '';
232                 // user info
233                 $r = q("SELECT `micro` FROM `contact` WHERE `uid` = %d AND `self`", intval($a->user['uid']));
234
235                 $r[0]['photo'] = (DBA::isResult($r) ? $a->removeBaseURL($r[0]['micro']) : 'images/person-48.jpg');
236                 $r[0]['name'] = $a->user['username'];
237         } elseif (!local_user() && remote_user()) {
238                 $r = q("SELECT `name`, `nick`, `micro` AS `photo` FROM `contact` WHERE `id` = %d", intval(remote_user()));
239                 $nav['remote'] = L10n::t('Guest');
240         } elseif (Model\Profile::getMyURL()) {
241                 $r = q("SELECT `name`, `nick`, `photo` FROM `gcontact`
242                                 WHERE `addr` = '%s' AND `network` = 'dfrn'",
243                         DBA::escape($webbie));
244                 $nav['remote'] = L10n::t('Visitor');
245         } else {
246                 $r = false;
247         }
248
249         $remoteUser = null;
250         if (DBA::isResult($r)) {
251                 $nav['userinfo'] = [
252                         'icon' => (DBA::isResult($r) ? $r[0]['photo'] : 'images/person-48.jpg'),
253                         'name' => $r[0]['name'],
254                 ];
255                 $remoteUser = $r[0];
256         }
257
258         if (!local_user() && !empty($server_url) && !is_null($remoteUser)) {
259                 $nav['logout'] = [$server_url . '/logout', L10n::t('Logout'), '', L10n::t('End this session')];
260
261                 // user menu
262                 $nav['usermenu'][] = [$server_url . '/profile/' . $remoteUser['nick'], L10n::t('Status'), '', L10n::t('Your posts and conversations')];
263                 $nav['usermenu'][] = [$server_url . '/profile/' . $remoteUser['nick'] . '?tab=profile', L10n::t('Profile'), '', L10n::t('Your profile page')];
264                 $nav['usermenu'][] = [$server_url . '/photos/' . $remoteUser['nick'], L10n::t('Photos'), '', L10n::t('Your photos')];
265                 $nav['usermenu'][] = [$server_url . '/videos/' . $remoteUser['nick'], L10n::t('Videos'), '', L10n::t('Your videos')];
266                 $nav['usermenu'][] = [$server_url . '/events/', L10n::t('Events'), '', L10n::t('Your events')];
267
268                 // navbar links
269                 $nav['network'] = [$server_url . '/network', L10n::t('Network'), '', L10n::t('Conversations from your friends')];
270                 $nav['events'] = [$server_url . '/events', L10n::t('Events'), '', L10n::t('Events and Calendar')];
271                 $nav['messages'] = [$server_url . '/message', L10n::t('Messages'), '', L10n::t('Private mail')];
272                 $nav['settings'] = [$server_url . '/settings', L10n::t('Settings'), '', L10n::t('Account settings')];
273                 $nav['contacts'] = [$server_url . '/contact', L10n::t('Contacts'), '', L10n::t('Manage/edit friends and contacts')];
274                 $nav['sitename'] = Config::get('config', 'sitename');
275         }
276 }
277
278 /**
279  * @brief: Search for contacts
280  *
281  * This function search for a users contacts. The code is copied from contact search
282  * in /src/Module/Contact.php. With this function the contacts will permitted to acl_lookup()
283  * and can grabbed as json. For this we use the type="r". This is usful to to let js
284  * grab the contact data.
285  * We use this to give the data to textcomplete and have a filter function at the
286  * contact page.
287  *
288  * @param App $a The app data @TODO Unused
289  * @param array $results The array with the originals from acl_lookup()
290  */
291 function frio_acl_lookup(App $a, &$results)
292 {
293         $nets = !empty($_GET['nets']) ? Strings::escapeTags(trim($_GET['nets'])) : '';
294
295         // we introduce a new search type, r should do the same query like it's
296         // done in /src/Module/Contact.php for connections
297         if ($results['type'] !== 'r') {
298                 return;
299         }
300
301         $sql_extra = '';
302         if ($results['search']) {
303                 $search_txt = DBA::escape(Strings::protectSprintf(preg_quote($results['search'])));
304                 $sql_extra .= " AND (`attag` LIKE '%%" . $search_txt . "%%' OR `name` LIKE '%%" . $search_txt . "%%' OR `nick` LIKE '%%" . $search_txt . "%%') ";
305         }
306
307         if ($nets) {
308                 $sql_extra .= sprintf(" AND network = '%s' ", DBA::escape($nets));
309         }
310
311         $total = 0;
312         $r = q("SELECT COUNT(*) AS `total` FROM `contact`
313                 WHERE `uid` = %d AND NOT `self` AND NOT `deleted` AND NOT `pending` $sql_extra ", intval($_SESSION['uid']));
314         if (DBA::isResult($r)) {
315                 $total = $r[0]['total'];
316         }
317
318         $sql_extra3 = Widget::unavailableNetworks();
319
320         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `self` AND NOT `deleted` AND NOT `pending` $sql_extra $sql_extra3 ORDER BY `name` ASC LIMIT %d, %d ",
321                 intval($_SESSION['uid']), intval($results['start']), intval($results['count'])
322         );
323
324         $contacts = [];
325
326         if (DBA::isResult($r)) {
327                 foreach ($r as $rr) {
328                         $contacts[] = Module\Contact::getContactTemplateVars($rr);
329                 }
330         }
331
332         $results['items'] = $contacts;
333         $results['tot'] = $total;
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 (
351                 local_user()
352                 && local_user() == $arr['item']['uid']
353                 && $arr['item']['parent'] == $arr['item']['id']
354                 && !$arr['item']['self'])
355         {
356                 $subthread = [
357                         'menu'   => 'follow_thread',
358                         'title'  => L10n::t('Follow Thread'),
359                         'action' => 'dosubthread(' . $arr['item']['id'] . '); return false;',
360                         'href'   => '#'
361                 ];
362         }
363         $arr['output']['subthread'] = $subthread;
364 }