PHP exec is blocked by ISP - php

I am trying to implement the following chat-html5 from git hub:
https://github.com/ivanph/Chat-HTML5
I have uploaded everything to my ISP but I have found that the ISP blocks exec for security reasons.
The file I am calling is :
<?php
/**
* Main Script of phpWebSockets
*
* Run this file in a shell or windows cmd to start the socket server.
* Sorry for calling this daemon but the goal is that this server run
* as daemon in near future.
*
* #author Moritz Wutz <moritzwutz#gmail.com>
* #version 0.1
* #package phpWebSockets
*/
ob_implicit_flush(true);
require 'socket.class.php';
require 'socketWebSocket.class.php';
require 'socketWebSocketTrigger.class.php';
$ip = exec ("ifconfig|grep 'inet:'|grep -v '127.0.0.1' |cut -d: -f2 |awk '{ print $1}'");
$WebSocket = new socketWebSocket($ip,8000);
?>
Is there an alternative way to do this ? Do all Isp's block this ?
What can I do?
Hi Guys
thanks for the responses.
I got a response from the script developer.
I have changed the $ip to the ip address of our web site. I now get the following error:
--2013-08-13 12:07:01-- http://www.wilsea.com/websockets2/startDaemon.php
Resolving www.wilsea.com... 188.64.188.21
Connecting to www.wilsea.com|188.64.188.21|:80... connected.
HTTP request sent, awaiting response... 500 Internal Server Error
2013-08-13 12:07:12 ERROR 500: Internal Server Error.
The developer says this means that the port (8000) is in use so I tried 80 - 443 - 8080 but got the same error.
I have emailed the ISP and asked if websockets are blocked or if I need a port opening.
Anyone else had this issue or any insights into this problem?

Is there an alternative way to do this ?
There also are system and shell_exec, but I'd assume this to be disabled for the same reasons.
Like pointed out in the comments, you could also just provide your server's static IP instead of having the script determine it. However, chances are sockets are disabled as well, so don't put your hopes too high.
Do all Isp's block this ?
Most Hosting Providers do. Obviously a hosting service doesn't want you to run arbitrary commands on the shell that could potentially reconfigure the machine.
What can I do?
Get paid hosting that allows you to exec. A VPS or a dedicated server comes to mind.

I could try to find a way to detect a good listening address in PHP to avoid this monstrosity. (The workaround would probably be $ip = '0';) Your next question would be: Fatal error: Call to undefined function socket_create() why is my ISP so mean?. exec() is blocked for good reasons on a shared hosting and you won't be allowed to create a socket with create_socket().
No hoster will want something like this on their shared server. You'll have to get your own system but then you wouldn't want this code to run on it. This is because you don't want to run this code anywhere unless you were trying to improve on it. But then you'd be improving on a solution which builds on the sand that is PHP. And that would be sad.
Get a cheap virtual server if you really want to use this. I don't recommend it. Looks like somebody is trying to use PHP for a task it's not designed to do. If you've gotten this far without understanding the issues involved I recommend you to stay far away from it. Try it on localhost if you must, so you're not harming other people.

The issue was the ISP blocks all port. They have now opened the port for me.
Cheers
SteveW

Related

How to connect to ESL from a remote server?

I would like to make a web interface in PHP to see the FreeSWITCH activities (calls, etc), possibly hosted on a different server than the one where FS is running.
I've seen the server status on the FS server using command line (php single_command.php status), but now I would like to see this status from another server.
When I try to copy ESL.php file to this remote server and try to check the status, I get this error message:
Fatal error: Call to undefined function new_ESLconnection() in
/var/www/freeswitch/ESL.php on line 127
This is my index.php file:
<?php
ini_set('display_errors', 1);
$password = "ClueCon";
$port = "8021";
$host = "192.168.2.12";
require_once('ESL.php');
set_time_limit(0); // Remove the PHP time limit of 30 seconds for completion due to loop watching events
// Connect to FreeSWITCH
$sock = new ESLconnection($host, $port, $password);
// We want all Events (probably will want to change this depending on your needs)
$sock->sendRecv("status");
// Grab Events until process is killed
while($sock->connected()){
$event = $sock->recvEvent();
print_r($event->serialize());
}
?>
I undestand that the webserver doesn't have FreeSWITCH installed, so the error message is obvious, but i don't see how to access to this information from this webserver.
Thank you for your help.
Depending upon your need you can use either Inbound or Outbound socket. I do not know much about PHP and FS Event Socket but yeah tried enough with python. I highly recommended to go through thislink.
So if you just want to do small task like initiating a call, bridging any two given number etc i think you should go with Inbound socket(making cli command from your web server to freeswitch server) or mod_xml_rpc.
And if you want to have full control of everything that happens on FS server like showing live call status and modifying their states or say a complete interactive telephony dashboard then you should go with Outbound socket.(Your FS server will send all events to your web server.)
However in your case problem is I think you did not properly build the php ESL module.
this link might help you installing ESL
Rather than using ESL, you might want to consider using the XMLRPC. The connection is very straight forward:
https://wiki.freeswitch.org/wiki/Freeswitch_XML-RPC
The credentials for the XMLRPC are in your autoloads_configs/xml_rpc.conf.xml

Check if PHP is running in local server

Well. I read some topics in SO but I not found a very specific answer.
I need to check with PHP if a PHP code is running in local or remote host. Currently I check with $_SERVER['SERVER_NAME'] but it is inconsistent. In this case, if I run PHP with listed IPs like 127.0.0.1 or localhost it'll consider local, otherwise remote. If I share my IP with a friend, my code still local, but it consider remote because the shared IP isn't listed.
Well, I think that check IP for localhost is not a good idea (except if you know a good method). I tried methods like gethostbyaddr() and gethostbyname() but don't work correctly too.
I don't have a PHP code to show, but my code is basically that:
// true = localhost
return $_SERVER['SERVER_NAME'] === '127.0.0.1';
The fundamental question is: what can determine that PHP is running local? What is "local" for PHP? I think that it can solve the problem.
Obs.: I don't have access to CMD/Shell with PHP.
You could do what most PHP frameworks do and set a flag during your app's bootstrap phase that defines which environment the code is running in. In it's simplest form:
// the setting when run on a dev machine
define('ENV', 'local');
Then it's a simple case of:
if ( ENV == 'local' )
{
// do stuff
}
This is how I do it, which I find more reliable than trying to detect for 127.0.0.1:
if( strpos(gethostname(), '.local') !== false ) { }
Basically, the hostname's on my workstations all have .local appended to it. You can change this to match your workstation's hostname entirely.
Check $_SERVER['REMOTE_ADDR']=='127.0.0.1'. This will only be true if running locally. Be aware that this means local to the server as well. So if you have any scripts running on the server which make requests to your PHP pages, they will satisfy this condition too.
If someone is visiting your site via the web, the IP address you see will never be 127.0.0.1 (or ::1 for IPV6), regardless of the usage of a proxy. (Unless of course you're running the proxy yourself on the same server ;)
As far as I know, only you will be able to know what addresses are local or not. Your network could be set up with IP addresses that don't look local at all. PHP cannot as far as I know determine this by itself.

Blank page when using file_get_contents() on a website with port 8080

When I run the following code on one of my websites:
<?php
$thing = file_get_contents("http://mywebsite.com:8080/Public");
echo($thing);
?>
It returns expected result, the contents of http://mywebsite.com:8080/Public
But when I run it on my other website (hosted on by a different company), it does not display anything. No errors and not the contents of http://mywebsite:8080/Public. However, if I run the following code:
<?php
$thing = file_get_contents("http://somerandomwebsite.com");
echo($thing);
?>
It returns the contents of somerandomwebsite.com. Is there a reason why it works on one of the websites and not the other? Why can it only fetch the contents of the file if the port is 80?
Check with your hosting provider if it allows PHP to make external requests. Some shared hosting providers disable that.
file_get_contents fails because it cannot retrieve the content at hand. Most likely, a simplistic firewall is blocking all traffic to non-80 ports. You'll have to use port 80 to avoid these simplistic firewalls.
The failure to download the resource makes php emit a warning. Most likely, warnings are not displayed on the production server. Check the server's log and the display_errors and error_reporting configurations.

Check if host computer is online with PHP?

I've been having some issues with my Internet connection and I was wondering what is the fastest, error-less and most reliable way to check if the host computer is connected to the Internet.
I'm looking for something like is_online() that returns true when online and false when not.
I've benchmarked some solutions: file_get_contents with HEAD request, gethostbynamel, checkdnsrr and the following solution seems to be more than 100 faster than all the others:
function is_online()
{
return (checkdnsrr('google.com', 'ANY') && checkdnsrr('yahoo.com', 'ANY') && checkdnsrr('microsoft.com', 'ANY'));
}
Takes about one microsecond per each host, while file_get_contents for instance takes more than one second per each host (when offline).
You could send a ping to a host that is probably up (e.g. Google).
There seems to be no PHP built-in for this, so you'd have to resort to shell commands. The return value of ping on *nix can tell you whether a reply was received.
Update: ping -c1 -q -w1 should be the right command on Linux. This will give you exit code 0 if a reply was received, something else otherwise, and it times out after one second.
Hence, something like this (warning, my PHP is rusty) should do the trick:
function is_online() {
$retval = 0;
system("ping -c1 -q -w1", $retval);
return $retval == 0;
}
Why don't you do a number of HTTP GET (or better still HTTP HEAD for speed) requests on popular web sites? Use majority voting to decide on the answer.
You can sometimes rely on ping too (through a system call in PHP) but note that not all web sites respond to ICMP (ping) requests.
Note that by increasing the number of ping/http requests you make before drawing a conclusion helps with the confidence level of the answer but can't be error free in the worst of cases.
Don't forget this assumes that your server will respond to ICMP requests. If that's the case then I agree, Net_Ping is probably the way to go. Failing that you could use the Net_Socket package, also on PEAR, to attempt a connection to some port that you know will get a response from - perhaps port 7 or port 80 depending on what services you have running.

php access network path under windows

within PHP (XAMPP) installed on a Windows XP Computer Im trying to read a dir which exists on a local network server. Im using is_dir() to check whether it is a dir that I can read.
In Windows Explorer I type \\\server\dir and that dir is being shown.
When I map a network drive a can access it with z:\dir as well.
In PHP I have that script:
<?php if( is_dir($dir){ echo 'success' } ) ?>
For $dir I tried:
/server/dir
//server/dir
\server\dir
\\server\dir
\\\\server\\dir
and
z:\dir
z:\\dir
z:/dir
z://dir
But I never get success?
Any idea?
thx
I solved it by changing some stuff in the registry of the server as explained in the last answer of this discussion:
http://bugs.php.net/bug.php?id=25805
Thanks to VolkerK and Gumbo anyway!
I love stackoverflow and their great people who help you so incredibly fast!!
EDIT (taken from php.net):
The service has limited access to network resources, such as shares
and pipes, because it has no credentials and must connect using a null
session. The following registry key contains the NullSessionPipes and
NullSessionShares values, which are used to specify the pipes and
shares to which null sessions may connect:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters
Alternatively, you could add the REG_DWORD value
RestrictNullSessAccess to the key and set it to 0 to allow all null
sessions to access all pipes and shares created on that machine.`
add RestrictNullSessAccess=0 to your registery.
You probably let xampp install apache as service and run the php scripts trough this apache. And the apache service (running as localsystem) is not allowed to access the network the way your user account is.
A service that runs in the context of the LocalSystem account inherits the security context of the SCM. The user SID is created from the SECURITY_LOCAL_SYSTEM_RID value. The account is not associated with any logged-on user account.
This has several implications:
...
* The service presents the computer's credentials to remote servers.
...
You can test this by starting the apache as console application (apache_start.bat in the xampp directory should do that) and run the script again. You can use both forward and backward slashes in the unc path. I'd suggest using //server/share since php doesn't care about / in string literals.
<?php
$uncpath = '//server/dir';
$dh = opendir($uncpath);
echo "<pre>\n";
var_dump($dh, error_get_last());
echo "\n</pre>";
Try the file: URI scheme:
file://server/dir
file:///Z:/dir
The begin is always file://. The next path segment is the server. If it’s on your local machine, leave it blank (see second example). See also File URIs in Windows.
Yes, I know this is an old post, but I still found it, and if anyone else does...
On Windows, with newer servers, verify the SMB is installed and enabled on the target machine.

Categories