#!/usr/bin/php
<?php
if (count($argv) != 2) {
    echo "usage: $argv[0] pagename\n";
    exit;
}

$wsdl = getenv('PHPWIKI_WSDL_URL');
if ($wsdl === false) {
    $wsdl = "http://phpwiki.fr/PhpWiki.wsdl";
}

try {
    $client = new SoapClient($wsdl);
} catch (SoapFault $fault) {
    die($fault->faultstring);
}

$phpwiki = getenv("HOME")."/.phpwiki";
if (!file_exists($phpwiki)) {
    $login = readline("Login: ");
    $password = readline("Password: ");
    $credentials = base64_encode($login.':'.$password);
    if ($fp = fopen($phpwiki, 'w')) {
        fprintf($fp, "%s:%s", $login, $password);
        fclose($fp);
        chmod($phpwiki, 0600);
    }
} else {
    $credentials = base64_encode(file_get_contents($phpwiki));
}

try {
    $pagemeta = $client->getPageMeta($argv[1], $credentials);
    foreach ($pagemeta as $key => $value) {
        echo "$key: $value\n";
    }
} catch (SoapFault $e) {
    echo 'Error: ' .  $e->getMessage() . "\n";
}
