From OrganicDesign Wiki
<?
# ENTRY CONDITIONS
# HTML STRUCTURE NAVIGATOR
# HTML ENVIRONMENT
# HTML PAGE RENDER
# HTML PAGE FUNCTIONS
# WINDOZE SHORTCUT FUNCTIONS
# NODAL FUNCTIONS
# UTILITY FUNCTIONS
# SHORTCUT DEBUGGING FUNCTIONS
# SWF COMPILE
# SWF DYNAMIC FUNCTIONS
# PAST COMMENTS
$ROOT=dirname($_SERVER['SCRIPT_FILENAME']).'/';
$QS='';
$CWD='/';
#########################################################################################################################
# ENTRY CONDITIONS
# SWF
# Outputs as QS for SWF and exits, otherwise continues and returns HTML for browser
if (isset($HTTP_GET_VARS['swf'])) {
# if no message from SWF interface, then return HTML to launch it
if (false) {
?><html>
<head>
<title>OpenWidnow</title>
<script language="javascript">
window.open('./interface.swf','OpenWindow','width='+(screen.width-10)+',height='+(screen.height-30)+',screenx=0,screeny=0,directories=0,fullscreen=1,location=0,menubar=0,scrollbars=0,status=0,toolbar=0');
</script>
</head>
<body>Loading OpenWindow...</body>
</html><?
exit;
}
}
# REBOOT Manual reboot (these commands would normally be initiated by IF (HTML/SWF)
# COMPILE Manual recompile
# SHELL Manual execution of a shell command
if (isset($HTTP_GET_VARS['shell'])) {
$shell=$HTTP_GET_VARS['shell'];
$file='shell'.md5(uniqid("")).'.bat';
$bat=str_replace('/',"\\","del \"$ROOT*.bat\"\n\"$shell\"");
if (!$handle = fopen($file, 'w')) echo "Cannot open file ($file)";
else {
if (fwrite($handle,$bat) === FALSE) echo "Cannot write to file ($file)";
fclose($handle);
}
header("Location: http://127.0.0.1/my organisation/$file");
exit;
}
#########################################################################################################################
# HTML STRUCTURE NAVIGATOR
# (defaults to an HTML renderer if no GET vars)
?>
<html>
<style type="text/css">
td
{
font-family: tahoma;
font-size: 9pt;
vertical-align: top;
}
a { text-decoration: none; }
</style>
<body bgcolor="#cccccc" link="black" vlink="black">
<?
#########################################################################################################################
# HTML ENVIRONMENT
# Pref content
# each folder can have a .pref folder specifying:
# content_before, content_after, child_before, child_after
# those prefs are stored in this hash
# prefs are PHP
# default container
$prefs['default']['content_before']='
print "<table width=100% cellpadding=0 cellspacing=0>\n";
$_bg=true;
';
$prefs['default']['content_after']='print "</table>\n";';
$prefs['default']['child_before']='
$_bg=!$_bg;
$_bg?$_bgcol="#e0e0e0":$_bgcol="#f0f0f0";
print "<tr><td bgcolor=\"$_bgcol\"> ";
';
$prefs['default']['child_after']='print "</td></tr>\n";';
# House-keeping (scan structure)
#scanTree('');
# Assumes QS is CWD if only one valueless item exists
$qscwd=false;
if (count($HTTP_GET_VARS)==1)
foreach ($HTTP_GET_VARS as $k=>$v)
if ($v=='') {
# Find referenced resource
$target=urldecode($_SERVER['QUERY_STRING']);
if (($CWD=searchTree('',$target))===false) {
print "<b>Could not resolve target in query string: ($target)</b><br>";
$CWD='/';
}
$qscwd=true;
}
# Read GET into local hash paths
if (!$qscwd) foreach ($HTTP_GET_VARS as $k=>$v) {
if (strtoupper($k)=='CWD') $CWD=$v;
# match submit to set page to render and code to execute
# match
}
# header, read POST into local hash paths
foreach ($HTTP_POST_VARS as $k=>$v) {
# match submit to set page to render and code to execute
# match
print "$k = $v<br>\n";
}
# Convert the independent part of the hash into QS so it can be referenced via URL
# i.e. all dependent data (eg. session-specific, wizard parts, submissions, errors etc
# should be in their own dynamic-data areas of the hash.
# update global $qs;
# Final CWD validation
if (file_exists("$ROOT$CWD")) {
if (is_dir("$ROOT$CWD")&&ereg('[^/]$',$CWD)) $CWD="$CWD/";
}
else $CWD='/';
$QS['cwd']=$CWD;
#########################################################################################################################
# HTML PAGE RENDER
?>
<center><h2>Render Title here</h2></center>
<form method="POST" action="<?print buildURL(array('cwd'=>$CWD));?>">
<hr>
<?
# Read .pref file
if (file_exists("$ROOT/$CWD.pref")) $pref=file("$ROOT$CWD.pref"); else $pref='default';
# render container_before
eval($prefs[$pref]['content_before']);
# Render address
eval($prefs[$pref]['child_before']);
print "<br> ";
print "<a href=\"".buildURL(array('cwd'=>'/'))."\">MY ORGANISATION / </a>";
$path='';
foreach (split('/',$CWD) as $i) if ($i) {
$path.="/$i";
print "<a href=\"".buildURL(array('cwd'=>"$path/"))."\">$i / </a>";
}
print "<br><br>";
eval($prefs[$pref]['child_after']);
# Read dir and render
if ($handle = opendir("$ROOT$CWD")) {
while (false !== ($target = readdir($handle))) if (ereg('^[^.]',$target)) {
#$name = ereg_replace('^!* *','',$target);
$name=$target;
# If target is a shortcut, change to real target
if (eregi("^(.+)\\.lnk$",$target,$m)) {
$target=readShortcut("$CWD$target");
$target=str_replace("\\",'/',$target);
$target=eregi_replace('^.+my organisation/','',$target);
$name=$m[1]." ->";
}
else $target="$CWD$target";
eval($prefs[$pref]['child_before']);
print "<input type=checkbox> \n";
if (file_exists("$ROOT$target")) {
if (is_dir("$ROOT$target")) print "<a href=\"".buildURL(array('cwd'=>"$target/"))."\">$name</a>\n";
else print "<a href=\"".buildURL(array('shell'=>"$ROOT$target"))."\">$name</a>\n";
} else print "<b>Broken link:</b> $name $target";
eval($prefs[$pref]['child_after']);
}
}
closedir($handle);
# render container_after
eval($prefs[$pref]['content_after']);
?>
<hr>
<input type="submit" name="mk" value=" Create ">
<input type="submit" name="rn" value=" Rename ">
<input type="submit" name="rm" value=" Delete ">
</form>
</body>
</html>
<?
#########################################################################################################################
# HTML PAGE FUNCTIONS
# Page 1
function page1() {
# Page 1 Code
# if invalid input go back (change pages to functions)
?>
<!-- Page 1 Form -->
<input type="text" <?insert_name_and_value('test_input');?>"><br>
<input type="submit" name="__prev_1" value=" Prev > ">
<input type="submit" name="__next_1" value=" Next > ">
<?
}
# These should be in include
function insert_name_and_value($input_name) {
}
#########################################################################################################################
# WINDOZE SHORTCUT FUNCTIONS
# Create a shortcut file
function createShortcut($shortcut, $target, $description) {
if (!ereg("^([A-Za-z]):[\\](.+)$",$target,$m)) return;
$drive=$m[1];
$target=$m[2];
# Create shortcut data structure
$lnk = "\x4c\x00\x00\x00"; // L
$lnk .= "\x01\x14\x02\x00\x00\x00\x00\x00\xc0\x00\x00\x00\x00\x00\x00\x46"; // GUID
$lnk .= "\x07\x00\x00\x00"; // Flags (bits: 0=itemlist 1=file/folder 2=description 3=relative path)
$lnk .= "\x10\x00\x00\x00"; // File attributes
$lnk .= "\x00\x00\x00\x00\x00\x00\x00\x00"; // Time 1
$lnk .= "\x00\x00\x00\x00\x00\x00\x00\x00"; // Time 2
$lnk .= "\x00\x00\x00\x00\x00\x00\x00\x00"; // Time 3
$lnk .= "\x00\x00\x00\x00"; // File length
$lnk .= "\x00\x00\x00\x00"; // Icon number
$lnk .= "\x01\x00\x00\x00"; // normal window
$lnk .= "\x00\x00\x00\x00"; // Hotkey
$lnk .= "\x00\x00\x00\x00"; // reserved
$lnk .= "\x00\x00\x00\x00"; // reserved
# Item id list (just copy dump for now)
$lnk .= intToWord(0x3e+strlen($target));
$lnk .= "\x14\x00"; # First item (?)
$lnk .= "\x1f\x0f\xe0\x4f";
$lnk .= "\xd0\x20\xea\x3a";
$lnk .= "\x69\x10\xa2\xd8";
$lnk .= "\x08\x00\x2b\x30";
$lnk .= "\x30\x9d";
$lnk .= "\x19\x00";
$lnk .= "\x23$drive\x3a\x5c"; #Drive
$lnk .= "\x00\x00\xb0\x0c";
$lnk .= "\xc1\x00\x00\x00";
$lnk .= "\x00\x00\x50\x42";
$lnk .= "\x42\x00\x00\x00";
$lnk .= "\x00\x71\x65";
$lnk .= intToWord(0xf+strlen($target));
$lnk .= "\x31\x00\x00\x00";
$lnk .= "\x00\x00\x56\x31";
$lnk .= "\xf2\x2c\x10\x00";
$lnk .= "$target\x00"; #Path
$lnk .= "\x00\x00";
$lnk .= "\x30\x00\x00\x00"; # Offset to description
# File location info (offsets are from start of this struct)
$lnk .= "\x2c\x00\x00\x00"; // Total length
$lnk .= "\x1c\x00\x00\x00"; // this blocks length (always &1c)
$lnk .= "\x01\x00\x00\x00"; // Flags (local volume only)
$lnk .= "\x1c\x00\x00\x00"; // Offset to local volume table
$lnk .= "\x00\x00\x00\x00";
$lnk .= "\x00\x00\x00\x00"; // Offset to network volume table
$lnk .= "\x00\x00\x00\x00";
# Local volume table
$lnk .= "\x18\x00\x00\x00"; // Length of table
$lnk .= "\x03\x00\x00\x00"; // fixed disk
$lnk .= "\x00\x00\x00\x00"; // Volume serial
$lnk .= "\x00\x00\x00\x00"; // Offset to volume label
# Description String
$lnk .= intToWord(strlen($description)); // Length of string
$lnk .= $description;
# Extra
$lnk .= "\x00\x00\x00\x00"; // End
# Write shortcut to file
if (!$handle = fopen($shortcut, 'w')) echo "Cannot open file ($filename)";
else {
if (fwrite($handle, $lnk) === FALSE) echo "Cannot write to file ($filename)";
fclose($handle);
}
}
# Extract target informaton from a shortcut
function readShortcut($shortcut) {
global $ROOT;
# Read in shortcut binary file
$bin=implode('',file("$ROOT$shortcut"));
$target='';
# Count blocks in item_id_list
$ptr=0x4e; $len=1;
while ($len>0) {
$len=ord(substr($bin,$ptr,1))+256*ord(substr($bin,$ptr+1,1));
$ptr+=$len;
}
$ptr+=2;
$descrip='';
$flags=ord(substr($bin,0x14,1));
if ($flags&4) {
$doffset=ord(substr($bin,$ptr,1))+256*ord(substr($bin,$ptr+1,1));
$dptr=$ptr+$doffset;
$dlen=ord(substr($bin,$dptr,1))+256*ord(substr($bin,$dptr+1,1));
$descrip=substr($bin,$dptr+2,$dlen);
# test if fatware's mangled it and return with target hacked from description - fuckin fatware!
if (substr($descrip,0,2)=="[\x00") {
$descrip=substr($bin,$dptr+3,$dlen*2-4);
$target=$ROOT.str_replace("\x00","",$descrip);
}
else if (substr($descrip,0,1)=="[") {
# one of ours
$target=$root.substr($descrip,1,strlen($descrip)-2);
print "Ours - $target<br>";
}
$ptr+=4;
}
if ($target=='') {
# It's not one of ours - just extract all strings and pick best
preg_match_all("/[ ^!@$^&\\[\\]()\\-_\\\\\\/+=~,.{}0-9a-zA-Z]{4,}\\x00/",$bin,$m);
foreach ($m[0] as $n) {
$n=str_replace("\x00","",$n);
if (ereg(":",$n)) {if (ereg("^.:",$n)) $target.=$n;}
else if (ereg("\\\\",$n)&&!ereg("\\\\\\\\",$n)) $target.=$n;
}
}
# Validate extracted info
if ($target=='') {
print "<br><b>Couldn't extract target from $shortcut</b><br>";
foreach ($m[0] as $n) print "$n<br>";
}
else if (!file_exists($target)) {
#print "<br><b>Broken link found:</b><br>$shortcut<br>-->$target<br>";
# Attempt to repair broken link
if (ereg("(\\\\[^\\\\]+\\\\[^\\\\]+)$",$target,$m)) {
if (file_exists($ROOT.$m[1])) {
$target=str_replace("/","\\",$target);
print "Repairing:$target<br>";
print $target;
createShortcut("$ROOT$shortcut",$target,'['.$m[1].']');
}
}
}
return $target;
}
#########################################################################################################################
# NODAL FUNCTIONS
#########################################################################################################################
# UTILITY FUNCTIONS
function quadNullString($s) {
$l = strlen($s)+1;
if (($nulls=($l%4))>0) $nulls=4-$nulls;
return $s.str_repeat("\x00",$nulls+1);
}
function intToDWord($i) {
return chr($i&0xff).chr(($i&0xff00)>>8).chr(($i&0xff0000)>>16).chr(($i&0xff000000)>>24);
}
function intToWord($i) {
return chr($i&0xff).chr(($i&0xff00)>>8);
}
# Recursively scan dir structure and apply house-keeping rules
function scanTree($dir) {
global $ROOT;
if ($handle = opendir("$ROOT$dir")) {
while (false !== ($name = readdir($handle))) {
if (!ereg('^[.]',$name)) {
if (is_dir("$dir/$name")) scanTree("$dir/$name");
#echo "$dir/$name<br>";
if (ereg('.lnk$',$name)) readShortcut("$dir/$name");
}
}
}
closedir($handle);
}
# Recursively search structure for a file
function searchTree($dir,$target) {
global $ROOT;
$target=strtoupper($target);
if ($handle = opendir("$ROOT$dir")) {
while (false !== ($name = readdir($handle))) {
if (strtoupper($name)==$target) {
closedir($handle);
return "$dir$name";
}
else if ($name!='.'&&$name!='..'&&is_dir("$ROOT$dir$name"))
if ($name=searchTree("$dir$name/",$target)) {
closedir($handle);
return $name;
}
}
}
closedir($handle);
return false;
}
# Build URL from root and query string
function buildURL($QS2) {
global $QS;
unset($QS['shell']);
$QS2=array_merge($QS,$QS2);
$sep='?';
$url=$_SERVER['SCRIPT_NAME'];
foreach ($QS2 as $k=>$v) {
$v=str_replace('&','%26',$v);
$url.="$sep$k=$v";
$sep='&';
}
return $url;
}
#########################################################################################################################
# SHORTCUT DEBUGGING FUNCTIONS
# Debugging - dump shortcut files
function dump($hex) {
$dump=''; $o=0;
for ($i=0; $i<strlen($hex);) {
$d=dechex($i); if (strlen($d)==1) $dump.="000$d : "; else $dump.="00$d : ";
$d1=dechex(ord(substr($hex,$i++,1))); if (strlen($d1)==1) $dump.="0$d1"; else $dump.="$d1";
$d2=dechex(ord(substr($hex,$i++,1))); if (strlen($d2)==1) $dump.="0$d2"; else $dump.="$d2";
$d3=dechex(ord(substr($hex,$i++,1))); if (strlen($d3)==1) $dump.="0$d3"; else $dump.="$d3";
$d4=dechex(ord(substr($hex,$i++,1))); if (strlen($d4)==1) $dump.="0$d4"; else $dump.="$d4";
$dump.=' : ';
$d1=hexdec($d1);
$d2=hexdec($d2);
$d3=hexdec($d3);
$d4=hexdec($d4);
if ($d1<32||$d1>126) $dump.='.'; else $dump.=chr($d1);
if ($d2<32||$d2>126) $dump.='.'; else $dump.=chr($d2);
if ($d3<32||$d3>126) $dump.='.'; else $dump.=chr($d3);
if ($d4<32||$d4>126) $dump.='.'; else $dump.=chr($d4);
# Output comments with shortcut dump
if ($i==4) $dump.=" L<br>";
else if ($i==8) $dump.=" GUID";
else if ($i==20) $dump.="<br>";
else if ($i==24) $dump.=" Flags<br>";
else if ($i==28) $dump.=" File Attributes<br>";
else if ($i==32) $dump.=" Time Stamps";
else if ($i==52) $dump.="<br>";
else if ($i==56) $dump.=" File Length<br>";
else if ($i==60) $dump.=" Icon Number<br>";
else if ($i==64) $dump.=" Window State<br>";
else if ($i==68) $dump.=" Hotkey<br>";
else if ($i==72) $dump.=" Reserved";
else if ($i==0x50) {$dump.=" (Skipping Item ID List)<br><br>File Location Block:";
#$o+=$d1-2; $i+=$o; # skip item id list
}
else if ($i==0x54+$o) $dump.=" ???<br>";
else if ($i==0x58+$o) $dump.=" Length of this structure<br>";
else if ($i==0x5c+$o) $dump.=" Flags<br>";
else if ($i==0x60+$o) $dump.=" Offset to LVtab<br>";
else if ($i==0x64+$o) $dump.=" Offset to local Path<br>";
$dump.=" <br>\n";
}
return $dump;
}
#########################################################################################################################
# SWF COMPILE
# currently swf-code is looked for in ./images etc - must be changed to.... what???
# also these PHP functions should originate from struct....? how??
function getmicrotime() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$startTime=getmicrotime();
$name="interface";
$out="./$name.swf";
$title=ucwords($name);
$dir=dirname($_SERVER["SCRIPT_FILENAME"]);
?><center><h1>SWF Project Compiler</h1><html><pre><?
echo "Building <b>$title</b> [$dir/build.php]</center>\n";
ming_useswfversion(6);
$ow=new SWFMovie();
$ow->setDimension(800,600);
$ow->setBackground(0x00,0x66,0x99);
$ow->setRate(50);
# LATER:
# - Move in to network.php script
# - build all aspects of build-struct from inodes (and build a specific project from passed inode ref)
# - allow inodes to be embedded in source and converted to text by compiler
# eg. (?) <:inode.optional-name-info:>
# this way, more content can become linked with real notions
# internal editor can treat inodes specially (eg. language, locale and linking)
# Import images from content in node list
# (later images should be able to be read dynamically somehow)
echo "</pre><b>Import images from content in node list:</b><br>";
if ($dh=opendir('../../../content')) {
while (false!==($file=readdir($dh))) if (ereg('\\.(jpg|gif|png)$',$file)) {
echo "[$file] ";
$img=new SWFBitmap(fopen("$dir/../../../content/$file",'rb'));
$sym=new SWFSprite();
$sym->add($img);
$sym->nextFrame();
$ow->addExport($sym,ereg_replace('\\....$','',$file));
}
closedir($dh);
}
?><pre><?
# Import images and convert to symbols
echo "</pre><b>Import images and convert to symbols:</b><br>";
if ($dh=opendir('./images')) {
while (false!==($file=readdir($dh))) if (ereg('^[^.]',$file)) {
echo "[$file] ";
$img=new SWFBitmap(fopen("$dir/images/$file",'rb'));
$sym=new SWFSprite();
$sym->add($img);
$sym->nextFrame();
$ow->addExport($sym,ereg_replace('\\....$','',$file));
}
closedir($dh);
}
?><pre><?
# Import and execute ming symbol creation scripts
$inc='';
echo "\n<b>Import and execute Ming symbol scripts:</b>\n";
if ($dh=opendir('./ming')) {
while (false!==($file=readdir($dh))) if (ereg('.php$',$file)) {
echo "\t$file\n";
$file="$dir/ming/$file";
$inc.=implode('',file($file));
}
closedir($dh);
}
eval($inc);
# Import shared library symbols
#echo "\n<b>Import shared library symbols:</b>\n";
#if ($dh=opendir('./swf')) {
# while (false!==($file=readdir($dh))) if (ereg('.swf$',$file)) {
# $file="$dir/swf/$file";
# echo "\t$file\n";
# $swf=$ow->importChar('./swf/lib.swf','icon_folder');
# $ow->addExport($swf,'icon_folder');
# }
# closedir($dh);
# }
# Write all exported images and symbols
$ow->writeExports();
# Import actionscript classes into frame 1
$actionscript=array();
$actionscript[1]='_root._id="OpenWindow-IF-MING-0.0a";';
$classes=array();
echo "\n<b>Import actionscript classes:</b>\n";
if ($dh=opendir('./classes')) {
while (false!==($file=readdir($dh))) if (ereg('(.+)\\.as$',$file,$m)) {
echo "\t$file\n";
$file="$dir/classes/$file";
if ($m[1]!='widget') $classes[]=$m[1];
$actionscript[1].=implode('',file($file));
}
closedir($dh);
}
# Include a function to create objects at runtime using [obj=]create(class,name,parent[,atts]);
$actionscript[1].="function create(class,name,parent,atts) {";
foreach ($classes as $class) $actionscript[1].="if (class=='$class') obj=new $class(name,parent,atts);";
$actionscript[1].="return obj;};";
# Import function sets
echo "\n<b>Import functions sets:</b>\n";
if ($dh=opendir('./functions')) {
while (false!==($file=readdir($dh))) if (ereg('(.+)\\.as$',$file,$m)) {
echo "\t$file\n";
$file="$dir/functions/$file";
$actionscript[1].=implode('',file($file));
}
closedir($dh);
}
# Add subsequent frame scripts
$frames=1;
echo "\n<b>Import in-frame actionscript:</b>\n";
while (file_exists("$dir/frame$frames.as")) $frames++;
$frames--;
for ($frame=1; $frame<=$frames; $frame++) {
$file="$dir/frame$frame.as";
echo "\tframe$frame.as\n";
if ($frame>1) $actionscript[$frame]='';
$actionscript[$frame].=implode('',file($file));
$ow->add(new SWFAction($actionscript[$frame]));
$ow->nextFrame();
}
# Output compiled result (9 is zlib compression)
$ow->save($out,9);
echo "\n<b>Output SWF file:</b>\t".dirname($out)."/$name.swf";
echo "\n<b>\tTime:</b>\t\t".(round((getmicrotime()-$startTime)*1000)/1000)." seconds";
ECHO "\n<b>\tSize:</b>\t\t".filesize($out)." bytes\n\n\t";
#########################################################################################################################
# SWF DYNAMIC FUNCTIONS
?>
</pre>
<?
#########################################################################################################################
# PAST COMMENTS
// 07 Nov 2004
// This is now slave to SWF on a 2Hz comms sync cycle
// But this PHP can also be the initial entry point:
// this is known by lack of exec/sync queue, in which case SWF is launched
// This version can also work without SWF, in which case it returns HTML
//
// Since this is now model-driven, we need to outline the structure of the two scripts
//
//
// PHP (we include all in here, even though they're their own things too)
// PHP-SWF
//
// SWF (moment)
// initEnv
// nodal
// cd (become, exec, build, this determines the exec-env...)
// ls,mk,rm,mv,rn... (or are these merely what's executable in this context?)
// Cycle (methods and instances)
// Symbols
// Container
// Desktop
// Space
// Content
// Charsets and character-symbols
// Mouse
// Pointer
// inputEvents
// Keyboard
// inputEvents
// PHP-Wrapped (adds to sync queue)
// buildPHP
// buildSWF
// readURL
// writeURL
// protocols
// Sync (change queues)
//
// PHP - all wrapped (executed from queue in sync - but all execution should be queue-based anyway)
// Messaging
// Sync
// Queue (exec named functions fom queue)
// buildPHP
// buildSWF - ming
// Init
// Import images
// Import and execute Ming code (which creates symbols - ie split into symbols)
// Import shared lib symbols (external symbols - not working yet)
// WriteExports
// Import actionscript classes
// runtime access function
// Import function sets
// Add sequential frame script
// Output compiled result
// Protocols:FTP,POP/SMTP,Messaging
//
//
// PHP-HTML
// Win32 Shortcuts
// createShortcut
// readShortcut
// intToDWord
// intToWord
// quadNullString
// Page Render
// CWD
// searchTree
// scanTree
// buildURL
// page_exec (wizard style)
// Form Processor
// processInput
// renderFormElement
?>