]> git.mxchange.org Git - friendica-addons.git/blob - blackout/blackout.php
c0e0dc598a0af06c6d9d06c9caafaaa97a208536
[friendica-addons.git] / blackout / blackout.php
1 <?php
2 /**
3  * Name: blackout
4  * Description: Blackout your ~friendica node during a given period, requires PHP >= 5.3
5  * License: MIT
6  * Version: 1.0
7  * Author: Tobias Diekershoff <https://f.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  * Requirements
26  * ============
27  *
28  * THIS ADDON REQUIRES PHP VERSION 5.3 OR HIGHER.
29  *
30  * License
31  * =======
32  *
33  * Permission is hereby granted, free of charge, to any person obtaining a copy
34  * of this software and associated documentation files (the "Software"), to deal
35  * in the Software without restriction, including without limitation the rights
36  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
37  * copies of the Software, and to permit persons to whom the Software is
38  * furnished to do so, subject to the following conditions:
39  *
40  * The above copyright notice and this permission notice shall be included in
41  * all copies or substantial portions of the Software.
42  *
43  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
44  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
45  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
46  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
47  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
48  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
49  * THE SOFTWARE.
50  */
51
52 use Friendica\Core\Config;
53 use Friendica\Core\Addon;
54 use Friendica\Core\L10n;
55 use Friendica\Core\Logger;
56 use Friendica\Core\Renderer;
57
58 function blackout_install() {
59     Addon::registerHook('page_header', 'addon/blackout/blackout.php', 'blackout_redirect');
60 }
61
62 function blackout_uninstall() {
63     Addon::unregisterHook('page_header', 'addon/blackout/blackout.php', 'blackout_redirect');
64 }
65 function blackout_redirect ($a, $b) {
66     // if we have a logged in user, don't throw her out
67     if (local_user()) {
68         return true;
69     }
70
71         if (! (version_compare(PHP_VERSION, '5.3.0') >= 0))
72                 return true;
73
74     // else...
75     $mystart = Config::get('blackout','begindate');
76     $myend   = Config::get('blackout','enddate');
77     $myurl   = Config::get('blackout','url');
78     $now = time();
79     $date1 = DateTime::createFromFormat('Y-m-d G:i', $mystart);
80     $date2 = DateTime::createFromFormat('Y-m-d G:i', $myend);
81     if ( $date1 && $date2 ) {
82         $date1 = DateTime::createFromFormat('Y-m-d G:i', $mystart)->format('U');
83         $date2 = DateTime::createFromFormat('Y-m-d G:i', $myend)->format('U');
84     } else {
85            $date1 = 0;
86            $date2 = 0;
87     }
88     if (( $date1 <= $now ) && ( $now <= $date2 )) {
89         Logger::log('redirecting user to blackout page');
90         System::externalRedirect($myurl);
91     }
92 }
93
94 function blackout_addon_admin(&$a, &$o) {
95     $mystart = Config::get('blackout','begindate');
96     if (! is_string($mystart)) { $mystart = "YYYY-MM-DD:hhmm"; }
97     $myend   = Config::get('blackout','enddate');
98     if (! is_string($myend)) { $myend = "YYYY-MM-DD:hhmm"; }
99     $myurl   = Config::get('blackout','url');
100     if (! is_string($myurl)) { $myurl = "http://www.example.com"; }
101     $t = Renderer::getMarkupTemplate( "admin.tpl", "addon/blackout/" );
102
103    $o = Renderer::replaceMacros($t, [
104         '$submit' => L10n::t('Save Settings'),
105         '$rurl' => ["rurl", "Redirect URL", $myurl, "all your visitors from the web will be redirected to this URL"],
106         '$startdate' => ["startdate", "Begin of the Blackout<br />(YYYY-MM-DD hh:mm)", $mystart, "format is <em>YYYY</em> year, <em>MM</em> month, <em>DD</em> day, <em>hh</em> hour and <em>mm</em> minute"],
107         '$enddate' => ["enddate", "End of the Blackout<br />(YYYY-MM-DD hh:mm)", $myend, ""],
108
109     ]);
110     $date1 = DateTime::createFromFormat('Y-m-d G:i', $mystart);
111     $date2 = DateTime::createFromFormat('Y-m-d G:i', $myend);
112     if ($date2 < $date1) {
113         $o = "<div style='border: 2px solid #f00; bakckground: #b00; text-align: center; padding: 10px; margin: 30px;'>The end-date is prior to the start-date of the blackout, you should fix this.</div>" . $o;
114     } else {
115         $o = '<p>Please double check that the current settings for the blackout. Begin will be <strong>'.$mystart.'</strong> and it will end <strong>'.$myend.'</strong>.</p>' . $o;
116     }
117 }
118 function blackout_addon_admin_post (&$a) {
119     $begindate = trim($_POST['startdate']);
120     $enddate = trim($_POST['enddate']);
121     $url = trim($_POST['rurl']);
122     Config::set('blackout','begindate',$begindate);
123     Config::set('blackout','enddate',$enddate);
124     Config::set('blackout','url',$url);
125 }