user_api_key_manager.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Manage user api keys.
  4. *
  5. * @license see /license.txt
  6. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  7. */
  8. class UserApiKeyManager
  9. {
  10. /**
  11. * The name of the default service.
  12. *
  13. * @return string
  14. */
  15. public static function default_service()
  16. {
  17. return 'chamilo';
  18. }
  19. public static function end_of_time()
  20. {
  21. $time = 2147483647; //mysql int max value
  22. }
  23. public static function get_by_id($id)
  24. {
  25. $table = Database::get_main_table(TABLE_MAIN_USER_API_KEY);
  26. $sql = "SELECT * FROM $table WHERE id=$id";
  27. $res = Database::query($sql);
  28. if (Database::num_rows($res) < 1) {
  29. return false;
  30. }
  31. $result = Database::fetch_array($res, 'ASSOC');
  32. return $result;
  33. }
  34. /**
  35. * @param int $duration in seconds
  36. * @param int $user_id
  37. * @param string $api_service
  38. * @param string $api_end_point
  39. *
  40. * @return AccessToken
  41. */
  42. public static function create_temp_token($api_service = null, $duration = 60, $user_id = null, $api_end_point = null)
  43. {
  44. $time = time();
  45. $validity_start_date = $time;
  46. $validity_end_date = $time + $duration;
  47. return self::create_token($user_id, $api_key = null, $api_service, $api_end_point, $validity_start_date, $validity_end_date);
  48. }
  49. /**
  50. * @param int $user_id
  51. * @param string $api_key
  52. * @param string $api_service
  53. * @param string $api_end_point
  54. * @param int $validity_start_date
  55. * @param int $validity_end_date
  56. * @param string $description
  57. *
  58. * @return AccessToken
  59. */
  60. public static function create_token($user_id = null, $api_key = null, $api_service = null, $api_end_point = null, $validity_start_date = null, $validity_end_date = null, $description = '')
  61. {
  62. $time = time();
  63. $user_id = $user_id ? $user_id : Chamilo::user()->user_id();
  64. $api_key = $api_key ? $api_key : uniqid('', true);
  65. $api_service = $api_service ? $api_service : self::default_service();
  66. $api_end_point = $api_end_point ? $api_end_point : '';
  67. $validity_start_date = $validity_start_date ? $validity_start_date : $time;
  68. $validity_end_date = $validity_end_date ? $validity_end_date : self::end_of_time();
  69. $created_date = $time;
  70. $user_id = (int) $user_id;
  71. $api_key = Database::escape_string($api_key);
  72. $api_service = Database::escape_string($api_service);
  73. $api_end_point = Database::escape_string($api_end_point);
  74. $validity_start_date = date('Y-m-d H:i:s', $validity_start_date);
  75. $validity_end_date = date('Y-m-d H:i:s', $validity_end_date);
  76. $created_date = date('Y-m-d H:i:s', $created_date);
  77. $values = [];
  78. $values['user_id'] = $user_id;
  79. $values['api_key'] = $api_key;
  80. $values['api_service'] = $api_service;
  81. $values['api_end_point'] = $api_end_point;
  82. $values['validity_start_date'] = $validity_start_date;
  83. $values['validity_end_date'] = $validity_end_date;
  84. $values['created_date'] = $created_date;
  85. $table = Database::get_main_table(TABLE_MAIN_USER_API_KEY);
  86. $id = Database::insert($table, $values);
  87. return AccessToken::create($id, $user_id, $api_key);
  88. }
  89. }