PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/php/func.php

https://github.com/fefantom/WhatsAPI
PHP | 87 lines | 75 code | 12 blank | 0 comment | 10 complexity | 329eeb694889b838295d49f465864d62 MD5 | raw file
  1. <?php
  2. function isShort($str){
  3. $len = strlen($str);
  4. if($len < 256)$res = true;
  5. else $res = false;
  6. return $res;
  7. }
  8. function strlen_wa($str){
  9. $len = strlen($str);
  10. if($len >= 256)$len = $len&0xFF00 >> 8;
  11. return $len;
  12. }
  13. function _hex($int){
  14. return (strlen(sprintf("%X", $int))%2==0) ? sprintf("%X", $int) : sprintf("0%X", $int);
  15. }
  16. function random_uuid(){
  17. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  18. mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
  19. mt_rand( 0, 0xffff ),
  20. mt_rand( 0, 0x0fff ) | 0x4000,
  21. mt_rand( 0, 0x3fff ) | 0x8000,
  22. mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
  23. );
  24. }
  25. function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false)
  26. {
  27. $algorithm = strtolower($algorithm);
  28. if (!in_array($algorithm, hash_algos(), true))
  29. die('PBKDF2 ERROR: Invalid hash algorithm.');
  30. if ($count <= 0 || $key_length <= 0)
  31. die('PBKDF2 ERROR: Invalid parameters.');
  32. $hash_length = strlen(hash($algorithm, "", true));
  33. $block_count = ceil($key_length / $hash_length);
  34. $output = "";
  35. for ($i = 1; $i <= $block_count; $i++) {
  36. $last = $salt . pack("N", $i);
  37. $last = $xorsum = hash_hmac($algorithm, $last, $password, true);
  38. for ($j = 1; $j < $count; $j++) {
  39. $xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
  40. }
  41. $output .= $xorsum;
  42. }
  43. if( $raw_output)
  44. return substr($output, 0, $key_length);
  45. else
  46. return bin2hex(substr($output, 0, $key_length));
  47. }
  48. function strtohex($str){
  49. $hex = '';
  50. for ($i=0; $i < strlen($str); $i++)$hex .= "\x".dechex(ord($str[$i]));
  51. return $hex;
  52. }
  53. function startsWith($haystack, $needle , $pos=0){
  54. $length = strlen($needle);
  55. return (substr($haystack, $pos, $length) === $needle);
  56. }
  57. function endsWith($haystack, $needle){
  58. $length = strlen($needle);
  59. $start = $length * -1;
  60. return (substr($haystack, $start) === $needle);
  61. }
  62. function createIcon($file)
  63. {
  64. $outfile = "thumb.jpg";
  65. $cmd = "convert $file -resize 100x100 $outfile";
  66. system($cmd);
  67. $fp = fopen($outfile, "r");
  68. $contents = fread($fp, filesize($outfile));
  69. fclose($fp);
  70. $b64 = base64_encode($contents);
  71. $outfile .= "b64";
  72. $fp = fopen($outfile, "w");
  73. fwrite($fp, $b64);
  74. fclose($fp);
  75. }