Skip to content

Instantly share code, notes, and snippets.

@capir
Last active December 17, 2015 16:58
Show Gist options
  • Save capir/5642231 to your computer and use it in GitHub Desktop.
Save capir/5642231 to your computer and use it in GitHub Desktop.
PHP: CI Helpers
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function valid_cui($cui)
{
preg_match('/^([\D]{2})?\s*(\d{2,10})$/', $cui, $m);
if(empty($m)) return false;
if(!empty($m[1]) && strtolower($m[1]) != 'ro') return false;
$key = str_split(strrev('753217532'));k
$cui = str_split(strrev($m[2]));
$chk = array_shift($cui);
$h = 0;
foreach($cui as $k => $e){
$h += $e * $key[$k];
}
return ($h*10)%11 == $chk;
}
function valid_cnp($cnp)
{
preg_match('/^\d{13}$/', $cnp, $m);
if(empty($m)) return false;
$key = str_split('279146358279');
$cnp = str_split($m[0]);
$chk = array_pop($cnp);
$h = 0;
foreach($cnp as $k => $e){
$h += $e * $key[$k];
}
$mod = $h%11;
if($mod == 10) return $chk == 1;
return $mod == $chk;
}
function getCurs($unit = 'eur', $date = false)
{
$unit = strtoupper($unit);
$allowed = 'CAD,DKK,INR,TRY,XDR,AED,CHF,JPY,BGN,GBP,UAH,SEK,AUD,EGP,XAU,ZAR,BRL,RSD,EUR,HUF,MXN,USD,NOK,CZK,MDL,KRW,RUB,NZD,CNY,PLN';
if(!in_array($unit, explode(',', $allowed))) return false;
$url = "http://openapi.ro/api/exchange/$unit.json";
if($date){
$date = date("Y-m-j", strtotime($date));
$url .= "?date=$date";
}
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
curl_close( $ch );
$resp = json_decode($content);
return $resp->rate;
}
class telefonie{
/**
*Clasa pentru validarea si aflarea diversilor
*operatori de telefonie mobile si fixa aflati
*pe piata romaneasca in acest moment .
*Clasa a fost scrisa la data de 19-Apr-2009 .
*Autor : Stresant
*Contact : admin [at] knd [dot] ro
**/
function __construct(){
/**
*Prefixurile Romtelecom Pentru Toate Judetele
**/
$this->romtelecom_fix = array(
'Alba' => '0258' ,
'Arad' => '0257' ,
'Arges' => '0248' ,
'Bacau' => '0234' ,
'Bihor' => '0259' ,
'Bistrita-Nasaud'=> '0263' ,
'Botosani' => '0231' ,
'Brasov' => '0268' ,
'Braila' => '0239' ,
'Bucuresti' => '021' ,
'Buzau' => '0238' ,
'Caras-Severin' => '0255' ,
'Calarasi' => '0242' ,
'Cluj' => '0264' ,
'Constanta' => '0241' ,
'Covasna' => '0267' ,
'Dambovita' => '0245' ,
'Dolj' => '0251' ,
'Galati' => '0236' ,
'Giurgiu' => '0246' ,
'Gorj' => '0253' ,
'Harghita' => '0266' ,
'Hunedoara' => '0254' ,
'Ialomita' => '0243' ,
'Iasi' => '0232' ,
'Ilfov' => '021' ,
'Maramures' => '0262' ,
'Mehedinti' => '0252' ,
'Mures' => '0265' ,
'Neamt' => '0233' ,
'Olt' => '0249' ,
'Prahova' => '0244' ,
'Satu Mare' => '0261' ,
'Salaj' => '0260' ,
'Sibiu' => '0269' ,
'Suceava' => '0230' ,
'Teleorman' => '0247' ,
'Timis' => '0256' ,
'Vaslui' => '0235' ,
'Valcea' => '0250' ,
'Vrancea' => '0237' ,
);
/**
*Prefixurile pentru restul retelelor de telefonie fix
*din tara . Deoarece RDS detine majoritatea , vom presupune
*ca RDS va fii operatorul de telefonie FIXA caruia ii
*corespund aceste prefixuri .
**/
$this->rds_fix = array(
'Alba' => '0358' ,
'Arad' => '0357' ,
'Arges' => '0348' ,
'Bacau' => '0334' ,
'Bihor' => '0359' ,
'Bistrita-Nasaud'=> '0363' ,
'Botosani' => '0331' ,
'Brasov' => '0368' ,
'Braila' => '0339' ,
'Bucuresti' => '031' ,
'Buzau' => '0338' ,
'Caras-Severin' => '0355' ,
'Calarasi' => '0342' ,
'Cluj' => '0364' ,
'Constanta' => '0341' ,
'Covasna' => '0367' ,
'Dambovita' => '0345' ,
'Dolj' => '0351' ,
'Galati' => '0336' ,
'Giurgiu' => '0346' ,
'Gorj' => '0353' ,
'Harghita' => '0366' ,
'Hunedoara' => '0354' ,
'Ialomita' => '0343' ,
'Iasi' => '0332' ,
'Ilfov' => '031' ,
'Maramures' => '0362' ,
'Mehedinti' => '0352' ,
'Mures' => '0365' ,
'Neamt' => '0333' ,
'Olt' => '0349' ,
'Prahova' => '0344' ,
'Satu Mare' => '0361' ,
'Salaj' => '0360' ,
'Sibiu' => '0369' ,
'Suceava' => '0330' ,
'Teleorman' => '0347' ,
'Timis' => '0356' ,
'Vaslui' => '0335' ,
'Valcea' => '0350' ,
'Vrancea' => '0337' ,
);
/**
*Prefix Romtelecom pentru telefonia Mobila .
**/
$this->romtelecom_mobil = array('0711');
/**
*Prefixuri Vodafone.
**/
$this->vodafone = array( '0720' , '0721' , '0722' , '0723' , '0724' ,
'0725' , '0726' , '0727' , '0728' , '0729' ,
'0730' , '0731' , '0732' , '0733' , '0734'
);
/**
*Prefixuri Orange.
**/
$this->orange = array( '0740' , '0741' , '0742' , '0743' , '0744' ,
'0745' , '0746' , '0747' , '0748' , '0749' ,
'0750' , '0751' , '0752' , '0753' , '0754' ,
'0755' , '0756'
);
/**
*Prefixuri Cosmote.
**/
$this->cosmote = array( '0760' , '0761' , '0762' , '0763' , '0764' ,
'0765' , '0766' , '0767'
);
/**
*Prefixuri RDS Mobil.
**/
$this->rds_mobil= array( '0770' , '0771' );
/**
*Prefixuri Zapp.
**/
$this->zapp = array( '0788' );
}
function validare_operator($numar_telefon,$judet=FALSE)
{
if( !is_numeric($numar_telefon) )
return false; //return 'Formatul numarului este invalid' ;
elseif( strlen($numar_telefon) != 10 AND strlen($numar_telefon) != 9 )
return false; //return 'Lungimea numarului este invalida' ;
else
{
$numar = substr($numar_telefon,strlen($numar_telefon) - 6 , 6 );
$prefix= str_replace($numar,'',$numar_telefon);
if( in_array($prefix , $this->romtelecom_fix) )
{
if( $judet == TRUE )
{
foreach( $this->romtelecom_fix AS $judet=>$jud_prefix )
{
if( $prefix == $jud_prefix )
return true; //return 'Romtelecom - ' . $judet ;
}
}
else
return true; //return 'Romtelecom';
}
elseif( in_array( $prefix , $this->romtelecom_mobil ) )
return true; //return 'Romtelecom Mobil';
elseif( in_array( $prefix , $this->rds_fix ) )
{
if( $judet == TRUE )
{
foreach( $this->rds_fix AS $judet=>$jud_prefix )
{
if( $prefix == $jud_prefix )
return true; //return 'RDS Fix - ' . $judet ;
}
}
else
return true; //return 'Romtelecom';
}
elseif( in_array( $prefix , $this->rds_mobil) )
return true; //return 'RDS Mobil';
elseif( in_array( $prefix , $this->vodafone ) )
return true; //return 'Vodafone';
elseif( in_array( $prefix , $this->orange ) )
return treu; //return 'Orange';
elseif( in_array( $prefix , $this->cosmote ) )
return true; //return 'Cosmote';
elseif( in_array( $prefix , $this->zapp ) )
return true; //return 'Zapp';
else
return false; //return 'Operator Necunoscut...';
}
}
/**
*Class Ends Here
**/
}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* On the fly resizer
*
* @author capir <catalin@ipmedia.ro>
* @version 2.0
*/
function img_resize($conf){
if(!is_array($conf))
return;
// it exists?
if(!isset($conf['file']) || !is_file($conf['file']))
return;
// conf setup
$path = $conf['file'];
$width = isset($conf['width']) ? $conf['width'] : null;
$height = isset($conf['height']) ? $conf['height'] : null;
$percent = isset($conf['percent']) ? $conf['percent'] : null;
$crop = isset($conf['crop']) ? (bool)$conf['crop'] : false;
$color = isset($conf['color']) ? $conf['color'] : '#ffffff';
$fresh = isset($conf['fresh']) ? (bool)$conf['fresh'] : false;
$sufixIt = isset($conf['sufixIt']) ? (bool)$conf['sufixIt'] : true;
$dumpRow = isset($conf['dumpRow']) ? (bool)$conf['dumpRow'] : false;
// existing file info
$finfo = pathinfo($path);
$file = $finfo['basename'];
if(is_null($width) && is_null($height) && is_null($percent))
return $file; // no job to be done
// new_path
if($sufixIt){
// append _wxn.ext
$suffix = '_';
if(!is_null($width)) {
$width = (int) $width;
$suffix .= $width;
}
$suffix .= 'x';
if(!is_null($height)) {
$height = (int) $height;
$suffix .= $height;
}
if(!is_null($percent)){
$suffix .= $percent.'p';
}
// does this file has this suffix?
if(!$fresh && strpos($finfo['filename'], $suffix) !== false)
return $finfo['basename'];
}
// defining new path
$new_path = $finfo['dirname'] . '/' . $finfo['filename'] . @$suffix . ".jpg";
// new file info
$new_finfo = pathinfo($new_path);
// does the file allready exist
if(!$fresh && is_file($new_path)){
return $new_finfo['basename'];
}
// source values
$sinfo = getimagesize($path);
$swidth = $sinfo[0];
$sheight= $sinfo[1];
$sratio = $swidth / $sheight;
// only one job can be done so:
// resize based on percent w/h mustn't be defined
if(is_null($width) && is_null($height) && !is_null($percent))
{
$p = $percent/100;
$width = ceil($swidth * $p);
$height = ceil($width / $sratio);
$dst_x = 0;
$dst_y = 0;
$src_x = 0;
$src_y = 0;
}
// resize by one side
elseif(is_null($width) || is_null($height))
{
if(is_null($width)) $width = (int)ceil($sratio * $height);
elseif(is_null($height)) $height = (int)ceil($width / $sratio);
$dst_x = 0;
$dst_y = 0;
$src_x = 0;
$src_y = 0;
}
// resize both sides crop (or not)
else
{
$ratio = $width / $height;
// original is wider
if($sratio > $ratio){
// w/ crop
if($crop){
$dst_y = 0;
$src_x = 0;
$src_y = 0;
$dst_w = ceil($height*$sratio);
$dst_h = $height;
$src_w = $swidth;
$src_h = $sheight;
$dst_x = ($dst_w-$width)/-2;
}
// no crop
else{
$src_x = 0;
$dst_w = $width;
$dst_h = ceil($width / $sratio);
$dst_x = 0;
$dst_y = ($height - $dst_h)/2;
$src_y = 0;
$src_w = $swidth;
$src_h = $sheight;
}
}
// original is taller
elseif($sratio < $ratio){
// w/ crop
if($crop){
$dst_x = 0;
$src_x = 0;
$src_y = 0;
$dst_w = $width;
$dst_h = ceil($width / $sratio);
$dst_y = ($dst_h-$height)/-2;
$src_w = $swidth;
$src_h = $sheight;
}
// no crop
else{
$src_x = 0;
$src_y = 0;
$dst_w = ceil($height * $sratio);
$dst_h = $height;
$dst_x = abs($width - $dst_w)/2;
$dst_y = 0;
$src_w = $swidth;
$src_h = $sheight;
}
}
// same ratio
else{
$dst_x = 0;
$dst_y = 0;
$src_x = 0;
$src_y = 0;
}
}
// create new canvas
$image = imagecreatetruecolor($width, $height);
preg_match_all('/\w{2}/i', $color, $m);
$r = hexdec($m[0][0]);
$g = hexdec($m[0][1]);
$b = hexdec($m[0][2]);
$background = imagecolorallocate($image, $r, $g, $b);
imagefill($image, 0, 0, $background);
// load original canvas
switch($sinfo['mime']){
case 'image/png':
$simage = imagecreatefrompng($path);
break;
case 'image/jpeg':
$simage = imagecreatefromjpeg($path);
break;
case 'image/gif':
$simage = imagecreatefromgif($path);
break;
default:
return $file;
}
// make sure all params are set
if(!isset($src_x)) $src_x = 0;
if(!isset($src_y)) $src_y = 0;
if(!isset($dst_x)) $dst_x = 0;
if(!isset($dst_y)) $dst_y = 0;
if(!isset($src_w)) $src_w = $swidth;
if(!isset($src_h)) $src_h = $sheight;
if(!isset($dst_w)) $dst_w = $width;
if(!isset($dst_h)) $dst_h = $height;
// homework done, magic must happen
imagecopyresampled($image, $simage, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// output it as is?
if($dumpRow){
header('Content-Type: image/jpeg');
imagejpeg($image);
}
// store it
imagejpeg($image, $new_path, 100);
imagedestroy($image);
imagedestroy($simage);
// should be on disk serve it
if(is_file($new_path))
return $new_finfo['basename'];
var_dump($new_path);
exit(__FILE__);
// still here? somethig wrong (fs permissions?)
return $file ;
}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function login($user, $pass){
$sv_user = isset($_SERVER['PHP_AUTH_USER'])?$_SERVER['PHP_AUTH_USER']:'';
$sv_pass = isset($_SERVER['PHP_AUTH_PW'])?$_SERVER['PHP_AUTH_PW']:'';
if((empty($sv_user) || empty($sv_pass) || $sv_user != $user || $sv_pass != $pass)) {
header('WWW-Authenticate: Basic realm="'. utf8_decode('Autentificare') .'"');
header('HTTP/1.0 401 Unauthorized');
exit;
}
}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Sets global front-end messages
* @author catalin@ipmedia.ro
* @version 1.1
*
* @param string $noty
* @param string $type
*/
function set_noty($noty, $type = 'information'){
if(!in_array($type, array('alert', 'success', 'error', 'warning', 'information'))) return;
$CI =& get_instance();
$noties = $CI->session->userdata('noties');
if(!isset($noties[$type]) || array_search($noty, $noties[$type]) === FALSE){
$noties[$type][] = $noty;
$CI->session->set_userdata('noties', $noties);
}
}
/**
* Generates front-end noties
*
* @author catalin@ipmedia.ro
* @version 1.0.1
*/
function get_noties($return = false){
$CI =& get_instance();
$noties = (array)$CI->session->userdata('noties');
$r = $CI->load->view('messages', array('noties' => $noties), $return);
$CI->session->unset_userdata('noties');
return $r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment