Autofix Posted May 4, 2009 Share Posted May 4, 2009 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 Quote Link to comment Share on other sites More sharing options...
operat0r_001 Posted May 5, 2009 Share Posted May 5, 2009 =/etc/passwd Quote Link to comment Share on other sites More sharing options...
Autofix Posted May 6, 2009 Author Share Posted May 6, 2009 What exactly do you mean by that? Quote Link to comment Share on other sites More sharing options...
wetelectric Posted May 13, 2009 Share Posted May 13, 2009 haha nice idea, i'll try and have a look at this at some point next week. Quote Link to comment Share on other sites More sharing options...
dr0p Posted May 15, 2009 Share Posted May 15, 2009 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. Quote Link to comment Share on other sites More sharing options...
Autofix Posted May 16, 2009 Author Share Posted May 16, 2009 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); Â Â Â Â } } Quote Link to comment Share on other sites More sharing options...
dr0p Posted May 16, 2009 Share Posted May 16, 2009 Should fix your typo on "$action = 2" (I doubt you want your action to always be 2) but other than that you should be fine. Quote Link to comment Share on other sites More sharing options...
Autofix Posted May 17, 2009 Author Share Posted May 17, 2009 I realised that just after I posted, but I obviously forgot to change it. Thanks for that though. Quote Link to comment Share on other sites More sharing options...
doweneedname Posted October 1, 2009 Share Posted October 1, 2009 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. yes i agree with that one more thing,its need linux server to be its hosting Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.