WebService.class.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Base class for Web Services
  5. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  6. * @package chamilo.webservices
  7. */
  8. abstract class WebService
  9. {
  10. protected $apiKey;
  11. /**
  12. * Class constructor
  13. */
  14. public function __construct()
  15. {
  16. $this->apiKey = null;
  17. }
  18. /**
  19. * Set the api key
  20. * @param string $apiKey The api key
  21. */
  22. public function setApiKey($apiKey)
  23. {
  24. $this->apiKey = $apiKey;
  25. }
  26. /**
  27. * @abstract
  28. */
  29. abstract public function getApiKey($username);
  30. /**
  31. * Check whether the username and password are valid
  32. * @param string $username The username
  33. * @param string $password the password
  34. * @return boolean Whether the password belongs to the username return true. Otherwise return false
  35. */
  36. public static function isValidUser($username, $password)
  37. {
  38. if (empty($username) || empty($password)) {
  39. return false;
  40. }
  41. $userTable = Database::get_main_table(TABLE_MAIN_USER);
  42. $whereConditions = array(
  43. "username = '?' " => $username,
  44. "AND password = '?'" => sha1($password)
  45. );
  46. $conditions = array(
  47. 'where' => $whereConditions
  48. );
  49. $table = Database::select('count(1) as qty', $userTable, $conditions);
  50. if ($table != false) {
  51. $row = current($table);
  52. if ($row['qty'] > 0) {
  53. return true;
  54. }
  55. }
  56. return false;
  57. }
  58. }