index.php 17 KB

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