]> git.mxchange.org Git - friendica-addons.git/blob - blackout/blackout.php
Changes:
[friendica-addons.git] / blackout / blackout.php
1 <?php
2 /**
3  * Name: blackout
4  * Description: Blackout your ~friendica node during a given period
5  * License: MIT
6  * Version: 1.1
7  * Author: Tobias Diekershoff <https://social.diekershoff.de/~tobias>
8  *
9  * About
10  * =====
11  *
12  * This addon will allow you to enter a date/time period during which
13  * all your ~friendica visitors from the web will be redirected to a page
14  * you can configure in the admin panel as well.
15  *
16  * Calls to the API and the communication with other ~friendica nodes is
17  * not effected from this addon.
18  *
19  * If you enter a period the current date would be affected none of the
20  * currently logged in users will be effected as well. But if they log
21  * out they can't login again. That way you dear admin can double check
22  * the entered time periode and fix typos without having to hack the
23  * database directly.
24  *
25  * License
26  * =======
27  *
28  * Permission is hereby granted, free of charge, to any person obtaining a copy
29  * of this software and associated documentation files (the "Software"), to deal
30  * in the Software without restriction, including without limitation the rights
31  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
32  * copies of the Software, and to permit persons to whom the Software is
33  * furnished to do so, subject to the following conditions:
34  *
35  * The above copyright notice and this permission notice shall be included in
36  * all copies or substantial portions of the Software.
37  *
38  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
41  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
42  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
43  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
44  * THE SOFTWARE.
45  */
46
47 use Friendica\App;
48 use Friendica\Core\Hook;
49 use Friendica\Core\Logger;
50 use Friendica\Core\Renderer;
51 use Friendica\Core\System;
52 use Friendica\DI;
53
54 function blackout_install() {
55         Hook::register('page_header', 'addon/blackout/blackout.php', 'blackout_redirect');
56 }
57
58 function blackout_redirect ($b)
59 {
60         // if we have a logged in user, don't throw her out
61         if (DI::userSession()->getLocalUserId()) {
62                 return true;
63         }
64
65         // else...
66         $mystart = DI::config()->get('blackout','begindate');
67         $myend   = DI::config()->get('blackout','enddate');
68         $myurl   = DI::config()->get('blackout','url');
69         $now = time();
70         $date1 = DateTime::createFromFormat('Y-m-d G:i', $mystart);
71         $date2 = DateTime::createFromFormat('Y-m-d G:i', $myend);
72         if ($date1 && $date2) {
73                 $date1 = DateTime::createFromFormat('Y-m-d G:i', $mystart)->format('U');
74                 $date2 = DateTime::createFromFormat('Y-m-d G:i', $myend)->format('U');
75         } else {
76                 $date1 = 0;
77                 $date2 = 0;
78         }
79
80         if (( $date1 <= $now ) && ( $now <= $date2 )) {
81                 Logger::notice('redirecting user to blackout page');
82                 System::externalRedirect($myurl);
83         }
84 }
85
86 function blackout_addon_admin(string &$o)
87 {
88         $mystart = DI::config()->get('blackout','begindate');
89         if (! is_string($mystart)) { $mystart = 'YYYY-MM-DD hh:mm'; }
90         $myend   = DI::config()->get('blackout','enddate');
91         if (! is_string($myend)) { $myend = 'YYYY-MM-DD hh:mm'; }
92         $myurl   = DI::config()->get('blackout','url');
93         if (! is_string($myurl)) { $myurl = 'https://www.example.com'; }
94         $t = Renderer::getMarkupTemplate( 'admin.tpl', 'addon/blackout/' );
95
96         $date1 = DateTime::createFromFormat('Y-m-d G:i', $mystart);
97         $date2 = DateTime::createFromFormat('Y-m-d G:i', $myend);
98         // a note for the admin
99         $adminnote = '';
100         if ($date2 < $date1) {
101                 $adminnote = DI::l10n()->t("The end-date is prior to the start-date of the blackout, you should fix this.");
102         } else {
103                 $adminnote = DI::l10n()->t("Please double check the current settings for the blackout. It will begin on <strong>%s</strong> and end on <strong>%s</strong>.", $mystart, $myend);
104         }
105         $o = Renderer::replaceMacros($t, [
106                 '$submit' => DI::l10n()->t('Save Settings'),
107                 '$rurl' => ['rurl', DI::l10n()->t("Redirect URL"), $myurl, DI::l10n()->t("All your visitors from the web will be redirected to this URL."), '', '', 'url'],
108                 '$startdate' => ['startdate', DI::l10n()->t("Begin of the Blackout"), $mystart, DI::l10n()->t("Format is <tt>YYYY-MM-DD hh:mm</tt>; <em>YYYY</em> year, <em>MM</em> month, <em>DD</em> day, <em>hh</em> hour and <em>mm</em> minute.")],
109                 '$enddate' => ['enddate', DI::l10n()->t("End of the Blackout"), $myend, ''],
110                 '$adminnote' => $adminnote,
111                 '$aboutredirect' => DI::l10n()->t("<strong>Note</strong>: The redirect will be active from the moment you press the submit button. Users currently logged in will <strong>not</strong> be thrown out but can't login again after logging out while the blackout is still in place."),
112         ]);
113 }
114
115 function blackout_addon_admin_post ()
116 {
117         DI::config()->set('blackout', 'begindate', trim($_POST['startdate']));
118         DI::config()->set('blackout', 'enddate', trim($_POST['enddate']));
119         DI::config()->set('blackout', 'url', trim($_POST['rurl']));
120 }