157 lines
5.6 KiB
PHP
157 lines
5.6 KiB
PHP
|
|
<?php
|
|
defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
class DiagGoogle extends CI_Controller
|
|
{
|
|
public function ping() { echo "OK"; }
|
|
// DiagGoogle.php
|
|
public function config()
|
|
{
|
|
$cfg = $this->config->item('google');
|
|
header('Content-Type: application/json');
|
|
echo json_encode($cfg, JSON_PRETTY_PRINT);
|
|
}
|
|
|
|
public function sa_file()
|
|
{
|
|
// on récupère la config (elle est autoloadée maintenant)
|
|
$cfg = $this->config->item('google');
|
|
$path = $cfg['credentials_path'] ?? '';
|
|
|
|
$out = [
|
|
'credentials_path' => $path,
|
|
'exists' => file_exists($path),
|
|
'readable' => is_readable($path),
|
|
'client_email' => null,
|
|
];
|
|
|
|
if (is_readable($path)) {
|
|
$j = json_decode(@file_get_contents($path), true);
|
|
$out['client_email'] = $j['client_email'] ?? null;
|
|
}
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
echo json_encode($out, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
|
}
|
|
|
|
public function token_drive_meta()
|
|
{
|
|
try {
|
|
$this->load->library('GoogleServiceJWT');
|
|
// Scope minimal pour tester: lecture des métadonnées Drive
|
|
$scope = 'https://www.googleapis.com/auth/drive.metadata.readonly';
|
|
$token = $this->googleservicejwt->getAccessToken($scope);
|
|
echo $token ? "TOKEN_OK" : "TOKEN_FAIL";
|
|
} catch (Throwable $e) {
|
|
header('Content-Type: text/plain; charset=utf-8');
|
|
echo "ERR: ".$e->getMessage();
|
|
}
|
|
}
|
|
|
|
public function drive_access()
|
|
{
|
|
try {
|
|
$cfg = $this->config->item('google');
|
|
$driveId = $cfg['shared_drive_id'] ?? null;
|
|
if (!$driveId) { echo "No shared_drive_id in config"; return; }
|
|
|
|
$this->load->library('GoogleServiceJWT');
|
|
|
|
// 🔧 Scope corrigé :
|
|
$token = $this->googleservicejwt->getAccessToken('https://www.googleapis.com/auth/drive.readonly');
|
|
|
|
$url = 'https://www.googleapis.com/drive/v3/drives/'.rawurlencode($driveId);
|
|
$ch = curl_init($url);
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_HTTPHEADER => ['Authorization: Bearer '.$token],
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => 20,
|
|
]);
|
|
$raw = curl_exec($ch);
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
if ($code === 200) {
|
|
echo $raw; // OK
|
|
} else {
|
|
echo json_encode(['code'=>$code, 'error'=>json_decode($raw, true)], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
|
|
}
|
|
} catch (Throwable $e) {
|
|
header('Content-Type: text/plain; charset=utf-8');
|
|
echo "ERR: ".$e->getMessage();
|
|
}
|
|
}
|
|
|
|
public function drive_list()
|
|
{
|
|
try {
|
|
$this->load->library('GoogleDriveDocs');
|
|
$res = $this->googledrivedocs->listSharedDriveFolders(20); // 20 pour tester
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
echo json_encode($res, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
|
} catch (Throwable $e) {
|
|
header('Content-Type: text/plain; charset=utf-8');
|
|
echo 'ERR: '.$e->getMessage();
|
|
}
|
|
}
|
|
public function create_doc_test($folderId = null)
|
|
{
|
|
try {
|
|
if (!$folderId) { echo "Fournis /DiagGoogle/create_doc_test/{folderId}"; return; }
|
|
|
|
// On utilise notre librairie sans Composer
|
|
$this->load->library('GoogleDriveDocs');
|
|
|
|
$res = $this->googledrivedocs->createGoogleDocInFolder(
|
|
$folderId,
|
|
'TEST CI ' . date('Y-m-d H:i:s'),
|
|
"Bonjour de MS1!\nCeci est un test automatique."
|
|
);
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
echo json_encode($res, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
|
} catch (Throwable $e) {
|
|
header('Content-Type: text/plain; charset=utf-8');
|
|
echo 'ERR: '.$e->getMessage();
|
|
}
|
|
}
|
|
public function cal_list()
|
|
{
|
|
try {
|
|
$this->load->library('GoogleServiceJWT');
|
|
|
|
// Optionnel: /DiagGoogle/cal_list?as=utilisateur@tondomaine.com
|
|
$as = $this->input->get('as'); // impersonation si Domain-wide Delegation activée
|
|
$token = $this->googleservicejwt->getAccessToken(
|
|
'https://www.googleapis.com/auth/calendar.readonly',
|
|
$as ?: null
|
|
);
|
|
|
|
$url = 'https://www.googleapis.com/calendar/v3/users/me/calendarList?maxResults=50';
|
|
$ch = curl_init($url);
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_HTTPHEADER => ['Authorization: Bearer '.$token],
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => 20,
|
|
]);
|
|
$raw = curl_exec($ch);
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
if ($code === 200) {
|
|
echo $raw; // doit contenir "items": [ {id, summary, ...}, ... ]
|
|
} else {
|
|
echo json_encode(['code'=>$code, 'error'=>json_decode($raw, true)], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
|
|
}
|
|
} catch (Throwable $e) {
|
|
header('Content-Type: text/plain; charset=utf-8'); echo 'ERR: '.$e->getMessage();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|