Page 1 of 1

UCP Profile valid URL & specific domain

Posted: Tue Jan 17, 2012 3:10 am
by Blue Blood
So I want to specify the domain Facebook & Twitter.
I know how to check for a valid URL but not specific domain.

How would I do this.
Thanks!!

Code: Select all

						'facebook'		=> array(
							array('string', true, 12, 255),
							array('match', true, '#^http[s]?://(.*?\.)*?[a-z0-9\-]+\.[a-z]{2,4}#i')),						
						'twitter'		=> array(
							array('string', true, 12, 255),
							array('match', true, '#^http[s]?://(.*?\.)*?[a-z0-9\-]+\.[a-z]{2,4}#i')),

Re: UCP Profile help

Posted: Tue Jan 17, 2012 5:15 am
by Blue Blood
So I have added this to functions_user.php

Code: Select all

    function validate_facebook($facebook, $allowed_facebook= false)
    {
       global $config, $db, $user;

       $facebook= strtolower($facebook);
       $validdomains = array('facebook.com');
       foreach ($validdomains as $validdomain) $validdomains = ( preg_match("/[@\.]$validdomain$/is", $facebook));
       if ( !$validdomains )
       {
          return 'WRONG_DATA_FACEBOOK';
       }

       return false;
    }
& added this to ucp_profile.php

Code: Select all

						'facebook'		=> array(
							array('string', true, 12, 255),
							array('match', true, '#^http[s]?://(.*?\.)*?[a-z0-9\-]+\.[a-z]{2,4}#i'),
							array('facebook')),	
This problem now is it will only allow
http://www.facebook.com
& NOT
http://www.facebook.com/profile.php?id=########

I only need it to check the domain not the whole address..

Re: UCP Profile help

Posted: Tue Jan 17, 2012 5:41 am
by Blue Blood
I figured it out!! made a few changes and here it is!!!

BUT

Now if the field is blank the error shows.

So my question now is how do I allow a blank field..
Thanks!!

IN functions_user.php

Code: Select all

    function validate_facebook($facebook, $allowed_facebook= false)
    {
       global $config, $db, $user;

       $validdomains = array('facebook.com/');
       $facebook == $validdomains;
       foreach ($validdomains as $validdomain) $validdomains = ( preg_match("/[@\.]$validdomain", $facebook));
       if ( !$validdomains )
       {
          return 'WRONG_DATA_FACEBOOK';
       }

       return false;
    }

/**
* Validate Twitter Domain
*
* @author ProjectBlueBlood.com
*/
    function validate_twitter($twitter, $allowed_twitter= false)
    {
       global $config, $db, $user;

       $validdomains = array('twitter.com/');
       $twitter== $validdomains;
       foreach ($validdomains as $validdomain) $validdomains = ( preg_match("/[@\.]$validdomain", $twitter));
       if ( !$validdomains )
       {
          return 'WRONG_DATA_TWITTER';
       }

       return false;
    }
IN ucp_profile.php

Code: Select all

						'facebook'		=> array('facebook'),
						'twitter'		=> array('twitter'),

Re: UCP Profile valid URL & specific domain

Posted: Tue Jan 17, 2012 2:24 pm
by Blue Blood
OK here is my solution. Not sure if this is the most efficient way but it works.

Code: Select all

/**
* Validate Facebook Domain
*
* @author ProjectBlueBlood.com
*/
    function validate_facebook($facebook, $allowed_facebook = false)
    {
       global $config, $db, $user;

       if ( $facebook )
       {
			$validdomains = array('facebook.com/');
			$facebook == $validdomains;
			foreach ($validdomains as $validdomain) $validdomains = ( preg_match("/[@\.]$validdomain", $facebook));
			if ( !$validdomains )
			{
				return 'WRONG_DATA_FACEBOOK';
			}
       }
	   


       return false;
    }

/**
* Validate Twitter Domain
*
* @author ProjectBlueBlood.com
*/
	function validate_twitter($twitter, $allowed_twitter= false)
    {
		global $config, $db, $user;
       
		if ( $twitter )
		{
			$validdomains = array('twitter.com/');
			$twitter == $validdomains;
			foreach ($validdomains as $validdomain) $validdomains = ( preg_match("/[@\.]$validdomain", $twitter));
			if ( !$validdomains )
			{
				return 'WRONG_DATA_TWITTER';
			}
		}
		return false;
    }

Re: UCP Profile valid URL & specific domain

Posted: Thu May 09, 2013 10:29 pm
by potiron
Hi,

Just to let you know: I got inspired by your code to also implement a validation on social networking.

The problem I found with your code is that the door was still open to a lot of possibilities and I wanted to restrain as much as possible to avoid spammers :twisted: .

Everything is explained in my post, but basically I change the validate function to:

Code: Select all

function validate_facebook($url)
{
	global $config, $db, $user;

	if ( $url )
	{
		$host = parse_url($url, PHP_URL_HOST);
		$validdomain = preg_match('/(?:[^\.]+\.)?(facebook)+\.[a-z]{2,3}$/i', $host);
		if ( !$validdomain )
		{
			return 'WRONG_DATA_FACEBOOK';
		}
	}
	return false;
}