PluginProbe
CryptX / 3.2.17
CryptX v3.2.17
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.17, at functions.php

435 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("&", "&", $string);
265 $blacklist = array(
266 '32', // Space
267 '34', // Double quote
268 '39', // Single quote
269 '60', // Less than
270 '62', // Greater than
271 '63', // Question mark
272 '92', // Backslash
273 '94', // Caret - circumflex
274 '96', // Grave accent
275 '127', // Delete
276 );
277 $crypt = '';
278 $ascii = 0;
279
280 for ($i = 0; $i < strlen( $string ); $i++) {
281
282 do {
283 $salt = mt_rand(0, 3);
284 $ascii = ord ( substr ( $string, $i ) ) + $salt;
285 if (8364 <= $ascii) {
286 $ascii = 128;
287 }
288
289 } while ( in_array($ascii, $blacklist) ); // blacklisted chars are impossible for hash! retry with new random...
290
291 $crypt .= $salt.chr($ascii);
292 }
293 return $crypt;
294 }
295
296 /*
297 * add link to email adresses
298 */
299 function rw_cryptx_autolink($content, $shortcode=false) {
300 global $post;
301 $postID = (is_object($post))? $post->ID : -1;
302 if (rw_cryptx_excluded($postID) AND $shortcode==false) return $content;
303 $src[]="/([\s])([_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,}))(<)/si";
305 $src[]="/(\()([_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,}))([\s])/si";
307 $src[]="/([\s])([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,}))(<)/si";
308 $src[]="/^([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,}))/si";
309 $src[]="/(<a[^>]*>)<a[^>]*>/";
310 $src[]="/(<\/A>)<\/A>/i";
311
312 $tar[]="\\1<a href=\"mailto:\\2\">\\2</a>";
313 $tar[]="\\1<a href=\"mailto:\\2\">\\2</a>\\6";
314 $tar[]="\\1<a href=\"mailto:\\2\">\\2</a>\\6";
315 $tar[]="\\1<a href=\"mailto:\\2\">\\2</a>\\6";
316 $tar[]="\\1<a href=\"mailto:\\2\">\\2</a>\\6";
317 $tar[]="<a href=\"mailto:\\0\">\\0</a>";
318 $tar[]="\\1";
319 $tar[]="\\1";
320 $content = preg_replace($src,$tar,$content);
321 return $content;
322 }
323
324 /*
325 * needed stuff for install process
326 */
327 function rw_cryptx_install() {
328 global $cryptX_var, $wpdb;
329 $cryptX_var = rw_loadDefaults(); // reread Options
330 $cryptX_var['admin_notices_deprecated']=true;
331 if ($cryptX_var['excludedIDs'] == "") {
332 $tmp = array();
333 $excludes = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'cryptxoff' AND meta_value = 'true'");
334 if(count($excludes) > 0) {
335 foreach ($excludes as $exclude) {
336 $tmp[] = $exclude->post_id;
337 }
338 sort($tmp);
339 $cryptX_var['excludedIDs'] = implode(",", $tmp);
340 update_option( 'cryptX', $cryptX_var);
341 $cryptX_var = rw_loadDefaults(); // reread Options
342 $wpdb->query("DELETE FROM $wpdb->postmeta WHERE meta_key = 'cryptxoff'");
343 }
344 }
345 if (empty($cryptX_var['c2i_font'])) {
346 $cryptX_var['c2i_font'] = plugin_dir_path( __FILE__ ).'fonts/'.$firstFont[0];
347 }
348 if (empty($cryptX_var['c2i_fontSize'])) {
349 $cryptX_var['c2i_fontSize'] = 10;
350 }
351 if (empty($cryptX_var['c2i_fontRGB'])) {
352 $cryptX_var['c2i_fontRGB'] = '000000';
353 }
354 update_option( 'cryptX', $cryptX_var);
355 $cryptX_var = rw_loadDefaults(); // reread Options
356 }
357
358 /*
359 * add support for CryptX MetaBox
360 */
361 function rw_cryptx_meta_box() {
362 if ( function_exists('add_meta_box') ) {
363 add_meta_box('cryptx','CryptX', 'rw_cryptx_meta','post');
364 add_meta_box('cryptx','CryptX', 'rw_cryptx_meta','page');
365 } else {
366 add_action('dbx_post_sidebar', 'rw_cryptx_option');
367 add_action('dbx_page_sidebar', 'rw_cryptx_option');
368 }
369 }
370
371 /*
372 * The MetaBox new-style
373 */
374 function rw_cryptx_meta() {
375 global $post;
376 ?>
377 <input type="checkbox" name="cryptxoff" <?php if (rw_cryptx_excluded($post->ID)) { echo 'checked="checked"'; } ?>/> Disable CryptX for this post/page
378 <?php
379 }
380
381 /*
382 * The MetaBox old-style
383 */
384 function rw_cryptx_option() {
385 global $post;
386 if ( current_user_can('edit_posts') ) { ?>
387 <fieldset id="cryptxoption" class="dbx-box">
388 <h3 class="dbx-handle">CryptX</h3>
389 <div class="dbx-content">
390 <input type="checkbox" name="cryptxoff" <?php if (rw_cryptx_excluded($post->ID)) { echo 'checked="checked"'; } ?>/> Disable CryptX for this post/page
391 </div>
392 </fieldset>
393 <?php
394 }
395 }
396
397 /*
398 * add ID to exclude list
399 */
400 function rw_cryptx_insert_post($pID) {
401 global $cryptX_var, $post;
402 $rev = wp_is_post_revision($pID);
403 if($rev) $pID = $rev;
404 $b = explode(",", $cryptX_var['excludedIDs']);
405 if($b[0] == '') unset($b[0]);
406 foreach($b as $x=>$y) {
407 if($y == $pID) {
408 unset($b[$x]);
409 break;
410 }
411 }
412 if (isset($_POST['cryptxoff'])) $b[] = $pID;
413 $b = array_unique($b);
414 sort($b);
415 $cryptX_var['excludedIDs'] = implode(",", $b);
416 update_option( 'cryptX', $cryptX_var);
417 $cryptX_var = rw_loadDefaults(); // reread Options
418 }
419
420 /**
421 * print admin notice
422 */
423 function rw_cryptx_showMessage($message, $errormsg = false)
424 {
425 if ($errormsg) {
426 echo '<div id="message" class="error">';
427 }
428 else {
429 echo '<div id="message" class="updated fade">';
430 }
431
432 echo "$message</div>";
433 }
434
435 ?>