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.

270 lines
7.3 KiB

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