Running imagesnap in PHP with Mac Server - php

I am trying to develop a small site to take snapshots of my house using a webcam connected to to my mac mini at home. The mac mini is running OS X Server. The Webcam is a Logitech HD Pro Webcam C920 (USB connected). It is based on PHP and HTML5 (no other code besides CSS).
My development machine is a MacBook Pro using the internal iSight camera. It is not running OS X server; I manually enabled apache and PHP. While developing the site on my laptop, everything worked flawlessly. When I installed it on my mac mini the imagesnap program turns on the camera but never finishes; I have to manually kill the process, and the snapshot is never created.
The following error is logged:
imagesnap[2363]: *** QTCaptureSession warning: Session received the following error while decompressing video: Error Domain=NSOSStatusErrorDomain Code=-12903 "The operation couldn’t be completed. (OSStatus error -12903.)". Make sure that the formats of all video outputs are properly configured.
This is only when running it from the site directly. If I run the same program from the command-line, it works just fine... even running it as the _www user!
sudo -u _www bin/imagesnap images/snapshot_$DATE.jpg
This is the code that does the call in PHP:
<?php
if(isset($_POST['submit'])) {
system('bin/run_imagesnap.sh', $return_value);
if ($return_value) {
echo "The command failed.";
}
}
?>
I was unable to figure out how to write the date to the output filename (I'm a noob with PHP), so I used a bash script to do it for me (I'm better with BASH):
#!/bin/bash
DATE=`date +"%m-%d-%y_%T"`
bin/imagesnap -q -w 3 images/snapshot_$DATE.jpg
I've also tried exec(), passthru(), and shell_exec() all with the same results.
Finally, I've also tried the following: Create test.php, put code:
<?php
system('bin/run_imagesnap.sh', $return_value);
if ($return_value) {
echo "The command failed.";
}
?>
and ran it on the command-line as the _www user using the php-cli:
sudo -u _www php test.php
and it also worked just fine (created the snapshot file and did not give error).
I'm thinking maybe it has something to do with the OS X server app, but google searches return nothing.
I'm stumped. Why does it break when I run the same exact code on the browser but works fine on the command-line using the same exact tools and user?

Unknown if this is the correct way, but I ended up using sudo to run the script.
visudo
and then give the _www user passwordless root access to the script:
_www ALL=NOPASSWD: /path/to/script
and now it works. There's always more than one way to skin a cat..

Related

php exec function not running correctly

I compile my c++ file by this command:-
g++ main.cpp -o main pkg-config --cflags --libs opencv
This runs fine. Now when i run main file by command line using this command './main', it's runs fine, and stared my webcam.
But when i write code in php to execute this file by php code, It's also runs but my webcam does not found, And gives me an error.
starting cam No webcam where found
I don't know what's the reason behind this.
I am putting some piece of code of my files.
main.cpp
CvMemStorage *storage = cvCreateMemStorage(0);
CvHaarClassifierCascade *cascade = (CvHaarClassifierCascade*)cvLoad("data/haarcascade_frontalface_default.xml");
printf("starting cam\n");
CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
if(capture == 0)
{
printf("No webcam where found");
return 1;
}
CvScalar red = CV_RGB(250,0,0);
// counters used for frame capture
int happy_count = 0;
int sad_count = 0;
printf("starting recognition\n");
output.php
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo shell_exec('./main');
Error on run php:- starting cam No webcam where found.
Note:-Works fine on terminal
That's my suggestion only, but could you check which user is executing programs while using PHP exec? Probably apache (are you using that?) has own user, which has no access to webcam. I suggest to try running apache as regular user to check if that helps, or give access to webcam for your apache user.
Here you have some help in that matter.
https://serverfault.com/questions/125865/finding-out-what-user-apache-is-running-as
Seems like permissions issue to me. When you execute the program through php script, it is executed by the user that runs the server process. I am guessing you are using apache2 webs server to serve php content, which runs as user www-data in Ubuntu. If this is true, you just add the user www-data to the group video.
sudo usermod -a -G video www-data
If you are using another web server or a different linux distribution, or your web server runs with a different user, you need to modify the command accordingly.

Raspberry Pi unable to execute script by PHP

I'm playing with Raspberry Pi and anm Arduino shield in order to run a script via Apache/PHP. This script simple blink a LED. I have already tested the script via shell and it works fine, with the command
/root/arduPi/blink_test
I'm able to see my LED blinking. So I made the same thing via Apache PHP with this short PHP script
<?php
if(isset($_GET['cmd'])){
echo '/root/arduPi/'.$_GET['cmd'];
exec('/root/arduPi/'.$_GET['cmd']);
}
?>
but nothing happen and no error has been displayed.
I tested the PHP code with
<?php
phpinfo();
?>
and it's fine. How can I fix this problem?
I had the same issue a while back it was because Apache does not have permission to access certain devices on the Pi. I fixed this by getting rid of the need to be root to access these devices. HERE Is my post about this same issue the fix was to setup sudo as passwordless.
THIS is what I used to accomplish setting up sudo as passwordless. You should then be able to execute the script as follows exec('sudo /root/arduPi/'.$_GET['cmd']);

Server side script for launching application

I have been trying unsuccessfully so far to write a php script that will run when a page is opened and that will launch metasploit!
I ve tried shell_exec and exec and all the other alternatives but although I can get it to do simple things (i.e. ls, cds etc) if I try msfconsole it doesnt do anything!
I have also tried a different script that launches firefox and again nothing happens!
Now i know that php runs on the server and I m not expecting to see a console or firefox opening in the clients machine! Instead in order to check if it works I am trying to echo out the output of the shell_exec!But anyway since im hosting the files on my machine (i.e. this is the server and a VM is the client) if it could actually launch firefox i should be able to see the app opening here in the same way as by just doing this from the command line!
What am I missing?
Is there any other way to do this?(i.e. Launch metasploit everytime a user opens up my page)
NOTE: I've tried specifying the full path for msfconsole but that didnt work either!
Heres what I have so far:
$output = shell_exec('/opt/local/libexec/metasploit3/msfconsole;show');
echo "<pre>$output</pre>";
The ";show" bit was used in order to actually make it run something and print some stuff but didnt make any difference!
When you run a gui application from the command prompt in a X window system, it will use the default display. When you run it using php which is embedded in apache webserver, the program may not know where to display the gui application.
there are 2 things to make this work.
The program that executes the gui application must have permission to use display
you need to tell the program which display to use.
I used the following in my php script
<?php
$cmd = `export DISPLAY=:0; gedit`;
shell_exec($cmd);
?>
and ran the script from terminal using php -f test.php
I got the gedit up and running.
You can test the same with the script in apache too.
Please add apache user with privileges to access display server
update: I just added the following in /etc/apache2/apache2.conf (I am using ubuntu)
User poomalai
Group poomalai
and restarted the web server
sudo service apache2 restart
now I accessed localhost/test.php
and Presto!! I got the gedit :)
Hope this helps

Executing a command line using php shell_exec() for initiating a camera on the server

I am using opencv for initiating the camera on my arch linux. Its getting initiated and works well when I actually do it from the command line on the server itself.
I want to initialize it using php. I tried doing it using shell_exec from PHP.
My PHP file looks like:
<?php
$output=shell_exec('LD_LIBRARY_PATH=usr/local/lib ./a.out 0 2>&1 1>/dev/null');
echo $output;
?>
It gives this output:
ERROR: capture is NULL
I am running this through my windows web browser as a client and the opencv and the related files are on the server that is my arch linux.
I want to start the camera and capture images when I run this php file from the windows web browser, but when executed it throws the error as mentioned.
While this may work when you are SSHed into your server. The webserver user is most likely different than the user you login as. Popular user ids/groups that webservers run as on Linux machines are http, www-data, nobody, and others.
From this point you have two options.
You can make sure the script you are trying to run from PHP (and all of it's children, if any) is able to be run by the webserver user.
You can modify your /etc/sudoers file which gives the webserver user access to elevate permissions for that script only. (NOTE: This potentially opens up security holes so be careful).
To find out what user your webserver runs as execute this: ps aux
Take a look at the output and the first column in the output lists the user that that process is running at. Here's an excerpt of my webserver (nginx) on one of my boxes:
www-data 26852 0.0 0.0 29768 3840 ? S Jun04 0:50 nginx: worker process
You can see that nginx runs with the user www-data here. You can also execute the command with grep to help you find the process quicker. Grep will only show you those lines which match what you send to it. Here's an example: ps aux | grep nginx
Ok now that you know what user the webserver is running as, let's try giving that user access to the script. Let's say your script is foo and is located in /usr/local/bin. You would do the following commands:
chown www-data /usr/local/bin/foo
After changing ownership on the file try to rerun your command again from your PHP page and see if it works.
For completeness I also said you could give your webserver user sudo privileges to that file. To do that you would need to append the following line to the bottom of your /etc/sudoers file:
www-data ALL= NOPASSWD: /usr/local/bin/foo
Then your shell_exec command could switch to this:
shell_exec('sudo /usr/local/bin/foo');
Please keep in mind that doing this would allow your webserver user to run the script as root which is incredibly dangerous in a variety of situations. However, the camera may require elevated permissions to work. I'm not sure what the permissions requirements are on the camera setup you are trying to invoke.
Good luck. Hope this helps!

Php : running ssh from Windows to login to a Linux and run a script

Here's my goal :
I have a Windows XP PC with all the source code in it and a development database.
Let's call it "pc.dev.XP".
I have a destination computer that runs Linux.
Let's call it "pc.demo.Linux".
Here's what I've done on "pc.dev.XP" (just so you get the context) :
installed all cygwin stuff
created a valid rsa key and put it on the dest
backup computer so that ssh doesn't
ask for a password
rsync works pretty well this way
If i try to do this on "pc.dev.XP" via a command line :
cd \cygwin\bin
ssh Fred#pc.demo.Linux "cd /var/www && ls -al"
this works perfectly without asking a password
Now here's what I want to do on the "pc.dev.XP":
launch a php script that extract the dev. database into a sql file
zip this file
transfer it via ftp to the "pc.demo.Linux"
log to the "pc.demo.Linux" and execute "unzip then mysql -e "source unzipped file"
if I run on "pc.dev.XP" manually :
putty -load "myconf" -l Fred -pw XXX -m script.file.that.unzip.and.integrates.sql
this works perfectly.
Same for :
cd \cygwin\bin
ssh Fred#dest "cd /var/www && ls -al"
If I try to exec() in php (wamp installed on "pc.dev.XP") those scripts they hangs. I'm pretty sure this is because the user is "SYSTEM" and not "Fred", and putty or ssh ask for a password but maybe I'm wrong.
Anyway I'm looking for a way to automate those 4 tasks I've described and I'm stuck because exec() hangs. There's no problem with safe_exec_mode or safe_exec_dir directives, they're disabled on the development machine, thus exec() works pretty well if I try some basic stuff like exec("dir")
Any idea what I could do / check / correct ?
I'm not sure if this is what you need, but I typically use a construct like this to sync databases across machines:
php extractFromDb.php | ssh user#remote.com "mysql remoteDatabaseName"
This executes the PHP script locally, and pipes the SQL commands the script prints out through SSH straigt into the remote mysql process which executes them in the remote database.
If you need compression, you can either use SSH's -C switch, or integrate the use of your compression program of choice like this:
php extractFromDb.php | gzip -9 | ssh user#remote.com "gunzip | mysql remoteDatabaseName"
You want to do this from PHP running under apache, as in I go to http://myWebserver.com/crazyScript.php and all this happens? Or you just want to write your scripts in PHP and invoke them via cmd line?
If you want the first solution, try running your apache/iss under a different user that has credentials to perform all those tasks.
"if I run on the development PC manually this works perfectly.".
Why not do it like that? When you run that script, I assume you're connecting to the local SSH server on the dev machine. When you do this, you are using the credentials Fred, so everything works. When you run the PHP script, you are right that it is probably running as SYSTEM.
Try either changing the user that apache is running as or use php to connect to the local ssh thereby using alternate credentials.
Here's what I did :
a batch file that :
Calls a php file via "php.exe my_extract_then_compress_then_ftp.php"
Calls rsync to synchronize the source folder
Calls putty -l user -pw password -m file_with_ssh_commands_to_execute
It works like a charm.

Categories