PageRenderTime 56ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_jfusionplugins/mantis/user.php

http://jfusion.googlecode.com/
PHP | 231 lines | 134 code | 34 blank | 63 comment | 18 complexity | 2b9ad341467622d25ef27ebe471b6925 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * @package JFusion_mantis
  4. * @version 1.0.7
  5. * @author JFusion development team
  6. * @copyright Copyright (C) 2008 JFusion. All rights reserved.
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  8. */
  9. // no direct access
  10. defined('_JEXEC' ) or die('Restricted access' );
  11. /**
  12. * load the JFusion framework
  13. */
  14. require_once(JPATH_ADMINISTRATOR .DS.'components'.DS.'com_jfusion'.DS.'models'.DS.'model.abstractuser.php');
  15. require_once(JPATH_ADMINISTRATOR .DS.'components'.DS.'com_jfusion'.DS.'models'.DS.'model.jplugin.php');
  16. /**
  17. * JFusion plugin class for mantis
  18. * @package JFusion_mantis
  19. */
  20. class JFusionUser_mantis extends JFusionUser {
  21. function &getUser($userinfo)
  22. {
  23. // get the username
  24. if (is_object($userinfo)){
  25. $username = $userinfo->username;
  26. } else {
  27. $username = $userinfo;
  28. }
  29. // initialise some objects
  30. $params = JFusionFactory::getParams($this->getJname());
  31. $db = JFusionFactory::getDatabase($this->getJname());
  32. $query = 'SELECT id as userid,username, realname as name, email, password, enabled FROM #__user_table WHERE username='. $db->Quote($username);
  33. $db->setQuery($query );
  34. $result = $db->loadObject();
  35. $result->activation = 0;
  36. $result->block = 0;
  37. return $result;
  38. }
  39. function getJname()
  40. {
  41. return 'mantis';
  42. }
  43. function deleteUser($userinfo)
  44. {
  45. //setup status array to hold debug info and errors
  46. $status = array();
  47. $status['debug'] = array();
  48. $status['error'] = array();
  49. $db = JFusionFactory::getDatabase($this->getJname());
  50. $query = 'DELETE FROM #__user_table WHERE username = '.$db->quote($userinfo->username);
  51. $db->setQuery($query);
  52. if (!$db->query()) {
  53. $status['error'][] = JText::_('USER_DELETION_ERROR') . ' ' . $db->stderr();
  54. } else {
  55. $status['error'] = false;
  56. $status['debug'][] = JText::_('USER_DELETION'). ' ' . $userinfo->username;
  57. }
  58. return $status;
  59. }
  60. function destroySession($userinfo, $options){
  61. // $params = JFusionFactory::getParams($this->getJname());
  62. // setcookie($params->get('cookie_name'), '',0,$params->get('cookie_path'),$params->get('cookie_domain'),$params->get('secure'),$params->get('httponly'));
  63. return JFusionJplugin::destroySession($userinfo, $options,$this->getJname());
  64. }
  65. function createSession($userinfo, $options) {
  66. /*
  67. // initialise some objects
  68. $params = JFusionFactory::getParams($this->getJname());
  69. $db = JFusionFactory::getDatabase($this->getJname());
  70. $cookie_expires = $params->get('cookie_expires');
  71. if ($cookie_expires) {
  72. $expires = 60*60*24*365;
  73. } else {
  74. $expires = 60 * $cookie_expires;
  75. }
  76. $expires = $expires+time();
  77. $query = 'SELECT cookie_string FROM #__user_table WHERE username='.$db->Quote($userinfo->username);
  78. $db->setQuery($query);
  79. $result = $db->loadObject();
  80. setcookie($params->get('cookie_name'), $result->cookie_string ,$expires,$params->get('cookie_path'),$params->get('cookie_domain'),$params->get('secure'),$params->get('httponly'));
  81. */
  82. return JFusionJplugin::createSession($userinfo, $options,$this->getJname());
  83. }
  84. function filterUsername($username) {
  85. //no username filtering implemented yet
  86. return $username;
  87. }
  88. function updatePassword($userinfo, &$existinguser, &$status)
  89. {
  90. if (isset($userinfo->password_clear)) {
  91. $existinguser->password = md5( $userinfo->password_clear );
  92. } else {
  93. $existinguser->password = $userinfo->password;
  94. }
  95. $db = JFusionFactory::getDatabase($this->getJname());
  96. $query = 'UPDATE #__user_table SET password = ' . $db->quote($existinguser->password). ' WHERE id = ' . $existinguser->userid;
  97. $db = JFusionFactory::getDatabase($this->getJname());
  98. $db->setQuery($query );
  99. if (!$db->query()) {
  100. $status['error'][] = JText::_('PASSWORD_UPDATE_ERROR') . $db->stderr();
  101. } else {
  102. $status['debug'][] = JText::_('PASSWORD_UPDATE') . ' ' . substr($existinguser->password,0,6) . '********';
  103. }
  104. }
  105. function updateEmail($userinfo, &$existinguser, &$status)
  106. {
  107. //we need to update the email
  108. $db = JFusionFactory::getDatabase($this->getJname());
  109. $query = 'UPDATE #__user_table SET email = ' . $db->quote($userinfo->email) . ' WHERE id = ' . $existinguser->userid;
  110. $db->setQuery($query);
  111. if (!$db->query()) {
  112. $status['error'][] = JText::_('EMAIL_UPDATE_ERROR') . $db->stderr();
  113. } else {
  114. $status['debug'][] = JText::_('EMAIL_UPDATE'). ': ' . $existinguser->email . ' -> ' . $userinfo->email;
  115. }
  116. }
  117. function createUser($userinfo, &$status)
  118. {
  119. //we need to create a new SMF user
  120. $db = JFusionFactory::getDatabase($this->getJname());
  121. $params = JFusionFactory::getParams($this->getJname());
  122. $source_path = $params->get('source_path');
  123. //prepare the user variables
  124. $user = new stdClass;
  125. $user->id = NULL;
  126. $user->username = $userinfo->username;
  127. $user->realname = $userinfo->name;
  128. $user->email = $userinfo->email;
  129. $user->date_created = $user->last_visit = date('Y-m-t H:i:s',time());
  130. if (isset($userinfo->password_clear)) {
  131. $user->password = md5( $userinfo->password_clear );
  132. } else {
  133. $user->password = $userinfo->password;
  134. }
  135. $t_seed = $user->email . $user->username;
  136. $user->cookie_string = JFusionUser_mantis::auth_generate_unique_cookie_string( $t_seed );
  137. if ($userinfo->activation) {
  138. $user->enabled = 0;
  139. } else {
  140. $user->enabled = 1;
  141. }
  142. $user->access_level = $params->get('usergroup', 10);
  143. //now append the new user data
  144. if (!$db->insertObject('#__user_table', $user, 'id' )) {
  145. //return the error
  146. $status['error'] = JText::_('USER_CREATION_ERROR'). ': ' . $db->stderr();
  147. return $status;
  148. } else {
  149. //return the good news
  150. $status['debug'][] = JText::_('USER_CREATION');
  151. $status['userinfo'] = $this->getUser($userinfo->username);
  152. return $status;
  153. }
  154. }
  155. /**
  156. * Generate a string to use as the identifier for the login cookie
  157. * It is not guaranteed to be unique and should be checked
  158. * The string returned should be 64 characters in length
  159. * @return string 64 character cookie string
  160. * @access public
  161. */
  162. function auth_generate_cookie_string() {
  163. $t_val = mt_rand( 0, mt_getrandmax() ) + mt_rand( 0, mt_getrandmax() );
  164. $t_val = md5( $t_val ) . md5( time() );
  165. return substr( $t_val, 0, 64 );
  166. }
  167. /**
  168. * Generate a UNIQUE string to use as the identifier for the login cookie
  169. * The string returned should be 64 characters in length
  170. * @return string 64 character cookie string
  171. * @access public
  172. */
  173. function auth_generate_unique_cookie_string() {
  174. do {
  175. $t_cookie_string = $this->auth_generate_cookie_string();
  176. }
  177. while( !$this->auth_is_cookie_string_unique( $t_cookie_string ) );
  178. return $t_cookie_string;
  179. }
  180. /**
  181. * Return true if the cookie login identifier is unique, false otherwise
  182. * @param string $p_cookie_string
  183. * @return bool indicating whether cookie string is unique
  184. * @access public
  185. */
  186. function auth_is_cookie_string_unique( $p_cookie_string ) {
  187. $db = JFusionFactory::getDatabase($this->getJname());
  188. $query = 'SELECT count(*) from #__user_table WHERE cookie_string='.$p_cookie_string;
  189. $db->setQuery($query );
  190. if( $db->loadResult() > 0 ) {
  191. return false;
  192. } else {
  193. return true;
  194. }
  195. }
  196. }