A Qcodo based CMS/ecommerce framework
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.

382 lines
16 KiB

12 years ago
  1. <?php
  2. if(!defined('QUASICMS') ) die("No quasi.");
  3. if (!defined("LOGINMODULE.CLASS.PHP")){
  4. define("LOGINMODULE.CLASS.PHP",1);
  5. /**
  6. * Class LoginModule - a login block
  7. * This class provides a basic login module that may be assigned (once per page)
  8. * to any content block. It validates that the fields are filled in, checks the account
  9. * table for the username and performs a match on the password. It supports both
  10. * Quasi native and OsCommerce encryption schemes (checking for both).
  11. *
  12. * On successful login the module will display the username and the length of time
  13. * that they have been logged in as well as a shopping cart status if the shopping
  14. * cart module is enabled. The user is redirected to a configurable landing page
  15. * (defaulting to "AccountHome").On failure, the entry fields are redrawn and an
  16. * error message is displayed.
  17. *
  18. * This module also provides a link to a registration page and a password retrieval
  19. * page - these should exist in the page table and contain modules for each of these
  20. * functions (CreateAccountModule and LostPasswordModule).
  21. *
  22. * The module stores an Account object in serialized form in $_SESSION['AccountLogin']
  23. * if login succeeds. Session timeout as configured for PHP determines the idle timeout.
  24. *
  25. *@todo
  26. * - more elegant error messages, its a bit messy ..
  27. * - create an enable/disable interface that will ensure that the registration and lost
  28. * password pages and modules exist.
  29. * - allow disabling individual parts, esp. shopping cart status
  30. * - when accepting an old oscommerce style password, transform to new style ..
  31. *
  32. *@author Erik Winn <erikwinnmail@yahoo.com>
  33. *
  34. * $Id: LoginModule.class.php 516 2009-03-19 20:14:17Z erikwinn $
  35. *@version 0.1
  36. *
  37. *@copyright (C) 2008 by Erik Winn
  38. *@license GPL v.2
  39. This program is free software; you can redistribute it and/or modify
  40. it under the terms of the GNU General Public License as published by
  41. the Free Software Foundation; either version 2 of the License, or
  42. (at your option) any later version.
  43. This program is distributed in the hope that it will be useful,
  44. but WITHOUT ANY WARRANTY; without even the implied warranty of
  45. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  46. GNU General Public License for more details.
  47. You should have received a copy of the GNU General Public License
  48. along with this program; if not, write to the Free Software
  49. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
  50. *
  51. *@package Quasi
  52. * @subpackage Modules
  53. */
  54. class LoginModule extends QPanel
  55. {
  56. // Our control block
  57. protected $objControlBlock;
  58. //Local reference to the Account object
  59. protected $objAccount;
  60. //shows "you are signed in as .."
  61. public $lblSignedInAs;
  62. //shows the login duration, just for fun ..
  63. public $lblLoginSpan;
  64. // Shopping cart status display ..
  65. public $lblShoppingCartStatus;
  66. //Input Controls
  67. public $txtUsername;
  68. public $txtPassword;
  69. // Button Controls
  70. public $btnLogin;
  71. public $btnLogout;
  72. /**
  73. * LoginModule constructor
  74. * NOTE: This module ignores the required extra parameters ..
  75. *@param ContentBlockView - parent controller object.
  76. *@param mixed - extra parameters, ignored
  77. */
  78. public function __construct( ContentBlockView $objControlBlock, $mixParameters=null)
  79. {
  80. //Parent should always be a ContentBlockView
  81. $this->objControlBlock =& $objControlBlock;
  82. try {
  83. parent::__construct($this->objControlBlock);
  84. } catch (QCallerException $objExc) {
  85. $objExc->IncrementOffset();
  86. throw $objExc;
  87. }
  88. $this->HtmlEntities = false;
  89. $this->strTemplate = __QUASI_CORE_TEMPLATES__ . '/LoginModule.tpl.php';
  90. $this->objAccount =& IndexPage::$objAccount;
  91. //if not logged in, set up the login fields
  92. if( ! $this->objAccount )
  93. {
  94. $this->txtUsername = new QTextBox($this, 'username');
  95. $this->txtUsername->Required = true;
  96. $this->txtUsername->TabIndex = 1;
  97. // $this->txtUsername->Name = 'Username: ';
  98. $this->txtUsername->Text = 'username';
  99. $this->txtUsername->AddAction(new QFocusEvent(), new QJavaScriptAction( 'clearText(this)'));
  100. $this->txtPassword = new QTextBox($this, 'password');
  101. $this->txtPassword->TextMode = QTextMode::Password;
  102. $this->txtPassword->Required = true;
  103. $this->txtPassword->Text = 'password';
  104. $this->txtPassword->AddAction(new QFocusEvent(), new QJavaScriptAction( 'clearText(this)'));
  105. if(IndexPage::$blnAjaxOk)
  106. $this->txtPassword->AddAction(new QEnterKeyEvent(), new QAjaxControlAction($this, 'btnLogin_Click'));
  107. else
  108. $this->txtPassword->AddAction(new QEnterKeyEvent(), new QServerControlAction($this, 'btnLogin_Click'));
  109. $this->txtPassword->CausesValidation = $this;
  110. $this->txtPassword->TabIndex = 2;
  111. // Create Buttons and Actions
  112. $this->btnLogin = new QButton($this,"LoginButton");
  113. $this->btnLogin->Text = Quasi::Translate('Login');
  114. if(IndexPage::$blnAjaxOk)
  115. $this->btnLogin->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnLogin_Click'));
  116. else
  117. $this->btnLogin->AddAction(new QClickEvent(), new QServerControlAction($this, 'btnLogin_Click'));
  118. if(IndexPage::$blnAjaxOk)
  119. $this->btnLogin->AddAction(new QEnterKeyEvent(), new QAjaxControlAction($this, 'btnLogin_Click'));
  120. else
  121. $this->btnLogin->AddAction(new QEnterKeyEvent(), new QServerControlAction($this, 'btnLogin_Click'));
  122. $this->btnLogin->CausesValidation = $this;
  123. $this->btnLogin->TabIndex = 3;
  124. }//otherwise, set up various logged in info ..
  125. else
  126. {
  127. $this->btnLogout = new QButton($this, "LogoutButton");
  128. $this->btnLogout->Text = Quasi::Translate('Logout');
  129. /* eh, this may be causing a bug
  130. if(IndexPage::$blnAjaxOk)
  131. $this->btnLogout->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnLogout_Click'));
  132. else
  133. */
  134. $this->btnLogout->AddAction(new QClickEvent(), new QServerControlAction($this, 'btnLogout_Click'));
  135. $this->lblSignedInAs_Create();
  136. $this->lblLoginSpan_Create();
  137. $this->lblShoppingCartStatus_Create();
  138. }
  139. }
  140. /**
  141. * Quasi supports importing user accounts from OsCommerce - this function checks an OSC style
  142. * encryption.
  143. *@param string strInput - the input string
  144. *@param string strStored - the stored encrypted string
  145. *@return bool true if valid
  146. */
  147. private function checkOSCPassword($strInput, $strStored)
  148. {
  149. // split apart the hash / salt
  150. $aryStack = explode(':', $strStored);
  151. if (sizeof($aryStack) != 2)
  152. return false;
  153. if (md5($aryStack[1] . $strInput) == $aryStack[0])
  154. return true;
  155. return false;
  156. }
  157. /**
  158. * Quasi native encrypted password check.
  159. * @todo - improve the encryption scheme
  160. *@param string strInput - the input string
  161. *@param string strStored - the stored encrypted string
  162. *@return bool true if valid
  163. */
  164. private function checkQuasiPassword($strInput, $strStored)
  165. {
  166. if( sha1( $strInput ) == $strStored)
  167. return true;
  168. return false;
  169. }
  170. /**
  171. * This Function is called when the login button is clicked - it checks the login data
  172. * returning false if it fails. Failure results in redrawing the form with error messages.
  173. */
  174. public function Validate()
  175. {
  176. $blnPassed = false;
  177. $blnValidPassword = true;
  178. $this->objAccount = Account::LoadByUsername($this->txtUsername->Text);
  179. if(! $this->objAccount || $this->objAccount->StatusId != AccountStatusType::Active)
  180. {
  181. $this->objAccount = null;
  182. unset($_SESSION["AccountLogin"]);
  183. $this->txtUsername->Warning = Quasi::Translate('Incorrect');
  184. return $blnPassed;
  185. }
  186. $strInput = $this->txtPassword->Text;
  187. $strStored = $this->objAccount->Password;
  188. if(! $this->objAccount->ValidPassword)
  189. $blnValidPassword = false;
  190. elseif( $this->checkQuasiPassword($strInput, $strStored))
  191. $blnPassed = true;
  192. elseif($this->checkOSCPassword($strInput, $strStored))
  193. $blnPassed = true;
  194. if( ! $blnPassed)
  195. {
  196. $this->objAccount = null;
  197. unset($_SESSION["AccountLogin"]);
  198. if($blnValidPassword)
  199. $this->txtPassword->Warning = Quasi::Translate('Incorrect');
  200. else
  201. $this->txtPassword->Warning .= Quasi::Translate('Not Valid');
  202. }
  203. return $blnPassed;
  204. }
  205. /**
  206. * This function sets the SESSION['AccountLogin'] and updates the login state and count.
  207. * If this is a onetime password, it sets valid_password false - the user must save a new
  208. * password to reset this.
  209. */
  210. public function btnLogin_Click($strFormId, $strControlId, $strParameter)
  211. {
  212. $this->objAccount->Online = true;
  213. $this->objAccount->LoginCount += 1;
  214. $this->objAccount->LastLogin = date("Y-m-d H:i:s");
  215. $this->objAccount->UpdateLoginState();
  216. $_SESSION["AccountLogin"] = serialize($this->objAccount);
  217. if( $this->objAccount->OnetimePassword )
  218. {
  219. $this->objAccount->ValidPassword = false;
  220. $this->objAccount->Save();
  221. Quasi::Redirect( __QUASI_SUBDIRECTORY__ . '/index.php/AccountHome/Settings/Password' );
  222. }
  223. Quasi::Redirect( __QUASI_SUBDIRECTORY__ . LOGIN_REDIRECT );
  224. /* header( 'Location: http://' . $_SERVER['SERVER_NAME'] . __QUASI_SUBDIRECTORY__ . LOGIN_REDIRECT );
  225. exit;*/
  226. }
  227. /**
  228. * This function logs out the user and unsets the session variable for the account.
  229. * @todo - FIXME error on logout if session has timed out already ..
  230. */
  231. public function btnLogout_Click($strFormId, $strControlId, $strParameter)
  232. {
  233. if( session_is_registered( 'AccountLogin' ) )
  234. {
  235. $this->objAccount = unserialize($_SESSION["AccountLogin"]);
  236. if($this->objAccount instanceof Account)
  237. {
  238. $this->objAccount->Online = false;
  239. $this->objAccount->UpdateLoginState();
  240. $this->objAccount = null;
  241. }
  242. session_unregister( 'AccountLogin' );
  243. }
  244. Quasi::Redirect(__QUASI_SUBDIRECTORY__ . '/index.php/Home');
  245. }
  246. /**
  247. * This a little label indicating who the user is logged it as
  248. */
  249. public function lblSignedInAs_Create()
  250. {
  251. $this->lblSignedInAs = new QLabel($this, "SignedInAs");
  252. $this->lblSignedInAs->HtmlEntities = false;
  253. if($this->Account instanceof Account )
  254. {
  255. $txt = Quasi::Translate('You are signed in as') . ':<br>&nbsp;&nbsp;<strong>' . $this->objAccount->Username . '</strong>';
  256. $this->lblSignedInAs->Text = $txt;
  257. }
  258. }
  259. /**
  260. * Here we create a little label indicating how long the user has been logged in.
  261. * Note: This is only updated on a full page load.
  262. */
  263. public function lblLoginSpan_Create()
  264. {
  265. $this->lblLoginSpan = new QLabel($this, "LoginSpan");
  266. if($this->Account instanceof Account )
  267. {
  268. // $dttLoginTime = QDateTime::FromTimestamp(strtotime( $this->Account->LastLogin) );
  269. $dttLoginTime = new QDateTime( $this->Account->LastLogin );
  270. // exit(var_dump($dttLoginTime));
  271. $txt = 'Logged in ' . $dttLoginTime->Age;
  272. $this->lblLoginSpan->Text = $txt;
  273. }
  274. }
  275. /**
  276. * This creates a small display of the shopping cart status
  277. * Note: This is only updated on a full page load.
  278. */
  279. public function lblShoppingCartStatus_Create()
  280. {
  281. $this->lblShoppingCartStatus = new QLabel($this, 'ShoppingCartStatus');
  282. $this->lblShoppingCartStatus->HtmlEntities = false;
  283. $strText = '<img src="' . __QUASI_CORE_IMAGES__ . '/default_shopping_cart_icon.gif"> &nbsp;&nbsp;';
  284. if(IndexPage::$objShoppingCart instanceof ShoppingCart )
  285. {
  286. // $strText .= Quasi::Translate('You have ');
  287. $intItemCount = ShoppingCartItem::CountByShoppingCartId(IndexPage::$objShoppingCart->Id);
  288. if($intItemCount > 0)
  289. {
  290. $strText .= $intItemCount . ' ';
  291. if($intItemCount > 1)
  292. $strText .= Quasi::Translate('items') . ' ';
  293. else
  294. $strText .= Quasi::Translate('item') . ' ';
  295. }
  296. else
  297. $strText .= Quasi::Translate('No items') . ' ';
  298. $strText .= Quasi::Translate('in your') . ' ';
  299. if($intItemCount)
  300. $strText .= '<a href="http://' . Quasi::$ServerName . __QUASI_SUBDIRECTORY__ . '/index.php/ShoppingCart' . '">'
  301. . Quasi::Translate('cart') . '</a>' . '.';
  302. else
  303. $strText .= Quasi::Translate('cart') . '.';
  304. }
  305. $this->lblShoppingCartStatus->Text = $strText;
  306. }
  307. public function __get($strName)
  308. {
  309. switch ($strName)
  310. {
  311. case 'Account':
  312. return $this->objAccount ;
  313. default:
  314. try {
  315. return parent::__get($strName);
  316. } catch (QCallerException $objExc) {
  317. $objExc->IncrementOffset();
  318. throw $objExc;
  319. }
  320. }
  321. }
  322. public function __set($strName, $mixValue)
  323. {
  324. switch ($strName)
  325. {
  326. case 'Account':
  327. try {
  328. return ($this->objAccount = QType::Cast($mixValue, 'Account' ));
  329. } catch (QInvalidCastException $objExc) {
  330. $objExc->IncrementOffset();
  331. throw $objExc;
  332. }
  333. default:
  334. try {
  335. return (parent::__set($strName, $mixValue));
  336. } catch (QCallerException $objExc) {
  337. $objExc->IncrementOffset();
  338. throw $objExc;
  339. }
  340. }
  341. }
  342. }//end class
  343. }//end define
  344. ?>