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\Config;
48 use Friendica\Core\Hook;
49 use Friendica\Core\L10n;
50 use Friendica\Core\Logger;
51 use Friendica\Core\Renderer;
52 use Friendica\Core\System;
54 function blackout_install() {
55 Hook::register('page_header', 'addon/blackout/blackout.php', 'blackout_redirect');
58 function blackout_uninstall() {
59 Hook::unregister('page_header', 'addon/blackout/blackout.php', 'blackout_redirect');
61 function blackout_redirect ($a, $b) {
62 // if we have a logged in user, don't throw her out
68 $mystart = Config::get('blackout','begindate');
69 $myend = Config::get('blackout','enddate');
70 $myurl = Config::get('blackout','url');
72 $date1 = DateTime::createFromFormat('Y-m-d G:i', $mystart);
73 $date2 = DateTime::createFromFormat('Y-m-d G:i', $myend);
74 if ( $date1 && $date2 ) {
75 $date1 = DateTime::createFromFormat('Y-m-d G:i', $mystart)->format('U');
76 $date2 = DateTime::createFromFormat('Y-m-d G:i', $myend)->format('U');
81 if (( $date1 <= $now ) && ( $now <= $date2 )) {
82 Logger::log('redirecting user to blackout page');
83 System::externalRedirect($myurl);
87 function blackout_addon_admin(&$a, &$o) {
88 $mystart = Config::get('blackout','begindate');
89 if (! is_string($mystart)) { $mystart = "YYYY-MM-DD hh:mm"; }
90 $myend = Config::get('blackout','enddate');
91 if (! is_string($myend)) { $myend = "YYYY-MM-DD hh:mm"; }
92 $myurl = Config::get('blackout','url');
93 if (! is_string($myurl)) { $myurl = "https://www.example.com"; }
94 $t = Renderer::getMarkupTemplate( "admin.tpl", "addon/blackout/" );
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
100 if ($date2 < $date1) {
101 $adminnote = L10n::t("The end-date is prior to the start-date of the blackout, you should fix this");
103 $adminnote = L10n::t("Please double check that the current settings for the blackout. Begin will be <strong>%s</strong> and it will end <strong>%s</strong>.", $mystart, $myend);
105 $o = Renderer::replaceMacros($t, [
106 '$submit' => L10n::t('Save Settings'),
107 '$rurl' => ["rurl", L10n::t("Redirect URL"), $myurl, L10n::t("all your visitors from the web will be redirected to this URL"), "", "", "url"],
108 '$startdate' => ["startdate", L10n::t("Begin of the Blackout"), $mystart, 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", L10n::t("End of the Blackout"), $myend, ""],
110 '$adminnote' => $adminnote,
111 '$aboutredirect' => 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 should the blackout is still in place."),
114 function blackout_addon_admin_post (&$a) {
115 $begindate = trim($_POST['startdate']);
116 $enddate = trim($_POST['enddate']);
117 $url = trim($_POST['rurl']);
118 Config::set('blackout','begindate',$begindate);
119 Config::set('blackout','enddate',$enddate);
120 Config::set('blackout','url',$url);