PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/gallery/helpers/module.php

http://github.com/gallery/gallery3
PHP | 598 lines | 354 code | 71 blank | 173 comment | 58 complexity | 46111247dec18623654667ee61edb712 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-2013 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. foreach ($modules->$module_name as &$value) {
  90. $value = html::purify($value);
  91. }
  92. $m =& $modules->$module_name;
  93. $m->installed = module::is_installed($module_name);
  94. $m->active = module::is_active($module_name);
  95. $m->code_version = $m->version;
  96. $m->version = module::get_version($module_name);
  97. $m->locked = false;
  98. if ($m->active && $m->version != $m->code_version) {
  99. site_status::warning(t("Some of your modules are out of date. <a href=\"%upgrader_url\">Upgrade now!</a>", array("upgrader_url" => url::abs_site("upgrader"))), "upgrade_now");
  100. }
  101. }
  102. // Lock certain modules
  103. $modules->gallery->locked = true;
  104. $identity_module = module::get_var("gallery", "identity_provider", "user");
  105. $modules->$identity_module->locked = true;
  106. $modules->uasort(array("module", "module_comparator"));
  107. self::$available = $modules;
  108. }
  109. return self::$available;
  110. }
  111. /**
  112. * Natural name sort comparator
  113. */
  114. static function module_comparator($a, $b) {
  115. return strnatcasecmp($a->name, $b->name);
  116. }
  117. /**
  118. * Return a list of all the active modules in no particular order.
  119. */
  120. static function active() {
  121. return self::$active;
  122. }
  123. /**
  124. * Check that the module can be activated. (i.e. all the prerequistes exist)
  125. * @param string $module_name
  126. * @return array an array of warning or error messages to be displayed
  127. */
  128. static function can_activate($module_name) {
  129. module::_add_to_path($module_name);
  130. $messages = array();
  131. $installer_class = "{$module_name}_installer";
  132. if (class_exists($installer_class) && method_exists($installer_class, "can_activate")) {
  133. $messages = call_user_func(array($installer_class, "can_activate"));
  134. }
  135. // Remove it from the active path
  136. module::_remove_from_path($module_name);
  137. return $messages;
  138. }
  139. /**
  140. * Allow modules to indicate the impact of deactivating the specified module
  141. * @param string $module_name
  142. * @return array an array of warning or error messages to be displayed
  143. */
  144. static function can_deactivate($module_name) {
  145. $data = (object)array("module" => $module_name, "messages" => array());
  146. module::event("pre_deactivate", $data);
  147. return $data->messages;
  148. }
  149. /**
  150. * Install a module. This will call <module>_installer::install(), which is responsible for
  151. * creating database tables, setting module variables and calling module::set_version().
  152. * Note that after installing, the module must be activated before it is available for use.
  153. * @param string $module_name
  154. */
  155. static function install($module_name) {
  156. module::_add_to_path($module_name);
  157. $installer_class = "{$module_name}_installer";
  158. if (class_exists($installer_class) && method_exists($installer_class, "install")) {
  159. call_user_func_array(array($installer_class, "install"), array());
  160. }
  161. module::set_version($module_name, module::available()->$module_name->code_version);
  162. // Set the weight of the new module, which controls the order in which the modules are
  163. // loaded. By default, new modules are installed at the end of the priority list. Since the
  164. // id field is monotonically increasing, the easiest way to guarantee that is to set the weight
  165. // the same as the id. We don't know that until we save it for the first time
  166. $module = ORM::factory("module")->where("name", "=", $module_name)->find();
  167. if ($module->loaded()) {
  168. $module->weight = $module->id;
  169. $module->save();
  170. }
  171. module::load_modules();
  172. // Now the module is installed but inactive, so don't leave it in the active path
  173. module::_remove_from_path($module_name);
  174. log::success(
  175. "module", t("Installed module %module_name", array("module_name" => $module_name)));
  176. }
  177. private static function _add_to_path($module_name) {
  178. $config = Kohana_Config::instance();
  179. $kohana_modules = $config->get("core.modules");
  180. array_unshift($kohana_modules, MODPATH . $module_name);
  181. $config->set("core.modules", $kohana_modules);
  182. // Rebuild the include path so the module installer can benefit from auto loading
  183. Kohana::include_paths(true);
  184. }
  185. private static function _remove_from_path($module_name) {
  186. $config = Kohana_Config::instance();
  187. $kohana_modules = $config->get("core.modules");
  188. if (($key = array_search(MODPATH . $module_name, $kohana_modules)) !== false) {
  189. unset($kohana_modules[$key]);
  190. $kohana_modules = array_values($kohana_modules); // reindex
  191. }
  192. $config->set("core.modules", $kohana_modules);
  193. Kohana::include_paths(true);
  194. }
  195. /**
  196. * Upgrade a module. This will call <module>_installer::upgrade(), which is responsible for
  197. * modifying database tables, changing module variables and calling module::set_version().
  198. * Note that after upgrading, the module must be activated before it is available for use.
  199. * @param string $module_name
  200. */
  201. static function upgrade($module_name) {
  202. $version_before = module::get_version($module_name);
  203. $installer_class = "{$module_name}_installer";
  204. $available = module::available();
  205. if (class_exists($installer_class) && method_exists($installer_class, "upgrade")) {
  206. call_user_func_array(array($installer_class, "upgrade"), array($version_before));
  207. } else {
  208. if (isset($available->$module_name->code_version)) {
  209. module::set_version($module_name, $available->$module_name->code_version);
  210. } else {
  211. throw new Exception("@todo UNKNOWN_MODULE");
  212. }
  213. }
  214. module::load_modules();
  215. $version_after = module::get_version($module_name);
  216. if ($version_before != $version_after) {
  217. log::success(
  218. "module", t("Upgraded module %module_name from %version_before to %version_after",
  219. array("module_name" => $module_name,
  220. "version_before" => $version_before,
  221. "version_after" => $version_after)));
  222. }
  223. if ($version_after != $available->$module_name->code_version) {
  224. throw new Exception("@todo MODULE_FAILED_TO_UPGRADE");
  225. }
  226. }
  227. /**
  228. * Activate an installed module. This will call <module>_installer::activate() which should take
  229. * any steps to make sure that the module is ready for use. This will also activate any
  230. * existing graphics rules for this module.
  231. * @param string $module_name
  232. */
  233. static function activate($module_name) {
  234. module::_add_to_path($module_name);
  235. $installer_class = "{$module_name}_installer";
  236. if (class_exists($installer_class) && method_exists($installer_class, "activate")) {
  237. call_user_func_array(array($installer_class, "activate"), array());
  238. }
  239. $module = module::get($module_name);
  240. if ($module->loaded()) {
  241. $module->active = true;
  242. $module->save();
  243. }
  244. module::load_modules();
  245. graphics::activate_rules($module_name);
  246. block_manager::activate_blocks($module_name);
  247. log::success(
  248. "module", t("Activated module %module_name", array("module_name" => $module_name)));
  249. }
  250. /**
  251. * Deactivate an installed module. This will call <module>_installer::deactivate() which should
  252. * take any cleanup steps to make sure that the module isn't visible in any way. Note that the
  253. * module remains available in Kohana's cascading file system until the end of the request!
  254. * @param string $module_name
  255. */
  256. static function deactivate($module_name) {
  257. $installer_class = "{$module_name}_installer";
  258. if (class_exists($installer_class) && method_exists($installer_class, "deactivate")) {
  259. call_user_func_array(array($installer_class, "deactivate"), array());
  260. }
  261. $module = module::get($module_name);
  262. if ($module->loaded()) {
  263. $module->active = false;
  264. $module->save();
  265. }
  266. module::load_modules();
  267. graphics::deactivate_rules($module_name);
  268. block_manager::deactivate_blocks($module_name);
  269. if (module::info($module_name)) {
  270. log::success(
  271. "module", t("Deactivated module %module_name", array("module_name" => $module_name)));
  272. } else {
  273. log::success(
  274. "module", t("Deactivated missing module %module_name", array("module_name" => $module_name)));
  275. }
  276. }
  277. /**
  278. * Deactivate modules that are unavailable or missing, yet still active.
  279. * This happens when a user deletes a module without deactivating it.
  280. */
  281. static function deactivate_missing_modules() {
  282. foreach (self::$modules as $module_name => $module) {
  283. if (module::is_active($module_name) && !module::info($module_name)) {
  284. module::deactivate($module_name);
  285. }
  286. }
  287. }
  288. /**
  289. * Uninstall a deactivated module. This will call <module>_installer::uninstall() which should
  290. * take whatever steps necessary to make sure that all traces of a module are gone.
  291. * @param string $module_name
  292. */
  293. static function uninstall($module_name) {
  294. $installer_class = "{$module_name}_installer";
  295. if (class_exists($installer_class) && method_exists($installer_class, "uninstall")) {
  296. call_user_func(array($installer_class, "uninstall"));
  297. }
  298. graphics::remove_rules($module_name);
  299. $module = module::get($module_name);
  300. if ($module->loaded()) {
  301. $module->delete();
  302. }
  303. module::load_modules();
  304. // We could delete the module vars here too, but it's nice to leave them around
  305. // in case the module gets reinstalled.
  306. log::success(
  307. "module", t("Uninstalled module %module_name", array("module_name" => $module_name)));
  308. }
  309. /**
  310. * Load the active modules. This is called at bootstrap time.
  311. */
  312. static function load_modules() {
  313. self::$modules = array();
  314. self::$active = array();
  315. $kohana_modules = array();
  316. // In version 32 we introduced a weight column so we can specify the module order
  317. // If we try to use that blindly, we'll break earlier versions before they can even
  318. // run the upgrader.
  319. $modules = module::get_version("gallery") < 32 ?
  320. ORM::factory("module")->find_all():
  321. ORM::factory("module")->order_by("weight")->find_all();
  322. foreach ($modules as $module) {
  323. self::$modules[$module->name] = $module;
  324. if (!$module->active) {
  325. continue;
  326. }
  327. if ($module->name == "gallery") {
  328. $gallery = $module;
  329. } else {
  330. self::$active[] = $module;
  331. $kohana_modules[] = MODPATH . $module->name;
  332. }
  333. }
  334. self::$active[] = $gallery; // put gallery last in the module list to match core.modules
  335. $config = Kohana_Config::instance();
  336. $config->set(
  337. "core.modules", array_merge($kohana_modules, $config->get("core.modules")));
  338. }
  339. /**
  340. * Run a specific event on all active modules.
  341. * @param string $name the event name
  342. * @param mixed $data data to pass to each event handler
  343. */
  344. static function event($name, &$data=null) {
  345. $args = func_get_args();
  346. array_shift($args);
  347. $function = str_replace(".", "_", $name);
  348. if (method_exists("gallery_event", $function)) {
  349. switch (count($args)) {
  350. case 0:
  351. gallery_event::$function();
  352. break;
  353. case 1:
  354. gallery_event::$function($args[0]);
  355. break;
  356. case 2:
  357. gallery_event::$function($args[0], $args[1]);
  358. break;
  359. case 3:
  360. gallery_event::$function($args[0], $args[1], $args[2]);
  361. break;
  362. case 4: // Context menu events have 4 arguments so lets optimize them
  363. gallery_event::$function($args[0], $args[1], $args[2], $args[3]);
  364. break;
  365. default:
  366. call_user_func_array(array("gallery_event", $function), $args);
  367. }
  368. }
  369. foreach (self::$active as $module) {
  370. if ($module->name == "gallery") {
  371. continue;
  372. }
  373. $class = "{$module->name}_event";
  374. if (class_exists($class) && method_exists($class, $function)) {
  375. call_user_func_array(array($class, $function), $args);
  376. }
  377. }
  378. // Give the admin theme a chance to respond, if we're in admin mode.
  379. if (theme::$is_admin) {
  380. $class = theme::$admin_theme_name . "_event";
  381. if (class_exists($class) && method_exists($class, $function)) {
  382. call_user_func_array(array($class, $function), $args);
  383. }
  384. }
  385. // Give the site theme a chance to respond as well. It gets a chance even in admin mode, as
  386. // long as the theme has an admin subdir.
  387. $class = theme::$site_theme_name . "_event";
  388. if (class_exists($class) && method_exists($class, $function)) {
  389. call_user_func_array(array($class, $function), $args);
  390. }
  391. }
  392. /**
  393. * Get a variable from this module
  394. * @param string $module_name
  395. * @param string $name
  396. * @param string $default_value
  397. * @return the value
  398. */
  399. static function get_var($module_name, $name, $default_value=null) {
  400. // We cache vars so we can load them all at once for performance.
  401. if (empty(self::$var_cache)) {
  402. self::$var_cache = Cache::instance()->get("var_cache");
  403. if (empty(self::$var_cache)) {
  404. // Cache doesn't exist, create it now.
  405. foreach (db::build()
  406. ->select("module_name", "name", "value")
  407. ->from("vars")
  408. ->order_by("module_name")
  409. ->order_by("name")
  410. ->execute() as $row) {
  411. // Mute the "Creating default object from empty value" warning below
  412. @self::$var_cache->{$row->module_name}->{$row->name} = $row->value;
  413. }
  414. Cache::instance()->set("var_cache", self::$var_cache, array("vars"));
  415. }
  416. }
  417. if (isset(self::$var_cache->$module_name->$name)) {
  418. return self::$var_cache->$module_name->$name;
  419. } else {
  420. return $default_value;
  421. }
  422. }
  423. /**
  424. * Store a variable for this module
  425. * @param string $module_name
  426. * @param string $name
  427. * @param string $value
  428. */
  429. static function set_var($module_name, $name, $value) {
  430. $var = ORM::factory("var")
  431. ->where("module_name", "=", $module_name)
  432. ->where("name", "=", $name)
  433. ->find();
  434. if (!$var->loaded()) {
  435. $var->module_name = $module_name;
  436. $var->name = $name;
  437. }
  438. $var->value = $value;
  439. $var->save();
  440. Cache::instance()->delete("var_cache");
  441. self::$var_cache = null;
  442. }
  443. /**
  444. * Increment the value of a variable for this module
  445. *
  446. * Note: Frequently updating counters is very inefficient because it invalidates the cache value
  447. * which has to be rebuilt every time we make a change.
  448. *
  449. * @todo Get rid of this and find an alternate approach for all callers (currently only Akismet)
  450. *
  451. * @deprecated
  452. * @param string $module_name
  453. * @param string $name
  454. * @param string $increment (optional, default is 1)
  455. */
  456. static function incr_var($module_name, $name, $increment=1) {
  457. db::build()
  458. ->update("vars")
  459. ->set("value", db::expr("`value` + $increment"))
  460. ->where("module_name", "=", $module_name)
  461. ->where("name", "=", $name)
  462. ->execute();
  463. Cache::instance()->delete("var_cache");
  464. self::$var_cache = null;
  465. }
  466. /**
  467. * Remove a variable for this module.
  468. * @param string $module_name
  469. * @param string $name
  470. */
  471. static function clear_var($module_name, $name) {
  472. db::build()
  473. ->delete("vars")
  474. ->where("module_name", "=", $module_name)
  475. ->where("name", "=", $name)
  476. ->execute();
  477. Cache::instance()->delete("var_cache");
  478. self::$var_cache = null;
  479. }
  480. /**
  481. * Remove all variables for this module.
  482. * @param string $module_name
  483. */
  484. static function clear_all_vars($module_name) {
  485. db::build()
  486. ->delete("vars")
  487. ->where("module_name", "=", $module_name)
  488. ->execute();
  489. Cache::instance()->delete("var_cache");
  490. self::$var_cache = null;
  491. }
  492. /**
  493. * Return the version of the installed module.
  494. * @param string $module_name
  495. */
  496. static function get_version($module_name) {
  497. return module::get($module_name)->version;
  498. }
  499. /**
  500. * Check if obsolete modules are active and, if so, return a warning message.
  501. * If none are found, return null.
  502. */
  503. static function get_obsolete_modules_message() {
  504. // This is the obsolete modules list. Any active module that's on the list
  505. // with version number at or below the one given will be considered obsolete.
  506. // It is hard-coded here, and may be updated with future releases of Gallery.
  507. $obsolete_modules = array("videos" => 4, "noffmpeg" => 1, "videodimensions" => 1,
  508. "digibug" => 2);
  509. // Before we check the active modules, deactivate any that are missing.
  510. module::deactivate_missing_modules();
  511. $modules_found = array();
  512. foreach ($obsolete_modules as $module => $version) {
  513. if (module::is_active($module) && (module::get_version($module) <= $version)) {
  514. $modules_found[] = $module;
  515. }
  516. }
  517. if ($modules_found) {
  518. // Need this to be on one super-long line or else the localization scanner may not work.
  519. // (ref: http://sourceforge.net/apps/trac/gallery/ticket/1321)
  520. return t("Recent upgrades to Gallery have made the following modules obsolete: %modules. We recommend that you <a href=\"%url_mod\">deactivate</a> the module(s). For more information, please see the <a href=\"%url_doc\">documentation page</a>.",
  521. array("modules" => implode(", ", $modules_found),
  522. "url_mod" => url::site("admin/modules"),
  523. "url_doc" => "http://codex.galleryproject.org/Gallery3:User_guide:Obsolete_modules"));
  524. }
  525. return null;
  526. }
  527. }