-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.php
More file actions
186 lines (171 loc) · 4.79 KB
/
Connection.php
File metadata and controls
186 lines (171 loc) · 4.79 KB
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
<?php
class MdtConnection
{
private static $usern;
private static $passwd;
private static $BP;
private static $url;
private $action;
private $get;
private $post;
private $arguments;
public function getAction(){
return $this->action;
}
public function setAction($action){
$this->action = $action;
return $this;
}
public function addArguments($args){
$this->addHttpVariable('arguments', $args);
return $this;
}
public function getArguments(){
return $this->arguments;
}
public function addHttpGet($get){
$this->addHttpVariable('get', $get);
return $this;
}
public function addHttpPost($post){
$this->addHttpVariable('post', $post);
return $this;
}
private function addHttpVariable($type, $data){
if(!isset($this->{$type})){
$this->{$type} = [];
}
if (!is_array($data)) {
$data = [$data];
}
foreach ($data as $key => $value) {
$this->{$type}[$key] = $value;
}
}
public function getResponse(){
$opts = ["ssl" => [
"verify_peer" => false,
"verify_peer_name" => false
]];
if(isset($this->post)){
$opts['http'] = [
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query($this->post)
];
}
$context = stream_context_create($opts);
$url = self::$url.'?action='.$this->getAction();
$url .= '&BP='.self::$BP.'&usern='.self::$usern.'&passwd='.self::$passwd;
$url .= (isset($this->get) ? "&".http_build_query($this->get) : '');
$result = file_get_contents($url, false, $context);
if($result == 'Username o password errata'){
throw new MdtLoginException($result);
}
if($result === false){
throw new MdtRequestException( error_get_last() );
}
return $result;
}
public function getResponseXml(){
$r = $this->getResponse();
try {
$result = new SimpleXMLElement($r);
} catch (Exception $e) {
print_r($r);
throw $e;
}
return $result;
}
public function multipleSelfRequest($list, $listByArgments = true){
$master = curl_multi_init();
$curl_arr = [];
$results = [];
if($listByArgments){
$par = 'arguments';
$action = $this->getAction();
} else {
$par = 'action';
$arguments = null;
}
if(!is_array($list)){
$list = [$list];
}
foreach ($list as $value) {
${$par} = $value;
if($arguments === null){
$arguments = [];
}
if(!is_array($arguments)){
$arguments = [$arguments];
}
if(count($this->arguments))
$arguments = array_merge($arguments,$this->arguments);
$arguments = json_encode($arguments);
$url = admin_url('admin-ajax.php')."?action=mdt&mdt=$action".($arguments !== []?"&arguments=$arguments" : '');
$url .= (isset($this->get) ? "&".http_build_query($this->get) : '');
$curl_arr[$value] = curl_init($url);
$cookies = http_build_query($_COOKIE,'','; ');
//curl_setopt($curl_arr[$value], CURLOPT_HEADER, 1);
$curlSettings = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => 1,
CURLOPT_COOKIE => $cookies,
CURLOPT_TIMEOUT_MS => 20000
];
curl_setopt_array($curl_arr[$value], $curlSettings);
/*
curl_setopt(, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_arr[$value], CURLOPT_POST, 1);
curl_setopt($curl_arr[$value], CURLOPT_COOKIE, $cookies);
*/
curl_multi_add_handle($master, $curl_arr[$value]);
if(!isset($this->post))
$this->post = [];
if(!is_array($this->post)){
$this->post = [$this->post];
}
//$this->post['arguments'] = $arguments;
curl_setopt($curl_arr[$value], CURLOPT_POSTFIELDS, http_build_query($this->post));
}
do {
curl_multi_exec($master,$running);
} while($running > 0);
foreach ($list as $value) {
$r = curl_multi_getcontent( $curl_arr[$value]);
$results[$value] = json_decode( $r, true );
}
$inforead = curl_multi_info_read($master);
if(($inforead) && ($inforead['result'] !== CURLE_OK)){
throw new MdtRequestException( $inforead );
}
return $results;
}
public function selfRequest($call, $arguments = [])
{
return $this->addArguments($arguments)->multipleSelfRequest($call, false);
}
public function __construct($action = '', $get = null, $post = null)
{
self::init();
$this->action = $action;
if($get != null){
$this->get = $get;
}
if($post != null){
$this->post = $post;
}
}
private static function init(){
if( (!isset(self::$usern)) || (!isset(self::$passwd)) || (!isset(self::$BP)) || (!isset(self::$url))){
self::$usern = (new MdtSharedOption('usern'))->getValue();
self::$passwd = (new MdtSharedOption('passwd'))->getValue();
self::$BP = (new MdtSharedOption('BP'))->getValue();
self::$url = (new MdtSharedOption('url'))->getValue();
}
if( (!self::$usern) || (!self::$passwd) || (!self::$BP) || (!self::$url)){
throw MdtLoginException::NoCredentialException(self::$usern, self::$passwd, self::$BP, self::$url);
}
}
}
?>