Page 1 of 2

plz help get delete function working

Posted: Thu Mar 18, 2004 6:07 pm
by m3rajk
the issue is that on my server it will remove pictures et al, infact the only thing it doesn't remove is the main user database file. but on the deployment server, the pictures and about half the table entries remain.
in both cases the pics are considered to be part of the server processes. (nobody on the deployment and apache on my server)

i am seeking advice on how to actually get the pictures deleted. i think the tables might be a result of the pictures not deleting

delete function code:

Code: Select all

function misc(){ # misc controls
  include("/home/dcfydllc/includes/fyd.altincs.php"); # includes file (precautionary measure)
  $un=$_COOKIE['un']; $pw=$_COOKIE['pw']; # variables to talk to the db
  $db=mysql_connect($host, $login3, $pass3) or die("cannot access mysql"); # connect to the db
  $fyd=mysql_select_db($dbname, $db) or die("cannot access db"); # get the db
  $memfind=mysql_query("SELECT uid FROM users WHERE username='$un' AND password='$pw'", $db);
  if(mysql_num_rows($memfind)==0){ # problem
    cae(); // call the forum access error page
  }else{ # we found the user
    $maininf=mysql_fetch_array($memfind); $uid=$maininf['uid']; $worked=0;
    $result='<h1 class="require">REPORT ALL ERRORS TO: ADMIN @ FindYourDesire.com</h1>';
    if(isset($_POST['act'])&&($_POST['act']=='reset')){ # reset the votes on the user
      $rstat=mysql_query("UPDATE stats SET pvt='0', pvc='0' WHERE uid='$uid'", $db);
      if(mysql_affected_rows($db)>'-1'){ $worked=1; } // did stats get reset
      else{ // there was a problem
        $errno=mysql_errno($db); $error=mysql_error($db); $result="<p>stats:$errno: $error";
      } // error has been recorded
      $rvote=mysql_query("DELETE FROM votes WHERE voteeuid='$uid'", $db);
      if((mysql_affected_rows($db)>'-1')&&($worked)){ $result="<p>Your desirability has been reset.</p>"; }
      else{ // there was a problem
        $errno=mysql_errno($db); $error=mysql_error($db); $result.="<br />votes:$errno: $error</p>";
      } // error has been recorded
    }elseif(isset($_POST['act'])&&($_POST['act']=='delete')){ // deleting your profile
      echo "      <p>Deleting $un...\n"; $imgbase=$faup; $npib=$fuup;
      foreach($images as $key=>$value){ # for each possible picture
        echo "      <br />Checking for $value\n"; flush(); $pica=$imgbase.$key.$un.'.jpg';
        $picb=$imgbase.'old.'.$key.$un.'.jpg'; $picc=$npib.$key.$un.'.jpg';
        /* delete the pics if they exist */
        if(is_file($pica)){ unlink($pica); echo "      <br />Removing $pica\n"; }
        if(is_file($picb)){ unlink($picb); echo "      <br />Removing $picb\n"; } 
        if(is_file($picc)){ unlink($picc); echo "      <br />Removing $picc\n"; }
      }
      echo "      <br />Deleting you from table BIO"; flush();
      $dfs=mysql_query("DELETE FROM bio WHERE uid='$uid'", $db);
      if(mysql_affected_rows($db)==1){ // successful deletion
        echo "      <br />Removed $un (user # $uid) from table BIO";
      }else{ // error
        $errno=mysql_errno($db); $error=mysql_error($db); $delerr=1;
        echo "<br />Bio:$errno: $error";
      }
      echo "      <br />Deleting you from table COMMENTS"; flush();
      $dfs=mysql_query("DELETE FROM comments WHERE ctuid='$uid'", $db);
      if(mysql_affected_rows($db)==1){ 
        echo "      <br />Removed $un (user # $uid) from table COMMENTS";
      }else{ // error
        $errno=mysql_errno($db); $error=mysql_error($db);
        echo "<br />Comments:$errno: $error";
      }
      echo "      <br />Deleting you from table FRIENDS"; flush();
      $dfs=mysql_query("DELETE FROM friends WHERE uid='$uid'", $db);
      if(mysql_affected_rows($db)==1){ 
        echo "      <br />Removed $un (user # $uid) from table FRIENDS";
      }else{ // error
        $errno=mysql_errno($db); $error=mysql_error($db);
        echo "<br />Friends:$errno: $error";
      }
      echo "      <br />Deleting you from table INTERESTS"; flush();
      $dfs=mysql_query("DELETE FROM interests WHERE uid='$uid", $db);
      if(mysql_affected_rows($db)==1){ 
        echo "      <br />Removed $un (user # $uid) from table INTERESTS";
      }else{ // error
        $errno=mysql_errno($db); $error=mysql_error($db); $delerr=1;
        echo "<br />Interests:$errno: $error";
      }
      echo "      <br />Deleting you from table MAIL"; flush();
      $dfs=mysql_query("DELETE FROM mail WHERE to_id='$uid'", $db);
      if(mysql_affected_rows($db)==1){ 
        echo "      <br />Removed $un (user # $uid) from table MAIL";
      }else{ // error
        $errno=mysql_errno($db); $error=mysql_error($db);
        echo "<br />Mail:$errno: $error";
      }
      echo "      <br />Deleting you from table MSGS"; flush();
      $dfs=mysql_query("DELETE FROM msgs WHERE to_id='$uid'", $db);
      if(mysql_affected_rows($db)==1){ 
        echo "      <br />Removed $un (user # $uid) from table MSGS";
      }else{ // error
        $errno=mysql_errno($db); $error=mysql_error($db);
        echo "<br />Msgs:$errno: $error";
      }
      echo "      <br />Deleting you from table MATCH"; flush();
      $dfs=mysql_query("DELETE FROM personality WHERE uid='$uid'", $db);
      if(mysql_affected_rows($db)==1){ 
        echo "      <br />Removed $un (user # $uid) from table MATCH";
      }else{ // error
        $errno=mysql_errno($db); $error=mysql_error($db);
        echo "<br />Match:$errno: $error";
      }
      echo "      <br />Deleting you from table PESTS"; flush();
      $dfs=mysql_query("DELETE FROM pests WHERE uid='$uid'", $db);
      if(mysql_affected_rows($db)==1){ 
        echo "      <br />Removed $un (user # $uid) from table PESTS";
      }else{ // error
        $errno=mysql_errno($db); $error=mysql_error($db);
        echo "<br />Pests:$errno: $error";
      }
      echo "      <br />Deleting you from table PERSONALITY"; flush();
      $dfs=mysql_query("DELETE FROM personality WHERE uid='$uid'", $db);
      if(mysql_affected_rows($db)==1){ 
        echo "      <br />Removed $un (user # $uid) from table PERSONALITY";
      }else{ // error
        $errno=mysql_errno($db); $error=mysql_error($db); $delerr=1;
        echo "<br />Personality:$errno: $error";
      }
      echo "      <br />Deleting you from table POSTS"; flush();
      $dfs=mysql_query("DELETE FROM posts WHERE author='$uid'", $db);
      if(mysql_affected_rows($db)==1){ 
        echo "      <br />Removed $un (user # $uid) from table POSTS";
      }else{ // error
        $errno=mysql_errno($db); $error=mysql_error($db);
        echo "<br />Posts:$errno: $error";
      }
      echo "      <br />Deleting you from table STATS"; flush();
      $dfs=mysql_query("DELETE FROM stats WHERE uid='$uid'", $db);
      if(mysql_affected_rows($db)==1){ 
        echo "      <br />Removed $un (user # $uid) from table STATS";
      }else{ // error
        $errno=mysql_errno($db); $error=mysql_error($db); $delerr=1;
        echo "<br />Stats:$errno: $error";
      }
      echo "      <br />Deleting you from table THREADS"; flush();
      $dfs=mysql_query("DELETE FROM threads WHERE thread_auth='$uid'", $db);
      if(mysql_affected_rows($db)==1){ 
        echo "      <br />Removed $un (user # $uid) from table THREADS";
      }else{ // error
        $errno=mysql_errno($db); $error=mysql_error($db);
        echo "<br />Threads:$errno: $error";
      }
      echo "      <br />Deleting you from table TOPS"; flush();
      $dfs=mysql_query("DELETE FROM tops WHERE memuid='$uid'", $db);
      if(mysql_affected_rows($db)==1){ 
        echo "      <br />Removed $un (user # $uid) from table TOPS";
      }else{ // error
        $errno=mysql_errno($db); $error=mysql_error($db); $delerr=1;
        echo "<br />Tops:$errno: $error";
      }
      echo "      <br />Deleting you from table VOTES"; flush();
      $dfs=mysql_query("DELETE FROM votes WHERE voteeuid='$uid'", $db); // votes on
      if(mysql_affected_rows($db)==1){ 
        echo "      <br />Removed $un (user # $uid) from table VOTES";
      }else{ // error
        $errno=mysql_errno($db); $error=mysql_error($db);
        echo "<br />Votes:$errno: $error";
      }
      $dfs=mysql_query("DELETE FROM votes WHERE voteruid='$uid'", $db); // votes made this time period
      if(mysql_affected_rows($db)==1){ 
        echo "      <br />Removed $un (user # $uid) from table VOTES";
      }else{ // error
        $errno=mysql_errno($db); $error=mysql_error($db);
        echo "<br />Votes:$errno: $error";
      }
      echo '</p>';
      if(!($delerr)){ # stats,bio,interests,personality,tops did not result in an error (may be expanded to more)
        echo "      <br />Deleting you from table USERS";
        $dfs=mysql_query("DELETE FROM users WHERE uid='$uid'", $db);
        if(mysql_affected_rows($db)==1){
          echo "      <br />Deletion complete.</p>\n$errs";
        }else{ // error
          $errno=mysql_errno($db); $error=mysql_error($db); $delerr=1;
          echo "<br />Users:$errno: $error</p>\n$errs";
        }
      }else{ // something is wrong
        echo "      <br />Deletion encountered errors. uid=$uid username=$un";
        echo "      <br />$errs</p>"; 
      }
    }
    // make the page
    echo <<<END
      $result
      <h2>NOTICE: THE FOLLOWING OPTIONS <strong>CANNOT</strong> BE UNDONE</h2>
      <script language="javascript" type="text/javascript">
        function doubleCheck(){
          var what=document.misc.act.value;
          
          if(what=="reset"){
            return confirm("Are you sure you want to reset your score?");
          }else if(what=="delete"){
            return confirm("Are you sure you want to delete your account? (This cannot be undone)");
          }else{
            return false;
          }
        }
      </script>
      <form action="{$_SERVER['PHP_SELF']}" name="misc" method="POST" onSubmit="return doubleCheck();">
        <input id="fn" type="hidden" name="fn" value="misc" />
        $tsnw
          <tbody>
            <tr>
              <td class="center">
                <select id="act" name="act" size="1">
                  <option value="">Choose an Option</option>
                  <option value="reset">Reset Your Score</option>
                  <option value="delete">Delete Your Profile</option>
                </select>
              </td>
            </tr>
            <tr>
              <td class="center"><input type="submit" value="Let's Do It!" /></td>
            </tr>
          </tbody>
        </table>
      </form>
END;
  }
}
thanx in advance for any help

Re: plz help get delete function working

Posted: Fri Mar 19, 2004 8:35 am
by TheBentinel.com
m3rajk wrote:the issue is that on my server it will remove pictures et al, infact the only thing it doesn't remove is the main user database file. but on the deployment server, the pictures and about half the table entries remain.
When a file is created on unix, it is created with a set of permissions based on the UMASK setting. It's funky and I always have to muck with it to get it working. I think if you use UMASK 0, then the files will be created with wide open permissions, which is probably what you want.

What might be happening is that your files get created by user1 with read/write permissions for them only, then some other user (the webserver?) tries to delete them. The O/S won't let the file be deleted, so it hangs around.

Maybe. It's worth looking at.

Posted: Fri Mar 19, 2004 1:32 pm
by m3rajk
yeah
just checked permissions. my server gets it as -rw-rw-rw-
the host give it as -rw-------

erm... if i'm right, the command to set it right would be
umask(444);
move_uploaded_file($from, $to);

1 is read, 2 is write, 4 is execute, therfore removing execure and leaving the rest is umask(444), right?

Posted: Fri Mar 19, 2004 1:43 pm
by TheBentinel.com
m3rajk wrote:1 is read, 2 is write, 4 is execute, therfore removing execure and leaving the rest is umask(444), right?
I quit pretending that I understood UMASK years ago. I always just set it, touch a file, check it, and do it again (and again, and again) until it's what I want.

444 sounds like as good a place to start as any. :-)

Posted: Fri Mar 19, 2004 4:45 pm
by johnperkins21
I think if you remove execute you have (333), but I can be wrong.


1 (read) + 2 (write) = 3 (rw-)

Posted: Fri Mar 19, 2004 4:52 pm
by TheBentinel.com
johnperkins21 wrote:I think if you remove execute you have (333), but I can be wrong.


1 (read) + 2 (write) = 3 (rw-)
That's where the voodoo of umask comes. It isn't "these are the permissions I want you to use", it's "subtract this from my default permissions". If I really knew it that well, I wouldn't be afraid of it, so that may not be dead on. But it definitely isn't straightforward.

From the manpage:
The Unix permission rwxr-x--- is represented as three sets of three bits, or three octal digits: 0750 (the leading 0 indicates octal and isn't one of the digits). The umask value is such a number representing disabled permissions bits. The permission (or ``mode'') values you pass mkdir or sysopen are modified by your umask, so even if you tell sysopen to create a file with permissions 0777, if your umask is 0022 then the file will actually be created with permissions 0755. If your umask were 0027 (group can't write; others can't read, write, or execute), then passing sysopen 0666 would create a file with mode 0640 (0666 &~ 027 is 0640).

Here's some advice: supply a creation mode of 0666 for regular files (in sysopen) and one of 0777 for directories (in mkdir) and executable files. This gives users the freedom of choice: if they want protected files, they might choose process umasks of 022, 027, or even the particularly antisocial mask of 077. Programs should rarely if ever make policy decisions better left to the user. The exception to this is when writing files that should be kept private: mail files, web browser cookies, .rhosts files, and so on.

Posted: Fri Mar 19, 2004 5:41 pm
by m3rajk
actually this is for a website.
the nature of it is that the user uploads a picture to be displayed on their profile. they may upload another over it later on.

however, this means that everyoene will hav ehte smae permission. it sounds to me like umask is subtracting to the poitn i want. the examples i found in mthe oreilly book use umask(777) to remove all permissions and umask(000) to set all permissions, which sounds to me like the oppposite of how unix does it

Posted: Fri Mar 19, 2004 5:49 pm
by m3rajk
apparently their umask is 18. i don't know if that means 0018 but suspect that
http://www.findyourdesire.com/umaskfind.php

Posted: Fri Mar 19, 2004 5:51 pm
by m3rajk
this is how the files show up in an ls -la (this is the directory of unapproved (denied or waiting approval) pictures. picture directory name has been changed
everything else unedited from print out
[dcfydllc@seed unapproved]$ ls -la
total 532
drwxrwxrwx 2 dcfydllc dcfydllc 4096 Mar 18 16:23 .
drwxr-xr-x 7 dcfydllc dcfydllc 4096 Mar 19 17:48 ..
-rw-rw-r-- 1 dcfydllc dcfydllc 1735 Jan 18 22:20 index.html
-rw------- 1 nobody nobody 4813 Mar 1 23:50 main.ajaxwhizzat.jpg
-rw------- 1 nobody nobody 125615 Mar 2 11:51 main.miranda.jpg
-rw------- 1 nobody nobody 11417 Feb 10 20:28 salute.joey__1982.jpg
-rw------- 1 nobody nobody 11797 Feb 1 18:40 salute.m2spoild.jpg
-rw------- 1 nobody nobody 78079 Feb 1 22:33 salute.nasalhair.jpg
-rw------- 1 nobody nobody 36830 Jan 20 22:43 salute.nephalim.jpg
-rw------- 1 nobody nobody 11864 Jan 29 23:08 salute.princessstone.jpg
-rw------- 1 nobody nobody 54605 Jan 19 21:22 salute.rain.jpg
-rw------- 1 nobody nobody 22185 Feb 8 17:58 salute.starrykiss.jpg
-rw------- 1 nobody nobody 49962 Jan 25 11:20 salute.uzaho.jpg
-rw------- 1 nobody nobody 25066 Feb 17 20:23 t2.mickeyisagyrl.jpg
-rw------- 1 nobody nobody 6359 Feb 26 15:30 t2.peaches11.jpg
-rw------- 1 nobody nobody 13837 Feb 1 23:43 t3.evilkefz2k1.jpg
-rw------- 1 nobody nobody 10300 Feb 13 23:59 t3.jerseygirl.jpg
-rw------- 1 nobody nobody 21628 Jan 19 12:44 t3.sexyjess.jpg

Posted: Fri Mar 19, 2004 5:55 pm
by m3rajk
ok. read = 4
write = 2
execute = 1

i wanna get it so the delete deletes the files. so since i want to get to permissions being 0666, i would need umask(0111)?

Posted: Fri Mar 19, 2004 7:19 pm
by TheBentinel.com
m3rajk wrote:i wanna get it so the delete deletes the files. so since i want to get to permissions being 0666, i would need umask(0111)?
I really just never know. Set it to 0, run it, and see what you get. Then set it to 777, run it, and see what you get. You definitely need to play with the umask, but what number to use? I have no idea.

Experimentation will get you what you need.

Posted: Wed Mar 24, 2004 9:06 pm
by m3rajk
umask isn't helping. seems no matter the umask setting it's still not deleting. i tested and found that whether it's -rwxrwxrwx or anything else it's still not deleted.
so i tested the $fuup and $faup which give the Full Unapproved User Pic path and Full Approved User Pic path respectively.
both give the correct paths, which measn all the file names are right. the problem would lie elsewhere. any ideas?

Posted: Wed Mar 24, 2004 9:17 pm
by markl999
What does echo'ing $pica show?

Posted: Thu Mar 25, 2004 10:54 am
by m3rajk
onthe test run it showed the correct path to the user /var/www/html/dcfydllc/userpics/main.gamma105.jpg

Posted: Thu Mar 25, 2004 1:12 pm
by markl999
Maybe it's just not reaching the unlink bit? *shrug*
Try using file_exists() instead of is_file()