Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
7.5KB

  1. <?php
  2. /**
  3. * @file src/Model/Photo.php
  4. * @brief This file contains the Photo class for database interface
  5. */
  6. namespace Friendica\Model;
  7. use Friendica\Core\Cache;
  8. use Friendica\Core\Config;
  9. use Friendica\Core\L10n;
  10. use Friendica\Core\System;
  11. use Friendica\Database\DBA;
  12. use Friendica\Object\Image;
  13. use Friendica\Util\DateTimeFormat;
  14. use Friendica\Util\Network;
  15. use Friendica\Util\Security;
  16. /**
  17. * Class to handle photo dabatase table
  18. */
  19. class Photo
  20. {
  21. /**
  22. * @param Image $Image image
  23. * @param integer $uid uid
  24. * @param integer $cid cid
  25. * @param integer $rid rid
  26. * @param string $filename filename
  27. * @param string $album album name
  28. * @param integer $scale scale
  29. * @param integer $profile optional, default = 0
  30. * @param string $allow_cid optional, default = ''
  31. * @param string $allow_gid optional, default = ''
  32. * @param string $deny_cid optional, default = ''
  33. * @param string $deny_gid optional, default = ''
  34. * @param string $desc optional, default = ''
  35. * @return object
  36. */
  37. public static function store(Image $Image, $uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '', $desc = '')
  38. {
  39. $photo = DBA::selectFirst('photo', ['guid'], ["`resource-id` = ? AND `guid` != ?", $rid, '']);
  40. if (DBA::isResult($photo)) {
  41. $guid = $photo['guid'];
  42. } else {
  43. $guid = System::createGUID();
  44. }
  45. $existing_photo = DBA::selectFirst('photo', ['id'], ['resource-id' => $rid, 'uid' => $uid, 'contact-id' => $cid, 'scale' => $scale]);
  46. $fields = [
  47. 'uid' => $uid,
  48. 'contact-id' => $cid,
  49. 'guid' => $guid,
  50. 'resource-id' => $rid,
  51. 'created' => DateTimeFormat::utcNow(),
  52. 'edited' => DateTimeFormat::utcNow(),
  53. 'filename' => basename($filename),
  54. 'type' => $Image->getType(),
  55. 'album' => $album,
  56. 'height' => $Image->getHeight(),
  57. 'width' => $Image->getWidth(),
  58. 'datasize' => strlen($Image->asString()),
  59. 'data' => $Image->asString(),
  60. 'scale' => $scale,
  61. 'profile' => $profile,
  62. 'allow_cid' => $allow_cid,
  63. 'allow_gid' => $allow_gid,
  64. 'deny_cid' => $deny_cid,
  65. 'deny_gid' => $deny_gid,
  66. 'desc' => $desc
  67. ];
  68. if (DBA::isResult($existing_photo)) {
  69. $r = DBA::update('photo', $fields, ['id' => $existing_photo['id']]);
  70. } else {
  71. $r = DBA::insert('photo', $fields);
  72. }
  73. return $r;
  74. }
  75. /**
  76. * @param string $image_url Remote URL
  77. * @param integer $uid user id
  78. * @param integer $cid contact id
  79. * @param boolean $quit_on_error optional, default false
  80. * @return array
  81. */
  82. public static function importProfilePhoto($image_url, $uid, $cid, $quit_on_error = false)
  83. {
  84. $thumb = '';
  85. $micro = '';
  86. $photo = DBA::selectFirst(
  87. 'photo', ['resource-id'], ['uid' => $uid, 'contact-id' => $cid, 'scale' => 4, 'album' => 'Contact Photos']
  88. );
  89. if (!empty($photo['resource-id'])) {
  90. $hash = $photo['resource-id'];
  91. } else {
  92. $hash = self::newResource();
  93. }
  94. $photo_failure = false;
  95. $filename = basename($image_url);
  96. $img_str = Network::fetchUrl($image_url, true);
  97. if ($quit_on_error && ($img_str == "")) {
  98. return false;
  99. }
  100. $type = Image::guessType($image_url, true);
  101. $Image = new Image($img_str, $type);
  102. if ($Image->isValid()) {
  103. $Image->scaleToSquare(300);
  104. $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 4);
  105. if ($r === false) {
  106. $photo_failure = true;
  107. }
  108. $Image->scaleDown(80);
  109. $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 5);
  110. if ($r === false) {
  111. $photo_failure = true;
  112. }
  113. $Image->scaleDown(48);
  114. $r = self::store($Image, $uid, $cid, $hash, $filename, 'Contact Photos', 6);
  115. if ($r === false) {
  116. $photo_failure = true;
  117. }
  118. $suffix = '?ts=' . time();
  119. $image_url = System::baseUrl() . '/photo/' . $hash . '-4.' . $Image->getExt() . $suffix;
  120. $thumb = System::baseUrl() . '/photo/' . $hash . '-5.' . $Image->getExt() . $suffix;
  121. $micro = System::baseUrl() . '/photo/' . $hash . '-6.' . $Image->getExt() . $suffix;
  122. // Remove the cached photo
  123. $a = get_app();
  124. $basepath = $a->getBasePath();
  125. if (is_dir($basepath . "/photo")) {
  126. $filename = $basepath . '/photo/' . $hash . '-4.' . $Image->getExt();
  127. if (file_exists($filename)) {
  128. unlink($filename);
  129. }
  130. $filename = $basepath . '/photo/' . $hash . '-5.' . $Image->getExt();
  131. if (file_exists($filename)) {
  132. unlink($filename);
  133. }
  134. $filename = $basepath . '/photo/' . $hash . '-6.' . $Image->getExt();
  135. if (file_exists($filename)) {
  136. unlink($filename);
  137. }
  138. }
  139. } else {
  140. $photo_failure = true;
  141. }
  142. if ($photo_failure && $quit_on_error) {
  143. return false;
  144. }
  145. if ($photo_failure) {
  146. $image_url = System::baseUrl() . '/images/person-300.jpg';
  147. $thumb = System::baseUrl() . '/images/person-80.jpg';
  148. $micro = System::baseUrl() . '/images/person-48.jpg';
  149. }
  150. return [$image_url, $thumb, $micro];
  151. }
  152. /**
  153. * @param string $exifCoord coordinate
  154. * @param string $hemi hemi
  155. * @return float
  156. */
  157. public static function getGps($exifCoord, $hemi)
  158. {
  159. $degrees = count($exifCoord) > 0 ? self::gps2Num($exifCoord[0]) : 0;
  160. $minutes = count($exifCoord) > 1 ? self::gps2Num($exifCoord[1]) : 0;
  161. $seconds = count($exifCoord) > 2 ? self::gps2Num($exifCoord[2]) : 0;
  162. $flip = ($hemi == 'W' || $hemi == 'S') ? -1 : 1;
  163. return floatval($flip * ($degrees + ($minutes / 60) + ($seconds / 3600)));
  164. }
  165. /**
  166. * @param string $coordPart coordPart
  167. * @return float
  168. */
  169. private static function gps2Num($coordPart)
  170. {
  171. $parts = explode('/', $coordPart);
  172. if (count($parts) <= 0) {
  173. return 0;
  174. }
  175. if (count($parts) == 1) {
  176. return $parts[0];
  177. }
  178. return floatval($parts[0]) / floatval($parts[1]);
  179. }
  180. /**
  181. * @brief Fetch the photo albums that are available for a viewer
  182. *
  183. * The query in this function is cost intensive, so it is cached.
  184. *
  185. * @param int $uid User id of the photos
  186. * @param bool $update Update the cache
  187. *
  188. * @return array Returns array of the photo albums
  189. */
  190. public static function getAlbums($uid, $update = false)
  191. {
  192. $sql_extra = Security::getPermissionsSQLByUserId($uid);
  193. $key = "photo_albums:".$uid.":".local_user().":".remote_user();
  194. $albums = Cache::get($key);
  195. if (is_null($albums) || $update) {
  196. if (!Config::get('system', 'no_count', false)) {
  197. /// @todo This query needs to be renewed. It is really slow
  198. // At this time we just store the data in the cache
  199. $albums = q("SELECT COUNT(DISTINCT `resource-id`) AS `total`, `album`, ANY_VALUE(`created`) AS `created`
  200. FROM `photo`
  201. WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra
  202. GROUP BY `album` ORDER BY `created` DESC",
  203. intval($uid),
  204. DBA::escape('Contact Photos'),
  205. DBA::escape(L10n::t('Contact Photos'))
  206. );
  207. } else {
  208. // This query doesn't do the count and is much faster
  209. $albums = q("SELECT DISTINCT(`album`), '' AS `total`
  210. FROM `photo` USE INDEX (`uid_album_scale_created`)
  211. WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' $sql_extra",
  212. intval($uid),
  213. DBA::escape('Contact Photos'),
  214. DBA::escape(L10n::t('Contact Photos'))
  215. );
  216. }
  217. Cache::set($key, $albums, Cache::DAY);
  218. }
  219. return $albums;
  220. }
  221. /**
  222. * @param int $uid User id of the photos
  223. * @return void
  224. */
  225. public static function clearAlbumCache($uid)
  226. {
  227. $key = "photo_albums:".$uid.":".local_user().":".remote_user();
  228. Cache::set($key, null, Cache::DAY);
  229. }
  230. /**
  231. * Generate a unique photo ID.
  232. *
  233. * @return string
  234. */
  235. public static function newResource()
  236. {
  237. return system::createGUID(32, false);
  238. }
  239. }