PHP Fun

Some bits of PHP I’ve added to my sites lately:
The first is to alert the site visitors that the site may go down due to the server being on battery power:

<?php
 $batt_time = shell_exec('/sbin/apcaccess | awk -F":" \'/TIMELEFT/ { print $2 }\'');
?>
<h1 style="color: #FF0000;">
     WARNING:</h1>
<p style="bold; color: #FF0000;">The server that hosts this website is running on emergency battery power and could shut down at any time. The UPS has approximately <?=$batt_time?>remaining.</p>

Condition for block display:

<?php
$batt_status = trim(shell_exec('/sbin/apcaccess | awk -F":" \'/STATUS/ { print $2 }\''));
return $batt_status != "ONLINE";
?>

The second is a block that prints the UNIX MOTD if the file has content:

<?php
     $res = fopen("/etc/motd", "r") or die;
     echo "<pre>\n";
     while (!feof($res)) {
           $line = fgets($res);
           echo $line;
     }
 
     echo "</pre>\n";
     fclose($res);
?>

And the block display condition:

<?php
     $starray = stat("/etc/motd");
     return (!($starray === FALSE || $starray[7] < 1));
?>

The third is a block that prints the output of the UNIX fortune(6) command:

<?php
     $fortune = array();
     exec("/usr/games/fortune", $fortune) or die;
     echo "<pre>\n";
 
     foreach ($fortune as $line)
           echo $line . "\n";
     echo "</pre>\n";
?>

And the block display condition:

<?php
     $mystat = stat("/usr/games/fortune");
 
     return (!($mystat === FALSE     ||  # stat() call failed
             !($mystat[2] & 0100001) ||  # is not a world-executable file
              ($mystat[2] &  040000) ||  # is setuid
              ($mystat[2] &  020000)));  # is setgid
?>
Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: