Ticket #2561: FCKeditor.pm

File FCKeditor.pm , 5.5 KB (added by Alexandr Ciornii, 15 years ago)

modified module

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
86#---------------------------------------------------------------------------------
87       
88# Declare and Initialize Globals
89
90
91our $InstanceName       = 'editor';
92our $BasePath           = './FCKeditor/';
93our $Height             = '400';
94our $Width              = '100%';
95our $ToolbarSet         = 'Default';
96our $Value              = '';
97our %Config             = ( );
98
99#---------------------------------------------------------------------------------
100
101#
102# Inputs:       $InstanceName = the <textarea> field you want to replace with FCKeditor output
103#                       $BasePath = the directory where the FCKeditor files reside
104#                       $Height = height you want FCKeditor to reach
105#                       $Width = width you wish FCKeditor to take
106#                       $ToolbarSet = any special set of tools
107#                       $Value = ???
108#
109# Outputs:      An HTML string which implements FCKeditor
110#
111sub Create
112{
113
114        $InstanceName   = shift;
115        $BasePath               = shift;
116        $Height                 = shift;
117        $Width                  = shift;
118        $ToolbarSet             = shift;
119        $Value                  = shift;
120
121        # Check to see if null and reset to original values
122        if ( !$InstanceName ) { $InstanceName   = "editor";             }
123        if ( !$BasePath         ) { $BasePath           = './FCKeditor/';       }
124        if ( !$Height           ) { $Height                     = '400';                        }
125        if ( !$Width            ) { $Width                      = '100%';                       }
126        if ( !$ToolbarSet       ) { $ToolbarSet         = 'Default';            }
127
128        return CreateHtml();
129
130}
131
132sub specialchar_cnv
133{
134
135        my ($ch) = @_;
136
137        $ch =~ s/&/&amp;/g;             # &
138        $ch =~ s/"/&quot;/g;    #"
139        $ch =~ s/'/&#39;/g;     # '
140        $ch =~ s/</&lt;/g;              # <
141        $ch =~ s/>/&gt;/g;              # >
142        return $ch;
143}
144
145sub CreateHtml
146{
147
148        $HtmlValue = specialchar_cnv($Value);
149        $Html = '<div>' ;
150        if(IsCompatible()) {
151                $Link = $BasePath . "editor/fckeditor.html?InstanceName=$InstanceName";
152                if($ToolbarSet ne '') {
153                        $Link .= "&amp;Toolbar=$ToolbarSet";
154                }
155                #// Render the linked hidden field.
156                $Html .= "<input type=\"hidden\" id=\"$InstanceName\" name=\"$InstanceName\" value=\"$HtmlValue\" />" ;
157
158                #// Render the configurations hidden field.
159                $cfgstr = GetConfigFieldString();
160                $wk = $InstanceName."___Config";
161                $Html .= "<input type=\"hidden\" id=\"$wk\" value=\"$cfgstr\" />" ;
162
163                #// Render the editor IFRAME.
164                $wk = $InstanceName."___Frame";
165                $Html .= "<iframe id=\"$wk\" src=\"$Link\" width=\"$Width\" height=\"$Height\" frameborder=\"no\" scrolling=\"no\"></iframe>";
166        } else {
167                if($Width =~ /\%/){
168                        $WidthCSS = $Width;
169                } else {
170                        $WidthCSS = $Width . 'px';
171                }
172                if($Height =~ /\%/){
173                        $HeightCSS = $Height;
174                } else {
175                        $HeightCSS = $Height . 'px';
176                }
177                $Html .= "<textarea name=\"$InstanceName\" rows=\"4\" cols=\"40\" style=\"width: $WidthCSS; height: $HeightCSS\" wrap=\"virtual\">$HtmlValue</textarea>";
178        }
179        $Html .= '</div>';
180        return $Html;
181}
182
183sub IsCompatible
184{
185
186        my $sAgent = $ENV{'HTTP_USER_AGENT'};
187        if(($sAgent =~ /MSIE/i) && !($sAgent =~ /mac/i) && !($sAgent =~ /Opera/i)) {
188                $iVersion = substr($sAgent,index($sAgent,'MSIE') + 5,3);
189                return($iVersion >= 5.5) ;
190        } elsif($sAgent =~ /Gecko\//i) {
191                $iVersion = substr($sAgent,index($sAgent,'Gecko/') + 6,8);
192                return ($iVersion >= 20030210) ;
193        } else {
194                return 0;               # 2.0 PR fix
195        }
196}
197
198sub GetConfigFieldString
199{
200        $sParams = '';
201        $bFirst = 0;
202        foreach my $sKey (keys %Config) {
203                $sValue = $Config{$sKey};
204                if($bFirst == 1) {
205                        $sParams .= '&amp;';
206                } else {
207                        $bFirst = 1;
208                }
209                $k = specialchar_cnv($sKey);
210                $v = specialchar_cnv($sValue);
211                if($sValue eq "true") {
212                        $sParams .= "$k=true";
213                } elsif($sValue eq "false") {
214                        $sParams .= "$k=false";
215                } else {
216                        $sParams .= "$k=$v";
217                }
218        }
219        return($sParams);
220}
221
2221;
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy