Cleanup. New article.
[loomclient.git] / ip.php
blob13c294554654e4edad2acb5ae8c86c634f250b1b
1 <?php
3 require_once "Socket.php";
4 require_once "LoomClient.php";
5 require_once "Cipher.php";
6 require_once "bcbitwise.php";
8 /*
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://...
16 function mq($x) {
17 if (get_magic_quotes_gpc()) return stripslashes($x);
18 else return $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 $contact = mq($_POST['contact']);
33 $showfolder = mq($_POST['showfolder']);
34 $newname = mq($_POST['newname']);
35 $oldname = mq($_POST['oldname']);
36 $newlocation = mq($_POST['newlocation']);
37 $savename = mq($_POST['savename']);
38 $delete = mq($_POST['delete']);
39 $add_location = mq($_POST['add_location']);
40 $commit = mq($_POST['commit']);
42 $client = new LoomClient();
44 // Redefine $encrypt_key if you want to use one.
45 // It should be a 32-character hex string, as generated
46 // by the grid-tutotial.php "Tools" page.
47 $client->disable_warnings();
48 $encrypt_key = '';
49 include "ip-config.php";
50 $client->reenable_warnings();
52 $cipher = '';
53 $session_cipher = '';
55 if ($encrypt_key != '') {
56 makeCiphers();
57 if ($session_cipher != '') {
58 if ($folderkv != '') {
59 $folderkv = $session_cipher->decrypthex($folderkv);
61 if ($valueskv != '') {
62 $valueskv = $session_cipher->decrypthex($valueskv);
67 $onload = 'zip';
68 $folder = '';
69 $values = '';
70 $message = '';
72 if ($page == 'refresh') {
73 $folderkv = '';
74 $valueskv = '';
75 $page = 'main';
76 } else {
77 if ($folderkv != '') $folder = $client->parsekv($folderkv, TRUE);
78 if ($valueskv != '') $values = $client->parsekv($valueskv, TRUE);
81 if (($session == '' && $passphrase == '') || !login()) {
82 $onload = 'passphrase';
83 $page = 'login';
84 } else {
85 if ($values == '') {
86 $values = scanFolder($folder);
87 $valueskv = $client->array2kv($values);
91 $title = "Loom Wallet";
93 if ($page == 'main') doMain();
94 elseif ($page == 'locations') doLocations();
95 elseif ($page == 'add_location') doAddLocation();
96 elseif ($page == 'logout') doLogout();
98 drawHead();
100 if ($page == 'login') drawLogin();
101 elseif ($page == 'main') drawMain();
102 elseif ($page == 'locations') drawLocations();
103 elseif ($page == 'add_location') drawAddLocation();
105 drawTail();
107 function doMain() {
108 global $qty, $type, $location, $take, $give;
109 global $client, $folder, $folder_loc, $folder_name;
110 global $values, $valueskv;
111 global $message;
112 global $greendot, $contact;
114 $id = '';
116 if ($greendot != '') {
117 $type = $greendot;
118 $location = $contact;
119 $qty = $values[$location][$type];
120 $take = "Take";
122 if ($type != '-- choose asset --') {
123 $t = $folder['types'][$type];
124 if ($t) {
125 $id = $t['id'];
126 $min_precision = $t['min_precision'];
127 if ($min_precision == '') $min_precision = 0;
128 $scale = $t['scale'];
129 if ($scale == '') $scale = 0;
132 if ($location != '-- choose contact --') {
133 $loc = $folder['locs'][$location];
135 if ($id != '' && $loc != '') {
136 if ($scale != '') $count = bcmul($qty, bcpow(10, $scale), 0);
137 $transferred = TRUE;
138 if ($count != '' && $id != '' && $loc != '') {
139 if ($take != '') {
140 $client->buy($id, $folder_loc, $folder_loc, $url);
141 $res = $client->move($id, $count, $loc, $folder_loc, $url);
142 $loc_orig = $location;
143 $loc_dest = $folder_name;
144 } else if ($give != '') {
145 $client->buy($id, $loc, $folder_loc, $url);
146 $res = $client->move($id, $count, $folder_loc, $loc, $url);
147 $loc_dest = $location;
148 $loc_orig = $folder_name;
149 } else $transferred = FALSE;
150 if ($transferred) {
151 $status = $res['status'];
152 if ($status == 'success') {
153 $value_orig = $client->applyScale($res['value_orig'], $min_precision, $scale);
154 $value_dest = $client->applyScale($res['value_dest'], $min_precision, $scale);
155 $values[$loc_orig][$type] = $value_orig;
156 ksort($values[$loc_orig]);
157 $values[$loc_dest][$type] = $value_dest;
158 ksort($values[$loc_dest]);
159 $valueskv = $client->array2kv($values);
160 } else $message = "Insufficient funds";
166 function doLocations() {
167 // Need to investigate how to delete.
168 // The "session" is an archive location containing the folder location
170 global $client, $folder, $values, $valueskv;
171 global $newname, $oldname;
172 global $savename, $delete, $add_location;
173 global $session;
174 global $message, $title, $onload, $page;
175 global $greendot, $cancel;
177 $onload = 'newname';
178 $title = 'Loom Contacts';
180 if ($savename != '') {
181 if ($newname == $oldname) return;
182 if ($folder['locs'][$newname] != '') {
183 $message = "Duplicate Contact Name";
184 return;
186 $res = $client->renameFolderLocation($session, $oldname, $newname);
187 //echo $res;
188 fullRefresh();
191 else if ($add_location != '') {
192 $title = 'Loom Add Contact';
193 $page = 'add_location';
196 else if ($greendot == '' && $cancel == '') refreshFolder();
200 function makeCiphers() {
201 global $cipher, $encrypt_key, $session, $session_cipher;
202 if ($encrypt_key != '') {
203 if ($cipher == '') {
204 $cipher = new Cipher($encrypt_key);
205 if ($session != '') $session = $cipher->decrypthex($session);
207 if ($session_cipher == '' && $session != '') {
208 $session_key = bcxorhex($session, $encrypt_key);
209 $session_cipher = new Cipher($session_key);
214 function doAddLocation() {
215 global $session, $commit, $page, $newname, $newlocation;
216 global $client, $message, $folder;
218 $page = 'locations';
220 if ($commit != 'commit') $message = 'Cancelled';
221 else if ($newname == '') $message = 'Blank name';
222 else if ($folder['locs'][$newname] != '') $message = "Name already exists";
223 else if ($newlocation != '' && !$client->isValidID($newlocation)) {
224 $message = 'Invalid location ID';
225 } else {
226 if ($newlocation == '') $newlocation = $client->random->random_id();
227 $client->newFolderLocation($session, $newname, $newlocation);
228 fullRefresh();
229 $message = "Contact created: $newname";
233 function doLogout() {
234 global $page, $folderkv, $valueskv;
235 global $client, $session;
237 $page = 'login';
238 $folderkv = '';
239 $valueskv = '';
240 $client->logout($session);
243 function drawHead() {
244 global $title, $onload;
246 <html>
247 <head>
248 <meta name="viewport" content="width=device-width" user-scalable="no" minimum-scale="1.0" maximum-scale="1.0"/>
249 <title><? echo $title; ?></title>
250 <link rel="apple-touch-icon" href="krugerand.png"/>
251 <link rel="shortcut icon" href="krugerand.png"/>
253 <script language="JavaScript">
254 function submitPage(page) {
255 document.forms["mainform"].page.value = page;
256 document.mainform.submit();
259 function greenDot(greendot, contact) {
260 document.forms["mainform"].greendot.value = greendot;
261 if (contact != "") document.forms["mainform"].contact.value = contact;
262 document.mainform.submit();
265 function hideAddressbar() {
266 window.scrollTo(0, 1);
269 function doOnLoad(selected) {
270 selected.select();
271 setTimeout(hideAddressbar, 250);
274 <? additionalHTMLScripts(); ?>
276 </script>
278 <style type="text/css">
279 body { font-family: verdana, arial, sans-serif; font-size: 12pt }
280 div { font-size:12pt }
281 p { font-size:12pt }
282 h1 { font-size:14pt }
283 h2 { font-size:12pt }
284 h3 { font-size:10pt }
285 td { font-size:12pt }
286 ul { font-size:12pt }
287 li { padding-bottom: 7px }
288 pre { font-family: verdana, arial, sans-serif; }
289 A:link, A:visited { color:blue; text-decoration:none }
290 A:hover { color:blue; text-decoration:underline }
291 A:active { color:#FAD805; text-decoration:underline }
292 .tt { font-family: Courier; font-size:10pt }
293 .mono { font-family: monospace; font-size: 11pt }
294 .large_mono { font-family: monospace; font-size: 10pt }
295 .giant_mono { font-family: monospace; font-size: 14pt }
296 .tiny_mono { font-family: monospace; font-size: 6pt }
297 .normal { font-size:10pt }
298 .smaller { font-size:6pt }
299 .small { font-size:8pt }
300 .large { font-size:12pt }
301 .alarm { color:red; font-weight: bold }
302 .focus_value { background-color:#DDDDDD }
303 .color_heading { margin-top:12px; padding:1px; background-color:#DDDDDD; width:100% }
304 A.label_link { font-weight:bold; }
305 A.highlight_link { font-weight:bold; }
306 A.cancel { background-color:#FFDDDD }
307 A.plain:link, A.plain:visited { color:black; text-decoration:none }
308 A.plain:hover { color:blue; text-decoration:underline }
309 A.plain:active { color:#FAD805; text-decoration:underline }
310 A.name_dot { font-size:16pt; font-weight:bold; color:green; }
311 </style>
313 </head>
314 <body onload="doOnLoad(document.forms[0].<? echo $onload; ?>)">
315 <table width="320px">
316 <tr><td>
320 function additionalHTMLScripts() {
321 global $page;
323 if ($page == 'add_location') {
326 function Do0(form) { form.newlocation.value = form.newlocation.value +"0" ;}
327 function Do1(form) { form.newlocation.value = form.newlocation.value +"1" ;}
328 function Do2(form) { form.newlocation.value = form.newlocation.value +"2" ;}
329 function Do3(form) { form.newlocation.value = form.newlocation.value +"3" ;}
330 function Do4(form) { form.newlocation.value = form.newlocation.value +"4" ;}
331 function Do5(form) { form.newlocation.value = form.newlocation.value +"5" ;}
332 function Do6(form) { form.newlocation.value = form.newlocation.value +"6" ;}
333 function Do7(form) { form.newlocation.value = form.newlocation.value +"7" ;}
334 function Do8(form) { form.newlocation.value = form.newlocation.value +"8" ;}
335 function Do9(form) { form.newlocation.value = form.newlocation.value +"9" ;}
336 function DoA(form) { form.newlocation.value = form.newlocation.value +"a" ;}
337 function DoB(form) { form.newlocation.value = form.newlocation.value +"b" ;}
338 function DoC(form) { form.newlocation.value = form.newlocation.value +"c" ;}
339 function DoD(form) { form.newlocation.value = form.newlocation.value +"d" ;}
340 function DoE(form) { form.newlocation.value = form.newlocation.value +"e" ;}
341 function DoF(form) { form.newlocation.value = form.newlocation.value +"f" ;}
342 function DoBksp(form)
343 { var T = form.newlocation.value;
344 var L = T.length;
345 var T2 = T.substr(0,L-1);
346 form.newlocation.value = T2;
348 function DoClr(form) { form.newlocation.value = ""; }
349 function DoEnter(form) { form.submit(); }
350 function DoCancel(form) {
351 form.commit.value = 'Cancel';
352 form.submit();
354 function submitOnRet(e, form) {
355 var keynum;
356 var keychar;
357 if (window.event) keynum = e.keyCode; // IE
358 else if (e.which) keynum = e.which; // Netscape/Firefox/Opera
359 keychar = String.fromCharCode(keynum);
360 if (keychar != "\n" && keychar != "\r") return true;
361 DoEnter(form);
362 return false;
364 </script>
369 function drawTail() {
370 ?></td></tr>
371 </table>
372 </body>
373 </html>
377 function drawLogin() {
379 $host = $_SERVER["HTTP_HOST"];
380 if (!$host || $host == '') $host = $_SERVER["SERVER_NAME"];
383 <p>Welcome to
384 <a href="https://loom.cc/">Loom</a> for iPhone. Note that in order to use this
385 confidently, you must trust that the PHP scripts at <? echo $host; ?> do
386 not steal your passphrase. I promise that unless somebody hacks my
387 site, the code running is what you can download from
388 <a href="./">here</a>, but don't trust that unless you know
389 me.</p>
390 <form method="post" action="" autocomplete="off">
391 <input type="hidden" name="page" value="main"/>
392 <table width="99%">
393 <tr>
394 <td>Passphrase:</td>
395 <td><input type="password" name="passphrase" size="35" /></td>
396 </tr>
397 <tr>
398 <td></td>
399 <td><input type="submit" name="login" text="Login" /></td>
400 </tr>
401 </table>
402 <p>Because my SSL certificate is a $20/year cheapie from
403 <a href="http://www.godaddy.com/">GoDaddy.com</a>, 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. In any case, your connection to my web site will be encrypted.</p>
405 <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
406 <a href="https://loom.cc/">
407 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>
408 </form>
412 function hsc($text) {
413 return htmlspecialchars($text);
416 function hiddenValue($name) {
417 eval('global $' . $name . ';');
418 echo '<input type="hidden" name="' . $name .
419 '" value="' . hsc(eval('return $' . $name . ';')) . '"/>' . "\n";
422 function drawValues($name, $typevalues) {
423 if (is_array($typevalues)) {
424 $str = '';
425 foreach ($typevalues as $type => $value) {
426 $link = '<a class="plain" href="javascript: greenDot(' . "'" .
427 hsc($type) . "', '" . hsc($name) . "')" . '" title="Take">';
428 if ($value != 0) {
429 if ($str == '') {
430 $str .= "<b>" . hsc($name) . "</b>\n";
431 $str .= '<table border="0"><tr><td width="50px">&nbsp;</td><td>';
432 $str .= '<table border="0">' . "\n";
434 $str .= '<tr><td align="right">' . $link . $value . "</a></td>\n" .
435 "<td>" . $link . $type. "</a></td></tr>\n";
438 if ($str != '') {
439 echo $str;
440 echo "</table></td></tr></table>\n";
445 function writeSessionInfo() {
446 global $cipher, $session_cipher;
447 global $session, $folderkv, $valueskv;
449 $enc_session = $session;
450 $enc_folderkv = $folderkv;
451 $enc_valueskv = $valueskv;
453 if ($cipher) $enc_session = $cipher->encrypt2hex($enc_session);
454 if ($session_cipher) {
455 $enc_folderkv = $session_cipher->encrypt2hex($enc_folderkv);
456 $enc_valueskv = $session_cipher->encrypt2hex($enc_valueskv);
458 echo '<input type="hidden" name="session" value="' . hsc($enc_session) . '"/>' . "\n";
459 echo '<input type="hidden" name="folderkv" value="' . hsc($enc_folderkv) . '"/>' . "\n";
460 echo '<input type="hidden" name="valueskv" value="' . hsc($enc_valueskv) . '"/>' . "\n";
463 function drawMain() {
464 global $session, $folder, $values, $folder_name;
465 global $qty, $type, $location;
466 global $message;
468 $page = 'main';
471 <table border="0" width="99%" cellpadding="3">
472 <tr>
473 <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>
474 &nbsp;
475 <a href="javascript:submitPage('locations');">Contacts</a>
476 &nbsp;
477 <a href="javascript:submitPage('logout');">Logout</a>
478 </span></td>
479 </tr>
480 </table>
482 drawValues($folder_name, $values[$folder_name]);
484 <table border="0" width="99%">
485 <form name="mainform" method="post" action="" autocomplete="off">
486 <input type="hidden" name="greendot" value=""/>
487 <input type="hidden" name="contact" value=""/>
489 writeSessionInfo();
490 hiddenValue('page');
492 <tr>
493 <td align="right">Qty:</td>
494 <td><input style="font-size: 12pt;" type="text" size="25" name="zip" value="<? echo $qty; ?>" style="text-align:right;"></td>
495 </tr><tr>
496 <td></td>
497 <td>
498 <select name="type" style="font-size: 10pt;">
499 <option value="">-- choose asset --</option>
501 foreach ($folder['types'] as $typename => $typearray) {
502 echo '<option value="' . hsc($typename) . '"';
503 if ($type == $typename) echo ' selected="selected"';
504 echo '>' . hsc($typename) . "</option>\n";
507 </select>
508 </td>
509 </tr><tr>
510 <td></td>
511 <td>
512 <select name="location" style="font-size: 10pt;">
513 <option value="">-- choose contact --</option>
515 foreach($values as $loc => $value) {
516 if ($loc != $folder_name) {
517 echo '<option value="' . hsc($loc) . '"';
518 if ($loc == $location) echo ' selected="selected"';
519 echo '>' . hsc($loc) . "</option>\n";
523 </select>
524 </td>
525 </tr><tr>
526 <td></td>
527 <td>
528 <input style="font-size: 10pt;" type="submit" name="take" value="Take"/>
529 <input style="font-size: 10pt;" type="submit" name="give" value="Give"/>
530 </td>
531 </tr>
533 if ($message != '') {
534 echo '<tr><td></td><td class="alarm">' . hsc($message) . "</td></tr>\n";
536 ?></table>
538 foreach ($values as $name => $typevalues) {
539 if ($name != $folder_name) {
540 drawValues($name, $typevalues);
544 <p>Tap a value or asset name to take all the assets in that line.</p>
545 </form>
549 function drawLocations() {
550 global $session, $folder, $values, $folder_name;
551 global $qty, $type, $location, $greendot;
552 global $message;
554 $page = 'locations';
556 <table border="0" width="99%" cellpadding="3">
557 <tr>
558 <td colspan="2" style="background-color: #c0c0c0; text-align: center;"><span style="font-weight: normal; font-size: 110%;"><a href="javascript:submitPage('main');">Wallet</a>
559 &nbsp;
560 <a href="javascript:submitPage('locations');"><b>Contacts</b></a>
561 &nbsp;
562 <a href="javascript:submitPage('logout');">Logout</a>
563 </span></td>
564 </tr>
565 </table>
566 <table border="0" width="99%">
567 <form name="mainform" method="post" action="" autocomplete="off">
569 writeSessionInfo();
570 echo '<input type="hidden" name="zip" value="' . $qty . '"/>' . "\n";
571 hiddenValue('type');
572 hiddenValue('location');
573 hiddenValue('newname');
574 hiddenValue('newlocation');
575 hiddenValue('page');
577 <input type="hidden" name="greendot" value=""/>
578 <table>
579 <tr>
580 <td valign="top">
581 <a class="name_dot" href="javascript: greenDot('<? echo $folder_name; ?>', '')" title="Edit Name">&nbsp;&bull;&nbsp;</a>
582 </td>
584 echo "<td>";
585 if ($greendot != $folder_name) {
586 echo '<a class="plain" href="javascript: greenDot(' . "'" .
587 hsc($folder_name) . "', '')" . '" title="Edit Name"><b>' .
588 $folder_name . '</b></a>';
589 } else {
590 echo '<input style="font-size: 12pt;" type="text" size="25" name="newname" value="' . hsc($folder_name) . '"/><br/>' . "\n";
591 echo '<input type="hidden" name="oldname" value="' . hsc($folder_name) . '"/>' . "\n";
592 echo '<input type="submit" name="savename" value="Save"/>' . "\n";
593 echo '<input type="submit" name="cancel" value="Cancel"/>' . "\n";
595 echo "</td></tr>\n";
596 $locs = $folder['locs'];
597 foreach ($locs as $name => $loc) {
598 if ($name != $folder_name) {
600 <tr>
601 <td valign="top">
602 <a class=name_dot href="javascript: greenDot('<? echo $name; ?>', '')" title="Edit Name or Delete Folder">&nbsp;&bull;&nbsp;</a>
603 </td>
605 echo "<td>";
606 if ($greendot != $name) {
607 echo '<a class="plain" href="javascript: greenDot(' . "'" .
608 hsc($name) . "', '')" . '" title="Edit Name or Delete Wallet">' .
609 $name . '</a>';
610 } else {
611 echo '<input style="font-size: 12pt;" type="text" size="25" name="newname" value="' . hsc($name) . '"/><br/>' . "\n";
612 echo '<input type="hidden" name="oldname" value="' . hsc($name) . '"/>' . "\n";
613 echo '<input type="submit" name="savename" value="Save"/>' . "\n";
614 echo '<input type="submit" name="cancel" value="Cancel"/>' . "\n";
615 echo "&nbsp;&nbsp;";
616 echo '<input type="submit" disabled name="delete" value="Delete..."/><br/>' . "\n";
617 echo '</td></tr><tr><td colspan="2"><span class="mono">' . "<b>$loc</b></span><br/>\n";
619 echo "</td></tr>\n";
622 if ($message != '') {
623 echo '<tr><td colspan="2" class="alarm">' . hsc($message) . "</td></tr>\n";
626 <tr><td colspan="2"><input type="submit" name="add_location" value="Add Contact"/></td></tr>
627 </table>
629 <p>Tap a contact name to see its hex value, or to rename or delete it.</p>
633 function drawAddLocation() {
634 global $newname, $newlocation;
635 global $commit;
637 $commit = 'commit';
640 <form name="entryForm" method="post" action=""><b>
642 writeSessionInfo();
643 hiddenValue('page');
644 hiddenValue('commit');
646 Enter name:<br/>
647 <input type="text" name="newname" style="font-size: 14pt;" size="25" value="<? echo $newname; ?>" onkeydown="submitOnRet(event, this.form)"/><br/>
648 Enter Contact, blank for random:<br/>
649 <input type="text" style="font-size: 11pt;" size="32" name="newlocation" value="<? echo $newlocation; ?>" onkeydown="submitOnRet(event, this.form)"/>
650 <table>
651 <tr>
652 <td><input type="button" style="font-size: 18pt;" value="7" onClick="Do7(this.form)"></td>
653 <td><input type="button" style="font-size: 18pt;" value="8" onClick="Do8(this.form)"></td>
654 <td><input type="button" style="font-size: 18pt;" value="9" onClick="Do9(this.form)"></td>
655 <td><input type="button" style="font-size: 18pt;" value="F" onClick="DoF(this.form)"></td>
656 </tr>
657 <tr>
658 <td><input type="button" style="font-size: 18pt;" value="4" onClick="Do4(this.form)"></td>
659 <td><input type="button" style="font-size: 18pt;" value="5" onClick="Do5(this.form)"></td>
660 <td><input type="button" style="font-size: 18pt;" value="6" onClick="Do6(this.form)"></td>
661 <td><input type="button" style="font-size: 18pt;" value="E" onClick="DoE(this.form)"></td>
662 </tr>
663 <tr>
664 <td><input type="button" style="font-size: 18pt;" value="1" onClick="Do1(this.form)"></td>
665 <td><input type="button" style="font-size: 18pt;" value="2" onClick="Do2(this.form)"></td>
666 <td><input type="button" style="font-size: 18pt;" value="3" onClick="Do3(this.form)"></td>
667 <td><input type="button" style="font-size: 18pt;" value="D" onClick="DoD(this.form)"></td>
668 </tr>
669 <tr>
670 <td><input type="button" style="font-size: 18pt;" value="0" onClick="Do0(this.form)"></td>
671 <td><input type="button" style="font-size: 18pt;" value="A" onClick="DoA(this.form)"></td>
672 <td><input type="button" style="font-size: 18pt;" value="B" onClick="DoB(this.form)"></td>
673 <td><input type="button" style="font-size: 18pt;" value="C" onClick="DoC(this.form)"></td>
674 </tr>
675 <tr>
676 </table>
677 <input type="button" style="font-size: 18pt;" value="Del" onClick="DoBksp(this.form)">
678 <input type="button" style="font-size: 18pt;" value="Clr" onClick="DoClr(this.form)">
679 <input type="button" style="font-size: 18pt;" value="Enter" onClick="DoEnter(this.form)"><br/>
680 <input type="button" style="font-size: 18pt;" value="Cancel" onClick="DoCancel(this.form)"><br/>
681 </b></form>
685 function login() {
686 global $folder, $folder_name, $folder_loc;
688 if ($folder == '') return refreshFolder();
689 $folder_name = $folder['name'];
690 $folder_loc = $folder['loc'];
691 return TRUE;
694 function refreshFolder() {
695 global $client, $passphrase, $session;
696 global $folder, $folderkv, $folder_name, $folder_loc;
698 $loc = FALSE;
700 if ($session != '') {
701 $res = $client->touch_archive($session, $url);
702 if ($res['status'] == 'success') $loc = $res['content'];
705 if (!$loc) {
706 if ($passphrase != '') {
707 $loc = $client->hash2location($client->sha256($passphrase));
708 $session = $client->folderSession($loc);
709 makeCiphers();
710 } else return FALSE;
713 $res = $client->touch_archive($loc, $url);
714 if ($res['status'] != 'success') return FALSE;
715 $folder = $client->parseFolder($loc, $res['content']);
716 $folderkv = $client->array2kv($folder);
718 $folder_name = $folder['name'];
719 $folder_loc = $folder['loc'];
720 return TRUE;
723 function fullRefresh() {
724 global $client, $folder, $values, $valueskv;
726 refreshFolder();
727 $values = scanFolder($folder);
728 $valueskv = $client->array2kv($values);
731 function blankToZero($x) {
732 if ($x == '') return 0;
733 return $x;
736 // This is currently n-squared for pretty big n, since it has to do
737 // a web call for each location/type pair.
738 // Patrick has promised a scan() function in the web API that would
739 // allow it to be done with a single call.
740 /* Now uses Patrick's scan() API call
741 function scanFolder($folder) {
742 global $client;
744 $types = $folder['types'];
745 $locs = $folder['locs'];
746 $values = array();
747 foreach ($locs as $locname => $loc) {
748 $loc_values = array();
749 foreach ($types as $typename => $type) {
750 $id = $type['id'];
751 $min_precision = $type['min_precision'];
752 $scale = $type['scale'];
753 $res = $client->touch($id, $loc, $url);
754 if ($res['status'] == 'success') {
755 $value = $client->applyScale($res['value'], $min_precision, $scale);
756 $loc_values[$typename] = $value;
759 $values[$locname] = $loc_values;
761 return $values;
765 function scanFolder($folder) {
766 global $client;
768 return $client->namedScan($folder['locs'], $folder['types'], FALSE, $url);
772 // Copyright 2008 Bill St. Clair
774 // Licensed under the Apache License, Version 2.0 (the "License");
775 // you may not use this file except in compliance with the License.
776 // You may obtain a copy of the License at
778 // http://www.apache.org/licenses/LICENSE-2.0
780 // Unless required by applicable law or agreed to in writing, software
781 // distributed under the License is distributed on an "AS IS" BASIS,
782 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
783 // See the License for the specific language governing permissions
784 // and limitations under the License.