Push to github
[loomclient.git] / ip.php
blob2f327a730dbc91f2cd9c0fb3ee481097c9b5cb00
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 $qty = $values[$contact][$type];
119 if ($contact != $folder_name) {
120 $location = $contact;
121 $take = "Take";
124 if ($type != '-- choose asset --') {
125 $t = $folder['types'][$type];
126 if ($t) {
127 $id = $t['id'];
128 $min_precision = $t['min_precision'];
129 if ($min_precision == '') $min_precision = 0;
130 $scale = $t['scale'];
131 if ($scale == '') $scale = 0;
134 if ($location != '-- choose contact --') {
135 $loc = $folder['locs'][$location];
137 if ($id != '' && $loc != '') {
138 if ($scale != '') $count = bcmul($qty, bcpow(10, $scale), 0);
139 $transferred = TRUE;
140 if ($count != '' && $id != '' && $loc != '') {
141 if ($take != '') {
142 $client->buy($id, $folder_loc, $folder_loc, $url);
143 $res = $client->move($id, $count, $loc, $folder_loc, $url);
144 $loc_orig = $location;
145 $loc_dest = $folder_name;
146 } else if ($give != '') {
147 $client->buy($id, $loc, $folder_loc, $url);
148 $res = $client->move($id, $count, $folder_loc, $loc, $url);
149 $loc_dest = $location;
150 $loc_orig = $folder_name;
151 } else $transferred = FALSE;
152 if ($transferred) {
153 $status = $res['status'];
154 if ($status == 'success') {
155 $value_orig = $client->applyScale($res['value_orig'], $min_precision, $scale);
156 $value_dest = $client->applyScale($res['value_dest'], $min_precision, $scale);
157 $values[$loc_orig][$type] = $value_orig;
158 ksort($values[$loc_orig]);
159 $values[$loc_dest][$type] = $value_dest;
160 ksort($values[$loc_dest]);
161 $valueskv = $client->array2kv($values);
162 } else $message = "Insufficient funds";
168 function doLocations() {
169 // Need to investigate how to delete.
170 // The "session" is an archive location containing the folder location
172 global $client, $folder, $values, $valueskv;
173 global $newname, $oldname;
174 global $savename, $delete, $add_location;
175 global $session;
176 global $message, $title, $onload, $page;
177 global $greendot, $cancel;
179 $onload = 'newname';
180 $title = 'Loom Contacts';
182 if ($savename != '') {
183 if ($newname == $oldname) return;
184 if ($folder['locs'][$newname] != '') {
185 $message = "Duplicate Contact Name";
186 return;
188 $res = $client->renameFolderLocation($session, $oldname, $newname);
189 //echo $res;
190 fullRefresh();
193 else if ($add_location != '') {
194 $title = 'Loom Add Contact';
195 $page = 'add_location';
198 else if ($greendot == '' && $cancel == '') refreshFolder();
202 function makeCiphers() {
203 global $cipher, $encrypt_key, $session, $session_cipher;
204 if ($encrypt_key != '') {
205 if ($cipher == '') {
206 $cipher = new Cipher($encrypt_key);
207 if ($session != '') $session = $cipher->decrypthex($session);
209 if ($session_cipher == '' && $session != '') {
210 $session_key = bcxorhex($session, $encrypt_key);
211 $session_cipher = new Cipher($session_key);
216 function doAddLocation() {
217 global $session, $commit, $page, $newname, $newlocation;
218 global $client, $message, $folder;
220 $page = 'locations';
222 if ($commit != 'commit') $message = 'Cancelled';
223 else if ($newname == '') $message = 'Blank name';
224 else if ($folder['locs'][$newname] != '') $message = "Name already exists";
225 else if ($newlocation != '' && !$client->isValidID($newlocation)) {
226 $message = 'Invalid location ID';
227 } else {
228 if ($newlocation == '') $newlocation = $client->random->random_id();
229 $client->newFolderLocation($session, $newname, $newlocation);
230 fullRefresh();
231 $message = "Contact created: $newname";
235 function doLogout() {
236 global $page, $folderkv, $valueskv;
237 global $client, $session;
239 $page = 'login';
240 $folderkv = '';
241 $valueskv = '';
242 $client->logout($session);
245 function drawHead() {
246 global $title, $onload;
248 <html>
249 <head>
250 <meta name="viewport" content="width=device-width" user-scalable="no" minimum-scale="1.0" maximum-scale="1.0"/>
251 <title><? echo $title; ?></title>
252 <link rel="apple-touch-icon" href="krugerand.png"/>
253 <link rel="shortcut icon" href="krugerand.png"/>
255 <script language="JavaScript">
256 function submitPage(page) {
257 document.forms["mainform"].page.value = page;
258 document.mainform.submit();
261 function greenDot(greendot, contact) {
262 document.forms["mainform"].greendot.value = greendot;
263 if (contact != "") document.forms["mainform"].contact.value = contact;
264 document.mainform.submit();
267 function hideAddressbar() {
268 window.scrollTo(0, 1);
271 function doOnLoad(selected) {
272 selected.select();
273 setTimeout(hideAddressbar, 250);
276 <? additionalHTMLScripts(); ?>
278 </script>
280 <style type="text/css">
281 body { font-family: verdana, arial, sans-serif; font-size: 12pt }
282 div { font-size:12pt }
283 p { font-size:12pt }
284 h1 { font-size:14pt }
285 h2 { font-size:12pt }
286 h3 { font-size:10pt }
287 td { font-size:12pt }
288 ul { font-size:12pt }
289 li { padding-bottom: 7px }
290 pre { font-family: verdana, arial, sans-serif; }
291 A:link, A:visited { color:blue; text-decoration:none }
292 A:hover { color:blue; text-decoration:underline }
293 A:active { color:#FAD805; text-decoration:underline }
294 .tt { font-family: Courier; font-size:10pt }
295 .mono { font-family: monospace; font-size: 11pt }
296 .large_mono { font-family: monospace; font-size: 10pt }
297 .giant_mono { font-family: monospace; font-size: 14pt }
298 .tiny_mono { font-family: monospace; font-size: 6pt }
299 .normal { font-size:10pt }
300 .smaller { font-size:6pt }
301 .small { font-size:8pt }
302 .large { font-size:12pt }
303 .alarm { color:red; font-weight: bold }
304 .focus_value { background-color:#DDDDDD }
305 .color_heading { margin-top:12px; padding:1px; background-color:#DDDDDD; width:100% }
306 A.label_link { font-weight:bold; }
307 A.highlight_link { font-weight:bold; }
308 A.cancel { background-color:#FFDDDD }
309 A.plain:link, A.plain:visited { color:black; text-decoration:none }
310 A.plain:hover { color:blue; text-decoration:underline }
311 A.plain:active { color:#FAD805; text-decoration:underline }
312 A.name_dot { font-size:16pt; font-weight:bold; color:green; }
313 </style>
315 </head>
316 <body onload="doOnLoad(document.forms[0].<? echo $onload; ?>)">
317 <table width="320px">
318 <tr><td>
322 function additionalHTMLScripts() {
323 global $page;
325 if ($page == 'add_location') {
328 function Do0(form) { form.newlocation.value = form.newlocation.value +"0" ;}
329 function Do1(form) { form.newlocation.value = form.newlocation.value +"1" ;}
330 function Do2(form) { form.newlocation.value = form.newlocation.value +"2" ;}
331 function Do3(form) { form.newlocation.value = form.newlocation.value +"3" ;}
332 function Do4(form) { form.newlocation.value = form.newlocation.value +"4" ;}
333 function Do5(form) { form.newlocation.value = form.newlocation.value +"5" ;}
334 function Do6(form) { form.newlocation.value = form.newlocation.value +"6" ;}
335 function Do7(form) { form.newlocation.value = form.newlocation.value +"7" ;}
336 function Do8(form) { form.newlocation.value = form.newlocation.value +"8" ;}
337 function Do9(form) { form.newlocation.value = form.newlocation.value +"9" ;}
338 function DoA(form) { form.newlocation.value = form.newlocation.value +"a" ;}
339 function DoB(form) { form.newlocation.value = form.newlocation.value +"b" ;}
340 function DoC(form) { form.newlocation.value = form.newlocation.value +"c" ;}
341 function DoD(form) { form.newlocation.value = form.newlocation.value +"d" ;}
342 function DoE(form) { form.newlocation.value = form.newlocation.value +"e" ;}
343 function DoF(form) { form.newlocation.value = form.newlocation.value +"f" ;}
344 function DoBksp(form)
345 { var T = form.newlocation.value;
346 var L = T.length;
347 var T2 = T.substr(0,L-1);
348 form.newlocation.value = T2;
350 function DoClr(form) { form.newlocation.value = ""; }
351 function DoEnter(form) { form.submit(); }
352 function DoCancel(form) {
353 form.commit.value = 'Cancel';
354 form.submit();
356 function submitOnRet(e, form) {
357 var keynum;
358 var keychar;
359 if (window.event) keynum = e.keyCode; // IE
360 else if (e.which) keynum = e.which; // Netscape/Firefox/Opera
361 keychar = String.fromCharCode(keynum);
362 if (keychar != "\n" && keychar != "\r") return true;
363 DoEnter(form);
364 return false;
366 </script>
371 function drawTail() {
372 ?></td></tr>
373 </table>
374 </body>
375 </html>
379 function drawLogin() {
381 $host = $_SERVER["HTTP_HOST"];
382 if (!$host || $host == '') $host = $_SERVER["SERVER_NAME"];
385 <p>Welcome to
386 <a href="https://loom.cc/">Loom</a> for iPhone. Note that in order to use this
387 confidently, you must trust that the PHP scripts at <? echo $host; ?> do
388 not steal your passphrase. I promise that unless somebody hacks my
389 site, the code running is what you can download from
390 <a href="./">here</a>, but don't trust that unless you know
391 me.</p>
392 <form method="post" action="" autocomplete="off">
393 <input type="hidden" name="page" value="main"/>
394 <table width="99%">
395 <tr>
396 <td>Passphrase:</td>
397 <td><input type="password" name="passphrase" size="35" /></td>
398 </tr>
399 <tr>
400 <td></td>
401 <td><input type="submit" name="login" text="Login" /></td>
402 </tr>
403 </table>
405 <p>One way to use this, without potentially 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 global $folder_name;
424 if ($name == $folder_name) $title = 'Move to Qty &amp; asset type';
425 else $title = 'Take';
426 if (is_array($typevalues)) {
427 $str = '';
428 foreach ($typevalues as $type => $value) {
429 $link = '<a class="plain" href="javascript: greenDot(' . "'" .
430 hsc($type) . "', '" . hsc($name) . "')" . '" title="' . $title . '">';
431 if ($value != 0) {
432 if ($str == '') {
433 $str .= "<b>" . hsc($name) . "</b>\n";
434 $str .= '<table border="0"><tr><td width="50px">&nbsp;</td><td>';
435 $str .= '<table border="0">' . "\n";
437 $str .= '<tr><td align="right">' . $link . $value . "</a></td>\n" .
438 "<td>" . $link . $type. "</a></td></tr>\n";
441 if ($str != '') {
442 echo $str;
443 echo "</table></td></tr></table>\n";
448 function writeSessionInfo() {
449 global $cipher, $session_cipher;
450 global $session, $folderkv, $valueskv;
452 $enc_session = $session;
453 $enc_folderkv = $folderkv;
454 $enc_valueskv = $valueskv;
456 if ($cipher) $enc_session = $cipher->encrypt2hex($enc_session);
457 if ($session_cipher) {
458 $enc_folderkv = $session_cipher->encrypt2hex($enc_folderkv);
459 $enc_valueskv = $session_cipher->encrypt2hex($enc_valueskv);
461 echo '<input type="hidden" name="session" value="' . hsc($enc_session) . '"/>' . "\n";
462 echo '<input type="hidden" name="folderkv" value="' . hsc($enc_folderkv) . '"/>' . "\n";
463 echo '<input type="hidden" name="valueskv" value="' . hsc($enc_valueskv) . '"/>' . "\n";
466 function drawMain() {
467 global $session, $folder, $values, $folder_name;
468 global $qty, $type, $location;
469 global $message;
471 $page = 'main';
474 <table border="0" width="99%" cellpadding="3">
475 <tr>
476 <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>
477 &nbsp;
478 <a href="javascript:submitPage('locations');">Contacts</a>
479 &nbsp;
480 <a href="javascript:submitPage('logout');">Logout</a>
481 </span></td>
482 </tr>
483 </table>
485 drawValues($folder_name, $values[$folder_name]);
487 <table border="0" width="99%">
488 <form name="mainform" method="post" action="" autocomplete="off">
489 <input type="hidden" name="greendot" value=""/>
490 <input type="hidden" name="contact" value=""/>
492 writeSessionInfo();
493 hiddenValue('page');
495 <tr>
496 <td align="right">Qty:</td>
497 <td><input style="font-size: 12pt;" type="text" size="25" name="zip" value="<? echo $qty; ?>" style="text-align:right;"></td>
498 </tr><tr>
499 <td></td>
500 <td>
501 <select name="type" style="font-size: 10pt;">
502 <option value="">-- choose asset --</option>
504 foreach ($folder['types'] as $typename => $typearray) {
505 echo '<option value="' . hsc($typename) . '"';
506 if ($type == $typename) echo ' selected="selected"';
507 echo '>' . hsc($typename) . "</option>\n";
510 </select>
511 </td>
512 </tr><tr>
513 <td></td>
514 <td>
515 <select name="location" style="font-size: 10pt;">
516 <option value="">-- choose contact --</option>
518 foreach($values as $loc => $value) {
519 if ($loc != $folder_name) {
520 echo '<option value="' . hsc($loc) . '"';
521 if ($loc == $location) echo ' selected="selected"';
522 echo '>' . hsc($loc) . "</option>\n";
526 </select>
527 </td>
528 </tr><tr>
529 <td></td>
530 <td>
531 <input style="font-size: 10pt;" type="submit" name="give" value="Give"/>
532 <input style="font-size: 10pt;" type="submit" name="take" value="Take"/>
533 </td>
534 </tr>
536 if ($message != '') {
537 echo '<tr><td></td><td class="alarm">' . hsc($message) . "</td></tr>\n";
539 ?></table>
541 foreach ($values as $name => $typevalues) {
542 if ($name != $folder_name) {
543 drawValues($name, $typevalues);
547 <p>Tap a value or asset name to take all the assets in that line.</p>
548 </form>
552 function drawLocations() {
553 global $session, $folder, $values, $folder_name;
554 global $qty, $type, $location, $greendot;
555 global $message;
557 $page = 'locations';
559 <table border="0" width="99%" cellpadding="3">
560 <tr>
561 <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>
562 &nbsp;
563 <a href="javascript:submitPage('locations');"><b>Contacts</b></a>
564 &nbsp;
565 <a href="javascript:submitPage('logout');">Logout</a>
566 </span></td>
567 </tr>
568 </table>
569 <table border="0" width="99%">
570 <form name="mainform" method="post" action="" autocomplete="off">
572 writeSessionInfo();
573 echo '<input type="hidden" name="zip" value="' . $qty . '"/>' . "\n";
574 hiddenValue('type');
575 hiddenValue('location');
576 hiddenValue('newname');
577 hiddenValue('newlocation');
578 hiddenValue('page');
580 <input type="hidden" name="greendot" value=""/>
581 <table>
582 <tr>
583 <td valign="top">
584 <a class="name_dot" href="javascript: greenDot('<? echo $folder_name; ?>', '')" title="Edit Name">&nbsp;&bull;&nbsp;</a>
585 </td>
587 echo "<td>";
588 if ($greendot != $folder_name) {
589 echo '<a class="plain" href="javascript: greenDot(' . "'" .
590 hsc($folder_name) . "', '')" . '" title="Edit Name"><b>' .
591 $folder_name . '</b></a>';
592 } else {
593 echo '<input style="font-size: 12pt;" type="text" size="25" name="newname" value="' . hsc($folder_name) . '"/><br/>' . "\n";
594 echo '<input type="hidden" name="oldname" value="' . hsc($folder_name) . '"/>' . "\n";
595 echo '<input type="submit" name="savename" value="Save"/>' . "\n";
596 echo '<input type="submit" name="cancel" value="Cancel"/>' . "\n";
598 echo "</td></tr>\n";
599 $locs = $folder['locs'];
600 foreach ($locs as $name => $loc) {
601 if ($name != $folder_name) {
603 <tr>
604 <td valign="top">
605 <a class=name_dot href="javascript: greenDot('<? echo $name; ?>', '')" title="Edit Name or Delete Folder">&nbsp;&bull;&nbsp;</a>
606 </td>
608 echo "<td>";
609 if ($greendot != $name) {
610 echo '<a class="plain" href="javascript: greenDot(' . "'" .
611 hsc($name) . "', '')" . '" title="Edit Name or Delete Wallet">' .
612 $name . '</a>';
613 } else {
614 echo '<input style="font-size: 12pt;" type="text" size="25" name="newname" value="' . hsc($name) . '"/><br/>' . "\n";
615 echo '<input type="hidden" name="oldname" value="' . hsc($name) . '"/>' . "\n";
616 echo '<input type="submit" name="savename" value="Save"/>' . "\n";
617 echo '<input type="submit" name="cancel" value="Cancel"/>' . "\n";
618 echo "&nbsp;&nbsp;";
619 echo '<input type="submit" disabled name="delete" value="Delete..."/><br/>' . "\n";
620 echo '</td></tr><tr><td colspan="2"><span class="mono">' . "<b>$loc</b></span><br/>\n";
622 echo "</td></tr>\n";
625 if ($message != '') {
626 echo '<tr><td colspan="2" class="alarm">' . hsc($message) . "</td></tr>\n";
629 <tr><td colspan="2"><input type="submit" name="add_location" value="Add Contact"/></td></tr>
630 </table>
632 <p>Tap a contact name to see its hex value, or to rename or delete it.</p>
636 function drawAddLocation() {
637 global $newname, $newlocation;
638 global $commit;
640 $commit = 'commit';
643 <form name="entryForm" method="post" action=""><b>
645 writeSessionInfo();
646 hiddenValue('page');
647 hiddenValue('commit');
649 Enter name:<br/>
650 <input type="text" name="newname" style="font-size: 14pt;" size="25" value="<? echo $newname; ?>" onkeydown="submitOnRet(event, this.form)"/><br/>
651 Enter Contact, blank for random:<br/>
652 <input type="text" style="font-size: 11pt;" size="32" name="newlocation" value="<? echo $newlocation; ?>" onkeydown="submitOnRet(event, this.form)"/>
653 <table>
654 <tr>
655 <td><input type="button" style="font-size: 18pt;" value="7" onClick="Do7(this.form)"></td>
656 <td><input type="button" style="font-size: 18pt;" value="8" onClick="Do8(this.form)"></td>
657 <td><input type="button" style="font-size: 18pt;" value="9" onClick="Do9(this.form)"></td>
658 <td><input type="button" style="font-size: 18pt;" value="F" onClick="DoF(this.form)"></td>
659 </tr>
660 <tr>
661 <td><input type="button" style="font-size: 18pt;" value="4" onClick="Do4(this.form)"></td>
662 <td><input type="button" style="font-size: 18pt;" value="5" onClick="Do5(this.form)"></td>
663 <td><input type="button" style="font-size: 18pt;" value="6" onClick="Do6(this.form)"></td>
664 <td><input type="button" style="font-size: 18pt;" value="E" onClick="DoE(this.form)"></td>
665 </tr>
666 <tr>
667 <td><input type="button" style="font-size: 18pt;" value="1" onClick="Do1(this.form)"></td>
668 <td><input type="button" style="font-size: 18pt;" value="2" onClick="Do2(this.form)"></td>
669 <td><input type="button" style="font-size: 18pt;" value="3" onClick="Do3(this.form)"></td>
670 <td><input type="button" style="font-size: 18pt;" value="D" onClick="DoD(this.form)"></td>
671 </tr>
672 <tr>
673 <td><input type="button" style="font-size: 18pt;" value="0" onClick="Do0(this.form)"></td>
674 <td><input type="button" style="font-size: 18pt;" value="A" onClick="DoA(this.form)"></td>
675 <td><input type="button" style="font-size: 18pt;" value="B" onClick="DoB(this.form)"></td>
676 <td><input type="button" style="font-size: 18pt;" value="C" onClick="DoC(this.form)"></td>
677 </tr>
678 <tr>
679 </table>
680 <input type="button" style="font-size: 18pt;" value="Del" onClick="DoBksp(this.form)">
681 <input type="button" style="font-size: 18pt;" value="Clr" onClick="DoClr(this.form)">
682 <input type="button" style="font-size: 18pt;" value="Enter" onClick="DoEnter(this.form)"><br/>
683 <input type="button" style="font-size: 18pt;" value="Cancel" onClick="DoCancel(this.form)"><br/>
684 </b></form>
688 function login() {
689 global $folder, $folder_name, $folder_loc;
691 if ($folder == '') return refreshFolder();
692 $folder_name = $folder['name'];
693 $folder_loc = $folder['loc'];
694 return TRUE;
697 function refreshFolder() {
698 global $client, $passphrase, $session;
699 global $folder, $folderkv, $folder_name, $folder_loc;
701 $loc = FALSE;
703 if ($session != '') {
704 $res = $client->touch_archive($session, $url);
705 if ($res['status'] == 'success') $loc = $res['content'];
708 if (!$loc) {
709 if ($passphrase != '') {
710 $loc = $client->hash2location($client->sha256($passphrase));
711 $session = $client->folderSession($loc);
712 makeCiphers();
713 } else return FALSE;
716 $res = $client->touch_archive($loc, $url);
717 if ($res['status'] != 'success') return FALSE;
718 $folder = $client->parseFolder($loc, $res['content']);
719 $folderkv = $client->array2kv($folder);
721 $folder_name = $folder['name'];
722 $folder_loc = $folder['loc'];
723 return TRUE;
726 function fullRefresh() {
727 global $client, $folder, $values, $valueskv;
729 refreshFolder();
730 $values = scanFolder($folder);
731 $valueskv = $client->array2kv($values);
734 function blankToZero($x) {
735 if ($x == '') return 0;
736 return $x;
739 // This is currently n-squared for pretty big n, since it has to do
740 // a web call for each location/type pair.
741 // Patrick has promised a scan() function in the web API that would
742 // allow it to be done with a single call.
743 /* Now uses Patrick's scan() API call
744 function scanFolder($folder) {
745 global $client;
747 $types = $folder['types'];
748 $locs = $folder['locs'];
749 $values = array();
750 foreach ($locs as $locname => $loc) {
751 $loc_values = array();
752 foreach ($types as $typename => $type) {
753 $id = $type['id'];
754 $min_precision = $type['min_precision'];
755 $scale = $type['scale'];
756 $res = $client->touch($id, $loc, $url);
757 if ($res['status'] == 'success') {
758 $value = $client->applyScale($res['value'], $min_precision, $scale);
759 $loc_values[$typename] = $value;
762 $values[$locname] = $loc_values;
764 return $values;
768 function scanFolder($folder) {
769 global $client;
771 return $client->namedScan($folder['locs'], $folder['types'], FALSE, $url);
774 /* ***** BEGIN LICENSE BLOCK *****
775 * Version: MPL 1.1/GPL 2.0/LGPL 2.1/Apache 2.0
777 * The contents of this file are subject to the Mozilla Public License Version
778 * 1.1 (the "License"); you may not use this file except in compliance with
779 * the License. You may obtain a copy of the License at
780 * http://www.mozilla.org/MPL/
782 * Software distributed under the License is distributed on an "AS IS" basis,
783 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
784 * for the specific language governing rights and limitations under the
785 * License.
787 * The Original Code is LoomClient PHP library
789 * The Initial Developer of the Original Code is
790 * Bill St. Clair.
791 * Portions created by the Initial Developer are Copyright (C) 2008
792 * the Initial Developer. All Rights Reserved.
794 * Contributor(s):
795 * Bill St. Clair <bill@billstclair.com>
797 * Alternatively, the contents of this file may be used under the
798 * terms of the GNU General Public License Version 2 or later (the
799 * "GPL"), the GNU Lesser General Public License Version 2.1 or later
800 * (the "LGPL"), or The Apache License Version 2.0 (the "AL"), in
801 * which case the provisions of the GPL, LGPL, or AL are applicable
802 * instead of those above. If you wish to allow use of your version of
803 * this file only under the terms of the GPL, the LGPL, or the AL, and
804 * not to allow others to use your version of this file under the
805 * terms of the MPL, indicate your decision by deleting the provisions
806 * above and replace them with the notice and other provisions
807 * required by the GPL or the LGPL. If you do not delete the
808 * provisions above, a recipient may use your version of this file
809 * under the terms of any one of the MPL, the GPL the LGPL, or the AL.
810 ****** END LICENSE BLOCK ***** */