<?
require_once('functions.phps');
session_start();
$debug='';

//if (!isset($contents)) $contents=array();
//session_register("contents");
if (!isset($_SESSION['contents'])) $_SESSION['contents']=array();
$contents=$_SESSION['contents'];
foreach (array(
'char''html''delfirst''dellast''delall''update1''update2''update3''update4''update5''addmulti''add''u8hex''u8url') as $v) {
//    if (isset($G[$v])) $$v=$G[$v];
    
if (isset($_REQUEST[$v])) $$v=$_REQUEST[$v];
    else unset($
$v);
}

if (isset(
$delfirst)) { array_shift($contents); }
elseif (isset(
$dellast)) { array_pop($contents); }
elseif (isset(
$delall)) { $contents=array(); }
elseif (isset(
$update1)) { // Update from the 'real chars' textarea
    
$contents=array();
    
$contents=Utf8StringToCodepoints($char);
} elseif (isset(
$update2)) { // Update from the 'html entity' version
    
$html2=$html;
    
$contents=array();
    
$mult=preg_match_all('/&#(\d+);/'$html$matchesPREG_SET_ORDER);
    foreach (
$matches as $m) {
        
$html2=preg_replace('/&#'.$m[1].';/'Codepoint2Utf8($m[1]), $html2);
    }
    
$contents=Utf8StringToCodepoints($html2);
} elseif (isset(
$update3)) { // Update from the 'UTF8 Hex' version
    
$u8hex=ass(trim($u8hex));
    
$tmp=Utf8StringToCodepoints(rawurldecode(Utf8HexToUrl($u8hex)));
    if (
is_array($tmp) && count($tmp)) $contents=$tmp;
    else 
$debug .= "Invalid UTF-8 hex-form entry.";
} elseif (isset(
$update4)) { // Update from the 'UTF-8 URL form' version
    
$u8url=ass(trim($u8url));
    
$tmp=Utf8StringToCodepoints(rawurldecode($u8url));
    if (
is_array($tmp) && count($tmp)) $contents=$tmp;
    else 
$debug .= "Invalid UTF-8 URL-form entry.";
//} elseif (isset($update5)) { // Update from the 'Plain URL-encoded' version
//    $urlenc=ass(trim($urlenc));
//    $contents=preg_split('//', rawurldecode($urlenc));
//    $contents=array($tmp);
} elseif (isset($addmulti)) {
    
// It expects to receive a :-delimited list of hex codepoints to add to contents[].
    
$tmp=preg_split('/:/'$addmulti, -1PREG_SPLIT_NO_EMPTY);
    foreach (
$tmp as $h$contents[]=$h;
} elseif (
$add) {
    
// It expects a SINGLE codepoint to be sent.
    
$contents[]=$add;
}

$_SESSION['contents']=$contents;

$htmlcontents=$charcontents=$u8hexcontents=$urlcontents='';
foreach (
$contents as $C) {
    
$htmlcontents .= "&amp;#$C;";
    
$charcontents .= "&#$C;";
    
$u8hexcontents .= Codepoint2Utf8Hex($C) . ' ';
//    $urlcontents .= rawurlencode($C) . ' ';
}
if (
$u8hexcontents) {
    
$u8urlcontents=Utf8HexToUrl($u8hexcontents);
    
$u8phpcontents=Utf8HexToPhpString($u8hexcontents);
    
$u8littlecontents=MakeEndianString($u8hexcontents'l');
    
$u8bigcontents=MakeEndianString($u8hexcontents'b');
}
?>
<html>
<head>
<? require('meta.phps'); ?>
<title>Li'l Unisearch Clipboard Helper</title>
<style type="text/css">
* {
    font-family: helvetica,arial,verdana,sans,univers;
}
.pophtml, INPUT {
    border: 1px solid;
    border-color: #FF99FF;
    background-color: #FFEEFF;
    color: #000000;
    font-size: 10pt;
}
.popchar {
    border: 1px solid;
    border-color: #FF99FF;
    background-color: #FFEEFF;
    color: #000000;
    font-size: 16pt;
}
.error {
    border: 1px solid #CC88DD;
    background-color: #EECCFF;
    color: #CC0000;
    font-size: 11pt;
    font-weight: bold;
    padding: 3px;
}
</style>
</head>
<body>
<h3>The Li'l Unisearch Clipboard Helper</h3>
<? if ($debug) echo "<P><span class=\"error\">$debug</span></P>\n"?>
<form method="POST" action="<?=$PHP_SELF;?>" name="theForm">
<input type="submit" name="delfirst" value=" Delete First Char ">
<input type="submit" name="dellast" value=" Delete Last Char ">
<input type="submit" name="delall" value=" Clear ">
<input type="submit" value=" Refresh ">
<input type="button" value=" Close Window " onClick="Javascript: window.close()">
<BR><BR>Character Version: <input type="submit" name="update1" value=" Update From This Clipboard: "><BR>
<textarea class="popchar" name="char" rows="2" cols="40" wrap="virtual"><?=$charcontents;?></textarea>
<BR><BR>HTML entity codes version: <input type="submit" name="update2" value=" Update From This Clipboard: "><BR>
<textarea class="pophtml" name="html" rows="2" cols="76" wrap="virtual"><?=$htmlcontents;?></textarea>
<!--
<BR><BR>URL-encoded version: <input type="submit" name="update5" value=" Update From This Clipboard: "><BR>
<textarea class="pophtml" name="urlenc" rows="2" cols="76" wrap="virtual"><?=$urlcontents;?></textarea>
-->
<BR><BR>UTF8 (Hexadecimal): <input type="submit" name="update3" value=" Update From This Clipboard: "><BR>
<textarea class="pophtml" name="u8hex" rows="2" cols="76" wrap="virtual"><?=$u8hexcontents;?></textarea>
<BR><BR>UTF8 (URL form): <input type="submit" name="update4" value=" Update From This Clipboard: "><BR>
<textarea class="pophtml" name="u8url" rows="2" cols="76" wrap="virtual"><?=$u8urlcontents;?></textarea>
<BR><BR>UTF8 (Scripting-language string):<BR>
<textarea class="pophtml" name="u8php" rows="2" cols="76" wrap="virtual"><?=$u8phpcontents;?></textarea>
<BR><BR>UTF8 (Little-endian Binary in hex):<BR>
<textarea class="pophtml" name="u8little" rows="2" cols="76" wrap="virtual"><?=$u8littlecontents;?></textarea>
<BR><BR>UTF8 (Big-endian Binary in hex):<BR>
<textarea class="pophtml" name="u8big" rows="2" cols="76" wrap="virtual"><?=$u8bigcontents;?></textarea>
</form></body></html>