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