2
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details, published at
13
# http://www.gnu.org/copyleft/gpl.html
15
package Foswiki::Plugins::TinyMCEPlugin;
21
our $VERSION = '$Rev: 3993 (2009-05-30) $';
22
our $RELEASE = '21 Jan 2009';
23
our $SHORTDESCRIPTION = 'Integration of TinyMCE with WysiwygPlugin';
28
# Info about browser type
32
$query = Foswiki::Func::getCgiQuery();
33
return 0 unless $query;
34
unless( $Foswiki::cfg{Plugins}{WysiwygPlugin}{Enabled} ) {
35
Foswiki::Func::writeWarning("TinyMCEPlugin is enabled but WysiwygPlugin is not enabled. Both plugins must be installed and enabled for TinyMCE.");
39
# Identify the browser from the user agent string
40
my $ua = $query->user_agent();
42
$browserInfo{isMSIE} = $ua =~ /MSIE/;
43
$browserInfo{isMSIE5} = $browserInfo{isMSIE} && ($ua =~ /MSIE 5/);
44
$browserInfo{isMSIE5_0} = $browserInfo{isMSIE} && ($ua =~ /MSIE 5.0/);
45
$browserInfo{isMSIE6} = $browserInfo{isMSIE} && $ua =~ /MSIE 6/;
46
$browserInfo{isMSIE7} = $browserInfo{isMSIE} && $ua =~ /MSIE 7/;
47
$browserInfo{isGecko} = $ua =~ /Gecko/; # Will also be true on Safari
48
$browserInfo{isSafari} = $ua =~ /Safari/;
49
$browserInfo{isOpera} = $ua =~ /Opera/;
50
$browserInfo{isMac} = $ua =~ /Mac/;
51
$browserInfo{isNS7} = $ua =~ /Netscape\/7/;
52
$browserInfo{isNS71} = $ua =~ /Netscape\/7.1/;
59
return "Disabled by preference '".Foswiki::Func::getPreferencesValue('TINYMCEPLUGIN_DISABLE')."'" if
60
Foswiki::Func::getPreferencesFlag('TINYMCEPLUGIN_DISABLE');
62
# Disable TinyMCE if we are on a specialised edit skin
63
my $skin = Foswiki::Func::getPreferencesValue('WYSIWYGPLUGIN_WYSIWYGSKIN');
64
return "$skin is active"
65
if( $skin && Foswiki::Func::getSkin() =~ /\b$skin\b/o );
67
return "No browser" unless $query;
69
return "Disabled by URL parameter" if $query->param('nowysiwyg');
71
# Check the client browser to see if it is blacklisted
72
my $ua = Foswiki::Func::getPreferencesValue('TINYMCEPLUGIN_BAD_BROWSERS') ||
73
'(?i-xsm:Konqueror|Opera)';
74
return 'Unsupported browser: '.$query->user_agent()
75
if $ua && $query->user_agent() && $query->user_agent() =~ /$ua/;
80
sub beforeEditHandler {
81
#my ($text, $topic, $web) = @_;
83
my $mess = _notAvailable();
85
if (($mess !~ /^Disabled/ || DEBUG) &&
86
defined &Foswiki::Func::setPreferencesValue) {
87
Foswiki::Func::setPreferencesValue(
89
'WYSIWYG could not be started: '.$mess);
93
if (defined &Foswiki::Func::setPreferencesValue) {
94
Foswiki::Func::setPreferencesValue('EDITOR_HELP', 'TinyMCEQuickHelp');
97
my $init = Foswiki::Func::getPreferencesValue('TINYMCEPLUGIN_INIT')
102
# The order of these conditions is important, because browsers
104
if ($browserInfo{isSafari}) {
106
} elsif ($browserInfo{isOpera}) {
108
} elsif ($browserInfo{isGecko}) {
110
} elsif ($browserInfo{isMSIE}) {
114
$extras = Foswiki::Func::getPreferencesValue(
115
'TINYMCEPLUGIN_INIT_'.$extras);
116
if (defined $extras) {
117
$init = join(',', (split(',',$init), split(',',$extras)));
121
require Foswiki::Plugins::WysiwygPlugin;
123
$mess = Foswiki::Plugins::WysiwygPlugin::notWysiwygEditable($_[0]);
125
if (defined &Foswiki::Func::setPreferencesValue) {
126
Foswiki::Func::setPreferencesValue(
128
'WYSIWYG could not be started: '.$mess);
134
if (Foswiki::Func::getPreferencesValue('TINYMCEPLUGIN_DEBUG')) {
138
# Add the Javascript for the editor. When it starts up the editor will
139
# use a REST call to the WysiwygPlugin tml2html REST handler to convert
140
# the textarea content from TML to HTML.
141
my $pluginURL = '%PUBURL%/%SYSTEMWEB%/TinyMCEPlugin';
142
my $tmceURL = $pluginURL.'/tinymce/jscripts/tiny_mce';
143
# expand and URL-encode the init string
144
my $metainit = Foswiki::Func::expandCommonVariables($init);
145
$metainit =~ s/([^0-9a-zA-Z-_.:~!*'\/%])/'%'.sprintf('%02x',ord($1))/ge;
148
require Foswiki::Contrib::BehaviourContrib;
149
if (defined(&Foswiki::Contrib::BehaviourContrib::addHEAD)) {
150
Foswiki::Contrib::BehaviourContrib::addHEAD();
155
Foswiki::Func::addToHEAD(
157
'<script type="text/javascript" src="%PUBURLPATH%/%SYSTEMWEB%/BehaviourContrib/behaviour.js"></script>');
159
# URL-encode the version number to include in the .js URLs, so that the browser re-fetches the .js
160
# when this plugin is upgraded.
161
my $encodedVersion = $VERSION;
162
# SMELL: This regex (and the one applied to $metainit, above) duplicates Foswiki::urlEncode(),
163
# but Foswiki::Func.pm does not expose that function, so plugins may not use it
164
$encodedVersion =~ s/([^0-9a-zA-Z-_.:~!*'\/%])/'%'.sprintf('%02x',ord($1))/ge;
165
Foswiki::Func::addToHEAD('tinyMCE', <<SCRIPT);
166
<meta name="TINYMCEPLUGIN_INIT" content="$metainit" />
167
<script language="javascript" type="text/javascript" src="$tmceURL/tiny_mce$USE_SRC.js?v=$encodedVersion"></script>
168
<script language="javascript" type="text/javascript" src="$pluginURL/foswiki_tiny$USE_SRC.js?v=$encodedVersion"></script>
169
<script language="javascript" type="text/javascript" src="$pluginURL/foswiki$USE_SRC.js?v=$encodedVersion"></script>
172
# See %SYSTEMWEB%.IfStatements for a description of this context id.
173
Foswiki::Func::getContext()->{textareas_hijacked} = 1;