andy's php projects
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Script and site usage:
All of the scripts on this site are provided free for non-commerical, personal or non-profit use. If you find these scripts useful to you, appreciate the free support, or are just an all round nice person, then why not donate a little dosh? If you wish to use any of these in a commercial system, please refer to the payment plan.


Practical Example?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    php.amnuts.com Forum Index -> User Quota
View previous topic :: View next topic  
Author Message
Rickster
Newbie


Joined: 08 May 2003
Posts: 3

PostPosted: Thu May 08, 2003 4:28 pm    Post subject: Practical Example? Reply with quote

How do I use this code to create a web page using the output of 'quota -v' ? In other words, how do you parse the data from the unix command?
Back to top
View user's profile Send private message
Rickster
Newbie


Joined: 08 May 2003
Posts: 3

PostPosted: Fri May 09, 2003 6:23 pm    Post subject: Underwhelmed Reply with quote

Guess nobody wants to take a shot at this one. :( I have tried to use EXEC() and SHELL_EXEC and backtick to run QUOTA or DU and pass the current disk isage back to the script but to no avail. We're running PHP4 and I can get other shell commands like LS and PWD to work with no problem.

I found one other poor soul on the web with the same issue. Nobody had an answer for him either. :cry:
Back to top
View user's profile Send private message
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Fri May 09, 2003 6:46 pm    Post subject: Reply with quote

Hi Rickster - wlecome to the board!

Okay, first of all, this isn't a high traffic board at all (at the moment, at least, but I'm always hopeful! Wink) So you'll have to give me more than 12 or so hours to reply to a message! Smile

The problem with running 'quota' from PHP is that more often than not PHP runs as 'nobody', because it's an apache module, or the CGI binary is set up that way. Because of that running 'quota -v' won't really produce any results. What you could try is using 'quota -u', which is what I started to do with the quota class before I decided it was better to pass the quota parameters to it to leave it open to display any quota. I cannot guarantee this'd work for all systems, but give something like this a go:

Code:
function getQuota($user = '')
{
     if ($user) $cmd = 'quota -u ' . escapeshellarg($user);
     else $cmd = 'quota';
     $pipe = @popen($cmd, 'r');
     if (!$pipe) return '';
     $output = array();
     while (!feof($pipe))
     {
           $line = fgets($pipe, 1024);
           $output[] = explode("\t", trim($line));
     }
     pclose($pipe);
     return $output;
}


Run that function and see what $output gives you. If nothing then you might not be able to run 'quota' via PHP, and maybe you need a subsidiary script using Perl, or scheel script, or something like that, which you can call. If, however, $output gave you some text then check it out and you might find you can run a couple regular expressions on it to parse out the information you want.

Just to show you some output, I ran this on one of my accounts and did a var_dump of the $output content, and this is what I got:

Code:
array(5) {
  [0]=>
  array(1) {
    [0]=>
    string(40) "Disk quotas for user amnuts (uid 32015):"
  }
  [1]=>
  array(1) {
    [0]=>
    string(74) "Filesystem  blocks   quota   limit   grace   files   quota   limit   grace"
  }
  [2]=>
  array(1) {
    [0]=>
    string(65) "/dev/sda7   82800  204800  204800            2776       0       0"
  }
  [3]=>
  array(1) {
    [0]=>
    string(65) "/dev/sda3      12  204800  204800               4       0       0"
  }
  [4]=>
  array(1) {
    [0]=>
    string(0) ""
  }
}


All I would then have to do is split the lines up into words and use the 2nd and 3rd words. However, not all accounts give that same kind of output... One of my other shell accounts, for example, puts the filesystem on a different line than the quota information! In instances like that, maybe you should count up the words of each line and determine what to do with it.

That should give you a starting block, I hope.

Thanks for checking out the script!

Andy


Last edited by amnuts on Tue Aug 26, 2003 3:57 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Rickster
Newbie


Joined: 08 May 2003
Posts: 3

PostPosted: Sat May 10, 2003 10:14 pm    Post subject: Reply with quote

Thanks so ever much for the hints! Really appreciate it. We're running FreeBSD 4.4-RELEASE (VKERN) #9 so the QUOTA output is much like your example. I'll try your suggestions and get back to you here.

cheers!
Rickster
Back to top
View user's profile Send private message
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Tue May 27, 2003 4:50 am    Post subject: Reply with quote

Hi Rickster,

Did you ever try hooking it up to your system? Any kind of success?

Andy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
beos
Newbie


Joined: 20 Oct 2004
Posts: 6

PostPosted: Wed Oct 20, 2004 4:12 am    Post subject: help plz Reply with quote

your code is great but how do i out put the function
my php sux
function getQuota($user = '')
{
if ($user) $cmd = 'quota -u ' . escapeshellarg($user);
else $cmd = 'quota';
$pipe = @popen($cmd, 'r');
if (!$pipe) return '';
$output = array();
while (!feof($pipe))
{
$line = fgets($pipe, 1024);
$output[] = explode("\t", trim($line));
}
pclose($pipe);
return $output;
}
please post how to use it i cant cut the string, please show the echo output examples, thanks i love it by the way
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    php.amnuts.com Forum Index -> User Quota All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group