Ticket #2561: FCKeditor.pm

File FCKeditor.pm, 5.6 KB (added by Doug Bierer, 16 years ago)
Line 
1#####
2#  FCKeditor - The text editor for internet
3#  Copyright (C) 2003-2005 Frederico Caldeira Knabben
4
5#  Licensed under the terms of the GNU Lesser General Public License:
6#               http://www.opensource.org/licenses/lgpl-license.php
7
8#  For further information visit:
9#               http://www.fckeditor.net/
10
11#  File Name: FCKeditor.pm
12#       This is the integration file for Perl.
13
14#  File Authors:
15#               Takashi Yamaguchi (jack@omakase.net)
16#
17#  Modified and made into a package by:
18#               Doug Bierer (doug@unlikelysource.com)
19#
20#  Usage:
21#       1.      Put this command at the top of your PERL program:
22#
23#                       use FCKeditor;
24#
25#       2.      Where you want to place the FCKeditor in your output,
26#               call FCKeditor::create() as follows:
27#
28#               $html_string = FCKeditor::create($InstanceName,$BasePath);
29#
30#               This will produce an HTML string which effectively replaces
31#               a <textarea name=$InstanceName></textarea> field in your form.
32#               Note that there are a few other parameters which you can
33#               specify in the create() call, noted below.  Height and
34#               width, for example.
35#
36#               You can then integrate this HTML code directly into your
37#               Output.  For example:
38#
39#               use FCKeditor;
40#               print 'content-type: text/html\n\n';
41#               print '<html><body><form method=get action="this.pl">';
42#               print '<p>Enter Your Message Here:</p>';
43#               print FCKeditor::create();
44#               print '<input type=submit name="OK" value="OK">';
45#               print '</form></body></html>';
46#
47#               Or you could send the string to the Template module,
48#               depending on how you output your HTML.  Your return CGI
49#               program will then look for a parameter "editor", which is
50#               the default (see $InstanceName).
51#
52#####
53
54package FCKeditor;
55
56require Exporter;
57
58#---------------------------------------------------------------------------------
59
60our $VERSION = 0.01;
61
62our @ISA = ("Exporter");
63
64our @EXPORT = qw(
65        &FCKeditor
66        &Create
67        &specialchar_cnv
68        &CreateHtml
69        &IsCompatible
70        &GetConfigFieldString
71        &new
72        );
73
74our %EXPORT_TAGS = ( );
75
76our @EXPORT_OK = qw(
77        $InstanceName
78        $BasePath
79        $Width
80        $Height
81        $ToolbarSet
82        $Value
83        %Config
84        );
85
86use vars qw(
87        $InstanceName
88        $BasePath
89        $Width
90        $Height
91        $ToolbarSet
92        $Value
93        %Config
94        );
95
96#---------------------------------------------------------------------------------
97       
98# Initialize Globals
99
100
101$InstanceName   = 'editor';
102$BasePath               = './FCKeditor/';
103$Height                 = '400';
104$Width                  = '100%';
105$ToolbarSet             = 'Default';
106$Value                  = '';
107%Config                 = ( );
108
109#---------------------------------------------------------------------------------
110
111#
112# Inputs:       $InstanceName = the <textarea> field you want to replace with FCKeditor output
113#                       $BasePath = the directory where the FCKeditor files reside
114#                       $Height = height you want FCKeditor to reach
115#                       $Width = width you wish FCKeditor to take
116#                       $ToolbarSet = any special set of tools
117#                       $Value = ???
118#
119# Outputs:      An HTML string which implements FCKeditor
120#
121sub Create
122{
123
124        $InstanceName   = shift;
125        $BasePath               = shift;
126        $Height                 = shift;
127        $Width                  = shift;
128        $ToolbarSet             = shift;
129        $Value                  = shift;
130
131        # Check to see if null and reset to original values
132        if ( !$InstanceName ) { $InstanceName   = "editor";             }
133        if ( !$BasePath         ) { $BasePath           = './FCKeditor/';       }
134        if ( !$Height           ) { $Height                     = '400';                        }
135        if ( !$Width            ) { $Width                      = '100%';                       }
136        if ( !$ToolbarSet       ) { $ToolbarSet         = 'Default';            }
137
138        return &CreateHtml();
139
140}
141
142sub specialchar_cnv
143{
144
145        local($ch) = @_;
146
147        $ch =~ s/&/&amp;/g;             # &
148        $ch =~ s/\"/&quot;/g;   #"
149        $ch =~ s/\'/&#39;/g;    # '
150        $ch =~ s/</&lt;/g;              # <
151        $ch =~ s/>/&gt;/g;              # >
152        return($ch);
153}
154
155sub CreateHtml
156{
157
158        $HtmlValue = &specialchar_cnv($Value);
159        $Html = '<div>' ;
160        if(&IsCompatible()) {
161                $Link = $BasePath . "editor/fckeditor.html?InstanceName=$InstanceName";
162                if($ToolbarSet ne '') {
163                        $Link .= "&amp;Toolbar=$ToolbarSet";
164                }
165                #// Render the linked hidden field.
166                $Html .= "<input type=\"hidden\" id=\"$InstanceName\" name=\"$InstanceName\" value=\"$HtmlValue\" />" ;
167
168                #// Render the configurations hidden field.
169                $cfgstr = &GetConfigFieldString();
170                $wk = $InstanceName."___Config";
171                $Html .= "<input type=\"hidden\" id=\"$wk\" value=\"$cfgstr\" />" ;
172
173                #// Render the editor IFRAME.
174                $wk = $InstanceName."___Frame";
175                $Html .= "<iframe id=\"$wk\" src=\"$Link\" width=\"$Width\" height=\"$Height\" frameborder=\"no\" scrolling=\"no\"></iframe>";
176        } else {
177                if($Width =~ /\%/g){
178                        $WidthCSS = $Width;
179                } else {
180                        $WidthCSS = $Width . 'px';
181                }
182                if($Height =~ /\%/g){
183                        $HeightCSS = $Height;
184                } else {
185                        $HeightCSS = $Height . 'px';
186                }
187                $Html .= "<textarea name=\"$InstanceName\" rows=\"4\" cols=\"40\" style=\"width: $WidthCSS; height: $HeightCSS\" wrap=\"virtual\">$HtmlValue</textarea>";
188        }
189        $Html .= '</div>';
190        return($Html);
191}
192
193sub IsCompatible
194{
195
196        $sAgent = $ENV{'HTTP_USER_AGENT'};
197        if(($sAgent =~ /MSIE/i) && !($sAgent =~ /mac/i) && !($sAgent =~ /Opera/i)) {
198                $iVersion = substr($sAgent,index($sAgent,'MSIE') + 5,3);
199                return($iVersion >= 5.5) ;
200        } elsif($sAgent =~ /Gecko\//i) {
201                $iVersion = substr($sAgent,index($sAgent,'Gecko/') + 6,8);
202                return($iVersion >= 20030210) ;
203        } else {
204                return(0);              # 2.0 PR fix
205        }
206}
207
208sub GetConfigFieldString
209{
210        $sParams = '';
211        $bFirst = 0;
212        foreach $sKey (keys %Config) {
213                $sValue = $Config{$sKey};
214                if($bFirst == 1) {
215                        $sParams .= '&amp;';
216                } else {
217                        $bFirst = 1;
218                }
219                $k = &specialchar_cnv($sKey);
220                $v = &specialchar_cnv($sValue);
221                if($sValue eq "true") {
222                        $sParams .= "$k=true";
223                } elsif($sValue eq "false") {
224                        $sParams .= "$k=false";
225                } else {
226                        $sParams .= "$k=$v";
227                }
228        }
229        return($sParams);
230}
231
2321;
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy