galleryOp.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * ownCloud - gallery application
  4. *
  5. * @author Bartek Przybylski
  6. * @copyright 2012 Bartek Przybylski bart.p.pl@gmail.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. header('Content-type: text/html; charset=UTF-8') ;
  23. OCP\JSON::checkLoggedIn();
  24. OCP\JSON::checkAppEnabled('gallery');
  25. function handleRename($oldname, $newname) {
  26. OC_Gallery_Album::rename($oldname, $newname, OCP\USER::getUser());
  27. OC_Gallery_Album::changeThumbnailPath($oldname, $newname);
  28. }
  29. function handleRemove($name) {
  30. $album_id = OC_Gallery_Album::find(OCP\USER::getUser(), $name);
  31. $album_id = $album_id->fetchRow();
  32. $album_id = $album_id['album_id'];
  33. OC_Gallery_Album::remove(OCP\USER::getUser(), $name);
  34. OC_Gallery_Photo::removeByAlbumId($album_id);
  35. }
  36. function handleGetThumbnails($albumname) {
  37. OCP\Response::enableCaching(3600 * 24); // 24 hour
  38. $view = OCP\Files::getStorage('gallery');
  39. $thumbnail = $view->fopen(urldecode($albumname).'.png', 'r');
  40. header('Content-Type: '.OC_Image::getMimeTypeForFile($thumbnail));
  41. OCP\Response::sendFile($thumbnail);
  42. }
  43. function handleGalleryScanning() {
  44. OCP\DB::beginTransaction();
  45. set_time_limit(0);
  46. OC_Gallery_Album::cleanup();
  47. $eventSource = new OC_EventSource();
  48. OC_Gallery_Scanner::scan($eventSource);
  49. $eventSource->close();
  50. OCP\DB::commit();
  51. }
  52. function handleFilescan($cleanup) {
  53. if ($cleanup) OC_Gallery_Album::cleanup();
  54. $pathlist = OC_Gallery_Scanner::find_paths();
  55. sort($pathlist);
  56. OCP\JSON::success(array('paths' => $pathlist));
  57. }
  58. function handleStoreSettings($root, $order) {
  59. if (!OC_Filesystem::file_exists($root)) {
  60. OCP\JSON::error(array('cause' => 'No such file or directory'));
  61. return;
  62. }
  63. if (!OC_Filesystem::is_dir($root)) {
  64. OCP\JSON::error(array('cause' => $root . ' is not a directory'));
  65. return;
  66. }
  67. $current_root = OCP\Config::getUserValue(OCP\USER::getUser(),'gallery', 'root', '/');
  68. $root = trim($root);
  69. $root = rtrim($root, '/').'/';
  70. $rescan = $current_root==$root?'no':'yes';
  71. OCP\Config::setUserValue(OCP\USER::getUser(), 'gallery', 'root', $root);
  72. OCP\Config::setUserValue(OCP\USER::getUser(), 'gallery', 'order', $order);
  73. OCP\JSON::success(array('rescan' => $rescan));
  74. }
  75. function handleGetGallery($path) {
  76. $a = array();
  77. $root = OCP\Config::getUserValue(OCP\USER::getUser(),'gallery', 'root', '/');
  78. $path = utf8_decode(rtrim($root.$path,'/'));
  79. if($path == '') $path = '/';
  80. $pathLen = strlen($path);
  81. $result = OC_Gallery_Album::find(OCP\USER::getUser(), null, $path);
  82. $album_details = $result->fetchRow();
  83. $result = OC_Gallery_Album::find(OCP\USER::getUser(), null, null, $path);
  84. while ($r = $result->fetchRow()) {
  85. $album_name = $r['album_name'];
  86. $size=OC_Gallery_Album::getAlbumSize($r['album_id']);
  87. // this is a fallback mechanism and seems expensive
  88. if ($size == 0) $size = OC_Gallery_Album::getIntermediateGallerySize($r['album_path']);
  89. $a[] = array('name' => utf8_encode($album_name), 'numOfItems' => min($size, 10),'path'=>substr($r['album_path'], $pathLen));
  90. }
  91. $result = OC_Gallery_Photo::find($album_details['album_id']);
  92. $p = array();
  93. while ($r = $result->fetchRow()) {
  94. $p[] = utf8_encode($r['file_path']);
  95. }
  96. $r = OC_Gallery_Sharing::getEntryByAlbumId($album_details['album_id']);
  97. $shared = false;
  98. $recursive = false;
  99. $token = '';
  100. if ($row = $r->fetchRow()) {
  101. $shared = true;
  102. $recursive = ($row['recursive'] == 1)? true : false;
  103. $token = $row['token'];
  104. }
  105. OCP\JSON::success(array('albums'=>$a, 'photos'=>$p, 'shared' => $shared, 'recursive' => $recursive, 'token' => $token));
  106. }
  107. function handleShare($path, $share, $recursive) {
  108. $recursive = $recursive == 'true' ? 1 : 0;
  109. $owner = OCP\USER::getUser();
  110. $root = OCP\Config::getUserValue(OCP\USER::getUser(),'gallery', 'root', '/');
  111. $path = utf8_decode(rtrim($root.$path,'/'));
  112. if($path == '') $path = '/';
  113. $r = OC_Gallery_Album::find($owner, null, $path);
  114. if ($row = $r->fetchRow()) {
  115. $albumId = $row['album_id'];
  116. } else {
  117. OCP\JSON::error(array('cause' => 'Couldn\'t find requested gallery'));
  118. exit;
  119. }
  120. if ($share == false) {
  121. OC_Gallery_Sharing::remove($albumId);
  122. OCP\JSON::success(array('sharing' => false));
  123. } else { // share, yeah \o/
  124. $r = OC_Gallery_Sharing::getEntryByAlbumId($albumId);
  125. if (($row = $r->fetchRow())) { // update entry
  126. OC_Gallery_Sharing::updateSharingByToken($row['token'], $recursive);
  127. OCP\JSON::success(array('sharing' => true, 'token' => $row['token'], 'recursive' => $recursive == 1 ? true : false ));
  128. } else { // and new sharing entry
  129. $date = new DateTime();
  130. $token = md5($owner . $date->getTimestamp());
  131. OC_Gallery_Sharing::addShared($token, intval($albumId), $recursive);
  132. OCP\JSON::success(array('sharing' => true, 'token' => $token, 'recursive' => $recursive == 1 ? true : false ));
  133. }
  134. }
  135. }
  136. if ($_GET['operation']) {
  137. switch($_GET['operation']) {
  138. case 'rename':
  139. handleRename($_GET['oldname'], $_GET['newname']);
  140. OCP\JSON::success(array('newname' => $_GET['newname']));
  141. break;
  142. case 'remove':
  143. handleRemove($_GET['name']);
  144. OCP\JSON::success();
  145. break;
  146. case 'get_covers':
  147. handleGetThumbnails(urldecode($_GET['albumname']));
  148. break;
  149. case 'scan':
  150. handleGalleryScanning();
  151. break;
  152. case 'store_settings':
  153. handleStoreSettings($_GET['root'], $_GET['order']);
  154. break;
  155. case 'get_gallery':
  156. handleGetGallery($_GET['path']);
  157. break;
  158. case 'share':
  159. handleShare($_GET['path'], $_GET['share'] == 'true' ? true : false, $_GET['recursive']);
  160. break;
  161. default:
  162. OCP\JSON::error(array('cause' => 'Unknown operation'));
  163. }
  164. }
  165. ?>