[ Index ]
 

Code source de phpMyVisites 2.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/libs/artichow/php4/ -> Artichow.class.php (source)

   1  <?php
   2  /*
   3   * This work is hereby released into the Public Domain.
   4   * To view a copy of the public domain dedication,
   5   * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
   6   * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
   7   *
   8   */
   9  
  10  // Artichow configuration
  11  
  12  // Some useful files
  13  require_once  ARTICHOW."/Component.class.php";
  14  require_once  ARTICHOW."/Image.class.php";
  15  require_once  ARTICHOW."/common.php";
  16  
  17  require_once  ARTICHOW."/inc/Grid.class.php";
  18  require_once  ARTICHOW."/inc/Tools.class.php";
  19  require_once  ARTICHOW."/inc/Drawer.class.php";
  20  require_once  ARTICHOW."/inc/Math.class.php";
  21  require_once  ARTICHOW."/inc/Tick.class.php";
  22  require_once  ARTICHOW."/inc/Axis.class.php";
  23  require_once  ARTICHOW."/inc/Legend.class.php";
  24  require_once  ARTICHOW."/inc/Mark.class.php";
  25  require_once  ARTICHOW."/inc/Label.class.php";
  26  require_once  ARTICHOW."/inc/Text.class.php";
  27  require_once  ARTICHOW."/inc/Color.class.php";
  28  require_once  ARTICHOW."/inc/Font.class.php";
  29  require_once  ARTICHOW."/inc/Gradient.class.php";
  30  
  31  // Catch all errors
  32  ob_start();
  33  
  34  /**
  35   * A graph 
  36   *
  37   * @package Artichow
  38   */
  39  class awGraph extends awImage {
  40  
  41      /**
  42       * Graph name
  43       *
  44       * @var string
  45       */
  46      var $name;
  47  
  48      /**
  49       * Cache timeout
  50       *
  51       * @var int
  52       */
  53      var $timeout = 0;
  54      
  55      /**
  56       * Graph timing ?
  57       *
  58       * @var bool
  59       */
  60      var $timing;
  61      
  62      /**
  63       * Components
  64       *
  65       * @var array
  66       */
  67      var $components = array();
  68      
  69      /**
  70       * Graph title
  71       *
  72       * @var Label
  73       */
  74      var $title;
  75      
  76      /**
  77       * Construct a new awgraph
  78       *
  79       * @param int $width Graph width
  80       * @param int $height Graph height
  81       * @param string $name Graph name for the cache (must be unique). Let it null to not use the cache.
  82       * @param int $timeout Cache timeout (unix timestamp)
  83       */
  84  	 function awGraph($width = NULL, $height = NULL, $name = NULL, $timeout = 0) {
  85          
  86          parent::awImage();
  87      
  88          $this->setSize($width, $height);
  89          
  90          if(ARTICHOW_CACHE) {
  91      
  92              $this->name = $name;
  93              $this->timeout = $timeout;
  94              
  95              // Clean sometimes all the cache
  96              if(mt_rand(0, 5000) ===  0) {
  97                  awGraph::cleanCache();
  98              }
  99              
 100              if($this->name !== NULL) {
 101              
 102                  $file = ARTICHOW."/cache/".$this->name."-time";
 103                  
 104                  if(is_file($file)) {
 105                  
 106                      $type = awGraph::cleanGraphCache($file);
 107                      
 108                      if($type === NULL) {
 109                          awGraph::deleteFromCache($this->name);
 110                      } else {
 111                          header("Content-Type: image/".$type);
 112                          readfile(ARTICHOW."/cache/".$this->name."");
 113                          exit;
 114                      }
 115                      
 116                  }
 117              
 118              }
 119              
 120          }
 121          
 122          
 123          $this->title = new awLabel(
 124              NULL,
 125              new awTuffy(16),
 126              NULL,
 127              0
 128          );
 129          $this->title->setAlign(LABEL_CENTER, LABEL_BOTTOM);
 130      
 131      }
 132      
 133      /**
 134       * Delete a graph from the cache
 135       *
 136       * @param string $name Graph name
 137       * @return bool TRUE on success, FALSE on failure
 138       */
 139  	  function deleteFromCache($name) {
 140          
 141          if(ARTICHOW_CACHE) {
 142          
 143              if(is_file(ARTICHOW."/cache/".$name."-time")) {
 144                  unlink(ARTICHOW."/cache/".$name."");
 145                  unlink(ARTICHOW."/cache/".$name."-time");
 146              }
 147              
 148          }
 149          
 150      }
 151      
 152      /**
 153       * Delete all graphs from the cache
 154       */
 155  	  function deleteAllCache() {
 156      
 157          if(ARTICHOW_CACHE) {
 158          
 159              $dp = opendir(ARTICHOW."/cache");
 160              
 161              while($file = readdir($dp)) {
 162                  if($file !== '.' and $file != '..') {
 163                      unlink(ARTICHOW."/cache/".$file);
 164                  }
 165              }
 166              
 167          }
 168      
 169      }
 170      
 171      /**
 172       * Clean cache
 173       */
 174  	  function cleanCache() {
 175      
 176          if(ARTICHOW_CACHE) {
 177      
 178              $glob = glob(ARTICHOW."/cache/*-time");
 179              
 180              foreach($glob as $file) {
 181                  
 182                  $type = awGraph::cleanGraphCache($file);
 183                  
 184                  if($type === NULL) {
 185                      $name = ereg_replace(".*/(.*)\-time", "\\1", $file);
 186                      awGraph::deleteFromCache($name);
 187                  }
 188              
 189              }
 190              
 191          }
 192          
 193      }
 194      
 195      /**
 196       * Enable/Disable graph timing
 197       *
 198       * @param bool $timing
 199       */
 200  	 function setTiming($timing) {
 201          $this->timing = (bool)$timing;
 202      }
 203       
 204      /**
 205       * Add a component to the graph
 206       *
 207       * @param &$component
 208       */
 209  	 function add(&$component) {
 210      
 211          $this->components[] = $component;
 212      
 213      }
 214      
 215      /**
 216       * Build the graph and draw component on it
 217       * Image is sent to the user browser
 218       */
 219  	 function draw() {
 220          
 221          if($this->timing) {
 222              $time = microtimeFloat();
 223          }
 224      
 225          $this->create();
 226          
 227          foreach($this->components as $component) {
 228          
 229              $this->drawComponent($component);
 230          
 231          }
 232          
 233          $this->drawTitle();
 234          $this->drawShadow();
 235          
 236          if($this->timing) {
 237              $this->drawTiming(microtimeFloat() - $time);
 238          }
 239          
 240          $this->send();
 241          
 242          if(ARTICHOW_CACHE) {
 243              
 244              if($this->name !== NULL) {
 245          
 246                  $data = ob_get_contents();
 247              
 248                  if(is_writable(ARTICHOW."/cache") === FALSE) {
 249                      trigger_error("Cache directory is not writable");
 250                  }
 251              
 252                  $file = ARTICHOW."/cache/".$this->name."";
 253                  file_put_contents($file, $data);
 254              
 255                  $file .= "-time";
 256                  file_put_contents($file, $this->timeout."\n".$this->getFormat());
 257                  
 258              }
 259              
 260          }
 261      
 262      }
 263          
 264  	 function drawTitle() {
 265      
 266          $drawer = $this->getDrawer();
 267      
 268          $point = new awPoint(
 269              $this->width / 2,
 270              10
 271          );
 272          
 273          $this->title->draw($drawer, $point);
 274      
 275      }
 276      
 277  	 function drawTiming($time) {
 278      
 279          $drawer = $this->getDrawer();
 280          
 281          $label = new awLabel;
 282          $label->set("(".sprintf("%.3f", $time)." s)");
 283          $label->setAlign(LABEL_LEFT, LABEL_TOP);
 284          $label->border->show();
 285          $label->setPadding(1, 0, 0, 0);
 286          $label->setBackgroundColor(new awColor(230, 230, 230, 25));
 287          
 288          $label->draw($drawer, new awPoint(5, $drawer->height - 5));
 289      
 290      }
 291      
 292  	  function cleanGraphCache($file) {
 293      
 294          list(
 295              $time,
 296              $type
 297          ) = explode("\n", file_get_contents($file));
 298          
 299          $time = (int)$time;
 300          
 301          if($time !== 0 and $time < time()) {
 302              return NULL;
 303          } else {
 304              return $type;
 305          }
 306          
 307          
 308      }
 309  
 310  }
 311  
 312  registerClass('Graph');
 313  
 314  /*
 315   * To preserve PHP 4 compatibility
 316   */
 317  function microtimeFloat() { 
 318      list($usec, $sec) = explode(" ", microtime()); 
 319      return (float)$usec + (float)$sec; 
 320  }
 321  ?>


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