00001 <?php
00002
00018 import('lib.pkp.classes.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 $pressId;
00036
00041 function UserXMLParser($pressId) {
00042 $this->parser = new XMLParser();
00043 $this->pressId = $pressId;
00044 }
00045
00051 function &parseData($file) {
00052 $roleDao =& DAORegistry::getDAO('RoleDAO');
00053
00054 $success = true;
00055 $this->usersToImport = array();
00056 $tree = $this->parser->parse($file);
00057
00058 $pressDao =& DAORegistry::getDAO('PressDAO');
00059 $press =& $pressDao->getById($this->pressId);
00060 $pressPrimaryLocale = AppLocale::getPrimaryLocale();
00061
00062 $site =& Request::getSite();
00063 $siteSupportedLocales = $site->getSupportedLocales();
00064
00065 if ($tree !== false) {
00066 foreach ($tree->getChildren() as $user) {
00067 if ($user->getName() == 'user') {
00068
00069 $newUser = new ImportedUser();
00070
00071 foreach ($user->getChildren() as $attrib) {
00072 switch ($attrib->getName()) {
00073 case 'username':
00074
00075 $newUser->setUsername(strtolower($attrib->getValue()));
00076 break;
00077 case 'password':
00078 $newUser->setMustChangePassword($attrib->getAttribute('change') == 'true'?1:0);
00079 $encrypted = $attrib->getAttribute('encrypted');
00080 if (isset($encrypted) && $encrypted !== 'plaintext') {
00081 $ompEncryptionScheme = Config::getVar('security', 'encryption');
00082 if ($encrypted != $ompEncryptionScheme) {
00083 $this->errors[] = __('plugins.importexport.users.import.encryptionMismatch', array('importHash' => $encrypted, 'ompHash' => $ompEncryptionScheme));
00084 }
00085 $newUser->setPassword($attrib->getValue());
00086 } else {
00087 $newUser->setUnencryptedPassword($attrib->getValue());
00088 }
00089 break;
00090 case 'salutation':
00091 $newUser->setSalutation($attrib->getValue());
00092 break;
00093 case 'first_name':
00094 $newUser->setFirstName($attrib->getValue());
00095 break;
00096 case 'middle_name':
00097 $newUser->setMiddleName($attrib->getValue());
00098 break;
00099 case 'last_name':
00100 $newUser->setLastName($attrib->getValue());
00101 break;
00102 case 'initials':
00103 $newUser->setInitials($attrib->getValue());
00104 break;
00105 case 'gender':
00106 $newUser->setGender($attrib->getValue());
00107 break;
00108 case 'affiliation':
00109 $locale = $attrib->getAttribute('locale');
00110 if (empty($locale)) $locale = $pressPrimaryLocale;
00111 $newUser->setAffiliation($attrib->getValue(), $locale);
00112 break;
00113 case 'email':
00114 $newUser->setEmail($attrib->getValue());
00115 break;
00116 case 'url':
00117 $newUser->setUrl($attrib->getValue());
00118 break;
00119 case 'phone':
00120 $newUser->setPhone($attrib->getValue());
00121 break;
00122 case 'fax':
00123 $newUser->setFax($attrib->getValue());
00124 break;
00125 case 'mailing_address':
00126 $newUser->setMailingAddress($attrib->getValue());
00127 break;
00128 case 'country':
00129 $newUser->setCountry($attrib->getValue());
00130 break;
00131 case 'signature':
00132 $locale = $attrib->getAttribute('locale');
00133 if (empty($locale)) $locale = $pressPrimaryLocale;
00134 $newUser->setSignature($attrib->getValue(), $locale);
00135 break;
00136 case 'interests':
00137 $locale = $attrib->getAttribute('locale');
00138 if (empty($locale)) $locale = $pressPrimaryLocale;
00139 $newUser->setInterests($attrib->getValue(), $locale);
00140 break;
00141 case 'gossip':
00142 $locale = $attrib->getAttribute('locale');
00143 if (empty($locale)) $locale = $pressPrimaryLocale;
00144 $newUser->setGossip($attrib->getValue(), $locale);
00145 break;
00146 case 'biography':
00147 $locale = $attrib->getAttribute('locale');
00148 if (empty($locale)) $locale = $pressPrimaryLocale;
00149 $newUser->setBiography($attrib->getValue(), $locale);
00150 break;
00151 case 'locales':
00152 $locales = array();
00153 foreach (explode(':', $attrib->getValue()) as $locale) {
00154 if (AppLocale::isLocaleValid($locale) && in_array($locale, $siteSupportedLocales)) {
00155 array_push($locales, $locale);
00156 }
00157 }
00158 $newUser->setLocales($locales);
00159 break;
00160 case 'role':
00161 $roleType = $attrib->getAttribute('type');
00162 if ($this->validRole($roleType)) {
00163 $role = new Role();
00164 $role->setRoleId($roleDao->getRoleIdFromPath($roleType));
00165 $newUser->addRole($role);
00166 }
00167 break;
00168 }
00169 }
00170 array_push($this->usersToImport, $newUser);
00171 }
00172 }
00173 }
00174
00175 return $this->usersToImport;
00176 }
00177
00184 function importUsers($sendNotify = false, $continueOnError = false) {
00185 $success = true;
00186 $this->importedUsers = array();
00187 $this->errors = array();
00188 $pressDao =& DAORegistry::getDAO('PressDAO');
00189 $press =& $pressDao->getById($this->pressId);
00190
00191 $userDao =& DAORegistry::getDAO('UserDAO');
00192 $roleDao =& DAORegistry::getDAO('RoleDAO');
00193
00194 if ($sendNotify) {
00195
00196 import('classes.mail.MailTemplate');
00197 $mail = new MailTemplate('USER_REGISTER');
00198
00199 $mail->setFrom($press->getSetting('contactEmail'), $press->getSetting('contactName'));
00200 }
00201
00202 for ($i=0, $count=count($this->usersToImport); $i < $count; $i++) {
00203 $user =& $this->usersToImport[$i];
00204
00205
00206 if ($user->getEmail() != null) {
00207 $emailExists = $userDao->getUserByEmail($user->getEmail(), true);
00208 if ($emailExists != null) {
00209 $user->setUsername($emailExists->getUsername());
00210 }
00211 }
00212 if ($user->getUsername() == null) {
00213 $newUsername = true;
00214 $this->generateUsername($user);
00215 } else {
00216 $newUsername = false;
00217 }
00218 if ($user->getUnencryptedPassword() != null) {
00219 $user->setPassword(Validation::encryptCredentials($user->getUsername(), $user->getUnencryptedPassword()));
00220 } else if ($user->getPassword() == null) {
00221 $this->generatePassword($user);
00222 }
00223
00224 if (!$newUsername) {
00225
00226 $userExists = $userDao->getByUsername($user->getUsername(), true);
00227 if ($userExists != null) {
00228 $user->setId($userExists->getId());
00229 }
00230 } else {
00231 $userExists = false;
00232 }
00233
00234 if ($newUsername || !$userExists) {
00235
00236
00237
00238 if (!$userDao->insertUser($user)) {
00239
00240 $this->errors[] = sprintf('%s: %s (%s)',
00241 __('manager.people.importUsers.failedToImportUser'),
00242 $user->getFullName(), $user->getUsername());
00243
00244 if ($continueOnError) {
00245
00246 $success = false;
00247 continue;
00248 } else {
00249 return false;
00250 }
00251 }
00252 }
00253
00254
00255
00256
00257 $userGroupDao =& DAORegistry::getDAO('UserGroupDAO');
00258
00259 foreach ($user->getRoles() as $role) {
00260 $userGroupIds =& $userGroupDao->getUserGroupIdsByRoleId($role->getRoleId(), $press->getId());
00261 foreach ($userGroupIds as $userGroupId) {
00262 if (!$userGroupDao->userInGroup($user->getId(), $userGroupId)) {
00263 if (!$userGroupDao->assignUserToGroup($user->getId(), $userGroupId, $press->getId())) {
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
00282 if ($sendNotify && !$userExists) {
00283
00284 $mail->addRecipient($user->getEmail(), $user->getFullName());
00285 $mail->sendWithParams(array(
00286 'pressName' => $press->getTitle($press->getPrimaryLocale()),
00287 'username' => $user->getUsername(),
00288 'password' => $user->getUnencryptedPassword() == null ? '-' : $user->getUnencryptedPassword(),
00289 'userFullName' => $user->getFullName()
00290 ));
00291 $mail->clearRecipients();
00292 }
00293
00294 array_push($this->importedUsers, $user);
00295 }
00296
00297 return $success;
00298 }
00299
00304 function &getUsersToImport() {
00305 return $this->usersToImport;
00306 }
00307
00312 function setUsersToImport($users) {
00313 $this->usersToImport = $users;
00314 }
00315
00320 function &getImportedUsers() {
00321 return $this->importedUsers;
00322 }
00323
00328 function &getErrors() {
00329 return $this->errors;
00330 }
00331
00338 function validRole($roleType) {
00339 return isset($roleType) && in_array($roleType, array('manager', 'editor', 'seriesEditor', 'productionEditor', 'reviewer', 'copyeditor', 'proofreader', 'author', 'reader'));
00340 }
00341
00346 function generateUsername(&$user) {
00347 $userDao =& DAORegistry::getDAO('UserDAO');
00348 $baseUsername = String::regexp_replace('/[^A-Z0-9]/i', '', $user->getLastName());
00349 if (empty($baseUsername)) {
00350 $baseUsername = String::regexp_replace('/[^A-Z0-9]/i', '', $user->getFirstName());
00351 }
00352 if (empty($username)) {
00353
00354 $baseUsername = 'user';
00355 }
00356
00357 for ($username = $baseUsername, $i=1; $userDao->userExistsByUsername($username, true); $i++) {
00358 $username = $baseUsername . $i;
00359 }
00360 $user->setUsername($username);
00361 }
00362
00367 function generatePassword(&$user) {
00368 $password = Validation::generatePassword();
00369 $user->setUnencryptedPassword($password);
00370 $user->setPassword(Validation::encryptCredentials($user->getUsername(), $password));
00371 }
00372
00373 }
00374
00375
00379 import('classes.user.User');
00380 class ImportedUser extends User {
00381
00383 var $roles;
00384
00388 function ImportedUser() {
00389 $this->roles = array();
00390 parent::User();
00391 }
00392
00397 function setUnencryptedPassword($unencryptedPassword) {
00398 $this->setData('unencryptedPassword', $unencryptedPassword);
00399 }
00400
00405 function getUnencryptedPassword() {
00406 return $this->getData('unencryptedPassword');
00407 }
00408
00413 function addRole(&$role) {
00414 array_push($this->roles, $role);
00415 }
00416
00421 function &getRoles() {
00422 return $this->roles;
00423 }
00424
00425 }
00426
00427 ?>