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

/admin/includes/settings.php

http://lilina.googlecode.com/
PHP | 302 lines | 171 code | 40 blank | 91 comment | 20 complexity | 45d827c20eab5f85f37960e50bb4b16b MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. * Settings page functions
  4. *
  5. * @package Lilina
  6. * @subpackage Administration
  7. */
  8. /**
  9. * available_locales() - {{@internal Missing Short Description}}}
  10. *
  11. * {{@internal Missing Long Description}}}
  12. */
  13. function available_locales() {
  14. $locale_list = array_map('basename', glob(LILINA_PATH . LANGDIR . '/*.mo'));
  15. $locale_list = apply_filters('locale_files', $locale_list);
  16. $locales = array();
  17. /** Special case for English */
  18. $locales[] = array('name' => 'English',
  19. 'file' => '',
  20. 'realname' => 'en');
  21. foreach($locale_list as $locale) {
  22. $locale = basename($locale, '.mo');
  23. if(file_exists( $locale . '.txt' )) {
  24. $locale_metadata = file_get_contents(LILINA_PATH . LANGDIR . $locale . '.txt');
  25. preg_match("|Name:(.*)|i", $locale_metadata, $name);
  26. $locales[$locale] = array(
  27. 'name' => $name,
  28. 'file' => $locale . '.mo',
  29. 'realname' => $locale
  30. );
  31. }
  32. else {
  33. $locales[$locale] = array(
  34. 'name' => $locale,
  35. 'file' => $locale . '.mo',
  36. 'realname' => $locale
  37. );
  38. }
  39. }
  40. return $locales;
  41. }
  42. /**
  43. * available_templates() - {{@internal Missing Short Description}}}
  44. *
  45. * {{@internal Missing Long Description}}}
  46. */
  47. function available_templates() {
  48. //Make sure we open it correctly
  49. if ($handle = opendir(LILINA_CONTENT_DIR . '/templates/')) {
  50. //Go through all entries
  51. while (false !== ($dir = readdir($handle))) {
  52. // just skip the reference to current and parent directory
  53. if ($dir != '.' && $dir != '..') {
  54. if (is_dir(LILINA_CONTENT_DIR . '/templates/' . $dir)) {
  55. if(file_exists(LILINA_CONTENT_DIR . '/templates/' . $dir . '/style.css')) {
  56. $list[] = $dir;
  57. }
  58. }
  59. }
  60. }
  61. // ALWAYS remember to close what you opened
  62. closedir($handle);
  63. }
  64. foreach($list as $the_template) {
  65. /*$temp_data = implode('', file(LILINA_CONTENT_DIR . '/templates/' . $the_template . '/style.css'));
  66. preg_match("|Name:(.*)|i", $temp_data, $real_name);
  67. preg_match("|Description:(.*)|i", $temp_data, $desc);
  68. $templates[] = array(
  69. 'name' => $the_template,
  70. 'real_name' => trim($real_name[1]),
  71. 'description' => trim($desc[1])
  72. );*/
  73. $meta = template_meta(LILINA_CONTENT_DIR . '/templates/' . $the_template . '/style.css');
  74. $meta->slug = $the_template;
  75. $meta->directory = LILINA_CONTENT_DIR . '/templates/' . $the_template;
  76. $templates[$the_template] = $meta;
  77. }
  78. return $templates;
  79. }
  80. function template_meta($filename) {
  81. $template_data = implode('', file($filename));
  82. $headers = array(
  83. 'name' => 'Name',
  84. 'uri' => 'URI',
  85. 'description' => 'Description',
  86. 'author' => 'Author',
  87. 'author_uri' => 'Author URI',
  88. 'version' => 'Version',
  89. 'min_version' => 'Min Version',
  90. );
  91. $headers = apply_filters('template_headers', $headers);
  92. $vals = array();
  93. foreach ( $headers as $friendly => $regex ) {
  94. $value = null;
  95. $success = preg_match('|' . preg_quote($regex, '|') . ':(.*)|i', $template_data, $value);
  96. if (!$success) {
  97. $value = array(1 => '');
  98. }
  99. $value = trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', $value[1]));
  100. $vals[$friendly] = $value;
  101. }
  102. $defaults = array(
  103. 'name' => 'Unnamed Template',
  104. 'description' => 'This template is missing a description!'
  105. );
  106. $template = (object) $vals;
  107. return $template;
  108. }
  109. /**
  110. * Activate a plugin
  111. *
  112. * @since 1.0
  113. *
  114. * @param string $plugin_file Relative path to plugin
  115. * @return bool Whether plugin was activated
  116. */
  117. function activate_plugin($plugin_file) {
  118. global $current_plugins;
  119. $plugin_file = trim($plugin_file);
  120. try {
  121. validate_plugin($plugin_file);
  122. }
  123. catch (Exception $e) {
  124. return false;
  125. }
  126. $current_plugins[md5($plugin_file)] = $plugin_file;
  127. $data = new DataHandler();
  128. $data->save('plugins.data', serialize($current_plugins));
  129. return true;
  130. }
  131. /**
  132. * Deactivate a plugin
  133. *
  134. * @since 1.0
  135. *
  136. * @param string $plugin_file Relative path to plugin
  137. * @return bool Whether plugin was deactivated
  138. */
  139. function deactivate_plugin($plugin_file) {
  140. global $current_plugins;
  141. if(!isset($current_plugins[md5($plugin_file)]))
  142. return false;
  143. try {
  144. validate_plugin($plugin_file);
  145. }
  146. catch (Exception $e) {
  147. return false;
  148. }
  149. unset($current_plugins[md5($plugin_file)]);
  150. $data = new DataHandler();
  151. $data->save('plugins.data', serialize($current_plugins));
  152. return true;
  153. }
  154. /**
  155. * Register an option for the whitelist
  156. *
  157. * @param string $name Option name
  158. * @param callback $sanitize_callback Callback to sanitize user input.
  159. */
  160. function register_option($name, $sanitize_callback = null) {
  161. AdminOptions::instance()->whitelisted[] = $name;
  162. if ( $sanitize_callback !== null )
  163. add_filter('options-sanitize-' . $name, $sanitize_callback);
  164. }
  165. /**
  166. * Add a section to the options page
  167. *
  168. * @see AdminOptions::add_section()
  169. */
  170. function add_option_section($id, $title, $callback) {
  171. AdminOptions::instance()->add_section($id, $title, $callback);
  172. }
  173. /**
  174. * Add a field to an option section
  175. *
  176. * @see AdminOptions::add_field()
  177. */
  178. function add_option_field($id, $title, $callback, $page, $section = 'default', $args = array()) {
  179. AdminOptions::instance()->add_field($id, $title, $callback, $page, $section, $args);
  180. }
  181. /**
  182. * Controls handling of options and displaying in the admin
  183. *
  184. * @package Lilina
  185. * @subpackage Administration
  186. */
  187. class AdminOptions {
  188. protected static $instance = null;
  189. public $whitelisted = array();
  190. public $sections = array();
  191. public function __construct() {
  192. $this->whitelisted = array('sitename', 'template', 'locale', 'timezone', 'updateon');
  193. }
  194. public static function &instance() {
  195. if ( empty(AdminOptions::$instance) ) {
  196. AdminOptions::$instance = new AdminOptions();
  197. }
  198. return AdminOptions::$instance;
  199. }
  200. /**
  201. * Add a section to the options page
  202. *
  203. * @param string $id String for use in the 'id' attribute of tags.
  204. * @param string $title Title of the section.
  205. * @param string $callback Function that fills the section with the desired content. The function should echo its output.
  206. */
  207. public function add_section($id, $title, $callback) {
  208. if ( !isset($this->sections[$id]) )
  209. $this->sections[$id] = array();
  210. $this->sections[$id] = array('id' => $id, 'title' => $title, 'callback' => $callback, 'fields' => array());
  211. }
  212. /**
  213. * Add a field to a section on the options page
  214. *
  215. * @param string $id ID attribute of the field
  216. * @param string $title Title of the field.
  217. * @param string $callback Function that prints the field itself.
  218. * @param string $section Section of the settings page
  219. * @param array $args Additional arguments (such as label_for and note)
  220. */
  221. public function add_field($id, $title, $callback, $section = 'default', $args = array()) {
  222. if ( !isset($this->sections[$section]) ) {
  223. throw new Exception(_r('Invalid section.'));
  224. return false;
  225. }
  226. $this->sections[$section]['fields'][$id] = array('id' => $id, 'title' => $title, 'callback' => $callback, 'args' => $args);
  227. }
  228. /**
  229. * Print the option sections and associated options
  230. */
  231. public function do_sections() {
  232. if ( empty($this->sections) )
  233. return;
  234. foreach($this->sections as $id => $section) {
  235. echo '<fieldset id="' . $section['id'] . '">';
  236. echo '<legend>' . $section['title'] . '</legend>';
  237. call_user_func($section['callback'], $section);
  238. $this->do_fields($section);
  239. echo '</fieldset>';
  240. }
  241. }
  242. /**
  243. * Print the option fields for a section
  244. *
  245. * @param string $section Section array as registered by add_section() and add_field()
  246. */
  247. public function do_fields($section) {
  248. if ( empty($section['fields']) )
  249. return;
  250. foreach($section['fields'] as $field) {
  251. echo '<div class="row" id="' . $field['id'] . '">';
  252. if ( !empty($field['args']['label_for']) )
  253. echo '<label for="' . $field['args']['label_for'] . '">' . $field['title'] . ':</label>';
  254. else
  255. echo '<p class="title">' . $field['title'] . '</p>';
  256. call_user_func($field['callback'], $field['args']);
  257. if ( !empty($field['args']['note']) )
  258. echo '<p class="sidenote">' . $field['args']['note'] . '</p>';
  259. echo '</div>';
  260. }
  261. }
  262. }