/modules/gallery/helpers/theme.php

https://github.com/ophian/gallery3 · PHP · 113 lines · 63 code · 14 blank · 36 comment · 17 complexity · 77daf2d5e14c164594c2d3501f242542 MD5 · raw file

  1. <?php defined("SYSPATH") or die("No direct script access.");
  2. /**
  3. * Gallery - a web based photo album viewer and editor
  4. * Copyright (C) 2000-2011 Bharat Mediratta
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. /**
  21. * This is the API for handling themes.
  22. *
  23. * Note: by design, this class does not do any permission checking.
  24. */
  25. class theme_Core {
  26. public static $admin_theme_name;
  27. public static $site_theme_name;
  28. public static $is_admin;
  29. /**
  30. * Load the active theme. This is called at bootstrap time. We will only ever have one theme
  31. * active for any given request.
  32. */
  33. static function load_themes() {
  34. $input = Input::instance();
  35. $path = $input->server("PATH_INFO");
  36. if (empty($path)) {
  37. $path = "/" . $input->get("kohana_uri");
  38. }
  39. $config = Kohana_Config::instance();
  40. $modules = $config->get("core.modules");
  41. // Normally Router::find_uri() strips off the url suffix for us, but we're working off of the
  42. // PATH_INFO here so we need to strip it off manually
  43. if ($suffix = Kohana::config("core.url_suffix")) {
  44. $path = preg_replace("#" . preg_quote($suffix) . "$#u", "", $path);
  45. }
  46. self::$is_admin = $path == "/admin" || !strncmp($path, "/admin/", 7);
  47. self::$site_theme_name = module::get_var("gallery", "active_site_theme");
  48. // If the site theme doesn't exist, fall back to wind.
  49. if (!file_exists(THEMEPATH . self::$site_theme_name . "/theme.info")) {
  50. site_status::error(t("Theme '%name' is missing. Falling back to the Wind theme.",
  51. array("name" => self::$site_theme_name)), "missing_site_theme");
  52. module::set_var("gallery", "active_site_theme", self::$site_theme_name = "wind");
  53. }
  54. if (self::$is_admin) {
  55. // Load the admin theme
  56. self::$admin_theme_name = module::get_var("gallery", "active_admin_theme");
  57. // If the admin theme doesn't exist, fall back to admin_wind.
  58. if (!file_exists(THEMEPATH . self::$admin_theme_name . "/theme.info")) {
  59. site_status::error(t("Admin theme '%name' is missing! Falling back to the Wind theme.",
  60. array("name" => self::$admin_theme_name)), "missing_admin_theme");
  61. module::set_var("gallery", "active_admin_theme", self::$admin_theme_name = "admin_wind");
  62. }
  63. array_unshift($modules, THEMEPATH . self::$admin_theme_name);
  64. // If the site theme has an admin subdir, load that as a module so that
  65. // themes can provide their own code.
  66. if (file_exists(THEMEPATH . self::$site_theme_name . "/admin")) {
  67. array_unshift($modules, THEMEPATH . self::$site_theme_name . "/admin");
  68. }
  69. // Admins can override the site theme, temporarily. This lets us preview themes.
  70. if (identity::active_user()->admin && $override = $input->get("theme")) {
  71. if (file_exists(THEMEPATH . $override)) {
  72. self::$admin_theme_name = $override;
  73. array_unshift($modules, THEMEPATH . self::$admin_theme_name);
  74. } else {
  75. Kohana_Log::add("error", "Missing override admin theme: '$override'");
  76. }
  77. }
  78. } else {
  79. // Admins can override the site theme, temporarily. This lets us preview themes.
  80. if (identity::active_user()->admin && $override = $input->get("theme")) {
  81. if (file_exists(THEMEPATH . $override)) {
  82. self::$site_theme_name = $override;
  83. } else {
  84. Kohana_Log::add("error", "Missing override site theme: '$override'");
  85. }
  86. }
  87. array_unshift($modules, THEMEPATH . self::$site_theme_name);
  88. }
  89. $config->set("core.modules", $modules);
  90. }
  91. static function get_info($theme_name) {
  92. $theme_name = preg_replace("/[^a-zA-Z0-9\._-]/", "", $theme_name);
  93. $file = THEMEPATH . "$theme_name/theme.info";
  94. $theme_info = new ArrayObject(parse_ini_file($file), ArrayObject::ARRAY_AS_PROPS);
  95. $theme_info->description = t($theme_info->description);
  96. $theme_info->name = t($theme_info->name);
  97. return $theme_info;
  98. }
  99. }