Anonymous 3D Imageboard http://cyberia.digital/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
4.7KB

  1. <?php
  2. /*
  3. __ __ __
  4. / / / /__ / /___ ___ __________
  5. / /_/ / _ \/ / __ \/ _ \/ ___/ ___/
  6. / __ / __/ / /_/ / __/ / (__ )
  7. /_/ /_/\___/_/ .___/\___/_/ /____/
  8. /_/
  9. Static function to help
  10. */
  11. require_once "./Utils/Constants.php";
  12. class Helpers
  13. {
  14. /**
  15. * Construct won't be called inside this class and is uncallable from
  16. * the outside. This prevents instantiating this class.
  17. * This is by purpose, because we want a static class.
  18. */
  19. private function __construct() {}
  20. private static $initialized = false;
  21. private static function initialize()
  22. {
  23. if (self::$initialized)
  24. return;
  25. self::$initialized = true;
  26. }
  27. /**
  28. * Serialize the LarZer data in Array.
  29. */
  30. public static function getDataFromLazer($lazerObj)
  31. {
  32. self::initialize();
  33. $lazerObj = (array) $lazerObj;
  34. if(array_key_exists ( '' . "\0" . '*' . "\0" . 'data' , $lazerObj ) && $lazerObj[ '' . "\0" . '*' . "\0" . 'data'] != null){
  35. $lazerObj = (array) $lazerObj[ '' . "\0" . '*' . "\0" . 'data'];
  36. } else if(array_key_exists ( '' . "\0" . '*' . "\0" . 'set' , $lazerObj ) && $lazerObj[ '' . "\0" . '*' . "\0" . 'set'] != null){
  37. $lazerObj = (array) $lazerObj[ '' . "\0" . '*' . "\0" . 'set'];
  38. }
  39. return $lazerObj;
  40. }
  41. /**
  42. * Get the dead line to consider a player as inactive.
  43. * (surival of a video game version of the forum)
  44. */
  45. public static function getDeadline()
  46. {
  47. self::initialize();
  48. $currentTimeStamp = time();
  49. $deadLine = $currentTimeStamp - 3600;
  50. return $deadLine;
  51. }
  52. /**
  53. * Get the dead line to consider a player should be deleted.
  54. * (surival of a video game version of the forum)
  55. */
  56. public static function getDeletionline()
  57. {
  58. self::initialize();
  59. $currentTimeStamp = time();
  60. $deadLine = $currentTimeStamp - 3600*24;
  61. return $deadLine;
  62. }
  63. /**
  64. * make a generic response for Web services
  65. */
  66. public static function makeGenericResponse($isOk, $result, $error)
  67. {
  68. self::initialize();
  69. //prepare the result
  70. $response = array(
  71. 'ok' => $isOk,
  72. 'result' => $result,
  73. 'error' => $error
  74. );
  75. header("Content-type: application/json");
  76. echo json_encode($response);
  77. exit();
  78. }
  79. /**
  80. * Get the user IP
  81. */
  82. public static function getClientIp() {
  83. // IP si internet partagé
  84. if (isset($_SERVER['HTTP_CLIENT_IP'])) {
  85. return $_SERVER['HTTP_CLIENT_IP'];
  86. }
  87. // IP derrière un proxy
  88. elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  89. return $_SERVER['HTTP_X_FORWARDED_FOR'];
  90. }
  91. // Sinon : IP normale
  92. else {
  93. return (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '');
  94. }
  95. }
  96. /**
  97. * CHECK IF BASE64 code is an image
  98. */
  99. public static function check_base64_image($base64) {
  100. $img = imagecreatefromstring(base64_decode($base64));
  101. if (!$img) {
  102. return false;
  103. }
  104. imagepng($img, 'tmp.png');
  105. $info = getimagesize('tmp.png');
  106. unlink('tmp.png');
  107. if ($info[0] > 0 && $info[1] > 0 && $info['mime']) {
  108. return true;
  109. }
  110. return false;
  111. }
  112. /*
  113. * Check Base64 size
  114. */
  115. public static function getBase64ImageSize($base64Image){ //return memory size in B, KB, MB
  116. try{
  117. $size_in_bytes = (int) (strlen(rtrim($base64Image, '=')) * 3 / 4);
  118. $size_in_kb = $size_in_bytes / 1024;
  119. return $size_in_kb;
  120. }
  121. catch(Exception $e){
  122. return $e;
  123. }
  124. }
  125. /**
  126. * Generate random Token
  127. */
  128. public static function generateToken(){
  129. //Generate a random string.
  130. $token = openssl_random_pseudo_bytes(16);
  131. //Convert the binary data into hexadecimal representation.
  132. return bin2hex($token);
  133. }
  134. /**
  135. * Hash password
  136. */
  137. public static function hash_pw($plain_pw)
  138. {
  139. // (optional) change logic here for different hash algorithm
  140. //return password_hash($plain_pw, PASSWORD_DEFAULT);
  141. return md5($plain_pw);
  142. }
  143. /**
  144. * Verify hashed password
  145. */
  146. public static function verify_pw($plain_pw, $hashed_pw)
  147. {
  148. // (optional) change logic here for different hash algorithm
  149. //return password_verify($plain_pw, $hashed_pw);
  150. return (md5($plain_pw)==$hashed_pw);
  151. }
  152. /**
  153. * Get Real File Name
  154. */
  155. public static function getRealFileName($fileName_)
  156. {
  157. return md5($fileName_.'_'.Constants::CONST_IMAGE_SALT);
  158. }
  159. }