4 * Description: Blackout your ~friendica node during a given period
7 * Author: Tobias Diekershoff <https://social.diekershoff.de/~tobias>
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.
16 * Calls to the API and the communication with other ~friendica nodes is
17 * not effected from this addon.
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
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:
35 * The above copyright notice and this permission notice shall be included in
36 * all copies or substantial portions of the Software.
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
47 use Friendica\Core\Hook;
48 use Friendica\Core\Logger;
49 use Friendica\Core\Renderer;
50 use Friendica\Core\System;
53 function blackout_install() {
54 Hook::register('page_header', 'addon/blackout/blackout.php', 'blackout_redirect');
57 function blackout_redirect ($a, $b) {
58 // if we have a logged in user, don't throw her out
64 $mystart = DI::config()->get('blackout','begindate');
65 $myend = DI::config()->get('blackout','enddate');
66 $myurl = DI::config()->get('blackout','url');
68 $date1 = DateTime::createFromFormat('Y-m-d G:i', $mystart);
69 $date2 = DateTime::createFromFormat('Y-m-d G:i', $myend);
70 if ( $date1 && $date2 ) {
71 $date1 = DateTime::createFromFormat('Y-m-d G:i', $mystart)->format('U');
72 $date2 = DateTime::createFromFormat('Y-m-d G:i', $myend)->format('U');
77 if (( $date1 <= $now ) && ( $now <= $date2 )) {
78 Logger::log('redirecting user to blackout page');
79 System::externalRedirect($myurl);
83 function blackout_addon_admin(&$a, &$o) {
84 $mystart = DI::config()->get('blackout','begindate');
85 if (! is_string($mystart)) { $mystart = "YYYY-MM-DD hh:mm"; }
86 $myend = DI::config()->get('blackout','enddate');
87 if (! is_string($myend)) { $myend = "YYYY-MM-DD hh:mm"; }
88 $myurl = DI::config()->get('blackout','url');
89 if (! is_string($myurl)) { $myurl = "https://www.example.com"; }
90 $t = Renderer::getMarkupTemplate( "admin.tpl", "addon/blackout/" );
92 $date1 = DateTime::createFromFormat('Y-m-d G:i', $mystart);
93 $date2 = DateTime::createFromFormat('Y-m-d G:i', $myend);
94 // a note for the admin
96 if ($date2 < $date1) {
97 $adminnote = DI::l10n()->t("The end-date is prior to the start-date of the blackout, you should fix this.");
99 $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);
101 $o = Renderer::replaceMacros($t, [
102 '$submit' => DI::l10n()->t('Save Settings'),
103 '$rurl' => ["rurl", DI::l10n()->t("Redirect URL"), $myurl, DI::l10n()->t("All your visitors from the web will be redirected to this URL."), "", "", "url"],
104 '$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.")],
105 '$enddate' => ["enddate", DI::l10n()->t("End of the Blackout"), $myend, ""],
106 '$adminnote' => $adminnote,
107 '$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."),
110 function blackout_addon_admin_post (&$a) {
111 $begindate = trim($_POST['startdate']);
112 $enddate = trim($_POST['enddate']);
113 $url = trim($_POST['rurl']);
114 DI::config()->set('blackout','begindate',$begindate);
115 DI::config()->set('blackout','enddate',$enddate);
116 DI::config()->set('blackout','url',$url);