Newer
Older
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
<?php
namespace QUI\Cron;
use QUI;
use QUI\System\Log;
class CronService
{
const CRONSERVICE_URL = "http://server.local";
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;
}
public function revokeRegistration()
{
$token = "0VamlwcIlNUgE79ocOgpUKTvjhS8I4cr";
$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)
{
$url = self::CRONSERVICE_URL . "/admin/ajax.php?" .
"_rf=" . urlencode("[\"package_pcsg_cronservice_ajax_register\"]") .
"&package=" . urlencode("pcsg/cronservice") .
"&lang=de" . // TODO Detect language
"&domain=" . urlencode($domain) .
"&email=" . urlencode($email) .
"&packageDir=" . urlencode($packageDir) .
"&https=" . ($https ? "1" : "0");
Log::addDebug($url);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'QUIQQER'
));
$response = curl_exec($curl);
curl_close($curl);
Log::addDebug($response);
}
/**
* 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=de";// TODO Detect language
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'];
}