Skip to content

Commit

Permalink
Port the frontend register controller to the Yii framework - by yaxar
Browse files Browse the repository at this point in the history
git-svn-id: file:///Users/Shitiz/Downloads/lssvn/source/limesurvey_yii@11761 b72ed6b6-b9f8-46b5-92b4-906544132732
  • Loading branch information
c-schmitz committed Dec 25, 2011
1 parent 422fc08 commit 531b00c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 65 deletions.
145 changes: 81 additions & 64 deletions application/controllers/register.php
Expand Up @@ -21,42 +21,53 @@
* @version $Id$
* @access public
*/
class register extends LSCI_Controller {
class register extends CAction {

/**
* register::__construct()
* Constructor
* @return
*/
function __construct()
{
parent::__construct();
}

public function run()
{
$actions = array_keys($_GET);
$_GET['method'] = $action = (!empty($actions[0])) ? $actions[0] : '';
Yii::app()->loadHelper('database');
Yii::app()->loadHelper('replacements');
//Yii::app();
//$this->getController();

if(!empty($action))
{
$this->$action($_GET[$action]);
}
else
{
$this->index();
}
}
function ajaxregisterform($surveyid)
{
$this->load->helper('database');
$this->load->helper('replacements');
$redata = compact(array_keys(get_defined_vars()));
$thistpl = $this->config->item("standardtemplaterootdir").'/default';
$thistpl = Yii::app()->getConfig("standardtemplaterootdir").'/default';
$surveyid = sanitize_int($surveyid);
$squery = "SELECT a.expires, a.startdate
FROM ".$this->db->dbprefix('surveys')." AS a
FROM {{surveys}} AS a
WHERE a.sid = $surveyid ";


$sresult = db_execute_assoc($squery) or show_error("Couldn't execute $squery");


$row = $sresult->row_array();
$row = $sresult->read();

$data['sid'] = $surveyid;
$data['startdate'] = $row['startdate'];
$data['enddate'] = $row['expires'];

$baselang = GetBaseLanguageFromSurveyID($surveyid);
$this->load->library('Limesurvey_lang',array($baselang));
$data['clang'] = $this->limesurvey_lang;
Yii::import('application.libraries.Limesurvey_lang');
$clang = new Limesurvey_lang(array('langcode' => $baselang));
echo templatereplace(file_get_contents("$thistpl/register.pstpl"),array(),$redata,'register.php',false,NULL,$data);

}
Expand All @@ -68,89 +79,94 @@ function ajaxregisterform($surveyid)
*/
function index()
{

$surveyid=$this->input->post('sid');
$postlang=$this->input->post('lang');


$surveyid = CHttpRequest::getPost('sid');
$postlang = CHttpRequest::getPost('lang');
if (!$surveyid)
{
redirect();
Yii::app()->request->redirect(Yii::app()->baseUrl);
}
$this->load->helper('database');

$usquery = "SELECT stg_value FROM ".$this->db->dbprefix."settings_global where stg_name='SessionName'";
$usquery = "SELECT stg_value FROM {{settings_global}} where stg_name='SessionName'";
$usresult = db_execute_assoc($usquery,'',true); //Checked
if ($usresult->num_rows() > 0)
if ($usresult->count() > 0)
{
$usrow = $usresult->row_array();
$usrow = $usresult->read();
$stg_SessionName=$usrow['stg_value'];
@session_name($stg_SessionName.'-runtime-'.$surveyid);
CHttpSession::setSessionName("$stg_SessionName-runtime-$surveyid");
}
else
{
session_name("LimeSurveyRuntime-$surveyid");
CHttpSession::setSessionName("LimeSurveyRuntime-$surveyid");
}

session_set_cookie_params(0,$this->config->item('relativeurl').'/');
session_start();

CHttpSession::setCookieParams(array(0, Yii::app()->getConfig('relativeurl').'/'));

// Get passed language from form, so that we dont loose this!
if (!isset($postlang) || $postlang == "" || !$postlang )
{
$baselang = GetBaseLanguageFromSurveyID($surveyid);
$this->load->library('Limesurvey_lang',array($baselang));
$clang = $this->limesurvey_lang;
//$this->load->library('Limesurvey_lang',array($baselang));
Yii::import('application.libraries.Limesurvey_lang');
$clang = new Limesurvey_lang(array('langcode' => $baselang));
//$clang = $this->limesurvey_lang;
} else {
$this->load->library('Limesurvey_lang',array($postlang));
$clang = $this->limesurvey_lang;
//$this->load->library('Limesurvey_lang',array($postlang));
//$clang = $this->limesurvey_lang;
Yii::import('application.libraries.Limesurvey_lang');
$clang = new Limesurvey_lang(array('langcode' => $postlang));
$baselang = $postlang;
}

$thissurvey=getSurveyInfo($surveyid,$baselang);

$register_errormsg = "";
$_POST = $this->input->post();
// Check the security question's answer
if (function_exists("ImageCreate") && captcha_enabled('registrationscreen',$thissurvey['usecaptcha']) )
if (function_exists("ImageCreate") && captcha_enabled('registrationscreen',$thissurvey['usecaptcha']) )
{
if (!isset($_POST['loadsecurity']) ||
!isset($_SESSION['secanswer']) ||
$_POST['loadsecurity'] != $_SESSION['secanswer'])
!isset(Yii::app()->session['secanswer']) ||
CHttpRequest::getPost('loadsecurity') != Yii::app()->session['secanswer'])
{
$register_errormsg .= $clang->gT("The answer to the security question is incorrect.")."<br />\n";
}
}

//Check that the email is a valid style address
if (!validate_email($_POST['register_email']))
if (!validate_email(CHttpRequest::getPost('register_email')))
{
$register_errormsg .= $clang->gT("The email you used is not valid. Please try again.");
}

if ($register_errormsg != "")
{
redirect($surveyid);
Yii::app()->request->redirect($surveyid);
}

$dbprefix = $this->db->dbprefix;
//$dbprefix = $this->db->dbprefix;
//Check if this email already exists in token database
$query = "SELECT email FROM {$dbprefix}tokens_$surveyid\n"
. "WHERE email = '".sanitize_email($_POST['register_email'])."'";
$query = "SELECT email FROM {{tokens_$surveyid}}\n"
. "WHERE email = '".sanitize_email(CHttpRequest::getPost('register_email'))."'";
$result = db_execute_assoc($query) or show_error("Unable to execute this query : \n <br/>".$query."<br />"); //Checked)
if (($result->num_rows()) > 0)
if (($result->count()) > 0)
{
$register_errormsg=$clang->gT("The email you used has already been registered.");
redirect($surveyid);
Yii::app()->request->redirect($surveyid);
//include "index.php";
//exit;
}

$mayinsert = false;

// Get the survey settings for token length
$this->load->model("surveys_model");
$tlresult = $this->surveys_model->getSomeRecords(array("tokenlength"),array("sid"=>$surveyid));
$tlrow = $tlresult->row_array();
//$this->load->model("surveys_model");
$tlresult = Survey::model()->getSomeRecords(array("tokenlength"),array("sid"=>$surveyid));
if (isset($tlresult[0])) {
$tlrow = $tlresult[0];
}
else
{
$tlrow = $tlresult;
}
$tokenlength = $tlrow['tokenlength'];
//if tokenlength is not set or there are other problems use the default value (15)
if(!isset($tokenlength) || $tokenlength == '')
Expand All @@ -161,27 +177,27 @@ function index()
while ($mayinsert != true)
{
$newtoken = sRandomChars($tokenlength);
$ntquery = "SELECT * FROM {$dbprefix}tokens_$surveyid WHERE token='$newtoken'";
$ntquery = "SELECT * FROM {{tokens_$surveyid}} WHERE token='$newtoken'";
$ntresult = db_execute_assoc($ntquery); //Checked
if (!$ntresult->num_rows()) {$mayinsert = true;}
if (!$ntresult->count()) {$mayinsert = true;}
}

$postfirstname=sanitize_xss_string(strip_tags($_POST['register_firstname']));
$postlastname=sanitize_xss_string(strip_tags($_POST['register_lastname']));
$starttime = sanitize_xss_string($this->input->post('startdate'));
$endtime = sanitize_xss_string($this->input->post('enddate'));
$postfirstname=sanitize_xss_string(strip_tags(CHttpRequest::getPost('register_firstname')));
$postlastname=sanitize_xss_string(strip_tags(CHttpRequest::getPost('register_lastname')));
$starttime = sanitize_xss_string(CHttpRequest::getPost('startdate'));
$endtime = sanitize_xss_string(CHttpRequest::getPost('enddate'));
/*$postattribute1=sanitize_xss_string(strip_tags(returnglobal('register_attribute1')));
$postattribute2=sanitize_xss_string(strip_tags(returnglobal('register_attribute2'))); */

//Insert new entry into tokens db
$query = "INSERT INTO {$dbprefix}tokens_$surveyid\n"
$query = "INSERT INTO {{tokens_$surveyid}}\n"
. "(firstname, lastname, email, emailstatus, token";

if ($starttime && $endtime)
$query .= ", validfrom, validuntil";

$query .=")\n"
. "VALUES ('$postfirstname', '$postlastname', '".$_POST['register_email']."', 'OK', '$newtoken'";
. "VALUES ('$postfirstname', '$postlastname', '".CHttpRequest::getPost('register_email')."', 'OK', '$newtoken'";

if ($starttime && $endtime)
$query .= ",$starttime,$endtime";
Expand All @@ -198,7 +214,7 @@ function index()
// $postattribute1, $postattribute2)
) or safe_die ($query."<br />".$connect->ErrorMsg()); //Checked - According to adodb docs the bound variables are quoted automatically
*/
$tid=$this->db->insert_id(); //$connect->Insert_ID("{$dbprefix}tokens_$surveyid","tid");
$tid = Yii::app()->db->getLastInsertID();; //$connect->Insert_ID("{$dbprefix}tokens_$surveyid","tid");


$fieldsarray["{ADMINNAME}"]=$thissurvey['adminname'];
Expand All @@ -218,33 +234,34 @@ function index()
if (getEmailFormat($surveyid) == 'html')
{
$useHtmlEmail = true;
$surveylink = site_url(''.$surveyid.'/lang-'.$baselang.'/tk-'.$newtoken);
$optoutlink = site_url('optout/local/'.$surveyid.'/'.$baselang.'/'.$newtoken);
$optinlink = site_url('optin/local/'.$surveyid.'/'.$baselang.'/'.$newtoken);
$surveylink = $this->getController()->createUrl(''.$surveyid.'/lang-'.$baselang.'/tk-'.$newtoken);
$optoutlink = $this->getController()->createUrl('optout/local/'.$surveyid.'/'.$baselang.'/'.$newtoken);
$optinlink = $this->getController()->createUrl('optin/local/'.$surveyid.'/'.$baselang.'/'.$newtoken);
$fieldsarray["{SURVEYURL}"]="<a href='$surveylink'>".$surveylink."</a>";
$fieldsarray["{OPTOUTURL}"]="<a href='$optoutlink'>".$optoutlink."</a>";
$fieldsarray["{OPTINURL}"]="<a href='$optinlink'>".$optinlink."</a>";
}
else
{
$useHtmlEmail = false;
$fieldsarray["{SURVEYURL}"]=site_url(''.$surveyid.'/lang-'.$baselang.'/tk-'.$newtoken);
$fieldsarray["{OPTOUTURL}"]= site_url('optout/local/'.$surveyid.'/'.$baselang.'/'.$newtoken);
$fieldsarray["{OPTINURL}"]= site_url('optin/local/'.$surveyid.'/'.$baselang.'/'.$newtoken);
$fieldsarray["{SURVEYURL}"]= $this->getController()->createUrl(''.$surveyid.'/lang-'.$baselang.'/tk-'.$newtoken);
$fieldsarray["{OPTOUTURL}"]= $this->getController()->createUrl('optout/local/'.$surveyid.'/'.$baselang.'/'.$newtoken);
$fieldsarray["{OPTINURL}"]= $this->getController()->createUrl('optin/local/'.$surveyid.'/'.$baselang.'/'.$newtoken);
}

$message=ReplaceFields($message, $fieldsarray);
$subject=ReplaceFields($subject, $fieldsarray);

$html=""; //Set variable
$sitename = Yii::app()->getConfig('sitename');

if (SendEmailMessage($message, $subject, $_POST['register_email'], $from, $sitename,$useHtmlEmail,getBounceEmail($surveyid)))
if (SendEmailMessage($message, $subject, CHttpRequest::getPost('register_email'), $from, $sitename,$useHtmlEmail,getBounceEmail($surveyid)))
{
// TLR change to put date into sent
// $query = "UPDATE {$dbprefix}tokens_$surveyid\n"
// ."SET sent='Y' WHERE tid=$tid";
$today = date_shift(date("Y-m-d H:i:s"), "Y-m-d H:i", $timeadjust);
$query = "UPDATE {$dbprefix}tokens_$surveyid\n"
$query = "UPDATE {{tokens_$surveyid}}\n"
."SET sent='$today' WHERE tid=$tid";
$result=db_execute_assoc($query) or show_error("Unable to execute this query : $query<br />"); //Checked
$html="<center>".$clang->gT("Thank you for registering to participate in this survey.")."<br /><br />\n".$clang->gT("An email has been sent to the address you provided with access details for this survey. Please follow the link in that email to proceed.")."<br /><br />\n".$clang->gT("Survey administrator")." {ADMINNAME} ({ADMINEMAIL})";
Expand All @@ -268,7 +285,7 @@ function index()

sendcacheheaders();
doHeader();

Yii::app()->lang = $clang;
foreach(file("$thistpl/startpage.pstpl") as $op)
{
echo templatereplace($op);
Expand Down
2 changes: 1 addition & 1 deletion application/models/Survey_dynamic.php
Expand Up @@ -26,7 +26,7 @@ class Survey_dynamic extends CActiveRecord
* @param int $surveyid
* @return CActiveRecord
*/
public static function model($sid = null)
public static function model($sid = NULL)
{
if (!is_null($sid))
self::sid($sid);
Expand Down

0 comments on commit 531b00c

Please sign in to comment.