Play images and video from Synology PhotoStation server

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. require_once('dsm_share.inc.php');
  3. class DsmShareAPI extends WebAPI {
  4. private $operationPemission = "dsm_account";
  5. protected $idPrefix = 'dsmshare_';
  6. function __construct() {
  7. parent::__construct(SZ_WEBAPI_API_DESCRIPTION_PATH);
  8. }
  9. protected function Process()
  10. {
  11. if (!strcasecmp($this->method, "list")) {
  12. $this->ShareList();
  13. } else if (!strcasecmp($this->method, "copy")) {
  14. $this->ShareCopy();
  15. } else if (!strcasecmp($this->method, "copymusic")) {
  16. $this->ShareCopyMusic();
  17. }
  18. }
  19. private function ShareList()
  20. {
  21. $resp = array();
  22. $params = $this->GetParams_List();
  23. if (!$params) {
  24. $this->SetError(WEBAPI_ERR_BAD_REQUEST);
  25. goto End;
  26. }
  27. $data = DsmShare::ListItem($params['user'], $params['id'], $params['type'], $params['offset'], $params['limit']);
  28. $items = array();
  29. foreach ($data['data'] as $k => $v) {
  30. $v['id'] = $this->EncodeItemId($v['id']);
  31. $items[] = $v;
  32. }
  33. $resp['items'] = $items;
  34. $resp['total'] = $data['total'];
  35. $offset = (0 > (int)$params['limit']) ? $resp['total'] : $params['offset'] + $params['limit'];
  36. $resp['offset'] = ($offset > $resp['total']) ? $resp['total'] : $offset;
  37. $this->SetResponse($resp);
  38. End:
  39. return;
  40. }
  41. private function ShareCopy()
  42. {
  43. $params = $this->GetParams_Copy();
  44. if (!$params) {
  45. $this->SetError(WEBAPI_ERR_BAD_REQUEST);
  46. goto End;
  47. }
  48. $res = DsmShare::CopyItem($params['user'], $params['id'], $params['sharepath'], $params['duplicate']);
  49. if (!$res) {
  50. $this->SetError(PHOTOSTATION_DSMSHARE_UPLOAD_ERROR);
  51. goto End;
  52. }
  53. End:
  54. return;
  55. }
  56. private function ShareCopyMusic()
  57. {
  58. $params = $this->GetParams_CopyMusic();
  59. if (!$params) {
  60. $this->SetError(WEBAPI_ERR_BAD_REQUEST);
  61. goto End;
  62. }
  63. if (SlideshowMusic::LIMIT < SlideshowMusic::GetCount() + count($params['id'])) {
  64. $this->SetError(array(PHOTOSTATION_SLIDESHOWMUSIC_EXCEED_LIMIT, SlideshowMusic::LIMIT));
  65. goto End;
  66. }
  67. foreach ($params['id'] as $id) {
  68. if (!SlideshowMusic::Add(basename($id), $id, '', $params['user'])) {
  69. $this->SetError(PHOTOSTATION_DSMSHARE_UPLOAD_ERROR);
  70. goto End;
  71. }
  72. }
  73. End:
  74. return;
  75. }
  76. private function GetParams_List()
  77. {
  78. $id = !$_REQUEST['id'] || 'fm_root' === $_REQUEST['id'] ? '' : $this->DecodeItemId($_REQUEST['id']);
  79. if (false === $id) {
  80. return false;
  81. }
  82. $types = explode(',', preg_replace('/\s/', '', $_REQUEST['type']));
  83. foreach ($types as $type) {
  84. if (!in_array($type, DsmShare::$allowTypes)) {
  85. return false;
  86. }
  87. }
  88. $user = $this->GetUser();
  89. if (!$user) {
  90. return false;
  91. }
  92. // $variable + 0 => convert to integer
  93. $params = array(
  94. 'id' => $id,
  95. 'user' => $user,
  96. 'type' => $types,
  97. 'offset' => !isset($_REQUEST['offset']) || $_REQUEST['offset'] < 0 ? 0 : $_REQUEST['offset'] + 0,
  98. 'limit' => !isset($_REQUEST['limit']) || $_REQUEST['limit'] < 0 ? NULL : $_REQUEST['limit'] + 0,
  99. );
  100. return $params;
  101. }
  102. private function GetUser()
  103. {
  104. $user = false;
  105. if (isset($_SESSION[SYNOPHOTO_ADMIN_USER]['reg_syno_user'])) {
  106. $user = $_SESSION[SYNOPHOTO_ADMIN_USER]['reg_syno_user'];
  107. } else if (isset($_SESSION[SYNOPHOTO_ADMIN_USER]['admin_syno_user'])) {
  108. $user = ("root" === SYNOPHOTO_ADMIN_USER)? "admin" : SYNOPHOTO_ADMIN_NAME;
  109. }
  110. return $user;
  111. }
  112. private function GetParams_Copy()
  113. {
  114. if(!isset($_REQUEST['id']) || !isset($_REQUEST['sharepath'])) {
  115. return false;
  116. }
  117. $idWithPrefix = explode(',', $_REQUEST['id']);
  118. $ids = array();
  119. $sharepath = trim($_REQUEST['sharepath'], "/");
  120. $dbPath = $sharepath ? $sharepath : '/';
  121. foreach ($idWithPrefix as $k) {
  122. $decodeId = $this->DecodeItemId($k);
  123. if (false === $decodeId) {
  124. return false;
  125. }
  126. if (dirname($decodeId) === SYNOPHOTO_SERVICE_REAL_DIR."/".$sharepath) {
  127. return false;
  128. }
  129. $ids[$decodeId] = 1;
  130. }
  131. if (!csSYNOPhotoMisc::CheckPathValid($sharepath) || !csSYNOPhotoMisc::CheckAlbumUploadable($dbPath)) {
  132. return false;
  133. }
  134. $duplicate = $_REQUEST['duplicate'] ? $_REQUEST['duplicate'] : DsmShare::IGNORE;
  135. if (!in_array($duplicate, array(DsmShare::OVERWRITE, DsmShare::IGNORE))) {
  136. return false;
  137. }
  138. $user = $this->GetUser();
  139. if (!$user) {
  140. return false;
  141. }
  142. // $variable + 0 => convert to integer
  143. $params = array(
  144. 'id' => array_keys($ids),
  145. 'user' => $user,
  146. 'sharepath' => $sharepath,
  147. 'duplicate' => $duplicate
  148. );
  149. return $params;
  150. }
  151. private function GetParams_CopyMusic()
  152. {
  153. if(!isset($_REQUEST['id'])) {
  154. return false;
  155. }
  156. $idWithPrefix = explode(',', $_REQUEST['id']);
  157. $ids = array();
  158. foreach ($idWithPrefix as $k) {
  159. $decodeId = $this->DecodeItemId($k);
  160. if (false === $decodeId) {
  161. return false;
  162. }
  163. $ids[$decodeId] = 1;
  164. }
  165. $user = $this->GetUser();
  166. if (!$user) {
  167. return false;
  168. }
  169. $params = array(
  170. 'id' => array_keys($ids),
  171. 'user' => $user
  172. );
  173. return $params;
  174. }
  175. protected function CheckPermission()
  176. {
  177. $res = true;
  178. $check = array(
  179. "list" => $this->operationPemission,
  180. "copy" => $this->operationPemission,
  181. "copymusic" => $this->operationPemission
  182. );
  183. if (!array_key_exists($this->method, $check)) {
  184. goto End;
  185. }
  186. $funcName = "check_".$check[$this->method];
  187. if (!method_exists($this, $funcName)) {
  188. $res = false;
  189. goto End;
  190. }
  191. $res = $this->$funcName();
  192. End:
  193. if (!$res) {
  194. $this->SetError(PHOTOSTATION_DSMSHARE_ACCESS_DENY);
  195. }
  196. return $res;
  197. }
  198. private function check_dsm_account()
  199. {
  200. csSYNOPhotoDB::GetDBInstance()->SetSessionCache();
  201. csSYNOPhotoMisc::CheckSessionTimeOut(true);
  202. return "1" === csSYNOPhotoMisc::GetConfigDB("global", "account_system", "photo_config") || isset($_SESSION[SYNOPHOTO_ADMIN_USER]['admin_syno_user']);
  203. }
  204. }
  205. $api = new DsmShareAPI();
  206. $api->Run();
  207. ?>