Page 1 of 1

Fixing an old (PHP4?) flatfile guestbook

Posted: Thu Feb 24, 2011 10:25 am
by timodemus
I'm trying to fix a flatfile -based PHP-guestbook called "Tiekku" (It worked fine in an older version of PHP).
"Tiekku" was originally made by user "Tumpi" on a finnish programming forum "Ohjelmointiputka" (http://www.ohjelmointiputka.net/).
"Tiekku" has an admin -page with modify, answer and delete options.

Pages and code comments are in finnish, some or all variables are in english.
Here are some translations from finnish to english (in case they help):

-----
Lue = Read
Kirjoita = Write

Nimi = Name
Kotisivut = Web page url
Viesti = Message

Salasana = Password
Kirjaudu = Log in

Vastaa viestiin = Answer to message
Muokkaa viestiä = Edit message
Poista viesti = Delete message
-----

some variables need to be modified from regular to $_GET or $_POST.
examples:
$var -> $_GET['var'] ,or
$var -> $_POST['var']

also:
$PHP_SELF -> $_SERVER['PHP_SELF']
$REMOTE_ADDR -> $_SERVER['REMOTE_ADDR']


I think the session doesn't work, because when I log in and try to answer to a message, it logs out.

"Tiekku" has 4 files:
- index.php
- settings.php
- styles.php
- messages.txt (stores the messages)

I have tried some fixes, but here are the original files, in case I did something wrong.

index.php:

Code: Select all

<?php
session_start();   

//Sisällytetään asetukset
include ("settings.php");

// Kirjaudutaan sisään
if ($login != "") {  
  if (md5($pword) == $password) { //Oikea salasana  
  
    session_register("pass"); 
    $pass = $password; 
    session_register("ipaddr");
    $ipaddr = $REMOTE_ADDR;        
    
    $alert_title = "Sisäänkirjautuminen"; 
    $alert_text = "Kirjauduit sisään.<br />\n<br />\nVoit nyt poistaa/muokata viestejä sekä vastata niihin."; 
    $op = "alert";   
  } 
  else { //Väärä salasana
    $alert_title = "Sisäänkirjautuminen"; 
    $alert_text = "Salasana väärin.<br />\n<br />\nVoit kirjautua uudelleen <a href=\"". $PHP_SELF ."?op=login\">tästä</a>."; 
    $op = "alert";  
  } 
} 
        
//Kirjaudutaan ulos
if ($logout != "") { 
  if ($pass == $password && $ipaddr == $REMOTE_ADDR) { 
    session_unregister("pass");
    session_unregister("ipaddr");    
    
    $alert_title = "Uloskirjautuminen"; 
    $alert_text = "Kirjauduit ulos.<br />\n<br />\nVoit kirjautua uudelleen <a href=\"". $PHP_SELF ."?op=login\">tästä</a>."; 
    $op = "alert"; 
  }
}
    
//Kirjoitetaan kirjaan
if ($write != "") {    
       
    //Poistetaan kaikki jo tässä vaiheessa

    //Haitalliset merkit poistetaan
    $name = str_replace("|", "", $name);
    $email = str_replace("|", "", $email); 
    $url = str_replace("|", "", $url); 
    $message = str_replace("|", "", $message); 
                        
    //HTML poies
    $name = strip_tags($name);
    $email = strip_tags($email); 
    $url = strip_tags($url); 
    $message = strip_tags($message); 
                                  
    //Vielä vähän kauneusleikkauksia...
    $message = trim($message);
    $message = str_replace("\n", "<br />", $message);    
                                         
  if ($name != "" && str_replace(" ", "", $name) != "" && $message != "") {           
  
    $time = date("d.m.y \k\l\o H:i:s"); 
                         
    //Viestille id
    srand((double)microtime()*1000000);
    $id = md5(uniqid(rand(),999999));
                                                                      
    //Lisättävä rivi
    $new = "$name|$email|$url|$message|$time|$REMOTE_ADDR||$id|\n";    
                 
  
    $fo=fopen("messages.txt", "r+"); //Avataan  
    $old = fread($fo, filesize("messages.txt"));
    fseek($fo, 0);    
    flock($fo, 2); //Lukitaan
    fwrite($fo, "${new}${old}");  //Kirjoitetaan  
    flock($fo, 3); //Vapautetaan   
    fclose($fo); //Suljetaan       
  
    header("location: ". $PHP_SELF .""); 
  } 
  else { // Kaikkia tietoja ei ollut täytetty oikein 
    $alert_title = "Tiedot puutteelliset"; 
    $alert_text = "Et täyttänyt kaikkia tarpeellisia tietoja.<br />\n<br />\nVoit kirjoittaa kirjaan uudelleen <a href=\"". $PHP_SELF ."?op=write\">tästä</a>."; 
    $op = "alert";  
  } 
}

//Vastataan
if ($answer != "") {                                          
  if ($pass == $password && $ipaddr == $REMOTE_ADDR && $wanswer == "true") {           
                                  
    //Siistitään
    $new_answer = str_replace("|", "", $new_answer);
    $new_answer = strip_tags($new_answer); 
    $new_answer = trim($new_answer);  
    $new_answer = str_replace("\n", "<br />", $new_answer);
                                                                                   
    $msg_file = "messages.txt"; 
    $msg_data = file($msg_file);  
    $msg_total = count($msg_data); 
                                   
    for ($i=0; $i<$msg_total; $i++) {    
      $entry = explode("|", $msg_data[$i]);   
         
       if ($entry[7] == $id) { 
         $answer_row = $i;
      }
    } 
    
    $entry = explode("|", $msg_data[$answer_row]);
    $msg_data[$answer_row] = "$entry[0]|$entry[1]|$entry[2]|$entry[3]|$entry[4]|$entry[5]|$new_answer|$entry[7]|\n";    
  
    $fo = fopen($msg_file,"w");
    foreach ($msg_data as $row)
    {
      fputs($fo, $row);
    } 
    fclose($fo);  
    
    header("location: ". $PHP_SELF ."");    
  }   
  else {
    $op = "answer";
  } 
} 

//Muokataan
if ($edit) {                                          
  if ($pass == $password && $ipaddr == $REMOTE_ADDR && $wedit == "true") {           
                                  
    //Siistitään 
    $new_name = str_replace("|", "", $new_name);
    $new_name = strip_tags($new_name); 
    $new_name = trim($new_name); 
    $new_email = str_replace("|", "", $new_email);
    $new_email = strip_tags($new_email); 
    $new_email = trim($new_email);
    $new_url = str_replace("|", "", $new_url);
    $new_url = strip_tags($new_url); 
    $new_url = trim($new_url);
    $new_message = str_replace("|", "", $new_message);
    $new_message = strip_tags($new_message); 
    $new_message = trim($new_message);   
    $new_message = str_replace("\n", "<br />", $new_message);
    $new_time = str_replace("|", "", $new_time);
    $new_time = strip_tags($new_time); 
    $new_time = trim($new_time);
                                                                                   
    $msg_file = "messages.txt"; 
    $msg_data = file($msg_file);  
    $msg_total = count($msg_data); 
                                   
    for ($i=0; $i<$msg_total; $i++) {    
      $entry = explode("|", $msg_data[$i]);   
         
       if ($entry[7] == $id) { 
         $answer_row = $i;
      }
    } 
    
    $entry = explode("|", $msg_data[$answer_row]);
    $msg_data[$answer_row] = "$new_name|$new_email|$new_url|$new_message|$new_time|$entry[5]|$entry[6]|$entry[7]|\n";    
  
    $fo = fopen($msg_file,"w");
    foreach ($msg_data as $row)
    {
      fputs($fo, $row);
    } 
    fclose($fo);  
    
    header("location: ". $PHP_SELF ."");    
  }   
  else {
    $op = "edit";
  } 
}
     
//Poistetaan
if ($remove != "") {                                          
  if ($pass == $password && $ipaddr == $REMOTE_ADDR && $wremove == "true") {           
                                                                                                                    
    $msg_file = "messages.txt"; 
    $msg_data = file($msg_file);  
    $msg_total = count($msg_data); 
                                   
    for ($i=0; $i<$msg_total; $i++) {    
      $entry = explode("|", $msg_data[$i]);   
         
       if ($entry[7] == $id) { 
         $answer_row = $i;
      }
    } 
    
    $msg_data[$answer_row] = "";    
  
    $fo = fopen($msg_file,"w");
    foreach ($msg_data as $row)
    {
      fputs($fo, $row);
    } 
    fclose($fo);  
    
    header("location: ". $PHP_SELF ."");    
  }   
  else {
    $op = "remove";
  } 
} 
   
// Tallennetaan asetukset 
if ($save_settings != "") { 
  if ($pass == $password && $ipaddr == $REMOTE_ADDR) {   
    
    if ($edit_password != "") { $edit_password = md5($edit_password); }
    else { $edit_password = $password; }
  
    $new_settings = "<?php\n\$table_width = \"$edit_table_width\";\n\n\$table_border_size = \"$edit_table_border_size\";\n\$table_border_style = \"$edit_table_border_style\";\n\$table_border_color = \"$edit_table_border_color\";\n\n\$table_cellspacing = \"$edit_table_cellspacing\";\n\$table_cellpadding = \"$edit_table_cellpadding\";\n\n\$background_body = \"$edit_background_body\";\n\$background_title = \"$edit_background_title\";\n\$background_text = \"$edit_background_text\";\n\n\$font_face = \"$edit_font_face\";\n\n\$font_size_title = \"$edit_font_size_title\";\n\$font_size_buttons = \"$edit_font_size_buttons\";\n\$font_size_text = \"$edit_font_size_text\";\n\n\$font_color_title = \"$edit_font_color_title\";\n\$font_color_text = \"$edit_font_color_text\";\n\n\$font_color_link = \"$edit_font_color_link\";\n\$font_color_alink = \"$edit_font_color_alink\";\n\$font_color_vlink = \"$edit_font_color_vlink\";\n\$font_color_hover = \"$edit_font_color_hover\";\n\n\$text_decoration_link = \"$edit_text_decoration_link\";\n\$text_decoration_alink = \"$edit_text_decoration_alink\";\n\$text_decoration_vlink = \"$edit_text_decoration_vlink\";\n\$text_decoration_hover = \"$edit_text_decoration_hover\";\n\n\$messages_per_page = \"$edit_messages_per_page\";\n\n\$password = \"$edit_password\";\n?>"; 

    $fo = fopen("settings.php", "w"); //Tiedosto missä asetukset sijaitsee
    flock($fo, 2); //Lukitaan tiedosto
    fwrite($fo, $new_settings); //Heitetään filuun uudet asetukset  
    flock($fo, 3); //Vapautetaan tiedosto lukituksesta
    fclose($fo); //Suljetaan filu   
                           
    //Vaihdetaan salasana jotta admini pysyy kirjautuneena
    session_unregister("pass");
    session_register("pass");
    $pass = $edit_password; 
    
    $alert_title = "Asetukset muutettu"; 
    $alert_text = "Asetukset muutettiin.<br />\n<br />\nJos et ole tyytyväinen uusiin asetuksiin, voit vaihtaa niitä <a href=\"". $PHP_SELF ."?op=edit_settings\">tästä</a>."; 
    $op = "alert";
  } 
  else {  
    $alert_title = "Et ole kirjautunut sisään"; 
    $alert_text = "Et ole kirjautunut sisään.<br />\n<br />\nVoit kirjautua sisään <a href=\"". $PHP_SELF ."?op=login\">tästä</a>."; 
    $op = "alert"; 
  } 
} 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  

<html>

<head>
  <title>Tiekku</title> 
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  
  <?php include("styles.php"); ?> 
</head>

<body>
<center>    

<table width="<?php echo $table_width; ?>" cellspacing="<?php echo $table_cellspacing; ?>" cellpadding="<?php echo $table_cellpadding; ?>"> 
  <tr> 
    <td class="title"> 
      <center>
        <a href="<?php echo $PHP_SELF; ?>">Lue</a> |
        <a href="<?php echo $PHP_SELF; ?>?op=write">Kirjoita</a> | 
<?php if ($pass == $password && $ipaddr == $REMOTE_ADDR && $logout != "true") { ?>
        <a href="<?php echo $PHP_SELF; ?>?op=edit_settings">Asetukset</a> | 
        <a href="<?php echo $PHP_SELF; ?>?logout=true">Kirjaudu ulos</a>
<?php } else { ?>
        <a href="<?php echo $PHP_SELF; ?>?op=login">Admin</a>
<?php } ?>
      </center>
    </td> 
  </tr> 
</table>  

<p>
          
<?php 
if ($op == "alert") {
?> 
<table width="<?php echo $table_width; ?>" cellspacing="<?php echo $table_cellspacing; ?>" cellpadding="<?php echo $table_cellpadding; ?>"> 
  <tr> 
    <td class="title"><center><b><?php echo $alert_title; ?></b></center></td> 
  </tr> 
</table>     

<table width="<?php echo $table_width; ?>" cellspacing="<?php echo $table_cellspacing; ?>" cellpadding="<?php echo $table_cellpadding; ?>"> 
  <tr> 
    <td class="text1"><?php echo $alert_text; ?></td> 
    <td class="text2">&nbsp;</td> 
  </tr> 
</table> 
<?php
}      

else if ($op == "edit_settings") { 
  if ($pass == $password && $ipaddr == $REMOTE_ADDR) {
?>                                                   
<form action="<?php echo $PHP_SELF; ?>" method="post">  
                                                         
<input type="hidden" name="save_settings" value="true">

  <table width="<?php echo $table_width; ?>" cellspacing="<?php echo $table_cellspacing; ?>" cellpadding="<?php echo $table_cellpadding; ?>"> 
    <tr> 
      <td class="title"><center><b>Muuta asetuksia</b></center></td> 
    </tr> 
  </table>    
  
  <table width="<?php echo $table_width; ?>" cellspacing="<?php echo $table_cellspacing; ?>" cellpadding="<?php echo $table_cellpadding; ?>"> 
    <tr> 
      <td class="text1"><b>Taulukon leveys<b></td> 
      <td class="text2" width="50%"><input type="text" size="35" name="edit_table_width" value="<?php echo $table_width; ?>"></td> 
    </tr> 
    <tr> 
      <td class="text1"><b>Reunuksen paksuus</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="edit_table_border_size" value="<?php echo $table_border_size; ?>"></td> 
    </tr>  
    <tr> 
      <td class="text1"><b>Reunuksen tyyli</b>&nbsp;</td> 
      <td class="text2">
        <select name="edit_table_border_style">
          <option value="solid"<?php if ($table_border_style == "solid") { echo " selected"; } ?>>Solid</option> 
          <option value="dashed"<?php if ($table_border_style == "dashed") { echo " selected"; } ?>>Dashed</option> 
          <option value="dotted"<?php if ($table_border_style == "dotted") { echo " selected"; } ?>>Dotted</option>          
        </select>
      </td> 
    </tr>
    <tr> 
      <td class="text1"><b>Reunuksen väri</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="edit_table_border_color" value="<?php echo $table_border_color; ?>"></td> 
    </tr>
    <tr> 
      <td class="text1"><b>Cellspacing (ei kannata vaihtaa)</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="edit_table_cellspacing" value="<?php echo $table_cellspacing; ?>"></td> 
    </tr>
    <tr> 
      <td class="text1"><b>Cellpadding</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="edit_table_cellpadding" value="<?php echo $table_cellpadding; ?>"></td> 
    </tr>
    <tr> 
      <td class="text1"><b>Dokumentin taustaväri</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="edit_background_body" value="<?php echo $background_body; ?>"></td> 
    </tr>
    <tr> 
      <td class="text1"><b>Otsikon taustaväri</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="edit_background_title" value="<?php echo $background_title; ?>"></td> 
    </tr>
    <tr> 
      <td class="text1"><b>Tekstin taustaväri</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="edit_background_text" value="<?php echo $background_text; ?>"></td> 
    </tr>
    <tr> 
      <td class="text1"><b>Fontti</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="edit_font_face" value="<?php echo $font_face; ?>"></td> 
    </tr>
    <tr> 
      <td class="text1"><b>Otsikon koko</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="edit_font_size_title" value="<?php echo $font_size_title; ?>"></td> 
    </tr>
    <tr> 
      <td class="text1"><b>Tekstin koko</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="edit_font_size_text" value="<?php echo $font_size_text; ?>"></td> 
    </tr>     
    <tr> 
      <td class="text1"><b>Nappuloiden, tekstikenttien ym. tekstin koko</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="edit_font_size_buttons" value="<?php echo $font_size_buttons; ?>"></td> 
    </tr>
    <tr> 
      <td class="text1"><b>Fontin väri, otsikko</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="edit_font_color_title" value="<?php echo $font_color_title; ?>"></td> 
    </tr>
    <tr> 
      <td class="text1"><b>Fontin väri, teksti</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="edit_font_color_text" value="<?php echo $font_color_text; ?>"></td> 
    </tr>  
    <tr> 
      <td class="text1"><b>Fontin väri, linkki (link)</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="edit_font_color_link" value="<?php echo $font_color_link; ?>"></td> 
    </tr>
    <tr> 
      <td class="text1"><b>Fontin väri, linkki (alink)</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="edit_font_color_alink" value="<?php echo $font_color_alink; ?>"></td> 
    </tr>
    <tr> 
      <td class="text1"><b>Fontin väri, linkki (vlink)</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="edit_font_color_vlink" value="<?php echo $font_color_vlink; ?>"></td> 
    </tr>
    <tr> 
      <td class="text1"><b>Fontin väri, linkki (hover)</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="edit_font_color_hover" value="<?php echo $font_color_hover; ?>"></td> 
    </tr>
    <tr> 
      <td class="text1"><b>Linkin tyyli (link)</b>&nbsp;</td> 
      <td class="text2">
        <select name="edit_text_decoration_link">
          <option value="none"<?php if ($text_decoration_link == "none") { echo " selected"; } ?>>Ei mitään</option>   
          <option value="underline"<?php if ($text_decoration_link == "underline") { echo " selected"; } ?>>Alleviivattu</option>  
          <option value="overline"<?php if ($text_decoration_link == "overline") { echo " selected"; } ?>>Ylleviivattu</option>  
          <option value="line-through"<?php if ($text_decoration_link == "line-through") { echo " selected"; } ?>>Yliviivattu</option>  
          <option value="overline underline"<?php if ($text_decoration_link == "overline underline") { echo " selected"; } ?>>Alle- ja ylleviivattu</option>  
          <option value="overline underline line-through"<?php if ($text_decoration_link == "overline underline line-through") { echo " selected"; } ?>>Alle-, ylle- ja yliviivattu</option>         
        </select>
      </td> 
    </tr>  
    <tr> 
      <td class="text1"><b>Linkin tyyli (alink)</b>&nbsp;</td> 
      <td class="text2">
        <select name="edit_text_decoration_alink">
          <option value="none"<?php if ($text_decoration_link == "none") { echo " selected"; } ?>>Ei mitään</option>   
          <option value="underline"<?php if ($text_decoration_alink == "underline") { echo " selected"; } ?>>Alleviivattu</option>  
          <option value="overline"<?php if ($text_decoration_alink == "overline") { echo " selected"; } ?>>Ylleviivattu</option>  
          <option value="line-through"<?php if ($text_decoration_alink == "line-through") { echo " selected"; } ?>>Yliviivattu</option>  
          <option value="overline underline"<?php if ($text_decoration_alink == "overline underline") { echo " selected"; } ?>>Alle- ja ylleviivattu</option>  
          <option value="overline underline line-through"<?php if ($text_decoration_alink == "overline underline line-through") { echo " selected"; } ?>>Alle-, ylle- ja yliviivattu</option>         
        </select>
      </td> 
    </tr>
    <tr> 
      <td class="text1"><b>Linkin tyyli (vlink)</b>&nbsp;</td> 
      <td class="text2">
        <select name="edit_text_decoration_vlink">
          <option value="none"<?php if ($text_decoration_vlink == "none") { echo " selected"; } ?>>Ei mitään</option>   
          <option value="underline"<?php if ($text_decoration_vlink == "underline") { echo " selected"; } ?>>Alleviivattu</option>  
          <option value="overline"<?php if ($text_decoration_vlink == "overline") { echo " selected"; } ?>>Ylleviivattu</option>  
          <option value="line-through"<?php if ($text_decoration_vlink == "line-through") { echo " selected"; } ?>>Yliviivattu</option>  
          <option value="overline underline"<?php if ($text_decoration_vlink == "overline underline") { echo " selected"; } ?>>Alle- ja ylleviivattu</option>  
          <option value="overline underline line-through"<?php if ($text_decoration_vlink == "overline underline line-through") { echo " selected"; } ?>>Alle-, ylle- ja yliviivattu</option>         
        </select>
      </td> 
    </tr>
    <tr> 
      <td class="text1"><b>Linkin tyyli (hover)</b>&nbsp;</td> 
      <td class="text2">
        <select name="edit_text_decoration_hover">
          <option value="none"<?php if ($text_decoration_hover == "none") { echo " selected"; } ?>>Ei mitään</option>   
          <option value="underline"<?php if ($text_decoration_hover == "underline") { echo " selected"; } ?>>Alleviivattu</option>  
          <option value="overline"<?php if ($text_decoration_hover == "overline") { echo " selected"; } ?>>Ylleviivattu</option>  
          <option value="line-through"<?php if ($text_decoration_hover == "line-through") { echo " selected"; } ?>>Yliviivattu</option>  
          <option value="overline underline"<?php if ($text_decoration_hover == "overline underline") { echo " selected"; } ?>>Alle- ja ylleviivattu</option>  
          <option value="overline underline line-through"<?php if ($text_decoration_hover == "overline underline line-through") { echo " selected"; } ?>>Alle-, ylle- ja yliviivattu</option>         
        </select>
      </td> 
    </tr>
    <tr> 
      <td class="text1"><b>Adminin salasana</b>&nbsp;</td> 
      <td class="text2"><input type="password" name="edit_password" value=""><br />(jätä tyhjäksi jos et halua muuttaa)</td> 
    </tr>
    <tr> 
      <td class="text1"><b>Viestejä/sivu</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="edit_messages_per_page" value="<?php echo $messages_per_page; ?>" size="2"></td> 
    </tr>
    <tr> 
      <td class="text1">&nbsp;</td> 
      <td class="text2"><input type="submit" value="Tallenna">&nbsp;<input type="reset" value="Palauta"></td> 
    </tr> 
  </table>   
  
</form> 
<?php
  }
}  
else if ($op == "login") {
?> 
<form action="<?php echo $PHP_SELF; ?>" method="post"> 

<input type="hidden" name="login" value="true">

  <table width="<?php echo $table_width; ?>" cellspacing="<?php echo $table_cellspacing; ?>" cellpadding="<?php echo $table_cellpadding; ?>"> 
    <tr> 
      <td class="title"><center><b>Kirjaudu sisään</b></center></td>  
    </tr> 
  </table> 
  <table width="<?php echo $table_width; ?>" cellspacing="<?php echo $table_cellspacing; ?>" cellpadding="<?php echo $table_cellpadding; ?>"> 
    <tr> 
      <td class="text1"><b>Salasana</b>&nbsp;</td> 
      <td class="text2"><input type="password" name="pword"></td> 
    </tr> 
    <tr> 
      <td class="text1">&nbsp;</td> 
      <td class="text2"><input type="submit" value="Kirjaudu"></td> 
    </tr> 
  </table> 
</form> 
<?php
}   
else if ($op == "write") {
?> 
<form action="<?php echo $PHP_SELF; ?>" method="post">
 
<input type="hidden" name="write" value="true">

  <table width="<?php echo $table_width; ?>" cellspacing="<?php echo $table_cellspacing; ?>" cellpadding="<?php echo $table_cellpadding; ?>"> 
    <tr> 
      <td class="title"><center><b>Kirjoita vieraskirjaan</b></center></td>  
    </tr> 
  </table> 
  <table width="<?php echo $table_width; ?>" cellspacing="<?php echo $table_cellspacing; ?>" cellpadding="<?php echo $table_cellpadding; ?>"> 
    <tr> 
      <td class="text1"><b>Nimi</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="name"></td> 
    </tr>  
    <tr> 
      <td class="text1"><b>E-Mail</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="email"></td> 
    </tr>                                                  
    <tr> 
      <td class="text1"><b>Kotisivut</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="url" value="http://"></td> 
    </tr> 
    <tr> 
      <td class="text1"><b>Viesti</b>&nbsp;</td> 
      <td class="text2"><textarea name="message" cols="30" rows="6"></textarea></td> 
    </tr>
    <tr> 
      <td class="text1">&nbsp;</td> 
      <td class="text2"><input type="submit" value="Kirjoita"></td> 
    </tr> 
  </table> 
</form> 
<?php
} 
else if ($op == "answer") {       
  $name = "";
  $message = ""; 
  
  $msg_file = "messages.txt"; 
  $msg_data = file($msg_file);  
  $msg_total = count($msg_data); 
                                   
  
  //Katsotaan löytyykö viestiä johon vastataan
  for ($i=0; $i<$msg_total; $i++) {    
    $entry = explode("|", $msg_data[$i]);   
         
    if ($entry[7] == $id) {
      $name = $entry[0];
      $message = $entry[3];   
      $old_answer = str_replace("<br />", "\n", $entry[6]); 
    }
  }                                        
  
  if ($name != "" && $message != "") {   
  ?>   
<form action="<?php echo $PHP_SELF; ?>" method="post">

<input type="hidden" name="answer" value="true">                                                
<input type="hidden" name="id" value="<?php echo $id; ?>">    
<input type="hidden" name="wanswer" value="true">
<input type="hidden" name="page" value="<?php echo $page; ?>">

  <table width="<?php echo $table_width; ?>" cellspacing="<?php echo $table_cellspacing; ?>" cellpadding="<?php echo $table_cellpadding; ?>"> 
    <tr> 
      <td class="title"><center><b>Vastaa viestiin</b></center></td>  
    </tr> 
  </table> 
  <table width="<?php echo $table_width; ?>" cellspacing="<?php echo $table_cellspacing; ?>" cellpadding="<?php echo $table_cellpadding; ?>">  
    <tr> 
      <td class="text1"><b>Kirjoittaja</b>&nbsp;</td> 
      <td class="text2"><?php echo $name; ?></td> 
    </tr>                                                  
    <tr> 
      <td class="text1"><b>Viesti</b>&nbsp;</td> 
      <td class="text2"><?php echo $message; ?></td> 
    </tr> 
    <tr> 
      <td class="text1">&nbsp;</td> 
      <td class="text2">&nbsp;</td> 
    </tr> 
    <tr> 
      <td class="text1"><b>Vastaus</b></td> 
      <td class="text2"><textarea name="new_answer" cols="30" rows="6"><?php echo $old_answer; ?></textarea></td> 
    </tr> 
    <tr> 
      <td class="text1">&nbsp;</td> 
      <td class="text2"><input type="submit" value="Vastaa"></td> 
    </tr> 
  </table> 
</form> 
  <?php
  }
} 
else if ($op == "edit") {       
  $name = "";
  $message = ""; 
  
  $msg_file = "messages.txt"; 
  $msg_data = file($msg_file);  
  $msg_total = count($msg_data); 
                                   
  
  //Löytyykö muokattava viesti
  for ($i=0; $i<$msg_total; $i++) {    
    $entry = explode("|", $msg_data[$i]);   
         
    if ($entry[7] == $id) {
      $name = $entry[0];
      $email = $entry[1];     
      $url = $entry[2];
      $message = str_replace("<br />", "\n", $entry[3]);
      $time = $entry[4];
    }
  }                                        
  
  if ($name != "" && $message != "") {   
  ?>   
<form action="<?php echo $PHP_SELF; ?>" method="post">
   
<input type="hidden" name="edit" value="true">                                             
<input type="hidden" name="id" value="<?php echo $id; ?>">  
<input type="hidden" name="wedit" value="true"> 
<input type="hidden" name="page" value="<?php echo $page; ?>">

  <table width="<?php echo $table_width; ?>" cellspacing="<?php echo $table_cellspacing; ?>" cellpadding="<?php echo $table_cellpadding; ?>"> 
    <tr> 
      <td class="title"><center><b>Muokkaa viestiä</b></center></td>  
    </tr> 
  </table> 
  <table width="<?php echo $table_width; ?>" cellspacing="<?php echo $table_cellspacing; ?>" cellpadding="<?php echo $table_cellpadding; ?>">  
    <tr> 
      <td class="text1"><b>Nimi</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="new_name" value="<?php echo $name; ?>"></td> 
    </tr>   
    <tr> 
      <td class="text1"><b>E-Mail</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="new_email" value="<?php echo $email; ?>"></td> 
    </tr> 
    <tr> 
      <td class="text1"><b>Kotisivut</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="new_url" value="<?php echo $url; ?>"></td> 
    </tr>                                                
    <tr> 
      <td class="text1"><b>Viesti</b>&nbsp;</td> 
      <td class="text2"><textarea name="new_message" cols="30" rows="6"><?php echo $message; ?></textarea></td> 
    </tr>
    <tr> 
      <td class="text1"><b>Kello</b>&nbsp;</td> 
      <td class="text2"><input type="text" size="35" name="new_time" value="<?php echo $time; ?>"></td> 
    </tr> 
    <tr> 
      <td class="text1">&nbsp;</td> 
      <td class="text2">&nbsp;</td> 
    </tr>  
    <tr> 
      <td class="text1">&nbsp;</td> 
      <td class="text2"><input type="submit" value="Muokkaa"></td> 
    </tr> 
  </table> 
</form> 
  <?php
  }
} 
else if ($op == "remove") {       
  $found = "false"; 
  
  $msg_file = "messages.txt"; 
  $msg_data = file($msg_file);  
  $msg_total = count($msg_data); 
                                   
  
  //Löytyykö poistettava viesti
  for ($i=0; $i<$msg_total; $i++) {    
    $entry = explode("|", $msg_data[$i]);   
         
    if ($entry[7] == $id) {
      $found = "true";
    }
  }                                        
  
  if ($found == "true") {   
  ?>   
<form action="<?php echo $PHP_SELF; ?>" method="post" name="removeform">

<input type="hidden" name="remove" value="true">                                                
<input type="hidden" name="id" value="<?php echo $id; ?>">  
<input type="hidden" name="wremove" value="true"> 
<input type="hidden" name="page" value="<?php echo $page; ?>">

  <table width="<?php echo $table_width; ?>" cellspacing="<?php echo $table_cellspacing; ?>" cellpadding="<?php echo $table_cellpadding; ?>"> 
    <tr> 
      <td class="title"><center><b>Poista viesti</b></center></td>  
    </tr> 
  </table> 
  <table width="<?php echo $table_width; ?>" cellspacing="<?php echo $table_cellspacing; ?>" cellpadding="<?php echo $table_cellpadding; ?>">  
    <tr> 
      <td class="text1"><b>Poistetaanko viesti?</b>&nbsp;</td> 
      <td class="text2">&nbsp;</td> 
    </tr>   
    <tr> 
      <td class="text1">&nbsp;</td> 
      <td class="text2"><a href="<?php echo $PHP_SELF; ?>?remove=true&wremove=true&id=<?php echo $id; ?>&page=<?php echo $page; ?>" onClick="removeform.submit();">Kyllä</a><br /><a href="<?php echo $PHP_SELF; ?>?page=<?php echo $page; ?>">Ei</a></td> 
    </tr>                                                
  </table> 
</form> 
  <?php
  }
}
else { 
  $msg_file = "messages.txt"; 
  $msg_data = file($msg_file);  
  $msg_total = count($msg_data);      
  
  if (!$page || $page == 0) { $page = 0; }
  if ($page >= $msg_total) { $page = $msg_total - 1; }
                                         
  if ($messages_per_page == 0) {
    $messages_per_page = $msg_total;
    $page = 0;
  }      
  
  if ($messages_per_page > $msg_total) {
    $messages_per_page = $msg_total;
    $page = 0;
  }
  
  $msg_start = $page * $messages_per_page;
  $msg_end = $page * $messages_per_page + $messages_per_page - 1;
  
  if ($msg_start < 0) { $msg_start = 0; }
  if ($msg_end >= $msg_total) { $msg_end = $msg_total - 1; }   
  
  $previouspage = $page - 1;    
  $nextpage = $page + 1;
?>
<table width="<?php echo $table_width; ?>" cellspacing="<?php echo $table_cellspacing; ?>" cellpadding="<?php echo $table_cellpadding; ?>"> 
  <tr> 
    <td class="title"><center><b>Lue vieraskirjaa</b></center></td> 
  </tr> 
</table>     

<table width="<?php echo $table_width; ?>" cellspacing="<?php echo $table_cellspacing; ?>" cellpadding="<?php echo $table_cellpadding; ?>"> 
  <tr> 
    <td class="text1" align=center><?php  
//Valikkosysteemit          
if ($msg_total != 0) {                  

  echo "Viestejä yhteensä ". $msg_total;
  if ($messages_per_page != $msg_total) { 
    if ($msg_start == $msg_end) { echo ", näytetään viesti ". intval($msg_start + 1); } 
    else { echo ", näytetään viestit ". intval($msg_start  + 1) ."-". intval($msg_end + 1); }  
  }
  
  echo "<br />"; 
  
  if ($messages_per_page == $msg_total) {
    echo "Näytetään kaikki viestit";
  }
  else {   
    if ($msg_start == 0) { 
      if ($messages_per_page == 1) { echo "Edellinen viesti"; }                       
      else { echo "Edelliset ". $messages_per_page ." viestiä"; }
    } 
    else {  
      if ($messages_per_page == 1) { echo "<a href=\"". $PHP_SELF ."?page=". $previouspage ."\">Edellinen viesti</a>"; }                       
      else { echo "<a href=\"". $PHP_SELF ."?page=". $previouspage ."\">Edelliset ". $messages_per_page ." viestiä</a>"; }
    } 
        
    echo " | ";
  
    if ($msg_end == $msg_total - 1) { 
      if ($messages_per_page == 1) { echo "Seuraava viesti"; }                       
      else { echo "Seuraavat ". $messages_per_page ." viestiä"; }
    } 
    else {  
      if ($messages_per_page == 1) { echo "<a href=\"". $PHP_SELF ."?page=". $nextpage ."\">Seuraava viesti</a>"; }                       
      else { echo "<a href=\"". $PHP_SELF ."?page=". $nextpage ."\">Seuraavat ". $messages_per_page ." viestiä</a>";}
    }
  } 
}  
else {     
  echo "Vieraskirjassa ei ole yhtään viestiä";
}
    ?></td> 
    <td class="text2">&nbsp;</td> 
  </tr>  
</table>
<?php  
  
for ($i=$msg_start; $i<=$msg_end; $i++) {
  $entry = explode("|", $msg_data[$i]);
?>
<br />
<table width="<?php echo $table_width; ?>" cellspacing="<?php echo $table_cellspacing; ?>" cellpadding="<?php echo $table_cellpadding; ?>"> 
  <tr> 
    <td class="title"><center><b><?php echo $entry[0]; ?></b></center></td>  
  </tr> 
</table> 
<table width="<?php echo $table_width; ?>" cellspacing="<?php echo $table_cellspacing; ?>" cellpadding="<?php echo $table_cellpadding; ?>">    
  <tr> 
    <td class="text1" width="75"><b>Nimi</b>&nbsp;</td> 
    <td class="text2"><?php echo $entry[0]; ?></td> 
  </tr>  
<?php
  if ($entry[1] != "") {
?>    
  <tr> 
    <td class="text1"><b>E-Mail</b>&nbsp;</td> 
    <td class="text2"><a href="mailto:<?php echo $entry[1]; ?>"><?php echo $entry[1]; ?></a></td> 
  </tr> 
<?php
  } 
?>  
<?php
  if ($entry[2] != "" && $entry[2] != "http://") {
?>    
  <tr> 
    <td class="text1"><b>Kotisivut</b>&nbsp;</td> 
    <td class="text2"><a href="<?php echo $entry[2]; ?>"><?php echo $entry[2]; ?></a></td> 
  </tr> 
<?php
  } 
?>   
  <tr> 
    <td class="text1"><b>Viesti</b>&nbsp;</td> 
    <td class="text2"><?php echo $entry[3]; ?></td> 
  </tr>  
<?php
  if ($entry[6] != "") {
?>    
  <tr> 
    <td class="text1"><b>Vastaus</b>&nbsp;</td> 
    <td class="text2"><?php echo $entry[6]; ?></td> 
  </tr> 
<?php
  } 
?>    
  <tr> 
    <td class="text1">&nbsp;</td> 
    <td class="text2">Viesti jätetty <?php echo $entry[4]; ?><?php if ($pass == $password && $ipaddr == $REMOTE_ADDR) { echo "<br />\nosoitteesta ". $entry[5]; } ?></td>                                                  
  </tr>   
<?php
  if ($pass == $password && $ipaddr == $REMOTE_ADDR) { 
?>   
    <tr> 
    <td class="text1">&nbsp;</td> 
    <td class="text2"><a href="<?php echo $PHP_SELF; ?>?answer=true&wanswer=false&id=<?php echo $entry[7]; ?>&page=<?php echo $page; ?>">Vastaa viestiin</a> | <a href="<?php echo $PHP_SELF; ?>?edit=true&wedit=false&id=<?php echo $entry[7]; ?>&page=<?php echo $page; ?>">Muokkaa viestiä</a> | <a href="<?php echo $PHP_SELF; ?>?remove=true&wremove=false&id=<?php echo $entry[7]; ?>&page=<?php echo $page; ?>">Poista viesti</a></td> 
  </tr> 
<?php
}
?> 
</table>
  <?php
  }
}   
?>               

</center>
</body>   

</html>


settings.php:

Code: Select all

<?php
$table_width = "500";

$table_border_size = "1px";
$table_border_style = "solid";
$table_border_color = "#000000";

$table_cellspacing = "0";
$table_cellpadding = "1";

$background_body = "#DDDD77";
$background_title = "#EEEE88";
$background_text = "#FFFF99";

$font_face = "Verdana, Arial, Helvetica, Courier";

$font_size_title = "12px";
$font_size_buttons = "9px";
$font_size_text = "11px";

$font_color_title = "#000000";
$font_color_text = "#000000";

$font_color_link = "#000000";
$font_color_alink = "#000000";
$font_color_vlink = "#000000";
$font_color_hover = "#000000";

$text_decoration_link = "none";
$text_decoration_alink = "none";
$text_decoration_vlink = "none";
$text_decoration_hover = "line-through";

$messages_per_page = "5";

$password = "1bdfeec4704575fe187c2397fecd3d93";

?>


styles.php:

Code: Select all

<?php include ("settings.php"); // Asetuksii ?> 
<style type="text/css"> 
  body { 
    background-color:<?php echo $background_body; ?>;
  }    
  table { 
    font-family:<?php echo $font_face; ?>; 
    font-size:<?php echo $font_size_text; ?>; 
    color:<?php echo $font_color_text; ?>; 
  } 
  textarea { 
    font-family:<?php echo $font_face; ?>; 
    font-size:<?php echo $font_size_buttons; ?>;
    color:<?php echo $font_color_text; ?>;
    background:<?php echo $background_text; ?>; 
  } 
  input { 
    font-family:<?php echo $font_face; ?>; 
    font-size:<?php echo $font_size_buttons; ?>;
    color:<?php echo $font_color_text; ?>;
    background:<?php echo $background_text; ?>; 
  } 
  select { 
    font-family:<?php echo $font_face; ?>; 
    font-size:<?php echo $font_size_buttons; ?>;
    color:<?php echo $font_color_text; ?>;
    background:<?php echo $background_text; ?>; 
  }   
     
  .text1 {                
    background-color:<?php echo $background_text; ?>; 
    border-left:<?php echo $table_border_style ." ". $table_border_size ." ". $table_border_color; ?>; 
    border-bottom:<?php echo $table_border_style ." ". $table_border_size ." ". $table_border_color; ?>; 
  }  
  .text2 {                
    background-color:<?php echo $background_text; ?>; 
    border-right:<?php echo $table_border_style ." ". $table_border_size ." ". $table_border_color; ?>; 
    border-bottom:<?php echo $table_border_style ." ". $table_border_size ." ". $table_border_color; ?>; 
  } 
  .title {          
    color:<?php echo $font_color_title; ?>; 
    background-color:<?php echo $background_title; ?>; 
    border:<?php echo $table_border_style ." ". $table_border_size ." ". $table_border_color; ?>; 
    font-size:<?php echo $font_size_title; ?>; 
  } 
  
  a:link { 
    color:<?php echo $font_color_link; ?>; 
    text-decoration:<?php echo $text_decoration_link; ?>; 
    font-size:<?php echo $font_size_text; ?>;  
  } 
  a:visited { 
    color:<?php echo $font_color_vlink; ?>; 
    text-decoration:<?php echo $text_decoration_vlink; ?>; 
    font-size:<?php echo $font_size_text; ?>;   
  } 
  a:active { 
    color:<?php echo $font_color_alink; ?>; 
    text-decoration:<?php echo $text_decoration_alink; ?>; 
    font-size:<?php echo $font_size_text; ?>;   
  } 
  a:hover { 
    color:<?php echo $font_color_hover; ?>; 
    text-decoration:<?php echo $text_decoration_hover; ?>; 
    font-size:<?php echo $font_size_text; ?>;       
  } 
</style>




Feel free to ask for more translations or anything else about the code!


- Timodemus -

EDIT: I don't want to change the script to SQL!

Re: [Challenge] Fixing an old (PHP4?) flatfile guestbook

Posted: Thu Feb 24, 2011 10:28 am
by AbraCadaver
More like "work for free" than a challenge.