PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/kernel/user/register.php

https://github.com/granitegreg/ezpublish
PHP | 331 lines | 247 code | 45 blank | 39 comment | 41 complexity | fef6127cac2fff008c45fa869350cb19 MD5 | raw file
Possible License(s): 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-2011 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. if ( $redirectNumber == '3' )
  48. {
  49. $tpl->setVariable( 'content_attributes', false );
  50. $Result = array();
  51. $Result['content'] = $tpl->fetch( 'design:user/register.tpl' );
  52. $Result['path'] = array( array( 'url' => false,
  53. 'text' => ezpI18n::tr( 'kernel/user', 'User' ) ),
  54. array( 'url' => false,
  55. 'text' => ezpI18n::tr( 'kernel/user', 'Register' ) ) );
  56. return $Result;
  57. }
  58. $db = eZDB::instance();
  59. $db->begin();
  60. // Create new user object if user is not logged in
  61. if ( !$http->hasSessionVariable( "RegisterUserID" ) )
  62. {
  63. // flag if user client supports cookies and if we should do redirect
  64. $userClientValidates = true;
  65. $doValidationRedirect = false;
  66. if ( !eZSession::userHasSessionCookie() )
  67. {
  68. if ( $redirectNumber == '2' )
  69. $userClientValidates = false;
  70. else
  71. $doValidationRedirect = true;
  72. }
  73. if ( $doValidationRedirect )
  74. {
  75. $db->rollback();
  76. return $Module->redirectTo( '/user/register/2' );
  77. }
  78. else if ( !$userClientValidates )
  79. {
  80. $db->rollback();
  81. $tpl->setVariable( 'user_has_cookie', eZSession::userHasSessionCookie(), 'User' );
  82. $tpl->setVariable( 'user_session_validates', true, 'User' );
  83. $Result = array();
  84. $Result['content'] = $tpl->fetch( 'design:user/register_user_not_valid.tpl' );
  85. $Result['path'] = array( array( 'url' => false,
  86. 'text' => ezpI18n::tr( 'kernel/user', 'User' ) ),
  87. array( 'url' => false,
  88. 'text' => ezpI18n::tr( 'kernel/user', 'Register' ) ) );
  89. return $Result;
  90. }
  91. // else create user object
  92. if ( $http->hasSessionVariable( 'StartedRegistration' ) )
  93. {
  94. eZDebug::writeWarning( 'Cancel module run to protect against multiple form submits', 'user/register' );
  95. $http->removeSessionVariable( "RegisterUserID" );
  96. $http->removeSessionVariable( 'StartedRegistration' );
  97. $db->commit();
  98. return eZModule::HOOK_STATUS_CANCEL_RUN;
  99. }
  100. else if ( $http->hasPostVariable( 'PublishButton' ) or $http->hasPostVariable( 'CancelButton' ) )
  101. {
  102. $http->setSessionVariable( 'StartedRegistration', 1 );
  103. }
  104. $ini = eZINI::instance();
  105. $errMsg = '';
  106. $checkErrNodeId = false;
  107. $defaultUserPlacement = (int)$ini->variable( "UserSettings", "DefaultUserPlacement" );
  108. $sql = "SELECT count(*) as count FROM ezcontentobject_tree WHERE node_id = $defaultUserPlacement";
  109. $rows = $db->arrayQuery( $sql );
  110. $count = $rows[0]['count'];
  111. if ( $count < 1 )
  112. {
  113. $errMsg = ezpI18n::tr( 'design/standard/user', 'The node (%1) specified in [UserSettings].DefaultUserPlacement setting in site.ini does not exist!', null, array( $defaultUserPlacement ) );
  114. $checkErrNodeId = true;
  115. eZDebug::writeError( "$errMsg" );
  116. $tpl->setVariable( 'errMsg', $errMsg );
  117. $tpl->setVariable( 'checkErrNodeId', $checkErrNodeId );
  118. }
  119. $userClassID = $ini->variable( "UserSettings", "UserClassID" );
  120. $class = eZContentClass::fetch( $userClassID );
  121. $userCreatorID = $ini->variable( "UserSettings", "UserCreatorID" );
  122. $defaultSectionID = $ini->variable( "UserSettings", "DefaultSectionID" );
  123. // Create object by user 14 in section 1
  124. $contentObject = $class->instantiate( $userCreatorID, $defaultSectionID );
  125. $objectID = $contentObject->attribute( 'id' );
  126. // Store the ID in session variable
  127. $http->setSessionVariable( "RegisterUserID", $objectID );
  128. $userID = $objectID;
  129. $nodeAssignment = eZNodeAssignment::create( array( 'contentobject_id' => $contentObject->attribute( 'id' ),
  130. 'contentobject_version' => 1,
  131. 'parent_node' => $defaultUserPlacement,
  132. 'is_main' => 1 ) );
  133. $nodeAssignment->store();
  134. }
  135. else
  136. {
  137. if ( $http->hasSessionVariable( 'StartedRegistration' ) )
  138. {
  139. eZDebug::writeWarning( 'Cancel module run to protect against multiple form submits', 'user/register' );
  140. $http->removeSessionVariable( "RegisterUserID" );
  141. $http->removeSessionVariable( 'StartedRegistration' );
  142. $db->commit();
  143. return eZModule::HOOK_STATUS_CANCEL_RUN;
  144. }
  145. $userID = $http->sessionVariable( "RegisterUserID" );
  146. }
  147. $Params['ObjectID'] = $userID;
  148. $Module->addHook( 'post_publish', 'registerSearchObject', 1, false );
  149. if ( !function_exists( 'checkContentActions' ) )
  150. {
  151. function checkContentActions( $module, $class, $object, $version, $contentObjectAttributes, $EditVersion, $EditLanguage )
  152. {
  153. if ( $module->isCurrentAction( 'Cancel' ) )
  154. {
  155. $http = eZHTTPTool::instance();
  156. if ( $http->hasPostVariable( 'RedirectIfDiscarded' ) )
  157. {
  158. eZRedirectManager::redirectTo( $module, $http->postVariable( 'RedirectIfDiscarded' ) );
  159. }
  160. else
  161. {
  162. eZRedirectManager::redirectTo( $module, '/' );
  163. }
  164. $version->removeThis();
  165. $http = eZHTTPTool::instance();
  166. $http->removeSessionVariable( "RegisterUserID" );
  167. $http->removeSessionVariable( 'StartedRegistration' );
  168. return eZModule::HOOK_STATUS_CANCEL_RUN;
  169. }
  170. if ( $module->isCurrentAction( 'Publish' ) )
  171. {
  172. $userID = $object->attribute( 'id' );
  173. $operationResult = eZOperationHandler::execute( 'user', 'register', array( 'user_id' => $userID ) );
  174. // send feedback
  175. $ini = eZINI::instance();
  176. $tpl = eZTemplate::factory();
  177. $hostname = eZSys::hostname();
  178. $user = eZUser::fetch( $userID );
  179. $feedbackTypes = $ini->variableArray( 'UserSettings', 'RegistrationFeedback' );
  180. foreach ( $feedbackTypes as $feedbackType )
  181. {
  182. switch ( $feedbackType )
  183. {
  184. case 'email':
  185. {
  186. // send feedback with the default email type
  187. $mail = new eZMail();
  188. $tpl->resetVariables();
  189. $tpl->setVariable( 'user', $user );
  190. $tpl->setVariable( 'object', $object );
  191. $tpl->setVariable( 'hostname', $hostname );
  192. $templateResult = $tpl->fetch( 'design:user/registrationfeedback.tpl' );
  193. if ( $tpl->hasVariable( 'content_type' ) )
  194. $mail->setContentType( $tpl->variable( 'content_type' ) );
  195. $emailSender = $ini->variable( 'MailSettings', 'EmailSender' );
  196. if ( $tpl->hasVariable( 'email_sender' ) )
  197. $emailSender = $tpl->variable( 'email_sender' );
  198. else if ( !$emailSender )
  199. $emailSender = $ini->variable( 'MailSettings', 'AdminEmail' );
  200. $feedbackReceiver = $ini->variable( 'UserSettings', 'RegistrationEmail' );
  201. if ( $tpl->hasVariable( 'email_receiver' ) )
  202. $feedbackReceiver = $tpl->variable( 'email_receiver' );
  203. else if ( !$feedbackReceiver )
  204. $feedbackReceiver = $ini->variable( 'MailSettings', 'AdminEmail' );
  205. if ( $tpl->hasVariable( 'subject' ) )
  206. $subject = $tpl->variable( 'subject' );
  207. else
  208. $subject = ezpI18n::tr( 'kernel/user/register', 'New user registered' );
  209. $mail->setSender( $emailSender );
  210. $mail->setReceiver( $feedbackReceiver );
  211. $mail->setSubject( $subject );
  212. $mail->setBody( $templateResult );
  213. $mailResult = eZMailTransport::send( $mail );
  214. } break;
  215. default:
  216. {
  217. $registrationFeedbackClass = false;
  218. // load custom registration feedback settings
  219. if ( $ini->hasGroup( 'RegistrationFeedback_' . $feedbackType ) )
  220. {
  221. if ( $ini->hasVariable( 'RegistrationFeedback_' . $feedbackType, 'File' ) )
  222. include_once( $ini->variable( 'RegistrationFeedback_' . $feedbackType, 'File' ) );
  223. $registrationFeedbackClass = $ini->variable( 'RegistrationFeedback_' . $feedbackType, 'Class' );
  224. }
  225. // try to call the registration feedback class with function registrationFeedback
  226. if ( $registrationFeedbackClass && method_exists( $registrationFeedbackClass, 'registrationFeedback' ) )
  227. call_user_func( array( $registrationFeedbackClass, 'registrationFeedback' ), $user, $tpl, $object, $hostname );
  228. else
  229. eZDebug::writeWarning( "Unknown feedback type '$feedbackType'", 'user/register' );
  230. }
  231. }
  232. }
  233. $http = eZHTTPTool::instance();
  234. $http->removeSessionVariable( "GeneratedPassword" );
  235. $http->removeSessionVariable( "RegisterUserID" );
  236. $http->removeSessionVariable( 'StartedRegistration' );
  237. // if everything is passed, login the user
  238. if( $operationResult['status'] === eZModuleOperationInfo::STATUS_CONTINUE )
  239. {
  240. $user->loginCurrent();
  241. }
  242. // check for redirectionvariable
  243. if( $operationResult['status'] === eZModuleOperationInfo::STATUS_CONTINUE ||
  244. $operationResult['status'] === eZModuleOperationInfo::STATUS_HALTED )
  245. {
  246. if ( $http->hasSessionVariable( 'RedirectAfterUserRegister' ) )
  247. {
  248. $module->redirectTo( $http->sessionVariable( 'RedirectAfterUserRegister' ) );
  249. $http->removeSessionVariable( 'RedirectAfterUserRegister' );
  250. }
  251. else if ( $http->hasPostVariable( 'RedirectAfterUserRegister' ) )
  252. {
  253. $module->redirectTo( $http->postVariable( 'RedirectAfterUserRegister' ) );
  254. }
  255. else
  256. {
  257. $module->redirectTo( '/user/success/' );
  258. }
  259. }
  260. else
  261. {
  262. eZDebug::writeError( 'Unexpected operation status: ' . $operationResult['status'], 'user/register' );
  263. // @todo: finish the failure code
  264. $module->redirectTo( '/user/register/5' );
  265. }
  266. }
  267. }
  268. }
  269. $Module->addHook( 'action_check', 'checkContentActions' );
  270. $OmitSectionSetting = true;
  271. $includeResult = include( 'kernel/content/attribute_edit.php' );
  272. $db->commit();
  273. if ( $includeResult != 1 )
  274. {
  275. return $includeResult;
  276. }
  277. $ini = eZINI::instance();
  278. if ( $ini->variable( 'SiteSettings', 'LoginPage' ) == 'custom' )
  279. {
  280. $Result['pagelayout'] = 'loginpagelayout.tpl';
  281. }
  282. $Result['path'] = array( array( 'url' => false,
  283. 'text' => ezpI18n::tr( 'kernel/user', 'User' ) ),
  284. array( 'url' => false,
  285. 'text' => ezpI18n::tr( 'kernel/user', 'Register' ) ) );
  286. ?>