2 /********************************* PHP-P10 ******************************
3 * P10 uplink class by pk910 (c)2011 pk910 *
4 ************************************************************************
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. *
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. *
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. *
21 ************************************************************************
23 * Bots/PHPGod.class.php
25 * simple PHP debugger...
29 class {$_NAME} extends Bot {
31 private $php, $phpcache = array();
33 public function load($uplink, $old = false) {
34 $this->uplink = $uplink;
38 $host = "Services.WebGamesNet.net";
40 $realname = "PHP PHP PHP PHP PHP";
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);
51 ModCMD::bind($this, BIND_CHANMSG, "recive_privmsg");
52 ModCMD::bind($this, BIND_QUIT, "recive_quit");
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);
61 $this->phpcache = array();
65 $this->uplink->delUser($this->php, "Bye.");
69 public function loop() {
70 foreach($this->phpcache as $id => $php) {
71 if(!$this->checkstate($php)) {
72 unset($this->phpcache[$id]);
77 function recive_privmsg($user, $channel, $message) {
78 $opOnPHPChannel = false;
79 $PHPChannel = P10_Channel::getChannelByName("#PHP");
81 $privs = $PHPChannel->getUserPrivs($user);
82 $opOnPHPChannel = ($privs & P10_Channel::USERPRIV_OPED);
84 if(!$user->getModes()->hasMode('o') && !$opOnPHPChannel) return 0;
85 $exp=explode(" ", $message, 2);
86 switch (strtolower($exp[0])) {
88 if(count($this->phpcache) > 5) {
89 $this->uplink->notice($this->php, $user, "too many running php processes at the moment!");
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!");
100 $entry['time'] = time();
101 fwrite($entry['pipes'][0], "<"."?php ".$exp[1]." ?".">");
102 fclose($entry['pipes'][0]);
103 $this->phpcache[] = $entry;
108 function recive_quit($user, $reason) {
109 if($user === $this->modman) {
110 $this->load($this->uplink);
114 function checkstate($php) {
115 $data = proc_get_status($php['proc']);
116 if(!$data['running']) {
118 while(!feof($php['pipes'][1])) {
119 $out .= fgets($php['pipes'][1], 128);
122 while(!feof($php['pipes'][2])) {
123 $eout .= fgets($php['pipes'][2], 128);
126 $out=str_replace("\r","",$out);
127 $lines=explode("\n",$out);
129 foreach($lines as $line) {
132 $this->uplink->privmsg($this->php, $php['channel'], "too many lines!");
135 $this->uplink->privmsg($this->php, $php['channel'], $line);
139 $eout=str_replace("\r","",$eout);
140 $lines=explode("\n",$eout);
142 foreach($lines as $line) {
145 $this->uplink->privmsg($this->php, $php['channel'], "too many lines!");
148 $this->uplink->privmsg($this->php, $php['channel'], "
\ 34".$line."
\ 3");
151 fclose($php['pipes'][1]);
152 fclose($php['pipes'][2]);
153 proc_close($php['proc']);
156 if($php['time']+10 > time()) {
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");
167 proc_terminate($php['proc']);
169 $this->uplink->privmsg($this->php, $php['channel'], "php timeout. (maximum of 10 seconds exceeded) sending SIGTERM");