[ Index ] |
|
Code source de phpMyVisites 2.3 |
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."/common.php"; 14 require_once ARTICHOW."/Component.class.php"; 15 require_once ARTICHOW."/Image.class.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 * Some labels to add to the component 71 * 72 * @var array 73 */ 74 var $labels = array(); 75 76 /** 77 * Graph title 78 * 79 * @var Label 80 */ 81 var $title; 82 83 /** 84 * Construct a new graph 85 * 86 * @param int $width Graph width 87 * @param int $height Graph height 88 * @param string $name Graph name for the cache (must be unique). Let it null to not use the cache. 89 * @param int $timeout Cache timeout (unix timestamp) 90 */ 91 function awGraph($width = NULL, $height = NULL, $name = NULL, $timeout = 0) { 92 93 parent::awImage(); 94 95 $this->setSize($width, $height); 96 97 $this->name = $name; 98 $this->timeout = $timeout; 99 100 // Clean sometimes all the cache 101 if(mt_rand(0, 5000) === 0) { 102 awGraph::cleanCache(); 103 } 104 105 if($this->name !== NULL) { 106 107 $file = ARTICHOW."/cache/".$this->name."-time"; 108 109 if(is_file($file)) { 110 111 $type = awGraph::cleanGraphCache($file); 112 113 if($type === NULL) { 114 awGraph::deleteFromCache($this->name); 115 } else { 116 header("Content-Type: image/".$type); 117 readfile(ARTICHOW."/cache/".$this->name.""); 118 exit; 119 } 120 121 } 122 123 } 124 125 126 $this->title = new awLabel( 127 NULL, 128 new awTuffy(16), 129 NULL, 130 0 131 ); 132 $this->title->setAlign(LABEL_CENTER, LABEL_BOTTOM); 133 134 } 135 136 /** 137 * Delete a graph from the cache 138 * 139 * @param string $name Graph name 140 * @return bool TRUE on success, FALSE on failure 141 */ 142 function deleteFromCache($name) { 143 144 if(is_file(ARTICHOW."/cache/".$name."-time")) { 145 unlink(ARTICHOW."/cache/".$name.""); 146 unlink(ARTICHOW."/cache/".$name."-time"); 147 } 148 149 } 150 151 /** 152 * Delete all graphs from the cache 153 */ 154 function deleteAllCache() { 155 156 $dp = opendir(ARTICHOW."/cache"); 157 158 while($file = readdir($dp)) { 159 if($file !== '.' and $file != '..') { 160 unlink(ARTICHOW."/cache/".$file); 161 } 162 } 163 164 } 165 166 /** 167 * Clean cache 168 */ 169 function cleanCache() { 170 171 $glob = glob(ARTICHOW."/cache/*-time"); 172 173 foreach($glob as $file) { 174 175 $type = awGraph::cleanGraphCache($file); 176 177 if($type === NULL) { 178 $name = ereg_replace(".*/(.*)\-time", "\\1", $file); 179 awGraph::deleteFromCache($name); 180 } 181 182 } 183 184 } 185 186 /** 187 * Enable/Disable Graph timing 188 * 189 * @param bool $timing 190 */ 191 function setTiming($timing) { 192 $this->timing = (bool)$timing; 193 } 194 195 /** 196 * Add a component to the graph 197 * 198 * @param &$component 199 */ 200 function add(&$component) { 201 202 $this->components[] = $component; 203 204 } 205 206 /** 207 * Add a label to the component 208 * 209 * @param &$label 210 * @param int $x Position on X axis of the center of the text 211 * @param int $y Position on Y axis of the center of the text 212 */ 213 function addLabel(&$label, $x, $y) { 214 215 $this->labels[] = array( 216 $label, $x, $y 217 ); 218 219 } 220 221 /** 222 * Add a label to the component with aboslute position 223 * 224 * @param &$label 225 * @param $point Text position 226 */ 227 function addAbsLabel(&$label, $point) { 228 229 $this->labels[] = array( 230 $label, $point 231 ); 232 233 } 234 235 /** 236 * Build the graph and draw component on it 237 * Image is sent to the user browser 238 * 239 * @param string $file Save the image in the specified file. Let it null to print image to screen. 240 */ 241 function draw($file = NULL) { 242 243 if($this->timing) { 244 $time = microtimeFloat(); 245 } 246 247 $this->create(); 248 249 foreach($this->components as $component) { 250 $this->drawComponent($component); 251 252 } 253 254 $this->drawTitle(); 255 $this->drawShadow(); 256 $this->drawLabels(); 257 258 if($this->timing) { 259 $this->drawTiming(microtimeFloat() - $time); 260 } 261 262 $this->send($file); 263 264 if($file === NULL) { 265 266 $data = ob_get_contents(); 267 268 if($this->name !== NULL) { 269 270 if(is_writable(ARTICHOW."/cache") === FALSE) { 271 trigger_error("Cache directory is not writable"); 272 } 273 274 $file = ARTICHOW."/cache/".$this->name.""; 275 file_put_contents($file, $data); 276 277 $file .= "-time"; 278 file_put_contents($file, $this->timeout."\n".$this->getFormat()); 279 280 } 281 282 } 283 284 } 285 286 function drawLabels() { 287 288 $drawer = $this->getDrawer(); 289 290 foreach($this->labels as $array) { 291 292 if(count($array) === 3) { 293 294 // Text in relative position 295 list($label, $x, $y) = $array; 296 297 $point = new awPoint( 298 $x * $this->width, 299 $y * $this->height 300 ); 301 302 } else { 303 304 // Text in absolute position 305 list($label, $point) = $array; 306 307 } 308 309 $label->draw($drawer, $point); 310 311 } 312 313 } 314 315 function drawTitle() { 316 317 $drawer = $this->getDrawer(); 318 319 $point = new awPoint( 320 $this->width / 2, 321 10 322 ); 323 324 $this->title->draw($drawer, $point); 325 326 } 327 328 function drawTiming($time) { 329 330 $drawer = $this->getDrawer(); 331 332 $label = new awLabel; 333 $label->set("(".sprintf("%.3f", $time)." s)"); 334 $label->setAlign(LABEL_LEFT, LABEL_TOP); 335 $label->border->show(); 336 $label->setPadding(1, 0, 0, 0); 337 $label->setBackgroundColor(new awColor(230, 230, 230, 25)); 338 339 $label->draw($drawer, new awPoint(5, $drawer->height - 5)); 340 341 } 342 343 function cleanGraphCache($file) { 344 345 list( 346 $time, 347 $type 348 ) = explode("\n", file_get_contents($file)); 349 350 $time = (int)$time; 351 352 if($time !== 0 and $time < time()) { 353 return NULL; 354 } else { 355 return $type; 356 } 357 358 359 } 360 361 } 362 363 registerClass('Graph'); 364 365 /* 366 * To preserve PHP 4 compatibility 367 */ 368 function microtimeFloat() { 369 list($usec, $sec) = explode(" ", microtime()); 370 return (float)$usec + (float)$sec; 371 } 372 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 14:10:01 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |