function testLogin($arr) { $email = $arr["username"]; $password = $arr["password"]; $testformat = new testFormat(); if ($testformat->testLogin($email, $password)) { $user = UserAction::login($email, $password); if (!($user === UserAction::$LOGIN_FAIL)) { $_SESSION['user'] = $user; $result = new Response(true, ""); return $result; } else { $result = new Response(false, "错误的账号/密码"); return $result; } } else { $result = new Response(false, "错误的格式,请按照相应格式输入"); return $result; } }
function testAddMeal($arr) { $meal = $arr["mealName"]; $testformat = new testFormat(); if ($testformat->testMeal($meal)) { $flag = MealAction::addMeal($meal); if ($flag === MealAction::$ADD_MEAL_MEAL_NAME_DUPLICATE) { $result = new Response(false, "the meal had been in the list"); return $result; } elseif ($flag === MealAction::$ADD_MEAL_FAIL) { $result = new Response(false, "add meal fail"); return $result; } elseif (isset($flag)) { return $flag; } } else { $result = new Response(false, "wrong format"); return $result; } return new Response(false, "服务器故障"); }
function testReg($arr) { $checkFormat = true; $email = $arr["email"]; $psw = $arr["password"]; $username = $arr["username"]; $nickname = "nickname"; $department = "百姓网"; //$nickname = ($_POST["nickname"]); //$department = ($_POST["department"]); $location = $arr["location"]; $description = $arr["taste"]; $testformat = new testFormat(); if (!$testformat->testReg($email, $psw, $username, $nickname, $department, $location, $description)) { $result = new Response(false, "格式错误,请按照一定的格式输入!"); return $result; } $fname = $_FILES['savator']["name"]; $cache_path = "icon/"; $uniqStr = uniqid(strtotime("now") . "_" . mt_rand(100000, 999999) . "_"); $suffix = strtolower(stristr($fname, ".")); $fname_new = $cache_path . $uniqStr . $suffix; // $upFilePath = "icon/".$_FILES['savator']['name']; if ($suffix != '' and $suffix != null) { if ($suffix != ".png" and $suffix != ".jpg" and $suffix != ".jpeg" and $suffix != ".gif") { $result = new Response(false, "wrong file type"); return $result; } $ok = move_uploaded_file($_FILES['savator']['tmp_name'], $fname_new); if ($ok === FALSE) { $result = new Response(false, "upload fail"); return $result; } else { $icon = $fname_new; if ($checkFormat) { $user = UserAction::register($email, $psw, $username, $nickname, $department, $location, $description, $icon); if ($user === UserAction::$REGISTER_FAIL) { $result = new Response(false, "注册失败!"); return $result; } elseif ($user === UserAction::$REGISTER_EMAIL_DUPLICATE) { $result = new Response(false, "该邮箱已被注册!"); return $result; } elseif (isset($user)) { $_SESSION['user'] = $user; $result = new Response(true); return $result; } } } } elseif ($checkFormat) { $icon = ""; $user = UserAction::register($email, $psw, $username, $nickname, $department, $location, $description, $icon); if ($user === UserAction::$REGISTER_FAIL) { $result = new Response(false, "注册失败!"); return $result; } elseif ($user === UserAction::$REGISTER_EMAIL_DUPLICATE) { $result = new Response(false, "该邮箱已被注册!"); return $result; } elseif (isset($user)) { $_SESSION['user'] = $user; $result = new Response(true); return $result; } } return new Response(false, "服务器故障"); }
/** * Created by PhpStorm. * User: Administrator * Date: 2015/7/16 * Time: 11:29 */ header("Content-Type: text/html; charset=utf-8"); require_once "../entity/User.php"; require_once "../config.php"; require_once "../provider/Database.php"; require_once "../action/UserAction.php"; require_once "../provider/testFormat.php"; require_once "response/LoginResponse.php"; session_start(); $arr = $_POST; $email = $arr["username"]; $password = $arr["password"]; $format = new testFormat(); $result = NULL; if ($format->testLogin($email, $password)) { $user = UserAction::login($email, $password); if ($user instanceof User) { $_SESSION['user'] = $user; $result = new LoginResponse(true, "", $user->userId, $user->username, $user->nickname); } else { $result = new LoginResponse(false, "用户名密码不匹配"); } } else { $result = new LoginResponse(false, "输入格式有误"); } echo json_encode($result);