using html_escape and auto_link on same variable in codeigniter - codeigniter

I want to display user comments on my site and I am escaping the output using html_escape (htmlspecialchars in Codeigniter). But I also want to activate URLs in the comments using Codeigniter's auto_link function.
How can I apply both functions to the same variable/content?
<?php echo html_escape($review); ?>
<?php echo auto_link($review); ?>
I have to use html_escape, because I don't trust the user content; but I would like to show URLs entered, if possible. Note: there is no conflict between the characters in html_escape and in auto_link.

auto_link(html_escape($review))
However, if you don't trust your users, don't parse their links. Assuming your users provide insightful comments like
Free cheap drugs at http://example.com/
auto_link(html_escape($review)) will still parse the link.

Related

Encode E-Mail in Laravel 5.7

I have a page who i can see all user profiles.
So i have a foreach who show me all my profiles.
i want to display the user email
this is the way i do:
{{$user->email}}
but i want to encode the mailto because i want to prevent spam mails
so i want it like:
<a href="mailto:mail#example.com">
instead of
<mail#example.com
does anyone know how i can do this in laravel?
because there is a tool like this:http://www.wbwip.com/wbw/emailencoder.html
but here i can only encode one email
is there a way in laravel who i can say encode($user->email) and then i have the mail like on the top??
thank you so much!
i searched everywhere but i can not find anything
maybe what you need is htmlentities in php
htmlentities — Convert all applicable characters to HTML entities
read more here
You should try this:
{!! link_to("mailto:".$user->email, $user->email) !!}

laravel - How to limit text string not including html tags

I have a field in database which contains html tags. I want to show that field in the view without showing HTML tags. I used str_limit($value, $limit = 100, $end = '...') but it shows the html tags also. Any idea?
Use the strip_tags() function.
For example this filters the <b></b> tags:
<?php
echo strip_tags("Hello <b>world!</b>");
?>
In your case would be something along the lines of:
<?php
strip_tags($value);
?>
See the documentation.
Check this answer.
str_limit only limits the number of characters. What you need to do is parse the database string and remove anything that looks like a tag. I've never used it, but the php strip_tags function might be more what you're looking for.

Using customer first name in order confirmation email

I've seen a bunch of articles (mostly 3-5 years old) detailing various methods of getting the customer's first name into the order confirmation email but I just get either a blank, or unparsed PHP.
For example this:
<?php echo $this->__('Hello, %s', Mage::getSingleton('customer/session')->getCustomer()->getFirstname()); ?>
Renders in the email like this:
__('Hello, %s', Mage::getSingleton('customer/session')->getCustomer()->getFirstname()); ?>
Could somebody point me in the right direction? This is on Community Edition 1.7.
Thanks
[EDITED] You can't use PHP code in your email-templates directly. If you want to insert dynamic data into your email-templates, you have two possibilities:
a) In every transactional email you can access the methods of the model, which is in charge for the transactional email, it is: for mails dealing with orders, this is the order-Model, for mails dealing with newsletters, this is the newsletter-model and so on. You can access the methods with the syntax:
{{var model.method()}}
So, in your case, to access the customer's first name in an order confirmation email, you need to look for a suiting method in the order Model, which is getCustomerFirstname() .
Then you can call it, following the given syntax:
{{var order.getCustomerFirstname()}}
b) You can include dynamic data into your email-template by creating a custom phtml-template and including it into your email-template via the {{block}} directive (as pointed out by benmarks in the comment below)

Action you have requested is not allowed error

I made a module named Gallery which works fine on my localhost with version 2.0.3, but when using version 2.1.0 on a remote site I can not submit a form and I get the error:
The action you have requested is not allowed.
Why is this?
I agree with #Jhourlad Estrella on fixing the problems instead of disabling a security feature, however I feel that the real problem is with the hidden input field that holds the token.
Instead of using plain HTML to create a form element use the the form_open() and form_close() helper functions. The reason why is because when you use the helper function it automatically inserts the csrf token as a hidden field in the form.
You could do this manually as well by adding the token as a hidden input field in the form
<input type="hidden" name="csrf_hash_name" value="your-hash-value-here">
Doing it this way will allow you to stay protected from CSRF attacks and fix the problem you are having.
Hope this helps someone else out there as this was driving me nuts the first time figuring this out.
It is a Codeigniter error related to the CSRF protection. You can cancel it in cms/config/config.php
On matters of programming, you don't go around problems, you fix it. What I mean to say is, this feature won't be here if it is unusable: 'coz it is and it works for me. You just have a problem on the implementation.
My answer: Remove all dashes, periods and any other non-alphanumeric characters from the values of following entries on application/config/config.php as seen below:
$config['sess_cookie_name'] = 'mycookiename'; //instead of "my_cookie_name"
$config['csrf_token_name'] = 'mycsrftoken'; //instead of "my.csrf.token"
$config['csrf_cookie_name'] = 'mycsrfcookie'; //instead of "my/csrf/cookie"
BTW, dashes sometimes work but I suggest using single words whenever possible when naming config values. Not unless you have the time and skills to study Codeigniter's core files related to what ever you are working on just to make sure it's safe to do so.
Anyways, I hope this help somebody out there even though my answer is more than a year late.
I have a form that was built outside of CI (in Joomla), but that I wanted to process with CI. My fix was to selectively disable csrf for specific referrers. I added this to config, directly after the default config options for csrf:
/* Set csrf off for specific referrers */
$csrf_off = array(
"http://yourdomain.com/your-form-url",
"http://yourdomain.com/some-other-url"
);
if (isset($_SERVER["HTTP_REFERER"])) {
if (in_array($_SERVER["HTTP_REFERER"],$csrf_off)) {
$config['csrf_protection'] = false;
}
}
This disables csrf protection for specific URLs in the $csrf_off array, but leaves it intact for all other requests.
I have found using the form helper functions
Example
<?php echo form_open('controller/function');?>
<?php echo form_input('username', 'Username');?>
<?php echo form_close();?>
Using the helper functions like above should stop the CSRF error message showing.
If I don't use echo form_input() if I place just normal input will trigger the CSRF error when reload.
<?php echo form_open('controller/function');?>
<input type="text" name="username" />
<?php echo form_close();?>
So I recommend using all form helper functions now.
It is an old question but this same problem did cost me so much time that I wanted to share what the problem was in my case. It may help someone.
I am using Codeigniter 3.0.6 and CommunityAuth 3 together with it and I was getting this error after a login.
It was confusing since the problem would sometimes happen and would not other times.
My 'base_url' in CI's config.php was set to something like 'www.mysite.com'
When you browse the site with 'mysite.com' (notice 'www' is not in the address) and you do a form submission that uses CI's 'base_url' setting, like CommunityAuth's login does, then CSRF check fails and you get 'The action you have requested is not allowed.' error.
This error is thrown by the function csrf_show_error() in system/core/Security.php when the CSRF token in $_COOKIE doesn't match your $_POST['csrf_token_name'].
Inside config.php, I had to ensure that $config['cookie_domain'] matched $config['base_url'], without the protocol (i.e. http(s)://).
Otherwise, the cookie wasn't being passed which meant the match couldn't be made.
Use the codeigniter form opener like this:
<php echo form_open(url,method,attributes);?>
see codeigniter form documentation for more.
This is probably a rare case, but I didn't see my issue since my server has many different domain names that are very similar. The problem was that I was landing on a domain that was completely wrong, but since "The action you have requested is not allowed." error takes precedence over " 404 Not Found Error" I couldn't see it. My problem was that I didn't change my base_url to the correct domain. So if none of the above solutions work for you, you might check your settings for $config['base_url'] in application/config.
For me the problem was that I was loading the view in the index, than I changed as follow and it worked:
public function index()
{
// Load Login Page
redirect('login/login_page','refresh');
}
public function login_page()
{
$data['title'] = 'Login Page';
$this->load->view('templates/header', $data);
$this->load->view('users/login_view', $data);
$this->load->view('templates/footer');
}
Im Using Codeigniter 3 same problem with
The action you have requested is not allowed.
Based on Isaac Pak's point, i changed my base_url to what i usally typed at the address bar. like this...
instead of putting
http://www.domain.org
i write it this way..
http://domain.org
since my base_url() is just..
$config['base_url'] = 'http://domain.org/';
the fix works for my site...

How do I direct to www.whatever.com using CodeIgniter's "anchor" function?

I'm using CodeIgniter (because it's awesome) and I have something like:
<?php echo anchor("/", "whatever.com" ); ?>
However, this results in http://www.whatever.com/.html which is not right. Help?
Is there any reason why you are using the anchor? It's purpose is to help you create anchors for your site, not really for external sites. If you are linking to an external site, just create a regular link?
The anchor helper parameters are
anchor(uri segments, text, attributes)
If you want to use the anchor function in CodeIgniter to link to an external site you must include the protocol part of the URL. So if you want to link to www.whatever.com you must write
anchor('http://www.whatever.com', 'The site name');
If you don't include the protocol part of the URL, CodeIgniter will think you mean an internal link and will create a link relative to the base URL of your site.
Digging into the CodeIgniter URL helper code you find
$site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
www.whatever.com is not matching the regular expression so you are getting an anchor with a URL relative to the site's base URL.
It has added .html to the end because you have a url_suffix in your config. As Wil says, anchor is not really meant for external sites.

Resources