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