![]() |
|
Snippets |
|
With this snippet we can implement the following functionalities from the global configuration file config/config.php:
Access control to the applications in certain environments. For example, disable access in the environment of development from production server.
IP access control to certain applications of the project. For example, to protect backend or administration zones of the project.
To have several global configuration files of config/config.php according to the environment in which we are. For example, to define different constants for each environment.
I developed a mini-library (config/config.lib.php) to include and use in the global config file config/config.php.
<?php /** * IP access control to an environment * * @param string $env environment name * @param array $ips IPs that has access * @param string $clientip client IP */ function envCorrecte ($env,$ips,$clientip) { if (SF_ENVIRONMENT==$env) { $acces=0; foreach($ips as $ip) { $ipclient=substr($clientip,0,strlen($ip)); if ($ipclient==$ip) { $acces=1; } } if ($acces==0) { echo "Keep away! this is not a public environment."; exit; } } } /** * IP access control to an application of the symfony project * * @param array $apps no public applications * @param array $ips IPs that has access * @param string $clientip client IP */ function appCorrecte ($apps,$ips,$clientip) { // solve problem with key '0' in array $apps_aux[0]=''; $apps = $apps_aux + $apps; // if (array_search(SF_APP,$apps)!=false) { $acces=0; foreach($ips as $ip) { $ADR=$HTTP_SERVER_VARS['REMOTE_ADDR']; $ipclient=substr($clientip,0,strlen($ip)); if ($ipclient==$ip) { $acces=1; } } if ($acces==0) { echo "Keep away! ".SF_APP." is not a public application."; exit; } } } ?>
Finally config/config.php file can be like this:
<?php include("config.lib.php"); /** * IP access control to 'env' and 'int' environments */ $ip_dev=array("10.138.0.","192.168."); $ip_int=array("10.138.0.","192.168."); envCorrecte('env',$ip_env,$HTTP_SERVER_VARS['REMOTE_ADDR']); envCorrecte('int',$ip_int,$HTTP_SERVER_VARS['REMOTE_ADDR']); /** * IP access control to backend applications */ $ip_apps=array("10.138.0.","192.168.","w.x.y.z"); $apps=array('myapp_1','myapp_2','myapp_n'); appCorrecte($apps,$ip_apps,$HTTP_SERVER_VARS['REMOTE_ADDR']); /** * Custom config.php file for each environment */ include("config.".SF_ENVIRONMENT.".php"); /** * Common config.php file for all environments */ define("example","content1"); ?>
sfValidator extension based on Credit Card Validator code by Harish Chauhan (from phpclasses.org). With this extension you can validate this type of credit cards: VISA, MASTERCARD, DISCOVER, AMEX, DINERS,JCB, Australian Bankcard, EnRoute And Switch Solo.
IMHO isn't a bad idea to include anything like this in standard sfValidator code. While here you have the source code of my adaptation of credit cards validator.
<?php /* Symfony integration as sfValidator of CCVAL Date - Jun 17, 2006 Author - Oriol Rius (oriol@joor.net) Credit CArd Validator Date - Jan 14, 2005 Author - Harish Chauhan ABOUT This PHP script will calidate credit cards by checking there length and pattern and checksum using mod 10. Supported credit cards are VISA, MASTERCARD, DISCOVER, AMEX, DINERS, JCB, Australian Bankcard, EnRoute And Switch Solo. */ class CCVAL extends sfValidator { public function execute (&$value, &$error) { // Recuperamos parámetros validar $num_param = $this->getParameterHolder()->get('num'); $num = $this->getContext()->getRequest()->getParameter($num_param); $tipo_param = $this->getParameterHolder()->get('tipo'); $tipo = $this->getContext()->getRequest()->getParameter($tipo_param); // Lanzamos la validación $validada=$this->_isVAlidCreditCard($num,$tipo,false); // Informamos de como ha ido la validación sfContext::getInstance()->getLogger()->info("CCVAL.class.php: Tipo: ".$tipo." Num: ".$num." Validada: ".$validada); if ($validada==false) { $error = $this->getParameterHolder()->get('error'); return false; } return true; } public function initialize ($context, $parameters = null) { // initialize parent parent::initialize($context); $this->getParameterHolder()->add($parameters); return true; } /** * Testing checksum * * @param integer $ccnum * @return boolean */ private function _checkSum($ccnum) { $checksum = 0; for ($i=(2-(strlen($ccnum) % 2)); $i<=strlen($ccnum); $i+=2) { $checksum += (int)($ccnum{$i-1}); } // Analyze odd digits in even length strings or even digits in odd length strings. for ($i=(strlen($ccnum)% 2) + 1; $i<strlen($ccnum); $i+=2) { $digit = (int)($ccnum{$i-1}) * 2; if ($digit < 10) { $checksum += $digit; } else { $checksum += ($digit-9); } } if (($checksum % 10) == 0) return true; else return false; } /** * Launch validation * * @param integer $ccnum * @param string $type * @param boolean $returnobj * @return boolean */ private function _isVAlidCreditCard($ccnum,$type="",$returnobj=false) { $creditcard=array( "visa"=>"/^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/", "mastercard"=>"/^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/", "discover"=>"/^6011-?\d{4}-?\d{4}-?\d{4}$/", "amex"=>"/^3[4,7]\d{13}$/", "diners"=>"/^3[0,6,8]\d{12}$/", "bankcard"=>"/^5610-?\d{4}-?\d{4}-?\d{4}$/", "jcb"=>"/^[3088|3096|3112|3158|3337|3528]\d{12}$/", "enroute"=>"/^[2014|2149]\d{11}$/", "switch"=>"/^[4903|4911|4936|5641|6333|6759|6334|6767]\d{12}$/"); if(empty($type)) { $match=false; foreach($creditcard as $type=>$pattern) if(preg_match($pattern,$ccnum)==1) { $match=true; break; } if(!$match) return false; else { if($returnobj) { $return=new stdclass; $return->valid=$this->_checkSum($ccnum); $return->ccnum=$ccnum; $return->type=$type; return $return; } else return $this->_checkSum($ccnum); } } else { if(@preg_match($creditcard[strtolower(trim($type))],$ccnum)==0) return false; else { if($returnobj) { $return=new stdclass; $return->valid=$this->_checkSum($ccnum); $return->ccnum=$ccnum; $return->type=$type; return $return; } else return $this->_checkSum($ccnum); } } } } ?>
An example of how you can call CCVAL validator from validate yml file:
methods: post: [ntarjeta] names: ntarjeta: required: Yes required_msg: Credit Card number is required validators: validarCC validarCC: class: CCVAL param: num: ntarjeta tipo: tipoCC error: Your credit card number is invalid
Sorry for my poor english.