PluginProbe
CryptX / 3.3.3
CryptX v3.3.3
4.2.0 4.1.1 trunk 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.9 2.0 2.1 2.2 2.3 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 All 92 releases
cryptx / include / functions.php

functions.php in CryptX 3.3.3, at include/functions.php

544 lines 17.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Don't load this file direct!
4 */
5 if (!defined('ABSPATH')) {
6 return ;
7 }
8
9 /**
10 * Define some global variables
11 */
12 $cryptx_imageCounter = 0;
13
14 /**
15 * Set settings-defaults and return as array
16 *
17 * @return array
18 */
19 function rw_Defaults() {
20 $firstFont = rw_cryptx_listDir(CRYPTX_DIR_PATH.'fonts', "ttf");
21 $defaults = array(
22 'version' => CRYPTX_VERSION,
23 'at' => ' [at] ',
24 'dot' => ' [dot] ',
25 'css_id' => '',
26 'css_class' => '',
27 'the_content' => 1,
28 'the_meta_key' => 1,
29 'the_excerpt' => 1,
30 'comment_text' => 1,
31 'widget_text' => 1,
32 'java' => 1,
33 'load_java' => 1,
34 'opt_linktext' => 0,
35 'autolink' => 1,
36 'alt_linktext' => '',
37 'alt_linkimage' => '',
38 'http_linkimage_title' => '',
39 'alt_linkimage_title' => '',
40 'excludedIDs' => '',
41 'metaBox' => 1,
42 'alt_uploadedimage' => '0',
43 'c2i_font' => $firstFont[0],
44 'c2i_fontSize' => 10,
45 'c2i_fontRGB' => '#000000',
46 'echo' => 1,
47 'filter' => array('the_content','the_meta_key','the_excerpt','comment_text','widget_text'),
48 'whiteList' => 'jpeg,jpg,png,gif',
49 );
50 return $defaults;
51 }
52
53 /**
54 * Loading defaults and parse with given seetings
55 *
56 * @param array $options given settings that should be completed with defaults
57 *
58 * @return array
59 */
60 function rw_loadDefaults($options='') {
61 $return = wp_parse_args( get_option('cryptX'), rw_Defaults() );
62 $return = (is_array($options))? wp_parse_args( $options, $return ) : $return ;
63
64 return $return;
65 }
66
67 /**
68 * Add support for Shortcode
69 *
70 * @param array $atts options
71 * @param string $content content that should be encrypted
72 *
73 * @return string
74 */
75 function rw_cryptx_shortcode( $atts, $content=null) {
76 global $cryptX_var;
77
78 if (@$cryptX_var[autolink]) $content = rw_cryptx_autolink($content, true);
79 $content = rw_cryptx_encryptx($content, true);
80 $content = rw_cryptx_linktext($content, true);
81
82 return $content;
83 }
84
85 /**
86 * create image from tinyurl
87 *
88 * @return image
89 */
90 function rw_cryptx_init_tinyurl() {
91 $cryptX_var = rw_loadDefaults();
92 $url = $_SERVER['REQUEST_URI'];
93 $params = explode( '/', $url );
94 if ( count( $params ) > 1 ) {
95 $tiny_url = $params[count( $params ) -2];
96 if ( $tiny_url == md5( get_bloginfo('url') ) ) {
97 $font = CRYPTX_DIR_PATH . 'fonts/' . $cryptX_var['c2i_font'];
98 $msg = $params[count( $params ) -1];
99 $size = $cryptX_var['c2i_fontSize'];
100 $pad = 1;
101 $transparent = 1;
102 $rgb = str_replace("#", "", $cryptX_var['c2i_fontRGB']);
103 $red = hexdec(substr($rgb,0,2));
104 $grn = hexdec(substr($rgb,2,2));
105 $blu = hexdec(substr($rgb,4,2));
106 $bg_red = 255 - $red;
107 $bg_grn = 255 - $grn;
108 $bg_blu = 255 - $blu;
109 $width = 0;
110 $height = 0;
111 $offset_x = 0;
112 $offset_y = 0;
113 $bounds = array();
114 $image = "";
115 $bounds = ImageTTFBBox($size, 0, $font, "W");
116 $font_height = abs($bounds[7]-$bounds[1]);
117 $bounds = ImageTTFBBox($size, 0, $font, $msg);
118 $width = abs($bounds[4]-$bounds[6]);
119 $height = abs($bounds[7]-$bounds[1]);
120 $offset_y = $font_height+abs(($height - $font_height)/2)-1;
121 $offset_x = 0;
122 $image = imagecreatetruecolor($width+($pad*2),$height+($pad*2));
123 imagesavealpha($image, true);
124 $foreground = ImageColorAllocate($image, $red, $grn, $blu);
125 $background = imagecolorallocatealpha($image, 0, 0, 0, 127);
126 imagefill($image, 0, 0, $background);
127 ImageTTFText($image, $size, 0, $offset_x+$pad, $offset_y+$pad, $foreground, $font, $msg);
128 Header("Content-type: image/png");
129 imagePNG($image);
130 die;
131 }
132 }
133 }
134
135 /**
136 * acivate needed filter
137 *
138 * @param string $apply wordpress filter hook
139 *
140 * @return nothing
141 */
142 function rw_cryptx_filter($apply) {
143 global $cryptX_var, $shortcode_tags;
144 if (@$cryptX_var['autolink']) {
145 add_filter($apply, 'rw_cryptx_autolink', 5);
146 if (!empty($shortcode_tags) || is_array($shortcode_tags)) {
147 add_filter($apply, 'rw_cryptx_autolink', 11);
148 }
149 }
150 add_filter($apply, 'rw_cryptx_encryptx', 12);
151 add_filter($apply, 'rw_cryptx_linktext', 13);
152 }
153
154 /**
155 * check if given ID is excuded from CryptX
156 *
157 * @param int $ID post/page ID
158 *
159 * @return bool
160 */
161 function rw_cryptx_excluded($ID) {
162 global $cryptX_var;
163 $return = false;
164 $exIDs = explode(",", $cryptX_var['excludedIDs']);
165 if(in_array($ID, $exIDs) > 0 ) $return = true;
166 return $return;
167 }
168
169 /**
170 * search for link texts
171 *
172 * @param string $content content given by post/page
173 * @param bool $shortcode overwrite exclude list if call from shortcode
174 *
175 * @return string
176 */
177 function rw_cryptx_linktext($content, $shortcode=false) {
178 global $post;
179 $postID = (is_object($post))? $post->ID : -1;
180 if (!rw_cryptx_excluded($postID) OR $shortcode!=false) {
181 $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 );
182 }
183 return $content;
184 }
185
186 /**
187 * replace linktexts
188 */
189 function rw_cryptx_do_linktext($Match) {
190 global $cryptX_var, $cryptx_imageCounter;
191 $vars = $cryptX_var;
192 // workaround for the retina issue with @-symbol in filename like xxx-@2x.jpg
193 $whiteList = array_filter(array_map('trim', explode(",", $vars['whiteList']) ));
194 $tmp = explode(".", $Match[0]);
195 if(in_array( end($tmp) , $whiteList ) ) return $Match[1];
196 unset($tmp);
197 switch ($vars['opt_linktext']) {
198
199 case 1: // alternative text for mail link
200 $linktext = $vars['alt_linktext'];
201 break;
202
203 case 2: // alternative image for mail link
204 $linktext = "<img src=\"" . $vars['alt_linkimage'] . "\" class=\"cryptxImage\" alt=\"" . $vars['alt_linkimage_title'] . "\" title=\"" . antispambot($vars['alt_linkimage_title']) . "\" />";
205 break;
206
207 case 3: // uploaded image for mail link
208 $imgurl = wp_get_attachment_url( $vars['alt_uploadedimage'] );
209 $linktext = "<img src=\"" . $imgurl . "\" class=\"cryptxImage cryptxImage_" . $cryptx_imageCounter . "\" alt=\"" . $vars['http_linkimage_title'] . " title=\"" .antispambot( $vars['http_linkimage_title']) . "\" />";
210 $cryptx_imageCounter++;
211 break;
212
213 case 4: // text scrambled by antispambot
214 $linktext = antispambot($Match[1]);
215 break;
216
217 case 5: // convert to image
218 $linktext = "<img src=\"" . get_bloginfo('url') . "/" . md5( get_bloginfo('url') ) . "/" . antispambot($Match[1]) . "\" class=\"cryptxImage cryptxImage_" . $cryptx_imageCounter . "\" alt=\"" . antispambot($Match[1]) . "\" title=\"" . antispambot($Match[1]) . "\" />";
219 $cryptx_imageCounter++;
220 break;
221
222 default:
223 $linktext = str_replace( "@", $vars['at'], $Match[1]);
224 $linktext = str_replace( ".", $vars['dot'], $linktext);
225
226 }
227 return $linktext;
228 }
229
230 /**
231 * get filtered content of directory
232 */
233 function rw_cryptx_listDir( $path, $filter) {
234 if(!is_array($filter)) $filter = (array)$filter;
235 $fh = opendir($path);
236 $verzeichnisinhalt = array();
237 while (true == ($file = readdir($fh)))
238 {
239 if ( in_array( substr( strtolower($file), -3), $filter ))
240 {
241 $verzeichnisinhalt[] = $file;
242 }
243 }
244 return $verzeichnisinhalt;
245 }
246
247 /**
248 * search for mailto tags
249 */
250 function rw_cryptx_encryptx($content, $shortcode=false) {
251 global $post;
252 $postID = (is_object($post))? $post->ID : -1;
253
254 if (!rw_cryptx_excluded($postID) OR $shortcode!=false) {
255 $content = preg_replace_callback('/<a (.*?)(href=("|\')mailto:(.*?)("|\')(.*?)|)>\s*(.*?)\s*<\/a>/i', 'rw_cryptx_mailtocrypt', $content );
256 }
257 return $content;
258 }
259
260 /**
261 * encryptx adresses with javascript
262 */
263 function rw_cryptx_mailtocrypt($Match) {
264 global $cryptX_var;
265 $return = $Match[0];
266 if(strpos($Match[4], '@') === false) return $return; // Do nothing if no email found, like mailto-links from Shariff.
267 $mailto = "mailto:" . $Match[4];
268 if (substr($Match[4], 0, 9) =="?subject=") return $return;
269 if (@$cryptX_var['java']) {
270 $javascript="javascript:DeCryptX('" . rw_cryptx_generate_hash($Match[4]) . "')";
271 $return = str_replace( "mailto:".$Match[4], $javascript, $return);
272 } else {
273 $return = str_replace( $mailto, antispambot($mailto), $return);
274 }
275 if(!empty($cryptX_var['css_id'])) {
276 $return = preg_replace( '/(.*)(">)/i', '$1" id="'.$cryptX_var['css_id'].'">', $return );
277 }
278 if(!empty($cryptX_var['css_class'])) {
279 $return = preg_replace( '/(.*)(">)/i', '$1" class="'.$cryptX_var['css_class'].'">', $return );
280 }
281 return $return;
282 }
283
284 /**
285 * generate the unique hash
286 */
287 function rw_cryptx_generate_hash($string) {
288 $string = str_replace("&", "&", $string);
289 $blacklist = array(
290 '32', // Space
291 '34', // Double quote
292 '39', // Single quote
293 '60', // Less than
294 '62', // Greater than
295 '63', // Question mark
296 '92', // Backslash
297 '94', // Caret - circumflex
298 '96', // Grave accent
299 '127', // Delete
300 );
301 $crypt = '';
302 $ascii = 0;
303
304 for ($i = 0; $i < strlen( $string ); $i++) {
305
306 do {
307 $salt = mt_rand(0, 3);
308 $ascii = ord ( substr ( $string, $i ) ) + $salt;
309 if (8364 <= $ascii) {
310 $ascii = 128;
311 }
312
313 } while ( in_array($ascii, $blacklist) ); // blacklisted chars are impossible for hash! retry with new random...
314
315 $crypt .= $salt.chr($ascii);
316 }
317 return $crypt;
318 }
319
320 /**
321 * add link to email adresses
322 */
323 function rw_cryptx_autolink($content, $shortcode=false) {
324 global $post;
325 $postID = (is_object($post))? $post->ID : -1;
326 if (rw_cryptx_excluded($postID) AND $shortcode==false) return $content;
327 $src[]="/([\s])([_a-zA-Z0-9-+]+(\.[_a-zA-Z0-9-+]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,}))/si";
328 $src[]="/(>)([_a-zA-Z0-9-+]+(\.[_a-zA-Z0-9-+]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,}))(<)/si";
329 $src[]="/(\()([_a-zA-Z0-9-+]+(\.[_a-zA-Z0-9-+]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,}))(\))/si";
330 $src[]="/(>)([_a-zA-Z0-9-+]+(\.[_a-zA-Z0-9-+]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,}))([\s])/si";
331 $src[]="/([\s])([_a-zA-Z0-9-+]+(\.[_a-zA-Z0-9-+]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,}))(<)/si";
332 $src[]="/^([_a-zA-Z0-9-+]+(\.[_a-zA-Z0-9-+]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,}))/si";
333 $src[]="/(<a[^>]*>)<a[^>]*>/";
334 $src[]="/(<\/A>)<\/A>/i";
335
336 $tar[]="\\1<a href=\"mailto:\\2\">\\2</a>";
337 $tar[]="\\1<a href=\"mailto:\\2\">\\2</a>\\6";
338 $tar[]="\\1<a href=\"mailto:\\2\">\\2</a>\\6";
339 $tar[]="\\1<a href=\"mailto:\\2\">\\2</a>\\6";
340 $tar[]="\\1<a href=\"mailto:\\2\">\\2</a>\\6";
341 $tar[]="<a href=\"mailto:\\0\">\\0</a>";
342 $tar[]="\\1";
343 $tar[]="\\1";
344 $content = preg_replace($src,$tar,$content);
345 return $content;
346 }
347
348 /**
349 * needed stuff for install process
350 */
351 function rw_cryptx_install() {
352 global $cryptX_var, $wpdb;
353 $cryptX_var = rw_loadDefaults(); // reread Options
354 $cryptX_var['admin_notices_deprecated']=true;
355 if ($cryptX_var['excludedIDs'] == "") {
356 $tmp = array();
357 $excludes = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'cryptxoff' AND meta_value = 'true'");
358 if(count($excludes) > 0) {
359 foreach ($excludes as $exclude) {
360 $tmp[] = $exclude->post_id;
361 }
362 sort($tmp);
363 $cryptX_var['excludedIDs'] = implode(",", $tmp);
364 update_option( 'cryptX', $cryptX_var);
365 $cryptX_var = rw_loadDefaults(); // reread Options
366 $wpdb->query("DELETE FROM $wpdb->postmeta WHERE meta_key = 'cryptxoff'");
367 }
368 }
369 if (empty($cryptX_var['c2i_font'])) {
370 $cryptX_var['c2i_font'] = CRYPTX_DIR_PATH . 'fonts/' . $firstFont[0];
371 }
372 if (empty($cryptX_var['c2i_fontSize'])) {
373 $cryptX_var['c2i_fontSize'] = 10;
374 }
375 if (empty($cryptX_var['c2i_fontRGB'])) {
376 $cryptX_var['c2i_fontRGB'] = '000000';
377 }
378 update_option( 'cryptX', $cryptX_var);
379 $cryptX_var = rw_loadDefaults(); // reread Options
380 }
381
382 /**
383 * add support for CryptX MetaBox
384 */
385 function rw_cryptx_meta_box() {
386 if ( function_exists('add_meta_box') ) {
387 add_meta_box('cryptx','CryptX', 'rw_cryptx_meta','post');
388 add_meta_box('cryptx','CryptX', 'rw_cryptx_meta','page');
389 } else {
390 add_action('dbx_post_sidebar', 'rw_cryptx_option');
391 add_action('dbx_page_sidebar', 'rw_cryptx_option');
392 }
393 }
394
395 /**
396 * the MetaBox new-style
397 */
398 function rw_cryptx_meta() {
399 global $post;
400 ?>
401 <input type="checkbox" name="cryptxoff" <?php if (rw_cryptx_excluded($post->ID)) { echo 'checked="checked"'; } ?>/> Disable CryptX for this post/page
402 <?php
403 }
404
405 /**
406 * the MetaBox old-style
407 */
408 function rw_cryptx_option() {
409 global $post;
410 if ( current_user_can('edit_posts') ) { ?>
411 <fieldset id="cryptxoption" class="dbx-box">
412 <h3 class="dbx-handle">CryptX</h3>
413 <div class="dbx-content">
414 <input type="checkbox" name="cryptxoff" <?php if (rw_cryptx_excluded($post->ID)) { echo 'checked="checked"'; } ?>/> Disable CryptX for this post/page
415 </div>
416 </fieldset>
417 <?php
418 }
419 }
420
421 /**
422 * add ID to exclude list
423 */
424 function rw_cryptx_insert_post($pID) {
425 global $cryptX_var, $post;
426 $rev = wp_is_post_revision($pID);
427 if($rev) $pID = $rev;
428 $b = explode(",", $cryptX_var['excludedIDs']);
429 if($b[0] == '') unset($b[0]);
430 foreach($b as $x=>$y) {
431 if($y == $pID) {
432 unset($b[$x]);
433 break;
434 }
435 }
436 if (isset($_POST['cryptxoff'])) $b[] = $pID;
437 $b = array_unique($b);
438 sort($b);
439 $cryptX_var['excludedIDs'] = implode(",", $b);
440 update_option( 'cryptX', $cryptX_var);
441 $cryptX_var = rw_loadDefaults(); // reread Options
442 }
443
444 /**
445 * print admin notice
446 */
447 function rw_cryptx_showMessage($message, $errormsg = false)
448 {
449 if ($errormsg) {
450 echo '<div id="message" class="error">';
451 }
452 else {
453 echo '<div id="message" class="updated fade">';
454 }
455
456 echo "$message</div>";
457 }
458
459 function rw_cryptx_getDomain()
460 {
461 $domain = preg_replace( '|https?://|', '', get_option( 'siteurl' ) );
462 if ( $slash = strpos( $domain, '/' ) ) {
463 $domain = substr( $domain, 0, $slash );
464 }
465 return $domain;
466 }
467
468 /**
469 * New Template functions...
470 * $content = string to convert
471 * $args = array with options
472 */
473 function encryptx( $content, $args="" ) {
474 global $cryptX_var;
475 $args = (array) $args;
476 $cryptX_var = wp_parse_args( $args, $cryptX_var);
477 $return = do_shortcode('[cryptx]'.$content.'[/cryptx]');
478 $cryptX_var = rw_loadDefaults();
479 return $return;
480 }
481
482 /**
483 * Template function that encrypt the get_post_meta result
484 * call it with the default get_post_meta parameters
485 */
486 function get_encryptx_meta( $post_id, $key, $single=false ) {
487
488 $values = get_post_meta( $post_id, $key, $single );
489
490 if(is_array($values)) {
491 $return = array();
492 foreach( $values as $value) {
493 $return[] = encryptx($value);
494 }
495 } else {
496 $return = encryptx($values);
497 }
498
499 return $return;
500
501 }
502
503 /**
504 * get CryptX Version
505 */
506 function rw_cryptx_version() {
507 if ( ! function_exists( 'get_plugins' ) )
508 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
509 $plugin_folder = get_plugins( '/' . plugin_basename( dirname( __FILE__ ) ) );
510 $plugin_file = CRYPTX_BASENAME;
511 return $plugin_folder[$plugin_file]['Version'];
512 }
513
514 /**
515 * Enqueue CryptX Frontend JavaScript
516 */
517 function cryptx_javascripts_load() {
518 global $cryptX_var;
519 wp_enqueue_script( 'cryptx-js', CRYPTX_DIR_URL . '/js/cryptx.min.js', false, false, $cryptX_var['load_java'] );
520 wp_enqueue_style( 'cryptx-styles', CRYPTX_DIR_URL . '/css/cryptx.css');
521 }
522
523 /**
524 * Do needed settings updates after new CryptX version is installed
525 */
526 function cryptx_do_updates() {
527 $cryptX_var = get_option('cryptX');
528 if( version_compare(CRYPTX_VERSION, $cryptX_var['version']) > 0 ) {
529 // Update settings version
530 if( isset($cryptX_var['version']) ) unset($cryptX_var['version']);
531 // reset Font to new Font selection
532 if( isset($cryptX_var['c2i_font']) ) unset($cryptX_var['c2i_font']);
533 // reset font color to new color picker value format
534 if( isset($cryptX_var['c2i_fontRGB']) ) $cryptX_var['c2i_fontRGB'] = "#".$cryptX_var['c2i_fontRGB'];
535 // reset uploaded image setting to the WP MediaLibrary requirement
536 if( isset($cryptX_var['alt_uploadedimage']) && !is_int($cryptX_var['alt_uploadedimage'])) {
537 unset($cryptX_var['alt_uploadedimage']);
538 // reset link option to default if uploaded image was selected
539 if( $cryptX_var['opt_linktext'] == 3 ) unset($cryptX_var['opt_linktext']);
540 }
541 $cryptX_var = wp_parse_args( $cryptX_var, rw_Defaults() );
542 update_option( 'cryptX', $cryptX_var);
543 }
544 }