Skip to content

Instantly share code, notes, and snippets.

@ozanmora
Last active January 26, 2022 07:38
Show Gist options
  • Save ozanmora/e64e2b63892c09fabba19f10a27ce4da to your computer and use it in GitHub Desktop.
Save ozanmora/e64e2b63892c09fabba19f10a27ce4da to your computer and use it in GitHub Desktop.
Some useful Codeigniter 3.x files.

Some useful Codeigniter 3.x files.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation
{
function __construct()
{
parent::__construct();
log_message('info', '*** Loaded::MY_Form_validation ***');
}
public function update_unique($str, $params)
{
if (substr_count($params, '.') > 2) {
sscanf($params, '%[^.].%[^.].%[^.].%[^.]', $table, $field, $where_value, $where_field);
} else {
sscanf($params, '%[^.].%[^.].%[^.]', $table, $field, $where_value);
$where_field = 'id';
}
return isset($this->CI->db) ? ($this->CI->db->get_where($table, [$field => $str, "{$where_field} !=" => $where_value], 1)->num_rows() === 0) : true;
}
public function recaptcha($str)
{
$google_url = "https://www.google.com/recaptcha/api/siteverify";
$secret = $this->CI->config->item('recaptcha_secret_key');
$ip = $this->CI->input->ip_address();
$url = $google_url . "?secret=" . $secret . "&response=" . $str . "&remoteip=" . $ip;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16");
$res = curl_exec($curl);
curl_close($curl);
$res = json_decode($res, true);
//reCaptcha success check
return ($res['success']) ? true : false;
}
public function valid_phone($str)
{
$result = true;
$regex = '/(([\+]90?)|([0]?))(\s*[\-]?)((\([0-9]{3}\))|([0-9]{3}))(\s*[\-]?)([0-9]{3})(\s*[\-]?)([0-9]{2})(\s*[\-]?)([0-9]{2})/'; // OLD
// $regex = '/^(1[ \-\+]{0,3}|\+1[ -\+]{0,3}|\+1|\+)?((\(\+?1-[2-9][0-9]{1,2}\))|(\(\+?[2-8][0-9][0-9]\))|(\(\+?[1-9][0-9]\))|(\(\+?[17]\))|(\([2-9][2-9]\))|([ \-\.]{0,3}[0-9]{2,4}))?([ \-\.][0-9])?([ \-\.]{0,3}[0-9]{2,4}){2,3}$/mi'; // NEW
if(strlen($str) < 8 || strlen($str) > 18 || !preg_match($regex, $str)) {
$result = false;
}
if ($result) {
$valid_phone_chars = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ' ', '+', '-', '(', ')');
foreach ($valid_phone_chars as $valid_phone_char) {
if (substr_count($str, $valid_phone_char) > 6) {
$result = false;
break;
}
}
}
return $result;
}
public function valid_domain($domain)
{
if (substr($domain, 1) === '-' || substr($domain, -1) === '-')
return false;
if (substr_count($domain, '.') > 2 || substr_count($domain, '.') < 1)
return false;
if (strlen($domain) <3 || strlen($domain) > 253)
return false;
$idn_dom = (mb_detect_encoding($domain) != "ASCII") ? idn_to_ascii($domain) : $domain;
return (!filter_var($idn_dom, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) ? false : true;
}
public function valid_date($date, $params = 'Y-m-d')
{
$d = DateTime::createFromFormat($params, $date);
return $d && $d->format($params) === $date;
}
public function valid_datetime($datetime, $params = 'Y-m-d H:i:s')
{
$d = DateTime::createFromFormat($params, $datetime);
return $d && $d->format($params) === $datetime;
}
}
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* MY_Log
*
* override the $_levels array to allow custom levels
*
*/
class MY_Log extends CI_Log
{
protected $_levels = array('ERROR' => 1, 'DEBUG' => 2, 'INFO' => 3, 'CUSTOM' => 4, 'ALL' => 5);
public function __construct()
{
parent::__construct();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment