PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/user/register.php

https://github.com/aurelienRT1/ezpublish
PHP | 404 lines | 298 code | 61 blank | 45 comment | 55 complexity | de9dd596b507fbd9931de9478d5d220e MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. //
  3. // Created on: <01-Aug-2002 09:58:09 bf>
  4. //
  5. // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  6. // SOFTWARE NAME: eZ Publish
  7. // SOFTWARE RELEASE: 4.1.x
  8. // COPYRIGHT NOTICE: Copyright (C) 1999-2010 eZ Systems AS
  9. // SOFTWARE LICENSE: GNU General Public License v2.0
  10. // NOTICE: >
  11. // This program is free software; you can redistribute it and/or
  12. // modify it under the terms of version 2.0 of the GNU General
  13. // Public License as published by the Free Software Foundation.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of version 2.0 of the GNU General
  21. // Public License along with this program; if not, write to the Free
  22. // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  23. // MA 02110-1301, USA.
  24. //
  25. //
  26. // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  27. //
  28. $http = eZHTTPTool::instance();
  29. $Module = $Params['Module'];
  30. $redirectNumber = $Params['redirect_number'];
  31. if ( isset( $Params['UserParameters'] ) )
  32. {
  33. $UserParameters = $Params['UserParameters'];
  34. }
  35. else
  36. {
  37. $UserParameters = array();
  38. }
  39. $viewParameters = array();
  40. $viewParameters = array_merge( $viewParameters, $UserParameters );
  41. $Params['TemplateName'] = "design:user/register.tpl";
  42. $EditVersion = 1;
  43. $tpl = eZTemplate::factory();
  44. $tpl->setVariable( 'view_parameters', $viewParameters );
  45. $Params['TemplateObject'] = $tpl;
  46. // $http->removeSessionVariable( "RegisterUserID" );
  47. $db = eZDB::instance();
  48. $db->begin();
  49. // Create new user object if user is not logged in
  50. if ( !$http->hasSessionVariable( "RegisterUserID" ) )
  51. {
  52. // flag if user client supports cookies and if we should do redirect
  53. $userClientValidates = true;
  54. $doValidationRedirect = false;
  55. if ( !eZSession::userHasSessionCookie() )
  56. {
  57. if ( $redirectNumber == '2' )
  58. $userClientValidates = false;
  59. else
  60. $doValidationRedirect = true;
  61. }
  62. if ( $doValidationRedirect )
  63. {
  64. $db->rollback();
  65. return $Module->redirectTo( '/user/register/2' );
  66. }
  67. else if ( !$userClientValidates )
  68. {
  69. $db->rollback();
  70. $tpl->setVariable( 'user_has_cookie', eZSession::userHasSessionCookie(), 'User' );
  71. $tpl->setVariable( 'user_session_validates', true, 'User' );
  72. $Result = array();
  73. $Result['content'] = $tpl->fetch( 'design:user/register_user_not_valid.tpl' );
  74. $Result['path'] = array( array( 'url' => false,
  75. 'text' => ezpI18n::tr( 'kernel/user', 'User' ) ),
  76. array( 'url' => false,
  77. 'text' => ezpI18n::tr( 'kernel/user', 'Register' ) ) );
  78. return $Result;
  79. }
  80. // else create user object
  81. if ( $http->hasSessionVariable( 'StartedRegistration' ) )
  82. {
  83. eZDebug::writeWarning( 'Cancel module run to protect against multiple form submits', 'user/register' );
  84. $http->removeSessionVariable( "RegisterUserID" );
  85. $http->removeSessionVariable( 'StartedRegistration' );
  86. $db->commit();
  87. return eZModule::HOOK_STATUS_CANCEL_RUN;
  88. }
  89. else if ( $http->hasPostVariable( 'PublishButton' ) or $http->hasPostVariable( 'CancelButton' ) )
  90. {
  91. $http->setSessionVariable( 'StartedRegistration', 1 );
  92. }
  93. $ini = eZINI::instance();
  94. $errMsg = '';
  95. $checkErrNodeId = false;
  96. $defaultUserPlacement = (int)$ini->variable( "UserSettings", "DefaultUserPlacement" );
  97. $sql = "SELECT count(*) as count FROM ezcontentobject_tree WHERE node_id = $defaultUserPlacement";
  98. $rows = $db->arrayQuery( $sql );
  99. $count = $rows[0]['count'];
  100. if ( $count < 1 )
  101. {
  102. $errMsg = ezpI18n::tr( 'design/standard/user', 'The node (%1) specified in [UserSettings].DefaultUserPlacement setting in site.ini does not exist!', null, array( $defaultUserPlacement ) );
  103. $checkErrNodeId = true;
  104. eZDebug::writeError( "$errMsg" );
  105. $tpl->setVariable( 'errMsg', $errMsg );
  106. $tpl->setVariable( 'checkErrNodeId', $checkErrNodeId );
  107. }
  108. $userClassID = $ini->variable( "UserSettings", "UserClassID" );
  109. $class = eZContentClass::fetch( $userClassID );
  110. $userCreatorID = $ini->variable( "UserSettings", "UserCreatorID" );
  111. $defaultSectionID = $ini->variable( "UserSettings", "DefaultSectionID" );
  112. // Create object by user 14 in section 1
  113. $contentObject = $class->instantiate( $userCreatorID, $defaultSectionID );
  114. $objectID = $contentObject->attribute( 'id' );
  115. // Store the ID in session variable
  116. $http->setSessionVariable( "RegisterUserID", $objectID );
  117. $userID = $objectID;
  118. $nodeAssignment = eZNodeAssignment::create( array( 'contentobject_id' => $contentObject->attribute( 'id' ),
  119. 'contentobject_version' => 1,
  120. 'parent_node' => $defaultUserPlacement,
  121. 'is_main' => 1 ) );
  122. $nodeAssignment->store();
  123. }
  124. else
  125. {
  126. if ( $http->hasSessionVariable( 'StartedRegistration' ) )
  127. {
  128. eZDebug::writeWarning( 'Cancel module run to protect against multiple form submits', 'user/register' );
  129. $http->removeSessionVariable( "RegisterUserID" );
  130. $http->removeSessionVariable( 'StartedRegistration' );
  131. $db->commit();
  132. return eZModule::HOOK_STATUS_CANCEL_RUN;
  133. }
  134. $userID = $http->sessionVariable( "RegisterUserID" );
  135. }
  136. $Params['ObjectID'] = $userID;
  137. $Module->addHook( 'post_publish', 'registerSearchObject', 1, false );
  138. if ( !function_exists( 'checkContentActions' ) )
  139. {
  140. function checkContentActions( $module, $class, $object, $version, $contentObjectAttributes, $EditVersion, $EditLanguage )
  141. {
  142. if ( $module->isCurrentAction( 'Cancel' ) )
  143. {
  144. $http = eZHTTPTool::instance();
  145. if ( $http->hasPostVariable( 'RedirectIfDiscarded' ) )
  146. {
  147. eZRedirectManager::redirectTo( $module, $http->postVariable( 'RedirectIfDiscarded' ) );
  148. }
  149. else
  150. {
  151. eZRedirectManager::redirectTo( $module, '/' );
  152. }
  153. $version->removeThis();
  154. $http = eZHTTPTool::instance();
  155. $http->removeSessionVariable( "RegisterUserID" );
  156. $http->removeSessionVariable( 'StartedRegistration' );
  157. return eZModule::HOOK_STATUS_CANCEL_RUN;
  158. }
  159. if ( $module->isCurrentAction( 'Publish' ) )
  160. {
  161. $http = eZHTTPTool::instance();
  162. $user = eZUser::currentUser();
  163. $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $object->attribute( 'id' ),
  164. 'version' => $version->attribute( 'version') ) );
  165. $object = eZContentObject::fetch( $object->attribute( 'id' ) );
  166. // Check if user should be enabled and logged in
  167. unset($user);
  168. $user = eZUser::fetch( $object->attribute( 'id' ) );
  169. $user->loginCurrent();
  170. $receiver = $user->attribute( 'email' );
  171. $mail = new eZMail();
  172. if ( !$mail->validate( $receiver ) )
  173. {
  174. }
  175. $ini = eZINI::instance();
  176. $tpl = eZTemplate::factory();
  177. $tpl->setVariable( 'user', $user );
  178. $tpl->setVariable( 'object', $object );
  179. $hostname = eZSys::hostname();
  180. $tpl->setVariable( 'hostname', $hostname );
  181. $password = $http->sessionVariable( "GeneratedPassword" );
  182. $tpl->setVariable( 'password', $password );
  183. // Check whether account activation is required.
  184. $verifyUserType = $ini->variable( 'UserSettings', 'VerifyUserType' );
  185. $sendUserMail = !!$verifyUserType;
  186. // For compatibility with old setting
  187. if ( $verifyUserType === 'email'
  188. && $ini->hasVariable( 'UserSettings', 'VerifyUserEmail' )
  189. && $ini->variable( 'UserSettings', 'VerifyUserEmail' ) !== 'enabled' )
  190. {
  191. $verifyUserType = false;
  192. }
  193. if ( $verifyUserType === 'email' ) // and if it is email type
  194. {
  195. // Disable user account and send verification mail to the user
  196. $userID = $object->attribute( 'id' );
  197. // Create enable account hash and send it to the newly registered user
  198. $hash = md5( mt_rand() . time() . $userID );
  199. if ( eZOperationHandler::operationIsAvailable( 'user_activation' ) )
  200. {
  201. $operationResult = eZOperationHandler::execute( 'user',
  202. 'activation', array( 'user_id' => $userID,
  203. 'user_hash' => $hash,
  204. 'is_enabled' => false ) );
  205. }
  206. else
  207. {
  208. eZUserOperationCollection::activation( $userID, $hash, false );
  209. }
  210. // Log out current user
  211. eZUser::logoutCurrent();
  212. $tpl->setVariable( 'hash', $hash );
  213. $sendUserMail = true;
  214. }
  215. else if ( $verifyUserType )// custom account activation
  216. {
  217. $verifyUserTypeClass = false;
  218. // load custom verify user settings
  219. if ( $ini->hasGroup( 'VerifyUserType_' . $verifyUserType ) )
  220. {
  221. if ( $ini->hasVariable( 'VerifyUserType_' . $verifyUserType, 'File' ) )
  222. include_once( $ini->variable( 'VerifyUserType_' . $verifyUserType, 'File' ) );
  223. $verifyUserTypeClass = $ini->variable( 'VerifyUserType_' . $verifyUserType, 'Class' );
  224. }
  225. // try to call the verify user class with function verifyUser
  226. if ( $verifyUserTypeClass && method_exists( $verifyUserTypeClass, 'verifyUser' ) )
  227. $sendUserMail = call_user_func( array( $verifyUserTypeClass, 'verifyUser' ), $user, $tpl );
  228. else
  229. eZDebug::writeWarning( "Unknown VerifyUserType '$verifyUserType'", 'user/register' );
  230. }
  231. // send verification mail to user if email type or custum verify user type returned true
  232. if ( $sendUserMail )
  233. {
  234. $templateResult = $tpl->fetch( 'design:user/registrationinfo.tpl' );
  235. if ( $tpl->hasVariable( 'content_type' ) )
  236. $mail->setContentType( $tpl->variable( 'content_type' ) );
  237. $emailSender = $ini->variable( 'MailSettings', 'EmailSender' );
  238. if ( $tpl->hasVariable( 'email_sender' ) )
  239. $emailSender = $tpl->variable( 'email_sender' );
  240. else if ( !$emailSender )
  241. $emailSender = $ini->variable( 'MailSettings', 'AdminEmail' );
  242. if ( $tpl->hasVariable( 'subject' ) )
  243. $subject = $tpl->variable( 'subject' );
  244. else
  245. $subject = ezpI18n::tr( 'kernel/user/register', 'Registration info' );
  246. $mail->setSender( $emailSender );
  247. $mail->setReceiver( $receiver );
  248. $mail->setSubject( $subject );
  249. $mail->setBody( $templateResult );
  250. $mailResult = eZMailTransport::send( $mail );
  251. }
  252. $feedbackTypes = $ini->variableArray( 'UserSettings', 'RegistrationFeedback' );
  253. foreach ( $feedbackTypes as $feedbackType )
  254. {
  255. switch ( $feedbackType )
  256. {
  257. case 'email':
  258. {
  259. // send feedback with the default email type
  260. $mail = new eZMail();
  261. $tpl->resetVariables();
  262. $tpl->setVariable( 'user', $user );
  263. $tpl->setVariable( 'object', $object );
  264. $tpl->setVariable( 'hostname', $hostname );
  265. $templateResult = $tpl->fetch( 'design:user/registrationfeedback.tpl' );
  266. if ( $tpl->hasVariable( 'content_type' ) )
  267. $mail->setContentType( $tpl->variable( 'content_type' ) );
  268. $emailSender = $ini->variable( 'MailSettings', 'EmailSender' );
  269. if ( $tpl->hasVariable( 'email_sender' ) )
  270. $emailSender = $tpl->variable( 'email_sender' );
  271. else if ( !$emailSender )
  272. $emailSender = $ini->variable( 'MailSettings', 'AdminEmail' );
  273. $feedbackReceiver = $ini->variable( 'UserSettings', 'RegistrationEmail' );
  274. if ( $tpl->hasVariable( 'email_receiver' ) )
  275. $feedbackReceiver = $tpl->variable( 'email_receiver' );
  276. else if ( !$feedbackReceiver )
  277. $feedbackReceiver = $ini->variable( 'MailSettings', 'AdminEmail' );
  278. if ( $tpl->hasVariable( 'subject' ) )
  279. $subject = $tpl->variable( 'subject' );
  280. else
  281. $subject = ezpI18n::tr( 'kernel/user/register', 'New user registered' );
  282. $mail->setSender( $emailSender );
  283. $mail->setReceiver( $feedbackReceiver );
  284. $mail->setSubject( $subject );
  285. $mail->setBody( $templateResult );
  286. $mailResult = eZMailTransport::send( $mail );
  287. } break;
  288. default:
  289. {
  290. $registrationFeedbackClass = false;
  291. // load custom registration feedback settings
  292. if ( $ini->hasGroup( 'RegistrationFeedback_' . $feedbackType ) )
  293. {
  294. if ( $ini->hasVariable( 'RegistrationFeedback_' . $feedbackType, 'File' ) )
  295. include_once( $ini->variable( 'RegistrationFeedback_' . $feedbackType, 'File' ) );
  296. $registrationFeedbackClass = $ini->variable( 'RegistrationFeedback_' . $feedbackType, 'Class' );
  297. }
  298. // try to call the registration feedback class with function registrationFeedback
  299. if ( $registrationFeedbackClass && method_exists( $registrationFeedbackClass, 'registrationFeedback' ) )
  300. call_user_func( array( $registrationFeedbackClass, 'registrationFeedback' ), $user, $tpl, $object, $hostname );
  301. else
  302. eZDebug::writeWarning( "Unknown feedback type '$feedbackType'", 'user/register' );
  303. }
  304. }
  305. }
  306. $http->removeSessionVariable( "GeneratedPassword" );
  307. $http->removeSessionVariable( "RegisterUserID" );
  308. $http->removeSessionVariable( 'StartedRegistration' );
  309. // check for redirectionvariable
  310. if ( $http->hasSessionVariable( 'RedirectAfterUserRegister' ) )
  311. {
  312. $module->redirectTo( $http->sessionVariable( 'RedirectAfterUserRegister' ) );
  313. $http->removeSessionVariable( 'RedirectAfterUserRegister' );
  314. }
  315. else if ( $http->hasPostVariable( 'RedirectAfterUserRegister' ) )
  316. {
  317. $module->redirectTo( $http->postVariable( 'RedirectAfterUserRegister' ) );
  318. }
  319. else
  320. {
  321. $module->redirectTo( '/user/success/' );
  322. }
  323. }
  324. }
  325. }
  326. $Module->addHook( 'action_check', 'checkContentActions' );
  327. $OmitSectionSetting = true;
  328. $includeResult = include( 'kernel/content/attribute_edit.php' );
  329. $db->commit();
  330. if ( $includeResult != 1 )
  331. {
  332. return $includeResult;
  333. }
  334. $ini = eZINI::instance();
  335. if ( $ini->variable( 'SiteSettings', 'LoginPage' ) == 'custom' )
  336. {
  337. $Result['pagelayout'] = 'loginpagelayout.tpl';
  338. }
  339. $Result['path'] = array( array( 'url' => false,
  340. 'text' => ezpI18n::tr( 'kernel/user', 'User' ) ),
  341. array( 'url' => false,
  342. 'text' => ezpI18n::tr( 'kernel/user', 'Register' ) ) );
  343. ?>