/modules/Usermanager/libraries/usermanager_user.php

https://github.com/chamnan/ionize · PHP · 351 lines · 274 code · 48 blank · 29 comment · 93 complexity · 96b86860c48392e318490afbc40193b1 MD5 · raw file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Usermanager_User {
  3. function __construct()
  4. {
  5. $ci = &get_instance();
  6. if (!isset($ci->usermanager_usermodel))
  7. $ci->load->model('usermanager_usermodel');
  8. }
  9. /*
  10. * Functions, that are used by the module itself
  11. */
  12. public function get_current_user($id_user = false)
  13. {
  14. $ci = &get_instance();
  15. $user = "";
  16. if ($id_user === false)
  17. {
  18. $user = $ci->connect->get_current_user();
  19. }
  20. else
  21. {
  22. $ci->load->model('connect_model');
  23. $user = $ci->connect_model->find_user(array($ci->connect_model->users_pk => $id_user));
  24. }
  25. if ($user)
  26. return array_merge($user, $ci->usermanager_usermodel->get_custom_fields($user));
  27. else
  28. return false;
  29. }
  30. public function get_custom_fields($id_user = false)
  31. {
  32. $ci = &get_instance();
  33. $user = "";
  34. if ($id_user === false)
  35. $user = $ci->connect->get_current_user();
  36. else
  37. $user = $ci->access->find_user(array($user_pk => $this->session->userdata($id_user)));
  38. if ($user)
  39. return $ci->usermanager_usermodel->get_custom_fields($user);
  40. else
  41. return false;
  42. }
  43. public function set_custom_fields($id)
  44. {
  45. $ci = &get_instance();
  46. return $ci->usermanager_usermodel->set_custom_fields($id);
  47. }
  48. // Used for registration and Profile editing
  49. // Only sets present fields
  50. public function update_custom_fields($id)
  51. {
  52. $ci = &get_instance();
  53. $ret = true;
  54. // First check for pictures and change them, then set the fields
  55. // The picture fields are also set in update_custom_fields
  56. $ret = $this->upload_pictures($id);
  57. $ret2 = $ci->usermanager_usermodel->update_custom_fields($id);
  58. return $ret && $ret2;
  59. }
  60. // Also users-table
  61. // Only sets present fields
  62. public function update_all_fields($id)
  63. {
  64. $ci = &get_instance();
  65. $ret = true;
  66. // First check for pictures and change them, then set the fields
  67. // The picture fields are also set in update_custom_fields
  68. $ret = $this->upload_pictures($id);
  69. $ret2 = $ci->usermanager_usermodel->update_all_fields($id);
  70. return $ret && $ret2;
  71. }
  72. public function upload_pictures($id)
  73. {
  74. include APPPATH . '../modules/Usermanager/config/config.php';
  75. $ci = &get_instance();
  76. if (!isset($ci->usermanager_picture))
  77. $ci->load->library('usermanager_picture');
  78. if (!isset($ci->usermanager_functions))
  79. $ci->load->model('usermanager_functions');
  80. foreach ($config['usermanager_user_model'] as $key => $val)
  81. {
  82. if ($val['special_field'] === "picture")
  83. {
  84. if ($val['save'] != 'users' && $val['save'] != false && $this->_upload_present($key))
  85. {
  86. $r = $ci->usermanager_picture->upload_picture($key, $id);
  87. if (!($r === true))
  88. {
  89. $ci->usermanager_functions->additional_err['upload'] = $r;
  90. $_POST[$key] = "0";
  91. return false;
  92. }
  93. else
  94. {
  95. $_POST[$key] = "1";
  96. }
  97. }
  98. elseif ($val['save'] != 'users' && $val['save'] != false && $this->_upload_to_delete($key))
  99. {
  100. $ci->usermanager_picture->delete_picture($key, $user);
  101. $_POST[$key] = "0";
  102. }
  103. else
  104. {
  105. $g = glob($config['usermanager_picture'][$key]['upload_path'] . "/".$id."/" . $id."_".$key."_"."original".".*");
  106. if (!$g || empty($g))
  107. $_POST[$key] = "0";
  108. else
  109. $_POST[$key] = "1";
  110. }
  111. }
  112. }
  113. return true;
  114. }
  115. public function update_field($id, $key, $val = false)
  116. {
  117. $ci = &get_instance();
  118. return $ci->usermanager_usermodel->update_field($id, $key, $val);
  119. }
  120. public function delete_user($id)
  121. {
  122. $ci = &get_instance();
  123. $d1 = $ci->usermanager_usermodel->delete_user($id);
  124. $d2 = $ci->users_model->delete($id);
  125. if ($d1 && $d2)
  126. return true;
  127. return false;
  128. }
  129. public function check_for_missing_tables($id)
  130. {
  131. $ci = &get_instance();
  132. $ci->usermanager_usermodel->check_for_missing_tables($id);
  133. }
  134. public function get_field($tag)
  135. {
  136. include APPPATH . '../modules/Usermanager/config/config.php';
  137. $ci = &get_instance();
  138. $user = $this->get_current_user(isset($tag->attr['id_user']) ? $tag->attr['id_user'] : false);
  139. //if (!$user)
  140. // return "";
  141. $ret = "";
  142. if ($tag->attr['attr'] === "username" && $config['usermanager_email_as_username'])
  143. $tag->attr['attr'] = "email";
  144. if (isset($config['usermanager_user_model'][$tag->attr['attr']]) && $config['usermanager_user_model'][$tag->attr['attr']]['special_field'] != "restricted")
  145. {
  146. // Don't use default value if it's a checkbox and we're in validation process:
  147. if (!($config['usermanager_user_model'][$tag->attr['attr']]['special_field'] === 'checkbox' && isset($tag->attr["from_post_data"]) && $tag->attr["from_post_data"] === $ci->input->post('form_name')))
  148. if (isset($tag->attr["from_default_value"]) && $tag->attr["from_default_value"] === "1")
  149. $ret = $config['usermanager_user_model'][$tag->attr['attr']]['default_value'] === false ? "" : $config['usermanager_user_model'][$tag->attr['attr']]['default_value'];
  150. if (isset($tag->attr["from_user_field"]) && $tag->attr["from_user_field"] === "1" && $user)
  151. $ret = isset($user[$tag->attr['attr']]) && $user[$tag->attr['attr']] != null ? $user[$tag->attr['attr']] : $ret;
  152. if (isset($tag->attr["from_post_data"]) && $ci->input->post('form_name') === $tag->attr["from_post_data"])
  153. $ret = !($ci->input->post($tag->attr['attr']) === false) ? $ci->input->post($tag->attr['attr']) : $ret;
  154. //$ret = $ci->input->post($tag->attr['attr']) === false && ($config['usermanager_user_model'][$tag->attr['attr']]['default_value'] === "1" || $config['usermanager_user_model'][$tag->attr['attr']]['default_value'] === 1) && !($ci->input->post("register") === "1" || $ci->input->post("login") === "1" || $ci->input->post("minilogin") === "1" || $ci->input->post("editdo") === "1") ? "1" : $ci->input->post($tag->attr['attr']); // Checkboxes
  155. }
  156. // If only the post data is requested
  157. if (!isset($tag->attr['is_like']))
  158. return $ret;
  159. // return (!isset($tag->attr['html_encode']) || $tag->attr['html_encode'] == "1") ? htmlentities(utf8_decode($ret)) : $ret;
  160. // If the post data is compared to is_like
  161. else
  162. return $tag->attr['is_like'] === $ret ? $tag->expand() : "";
  163. }
  164. /*
  165. * Functions, that are used by tags
  166. */
  167. public function is($tag)
  168. {
  169. $ci = &get_instance();
  170. $user = $this->get_current_user(isset($tag->attr['id_user']) ? $tag->attr['id_user'] : false);
  171. if (!$user)
  172. return "";
  173. if (isset($tag->attr['is']))
  174. {
  175. if ($ci->connect->is($tag->attr['is']))
  176. {
  177. return $tag->expand();
  178. }
  179. }
  180. return "";
  181. }
  182. public function is_editor($tag)
  183. {
  184. $ci = &get_instance();
  185. $user = $this->get_current_user(isset($tag->attr['id_user']) ? $tag->attr['id_user'] : false);
  186. if (!$user)
  187. return "";
  188. if (isset($tag->attr['is_like']))
  189. {
  190. if ($user['group']['id_group'] < 4 && $tag->attr['is_like'] == '1')
  191. return $tag->expand();
  192. if ($user['group']['id_group'] >= 4 && $tag->attr['is_like'] == '1')
  193. return "";
  194. if ($user['group']['id_group'] < 4 && $tag->attr['is_like'] == '0')
  195. return "";
  196. if ($user['group']['id_group'] >= 4 && $tag->attr['is_like'] == '0')
  197. return $tag->expand();
  198. }
  199. else
  200. {
  201. if($user['group']['id_group'] < 4)
  202. return $tag->expand();
  203. return "";
  204. }
  205. return "";
  206. }
  207. public function is_logged_in($tag)
  208. {
  209. $ci = &get_instance();
  210. $user = $this->get_current_user(isset($tag->attr['id_user']) ? $tag->attr['id_user'] : false);
  211. if (isset($tag->attr['is_like']))
  212. {
  213. if ($user && $tag->attr['is_like'] == '1')
  214. return $tag->expand();
  215. if (!$user && $tag->attr['is_like'] == '1')
  216. return "";
  217. if ($user && $tag->attr['is_like'] == '0')
  218. return "";
  219. if (!$user && $tag->attr['is_like'] == '0')
  220. return $tag->expand();
  221. }
  222. else
  223. {
  224. if (!$user)
  225. return "";
  226. return $tag->expand();
  227. }
  228. return "";
  229. }
  230. public function get_picture($tag)
  231. {
  232. include APPPATH . '../modules/Usermanager/config/config.php';
  233. $ci = &get_instance();
  234. if (!isset($ci->usermanager_picture))
  235. $ci->load->library('usermanager_picture');
  236. $user = $this->get_current_user(isset($tag->attr['id_user']) ? $tag->attr['id_user'] : false);
  237. if (!$user || !$tag->attr['field'])
  238. return "";
  239. $dimensions = $tag->attr['dimensions'] ? $tag->attr['dimensions'] : "original";
  240. if ($user[$tag->attr['field']] == "1")
  241. {
  242. $g = glob($config['usermanager_picture'][$tag->attr['field']]['upload_path'] . "/".$user['id_user']."/" . $user['id_user']."_".$tag->attr['field']."_".$dimensions.".*");
  243. if (!$g || empty($g))
  244. return $config['usermanager_picture'][$tag->attr['field']]['default'][$dimensions];
  245. else
  246. return $config['usermanager_picture'][$tag->attr['field']]['view_path'] . "/".$user['id_user']."/" . $user['id_user']."_".$tag->attr['field']."_".$dimensions.".".$ci->usermanager_picture->get_extention($g[0]);
  247. }
  248. return $config['usermanager_picture'][$tag->attr['field']]['default'][$dimensions];
  249. }
  250. /**
  251. * Return the activation key stored in locals vars.
  252. * The activation key should be set in locals before calling this function
  253. *
  254. */
  255. public function get_activation_key($tag)
  256. {
  257. if ( ! empty($tag->locals->vars['activation_key']))
  258. {
  259. return $tag->locals->vars['activation_key'];
  260. }
  261. return '';
  262. }
  263. public function activate($tag)
  264. {
  265. $ci = &get_instance();
  266. if (!isset($ci->usermanager_functions))
  267. $ci->load->library('usermanager_functions');
  268. if (isset($tag->attr['has_success']))
  269. {
  270. if (!empty($ci->usermanager_functions->additional_success) && $tag->attr['has_success'] == '1')
  271. return $tag->expand();
  272. if (empty($ci->usermanager_functions->additional_success) && $tag->attr['has_success'] == '0')
  273. return $tag->expand();
  274. }
  275. return '';
  276. }
  277. /*
  278. * Private functions
  279. */
  280. private function _upload_present($key)
  281. {
  282. $ci = &get_instance();
  283. if (!($ci->input->post($key) === false) &&
  284. isset($_FILES[$key]) &&
  285. isset($_FILES[$key]['name']) &&
  286. isset($_FILES[$key]['size']) &&
  287. $_FILES[$key]['name'] &&
  288. $_FILES[$key]['size'] &&
  289. !($ci->input->post($key) === "delete"))
  290. return true;
  291. else
  292. return false;
  293. }
  294. private function _upload_to_delete($key)
  295. {
  296. $ci = &get_instance();
  297. if (!($ci->input->post($key) === false) &&
  298. ($ci->input->post($key) === "delete"))
  299. return true;
  300. else
  301. return false;
  302. }
  303. }