Commit 82b9ba15 authored by Piers Harding's avatar Piers Harding
Browse files

Merge branch 'master' of git@gitorious.org:mahara/mahara

parents b57da3b2 99842fe0
......@@ -164,6 +164,14 @@ if (get_config('showtagssideblock')) {
'rules' => array('integer' => true, 'minvalue' => 0, 'maxvalue' => 1000),
);
}
if (get_config('userscanhiderealnames')) {
$elements['hiderealname'] = array(
'type' => 'checkbox',
'title' => get_string('hiderealname', 'account'),
'description' => get_string('hiderealnamedescription', 'account'),
'defaultvalue' => $prefs->hiderealname,
);
}
$elements['submit'] = array(
'type' => 'submit',
'value' => get_string('save')
......
......@@ -257,6 +257,12 @@ $siteoptionform = array(
'defaultvalue' => get_config('remoteavatars'),
'help' => true,
),
'userscanhiderealnames' => array(
'type' => 'checkbox',
'title' => get_string('userscanhiderealnames', 'admin'),
'description' => get_string('userscanhiderealnamesdescription', 'admin'),
'defaultvalue' => get_config('userscanhiderealnames'),
),
)
);
......@@ -282,7 +288,7 @@ function siteoptions_submit(Pieform $form, $values) {
'registration_sendweeklyupdates', 'institutionexpirynotification', 'institutionautosuspend',
'captchaonregisterform', 'captchaoncontactform', 'showselfsearchsideblock', 'showtagssideblock',
'tagssideblockmaxtags', 'country', 'viewmicroheaders', 'userscanchooseviewthemes',
'remoteavatars',
'remoteavatars', 'userscanhiderealnames'
);
$oldlanguage = get_config('lang');
$oldtheme = get_config('theme');
......
......@@ -35,6 +35,8 @@ define('SECTION_PLUGINNAME', 'admin');
require_once('pieforms/pieform.php');
require_once('institution.php');
$TRANSPORTER = null;
if ($USER->get('admin')) {
$authinstances = auth_get_auth_instances();
}
......@@ -141,7 +143,7 @@ $form = pieform(array(
function adduser_validate(Pieform $form, $values) {
global $USER, $LEAP2A_FILE;
global $USER, $TRANSPORTER;
$authobj = AuthFactory::create($values['authinstance']);
......@@ -188,60 +190,33 @@ function adduser_validate(Pieform $form, $values) {
return;
}
$date = time();
$nicedate = date('Y/m/d h:i:s', $date);
$niceuser = preg_replace('/[^a-zA-Z0-9_-]/', '-', $values['username']);
$uploaddir = get_config('dataroot') . 'import/' . $niceuser . '-' . $date . '/';
$filename = $uploaddir . $values['leap2afile']['name'];
check_dir_exists($uploaddir);
if (!move_uploaded_file($values['leap2afile']['tmp_name'], $filename)) {
$form->set_error('leap2afile', get_string('failedtoobtainuploadedleapfile', 'admin'));
}
if ($values['leap2afile']['type'] == 'application/octet-stream') {
// the browser wasn't sure, so use mime_content_type to guess
$mimetype = mime_content_type($filename);
$mimetype = mime_content_type($values['leap2afile']['tmp_name']);
}
else {
$mimetype = $values['leap2afile']['type'];
}
safe_require('artefact', 'file');
$ziptypes = PluginArtefactFile::get_mimetypes_from_description('zip');
if (in_array($mimetype, $ziptypes)) {
// Unzip the file
$command = sprintf('%s %s %s %s',
escapeshellcmd(get_config('pathtounzip')),
escapeshellarg($filename),
get_config('unzipdirarg'),
escapeshellarg($uploaddir)
);
$output = array();
exec($command, $output, $returnvar);
if ($returnvar != 0) {
log_debug("unzip command failed with return value $returnvar");
// Let's make it obvious if the cause is obvious :)
if ($returnvar == 127) {
log_debug("This means that 'unzip' isn't installed, or the config var \$cfg->pathtounzip is not"
. " pointing at unzip (see Mahara's file lib/config-defaults.php)");
}
$form->set_error('leap2afile', get_string('failedtounzipleap2afile', 'admin'));
return;
}
$filename = $uploaddir . 'leap2a.xml';
if (!is_file($filename)) {
$form->set_error('leap2afile', get_string('noleap2axmlfiledetected', 'admin'));
return;
}
$date = time();
$niceuser = preg_replace('/[^a-zA-Z0-9_-]/', '-', $values['username']);
safe_require('import', 'leap');
$fakeimportrecord = (object)array(
'data' => array(
'importfile' => $values['leap2afile']['tmp_name'],
'importfilename' => $values['leap2afile']['name'],
'importid' => $niceuser . '-' . $date,
'mimetype' => $mimetype,
)
);
$TRANSPORTER = new LocalImporterTransport($fakeimportrecord);
try {
$TRANSPORTER->extract_file();
PluginImportLeap::validate_transported_data($TRANSPORTER);
}
else if ($mimetype != 'text/xml') {
$form->set_error('leap2afile', get_string('fileisnotaziporxmlfile', 'admin'));
catch (Exception $e) {
$form->set_error('leap2afile', $e->getMessage());
}
$LEAP2A_FILE = $filename;
}
else {
if (!$form->get_error('firstname') && !preg_match('/\S/', $firstname)) {
......@@ -266,7 +241,7 @@ function adduser_validate(Pieform $form, $values) {
}
function adduser_submit(Pieform $form, $values) {
global $USER, $SESSION, $LEAP2A_FILE;
global $USER, $SESSION, $TRANSPORTER;
db_begin();
ini_set('max_execution_time', 180);
......@@ -305,28 +280,22 @@ function adduser_submit(Pieform $form, $values) {
if (isset($values['leap2afile'])) {
// And we're good to go
$filename = substr($LEAP2A_FILE, strlen(get_config('dataroot')));
$logfile = dirname($LEAP2A_FILE) . '/import.log';
require_once(get_config('docroot') . 'import/lib.php');
safe_require('import', 'leap');
$importer = PluginImport::create_importer(null, (object)array(
$importdata = (object)array(
'token' => '',
//'host' => '',
'usr' => $user->id,
'queue' => (int)!(PluginImport::import_immediately_allowed()), // import allowed straight away? Then don't queue
'ready' => 0, // maybe 1?
'expirytime' => db_format_timestamp(time()+(60*60*24)),
'format' => 'leap',
'data' => array('filename' => $filename),
'loglevel' => PluginImportLeap::LOG_LEVEL_VERBOSE,
'logtargets' => LOG_TARGET_FILE,
'logfile' => $logfile,
'profile' => true,
));
);
$importer = PluginImport::create_importer(null, $TRANSPORTER, $importdata);
try {
$importer->process();
log_info("Imported user account $user->id from leap2a file, see $logfile for a full log");
log_info("Imported user account $user->id from leap2a file, see " . $importer->get('logfile') . ' for a full log');
}
catch (ImportException $e) {
log_info("LEAP2A import failed: " . $e->getMessage());
......
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
* http://wiki.mahara.org/Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage export
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
*
*/
define('INTERNAL', 1);
define('ADMIN', 1);
define('BULKEXPORT', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
raise_memory_limit("1024M");
ini_set('max_execution_time', 300); // 5 minutes
// Download the export file if it's been generated
if ($exportfile = $SESSION->get('exportfile')) {
$SESSION->set('exportdata', '');
$SESSION->set('exportfile', '');
require_once('file.php');
serve_file($exportfile, basename($exportfile), 'application/x-zip', array('lifetime' => 0, 'forcedownload' => true));
exit;
// TODO: delete the zipfile (and temporary files) once it's been downloaded
}
// Turn off all compression because it prevents output from being flushed
if (function_exists('apache_setenv')) {
apache_setenv('no-gzip', 1);
}
@ini_set('zlib.output_compression', 0);
if (!$exportdata = $SESSION->get('exportdata')) {
redirect(get_config('wwwroot').'admin/users/bulkexport.php');
}
$SESSION->set('exportdata', '');
$stylesheets = array_reverse($THEME->get_url('style/style.css', true));
?>
<html>
<head>
<title></title>
<?php foreach ($stylesheets as $stylesheet) { ?>
<link rel="stylesheet" type="text/css" href="<?php echo hsc($stylesheet); ?>">
<?php } ?>
<style type="text/css">
html, body {
margin: 0;
padding: 0;
background-color: #808080;
}
</style>
</head>
<body>
<div style="width: 100%; background-color: #808080;" class="progress-bar"></div>
<p class="progress-text"><?php echo get_string('Starting', 'export'); ?></p>
<?php
flush();
/**
* Outputs enough HTML to make a pretty error message in the iframe
*
* @param string $message The message to display to the user
*/
function export_iframe_die($message) {
echo '<div class="progress-bar" style="width: 100%;"><p>' . hsc($message) . '</p></div></body></html>';
exit;
}
/**
* Registered as the progress report handler for the export. Streams updates
* back to the browser
*
* @param int $percent How far complete the export is
* @param string $status A human-readable string describing the current step
*/
function export_iframe_progress_handler($percent, $status) {
// "Erase" the current output with a new background div
echo '<div style="width: 100%; background-color: #808080;" class="progress-bar"></div>';
// The progress bar itself
echo '<div class="progress-bar" style="width: ' . intval($percent) . '%;"></div>' . "\n";
// The status text
echo '<p class="progress-text">' . hsc($status) . "</p>\n";
ob_flush();
}
/**
* Convert a 2D array to a CSV file. This follows the basic rules from http://en.wikipedia.org/wiki/Comma-separated_values
*
* @param array $input 2D array of values: each line is an array of values
*/
function data_to_csv($input) {
if (empty($input) or !is_array($input)) {
return '';
}
$output = '';
foreach ($input as $line) {
$lineoutput = '';
foreach ($line as $element) {
$element = str_replace('"', '""', $element);
if (!empty($lineoutput)) {
$lineoutput .= ',';
}
$lineoutput .= "\"$element\"";
}
$output .= $lineoutput . "\r\n";
}
return $output;
}
function create_zipfile($listing, $files) {
global $USER;
if (empty($listing) or empty($files)) {
return false;
}
if (count($listing) != count($files)) {
throw new MaharaException("Files and listing don't match.");
}
// create temporary directories for the export
$exportdir = get_config('dataroot') . 'export/'
. $USER->get('id') . '/' . time() . '/';
if (!check_dir_exists($exportdir)) {
throw new SystemException("Couldn't create the temporary export directory $exportdir");
}
$usersdir = 'users/';
if (!check_dir_exists($exportdir . $usersdir)) {
throw new SystemException("Couldn't create the temporary export directory $usersdir");
}
// move user zipfiles into the export directory
foreach ($files as $filename) {
if (copy($filename, $exportdir . $usersdir . basename($filename))) {
unlink($filename);
}
else {
throw new SystemException("Couldn't move $filename to $usersdir");
}
}
// write username listing to a file
$listingfile = 'usernames.csv';
if (!file_put_contents($exportdir . $listingfile, data_to_csv($listing))) {
throw new SystemException("Couldn't write usernames to a file");
}
// zip everything up
$zipfile = $exportdir . 'mahara-bulk-export-' . time() . '.zip';
$cwd = getcwd();
$command = sprintf('%s %s %s %s %s',
get_config('pathtozip'),
get_config('ziprecursearg'),
escapeshellarg($zipfile),
escapeshellarg($listingfile),
escapeshellarg($usersdir)
);
$output = array();
chdir($exportdir);
exec($command, $output, $returnvar);
chdir($cwd);
if ($returnvar != 0) {
throw new SystemException('Failed to zip the export file: return code ' . $returnvar);
}
return $zipfile;
}
// Bail if we don't have enough data to do an export
if (empty($exportdata)) {
export_iframe_die(get_string('unabletogenerateexport', 'export'));
}
ob_start();
export_iframe_progress_handler(0, get_string('Setup', 'export'));
safe_require('export', 'leap');
$listing = array();
$files = array();
$exportcount = 0;
$exporterrors = array();
foreach ($exportdata as $username) {
$user = new User();
try {
$user->find_by_username($username);
} catch (AuthUnknownUserException $e) {
continue; // Skip non-existent users
}
$percentage = (double)$exportcount / count($exportdata) * 100;
$percentage = min($percentage, 98);
export_iframe_progress_handler($percentage, get_string('exportingusername', 'admin', $username));
$exporter = new PluginExportLeap($user, PluginExport::EXPORT_ALL_VIEWS, PluginExport::EXPORT_ALL_ARTEFACTS);
try {
$zipfile = $exporter->export();
} catch (Exception $e) {
$exporterrors[] = $username;
continue;
}
$listing[] = array($username, $zipfile);
$files[] = $exporter->get('exportdir') . $zipfile;
$exportcount++;
}
export_iframe_progress_handler(99, get_string('creatingzipfile', 'export'));
if (!$zipfile = create_zipfile($listing, $files)) {
export_iframe_die(get_string('bulkexportempty', 'admin'));
}
export_iframe_progress_handler(100, get_string('Done', 'export'));
ob_end_flush();
log_info("Exported $exportcount users to $zipfile");
if (!empty($exporterrors)) {
$SESSION->add_error_msg(get_string('couldnotexportusers', 'admin', implode(', ', $exporterrors)));
}
// Store the filename in the session, and redirect the iframe to it to trigger
// the download. Here it would be nice to trigger the download for everyone,
// but alas this is not possible for people without javascript.
$SESSION->set('exportfile', $zipfile);
$wwwroot = get_config('wwwroot');
$strexportgeneratedsuccessfullyjs = get_string('exportgeneratedsuccessfullyjs', 'export', '<a href="' . $wwwroot . '" target="_top">', '</a>');
$strexportgeneratedsuccessfully = get_string('exportgeneratedsuccessfully', 'export', '<a href="bulkdownload.php" target="_top">', '</a>');
?>
<script type="text/javascript">
document.write('<div class="progress-bar" style="width: 100%;"><p><?php echo $strexportgeneratedsuccessfullyjs; ?></p></div>');
if (!window.opera) {
// Opera can't handle this for some reason - it vomits out the
// download inline in the iframe
document.location = 'bulkdownload.php';
}
</script>
<div class="progress-bar" style="width: 100%;">
<p><?php echo $strexportgeneratedsuccessfully; ?></p>
</div>
</body>
</html>
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
* http://wiki.mahara.org/Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage admin
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
*
*/
define('INTERNAL', 1);
define('ADMIN', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once('pieforms/pieform.php');
define('TITLE', get_string('bulkexporttitle', 'admin'));
function bulkexport_submit(Pieform $form, $values) {
global $SESSION;
$usernames = array();
// Read in the usernames explicitly specified
foreach (split("\n", $values['usernames']) as $username) {
$username = trim($username);
if (!empty($username)) {
$usernames[] = $username;
}
}
if (empty($usernames) and !empty($values['authinstance'])) {
// Export all users from the selected institution
$rs = get_recordset_select('usr', 'authinstance = ? AND deleted = 0', array($values['authinstance']), '', 'username');
while ($record = $rs->FetchRow()) {
$usernames[] = $record['username'];
}
}
$SESSION->set('exportdata', $usernames);
$smarty = smarty();
$smarty->assign('heading', '');
$smarty->display('admin/users/bulkdownload.tpl');
exit;
}
$authinstanceelement = array('type' => 'hidden', 'value' => '');
$authinstances = auth_get_auth_instances();
if (count($authinstances) > 0) {
$options = array();
foreach ($authinstances as $authinstance) {
$options[$authinstance->id] = $authinstance->displayname. ': '.$authinstance->instancename;
}
$default = key($options);
$authinstanceelement = array(
'type' => 'select',
'title' => get_string('institution'),
'description' => get_string('bulkexportinstitution', 'admin'),
'options' => $options,
'defaultvalue' => $default
);
}
$form = array(
'name' => 'bulkexport',
'elements' => array(
'authinstance' => $authinstanceelement,
'usernames' => array(
'type' => 'textarea',
'rows' => 25,
'cols' => 50,
'title' => get_string('bulkexportusernames', 'admin'),
'description' => get_string('bulkexportusernamesdescription', 'admin'),
),
'submit' => array(
'type' => 'submit',
'value' => get_string('bulkexport', 'admin')
)
)
);
$form = pieform($form);
$smarty = smarty();
$smarty->assign('bulkexportform', $form);
$smarty->assign('bulkexportdescription', get_string('bulkexportdescription', 'admin'));
$smarty->assign('PAGEHEADING', hsc(TITLE));
$smarty->display('admin/users/bulkexport.tpl');
......@@ -32,13 +32,37 @@ require_once('pieforms/pieform.php');
require_once('institution.php');
safe_require('artefact', 'internal');
safe_require('artefact', 'file');
raise_memory_limit("512M");
raise_memory_limit('1024M');
set_time_limit(300); // 5 minutes
define('TITLE', get_string('bulkleap2aimport', 'admin'));
// Turn on autodetecting of line endings, so mac newlines (\r) will work
ini_set('auto_detect_line_endings', 1);
$ADDEDUSERS = $SESSION->get('bulkimport_addedusers');
if (empty($ADDEDUSERS)) {
$ADDEDUSERS = array();
}
$FAILEDUSERS = $SESSION->get('bulkimport_failedusers');
if (empty($FAILEDUSERS)) {
$FAILEDUSERS = array();
}
$LEAP2AFILES = $SESSION->get('bulkimport_leap2afiles');
if (empty($LEAP2AFILES)) {
$LEAP2AFILES = array();
}
$AUTHINSTANCE = $SESSION->get('bulkimport_authinstance');
$EMAILUSERS = $SESSION->get('bulkimport_emailusers');
// Import in progress
if (!empty($LEAP2AFILES)) {
import_next_user();
}
elseif (!empty($ADDEDUSERS) or !empty($FAILEDUSERS)) {
finish_import();
}
$authinstances = auth_get_auth_instances();
if (count($authinstances) > 0) {
......@@ -62,10 +86,11 @@ $form = array(
'name' => 'bulkimport',
'elements' => array(
'authinstance' => $authinstanceelement,
'directory' => array(
'file' => array(
'type' => 'text',
'title' => get_string('Directory', 'admin'),
'description' => get_string('bulkleap2aimportdirdescription', 'admin'),
'title' => get_string('importfile', 'admin'),
'size' => 40,
'description' => get_string('bulkleap2aimportfiledescription', 'admin'),
'rules' => array(
'required' => true
)
......@@ -83,6 +108,16 @@ $form = array(