Do you wish to register an account?
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

845 lines
18 KiB

  1. <?php
  2. /**
  3. * Public Library Todo
  4. *
  5. * Library for general tasks in Todo application
  6. * @package Todo
  7. */
  8. class Todo {
  9. private $user, $pass, $uid; //For user registration
  10. /**
  11. * @var MY_Controller
  12. */
  13. protected $CI;
  14. /**
  15. * Constructor
  16. */
  17. public function __construct()
  18. {
  19. $this->CI =& get_instance();
  20. }
  21. // --------------------------------------------------------------------------
  22. /**
  23. * Get User From Id
  24. *
  25. * Retrieve a user's username from their userid
  26. * @param int $user_id
  27. * @return string
  28. */
  29. public function get_user_from_id($user_id)
  30. {
  31. $this->CI->db->select('id, username')
  32. ->from('todo_user')
  33. ->where('id', (int) $user_id);
  34. $res = $this->CI->db->get();
  35. $row = $res->row();
  36. return $row->username;
  37. }
  38. // --------------------------------------------------------------------------
  39. /**
  40. * Crypt Pass
  41. *
  42. * Hashes passwords
  43. * @param string $password
  44. * @return string
  45. */
  46. public function crypt_pass($password)
  47. {
  48. return password_hash($password, PASSWORD_BCRYPT);
  49. }
  50. // --------------------------------------------------------------------------
  51. /**
  52. * Add Reg
  53. *
  54. * Submits a new user to the database
  55. * @return integer
  56. */
  57. public function add_reg()
  58. {
  59. $user = $this->CI->input->post('user', TRUE);
  60. $pass = $this->crypt_pass($this->CI->input->post('pass', TRUE));
  61. $email = $this->CI->input->post('email', TRUE);
  62. $this->CI->db->set('username', $user)
  63. ->set('password', $pass)
  64. ->set('email', $email);
  65. $this->CI->db->insert('user');
  66. //Get affected rows
  67. $affected_rows = $this->CI->db->affected_rows();
  68. //Get the userid of the latest user
  69. $res = $this->CI->db->select('MAX(id) as id')
  70. ->from('user')
  71. ->get();
  72. $row = $res->row();
  73. $this->uid = $row->id;
  74. //Add a group with the same name as the user
  75. $this->CI->db->set('name', $user)
  76. ->insert('group');
  77. //Get the groupid of the latest group
  78. $res2 = $this->CI->db->select('MAX(id) as id')
  79. ->from('group')
  80. ->get();
  81. $row = $res2->row();
  82. $g_id = $row->id;
  83. //Set that user as the admin of that group
  84. $this->CI->db->set('group_id', $g_id)
  85. ->set('user_id', $this->uid)
  86. ->set('is_admin', 1)
  87. ->insert('group_users_link');
  88. //Return affected rows
  89. return $affected_rows;
  90. }
  91. // --------------------------------------------------------------------------
  92. /**
  93. * Get Categories
  94. *
  95. * Retrieves list of category types from the database
  96. * @return array
  97. */
  98. public function get_category_list()
  99. {
  100. $user_group_id = $this->get_user_group();
  101. $cat = $this->CI->db->select('id,title,description,group_id')
  102. ->from('category')
  103. ->where('group_id', $user_group_id)
  104. ->or_where('group_id', 0)
  105. ->order_by('group_id', 'desc')
  106. ->order_by('title', 'asc')
  107. ->get();
  108. return $cat->result_array();
  109. }
  110. // --------------------------------------------------------------------------
  111. /**
  112. * Get Group List
  113. *
  114. * An alias for the private get_groups method
  115. * @param int
  116. * @return array
  117. */
  118. public function get_group_list($user_id)
  119. {
  120. return $this->get_groups($user_id);
  121. }
  122. // --------------------------------------------------------------------------
  123. /**
  124. * Add Category
  125. *
  126. * Submits a new category to the database
  127. * @return bool
  128. */
  129. public function add_category()
  130. {
  131. if($this->CI->input->post('title') == FALSE || $this->CI->input->post('desc') == FALSE)
  132. {
  133. show_error('You must put a title and description!');
  134. return false;
  135. }
  136. $title = $this->CI->input->post('title', TRUE);
  137. $desc = $this->CI->input->post('desc', TRUE);
  138. //Check for the current category
  139. $this->CI->db->select('title')
  140. ->from('category')
  141. ->where('title', $title);
  142. $res = $this->CI->db->get();
  143. if($res->num_rows() == 0)
  144. {
  145. //Get the current user's primary group
  146. $group_id = $this->get_user_group();
  147. $this->CI->db->set('title', $title)
  148. ->set('description', $desc)
  149. ->set('group_id', $group_id);
  150. //Insert the new record
  151. $this->CI->db->insert('category');
  152. $this->CI->session->flashdata('message', 'Successfully added new category.');
  153. return true;
  154. }
  155. else
  156. {
  157. show_error('This category already exists!');
  158. return false;
  159. }
  160. }
  161. // --------------------------------------------------------------------------
  162. /**
  163. * Add Group
  164. *
  165. * Submits a new group to the database
  166. * @return bool
  167. */
  168. public function add_group()
  169. {
  170. if($this->CI->input->post('name') == FALSE)
  171. {
  172. show_error('You must have a name for your new group!');
  173. return false;
  174. }
  175. $name = $this->CI->input->post('name');
  176. //Add group
  177. $this->CI->db->set("name", $name)->insert('group');
  178. //Get the groupid of the latest group
  179. $res = $this->CI->db->select('MAX(id) as id')
  180. ->from('group')
  181. ->get();
  182. $row = $res->row();
  183. $g_id = $row->id;
  184. //Set that user as the admin of that group
  185. $this->CI->db->set('group_id', $g_id)
  186. ->set('user_id', $this->CI->session->userdata('uid'))
  187. ->set('is_admin', 1)
  188. ->insert('group_users_link');
  189. }
  190. // --------------------------------------------------------------------------
  191. /**
  192. * Get Category Select
  193. *
  194. * Generates select options for categories when adding a new task
  195. * @return string
  196. */
  197. public function get_category_select()
  198. {
  199. $select_array = $this->get_category_list();
  200. $html = '';
  201. foreach($select_array as $r)
  202. {
  203. $html .= T4.'<option value="'.$r['id'].'">' . $r['title'] . '</option>'. "\n";
  204. }
  205. return $html;
  206. }
  207. // --------------------------------------------------------------------------
  208. /**
  209. * Get Priority Select
  210. *
  211. * Generates select options for priorities when adding a new task
  212. * @return string
  213. */
  214. public function get_priority_select()
  215. {
  216. $select_array = $this->get_priorities();
  217. $html = '';
  218. foreach($select_array as $r)
  219. {
  220. $html .= T4.'<option value="'.$r['id'].'" ';
  221. $html .= ($r['id'] == 5) ? 'selected="selected">': '>';
  222. $html .= $r['value'] . '</option>'. "\n";
  223. }
  224. return $html;
  225. }
  226. // --------------------------------------------------------------------------
  227. /**
  228. * Get Group Select
  229. *
  230. * Generates select options for groups when adding a friend
  231. * @param int $user_id
  232. * @return string
  233. */
  234. public function get_group_select($user_id)
  235. {
  236. $select_array = $this->get_groups($user_id);
  237. $html = '';
  238. foreach($select_array as $r)
  239. {
  240. $html .= T4.'<option value="'.$r['id'].'">' . $r['name'] . '</option>'. "\n";
  241. }
  242. return $html;
  243. }
  244. // --------------------------------------------------------------------------
  245. /**
  246. * Validate Pass
  247. *
  248. * Validate Password Change
  249. * @return mixed
  250. */
  251. public function validate_pass()
  252. {
  253. $err = array();
  254. $user = (int) $this->CI->session->userdata('uid');
  255. $pass = $this->CI->input->post('pass');
  256. $pass1 = $this->CI->input->post('pass1');
  257. $old_pass = $this->CI->input->post('old_pass');
  258. if($pass != $pass1)
  259. $err[] = "Passwords do not match.";
  260. //Check for current password in the database
  261. $user_check = $this->CI->db->select('password')
  262. ->from('user')
  263. ->where('id', $user)
  264. ->get();
  265. $row = $user_check->row();
  266. if ( ! password_verify($old_pass, $row->password))
  267. {
  268. $err[] = "Wrong password";
  269. }
  270. $res = (empty($err)) ? true : $err;
  271. if($res == TRUE)
  272. {
  273. $this->user = $user;
  274. $this->pass = $pass;
  275. }
  276. return $res;
  277. }
  278. // --------------------------------------------------------------------------
  279. /**
  280. * Update Pass
  281. *
  282. * Updates user's password in the database
  283. */
  284. public function update_pass()
  285. {
  286. $pass = $this->crypt_pass($this->pass);
  287. $this->CI->db->set('password', $pass)
  288. ->where('id', $this->user)
  289. ->update('user');
  290. }
  291. // --------------------------------------------------------------------------
  292. /**
  293. * Redirect 303
  294. *
  295. * Shortcut function for 303 redirect
  296. * @param string $url
  297. */
  298. public function redirect_303($url)
  299. {
  300. if (stripos($url, 'http') === FALSE)
  301. {
  302. $url = site_url($url);
  303. }
  304. $this->CI->output->set_header("HTTP/1.1 303 See Other");
  305. $this->CI->output->set_header("Location:" . $url);
  306. }
  307. // --------------------------------------------------------------------------
  308. /**
  309. * Set Timezone
  310. *
  311. * Sets the timezone based on the user's settings
  312. * @param int $uid
  313. * @param string $timezone
  314. * @return bool
  315. */
  316. public function set_timezone($uid, $timezone)
  317. {
  318. $this->CI->db->set('timezone', $timezone)
  319. ->where('id', $uid)
  320. ->update('user');
  321. return ($this->CI->db->affected_rows == 1);
  322. }
  323. // --------------------------------------------------------------------------
  324. /**
  325. * Get Priorities
  326. *
  327. * Retreives list of priority types from the database
  328. * @return array
  329. */
  330. public function get_priorities()
  331. {
  332. $pri = $this->CI->db->select('id,value')
  333. ->from('priority')
  334. ->order_by('id', 'asc')
  335. ->get();
  336. return $pri->result_array();
  337. }
  338. // --------------------------------------------------------------------------
  339. /**
  340. * Get Groups
  341. *
  342. * Retrieves user's groups from db
  343. * @param int $user_id
  344. * @return array
  345. */
  346. private function get_groups($user_id)
  347. {
  348. $username = $this->get_user_from_id($user_id);
  349. $groups = $this->CI->db->select("group.id, name")
  350. ->from('group')
  351. ->join('group_users_link', 'group.id = group_users_link.group_id', 'inner')
  352. ->where('user_id', $user_id)
  353. ->where('name !=', $username)
  354. ->where('is_admin', 1)
  355. ->order_by('name')
  356. ->get();
  357. return $groups->result_array();
  358. }
  359. // --------------------------------------------------------------------------
  360. /**
  361. * Get User Account By Id
  362. *
  363. * Retrieves user's account info from db
  364. * @param int $user_id
  365. * @return array
  366. */
  367. public function get_user_account_by_id($user_id)
  368. {
  369. $user_account = array();
  370. //Get the user
  371. $user_query = $this->CI->db->from('user')
  372. ->where('id', (int) $user_id)
  373. ->get();
  374. $user = $user_query->row();
  375. $user_account['timezone'] = $user->timezone;
  376. $user_account['user'] = $user->username;
  377. $user_account['email'] = $user->email;
  378. $user_account['num_format'] = $user->num_format;
  379. return $user_account;
  380. }
  381. // --------------------------------------------------------------------------
  382. /**
  383. * Get User Group
  384. *
  385. * Gets the current user's primary group
  386. * @return int
  387. */
  388. public function get_user_group()
  389. {
  390. $user_id = $this->CI->session->userdata('uid');
  391. //Get the username
  392. $uname = $this->get_user_from_id($user_id);
  393. $group_query = $this->CI->db->select('group.id as group_id')
  394. ->from('group')
  395. ->where('name', $uname)
  396. ->limit(1)
  397. ->get();
  398. $group = $group_query->row();
  399. $group_id = $group->group_id;
  400. return $group_id;
  401. }
  402. // --------------------------------------------------------------------------
  403. /**
  404. * Get Friend List
  405. *
  406. * Gets the friends of the current user
  407. * @return array
  408. */
  409. public function get_friend_list()
  410. {
  411. $user_id = $this->CI->session->userdata('uid');
  412. //Get the current user's username
  413. $uname = $this->CI->db->select('username')
  414. ->from('user')
  415. ->where('id', $user_id)
  416. ->get();
  417. $user_n = $uname->row();
  418. $username = $user_n->username;
  419. //Get the list of friends
  420. $friends = $this->CI->db
  421. ->select('user_friend_id,user_friend_link.user_id as uid,user.username')
  422. ->from('todo_user_friend_link')
  423. ->join('user', 'user.id=user_friend_link.user_friend_id OR todo_user.id=todo_user_friend_link.user_id', 'inner')
  424. ->where('confirmed', FRIEND_CONFIRMED)
  425. ->where('username !=', $username)
  426. ->group_start()
  427. ->where_in('todo_user_friend_link.user_id', $user_id)
  428. ->or_where_in('todo_user_friend_link.user_friend_id', $user_id)
  429. ->group_end()
  430. ->order_by('username', 'asc')
  431. ->get();
  432. return $friends->result_array();
  433. }
  434. // --------------------------------------------------------------------------
  435. /**
  436. * Get Friends in Group
  437. *
  438. * Returns members of a group
  439. * @param int $group_id
  440. * @return array
  441. */
  442. public function get_friends_in_group($group_id)
  443. {
  444. $friends = $this->CI->db
  445. ->select('user_id')
  446. ->from('group_users_link')
  447. ->where('group_id', $group_id)
  448. ->order_by('user_id')
  449. ->get();
  450. return $friends->result_array();
  451. }
  452. // --------------------------------------------------------------------------
  453. /**
  454. * Update group
  455. *
  456. * Updates a group's membership
  457. */
  458. public function update_group()
  459. {
  460. $friends = $this->CI->input->post('friends');
  461. $group_name = $this->CI->input->post('group_name');
  462. $group_id = (int)$this->CI->uri->segment('3');
  463. //Drop members in group except the creator
  464. $this->CI->db->where('group_id', $group_id)
  465. ->where('is_admin', 0)
  466. ->delete('group_users_link');
  467. //Update the group name
  468. $this->CI->db->set('name', $group_name)
  469. ->where('id', $group_id)
  470. ->update('group');
  471. foreach ($friends as $friend)
  472. {
  473. //Insert new friends
  474. $this->CI->db->set('group_id', $group_id)
  475. ->set('user_id', (int) $friend)
  476. ->set('is_admin', 0)
  477. ->insert('group_users_link');
  478. }
  479. return 1;
  480. }
  481. // --------------------------------------------------------------------------
  482. /**
  483. * Del group
  484. *
  485. * Deletes a friend group
  486. * @param int $group_id
  487. * @return int
  488. */
  489. public function del_group($group_id)
  490. {
  491. //Check if the current user is group admin
  492. $is_admin = $this->CI->db->from('group_users_link')
  493. ->where('group_id', $group_id)
  494. ->where('is_admin', 1)
  495. ->get();
  496. //The user is admin
  497. if($is_admin->num_rows() > 0)
  498. {
  499. //Delete the related records
  500. $this->CI->db->where('group_id', $group_id)
  501. ->delete('group_users_link');
  502. $this->CI->db->where('group_id', $group_id)
  503. ->delete('group_task_link');
  504. //Delete the group
  505. $this->CI->db->where('id', $group_id)
  506. ->delete('group');
  507. return 1;
  508. }
  509. else
  510. {
  511. return -1;
  512. }
  513. }
  514. // --------------------------------------------------------------------------
  515. /**
  516. * Del Cat
  517. *
  518. * Deletes a task category
  519. * @param int $cat_id
  520. * @return int
  521. */
  522. public function del_cat($cat_id)
  523. {
  524. //Get the user group id
  525. $gid = $this->get_user_group();
  526. //Delete the category that matches the cat_id and gid
  527. $this->CI->db->where('group_id', $gid)
  528. ->where('id', $cat_id)
  529. ->delete('category');
  530. if($this->CI->db->affected_rows() > 0)
  531. {
  532. return $this->CI->db->affected_rows();
  533. }
  534. else
  535. {
  536. return -1;
  537. }
  538. }
  539. // --------------------------------------------------------------------------
  540. /**
  541. * Get group name by id
  542. *
  543. * Gets a group name from the group id
  544. * @param int $group_id
  545. * @return string
  546. */
  547. public function get_group_name_by_id($group_id)
  548. {
  549. $query = $this->CI->db->select('name')
  550. ->from('group')
  551. ->where('id', (int) $group_id)
  552. ->get();
  553. $qrow = $query->row();
  554. return $qrow->name;
  555. }
  556. // --------------------------------------------------------------------------
  557. /**
  558. * Kanji Num
  559. *
  560. * Converts arabic to chinese number
  561. * @param int $orig_number
  562. * @return string
  563. */
  564. public function kanji_num($orig_number)
  565. {
  566. $kanji_num = '';
  567. $number = (int) $orig_number;
  568. // Return early on a zero
  569. if ($number === 0) return ZERO;
  570. // Map variables to their values and characters
  571. $meta_map = [
  572. 100000000 => HUNDRED_MILLION,
  573. 10000 => TEN_THOUSAND,
  574. 1000 => THOUSAND,
  575. 100 => HUNDRED,
  576. 10 => TEN
  577. ];
  578. // Map values to their kanji equivalent
  579. $char_map = [
  580. 1 => ONE,
  581. 2 => TWO,
  582. 3 => THREE,
  583. 4 => FOUR,
  584. 5 => FIVE,
  585. 6 => SIX,
  586. 7 => SEVEN,
  587. 8 => EIGHT,
  588. 9 => NINE,
  589. ];
  590. // Go through each place value
  591. // to get the kanji equivalent of
  592. foreach($meta_map as $value => $char)
  593. {
  594. if ($number < $value) continue;
  595. // Calculate the place value variable
  596. $place_value = floor($number / $value);
  597. // Get the remainder for the next place value;
  598. $number = $number - ($place_value * $value);
  599. // Recurse if the number is between 11,000
  600. // and 100,000,000 to get the proper prefix,
  601. // which can be up to 9,999
  602. if ($orig_number > 10000 && $place_value > 9)
  603. {
  604. $kanji_num .= $this->kanji_num($place_value);
  605. $place_value = 1;
  606. }
  607. // Add place value character and
  608. // place value to the output string,
  609. // skipping zero and one. A zero value
  610. // hides the place value character, and one
  611. // value is implied if there is no value
  612. // prefixing the place value character
  613. $kanji_num .= ($place_value > 1)
  614. ? $char_map[$place_value] . $char
  615. : $char;
  616. }
  617. // Add the smallest place value last, as a
  618. // one value is significant here
  619. $kanji_num .= ($number > 0) ? $char_map[$number] : '';
  620. return $kanji_num;
  621. }
  622. // --------------------------------------------------------------------------
  623. /**
  624. * Get Category
  625. *
  626. * Returns a category from id
  627. * @param int $cat_id
  628. * @return array
  629. */
  630. public function get_category($cat_id)
  631. {
  632. $cats = $this->CI->db->select('title, description')
  633. ->from('category')
  634. ->where('id', $cat_id)
  635. ->limit('1')
  636. ->get();
  637. $cat = $cats->row_array();
  638. return $cat;
  639. }
  640. // --------------------------------------------------------------------------
  641. /**
  642. * Get Friend Requests
  643. *
  644. * Retrieves number of friend requests for the current user
  645. * @return int
  646. */
  647. public function get_friend_requests()
  648. {
  649. static $requests = NULL;
  650. if (is_null($requests))
  651. {
  652. //Get friend requests for the current user
  653. $requests = $this->CI->db->select('user_id')
  654. ->distinct()
  655. ->from('user_friend_link')
  656. ->where('user_friend_id', $this->CI->session->userdata('uid'))
  657. ->where('confirmed', -1)
  658. ->get()
  659. ->num_rows();
  660. }
  661. return $requests;
  662. }
  663. /**
  664. * Authenticate the user
  665. *
  666. * @return string
  667. */
  668. public function verify_user()
  669. {
  670. $user = $this->CI->input->post('user');
  671. $pass = $this->CI->input->post('pass');
  672. //Check for the user in the database
  673. $uid_check = $this->CI->db->select('id, username, email, password, timezone, num_format')
  674. ->from('user')
  675. ->group_start()
  676. ->where('email', $user)
  677. ->or_where('username', $user)
  678. ->group_end()
  679. ->get();
  680. $row = $uid_check->row();
  681. if (password_verify($pass, $row->password))
  682. {
  683. $this->CI->session->set_userdata('uid', $row->id);
  684. $this->CI->session->set_userdata('num_format', $row->num_format);
  685. $this->CI->session->set_userdata('username', $row->username);
  686. //Set Timezone
  687. $zone = $row->timezone;
  688. $tz_set = date_default_timezone_set($zone);
  689. if($tz_set == FALSE) display_error('Could not set timezone');
  690. //Redirect to task list
  691. return TRUE;
  692. }
  693. else
  694. {
  695. return "Invalid username or password";
  696. }
  697. }
  698. }
  699. // End of libraries/Todo.php