TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

Members
  • Total Members: 3,963
  • Latest: BiZaJe
Stats
  • Total Posts: 195,917
  • Total Topics: 21,308
  • Online today: 884
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online
  • Users: 0
  • Guests: 331
  • Total: 331

PHP code in a block - works fine on a page, but not in the block??

Started by Discovery UK, May 24, 2009, 08:40:28 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Discovery UK

Well - basically what the title says. I have been creating a server status script for a friend, and its taken a while - but I've finally got there.

The script works perfectly here: http://stats.discoveryfluk.com/server/

But, when I add it to a php block in TinyPortal - it doesn't work??? This is the script:

include("admin.class.php");

$ip = "********************";
$port = "****";
$pass = "********";
$flhookHeader = "Welcome to FLHack, please authenticate";

function display($page)
{
global $flhook, $theme, $lang;

switch ($page)
{
case "plyrList":
$theme->displayPlyrList($players);
break;
}
}
function genServerInfo()
{
global $flhook;
$serverInfo = $flhook->processCmd("serverinfo");

$serverInfo = explode(" ",$serverInfo);
$serverInfo[0] = str_replace("serverload=","",$serverInfo[0]);
$serverInfo[1] = str_replace("npcspawn=","",$serverInfo[1]);
$serverInfo[2] = str_replace("uptime=","",$serverInfo[2]);
return $serverInfo;
}

class theme
{
function header()
{
echo "<head><style type='text/css'>body{
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:10px;
color:606060;
}

.tblHeader{
color:000000;
font-weight:bold;
}
.header{
color:000000;
font-weight:bold;
font-size:12px;
width:100%;
}</style></head>";
}
function displayPlyrList($players)
{
$serverInfo = genServerInfo();

echo "<div align='center'>";
echo "<font class='tblHeader'>Serverload:</font> " . $serverInfo[0];
echo "<br><font class='tblHeader'>NPC Spawn:</font> " . $serverInfo[1];
echo "<br><font class='tblHeader'>Uptime:</font> " . $serverInfo[2];
echo "</div>";
}
}

$theme = new theme();
$theme->header();

$action = $_GET['action'];

$flhook = new admin();
$flhook->connect($ip, $port);
if ( $flhook->con )
{
$flhook->login($pass);
if ( $flhook->loggedIn )
{
switch ($action)
{
default:
display("plyrList");
break;
}
}
else
{
echo "Error: " . $flhook->error;
}
}
else
{
echo "<div align='center'>Server is offline.</div>";
}


I have removed the <?php ?> tags because that causes a problem with other blocks, and it's just the same with this one. Include them and it comes back with an error in Sources/Load.php

At the moment, I receive:

QuoteError: Welcome to FLHack, please authenticate OK

Which is an echo of:

$flhookHeader = "Welcome to FLHack, please authenticate";

Followed by OK to show that the user has been authenticated.

I think the issue might be here though:

<?php
class 
admin
{
var $loggedIn false;
var $con;
var $timeout 1000;
var $errn;
var $errs;
var $error;
var $ok "OK";
var $err "ERR";
var $cmds = array(
  //array([number of parameters],[returns data])
  //Commands with 1 parameter
  "getplayers" => array(0,1),
  "serverinfo" => array(0,1));


function admin()
{
}

function connect($ip$port)
{

$this->con fsockopen($ip,$port,$this->errn,$this->errs,$this->timeout);
}

function quit()
{
fclose($this->con);
}
function login($pass)
{
$check $this->sendCmd("pass"$pass);
if ( $check == $this->ok)
{
$this->loggedIn true;
}
else
{
$this->loggedIn false;
$this->error $check;
}
}
function sendCmd($cmd$param="")
{
fwrite($this->con$cmd " " .$param."\r\n");
while(1)
{
$output .= fgetc($this->con);
//\n and space used incase a player is named OK or ERR
if ( ( trim(substr($output,-3,) == "\nOK") ) || ( trim(substr($output,-4,) == "ERR ") ))
break;
else
continue;
}
return trim($this->cleanOutput($output));

}
function processCmd($cmd$params="")
{
switch ($this->cmds[$cmd][0])
{
case 0:
$params str_replace(" ","",$params);
$output $this->cleanOutput($this->sendCmd($cmd$params));
break;

case 1:
$params str_replace(",","",$params);
$params preg_replace("/[a-zA-Z]/","",$params);
$output $this->cleanOutput($this->sendCmd($cmd$params));
break;

case 2:
$output $this->cleanOutput($this->sendCmd($cmd$params));
break;
}

switch ($this->cmds[$cmd][1])
{
//No return needed, other than OK
case 0:
if ( trim(substr($output,-2,) == "OK") )
return true;
else
$this->error trim($output);
return false;
break;
//Data needs to be returned
case 1:

if ( trim(substr($output,-2,) == "OK") )
return substr($output,0,(strlen$output ) - 2));
else
$this->error trim($output);
return false;
break;
}
}
function cleanOutput($output)
{
global $flhookHeader;
//Removes flhook welcome message, so we only have the data returned by the server after sending a command.
$remove = array("/".$flhookHeader."/");
$output preg_replace($remove,"",$output);
return trim($output);
}

}
?>


I edited out the $ip, $port and $pass for security purposes.

Any ideas anyone, this has really gotten to me? Thanks.

JPDeni

The issue is likely the path to the included file. Blocks use the Sources/ directory as their base directory and you need to either use a path relative to that directory or put in the full path.

Discovery UK

Nope - makes no difference. Tried putting one in the root and sources directory - but absolutely nothing.

Any other ideas? Thanks for trying though.

JPDeni

Well, it was a thought. :)

I don't understand that whole php class thing. I've looked at other scripts with a similar structure and they don't make any sense to me.

Ianedres

Use a relative path to the admin.class.php file in the include statement, as it is looking for the file from wherever your forum exists in the directory tree.

Try '../forumdirectoryname/Sources/class.admin.php' for the include and substitute the directory you are running your SMF site in. Make sure you use the same case in the filename also...  no uppers or lowers bouncing around in the directory.


Discovery UK

Nope - nothing. I even put the contents of admin.class.php where the include statement was, to see if it would work directly.

It doesn't. If I use a relative or fixed path to admin.class.php in the sources directory, it just says admin() was not found in Sources/Load.php??

IchBin

What about a full path? Right from the root of the server. /home/username/www/path/to/file ?

You do some checks to see if things are working? You can always check to see if the file_exists() or class_exists() before you try and execute the code.

Discovery UK


This website is proudly hosted on Crocweb Cloud Website Hosting.