PageRenderTime 53ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/admin_scripts_php/wb_functions.php

https://gitlab.com/urbanjunglestudio/whambush-bananas
PHP | 167 lines | 113 code | 32 blank | 22 comment | 15 complexity | b9b34f531c54229a0fa7777a5d105b73 MD5 | raw file
  1. <?php
  2. $api = 'https://api.whambush.com/v2.0/';
  3. function http_post($endpoint,$data,$auth="0")
  4. {
  5. global $api;
  6. $url = $api.$endpoint;
  7. $json = json_encode($data);
  8. if ($auth != "0") {
  9. $header = "Content-type: application/x-www-form-urlencoded\r\nAuthorization: Token ".$auth."\r\n";
  10. } else {
  11. $header = "Content-type: application/x-www-form-urlencoded\r\n";
  12. }
  13. $options = array(
  14. 'http' => array(
  15. 'header' => $header,
  16. 'method' => 'POST',
  17. 'content' => http_build_query($data),
  18. ),
  19. );
  20. $context = stream_context_create($options);
  21. $result = file_get_contents($url, false, $context);
  22. return json_decode($result,true);
  23. }
  24. function http_get($endpoint,$auth="0",$fullurl=FALSE)
  25. {
  26. global $api;
  27. if ($fullurl) {
  28. $url = $endpoint;
  29. } else {
  30. $url = $api.$endpoint;
  31. }
  32. //$json = json_encode($data);
  33. if ($auth != "0") {
  34. $header = "Content-type: application/x-www-form-urlencoded\r\nAuthorization: Token ".$auth."\r\n";
  35. } else {
  36. $header = "Content-type: application/x-www-form-urlencoded\r\n";
  37. }
  38. $options = array(
  39. 'http' => array(
  40. 'header' => $header,
  41. 'method' => 'GET',
  42. ),
  43. );
  44. $context = stream_context_create($options);
  45. $result = file_get_contents($url, false, $context);
  46. $finfo = new finfo(FILEINFO_MIME_TYPE);
  47. //print_r($finfo->buffer($result));
  48. return json_decode($result,true);
  49. }
  50. function login()
  51. {
  52. echo "This script requires authentication".PHP_EOL;
  53. echo "Username: ";
  54. $username = trim(fgets(STDIN));
  55. echo "Password: ";
  56. system('stty -echo');
  57. $password = trim(fgets(STDIN));
  58. system('stty echo');
  59. // add a new line since the users CR didn't echo
  60. echo PHP_EOL;
  61. $data = array('username' => $username,
  62. 'password' => $password,
  63. 'guest_id' => '1',
  64. 'manufacturer'=>'jk',
  65. 'os'=>'php',
  66. 'os_version'=>'1',
  67. 'device'=>'admin',
  68. 'whambush_version'=>'1.3');
  69. $loginarray = http_post("login/",$data,0);
  70. if (strlen($loginarray['token']) > 1) {
  71. echo "////////////////".PHP_EOL;
  72. return $loginarray;
  73. } else {
  74. echo "Login failed!".PHP_EOL;
  75. exit;
  76. }
  77. }
  78. /**
  79. * creating between two date
  80. * @param string since
  81. * @param string until
  82. * @param string step
  83. * @param string date format
  84. * @return array
  85. * @author Ali OYGUR <alioygur@gmail.com>
  86. */
  87. function dateRange($first, $last, $step = '+1 day', $format = 'd/m/Y',$fix=FALSE ) {
  88. date_default_timezone_set("Europe/Helsinki");
  89. $dates = array();
  90. $current = strtotime($first);
  91. $last = strtotime($last);
  92. while( $current <= $last ) {
  93. if ($fix) {
  94. $dates[] = strftime($format, $current);
  95. } else {
  96. $dates[] = date($format, $current);
  97. }
  98. $current = strtotime($step, $current);
  99. }
  100. return $dates;
  101. }
  102. // converts stdObject to array
  103. function objectToArray($d) {
  104. if (is_object($d)) {
  105. // Gets the properties of the given object
  106. // with get_object_vars function
  107. $d = get_object_vars($d);
  108. }
  109. if (is_array($d)) {
  110. /*
  111. * Return array converted to object
  112. * Using __FUNCTION__ (Magic constant)
  113. * for recursive call
  114. */
  115. return array_map(__FUNCTION__, $d);
  116. }
  117. else {
  118. // Return array
  119. return $d;
  120. }
  121. }
  122. // puts array/string to file and creates dirs
  123. function file_force_contents($dir, $contents)
  124. {
  125. $parts = explode('/', $dir);
  126. $file = array_pop($parts);
  127. $dir = '';
  128. foreach($parts as $part) {
  129. if(!is_dir($dir .= "$part/")) {
  130. mkdir($dir);
  131. }
  132. }
  133. file_put_contents("$dir/$file", $contents);
  134. }
  135. ?>