> * * ----------------------------------------------------------------------------- * * See main.inc.php for release information * * manage all the ajax requests * ----------------------------------------------------------------------------- */ // in this case, PHPWG_ROOT_PATH must be declared as an absolute path... define('PHPWG_ROOT_PATH',dirname(dirname(dirname(__FILE__))).'/'); if(!defined('AJAX_CALL')) define('AJAX_CALL', true); /* * set ajax module in admin mode if request is used for admin interface */ if(!isset($_REQUEST['ajaxfct'])) $_REQUEST['ajaxfct']=''; if(preg_match('/^admin\./i', $_REQUEST['ajaxfct'])) define('IN_ADMIN', true); // the common.inc.php file loads all the main.inc.php plugins files include_once(PHPWG_ROOT_PATH.'include/common.inc.php' ); include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCAjax.class.inc.php'); include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCUserAgent.class.inc.php'); include_once('estat_root.class.inc.php'); load_language('plugin.lang', ESTAT_PATH); class EStat_Ajax extends EStat_root { const MAX_LOAD_LOG = 1000; /** * constructor */ public function __construct($prefixeTable, $filelocation) { parent::__construct($prefixeTable, $filelocation); $this->loadConfig(); $this->checkRequest(); $this->returnAjaxContent(); } /** * check the $_REQUEST values and set default values * */ protected function checkRequest() { global $user; if(!isset($_REQUEST['errcode'])) $_REQUEST['errcode']=''; GPCAjax::checkToken(); // check if asked function is valid if(!($_REQUEST[GPC_AJAX]=='admin.install.buildIpList' or $_REQUEST[GPC_AJAX]=='admin.migrate.loadLogs' or $_REQUEST[GPC_AJAX]=='admin.migrate.consolidate' or $_REQUEST[GPC_AJAX]=='admin.migrate.finalize' or $_REQUEST[GPC_AJAX]=='admin.stat.periodTreeList' or $_REQUEST[GPC_AJAX]=='admin.stat.history.periodList' or $_REQUEST[GPC_AJAX]=='admin.stat.history' or $_REQUEST[GPC_AJAX]=='admin.stat.history.graphAllPeriods' or $_REQUEST[GPC_AJAX]=='admin.stat.history.graphCurrentPeriod' or $_REQUEST[GPC_AJAX]=='admin.stat.period' or $_REQUEST[GPC_AJAX]=='admin.stat.period.graph' or $_REQUEST[GPC_AJAX]=='admin.stat.ip' or $_REQUEST[GPC_AJAX]=='admin.stat.ip.graphType' or $_REQUEST[GPC_AJAX]=='admin.stat.ip.graphCountry' or $_REQUEST[GPC_AJAX]=='admin.stat.category' or $_REQUEST[GPC_AJAX]=='admin.stat.image' or $_REQUEST[GPC_AJAX]=='admin.fct.validValues' )) $_REQUEST[GPC_AJAX]=''; if(preg_match('/^admin\./i', $_REQUEST['ajaxfct']) and !is_admin()) $_REQUEST['ajaxfct']=''; if($_REQUEST[GPC_AJAX]!='') { switch($_REQUEST[GPC_AJAX]) { case 'admin.install.buildIpList': if(!isset($_REQUEST['start'])) $_REQUEST['ajaxfct']=''; break; case 'admin.migrate.loadLogs': if(!isset($_REQUEST['lastId'])) $_REQUEST['lastId']=0; if(!isset($_REQUEST['period'])) $_REQUEST['period']=''; if(!is_numeric($_REQUEST['lastId']) or $_REQUEST['period']=='' or !preg_match('/\\d{4}-\\d{2}/i', $_REQUEST['period'])) $_REQUEST['ajaxfct']=''; break; case 'admin.migrate.consolidate': if(!isset($_REQUEST['period'])) $_REQUEST['period']=''; if(!isset($_REQUEST['process'])) $_REQUEST['process']=0; if(!is_numeric($_REQUEST['process']) or $_REQUEST['period']=='' or !preg_match('/\\d{4}-\\d{2}/i', $_REQUEST['period'])) $_REQUEST['ajaxfct']=''; break; case 'admin.migrate.finalize': break; case 'admin.stat.history.periodList': break; case 'admin.stat.periodTreeList': break; case 'admin.stat.history': if(!isset($_REQUEST['period'])) $_REQUEST['period']=''; if(!isset($_REQUEST['currentPage'])) $_REQUEST['currentPage']=0; if(!isset($_REQUEST['nbItemsPage'])) $_REQUEST['nbItemsPage']=0; if(!isset($_REQUEST['sort'])) $_REQUEST['sort']=array( array('direction' => 'A', 'id' => 'date') ); if(!isset($_REQUEST['filter'])) $_REQUEST['filter']=array(); if(!is_numeric($_REQUEST['currentPage']) or $_REQUEST['currentPage']<0) $_REQUEST['currentPage']=0; if(!is_numeric($_REQUEST['nbItemsPage']) or $_REQUEST['nbItemsPage']<0) $_REQUEST['nbItemsPage']=50; if($_REQUEST['period']=='') $_REQUEST['ajaxfct']=''; break; case 'admin.stat.history.graphCurrentPeriod': if(!isset($_REQUEST['period'])) $_REQUEST['period']=''; if($_REQUEST['period']=='') $_REQUEST['ajaxfct']=''; break; case 'admin.stat.history.graphAllPeriods': if(!isset($_REQUEST['period'])) $_REQUEST['period']=''; if(!isset($_REQUEST['nbMonth'])) $_REQUEST['nbMonth']=0; break; case 'admin.stat.period': if(!isset($_REQUEST['period'])) $_REQUEST['period']=''; if(!isset($_REQUEST['currentPage'])) $_REQUEST['currentPage']=0; if(!isset($_REQUEST['nbItemsPage'])) $_REQUEST['nbItemsPage']=0; if(!isset($_REQUEST['sort'])) $_REQUEST['sort']=array( array('direction' => 'A', 'id' => 'year'), array('direction' => 'A', 'id' => 'month') ); if(!isset($_REQUEST['additionalFilter'])) $_REQUEST['additionalFilter']=array(); if(!is_numeric($_REQUEST['currentPage']) or $_REQUEST['currentPage']<0) $_REQUEST['currentPage']=0; if(!is_numeric($_REQUEST['nbItemsPage']) or $_REQUEST['nbItemsPage']<0) $_REQUEST['nbItemsPage']=50; if($_REQUEST['period']=='') $_REQUEST['ajaxfct']=''; $_REQUEST['filter']=$_REQUEST['additionalFilter']; break; case 'admin.stat.period.graph': if(!isset($_REQUEST['period'])) $_REQUEST['period']=''; if(!isset($_REQUEST['additionalFilter'])) $_REQUEST['additionalFilter']=array(); if($_REQUEST['period']=='') $_REQUEST['ajaxfct']=''; $_REQUEST['filter']=$_REQUEST['additionalFilter']; break; case 'admin.stat.ip': if(!isset($_REQUEST['period'])) $_REQUEST['period']='a'; if(!isset($_REQUEST['currentPage'])) $_REQUEST['currentPage']=0; if(!isset($_REQUEST['nbItemsPage'])) $_REQUEST['nbItemsPage']=0; if(!isset($_REQUEST['group'])) $_REQUEST['group']=array(); if(!isset($_REQUEST['sort'])) $_REQUEST['sort']=array( array('direction' => 'A', 'id' => 'ip') ); if(!isset($_REQUEST['additionalFilter'])) $_REQUEST['additionalFilter']=array(); if(!isset($_REQUEST['filter'])) $_REQUEST['filter']=array(); if(!is_numeric($_REQUEST['currentPage']) or $_REQUEST['currentPage']<0) $_REQUEST['currentPage']=0; if(!is_numeric($_REQUEST['nbItemsPage']) or $_REQUEST['nbItemsPage']<0) $_REQUEST['nbItemsPage']=50; if($_REQUEST['period']=='') $_REQUEST['ajaxfct']=''; $_REQUEST['filter']['additionalFilter']=$_REQUEST['additionalFilter']; break; case 'admin.stat.ip.graphType': if(!isset($_REQUEST['period'])) $_REQUEST['period']='a'; if(!isset($_REQUEST['additionalFilter'])) $_REQUEST['additionalFilter']=array(); if($_REQUEST['period']=='') $_REQUEST['ajaxfct']=''; $_REQUEST['filter']=$_REQUEST['additionalFilter']; break; case 'admin.stat.ip.graphCountry': if(!isset($_REQUEST['period'])) $_REQUEST['period']='a'; if(!isset($_REQUEST['additionalFilter'])) $_REQUEST['additionalFilter']=array(); if($_REQUEST['period']=='') $_REQUEST['ajaxfct']=''; $_REQUEST['filter']=$_REQUEST['additionalFilter']; break; case 'admin.stat.category': case 'admin.stat.image': if(!isset($_REQUEST['period'])) $_REQUEST['period']='a'; if(!isset($_REQUEST['currentPage'])) $_REQUEST['currentPage']=0; if(!isset($_REQUEST['nbItemsPage'])) $_REQUEST['nbItemsPage']=0; if(!isset($_REQUEST['group'])) $_REQUEST['group']=array(); if(!isset($_REQUEST['sort'])) $_REQUEST['sort']=array( array('direction' => 'A', 'id' => 'nbVisits') ); if(!isset($_REQUEST['additionalFilter'])) $_REQUEST['additionalFilter']=array(); if(!isset($_REQUEST['filter'])) $_REQUEST['filter']=array(); if(!is_numeric($_REQUEST['currentPage']) or $_REQUEST['currentPage']<0) $_REQUEST['currentPage']=0; if(!is_numeric($_REQUEST['nbItemsPage']) or $_REQUEST['nbItemsPage']<0) $_REQUEST['nbItemsPage']=50; if($_REQUEST['period']=='') $_REQUEST['ajaxfct']=''; $_REQUEST['filter']['additionalFilter']=$_REQUEST['additionalFilter']; break; case 'admin.fct.validValues': if(!isset($_REQUEST['domain'])) $_REQUEST['domain']=''; if(!isset($_REQUEST['filter'])) $_REQUEST['filter']=''; if(!($_REQUEST['domain']=='uaBrowser' or $_REQUEST['domain']=='uaEngine' or $_REQUEST['domain']=='uaOS' or $_REQUEST['domain']=='uaType' or $_REQUEST['domain']=='country')) $_REQUEST[GPC_AJAX]=''; break; default: $_REQUEST[GPC_AJAX]=''; break; } } } //checkRequest /** * return ajax content */ protected function returnAjaxContent() { $result="

An error has occured [".$_REQUEST[GPC_AJAX]."]

"; switch($_REQUEST[GPC_AJAX]) { case 'admin.install.buildIpList': $result=$this->ajax_estat_admin_installBuildIpList($_REQUEST['start']); break; case 'admin.migrate.loadLogs': $result=$this->ajax_estat_admin_migrateLoadLogs($_REQUEST['period'], $_REQUEST['lastId']); break; case 'admin.migrate.consolidate': $result=$this->ajax_estat_admin_migrateConsolidate($_REQUEST['period'], $_REQUEST['process']); break; case 'admin.migrate.finalize': $result=$this->ajax_estat_admin_migrateFinalize(); break; case 'admin.stat.history': $result=$this->ajax_estat_admin_statHistory($_REQUEST['period'], $_REQUEST['filter'], $_REQUEST['sort'], $_REQUEST['currentPage'], $_REQUEST['nbItemsPage']); break; case 'admin.stat.history.graphCurrentPeriod': $result=$this->ajax_estat_admin_statHistoryGraphCurrentPeriod($_REQUEST['period']); break; case 'admin.stat.history.graphAllPeriods': $result=$this->ajax_estat_admin_statHistoryVisitsAllPeriods($_REQUEST['period'], $_REQUEST['nbMonth']); break; case 'admin.stat.period': $result=$this->ajax_estat_admin_statPeriod($_REQUEST['period'], $_REQUEST['filter'], $_REQUEST['sort'], $_REQUEST['currentPage'], $_REQUEST['nbItemsPage']); break; case 'admin.stat.period.graph': $result=$this->ajax_estat_admin_statPeriodGraph($_REQUEST['period'], $_REQUEST['filter']); break; case 'admin.stat.ip': $result=$this->ajax_estat_admin_statIP($_REQUEST['period'], $_REQUEST['filter'], $_REQUEST['sort'], $_REQUEST['group'], $_REQUEST['currentPage'], $_REQUEST['nbItemsPage']); break; case 'admin.stat.ip.graphType': $result=$this->ajax_estat_admin_statIPGraphType($_REQUEST['period'], $_REQUEST['filter']); break; case 'admin.stat.ip.graphCountry': $result=$this->ajax_estat_admin_statIPGraphCountry($_REQUEST['period'], $_REQUEST['filter']); break; case 'admin.stat.category': $result=$this->ajax_estat_admin_statCategory($_REQUEST['period'], $_REQUEST['filter'], $_REQUEST['sort'], $_REQUEST['group'], $_REQUEST['currentPage'], $_REQUEST['nbItemsPage']); break; case 'admin.stat.image': $result=$this->ajax_estat_admin_statImage($_REQUEST['period'], $_REQUEST['filter'], $_REQUEST['sort'], $_REQUEST['group'], $_REQUEST['currentPage'], $_REQUEST['nbItemsPage']); break; //revoir les id des fonctions case 'admin.stat.history.periodList': $result=$this->ajax_estat_admin_statHistoryPeriodList(); break; case 'admin.stat.periodTreeList': $result=$this->ajax_estat_admin_statPeriodTreeList(); break; case 'admin.fct.validValues': $result=$this->ajax_estat_admin_fctValidValues($_REQUEST['domain'], $_REQUEST['filter']); break; } GPCAjax::returnResult($result); } /*------------------------------------------------------------------------* * * ADMIN FUNCTIONS * *----------------------------------------------------------------------- */ /** * import IP country file process * * return an array of informations * 'nb' => nb IP range processed for this call * 'nbTotal' => nb total of IP range processed from the begining * * @param Integer start: IP range start load * @return Array */ protected function ajax_estat_admin_installBuildIpList($start) { $returned=array( 'nb' => 0, 'nbTotal' => 0 ); $dbCountry=new StatDBCountry($this->fileStatDir, self::FILE_COUNTRY); if($dbCountry->open(ASDF_OPEN_WRITE)) { $returned['nb']=$dbCountry->loadIpFile(ESTAT_PATH.'data/ipCountry.bin', $start, 5000); $returned['nbTotal']=$dbCountry->getInfoProperty('nfo', 'numberOfIP'); $dbCountry->close(); } return(json_encode($returned)); } /** * Migration process: import data from the history table * * @param String $period : period to be imported 'YYYY-MM' * @param Longint $lastId : id to start * @return String : an array in json format * 'lastId' => (Integer) the last Id processed in the history table * 'nbLogs' => (Integer) number of logs processed */ protected function ajax_estat_admin_migrateLoadLogs($period, $lastId) { if(substr($period,5)>'12' or substr($period,5)<'01') return(-1); $year=substr($period, 0,4); $month=substr($period, 5); $sql="SELECT id, date, time, user_id, IP, section, category_id, tag_ids, image_id, image_type FROM ".HISTORY_TABLE." WHERE id > $lastId AND YEAR(`date`) = $year AND MONTH(`date`) = $month ORDER BY id LIMIT 0,".self::MAX_LOAD_LOG; $nbLogs=0; $lastId=0; $result=pwg_query($sql); if($result) { // define file stat for the year/month period, open it ans start transaction $monthStatFile=new StatDBMonth($this->fileStatDir, self::FILE_MONTH); $monthStatFile->setPeriod($year, $month); $monthStatFile->open(ASDF_OPEN_WRITE); $monthStatFile->startTransac(); if($monthStatFile->getInfoProperty('log', 'startImport')==null) $monthStatFile->setInfoProperty('log', 'startImport', date('Y-m-d H:i:s')); // try to define from reverse DNS if IP is a crawler or not $ipList=new ReverseIP(); //$ipList->loadIPFile($this->fileStatDir.'tmpIpFile.dat'); // build list of data to import $list=array(); // items from history $break=false; while(!$break and $tmp=pwg_db_fetch_assoc($result)) { if(substr($tmp['date'],0,7)==$period) { $list[]=$tmp; $nbLogs++; $lastId=$tmp['id']; if($ipList->getIP($tmp['IP'])==-1) $ipList->addIP($tmp['IP']); } else $break=true; } //$ipList->saveIPFile(); foreach($list as $val) { $uaType=$ipList->getIP($val['IP']); $log=array( 'date' => strtotime($val['date']." ".$val['time']), 'IPadress' => $val['IP'], 'userId' => $val['user_id'], 'catId' => $val['category_id'], 'imageId' => ($val['image_id']!=null)?$val['image_id']:0, 'tagsId' => $val['tag_ids'], 'section' => $val['section'], 'userAgent' => '', 'uaBrowser' => ($uaType==UA_BROWSER_TYPE_CRAWLER)?UA_BOT_UNKNOWN:UA_BROWSER_UNKNOWN, 'uaBrowserVersion' => '', 'uaEngine' => UA_ENGINE_UNKNOWN, 'uaEngineVersion' => '', 'uaOS' => UA_OS_UNKNOWN, 'uaOSVersion' => '', 'uaType' => $uaType ); $monthStatFile->addLog($log); } $monthStatFile->setInfoProperty('log', 'stopImport', date('Y-m-d H:i:s')); // commit changes and close the file $monthStatFile->stopTransac(); $monthStatFile->close(); // if imported data are from a previous period, pack it if($nbLogspack()===true) $monthStatFile->delete(ASDF_DELETE_UNPACKED);*/ } } return( json_encode( Array( 'lastId' => $lastId, 'nbLogs' => $nbLogs ) ) ); } /** * Migration process: consolidate logs on Global file * * @param String $period : period to consolidate (YYYY-MM) * @param Integer $process : step */ protected function ajax_estat_admin_migrateConsolidate($period, $process) { if(substr($period,5)>'12' or substr($period,5)<'01') return(-1); $year=substr($period, 0,4); $month=substr($period, 5); $gStatFile=new StatDBGlobal($this->fileStatDir, self::FILE_GLOBAL); $gStatFile->setIpCountryFile(ESTAT_PATH.'data/ipCountry.db'); $gStatFile->open(ASDF_OPEN_WRITE); if($gStatFile->getInfoProperty('log', 'startImport')==null) $gStatFile->setInfoProperty('log', 'startImport', date('Y-m-d H:i:s')); $gStatFile->buildStatPeriod($this->fileStatDir, self::FILE_MONTH, $year, $month); $gStatFile->setInfoProperty('log', 'stopImport', date('Y-m-d H:i:s')); $gStatFile->close(); return('Y'); } /** * Migration process: finalize the migration */ protected function ajax_estat_admin_migrateFinalize() { $this->config['plugin.newInstall']='n'; $this->saveConfig(); return('Y'); } /** * Return a list of available history periods * @return String : an array in json format * 'value' => (String) the period in 'YYYY-MM' format * 'cols' => (Array) * (String) FileName prefix * (String) Number of events in log file */ protected function ajax_estat_admin_statHistoryPeriodList() { global $lang; $returned=array(); $gStatFile=new StatDBGlobal($this->fileStatDir, self::FILE_GLOBAL); $gStatFile->open(ASDF_OPEN_READ); $files=$gStatFile->getFilesList(); $gStatFile->close(); foreach($files as $file) { $returned[]=array( 'value' => sprintf('%04d%02d', $file['year'], $file['month']), 'cols' => array( $lang['month'][$file['month']].' '.$file['year'], $file['logs'].' '.l10n('estat_events') ) ); } return(json_encode($returned)); } /** * return the logs events from a period * * @param String $period : the period to be processed 'YYYYMM' * @param Array $filter : filter options * @param Integer $pageNumber : page number to return * @return String : an array in json format * (Array) events properties * 'date' => (String) date time in 'YYYY-MM-DD HH:II:SS' format * 'IpUserId' => (String) IP or user login * 'country' => (String) country code (ISO 3366-1) * 'catId' => (String) name of the album * 'imageId' => (String) name of image * 'tags' => (String) tags list * 'section' => (String) section name * 'uaEngine' => (String) browser: engine+version * 'uaBrowser' => (String) browser name+version * 'uaOS' => (String) browser os: name+version * 'uaType' => (String) browser type: name * 'userAgent' => (String) user agent string */ protected function ajax_estat_admin_statHistory($period, $filterPost, $sort, $pageNumber, $nbItemsPage) { $year=substr($period,0,4); $month=substr($period,4,2); $filter=array(); foreach($filterPost as $val) { $filter[$val['id']]=$val; } // open db file for the period $monthStatFile=new StatDBMonth($this->fileStatDir, self::FILE_MONTH); $monthStatFile->setPeriod($year, $month); $monthStatFile->open(ASDF_OPEN_READ); // get history & total number of items $returned=array( 'rows' => $monthStatFile->getLogs(ASDF_GET_ROWS, $pageNumber, $nbItemsPage, $filter, $sort), 'nbItems' => $monthStatFile->getLogs(ASDF_GET_COUNT, 0, 0, $filter, $sort) ); $monthStatFile->close(); // prepare an EStat_IdList to buid a list of unique Id $idList=new EStat_IdList(array('catId', 'imageId', 'tagsId', 'userId', 'IPadress')); // $idAssoc will get the label associated to the id $idAssoc=array( 'catId'=>array(), 'imageId'=>array(), 'tagsId'=>array(), 'userId'=>array(), 'IPadress'=>array() ); // build list of unique Id foreach($returned['rows'] as $row) { $idList->addItems( array( 'catId' => $row['catId'], 'imageId' => $row['imageId'], 'tagsId' => $row['tagsId'], 'userId' => $row['userId'], 'IPadress' => $row['IPadress'] ) ); } if(count($idList->getItems('catId')) > 0) $this->prepareIdList($idAssoc, 'catId', "SELECT id, name FROM ".CATEGORIES_TABLE." WHERE id IN (".implode(',', $idList->getItems('catId')).") ORDER BY id;"); if(count($idList->getItems('imageId')) > 0) $this->prepareIdList($idAssoc, 'imageId', "SELECT id, file AS name, path FROM ".IMAGES_TABLE." WHERE id IN (".implode(',', $idList->getItems('imageId')).") ORDER BY id;"); if(count($idList->getItems('tagsId')) > 0) $this->prepareIdList($idAssoc, 'tagsId', "SELECT id, name FROM ".TAGS_TABLE." WHERE id IN (".implode(',', $idList->getItems('tagsId')).") ORDER BY id;"); if(count($idList->getItems('userId')) > 0) $this->prepareIdList($idAssoc, 'userId', "SELECT id, username AS name FROM ".USERS_TABLE." WHERE id IN (".implode(',', $idList->getItems('userId')).") ORDER BY id;"); // for reverse DNS if($this->config['logs.ipCountry']=='y') $this->getIpInfos($idAssoc, $idList->getItems('IPadress')); // complete the data foreach($returned['rows'] as $key=>$row) { $userAgentNfo=GPCUserAgent::getProperties( array( UA_DATA_BROWSER => $row['uaBrowser'], UA_DATA_BROWSER_TYPE => $row['uaType'], UA_DATA_OS => $row['uaOS'], UA_DATA_ENGINE => $row['uaEngine'] ) ); if($userAgentNfo[UA_DATA_ENGINE_NAME]=='Unknown') $userAgentNfo[UA_DATA_ENGINE_NAME]='ua_Unknown'; if($userAgentNfo[UA_DATA_BROWSER_NAME]=='Unknown') $userAgentNfo[UA_DATA_BROWSER_NAME]='ua_Unknown'; if($userAgentNfo[UA_DATA_OS_NAME]=='Unknown') $userAgentNfo[UA_DATA_OS_NAME]='ua_Unknown'; //$userAgent = UserAgent::parse($row['userAgent']); $tmp=array( 'date' => date('Y-m-d H:i:s', $row['date']), 'IpUserId' => ($row['userId']==2)?$row['IPadress']:$this->getId($idAssoc, 'userId', $row['userId'], $row['IPadress'], 'name'), //'country' => $row['country'], 'catId' => GPCCore::getUserLanguageDesc($this->getId($idAssoc, 'catId', $row['catId'], ($row['catId']==0)?'-':'?')), 'imageId' => $this->getId($idAssoc, 'imageId', $row['imageId'], ($row['imageId']==0)?'-':'?', 'name'), 'tags' => $this->getId($idAssoc, 'tagsId', $row['tagsId'], '', 'name'), 'section' => l10n($row['section']), 'uaEngine' => l10n($userAgentNfo[UA_DATA_ENGINE_NAME]), 'uaBrowser' => l10n($userAgentNfo[UA_DATA_BROWSER_NAME]), 'uaOS' => l10n($userAgentNfo[UA_DATA_OS_NAME]), 'uaType' => l10n('ua_'.$userAgentNfo[UA_DATA_BROWSER_TYPE_NAME]), 'userAgent' => $row['userAgent'] ); if($userAgentNfo[UA_DATA_BROWSER_URL]!='') $tmp['uaBrowser']=''.$tmp['uaBrowser'].''; if($row['uaBrowserVersion']!='') $tmp['uaBrowser'].=' '.$row['uaBrowserVersion'].''; if($userAgentNfo[UA_DATA_OS_URL]!='') $tmp['uaOS']=''.$tmp['uaOS'].''; if($row['uaOSVersion']!='') $tmp['uaOS'].=' '.$row['uaOSVersion'].''; if($userAgentNfo[UA_DATA_ENGINE_URL]!='') $tmp['uaEngine']=''.$tmp['uaEngine'].''; if($row['uaEngineVersion']!='') $tmp['uaEngine'].=' '.$row['uaEngineVersion'].''; switch($tmp['catId']) { case '-': break; case '?': $tmp['catId']=l10n('estat_unknown').'['.$row['catId'].']'; break; default: $tmp['catId']=''.$tmp['catId'].''; break; } $tmp['thumb']=''; switch($tmp['imageId']) { case '-': break; case '?': $tmp['imageId']=l10n('estat_unknown').'['.$row['imageId'].']'; break; default: $tmp['imageId']=''.$tmp['imageId'].''; $imageNfo = new SrcImage( array( 'id'=>$row['imageId'], 'path'=>$this->getId($idAssoc, 'imageId', $row['imageId'], '', 'path') ) ); $tmp['thumb']='./'.substr(DerivativeImage::url(IMG_THUMB, $imageNfo),strlen(PHPWG_ROOT_PATH)); break; } if($row['userId']==2) { $ipInfo=$this->getId($idAssoc, 'IPadress', $tmp['IpUserId'], '?'); if($this->config['logs.reverseDNS']=='y') {/* $reverseIp=$this->getId($idAssoc, 'IPadress', $tmp['IpUserId'], '?'); if($reverseIp!='?' and $reverseIp!='' and $reverseIp!=$tmp['IpUserId']) { $reverseIp=' ('.$reverseIp.')'; } else { $reverseIp=''; }*/ } if($ipInfo['country']!='--' and $ipInfo['country']!='XA') { $tmp['IpUserId'].='['.$ipInfo['country'].']'; } else { //$tmp['IpUserId'].='[--]'; } } $returned['rows'][$key]=array( $tmp['date'], $tmp['IpUserId'], $tmp['catId'], $tmp['imageId'], $tmp['tags'], $tmp['section'], $tmp['uaBrowser'], $tmp['uaEngine'], $tmp['uaOS'], $tmp['uaType'], $tmp['userAgent'], $tmp['thumb'] ); } return(json_encode($returned)); } /** * return the number of logs events per day for the period * * @param String $period: period in 'YYYYMM' format * @return String : an array in json format * (Array) * 'axis' => (Array) period as a formatted string * 'series' => (Array) serie values * 'maxValue' => (Integer) maximum value found on all series */ protected function ajax_estat_admin_statHistoryGraphCurrentPeriod($period) { global $lang; $returned=array( 'maxValue' => 0, 'axis' => array(), 'series' => array( 'total' => array( 'name' => l10n('estat_total'), 'nbVisits' => array() ), 'computer' => array( 'name' => l10n('ua_Computer'), 'nbVisits' => array() ), 'mobile' => array( 'name' => l10n('ua_Mobile'), 'nbVisits' => array() ), 'crawler' => array( 'name' => l10n('ua_Crawler'), 'nbVisits' => array() ), 'other' => array( 'name' => l10n('ua_Other'), 'nbVisits' => array() ) ) ); $year=substr($period, 0 ,4); $month=substr($period, 4, 2); // open db file for the period $mStatFile=new StatDBMonth($this->fileStatDir, self::FILE_MONTH); $mStatFile->setPeriod($year, $month); $mStatFile->open(ASDF_OPEN_READ); // get number of visits per type of user agent (mobile, computer, crawler, ...) $data=$mStatFile->getStatUserAgent(ASDF_GET_ROWS, 0, 0, array('day', 'uaValue'), array('uaData' => array('operator'=>'=', 'value'=>UA_DATA_BROWSER_TYPE)), array( array('id'=>'day', 'direction'=>'A') ) ); $mStatFile->close(); // nb of days for the period $nbDay=$mStatFile->getNbDays(); // prepare data $axis=array(); $series=array( 'total' => array(), 'computer' => array(), 'mobile' => array(), 'crawler' => array(), 'other' => array() ); for($i=1;$i<=$nbDay;$i++) { $returned['axis'][]=$i; $series['total'][$i]=0; $series['computer'][$i]=0; $series['mobile'][$i]=0; $series['crawler'][$i]=0; $series['other'][$i]=0; } foreach($data as $value) { $series['total'][$value['day']]+=$value['nbVisits']; if($returned['maxValue']<$series['total'][$value['day']]) $returned['maxValue']=$series['total'][$value['day']]; switch($value['uaValue']) { case UA_BROWSER_TYPE_COMPUTER: $series['computer'][$value['day']]+=$value['nbVisits']; break; case UA_BROWSER_TYPE_MOBILE: $series['mobile'][$value['day']]+=$value['nbVisits']; break; case UA_BROWSER_TYPE_CRAWLER: $series['crawler'][$value['day']]+=$value['nbVisits']; break; default: $series['other'][$value['day']]+=$value['nbVisits']; break; } } // format axis (add day letter for sunday) foreach($returned['axis'] as $key=>$day) { $period=$year.'-'.$month.'-'.$day; if(date('w', strtotime($period))==0) $returned['axis'][$key]=l10n('letter_day_sunday').' '.$day; } // format series values foreach($series as $serie=>$period) { foreach($period as $nbVisits) { $returned['series'][$serie]['nbVisits'][]=$nbVisits; } } return(json_encode($returned)); } /** * return the number of logs events per period (for all periods) * * data can be filtered to retrieve informations from a limited range of $nbMonth around * the $currentPeriod * * @param String $currentPeriod: current period (YYYYMM); no period=all periods * @param Integer $nbMonth: number of month to retrieve; 0=all periods * @return String : an array in json format * (Array) * 'axis' => (Array) period as a formatted string * 'series' => (Array) serie values * 'maxValue' => (Integer) maximum value found on all series */ protected function ajax_estat_admin_statHistoryVisitsAllPeriods($currentPeriod='', $nbMonth=0) { if($currentPeriod!='' and $nbMonth>0) { $managePeriod=true; $year=substr($currentPeriod,0,4); $month=substr($currentPeriod,4,2); $before=round($nbMonth/2,0); $after=$nbMonth-$before-1; $firstPeriod=date('Y-m', strtotime('-'.$before.' month', strtotime($year.'-'.$month.'-01'))); $lastPeriod=date('Y-m', strtotime('+'.$after.' month', strtotime($year.'-'.$month.'-01'))); $periodList=array(); for($i=0;$i<$nbMonth;$i++) { $period=date('Y-m', strtotime("+$i month", strtotime($firstPeriod.'-01'))); $periodList[$period]=array('y' => substr($period, 0, 4), 'm' => substr($period, 5, 2)); } } else { $managePeriod=false; $before=0; $after=0; $year=''; $month=''; $firstPeriod=''; $lastPeriod=''; $periodList=array(); } $returned=array( 'maxValue' => 0, 'axis' => array(), 'series' => array( 'total' => array( 'name' => l10n('estat_total'), 'nbVisits' => array() ), 'computer' => array( 'name' => l10n('ua_Computer'), 'nbVisits' => array() ), 'mobile' => array( 'name' => l10n('ua_Mobile'), 'nbVisits' => array() ), 'crawler' => array( 'name' => l10n('ua_Crawler'), 'nbVisits' => array() ), 'other' => array( 'name' => l10n('ua_Other'), 'nbVisits' => array() ) ) ); // open db file $gStatFile=new StatDBGlobal($this->fileStatDir, self::FILE_GLOBAL); $gStatFile->open(ASDF_OPEN_READ); // get number of visits per type of user agent (mobile, computer, crawler, ...) $data=$gStatFile->getStatUserAgent(ASDF_GET_ROWS, 0, 0, array('year', 'month', 'uaValue'), array('uaData' => array('operator'=>'=', 'value'=>UA_DATA_BROWSER_TYPE)), array( array('id'=>'year', 'direction'=>'A'), array('id'=>'month', 'direction'=>'A') ) ); $gStatFile->close(); // prepare data $axis=array(); $series=array( 'total' => array(), 'computer' => array(), 'mobile' => array(), 'crawler' => array(), 'other' => array() ); if($managePeriod) { foreach($periodList as $period=>$value) { $axis[$period]=$value; $series['total'][$period]=0; $series['computer'][$period]=0; $series['mobile'][$period]=0; $series['crawler'][$period]=0; $series['other'][$period]=0; } } foreach($data as $value) { $value['month']=sprintf('%02d', 1*$value['month']); $period=$value['year'].'-'.$value['month']; if($managePeriod and isset($periodList[$period]) or !$managePeriod) { if($managePeriod) $periodList[$period]=true; if(!isset($axis[$period])) { $axis[$period]=array('y'=>$value['year'], 'm'=>$value['month']); $series['total'][$period]=0; $series['computer'][$period]=0; $series['mobile'][$period]=0; $series['crawler'][$period]=0; $series['other'][$period]=0; } $series['total'][$period]+=$value['nbVisits']; if($returned['maxValue']<$series['total'][$period]) $returned['maxValue']=$series['total'][$period]; switch($value['uaValue']) { case UA_BROWSER_TYPE_COMPUTER: $series['computer'][$period]+=$value['nbVisits']; break; case UA_BROWSER_TYPE_MOBILE: $series['mobile'][$period]+=$value['nbVisits']; break; case UA_BROWSER_TYPE_CRAWLER: $series['crawler'][$period]+=$value['nbVisits']; break; default: $series['other'][$period]+=$value['nbVisits']; break; } } } // format axis values "month [year]" $i=0; foreach($axis as $period) { if($period['m']==1 or $i==0) { $returned['axis'][]='('.$period['y'].') '.l10n('month_'.$period['m']); } else { $returned['axis'][]=l10n('month_'.$period['m']); } $i++; } // format series values foreach($series as $serie=>$period) { foreach($period as $nbVisits) { $returned['series'][$serie]['nbVisits'][]=$nbVisits; } } return(json_encode($returned)); } /** * return the number of visits from a period * * @param String $period : the period to be processed * 'allYear' => process all year * 'y-YYYY' => process year YYYY * 'ym-YYYYMM' => process year/month YYYY/MM * @param Array $filterPost : filter options * @param Integer $pageNumber : page number to return * @return String : an array in json format * (Array) visits properties * 'period' => (String) * 'nbPages' => (String) IP or user login * 'nbCat' => (String) country code (ISO 3366-1) * 'nbPhotos' => (String) name of the album * 'nbIP' => (String) name of image */ protected function ajax_estat_admin_statPeriod($period, $filterPost, $sort, $pageNumber, $nbItemsPage) { global $lang; $periodType=''; $year=null; $month=null; $result=array( 'rows' => null, 'total' => null ); $returned=array( 'rows' => array(), 'total' => array() ); $fields=array( 'rows' => array(), 'total' => array() ); $filter=array('uaData' => UA_DATA_BROWSER, 'catId' => array(), 'year'=>array(), 'month'=>array()); if(substr($period,0,3)=='ym-') { $periodType='ym'; $year=substr($period,3,4)*1; $month=substr($period,7,2)*1; $fields['rows'][]='day'; } elseif(substr($period,0,2)=='y-') { $periodType='y'; $fields['rows'][]='month'; $year=substr($period,2,4)*1; } else // all other values assume $period=='allYear' { $fields['rows'][]='year'; $fields['total']=array(); } // filter on catId? if(isset($filterPost['catId'])) { $filter['catId']=$this->buildCatIdFilter($filterPost['catId']); } if($year!=null) $filter['year']=array('operator' => '=', 'value' => $year); if($month!=null) $filter['month']=array('operator' => '=', 'value' => $month); // open db file for the period if($periodType=='ym') { $statFile=new StatDBMonth($this->fileStatDir, self::FILE_MONTH); $statFile->setPeriod($year, sprintf('%02d', $month)); } else { $statFile=new StatDBGlobal($this->fileStatDir, self::FILE_GLOBAL); } $statFile->open(ASDF_OPEN_READ); // get stats $result['rows']=$statFile->getStatPeriod($pageNumber, $nbItemsPage, $fields['rows'], $filter, $sort); // reformat stats for table switch($periodType) { case 'ym': foreach($result['rows'] as $day => $values) { $returned['rows'][]=array( $day, $values['T'], $values['C'], $values['I'], $values['A'], $day ); } $tmp=$statFile->getStatPeriod(0, 0, $fields['total'], $filter, $sort); $returned['total']=array('', $tmp[0]['T'], $tmp[0]['C'], $tmp[0]['I'], $tmp[0]['A']); break; case 'y': foreach($result['rows'] as $year => $months) { foreach($months as $month => $values) { $returned['rows'][]=array( $lang['month'][$month], $values['T'], $values['C'], $values['I'], $values['A'], sprintf('%02d', $month) ); } } $tmp=$statFile->getStatPeriod(0, 0, $fields['total'], $filter, $sort); $returned['total']=array('', $tmp[0][0]['T'], $tmp[0][0]['C'], $tmp[0][0]['I'], $tmp[0][0]['A']); break; default: // all years... foreach($result['rows'] as $year => $values) { if($year>0) { $returned['rows'][]=array( $year, $values[0]['T'], $values[0]['C'], $values[0]['I'], $values[0]['A'], sprintf('%02d', $year) ); } } $tmp=$statFile->getStatPeriod(0, 0, $fields['total'], $filter, $sort); $returned['total']=array('', $tmp[0][0]['T'], $tmp[0][0]['C'], $tmp[0][0]['I'], $tmp[0][0]['A']); break; } $statFile->close(); return(json_encode($returned)); } /** * return the number of visits from a period, formatted for a graph * * @param String $period : the period to be processed * 'allYear' => process all year * 'y-YYYY' => process year YYYY * 'ym-YYYYMM' => process year/month YYYY/MM * @param Array $filterPost : filter options * @param Integer $pageNumber : page number to return * @return String : an array in json format * (Array) * 'axis' => (Array) period as a formatted string * 'series' => (Array) serie values * 'maxValue' => (Integer) maximum value found on all series */ protected function ajax_estat_admin_statPeriodGraph($period, $filterPost) { global $lang; $periodType=''; $year=null; $month=null; $returned=array( 'maxValue' => 0, 'axis' => array(), 'series' => array( 'viewedPages' => array( 'name' => l10n('estat_viewedPages'), 'nbVisits' => array() ), 'viewedAlbums' => array( 'name' => l10n('estat_viewedAlbums'), 'nbVisits' => array() ), 'viewedImages' => array( 'name' => l10n('estat_viewedImages'), 'nbVisits' => array() ), 'uniqueIP' => array( 'name' => l10n('estat_uniqueIP'), 'nbVisits' => array() ) ) ); $filter=array('uaData' => UA_DATA_BROWSER, 'catId' => array(), 'year'=>array(), 'month'=>array() ); $fields=array(); if(substr($period,0,3)=='ym-') { $periodType='ym'; $year=substr($period,3,4)*1; $month=substr($period,7,2)*1; $fields[]='day'; } elseif(substr($period,0,2)=='y-') { $periodType='y'; $fields[]='month'; $year=substr($period,2,4)*1; } else // all other values assume $period=='allYear' { $fields[]='year'; } // filter on catId? if(isset($filterPost['catId'])) { $filter['catId']=$this->buildCatIdFilter($filterPost['catId']); } if($year) $filter['year']=array('operator' => '=', 'value' => $year); if($month) $filter['month']=array('operator' => '=', 'value' => $month); // open db file for the period if($periodType=='ym') { $statFile=new StatDBMonth($this->fileStatDir, self::FILE_MONTH); $statFile->setPeriod($year, sprintf('%02d', $month)); } else { $statFile=new StatDBGlobal($this->fileStatDir, self::FILE_GLOBAL); } $statFile->open(ASDF_OPEN_READ); // get stats $result['rows']=$statFile->getStatPeriod(0, 0, $fields, $filter, null); // prepare data $axis=array(); $series=array( 'viewedPages' => array(), 'viewedAlbums' => array(), 'viewedImages' => array(), 'uniqueIP' => array() ); switch($periodType) { case 'ym': foreach($result['rows'] as $day => $values) { if($returned['maxValue']<$values['T']) $returned['maxValue']=$values['T']; $returned['axis'][]=$day; $series['viewedPages'][$day]=$values['T']; $series['viewedAlbums'][$day]=$values['C']; $series['viewedImages'][$day]=$values['I']; $series['uniqueIP'][$day]=$values['A']; } // format axis (add day letter for sunday) foreach($returned['axis'] as $key=>$day) { $period=$year.'-'.$month.'-'.$day; if(date('w', strtotime($period))==0) $returned['axis'][$key]=l10n('letter_day_sunday').' '.$day; } break; case 'y': foreach($result['rows'] as $year => $months) { foreach($months as $month => $values) { if($returned['maxValue']<$values['T']) $returned['maxValue']=$values['T']; $returned['axis'][]=$lang['month'][$month]; $series['viewedPages'][$month]=$values['T']; $series['viewedAlbums'][$month]=$values['C']; $series['viewedImages'][$month]=$values['I']; $series['uniqueIP'][$month]=$values['A']; } } break; default: foreach($result['rows'] as $year => $values) { if($year>0) { if($returned['maxValue']<$values[0]['T']) $returned['maxValue']=$values[0]['T']; $returned['axis'][]=$year; $series['viewedPages'][$year]=$values[0]['T']; $series['viewedAlbums'][$year]=$values[0]['C']; $series['viewedImages'][$year]=$values[0]['I']; $series['uniqueIP'][$year]=$values[0]['A']; } } break; } // format series values foreach($series as $serie=>$period) { foreach($period as $nbVisits) { $returned['series'][$serie]['nbVisits'][]=$nbVisits; } } return(json_encode($returned)); } /** * return the detail for IP from a period * * @param String $period : the period to be processed * 'allYear' => process all year * 'y-YYYY' => process year YYYY * 'ym-YYYYMM' => process year/month YYYY/MM * @param Array $filterPost : filter options * @param Array $sort: sort options * @param Array $group: grouped items; could be: * 'IPadress' * 'country' * 'uaType' * @param Integer $pageNumber : page number to return * @return String : an array in json format * (Array) * 'rows' => (Array) * 'IPadress' => (String) * 'country' => (String) * 'uaType' => (String) * 'nbVisits' => (Integer) * 'total' => (Array) * 'IPadress' => (String) [empty] * 'country' => (String) [empty] * 'uaType' => (String) [empty] * 'nbVisits' => (Integer) * 'nbItems' => (Integer) */ protected function ajax_estat_admin_statIP($period, $filterPost, $sort, $group, $pageNumber, $nbItemsPage) { global $lang; $year=null; $month=null; $result=array( 'rows' => array(), 'total' => array() ); $returned=array( 'rows' => array(), 'total' => array(), 'nbItems' => 0 ); $fields=array( 'rows' => array_diff(array('uaType', 'IPadress', 'country'), $group), 'total' => array() ); $filter=array(); if(substr($period,0,3)=='ym-') { $year=substr($period,3,4)*1; $month=substr($period,7,2)*1; } elseif(substr($period,0,2)=='y-') { $year=substr($period,2,4)*1; } foreach($filterPost as $key => $val) { if($key==='additionalFilter') { if(isset($val['catId'])) $filter['catId']=$this->buildCatIdFilter($val['catId']); } else { $filter[$val['id']]=$val; } } if($year!=null) $filter['year']=array('operator' => '=', 'value' => $year); if($month!=null) $filter['month']=array('operator' => '=', 'value' => $month); // open db file for the period $statFile=new StatDBGlobal($this->fileStatDir, self::FILE_GLOBAL); $statFile->open(ASDF_OPEN_READ); // get stats $result['rows']=$statFile->getStatIP(ASDF_GET_ROWS, $pageNumber, $nbItemsPage, $fields['rows'], $filter, $sort); $result['total']=$statFile->getStatIP(ASDF_GET_ROWS, 0, 0, array(), $filter, array()); foreach($result['rows'] as $row) { if(isset($row['uaType'])) { $userAgentNfo=GPCUserAgent::getProperties( array( UA_DATA_BROWSER_TYPE => $row['uaType'] ) ); $row['uaType']=l10n('ua_'.$userAgentNfo[UA_DATA_BROWSER_TYPE_NAME]); } else { $row['uaType']=''; } if(isset($row['country'])) { $country='estat_country_'.strtoupper($row['country']); if(isset($lang[$country])) { $country="".$row['country'].''.$lang[$country]; } else { $country="".$row['country'].''; } } else { $country=''; $row['country']=''; } if(isset($row['IPadress'])) { if($row['country']!='--' and $row['country']!='XA') { $row['IPadress']=''.$row['IPadress'].''; } } else { $row['IPadress']=''; } $returned['rows'][]=array( $row['IPadress'], $country, $row['uaType'], sprintf('%d%0.2f%%', $row['nbVisits'], 100*$row['nbVisits']/$result['total'][0]['nbVisits']) ); } $returned['total']=array( '', '', '', isset($result['total'][0]['nbVisits'])?$result['total'][0]['nbVisits']:'0' ); $returned['nbItems']=$statFile->getStatIP(ASDF_GET_COUNT, 0, 0, $fields['rows'], $filter, array()); $statFile->close(); return(json_encode($returned)); } /** * return the detail needed to draw a GraphType for IP from a period * * @param String $period : the period to be processed * 'allYear' => process all year * 'y-YYYY' => process year YYYY * 'ym-YYYYMM' => process year/month YYYY/MM * @param Array $filterPost : filter options * @return String : an array in json format * (Array) * 'values' => (Array) (Integer) * 'valuesLabels' => (Array) (String) */ protected function ajax_estat_admin_statIPGraphType($period, $filterPost) { global $lang; $year=null; $month=null; $returned=array( 'values' => array(0,0,0,0,0), 'valuesLabels' => array( l10n('ua_'.GPCUserAgentValues::$UA_BrowserInfo[UA_DATA_BROWSER_TYPE][UA_BROWSER_TYPE_CRAWLER]), l10n('ua_'.GPCUserAgentValues::$UA_BrowserInfo[UA_DATA_BROWSER_TYPE][UA_BROWSER_TYPE_COMPUTER]), l10n('ua_'.GPCUserAgentValues::$UA_BrowserInfo[UA_DATA_BROWSER_TYPE][UA_BROWSER_TYPE_MOBILE]), l10n('ua_'.GPCUserAgentValues::$UA_BrowserInfo[UA_DATA_BROWSER_TYPE][UA_BROWSER_TYPE_CONSOLE]), l10n('ua_'.GPCUserAgentValues::$UA_BrowserInfo[UA_DATA_BROWSER_TYPE][UA_BROWSER_TYPE_UNKNOWN]) ) ); $fields=array('uaType'); $filter=array(); if(substr($period,0,3)=='ym-') { $year=substr($period,3,4)*1; $month=substr($period,7,2)*1; } elseif(substr($period,0,2)=='y-') { $year=substr($period,2,4)*1; } if(isset($filterPost['catId'])) $filter['catId']=$this->buildCatIdFilter($filterPost['catId']); if($year!=null) $filter['year']=array('operator' => '=', 'value' => $year); if($month!=null) $filter['month']=array('operator' => '=', 'value' => $month); $sort=array( array('id'=>'uaType', 'direction'=>'A') ); // open db file for the period $statFile=new StatDBGlobal($this->fileStatDir, self::FILE_GLOBAL); $statFile->open(ASDF_OPEN_READ); // get stats $result=$statFile->getStatIP(ASDF_GET_ROWS, 0, 0, $fields, $filter, $sort); $statFile->close(); $sum=0; foreach($result as $row) { switch($row['uaType']) { case UA_BROWSER_TYPE_CRAWLER: $key=0; break; case UA_BROWSER_TYPE_COMPUTER: $key=1; break; case UA_BROWSER_TYPE_MOBILE: $key=2; break; case UA_BROWSER_TYPE_CONSOLE: $key=3; break; case UA_BROWSER_TYPE_UNKNOWN: $key=4; break; } $returned['values'][$key]=$row['nbVisits']; $sum+=$row['nbVisits']; } foreach($returned['values'] as $key => $val) { if($val>0) $returned['valuesLabels'][$key].=sprintf(' (%0.2f%%)', 100*$val/$sum); } return(json_encode($returned)); } /** * return the detail needed to draw a GraphCountry for IP from a period * * @param String $period : the period to be processed * 'allYear' => process all year * 'y-YYYY' => process year YYYY * 'ym-YYYYMM' => process year/month YYYY/MM * @param Array $filterPost : filter options * @return String : an array in json format * (Array) * 'values' => (Array) (Integer) * 'valuesLabels' => (Array) (String) */ protected function ajax_estat_admin_statIPGraphCountry($period, $filterPost) { global $lang; $year=null; $month=null; $returned=array( 'values' => array(), 'valuesLabels' => array() ); $fields=array('country'); $filter=array(); if(substr($period,0,3)=='ym-') { $year=substr($period,3,4)*1; $month=substr($period,7,2)*1; } elseif(substr($period,0,2)=='y-') { $year=substr($period,2,4)*1; } if(isset($filterPost['catId'])) $filter['catId']=$this->buildCatIdFilter($filterPost['catId']); if($year!=null) $filter['year']=array('operator' => '=', 'value' => $year); if($month!=null) $filter['month']=array('operator' => '=', 'value' => $month); // open db file for the period $statFile=new StatDBGlobal($this->fileStatDir, self::FILE_GLOBAL); $statFile->open(ASDF_OPEN_READ); // get stats $result=$statFile->getStatIP(ASDF_GET_ROWS, 0, 0, $fields, $filter); $statFile->close(); $sum=0; foreach($result as $row) { $sum+=$row['nbVisits']; } $i=0; $nb=0; $other=-1; foreach($result as $row) { $pct=100*$row['nbVisits']/$sum; if($i<9 and $pct>=0.5) { $country='estat_country_'.strtoupper($row['country']); if(isset($lang[$country])) { $country=$row['country'].' - '.$lang[$country]; } else { $country=$row['country']; } $country.=sprintf(' (%0.2f%%)', $pct); $returned['values'][$i]=$row['nbVisits']; $returned['valuesLabels'][$i]=$country; $i++; } else { $nb++; $other=$i; if(!isset($returned['values'][$i])) { $returned['values'][$i]=0; $returned['valuesLabels'][$i]=l10n('estat_Other'); } $returned['values'][$i]+=$row['nbVisits']; } } if($other>-1) { $nbCountry=l10n('estat_country_nb'); if($nb>1) $nbCountry=l10n('estat_countries_nb'); $returned['valuesLabels'][$other].=sprintf(' ('.$nbCountry.', %0.2f%%)', $nb, 100*$returned['values'][$other]/$sum ); } return(json_encode($returned)); } /** * return the detail for albums/categories from a period * * @param String $period : the period to be processed * 'allYear' => process all year * 'y-YYYY' => process year YYYY * 'ym-YYYYMM' => process year/month YYYY/MM * @param Array $filterPost : filter options * @param Array $sort: sort options * @param Array $group: grouped items; could be: * 'uaType' * @param Integer $pageNumber : page number to return * @return String : an array in json format * (Array) * 'rows' => (Array) * 'category' => (String) * 'uaType' => (String) * 'nbVisits' => (Integer) * 'total' => (Array) * 'category' => (String) [empty] * 'uaType' => (String) [empty] * 'nbVisits' => (Integer) * 'nbItems' => (Integer) */ protected function ajax_estat_admin_statCategory($period, $filterPost, $sort, $group, $pageNumber, $nbItemsPage) { global $lang; $year=null; $month=null; $result=array( 'rows' => array(), 'total' => array() ); $returned=array( 'rows' => array(), 'total' => array(), 'nbItems' => 0 ); $fields=array( 'rows' => array_diff(array('catId', 'uaType'), $group), 'total' => array() ); $filter=array(); if(substr($period,0,3)=='ym-') { $year=substr($period,3,4)*1; $month=substr($period,7,2)*1; } elseif(substr($period,0,2)=='y-') { $year=substr($period,2,4)*1; } foreach($filterPost as $key => $val) { if($key==='additionalFilter') { if(isset($val['catId'])) $filter['catId']=$this->buildCatIdFilter($val['catId']); } else { $filter[$val['id']]=$val; } } if($year!=null) $filter['year']=array('operator' => '=', 'value' => $year); if($month!=null) $filter['month']=array('operator' => '=', 'value' => $month); // open db file for the period $statFile=new StatDBGlobal($this->fileStatDir, self::FILE_GLOBAL); $statFile->open(ASDF_OPEN_READ); // get stats $result['rows']=$statFile->getStatCat(ASDF_GET_ROWS, $pageNumber, $nbItemsPage, $fields['rows'], $filter, $sort); $result['total']=$statFile->getStatCat(ASDF_GET_ROWS, 0, 0, array(), $filter, array()); // prepare categories&images properties $idList=new EStat_IdList(array('catId', 'imageId')); $idAssoc=array( 'catId'=>array(), 'imageId'=>array() ); // first, build category_id list foreach($result['rows'] as $row) { $idList->addItems(array('catId' => $row['catId'])); } // get upper categories and complete category_id list if(count($idList->getItems('catId')) > 0) { $sql="SELECT DISTINCT uppercats FROM ".CATEGORIES_TABLE." WHERE id IN (".implode(',', $idList->getItems('catId')).");"; $result2=pwg_query($sql); if($result2) { while($row=pwg_db_fetch_row($result2)) { $ids=explode(',', $row[0]); foreach($ids as $id) { $idList->addItems(array('catId' => $id)); } } } } // get all needed properties for each category_id if(count($idList->getItems('catId')) > 0) $this->prepareIdList($idAssoc, 'catId', "SELECT id, name, uppercats, representative_picture_id FROM ".CATEGORIES_TABLE." WHERE id IN (".implode(',', $idList->getItems('catId')).") ORDER BY id;"); // prepare image_id informations (for category representative picture) $categories=$idList->getItems('catId'); foreach($idAssoc['catId'] as $category) { if($category['representative_picture_id']!=null) $idList->addItems(array('imageId' => $category['representative_picture_id'])); } if(count($idList->getItems('imageId')) > 0) $this->prepareIdList($idAssoc, 'imageId', "SELECT id, file AS name, path FROM ".IMAGES_TABLE." WHERE id IN (".implode(',', $idList->getItems('imageId')).") ORDER BY id;"); foreach($result['rows'] as $row) { if(isset($row['uaType'])) { $userAgentNfo=GPCUserAgent::getProperties( array( UA_DATA_BROWSER_TYPE => $row['uaType'] ) ); $row['uaType']=l10n('ua_'.$userAgentNfo[UA_DATA_BROWSER_TYPE_NAME]); } else { $row['uaType']=''; } if($row['catId']==0) { $categoryNfo=l10n('estat_special_category'); } elseif($this->getId($idAssoc, 'catId', $row['catId'], '', 'name')=='') { $categoryNfo=l10n('estat_deleted_category'); } else { $categoryNfo=''.$this->getId($idAssoc, 'catId', $row['catId'], '', 'name').''; } $uppercats=explode(',', $this->getId($idAssoc, 'catId', $row['catId'], null, 'uppercats')); if(count($uppercats)>1) { $categoryNfo=$this->getId($idAssoc, 'catId', $uppercats[count($uppercats)-2], '', 'name')." / ".$categoryNfo; } $representativeUrl=$this->getId($idAssoc, 'catId', $row['catId'], null, 'representative_picture_id'); if($representativeUrl!=null) { $imageNfo = new SrcImage( array( 'id'=>$representativeUrl, 'path'=>$this->getId($idAssoc, 'imageId', $representativeUrl, '', 'path') ) ); $representativeUrl='./'.substr(DerivativeImage::url(IMG_THUMB, $imageNfo),strlen(PHPWG_ROOT_PATH)); } else { $representativeUrl=''; } $returned['rows'][]=array( $categoryNfo, $row['uaType'], sprintf('%d%0.2f%%', $row['nbVisits'], 100*$row['nbVisits']/$result['total'][0]['nbVisits']), $representativeUrl ); } $returned['total']=array( '', '', isset($result['total'][0]['nbVisits'])?$result['total'][0]['nbVisits']:'0' ); $returned['nbItems']=$statFile->getStatCat(ASDF_GET_COUNT, 0, 0, $fields['rows'], $filter, array()); $statFile->close(); return(json_encode($returned)); } /** * return the detail for images from a period * * @param String $period : the period to be processed * 'allYear' => process all year * 'y-YYYY' => process year YYYY * 'ym-YYYYMM' => process year/month YYYY/MM * @param Array $filterPost : filter options * @param Array $sort: sort options * @param Array $group: grouped items; could be: * 'uaType' * @param Integer $pageNumber : page number to return * @return String : an array in json format * (Array) * 'rows' => (Array) * 'category' => (String) * 'uaType' => (String) * 'nbVisits' => (Integer) * 'total' => (Array) * 'category' => (String) [empty] * 'uaType' => (String) [empty] * 'nbVisits' => (Integer) * 'nbItems' => (Integer) */ protected function ajax_estat_admin_statImage($period, $filterPost, $sort, $group, $pageNumber, $nbItemsPage) { global $lang; $year=null; $month=null; $result=array( 'rows' => array(), 'total' => array() ); $returned=array( 'rows' => array(), 'total' => array(), 'nbItems' => 0 ); $fields=array( 'rows' => array_diff(array('imageId', 'catId', 'uaType'), $group), 'total' => array() ); $filter=array(); if(substr($period,0,3)=='ym-') { $year=substr($period,3,4)*1; $month=substr($period,7,2)*1; } elseif(substr($period,0,2)=='y-') { $year=substr($period,2,4)*1; } foreach($filterPost as $key => $val) { if($key==='additionalFilter') { if(isset($val['catId'])) $filter['catId']=$this->buildCatIdFilter($val['catId']); } else { $filter[$val['id']]=$val; } } if($year!=null) $filter['year']=array('operator' => '=', 'value' => $year); if($month!=null) $filter['month']=array('operator' => '=', 'value' => $month); // open db file for the period $statFile=new StatDBGlobal($this->fileStatDir, self::FILE_GLOBAL); $statFile->open(ASDF_OPEN_READ); // get stats $result['rows']=$statFile->getStatImages(ASDF_GET_ROWS, $pageNumber, $nbItemsPage, $fields['rows'], $filter, $sort); $result['total']=$statFile->getStatImages(ASDF_GET_ROWS, 0, 0, array(), $filter, array()); // prepare categories&images properties $idList=new EStat_IdList(array('catId', 'imageId')); $idAssoc=array( 'catId'=>array(), 'imageId'=>array() ); // first, build category_id list foreach($result['rows'] as $row) { $idList->addItems(array('catId' => $row['catId'])); $idList->addItems(array('imageId' => $row['imageId'])); } // get all needed properties for each category_id if(count($idList->getItems('catId')) > 0) $this->prepareIdList($idAssoc, 'catId', "SELECT id, name FROM ".CATEGORIES_TABLE." WHERE id IN (".implode(',', $idList->getItems('catId')).") ORDER BY id;"); if(count($idList->getItems('imageId')) > 0) $this->prepareIdList($idAssoc, 'imageId', "SELECT id, file AS name, path FROM ".IMAGES_TABLE." WHERE id IN (".implode(',', $idList->getItems('imageId')).") ORDER BY id;"); foreach($result['rows'] as $row) { if(isset($row['uaType'])) { $userAgentNfo=GPCUserAgent::getProperties( array( UA_DATA_BROWSER_TYPE => $row['uaType'] ) ); $row['uaType']=l10n('ua_'.$userAgentNfo[UA_DATA_BROWSER_TYPE_NAME]); } else { $row['uaType']=''; } if($row['catId']==0) { $imageName=l10n('estat_special_category'); } elseif($this->getId($idAssoc, 'catId', $row['catId'], '', 'name')=='') { $imageName=l10n('estat_deleted_category').' ['.$row['catId'].']'; } else { $imageName=''.$this->getId($idAssoc, 'catId', $row['catId'], '', 'name').''; } $imageName.=' / '; $imageNfo=$this->getId($idAssoc, 'imageId', $row['imageId'], '', 'name'); if($imageNfo!='') { $imageName.=''.$imageNfo.''; } else { $imageName.=l10n('estat_deleted_image'); } $imagePath=$this->getId($idAssoc, 'imageId', $row['imageId'], '', 'path'); if($imagePath!='') { $imageNfo = new SrcImage( array( 'id'=>$row['imageId'], 'path'=>$imagePath ) ); $imageUrl='./'.substr(DerivativeImage::url(IMG_THUMB, $imageNfo),strlen(PHPWG_ROOT_PATH)); } else { $imageUrl=''; } $returned['rows'][]=array( $imageName, $row['uaType'], sprintf('%d%0.2f%%', $row['nbVisits'], 100*$row['nbVisits']/$result['total'][0]['nbVisits']), $imageUrl ); } $returned['total']=array( '', '', isset($result['total'][0]['nbVisits'])?$result['total'][0]['nbVisits']:'0' ); $returned['nbItems']=$statFile->getStatImages(ASDF_GET_COUNT, 0, 0, $fields['rows'], $filter, array()); $statFile->close(); return(json_encode($returned)); } /** * Return a list of valid values for a given domain * * @param String $domain: domain of values * @return String: an array of values in json string * each values is an array('id'=>id, 'value'=>value) */ protected function ajax_estat_admin_fctValidValues($domain, $filter) { $returned=array(); $id=-1; $idFilter=-1; switch($domain) { case 'uaBrowser': $id=UA_DATA_BROWSER; switch($filter) { case 'unknown': $idFilter=UA_BROWSER_TYPE_UNKNOWN; break; case 'crawler': $idFilter=UA_BROWSER_TYPE_CRAWLER; break; case 'computer': $idFilter=UA_BROWSER_TYPE_COMPUTER; break; case 'mobile': $idFilter=UA_BROWSER_TYPE_MOBILE; break; case 'console': $idFilter=UA_BROWSER_TYPE_CONSOLE; break; } break; case 'uaEngine': $id=UA_DATA_ENGINE; break; case 'uaOS': $id=UA_DATA_OS; switch($filter) { case 'unknown': $idFilter=UA_OS_TYPE_UNKNOWN; break; case 'linux': $idFilter=UA_OS_TYPE_LINUX; break; case 'bsd': $idFilter=UA_OS_TYPE_BSD; break; case 'unix': $idFilter=UA_OS_TYPE_UNIX; break; case 'windows': $idFilter=UA_OS_TYPE_WINDOWS; break; case 'OS2': $idFilter=UA_OS_TYPE_OS2; break; } break; case 'uaType': $id=UA_DATA_BROWSER_TYPE; break; case 'country': foreach($this->countryCodes as $countryCode) { $returned[]=array( 'value' => $countryCode, 'cols' => array(l10n('estat_country_'.$countryCode), "[$countryCode]") ); } return(json_encode($returned)); break; } foreach(GPCUserAgentValues::$UA_BrowserInfo[$id] as $key=>$value) { if($value[UA_PROP_NAME]=='Unknown') $value[UA_PROP_NAME]='ua_Unknown'; switch($domain) { case 'uaBrowser': if($idFilter==-1 or ($idFilter>-1 and $value[UA_PROP_TYPE]!=$idFilter)) $returned[]=array( 'value' => $key, 'cols' => array( l10n($value[UA_PROP_NAME]), l10n('ua_'.GPCUserAgentValues::$UA_BrowserInfo[UA_DATA_BROWSER_TYPE][$value[UA_PROP_TYPE]]) ) ); break; case 'uaOS': if(GPCUserAgentValues::$UA_BrowserInfo[UA_DATA_OS_TYPE][$value[UA_PROP_TYPE]]=='Unknown') GPCUserAgentValues::$UA_BrowserInfo[UA_DATA_OS_TYPE][$value[UA_PROP_TYPE]]='ua_Unknown'; if($idFilter==-1 or ($idFilter>-1 and $value[UA_PROP_TYPE]!=$idFilter)) $returned[]=array( 'value' => $key, 'cols' => array( l10n($value[UA_PROP_NAME]), l10n(GPCUserAgentValues::$UA_BrowserInfo[UA_DATA_OS_TYPE][$value[UA_PROP_TYPE]]) ) ); break; case 'uaEngine': $returned[]=array( 'value' => $key, 'cols' => array(l10n($value[UA_PROP_NAME])) ); break; case 'uaType': $returned[]=array( 'value' => $key, 'cols' => array(l10n('ua_'.$value)) ); break; } } return(json_encode($returned)); } /** * retrieve all the availables periods in a tree list * * @return String: an array in a json string */ private function ajax_estat_admin_statPeriodTreeList() { global $lang; $returned=array( 'items'=>array( 0 => array( 'id' => 'allYear', 'name' => l10n('estat_allPeriods'), 'nfo' => '', 'level' => 0, 'childs' => array() ) ) ); $items=array(); $gStatFile=new StatDBGlobal($this->fileStatDir, self::FILE_GLOBAL); $gStatFile->open(ASDF_OPEN_READ); $files=$gStatFile->getFilesList(); $gStatFile->close(); foreach($files as $file) { if(!isset($items[$file['year']])) $items[$file['year']]=array( 'id' => 'y-'.$file['year'], 'name' => $file['year'], 'nfo' => '', 'level' => 0, 'childs' => array() ); $items[$file['year']]['childs'][]=array( 'id' => sprintf('ym-%04d%02d', $file['year'], $file['month']), 'name' => $lang['month'][$file['month']], 'nfo' => '', 'level' => 1, 'childs' => array() ); } foreach($items as $item) { $returned['items'][]=$item; } return(json_encode($returned)); } /* * ------------------------------------------------------------------------- * -- private functions * ------------------------------------------------------------------------- */ } //class EStat_Ajax $returned=new EStat_Ajax($prefixeTable, __FILE__); ?>