Merge branch 'develop' of https://github.com/friendica/friendica-addons into develop
[friendica-addons.git/.git] / impressum / impressum.php
1 <?php
2 /**
3  * Name: Impressum
4  * Description: Addon to add contact information to the about page (/friendica)
5  * Version: 1.3
6  * Author: Tobias Diekershoff <https://f.diekershoff.de/profile/tobias>
7  * License: 3-clause BSD license
8  */
9
10 use Friendica\App;
11 use Friendica\Content\Text\BBCode;
12 use Friendica\Core\Hook;
13 use Friendica\Core\Logger;
14 use Friendica\Core\Renderer;
15 use Friendica\DI;
16 use Friendica\Core\Config\Util\ConfigFileManager;
17 use Friendica\Model\User;
18
19 function impressum_install()
20 {
21         Hook::register('load_config', 'addon/impressum/impressum.php', 'impressum_load_config');
22         Hook::register('about_hook', 'addon/impressum/impressum.php', 'impressum_show');
23         Hook::register('page_end', 'addon/impressum/impressum.php', 'impressum_footer');
24         Logger::notice("installed impressum Addon");
25 }
26
27 /**
28  * This is a statement rather than an actual function definition. The simple
29  * existence of this method is checked to figure out if the addon offers a
30  * module.
31  */
32 function impressum_module() {}
33
34 function impressum_content()
35 {
36         DI::baseUrl()->redirect('friendica/');
37 }
38
39 function obfuscate_email (string $s): string
40 {
41         $s = str_replace('@', '(at)', $s);
42         $s = str_replace('.', '(dot)', $s);
43         return $s;
44 }
45
46 function impressum_footer(string &$body)
47 {
48         $text = BBCode::convertForUriId(User::getSystemUriId(), DI::config()->get('impressum', 'footer_text'));
49
50         if ($text != '') {
51                 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl() . '/addon/impressum/impressum.css" media="all" />';
52                 $body .= '<div class="clear"></div>';
53                 $body .= '<div id="impressum_footer">' . $text . '</div>';
54         }
55 }
56
57 function impressum_load_config(ConfigFileManager $loader)
58 {
59         DI::app()->getConfigCache()->load($loader->loadAddonConfig('impressum'), \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC);
60 }
61
62 function impressum_show(string &$body)
63 {
64         $body          .= '<h3>' . DI::l10n()->t('Impressum') . '</h3>';
65         $owner         = DI::config()->get('impressum', 'owner');
66         $owner_profile = DI::config()->get('impressum', 'ownerprofile');
67         $postal        = BBCode::convertForUriId(User::getSystemUriId(), DI::config()->get('impressum', 'postal'));
68         $notes         = BBCode::convertForUriId(User::getSystemUriId(), DI::config()->get('impressum', 'notes'));
69
70         if ($owner) {
71                 if ($owner_profile) {
72                         $tmp = '<a href="' . $owner_profile . '">' . $owner . '</a>';
73                 } else {
74                         $tmp = $owner;
75                 }
76
77                 if ($email = DI::config()->get('impressum', 'email')) {
78                         $body .= '<p><strong>' . DI::l10n()->t('Site Owner').'</strong>: ' . $tmp .'<br /><strong>' . DI::l10n()->t('Email Address') . '</strong>: ' . obfuscate_email($email) . '</p>';
79                 } else {
80                         $body .= '<p><strong>' . DI::l10n()->t('Site Owner').'</strong>: ' . $tmp .'</p>';
81                 }
82
83                 if ($postal) {
84                         $body .= '<p><strong>' . DI::l10n()->t('Postal Address') . '</strong><br />' . $postal . '</p>';
85                 }
86
87                 if ($notes) {
88                         $body .= '<p>' . $notes . '</p>';
89                 }
90         } else {
91                 $body .= '<p>' . DI::l10n()->t('The impressum addon needs to be configured!<br />Please add at least the <tt>owner</tt> variable to your config file. For other variables please refer to the README file of the addon.') . '</p>';
92         }
93 }
94
95 function impressum_addon_admin_post ()
96 {
97         DI::config()->set('impressum', 'owner', strip_tags(trim($_POST['owner'] ?? '')));
98         DI::config()->set('impressum', 'ownerprofile', strip_tags(trim($_POST['ownerprofile'] ?? '')));
99         DI::config()->set('impressum', 'postal', strip_tags(trim($_POST['postal'] ?? '')));
100         DI::config()->set('impressum', 'email', strip_tags(trim($_POST['email'] ?? '')));
101         DI::config()->set('impressum', 'notes', strip_tags(trim($_POST['notes'] ?? '')));
102         DI::config()->set('impressum', 'footer_text', strip_tags(trim($_POST['footer_text'] ?? '')));
103 }
104
105 function impressum_addon_admin (string &$o)
106 {
107         $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/impressum/' );
108         $o = Renderer::replaceMacros($t, [
109                 '$submit' => DI::l10n()->t('Save Settings'),
110                 '$owner' => ['owner', DI::l10n()->t('Site Owner'), DI::config()->get('impressum','owner'), DI::l10n()->t('The page operators name.')],
111                 '$ownerprofile' => ['ownerprofile', DI::l10n()->t('Site Owners Profile'), DI::config()->get('impressum','ownerprofile'), DI::l10n()->t('Profile address of the operator.')],
112                 '$postal' => ['postal', DI::l10n()->t('Postal Address'), DI::config()->get('impressum','postal'), DI::l10n()->t('How to contact the operator via snail mail. You can use BBCode here.')],
113                 '$notes' => ['notes', DI::l10n()->t('Notes'), DI::config()->get('impressum','notes'), DI::l10n()->t('Additional notes that are displayed beneath the contact information. You can use BBCode here.')],
114                 '$email' => ['email', DI::l10n()->t('Email Address'), DI::config()->get('impressum','email'), DI::l10n()->t('How to contact the operator via email. (will be displayed obfuscated)')],
115                 '$footer_text' => ['footer_text', DI::l10n()->t('Footer note'), DI::config()->get('impressum','footer_text'), DI::l10n()->t('Text for the footer. You can use BBCode here.')],
116         ]);
117 }