PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Controller/UsersController.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 130 lines | 86 code | 20 blank | 24 comment | 6 complexity | ab6f45eb24aed90f905af7d219094005 MD5 | raw file
  1. <?php
  2. /*
  3. Document : fktwitter
  4. Created on : Dec 26, 2011, 7:12:34 PM
  5. Author : udeshika
  6. Description: fktwitter
  7. */
  8. class UsersController extends AppController {
  9. public $name = 'Users';
  10. public $helpers = array('Html', 'Form');
  11. public $components = array('Session');
  12. public function index() {
  13. }
  14. /**
  15. * login process and login form
  16. * @access public
  17. * @param
  18. * @return none
  19. */
  20. public function login() {
  21. $this->layout = 'register';
  22. if (empty($this->data) == false) {
  23. if ($this->validate_login($this->data) == 1) {
  24. $this->Session->write('User', $this->data['username']);
  25. $this->Session->setFlash('You\'ve successfully logged in.');
  26. $this->redirect('../twits/stream/everyone');
  27. exit();
  28. } else {
  29. $this->Session->setFlash('Sorry, the information you\'ve entered is incorrect.');
  30. exit();
  31. }
  32. }
  33. }
  34. public function logout() {
  35. $this->Session->destroy();
  36. $this->Session->setFlash('You\'ve successfully logged out.');
  37. $this->redirect('login');
  38. }
  39. public function view($id) {
  40. $this->Twits->id = $id;
  41. $this->set('post', $this->Twits->read());
  42. }
  43. public function viewall() {
  44. $results = $this->User->find('all');
  45. $this->set('users', $results);
  46. $this->layout = '';
  47. }
  48. /**
  49. * add new user after jq validations
  50. * @access public
  51. * @param unabale to use mysql_real_escape_string without $link
  52. * @return none
  53. */
  54. public function add() {
  55. $this->layout = '';
  56. if ($this->request->is('post')) {
  57. $postdata = $this->request->data;
  58. $link = mysql_connect('localhost', 'root', 'mypass');
  59. $sql = "INSERT INTO `cake`.`users` (
  60. `id` ,
  61. `username` ,
  62. `password` ,
  63. `fname` ,
  64. `lname` ,
  65. `email` ,
  66. `location` ,
  67. `web` ,
  68. `bio` ,
  69. `active` ,
  70. `created` ,
  71. `modified`,
  72. `image`
  73. )
  74. VALUES (
  75. NULL ,
  76. '" . $postdata['username'] . "',
  77. '" . $postdata['password'] . "',
  78. '" . $postdata['fname'] . "',
  79. '" . $postdata['lname'] . "',
  80. '" . $postdata['email'] . "',
  81. '" . $postdata['location'] . "',
  82. '" . $postdata['web'] . "',
  83. '" . $postdata['bio'] . "',
  84. '1',
  85. '',
  86. '',
  87. '" . mysql_real_escape_string(file_get_contents($this->params['data']['User']['image']['tmp_name']), $link) . "'
  88. );";
  89. $echo = $this->User->query($sql);
  90. $this->Session->setFlash('Your post has been created.');
  91. $this->redirect(array('action' => 'login'));
  92. }
  93. }
  94. /**
  95. * count users with same username
  96. * @access public
  97. * @param $login user data array
  98. * @return $count; number of users with same username
  99. */
  100. public function validate_login($login) {
  101. $count = $this->User->find('count', array(
  102. 'conditions' => array('User.username' => $login['username'], 'User.password' => $login['password'])
  103. ));
  104. return $count;
  105. }
  106. public function register() {
  107. $this->layout = 'register';
  108. }
  109. }
  110. ?>