Commit e113aeb6 authored by Alan McNatty's avatar Alan McNatty

Mobile upload token changed to a multitext field (Bug #1032414).

This enables users to set-up multiple tokens - one for each
mobile device

Change-Id: I6d6c4d1d4c3a08b8f115594b82150323f42cee61
Signed-off-by: default avatarAlan McNatty <alan@catalyst.net.nz>
parent ba87daba
......@@ -182,6 +182,12 @@ function accountprefs_validate(Pieform $form, $values) {
$form->set_error('urlid', get_string('urlalreadytaken', 'account'));
}
}
foreach ($values['mobileuploadtoken'] as $k => $text) {
if (strlen($text) > 0 && !preg_match('/^[a-zA-Z0-9 !@#$%^&*()\-_=+\[{\]};:\'",<\.>\/?]{6,}$/', $text)) {
$form->set_error('mobileuploadtoken', get_string('badmobileuploadtoken', 'account'));
}
}
}
function accountprefs_submit(Pieform $form, $values) {
......@@ -214,6 +220,12 @@ function accountprefs_submit(Pieform $form, $values) {
$oldlang = $USER->get_account_preference('lang');
$oldtheme = $USER->get_account_preference('theme');
// Make sure the mobile token is formatted / saved correctly
$values['mobileuploadtoken'] = array_filter($values['mobileuploadtoken']);
$new_token_pref = empty($values['mobileuploadtoken']) ? null : ('|' . join('|', $values['mobileuploadtoken']) . '|');
$USER->set_account_preference('mobileuploadtoken', $new_token_pref);
unset($values['mobileuploadtoken']);
// Set user account preferences
foreach ($expectedprefs as $eprefkey => $epref) {
if (isset($values[$eprefkey]) && $values[$eprefkey] != get_account_preference($USER->get('id'), $eprefkey)) {
......
......@@ -284,7 +284,8 @@ class User {
FROM
{usr} u
LEFT JOIN {usr_account_preference} p ON u.id = p.usr
WHERE p.field=\'mobileuploadtoken\' AND p.value = ? AND u.username = ?
WHERE p.field=\'mobileuploadtoken\'
AND p.value ' . db_ilike() . ' \'%|\' || ? || \'|%\' AND u.username = ?
';
$user = get_record_sql($sql, array($token, $username));
......@@ -294,6 +295,7 @@ class User {
}
$this->populate($user);
$this->accountprefs = load_account_preferences($user->id);
return $this;
}
......@@ -301,12 +303,20 @@ class User {
* Refreshes a users mobile 'token' and returns it
*
*/
public function refresh_mobileuploadtoken() {
$new_token = md5( uniqid() );
$this->set_account_preference('mobileuploadtoken', $new_token);
public function refresh_mobileuploadtoken($old_token) {
$new_token = md5(openssl_random_pseudo_bytes(8));
$old_tokenstring = $this->get_account_preference('mobileuploadtoken');
$tokenarray = explode('|', trim($old_tokenstring, '|'));
foreach ($tokenarray as $k => $v) {
if ( $v == $old_token ) {
$tokenarray[$k] = $new_token;
}
}
$new_tokenstring = empty($tokenarray) ? null : ('|' . join('|', $tokenarray) . '|');
$this->set_account_preference('mobileuploadtoken', $new_tokenstring);
$this->set('lastaccess', time());
$this->commit();
return $new_token;
$this->commit();
return $new_token;
}
/**
......
......@@ -78,7 +78,8 @@ $string['showhomeinfo1'] = 'Homepage information';
$string['showhomeinfodescription'] = 'Display information about how to use %s on the homepage.';
$string['mobileuploadtoken'] = 'Mobile upload token';
$string['mobileuploadtokendescription'] = 'Enter a token here and on your phone to enable uploads (note: it will change automatically after each upload. <br/>If you have any problems - simply reset it here and on your phone.';
$string['mobileuploadtokendescription'] = 'Enter a token for each of your mobile devices here and then on your phone to enable uploads (note: tokens must be at least 6 characters and they will be change automatically after each upload). <br/>If you have any problems - simply reset it here and on your phone.';
$string['badmobileuploadtoken'] = 'Sorry that mobile upload token is invalid - tokens must be at least 6 characters in length.';
$string['prefssaved'] = 'Preferences saved';
$string['prefsnotsaved'] = 'Failed to save your preferences.';
......
......@@ -3097,5 +3097,11 @@ function xmldb_core_upgrade($oldversion=0) {
}
}
if ($oldversion < 2012080300) {
// For multi-tokens we need '|' aka pipe characters either side of their old single token
execute_sql('UPDATE {usr_account_preference} SET value = \'|\' || value || \'|\'
WHERE field=\'mobileuploadtoken\' AND NOT value ' . db_ilike() . '\'|%|\'');
}
return $status;
}
......@@ -320,11 +320,15 @@ function general_account_prefs_form_elements($prefs) {
);
}
if (get_config('allowmobileuploads')) {
$defaultvalue = array();
$mobileuploadtoken = isset($prefs->mobileuploadtoken) ? $prefs->mobileuploadtoken : get_config('mobileuploadtoken');
$defaultvalue = explode('|', trim($mobileuploadtoken, '|'));
$elements['mobileuploadtoken'] = array(
'type' => 'text',
'type' => 'multitext',
'title' => get_string('mobileuploadtoken', 'account'),
'description' => get_string('mobileuploadtokendescription', 'account'),
'defaultvalue' => isset($prefs->mobileuploadtoken) ? $prefs->mobileuploadtoken : get_config('mobileuploadtoken')
'defaultvalue' => $defaultvalue
);
}
if (get_config_plugin('artefact', 'file', 'resizeonuploadenable')) {
......
......@@ -30,7 +30,7 @@ defined('INTERNAL') || die();
$config = new StdClass;
// See https://wiki.mahara.org/index.php/Developer_Area/Version_Numbering_Policy
// For upgrades on stable branches, increment the version by one. On master, use the date.
$config->version = 2012080200;
$config->version = 2012080300;
$config->release = '1.6.0dev';
$config->minupgradefrom = 2008040200;
$config->minupgraderelease = '1.0.0 (release tag 1.0.0_RELEASE)';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment