Esempio n. 1
0
 public function GET()
 {
     $policy = new Policy_LoggedIn($this->app);
     $policy->ensure();
     $userid = $policy->getData();
     $date = date('n.j.Y');
     header("Content-type: text/csv");
     header("Content-disposition: attachment; filename=weights-{$date}.csv");
     $mapper = new Mapper_Weight();
     $weights = $mapper->getWeightsForUser($userid, 'all');
     echo "Date,Weight,Comment\n";
     foreach ($weights as $weight) {
         $time = date('F j Y g:i a', $weight['create_time']);
         echo $time . "," . $weight['weight'];
         if ($weight['comment']) {
             echo ',' . self::escapeCSVValue($weight['comment']);
         }
         echo "\n";
     }
 }
Esempio n. 2
0
 public function POST()
 {
     $policy = new Policy_LoggedIn($this->app);
     $policy->ensure();
     $userid = $policy->getData();
     $mapper = new Mapper_User();
     $user = $mapper->getUserById($userid);
     $app = Config::get('app');
     $request = $this->app->request();
     $email = trim($request->post('email'));
     if (!$email) {
         $this->error("Email is a required field.");
     }
     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
         $this->error("Invalid email format.");
     }
     $possibleUser = $mapper->getUserByEmail($email);
     if ($possibleUser && $possibleUser['id'] != $userid) {
         $this->error("A user with that email address already exists.");
     }
     $oldPassword = trim($request->post('old_password'));
     $newPassword = trim($request->post('new_password'));
     if ($oldPassword && !$newPassword || !$oldPassword && $newPassword) {
         $this->error("You must enter both your old and your new passwords.");
     } else {
         if ($oldPassword && $newPassword) {
             if ($user['password_hash'] != Mapper_User::generateHash($oldPassword)) {
                 $this->error("Old password is incorrect.");
             }
             if (strlen($newPassword) < 5 || strlen($newPassword) > 15) {
                 $this->error("New password must be between 5 and 15 characters.");
             }
             if (!ctype_alnum($newPassword)) {
                 $this->error("Invalid password. Only letters and numbers are allowed.");
             }
             $mapper->updatePasswordForUser($userid, $newPassword);
         }
     }
     $mapper->updateEmailForUser($userid, $email);
     $this->success();
 }
Esempio n. 3
0
 public function POST()
 {
     $policy = new Policy_LoggedIn($this->app);
     $policy->ensure();
     $userid = $policy->getData();
     $request = $this->app->request();
     $tempPassword = $request->post('password');
     $user_mapper = new Mapper_User();
     $user = $user_mapper->getUserById($userid);
     if ($user['password_hash'] != Mapper_User::generateHash($tempPassword)) {
         $this->error("The password you entered was invalid.");
     } else {
         // Delete settings
         $settings_mapper = new Mapper_Settings();
         $settings_mapper->deleteAllSettingsForUser($userid);
         // Delete weights
         $weight_mapper = new Mapper_Weight();
         $weight_mapper->deleteAllWeightsForUser($userid);
         // Delete user last
         $user_mapper->deleteUserById($userid);
         $this->success();
     }
 }
Esempio n. 4
0
 public function POST()
 {
     $policy = new Policy_LoggedIn($this->app);
     $policy->ensure();
     $userid = $policy->getData();
     if (!isset($_FILES['file'])) {
         $this->error("Nothing to do.");
     }
     $file = $_FILES['file'];
     if (isset($file['error']) && $file['error'] > 0) {
         $error = $file['error'];
         if ($error == UPLOAD_ERR_NO_FILE) {
             $this->error("No file was selected.");
         } else {
             if ($error == UPLOAD_ERR_INI_SIZE) {
                 $this->error("The file you're trying to upload is too big.");
             } else {
                 $this->error("Something went wrong, please try again later.");
             }
         }
     }
     $tmpName = $file['tmp_name'];
     ini_set('auto_detect_line_endings', true);
     $handle = fopen($tmpName, 'r');
     $dataLines = array();
     while (($data = fgetcsv($handle)) !== false) {
         $dataLines[] = $data;
     }
     ini_set('auto_detect_line_endings', false);
     if (count($dataLines) < 2) {
         $this->error("The file uploaded does not contain enough data to import.");
     }
     $descripData = $dataLines[0];
     $dateOffset = false;
     $weightOffset = false;
     $commentOffset = false;
     for ($i = 0; $i < count($descripData); $i++) {
         $field = strtolower(trim($descripData[$i]));
         if ($field == "date") {
             $dateOffset = $i;
         } else {
             if ($field == "weight") {
                 $weightOffset = $i;
             } else {
                 if ($field == "comment" || $field == "comments" || $field == "note" || $field == "notes") {
                     $commentOffset = $i;
                 }
             }
         }
     }
     if ($dateOffset === false || $weightOffset === false) {
         $this->error("The file uploaded is missing the required fields.");
     }
     $validRows = 0;
     for ($i = 1; $i < count($dataLines); $i++) {
         $tmpData = $dataLines[$i];
         $tmpDate = trim($tmpData[$dateOffset]);
         $tmpWeight = trim($tmpData[$weightOffset]);
         $tmpComment = '';
         if ($commentOffset && isset($tmpData[$commentOffset])) {
             $tmpComment = trim($tmpData[$commentOffset]);
         }
         $tmpWeight = Helper_Weight::validateWeight($tmpWeight);
         $tmpDate = Helper_Date::validateDate($tmpDate);
         if ($tmpDate && $tmpWeight) {
             $mapper = new Mapper_Weight();
             $mapper->addWeight($userid, $tmpWeight, $tmpComment, $tmpDate);
             $validRows++;
         }
     }
     if ($validRows == 0) {
         $this->error("No valid data found to import.");
     }
     $this->success("Import complete. {$validRows} " . ($validRows != 1 ? "rows" : "row") . " were just imported.");
 }
Esempio n. 5
0
 public function GET()
 {
     $policy = new Policy_LoggedIn($this->app);
     $policy->ensure();
 }
Esempio n. 6
0
 public function POST()
 {
     $policy = new Policy_LoggedIn($this->app);
     $policy->ensure();
     $userid = $policy->getData();
     $app = Config::get('app');
     $request = $this->app->request();
     $user_settings = $app->user_settings;
     foreach ($user_settings as $setting) {
         $val = trim($request->post($setting['name']));
         $newVal = $setting['default'];
         if ($setting['validate'] == 'boolean') {
             if ($val == 'on') {
                 $newVal = 1;
             } else {
                 $newVal = 0;
             }
         } else {
             if ($setting['validate'] == 'height') {
                 $newVal = $val;
                 if (!is_numeric($newVal)) {
                     $newVal = 0;
                 } else {
                     if ($newVal < 0) {
                         $newVal = 0;
                     } else {
                         if ($newVal > 120) {
                             $newVal = 120;
                         }
                     }
                 }
                 $newVal = round($newVal, 1);
             } else {
                 if ($setting['validate'] == 'weight') {
                     $newVal = $val;
                     if (!is_numeric($newVal)) {
                         $newVal = 0;
                     } else {
                         if ($newVal < 0) {
                             $newVal = 0;
                         } else {
                             if ($newVal > 1000) {
                                 $newVal = 1000;
                             }
                         }
                     }
                     $newVal = round($newVal, 1);
                 } else {
                     if ($setting['validate'] == 'timezone') {
                         $zones = DateTimeZone::listIdentifiers();
                         if (in_array($val, $zones)) {
                             $newVal = $val;
                         }
                     }
                 }
             }
         }
         $settings_mapper = new Mapper_Settings();
         $settings_mapper->updateSettingForUserid($userid, $setting['name'], $newVal);
     }
     Helper_Message::setSuccess($this->app, "Your settings were updated.");
     $this->app->redirect('/settings');
     die;
 }