[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/dao/ -> referers.class.php (source)

   1  <?php
   2  
   3      lt_include( PLOG_CLASS_PATH."class/dao/model.class.php" );
   4      lt_include( PLOG_CLASS_PATH."class/config/config.class.php" );
   5      
   6      define( "REFERRERS_LIST_ITEMS_PER_PAGE", 15 );    
   7  
   8      /**
   9       * Data access class (or model) to handle all the referers.
  10       *
  11       * \ingroup DAO
  12       */
  13      class Referers extends Model 
  14      {
  15  
  16          var $_enabled;
  17  
  18          /**
  19           * Constructor.
  20           */
  21      	function Referers()
  22          {
  23              $this->Model();
  24  
  25              $this->table = $this->getPrefix()."referers";
  26          }
  27  
  28          /**
  29           * Adds a referer to the database.
  30           *
  31           * @param refererHeader The referer header as was received by PHP.
  32           * @param articleId The article being hit by this referer.
  33           * @return Returns true if successful or false otherwise.
  34           */
  35          function addReferer( $refererHeader, $articleId, $blogId )
  36          {
  37              lt_include( PLOG_CLASS_PATH."class/net/url.class.php" );
  38  
  39              // we only add a new referer if we come from somewhere else than our own server
  40              $ourHost = $_SERVER["HTTP_HOST"];
  41  
  42              $refererUrl = new Url( $refererHeader );
  43              $refererUrlHost = ( $refererUrl->getPort() == 80 ) ? $refererUrl->getHost() : $refererUrl->getHost().':'.$refererUrl->getPort();
  44  
  45              // if they're the same, we quit
  46              if( $refererUrlHost == $ourHost || $refererUrlHost == "" )
  47                  return;
  48  
  49              // we have to check if a referer with that information exists
  50              // in the database
  51              $query = "UPDATE ".$this->getPrefix()."referers SET hits = hits + 1 WHERE url = '".Db::qstr($refererHeader).
  52                       "' AND article_id = '".Db::qstr($articleId)."' AND blog_id = '".Db::qstr($blogId)."';";
  53              $result = $this->Execute( $query );
  54              if( !$result )
  55                  return false;
  56  
  57              // check how many rows were updated this time.
  58              if( $this->_db->Affected_Rows() == 0 ) {
  59                  // we have to insert the row manually
  60                  $query2 = "INSERT INTO ".$this->getPrefix()."referers (url,article_id,blog_id) 
  61                             VALUES ('".Db::qstr($refererHeader)."','".Db::qstr($articleId)."','".Db::qstr($blogId)."');";
  62                  $result2 = $this->Execute( $query2 );
  63              }
  64  
  65              return true;
  66          }
  67  
  68          /**
  69           * Reads all the referers for a given article.
  70           *
  71           * @param articleId The identifier of the article from which we want to know
  72           * the referers.
  73           * @return An array of Referer objects with the information about all the referers
  74           * for this article, or false if the article does not have any referer.
  75           */
  76          function getArticleReferers( $articleId, $blogId = 0, $page = 0, $itemsPerPage = 0 )
  77          {
  78              return $this->_getReferers( $blogId, $articleId, $page, $itemsPerPage );
  79          }
  80  
  81          /**
  82           * Retrieves all the referers from the database.
  83           *
  84           * @return An array of Referer objects containing all the referers from the
  85           * database, or false if there are no referers.
  86           */
  87          function getAllReferers( $page = 0, $itemsPerPage = 0)
  88          {
  89              return $this->_getReferers( 0, 0, $page, $itemsPerPage );
  90          }
  91  
  92          /**
  93           * Returns a list with all the referers for a blog.
  94           *
  95           * @param blogId Blog identifier. If not specified, retrieves all the referers.
  96           * @param page current page
  97           * @param itemsPerPage defaults to REFERRERS_LIST_ITEMS_PER_PAGE
  98           * @return An array with all the referers for a blog.
  99           * article.
 100           */
 101          function getBlogReferers( $blogId = 0, $page = 0, $itemsPerPage = REFERRERS_LIST_ITEMS_PER_PAGE)
 102          {
 103              return $this->_getReferers( $blogId, 0, $page, $itemsPerPage );
 104          }
 105          
 106          /**
 107           * @private
 108           * builds up and executes a query based on the conditions passed as parameters
 109           */
 110  		function _getReferers( $blogId = 0, $articleId = 0, $page = -1, $itemsPerPage = REFERRERS_LIST_ITEMS_PER_PAGE )
 111          {        
 112              $query = "SELECT * FROM ".$this->getPrefix()."referers";
 113              
 114              $conds = false;
 115              $where = "";
 116              if( $blogId > 0 ) {
 117                  $where .= " blog_id = '".Db::qstr($blogId)."'";
 118                  $conds = true;
 119              }
 120              
 121              if( $articleId > 0 ) {
 122                  if( $conds ) $where .= " AND ";
 123                  $where .= " article_id = '".Db::qstr($articleId)."'";
 124                  $conds = true;
 125              }
 126              
 127              if( $conds )
 128                  $query .= " WHERE $where";
 129                        
 130              $query .= " ORDER BY last_date DESC";
 131  
 132              $result = $this->Execute( $query, $page, $itemsPerPage );
 133  
 134              if( !$result )
 135                  return Array();
 136  
 137              $referers = Array();
 138              while( $row = $result->FetchRow())
 139                  array_push( $referers, $this->mapRow( $row ));
 140              $result->Close();
 141  
 142              return $referers;
 143          
 144          }
 145  
 146          /**
 147           * Private function.
 148           */
 149          function mapRow( $row )
 150          {
 151              lt_include( PLOG_CLASS_PATH."class/dao/referer.class.php" );
 152  
 153              $referer = new Referer( $row["url"], 
 154                                      $row["article_id"], 
 155                                      $row["blog_id"], 
 156                                      $row["last_date"], 
 157                                      $row["hits"], 
 158                                      $row["id"] );
 159  
 160              return $referer;
 161          }
 162          
 163          /**
 164           * retrieves information about one particular referrer
 165           *
 166           * @param referrerId
 167           * @param blogId
 168           * @return false if unsuccessful or a Referer object if successful
 169           */
 170  		function getBlogReferer( $referrerId, $blogId = -1 )
 171          {
 172              $prefix = $this->getPrefix();
 173              $query = "SELECT * FROM {$prefix}referers
 174                        WHERE id = '".Db::qstr($referrerId)."'";
 175              if( $blogId > 0 )
 176                  $query .= " AND blog_id = '".Db::qstr($blogId)."'";
 177                  
 178              $result = $this->Execute( $query );
 179              
 180              if( !$result )
 181                  return false;
 182                  
 183              if( $result->RowCount() == 0 ){
 184                  $result->Close();
 185                  return false;
 186              }
 187                  
 188              $row = $result->FetchRow();
 189              $referrer = $this->mapRow( $row );
 190              $result->Close();
 191  
 192              return $referrer;
 193          }
 194          
 195          /**
 196           * removes a referrer from the database
 197           *
 198           * @param referrerId
 199           * @param blogId
 200           * @return True if successful or false otherwise
 201           */
 202  		function deleteBlogReferer( $referrerId, $blogId = -1 )
 203          {
 204              $prefix = $this->getPrefix();
 205              $query = "DELETE FROM {$prefix}referers
 206                        WHERE id = '".Db::qstr($referrerId)."'";
 207              if( $blogId > 0 )
 208                  $query .= " AND blog_id = '".Db::qstr($blogId)."'";
 209                  
 210              $result = $this->Execute( $query );
 211              
 212              return $result;
 213          }
 214          
 215          /**
 216           * returns how many referrers the blog has
 217           *
 218           *Ê@param blogId
 219           * @param articleId
 220           * @return a number
 221           */
 222  		function getBlogTotalReferers( $blogId, $articleId = -1 )
 223          {
 224              $prefix = $this->getPrefix();
 225              $table  = "{$prefix}referers";
 226              $cond = "blog_id = '".Db::qstr($blogId)."'";
 227              if( $articleId > -1 )
 228                  $cond .= " AND article_id = '".Db::qstr($articleId)."'";
 229              
 230              return( $this->getNumItems( $table, $cond ));
 231          }
 232          
 233          /**
 234           * Delete all the data that depends on a given blog
 235           */
 236  		function deleteBlogReferers( $blogId )
 237          {            
 238              return( $this->delete( "blog_id", $blogId ));
 239          }
 240      }
 241  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics