Ticket #2220: 2220.patch

File 2220.patch, 9.9 KB (added by asuter, 22 months ago)

Patch

  • editor/dialog/fck_link/fck_link.js

     
    1 /* 
     1/* 
    22 * FCKeditor - The text editor for Internet - http://www.fckeditor.net 
    33 * Copyright (C) 2003-2008 Frederico Caldeira Knabben 
    44 * 
     
    7777 
    7878var oParser = new Object() ; 
    7979 
    80 oParser.ParseEMailUrl = function( emailUrl ) 
     80// This method simply returns the two inputs in numerical order. You can even 
     81// provide strings, as the method would parseInt() the values. 
     82oParser.SortNumerical = function(a, b) 
    8183{ 
     84        return parseInt( a ) - parseInt( b ) ; 
     85} 
     86 
     87oParser.ParseEMailParams = function(sParams) 
     88{ 
     89        // Initialize the oEMailParams object. 
     90        var oEMailParams = new Object() ; 
     91        oEMailParams.Subject = '' ; 
     92        oEMailParams.Body = '' ; 
     93 
     94        var aMatch = sParams.match( /(^|^\?|&)subject=([^&]+)/i ) ; 
     95        if ( aMatch ) oEMailParams.Subject = decodeURIComponent( aMatch[2] ) ; 
     96 
     97        aMatch = sParams.match( /(^|^\?|&)body=([^&]+)/i ) ; 
     98        if ( aMatch ) oEMailParams.Body = decodeURIComponent( aMatch[2] ) ; 
     99 
     100        return oEMailParams ; 
     101} 
     102 
     103// This method returns either an object containing the email info, or FALSE 
     104// if the parameter is not an email link. 
     105oParser.ParseEMailUri = function( sUrl ) 
     106{ 
    82107        // Initializes the EMailInfo object. 
    83108        var oEMailInfo = new Object() ; 
    84         oEMailInfo.Address      = '' ; 
    85         oEMailInfo.Subject      = '' ; 
    86         oEMailInfo.Body         = '' ; 
     109        oEMailInfo.Address = '' ; 
     110        oEMailInfo.Subject = '' ; 
     111        oEMailInfo.Body = '' ; 
    87112 
    88         var oParts = emailUrl.match( /^([^\?]+)\??(.+)?/ ) ; 
    89         if ( oParts ) 
     113        var aLinkInfo = sUrl.match( /^(\w+):(.*)$/ ) ; 
     114        if ( aLinkInfo && aLinkInfo[1] == 'mailto' ) 
    90115        { 
    91                 // Set the e-mail address. 
    92                 oEMailInfo.Address = oParts[1] ; 
     116                // This seems to be an unprotected email link. 
     117                var aParts = aLinkInfo[2].match( /^([^\?]+)\??(.+)?/ ) ; 
     118                if ( aParts ) 
     119                { 
     120                        // Set the e-mail address. 
     121                        oEMailInfo.Address = aParts[1] ; 
    93122 
    94                 // Look for the optional e-mail parameters. 
    95                 if ( oParts[2] ) 
     123                        // Look for the optional e-mail parameters. 
     124                        if ( aParts[2] ) 
     125                        { 
     126                                var oEMailParams = oParser.ParseEMailParams( aParts[2] ) ; 
     127                                oEMailInfo.Subject = oEMailParams.Subject ; 
     128                                oEMailInfo.Body = oEMailParams.Body ; 
     129                        } 
     130                } 
     131                return oEMailInfo ; 
     132        } 
     133        else if ( aLinkInfo && aLinkInfo[1] == 'javascript' ) 
     134        { 
     135                // This may be a protected email. 
     136 
     137                // Try to match the url against the EMailProtectionFunction. 
     138                var func = FCKConfig.EMailProtectionFunction ; 
     139                if ( func != null ) 
    96140                { 
    97                         var oMatch = oParts[2].match( /(^|&)subject=([^&]+)/i ) ; 
    98                         if ( oMatch ) oEMailInfo.Subject = decodeURIComponent( oMatch[2] ) ; 
     141                        try 
     142                        { 
     143                                // Escape special chars. 
     144                                func = func.replace( /\//g, '\\/' ) ; 
     145                                func = func.replace( /\^/g, '\\^' ) ; 
     146                                func = func.replace( /\$/g, '\\$' ) ; 
     147                                func = func.replace( /\*/g, '\\*' ) ; 
     148                                func = func.replace( /\+/g, '\\+' ) ; 
     149                                func = func.replace( /\./g, '\\.' ) ; 
     150                                func = func.replace( /\?/g, '\\?' ) ; 
     151                                func = func.replace( /\(/g, '\\(' ) ; 
     152                                func = func.replace( /\)/g, '\\)' ) ; 
     153                                func = func.replace( /\[/g, '\\[' ) ; 
     154                                func = func.replace( /\]/g, '\\]' ) ; 
    99155 
    100                         oMatch = oParts[2].match( /(^|&)body=([^&]+)/i ) ; 
    101                         if ( oMatch ) oEMailInfo.Body = decodeURIComponent( oMatch[2] ) ; 
     156                                // Define the possible keys. 
     157                                var keys = new Array('NAME', 'DOMAIN', 'SUBJECT', 'BODY') ; 
     158 
     159                                // Get the order of the keys (hold them in the array <pos>) and 
     160                                // the function replaced by regular expression patterns. 
     161                                var sFunc = func ; 
     162                                var pos = new Array() ; 
     163                                for ( var i = 0 ; i < keys.length ; i ++ ) 
     164                                { 
     165                                        var rexp = new RegExp( keys[i] ) ; 
     166                                        var p = func.search( rexp ) ; 
     167                                        if ( p >= 0 ) 
     168                                        { 
     169                                                sFunc = sFunc.replace( rexp, '\'([^\']*)\'' ) ; 
     170                                                pos[pos.length] = p + ':' + keys[i] ; 
     171                                        } 
     172                                } 
     173 
     174                                // Sort the available keys. 
     175                                pos.sort( oParser.SortNumerical ) ; 
     176 
     177                                // Replace the excaped single quotes in the url, such they do 
     178                                // not affect the regexp afterwards. 
     179                                aLinkInfo[2] = aLinkInfo[2].replace( /\\'/g, '###SINGLE_QUOTE###' ) ; 
     180 
     181                                // Create the regexp and execute it. 
     182                                var rFunc = new RegExp( '^' + sFunc + '$' ) ; 
     183                                var aMatch = rFunc.exec( aLinkInfo[2] ) ; 
     184                                if ( aMatch ) 
     185                                { 
     186                                        var aInfo = new Array(); 
     187                                        for ( var i = 1 ; i < aMatch.length ; i ++ ) 
     188                                        { 
     189                                                var k = pos[i-1].match(/^\d+:(.+)$/) ; 
     190                                                aInfo[k[1]] = aMatch[i].replace(/###SINGLE_QUOTE###/, '\'') ; 
     191                                        } 
     192 
     193                                        // Fill the EMailInfo object that will be returned 
     194                                        oEMailInfo.Address = aInfo['NAME'] + '@' + aInfo['DOMAIN'] ; 
     195                                        oEMailInfo.Subject = decodeURIComponent( aInfo['SUBJECT'] ) ; 
     196                                        oEMailInfo.Body = decodeURIComponent( aInfo['BODY'] ) ; 
     197 
     198                                        return oEMailInfo ; 
     199                                } 
     200                        } 
     201                        catch (e) 
     202                        { 
     203                        } 
    102204                } 
     205 
     206                // Try to match the email against the encode protection. 
     207                var aMatch = aLinkInfo[2].match( /^location\.href='mailto:'\+(String\.fromCharCode\([\d,]+\))\+'(.*)'$/ ) ; 
     208                if ( aMatch ) 
     209                { 
     210                        // The link is encoded 
     211                        oEMailInfo.Address = eval( aMatch[1] ) ; 
     212                        if ( aMatch[2] ) 
     213                        { 
     214                                var oEMailParams = oParser.ParseEMailParams( aMatch[2] ) ; 
     215                                oEMailInfo.Subject = oEMailParams.Subject ; 
     216                                oEMailInfo.Body = oEMailParams.Body ; 
     217                        } 
     218                        return oEMailInfo ; 
     219                } 
    103220        } 
    104  
    105         return oEMailInfo ; 
     221        return false; 
    106222} 
    107223 
    108224oParser.CreateEMailUri = function( address, subject, body ) 
    109225{ 
    110         var sBaseUri = 'mailto:' + address ; 
     226        // Switch for the EMailProtection setting. 
     227        switch ( FCKConfig.EMailProtection ) 
     228        { 
     229                case 'function' : 
     230                        var func = FCKConfig.EMailProtectionFunction ; 
     231                        if ( func == null ) 
     232                        { 
     233                                if ( FCKConfig.Debug ) 
     234                                { 
     235                                        alert('EMailProtection alert!\nNo function defined. Please set "FCKConfig.EMailProtectionFunction"') ; 
     236                                } 
     237                                return ''; 
     238                        } 
    111239 
    112         var sParams = '' ; 
     240                        // Split the email address into name and domain parts. 
     241                        var atPosition = address.indexOf('@') ; 
     242                        var name = '' ; 
     243                        var domain = '' ; 
     244                        if ( atPosition >= 0 ) 
     245                        { 
     246                                name = address.substring(0, atPosition) ; 
     247                                domain = address.substring(atPosition + 1) ; 
     248                        } 
    113249 
    114         if ( subject.length > 0 ) 
    115                 sParams = '?subject=' + encodeURIComponent( subject ) ; 
     250                        // Replace the keys by their values (embedded in single quotes). 
     251                        func = func.replace(/NAME/g, "'" + name.replace(/'/g, '\\\'') + "'") ; 
     252                        func = func.replace(/DOMAIN/g, "'" + domain.replace(/'/g, '\\\'') + "'") ; 
     253                        func = func.replace(/SUBJECT/g, "'" + encodeURIComponent( subject ).replace(/'/g, '\\\'') + "'") ; 
     254                        func = func.replace(/BODY/g, "'" + encodeURIComponent( body ).replace(/'/g, '\\\'') + "'") ; 
     255                        return 'javascript:' + func ; 
     256                        break ; 
     257                case 'encode' : 
     258                        var sAddress = ''; 
     259                        var sParams = '' ; 
    116260 
    117         if ( body.length > 0 ) 
    118         { 
    119                 sParams += ( sParams.length == 0 ? '?' : '&' ) ; 
    120                 sParams += 'body=' + encodeURIComponent( body ) ; 
     261                        if ( subject.length > 0 ) 
     262                                sParams = '?subject=' + encodeURIComponent( subject ) ; 
     263 
     264                        if ( body.length > 0 ) 
     265                        { 
     266                                sParams += ( sParams.length == 0 ? '?' : '&' ) ; 
     267                                sParams += 'body=' + encodeURIComponent( body ) ; 
     268                        } 
     269 
     270                        for ( var i = 0 ; i < address.length ; i ++ ) { 
     271                                if ( i > 0 ) { sAddress += ','; } 
     272                                sAddress += address.charCodeAt(i) ; 
     273                        } 
     274 
     275                        return 'javascript:location.href=\'mailto:\'+String.fromCharCode(' + sAddress + ')+\'' + sParams + '\'' ; 
     276                        break ; 
     277                default : // 'none' 
     278                        var sBaseUri = 'mailto:' + address ; 
     279 
     280                        var sParams = '' ; 
     281 
     282                        if ( subject.length > 0 ) 
     283                                sParams = '?subject=' + encodeURIComponent( subject ) ; 
     284 
     285                        if ( body.length > 0 ) 
     286                        { 
     287                                sParams += ( sParams.length == 0 ? '?' : '&' ) ; 
     288                                sParams += 'body=' + encodeURIComponent( body ) ; 
     289                        } 
     290 
     291                        return sBaseUri + sParams ; 
    121292        } 
    122  
    123         return sBaseUri + sParams ; 
    124293} 
    125294 
    126295//#### Initialization Code 
     
    263432        // Search for the protocol. 
    264433        var sProtocol = oRegex.UriProtocol.exec( sHRef ) ; 
    265434 
    266         if ( sProtocol ) 
     435        // Search for a protected email link. 
     436        var oEMailInfo = oParser.ParseEMailUri( sHRef ); 
     437 
     438        if ( oEMailInfo ) 
    267439        { 
     440                sType = 'email' ; 
     441 
     442                GetE('txtEMailAddress').value = oEMailInfo.Address ; 
     443                GetE('txtEMailSubject').value = oEMailInfo.Subject ; 
     444                GetE('txtEMailBody').value    = oEMailInfo.Body ; 
     445        } 
     446        else if ( sProtocol ) 
     447        { 
    268448                sProtocol = sProtocol[0].toLowerCase() ; 
    269449                GetE('cmbLinkProtocol').value = sProtocol ; 
    270450 
    271451                // Remove the protocol and get the remaining URL. 
    272452                var sUrl = sHRef.replace( oRegex.UriProtocol, '' ) ; 
    273  
    274                 if ( sProtocol == 'mailto:' )   // It is an e-mail link. 
    275                 { 
    276                         sType = 'email' ; 
    277  
    278                         var oEMailInfo = oParser.ParseEMailUrl( sUrl ) ; 
    279                         GetE('txtEMailAddress').value   = oEMailInfo.Address ; 
    280                         GetE('txtEMailSubject').value   = oEMailInfo.Subject ; 
    281                         GetE('txtEMailBody').value              = oEMailInfo.Body ; 
    282                 } 
    283                 else                            // It is a normal link. 
    284                 { 
    285                         sType = 'url' ; 
    286                         GetE('txtUrl').value = sUrl ; 
    287                 } 
     453                sType = 'url' ; 
     454                GetE('txtUrl').value = sUrl ; 
    288455        } 
    289456        else if ( sHRef.substr(0,1) == '#' && sHRef.length > 1 )        // It is an anchor link. 
    290457        { 
  • fckconfig.js

     
    1 /* 
     1/* 
    22 * FCKeditor - The text editor for Internet - http://www.fckeditor.net 
    33 * Copyright (C) 2003-2008 Frederico Caldeira Knabben 
    44 * 
     
    7676FCKConfig.FormatOutput          = true ; 
    7777FCKConfig.FormatIndentator      = '    ' ; 
    7878 
     79FCKConfig.EMailProtection = 'encode' ; // none | encode | function 
     80FCKConfig.EMailProtectionFunction = 'mt(NAME,DOMAIN,SUBJECT,BODY)' ; 
     81 
    7982FCKConfig.StartupFocus  = false ; 
    8083FCKConfig.ForcePasteAsPlainText = false ; 
    8184FCKConfig.AutoDetectPasteFromWord = true ;      // IE only.