<?php
|
|
|
|
use Google\Auth\Credentials\UserRefreshCredentials;
|
|
use Google\Auth\HttpHandler\Guzzle7HttpHandler;
|
|
use Google\Auth\OAuth2;
|
|
use GuzzleHttp\Client;
|
|
|
|
function connectWithGooglePhotos($request, array $scopes, $redirectURI) {
|
|
$clientSecretJson = json_decode(
|
|
file_get_contents('client_secret_1000190146810-j9mohjt8m4m8j8sj6hupl1bghp6o1pdn.apps.googleusercontent.com.json'),
|
|
true
|
|
)['web'];
|
|
// dump($clientSecretJson);exit;
|
|
$clientId = $clientSecretJson['client_id'];
|
|
$clientSecret = $clientSecretJson['client_secret'];
|
|
|
|
$oauth2 = new OAuth2([
|
|
'clientId' => $clientId,
|
|
'clientSecret' => $clientSecret,
|
|
'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth',
|
|
// Where to return the user to if they accept your request to access their account.
|
|
// You must authorize this URI in the Google API Console.
|
|
'redirectUri' => $redirectURI,
|
|
'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',
|
|
'scope' => $scopes,
|
|
'expiry' => 7200,
|
|
]);
|
|
|
|
$client = new Client(['proxy' => 'http://127.0.0.1:1087']);
|
|
$httpHandler = new Guzzle7HttpHandler($client);
|
|
|
|
// The authorization URI will, upon redirecting, return a parameter called code.
|
|
$code = $request->get('code');
|
|
|
|
if ($code == null || $code == "") {
|
|
$authenticationUrl = $oauth2->buildFullAuthorizationUri(['access_type' => 'offline']);
|
|
// dump($authenticationUrl);
|
|
// redirect($authenticationUrl);
|
|
// header("Location: " . $authenticationUrl);
|
|
// redirect()->to($authenticationUrl);
|
|
return redirect($authenticationUrl);
|
|
} else {
|
|
dump("code is " . $code);
|
|
// With the code returned by the OAuth flow, we can retrieve the refresh token.
|
|
$oauth2->setCode($code);
|
|
$authToken = $oauth2->fetchAuthToken();
|
|
$refreshToken = $authToken['access_token'];
|
|
|
|
// The UserRefreshCredentials will use the refresh token to 'refresh' the credentials when
|
|
// they expire.
|
|
$credentials = new UserRefreshCredentials(
|
|
$scopes,
|
|
[
|
|
'client_id' => $clientId,
|
|
'client_secret' => $clientSecret,
|
|
'refresh_token' => $refreshToken,
|
|
]
|
|
);
|
|
session(['credentials' => $credentials]);
|
|
|
|
// Return the user to the home page.
|
|
// dump("before redirect");
|
|
return redirect('google/photo/index');
|
|
// header("Location: index.php");
|
|
}
|
|
}
|
|
|
|
function iconv_gbk_to_uft8($string) {
|
|
|
|
if (!$string) {
|
|
return '';
|
|
}
|
|
|
|
$encode = mb_detect_encoding($string, array("ASCII", "GBK", "GB2312", 'BIG5', 'UTF-8'));
|
|
// dump($encode);
|
|
return iconv($encode, "UTF-8", $string);
|
|
|
|
}
|
|
|
|
function scanFilesWithoutPath($path): array {
|
|
$allFiles = [];
|
|
if (is_dir($path)) {
|
|
$files = scandir($path);
|
|
foreach ($files as $file) {
|
|
if ($file == "." || $file == "..") {
|
|
continue;
|
|
}
|
|
if (is_dir($path . DIRECTORY_SEPARATOR . $file)) {
|
|
$allFiles = array_merge($allFiles, scanFilesWithoutPath($path . DIRECTORY_SEPARATOR . $file));
|
|
}
|
|
if (is_file($path . DIRECTORY_SEPARATOR . $file)) {
|
|
$allFiles[] = $file;
|
|
}
|
|
}
|
|
}
|
|
return $allFiles;
|
|
}
|
|
|
|
/**
|
|
* 跳过一些不需要处理的默认文件和路径
|
|
* @param $file
|
|
* @return bool
|
|
*/
|
|
function skipDefaultPathAndFile($file): bool {
|
|
if ($file == "." || $file == ".." || $file == "config.json" || $file == "config.backup.json" || $file == ".DS_Store") {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function processFirstLevelDirectory($dir): bool {
|
|
if (!is_dir($dir)) {
|
|
return false;
|
|
}
|
|
// 遍历第一层文件夹
|
|
$list = scandir($dir);
|
|
foreach ($list as $file) {
|
|
if (skipDefaultPathAndFile($file)) {
|
|
continue;
|
|
}
|
|
$firstLevelDir = implode(DIRECTORY_SEPARATOR, [$dir, $file]);
|
|
if (is_dir($firstLevelDir)) {
|
|
// echo "process inner $firstLevelDir";
|
|
processInnerDirectory($firstLevelDir, $file);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function processInnerDirectory($dir, $lastLevelName) {
|
|
if (!is_dir($dir)) {
|
|
return false;
|
|
}
|
|
// 遍历内部文件
|
|
$list = scandir($dir);
|
|
foreach ($list as $file) {
|
|
if (skipDefaultPathAndFile($file)) {
|
|
continue;
|
|
}
|
|
$secondLevelFile = implode(DIRECTORY_SEPARATOR, [$dir, $file]);
|
|
// echo "second is $secondLevelFile";
|
|
if (is_file($secondLevelFile)) {
|
|
if (str_contains($file, $lastLevelName)) {
|
|
// echo "file has contains lastLevelName\n";
|
|
continue;
|
|
} else {
|
|
$newFileName = $dir . DIRECTORY_SEPARATOR . $lastLevelName . "_" . $file;
|
|
echo "old name is : " . $secondLevelFile . " \n new name is : " . $newFileName . "\n";
|
|
rename($secondLevelFile, $newFileName);
|
|
}
|
|
} else if ($file == "extrafanart") {
|
|
// $secondLevelFile ==> \A2H\EBOD-666\
|
|
// $extrafanartDir ==> \A2H\EBOD-666\extrafanart
|
|
$extrafanartDir = $secondLevelFile;
|
|
echo "enter extrafanart: " . $extrafanartDir . "\n";
|
|
if (is_dir($extrafanartDir)) {
|
|
$extrafanartFiles = scandir($extrafanartDir);
|
|
foreach ($extrafanartFiles as $extrafanartFile) {
|
|
if (skipDefaultPathAndFile($extrafanartFile)) {
|
|
continue;
|
|
}
|
|
$extrafanartEntireFile = implode(DIRECTORY_SEPARATOR, [$extrafanartDir, $extrafanartFile]);
|
|
if (is_file($extrafanartEntireFile)) {
|
|
if (str_contains($extrafanartFile, $lastLevelName)) {
|
|
continue;
|
|
} else {
|
|
// 把 extrafanart 目录去掉,让文件都在同一层
|
|
$newExtraFanArtEntireFile = $dir . DIRECTORY_SEPARATOR . $lastLevelName . "_" . $extrafanartFile;
|
|
echo "old name is : " . $extrafanartEntireFile . " \n new name is : " . $newExtraFanArtEntireFile . "\n";
|
|
rename($extrafanartEntireFile, $newExtraFanArtEntireFile);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|