init
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
include_once 'lib/ghl/oauth2.php';
|
||||
include_once 'config.php';
|
||||
include_once 'project-model.php';
|
||||
|
||||
|
||||
$config = MkdConfig::get_instance()->get_config();
|
||||
|
||||
$oauth = new GHLOAuth2($config);
|
||||
|
||||
|
||||
Route::add('/ghl/authorize/([a-zA-Z0-9]+)', function ($id) use ($oauth) {
|
||||
check_login();
|
||||
|
||||
$url = $oauth->getAuthorizationUrl();
|
||||
$redirect_uri = $_GET['redirect_uri'];
|
||||
$project = explode('p', $id)[0];
|
||||
$page = explode('p', $id)[1];
|
||||
$state = $project . '|' . $redirect_uri . '|' . $page;
|
||||
|
||||
header('Location: ' . $url . '&state=' . $state);
|
||||
exit;
|
||||
}, 'get');
|
||||
|
||||
|
||||
Route::add('/ghl/callback', function () use ($oauth) {
|
||||
check_login();
|
||||
|
||||
$code = $_GET['code'];
|
||||
$projectId = explode('|', $_GET['state'])[0] ?? 0;
|
||||
$redirect_uri = explode('|', $_GET['state'])[1] ?? '/admin/project';
|
||||
$page = explode('|', $_GET['state'])[2] ?? 1;
|
||||
|
||||
$token = $oauth->getAccessToken($code);
|
||||
if (!$token['success']) {
|
||||
header('Location: ' . $redirect_uri . '?oauth_error=' . $token['error'] . '&page=' . $page);
|
||||
exit;
|
||||
}
|
||||
|
||||
$projectModel = new ProjectModel();
|
||||
$data = [
|
||||
'access_token' => $token['data']['access_token'],
|
||||
'refresh_token' => $token['data']['refresh_token'],
|
||||
'token_expiry' => $token['data']['expires_in'],
|
||||
];
|
||||
$projectModel->edit($data, $projectId);
|
||||
|
||||
// $page = guessProjectPage($projectId);
|
||||
|
||||
|
||||
|
||||
header('Location: ' . $redirect_uri . '?oauth_success=true&page=' . $page);
|
||||
exit;
|
||||
}, 'get');
|
||||
|
||||
|
||||
|
||||
function guessProjectPage($projectId, $limit = 15) {
|
||||
$projectModel = new ProjectModel();
|
||||
|
||||
// Get the project to find its position
|
||||
$project = $projectModel->get($projectId);
|
||||
|
||||
if ($project) {
|
||||
// Use the same parameters as the listing page would use
|
||||
$parameters = [];
|
||||
|
||||
// Get all projects with higher IDs (which come before in DESC order)
|
||||
$parameters[] = "id > " . $projectId;
|
||||
$projectsBefore = $projectModel->count($parameters);
|
||||
|
||||
// Add 1 to account for current page
|
||||
$pageNumber = floor($projectsBefore / $limit) + 1;
|
||||
|
||||
return $pageNumber;
|
||||
}
|
||||
|
||||
return 1; // Default to first page if project not found
|
||||
}
|
||||
Reference in New Issue
Block a user