e8c573ee6e343a61af35cbfac33807cfe4916edd
[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\Addon;
11 use Friendica\Core\Config;
12 use Friendica\Core\L10n;
13
14 function cookienotice_install()
15 {
16     $file = 'addon/cookienotice/cookienotice.php';
17     Addon::registerHook('page_content_top', $file, 'cookienotice_page_content_top');
18     Addon::registerHook('page_end', $file, 'cookienotice_page_end');
19     Addon::registerHook('addon_settings', $file, 'cookienotice_addon_settings');
20     Addon::registerHook('addon_settings_post', $file, 'cookienotice_addon_settings_post');
21 }
22
23 function cookienotice_uninstall()
24 {
25     $file = 'addon/cookienotice/cookienotice.php';
26     Addon::unregisterHook('page_content_top', $file, 'cookienotice_page_content_top');
27     Addon::unregisterHook('page_end', $file, 'cookienotice_page_end');
28     Addon::unregisterHook('addon_settings', $file, 'cookienotice_addon_settings');
29     Addon::unregisterHook('addon_settings_post', $file, 'cookienotice_addon_settings_post');
30 }
31
32 function cookienotice_addon_settings(&$a, &$s)
33 {
34     if (!is_site_admin())
35         return;
36
37     /* Add our stylesheet to the page so we can make our settings look nice */
38
39     $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="/addon/cookienotice/cookienotice.css" media="all" />' . "\r\n";
40
41
42     $text = Config::get('cookienotice', 'text');
43     if (!$text) {
44         $text = '';
45     }
46     $oktext = Config::get('cookienotice', 'oktext');
47     if (!$oktext) {
48         $oktext = '';
49     }
50
51     $t = get_markup_template("settings.tpl", "addon/cookienotice/");
52     $s .= replace_macros($t, [
53         '$title'       => L10n::t('"cookienotice" Settings'),
54         '$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.'),
55         '$text'        => ['cookienotice-text', L10n::t('Cookie Usage Notice'), $text, L10n::t('The cookie usage notice')],
56         '$oktext'      => ['cookienotice-oktext', L10n::t('OK Button Text'), $oktext, L10n::t('The OK Button text')],
57         '$submit'      => L10n::t('Save Settings')
58     ]);
59
60     return;
61 }
62
63 function cookienotice_addon_settings_post(&$a, &$b)
64 {
65
66     if (!is_site_admin())
67         return;
68
69     if ($_POST['cookienotice-submit']) {
70         Config::set('cookienotice', 'text', trim(strip_tags($_POST['cookienotice-text'])));
71         Config::set('cookienotice', 'oktext', trim(strip_tags($_POST['cookienotice-oktext'])));
72         info(L10n::t('cookienotice Settings saved.') . EOL);
73     }
74 }
75
76 /**
77  * adds the link and script to the page head
78  * 
79  * @param App $a
80  * @param string $b - The page html before page_content_top
81  */
82 function cookienotice_page_content_top($a, &$b)
83 {
84     $head                = file_get_contents(__DIR__ . '/templates/head.tpl');
85     $a->page['htmlhead'] .= $head;
86 }
87
88 /**
89  * adds the html to page end
90  * page_end hook function
91  * 
92  * @param App $a
93  * @param string $b - The page html
94  */
95 function cookienotice_page_end($a, &$b)
96 {
97
98     $text   = (string) Config::get('cookienotice', 'text');
99     $oktext = (string) Config::get('cookienotice', 'oktext');
100
101     $page_end_tpl = get_markup_template("cookienotice.tpl", "addon/cookienotice/");
102
103     $page_end = replace_macros($page_end_tpl, [
104         '$text'   => $text,
105         '$oktext' => $oktext,
106     ]);
107
108     $b .= $page_end;
109 }