~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/lib/Foswiki/Plugins/TinyMCEPlugin.pm

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
 
3
#
 
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.
 
8
#
 
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
 
14
 
 
15
package Foswiki::Plugins::TinyMCEPlugin;
 
16
 
 
17
use strict;
 
18
 
 
19
use Assert;
 
20
 
 
21
our $VERSION = '$Rev: 3993 (2009-05-30) $';
 
22
our $RELEASE = '21 Jan 2009';
 
23
our $SHORTDESCRIPTION = 'Integration of TinyMCE with WysiwygPlugin';
 
24
 
 
25
use Foswiki::Func;
 
26
 
 
27
my $query;
 
28
# Info about browser type
 
29
my %browserInfo;
 
30
 
 
31
sub initPlugin {
 
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.");
 
36
        return 0;
 
37
    }
 
38
 
 
39
    # Identify the browser from the user agent string
 
40
    my $ua = $query->user_agent();
 
41
    if ($ua) {
 
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/;
 
53
    }
 
54
 
 
55
    return 1;
 
56
}
 
57
 
 
58
sub _notAvailable {
 
59
    return "Disabled by preference '".Foswiki::Func::getPreferencesValue('TINYMCEPLUGIN_DISABLE')."'" if
 
60
      Foswiki::Func::getPreferencesFlag('TINYMCEPLUGIN_DISABLE');
 
61
 
 
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 );
 
66
 
 
67
    return "No browser" unless $query;
 
68
 
 
69
    return "Disabled by URL parameter" if $query->param('nowysiwyg');
 
70
 
 
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/;
 
76
 
 
77
    return 0;
 
78
}
 
79
 
 
80
sub beforeEditHandler {
 
81
    #my ($text, $topic, $web) = @_;
 
82
 
 
83
    my $mess = _notAvailable();
 
84
    if ($mess) {
 
85
        if (($mess !~ /^Disabled/ || DEBUG) &&
 
86
              defined &Foswiki::Func::setPreferencesValue) {
 
87
            Foswiki::Func::setPreferencesValue(
 
88
                'EDITOR_MESSAGE',
 
89
                'WYSIWYG could not be started: '.$mess);
 
90
        }
 
91
        return;
 
92
    }
 
93
    if (defined &Foswiki::Func::setPreferencesValue) {
 
94
        Foswiki::Func::setPreferencesValue('EDITOR_HELP', 'TinyMCEQuickHelp');
 
95
    }
 
96
 
 
97
    my $init = Foswiki::Func::getPreferencesValue('TINYMCEPLUGIN_INIT')
 
98
      || <<'HERE';
 
99
'
 
100
HERE
 
101
    my $extras = '';
 
102
    # The order of these conditions is important, because browsers
 
103
    # spoof eachother
 
104
    if ($browserInfo{isSafari}) {
 
105
        $extras = 'SAFARI';
 
106
    } elsif ($browserInfo{isOpera}) {
 
107
        $extras = 'OPERA';
 
108
    } elsif ($browserInfo{isGecko}) {
 
109
        $extras = 'GECKO';
 
110
    } elsif ($browserInfo{isMSIE}) {
 
111
        $extras = 'MSIE';
 
112
    }
 
113
    if ($extras) {
 
114
        $extras = Foswiki::Func::getPreferencesValue(
 
115
            'TINYMCEPLUGIN_INIT_'.$extras);
 
116
        if (defined $extras) {
 
117
            $init = join(',', (split(',',$init), split(',',$extras)));
 
118
        }
 
119
    }
 
120
 
 
121
    require Foswiki::Plugins::WysiwygPlugin;
 
122
 
 
123
    $mess = Foswiki::Plugins::WysiwygPlugin::notWysiwygEditable($_[0]);
 
124
    if ($mess) {
 
125
        if (defined &Foswiki::Func::setPreferencesValue) {
 
126
            Foswiki::Func::setPreferencesValue(
 
127
                'EDITOR_MESSAGE',
 
128
                'WYSIWYG could not be started: '.$mess);
 
129
        }
 
130
        return;
 
131
    }
 
132
 
 
133
    my $USE_SRC = '';
 
134
    if (Foswiki::Func::getPreferencesValue('TINYMCEPLUGIN_DEBUG')) {
 
135
        $USE_SRC = '_src';
 
136
    }
 
137
 
 
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;
 
146
    my $behaving;
 
147
    eval {
 
148
        require Foswiki::Contrib::BehaviourContrib;
 
149
        if (defined(&Foswiki::Contrib::BehaviourContrib::addHEAD)) {
 
150
            Foswiki::Contrib::BehaviourContrib::addHEAD();
 
151
            $behaving = 1;
 
152
        }
 
153
    };
 
154
    unless ($behaving) {
 
155
        Foswiki::Func::addToHEAD(
 
156
            'BEHAVIOURCONTRIB',
 
157
            '<script type="text/javascript" src="%PUBURLPATH%/%SYSTEMWEB%/BehaviourContrib/behaviour.js"></script>');
 
158
    }
 
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>
 
170
SCRIPT
 
171
 
 
172
    # See %SYSTEMWEB%.IfStatements for a description of this context id.
 
173
    Foswiki::Func::getContext()->{textareas_hijacked} = 1;
 
174
}
 
175
 
 
176
1;
 
177