/* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2006 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: | +----------------------------------------------------------------------+ */ /* $Id: header,v 1.16.2.1 2006/01/01 12:50:00 sniper Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "zend_compile.h" #include "zend_interfaces.h" #include "zend_exceptions.h" #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_validate_ptbr.h" static int le_validate_ptbr; zend_module_entry validate_ptbr_module_entry = { #if ZEND_MODULE_API_NO >= 20010901 STANDARD_MODULE_HEADER, #endif "validate_ptbr", validate_ptbr_functions, PHP_MINIT(validate_ptbr), PHP_MSHUTDOWN(validate_ptbr), PHP_RINIT(validate_ptbr), /* Replace with NULL if there's nothing to do at request start */ PHP_RSHUTDOWN(validate_ptbr), /* Replace with NULL if there's nothing to do at request end */ PHP_MINFO(validate_ptbr), #if ZEND_MODULE_API_NO >= 20010901 "0.1", /* Replace with version number for your extension */ #endif STANDARD_MODULE_PROPERTIES }; /* }}} */ #ifdef COMPILE_DL_VALIDATE_PTBR ZEND_GET_MODULE(validate_ptbr) #endif PHP_METHOD(Validate_ptBR,cnpj) { char *cnpj = NULL; int argc = ZEND_NUM_ARGS(), cnpj_len; int cnpjnumero[14]; int sum = 0, i = 0; int rrpd = 0, rrsd = 0, pdv = 0, sdv = 0; if (zend_parse_parameters(argc TSRMLS_CC, "s", &cnpj, &cnpj_len) == FAILURE) return; for(i = 0; i < 12; i++) { cnpjnumero[i] = (int) cnpj[i] - 48; } sum = 5*cnpjnumero[0] + 4*cnpjnumero[1] + 3*cnpjnumero[2] + 2*cnpjnumero[3] + 9*cnpjnumero[4] + 8*cnpjnumero[5] + 7*cnpjnumero[6] + 6*cnpjnumero[7] + 5*cnpjnumero[8] + 4*cnpjnumero[9] + 3*cnpjnumero[10] + 2*cnpjnumero[11]; rrpd = sum % 11; pdv = rrpd < 2 ? 0 : 11 - rrpd ; sum = 0; cnpjnumero[12] = pdv; sum = 6*cnpjnumero[0] + 5*cnpjnumero[1] + 4*cnpjnumero[2] + 3*cnpjnumero[3] + 2*cnpjnumero[4] + 9*cnpjnumero[5] + 8*cnpjnumero[6] + 7*cnpjnumero[7] +6*cnpjnumero[8] + 5*cnpjnumero[9] + 4*cnpjnumero[10] + 3*cnpjnumero[11] + 2*cnpjnumero[12]; rrsd = sum % 11; sdv = rrsd < 2 ? 0 : 11 - rrsd; if( (pdv == (int) cnpj[12] - 48) && (sdv = (int) cnpj[13] - 48) ) { RETURN_TRUE; } else RETURN_FALSE; } PHP_METHOD(Validate_ptBR,cpf) { char *cpf = NULL; int argc = ZEND_NUM_ARGS(), cpf_len, cpfnumero[12], i,j,sum = 0, resultado_primeiro_digito, resultado_resto_primeiro_digito, pdv, sdv, a, b, resultado_segundo_digito, resultado_resto_segundo_digito; if (zend_parse_parameters(argc TSRMLS_CC, "s", &cpf, &cpf_len) == FAILURE) return; else if ( (strlen(cpf) != 11) ) { RETURN_FALSE; } else if(strcmp(cpf,"11111111111") == 0 ) { RETURN_FALSE; } else if(strcmp(cpf,"22222222222") == 0 ) { RETURN_FALSE; } else if(strcmp(cpf,"33333333333") == 0 ) { RETURN_FALSE; } else if(strcmp(cpf,"44444444444") == 0 ) { RETURN_FALSE; } else if(strcmp(cpf,"55555555555") == 0 ) { RETURN_FALSE; } else if(strcmp(cpf,"66666666666") == 0 ) { RETURN_FALSE; } else if(strcmp(cpf,"77777777777") == 0 ) { RETURN_FALSE; } else if(strcmp(cpf,"88888888888") == 0 ) { RETURN_FALSE; } else if(strcmp(cpf,"99999999999") == 0 ) { RETURN_FALSE; } else if(strcmp(cpf,"00000000000") == 0 ) { RETURN_FALSE; } else { for(i=0,j=10; i<9; i++,j--) { cpfnumero[j] = (int) cpf[i] - 48; sum = sum + j*(cpfnumero[j]); } a = (int)cpf[9] - 48; b = (int)cpf[10] - 48; resultado_primeiro_digito = sum / 11; resultado_resto_primeiro_digito = sum % 11; pdv = (resultado_resto_primeiro_digito < 2) ? 0 : (11 - resultado_resto_primeiro_digito); sum = 0; for(i = 10, j=11; i > 2; i--, j--) cpfnumero[j] = cpfnumero[i]; cpfnumero[2] = pdv; for(j=11; j > 1; j--) sum = sum + j*(cpfnumero[j]); resultado_segundo_digito = sum / 11; resultado_resto_segundo_digito = sum % 11; sdv = (resultado_resto_segundo_digito < 2) ? 0 : (11 - resultado_resto_segundo_digito); cpfnumero[1] = sdv; if (a == pdv && b == sdv) { RETURN_TRUE; } else { RETURN_FALSE; } } } zend_function_entry validate_ptbr_class_functions[] = { PHP_ME(Validate_ptBR, cpf, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME(Validate_ptBR, cnpj, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) {NULL, NULL, NULL} }; PHP_MINIT_FUNCTION(validate_ptbr) { /* If you have INI entries, uncomment these lines REGISTER_INI_ENTRIES(); */ return SUCCESS; } PHP_MSHUTDOWN_FUNCTION(validate_ptbr) { /* uncomment this line if you have INI entries UNREGISTER_INI_ENTRIES(); */ return SUCCESS; } PHP_RINIT_FUNCTION(validate_ptbr) { return SUCCESS; } PHP_RSHUTDOWN_FUNCTION(validate_ptbr) { return SUCCESS; } PHP_MINFO_FUNCTION(validate_ptbr) { php_info_print_table_start(); php_info_print_table_header(2, "validate_ptbr support", "enabled"); php_info_print_table_end(); } /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */