broken-link-checker
Last commit date
images
17 years ago
JSON.php
17 years ago
broken-link-checker.php
16 years ago
config-manager.php
16 years ago
core.php
16 years ago
highlighter-class.php
16 years ago
instance-classes.php
17 years ago
link-classes.php
16 years ago
readme.txt
16 years ago
uninstall.php
16 years ago
utility-class.php
16 years ago
utility-class.php
174 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @author W-Shadow |
| 5 | * @copyright 2009 |
| 6 | */ |
| 7 | |
| 8 | |
| 9 | if ( is_admin() && !function_exists('json_encode') ){ |
| 10 | //Load JSON functions for PHP < 5.2 |
| 11 | if (!class_exists('Services_JSON')){ |
| 12 | require 'JSON.php'; |
| 13 | } |
| 14 | |
| 15 | //Backwards compatible json_encode |
| 16 | function json_encode($data) { |
| 17 | $json = new Services_JSON(); |
| 18 | return( $json->encode($data) ); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | if ( !function_exists('sys_get_temp_dir')) { |
| 23 | function sys_get_temp_dir() { |
| 24 | if (!empty($_ENV['TMP'])) { return realpath($_ENV['TMP']); } |
| 25 | if (!empty($_ENV['TMPDIR'])) { return realpath( $_ENV['TMPDIR']); } |
| 26 | if (!empty($_ENV['TEMP'])) { return realpath( $_ENV['TEMP']); } |
| 27 | $tempfile = tempnam(uniqid(rand(),TRUE),''); |
| 28 | if (file_exists($tempfile)) { |
| 29 | unlink($tempfile); |
| 30 | return realpath(dirname($tempfile)); |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | if ( !class_exists('blcUtility') ){ |
| 36 | |
| 37 | class blcUtility { |
| 38 | |
| 39 | //A regxp for images |
| 40 | function img_pattern(){ |
| 41 | // \1 \2 \3 URL \4 |
| 42 | return '/(<img[\s]+[^>]*src\s*=\s*)([\"\'])([^>]+?)\2([^<>]*>)/i'; |
| 43 | } |
| 44 | |
| 45 | //A regexp for links |
| 46 | function link_pattern(){ |
| 47 | // \1 \2 \3 URL \4 \5 Text \6 |
| 48 | return '/(<a[\s]+[^>]*href\s*=\s*)([\"\'])([^>]+?)\2([^<>]*>)((?sU).*)(<\/a>)/i'; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * blcUtility::normalize_url() |
| 53 | * |
| 54 | * @param string $url |
| 55 | * @params string $base_url (Optional) The base URL is used to convert a relative URL to a fully-qualified one |
| 56 | * @return string A normalized URL or FALSE if the URL is invalid |
| 57 | */ |
| 58 | function normalize_url($url, $base_url = ''){ |
| 59 | //Sometimes links may contain shortcodes. Parse them. |
| 60 | $url = do_shortcode($url); |
| 61 | |
| 62 | $parts = @parse_url($url); |
| 63 | if(!$parts) return false; //Invalid URL |
| 64 | |
| 65 | if(isset($parts['scheme'])) { |
| 66 | //Only HTTP(S) links are checked. Other protocols are not supported. |
| 67 | if ( ($parts['scheme'] != 'http') && ($parts['scheme'] != 'https') ) |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | $url = html_entity_decode($url); |
| 72 | $url = preg_replace( |
| 73 | array('/([\?&]PHPSESSID=\w+)$/i', //remove session ID |
| 74 | '/(#[^\/]*)$/', //and anchors/fragments |
| 75 | '/&/', //convert improper HTML entities |
| 76 | '/^(javascript:.*)/i', //treat links that contain JS as links with an empty URL |
| 77 | '/([\?&]sid=\w+)$/i' //remove another flavour of session ID |
| 78 | ), |
| 79 | array('','','&','',''), |
| 80 | $url); |
| 81 | $url = trim($url); |
| 82 | |
| 83 | if ( $url=='' ) return false; |
| 84 | |
| 85 | // turn relative URLs into absolute URLs |
| 86 | if ( empty($base_url) ) $base_url = get_option('siteurl'); |
| 87 | $url = blcUtility::relative2absolute( $base_url, $url); |
| 88 | return $url; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * blcUtility::relative2absolute() |
| 93 | * Turns a relative URL into an absolute one given a base URL. |
| 94 | * |
| 95 | * @param string $absolute Base URL |
| 96 | * @param string $relative A relative URL |
| 97 | * @return string |
| 98 | */ |
| 99 | function relative2absolute($absolute, $relative) { |
| 100 | $p = @parse_url($relative); |
| 101 | if(!$p) { |
| 102 | //WTF? $relative is a seriously malformed URL |
| 103 | return false; |
| 104 | } |
| 105 | if( isset($p["scheme"]) ) return $relative; |
| 106 | |
| 107 | $parts=(parse_url($absolute)); |
| 108 | |
| 109 | if(substr($relative,0,1)=='/') { |
| 110 | $cparts = (explode("/", $relative)); |
| 111 | array_shift($cparts); |
| 112 | } else { |
| 113 | if(isset($parts['path'])){ |
| 114 | $aparts=explode('/',$parts['path']); |
| 115 | array_pop($aparts); |
| 116 | $aparts=array_filter($aparts); |
| 117 | } else { |
| 118 | $aparts=array(); |
| 119 | } |
| 120 | |
| 121 | $rparts = (explode("/", $relative)); |
| 122 | |
| 123 | $cparts = array_merge($aparts, $rparts); |
| 124 | foreach($cparts as $i => $part) { |
| 125 | if($part == '.') { |
| 126 | unset($cparts[$i]); |
| 127 | } else if($part == '..') { |
| 128 | unset($cparts[$i]); |
| 129 | unset($cparts[$i-1]); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | $path = implode("/", $cparts); |
| 134 | |
| 135 | $url = ''; |
| 136 | if($parts['scheme']) { |
| 137 | $url = "$parts[scheme]://"; |
| 138 | } |
| 139 | if(isset($parts['user'])) { |
| 140 | $url .= $parts['user']; |
| 141 | if(isset($parts['pass'])) { |
| 142 | $url .= ":".$parts['pass']; |
| 143 | } |
| 144 | $url .= "@"; |
| 145 | } |
| 146 | if(isset($parts['host'])) { |
| 147 | $url .= $parts['host']."/"; |
| 148 | } |
| 149 | $url .= $path; |
| 150 | |
| 151 | return $url; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | /** |
| 156 | * blcUtility::urlencodefix() |
| 157 | * Takes an URL and replaces spaces and some other non-alphanumeric characters with their urlencoded equivalents. |
| 158 | * |
| 159 | * @param string $str |
| 160 | * @return string |
| 161 | */ |
| 162 | function urlencodefix($url){ |
| 163 | return preg_replace_callback( |
| 164 | '|[^a-z0-9\+\-\/\\#:.=?&%@]|i', |
| 165 | create_function('$str','return rawurlencode($str[0]);'), |
| 166 | $url |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | }//class |
| 171 | |
| 172 | }//class_exists |
| 173 | |
| 174 | ?> |