Mapped Network Drives - php

I have mapped a network drive to a computer in my home network. Now I am trying to access it via PHP - I did this quick test:
echo opendir('Z:\\');
This gives me:
Warning: opendir(Z:\) [function.opendir]: failed to open dir: No error in C:\wamp\www\webs\tester-function.php on line 3
What have I done wrong here?
I don't want my users typing in the UNC path so is there a way to get the UNC path for them and maybe that will work when I try to access it? This is possible in Microsoft languages but I am not sure how to get PHP to do this - maybe using a cmd.exe command?
Please note, the mapped drive does exist as I can see it and I can access it. It also does not appear to be a permissions problem as I am assuming it would of complained about this IF it could access that drive...right?
Thanks all for any help

The User Contributed Notes to opendir() have this:
I was trying to access network drives using this opendir function. I read so many posts saying that it was almost impossible to access a network drive and finally, I found the answer; there are 2 steps to be followed to access a network drive with PHP either on the same machine or another machine.
in this case, the user rights seemed indeed to be the problem, which can be different from your rights depending on what user PHP / the web server are running on.

Open the Services MMC by going to Start -> Run (or by using the search box, if using Windows 7) and typing Services.msc and pressing enter.
Find the Apache service. Having never used WAMP, I have no idea what it's been named. Hopefully it starts with the word "Apache" though, and thus shouldn't be too hard to find.
Right-click on the service and select Properties.
Switch to the Log On tab.
Select the radio button next to "This account:" and enter in the credentials for the user account you want to run Apache under. If it's a network/domain account, use the "DOMAIN\user" syntax (or search for the account using the Browse button).
Select OK and restart the Apache service.
Verify that your changes worked by looking for the "httpd.exe" process in Task Manager and checking to see which user name the process is running under.
reference http://board.phpbuilder.com/board/showthread.php?t=10371870&page=2

simple, use: "\\\\server\\dir\\subfolder" as the path. make sure you can access the "server".
works for me.
$arquivo = "\\\\server\\dir\\subfolder\\file.txt";
echo is_file($arquivo); //print 1.

Related

How do I restart IIS (or single site) from a PHP page?

When I was looking at Azure, there was an option to restart IIS from the web-based control panel: according to a friend of mine, the interface is just calling powershell scripts on the backend. My current VPS provider offers the same thing, restarting the web server from a web page.
I've tried
echo Shell_Exec ('powershell.exe -executionpolicy bypass -NoProfile -Command "& {iisreset}"');
but get "Access Denied."
I've tried
echo Shell_Exec ('command.exe appcmd stop site "that_website"');
and it doesn't throw an error, but it doesn't work either.
Here's the goal: occasionally, PHP will go toes up (Fast-CGI process has stopped working) and my phone rings. I'd like to give the client the option to press a button and restart their site. What permissions do I need, and/or what code would do that?
I can get appcmd work when I modify your code to
echo Shell_Exec ('appcmd.exe stop site "website"');
If you want to do something like iireset with powershell in PHP, just try to change IIS manager->site node->basic settings->connect as-> specific user-> try to replace it to a domain administrator account.
But it is not recommended to do this operation in a PHP application.
When you are facing access denied error, try to use process monitor to troubleshooting this kind of issue.
https://learn.microsoft.com/en-us/sysinternals/downloads/procmon

PHP exec() command not working

After hours and hours of trying and searching for an answer i coudn't get it working.
Hosting details:
Windows Server 2012 R2
Plesk 12
IIS 8.5
The problem
My problem is:
I want to execute this command in PHP. The command is necessary to reload the configs for filezilla, after creating a new user with an PHP script.
C:\Program Files (x86)\FileZilla Server\FileZilla Server.exe /reload-config
It does work when i try it directly in CMD.
But in PHP if doens't somehow work. I tried so many things, that im going to try to give you all the manners i tried.
I tried to do this at the following ways:
Trial 1
$command = '"C:\\Program Files (x86)\\FileZilla Server\\FileZilla Server.exe" /reload-config';
exec( $command, $output, $return);
print_r($output);
echo '<br/>' .$return;
Output:
Array ( )
0
Trial 2
$command = '"C:\\Program Files (x86)\\FileZilla Server\\FileZilla Server.exe" /reload-config';
system( $command, $output);
print_r($output);
Output: 0
Trial 3
$command = 'C:\\"Program Files (x86)\\FileZilla Server\\FileZilla Server.exe" /reload-config';
exec( $command, $output, $return);
print_r($output);
echo '<br/>' .$return;
Output: 0
Trial 4 (The only one that returns an error!)
exec( 'C:\\"Program Files (x86)\\FileZilla Server\\FileZilla Server.exe" /stop 2>&1', $output);
print_r($output);
Output:
Array ( [0] => Toegang geweigerd. )
In english:
Array ( [0] => Access denied. )
Privileges:
In allmost all awnsers found they talked about privileges. I did following privileges:
Gave all web users and IUSR privileges to CMD.exe and FileZilla Server.exe,
and tried to set the IIS authentication from anonymous to an Administrator account
Somebody said to try this:
echo '<pre>';
system('set', $retval);
echo ' </pre>';
If i do that my output is: http://pastebin.com/SjzeQpJ0
If you guys want anymore details just ask. I hope you guys can figure it out for me. I tried to be as clear as possible. Thanks.
Finaly found the solution!!
If you encounter the same problem as i did, and you dont know how to fix this, follow this steps.
Login to the Remote of your server (if you have access to it) Open
Open the IIS manager
Select -> Application Pools node underneath the machine node (left panel)
Right click on the desired domainname -> Advanced settings
Scroll to Process Model -> Id
Click on the 3 dots
Check the Custom Account radio button
Fill in an existing administration account which has access to the
remote desktop and of course its password
Press OK
Now go to the .exe or whatever file you want to access throught the
exec command and
Right click on the file and select properties
Go to the security tab
Select Edit
Now grand full rights to the account you selected before. If its
not listed than add it manualy by clicking on Add
If you have done all of this, it now should work.
Finaly found the solution!!
If you encounter the same problem as i did, and you dont know how to fix this, follow this steps.
Login to the Remote of your server (if you have access to it)
Open the IIS manager
Select -> Application Pools node underneath the machine node (left panel)
Right click on the desired domainname -> Advanced settings
Scroll to Process Model -> Identity
Click on the 3 dots
Check the Custom Account radio button
Fill in an existing administration account which has access to the
remote desktop and of course its password
Press OK
Now go to the .exe or whatever file you want to access throught the
exec command and
Right click on the file and select properties
Go to the security tab
Select Edit
Now grand full rights to the account you selected before. If its
not listed than add it manualy by clicking on Add
If you have done all of this, it now should work.
There is a much safer way to do this I figured out by using the above and exploring some. All you need to do is enable "Load User Profile" and it will then have all the normal user permissions assigned by a regular user. Just got it working like this instead of giving administrative access:
Login to the Remote of your server (if you have access to it)
Open the IIS manager
Select -> Application Pools node underneath the machine node (left panel)
Right click on the desired domainname -> Advanced settings
Scroll to Process Model -> Id
Set Load User Profile = True
and working! specifically fixed Windows 2012 R2
Go to IIS
Click on Application Pool
Select the application pool -> Advance Settings
Under Popup -> Process Model -> Set Load User Profile to TRUE
And -> Process Model -> Identity -> Local System
This will act as a current user
Note: If current has high privileges over system, This will be high risk.

Trouble with custom PHP extension

A couple of months ago, I built a serial connection library in C for the Arduino Micro-controller. I was thinking about how awesome it would be if I could write a PHP wrapper for the library.
I wrote a PHP extension with just one function called "acm_get_door()," which should either return a -1, 0, or 1, depending upon the status of a certain pin on my Arduino. If it returns -1, then that means that the connection wasn't set.
So after loading up the custom extension and changing both of my php.ini files, and restarting everything else, I ran this test via the CLI:
$ php -r 'echo acm_get_door();'
It returned a '1'. I was excited, so I played with the circuitry to see if I could get a '0'. Well I did and I thought my extension was working perfectly.
I then went to a .php file in my test webserver (same computer) and tried to run the function. I got a '-1' result from it. The extension does load, but I can't seem to initiate a connection to the Arduino.
I am wondering if it might be a permissions thing regarding access to certain files. The function needs to get access to a file called "/dev/ttyACM0" which it is able to in the CLI version. It is possible that my Apache web server doesn't have the permissions to access that file, and if so, how would I be able to adjust stuff so that it does?
Thank you for your time.
Probably the group has permissions to read/write the device.
Therefore, find out the group owner of the device using ls -l /dev/tty/ACM0. Then, add yourself to the group using:
sudo adduser <me> <the_group>
(where me is your username and the_group is the group of the device)
You might need to log out/in to gain the effect of the changed group permissions.
Okay, thanks to John Jesus, I just had to change the permissions of the file /dev/ttyACM0.
It was set to 660, 666 seems to work (though it it probably a bad idea).

Changing ip address of apache server using php

I am trying to create a settings page(for the clients) where in they can view the current up address,change the ip address etc. I have a php file to view the ip address
<?php
$res=shell_exec("ifconfig");
echo $res;
?>
This code works just fine and displays the expected result.
However the code to change the ip address of the server is not working properly.
<?php
shell_exec("ifconfig eth0 192.168.163.136");
?>
After running this code when i check the ipaddress on the terminal using ipaddr i don't see any change in the ipaddress.
Pls point out where i am going wrong. I think its a problem of apache not being a super/root user. If that is the case i don't know how to make apache run as a root user.
Your PHP script doesn't have enough privileges to change the interface address. You may want to write a Shellscript, give it the right privileges (e.g., change its owner to root, and set the suid bit), then run it from your PHP script. Yet, I advise against doing such thing
IP address are configured in Network Layer of an Network Protocol, not in application layer where PHP runs. Simply, PHP does not have access to it and cannot changed them.
Just imagine the vulnerabilities it could create if this was possible.
Adding /srv/http before ifconfig worked. All it needed was the root owned location.

PHP's shell_exec won't allow "Watir::Browser.new :firefox" to execute

I'm brand new to ruby and Watir, here's my issue...
I have a MySQL DB with test data that I need. I've done a lot in the past with this data and so I have a whole library of PHP tools for accessing this data, marking data as in use/used/bad/etc, and in general I have a lot of time invested in the PHP framework. So I'd really like to use the PHP framework as a wrapper around the Watir script - for example, use PHP to grab test user login data from the DB and pass it to the ruby script for processing.
I now have sites with javascript that need work/testing and PHP & cURL can't deal with this. So I'm working with Watir-WebDriver on Ubuntu 10.10 (Maverick, Desktop not Server) for these sites. The problem I'm having is with the use of PHP's shell_exec of the ruby script with all the Watir code.
The PHP shell_exec is executing the file - I can see it because I have some puts lines in the file which are being displayed. However, the code appears to be failing on the line
ff = Watir::Browser.new :firefox
I'm not getting an error from PHP.
The PHP line is:
echo shell_exec('ruby /var/www/watir_test.rb');
The ruby script works fine when I call it from a terminal window with the line:
ruby /var/www/watir_test.rb
I originally expected this was a permissions issue since it worked from the command line but not from a browser. However, since it can call the file well enough to return the hardcoded data I've provided for the test then ruby file permissions don't seem to be the issue. Could there be a permissions issue with opening a Firefox window from the www-data user?
When I run
ruby -d -v /var/www/watir_test.rb
I get:
{:extension=>:webdriver} {"app.update.enabled"=>"false"} {"browser.link.open_newwindow"=>"2"} {"browser.shell.checkDefaultBrowser"=>"false"} {"extensions.update.enabled"=>"false"} {"security.warn_entering_secure.show_once"=>"false"} {"webdriver_assume_untrusted_issuer"=>true} {"startup.homepage_welcome_url"=>"\"about:blank\""} {"browser.tabs.warnOnClose"=>"false"} {"extensions.update.notifyUser"=>"false"} {"toolkit.networkmanager.disable"=>"true"} {"security.warn_entering_weak.show_once"=>"false"} {"webdriver_firefox_port"=>"7055"} {"browser.download.manager.showWhenStarting"=>"false"} {"extensions.logging.enabled"=>"true"} {"network.manage-offline-status"=>"false"} {"network.http.max-connections-per-server"=>"10"} {"security.warn_submit_insecure"=>"false"} {"security.warn_entering_weak"=>"false"} {"security.warn_leaving_secure"=>"false"} {"prompts.tab_modal.enabled"=>"false"} {"security.warn_viewing_mixed.show_once"=>"false"} {"dom.max_script_run_time"=>"30"} {"webdriver_accept_untrusted_certs"=>true} {"browser.safebrowsing.enabled"=>"false"} {"security.warn_leaving_secure.show_once"=>"false"} {"signon.rememberSignons"=>"false"} {"javascript.options.showInConsole"=>"true"} {"app.update.auto"=>"false"} {"browser.EULA.3.accepted"=>"true"} {"browser.tabs.warnOnOpen"=>"false"} {"dom.disable_open_during_load"=>"false"} {"network.http.phishy-userpass-length"=>"255"} {"security.warn_entering_secure"=>"false"} {"browser.startup.homepage"=>"\"about:blank\""} {"browser.EULA.override"=>"true"} {"browser.dom.window.dump.enabled"=>"true"} {"browser.startup.page"=>"0"} {"browser.link.open_external"=>"2"} {"browser.search.update"=>"false"} {"browser.sessionstore.resume_from_crash"=>"false"} {"security.warn_viewing_mixed"=>"false"} {"dom.report_all_js_exceptions"=>"true"} {"webdriver_enable_native_events"=>false} {"devtools.errorconsole.enabled"=>"true"}
How do I get PHP to execute the shell_exec properly? The script works and my initial tests were run using firewatir (which shell_exec ran fine) but I am really wanting to use Watir-WebDriver instead of FireWatir - WatirWebDriver should be capable of running a Chrome browser (and IE on a Windows machine) while FireWatir can only run Firefox.
Thanks
Gabe
Here's my "Create Browser" code:
# Include the RubyGems file
require 'rubygems'
# Include the Watir-WebDriver file.
require 'watir-webdriver'
# Create the necessary objects
def create_browser(proxy)
# Setup the proper Firefox Profile
profile = Selenium::WebDriver::Firefox::Profile.new
profile.proxy = Selenium::WebDriver::Proxy.new :http => proxy
puts "<br>Using proxy " + proxy + "..."
#ff=FireWatir::Firefox.new :profile => profile
ff = Watir::Browser.new :firefox #, :profile => profile
puts "<br>Firefox ready..."
return ff
end
If the server is headless, you should install the headless gem so that Firefox can work.
require 'watir-webdriver'
require 'headless'
headless = Headless.new
headless.start
b = Watir::Browser.start 'www.google.com'
puts b.title
b.close
headless.destroy
See: http://watirwebdriver.com/headless/
It't a permission problem, the PHP script runs with the permissions of the server, normally apache.
You can do a sudo www and try to run the script then with rb to see if there is a problem when running ruby with the server user.
Put the two lines of code that is below at the very top of your PHP script. The result of this is that when you browse to your PHP page with your browser it will display exactly what the errors are, including any permission errors.
ini_set("display_errors",1);
error_reporting(E_ALL);
So a recent update to Firefox killed it's support for Watir (no JSSH update if I remember). As a result I rewrote what little code I had for Selenium::WebDriver. But I'm thinking that isn't particularly relevant (its included in case it is relevant I don't know it).
My ultimate solution was to use phpseclib. This allows me to SSH into the machine via their SSH2 PHP class. Once logged in as my typical username (with typical password) I was able to fire off a headless version of my script no problem. The only real issues this creates is that I now have to view everything that's going on through log files and screenshots but that was likely to be true no matter what solution I came up with.
phpseclib needs your username and password for the server (at least until you set some form of public private key pair). So I wouldn't want to do this on a publicly available machine without a couple of layers of security - like setting .htaccess to deny read access to the file with the login data, encrypting the password stored in the file, etc. However, for my purposes I'm logging into one machine on my LAN from another machine on my LAN. The password is only good on my LAN (not my web servers) and while my LAN can see out it should not be (easily) available to the rest of the world (to the best of my knowledge). So the security concerns are minimal.
I never did figure this out. Headless isn't the answer to getting PHP to exec the script. I'm pretty sure it is a permissions issue with Firefox's executable but I can't be positive until I find an actual fix.
Ultimately I've had to break up the tool where PHP manages the DB and task scheduling. Then PHP creates text files with all the data necessary for ruby to run the browser to right site, login, etc, etc... Then ruby moves the data file to one of a few different folders (success, failure, bad login, etc) and adds some text to the data file. Finally PHP parses all this info in the moved text files and updates the DB with that info.
It's less than ideal but it is getting the job done. Now I just need to figure out how to run all of this with mutliple threads...
Thanks for the help

Categories