TinyPortal

Development => Support => Topic started by: IchBin on March 15, 2005, 02:09:34 AM

Title: phpbox code question
Post by: IchBin on March 15, 2005, 02:09:34 AM
I have a script that has a bunch of phpcode to query a server to get information like what map is playing, and who is on the server currently. At the bottom of the phpcode is the html code for output. How can I get this to work in one of my TPortal blocks? Here's the code if you'd like to see it.
<?php
/*
server.php
This script connects to a Call of Duty server, and puts the server information in a table.
Change the variables below to get it working.
You can also address the script as follows to get your own server information:
server.php?server=xxx.xxx.xxx.xxx:port (ie. server.php?server=137.224.238.228:28960)

Made by Ghostie (Peter Verboom)

Disclaimer:
This file is only ment for use on your website, not for redistribution.
Please refer to my clan (aG, http://www.ag-clan.nl) for credit.
*/

/*
Version history:
V 1.0 : Fetching server info. Simple layout
V 1.01: Layout changed, added pictures for maps.
V 1.02: Added possibility for other servers by server=xxx.xxx.xxx.xxx:port
V 1.1 : Fixed problem with some servers.
V 1.11: Layout changed.
V 1.12: Small player bug fixed, should be working on ALL servers now.
V 1.13: Fixed error when register_globals = off
V 1.14: Colors set in variables
V 2.0: Display image when server is offline
V 2.0.1 Small bug fix for newer PHP versions
V 3.0 Optimized for Call of Duty
*/

// Server ip and port:
if (!isset($_GET['server'])) {

$server_ip = "69.93.65.219";
$server_port = "28960";

} else {

$server = explode(":", $_GET['server']);
$server_ip = $server[0];
$server_port = $server[1];

}

// Set the color variables:
$color1 = "#acacac";
$color2 = "#acacac";
$bgcolor = "#acacac";

// Set the offline image:
$img_offline = "images/offline.jpg";

// Do NOT change anything below this line!

function colors($string) {
$string = str_replace("^0", "<font color=\"black\">", $string);
$string = str_replace("^1", "<font color=\"red\">", $string);
$string = str_replace("^2", "<font color=\"darkgreen\">", $string);
$string = str_replace("^3", "<font color=\"yellow\">", $string);
$string = str_replace("^4", "<font color=\"blue\">", $string);
$string = str_replace("^5", "<font color=\"lightblue\">", $string);
$string = str_replace("^6", "<font color=\"pink\">", $string);
$string = str_replace("^7", "<font color=\"white\">", $string);
return
$string . "</font>";
}

function
stripcolors($string) {
$string = str_replace("^0", "", $string);
$string = str_replace("^1", "", $string);
$string = str_replace("^2", "", $string);
$string = str_replace("^3", "", $string);
$string = str_replace("^4", "", $string);
$string = str_replace("^5", "", $string);
$string = str_replace("^6", "", $string);
$string = str_replace("^7", "", $string);
return
$string;
}

// Creating the URL for the connection:
$server_ip = "udp://" . $server_ip;

// Open the connection:
$connect = fsockopen($server_ip, $server_port, $errno, $errstr, 30);
if (
$connect) {
// Set the timeout:
socket_set_timeout ($connect, 1, 000000);

// Get the information from the server, and put it into the $output array:

$send = "ÃÆ'ƒÂÃ,¿ÃÆ'ƒÂÃ,¿ÃÆ'ƒÂÃ,¿ÃÆ'ƒÂÃ,¿" . chr (0x02) . "getstatus";
fputs($connect, $send);
fwrite ($connect, $send);
$output = fread ($connect, 1);
if (! empty (
$output)) {
  do {
   
$status_pre = socket_get_status ($connect);
   
$output = $output . fread ($connect, 1);
   
$status_post = socket_get_status ($connect);
  } while (
$status_pre['unread_bytes'] != $status_post['unread_bytes']);
};

// Close the connection:
fclose($connect);

// Select the variables from the $output array:
$output = explode ("\\", $output);

$max_index = array_search ("sv_maxclients", $output);
$max_clients = $output[$max_index+1];

$max_index = array_search ("mapname", $output);
$mapname = $output[$max_index+1];

$max_index = array_search ("sv_hostname", $output);
$hostname = $output[$max_index+1];

$max_index = array_search ("g_gametype", $output);
$gametype = $output[$max_index+1];

if (
$gametype == "re") {
$gametype = "Retrieval";
}
if (
$gametype == "dm") {
$gametype = "Deathmatch";
}
if (
$gametype == "sd") {
$gametype = "Search and Destroy";
}
if (
$gametype == "tdm") {
$gametype = "Team Deathmatch";
}
if (
$gametype == "bel") {
$gametype = "Behind Enemy Lines";
}
$last_value = count($output) - 1;
$players_string = $output[$last_value];
$players_string = explode("\n", $players_string);

$i = 1;
while (
$i != count($players_string)) {
$split = explode(" ", $players_string[$i], 3);
$playerlist[$i-1]['score'] = $split[0];
$playerlist[$i-1]['ping'] = $split[1];
$playerlist[$i-1]['player'] = $split[2];
$i++;
}

function
cmp ($a, $b) {
   if (
$a["score"] > $b["score"]) {
   return -
1;
   }
   if (
$a["score"] < $b["score"]) {
   return
1;
   }
   if (
$a["score"] == $b["score"]) {
   return
0;
   }
}

@
usort($playerlist, "cmp");

$players = count($playerlist);

// Create the image url:
if (substr($mapname, 0, 3) == "dm/") {
$picture_src = str_replace("dm/", "", $mapname);
} else {
$picture_src = str_replace("obj/", "", $mapname);
}
$picture_src = "/images/q3a_main/" . $picture_src . ".jpg";
}

// Check if the server is reached:
if (!$max_clients) {
?>
<html>
<head>
      <title>Server offline</title>
</head>
<body bgcolor="<?=$bgcolor?>">
<table align="center" bgcolor="<?=$color1?>">
<tr>
    <td><img src="<?=$img_offline?>" border="0"></td>
</tr>
</table>
</body>
</html>
<?
exit;
}

// Start the output:
?>
<html>
<head>
      <title><?=stripcolors($hostname)?></title>
</head>
<body bgcolor="#acacac">
<table width="150" border="0" align="center">

<tr>
    <td width="200" valign="top" bgcolor="<?=$color1?>">
    <table border="0">
         
         <tr>
             <td bgcolor="<?=$color2?>" width="200"><img src="<?=$picture_src?>"></td>
         </tr>
    </td>
    <td valign="top" bgcolor="<?=$color1?>">
        <table border="0">
        <tr>
            <td bgcolor="<?=$color2?>" width="200">Player name:</td>
        </tr>
        <?
        $i = 1;
        while (count($playerlist) != $i) {
        ?>
        <tr>
            <td bgcolor="<?=$color2?>" width="200"><font size="2">
                <?=colors(str_replace("\"", "", $playerlist[$i]['player']))?>
            </font></td>
           
        </tr>

        <?
        $i = $i + 1;
        }
    ?>
<tr><td><font size="2" color="<?=$color2?>">Call of Duty Serverstats created by <a href="mailto:peter.verboom@gnomezz.com">Ghostie</a></font></td></tr>
</table>
    </table>
    </td>

</tr>
</table>
<center>

</center>
</body>
</html>
Title: Re: phpbox code question
Post by: IchBin on March 15, 2005, 04:40:40 AM
Well I did get another script to work using the include method. But I still cannot get this script to work. It seems that I cannot connect to the server. It gives me the error that the server is offline. I believe this is not your problem now hehe.
Title: Re: phpbox code question
Post by: Bjørn on March 15, 2005, 07:28:30 AM
:) Probably not.

IF you want to try it in a block, remove the ?php and ?> from it, put all the html stuff inside a echo and remove the <html> and <head>/<body> tags. Then test it in the preview box. It may work...when the server is OK of course. ;)
Title: Re: phpbox code question
Post by: NightOwl on March 15, 2005, 09:59:42 AM
the phpbox seems refuse to fetch outside script.

This is the code it refuse to run the script:

echo'People in the Chatroom:<br /><script src="http://www.everywherechat.com/members.asp?room=', $modSettings['chatRoomName'], '"></script>';
Title: Re: phpbox code question
Post by: knat on March 15, 2005, 10:14:31 AM
I wanted to use this code to get the latest post in a blog on the frontpage.

require("/customers/knat.net/knat.net/httpd.www/gw/SSI.php");
ssi_recentTopics();


but i get these errors:



Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /customers/knat.net/knat.net/httpd.www/gw/TPpreview.php(18) : eval()'d code on line 1

Parse error: parse error, unexpected $ in /customers/knat.net/knat.net/httpd.www/gw/TPpreview.php(18) : eval()'d code on line 1


im not any good at php just thought it might help  ;)

Title: Re: phpbox code question
Post by: Bjørn on March 15, 2005, 11:28:42 AM
Hm..the preview box can't read SSI.php actually..how you tried to just make it show in the block? it might work there.

And you won't need require at all, just use ssi_recentTopics();...in fact, thats what "recent topics" block use.. ;)
Title: Re: phpbox code question
Post by: knat on March 15, 2005, 12:05:19 PM
Well im not so good with php.  ???

I would really like a box saying like this:
Quote
Hello [username]

You are a part of the [groupname] group

If you think you belong in a different group then contact [admin name].

Could you help me out with that code bloc, or should i start study php ?  ;D

Btw the "recent topics" shows now after i installed your fix. ( no more 500 error ) Thanks  ;)
Title: Re: phpbox code question
Post by: Bjørn on March 15, 2005, 01:05:05 PM
how about:
    global $db_prefix;
    $mgroup=array();
    $request = db_query("SELECT ID_GROUP,groupName FROM {$db_prefix}membergroups WHERE 1", __FILE__, __LINE__);
    if (mysql_num_rows($request) > 0){
        while($row=mysql_fetch_assoc($request)){
                $mgroup[]=array(
                        'id' => $row['ID_GROUP'],
                        'name' => $row['groupName'],
                );
        }
        mysql_free_result($request);
     }
     echo 'Hello '.$context['user']['name'].' !<br /><br />You are a part of these group(s):<br /><br />';

     foreach($mgroup as $mg){
         if(in_array($mg['id'],$context['TPortal']['membergroups']))
             echo $mg['name'].'<br />';
     }
     echo '<br />If you think you belong in a different group then contact admin.';


It wont work in preview mode, but it does in a block. As you can see, some variables are already present. Like username and which groups it belong. It just does not contain the names of groups, so thats what the SQl call is for.
Title: Re: phpbox code question
Post by: knat on March 15, 2005, 01:58:08 PM
Thank You (https://www.tinyportal.net/proxy.php?request=http%3A%2F%2Fwww.pl-clan.org%2Fmodules%2FForums%2Fimages%2Fsmiles%2Fdancinfool.gif&hash=b2dd3a62b9a6d2a4845d531561a6006d4efc97e8)
Title: Re: phpbox code question
Post by: Pierse on March 20, 2005, 05:42:41 PM
I am trying to insert this code to pull stats from my gallery.

include "gallery/cpmfetch/coppermine.php";
$objCpm = new cpm('/gallery');

$objCpm->cpm_formatStats("Here is a percent sign %% There are
   %f files in %c categories containing %a albums,  having served %v views and getting %n comments");

$objCpm->cpm_close();

However, I get this error when running phptest
QuoteWarning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/pierson/public_html/smf/TPpreview.php(18) : eval()'d code on line 1

Parse error: parse error, unexpected $ in /home/pierson/public_html/smf/TPpreview.php(18) : eval()'d code on line 1


output errors:
QuoteWarning: Unknown(gallery/cpmfetch/coppermine.php): failed to open stream: No such file or directory in /home/pierson/public_html/smf/Sources/Load.php(1042) : eval()'d code(31) : eval()'d code on line 1

Warning: Unknown(gallery/cpmfetch/coppermine.php): failed to open stream: No such file or directory in /home/pierson/public_html/smf/Sources/Load.php(1042) : eval()'d code(31) : eval()'d code on line 1

Warning: (null)(): Failed opening 'gallery/cpmfetch/coppermine.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/pierson/public_html/smf/Sources/Load.php(1042) : eval()'d code(31) : eval()'d code on line 1

Fatal error: Cannot instantiate non-existent class: cpm in /home/pierson/public_html/smf/Sources/Load.php(1042) : eval()'d code(31) : eval()'d code on line 2

any ideas?
Title: Re: phpbox code question
Post by: Bjørn on March 20, 2005, 11:13:30 PM
Try to set the full path instead.
Title: Re: phpbox code question
Post by: Pierse on March 21, 2005, 04:22:38 AM
I did this... and all the above errors are gone... now I get this error:

QuoteFatal error: Cannot instantiate non-existent class: cpm in /home/pierson/public_html/smf/Sources/Load.php(1042) : eval()'d code(31) : eval()'d code on line 2
Title: Re: phpbox code question
Post by: Bjørn on March 21, 2005, 09:29:12 AM
That is something with the included file...the class cannot be started. Have you checked the class actually exist in the coppermine.php file? That it is not included there too?
Title: Re: phpbox code question
Post by: guido on March 31, 2005, 06:07:35 PM
hi
I want to insert a small code php, the function forumkings() of the mod Forumkings, in a html box, but I am simply not able to do it. It appears in the box as forumkings() and not executed. I placed the keys of php, I removed them, placed html, but nothing happened; it does'nt interpret the php of the include. Is there a way to make it hapen??


saludos

guido
Title: Re: phpbox code question
Post by: Kindred on March 31, 2005, 07:41:07 PM
place php functions in the php box, noth the html box...
Title: Re: phpbox code question
Post by: guido on April 01, 2005, 02:48:40 PM
Quote from: Kindred on March 31, 2005, 07:41:07 PM
place php functions in the php box, noth the html box...

phpbox?,  that phpbox?.
  I believe that he must be of beta5, and I only have beta4 :(
Title: Re: phpbox code question
Post by: Bjørn on April 01, 2005, 04:18:28 PM
Quite right, "phpbox" is only in beta5  I am afraid.
Title: Re: phpbox code question
Post by: JustClem on April 02, 2005, 06:57:06 AM
Quote from: Bloc on March 15, 2005, 07:28:30 AMIF you want to try it in a block, remove the ?php and ?> from it, put all the html stuff inside a echo and remove the <html> and <head>/<body> tags. Then test it in the preview box. It may work...when the server is OK of course. ;)

I am confused (big surprise, eh?)

In trying to add a TeamSpeak block to the portal, I found out that my TS host blocks the query port, so I have to use a service they provide in order to get server status.  Essentially, I can use the PHP code below and it works, but it is larger than I would like.


$server_id="b14231b2dd3e7b50b6fa6e7af8fd11af";
@readfile("http://osg.cc/voicestatus/remote.php?server=$server_id");


The example HTML code uses a CSS style that can be edited to make the text smaller; however, I can't get that to work because HTML does not seem to work in a phpBox and PHP doesn't seem to work inside an HTML box.  Is that correct, or am I doing something wrong?  Is there a way to have PHP embedded inside HTML in a block?
Title: Re: phpbox code question
Post by: Bjørn on April 02, 2005, 08:49:16 AM
If its html in that example, just enclose it in a echo staement, i assume it will look like this:

echo 'htmlfromyourexamplehere';

$server_id="b14231b2dd3e7b50b6fa6e7af8fd11af";
@readfile("http://osg.cc/voicestatus/remote.php?server=$server_id");

echo 'anyendhtmlcodehere';

So yes, html does work in phpbox, you just need to enclose it in a echo statement.
Title: Re: phpbox code question
Post by: JustClem on April 02, 2005, 08:51:34 AM
Bloc, thanks.  Will try it now 

Edit:  Worked like a charm!  Thanks, Bloc 8)
Title: Re: phpbox code question
Post by: hivelord on May 03, 2005, 07:48:32 AM
Yeah, I'm getting better at PHP myself.

I realized that, really, all it does is present dynamic content to webpages. Here, I thought itwas a full fledged programming language or something.

I'm sure it can do much more than just write HTML, but it sure does that well.
Title: Re: phpbox code question
Post by: Bjørn on May 03, 2005, 09:43:49 AM
oh yes, much more. And it is a programming language. It has all the usual constructs with objects,if/then, select,do/while,strings/math etc.

It even exists a program to translate PHP scripts/applications to WIndows programs, just with a extra library for accomandate for what the server usually does.

The html part is really very simple - it just takes what you put into the echo statement, and do all kinds of manipulations on it, thereafter just "writes"(echo) it out to the browser. Its the browser that actually translates html, PHP is the powerful "shaper" of the material it gets from either manually put in, databasebased or fed by POST values.