api.plugin.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /*
  3. @name Api
  4. @author Valentin CARRUESCO <idleman@idleman.fr>
  5. @link Http://blog.idleman.fr
  6. @licence Cc -by-nc-sa
  7. @version 1.0
  8. @type component
  9. @description API JSon pour l'interconnexion avec d'autres services
  10. */
  11. function api_plugin_api(&$_,&$response){
  12. global $conf,$myUser;
  13. if(!isset($_['object'])) throw new Exception('L\'objet doit être précisé');
  14. if($myUser->getId()==0) throw new Exception('L\'utilisateur doit être connecté');
  15. $response = array_merge($response,UserApi::route($_,'user'));
  16. $response = array_merge($response,SystemApi::route($_,'system'));
  17. $response = array_merge($response,GpioApi::route($_,'gpio'));
  18. $response = array_merge($response,IpApi::route($_,'ip'));
  19. }
  20. Plugin::addHook("api", "api_plugin_api");
  21. abstract class Api {
  22. public static function route($_,$uri){
  23. $response = array();
  24. if($_['object']!= $uri) return $response;
  25. if(!method_exists(get_called_class(),$_['method'])) throw new Exception('Méthode :'.$_['method'].' non définie dans l\'objet '.$uri.'('.get_called_class().')');
  26. $class = get_called_class();
  27. return $class::$_['method']();
  28. }
  29. }
  30. class SystemApi extends Api{
  31. public static function get_cpu(){
  32. return Monitoring::cpu();
  33. }
  34. public static function get_heat(){
  35. return Monitoring::heat();
  36. }
  37. public static function get_disks(){
  38. return Monitoring::disks();
  39. }
  40. public static function get_ram(){
  41. return Monitoring::ram();
  42. }
  43. }
  44. class GpioApi extends Api{
  45. public static function get_cpu(){
  46. return Monitoring::gpio();
  47. }
  48. }
  49. class IpApi extends Api{
  50. public static function get_wan(){
  51. return array(Monitoring::externalIp());
  52. }
  53. public static function get_lan(){
  54. return array(Monitoring::internalIp());
  55. }
  56. }
  57. class UserApi extends Api{
  58. public static function attributes(){
  59. global $conf,$myUser;
  60. $response['user'] = $myUser->toArray();
  61. unset($response['user']['password']);
  62. return $response;
  63. }
  64. }
  65. ?>