/
TinyMCEPlugin.pm
226 lines (189 loc) · 7.73 KB
/
TinyMCEPlugin.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# See bottom of file for license and copyright information
package Foswiki::Plugins::TinyMCEPlugin;
use strict;
use warnings;
use Assert;
our $VERSION = '$Rev$';
our $RELEASE = '1.1.10';
our $SHORTDESCRIPTION = 'Integration of the Tiny MCE WYSIWYG Editor';
our $NO_PREFS_IN_TOPIC = 1;
use Foswiki::Func ();
our %defaultINIT_BROWSER = (
MSIE => '',
OPERA => '',
GECKO => '"gecko_spellcheck" : true',
SAFARI => '',
CHROME => '',
);
my $query;
# Info about browser type
my %browserInfo;
sub initPlugin {
$query = Foswiki::Func::getCgiQuery();
return 0 unless $query;
unless ( $Foswiki::cfg{Plugins}{WysiwygPlugin}{Enabled} ) {
Foswiki::Func::writeWarning(
"TinyMCEPlugin is enabled but WysiwygPlugin is not. Both must be installed and enabled for TinyMCE."
);
return 0;
}
unless ( $Foswiki::cfg{Plugins}{JQueryPlugin}{Enabled} ) {
Foswiki::Func::writeWarning(
"TinyMCEPlugin is enabled but JQueryPlugin is not. Both must be installed and enabled for TinyMCE."
);
return 0;
}
# Identify the browser from the user agent string
my $ua = $query->user_agent();
if ($ua) {
$browserInfo{isMSIE} = $ua =~ /MSIE/;
$browserInfo{isMSIE5} = $browserInfo{isMSIE} && ( $ua =~ /MSIE 5/ );
$browserInfo{isMSIE5_0} = $browserInfo{isMSIE} && ( $ua =~ /MSIE 5.0/ );
$browserInfo{isMSIE6} = $browserInfo{isMSIE} && $ua =~ /MSIE 6/;
$browserInfo{isMSIE7} = $browserInfo{isMSIE} && $ua =~ /MSIE 7/;
$browserInfo{isMSIE8} = $browserInfo{isMSIE} && $ua =~ /MSIE 8/;
$browserInfo{isGecko} = $ua =~ /Gecko/; # Will also be true on Safari
$browserInfo{isSafari} = $ua =~ /Safari/; # Will also be true on Chrome
$browserInfo{isOpera} = $ua =~ /Opera/;
$browserInfo{isChrome} = $ua =~ /Chrome/;
$browserInfo{isMac} = $ua =~ /Mac/;
$browserInfo{isNS7} = $ua =~ /Netscape\/7/;
$browserInfo{isNS71} = $ua =~ /Netscape\/7.1/;
}
return 1;
}
sub _notAvailable {
for my $c qw(TINYMCEPLUGIN_DISABLE NOWYSIWYG) {
return "Disabled by * Set $c = "
. Foswiki::Func::getPreferencesValue($c)
if Foswiki::Func::getPreferencesFlag($c);
}
# Disable TinyMCE if we are on a specialised edit skin
my $skin = Foswiki::Func::getPreferencesValue('WYSIWYGPLUGIN_WYSIWYGSKIN');
return "$skin is active"
if ( $skin && Foswiki::Func::getSkin() =~ /\b$skin\b/o );
return "No browser" unless $query;
return "Disabled by URL parameter" if $query->param('nowysiwyg');
# Check the client browser to see if it is blacklisted
my $ua = Foswiki::Func::getPreferencesValue('TINYMCEPLUGIN_BAD_BROWSERS')
|| '(?i-xsm:Konqueror)';
return 'Unsupported browser: ' . $query->user_agent()
if $ua && $query->user_agent() && $query->user_agent() =~ /$ua/;
# This should only ever happen on Foswiki 1.0.9 and earlier
return 'TinyMCEPlugin requires ZonePlugin to be installed and enabled'
unless ( defined &Foswiki::Func::addToZone );
return 0;
}
sub beforeEditHandler {
my ( $text, $topic, $web ) = @_;
my $mess = _notAvailable();
if ($mess) {
if ( ( $mess !~ /^Disabled/ || DEBUG )
&& defined &Foswiki::Func::setPreferencesValue )
{
Foswiki::Func::setPreferencesValue( 'EDITOR_MESSAGE',
'WYSIWYG could not be started: ' . $mess );
}
return;
}
if ( defined &Foswiki::Func::setPreferencesValue ) {
Foswiki::Func::setPreferencesValue( 'EDITOR_HELP', 'TinyMCEQuickHelp' );
}
my $initTopic =
Foswiki::Func::getPreferencesValue('TINYMCEPLUGIN_INIT_TOPIC')
|| $Foswiki::cfg{SystemWebName} . '.TinyMCEPlugin';
my $init = Foswiki::Func::getPreferencesValue('TINYMCEPLUGIN_INIT')
|| Foswiki::Func::expandCommonVariables(
'%INCLUDE{"'
. $initTopic
. '" section="TINYMCEPLUGIN_INIT" warn="off"}%',
$topic, $web
);
my $browser = '';
# The order of these conditions is important, because browsers
# spoof eachother
if ( $browserInfo{isChrome} ) {
$browser = 'CHROME';
}
elsif ( $browserInfo{isSafari} ) {
$browser = 'SAFARI';
}
elsif ( $browserInfo{isOpera} ) {
$browser = 'OPERA';
}
elsif ( $browserInfo{isGecko} ) {
$browser = 'GECKO';
}
elsif ( $browserInfo{isMSIE} ) {
$browser = 'MSIE';
}
if ($browser) {
my $settings =
Foswiki::Func::getPreferencesValue( 'TINYMCEPLUGIN_INIT_' . $browser )
|| $defaultINIT_BROWSER{$browser};
if ($settings) {
$init =
join( ',', ( split( ',', $init ), split( ',', $settings ) ) );
}
}
require Foswiki::Plugins::WysiwygPlugin;
$mess = Foswiki::Plugins::WysiwygPlugin::notWysiwygEditable($text);
if ($mess) {
if ( defined &Foswiki::Func::setPreferencesValue ) {
Foswiki::Func::setPreferencesValue( 'EDITOR_MESSAGE',
'WYSIWYG could not be started: ' . $mess );
}
return;
}
my $USE_SRC = '';
if ( Foswiki::Func::getPreferencesValue('TINYMCEPLUGIN_DEBUG') ) {
$USE_SRC = '_src';
}
# Add the Javascript for the editor. When it starts up the editor will
# use a REST call to the WysiwygPlugin tml2html REST handler to convert
# the textarea content from TML to HTML.
my $pluginURL = '%PUBURLPATH%/%SYSTEMWEB%/TinyMCEPlugin';
my $tmceURL = $pluginURL . '/tinymce/jscripts/tiny_mce';
# URL-encode the version number to include in the .js URLs, so that
# the browser re-fetches the .js when this plugin is upgraded.
my $encodedVersion = $VERSION;
# SMELL: This regex (and the one applied to $metainit, above)
# duplicates Foswiki::urlEncode(), but Foswiki::Func.pm does not
# expose that function, so plugins may not use it
$encodedVersion =~
s/([^0-9a-zA-Z-_.:~!*'\/%])/'%'.sprintf('%02x',ord($1))/ge;
# Inline JS to set config? Heresy! Well, we were encoding into <meta tags
# but this caused problems with non-8bit encodings (See Item9973). Given
# that we blindly eval'd the unescaped TINYMCEPLUGIN_INIT anyway, PaulHarvey
# doesn't think it was any more secure anyway. Alternative is to use
# https://github.com/douglascrockford/JSON-js lib
my $scripts = <<"SCRIPT";
<script type="text/javascript" src="$tmceURL/tiny_mce$USE_SRC.js?v=$encodedVersion"></script>
<script type="text/javascript" src="$pluginURL/foswiki_tiny$USE_SRC.js?v=$encodedVersion"></script>
<script type="text/javascript">
FoswikiTiny.init = {
$init
};</script>
<script type="text/javascript" src="$pluginURL/foswiki$USE_SRC.js?v=$encodedVersion"></script>
SCRIPT
Foswiki::Func::addToZone( 'script', 'TinyMCEPlugin', $scripts,
'JQUERYPLUGIN::FOSWIKI' );
# See %SYSTEMWEB%.IfStatements for a description of this context id.
Foswiki::Func::getContext()->{textareas_hijacked} = 1;
return;
}
1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2008-2010 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.