Deleting posts from file, file format bug, help.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • helraizer1
    New Member
    • Mar 2007
    • 118

    Deleting posts from file, file format bug, help.

    Hi folks,

    I have a file for my chatbox called data.line, which the posts are in the layout

    Code:
    CHATBOXTEXT
    7
    username=helraizer
    1202416953
    ip=86.140.73.183
    color=yellow
    font=palab
    message=bit of a bug, I admit
    ###
    username=Helraizer
    1202420235
    ip=86.140.73.183
    color=yellow
    font=palab
    message=Teeeeeeesting
    ###
    username=Fjar
    1202420306
    ip=81.77.39.76
    color=yellow
    font=palab
    message=I love you, my Sam ^_^
    ###
    username=helraizer
    1202420637
    ip=81.77.39.76
    color=aqua
    font=palab
    message=I love you too, my Fjar!! <3
    ###
    This is fine, when you post a comment it posts your data like this from a form using this code
    index.php (only relavent code shown)

    [code=php]

    <?php
    $text = htmlspecialchar s(stripcslashes ($_POST['input'] . "\n"));
    $username = htmlspecialchar s(stripslashes( $_POST['username']));
    $color = $_POST['color'];
    $font = $_POST['font'];
    $ip = $_SERVER['REMOTE_ADDR'] . "\n";
    $ip1 = mysql_real_esca pe_string($_SER VER['REMOTE_ADDR']);
    $_SESSION['username'] = $username;
    $_SESSION['color'] = $color;
    $current_time = mysql_real_esca pe_string(time( ));

    $data[] = "\n".trim("\nus ername=".htmlsp ecialchars_deco de(substr($user name, 0, 10)) . "\r");
    $data[] = trim($current_t ime) . "\r";
    $data[] = "ip=".trim($ip1 ) . "\r";
    $data[] = "color=".trim($ color) . "\r";
    $data[] = "font=".trim($f ont) . "\r";
    $data[] = "message=".html specialchars_de code(trim(subst r($text, 0, 75)) . "\r");
    $data[] = trim("###");

    //followed later by


    $datal = file_get_conten ts("data.line") ;
    if (stristr($datal , $_SERVER['REMOTE_ADDR'])) {

    echo "<a href='http://helraizer.dnsal ias.net/Chat/index.php5?acti on=delete'><b>D elete your post</b></a>";

    $_SESSION['a'] = 1;

    if (isset($_SESSIO N['a']) && $_GET['action']=="delete") {
    ?><table align='center'> <tr><td><b>Po st Deletion</b></td></tr><tr><td>
    <?php
    $us_name = $_POST['user'];
    echo "<form align='center' name='post' action='delete. php5' method='post'>" ;
    echo "<label align='center' for='user'>Plea se enter the username you used in your post:</label>";
    echo "&nbsp;<inp ut type='text' align='center' id='usera' name='usera' size='10' maxlength='10'> ";
    echo "&nbsp;<inp ut type='submit' value='Delete!' name='submita' id='submita'>";

    ?>
    </form></td></tr></table><br><br>< br>
    <?php
    }
    }
    ?>[/code]

    Which is fine. It adds the new post after the previous, starting on a new line each time. It must do this or the script won't read be able to read the file at all, or will pull the wrong information out and thus break the formatting.


    function.php

    [code=php]

    <?php
    function ddfm_flock($han dle, $param) {

    global $enable_file_lo cking;

    if ($enable_file_l ocking == TRUE) {
    return flock($handle, $param);
    } else {
    return TRUE;
    }
    ?>
    }[/code]


    Then in finally in delete.php


    [code=php]

    <?php
    if (isset($_POST['submita'])) {

    $entries = file_get_conten ts("data.line") ;
    $entries = (array )explode('###', $entries);

    foreach ($entries as $entry) {
    $data_t = explode("\r", trim($entry));

    if ($data_t[0] == "username=" . $_POST['usera']) {

    $data = array();
    foreach ($data_t as $dt) {
    if (strpos($dt, '=') != false) {
    $k = substr($dt, 0, strpos($dt, '='));
    $v = substr($dt, strpos($dt, '=') + 1, strlen($dt) - strpos($dt, '='));
    $data[$k] = $v;
    }
    }
    $data['timestamp'] = $data_t[1];


    if (isset($_POST['usera'])) {
    if (is_string($_PO ST['usera'])) {
    $item = "username=" . $_POST['usera'];
    }
    }

    $the_ip = "";
    $entries = file_get_conten ts("data.line") ;
    $entries = (array )explode('###', $entries);
    foreach ($entries as $entry) {
    $data_t = explode("\r", trim($entry));
    if (trim($data_t[0]) == $item) {
    foreach ($data_t as $d) {
    if (strpos($d, 'username=') === 0) {
    $the_ip = substr($d, 9, strlen($d) - 1);
    }
    }
    }
    }

    $entries = file_get_conten ts("data.line") ;
    $entries = (array )explode('###', $entries);

    // recreate file
    $handle = fopen("data.lin e", "w");

    if (ddfm_flock($ha ndle, LOCK_EX)) { // do an exclusive lock

    foreach ($entries as $entry) {

    $data_t = explode("\r", trim($entry));

    if (trim($data_t[0]) != "") { // if valid item

    foreach ($data_t as $d) {
    if (strpos($d, 'username=') === 0) {
    $test_ip = substr($d, 9, strlen($d) - 1);
    }
    }

    if ($test_ip != $the_ip) { // put back

    foreach ($data_t as $d) {
    fwrite($handle, $d . "\r");
    }
    fwrite($handle, "###\r");

    } else {
    // skip items from this IP
    }

    }
    }

    ddfm_flock($han dle, LOCK_UN); // release the lock
    } else {
    }
    }
    }
    ?>[/code]

    which adds a blank line to the chatbox and thus the chatbox returns the wrong values and format.. How would I stop it from adding this extra line? If I change fwrite($handle, "###\r") to fwrite($handle, "###") it

    makes data.line format as:
    Code:
    CHATBOXTEXT
    7
    username=helraizer
    1202416953
    ip=86.140.73.183
    color=yellow
    font=palab
    message=bit of a bug, I admit
    ###username=Fjar
    1202420306
    ip=81.77.39.76
    color=yellow
    font=palab
    message=I love you, my Sam ^_^
    ###username=helraizer 
    1202420637
    ip=81.77.39.76
    color=aqua
    font=palab
    message=I love you too, my Fjar!! <3
    ###
    The username of the next post starts on the same lines as the ### and the same problem occurs

    Instead of reading

    [helraizer]bit of a bug, I admit (should be yellow)
    [Fjar] I love you my Sam!! ^_^ (should be yellow)
    [helraizer]I love you too, my Fjar!! <3 (should be blue)

    it reads

    [palab]palab
    [202416953][202420306]
    helraizer

    ^all of them white (the default if $color is not specified or different to those it is supposed to be).

    How would I get it to, when I delete a post, print the comment (all information) starting on the line after the previous ###?

    Hope that makes sense. Just ask if you need more information.

    Thanks,
    Sam

    The normal look:

    The broken look:
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Sam.

    Can you store these values in a database? SQLite can take care of everything for you, and you don't have to run a separate db server (http://php.net/sqlite).

    Comment

    • helraizer1
      New Member
      • Mar 2007
      • 118

      #3
      Originally posted by pbmods
      Heya, Sam.

      Can you store these values in a database? SQLite can take care of everything for you, and you don't have to run a separate db server (http://php.net/sqlite).
      Heya pbmods,

      Yeah, I moved onto a database a while back because of this deletion and editing problem. It made the functionality of the code much easier to work with.

      It was just that I made the first incarnation of the script was for flat file, which worked perfectly until I needed to edit or delete. Hense the move to a db.

      Thanks for the response.

      Sam

      Comment

      Working...