logging != true ) return; if ( $this->log2syslog == true ) syslog(LOG_NOTICE,$message); else { $fp = fopen($this->logfile, "a"); if ($fp) { fwrite($fp,trim($message)."\n"); fclose($fp); } else syslog(LOG_NOTICE,"ispc3-awstats plugin cannot write to ".$this->logfile); } return true; } /* This function is called when the plugin is loaded */ function onLoad() { global $app; // // Register for the events // $app->plugins->registerEvent('web_domain_insert',$this->plugin_name,'domain_insert'); $app->plugins->registerEvent('web_domain_update',$this->plugin_name,'domain_update'); $app->plugins->registerEvent('web_domain_delete',$this->plugin_name,'domain_delete'); $app->plugins->registerEvent('client_insert',$this->plugin_name,'client_insert'); $app->plugins->registerEvent('client_update',$this->plugin_name,'client_update'); $app->plugins->registerEvent('client_delete',$this->plugin_name,'client_delete'); if ( $this->log2syslog == false && $this->logtype == "overwrite" ) { // wipe log for update $fp = fopen($this->logfile, "w"); fclose($fp); } return true; } function write_global_password_file() { global $app, $conf; $result = $app->db->queryAllRecords("SELECT DISTINCT c.username,c.password FROM web_domain wd,sys_group sg,client c where wd.sys_groupid = sg.groupid and sg.client_id = c.client_id"); // // GLOBAL HTPASSWORD // $fp = fopen($this->globalpasswordfile,"w"); if ($fp) { $userlist=''; foreach($result as $row) { fwrite($fp,$row['username'].":".$row['password']."\n"); $userlist .= $row['username']." "; } if ( ! empty($this->admin_pass) ) fwrite($fp,$this->admin_name.":".crypt($this->admin_pass)."\n"); fclose($fp); $this->logger("write_global_password_file() Global .htpasswd_stats updated -> $userlist"); } return true; } function write_password_file($row) { $domain = $row['domain']; $username = $row['username']; $password = $row['password']; $statspassword = $row['stats_password']; $domroot = $row['document_root']; // // HTPASSWORD // $fp = fopen($domroot."/.htpasswd_stats","w"); if ($fp) { $writepass=$statspassword!=""?$statspassword:$password; fwrite($fp,$username.":".$writepass."\n"); if ( ! empty($this->admin_pass) ) fwrite($fp,$this->admin_name.":".crypt($this->admin_pass)."\n"); fclose($fp); $this->logger("write_password_file() Domain::$domain DomRoot::$domroot .htpasswd_stats updated"); } return true; } function write_domain_access($row) { $domain = $row['domain']; if ( $row['is_subdomainwww']==1 && $row['subdomain']=='www' ) $domain = "www.".$row['domain']; $username = $row['username']; $password = $row['password']; $statspassword = $row['stats_password']; $domroot = $row['document_root']; $this->logger("write_domain_access() Domain::$domain DomRoot::$domroot .htaccess update base off data ".print_r($row,true)); // // HTACCESS // if ( file_exists("/etc/awstats/awstats.".$row["old"]["domain"].".conf") && is_file("/etc/awstats/awstats.".$row["old"]["domain"].".conf") ) unlink("/etc/awstats/awstats.".$row["old"]["domain"].".conf"); $fp = fopen($domroot."/web/stats/.htaccess","w"); if ($fp) { $access ="AuthType Basic\n"; $access.="AuthName \"Site Client Access Only\"\n"; $access.="AuthUserFile $domroot/.htpasswd_stats\n"; $access.="\n"; $access.="require valid-user\n"; $access.="\n"; if ( $this->jawstats_enabled ) $access.="Redirect 301 /stats http://$domain/jawstats/?config=$domain&\n\n"; else $access.="Redirect 301 /stats http://$domain/ispcstats/awstats.pl?config=$domain&\n\n"; fwrite($fp,$access); fclose($fp); $this->logger("write_domain_access() Domain::$domain DomRoot::$domroot .htaccess updated"); } else $this->logger("write_domain_access() Failed to open $domroot/web/stats/.htaccess for writing!"); $this->logger("write_domain_access() Domain::$domain DomRoot::$domroot Conf -> /etc/awstats/awstats.".$domain.".conf"); return true; } function write_domain_conf($row) { $domain = $row['domain']; if ( $row['is_subdomainwww']==1 && $row['subdomain']==www ) $domain = "www.".$row['domain']; $username = $row['username']; $password = $row['password']; // // AWSTATA.DOMAIN.CONF // $fp = fopen("/etc/awstats/awstats.".$domain.".conf","w"); if ($fp) { $aw_config = "Include \"/etc/awstats/awstats.conf\"\n"; $aw_config .= "LogFile=\"/var/log/ispconfig/httpd/".$domain."/access.log\"\n"; $aw_config .= "SiteDomain=\"".$domain."\"\n"; $aw_config .= "AllowAccessFromWebToFollowingAuthenticatedUsers=\"$username ".$this->admin_name."\"\n"; fwrite($fp,$aw_config); fclose($fp); $this->logger("write_domain_conf() Domain::$domain Conf updated"); } else $this->logger("write_domain_conf() Domain::$domain Conf failed to update"); } function write_jawstats_config() { global $app, $conf; $result = $app->db->queryAllRecords("SELECT c.username,c.password,wd.domain,wd.document_root FROM web_domain wd,sys_group sg,client c where wd.sys_groupid = sg.groupid and sg.client_id = c.client_id and wd.type='vhost' $mysqlextra"); $template=" \"/var/lib/awstats.data/\",\n"; $template.="\"updatepath\" => \"/usr/lib/cgi-bin/awstats.pl/\",\n"; $template.="\"siteurl\" => \"http://".$row['domain']."\",\n"; $template.="\"theme\" => \"default\",\n"; $template.="\"fadespeed\" => 250,\n"; $template.="\"password\" => \"\",\n"; $template.="\"includes\" => \"\"\n"; $template.=");\n\n"; } file_put_contents($this->jawstats_ispc_conf,$template); } function client_insert($event_name,$data) { global $app, $conf; // $this->write_domain_conf($data); NOT SURE IF THIS ONE IS NEEDED return true; } function client_update($event_name,$data) { global $app, $conf; $this->write_domain_conf($data['new']); $mysqlextra = ""; if ( $this->update_all_clients == false ) $mysqlextra = "and c.username='".$data['new']['username']."'"; $result = $app->db->queryAllRecords("SELECT c.username,c.password,wd.domain,wd.document_root FROM web_domain wd,sys_group sg,client c where wd.sys_groupid = sg.groupid and sg.client_id = c.client_id and wd.type='vhost' $mysqlextra"); foreach($result as $row) { $this->write_domain_conf($row); #EIT $this->write_domain_access($row); if ( $data['new']['username'] != $data['old']['username'] || $data['new']['password'] != $data['old']['password'] ) { $this->logger("client_update() Client::".$data['new']['username']." Username/Password different, updating password files"); $this->write_global_password_file(); $this->write_password_file($row); } } return true; } function client_delete($event_name,$data) { global $app, $conf; $this->write_domain_conf($data['new']); if ( $this->update_all_clients == false ) $mysqlextra = "and c.username='".$data['new']['username']."'"; $result = $app->db->queryAllRecords("SELECT DISTINCT c.username,c.password,wd.domain FROM web_domain wd,sys_group sg,client c where wd.sys_groupid = sg.groupid and sg.client_id = c.client_id and wd.type='vhost' $mysqlextra"); foreach($result as $row) { $this->write_domain_conf($data); if ( $this->update_all_domains == false ) break; } $this->write_global_password_file(); return true; } function domain_update($event_name,$data) { global $app, $conf; $mysqlextra=''; if ( $this->update_all_domains == false ) $mysqlextra = "and wd.domain='".$data['new']['domain']."'"; $result = $app->db->queryAllRecords("SELECT * FROM web_domain wd,sys_group sg,client c where wd.sys_groupid = sg.groupid and sg.client_id = c.client_id and wd.type = 'vhost' $mysqlextra"); foreach($result as $row) { $this->write_domain_conf($row); $this->write_domain_access($row); $this->write_password_file($row); if ( $this->jawstats_enabled ) $this->write_jawstats_config(); } $this->write_global_password_file(); return true; } function domain_insert($event_name,$data) { global $app, $conf; $mysqlextra=''; if ( $this->update_all_domains == false ) $mysqlextra = "and wd.domain='".$data['new']['domain']."'"; $result = $app->db->queryAllRecords("SELECT * FROM web_domain wd,sys_group sg,client c where wd.sys_groupid = sg.groupid and sg.client_id = c.client_id and wd.type = 'vhost' $mysqlextra"); foreach($result as $row) { $this->write_domain_conf($row); $this->write_domain_access($row); $this->write_password_file($row); if ( $this->jawstats_enabled ) $this->write_jawstats_config(); } $this->write_global_password_file(); return true; } function domain_delete($event_name,$data) { if($data["old"]["domain"] != '' ) { if ( @is_file("/etc/awstats/awstats.".$data["old"]["domain"].".conf") ) { @unlink("/etc/awstats/awstats.".$data["old"]["domain"].".conf"); # @unlink($data["old"]["domain"]."/.htpasswd_stats"); $this->logger("domain_delete() /etc/awstats/awstats.".$data["old"]["domain"].".conf removed"); # $this->logger("domain_delete() ".$data["old"]["domain"]."/.htpasswd_stats removed"); } } $this->write_global_password_file(); if ( $this->jawstats_enabled ) $this->write_jawstats_config(); return true; } } ?>