<commit_before><?php
/**
 * @copyright Copyright (C) 2010-2021, the Friendica project
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

namespace Friendica\Model\Post;

use Friendica\Core\Logger;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Util\Proxy;

/**
 * Class Link
 *
 * This Model class handles post related external links
 */
class Link
{
	public static function getByLink(int $uri_id, string $url, $size = '')
	{
		if (empty($url) || Proxy::isLocalImage($url)) {
			return $url;
		}

		$link = DBA::selectFirst('post-link', ['id'], ['uri-id' => $uri_id, 'url' => $url]);
		if (!empty($link['id'])) {
			$id = $link['id'];
			Logger::info('Found', ['id' => $id, 'url' => $url]);
		} else {
			$mime = self::fetchMimeType($url);

			DBA::insert('post-link', ['uri-id' => $uri_id, 'url' => $url, 'mimetype' => $mime]);
			$id = DBA::lastInsertId();
			Logger::info('Inserted', ['id' => $id, 'url' => $url]);
		}

		if (empty($id)) {
			return $url;
		}

		$url = DI::baseUrl() . '/photo/link/';
		switch ($size) {
			case Proxy::SIZE_MICRO:
				$url .= Proxy::PIXEL_MICRO . '/';
				break;
			case Proxy::SIZE_THUMB:
				$url .= Proxy::PIXEL_THUMB . '/';
				break;
			case Proxy::SIZE_SMALL:
				$url .= Proxy::PIXEL_SMALL . '/';
				break;
			case Proxy::SIZE_MEDIUM:
				$url .= Proxy::PIXEL_MEDIUM . '/';
				break;
			case Proxy::SIZE_LARGE:
				$url .= Proxy::PIXEL_LARGE . '/';
				break;
		}
		return $url . $id;
	}

	private static function fetchMimeType(string $url)
	{
		$timeout = DI::config()->get('system', 'xrd_timeout');

		$curlResult = DI::httpRequest()->head($url, ['timeout' => $timeout]);
		if ($curlResult->isSuccess()) {
			if (empty($media['mimetype'])) {
				return $curlResult->getHeader('Content-Type');
			}
		}
		return '';
	}

	/**
	 * Add external links and replace them in the body
	 *
	 * @param integer $uriid
	 * @param string $body
	 * @return string Body with replaced links
	 */
	public static function insertFromBody(int $uriid, string $body)
	{
		if (preg_match_all("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", $body, $pictures, PREG_SET_ORDER)) {
			foreach ($pictures as $picture) {
				$body = str_replace($picture[3], self::getByLink($uriid, $picture[3]), $body);
			}
		}

		if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
			foreach ($pictures as $picture) {
				if (parse_url($picture[1], PHP_URL_SCHEME)) {
					$body = str_replace($picture[1], self::getByLink($uriid, $picture[1]), $body);
				}
			}
		}

		if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/ism", $body, $pictures, PREG_SET_ORDER)) {
			foreach ($pictures as $picture) {
				$body = str_replace($picture[1], self::getByLink($uriid, $picture[1]), $body);
			}
		}

		return trim($body);
	}
}
<commit_msg>Quit on empty uri-id<commit_after><?php
/**
 * @copyright Copyright (C) 2010-2021, the Friendica project
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

namespace Friendica\Model\Post;

use Friendica\Core\Logger;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Util\Proxy;

/**
 * Class Link
 *
 * This Model class handles post related external links
 */
class Link
{
	public static function getByLink(int $uri_id, string $url, $size = '')
	{
		if (empty($uri_id) || empty($url) || Proxy::isLocalImage($url)) {
			return $url;
		}

		$link = DBA::selectFirst('post-link', ['id'], ['uri-id' => $uri_id, 'url' => $url]);
		if (!empty($link['id'])) {
			$id = $link['id'];
			Logger::info('Found', ['id' => $id, 'url' => $url]);
		} else {
			$mime = self::fetchMimeType($url);

			DBA::insert('post-link', ['uri-id' => $uri_id, 'url' => $url, 'mimetype' => $mime]);
			$id = DBA::lastInsertId();
			Logger::info('Inserted', ['id' => $id, 'url' => $url]);
		}

		if (empty($id)) {
			return $url;
		}

		$url = DI::baseUrl() . '/photo/link/';
		switch ($size) {
			case Proxy::SIZE_MICRO:
				$url .= Proxy::PIXEL_MICRO . '/';
				break;
			case Proxy::SIZE_THUMB:
				$url .= Proxy::PIXEL_THUMB . '/';
				break;
			case Proxy::SIZE_SMALL:
				$url .= Proxy::PIXEL_SMALL . '/';
				break;
			case Proxy::SIZE_MEDIUM:
				$url .= Proxy::PIXEL_MEDIUM . '/';
				break;
			case Proxy::SIZE_LARGE:
				$url .= Proxy::PIXEL_LARGE . '/';
				break;
		}
		return $url . $id;
	}

	private static function fetchMimeType(string $url)
	{
		$timeout = DI::config()->get('system', 'xrd_timeout');

		$curlResult = DI::httpRequest()->head($url, ['timeout' => $timeout]);
		if ($curlResult->isSuccess()) {
			if (empty($media['mimetype'])) {
				return $curlResult->getHeader('Content-Type');
			}
		}
		return '';
	}

	/**
	 * Add external links and replace them in the body
	 *
	 * @param integer $uriid
	 * @param string $body
	 * @return string Body with replaced links
	 */
	public static function insertFromBody(int $uriid, string $body)
	{
		if (preg_match_all("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", $body, $pictures, PREG_SET_ORDER)) {
			foreach ($pictures as $picture) {
				$body = str_replace($picture[3], self::getByLink($uriid, $picture[3]), $body);
			}
		}

		if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
			foreach ($pictures as $picture) {
				if (parse_url($picture[1], PHP_URL_SCHEME)) {
					$body = str_replace($picture[1], self::getByLink($uriid, $picture[1]), $body);
				}
			}
		}

		if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/ism", $body, $pictures, PREG_SET_ORDER)) {
			foreach ($pictures as $picture) {
				$body = str_replace($picture[1], self::getByLink($uriid, $picture[1]), $body);
			}
		}

		return trim($body);
	}
}

{'fraction_non_alphanumeric': 0.18949477601259482, 'fraction_numerical': 0.0060111635895234005, 'mean_word_length': 3.6247518199867637, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7284896', 'n_tokens_mistral': 2772, 'n_tokens_neox': 2453, 'n_words': 640}