Bnc & serveur web class/php5

Description

Un BNC pas complet mais qui est fonctionnel dont la modification des options peut-étre fait par le biais du serveur http intégré.

PHP5: Le script utilise PHP5 dans le dossier du Zend Engine(php.exe) ensuite exécuter le fichier batch go-phpbnc.bat ou par la ligne de commande allez dans le dossier du zend engine et executez le script par la commande "php.exe test.php"

Si vous n'avez pas php5 installez vous pouvez le télécharger avec le phpbnc à cette adresse : http://progcity.free.fr/php5.zip et lancez ensuite phpbnc.bat

Source / Exemple :


<?
if (!extension_loaded('sockets')) {
  dl(((PHP_SHLIB_SUFFIX=='dll')?'php_':'').'sockets.'.PHP_SHLIB_SUFFIX);
}
define(el,"\r\n");

//Background in black \x16
//Colors \x03
//Bold \x02
//Underline \x1F
//Background in colors "\x03".$i.','.$j // where $i is the color index of the text and $j the color index of the background
class config{
	var $config=Array();
	var $name='';
	
	function __wakeup(){
		$this->__construct();
	}
	
	function __construct($name='config.php'){
		$this->name=$name;
		$this->config['scriptlimit']=0;
		$this->config['tpldir']='./template/';
		$this->config['name']='MagicKing\'s BNC server';
		$this->config['datestart']=date('r',$this->config['timestart']);
		$this->config['author']='MagicKing';
		$this->config['timelimit']=60*60*24*31;
		$this->config['active']=0;
		$this->config['maxconnection']=20;
	}
	
	function __destruct(){
		$fp=fopen($this->name,'w');
		fwrite($fp,serialize($this));
		fclose($fp);
	}
	
	function exec(){
		$this->config['datestart']=date('r',$this->config['timestart']);
		set_time_limit($this->config['scriptlimit']);
	}
}

class template{
	var $search = array();
	var $replace = array();
	var $file   = "";
	var $i       = 0;

	function __construct($name = "",$bin=0){
		if($bin){
			$fp=fopen($name,'rb');
			while(!feof($fp))$this->file.=fread($fp,4096);
			return 1;
		}
		if(($this->file=@implode("",@file($name))) || $name == "")return 1;
		else return 0;
	}

	function add($mixed,$el = ""){
		if(is_array($mixed)){
			$this->file.=implode($el,$mixed);
			return 1;
		}else if(is_string($mixed) || is_numeric($mixed)){
			$this->file.=$mixed;
			return 1;
		}
		return 0;
	}

	function assign_array($array){
		if(is_array($array) && $this->file=="")return 0;
		reset($array);
		while(list($key,$value)=each($array)){
			$this->search[$this->i]='{'.strtoupper($key).'}';
			$this->replace[$this->i++]=$value;
		}
	}

	function reset(){
		$this->file = "";
		$this->search   = array();
		$this->replace = array();
		$this->i = 0;
	}

	function show(){
		if($this->file=="")return 0;
		reset($this->search);
		reset($this->replace);
		echo str_ireplace($this->search,$this->replace,$this->file);//If you use PHP5 change to str_ireplace
	}

	function retour(){
		if($this->file=="")return 0;
		reset($this->search);
		reset($this->replace);
		return str_ireplace($this->search,$this->replace,$this->file);//If you use PHP5 change to str_ireplace
	}

}
class user{
	var $fp;
	var $fp2;
	var $i;
	var $i2;
	var $buf='';
	var $buf2='';
	var $nick='Unamed';
	var $user;
	var $sc1 = 0;
	var $sc2 = 0;
	var $base;
	var $server='';
	var $password='5';
	var $id=0;
	var $port=6667;
	var $timelimit=0;
	var $chan;
	
	function __construct($fp,$id,$host = NULL,$port=6667){
		$this->fp=$fp;
		$this->sc1=1;
		$this->id=$id;
		$this->port=$port;
		$this->password=rand(1000,9999);
		if(!$host)$this->server=gethostbyaddr('127.0.0.1');
		else $this->server=$host;
		$this->write(1,"Welcome on \x02PHP BNC.");
		$this->write(2,"The \x02id\x02 of this connection is \x0304".$this->id."\x03.");
		$this->write(3,"The \x02password\x02 of this connection is \"\x0312".$this->password."\x03\".");
		$this->write(4,'Type : '."\x034".'/connect'."\x03".' '."\x034".'server'."\x03\x0312".':port'."\x03 to connect."); //Rouge
		$this->write(5,'Type : '."\x034".'/retrieve'."\x03".' '."\x034".'id'."\x03\x0312".' password'."\x03 to retrieve your connection."); //Rouge
	}
	
	function __destruct(){
		if($this->sc1)
			socket_write($this->fp,"ERROR :Closing Link: PHP BNC\r\n");
		if($this->sc2)
			socket_write($this->fp2,"QUIT :PHP BNC by MagicKing\n");
	}

	function write($code,$message){
		if(!$this->sc1)return 0;
		return socket_write($this->fp,$this->message(str_pad($code,3,'0',STR_PAD_LEFT)).$message.el);
	}
		
	function message($code,$other=''){
		$this->base[0]=':'.$this->server;
		$this->base[1]=$code;
		$this->base[2]=$this->nick;
		$this->base[3]=$other.' :';
		return implode(' ',$this->base);
	}
	
	function check(){
		if(!empty($GLOBALS['waiting'][$this->id])){
			if($GLOBALS['waiting'][$this->id]->password=$this->password){
				$this->fp=$GLOBALS['waiting'][$this->id]->fp;
				$this->sc1=1;
				$this->buf='';
				$this->i=0;
				socket_write($this->fp,':'.$GLOBALS['waiting'][$this->id]->name.' NICK :'.$this->nick.el);
				unset($GLOBALS['waiting'][$this->id]);
				reset($this->chan);
				while(list($key,$value)=each($this->chan))
					$value->join($this->fp,$this->fp2);
			}
		}
		if($this->sc1){
			$read=array($this->fp);
			if (false === ($sockets_modifie = socket_select($read , $w=NULL, $e=NULL, 0))) {
			}else if($sockets_modifie > 0) {
				$this->buf.=@socket_read($read[0],1);

				if($this->buf[$this->i]=="\n"){
					preg_match('/^([^ ]+) ([^'."\n".']+)/i',$this->buf,$tmp);
					switch(strtolower($tmp[1])){
					case 'quitbnc':
						exit;
						break; //lol
					case 'quit':
						$this->buf='';
						$this->sc1=0;
						$this->timelimit=$GLOBALS['config']->config['timelimit']+time();
						break;
					case 'retrieve':
						preg_match('/([^ ]+) ([^'."\n".']+)/i',$tmp[2],$out);
						$GLOBALS['waiting'][$out[1]]=new wait($this->fp,$out[1],$out[2],$this->name);
						$this->fp=NULL;
						$this->sc1=0;
						$this->timelimit=-1;
						break;
					case 'nick':
						if(!$this->sc2){
							$tmpname=($tmp[2][0]==':')?substr($tmp[2],1,strlen($tmp[2])):$tmp[2];
							socket_write($this->fp,':'.$this->nick.' NICK :'.$tmpname.el);
							$this->nick=$tmpname;
						}
						break;
					case 'connect':
						if(preg_match('/:(([^'."\n".']+))/i',$tmp[2],$out)){
							$this->port=$out[1];
							$tmp[2]=substr($tmp[2],0,strpos($tmp[2],':'));
						}
						if($this->sc2){
							@socket_write($this->fp2,"QUIT :PHP BNC by MagicKing\n");
							@socket_close($this->fp2);
						}
						$this->fp2=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
						socket_connect($this->fp2,$tmp[2],$this->port);
						$this->sc2=1;
						$this->buf2='';
						$this->i2=0;
						$this->buf='NICK '.$this->nick."\nUSER anonymous \"phpbnc\" \"*.*.*.*\" :PHP BNC\n";
						socket_clear_error($this->fp2);
						break;
					case 'unconnect':
						$this->sc1=0;
						$this->sc2=0;
						return 1;
						break; //lol
					case 'user':
						$this->user=$tmp[2];
						socket_write($this->fp,$this->Sfirst);
						break;
					}
					if($this->sc2 && $this->fp2!=NULL)socket_write($this->fp2,$this->buf);
					$this->buf='';
					$this->i=-1;
				}
				$this->i++;
			}

		}
		
		if($this->sc2){
			$read=array($this->fp2);
			if (false === ($sockets_modifie = socket_select($read , $w=NULL, $e=NULL, 0))) {
	
			}else if($sockets_modifie > 0) {
				$this->buf2.=@socket_read($read[0],1);
				if($this->buf2[$this->i2]=="\n"){
					preg_match('/:([^ ]+) (.*?)$/i',$this->buf2,$tmp);
					if((strtolower($tmp[1])!=$this->nick) && (false===strpos($tmp[1],'!')))$this->buf2=preg_replace('/:([^ ]+) (.*?)$/i',':'.$this->server.' \\2',$this->buf2);
					if(preg_match('/:([^!]+)([^ ]+) ([^ ]+) (:|)([^'."\r".']+)/i',$this->buf2,$tmp))
					switch(strtolower($tmp[3])){
					case 'nick':
						$this->nick=$tmp[5];
						break;
					case 'join':
						$this->chan[$tmp[5]]=new channel($tmp[1],$tmp[2],$tmp[5]);
						$this->buf2;
						break;
					case 'part':
						unset($this->chan[$tmp[5]]);
						break;
					}
					if(substr($this->buf2,0,4)=='PING'){$this->buf2[1]='O';@socket_write($this->fp2,$this->buf2);}
					else if($this->fp!=NULL && $this->sc1)@socket_write($this->fp,$this->buf2);
					$this->buf2='';
					$this->i2=-1;
				}
				$this->i2++;
			}
		}
		if(($this->fp==NULL || socket_last_error($this->fp)!=0) && $this->timelimit==0){$this->fp=NULL;$this->sc1=0;$this->timelimit=$GLOBALS['config']->config['timelimit']+time();}
		if($this->fp2==NULL || socket_last_error($this->fp2)!=0)$this->sc2=0;
		if($this->fp2!=NULL)socket_clear_error($this->fp2);
		if($this->fp!=NULL)socket_clear_error($this->fp);
		
		if((!$this->sc1 && ($this->timelimit-time())<0) || (!$this->sc1 && !$this->sc2))return 1;
		return 0;
	}
}
class webserver{
	var $port;
	var $bind;
	var $fp;
	var $child=Array();
	var $reset=0;
	
	function __construct($port,$bind='0.0.0.0'){
		$this->port=$port;
		$this->bind=$bind;
		
		if(!($this->fp=socket_create(AF_INET,SOCK_STREAM,SOL_TCP)))return false;
		if(!(socket_bind($this->fp,$this->bind,$this->port)))return false;
		if(!(socket_listen($this->fp)))return false;
		return true;
		
	}
	
	function check(){
		$r = array($this->fp);
		if (false === ($sockets_modifie = socket_select($r, $w = NULL, $e = NULL, 0))) {
		}else if($sockets_modifie) {
			$this->child[]=new child(socket_accept($this->fp));
			$this->reset=1;
		}
		
		reset($this->child);
		while(list($key,$value)=each($this->child))
			if($value->check())unset($this->child[$key]);
		
		if(!count($this->child) && $this->reset){
			$this->child=Array();
			$this->reset=0;
		}
	}
}

class child{
	var $fp;
	var $buf='';
	var $i=0;
	var $header=Array();
	var $ok=0;
	var $global=Array();
	
	function __construct($fp){
		$this->fp=$fp;
		$this->header[0]='HTTP/1.0 ';
		$this->header[1]=gmdate('r').el;
		$this->header[2]='Server: PHP BNC Server'.el;
		$this->header[3]='Content-Type: text/html'.el.el;
		$this->header[4]='';
		$this->header[5]='';
	}
	
	function __destruct(){
		@socket_close($this->fp);
	}
	
	function check(){
		$r = array($this->fp);
		if (false === ($sockets_modifie = socket_select($r, $w = NULL, $e = NULL, 0))) {
		}else if($sockets_modifie){
			$this->buf.=@socket_read($this->fp,1);
			if(($this->buf[$this->i-1]=="\n" || $this->buf[$this->i-1]=="\r") && ($this->buf[$this->i]=="\n" || $this->buf[$this->i]=="\r") && !$this->ok){
				preg_match('/GET ([^ ]+)/i',$this->buf,$out);
				$out=str_replace(array('\\','//'),array('/','/'),$out[1]);
				if(!(strpos($out,'?')===false)){
					$var=substr($out,strpos($out,'?')+1,strlen($out));
					$out=substr($out,0,strpos($out,'?'));
					preg_match_all('/([^=]+)=([^&]+)/i',$var,$out1);
					for($i=0;$i<count($out1[1]);$i++)
						$this->global[str_replace('&','',$out1[1][$i])]=urldecode(str_replace(array('..','='),'',$out1[2][$i]));
				}
				if(isset($this->global['name']))$GLOBALS['config']->config['name']=$this->global['name'];
				if(isset($this->global['quit']))exit;
				if($out=='/')$out='index.tpl';
				
				if(!file_exists($GLOBALS['config']->config['tpldir'].$out)){
					$this->header[0].='404 Not Found'.el;
					$temp = new template($GLOBALS['config']->config['tpldir'].'404.tpl');
				}else
				{
					$this->header[0].='200 OK'.el;
					if(substr($out,-4)=='.tpl'){
						$temp = new template($GLOBALS['config']->config['tpldir'].$out);
						$temp->assign_array($GLOBALS['config']->config);
					}else $temp = new template($GLOBALS['config']->config['tpldir'].$out,1);
				}
				$this->header[4]=$temp->retour();
				$this->ok=1;
			}
			
			if(($this->buf[$this->i-1]=="\n" || $this->buf[$this->i-1]=="\r") && ($this->buf[$this->i]=="\n" || $this->buf[$this->i]=="\r") && ($this->buf[$this->i-3]=="\n" || $this->buf[$this->i-3]=="\r") && ($this->buf[$this->i-2]=="\n" || $this->buf[$this->i-2]=="\r") && ($this->ok)){
				@socket_write($this->fp,implode('',$this->header));
				@socket_close($this->fp);
				return 1;
			}
			
			$this->i++;
		}
		if(socket_last_error($this->fp)!=0)return 1;
		return 0;
	}
}
class wait{
	var $fp;
	var $id;
	var $pass;
	var $name;
	
	function __construct($fp,$id,$pass,$name){
		$this->id=$id;
		$this->fp=$fp;
		$this->pass=$pass;
		$this->name=$name;
	}
}

class channel{
	var $name;
	var $addr;
	var $chan;
	
	function __construct($name,$addr,$chan){
		$this->name=$name;
		$this->addr=$addr;
		$this->chan=$chan;
	}
	
	function join($fp,$fp2){
		socket_write($fp,':'.$this->name.'!'.$this->addr.' JOIN :'.$this->chan.el);
		socket_write($fp2,'NAMES '.$this->chan."\n");
	}
}
if(!($fp=@fopen('config.php','r')))$config=new config('config.php');
else {while(!feof($fp))$tmp.=fread($fp,512);$config=unserialize($tmp);}
$config->config['timestart']=time();
$config->exec();

$waiting=Array();

$classarray=Array();
$fpp=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_bind($fpp,'0.0.0.0',6667);
socket_listen($fpp);
echo 'Listening...'.el;
$i=0;

if(false===($classarray[]=new webserver(80)))$classarray=Array();

while(1){
	$r = array($fpp);
	if (false === ($sockets_modifie = socket_select($r, $w = NULL, $e = NULL, 0))) {
	}else if($sockets_modifie) {
		$fp=socket_accept($fpp);
		if(count($classarray)<=$config->config['maxconnection']){
			$i++;
			$classarray[]=new user($fp,$i);
		}else{
			socket_close($fp);
		}
		
	}
	
	reset($classarray);
	while(list($key,$value)=each($classarray)){
		if($value->check())unset($classarray[$key]);
		$config->config['active']=count($classarray);
	}
	usleep(1); //CPU Overload protection
}
socket_close($fpp);
?>

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.