Skip to content

Instantly share code, notes, and snippets.

@JonMadVal
Forked from cristianmeza/helpers.php
Created January 12, 2018 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonMadVal/228b3ec45fc41a11eef5638c9ce8e186 to your computer and use it in GitHub Desktop.
Save JonMadVal/228b3ec45fc41a11eef5638c9ce8e186 to your computer and use it in GitHub Desktop.
<?php
use JWTAuth as Auth;
if (! function_exists('qString')) {
/**
* Obtener query string con el mismo nombre.
*
* @param string $key
*/
function qString($key)
{
$query = explode('&', request()->server->get('QUERY_STRING'));
$params = array();
foreach( $query as $param )
{
list($name, $value) = explode('=', $param, 2);
$params[urldecode($name)][] = urldecode($value);
}
return $params[$key];
}
}
if (! function_exists('ip')) {
/**
* Enviar IP a http://ip-api.com/php
*
* @param string $key
*/
function ip($ip)
{
return @unserialize(file_get_contents("http://ip-api.com/php/{$ip}"));
}
}
if (! function_exists('countries')) {
/**
* Retornar paises de Súdamerica
*/
function countries()
{
return collect([
['id' => 1, 'name' => "Chile"], ['id' => 2, 'name' => "Argentina"],
['id' => 3, 'name' => "Ecuador"], ['id' => 4, 'name' => "Colombia"],
['id' => 5, 'name' => "Perú"], ['id' => 6, 'name' => "Bolivia"],
['id' => 7, 'name' => "Venezuela"], ['id' => 8, 'name' => "Uruguay"],
['id' => 9, 'name' => "Paraguay"], ['id' => 10, 'name' => "Brasil"],
['id' => 11, 'name' => "México"], ['id' => 12, 'name' => "Panamá"],
['id' => 13, 'name' => "Guatemala"], ['id' => 14, 'name' => "El Salvador"],
['id' => 15, 'name' => "Nicaragua"], ['id' => 16, 'name' => "Estados Unidos"]
]);
}
}
if (! function_exists('links')) {
/**
* Construir links a traves del string
*/
function links($comment) {
$comment = strip_tags($comment, '<a>');
return preg_replace_callback(
'@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.-]*(\?\S+)?)?)?)@',
function($matches){
return
"<a href=\"{$matches[1]}\" rel=\"nofollow\" target=\"_blank\">".parse_url($matches[1], PHP_URL_HOST)."</a>";
}, $comment);
}
}
if (! function_exists('jwtCheck')) {
/**
* Verifica si el usuario está logueado a través de JWT
*/
function jwtCheck() {
try {
$user = Auth::parseToken()->authenticate();
return $user ? true : false;
} catch(\Tymon\JWTAuth\Exceptions\JWTException $e){
}
}
}
if (! function_exists('jwtId')) {
/**
* Retorna ID de usuario logueado a traves de JWT
*/
function jwtId()
{
return Auth::parseToken()->authenticate()->id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment