SiteController.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. class SiteController extends Controller
  3. {
  4. /**
  5. * Declares class-based actions.
  6. */
  7. public function actions()
  8. {
  9. return array(
  10. // captcha action renders the CAPTCHA image displayed on the contact page
  11. 'captcha'=>array(
  12. 'class'=>'CCaptchaAction',
  13. 'backColor'=>0xFFFFFF,
  14. ),
  15. // page action renders "static" pages stored under 'protected/views/site/pages'
  16. // They can be accessed via: index.php?r=site/page&view=FileName
  17. 'page'=>array(
  18. 'class'=>'CViewAction',
  19. ),
  20. );
  21. }
  22. /**
  23. * This is the default 'index' action that is invoked
  24. * when an action is not explicitly requested by users.
  25. */
  26. public function actionIndex()
  27. {
  28. // renders the view file 'protected/views/site/index.php'
  29. // using the default layout 'protected/views/layouts/main.php'
  30. $this->render('index');
  31. }
  32. /**
  33. * This is the action to handle external exceptions.
  34. */
  35. public function actionError()
  36. {
  37. if($error=Yii::app()->errorHandler->error)
  38. {
  39. if(Yii::app()->request->isAjaxRequest)
  40. echo $error['message'];
  41. else
  42. $this->render('error', $error);
  43. }
  44. }
  45. /**
  46. * Displays the contact page
  47. */
  48. public function actionContact()
  49. {
  50. $model=new ContactForm;
  51. if(isset($_POST['ContactForm']))
  52. {
  53. $model->attributes=$_POST['ContactForm'];
  54. if($model->validate())
  55. {
  56. $name='=?UTF-8?B?'.base64_encode($model->name).'?=';
  57. $subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';
  58. $headers="From: $name <{$model->email}>\r\n".
  59. "Reply-To: {$model->email}\r\n".
  60. "MIME-Version: 1.0\r\n".
  61. "Content-Type: text/plain; charset=UTF-8";
  62. mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers);
  63. Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
  64. $this->refresh();
  65. }
  66. }
  67. $this->render('contact',array('model'=>$model));
  68. }
  69. /**
  70. * Displays the login page
  71. */
  72. public function actionLogin()
  73. {
  74. $model=new LoginForm;
  75. // if it is ajax validation request
  76. if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
  77. {
  78. echo CActiveForm::validate($model);
  79. Yii::app()->end();
  80. }
  81. // collect user input data
  82. if(isset($_POST['LoginForm']))
  83. {
  84. $model->attributes=$_POST['LoginForm'];
  85. // validate user input and redirect to the previous page if valid
  86. if($model->validate() && $model->login())
  87. $this->redirect(Yii::app()->user->returnUrl);
  88. }
  89. // display the login form
  90. $this->render('login',array('model'=>$model));
  91. }
  92. /**
  93. * Logs out the current user and redirect to homepage.
  94. */
  95. public function actionLogout()
  96. {
  97. Yii::app()->user->logout();
  98. $this->redirect(Yii::app()->homeUrl);
  99. }
  100. }