Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
executable file 4681 lines (3762 sloc) 164 KB
<?php
if(isset($_GET["verbose"])){ini_set_verbosedx();}else{ ini_set('display_errors', 0);ini_set('error_reporting', 0);}
$GLOBALS["KAV4PROXY_NOSESSION"]=true;
$GLOBALS["OUTPUT"]=false;
$GLOBALS["FORCE"]=false;
$GLOBALS["RELOAD"]=false;
$GLOBALS["RESTART"]=false;
$GLOBALS["TITLENAME"]="URLfilterDB daemon";
$_GET["LOGFILE"]="/var/log/artica-postfix/dansguardian.compile.log";
if(posix_getuid()<>0){
//$GLOBALS["VERBOSE"]=true;
if(isset($_POST["smtp-send-email"])){parseTemplate_smtp_post();exit;}
if(isset($_POST["unlock-www"])){parseTemplate_unlock_save();exit;}
if(isset($_GET["unlock"])){parseTemplate_unlock();exit;}
if(isset($_GET["SquidGuardWebAllowUnblockSinglePass"])){parseTemplate_SinglePassWord();die();}
if(isset($_GET["smtp-send-js"])){parseTemplate_sendemail_js();exit;}
if(isset($_REQUEST["send-smtp-notif"])){parseTemplate_sendemail_perform();exit;}
if(isset($_POST["USERNAME"])){parseTemplate_LocalDB_receive();die();}
if(isset($_GET["SquidGuardWebUseLocalDatabase"])){parseTemplate_LocalDB();die();}
parseTemplate();die();}
if(preg_match("#--ouput#",implode(" ",$argv),$re)){$GLOBALS["OUTPUT"]=true;}
if(preg_match("#--schedule-id=([0-9]+)#",implode(" ",$argv),$re)){$GLOBALS["SCHEDULE_ID"]=$re[1];}
$GLOBALS["GETPARAMS"]=@implode(" Params:",$argv);
$GLOBALS["CMDLINEXEC"]=@implode("\nParams:",$argv);
include_once(dirname(__FILE__)."/ressources/class.user.inc");
include_once(dirname(__FILE__)."/ressources/class.groups.inc");
include_once(dirname(__FILE__)."/ressources/class.ldap.inc");
include_once(dirname(__FILE__)."/ressources/class.system.network.inc");
include_once(dirname(__FILE__)."/ressources/class.dansguardian.inc");
include_once(dirname(__FILE__)."/ressources/class.squid.inc");
include_once(dirname(__FILE__)."/ressources/class.squidguard.inc");
include_once(dirname(__FILE__)."/ressources/class.mysql.inc");
include_once(dirname(__FILE__)."/ressources/class.compile.ufdbguard.inc");
include_once(dirname(__FILE__)."/ressources/class.compile.dansguardian.inc");
include_once(dirname(__FILE__).'/framework/class.unix.inc');
include_once(dirname(__FILE__)."/framework/frame.class.inc");
include_once(dirname(__FILE__).'/ressources/class.ufdbguard-tools.inc');
include_once(dirname(__FILE__)."/ressources/class.os.system.inc");
if(posix_getuid()<>0){die("Cannot be used in web server mode\n\n");}
if(count($argv)>0){
$imploded=implode(" ",$argv);
if(preg_match("#--(output|ouptut)#",$imploded)){
$GLOBALS["OUTPUT"]=true;
}
if(preg_match("#--verbose#",$imploded)){
$GLOBALS["VERBOSE"]=true;$GLOBALS["debug"]=true;
$GLOBALS["OUTPUT"]=true;ini_set_verbosed();
}
if(preg_match("#--reload#",$imploded)){$GLOBALS["RELOAD"]=true;}
if(preg_match("#--force#",$imploded)){$GLOBALS["FORCE"]=true;}
if(preg_match("#--shalla#",$imploded)){$GLOBALS["SHALLA"]=true;}
if(preg_match("#--restart#",$imploded)){$GLOBALS["RESTART"]=true;}
if(preg_match("#--catto=(.+?)\s+#",$imploded,$re)){$GLOBALS["CATTO"]=$re[1];}
if($argv[1]=="--disks"){DisksStatus();exit;}
if($argv[1]=="--version"){checksVersion();exit;}
if($argv[1]=="--dump-adrules"){dump_adrules($argv[2]);exit;}
if($argv[1]=="--dbmem"){ufdbdatabases_in_mem();exit;}
if($argv[1]=="--notify-start"){ufdguard_start_notify();exit;}
if($argv[1]=="--artica-db-status"){ufdguard_artica_db_status();exit;}
$argvs=$argv;
unset($argvs[0]);
if($argv[1]=="--stop"){stop_ufdbguard();exit;}
if($argv[1]=="--reload"){build_ufdbguard_HUP();exit;}
if($argv[1]=="--reload-ufdb"){build_ufdbguard_HUP();exit;}
if($argv[1]=="--dansguardian"){buildDans();exit;}
if($argv[1]=="--databases-status"){databases_status();exit;}
if($argv[1]=="--ufdbguard-status"){print_r(UFDBGUARD_STATUS());exit;}
if($argv[1]=="--cron-compile"){cron_compile();exit;}
if($argv[1]=="--compile-category"){UFDBGUARD_COMPILE_CATEGORY($argv[2]);exit;}
if($argv[1]=="--compile-all-categories"){UFDBGUARD_COMPILE_ALL_CATEGORIES();exit;}
if($argv[1]=="--ufdbguard-recompile-dbs"){echo UFDBGUARD_COMPILE_ALL_CATEGORIES();exit;}
if($argv[1]=="--phraselists"){echo CompileCategoryWords();exit;}
if($argv[1]=="--fix1"){echo FIX_1_CATEGORY_CHECKED();exit;}
if($argv[1]=="--bads"){echo remove_bad_files();exit;}
if($argv[1]=="--reload131"){exit;}
$GLOBALS["EXECUTEDCMDLINE"]=@implode(" ", $argvs);
ufdbguard_admin_events("receive ".$GLOBALS["EXECUTEDCMDLINE"],"MAIN",__FILE__,__LINE__,"config");
if($GLOBALS["VERBOSE"]){echo "Execute ".@implode(" ", $argv)."\n";}
if($argv[1]=="--inject"){echo inject($argv[2],$argv[3]);exit;} // category filepath
if($argv[1]=="--parse"){echo inject($argv[2],$argv[3],$argv[4]);exit;}
if($argv[1]=="--conf"){echo build();exit;}
if($argv[1]=="--ufdb-monit"){echo ufdbguard_watchdog();exit;}
if($argv[1]=="--ufdbguard-compile"){echo UFDBGUARD_COMPILE_SINGLE_DB($argv[2]);exit;}
if($argv[1]=="--ufdbguard-dbs"){echo UFDBGUARD_COMPILE_DB();exit;}
if($argv[1]=="--ufdbguard-miss-dbs"){echo ufdbguard_recompile_missing_dbs();exit;}
if($argv[1]=="--ufdbguard-schedule"){ufdbguard_schedule();exit;}
if($argv[1]=="--ufdbguard-start"){ufdbguard_start();exit;}
if($argv[1]=="--list-missdbs"){BuildMissingUfdBguardDBS(false,true);exit;}
if($argv[1]=="--parsedir"){ParseDirectory($argv[2]);exit;}
if($argv[1]=="--notify-dnsmasq"){notify_remote_proxys_dnsmasq();exit;}
if($argvs[1]=='--build-ufdb-smoothly'){$GLOBALS["FORCE"]=true;echo build_ufdbguard_smooth();echo "Starting......: ".date("H:i:s")." Starting UfdGuard FINISH DONE\n";exit;}
}
$unix=new unix();
$pidfile="/etc/artica-postfix/pids/".basename(__FILE__).".MAIN.pid";
$pid=@file_get_contents($pidfile);
if($unix->process_exists($pid,basename(__FILE__))){
$timefile=$unix->PROCCESS_TIME_MIN($pid);
if($timefile<6){
writelogs(basename(__FILE__).": Already running PID $pid since {$timefile}mn.. aborting the process",
basename(__FILE__),__FILE__,__LINE__);
die();
}else{
$kill=$unix->find_program("kill");
unix_system_kill_force($pid);
}
}
@file_put_contents($pidfile, getmypid());
if($GLOBALS["VERBOSE"]){echo "New PID ".getmypid()." [1]={$argv[1]}\n";}
if($argv[1]=="--categories"){build_categories();exit;}
if(isset($argv[2])){if($argv[2]=="--reload"){$GLOBALS["RELOAD"]=true;}}
if($argv[1]=="--build"){build();die();}
if($argv[1]=="--status"){echo status();exit;}
if($argv[1]=="--compile"){echo compile_databases();exit;}
if($argv[1]=="--db-status"){print_r(databasesStatus());exit;}
if($argv[1]=="--db-status-www"){echo serialize(databasesStatus());exit;}
if($argv[1]=="--compile-single"){echo CompileSingleDB($argv[2]);exit;}
if($argv[1]=="--conf"){echo conf();exit;}
//http://cri.univ-tlse1.fr/documentations/cache/squidguard.html
function build_categories(){
$q=new mysql_squid_builder();
$sql="SELECT LOWER(pattern) FROM category_porn WHERE enabled=1 AND pattern REGEXP '[a-zA-Z0-9\_\-]+\.[a-zA-Z0-9\_\-]+' ORDER BY pattern INTO OUTFILE '/tmp/porn.txt' FIELDS OPTIONALLY ENCLOSED BY 'n'";
$q->QUERY_SQL($sql);
if(!$q->ok){echo $q->mysql_error."\n";}
}
function build_ufdbguard_smooth(){
$users=new usersMenus();
if(!$users->APP_UFDBGUARD_INSTALLED){echo "Starting......: ".date("H:i:s")." ufdbGuard is not installed, aborting\n";return;}
$sock=new sockets();
$EnableRemoteStatisticsAppliance=$sock->GET_INFO("EnableRemoteStatisticsAppliance");
if(!is_numeric($EnableRemoteStatisticsAppliance)){$EnableRemoteStatisticsAppliance=0;}
if($EnableRemoteStatisticsAppliance==1){echo "Starting......: ".date("H:i:s")." It use Statistics appliance, aborting\n";return;}
if(function_exists('WriteToSyslogMail')){WriteToSyslogMail("build_ufdbguard_smooth() -> reconfigure UfdbGuardd", basename(__FILE__));}
echo "Starting......: ".date("H:i:s")." ufdbGuard ". date("Y-m-d H:i:s")."\n";
build_ufdbguard_config();
build_ufdbguard_HUP();
}
function build_ufdbguard_HUP(){
if(isset($GLOBALS["build_ufdbguard_HUP_EXECUTED"])){return;}
$GLOBALS["build_ufdbguard_HUP_EXECUTED"]=true;
$unix=new unix();
$sock=new sockets();$forceTXT=null;
$ufdbguardReloadTTL=intval($sock->GET_INFO("ufdbguardReloadTTL"));
if($ufdbguardReloadTTL<1){$ufdbguardReloadTTL=10;}
$php5=$unix->LOCATE_PHP5_BIN();
shell_exec("$php5 /usr/share/artica-postfix/exec.ufdbclient.reload.php");
if(function_exists("debug_backtrace")){
$trace=@debug_backtrace();
if(isset($trace[1])){
$called="called by ". basename($trace[1]["file"])." {$trace[1]["function"]}() line {$trace[1]["line"]}";
}
}
$trace=debug_backtrace();if(isset($trace[1])){$called=" called by ". basename($trace[1]["file"])." {$trace[1]["function"]}() line {$trace[1]["line"]}";}
$timeFile="/etc/artica-postfix/pids/UfdbGuardReload.time";
$TimeReload=$unix->file_time_min($timeFile);
if(!$GLOBALS["FORCE"]){
if($TimeReload<$ufdbguardReloadTTL){
$unix->_syslog("ufdbGuard Aborting reload, last reload since {$TimeReload}Mn, need at least {$ufdbguardReloadTTL}Mn", basename(__FILE__));
echo "Starting......: ".date("H:i:s")." ufdbGuard Aborting reload, last reload since {$TimeReload}Mn, need at least {$ufdbguardReloadTTL}Mn\n";
return;
}
}else{
echo "Starting......: ".date("H:i:s")." --- FORCED --- ufdbGuard last reload was {$TimeReload}mn\n";
}
@unlink($timeFile);
@file_put_contents($timeFile, time());
$pid=ufdbguard_pid();
$squidbin=$unix->find_program("squid");
if(!is_file($squidbin)){$unix->find_program("squid3");}
$ufdbguardd=$unix->find_program("ufdbguardd");
if(strlen($ufdbguardd)<5){WriteToSyslogMail("ufdbguardd no such binary", basename(__FILE__));return;}
$kill=$unix->find_program("kill");
if($unix->process_exists($pid)){
$processTTL=intval($unix->PROCCESS_TIME_MIN($pid));
$LastTime=intval($unix->file_time_min($timeFile));
echo "Starting......: ".date("H:i:s")." ufdbGuard Reloading service TTL {$processTTL}Mn\n";
echo "Starting......: ".date("H:i:s")." ufdbGuard Reloading service Last config since {$LastTime}Mn\n";
echo "Starting......: ".date("H:i:s")." ufdbGuard Reloading Max reload {$ufdbguardReloadTTL}Mn\n";
if(!$GLOBALS["FORCE"]){
echo "Starting......: ".date("H:i:s")." ufdbGuard Reloading force is disabled\n";
if($LastTime<$ufdbguardReloadTTL){
squid_admin_mysql(1, "Reloading Web Filtering [Aborted] last reload {$LastTime}Mn, need {$ufdbguardReloadTTL}mn",null,__FILE__,__LINE__);
echo "Starting......: ".date("H:i:s")." ufdbGuard Reloading service Aborting... minimal time was {$ufdbguardReloadTTL}mn - Current {$LastTime}mn\n";
return;
}
if($processTTL<$ufdbguardReloadTTL){
squid_admin_mysql(1, "Reloading Web Filtering [Aborted] {$processTTL}Mn, need {$ufdbguardReloadTTL}mn",null,__FILE__,__LINE__);
echo "Starting......: ".date("H:i:s")." ufdbGuard Reloading service Aborting... minimal time was {$ufdbguardReloadTTL}mn\n";
return;
}
}
if($GLOBALS["FORCE"]){ $forceTXT=" with option FORCE enabled";}
@unlink($timeFile);
@file_put_contents($timeFile, time());
echo "Starting......: ".date("H:i:s")." ufdbGuard reloading service PID:$pid {$processTTL}mn\n";
squid_admin_mysql(1, "Reloading Web Filtering service PID: $pid TTL {$processTTL}Mn","$forceTXT\n$called\n{$GLOBALS["CMDLINEXEC"]}");
unix_system_HUP($pid);
return;
}
squid_admin_mysql(1, "Warning, Reloading Web Filtering but not running [action=start]","$forceTXT\n$called\n{$GLOBALS["CMDLINEXEC"]}");
echo "Starting......: ".date("H:i:s")." UfdbGuard reloading service no pid is found, Starting service...\n";
@unlink($timeFile);
@file_put_contents($timeFile, time());
ufdbguard_start();
echo "Starting......: ".date("H:i:s")." UfdbGuard restarting ufdb-tail process\n";
shell_exec("/etc/init.d/ufdb-tail restart");
}
function ufdbguard_pid(){
$unix=new unix();
$pid=$unix->get_pid_from_file("/var/tmp/ufdbguardd.pid");
if($unix->process_exists($pid)){
$cmdline=trim(@file_get_contents("/proc/$pid/cmdline"));
if(!preg_match("#ufdbcatdd#", $cmdline)){return $pid;}
}
$ufdbguardd=$unix->find_program("ufdbguardd");
return $unix->PIDOF($ufdbguardd);
}
function ufdguard_start_notify(){
squid_admin_mysql(2, "Starting Web Filtering engine service by init.d script","",__FILE__,__LINE__);
$unix=new unix();
$fuser=$unix->find_program("fuser");
$port=ufdguard_get_listen_port();
$results=array();
echo "Starting......: ".date("H:i:s")." UfdbGuard Listen on port $port\n";
$cmd="$fuser $port/tcp 2>&1";
exec("$cmd",$results);
echo "Starting......: ".date("H:i:s")." UfdbGuard `$cmd` ". count($results) ." lines.\n";
while (list ($num, $ligne) = each ($results) ){
if(preg_match("#$port\/tcp:(.+)#", $ligne,$re)){
$ff=explode(" ", $re[1]);
while (list ($index, $ligne2) = each ($ff) ){
$ligne2=trim($ligne2);
if(!is_numeric($ligne2)){continue;}
echo "Starting......: ".date("H:i:s")." UfdbGuard killing PID $ligne2\n";
$unix->KILL_PROCESS($ligne2,9);
}
}
}
}
function ufdguard_get_listen_port(){
$f=explode("\n",@file_get_contents("/etc/squid3/ufdbGuard.conf"));
while (list ($index, $ligne) = each ($f) ){
if(preg_match("#^port\s+([0-9]+)#", $ligne,$re)){return $re[1];}
}
return 3977;
}
function ufdbguard_start(){
$unix=new unix();
$sock=new sockets();
$nohup=$unix->find_program("nohup");
$pidfile="/etc/artica-postfix/pids/".basename(__FILE__).".". __FUNCTION__.".pid";
$pid=@file_get_contents($pidfile);
if($unix->process_exists($pid,basename(__FILE__))){
echo "Starting......: ".date("H:i:s")." UfdbGuard Starting service aborted, task pid already running $pid\n";
writelogs(basename(__FILE__).":Already executed.. aborting the process",basename(__FILE__),__FILE__,__LINE__);
return;
}
@file_put_contents($pidfile, getmypid());
$pid_path="/var/tmp/ufdbguardd.pid";
if(!is_dir("/var/tmp")){@mkdir("/var/tmp",0775,true);}
$ufdbguardd_path=$unix->find_program("ufdbguardd");
$master_pid=ufdbguard_pid();
if(!$unix->process_exists($master_pid)){
if(function_exists("WriteToSyslogMail")){WriteToSyslogMail("UfdGuard master Daemon seems to not running, trying with pidof", basename(__FILE__));}
$master_pid=$unix->PIDOF($ufdbguardd_path);
if($unix->process_exists($master_pid)){
echo "Starting......: ".date("H:i:s")." UfdGuard master is running, updating PID file with $master_pid\n";
if(function_exists("WriteToSyslogMail")){WriteToSyslogMail("UfdGuard master is running, updating PID file with $master_pid", basename(__FILE__));}
@file_put_contents($pid_path,$master_pid);
return;
}
}
$EnableUfdbGuard=$sock->EnableUfdbGuard();
$SQUIDEnable=$sock->GET_INFO("SQUIDEnable");
$UseRemoteUfdbguardService=$sock->GET_INFO('UseRemoteUfdbguardService');
if(!is_numeric($UseRemoteUfdbguardService)){$UseRemoteUfdbguardService=0;}
if(!is_numeric($SQUIDEnable)){$SQUIDEnable=1;}
if($UseRemoteUfdbguardService==1){$EnableUfdbGuard=0;}
if($SQUIDEnable==0){$EnableUfdbGuard=0;}
if($EnableUfdbGuard==0){echo "Starting......: ".date("H:i:s")." Starting UfdGuard master service Aborting, service is disabled\n";return;}
$trace=debug_backtrace();if(isset($trace[1])){$called=" called by ". basename($trace[1]["file"])." {$trace[1]["function"]}() line {$trace[1]["line"]}";}
squid_admin_mysql(2, "Starting Web Filtering engine service","$trace\n{$GLOBALS["CMDLINEXEC"]}");
ufdbguard_admin_events("Asking to start ufdbguard $trace",__FUNCTION__,__FILE__,__LINE__,"ufdbguard-service");
echo "Starting......: ".date("H:i:s")." Starting UfdGuard master service...\n";
if(function_exists("WriteToSyslogMail")){WriteToSyslogMail("Starting UfdGuard master service...", basename(__FILE__));}
@mkdir("/var/log/ufdbguard",0755,true);
@file_put_contents("/var/log/ufdbguard/ufdbguardd.log", "#");
@chown("/var/log/ufdbguard/ufdbguardd.log", "squid");
@chgrp("/var/log/ufdbguard/ufdbguardd.log", "squid");
shell_exec("$nohup /etc/init.d/ufdb start >/dev/null 2>&1 &");
for($i=1;$i<5;$i++){
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." Starting UfdGuard waiting $i/5\n";}
sleep(1);
$pid=ufdbguard_pid();
if($unix->process_exists($pid)){break;}
}
echo "Starting......: ".date("H:i:s")." Starting UfdGuard master init.d ufdb done...\n";
$master_pid=ufdbguard_pid();
if(!$unix->process_exists($master_pid)){
echo "Starting......: ".date("H:i:s")." Starting UfdGuard master service failed...\n";
squid_admin_mysql(0, "Starting Web Filtering engine service failed","$trace\n{$GLOBALS["CMDLINEXEC"]}\n");
}else{
echo "Starting......: ".date("H:i:s")." Starting UfdGuard master success pid $master_pid...\n";
}
echo "Starting......: ".date("H:i:s")." Starting UfdGuard master ufdbguard_start() function done\n";
}
function checksVersion(){
$unix=new unix();
$ufdbguardd=$unix->find_program("ufdbguardd");
if(!is_file($ufdbguardd)){return;}
$mustcompile=false;
exec("ufdbguardd -v 2>&1",$results);
while (list ($a, $line) = each ($results)){
if(preg_match("#ufdbguardd:\s+([0-9\.]+)#", $line,$re)){
$version=$re[1];
$version=str_replace(".", "", $version);
break;
}
}
echo "Starting......: ".date("H:i:s")." Starting UfdGuard binary version $version\n";
if($version<130){$mustcompile=true;}
if(!$mustcompile){
$binadate=filemtime($ufdbguardd);
$fileatime=fileatime($ufdbguardd);
echo "Starting......: ".date("H:i:s")." Starting UfdGuard version date $binadate (".date("Y-m-d",$binadate).")\n";
if($binadate<1358240994){
$mustcompile=true;
}
}
if($mustcompile){
echo "Starting......: ".date("H:i:s")." Starting UfdGuard must be updated !!\n";
shell_exec("/usr/share/artica-postfix/bin/artica-make APP_UFDBGUARD");
}
}
function build_ufdbguard_config(){
checksVersion();
$sock=new sockets();
$DenyUfdbWriteConf=$sock->GET_INFO("DenyUfdbWriteConf");
if(!is_numeric($DenyUfdbWriteConf)){$DenyUfdbWriteConf=0;}
$EnableRemoteStatisticsAppliance=$sock->GET_INFO("EnableRemoteStatisticsAppliance");
if(!is_numeric($EnableRemoteStatisticsAppliance)){$EnableRemoteStatisticsAppliance=0;}
if($EnableRemoteStatisticsAppliance==1){return;}
$unix=new unix();
$chmod=$unix->find_program("chmod");
$chown=$unix->find_program("chown");
$ln=$unix->find_program("ln");
$trace=debug_backtrace();if(isset($trace[1])){$called=" called by ". basename($trace[1]["file"])." {$trace[1]["function"]}() line {$trace[1]["line"]}";}
$unix->send_email_events("Order to rebuild ufdbGuard config" , $called, "proxy");
$sock=new sockets();
$EnableRemoteStatisticsAppliance=$sock->GET_INFO("EnableRemoteStatisticsAppliance");
$EnableWebProxyStatsAppliance=$sock->GET_INFO("EnableWebProxyStatsAppliance");
if(!is_numeric($EnableRemoteStatisticsAppliance)){$EnableRemoteStatisticsAppliance=0;}
if(!is_numeric($EnableWebProxyStatsAppliance)){$EnableWebProxyStatsAppliance=0;}
$users=new usersMenus();
@mkdir("/var/tmp",0775,true);
@mkdir("/etc/ufdbguard",0777,true);
@mkdir("/etc/squid3",0755,true);
@mkdir("/var/log/squid",0755,true);
@mkdir("/var/lib/ufdbartica",0755,true);
@unlink("/etc/ufdbguard/ufdbGuard.conf");
@unlink("/etc/squid3/ufdbGuard.conf");
remove_bad_files();
$ufdb=new compile_ufdbguard();
$datas=$ufdb->buildConfig();
if(is_file("/var/log/squid/UfdbguardCache.db")){@unlink("/var/log/squid/UfdbguardCache.db"); }
if($EnableWebProxyStatsAppliance==1){
@file_put_contents("/usr/share/artica-postfix/ressources/databases/ufdbGuard.conf",$datas);
}
if($DenyUfdbWriteConf==0){
@file_put_contents("/etc/ufdbguard/ufdbGuard.conf",$datas);
@file_put_contents("/etc/squid3/ufdbGuard.conf",$datas);
$sock->TOP_NOTIFY("{webfiltering_parameters_was_saved}");
}
shell_exec("$chmod 755 /etc/squid3/ufdbGuard.conf");
shell_exec("$chmod -R 755 /etc/squid3/ufdbGuard.conf");
shell_exec("$chmod -R 755 /etc/ufdbguard");
shell_exec("chown -R squid:squid /etc/ufdbguard");
shell_exec("chown -R squid:squid /var/log/squid");
shell_exec("chown -R squid:squid /etc/squid3");
shell_exec("chown -R squid:squid /var/lib/ufdbartica");
}
function conf(){
$sock=new sockets();
$EnableRemoteStatisticsAppliance=$sock->GET_INFO("EnableRemoteStatisticsAppliance");
if(!is_numeric($EnableRemoteStatisticsAppliance)){$EnableRemoteStatisticsAppliance=0;}
if($EnableRemoteStatisticsAppliance==1){return;}
$users=new usersMenus();
$sock=new sockets();
$unix=new unix();
@mkdir("/var/tmp",0775,true);
if(!is_file("/var/log/ufdbguard/ufdbguardd.log")){
@mkdir("/var/log/ufdbguard",0755,true);
@file_put_contents("/var/log/ufdbguard/ufdbguardd.log", "see /var/log/squid/ufdbguardd.log\n");
shell_exec("chmod 777 /var/log/ufdbguard/ufdbguardd.log");
}
if(is_file("/usr/sbin/ufdbguardd")){
if(!is_file("/usr/bin/ufdbguardd")){
$unix=new unix();
$ln=$unix->find_program("ln");
shell_exec("$ln -s /usr/sbin/ufdbguardd /usr/bin/ufdbguardd");
}
}
@mkdir("/etc/ufdbguard",0755,true);
build_ufdbguard_config();
buildDans();
ufdbguard_schedule();
if($users->APP_UFDBGUARD_INSTALLED){
$chmod=$unix->find_program("chmod");
shell_exec("$chmod 755 /etc >/dev/null 2>&1");
shell_exec("$chmod 755 /etc/ufdbguard >/dev/null 2>&1");
shell_exec("$chmod 755 /var/log/ufdbguard >/dev/null 2>&1");
shell_exec("$chmod 755 /var/log/squid >/dev/null 2>&1");
shell_exec("$chmod -R 755 /var/lib/squidguard >/dev/null 2>&1 &");
ufdbguard_admin_events("Asking to reload ufdbguard",__FUNCTION__,__FILE__,__LINE__,"ufdbguard-service");
build_ufdbguard_HUP();
}
}
function buildDans(){
if(!is_dir("/var/run/dansguardian")){@mkdir("/var/run/dansguardian",0755,true);}
$dans=new compile_dansguardian();
$dans->build();
$sock=new sockets();
$EnableWebProxyStatsAppliance=$sock->GET_INFO("EnableWebProxyStatsAppliance");
$EnableRemoteStatisticsAppliance=$sock->GET_INFO("EnableRemoteStatisticsAppliance");
if(!is_numeric($EnableWebProxyStatsAppliance)){$EnableWebProxyStatsAppliance=0;}
if(!is_numeric($EnableRemoteStatisticsAppliance)){$EnableRemoteStatisticsAppliance=0;}
if($EnableWebProxyStatsAppliance==1){
echo "TO DEV -> Send to stats appliance !\n";
return;
}
}
function remove_bad_files(){
$unix=new unix();
$dirs=$unix->dirdir("/var/lib/ftpunivtlse1fr");
while (list ($directory, $b) = each ($dirs)){
$dirname=basename($directory);
if(is_link("$directory/$dirname")){
echo "Starting......: ".date("H:i:s")." UfdBguard removing $dirname/$dirname bad file\n";
@unlink("$directory/$dirname");
}
}
echo "Starting......: ".date("H:i:s")." UfdBguard removing bad files done...\n";
}
function build(){
$sock=new sockets();
$EnableRemoteStatisticsAppliance=$sock->GET_INFO("EnableRemoteStatisticsAppliance");
if(!is_numeric($EnableRemoteStatisticsAppliance)){$EnableRemoteStatisticsAppliance=0;}
if($EnableRemoteStatisticsAppliance==1){return;}
send_email_events("Order to rebuild filters configuration",@implode("\nParams:",$argv),"proxy");
$funtion=__FUNCTION__;
if(!isset($GLOBALS["VERBOSE"])){$GLOBALS["VERBOSE"]=false;}
if($GLOBALS["VERBOSE"]){echo "$funtion::".__LINE__." Loading libraries\n";}
$users=new usersMenus();
$sock=new sockets();
$unix=new unix();
$php5=$unix->LOCATE_PHP5_BIN();
$chown=$unix->find_program("chown");
$chmod=$unix->find_program("chmod");
$squidbin=$unix->find_program("squid3");
$nohup=$unix->find_program("nohup");
$unix->SystemCreateUser("squid","squid");
@mkdir("/var/tmp",0775,true);
$EnableWebProxyStatsAppliance=$sock->GET_INFO("EnableWebProxyStatsAppliance");
$EnableRemoteStatisticsAppliance=$sock->GET_INFO("EnableRemoteStatisticsAppliance");
if(!is_numeric($EnableWebProxyStatsAppliance)){$EnableWebProxyStatsAppliance=0;}
if(!is_numeric($EnableRemoteStatisticsAppliance)){$EnableRemoteStatisticsAppliance=0;}
$UseRemoteUfdbguardService=$sock->GET_INFO('UseRemoteUfdbguardService');
if(!is_file($squidbin)){$squidbin=$unix->find_program("squid");}
$EnableUfdbGuard=$sock->EnableUfdbGuard();
$SQUIDEnable=$sock->GET_INFO("SQUIDEnable");
if(!is_numeric($SQUIDEnable)){$SQUIDEnable=1;}
if(!is_numeric($UseRemoteUfdbguardService)){$UseRemoteUfdbguardService=0;}
if($GLOBALS["VERBOSE"]){echo "DEBUG::$funtion:: EnableWebProxyStatsAppliance=$EnableWebProxyStatsAppliance\n";}
if($GLOBALS["VERBOSE"]){echo "DEBUG::$funtion:: EnableRemoteStatisticsAppliance=$EnableRemoteStatisticsAppliance\n";}
if($GLOBALS["VERBOSE"]){echo "DEBUG::$funtion:: EnableUfdbGuard=$EnableUfdbGuard\n";}
if($GLOBALS["VERBOSE"]){echo "DEBUG::$funtion:: SQUIDEnable=$SQUIDEnable\n";}
if($GLOBALS["VERBOSE"]){echo "DEBUG::$funtion:: UseRemoteUfdbguardService=$UseRemoteUfdbguardService\n";}
$GLOBALS["SQUIDBIN"]=$squidbin;
if($EnableWebProxyStatsAppliance==0){
$installed=false;
if($users->SQUIDGUARD_INSTALLED){$installed=true;echo "Starting......: ".date("H:i:s")." SquidGuard is installed\n";}
if($users->APP_UFDBGUARD_INSTALLED){$installed=true;echo "Starting......: ".date("H:i:s")." UfdBguard is installed\n";}
if($users->DANSGUARDIAN_INSTALLED){$installed=true;echo "Starting......: ".date("H:i:s")." Dansguardian is installed\n";}
if(!$installed){if($GLOBALS["VERBOSE"]){echo "No one installed...\n";
shell_exec("$nohup ".LOCATE_PHP5_BIN2()." ".dirname(__FILE__)."/exec.usrmactranslation.php >/dev/null 2>&1 &");
return false;}}
}
if($EnableUfdbGuard==0){if($GLOBALS["VERBOSE"]){echo "UfDbguard is disabled ( see EnableUfdbGuard ) in line: ". __LINE__."\n";}return;}
if($SQUIDEnable==0){if($GLOBALS["VERBOSE"]){echo "UfDbguard is disabled ( see SQUIDEnable ) in line: ". __LINE__."\n";}return;}
if($UseRemoteUfdbguardService==1){if($GLOBALS["VERBOSE"]){echo "UfDbguard is disabled ( see UseRemoteUfdbguardService ) in line: ". __LINE__."\n";}return;}
if($GLOBALS["VERBOSE"]){echo "FIX_1_CATEGORY_CHECKED()\n";}
FIX_1_CATEGORY_CHECKED();
if($EnableRemoteStatisticsAppliance==1){
if($GLOBALS["VERBOSE"]){echo "Use the Web statistics appliance to get configuration file...\n";}
shell_exec("$nohup ".LOCATE_PHP5_BIN2()." ".dirname(__FILE__)."/exec.usrmactranslation.php >/dev/null 2>&1 &");
ufdbguard_remote();
return;
}
if($GLOBALS["VERBOSE"]){echo "$funtion::".__LINE__."Loading compile_dansguardian()\n";}
$dans=new compile_dansguardian();
if($GLOBALS["VERBOSE"]){echo "$funtion::".__LINE__."Loading compile_dansguardian::->build()\n";}
$dans->build();
echo "Starting......: ".date("H:i:s")." Dansguardian compile done...\n";
if(function_exists('WriteToSyslogMail')){WriteToSyslogMail("build() -> reconfigure UfdbGuardd", basename(__FILE__));}
build_ufdbguard_config();
ufdbguard_schedule();
if($EnableWebProxyStatsAppliance==1){
echo "Starting......: ".date("H:i:s")." This server is a Squid Appliance, compress databases and notify proxies\n";
CompressCategories();
notify_remote_proxys();
}
shell_exec("$php5 /usr/share/artica-postfix/exec.initslapd.php --ufdbguard");
CheckPermissions();
ufdbguard_admin_events("Service will be rebuiled and restarted",__FUNCTION__,__FILE__,__LINE__,"config");
shell_exec("$nohup ".LOCATE_PHP5_BIN2()." ".dirname(__FILE__)."/exec.usrmactranslation.php >/dev/null 2>&1 &");
if(!$GLOBALS["RESTART"]){
if(is_file("/etc/init.d/ufdb")){
echo "Starting......: ".date("H:i:s")." Checking watchdog\n";
ufdbguard_watchdog();
echo "Starting......: ".date("H:i:s")." ufdbGuard reloading service\n";
build_ufdbguard_HUP();
}
}
if($GLOBALS["RESTART"]){
if(is_file("/etc/init.d/ufdb")){
echo "Starting......: ".date("H:i:s")." Restarting\n";
shell_exec("/etc/init.d/ufdb restart");
}
}
if($users->DANSGUARDIAN_INSTALLED){
echo "Starting......: ".date("H:i:s")." Dansguardian reloading service\n";
shell_exec("/usr/share/artica-postfix/bin/artica-install --reload-dansguardian --withoutconfig");
}
}
function FileMD5($path){
if(strlen(trim($GLOBALS["md5sum"]))==0){
$unix=new unix();
$md5sum=$unix->find_program("md5sum");
$GLOBALS["md5sum"]=$md5sum;
}
if(strlen(trim($GLOBALS["md5sum"]))==0){return md5(@file_get_contents($path));}
exec("{$GLOBALS["md5sum"]} $path 2>&1",$res);
$data=trim(@implode(" ",$res));
if(preg_match("#^(.+?)\s+.+?#",$data,$re)){return trim($re[1]);}
}
function ufdbguard_watchdog_remove(){
}
function ufdbguard_watchdog(){
}
function dump_adrules($ruleid){
$ufbd=new compile_ufdbguard();
$ufbd->build_membersrule($ruleid);
@file_put_contents("/usr/share/artica-postfix/ressources/logs/web/ufdb-dump-$ruleid.wt",0);
@file_put_contents("/usr/share/artica-postfix/ressources/logs/web/ufdb-dump-$ruleid.txt","\n");
@chmod("/usr/share/artica-postfix/ressources/logs/web/ufdb-dump-$ruleid.wt",0777);
@chmod("/usr/share/artica-postfix/ressources/logs/web/ufdb-dump-$ruleid.txt",0777);
if($GLOBALS["VERBOSE"]){echo "/usr/share/artica-postfix/external_acl_squid_ldap.php --db $ruleid\n";}
exec("/usr/share/artica-postfix/external_acl_squid_ldap.php --db $ruleid --output 2>&1", $results);
@file_put_contents("/usr/share/artica-postfix/ressources/logs/web/ufdb-dump-$ruleid.wt",1);
@file_put_contents("/usr/share/artica-postfix/ressources/logs/web/ufdb-dump-$ruleid.txt",@implode("\n", $results));
}
function CheckPermissions(){
$unix=new unix();
$mv=$unix->find_program("mv");
$chown=$unix->find_program("chown");
$chmod=$unix->find_program("chmod");
$ln=$unix->find_program("ln");
@mkdir("/var/lib/squidguard",644,true);
@mkdir("/etc/ufdbguard",644,true);
$user=GetSquidUser();
if(!is_file("/squid/log/squid/squidGuard.log")){
@mkdir("/squid/log/squid",0755,true);
@file_put_contents("/squid/log/squid/squidGuard.log","#");
shell_exec("$chown $user /squid/log/squid/squidGuard.log");
}
if(!is_dir("/var/run/dansguardian")){@mkdir("/var/run/dansguardian",0755,true);}
if(!is_dir("/etc/dansguardian")){@mkdir("/etc/dansguardian",0755,true);}
shell_exec("$chown squid:squid /var/run/dansguardian");
if(is_file("/usr/sbin/ufdbguardd")){if(!is_file("/usr/bin/ufdbguardd")){$unix=new unix();$ln=$unix->find_program("ln");shell_exec("$ln -s /usr/sbin/ufdbguardd /usr/bin/ufdbguardd");}}
if(!is_dir("/var/lib/ftpunivtlse1fr")){@mkdir("/var/lib/ftpunivtlse1fr",0755,true);}
if(!is_dir("/var/lib/squidguard/checked")){@mkdir("/var/lib/squidguard/checked",0755,true);@chown("/var/lib/squidguard/checked","squid");}
if(!is_file("/squid/log/squid/squidGuard.log")){
@mkdir("/squid/log/squid",0755,true);
@file_put_contents("/squid/log/squid/squidGuard.log","#");
shell_exec("$chown $user /squid/log/squid/squidGuard.log");
}
if(!is_file("/var/log/ufdbguard/ufdbguardd.log")){
@mkdir("/var/log/ufdbguard",0755,true);
@file_put_contents("/var/log/ufdbguard/ufdbguardd.log", "see /var/log/squid/ufdbguardd.log\n");
}
if(is_file("/usr/sbin/ufdbguardd")){if(!is_file("/usr/bin/ufdbguardd")){shell_exec("$ln -s /usr/sbin/ufdbguardd /usr/bin/ufdbguardd");}}
@mkdir("/etc/ufdbguard",0755,true);
@mkdir("/var/lib/ufdbartica",0755,true);
shell_exec("$chown $user /var/lib/squidguard");
shell_exec("$chown $user /var/lib/ftpunivtlse1fr");
shell_exec("$chown -R $user /var/lib/squidguard");
shell_exec("$chown -R $user /etc/dansguardian");
shell_exec("$chown -R $user /var/log/squid");
shell_exec("$chown -R $user /var/log/squid/");
shell_exec("$chown -R $user /etc/ufdbguard");
shell_exec("$chown -R $user /etc/ufdbguard");
shell_exec("$chown -R $user /var/lib/ftpunivtlse1fr");
shell_exec("$chmod -R ug+x /var/lib/squidguard/");
shell_exec("$chown -R $user /var/lib/squidguard");
shell_exec("$chown -R $user /var/lib/ufdbartica");
shell_exec("$chown -R $user /var/lib/ufdbartica");
shell_exec("$chown -R $user /var/log/squid");
shell_exec("$chmod -R 755 /var/lib/squidguard");
shell_exec("$chmod -R 755 /var/lib/ufdbartica");
shell_exec("$chmod -R ug+x /var/lib/squidguard");
@chown("/var/lib/squidguard/checked","squid");
if(!is_file("/var/log/ufdbguard/ufdbguardd.log")){@mkdir("/var/log/ufdbguard",0755,true);@file_put_contents("/var/log/ufdbguard/ufdbguardd.log", "see /var/log/squid/ufdbguardd.log\n");}
shell_exec("chmod 755 /var/log/ufdbguard/ufdbguardd.log");
@link(dirname(__FILE__)."/ressources/logs/squid-template.log", "/var/log/squid/squid-template.log");
}
function UFDBGUARD_COMPILE_SINGLE_DB($path){
$timeStart=time();
$OriginalDirename=dirname($path);
$unix=new unix();
$path=str_replace(".ufdb","",$path);
$pidpath="/etc/artica-postfix/pids/".basename(__FILE__).".".__FUNCTION__.md5($path).".pid";
$pid=@file_get_contents($pidpath);
if($unix->process_exists($pid)){
events_ufdb_tail("Check \"$path\"... Already process PID \"$pid\" running task has been aborted");
return;
}
$category=null;
$ufdbGenTable=$unix->find_program("ufdbGenTable");
if(!is_file($ufdbGenTable)){writelogs("ufdbGenTable no such binary",__FUNCTION__,__FILE__,__LINE__);return;}
events_ufdb_tail("Check \"$path\"...",__LINE__);
if(preg_match("#\/var\/lib\/squidguard\/(.+?)\/(.+?)/(.+?)$#",$path,$re)){
$category=$re[2];
$domain_path="/var/lib/squidguard/{$re[1]}/{$re[2]}/domains";
}
if($category==null){
if(preg_match("#\/var\/lib\/squidguard\/(.+?)\/domains#",$path,$re)){
$category=$re[1];
$domain_path="/var/lib/squidguard/{$re[1]}/domains";
}
}
if(preg_match("#web-filter-plus\/BL\/(.+?)\/domains#",$path,$re)){
$category=$re[1];
$domain_path="/var/lib/squidguard/web-filter-plus/BL/$category/domains";
}
if(preg_match("#blacklist-artica\/(.+?)\/(.+?)\/domains#",$path,$re)){
events_ufdb_tail("find double category \"{$re[1]}-{$re[2]}\"...",__LINE__);
$category="{$re[1]}-{$re[2]}";
$domain_path="/var/lib/squidguard/blacklist-artica/{$re[1]}/{$re[2]}/domains";
}
if(preg_match("#blacklist-artica\/sex\/(.+?)\/domains#",$path,$re)){
$category=$re[1];
$domain_path="/var/lib/squidguard/blacklist-artica/sex/$category/domains";
}
if($category==null){
events_ufdb_tail("exec.squidguard.php:: \"$path\" cannot understand...");
}
events_ufdb_tail("exec.squidguard.php:: Found category \"$category\"",__LINE__);
if(!is_file($path)){
events_ufdb_tail("exec.squidguard.php:$category: \"$path\" no such file, build it",__LINE__);
@file_put_contents($domain_path," ");
}
$category_compile=substr($category,0,15);
if(strlen($category_compile)>15){
$category_compile=str_replace("recreation_","recre_",$category_compile);
$category_compile=str_replace("automobile_","auto_",$category_compile);
$category_compile=str_replace("finance_","fin_",$category_compile);
if(strlen($category_compile)>15){
$category_compile=str_replace("_", "", $category_compile);
if(strlen($category_compile)>15){
$category_compile=substr($category_compile, strlen($category_compile)-15,15);
}
}
}
events_ufdb_tail("exec.squidguard.php:: category \"$category\" retranslated to \"$category_compile\"",__LINE__);
if(is_file("$domain_path.ufdb")){
events_ufdb_tail("exec.squidguard.php:: removing \"$domain_path.ufdb\" ...");
@unlink("$domain_path.ufdb");
}
if(!is_file($domain_path)){
events_ufdb_tail("exec.squidguard.php:: $domain_path no such file, create an empty one",__LINE__);
@mkdir(dirname($domain_path),0755,true);
@file_put_contents($domain_path,"#");
}
$urlcmd=null;
$d=" -d $domain_path";
if(is_file("$OriginalDirename/urls")){
$urlssize=@filesize("$OriginalDirename/urls");
events_ufdb_tail("exec.squidguard.php:: $OriginalDirename/urls $urlssize bytes...",__LINE__);
if($urlssize>50){
$urlcmd=" -u $OriginalDirename/urls";
}
}
$NICE=EXEC_NICE();
$cmd="$NICE$ufdbGenTable -n -D -W -t $category_compile$d$urlcmd 2>&1";
events_ufdb_tail("exec.squidguard.php:$category:$cmd");
$time=time();
exec($cmd,$results);
exec($cmd,$results);
while (list ($a, $b) = each ($results)){
if(strpos($b,"is not added because it was already matched")){continue;}
if(strpos($b,"has optimised subdomains")){continue;}
events_ufdb_tail("exec.squidguard.php:$category:$b");
}
$tookrecompile=$unix->distanceOfTimeInWords($time,time());
events_ufdb_tail("exec.squidguard.php:$category_compile: execution $tookrecompile",__LINE__);
events_ufdb_tail("exec.squidguard.php:$category:done..");
$user=GetSquidUser();
$chown=$unix->find_program("chown");
if(is_file($chown)){
events_ufdb_tail("exec.squidguard.php:$category:$chown -R $user $OriginalDirename");
shell_exec("$chown -R $user $OriginalDirename/*");
shell_exec("$chown -R $user /var/log/squid/*");
}
$sock=new sockets();
$took=$unix->distanceOfTimeInWords($timeStart,time());
$sock->TOP_NOTIFY("$OriginalDirename webfiltering database ($category) was recompiled took $took hard compilation took: $tookrecompile","info");
}
function databasesStatus(){
$datas=explode("\n",@file_get_contents("/etc/squid/squidGuard.conf"));
$count=0;
$f=array();
while (list ($a, $b) = each ($datas)){
if(preg_match("#domainlist.+?(.+)#",$b,$re)){
$f[]["domainlist"]["path"]="/var/lib/squidguard/{$re[1]}";
continue;
}
if(preg_match("#expressionlist.+?(.+)#",$b,$re)){
$f[]["expressionlist"]["path"]="/var/lib/squidguard/{$re[1]}";
continue;
}
if(preg_match("#urllist.+?(.+)#",$b,$re)){
$f[]["urllist"]["path"]="/var/lib/squidguard/{$re[1]}";
continue;
}
}
while (list ($a, $b) = each ($f)){
$domainlist=$b["domainlist"]["path"];
$expressionlist=$b["expressionlist"]["path"];
$urllist=$b["urllist"]["path"];
if(is_file($domainlist)){
$key="domainlist";
$path=$domainlist;
}
if(is_file($expressionlist)){
$key="expressionlist";
$path=$expressionlist;
}
if(is_file($urllist)){
$key="urllist";
$path=$urllist;
}
$d=explode("\n",@file_get_contents($path));
$i[$path]["type"]=$key;
$i[$path]["size"]=@filesize("$domainlist.db");
$i[$path]["linesn"]=count($d);
$i[$path]["date"]=filemtime($path);
}
return $i;
}
function status(){
$squid=new squidbee();
$array=$squid->SquidGuardDatabasesStatus();
$conf[]="[APP_SQUIDGUARD]";
$conf[]="service_name=APP_SQUIDGUARD";
if(is_array($array)){
$conf[]="running=0";
$conf[]="why={waiting_database_compilation}<br>{databases}:&nbsp;".count($array);
return implode("\n",$conf);
}
$unix=new unix();
$users=new usersMenus();
$pidof=$unix->find_program("pidof");
exec("$pidof $users->SQUIDGUARD_BIN_PATH",$res);
$array=explode(" ",implode(" ",$res));
while (list ($index, $line) = each ($array)){
if(preg_match("#([0-9]+)#",$line,$ri)){
$pid=$ri[1];
$inistance=$inistance+1;
$mem=$mem+$unix->MEMORY_OF($pid);
$ppid=$unix->PPID_OF($pid);
}
}
$conf[]="running=1";
$conf[]="master_memory=$mem";
$conf[]="master_pid=$ppid";
$conf[]="other={processes}:$inistance";
return implode("\n",$conf);
}
function CompileSingleDB($db_path){
$user=GetSquidUser();
$users=new usersMenus();
$unix=new unix();
if(strpos($db_path,".db")>0){$db_path=str_replace(".db","",$db_path);}
$verb=" -d";
$chown=$unix->find_program("chown");
$chmod=$unix->find_program("chmod");
exec($users->SQUIDGUARD_BIN_PATH." $verb -C $db_path",$repair);
shell_exec("$chown -R $user /var/lib/squidguard/*");
shell_exec("$chmod -R 755 /var/lib/squidguard/*");
shell_exec("$chmod -R ug+x /var/lib/squidguard/*");
$db_recover=$unix->LOCATE_DB_RECOVER();
shell_exec("$db_recover -h ".dirname($db_path));
build();
KillSquidGuardInstances();
send_email_events("squidGuard: $db_path repair","the database $db_path was repair by artica\n",@implode("\n",$repair),"squid");
}
function KillSquidGuardInstances(){
$unix=new unix();
$users=new usersMenus();
$pidof=$unix->find_program("pidof");
if(strlen($pidof)>3){
exec("$pidof $users->SQUIDGUARD_BIN_PATH 2>&1",$results);
$pids=trim(@implode(" ",$results));
if(strlen($pids)>3){
echo "Starting......: ".date("H:i:s")." squidGuard kill $pids PIDs\n";
shell_exec("/bin/kill $pids");
}
}
}
function compile_databases(){
$users=new usersMenus();
$squid=new squidbee();
$array=$squid->SquidGuardDatabasesStatus();
$verb=" -d";
$array=$squid->SquidGuardDatabasesStatus(0);
if( count($array)>0){
while (list ($index, $file) = each ($array)){
echo "Starting......: ".date("H:i:s")." squidGuard compiling ". count($array)." databases\n";
$file=str_replace(".db",'',$file);
$textfile=str_replace("/var/lib/squidguard/","",$file);
echo "Starting......: ".date("H:i:s")." squidGuard compiling $textfile database ".($index+1) ."/". count($array)."\n";
if($GLOBALS["VERBOSE"]){$verb=" -d";echo $users->SQUIDGUARD_BIN_PATH." $verb -C $file\n";}
system($users->SQUIDGUARD_BIN_PATH." -P$verb -C $file");
}
}else{
echo "Starting......: ".date("H:i:s")." squidGuard compiling all databases\n";
if($GLOBALS["VERBOSE"]){$verb=" -d";echo $users->SQUIDGUARD_BIN_PATH." $verb -C all\n";}
system($users->SQUIDGUARD_BIN_PATH." -P$verb -C all");
}
$user=GetSquidUser();
$unix=new unix();
$chown=$unix->find_program("chown");
$chmod=$unix->find_program("chmod");
shell_exec("$chown -R $user /var/lib/squidguard/*");
shell_exec("$chmod -R 755 /var/lib/squidguard/*");
system(LOCATE_PHP5_BIN2()." ".dirname(__FILE__)."/exec.squid.php --build");
build();
KillSquidGuardInstances();
}
function parseTemplate_extension($uri){
$array=parse_url($uri);
if(count($array)==0){return false;}
if(!isset($array["path"])){return false;}
$path_parts = pathinfo($array["path"]);
$ext=$path_parts['extension'];
if(preg_match("#(.+?)\?#", $ext,$re)){$ext=$re[1];}
if($ext=="php"){return false;}
if($ext=="html"){return false;}
$basename=$path_parts['basename'];
$filename=$path_parts['basename'];
if(preg_match("#\/pixel\?#", $uri)){
parseTemplate_extension_gif();
return true;
}
if($filename==null){$filename="1x1.$ext";}
$ctype=null;
switch ($ext) {
case "gif": parseTemplate_extension_gif($filename);return true;
case "png": $ctype="image/png"; break;
case "jpeg": $ctype="image/jpg";break;
case "jpg": $ctype="image/jpg";;break;
case "js": $ctype="application/x-javascript";;break;
case "css": $ctype="text/css";;break;
}
//aspx
if($ext=="js"){
header("content-type: application/x-javascript");echo "// blocked by url filtering\n";
return true;
}
if($ext=="css"){
header("content-type: text/css");echo "\n";
echo "/**\n";
echo "* blocked by url filtering\n";
echo "* \n";
echo "*/\n";
return true;
}
if($ext=="ico"){
$fsize = filesize("ressources/templates/Squid/favicon.ico");
header("content-type: image/vnd.microsoft.icon");
header("Content-Length: ".$fsize);
ob_clean();
flush();
readfile( $fsize );
return true;
}
if($ctype<>null){
if(!is_file("img/$filename")){$filename=null;}
if($filename==null){$filename="1x1.$ext";}
$fsize = filesize("img/$filename");
header("Content-Type: $ctype");
header("Content-Length: ".$fsize);
ob_clean();
flush();
readfile( $fsize );
return true;
}
writelogs("$uri: $ext ($filename) Unkown",__FUNCTION__,__FILE__,__LINE__);
}
function parseTemplate_extension_gif($filename){
$fsize = filesize("img/1x1.gif");
header("Content-Type: image/gif");
header("Content-Length: ".$fsize);
ob_clean();
flush();
readfile( "img/1x1.gif" );
}
function parseTemplateLogs($text=null,$function,$file,$line){
$time=date('m-d H:i:s');
if($GLOBALS["VERBOSE"]){echo "[$time]:$function:$text in line $line<br>\n";}
$logFile=dirname(__FILE__)."/ressources/logs/squid-template.log";
if(!is_dir(dirname($logFile))){mkdir(dirname($logFile));}
if (is_file($logFile)) {
$size=filesize($logFile);
if($size>1000000){unlink($logFile);}
}
$logFile=str_replace("//","/",$logFile);
$f = @fopen($logFile, 'a');
@fwrite($f, "[$time]:$function:$text in line $line\n");
@fclose($f);
}
function parseTemplateForcejs($uri){
if(preg_match("#ad\.doubleclick\.net\/adj\/#", $uri)){return true;}
}
function parseTemplate_LocalDB_receive(){
session_start();
include_once(dirname(__FILE__)."/ressources/class.sockets.inc");
include_once(dirname(__FILE__)."/ressources/class.mysql.inc");
include_once(dirname(__FILE__)."/ressources/class.page.builder.inc");
include_once(dirname(__FILE__)."/ressources/class.templates.inc");
include_once(dirname(__FILE__)."/ressources/class.mysql.squid.builder.php");
include_once(dirname(__FILE__)."/ressources/class.ldap.inc");
include_once(dirname(__FILE__)."/ressources/class.user.inc");
$user=new user($_POST["USERNAME"]);
if($_POST["password"]<>md5($user->password)){
$tpl=new templates();
echo $tpl->javascript_parse_text("{failed}: {wrong_password}");
die();
}
$privs=new privileges($user->uid);
$privileges_array=$privs->privs;
if($privileges_array["AllowDansGuardianBanned"]<>"yes"){
$tpl=new templates();
echo $tpl->javascript_parse_text("{failed}: {ERROR_NO_PRIVS}");
return;
}
//AllowDansGuardianBanned
$Whitehost=$_POST["Whitehost"];
$CLIENT=$_POST["CLIENT"];
$MEMBER=$_POST["USERNAME"];
$md5=md5("$CLIENT$Whitehost$MEMBER");
$sql="INSERT IGNORE INTO webfilters_usersasks (zmd5,ipaddr,sitename,uid)
VALUES ('$md5','$CLIENT','$Whitehost','$MEMBER')";
$q=new mysql_squid_builder();
$q->QUERY_SQL($sql);
if(!$q->ok){
if(strpos($q->mysql_error, "doesn't exist")>0){$q->CheckTables();$q->QUERY_SQL($sql);}
}
if(!$q->ok){echo $q->mysql_error;return;}
$tpl=new templates();
echo $tpl->_ENGINE_parse_body("{success_restart_query_in_few_seconds}");
$sock=new sockets();
$sock->getFrameWork("squid.php?build-smooth=yes");
}
function parseTemplate_LocalDB(){
session_start();
include_once(dirname(__FILE__)."/ressources/class.sockets.inc");
include_once(dirname(__FILE__)."/ressources/class.mysql.inc");
$sock=new sockets();
$SquidGuardServerName=$sock->GET_INFO("SquidGuardServerName");
$SquidGuardApachePort=$sock->GET_INFO("SquidGuardApachePort");
$GLOBALS["JS_NO_CACHE"]=true;
$proto="http";
if (isset($_SERVER['HTTPS'])){if (strtolower($_SERVER['HTTPS']) == 'on'){$proto="https";}}
$GLOBALS["JS_HEAD_PREPREND"]="$proto://{$_SERVER["SERVER_NAME"]}:{$_SERVER["SERVER_PORT"]}";
$t=time();
include_once(dirname(__FILE__)."/ressources/class.page.builder.inc");
include_once(dirname(__FILE__)."/ressources/class.templates.inc");
$page=CurrentPageName();
$tpl=new templates();
$ask_password=$tpl->javascript_parse_text("{password}:");
$url=base64_decode($_GET["url"]);
$clientaddr=base64_decode($_GET["clientaddr"]);
$array=parse_url($url);
$Whitehost=strtolower($array["host"]);
$pp=new pagebuilder();
$head=$pp->jsArtica()."\n\n".$pp->headcss();
if(preg_match("#^www.(.+)#", $Whitehost,$re)){$Whitehost=$re[1];}
$yahoo=$pp->YahooBody();
$t=time();
$html="
<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />
<title>$ask_password</title>
<meta http-equiv=\"X-UA-Compatible\" content=\"IE=EmulateIE7\" />
<link href='/css/styles_main.css' rel=\"styleSheet\" type='text/css' />
<link href='/css/styles_header.css' rel=\"styleSheet\" type='text/css' />
<link href='/css/styles_middle.css' rel=\"styleSheet\" type='text/css' />
<link href='/css/styles_tables.css' rel=\"styleSheet\" type='text/css' />
<link href=\"/css/styles_rounded.css\" rel=\"stylesheet\" type=\"text/css\" />
$head
</head>
<body style='background: url(\"/css/images/pattern.png\") repeat scroll 0pt 0pt rgb(38, 56, 73); padding: 0px;padding-top: 15px; margin: 0px; border: 0px solid black; width: 100%; cursor: default; -moz-user-select: inherit;'>
$yahoo
<div id='div-$t'></div>
<table style='width:99%' class=form>
<tr>
<td class=legend style='font-size:16px'>{client}:</td>
<td style='font-size:16px'><strong>$clientaddr</strong></td>
</tr>
<tr>
<td class=legend style='font-size:16px'>{website}:</td>
<td style='font-size:16px'><strong>$Whitehost</strong></td>
</tr>
<tr>
<td class=legend style='font-size:16px'>{username}:</td>
<td style='font-size:16px'>". Field_text("USERNAME-$t",null,"font-size:16px;font-weight:bolder",null,null,null,false,"SendPassCheck(event)")."</td>
</tr>
<tr>
<td class=legend style='font-size:16px'>{password}:</td>
<td style='font-size:16px'>". Field_password("#nolock:PASS-$t",null,"font-size:16px",null,null,null,false,"SendPassCheck(event)")."</td>
</tr>
<tr>
<td colspan=2 align='right'><hr>". button("{submit}","SendPass$t()",18)."</td>
</tr>
</table>
<script>
var X_SendPass= function (obj) {
var tempvalue=obj.responseText;
if(tempvalue.length>3){alert(tempvalue);}
document.getElementById('div-$t').innerHTML='';
}
function SendPassCheck(e){
if(checkEnter(e)){SendPass$t();}
}
function SendPass$t(){
var password=MD5(document.getElementById('PASS-$t').value);
var XHR = new XHRConnection();
XHR.appendData('password',password);
XHR.appendData('USERNAME',document.getElementById('USERNAME-$t').value);
XHR.appendData('CLIENT','$clientaddr');
XHR.appendData('Whitehost','$Whitehost');
AnimateDiv('div-$t');
XHR.sendAndLoad('$page', 'POST',X_SendPass);
}
MessagesTophideAllMessages();
</script>
</body>
</html>";
echo $tpl->_ENGINE_parse_body($html);
}
function parseTemplate_SinglePassWord(){
session_start();
include_once(dirname(__FILE__)."/ressources/class.sockets.inc");
include_once(dirname(__FILE__)."/ressources/class.mysql.inc");
$sock=new sockets();
$proto="http";
if (isset($_SERVER['HTTPS'])){if (strtolower($_SERVER['HTTPS']) == 'on'){$proto="https";}}
$GLOBALS["JS_HEAD_PREPREND"]="$proto://{$_SERVER["SERVER_NAME"]}:{$_SERVER["SERVER_PORT"]}";
$GLOBALS["JS_NO_CACHE"]=true;
$t=time();
include_once(dirname(__FILE__)."/ressources/class.page.builder.inc");
include_once(dirname(__FILE__)."/ressources/class.templates.inc");
$page=CurrentPageName();
$tpl=new templates();
$ask_password=$tpl->javascript_parse_text("{password}:");
$url=base64_decode($_GET["url"]);
$clientaddr=base64_decode($_GET["clientaddr"]);
$array=parse_url($url);
$Whitehost=strtolower($array["host"]);
$pp=new pagebuilder();
$head=$pp->jsArtica()."\n\n".$pp->headcss();
if(preg_match("#^www.(.+)#", $Whitehost,$re)){$Whitehost=$re[1];}
$yahoo=$pp->YahooBody();
$t=time();
$unlock=$tpl->_ENGINE_parse_body("{unlock}");
$title="$unlock &laquo;$Whitehost&raquo;";
$html="<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">
<html lang=\"en\">
<head>
<meta charset=\"utf-8\">
<title>$ask_password</title>
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
<meta name=\"description\" content=\"\">
<meta name=\"author\" content=\"\">
<link rel=\"stylesheet\" type=\"text/css\" href=\"/bootstrap/css/bootstrap.css\">
<link rel=\"stylesheet\" type=\"text/css\" href=\"/bootstrap/css/bootstrap-responsive.css\">
<script type=\"text/javascript\" language=\"javascript\" src=\"{$GLOBALS["JS_HEAD_PREPREND"]}/mouse.js\"></script>
<script type=\"text/javascript\" language=\"javascript\" src=\"{$GLOBALS["JS_HEAD_PREPREND"]}/js/md5.js\"></script>
<script type=\"text/javascript\" language=\"javascript\" src=\"{$GLOBALS["JS_HEAD_PREPREND"]}/XHRConnection.js\"></script>
<script type=\"text/javascript\" language=\"javascript\" src=\"{$GLOBALS["JS_HEAD_PREPREND"]}/js/float-barr.js\"></script>
<script type=\"text/javascript\" language=\"javascript\" src=\"{$GLOBALS["JS_HEAD_PREPREND"]}/TimersLogs.js\"></script>
<script type=\"text/javascript\" language=\"javascript\" src=\"{$GLOBALS["JS_HEAD_PREPREND"]}/js/artica_confapply.js\"></script>
<script type=\"text/javascript\" language=\"javascript\" src=\"{$GLOBALS["JS_HEAD_PREPREND"]}/js/edit.user.js\"></script>
<script type=\"text/javascript\" language=\"javascript\" src=\"{$GLOBALS["JS_HEAD_PREPREND"]}/js/cookies.js\"></script>
<script type=\"text/javascript\" language=\"javascript\" src=\"{$GLOBALS["JS_HEAD_PREPREND"]}/default.js\"></script>
<script type=\"text/javascript\" language=\"javascript\" src=\"{$GLOBALS["JS_HEAD_PREPREND"]}/ressources/templates/endusers/js/jquery-1.8.0.min.js\"></script>
<script type=\"text/javascript\" language=\"javascript\" src=\"{$GLOBALS["JS_HEAD_PREPREND"]}/ressources/templates/endusers/js/jquery-ui-1.8.23.custom.min.js\"></script>
<script type='text/javascript' language='javascript' src='{$GLOBALS["JS_HEAD_PREPREND"]}/js/jquery.uilock.min.js'></script>
<script type='text/javascript' language='javascript' src='{$GLOBALS["JS_HEAD_PREPREND"]}/js/jquery.blockUI.js'></script>
<style type=\"text/css\">
body {
padding-top: 40px;
padding-bottom: 40px;
background-color: #f5f5f5;
}
.form-signin {
max-width: 300px;
padding: 19px 29px 29px;
margin: 0 auto 20px;
background-color: #fff;
border: 1px solid #e5e5e5;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05);
-moz-box-shadow: 0 1px 2px rgba(0,0,0,.05);
box-shadow: 0 1px 2px rgba(0,0,0,.05);
}
.form-signin .form-signin-heading,
.form-signin .checkbox {
margin-bottom: 10px;
}
.form-signin input[type=\"text\"],
.form-signin input[type=\"password\"] {
font-size: 16px;
height: auto;
margin-bottom: 15px;
padding: 7px 9px;
}
</style>
<!--[if IE]>
<link rel=\"stylesheet\" type=\"text/css\" href=\"{$GLOBALS["JS_HEAD_PREPREND"]}/bootstrap/css/ie-only.css\" />
<![endif]-->
</head>
<body>
<input type='hidden' id='LoadAjaxPicture' name=\"LoadAjaxPicture\" value=\"{$GLOBALS["JS_HEAD_PREPREND"]}/ressources/templates/endusers/ajax-loader-eu.gif\">
<div class=\"form-signin\">
<div id='div-$t'></div>
<h2 class=\"form-signin-heading\">$title</h2>
<input type=\"password\" class=\"input-block-level\" placeholder=\"Password\" id=\"PASS-$t\">
<button class=\"btn btn-large btn-primary\" type=\"button\" id=\"signin\">$unlock</button>
</div>
<script type=\"text/javascript\">
$('#signin').on('click', function (e) {
//if(!checkEnter(e)){return;}
SendPass$t();
});
$('.input-block-level').keypress(function (e) {
if (e.which == 13) {
SendPass$t();
}
});
var xSendPass$t= function (obj) {
var tempvalue=obj.responseText;
if(tempvalue.length>3){alert(tempvalue);}
document.getElementById('div-$t').innerHTML='';
}
function SendPass$t(){
var password=MD5(document.getElementById('PASS-$t').value);
var XHR = new XHRConnection();
XHR.appendData('password',password);
XHR.appendData('CLIENT','$clientaddr');
XHR.appendData('Whitehost','$Whitehost');
AnimateDiv('div-$t');
XHR.sendAndLoad('{$GLOBALS["JS_HEAD_PREPREND"]}/$page', 'POST',xSendPass$t);
}
</script>
</body>
</html>";
echo $html;return;
}
function parseTemplate_categoryname($category,$license=0,$nosuffix=0){
$CATEGORY_PLUS_TXT=null;
parseTemplateLogs("parseTemplate_categoryname($category,$license)",__FUNCTION__,__FILE__,__LINE__);
$sock=new sockets();
$SquidGuardApacheShowGroupNameTXT=null;
if($license==1){
$SquidGuardApacheShowGroupName=$sock->GET_INFO("SquidGuardApacheShowGroupName");
if(!is_numeric($SquidGuardApacheShowGroupName)){$SquidGuardApacheShowGroupName=0;}
if($SquidGuardApacheShowGroupName==1){
$SquidGuardApacheShowGroupNameTXT=$sock->GET_INFO("SquidGuardApacheShowGroupNameTXT");
if($SquidGuardApacheShowGroupNameTXT==null){
$LicenseInfos=unserialize(base64_decode($sock->GET_INFO("LicenseInfos")));
if($LicenseInfos["COMPANY"]==null){
$WizardSavedSettings=unserialize(base64_decode($sock->GET_INFO("WizardSavedSettings")));
$LicenseInfos["COMPANY"]=$WizardSavedSettings["company_name"];}
}
$SquidGuardApacheShowGroupNameTXT=$LicenseInfos["COMPANY"];
}
}
$category=strtolower(trim($category));
include_once(dirname(__FILE__)."/ressources/class.ufdbguard-tools.inc");
if(preg_match("#^art(.+)#", $category,$re)){
parseTemplateLogs("Parsing: `$category`=`{$re[1]}`",__FUNCTION__,__FILE__,__LINE__);
$category=CategoryCodeToCatName($category);
$CATEGORY_PLUS_TXT="Artica Database";
$users=new usersMenus();
if($users->WEBSECURIZE){$CATEGORY_PLUS_TXT="Web Securize Database";}
if($users->LANWANSAT){$CATEGORY_PLUS_TXT="LanWanSAT Database";}
}
if(preg_match("#^tls(.+)#", $category,$re)){
parseTemplateLogs("Parsing: `$category`=`{$re[1]}`",__FUNCTION__,__FILE__,__LINE__);
$category=CategoryCodeToCatName($category);
$CATEGORY_PLUS_TXT="Toulouse University Database";
}
parseTemplateLogs("Parsing: `$category` - $CATEGORY_PLUS_TXT nosuffix=$nosuffix",__FUNCTION__,__FILE__,__LINE__);
if($nosuffix==1){return $category;}
if($SquidGuardApacheShowGroupNameTXT<>null){$CATEGORY_PLUS_TXT=$SquidGuardApacheShowGroupNameTXT;}
if($CATEGORY_PLUS_TXT<>null){
return $category." (".$CATEGORY_PLUS_TXT.")";
}
return $category;
}
function hostfrom_url($url){
$URL_ARRAY=parse_url($url);
$src_hostname=$URL_ARRAY["host"];
if(preg_match("#^www.(.+)#", $src_hostname,$re)){$src_hostname=$re[1];}
if(preg_match("#^(.+?):[0-9]+#", $src_hostname,$re)){$src_hostname=$re[1];}
return $src_hostname;
}
function CacheManager_default(){
$sock=new sockets();
$LicenseInfos=unserialize(base64_decode($sock->GET_INFO("LicenseInfos")));
$WizardSavedSettings=unserialize(base64_decode($sock->GET_INFO("WizardSavedSettings")));
if($LicenseInfos["EMAIL"]==null){$LicenseInfos["EMAIL"]=$WizardSavedSettings["mail"];}
if($LicenseInfos["EMAIL"]==null){$LicenseInfos["EMAIL"]="contact@articatech.com";}
$LicenseInfos["EMAIL"]=str_replace("'", "", $LicenseInfos["EMAIL"]);
$LicenseInfos["EMAIL"]=str_replace('"', "", $LicenseInfos["EMAIL"]);
$LicenseInfos["EMAIL"]=str_replace(' ', "", $LicenseInfos["EMAIL"]);
return $LicenseInfos["EMAIL"];
}
function CacheManager(){
$sock=new sockets();
$cache_mgr_user=$sock->GET_INFO("cache_mgr_user");
if($cache_mgr_user<>null){return $cache_mgr_user;}
return CacheManager_default();
}
function parseadmin($emailTemplate,$subj){
$CacheManager=CacheManager();
$subject=rawurlencode("Web Filtering complain [$subj]");
$emailTemplate=rawurlencode($emailTemplate);
return "<a href=\"mailto:$CacheManager?subject=$subject&body=$emailTemplate\">$CacheManager</a>";
}
function parseTemplate(){
session_start();
$template_default_file=dirname(__FILE__)."/ressources/databases/dansguard-template.html";
if(isset($_GET["verbose"])){$GLOBALS["VERBOSE"]=true;}
include_once(dirname(__FILE__)."/ressources/class.sockets.inc");
include_once(dirname(__FILE__)."/ressources/class.mysql.inc");
$sock=new sockets();
$users=new usersMenus();
//$q=new mysql_squid_builder();
$UfdbGuardRedirectCategories=unserialize(base64_decode($sock->GET_INFO("UfdbGuardRedirectCategories")));
$SquidGuardWebFollowExtensions=$sock->GET_INFO("SquidGuardWebFollowExtensions");
$SquidGuardServerName=$sock->GET_INFO("SquidGuardServerName");
$SquidGuardApachePort=$sock->GET_INFO("SquidGuardApachePort");
$SquidGuardWebUseLocalDatabase=$sock->GET_INFO("SquidGuardWebUseLocalDatabase");
$SquidGuardWebBlankReferer=intval($sock->GET_INFO("SquidGuardWebBlankReferer"));
if(!is_numeric($SquidGuardWebFollowExtensions)){$SquidGuardWebFollowExtensions=1;}
if(!is_numeric($SquidGuardWebUseLocalDatabase)){$SquidGuardWebUseLocalDatabase=0;}
$CATEGORY_SOURCE=$_GET["category"];
$TARGET_GROUP_SOURCE=$_GET["targetgroup"];
$proto="http";
$QUERY_STRING=$_SERVER["QUERY_STRING"];
$HTTP_REFERER=$_SERVER["HTTP_REFERER"];
$url=$_GET["url"];
$URL_HOST=hostfrom_url($url);
$HTTP_REFERER_HOST=hostfrom_url($HTTP_REFERER);
if($SquidGuardWebBlankReferer==1){
if($URL_HOST<>$HTTP_REFERER_HOST){
$data="<html><head></head><body></body></html>";
header("Content-Length: ".strlen($data));
header("Content-Type: text/html");
echo $data;
die();
}
}
if (isset($_SERVER['HTTPS'])){if (strtolower($_SERVER['HTTPS']) == 'on'){$proto="https";}}
while (list ($num, $ligne) = each ($_GET) ){
parseTemplateLogs("GET[$num]=`$ligne`",__FUNCTION__,__FILE__,__LINE__);
}
$GLOBALS["JS_NO_CACHE"]=true;
$GLOBALS["JS_HEAD_PREPREND"]="$proto://{$_SERVER["SERVER_NAME"]}:{$_SERVER["SERVER_PORT"]}";
if($SquidGuardWebFollowExtensions==1){
if(parseTemplate_extension($_GET["url"])){return;}
}
if(parseTemplateForcejs($_GET["url"])){
parseTemplateLogs("JS detected : For {$_GET["url"]}",__FUNCTION__,__FILE__,__LINE__);
header("content-type: application/x-javascript");
echo "// blocked by url filtering\n";
return true;
return;
}
$defaultjs="alert('Disabled')";
$ADD_JS_PACK=false;
if($SquidGuardWebUseLocalDatabase==1){
$clientaddr=base64_encode($_GET["clientaddr"]);
$defaultjs="s_PopUp('{$GLOBALS["JS_HEAD_PREPREND"]}/". basename(__FILE__)."?SquidGuardWebUseLocalDatabase=1&url=".base64_encode("{$_GET["url"]}")."&clientaddr=$clientaddr',640,350)";
$ADD_JS_PACK=true;
}
if($users->CORP_LICENSE){$LICENSE=1;$FOOTER=null;}
if(!$users->CORP_LICENSE){$LICENSE=0;}
parseTemplateLogs("{$_GET["clientaddr"]}: Category=`$CATEGORY_SOURCE` targetgroup=`{$_GET["targetgroup"]}` LICENSE:$LICENSE",__FUNCTION__,__FILE__,__LINE__);
$CATEGORY_KEY=null;
$_GET["targetgroup"]=parseTemplate_categoryname($_GET["targetgroup"],$LICENSE);
$_GET["clientgroup"]=parseTemplate_categoryname($_GET["clientgroup"],$LICENSE);
$_GET["category"]=parseTemplate_categoryname($CATEGORY_SOURCE,$LICENSE);
$CATEGORY_KEY=parseTemplate_categoryname($CATEGORY_SOURCE,$LICENSE,1);
if($CATEGORY_KEY==null){
$CATEGORY_KEY=parseTemplate_categoryname($TARGET_GROUP_SOURCE,$LICENSE,1);
}
$_CATEGORIES_K=$_GET["category"];
$_RULE_K=$_GET["clientgroup"];
if($_CATEGORIES_K==null){$_CATEGORIES_K=$_GET["targetgroup"];}
if($_RULE_K==null){$_RULE_K="{web_filtering}";}
$REASONGIVEN="{web_filtering} $_CATEGORIES_K";
if($_CATEGORIES_K=="restricted_time"){$REASONGIVEN="{restricted_access}";}
parseTemplateLogs("{$REASONGIVEN}: _CATEGORIES_K=`$_CATEGORIES_K` _RULE_K=$_RULE_K` LICENSE:$LICENSE",__FUNCTION__,__FILE__,__LINE__);
$IpToUid=null;
//$IpToUid=$q->IpToUid($_GET["clientaddr"]);
if($IpToUid<>null){$IpToUid="&nbsp;($IpToUid)";}
if($LICENSE==1){
if($CATEGORY_KEY<>null){
$RedirectCategory=$UfdbGuardRedirectCategories[$CATEGORY_KEY];
if($RedirectCategory["enable"]==1){
if($RedirectCategory["blank_page"]==1){
parseTemplateLogs("[$CATEGORY_KEY]: blank_page : For {$_GET["url"]}",__FUNCTION__,__FILE__,__LINE__);
header("HTTP/1.1 200 OK");
die();
return;
}
if(trim($RedirectCategory["template_data"])<>null){
header('Content-Type: text/html; charset=iso-8859-1');
$TemplateErrorFinal=$RedirectCategory["template_data"];
return;
}
}
}
}
$EnableSquidFilterWhiteListing=$sock->GET_INFO("EnableSquidFilterWhiteListing");
if(isset($_GET["rule-id"])){$ID=$_GET["rule-id"];}
parseTemplateLogs("ID: $ID",__FUNCTION__,__FILE__,__LINE__);
if(isset($_GET["fatalerror"])){$ID=0;}
if(isset($_GET["loading-database"])){$ID=0;}
if($LICENSE==1){
if(is_numeric($ID)){
if($ID==0){
$ligne["groupname"]="Default";
}else{
$sql="SELECT groupname FROM webfilter_rules WHERE ID=$ID";
$q=new mysql_squid_builder();
$ligne=mysql_fetch_array($q->QUERY_SQL($sql));
$ruleName=$ligne["groupname"];
}
}else{
writelogs("ID: not a numeric",__FUNCTION__,__FILE__,__LINE__);
}
}
if(isset($_GET["fatalerror"])){
$_GET["clientaddr"]=$_SERVER["REMOTE_ADDR"];
$_GET["clientname"]=$_SERVER["REMOTE_HOST"];
$REASONGIVEN="{webfiltering_issue}";
$_CATEGORIES_K="{system_Webfiltering_error}";
$_RULE_K="{service_error}";
$_GET["url"]=$_SERVER['HTTP_REFERER'];
}
if(isset($_GET["loading-database"])){
$_GET["clientaddr"]=$_SERVER["REMOTE_ADDR"];
$_GET["clientname"]=$_SERVER["REMOTE_HOST"];
$REASONGIVEN="{Webfiltering_maintenance}";
$_CATEGORIES_K="{please_wait_reloading_databases}";
$_RULE_K="{waiting_service}....";
$_GET["url"]=$_SERVER['HTTP_REFERER'];
}
if(!isset($_SESSION["IPRES"][$_GET["clientaddr"]])){$_SESSION["IPRES"][$_GET["clientaddr"]]=gethostbyaddr($_GET["clientaddr"]);}
if(isset($_GET["source"])){$_GET["clientaddr"]=$_GET["source"];}
if(isset($_GET["user"])){$_GET["clientname"]=$_GET["user"];}
if(isset($_GET["virus"])){$_GET["targetgroup"]=$_GET["virus"];$ruleName=null;}
if($_GET["clientuser"]<>null){$_GET["clientname"]=$_GET["clientuser"];}
$ruleName=parseTemplate_categoryname($ruleName,$LICENSE);
$ARRAY["URL"]=$_GET["url"];
$ARRAY["IPADDR"]=$_GET["clientaddr"];
$ARRAY["REASONGIVEN"]=$REASONGIVEN;
$ARRAY["CATEGORY_KEY"]=$CATEGORY_KEY;
$ARRAY["RULE_ID"]=$ID;
$ARRAY["CATEGORY"]=$_CATEGORIES_K;
$ARRAY["RULE"]=$_RULE_K;
if($ruleName<>null){
$ARRAY["RULE"]=$ruleName;
}
$ARRAY["targetgroup"]=$_GET["targetgroup"];
$ARRAY["IpToUid"]=$IpToUid;
$ARRAY["clientname"]=$_GET["clientname"];
$ARRAY["HOST"]=$_SESSION["IPRES"][$_GET["clientaddr"]];
echo parseTemplate_build_main($ARRAY);
}
function GetSquidUser(){
$unix=new unix();
$squidconf=$unix->SQUID_CONFIG_PATH();
$group=null;
if(!is_file($squidconf)){
echo "Starting......: ".date("H:i:s")." squidGuard unable to get squid configuration file\n";
return "squid:squid";
}
$array=explode("\n",@file_get_contents($squidconf));
while (list ($index, $line) = each ($array)){
if(preg_match("#cache_effective_user\s+(.+)#",$line,$re)){
$user=trim($re[1]);
}
if(preg_match("#cache_effective_group\s+(.+)#",$line,$re)){
$group=trim($re[1]);
}
}
if($group==null){$group="squid";}
return "$user:$group";
}
function ParseDirectory($path){
if(!is_dir($path)){echo "$path No such directory\n";return;}
$sock=new sockets();
$unix=new unix();
$uuid=$unix->GetUniqueID();
if($uuid==null){echo "No uuid\n";return;}
$handle=opendir($path);
$q=new mysql_squid_builder();
$f=false;
while (false !== ($dir = readdir($handle))) {
if($dir=="."){continue;}
if($dir==".."){continue;}
if(!is_file("$path/$dir/domains")){echo "$path/$dir/domains no such file\n";continue;}
$category=sourceCategoryToArticaCategory($dir);
if($category==null){echo "$path/$dir/domains no such category\n";continue;}
$table="category_".$q->category_transform_name($category);
if(!$q->TABLE_EXISTS($table)){echo "$category -> no such table $table\n";continue;}
inject($category,$table,"$path/$dir/domains");
}
$tables=$q->LIST_TABLES_CATEGORIES();
while (list ($table, $www) = each ($tables)){
$sql="SELECT COUNT(zmd5) as tcount FROM $table WHERE sended=0 and enabled=1";
$ligne=mysql_fetch_array($q->QUERY_SQL($sql));
$prefix="INSERT IGNORE INTO categorize (zmd5 ,pattern,zDate,uuid,category) VALUES";
if($ligne["tcount"]>0){
echo "$table {$ligne["tcount"]} items to export\n";
$results=$q->QUERY_SQL("SELECT * FROM $table WHERE sended=0 and enabled=1");
while($ligne2=mysql_fetch_array($results,MYSQL_ASSOC)){
$f[]="('{$ligne2["zmd5"]}','{$ligne2["pattern"]}','{$ligne2["zDate"]}','$uuid','{$ligne2["category"]}')";
$c++;
if(count($f)>3000){
$q->QUERY_SQL($prefix.@implode(",",$f));
if(!$q->ok){echo $q->mysql_error."\n";return;}
$f=array();
}
}
$q->QUERY_SQL("UPDATE $table SET sended=1 WHERE sended=0");
}
}
if(count($f)>0){
$q->QUERY_SQL($prefix.@implode(",",$f));
$f=array();
}
}
function sourceCategoryToArticaCategory($category){
$array["gambling"]="gamble";
$array["gamble"]="gamble";
$array["hacking"]="hacking";
$array["malware"]="malware";
$array["phishing"]="phishing";
$array["porn"]="porn";
$array["sect"]="sect";
$array["socialnetwork"]="socialnet";
$array["violence"]="violence";
$array["adult"]="porn";
$array["ads"]="publicite";
$array["warez"]="warez";
$array["drugs"]="drogue";
$array["forums"]="forums";
$array["filehosting"]="filehosting";
$array["games"]="games";
$array["astrology"]="astrology";
$array["publicite"]="publicite";
$array["radio"]="webradio";
$array["sports"]="recreation/sports";
$array["getmarried"]="getmarried";
$array["police"]="police";
$array["press"]="news";
$array["audio-video"]="audio-video";
$array["webmail"]="webmail";
$array["chat"]="chat";
$array["social_networks"]="socialnet";
$array["ads"]="publicite";
$array["adult"]="porn";
$array["aggressive"]="aggressive";
$array["astrology"]="astrology";
$array["audio-video"]="audio-video";
$array["bank"]="finance/banking";
$array["blog"]="blog";
$array["celebrity"]="celebrity";
$array["chat"]="chat";
$array["cleaning"]="cleaning";
$array["dangerous_material"]="dangerous_material";
$array["dating"]="dating";
$array["drugs"]="porn";
$array["filehosting"]="filehosting";
$array["financial"]="financial";
$array["forums"]="forums";
$array["gambling"]="gamble";
$array["games"]="games";
$array["hacking"]="hacking";
$array["jobsearch"]="jobsearch";
$array["liste_bu"]="liste_bu";
$array["malware"]="malware";
$array["marketingware"]="marketingware";
$array["mixed_adult"]="mixed_adult";
$array["mobile-phone"]="mobile-phone";
$array["phishing"]="phishing";
$array["radio"]="webradio";
$array["reaffected"]="reaffected";
$array["redirector"]="redirector";
$array["remote-control"]="remote-control";
$array["sect"]="sect";
$array["sexual_education"]="sexual_education";
$array["shopping"]="shopping";
$array["social_networks"]="socialnet";
$array["sports"]="recreation/sports";
$array["getmarried"]="getmarried";
$array["police"]="police";
$array["tricheur"]="tricheur";
$array["violence"]="violence";
$array["warez"]="warez";
$array["webmail"]="webmail";
$array["ads"]="publicite";
$array["adult"]="porn";
$array["aggressive"]="aggressive";
$array["astrology"]="astrology";
$array["audio-video"]="audio-video";
$array["bank"]="finance/banking";
$array["blog"]="blog";
$array["celebrity"]="celebrity";
$array["chat"]="chat";
$array["cleaning"]="cleaning";
$array["dangerous_material"]="dangerous_material";
$array["dating"]="dating";
$array["drugs"]="porn";
$array["filehosting"]="filehosting";
$array["financial"]="financial";
$array["forums"]="forums";
$array["gambling"]="gamble";
$array["games"]="games";
$array["hacking"]="hacking";
$array["jobsearch"]="jobsearch";
$array["liste_bu"]="liste_bu";
$array["malware"]="malware";
$array["marketingware"]="marketingware";
$array["mixed_adult"]="mixed_adult";
$array["mobile-phone"]="mobile-phone";
$array["phishing"]="phishing";
$array["radio"]="webradio";
$array["reaffected"]="reaffected";
$array["redirector"]="redirector";
$array["remote-control"]="remote-control";
$array["sect"]="sect";
$array["sexual_education"]="sexual_education";
$array["shopping"]="shopping";
$array["social_networks"]="socialnet";
$array["sports"]="recreation/sports";
$array["getmarried"]="getmarried";
$array["police"]="police";
$array["tricheur"]="tricheur";
$array["violence"]="violence";
$array["warez"]="warez";
$array["webmail"]="webmail";
$array["adv"]="publicite";
$array["aggressive"]="aggressive";
$array["automobile"]="automobile/cars";
$array["chat"]="chat";
$array["dating"]="dating";
$array["downloads"]="downloads";
$array["drugs"]="drugs";
$array["education"]="recreation/schools";
$array["finance"]="financial";
$array["forum"]="forums";
$array["gamble"]="gamble";
$array["government"]="governments";
$array["hacking"]="hacking";
$array["hospitals"]="hospitals";
$array["imagehosting"]="imagehosting";
$array["isp"]="isp";
$array["jobsearch"]="jobsearch";
$array["library"]="books";
$array["models"]="models";
$array["movies"]="movies";
$array["music"]="music";
$array["news"]="news";
$array["porn"]="porn";
$array["redirector"]="redirector";
$array["religion"]="religion";
$array["remotecontrol"]="remote-control";
$array["ringtones"]="ringtones";
$array["searchengines"]="searchengines";
$array["shopping"]="shopping";
$array["socialnet"]="socialnet";
$array["spyware"]="spyware";
$array["tracker"]="tracker";
$array["updatesites"]="updatesites";
$array["violence"]="violence";
$array["warez"]="warez";
$array["weapons"]="weapons";
$array["webmail"]="webmail";
$array["webphone"]="webphone";
$array["webradio"]="webradio";
$array["webtv"]="webtv";
if(!isset($array[$category])){return null;}
return $array[$category];
}
// exec.squidguard.php --inject porn /root/blablabl/domains
function inject($category,$table=null,$file=null){
include_once(dirname(__FILE__)."/ressources/class.dansguardian.inc");
$unix=new unix();
$q=new mysql_squid_builder();
if(is_file($category)){
$file=$category;
$category_name=basename($file);
echo "$file -> $category_name\n";
if(preg_match("#(.+?)\.gz$#", $category_name)){
echo "$category_name -> gunzip\n";
$new_category_name=str_replace(".gz", "", $category_name);
$gunzip=$unix->find_program("gunzip");
$target_file=dirname($file)."/$new_category_name";
$cmd="/bin/gunzip -d -c \"$file\" >$target_file 2>&1";
echo "$cmd\n";
shell_exec($cmd);
if(!is_file($target_file)){echo "Uncompress failed\n";return;}
$file=$target_file;
$table=$new_category_name;
$category=$q->tablename_tocat($table);
echo "$new_category_name -> $table\n";
}else{
$table=$category_name;
echo "$new_category_name -> $table\n";
$category=$q->tablename_tocat($table);
}
echo "Table: $table\nSource File:$file\nCategory: $category\n";
}
if(!is_file($file)){
if(!is_file($table)){echo "`$table` No such file\n";}
if(is_file($table)){$file=$table;$table=null;}
}
if($table==null){
$table="category_".$q->category_transform_name($category);
echo "Table will be $table\n";
}
if(!$q->TABLE_EXISTS($table)){
echo "$table does not exists, check if it is an official one\n";
$dans=new dansguardian_rules();
if(isset($dans->array_blacksites[$category])){
$q->CreateCategoryTable($category);
}
}
if(!$q->TABLE_EXISTS($table)){
echo "`$category` -> no such table \"$table\"\n";return;
}
$sql="SELECT COUNT(*) AS TCOUNT FROM $table";
$q->QUERY_SQL($sql);
if(!$q->ok){
echo $q->mysql_error."\n";
if(preg_match("#is marked as crashed and last#", $q->mysql_error)){
echo "`$table` -> crashed, remove \"$table\"\n";
$q->QUERY_SQL("DROP TABLE $table");
$q->QUERY_SQL("flush tables");
$q=new mysql_squid_builder();
echo "`$table` -> Create category \"$category\"\n";
$q->CreateCategoryTable($category);
$q->CreateCategoryTable($category);
$q=new mysql_squid_builder();
}
if(!$q->TABLE_EXISTS($table)){
echo "`$category` -> no such table \"$table\"\n";
return;
}
}
if($file==null){
$dir="/var/lib/squidguard";
if($GLOBALS["SHALLA"]){$dir="/root/shalla/BL";}
if(!is_file("$dir/$category/domains")){
echo "$dir/$category/domains no such file";
return;
}
$file="$dir/$category/domains";
}
if(!is_file($file)){echo "$file no such file";return;}
$sock=new sockets();
$unix=new unix();
$uuid=$unix->GetUniqueID();
if($uuid==null){echo "No uuid\n";return;}
echo "open $file\n";
$handle = @fopen($file, "r");
if (!$handle) {echo "Failed to open file\n";return;}
$q=new mysql_squid_builder();
if($GLOBALS["CATTO"]<>null){$category=$GLOBALS["CATTO"];}
$countstart=$q->COUNT_ROWS($table);
$prefix="INSERT IGNORE INTO $table (zmd5,zDate,category,pattern,uuid) VALUES ";
echo "$prefix\n";
$c=0;
$CBAD=0;
$CBADIP=0;
$CBADNULL=0;
while (!feof($handle)){
$c++;
$www =trim(fgets($handle, 4096));
if($www==null){$CBADNULL++;continue;}
$www=str_replace('"', "", $www);
if(preg_match("#^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$#", $www)){$CBADIP++;continue;}
$www=trim(strtolower($www));
if($www=="thisisarandomentrythatdoesnotexist.com"){$CBAD++;continue;}
if($www==null){$CBADNULL++;continue;}
if(preg_match("#(.+?)\s+(.+)#", $www,$re)){$www=$re[1];}
if(preg_match("#^\.(.*)$#", $www,$re)){$www=$re[1];}
if(strpos($www, "#")>0){echo "FALSE: $www\n";continue;}
if(strpos($www, "'")>0){echo "FALSE: $www\n";continue;}
if(strpos($www, "{")>0){echo "FALSE: $www\n";continue;}
if(strpos($www, "(")>0){echo "FALSE: $www\n";continue;}
if(strpos($www, ")")>0){echo "FALSE: $www\n";continue;}
if(strpos($www, "%")>0){echo "FALSE: $www\n";continue;}
$md5=md5($www.$category);
$n[]="('$md5',NOW(),'$category','$www','$uuid')";
if(count($n)>6000){
$sql=$prefix.@implode(",",$n);
$q->QUERY_SQL($sql,"artica_backup");
if(!$q->ok){echo $q->mysql_error."\n";$n=array();continue;}
$countend=$q->COUNT_ROWS($table);
$final=$countend-$countstart;
echo "".numberFormat($c,0,""," ")." items, ".numberFormat($final,0,""," ")." new entries added - $CBADNULL bad entries for null value,$CBADIP entries for IP addresses\n";
$n=array();
}
}
fclose($handle);
if(count($f)>0){
if($c>0){
$countend=$q->COUNT_ROWS($table);
$final=$countend-$countstart;
echo "$c items, $final new entries added - $CBAD bad entries\n";
$sql=$prefix.@implode(",",$n);