Play images and video from Synology PhotoStation server

albumutil.php 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. <?php
  2. require_once('album.inc.php');
  3. class AlbumAPIUtil {
  4. /**
  5. * @param $parentAlbumName
  6. * @param $data
  7. * @return
  8. * id - success
  9. * -1 - general fail
  10. * -2 - no upload right
  11. * -3 - not admin
  12. * -4 - album hsa exist
  13. */
  14. static function SYNOPHOTO_ADMIN_AddAlbum($parentAlbumName = '', $params)
  15. {
  16. if (null !== $params['sort_by'] || null !== $params['sort_direction'] || null !== $params['conversion'] ||
  17. null !== $params['allow_comment'] || null !== $params['type'] || null !== $params['password']) {
  18. if (!isset($_SESSION[SYNOPHOTO_ADMIN_USER]['admin_syno_user'])) {
  19. return -3;
  20. }
  21. } else {
  22. if (!csSynoPhotoMisc::CheckAlbumUploadable(('' === $parentAlbumName) ? '/' : $parentAlbumName)) {
  23. return -2;
  24. }
  25. }
  26. $album_data = array();
  27. // only 1st level share can set 'allow_comment' and 'allow conversion'
  28. if ('' === $parentAlbumName) {
  29. if (null === $params['allow_comment']) {
  30. $album_data['comment'] = ('on' === csSYNOPhotoMisc::GetConfigDB("photo", "album_def_allow_comment", "photo_config")) ? 't' : 'f';
  31. } else {
  32. $album_data['comment'] = ('true' === $params['allow_comment']) ? 't' : 'f';
  33. }
  34. if (null === $params['conversion']) {
  35. $album_data['conversion'] = ('on' === csSYNOPhotoMisc::GetConfigDB("photo", "def_album_disable_conversion", "photo_config")) ? 'f' : 't';
  36. } else {
  37. $album_data['conversion'] = ('true' === $params['conversion']) ? 't' : 'f';
  38. }
  39. } else {
  40. $album_data['comment'] = 'f';
  41. $album_data['conversion'] = 't';
  42. }
  43. $album_name = stripcslashes($params['name']);
  44. $album_data['sharename'] = ($parentAlbumName == '') ? $album_name : $parentAlbumName.'/'.$album_name;
  45. $album_data['title'] = stripcslashes($params['title']);
  46. $album_data['description'] = stripcslashes($params['description']);
  47. $default_album_public = 'f';
  48. if (isSet($_SESSION[SYNOPHOTO_ADMIN_USER]['system_config']['pkgCfg']['albumdefpublic']) &&
  49. 'yes' == $_SESSION[SYNOPHOTO_ADMIN_USER]['system_config']['pkgCfg']['albumdefpublic']) {
  50. $default_album_public = 't';
  51. }
  52. $album_data['public'] = null === $params['type'] ? $default_album_public : (('public' === $params['type']) ? 't' : 'f');
  53. $album_data['is_subdir'] = ($parentAlbumName == '') ? 'f' : 't';
  54. $album_data['password'] = (null !== $params['password']) ? md5($params['password']) : '';
  55. $inherit = ('' === $album_data['password']) ? $params['inheritParent'] : false;
  56. $path = SYNOPHOTO_SERVICE_REAL_DIR.'/'.$album_data['sharename'];
  57. //3rd param is true while create album but not assign permission so that inheriting permission of parent album
  58. //4th param is true while the album creator is not admin so that it need to inherit the 'public' column of parent album,
  59. // althought 3rd param is false, AddAlbumToDB still call inherit because of creator is not admin.
  60. $createID = SYNOPHOTO_ADMIN_AddAlbumToDB($album_data, $path, $inherit, !$inherit);
  61. if ('album existed' === $createID) {
  62. return -4;
  63. }
  64. //sort
  65. $data = array();
  66. $data['type'] = (null !== $params['sort_by']) ? self::ConvertToSortCode($params['sort_by']) : -1;
  67. $data['order'] = (null !== $params['sort_by']) ? self::ConvertToSortDirectionCode($params['sort_direction']) : 0;
  68. csSYNOPhotoAlbum::GetAlbumInstance()->SaveAlbumThumbSortType($album_data['sharename'], $data);
  69. csSYNOPhotoDB::GetDBInstance()->SetSessionCache(true);
  70. return $createID;
  71. }
  72. /**
  73. * @param $shareName
  74. * @param $params
  75. * @return
  76. * 0 - success
  77. * -1 - general fail
  78. * -2 - no manage right
  79. * -3 - not admin
  80. */
  81. static function SYNOPHOTO_ADMIN_UpdateAlbum($shareName, $params)
  82. {
  83. // description, title - manage right
  84. if ((null !== $params['description'] || null !== $params['title']) && null === $params['sort_by'] && null === $params['sort_direction'] &&
  85. null === $params['allow_comment'] && null === $params['type'] && null === $params['password'] && null === $params['conversion']) {
  86. if (!csSynoPhotoMisc::CheckAlbumManageable(('' === $shareName) ? '/' : $shareName)) {
  87. return -2;
  88. }
  89. // not changed
  90. } elseif (null === $params['description'] && null === $params['title'] && null === $params['sort_by'] && null === $params['sort_direction'] &&
  91. null === $params['allow_comment'] && null === $params['type'] && null === $params['password'] && null === $params['conversion']) {
  92. return 0;
  93. } else {
  94. // title, sort_by, sort_direction, allow_comment, type, password, conversion - admin
  95. if (!isset($_SESSION[SYNOPHOTO_ADMIN_USER]['admin_syno_user'])) {
  96. return -3;
  97. }
  98. }
  99. if ('' === $shareName) {
  100. $shareName = '/';
  101. }
  102. $setCondition = array();
  103. $setQueryParam = array();
  104. if (null !== $params['title']) {
  105. $setValue = "title = ? ";
  106. array_push($setCondition, $setValue);
  107. $setQueryParam[] = stripcslashes($params['title']);
  108. }
  109. if (null !== $params['description']) {
  110. $setValue = "description = ?";
  111. array_push($setCondition, $setValue);
  112. $setQueryParam[] = stripcslashes($params['description']);
  113. }
  114. $objs = Album::GetBySharename(array($shareName), true);
  115. $share = $objs[$shareName];
  116. if ($share) {
  117. $shareid = $share['shareid'];
  118. } else {
  119. return -1;
  120. }
  121. if ('public' === $params['type']) {
  122. $setValue = "public = 't', password = ''";
  123. array_push($setCondition, $setValue);
  124. /* clear access right table if album is public */
  125. PHOTO_DB_Query("DELETE FROM " . PHOTO_ACCESS_RIGHT_TABLE . " WHERE shareid = ".$shareid);
  126. } else if ('private' === $params['type']) {
  127. $setValue = "public = 'f', password = ''";
  128. array_push($setCondition, $setValue);
  129. /* remove all permissions if album is password */
  130. PHOTO_DB_Query("DELETE FROM " . PHOTO_ACCESS_RIGHT_TABLE . " WHERE shareid = ".$shareid);
  131. PHOTO_DB_Query("DELETE FROM " . PHOTO_UPLOAD_RIGHT_TABLE . " WHERE shareid = ".$shareid);
  132. PHOTO_DB_Query("DELETE FROM " . PHOTO_MANAGE_RIGHT_TABLE . " WHERE shareid = ".$shareid);
  133. } else if ('password' === $params['type'] && $params['password'] !== '') {
  134. $setValue = "public = 'f',password='".md5($params['password'])."'";
  135. array_push($setCondition, $setValue);
  136. /* remove all permissions if album is password */
  137. PHOTO_DB_Query("DeLETE FROM " . PHOTO_ACCESS_RIGHT_TABLE . " WHERE shareid = ".$shareid);
  138. PHOTO_DB_Query("DELETE FROM " . PHOTO_UPLOAD_RIGHT_TABLE . " WHERE shareid = ".$shareid);
  139. PHOTO_DB_Query("DELETE FROM " . PHOTO_MANAGE_RIGHT_TABLE . " WHERE shareid = ".$shareid);
  140. }
  141. if (null !== $params['allow_comment']) {
  142. if($params['allow_comment'] == 'true') {
  143. $setValue = "comment = 't'";
  144. } else {
  145. $setValue = "comment = 'f'";
  146. }
  147. array_push($setCondition, $setValue);
  148. }
  149. if (null !== $params['conversion']) {
  150. if ($params['conversion'] === 'true' && !$share['conversion']) {
  151. $needReindex = true;
  152. $setValue = "conversion = 't'";
  153. array_push($setCondition, $setValue);
  154. } elseif ($params['conversion'] !=='true' && $share['conversion']) {
  155. $setValue = "conversion = 'f'";
  156. array_push($setCondition, $setValue);
  157. }
  158. }
  159. $setString = implode(',', $setCondition);
  160. $query = "UPDATE photo_share SET {$setString} WHERE sharename = ?";
  161. $setQueryParam[] = $shareName;
  162. $db_result = PHOTO_DB_Query($query, $setQueryParam);
  163. //sort
  164. $data = array();
  165. if (null !== $params['sort_by']) {
  166. $data['type'] = self::ConvertToSortCode($params['sort_by']);
  167. }
  168. if (null !== $params['sort_direction']) {
  169. $data['order'] = self::ConvertToSortDirectionCode($params['sort_direction']);
  170. }
  171. if ('/' !== $shareName) {
  172. csSYNOPhotoAlbum::GetAlbumInstance()->SaveAlbumThumbSortType($shareName, $data);
  173. }
  174. csSYNOPhotoDB::GetDBInstance()->SetSessionCache(true);
  175. if ($needReindex) {
  176. @exec("/usr/syno/bin/synoindex -P PhotoStation -R " . escapeshellarg(SYNOPHOTO_SERVICE_REAL_DIR . "/" . $shareName));
  177. }
  178. return 0;
  179. }
  180. static function ConvertToTypeName($type) {
  181. switch ($type) {
  182. case 0:
  183. $typeName = 'album';
  184. break;
  185. case 1:
  186. $typeName = 'photo';
  187. break;
  188. case 2:
  189. $typeName = 'video';
  190. break;
  191. default:
  192. break;
  193. }
  194. return $typeName;
  195. }
  196. static function ConvertToSortDirection($value) {
  197. switch ($value) {
  198. case -1:
  199. $sortDirection = 'default';
  200. break;
  201. case 0:
  202. $sortDirection = 'asc';
  203. break;
  204. case 1:
  205. $sortDirection = 'desc';
  206. break;
  207. default:
  208. break;
  209. }
  210. return $sortDirection;
  211. }
  212. static function ConvertToSortDirectionCode($name) {
  213. switch ($name) {
  214. case 'default':
  215. $sortCode = -1;
  216. break;
  217. case 'asc':
  218. $sortCode = 0;
  219. break;
  220. case 'desc':
  221. $sortCode = 1;
  222. break;
  223. default:
  224. break;
  225. }
  226. return $sortCode;
  227. }
  228. static function ConvertToSortName($type) {
  229. switch ($type) {
  230. case -1:
  231. $sortBy = 'default';
  232. break;
  233. case 0:
  234. $sortBy = 'filename';
  235. break;
  236. case 1:
  237. $sortBy = 'takendate';
  238. break;
  239. case 2:
  240. $sortBy = 'createdate';
  241. break;
  242. case 3:
  243. $sortBy = 'preference';
  244. break;
  245. default:
  246. break;
  247. }
  248. return $sortBy;
  249. }
  250. static function ConvertToSortCode($name) {
  251. switch ($name) {
  252. case 'default':
  253. $sortCode = -1;
  254. break;
  255. case 'filename':
  256. $sortCode = 0;
  257. break;
  258. case 'takendate':
  259. $sortCode = 1;
  260. break;
  261. case 'createdate':
  262. $sortCode = 2;
  263. break;
  264. case 'preference':
  265. $sortCode = 3;
  266. break;
  267. default:
  268. break;
  269. }
  270. return $sortCode;
  271. }
  272. static function FilterByType($items, $type)
  273. {
  274. $filterItems = array();
  275. foreach ($items as $item) {
  276. $typeName = self::ConvertToTypeName($item['itemType']);
  277. if (in_array($typeName, $type)) {
  278. array_push($filterItems, $item);
  279. }
  280. }
  281. return $filterItems;
  282. }
  283. static function SortItem($photo_video_items, $sortBy, $sortDirection, $offset, $limit) {
  284. $itemsSort = array();
  285. // sort by photo, video
  286. if ('filename' === $sortBy) {
  287. if ('asc' === $sortDirection) {
  288. usort($photo_video_items, 'self::SortFilename_ASC');
  289. } else {
  290. usort($photo_video_items, 'self::SortFilename_DESC');
  291. }
  292. } elseif ('takendate' === $sortBy) {
  293. if ('asc' === $sortDirection) {
  294. usort($photo_video_items, 'self::SortTakendate_ASC');
  295. } else {
  296. usort($photo_video_items, 'self::SortTakendate_DESC');
  297. }
  298. } elseif ('createdate' === $sortBy) {
  299. if ('asc' === $sortDirection) {
  300. usort($photo_video_items, 'self::SortCreatedate_ASC');
  301. } else {
  302. usort($photo_video_items, 'self::SortCreatedate_DESC');
  303. }
  304. }
  305. if (false === $offset && false === $limit) {
  306. return $photo_video_items;
  307. }
  308. // set offset and limit
  309. $cutItemsSort = array();
  310. if (0 > (int)$limit) {
  311. $cutItemsSort = array_slice($photo_video_items, $offset);
  312. } else {
  313. $cutItemsSort = array_slice($photo_video_items, $offset, $limit);
  314. }
  315. return $cutItemsSort;
  316. }
  317. private function ExtractItem($items, &$albumItems, &$photo_video_items) {
  318. $index = count($items);
  319. foreach ($items as $key => $item) {
  320. if ('album' !== self::ConvertToTypeName($item['itemType'])) {
  321. $index = $key;
  322. break;
  323. }
  324. }
  325. $albumItems = array_slice($items, 0, $index);
  326. $photo_video_items = array_slice($items, $index);
  327. }
  328. static function SortFilename_ASC($a, $b) {
  329. if ($a['name'] == $b['name']) {
  330. return 0;
  331. }
  332. return ($a['name'] < $b['name']) ? -1 : 1;
  333. }
  334. static function SortFilename_DESC($a, $b) {
  335. if ($a['name'] == $b['name']) {
  336. return 0;
  337. }
  338. return ($a['name'] > $b['name']) ? -1 : 1;
  339. }
  340. static function SortTakendate_ASC($a, $b) {
  341. if ($a['takendate'] == $b['takendate']) {
  342. return ($a['name'] < $b['name']) ? -1 : 1;
  343. }
  344. return ($a['takendate'] < $b['takendate']) ? -1 : 1;
  345. }
  346. static function SortTakendate_DESC($a, $b) {
  347. if ($a['takendate'] == $b['takendate']) {
  348. return ($a['name'] > $b['name']) ? -1 : 1;
  349. }
  350. return ($a['takendate'] > $b['takendate']) ? -1 : 1;
  351. }
  352. static function SortCreatedate_ASC($a, $b) {
  353. if ($a['createdate'] == $b['createdate']) {
  354. return ($a['name'] < $b['name']) ? -1 : 1;
  355. }
  356. return ($a['createdate'] < $b['createdate']) ? -1 : 1;
  357. }
  358. static function SortCreatedate_DESC($a, $b) {
  359. if ($a['createdate'] == $b['createdate']) {
  360. return ($a['name'] > $b['name']) ? -1 : 1;
  361. }
  362. return ($a['createdate'] > $b['createdate']) ? -1 : 1;
  363. }
  364. static function SortPreference($items, $sharePath) {
  365. $sort = csSYNOPhotoAlbum::GetAlbumThumbSortType($sharePath);
  366. $list = array();
  367. $result = array();
  368. if (!empty($sort['list'])) {
  369. $list = $sort['list'];
  370. $preArr = array();
  371. foreach ($list as $key) {
  372. foreach ($items as $item) {
  373. if ($key === $item['name']) {
  374. array_push($result, $item);
  375. }
  376. }
  377. }
  378. // rest photo and video item
  379. foreach ($items as $item) {
  380. if (!in_array($item, $result)) {
  381. array_push($result, $item);
  382. }
  383. }
  384. } else {
  385. $result = $items;
  386. }
  387. return $result;
  388. }
  389. /*
  390. * @return [string] broken / small, large
  391. */
  392. static function GetThumbStatus($dbPath, $needSize, $orig_resolutionx, $orig_resolutiony, $blThumbRotated, $blConversion) {
  393. /* special case for gif, return original file */
  394. $path = SYNOPHOTO_SERVICE_REAL_DIR_PREFIX.$dbPath;
  395. $info = pathinfo($path);
  396. if ('gif' === strtolower($info['extension'])) {
  397. $ret['status'] = 'small,large';
  398. if ($needSize) {
  399. $imgInfo = @getImageSize($path);
  400. $mtime = @filemtime($path);
  401. $ret['size']['preview']['resolutionx'] = $imgInfo[0];
  402. $ret['size']['preview']['resolutiony'] = $imgInfo[1];
  403. $ret['size']['preview']['mtime'] = $mtime;
  404. $ret['size']['small']['resolutionx'] = $imgInfo[0];
  405. $ret['size']['small']['resolutiony'] = $imgInfo[1];
  406. $ret['size']['small']['mtime'] = $mtime;
  407. $ret['size']['large']['resolutionx'] = $imgInfo[0];
  408. $ret['size']['large']['resolutiony'] = $imgInfo[1];
  409. $ret['size']['large']['mtime'] = $mtime;
  410. }
  411. return $ret;
  412. }
  413. if ($needSize) {
  414. $ret['size']['preview']['resolutionx'] = 0;
  415. $ret['size']['preview']['resolutiony'] = 0;
  416. $ret['size']['small']['resolutionx'] = 0;
  417. $ret['size']['small']['resolutiony'] = 0;
  418. $ret['size']['large']['resolutionx'] = 0;
  419. $ret['size']['large']['resolutiony'] = 0;
  420. }
  421. $file_name = basename($path);
  422. $dir = dirname($path);
  423. if (SYNOPhotoEA::checkFilePathByDirFile($dir, $file_name, SYNOPhotoEA::FILE_THUMB_M_FAIL, $thumbSmallBroken) &&
  424. SYNOPhotoEA::checkFilePathByDirFile($dir, $file_name, SYNOPhotoEA::FILE_THUMB_XL_FAIL, $thumbLargeBroken)) {
  425. $ret['status'] = 'broken';
  426. } else {
  427. $thumbStatus = array();
  428. $hasSmall = $hasLarge = false;
  429. if (!$blConversion) {
  430. $thumbStatus[] = 'disable';
  431. }
  432. if (SYNOPhotoEA::checkFilePathByDirFile($dir, $file_name, SYNOPhotoEA::FILE_THUMB_M, $thumbSmallPath)) {
  433. $hasSmall = true;
  434. if ($needSize) {
  435. $ret['size']['small'] = self::CalculateThumbSize($orig_resolutionx, $orig_resolutiony, $blThumbRotated, SYNOPHOTO_THUMBMEDIUM_WIDTH);
  436. $ret['size']['small']['mtime'] = @filemtime($thumbSmallPath);
  437. }
  438. }
  439. if (SYNOPhotoEA::checkFilePathByDirFile($dir, $file_name, SYNOPhotoEA::FILE_THUMB_PREVIEW, $thumbPreviewPath)) {
  440. $thumbStatus[] = 'preview';
  441. if ($needSize) {
  442. if ($hasSmall) {
  443. $ret['size']['preview'] = self::CalculateThumbSize($orig_resolutionx, $orig_resolutiony, $blThumbRotated, SYNOPHOTO_THUMBPREVIEW_WIDTH);
  444. } else {
  445. $ret['size']['preview'] = self::GetThumbSize($path, $thumbPreviewPath);
  446. }
  447. $ret['size']['preview']['mtime'] = @filemtime($thumbPreviewPath);
  448. }
  449. //use preview-thumb as small-thumb in thumbnail-less browsing
  450. if (!$blConversion && !$hasSmall) {
  451. $hasSmall = true;
  452. if ($needSize) {
  453. $ret['size']['small'] = $ret['size']['preview'];
  454. $ret['size']['small']['mtime'] = $ret['size']['preview']['mtime'];
  455. }
  456. }
  457. }
  458. if (SYNOPhotoEA::checkFilePathByDirFile($dir, $file_name, SYNOPhotoEA::FILE_THUMB_XL, $thumbLargePath)) {
  459. $hasLarge = true;
  460. if ($needSize) {
  461. $ret['size']['large'] = self::CalculateThumbSize($orig_resolutionx, $orig_resolutiony, $blThumbRotated, SYNOPHOTO_THUMBXLARGE_WIDTH);
  462. $ret['size']['large']['mtime'] = @filemtime($thumbLargePath);
  463. }
  464. }
  465. if (!$hasLarge && SYNOPhotoEA::checkFilePathByDirFile($dir, $file_name, SYNOPhotoEA::FILE_THUMB_LARGE_PREVIEW, $thumbLargePreviewPath)) {
  466. $thumbStatus[] = 'large';
  467. if ($needSize) {
  468. $ret['size']['large'] = self::GetThumbSize($path, $thumbLargePreviewPath);
  469. $ret['size']['large']['mtime'] = @filemtime($thumbLargePreviewPath);
  470. }
  471. }
  472. //use original file as large thumb in thumbnail-less browsing
  473. if (!$blConversion && !$hasLarge && in_array(strtolower($info['extension']), array('jpeg', 'bmp', 'png', 'jpg', 'jpe'/*, 'image/tiff'*/))) {
  474. $hasLarge = true;
  475. if ($needSize) {
  476. $ret['size']['large']['resolutionx'] = $orig_resolutionx;
  477. $ret['size']['large']['resolutiony'] = $orig_resolutiony;
  478. $ret['size']['large']['mtime'] = @filemtime($path);
  479. }
  480. $thumbStatus[] = "large_original";
  481. }
  482. if ($hasSmall) {
  483. $thumbStatus[] = 'small';
  484. }
  485. if ($hasLarge) {
  486. $thumbStatus[] = 'large';
  487. }
  488. $ret['status'] = implode(',', $thumbStatus);
  489. }
  490. return $ret;
  491. }
  492. static function CalculateThumbSize($orig_resolutionx, $orig_resolutiony, $blThumbRotated, $thumbSize)
  493. {
  494. $thumb['resolutionx'] = $thumb['resolutiony'] = 0;
  495. if (0 >= $orig_resolutionx || 0 >= $orig_resolutiony || 0 >= $thumbSize) {
  496. goto End;
  497. }
  498. $ratio = $orig_resolutionx / $orig_resolutiony;
  499. if (0 >= $ratio) {
  500. goto End;
  501. }
  502. $thumb['resolutionx'] = $orig_resolutionx;
  503. $thumb['resolutiony'] = $orig_resolutiony;
  504. if ($thumbSize < $orig_resolutionx && $thumbSize < $orig_resolutiony) {
  505. if ($orig_resolutionx >= $orig_resolutiony) {
  506. $thumb['resolutionx'] = ceil($thumbSize * $ratio);
  507. $thumb['resolutiony'] = $thumbSize;
  508. } else {
  509. $thumb['resolutionx'] = $thumbSize;
  510. $thumb['resolutiony'] = ceil($thumbSize / $ratio);
  511. }
  512. }
  513. if ($blThumbRotated) {
  514. $tmp = $thumb['resolutionx'];
  515. $thumb['resolutionx'] = $thumb['resolutiony'];
  516. $thumb['resolutiony'] = $tmp;
  517. }
  518. End:
  519. return $thumb;
  520. }
  521. static function GetThumbSize($imgPath, $thumbPath) {
  522. $thumbInfo = @getImageSize($thumbPath);
  523. $ret = array(
  524. 'resolutionx' => $thumbInfo[0],
  525. 'resolutiony' => $thumbInfo[1]
  526. );
  527. $info = pathinfo($imgPath);
  528. if (!in_array(strtolower($info['extension']), array('jpeg', 'bmp', 'png', 'jpg', 'jpe'/*, 'image/tiff'*/))) {
  529. goto END;
  530. }
  531. $imgInfo = @getImageSize($imgPath);
  532. $rotated = ($thumbInfo[0] > $thumbInfo[1] && $imgInfo[0] <= $imgInfo[1]) || ($thumbInfo[0] <= $thumbInfo[1] && $imgInfo[0] > $imgInfo[1]);
  533. if ($rotated && $thumbInfo[0] > $imgInfo[1]) {
  534. $ret['resolutionx'] = $imgInfo[1];
  535. $ret['resolutiony'] = $imgInfo[0];
  536. } else if (!$rotated && $thumbInfo[0] > $imgInfo[0]) {
  537. $ret['resolutionx'] = $imgInfo[0];
  538. $ret['resolutiony'] = $imgInfo[1];
  539. }
  540. END:
  541. return $ret;
  542. }
  543. static function FillThumbStatus(&$item, $coverDBPath, $needThumbSize, $orig_resolutionx, $orig_resolutiony, $blThumbRotated, $blConversion)
  544. {
  545. if ('' !== $coverDBPath) {
  546. $thumbnail = self::GetThumbStatus($coverDBPath, $needThumbSize, $orig_resolutionx, $orig_resolutiony, $blThumbRotated, $blConversion);
  547. } else {
  548. $thumbnail = array('status' => 'default');
  549. if (!$blConversion) {
  550. $thumbnail['status'] .= ',disable';
  551. }
  552. if ($needThumbSize) {
  553. $thumbnail['size'] = array(
  554. 'preview' => array('resolutionx' => 0, 'resolutiony' => 0),
  555. 'small' => array('resolutionx' => 0, 'resolutiony' => 0),
  556. 'large' => array('resolutionx' => 0, 'resolutiony' => 0)
  557. );
  558. }
  559. }
  560. $item['thumbnail_status'] = $thumbnail['status'];
  561. if ($needThumbSize) {
  562. if (!is_array($item['additional'])) {
  563. $item['additional'] = array();
  564. }
  565. $item['additional']['thumb_size'] = $thumbnail['size'];
  566. $item['additional']['thumb_size']['sig'] = bin2hex($coverDBPath);
  567. }
  568. }
  569. static function GetCoverResolutionRotation($coverPath) {
  570. if ('' === $coverPath) {
  571. return array(0, 0, false);
  572. }
  573. $file = csSYNOPhotoMisc::IsPhotoFile($coverPath) ?
  574. File::GetPhotoByDBPath(array($coverPath)) : File::GetVideoByDBPath(array($coverPath));
  575. $info = $file[$coverPath];
  576. return array((int)$info['resolutionx'], (int)$info['resolutiony'], 0 !== (int)$info['version'] % 2);
  577. }
  578. static function GetAlbumPermission($sharePath) {
  579. $permission['browse'] = csSYNOPhotoMisc::CheckAlbumAccessible($sharePath);
  580. $permission['upload'] = csSYNOPhotoMisc::CheckAlbumUploadable($sharePath);
  581. $permission['manage'] = csSYNOPhotoMisc::CheckAlbumManageable($sharePath);
  582. return $permission;
  583. }
  584. static function GetUnConfirmItemList($limit=false, $offset=false)
  585. {
  586. $result = array();
  587. $limitOffsetString = PHOTO_DB_GetLimitOffsetString($limit, $offset);
  588. $query = "SELECT photo_image.path FROM photo_image_label LEFT JOIN photo_image ON photo_image_label.image_id=photo_image.id WHERE status='f'";
  589. $queryCount = "SELECT COUNT(DISTINCT photo_image.path) FROM photo_image_label LEFT JOIN photo_image ON photo_image_label.image_id=photo_image.id WHERE status='f'";
  590. $albumCondition = csSYNOPhotoMisc::GetAccessibleAlbumQueryConditionWithExcludeFormat();
  591. if(!$albumCondition['albumCond']) {
  592. return $result;
  593. }
  594. $query .= " AND {$albumCondition['albumCond']}";
  595. $query .= " ORDER BY photo_image.create_time DESC ";
  596. $query .= $limitOffsetString;
  597. $queryCount .= " AND {$albumCondition['albumCond']}";
  598. $db_result = PHOTO_DB_Query($query, $albumCondition['sqlParam']);
  599. while (false !== ($row = PHOTO_DB_FetchRow($db_result))) {
  600. if ($row['path']) {
  601. $result[SYNOPHOTO_SERVICE_REAL_DIR_PREFIX.$row['path']] = SYNOPHOTO_ITEM_TYPE_PHOTO;
  602. }
  603. }
  604. $db_result = PHOTO_DB_Query($queryCount, $sqlParam);
  605. $row = PHOTO_DB_FetchRow($db_result);
  606. $result['total'] = $row[0];
  607. return $result;
  608. }
  609. /*
  610. * @param $videoPath video db path
  611. */
  612. static function GetConvertedVideoInfo($dPath)
  613. {
  614. $query = "SELECT * FROM video_convert WHERE video_path = ?";
  615. $sqlParam = array($dPath);
  616. $db_result = PHOTO_DB_Query($query, $sqlParam);
  617. $videoArr = array();
  618. while ($videoInfo = PHOTO_DB_FetchRow($db_result)) {
  619. $videoQuality['id'] = bin2hex(SYNOPHOTO_SERVICE_REAL_DIR_PREFIX.$videoInfo['convert_file_path']);
  620. $videoQuality['container'] = $videoInfo['container_type'];
  621. $videoQuality['vcodec'] = $videoInfo['vcodec'];
  622. $videoQuality['acodec'] = $videoInfo['acodec'];
  623. $videoQuality['filesize'] = (int)$videoInfo['filesize'];
  624. $videoQuality['resolutionx'] = (int)$videoInfo['resolutionx'];
  625. $videoQuality['resolutiony'] = (int)$videoInfo['resolutiony'];
  626. $videoQuality['video_bitrate'] = (int)$videoInfo['video_bitrate'];
  627. $videoQuality['audio_bitrate'] = (int)$videoInfo['audio_bitrate'];
  628. $videoQuality['video_profile'] = (int)$videoInfo['video_profile'];
  629. $videoQuality['video_level'] = (int)$videoInfo['video_level'];
  630. $videoQuality['profile_name'] = self::GetConvertedName($videoInfo['convert_file_path']);
  631. array_push($videoArr, $videoQuality);
  632. }
  633. return $videoArr;
  634. }
  635. static function GetConvertedName($convertPath)
  636. {
  637. $fileName = basename($convertPath);
  638. $pool = array(SYNOPhotoEA::FILE_FILM_FLV, SYNOPhotoEA::FILE_FILM_H264_MP4, SYNOPhotoEA::FILE_FILM_H_MP4, SYNOPhotoEA::FILE_FILM_M_MP4, SYNOPhotoEA::FILE_FILM_L_MP4,
  639. SYNOPhotoEA::FILE_FILM_MOBILE_MP4, SYNOPhotoEA::FILE_FILM_MOBILE_IPHONE, SYNOPhotoEA::FILE_FILM_MOBILE_ANDROID);
  640. foreach ($pool as $name) {
  641. $oldName = SYNOPhotoEA::SYNO_PHOTO_PREFIX_OLD.$name;
  642. $newName = SYNOPhotoEA::SYNO_PHOTO_PREFIX.$name;
  643. if ($oldName === $fileName || $newName === $fileName) {
  644. if (SYNOPhotoEA::FILE_FILM_FLV === $name) {
  645. $ret = 'flv';
  646. } elseif (SYNOPhotoEA::FILE_FILM_H264_MP4 === $name) {
  647. $ret = 'orig_h264';
  648. } elseif (SYNOPhotoEA::FILE_FILM_MPEG4_MP4 === $name) {
  649. $ret = 'orig_mp4';
  650. } elseif (SYNOPhotoEA::FILE_FILM_H_MP4 === $name) {
  651. $ret = 'high';
  652. } elseif (SYNOPhotoEA::FILE_FILM_M_MP4 === $name) {
  653. $ret = 'medium';
  654. } elseif (SYNOPhotoEA::FILE_FILM_L_MP4 === $name) {
  655. $ret = 'low';
  656. } elseif (SYNOPhotoEA::FILE_FILM_MOBILE_MP4 === $name) {
  657. $ret = 'mobile';
  658. } elseif (SYNOPhotoEA::FILE_FILM_MOBILE_IPHONE === $name) {
  659. $ret = 'orig_iphone';
  660. } elseif (SYNOPhotoEA::FILE_FILM_MOBILE_ANDROID === $name) {
  661. $ret = 'orig_android';
  662. } elseif (SYNOPhotoEA::FILE_FILM_CONVERT_MPEG4_MP4 === $name) {
  663. $ret = 'convert_mp4';
  664. }
  665. break;
  666. }
  667. }
  668. return $ret;
  669. }
  670. static function IsAlbumCommentalbGlobal($checkSession = false)
  671. {
  672. $blAllowUserComment = ('on' === csSYNOPhotoMisc::GetConfigDB("photo", "allow_user_comment", "photo_config"));
  673. $blAllowGuestComment = ('on' === csSYNOPhotoMisc::GetConfigDB("photo", "allow_guest_comment", "photo_config"));
  674. if ($blAllowGuestComment) {
  675. return true;
  676. } elseif ($blAllowUserComment) {
  677. if ($checkSession) {
  678. csSYNOPhotoMisc::CheckSessionTimeOut(true);
  679. return true;
  680. } else {
  681. if (isset($_SESSION[SYNOPHOTO_ADMIN_USER]['reg_syno_user']) || isset($_SESSION[SYNOPHOTO_ADMIN_USER]['admin_syno_user'])) {
  682. return true;
  683. }
  684. }
  685. }
  686. return false;
  687. }
  688. static function IsShowAlbumHit() {
  689. $value = csSYNOPhotoMisc::GetConfigDB("photo", "show_album_hit", "photo_config");
  690. $show_album_hit = 'off' === $value ? false : true ;
  691. return $show_album_hit;
  692. }
  693. static function FormAlbum($album_db_object, $global_allow_comment, $show_hits) {
  694. $name = pathinfo($album_db_object['sharename'], PATHINFO_BASENAME);
  695. $res = array(
  696. 'sharepath' => $album_db_object['sharename'],
  697. 'name' => $name,
  698. 'title' => $album_db_object['title'] ? $album_db_object['title'] : $name,
  699. 'description' => $album_db_object['description'],
  700. 'hits' => $show_hits ? $album_db_object['hits'] : 0,
  701. 'type' => $album_db_object['public'] ? 'public' : ($album_db_object['password'] ? 'password' : 'private'),
  702. 'conversion' => $album_db_object['conversion'],
  703. 'allow_comment' => $global_allow_comment
  704. );
  705. return $res;
  706. }
  707. static function FormAdditional($type, $item, $params) {
  708. if ('album' === $type) {
  709. $sharePath = $item['sharename'];
  710. $shareid = $item['shareid'];
  711. if (in_array('album_sorting', $params['additional'])) {
  712. $sortData = csSYNOPhotoAlbum::GetAlbumInstance()->GetAlbumThumbSortType($sharePath);
  713. $sortBy = AlbumAPIUtil::ConvertToSortName($sortData['type']);
  714. $additional['album_sorting']['sort_by'] = $sortBy;
  715. $additional['album_sorting']['sort_direction'] = AlbumAPIUtil::ConvertToSortDirection($sortData['order']);
  716. $additional['album_sorting']['has_preference_sort'] = !empty($sortData);
  717. }
  718. if (in_array('album_permission', $params['additional'])) {
  719. // here
  720. $additional['album_permission'] = AlbumAPIUtil::GetAlbumPermission($sharePath);
  721. }
  722. if (in_array('item_count', $params['additional'])) {
  723. $additional['item_count'] = array(
  724. 'photo' => Album::GetPhotoCount($item['shareid']),
  725. 'video' => Album::GetVideoCount($item['shareid'])
  726. );
  727. }
  728. } elseif ('photo' === $type) {
  729. if (in_array('photo_exif', $params['additional'])) {
  730. $photoInfo = $item;
  731. $additional['photo_exif']['takendate'] = $photoInfo['timetaken'];
  732. $additional['photo_exif']['camera'] = $photoInfo['camera_make'];
  733. $additional['photo_exif']['camera_model'] = $photoInfo['camera_model'];
  734. $additional['photo_exif']['exposure'] = $photoInfo['exposure'];
  735. $additional['photo_exif']['aperture'] = $photoInfo['aperture'];
  736. $additional['photo_exif']['iso'] = (int)$photoInfo['iso'];
  737. $additional['photo_exif']['gps'] = json_decode($photoInfo['gps'], true);
  738. $additional['photo_exif']['focal_length'] = $photoInfo['focal_length_v2'];
  739. $additional['photo_exif']['lens'] = $photoInfo['lens_v2'];
  740. $additional['photo_exif']['flash'] = $photoInfo['flash_v2'];
  741. }
  742. } elseif ('video' === $type) {
  743. if (in_array('video_codec', $params['additional'])) {
  744. $videoInfo = $item;
  745. $additional['video_codec']['container'] = $videoInfo['container_type'];
  746. $additional['video_codec']['vcodec'] = $videoInfo['video_codec'];
  747. $additional['video_codec']['acodec'] = $videoInfo['audio_codec'];
  748. $additional['video_codec']['resolutionx'] = (int)$videoInfo['resolutionx'];
  749. $additional['video_codec']['resolutiony'] = (int)$videoInfo['resolutiony'];
  750. $additional['video_codec']['frame_bitrate'] = (int)$videoInfo['frame_bitrate'];
  751. $additional['video_codec']['video_bitrate'] = (int)$videoInfo['video_bitrate'];
  752. $additional['video_codec']['audio_bitrate'] = (int)$videoInfo['audio_bitrate'];
  753. }
  754. if (in_array('video_quality', $params['additional'])) {
  755. $additional['video_quality'] = AlbumAPIUtil::GetConvertedVideoInfo($item['path']);
  756. }
  757. }
  758. if (in_array('file_location', $params['additional'])) {
  759. $additional['file_location'] = 'album' === $type ?
  760. $sharePath : substr($item['path'], strlen(SYNOPHOTO_SERVICE_REAL_DIR_PATH));
  761. }
  762. return $additional;
  763. }
  764. static function GetRootAlbumInfo($params){
  765. $info['description'] = '';
  766. $info['sharepath'] = '/';
  767. $info['name'] = '/';
  768. $info['title'] = csSYNOPhotoMisc::GetConfigDB("photo", "photo_page_title", "photo_config");
  769. $info['hits'] = null;
  770. $info['allow_comment'] = ('on' === csSYNOPhotoMisc::GetConfigDB("photo", "album_def_allow_comment", "photo_config")) ? true : false;
  771. $info['type'] = 'public';
  772. $info['conversion'] = csSYNOPhotoMisc::IsAlbumAllowConversion('/');
  773. $item['id'] = null;
  774. $item['info'] = $info;
  775. if (in_array('album_sorting', $params['additional'])) {
  776. $value = csSYNOPhotoMisc::GetConfigDB("album", "album_order_type", "photo_config");
  777. $additional['album_sorting']['sort_by'] = ('1' === $value) ? 'preference' : 'filename';
  778. $value = csSYNOPhotoMisc::GetConfigDB("album", "album_order_type_is_desc", "photo_config");
  779. $additional['album_sorting']['sort_direction'] = ('1' === $value) ? 'desc' : 'asc';
  780. $value = csSYNOPhotoAlbum::GetAlbumThumbSortType($info['sharepath']);
  781. $additional['album_sorting']['has_preference_sort'] = !empty($value);
  782. }
  783. if (in_array('album_permission', $params['additional'])) {
  784. $permission = self::GetAlbumPermission('/');
  785. $additional['album_permission'] = $permission;
  786. }
  787. if (null !== $additional) {
  788. $item['additional'] = $additional;
  789. }
  790. return $item;
  791. }
  792. }
  793. ?>