Duffer Derek
<?php
namespace App\Repository\Utils;
use App\Repository\General\PodioConfig;
use App\Repository\General\Constants;
use App\Repository\General\DB;
use App\Repository\General\Log;
use PDO;
use Podio;
use PodioError;
use PodioOAuth;
use PodioUser;
use PodioUserStatus;
/**
* Created by IntelliJ IDEA.
* User: jis
* Date: 1/4/17
* Time: 11:57 AM
*/
class PodioAuthRepo
{
private $key1RateLimitGen = 1000;
private $key1RateLimit = 1000;
public function podioSetUpUsingKey1()
{
$key = PodioConfig::getKey2();
Podio::setup($key['client_id'], $key['client_secret']);
}
public function podioSetUpUsingKey2()
{
$key = PodioConfig::getKey2();
Podio::setup($key['client_id'], $key['client_secret']);
}
public function podioPasswordAuthenticate($key, $userID)
{
try {
$user = NULL;
switch ($userID) {
case '':
$user = PodioConfig::getCustomUser();
break;
case 4604600:
$user = PodioConfig::getPhasesUser();
break;
}
if ($user) {
Log::log("podioPasswordAuthenticate ", $user, "info");
Podio::authenticate('password', array(
'username' => $user['username'],
'password' => $user['password']
));
$user = PodioUserStatus::get();
Log::log("podioPasswordAuthenticate ", $user, "info");
$data = array(
'access_token' => Podio::$oauth->access_token,
'refresh_token' => Podio::$oauth->refresh_token,
'expire_time' => time() + Podio::$oauth->expires_in,
'start_time' => time(), // rate limit count - start time
'end_time' => time() + (60 * 60), // rate limit count - end time
'secret' => PodioConfig::$SECRET_KEY,
'user' => $userID,
'key' => $key
);
Log::log("podioPasswordAuthenticate ", $data, "info");
$this->saveUserAuth($data);
return true;
}
} catch (PodioError $e) {
Log::logError($e . "", null, "podioPasswordAuthenticate");
}
return false;
}
public function authenticate1($identifier)
{
try {
$sql = "SELECT `key`,`user` FROM transfer_process where id=:id";
$STH = DB::prepare($sql);
$STH->execute(array(
'id' => $identifier,
));
$result = $STH->fetchAll(PDO::FETCH_ASSOC);
if ($result && count($result) > 0) {
$transferProcess = $result[0];
if ($transferProcess['key'] == 1)
$this->podioSetUpUsingKey1();
else // else key will be 2
$this->podioSetUpUsingKey2();
$sql = "SELECT `id`, `expire_time` , `rate_limit_gen` ,`rate_limit` , `start_time` ,`end_time`,
AES_DECRYPT(`access_token`,:secret) as access_token,
AES_DECRYPT(`refresh_token`,:secret) as refresh_token
from `auth` where `key`=:keyVal and `user`=:userID";
$STH = DB::prepare($sql);
$STH->execute(array(
'secret' => PodioConfig::$SECRET_KEY,
'keyVal' => $transferProcess['key'],
'userID' => $transferProcess['user'],
));
$result = $STH->fetchAll(PDO::FETCH_ASSOC);
if (!$result || count($result) < 1) { // no values in DB
return $this->authenticateAndSaveTokens($transferProcess['key'], $transferProcess['user']);
} else {
$auth = $result[0];
if ($this->isExpired($auth)) {
return $this->authenticateAndSaveTokens($transferProcess['key'], $transferProcess['user']);
}
}
if ($result && count($result) > 0) {
$auth = $result[0];
return $this->authenticateUsingAccessToken($auth, $priority);
}
}
} catch (PodioError $e) {
Log::logError($e . "", null, "podioAuthPers");
}
return false;
}
private function authenticateAndSaveTokens($key, $userVal)
{
$user = null;
switch ($userVal) {
case "jis":
$user = PodioConfig::getPersUser();
break;
case "mc_automation1":
$user = PodioConfig::getMCAutomationUser1();
break;
case "mc_automation2":
$user = PodioConfig::getMCAutomationUser2();
break;
}
if ($user) {
Log::log("podioPasswordAuthenticate ", $user, "info");
Podio::authenticate('password', array(
'username' => $user['username'],
'password' => $user['password']
));
$user = PodioUserStatus::get();
Log::log("podioPasswordAuthenticate ", $user, "info");
$data = array(
'access_token' => Podio::$oauth->access_token,
'refresh_token' => Podio::$oauth->refresh_token,
'expire_time' => time() + Podio::$oauth->expires_in,
'start_time' => time(), // rate limit count - start time
'end_time' => time() + (60 * 60), // rate limit count - end time
'secret' => PodioConfig::$SECRET_KEY,
'user' => $userVal,
'key' => $key
);
Log::log("podioPasswordAuthenticate ", $data, "info");
$this->saveUserAuth($data);
return true;
}
return false;
}
private function authenticateUsingAccessToken($auth, $priority)
{
$auth = $this->checkEndTime($auth, $priority);
$time = $auth['expire_time'] - time() - (2 * 60);
Podio::setup($auth['client_id'], $auth['client_secret']);
Podio::$auth_type = array(
"type" => "password",
"identifier" => $auth['identifier']
);
Podio::$oauth = new PodioOAuth(
$auth['access_token'],
$auth['refresh_token'],
$time,
array("type" => "password", "id" => $auth['identifier'])
);
// todo
try {
if (Podio::is_authenticated()) {
$user = PodioUserStatus::get();
$this->updateRateLimitObject($auth);
return true;
}
} catch (PodioError $e) {
Log::logError($e . "", null, "podioAuthPers");
}
return false;
}
public function authenticate($identifier) //
{
$sql = "SELECT `key`,`user` FROM transfer_process where id=:id";
// $sql = "SELECT `key`,`user` FROM transfer_process where `key`=2"; //testing
$STH = DB::prepare($sql);
$STH->execute(array(
'id' => $identifier,
));
$result = $STH->fetchAll(PDO::FETCH_ASSOC);
if ($result && count($result) > 0) {
$transferProcess = $result[0];
if ($transferProcess['key'] == 1)
$this->podioSetUpUsingKey1();
else // else key will be 2
$this->podioSetUpUsingKey2();
$sql = "SELECT `id`, `expire_time` , `rate_limit_gen` ,`rate_limit` , `start_time` ,`end_time`,
AES_DECRYPT(`access_token`,:secret) as access_token,
AES_DECRYPT(`refresh_token`,:secret) as refresh_token
from `auth` where `key`=:keyVal and `user`=:userID";
$STH = DB::prepare($sql);
$STH->execute(array(
'secret' => PodioConfig::$SECRET_KEY,
'keyVal' => $transferProcess['key'],
'userID' => $transferProcess['user'],
));
$result = $STH->fetchAll(PDO::FETCH_ASSOC);
if (!$result || count($result) < 1) { // no values in DB
return $this->podioPasswordAuthenticate($transferProcess['key'], $transferProcess['user']);
} else {
$auth = $result[0];
$auth['identifier'] = 1;
$time = time() + (10 * 60); // add 10 min as
if ($auth['expire_time'] > $time) { // not expired yet
Podio::$auth_type = array(
"type" => "password",
"identifier" => $auth['identifier']
);
Podio::$oauth = new PodioOAuth(
$auth['access_token'],
$auth['refresh_token'],
$time,
array("type" => "password", "id" => $auth['identifier'])
);
if (Podio::is_authenticated()) {
try {
$user = PodioUserStatus::get(); // get the user values - to make sure auth is correct
return true;
} catch (\Exception $e) {
Log::logError($e . "", null, "authenticate");
}
}
} else { // auth value is expired, re authenticate using refresh token
try {
$resp = Podio::authenticate('refresh_token', array('refresh_token' => $auth['refresh_token']));
if (Podio::is_authenticated()) {
$user = PodioUserStatus::get(); // get the user values - to make sure auth is correct
return true;
}
} catch (\Exception $e) {
Log::logError($e . "", null, "authenticate");
}
}
// not authenticated yet, try password authentication
return $this->podioPasswordAuthenticate($transferProcess['key'], $transferProcess['user']);
}
}
return false;
}
public function authenticateSF($identifier)
{
try {
$sql = "SELECT `id`, `expire_time` , `rate_limit_gen` ,`rate_limit` , `start_time` ,`end_time`,
AES_DECRYPT(`access_token`,:secret) as access_token,
AES_DECRYPT(`refresh_token`,:secret) as refresh_token
from `auth` where `key`=:keyVal and `user`=:userID";
$STH = DB::prepare($sql);
$STH->execute(array(
'secret' => PodioConfig::$SECRET_KEY,
'keyVal' => 1,
'userID' => 4544429,
));
$result = $STH->fetchAll(PDO::FETCH_ASSOC);
$auth = $result[0];
$auth['identifier'] = 1; // this is only for logmein
$time = time() + (10 * 60); // add 10 min as
$diff = intval($auth['expire_time']) - $time;
Log::log("podioPasswordAuthenticate ", $auth, "info");
if (intval($auth['expire_time']) < $time) { // not expired yet
if (1 == 1) {
$this->podioSetUpUsingKey1();
} else // else key will be 2
{
$this->podioSetUpUsingKey2();
}
Podio::$auth_type = array(
"type" => "password",
"identifier" => $auth['identifier']
);
Podio::$oauth = new PodioOAuth(
$auth['access_token'],
$auth['refresh_token'],
$time,
array("type" => "password", "id" => 1)
);
if (Podio::is_authenticated()) {
try {
echo "<pre>";
$resp = PodioUser::get();;
print_r($resp);
} catch (\Exception $e) {
Log::logError($e . "", array($identifier), "AuthError");
return false;
}
// todo
return true;
} else {
echo "<br> ";
}
}
// auth value is expired, re authenticate using refresh token
if (1 == 1) {
$this->podioSetUpUsingKey1();
} else // else key will be 2
{
$this->podioSetUpUsingKey2();
}
try {
$resp = Podio::authenticate('refresh_token', array('refresh_token' => $auth['refresh_token']));
} catch (\Exception $e) {
echo "<pre>";
print_r($e);
Log::logError($e . "", array($identifier), "AuthError");
return false;
}
if (Podio::is_authenticated()) {
try {
$user = PodioUserStatus::get(); // get the user values - to make sure auth is correct
} catch (\Exception $e) {
print_r($e);
Log::logError($e . "", array($identifier), "AuthError");
return false;
}
return true;
}
// not authenticated yet, try password authentication
echo "<br/> not authenticated yet, try password authentication";
exit;
return $this->podioPasswordAuthenticate(1, 5406021);
// return $this->podioPasswordAuthenticate($transferProcess['key'], $transferProcess['user']);
// }
return false;
// } else {
// return true;
// }
} catch (\Exception $e) {
Log::logError($e . "", array($identifier), "AuthError");
}
return false;
}
private
function updateTRateLimit()
{
// todo
}
private function saveUserAuth($data)
{
$sql = "DELETE FROM auth WHERE `key`=:keyVal and `user`=:userID";
$STH = DB::prepare($sql);
$STH->execute(array(
'keyVal' => $data['key'],
'userID' => $data['user'],
));
$sql = "insert into `auth` (`access_token` , `refresh_token` ,`expire_time`, `start_time` , `end_time`,`key`,`user` )
values ( AES_ENCRYPT(:access_token,:secret),AES_ENCRYPT(:refresh_token,:secret),:expire_time,:start_time,:end_time,:key,:user)";
$data['secret'] = PodioConfig::$SECRET_KEY;
$STH = DB::prepare($sql);
$STH->execute($data);
}
// feaff4e4a8594976acabd3d2bbf14ab0
// feaff4e4a8594976acabd3d2bbf14ab0
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists