lib_remoteStorage.php 2.3 KB

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