Example #1
0
 function update_location($args, $apidata)
 {
     parent::handle($args);
     if ($_SERVER['REQUEST_METHOD'] != 'POST') {
         $this->clientError(_('This method requires a POST.'), 400, $apidata['content-type']);
         return;
     }
     $location = trim($this->arg('location'));
     if (!is_null($location) && mb_strlen($location) > 255) {
         // XXX: But Twitter just truncates and runs with it. -- Zach
         $this->clientError(_('That\'s too long. Max notice size is 255 chars.'), 406, $apidate['content-type']);
         return;
     }
     $user = $apidata['user'];
     // Always the auth user
     $profile = $user->getProfile();
     $orig_profile = clone $profile;
     $profile->location = $location;
     $result = $profile->update($orig_profile);
     if (empty($result)) {
         common_log_db_error($profile, 'UPDATE', __FILE__);
         $this->serverError(_('Couldn\'t save profile.'));
         return;
     }
     common_broadcast_profile($profile);
     $type = $apidata['content-type'];
     $this->init_document($type);
     $this->show_profile($profile, $type);
     $this->end_document($type);
 }
Example #2
0
function updateUserUrls()
{
    printfnq("Updating user URLs...\n");
    // XXX: only update user URLs where out-of-date
    $qry = "SELECT * FROM profile order by id asc";
    $pflQry = new Profile();
    $pflQry->query($qry);
    $members = array();
    while ($pflQry->fetch()) {
        $members[] = clone $pflQry;
    }
    $pflQry->free();
    foreach ($members as $member) {
        $user = $member->getUser();
        printfv("Updating user {$user->nickname}...");
        try {
            $profile = $user->getProfile();
            updateProfileUrl($profile);
            updateAvatarUrls($profile);
            // Broadcast for remote users
            common_broadcast_profile($profile);
        } catch (Exception $e) {
            printv("Error updating URLs: " . $e->getMessage());
        }
        printfv("DONE.");
    }
}
function updateProfileURL($user)
{
    $profile = $user->getProfile();
    if (empty($profile)) {
        throw new Exception("Can't find profile for user {$user->nickname} ({$user->id})");
    }
    $orig = clone $profile;
    $profile->profileurl = common_profile_url($user->nickname);
    if (!have_option('q', 'quiet')) {
        print "Updating profile url for {$user->nickname} ({$user->id}) " . "from {$orig->profileurl} to {$profile->profileurl}...";
    }
    $result = $profile->update($orig);
    if (!$result) {
        print "FAIL.\n";
        common_log_db_error($profile, 'UPDATE', __FILE__);
        throw new Exception("Can't update profile for user {$user->nickname} ({$user->id})");
    }
    common_broadcast_profile($profile);
    print "OK.\n";
}
function updateUserUrls()
{
    printfnq("Updating user URLs...\n");
    // XXX: only update user URLs where out-of-date
    $user = new User();
    if ($user->find()) {
        while ($user->fetch()) {
            printfv("Updating user {$user->nickname}...");
            try {
                $profile = $user->getProfile();
                updateProfileUrl($profile);
                updateAvatarUrls($profile);
                // Broadcast for remote users
                common_broadcast_profile($profile);
            } catch (Exception $e) {
                printv("Error updating URLs: " . $e->getMessage());
            }
            printfv("DONE.");
        }
    }
}
 function onStartAvatarSaveForm($action)
 {
     if ($action->arg('add')) {
         $result = $this->gravatar_save();
         if ($result['success'] === true) {
             common_broadcast_profile(common_current_user()->getProfile());
         }
         $action->showForm($result['message'], $result['success']);
         return false;
     } else {
         if ($action->arg('remove')) {
             $result = $this->gravatar_remove();
             if ($result['success'] === true) {
                 common_broadcast_profile(common_current_user()->getProfile());
             }
             $action->showForm($result['message'], $result['success']);
             return false;
         } else {
             return true;
         }
     }
 }
Example #6
0
 /**
  * Handle the results of jcrop.
  *
  * @return void
  */
 function cropAvatar()
 {
     $filedata = $_SESSION['FILEDATA'];
     if (!$filedata) {
         $this->serverError(_('Lost our file data.'));
         return;
     }
     $file_d = $filedata['width'] > $filedata['height'] ? $filedata['height'] : $filedata['width'];
     $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x') : 0;
     $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y') : 0;
     $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w') : $file_d;
     $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h') : $file_d;
     $size = min($dest_w, $dest_h, MAX_ORIGINAL);
     $user = common_current_user();
     $profile = $user->getProfile();
     $imagefile = new ImageFile($user->id, $filedata['filepath']);
     $filename = $imagefile->resize($size, $dest_x, $dest_y, $dest_w, $dest_h);
     if ($profile->setOriginal($filename)) {
         @unlink($filedata['filepath']);
         unset($_SESSION['FILEDATA']);
         $this->mode = 'upload';
         $this->showForm(_('Avatar updated.'), true);
         common_broadcast_profile($profile);
     } else {
         $this->showForm(_('Failed updating avatar.'));
     }
 }
Example #7
0
 /**
  * Handle a post
  *
  * Validate input and save changes. Reload the form with a success
  * or error message.
  *
  * @return void
  */
 function handlePost()
 {
     // CSRF protection
     $token = $this->trimmed('token');
     if (!$token || $token != common_session_token()) {
         // TRANS: Form validation error.
         $this->showForm(_('There was a problem with your session token. ' . 'Try again, please.'));
         return;
     }
     if (Event::handle('StartProfileSaveForm', array($this))) {
         try {
             $nickname = Nickname::normalize($this->trimmed('nickname'));
         } catch (NicknameException $e) {
             $this->showForm($e->getMessage());
             return;
         }
         $fullname = $this->trimmed('fullname');
         $homepage = $this->trimmed('homepage');
         $bio = $this->trimmed('bio');
         $location = $this->trimmed('location');
         $autosubscribe = $this->boolean('autosubscribe');
         $subscribe_policy = $this->trimmed('subscribe_policy');
         $private_stream = $this->boolean('private_stream');
         $language = $this->trimmed('language');
         $timezone = $this->trimmed('timezone');
         $tagstring = $this->trimmed('tags');
         // Some validation
         if (!User::allowed_nickname($nickname)) {
             // TRANS: Validation error in form for profile settings.
             $this->showForm(_('Not a valid nickname.'));
             return;
         } else {
             if (!is_null($homepage) && strlen($homepage) > 0 && !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
                 // TRANS: Validation error in form for profile settings.
                 $this->showForm(_('Homepage is not a valid URL.'));
                 return;
             } else {
                 if (!is_null($fullname) && mb_strlen($fullname) > 255) {
                     // TRANS: Validation error in form for profile settings.
                     $this->showForm(_('Full name is too long (maximum 255 characters).'));
                     return;
                 } else {
                     if (Profile::bioTooLong($bio)) {
                         // TRANS: Validation error in form for profile settings.
                         // TRANS: Plural form is used based on the maximum number of allowed
                         // TRANS: characters for the biography (%d).
                         $this->showForm(sprintf(_m('Bio is too long (maximum %d character).', 'Bio is too long (maximum %d characters).', Profile::maxBio()), Profile::maxBio()));
                         return;
                     } else {
                         if (!is_null($location) && mb_strlen($location) > 255) {
                             // TRANS: Validation error in form for profile settings.
                             $this->showForm(_('Location is too long (maximum 255 characters).'));
                             return;
                         } else {
                             if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
                                 // TRANS: Validation error in form for profile settings.
                                 $this->showForm(_('Timezone not selected.'));
                                 return;
                             } else {
                                 if ($this->nicknameExists($nickname)) {
                                     // TRANS: Validation error in form for profile settings.
                                     $this->showForm(_('Nickname already in use. Try another one.'));
                                     return;
                                 } else {
                                     if (!is_null($language) && strlen($language) > 50) {
                                         // TRANS: Validation error in form for profile settings.
                                         $this->showForm(_('Language is too long (maximum 50 characters).'));
                                         return;
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         $tags = array();
         $tag_priv = array();
         if (is_string($tagstring) && strlen($tagstring) > 0) {
             $tags = preg_split('/[\\s,]+/', $tagstring);
             foreach ($tags as &$tag) {
                 $private = @$tag[0] === '.';
                 $tag = common_canonical_tag($tag);
                 if (!common_valid_profile_tag($tag)) {
                     // TRANS: Validation error in form for profile settings.
                     // TRANS: %s is an invalid tag.
                     $this->showForm(sprintf(_('Invalid tag: "%s".'), $tag));
                     return;
                 }
                 $tag_priv[$tag] = $private;
             }
         }
         $user = common_current_user();
         $user->query('BEGIN');
         if ($user->nickname != $nickname || $user->language != $language || $user->timezone != $timezone) {
             common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname, __FILE__);
             common_debug('Updating user language from ' . $user->language . ' to ' . $language, __FILE__);
             common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone, __FILE__);
             $original = clone $user;
             $user->nickname = $nickname;
             $user->language = $language;
             $user->timezone = $timezone;
             $result = $user->updateKeys($original);
             if ($result === false) {
                 common_log_db_error($user, 'UPDATE', __FILE__);
                 // TRANS: Server error thrown when user profile settings could not be updated.
                 $this->serverError(_('Could not update user.'));
                 return;
             } else {
                 // Re-initialize language environment if it changed
                 common_init_language();
                 // Clear the site owner, in case nickname changed
                 if ($user->hasRole(Profile_role::OWNER)) {
                     User::blow('user:site_owner');
                 }
             }
         }
         // XXX: XOR
         if ($user->autosubscribe ^ $autosubscribe || $user->private_stream ^ $private_stream || $user->subscribe_policy != $subscribe_policy) {
             $original = clone $user;
             $user->autosubscribe = $autosubscribe;
             $user->private_stream = $private_stream;
             $user->subscribe_policy = $subscribe_policy;
             $result = $user->update($original);
             if ($result === false) {
                 common_log_db_error($user, 'UPDATE', __FILE__);
                 // TRANS: Server error thrown when user profile settings could not be updated to
                 // TRANS: automatically subscribe to any subscriber.
                 $this->serverError(_('Could not update user for autosubscribe or subscribe_policy.'));
                 return;
             }
         }
         $profile = $user->getProfile();
         $orig_profile = clone $profile;
         $profile->nickname = $user->nickname;
         $profile->fullname = $fullname;
         $profile->homepage = $homepage;
         $profile->bio = $bio;
         $profile->location = $location;
         $loc = Location::fromName($location);
         if (empty($loc)) {
             $profile->lat = null;
             $profile->lon = null;
             $profile->location_id = null;
             $profile->location_ns = null;
         } else {
             $profile->lat = $loc->lat;
             $profile->lon = $loc->lon;
             $profile->location_id = $loc->location_id;
             $profile->location_ns = $loc->location_ns;
         }
         $profile->profileurl = common_profile_url($nickname);
         if (common_config('location', 'share') == 'user') {
             $exists = false;
             $prefs = User_location_prefs::staticGet('user_id', $user->id);
             if (empty($prefs)) {
                 $prefs = new User_location_prefs();
                 $prefs->user_id = $user->id;
                 $prefs->created = common_sql_now();
             } else {
                 $exists = true;
                 $orig = clone $prefs;
             }
             $prefs->share_location = $this->boolean('sharelocation');
             if ($exists) {
                 $result = $prefs->update($orig);
             } else {
                 $result = $prefs->insert();
             }
             if ($result === false) {
                 common_log_db_error($prefs, $exists ? 'UPDATE' : 'INSERT', __FILE__);
                 // TRANS: Server error thrown when user profile location preference settings could not be updated.
                 $this->serverError(_('Could not save location prefs.'));
                 return;
             }
         }
         common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
         common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
         $result = $profile->update($orig_profile);
         if ($result === false) {
             common_log_db_error($profile, 'UPDATE', __FILE__);
             // TRANS: Server error thrown when user profile settings could not be saved.
             $this->serverError(_('Could not save profile.'));
             return;
         }
         // Set the user tags
         $result = $user->setSelfTags($tags, $tag_priv);
         if (!$result) {
             // TRANS: Server error thrown when user profile settings tags could not be saved.
             $this->serverError(_('Could not save tags.'));
             return;
         }
         $user->query('COMMIT');
         Event::handle('EndProfileSaveForm', array($this));
         common_broadcast_profile($profile);
         // TRANS: Confirmation shown when user profile settings are saved.
         $this->showForm(_('Settings saved.'), true);
     }
 }
 /**
  * 注册公共函数
  * 由一些条件的判断完成最终注册
  * @param $platform_userid 用户id唯一
  * @param $platform_type 类型:sina、qq
  * @param null $nickname 昵称
  * @param null $description 描述
  * @param null $location 当前所在地
  * @param int $gender 性别
  */
 function regist_common($platform_userid, $platform_type, $nickname = null, $profile_image_url = null, $description = null, $gender = 0, $location = null)
 {
     $head = null;
     //拼接userid头
     switch ($platform_type) {
         case 2:
             $head = "qq";
             break;
         case 1:
             $head = 'sina';
             break;
     }
     $user = new User();
     $sql = "platform_type='{$platform_type}' AND platform_userid='{$platform_userid}'";
     $user->whereAdd($sql);
     $user->limit(1);
     $user->find();
     if ($user->fetch()) {
         $this->showUserResult($user, 1);
         return;
     }
     $originalUsername = $head . $platform_userid;
     $username = $this->nicknameFromName($originalUsername);
     $email = $this->trimmed("email");
     $homepage = $this->trimmed("homepage");
     $password = $this->password;
     if (!User::allowed_nickname($nickname)) {
         // TRANS: Client error displayed when trying to create a new user with an invalid username.
         $this->clientError(_('username bad'), 400);
         return;
     }
     $user_check = User::staticGet('nickname', $username);
     if ($user_check) {
         $this->clientError('username exists', 400);
         return;
     }
     $user = User::register(array('nickname' => $username, 'password' => $password, 'email' => $email, 'fullname' => $nickname, 'homepage' => $homepage, 'bio' => $description, 'location' => $location, 'code' => $code, 'gender' => $gender, 'platform_userid' => $platform_userid, 'platform_type' => $platform_type));
     if (!$user) {
         // TRANS: Form validation error displayed when trying to register with an invalid username or password.
         $this->clientError(_('Invalid username or password.', 400, 'json'));
         return;
     }
     // success!
     if (!common_set_user($user)) {
         // TRANS: Server error displayed when saving fails during user registration.
         $this->serverError(_('Error setting user.', '500', 'json'));
         return;
     }
     // this is a real login
     common_real_login(true);
     if ($this->boolean('rememberme')) {
         common_debug('Adding rememberme cookie for ' . $nickname);
         common_rememberme($user);
     }
     // Re-init language env in case it changed (not yet, but soon)
     common_init_language();
     Event::handle('EndRegistrationTry', array($this));
     if (!empty($profile_image_url)) {
         try {
             $user->getProfile()->setOriginalAvatarUrl($profile_image_url);
             common_broadcast_profile($user->getProfile());
         } catch (Exception $exc) {
         }
     }
     $this->showUserResult($user, 0);
 }
 /**
  * Handle a post
  *
  * Validate input and save changes. Reload the form with a success
  * or error message.
  *
  * @return void
  */
 function handlePost()
 {
     // CSRF protection
     $token = $this->trimmed('token');
     if (!$token || $token != common_session_token()) {
         $this->showForm(_('There was a problem with your session token. ' . 'Try again, please.'));
         return;
     }
     if (Event::handle('StartProfileSaveForm', array($this))) {
         $nickname = $this->trimmed('nickname');
         $fullname = $this->trimmed('fullname');
         $homepage = $this->trimmed('homepage');
         $bio = $this->trimmed('bio');
         $location = $this->trimmed('location');
         $autosubscribe = $this->boolean('autosubscribe');
         $language = $this->trimmed('language');
         $timezone = $this->trimmed('timezone');
         $tagstring = $this->trimmed('tags');
         // Some validation
         if (!Validate::string($nickname, array('min_length' => 1, 'max_length' => 64, 'format' => NICKNAME_FMT))) {
             $this->showForm(_('Nickname must have only lowercase letters and numbers and no spaces.'));
             return;
         } else {
             if (!User::allowed_nickname($nickname)) {
                 $this->showForm(_('Not a valid nickname.'));
                 return;
             } else {
                 if (!is_null($homepage) && strlen($homepage) > 0 && !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
                     $this->showForm(_('Homepage is not a valid URL.'));
                     return;
                 } else {
                     if (!is_null($fullname) && mb_strlen($fullname) > 255) {
                         $this->showForm(_('Full name is too long (max 255 chars).'));
                         return;
                     } else {
                         if (Profile::bioTooLong($bio)) {
                             $this->showForm(sprintf(_('Bio is too long (max %d chars).'), Profile::maxBio()));
                             return;
                         } else {
                             if (!is_null($location) && mb_strlen($location) > 255) {
                                 $this->showForm(_('Location is too long (max 255 chars).'));
                                 return;
                             } else {
                                 if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
                                     $this->showForm(_('Timezone not selected.'));
                                     return;
                                 } else {
                                     if ($this->nicknameExists($nickname)) {
                                         $this->showForm(_('Nickname already in use. Try another one.'));
                                         return;
                                     } else {
                                         if (!is_null($language) && strlen($language) > 50) {
                                             $this->showForm(_('Language is too long (max 50 chars).'));
                                             return;
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         if ($tagstring) {
             $tags = array_map('common_canonical_tag', preg_split('/[\\s,]+/', $tagstring));
         } else {
             $tags = array();
         }
         foreach ($tags as $tag) {
             if (!common_valid_profile_tag($tag)) {
                 $this->showForm(sprintf(_('Invalid tag: "%s"'), $tag));
                 return;
             }
         }
         $user = common_current_user();
         $user->query('BEGIN');
         if ($user->nickname != $nickname || $user->language != $language || $user->timezone != $timezone) {
             common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname, __FILE__);
             common_debug('Updating user language from ' . $user->language . ' to ' . $language, __FILE__);
             common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone, __FILE__);
             $original = clone $user;
             $user->nickname = $nickname;
             $user->language = $language;
             $user->timezone = $timezone;
             $result = $user->updateKeys($original);
             if ($result === false) {
                 common_log_db_error($user, 'UPDATE', __FILE__);
                 $this->serverError(_('Couldn\'t update user.'));
                 return;
             } else {
                 // Re-initialize language environment if it changed
                 common_init_language();
                 // Clear the site owner, in case nickname changed
                 if ($user->hasRole(Profile_role::OWNER)) {
                     User::blow('user:site_owner');
                 }
             }
         }
         // XXX: XOR
         if ($user->autosubscribe ^ $autosubscribe) {
             $original = clone $user;
             $user->autosubscribe = $autosubscribe;
             $result = $user->update($original);
             if ($result === false) {
                 common_log_db_error($user, 'UPDATE', __FILE__);
                 $this->serverError(_('Couldn\'t update user for autosubscribe.'));
                 return;
             }
         }
         $profile = $user->getProfile();
         $orig_profile = clone $profile;
         $profile->nickname = $user->nickname;
         $profile->fullname = $fullname;
         $profile->homepage = $homepage;
         $profile->bio = $bio;
         $profile->location = $location;
         $loc = Location::fromName($location);
         if (empty($loc)) {
             $profile->lat = null;
             $profile->lon = null;
             $profile->location_id = null;
             $profile->location_ns = null;
         } else {
             $profile->lat = $loc->lat;
             $profile->lon = $loc->lon;
             $profile->location_id = $loc->location_id;
             $profile->location_ns = $loc->location_ns;
         }
         $profile->profileurl = common_profile_url($nickname);
         if (common_config('location', 'share') == 'user') {
             $exists = false;
             $prefs = User_location_prefs::staticGet('user_id', $user->id);
             if (empty($prefs)) {
                 $prefs = new User_location_prefs();
                 $prefs->user_id = $user->id;
                 $prefs->created = common_sql_now();
             } else {
                 $exists = true;
                 $orig = clone $prefs;
             }
             $prefs->share_location = $this->boolean('sharelocation');
             if ($exists) {
                 $result = $prefs->update($orig);
             } else {
                 $result = $prefs->insert();
             }
             if ($result === false) {
                 common_log_db_error($prefs, $exists ? 'UPDATE' : 'INSERT', __FILE__);
                 $this->serverError(_('Couldn\'t save location prefs.'));
                 return;
             }
         }
         common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
         common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
         $result = $profile->update($orig_profile);
         if ($result === false) {
             common_log_db_error($profile, 'UPDATE', __FILE__);
             $this->serverError(_('Couldn\'t save profile.'));
             return;
         }
         // Set the user tags
         $result = $user->setSelfTags($tags);
         if (!$result) {
             $this->serverError(_('Couldn\'t save tags.'));
             return;
         }
         $user->query('COMMIT');
         Event::handle('EndProfileSaveForm', array($this));
         common_broadcast_profile($profile);
         $this->showForm(_('Settings saved.'), true);
     }
 }
Example #10
0
 /**
  * Handle the results of jcrop.
  *
  * @return void
  */
 function cropAvatar()
 {
     $filedata = $_SESSION['FILEDATA'];
     if (!$filedata) {
         // TRANS: Server error displayed if an avatar upload went wrong somehow server side.
         $this->serverError(_('Lost our file data.'));
         return;
     }
     $file_d = $filedata['width'] > $filedata['height'] ? $filedata['height'] : $filedata['width'];
     $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x') : 0;
     $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y') : 0;
     $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w') : $file_d;
     $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h') : $file_d;
     $size = min($dest_w, $dest_h, MAX_ORIGINAL);
     $user = common_current_user();
     $profile = $user->getProfile();
     $imagefile = new ImageFile($user->id, $filedata['filepath']);
     $filename = $imagefile->resize($size, $dest_x, $dest_y, $dest_w, $dest_h);
     if ($profile->setOriginal($filename)) {
         @unlink($filedata['filepath']);
         unset($_SESSION['FILEDATA']);
         $this->mode = 'upload';
         // TRANS: Success message for having updated a user avatar.
         $this->showForm(_('Avatar updated.'), true);
         common_broadcast_profile($profile);
     } else {
         // TRANS: Error displayed on the avatar upload page if the avatar could not be updated for an unknown reason.
         $this->showForm(_('Failed updating avatar.'));
     }
 }
Example #11
0
 /**
  * Handle a post
  *
  * Validate input and save changes. Reload the form with a success
  * or error message.
  *
  * @return void
  */
 function handlePost()
 {
     # CSRF protection
     $token = $this->trimmed('token');
     if (!$token || $token != common_session_token()) {
         $this->showForm(_('There was a problem with your session token. ' . 'Try again, please.'));
         return;
     }
     $nickname = $this->trimmed('nickname');
     $fullname = $this->trimmed('fullname');
     $homepage = $this->trimmed('homepage');
     $bio = $this->trimmed('bio');
     $location = $this->trimmed('location');
     $autosubscribe = $this->boolean('autosubscribe');
     $language = $this->trimmed('language');
     $timezone = $this->trimmed('timezone');
     $tagstring = $this->trimmed('tags');
     # Some validation
     if (!Validate::string($nickname, array('min_length' => 1, 'max_length' => 64, 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
         $this->showForm(_('Nickname must have only lowercase letters and numbers and no spaces.'));
         return;
     } else {
         if (!User::allowed_nickname($nickname)) {
             $this->showForm(_('Not a valid nickname.'));
             return;
         } else {
             if (!is_null($homepage) && strlen($homepage) > 0 && !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
                 $this->showForm(_('Homepage is not a valid URL.'));
                 return;
             } else {
                 if (!is_null($fullname) && mb_strlen($fullname) > 255) {
                     $this->showForm(_('Full name is too long (max 255 chars).'));
                     return;
                 } else {
                     if (!is_null($bio) && mb_strlen($bio) > 140) {
                         $this->showForm(_('Bio is too long (max 140 chars).'));
                         return;
                     } else {
                         if (!is_null($location) && mb_strlen($location) > 255) {
                             $this->showForm(_('Location is too long (max 255 chars).'));
                             return;
                         } else {
                             if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
                                 $this->showForm(_('Timezone not selected.'));
                                 return;
                             } else {
                                 if ($this->nicknameExists($nickname)) {
                                     $this->showForm(_('Nickname already in use. Try another one.'));
                                     return;
                                 } else {
                                     if (!is_null($language) && strlen($language) > 50) {
                                         $this->showForm(_('Language is too long (max 50 chars).'));
                                         return;
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     if ($tagstring) {
         $tags = array_map('common_canonical_tag', preg_split('/[\\s,]+/', $tagstring));
     } else {
         $tags = array();
     }
     foreach ($tags as $tag) {
         if (!common_valid_profile_tag($tag)) {
             $this->showForm(sprintf(_('Invalid tag: "%s"'), $tag));
             return;
         }
     }
     $user = common_current_user();
     $user->query('BEGIN');
     if ($user->nickname != $nickname || $user->language != $language || $user->timezone != $timezone) {
         common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname, __FILE__);
         common_debug('Updating user language from ' . $user->language . ' to ' . $language, __FILE__);
         common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone, __FILE__);
         $original = clone $user;
         $user->nickname = $nickname;
         $user->language = $language;
         $user->timezone = $timezone;
         $result = $user->updateKeys($original);
         if ($result === false) {
             common_log_db_error($user, 'UPDATE', __FILE__);
             $this->serverError(_('Couldn\'t update user.'));
             return;
         } else {
             # Re-initialize language environment if it changed
             common_init_language();
         }
     }
     # XXX: XOR
     if ($user->autosubscribe ^ $autosubscribe) {
         $original = clone $user;
         $user->autosubscribe = $autosubscribe;
         $result = $user->update($original);
         if ($result === false) {
             common_log_db_error($user, 'UPDATE', __FILE__);
             $this->serverError(_('Couldn\'t update user for autosubscribe.'));
             return;
         }
     }
     $profile = $user->getProfile();
     $orig_profile = clone $profile;
     $profile->nickname = $user->nickname;
     $profile->fullname = $fullname;
     $profile->homepage = $homepage;
     $profile->bio = $bio;
     $profile->location = $location;
     $profile->profileurl = common_profile_url($nickname);
     common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
     common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
     $result = $profile->update($orig_profile);
     if (!$result) {
         common_log_db_error($profile, 'UPDATE', __FILE__);
         $this->serverError(_('Couldn\'t save profile.'));
         return;
     }
     # Set the user tags
     $result = $user->setSelfTags($tags);
     if (!$result) {
         $this->serverError(_('Couldn\'t save tags.'));
         return;
     }
     $user->query('COMMIT');
     common_broadcast_profile($profile);
     $this->showForm(_('Settings saved.'), true);
 }
function updateAvatars($user)
{
    $touched = false;
    if (!have_option('q', 'quiet')) {
        print "Updating avatars for user '" . $user->nickname . "' (" . $user->id . ")...";
    }
    $avatar = new Avatar();
    $avatar->profile_id = $user->id;
    if (!$avatar->find()) {
        if (have_option('v', 'verbose')) {
            print "(none found)...";
        }
    } else {
        while ($avatar->fetch()) {
            if (have_option('v', 'verbose')) {
                if ($avatar->original) {
                    print "original...";
                } else {
                    print $avatar->width . "...";
                }
            }
            $orig_url = $avatar->url;
            $avatar->url = Avatar::url($avatar->filename);
            if ($avatar->url != $orig_url) {
                $sql = "UPDATE avatar SET url = '" . $avatar->url . "' " . "WHERE profile_id = " . $avatar->profile_id . " " . "AND width = " . $avatar->width . " " . "AND height = " . $avatar->height . " ";
                if ($avatar->original) {
                    $sql .= "AND original = 1 ";
                }
                if (!$avatar->query($sql)) {
                    throw new Exception("Can't update avatar for user " . $user->nickname . ".");
                } else {
                    $touched = true;
                }
            }
        }
    }
    if ($touched) {
        $profile = $user->getProfile();
        common_broadcast_profile($profile);
    }
    if (have_option('v', 'verbose')) {
        print "DONE.";
    }
    if (!have_option('q', 'quiet') || have_option('v', 'verbose')) {
        print "\n";
    }
}
Example #13
0
 /**
  * Handle a post
  *
  * Validate input and save changes. Reload the form with a success
  * or error message.
  *
  * @return void
  */
 function handlePost()
 {
     // CSRF protection
     $token = $this->trimmed('token');
     if (!$token || $token != common_session_token()) {
         $this->showForm(_('网页错误,请返回重试
                           '));
         return;
     }
     if (Event::handle('StartProfileSaveForm', array($this))) {
         $fullname = $this->trimmed('fullname');
         $homepage = $this->trimmed('homepage');
         $bio = $this->trimmed('bio');
         $location = $this->trimmed('location');
         $tagstring = $this->trimmed('tags');
         // Some validation
         if (!is_null($homepage) && strlen($homepage) > 0 && !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
             $this->showForm(_('个人主页地址不正确'));
             return;
         } else {
             if (!is_null($fullname) && mb_strlen($fullname) > 255) {
                 $this->showForm(_('真实姓名过长'));
                 return;
             } else {
                 if (Profile::bioTooLong($bio)) {
                     $this->showForm(sprintf(_('自我描述过长'), Profile::maxBio()));
                     return;
                 } else {
                     if (!is_null($location) && mb_strlen($location) > 255) {
                         $this->showForm(_('位置信息过长'));
                         return;
                     }
                 }
             }
         }
         if ($tagstring) {
             $tags = array_map('common_canonical_tag', preg_split('/[\\s,]+/', $tagstring));
         } else {
             $tags = array();
         }
         foreach ($tags as $tag) {
             if (!common_valid_profile_tag($tag)) {
                 $this->showForm(sprintf(_('标签格式不正确: "%s"'), $tag));
                 return;
             }
         }
         $user = common_current_user();
         $user->query('BEGIN');
         $profile = $user->getProfile();
         $orig_profile = clone $profile;
         $profile->nickname = $user->nickname;
         $profile->fullname = $fullname;
         $profile->homepage = $homepage;
         $profile->bio = $bio;
         $profile->location = $location;
         $loc = Location::fromName($location);
         if (empty($loc)) {
             $profile->lat = null;
             $profile->lon = null;
             $profile->location_id = null;
             $profile->location_ns = null;
         } else {
             $profile->lat = $loc->lat;
             $profile->lon = $loc->lon;
             $profile->location_id = $loc->location_id;
             $profile->location_ns = $loc->location_ns;
         }
         if (common_config('location', 'share') == 'user') {
             $exists = false;
             $prefs = User_location_prefs::staticGet('user_id', $user->id);
             if (empty($prefs)) {
                 $prefs = new User_location_prefs();
                 $prefs->user_id = $user->id;
                 $prefs->created = common_sql_now();
             } else {
                 $exists = true;
                 $orig = clone $prefs;
             }
             $prefs->share_location = $this->boolean('sharelocation');
             if ($exists) {
                 $result = $prefs->update($orig);
             } else {
                 $result = $prefs->insert();
             }
             if ($result === false) {
                 common_log_db_error($prefs, $exists ? 'UPDATE' : 'INSERT', __FILE__);
                 $this->serverError(_('Couldn\'t save location prefs.'));
                 return;
             }
         }
         common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
         common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
         $result = $profile->update($orig_profile);
         if ($result === false) {
             common_log_db_error($profile, 'UPDATE', __FILE__);
             $this->serverError(_('无法保存个人信息'));
             return;
         }
         // Set the user tags
         $result = $user->setSelfTags($tags);
         if (!$result) {
             $this->serverError(_('无法保存标签信息'));
             return;
         }
         $user->query('COMMIT');
         Event::handle('EndProfileSaveForm', array($this));
         common_broadcast_profile($profile);
         $this->showForm(_('个人信息已保存'), true);
     }
 }
 /**
  * Handle the request
  *
  * Check whether the credentials are valid and output the result
  *
  * @param array $args $_REQUEST data (unused)
  *
  * @return void
  */
 function handle($args)
 {
     parent::handle($args);
     if ($_SERVER['REQUEST_METHOD'] != 'POST') {
         $this->clientError(_('This method requires a POST.'), 400, $this->format);
         return;
     }
     // Workaround for PHP returning empty $_POST and $_FILES when POST
     // length > post_max_size in php.ini
     if (empty($_FILES) && empty($_POST) && $_SERVER['CONTENT_LENGTH'] > 0) {
         // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
         // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
         $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.', 'The server was unable to handle that much POST data (%s bytes) due to its current configuration.', intval($_SERVER['CONTENT_LENGTH']));
         $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
         return;
     }
     if (empty($this->user)) {
         // TRANS: Client error displayed updating profile image without having a user object.
         $this->clientError(_('No such user.'), 404, $this->format);
         return;
     }
     try {
         $imagefile = ImageFile::fromUpload('image');
     } catch (Exception $e) {
         $this->clientError($e->getMessage(), 400, $this->format);
         return;
     }
     $type = $imagefile->preferredType();
     $filename = Avatar::filename($user->id, image_type_to_extension($type), null, 'tmp' . common_timestamp());
     $filepath = Avatar::path($filename);
     $imagefile->copyTo($filepath);
     $profile = $this->user->getProfile();
     if (empty($profile)) {
         // TRANS: Error message displayed when referring to a user without a profile.
         $this->clientError(_('User has no profile.'));
         return;
     }
     $profile->setOriginal($filename);
     common_broadcast_profile($profile);
     $twitter_user = $this->twitterUserArray($profile, true);
     if ($this->format == 'xml') {
         $this->initDocument('xml');
         $this->showTwitterXmlUser($twitter_user, 'user', true);
         $this->endDocument('xml');
     } elseif ($this->format == 'json') {
         $this->initDocument('json');
         $this->showJsonObjects($twitter_user);
         $this->endDocument('json');
     }
 }
 /**
  * Save fields that should be stored in the main profile object
  *
  * XXX: There's a lot of dupe code here from ProfileSettingsAction.
  *      Do not want.
  *
  * @param User $user the current user
  */
 function saveStandardProfileDetails($user)
 {
     $fullname = $this->trimmed('extprofile-fullname');
     $location = $this->trimmed('extprofile-location');
     $tagstring = $this->trimmed('extprofile-tags');
     $bio = $this->trimmed('extprofile-bio');
     if ($tagstring) {
         $tags = array_map('common_canonical_tag', preg_split('/[\\s,]+/', $tagstring));
     } else {
         $tags = array();
     }
     foreach ($tags as $tag) {
         if (!common_valid_profile_tag($tag)) {
             // TRANS: Validation error in form for profile settings.
             // TRANS: %s is an invalid tag.
             throw new Exception(sprintf(_m('Invalid tag: "%s".'), $tag));
         }
     }
     $profile = $user->getProfile();
     $oldTags = $user->getSelfTags();
     $newTags = array_diff($tags, $oldTags);
     if ($fullname != $profile->fullname || $location != $profile->location || !empty($newTags) || $bio != $profile->bio) {
         $orig = clone $profile;
         $profile->nickname = $user->nickname;
         $profile->fullname = $fullname;
         $profile->bio = $bio;
         $profile->location = $location;
         $loc = Location::fromName($location);
         if (empty($loc)) {
             $profile->lat = null;
             $profile->lon = null;
             $profile->location_id = null;
             $profile->location_ns = null;
         } else {
             $profile->lat = $loc->lat;
             $profile->lon = $loc->lon;
             $profile->location_id = $loc->location_id;
             $profile->location_ns = $loc->location_ns;
         }
         $profile->profileurl = common_profile_url($user->nickname);
         $result = $profile->update($orig);
         if ($result === false) {
             common_log_db_error($profile, 'UPDATE', __FILE__);
             // TRANS: Server error thrown when user profile settings could not be saved.
             $this->serverError(_m('Could not save profile.'));
             return;
         }
         // Set the user tags
         $result = $user->setSelfTags($tags);
         if (!$result) {
             // TRANS: Server error thrown when user profile settings tags could not be saved.
             $this->serverError(_m('Could not save tags.'));
             return;
         }
         Event::handle('EndProfileSaveForm', array($this));
         common_broadcast_profile($profile);
     }
 }
 /**
  * Handle the request
  *
  * See which request params have been set, and update the profile
  *
  * @param array $args $_REQUEST data (unused)
  *
  * @return void
  */
 function handle($args)
 {
     parent::handle($args);
     if ($_SERVER['REQUEST_METHOD'] != 'POST') {
         $this->clientError(_('This method requires a POST.'), 400, $this->format);
         return;
     }
     if (!in_array($this->format, array('xml', 'json'))) {
         $this->clientError(_('API method not found.'), 404, $this->format);
         return;
     }
     if (empty($this->user)) {
         // TRANS: Client error displayed if a user could not be found.
         $this->clientError(_('No such user.'), 404, $this->format);
         return;
     }
     $profile = $this->user->getProfile();
     if (empty($profile)) {
         // TRANS: Client error displayed if a user profile could not be found.
         $this->clientError(_('User has no profile.'));
         return;
     }
     $original = clone $profile;
     if (!empty($this->name)) {
         $profile->fullname = $this->name;
     }
     if (!empty($this->url)) {
         $profile->homepage = $this->url;
     }
     if (!empty($this->description)) {
         $profile->bio = $this->description;
     }
     if (!empty($this->location)) {
         $profile->location = $this->location;
         $loc = Location::fromName($location);
         if (!empty($loc)) {
             $profile->lat = $loc->lat;
             $profile->lon = $loc->lon;
             $profile->location_id = $loc->location_id;
             $profile->location_ns = $loc->location_ns;
         }
     }
     $result = $profile->update($original);
     if (!$result) {
         common_log_db_error($profile, 'UPDATE', __FILE__);
         // TRANS: Server error displayed if a user profile could not be saved.
         $this->serverError(_('Could not save profile.'));
         return;
     }
     common_broadcast_profile($profile);
     $twitter_user = $this->twitterUserArray($profile, true);
     if ($this->format == 'xml') {
         $this->initDocument('xml');
         $this->showTwitterXmlUser($twitter_user);
         $this->endDocument('xml');
     } elseif ($this->format == 'json') {
         $this->initDocument('json');
         $this->showJsonObjects($twitter_user);
         $this->endDocument('json');
     }
 }
 /**
  * Handle the request
  *
  * Check whether the credentials are valid and output the result
  *
  * @param array $args $_REQUEST data (unused)
  *
  * @return void
  */
 function handle($args)
 {
     parent::handle($args);
     if ($_SERVER['REQUEST_METHOD'] != 'POST') {
         $this->clientError(_('This method requires a POST.'), 400, $this->format);
         return;
     }
     // Workaround for PHP returning empty $_POST and $_FILES when POST
     // length > post_max_size in php.ini
     if (empty($_FILES) && empty($_POST) && $_SERVER['CONTENT_LENGTH'] > 0) {
         $msg = _('The server was unable to handle that much POST ' . 'data (%s bytes) due to its current configuration.');
         $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
         return;
     }
     if (empty($this->user)) {
         $this->clientError(_('No such user.'), 404, $this->format);
         return;
     }
     try {
         $imagefile = ImageFile::fromUpload('image');
     } catch (Exception $e) {
         $this->clientError($e->getMessage(), 400, $this->format);
         return;
     }
     $filename = Avatar::filename($user->id, image_type_to_extension($imagefile->type), null, 'tmp' . common_timestamp());
     $filepath = Avatar::path($filename);
     move_uploaded_file($imagefile->filepath, $filepath);
     $profile = $this->user->getProfile();
     if (empty($profile)) {
         $this->clientError(_('User has no profile.'));
         return;
     }
     $profile->setOriginal($filename);
     common_broadcast_profile($profile);
     $twitter_user = $this->twitterUserArray($profile, true);
     if ($this->format == 'xml') {
         $this->initDocument('xml');
         $this->showTwitterXmlUser($twitter_user);
         $this->endDocument('xml');
     } elseif ($this->format == 'json') {
         $this->initDocument('json');
         $this->showJsonObjects($twitter_user);
         $this->endDocument('json');
     }
 }