KXStudio Website https://kx.studio/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
5.3KB

  1. <?php
  2. require_once "filecacher.php";
  3. require_once "memcacher.php";
  4. class Cacher extends Modules {
  5. public function __init() {
  6. # Prepare actions that should result in new cache files.
  7. $this->prepare_cache_updaters();
  8. $config = Config::current();
  9. $config->cache_exclude = (array) $config->cache_exclude;
  10. if (!empty($config->cache_exclude))
  11. foreach ($config->cache_exclude as &$exclude)
  12. if (substr($exclude, 7) != "http://")
  13. $exclude = $config->url."/".ltrim($exclude, "/");
  14. if(count((array)$config->cache_memcached_hosts) > 0){
  15. $this->cacher = new MemCacher(self_url(), $config);
  16. }else{
  17. $this->cacher = new FileCacher(self_url(), $config);
  18. }
  19. # Prepare actions that should result in new cache files.
  20. $this->prepare_cache_updaters();
  21. }
  22. static function __install() {
  23. $config = Config::current();
  24. $config->set("cache_expire", 1800);
  25. $config->set("cache_exclude", array());
  26. $config->set("cache_memcached_hosts", array());
  27. }
  28. static function __uninstall() {
  29. $config = Config::current();
  30. $config->remove("cache_expire");
  31. $config->remove("cache_exclude");
  32. $config->remove("cache_memcached_hosts");
  33. }
  34. public function route_init($route) {
  35. if (!empty($_POST) or
  36. !($route->controller instanceof MainController) or
  37. in_array($this->url, Config::current()->cache_exclude) or
  38. $this->cancelled or
  39. !$this->cacher->url_available() or
  40. Flash::exists())
  41. return;
  42. $cache = $this->cacher->get($route);
  43. foreach($cache['headers'] as $header)
  44. header($header);
  45. exit($cache['contents']);
  46. }
  47. public function end($route) {
  48. if (!($route->controller instanceof MainController) or
  49. in_array($this->url, Config::current()->cache_exclude) or
  50. $this->cancelled or
  51. $this->cacher->url_available() or
  52. Flash::exists())
  53. return;
  54. $this->cacher->set(ob_get_contents());
  55. }
  56. public function prepare_cache_updaters() {
  57. $regenerate = array("add_post", "add_page",
  58. "update_post", "update_page",
  59. "delete_post", "delete_page",
  60. "change_setting");
  61. Trigger::current()->filter($regenerate, "cacher_regenerate_triggers");
  62. foreach ($regenerate as $action)
  63. $this->addAlias($action, "regenerate");
  64. $post_triggers = array();
  65. foreach (Trigger::current()->filter($post_triggers, "cacher_regenerate_posts_triggers") as $action)
  66. $this->addAlias($action, "remove_post_cache");
  67. }
  68. public function regenerate() {
  69. $this->cacher->regenerate();
  70. }
  71. public function regenerate_local($user = null) {
  72. $this->cacher->regenerate_local($user);
  73. }
  74. public function remove_caches_for($url) {
  75. $this->cacher->remove_caches_for($url);
  76. }
  77. public function remove_post_cache($thing) {
  78. $this->remove_caches_for(htmlspecialchars_decode($thing->post()->url()));
  79. }
  80. public function update_user($user) {
  81. $this->regenerate_local(sanitize($user->login));
  82. }
  83. public function settings_nav($navs) {
  84. if (Visitor::current()->group->can("change_settings"))
  85. $navs["cache_settings"] = array("title" => __("Cache", "cacher"));
  86. return $navs;
  87. }
  88. public function admin_cache_settings($admin) {
  89. if (!Visitor::current()->group->can("change_settings"))
  90. show_403(__("Access Denied"), __("You do not have sufficient privileges to change settings."));
  91. if (empty($_POST))
  92. return $admin->display("cache_settings");
  93. if (!isset($_POST['hash']) or $_POST['hash'] != Config::current()->secure_hashkey)
  94. show_403(__("Access Denied"), __("Invalid security key."));
  95. $exclude = (empty($_POST['cache_exclude']) ? array() : explode(", ", $_POST['cache_exclude']));
  96. $memcached_hosts = empty($_POST['cache_memcached_hosts']) ? array() : explode(", ", $_POST['cache_memcached_hosts']);
  97. $config = Config::current();
  98. if ($config->set("cache_expire", $_POST['cache_expire']) and $config->set("cache_exclude", $exclude) and $config->set("cache_memcached_hosts", $memcached_hosts))
  99. Flash::notice(__("Settings updated."), "/admin/?action=cache_settings");
  100. }
  101. public function admin_clear_cache() {
  102. if (!Visitor::current()->group->can("change_settings"))
  103. show_403(__("Access Denied"), __("You do not have sufficient privileges to change settings."));
  104. $this->regenerate();
  105. Flash::notice(__("Cache cleared.", "cacher"), "/admin/?action=cache_settings");
  106. }
  107. }