* @copyright Copyright (C) 2006 Chris Lamb * @copyright Copyright (C) 2007, 2008, 2009, 2010 Abel Cheung * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU AGPL v3 */ /** * This class is used for encapsulating various admin related functions * * @since 0.3.50 * @package ScoreRender * @access private This class is not supposed to be used beyond ScoreRender */ class ScoreRenderAdmin { /** * Returns number of cached images inside cache directory * * @since 0.2 * @uses scorerender_get_cache_location() For reading cached image folder and counting images * @return integer number of images inside cache directory, or -1 if cache dir can't be read * @access private */ private function get_num_of_images () { if ( false === ( $handle = opendir (scorerender_get_cache_location()) ) ) return -1; $count = 0; while (false !== ($file = readdir ($handle))) if (preg_match (REGEX_CACHE_IMAGE, $file)) $count++; closedir ($handle); return $count; } /** * Remove all cached images in cache directory * * @since 0.2 * @uses scorerender_get_cache_location() For searching cached image folder and deleting images * @access private */ private function remove_cache () { if ( false === ( $handle = opendir (scorerender_get_cache_location()) ) ) return; while (false !== ($file = readdir ($handle))) { if (preg_match (REGEX_CACHE_IMAGE, $file)) @unlink ($dir . DIRECTORY_SEPARATOR . $file); } closedir ($handle); return; } /** * Update ScoreRender options in database with submitted options. * * A warning banner will be shown on top of admin page for each * error encountered in various options. In some cases supplied * config values will be discarded. * * @uses transform_paths() * @uses ScoreRender::is_web_hosting() * @uses ScoreRender::is_prog_usable() Check if ImageMagick is usable * @uses scorerender_get_def_settings() * @uses scorerender_get_cache_location() Also checks if cached image folder is writable * @access private */ private function update_options () { if ( !current_user_can ('manage_options') ) wp_die (__('Cheatin’ uh?', TEXTDOMAIN)); global $sr_options; $newopt = (array) $_POST['ScoreRender']; transform_paths ($newopt, TRUE); $errmsgs = array (); $sr_adm_msgs = array ( 'temp_dir_not_writable' => array ( 'level' => MSG_WARNING, 'content' => __('Temporary directory is NOT writable! Will fall back to system default setting.', TEXTDOMAIN)), 'cache_dir_not_writable' => array ( 'level' => MSG_FATAL , 'content' => sprintf (__('Cache directory is NOT writable! If default value is used, please go to WordPress file upload setting and check default upload directory; otherwise please make sure the cache directory you specified can be accessed by web server. The plugin will stop working.', TEXTDOMAIN)), admin_url('options-misc.php')), 'wrong_frag_per_comment' => array ( 'level' => MSG_WARNING, 'content' => __('Fragment per comment is not a non-negative integer. Value discarded.', TEXTDOMAIN)), 'wrong_image_max_width' => array ( 'level' => MSG_WARNING, 'content' => __('Image maximum width must be positive integer >= 72. Value discarded.', TEXTDOMAIN)), 'convert_bin_problem' => array ( 'level' => MSG_FATAL , 'content' => __('Failed to detect usable ImageMagick convert program! The plugin will stop working.', TEXTDOMAIN)), 'prog_check_disabled' => array ( 'level' => MSG_WARNING, 'content' => __('Some PHP functions are disabled due to security reasons. Program validation will not be done.', TEXTDOMAIN)), ); // error message definition for each notation do_action_ref_array ('scorerender_define_adm_msgs', array(&$sr_adm_msgs)); /* * general options */ if ( !empty ($newopt['TEMP_DIR']) && !is_writable ($newopt['TEMP_DIR']) ) { $errmsgs[] = 'temp_dir_not_writable'; $newopt['TEMP_DIR'] = ''; } if ( !empty ($newopt['CACHE_DIR']) ) { if ( !is_writable ($newopt['CACHE_DIR']) ) $errmsgs[] = 'cache_dir_not_writable'; } else { $newopt['CACHE_DIR'] = ''; if ( !is_writable ( scorerender_get_cache_location() ) ) $errmsgs[] = 'cache_dir_not_writable'; } if ( ScoreRender::is_web_hosting() ) $errmsgs[] = 'prog_check_disabled'; if ( ! ScoreRender::is_prog_usable ('ImageMagick', $newopt['CONVERT_BIN'], '-version') ) $errmsgs[] = 'convert_bin_problem'; // Any boolean values set to false would not appear in $_POST $var_types = scorerender_get_def_settings (TYPES_ONLY); foreach ($var_types as $key => $type) if ($type == 'bool') $newopt[$key] = isset ($newopt[$key]); if ( isset ($newopt['FRAGMENT_PER_COMMENT']) && !ctype_digit ($newopt['FRAGMENT_PER_COMMENT']) ) { $errmsgs[] = 'wrong_frag_per_comment'; unset ($newopt['FRAGMENT_PER_COMMENT']); } if ( !ctype_digit ($newopt['IMAGE_MAX_WIDTH']) || ($newopt['IMAGE_MAX_WIDTH'] < (1 * DPI)) ) { $errmsgs[] = 'wrong_image_max_width'; unset ($newopt['IMAGE_MAX_WIDTH']); } // program checking for each notation do_action_ref_array ('scorerender_check_notation_progs', array(&$errmsgs, &$newopt)); $sr_options = array_merge ($sr_options, $newopt); transform_paths ($sr_options, TRUE); update_option ('scorerender_options', $sr_options); transform_paths ($sr_options, FALSE); if ( !empty ($errmsgs) ) { foreach (array_values ($errmsgs) as $m) { if ($sr_adm_msgs[$m]['level'] == MSG_WARNING) { $class = 'scorerender-warning'; $mesg = __('WARNING: %s', TEXTDOMAIN); } elseif ($sr_adm_msgs[$m]['level'] == MSG_FATAL) { $class = 'scorerender-error'; $mesg = __('ERROR: %s', TEXTDOMAIN); } printf ("

%s

\n", 'sr-err-' . $sr_adm_msgs[$m], $class, sprintf ($mesg, $sr_adm_msgs[$m]['content']) ); } } else echo '

' . __('Options saved.', TEXTDOMAIN) . "

\n"; } /** * WP hook added to admin page header * * @since 0.3 */ public function admin_head() { ?>

NOT accessible from web. System default will be used if left blank.', TEXTDOMAIN) ?>

'); ?>
emulation of translucency in PNG images in IE 5.5/6, which is not supported by IE below version 7. This option only affects images rendered by ScoreRender. Make sure you have NOT installed any plugin with the same functionality before turning on this option, which may conflict with each other.', 'http://www.twinhelix.com/css/iepngfix/' ); ?>




%s’ checkbox above instead. This option does not affect posts and pages.', TEXTDOMAIN), __('Enable rendering for comments', TEXTDOMAIN)); ?>

get_num_of_images(); if ( 0 > $img_count ) echo "" . __('Cache directory is not readable, thus no image count is shown.', TEXTDOMAIN) . "
"; else printf (__ngettext("Cache directory contains %d image.\n", "Cache directory contains %d images.\n", $img_count, TEXTDOMAIN), $img_count); $dir = scorerender_get_cache_location(); if ( is_writable ($dir) && is_readable ($dir) ) : ?>
remove_cache(); } if ( isset($_POST['Submit']) && isset($_POST['ScoreRender']) ) { check_admin_referer ('scorerender-update-options'); $this->update_options(); } ?>

admin_section_path(); // program location options $this->admin_section_prog(); // image options $this->admin_section_image(); // content options $this->admin_section_content(); // caching options $this->admin_section_caching(); ?>

%s', admin_url ('options-general.php?page=scorerender'), __('Settings') // use global WP translation ); return $links; } /** * Append submenu item into WordPress menu * * @uses ScoreRenderAdmin::admin_head() * @uses ScoreRenderAdmin::admin_footer() * @uses ScoreRenderAdmin::admin_page() */ public function register_admin_page () { $plugin_page = add_options_page (__('ScoreRender options', TEXTDOMAIN), 'ScoreRender', 'manage_options', 'scorerender', array (&$this, 'admin_page')); add_action('admin_head-' . $plugin_page, array (&$this, 'admin_head')); // not using print_scripts hooks, not sanitized until WP 2.8 add_action('admin_footer-' . $plugin_page, array (&$this, 'admin_footer')); } /** * Output warning to tell users to turn off 'correct invalidly nested * XHTML automatically' option. */ public function turn_off_balance_tags() { echo '

' . sprintf (__('OPTION CONFLICT: The ‘correct invalidly nested XHTML automatically’ option conflicts with ScoreRender plugin, because it will mangle certain Lilypond and Mup fragments. The option is available in Writing option page.', TEXTDOMAIN), "options-writing.php") . "

"; } /** * Class constructor, for adding wordpress admin related hooks * * @uses ScoreRenderAdmin::register_admin_page() * @uses ScoreRenderAdmin::settings_link() * @uses ScoreRenderAdmin::turn_off_balance_tags() * @access private */ public function __construct () { add_action ('admin_menu', array (&$this, 'register_admin_page')); add_filter ('plugin_action_links', array (&$this, 'settings_link'), 10, 2); if ( 0 != get_option('use_balanceTags') ) add_action ('admin_notices', array (&$this, 'turn_off_balance_tags')); } } // end class global $sr_admin; $sr_admin = new ScoreRenderAdmin(); ?>