run exec() or system() from web app as user?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
iansane
Forum Commoner
Posts: 62
Joined: Sun Apr 18, 2010 1:26 pm

run exec() or system() from web app as user?

Post by iansane »

Hi,

I am working on a application written by someone else that is supposed to create a simple enrolment form from a admin page. The purpose is to make many different enrollment forms for different participants but be able to do it all from the admin page. The problem is that the admin page only populates a database and gives the enrollment application an ID#. The company administrator still has to manually create a folder and drop in a index.php template and modify the $ID variable in order for the enrollment form to work.

What I want to do is have php create the folder and write the index.php file with the correct $ID allready in it so the whole thing is automatic and doesn't require the administrator logging into the server and having to manually do things like create the directory and copy in the index.php. The point of the app is so the administrator can create the app with no knowlege of hosting, directories, or php but without this functionality it is defeating the purpose of the app.

If I create a directory with

Code: Select all

mkdir("./someAppName", 0700);
the directory in which the php is located has to have priveleges for the www user so this could still require a server login to chmod the directory so www can create the directory.

Any suggestions or advice on doing this so when the admin is logged into the app, the app will be able to run and have full permissions to create and delete files and directories?

Thanks
iansane
Forum Commoner
Posts: 62
Joined: Sun Apr 18, 2010 1:26 pm

Re: run exec() or system() from web app as user?

Post by iansane »

OK this is working for my purpose at the moment

Code: Select all

<?php
//create directory
$pathRoot = "../";
$dir = "mkdirTest/mkdirTest2";
$path = $pathRoot.$dir;

mkdir($path, 0777, true);


//create index.php
$fileName = "../mkdirTest/mkdirTest2/index.php";
$handle = fopen($fileName, 'w') or die("Can't open file");
$fileString = "<?php echo 'This is a test'; ?>";
fwrite($handle,$fileString)or die("can't write file");

//make link to new url
$domain = "lotuscontrol.homelinux.net/";
$url = "http://".$domain.$dir;

echo "Here is a link to your new application: ";
echo "<a href='".$url."'>NewApp</a>"; 
?>
I still would like to know how to send commands through the exec() and/or system() functions from www as root so I can chmod file and folder permissions when needed because this only works if I build the directory structure beginning in the www or htdocs folder for which www-data user has access.

If I created a directory through ssh, ftp, etc. It would not have the same permissions so it may or may not work without a way to send the mkdir command through system() or exec() as a uid with the right permissions.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: run exec() or system() from web app as user?

Post by VladSun »

Install and use sudo.
http://www.sudo.ws/sudo/sudo.man.html

You need to edit the /etc/sudoers *AFTER* reading the manual.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply