Merge pull request #831 from tobiasd/20190322-sv
[friendica-addons.git/.git] / cookienotice / cookienotice.php
1 <?php
2
3 /**
4  * Name: Cookie Notice
5  * Description: Configure, show and handle a simple cookie notice
6  * Version: 1.0
7  * Author: Peter Liebetrau <https://socivitas/profile/peerteer>
8  * 
9  */
10 use Friendica\Core\Hook;
11 use Friendica\Core\Config;
12 use Friendica\Core\L10n;
13 use Friendica\Core\Renderer;
14
15 /**
16  * cookienotice_install
17  * registers hooks
18  * 
19  * @return void
20  */
21 function cookienotice_install()
22 {
23         Hook::register('page_content_top', __FILE__, 'cookienotice_page_content_top');
24         Hook::register('page_end', __FILE__, 'cookienotice_page_end');
25         Hook::register('addon_settings', __FILE__, 'cookienotice_addon_settings');
26         Hook::register('addon_settings_post', __FILE__, 'cookienotice_addon_settings_post');
27 }
28
29 /**
30  * cookienotice_uninstall
31  * unregisters hooks
32  * 
33  * @return void
34 */
35 function cookienotice_uninstall()
36 {
37         Hook::unregister('page_content_top', __FILE__, 'cookienotice_page_content_top');
38         Hook::unregister('page_end', __FILE__, 'cookienotice_page_end');
39         Hook::unregister('addon_settings', __FILE__, 'cookienotice_addon_settings');
40         Hook::unregister('addon_settings_post', __FILE__, 'cookienotice_addon_settings_post');
41 }
42
43 /**
44  * cookienotice_addon_settings
45  * addon_settings hook
46  * creates the admins config panel
47  * 
48  * @param \Friendica\App $a
49  * @param string $s The existing config panel html so far
50  * 
51  * @return void
52  */
53 function cookienotice_addon_settings(\Friendica\App $a, &$s)
54 {
55         if (!is_site_admin()) {
56                 return;
57         }
58
59         /* Add our stylesheet to the page so we can make our settings look nice */
60         $stylesheetPath = 'addon/cookienotice/cookienotice.css';
61         $a->registerStylesheet($stylesheetPath);
62
63         $text = Config::get('cookienotice', 'text', L10n::t('This website uses cookies. If you continue browsing this website, you agree to the usage of cookies.'));
64         $oktext = Config::get('cookienotice', 'oktext', L10n::t('OK'));
65
66         $t = Renderer::getMarkupTemplate("settings.tpl", "addon/cookienotice/");
67         $s .= Renderer::replaceMacros($t, [
68                 '$title' => L10n::t('"cookienotice" Settings'),
69                 '$description' => L10n::t('<b>Configure your cookie usage notice.</b> It should just be a notice, saying that the website uses cookies. It is shown as long as a user didnt confirm clicking the OK button.'),
70                 '$text' => ['cookienotice-text', L10n::t('Cookie Usage Notice'), $text, L10n::t('The cookie usage notice')],
71                 '$oktext' => ['cookienotice-oktext', L10n::t('OK Button Text'), $oktext, L10n::t('The OK Button text')],
72                 '$submit' => L10n::t('Save Settings')
73         ]);
74
75         return;
76 }
77
78 /**
79  * cookienotice_addon_settings_post
80  * addon_settings_post hook
81  * handles the post request from the admin panel
82  * 
83  * @param \Friendica\App $a
84  * @param string $b
85  * 
86  * @return void
87  */
88 function cookienotice_addon_settings_post(\Friendica\App $a, &$b)
89 {
90         if (!is_site_admin()) {
91                 return;
92         }
93
94         if ($_POST['cookienotice-submit']) {
95                 Config::set('cookienotice', 'text', trim(strip_tags($_POST['cookienotice-text'])));
96                 Config::set('cookienotice', 'oktext', trim(strip_tags($_POST['cookienotice-oktext'])));
97                 info(L10n::t('cookienotice Settings saved.') . EOL);
98         }
99 }
100
101 /**
102  * cookienotice_page_content_top
103  * page_content_top hook
104  * adds css and scripts to the <head> section of the html
105  * 
106  * @param \Friendica\App $a
107  * @param string $b unnused - the header html incl. nav
108  * 
109  * @return void
110  */
111 function cookienotice_page_content_top(\Friendica\App $a, &$b)
112 {
113         $stylesheetPath = 'addon/cookienotice/cookienotice.css';
114         $footerscriptPath = 'addon/cookienotice/cookienotice.js';
115
116         $a->registerStylesheet($stylesheetPath);
117         $a->registerFooterScript($footerscriptPath);
118 }
119
120 /**
121  * cookienotice_page_end
122  * page_end hook
123  * ads our cookienotice box to the end of the html
124  * 
125  * @param \Friendica\App $a
126  * @param string $b the page html
127  * 
128  * @return void
129  */
130 function cookienotice_page_end(\Friendica\App $a, &$b)
131 {
132         $text = (string) Config::get('cookienotice', 'text', L10n::t('This website uses cookies to recognize revisiting and logged in users. You accept the usage of these cookies by continue browsing this website.'));
133         $oktext = (string) Config::get('cookienotice', 'oktext', L10n::t('OK'));
134
135         $page_end_tpl = Renderer::getMarkupTemplate("cookienotice.tpl", "addon/cookienotice/");
136
137         $page_end = Renderer::replaceMacros($page_end_tpl, [
138                 '$text' => $text,
139                 '$oktext' => $oktext,
140         ]);
141
142         $b .= $page_end;
143 }