PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/system/expressionengine/third_party/ce_cache/libraries/drivers/Ce_cache_driver.php

https://gitlab.com/sops21/mt-rubidoux
PHP | 262 lines | 115 code | 32 blank | 115 comment | 20 complexity | 0953e2f51317659c73e71b5ded404a91 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CE Cache - Driver abstract class.
  4. *
  5. * @author Aaron Waldon
  6. * @copyright Copyright (c) 2013 Causing Effect
  7. * @license http://www.causingeffect.com/software/expressionengine/ce-cache/license-agreement
  8. * @link http://www.causingeffect.com
  9. */
  10. abstract class Ce_cache_driver
  11. {
  12. protected $debug = false;
  13. public function __construct()
  14. {
  15. $this->EE = get_instance();
  16. //if the template debugger is enabled, and a super admin user is logged in, enable debug mode
  17. $this->debug = false;
  18. if ( $this->EE->session->userdata['group_id'] == 1 && $this->EE->config->item('template_debugging') == 'y' )
  19. {
  20. $this->debug = true;
  21. }
  22. }
  23. /**
  24. * Is the driver supported?
  25. *
  26. * @abstract
  27. * @return bool
  28. */
  29. abstract public function is_supported();
  30. /**
  31. * The driver name.
  32. *
  33. * @abstract
  34. * @return void
  35. */
  36. abstract public function name();
  37. //------------------------------------ cache item methods ------------------------------------
  38. /**
  39. * Stores a cache item.
  40. *
  41. * @abstract
  42. * @param $id
  43. * @param string $content
  44. * @param string $seconds
  45. * @return bool
  46. */
  47. abstract public function set( $id, $content = '', $seconds = '' );
  48. /**
  49. * Retrieve an item from the cache.
  50. *
  51. * @abstract
  52. * @param $id
  53. * @return mixed
  54. */
  55. abstract public function get( $id );
  56. /**
  57. * Remove an item from the cache.
  58. *
  59. * @abstract
  60. * @param $id
  61. * @return bool
  62. */
  63. abstract public function delete( $id );
  64. /**
  65. * Gives information about the item.
  66. *
  67. * @abstract
  68. * @param $id
  69. * @param bool $get_content Include the content in the return array?
  70. * @return array|bool An array with the keys 'expiry', 'made', 'ttl', 'ttl_remaining', 'size', and 'size_raw' and 'content' (if $get_content is set to true) on success, or false on failure
  71. */
  72. abstract public function meta( $id, $get_content = true );
  73. //------------------------------------ cache methods ------------------------------------
  74. /**
  75. * Purges the entire cache.
  76. *
  77. * @abstract
  78. * @return bool
  79. */
  80. abstract public function clear();
  81. /**
  82. * Retrieves all of the cached items at the specified relative path.
  83. *
  84. * @abstract
  85. * @param $relative_path
  86. * @return array
  87. */
  88. abstract public function get_all( $relative_path );
  89. /**
  90. * Retrieves all of the cached items (or folder paths) at the specified relative path for 1 level of depth.
  91. *
  92. * @param $relative_path
  93. * @return array
  94. */
  95. public function get_level( $relative_path )
  96. {
  97. $items = $this->get_all( $relative_path );
  98. if ( empty( $items ) )
  99. {
  100. return array();
  101. }
  102. $driver = $this->name();
  103. if ( $driver == 'db' || $driver == 'apc' ) //db and apc drivers
  104. {
  105. $files = array();
  106. $folders = array();
  107. foreach ( $items as $index => $item )
  108. {
  109. $slash_pos = strpos( $item['id'], '/' ); //is there a slash?
  110. if ( $slash_pos !== false ) //a directory (first segment of item path at a higher level)
  111. {
  112. $folders[] = substr( $item['id'], 0, $slash_pos + 1 );
  113. }
  114. else //an item at the current level
  115. {
  116. $files[$index] = $items[$index];
  117. }
  118. }
  119. $folders = array_unique( $folders );
  120. $dirs = array();
  121. foreach ( $folders as $folder )
  122. {
  123. $dirs[] = array( 'id' => $folder );
  124. }
  125. $items = array_merge( $dirs, $files );
  126. unset( $folders, $files, $dirs );
  127. return $items;
  128. }
  129. else //other drivers
  130. {
  131. foreach ( $items as $index => $item )
  132. {
  133. $slash_pos = strpos( $item, '/' );
  134. if ( $slash_pos !== false ) //a directory (first segment of item path at a higher level)
  135. {
  136. $items[ $index ] = substr( $item, 0, $slash_pos + 1 );
  137. }
  138. }
  139. return array_unique( $items );
  140. }
  141. }
  142. /**
  143. * Retrieves basic info about the cache.
  144. *
  145. * @abstract
  146. * @return array|bool
  147. */
  148. abstract public function info();
  149. //------------------------------------ helpers ------------------------------------
  150. /**
  151. * Removes double slashes, except when they are preceded by ':', so that 'http://', etc are preserved.
  152. *
  153. * @param string $str The string from which to remove the double slashes.
  154. * @return string The string with double slashes removed.
  155. */
  156. protected function remove_duplicate_slashes( $str )
  157. {
  158. return preg_replace( '#(?<!:)//+#', '/', $str );
  159. }
  160. /**
  161. * Determines if an id is valid or if it contains invalid characters.
  162. *
  163. * @param $id
  164. * @return bool
  165. */
  166. protected function id_is_valid( $id )
  167. {
  168. $sanitized = $this->EE->security->sanitize_filename( $id, true );
  169. return ( $id === $sanitized );
  170. }
  171. /**
  172. * Simple method to log a debug message to the EE Debug console.
  173. *
  174. * @param string $method
  175. * @param string $message
  176. * @return void
  177. */
  178. protected function log_debug_message( $method = '', $message = '' )
  179. {
  180. $this->EE->TMPL->log_item( "&nbsp;&nbsp;***&nbsp;&nbsp;CE Cache - " . $this->name() . " - $method debug: " . $message );
  181. }
  182. /**
  183. * Converts a file size from bytes to a human readable format.
  184. *
  185. * A method derived from a function originally posted by xelozz -at- gmail.com 18-Feb-2010 10:34 to http://us2.php.net/manual/en/function.memory-get-usage.php#96280
  186. * Original code licensed under: http://creativecommons.org/licenses/by/3.0/legalcode
  187. *
  188. * @param int $size Bytes.
  189. * @param int $precision
  190. * @return string Human readable file size.
  191. */
  192. public static function convert_size( $size, $precision = 2 )
  193. {
  194. if ( ! is_numeric( $size ) || $size <= 0 )
  195. {
  196. return '0 b';
  197. }
  198. if ( ! is_numeric( $precision ) || $precision < 0 )
  199. {
  200. $precision = 2;
  201. }
  202. $unit = array( 'b', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB' );
  203. $i = floor( log( $size, 1024 ) );
  204. if ( isset( $unit[$i] ) )
  205. {
  206. return @round( $size / pow( 1024, $i ), $precision ) . ' ' . $unit[$i];
  207. }
  208. else
  209. {
  210. return $size . ' ' . 'b';
  211. }
  212. }
  213. /**
  214. * Get the file size of a string.
  215. * @static
  216. * @param $string
  217. * @return int
  218. */
  219. protected static function size( $string )
  220. {
  221. if ( function_exists( 'mb_strlen' ) )
  222. {
  223. return mb_strlen( $string, '8bit' );
  224. }
  225. else
  226. {
  227. return strlen( $string );
  228. }
  229. }
  230. }