$cache_limit){ $cachedir = opendir(ANYFONT_CACHE); while ($curfile = readdir($cachedir)) { if(!is_dir($curfile)){ $split = explode("-", $curfile); if($split[1] == "admin.png"){ unlink(ANYFONT_CACHE."/$curfile"); } } } } update_option('anyfont_cache_last_checked', time()); } if(get_option('anyfont_image_module') && get_option('anyfont_image_module') != "auto"){ $libver = get_option('anyfont_image_module'); } else { if(!function_exists('version_compare') || version_compare( phpversion(), '5', '<' )){ $libver = "php4"; }else if(!extension_loaded("imagick")) { $libver = "php4"; }else{ $libver = "php5"; } } define('ANYFONT_LIBDIR', ANYFONT_ROOT."/lib"); define('ANYFONT_LIB_VERSION', $libver); $dont_replace_title = false; function anyfont_admin_menu(){ $settings = __("Settings", 'anyfont'); $fontmanager = __("Manage Fonts", 'anyfont'); $stylemanager = __("Manage Styles", 'anyfont'); $insertmanager = __("Text Replacements", 'anyfont'); add_menu_page('AnyFont', 'AnyFont', 8, 'anyfont-settings', 'anyfont_settings_page', ANYFONT_URL."/img/anyfont-icon.png"); add_submenu_page('anyfont-settings', $settings, $settings, 8, 'anyfont-settings', 'anyfont_settings_page'); add_submenu_page('anyfont-settings', $fontmanager, $fontmanager, 8, 'anyfont-fonts', 'anyfont_fonts_page'); add_submenu_page('anyfont-settings', $stylemanager, $stylemanager, 8, 'anyfont-styles', 'anyfont_styles_page'); } function anyfont_settings_page(){ require_once(ANYFONT_LIBDIR."/class.admin.php"); $config = array("page" => "settings", "title" => __("Settings", 'anyfont')); $page = new anyfontAdmin($config); $page->printPage(); } function anyfont_fonts_page() { require_once(ANYFONT_LIBDIR."/class.admin.php"); $config = array("page" => "fonts", "title" => __("Font Manager", 'anyfont')); $page = new anyfontAdmin($config); $page->printPage(); } function anyfont_styles_page() { require_once(ANYFONT_LIBDIR."/class.admin.php"); $config = array("page" => "styles", "title" => __("Style Manager", 'anyfont')); $page = new anyfontAdmin($config); $page->printPage(); } function anyfont_insert_scripts() { $page_req = parse_url( $_SERVER['REQUEST_URI']); if(isset($page_req['query'])){ $page = explode("=", $page_req['query']); $anyfont_pages = array('anyfont-settings','anyfont-fonts','anyfont-styles'); if(in_array($page[1], $anyfont_pages)){ wp_enqueue_script("anyfont", ANYFONT_URL."/anyfont.js", array('prototype', 'scriptaculous')); } } } /** * Check if permalinks are in use. * @return boolean */ function anyfont_check_permalink_status(){ return (get_option("permalink_structure") != '' && !get_option('anyfont-use-htaccess')) ? true : false; } /** * Update .htaccess file according to permalink status * @param string */ function anyfont_update_permalink_status($rules){ if((!anyfont_check_permalink_status() && $rules !== "uninstall") || (!anyfont_check_permalink_status() && $rules === "install")){ $htaccess_file = ABSPATH.".htaccess"; $anyfont_url = parse_url(ANYFONT_URL); $anyfont_path = trailingslashit($anyfont_url['path']); $rules = "# BEGIN AnyFont\n"; $rules .= "\n"; $rules .= "RewriteEngine On\n"; $rules .= "RewriteBase /\n"; $rules .= "RewriteCond %{REQUEST_FILENAME} !-f\n"; $rules .= "RewriteRule ^images/(.*)\.png$ {$anyfont_path}img.php [L]\n"; $rules .= "\n\n"; $rules .= "# END AnyFont\n\n"; if(!file_exists($htaccess_file)){ if(is_writeable(ABSPATH)){ touch($htaccess_file); anyfont_file_put_contents($htaccess_file, $rules); } } else { $orig = file_get_contents($htaccess_file); if(!strstr($orig, $rules)){ anyfont_file_put_contents($htaccess_file, $rules.$orig); } } }else if(anyfont_check_permalink_status() || $rules === "uninstall"){ $htaccess_file = ABSPATH.".htaccess"; if(file_exists($htaccess_file)){ if(is_writeable($htaccess_file)){ $contents = file_get_contents($htaccess_file); $start = mb_strpos($contents, "# BEGIN AnyFont"); $end = mb_strpos($contents, "# END AnyFont") + mb_strlen("# END AnyFont"); if(is_numeric($start)){ $new_contents = substr_replace($contents, "", $start, $end); anyfont_file_put_contents($htaccess_file, $new_contents); } } } } return $rules; } function anyfont_install(){ add_option('anyfont_image_module', 'auto'); add_option('anyfont_disable_hotlinking', true); add_option('anyfont_enable_tinymce', true); add_option('anyfont_cache_last_checked', time()); (file_exists(ABSPATH.".htaccess") && is_writeable(ABSPATH.".htaccess")) ? add_option('anyfont-use-htaccess', true) : 0; anyfont_update_permalink_status("install"); if(!file_exists(ANYFONT_FONTDIR."/styles.ini")){ touch(ANYFONT_FONTDIR."/styles.ini"); } else { $styles = parse_ini_file(ANYFONT_FONTDIR."/styles.ini", true); if(isset($styles['admin'])){ unset($styles['admin']); anyfont_write_styles($styles); } } return true; } function anyfont_uninstall(){ anyfont_update_permalink_status("uninstall"); } function anyfont_file_put_contents($filename, $data){ if(is_writeable($filename)){ if(!function_exists('version_compare') || version_compare( phpversion(), '5', '<' )){ $fw = fopen($filename, 'w'); fwrite($fw, $data); fclose($fw); } else { file_put_contents($filename, $data); } return true; } return false; } function anyfont_serialize_array($array, $prefix = ''){ $ini = array(); if( is_array($array)){ ksort($array); foreach ($array as $key => $value){ // parse data types if ($value === true || $value == '1') $value = 'true'; else if ($value === false || $value === '') $value = 'false'; else if (is_string($value) && $key != 'font-size') $value = '"' . addslashes($value) . '"'; // serialize value if (!is_array($value) && !is_numeric($key)) $ini[] = ($prefix ? $prefix . '.' : '') . $key . ' = ' . $value; else if (!is_array($value)) $ini[] = $prefix . '[] = ' . $value; else $ini = array_merge($ini, anyfont_serialize_array($value, ($prefix ? $prefix . '.' : '') . $key)); } return $ini; } else { return false; } } function anyfont_check_styles_ini(){ if(!file_exists(ANYFONT_FONTDIR."/styles.ini")){ if(is_writeable(ANYFONT_FONTDIR)){ touch(ANYFONT_FONTDIR."/styles.ini"); } else { $result = array("savestatus" => "failed", "error" => sprintf(__("The folder '%s' does not exist or cannot be written to, please check that this folder exists and has the correct permissions. As a last resort set the folder permissions to 0777"), ANYFONT_FONTDIR)); return false; } } return true; } function anyfont_edit_styles(){ if(isset($_REQUEST['update_style']) && $_REQUEST['update_style'] != ''){ if(anyfont_check_styles_ini() !== false){ $style = _anyfont_edit_styles(false); anyfont_write_styles($style); if(!isset($_REQUEST['new-style'])){ $result =anyfont_edit_style_return_img(false); } else { require_once(ANYFONT_LIBDIR."/class.admin.php"); $admn = new anyfontAdmin(); $style_block = $admn->getStyleBlock($_REQUEST['update_style'], $style[$_REQUEST['update_style']]); $result = array("savestatus" => "savedNew", "stylename" => $_REQUEST['update_style'], "styleblock" => $style_block, "msg" => $_REQUEST['update_style']." has been saved."); } } } else { $result = array("savestatus" => "failed", "error" => _("Please enter the style name.")); } return_json($result); } function anyfont_preview_style(){ $style = _anyfont_edit_styles(true); anyfont_write_styles($style); return_json(anyfont_edit_style_return_img(true)); } function anyfont_edit_style_return_img($preview=false){ $style_name = !$preview ? urlencode($_REQUEST['update_style']) : "Preview"; return array("savestatus" => "saved", "stylename" => str_replace(" ", "-", $_REQUEST['update_style']), // RW : Added an @ to the base64_encode to prevent AJAX errors on my prod server while saving an edited style. "img" => @base64_encode(file_get_contents(get_option('siteurl')."/images/$style_name/".urlencode($_REQUEST['update_style']).".png")) ); } function _anyfont_edit_styles($preview=false){ $update_style = !$preview ? $_REQUEST['update_style'] : "Preview"; $style = parse_ini_file(ANYFONT_FONTDIR."/styles.ini", true); $style[$update_style]['font-name'] = $_REQUEST['font-name']; $style[$update_style]['font-size'] = (int)str_replace("pt", "", $_REQUEST['font-size']); $style[$update_style]['color'] = str_replace("#", "", $_REQUEST['color']); $style[$update_style]['background-color'] = str_replace("#", "", $_REQUEST['background-color']); $style[$update_style]['shadow'] = $_REQUEST['shadow'] == 'on' ? true : false; $style[$update_style]['shadow-color'] = str_replace("#", "", $_REQUEST['shadow-color']); $style[$update_style]['shadow-spread'] = $_REQUEST['shadow-spread'].""; $style[$update_style]['shadow-distance'] = (int)str_replace("px", "", $_REQUEST['shadow-distance']); $style[$update_style]['soften-shadow'] = $_REQUEST['soften-shadow'] == 'on' ? true : false; $style[$update_style]['image-padding'] = $_REQUEST['image-padding'] == 'on' ? true : false; $style[$update_style]['image-padding-radius'] = (int)str_replace("px", "", $_REQUEST['image-padding-radius']); // RW : New attributes $style[$update_style]['limit-width'] = $_REQUEST['limit-width'] == 'on' ? true : false; $style[$update_style]['max-width'] = (int)str_replace(" characters", "", $_REQUEST['max-width']); return $style; } function anyfont_write_styles($style){ ksort($style); $sections = array_unique(array_merge(array(''), array_keys($style))); $ini = array(); foreach ($sections as $section) { // write sections if ($section != '') $ini[] = '[' . $section . ']'; // serialize value array if(is_array($style[$section])){ $ini = array_merge($ini, anyfont_serialize_array($style[$section])); } $ini[] = ''; } anyfont_file_put_contents(ANYFONT_FONTDIR."/styles.ini", implode("\r\n", $ini)); } function anyfont_delete_font(){ if(isset($_REQUEST['font-name'])){ $font = urldecode($_REQUEST['font-name']); $filename = ANYFONT_FONTDIR."/$font".(!file_exists(ANYFONT_FONTDIR."/$font.ttf") ? ".otf" : ".ttf"); unlink($filename); printf(__("%s has been deleted.", 'anyfont'), $font); exit(0); }else if(isset($_REQUEST['fonts'])){ $fonts = explode(",", $_REQUEST['fonts']); foreach($fonts as $font){ $font = urldecode($font); $filename = ANYFONT_FONTDIR."/$font".(!file_exists(ANYFONT_FONTDIR."/$font.ttf") ? ".otf" : ".ttf"); unlink($filename); } _e("The selected fonts have been deleted.", 'anyfont'); exit(0); } } function anyfont_delete_style(){ $style = parse_ini_file(ANYFONT_FONTDIR."/styles.ini", true); if(isset($_REQUEST['styles'])){ $styles = explode(",", $_REQUEST['styles']); foreach($styles as $stylename){ unset($style[$stylename]); $msg = __("The selected styles have been deleted.", 'anyfont'); } }else if(isset($_REQUEST['style-name'])){ unset($style[$_REQUEST['style-name']]); $msg = sprintf(__("%s has been deleted.", 'anyfont'), $_REQUEST['style-name']); } anyfont_write_styles($style); echo $msg; exit(0); } function anyfont_clear_cache(){ $cachedir = ANYFONT_CACHE; $dir = opendir($cachedir); while ($filename = readdir($dir)) { if(!is_dir($filename)){ unlink($cachedir."/".$filename); } } rmdir($cachedir); wp_mkdir_p(ANYFONT_CACHE); require_once(ANYFONT_LIBDIR."/class.admin.php"); $html = new anyfontAdmin(false); $html->checkAnyFontHealth(); $return = array("block"=>"anyfont_disk_usage", "content"=>$html->getDiskCache(true), "message"=>__("Cache Cleared", 'anyfont') ); return_json($return); } function anyfont_replace_title($title){ global $dont_replace_title; if(!in_the_loop() || is_feed()){ return $title; }else{ $uri = explode("/", $_SERVER['REQUEST_URI']); $title_replace = !is_page() ? get_option('anyfont_post_title') : get_option('anyfont_page_title'); if(!$dont_replace_title && $title_replace && !in_array("wp-admin", $uri)){ $style = !is_page() ? get_option('anyfont_post_title_style') : get_option('anyfont_page_title_style'); $url = get_option('siteurl'); $urltitle = urlencode($title); $style = urlencode($style); return "\"$title\""; }else{ return $title; } } } function anyfont_replace_tag_title($title){ $uri = explode("/", $_SERVER['REQUEST_URI']); if(get_option('anyfont_tag_title') && !in_array("wp-admin", $uri)){ remove_filter("single_tag_title", "anyfont_replace_tag_title", 10, 2); $style = get_option('anyfont_tag_title_style'); $url = get_option('siteurl'); $urltitle = urlencode($title); $style = urlencode($style); return "\"$title\""; }else{ return $title; } } function anyfont_replace_cat_title($title){ $uri = explode("/", $_SERVER['REQUEST_URI']); if(is_category() && get_option('anyfont_cat_title') && !in_array("wp-admin", $uri)){ remove_filter("single_cat_title", "anyfont_replace_cat_title", 10, 2); $style = get_option('anyfont_cat_title_style'); $url = get_option('siteurl'); $urltitle = urlencode($title); $style = urlencode($style); echo "\"$title\""; }else{ return $title; } } function anyfont_replace_widget_title($params){ if(get_option('anyfont_widget_title')){ $style = urlencode(get_option('anyfont_widget_title_style')); $url = get_option('siteurl'); foreach($params as $key => $param){ if(!preg_match("//", $params[$key]['before_title'], $match) && !preg_match("//", $params[$key]['after_title'], $match)){ $params[$key]['before_title'] = $params[$key]['before_title']."\"".$params[$key]['after_title']; } } } return $params; } function anyfont_update_option(){ if(is_array($_REQUEST['option'])){ foreach($_REQUEST['option'] as $option){ if(!is_array($_REQUEST[$option])){ update_option($option , $_REQUEST[$option]); $option === 'anyfont-use-htaccess' ? anyfont_update_permalink_status() : 0; if($option == 'anyfont-limit-cache-size' || $option == 'anyfont-cache-size-limit'){ require_once(ANYFONT_LIBDIR."/class.admin.php"); $html = new anyfontAdmin(false); $html->checkAnyFontHealth(false); $return = array("type"=>"replace", "block"=>"anyfont_disk_usage", "content"=>$html->getDiskCache(true), "message"=>__("Your settings have been saved!", 'anyfont') ); } else { $return = array("type"=>"message", "message"=>__("Your settings have been saved!", 'anyfont') ); } }else { $result = $_REQUEST[$option]['result'] == "on" ? true : false; update_option($option , $result); if($result){ if($option['style'] !== "false"){ update_option($option."_style", $_REQUEST[$option]['style']); if($option == "anyfont_menu"){ update_option($option."_hover", $_REQUEST[$option]['hover']); update_option($option."_active", $_REQUEST[$option]['active']); } $return = array("type"=>"message", "message"=>__("Your settings have been saved!", 'anyfont') ); } else { $return = array("type"=>"message", "message"=>__("Error: No Style Selected! If you have not created any styles yet, click here to go to the style manager now.", 'anyfont') ); } } } } } return_json($return); } function return_json($return){ header("Content-type: application/json"); if (function_exists('json_encode')) { echo json_encode($return); exit(0); } else { require_once(ANYFONT_ROOT.'/lib/class.json.php'); $JSON = new serviceJSON(); echo $JSON->encode($return); exit(0); } } function anyfont_bloginfo_replace($output, $show){ switch($show){ case 'name': if(get_option('anyfont_blog_title')){ $style = get_option('anyfont_blog_title_style'); $url = get_option('siteurl'); $urltitle = urlencode($output); $style = urlencode($style); !get_option('anyfont_blog_desc') ? remove_filter("bloginfo", "anyfont_bloginfo_replace", 10, 2) : 0; return "\"$output\""; }else{ return $output; } case 'description': if(get_option('anyfont_blog_desc')){ $style = get_option('anyfont_blog_desc_style'); $url = get_option('siteurl'); $urltitle = urlencode($output); $style = urlencode($style); remove_filter("bloginfo", "anyfont_bloginfo_replace", 10, 2); return "\"$output\""; }else{ return $output; } default: return $output; } } function anyfont_img_output(){ if(anyfont_ini_get_bool('display_errors')){ ini_set('display_errors', 0); } if($_SERVER['REMOTE_ADDR'] != $_SERVER['SERVER_ADDR']){ $site_url = parse_url(get_option('siteurl')); $referer = parse_url($_SERVER['HTTP_REFERER']); if(!strstr($site_url['host'], $referer['host']) && get_option('anyfont_disable_hotlinking')){ wp_redirect(get_option('siteurl')); exit(0); } } $req_vars = explode( "/", $_SERVER['REQUEST_URI']); foreach($req_vars as $n => $v){ if($v == "images"){ $sn = $n; continue; } } $style = urldecode($req_vars[($sn+1)]); $text = str_replace(".png", "", html_entity_decode(urldecode($req_vars[($sn+2)]), ENT_QUOTES, 'UTF-8')); $displaytext = isset($req_vars[($sn+3)]) ? str_replace(".png", "", html_entity_decode(urldecode($req_vars[($sn+3)]), ENT_QUOTES, 'UTF-8')) : false; if(!isset($text) || !isset($style)){ wp_redirect(get_option('siteurl')); exit(0); }else{ $gzip = get_option('anyfont_enable_gzip') == "on" ? isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') ? 1 : 0 : 0 : 0; require_once(ANYFONT_LIBDIR."/".ANYFONT_LIB_VERSION."/class.image.php"); new ttfImage($text, $style, $displaytext, $gzip); } } function anyfont_img_headers($headers){ $headers = false; return $headers; } function anyfont_add_header_filter(){ add_filter("bloginfo", "anyfont_bloginfo_replace", 10, 2); add_filter("single_cat_title", "anyfont_replace_cat_title", 10, 2); add_filter("single_tag_title", "anyfont_replace_tag_title", 10, 2); } function anyfont_page_menu_filter($menu){ if(get_option('anyfont_menu')){ $style = get_option('anyfont_menu_style'); $url = get_option('siteurl'); $urltitle = urlencode($url); $style = urlencode($style); if(preg_match_all("/title=\W([\w*\s?]*)\W/", $menu, $titles) > 0){ $n=0; foreach($titles[0] as $key){ $menu = preg_replace("/($key\W?)({$titles[1][$n]})/", '$1$2', $menu); $n+=1; } } } return $menu; } function anyfont_ini_get_bool($a){ $b = ini_get($a); switch (strtolower($b)){ case 'on': case 'yes': case 'true': return 'assert.active' !== $a; case 'stdout': case 'stderr': return 'display_errors' === $a; default: return (bool)(int)$b; } } function anyfont_image_swap(){ $url = get_option('siteurl'); $menu_style = get_option('anyfont_menu_style'); $hover_style = is_string(get_option('anyfont_menu_hover')) ? get_option('anyfont_menu_hover') : get_option('anyfont_menu_style'); $active_style = is_string(get_option('anyfont_menu_active')) ? get_option('anyfont_menu_active') : get_option('anyfont_menu_style'); $script = ""; echo $script; } function anyfont_load_js(){ wp_enqueue_script('prototype'); } function anyfont_register_button($buttons) { array_push($buttons, "separator", "anyfont"); return $buttons; } function anyfont_add_tinymce_plugin($plugin_array) { $plugin_array['anyfont'] = WP_PLUGIN_URL.'/anyfont/mce_anyfont/editor_plugin.js'; return $plugin_array; } function anyfont_settings_link($links, $filename) { $filename == plugin_basename(__FILE__) ? array_unshift($links, 'Settings') : 0; return $links; } function anyfont_dashboard_widget_function() { require_once(ANYFONT_LIBDIR."/class.admin.php"); $html = new anyfontAdmin(false); $html->checkAnyFontHealth(); echo $html->getDiskCache(); } function anyfont_dashboard_widget() { wp_add_dashboard_widget('anyfont_dashboard_widget', 'AnyFont Disk Usage Summary', 'anyfont_dashboard_widget_function'); } if(preg_match("/\/images\/(.*)\.png/", $_SERVER['REQUEST_URI'])){ add_filter("wp_headers", "anyfont_img_headers"); add_action('plugins_loaded', 'anyfont_img_output'); } else { register_activation_hook(__FILE__, 'anyfont_install'); register_deactivation_hook(__FILE__, 'anyfont_uninstall'); add_action('wp_ajax_anyfont_edit_styles', 'anyfont_edit_styles'); add_action('wp_ajax_anyfont_delete_font', 'anyfont_delete_font'); add_action('wp_ajax_anyfont_delete_style', 'anyfont_delete_style'); add_action('wp_ajax_anyfont_clear_cache', 'anyfont_clear_cache'); add_action('wp_ajax_anyfont_update_option', 'anyfont_update_option'); add_action('wp_ajax_anyfont_preview_style', 'anyfont_preview_style'); add_action('admin_menu', 'anyfont_admin_menu'); add_action("admin_print_scripts", 'anyfont_insert_scripts'); add_filter("the_title", "anyfont_replace_title"); add_action("wp_head", "anyfont_add_header_filter", 10, 0); add_filter("dynamic_sidebar_params", "anyfont_replace_widget_title"); add_filter("wp_page_menu", 'anyfont_page_menu_filter'); add_filter("wp_list_page", 'anyfont_page_menu_filter'); add_filter("wp_list_pages", 'anyfont_page_menu_filter'); add_filter( 'plugin_action_links', 'anyfont_settings_link', 10, 2 ); add_action("permalink_structure_changed", "anyfont_update_permalink_status"); if(get_option('anyfont_menu') != false){ add_action('template_redirect', 'anyfont_load_js'); add_action("wp_head", "anyfont_image_swap"); } if(get_option('anyfont_cache_dashboard_widget') != false){ add_action('wp_dashboard_setup', 'anyfont_dashboard_widget' ); } if(get_option('anyfont_enable_tinymce') != false){ add_filter("mce_external_plugins", "anyfont_add_tinymce_plugin"); add_filter('mce_buttons', 'anyfont_register_button'); } } ?>