Location: PHPKode > scripts > pvpgn_mail > pvpgn_mail/bnetmail.class.php
<?php
 
 class bnetMail{
     var $error;
     private $pvpgn;
     private $mail_path;
     
     private $_db;
     private $_user;
     private $_pass;
     private $_cmd;
     private $_host;
     
     function bnetMail($pvpgn_path){
         if ($pvpgn_path[strlen($pvpgn_path)-1] != "/"){
             $pvpgn_path .= "/";
         }
         $this->pvpgn = $pvpgn_path;
         $this->mail_path =  $pvpgn_path ."var/bnmail/";
         
         $this->getConf();
         $this->_db = $this->get_command("prefix") . $this->get_command("name");
         $this->_user = $this->get_command("user");
         $this->_pass = $this->get_command("pass");
     }
     
     private function getConf(){
        
        $file = $this->pvpgn ."/conf/bnetd.conf";
        if (file_exists($file)){
            $lineas = file($file);
            $c = 0; $conf = null;
            foreach ($lineas as $linea){
                $linea = trim($linea);
                $first = substr($linea,0,1);
                if ($first != "#" ){
                    if ($first != false){
                        $temp = explode("=",$linea);
                        if (substr_count($linea,"host") >0 ){
                             $temp = explode(";",$linea);
                             foreach ($temp as $l){
                                $temp1 = explode("=",$l); 
                                $comm = trim($temp1[0]); $v =str_replace("\\","/",trim($temp1[1]));
                                $command[$comm] = $v; 
                             }
                             $conf .= $linea. "\r\n"; 
                             continue;
                        }  
                        $comm = trim($temp[0]); $v =str_replace("\\","/",trim($temp[1]));
                        $command[$comm] = $v;
                        $c += 1; 
                        $conf .= $comm. " = " .$v. "\r\n";
                    }
                }
            }
            
            $this->_cmd = $command;
        }else{
            $this->error = "File not found: $file";
        } 
     }
     
     private function get_command($BnetCommand){
          if (isset($this->_cmd[$BnetCommand])){
                return $this->_cmd[$BnetCommand];  
          }else{
                $this->error = "Command NOT FOUND";
          }
          
     }
      
     function sendMailById($idrecipient,$sender,$msg){
         $file = "00000". time();
         $dir = $this->mail_path . substr("000000",0,(6-strlen($idrecipient))) .$idrecipient ;
         if (!is_dir($dir) ){
             //no existe, crear directorio;  // make mails dir
             if (mkdir($dir) == false){
                 $this->error = "No se pudo crear el directorio " .$dir;
             }
         }else{
                 $msg = $sender ."\n". $msg ."\n";
                 
                 // dos mensajes en el mismo time.  // too messages at same time
                 $c = 1;
                 while(file_exists($dir."/".$file)){
                      $file = "00000". (time()+ $c);
                      $c +=1;
                 }
                                           
                 file_put_contents($dir."/".$file,$msg);
         }             
         
     }
     
     function sendMailByName($nameRecipient,$sender,$msg){
         $bd = $this->_db;
         $userbd = $this->_user;
         $passbd = $this->_pass;
         $host = $this->_host;
         
         //conectar a la BD y obtener ID
         $conexion = mysql_connect ($host, $userbd, $passbd);
         mysql_select_db ($bd);                                                 
         $instruccion = "select * from bnet WHERE username =  '$nameRecipient'";
         
         $consulta = mysql_query ($instruccion, $conexion);
         if ($consulta == false){
             $this->error = "Fallo en la consulta";
             return false;
         }
         
         $nfilas = mysql_num_rows ($consulta);
         
         $resultado = mysql_fetch_array($consulta);
         $id = $resultado['uid'];
         mysql_close ($conexion); 
                  
         $this->sendMailById($id,$sender,$msg);
     }
     
     function getMails($userId){
         $folder = $this->pvpgn ."var/bnmail/" .substr("000000",0,(6-strlen($userId))) .$userId;
         if (is_dir($folder) ){ 
              $dir = opendir($folder);
              $c = 0;
              while (false !== ($archivo = readdir($dir))) {
                  if ($archivo != ".."){
                      if ($archivo != "." ){
                          $arch[] =  $archivo;
                      }
                  }
              }
              closedir($dir);
              
              $cantemail = count($arch);
              if (isset($arch)){
                  $inf = null;
                  $c = 0;
                  foreach($arch as $archivo){
                      $file = $folder."/".$archivo ;
                      $temp = file($file);
                      $fecha = date('Y-m-d', $archivo);
                      $hora = idate('H', $archivo) .":".idate('i', $archivo);
                      
                      $remitente = trim($temp[0]); 
                      unset($temp[0]);  
                      $mensaje .= implode("<br />",$temp) ."<br />";
                      
                      $mails[$c]['file'] = $file;
                      $mails[$c]['date'] = $fecha;
                      $mails[$c]['time'] = $hora;
                      $mails[$c]['sender'] = $remitente;
                      $mails[$c]['message'] = $mensaje;
                       
                      $c +=1;
                  }
                return $mails;
              } 
         }
         return null;
     } // end getMails
     
     function broadCast($mail){
         
     }
     
     
 } // en class
 
 //$mail = new bnetMail("D:/Users/Prof.Luis/Programas/pvpgn-1.8.5/");
 //$mensajes = $mail->getMails(1);
 
 
 // enviar BnetMails
 /*$mail->sendMailById(1,"pepe","hola");
 $mail->sendMailById(1,"pepe1","hola1");
 
 $mail->sendMailByName("root","tank","hola");
 
 if ($mail->error != null){
     echo $mail->error;
 }else{
     echo "Mensaje enviado correctamente";   
 }*/
?>
Return current item: pvpgn_mail