index.php
changeset 0 ac947994d4a1
child 2 def69d70eb6e
child 10 2a05edf9dc87
equal deleted inserted replaced
-1:000000000000 0:ac947994d4a1
       
     1 <?php
       
     2 
       
     3 const FILE = "var/abook.txt";	// parent dir must exist
       
     4 const RS = "\n";
       
     5 const FS = "\t";
       
     6 
       
     7 function open_db($file) {
       
     8 	@mkdir(dirname($file));
       
     9 
       
    10 	$fh = fopen($file, "a+b");
       
    11 	flock($fh, LOCK_EX);
       
    12 	$stat = fstat($fh);
       
    13 	if ($stat['size'] == 0) {
       
    14 		fputs($fh, join(FS, array("Hans Hanson", "0815", "hans@hanson.de"))
       
    15 			. RS);
       
    16 	}
       
    17 	flock($fh, LOCK_SH);
       
    18 	fseek($fh, 0, SEEK_SET);
       
    19 
       
    20 	return $fh;
       
    21 }
       
    22 
       
    23 function get_entries($fh) {
       
    24 	$entries = array();
       
    25 	fseek($fh, 0, SEEK_SET);
       
    26 	while($line = stream_get_line($fh, 0, RS)) {
       
    27 		if ($line === FALSE) break;
       
    28 		$entries[] = explode(FS, $line);
       
    29 	}
       
    30 	return $entries;
       
    31 }
       
    32 
       
    33 function add_entry($fh, $entry) {
       
    34 	$fields = array('name', 'tel', 'mail');
       
    35 
       
    36 	$new = array();
       
    37 	foreach($fields as $key) {
       
    38 		if (!isset($entry[$key])) return;
       
    39 		$new[$key] = $entry[$key];
       
    40 		trim($new[$key]);
       
    41 		preg_replace('/['.RS.FS.' ]+/', ' ', $new[$key]);
       
    42 		if (empty($new[$key])) return;
       
    43 	}
       
    44 
       
    45 	flock($fh, LOCK_EX);
       
    46 	fputs($fh, join(FS, $new) . RS);
       
    47 	flock($fh, LOCK_UN);
       
    48 	header("Location: $_SERVER[PHP_SELF]?action=add");
       
    49 	exit;
       
    50 }
       
    51 
       
    52 function search_entries($fh, $pattern) {
       
    53 	fseek($fh, 0, SEEK_SET);
       
    54 	$entries = array();
       
    55 	while ($line = stream_get_line($fh, 0, RS)) {
       
    56 		if ($line === FALSE) break;
       
    57 		if (!preg_match("/$pattern/i", $line)) continue;
       
    58 		$entry = explode(FS, $line);
       
    59 		$entries[] = array('name' => $entry[0],
       
    60 			           'tel' => $entry[1],
       
    61 				   'mail' => $entry[2]);
       
    62 	}
       
    63 	return $entries;
       
    64 }
       
    65 
       
    66 
       
    67 $abook = open_db(FILE);
       
    68 
       
    69 switch ($_REQUEST['action']) {
       
    70 	case 'add':	add_entry($abook, $_REQUEST);
       
    71 				break;
       
    72 	case 'search':  $entries = search_entries($abook, $_REQUEST['pattern']);
       
    73 				break;
       
    74 }
       
    75 
       
    76 
       
    77 ?>
       
    78 <!-- HTML -->
       
    79 <html>
       
    80 
       
    81 <style type="text/css">
       
    82 	form label { display:block; float:left; width:10ex; }
       
    83 </style>
       
    84 
       
    85 <body>
       
    86 
       
    87 <? if ($_REQUEST['action'] == 'add') { ?>
       
    88 	[ <a href="<?=$_SERVER['PHP_SELF']?>">Home</a> ]
       
    89 <? } else { ?>
       
    90 	[ <a href="<?=$_SERVER['PHP_SELF']?>?action=add">Add Entries</a> ]
       
    91 <? } ?>
       
    92 
       
    93 <h1>Adressbuch</h1>
       
    94 
       
    95 <? if ($_REQUEST['action'] == 'add') { ?>
       
    96 	<p>
       
    97 	<form>
       
    98 	<label for=name>Name</label>
       
    99 	<input  id=name type=text name=name /><br>
       
   100 	<label for=tel>Telefon</label>
       
   101 	<input  id=tel type=text name=tel /><br>
       
   102 	<label for=mail>Mail</label>
       
   103 	<input  id=mail type=text name=mail /><br>
       
   104 	<input type=hidden name=action value=add />
       
   105 	<input type=submit />
       
   106 	</form>
       
   107 
       
   108 <? } else { ?>
       
   109 
       
   110 	<form>
       
   111 	<input type=text name=pattern onChange="submit()" />
       
   112 	<input type=hidden name=action value=search />
       
   113 	<noscript><input type=submit /></noscript>
       
   114 	</form>
       
   115 
       
   116 	<? if ($entries) { ?>
       
   117 		<table>
       
   118 		<tr><th>Name<th>Telefon<th>Mail</tr>
       
   119 		<? foreach ($entries as $entry) { ?>
       
   120 			<tr>
       
   121 				<td><?=$entry['name']?>
       
   122 				<td><?=$entry['tel']?>
       
   123 				<td><?=$entry['mail']?>
       
   124 			</tr>
       
   125 		<? } ?>
       
   126 		</table>
       
   127 	<? } else { ?>
       
   128 		Sorry.
       
   129 	<? } ?>
       
   130 
       
   131 <? } ?>
       
   132 
       
   133 </body>