Jump to content

Roflbot/Alarm/Remote


Recommended Posts

I've been watching Hak.5 for a while I love Darren's php/apache segments. When I saw the roflbot I knew I had to do something with it so I decided to make the roflbot more tamagochi-like.

I've bascially taken the roflbot code and but added another webpage to control it from, where you can feed, play and make it sleep. I've also used imagemagick and ffmpeg to create different videos for each of the actions depending on the time of day and the temperature.

And comments will be greatly appreciated as I'm looking to actually add in the alarm part of this project so that the roflbot actually does something the same way the rss alarm clock did, but thought I would just get this out and see what people though. I've also attached the pictures that I used if you would like to try it out. My artistic skills may not be as good as Darren's, but at least I tried to use arrays :P.

roflbot.php

<?php
$loopcount=1;
$currbg =  array('blink'=>'','dance'=>'','eating'=>'','fight'=>'','hungry'=>'','sleep'=>'',
	'angry'=>'','tired'=>'','walk'=>'');
$title = '';
$currsky ='';
$prevbot = '';
$roflbotarray = array('angry','blink','dance','eating','fight','hungry','sleep','tired','walk');
$roflbotrand = array('blink','fight','walk');
$roflbot = $roflbotrand[0];
$hunger = 0;
$energy = 100;
$fun = 100;
$videochoice = array ('angry'=>5,'blink'=>6,'dance'=>7,'eating'=>8,'fight'=>9,'hungry'=>10,
	'sleep'=>11,'tired'=>12,'walk'=>13);

while ($title != "#roflbot *exit*") { //kill command

//Parse rss feed
$weather_feed = file_get_contents("http://weather.yahooapis.com/forecastrss?p=ASXX0075&u=c");
$weather = simplexml_load_string($weather_feed);
if(!$weather) die('weather failed');
$copyright = $weather->channel->copyright;
$channel_yweather = $weather->channel->children("http://xml.weather.yahoo.com/ns/rss/1.0");

foreach($channel_yweather as $x => $channel_item) 
	foreach($channel_item->attributes() as $k => $attr) 
		$yw_channel[$x][$k] = $attr;

$item_yweather = $weather->channel->item->children("http://xml.weather.yahoo.com/ns/rss/1.0");

foreach($item_yweather as $x => $yw_item) {
	foreach($yw_item->attributes() as $k => $attr) {
		if($k == 'day') $day = $attr;
		if($x == 'forecast') { $yw_forecast[$x][$day . ''][$k] = $attr;	} 
		else { $yw_forecast[$x][$k] = $attr; }
	}
}

//Get needed data
$sunrise = date("Hi",strtotime($yw_channel['astronomy']['sunrise']));
$sunset = date("Hi",strtotime($yw_channel['astronomy']['sunset']));
$currtemp = $yw_forecast['condition']['temp'];	
$time = date("Hi");

//Set time of day
if ( $time < $sunrise || $time > $sunset){
	$sky = 'night';
}elseif( $time < 1200){
	$sky = 'morning';
}else{
	$sky = 'afternoon';
}

//Set background depending on temperature 
if ( $currtemp < 10 ){ //Cold
	$bg = 'coldbg.gif';
}elseif ( $currtemp < 20){ //Normal
	$bg = 'normalbg.gif';
}else { // Hot
	$bg = 'hotbg.gif';
}

//no activity for 5 minutes
if ($loopcount >= 30) { 
	echo "\nI've been idle too long. Doing something new. ";
	while ( $prevbot == $roflbot ){
		$choice = array_rand($roflbotrand);
		$roflbot = $roflbotrand[$choice];
	}
	echo "Randomly picked " . $roflbot  . "\n";
}

//Get action from control.php
$file=fopen("action.txt","r");
$action = '';
$action = fgets($file);
fclose($file);
$file=fopen("action.txt","w");
fwrite($file,'');
fclose($file);

//Change roflbot if option chosen on control.php
if ($action!= '') {
	echo "Action: ". $action . "\n";
	$roflbot = $roflbotarray[$action];
	if ($action==3) {
		$hunger=0;
	} elseif ($action==7) {
		$energy = 100;
	} elseif ($action==2) {
		$fun = 100;
	}
}

//Check hunger, energy and fun
$mood = array('angry','hungry','tired');
if ( $hunger > 100 && $energy < 0 && $fun < 0 ){
	$roflbot = $mood[rand(0,2)];
} elseif ( $hunger > 100 && $fun < 0 ){
	$roflbot = $mood[rand(0,1)];
} elseif ( $fun < 0 && $energy < 0) {
	$roflbot = $mood[rand(0,2)];
} elseif ( $hunger > 100 && $energy < 0 ){
	$roflbot = $mood[rand(1,2)];
} elseif($hunger >  100) {
	$roflbot = 'hungry';
} elseif ($energy < 0 ) {
	$roflbot = 'tired';
} elseif ( $fun < 0 ) {
	$roflbot = 'angry';
}

//Action chosen on control.php and not same roflbot
if ( $prevbot != $roflbot ){
	echo "Roflbot has changed\n";

	$choice = $videochoice[$roflbot];

	//Changing background
	if ( ($currbg[$roflbot] != $bg) || ($sky != $currsky) ){
		echo "Changing video\n";
		$d="images\\";
		$num = 1;

		//Set background picture
		if($sky == 'morning'){
			$skyimage = 'sun';
			$skypos = 'NorthWest';
		} elseif($sky == 'afternoon'){
			$skyimage = 'sun';
			$skypos = 'NorthEast';
		}else{
			$skyimage = 'moon';
			$skypos = 'NorthWest';
		}

		//Create new pictures
		while ( $num <= 10 ){
			exec('composite -gravity '.$skypos.' '.$d.''.$skyimage.'.gif '.$d.''.$bg.' '.$d.'image'.$num .'.gif');
			exec('composite -gravity center '.$d.''.$roflbot.''.$num.'.gif '.$d.'image'.$num. '.gif '.$d.'image'.$num.'.gif');
			$num = $num + 1;
		}

		//Create new video from pictures
		exec('ffmpeg -r 1 -y -i '. $d .'image%d.gif -r 25 videos\\'. $roflbot .'.avi');

		$currbg[$roflbot] = $bg;
		$currsky = $sky;
		$prevbot = $roflbot;
	}

	//Play new video
	system('curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=play -d item=' . $choice . ' -G > output.txt');
	$loopcount = 0;
	$prevbot = $roflbot;
}

echo " " . $loopcount;
$loopcount=$loopcount+1;

//Change stats
$hunger = $hunger + 1;
$energy = $energy - 1;
$fun = $fun - 1;
$file=fopen("stats.txt","w");
fwrite($file,$hunger."\n");
fwrite($file,$energy."\n");
fwrite($file,$fun);
fclose($file);

sleep ("10");
} // end while
system("curl --connect-timeout 3 127.0.0.1:8080/old/ -d control=shutdown -G > output.txt");
?>

control.php

<html>
<body>
<?php

if (isset($_GET['action'])) {
    $action=$_GET['action'];
	$file=fopen("action.txt","w");
	fwrite($file,$action);
	fclose($file);
}
$file=fopen("stats.txt","r");
echo "Hunger: ". fgets($file) . "<br/>";
echo "Energy: ". fgets($file) . "<br/>";
echo "Fun:    ". fgets($file) . "<br/>";
fclose($file);

?>

<p>
<a href="control.php?action=3">Feed</a>
<a href="control.php?action=7">Sleep</a>
<a href="control.php?action=2">Dance</a><p/>
<p><a href="control.php">Refresh</a></p>
</body>
</html>

images.rar

Link to comment
Share on other sites

Code is vulnerable to PHP code injection which would leave your site owned, not to mention your entire box in all probability. You should really make sure to filter all of your inputs.

Link to comment
Share on other sites

Something like this in control.php?

if (isset($_GET['action'])) {
    $action=$_GET['action'];
    if ( $action == 3 || $action == 7 || $action == 2 ){
        $file=fopen("action.txt","w");
        fwrite($file,$action);
        fclose($file);
    }
}

Link to comment
Share on other sites

  • 4 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...