简体   繁体   中英

PHP-Script deletes lines in .dat file

I'm struggling with the following problem since 9 hours and can't find the solution. I'm using a very old script from here ( http://www.teralaser.net/invitephp/ ) to build an invitation-script. I already did all the styling within the past week and it looks great, now. the script stores the single datasets in a .dat file. Now I realized, that it always keeps just the first 125 lines of the database and deletes the rest. That's a hug he problem, as it is for an invitation with nearly 1000 persons. After some hours I found out (better I guess), that it's the following line, which destroys it:

case "adminadd":
  if (isset($_POST["pwd"])) $pwd = $_POST["pwd"];
  else $pwd="";
  $pwdchk = 1;
  if (isset($_GET["pwd2"])) $pwdchk = strcasecmp(md5( date("z") . $admin_password),$_GET["pwd2"]);

  if (strcasecmp($pwd,$admin_password) != 0 && $pwdchk != 0) {
         echo "<HTML><HEAD><META HTTP-EQUIV='REFRESH' CONTENT='1; URL=$PHP_SELF?do=admin'></HEAD><BODY bgcolor='#ffffff'><CENTER><H2>Falsches Passwort!</H2>Bitte warten...</CENTER></BODY><HTML>";
         exit;
  }

  // Get all administrator changes
  $record = file($data_file);
  rsort($record);
  $jmlrec = count($record);


  // Delete duplicates (the dum way...) and get host email if any
  $hostemail = "";
  for ($i = 0; $i < $jmlrec; $i++) {
    if (isset($record[$i])) {
      $r = $record[$i];
      $row = explode("|~~|",$r);
      $chkmail = $row[2];
      if ($row[4] == "Gastgeber") $hostemail = $row[2];
      for($j = $i+1; $j < $jmlrec; $j++) {
        $r = $record[$j];
        $row = explode("|~~|",$r);        
        if (strcasecmp($row[2],$chkmail)==0 ) { $record[$j] = ""; }
      }
    }
  }

  $updno = 0;
  $err = 0;
  for ($i = 0; $i < $jmlrec; $i++) { // Geändert von $jmlrec+12
    // Changes ?
    if (isset($_POST["vemail".$i])) $chkmail = $_POST["vemail".$i]; else $chkmail ="";
    $found = -1;
    if ($chkmail != "") {
      // Find existing record if possible...
      $recno = count($record);
      for($j = 0; $j < $recno; $j++) {
        $r = $record[$j];
        $row = explode("|~~|",$r);
        if (strcasecmp($row[2],$chkmail) == 0) { $found = $j; break; }
      }
      $changed = 1;
      if ($found >= 0) {
        // Changed ?
        if ( $row[2] == $_POST["vemail".$i] && $row[3] == $_POST["vname".$i] && $row[4] == $_POST["vreply".$i] && $row[5] == $_POST["vamount".$i] &&
             $row[6] == $_POST["vcompany".$i] && $row[7] == $_POST["vcomment".$i] ) $changed = 0;
      }
      if ( $changed != 0 ) {
        $updno++;
        $idx = date("YmdHis");
        $tgl = date("d.m.Y H:i");

        $vemail = str_replace("<","",$_POST["vemail".$i]);
        $vemail = str_replace(">","",$vemail);
        $vemail = str_replace("~~","--",$vemail);
        $vemail = str_replace("\"","&quot;",$vemail);

        $vname = str_replace("<","",$_POST["vname".$i]);
        $vname = str_replace(">","",$vname);
        $vname = str_replace("~","-",$vname);
        $vname = str_replace("\"","&quot;",$vname);

        $vreply = str_replace("<","",$_POST["vreply".$i]);
        $vreply = str_replace(">","",$vreply);
        if ($vreply != "ungesehen" && $vreply != "gesehen" && $vreply != "unentschlossen" && $vreply != "abgesagt" && $vreply != "zugesagt" && $vreply != "Gastgeber") $vreply = "ungesehen";
        if ($vreply != "Gastgeber" && $vreply != "ungesehen" && $found >= 0 ) $tgl = $tgl . "*"; // admin change

        $vamount = str_replace("<","&lt;",$_POST["vamount".$i]);
        $vamount = str_replace(">","&gt;",$vamount);
        $vamount = str_replace("~","-",$vamount);
        $vamount = str_replace("\"","&quot;",$vamount);

        $vcomment = str_replace("<","&lt;",$_POST["vcomment".$i]);
        $vcomment = str_replace(">","&gt;",$vcomment);
        $vcomment = str_replace("~","-",$vcomment);
        $vcomment = str_replace("\"","&quot;",$vcomment);

        $vcompany = str_replace("<","&lt;",$_POST["vcompany".$i]);
        $vcompany = str_replace(">","&gt;",$vcompany);
        $vcompany = str_replace("~","-",$vcompany);
        $vcompany = str_replace("\"","&quot;",$vcompany);

        if (strtoupper($os) == "WIN") {
       $vcomment = str_replace("\r\n","<BR>",$_POST["vcomment".$i]);
       $vcomment = str_replace("\r","",$vcomment);
       $vcomment = str_replace("\n","",$vcomment);
        } else {
       $vcomment = str_replace("\n","<BR>",$vcomment);
       $vcomment = str_replace("\r","",$vcomment);
        }
        $validemail = 1;
        if (!preg_match("/([\w\.\-]+)(\@[\w\.\-]+)(\.[a-z]{2,4})+/i", $vemail)) $validemail = 0;
        if ( $vname != "" && $validemail == 0 ) { echo "Email not valid for $vname : $vemail <br>"; $err++; }
        if ( $vname != "" && $validemail == 1 ) {
          if ($found < 0 ) { $record[] = "\n"; $found = count($record) - 1; }
          $newdata = "<?//|~~|$idx|~~|$vemail|~~|$vname|~~|$vreply|~~|$vamount|~~|$vcompany|~~|$vcomment|~~|$tgl|~~|?>\n";
          $newdata = "!" . stripslashes($newdata);
          $record[$found] = $newdata;
        } else { 
         if ( $vname != "") { $updno--; $changed = 0; }
        }
      }
      if ( $changed == 0 ) {
        if ($found >= 0) $record[$found] = "!".$record[$found];
      }
    }
  }

  // Quick add
  $quickadd = "";
  if (isset($_POST["quickadd"]) && $_POST["quickadd"] != "") $quickadd = stripslashes($_POST["quickadd"]);
  if (trim($quickadd) != "") {
    $qadd = explode("\"",$quickadd);
    $qaddn = count($qadd);
    $nametoadd = "";
    $companytoadd = "";


    for($i = 0; $i < $qaddn; $i++) {
      if ($i % 2 == 0) {
        $q2 = explode(",",$qadd[$i]);
        $q2n = count($q2);
        for($j = 0; $j < $q2n; $j++) {
          $mailtoadd = trim($q2[$j]);
          $mailtoadd = str_replace("<","",$mailtoadd);
          $mailtoadd = str_replace(">","",$mailtoadd);
          $mailtoadd = str_replace("~~","--",$mailtoadd);
          if ($mailtoadd != "") {
            $validemail = 1;
            if (!preg_match("/([\w\.\-]+)(\@[\w\.\-]+)(\.[a-z]{2,4})+/i", $mailtoadd)) $validemail = 0;
            if ($validemail == 0 ) { echo "Fehler bei $mailtoadd (Keine g&uuml;ltige eMail-Addresse)"; $err++; } else {
              if ($nametoadd == "") { $nametoaddarr = explode("@",$mailtoadd); $nametoadd = $nametoaddarr[0]; }
              $idx = date("YmdHis");
              $newdata = "<?//|~~|$idx|~~|$mailtoadd|~~|$nametoadd|~~|ungesehen|~~|1|~~|$companytoadd|~~||~~|?>\n";
              $newdata = "!" . stripslashes($newdata);
              $record[]= $newdata;
              $nametoadd = "";
              $companytoadd = "";
            }
          }
        }
      }
      if ($i % 2 == 1) {
        $data = $qadd[$i];
        list($nametoadd, $companytoadd) = explode(";", $data);

        $nametoadd = str_replace("<","" ,$nametoadd);
        $nametoadd = str_replace(">","" ,$nametoadd);
        $nametoadd = str_replace("~","-",$nametoadd);

        $companytoadd = str_replace("|","" ,$companytoadd);
        $companytoadd = str_replace("<","" ,$companytoadd);
        $companytoadd = str_replace(">","" ,$companytoadd);
        $companytoadd = str_replace("~","-",$companytoadd);
      }

    }
  }


/* Hier liegt der tote Fuchs begraben!!! */

  $jmlrec = count($record);

  $update_data = fopen($data_file,"w");


  if (strtoupper($os) == "UNIX") {
     if (flock($update_data,LOCK_EX)) {
        for ($j=0; $j<$jmlrec; $j++) {
             if (strncasecmp($record[$j],"!",1) != 0 ) $updno++;
             if ($record[$j] != "" && strncasecmp($record[$j],"!",1) == 0 ) fputs($update_data,substr($record[$j],1));
     }
     flock($update_data,LOCK_UN);
    }
   } else {

       for ($j=0; $j<$jmlrec; $j++) {
             if (strncasecmp($record[$j],"!",1) != 0 ) $updno++;
             if ($record[$j] != ""  && strncasecmp($record[$j],"!",1) == 0 ) fputs($update_data,substr($record[$j],1));

     }
   }
   fclose($update_data);

I'm looking forward for some ideas. I'll love to invite you for a coffee... ;) I can also send the whole script, if someone is interested.

Best!

Philipp

EDIT: I'm still totally lost in the code, so I want to share the other parts of the code with you and maybe someone finds the problem. ;) Thanks!

$jml_page = intval($jmlrec/$max_entry_per_page);
                                        $sisa = $jmlrec%$max_entry_per_page;
                                        if ($sisa > 0) $jml_page++;
                                        // Set $nomaybe, $nono, $noyes DEBUG
                                        $nomaybe = 0;
                                        $nono = 0;
                                        $noyes = 0;
                                        $countmaybe = 0;
                                        $countno = 0;
                                        $countyes = 0;
                                        $counttotal = 0;
                                        $no = 0;

                                        if ($jmlrec == 0) echo "<tr><td align='center'>Noch keine Einträge.</td></tr>";

                                          $w = 0; //--Color

                                          for ($i=0; $i<$max_entry_per_page; $i++) {
                                                  // Find the lowest next possible record.
                                                  $no = $jmlrec + 1;
                                          if ($nomaybe < $jmlrec   ) $no = $nomaybe + 1;
                                          if ($nono    < $no - 1   ) $no = $nono + 1;
                                          if ($noyes   < $no - 1   ) $no = $noyes + 1;
                                                  // Check this is valid.
                                                  do {
                                                    while (($no < $jmlrec + 1) && (!isset($record[$no-1]) || $record[$no-1] == "")) { $no++; }
                                              $recno = $no - 1;
                                                    $cont = 0;
                                                    if ($no < $jmlrec + 1) {
                                                      $row = explode("|~~|",$record[$recno]);
                                                      $vr = $row[4];
                                                      $cont = 0;
                                                      if (($vr == "ungesehen" || $vr == "gesehen" || $vr == "unentschlossen") && ($no <= $nomaybe)) $cont = 1;
                                                      if (($vr == "abgesagt" ) && ($no <= $nono)) $cont = 1;
                                                      if (($vr == "zugesagt" || $vr == "Gastgeber") && ($no <= $noyes)) $cont = 1;
                                                      if ($cont == 1) $no++;
                                                    }
                                                  } while ($cont == 1);

                                          if (isset($record[$recno]) && $record[$recno] != "") {
                                           if ($w==0) { 
                                             $warna = $table_content_1a;
                                             $warna2 = $table_content_1b;
                                             $w=1;
                                           } else { 
                                             $warna = $table_content_2a;
                                             $warna2 = $table_content_2b;
                                             $w=0;
                                           }



                                      do {
                                         $nomaybe++;
                                         $recno = $nomaybe-1;
                                         $row = explode("|~~|",$record[$recno]);
                                       } 

                                       while ($nomaybe <= $jmlrec && $row[4] != "ungesehen" && $row[4] != "gesehen" && $row[4] != "unentschlossen" );

                                       if ($row[4] == "ungesehen" || $row[4] == "gesehen" || $row[4]=="unentschlossen") { 
                                         echo "<tr><td>$row[3]</td>";
                                          if (isset($row[2]) && $row[2] != "" ) echo "<td><p>$row[2]</p></td>"; else echo "<td><p></p></td>";
                                         if (isset($row[6]) && $row[6] != "" ) echo "<td><p>$row[6]</p></td>"; else echo "<td><p></p></td>";
                                         $countmaybe++;
                                       }


                                       if ($row[4] == "gesehen") echo "<td><b>gesehen</b></td><td>$row[8]</td>";
                                       if ($row[4] == "unentschlossen") echo "<td><b>unentschlossen</b></td><td>$row[8]</td>";
                                       if ($row[4] == "ungesehen") echo "<td><b>ungesehen</b></td><td></td>";
                                       echo "<td><span class='date'><b> </b></span></td>";
                                       if (isset($row[7]) && ($row[4]=="gesehen" || $row[4]=="unentschlossen" || $row[4]=="ungesehen" ) && $row[7] != "") 
                                        echo "<td><span class='comment'><i>$row[7]</i></span></td></tr>"; else echo "<td><span class='comment'></span></td></tr>";



                                      do {
                                         $nono++;
                                         $recno = $nono-1;
                                         $row = explode("|~~|",$record[$recno]);
                                       } 

                                       while ($nono <= $jmlrec && $row[4] != "abgesagt" && $row != "gesehen" && $row[5] != "unentschlossen" );
                                       $row = explode("|~~|",$record[$recno]);

                                       if ($row[4] == "abgesagt" ) { echo "<tr><td>$row[3]</td><td><p>$row[6]</p></td><td><b>abgesagt</b></td><td>$row[8]</td>";
                                         echo "<td><span class='date'><b> </b></span></td>";
                                         if (isset($row[7]) && $row[7] != "") echo "<td><span class='comment'><i>$row[7]</i></span></td></tr>"; else echo "<td><span class='comment'></span></td></tr>";
                                         $countno++;
                                       }


                                       do {
                                         $noyes++;
                                         $recno = $noyes-1;
                                         $row = explode("|~~|",$record[$recno]);
                                       } while ($noyes <= $jmlrec && $row[4] != "zugesagt" && $row[4] != "Gastgeber" );
                                       if ($row[4] == "zugesagt" || $row[4] == "Gastgeber" ) { 
                                         echo "<tr><td>$row[3]";
                                         if ($row[4] == "Gastgeber") print " (Gastgeber) ";
                                         echo "</td>";
                                        if (isset($row[6]) && $row[6] != "" ) echo "<td><p>$row[6]</p></td>"; else echo "<td><p></p></td>";
                                         echo "<td><b>zugesagt</b></td><td>$row[8]</td>";
                                        if (isset($row[5]) && $row[5] != "") echo "<td><p>$row[5]</p></td>"; else echo "<td><p>1</p></td>";

                                         if (isset($row[7]) && $row[7] != "" ) echo "<td><span class='comment'><i>$row[7]</i></span></td></tr>"; else echo "<td><span class='comment'></span></td></tr>";

                                         $countyes++;
                                         if (isset($row[5]) && $row[5] != "") $counttotal = $counttotal + intval($row[5]);
                                       }


                                         } //--end if  
                                         } //--end for 

It would appear that updated entries are marked by prepending them with ! . This loop appears to only be saving the updates, not every record.

Perhaps there was a typo - fopen with mode "w" will truncate the data file at 0 bytes before writing, if only the updated records are to be written, shouldn't that be mode "a" to append them to the existing file?

It is likely there is also a limit on how large a data file is allowed to grow, make sure you have set it large enough to allow 1000 records.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM