<?php
/**
 * ThunderSnarf
 *
 * @author Ricardo H.F. Sette P. <ricardohfsp@gmail.com>
 * @link http://www.freebsdbrasil.com.br/
 * @copyright Copyright &copy; 2010 - FreeBSD Brasil LTDA
 * @license licença GNU/GPL v2.0 http://www.gnu.org/licenses/gpl-2.0.html
 */
?>
<?php

class RuntimeController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to 'column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='column1';

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('thundercache'),
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Relatorio do lusca.
	 */
	public function actionThundercache()
	{
		$teste = "root          72567  0.0  0.1 17980  5984  ??  I     4:02PM   0:00.41 thunder: thunder 127.0.0.1 i3.ytimg.com/vi/j_w2jU5rlQw/default.jpg (thunder)
root          72568  0.1  0.2 17980  5812  ??  I     4:02PM   0:00.03 thunder: 127.0.0.1 video.globo.com/Portal/videos/cda/js/glb_videos.js (thunder)
root          72569  0.0  0.1 17980  5812  ??  I     4:02PM   0:00.00 thunder: 127.0.0.1 video.globo.com/Portal/cda/css/0,,7823-xhtmls,00.css (thunder)
root          72570  0.0  0.1 17980  5812  ??  I     4:02PM   0:00.00 thunder: 127.0.0.1 video.globo.com/Portal/gmc4/cda/player/js/glb_gmc4_player_embed_javascript (thunder)
root          72571  0.0  0.1 17980  5812  ??  I     4:02PM   0:00.00 thunder: 127.0.0.1 video.globo.com/Portal/cda/Barra/css/barra4.css (thunder)
root          72572  0.0  0.1 17980  5812  ??  I     4:02PM   0:00.00 thunder: 127.0.0.1 video.globo.com/Portal/cda/Barra/css/barra_videos3.css (thunder)
root          72573  0.0  0.1 17980  5924  ??  I     4:02PM   0:00.08 thunder: 127.0.0.1 i2.ytimg.com/vi/eqRfnV2Q5wc/default.jpg (thunder)
root          72574  0.0  0.1 17980  5928  ??  I     4:02PM   0:00.02 thunder: 127.0.0.1 www.youtube.com/v/ILwU5GhY9MI (thunder)
root          72575  0.0  0.1 17980  5884  ??  I     4:02PM   0:00.03 thunder: 127.0.0.1 www.youtube.com/v/ILwU5GhY9MI (thunder)
root          72576  0.0  0.1 17980  5876  ??  I     4:02PM   0:00.03 thunder: 127.0.0.1 i4.ytimg.com/vi/3hAIOnQ-DD4/default.jpg (thunder)
root          72577  0.0  0.1 17980  5844  ??  I     4:02PM   0:00.01 thunder: 127.0.0.1 i.ytimg.com/vi/7ncfzNqZC4A/default.jpg (thunder)
root          72578  0.0  0.1 17980  5860  ??  I     4:02PM   0:00.01 thunder: 127.0.0.1 i2.ytimg.com/crossdomain.xml (thunder)
root          72579  0.0  0.1 17980  5844  ??  I     4:02PM   0:00.00 thunder: 127.0.0.1 www.youtube.com/xlb/watch/strings-pt_BR.xlb (thunder)
root          72580  0.0  0.1 17980  5848  ??  I     4:02PM   0:00.00 thunder: 127.0.0.1 i.ytimg.com/vi/6tBfXixTY5M/default.jpg (thunder)
root          72581  0.0  0.1 17980  5852  ??  I     4:02PM   0:00.24 thunder: 127.0.0.1 s.ytimg.com/yt/swf/endscreen-vfl188392.swf (thunder)
root          72582  0.0  0.1 17980  5832  ??  I     4:02PM   0:00.00 thunder: 127.0.0.1 i3.ytimg.com/vi/ZWsk2CJ7o6A/default.jpg (thunder)
root          72583  0.0  0.1 17980  5860  ??  I     4:02PM   0:00.01 thunder: 127.0.0.1 s.ytimg.com/yt/swf/cps-vfl187250.swf (thunder)
root          72584  0.0  0.1 17980  5848  ??  I     4:02PM   0:00.00 thunder: 127.0.0.1 i2.ytimg.com/vi/ILwU5GhY9MI/hqdefault.jpg (thunder)
root          72581  0.0  0.1 17980  5856  ??  S     4:02PM   0:00.25 thunder: 127.0.0.1 au.download.windowsupdate.com/msdownload/update/software/secu/2008/10/msxml4-kb954430-enu_c40d3e3035b8cea714f27532fea37c05b2d4dc65.exe (thunder)
root          72582  0.0  0.1 17980  5884  ??  S     4:02PM   0:00.03 thunder: 127.0.0.1 dc146.4shared.com/download/jAE0VLTA/Demnios_da_Garoa_-_Samba_itali.mp3?tsid=20100908-160637-477f608a (thunder)
root          72583  0.0  0.1 17980  5880  ??  S     4:02PM   0:00.02 thunder: 127.0.0.1 au.download.windowsupdate.com/msdownload/update/software/svpk/2009/04/dotnetfx35_x86_1aab3734ef80ad9a9ea342c887c3ea76340961e8.exe (thunder)
root          72584  0.0  0.1 17980  5860  ??  S     4:02PM   0:00.01 thunder: 127.0.0.1 armdl.adobe.com/pub/adobe/reader/win/9.x/9.3.4/misc/AdbeRdrUpd934_all_incr.msp (thunder)
root          72585  0.0  0.1 17980  5932  ??  S     4:02PM   0:00.02 thunder: 127.0.0.1 img.youtube.com/vi/vYqBWGTkbFY/default.jpg?h=60&w=80&sigh=__uFdiYLqjL4N0QDe0yuePxAw4UY8= (thunder)
root          72586  0.0  0.1 17980  5892  ??  S     4:02PM   0:00.02 thunder: 127.0.0.1 dc110.4shared.com/servlet/ProgressStatus?dcID=110&sId=tNHfe53J7Ne4RDiG&globSysLang=en&random=0.05884859870010495 (thunder)
root          72587  0.0  0.1 17980  5940  ??  S     4:02PM   0:00.03 thunder: 127.0.0.1 mail.globo.com/mail/channel/bind?VER=8&at=AF6bupNGsat7J48gsrOIKL6ajr8-F_eOYg&it=495452&RID=rpc&SID=A5916E1DD78C198B&CI=1&AID=61&TYPE=html&zx=oxxisd-r1avsl&DOMAIN&t=1 (thunder)
root          72588  0.0  0.1 17980  5888  ??  S     4:02PM   0:00.02 thunder: 127.0.0.1 wm-ttv-mirror03-mia.terra.com/kboing/877103.wmv (thunder)
root          72589  0.0  0.1 17980  5956  ??  S     4:02PM   0:00.02 thunder: 127.0.0.1 au.download.windowsupdate.com/msdownload/update/software/secu/2010/04/windowsxp-kb978542-x86-ptb_7ccc05fb4aae9f06cb31bc4f84631884088ef7fc.psf (thunder)
root          72590  0.0  0.1 17980  5856  ??  S     4:02PM   0:00.01 thunder: 127.0.0.1 meusjogosdemeninas.uol.com.br/games/menina-virtual.swf (thunder)
root          72591  0.0  0.1 17980  5876  ??  S     4:02PM   0:00.01 thunder: 127.0.0.1 img.youtube.com/vi/qK9IqVKvrGU/default.jpg?h=60&w=80&sigh=__2b-eznBQ2puUd1qZfUN8MNwhBpA= (thunder)
root          72592  0.0  0.1 17980  5896  ??  S     4:02PM   0:00.01 thunder: 127.0.0.1 au.download.windowsupdate.com/msdownload/update/software/secu/2010/04/windowsxp-kb978542-x86-ptb_7ccc05fb4aae9f06cb31bc4f84631884088ef7fc.psf (thunder)
root          72593  0.0  0.1 17980  5884  ??  S     4:02PM   0:00.01 thunder: 127.0.0.1 au.download.windowsupdate.com/msdownload/update/software/secu/2010/04/windowsxp-kb978542-x86-ptb_7ccc05fb4aae9f06cb31bc4f84631884088ef7fc.psf (thunder)
root          72594  0.0  0.1 17980  5884  ??  S     4:02PM   0:00.01 thunder: 127.0.0.1 au.download.windowsupdate.com/msdownload/update/software/secu/2010/04/windowsxp-kb978542-x86-ptb_7ccc05fb4aae9f06cb31bc4f84631884088ef7fc.psf (thunder)
root          72595  0.0  0.1 17980  5848  ??  S     4:02PM   0:00.01 thunder: 127.0.0.1 au.download.windowsupdate.com/msdownload/update/software/secu/2010/04/windowsxp-kb978542-x86-ptb_7ccc05fb4aae9f06cb31bc4f84631884088ef7fc.psf (thunder)
root          72596  0.0  0.1 17980  5892  ??  S     4:02PM   0:00.01 thunder: 127.0.0.1 dc110.4shared.com/images/progress_empty.gif (thunder)
root          72597  0.0  0.1 17980  5884  ??  S     4:02PM   0:00.01 thunder: 127.0.0.1 au.download.windowsupdate.com/msdownload/update/software/secu/2010/04/windowsxp-kb978542-x86-ptb_7ccc05fb4aae9f06cb31bc4f84631884088ef7fc.psf (thunder)
root          72598  0.0  0.1 17980  5884  ??  S     4:02PM   0:00.01 thunder: 127.0.0.1 wm-ttv-mirror03-mia.terra.com/kboing/1076214.wmv (thunder)
root          72599  0.0  0.1 17980  5852  ??  S     4:02PM   0:00.01 thunder: 127.0.0.1 au.download.windowsupdate.com/msdownload/update/software/secu/2010/04/windowsxp-kb978542-x86-ptb_7ccc05fb4aae9f06cb31bc4f84631884088ef7fc.psf (thunder)
root          72600  0.0  0.1 17980  5864  ??  S     4:02PM   0:00.01 thunder: 127.0.0.1 g.microsoft.com/_0sfdata/1?aa=1:07:27:07&ab=1136&ac=126&ad=0&ae=54&af=72&ag=217&ah=0&ai=49&aj=100&ak=0&al=16&nr=17&cg=%7bb3bb5bba-e7d5-40ab-a041-a5b1c0b26c8f%7d (thunder)";


                $myDataFreeBSD = array();
                $comando = '/bin/ps auxwwwwww | grep "thunder" | grep -v waiting | grep -v grep | grep -v "main process"';
                $erro = 0;
		exec($comando,$retornoComandoThunder,$erro);
                //$retornoComandoThunder = explode("\n", $teste);
		if(count($retornoComandoThunder)>0){
                        if(count($retornoComandoThunder)>=2){
                                array_shift($retornoComandoThunder);
                                array_shift($retornoComandoThunder);
                        }
			foreach($retornoComandoThunder as $key=>$valor){
                                //root    30287  0.0  1.0  6756  4816  ??  S     8:33AM   0:00.19 thunder: 10.69.69.3 www.vericia.com/appsearch/?city=Belo%20horizonte&state=MG&neighborhood=Sagrada%20Familia&&&qtd=4&home_id=15981719&home_type=2 (thunder)
				$valor = preg_replace('/\s\s+/', ' ', $valor);
				preg_match('/^(.*)\s(.*)\s(.*)\s(.*)\s(.*)\s(.*)\s(.*)\s(.*)\s(.*)\s(.*)\s(.*)/',$valor, $dados);
                                $dados = explode(" ", $valor);
				$url_inicio = "";
				$url_final = "";
				$url_link = "";
				$USER = array_shift($dados);
				$PID = array_shift($dados);
				$CPU = array_shift($dados);
				$MEM = array_shift($dados);
				$VSZ = array_shift($dados);
				$RSS = array_shift($dados);
				$TT = array_shift($dados);
				$STAT = array_shift($dados);
				$STARTED = array_shift($dados);
				$TIME = array_shift($dados);
				array_shift($dados);
				$IP = array_shift($dados);
				$URL = array_shift($dados);
				$parenteses = array_shift($dados);
				if(strlen($URL)>10 AND strpos($URL, "/", 10)>0){
					$url_inicio = 'http://'.substr($URL,0,strpos($URL, "/", 10))."/";
					while(strlen($url_inicio)>20){
						$url_inicio = substr($url_inicio,0,-1);
					}
					$url_final = substr($URL,strrpos($URL, "/")+1);
					while(strlen($url_final)>20){
						$url_final = substr($url_final,1);
					}
					if(in_array(substr($url_final,-3), array('gif','peg','bin','png','jpg','jpeg','exe','.gz','tar','zip','bz2','avi','flv','xml','wmv','wma','cab','pdf','.js','swf','exe'))){
						$url_extensao = substr($url_final,-3);
						$url_final = substr($url_final,0,-3)."<b>".$url_extensao."</b>";
					}
					$url_link = "<a href='http://".$URL."' title='".$URL."' alt='".$URL."' target='_blank'>(.....)</a>";
				}else{
					$url_inicio = 'http://'.$URL;
				}
				$myDataFreeBSD[] = array(
					"user"=>$USER,
					"pid"=>$PID,
					"cpu"=>$CPU,
					"mem"=>$MEM,
					"vsz"=>$VSZ,
					"rss"=>$RSS,
					"tt"=>$TT,
					"stat"=>$STAT,
					"started"=>$STARTED,
					"time"=>$TIME,
					"ip"=>$IP,
					"url"=>$url_inicio.$url_link.$url_final." ".$parenteses,
				);
			}
		}

                 /*
                 Fork[86].id 19079
                    Fork[86].status CONNECTED
                    Fork[86].connects 235
                    Fork[86].totalread 25
                    Fork[86].totaltrans 737955
                    Fork[86].cache_type NONE
                    Fork[86].cache_response CANCEL
                    Fork[86].domain
                    Fork[86].file

                    Fork[178].id 25608
                    Fork[178].status CONNECTED
                    Fork[178].connects 20
                    Fork[178].totalread 3027
                    Fork[178].totaltrans 56766
                    Fork[178].cache_type NONE
                    Fork[178].cache_response CANCEL
                    Fork[178].domain
                    Fork[178].file
                  */
                $myDataThunder = array();
                //$comando = 'curl --proxy 201.48.151.226:8012 "http://127.0.0.1/thunderlocalinfo"';
                $comando = '/usr/local/bin/curl --proxy 127.0.0.1:8012 "http://127.0.0.1/thunderlocalinfo"';
		$erro = 0;
                $retornoComandoThunder = "";
		exec($comando,$retornoComandoThunder,$erro);
                $numeroAnterior = null;
                if(count($retornoComandoThunder)>0){
                    $id = '';
                    $status = '';
                    $connects = '';
                    $totalread = '';
                    $totaltrans = '';
                    $cache_type = '';
                    $cache_response = '';
                    $domain = '';
                    $file = '';
                    foreach($retornoComandoThunder as $key=>$valor){
                        $valor = preg_replace('/\s\s+/', ' ', $valor);
                        if(trim($valor)!=''){
                            preg_match('/^.*\[(.*)\]\.(.*)/',$valor, $dados);
                            //echo "<pre>";print_r($dados);echo "</pre>";
                            array_shift($dados);
                            $numero = array_shift($dados);
                            if(is_null($numeroAnterior)){
                                $numeroAnterior = $numero;
                            }
                            if($numeroAnterior!=$numero){
                                $numeroAnterior=$numero;
                                if($status!='WAITING'){
                                    array_unshift($myDataThunder, array(
                                            "id"=>$id,
                                            "status"=>$status,
                                            "connects"=>$connects,
                                            "totalread"=>$totalread,
                                            "totaltrans"=>$totaltrans,
                                            "cache_type"=>$cache_type,
                                            "cache_response"=>$cache_response,
                                            "domain"=>$domain,
                                            "file"=>$file,
                                    ));
                                }else{
                                    $myDataThunder[] = array(
                                            "id"=>$id,
                                            "status"=>$status,
                                            "connects"=>$connects,
                                            "totalread"=>$totalread,
                                            "totaltrans"=>$totaltrans,
                                            "cache_type"=>$cache_type,
                                            "cache_response"=>$cache_response,
                                            "domain"=>$domain,
                                            "file"=>$file,
                                    );
                                }
                                $id = '';
                                $status = '';
                                $connects = '';
                                $totalread = '';
                                $totaltrans = '';
                                $cache_type = '';
                                $cache_response = '';
                                $domain = '';
                                $file = '';
                            }
                            $tipo = array_shift($dados);
                            if(substr($tipo,0,2)=='id')
                                $id = substr($tipo,2);
                            elseif(substr($tipo,0,6)=='status')
                                $status = substr($tipo,7);
                            elseif(substr($tipo,0,8)=='connects')
                                $connects = substr($tipo,9);
                            elseif(substr($tipo,0,9)=='totalread')
                                $totalread = substr($tipo,10);
                            elseif(substr($tipo,0,10)=='totaltrans')
                                $totaltrans = substr($tipo,11);
                            elseif(substr($tipo,0,10)=='cache_type')
                                $cache_type = substr($tipo,11);
                            elseif(substr($tipo,0,14)=='cache_response')
                                $cache_response = substr($tipo,15);
                            elseif(substr($tipo,0,6)=='domain')
                                $domain = substr($tipo,7);
                            elseif(substr($tipo,0,4)=='file')
                                $file = substr($tipo,5);
                        }
                    }
                }
		$this->render('thundercache',array(
			'modelThunderCache'=>$myDataThunder,
			'modelFreeBSD'=>$myDataFreeBSD,
		));
	}
}
