X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=mod%2Fproxy.php;h=6dc396787664ff64211f5d3c9a4bc2987a6be7fe;hb=daa1177e3a1e42b4c95e0a8759f1610942b952c7;hp=65b3fdc510f89b9aca131f07b33000e1ddbac71c;hpb=3c24bed412235cf8c7a3f16b46fed18004abf87b;p=friendica.git diff --git a/mod/proxy.php b/mod/proxy.php index 65b3fdc510..6dc3967876 100644 --- a/mod/proxy.php +++ b/mod/proxy.php @@ -1,8 +1,18 @@ +/** + * @file mod/proxy.php + * @brief Based upon "Privacy Image Cache" by Tobias Hößl + */ use Friendica\App; +use Friendica\Core\Config; use Friendica\Core\System; +use Friendica\Database\dba; +use Friendica\Database\DBM; +use Friendica\Model\Photo; +use Friendica\Object\Image; +use Friendica\Util\DateTimeFormat; +use Friendica\Util\Network; define('PROXY_DEFAULT_TIME', 86400); // 1 Day @@ -13,7 +23,6 @@ define('PROXY_SIZE_MEDIUM', 'medium'); define('PROXY_SIZE_LARGE', 'large'); require_once 'include/security.php'; -require_once 'include/Photo.php'; function proxy_init(App $a) { // Pictures are stored in one of the following ways: @@ -98,7 +107,7 @@ function proxy_init(App $a) { $url = substr($url, 0, $pos + 1); } - $url = str_replace(array('.jpg', '.jpeg', '.gif', '.png'), array('','','',''), $url); + $url = str_replace(['.jpg', '.jpeg', '.gif', '.png'], ['','','',''], $url); $url = base64_decode(strtr($url, '-_', '+/'), true); @@ -125,9 +134,9 @@ function proxy_init(App $a) { // reduce quality - if it isn't a GIF if ($mime != 'image/gif') { - $img = new Photo($img_str, $mime); - if ($img->is_valid()) { - $img_str = $img->imageString(); + $Image = new Image($img_str, $mime); + if ($Image->isValid()) { + $img_str = $Image->asString(); } } @@ -139,24 +148,23 @@ function proxy_init(App $a) { } $valid = true; - $r = array(); - + $photo = null; if (!$direct_cache && ($cachefile == '')) { - $r = qu("SELECT * FROM `photo` WHERE `resource-id` = '%s' LIMIT 1", $urlhash); - if (dbm::is_result($r)) { - $img_str = $r[0]['data']; - $mime = $r[0]['desc']; + $photo = dba::selectFirst('photo', ['data', 'desc'], ['resource-id' => $urlhash]); + if (DBM::is_result($photo)) { + $img_str = $photo['data']; + $mime = $photo['desc']; if ($mime == '') { $mime = 'image/jpeg'; } } } - if (!dbm::is_result($r)) { + if (!DBM::is_result($photo)) { // It shouldn't happen but it does - spaces in URL $_REQUEST['url'] = str_replace(' ', '+', $_REQUEST['url']); $redirects = 0; - $img_str = fetch_url($_REQUEST['url'], true, $redirects, 10); + $img_str = Network::fetchUrl($_REQUEST['url'], true, $redirects, 10); $tempfile = tempnam(get_temppath(), 'cache'); file_put_contents($tempfile, $img_str); @@ -169,10 +177,10 @@ function proxy_init(App $a) { $mime = 'image/png'; $cachefile = ''; // Clear the cachefile so that the dummy isn't stored $valid = false; - $img = new Photo($img_str, 'image/png'); - if ($img->is_valid()) { - $img->scaleImage(10); - $img_str = $img->imageString(); + $Image = new Image($img_str, 'image/png'); + if ($Image->isValid()) { + $Image->scaleDown(10); + $img_str = $Image->asString(); } } elseif ($mime != 'image/jpeg' && !$direct_cache && $cachefile == '') { $image = @imagecreatefromstring($img_str); @@ -181,27 +189,15 @@ function proxy_init(App $a) { die(); } - q("INSERT INTO `photo` - ( `uid`, `contact-id`, `guid`, `resource-id`, `created`, `edited`, `filename`, `album`, `height`, `width`, `desc`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` ) - VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', %d, %d, '%s', '%s', '%s', '%s' )", - 0, 0, get_guid(), dbesc($urlhash), - dbesc(datetime_convert()), - dbesc(datetime_convert()), - dbesc(basename(dbesc($_REQUEST['url']))), - dbesc(''), - intval(imagesy($image)), - intval(imagesx($image)), - $mime, - dbesc($img_str), - 100, - intval(0), - dbesc(''), dbesc(''), dbesc(''), dbesc('') - ); - + $fields = ['uid' => 0, 'contact-id' => 0, 'guid' => System::createGUID(), 'resource-id' => $urlhash, 'created' => DateTimeFormat::utcNow(), 'edited' => DateTimeFormat::utcNow(), + 'filename' => basename($_REQUEST['url']), 'type' => '', 'album' => '', 'height' => imagesy($image), 'width' => imagesx($image), + 'datasize' => 0, 'data' => $img_str, 'scale' => 100, 'profile' => 0, + 'allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '', 'desc' => $mime]; + dba::insert('photo', $fields); } else { - $img = new Photo($img_str, $mime); - if ($img->is_valid() && !$direct_cache && ($cachefile == '')) { - $img->store(0, 0, $urlhash, $_REQUEST['url'], '', 100); + $Image = new Image($img_str, $mime); + if ($Image->isValid() && !$direct_cache && ($cachefile == '')) { + Photo::store($Image, 0, 0, $urlhash, $_REQUEST['url'], '', 100); } } } @@ -210,10 +206,10 @@ function proxy_init(App $a) { // reduce quality - if it isn't a GIF if ($mime != 'image/gif') { - $img = new Photo($img_str, $mime); - if ($img->is_valid()) { - $img->scaleImage($size); - $img_str = $img->imageString(); + $Image = new Image($img_str, $mime); + if ($Image->isValid()) { + $Image->scaleDown($size); + $img_str = $Image->asString(); } } @@ -266,11 +262,11 @@ function proxy_url($url, $writemode = false, $size = '') { // Only continue if it isn't a local image and the isn't deactivated if (proxy_is_local_image($url)) { - $url = str_replace(normalise_link(App::get_baseurl()) . '/', App::get_baseurl() . '/', $url); + $url = str_replace(normalise_link(System::baseUrl()) . '/', System::baseUrl() . '/', $url); return $url; } - if (get_config('system', 'proxy_disabled')) { + if (Config::get('system', 'proxy_disabled')) { return $url; } @@ -293,13 +289,13 @@ function proxy_url($url, $writemode = false, $size = '') { // Extract the URL extension $extension = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION); - $extensions = array('jpg', 'jpeg', 'gif', 'png'); + $extensions = ['jpg', 'jpeg', 'gif', 'png']; if (in_array($extension, $extensions)) { $shortpath .= '.' . $extension; $longpath .= '.' . $extension; } - $proxypath = App::get_baseurl() . '/proxy/' . $longpath; + $proxypath = System::baseUrl() . '/proxy/' . $longpath; if ($size != '') { $size = ':' . $size; @@ -310,7 +306,7 @@ function proxy_url($url, $writemode = false, $size = '') { if ((strlen($proxypath) > 250) && $writemode) { return $shortpath; } elseif (strlen($proxypath) > 250) { - return App::get_baseurl() . '/proxy/' . $shortpath . '?url=' . urlencode($url); + return System::baseUrl() . '/proxy/' . $shortpath . '?url=' . urlencode($url); } elseif ($writemode) { return $longpath; } else { @@ -332,7 +328,7 @@ function proxy_is_local_image($url) { } // links normalised - bug #431 - $baseurl = normalise_link(App::get_baseurl()); + $baseurl = normalise_link(System::baseUrl()); $url = normalise_link($url); return (substr($url, 0, strlen($baseurl)) == $baseurl); } @@ -347,7 +343,7 @@ function proxy_parse_query($url) { $query = parse_url($url, PHP_URL_QUERY); $query = html_entity_decode($query); $query_list = explode('&', $query); - $arr = array(); + $arr = []; foreach ($query_list as $key_value) { $key_value_list = explode('=', $key_value); @@ -374,7 +370,7 @@ function proxy_img_cb($matches) { } function proxy_parse_html($html) { - $html = str_replace(normalise_link(App::get_baseurl()) . '/', App::get_baseurl() . '/', $html); + $html = str_replace(normalise_link(System::baseUrl()) . '/', System::baseUrl() . '/', $html); return preg_replace_callback('/(]*src *= *["\'])([^"\']+)(["\'][^>]*>)/siU', 'proxy_img_cb', $html); }