3 require_once "Socket.php";
4 require_once "LoomClient.php";
5 require_once "Cipher.php";
6 require_once "bcbitwise.php";
9 * iPhone interface to the Loom folder.
10 * Don't run this unencrypted.
11 * See http://www.whoopis.com/howtos/apache-rewrite.html
12 * for unstructions on setting up a .htaccess file to rewrite
13 * http://... to https://...
17 if (get_magic_quotes_gpc()) return stripslashes($x);
21 $passphrase = mq($_POST['passphrase']);
22 $session = mq($_POST['session']);
23 $qty = mq($_POST['zip']);
24 $type = mq($_POST['type']);
25 $location = mq($_POST['location']);
26 $folderkv = mq($_POST['folderkv']);
27 $valueskv = mq($_POST['valueskv']);
28 $take = mq($_POST['take']);
29 $give = mq($_POST['give']);
30 $page = mq($_POST['page']);
31 $greendot = mq($_POST['greendot']);
32 $showfolder = mq($_POST['showfolder']);
33 $newname = mq($_POST['newname']);
34 $oldname = mq($_POST['oldname']);
35 $newlocation = mq($_POST['newlocation']);
36 $savename = mq($_POST['savename']);
37 $delete = mq($_POST['delete']);
38 $add_location = mq($_POST['add_location']);
39 $commit = mq($_POST['commit']);
41 $client = new LoomClient();
43 // Redefine $encrypt_key if you want to use one.
44 // It should be a 32-character hex string, as generated
45 // by the grid-tutotial.php "Tools" page.
46 $client->disable_warnings();
48 include "ip-config.php";
49 $client->reenable_warnings();
54 if ($encrypt_key != '') {
56 if ($session_cipher != '') {
57 if ($folderkv != '') {
58 $folderkv = $session_cipher->decrypthex($folderkv);
60 if ($valueskv != '') {
61 $valueskv = $session_cipher->decrypthex($valueskv);
71 if ($page == 'refresh') {
76 if ($folderkv != '') $folder = $client->parsekv($folderkv, TRUE);
77 if ($valueskv != '') $values = $client->parsekv($valueskv, TRUE);
80 if (($session == '' && $passphrase == '') ||
!login()) {
81 $onload = 'passphrase';
85 $values = scanFolder($folder);
86 $valueskv = $client->array2kv($values);
90 $title = "Loom Folder";
92 if ($page == 'main') doMain();
93 elseif ($page == 'locations') doLocations();
94 elseif ($page == 'add_location') doAddLocation();
95 elseif ($page == 'logout') doLogout();
99 if ($page == 'login') drawLogin();
100 elseif ($page == 'main') drawMain();
101 elseif ($page == 'locations') drawLocations();
102 elseif ($page == 'add_location') drawAddLocation();
107 global $qty, $type, $location, $take, $give;
108 global $client, $folder, $folder_loc, $folder_name;
109 global $values, $valueskv;
114 if ($type != '-- choose asset --') {
115 $t = $folder['types'][$type];
118 $min_precision = $t['min_precision'];
119 if ($min_precision == '') $min_precision = 0;
120 $scale = $t['scale'];
121 if ($scale == '') $scale = 0;
124 if ($location != '-- choose location --') {
125 $loc = $folder['locs'][$location];
127 if ($id != '' && $loc != '') {
128 if ($scale != '') $count = bcmul($qty, bcpow(10, $scale), 0);
130 if ($count != '' && $id != '' && $loc != '') {
132 $client->buy($id, $folder_loc, $folder_loc, $url);
133 $res = $client->move($id, $count, $loc, $folder_loc, $url);
134 $loc_orig = $location;
135 $loc_dest = $folder_name;
136 } else if ($give != '') {
137 $client->buy($id, $loc, $folder_loc, $url);
138 $res = $client->move($id, $count, $folder_loc, $loc, $url);
139 $loc_dest = $location;
140 $loc_orig = $folder_name;
141 } else $transferred = FALSE;
143 $status = $res['status'];
144 if ($status == 'success') {
145 $value_orig = $client->applyScale($res['value_orig'], $min_precision, $scale);
146 $value_dest = $client->applyScale($res['value_dest'], $min_precision, $scale);
147 $values[$loc_orig][$type] = $value_orig;
148 ksort($values[$loc_orig]);
149 $values[$loc_dest][$type] = $value_dest;
150 ksort($values[$loc_dest]);
151 $valueskv = $client->array2kv($values);
152 } else $message = "Insufficient funds";
158 function doLocations() {
159 // Need to investigate how to delete.
160 // The "session" is an archive location containing the folder location
162 global $client, $folder, $values, $valueskv;
163 global $newname, $oldname;
164 global $savename, $delete, $add_location;
166 global $message, $title, $onload, $page;
169 $title = 'Loom Locations';
171 if ($savename != '') {
172 if ($newname == $oldname) return;
173 if ($folder['locs'][$newname] != '') {
174 $message = "Duplicate Location Name";
177 $res = $client->renameFolderLocation($session, $oldname, $newname);
182 else if ($add_location != '') {
183 $title = 'Loom Add Location';
184 $page = 'add_location';
187 else refreshFolder();
191 function makeCiphers() {
192 global $cipher, $encrypt_key, $session, $session_cipher;
193 if ($encrypt_key != '') {
195 $cipher = new Cipher($encrypt_key);
196 if ($session != '') $session = $cipher->decrypthex($session);
198 if ($session_cipher == '' && $session != '') {
199 $session_key = bcxorhex($session, $encrypt_key);
200 $session_cipher = new Cipher($session_key);
205 function doAddLocation() {
206 global $session, $commit, $page, $newname, $newlocation;
207 global $client, $message, $folder;
211 if ($commit != 'commit') $message = 'Cancelled';
212 else if ($newname == '') $message = 'Blank name';
213 else if ($folder['locs'][$newname] != '') $message = "Name already exists";
214 else if ($newlocation != '' && !$client->isValidID($newlocation)) {
215 $message = 'Invalid location ID';
217 if ($newlocation == '') $newlocation = $client->random
->random_id();
218 $client->newFolderLocation($session, $newname, $newlocation);
220 $message = "Location created: $newname";
224 function doLogout() {
225 global $page, $folderkv, $valueskv;
226 global $client, $session;
231 $client->logout($session);
234 function drawHead() {
235 global $title, $onload;
239 <meta name
="viewport" content
="width=device-width" user
-scalable
="no" minimum
-scale
="1.0" maximum
-scale
="1.0"/>
240 <title
><?
echo $title; ?
></title
>
242 <script language
="JavaScript">
243 function submitPage(page
) {
244 document
.forms
["mainform"].page
.value
= page
;
245 document
.mainform
.submit();
248 function greenDot(greendot
) {
249 document
.forms
["mainform"].greendot
.value
= greendot
;
250 document
.mainform
.submit();
253 function hideAddressbar() {
254 window
.scrollTo(0, 1);
257 function doOnLoad(selected
) {
259 setTimeout(hideAddressbar
, 250);
262 <?
additionalHTMLScripts(); ?
>
266 <style type
="text/css">
267 body
{ font
-family
: verdana
, arial
, sans
-serif
; font
-size
: 12pt
}
268 div
{ font
-size
:12pt
}
270 h1
{ font
-size
:14pt
}
271 h2
{ font
-size
:12pt
}
272 h3
{ font
-size
:10pt
}
273 td
{ font
-size
:12pt
}
274 ul
{ font
-size
:12pt
}
275 li
{ padding
-bottom
: 7px
}
276 pre
{ font
-family
: verdana
, arial
, sans
-serif
; }
277 A
:link
, A
:visited
{ color
:blue
; text
-decoration
:none
}
278 A
:hover
{ color
:blue
; text
-decoration
:underline
}
279 A
:active
{ color
:#FAD805; text-decoration:underline }
280 .tt
{ font
-family
: Courier
; font
-size
:10pt
}
281 .mono
{ font
-family
: monospace
; font
-size
: 11pt
}
282 .large_mono
{ font
-family
: monospace
; font
-size
: 10pt
}
283 .giant_mono
{ font
-family
: monospace
; font
-size
: 14pt
}
284 .tiny_mono
{ font
-family
: monospace
; font
-size
: 6pt
}
285 .normal
{ font
-size
:10pt
}
286 .smaller
{ font
-size
:6pt
}
287 .small
{ font
-size
:8pt
}
288 .large
{ font
-size
:12pt
}
289 .alarm
{ color
:red
; font
-weight
: bold
}
290 .focus_value
{ background
-color
:#DDDDDD }
291 .color_heading
{ margin
-top
:12px
; padding
:1px
; background
-color
:#DDDDDD; width:100% }
292 A
.label_link
{ font
-weight
:bold
; }
293 A
.highlight_link
{ font
-weight
:bold
; }
294 A
.cancel
{ background
-color
:#FFDDDD }
295 A
.plain
:link
, A
.plain
:visited
{ color
:black
; text
-decoration
:none
}
296 A
.plain
:hover
{ color
:blue
; text
-decoration
:underline
}
297 A
.plain
:active
{ color
:#FAD805; text-decoration:underline }
298 A
.name_dot
{ font
-size
:16pt
; font
-weight
:bold
; color
:green
; }
302 <body onload
="doOnLoad(document.forms[0].<? echo $onload; ?>)">
303 <table width
="320px">
308 function additionalHTMLScripts() {
311 if ($page == 'add_location') {
314 function Do0(form
) { form
.newlocation
.value
= form
.newlocation
.value +
"0" ;}
315 function Do1(form
) { form
.newlocation
.value
= form
.newlocation
.value +
"1" ;}
316 function Do2(form
) { form
.newlocation
.value
= form
.newlocation
.value +
"2" ;}
317 function Do3(form
) { form
.newlocation
.value
= form
.newlocation
.value +
"3" ;}
318 function Do4(form
) { form
.newlocation
.value
= form
.newlocation
.value +
"4" ;}
319 function Do5(form
) { form
.newlocation
.value
= form
.newlocation
.value +
"5" ;}
320 function Do6(form
) { form
.newlocation
.value
= form
.newlocation
.value +
"6" ;}
321 function Do7(form
) { form
.newlocation
.value
= form
.newlocation
.value +
"7" ;}
322 function Do8(form
) { form
.newlocation
.value
= form
.newlocation
.value +
"8" ;}
323 function Do9(form
) { form
.newlocation
.value
= form
.newlocation
.value +
"9" ;}
324 function DoA(form
) { form
.newlocation
.value
= form
.newlocation
.value +
"a" ;}
325 function DoB(form
) { form
.newlocation
.value
= form
.newlocation
.value +
"b" ;}
326 function DoC(form
) { form
.newlocation
.value
= form
.newlocation
.value +
"c" ;}
327 function DoD(form
) { form
.newlocation
.value
= form
.newlocation
.value +
"d" ;}
328 function DoE(form
) { form
.newlocation
.value
= form
.newlocation
.value +
"e" ;}
329 function DoF(form
) { form
.newlocation
.value
= form
.newlocation
.value +
"f" ;}
330 function DoBksp(form
)
331 { var T
= form
.newlocation
.value
;
333 var T2
= T
.substr(0,L
-1);
334 form
.newlocation
.value
= T2
;
336 function DoClr(form
) { form
.newlocation
.value
= ""; }
337 function DoEnter(form
) { form
.submit(); }
338 function DoCancel(form
) {
339 form
.commit
.value
= 'Cancel';
342 function submitOnRet(e
, form
) {
345 if (window
.event
) keynum
= e
.keyCode
; // IE
346 else if (e
.which
) keynum
= e
.which
; // Netscape/Firefox/Opera
347 keychar
= String.fromCharCode(keynum
);
348 if (keychar
!= "\n" && keychar
!= "\r") return true;
357 function drawTail() {
365 function drawLogin() {
367 $host = $_SERVER["HTTP_HOST"];
368 if (!$host ||
$host == '') $host = $_SERVER["SERVER_NAME"];
372 <a href
="https://loom.cc/">Loom
</a
> for iPhone
. Note that in order to
use this
373 confidently
, you must trust that the PHP scripts at
<?
echo $host; ?
> do
374 not steal your passphrase
. I promise that unless somebody hacks my
375 site
, the code running is what you can download from
376 <a href
="./">here
</a
>, but don
't trust that unless you know
378 <form method="post" action="" autocomplete="off">
379 <input type="hidden" name="page" value="main"/>
383 <td><input type="password" name="passphrase" size="35" /></td>
387 <td><input type="submit" name="login" text="Login" /></td>
390 <p>Because my SSL certificate is from
391 <a href="http://www.cacert.org/">CAcert.org</a>, a free certificate authority (CA), you'll see a warning message on your iPhone when you come here
: "The certificate for this website is invalid..." Click the
"Continue" button to go ahead
. Your regular browser should allow you to easily add the CA certificate to eliminate warnings
, but the iPhone browser does not
.</p
>
393 <p
>One way to
use this
, without giving me the keys to your kingdom
, is to create a separate
"Mobile" folder in Loom
. Transfer assets that you think you might need on the road to a drop
-point shared between your main folder
and the
"Mobile" folder
, and only aim this page at the
"Mobile" folder
. You can always login to your main folder directly via
394 <a href
="https://loom.cc/">
395 loom
.cc
</a
>, if you need something there
. The regular
interface isn
't as convenient as this one, but it works on the mobile browser.</p>
400 function hsc($text) {
401 return htmlspecialchars($text);
404 function hiddenValue($name) {
405 eval('global $
' . $name . ';');
406 echo '<input type
="hidden" name
="' . $name .
407 '" value
="' . hsc(eval('return $' . $name . ';')) . '"/>' . "\n";
410 function drawValues($name, $typevalues) {
411 if (is_array($typevalues)) {
413 foreach ($typevalues as $type => $value) {
416 $str .= "<b>$name</b>\n";
417 $str .= '<table border
="0"><tr
><td width
="50px"> 
;</td
><td
>';
418 $str .= '<table border
="0">' . "\n";
420 $str .= '<tr
><td align
="right">' . $value . "<td><td>$type</td></tr>\n";
425 echo "</table></td></tr></table>\n";
430 function writeSessionInfo() {
431 global $cipher, $session_cipher;
432 global $session, $folderkv, $valueskv;
434 $enc_session = $session;
435 $enc_folderkv = $folderkv;
436 $enc_valueskv = $valueskv;
438 if ($cipher) $enc_session = $cipher->encrypt2hex($enc_session);
439 if ($session_cipher) {
440 $enc_folderkv = $session_cipher->encrypt2hex($enc_folderkv);
441 $enc_valueskv = $session_cipher->encrypt2hex($enc_valueskv);
443 echo '<input type
="hidden" name
="session" value
="' . hsc($enc_session) . '"/>' . "\n";
444 echo '<input type
="hidden" name
="folderkv" value
="' . hsc($enc_folderkv) . '"/>' . "\n";
445 echo '<input type
="hidden" name
="valueskv" value
="' . hsc($enc_valueskv) . '"/>' . "\n";
448 function drawMain() {
449 global $session, $folder, $values, $folder_name;
450 global $qty, $type, $location;
456 <table border="0" width="99%" cellpadding="3">
458 <td colspan="2" style="background-color: #c0c0c0; text-align: center;"><span style="font-weight: normal; font-size: 110%;"><a href="javascript:submitPage('refresh
');">Refresh</a>
460 <a href="javascript:submitPage('locations
');">Locations</a>
462 <a href="javascript:submitPage('logout
');">Logout</a>
467 drawValues($folder_name, $values[$folder_name]);
469 <table border="0" width="99%">
470 <form name="mainform" method="post" action="" autocomplete="off">
476 <td align="right">Qty:</td>
477 <td><input style="font-size: 12pt;" type="text" size="25" name="zip" value="<? echo $qty; ?>" style="text-align:right;"></td>
481 <select name="type" style="font-size: 10pt;">
482 <option value="">-- choose asset --</option>
484 foreach ($folder['types
'] as $typename => $typearray) {
485 echo '<option value
="' . hsc($typename) . '"';
486 if ($type == $typename) echo ' selected
="selected"';
487 echo '>' . hsc($typename) . "</option>\n";
495 <select name="location" style="font-size: 10pt;">
496 <option value="">-- choose location --</option>
498 foreach($values as $loc => $value) {
499 if ($loc != $folder_name) {
500 echo '<option value
="' . hsc($loc) . '"';
501 if ($loc == $location) echo ' selected
="selected"';
502 echo '>' . hsc($loc) . "</option>\n";
511 <input style="font-size: 10pt;" type="submit" name="take" value="Take"/>
512 <input style="font-size: 10pt;" type="submit" name="give" value="Give"/>
516 if ($message != '') {
517 echo '<tr
><td
></td
><td
class="alarm">' . hsc($message) . "</td></tr>\n";
522 foreach ($values as $name => $typevalues) {
523 if ($name != $folder_name) {
524 drawValues($name, $typevalues);
531 function drawLocations() {
532 global $session, $folder, $values, $folder_name;
533 global $qty, $type, $location, $greendot;
538 <table border="0" width="99%" cellpadding="3">
540 <td colspan="2" style="background-color: #c0c0c0; text-align: center;"><span style="font-weight: normal; font-size: 110%;"><a href="javascript:submitPage('main
');">Folder</a>
542 <a href="javascript:submitPage('locations
');"><b>Locations</b></a>
544 <a href="javascript:submitPage('logout
');">Logout</a>
548 <table border="0" width="99%">
549 <form name="mainform" method="post" action="" autocomplete="off">
552 echo '<input type
="hidden" name
="zip" value
="' . $qty . '"/>' . "\n";
554 hiddenValue('location
');
555 hiddenValue('newname
');
556 hiddenValue('newlocation
');
559 <input type="hidden" name="greendot" value=""/>
562 <td valign="top"><a class=name_dot href="javascript: greenDot('<?
echo $folder_name; ?
>')" title="Edit Name"> • </a></td>
565 if ($greendot != $folder_name) echo "<b>$folder_name</b>";
567 echo '<input style
="font-size: 12pt;" type
="text" size
="25" name
="newname" value
="' . hsc($folder_name) . '"/><br
/>' . "\n";
568 echo '<input type
="hidden" name
="oldname" value
="' . hsc($folder_name) . '"/>' . "\n";
569 echo '<input type
="submit" name
="savename" value
="Save"/>' . "\n";
570 echo '<input type
="submit" name
="cancel" value
="Cancel"/>' . "\n";
573 $locs = $folder['locs
'];
574 foreach ($locs as $name => $loc) {
575 if ($name != $folder_name) {
578 <td valign="top"><a class=name_dot href="javascript: greenDot('<?
echo $name; ?
>')" title="Edit Name or Delete Folder"> • </a></td>
581 if ($greendot != $name) echo $name;
583 echo '<input style
="font-size: 12pt;" type
="text" size
="25" name
="newname" value
="' . hsc($name) . '"/><br
/>' . "\n";
584 echo '<input type
="hidden" name
="oldname" value
="' . hsc($name) . '"/>' . "\n";
585 echo '<input type
="submit" name
="savename" value
="Save"/>' . "\n";
586 echo '<input type
="submit" name
="cancel" value
="Cancel"/>' . "\n";
588 echo '<input type
="submit" disabled name
="delete" value
="Delete..."/><br
/>' . "\n";
589 echo '</td
></tr
><tr
><td colspan
="2"><span
class="mono">' . "<b>$loc</b></span><br/>\n";
594 if ($message != '') {
595 echo '<tr
><td colspan
="2" class="alarm">' . hsc($message) . "</td></tr>\n";
598 <tr><td colspan="2"><input type="submit" name="add_location" value="Add Location"/></td></tr>
601 <p>Click the green dot by a folder name to see its hex value, or to rename or delete it.</p>
605 function drawAddLocation() {
606 global $newname, $newlocation;
612 <form name="entryForm" method="post" action=""><b>
616 hiddenValue('commit
');
619 <input type="text" name="newname" style="font-size: 14pt;" size="25" value="<? echo $newname; ?>" onkeydown="submitOnRet(event, this.form)"/><br/>
620 Enter Location, blank for random:<br/>
621 <input type="text" style="font-size: 11pt;" size="32" name="newlocation" value="<? echo $newlocation; ?>" onkeydown="submitOnRet(event, this.form)"/>
624 <td><input type="button" style="font-size: 18pt;" value="7" onClick="Do7(this.form)"></td>
625 <td><input type="button" style="font-size: 18pt;" value="8" onClick="Do8(this.form)"></td>
626 <td><input type="button" style="font-size: 18pt;" value="9" onClick="Do9(this.form)"></td>
627 <td><input type="button" style="font-size: 18pt;" value="F" onClick="DoF(this.form)"></td>
630 <td><input type="button" style="font-size: 18pt;" value="4" onClick="Do4(this.form)"></td>
631 <td><input type="button" style="font-size: 18pt;" value="5" onClick="Do5(this.form)"></td>
632 <td><input type="button" style="font-size: 18pt;" value="6" onClick="Do6(this.form)"></td>
633 <td><input type="button" style="font-size: 18pt;" value="E" onClick="DoE(this.form)"></td>
636 <td><input type="button" style="font-size: 18pt;" value="1" onClick="Do1(this.form)"></td>
637 <td><input type="button" style="font-size: 18pt;" value="2" onClick="Do2(this.form)"></td>
638 <td><input type="button" style="font-size: 18pt;" value="3" onClick="Do3(this.form)"></td>
639 <td><input type="button" style="font-size: 18pt;" value="D" onClick="DoD(this.form)"></td>
642 <td><input type="button" style="font-size: 18pt;" value="0" onClick="Do0(this.form)"></td>
643 <td><input type="button" style="font-size: 18pt;" value="A" onClick="DoA(this.form)"></td>
644 <td><input type="button" style="font-size: 18pt;" value="B" onClick="DoB(this.form)"></td>
645 <td><input type="button" style="font-size: 18pt;" value="C" onClick="DoC(this.form)"></td>
649 <input type="button" style="font-size: 18pt;" value="Del" onClick="DoBksp(this.form)">
650 <input type="button" style="font-size: 18pt;" value="Clr" onClick="DoClr(this.form)">
651 <input type="button" style="font-size: 18pt;" value="Enter" onClick="DoEnter(this.form)"><br/>
652 <input type="button" style="font-size: 18pt;" value="Cancel" onClick="DoCancel(this.form)"><br/>
658 global $folder, $folder_name, $folder_loc;
660 if ($folder == '') return refreshFolder();
661 $folder_name = $folder['name
'];
662 $folder_loc = $folder['loc
'];
666 function refreshFolder() {
667 global $client, $passphrase, $session;
668 global $folder, $folderkv, $folder_name, $folder_loc;
672 if ($session != '') {
673 $res = $client->touch_archive($session, $url);
674 if ($res['status
'] == 'success
') $loc = $res['content
'];
678 if ($passphrase != '') {
679 $loc = $client->hash2location($client->sha256($passphrase));
680 $session = $client->folderSession($loc);
685 $res = $client->touch_archive($loc, $url);
686 if ($res['status
'] != 'success
') return FALSE;
687 $folder = $client->parseFolder($loc, $res['content
']);
688 $folderkv = $client->array2kv($folder);
690 $folder_name = $folder['name
'];
691 $folder_loc = $folder['loc
'];
695 function fullRefresh() {
696 global $client, $folder, $values, $valueskv;
699 $values = scanFolder($folder);
700 $valueskv = $client->array2kv($values);
703 function blankToZero($x) {
704 if ($x == '') return 0;
708 // This is currently n-squared for pretty big n, since it has to do
709 // a web call for each location/type pair.
710 // Patrick has promised a scan() function in the web API that would
711 // allow it to be done with a single call.
712 /* Now uses Patrick's
scan() API call
713 function scanFolder($folder) {
716 $types = $folder['types'];
717 $locs = $folder['locs'];
719 foreach ($locs as $locname => $loc) {
720 $loc_values = array();
721 foreach ($types as $typename => $type) {
723 $min_precision = $type['min_precision'];
724 $scale = $type['scale'];
725 $res = $client->touch($id, $loc, $url);
726 if ($res['status'] == 'success') {
727 $value = $client->applyScale($res['value'], $min_precision, $scale);
728 $loc_values[$typename] = $value;
731 $values[$locname] = $loc_values;
737 function scanFolder($folder) {
740 return $client->namedScan($folder['locs'], $folder['types'], FALSE, $url);
744 // Copyright 2008 Bill St. Clair
746 // Licensed under the Apache License, Version 2.0 (the "License");
747 // you may not use this file except in compliance with the License.
748 // You may obtain a copy of the License at
750 // http://www.apache.org/licenses/LICENSE-2.0
752 // Unless required by applicable law or agreed to in writing, software
753 // distributed under the License is distributed on an "AS IS" BASIS,
754 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
755 // See the License for the specific language governing permissions
756 // and limitations under the License.