core.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. // @BRIEF Generates a secure, random string.
  35. // @RETURNS Returns the generated string.
  36. // @CREDITS https://stackoverflow.com/questions/4356289/php-random-string-generator/31107425#31107425
  37. function GET_RANDOM_STRING($length = RANDOM_STRING_DEFAULT_LENGTH, $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'){
  38. $pieces = [];
  39. $max = mb_strlen($keyspace, '8bit') - 1;
  40. for ($i = 0; $i < $length; ++$i) {
  41. $pieces []= $keyspace[random_int(0, $max)];
  42. }
  43. return implode('', $pieces);
  44. }
  45. // @BRIEF Generates a random filename.
  46. // @CREDITS php-image-serve.
  47. function GET_RANDOM_FILENAME($username, $type){
  48. $an = '0123456789';
  49. $str = '';
  50. for ($i = 0; $i < 5; $i++){
  51. $str .= substr($an, rand(0, strlen($an) - 1), 1);
  52. }
  53. if (!file_exists(GET_USER_CONTENT_DIR_PATH($username) . $str . "." . $type)){
  54. return $str;
  55. }
  56. else{
  57. return GET_RANDOM_FILENAME($username, $type);
  58. }
  59. }
  60. function SAVE_IMAGE($username, $mime_type, $tmp_name){
  61. $save_dir = GET_USER_CONTENT_DIR_PATH($username);
  62. $mime_type_arr = explode('/', $mime_type);
  63. $type = $mime_type_arr[1];
  64. $name = GET_RANDOM_FILENAME($username, $type);
  65. $final_filename = $name . "." . $type;
  66. if (move_uploaded_file($tmp_name, $save_dir . "/" . $final_filename)){
  67. $img_url = HTTP_PROTOCOL . "://" . FQDN . PUBLIC_USER_CONTENT_DIR . "/" . $username . "/" . $final_filename;
  68. echo $img_url;
  69. }
  70. }
  71. function GET_JSON_DATA_FROM_FILE($full_path_to_file){
  72. $filename = $full_path_to_file;
  73. $fp = fopen($filename, 'r');
  74. $data = fread($fp, filesize($filename));
  75. fclose($fp);
  76. $r = json_decode($data, true);
  77. return $r;
  78. }
  79. // Returns the language-specific string with the specified key.
  80. function GET_LANG_STR($str){
  81. $lang = "en";
  82. $filename = "lang_strings.json";
  83. $not_found = strtoupper($str);
  84. if (!isset($filename)){
  85. return $not_found;
  86. }
  87. $contents = file_get_contents($filename);
  88. $json = json_decode($contents, true);
  89. if ($json == null){
  90. return $not_found;
  91. }
  92. if (array_key_exists($lang, $json)){
  93. if (array_key_exists($str, $json[$lang])){
  94. return $json[$lang][$str];
  95. }
  96. else{
  97. return $not_found;
  98. }
  99. }
  100. else{
  101. return $not_found;
  102. }
  103. }
  104. // Die with error string.
  105. function DIE_ERR($str){
  106. if (!DETAILED_ERRORS){
  107. $str = substr($str, 0, -2);
  108. }
  109. if (ENCLOSE_ERRORS_WITH_PRE_TAG){
  110. die("<pre>" . GET_LANG_STR($str) . "</pre>");
  111. }else{
  112. die(GET_LANG_STR($str));
  113. }
  114. }
  115. // Create the user directory within the UCD if it doesn't exist. Returns true on success or false on failure.
  116. function CREATE_USER_CONTENT_DIR($username){
  117. // Attempt to create the directory if it doesn't exist.
  118. $user_content_dir = USER_CONTENT_DIR . "/" . $username;
  119. if (!file_exists($user_content_dir)){
  120. mkdir($user_content_dir, 0777, true);
  121. }
  122. // Check and make sure it now exists.
  123. if (!file_exists($user_content_dir)){
  124. return false;
  125. }
  126. return true;
  127. }
  128. // Returns full path to the user's directory within the UCD.
  129. function GET_USER_CONTENT_DIR_PATH($username){
  130. return USER_CONTENT_DIR . "/" . $username;
  131. }
  132. // Check user authorization.
  133. function CHECK_USER(&$err){
  134. // Is a valid user provided?
  135. if (isset($_REQUEST['u']) && !empty($_REQUEST['u'])){
  136. // Is a valid API key provided?
  137. if (isset($_REQUEST['k']) && !empty($_REQUEST['k'])){
  138. // Save request username and API key.
  139. $r_user = $_REQUEST['u'];
  140. $r_key = $_REQUEST['k'];
  141. // Fetch user database and check the username and API key combination.
  142. $users = GET_JSON_DATA_FROM_FILE(USERS_JSON);
  143. // Does user exist?
  144. if (isset($users[$r_user])){
  145. // Is user access enabled?
  146. if (isset($users[$r_user]['enabled'])){
  147. if ($users[$r_user]['enabled']){
  148. if (ARE_STRINGS_EQUAL($r_key, $users[$r_user]['api_key'], FORCE_CASE_SENSITIVE_API_KEYS)){
  149. // ALL USER CHECKS PASS.
  150. return true;
  151. }
  152. else{
  153. $err = "error_403_4";
  154. return false;
  155. }
  156. }
  157. else{
  158. $err = "error_403_3";
  159. return false;
  160. }
  161. }
  162. else{
  163. $err = "error_403_2";
  164. return false;
  165. }
  166. }
  167. else{
  168. $err = "error_403_1";
  169. return false;
  170. }
  171. }
  172. else{
  173. $err = "error_401_2";
  174. return false;
  175. }
  176. }
  177. else{
  178. $err = "error_401_1";
  179. return false;
  180. }
  181. }
  182. // Check image prelim data.
  183. function CHECK_IMAGE_PRELIM_DATA(&$err){
  184. global $allowed_mime_types;
  185. if (empty($_FILES)){
  186. $err = "error_415_1";
  187. return false;
  188. }
  189. if (filesize($_FILES['image']['tmp_name']) > 0){
  190. if (in_array($_FILES['image']['type'], $allowed_mime_types)){
  191. $err = null;
  192. return true;
  193. }
  194. else{
  195. $err = "error_415_1";
  196. return false;
  197. }
  198. }
  199. else{
  200. $err = "error_400_2";
  201. return false;
  202. }
  203. }
  204. // Check if any errors on file upload.
  205. function CHECK_IMAGE_ERRORS(&$err){
  206. if ($_FILES['image']['error'] > 0){
  207. $err = "error_500_0_";
  208. return false;
  209. }
  210. $err = null;
  211. return true;
  212. }