Skip to content

Instantly share code, notes, and snippets.

@apertaoxis
Created January 4, 2018 17:47
Show Gist options
  • Select an option

  • Save apertaoxis/e7e93cbf1d782f76561ce628d9a50596 to your computer and use it in GitHub Desktop.

Select an option

Save apertaoxis/e7e93cbf1d782f76561ce628d9a50596 to your computer and use it in GitHub Desktop.
<?php
if (!function_exists('idade')) {
/**
* @param string $date (null)
* @param string $formato (null)
* @return int
*/
function idade($date = null, $formato = null)
{
// Se o formato for diferente de 'Y-m-d', use o segundo parametro
if($formato){
$date = DateTime::createFromFormat($formato, $date)->format('Y-m-d');
}
$date = new DateTime($date);
$interval = $date->diff( new DateTime() );
return $interval->format( '%Y' );
}
}
if (!function_exists('validaCPF')) {
/**
* @param string|int $cpf
* @return boolean
*/
function validaCPF($cpf = null)
{
if(empty($cpf)) {
return false;
}
/* Remove todo conteúdo não númerico */
$cpf = preg_replace('/[^0-9]/', '', $cpf);
$cpf = str_pad($cpf, 11, '0', STR_PAD_LEFT);
$CPFNulo = ['00000000000', '11111111111', '22222222222', '33333333333', '44444444444', '55555555555', '66666666666', '77777777777', '88888888888', '99999999999'];
if (isset($cpf[10]) == false) {
return false;
}
else if (in_array($cpf, $CPFNulo)) {
return false;
}
else {
for ($t = 9; $t < 11; $t++) {
for ($d = 0, $c = 0; $c < $t; $c++) {
$d += $cpf{$c} * (($t + 1) - $c);
}
$d = ((10 * $d) % 11) % 10;
if ($cpf{$c} != $d) {
return false;
}
}
return true;
}
}
}
if (!function_exists('isSecure')) {
/**
* @return boolean
*/
function isSecure()
{
$https = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443;
return $https;
}
}
if (!function_exists('detect_bot')) {
/**
* @param string $userAgent (null)
* @param bool $bot (null)
* @return bool|string
*/
function detect_bot($userAgent = null, $bot = null)
{
if (!$userAgent) {
$userAgent = $_SERVER['USER_AGENT'];
}
$crawlers_agents = strtolower('LinkWalker|msnbot|Twitterbot|Telegrambot|ASPSeek|FeedFetcher-Google|Yahoo|YoudaoBot|AdsBot-Google|Googlebot|Scooter|Gigabot|Charlotte|eStyle|AcioRobot|GeonaBot|msnbot-media|CocoCrawler|Google|Charlotte t|MSRBOT|Yahoo! Slurp|Java VM|accoona|Yahoo|facebookexternalhit|msnbot|Bingbot|AdIdxBot|BingPreview');
$crawlers = explode("|", $crawlers_agents);
if(is_array($crawlers)) {
foreach($crawlers as $crawler) {
if (strpos(strtolower($userAgent), strtolower(trim($crawler))) !== false) {
if($bot){
return $bot;
}
return true;
}
}
}
return false;
}
}
if (!function_exists('buscaArquivo')) {
/**
* @param string $dir - Path name
* @param array|string $type - Array or string contains filetypes: 'jpg' OR ['jpg', 'png']
* @param int $qnt - Amount of values ​​returned
* @param string $prefix - Find the file that begins with the prefix
* @param string $contains - Find the file that contains the word
* @param bool $rand - mixes the returned array
* @param bool $fulllPath - Return path and file name or return file name if set to false/null
* @param bool $folders - Includes folders in search result
* @author apertaoxis (Rafael Menezes) <contato@rafaemenezes.net.br>
* @return array
*
* Usage: buscaArquivo('ṕath', ['png', 'jpg'], 3); buscaArquivo('path'); buscaArquivo('path', null, null, 'name')
*/
function buscaArquivo($dir = null, $type = null, $qnt = null, $prefix = null, $contains=null, $rand=null, $fullPath = true, $folders=null)
{
if (!is_dir($dir)) {
return false;
}
$dir = new DirectoryIterator($dir);
foreach ($dir as $file) {
if ($file->isFile()) {
if ( ($type) && ( ($file->getExtension() == $type) || (in_array($file->getExtension(), $type)) ) ) {
if ($prefix) {
if (substr($file->getFilename(), 0, strlen($prefix)) == $prefix) {
$retorno[] = $fullPath ? $file->getPathname() : $file->getFilename();
}
}
else if ($contains) {
if (strpos($file->getFilename(), $contains) !== false) {
$retorno[] = $fullPath ? $file->getPathname() : $file->getFilename();
}
}
else {
$retorno[] = $fullPath ? $file->getPathname() : $file->getFilename();
}
}
else if(!$type){
if ($prefix) {
if (substr($file->getFilename(), 0, strlen($prefix)) == $prefix) {
$retorno[] = $fullPath ? $file->getPathname() : $file->getFilename();
}
}
else if ($contains) {
if (strpos($file->getFilename(), $contains) !== false) {
$retorno[] = $fullPath ? $file->getPathname() : $file->getFilename();
}
}
else {
$retorno[] = $fullPath ? $file->getPathname() : $file->getFilename();
}
}
}
else if ( ($file->isDir()) && ($folders) ) {
$retorno[] = $file->getPathname();
}
}
if ( (int) $qnt > 0) {
$valores = $retorno;
$retorno = [];
$qnt = (int) $qnt;
if ($qnt < count($valores)) {
for ($i = 0; $i < $qnt; $i++) {
if ( isset($valores[$i]) ) {
$retorno[] = $valores[$i];
}
}
}
}
if($rand) {
shuffle($retorno);
}
return $retorno;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment