Newer
Older
<?php
namespace QUI\Cron;
use QUI;
use QUI\System\Log;
class CronService
{
const CRONSERVICE_URL = "https://cron.quiqqer.com";
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
private $domain;
private $https;
private $packageDir;
public function __construct()
{
$host = QUI::$Conf->get("globals", "host");
$cms_dir = QUI::$Conf->get("globals", "cms_dir");
$opt_dir = QUI::$Conf->get("globals", "opt_dir");
$url_dir = QUI::$Conf->get("globals", "url_dir");
// Parse Domain and protocol
if (strpos($host, "https://") !== false) {
$this->https = true;
$this->domain = str_replace("https://", "", $host);
} elseif (strpos($host, "http://") !== false) {
$this->https = false;
$this->domain = str_replace("http://", "", $host);
} else {
$this->https = false;
$this->domain = $host;
}
// Parse Package dir
$this->packageDir = $url_dir . str_replace($cms_dir, "", $opt_dir);
}
/**
* Will register this quiqqer instance.
*
* @param $email - Email used for communication. Must be valid.
*/
public function register($email)
{
$this->sendRegistrationRequest($this->domain, $email, $this->packageDir, $this->https);
}
/**
* Gets the status of the given domain.
*
* Returnformat :
* array(
* 'status' => 0, (0=unregistered; 1=active; 2=inactive)
* 'current_failures' => int,
* 'total_failures' => int,
* 'last_execution' => string (mysql dateformat)
* @return mixed
*/
public function getStatus()
{
$status = $this->makeServerAjaxCall('package_pcsg_cronservice_ajax_getStatus', array(
'domain' => $this->domain
));
return $status;
}
/**
* Revoked the registration for this quiqqer instance
*/
public function revokeRegistration()
{
$this->makeServerAjaxCall('package_pcsg_cronservice_ajax_revokeRegistration', array(
'domain' => $this->domain,
'token' => $token
));
}
/**
* Sends an ajax request to the cronservice server.
*
* @param $domain - The domain to be registered. Example : example.org
* @param $email - The Email that should be used for communication.
* @param $packageDir - The package url dir
* @param $https - wether or not http secure should be used to call the cron.php
*/
private function sendRegistrationRequest($domain, $email, $packageDir, $https)
{
if (empty($domain)) {
throw new Exception(array("quiqqer/cron", "exception.registration.empty.domain"));
}
if (empty($email)) {
throw new Exception(array("quiqqer/cron", "exception.registration.empty.email"));
}
if (empty($packageDir)) {
throw new Exception(array("quiqqer/cron", "exception.registration.empty.packageDir"));
}
$url = self::CRONSERVICE_URL . "/admin/ajax.php?" .
"_rf=" . urlencode("[\"package_pcsg_cronservice_ajax_register\"]") .
"&package=" . urlencode("pcsg/cronservice") .
"&lang=" . QUI::getUserBySession()->getLang() .
"&domain=" . urlencode($domain) .
"&email=" . urlencode($email) .
"&packageDir=" . urlencode($packageDir) .
"&https=" . ($https ? "1" : "0") .
"&user=" . QUI::getUserBySession()->getName();
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'QUIQQER'
));
$response = curl_exec($curl);
$response = substr($response, 9, -10);
$data = json_decode($response, true);
if (json_last_error() != JSON_ERROR_NONE) {
Log::addDebug($response);
throw new Exception(json_last_error_msg());
}
if (!isset($data['package_pcsg_cronservice_ajax_register']['result'])) {
throw new Exception("Something went wrong!");
}
$data = $data['package_pcsg_cronservice_ajax_register']['result'];
if (!isset($data['status']) || $data['status'] != 1) {
Log::addDebug($response);
Log::writeRecursive($data);
if (isset($data['message'])) {
throw new Exception($data['message']);
}
throw new Exception("Something went wrong!");
}
$revokeCode = $data['revokeCode'];
$this->saveRevokeToken($revokeCode);
curl_close($curl);
}
/**
* Calls the given ajax function on the Cronservice server and returns its output
* @param $function - Ajax function name
* @param $params - Params to pass
* @return mixed
* @throws QUI\Exception
*/
private function makeServerAjaxCall($function, $params)
{
$url = self::CRONSERVICE_URL . "/admin/ajax.php?" .
"_rf=" . urlencode('["' . $function . '"]') .
"&package=" . urlencode("pcsg/cronservice") .
"&lang=" . QUI::getUserBySession()->getLang();
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
199
200
201
foreach ($params as $param => $value) {
$url .= '&' . $param . '=' . urlencode($value);
}
Log::addDebug("Ajax Request : " . $url);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'QUIQQER'
));
$response = curl_exec($curl);
Log::addDebug($response);
curl_close($curl);
// Process raw ajax response
$response = substr($response, 9, -10);
$response = json_decode($response, true);
if (isset($response[$function]['Exception'])) {
throw new QUI\Exception($response[$function]['Exception']['message']);
}
Log::writeRecursive($response);
return $response[$function]['result'];
}
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
* Saves the revoke token into a file
* @param $token
*/
private function saveRevokeToken($token)
{
$varDir = QUI::getPackage('quiqqer/cron')->getVarDir() . '/cronservice';
$fileName = $varDir . '/.revoketoken';
if (!is_dir($varDir)) {
mkdir($varDir, 0700, true);
}
file_put_contents($fileName, $token);
}
/**
* Reads the revoke token from the filesystem
* @return string
* @throws Exception
*/
private function readRevokeToken()
{
$varDir = QUI::getPackage('quiqqer/cron')->getVarDir() . '/cronservice';
$fileName = $varDir . '/.revoketoken';
if (!file_exists($fileName)) {
throw new Exception("Tokenfile not present");
}
$token = file_get_contents($fileName);
if ($token === false) {
throw new Exception("Could not read tokenfile.");
}
return $token;
}