PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/_plugins_/phpmyvisites/libs/artichow/php4/Artichow.class.php

https://bitbucket.org/pombredanne/spip-zone-treemap
PHP | 321 lines | 152 code | 85 blank | 84 comment | 19 complexity | 8cbcb74ff0a4e43809c6b380c1040dbf MD5 | raw file
  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. // Artichow configuration
  10. // Some useful files
  11. require_once ARTICHOW."/Component.class.php";
  12. require_once ARTICHOW."/Image.class.php";
  13. require_once ARTICHOW."/common.php";
  14. require_once ARTICHOW."/inc/Grid.class.php";
  15. require_once ARTICHOW."/inc/Tools.class.php";
  16. require_once ARTICHOW."/inc/Drawer.class.php";
  17. require_once ARTICHOW."/inc/Math.class.php";
  18. require_once ARTICHOW."/inc/Tick.class.php";
  19. require_once ARTICHOW."/inc/Axis.class.php";
  20. require_once ARTICHOW."/inc/Legend.class.php";
  21. require_once ARTICHOW."/inc/Mark.class.php";
  22. require_once ARTICHOW."/inc/Label.class.php";
  23. require_once ARTICHOW."/inc/Text.class.php";
  24. require_once ARTICHOW."/inc/Color.class.php";
  25. require_once ARTICHOW."/inc/Font.class.php";
  26. require_once ARTICHOW."/inc/Gradient.class.php";
  27. // Catch all errors
  28. ob_start();
  29. /**
  30. * A graph
  31. *
  32. * @package Artichow
  33. */
  34. class awGraph extends awImage {
  35. /**
  36. * Graph name
  37. *
  38. * @var string
  39. */
  40. var $name;
  41. /**
  42. * Cache timeout
  43. *
  44. * @var int
  45. */
  46. var $timeout = 0;
  47. /**
  48. * Graph timing ?
  49. *
  50. * @var bool
  51. */
  52. var $timing;
  53. /**
  54. * Components
  55. *
  56. * @var array
  57. */
  58. var $components = array();
  59. /**
  60. * Graph title
  61. *
  62. * @var Label
  63. */
  64. var $title;
  65. /**
  66. * Construct a new awgraph
  67. *
  68. * @param int $width Graph width
  69. * @param int $height Graph height
  70. * @param string $name Graph name for the cache (must be unique). Let it null to not use the cache.
  71. * @param int $timeout Cache timeout (unix timestamp)
  72. */
  73. function awGraph($width = NULL, $height = NULL, $name = NULL, $timeout = 0) {
  74. parent::awImage();
  75. $this->setSize($width, $height);
  76. if(ARTICHOW_CACHE) {
  77. $this->name = $name;
  78. $this->timeout = $timeout;
  79. // Clean sometimes all the cache
  80. if(mt_rand(0, 5000) === 0) {
  81. awGraph::cleanCache();
  82. }
  83. if($this->name !== NULL) {
  84. $file = ARTICHOW."/cache/".$this->name."-time";
  85. if(is_file($file)) {
  86. $type = awGraph::cleanGraphCache($file);
  87. if($type === NULL) {
  88. awGraph::deleteFromCache($this->name);
  89. } else {
  90. header("Content-Type: image/".$type);
  91. readfile(ARTICHOW."/cache/".$this->name."");
  92. exit;
  93. }
  94. }
  95. }
  96. }
  97. $this->title = new awLabel(
  98. NULL,
  99. new awTuffy(16),
  100. NULL,
  101. 0
  102. );
  103. $this->title->setAlign(LABEL_CENTER, LABEL_BOTTOM);
  104. }
  105. /**
  106. * Delete a graph from the cache
  107. *
  108. * @param string $name Graph name
  109. * @return bool TRUE on success, FALSE on failure
  110. */
  111. function deleteFromCache($name) {
  112. if(ARTICHOW_CACHE) {
  113. if(is_file(ARTICHOW."/cache/".$name."-time")) {
  114. unlink(ARTICHOW."/cache/".$name."");
  115. unlink(ARTICHOW."/cache/".$name."-time");
  116. }
  117. }
  118. }
  119. /**
  120. * Delete all graphs from the cache
  121. */
  122. function deleteAllCache() {
  123. if(ARTICHOW_CACHE) {
  124. $dp = opendir(ARTICHOW."/cache");
  125. while($file = readdir($dp)) {
  126. if($file !== '.' and $file != '..') {
  127. unlink(ARTICHOW."/cache/".$file);
  128. }
  129. }
  130. }
  131. }
  132. /**
  133. * Clean cache
  134. */
  135. function cleanCache() {
  136. if(ARTICHOW_CACHE) {
  137. $glob = glob(ARTICHOW."/cache/*-time");
  138. foreach($glob as $file) {
  139. $type = awGraph::cleanGraphCache($file);
  140. if($type === NULL) {
  141. $name = ereg_replace(".*/(.*)\-time", "\\1", $file);
  142. awGraph::deleteFromCache($name);
  143. }
  144. }
  145. }
  146. }
  147. /**
  148. * Enable/Disable graph timing
  149. *
  150. * @param bool $timing
  151. */
  152. function setTiming($timing) {
  153. $this->timing = (bool)$timing;
  154. }
  155. /**
  156. * Add a component to the graph
  157. *
  158. * @param &$component
  159. */
  160. function add(&$component) {
  161. $this->components[] = $component;
  162. }
  163. /**
  164. * Build the graph and draw component on it
  165. * Image is sent to the user browser
  166. */
  167. function draw() {
  168. if($this->timing) {
  169. $time = microtimeFloat();
  170. }
  171. $this->create();
  172. foreach($this->components as $component) {
  173. $this->drawComponent($component);
  174. }
  175. $this->drawTitle();
  176. $this->drawShadow();
  177. if($this->timing) {
  178. $this->drawTiming(microtimeFloat() - $time);
  179. }
  180. $this->send();
  181. if(ARTICHOW_CACHE) {
  182. if($this->name !== NULL) {
  183. $data = ob_get_contents();
  184. if(is_writable(ARTICHOW."/cache") === FALSE) {
  185. trigger_error("Cache directory is not writable");
  186. }
  187. $file = ARTICHOW."/cache/".$this->name."";
  188. file_put_contents($file, $data);
  189. $file .= "-time";
  190. file_put_contents($file, $this->timeout."\n".$this->getFormat());
  191. }
  192. }
  193. }
  194. function drawTitle() {
  195. $drawer = $this->getDrawer();
  196. $point = new awPoint(
  197. $this->width / 2,
  198. 10
  199. );
  200. $this->title->draw($drawer, $point);
  201. }
  202. function drawTiming($time) {
  203. $drawer = $this->getDrawer();
  204. $label = new awLabel;
  205. $label->set("(".sprintf("%.3f", $time)." s)");
  206. $label->setAlign(LABEL_LEFT, LABEL_TOP);
  207. $label->border->show();
  208. $label->setPadding(1, 0, 0, 0);
  209. $label->setBackgroundColor(new awColor(230, 230, 230, 25));
  210. $label->draw($drawer, new awPoint(5, $drawer->height - 5));
  211. }
  212. function cleanGraphCache($file) {
  213. list(
  214. $time,
  215. $type
  216. ) = explode("\n", file_get_contents($file));
  217. $time = (int)$time;
  218. if($time !== 0 and $time < time()) {
  219. return NULL;
  220. } else {
  221. return $type;
  222. }
  223. }
  224. }
  225. registerClass('Graph');
  226. /*
  227. * To preserve PHP 4 compatibility
  228. */
  229. function microtimeFloat() {
  230. list($usec, $sec) = explode(" ", microtime());
  231. return (float)$usec + (float)$sec;
  232. }
  233. ?>