|
<?php |
|
|
|
$url = new AnrDaemon\Net\Url("https://www.example.org/login/page"); |
|
|
|
// Ask for authentication password, fill the form |
|
fwrite(STDERR, 'Enter password: '); |
|
$form = [ |
|
"login" => 'admin', |
|
"pass" => trim(fgets(STDIN)), |
|
]; |
|
|
|
// Create cURL instance |
|
$curl = curl_init($url); |
|
curl_setopt_array($curl, [ |
|
CURLOPT_COOKIEFILE => '', |
|
CURLOPT_COOKIESESSION => true, |
|
CURLOPT_RETURNTRANSFER => true, |
|
CURLOPT_CONNECTTIMEOUT_MS => 500, |
|
//CURLOPT_URL => '', |
|
//CURLOPT_HTTPGET => true, |
|
CURLOPT_POSTFIELDS => $form, |
|
]); |
|
|
|
// Login to the website |
|
curl_exec($curl); |
|
$result = curl_getinfo($curl, CURLINFO_RESPONSE_CODE); |
|
if($result != 303 && $result != 202) |
|
die("Unauthorized."); |
|
|
|
// Prepare two template URL instances: $url to post data and $req to search |
|
$url = $url->parse("/api/endpoint"); |
|
$req = $url->setParts(['query' => ['action' => 'search'] + $url->query]); |
|
$i = 0; |
|
foreach($list as $item) |
|
{ |
|
$i++; |
|
|
|
try |
|
{ // Search for existing item... |
|
curl_setopt_array($curl, [ |
|
CURLOPT_URL => $req->setParts(['query' => ['series' => $item['series']] + $req->query]), |
|
CURLOPT_HTTPGET => true, |
|
//CURLOPT_POSTFIELDS => [], |
|
]); |
|
$result = curl_exec($curl); |
|
if(curl_getinfo($curl, CURLINFO_RESPONSE_CODE) != 200) |
|
throw new \ErrorException("Search failed"); |
|
|
|
// Decode search results |
|
$set = json_decode($result, true); |
|
if(json_last_error() !== JSON_ERROR_NONE) |
|
throw new \ErrorException(json_last_error_msg(), json_last_error()); |
|
if(count($set) > 1) |
|
throw new \ErrorException("Ambiguous results"); |
|
|
|
if(empty($set)) |
|
{ // No existing items - prepare to insert new one |
|
print "Not found {$item['series']}.\n"; |
|
$url = $url->setParts(['query' => ['id' => 0] + $url->query]); |
|
} |
|
else |
|
{ // Found existing items - prepare to replace |
|
$old = reset($set); |
|
print "Found {$old['series']} (" . strlen($old['content']) . ").\n"; |
|
$url = $url->setParts(['query' => ['id' => $old['id']] + $url->query]); |
|
} |
|
} |
|
catch(\ErrorException $e) |
|
{ // Log errors and continue. |
|
error_log($e); |
|
print "Error searching for {$item['series']} ({$e->getMessage()}).\n"; |
|
continue; |
|
} |
|
|
|
// --- POST item --- |
|
|
|
try |
|
{ |
|
$form = [ |
|
"action" => "save", |
|
"series" => $item['series'], |
|
"name" => $item['name'], |
|
"content" => $template->fetch('item.tpl', $item), |
|
]; |
|
|
|
curl_setopt_array($curl, [ |
|
CURLOPT_URL => $url, |
|
//CURLOPT_HTTPGET => true, |
|
CURLOPT_POSTFIELDS => $form, |
|
]); |
|
curl_exec($curl); |
|
$result = curl_getinfo($curl, CURLINFO_RESPONSE_CODE); |
|
if($result != 303 && $result != 202) |
|
throw new \ErrorException("Send failed"); |
|
|
|
print "Sent {$item['series']}.\n"; |
|
} |
|
catch(\ErrorException $e) |
|
{ |
|
error_log($e); |
|
print "Error posting {$item['series']} ({$e->getMessage()}).\n"; |
|
} |
|
|
|
unset($old, $form); |
|
} |