PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-content/plugins/nextgen-gallery/non_pope/class.photocrati_installer.php

https://github.com/Fishgate/privatecollectionswp
PHP | 233 lines | 147 code | 35 blank | 51 comment | 22 complexity | 1dd45a5dbce780c480f48aef2a09ce54 MD5 | raw file
  1. <?php
  2. if (!class_exists('C_Photocrati_Installer'))
  3. {
  4. class C_Photocrati_Installer
  5. {
  6. static $_instance = NULL;
  7. static function get_instance()
  8. {
  9. if (is_null(self::$_instance)) {
  10. $klass = get_class();
  11. self::$_instance = new $klass();
  12. }
  13. return self::$_instance;
  14. }
  15. /**
  16. * Each product and module will register it's own handler (a class, with an install() and uninstall() method)
  17. * to be used for install/uninstall routines
  18. * @param $name
  19. * @param $handler
  20. */
  21. static function add_handler($name, $handler)
  22. {
  23. self::get_instance()->_installers[$name] = $handler;
  24. }
  25. /**
  26. * Gets an instance of an installation handler
  27. * @param $name
  28. * @return mixed
  29. */
  30. static function get_handler_instance($name)
  31. {
  32. $installers = $handler = self::get_instance()->_installers;
  33. if (isset($installers[$name])) {
  34. $klass = $installers[$name];
  35. return new $klass;
  36. }
  37. else return NULL;
  38. }
  39. /**
  40. * Uninstalls a product
  41. * @param $product
  42. * @param bool $hard
  43. * @return mixed
  44. */
  45. static function uninstall($product, $hard=FALSE)
  46. {
  47. $handler = self::get_handler_instance($product);
  48. if (method_exists($handler, 'uninstall')) return $handler->uninstall($hard);
  49. if ($hard) {
  50. C_NextGen_Settings::get_instance()->destroy();
  51. C_NextGen_Global_Settings::get_instance()->destroy();
  52. }
  53. }
  54. static function can_do_upgrade()
  55. {
  56. $proceed = FALSE;
  57. // Proceed if no other process has started the installer routines
  58. if (!($doing_upgrade = get_option('ngg_doing_upgrade', FALSE))) {
  59. update_option('ngg_doing_upgrade', time());
  60. $proceed = TRUE;
  61. }
  62. // Or, force proceeding if we have a stale ngg_doing_upgrade record
  63. elseif ($doing_upgrade === TRUE OR time() - $doing_upgrade > 120) {
  64. update_option('ngg_doing_upgrade', time());
  65. $proceed = TRUE;
  66. }
  67. return $proceed;
  68. }
  69. static function done_upgrade()
  70. {
  71. update_option('ngg_doing_upgrade', FALSE);
  72. }
  73. static function update($reset=FALSE)
  74. {
  75. $local_settings = C_NextGen_Settings::get_instance();
  76. $global_settings = C_NextGen_Global_Settings::get_instance();
  77. // This is a specific hack/work-around/fix and can probably be removed sometime after 2.0.20's release
  78. //
  79. // NextGen 2x was not multisite compatible until 2.0.18. Users that upgraded before this
  80. // will have nearly all of their settings stored globally (network wide) in wp_sitemeta. If
  81. // pope_module_list (which should always be a local setting) exists site-wide we wipe the current
  82. // global ngg_options and restore from defaults. This should only ever run once.
  83. if (is_multisite() && isset($global_settings->pope_module_list))
  84. {
  85. // Setting this to TRUE will wipe current settings for display types, but also
  86. // allows the display type installer to run correctly
  87. $reset = TRUE;
  88. $settings_installer = new C_NextGen_Settings_Installer();
  89. $global_defaults = $settings_installer->get_global_defaults();
  90. // Preserve the network options we honor by restoring them after calling $global_settings->reset()
  91. $global_settings_to_keep = array();
  92. foreach ($global_defaults as $key => $val) {
  93. $global_settings_to_keep[$key] = $global_settings->$key;
  94. }
  95. // Resets internal options to an empty array
  96. $global_settings->reset();
  97. // Restore the defaults, then our saved values. This must be done again later because
  98. // we've set $reset to TRUE.
  99. $settings_installer->install_global_settings();
  100. foreach ($global_settings_to_keep as $key => $val) {
  101. $global_settings->$key = $val;
  102. }
  103. }
  104. $last_module_list = self::_get_last_module_list($reset);
  105. $current_module_list = self::_generate_module_info();
  106. if (count(($modules = array_diff($current_module_list, $last_module_list))) > 0 && self::can_do_upgrade())
  107. {
  108. // Clear APC cache
  109. if (function_exists('apc_clear_cache')) {
  110. @apc_clear_cache('opcode');
  111. apc_clear_cache();
  112. }
  113. // The cache should be flushed
  114. C_Photocrati_Cache::flush('all');
  115. // Remove all NGG created cron jobs
  116. self::refresh_cron();
  117. // Delete auto-update cache
  118. update_option('photocrati_auto_update_admin_update_list', null);
  119. update_option('photocrati_auto_update_admin_check_date', '');
  120. // Other Pope applications might be loaded, and therefore
  121. // all singletons should be destroyed, so that they can be
  122. // adapted as necessary. For now, we'll just assume that the factory
  123. // is the only singleton that will be used by other Pope applications
  124. C_Component_Factory::$_instances = array();
  125. foreach ($modules as $module_name) {
  126. if (($handler = self::get_handler_instance(array_shift(explode('|', $module_name))))) {
  127. if (method_exists($handler, 'install'))
  128. $handler->install($reset);
  129. }
  130. }
  131. // Update the module list
  132. update_option('pope_module_list', $current_module_list);
  133. // NOTE & TODO: if the above section that declares $global_settings_to_keep is removed this should also
  134. // Since a hard-reset of the settings was forced we must again re-apply our previously saved values
  135. if (isset($global_settings_to_keep)) {
  136. foreach ($global_settings_to_keep as $key => $val) {
  137. $global_settings->$key = $val;
  138. }
  139. }
  140. // Save any changes settings
  141. $global_settings->save();
  142. $local_settings->save();
  143. self::done_upgrade();
  144. }
  145. // Another workaround to an issue caused by NextGen's lack of multisite compatibility. It's possible
  146. // the string substitation wasn't performed, so if a '%' symbol exists in gallerypath we reset it. It's
  147. // another db call, but again this should only ever run once.
  148. //
  149. // Remove this when removing the above reset-global-settings code
  150. if (strpos($local_settings->gallerypath, '%'))
  151. {
  152. $settings_installer = new C_NextGen_Settings_Installer();
  153. $local_settings->gallerypath = $settings_installer->gallerypath_replace($global_settings->gallerypath);
  154. $local_settings->save();
  155. }
  156. }
  157. static function _get_last_module_list($reset=FALSE)
  158. {
  159. // Return empty array to reset
  160. if ($reset) return array();
  161. // First try getting the list from a single WP option, "pope_module_list"
  162. $retval = get_option('pope_module_list', array());
  163. if (!$retval) {
  164. $local_settings = C_NextGen_Settings::get_instance();
  165. $retval = $local_settings->get('pope_module_list', array());
  166. $local_settings->delete('pope_module_list');
  167. }
  168. return $retval;
  169. }
  170. static function _generate_module_info()
  171. {
  172. $retval = array();
  173. $registry = C_Component_Registry::get_instance();
  174. foreach ($registry->get_module_list() as $module_id) {
  175. $module_version = $registry->get_module($module_id)->module_version;
  176. $retval[] = "{$module_id}|{$module_version}";
  177. }
  178. return $retval;
  179. }
  180. static function refresh_cron()
  181. {
  182. if (!extension_loaded('suhosin')) @ini_set('memory_limit', -1);
  183. // Remove all cron jobs created by NextGEN Gallery
  184. $cron = _get_cron_array();
  185. if (is_array($cron)) {
  186. foreach ($cron as $timestamp => $job) {
  187. if (is_array($job)) {
  188. unset($cron[$timestamp]['ngg_delete_expired_transients']);
  189. if (empty($cron[$timestamp])) {
  190. unset($cron[$timestamp]);
  191. }
  192. }
  193. }
  194. }
  195. _set_cron_array($cron);
  196. }
  197. }
  198. }