2 /******************************* PHP-P10 v2 *****************************
3 * Copyright (C) 2011-2012 Philipp Kreil (pk910) *
5 * This program is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 3 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 ************************************************************************
20 * Bots/PHPGod.class.php
22 * simple PHP debugger...
26 class {$_NAME} extends Bot {
28 private $php, $phpcache = array();
30 public function load($uplink, $old = false) {
31 $this->uplink = $uplink;
35 $host = "Services.WebGamesNet.net";
37 $realname = "PHP PHP PHP PHP PHP";
39 $this->php = $this->uplink->addUser($nick,$ident,$host,$ip,$modes,$realname);
40 if(is_a($this->php, "P10_User")) {
41 $this->uplink->join($this->php, "#php", (P10_Channel::USERPRIV_OPED | P10_Channel::USERPRIV_VOICE));
42 $this->uplink->join($this->php, "#dev", P10_Channel::USERPRIV_VOICE);
43 $this->uplink->join($this->php, "#CoderCom", P10_Channel::USERPRIV_VOICE);
49 ModCMD::bind($this, BIND_CHANMSG, "recive_privmsg");
50 ModCMD::bind($this, BIND_QUIT, "recive_quit");
53 public function unload($rehash = false) {
54 foreach($this->phpcache as $id => $php) {
55 fclose($php['pipes'][1]);
56 fclose($php['pipes'][2]);
57 proc_terminate($php['proc'],9);
59 $this->phpcache = array();
63 $this->uplink->delUser($this->php, "Bye.");
67 public function loop() {
68 foreach($this->phpcache as $id => $php) {
69 if(!$this->checkstate($php)) {
70 unset($this->phpcache[$id]);
75 function recive_privmsg($user, $channel, $message) {
76 $opOnPHPChannel = false;
77 $PHPChannel = P10_Channel::getChannelByName("#PHP");
79 $privs = $PHPChannel->getUserPrivs($user);
80 $opOnPHPChannel = ($privs & P10_Channel::USERPRIV_OPED);
82 if(!$user->getModes()->hasMode('o') && !$opOnPHPChannel) return 0;
83 $exp=explode(" ", $message, 2);
84 switch (strtolower($exp[0])) {
86 if(count($this->phpcache) > 5) {
87 $this->uplink->notice($this->php, $user, "too many running php processes at the moment!");
91 $entry['channel'] = $channel;
92 $descriptor = array(0 => array("pipe", "r"),1 => array("pipe", "w"),2 => array("pipe", "w"));
93 $entry['proc'] = proc_open('php', $descriptor, $entry['pipes']);
94 if(!is_resource($entry['proc'])) {
95 $this->uplink->notice($this->php, $user, "error while loading php!");
98 $entry['time'] = time();
99 if(preg_match("#pastebin\.com/([a-zA-Z0-9]*)$#i", $exp[1])) {
100 $pasteid = explode("/", $exp[1]);
101 $pasteid = $pasteid[count($pasteid)-1];
102 $codecontent = file_get_contents("http://pastebin.com/download.php?i=".$pasteid);
103 if(preg_match("#Unknown Paste ID!#i", $codecontent)) {
104 $this->uplink->notice($this->bot, $user, "Unknown Paste ID!");
107 $code = $codecontent;
109 $code = "<"."?php " . $exp[1] . " ?".">";
111 fwrite($entry['pipes'][0], $code);
112 fclose($entry['pipes'][0]);
113 $this->phpcache[] = $entry;
118 function recive_quit($user, $reason) {
119 if($user === $this->php) {
120 $this->load($this->uplink);
124 function checkstate($php) {
125 $data = proc_get_status($php['proc']);
126 if(!$data['running']) {
128 while(!feof($php['pipes'][1])) {
129 $out .= fgets($php['pipes'][1], 128);
132 while(!feof($php['pipes'][2])) {
133 $eout .= fgets($php['pipes'][2], 128);
136 $out=str_replace("\r","",$out);
137 $lines=explode("\n",$out);
139 foreach($lines as $line) {
142 $this->uplink->privmsg($this->php, $php['channel'], "too many lines!");
145 $this->uplink->privmsg($this->php, $php['channel'], $line);
149 $eout=str_replace("\r","",$eout);
150 $lines=explode("\n",$eout);
152 foreach($lines as $line) {
155 $this->uplink->privmsg($this->php, $php['channel'], "too many lines!");
158 $this->uplink->privmsg($this->php, $php['channel'], "
\ 34".$line."
\ 3");
161 fclose($php['pipes'][1]);
162 fclose($php['pipes'][2]);
163 proc_close($php['proc']);
166 if($php['time']+10 > time()) {
171 fclose($php['pipes'][1]);
172 fclose($php['pipes'][2]);
173 proc_terminate($php['proc'],9);
174 $this->uplink->privmsg($this->php, $php['channel'], "php hard timeout. sending SIGKILL");
177 proc_terminate($php['proc']);
179 $this->uplink->privmsg($this->php, $php['channel'], "php timeout. (maximum of 10 seconds exceeded) sending SIGTERM");