PageRenderTime 84ms CodeModel.GetById 54ms RepoModel.GetById 0ms app.codeStats 0ms

/application/controllers/api/users.php

https://github.com/reverseproductions/socialigniter
PHP | 449 lines | 319 code | 69 blank | 61 comment | 46 complexity | 4ba4874b5322905ba3e83337ac22e96b MD5 | raw file
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /*
  3. * Social-Igniter : Core : Users : API Controller
  4. *
  5. * @package Social Igniter
  6. * @subpackage Social Igniter Library
  7. * @author Brennan Novak
  8. * @link http://social-igniter.com
  9. *
  10. */
  11. class Users extends Oauth_Controller
  12. {
  13. function __construct()
  14. {
  15. parent::__construct();
  16. $this->form_validation->set_error_delimiters('', '');
  17. }
  18. function recent_get()
  19. {
  20. $users = $this->social_auth->get_users('active', 1);
  21. if($users)
  22. {
  23. $message = array('status' => 'success', 'message' => '1 - 10 recent users', 'data' => $users);
  24. }
  25. else
  26. {
  27. $message = array('status' => 'error', 'message' => 'Oops could not find any users');
  28. }
  29. $this->response($message, 200);
  30. }
  31. function view_get()
  32. {
  33. if(!$this->get('user_id'))
  34. {
  35. $message = array('status' => 'error', 'message' => 'You must specific a user_id in the url');
  36. }
  37. $user = $this->social_auth->get_user($this->get('id'));
  38. if($user)
  39. {
  40. $mesage = array('status' => 'success', 'message' => 'User found', 'data' => $user);
  41. }
  42. else
  43. {
  44. $message = array('status' => 'error', 'message' => 'User could not be found');
  45. }
  46. $this->response($message, 200);
  47. }
  48. function create_post()
  49. {
  50. $this->form_validation->set_rules('name', 'Name', 'required');
  51. $this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
  52. $this->form_validation->set_rules('password', 'Password', 'required|min_length['.config_item('min_password_length').']|max_length['.$this->config->item('max_password_length').']|strong_pass['.config_item('password_strength').']|matches[password_confirm]');
  53. $this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required');
  54. if ($this->form_validation->run() == true)
  55. {
  56. $username = url_username($this->input->post('name'), 'none', true);
  57. $email = $this->input->post('email');
  58. $password = $this->input->post('password');
  59. $additional_data = array(
  60. 'name' => $this->input->post('name'),
  61. 'image' => ''
  62. );
  63. if ($this->social_auth->register($username, $password, $email, $additional_data, config_item('default_group')))
  64. {
  65. $message = array('status' => 'success', 'message' => 'User successfully created');
  66. }
  67. else
  68. {
  69. $message = array('status' => 'error', 'message' => 'Oops could not create user');
  70. }
  71. }
  72. else
  73. {
  74. $message = array('message' => 'Oops '.validation_errors());
  75. }
  76. $this->response($message, 200);
  77. }
  78. function login_post()
  79. {
  80. // Validate form input
  81. $this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
  82. $this->form_validation->set_rules('password', 'Password', 'required');
  83. if ($this->form_validation->run() == true)
  84. {
  85. // Check "remember me"
  86. if ($this->input->post('remember') == 1)
  87. {
  88. $remember = TRUE;
  89. }
  90. else
  91. {
  92. $remember = FALSE;
  93. }
  94. // Attempt Login
  95. if ($this->social_auth->login($this->input->post('email'), $this->input->post('password'), $remember))
  96. {
  97. $message = array('status' => 'success', 'message' => 'User successfully logged in');
  98. }
  99. else
  100. {
  101. $message = array('status' => 'error', 'message' => 'Oops could not log you in');
  102. }
  103. }
  104. else
  105. {
  106. $message = array('message' => 'Oops '.validation_errors());
  107. }
  108. $this->response($message, 200);
  109. }
  110. function set_userdata_signup_email_post()
  111. {
  112. log_message('debug', 'AHHHHH At Top Of Shizzle');
  113. $this->form_validation->set_rules('signup_email', 'Email Address', 'required|valid_email');
  114. if ($this->form_validation->run() == true)
  115. {
  116. log_message('debug', 'AHHHHH Inside Validator');
  117. $email = $this->input->post('signup_email');
  118. if ($user = $this->social_auth->get_user('email', $email))
  119. {
  120. $message = array('status' => 'error', 'message' => 'Oops that email address is in use by someone else!', 'data' => $user);
  121. }
  122. else
  123. {
  124. $this->session->set_userdata('signup_email', $email);
  125. $this->session->set_userdata('signup_user_state', 'has_connection_and_email');
  126. $message = array('status' => 'success', 'message' => 'Awesome, you will now be redirected to finish setting up your account');
  127. }
  128. }
  129. else
  130. {
  131. log_message('debug', 'AHHHHH Not valid');
  132. $message = array('message' => 'Oops '.validation_errors());
  133. }
  134. $this->response($message, 200);
  135. }
  136. // Update User
  137. function modify_authd_post()
  138. {
  139. if ($this->oauth_user_id == $this->get('id'))
  140. {
  141. // User
  142. $user_id = $this->oauth_user_id;
  143. // Delete Picture
  144. if ($this->input->post('delete_pic') == 1)
  145. {
  146. $this->load->helper('file');
  147. delete_files($this->config->item('profile_images').$user->user_id."/");
  148. $user_picture = '';
  149. }
  150. else
  151. {
  152. $user_picture = '';
  153. }
  154. /*
  155. // Upload Picture
  156. if (!$this->input->post('userfile'))
  157. {
  158. $config['upload_path'] = config_item('uploads_folder');
  159. $config['allowed_types'] = config_item('users_images_formats');
  160. $config['overwrite'] = true;
  161. $config['max_size'] = config_item('users_images_max_size');
  162. $config['max_width'] = config_item('users_images_max_dimensions');
  163. $config['max_height'] = config_item('users_images_max_dimensions');
  164. $this->load->library('upload',$config);
  165. if (!$this->upload->do_upload())
  166. {
  167. $error = array('error' => $this->upload->display_errors());
  168. }
  169. else
  170. {
  171. // Load Image Model
  172. $this->load->model('image_model');
  173. // Upload & Sizes
  174. $file_data = $this->upload->data();
  175. $image_sizes = array('full', 'large', 'medium', 'small');
  176. // Process New Images
  177. $image_size = getimagesize(config_item('uploads_folder').$image_save);
  178. $file_data = array('file_name' => $image_save, 'image_width' => $image_size[0], 'image_height' => $image_size[1]);
  179. $image_sizes = array('full', 'large', 'medium', 'small');
  180. $create_path = config_item('users_images_folder').$user_id.'/';
  181. $this->image_model->make_images($file_data, 'users', $image_sizes, $create_path, TRUE);
  182. }
  183. }
  184. */
  185. $update_data = array(
  186. 'username' => url_username($this->input->post('username'), 'none', true),
  187. 'email' => $this->input->post('email'),
  188. 'gravatar' => md5($this->input->post('email')),
  189. 'name' => $this->input->post('name'),
  190. 'image' => $user_picture,
  191. 'time_zone' => $this->input->post('time_zone'),
  192. 'privacy' => $this->input->post('privacy'),
  193. 'language' => $this->input->post('language'),
  194. 'geo_enabled' => $this->input->post('geo_enabled'),
  195. );
  196. if ($this->social_auth->update_user($user_id, $update_data))
  197. {
  198. $user = $this->social_auth->get_user('user_id', $user_id);
  199. $this->social_auth->set_userdata($user);
  200. $message = array('status' => 'success', 'message' => 'User changes saved', 'data' => $user);
  201. }
  202. else
  203. {
  204. $message = array('status' => 'error', 'message' => 'Could not save user changes');
  205. }
  206. }
  207. else
  208. {
  209. $message = array('status' => 'error', 'message' => 'Ooops this is not your user account');
  210. }
  211. $this->response($message, 200);
  212. }
  213. function details_authd_post()
  214. {
  215. if ($this->oauth_user_id == $this->get('id'))
  216. {
  217. $user_meta_data = array();
  218. // User
  219. $user_id = $this->oauth_user_id;
  220. // Site
  221. if ($this->input->post('site_id')) $site_id = $this->input-->post('site_id');
  222. else $site_id = config_item('site_id');
  223. // Build Meta
  224. foreach (config_item('user_meta_details') as $config_meta)
  225. {
  226. $user_meta_data[$config_meta] = $this->input->post($config_meta);
  227. }
  228. // Update
  229. if ($update_meta = $this->social_auth->update_user_meta($site_id, $user_id, $this->input->post('module'), $user_meta_data))
  230. {
  231. // Update User Data
  232. $this->social_auth->set_userdata_meta($user_id);
  233. $message = array('status' => 'success', 'message' => 'User details saved', 'data' => $user_meta_data);
  234. }
  235. else
  236. {
  237. $message = array('status' => 'error', 'message' => 'Could not save user details at this time');
  238. }
  239. }
  240. else
  241. {
  242. $message = array('status' => 'error', 'message' => 'Ooops this is not your user account');
  243. }
  244. // STILL NEED AN ELSE FOR ADMINS TO MODIFY
  245. $this->response($message, 200);
  246. }
  247. function password_authd_post()
  248. {
  249. $this->form_validation->set_rules('old_password', 'Old password', 'required');
  250. $this->form_validation->set_rules('new_password', 'New Password', 'required|min_length['.config_item('min_password_length').']|max_length['.config_item('max_password_length').']|matches[new_password_confirm]');
  251. $this->form_validation->set_rules('new_password_confirm', 'Confirm New Password', 'required');
  252. if ($this->form_validation->run() == true)
  253. {
  254. if ($change = $this->social_auth->change_password($this->oauth_user_id, $this->input->post('old_password'), $this->input->post('new_password')))
  255. {
  256. $message = array('status' => 'success', 'message' => 'Password changed Successfully');
  257. }
  258. else
  259. {
  260. $message = array('status' => 'error', 'message' => 'Oops could not change your password');
  261. }
  262. }
  263. else
  264. {
  265. $message = array('status' => 'error', 'message' => validation_errors());
  266. }
  267. $this->response($message, 200);
  268. }
  269. function mobile_add_authd_post()
  270. {
  271. $this->form_validation->set_rules('phone', 'Phone', 'required|valid_phone_number');
  272. if ($this->form_validation->run() == true)
  273. {
  274. if ($user->phone_verify == 'verified') { $phone = $user->phone; }
  275. else { $phone = ereg_replace("[^0-9]", "", $this->input->post('phone')); }
  276. if ($user->phone_verify == 'verified') { $phone_verify = $user->phone_verify; }
  277. else { $phone_verify = random_element(config_item('mobile_verify')); }
  278. $update_data = array(
  279. 'phone' => $phone,
  280. 'phone_verify' => $phone_verify,
  281. 'phone_active' => $this->input->post('phone_active'),
  282. 'phone_search' => $this->input->post('phone_search')
  283. );
  284. if ($this->social_auth->update_user($this->session->userdata('user_id'), $update_data))
  285. {
  286. $this->session->set_flashdata('message', "Phone Number Added");
  287. redirect('settings/mobile', 'refresh');
  288. }
  289. else
  290. {
  291. redirect('settings/mobile', 'refresh');
  292. }
  293. }
  294. else
  295. {
  296. $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
  297. $this->data['phone'] = $this->input->post('phone');
  298. $this->data['phone_active_array'] = array('1'=>'Yes','0'=>'No');
  299. $this->data['phone_active'] = $this->input->post('phone_active');
  300. if ($user->phone_search) { $phone_search_checked = true; }
  301. else { $phone_search_checked = false; }
  302. $this->data['phone_search'] = array(
  303. 'name' => 'phone_search',
  304. 'id' => 'phone_search',
  305. 'value' => $user->phone_search,
  306. 'checked' => $phone_search_checked,
  307. );
  308. }
  309. $this->data['phone'] = is_empty($user->phone);
  310. $this->data['phone_verify'] = $user->phone_verify;
  311. $this->data['phone_active'] = $user->phone_active;
  312. if ($user->phone_search) { $phone_search_checked = true; }
  313. else { $phone_search_checked = false; }
  314. $this->data['phone_search'] = array(
  315. 'name' => 'phone_search',
  316. 'id' => 'phone_search',
  317. 'value' => $user->phone_search,
  318. 'checked' => $phone_search_checked,
  319. );
  320. $this->response($message, 200);
  321. }
  322. function mobile_destroy_authd_get()
  323. {
  324. $user = $this->social_auth->get_user($this->session->userdata('user_id'));
  325. if ($user->phone != "")
  326. {
  327. $update_data = array(
  328. 'phone' => "",
  329. 'phone_verify' => "",
  330. 'phone_active' => "",
  331. 'phone_search' => ""
  332. );
  333. if ($this->social_auth->update_user($this->session->userdata('user_id'), $update_data))
  334. {
  335. $message = array('status' => 'success', 'message' => 'Phone number deleted');
  336. }
  337. else
  338. {
  339. $message = array('status' => 'error', 'message' => 'Could not delete phone number');
  340. }
  341. }
  342. $this->response($message, 200);
  343. }
  344. /* Advanced Fields */
  345. function advanced_authd_post()
  346. {
  347. $message = array('status' => 'success', 'message' => 'User advanced settings updated');
  348. $this->response($message, 200);
  349. }
  350. // Activate User
  351. function activate_authd_get()
  352. {
  353. if ($activation = $this->social_auth->activate($this->get('id'), $this->get('code')))
  354. {
  355. $message = array('status' => 'success', 'message' => 'User activated');
  356. }
  357. else
  358. {
  359. $message = array('status' => 'error', 'message' => 'User could not be activated');
  360. }
  361. $this->response($message, 200);
  362. }
  363. // Deactivate User
  364. function deactivate_authd_get($id)
  365. {
  366. $this->social_auth->deactivate($id);
  367. $this->response($message, $response);
  368. }
  369. function destroy_get()
  370. {
  371. // $this->some_model->deletesomething($this->get('id'));
  372. $message = array('status' => 'success', 'message' => 'User was deleted');
  373. $this->response($message, 200);
  374. }
  375. }