コード例 #1
0
 public static function site_http($site_http = NULL)
 {
     if ($site_http === NULL) {
         return self::$site_http;
     } else {
         self::$site_http = CrayonUtil::url_slash($site_http);
     }
 }
コード例 #2
0
<?php

require_once '../global.php';
require_once CRAYON_HIGHLIGHTER_PHP;
// These will depend on your framework
CrayonGlobalSettings::site_http('http://localhost/crayon/wp-content/plugins/crayon-syntax-highlighter/');
CrayonGlobalSettings::site_path(dirname(__FILE__));
CrayonGlobalSettings::plugin_path('http://localhost/crayon/wp-content/plugins/crayon-syntax-highlighter/');
// Should be in the header
crayon_resources();
$crayon = new CrayonHighlighter();
$crayon->code('some code');
$crayon->language('php');
$crayon->title('the title');
$crayon->marked('1-2');
$crayon->is_inline(FALSE);
// Settings
$settings = array(CrayonSettings::NUMS => FALSE, CrayonSettings::TOOLBAR => TRUE, CrayonSettings::ENQUEUE_THEMES => FALSE, CrayonSettings::ENQUEUE_FONTS => FALSE);
$settings = CrayonSettings::smart_settings($settings);
$crayon->settings($settings);
// Print the Crayon
$crayon_formatted = $crayon->output(TRUE, FALSE);
echo $crayon_formatted;
// Utility Functions
function crayon_print_style($id, $url, $version)
{
    echo '<link id="', $id, '" href="', $url, '?v=', $version, '" type="text/css" rel="stylesheet" />', "\n";
}
function crayon_print_script($id, $url, $version)
{
    echo '<script id="', $id, '" src="', $url, '?v=', $version, '" type="text/javascript"></script>', "\n";
コード例 #3
0
 public static function load_settings($just_load_settings = FALSE)
 {
     if (self::$options === NULL) {
         // Load settings from db
         if (!(self::$options = get_option(self::OPTIONS))) {
             self::$options = CrayonSettings::get_defaults_array();
             update_option(self::OPTIONS, self::$options);
         }
         // Initialise default global settings and update them from db
         CrayonGlobalSettings::set(self::$options);
     }
     if (!self::$is_fully_loaded && !$just_load_settings) {
         // Load everything else as well
         // Load all available languages and themes
         CrayonResources::langs()->load();
         CrayonResources::themes()->load();
         // For local file loading
         // This is used to decouple WP functions from internal Crayon classes
         CrayonGlobalSettings::site_http(home_url());
         CrayonGlobalSettings::site_path(ABSPATH);
         CrayonGlobalSettings::plugin_path(plugins_url('', __FILE__));
         // Ensure all missing settings in db are replaced by default values
         $changed = FALSE;
         foreach (CrayonSettings::get_defaults_array() as $name => $value) {
             // Add missing settings
             if (!array_key_exists($name, self::$options)) {
                 self::$options[$name] = $value;
                 $changed = TRUE;
             }
         }
         // A setting was missing, update options
         if ($changed) {
             update_option(self::OPTIONS, self::$options);
         }
         self::$is_fully_loaded = TRUE;
     }
 }
コード例 #4
0
 private function load()
 {
     if (empty($this->url)) {
         $this->error('The specified URL is empty, please provide a valid URL.');
         return;
     }
     /*	Try to replace the URL with an absolute path if it is local, used to prevent scripts
     		 from executing when they are loaded. */
     $url = $this->url;
     if ($this->setting_val(CrayonSettings::DECODE_ATTRIBUTES)) {
         $url = CrayonUtil::html_entity_decode($url);
     }
     $url = CrayonUtil::pathf($url);
     $local = FALSE;
     // Whether to read locally
     $site_http = CrayonGlobalSettings::site_http();
     $site_path = CrayonGlobalSettings::site_path();
     $scheme = parse_url($url, PHP_URL_SCHEME);
     // Try to replace the site URL with a path to force local loading
     if (strpos($url, $site_http) !== FALSE || strpos($url, $site_path) !== FALSE) {
         $url = str_replace($site_http, $site_path, $url);
         // Attempt to load locally
         $local = TRUE;
         $local_url = $url;
     } else {
         if (empty($scheme)) {
             // No url scheme is given - path may be given as relative
             $local_url = preg_replace('#^((\\/|\\\\)*)?#', $site_path . $this->setting_val(CrayonSettings::LOCAL_PATH), $url);
             $local = TRUE;
         }
     }
     // Try to find the file locally
     if ($local == TRUE) {
         if (($contents = CrayonUtil::file($local_url)) !== FALSE) {
             $this->code($contents);
         } else {
             $local = FALSE;
             CrayonLog::log("Local url ({$local_url}) could not be loaded", '', FALSE);
         }
     }
     // If reading the url locally produced an error or failed, attempt remote request
     if ($local == FALSE) {
         if (empty($scheme)) {
             $url = 'http://' . $url;
         }
         $http_code = 0;
         // If available, use the built in wp remote http get function, we don't need SSL
         if (function_exists('wp_remote_get')) {
             $url_uid = 'crayon_' . CrayonUtil::str_uid($url);
             $cached = get_transient($url_uid, 'crayon-syntax');
             CrayonSettingsWP::load_cache();
             if ($cached !== FALSE) {
                 $content = $cached;
                 $http_code = 200;
             } else {
                 $response = @wp_remote_get($url, array('sslverify' => false, 'timeout' => 20));
                 $content = wp_remote_retrieve_body($response);
                 $http_code = wp_remote_retrieve_response_code($response);
                 $cache = $this->setting_val(CrayonSettings::CACHE);
                 $cache_sec = CrayonSettings::get_cache_sec($cache);
                 if ($cache_sec > 1 && $http_code >= 200 && $http_code < 400) {
                     set_transient($url_uid, $content, $cache_sec);
                     CrayonSettingsWP::add_cache($url_uid);
                 }
             }
         } else {
             $ch = curl_init($url);
             curl_setopt($ch, CURLOPT_HEADER, FALSE);
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
             // For https connections, we do not require SSL verification
             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
             curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
             curl_setopt($ch, CURLOPT_FRESH_CONNECT, FALSE);
             curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
             curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
             $content = curl_exec($ch);
             $error = curl_error($ch);
             $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
             curl_close($ch);
         }
         if ($http_code >= 200 && $http_code < 400) {
             $this->code($content);
         } else {
             if (empty($this->code)) {
                 // If code is also given, just use that
                 $this->error("The provided URL ('{$this->url}'), parsed remotely as ('{$url}'), could not be accessed.");
             }
         }
     }
     $this->needs_load = FALSE;
 }