Skip to content

Commit

Permalink
Fixes issue symphonycms#151
Browse files Browse the repository at this point in the history
  • Loading branch information
simoneeconomo committed Jun 9, 2011
1 parent 52d1d47 commit 76014a0
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 48 deletions.
2 changes: 1 addition & 1 deletion content/content.roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __viewIndex() {

else if(is_null(extension_Members::getMembersSection())) {
$aTableBody = array(Widget::TableRow(
array(Widget::TableData(__('No Member section has been specified in %s. Please do this first.', array('<a href="'.SYMPHONY_URL.'/system/preferences/">Preferences</a>')), 'inactive', NULL, count($aTableHead)))
array(Widget::TableData(__('No Member section has been specified in <a href="%s">Preferences</a>. Please do this first.', array(SYMPHONY_URL.'/system/preferences/')), 'inactive', NULL, count($aTableHead)))
));
}

Expand Down
79 changes: 64 additions & 15 deletions extension.driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,39 @@ public function update($previousVersion) {
Symphony::Configuration()->remove('authentication', 'members');
Administration::instance()->saveConfig();
}

if(version_compare($previousVersion, '1.1', '<')) {
// Updates values in each `code_expiry` column (see Password Field and Activation Field)
$activation_table = Symphony::Database()->fetchRow(0, "SHOW TABLES LIKE 'tbl_fields_memberactivation';");
if(!empty($activation_table)) {
$values = Symphony::Database()->fetch("
SELECT `id`, `code_expiry` FROM `tbl_fields_memberactivation` ORDER BY `id` ASC;
");
$this->fromStrtotimeToMinutes($values);
foreach($values as $index => $value) {
Symphony::Database()->update(
array('code_expiry' => $value['code_expiry']),
"tbl_fields_memberactivation",
"`id` = " . $value['id']
);
}
}

$password_table = Symphony::Database()->fetchRow(0, "SHOW TABLES LIKE 'tbl_fields_memberpassword';");
if(!empty($password_table)) {
$values = Symphony::Database()->fetch("
SELECT `id`, `code_expiry` FROM `tbl_fields_memberpassword` ORDER BY `id` ASC;
");
$this->fromStrtotimeToMinutes($values);
foreach($values as $index => $value) {
Symphony::Database()->update(
array('code_expiry' => $value['code_expiry']),
"tbl_fields_memberpassword",
"`id` = " . $value['id']
);
}
}
}
}

/*-------------------------------------------------------------------------
Expand Down Expand Up @@ -537,24 +570,24 @@ public function appendFilter($context) {
}
}

public static function findCodeExpiry($table) {
$default = array('1 hour' => '1 hour', '24 hours' => '24 hours');
# public static function findCodeExpiry($table) {
# $default = array('1 hour' => '1 hour', '24 hours' => '24 hours');

try {
$used = Symphony::Database()->fetchCol('code_expiry', sprintf("
SELECT DISTINCT(code_expiry) FROM `%s`
", $table));
# try {
# $used = Symphony::Database()->fetchCol('code_expiry', sprintf("
# SELECT DISTINCT(code_expiry) FROM `%s`
# ", $table));

if(is_array($used) && !empty($used)) {
$default = array_merge($default, array_combine($used, $used));
}
}
catch (DatabaseException $ex) {
// Table doesn't exist yet, it's ok we have defaults.
}
# if(is_array($used) && !empty($used)) {
# $default = array_merge($default, array_combine($used, $used));
# }
# }
# catch (DatabaseException $ex) {
# // Table doesn't exist yet, it's ok we have defaults.
# }

return $default;
}
# return $default;
# }

public static function fetchEmailTemplates() {
$options = array();
Expand Down Expand Up @@ -611,6 +644,22 @@ public static function fetchEmailTemplates() {
return $options;
}

public static function fromStrtotimeToMinutes(&$values) {
$default = 1440; // 24 hours
$current_time = time();

foreach($values as $index => &$value){
$computed_time = strtotime($value['code_expiry']);
if($computed_time && ($computed_time > $current_time)){
// Sorry, we can't afford second precision
$value['code_expiry'] = round(($computed_time - $current_time) / 60, 0, PHP_ROUND_HALF_UP);
} else {
// If the stored value wasn't valid (e.g. "last Monday" or "yesterday"), it's reverted to the default
$value['code_expiry'] = $default;
}
}
}

/*-------------------------------------------------------------------------
Preferences:
-------------------------------------------------------------------------*/
Expand Down
27 changes: 15 additions & 12 deletions fields/field.memberactivation.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function isCodeActive($entry_id) {
LIMIT 1",
$this->get('id'),
$entry_id,
DateTimeObj::get('Y-m-d H:i:s', strtotime('now + ' . $this->get('code_expiry')))
DateTimeObj::get('Y-m-d H:i:s', strtotime('now + ' . $this->get('code_expiry') . ' minutes'))
));

if(is_array($code) && !empty($code) && !is_null($code['code'])) {
Expand Down Expand Up @@ -153,7 +153,7 @@ public function purgeCodes($entry_id = null){
),
"`tbl_entries_data_{$this->get('id')}`",
sprintf("`activated` = 'no' AND DATE_FORMAT(timestamp, '%%Y-%%m-%%d %%H:%%i:%%s') < '%s' %s",
DateTimeObj::get('Y-m-d H:i:s', strtotime('now - ' . $this->get('code_expiry'))),
DateTimeObj::get('Y-m-d H:i:s', strtotime('now - ' . $this->get('code_expiry') . ' minutes')),
($entry_id ? " OR `entry_id` = $entry_id" : '')
)
);
Expand Down Expand Up @@ -201,24 +201,24 @@ public function displaySettingsPanel(&$wrapper, $errors=NULL){

$label = Widget::Label(__('Activation Code Expiry'));
$label->appendChild(
new XMLElement('i', __('How long a member\'s activation code will be valid for before it expires'))
new XMLElement('i', __('How long a member\'s activation code will be valid for before it expires (in minutes)'))
);
$label->appendChild(Widget::Input(
"fields[{$this->get('sortorder')}][code_expiry]", $this->get('code_expiry')
));

$ul = new XMLElement('ul', NULL, array('class' => 'tags singular'));
$tags = fieldMemberActivation::findCodeExpiry();
foreach($tags as $name => $time) {
$ul->appendChild(new XMLElement('li', $name, array('class' => $time)));
}
# $ul = new XMLElement('ul', NULL, array('class' => 'tags singular'));
# $tags = fieldMemberActivation::findCodeExpiry();
# foreach($tags as $name => $time) {
# $ul->appendChild(new XMLElement('li', $name, array('class' => $time)));
# }

if (isset($errors['code_expiry'])) {
$label = Widget::wrapFormElementWithError($label, $errors['code_expiry']);
}

$div->appendChild($label);
$div->appendChild($ul);
# $div->appendChild($ul);

// Get Roles in system
$roles = RoleManager::fetch();
Expand Down Expand Up @@ -268,8 +268,11 @@ public function checkFields(&$errors, $checkForDuplicates=true) {
$errors['code_expiry'] = __('This is a required field.');
}

if(!DateTimeObj::validate($this->get('code_expiry'))) {
$errors['code_expiry'] = __('Code expiry must be a unit of time, such as <code>1 day</code> or <code>2 hours</code>');
# if(!DateTimeObj::validate($this->get('code_expiry'))) {
# $errors['code_expiry'] = __('Code expiry must be a unit of time, such as <code>1 day</code> or <code>2 hours</code>');
# }
if(!preg_match("/^[1-9]+[0-9]*$/", trim($this->get('code_expiry')))) {
$errors['code_expiry'] = __('Code expiry must be a valid value for minutes, such as <code>60</code> (1 hour) or <code>1440</code> (1 day)');
}
}

Expand Down Expand Up @@ -409,7 +412,7 @@ public function appendFormattedElement(&$wrapper, $data, $encode=false){
);

// Add expiry timestamp, including how long the code is valid for
$expiry = General::createXMLDateObject(strtotime($data['timestamp'] . ' + ' . $this->get('code_expiry')), 'expires');
$expiry = General::createXMLDateObject(strtotime($data['timestamp'] . ' + ' . $this->get('code_expiry') . ' minutes'), 'expires');
$expiry->setAttribute('expiry', $this->get('code_expiry'));
$el->appendChild($expiry);
}
Expand Down
4 changes: 2 additions & 2 deletions fields/field.memberemail.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function fetchMemberIDBy($needle) {

if(empty($email)) {
extension_Members::$_errors[$this->get('element_name')] = array(
'message' => __('%s is a required field.', array($this->get('label'))),
'message' => __('\'%s\' is a required field.', array($this->get('label'))),
'type' => 'missing',
'label' => $this->get('label')
);
Expand Down Expand Up @@ -135,7 +135,7 @@ public function checkPostFieldData($data, &$message, $entry_id = null){

// If the field is required
if($required && empty($email)) {
$message = __('%s is a required field.', array($this->get('label')));
$message = __('\'%s\' is a required field.', array($this->get('label')));
return self::__MISSING_FIELDS__;
}

Expand Down
35 changes: 19 additions & 16 deletions fields/field.memberpassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function fetchMemberIDBy($needle, $member_id) {

if(empty($password)) {
extension_Members::$_errors[$this->get('element_name')] = array(
'message' => __('%s is a required field.', array($this->get('label'))),
'message' => __('\'%s\' is a required field.', array($this->get('label'))),
'type' => 'missing',
'label' => $this->get('label')
);
Expand All @@ -127,7 +127,7 @@ public function fetchMemberIDBy($needle, $member_id) {
AND DATE_FORMAT(expires, '%%Y-%%m-%%d %%H:%%i:%%s') > '%s'
LIMIT 1
",
$this->get('id'), $data['entry_id'], DateTimeObj::get('Y-m-d H:i:s', strtotime('now - '. $this->get('code_expiry')))
$this->get('id'), $data['entry_id'], DateTimeObj::get('Y-m-d H:i:s', strtotime('now - '. $this->get('code_expiry') . ' minutes'))
));

// If we didn't get an entry_id back, then it's because it was expired
Expand Down Expand Up @@ -304,24 +304,24 @@ public function displaySettingsPanel(&$wrapper, $errors=NULL){

$label = Widget::Label(__('Recovery Code Expiry'));
$label->appendChild(
new XMLElement('i', __('How long a member\'s recovery code will be valid for before it expires'))
new XMLElement('i', __('How long a member\'s recovery code will be valid for before it expires (in minutes)'))
);
$label->appendChild(Widget::Input(
"fields[{$this->get('sortorder')}][code_expiry]", $this->get('code_expiry')
));

$ul = new XMLElement('ul', NULL, array('class' => 'tags singular'));
$tags = fieldMemberPassword::findCodeExpiry();
foreach($tags as $name => $time) {
$ul->appendChild(new XMLElement('li', $name, array('class' => $time)));
}
# $ul = new XMLElement('ul', NULL, array('class' => 'tags singular'));
# $tags = fieldMemberPassword::findCodeExpiry();
# foreach($tags as $name => $time) {
# $ul->appendChild(new XMLElement('li', $name, array('class' => $time)));
# }

if (isset($errors['code_expiry'])) {
$label = Widget::wrapFormElementWithError($label, $errors['code_expiry']);
}

$div->appendChild($label);
$div->appendChild($ul);
# $div->appendChild($ul);

$group->appendChild($div);
$wrapper->appendChild($group);
Expand All @@ -346,8 +346,11 @@ public function checkFields(&$errors, $checkForDuplicates = true) {
$errors['code_expiry'] = __('This is a required field.');
}

if(!DateTimeObj::validate($this->get('code_expiry'))) {
$errors['code_expiry'] = __('Code expiry must be a unit of time, such as <code>1 day</code> or <code>2 hours</code>');
# if(!DateTimeObj::validate($this->get('code_expiry'))) {
# $errors['code_expiry'] = __('Code expiry must be a unit of time, such as <code>1 day</code> or <code>2 hours</code>');
# }
if(!preg_match("/^[1-9]+[0-9]*$/", trim($this->get('code_expiry')))) {
$errors['code_expiry'] = __('Code expiry must be a valid value for minutes, such as <code>60</code> (1 hour) or <code>1440</code> (1 day)');
}
}

Expand Down Expand Up @@ -462,24 +465,24 @@ public function checkPostFieldData($data, &$message, $entry_id = null){

// If the field is required, we should have both a $username and $password.
if($required && !isset($data['optional']) && (empty($password))) {
$message = __('%s is a required field.', array($this->get('label')));
$message = __('\'%s\' is a required field.', array($this->get('label')));
return self::__MISSING_FIELDS__;
}

// Check password
if(!empty($password)) {
if($confirm !== $password) {
$message = __('%s confirmation does not match.', array($this->get('label')));
$message = __('Passwords don\'t match.');
return self::__INVALID_FIELDS__;
}

if(strlen($password) < (int)$this->get('length')) {
$message = __('%s is too short. It must be at least %d characters.', array($this->get('label'), $this->get('length')));
$message = __('Password is too short. It must be at least %d characters.', array($this->get('length')));
return self::__INVALID_FIELDS__;
}

if (!fieldMemberPassword::compareStrength(fieldMemberPassword::checkPassword($password), $this->get('strength'))) {
$message = __('%s is not strong enough.', array($this->get('label')));
$message = __('Password is not strong enough.');
return self::__INVALID_FIELDS__;
}
}
Expand Down Expand Up @@ -529,7 +532,7 @@ public function appendFormattedElement(&$wrapper, $data, $encode=false) {
new XMLElement('recovery-code', $data['recovery-code'])
);
// Add expiry timestamp, including how long the code is valid for
$expiry = General::createXMLDateObject(strtotime($data['timestamp'] . ' + ' . $this->get('code_expiry')), 'expires');
$expiry = General::createXMLDateObject(strtotime($data['timestamp'] . ' + ' . $this->get('code_expiry') . ' minutes'), 'expires');
$expiry->setAttribute('expiry', $this->get('code_expiry'));
$pw->appendChild($expiry);
}
Expand Down
4 changes: 2 additions & 2 deletions fields/field.memberusername.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function fetchMemberIDBy($needle) {

if(empty($username)) {
extension_Members::$_errors[$this->get('element_name')] = array(
'message' => __('%s is a required field.', array($this->get('label'))),
'message' => __('\'%s\' is a required field.', array($this->get('label'))),
'type' => 'missing',
'label' => $this->get('label')
);
Expand Down Expand Up @@ -147,7 +147,7 @@ public function checkPostFieldData($data, &$message, $entry_id = null){

// If the field is required
if(($this->get('required') == "yes") && empty($username)) {
$message = __('%s is a required field.', array($this->get('label')));
$message = __('\'%s\' is a required field.', array($this->get('label')));
return self::__MISSING_FIELDS__;
}

Expand Down

0 comments on commit 76014a0

Please sign in to comment.