Update viewtext.php to make search and line numbers more accessible.
[loomclient.git] / grid-tutorial.php
blobf384af3c1a0f30d88bb08a171b02672ae2b31c1d
1 <?php
2 require_once "LoomClient.php";
3 require_once "LoomRandom.php";
4 require_once "bcbitwise.php";
5 //require_once "Diceware.php"; // required below only when used (it's big)
7 function mq($x) {
8 if (get_magic_quotes_gpc()) return stripslashes($x);
9 else return $x;
12 $page = mq($_GET['page']);
13 if ($page == '') $page = 'grid';
14 $common_type = mq($_POST['common_type']);
15 $default_server = 'https://loom.cc/';
16 $loom_server = mq($_REQUEST['loom_server']);
17 if ($loom_server == '') $loom_server = $default_server;
18 if (substr($loom_server, -1) != '/') $loom_server .= '/';
19 $buy_loc = mq($_POST['buy_loc']);
20 $buy_usage = mq($_POST['buy_usage']);
21 $issuer_orig = mq($_POST['issuer_orig']);
22 $issuer_dest = mq($_POST['issuer_dest']);
23 $touch_loc = mq($_POST['touch_loc']);
24 $look_hash = mq($_POST['look_hash']);
25 $move_qty = mq($_POST['move_qty']);
26 $move_orig = mq($_POST['move_orig']);
27 $move_dest = mq($_POST['move_dest']);
28 $content = mq($_POST['content']);
29 $id = mq($_POST['id']);
30 $passphrase = mq($_POST['passphrase']);
31 $idhash = mq($_POST['idhash']);
33 function maybe_echo_server($q) {
34 global $loom_server, $default_server;
35 if ($loom_server != $default_server) {
36 echo $q . 'loom_server=' . urlencode($loom_server);
40 <html>
41 <head>
42 <title>Loom Grid &amp; Archive Tutorials in PHP</title>
43 <link rel="shortcut icon" href="krugerand.png"/>
44 <style type="text/css">
45 body { font-family: verdana, arial, sans-serif; font-size: 10pt }
46 div { font-size:10pt }
47 p { font-size:10pt }
48 h1 { font-size:12pt }
49 h2 { font-size:10pt }
50 h3 { font-size:9pt }
51 td { font-size:10pt }
52 ul { font-size:10pt }
53 li { padding-bottom: 7px }
54 pre { font-family: verdana, arial, sans-serif; }
55 A:link, A:visited { color:blue; text-decoration:none }
56 A:hover { color:blue; text-decoration:underline }
57 A:active { color:#FAD805; text-decoration:underline }
58 .tt { font-family: Courier; font-size:10pt }
59 .mono { font-family: monospace; font-size: 8pt }
60 .large_mono { font-family: monospace; font-size: 10pt }
61 .giant_mono { font-family: monospace; font-size: 14pt }
62 .tiny_mono { font-family: monospace; font-size: 6pt }
63 .normal { font-size:10pt }
64 .smaller { font-size:6pt }
65 .small { font-size:8pt }
66 .large { font-size:12pt }
67 .alarm { color:red }
68 .focus_value { background-color:#DDDDDD }
69 .color_heading { margin-top:12px; padding:1px; background-color:#DDDDDD; width:100% }
70 A.label_link { font-weight:bold; }
71 A.highlight_link { font-weight:bold; }
72 A.cancel { background-color:#FFDDDD }
73 A.plain:link, A.plain:visited { color:black; text-decoration:none }
74 A.plain:hover { color:blue; text-decoration:underline }
75 A.plain:active { color:#FAD805; text-decoration:underline }
76 A.name_dot { font-size:14pt; font-weight:bold; color:green; }
77 </style></head>
78 <body>
79 <!--
80 <pre>
81 <?php
82 print_r($_POST);
84 </pre>
85 -->
86 This is a PHP translation of the Loom.cc
87 <?php
88 if ($page == 'grid') {
89 echo '<a href="https://loom.cc/?function=grid_tutorial&mode=advanced">grid tutorial</a>.';
90 } elseif ($page == 'archive') {
91 echo '<a href="https://loom.cc/?function=archive_tutorial&mode=advanced">archive tutorial</a>.';
92 } else {
93 echo 'Tools page (login required).';
96 Note that if you enter any real locations containing valuable assets in this page, those locations are being transferred through my web site to loom.cc. All trasmissions are encrypted, so it's unlikely that anybody can steal your valuable information en-route, but you ARE trusting me to not snarf your locations with my PHP code. I promise that unless somebody hacks my site, the code that is running is the code I've posted at the link below, which does NOT save your information anywhere, just passes it on to loom.cc. But if you don't trust me, don't give away anything important.
97 <p>
98 Download source at
99 <a href="../loomclient.tar.gz">loomclient.tar.gz</a>
101 <hr>
102 <a href="index.html">Loom Index</a> |
103 <a href="grid-tutorial.php<? maybe_echo_server('?'); ?>">
104 <?php
105 if ($page == 'grid') {
106 echo '<b>Grid</b>';
107 } else echo 'Grid';
109 </a> |
110 <a href="grid-tutorial.php?page=archive<? maybe_echo_server('&'); ?>">
111 <?php
112 if ($page == 'archive') {
113 echo '<b>Archive</b>';
114 } else echo 'Archive';
116 </a> |
117 <a href="grid-tutorial.php?page=tools<? maybe_echo_server('&'); ?>">
118 <?php
119 if ($page == 'tools') {
120 echo '<b>Tools</b>';
121 } else echo 'Tools';
123 </a>
124 <hr>
125 <?php
126 // Process the post
128 $client = new LoomClient($loom_server);
129 $res = '';
131 if ($page == 'grid') {
132 $buy_message = '';
133 $issuer_message = '';
134 $touch_message = '';
135 $look_message = '';
136 $move_message = '';
137 if ($_POST['buy'] != '') {
138 $res = $client->buy($common_type, $buy_loc, $buy_usage, &$url);
139 checkResult($res, 'usage_balance', 'error_loc', &$buy_message);
140 } elseif ($_POST['sell'] != '') {
141 $res = $client->sell($common_type, $buy_loc, $buy_usage, &$url);
142 checkResult($res, 'usage_balance', 'error_loc', &$buy_message);
143 } elseif ($_POST['issuer'] != '') {
144 $res = $client->issuer($common_type, $issuer_orig, $issuer_dest, &$url);
145 checkResult($res, 'status', 'status', &$issuer_message);
146 } elseif ($_POST['touch'] != '') {
147 $res = $client->touch($common_type, $touch_loc, &$url);
148 checkResult($res, 'value', 'error_loc', &$touch_message);
149 } elseif ($_POST['look'] != '') {
150 $res = $client->look($common_type, $look_hash, &$url);
151 checkResult($res, 'value', 'error_loc', &$look_message);
152 } elseif ($_POST['move'] != '') {
153 $res = $client->move($common_type, $move_qty, $move_orig, $move_dest, &$url);
154 checkResult($res, 'value_dest', 'error_qty', &$move_message);
155 } elseif ($_POST['move_back'] != '') {
156 $res = $client->move($common_type, $move_qty, $move_dest, $move_orig, &$url);
157 checkResult($res, 'value_orig', 'error_qty', &$move_message);
159 } else if ($page == 'archive') {
160 if ($_POST['look_archive'] != '') {
161 $res = $client->look_archive($look_hash, &$url);
162 if ($res != '') $content = htmlspecialchars($res['content']);
163 } elseif ($_POST['touch_archive'] != '') {
164 $res = $client->touch_archive($touch_loc, &$url);
165 if ($res != '') {
166 $content = htmlspecialchars($res['content']);
167 if ($res['hash'] != '') $look_hash = $res['hash'];
169 } elseif ($_POST['buy_archive'] != '') {
170 $res = $client->buy_archive($touch_loc, $buy_usage, &$url);
171 } elseif ($_POST['sell_archive'] != '') {
172 $res = $client->sell_archive($touch_loc, $buy_usage, &$url);
173 } elseif ($_POST['write_archive'] != '') {
174 $res = $client->write_archive($touch_loc, $buy_usage, html_entity_decode($content), &$url);
175 if ($res['hash'] != '') $look_hash = $res['hash'];
177 } else {
178 if ($_POST['random_id'] != '') {
179 $random = new LoomRandom();
180 $id = $random->random_id();
181 $idhash = '';
182 $passphrase = '';
183 } elseif ($_POST['id_hash'] != '') {
184 $idhash = $client->sha256($client->hex2bin($id));
185 } elseif ($_POST['random_passphrase'] != '') {
186 require_once "Diceware.php";
187 if (!isset($diceware)) $diceware = new Diceware();
188 $passphrase = $diceware->random_words(5);
189 $id = '';
190 $idhash = '';
191 } elseif ($_POST['hash_passphrase'] != '') {
192 $hash = $client->sha256($passphrase);
193 $id = $client->hash2location($hash);
194 $idhash = '';
198 if ($page == 'grid') print_grid();
199 else if ($page == 'archive') print_archive();
200 else print_tools();
202 if ($res != '') {
203 echo "<p><b>URL</b><p>$url<p><b>Result (in KV format)</b><p><pre>";
204 echo "(\n";
205 foreach ($res as $key => $value) {
206 echo ":" . htmlspecialchars($client->quote_cstring($key)) . "\n";
207 echo "=" . htmlspecialchars($client->quote_cstring($value)) . "\n";
209 echo ")\n";
210 echo '</pre>';
213 </body>
214 </html>
216 <?php
218 function checkResult($res, $success_key, $fail_key, &$var) {
219 $status = $res['status'];
220 if ($status == 'success') $var = $res[$success_key];
221 else $var = $res[$fail_key];
222 if ($var == '') $var = $status;
225 function hsc($text) {
226 return htmlspecialchars($text);
229 function print_grid() {
230 global $common_type, $loom_server, $buy_loc, $buy_usage, $issuer_orig;
231 global $issuer_dest, $touch_loc, $look_hash, $move_qty, $move_orig, $move_dest;
232 global $buy_message, $issuer_message, $touch_message, $look_message, $move_message;
236 This is an interactive tutorial to help you set up and test grid operations.
237 Be careful using sensitive locations in this tutorial, since they do show
238 up in plain text on this screen. The grid API is <a href="https://loom.cc/?function=help&amp;topic=grid&amp;mode=advanced">fully documented</a>.
241 <form method=post action="grid-tutorial.php" autocomplete=off>
242 <table border=1 cellpadding=10 style='border-collapse:collapse'>
244 <tr>
245 <td>
246 <table border=0 style='border-collapse:collapse'>
248 <colgroup>
249 <col width=150>
250 <col width=600>
251 </colgroup>
254 <tr>
255 <td>
256 Asset Type:
257 </td>
258 <td>
259 <input type=text class=tt name=common_type size=36 value=<? echo hsc($common_type); ?>>
260 </td>
261 </tr>
263 <tr>
264 <td>
265 Loom server:
266 </td>
267 <td>
268 <input type=text name=loom_server size=50 value=<? echo hsc($loom_server); ?>>
269 </td>
270 </tr>
272 </table>
274 </td>
275 </tr>
277 <tr>
278 <td>
279 <table border=0 style='border-collapse:collapse'>
280 <colgroup>
281 <col width=150>
282 <col width=600>
283 </colgroup>
286 <tr>
287 <td>
288 <input type=submit name=buy value="Buy">
289 <input type=submit name=sell value="Sell">
290 </td>
292 <td class=small>
293 Buy location for one usage token, or sell location for refund.
294 </td>
295 </tr>
297 <tr>
298 <td>
299 Location:
300 </td>
301 <td>
302 <input type=text class=tt name=buy_loc size=36 value=<? echo hsc($buy_loc); ?>>
303 <? echo $buy_message; ?>
304 </td>
305 </tr>
307 <tr>
308 <td>
309 Usage location:
311 </td>
312 <td>
313 <input type=text class=tt name=buy_usage size=36 value=<? echo hsc($buy_usage); ?>>
314 </td>
315 </tr>
317 </table>
319 </td>
320 </tr>
322 <tr>
323 <td>
324 <table border=0 style='border-collapse:collapse'>
325 <colgroup>
326 <col width=150>
327 <col width=600>
329 </colgroup>
332 <tr>
333 <td>
334 <input type=submit name=issuer value="Issuer">
335 </td>
336 <td class=small>
337 Change the issuer location for a type.
338 </td>
339 </tr>
341 <tr>
342 <td>
343 Current Issuer:
344 </td>
345 <td>
347 <input type=text class=tt name=issuer_orig size=36 value=<? echo hsc($issuer_orig);?>>
348 <? echo $issuer_message; ?>
349 </td>
350 </tr>
352 <tr>
353 <td>
354 New Issuer:
355 </td>
356 <td>
357 <input type=text class=tt name=issuer_dest size=36 value=<? echo hsc($issuer_dest); ?>>
358 </td>
359 </tr>
361 </table>
363 </td>
364 </tr>
366 <tr>
367 <td>
368 <table border=0 style='border-collapse:collapse'>
369 <colgroup>
370 <col width=150>
371 <col width=600>
372 </colgroup>
375 <tr>
376 <td>
377 <input type=submit name=touch value="Touch">
378 </td>
379 <td class=small>
380 Touch a location directly to see its value.
381 </td>
383 </tr>
385 <tr>
386 <td>
387 Location:
388 </td>
389 <td>
390 <input type=text class=tt name=touch_loc size=36 value=<? echo hsc($touch_loc); ?>>
391 <? echo $touch_message; ?>
392 </td>
393 </tr>
395 </table>
397 </td>
398 </tr>
400 <tr>
402 <td>
403 <table border=0 style='border-collapse:collapse'>
404 <colgroup>
405 <col width=150>
406 <col width=600>
407 </colgroup>
410 <tr>
411 <td>
412 <input type=submit name=look value="Look">
413 </td>
414 <td class=small>
415 Look at a location by its hash.
416 </td>
417 </tr>
419 <tr>
420 <td>
421 Hash:
422 </td>
423 <td>
424 <input type=text class=tt name=look_hash size=72 value=<? echo hsc($look_hash); ?>>
425 <? echo $look_message; ?>
426 </td>
427 </tr>
429 </table>
431 </td>
432 </tr>
434 <tr>
435 <td>
436 <table border=0 style='border-collapse:collapse'>
438 <colgroup>
439 <col width=150>
440 <col width=600>
441 </colgroup>
444 <tr>
445 <td>
446 <input type=submit name=move value="Move">
447 <input type=submit name=move_back value="Back">
448 </td>
449 <td class=small>
450 Move units from one location to another.
451 </td>
452 </tr>
454 <tr>
456 <td>
457 Quantity:
458 </td>
459 <td>
460 <input type=text class=tt name=move_qty size=45 value=<? echo hsc($move_qty); ?>>
461 </td>
462 </tr>
464 <tr>
465 <td>
466 Origin:
467 </td>
468 <td>
469 <input type=text class=tt name=move_orig size=36 value=<? echo hsc($move_orig); ?>>
470 </td>
471 </tr>
473 <tr>
474 <td>
475 Destination:
476 </td>
477 <td>
478 <input type=text class=tt name=move_dest size=36 value=<? echo hsc($move_dest); ?>>
479 <? echo $move_message; ?>
480 </td>
481 </tr>
483 </table>
485 </td>
486 </tr>
488 </table>
490 </form>
491 <?php
494 function print_archive() {
495 global $loom_server, $look_hash, $touch_loc, $buy_usage, $content;
497 <form method=post action="grid-tutorial.php?page=archive" autocomplete=off>
500 This screen is a simple user interface into the archive function <a href="https://loom.cc/?function=help&amp;topic=archive&amp;mode=advanced">documented here</a>.
501 It also serves as a tutorial, showing you the API url and result.
504 <table border=0 style='border-collapse:collapse'>
505 <colgroup>
506 <col width=150>
507 <col width=700>
509 </colgroup>
511 <tr>
512 <td>
513 Loom server:
514 </td>
515 <td>
516 <input type=text name=loom_server size=50 value=<? echo hsc($loom_server); ?>>
517 </td>
518 </tr>
519 <tr>
520 <td>
521 Archive Hash:
522 </td>
523 <td>
524 <input type=text class=tt name=look_hash size=72 value=<? echo hsc($look_hash); ?>>
525 <input type=submit name=look_archive value="Look">
526 </td>
527 </tr>
529 <tr>
530 <td>
531 Archive Location:
532 </td>
533 <td>
535 <input type=text class=tt name=touch_loc size=36 value=<? echo hsc($touch_loc); ?>>
536 <input type=submit name=touch_archive value="Touch">
537 <input type=submit name=buy_archive value="Buy">
538 <input type=submit name=sell_archive value="Sell">
540 </td>
541 </tr>
543 <tr>
544 <td>
545 Usage Location:
546 </td>
547 <td>
548 <input type=text class=tt name=buy_usage size=36 value=<? echo hsc($buy_usage); ?>>
549 </td>
550 </tr>
552 <tr>
553 <td>
554 Archive Content:
555 </td>
556 <td>
557 <a href="<? echo hsc($loom_server); ?>?function=view&hash=<? echo hsc($look_hash); ?>" title="View as web page">View</a>
558 <input type=submit name=write_archive value="Write">
560 </td>
561 </tr>
562 <tr>
563 <td colspan=2>
564 <textarea name=content rows=20 cols=120>
565 <? echo hsc($content); ?>
566 </textarea>
567 </td>
568 </tr>
570 </table>
571 </form>
573 <?php
576 function print_tools() {
577 global $loom_server, $id, $passphrase, $idhash;
579 <form method=post action="grid-tutorial.php?page=tools" autocomplete=off>
580 <h1> Tools </h1>
583 Here is an assortment of tools which you may occasionally find useful.
585 On this panel, you may generate a new random identifier or random passphrase.
586 You may also compute the "hash" of a passphrase, which converts a passphrase
587 into an identifier. You may enter the passphrase manually if you don't want
588 to use a random one.
590 <table border=0 cellpadding=5 style='border-collapse:collapse'>
591 <colgroup>
592 <col width=100>
593 <col width=600>
594 </colgroup>
596 <tr>
597 <td>
598 Loom server:
599 </td>
600 <td>
601 <input type=text name=loom_server size=50 value=<? echo hsc($loom_server); ?>>
602 </td>
603 </tr>
605 <tr>
606 <td>Hash:</td>
607 <td>
608 <input type=text class=tt name=idhash size=72 value="<? echo hsc($idhash); ?>">
609 </td>
610 </tr>
612 <tr>
613 <td>Identifier:</td>
614 <td>
615 <input type=text class=tt name=id size=36 value="<? echo hsc($id); ?>">
616 <input type=submit name=random_id value="Random">
617 <input type=submit name=id_hash value="Hash">
618 </td>
619 </tr>
621 <tr>
622 <td>Passphrase:</td>
623 <td>
624 <input type=text name=passphrase size=50 value="<? echo hsc($passphrase); ?>">
625 <input type=submit name=random_passphrase value="Random">
626 <input type=submit name=hash_passphrase value="Hash">
627 </td>
628 </tr>
629 </table>
631 </form>
633 <?php
637 // Copyright 2008 Bill St. Clair
639 // Licensed under the Apache License, Version 2.0 (the "License");
640 // you may not use this file except in compliance with the License.
641 // You may obtain a copy of the License at
643 // http://www.apache.org/licenses/LICENSE-2.0
645 // Unless required by applicable law or agreed to in writing, software
646 // distributed under the License is distributed on an "AS IS" BASIS,
647 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
648 // See the License for the specific language governing permissions
649 // and limitations under the License.