-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathajax.php
645 lines (590 loc) · 20.5 KB
/
ajax.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
199
200
201
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
<?php
require("script.php");
/*
* Parse all input parameters
*/
$qs_wikiuser = @$_POST['param']['WikiBotUser'];
$qs_wikipass = @$_POST['param']['WikiBotPass'];
$qs_action = @$_POST['param']['action'];
$qs_area51 = @$_POST['param']['area51'];
$qs_merge51 = @$_POST['param']['merge51'];
$qs_delete51 = @$_POST['param']['delete51'];
$qs_latest = @$_POST['param']['latestDate'];
$response = $_POST;
$response['data'] = array();
/*
* Handle github/wiki action
*/
$oGithub = new cGithub();
$oWiki = new cWiki($qs_area51, $oGithub);
$oWiki->setCredential($qs_wikiuser, $qs_wikipass);
switch ($qs_action) {
case "Area51":
// Retrive the functions from area51 page
$response['data']['status'] = $oWiki->getArea51();
$response['data']['table'] = $oWiki->area51_table;
break;
case "Area51Insert":
// Add the functions from area51 and merge it with the original page
$response['data']['status'] = $oWiki->getArea51($qs_merge51);
$response['data']['merged'] = $oWiki->area51_ok;
$response['data']['table'] = $oWiki->area51_table;
break;
case "Area51Delete":
// Delete the function from area51
$response['data']['status'] = $oWiki->delArea51($qs_delete51);
$response['data']['deleted'] = $oWiki->area51_ok;
$response['data']['table'] = $oWiki->area51_table;
break;
}
print json_encode($response);
/*
* Class to handle all Wiki integration
*/
class cWiki {
// Wiki Access parameters
private $api_url = WIKI_URL . "api.php";
private $username = "";
private $password = "";
private $login_token = "";
private $csrf_token = "";
// Public Working property
public $area51_table;
public $area51_content;
public $area51_ok = array();
// Private working property
private $area51 = "";
private $git;
private $tmp_table;
private $tmp_content;
function __construct($area51, $github) {
$this->area51 = basename($area51);
$this->git = $github;
}
/*
* Set the Wiki bot credential to handle merge/delete
*/
function setCredential($username, $password) {
$this->username = $username;
$this->password = $password;
}
/*
* Get all functions from Area51 and official Wikipages
* $insert = array of function name in Area51 that would be merged
*/
function getArea51($insert = array()) {
$arrWikipage = array();
$arrWikicont = array();
$ret = $this->getFunctions($this->area51);
$this->area51_table = $this->tmp_table;
$this->area51_content = $this->tmp_content;
// check mergable
foreach ($this->area51_table as $kTable => $vTable) {
foreach ($vTable as $kMerge => $vMerge) {
if ($vMerge["NAME"] != "STUBLAST") {
// Base information
$this->area51_table[$kTable][$kMerge]["ID"] = $vMerge["NAME"]; // Unique function reference
$this->area51_table[$kTable][$kMerge]["MERGE"] = MERGE_NONE; // Is marging possibile
$this->area51_table[$kTable][$kMerge]["NOTE" ] = ""; // Output information for human
// Separate NAME from PR
$arrName = $this->splitPR($vMerge["NAME"]);
$this->area51_table[$kTable][$kMerge]["NAME"] = $arrName[0]; // Name of the function
$this->area51_table[$kTable][$kMerge]["PR"] = (isset($arrName[1]) ? $arrName[1] : ""); // PR reference number
$this->area51_table[$kTable][$kMerge]["PR"] .= ' ' . ($arrName[2] ? '<span class="mergeOK">✓' : '<span class="mergeKO">❌') . '</span>';
// Extract the content and fix it
$tmpContent = $this->getContent($this->area51_content, $vMerge["OFFSET"]);
$tmpContent = "==" . $this->area51_table[$kTable][$kMerge]["NAME"] . "==\n" . $tmpContent . "\n";
$this->area51_table[$kTable][$kMerge]["CONTENT"] = $tmpContent; // Content of the function
// Add other information
$this->area51_table[$kTable][$kMerge]["LINK"] = WIKI_URL . "w/" . $this->area51 . "#" . $this->wikiAnchor($vMerge["NAME"]); // Link to the area51 function
$this->area51_table[$kTable][$kMerge]["WIKI_LINK"] = ""; // Link to the real wiki page
$this->area51_table[$kTable][$kMerge]["OFFSET_INSERT"] = -1; // Offset where insert the function
$this->area51_table[$kTable][$kMerge]["OFFSET_STOP"] = -1; // Offset to stop insert the function (in MERGE_REPLACE)
// Check if I can separate function markup from other text
if ($this->area51_table[$kTable][$kMerge]["CONTENT"] != "") {
// Check if there is a wiki page assignet to this section
if (isset(WIKI_PAGES[$kTable]) && WIKI_PAGES[$kTable] != "") {
// Check if there is a PR
if ($this->area51_table[$kTable][$kMerge]["PR"] != "") {
// Search where to add this functions
if (!isset($arrWikipage[$kTable])) {
$ret = $this->getFunctions(WIKI_PAGES[$kTable]);
if ($ret != "OK") {
} else {
$arrWikipage[$kTable] = $this->tmp_table;
$arrWikicont[$kTable] = $this->tmp_content;
}
}
// Check again if there is a functions page
if (isset($arrWikipage[$kTable])) {
// Check if there is more than a result
if (count($arrWikipage[$kTable]) == 1) {
$arrFunctions = reset($arrWikipage[$kTable]);
// First search for REPLACE (avoid STUBLAST element)
for ($i = count($arrFunctions) - 2; $i >= 0; $i--) {
$tmpCompare = strcmp($arrFunctions[$i]["NAME"], $this->area51_table[$kTable][$kMerge]["NAME"]);
// Replace the function text
if ($tmpCompare === 0) {
$this->area51_table[$kTable][$kMerge]["MERGE"] = MERGE_REPLACE;
$this->area51_table[$kTable][$kMerge]["NOTE"] = "REPLACE " . $arrFunctions[$i]["NAME"];
$this->area51_table[$kTable][$kMerge]["OFFSET_INSERT"] = $arrFunctions[$i]["OFFSET"];
$this->area51_table[$kTable][$kMerge]["OFFSET_STOP" ] = $arrFunctions[$i+1]["OFFSET"];
break;
}
}
// Last search for INSERT in a-z order (avoid STUBLAST element)
if ($this->area51_table[$kTable][$kMerge]["MERGE"] == MERGE_NONE) {
for ($i = count($arrFunctions) - 2; $i >= 0; $i--) {
$tmpCompare = strcmp($arrFunctions[$i]["NAME"], $this->area51_table[$kTable][$kMerge]["NAME"]);
// Insert the function text
if ($tmpCompare < 0) {
$this->area51_table[$kTable][$kMerge]["MERGE"] = MERGE_INSERT;
$this->area51_table[$kTable][$kMerge]["NOTE"] = "INSERT after " . $arrFunctions[$i]["NAME"];
$this->area51_table[$kTable][$kMerge]["OFFSET_INSERT"] = $arrFunctions[$i+1]["OFFSET"];
break;
}
}
}
if ($this->area51_table[$kTable][$kMerge]["MERGE"] == MERGE_NONE && $this->area51_table[$kTable][$kMerge]["NOTE"] == "") {
$this->area51_table[$kTable][$kMerge]["NOTE"] = "Cannot find where insert functions";
} else {
$this->area51_table[$kTable][$kMerge]["WIKI_LINK"] = WIKI_URL . "w/" . WIKI_PAGES[$kTable] . "#" . $this->wikiAnchor($arrFunctions[$i]["NAME"]);
}
} else {
$this->area51_table[$kTable][$kMerge]["NOTE"] = "Too many sections " . $kTable;
}
} else {
$this->area51_table[$kTable][$kMerge]["NOTE"] = "Cannot load functions page " . $kTable;
}
} else {
$this->area51_table[$kTable][$kMerge]["NOTE"] = "Cannot merge a functions without PR";
}
} else {
$this->area51_table[$kTable][$kMerge]["NOTE"] = "Sections not found or not mergeable";
}
} else {
$this->area51_table[$kTable][$kMerge]["NOTE"] = "Cannot detect function content";
}
}
}
}
// Let's start the real work
if ($ret == "OK" && count($insert) > 0) {
foreach ($this->area51_table as $kTable => $vTable) {
$bolOneAdd = false;
$arrOK = array();
$vTable = array_reverse($vTable);
foreach ($vTable as $kMerge => $vMerge) {
if ($vMerge["NAME"] != "STUBLAST") {
if ($vMerge["OFFSET_INSERT"] > 0 && in_array($vMerge["ID"], $insert)) {
$bolOneAdd = true;
// replace the wiki page content with the functions one
if ($vMerge["MERGE"] == MERGE_INSERT) {
$arrWikicont[$kTable] = substr_replace($arrWikicont[$kTable], $vMerge["CONTENT"], $vMerge["OFFSET_INSERT"], 0);
$arrOK[] = $vMerge["ID"];
} elseif ($vMerge["MERGE"] == MERGE_REPLACE) {
$arrWikicont[$kTable] = substr_replace($arrWikicont[$kTable], $vMerge["CONTENT"], $vMerge["OFFSET_INSERT"], $vMerge["OFFSET_STOP"] - $vMerge["OFFSET_INSERT"]);
$arrOK[] = $vMerge["ID"];
}
}
}
}
// wiki call to edit the original page
if ($bolOneAdd) {
$tmpRet = $this->loginAll(WIKI_PAGES[$kTable], $arrWikicont[$kTable]);
if ($tmpRet == "OK") {
$this->area51_ok = array_merge($this->area51_ok, $arrOK);
} else {
$ret .= $tmpRet . "\n";
}
}
}
}
return $ret;
}
/*
* Get all functions from Area51
* $delete = array of function name in Area51 that would be deleted
*/
function delArea51($delete) {
$ret = $this->getFunctions($this->area51);
// Devo partire dal fondo altrimenti gli offset non tornano dopo la cancellazione
$this->area51_table = array_reverse($this->tmp_table);
$this->area51_content = $this->tmp_content;
// Let's start the real work
if ($ret == "OK" && count($delete) > 0) {
$arrOK = array();
foreach ($this->area51_table as $kTable => $vTable) {
// devo partire dal fondo a cancellare
$vTable = array_reverse($vTable);
for ($i = 0; $i < count($vTable); $i++) {
$vMerge = $vTable[$i];
if ($vMerge["NAME"] != "STUBLAST") {
if (in_array($vMerge["NAME"], $delete)) {
// delete the wiki area51 content
$this->area51_content = substr_replace($this->area51_content, '', $vMerge["OFFSET"], $vTable[$i-1]["OFFSET"] - $vMerge["OFFSET"]);
$arrOK[] = $vMerge["NAME"];
}
}
}
// wiki call to update the area51 page
$tmpRet = $this->loginAll(basename($this->area51), $this->area51_content);
if ($tmpRet == "OK") {
$this->area51_ok = array_merge($this->area51_ok, $arrOK);
} else {
$ret .= $tmpRet . "\n";
}
}
}
return $ret;
}
/*
* Extract functions from a wikipage using category and section marker
* $page = the name (not url) of the wikipage
*/
function getFunctions($page) {
$arrSections = array();
$arrFunctions = array();
$this->tmp_table = array();
// request a page to wikimedia
$url = $this->api_url . "?action=query&prop=revisions&titles=" . $page . "&rvslots=*&rvprop=content&formatversion=2&format=json";
$response = get_web_page($url, array(), true, array());
$ret = $this->isJson($response["content"]);
if ($ret == "OK") {
$responseJSON = json_decode($response["content"], true);
$ret = $this->isError($responseJSON);
if ($ret == "OK") {
$this->tmp_content = $responseJSON["query"]["pages"][0]["revisions"][0]["slots"]["main"]["content"];
// Search for sections and functions
preg_match_all('/^=[^=].+[^=]=/m', $this->tmp_content, $arrSections, PREG_OFFSET_CAPTURE);
preg_match_all('/^==[^=].+[^=]==/m', $this->tmp_content, $arrFunctions, PREG_OFFSET_CAPTURE);
foreach ($arrFunctions[0] as $functions) {
for ($i = count($arrSections[0]) - 1; $i >= 0; $i--) {
$sections = $arrSections[0][$i];
if ($functions[MATCH_OFFSET] >= $sections[MATCH_OFFSET]) {
// fix name per output display
$secName = $this->cleanName($sections[MATCH_NAME]);
$funName = $this->cleanName($functions[MATCH_NAME]);
// create / populate the output array
if (!isset($this->tmp_table[$secName])) $this->tmp_table[$secName] = array();
$this->tmp_table[$secName][] = array(
"NAME" => $funName,
"OFFSET" => $functions[MATCH_OFFSET]
);
break;
}
}
}
// Add the lastest stub element to every section
for ($i = count($arrSections[0]) - 1; $i >= 0; $i--) {
$sections = $arrSections[0][$i];
$secName = $this->cleanName($sections[MATCH_NAME]);
if (isset($this->tmp_table[$secName]) && count($this->tmp_table[$secName]) > 0) {
$tmpFunction = end($this->tmp_table[$secName]);
// Search the last section of a page, skip the first line with section title
if (preg_match("/^(=|\[)/m", $this->tmp_content, $matches, PREG_OFFSET_CAPTURE, $tmpFunction["OFFSET"] + 1)) {
$this->tmp_table[$secName][] = array(
"NAME" => "STUBLAST",
"OFFSET" => $matches[0][1]
);
}
}
}
}
}
return $ret;
}
/*
* Extract the content of a function
*/
private function getContent($content, $offset) {
$ret = "";
// Position at function title
$content = substr($content, $offset);
// Extract the title
$posTitle = strpos($content, "\n");
$title = substr($content, 0, $posTitle);
// Search for end
$content = substr($content, $posTitle);
if (preg_match("/^(=|\[)/m", $content, $matches, PREG_OFFSET_CAPTURE)) {
$ret = substr($content, 0, $matches[0][1] - 1);
}
return $ret;
}
/*
* Removes special caracther from a function name
*/
private function cleanName($name) {
return trim(str_replace("=", "", $name));
}
/*
* Split a function name in area51 from its PR and attach a proper github PR status
*/
private function splitPR($name) {
$name = str_replace(", ", " ", $name);
$arrName = explode(" ", $name, 2);
// try to extract PR number
if (isset($arrName[1])) {
if (preg_match("/#(\d+)/", $arrName[1], $matches)) {
$arrName[1] = "#" . $matches[1] . " ";
// search PR status in GIT
if ($this->git->getPR($matches[1]) == "OK") {
$arrName[1] .= $this->git->state['STATE'];
$arrName[2] = $this->git->state['MERGED'];
} else {
$arrName[1] .= "NOT FOUND";
$arrName[2] = false;
}
}
}
return $arrName;
}
/*
* Proper escapes a wiki anchor
*/
private function wikiAnchor($anchor) {
$anchor = urlencode($anchor);
$anchor = str_replace(array("%", "+"), array(".", "_"), $anchor);
return $anchor;
}
/*
* Functions to login in Wiki and modify pages, it needs 4 steps
*/
private function loginAll($page, $content) {
$ret = "OK";
if ($this->login_token == "") {
$ret = $this->getLoginToken(); // Step 1
if ($ret == "OK") {
$ret = $this->loginRequest(); // Step 2
if ($ret != "OK") $ret = "loginRequest: " . $ret;
} else {
$ret = "LoginToken: " . $ret;
}
}
if ($this->csrf_token == "" && $ret == "OK") {
$ret = $this->getCSRFToken(); // Step 3
if ($ret != "OK") $ret = "getCSRFToken: " . $ret;
}
if ($ret == "OK") {
$ret = $this->editRequest($page, $content); // Step 4
}
return $ret;
}
// Step 1: GET request to fetch login token
private function getLoginToken() {
$params1 = [
"action" => "query",
"meta" => "tokens",
"type" => "login",
"format" => "json"
];
$url = $this->api_url . "?" . http_build_query( $params1 );
$response = get_web_page($url, array(), true, array(
CURLOPT_COOKIEJAR => "wikiedit.txt",
CURLOPT_COOKIEFILE => "wikiedit.txt"
));
$ret = $this->isJson($response["content"]);
if ($ret == "OK") {
$responseJSON = json_decode($response["content"], true);
$ret = $this->isError($responseJSON);
if ($ret == "OK") {
$this->login_token = $responseJSON["query"]["tokens"]["logintoken"];
}
}
return $ret;
}
// Step 2: POST request to log in. Use of main account for login is not
// supported. Obtain credentials via Special:BotPasswords
private function loginRequest() {
$params2 = [
"action" => "login",
"lgname" => $this->username, // BOT NAME
"lgpassword" => $this->password, // BOT PASSWORD
"lgtoken" => $this->login_token,
"format" => "json"
];
$response = get_web_page($this->api_url, $params2, false, array(
CURLOPT_COOKIEJAR => "wikiedit.txt",
CURLOPT_COOKIEFILE => "wikiedit.txt"
));
$ret = $this->isJson($response["content"]);
// var_dump("loginRequest");
// var_dump($response);
if ($ret == "OK") {
$responseJSON = json_decode($response["content"], true);
$ret = $this->isError($responseJSON);
if ($ret == "OK") {
// Data saved in Cookie
}
}
return $ret;
}
// Step 3: GET request to fetch CSRF token
private function getCSRFToken() {
$params3 = [
"action" => "query",
"meta" => "tokens",
"format" => "json"
];
$url = $this->api_url . "?" . http_build_query( $params3 );
$response = get_web_page($url, array(), true, array(
CURLOPT_COOKIEJAR => "wikiedit.txt",
CURLOPT_COOKIEFILE => "wikiedit.txt"
));
$ret = $this->isJson($response["content"]);
if ($ret == "OK") {
$responseJSON = json_decode($response["content"], true);
$ret = $this->isError($responseJSON);
if ($ret == "OK") {
$this->csrf_token = $responseJSON["query"]["tokens"]["csrftoken"];
}
}
return $ret;
}
// Step 4: POST request to edit a page
public function editRequest($page, $content) {
if (DEBUG) $page = DEBUG_PAGE; // Only for debug
$params4 = [
"action" => "edit",
"title" => $page,
"text" => $content,
"token" => $this->csrf_token,
"bot" => 1,
"format" => "json"
];
$response = get_web_page($this->api_url, $params4, false, array(
CURLOPT_COOKIEJAR => "wikiedit.txt",
CURLOPT_COOKIEFILE => "wikiedit.txt"
));
// var_dump($page);
// var_dump($response["content"]);
$ret = $this->isJson($response["content"]);
if ($ret == "OK") {
$responseJSON = json_decode($response["content"], true);
$ret = $this->isError($responseJSON);
if ($ret == "OK") {
// $this->status = $responseJSON["edit"]["result"];
}
}
return $ret;
}
/*
* check if JSON is valid
*/
private function isJson($json) {
$ret = "OK";
json_decode($json);
if (json_last_error() !== JSON_ERROR_NONE) {
// Errore JSON
$ret = json_last_error();
}
return $ret;
}
/*
* Check if JSON result is a error
*/
function isError($json) {
$ret = "OK";
if (isset($json['error'])) {
$ret = $json['error']['code'] . ' - ' . $json['error']['info'];
}
return $ret;
}
}
/*
* Class to handle all Github integration
*/
class cGithub {
public $state = "";
private $PR = array();
private $token = GITHUB_TOKEN;
private $api_url = GITHUB_API;
/*
* get the PR status and return it
*/
function getPR($number) {
if (isset($this->PR[$number])) {
$ret = "OK";
$this->state = $this->PR[$number];
} else {
$response = get_web_page($this->api_url . $number, array(), true, array(
CURLOPT_HTTPHEADER => array (
"Accept: application/vnd.github+json",
"Authorization: Bearer " . $this->token,
"X-GitHub-Api-Version: 2022-11-28"
)));
$ret = $this->isJson($response["content"]);
if ($ret == "OK") {
$responseJSON = json_decode($response["content"], true);
$ret = $this->isError($responseJSON);
if ($ret == "OK") {
$this->PR[$number] = array(
'STATE' => $responseJSON['state'],
'MERGED' => $responseJSON['merged']
);
$this->state = $this->PR[$number];
}
}
}
return $ret;
}
/*
* check if JSON is valid
*/
private function isJson($json) {
$ret = "OK";
json_decode($json);
if (json_last_error() !== JSON_ERROR_NONE) {
// Errore JSON
$ret = json_last_error();
}
return $ret;
}
/*
* Check if JSON result is a error
*/
function isError($json) {
$ret = "OK";
if (isset($json['message'])) {
$ret = $json['message'] . ' - ' . $json['documentation_url'];
}
return $ret;
}
}
/*
* Curl request... just a little more verbose
*/
function get_web_page( $url, $post = array(), $bolGET = false, $options = array()) {
$options = array_replace(array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
//CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false
), $options);
if (!$bolGET) {
$options[CURLOPT_POST ] = 1;
$options[CURLOPT_POSTFIELDS] = $post;
} else {
$options[CURLOPT_HTTPGET ] = true;
}
$ch = curl_init( $url );
foreach ($options as $key => $value) {
curl_setopt( $ch, $key, $value );
}
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}