public function __construct($id, $record = null) { parent::__construct($id, $record); $data = $this->get('data'); $this->manifest = $data['filesmanifest']; $this->zipfilesha1 = $data['zipfilesha1']; }
public function get($field) { if ($field == 'xml') { return $this->xml; } return parent::get($field); }
function import_submit(Pieform $form, $values) { global $SESSION; $date = time(); $nicedate = date('Y/m/d h:i:s', $date); $uploaddir = get_config('dataroot') . 'import/test-' . $date . '/'; $filename = $uploaddir . $values['file']['name']; check_dir_exists($uploaddir); move_uploaded_file($values['file']['tmp_name'], $filename); if ($values['file']['type'] == 'application/zip') { // Unzip here $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) { $SESSION->add_error_msg('Unable to unzip the file'); redirect('/import/'); } $filename = $uploaddir . 'leap2a.xml'; if (!is_file($filename)) { $SESSION->add_error_msg('No leap2a.xml file detected - please check your export file again'); redirect('/import/'); } } // Create dummy user $user = (object) array('username' => 'import_' . $date, 'password' => 'import1', 'firstname' => 'Imported', 'lastname' => 'User (' . $nicedate . ')', 'email' => '*****@*****.**'); $userid = create_user($user); // And we're good to go echo '<pre>'; $filename = substr($filename, strlen(get_config('dataroot'))); require_once dirname(dirname(__FILE__)) . '/import/lib.php'; safe_require('import', 'leap'); db_begin(); $importer = PluginImport::create_importer(null, (object) array('token' => '', 'usr' => $userid, 'queue' => (int) (!PluginImport::import_immediately_allowed()), 'ready' => 0, 'expirytime' => db_format_timestamp(time() + 60 * 60 * 24), 'format' => 'leap', 'data' => array('filename' => $filename), 'loglevel' => PluginImportLeap::LOG_LEVEL_VERBOSE, 'logtargets' => LOG_TARGET_STDOUT, 'profile' => true)); $importer->process(); // Now done, delete the temporary e-mail address if there's a new one // A bit sucky, presumes only one email in the import $email = artefact_instance_from_id(get_field('artefact', 'id', 'title', '*****@*****.**', 'artefacttype', 'email', 'owner', $userid)); $email->delete(); execute_sql('UPDATE {artefact_internal_profile_email} SET principal = 1 WHERE "owner" = ?', array($userid)); db_commit(); echo "\n\n"; echo 'Done. You can <a href="' . get_config('wwwroot') . '/admin/users/changeuser.php?id=' . $userid . '">change to this user</a> to inspect the result, '; echo 'or <a href="' . get_config('wwwroot') . 'import/">try importing again</a>'; echo '</pre>'; exit; }
function do_import() { global $SESSION, $USER, $TRANSPORTER, $IMPORTER; safe_require('import', 'leap'); // Get $TRANSPORTER and $IMPORTER from $SESSION $importrecord = (object) array('data' => array('importid' => $SESSION->get('importid'), 'extracted' => $SESSION->get('extracted'), 'mimetype' => $SESSION->get('mimetype'))); $TRANSPORTER = new LocalImporterTransport($importrecord); $importdata = (object) array('token' => '', 'usr' => $USER->get('id'), 'queue' => (int) false, 'ready' => 0, 'expirytime' => db_format_timestamp(time() + 60 * 60 * 24), 'format' => 'leap', 'loglevel' => PluginImportLeap::LOG_LEVEL_STANDARD, 'logtargets' => LOG_TARGET_FILE, 'profile' => true); $IMPORTER = PluginImport::create_importer(null, $TRANSPORTER, $importdata); try { $result = $IMPORTER->do_import_from_requests(); } catch (ImportException $e) { log_info("Leap2A import failed: " . $e->getMessage()); die_info(get_string('importfailed', 'import')); } $smarty = smarty(); $smarty->assign('PAGEHEADING', get_string('importresult', 'import')); $smarty->assign('form', $result); $smarty->display('form.tpl'); }
function adduser_submit(Pieform $form, $values) { global $USER, $SESSION, $TRANSPORTER; db_begin(); raise_time_limit(180); // Create user $user = (object) array('authinstance' => $values['authinstance'], 'username' => $values['username'], 'firstname' => $values['firstname'] ? $values['firstname'] : 'Imported', 'lastname' => $values['lastname'] ? $values['lastname'] : 'User', 'email' => $values['email'], 'password' => $values['password'], 'passwordchange' => 1); if ($USER->get('admin')) { // Not editable by institutional admins $user->staff = (int) ($values['staff'] == 'on'); $user->admin = (int) ($values['admin'] == 'on'); } if ($USER->get('admin') || get_config_plugin('artefact', 'file', 'institutionaloverride')) { $user->quota = $values['quota']; } $authinstance = get_record('auth_instance', 'id', $values['authinstance']); $remoteauth = false; if ($authinstance->authname != 'internal') { $remoteauth = true; } if (!isset($values['remoteusername'])) { $values['remoteusername'] = null; } $user->id = create_user($user, array(), $authinstance->institution, $remoteauth, $values['remoteusername'], $values); if (isset($user->admin) && $user->admin) { require_once 'activity.php'; activity_add_admin_defaults(array($user->id)); } if ($values['institutionadmin']) { set_field('usr_institution', 'admin', 1, 'usr', $user->id, 'institution', $authinstance->institution); } if (isset($values['leap2afile'])) { // And we're good to go $importdata = (object) array('token' => '', 'usr' => $user->id, 'queue' => (int) (!PluginImport::import_immediately_allowed()), 'ready' => 0, 'expirytime' => db_format_timestamp(time() + 60 * 60 * 24), 'format' => 'leap', 'loglevel' => PluginImportLeap::LOG_LEVEL_VERBOSE, 'logtargets' => LOG_TARGET_FILE, 'profile' => true); $importer = PluginImport::create_importer(null, $TRANSPORTER, $importdata); try { $importer->process(); 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()); die_info(get_string('leap2aimportfailed', 'admin')); } // Reload the user details, as various fields are changed by the // importer when importing (e.g. firstname/lastname) $user = get_record('usr', 'id', $user->id); } db_commit(); if (!empty($user->email)) { try { email_user($user, $USER, get_string('accountcreated', 'mahara', get_config('sitename')), get_string('accountcreatedchangepasswordtext', 'mahara', $user->firstname, get_config('sitename'), $user->username, $values['password'], get_config('wwwroot'), get_config('sitename')), get_string('accountcreatedchangepasswordhtml', 'mahara', $user->firstname, get_config('wwwroot'), get_config('sitename'), $user->username, $values['password'], get_config('wwwroot'), get_config('wwwroot'), get_config('sitename'))); } catch (EmailException $e) { $SESSION->add_error_msg(get_string('newuseremailnotsent', 'admin')); } } $SESSION->add_ok_msg(get_string('newusercreated', 'admin')); redirect('/admin/users/edit.php?id=' . $user->id); }
/** * Returns a list of entry IDs that are children of this folder * * If necessary, this method can act recursively to find children at all * levels under this folder * * TODO: protection against circular references * * @param SimpleXMLElement $entry The folder to get children for * @param PluginImport $importer The importer * @param boolean $recurse Whether to return children at all levels below this folder * @return array A list of the entry IDs of children in this folder */ private static function get_children_of_folder(SimpleXMLElement $entry, PluginImport $importer, $recurse = false) { $children = array(); // Get entries that this folder feels are a part of it foreach ($entry->link as $link) { if ($importer->curie_equals($link['rel'], PluginImportLeap::NS_LEAP, 'has_part') && isset($link['href'])) { $child = $importer->get_entry_by_id((string) $link['href']); if ($child) { if (self::is_file($child, $importer) || self::is_folder($child, $importer)) { $children[] = (string) $link['href']; } else { $importer->trace("NOTICE: Child {$child->id} of folder {$entry->id} won't be imported by the file plugin because it is not a file or folder"); } } else { $importer->trace("WARNING: folder {$entry->id} claims to have child {$link['href']} which does not exist"); } } } if ($recurse) { foreach ($children as $childid) { $child = $importer->get_entry_by_id($childid); if (self::is_folder($child, $importer)) { $children = array_merge($children, self::get_children_of_folder($child, $importer, true)); } } } return $children; }
function import_next_user($filename, $username, $authinstance) { global $ADDEDUSERS, $FAILEDUSERS; log_debug('adding user ' . $username . ' from ' . $filename); $authobj = get_record('auth_instance', 'id', $authinstance); $institution = new Institution($authobj->institution); $date = time(); $nicedate = date('Y/m/d h:i:s', $date); $niceuser = preg_replace('/[^a-zA-Z0-9_-]/', '-', $username); $uploaddir = get_config('dataroot') . 'import/' . $niceuser . '-' . $date . '/'; check_dir_exists($uploaddir); // Unzip the file $archive = new ZipArchive(); if ($archive->open($filename) && $archive->extractTo($uploaddir)) { // successfully extracted $archive->close(); } else { $FAILEDUSERS[$username] = get_string('unzipfailed', 'admin', hsc($filename)); return; } $leap2afilename = $uploaddir . 'leap2a.xml'; if (!is_file($leap2afilename)) { $FAILEDUSERS[$username] = get_string('noleap2axmlfiledetected', 'admin'); log_debug($FAILEDUSERS[$username]); return; } // If the username is already taken, append something to the end while (get_record('usr', 'username', $username)) { $username .= "_"; } $user = (object) array('authinstance' => $authinstance, 'username' => $username, 'firstname' => 'Imported', 'lastname' => 'User', 'password' => get_random_key(6), 'passwordchange' => 1); db_begin(); try { $user->id = create_user($user, array(), $institution, $authobj); } catch (EmailException $e) { // Suppress any emails (e.g. new institution membership) sent out // during user creation, becuase the user doesn't have an email // address until we've imported them from the Leap2A file. log_debug("Failed sending email during user import"); } $niceuser = preg_replace('/[^a-zA-Z0-9_-]/', '-', $user->username); $record = (object) array('token' => '', 'usr' => $user->id, 'queue' => (int) (!PluginImport::import_immediately_allowed()), 'ready' => 0, 'expirytime' => db_format_timestamp(time() + 60 * 60 * 24), 'format' => 'leap', 'data' => array('importfile' => $filename, 'importfilename' => $filename, 'importid' => $niceuser . time(), 'mimetype' => file_mime_type($filename)), 'loglevel' => PluginImportLeap::LOG_LEVEL_VERBOSE, 'logtargets' => LOG_TARGET_FILE, 'profile' => true); $tr = new LocalImporterTransport($record); $tr->extract_file(); $importer = PluginImport::create_importer(null, $tr, $record); unset($record, $tr); try { $importer->process(); 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()); $FAILEDUSERS[$username] = get_string("leap2aimportfailed"); db_rollback(); } db_commit(); if (empty($FAILEDUSERS[$username])) { // Reload the user details, as various fields are changed by the // importer when importing (e.g. firstname/lastname) $newuser = get_record('usr', 'id', $user->id); $newuser->clearpasswd = $user->password; $ADDEDUSERS[] = $newuser; } return; }
/** * Imports data for the personalinformation artefact type, by looking for * it in the persondata element */ public static function import_author_data(PluginImport $importer, $persondataid) { if ($persondataid) { $composites = array(); $person = $importer->get_entry_by_id($persondataid); $namespaces = $importer->get_namespaces(); $ns = $importer->get_leap2a_namespace(); $persondata = $person->xpath($namespaces[$ns] . ':persondata'); foreach ($persondata as $item) { $leapattributes = PluginImportLeap::get_attributes($item, $ns); if (!isset($leapattributes['field'])) { // 'Field' is required // http://wiki.cetis.ac.uk/2009-03/Leap2A_personal_data#field $importer->trace('WARNING: persondata element did not have leap2:field attribute'); continue; } if ($leapattributes['field'] == 'dob') { $composites['dateofbirth'] = (string) $item; } if ($leapattributes['field'] == 'gender') { $gender = (string) $item; if ($gender == '1') { $composites['gender'] = 'male'; } else { if ($gender == '2') { $composites['gender'] = 'female'; } else { $importer->trace('WARNING: gender found but not male or female - no gender stored for this user'); } } } $maharaattributes = PluginImportLeap::get_attributes($item, PluginImportLeap::NS_MAHARA); if (isset($maharaattributes['field'])) { if (in_array($maharaattributes['field'], array('placeofbirth', 'citizenship', 'visastatus', 'maritalstatus'))) { $composites[$maharaattributes['field']] = (string) $item; } } } if ($composites) { $importer->trace('Resume personal information:'); $importer->trace($composites); $artefact = new ArtefactTypePersonalinformation(0, array('owner' => $importer->get('usr'))); foreach ($composites as $key => $value) { $artefact->set_composite($key, $value); } $artefact->commit(); } } }
function send_content_ready($token, $username, $format, $importdata, $fetchnow = false) { global $REMOTEWWWROOT; require_once get_config('docroot') . 'import/lib.php'; list($user, $authinstance) = find_remote_user($username, $REMOTEWWWROOT); if (!$user) { throw new ImportException(null, "Could not find user {$username} for {$REMOTEWWWROOT}"); } // go verify the token if (!($queue = get_record('import_queue', 'token', $token, 'host', $REMOTEWWWROOT))) { throw new ImportException(null, "Could not find queue record with given token for username {$username} for {$REMOTEWWWROOT}"); } if (strtotime($queue->expirytime) < time()) { throw new ImportException(null, "Queue record has expired"); } $class = null; try { $class = PluginImport::class_from_format($format); } catch (Exception $e) { throw new ImportException(null, "Invalid format {$format}"); } $queue->format = $format; if ($class == 'PluginImportLeap') { // don't import persondata over mnet // because it will just silently overwrite stuff // which is not really desirable. $queue->loglevel = get_config('leapovermnetloglevel'); $importdata['skippersondata'] = true; } $queue->data = serialize($importdata); update_record('import_queue', $queue); $tr = new MnetImporterTransport($queue); try { $tr->validate_import_data(); } catch (Exception $e) { throw new ImportException(null, 'Invalid importdata: ' . $e->getMessage()); } if (!array_key_exists('totalsize', $importdata)) { throw new ImportException(null, 'Invalid importdata: missing totalsize'); } if (!$user->quota_allowed($importdata['totalsize'])) { $e = new ImportException(null, 'Exceeded user quota'); $e->set_log_off(); throw $e; } $result = new StdClass(); if ($fetchnow && PluginImport::import_immediately_allowed()) { // either immediately spawn a curl request to go fetch the file $importer = PluginImport::create_importer($queue->id, $tr, $queue); $importer->prepare(); try { $importer->validate_transported_data($tr); } catch (Exception $e) { throw new ImportException(null, 'Invalid importdata: ' . $e->getMessage()); } $importer->process(); $importer->cleanup(); delete_records('import_queue', 'id', $queue->id); $result->status = true; $result->type = 'complete'; $returndata = $importer->get_return_data(); $result->querystring = '?'; foreach ($importer->get_return_data() as $k => $v) { $result->querystring .= $k . '=' . $v . '&'; } $importer->get('importertransport')->cleanup(); } else { // or set ready to 1 for the next cronjob to go fetch it. $result->status = set_field('import_queue', 'ready', 1, 'id', $queue->id); $result->type = 'queued'; } return $result; }
/** * @todo check the rest of the queue table for options * Generate a new import to be queued * * @param int $userid idof user to import for * @param string $plugin plugin to handle the import * not always known at this point * @param string $host wwwroot of mnet host if applicable * @param int $ready whether the import is ready to start (usually no) */ public static function create_new_queue($userid, $plugin = null, $host = null, $ready = 0) { // generate a token, insert it into the queue table $queue = (object) array('token' => generate_token(), 'host' => $host, 'usr' => $userid, 'queue' => (int) (!PluginImport::import_immediately_allowed()), 'ready' => $ready, 'expirytime' => db_format_timestamp(time() + 60 * 60 * 24), 'plugin' => $plugin); $queue->id = insert_record('import_queue', $queue); return $queue; }
/** * Create or update a composite artefact for resume from an import entry request * @param PluginImport $importer * @param unknown_type $entry_request */ private static function create_composite_artefact_from_request(PluginImport $importer, $entry_request) { global $USER; $aid = 0; $classname = generate_artefact_class_name($entry_request->entrytype); $values = unserialize($entry_request->entrycontent); switch ($entry_request->decision) { case PluginImport::DECISION_IGNORE: $duplicatedids = unserialize($entry_request->duplicateditemids); if (!empty($duplicatedids)) { $aid = $duplicatedids[0]; } break; // For composite artefacts, it only makes sense to ignore or replace them, and those are // the only options the form should have shown! To make things less crashy, though, we'll // just default to the same behavior as long as you specified anything other than // DECISION_IGNORE // For composite artefacts, it only makes sense to ignore or replace them, and those are // the only options the form should have shown! To make things less crashy, though, we'll // just default to the same behavior as long as you specified anything other than // DECISION_IGNORE case PluginImport::DECISION_REPLACE: case PluginImport::DECISION_ADDNEW: case PluginImport::DECISION_APPEND: default: $result = ArtefactTypeResumeComposite::ensure_composite_value($values, $entry_request->entrytype, $USER->get('id')); $aid = isset($result['error']) ? 0 : $result['artefactid']; break; } if ($aid) { $importer->add_artefactmapping($entry_request->entryid, $aid); return $aid; } return null; }
/** * Attaches files to blog posts * * We look at the leap relationships to add attachments. Currently this * looks explicitly for the has_attachment relationship. * * If importing an entry resulted in importing a new file (caused by the * entry having out-of-line content), we attach that file to the entry. */ public static function setup_relationships(SimpleXMLElement $entry, PluginImport $importer, $strategy, array $otherentries) { switch ($strategy) { case self::STRATEGY_IMPORT_AS_BLOG: foreach ($otherentries as $entryid) { $blogpostentry = $importer->get_entry_by_id($entryid); // Get all attachments this blogpost things are attached to it // TODO: get all entries that think they're attached to the blogpost. // I think we can only look for files, Mahara doesn't understand // attaching something that isn't a file to a blogpost foreach ($blogpostentry->link as $blogpostlink) { $blogpost = null; if ($importer->curie_equals($blogpostlink['rel'], PluginImportLeap::NS_LEAP, 'has_attachment') && isset($blogpostlink['href'])) { if (!$blogpost) { $artefactids = $importer->get_artefactids_imported_by_entryid((string) $blogpostentry->id); $blogpost = new ArtefactTypeBlogPost($artefactids[0]); } $importer->trace("Attaching file {$blogpostlink['href']} to blog post {$blogpostentry->id}", PluginImportLeap::LOG_LEVEL_VERBOSE); $artefactids = $importer->get_artefactids_imported_by_entryid((string) $blogpostlink['href']); $blogpost->attach_file($artefactids[0]); } if ($blogpost) { $blogpost->commit(); } } self::setup_outoflinecontent_relationship($blogpostentry, $importer); } break; case self::STRATEGY_IMPORT_AS_ENTRY: self::setup_outoflinecontent_relationship($entry, $importer); break; default: throw new ImportException($importer, 'TODO: get_string: unknown strategy chosen for importing entry'); } }
function import_next_user() { global $SESSION, $ADDEDUSERS, $FAILEDUSERS, $LEAP2AFILES, $AUTHINSTANCE; require_once get_config('docroot') . 'import/lib.php'; safe_require('import', 'leap'); // Pop the last element off of the LEAP2AFILES array $filename = end($LEAP2AFILES); $username = key($LEAP2AFILES); unset($LEAP2AFILES[$username]); log_debug('adding user ' . $username . ' from ' . $filename); $authobj = get_record('auth_instance', 'id', $AUTHINSTANCE); $institution = new Institution($authobj->institution); $date = time(); $nicedate = date('Y/m/d h:i:s', $date); $niceuser = preg_replace('/[^a-zA-Z0-9_-]/', '-', $username); $uploaddir = get_config('dataroot') . 'import/' . $niceuser . '-' . $date . '/'; check_dir_exists($uploaddir); // 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) { $FAILEDUSERS[$username] = get_string('unzipfailed', 'admin', hsc($filename)); log_debug("unzip command failed with return value {$returnvar}"); continue; } $leap2afilename = $uploaddir . 'leap2a.xml'; if (!is_file($leap2afilename)) { $FAILEDUSERS[$username] = get_string('noleap2axmlfiledetected', 'admin'); log_debug($FAILEDUSERS[$username]); continue; } // If the username is already taken, append something to the end while (get_record('usr', 'username', $username)) { $username .= "_"; } $user = (object) array('authinstance' => $AUTHINSTANCE, 'username' => $username, 'firstname' => 'Imported', 'lastname' => 'User', 'password' => get_random_key(6), 'passwordchange' => 1); db_begin(); try { $user->id = create_user($user, array(), $institution, $authobj); } catch (EmailException $e) { // Suppress any emails (e.g. new institution membership) sent out // during user creation, becuase the user doesn't have an email // address until we've imported them from the Leap2A file. log_debug("Failed sending email during user import"); } $importerfilename = substr($leap2afilename, strlen(get_config('dataroot'))); $logfile = dirname($leap2afilename) . '/import.log'; $importer = PluginImport::create_importer(null, (object) array('token' => '', 'usr' => $user->id, 'queue' => (int) (!PluginImport::import_immediately_allowed()), 'ready' => 0, 'expirytime' => db_format_timestamp(time() + 60 * 60 * 24), 'format' => 'leap', 'data' => array('filename' => $importerfilename), 'loglevel' => PluginImportLeap::LOG_LEVEL_VERBOSE, 'logtargets' => LOG_TARGET_FILE, 'logfile' => $logfile, 'profile' => true)); try { $importer->process(); log_info("Imported user account {$user->id} from Leap2A file, see {$logfile} for a full log"); } catch (ImportException $e) { log_info("Leap2A import failed: " . $e->getMessage()); $FAILEDUSERS[$username] = get_string("leap2aimportfailed"); db_rollback(); continue; } db_commit(); // Reload the user details, as various fields are changed by the // importer when importing (e.g. firstname/lastname) $ADDEDUSERS[] = get_record('usr', 'id', $user->id); $SESSION->set('bulkimport_leap2afiles', $LEAP2AFILES); $SESSION->set('bulkimport_addedusers', $ADDEDUSERS); $SESSION->set('bulkimport_failedusers', $FAILEDUSERS); meta_redirect(); }
function import_submit(Pieform $form, $values) { global $USER, $TRANSPORTER, $IMPORTER; safe_require('import', 'leap'); $importdata = (object) array('token' => '', 'usr' => $USER->get('id'), 'queue' => (int) false, 'ready' => 0, 'expirytime' => db_format_timestamp(time() + 60 * 60 * 24), 'format' => 'leap', 'loglevel' => PluginImportLeap::LOG_LEVEL_STANDARD, 'logtargets' => LOG_TARGET_FILE, 'profile' => true); $IMPORTER = PluginImport::create_importer(null, $TRANSPORTER, $importdata); try { $IMPORTER->process(PluginImport::STEP_INTERACTIVE_IMPORT_FORM); } catch (ImportException $e) { log_info("Leap2A import failed: " . $e->getMessage()); die_info(get_string('importfailed', 'import')); } set_importer_to_session(); redirect('/import/index.php?action=' . PRINTIMPORTITEMSFORM_ACT); }
/** * Creates an artefact in the manner required to overwrite existing profile * artefacts * * @param PluginImport $importer The importer * @param string $artefacttype The type of artefact to create * @param string $title The title for the artefact (with profile * fields, this is the main data) * @return int The ID of the artefact created */ private static function create_artefact(PluginImport $importer, $artefacttype, $title) { $classname = 'ArtefactType' . ucfirst($artefacttype); $artefact = new $classname(0, array('owner' => $importer->get('usr'))); $artefact->set('title', $title); $artefact->commit(); return $artefact->get('id'); }