Forum for discussing general topics related to Couch.
10 posts Page 1 of 1
Hi

I am trying to cloak a email where the admin user can add edit it in the cms side and it's using a globals masterpage so I have done the following but no email address is displayed

My kfunctions.php looks like the following

Code: Select all
<?php
$FUNCS->register_shortcode( 'googlemap', 'googlemap_handler' );
function googlemap_handler( $params, $content=null ){
    global $FUNCS, $CTX;

    extract( $FUNCS->get_named_vars(array(
        'map' => ''
    ), $params) );

    return '<iframe src="'.$map.'" width="100%" height="400px" id="mapcanvas" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" style="border:0" allowfullscreen><div id="map" style="height:400px;width:100%;"></div></iframe>';
}

// 6.
// Obfuscate email
// Usage: [mailto]email@mydomain.com[/mailto]
$FUNCS->register_shortcode( 'mailto', 'mailto_handler' );
function mailto_handler( $params, $content=null ){
global $FUNCS;

// Create Couch script.. we'll use the 'cloak_email' tag to encrypt email
$html = "<cms:cloak_email \"<cms:get_custom_field 'header_email_link' masterpage='globalstwo.php' />\" />";

// Pass on the code to Couch for execution using the 'embed' function
return $FUNCS->embed( $html, $is_code=1 );
};
?>


My includes/header.php looks like the following

Code: Select all
<li>
<a href="<cms:do_shortcodes><cms:show header_email_link /></cms:do_shortcodes>">
<i class="bx bx-envelope"></i>
Email: <cms:do_shortcodes><cms:show header_email_link /></cms:do_shortcodes>
</a>
</li>


In the cms side I added [mailto]email@domain.co.uk[/mailto]

I followed this guide viewtopic.php?f=4&t=7044 but unsure what I have done wrong

Update: I thought it may of been due to the field type being text in the globalstwo.php file so changed it to richtext and can see it's now richtext type in the cms side but does still not show the email address. Is there something else I need to do for shortcodes to work in includes/header.php file?
Just a thought, I found email guardian post and thought wonder if that would work: viewtopic.php?f=8&t=11001

I have added the code in the kfunctions file and the code in my includes/header.php file so looks like the following

kfunctions.php file in the root
Code: Select all
class CustomTags {
    static function email_guardian( $params, $node ){
        global $FUNCS;
        foreach( $node->children as $child ){
            $html .= $child->get_HTML();
        }
        preg_match_all('/\b[^\s]+@[^\s]+/', strip_tags(htmlspecialchars_decode($html, ENT_QUOTES)), $emails);
        foreach($emails[0] as $email){
            $email = trim(strtolower($email), ".,;:?!\"\'‘’‚“”„‹›-+&#*%$@~`^()|<>[]{}/\\");
            $code = $FUNCS->embed( "<cms:cloak_email email='{$email}' />", $is_code=1 );
            $pos = strpos($html, $email);
            if ($pos !== false) {
                $html = substr_replace($html, $code, $pos, strlen($email));
            }
        }
        return $html;
    }
}
$FUNCS->register_tag( 'email_guardian', array('CustomTags', 'email_guardian') );


includes/header.php
Code: Select all
<a href="mailto:<cms:email_guardian><cms:get_custom_field 'header_email_link' masterpage='globalstwo.php'/>"></cms:email_guardian>
<i class="bx bx-envelope"></i>
Email: <cms:email_guardian><cms:get_custom_field 'header_email_link' masterpage='globalstwo.php'/></cms:email_guardian>
</a>


Just unsure if it's worked or not, the email address is showing and the mailto link looks correct but unsure if it's scanning for email addresses and automatically obfuscate them using Couch's cloak_email tag
I have gone back to going to get the first method work and amending the code to the following

kfunctions.php file
Code: Select all
// 6.
// Obfuscate email
// Usage: [mailto]email@mydomain.com[/mailto]
$FUNCS->register_shortcode( 'mailto', 'mailto_handler' );
function mailto_handler( $params, $content=null ){
global $FUNCS;

// Create Couch script.. we'll use the 'cloak_email' tag to encrypt email
$html = "<cms:cloak_email \"<cms:get_custom_field 'header_email_address' masterpage='globalstwo.php' />\" />";

// Pass on the code to Couch for execution using the 'embed' function
return $FUNCS->embed( $html, $is_code=1 );
}


includes/header.php file
Code: Select all
<a href="mailto:<cms:get_custom_field 'header_email_link' masterpage='globalstwo.php'/>">
<i class="bx bx-envelope"></i>
Email: <cms:do_shortcodes><cms:show header_email_address /></cms:do_shortcodes>
</a>


The code above outputs the following in the source code, can't work out why the email address is not showing on the site

Code: Select all
<a href="mailto:email@domain.co.uk">
<i class="bx bx-envelope"></i>
Email:
</a>


In the shortcode I am using [mailto]email@domain.co.uk[/mailto]
So sorry will def need help on this one as can't seem to get it working, I have tried richtext and text fields and done the shortcode on just the
Code: Select all
<a href="mailto:
and on displaying the email address but really don't get it and nothing seems to work, all I can think is that shortcodes don't work in includes/header.php files
Sorry went with this way in the end and managed to get it working with the help of Tim: viewtopic.php?f=8&t=11001&p=35671
Hello,

after hours of struggle, I just can't obfuscate email neither with shortcode [mailto] nor with email guardian.
I don't know if the kfunctions.php is in the wrong place - I tried in addons, in the root of the site (next to index.php).
Nothing works.

Of course I have used <cms:do_shortcodes></cms:do_shortcodes> around the editable region in question in my index.php.

I have in kfunctions:

Code: Select all
<?php
 
   // 6.
   // Obfuscate email
   // Usage: [mailto]email@mydomain.com[/mailto]
   $FUNCS->register_shortcode( 'mailto', 'mailto_handler' );
   function mailto_handler( $params, $content=null ){
      global $FUNCS;

      // Create Couch script.. we'll use the 'cloak_email' tag to encrypt email
      $html = "<cms:cloak_email email='{$content}' />";

      // Pass on the code to Couch for execution using the 'embed' function
      return $FUNCS->embed( $html, $is_code=1 );
   }
   
 


What am I doing wrong?
@aztk
The preferred location for the kfunctions.php would be within /couch/addons/ (there already exists a sample file in that location that can be renamed to kfunctions.php).

Assuming the code is in the proper location, please do a view-source: of the page where the short-code is being used - if you happen to see the string "[mailto]email@mydomain.com[/mailto]" verbatim, then obviously the obfuscation did not happen; else, either you should see the generated JS code or something that should show what is going wrong.
Hi,

The kfunctions.php is now back in addons folder.

in my index.php I have:

Code: Select all
<cms:do_shortcodes><cms:editable name='for_patients' type='richtext' toolbar='full' group='offer'>whateva content</cms:editable></cms:do_shortcodes>


I have pasted in the field's editor [mailto]email@mydomain.com[/mailto] and what I get on the site is the same plain text "[mailto]email@mydomain.com[/mailto]".

It doesn't seem to work and there's not a hint of what is going on...

Ah, by the way, is there a way to avoid the editor do html encode on the text in the fields' editor, at least when editing the "source"? This will resolve my problem - I could use a different obfuscation method, if only the editor didn't break my code each time it saves.

Thanks!
@aztk, thanks for sending me the source files.

I had a look and found that you were trying the use <cms:do_shortcodes> within the <cms:template> block (which is meant for only 'defining' the regions, as opposed to outputting their contents) -
Code: Select all
<cms:template..>
    ..
    <cms:do_shortcodes><cms:editable name='for_patients' type='richtext' toolbar='full' group='offer'>whateva content</cms:editable></cms:do_shortcodes>
    ..
</cms:template>

The correct place to use <cms:do_shortcodes> is where you actually display the contents of the editable region in question.
In your template, this is a little further in the body -
Code: Select all
<div class="col-lg-6 col-md-12 pb-4">
                       
            <cms:show for_patients />

</div>

Please modify it to make it as follows and that should solve the issue -
Code: Select all
<div class="col-lg-6 col-md-12 pb-4">
                       
    <cms:do_shortcodes><cms:show for_patients /></cms:do_shortcodes>

</div>

Hope this helps.
Thank you!
This works.

Is there a way to use [mailto] shortcode and incorporate the mail subject + target="_blank"?

By the way I found how to do it with email guardian, but there's a curious side effect - if some of my articles (between the <cms:email_guardian></cms:email_guardian>) do not contain a mail address, they do not open (the modal does not open or they open with just a title, depending where <cms:email_guardian> tags are).
10 posts Page 1 of 1