diff --git a/plugins/Common.php b/plugins/Common.php index 8836ef7..2bf1e6a 100644 --- a/plugins/Common.php +++ b/plugins/Common.php @@ -69,7 +69,9 @@ Class Common { } $cache_filename = __DIR__ . '/../runtime/custom_config_usermap.json'; - file_put_contents($cache_filename, json_encode(compact('tajian_user_map'), JSON_PRETTY_PRINT)); + $saved = file_put_contents($cache_filename, json_encode(compact('tajian_user_map'), JSON_PRETTY_PRINT)); + + return $saved === false ? false : true; } //获取新收藏夹目录名 @@ -171,6 +173,29 @@ Class Common { return true; } + //判断某个收藏夹是否属于当前用户 + public static function isMyFavDir($cellphone, $username, $fav_dir) { + try { + $rootDir = __DIR__ . '/../www/' . FSC::$app['config']['content_directory']; + $rootDir = str_replace("/{$username}", '', $rootDir); //获取当前收藏夹的上一级目录 + + $userDir = "{$rootDir}/{$fav_dir}"; //目标收藏夹目录 + if (!is_dir($userDir)) { //如果不存在 + return false; + } + + $filepath = "{$userDir}/README_cellphone.txt"; + $content = file_get_contents($filepath); + if (!empty($content) && strpos($content, $cellphone) !== false) { + return true; + } + }catch(Exception $e) { + return false; + } + + return false; + } + //根据手机号码获取用户名ID //规则:前6位对 97 求余数,再拼接后5位 public static function getUserId($cellphone){ @@ -235,11 +260,16 @@ Class Common { } //判断用户数据目录是否存在 - public static function getUserDataDir($cellphone) { + public static function getUserDataDir($cellphone, $currentUsername = '') { $rootDir = __DIR__ . '/../www/' . FSC::$app['config']['content_directory']; $username = self::getMappedUsername($cellphone); - $userDir = "{$rootDir}{$username}"; + if (!empty($currentUsername)) { + $userDir = str_replace("/{$currentUsername}", "/{$username}", $rootDir); + }else { + $userDir = "{$rootDir}{$username}"; + } + return is_dir($userDir) ? $userDir : false; } diff --git a/themes/tajian/controller/FrontapiController.php b/themes/tajian/controller/FrontapiController.php index 8069ed4..5e2f852 100644 --- a/themes/tajian/controller/FrontapiController.php +++ b/themes/tajian/controller/FrontapiController.php @@ -1253,5 +1253,82 @@ eof; return $this->renderJson(compact('code', 'msg', 'err')); } + //账号共享接口 + public function actionSharedir() { + $ip = $this->getUserIp(); + $check_time = 120; //2 分钟内 + $max_time_in_minutes = 10; //最多 10 次 + + $isUserGotRequestLimit = $this->requestLimit($ip, $max_time_in_minutes, $check_time); + if ($isUserGotRequestLimit) { + $this->logError("Request limit got, ip: {$ip}"); + throw new Exception('Oops,操作太快了,请喝杯咖啡休息会吧...'); + } + + //只允许修改自己的数据 + $loginedUser = Common::getUserFromSession(); + if (empty($loginedUser['username'])) { + throw new Exception('Oops,你还没登录哦'); + }else if ( + !empty(FSC::$app['config']['multipleUserUriParse']) + && (empty(FSC::$app['user_id']) || FSC::$app['user_id'] != $loginedUser['username']) + ) { + throw new Exception('Oops,请求地址有误'); + } + + //VIP身份判断 + if (empty($loginedUser['cellphone']) || !in_array($loginedUser['cellphone'], FSC::$app['config']['tajian_vip_user'])) { + throw new Exception('Oops,你还不是VIP,请联系首页底部客服邮箱开通。'); + } + + + //返回给视图的变量 + $code = 0; + $msg = ''; + $err = ''; + + //用户提交的数据检查 + $postParams = $this->post(); + if (!empty($postParams)) { + $friends_cellphone = $this->post('cellphone', ''); + $share_dir = $this->post('dir', ''); + + if (empty($friends_cellphone) || Common::isCellphoneNumber($friends_cellphone) == false) { + $err = "请填写正确的手机号码"; + }else if (empty($share_dir)) { + $err = "请选择要共享的账号"; + }else if ($friends_cellphone == $loginedUser['cellphone']) { + $err = "只能共享给朋友,不能共享给自己哦"; + } + + //只能共享属于自己的账号 + if (empty($err)) { + $isMine = Common::isMyFavDir($loginedUser['cellphone'], $loginedUser['username'], $share_dir); + if (empty($isMine)) { + $err = '只能共享自己的账号,朋友共享给你的账号不能再共享给他人'; + }else { + //检查朋友的账号是否存在 + $friend_exist = Common::getUserDataDir($friends_cellphone, $loginedUser['username']); + if (empty($friend_exist)) { + $err = "{$friends_cellphone} 还没注册哦,请朋友先注册吧"; + } + } + } + + if (empty($err)) { //如果数据检查通过,尝试保存 + $saved = Common::saveUserDirMap($friends_cellphone, $share_dir); + + if ($saved !== false) { + $msg = "账号共享完成"; + $code = 1; + }else { + $err = "账号共享失败,请稍后重试"; + } + } + } + + return $this->renderJson(compact('code', 'msg', 'err')); + } + } diff --git a/themes/tajian/controller/MyController.php b/themes/tajian/controller/MyController.php index 509061a..5954083 100644 --- a/themes/tajian/controller/MyController.php +++ b/themes/tajian/controller/MyController.php @@ -66,10 +66,13 @@ Class MyController extends SiteController { //昵称支持 $nickname = $this->getNickname($readmeFile); + //显示手机号码 + $cellphone_hide = preg_replace("/^(.{3,})\d{4}(.{4})$/i", '$1****$2', $loginedUser['cellphone']); + $pageTitle = "{$defaultTitle} | " . FSC::$app['config']['site_name']; $params = compact( 'cateId', 'dirTree', 'scanResults', - 'htmlReadme', 'tags', 'nickname' + 'htmlReadme', 'tags', 'nickname', 'cellphone_hide' ); if (!empty($viewData)) { @@ -123,7 +126,7 @@ Class MyController extends SiteController { //切换收藏夹 public function actionDirs() { - $myDirs = $myNicks = array(); + $myDirs = $myNicks = $isMine = array(); $loginedUser = Common::getUserFromSession(); if (!empty($loginedUser['cellphone'])) { @@ -131,28 +134,60 @@ Class MyController extends SiteController { if (!empty($myDirs)) { foreach($myDirs as $dir) { $myNicks[$dir] = Common::getNicknameByDir($dir, $loginedUser['username']); + $isMine[$dir] = Common::isMyFavDir($loginedUser['cellphone'], $loginedUser['username'], $dir); } } } + //VIP身份判断 + $isVipUser = true; + if (empty($loginedUser['cellphone']) || !in_array($loginedUser['cellphone'], FSC::$app['config']['tajian_vip_user'])) { + $isVipUser = false; + } + $defaultTitle = "切换账号"; $viewName = 'switchdir'; - return $this->actionIndex($viewName, $defaultTitle, compact('myDirs', 'myNicks')); + return $this->actionIndex($viewName, $defaultTitle, compact('myDirs', 'myNicks', 'isMine', 'isVipUser')); } //添加收藏夹 public function actionCreatedir() { - $myDirs = $myNicks = array(); - //VIP身份判断 $loginedUser = Common::getUserFromSession(); + $isVipUser = true; if (empty($loginedUser['cellphone']) || !in_array($loginedUser['cellphone'], FSC::$app['config']['tajian_vip_user'])) { - throw new Exception('Oops,你还不是VIP,请联系首页底部客服邮箱开通。'); + $isVipUser = false; } $defaultTitle = "添加账号"; $viewName = 'createdir'; - return $this->actionIndex($viewName, $defaultTitle); + return $this->actionIndex($viewName, $defaultTitle, compact('isVipUser')); + } + + //共享收藏夹 + public function actionSharedir() { + $myDirs = $myNicks = $isMine = array(); + + $loginedUser = Common::getUserFromSession(); + if (!empty($loginedUser['cellphone'])) { + $myDirs = Common::getMyDirs($loginedUser['cellphone']); + if (!empty($myDirs)) { + foreach($myDirs as $dir) { + $myNicks[$dir] = Common::getNicknameByDir($dir, $loginedUser['username']); + $isMine[$dir] = Common::isMyFavDir($loginedUser['cellphone'], $loginedUser['username'], $dir); + } + } + } + + //VIP身份判断 + $isVipUser = true; + if (empty($loginedUser['cellphone']) || !in_array($loginedUser['cellphone'], FSC::$app['config']['tajian_vip_user'])) { + $isVipUser = false; + } + + $defaultTitle = "共享账号"; + $viewName = 'sharedir'; + return $this->actionIndex($viewName, $defaultTitle, compact('myDirs', 'myNicks', 'isMine', 'isVipUser')); } } \ No newline at end of file diff --git a/themes/tajian/views/my/createdir.php b/themes/tajian/views/my/createdir.php index 95467b5..3946c79 100644 --- a/themes/tajian/views/my/createdir.php +++ b/themes/tajian/views/my/createdir.php @@ -11,10 +11,14 @@ $max_num = !empty(FSC::$app['config']['tajian']['max_dir_num']) ? FSC::$app['con <<返回