fixed some warnings
[PHP-P10.git] / Bots / PHPGod.class.php
1 <?php
2 /********************************* PHP-P10 ******************************
3  *    P10 uplink class by pk910   (c)2011 pk910                         *
4  ************************************************************************
5  *                          Version 2 (OOP)                             *
6  *                                                                      *
7  * PHP-P10 is free software; you can redistribute it and/or modify      *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or    *
10  * (at your option) any later version.                                  *
11  *                                                                      *
12  * This program is distributed in the hope that it will be useful,      *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
15  * GNU General Public License for more details.                         *
16  *                                                                      *
17  * You should have received a copy of the GNU General Public License    *
18  * along with PHP-P10; if not, write to the Free Software Foundation,   *
19  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.       *
20  *                                                                      *
21  ************************************************************************
22  * 
23  *  Bots/PHPGod.class.php
24  *
25  * simple PHP debugger...
26  *
27  */
28
29 class {$_NAME} extends Bot {
30         private $uplink;
31         private $php, $phpcache = array();
32         
33         public function load($uplink, $old = false) {
34                 $this->uplink = $uplink;
35                 if(!$old) {
36                         $nick = "PHP";
37                         $ident = "php";
38                         $host = "Services.WebGamesNet.net";
39                         $ip = "0::0";
40                         $realname = "PHP PHP PHP PHP PHP";
41                         $modes = "ioknISD";
42                         $this->php = $this->uplink->addUser($nick,$ident,$host,$ip,$modes,$realname);
43                         if(is_a($this->php, "P10_User")) {
44                                 $this->uplink->join($this->php, "#php", (P10_Channel::USERPRIV_OPED | P10_Channel::USERPRIV_VOICE));
45                                 $this->uplink->join($this->php, "#dev", P10_Channel::USERPRIV_VOICE);
46                         }
47                 } else {
48                         $this->php = $old;
49                 }
50                 
51                 ModCMD::bind($this, BIND_CHANMSG, "recive_privmsg");
52                 ModCMD::bind($this, BIND_QUIT, "recive_quit");
53         }
54         
55         public function unload($rehash = false) {
56                 foreach($this->phpcache as $id => $php) {
57                         fclose($php['pipes'][1]);
58                         fclose($php['pipes'][2]);
59                         proc_terminate($php['proc'],9);
60                 }
61                 $this->phpcache = array();
62                 if($rehash) {
63                         return $this->php;
64                 } else {
65                         $this->uplink->delUser($this->php, "Bye.");
66                 }
67         }
68         
69         public function loop() {
70                 foreach($this->phpcache as $id => $php) {
71                         if(!$this->checkstate($php)) {
72                                 unset($this->phpcache[$id]);
73                         }
74                 }
75         }
76         
77         function recive_privmsg($user, $channel, $message) {
78                 $opOnPHPChannel = false;
79                 $PHPChannel = P10_Channel::getChannelByName("#PHP");
80                 if($PHPChannel) {
81                         $privs = $PHPChannel->getUserPrivs($user);
82                         $opOnPHPChannel = ($privs & P10_Channel::USERPRIV_OPED);
83                 }
84                 if(!$user->getModes()->hasMode('o') && !$opOnPHPChannel) return 0;
85                 $exp=explode(" ", $message, 2);
86                 switch (strtolower($exp[0])) {
87                         case "~php":
88                                 if(count($this->phpcache) > 5) {
89                                         $this->uplink->notice($this->php, $user, "too many running php processes at the moment!");
90                                         return;
91                                 }
92                                 $entry=array();
93                                 $entry['channel'] = $channel;
94                                 $descriptor = array(0 => array("pipe", "r"),1 => array("pipe", "w"),2 => array("pipe", "w"));
95                                 $entry['proc'] = proc_open('php', $descriptor, $entry['pipes']);
96                                 if(!is_resource($entry['proc'])) {
97                                         $this->uplink->notice($this->php, $user, "error while loading php!");
98                                         return;
99                                 }
100                                 $entry['time'] = time();
101                                 fwrite($entry['pipes'][0], "<"."?php ".$exp[1]." ?".">");
102                                 fclose($entry['pipes'][0]);
103                                 $this->phpcache[] = $entry;
104                                 break;
105                 }
106         }
107         
108         function recive_quit($user, $reason) {
109                 if($user === $this->php) {
110                         $this->load($this->uplink);
111                 }
112         }
113         
114         function checkstate($php) {
115                 $data = proc_get_status($php['proc']);
116                 if(!$data['running']) {
117                         $out="";
118                         while(!feof($php['pipes'][1])) {
119                                 $out .= fgets($php['pipes'][1], 128);
120                         }
121                         $eout="";
122                         while(!feof($php['pipes'][2])) {
123                                 $eout .= fgets($php['pipes'][2], 128);
124                         }
125                         if($out != "") {
126                                 $out=str_replace("\r","",$out);
127                                 $lines=explode("\n",$out);
128                                 $i=0;
129                                 foreach($lines as $line) {
130                                         $i++;
131                                         if($i>1000) {
132                                                 $this->uplink->privmsg($this->php, $php['channel'], "too many lines!");
133                                                 break; 
134                                         }
135                                         $this->uplink->privmsg($this->php, $php['channel'], $line);
136                                 }
137                         }
138                         if($eout != "") {
139                                 $eout=str_replace("\r","",$eout);
140                                 $lines=explode("\n",$eout);
141                                 $i=0;
142                                 foreach($lines as $line) {
143                                         $i++;
144                                         if($i>1000) { 
145                                                 $this->uplink->privmsg($this->php, $php['channel'], "too many lines!");
146                                                 break; 
147                                         }
148                                         $this->uplink->privmsg($this->php, $php['channel'], "\ 34".$line."\ 3");
149                                 }
150                         }
151                         fclose($php['pipes'][1]);
152                         fclose($php['pipes'][2]);
153                         proc_close($php['proc']);
154                         return false;
155                 } else {
156                         if($php['time']+10 > time()) {
157                                 return true;
158                         } else {
159                                 //TIMEOUT
160                                 if($php['term']) {
161                                         fclose($php['pipes'][1]);
162                                         fclose($php['pipes'][2]);
163                                         proc_terminate($php['proc'],9);
164                                         $this->uplink->privmsg($this->php, $php['channel'], "php hard timeout. sending SIGKILL");
165                                         return false;
166                                 } else {
167                                         proc_terminate($php['proc']);
168                                         $php['term']=true;
169                                         $this->uplink->privmsg($this->php, $php['channel'], "php timeout. (maximum of 10 seconds exceeded)  sending SIGTERM"); 
170                                         return true;
171                                 }
172                         }
173                 }
174         }
175         
176 }
177
178 ?>