示例#1
0
 /** Settings page action */
 public function settingsAction()
 {
     if (!$this->logged || $this->isDemoMode()) {
         $this->disableView();
         return false;
     }
     $userId = $this->getParam('userId');
     if (isset($userId) && $userId != $this->userSession->Dao->getKey() && !$this->userSession->Dao->isAdmin()) {
         throw new Zend_Exception(MIDAS_ADMIN_PRIVILEGES_REQUIRED);
     } elseif (isset($userId)) {
         $userDao = $this->User->load($userId);
     } else {
         $userDao = $this->userSession->Dao;
     }
     if (empty($userDao) || $userDao == false) {
         throw new Zend_Exception('Unable to load user');
     }
     $notifications = Zend_Registry::get('notifier')->callback('CALLBACK_CORE_ALLOW_PASSWORD_CHANGE', array('user' => $userDao, 'currentUser' => $this->userSession->Dao));
     $this->view->allowPasswordChange = true;
     foreach ($notifications as $allow) {
         if ($allow['allow'] === false) {
             $this->view->allowPasswordChange = false;
             break;
         }
     }
     $defaultValue = array();
     $defaultValue['email'] = $userDao->getEmail();
     $defaultValue['firstname'] = $userDao->getFirstname();
     $defaultValue['lastname'] = $userDao->getLastname();
     $defaultValue['company'] = $userDao->getCompany();
     $defaultValue['privacy'] = $userDao->getPrivacy();
     $defaultValue['city'] = $userDao->getCity();
     $defaultValue['country'] = $userDao->getCountry();
     $defaultValue['website'] = $userDao->getWebsite();
     $defaultValue['biography'] = $userDao->getBiography();
     $accountForm = $this->Form->User->createAccountForm($defaultValue);
     $this->view->accountForm = $this->getFormAsArray($accountForm);
     $this->view->prependFields = array();
     $this->view->appendFields = array();
     $moduleFields = Zend_Registry::get('notifier')->callback('CALLBACK_CORE_USER_PROFILE_FIELDS', array('user' => $userDao, 'currentUser' => $this->userSession->Dao));
     foreach ($moduleFields as $field) {
         if (isset($field['position']) && $field['position'] == 'top') {
             $this->view->prependFields[] = $field;
         } else {
             $this->view->appendFields[] = $field;
         }
     }
     if ($this->_request->isPost()) {
         $this->disableView();
         $this->disableLayout();
         $submitPassword = $this->getParam('modifyPassword');
         $modifyAccount = $this->getParam('modifyAccount');
         $modifyPicture = $this->getParam('modifyPicture');
         $modifyPictureGravatar = $this->getParam('modifyPictureGravatar');
         if (isset($submitPassword) && $this->logged) {
             if (!$this->view->allowPasswordChange) {
                 throw new Zend_Exception('Changing password is disallowed for this user');
             }
             $oldPass = $this->getParam('oldPassword');
             if ($userDao->getSalt() == '') {
                 $this->User->convertLegacyPasswordHash($userDao, $oldPass);
             }
             $newPass = $this->getParam('newPassword');
             $instanceSalt = Zend_Registry::get('configGlobal')->password->prefix;
             $hashedPasswordOld = hash($userDao->getHashAlg(), $instanceSalt . $userDao->getSalt() . $oldPass);
             if (!$userDao->isAdmin() && $this->userSession->Dao->isAdmin() || $this->User->hashExists($hashedPasswordOld)) {
                 $this->User->changePassword($userDao, $newPass);
                 if (!isset($userId)) {
                     $this->userSession->Dao = $userDao;
                 }
                 echo JsonComponent::encode(array(true, $this->t('Changes saved')));
                 Zend_Registry::get('notifier')->callback('CALLBACK_CORE_PASSWORD_CHANGED', array('userDao' => $userDao, 'password' => $newPass));
             } else {
                 echo JsonComponent::encode(array(false, $this->t('The old password is incorrect')));
                 return;
             }
         }
         if (isset($modifyAccount) && $this->logged) {
             $newEmail = trim($this->getParam('email'));
             $firtname = trim($this->getParam('firstname'));
             $lastname = trim($this->getParam('lastname'));
             $company = trim($this->getParam('company'));
             $privacy = $this->getParam('privacy');
             $city = $this->getParam('city');
             $country = $this->getParam('country');
             $website = $this->getParam('website');
             $biography = $this->getParam('biography');
             if (!$accountForm->isValid($this->getRequest()->getPost())) {
                 echo JsonComponent::encode(array(false, 'Invalid form value'));
                 return;
             }
             $userDao = $this->User->load($userDao->getKey());
             if (!isset($privacy) || $privacy != MIDAS_USER_PRIVATE && $privacy != MIDAS_USER_PUBLIC) {
                 echo JsonComponent::encode(array(false, 'Error: invalid privacy flag'));
                 return;
             }
             if (!isset($lastname) || !isset($firtname) || empty($lastname) || empty($firtname)) {
                 echo JsonComponent::encode(array(false, 'Error: First and last name required'));
                 return;
             }
             if ($newEmail != $userDao->getEmail()) {
                 $existingUser = $this->User->getByEmail($newEmail);
                 if ($existingUser) {
                     echo JsonComponent::encode(array(false, 'Error: that email address belongs to another account'));
                     return;
                 }
                 $userDao->setEmail($newEmail);
             }
             $userDao->setFirstname($firtname);
             $userDao->setLastname($lastname);
             if (isset($company)) {
                 $userDao->setCompany($company);
             }
             if (isset($city)) {
                 $userDao->setCity($city);
             }
             if (isset($country)) {
                 $userDao->setCountry($country);
             }
             if (isset($website)) {
                 $userDao->setWebsite($website);
             }
             if (isset($biography)) {
                 $userDao->setBiography($biography);
             }
             $userDao->setPrivacy($privacy);
             if ($this->userSession->Dao->isAdmin() && $this->userSession->Dao->getKey() != $userDao->getKey()) {
                 $adminStatus = (bool) $this->getParam('adminStatus');
                 $userDao->setAdmin($adminStatus ? 1 : 0);
             }
             $this->User->save($userDao);
             if (!isset($userId)) {
                 $this->userSession->Dao = $userDao;
             }
             try {
                 Zend_Registry::get('notifier')->callback('CALLBACK_CORE_USER_SETTINGS_CHANGED', array('user' => $userDao, 'currentUser' => $this->userSession->Dao, 'fields' => $this->getAllParams()));
             } catch (Exception $e) {
                 echo JsonComponent::encode(array(false, $e->getMessage()));
                 return;
             }
             echo JsonComponent::encode(array(true, $this->t('Changes saved')));
         }
         if (isset($modifyPicture) && $this->logged) {
             if ($this->isTestingEnv()) {
                 // simulate file upload
                 $path = BASE_PATH . '/tests/testfiles/search.png';
                 $size = filesize($path);
                 $mime = 'image/png';
             } else {
                 $mime = $_FILES['file']['type'];
                 $upload = new Zend_File_Transfer();
                 $upload->receive();
                 $path = $upload->getFileName();
                 $size = $upload->getFileSize();
             }
             if (!empty($path) && file_exists($path) && $size > 0) {
                 if (file_exists($path) && $mime == 'image/jpeg') {
                     try {
                         $src = imagecreatefromjpeg($path);
                     } catch (Exception $exc) {
                         echo JsonComponent::encode(array(false, 'Error: Unable to read jpg file'));
                         return;
                     }
                 } elseif (file_exists($path) && $mime == 'image/png') {
                     try {
                         $src = imagecreatefrompng($path);
                     } catch (Exception $exc) {
                         echo JsonComponent::encode(array(false, 'Error: Unable to read png file'));
                         return;
                     }
                 } elseif (file_exists($path) && $mime == 'image/gif') {
                     try {
                         $src = imagecreatefromgif($path);
                     } catch (Exception $exc) {
                         echo JsonComponent::encode(array(false, 'Error: Unable to read gif file'));
                         return;
                     }
                 } else {
                     echo JsonComponent::encode(array(false, 'Error: wrong format'));
                     return;
                 }
                 $tmpPath = $this->getDataDirectory('thumbnail') . '/' . $this->Component->Random->generateInt();
                 if (!file_exists($this->getDataDirectory('thumbnail'))) {
                     throw new Zend_Exception('Thumbnail path does not exist: ' . $this->getDataDirectory('thumbnail'));
                 }
                 if (!file_exists($tmpPath)) {
                     mkdir($tmpPath);
                 }
                 $tmpPath .= '/' . $this->Component->Random->generateInt();
                 if (!file_exists($tmpPath)) {
                     mkdir($tmpPath);
                 }
                 $destination = $tmpPath . '/' . $this->Component->Random->generateInt() . '.jpg';
                 while (file_exists($destination)) {
                     $destination = $tmpPath . '/' . $this->Component->Random->generateInt() . '.jpg';
                 }
                 $pathThumbnail = $destination;
                 list($x, $y) = getimagesize($path);
                 //--- get size of img ---
                 $thumb = 32;
                 //--- max. size of thumb ---
                 if ($x > $y) {
                     $tx = $thumb;
                     //--- landscape ---
                     $ty = round($thumb / $x * $y);
                 } else {
                     $tx = round($thumb / $y * $x);
                     //--- portrait ---
                     $ty = $thumb;
                 }
                 $thb = imagecreatetruecolor($tx, $ty);
                 //--- create thumbnail ---
                 imagecopyresampled($thb, $src, 0, 0, 0, 0, $tx, $ty, $x, $y);
                 imagejpeg($thb, $pathThumbnail, 80);
                 imagedestroy($thb);
                 imagedestroy($src);
                 if (file_exists($pathThumbnail)) {
                     $userDao = $this->User->load($userDao->getKey());
                     $oldThumbnail = $userDao->getThumbnail();
                     if (!empty($oldThumbnail) && file_exists(BASE_PATH . '/' . $oldThumbnail)) {
                         unlink(BASE_PATH . '/' . $oldThumbnail);
                     }
                     $userDao->setThumbnail(substr($pathThumbnail, strlen(BASE_PATH) + 1));
                     $this->User->save($userDao);
                     if (!isset($userId)) {
                         $this->userSession->Dao = $userDao;
                     }
                     echo JsonComponent::encode(array(true, $this->t('Changes saved'), $this->view->webroot . '/' . $userDao->getThumbnail()));
                 } else {
                     echo JsonComponent::encode(array(false, 'Error'));
                     return;
                 }
             }
             if (isset($modifyPictureGravatar) && $this->logged) {
                 $gravatarUrl = $this->User->getGravatarUrl($userDao->getEmail());
                 if ($gravatarUrl != false) {
                     $userDao = $this->User->load($userDao->getKey());
                     $oldThumbnail = $userDao->getThumbnail();
                     if (!empty($oldThumbnail) && file_exists(BASE_PATH . '/' . $oldThumbnail)) {
                         unlink(BASE_PATH . '/' . $oldThumbnail);
                     }
                     $userDao->setThumbnail($gravatarUrl);
                     $this->User->save($userDao);
                     if (!isset($userId)) {
                         $this->userSession->Dao = $userDao;
                     }
                     echo JsonComponent::encode(array(true, $this->t('Changes saved'), $userDao->getThumbnail()));
                 } else {
                     echo JsonComponent::encode(array(false, 'Error'));
                 }
             }
         }
     }
     $communities = array();
     $groups = $userDao->getGroups();
     foreach ($groups as $group) {
         $community = $group->getCommunity();
         if (!isset($communities[$community->getKey()])) {
             $community->groups = array();
             $communities[$community->getKey()] = $community;
         }
         $communities[$community->getKey()]->groups[] = $group;
     }
     $this->Component->Sortdao->field = 'name';
     $this->Component->Sortdao->order = 'asc';
     usort($communities, array($this->Component->Sortdao, 'sortByName'));
     $this->view->useGravatar = Zend_Registry::get('configGlobal')->gravatar;
     $this->view->isGravatar = $this->User->getGravatarUrl($userDao->getEmail());
     $this->view->communities = $communities;
     $this->view->user = $userDao;
     $this->view->currentUser = $this->userSession->Dao;
     $this->view->thumbnail = $userDao->getThumbnail();
     $this->view->jsonSettings = array();
     $this->view->jsonSettings['accountErrorFirstname'] = $this->t('Please set your firstname');
     $this->view->jsonSettings['accountErrorLastname'] = $this->t('Please set your lastname');
     $this->view->jsonSettings['passwordErrorShort'] = $this->t('Password too short');
     $this->view->jsonSettings['passwordErrorMatch'] = $this->t('The passwords are not the same');
     $this->view->jsonSettings = JsonComponent::encode($this->view->jsonSettings);
     $this->view->customTabs = Zend_Registry::get('notifier')->callback('CALLBACK_CORE_GET_CONFIG_TABS', array('user' => $userDao));
     $breadcrumbs = array();
     $breadcrumbs[] = array('type' => 'user', 'object' => $userDao);
     $breadcrumbs[] = array('type' => 'custom', 'text' => 'My Account', 'icon' => $this->view->coreWebroot . '/public/images/icons/edit.png');
     $this->Component->Breadcrumb->setBreadcrumbHeader($breadcrumbs, $this->view);
 }