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