[ Index ]
 

Code source de phpMyVisites 2.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/core/include/ -> Site.class.php (source)

   1  <?php
   2  /* 

   3   * phpMyVisites : website statistics and audience measurements

   4   * Copyright (C) 2002 - 2006

   5   * http://www.phpmyvisites.net/ 

   6   * phpMyVisites is free software (license GNU/GPL)

   7   * Authors : phpMyVisites team

   8  */
   9  
  10  // $Id: Site.class.php 232 2007-07-06 09:31:19Z matthieu_ $

  11  
  12  
  13  require_once  INCLUDE_PATH . "/core/include/Date.class.php";
  14  require_once  INCLUDE_PATH . "/core/include/functions.php";;
  15  require_once  INCLUDE_PATH . "/core/include/Request.class.php";
  16  require_once  INCLUDE_PATH . "/core/include/User.class.php";
  17  
  18  require_once  INCLUDE_PATH . "/core/include/SiteConfigDb.class.php";
  19  
  20  $GLOBALS['a_info_all_sites'] = false;
  21  
  22  /**

  23   * Class containing info relative to the site we're working on

  24   */
  25  class Site
  26  {
  27      /**

  28       * @var int site id, in the table 'site'

  29       */
  30      var $id;
  31      
  32      /**

  33       * @var array Urls of the site registered in the database

  34       */
  35      var $a_urls;
  36      
  37      /**

  38       * @var array Misc info relative to the site, registered in the database

  39       */
  40      var $a_info;
  41      
  42      /**

  43       * @var string File name of the logo

  44       */
  45      var $logo;
  46      
  47      /**

  48       * @var array Array containing info about URLs parameters excluding

  49       */
  50      var $a_params;
  51      
  52      /**

  53       * @var array Array containing info about newsletters registered

  54       */
  55      var $a_newsletters;
  56      
  57      /**

  58      * @var array Array containing info about partners_name URL=>Name

  59      */
  60      var $a_partnerName;
  61      
  62      /**

  63      * @var array Array containing info about partners_id : Id=>Name

  64      */
  65      var $a_partnerId;
  66          
  67      /**

  68      * @var array Array containing info about newsletters registered Name=>array(url1, url2), Name2=>...

  69      */
  70      var $a_partnerUrls;
  71      
  72      /**

  73      * @var string Url of the partner found during the call of _isUrlInArray

  74      */
  75      var $_partner_found_url;
  76      
  77      /**

  78      * @var object Date of the first day with log for this site

  79      */
  80      var $minDay;
  81      
  82      /**

  83       * Constructor. Loads Urls and misc info about the site.

  84       * 

  85       * @param int $id

  86       */
  87  	function Site($id)
  88      {
  89          $this->id = $id;
  90  
  91          $a = $this->loadUrls();
  92  
  93          if(isset($a[$this->id]))
  94              $this->a_urls = $a[$this->id];
  95          if($GLOBALS['a_info_all_sites'] === false)
  96          {
  97              $GLOBALS['a_info_all_sites'] = $this->loadInfo();
  98          }
  99          
 100          if(isset($GLOBALS['a_info_all_sites'][$this->id]))
 101          {
 102              $this->a_info = $GLOBALS['a_info_all_sites'][$this->id];
 103          }
 104          
 105          
 106      }
 107      
 108  	function getSitesId()
 109      {
 110          $return = array();
 111          if($GLOBALS['a_info_all_sites'] !== false )
 112          {
 113              foreach($GLOBALS['a_info_all_sites'] as $value)
 114              {
 115                  $return[] = $value['idsite'];
 116              }
 117          }
 118          return $return;
 119      }
 120      /**

 121       * returns site id

 122       */
 123  	function getId()
 124      {
 125          return $this->id;
 126      }
 127      
 128  	function getName()
 129      {
 130          return $this->a_info['name'];
 131      }
 132      
 133      /**

 134      * @return pathTheme of the site

 135      */
 136  	function getPathTheme()
 137      {
 138          $ret = THEME_DEFAULT;
 139          if ((isset($this->a_info['path_theme']))
 140          &&  ($this->a_info['path_theme'] != null)
 141          &&  ($this->a_info['path_theme'] != "")) {
 142              $ret = $this->a_info['path_theme'];
 143          }
 144          return $ret;    
 145      }
 146      
 147      /**

 148      * @return idpdf by default

 149      */
 150  	function getIdPdf()
 151      {
 152          return $this->a_info['idpdf'];    
 153      }
 154      
 155      /**

 156       * returns array of params options

 157       */
 158  	function getParams()
 159      {
 160          return array('params_choice' => $this->a_info['params_choice'], 
 161                       'params_names' => $this->a_info['params_names']);
 162      }
 163      
 164      /**

 165       * tests if the Url belongs to the array

 166       * Also assigns the value to partner_found if found

 167       * 

 168       * @access private

 169       * 

 170       * @param string $refererUrl

 171       * @param array $arrayUrls

 172       * 

 173       * @return bool

 174       */
 175  	function _isUrlInArray($refererUrl, $arrayUrls)
 176      {
 177          if(!empty($refererUrl))
 178          {
 179              $refererParse = parse_url($refererUrl);
 180              if(is_array($refererParse) 
 181                  && isset($refererParse['host']) 
 182                  && isset($refererParse['scheme'])
 183                  && is_array($arrayUrls)
 184                  && in_array($refererParse['scheme'].'://'.$refererParse['host'], $arrayUrls)
 185              )
 186              {
 187                  $this->_partner_found_url = $refererParse['scheme'].'://'.$refererParse['host'];
 188                  return true;
 189              }
 190          }
 191          return false;
 192      }
 193      
 194      /**

 195       * return the Date object of the first day of logs for the site

 196       */
 197  	function getMinDay($type = 'object')
 198      {
 199          if(!isset($this->minDay))
 200          {
 201              $this->loadMinDay();
 202          }
 203          
 204          return ($type == 'object') 
 205                              ? $this->minDay
 206                              : $this->minDay->get();
 207      }
 208  
 209  
 210      /**

 211       * loads info about the first day of log

 212       * 

 213       * @return object

 214       */
 215  	function loadMinDay()
 216      {                
 217          
 218          $firstDate = getDateFromTimestamp(time());
 219          
 220          $db =& Db::getInstance();
 221          if(!$db->isReady()
 222              || !$db->areAllTablesInstalled())
 223          {
 224              $this->minDay = new Date( $firstDate );
 225              return false;
 226          }
 227          
 228          $siteFirstDate = array();
 229          
 230          $fileAdress = INCLUDE_PATH."/config/site_first_date.php";
 231          
 232          if(is_file($fileAdress))
 233          {
 234              require $fileAdress;
 235          }
 236  
 237          if(!isset($siteFirstDate[$this->id]))
 238          {
 239              
 240              $r = query("SELECT date1
 241                          FROM ".T_ARCHIVES."
 242                          WHERE period = ".DB_ARCHIVES_PERIOD_DAY."
 243                            AND idsite = ". $this->getId() ."
 244                          ORDER BY date1 ASC
 245                          LIMIT 1
 246                          ");
 247                              
 248              
 249              if(mysql_num_rows($r) === 0)
 250              {
 251                  $r2 = query("SELECT server_date" .
 252                  " FROM ".T_VISIT."
 253                    WHERE idsite = ". $this->getId() .
 254                  " LIMIT 1"
 255                  );
 256  
 257                  if(mysql_num_rows($r2) === 0)
 258                  {
 259                      if(!isset($GLOBALS['sitePrinted'][$this->id]))
 260                      {
 261                          //print($GLOBALS['lang']['generique_aucune_visite_bdd'] . 

 262                          //        "<br><b>Site: ".$this->getName()." (id=".$this->id.")</b><br><br>");

 263                          $GLOBALS['sitePrinted'][$this->id] = true;
 264                      }
 265                  }
 266                  else
 267                  {
 268                      $l = mysql_fetch_assoc($r2);
 269                      $firstDate = $l['server_date'];
 270                  }
 271              }
 272              else
 273              {
 274                  $l = mysql_fetch_assoc($r);
 275                  
 276                  $firstDate = $l['date1'];
 277              }
 278              
 279              $o_firstDate = new Date($firstDate);
 280              
 281              if($o_firstDate->getTimestamp() > time())
 282              {
 283                  $firstDate =  getDateFromTimestamp(time());
 284              }
 285              
 286              $siteFirstDate[$this->id] = $firstDate;
 287              
 288              // save new info in config file

 289              saveConfigFile($fileAdress, $siteFirstDate, 'siteFirstDate');
 290      
 291          }
 292          else
 293          {
 294              $firstDate = $siteFirstDate[$this->id];
 295          }
 296          
 297          $this->minDay = new Date($firstDate);
 298          return true;
 299      }
 300      
 301      /**

 302       * tests if an url belongs to the site urls

 303       * 

 304       * @param string $refererUrl

 305       * 

 306       * @return bool

 307       */
 308  	function isUrlIn($refererUrl)
 309      {
 310          return $this->_isUrlInArray($refererUrl, $this->a_urls);
 311      }
 312      
 313      /**

 314       * tests if the url is a partner url

 315       * 

 316       * @param string $refererUrl

 317       * 

 318       * @return bool

 319       */
 320  	function isPartner($refererUrl)
 321      {
 322          return $this->_isUrlInArray($refererUrl, $this->getPartnerUrlsFlat());
 323      }
 324      
 325      /**

 326       * returns logo file name

 327       * 

 328       * @return string

 329       */
 330  	function getLogo()
 331      {
 332          return $this->a_info['logo'];
 333      }
 334      
 335      /**

 336       * returns array of site urls

 337       * 

 338       * @return array

 339       */
 340  	function getUrls()
 341      {
 342          return $this->a_urls;
 343      }
 344      
 345      /**

 346       * returns array of site info

 347       * 

 348       * @return array

 349       */
 350  	function getInfo()
 351      {
 352          return $this->a_info;
 353      }
 354      
 355      /**

 356       * returns array of Partner urls group by partner host

 357       * 

 358       * @return array

 359       */
 360  	function getPartnerUrls()
 361      {
 362          if(!isset($this->a_partnerUrls))
 363          {
 364              $this->loadPartner();
 365          }
 366          return $this->a_partnerUrls;
 367      }
 368      
 369      /**

 370       * returns array of partner urls, flat, without being grouped by hostname

 371       * 

 372       * @return array

 373       */
 374  	function getPartnerUrlsFlat()
 375      {
 376          if(!isset($this->a_partnerUrlsFlat))
 377          {
 378              $this->loadPartner();
 379          }
 380          return $this->a_partnerUrlsFlat;
 381      }
 382      
 383      /**

 384       * returns partner name which has been detected previously with isPartner

 385       *  

 386       * @return string

 387       */
 388  	function getPartnerName()
 389      {
 390          if(!isset($this->a_partnerUrls))
 391          {
 392              $this->loadPartner();
 393          }
 394          return $this->a_partnerName[$this->_partner_found_url];
 395      }
 396      
 397      /**

 398       * returns Name of the partner who has id = $id

 399       * 

 400       * @param int $id

 401       * 

 402       * @return string

 403       */
 404  	function getPartnerNameFromId($id)
 405      {
 406          if(!isset($this->a_partnerId))
 407          {
 408              $this->loadPartner();
 409          }
 410          return isset($this->a_partnerId[$id])
 411                      ? $this->a_partnerId[$id]
 412                      : 'unknown partner';
 413      }
 414      
 415  	function getPartnerUrlsId( $idPartner)
 416      {
 417          if(!isset($this->a_partnerUrlsId))
 418          {
 419              $this->loadPartner();
 420          }
 421          return $this->a_partnerUrlsId[$idPartner];
 422      }
 423  
 424  	function getPartnerSite()
 425      {
 426          if(!isset($this->a_partnerSite))
 427          {
 428              $this->loadPartner();
 429          }
 430          return $this->a_partnerSite;
 431      }
 432      /**

 433      * returns Name of the newsletter who has id = $id

 434      * 

 435      * @param int $id

 436      * @param bool $forceCreate : if newsletter not exists create it

 437      * 

 438      * @return string

 439      */
 440  	function getNewsletterName($id, $forceCreate = false)
 441      {        
 442          if(!isset($this->a_newsletters))
 443          {
 444              $this->loadNewsletter();
 445          }
 446          if ((! isset($this->a_newsletters[$id])) && ($forceCreate))
 447          {
 448              // If not exists, and force create : create Newsletter

 449              $confSite = new SiteConfigDb();
 450              if (defined("NEWSLETTER_CREATE_NAME")) {
 451                  $confSite->createNewsletter($id, NEWSLETTER_CREATE_NAME.$id, $this->id);
 452              }
 453              else {
 454                  $confSite->createNewsletter($id, 'Newsletter : '.$id, $this->id);
 455              }
 456              // Reload Newsletter

 457              $this->loadNewsletter();
 458          }
 459          
 460          return isset($this->a_newsletters[$id])?$this->a_newsletters[$id]:false;
 461      }
 462      
 463  	function getNewsletters()
 464      {
 465          if(!isset($this->a_newsletters))
 466          {
 467              $this->loadNewsletter();
 468          }
 469          return $this->a_newsletters;
 470      }
 471      
 472  	function getNewslettersSite()
 473      {
 474          if(!isset($this->a_newsletters))
 475          {
 476              $this->loadNewsletter();
 477          }
 478          return $this->a_newslettersSite;
 479      }
 480      
 481      /**

 482       * loads all partners properties

 483       */
 484  	function loadPartner()
 485      {
 486          $r = query("SELECT pu.url as url, p.name as name, p.idsite_partner as id, p.idsite as idsite
 487                      FROM ".T_SITE_PARTNER." as p 
 488                          LEFT JOIN ".T_SITE_PARTNER_URL." as pu 
 489                              USING (idsite_partner)
 490              "        
 491              //WHERE p.idsite = ".$this->id."

 492                      );
 493          $p = array();
 494          $p2 = array();
 495          $p3 = array();
 496          $p4 = array();
 497          $p5 = array();
 498          $p6 = array();
 499          while($l = mysql_fetch_assoc($r))
 500          {
 501              $p[$l['name']][] = $l['url'];
 502              $p2[$l['url']] = $l['name'];
 503              $p3[] = $l['url'];
 504              
 505              $p4[$l['id']] = $l['name'];
 506              $p5[$l['id']][] = $l['url'];
 507              
 508              if($l['idsite'] == $this->id)
 509              {
 510                  $p6[$l['id']] = $l['name'];
 511              }
 512              
 513          }
 514          $this->a_partnerUrls = $p;
 515          $this->a_partnerName = $p2;
 516          $this->a_partnerId = $p4;
 517          
 518          $this->a_partnerUrlsId = $p5;
 519          $this->a_partnerUrlsFlat = $p3;
 520          
 521          $this->a_partnerSite = $p6;
 522      }
 523      
 524      /**

 525       * load all newsletters properties

 526       */
 527  	function loadNewsletter()
 528      {
 529          $r = query("SELECT name, idsite, idnewsletter as id
 530                      FROM ".T_NEWSLETTER."
 531                      WHERE idsite = ".$this->id."
 532                      ");
 533          $p = array();
 534          $p2 = array();
 535          while($l = mysql_fetch_assoc($r))
 536          {
 537              $p[$l['id']] = $l['name'];
 538              
 539              if($l['idsite'] == $this->id)
 540              {
 541                  $p2[$l['id']] = $l['name'];
 542              }
 543          }
 544          $this->a_newsletters = $p;
 545          
 546          $this->a_newslettersSite = $p2;
 547          //printDebug($p);

 548          return true;
 549      }
 550      
 551      /**

 552       * load site Urls

 553       * 

 554       * @return array site urls

 555       */
 556  	function loadUrls()
 557      {        
 558          if(!Request::isPhpmvLogModule())
 559          {
 560              $db =& Db::getInstance();
 561              if(!Request::isPhpmvLogModule())
 562              if( 
 563                  Request::isCurrentModuleAnInstallModule() 
 564                  || Request::isPhpmvLogModule()
 565                  || !$db->isReady()
 566                  || !$db->areAllTablesInstalled() 
 567                  )
 568              { 
 569                  return array();
 570              }
 571          }
 572          $siteUrls = array();
 573          
 574          $fileAdress = INCLUDE_PATH."/config/site_urls.php";
 575          if(is_file($fileAdress))
 576          {
 577              require $fileAdress;
 578              if(sizeof($siteUrls) != 0)
 579              {
 580                  return $siteUrls;
 581              }
 582          }
 583          
 584          $r = query("SELECT idsite, url 
 585                      FROM ".T_SITE_URL."
 586                      ORDER BY idsite_url "
 587              );
 588      
 589          if(mysql_num_rows($r)==0)
 590          {
 591              $GLOBALS['content_message_tpl'] = $GLOBALS['lang']['generique_aucune_site_bdd'];
 592              
 593              return;
 594          }
 595          else
 596          {
 597              while($l = mysql_fetch_assoc($r))
 598              {
 599                  $siteUrls[$l['idsite']][] = $l['url'];
 600              }
 601              saveConfigFile($fileAdress, $siteUrls, 'siteUrls');
 602              return $siteUrls;
 603          }
 604      }
 605      
 606      /**

 607       * returns only allowed websites for admin/view

 608       */
 609  	function getAllowedSites( $type )
 610      {
 611          $return = array();
 612          $request =& Request::getInstance();
 613          $user =& User::getInstance();
 614          foreach($GLOBALS['a_info_all_sites'] as $id => $a_info)
 615          {
 616              
 617              if( ($type == 'view' && $user->isSiteAllowedView( $id ))
 618                  ||
 619                  ($type == 'admin' && $user->isSiteAllowedAdmin( $id ))
 620                  || $request->isCrontabAllowed()
 621              )            
 622              {
 623                  if(isset($a_info['name']))
 624                  {
 625                      $return[$id] = $a_info['name'];
 626                  }
 627              }    
 628          }
 629          ksort($return);
 630          return $return; 
 631      }
 632  
 633  	function generateFiles()
 634      {
 635          $this->loadInfo();
 636          $this->loadUrls();
 637          $this->loadMinDay();
 638          $f = INCLUDE_PATH . "/config/";
 639          
 640          @chmod( $f.'site_info.php', 0777);
 641          @chmod( $f.'site_urls.php', 0777);
 642          @chmod( $f.'site_first_date.php', 0777);
 643          
 644          @unlink( $f.'site_info.php' );
 645          @unlink( $f.'site_first_date.php' );
 646          @unlink( $f.'site_urls.php' );
 647          
 648          // if unlink didn't work we use a second technique, 

 649          // fill the config file with an empty array, which will then be 

 650          // detected by the loadInfo or loadUrls and then it will regenerate the file as if it was absent

 651          saveConfigFile($f.'site_urls.php', array(), 'siteUrls');
 652          saveConfigFile($f.'site_info.php', array(), 'siteInfo');
 653          
 654          $this->loadInfo();
 655          $this->loadUrls();
 656          $this->loadMinDay();
 657      }
 658      
 659      
 660  	function getIpArray()
 661      {
 662          return isset($this->a_info['ips'])
 663                      ? $this->a_info['ips']
 664                      : array();
 665      }
 666      
 667  	function getFirstSiteAvailable()
 668      {
 669          $db =& Db::getInstance();
 670          if($db->isReady()
 671              && $db->areAllTablesInstalled() )
 672          {
 673              $r = query("SELECT idsite
 674                          FROM ".T_SITE
 675                      );
 676              if(mysql_num_rows($r) > 0)
 677              {
 678                  if(DEFAULT_SITE == -1)
 679                  {
 680                      return -1;
 681                  }
 682  
 683                  $l = mysql_fetch_row($r);
 684                  $firstSiteAvailable = $l[0];
 685                  do
 686                  {
 687                      if($l[0] == DEFAULT_SITE)
 688                      {
 689                          return DEFAULT_SITE;
 690                      }
 691                  } while($l = mysql_fetch_row($r));
 692                  return $firstSiteAvailable;
 693              }
 694          }
 695          return 1;
 696      }
 697      
 698  	function getTotalSites()
 699      {
 700              $r = query("SELECT count(*)" .
 701                      " FROM ".T_SITE
 702                      );
 703              $l = mysql_fetch_row($r);
 704              return $l[0];
 705      }
 706      /**

 707       * load site Info

 708       * 

 709       * @return array site info (name of logo.suffix, params_include_only, params_exclude)

 710       */
 711  	function loadInfo()
 712      {
 713          if(!Request::isPhpmvLogModule())
 714          {
 715              $db =& Db::getInstance();
 716              if(!$db->isReady()
 717                  || !$db->areAllTablesInstalled()
 718                  || Request::isCurrentModuleAnInstallModule()
 719                  )
 720              {
 721                  return array();
 722              }
 723          }
 724          $siteInfo = array();
 725          $fileAdress = INCLUDE_PATH."/config/site_info.php";
 726          
 727          // file exists and is correct

 728          if(is_file($fileAdress))
 729          {
 730              require $fileAdress;
 731              if(sizeof($siteInfo) != 0)
 732              {
 733                  return $siteInfo;
 734              }
 735          }
 736          
 737          $r = query("SELECT idsite, name, logo, params_choice, params_names, idpdf, path_theme" .
 738                  " FROM ".T_SITE
 739                  );
 740          
 741          if(mysql_num_rows($r)==0)
 742          {
 743              $GLOBALS['content_message_tpl'] = $GLOBALS['lang']['generique_aucune_site_bdd'];
 744              return;
 745          }
 746          else
 747          {            
 748              while($l = mysql_fetch_assoc($r))
 749              {
 750                  $siteInfo[$l['idsite']] = $l;
 751              }
 752              
 753      
 754              // ip to exclude

 755              
 756              // select excluded Ip2long

 757              $r2 = query("SELECT ip_min, ip_max, idsite " .
 758                      " FROM ".T_IP_IGNORE
 759                      );
 760              if(mysql_num_rows($r2)>0)
 761              {
 762                  $i = 0;
 763                  while($l2 = mysql_fetch_assoc($r2))
 764                  {
 765                      $siteInfo[$l2['idsite']]['ips'][] = array(
 766                                                                  $l2['ip_min'],
 767                                                                  $l2['ip_max']
 768                                                                  );
 769                  }
 770              }
 771              saveConfigFile($fileAdress, $siteInfo, 'siteInfo');
 772              return $siteInfo;
 773          }
 774      }
 775  }
 776  ?>


Généré le : Mon Nov 26 14:10:01 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics