requireUser(WEB_ROOT.'/login.'.SITE_CONFIG_PAGE_EXTENSION);
// load user
$user = UserPeer::loadUserById($Auth->id);
if (!$user)
{
coreFunctions::redirect(WEB_ROOT);
}
// setup page
define("PAGE_NAME", t("account_edit_page_name", "Account Details"));
define("PAGE_DESCRIPTION", t("account_edit_meta_description", "Account details"));
define("PAGE_KEYWORDS", t("account_edit_meta_keywords", "details, account, short, url, user"));
define("SELECTED_NAVIGATION_LINK", "settings");
// dropdowns
$watermarkPositionOptions = array();
$watermarkPositionOptions['top left'] = 'Top-Left';
$watermarkPositionOptions['top'] = 'Top-Middle';
$watermarkPositionOptions['top right'] = 'Top-Right';
$watermarkPositionOptions['right'] = 'Right';
$watermarkPositionOptions['bottom right'] = 'Bottom-Right';
$watermarkPositionOptions['bottom'] = 'Bottom-Middle';
$watermarkPositionOptions['bottom left'] = 'Bottom-Left';
$watermarkPositionOptions['left'] = 'Left';
$watermarkPositionOptions['center'] = 'Middle';
// get account stats
$totalFreeSpace = UserPeer::getAvailableFileStorage($Auth->id);
$totalActiveFiles = file::getTotalActiveFilesByUser($Auth->id);
$totalDownloads = file::getTotalDownloadsByUserOwnedFiles($Auth->id);
// update user
if (isset($_REQUEST['submitme']) && (int)$_REQUEST['submitme'])
{
// validation
$title = trim($_REQUEST['title']);
$firstname = trim($_REQUEST['firstname']);
$lastname = trim($_REQUEST['lastname']);
$emailAddress = trim(strtolower($_REQUEST['emailAddress']));
$password = trim($_REQUEST['password']);
$passwordConfirm = trim($_REQUEST['passwordConfirm']);
$languageId = null;
if(isset($_REQUEST['languageId']))
{
$languageId = (int)$_REQUEST['languageId'];
}
$privateFileStatistics = (int)$_REQUEST['privateFileStatistics'];
$isPublic = (int)$_REQUEST['isPublic'];
$uploadedAvatar = null;
if((isset($_FILES['avatar']['tmp_name'])) && (strlen($_FILES['avatar']['tmp_name'])))
{
$uploadedAvatar = $_FILES['avatar'];
}
$uploadedWatermark = null;
if((isset($_FILES['watermark']['tmp_name'])) && (strlen($_FILES['watermark']['tmp_name'])))
{
$uploadedWatermark = $_FILES['watermark'];
}
$removeAvatar = false;
if((isset($_REQUEST['removeAvatar'])) && ((int)$_REQUEST['removeAvatar'] == 1))
{
$removeAvatar = true;
}
$removeWatermark = false;
if((isset($_REQUEST['removeWatermark'])) && ((int)$_REQUEST['removeWatermark'] == 1))
{
$removeWatermark = true;
}
$watermarkPosition = trim($_REQUEST['watermarkPosition']);
$watermarkPadding = (int)$_REQUEST['watermarkPadding'];
if (!strlen($title))
{
notification::setError(t("please_enter_your_title", "Please enter your title"));
}
elseif (!strlen($firstname))
{
notification::setError(t("please_enter_your_firstname", "Please enter your firstname"));
}
elseif (!strlen($lastname))
{
notification::setError(t("please_enter_your_lastname", "Please enter your lastname"));
}
elseif (!strlen($emailAddress))
{
notification::setError(t("please_enter_your_email_address", "Please enter your email address"));
}
elseif (!validation::validEmail($emailAddress))
{
notification::setError(t("your_email_address_is_invalid", "Your email address is invalid"));
}
elseif(coreFunctions::inDemoMode() == true)
{
notification::setError(t("no_changes_in_demo_mode"));
}
else
{
$checkEmail = UserPeer::loadUserByEmailAddress($emailAddress);
if (($checkEmail) && ($checkEmail->id != $Auth->id))
{
// username exists
notification::setError(t("email_address_already_exists", "Email address already exists on another account"));
}
else
{
// check password if one set
if(strlen($password))
{
if($password != $passwordConfirm)
{
notification::setError(t("your_password_confirmation_does_not_match", "Your password confirmation does not match"));
}
else
{
$passValid = passwordPolicy::validatePassword($password);
if(is_array($passValid))
{
notification::setError(implode('
', $passValid));
}
}
}
}
}
if (!notification::isErrors())
{
if($uploadedAvatar)
{
// check filesize
$maxAvatarSize = 1024*1024*5;
if($uploadedAvatar['size'] > ($maxAvatarSize))
{
notification::setError(t("account_edit_avatar_is_too_large", "The uploaded image can not be more than [[[MAX_SIZE_FORMATTED]]]", array('MAX_SIZE_FORMATTED'=>coreFunctions::formatSize($maxAvatarSize))));
}
else
{
// make sure it's an image
$imagesizedata = @getimagesize($uploadedAvatar['tmp_name']);
if ($imagesizedata === FALSE)
{
//not image
notification::setError(t("account_edit_avatar_is_not_an_image", "Your avatar must be a jpg, png or gif image."));
}
}
}
}
if (!notification::isErrors())
{
if($uploadedWatermark)
{
// check filesize
$maxWatermarkSize = 1024*1024*5;
if($uploadedWatermark['size'] > ($maxWatermarkSize))
{
notification::setError(t("account_edit_watermark_is_too_large", "The uploaded watermark can not be more than [[[MAX_SIZE_FORMATTED]]]", array('MAX_SIZE_FORMATTED'=>coreFunctions::formatSize($maxWatermarkSize))));
}
else
{
// make sure it's a png image
$imgInfo = getimagesize($uploadedWatermark['tmp_name']);
if($imgInfo[2] != IMAGETYPE_PNG)
{
// not image
notification::setError(t("account_edit_watermark_is_not_a_png", "Your watermark must be a png image."));
}
}
}
}
// update the account
if (!notification::isErrors())
{
// if password changed send confirmation notice to user
if(SITE_CONFIG_SECURITY_SEND_USER_EMAIL_ON_PASSWORD_CHANGE == 'yes')
{
if(strlen($password))
{
$subject = t('password_change_email_subject', 'Password changed for account on [[[SITE_NAME]]]', array('SITE_NAME' => SITE_CONFIG_SITE_NAME));
$replacements = array(
'FIRST_NAME' => $Auth->user->firstname,
'SITE_NAME' => SITE_CONFIG_SITE_NAME,
'WEB_ROOT' => WEB_ROOT,
'USERNAME' => $Auth->username,
);
$defaultContent = "Dear [[[FIRST_NAME]]],
";
$defaultContent .= "This is a courtesy email notifying you that your account password on [[[SITE_NAME]]] has been changed.
";
$defaultContent .= "If you didn't change your password, please contact us immediately. Otherwise just ignore this email.
";
$defaultContent .= "Url: [[[WEB_ROOT]]]
";
$defaultContent .= "Username: [[[USERNAME]]]
";
$defaultContent .= "Feel free to contact us if you need any support with your account.
";
$defaultContent .= "Regards,
";
$defaultContent .= "[[[SITE_NAME]]] Admin";
$htmlMsg = t('password_change_email_content', $defaultContent, $replacements);
coreFunctions::sendHtmlEmail($Auth->email, $subject, $htmlMsg, SITE_CONFIG_DEFAULT_EMAIL_ADDRESS_FROM, strip_tags(str_replace("
", "\n", $htmlMsg)));
}
}
// if email changed send confirmation notice to user
if(SITE_CONFIG_SECURITY_SEND_USER_EMAIL_ON_EMAIL_CHANGE == 'yes')
{
if($emailAddress != $Auth->email)
{
$subject = t('email_change_email_subject', 'Email changed for account on [[[SITE_NAME]]]', array('SITE_NAME' => SITE_CONFIG_SITE_NAME));
$replacements = array(
'FIRST_NAME' => $Auth->user->firstname,
'SITE_NAME' => SITE_CONFIG_SITE_NAME,
'WEB_ROOT' => WEB_ROOT,
'USERNAME' => $Auth->username,
'NEW_EMAIL' => $emailAddress,
);
$defaultContent = "Dear [[[FIRST_NAME]]],
";
$defaultContent .= "This is a courtesy email notifying you that your account email address on [[[SITE_NAME]]] has been changed to [[[NEW_EMAIL]]].
";
$defaultContent .= "If you didn't change your email address, please contact us immediately. Otherwise just ignore this email.
";
$defaultContent .= "Url: [[[WEB_ROOT]]]
";
$defaultContent .= "Username: [[[USERNAME]]]
";
$defaultContent .= "New Email: [[[NEW_EMAIL]]]
";
$defaultContent .= "Feel free to contact us if you need any support with your account.
";
$defaultContent .= "Regards,
";
$defaultContent .= "[[[SITE_NAME]]] Admin";
$htmlMsg = t('email_change_email_content', $defaultContent, $replacements);
coreFunctions::sendHtmlEmail($Auth->email, $subject, $htmlMsg, SITE_CONFIG_DEFAULT_EMAIL_ADDRESS_FROM, strip_tags(str_replace("
", "\n", $htmlMsg)));
}
}
$db = Database::getDatabase(true);
$rs = $db->query('UPDATE users SET title = :title, firstname = :firstname, lastname = :lastname, email = :email, languageId = :languageId, privateFileStatistics = :privateFileStatistics, isPublic = :isPublic WHERE id = :id', array('title' => $title, 'firstname' => $firstname, 'lastname' => $lastname, 'email' => $emailAddress, 'id' => $Auth->id, 'languageId' => $languageId, 'privateFileStatistics' => $privateFileStatistics, 'isPublic' => $isPublic));
if ($rs)
{
// do password
if(strlen($password))
{
$rs = $db->query('UPDATE users SET password = :password WHERE id = :id', array('password' => Password::createHash($password), 'id' => $Auth->id));
}
// reset site language if updated
if($languageId != null)
{
$languageName = $db->getValue("SELECT languageName FROM language WHERE isActive = 1 AND id = " . (int)$languageId . " LIMIT 1");
if($languageName)
{
$_SESSION['_t'] = $languageName;
}
}
// save avatar
$src = null;
if($uploadedAvatar)
{
// convert all images to jpg
$imgInfo = getimagesize($uploadedAvatar['tmp_name']);
switch ($imgInfo[2])
{
case IMAGETYPE_GIF: $src = imagecreatefromgif($uploadedAvatar['tmp_name']); break;
case IMAGETYPE_JPEG: $src = imagecreatefromjpeg($uploadedAvatar['tmp_name']); break;
case IMAGETYPE_PNG: $src = imagecreatefrompng($uploadedAvatar['tmp_name']); break;
default: $src = null;
}
}
// if we've loaded the image store it as jpg
if(($src) || ($removeAvatar == true))
{
ob_start();
imagejpeg($src, null, 100);
$imageData = ob_get_contents();
ob_end_clean();
$avatarCachePath = 'user/'.(int)$Auth->id.'/profile';
// delete any existing avatar files including generate cache
if(file_exists(CACHE_DIRECTORY_ROOT.'/'.$avatarCachePath))
{
$files = coreFunctions::getDirectoryListing(CACHE_DIRECTORY_ROOT.'/'.$avatarCachePath);
if(COUNT($files))
{
foreach($files AS $file)
{
@unlink($file);
}
}
}
if($src)
{
// save new file
cache::saveCacheToFile($avatarCachePath.'/avatar_original.jpg', $imageData);
}
}
// track whether to clear any image cache or now
$clearImageCache = false;
// save watermark image
if(($uploadedWatermark) || ($removeWatermark == true))
{
$imageData = file_get_contents($uploadedWatermark['tmp_name']);
$watermarkCachePath = 'user/'.(int)$Auth->id.'/watermark';
// delete any existing avatar files including generate cache
if(file_exists(CACHE_DIRECTORY_ROOT.'/'.$watermarkCachePath))
{
$files = coreFunctions::getDirectoryListing(CACHE_DIRECTORY_ROOT.'/'.$watermarkCachePath);
if(COUNT($files))
{
foreach($files AS $file)
{
@unlink($file);
}
}
}
if($uploadedWatermark)
{
// save new file
cache::saveCacheToFile($watermarkCachePath.'/watermark_original.png', $imageData);
}
// clear image cache
$clearImageCache = true;
}
// if there's any changes to the watermarking, clear the cache
if(($watermarkPosition != $user->getProfileValue('watermarkPosition')) || ($watermarkPadding != $user->getProfileValue('watermarkPadding')))
{
// clear image cache
$clearImageCache = true;
}
// setup for profile data
$profile = array();
$profile['watermarkPosition'] = $watermarkPosition;
$profile['watermarkPadding'] = $watermarkPadding;
// update any profile information, this is used for fields which may not be part of the core script, so theme specific
$Auth->user->storeProfileData($profile);
// clear any image cache if we need to
if($clearImageCache == true)
{
$folders = $db->getRows('SELECT id FROM file_folder WHERE userId = ' . (int)$Auth->id);
if($folders)
{
$pluginObj = pluginHelper::getInstance('filepreviewer');
foreach($folders AS $folder)
{
$files = file::loadAllActiveByFolderId((int)$folder['id']);
if($files)
{
foreach($files AS $file)
{
$pluginObj->deleteImagePreviewCache($file['id']);
}
}
}
}
}
// message
notification::setSuccess(t("account_updated_success_message", "Account details successfully updated"));
}
else
{
notification::setError(t("problem_creating_your_account_try_again_later", "There was a problem creating your account, please try again later"));
}
}
}
else
{
$title = $user->title;
$firstname = $user->firstname;
$lastname = $user->lastname;
$emailAddress = $user->email;
$languageId = $user->languageId;
$isPublic = (int)$user->isPublic;
if($languageId == null)
{
$languageId = $db->getValue("SELECT id FROM language WHERE isActive = 1 AND languageName = '" . $db->escape(trim($_REQUEST['_t'])) . "' LIMIT 1");
}
$privateFileStatistics = $user->privateFileStatistics;
// load any profile info
$watermarkPosition = $user->getProfileValue('watermarkPosition')?$user->getProfileValue('watermarkPosition'):'bottom-right';
$watermarkPadding = (int)$user->getProfileValue('watermarkPadding')?$user->getProfileValue('watermarkPadding'):10;
}
// check for existing avatar
$hasAvatar = false;
$avatarCachePath = 'user/'.(int)$Auth->id.'/profile/avatar_original.jpg';
if(cache::checkCacheFileExists($avatarCachePath))
{
$hasAvatar = true;
}
// check for existing profile image
$hasWatermark = false;
$watermarkCachePath = 'user/'.(int)$Auth->id.'/watermark/watermark_original.png';
$watermarkCacheUrl = CACHE_WEB_ROOT.'/'.$watermarkCachePath;
if(cache::checkCacheFileExists($watermarkCachePath))
{
$hasWatermark = true;
}
// include header
require_once(SITE_TEMPLATES_PATH . '/partial/_header.inc.php');
// file manager javascript
require_once(SITE_TEMPLATES_PATH . '/partial/_account_home_javascript.inc.php');
if (notification::isSuccess())
{
echo templateFunctions::outputSuccess();
}
elseif (notification::isErrors())
{
echo templateFunctions::outputErrors();
}
?>