core.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /*
  3. *
  4. * Application Name: PHP Custom Content Uploader
  5. * Module Name: core.php
  6. *
  7. * Copyright (c) 2020 DBMXPCA Technologies. All rights reserved.
  8. * https://www.dbmxpca.com/
  9. *
  10. */
  11. require_once($_SERVER['DOCUMENT_ROOT'] . "/config.php");
  12. // Returns true if both strings are the same. Performs a case-insensitive comparison unless third parameter is true.
  13. function ARE_STRINGS_EQUAL($str1, $str2, $case_sensitive = false){
  14. switch($case_sensitive){
  15. case true:
  16. if (strcmp($str1, $str2) == 0){
  17. return true;
  18. }
  19. else{
  20. return false;
  21. }
  22. break;
  23. default:
  24. if (strcasecmp($str1, $str2) == 0){
  25. return true;
  26. }
  27. else{
  28. return false;
  29. }
  30. break;
  31. }
  32. return false;
  33. }
  34. function GET_JSON_DATA_FROM_FILE($full_path_to_file){
  35. $filename = $full_path_to_file;
  36. $fp = fopen($filename, 'r');
  37. $data = fread($fp, filesize($filename));
  38. fclose($fp);
  39. $r = json_decode($data, true);
  40. return $r;
  41. }
  42. // Returns the language-specific string with the specified key.
  43. function GET_LANG_STR($str){
  44. $lang = "en";
  45. $filename = "lang_strings.json";
  46. $not_found = strtoupper($str);
  47. if (!isset($filename)){
  48. return $not_found;
  49. }
  50. $contents = file_get_contents($filename);
  51. $json = json_decode($contents, true);
  52. if ($json == null){
  53. return $not_found;
  54. }
  55. if (array_key_exists($lang, $json)){
  56. if (array_key_exists($str, $json[$lang])){
  57. return $json[$lang][$str];
  58. }
  59. else{
  60. return $not_found;
  61. }
  62. }
  63. else{
  64. return $not_found;
  65. }
  66. }
  67. // Die with error string.
  68. function DIE_ERR($str){
  69. if (!DETAILED_ERRORS){
  70. $str = substr($str, 0, -2);
  71. }
  72. if (ENCLOSE_ERRORS_WITH_PRE_TAG){
  73. die("<pre>" . GET_LANG_STR($str) . "</pre>");
  74. }else{
  75. die(GET_LANG_STR($str));
  76. }
  77. }
  78. // Create the user directory within the UCD if it doesn't exist. Returns true on success or false on failure.
  79. function CREATE_USER_CONTENT_DIR($username){
  80. // Attempt to create the directory if it doesn't exist.
  81. $user_content_dir = USER_CONTENT_DIR . "/" . $username;
  82. if (!file_exists($user_content_dir)){
  83. mkdir($user_content_dir, 0777, true);
  84. }
  85. // Check and make sure it now exists.
  86. if (!file_exists($user_content_dir)){
  87. return false;
  88. }
  89. return true;
  90. }
  91. // Returns full path to the user's directory within the UCD.
  92. function GET_USER_CONTENT_DIR_PATH($username){
  93. return USER_CONTENT_DIR . "/" . $username;
  94. }
  95. // Check user authorization.
  96. function CHECK_USER(&$err){
  97. // Is a valid user provided?
  98. if (isset($_REQUEST['u']) && !empty($_REQUEST['u'])){
  99. // Is a valid API key provided?
  100. if (isset($_REQUEST['k']) && !empty($_REQUEST['k'])){
  101. // Save request username and API key.
  102. $r_user = $_REQUEST['u'];
  103. $r_key = $_REQUEST['k'];
  104. // Fetch user database and check the username and API key combination.
  105. $users = GET_JSON_DATA_FROM_FILE(USERS_JSON);
  106. // Does user exist?
  107. if (isset($users[$r_user])){
  108. // Is user access enabled?
  109. if (isset($users[$r_user]['enabled'])){
  110. if ($users[$r_user]['enabled']){
  111. if (ARE_STRINGS_EQUAL($r_key, $users[$r_user]['api_key'], FORCE_CASE_SENSITIVE_API_KEYS)){
  112. // ALL USER CHECKS PASS.
  113. return true;
  114. }
  115. else{
  116. $err = "error_403_4";
  117. return false;
  118. }
  119. }
  120. else{
  121. $err = "error_403_3";
  122. return false;
  123. }
  124. }
  125. else{
  126. $err = "error_403_2";
  127. return false;
  128. }
  129. }
  130. else{
  131. $err = "error_403_1";
  132. return false;
  133. }
  134. }
  135. else{
  136. $err = "error_401_2";
  137. return false;
  138. }
  139. }
  140. else{
  141. $err = "error_401_1";
  142. return false;
  143. }
  144. }
  145. // Check image prelim data.
  146. function CHECK_IMAGE_PRELIM_DATA(&$err){
  147. if (empty($_FILES)){
  148. $err = "error_415_1";
  149. return false;
  150. }
  151. if (filesize($_FILES['image']['tmp_name']) > 0){
  152. if (!in_array($_FILES['image']['type'], $allowed_mime_types)){
  153. $err = null;
  154. return true;
  155. }
  156. else{
  157. $err = "error_415_1";
  158. return false;
  159. }
  160. }
  161. else{
  162. $err = "error_400_2";
  163. return false;
  164. }
  165. }
  166. // Check if any errors on file upload.
  167. function CHECK_IMAGE_ERRORS(&$err){
  168. if ($_FILES['image']['error'] > 0){
  169. $err = "error_500_0_";
  170. return false;
  171. }
  172. $err = null;
  173. return true;
  174. }