<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RequestStack;
use phpCAS;
class CustomController extends AbstractController {
protected $isAuth = false;
protected $userEmail = '';
protected function selectFromWhere($table, $opt_where = '', $order_by = " D_MODIFICATION desc " , $DBG = false):array {
$out = array();
if (!empty($table)) {
$RAW_QUERY = "select * from ".$table
. (!empty($opt_where) ? ' where '.$opt_where : '')
. (!empty($order_by) ? ' order by '.$order_by : '')
;
// echo(__METHOD__.' RAW_QUERY: '.$RAW_QUERY);
if ($DBG != false) {
echo(__METHOD__
. ': RAW_QUERY: '.$RAW_QUERY
);
}
$found = $this->selectRaw($RAW_QUERY, $DBG);
/*
if (count($found) == 1) {
$out = $found[0];
} else
// */
// {
$out = $found;
// }
}
if ($DBG != false) {
echo(__METHOD__
// . ': RAW_QUERY: '.$RAW_QUERY
.': out: '.var_export($out, true));
}
return $out;
}
protected function selectRaw($RAW_QUERY, $DBG = false) : array {
$out = array();
if (!empty($RAW_QUERY)) {
$em = $this->getDoctrine()->getManager();
$statement = $em->getConnection()->prepare($RAW_QUERY);
// Set parameters
// $statement->bindValue('status', 1);
$res = $statement->execute();
$results = $res->fetchAllAssociative();
if (count($results) > 0) {
$out = $results;
}
}
if ($DBG) {
echo(__METHOD__
. ': SQL: '.$RAW_QUERY
.': out: '. var_export($out, true));
}
return $out;
}
public function testConnection($connection) {
// $conf = $connection->getConfiguration()->get('DATABASE_URL');
// echo(__METHOD__.' conf: '.var_export($conf, true)); exit();
$databaseUrl = $_ENV['DATABASE_URL'];
echo(var_export($databaseUrl));
$conn = oci_connect($connection->getUsername(), $connection->getPassword(), $connection->getHost().'/GRHV4'); // ('EASYID', '******', 'am-oracle-4/GRHV4');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$stid = oci_parse($conn, 'SELECT * FROM VCARD');
oci_execute($stid);
echo "<table border='1'>\n";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
echo "<tr>\n";
foreach ($row as $item) {
echo " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "") . "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
echo(__METHOD__.': '
.'Connection: '. var_export($connection->getPassword(), true)
.'User: '. var_export($connection->getUsername(), true)
.'Host: '. var_export($connection->getHost(), true)
// .'Host: '. var_export($connection->getTnsname(), true)
);
exit();
}
protected function redirectToAuth($current_route = null) {
$args = array();
if (!empty($current_route)) {
$args['referer'] = $current_route;
}
$this->redirectToRoute('app_v_card_auth', $args, Response::HTTP_SEE_OTHER);
exit();
}
protected function getAuthCAS($request) : string {
$out = '';
// Load the settings from the central config file
require_once 'config_CAS.php';
// Load the CAS lib
// echo(__METHOD__.': phpcas_path: '.$phpcas_path); exit();
require_once $phpcas_path . '/CAS.php';
// Enable debugging
phpCAS::setLogger();
// Enable verbose error messages. Disable in production!
phpCAS::setVerbose(true);
// Initialize phpCAS
phpCAS::client(CAS_VERSION_2_0, $cas_host, $cas_port, $cas_context, $client_service_name);
// For production use set the CA certificate that is the issuer of the cert
// on the CAS server and uncomment the line below
// phpCAS::setCasServerCACert($cas_server_ca_cert_path);
// For quick testing you can disable SSL validation of the CAS server.
// THIS SETTING IS NOT RECOMMENDED FOR PRODUCTION.
// VALIDATING THE CAS SERVER IS CRUCIAL TO THE SECURITY OF THE CAS PROTOCOL!
phpCAS::setNoCasServerValidation();
// force CAS authentication
phpCAS::forceAuthentication();
// at this step, the user has been authenticated by the CAS server
// and the user's login name can be read with phpCAS::getUser().
// echo(__METHOD__.': authenticated!'); exit();
// logout if desired
if (isset($_REQUEST['logout'])) {
phpCAS::logout();
} else {
$session = $request->getSession();
$this->isAuth = true;
$out = $this->userEmail = phpCAS::getUser();
$session->set('is_auth', $this->isAuth);
$session->set('user_email', $this->userEmail);
}
// echo(__METHOD__.': Hello '.phpCAS::getUser());
return $out;
}
}