Commit b5dc6a79 authored by Freddy's avatar Freddy

Füge 100commonsdays hinzu

Summary: Füge Hochlader für 100commonsdays hinzu

Reviewers: O5 LarusBot, Luke

Reviewed By: O5 LarusBot, Luke

Subscribers: Luke

Maniphest Tasks: T744

Differential Revision: https://rcm-2.wmflabs.org/D372
parent b9670e74
<?php
include 'BotCore.php';
/** Upload.php
* Hochlademodul für 100commonsdays.
* @Author Freddy2001 <freddy2001@wikipedia.de>
* @Version 0.1
* @Status Alpha
*/
class main extends Core {
function __construct() {
$this->initcurl("Freddy2001@commons", "100commonsdays", $pUseHTTPS = true);
$date = "100commonsdays/" . date('Y-m-d');
$files1 = scandir($date);
print_r($files1);
for($i = 0; $i < count($files1); $i++) {
if($files1[$i] != ".") {
if($files1[$i] != "..") {
$files[] = $files1[$i];
}
}
}
$found = true;
for($i = 0; $i < count($files); $i++) {
if($files[$i] == "desc.txt" OR $files[$i] == "img.jpg" OR $files[$i] == "title.txt") {
} else {
$found = false;
}
}
if($found == false) {
die("nicht alle benötigten dateien vorhanden.");
}
$name = file_get_contents($date . "/title.txt");
if($name == "") {
die("kein name gegeben");
}
$desc = file_get_contents($date . "/desc.txt");
if($desc == "") {
die("keine beschreibung gegeben");
}
$img = file_get_contents($date . "/img.jpg");
echo "lade hoch... \n";
$upload = $this->upload($name, $desc, $date . "/img.jpg", false);
echo $upload;
$empfaenger = 'freddy2001@tools.wmflabs.org';
$betreff = 'Commonsdays Upload ' . $date . ' erfolgreich';
$nachricht = 'Siehe Datei auf Commons:\n\n' . urlencode('https://commons.wikimedia.org/wiki/File:'. $name) . "\n\n" . $upload;
$header = 'From: freddy2001@tools.wmflabs.org' . "\n" . 'X-Mailer: PHP/' . phpversion();
mail($empfaenger, $betreff, $nachricht, $header);
}
public function upload ($dateiname, $beschreibung, $file, $ignorieren) {
try {
$result = $this->httpRequest('action=query&format=php&meta=tokens&type=csrf', $this->job, 'GET');
} catch (Exception $e) {
throw $e;
}
$tree = unserialize($result);
$token = $tree['query']['tokens']['csrftoken'];
if ($token === '')
throw new Exception('could not receive csrf token.');
$cfile = curl_file_create($file,'image/jpg','test_name');
$comment = "Uploaded for 100commonsdays";
$request = array('action' => 'upload',
'format'=>'json',
"filename" => $dateiname,
"comment" => $comment,
"text" => $beschreibung,
"token" => $token,
"formatversion" => "2",
"watchlist"=>"watch",
"ignorewarnings"=>"1",
'file' => $cfile);
return $this->httpRequestFile($request);
}
protected function httpRequestFile($Arguments, $Method = 'POST', $Target = 'w/api.php') {
$baseURL = $this->protocol . '://' .
$this->site . '/' .
$Target;
$Method = strtoupper($Method);
if ($Arguments != '') {
if ($Method === 'POST') {
$requestURL = $baseURL;
$postFields = $Arguments;
} else if ($Method === 'GET') {
$requestURL = $baseURL . '?' .
$Arguments;
} else
throw new Exception('unknown http request method.');
}
if (!$requestURL)
throw new Exception('no arguments for http request found.');
// set curl options
curl_setopt($this->curlHandle, CURLOPT_USERAGENT, "Cygnus");
curl_setopt($this->curlHandle, CURLOPT_URL, $requestURL);
curl_setopt($this->curlHandle, CURLOPT_ENCODING, "UTF-8");
curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curlHandle, CURLOPT_COOKIEFILE, realpath('Cookies.tmp'));
curl_setopt($this->curlHandle, CURLOPT_COOKIEJAR, realpath('Cookies.tmp'));
curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, array('Content-Type' => 'multipart/form-data', 'Content-Disposition' => 'Content-Disposition: form-data; name="file"; filename="test.jpg"'));
// if posted, add post fields
if ($Method === 'POST' && $postFields != '') {
curl_setopt($this->curlHandle, CURLOPT_POST, 1);
curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, $postFields);
} else {
curl_setopt($this->curlHandle, CURLOPT_POST, 0);
curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, '');
}
// perform request
$rqResult = curl_exec($this->curlHandle);
if ($rqResult === false)
throw new Exception('curl request failed: ' . curl_error($this->curlHandle));
return $rqResult;
}
}
$tool = new main();
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment