Advertisement
Guest User

Untitled

a guest
May 5th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. <?php
  2. /**
  3. * AkabeiJSON
  4. *
  5. * Akabei Json API
  6. * This library is licensed under the GPL
  7. *
  8. * Copyright (c) 2011 - Manuel Tortosa <manutortosa[at]chakra-project[dot]org>
  9. **/
  10.  
  11. /**
  12. * This class defines a remote interface for fetching data
  13. * from Akabei using JSON formatted elements.
  14. * @package rpc
  15. * @subpackage classes
  16. **/
  17. class AkabeiJSON {
  18. private $exposed_methods = array('list_files');
  19.  
  20. /**
  21. * Handles post data, and routes the request.
  22. * @param string $post_data The post data to parse and handle.
  23. * @return string The JSON formatted response data.
  24. **/
  25. public function handle($http_data) {
  26. // handle error states
  27. if ( !isset($http_data['type']) || !isset($http_data['arg']) ) {
  28. return $this->json_error('No request type/data specified.');
  29. }
  30.  
  31. // do the routing
  32. if ( in_array($http_data['type'], $this->exposed_methods) ) {
  33.  
  34. // ugh. this works. I hate you php.
  35. $json = call_user_func(array(&$this,$http_data['type']),
  36. $http_data['arg']);
  37.  
  38. // allow rpc callback for XDomainAjax
  39. if ( isset($http_data['callback']) ) {
  40. // it is more correct to send text/javascript
  41. // content-type for jsonp-callback
  42. header('content-type: text/javascript');
  43. return $http_data['callback'] . "({$json})";
  44. }
  45. else {
  46. // set content type header to app/json
  47. header('content-type: application/json');
  48. return $json;
  49. }
  50. }
  51. else {
  52. return $this->json_error('Incorrect request type specified.');
  53. }
  54. }
  55.  
  56. /**
  57. * Returns a JSON formatted error string.
  58. *
  59. * @param $msg The error string to return
  60. * @return mixed A json formatted error response.
  61. **/
  62. private function json_error($msg){
  63. // set content type header to app/json
  64. header('content-type: application/json');
  65. return $this->json_results('error',$msg);
  66. }
  67.  
  68. /**
  69. * Returns a list of files in a given path
  70. * @param $directory The target directory
  71. **/
  72. private function listfiles($directory,$arch){
  73.  
  74. $d = "$directory/$arch";
  75. $results = array();
  76. $handler = opendir($d);
  77.  
  78. while ($file = readdir($handler)){
  79. if ($file != "." && $file != ".."){
  80.  
  81. // Get the file attributes
  82. $cleanname = str_replace("-$arch.cb","",$file);
  83. $onlyname = implode("-",explode("-",$cleanname,-2));
  84. $version = str_replace("$onlyname-","",$cleanname);
  85. $size = @filesize("$d/$file");
  86. $date = @filemtime("$d/$file");
  87.  
  88. $results[] = array(
  89. "filename" => $file,
  90. "name" => $onlyname,
  91. "version" => $version,
  92. "size" => $size,
  93. "date" => date( "d-m-Y H:i:s", $date),
  94. );
  95. }
  96. }
  97. closedir($handler);
  98. return $results;
  99. }
  100.  
  101. /**
  102. * Returns a JSON formatted result data.
  103. * @param $type The response method type.
  104. * @param $data The result data to return
  105. * @return mixed A json formatted result response.
  106. **/
  107. private function json_results($type,$data){
  108. return json_encode( array('type' => $type, 'results' => $data) );
  109. }
  110.  
  111. /**
  112. * Returns the list of available bundles in a given architechture
  113. * @param $arch The target architechture
  114. **/
  115. private function list_files($arch) {
  116. if ($arch !== "i686" && $arch !== "x86_64") {
  117. return $this->json_error('Incorrect architechture.');
  118. }
  119.  
  120. $results = $this->listfiles("../repo/bundles",$arch);
  121.  
  122. if ($results) {
  123. return $this->json_results('list_files', $results);
  124. }
  125. else {
  126. return $this->json_error('No results found');
  127. }
  128. }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement