00001 <?php
00002
00016
00017
00018 import('xml.XMLParser');
00019
00020 class UserXMLParser {
00021
00023 var $parser;
00024
00026 var $usersToImport;
00027
00029 var $importedUsers;
00030
00032 var $errors;
00033
00035 var $schedConfId;
00036
00038 var $conferenceId;
00039
00044 function UserXMLParser($conferenceId, $schedConfId) {
00045 $this->parser = new XMLParser();
00046 $this->conferenceId = $conferenceId;
00047 $this->schedConfId = $schedConfId;
00048 }
00049
00055 function &parseData($file) {
00056 $roleDao =& DAORegistry::getDAO('RoleDAO');
00057
00058 $success = true;
00059 $this->usersToImport = array();
00060 $tree = $this->parser->parse($file);
00061
00062 $schedConfDao =& DAORegistry::getDAO('SchedConfDAO');
00063 $schedConf =& $schedConfDao->getSchedConf($this->schedConfId);
00064 $schedConfPrimaryLocale = AppLocale::getPrimaryLocale();
00065
00066 $site =& Request::getSite();
00067 $siteSupportedLocales = $site->getSupportedLocales();
00068
00069 if ($tree !== false) {
00070 foreach ($tree->getChildren() as $user) {
00071 if ($user->getName() == 'user') {
00072
00073 $newUser = new ImportedUser();
00074
00075 foreach ($user->getChildren() as $attrib) {
00076 switch ($attrib->getName()) {
00077 case 'username':
00078
00079 $newUser->setUsername(strtolower($attrib->getValue()));
00080 break;
00081 case 'password':
00082 $newUser->setMustChangePassword($attrib->getAttribute('change') == 'true'?1:0);
00083 $encrypted = $attrib->getAttribute('encrypted');
00084 if (isset($encrypted) && $encrypted !== 'plaintext') {
00085 $ocsEncryptionScheme = Config::getVar('security', 'encryption');
00086 if ($encrypted != $ocsEncryptionScheme) {
00087 $this->errors[] = __('plugins.importexport.users.import.encryptionMismatch', array('importHash' => $encrypted, 'ocsHash' => $ocsEncryptionScheme));
00088 }
00089 $newUser->setPassword($attrib->getValue());
00090 } else {
00091 $newUser->setUnencryptedPassword($attrib->getValue());
00092 }
00093 break;
00094 case 'salutation':
00095 $newUser->setSalutation($attrib->getValue());
00096 break;
00097 case 'first_name':
00098 $newUser->setFirstName($attrib->getValue());
00099 break;
00100 case 'middle_name':
00101 $newUser->setMiddleName($attrib->getValue());
00102 break;
00103 case 'last_name':
00104 $newUser->setLastName($attrib->getValue());
00105 break;
00106 case 'initials':
00107 $newUser->setInitials($attrib->getValue());
00108 break;
00109 case 'gender':
00110 $newUser->setGender($attrib->getValue());
00111 break;
00112 case 'affiliation':
00113 $newUser->setAffiliation($attrib->getValue());
00114 break;
00115 case 'email':
00116 $newUser->setEmail($attrib->getValue());
00117 break;
00118 case 'url':
00119 $newUser->setUrl($attrib->getValue());
00120 break;
00121 case 'phone':
00122 $newUser->setPhone($attrib->getValue());
00123 break;
00124 case 'fax':
00125 $newUser->setFax($attrib->getValue());
00126 break;
00127 case 'mailing_address':
00128 $newUser->setMailingAddress($attrib->getValue());
00129 break;
00130 case 'country':
00131 $newUser->setCountry($attrib->getValue());
00132 break;
00133 case 'signature':
00134 $locale = $attrib->getAttribute('locale');
00135 if (empty($locale)) $locale = $schedConfPrimaryLocale;
00136 $newUser->setSignature($attrib->getValue(), $locale);
00137 break;
00138 case 'interests':
00139 $locale = $attrib->getAttribute('locale');
00140 if (empty($locale)) $locale = $schedConfPrimaryLocale;
00141 $newUser->setInterests($attrib->getValue(), $locale);
00142 break;
00143 case 'gossip':
00144 $locale = $attrib->getAttribute('locale');
00145 if (empty($locale)) $locale = $schedConfPrimaryLocale;
00146 $newUser->setGossip($attrib->getValue(), $locale);
00147 break;
00148 case 'biography':
00149 $locale = $attrib->getAttribute('locale');
00150 if (empty($locale)) $locale = $schedConfPrimaryLocale;
00151 $newUser->setBiography($attrib->getValue(), $locale);
00152 break;
00153 case 'locales':
00154 $locales = array();
00155 foreach (explode(':', $attrib->getValue()) as $locale) {
00156 if (AppLocale::isLocaleValid($locale) && in_array($locale, $siteSupportedLocales)) {
00157 array_push($locales, $locale);
00158 }
00159 }
00160 $newUser->setLocales($locales);
00161 break;
00162 case 'role':
00163 $roleType = $attrib->getAttribute('type');
00164 if ($this->validRole($roleType)) {
00165 $role = new Role();
00166 $role->setRoleId($roleDao->getRoleIdFromPath($roleType));
00167 $newUser->addRole($role);
00168 }
00169 break;
00170 }
00171 }
00172 array_push($this->usersToImport, $newUser);
00173 }
00174 }
00175 }
00176
00177 return $this->usersToImport;
00178 }
00179
00186 function importUsers($sendNotify = false, $continueOnError = false) {
00187 $success = true;
00188 $this->importedUsers = array();
00189 $this->errors = array();
00190
00191 $userDao =& DAORegistry::getDAO('UserDAO');
00192 $roleDao =& DAORegistry::getDAO('RoleDAO');
00193
00194 if ($sendNotify) {
00195
00196 import('mail.MailTemplate');
00197 $mail = new MailTemplate('USER_REGISTER');
00198
00199 $schedConfDao =& DAORegistry::getDAO('SchedConfDAO');
00200 $schedConf =& $schedConfDao->getSchedConf($this->schedConfId);
00201 $mail->setFrom($schedConf->getSetting('contactEmail'), $schedConf->getSetting('contactName'));
00202 }
00203
00204 for ($i=0, $count=count($this->usersToImport); $i < $count; $i++) {
00205 $user =& $this->usersToImport[$i];
00206
00207
00208 if ($user->getEmail() != null) {
00209 $emailExists = $userDao->getUserByEmail($user->getEmail(), true);
00210 if ($emailExists != null) {
00211 $user->setUsername($emailExists->getUsername());
00212 }
00213 }
00214 if ($user->getUsername() == null) {
00215 $newUsername = true;
00216 $this->generateUsername($user);
00217 } else {
00218 $newUsername = false;
00219 }
00220 if ($user->getUnencryptedPassword() != null) {
00221 $user->setPassword(Validation::encryptCredentials($user->getUsername(), $user->getUnencryptedPassword()));
00222 } else if ($user->getPassword() == null) {
00223 $this->generatePassword($user);
00224 }
00225
00226 if (!$newUsername) {
00227
00228 $userExists = $userDao->getUserByUsername($user->getUsername(), true);
00229 if ($userExists != null) {
00230 $user->setId($userExists->getId());
00231 }
00232 } else {
00233 $userExists = false;
00234 }
00235
00236 if ($newUsername || !$userExists) {
00237
00238
00239
00240 if (!$userDao->insertUser($user)) {
00241
00242 $this->errors[] = sprintf('%s: %s (%s)',
00243 __('manager.people.importUsers.failedToImportUser'),
00244 $user->getFullName(), $user->getUsername());
00245
00246 if ($continueOnError) {
00247
00248 $success = false;
00249 continue;
00250 } else {
00251 return false;
00252 }
00253 }
00254 }
00255
00256
00257
00258 foreach ($user->getRoles() as $role) {
00259 $role->setUserId($user->getId());
00260 $role->setConferenceId($this->conferenceId);
00261 $role->setSchedConfId($this->schedConfId);
00262 if (!$roleDao->roleExists($role->getConferenceId(), $role->getSchedConfId(), $role->getUserId(), $role->getRoleId())) {
00263 if (!$roleDao->insertRole($role)) {
00264
00265 $this->errors[] = sprintf('%s: %s - %s (%s)',
00266 __('manager.people.importUsers.failedToImportRole'),
00267 $role->getRoleName(),
00268 $user->getFullName(), $user->getUsername());
00269
00270 if ($continueOnError) {
00271
00272 $success = false;
00273 continue;
00274 } else {
00275 return false;
00276 }
00277 }
00278 }
00279 }
00280
00281 if ($sendNotify && !$userExists) {
00282
00283 $mail->addRecipient($user->getEmail(), $user->getFullName());
00284 $mail->sendWithParams(array(
00285 'username' => $user->getUsername(),
00286 'password' => $user->getUnencryptedPassword() == null ? '-' : $user->getUnencryptedPassword(),
00287 'userFullName' => $user->getFullName()
00288 ));
00289 $mail->clearRecipients();
00290 }
00291
00292 array_push($this->importedUsers, $user);
00293 }
00294
00295 return $success;
00296 }
00297
00302 function &getUsersToImport() {
00303 return $this->usersToImport;
00304 }
00305
00310 function setUsersToImport($users) {
00311 $this->usersToImport = $users;
00312 }
00313
00318 function &getImportedUsers() {
00319 return $this->importedUsers;
00320 }
00321
00326 function &getErrors() {
00327 return $this->errors;
00328 }
00329
00336 function validRole($roleType) {
00337 return isset($roleType) && in_array($roleType, array('manager', 'director', 'trackDirector', 'reviewer', 'author', 'reader'));
00338 }
00339
00344 function generateUsername(&$user) {
00345 $userDao =& DAORegistry::getDAO('UserDAO');
00346 $baseUsername = String::regexp_replace('/[^A-Z0-9]/i', '', $user->getLastName());
00347 if (empty($baseUsername)) {
00348 $baseUsername = String::regexp_replace('/[^A-Z0-9]/i', '', $user->getFirstName());
00349 }
00350 if (empty($username)) {
00351
00352 $baseUsername = 'user';
00353 }
00354
00355 for ($username = $baseUsername, $i=1; $userDao->userExistsByUsername($username, true); $i++) {
00356 $username = $baseUsername . $i;
00357 }
00358 $user->setUsername($username);
00359 }
00360
00365 function generatePassword(&$user) {
00366 $password = Validation::generatePassword();
00367 $user->setUnencryptedPassword($password);
00368 $user->setPassword(Validation::encryptCredentials($user->getUsername(), $password));
00369 }
00370
00371 }
00372
00373
00377 import('user.User');
00378 class ImportedUser extends User {
00379
00381 var $roles;
00382
00386 function ImportedUser() {
00387 $this->roles = array();
00388 parent::User();
00389 }
00390
00395 function setUnencryptedPassword($unencryptedPassword) {
00396 $this->setData('unencryptedPassword', $unencryptedPassword);
00397 }
00398
00403 function getUnencryptedPassword() {
00404 return $this->getData('unencryptedPassword');
00405 }
00406
00411 function addRole(&$role) {
00412 array_push($this->roles, $role);
00413 }
00414
00419 function &getRoles() {
00420 return $this->roles;
00421 }
00422
00423 }
00424
00425 ?>