User:ShakataGaNai/phpmwupper/phpmwupper.php

From Wikimedia Commons, the free media repository
Jump to: navigation, search

Copy and save the code as phpmwupper.php.

Nuvola apps important.svg

Caution – Do not edit unless you know what you are doing!


<?php
// Project: phpmwupper
// Version: v0.1
// By: Jon Davis [[User:ShakataGaNai]]
// Homepage: http://commons.wikimedia.org/wiki/User:ShakataGaNai/phpmwupper
// License: GPL -- http://www.gnu.org/copyleft/gpl.html
// Note: I provide no warranty in using this software. While it isn't expected, your computer could explode, or become sentient.
//		Either way, don't blame me for anything that happens in the use of this software. Oh, and play nicely with it.
//		That means DONT SPAM uploads.

require_once('config.php');
$GLOBALS['PWD'] = dirname( __FILE__ );


$GLOBAL['EXTEN'] = array('jpg', 'jpeg', 'png', 'gif', 'bmp'); 
//Check to see if CURL is loaded, if not, try to load it.
if(!extension_loaded('curl')){
	if($_SERVER["OS"] == "Windows_NT"){
		if(!dl('php_curl.dll')){
			die("Sorry, but CURL isn't enabled");
		}
	}else{
		if(!dl('curl.so')){
			die("Sorry, but CURL isn't enabled");
		}
	}
}

$desc = file_get_contents($GLOBALS['PWD'].'\description.txt') . "\n<!-- Uploaded by phpmwupper -->";
print "The description for every file uploaded will be as follows:\n";
print "=-=-=-=-= Start =-=-=-=-=\n$desc\n=-=-=-=-= Stop =-=-=-=-=\n";
print "\n\nBelow will be a list of all files to be uploaded from your specifed\ndirectory, and their uploaded name\n";
$filelist = array();
$a = START_NUMBER;
if($handle = opendir(IMAGE_DIR)){
	while(false !== ($file = readdir($handle))) {
		$ext = strtolower(substr($file, strpos($file,'.') + 1));
		if(in_array($ext, $GLOBAL['EXTEN'])){
			$outfile = UPLOAD_PREFIX. " - " .str_pad($a, PAD_DISTANCE, "0", STR_PAD_LEFT). "." .$ext;
			print "$file -> File:$outfile\n";
			$filelist[$file] = $outfile;
			$a++;
		}
	}	
	closedir($handle);
}

print "File Listing has completed. If you're happy with the above list, just wait.\nIf not, you've got 10 seconds to hit ctrl+c\n";
sleep(10);

print "Ok, ROCK AND ROLL. Time to upload - this might take a VERY long time\ndepending on file sizes/internet connection.\n\n";
$GLOBALS['cookies'] = tempnam($GLOBALS['PWD'], "CRL");
if(goLogmein()){
	print "Logged in\n";
}else{
	die("Can't login");
}


foreach($filelist as $loc => $rem){
	print "Uploading: $loc as File:$rem -- ";
	if(goUpload($rem,IMAGE_DIR."\\".$loc,$desc)){
		print "Completed\n";
	}else{
		print "!!FAILED!!\n";
	}
}


print "\n<war3>Jobs Done</war3>\n";
unlink($GLOBALS['cookies']);
exit;
/******** Funkyshuns *********/
function curlIt($url, $post=0, $poststr="", $file=null){
		$UA = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2";
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_VERBOSE, 0); //--Debug only
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_COOKIEJAR, $GLOBALS['cookies']);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $GLOBALS['cookies']);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, $UA);
        if($post){
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $poststr);
        }
		if(!is_null($file)){
			$filecur = fopen($file,"r");
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_INFILE, $filecur);
			curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
			curl_setopt($ch, CURLOPT_POSTFIELDS, json_decode($poststr,true));
			curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect:' ) );
			}
		$retHTML = curl_exec($ch);

		if(!is_null($file)){
			fclose($filecur);
		}
        return($retHTML);
}

function goLogmein(){
	//Get the URL for Submitting Login
	$html = curlIt(UPLOAD_SERVER.INDEX_URL.SPECIAL_LOGIN);
	preg_match("/<form name=\"userlogin\" method=\"post\" action=\"(.*)\">/", $html, $ray);
	$sub2 = htmlspecialchars_decode($ray[1]);

	//Login
	$html = curlIt(UPLOAD_SERVER.$sub2, true, "wpName=".WIKI_USER."&wpPassword=".WIKI_PASS."&wpLoginattempt=Log+in");
	if(preg_match("/var wgUserName = \"(.*)\"/", $html, $ray)){
		//Found the username - We're logged in
		return true;
	}else{
		return false;
		
	}
}

function goUpload($remotefile,$localfile,$desc){
	$html = curlIt(UPLOAD_SERVER.INDEX_URL.SPECIAL_UPLOAD);
	preg_match("/var wgUserName = \"(.*)\"/", $html, $ray);
	if($ray[1] != WIKI_USER){ 
		die("Lost session - not logged in");
	}

	// Code useful, but not needed for commons
	//preg_match("/<form method=\"post\" action=\"(.*)\" enctype=\"multipart\/form-data\" id=\"mw-upload-form\">/", $html, $ray);
	//$act = htmlspecialchars_decode($ray[1]);
	//preg_match("/<input name=\"wpEditToken\" type=\"hidden\" value=\"(.*)\" \/>/", $html, $rayz);
	//$token = $rayz[1];

	$desc = utf8_encode($desc);
	$data = json_encode(array(
		//'wpEditToken'	=>	$token, // Commons doesn't use a Token
		'wpUpload'		=>	'Upload file',
		'wpDestFile'	=>	$remotefile,
		'wpWatchthis'	=>	'1',
		'wpUploadFile'	=>	'@'.$localfile,
		'wpUploadDescription'	=>	$desc,
		//'wpUploadAffirm'	=>	'1',
		'wpIgnoreWarning'	=>	'1',
		'wpSourceType'		=>	'file',
		'wpLicense'			=>	''
	));

	$html = curlIt(UPLOAD_SERVER.INDEX_URL."?title=Special:Upload&action=submit", true, $data, $localfile);
	if(preg_match("/var wgPageName = \"File:(.*)\"/", $html, $ray)){
		//Found File:Something - Probably worked for upload
		return true;
	}else{
		return false;
	}
}