index.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. /*
  156. if(empty($_SERVER["HTTPS"])){
  157. //本地开发
  158. $ok = setcookie("user_uid", $user_uuid, $ExpTime , "/");
  159. setcookie("user_id", $Fetch[0]["id"], $ExpTime,"/");
  160. setcookie("token", $jwt, $ExpTime,"/");
  161. }else{
  162. //服务器运行
  163. setcookie("user_uid", $user_uuid, $ExpTime,"/");
  164. setcookie("user_id", $Fetch[0]["id"], $ExpTime,"/");
  165. setcookie("token", $jwt, $ExpTime,"/");
  166. }
  167. */
  168. #给js用的
  169. setcookie("curr-time", time(), $ExpTime,"/");
  170. setcookie("user_id", $uid, $ExpTime,"/","",false,true);
  171. setcookie("user_uid", $user_uuid, $ExpTime,"/","",false,true);
  172. setcookie("user_uid_1", $user_uuid,["expires"=>$ExpTime,"path"=>"/","secure"=>false,"httponly"=>true]);
  173. setcookie("uid", $uid, $ExpTime,"/");
  174. setcookie("username", $username, $ExpTime,"/");
  175. setcookie("userid", $user_uuid, $ExpTime,"/");
  176. setcookie("nickname", $nickname, $ExpTime,"/");
  177. setcookie("email", $email, $ExpTime,"/");
  178. if (isset($_POST["url"])) {
  179. $goto_url = $_POST["url"];
  180. }
  181. #设置新密码
  182. if (isset($_COOKIE["url"])) {
  183. setcookie("pwd_set", "on", time() + 60, "/");
  184. }
  185. ?>
  186. <!DOCTYPE html>
  187. <html>
  188. <head>
  189. <title>wikipali starting</title>
  190. <?php
  191. if (isset($goto_url)) {
  192. $goto = $goto_url;
  193. } else {
  194. $goto = "../studio/index.php";
  195. }
  196. ?>
  197. <meta http-equiv="refresh" content="0,<?php echo $goto; ?>"/>
  198. <script>
  199. localStorage.setItem('token',"<?php echo $jwt; ?>");
  200. </script>
  201. </head>
  202. <body>
  203. <br>
  204. <br>
  205. <p align="center"><a href="../studio/index.php">Auto Redirecting to Homepage! IF NOT WORKING, CLICK HERE</a></p>
  206. </body>
  207. </html>
  208. <?php
  209. exit;
  210. } else {
  211. //用户名不存在
  212. $_post_error = $_local->gui->incorrect_ID_PASS;
  213. }
  214. }
  215. }
  216. }
  217. ?>
  218. <!DOCTYPE html>
  219. <html>
  220. <head>
  221. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  222. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  223. <link type="text/css" rel="stylesheet" href="../studio/css/font.css"/>
  224. <link type="text/css" rel="stylesheet" href="../studio/css/style.css"/>
  225. <link type="text/css" rel="stylesheet" href="../studio/css/color_day.css" id="colorchange" />
  226. <title>wikipali login</title>
  227. <script src="../public/js/comm.js"></script>
  228. <script src="../studio/js/jquery-3.3.1.min.js"></script>
  229. <script src="../studio/js/fixedsticky.js"></script>
  230. <style>
  231. #login_body{
  232. display: flex;
  233. padding: 2em;
  234. margin: auto;
  235. }
  236. #login_left {
  237. padding-right: 12em;
  238. padding-top: 5em;
  239. }
  240. .title{
  241. font-size: 150%;
  242. margin-top: 1em;
  243. margin-bottom: 0.5em;
  244. }
  245. #login_form{
  246. padding: 2em 0 1em 0;
  247. }
  248. #tool_bar {
  249. padding: 1em;
  250. display: flex;
  251. justify-content: space-between;
  252. }
  253. #login_shortcut {
  254. display: flex;
  255. flex-direction: column;
  256. padding: 2em 0;
  257. }
  258. #login_shortcut button{
  259. height:3em;
  260. }
  261. #button_area{
  262. text-align: right;
  263. padding: 1em 0;
  264. }
  265. .form_help{
  266. font-weight: 400;
  267. color: var(--bookx);
  268. }
  269. .login_form input{
  270. margin-top:2em;
  271. padding:0.5em 0.5em;
  272. }
  273. .login_form select{
  274. margin-top:2em;
  275. padding:0.5em 0.5em;
  276. }
  277. .login_form input[type="submit"]{
  278. margin-top:2em;
  279. padding:0.1em 0.5em;
  280. }
  281. .form_error{
  282. color:var(--error-text);
  283. }
  284. #login_form_div{
  285. width:30em;
  286. }
  287. #ucenter_body {
  288. display: flex;
  289. flex-direction: column;
  290. margin: 0;
  291. padding: 0;
  292. background-color: var(--tool-bg-color3);
  293. color: var(--btn-color);
  294. }
  295. .icon_big {
  296. height: 2em;
  297. width: 2em;
  298. fill: var(--btn-color);
  299. transition: all 0.2s ease;
  300. }
  301. .form_field_name{
  302. position: absolute;
  303. margin-left: 7px;
  304. margin-top: 2em;
  305. color: var(--btn-border-line-color);
  306. -webkit-transition-duration: 0.4s;
  307. -moz-transition-duration: 0.4s;
  308. transition-duration: 0.4s;
  309. transform: translateY(0.5em);
  310. }
  311. .viewswitch_on {
  312. position: absolute;
  313. margin-left: 7px;
  314. margin-top: 1.5em;
  315. color: var(--bookx);
  316. -webkit-transition-duration: 0.4s;
  317. -moz-transition-duration: 0.4s;
  318. transition-duration: 0.4s;
  319. transform: translateY(-15px);
  320. }
  321. </style>
  322. <script>
  323. function login_init(){
  324. $("input").focus(function(){
  325. let name = $(this).attr("name");
  326. var objNave = document.getElementById("tip_"+name);
  327. objNave.className = "viewswitch_on";
  328. });
  329. $(".form_field_name").click(function(){
  330. let id = $(this).attr("id");
  331. var objNave = document.getElementById(id);
  332. objNave.className = "viewswitch_on";
  333. let arrId=id.split("_");
  334. document.getElementById('input_'+arrId[1]).focus();
  335. });
  336. }
  337. </script>
  338. <link type="text/css" rel="stylesheet" href="mobile.css" media="screen and (max-width:800px)">
  339. </head>
  340. <body id="ucenter_body" onload="login_init()">
  341. <div id="tool_bar">
  342. <div>
  343. </div>
  344. <div>
  345. <?php
  346. require_once '../lang/lang.php';
  347. ?>
  348. </div>
  349. </div>
  350. <div id="login_body" >
  351. <div id="login_left">
  352. <div >
  353. <svg style="height: 8em;width: 25em;">
  354. <use xlink:href="../public/images/svg/wikipali_login_page.svg#logo_login"></use>
  355. </svg>
  356. </div>
  357. <div style=" padding: 1em 0 0 3.5em;font-weight: 400;">
  358. <?php echo $_local->gui->pali_literature_platform; ?>
  359. <ul style="padding-left: 1.2em;">
  360. <li><?php echo $_local->gui->online_dict_db; ?></li>
  361. <li><?php echo $_local->gui->user_data_share; ?></li>
  362. <li><?php echo $_local->gui->cooperate_edit; ?></li>
  363. </ul>
  364. </div>
  365. </div>
  366. <div id="login_right">
  367. <div id = "login_form_div" class="fun_block" >
  368. <?php
  369. if (isset($error_comm)) {
  370. echo '<div class="form_error">';
  371. echo $error_comm;
  372. echo '</div>';
  373. }
  374. if (isset($message_comm)) {
  375. echo '<div class="form_help">';
  376. echo $message_comm;
  377. echo '</div>';
  378. }
  379. if ($op == "new") {
  380. //新建账号
  381. ?>
  382. <div class="title">
  383. <?php echo $_local->gui->join_wikipali; ?>
  384. </div>
  385. <div class="login_new">
  386. <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>
  387. </div>
  388. <div class="login_form" style=" padding: 3em 0 3em 0;">
  389. <form action="index.php" method="post">
  390. <div>
  391. <div>
  392. <span id='tip_username' class='form_field_name'><?php echo $_local->gui->account; ?></span>
  393. <input type="input" name="username" value="<?php echo $post_username; ?>" />
  394. </div>
  395. <div id="error_username" class="form_error">
  396. <?php
  397. if (isset($error_username)) {echo $error_username;}
  398. ?>
  399. </div>
  400. <div class="form_help">
  401. <?php echo $_local->gui->account_demond; ?>
  402. </div>
  403. <div>
  404. <span id='tip_email' class='form_field_name'><?php echo $_local->gui->email_address; ?></span>
  405. <input type="input" name="email" value="<?php echo $post_email; ?>" />
  406. <div id="error_email" class="form_error">
  407. <?php
  408. if (isset($error_email)) {echo $error_email;}
  409. ?>
  410. </div>
  411. </div>
  412. <div>
  413. <span id='tip_password' class='form_field_name'><?php echo $_local->gui->password; ?></span>
  414. <input type="password" name="password" placeholder="<?php echo $_local->gui->password; ?>" value="<?php echo $post_password; ?>" />
  415. <input type="password" name="repassword" placeholder="<?php echo $_local->gui->password_again; ?>" value="<?php echo $post_password; ?>" />
  416. </div>
  417. <div class="form_help">
  418. <?php echo $_local->gui->password_demond; ?>
  419. </div>
  420. <div id="error_password" class="form_error">
  421. <?php
  422. if (isset($error_password)) {echo $error_password;}
  423. ?>
  424. </div>
  425. <div>
  426. <span id='tip_language' class='viewswitch_on'><?php echo "惯常使用的语言"; ?></span>
  427. <select name="language" style="width: 100%;">
  428. <?php
  429. $currLang = $_COOKIE["language"];
  430. $langList = [
  431. "en"=>$_local->language->en,
  432. "zh-cn"=>$_local->language->zh_cn,
  433. "zh-tw"=>$_local->language->zh_tw,
  434. "my"=>$_local->language->my,
  435. "si"=>$_local->language->si,
  436. ];
  437. foreach ($langList as $key => $value) {
  438. # code...
  439. if($currLang==$key){
  440. $selected = " selected";
  441. }else{
  442. $selected = "";
  443. }
  444. echo "<option value='{$key}' {$selected}>{$value}</option>";
  445. }
  446. ?>
  447. </select>
  448. </div>
  449. <div>
  450. <span id='tip_nickname' class='form_field_name'><?php echo $_local->gui->nick_name; ?></span>
  451. <input type="input" name="nickname" value="<?php echo $post_nickname; ?>" />
  452. </div>
  453. <?php
  454. if (isset($error_nickname)) {
  455. echo '<div id="error_nickname" class="form_error">';
  456. echo $error_nickname;
  457. echo '</div>';
  458. }
  459. else{
  460. echo '<div class="form_help">';
  461. echo $_local->gui->name_for_show;
  462. echo '</div>';
  463. }
  464. ?>
  465. <input type="hidden" name="op" value="new" />
  466. <input type="hidden" name="invite" value="<?php echo $_REQUEST["invite"]; ?>" />
  467. </div>
  468. <div id="button_area">
  469. <input type="submit" value="<?php echo $_local->gui->continue; ?>" style="background-color: var(--link-hover-color);border-color: var(--link-hover-color);" />
  470. </div>
  471. </form>
  472. </div>
  473. <?php
  474. } else {
  475. ?>
  476. <div class="title">
  477. <?php
  478. if (isset($_POST["username"]) && $_username_ok == true) {
  479. echo $_POST["username"];
  480. } else {
  481. echo $_local->gui->login;
  482. }
  483. ?>
  484. </div>
  485. <div class="login_new">
  486. <?php
  487. if (isset($_POST["username"]) && $_username_ok == true) {
  488. //已经输入用户名
  489. echo '<a href="index.php?language=' . $currLanguage . '">切换账户</a>';
  490. } else {
  491. 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>';
  492. }
  493. ?>
  494. <a href="forgot_pwd.php">忘记密码</a>
  495. <div class="login_form" style="padding: 3em 0 3em 0;">
  496. <form action="index.php" method="post">
  497. <div>
  498. <?php
  499. if (isset($goto_url)) {
  500. echo "<input type=\"hidden\" name=\"url\" value=\"{$goto_url}\" />";
  501. } else if (isset($_POST["url"])) {
  502. echo "<input type=\"hidden\" name=\"url\" value=\"{$_POST["url"]}\" />";
  503. }
  504. if (isset($_POST["username"]) && $_username_ok == true) {
  505. echo "<span id='tip_password' class='form_field_name'>" . $_local->gui->password . "</span>";
  506. echo '<input type="password" name="password" />';
  507. echo "<input type=\"hidden\" name=\"username\" value=\"{$_POST["username"]}\" />";
  508. if (isset($_post_error)) {
  509. echo '<div id="error_nikename" class="form_error">';
  510. echo $_post_error;
  511. echo '</div>';
  512. }
  513. } else {
  514. echo "<span id='tip_username' class='form_field_name'>" . $_local->gui->account . "/" . $_local->gui->e_mail . "</span>";
  515. echo '<input type="input" name="username" id="input_username" />';
  516. if (isset($_post_error)) {
  517. echo '<div id="error_nikename" class="form_error">';
  518. echo $_post_error;
  519. echo '</div>';
  520. }
  521. }
  522. ?>
  523. </div>
  524. <div id="button_area">
  525. <input type="submit" value="<?php echo $_local->gui->continue; ?>" style="background-color: var(--link-hover-color);border-color: var(--link-hover-color);" />
  526. </div>
  527. </form>
  528. </div>
  529. <div id="login_shortcut" style="display:none;">
  530. <button class="form_help"><?php echo $_local->gui->login_with_google; ?>&nbsp;
  531. <svg class="icon">
  532. <use xlink:href="../studio/svg/icon.svg#google_logo"></use>
  533. </svg>
  534. </button>
  535. <button class="form_help"><?php echo $_local->gui->login_with_facebook; ?>&nbsp;
  536. <svg class="icon">
  537. <use xlink:href="../studio/svg/icon.svg#facebook_logo"></use>
  538. </svg>
  539. </button>
  540. <button class="form_help"><?php echo $_local->gui->login_with_wechat; ?>&nbsp;
  541. <svg class="icon">
  542. <use xlink:href="../studio/svg/icon.svg#wechat_logo"></use>
  543. </svg>
  544. </button>
  545. </div>
  546. <?php
  547. }
  548. ?>
  549. </div>
  550. </div>
  551. </div>
  552. <script>
  553. login_init();
  554. </script>
  555. </body>
  556. </html>