changed file headers & added AUTHORS file
[PHP-P10.git] / Bots / PHPGod.class.php
1 <?php
2 /******************************* PHP-P10 v2 *****************************
3  * Copyright (C) 2011  Philipp Kreil (pk910)                            *
4  *                                                                      *
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.                                  *
9  *                                                                      * 
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.                         *
14  *                                                                      *
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/>. *
17  *                                                                      *
18  ************************************************************************
19  * 
20  *  Bots/PHPGod.class.php
21  *
22  * simple PHP debugger...
23  *
24  */
25
26 class {$_NAME} extends Bot {
27         private $uplink;
28         private $php, $phpcache = array();
29         
30         public function load($uplink, $old = false) {
31                 $this->uplink = $uplink;
32                 if(!$old) {
33                         $nick = "PHP";
34                         $ident = "php";
35                         $host = "Services.WebGamesNet.net";
36                         $ip = "0::0";
37                         $realname = "PHP PHP PHP PHP PHP";
38                         $modes = "ioknISD";
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                         }
44                 } else {
45                         $this->php = $old;
46                 }
47                 
48                 ModCMD::bind($this, BIND_CHANMSG, "recive_privmsg");
49                 ModCMD::bind($this, BIND_QUIT, "recive_quit");
50         }
51         
52         public function unload($rehash = false) {
53                 foreach($this->phpcache as $id => $php) {
54                         fclose($php['pipes'][1]);
55                         fclose($php['pipes'][2]);
56                         proc_terminate($php['proc'],9);
57                 }
58                 $this->phpcache = array();
59                 if($rehash) {
60                         return $this->php;
61                 } else {
62                         $this->uplink->delUser($this->php, "Bye.");
63                 }
64         }
65         
66         public function loop() {
67                 foreach($this->phpcache as $id => $php) {
68                         if(!$this->checkstate($php)) {
69                                 unset($this->phpcache[$id]);
70                         }
71                 }
72         }
73         
74         function recive_privmsg($user, $channel, $message) {
75                 $opOnPHPChannel = false;
76                 $PHPChannel = P10_Channel::getChannelByName("#PHP");
77                 if($PHPChannel) {
78                         $privs = $PHPChannel->getUserPrivs($user);
79                         $opOnPHPChannel = ($privs & P10_Channel::USERPRIV_OPED);
80                 }
81                 if(!$user->getModes()->hasMode('o') && !$opOnPHPChannel) return 0;
82                 $exp=explode(" ", $message, 2);
83                 switch (strtolower($exp[0])) {
84                         case "~php":
85                                 if(count($this->phpcache) > 5) {
86                                         $this->uplink->notice($this->php, $user, "too many running php processes at the moment!");
87                                         return;
88                                 }
89                                 $entry=array();
90                                 $entry['channel'] = $channel;
91                                 $descriptor = array(0 => array("pipe", "r"),1 => array("pipe", "w"),2 => array("pipe", "w"));
92                                 $entry['proc'] = proc_open('php', $descriptor, $entry['pipes']);
93                                 if(!is_resource($entry['proc'])) {
94                                         $this->uplink->notice($this->php, $user, "error while loading php!");
95                                         return;
96                                 }
97                                 $entry['time'] = time();
98                                 fwrite($entry['pipes'][0], "<"."?php ".$exp[1]." ?".">");
99                                 fclose($entry['pipes'][0]);
100                                 $this->phpcache[] = $entry;
101                                 break;
102                 }
103         }
104         
105         function recive_quit($user, $reason) {
106                 if($user === $this->php) {
107                         $this->load($this->uplink);
108                 }
109         }
110         
111         function checkstate($php) {
112                 $data = proc_get_status($php['proc']);
113                 if(!$data['running']) {
114                         $out="";
115                         while(!feof($php['pipes'][1])) {
116                                 $out .= fgets($php['pipes'][1], 128);
117                         }
118                         $eout="";
119                         while(!feof($php['pipes'][2])) {
120                                 $eout .= fgets($php['pipes'][2], 128);
121                         }
122                         if($out != "") {
123                                 $out=str_replace("\r","",$out);
124                                 $lines=explode("\n",$out);
125                                 $i=0;
126                                 foreach($lines as $line) {
127                                         $i++;
128                                         if($i>1000) {
129                                                 $this->uplink->privmsg($this->php, $php['channel'], "too many lines!");
130                                                 break; 
131                                         }
132                                         $this->uplink->privmsg($this->php, $php['channel'], $line);
133                                 }
134                         }
135                         if($eout != "") {
136                                 $eout=str_replace("\r","",$eout);
137                                 $lines=explode("\n",$eout);
138                                 $i=0;
139                                 foreach($lines as $line) {
140                                         $i++;
141                                         if($i>1000) { 
142                                                 $this->uplink->privmsg($this->php, $php['channel'], "too many lines!");
143                                                 break; 
144                                         }
145                                         $this->uplink->privmsg($this->php, $php['channel'], "\ 34".$line."\ 3");
146                                 }
147                         }
148                         fclose($php['pipes'][1]);
149                         fclose($php['pipes'][2]);
150                         proc_close($php['proc']);
151                         return false;
152                 } else {
153                         if($php['time']+10 > time()) {
154                                 return true;
155                         } else {
156                                 //TIMEOUT
157                                 if($php['term']) {
158                                         fclose($php['pipes'][1]);
159                                         fclose($php['pipes'][2]);
160                                         proc_terminate($php['proc'],9);
161                                         $this->uplink->privmsg($this->php, $php['channel'], "php hard timeout. sending SIGKILL");
162                                         return false;
163                                 } else {
164                                         proc_terminate($php['proc']);
165                                         $php['term']=true;
166                                         $this->uplink->privmsg($this->php, $php['channel'], "php timeout. (maximum of 10 seconds exceeded)  sending SIGTERM"); 
167                                         return true;
168                                 }
169                         }
170                 }
171         }
172         
173 }
174
175 ?>