krgm.so-manager-dev.com/app/Models/Utils.php
ou.zaikou e1629913bd 初回コミット
Signed-off-by:  ou.zaikou<caihaoweng@gmail.com>
2025-08-21 23:09:40 +09:00

134 lines
4.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* ユーティリティクラス
* Laravel 5.7から引き継いだヘルパー機能
* Created by PhpStorm.
* User: mrpha
* Date: 5/31/2016
* Time: 1:49 PM
*/
namespace App\Models;
use File;
use Illuminate\Support\Facades\Hash;
/**
* 共通ユーティリティクラス
* ファイルアップロード、パスワード暗号化、CSV処理など
*/
class Utils
{
// ページあたりのアイテム数(旧システムから継承)
const item_per_page = 50;
// 画像保存パス(旧システムから継承)
const image_path = 'storage/images/';
/**
* 画像ファイルをimagesフォルダにアップロード
* Laravel 5.7から継承したメソッド
*
* @param \Illuminate\Http\UploadedFile $file アップロードファイル
* @param string|null $fileName ファイル名(指定しない場合は自動生成)
* @return string|false アップロード成功時はファイル名、失敗時はfalse
*/
public static function uploadFile($file, $fileName = null)
{
$destinationPath = self::getImagePath();
$date = strtotime(date('Y-m-d H:i:s'));
// ファイルをフォルダに移動
$fileName = $fileName . $date . '.' . $file->getClientOriginalExtension();
if ($file->move($destinationPath, $fileName)) {
$filePath = $destinationPath.$fileName;
if (file_exists($filePath)){
chmod($filePath, 0755); // ファイル権限設定
}
return $fileName;
}
return false;
}
/**
* 画像パスを取得
*
* @param string $filname ファイル名
* @return string 画像パス
*/
public static function getImagePath($filname = '')
{
$path = self::image_path;
if ($filname) $path .= $filname;
return $path;
}
/**
* 画像URLを取得
*
* @param string $filname ファイル名
* @return string 画像URL
*/
public static function getImageUrl($filname = '')
{
return url(self::getImagePath($filname));
}
/**
* パスワードのハッシュ化
* Laravel 12変更点Hashファサードを使用してハッシュ化
* Laravel 5.7: 独自のソルト処理を実装していた
*
* @param string $pw 平文パスワード
* @param string $seq ユーザーシーケンス(ソルト用)
* @return string ハッシュ化されたパスワード
*/
public static function getHashPassword($pw, $seq)
{
// 旧システムと同じソルト形式を維持
$salt = $seq.'SOMSALT';
// Laravel 12: Argon2ハッシュアルゴリズムを使用
return Hash::make($pw, [
'memory' => 1024,
'time' => 25,
'threads' => 2,
'salt' => $salt
]);
}
/**
* CSVファイルを配列に変換
* Laravel 5.7から継承したメソッド(データインポート用)
*
* @param string $filename CSVファイルパス
* @param string $delimiter 区切り文字(デフォルト:カンマ)
* @return array|false 変換された配列、またはfalse失敗時
*/
public static function csvToArray($filename = '', $delimiter = ',')
{
// ファイル存在確認
if (!file_exists($filename) || !is_readable($filename))
return false;
$header = null;
$data = array();
// CSVファイルを読み込み
if (($handle = fopen($filename, 'r')) !== false)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== false)
{
if (!$header)
$header = $row; // 最初の行はヘッダーとして扱う
else
$data[] = $row; // データ行として配列に追加
}
fclose($handle);
}
return $data;
}
}