<?php
// Web service to add pages to the wiki.
// Version: 1.0.1
 
// Customization required:
$wiki_root_url="http://wiki.swelab.info/selabwiki";  # Base URL of your wiki
include "Snoopy-1.2.3/Snoopy.class.php";  # Location of the Snoopy class
$debug_file_name "/tmp/addpage_debug.txt";
$strict_title_rules true;
// End of customization section
 
require_once ('SOAP/Server.php');
require_once (
'SOAP/Disco.php');
 
class 
AddPageService
    
{
    var 
$__dispatch_map = array();
 
    function 
AddPageService()
        {
        
// Define the signature of the dispatch map on the Web services method
        // Necessary for WSDL creation
        
$this->__dispatch_map['addPage']=array
            (
            
'in'  => array
                (
                
'title'    => 'string',
                
'text'     => 'string',
                
'user'     => 'string',
                
'password' => 'string'
                
),
            
'out' => array('result' => 'string'),
            );
        }
 
    function 
addPage($title$text$user$password)
        {
        
// Validate parameters
        
if (empty($title))
            return 
"Title is required.";
    ... 
        
// Fill web form
        
$result=AddPageService::fillForm($title$text$user$password);
        return 
$result;
        }
 
    private static function 
fillForm($title$text$user$password)
        {
        global 
$wiki_root_url;
        global 
$debug_file_name;
        
$login_url=$wiki_root_url "/index.php?title=Special:Userlogin&action=submitlogin";
        
# Set the username and password below:
        
$login_vars['wpName']=$user;
        
$login_vars['wpPassword']=$password;
        
$login_vars['wpRemember']="0";
        
# Login to Wiki
        
$snoopy = new Snoopy;
        if (!
$snoopy->submit($login_url$login_vars))
            return 
"Error submiting login form.";
        if (
strpos($snoopy->results'errorbox') !== false) {
            if (!empty(
$debug_file_name)) {
                
$fp fopen($debug_file_name"w");
                
fwrite($fp$editpage);
                
fclose($fp);
            }        
            return 
"Login error.";
        }
        
# Submit to edit page for $title and get contents into $editpage
        
$url=$wiki_root_url ."/index.php?title=" $safetitle "&action=edit";
        if (!
$snoopy->fetch($url))
            return 
"Error fetching edit form.";
        
$editpage=$snoopy->results;
        
# Pick out Edit Token
        
$ans=preg_match('/.*value="(.*?)".*name="wpEditToken"/'$editpage$matches);
        if (empty(
$matches[1]))
            return 
"No edit token on the web form answer.";
        
$edit_token=$matches[1];
        
# Set Post Variables before submitting
        
$text                      =trim($text);
        
$submit_vars['wpTextbox1'] =$text;
        
$submit_vars['wpStarttime']=gmdate('YmdHis'time());
        
$submit_vars['wpSave']     ="Save page";
        
$submit_vars['wpEditToken']=$edit_token;
        
# Submit or Post to create the page
        
if (!$snoopy->submit($wiki_root_url "/index.php?title=" $safetitle "&action=submit"$submit_vars))
            return 
"Error submiting filled form.";
        if (
strpos($snoopy->results'wpTextbox1') !== false)
            return 
"Error saving the web form.";
 
        
# report success
        
return "OK";
        }
 
    static function 
main()
        {
        global 
$_SERVER$HTTP_RAW_POST_DATA;
        
$server    =new SOAP_Server();
        
$webservice=new AddPageService();
        
$server->addObjectMap($webservice'http://schemas.xmlsoap.org/soap/envelope/');
 
        if (isset(
$_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST')
            {
            
$server->service($HTTP_RAW_POST_DATA);
            }
        else
            {
            
$disco=new SOAP_DISCO_Server($server'AddPageService');
            
header ("Content-type: text/xml");
 
            if (isset(
$_SERVER['QUERY_STRING']) && strcasecmp($_SERVER['QUERY_STRING'], 'wsdl') == 0)
                {
                echo 
$disco->getWSDL();
                }
            else
                {
                echo 
$disco->getDISCO();
                }
            }
        }
    }
 
AddPageService::main();
?>