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>.
6 * Author: Rabuzarus <https://friendica.kommune4.de/profile/rabuzarus>
11 use Friendica\Content\Text\Plaintext;
12 use Friendica\Content\Widget;
13 use Friendica\Core\Hook;
14 use Friendica\Core\Logger;
15 use Friendica\Core\Renderer;
16 use Friendica\Core\Session;
17 use Friendica\Database\DBA;
20 use Friendica\Model\Contact;
22 use Friendica\Util\Strings;
24 function frio_init(App $a)
27 $frio = 'view/theme/frio';
29 // disable the events module link in the profile tab
30 $a->theme_events_in_profile = false;
33 Renderer::setActiveTemplateEngine('smarty3');
35 // if the device is a mobile device set js is_mobile
36 // variable so the js scripts can use this information
37 if (DI::mode()->isMobile() || DI::mode()->isMobile()) {
38 DI::page()['htmlhead'] .= <<< EOT
39 <script type="text/javascript">
46 function frio_install()
48 Hook::register('prepare_body_final', 'view/theme/frio/theme.php', 'frio_item_photo_links');
49 Hook::register('item_photo_menu', 'view/theme/frio/theme.php', 'frio_item_photo_menu');
50 Hook::register('contact_photo_menu', 'view/theme/frio/theme.php', 'frio_contact_photo_menu');
51 Hook::register('nav_info', 'view/theme/frio/theme.php', 'frio_remote_nav');
52 Hook::register('acl_lookup_end', 'view/theme/frio/theme.php', 'frio_acl_lookup');
53 Hook::register('display_item', 'view/theme/frio/theme.php', 'frio_display_item');
55 Logger::log('installed theme frio');
59 * Replace friendica photo links hook
61 * This function does replace the links to photos
62 * of other friendica users. Original the photos are
63 * linked to the photo page. Now they will linked directly
64 * to the photo file. This function is nessesary to use colorbox
65 * in the network stream
67 * @param App $a Unused but required by hook definition
68 * @param array $body_info The item and its html output
70 function frio_item_photo_links(App $a, &$body_info)
73 $p = Plaintext::getBoundariesPosition($body_info['html'], '<a', '>');
74 while ($p !== false && ($occurence++ < 500)) {
75 $link = substr($body_info['html'], $p['start'], $p['end'] - $p['start']);
78 preg_match('/\/photos\/[\w]+\/image\/([\w]+)/', $link, $matches);
80 // Replace the link for the photo's page with a direct link to the photo itself
81 $newlink = str_replace($matches[0], "/photo/{$matches[1]}", $link);
83 // Add a "quiet" parameter to any redir links to prevent the "XX welcomes YY" info boxes
84 $newlink = preg_replace('/href="([^"]+)\/redir\/([^"]+)&url=([^"]+)"/', 'href="$1/redir/$2&quiet=1&url=$3"', $newlink);
86 // Having any arguments to the link for Colorbox causes it to fetch base64 code instead of the image
87 $newlink = preg_replace('/\/[?&]zrl=([^&"]+)/', '', $newlink);
89 $body_info['html'] = str_replace($link, $newlink, $body_info['html']);
92 $p = Plaintext::getBoundariesPosition($body_info['html'], '<a', '>', $occurence);
97 * Replace links of the item_photo_menu hook
99 * This function replaces the original poke and the message links
100 * to call the addToModal javascript function so this pages can
101 * be loaded in a bootstrap modal
103 * @param App $a Unused but required by the hook definition
104 * @param array $arr Contains item data and the original photo_menu
106 function frio_item_photo_menu(App $a, &$arr)
108 foreach ($arr['menu'] as $k => $v) {
109 if (strpos($v, '/poke') === 0 || strpos($v, 'message/new/') === 0) {
110 $v = 'javascript:addToModal(\'' . $v . '\'); return false;';
111 $arr['menu'][$k] = $v;
117 * Replace links of the contact_photo_menu
119 * This function replaces the original poke and the message links
120 * to call the addToModal javascript function so this pages can
121 * be loaded in a bootstrap modal
122 * Additionally the profile, status and photo page links will be changed
123 * to don't open in a new tab if the contact is a friendica contact.
125 * @param App $a The app data
126 * @param array $args Contains contact data and the original photo_menu
128 function frio_contact_photo_menu(App $a, &$args)
130 $cid = $args['contact']['id'];
132 if (!empty($args['menu']['poke'])) {
133 $pokelink = $args['menu']['poke'][1];
138 if (!empty($args['menu']['poke'])) {
139 $pmlink = $args['menu']['pm'][1];
144 // Set the the indicator for opening the status, profile and photo pages
145 // in a new tab to false if the contact a dfrn (friendica) contact
146 // We do this because we can go back on foreign friendica pages throuhg
147 // friendicas "magic-link" which indicates a friendica user on froreign
148 // friendica servers as remote user or visitor
150 // The value for opening in a new tab is e.g. when
151 // $args['menu']['status'][2] is true. If the value of the [2] key is true
152 // and if it's a friendica contact we set it to false
153 foreach ($args['menu'] as $k => $v) {
154 if ($k === 'status' || $k === 'profile' || $k === 'photos') {
155 $v[2] = (($args['contact']['network'] === 'dfrn') ? false : true);
156 $args['menu'][$k][2] = $v[2];
160 // Add to pm and poke links a new key with the value 'modal'.
161 // Later we can make conditions in the corresponing templates (e.g.
162 // contact_template.tpl)
163 if (strpos($pokelink, $cid . '/poke') !== false) {
164 $args['menu']['poke'][3] = 'modal';
167 if (strpos($pmlink, 'message/new/' . $cid) !== false) {
168 $args['menu']['pm'][3] = 'modal';
173 * Construct remote nav menu
175 * It creates a remote baseurl form $_SESSION for remote users and friendica
176 * visitors. This url will be added to some of the nav links. With this behaviour
177 * the user will come back to her/his own pages on his/her friendica server.
178 * Not all possible links are available (notifications, administrator, manage,
179 * notes aren't available because we have no way the check remote permissions)..
180 * Some links will point to the local pages because the user would expect
181 * local page (these pages are: search, community, help, apps, directory).
183 * @param App $a The App class
184 * @param array $nav The original nav menu
186 function frio_remote_nav($a, &$nav)
188 // get the homelink from $_XSESSION
189 $homelink = Model\Profile::getMyURL();
191 $homelink = Session::get('visitor_home', '');
194 // since $userinfo isn't available for the hook we write it to the nav array
195 // this isn't optimal because the contact query will be done now twice
196 $fields = ['id', 'url', 'avatar', 'micro', 'name', 'nick', 'baseurl'];
197 if (local_user() && !empty($a->user['uid'])) {
198 $remoteUser = Contact::selectFirst($fields, ['uid' => $a->user['uid'], 'self' => true]);
199 } elseif (!local_user() && remote_user()) {
200 $remoteUser = Contact::getById(remote_user(), $fields);
201 $nav['remote'] = DI::l10n()->t('Guest');
202 } elseif (Model\Profile::getMyURL()) {
203 $remoteUser = Contact::getByURL($homelink, null, $fields);
204 $nav['remote'] = DI::l10n()->t('Visitor');
209 if (DBA::isResult($remoteUser)) {
211 'icon' => Contact::getMicro($remoteUser),
212 'name' => $remoteUser['name'],
214 $server_url = $remoteUser['baseurl'];
217 if (!local_user() && !empty($server_url) && !is_null($remoteUser)) {
219 $nav['usermenu'][] = [$server_url . '/profile/' . $remoteUser['nick'], DI::l10n()->t('Status'), '', DI::l10n()->t('Your posts and conversations')];
220 $nav['usermenu'][] = [$server_url . '/profile/' . $remoteUser['nick'] . '/profile', DI::l10n()->t('Profile'), '', DI::l10n()->t('Your profile page')];
221 $nav['usermenu'][] = [$server_url . '/photos/' . $remoteUser['nick'], DI::l10n()->t('Photos'), '', DI::l10n()->t('Your photos')];
222 $nav['usermenu'][] = [$server_url . '/videos/' . $remoteUser['nick'], DI::l10n()->t('Videos'), '', DI::l10n()->t('Your videos')];
223 $nav['usermenu'][] = [$server_url . '/events/', DI::l10n()->t('Events'), '', DI::l10n()->t('Your events')];
226 $nav['network'] = [$server_url . '/network', DI::l10n()->t('Network'), '', DI::l10n()->t('Conversations from your friends')];
227 $nav['events'] = [$server_url . '/events', DI::l10n()->t('Events'), '', DI::l10n()->t('Events and Calendar')];
228 $nav['messages'] = [$server_url . '/message', DI::l10n()->t('Messages'), '', DI::l10n()->t('Private mail')];
229 $nav['settings'] = [$server_url . '/settings', DI::l10n()->t('Settings'), '', DI::l10n()->t('Account settings')];
230 $nav['contacts'] = [$server_url . '/contact', DI::l10n()->t('Contacts'), '', DI::l10n()->t('Manage/edit friends and contacts')];
231 $nav['sitename'] = DI::config()->get('config', 'sitename');
236 * Search for contacts
238 * This function search for a users contacts. The code is copied from contact search
239 * in /src/Module/Contact.php. With this function the contacts will permitted to acl_lookup()
240 * and can grabbed as json. For this we use the type="r". This is usful to to let js
241 * grab the contact data.
242 * We use this to give the data to textcomplete and have a filter function at the
245 * @param App $a The app data @TODO Unused
246 * @param array $results The array with the originals from acl_lookup()
248 function frio_acl_lookup(App $a, &$results)
250 $nets = !empty($_GET['nets']) ? Strings::escapeTags(trim($_GET['nets'])) : '';
252 // we introduce a new search type, r should do the same query like it's
253 // done in /src/Module/Contact.php for connections
254 if ($results['type'] !== 'r') {
259 if ($results['search']) {
260 $search_txt = DBA::escape(Strings::protectSprintf(preg_quote($results['search'])));
261 $sql_extra .= " AND (`attag` LIKE '%%" . $search_txt . "%%' OR `name` LIKE '%%" . $search_txt . "%%' OR `nick` LIKE '%%" . $search_txt . "%%') ";
265 $sql_extra .= sprintf(" AND network = '%s' ", DBA::escape($nets));
269 $r = q("SELECT COUNT(*) AS `total` FROM `contact`
270 WHERE `uid` = %d AND NOT `self` AND NOT `deleted` AND NOT `pending` $sql_extra ", intval($_SESSION['uid']));
271 if (DBA::isResult($r)) {
272 $total = $r[0]['total'];
275 $sql_extra3 = Widget::unavailableNetworks();
277 $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 ",
278 intval($_SESSION['uid']), intval($results['start']), intval($results['count'])
283 if (DBA::isResult($r)) {
284 foreach ($r as $rr) {
285 $contacts[] = Module\Contact::getContactTemplateVars($rr);
289 $results['items'] = $contacts;
290 $results['tot'] = $total;
294 * Manipulate the data of the item
296 * At the moment we use this function to add some own stuff to the item menu
298 * @param App $a App $a The app data
299 * @param array $arr Array with the item and the item actions<br>
300 * 'item' => Array with item data<br>
301 * 'output' => Array with item actions<br>
303 function frio_display_item(App $a, &$arr)
305 // Add subthread to the item menu
309 && local_user() == $arr['item']['uid']
310 && $arr['item']['gravity'] == GRAVITY_PARENT
311 && !$arr['item']['self'])
314 'menu' => 'follow_thread',
315 'title' => DI::l10n()->t('Follow Thread'),
316 'action' => 'dosubthread(' . $arr['item']['id'] . ');',
320 $arr['output']['subthread'] = $subthread;
324 * @param int|null $uid
326 * @see \Friendica\Core\Theme::getBackgroundColor()
328 function frio_get_background_color(int $uid = null)
330 $background_color = DI::config()->get('frio', 'background_color') ?: '#ededed';
333 $background_color = DI::pConfig()->get($uid, 'frio', 'background_color') ?: $background_color;
336 $scheme = DI::config()->get('frio', 'scheme', DI::config()->get('frio', 'schema'));
337 $scheme = Strings::sanitizeFilePathItem($scheme);
339 if ($scheme && ($scheme != '---') && file_exists('view/theme/frio/scheme/' . $scheme . '.php')) {
340 $schemefile = 'view/theme/frio/scheme/' . $scheme . '.php';
341 require_once $schemefile;
344 return $background_color;
348 * @param int|null $uid
350 * @see \Friendica\Core\Theme::getThemeColor()
352 function frio_get_theme_color(int $uid = null)
354 $nav_bg = DI::config()->get('frio', 'nav_bg') ?: '#708fa0';
357 $nav_bg = DI::pConfig()->get($uid, 'frio', 'background_color') ?: $nav_bg;
360 $scheme = DI::config()->get('frio', 'scheme', DI::config()->get('frio', 'schema'));
361 $scheme = Strings::sanitizeFilePathItem($scheme);
363 if ($scheme && ($scheme != '---') && file_exists('view/theme/frio/scheme/' . $scheme . '.php')) {
364 $schemefile = 'view/theme/frio/scheme/' . $scheme . '.php';
365 require_once $schemefile;