broken-link-checker
Last commit date
images
17 years ago
languages
16 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
16 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
242 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 | /** |
| 171 | * blcUtility::is_safe_mode() |
| 172 | * Checks if PHP is running in safe mode |
| 173 | * |
| 174 | * @return bool |
| 175 | */ |
| 176 | function is_safe_mode(){ |
| 177 | $safe_mode = ini_get('safe_mode'); |
| 178 | //Null, 0, '', '0' and so on count as false |
| 179 | if ( !$safe_mode ) return false; |
| 180 | //Test for some textual true/false variations |
| 181 | switch ( strtolower($safe_mode) ){ |
| 182 | case 'on': |
| 183 | case 'true': |
| 184 | case 'yes': |
| 185 | return true; |
| 186 | |
| 187 | case 'off': |
| 188 | case 'false': |
| 189 | case 'no': |
| 190 | return false; |
| 191 | |
| 192 | default: //Let PHP handle anything else |
| 193 | return (bool)(int)$safe_mode; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * blcUtility::is_open_basedir() |
| 199 | * Checks if open_basedir is enabled |
| 200 | * |
| 201 | * @return bool |
| 202 | */ |
| 203 | function is_open_basedir(){ |
| 204 | $open_basedir = ini_get('open_basedir'); |
| 205 | return $open_basedir && ( strtolower($open_basedir) != 'none' ); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * blcUtility::is_writable() |
| 210 | * Check if a file or directory is writable |
| 211 | * |
| 212 | * @param string $path |
| 213 | * @return bool |
| 214 | */ |
| 215 | function is_writable($path){ |
| 216 | //will work in despite of Windows ACLs bug |
| 217 | //NOTE: use a trailing slash for folders!!! |
| 218 | //see http://bugs.php.net/bug.php?id=27609 |
| 219 | //see http://bugs.php.net/bug.php?id=30931 |
| 220 | |
| 221 | if ($path{strlen($path) - 1} == '/') // recursively return a temporary file path |
| 222 | return blcUtility::is_writable($path . uniqid(mt_rand()) . '.tmp'); |
| 223 | else |
| 224 | if (@is_dir($path)) |
| 225 | return blcUtility::is_writable($path . '/' . uniqid(mt_rand()) . '.tmp'); |
| 226 | // check tmp file for read/write capabilities |
| 227 | $rm = @file_exists($path); |
| 228 | $f = @fopen($path, 'a'); |
| 229 | if ($f === false) |
| 230 | return false; |
| 231 | fclose($f); |
| 232 | if (!$rm) |
| 233 | unlink($path); |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | |
| 238 | }//class |
| 239 | |
| 240 | }//class_exists |
| 241 | |
| 242 | ?> |