Skip to content

Instantly share code, notes, and snippets.

@maxcelos
Last active October 4, 2018 12:55
Show Gist options
  • Save maxcelos/a8e6a7c4442e913734573821afc125f4 to your computer and use it in GitHub Desktop.
Save maxcelos/a8e6a7c4442e913734573821afc125f4 to your computer and use it in GitHub Desktop.
Gera CPF, CNPJ e IMEI via linha de comando
#!/usr/bin/php
<?php
$argv[1] = $argv[1] ?? 'default';
switch ($argv[1]) {
case 'imei':
echo 'IMEI gerado: ' . imeiRandom() . "\n\n";
break;
case 'cpf' :
echo 'CPF gerado: ' . cpf($argv[2] ?? 0) . "\n\n";
break;
case 'cnpj':
echo 'CNPJ gerado: ' . cnpj($argv[2] ?? 0) . "\n\n";
break;
case 'default':
echo "MANUAL\n";
echo "Informe o tipo de doc desejado, entre imei, cnpj e cpf\n\n";
echo "Para CPF e CNPJ, insira '1' ou '0' (opcionalmente) para indicar que quer com pontuação ou não, respectivamente\n\n";
echo "Ex.:\n";
echo "php docgen.php cpf 1\n\n";
echo "Saída:\n";
echo "654.678.656-45\n";
break;
}
/**
* Generates IMEI code valid and random
* Generates 14 aleatory digits. These 14, multiplies the multiples of 2 by 2 and sum the result
* The result Must be divisible by 10,
* Then get the diference and genaretes the last digit
*
* @return int $imei
*/
function imeiRandom() {
$code = intRandom(14);
$position = 0;
$total = 0;
while ($position < 14) {
if ($position % 2 == 0) {
$prod = 1;
} else {
$prod = 2;
}
$actualNum = $prod * $code[$position];
if ($actualNum > 9) {
$strNum = strval($actualNum);
$total += $strNum[0] + $strNum[1];
} else {
$total += $actualNum;
}
$position++;
}
$last = 10 - ($total % 10);
if ($last == 10) {
$imei = $code . 0;
} else {
$imei = $code . $last;
}
return $imei;
}
/**
* @param int $size
* @return $int
*/
function intRandom($size) {
$validCharacters = utf8_decode("0123456789");
$validCharNumber = strlen($validCharacters);
$int = '';
while (strlen($int) < $size) {
$index = mt_rand(0, $validCharNumber - 1);
$int .= $validCharacters[$index];
}
return $int;
}
function mod( $dividendo, $divisor ) {
return round( $dividendo - ( floor( $dividendo / $divisor ) * $divisor ) );
}
function cpf( $compontos = 0) {
$n1 = rand( 0, 9 );
$n2 = rand( 0, 9 );
$n3 = rand( 0, 9 );
$n4 = rand( 0, 9 );
$n5 = rand( 0, 9 );
$n6 = rand( 0, 9 );
$n7 = rand( 0, 9 );
$n8 = rand( 0, 9 );
$n9 = rand( 0, 9 );
$d1 = $n9 * 2 + $n8 * 3 + $n7 * 4 + $n6 * 5 + $n5 * 6 + $n4 * 7 + $n3 * 8 + $n2 * 9 + $n1 * 10;
$d1 = 11 - ( mod( $d1, 11 ) );
if ( $d1 >= 10 ) {
$d1 = 0;
}
$d2 = $d1 * 2 + $n9 * 3 + $n8 * 4 + $n7 * 5 + $n6 * 6 + $n5 * 7 + $n4 * 8 + $n3 * 9 + $n2 * 10 + $n1 * 11;
$d2 = 11 - ( mod( $d2, 11 ) );
if ( $d2 >= 10 ) {
$d2 = 0;
}
$retorno = '';
if ( $compontos == 1 ) {
$retorno = '' . $n1 . $n2 . $n3 . "." . $n4 . $n5 . $n6 . "." . $n7 . $n8 . $n9 . "-" . $d1 . $d2;
} else {
$retorno = '' . $n1 . $n2 . $n3 . $n4 . $n5 . $n6 . $n7 . $n8 . $n9 . $d1 . $d2;
}
return $retorno;
}
function cnpj( $compontos = 0 ) {
$n1 = rand( 0, 9 );
$n2 = rand( 0, 9 );
$n3 = rand( 0, 9 );
$n4 = rand( 0, 9 );
$n5 = rand( 0, 9 );
$n6 = rand( 0, 9 );
$n7 = rand( 0, 9 );
$n8 = rand( 0, 9 );
$n9 = 0;
$n10 = 0;
$n11 = 0;
$n12 = 1;
$d1 = $n12 * 2 + $n11 * 3 + $n10 * 4 + $n9 * 5 + $n8 * 6 + $n7 * 7 + $n6 * 8 + $n5 * 9 + $n4 * 2 + $n3 * 3 + $n2 * 4 + $n1 * 5;
$d1 = 11 - ( mod( $d1, 11 ) );
if ( $d1 >= 10 ) {
$d1 = 0;
}
$d2 = $d1 * 2 + $n12 * 3 + $n11 * 4 + $n10 * 5 + $n9 * 6 + $n8 * 7 + $n7 * 8 + $n6 * 9 + $n5 * 2 + $n4 * 3 + $n3 * 4 + $n2 * 5 + $n1 * 6;
$d2 = 11 - ( mod( $d2, 11 ) );
if ( $d2 >= 10 ) {
$d2 = 0;
}
$retorno = '';
if ( $compontos == 1 ) {
$retorno = '' . $n1 . $n2 . "." . $n3 . $n4 . $n5 . "." . $n6 . $n7 . $n8 . "/" . $n9 . $n10 . $n11 . $n12 . "-" . $d1 . $d2;
} else {
$retorno = '' . $n1 . $n2 . $n3 . $n4 . $n5 . $n6 . $n7 . $n8 . $n9 . $n10 . $n11 . $n12 . $d1 . $d2;
}
return $retorno;
}
?>
@maxcelos
Copy link
Author

maxcelos commented Oct 4, 2018

Instalação rápida

wget https://gist.github.com/maxcelos/a8e6a7c4442e913734573821afc125f4/raw/2d470ecd1f2a2f64ebfa1c734f585e4a999689cc/docgen.php

chmod +x docgen.php

sudo mv docgen.php /usr/local/bin/docgen

Como usar

docgen [imei|cnpj|cpf] [1|0]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment