<?php
/**
* @copyright (C) 2013 iJoomla, Inc. - All rights reserved.
* @license GNU General Public License, version 2 (http://www.gnu.org/licenses/gpl-2.0.html)
* @author iJoomla.com <webmaster@ijoomla.com>
* @url https://www.jomsocial.com/license-agreement
* The PHP code portions are distributed under the GPL license. If not otherwise stated, all images, manuals, cascading style sheets, and included JavaScript *are NOT GPL, and are released under the IJOOMLA Proprietary Use License v1.0
* More info at https://www.jomsocial.com/license-agreement
*/

use Joomla\Filesystem\Folder;

defined('_JEXEC') or die('Restricted access');

class CValidateHelper
{
	static public function email($data, $strict = false)
	{
		$regex = $strict ? '/^([.0-9a-z_-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i' : '/^([*+!.&#$¦\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,8})$/i';

		if(preg_match($regex, CStringHelper::trim($data), $matches))
		{
			return array($matches[1], $matches[2]);
		}
		else
		{
			return false;
		}
	}

	static public function domain($address, $domain)
	{
		$regex = '/^([.0-9a-z_-]+)@'.$domain.'$/i';

		if(preg_match($regex, CStringHelper::trim($address), $matches))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	static public function alias( $username )
	{
		jimport( 'joomla.filesystem.folder' );

		$views		= Folder::folders( JPATH_ROOT .'/components/com_community/views' );

		return !in_array( $username , $views );
	}

	static public function username( $username )
	{
		// Make sure the username is at least 1 char and contain no funny char
		$filterInput = \JFilterInput::getInstance();

		if (preg_match('#[<>"\'%;()&\\\\]|\\.\\./#', $username) || strlen(mb_convert_encoding($username, "UTF-8", mb_detect_encoding($username))) < 2
			|| $filterInput->clean($username, 'TRIM') !== $username) {

			return false;
		}
		
		return true;
	}

	static public function url( $url )
	{
		//$regex = '/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i';
		//$regex = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,6}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&amp;?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/';
		$regex = '/^[a-zA-Z]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[A-Za-z0-9+\.\/%&=\?\-_#!]+$/i';

		if (preg_match($regex, CStringHelper::trim($url), $matches))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/*
	 * Check whether the string is a phone number or not.
	 * Supported US phone number format :
	 * 1-234-567-8901
	 * 1-234-567-8901 x1234
	 * 1-234-567-8901 ext1234
	 * 1 (234) 567-8901
	 * 1.234.567.8901
	 * 1/234/567/8901
	 * 12345678901
	 *
	 */
	static public function phone($phone)
	{
		$regex = '/^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$/i';

		if (preg_match($regex, CStringHelper::trim($phone), $matches))
		{
			return array($matches[1], $matches[2]);
		}
		else
		{
			return false;
		}
	}

	/*
	 * Check if the length of the string is between the given range
	 * @param : min (minimum length of string)
	 * @param : max (maximum length of string)
	 * @param : string
	 * @return : boolean
	 */
	static public function characterLength( $min, $max, $string ){
		$str_length = strlen($string);

		if($min > $max){
			return false;
		}

		if($str_length >= $min && $str_length <= $max){
			return true;
		}else{
			return false;
		}
	}
}