PluginProbe
CryptX / 3.2.16
CryptX v3.2.16
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 / functions.php

functions.php in CryptX 3.2.16, at functions.php

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