file($tmp_name, FILEINFO_MIME_TYPE); $safe_img_mime = array( "image/gif", "image/jpeg", "image/png", "image/svg+xml", "image/webp", "image/bmp", "image/x-icon", "image/tiff" ); if(in_array($tmp_ext,$safe_img_mime)){ return $tmp_ext; }else{ return false; } } //ファイル形式チェック(画像かどうか) function check_mime_video($tmp_name){ $finfo = new finfo(); $tmp_ext = $finfo->file($tmp_name, FILEINFO_MIME_TYPE); $safe_vid_mime = array( "video/mpeg", "video/mp4", "video/webm", "video/x-msvideo", ); if(in_array($tmp_ext,$safe_vid_mime)){ return $tmp_ext; }else{ return false; } } //ファイル形式チェック(Base64の場合) function base64_mime($Base64,$userid){ $Base64 = base64_decode($Base64); $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime_type = finfo_buffer($finfo, $Base64); $safe_img_mime = [ "image/gif" => 'gif', "image/jpeg" => 'jpg', "image/png" => 'png', "image/svg+xml" => 'svg', "image/webp" => 'webp', "image/bmp" => 'bmp', "image/x-icon" => 'ico', "image/tiff" => 'tiff' ]; if(isset($safe_img_mime[$mime_type])){ $extension = $safe_img_mime[$mime_type]; $temp_file = tempnam(sys_get_temp_dir(), 'img'); file_put_contents($temp_file, $Base64); delete_exif($extension, $temp_file); $newFilename = uniqid() . '-' . $userid . '.' . $extension; $uploadedPath = '../ueuseimages/' . $newFilename; $result = copy($temp_file, "../".$uploadedPath); if($result){ return $uploadedPath; } else { return false; } } else { return false; } } function resizeImage($filePath, $maxWidth, $maxHeight) { if (file_exists($filePath)) { // 元の画像タイプを取得 $imageType = check_mime($filePath); // 画像タイプに応じてリソースを作成 if($imageType == "image/jpeg"){ $originalImage = imagecreatefromjpeg($filePath); } elseif($imageType == "image/png") { $originalImage = imagecreatefrompng($filePath); } elseif($imageType == "image/webp") { $originalImage = imagecreatefromwebp($filePath); } elseif($imageType == "image/bmp") { $originalImage = imagecreatefrombmp($filePath); } else { return; } // 元の画像のサイズを取得 list($originalWidth, $originalHeight) = getimagesize($filePath); // 縦横比を計算 $aspectRatio = $originalWidth / $originalHeight; // 新しいサイズを計算 if ($maxWidth / $maxHeight > $aspectRatio) { $newWidth = $maxHeight * $aspectRatio; $newHeight = $maxHeight; } else { $newWidth = $maxWidth; $newHeight = $maxWidth / $aspectRatio; } // 新しい画像リソースを作成 $resizedImage = imagecreatetruecolor($newWidth, $newHeight); // 画像をリサイズ imagecopyresampled($resizedImage, $originalImage, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight); // リサイズされた画像を表示 imagewebp($resizedImage, $filePath); // メモリの解放 imagedestroy($originalImage); imagedestroy($resizedImage); } } //文字装飾・URL変換など function processMarkdownAndWrapEmptyLines($markdownText) { $placeholders = []; // インラインコードをプレースホルダーに置き換える $markdownText = preg_replace_callback('/`([^`\n]+)`/', function($matches) use (&$placeholders) { $placeholder = 'PLACEHOLDER_' . count($placeholders); $placeholders[$placeholder] = '' . $matches[1] . ''; return $placeholder; }, $markdownText); // ここから先の処理はインラインコードとコードブロックに影響しない $markdownText = preg_replace('/\[\[buruburu (.+)\]\]/m', '$1', $markdownText);//ぶるぶる $markdownText = preg_replace_callback('/\[\[time (\d+)\]\]/m', function($matches) { $timestamp = $matches[1]; return '' . date("Y/m/d H:i", htmlentities($timestamp, ENT_QUOTES, 'UTF-8', false)) . ''; }, $markdownText); //太字&斜体------------------------------------------------------------------------ $markdownText = preg_replace('/\*\*\*(.+)\*\*\*(?=\s)/', '$1', $markdownText);//太字&斜体の全部のせセット $markdownText = preg_replace('/\*\*\*(.+)\*\*\*/', '$1', $markdownText);//太字&斜体の全部のせセット $markdownText = preg_replace('/\_\_\_(.+)\_\_\_(?=\s)/', '$1', $markdownText);//太字&斜体の全部のせセット $markdownText = preg_replace('/\b\_\_\_(.+)\_\_\_\b/', '$1', $markdownText);//太字&斜体の全部のせセット //太字----------------------------------------------------------------------------- $markdownText = preg_replace('/\*\*(.+)\*\*/', '$1', $markdownText);//太字 $markdownText = preg_replace('/\b\*\*(.+)\*\*\b/', '$1', $markdownText);//太字 $markdownText = preg_replace('/\_\_(.+)\_\_(?=\s)/', '$1', $markdownText);//太字 $markdownText = preg_replace('/\b\_\_(.+)\_\_\b/', '$1', $markdownText);//太字 //斜体----------------------------------------------------------------------------- $markdownText = preg_replace('/\*(.+)\*/', '$1', $markdownText);//斜体 $markdownText = preg_replace('/\b\*(.+)\*\b/', '$1', $markdownText);//斜体 $markdownText = preg_replace('/\_(.+)\_(?=\s)/', '$1', $markdownText);//斜体 $markdownText = preg_replace('/\b\_(.+)\_\b/', '$1', $markdownText);//斜体 $markdownText = preg_replace('/\~\~(.+)\~\~/m', '$1', $markdownText);//打ち消し線 $markdownText = preg_replace('/>>> (.+)/m', '$1', $markdownText);//>>> 引用 $markdownText = preg_replace('/\|\|(.+)\|\|/m', '$1', $markdownText);//黒塗り // タイトル(#、##、###)をHTMLのhタグに変換 $markdownText = preg_replace('/^# (.+)/m', '

$1

', $markdownText); $markdownText = preg_replace('/^## (.+)/m', '

$1

', $markdownText); $markdownText = preg_replace('/^### (.+)/m', '

$1

', $markdownText); // 箇条書き(-)をHTMLのul/liタグに変換 $markdownText = preg_replace('/^- (.+)/m', '

・ $1

', $markdownText); // 空行の前に何もない行をHTMLのpタグに変換 $markdownText = preg_replace('/(^\s*)(?!\s)(.*)/m', '$1

$2

', $markdownText); // プレースホルダーを元のコードに戻す foreach ($placeholders as $placeholder => $original) { $markdownText = str_replace($placeholder, $original, $markdownText); } return $markdownText; } //Profile function replaceProfileEmojiImages($postText) { $postText = str_replace(''', '\'', $postText); // プロフィール名で絵文字名(:emoji:)を検出して画像に置き換える $emojiPattern = '/:(\w+):/'; $postTextWithImages = preg_replace_callback($emojiPattern, function($matches) { $emojiName = $matches[1]; //絵文字path取得 $dbh = new PDO('mysql:charset=utf8mb4;dbname='.DB_NAME.';host='.DB_HOST, DB_USER, DB_PASS, array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true, )); $emoji_Query = $dbh->prepare("SELECT emojifile, emojiname FROM emoji WHERE emojiname = :emojiname"); $emoji_Query->bindValue(':emojiname', $emojiName); $emoji_Query->execute(); $emoji_row = $emoji_Query->fetch(); if(empty($emoji_row["emojifile"])){ $emoji_path = "img/sysimage/errorimage/emoji_404.png"; }else{ $emoji_path = $emoji_row["emojifile"]; } return ":$emojiName:"; }, $postText); return $postTextWithImages; } // ユーズ内の絵文字やhashtagを画像に置き換える function replaceEmojisWithImages($postText) { $postText = str_replace(''', '\'', $postText); // ユーズ内で絵文字名(:emoji:)を検出して画像に置き換える $emojiPattern = '/:(\w+):/'; $postTextWithImages = preg_replace_callback($emojiPattern, function($matches) { $emojiName = $matches[1]; //絵文字path取得 $dbh = new PDO('mysql:charset=utf8mb4;dbname='.DB_NAME.';host='.DB_HOST, DB_USER, DB_PASS, array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true, )); $emoji_Query = $dbh->prepare("SELECT emojifile, emojiname FROM emoji WHERE emojiname = :emojiname"); $emoji_Query->bindValue(':emojiname', $emojiName); $emoji_Query->execute(); $emoji_row = $emoji_Query->fetch(); if(empty($emoji_row["emojifile"])){ $emoji_path = "img/sysimage/errorimage/emoji_404.png"; return ":".$emojiName.":"; }else{ $emoji_path = $emoji_row["emojifile"]; return ":$emojiName:"; } }, $postText); // @username を検出してリンクに置き換える $usernamePattern = '/@(\w+)/'; $postTextWithImagesAndUsernames = preg_replace_callback($usernamePattern, function($matches) { $username = $matches[1]; $dbh = new PDO('mysql:charset=utf8mb4;dbname='.DB_NAME.';host='.DB_HOST, DB_USER, DB_PASS, array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true, )); $mentionsuserQuery = $dbh->prepare("SELECT username, userid FROM account WHERE userid = :userid"); $mentionsuserQuery->bindValue(':userid', $username); $mentionsuserQuery->execute(); $mentionsuserData = $mentionsuserQuery->fetch(); if(empty($mentionsuserData)){ return "@$username"; }else{ return "@".replaceProfileEmojiImages(htmlentities($mentionsuserData["username"], ENT_QUOTES, 'UTF-8', false)).""; } }, $postTextWithImages); $hashtagsPattern = '/#([\p{Han}\p{Hiragana}\p{Katakana}A-Za-z0-9ー_!]+)/u'; $postTextWithHashtags = preg_replace_callback($hashtagsPattern, function($matches) { $hashtags = $matches[1]; return "" . '#' . $hashtags . ""; }, $postTextWithImagesAndUsernames); return $postTextWithHashtags; } function replaceURLsWithLinks($postText, $maxLength = 48) { $pattern = '/(https:\/\/[\w!?\/+\-_~;.,*&@#$%()+|https:\/\/[ぁ-んァ-ヶ一-龠々\w\-\/?=&%.]+)/'; $convertedText = preg_replace_callback($pattern, function($matches) use ($maxLength) { $link = $matches[0]; if(!(preg_match('/:(\w+):/',$link))){ $no_https_link = str_replace("https://", "", $link); if (mb_strlen($link) > $maxLength) { $truncatedLink = mb_substr($no_https_link, 0, $maxLength).'...'; return ''.$truncatedLink.''; } else { return ''.$no_https_link.''; } }else{ return $link; } }, $postText); return $convertedText; } function YouTube_and_nicovideo_Links($postText) { // URLを正規表現を使って検出 $pattern = '/(https:\/\/[^\s<>\[\]\'"]+)/'; // 改良された正規表現 preg_match_all($pattern, $postText, $matches); if(empty($url)){ $postText = ""; } // 検出したURLごとに処理を行う foreach ($matches[0] as $url) { // ドメイン部分を抽出 $parsedUrl = parse_url($url); if(!(empty($parsedUrl['host']))){ $video_time = "0"; $video_id = ""; if($parsedUrl['host'] == "youtube.com" || $parsedUrl['host'] == "youtu.be" || $parsedUrl['host'] == "www.youtube.com" || $parsedUrl['host'] == "m.youtube.com"){ if (isset($parsedUrl['query'])) { // クエリ部分を連想配列に変換する parse_str($parsedUrl['query'], $queryParams); // video_idの取得 if (isset($queryParams['v'])) { $video_id = safetext($queryParams['v']); $iframe = true; } else { $video_id = str_replace('/', '', safetext($parsedUrl['path'])); $iframe = true; } // video_timeの取得 if (isset($queryParams['amp;t'])) { $video_time = safetext($queryParams['amp;t']); if(!(is_numeric($video_time))){ $video_time = "0"; } } else { $video_time = "0"; } $video_id = str_replace('&', '?', $video_id); } elseif (isset($parsedUrl['path'])) { $video_id = str_replace('/', '', safetext($parsedUrl['path'])); $video_time = "0"; $iframe = true; } else { $video_id = ""; $video_time = "0"; $iframe = false; } // 不要な文字を削除してaタグを生成 if ($iframe) { $link = ''; } else { $link = ""; } // URLをドメインのみを表示するaタグで置き換え $postText = $link; }elseif($parsedUrl['host'] == "nicovideo.jp" || $parsedUrl['host'] == "www.nicovideo.jp"){ if(isset($parsedUrl['path'])){ $video_id = str_replace('/watch/', '', safetext($parsedUrl['path'])); $iframe = true; }else{ $video_id = ""; $iframe = false; } if (isset($parsedUrl['query'])) { // クエリ部分を連想配列に変換する parse_str($parsedUrl['query'], $queryParams); // video_timeの取得 if (isset($queryParams['from'])) { $video_time = safetext($queryParams['from']); if(!(is_numeric($video_time))){ $video_time = "0"; } } else { $video_time = "0"; } } // 不要な文字を削除してaタグを生成 if($iframe == true){ $link = '