]> git.mxchange.org Git - friendica-addons.git/blob - cookienotice/cookienotice.php
Merge origin/develop into develop
[friendica-addons.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\Addon;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Config;
13 use Friendica\Core\L10n;
14 use Friendica\Core\Renderer;
15
16 /**
17  * cookienotice_install
18  * registers hooks
19  * 
20  * @return void
21  */
22 function cookienotice_install()
23 {
24         $file = 'addon/cookienotice/cookienotice.php';
25         Hook::register('page_content_top', $file, 'cookienotice_page_content_top');
26         Hook::register('page_end', $file, 'cookienotice_page_end');
27         Hook::register('addon_settings', $file, 'cookienotice_addon_settings');
28         Hook::register('addon_settings_post', $file, 'cookienotice_addon_settings_post');
29 }
30
31 /**
32  * cookienotice_uninstall
33  * unregisters hooks
34  * 
35  * @return void
36 */
37 function cookienotice_uninstall()
38 {
39         $file = 'addon/cookienotice/cookienotice.php';
40         Hook::unregister('page_content_top', $file, 'cookienotice_page_content_top');
41         Hook::unregister('page_end', $file, 'cookienotice_page_end');
42         Hook::unregister('addon_settings', $file, 'cookienotice_addon_settings');
43         Hook::unregister('addon_settings_post', $file, 'cookienotice_addon_settings_post');
44 }
45
46 /**
47  * cookienotice_addon_settings
48  * addon_settings hook
49  * creates the admins config panel
50  * 
51  * @param \Friendica\App $a
52  * @param string $s The existing config panel html so far
53  * 
54  * @return void
55  */
56 function cookienotice_addon_settings(\Friendica\App $a, &$s)
57 {
58         if (!is_site_admin()) {
59                 return;
60         }
61
62         /* Add our stylesheet to the page so we can make our settings look nice */
63     $stylesheetPath = 'addon/cookienotice/cookienotice.css';
64     $a->registerStylesheet($stylesheetPath);
65
66         $text = Config::get('cookienotice', 'text');
67         if (!$text) {
68                 $text = '';
69         }
70         $oktext = Config::get('cookienotice', 'oktext');
71         if (!$oktext) {
72                 $oktext = '';
73         }
74
75         $t = Renderer::getMarkupTemplate("settings.tpl", "addon/cookienotice/");
76         $s .= Renderer::replaceMacros($t, [
77                 '$title' => L10n::t('"cookienotice" Settings'),
78                 '$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.'),
79                 '$text' => ['cookienotice-text', L10n::t('Cookie Usage Notice'), $text, L10n::t('The cookie usage notice')],
80                 '$oktext' => ['cookienotice-oktext', L10n::t('OK Button Text'), $oktext, L10n::t('The OK Button text')],
81                 '$submit' => L10n::t('Save Settings')
82         ]);
83
84         return;
85 }
86
87 /**
88  * cookienotice_addon_settings_post
89  * addon_settings_post hook
90  * handles the post request from the admin panel
91  * 
92  * @param \Friendica\App $a
93  * @param string $b
94  * 
95  * @return void
96  */
97 function cookienotice_addon_settings_post(\Friendica\App $a, &$b)
98 {
99         if (!is_site_admin()) {
100                 return;
101         }
102
103         if ($_POST['cookienotice-submit']) {
104                 Config::set('cookienotice', 'text', trim(strip_tags($_POST['cookienotice-text'])));
105                 Config::set('cookienotice', 'oktext', trim(strip_tags($_POST['cookienotice-oktext'])));
106                 info(L10n::t('cookienotice Settings saved.') . EOL);
107         }
108 }
109
110 /**
111  * cookienotice_page_content_top
112  * page_content_top hook
113  * adds css and scripts to the <head> section of the html
114  * 
115  * @param \Friendica\App $a
116  * @param string $b unnused - the header html incl. nav
117  * 
118  * @return void
119  */
120 function cookienotice_page_content_top(\Friendica\App $a, &$b)
121 {
122     $stylesheetPath = 'addon/cookienotice/cookienotice.css';
123     $footerscriptPath = 'addon/cookienotice/cookienotice.js';
124
125     $a->registerStylesheet($stylesheetPath);
126     $a->registerFooterScript($footerscriptPath);
127 }
128
129 /**
130  * cookienotice_page_end
131  * page_end hook
132  * ads our cookienotice box to the end of the html
133  * 
134  * @param \Friendica\App $a
135  * @param string $b the page html
136  * 
137  * @return void
138  */
139 function cookienotice_page_end(\Friendica\App $a, &$b)
140 {
141         $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.'));
142         $oktext = (string) Config::get('cookienotice', 'oktext', L10n::t('OK'));
143
144         $page_end_tpl = Renderer::getMarkupTemplate("cookienotice.tpl", "addon/cookienotice/");
145
146         $page_end = Renderer::replaceMacros($page_end_tpl, [
147                 '$text' => $text,
148                 '$oktext' => $oktext,
149         ]);
150
151         $b .= $page_end;
152 }