Guest User

Untitled

a guest
Jan 11th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. <?php
  2. class MY_Model extends CI_Model
  3. {
  4. function __construct(){
  5. parent::__construct();
  6. // Load the Database Module REQUIRED for this to work.
  7. $this->load->database();//Without it -> Message: Undefined property: XXXController::$db
  8. }
  9.  
  10. function fetch($select = "*", $table = "", $where = "", $order = "", $by = "DESC", $start = -1, $limit = 0, $return_array = false)
  11. {
  12. $this->db->select($select);
  13. if($where != "")
  14. {
  15. $this->db->where($where);
  16. }
  17. if($order != "" && (strtolower($by) == "desc" || strtolower($by) == "asc"))
  18. {
  19. if($order == 'rand'){
  20. $this->db->order_by('rand()');
  21. }else{
  22. $this->db->order_by($order, $by);
  23. }
  24. }
  25.  
  26. if((int)$start >= 0 && (int)$limit > 0)
  27. {
  28. $this->db->limit($limit, $start);
  29. }
  30. #Query
  31. $query = $this->db->get($table);
  32. if($return_array){
  33. $result = $query->result_array();
  34. } else {
  35. $result = $query->result();
  36. }
  37. $query->free_result();
  38. return $result;
  39. }
  40.  
  41. function get($select = "*", $table = "", $where = "", $order = "", $by = "DESC", $return_array = false)
  42. {
  43. $this->db->select($select);
  44. if($where != "")
  45. {
  46. $this->db->where($where);
  47. }
  48. if($order != "" && (strtolower($by) == "desc" || strtolower($by) == "asc"))
  49. {
  50. if($order == 'rand'){
  51. $this->db->order_by('rand()');
  52. }else{
  53. $this->db->order_by($order, $by);
  54. }
  55. }
  56. #Query
  57. $query = $this->db->get($table);
  58. if($return_array){
  59. $result = $query->row_array();
  60. } else {
  61. $result = $query->row();
  62. }
  63. $query->free_result();
  64. return $result;
  65. }
  66.  
  67. function history_ip($USER= array()){
  68. if(!empty($USER)){
  69. $ip= getIP();$arr_ip= array();$key= false;
  70. if(!is_array(json_decode($USER->history_ip))){
  71. $arr_ip= @get_object_vars(json_decode($USER->history_ip));
  72. }else{
  73. $arr_ip= json_decode($USER->history_ip);
  74. }
  75.  
  76. if(!empty($arr_ip)){
  77. $key = array_key_exists($ip, $arr_ip); // $key = 1;
  78. }else{
  79. $arr_ip= array($ip=>1);
  80. }
  81.  
  82. if(empty($key)){
  83. //NOT EXIST
  84. if(empty($USER->history_ip)){
  85. $arr_ip= array($ip=>1);
  86. }else{
  87. $new_arr_ip= array($ip=>1);
  88. $arr_ip= array_merge($arr_ip, $new_arr_ip);
  89. }
  90. }else{
  91. $arr_ip[$ip]= $arr_ip[$ip]+1;
  92. }
  93.  
  94. $update['history_ip']= json_encode($arr_ip);
  95. $this->db->update(USER_TB, $update, "id = '{$USER->id}'");
  96. }
  97. }
  98.  
  99. function session($USER= ''){
  100. set_session('uid', $USER->id);
  101. set_session('admin', $USER->admin);
  102. set_session('pid', $USER->pid);
  103. set_session('fullname', $USER->fullname);
  104. set_session('username', $USER->email);
  105. set_session('user_type', $USER->type);
  106. set_session('user_created', $USER->created);
  107. if (!file_exists('uploads/user'.(string)$USER->id)) {
  108. mkdir('uploads/user'.$USER->id, 0777, true);
  109. }
  110. set_cookie('folderid', 'user'.(string)$USER->id, 86400);
  111. }
  112.  
  113. function validate_error(&$error='', &$form_error=''){
  114. $error= (empty($error))?$form_error:true;
  115. return $error;
  116. }
  117.  
  118. function validate_null(&$arr_error='', &$form_error='', $field=''){
  119. $field= $this->input->post($field);
  120.  
  121. if(!is_array($field))
  122. {
  123. $field= trim($field);
  124. if($field == '')
  125. {
  126. $form_error= true;
  127. return FALSE;
  128. }
  129. }
  130. else
  131. {
  132. $field= trim($field[0]);
  133. if($field == '')
  134. {
  135. $form_error= true;
  136. return FALSE;
  137. }
  138. }
  139. return TRUE;
  140. }
  141.  
  142. function validate_ext(&$arr_error='', &$error='', $field='', $txt=''){
  143. if($this->validate_null($arr_error, $form_error, $field)){
  144. }else{
  145. $arr_error[]= array(
  146. 'field' => $field,
  147. 'txt' => (!empty($txt)) ? $txt : $this->require_txt
  148. );
  149. }
  150. $this->validate_error($error, $form_error);
  151. }
  152.  
  153. function validate_youtube(&$arr_error='', &$error='', $field='', $txt='', &$youtube_id=''){
  154. if($this->validate_null($arr_error, $form_error, $field)){
  155. $youtube_id= youtube_id($this->input->post($field));
  156. if(empty($youtube_id))
  157. {
  158. $arr_error[]= array(
  159. 'field' => $field,
  160. 'txt' => $txt
  161. );
  162. $form_error= true;
  163. }
  164. }else{
  165. $arr_error[]= array(
  166. 'field' => $field,
  167. 'txt' => $txt
  168. );
  169. }
  170. $this->validate_error($error, $form_error);
  171. }
  172.  
  173. function permission($mid= 0, $type= ''){
  174. if(!$this->session->userdata('admin'))
  175. {
  176. if(!empty($mid))
  177. {
  178. $rid= $this->session->userdata('admin_rid');
  179. $role_permission= $this->adminls_model->get('*', ADMIN_ROLE_PERMISSION_TB, "rid = '{$rid}' AND mid = '{$mid}'");
  180.  
  181. if(empty($role_permission))
  182. {
  183. redirect(base_url().LINK_ADMIN_PERMISSION_DENY);
  184. }
  185. else
  186. {
  187. $permission= json_decode($role_permission->permission);
  188. switch($type){
  189. case('read'):
  190. case('edit'):
  191. case('delete'):
  192. case('manage'):
  193. if(empty($permission->$type)) redirect(base_url().LINK_ADMIN_PERMISSION_DENY);
  194. break;
  195. }
  196. }
  197. }
  198. else
  199. {
  200. redirect(base_url().LINK_ADMIN_PERMISSION_DENY);
  201. }
  202. }
  203. else
  204. {
  205. $module= $this->get('*', ADMIN_MODULE_TB, "id = '{$mid}'");
  206. if(!empty($module))
  207. {
  208. $data_module= json_decode($module->data);
  209. switch($type){
  210. case('edit'):
  211. if(empty($data_module->edit)) redirect(base_url().LINK_ADMIN_PERMISSION_DENY);
  212. break;
  213. case('delete'):
  214. if(empty($data_module->delete)) redirect(base_url().LINK_ADMIN_PERMISSION_DENY);
  215. break;
  216. }
  217. }
  218. }
  219. }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment