| 1 |
<?php |
| 2 |
/** |
| 3 |
* CryptX functions |
| 4 |
**/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Don't load this file direct! |
| 8 |
*/ |
| 9 |
if (!defined('ABSPATH')) { |
| 10 |
return ; |
| 11 |
} |
| 12 |
|
| 13 |
/* |
| 14 |
* Loading defaults |
| 15 |
*/ |
| 16 |
function rw_loadDefaults($options='') { |
| 17 |
$firstImage = rw_cryptx_dirImages(); |
| 18 |
$firstFont = rw_cryptx_dirFonts(); |
| 19 |
$defaults = array( |
| 20 |
'at' => ' [at] ', |
| 21 |
'dot' => ' [dot] ', |
| 22 |
'css_id' => '', |
| 23 |
'css_class' => '', |
| 24 |
'the_content' => 1, |
| 25 |
'the_meta_key' => 1, |
| 26 |
'the_excerpt' => 1, |
| 27 |
'comment_text' => 1, |
| 28 |
'widget_text' => 1, |
| 29 |
'java' => 1, |
| 30 |
'load_java' => 0, |
| 31 |
'opt_linktext' => 0, |
| 32 |
'autolink' => 1, |
| 33 |
'alt_linktext' => '', |
| 34 |
'alt_linkimage' => '', |
| 35 |
'http_linkimage_title' => '', |
| 36 |
'alt_linkimage_title' => '', |
| 37 |
'excludedIDs' => '', |
| 38 |
'metaBox' => 1, |
| 39 |
'alt_uploadedimage' => plugins_url('cryptx/images/').$firstImage[0], |
| 40 |
'c2i_font' => plugin_dir_path( __FILE__ ).'fonts/'.$firstFont[0], |
| 41 |
'c2i_fontSize' => 10, |
| 42 |
'c2i_fontRGB' => '000000', |
| 43 |
'echo' => 1, |
| 44 |
'filter' => array('the_content','the_meta_key','the_excerpt','comment_text','widget_text') |
| 45 |
); |
| 46 |
$return = wp_parse_args( get_option('cryptX'), $defaults ); |
| 47 |
$return = (is_array($options))? wp_parse_args( $options, $return ) : $return ; |
| 48 |
|
| 49 |
return $return; |
| 50 |
} |
| 51 |
|
| 52 |
/* |
| 53 |
* Add support for Shortcode |
| 54 |
*/ |
| 55 |
function rw_cryptx_shortcode( $atts, $content=null) { |
| 56 |
global $cryptX_var; |
| 57 |
|
| 58 |
if (@$cryptX_var[autolink]) $content = rw_cryptx_autolink($content, true); |
| 59 |
$content = rw_cryptx_encryptx($content, true); |
| 60 |
$content = rw_cryptx_linktext($content, true); |
| 61 |
|
| 62 |
return $content; |
| 63 |
} |
| 64 |
|
| 65 |
/* |
| 66 |
* load CryptX news |
| 67 |
*/ |
| 68 |
function rw_cryptx_parse_request( $wp ) { |
| 69 |
if ( isset($_GET['cryptx']) ) { |
| 70 |
switch( $_GET['cryptx'] ) { |
| 71 |
case 'news': |
| 72 |
include( 'ajax/news.php' ); |
| 73 |
break; |
| 74 |
} |
| 75 |
exit; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/* |
| 80 |
* creyte image from tinyurl |
| 81 |
*/ |
| 82 |
function rw_cryptx_init_tinyurl() { |
| 83 |
global $cryptX_var; |
| 84 |
$url = $_SERVER['REQUEST_URI']; |
| 85 |
$params = explode( '/', $url ); |
| 86 |
if ( count( $params ) > 1 ) { |
| 87 |
$tiny_url = $params[count( $params ) -2]; |
| 88 |
if ( $tiny_url == md5( get_bloginfo('url') ) ) { |
| 89 |
$font = $cryptX_var['c2i_font']; |
| 90 |
$msg = $params[count( $params ) -1]; |
| 91 |
$size = $cryptX_var['c2i_fontSize']; |
| 92 |
$pad = 1; |
| 93 |
$transparent = 1; |
| 94 |
$red = hexdec(substr($cryptX_var['c2i_fontRGB'],0,2)); |
| 95 |
$grn = hexdec(substr($cryptX_var['c2i_fontRGB'],2,2)); |
| 96 |
$blu = hexdec(substr($cryptX_var['c2i_fontRGB'],4,2)); |
| 97 |
$bg_red = 255 - $red; |
| 98 |
$bg_grn = 255 - $grn; |
| 99 |
$bg_blu = 255 - $blu; |
| 100 |
$width = 0; |
| 101 |
$height = 0; |
| 102 |
$offset_x = 0; |
| 103 |
$offset_y = 0; |
| 104 |
$bounds = array(); |
| 105 |
$image = ""; |
| 106 |
$bounds = ImageTTFBBox($size, 0, $font, "W"); |
| 107 |
$font_height = abs($bounds[7]-$bounds[1]); |
| 108 |
$bounds = ImageTTFBBox($size, 0, $font, $msg); |
| 109 |
$width = abs($bounds[4]-$bounds[6]); |
| 110 |
$height = abs($bounds[7]-$bounds[1]); |
| 111 |
$offset_y = $font_height+abs(($height - $font_height)/2)-1; |
| 112 |
$offset_x = 0; |
| 113 |
$image = imagecreatetruecolor($width+($pad*2),$height+($pad*2)); |
| 114 |
imagesavealpha($image, true); |
| 115 |
$foreground = ImageColorAllocate($image, $red, $grn, $blu); |
| 116 |
$background = imagecolorallocatealpha($image, 0, 0, 0, 127); |
| 117 |
imagefill($image, 0, 0, $background); |
| 118 |
ImageTTFText($image, $size, 0, $offset_x+$pad, $offset_y+$pad, $foreground, $font, $msg); |
| 119 |
Header("Content-type: image/png"); |
| 120 |
imagePNG($image); |
| 121 |
die; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/* |
| 127 |
* acivate needed filter |
| 128 |
*/ |
| 129 |
function rw_cryptx_filter($apply) { |
| 130 |
global $cryptX_var, $shortcode_tags; |
| 131 |
if (@$cryptX_var['autolink']) { |
| 132 |
add_filter($apply, 'rw_cryptx_autolink', 5); |
| 133 |
if (!empty($shortcode_tags) || is_array($shortcode_tags)) { |
| 134 |
add_filter($apply, 'rw_cryptx_autolink', 11); |
| 135 |
} |
| 136 |
} |
| 137 |
add_filter($apply, 'rw_cryptx_encryptx', 12); |
| 138 |
add_filter($apply, 'rw_cryptx_linktext', 13); |
| 139 |
} |
| 140 |
|
| 141 |
/* |
| 142 |
* check for excuded IDs |
| 143 |
*/ |
| 144 |
function rw_cryptx_excluded($ID) { |
| 145 |
global $cryptX_var; |
| 146 |
$return = false; |
| 147 |
$exIDs = explode(",", $cryptX_var['excludedIDs']); |
| 148 |
if(in_array($ID, $exIDs) > 0 ) $return = true; |
| 149 |
return $return; |
| 150 |
} |
| 151 |
|
| 152 |
/* |
| 153 |
* search for link texts |
| 154 |
*/ |
| 155 |
function rw_cryptx_linktext($content, $shortcode=false) { |
| 156 |
global $post; |
| 157 |
$postID = (is_object($post))? $post->ID : -1; |
| 158 |
if (!rw_cryptx_excluded($postID) OR $shortcode!=false) { |
| 159 |
$content = preg_replace_callback("/([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,}))/i", 'rw_cryptx_do_Linktext', $content ); |
| 160 |
} |
| 161 |
return $content; |
| 162 |
} |
| 163 |
|
| 164 |
/* |
| 165 |
* replace linktexts |
| 166 |
*/ |
| 167 |
function rw_cryptx_do_linktext($Match) { |
| 168 |
global $cryptX_var; |
| 169 |
$vars = $cryptX_var; |
| 170 |
switch ($vars['opt_linktext']) { |
| 171 |
|
| 172 |
case 1: // alternative text for mail link |
| 173 |
$linktext = $vars['alt_linktext']; |
| 174 |
break; |
| 175 |
|
| 176 |
case 2: // alternative image for mail link |
| 177 |
$linktext = "<img src=\"" . $vars['alt_linkimage'] . "\" class=\"cryptxImage\" alt=\"" . $vars['alt_linkimage_title'] . "\" title=\"" . antispambot($vars['alt_linkimage_title']) . "\" />"; |
| 178 |
break; |
| 179 |
|
| 180 |
case 3: // uploaded image for mail link |
| 181 |
$imgurl = $vars['alt_uploadedimage']; |
| 182 |
$linktext = "<img src=\"" . $imgurl . "\" class=\"cryptxImage\" alt=\"" . $vars['http_linkimage_title'] . "\" title=\"" .antispambot( $vars['http_linkimage_title']) . "\" />"; |
| 183 |
break; |
| 184 |
|
| 185 |
case 4: // text scrambled by antispambot |
| 186 |
$linktext = antispambot($Match[1]); |
| 187 |
break; |
| 188 |
|
| 189 |
case 5: // convert to image |
| 190 |
$linktext = "<img src=\"" . get_bloginfo('url') . "/" . md5( get_bloginfo('url') ) . "/" . antispambot($Match[1]) . "\" class=\"cryptxImage\" alt=\"" . antispambot($Match[1]) . "\" title=\"" . antispambot($Match[1]) . "\" />"; |
| 191 |
break; |
| 192 |
|
| 193 |
default: |
| 194 |
$linktext = str_replace( "@", $vars['at'], $Match[1]); |
| 195 |
$linktext = str_replace( ".", $vars['dot'], $linktext); |
| 196 |
|
| 197 |
} |
| 198 |
return $linktext; |
| 199 |
} |
| 200 |
|
| 201 |
/* |
| 202 |
* show images from dir |
| 203 |
*/ |
| 204 |
function rw_cryptx_dirImages() { |
| 205 |
$dir = plugin_dir_path( __FILE__ ).'images'; |
| 206 |
$fh = opendir($dir); |
| 207 |
$verzeichnisinhalt = array(); |
| 208 |
while (true == ($file = readdir($fh))) |
| 209 |
{ |
| 210 |
if ((substr(strtolower($file), -3)=="jpg") or (substr(strtolower($file), -3)=="gif")) |
| 211 |
{ |
| 212 |
$verzeichnisinhalt[] = $file; |
| 213 |
} |
| 214 |
} |
| 215 |
return $verzeichnisinhalt; |
| 216 |
} |
| 217 |
|
| 218 |
/* |
| 219 |
* show fonts from dir |
| 220 |
*/ |
| 221 |
function rw_cryptx_dirFonts() { |
| 222 |
$dir = plugin_dir_path( __FILE__ ).'fonts'; |
| 223 |
$fh = opendir($dir); |
| 224 |
$verzeichnisinhalt = array(); |
| 225 |
while (true == ($file = readdir($fh))) |
| 226 |
{ |
| 227 |
if ((substr(strtolower($file), -3)=="ttf") or (substr(strtolower($file), -3)=="ttf")) |
| 228 |
{ |
| 229 |
$verzeichnisinhalt[] = $file; |
| 230 |
} |
| 231 |
} |
| 232 |
return $verzeichnisinhalt; |
| 233 |
} |
| 234 |
|
| 235 |
/* |
| 236 |
* search for mailto tags |
| 237 |
*/ |
| 238 |
function rw_cryptx_encryptx($content, $shortcode=false) { |
| 239 |
global $post; |
| 240 |
$postID = (is_object($post))? $post->ID : -1; |
| 241 |
|
| 242 |
if (!rw_cryptx_excluded($postID) OR $shortcode!=false) { |
| 243 |
$content = preg_replace_callback('/<a (.*?)(href=("|\')mailto:(.*?)("|\')(.*?)|)>(.*?)<\/a>/i', 'rw_cryptx_mailtocrypt', $content ); |
| 244 |
} |
| 245 |
return $content; |
| 246 |
} |
| 247 |
|
| 248 |
/* |
| 249 |
* encryptx adresses with javascript |
| 250 |
*/ |
| 251 |
function rw_cryptx_mailtocrypt($Match) { |
| 252 |
global $cryptX_var; |
| 253 |
$return = $Match[0]; |
| 254 |
$mailto = "mailto:" . $Match[4]; |
| 255 |
if (substr($Match[4], 0, 9) =="?subject=") return $return; |
| 256 |
if (@$cryptX_var['java']) { |
| 257 |
$crypt = ''; |
| 258 |
$ascii = 0; |
| 259 |
for ($i = 0; $i < strlen( $Match[4] ); $i++) { |
| 260 |
$ascii = ord ( substr ( $Match[4], $i ) ); |
| 261 |
if (8364 <= $ascii) { |
| 262 |
$ascii = 128; |
| 263 |
} |
| 264 |
$crypt .= chr($ascii + 1); |
| 265 |
} |
| 266 |
$javascript="javascript:DeCryptX('" . $crypt . "')"; |
| 267 |
$return = str_replace( "mailto:".$Match[4], $javascript, $return); |
| 268 |
} else { |
| 269 |
$return = str_replace( $mailto, antispambot($mailto), $return); |
| 270 |
} |
| 271 |
if(!empty($cryptX_var['css_id'])) { |
| 272 |
$return = preg_replace( '/(.*)(">)/i', '$1" id="'.$cryptX_var['css_id'].'">', $return ); |
| 273 |
} |
| 274 |
if(!empty($cryptX_var['css_class'])) { |
| 275 |
$return = preg_replace( '/(.*)(">)/i', '$1" class="'.$cryptX_var['css_class'].'">', $return ); |
| 276 |
} |
| 277 |
return $return; |
| 278 |
} |
| 279 |
|
| 280 |
/* |
| 281 |
* add link to email adresses |
| 282 |
*/ |
| 283 |
function rw_cryptx_autolink($content, $shortcode=false) { |
| 284 |
global $post; |
| 285 |
$postID = (is_object($post))? $post->ID : -1; |
| 286 |
if (rw_cryptx_excluded($postID) AND $shortcode==false) return $content; |
| 287 |
$src[]="/([\s])([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,}))/si"; |
| 288 |
$src[]="/(>)([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,}))(<)/si"; |
| 289 |
$src[]="/(\()([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,}))(\))/si"; |
| 290 |
$src[]="/(>)([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,}))([\s])/si"; |
| 291 |
$src[]="/([\s])([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,}))(<)/si"; |
| 292 |
$src[]="/^([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,}))/si"; |
| 293 |
$src[]="/(<a[^>]*>)<a[^>]*>/"; |
| 294 |
$src[]="/(<\/A>)<\/A>/i"; |
| 295 |
|
| 296 |
$tar[]="\\1<a href=\"mailto:\\2\">\\2</a>"; |
| 297 |
$tar[]="\\1<a href=\"mailto:\\2\">\\2</a>\\6"; |
| 298 |
$tar[]="\\1<a href=\"mailto:\\2\">\\2</a>\\6"; |
| 299 |
$tar[]="\\1<a href=\"mailto:\\2\">\\2</a>\\6"; |
| 300 |
$tar[]="\\1<a href=\"mailto:\\2\">\\2</a>\\6"; |
| 301 |
$tar[]="<a href=\"mailto:\\0\">\\0</a>"; |
| 302 |
$tar[]="\\1"; |
| 303 |
$tar[]="\\1"; |
| 304 |
$content = preg_replace($src,$tar,$content); |
| 305 |
return $content; |
| 306 |
} |
| 307 |
|
| 308 |
/* |
| 309 |
* needed stuff for install process |
| 310 |
*/ |
| 311 |
function rw_cryptx_install() { |
| 312 |
global $cryptX_var, $wpdb; |
| 313 |
$cryptX_var = rw_loadDefaults(); // reread Options |
| 314 |
$cryptX_var['admin_notices_deprecated']=true; |
| 315 |
if ($cryptX_var['excludedIDs'] == "") { |
| 316 |
$tmp = array(); |
| 317 |
$excludes = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'cryptxoff' AND meta_value = 'true'"); |
| 318 |
if(count($excludes) > 0) { |
| 319 |
foreach ($excludes as $exclude) { |
| 320 |
$tmp[] = $exclude->post_id; |
| 321 |
} |
| 322 |
sort($tmp); |
| 323 |
$cryptX_var['excludedIDs'] = implode(",", $tmp); |
| 324 |
update_option( 'cryptX', $cryptX_var); |
| 325 |
$cryptX_var = rw_loadDefaults(); // reread Options |
| 326 |
$wpdb->query("DELETE FROM $wpdb->postmeta WHERE meta_key = 'cryptxoff'"); |
| 327 |
} |
| 328 |
} |
| 329 |
if (empty($cryptX_var['c2i_font'])) { |
| 330 |
$cryptX_var['c2i_font'] = plugin_dir_path( __FILE__ ).'fonts/'.$firstFont[0]; |
| 331 |
} |
| 332 |
if (empty($cryptX_var['c2i_fontSize'])) { |
| 333 |
$cryptX_var['c2i_fontSize'] = 10; |
| 334 |
} |
| 335 |
if (empty($cryptX_var['c2i_fontRGB'])) { |
| 336 |
$cryptX_var['c2i_fontRGB'] = '000000'; |
| 337 |
} |
| 338 |
update_option( 'cryptX', $cryptX_var); |
| 339 |
$cryptX_var = rw_loadDefaults(); // reread Options |
| 340 |
} |
| 341 |
|
| 342 |
/* |
| 343 |
* add code to site for loading the needed javascript |
| 344 |
*/ |
| 345 |
function rw_cryptx_header() { |
| 346 |
$cryptX_script = "<script type=\"text/javascript\" src=\"" . site_url() . '/' . PLUGINDIR . '/' . dirname(plugin_basename (__FILE__)) . "/js/cryptx.min.js\"></script>\n"; |
| 347 |
print($cryptX_script); |
| 348 |
} |
| 349 |
|
| 350 |
/* |
| 351 |
* add support for CryptX MetaBox |
| 352 |
*/ |
| 353 |
function rw_cryptx_meta_box() { |
| 354 |
if ( function_exists('add_meta_box') ) { |
| 355 |
add_meta_box('cryptx','CryptX', 'rw_cryptx_meta','post'); |
| 356 |
add_meta_box('cryptx','CryptX', 'rw_cryptx_meta','page'); |
| 357 |
} else { |
| 358 |
add_action('dbx_post_sidebar', 'rw_cryptx_option'); |
| 359 |
add_action('dbx_page_sidebar', 'rw_cryptx_option'); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
/* |
| 364 |
* The MetaBox new-style |
| 365 |
*/ |
| 366 |
function rw_cryptx_meta() { |
| 367 |
global $post; |
| 368 |
?> |
| 369 |
<input type="checkbox" name="cryptxoff" <?php if (rw_cryptx_excluded($post->ID)) { echo 'checked="checked"'; } ?>/> Disable CryptX for this post/page |
| 370 |
<?php |
| 371 |
} |
| 372 |
|
| 373 |
/* |
| 374 |
* The MetaBox old-style |
| 375 |
*/ |
| 376 |
function rw_cryptx_option() { |
| 377 |
global $post; |
| 378 |
if ( current_user_can('edit_posts') ) { ?> |
| 379 |
<fieldset id="cryptxoption" class="dbx-box"> |
| 380 |
<h3 class="dbx-handle">CryptX</h3> |
| 381 |
<div class="dbx-content"> |
| 382 |
<input type="checkbox" name="cryptxoff" <?php if (rw_cryptx_excluded($post->ID)) { echo 'checked="checked"'; } ?>/> Disable CryptX for this post/page |
| 383 |
</div> |
| 384 |
</fieldset> |
| 385 |
<?php |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
/* |
| 390 |
* add ID to exclude list |
| 391 |
*/ |
| 392 |
function rw_cryptx_insert_post($pID) { |
| 393 |
global $cryptX_var, $post; |
| 394 |
$rev = wp_is_post_revision($pID); |
| 395 |
if($rev) $pID = $rev; |
| 396 |
$b = explode(",", $cryptX_var['excludedIDs']); |
| 397 |
if($b[0] == '') unset($b[0]); |
| 398 |
foreach($b as $x=>$y) { |
| 399 |
if($y == $pID) { |
| 400 |
unset($b[$x]); |
| 401 |
break; |
| 402 |
} |
| 403 |
} |
| 404 |
if (isset($_POST['cryptxoff'])) $b[] = $pID; |
| 405 |
$b = array_unique($b); |
| 406 |
sort($b); |
| 407 |
$cryptX_var['excludedIDs'] = implode(",", $b); |
| 408 |
update_option( 'cryptX', $cryptX_var); |
| 409 |
$cryptX_var = rw_loadDefaults(); // reread Options |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* print admin notice |
| 414 |
*/ |
| 415 |
function rw_cryptx_showMessage($message, $errormsg = false) |
| 416 |
{ |
| 417 |
if ($errormsg) { |
| 418 |
echo '<div id="message" class="error">'; |
| 419 |
} |
| 420 |
else { |
| 421 |
echo '<div id="message" class="updated fade">'; |
| 422 |
} |
| 423 |
|
| 424 |
echo "$message</div>"; |
| 425 |
} |
| 426 |
|
| 427 |
?> |