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