/wp-content/plugins/nextgen-gallery/vendor/imagely/pope-framework/lib/class.pope_cache.php

https://github.com/livinglab/openlab · PHP · 334 lines · 277 code · 55 blank · 2 comment · 54 complexity · 6ea64a8a8bbfee5eeeb0c3965a3cb996 MD5 · raw file

  1. <?php
  2. class C_Pope_Cache
  3. {
  4. static $_instance = NULL;
  5. static $_driver = NULL;
  6. static $key_prefix = array('pope');
  7. static $enabled = TRUE; // the cache is not used at all
  8. static $do_not_lookup = FALSE; // no lookups are made to the cache
  9. static $force_update = FALSE; // force the cache to be updated
  10. protected $_queue = array();
  11. static function &get_instance($driver='C_Pope_Cache_SingleFile')
  12. {
  13. if (!isset(self::$_instance))
  14. self::$_instance = new C_Pope_Cache($driver);
  15. return self::$_instance;
  16. }
  17. function __construct($driver = 'C_Pope_Cache_SingleFile')
  18. {
  19. if (is_null(self::$_driver))
  20. self::set_driver($driver);
  21. }
  22. static function set_driver($class_name)
  23. {
  24. self::$_driver = $class_name;
  25. }
  26. static function add_key_prefix($prefix)
  27. {
  28. self::$key_prefix[] = $prefix;
  29. $driver = self::$_driver;
  30. return call_user_func("{$driver}::add_key_prefix", $prefix);
  31. }
  32. static function get($params, $default=NULL)
  33. {
  34. if (!self::$enabled)
  35. return $default;
  36. $cache = self::get_instance();
  37. $key = $cache->get_key_from_params($params);
  38. if (self::$do_not_lookup)
  39. return $default;
  40. else
  41. return $cache->lookup($key, $default);
  42. }
  43. static function set($params, $value)
  44. {
  45. if (self::$enabled)
  46. {
  47. $cache = self::get_instance();
  48. $key = $cache->get_key_from_params($params);
  49. $cache->update($key, $value);
  50. }
  51. }
  52. function get_key_from_params($params)
  53. {
  54. return md5(json_encode($params));
  55. }
  56. function lookup($key, $default=NULL)
  57. {
  58. $driver = self::$_driver;
  59. return call_user_func("{$driver}::lookup", $key, $default);
  60. }
  61. function update($key, $value)
  62. {
  63. $driver = self::$_driver;
  64. return call_user_func("{$driver}::update", $key, $value);
  65. }
  66. function flush()
  67. {
  68. $driver = self::$_driver;
  69. return call_user_func("{$driver}::flush");
  70. }
  71. }
  72. interface I_Pope_Cache_Driver
  73. {
  74. public static function add_key_prefix($prefix);
  75. public static function flush();
  76. public static function lookup($key, $default = NULL);
  77. public static function update($key, $value);
  78. }
  79. class C_Pope_Cache_MultiFile implements I_Pope_Cache_Driver
  80. {
  81. static $initialized = FALSE;
  82. static $cache_dir = NULL;
  83. static $use_cache_subdir = TRUE;
  84. public static function initialize()
  85. {
  86. if (self::$initialized)
  87. return;
  88. if (is_null(self::$cache_dir))
  89. self::set_cache_dir();
  90. self::$initialized = TRUE;
  91. }
  92. public static function add_key_prefix($prefix)
  93. {
  94. self::set_cache_dir();
  95. }
  96. public static function lookup($key, $default=NULL)
  97. {
  98. self::initialize();
  99. $filename = self::get_filename_from_key($key);
  100. if (@file_exists($filename))
  101. return json_decode(@file_get_contents($filename));
  102. else
  103. return $default;
  104. }
  105. public static function update($key, $value)
  106. {
  107. self::initialize();
  108. // TODO: log/warn users their cache dir can't be used
  109. if (@file_exists(self::$cache_dir) && !@is_dir(self::$cache_dir))
  110. return;
  111. $filename = self::get_filename_from_key($key);
  112. if (@file_exists($filename) && C_Pope_Cache::$force_update == FALSE)
  113. return;
  114. @file_put_contents($filename, json_encode($value));
  115. }
  116. public static function flush()
  117. {
  118. self::initialize();
  119. $dir = self::$cache_dir;
  120. if (@file_exists($dir) && @is_dir($dir))
  121. {
  122. foreach (@scandir($dir) as $file) {
  123. if ($file == '.' || $file == '..')
  124. continue;
  125. $file = self::join_paths($dir, $file);
  126. if (is_dir($file) && self::$use_cache_subdir)
  127. {
  128. self::flush($dir);
  129. }
  130. else {
  131. if (!self::$use_cache_subdir && strpos(basename($file), implode('_', C_Pope_Cache::$key_prefix) . '_') === 0)
  132. @unlink($file);
  133. elseif (self::$use_cache_subdir)
  134. @unlink($file);
  135. }
  136. }
  137. }
  138. }
  139. public static function set_cache_dir()
  140. {
  141. if (defined('POPE_CACHE_DIR'))
  142. {
  143. self::$cache_dir = POPE_CACHE_DIR;
  144. if (!@file_exists(self::$cache_dir))
  145. @mkdir(self::$cache_dir, 0777, TRUE);
  146. }
  147. else {
  148. if (self::$use_cache_subdir)
  149. {
  150. self::$cache_dir = self::join_paths(sys_get_temp_dir(), C_Pope_cache::$key_prefix);
  151. if (!@file_exists(self::$cache_dir))
  152. {
  153. // if we can't create a subdirectory we fallback to prefixing filenames (eg /tmp/pope_$key)
  154. $mkdir_result = @mkdir(self::$cache_dir, 0777, TRUE);
  155. if (FALSE === $mkdir_result)
  156. {
  157. self::$use_cache_subdir = FALSE;
  158. self::$cache_dir = self::join_paths(sys_get_temp_dir());
  159. }
  160. }
  161. }
  162. else {
  163. self::$cache_dir = self::join_paths(sys_get_temp_dir());
  164. }
  165. }
  166. $func = function_exists('wp_is_writable') ? 'wp_is_writable' : 'is_writable';
  167. if (!@$func(self::$cache_dir))
  168. C_Pope_Cache::$enabled = FALSE;
  169. }
  170. public static function get_filename_from_key($key)
  171. {
  172. if (self::$use_cache_subdir)
  173. $filename = self::join_paths(self::$cache_dir, $key);
  174. else
  175. $filename = self::join_paths(self::$cache_dir) . DIRECTORY_SEPARATOR . implode('_', C_Pope_Cache::$key_prefix) . '_' . $key;
  176. return $filename;
  177. }
  178. public static function join_paths()
  179. {
  180. $args = func_get_args();
  181. foreach ($args as &$arg) {
  182. if (is_array($arg))
  183. $arg = implode(DIRECTORY_SEPARATOR, $arg);
  184. }
  185. return implode(DIRECTORY_SEPARATOR, $args);
  186. }
  187. }
  188. function C_Pope_Cache_SingleFile_Shutdown()
  189. {
  190. $filename = C_Pope_Cache_SingleFile::get_filename();
  191. @file_put_contents(
  192. $filename,
  193. json_encode(C_Pope_Cache_SingleFile::$cache)
  194. );
  195. }
  196. class C_Pope_Cache_SingleFile implements I_Pope_Cache_Driver
  197. {
  198. static $initialized = FALSE;
  199. static $cache_dir = NULL;
  200. static $cache = array();
  201. static $writepending = FALSE;
  202. public static function initialize()
  203. {
  204. if (self::$initialized)
  205. return;
  206. if (is_null(self::$cache_dir))
  207. self::set_cache_dir();
  208. $filename = self::get_filename();
  209. if (@file_exists($filename))
  210. self::$cache = json_decode(@file_get_contents($filename), TRUE);
  211. if (!is_array(self::$cache))
  212. self::$cache = array();
  213. register_shutdown_function('C_Pope_Cache_SingleFile_Shutdown');
  214. self::$initialized = TRUE;
  215. }
  216. public static function add_key_prefix($prefix)
  217. {
  218. self::set_cache_dir();
  219. }
  220. public static function lookup($key, $default=NULL)
  221. {
  222. self::initialize();
  223. if (!empty(self::$cache[$key]))
  224. return self::$cache[$key];
  225. else
  226. return $default;
  227. }
  228. public static function update($key, $value)
  229. {
  230. self::initialize();
  231. $dupe = FALSE;
  232. if (!empty(self::$cache[$key])) {
  233. if (self::$cache[$key] == $value)
  234. $dupe = TRUE;
  235. }
  236. if ($dupe == TRUE && C_Pope_Cache::$force_update == FALSE)
  237. return;
  238. self::$cache[$key] = $value;
  239. self::$writepending = TRUE;
  240. }
  241. public static function flush()
  242. {
  243. self::initialize();
  244. $filename = self::get_filename();
  245. if (@file_exists($filename))
  246. @unlink($filename);
  247. }
  248. public static function get_filename()
  249. {
  250. if (count(C_Pope_Cache::$key_prefix) == 1)
  251. C_Pope_Cache::add_key_prefix('cache');
  252. $filename = implode('_', C_Pope_Cache::$key_prefix);
  253. return self::join_paths(self::$cache_dir, $filename);
  254. }
  255. public static function set_cache_dir()
  256. {
  257. if (defined('POPE_CACHE_DIR'))
  258. {
  259. self::$cache_dir = POPE_CACHE_DIR;
  260. if (!@file_exists(self::$cache_dir))
  261. @mkdir(self::$cache_dir, 0777, TRUE);
  262. }
  263. else {
  264. self::$cache_dir = self::join_paths(sys_get_temp_dir());
  265. }
  266. $func = function_exists('wp_is_writable') ? 'wp_is_writable' : 'is_writable';
  267. if (!@$func(self::$cache_dir))
  268. C_Pope_Cache::$enabled = FALSE;
  269. }
  270. public static function join_paths()
  271. {
  272. $args = func_get_args();
  273. foreach ($args as &$arg) {
  274. if (is_array($arg))
  275. $arg = implode(DIRECTORY_SEPARATOR, $arg);
  276. }
  277. return implode(DIRECTORY_SEPARATOR, $args);
  278. }
  279. }