/wp-content/plugins/nextgen-gallery/non_pope/class.nextgen_style_manager.php

https://github.com/livinglab/openlab · PHP · 302 lines · 195 code · 50 blank · 57 comment · 24 complexity · c9b6d2611704def978a592c53f925f6b MD5 · raw file

  1. <?php
  2. class C_NextGen_Style_Manager
  3. {
  4. static $_instance = NULL;
  5. var $directories = array();
  6. var $unsafe_directories = array();
  7. var $default_dir = '';
  8. var $new_dir = '';
  9. function __construct()
  10. {
  11. $this->default_dir = implode(DIRECTORY_SEPARATOR, array(
  12. NGG_MODULE_DIR,
  13. 'ngglegacy',
  14. 'css'
  15. ));
  16. $this->new_dir = implode(DIRECTORY_SEPARATOR, array(
  17. rtrim(WP_CONTENT_DIR, "/\\"),
  18. 'ngg_styles'
  19. ));
  20. // The last place we look for a stylesheet is in ngglegacy
  21. $this->add_directory($this->default_dir);
  22. // This is where all stylesheets should be stored
  23. $this->add_directory($this->new_dir);
  24. // We also check wp-content/ngg/styles
  25. $this->add_directory(implode(DIRECTORY_SEPARATOR, array(
  26. WP_CONTENT_DIR, 'ngg', 'styles'
  27. )));
  28. // We check the parent theme directory. Needed for child themes
  29. $this->add_directory(rtrim(get_template_directory(), "/\\"), TRUE);
  30. // We also check parent_theme/nggallery
  31. $this->add_directory(implode(DIRECTORY_SEPARATOR, array(
  32. rtrim(get_template_directory(), "/\\"),
  33. 'nggallery'
  34. )), TRUE);
  35. // We also check parent_theme/ngg_styles
  36. $this->add_directory(implode(DIRECTORY_SEPARATOR, array(
  37. rtrim(get_template_directory(), "/\\"),
  38. 'ngg_styles'
  39. )), TRUE);
  40. // We check the root directory of the theme. Users shouldn't store here,
  41. // but they might
  42. $this->add_directory(rtrim(get_stylesheet_directory(), "/\\"), TRUE);
  43. // We also check the theme/nggallery directory
  44. $this->add_directory(implode(DIRECTORY_SEPARATOR, array(
  45. rtrim(get_stylesheet_directory(), "/\\"),
  46. 'nggallery'
  47. )), TRUE);
  48. // We also check the theme/ngg_styles directory
  49. $this->add_directory(implode(DIRECTORY_SEPARATOR, array(
  50. rtrim(get_stylesheet_directory(), "/\\"),
  51. 'ngg_styles'
  52. )), TRUE);
  53. }
  54. /**
  55. * Add a directory to search for stylesheets
  56. * @param $dir
  57. * @param bool $unsafe
  58. */
  59. function add_directory($dir, $unsafe=FALSE)
  60. {
  61. array_unshift($this->directories, $dir);
  62. if ($unsafe) {
  63. $this->unsafe_directories[] = $dir;
  64. }
  65. }
  66. /**
  67. * Determines if a directory is upgrade-safe or not
  68. * @param $dir
  69. * @return bool
  70. */
  71. function is_directory_unsafe($dir=FALSE)
  72. {
  73. if (!$dir) $dir = dirname($this->find_selected_stylesheet_abspath());
  74. return in_array($dir, $this->unsafe_directories);
  75. }
  76. /**
  77. * Determines if the directory is the default ngglegacy path
  78. * @param $dir
  79. * @return bool
  80. */
  81. function is_default_dir($dir)
  82. {
  83. return rtrim($dir, "/\\") == $this->default_dir;
  84. }
  85. function get_new_dir($filename)
  86. {
  87. return implode(DIRECTORY_SEPARATOR, array(
  88. rtrim($this->new_dir, "/\\"),
  89. $filename
  90. ));
  91. }
  92. /**
  93. * Gets the location where the selected stylesheet will be saved to
  94. * @param bool|string $selected
  95. * @return string
  96. */
  97. function get_selected_stylesheet_saved_abspath($selected=FALSE)
  98. {
  99. if (!$selected) $selected = $this->get_selected_stylesheet();
  100. $abspath = $this->find_selected_stylesheet_abspath($selected);
  101. if ($this->is_default_dir(dirname($abspath))) {
  102. $abspath = $this->get_new_dir(basename($abspath));
  103. }
  104. return $abspath;
  105. }
  106. function save($contents, $selected=FALSE)
  107. {
  108. $retval = FALSE;
  109. if (!$selected) $selected = $this->get_selected_stylesheet();
  110. $abspath = $this->get_selected_stylesheet_saved_abspath($selected);
  111. wp_mkdir_p(dirname($abspath));
  112. if (is_writable($abspath) OR (!@file_exists($abspath) && is_writable(dirname($abspath)))) {
  113. $retval = file_put_contents($abspath, $contents);
  114. }
  115. return $retval;
  116. }
  117. /**
  118. * Gets the selected stylesheet from the user
  119. * @return mixed
  120. */
  121. function get_selected_stylesheet()
  122. {
  123. $settings = C_NextGen_Settings::get_instance();
  124. // use the same css resource for all subsites when wpmuStyle=true
  125. if (!is_multisite() || (is_multisite() && $settings->get('wpmuStyle')))
  126. return $settings->get('CSSfile', 'nggallery.css');
  127. else
  128. return C_Nextgen_Global_Settings::get_instance()->get('wpmuCSSfile');
  129. }
  130. /**
  131. * Finds the location of the selected stylesheet
  132. */
  133. function find_selected_stylesheet_abspath($selected=FALSE)
  134. {
  135. if (!$selected) $selected = $this->get_selected_stylesheet();
  136. $retval = implode(DIRECTORY_SEPARATOR, array(
  137. rtrim($this->default_dir, "/\\"),
  138. $selected
  139. ));
  140. foreach ($this->directories as $dir) {
  141. $path = implode(DIRECTORY_SEPARATOR, array(
  142. rtrim($dir, "/\\"),
  143. $selected
  144. ));
  145. if (@file_exists($path)) {
  146. $retval = $path;
  147. break;
  148. }
  149. }
  150. $retval = str_replace('/', DIRECTORY_SEPARATOR, $retval);
  151. return $retval;
  152. }
  153. /**
  154. * Returns the url to the selected stylesheet
  155. * @return mixed
  156. */
  157. function get_selected_stylesheet_url($selected=FALSE)
  158. {
  159. if (!$selected)
  160. $selected = $this->get_selected_stylesheet();
  161. $abspath = $this->find_selected_stylesheet_abspath($selected);
  162. // default_dir is the only resource loaded from inside the plugin directory
  163. $type = 'content';
  164. $url = content_url();
  165. if (0 === strpos($abspath, $this->default_dir))
  166. {
  167. $type = 'plugins';
  168. $url = plugins_url();
  169. }
  170. // Credit to Sam Soysa for this line -- Windows servers have so many special needs.
  171. $abspath = str_replace('\\', '/', $abspath);
  172. $retval = str_replace(
  173. C_Fs::get_instance()->get_document_root($type),
  174. $url,
  175. $abspath
  176. );
  177. return rtrim(str_replace('\\', '/', $retval), "/");
  178. }
  179. function find_all_stylesheets($dir = FALSE)
  180. {
  181. $retval = array();
  182. if (!$dir)
  183. $dir = $this->directories;
  184. foreach (array_reverse($dir) as $dir) {
  185. $path = implode(DIRECTORY_SEPARATOR, array(
  186. rtrim($dir, "/\\"),
  187. '*.css'
  188. ));
  189. $files = glob($path);
  190. if (is_array($files)) foreach ($files as $abspath) {
  191. if (($meta = $this->get_stylesheet_metadata($abspath))) {
  192. $filename = $meta['filename'];
  193. $retval[$filename] = $meta;
  194. }
  195. }
  196. }
  197. return $retval;
  198. }
  199. /**
  200. * Gets the metadata for a particular stylesheet
  201. * @param $abspath
  202. * @return array
  203. */
  204. function get_stylesheet_metadata($abspath)
  205. {
  206. $retval = array();
  207. $contents = file_get_contents($abspath);
  208. $name = '';
  209. $desc = '';
  210. $version = '';
  211. $author = '';
  212. // Find the name of the stylesheet
  213. if (preg_match("/CSS Name:(.*)/i", $contents, $match)) {
  214. $name = trim($match[1]);
  215. }
  216. // Find the description of the stylesheet
  217. if (preg_match("/Description:(.*)/", $contents, $match)) {
  218. $desc = trim($match[1]);
  219. }
  220. // Find the author of the stylesheet
  221. if (preg_match("/Author:(.*)/", $contents, $match)) {
  222. $author = trim($match[1]);
  223. }
  224. // Find the version of the stylesheet
  225. if (preg_match("/Version:(.*)/", $contents, $match)) {
  226. $version = trim($match[1]);
  227. }
  228. if ($name) {
  229. $retval = array(
  230. 'filename' => basename($abspath),
  231. 'abspath' => $abspath,
  232. 'name' => $name,
  233. 'description' => $desc,
  234. 'author' => $author,
  235. 'version' => $version
  236. );
  237. }
  238. return $retval;
  239. }
  240. /**
  241. * Gets an instance of the class
  242. * @return C_NextGen_Style_Manager
  243. */
  244. static function get_instance()
  245. {
  246. if (is_null(self::$_instance)){
  247. $klass = get_class();
  248. self::$_instance = new $klass();
  249. }
  250. return self::$_instance;
  251. }
  252. }