Page 17 of 76

Re: LUA Pass2php

Posted: Monday 30 January 2017 10:13
by dutchdevil83
Egregius wrote:OK that's good. Remove the lg line again, or place // before it to place it as a comment, could be usefull later.
Then in pass2php.php add this:

Code: Select all

<?php
lg($device.' = '.$status)
Then we know that the path and filename is good.
this one i don`t understand? This is the same line you ask me to comment out in pass2php.php?

Re: LUA Pass2php

Posted: Monday 30 January 2017 10:29
by Egregius
Yes, but now it's placed inside pass2php/TEST.php
We need to check step by step where the script stops by adding lg commands.

Edit: oh, sorry, forgot to change the filename in my previous post.

Re: LUA Pass2php

Posted: Monday 30 January 2017 10:35
by dutchdevil83
Egregius wrote:Yes, but now it's placed inside pass2php/TEST.php
We need to check step by step where the script stops by adding lg commands.

Edit: oh, sorry, forgot to change the filename in my previous post.
Ok now i understand... you said to put this line pass2php.php. If i put this line in pass2php/TEST.php nothing happens in the log when i press the switch:

Code: Select all

2017-01-30 10:31:57.457 User: Admin initiated a switch command (563/TEST/On)
2017-01-30 10:31:57.457 (Dummy) Light/Switch (TEST)

Re: LUA Pass2php

Posted: Monday 30 January 2017 10:40
by Egregius
Wait a minute, where did you get this part?

Code: Select all

if(@include 'http://127.0.0.1/secure/pass2php/'.$device.'.php'){ 
That should be a path, not a url:

Code: Select all

if(@include '/volume1/web/secure/pass2php/'.$device.'.php'){ 

Re: LUA Pass2php

Posted: Monday 30 January 2017 10:47
by dutchdevil83
Egregius wrote:Wait a minute, where did you get this part?

Code: Select all

if(@include 'http://127.0.0.1/secure/pass2php/'.$device.'.php'){
That should be a path, not a url:

Code: Select all

if(@include '/volume1/web/secure/pass2php/'.$device.'.php'){
Ok edited pass2php.php with following information but still don`t get to see anything in log:

Code: Select all

<?php error_reporting(E_ALL);ini_set("display_errors","on");date_default_timezone_set('Europe/Amsterdam');
define('time',$_SERVER['REQUEST_TIME']);$actions=0;
$c=ex($_REQUEST['c']);
//lg('4. Egregius Start of Pass2PHP');
foreach($c as $device=>$status){
 // lg($device.' = '.$status);
	if(@include '/var/www/html/secure/pass2php/'.$device.'.php'){
		//Filter the 'set level' stuff away for dimmers
		if(in_array($device,array('Verlichting Slaapkamer'))){
			if($status=='Off')apcu_store('s'.$device,'Off');
			else apcu_store('s'.$device,filter_var($status,FILTER_SANITIZE_NUMBER_INT));
		}else apcu_store('s'.$device,$status);
		apcu_store('t'.$device,time);
		$dev=$device;
		}
}
if(!isset($dev))die();
include '/var/www/html/secure/pass2php/__CRON.php';
function sw($idx,$action='',$info=''){
	lg('SWITCH '.$idx.' '.$action.' '.$info);
	if(empty($action))file_get_contents('http://127.0.0.1:8080/json.htm?type=command&param=switchlight&idx='.$idx.'&switchcmd=Toggle');
	else file_get_contents('http://127.0.0.1:8080/json.htm?type=command&param=switchlight&idx='.$idx.'&switchcmd='.$action);
	global $actions;$actions=$actions+1;
}

Re: LUA Pass2php

Posted: Monday 30 January 2017 10:50
by Egregius
Permissions are good on the folder/file so the user that runs the webserver can read them?

Re: LUA Pass2php

Posted: Monday 30 January 2017 11:08
by dutchdevil83
Egregius wrote:Permissions are good on the folder/file so the user that runs the webserver can read them?
yep all users have read acces to the map

Re: LUA Pass2php

Posted: Monday 30 January 2017 11:23
by Egregius
What is the output of this command in bash?

Code: Select all

cat /var/www/html/secure/pass2php/TEST.php

Re: LUA Pass2php

Posted: Monday 30 January 2017 11:37
by dutchdevil83
Egregius wrote:What is the output of this command in bash?

Code: Select all

cat /var/www/html/secure/pass2php/TEST.php
found the error i think.... when running as user pi i get a acces denied error. How can i change apache2 settings to run command as root without losing security....

or maybe its better to move the map to a new dir under /home/pi/ ?

EDIT: changed file to another directory which pi user has rights to but still nothings happens when pressing TEST switch.

Code: Select all

<?php error_reporting(E_ALL);ini_set("display_errors","on");date_default_timezone_set('Europe/Amsterdam');
define('time',$_SERVER['REQUEST_TIME']);$actions=0;
$c=ex($_REQUEST['c']);
//lg('4. Egregius Start of Pass2PHP');
foreach($c as $device=>$status){
 // lg($device.' = '.$status);
	if(@include '/home/pi/domoticz/scripts/lua/pass2php/pass2php/'.$device.'.php'){
		//Filter the 'set level' stuff away for dimmers
		if(in_array($device,array('Verlichting Slaapkamer'))){
			if($status=='Off')apcu_store('s'.$device,'Off');
			else apcu_store('s'.$device,filter_var($status,FILTER_SANITIZE_NUMBER_INT));
		}else apcu_store('s'.$device,$status);
		apcu_store('t'.$device,time);
		$dev=$device;
		}
}
output of cat:

Code: Select all

raspberrypi% cat /home/pi/domoticz/scripts/lua/pass2php/pass2php/TEST.php
<?php
lg($device.' = '.$status)
if($s['TEST']=='On')sw($i['Verlichting Dressoir'],'On');
?>%  

Re: LUA Pass2php

Posted: Monday 30 January 2017 11:57
by Egregius
Never run a webserver as root...
You should check under wich user your webserver runs, mostly www-data or similar
That user needs access to the secure/pass2php folder and the TEST.php file

Re: LUA Pass2php

Posted: Monday 30 January 2017 12:38
by dutchdevil83
Egregius wrote:Never run a webserver as root...
You should check under wich user your webserver runs, mostly www-data or similar
That user needs access to the secure/pass2php folder and the TEST.php file
Its running as user and group pi

EDIT: Ok changed everything back to www-data and make www-date the owner of var/www folder. Added pi to the user-group www-data and set the permission to 775.... But still nothings happens when i press the switch button :?: Looks like the TEST.php isn`t being found or something.. is there anyway to log this which call is being made (or isn`t made)

Re: LUA Pass2php

Posted: Monday 30 January 2017 14:25
by Egregius
make a file /var/www/test.php:

Code: Select all

<?php
error_reporting(E_ALL);
ini_set("display_errors","on");
include('/var/www/secure/pass2php/TEST.php');
 
Call the page in your browser http://192.168.x.x/test.php
Output must be something like 'call to undefined function lg...' because that function doesn't exist in that file.
I'm expecting a output like 'file not found', or 'permission denied',...

Re: LUA Pass2php

Posted: Monday 30 January 2017 14:45
by dutchdevil83
Egregius wrote:make a file /var/www/test.php:

Code: Select all

<?php
error_reporting(E_ALL);
ini_set("display_errors","on");
include('/var/www/secure/pass2php/TEST.php');
Call the page in your browser http://192.168.x.x/test.php
Output must be something like 'call to undefined function lg...' because that function doesn't exist in that file.
I'm expecting a output like 'file not found', or 'permission denied',...
You are right.....

Code: Select all

Warning: include(/var/www/html/secure/pass2php/TEST.php): failed to open stream: Permission denied in /var/www/html/test.php on line 4

Warning: include(): Failed opening '/var/www/html/secure/pass2php/TEST.php' for inclusion (include_path='.:/usr/share/php') in /var/www/html/test.php on line 4
so there are still permission issues. Do you have some guide to set the permissions the right way...

Thanx for helping me out so fast! really appreciate it

Re: LUA Pass2php

Posted: Monday 30 January 2017 15:08
by Egregius
If www-data is the user that runs the webserver

sudo chmod 755 /var/www/secure
sudo chmod 755 /var/www/secure/pass2php
cd /var/www/secure
sudo chmod 644 *
cd /var/www/secure/pass2php
sudo chmod 644 *

So, 755 for dirs (read/write/execute for owner, read/execute for others) and 644 for php files (read/write for owner, read for others).

Re: LUA Pass2php

Posted: Monday 30 January 2017 15:30
by dutchdevil83
Egregius wrote:If www-data is the user that runs the webserver

sudo chmod 755 /var/www/secure
sudo chmod 755 /var/www/secure/pass2php
cd /var/www/secure
sudo chmod 644 *
cd /var/www/secure/pass2php
sudo chmod 644 *

So, 755 for dirs (read/write/execute for owner, read/execute for others) and 644 for php files (read/write for owner, read for others).
ok did this all but makes no changes... still getting acces denied. Is this because domoticz is running as pi? or is this all php related?
Or do i have to restart apache or php or something after this changes?

Re: LUA Pass2php

Posted: Monday 30 January 2017 15:46
by Egregius
This is apache related, but very strange.
Are there other files in /var/www
otherwise, chmod everything
sudo chmod -R 777 /var/www
that gives everybode read/write/execute permissions on /var/www and everything below that.

Re: LUA Pass2php

Posted: Monday 30 January 2017 15:49
by Egregius
And you're sure /var/www/html is the root of Apache? Isn't that normally just /var/www?

Re: LUA Pass2php

Posted: Monday 30 January 2017 18:13
by dutchdevil83
Egregius wrote:And you're sure /var/www/html is the root of Apache? Isn't that normally just /var/www?
on raspbian jessie this has changed to /var/www/html if have read somewhere. Now did chmod 777 and now it executed at least its giving me another error instead of acces denied:

Code: Select all

Parse error: syntax error, unexpected 'if' (T_IF) in /var/www/html/secure/pass2php/TEST.php on line 3
EDIT: Have it working now... was missing ";" after log line

now the output in domoticz is this when pressing the switch TEST:

Code: Select all

2017-01-30 18:16:45.317 User: Admin initiated a switch command (563/TEST/On)
2017-01-30 18:16:45.317 (Dummy) Light/Switch (TEST)
2017-01-30 18:16:45.798 => TEST = On
this is my code in TEST.php:

Code: Select all

<?php
lg($device.' = '.$status);
if($s['TEST']=='On')sw($i['Verlichting Dressoir'],'On');
if($s['TEST']=='Off')sw($i['Verlichting Dressoir'],'Off');
?>
I see the log but nothing is happening when i flip the switch.... Should is see a log line when php is trying to sending a command to domoticz?

Re: LUA Pass2php

Posted: Monday 30 January 2017 18:20
by Egregius
Lol, yes, a simple ; can break a whole script.
It should have been in the domoticz log, if you look at it in bash (not in web ui).

Re: LUA Pass2php

Posted: Monday 30 January 2017 18:35
by dutchdevil83
Egregius wrote:Lol, yes, a simple ; can break a whole script.
It should have been in the domoticz log, if you look at it in bash (not in web ui).
what do you mean look into the log in bash? Is there another way to acces domoticz log and does that give more information why my switch is not switching? Maybe i need the code with APCU command in it because all is stored now with APCU_store ?

EDIT: Have something working but it only switch when using the IDX... This code is working for turning my test lights on and off:

Code: Select all

<?php
lg($device.' = '.$status);

if ($status=="On") {
	lg("Switching on..");
   sw(580,'On');
} else {
	lg("Switching off...");
	sw(580,'Off');
}
Result in domoticz:

Code: Select all

2017-01-30 20:21:29.916 User: Admin initiated a switch command (563/TEST/On)
2017-01-30 20:21:30.356 (Dummy) Light/Switch (TEST)
2017-01-30 20:21:30.872 => TEST = On
2017-01-30 20:21:30.874 => Switching on..
2017-01-30 20:21:30.876 => SWITCH 580 On
2017-01-30 20:21:30.877 User: Admin initiated a switch command (580/Verlichting Dressoir/On)
2017-01-30 20:21:30.878 OpenZWave: Domoticz has send a Switch command! NodeID: 43 (0x2b)
2017-01-30 20:21:30.879 (Zwave) Light/Switch (Verlichting Dressoir)
But when i try to use the sw functions this way it doesn`t work :

Code: Select all

 sw($i['Verlichting Dressoir'],'On');
and when using apcu this isn`t working also:

Code: Select all

sw(apcu_fetch('sVerlichting Dressoir'),'On');