slier
slier

Reputation: 6750

File permission confusion

Found this bit of code in FrogCms

function isWritable($file=null) {
  $perms = fileperms($file);
  if (is_writable($file) || ($perms & 0x0080) || ($perms & 0x0010) || ($perms & 0x0002))
    return true;
}

I had a hard time understanding this part

(is_writable($file) || ($perms & 0x0080) || ($perms & 0x0010) || ($perms & 0x0002))

After doing some research i know that 0x0080 is permission for owner, 0x0010 is permission for group and 0x0002 is permission for other.Where do this value come form? Is this is a predetermined value for permission system?

And can someone explain to me for example how $perms & 0x0080 resolve to true, cause for example fileperms(__FILE__) return a value like 33206.How can u compare 33206 with 0x0080?

Upvotes: 1

Views: 2316

Answers (2)

Jon
Jon

Reputation: 437424

The constants you refer to (0x0080 etc) do not have the exact meanings you think they do. See the fileperms documentation:

0x0080 = owner writable
0x0010 = group writable
0x0002 = world writable

Also, you are not comparing them, you are performing a bitwise AND.

    1000000110110110 = 33206 (dec)
    0000000010000000 = 0x80  (hex)
AND ------------------------
    0000000010000000 = result (bin)

Since the result is non-zero, this means that the particular file is owner-writable.

But what does the code you give do?

Well, it returns true if and only if the file is writable by any of the following:

  • the user that the script runs as (checked by is_writable)
  • the user that owns the file
  • the group that owns the file
  • any user

This certainly looks like illogical and buggy to me, because e.g.:

  • if e.g. only the owner can write the file the function will return true; however, that certainly does not mean the file is writable for you, unless you are the owner!
  • if the aim is to check if anyone could write the file (which is kind of pointless really), the is_writable check would be superfluous and at the very least confusing.

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798744

Where do this value come form?

They're hexadecimal equivalents of the permission bits, normally specified in octal.

Is this is a predetermined value for permission system?

Yes.

And can someone explain to me for example how $perms & 0x0080 resolve to true, cause for example fileperms(__FILE__) return a value like 33206

>>> hex(33206)
'0x81b6'
>>> 0x81b6 & 0x0080
128

Non-zero values are true.

Upvotes: 2

Related Questions