Tuesday, January 22, 2019

ci pagination helper

function init_pagination2($uri, $total_rows, $per_page = 10, $segment = 4){
    $ci =& get_instance();
    $config['per_page'] = $per_page;
    $config['uri_segment'] = $segment;
    $config['base_url'] = base_url().$uri;
    $config['total_rows'] = $total_rows;
    $config['use_page_numbers'] = TRUE;
    $config['prev_link'] = '';
    $config['next_link'] = '';
    $config['last_link'] = '';
    $config['first_link'] = '';
    $config['full_tag_open'] = '<div class="btn-group" role="group" aria-label="First group">';
    $config['full_tag_close'] = '</div>';
    $config['first_tag_open'] = '<li class="btn btn-outline-secondary pagi_first">';
    $config['first_tag_close'] = '</li>';
    $config['last_tag_open'] = '<li class="btn btn-outline-secondary pagi_last">';
    $config['last_tag_close'] = '</span></li>';
    $config['prev_tag_open'] = '<li class="btn btn-outline-secondary pagi_prev">';
    $config['prev_tag_close'] = '</li>';
    $config['next_tag_open'] = '<li class="btn btn-outline-secondary pagi_next">';
    $config['next_tag_close'] = '</li>';
    $config['cur_tag_open'] = '<li class="btn btn-outline-secondary active">';
    $config['cur_tag_close'] = '</li>';
    $config['num_tag_open'] = '<li class="btn btn-outline-secondary">';
    $config['num_tag_close'] = '</li>';
    $ci->pagination->initialize($config);
   
    return $config;
}

Sunday, January 20, 2019

EDB

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 * database active records with AES Encryption
 *
 *
 */
class Edb {
/**
* Codeigniter instance
* @var codeigniter
*/
protected $_CI;
protected $_config;

// active records
var $arr_select   = array();
var $arr_distinct = array();
var $arr_where   = array();
var $arr_set   = array();
var $arr_join   = array();
var $arr_table   = array();

var $data_types   = array();
var $edata_types  = array();

var $encrypt   = array();
var $non_encrypt  = array();

public function __construct($config)
{
$this->_CI =& get_instance();
$this->_CI->load->database();

$this->_config = $config;

$this->data_types = $this->_config['non_encrypt'];
$this->edata_types = $this->_config['encrypt'];
}

public function reset()
{
$this->arr_select   = array();
$this->arr_distinct = array();
$this->arr_where    = array();
$this->arr_set     = array();
$this->arr_join     = array();
$this->arr_table    = array();
$this->encrypt     = array();
$this->non_encrypt  = array();
}

/**
* Select
*
* Generates the SELECT decrypted value of the query
*
* @param string
* @param boolean
* @return object
*/
public function select($select = '*')
{
if ($this->_config['enabled'] == TRUE) {
if (is_string($select)) {
$select = explode(',', $select);
}

foreach ($select as $val) {
$val = trim($val);

if ($val != '') {
$this->arr_select[] = trim($val);
}
}
} else {
$this->_CI->db->select($select);
}
}

/**
* Select Distinct
*
* Generates the SELECT DISTINCT decrypted value of the query
*
* @param string
* @param boolean
* @return object
*/
public function distinct($select = '*')
{
if ($this->_config['enabled'] == TRUE) {
if (is_string($select)) {
$select = explode(',', $select);
}

foreach ($select as $val) {
$val = trim($val);

if ($val != '') {
$this->arr_distinct[] = trim($val);
}
}
} else {
$this->_CI->db->distinct($select);
}
}

/**
* Where
*
* Generates the WHERE portion with encrypted value of the query. Separates
* multiple calls with AND
*
* @param mixed
* @param mixed
* @param boolean
* @return object
*/
public function where($key, $value = NULL)
{
if ($this->_config['enabled'] == TRUE) {
if (!is_array($key)) {
$key = array($key => $value);
}

foreach ($key as $k => $v) {
$this->arr_where[$k] = trim($v);
}
} else {
$this->_CI->db->where($key,$value);
}
}

public function or_where($key, $value = NULL)
{
$this->_CI->db->or_where($key,$value);
}


/**
* Where_not_in
*
* Generates a WHERE field NOT IN ('item', 'item') SQL query joined
* with AND if appropriate
*
* @param string The field to search
* @param array The values searched on
* @return object
*/
public function where_not_in($key = NULL, $values = NULL)
{
$this->_CI->db->where_not_in($key, $values);
}

/**
* The "set" function.  Allows key/value pairs to be set for inserting or updating with encrypted values
*
* @param mixed
* @param string
* @param boolean
* @return object
*/
public function set($key, $value = '')
{
if ($this->_config['enabled'] == TRUE) {
if ( ! is_array($key)) {
$key = array($key => $value);
}

foreach ($key as $k => $v) {
$this->arr_set[$k] = trim($v);
}

return $this;
} else {
$this->_CI->db->set($key,$value);
}
}


/**
* Join
*
* Generates the JOIN portion of the query.
* WARNING! Do not join field with encrypt values
*
* @param string
* @param string the join condition
* @param string the type of join
* @return object
*/
public function join($table, $cond, $type = '')
{
if ($this->_config['enabled'] == TRUE) {
// check alias define
if (preg_match('/\\sAS\\s/', $table)) {
$t = explode(' AS ', $table);
} elseif (preg_match('/\\s/', $table)) {
$t = explode(' ', $table);
} else {
$t = array($table,$table);
}
$this->arr_table[$t[0]] = $t[1];

$this->arr_join[] = $t[0];
}
$this->_CI->db->join($table,$cond,$type);
}

/**
* GROUP BY
*
* @param string
* @return object
*/
public function group_by($by)
{
if (is_string($by))
{
$by = explode(',', $by);
}

foreach ($by as $val)
{
$this->_CI->db->group_by($by);
}
}

/**
* Sets the ORDER BY value
*
* @param string
* @param string direction: asc or desc
* @return object
*/
public function order_by($orderby, $direction = '')
{
if ($this->_config['enabled'] == TRUE) {
$orderby = 'AES_DECRYPT('.$orderby.',"'.$this->_config['secret_key'].'")';
$this->_CI->db->order_by($orderby,$direction,FALSE);
} else {
$this->_CI->db->order_by($orderby,$direction);
}
}

/**
* Sets the ORDER BY value
*
* @param string
* @param string direction: asc or desc
* @return object
*/
public function order_by_convert($orderby, $direction = '')
{
if ($this->_config['enabled'] == TRUE) {
$orderby = 'CONVERT(AES_DECRYPT('.$orderby.',"'.$this->_config['secret_key'].'") using latin1)';
$this->_CI->db->order_by($orderby,$direction,FALSE);
} else {
$this->_CI->db->order_by($orderby,$direction);
}
}

/**
* Like
*
* Generates a %LIKE% portion of the query. Separates
* multiple calls with AND
*
* @param mixed
* @param mixed
* @return object
*/
public function like_concat($field1, $field2, $match = '')
{
if ($this->_config['enabled'] == TRUE) {
$field1 = $this->decrypt($field1);
$field2 = $this->decrypt($field2);
$this->_CI->db->like('CONCAT('.$field1.'," ",'.$field2.')', $match, 'AND ', 'both', NULL, FALSE);
} else {
$this->_CI->db->like('CONCAT('.$field1.'," ",'.$field2.')', $match);
}
}

/**
* Insert
*
* Compiles an insert string and runs the query
*
* @param string the table to insert data into
* @param array an associative array of insert values
* @return object
*/
function insert($table = '', $set = NULL)
{
if ($table == '') {
return $this->_CI->db->display_error('db_must_set_table');
}

if ($this->_config['enabled'] == TRUE) {
if ($set) {
$this->set($set);
}

// Getting all the datatypes of the table
$q = $this->_CI->db->query('DESCRIBE '.$table);
$r = $q->result();

foreach ($r as $k => $v) {
$type = explode('(', $v->Type);
if (in_array($type[0], $this->data_types)) {
$this->non_encrypt[] = $v->Field;
} else {
$this->encrypt[] = $v->Field;
}
}

// set
foreach ($this->arr_set as $field => $value) {
if (in_array($field, $this->non_encrypt)) {
$this->_CI->db->set($field,$value);
} else {
$value = $this->encrypt($value);
$this->_CI->db->set($field,trim($value),FALSE);
}
}

$this->reset();
return $this->_CI->db->insert($table);
} else {
return $this->_CI->db->insert($table,$set);
}
}

/**
* Update
*
* Compiles an update string and runs the query
*
* @param string the table to retrieve the results from
* @param array an associative array of update values
* @param mixed the where clause
* @return object
*/
public function update($table = '', $set = NULL)
{
if ($table == '') {
return $this->_CI->db->display_error('db_must_set_table');
}

if ($this->_config['enabled'] == TRUE) {
if ($set) {
$this->set($set);
}

// Getting all the datatypes of the table
$q = $this->_CI->db->query('DESCRIBE '.$table);
$r = $q->result();

foreach ($r as $k => $v) {
$type = explode('(', $v->Type);
if (in_array($type[0], $this->data_types)) {
$this->non_encrypt[] = $v->Field;
} else {
$this->encrypt[] = $v->Field;
}
}

// set
foreach ($this->arr_set as $field => $value) {
if (in_array($field, $this->non_encrypt)) {
$this->_CI->db->set($field,$value);
} else {
$value = $this->encrypt($value);
$this->_CI->db->set($field,$value,FALSE);
}
}

// where
$tmp = array();
if ($this->arr_where) {
foreach ($this->arr_where as $field => $value) {
$field1 = $field;

if (preg_match('/\./', $field)) {
$tmp = explode('.', $field);
$field1 = $tmp[1];
}

if (in_array($field1, $this->non_encrypt)) {
$this->_CI->db->where($field,$value);
} else {
$value = $this->encrypt($value);
$this->_CI->db->where($field,$value,FALSE);
}
}
}

$this->reset();
return $this->_CI->db->update($table);
} else {
return $this->_CI->db->update($table,$set);
}
}

/**
* Get
*
* Compiles the select statement based on the other functions called
* and runs the query
*
* @param string the table
* @param string the limit clause
* @param string the offset clause
* @return object
*/
public function get($table, $limit = null, $offset = null)
{
if ($this->_config['enabled'] == TRUE) {
if (preg_match('/\\sAS\\s/', $table)) {
$tmp = explode(' AS ', $table);
$ptable = $tmp[0];
} else {
$ptable = $table;
}

// Getting all the datatypes of the table
$q = $this->_CI->db->query('DESCRIBE '.$ptable);
$r = $q->result();

foreach ($r as $k => $v) {
$type = explode('(', $v->Type);
if (in_array($type[0], $this->data_types)) {
$this->non_encrypt[] = $v->Field;
} else {
$this->encrypt[] = $v->Field;
}
}

// check if join
if ($this->arr_join) {
foreach ($this->arr_join as $jtable) {

// Getting all the datatypes of the table
$jq = $this->_CI->db->query('DESCRIBE '.$jtable);
$jr[$jtable] = $jq->result();

foreach ($jr[$jtable] as $k => $v) {
$type = explode('(', $v->Type);
if (in_array($type[0], $this->data_types)) {
$this->non_encrypt[] = $v->Field;
} else {
$this->encrypt[] = $v->Field;
}
}
}
}

$this->check_alias($table);

// select
if ($this->arr_distinct) {
foreach ($this->arr_distinct as $val) {
if ($val == '*') {
// for primary table
foreach ($r as $k => $v) {
if (in_array($v->Field, $this->non_encrypt)) {
$this->_CI->db->distinct($this->arr_table[$ptable].'.'.$v->Field.' AS '.$v->Field);
} else {
$this->_CI->db->distinct('AES_DECRYPT('.$this->arr_table[$ptable].'.'.$v->Field.',"'.$this->_config['secret_key'].'") AS '.$v->Field,FALSE);
}
}

if ($this->arr_join) {
// for join table
foreach ($this->arr_join as $jtable) {
foreach ($jr[$jtable] as $k => $v) {
if (in_array($v->Field, $this->non_encrypt)) {
$this->_CI->db->distinct($this->arr_table[$jtable].'.'.$v->Field.' AS '.$v->Field);
} else {
$this->_CI->db->distinct('AES_DECRYPT('.$this->arr_table[$jtable].'.'.$v->Field.',"'.$this->_config['secret_key'].'") AS '.$v->Field,FALSE);
}
}
}
}
} else {
if (preg_match('/\./', $val)) {
$tmp = explode('.', $val);
$field = $tmp[1];
} else {
$field = $val;
}

if (preg_match('/\\sAS\\s/', $val)) {
$tmp = explode(' AS ', $val);
$val = $tmp[0];
$field = $tmp[1];
}

if (in_array($field, $this->non_encrypt)) {
$this->_CI->db->distinct($val);
} else {
$this->_CI->db->distinct('AES_DECRYPT('.$val.',"'.$this->_config['secret_key'].'") AS '.$field,FALSE);
}
}
}
} elseif ($this->arr_select) { // For specifying tables
foreach ($this->arr_select as $val) {
if ($val == '*') {
// for primary table
foreach ($r as $k => $v) {
if (in_array($v->Field, $this->non_encrypt)) {
$this->_CI->db->select($this->arr_table[$ptable].'.'.$v->Field.' AS `'.$v->Field.'`');
} else {
$this->_CI->db->select('AES_DECRYPT('.$this->arr_table[$ptable].'.'.$v->Field.',"'.$this->_config['secret_key'].'") AS `'.$v->Field.'`',FALSE);
}
}

if ($this->arr_join) {
// for join table
foreach ($this->arr_join as $jtable) {
foreach ($jr[$jtable] as $k => $v) {
if (in_array($v->Field, $this->non_encrypt)) {
$this->_CI->db->select($this->arr_table[$jtable].'.'.$v->Field.' AS `'.$v->Field.'`');
} else {
$this->_CI->db->select('AES_DECRYPT('.$this->arr_table[$jtable].'.'.$v->Field.',"'.$this->_config['secret_key'].'") AS `'.$v->Field.'`',FALSE);
}
}
}
}
} else {
if (preg_match('/\./', $val)) {
$tmp = explode('.', $val);
$field = $tmp[1];
} else {
$field = $val;
}

if (preg_match('/\\sAS\\s/', $val)) {
$tmp = explode(' AS ', $val);
$val = $tmp[0];
$field = $tmp[1];
}

if (in_array($field, $this->non_encrypt)) {
$this->_CI->db->select($val);
} else {
$this->_CI->db->select('AES_DECRYPT('.$val.',"'.$this->_config['secret_key'].'") AS '.$field,FALSE);
}
}
}
} else { // SELECT is *
// for primary table
foreach ($r as $k => $v) {
if (in_array($v->Field, $this->non_encrypt)) {
$this->_CI->db->select($this->arr_table[$ptable].'.'.$v->Field.' AS `'.$v->Field.'`');
} else {
$this->_CI->db->select('AES_DECRYPT('.$this->arr_table[$ptable].'.'.$v->Field.',"'.$this->_config['secret_key'].'") AS `'.$v->Field.'`',FALSE);
}
}

if ($this->arr_join) {
// for join table

foreach ($this->arr_join as $jtable) {
foreach ($jr[$jtable] as $k => $v) {
if (in_array($v->Field, $this->non_encrypt)) {
$this->_CI->db->select($this->arr_table[$jtable].'.'.$v->Field.' AS `'.$v->Field.'`');
} else {
$this->_CI->db->select('AES_DECRYPT('.$this->arr_table[$jtable].'.'.$v->Field.',"'.$this->_config['secret_key'].'") AS `'.$v->Field.'`',FALSE);
}
}
}
}

}

// where
$tmp = array();
if ($this->arr_where) {
foreach ($this->arr_where as $field => $value) {
$field1 = $field;

if (preg_match('/\./', $field)) {
$tmp = explode('.', $field);
$field1 = $tmp[1];
}

if (in_array($field1, $this->non_encrypt)) {
$this->_CI->db->where($field,$value);
} else {
$this->_CI->db->where($field,'AES_ENCRYPT("'.$value.'","'.$this->_config['secret_key'].'")',FALSE);
}
}
}

$this->reset();
return $this->_CI->db->get($table,$limit,$offset);
} else {
return $this->_CI->db->get($table,$limit,$offset);
}
}

public function check_alias($table)
{
// check alias define
if (preg_match('/\\sAS\\s/', $table)) {
$t = explode(' AS ', $table);
} elseif (preg_match('/\\s/', $table)) {
$t = explode(' ', $table);
} else {
$t = array($table,$table);
}
$this->arr_table[$t[0]] = $t[1];
}

public function decrypt($var)
{
return 'AES_DECRYPT('.$var.',"'.$this->_config['secret_key'].'")';
}

public function encrypt($var)
{
return 'AES_ENCRYPT("'.$var.'","'.$this->_config['secret_key'].'")';
}
}

.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#<91>system<92> can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn<92>t true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file
#Submitted by Michael Radlmaier (mradlmaier)
RewriteCond $1 !^(index\.php|img|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
# If we don<92>t have mod_rewrite installed, all 404<92>s
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin

ErrorDocument 404 /index.php
</IfModule>
php_value upload_max_filesize "5M"
php_value date.timezone "Asia/Manila"

Thursday, July 6, 2017

Custom Google Map

function InitiallizeMap(){
 var myLatLng = new google.maps.LatLng(-33.903236,151.142783);
 var myOptions = {
  zoom: 16,
  center: myLatLng,
  mapTypeId: google.maps.MapTypeId.ROADMAP,
  mapTypeControl: false,
  zoomControl:false,
  streetViewControl:false,
 }
 var styles = [
  {
   "stylers": [
    { "invert_lightness": true },
    { "saturation": -47 },
    { "lightness": -9 }
   ]
  },{
   "featureType": "road",
   "stylers": [
    { "saturation": -91 },
    { "lightness": 25 },
    { "gamma": 0.92 }
   ]
  }
 ];

 
 var image = '/images/img_map-overlay-icon.png';
 var map = new google.maps.Map(document.getElementById("googleMap"),myOptions);
 var marker = new google.maps.Marker({
    position: new google.maps.LatLng(-33.903236,151.142783),
    map : map,
    icon: image
 });
 map.setOptions({styles: styles});
 google.maps.event.addListener(marker,'click', function() {
  window.open("https://maps.google.com/maps?q=The+Hub+House,+412+New+Canterbury+Road,+Dulwich+Hill,+NSW,+2203,+Australia&hl=en&ll=-33.903245,151.142778&spn=0.00976,0.021007&sll=-33.903485,151.143653&sspn=0.00244,0.005252&oq=hub410-412+New+canterbury+Road,Dulwich+hill.+NSW+2203&hq=The+Hub+House,&hnear=412+New+Canterbury+Rd,+Dulwich+Hill+New+South+Wales+2203,+Australia&t=m&z=16");   
 });  
}

Thursday, February 9, 2017

php search in a multi dimensional associative array

function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $key => $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return (object) $haystack[$key];
}
}
return false;
}

Input Amount jQuery Plugin

/*
Name:input_amount
Author:Reyneil Lato
Date:February 09, 2017
Time:04:47PM
Version:1.0
Description: amount formatter
*/

(function($){
$.fn.input_amount = function(options) {
// defaults
var defaults = {
decimal : 2
};  
var options = $.extend(defaults, options);
return this.each(function(){
jQuery(this).on("keypress keyup blur keydown",function(event) {
var t = jQuery(this).val();
// prevent letters and arrow
if((event.which >= 65 && event.keyCode <= 90) || (event.which >= 37 && event.which <= 40) || event.which == 32){
event.preventDefault();
}

// only 1 decimal point and 2 decimal places
if(t.indexOf(".") != -1){
var dec = t.toString().split(".");
if((event.which == 190 || event.which == 110) || (dec[1].length >= parseInt(options.decimal) && event.which != 8)){
event.preventDefault();
}
}

// format with commas every 3 digit
jQuery(this).val(function(index, value) {
var c = value.toString();

if(c.indexOf(".") != -1){
var x = c.split(".");
x[0] = x[0].toString().replace(/,/gi, "");
x[0] = x[0].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");

return x.join(".");
}
else{
var z = c.toString().replace(/,/gi, "");
z = z.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return z;
}
});
});
});
};
})(jQuery);

Wednesday, February 8, 2017

revert old firebug

browse
about:config

search
browser.tabs.remote.autostart,
browser.tabs.remote.autostart.1
browser.tabs.remote.autostart.2

and change all values to false