Livre d'or
Livre d'or

la GazetteCommunauté
Que pensez-vous de Vikidia ? Venez mettre un message sur le livre d'or !

Vikidia:Achille/sources/wiki.php

Aller à : navigation, rechercher
Version 0.1.5 (c)
Ces pages sont données pour consultation. Si vous souhaitez utiliser Achille, téléchargez de préférence les sources au format zip


<?php
	/* gestion de Vikidia
	GPL Thomas Pimmel 2008
	*/
/*--------------------------------
Fonctions haut-niveau
----------------------------------*/
// connexion
function w_Login($username, $password)
{
	require ("includes/adrs.php");
	// fichier coockies
	$coock='users/'.$username.'.vikidia.txt';
	file_put_contents ($coock,"\n",LOCK_EX);
	// démo
	if ($_SESSION['status']==3)
	{
		$_SESSION['ctc']=true;
		return true;
	}
	// commande
	$username=urlencode($username);
	$url = $adrs['viki']['api']. '/api.php?action=login&lgname=' . $username . '&lgpassword=' . $password . '&format=php';
	$post="wpName=".$username."&wpPassword=".$password."&wpLoginattempt=true";
	$ret=w_Curl($url,$coock,$post);
	// Analyse réponse
	$ret = unserialize($ret);
	$result = $ret['login']['result'];
	if ($result != "Success")
	{
		Message ("Erreur lors de la connexion : $result<br>",1);
		return false;
	}
	// succès
	$_SESSION['ctc']=true;
        return	true;
}
//déconnexion
function w_Logout()
{
	require ("includes/adrs.php");
	if (!isset($_SESSION['ctc']))	return false;
	// démo
	if ($_SESSION['status']==3)
	{
		unset ($_SESSION['ctc']);
		return true;
	}
	$coock='users/'.$_SESSION['user'] . '.vikidia.txt';
	$url =  $adrs['viki']['api'].'/api.php?action=logout';
	w_Curl($url,$coock);
	unset ($_SESSION['ctc']);
	return true;
}
// lecture d'une page
function w_GetPage($article,$url=false)
{
	require ("includes/adrs.php");
	if (!$url)		$url=$adrs['viki']['api'];
	$article = rawurldecode($article);
	$article = rawurlencode($article);
	//$article=str_replace(" ","_",$article);
	$url = $url . '/api.php?action=query&prop=revisions&titles=' . $article . '&rvprop=content&format=php';
	$ret=w_Curl($url);
	if (!$ret)		return false;
	$recup=unserialize($ret);
	$recup=$recup['query'];
	foreach ($recup['pages'] as $retval)
	{
		if (isset($retval['revisions']))
		{
			foreach ($retval['revisions']	as $pipo)
			{
				return $pipo ["*"];
			}
		}
	}

	return false;
}
// liste des pages selon recherche
function w_Search($search,$namespace)
{
	require ("includes/adrs.php");
	$limit=100;
	if ($_SESSION['status']==3)
	{
		print ("<i>Démonstration, limitée à 10 pages</i><br />");
		 $limit=10;
	}
	$url=$adrs['viki']['api'];
	// nom de la page
	$search=rawurlencode($search);
	// Récup des modèles
	$url = $url . '/api.php?action=query&srnamespace='. $namespace
			. '&list=search&srwhat=text&srsearch="'.$search.'"&srlimit='.$limit.'&format=php';
	$getpage = w_Curl($url);
	$recup=unserialize($getpage);
	// mise sous forme de liste
	$n=0;
	$list=false;
	while (isset($recup['query']['search'][$n]))
	{
		$list[$n]=$recup['query']['search'][$n]['title'];
		//print ("$list[$n]<br>");
		$n++;
	}
	if (isset($recup['query-continue']))	message ("Attention, le nombre de réponses excède 100.");
	return $list;
}
// liste des pages d'une catégorie
function w_GetPagesCat($category,&$continue="",$limit=500,$url=false)
{
	require ("includes/adrs.php");
	// nom de la page
	$category=rawurldecode($category);
	$category=rawurlencode($category);
	// Récup des modèles
	if (!$url)	$url = $adrs['viki']['api'];
	$url=$url . '/api.php?action=query&list=categorymembers&cmtitle=Category:'.$category.'&cmlimit='.$limit.'&format=php';
	if ($continue)
	{
		$conti=rawurlencode($continue);
		$url=$url."&cmcontinue=".$conti;
	}
	$getpage=w_Curl($url);
	//print ($getpage);
	$recup=unserialize($getpage);
	// mise sous forme de liste
	$n=0;
	$list=false;
	while (isset($recup['query']['categorymembers'][$n]))
	{
		$list[$n]=$recup['query']['categorymembers'][$n]['title'];
		//print ("$list[$n]<br>");
		$n++;
	}
	if (isset($recup['query-continue']['categorymembers']['cmcontinue']))	$continue=$recup['query-continue']['categorymembers']['cmcontinue'];
	else	$continue="";
	return $list;
}
// chercher une information sur une page
function w_GetPageInfo ($page,$nature,$url=false)
{
	require ("includes/adrs.php");
	$page=rawurldecode($page);
	$page=rawurlencode($page);
	if (!$url)	$url=$adrs['viki']['api'];
	$request = $url . '/api.php?action=query&titles='.$page.'&prop='.$nature.'&format=php';
	$result = w_Curl($request);
	if (!$result)	return false;
	$result=unserialize($result);
	$pipo=$result['query']['pages'];
	$pipo = array_shift($pipo);
	$pipo = array_shift($pipo);
	$rawlist = $result['query']['pages'][$pipo][$nature];

	if ($nature=='extlinks')	$idx='*';
	else				$idx='title';
	// mise sous forme de liste
	$n=0;
	unset ($list);
	while (isset($rawlist[$n]))
	{
		$list[$n]=$rawlist[$n][$idx];
		//print ("$list[$n]<br>");
		$n++;
	}
	return $list;
}
// chercher si existe
function w_GetExist($page,$url=false)
{
	require ("includes/adrs.php");
	//print ("Test $page<br>");
	$page=rawurldecode($page);
	$page=rawurlencode($page);
	if (!$url)	$url=$adrs['viki']['api'];
	$request = $url . '/api.php?action=query&titles='.$page;
	$result = w_Curl($request);
	if (!$result)	return false;
	$missing=strpos($result,'missing');
	if (!is_int($missing))	return true;
	else			return false;
}

// parser une variable
function w_Parse($text)
{
	require ("includes/adrs.php");
	$text=rawurlencode($text);
	//$coock='users/'.$_SESSION['user'] . '.vikidia.txt';
	$url=$adrs['viki']['api']. "/api.php?action=parse&text=" . $text . "&format=php&prop=text";
	$response=w_Curl($url);
	if (!$response)	return false;
	$response=unserialize($response);
	$response=$response['parse']['text']['*'];
	$response=str_ireplace("src=\"/images/","src=\"".$adrs['viki']['idx']."/images/",$response);
	return $response;
}
// chercher tous les fichiers
function w_GetAllFiles(&$from,$limit,$namespace)
{
	require ("includes/adrs.php");
	if ($_SESSION['status']==3 && $limit>10)
	{
		print ("<i>Démonstration, limitée à 10 pages</i><br />");
		 $limit=10;
	}
	$from=rawurlencode($from);
	$url = $adrs['viki']['api']. '/api.php?action=query&list=allpages&apnamespace='.$namespace
					.'&apfrom='. $from . '&aplimit='.$limit.'&format=php';
	$page = w_Curl($url);
	if (!$page)	return false;
	$page=unserialize($page);
	// mise sous forme de liste
	$n=0;
	$list=false;
	while (isset($page['query']['allpages'][$n]))
	{
		$list[$n]=$page['query']['allpages'][$n]['title'];
		//print ("$list[$n]<br>");
		$n++;
	}
	$from=false;
	if (isset($page['query-continue']['allpages']['apfrom']))	$from=$page['query-continue']['allpages']['apfrom'];
	return $list;
}

// Chercher modifs récentes
function w_GetRC ($what,$stamp)
{
	require ("includes/adrs.php");
	$limit=500;
	if ($_SESSION['status']==3)
	{
		print ("<i>Démonstration, limitée à 10 pages</i><br />");
		 $limit=10;
	}
	switch ($what)
	{
		case "nouvelles images":
			$addurl='&rctype=log&rcnamespace=6&rcprop=title';
			break;
		case "nouveaux articles":
			$addurl='&rctype=new&rcnamespace=0&rcprop=title';
			break;
		case "articles édités":
			$addurl='&rctype=edit&rcnamespace=0&rcprop=title';
			break;
		default:
			return false;
	}
	$request=$adrs['viki']['api'] . '/api.php?action=query&list=recentchanges&format=php'.$addurl.'&rclimit='.$limit.'&rcend='.$stamp;
	$result = w_Curl($request);
	if (!$result)	return false;
	$result=unserialize($result);
	if (!$result['query']['recentchanges'])	return 0;
	return $result['query']['recentchanges'];
}
// lire sa page de discussion
function w_ReadMail($parse=false,$prefix="Discussion_Utilisateur:",$post="",$user="")
{
	if ($_SESSION['status']==3)
	{
		if ($parse)	message ("Fonction désactivée");
		return ("<i>Mode démonstration</i>");
	}
	require ("includes/adrs.php");
	$coock='users/'.$_SESSION['user'] . '.vikidia.txt';
	if (!$user)
	{
		$user=str_replace(" ", "_", $_SESSION['user']);
		$user=rawurlencode($user);
	}
	if (!$parse) $url = $adrs['viki']['idx'] . '/index.php/' . $prefix . $user . $post;
	else $url=$adrs['viki']['api']. "/api.php?action=parse&page=" . $prefix . $user. $post . "&format=php&prop=text";
	$response=w_Curl($url,$coock);
	if ($response && $parse)
	{
		$recup=unserialize($response);
		$response=$recup['parse']['text']['*'];
		$response=str_ireplace("src=\"/images/","src=\"".$adrs['viki']['idx']."/images/",$response);
	}
	return $response;
}
// ajouter à une page une section
function w_AddToPage($page,$editsum,$newtext,$minor,$botedit)
{
	if ($_SESSION['status']==3)
	{
		print ("<i>Simulation d'écriture $page</i><br />");
		return true;
	}
	require ("includes/adrs.php");
	$coock='users/'.$_SESSION['user'] . '.vikidia.txt';
	$page=rawurlencode($page);
	$url = $adrs['viki']['idx'] . '/index.php?action=edit&section=new&title=' . $page;
	$response=w_Curl($url,$coock);
	$newsection=w_Value($response,"wpSection");
	$starttime=w_Value($response,"wpStarttime");
	$edittime=w_Value($response,"wpEdittime");
	$autosum=w_Value($response,"wpAutoSummary");
	$token=w_Value($response,"wpEditToken");
	//echo $response;
	// test nouveaux messages
	$usernospace=str_replace(" ", "_", $_SESSION['user']);
	$match='<a href="/index.php?title=Discussion_Utilisateur:' . $usernospace . '&redirect=no" title="Discussion Utilisateur:'
		.$_SESSION['user'] . '">nouveaux messages</a>';
	if (strpos($response,$match))
	{
		message ("Le robot a reçu du courrier. Arrêt !",1);
		$retour=w_OwnMsg("Arrêt (message reçu)");
		$_SESSION['log']['result']="Arrêt (message reçu)";
		return false;
	}
	//return true;	// DEBUG !!!!
	$url = $adrs['viki']['idx']. "/index.php?title=" . $page . "&action=submit". "&bot=$botedit";
	$postData['wpScrolltop'] = '';
	$postData['wpSection']=$newsection;
	$postData['wpEditToken']=$token;
	$postData['wpAutoSummary']=$autosum;
	$postData['wpSummary']=$editsum;
	$postData['wpTextbox1']=$newtext;
	$postData['wpSave']="Save page";
	$postData['wpStarttime']=$starttime;
	$postData['wpEdittime']=$edittime;
	if ($minor) $postData['wpMinoredit']=$minor;
	$return=w_Curl($url,$coock,$postData);
	if (!$return)
	{
		sleep(EDIT_DELAY);
		return true;
	}
	else
	{
		print ("$return<br>");
		return false;
	}
}
// ajouter une section
function w_OwnMsg($nature)
{
	require ("includes/adrs.php");
	$coock='users/'.$_SESSION['user'] . '.vikidia.txt';
	$page = rawurlencode("Discussion_Utilisateur:".$_SESSION['user']);
	w_ReadMail();	//pour enlever message reçu !
	$url =  $adrs['viki']['idx']. '/index.php?action=edit&section=new&title=' . $page;
	$response=w_Curl($url,$coock);
	$newsection=w_Value($response,"wpSection");
	$starttime=w_Value($response,"wpStarttime");
	$edittime=w_Value($response,"wpEdittime");
	$autosum=w_Value($response,"wpAutoSummary");
	$token=w_Value($response,"wpEditToken");

	$url = $adrs['viki']['idx'] . '/index.php?title=' . $page . '&action=submit'. "&bot=1";
	$postData['wpScrolltop']='';
	$postData['wpSection']=$newsection;
	$postData['wpEditToken']=$token;
	$postData['wpAutoSummary']=$autosum;
	$postData['wpSummary']='';
	$postData['wpTextbox1']=":$nature. ~~~~";
	$postData['wpSave']="Save page";
	$postData['wpStarttime'] = $starttime;
	$postData['wpEdittime'] = $edittime;
	$return=w_Curl($url,$coock,$postData);
	if (!$return)
	{
		return true;
	}
	else
	{
		print ("$return<br>");
		return false;
	}
}
// écrire une page
function w_PutPage($page,$editsum,$newtext,$minor,$botedit)
{
	if ($_SESSION['status']==3)
	{
		print ("<i>Simulation d'écriture $page</i><br />");
		return true;
	}
	require ("includes/adrs.php");
	$coock='users/'.$_SESSION['user'] . '.vikidia.txt';
	$page = urlencode($page);
	$url = $adrs['viki']['idx'] . '/index.php?action=edit&title=' . $page;
	$response=w_Curl($url,$coock);
	$starttime=w_Value($response,"wpStarttime");
	$edittime=w_Value($response,"wpEdittime");
	$autosum=w_Value($response,"wpAutoSummary");
	$token=w_Value($response,"wpEditToken");
	// test nouveaux messages
	$usernospace=str_replace(" ", "_", $_SESSION['user']);
	$match='<a href="/index.php?title=Discussion_Utilisateur:' . $usernospace . '&redirect=no" title="Discussion Utilisateur:'.$_SESSION['user'] . '">nouveaux messages</a>';
	//print ($match);
	if (strpos($response,$match))
	{
		message ("Le robot a reçu du courrier. Arrêt !",1);
		$retour=w_OwnMsg("Arrêt (message reçu)");
		$_SESSION['log']['result']="Arrêt (message reçu)";
		return false;
	}
	$url = $adrs['viki']['idx'] . '/index.php?title=' . $page . '&action=submit' . "&bot=$botedit";

	$postData['wpScrolltop']='';
	$postData['wpSection']='';
	$postData['wpEditToken']=$token;
	$postData['wpAutoSummary']=$autosum;
	$postData['wpSummary']=$editsum;
	$postData['wpTextbox1']=$newtext;
	$postData['wpSave']="Save page";
	$postData['wpStarttime']=$starttime;
	$postData['wpEdittime']=$edittime;
	if ($minor) $postData['wpMinoredit']=$minor;
	$return=w_Curl($url,$coock,$postData);
	if (!$return)
	{
		sleep(EDIT_DELAY);
		return true;
	}
	else
	{
		print ("$return<br>");
		return false;
	}
}
// écrire une page
function wiki_PutImage($imagefile,$imagename,$imagedesc,$minor,$botedit)
{
	if ($_SESSION['status']==3)
	{
		print ("<i>Simulation d'écriture $imagename</i><br />");
		return true;
	}
	require ("includes/adrs.php");
	$coock='users/'.$_SESSION['user'] . '.vikidia.txt';
	$url=$adrs['viki']['idx'] . '/index.php?title=Special:T%C3%A9l%C3%A9chargement&action=submit'."&bot=$botedit";
	$postData['wpUploadFile']='@'.$imagefile;
	//print ("$imagename--<br>");
	$postData['wpDestFile']=$imagename;
	$postData['wpUploadDescription']=$imagedesc;
	$postData['wpIgnoreWarning']=true;
	$postData['wpUpload']="Upload file";
	if ($minor != null) { $postData['wpMinoredit'] = $minor; }
	$return=w_Curl($url,$coock,$postData);
	if (!$return)
	{
		//sleep(EDIT_DELAY);
		return true;
	}
	else
	{
		print ("$return<br>");
		return false;
	}
}
/*--------------------------------
Fonctions bas-niveau
----------------------------------*/
// usage de curl
function w_Curl ($url, $coock=false, $post=false)
{
	global $proxyaddr;
	//print ("<b>$url</b></br>");
	// init et exécution
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	if ($proxyaddr) curl_setopt($ch, CURLOPT_PROXY, $proxyaddr);
	if ($post)
	{
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
	}
	if ($coock)
	{
		curl_setopt ($ch, CURLOPT_COOKIEJAR, $coock);
		curl_setopt ($ch, CURLOPT_COOKIEFILE, $coock);
	}
	$response = curl_exec($ch);
	// traitement réponse
	if(curl_errno($ch))
	{
		$err=curl_error($ch);
		Message ("Erreur : $err<br>",1);
		$_SESSION['log']['result']="Erreur : $err";
		$response=false;
	}
	curl_close($ch);
	return $response;
}
// recherche d'une valeur dans une page html
function w_Value($page,$variable)
{
	// recherche partie formulaires
	$idx=strpos($page,"<form");
	if (!is_int($idx))	return false;	// non-trouvé
	// recherche de la variable
	$var=strpos($page,$variable,$idx);
	if (!is_int($var))	return false;	// non-trouvé
	// recherche du début
	$deb=$idx;
	while (is_int($deb) && $deb<$var)
	{
		$start=$deb++;
		$deb=strpos($page,"<input",$deb);	
	}
	// recherche de la valeur
	$value=strpos($page,"value=",$start);
	if (!is_int($value))	return false;
	$value=$value+6;
	$ns=$page[$value++];				// guillemet début
	$valend=strpos($page,$ns,$value);		// on trouve la fin
	$found=substr($page,$value,$valend-$value);
	//print ("Found : $found<br>");
	return $found;
}
?>