preg_match to only allow https:// in an URL - preg-match

I'd like to allow only https:// links to be used as remote avatar images in phpbb to avoid mixed content. This seems to be the code that is used to check whether the entered url is correct (to be found in /phpbb/avatar/driver/remote.php):
if (!preg_match('#^(http|https|ftp)://(?:(.*?\.)*?[a-z0-9\-]+?\.[a-z]{2,4}|(?:\d{1,3}\.){3,5}\d{1,3}):?([0-9]*?).*?\.('. implode('|', $this->allowed_extensions) . ')$#i', $url))
{
$error[] = 'AVATAR_URL_INVALID';
return false;
}
I'd like to add a if{}-condition before this code block to give an informative error message if the user selected an image from a non-secure server. Can anyone help me defining the correct preg_match() string please?

Based on the suggestion by #casimir, I used the following code and it works:
$urlchk = parse_url($url);
$urlscheme = isset($urlchk['scheme']) ? $urlchk['scheme'].'://' : 'http://';
if ($urlscheme=='http://'){
// error message
}

Related

rewriting the URLs dynamically laravel

I am trying to rewrite some of the pages URL in my web app, for example, I have a company details page and I want the URL of this page to contain the name of the company without space or special characters and to have the id too, and I have no idea on how to do that or where to start.
I tried adding the name of the company in the route as a parameter like this:
Route::get('/{RS}-{id}', 'App\Http\Controllers\SiteController#getDetails')->name('details');
It didn't work for some reason and even if it worked there's no way to replace the special characters with their normal form and replace the space with dashes.
This the URL I'm getting: http://mywebsite.come/entreprise_details-13109
this the URL I want: http://mywebsite.come/yoorika-managements-13109 ( with Yoorika Managements being the name of the company )
I searched a lot but I can't seem to find what I am looking for, I just need someone to help me find the right term. thank you!
In route :
Route::get('/{input}', 'App\Http\Controllers\SiteController#getDetails')->name('details');
In controller :
public function getDetails($input)
{
$input_array = explode("-", $input);
$name = $input_array[0];
$id = $input_array[1];
$data = MyModel::where('id', $id)->get();
// return data to view
}
In list view, you may display data like this (table cell ) :
<td> <a href="/{{name .'-'.{{id}}"> {{name .'-'.{{id}}<td/>

Invalid Website URL value, email format expected SagePay Intergration Kit

I'm attempting to use SagePay's Server Intergration PHP Kit, but on installation I'm getting the error Warning: Invalid Website URL value, email format expected this is in /lib/classes/settings.php on line 1127, all I've done to edit the kit is enter the database details that I have set up
any ideas?
For quick fix this issue you can add url of your test site into sagedemo/lib/classes/settings.php in public function setWebsite($website) like this:
public function setWebsite($website)
{
$website = 'http://sagepayintegrationkit.dev/';
if (!empty($website) && SagepayValid::url($website))
{
$this->_website = $website;
}
else
{
trigger_error("Invalid Website URL value, email format expected, '" . $website . "' given", E_USER_WARNING);
}
}
It works form but it looks like banal crutch :)

CodeIgniter - Dynamic URL segments

I was wondering if someone could help me out.
Im building a forum into my codeigniter application and im having a little trouble figuring out how i build the segments.
As per the CI userguide the uri is built as follows
www.application.com/CLASS/METHOD/ARGUMENTS
This is fine except i need to structure that part a bit different.
In my forum i have categories and posts, so to view a category the following url is used
www.application.com/forums
This is fine as its the class name, but i want to have the next segment dynamic, for instance if i have a category called 'mycategory' and a post by the name of 'this-is-my-first-post', then the structure SHOULD be
www.application.com/forums/mycategory/this-is-my-first-post
I cant seem to achieve that because as per the documentation the 'mycategory' needs to be a method, even if i was to do something like /forums/category/mycategory/this-is-my-first-post it still gets confusing.
If anyone has ever done something like this before, could they shed a little light on it for me please, im quite stuck on this.
Cheers,
Nothing is confusing in the document but you are a little bit confused. Let me give you some suggestions.
You create a view where you create hyperlinks to be clicked and in the hyperlink you provide this instruction
First Post
In the controller you can easily get this
$category = $this->uri->segment(3);
$post = $this->uri->segment(4);
And now you can proceed.
If you think your requirements are something else you can use a hack i have created a method for this which dynamically assign segments.
Go to system/core/uri.php and add this method
function assing_segment($n,$num)
{
$this->segments[$n] = $num;
return $this->segments[$n];
}
How to use
$this->uri->assign_segment(3,'mycategory');
$this->uri->assign_segment(4,'this-is-my-first-post');
And if you have error 'The uri you submitted has disallowed characters' then go to application/config/config.php and add - to this
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
You could make a route that forwards to a lookup function.
For example in your routes.php add a line something like;
$route['product/(:any)/(:any)'] = "forums/cat_lookup/$1/$2";
This function would then do a database lookup to find the category.
...
public function cat_lookup($cat, $post) {
$catid = $this->forum_model->get_by_name($cat);
if ($catid == FALSE) {
redirect('/home');
}
$post_id = $this->post_model->get_by_name($post);
/* whatever else you want */
// then call the function you want or load the view
$this->load->view('show_post');
}
...
This method will keep the url looking as you want and handle any problems if the category does not exist.Don't forget you can store the category/posts in your database using underscores and use the uri_title() function to make them pretty,
Set in within config/routes.php
$route['song-album/(:any)/:num'] = 'Home/song_album/$id';
fetch in function with help of uri segment.
$this->uri->segment(1);

Magento returning incorrect customer data on frontend pages

isn't this the right method to get Name of logged in customer?
<?php echo Mage::helper('customer')->getCustomer()->getName(); ?>
I have a website with live chat functionality. Yesterday I have been asked to pass email address and the name of the logged into the user into the Javascript Tracking variable code placed in the head section of the website. So that the operators could see who is on the website and whom are they talking to without any need to ask about their information.
So I passed the information from Magento into the Javascript code but now I see this very strange thing happening. For example,
If I am logged in with credentials Name = John Email =
john12#yahoo.com
Then This name and email variable values are changing with the change of pages. For example if I click on any product page the variable values which I am passing changes to some other user's information.
Name becomes Ricky Email becomes ricky23#gmail.com
this variable values are kept on changing back to john and from john to something else with the change of pages. So operator does not have any idea whom are they talking because the values are kept on changing. Also, user ricky or who ever it changes to also exist in the database. so it is picking up random person from the database.
This is what i did to pass the code to javascript. Please let me know if that is not the right code to pass the information. Please check the php code I am using to fetch information from Magento. Roughly, I receive incorrect value once in 5 times. Please provide some assistance. Thanks in advance.
<?php
$customer = Mage::getSingleton('customer/session')->getCustomer();
$email = $customer->getEmail();
$firstname = $customer->getFirstname();
$lastname= $customer->getLastname();
$name = $firstname . ' ' . $lastname;
?>
<script type="text/javascript">
if (typeof(lpMTagConfig) == "undefined"){ lpMTagConfig = {};}
if (typeof(lpMTagConfig.visitorVar) == "undefined"){ lpMTagConfig.visitorVar = [];}
lpMTagConfig.visitorVar[lpMTagConfig.visitorVar.length] = 'Email=<?php echo $email; ?>';
lpMTagConfig.visitorVar[lpMTagConfig.visitorVar.length] = 'Name=<?php echo $name; ?>';
</script>
I'm also attaching a snap shot
I'd be interested to hear how you're adding this code to the page? Is it in it's own block, or are you adding it to footer.phtml, or similar? If your adding to an existing block be sure to check the block caching settings of that template.
To confirm the caching hypothesis I'd ask the following:
Do you get the same name, all the time, on the same page? When you refresh the page, do you get the same name and email in the Javascript?
Does the problem persist with caching disabled?
This doesn't sound like a singleton problem at all. Each execution of the PHP script is isolated from the others, serving one page request. There's no chance of another customer's object moving between invokations of the script.
It is a matter of understanding the singleton pattern. If you call your code twice:
$customer_1 = Mage::helper('customer')->getCustomer()->getName();
$customer_2 = Mage::helper('customer')->getCustomer()->getName();
you get two different instances of the object. But... if one of them has already implemented a singleton pattern in its constructor or has implemented a singleton getInstance then both objects will actually point to the same thing.
Looking at the customer/helper/Data.php code you can see the function
public function getCustomer()
{
if (empty($this->_customer)) {
$this->_customer = Mage::getSingleton('customer/session')->getCustomer();
}
return $this->_customer;
}
That means that in one of the cases singleton is already implemented/called and in other one - not as the property is already set.
The correct way to work with quote/customer/cart in order to get always the correct data is always to use the singleton pattern.
So using this:
$customer = Mage::getSingleton('customer/session')->getCustomer();
always guarantee that you get the correct customer in that session. And as may be you know singleton pattern is based on registry pattern in app/Mage.php:
public static function getSingleton($modelClass='', array $arguments=array())
{
$registryKey = '_singleton/'.$modelClass;
if (!self::registry($registryKey)) {
self::register($registryKey, self::getModel($modelClass, $arguments));
}
return self::registry($registryKey);
}
and looking at app/Mage.php:
public static function register($key, $value, $graceful = false)
{
if (isset(self::$_registry[$key])) {
if ($graceful) {
return;
}
self::throwException('Mage registry key "'.$key.'" already exists');
}
self::$_registry[$key] = $value;
}
...
public static function registry($key)
{
if (isset(self::$_registry[$key])) {
return self::$_registry[$key];
}
return null;
}
you can see that Magento checks is it is already set. If so, Magento will either throw an Exception, which is the default behavior or return null.
Hope this will help you to understand the issue you face.
I have sorted this out. I have moved the code from footer.phtml to head.phtml and it's working fine now.Values are not changing anymore. If anyone know the logic behind please post and I will change my answer. So far this is working.

CodeIgniter: urlencoded URL in URI segment does not work

I'm trying to put a URL as the value of one of my URI segments in CI. My controller method is defined to accept such an argument. However, when I go to the URL, I get a 404 error. For example:
www.domain.com/foo/urlencoded-url/
Any ideas what's wrong? Should I do this via GET instead?
UPDATE:
// URL that generates 404
http://localhost/myapp/profile_manager/confirm_profile_parent_delete/ugpp_533333338/http%3A%2F%2Flocalhost%2Fmyapp%2Fdashboard%2F
// This is in my profile_manager controller
public function confirm_profile_parent_delete($encrypted_user_group_profile_parent_id = '', $url_current = '')
If I remove the second URI segement, I don't get a 404: http://localhost/myapp/profile_manager/confirm_profile_parent_delete/ugpp_533333338/
It seems that the %2F breaks things for apache.
Possible solutions:
preg_replace the /'s to -'s (or something else) before sending the url then switch it back on the other end.
Set apache to AllowEncodedSlashes On
bit of a hack, but you could even save the url to a session variable or something instead of sending through the url *shrug *
double url encode it before sending
Pass urlendode()'d URL in segment and then decode it with own (MY_*) class:
application/core/MY_URI.php:
class MY_URI extends CI_URI {
function _filter_uri($str)
{
return rawurldecode(parent::_filter_uri($str));
}
}
// EOF
You may need to change the rule in config/route.php to accept the encoded characters in URL. Also you can take a look at some of the solution from below articles:
http://codeigniter.com/forums/viewthread/81365/
http://sholsinger.com/archive/2009/04/passing-email-addresses-in-urls-with-codeigniter/
Passing URL in Codeigniter URL segment
I actually had to do urlencode(urlencode(urlencode(
and urldecode(urldecode(urldecode(
3 times!! and it finally worked, twice didn't cut it.
try
function __autoload($class){
if(!empty($_SERVER['REQUEST_URI'])){
$_SERVER['REQUEST_URI'] = $_SERVER['REDIRECT_QUERY_STRING'] = $_SERVER['QUERY_STRING'] = $_SERVER['REDIRECT_URL'] = $_SERVER['argv'][0] = urldecode($_SERVER['REQUEST_URI']);
}
}
in config.php
this method work for me
This is very old, but I thought I'd share my solution.
Instead of accepting the parameter as a url path, accept it as a get variable:
http://localhost/myapp/profile_manager/confirm_profile_parent_delete/ugpp_533333338?url_current=http%3A%2F%2Flocalhost%2Fmyapp%2Fdashboard%2F
and in code:
function confirm_profile_parent_delete($encrypted_user_group_profile_parent_id = '') {
$url_current = $this->input->get('url_current');
...
This seems to work.

Resources