index.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. <?php
  2. require_once '../config.php';
  3. require_once "../public/load_lang.php";
  4. require_once "../public/_pdo.php";
  5. require_once "../public/function.php";
  6. require_once "../redis/function.php";
  7. // Require Composer's autoloader.
  8. require_once '../../vendor/autoload.php';
  9. use Firebase\JWT\JWT;
  10. use Firebase\JWT\Key;
  11. if (isset($_REQUEST["op"])) {
  12. $op = $_REQUEST["op"];
  13. } else {
  14. $op = "login";
  15. }
  16. switch ($op) {
  17. case "login":
  18. if (isset($_GET["url"])) {
  19. $goto_url = $_GET["url"];
  20. }
  21. break;
  22. case "logout":
  23. if (isset($_COOKIE["username"])) {
  24. $message_comm = $_local->gui->user . " " . $_COOKIE["username"] . " " . $_local->gui->loged_out;
  25. }
  26. setcookie("user_uid", "", time() - 60, "/");
  27. setcookie("user_id", "", time() - 60, "/");
  28. setcookie("token", "", time() - 60, "/");
  29. setcookie("uid", "", time() - 60, "/");
  30. setcookie("username", "", time() - 60, "/");
  31. setcookie("userid", "", time() - 60, "/");
  32. setcookie("nickname", "", time() - 60, "/");
  33. setcookie("email", "", time() - 60, "/");
  34. break;
  35. case "new":
  36. $host = $_SERVER['HTTP_HOST'];
  37. //if (strpos($host, "wikipali.org") !== false)
  38. {
  39. if(isset($_REQUEST["invite"])){
  40. $redis = redis_connect();
  41. if ($redis == false) {
  42. echo "no redis connect\n";
  43. exit;
  44. }
  45. $code = $redis->exists("invitecode://".$_REQUEST["invite"]);
  46. if(!$code){
  47. echo "无效的邀请码,或邀请码已经过期。";
  48. exit;
  49. }
  50. $invite_email = $redis->get("invitecode://".$_REQUEST["invite"]);
  51. }else{
  52. echo "无邀请码";
  53. exit;
  54. }
  55. }
  56. break;
  57. }
  58. $post_nickname = "";
  59. $post_username = "";
  60. $post_password = "";
  61. $post_email = "";
  62. if (isset($_POST["op"]) && $_POST["op"] == "new") {
  63. PDO_Connect( _FILE_DB_USERINFO_ , _DB_USERNAME_ , _DB_PASSWORD_);
  64. //建立账号
  65. $op = "new";
  66. $post_username = trim($_POST["username"]);
  67. $post_password = trim($_POST["password"]);
  68. $post_nickname = trim($_POST["nickname"]);
  69. $post_email = trim($_POST["email"]);
  70. $post_error = false;
  71. if (empty($post_username)) {
  72. $error_username = $_local->gui->account . $_local->gui->cannot_empty;
  73. $post_error = true;
  74. }
  75. else{
  76. $query = "SELECT count(*) as co from "._TABLE_USER_INFO_." where username = ?" ;
  77. $iFetch = PDO_FetchOne($query,array($post_username));
  78. if ($iFetch > 0) { //username is existed
  79. $error_username = $_local->gui->account_existed;
  80. $post_error = true;
  81. }
  82. }
  83. if (empty($post_email)) {
  84. $error_email = $_local->gui->email . $_local->gui->cannot_empty;
  85. $post_error = true;
  86. }else{
  87. $query = "SELECT count(*) as co from "._TABLE_USER_INFO_." where email = ?" ;
  88. $iFetch = PDO_FetchOne($query,array($post_email));
  89. if ($iFetch > 0) { //username is existed
  90. $error_email = $_local->gui->email . "已经存在";
  91. $post_error = true;
  92. }
  93. }
  94. if (empty($post_password)) {
  95. $error_password = $_local->gui->password . $_local->gui->cannot_empty;
  96. $post_error = true;
  97. }else{
  98. if(strlen($post_password)<6){
  99. $error_password = $_local->gui->password . "过短";
  100. $post_error = true;
  101. }
  102. }
  103. if (empty($post_nickname)) {
  104. $error_nickname = $_local->gui->nick_name . $_local->gui->cannot_empty;
  105. $post_error = true;
  106. }
  107. if (!$post_error) {
  108. $md5_password = md5($post_password);
  109. $new_userid = UUID::v4();
  110. $query = "INSERT INTO "._TABLE_USER_INFO_." ('id','userid','username','password','nickname','email') VALUES (NULL," . $PDO->quote($new_userid) . "," . $PDO->quote($post_username) . "," . $PDO->quote($md5_password) . "," . $PDO->quote($post_nickname) . "," . $PDO->quote($post_email) . ")";
  111. $stmt = @PDO_Execute($query);
  112. if (!$stmt || ($stmt && $stmt->errorCode() != 0)) {
  113. $error = PDO_ErrorInfo();
  114. $error_comm = $error[2] . "系统错误,抱歉!请再试一次";
  115. } else {
  116. $message_comm = "新账户建立成功";
  117. $op = "login";
  118. unset($_POST["username"]);
  119. //TODO create channel
  120. //TODO create studio
  121. }
  122. }
  123. } else {
  124. //登录
  125. if (isset($_POST["username"])) {
  126. $_username_ok = true;
  127. if ($_POST["username"] == "") {
  128. $_username_ok = false;
  129. $_post_error = $_local->gui->account . $_local->gui->account_existed;
  130. } else if (isset($_POST["password"])) {
  131. $md5_password = md5($_POST["password"]);
  132. PDO_Connect(_FILE_DB_USERINFO_);
  133. $query = "SELECT * from "._TABLE_USER_INFO_." where (\"username\"=" . $PDO->quote($_POST["username"]) . " or \"email\"=" . $PDO->quote($_POST["username"]) . " ) and \"password\"=" . $PDO->quote($md5_password);
  134. $Fetch = PDO_FetchAll($query);
  135. $iFetch = count($Fetch);
  136. if ($iFetch > 0) {
  137. //验证成功
  138. $uid = $Fetch[0]["id"];
  139. $username = $Fetch[0]["username"];
  140. $user_uuid = $Fetch[0]["userid"];
  141. $nickname = $Fetch[0]["nickname"];
  142. $email = $Fetch[0]["email"];
  143. $ExpTime = time() + 60 * 60 * 24 * 365;
  144. //JWT
  145. $key = APP_KEY;
  146. $payload = [
  147. 'nbf' => time(),
  148. 'exp' => $ExpTime,
  149. 'uid' => $user_uuid,
  150. 'id' => $uid
  151. ];
  152. $jwt = JWT::encode($payload,$key,'HS512');
  153. //End of JWT
  154. // set cookie
  155. if(empty($_SERVER["HTTPS"])){
  156. //本地开发
  157. setcookie("user_uid", $user_uuid,["expires"=>$ExpTime,"path"=>"/","secure"=>false,"httponly"=>true]);
  158. setcookie("user_id", $Fetch[0]["id"], ["expires"=>$ExpTime,"path"=>"/","secure"=>false,"httponly"=>true]);
  159. setcookie("token", $jwt, ["expires"=>$ExpTime,"path"=>"/","secure"=>false,"httponly"=>true]);
  160. }else{
  161. //服务器运行
  162. setcookie("user_uid", $user_uuid, ["expires"=>$ExpTime,"path"=>"/","secure"=>true,"httponly"=>true]);
  163. setcookie("user_id", $Fetch[0]["id"], ["expires"=>$ExpTime,"path"=>"/","secure"=>true,"httponly"=>true]);
  164. setcookie("token", $jwt, ["expires"=>$ExpTime,"path"=>"/","secure"=>true,"httponly"=>true]);
  165. }
  166. #给js用的
  167. setcookie("mint_token", $jwt, time()+60*60*24*365,"/");
  168. setcookie("uid", $uid, time()+60*60*24*365,"/");
  169. setcookie("username", $username, time()+60*60*24*365,"/");
  170. setcookie("userid", $user_uuid, time()+60*60*24*365,"/");
  171. setcookie("nickname", $nickname, time()+60*60*24*365,"/");
  172. setcookie("email", $email, time()+60*60*24*365,"/");
  173. if (isset($_POST["url"])) {
  174. $goto_url = $_POST["url"];
  175. }
  176. #设置新密码
  177. if (isset($_COOKIE["url"])) {
  178. setcookie("pwd_set", "on", time() + 60, "/");
  179. }
  180. ?>
  181. <!DOCTYPE html>
  182. <html>
  183. <head>
  184. <title>wikipali starting</title>
  185. <?php
  186. if (isset($goto_url)) {
  187. $goto = $goto_url;
  188. } else {
  189. $goto = "../studio/index.php";
  190. }
  191. ?>
  192. <meta http-equiv="refresh" content="0,<?php echo $goto; ?>"/>
  193. <script>
  194. localStorage.setItem('token',"<?php echo $jwt; ?>");
  195. </script>
  196. </head>
  197. <body>
  198. <br>
  199. <br>
  200. <p align="center"><a href="../studio/index.php">Auto Redirecting to Homepage! IF NOT WORKING, CLICK HERE</a></p>
  201. </body>
  202. </html>
  203. <?php
  204. exit;
  205. } else {
  206. //用户名不存在
  207. $_post_error = $_local->gui->incorrect_ID_PASS;
  208. }
  209. }
  210. }
  211. }
  212. ?>
  213. <!DOCTYPE html>
  214. <html>
  215. <head>
  216. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  217. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  218. <link type="text/css" rel="stylesheet" href="../studio/css/font.css"/>
  219. <link type="text/css" rel="stylesheet" href="../studio/css/style.css"/>
  220. <link type="text/css" rel="stylesheet" href="../studio/css/color_day.css" id="colorchange" />
  221. <title>wikipali login</title>
  222. <script src="../public/js/comm.js"></script>
  223. <script src="../studio/js/jquery-3.3.1.min.js"></script>
  224. <script src="../studio/js/fixedsticky.js"></script>
  225. <style>
  226. #login_body{
  227. display: flex;
  228. padding: 2em;
  229. margin: auto;
  230. }
  231. #login_left {
  232. padding-right: 12em;
  233. padding-top: 5em;
  234. }
  235. .title{
  236. font-size: 150%;
  237. margin-top: 1em;
  238. margin-bottom: 0.5em;
  239. }
  240. #login_form{
  241. padding: 2em 0 1em 0;
  242. }
  243. #tool_bar {
  244. padding: 1em;
  245. display: flex;
  246. justify-content: space-between;
  247. }
  248. #login_shortcut {
  249. display: flex;
  250. flex-direction: column;
  251. padding: 2em 0;
  252. }
  253. #login_shortcut button{
  254. height:3em;
  255. }
  256. #button_area{
  257. text-align: right;
  258. padding: 1em 0;
  259. }
  260. .form_help{
  261. font-weight: 400;
  262. color: var(--bookx);
  263. }
  264. .login_form input{
  265. margin-top:2em;
  266. padding:0.5em 0.5em;
  267. }
  268. .login_form select{
  269. margin-top:2em;
  270. padding:0.5em 0.5em;
  271. }
  272. .login_form input[type="submit"]{
  273. margin-top:2em;
  274. padding:0.1em 0.5em;
  275. }
  276. .form_error{
  277. color:var(--error-text);
  278. }
  279. #login_form_div{
  280. width:30em;
  281. }
  282. #ucenter_body {
  283. display: flex;
  284. flex-direction: column;
  285. margin: 0;
  286. padding: 0;
  287. background-color: var(--tool-bg-color3);
  288. color: var(--btn-color);
  289. }
  290. .icon_big {
  291. height: 2em;
  292. width: 2em;
  293. fill: var(--btn-color);
  294. transition: all 0.2s ease;
  295. }
  296. .form_field_name{
  297. position: absolute;
  298. margin-left: 7px;
  299. margin-top: 2em;
  300. color: var(--btn-border-line-color);
  301. -webkit-transition-duration: 0.4s;
  302. -moz-transition-duration: 0.4s;
  303. transition-duration: 0.4s;
  304. transform: translateY(0.5em);
  305. }
  306. .viewswitch_on {
  307. position: absolute;
  308. margin-left: 7px;
  309. margin-top: 1.5em;
  310. color: var(--bookx);
  311. -webkit-transition-duration: 0.4s;
  312. -moz-transition-duration: 0.4s;
  313. transition-duration: 0.4s;
  314. transform: translateY(-15px);
  315. }
  316. </style>
  317. <script>
  318. function login_init(){
  319. $("input").focus(function(){
  320. let name = $(this).attr("name");
  321. var objNave = document.getElementById("tip_"+name);
  322. objNave.className = "viewswitch_on";
  323. });
  324. $(".form_field_name").click(function(){
  325. let id = $(this).attr("id");
  326. var objNave = document.getElementById(id);
  327. objNave.className = "viewswitch_on";
  328. let arrId=id.split("_");
  329. document.getElementById('input_'+arrId[1]).focus();
  330. });
  331. }
  332. </script>
  333. <link type="text/css" rel="stylesheet" href="mobile.css" media="screen and (max-width:800px)">
  334. </head>
  335. <body id="ucenter_body" onload="login_init()">
  336. <div id="tool_bar">
  337. <div>
  338. </div>
  339. <div>
  340. <?php
  341. require_once '../lang/lang.php';
  342. ?>
  343. </div>
  344. </div>
  345. <div id="login_body" >
  346. <div id="login_left">
  347. <div >
  348. <svg style="height: 8em;width: 25em;">
  349. <use xlink:href="../public/images/svg/wikipali_login_page.svg#logo_login"></use>
  350. </svg>
  351. </div>
  352. <div style=" padding: 1em 0 0 3.5em;font-weight: 400;">
  353. <?php echo $_local->gui->pali_literature_platform; ?>
  354. <ul style="padding-left: 1.2em;">
  355. <li><?php echo $_local->gui->online_dict_db; ?></li>
  356. <li><?php echo $_local->gui->user_data_share; ?></li>
  357. <li><?php echo $_local->gui->cooperate_edit; ?></li>
  358. </ul>
  359. </div>
  360. </div>
  361. <div id="login_right">
  362. <div id = "login_form_div" class="fun_block" >
  363. <?php
  364. if (isset($error_comm)) {
  365. echo '<div class="form_error">';
  366. echo $error_comm;
  367. echo '</div>';
  368. }
  369. if (isset($message_comm)) {
  370. echo '<div class="form_help">';
  371. echo $message_comm;
  372. echo '</div>';
  373. }
  374. if ($op == "new") {
  375. //新建账号
  376. ?>
  377. <div class="title">
  378. <?php echo $_local->gui->join_wikipali; ?>
  379. </div>
  380. <div class="login_new">
  381. <span class="form_help"><?php echo $_local->gui->have_account; ?> ?</span><a href="index.php?language=<?php echo $currLanguage; ?>">&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $_local->gui->login; //登入账户 ?></a>
  382. </div>
  383. <div class="login_form" style=" padding: 3em 0 3em 0;">
  384. <form action="index.php" method="post">
  385. <div>
  386. <div>
  387. <span id='tip_username' class='form_field_name'><?php echo $_local->gui->account; ?></span>
  388. <input type="input" name="username" value="<?php echo $post_username; ?>" />
  389. </div>
  390. <div id="error_username" class="form_error">
  391. <?php
  392. if (isset($error_username)) {echo $error_username;}
  393. ?>
  394. </div>
  395. <div class="form_help">
  396. <?php echo $_local->gui->account_demond; ?>
  397. </div>
  398. <div>
  399. <span id='tip_email' class='form_field_name'><?php echo $_local->gui->email_address; ?></span>
  400. <input type="input" name="email" value="<?php echo $post_email; ?>" />
  401. <div id="error_email" class="form_error">
  402. <?php
  403. if (isset($error_email)) {echo $error_email;}
  404. ?>
  405. </div>
  406. </div>
  407. <div>
  408. <span id='tip_password' class='form_field_name'><?php echo $_local->gui->password; ?></span>
  409. <input type="password" name="password" placeholder="<?php echo $_local->gui->password; ?>" value="<?php echo $post_password; ?>" />
  410. <input type="password" name="repassword" placeholder="<?php echo $_local->gui->password_again; ?>" value="<?php echo $post_password; ?>" />
  411. </div>
  412. <div class="form_help">
  413. <?php echo $_local->gui->password_demond; ?>
  414. </div>
  415. <div id="error_password" class="form_error">
  416. <?php
  417. if (isset($error_password)) {echo $error_password;}
  418. ?>
  419. </div>
  420. <div>
  421. <span id='tip_language' class='viewswitch_on'><?php echo "惯常使用的语言"; ?></span>
  422. <select name="language" style="width: 100%;">
  423. <?php
  424. $currLang = $_COOKIE["language"];
  425. $langList = [
  426. "en"=>$_local->language->en,
  427. "zh-cn"=>$_local->language->zh_cn,
  428. "zh-tw"=>$_local->language->zh_tw,
  429. "my"=>$_local->language->my,
  430. "si"=>$_local->language->si,
  431. ];
  432. foreach ($langList as $key => $value) {
  433. # code...
  434. if($currLang==$key){
  435. $selected = " selected";
  436. }else{
  437. $selected = "";
  438. }
  439. echo "<option value='{$key}' {$selected}>{$value}</option>";
  440. }
  441. ?>
  442. </select>
  443. </div>
  444. <div>
  445. <span id='tip_nickname' class='form_field_name'><?php echo $_local->gui->nick_name; ?></span>
  446. <input type="input" name="nickname" value="<?php echo $post_nickname; ?>" />
  447. </div>
  448. <?php
  449. if (isset($error_nickname)) {
  450. echo '<div id="error_nickname" class="form_error">';
  451. echo $error_nickname;
  452. echo '</div>';
  453. }
  454. else{
  455. echo '<div class="form_help">';
  456. echo $_local->gui->name_for_show;
  457. echo '</div>';
  458. }
  459. ?>
  460. <input type="hidden" name="op" value="new" />
  461. <input type="hidden" name="invite" value="<?php echo $_REQUEST["invite"]; ?>" />
  462. </div>
  463. <div id="button_area">
  464. <input type="submit" value="<?php echo $_local->gui->continue; ?>" style="background-color: var(--link-hover-color);border-color: var(--link-hover-color);" />
  465. </div>
  466. </form>
  467. </div>
  468. <?php
  469. } else {
  470. ?>
  471. <div class="title">
  472. <?php
  473. if (isset($_POST["username"]) && $_username_ok == true) {
  474. echo $_POST["username"];
  475. } else {
  476. echo $_local->gui->login;
  477. }
  478. ?>
  479. </div>
  480. <div class="login_new">
  481. <?php
  482. if (isset($_POST["username"]) && $_username_ok == true) {
  483. //已经输入用户名
  484. echo '<a href="index.php?language=' . $currLanguage . '">切换账户</a>';
  485. } else {
  486. echo '<span class="form_help">' . $_local->gui->new_to_wikipali . ' ?</span><a href="index.php?language=' . $currLanguage . '&op=new">&nbsp;&nbsp;&nbsp;&nbsp;' . $_local->gui->create_account . '</a>';
  487. }
  488. ?>
  489. <a href="forgot_pwd.php">忘记密码</a>
  490. <div class="login_form" style="padding: 3em 0 3em 0;">
  491. <form action="index.php" method="post">
  492. <div>
  493. <?php
  494. if (isset($goto_url)) {
  495. echo "<input type=\"hidden\" name=\"url\" value=\"{$goto_url}\" />";
  496. } else if (isset($_POST["url"])) {
  497. echo "<input type=\"hidden\" name=\"url\" value=\"{$_POST["url"]}\" />";
  498. }
  499. if (isset($_POST["username"]) && $_username_ok == true) {
  500. echo "<span id='tip_password' class='form_field_name'>" . $_local->gui->password . "</span>";
  501. echo '<input type="password" name="password" />';
  502. echo "<input type=\"hidden\" name=\"username\" value=\"{$_POST["username"]}\" />";
  503. if (isset($_post_error)) {
  504. echo '<div id="error_nikename" class="form_error">';
  505. echo $_post_error;
  506. echo '</div>';
  507. }
  508. } else {
  509. echo "<span id='tip_username' class='form_field_name'>" . $_local->gui->account . "/" . $_local->gui->e_mail . "</span>";
  510. echo '<input type="input" name="username" id="input_username" />';
  511. if (isset($_post_error)) {
  512. echo '<div id="error_nikename" class="form_error">';
  513. echo $_post_error;
  514. echo '</div>';
  515. }
  516. }
  517. ?>
  518. </div>
  519. <div id="button_area">
  520. <input type="submit" value="<?php echo $_local->gui->continue; ?>" style="background-color: var(--link-hover-color);border-color: var(--link-hover-color);" />
  521. </div>
  522. </form>
  523. </div>
  524. <div id="login_shortcut" style="display:none;">
  525. <button class="form_help"><?php echo $_local->gui->login_with_google; ?>&nbsp;
  526. <svg class="icon">
  527. <use xlink:href="../studio/svg/icon.svg#google_logo"></use>
  528. </svg>
  529. </button>
  530. <button class="form_help"><?php echo $_local->gui->login_with_facebook; ?>&nbsp;
  531. <svg class="icon">
  532. <use xlink:href="../studio/svg/icon.svg#facebook_logo"></use>
  533. </svg>
  534. </button>
  535. <button class="form_help"><?php echo $_local->gui->login_with_wechat; ?>&nbsp;
  536. <svg class="icon">
  537. <use xlink:href="../studio/svg/icon.svg#wechat_logo"></use>
  538. </svg>
  539. </button>
  540. </div>
  541. <?php
  542. }
  543. ?>
  544. </div>
  545. </div>
  546. </div>
  547. <script>
  548. login_init();
  549. </script>
  550. </body>
  551. </html>