/modules/Usermanager/libraries/usermanager_user.php

https://github.com/be3/ionize · PHP · 333 lines · 259 code · 46 blank · 28 comment · 92 complexity · 8640200dc6986d2f943753880bfe0820 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 (!isset($tag->attr['html_encode']) || $tag->attr['html_encode'] == "1") ? htmlentities(utf8_decode($ret)) : $ret;
  159. // If the post data is compared to is_like
  160. else
  161. return $tag->attr['is_like'] === $ret ? $tag->expand() : "";
  162. }
  163. /*
  164. * Functions, that are used by tags
  165. */
  166. public function is_editor($tag)
  167. {
  168. $ci = &get_instance();
  169. $user = $this->get_current_user(isset($tag->attr['id_user']) ? $tag->attr['id_user'] : false);
  170. if (!$user)
  171. return "";
  172. if (isset($tag->attr['is_like']))
  173. {
  174. if ($user['group']['id_group'] < 4 && $tag->attr['is_like'] == '1')
  175. return $tag->expand();
  176. if ($user['group']['id_group'] >= 4 && $tag->attr['is_like'] == '1')
  177. return "";
  178. if ($user['group']['id_group'] < 4 && $tag->attr['is_like'] == '0')
  179. return "";
  180. if ($user['group']['id_group'] >= 4 && $tag->attr['is_like'] == '0')
  181. return $tag->expand();
  182. }
  183. else
  184. {
  185. if($user['group']['id_group'] < 4)
  186. return $tag->expand();
  187. return "";
  188. }
  189. return "";
  190. }
  191. public function is_logged_in($tag)
  192. {
  193. $ci = &get_instance();
  194. $user = $this->get_current_user(isset($tag->attr['id_user']) ? $tag->attr['id_user'] : false);
  195. if (isset($tag->attr['is_like']))
  196. {
  197. if ($user && $tag->attr['is_like'] == '1')
  198. return $tag->expand();
  199. if (!$user && $tag->attr['is_like'] == '1')
  200. return "";
  201. if ($user && $tag->attr['is_like'] == '0')
  202. return "";
  203. if (!$user && $tag->attr['is_like'] == '0')
  204. return $tag->expand();
  205. }
  206. else
  207. {
  208. if (!$user)
  209. return "";
  210. return $tag->expand();
  211. }
  212. return "";
  213. }
  214. public function get_picture($tag)
  215. {
  216. include APPPATH . '../modules/Usermanager/config/config.php';
  217. $ci = &get_instance();
  218. if (!isset($ci->usermanager_picture))
  219. $ci->load->library('usermanager_picture');
  220. $user = $this->get_current_user(isset($tag->attr['id_user']) ? $tag->attr['id_user'] : false);
  221. if (!$user || !$tag->attr['field'])
  222. return "";
  223. $dimensions = $tag->attr['dimensions'] ? $tag->attr['dimensions'] : "original";
  224. if ($user[$tag->attr['field']] == "1")
  225. {
  226. $g = glob($config['usermanager_picture'][$tag->attr['field']]['upload_path'] . "/".$user['id_user']."/" . $user['id_user']."_".$tag->attr['field']."_".$dimensions.".*");
  227. if (!$g || empty($g))
  228. return $config['usermanager_picture'][$tag->attr['field']]['default'][$dimensions];
  229. else
  230. 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]);
  231. }
  232. return $config['usermanager_picture'][$tag->attr['field']]['default'][$dimensions];
  233. }
  234. /**
  235. * Return the activation key stored in locals vars.
  236. * The activation key should be set in locals before calling this function
  237. *
  238. */
  239. public function get_activation_key($tag)
  240. {
  241. if ( ! empty($tag->locals->vars['activation_key']))
  242. {
  243. return $tag->locals->vars['activation_key'];
  244. }
  245. return '';
  246. }
  247. public function activate($tag)
  248. {
  249. $ci = &get_instance();
  250. if (!isset($ci->usermanager_functions))
  251. $ci->load->library('usermanager_functions');
  252. if (isset($tag->attr['has_success']))
  253. {
  254. if (!empty($ci->usermanager_functions->additional_success) && $tag->attr['has_success'] == '1')
  255. return $tag->expand();
  256. if (empty($ci->usermanager_functions->additional_success) && $tag->attr['has_success'] == '0')
  257. return $tag->expand();
  258. }
  259. return '';
  260. }
  261. /*
  262. * Private functions
  263. */
  264. private function _upload_present($key)
  265. {
  266. $ci = &get_instance();
  267. if (!($ci->input->post($key) === false) &&
  268. isset($_FILES[$key]) &&
  269. isset($_FILES[$key]['name']) &&
  270. isset($_FILES[$key]['size']) &&
  271. $_FILES[$key]['name'] &&
  272. $_FILES[$key]['size'] &&
  273. !($ci->input->post($key) === "delete"))
  274. return true;
  275. else
  276. return false;
  277. }
  278. private function _upload_to_delete($key)
  279. {
  280. $ci = &get_instance();
  281. if (!($ci->input->post($key) === false) &&
  282. ($ci->input->post($key) === "delete"))
  283. return true;
  284. else
  285. return false;
  286. }
  287. }