lib_remoteStorage.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. class OC_remoteStorage {
  3. public static function getValidTokens($ownCloudUser, $category) {
  4. $query=OCP\DB::prepare("SELECT token,appUrl,category FROM *PREFIX*authtoken WHERE user=? LIMIT 100");
  5. $result=$query->execute(array($ownCloudUser));
  6. $ret = array();
  7. while($row=$result->fetchRow()){
  8. if(in_array($category, explode(',', $row['category']))) {
  9. $ret[$row['token']]=true;
  10. }
  11. }
  12. return $ret;
  13. }
  14. public static function getTokenFor($appUrl, $categories) {
  15. $user=OCP\USER::getUser();
  16. $query=OCP\DB::prepare("SELECT token FROM *PREFIX*authtoken WHERE user=? AND appUrl=? AND category=? LIMIT 1");
  17. $result=$query->execute(array($user, $appUrl, $categories));
  18. if($row=$result->fetchRow()) {
  19. return base64_encode('remoteStorage:'.$row['token']);
  20. } else {
  21. return false;
  22. }
  23. }
  24. public static function getAllTokens() {
  25. $user=OCP\USER::getUser();
  26. $query=OCP\DB::prepare("SELECT token,appUrl,category FROM *PREFIX*authtoken WHERE user=? LIMIT 100");
  27. $result=$query->execute(array($user));
  28. $ret = array();
  29. while($row=$result->fetchRow()){
  30. $ret[$row['token']] = array(
  31. 'appUrl' => $row['appUrl'],
  32. 'categories' => $row['category'],
  33. );
  34. }
  35. return $ret;
  36. }
  37. public static function deleteToken($token) {
  38. $user=OCP\USER::getUser();
  39. $query=OCP\DB::prepare("DELETE FROM *PREFIX*authtoken WHERE token=? AND user=?");
  40. $query->execute(array($token,$user));
  41. return 'unknown';//how can we see if any rows were affected?
  42. }
  43. private static function addToken($token, $appUrl, $categories){
  44. $user=OCP\USER::getUser();
  45. $query=OCP\DB::prepare("INSERT INTO *PREFIX*authtoken (`token`,`appUrl`,`user`,`category`) VALUES(?,?,?,?)");
  46. $query->execute(array($token,$appUrl,$user,$categories));
  47. }
  48. public static function createCategories($appUrl, $categories) {
  49. $token=uniqid();
  50. OC_Util::setupFS(OCP\USER::getUser());
  51. self::addToken($token, $appUrl, $categories);
  52. foreach(explode(',', $categories) as $category) {
  53. //TODO: input checking on $category
  54. $scopePathParts = array('remoteStorage', $category);
  55. for($i=0;$i<=count($scopePathParts);$i++){
  56. $thisPath = '/'.implode('/', array_slice($scopePathParts, 0, $i));
  57. if(!OC_Filesystem::file_exists($thisPath)) {
  58. OC_Filesystem::mkdir($thisPath);
  59. }
  60. }
  61. }
  62. return base64_encode('remoteStorage:'.$token);
  63. }
  64. }