Yii フレームワークは初めてです。既存の Yii アプリケーションの 1 つで問題が発生しました。
そこに「パスワードを忘れた」機能があります。ユーザー名とセキュリティの質問の答えを入力するように機能しています。両方が正しい場合、システムはパスワードをリセットするためのリンクをユーザーの電子メールに送信します。以下は機能です:
public function actionCheckAnswer()
{
if(IS_AJAX && isset($_POST['answer']) && isset($_POST['username']))
{
$username=$_POST['username'];
$answer=$_POST['answer'];
$user=User::model()->findByAttributes(array('username'=>$username));
if($user!=null)
{
$realAnswer = $user->secretAnswer;
if(strlen($realAnswer)>0)
{
$profile=Profile::model()->findByAttributes(array('userId'=>$user->id));
if($this->checkAnswerSpam($profile->id))
{
if(strtolower($realAnswer)==strtolower($answer))
{
Activity::log(22, null, $profile->id, -1);
$stamp=Activity::model()->getLogTime(null, $profile->id, -1, 22);
$hash=$profile->id.'_'.sha1($profile->id.$stamp);
$url=Yii::app()->createAbsoluteUrl('site/recover').'/'.$hash;
echo $url;
$this->sendPasswordRecoveryLink($profile->fullName, $profile->email, $url);
//echo '<br />'.CHtml::link($url, $url).'<br />';
echo 'Correct! A link to your password recovery form has been sent to your e-mail. The link expires in 1 hour.<br />If you don\'t receive a mail, please check your spam folder.';
} else {
Activity::log(24, null, $profile->id, -1);
echo 'Sorry, that answer is not correct.';
}
}
} else {
echo 'Sorry, you have not set a secret question answer.';
}
} else {
echo 'No user "'.$username.'" found.';
}
}
}
現在、この関数はメールを送信しません。トラブルシューティングをActivity::log(22, null, $profile->id, -1);
行ったところ、エラーが発生することがわかりました。この行にコメントを付けると、パスワード リセット リンクが記載されたメールが送信されますが、リンクは常に期限切れです。以下はログ機能です。
public function log($action=0, $trainingId=null, $profileId=null, $piId=null)
{
if($profileId==null) $profileId=Yii::app()->user->profileId;
if($piId==null) $piId=(isset(Yii::app()->user->piId))?Yii::app()->user->piId:0;
$activity=new Activity;
$activity->trainingId=$trainingId;
$activity->profileId=$profileId;
$activity->piId=$piId;
$activity->action=$action;
$activity->save();
}
以下は、有効期限を確認する関数です。
public function getLogTime($trainingId, $profileId, $piId, $action)
{
$all = Activity::model()->findAllByAttributes(array(
'trainingId'=>$trainingId,
'profileId'=>$profileId,
'piId'=>$piId,
'action'=>$action,
));
foreach($all as $single) $return = $single;
return $return->timestamp;
}
public function checkRecoveryHash($hash)
{
$explode=explode('_', $hash);
$stamp=$this->getLogTime(null, $explode[0], -1, 22);
if(strlen($stamp)>0)
{
$time=time();
$stamptime=strtotime($stamp);
$passed=$time-$stamptime;
if($passed < 720*720) //1 hour
return true;
else
return false;
}
}
どの部分をどのように修正すればよいかわかりません。誰が何が悪いのか教えてもらえますか?