-
Notifications
You must be signed in to change notification settings - Fork 12
/
xml_automatic_updater.php
198 lines (157 loc) · 5.58 KB
/
xml_automatic_updater.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/**
* This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* @author Benjamin Pick
*
*/
/** Updated by
* Thomas Arend
* New DOWNLOAD_URL
*
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
require_once(ABSPATH . '/wp-admin/includes/file.php');
class HerrnhuterLosungenPlugin_Xml_Automatic_Update
{
const DOWNLOAD_URL = 'https://www.losungen.de/fileadmin/media-losungen/download/Losung_%s_XML.zip';
protected $alternate_dir;
public function __construct()
{
// Look for the Losungen xml in the /uploads-Folder as well
add_filter('herrnhuterlosung_filename_xml', array($this, 'xmlFileName'), 10, 2);
$upload_dir = wp_upload_dir();
$this->alternate_dir = $upload_dir['basedir'];
}
public function xmlFileName($oldFilename, $date)
{
if (file_exists($oldFilename))
return $oldFilename;
$newFilename = $this->alternate_dir . "/losungen" . (int) $date['year'] . ".xml";
return $newFilename;
}
protected function _getDownloadUrl($date)
{
return sprintf(apply_filters('herrnhuterlosung_download_url', self::DOWNLOAD_URL), (int) $date['year']);
}
public function updater_gui()
{
if (@$_POST['action'] == 'updatelosungen')
{
$year = (int) $_POST['year'];
$ret = $this->doUpdate(array('year' => $year));
if ($ret === true) {
$message = 'Die Losungen für ' . $year . ' wurden erfolgreich installiert.';
} elseif (is_wp_error($ret)) {
$error = '<b>Fehler:</b> ' . $ret->get_error_message();
}
}
$updateAvailable = $this->checkIfUpdatesAreAvailable();
include(dirname(__FILE__) . '/../views/updater.php');
}
/**
* Wann soll das ausgeführt werden?
* * Am 28. Dezember des Vorjahres (via cron -> Failures should be reported via email)
* * Direkt nach Installation des Plugins (activate?)
* * Manuell im Backend (Knopf im Widget? Optionen?)
*
* @param int $year
*/
public function doUpdate($date)
{
global $wp_filesystem;
$download_url = $this->_getDownloadUrl($date);
$this->_setup_WP_Filesystem($download_url);
// 1) Download File
$tmpFile = download_url($download_url);
if (is_wp_error($tmpFile))
return $tmpFile;
// 2) Unzip to tempDir
$tempDir = $this->alternate_dir . '/update-losungen-' . time() . '/';
if (!is_dir($tempDir))
$wp_filesystem->mkdir($tempDir);
$ret = unzip_file($tmpFile, $tempDir);
if (is_wp_error($ret))
return $ret;
// 3) Search any XML File!
$losungenFile = '';
foreach (list_files($tempDir) as $file)
{
if (substr($file, -4) == '.xml') {
$losungenFile = $file;
break;
}
}
// TODO Syntax Validation of XML file
if (!$losungenFile)
return new WP_Error(17000, 'In der Zip-Datei war keine Losungs-XML! (' . $download_url . ')');
// 4) Put in the right place
if (!$wp_filesystem->move($losungenFile, $this->xmlFileName('', $date), true))
return new WP_Error(17000, 'Die XML-Datei konnte nicht geschrieben werden! (' . $this->xmlFileName('', $date) . ')');
// 5) Finally, clean up
$wp_filesystem->delete($tmpFile);
$wp_filesystem->delete($tempDir, true);
return file_exists($this->xmlFileName('', $date));
}
public function checkIfUpdateAvailable($date)
{
$url = $this->_getDownloadUrl($date);
$ret = wp_remote_head($url);
if (is_wp_error($ret)) {
if (WP_DEBUG)
echo "<div class='error'>Fehler: $url: " . $ret->get_error_message() . '<br /></div>';
return false;
}
return ($ret['response']['code'] == 200);
}
protected function _setup_WP_Filesystem($download_url) {
global $wp_filesystem;
if ( false === ( $credentials = request_filesystem_credentials( $download_url, '', false, $this->alternate_dir ) ) ) {
return;
}
if ( ! WP_Filesystem( $credentials, $this->alternate_dir ) ) {
// Failed to connect, Error and request again
request_filesystem_credentials( $download_url, '', true, $this->alternate_dir );
return;
}
if ( $wp_filesystem->errors->get_error_code() ) {
foreach ( $wp_filesystem->errors->get_error_messages() as $message )
show_message($message);
return;
}
}
/**
* Überprüfe, ob aktuelle und nächstjährige Losungen vorhanden sind.
* Falls nicht, überprüfe ob sie heruntergeladen werden können.
*/
function checkIfUpdatesAreAvailable() {
$now = getdate();
$nextYear = getdate(strtotime('next year'));
$updateAvailable = array();
foreach (array($now, $nextYear) as $date)
{
if ($this->checkIfUpdateIsAvailable($date))
$updateAvailable[] = $date;
}
return $updateAvailable;
}
private function checkIfUpdateIsAvailable($date)
{
$losungen = new HerrnhuterLosungenPlugin_Xml();
if (!$losungen->checkIfLosungenAvailable($date))
{
if ($this->checkIfUpdateAvailable($date))
return true;
}
return false;
}
}