fa01e9b768c4d57e97c378472d08f689935c448c
[platal.git] / include / wiki / e-protect.php
1 <?php if (!defined('PmWiki')) exit();
2 /*
3 * Copyright *
4 Copyright 2004, by Steven Leite (steven_leite@kitimat.net).
5 Changes 2004, by Karl Loncarek (Klonk)
6
7 * License *
8 Same as PmWiki (GNU GPL)
9
10 * Special Thanks *
11 Thanks to Pm (Patrick Michaud, www.pmichaud.com), for creating
12 PmWiki, and for sharing his knowledge and insightfulness with the
13 PmWiki community.
14
15 * Description *
16 eProtect is an email obfuscation add-on for PmWiki. It intercepts
17 pages before they are saved, and rewrites email addresses in a
18 protected format.
19
20 Example: "[[mailto:username@domain.net]]" is automatically replaced with:
21 "[[hidden-email:hfre@qbznva.arg]]".
22
23 When a WikiPage is requested the [[hidden-email:]] directive is
24 detected, and then translated into a small javascript. Once the page
25 is rendered to the in website, it's at that point which the javascript
26 decodes the protected address. Viewing the source-code of the page
27 won't reveal the true email address, nor will clicking on the link to
28 "Edit This Page".
29
30 Also you could give some text that is displayed instead the email address.
31
32 Example: "[[mailto:username@domain.net | Special Guy]]" is automatically
33 replaced with: "[[hidden-email:hfre@qbznva.arg | Special Guy]]". On the
34 page you only can see "Special Guy" as urllink.
35
36 But beware: giving the email address as alternative text renders this script
37 useless, because this alternative text is NOT encoded, and thus is also visible
38 in HTML source code!
39
40
41 * Installation Instructions *
42 1. Copy this script (e-protect.php) to local/scripts/
43 2. In your config.php file, add the following line:
44 include_once('scripts/e-protect.php');
45 3. That's it!
46
47 * History *
48 Apr 7 2005 - * Replaced str_rot13 with strtr to make it work with PHP <4.2.0,
49 and to have the @ recoded.
50 Feb 18 2005 - * Added some comment signs to get valid XHTML when validating (Klonk)
51 Feb 15 2005 - * Modified encoding and added additional markup for handling (Klonk)
52 [[text -> mailto:...]]
53 Jan 7 2005 - * Moved decoding script to Header, due to problem, when SideBar and Main Text
54 contains addresses to decode (Klonk)
55
56 Nov 17 2004 - * Calling the decoding function as Custom Markup for PmWiki2
57 (by Klonk) * made decoding of [[hidden-email:...]] working
58 BONUS: [[hidden-email:... |DisplayedText]] works also now
59 * added class='urllink' to decoded output for same CSS formating
60 as for other links in PmWiki2
61 * inserted own function call in array $EditFunctions before 'ReplaceOnSave'
62
63 May 11, 2004 - Working Beta. Still a few improvements to be made, but
64 the script is ready for public testing. Please feel
65 free to email me your comments/suggestions. Thanks!
66 May 8, 2004 - Alpha release. Not released to public.
67
68 * Configuration *
69 There aren't (yet) any configuration variables for this script. */
70
71 //----------------------------------------------------------------------
72
73 ## [[hidden-email:target]]
74 Markup('hidden-email','<links',
75 "/\\[\\[hidden-email:([^\\s$UrlExcludeChars]*)\\s*\\]\\]($SuffixPattern)/e",
76 "eProtectDecode('$1','')");
77
78 ## [[hidden-email:target | text]]
79 Markup('hidden-email|','<hidden-email',
80 "/\\[\\[hidden-email:([^\\s$UrlExcludeChars]*)\\s*\\|\\s*(.*?)\\s*\\]\\]($SuffixPattern)/e",
81 "eProtectDecode('$1','$2')");
82
83 ## [[ text -> hidden-email:target]]
84 Markup('-hidden-email','<hidden-email',
85 "/\\[\\[(.*?)\\s*-+&gt;\\s*hidden-email:([^\\s$UrlExcludeChars]*)\\s*\\]\\]($SuffixPattern)/e",
86 "eProtectDecode('$2','$1')");
87
88 ## Add own function in array $EditFunctions before ReplaceOnSave, so it is called, when saving is performed.
89 array_splice ($EditFunctions, array_search('ReplaceOnSave',$EditFunctions), 1,
90 array('eProtectEncode','ReplaceOnSave'));
91
92 ## Add decoding script to Header
93 $HTMLHeaderFmt['eProtect']= "\n<script type='text/JavaScript'>\n<!--\nNix={map:null,convert:function(a){Nix.init();var s='';for(i=0;i<a.length;i++){var b=a.charAt(i);s+=((b>='A'&&b<='Z')||(b>='a'&&b<='z')?Nix.map[b]:b);}return s;},init:function(){if(Nix.map!=null)return;var map=new Array();var s='abcdefghijklmnopqrstuvwxyz';for(i=0;i<s.length;i++)map[s.charAt(i)]=s.charAt((i+13)%26);for(i=0;i<s.length;i++)map[s.charAt(i).toUpperCase()]=s.charAt((i+13)%26).toUpperCase();Nix.map=map;},decode:function(a){document.write(Nix.convert(a));}}\n//-->\n</script>\n";
94
95 //----------------------------------------------------------------------
96 function eProtectStrRecode ($s) {
97 /* str_rot13, extended to recode digits and @#. */
98 //----------------------------------------------------------------------
99 return strtr ($s,
100 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
101 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM');
102 }
103
104 //----------------------------------------------------------------------
105 function eProtectDecode ($CompressedEmailAddress,$AlternateText) {
106 //----------------------------------------------------------------------
107 $email = $CompressedEmailAddress;
108 $html = "\n<!--eProtect-->\n";
109 if ($AlternateText=='')
110 $html .= "<script type='text/JavaScript'>\n<!--\nNix.decode" .
111 "(\"<n pynff='heyyvax' uers='znvygb:$email'>$email</n>\");" . "\n//-->\n</script>";
112 else
113 $html .= "<script type='text/JavaScript'>\n<!--\nNix.decode" .
114 "(\"<n pynff='heyyvax' uers='znvygb:$email'>\");" . "\n//-->\n</script>" . $AlternateText . "<script
115 type='text/JavaScript'><!--\nNix.decode" .
116 "(\"</n>\");" . "\n//-->\n</script>";
117 $html .= "\n<!--/eProtect-->\n";
118 return Keep($html);
119 }
120
121 //----------------------------------------------------------------------
122 function eProtectEncode ($pagename,$page,&$new) {
123 //----------------------------------------------------------------------
124 global $KeepToken, $KPV, $UrlExcludeChars;
125 if (!@$_POST['post']) return; // only Encode, when posting
126 $text = $new['text'];
127 $text = preg_replace_callback("/\\[\\=(.*?)\\=\\]/s", create_function('$str', 'return Keep($str[0]);'), $text); // extract the [= .. =] and temporarily store in $KPV[]
128 # $text = preg_replace_callback("/\\[\\[mailto:([^\\s$UrlExcludeChars]*)/", create_function('$m','return "[[hidden-email:".trim(eProtectStrRecode($m[1]));'), $text);
129 $text = preg_replace_callback("/\\[\\[(.*?)mailto:([^\\s$UrlExcludeChars]*)(.*?)\\]\\]/", create_function('$m','return "[[".$m[1]."hidden-email:".trim(eProtectStrRecode($m[2])).$m[3]."]]";'), $text);
130 $text = preg_replace("/$KeepToken(\\d+)$KeepToken/e",'$KPV[$1]',$text); // put the [= .. =] back in to the text
131 $new['text'] = $text;
132 }
133 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
134 ?>