PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/gallery/helpers/module.php

https://github.com/jersub/gallery3
PHP | 537 lines | 319 code | 62 blank | 156 comment | 43 complexity | 60f88fda01e27c61ed90d7a51de63f46 MD5 | raw file
Possible License(s): GPL-2.0
  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-2010 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 modules.
  22. *
  23. * Note: by design, this class does not do any permission checking.
  24. */
  25. class module_Core {
  26. public static $active = array();
  27. public static $modules = array();
  28. public static $var_cache = null;
  29. public static $available = array();
  30. /**
  31. * Set the version of the corresponding Module_Model
  32. * @param string $module_name
  33. * @param integer $version
  34. */
  35. static function set_version($module_name, $version) {
  36. $module = module::get($module_name);
  37. if (!$module->loaded()) {
  38. $module->name = $module_name;
  39. $module->active = $module_name == "gallery"; // only gallery is active by default
  40. }
  41. $module->version = $version;
  42. $module->save();
  43. Kohana_Log::add("debug", "$module_name: version is now $version");
  44. }
  45. /**
  46. * Load the corresponding Module_Model
  47. * @param string $module_name
  48. */
  49. static function get($module_name) {
  50. if (empty(self::$modules[$module_name])) {
  51. return ORM::factory("module")->where("name", "=", $module_name)->find();
  52. }
  53. return self::$modules[$module_name];
  54. }
  55. /**
  56. * Get the information about a module
  57. * @returns ArrayObject containing the module information from the module.info file or false if
  58. * not found
  59. */
  60. static function info($module_name) {
  61. $module_list = module::available();
  62. return isset($module_list->$module_name) ? $module_list->$module_name : false;
  63. }
  64. /**
  65. * Check to see if a module is installed
  66. * @param string $module_name
  67. */
  68. static function is_installed($module_name) {
  69. return array_key_exists($module_name, self::$modules);
  70. }
  71. /**
  72. * Check to see if a module is active
  73. * @param string $module_name
  74. */
  75. static function is_active($module_name) {
  76. return array_key_exists($module_name, self::$modules) &&
  77. self::$modules[$module_name]->active;
  78. }
  79. /**
  80. * Return the list of available modules, including uninstalled modules.
  81. */
  82. static function available() {
  83. if (empty(self::$available)) {
  84. $modules = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
  85. foreach (glob(MODPATH . "*/module.info") as $file) {
  86. $module_name = basename(dirname($file));
  87. $modules->$module_name =
  88. new ArrayObject(parse_ini_file($file), ArrayObject::ARRAY_AS_PROPS);
  89. $m =& $modules->$module_name;
  90. $m->installed = module::is_installed($module_name);
  91. $m->active = module::is_active($module_name);
  92. $m->code_version = $m->version;
  93. $m->version = module::get_version($module_name);
  94. $m->locked = false;
  95. if ($m->active && $m->version != $m->code_version) {
  96. site_status::warning(t("Some of your modules are out of date. <a href=\"%upgrader_url\">Upgrade now!</a>", array("upgrader_url" => url::site("upgrader"))), "upgrade_now");
  97. }
  98. }
  99. // Lock certain modules
  100. $modules->gallery->locked = true;
  101. $identity_module = module::get_var("gallery", "identity_provider", "user");
  102. $modules->$identity_module->locked = true;
  103. $modules->ksort();
  104. self::$available = $modules;
  105. }
  106. return self::$available;
  107. }
  108. /**
  109. * Return a list of all the active modules in no particular order.
  110. */
  111. static function active() {
  112. return self::$active;
  113. }
  114. /**
  115. * Check that the module can be activated. (i.e. all the prerequistes exist)
  116. * @param string $module_name
  117. * @return array an array of warning or error messages to be displayed
  118. */
  119. static function can_activate($module_name) {
  120. module::_add_to_path($module_name);
  121. $messages = array();
  122. $installer_class = "{$module_name}_installer";
  123. if (method_exists($installer_class, "can_activate")) {
  124. $messages = call_user_func(array($installer_class, "can_activate"));
  125. }
  126. // Remove it from the active path
  127. module::_remove_from_path($module_name);
  128. return $messages;
  129. }
  130. /**
  131. * Allow modules to indicate the impact of deactivating the specified module
  132. * @param string $module_name
  133. * @return array an array of warning or error messages to be displayed
  134. */
  135. static function can_deactivate($module_name) {
  136. $data = (object)array("module" => $module_name, "messages" => array());
  137. module::event("pre_deactivate", $data);
  138. return $data->messages;
  139. }
  140. /**
  141. * Install a module. This will call <module>_installer::install(), which is responsible for
  142. * creating database tables, setting module variables and calling module::set_version().
  143. * Note that after installing, the module must be activated before it is available for use.
  144. * @param string $module_name
  145. */
  146. static function install($module_name) {
  147. module::_add_to_path($module_name);
  148. $installer_class = "{$module_name}_installer";
  149. if (method_exists($installer_class, "install")) {
  150. call_user_func_array(array($installer_class, "install"), array());
  151. } else {
  152. module::set_version($module_name, module::available()->$module_name->code_version);
  153. }
  154. // Set the weight of the new module, which controls the order in which the modules are
  155. // loaded. By default, new modules are installed at the end of the priority list. Since the
  156. // id field is monotonically increasing, the easiest way to guarantee that is to set the weight
  157. // the same as the id. We don't know that until we save it for the first time
  158. $module = ORM::factory("module")->where("name", "=", $module_name)->find();
  159. if ($module->loaded()) {
  160. $module->weight = $module->id;
  161. $module->save();
  162. }
  163. module::load_modules();
  164. // Now the module is installed but inactive, so don't leave it in the active path
  165. module::_remove_from_path($module_name);
  166. log::success(
  167. "module", t("Installed module %module_name", array("module_name" => $module_name)));
  168. }
  169. private static function _add_to_path($module_name) {
  170. $config = Kohana_Config::instance();
  171. $kohana_modules = $config->get("core.modules");
  172. array_unshift($kohana_modules, MODPATH . $module_name);
  173. $config->set("core.modules", $kohana_modules);
  174. // Rebuild the include path so the module installer can benefit from auto loading
  175. Kohana::include_paths(true);
  176. }
  177. private static function _remove_from_path($module_name) {
  178. $config = Kohana_Config::instance();
  179. $kohana_modules = $config->get("core.modules");
  180. if (($key = array_search(MODPATH . $module_name, $kohana_modules)) !== false) {
  181. unset($kohana_modules[$key]);
  182. $kohana_modules = array_values($kohana_modules); // reindex
  183. }
  184. $config->set("core.modules", $kohana_modules);
  185. Kohana::include_paths(true);
  186. }
  187. /**
  188. * Upgrade a module. This will call <module>_installer::upgrade(), which is responsible for
  189. * modifying database tables, changing module variables and calling module::set_version().
  190. * Note that after upgrading, the module must be activated before it is available for use.
  191. * @param string $module_name
  192. */
  193. static function upgrade($module_name) {
  194. $version_before = module::get_version($module_name);
  195. $installer_class = "{$module_name}_installer";
  196. $available = module::available();
  197. if (method_exists($installer_class, "upgrade")) {
  198. call_user_func_array(array($installer_class, "upgrade"), array($version_before));
  199. } else {
  200. if (isset($available->$module_name->code_version)) {
  201. module::set_version($module_name, $available->$module_name->code_version);
  202. } else {
  203. throw new Exception("@todo UNKNOWN_MODULE");
  204. }
  205. }
  206. module::load_modules();
  207. $version_after = module::get_version($module_name);
  208. if ($version_before != $version_after) {
  209. log::success(
  210. "module", t("Upgraded module %module_name from %version_before to %version_after",
  211. array("module_name" => $module_name,
  212. "version_before" => $version_before,
  213. "version_after" => $version_after)));
  214. }
  215. if ($version_after != $available->$module_name->code_version) {
  216. throw new Exception("@todo MODULE_FAILED_TO_UPGRADE");
  217. }
  218. }
  219. /**
  220. * Activate an installed module. This will call <module>_installer::activate() which should take
  221. * any steps to make sure that the module is ready for use. This will also activate any
  222. * existing graphics rules for this module.
  223. * @param string $module_name
  224. */
  225. static function activate($module_name) {
  226. module::_add_to_path($module_name);
  227. $installer_class = "{$module_name}_installer";
  228. if (method_exists($installer_class, "activate")) {
  229. call_user_func_array(array($installer_class, "activate"), array());
  230. }
  231. $module = module::get($module_name);
  232. if ($module->loaded()) {
  233. $module->active = true;
  234. $module->save();
  235. }
  236. module::load_modules();
  237. graphics::activate_rules($module_name);
  238. block_manager::activate_blocks($module_name);
  239. log::success(
  240. "module", t("Activated module %module_name", array("module_name" => $module_name)));
  241. }
  242. /**
  243. * Deactivate an installed module. This will call <module>_installer::deactivate() which should
  244. * take any cleanup steps to make sure that the module isn't visible in any way. Note that the
  245. * module remains available in Kohana's cascading file system until the end of the request!
  246. * @param string $module_name
  247. */
  248. static function deactivate($module_name) {
  249. $installer_class = "{$module_name}_installer";
  250. if (method_exists($installer_class, "deactivate")) {
  251. call_user_func_array(array($installer_class, "deactivate"), array());
  252. }
  253. $module = module::get($module_name);
  254. if ($module->loaded()) {
  255. $module->active = false;
  256. $module->save();
  257. }
  258. module::load_modules();
  259. graphics::deactivate_rules($module_name);
  260. block_manager::deactivate_blocks($module_name);
  261. log::success(
  262. "module", t("Deactivated module %module_name", array("module_name" => $module_name)));
  263. }
  264. /**
  265. * Uninstall a deactivated module. This will call <module>_installer::uninstall() which should
  266. * take whatever steps necessary to make sure that all traces of a module are gone.
  267. * @param string $module_name
  268. */
  269. static function uninstall($module_name) {
  270. $installer_class = "{$module_name}_installer";
  271. if (method_exists($installer_class, "uninstall")) {
  272. call_user_func(array($installer_class, "uninstall"));
  273. }
  274. graphics::remove_rules($module_name);
  275. $module = module::get($module_name);
  276. if ($module->loaded()) {
  277. $module->delete();
  278. }
  279. module::load_modules();
  280. // We could delete the module vars here too, but it's nice to leave them around
  281. // in case the module gets reinstalled.
  282. log::success(
  283. "module", t("Uninstalled module %module_name", array("module_name" => $module_name)));
  284. }
  285. /**
  286. * Load the active modules. This is called at bootstrap time.
  287. */
  288. static function load_modules() {
  289. self::$modules = array();
  290. self::$active = array();
  291. $kohana_modules = array();
  292. // In version 32 we introduced a weight column so we can specify the module order
  293. // If we try to use that blindly, we'll break earlier versions before they can even
  294. // run the upgrader.
  295. $modules = module::get_version("gallery") < 32 ?
  296. ORM::factory("module")->find_all():
  297. ORM::factory("module")->order_by("weight")->find_all();
  298. foreach ($modules as $module) {
  299. self::$modules[$module->name] = $module;
  300. if (!$module->active) {
  301. continue;
  302. }
  303. if ($module->name == "gallery") {
  304. $gallery = $module;
  305. } else {
  306. self::$active[] = $module;
  307. $kohana_modules[] = MODPATH . $module->name;
  308. }
  309. }
  310. self::$active[] = $gallery; // put gallery last in the module list to match core.modules
  311. $config = Kohana_Config::instance();
  312. $config->set(
  313. "core.modules", array_merge($kohana_modules, $config->get("core.modules")));
  314. }
  315. /**
  316. * Run a specific event on all active modules.
  317. * @param string $name the event name
  318. * @param mixed $data data to pass to each event handler
  319. */
  320. static function event($name, &$data=null) {
  321. $args = func_get_args();
  322. array_shift($args);
  323. $function = str_replace(".", "_", $name);
  324. if (method_exists("gallery_event", $function)) {
  325. switch (count($args)) {
  326. case 0:
  327. gallery_event::$function();
  328. break;
  329. case 1:
  330. gallery_event::$function($args[0]);
  331. break;
  332. case 2:
  333. gallery_event::$function($args[0], $args[1]);
  334. break;
  335. case 3:
  336. gallery_event::$function($args[0], $args[1], $args[2]);
  337. break;
  338. case 4: // Context menu events have 4 arguments so lets optimize them
  339. gallery_event::$function($args[0], $args[1], $args[2], $args[3]);
  340. break;
  341. default:
  342. call_user_func_array(array("gallery_event", $function), $args);
  343. }
  344. }
  345. foreach (self::$active as $module) {
  346. if ($module->name == "gallery") {
  347. continue;
  348. }
  349. $class = "{$module->name}_event";
  350. if (method_exists($class, $function)) {
  351. call_user_func_array(array($class, $function), $args);
  352. }
  353. }
  354. // Give the admin theme a chance to respond, if we're in admin mode.
  355. if (theme::$is_admin) {
  356. $class = theme::$admin_theme_name . "_event";
  357. if (method_exists($class, $function)) {
  358. call_user_func_array(array($class, $function), $args);
  359. }
  360. }
  361. // Give the site theme a chance to respond as well. It gets a chance even in admin mode, as
  362. // long as the theme has an admin subdir.
  363. $class = theme::$site_theme_name . "_event";
  364. if (method_exists($class, $function)) {
  365. call_user_func_array(array($class, $function), $args);
  366. }
  367. }
  368. /**
  369. * Get a variable from this module
  370. * @param string $module_name
  371. * @param string $name
  372. * @param string $default_value
  373. * @return the value
  374. */
  375. static function get_var($module_name, $name, $default_value=null) {
  376. // We cache vars so we can load them all at once for performance.
  377. if (empty(self::$var_cache)) {
  378. self::$var_cache = Cache::instance()->get("var_cache");
  379. if (empty(self::$var_cache)) {
  380. // Cache doesn't exist, create it now.
  381. foreach (db::build()
  382. ->select("module_name", "name", "value")
  383. ->from("vars")
  384. ->order_by("module_name")
  385. ->order_by("name")
  386. ->execute() as $row) {
  387. // Mute the "Creating default object from empty value" warning below
  388. @self::$var_cache->{$row->module_name}->{$row->name} = $row->value;
  389. }
  390. Cache::instance()->set("var_cache", self::$var_cache, array("vars"));
  391. }
  392. }
  393. if (isset(self::$var_cache->$module_name->$name)) {
  394. return self::$var_cache->$module_name->$name;
  395. } else {
  396. return $default_value;
  397. }
  398. }
  399. /**
  400. * Store a variable for this module
  401. * @param string $module_name
  402. * @param string $name
  403. * @param string $value
  404. */
  405. static function set_var($module_name, $name, $value) {
  406. $var = ORM::factory("var")
  407. ->where("module_name", "=", $module_name)
  408. ->where("name", "=", $name)
  409. ->find();
  410. if (!$var->loaded()) {
  411. $var->module_name = $module_name;
  412. $var->name = $name;
  413. }
  414. $var->value = $value;
  415. $var->save();
  416. Cache::instance()->delete("var_cache");
  417. self::$var_cache = null;
  418. }
  419. /**
  420. * Increment the value of a variable for this module
  421. *
  422. * Note: Frequently updating counters is very inefficient because it invalidates the cache value
  423. * which has to be rebuilt every time we make a change.
  424. *
  425. * @todo Get rid of this and find an alternate approach for all callers (currently only Akismet)
  426. *
  427. * @deprecated
  428. * @param string $module_name
  429. * @param string $name
  430. * @param string $increment (optional, default is 1)
  431. */
  432. static function incr_var($module_name, $name, $increment=1) {
  433. db::build()
  434. ->update("vars")
  435. ->set("value", db::expr("`value` + $increment"))
  436. ->where("module_name", "=", $module_name)
  437. ->where("name", "=", $name)
  438. ->execute();
  439. Cache::instance()->delete("var_cache");
  440. self::$var_cache = null;
  441. }
  442. /**
  443. * Remove a variable for this module.
  444. * @param string $module_name
  445. * @param string $name
  446. */
  447. static function clear_var($module_name, $name) {
  448. db::build()
  449. ->delete("vars")
  450. ->where("module_name", "=", $module_name)
  451. ->where("name", "=", $name)
  452. ->execute();
  453. Cache::instance()->delete("var_cache");
  454. self::$var_cache = null;
  455. }
  456. /**
  457. * Remove all variables for this module.
  458. * @param string $module_name
  459. */
  460. static function clear_all_vars($module_name) {
  461. db::build()
  462. ->delete("vars")
  463. ->where("module_name", "=", $module_name)
  464. ->execute();
  465. Cache::instance()->delete("var_cache");
  466. self::$var_cache = null;
  467. }
  468. /**
  469. * Return the version of the installed module.
  470. * @param string $module_name
  471. */
  472. static function get_version($module_name) {
  473. return module::get($module_name)->version;
  474. }
  475. }