user_api_key_manager.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /**
  20. *
  21. */
  22. public static function end_of_time()
  23. {
  24. $time = 2147483647; //mysql int max value
  25. }
  26. public static function get_by_id($id)
  27. {
  28. $table = Database::get_main_table(TABLE_MAIN_USER_API_KEY);
  29. $sql = "SELECT * FROM $table WHERE id=$id";
  30. $res = Database::query($sql);
  31. if (Database::num_rows($res) < 1) {
  32. return false;
  33. }
  34. $result = Database::fetch_array($res, 'ASSOC');
  35. return $result;
  36. }
  37. /**
  38. *
  39. * @param int $duration in seconds
  40. * @param int $user_id
  41. * @param string $api_service
  42. * @param string $api_end_point
  43. * @return AccessToken
  44. */
  45. public static function create_temp_token($api_service = null, $duration = 60, $user_id = null, $api_end_point = null)
  46. {
  47. $time = time();
  48. $validity_start_date = $time;
  49. $validity_end_date = $time + $duration;
  50. return self::create_token($user_id, $api_key = null, $api_service, $api_end_point, $validity_start_date, $validity_end_date);
  51. }
  52. /**
  53. *
  54. * @param int $user_id
  55. * @param string $api_key
  56. * @param string $api_service
  57. * @param string $api_end_point
  58. * @param int $validity_start_date
  59. * @param int $validity_end_date
  60. * @param string $description
  61. * @return AccessToken
  62. */
  63. 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 = '')
  64. {
  65. $time = time();
  66. $user_id = $user_id ? $user_id : Chamilo::user()->user_id();
  67. $api_key = $api_key ? $api_key : uniqid('', true);
  68. $api_service = $api_service ? $api_service : self::default_service();
  69. $api_end_point = $api_end_point ? $api_end_point : '';
  70. $validity_start_date = $validity_start_date ? $validity_start_date : $time;
  71. $validity_end_date = $validity_end_date ? $validity_end_date : self::end_of_time();
  72. $created_date = $time;
  73. $user_id = (int) $user_id;
  74. $api_key = Database::escape_string($api_key);
  75. $api_service = Database::escape_string($api_service);
  76. $api_end_point = Database::escape_string($api_end_point);
  77. $validity_start_date = date('Y-m-d H:i:s', $validity_start_date);
  78. $validity_end_date = date('Y-m-d H:i:s', $validity_end_date);
  79. $created_date = date('Y-m-d H:i:s', $created_date);
  80. $values = array();
  81. $values['user_id'] = $user_id;
  82. $values['api_key'] = $api_key;
  83. $values['api_service'] = $api_service;
  84. $values['api_end_point'] = $api_end_point;
  85. $values['validity_start_date'] = $validity_start_date;
  86. $values['validity_end_date'] = $validity_end_date;
  87. $values['created_date'] = $created_date;
  88. $table = Database::get_main_table(TABLE_MAIN_USER_API_KEY);
  89. $id = Database::insert($table, $values);
  90. return AccessToken::create($id, $user_id, $api_key);
  91. }
  92. }