function.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. require_once __DIR__ . '/casesuf.inc';
  3. require_once __DIR__ . '/config.php';
  4. require_once __DIR__ . '/../config.php';
  5. // Require Composer's autoloader.
  6. require_once __DIR__ . '/../../vendor/autoload.php';
  7. $_book_index = null; //书的列表
  8. /*
  9. $mode:
  10. 0
  11. */
  12. function getPaliWordBase($word, $mode = 0)
  13. {
  14. global $case;
  15. //去除尾查
  16. $newWord = array();
  17. for ($row = 0; $row < count($case); $row++) {
  18. $len = mb_strlen($case[$row][1], "UTF-8");
  19. $end = mb_substr($word, 0 - $len, null, "UTF-8");
  20. if ($end == $case[$row][1]) {
  21. $base = mb_substr($word, 0, mb_strlen($word, "UTF-8") - $len, "UTF-8") . $case[$row][0];
  22. if ($base != $word) {
  23. $type = ".n.";
  24. $gramma = $case[$row][2];
  25. $parts = "{$base}+{$case[$row][1]}";
  26. if (isset($newWord[$base])) {
  27. array_push($newWord[$base], array("type" => $type,
  28. "gramma" => $gramma,
  29. "parts" => $parts,
  30. ));
  31. } else {
  32. $newWord[$base] = array(array("type" => $type,
  33. "gramma" => $gramma,
  34. "parts" => $parts,
  35. ));
  36. }
  37. }
  38. }
  39. }
  40. return ($newWord);
  41. }
  42. function _load_book_index()
  43. {
  44. global $_book_index, $_dir_lang, $currLanguage, $_dir_book_index;
  45. if (file_exists($_dir_lang . $currLanguage . ".json")) {
  46. $_book_index = json_decode(file_get_contents($_dir_book_index . "a/" . $currLanguage . ".json"));
  47. } else {
  48. $_book_index = json_decode(file_get_contents($_dir_book_index . "a/default.json"));
  49. }
  50. //print_r($_book_index);
  51. }
  52. function _get_book_info($index)
  53. {
  54. global $_book_index;
  55. foreach ($_book_index as $book) {
  56. if ($book->row == $index) {
  57. return ($book);
  58. }
  59. }
  60. return (null);
  61. }
  62. function _get_book_path($index)
  63. {
  64. global $_book_index;
  65. }
  66. function _get_para_path($book, $paragraph)
  67. {
  68. $dns = _FILE_DB_PALITEXT_;
  69. $dbh = new PDO($dns, _DB_USERNAME_, _DB_PASSWORD_, array(PDO::ATTR_PERSISTENT => true));
  70. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  71. $path = "";
  72. $parent = $paragraph;
  73. $deep = 0;
  74. $sFirstParentTitle = "";
  75. //循环查找父标题 得到整条路径
  76. while ($parent > -1) {
  77. $query = "SELECT * from "._TABLE_PALI_TEXT_." where book = ? and paragraph = ?";
  78. $stmt = $dbh->prepare($query);
  79. $stmt->execute(array($book, $parent));
  80. $FetParent = $stmt->fetch(PDO::FETCH_ASSOC);
  81. if($FetParent){
  82. $toc = "<chapter book='{$book}' para='{$parent}' title='{$FetParent["toc"]}'>{$FetParent["toc"]}</chapter>";
  83. if ($path == "") {
  84. if ($FetParent["level"] < 100) {
  85. $path = $toc;
  86. } else {
  87. $path = "<para book='{$book}' para='{$parent}' title='{$FetParent["toc"]}'>{$paragraph}</para>";
  88. }
  89. } else {
  90. $path = $toc . $path;
  91. }
  92. if ($sFirstParentTitle == "") {
  93. $sFirstParentTitle = $FetParent["toc"];
  94. }
  95. $parent = $FetParent["parent"];
  96. }else{
  97. break;
  98. }
  99. $deep++;
  100. if ($deep > 5) {
  101. break;
  102. }
  103. }
  104. $dbh = null;
  105. return ($path);
  106. }
  107. use Firebase\JWT\JWT;
  108. use Firebase\JWT\Key;
  109. class USER
  110. {
  111. public static function current(){
  112. if(isset($_COOKIE['token'])){
  113. $jwt = JWT::decode($_COOKIE['token'],new Key(APP_KEY,'HS512'));
  114. if($jwt->exp < time()){
  115. return [];
  116. }else{
  117. //有效的token
  118. return ['user_uid'=>$jwt->uid,'user_id'=>$jwt->id];
  119. }
  120. }else if(isset($_COOKIE['user_uid'])){
  121. return ['user_uid'=>$_COOKIE['user_uid'],'user_id'=>$_COOKIE['user_id']];
  122. }else{
  123. return [];
  124. }
  125. }
  126. public static function isSignin(){
  127. if(isset($_COOKIE['token'])){
  128. $jwt = JWT::decode($_COOKIE['token'],new Key(APP_KEY,'HS512'));
  129. if($jwt->exp < time()){
  130. return false;
  131. }else{
  132. //有效的token
  133. return true;
  134. }
  135. }else if(isset($_COOKIE['user_uid'])){
  136. return true;
  137. }else{
  138. return false;
  139. }
  140. }
  141. }
  142. class UUID
  143. {
  144. public static function v3($namespace, $name)
  145. {
  146. if (!self::is_valid($namespace)) {
  147. return false;
  148. }
  149. // Get hexadecimal components of namespace
  150. $nhex = str_replace(array('-', '{', '}'), '', $namespace);
  151. // Binary Value
  152. $nstr = '';
  153. // Convert Namespace UUID to bits
  154. for ($i = 0; $i < strlen($nhex); $i += 2) {
  155. $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1]));
  156. }
  157. // Calculate hash value
  158. $hash = md5($nstr . $name);
  159. return sprintf('%08s-%04s-%04x-%04x-%12s',
  160. // 32 bits for "time_low"
  161. substr($hash, 0, 8),
  162. // 16 bits for "time_mid"
  163. substr($hash, 8, 4),
  164. // 16 bits for "time_hi_and_version",
  165. // four most significant bits holds version number 3
  166. (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000,
  167. // 16 bits, 8 bits for "clk_seq_hi_res",
  168. // 8 bits for "clk_seq_low",
  169. // two most significant bits holds zero and one for variant DCE1.1
  170. (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
  171. // 48 bits for "node"
  172. substr($hash, 20, 12)
  173. );
  174. }
  175. public static function v4()
  176. {
  177. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  178. // 32 bits for "time_low"
  179. mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  180. // 16 bits for "time_mid"
  181. mt_rand(0, 0xffff),
  182. // 16 bits for "time_hi_and_version",
  183. // four most significant bits holds version number 4
  184. mt_rand(0, 0x0fff) | 0x4000,
  185. // 16 bits, 8 bits for "clk_seq_hi_res",
  186. // 8 bits for "clk_seq_low",
  187. // two most significant bits holds zero and one for variant DCE1.1
  188. mt_rand(0, 0x3fff) | 0x8000,
  189. // 48 bits for "node"
  190. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  191. );
  192. }
  193. public static function v5($namespace, $name)
  194. {
  195. if (!self::is_valid($namespace)) {
  196. return false;
  197. }
  198. // Get hexadecimal components of namespace
  199. $nhex = str_replace(array('-', '{', '}'), '', $namespace);
  200. // Binary Value
  201. $nstr = '';
  202. // Convert Namespace UUID to bits
  203. for ($i = 0; $i < strlen($nhex); $i += 2) {
  204. $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1]));
  205. }
  206. // Calculate hash value
  207. $hash = sha1($nstr . $name);
  208. return sprintf('%08s-%04s-%04x-%04x-%12s',
  209. // 32 bits for "time_low"
  210. substr($hash, 0, 8),
  211. // 16 bits for "time_mid"
  212. substr($hash, 8, 4),
  213. // 16 bits for "time_hi_and_version",
  214. // four most significant bits holds version number 5
  215. (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,
  216. // 16 bits, 8 bits for "clk_seq_hi_res",
  217. // 8 bits for "clk_seq_low",
  218. // two most significant bits holds zero and one for variant DCE1.1
  219. (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
  220. // 48 bits for "node"
  221. substr($hash, 20, 12)
  222. );
  223. }
  224. public static function is_valid($uuid)
  225. {
  226. return preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' .
  227. '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', $uuid) === 1;
  228. }
  229. }
  230. function pali2english($subject)
  231. {
  232. $subject = mb_strtolower($subject, 'UTF-8');
  233. $search = array('ā', 'ī', 'ū', 'ṅ', 'ñ', 'ṇ', 'ṭ', 'ḍ', 'ḷ', 'ṃ');
  234. $replace = array('a', 'i', 'u', 'n', 'n', 'n', 't', 'd', 'l', 'm');
  235. $title_en = str_replace($search, $replace, $subject);
  236. return ($title_en);
  237. }
  238. function mTime()
  239. {
  240. return (sprintf("%d", microtime(true) * 1000));
  241. }
  242. function getLanguageCode($inString)
  243. {
  244. return ("zh-cn");
  245. }