function index() { if(!isset($_SESSION['active_user'])) redirect_to(make_url("users")); $usr = instance_model('users'); $user = $usr->get_user_by_id($_SESSION['active_user']['id']); if($user == array()) throw new no_such_user_exception(); if(!isset($_POST['Submit'])) { $form_vals = array( $user[0]['E-mail'], $user[0]['Full_name'], $user[0]['Location'], $user[0]['Web'], $user[0]['Bio']); // Display main $view = instance_view("settings_main"); $view = $view->parse_to_variable(array( 'form_vals' => $form_vals)); } else { $form_vals = $_POST; // Validate email try { validate_email($form_vals[0]); } catch(exception $e) { new_flash('Email address is invalid', 1); $form_vals[0] = $user[0]['E-mail']; } // Validate full name try { validate_50($form_vals[1]); } catch(exception $e) { new_flash('Full name is too long, max 50 chars', 1); $form_vals[1] = $user[0]['User_name']; } // Validate location try { validate_50($form_vals[2]); } catch(exception $e) { new_flash('Location is too long, max 50 chars', 1); $form_vals[2] = $user[0]['Location']; } // Validate web try { validate_url($form_vals[3]); } catch(exception $e) { new_flash('Website URL is invalid', 1); $form_vals[3] = $user[0]['Web']; } // Validate bio try { validate_bio($form_vals[4]); } catch(exception $e) { new_flash('Bio is invalid', 1); $form_vals[4] = $user[0]['Bio']; } if(count(get_errors()) == 0) { // Everything was vald, save updated user options $usr->update_user( $user[0]['ID'], $form_vals[0], $form_vals[1], $form_vals[2], $form_vals[3], $form_vals[4]); redirect_to(make_url('settings')); } else { // Something was invalid, redisplay main $view = instance_view("settings_main"); $view = $view->parse_to_variable(array( 'form_vals' => $form_vals)); } } // Display sidebar $sb_view = instance_view("settings_sidebar"); $sb_view = $sb_view->parse_to_variable(array( 'uid' => $_SESSION['active_user']['id'], 'uname' => $_SESSION['active_user']['name'])); $this->set_template_paramiters( array('main_content' => $view, 'sidebar' => $sb_view)); }
function update_user($id, $email, $full_name, $location, $web, $bio) { $this->verify_user_id($id); validate_email($email); validate_50($full_name); validate_50($location); validate_url($web); validate_bio($bio); $query = "UPDATE `users` SET `E-mail` = '@v', `Full_name` = '@v', `Location` = '@v', `Web` = '@v', `Bio` = '@v' WHERE `ID` = '@v' LIMIT 1"; $this->query($query, $email, $full_name, $location, $web, $bio, $id); }
function test_validate_50_invalid() { $this->setExpectedException('over_50_exception'); $str = ''; for($i = 0; $i < 51; $i ++) $str .= 'a'; validate_50($str); }