Refresh folder on entering Locations page
[loomclient.git] / ip.php
blob57a7cb9c47191427619bd05e004c9294d0b33aaa
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 $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();
47 $encrypt_key = '';
48 include "ip-config.php";
49 $client->reenable_warnings();
51 $cipher = '';
52 $session_cipher = '';
54 if ($encrypt_key != '') {
55 makeCiphers();
56 if ($session_cipher != '') {
57 if ($folderkv != '') {
58 $folderkv = $session_cipher->decrypthex($folderkv);
60 if ($valueskv != '') {
61 $valueskv = $session_cipher->decrypthex($valueskv);
66 $onload = 'zip';
67 $folder = '';
68 $values = '';
69 $message = '';
71 if ($page == 'refresh') {
72 $folderkv = '';
73 $valueskv = '';
74 $page = 'main';
75 } else {
76 if ($folderkv != '') $folder = $client->parsekv($folderkv, TRUE);
77 if ($valueskv != '') $values = $client->parsekv($valueskv, TRUE);
80 if (($session == '' && $passphrase == '') || !login()) {
81 $onload = 'passphrase';
82 $page = 'login';
83 } else {
84 if ($values == '') {
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();
97 drawHead();
99 if ($page == 'login') drawLogin();
100 elseif ($page == 'main') drawMain();
101 elseif ($page == 'locations') drawLocations();
102 elseif ($page == 'add_location') drawAddLocation();
104 drawTail();
106 function doMain() {
107 global $qty, $type, $location, $take, $give;
108 global $client, $folder, $folder_loc, $folder_name;
109 global $values, $valueskv;
110 global $message;
112 $id = '';
114 if ($type != '-- choose asset --') {
115 $t = $folder['types'][$type];
116 if ($t) {
117 $id = $t['id'];
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);
129 $transferred = TRUE;
130 if ($count != '' && $id != '' && $loc != '') {
131 if ($take != '') {
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;
142 if ($transferred) {
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;
165 global $session;
166 global $message, $title, $onload, $page;
168 $onload = 'newname';
169 $title = 'Loom Locations';
171 if ($savename != '') {
172 if ($newname == $oldname) return;
173 if ($folder['locs'][$newname] != '') {
174 $message = "Duplicate Location Name";
175 return;
177 $res = $client->renameFolderLocation($session, $oldname, $newname);
178 //echo $res;
179 fullRefresh();
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 != '') {
194 if ($cipher == '') {
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;
209 $page = 'locations';
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';
216 } else {
217 if ($newlocation == '') $newlocation = $client->random->random_id();
218 $client->newFolderLocation($session, $newname, $newlocation);
219 fullRefresh();
220 $message = "Location created: $newname";
224 function doLogout() {
225 global $page, $folderkv, $valueskv;
226 global $client, $session;
228 $page = 'login';
229 $folderkv = '';
230 $valueskv = '';
231 $client->logout($session);
234 function drawHead() {
235 global $title, $onload;
237 <html>
238 <head>
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) {
258 selected.select();
259 setTimeout(hideAddressbar, 250);
262 <? additionalHTMLScripts(); ?>
264 </script>
266 <style type="text/css">
267 body { font-family: verdana, arial, sans-serif; font-size: 12pt }
268 div { font-size:12pt }
269 p { 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; }
299 </style>
301 </head>
302 <body onload="doOnLoad(document.forms[0].<? echo $onload; ?>)">
303 <table width="320px">
304 <tr><td>
308 function additionalHTMLScripts() {
309 global $page;
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;
332 var L = T.length;
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';
340 form.submit();
342 function submitOnRet(e, form) {
343 var keynum;
344 var keychar;
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;
349 DoEnter(form);
350 return false;
352 </script>
357 function drawTail() {
358 ?></td></tr>
359 </table>
360 </body>
361 </html>
365 function drawLogin() {
367 $host = $_SERVER["HTTP_HOST"];
368 if (!$host || $host == '') $host = $_SERVER["SERVER_NAME"];
371 <p>Welcome to
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
377 me.</p>
378 <form method="post" action="" autocomplete="off">
379 <input type="hidden" name="page" value="main"/>
380 <table width="99%">
381 <tr>
382 <td>Passphrase:</td>
383 <td><input type="password" name="passphrase" size="35" /></td>
384 </tr>
385 <tr>
386 <td></td>
387 <td><input type="submit" name="login" text="Login" /></td>
388 </tr>
389 </table>
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>
396 </form>
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)) {
412 $str = '';
413 foreach ($typevalues as $type => $value) {
414 if ($value != 0) {
415 if ($str == '') {
416 $str .= "<b>$name</b>\n";
417 $str .= '<table border="0"><tr><td width="50px">&nbsp;</td><td>';
418 $str .= '<table border="0">' . "\n";
420 $str .= '<tr><td align="right">' . $value . "<td><td>$type</td></tr>\n";
423 if ($str != '') {
424 echo $str;
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;
451 global $message;
453 $page = 'main';
456 <table border="0" width="99%" cellpadding="3">
457 <tr>
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>
459 &nbsp;
460 <a href="javascript:submitPage('locations');">Locations</a>
461 &nbsp;
462 <a href="javascript:submitPage('logout');">Logout</a>
463 </span></td>
464 </tr>
465 </table>
467 drawValues($folder_name, $values[$folder_name]);
469 <table border="0" width="99%">
470 <form name="mainform" method="post" action="" autocomplete="off">
472 writeSessionInfo();
473 hiddenValue('page');
475 <tr>
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>
478 </tr><tr>
479 <td></td>
480 <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";
490 </select>
491 </td>
492 </tr><tr>
493 <td></td>
494 <td>
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";
506 </select>
507 </td>
508 </tr><tr>
509 <td></td>
510 <td>
511 <input style="font-size: 10pt;" type="submit" name="take" value="Take"/>
512 <input style="font-size: 10pt;" type="submit" name="give" value="Give"/>
513 </td>
514 </tr>
516 if ($message != '') {
517 echo '<tr><td></td><td class="alarm">' . hsc($message) . "</td></tr>\n";
519 ?></table>
520 </form>
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;
534 global $message;
536 $page = 'locations';
538 <table border="0" width="99%" cellpadding="3">
539 <tr>
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>
541 &nbsp;
542 <a href="javascript:submitPage('locations');"><b>Locations</b></a>
543 &nbsp;
544 <a href="javascript:submitPage('logout');">Logout</a>
545 </span></td>
546 </tr>
547 </table>
548 <table border="0" width="99%">
549 <form name="mainform" method="post" action="" autocomplete="off">
551 writeSessionInfo();
552 echo '<input type="hidden" name="zip" value="' . $qty . '"/>' . "\n";
553 hiddenValue('type');
554 hiddenValue('location');
555 hiddenValue('newname');
556 hiddenValue('newlocation');
557 hiddenValue('page');
559 <input type="hidden" name="greendot" value=""/>
560 <table>
561 <tr>
562 <td valign="top"><a class=name_dot href="javascript: greenDot('<? echo $folder_name; ?>')" title="Edit Name">&nbsp;&bull;&nbsp;</a></td>
564 echo "<td>";
565 if ($greendot != $folder_name) echo "<b>$folder_name</b>";
566 else {
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";
572 echo "</td></tr>\n";
573 $locs = $folder['locs'];
574 foreach ($locs as $name => $loc) {
575 if ($name != $folder_name) {
577 <tr>
578 <td valign="top"><a class=name_dot href="javascript: greenDot('<? echo $name; ?>')" title="Edit Name or Delete Folder">&nbsp;&bull;&nbsp;</a></td>
580 echo "<td>";
581 if ($greendot != $name) echo $name;
582 else {
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";
587 echo "&nbsp;&nbsp;";
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";
591 echo "</td></tr>\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>
599 </table>
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;
607 global $commit;
609 $commit = 'commit';
612 <form name="entryForm" method="post" action=""><b>
614 writeSessionInfo();
615 hiddenValue('page');
616 hiddenValue('commit');
618 Enter name:<br/>
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)"/>
622 <table>
623 <tr>
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>
628 </tr>
629 <tr>
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>
634 </tr>
635 <tr>
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>
640 </tr>
641 <tr>
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>
646 </tr>
647 <tr>
648 </table>
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/>
653 </b></form>
657 function login() {
658 global $folder, $folder_name, $folder_loc;
660 if ($folder == '') return refreshFolder();
661 $folder_name = $folder['name'];
662 $folder_loc = $folder['loc'];
663 return TRUE;
666 function refreshFolder() {
667 global $client, $passphrase, $session;
668 global $folder, $folderkv, $folder_name, $folder_loc;
670 $loc = FALSE;
672 if ($session != '') {
673 $res = $client->touch_archive($session, $url);
674 if ($res['status'] == 'success') $loc = $res['content'];
677 if (!$loc) {
678 if ($passphrase != '') {
679 $loc = $client->hash2location($client->sha256($passphrase));
680 $session = $client->folderSession($loc);
681 makeCiphers();
682 } else return FALSE;
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'];
692 return TRUE;
695 function fullRefresh() {
696 global $client, $folder, $values, $valueskv;
698 refreshFolder();
699 $values = scanFolder($folder);
700 $valueskv = $client->array2kv($values);
703 function blankToZero($x) {
704 if ($x == '') return 0;
705 return $x;
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) {
714 global $client;
716 $types = $folder['types'];
717 $locs = $folder['locs'];
718 $values = array();
719 foreach ($locs as $locname => $loc) {
720 $loc_values = array();
721 foreach ($types as $typename => $type) {
722 $id = $type['id'];
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;
733 return $values;
737 function scanFolder($folder) {
738 global $client;
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.