3 * @file src/Module/Photo.php
6 namespace Friendica\Module;
8 use Friendica\BaseModule;
9 use Friendica\Core\Logger;
10 use Friendica\Core\System;
11 use Friendica\Model\Photo as MPhoto;
12 use Friendica\Object\Image;
13 use Friendica\Util\Security;
14 use Friendica\Network\HTTPException\NotFoundException;
15 use Friendica\Network\HTTPException\BadRequestException;
16 use Friendica\Network\HTTPException\InternalServerErrorException;
21 class Photo extends BaseModule
25 * @brief Module initializer
27 * Fetch a photo or an avatar, in optional size, check for permissions and
30 public static function init()
33 if ($a->argc <= 1 || $a->argc > 4) {
34 System::httpExit(400, "Bad Request");
37 if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
38 header("HTTP/1.1 304 Not Modified");
39 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
40 if (!empty($_SERVER["HTTP_IF_NONE_MATCH"])) {
41 header("Etag: " . $_SERVER["HTTP_IF_NONE_MATCH"]);
43 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
44 header("Cache-Control: max-age=31536000");
45 if (function_exists("header_remove")) {
46 header_remove("Last-Modified");
47 header_remove("Expires");
48 header_remove("Cache-Control");
56 $customsize = intval($a->argv[2]);
57 $uid = self::stripExtension($a->argv[3]);
58 $photo = self::getAvatar($uid, $a->argv[1]);
61 $uid = self::stripExtension($a->argv[2]);
62 $photo = self::getAvatar($uid, $a->argv[1]);
65 $photoid = self::stripExtension($a->argv[1]);
67 if (substr($photoid, -2, 1) == "-") {
68 $scale = intval(substr($photoid, -1, 1));
69 $photoid = substr($photoid, 0, -2);
71 $photo = MPhoto::getPhoto($photoid, $scale);
76 // not using System::httpExit() because we don't want html here.
77 header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found" , true, 404);
81 $cacheable = ($photo["allow_cid"] . $photo["allow_gid"] . $photo["deny_cid"] . $photo["deny_gid"] === "") && (isset($photo["cacheable"]) ? $photo["cacheable"] : true);
83 $img = MPhoto::getImageForPhoto($photo);
85 if (is_null($img) || !$img->isValid()) {
86 Logger::log("Invalid photo with id {$photo["id"]}.");
87 System::httpExit(500, "Internal Server Error");
91 // if customsize is set and image is not a gif, resize it
92 if ($img->getType() !== "image/gif" && $customsize > 0 && $customsize < 501) {
93 $img->scaleToSquare($customsize);
97 if (function_exists("header_remove")) {
98 header_remove("Pragma");
99 header_remove("pragma");
102 header("Content-type: " . $img->getType());
105 // it is a private photo that they have no permission to view.
106 // tell the browser not to cache it, in case they authenticate
107 // and subsequently have permission to see it
108 header("Cache-Control: no-store, no-cache, must-revalidate");
110 $md5 = md5($img->asString());
111 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
112 header("Etag: \"{$md5}\"");
113 header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
114 header("Cache-Control: max-age=31536000");
118 echo $img->asString();
124 private static function stripExtension($name)
126 $name = str_replace([".jpg", ".png", ".gif"], ["", "", ""], $name);
127 foreach (Image::supportedTypes() as $m => $e) {
128 $name = str_replace("." . $e, "", $name);
133 private static function getAvatar($uid, $type="avatar")
140 $default = "images/person-300.jpg";
144 $default = "images/person-48.jpg";
149 $default = "images/person-80.jpg";
152 $photo = MPhoto::selectFirst([], ["scale" => $scale, "uid" => $uid, "profile" => 1]);
153 if ($photo === false) {
154 $photo = MPhoto::createPhotoForSystemResource($default);