index.php 17 KB

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