Add openid security
[contractdashboard.git] / lib / common.inc.php
blob:a/lib/common.inc.php -> blob:b/lib/common.inc.php
<?php <?php
date_default_timezone_set("Australia/ACT"); date_default_timezone_set("Australia/ACT");
   
error_reporting(E_ALL ^ E_NOTICE); error_reporting(E_ALL ^ E_NOTICE);
   
   
$conn = new PDO("pgsql:dbname=contractDashboard;user=postgres;password=snmc;host=localhost"); $conn = new PDO("pgsql:dbname=contractdashboard;user=postgres;password=snmc;host=localhost");
   
if (!$conn) { if (!$conn) {
die("A database error occurred.\n"); die("A database error occurred.\n");
} }
   
define('ROOT' , pathinfo(__FILE__, PATHINFO_DIRNAME)); define('ROOT', pathinfo(__FILE__, PATHINFO_DIRNAME));
if (strstr($_SERVER['PHP_SELF'], "labs/")) { if (strstr($_SERVER['PHP_SELF'], "labs/") || strstr($_SERVER['PHP_SELF'], "admin/") || strstr($_SERVER['PHP_SELF'], "heuristics/")) {
$basePath = "../"; $basePath = "../";
} }
  require ROOT . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'openid.php';
require ROOT . DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'openid.php'; require(ROOT.'/solarium/vendor/autoload.php');
$openid = new LightOpenID($_SERVER['HTTP_HOST']); require(ROOT.'/solarium/library/Solarium/Autoloader.php');
  Solarium\Autoloader::register();
function login() { // check solarium version available
global $openid; //echo 'Solarium library version: ' . Solarium\Client::VERSION . ' - ';
if (!$openid->mode) { $config = array(
$openid->required = array('contact/email'); 'endpoint' => array(
$openid->identity = 'https://www.google.com/accounts/o8/id'; 'localhost' => array(
header('Location: ' . $openid->authUrl()); 'host' => '127.0.0.1',
} 'port' => 8983,
} 'path' => '/solr/contracts/',
  )
function auth() { )
global $openid; );
if ($_SESSION['authed'] == true) { // create a client instance
return true; $solr_client = new Solarium\Client($config);
}  
   
if ($openid->mode) { $openid = new LightOpenID($_SERVER['HTTP_HOST']);
$attr = $openid->getAttributes(); // you have to open the session to be able to modify or remove it
if ($attr['contact/email'] != 'maxious@gmail.com') { session_start();
die('Access Denied'); function login() {
} else { global $openid;
$_SESSION['authed'] = true; if (!$openid->mode) {
} $openid->required = array('contact/email');
} else { $openid->identity = 'https://www.google.com/accounts/o8/id';
login(); header('Location: ' . $openid->authUrl());
} }
} }
   
// $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); function auth() {
function databaseError($errMsg) { global $openid;
if ($errMsg[2] != "") { if ($_SESSION['authed'] == true) {
echo '<div class="alert-message error">'; return true;
die(print_r($errMsg, true)); }
echo "</div>";  
} if ($openid->mode) {
} $attr = $openid->getAttributes();
  if ($attr['contact/email'] != 'maxious@gmail.com') {
function ucsmart($str) { die('Access Denied');
$shortWords = Array("The", "Pty", "Ltd", "Inc", "Red", "Oil", "A", "An", "And", "At", "For", "In" } else {
, "Of", "On", "Or", "The", "To", "With"); $_SESSION['authed'] = true;
$strArray = explode(" ", preg_replace("/(?<=(?<!:|’s)\W) }
(A|An|And|At|For|In|Of|On|Or|The|To|With) } else {
(?=\W)/e", 'strtolower("$1")', ucwords(strtolower($str)))); login();
foreach ($strArray as &$word) { }
if (strlen($word) <= 4 && !in_array($word, $shortWords)) }
$word = strtoupper($word);  
} // $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return implode(" ", $strArray); function databaseError($errMsg) {
} if ($errMsg[2] != "") {
  echo '<div class="alert-message error">';
function percent($num_amount, $num_total) { die(print_r($errMsg, true));
$count1 = $num_amount / $num_total; echo "</div>";
$count2 = $count1 * 100; }
$count = number_format($count2, 2); }
return $count;  
} function ucsmart($str) {
  $shortWords = Array("The", "Pty", "Ltd", "Inc", "Red", "Oil", "A", "An", "And", "At", "For", "In"
function array_sum_all($a) { , "Of", "On", "Or", "The", "To", "With");
if (!is_array($a)) $strArray = explode(" ", preg_replace_callback("/(?<=(?<!:|’s)\W)
return $a; (A|An|And|At|For|In|Of|On|Or|The|To|With)
foreach ($a as $key => $value) (?=\W)/",
$totale += array_sum_all($value); function($matches) { return strtolower($matches[1]); } , ucwords(strtolower($str))));
return $totale; foreach ($strArray as &$word) {
} if (strlen($word) <= 4 && !in_array($word, $shortWords))
  $word = strtoupper($word);
// magic query modifiers }
$agency = filter_var($_REQUEST['agency'], FILTER_SANITIZE_STRING); return implode(" ", $strArray);
if ($agency != "") }
$agencyQ = "agencyName = '" . $agency . "' AND ";  
  function percent($num_amount, $num_total) {
$supplier = filter_var($_REQUEST['supplier'], FILTER_SANITIZE_STRING); $count1 = $num_amount / $num_total;
if ($supplier != "") { $count2 = $count1 * 100;
$supplierParts = explode("-", $supplier); $count = number_format($count2, 2);
$supplierName = "%" . $supplierParts[1] . "%"; return $count;
$supplierABN = $supplierParts[0]; }
if ($supplierParts[0] > 0)  
$supplierQ = ' "supplierABN" = :supplierABN AND '; function array_sum_all($a) {
else if (!is_array($a))
$supplierQ = ' "supplierName" LIKE :supplierName AND '; return $a;
} foreach ($a as $key => $value)
  $totale += array_sum_all($value);
$startYear = 2007; return $totale;
$year = filter_var($_REQUEST['year'], FILTER_SANITIZE_NUMBER_INT); }
if ($year != "")  
$yearQ = "YEAR(publishDate) = " . $year . " AND "; // magic query modifiers
  $agency = filter_var($_REQUEST['agency'], FILTER_SANITIZE_STRING);
$standardQ = ' "childCN" is null '; // AND YEAR(contractStart) >= 2007 AND YEAR(contractStart) <= 2010'; if ($agency != "")
$start = 0.0; $agencyQ = "agencyName = '" . $agency . "' AND ";
   
function include_header($title) { $supplier = filter_var($_REQUEST['supplier'], FILTER_SANITIZE_STRING);
global $start; if ($supplier != "") {
?> $supplierParts = explode("-", $supplier);
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" $supplierName = "%" . $supplierParts[1] . "%";
"http://www.w3.org/TR/html4/strict.dtd"> $supplierABN = $supplierParts[0];
<html> if ($supplierParts[0] > 0)
<head> $supplierQ = ' "supplierABN" = :supplierABN AND ';
<title>Contract Dashboard - <?php echo $title; ?></title> else
<link rel="stylesheet" type="text/css" href="bootstrap.min.css"> $supplierQ = ' "supplierName" ILIKE :supplierName AND ';
<!-- Le HTML5 shim, for IE6-8 support of HTML elements --> }
<!--[if lt IE 9]>  
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> $startYear = 2007;
<![endif]--> $year = filter_var($_REQUEST['year'], FILTER_SANITIZE_NUMBER_INT);
<script type="text/javascript" src="lib/bsn.AutoSuggest_2.1.3_comp.js" charset="utf-8"></script> if ($year != "") {
<link rel="stylesheet" href="autosuggest_inquisitor.css" type="text/css" media="screen" charset="utf-8" /> $yearQ = 'extract(year from "contractStart") = ' . $year . " AND ";
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> }
<script type="text/javascript"> $standardQ = ' "childCN" is null '; // AND YEAR(contractStart) >= 2007 AND YEAR(contractStart) <= 2010';
$(document).ready(function() $start = 0.0;
{  
//hide the all of the element with class msg_body function local_url() {
$(".msg_body").hide(); return "http://" . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\') . "/";
//toggle the componenet with class msg_body }
$(".msg_head").click(function()  
{ function include_header($title) {
$(this).next(".msg_body").slideToggle(600); global $start;
}); ?>
}); <!DOCTYPE html>
</script> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" version="XHTML+RDFa 1.1"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
<style type="text/css" title="currentStyle"> xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
@import "media/css/demo_table.css"; xmlns:gr="http://purl.org/goodrelations/v1#"
</style> xmlns:dc="http://purl.org/dc/terms/"
<script type="text/javascript" language="javascript" src="media/js/jquery.dataTables.js"></script> xmlns:pc="http://purl.org/procurement#"
<script type="text/javascript" language="javascript" src="lib/bootstrap-dropdown.js"></script> xmlns:unspsc="http://www.ksl.stanford.edu/projects/DAML/UNSPSC.daml#"
<script type="text/javascript" charset="utf-8"> xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
jQuery.fn.dataTableExt.aTypes.unshift( xmlns:pcdt="http://purl.org/procurement/public-contracts-datatypes#"
function ( sData ) prefix="rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
{ rdfs: http://www.w3.org/2000/01/rdf-schema#
var sValidChars = "0123456789.-,"; gr: http://purl.org/goodrelations/v1#
var Char; dcterms: http://purl.org/dc/terms/
  pc: http://purl.org/procurement/public-contracts#
/* Check the numeric part */ cpv: http://purl.org/weso/pscs/cpv/2008/resource/
for ( i=1 ; i<sData.length ; i++ ) unspsc: http://www.ksl.stanford.edu/projects/DAML/UNSPSC.daml#
{ v: http://www.w3.org/2006/vcard/ns#
Char = sData.charAt(i); payment: http://reference.data.gov.uk/def/payment#
if (sValidChars.indexOf(Char) == -1) br: http://purl.org/business-register#
{ xsd: http://www.w3.org/2001/XMLSchema#
return null; pcdt: http://purl.org/procurement/public-contracts-datatypes#">
} <head>
} <title><?php echo $title; ?> - Contract Dashboard</title>
  <link rel="stylesheet" type="text/css" href="bootstrap.min.css">
/* Check prefixed by currency */ <link rel="stylesheet" type="text/css" href="bootstrap-responsive.css">
if ( sData.charAt(0) == '$' || sData.charAt(0) == '£' ) <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
{ <!--[if lt IE 9]>
return 'currency'; <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
} <![endif]-->
return null; <script type="text/javascript" src="lib/bsn.AutoSuggest_2.1.3_comp.js" charset="utf-8"></script>
} <link rel="stylesheet" href="autosuggest_inquisitor.css" type="text/css" media="screen" charset="utf-8" />
); <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
jQuery.fn.dataTableExt.oSort['currency-asc'] = function(a,b) { <script type="text/javascript">
/* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */ $(document).ready(function()
var x = a == "-" ? 0 : a.replace( /,/g, "" ); {
var y = b == "-" ? 0 : b.replace( /,/g, "" ); //hide the all of the element with class msg_body
  $(".msg_body").hide();
/* Remove the currency sign */ //toggle the componenet with class msg_body
x = x.substring( 1 ); $(".msg_head").click(function()
y = y.substring( 1 ); {
  $(this).next(".msg_body").slideToggle(600);
/* Parse and return */ });
x = parseFloat( x ); });
y = parseFloat( y ); </script>
return x - y;  
}; <style type="text/css" title="currentStyle">
  @import "media/css/demo_table.css";
jQuery.fn.dataTableExt.oSort['currency-desc'] = function(a,b) { </style>
/* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */ <script type="text/javascript" language="javascript" src="media/js/jquery.dataTables.js"></script>
var x = a == "-" ? 0 : a.replace( /,/g, "" ); <script type="text/javascript" language="javascript" src="lib/bootstrap-dropdown.js"></script>
var y = b == "-" ? 0 : b.replace( /,/g, "" ); <script type="text/javascript" charset="utf-8">
  jQuery.fn.dataTableExt.aTypes.unshift(
/* Remove the currency sign */ function ( sData )
x = x.substring( 1 ); {
y = y.substring( 1 ); var sValidChars = "0123456789.-,";
  var Char;
/* Parse and return */  
x = parseFloat( x ); /* Check the numeric part */
y = parseFloat( y ); for ( i=1 ; i<sData.length ; i++ )
return y - x; {
}; Char = sData.charAt(i);
$(document).ready(function() { if (sValidChars.indexOf(Char) == -1)
$('table').dataTable(); {
} ); return null;
</script> }
<link type="text/css" rel="stylesheet" href="style.css"> }
</head>  
<body> /* Check prefixed by currency */
<div class="topbar"> if ( sData.charAt(0) == '$' || sData.charAt(0) == '£' )
<div class="topbar-inner"> {
<div class="container-fluid"> return 'currency';
<a class="brand" href="#">contract dashboard</a> }
<ul class="nav"> return null;
<li><a href="displayAgency.php">agencies</a></li> }
<li><a href="displaySupplier.php">suppliers</a></li> );
<li><a href="displayCategory.php">categories</a></li> jQuery.fn.dataTableExt.oSort['currency-asc'] = function(a,b) {
<li><a href="displayCalendar.php">time periods</a></li> /* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
<!-- <li class="dropdown"> var x = a == "-" ? 0 : a.replace( /,/g, "" );
<a href="#" class="dropdown-toggle">metrics</a> var y = b == "-" ? 0 : b.replace( /,/g, "" );
<ul class="dropdown-menu">-->  
<li><a href="displayProcurementMethod.php">tenderm</a></li> /* Remove the currency sign */
<li><a href="displayConfidentialities.php">confidentiality</a></li> x = x.substring( 1 );
<li><a href="displayConsultancies.php">consultancies</a></li> y = y.substring( 1 );
<li><a href="displayAmendments.php">amendments</a></li>  
<li><a href="displayMap.php">geo</a></li> /* Parse and return */
<!-- </ul> x = parseFloat( x );
</li>--> y = parseFloat( y );
</ul> return x - y;
  };
   
<form method="post" action="search.php" class="pull-right"> jQuery.fn.dataTableExt.oSort['currency-desc'] = function(a,b) {
<input type="text" id="searchKeyword" name="searchKeyword" value="" placeholder="Search" /> /* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
<input type="hidden" id="searchID" name="searchID" value=""/> var x = a == "-" ? 0 : a.replace( /,/g, "" );
</form> var y = b == "-" ? 0 : b.replace( /,/g, "" );
   
</div> /* Remove the currency sign */
</div><!-- /topbar-inner --> x = x.substring( 1 );
</div><!-- /topbar --> y = y.substring( 1 );
</div><!-- /topbar-wrapper -->  
<script type="text/javascript"> /* Parse and return */
  x = parseFloat( x );
var options_xml = { y = parseFloat( y );
script: function (input) { return "search_autosuggest.php?input="+input; }, return y - x;
varname:"input", };
callback: function (obj) { document.getElementById('searchID').value = obj.id; } $(document).ready(function() {
}; $('table').dataTable();
var as_xml = new bsn.AutoSuggest('searchKeyword', options_xml); } );
</script> </script>
<div class="container-fluid"> <link type="text/css" rel="stylesheet" href="style.css">
<div class="sidebar"> </head>
<div class="well"> <body>
Filter by:<li> <div class="navbar">
<li>year <div class="navbar-inner">
<li><li>2008</li> <div class="container-fluid">
</li> <a class="brand" href="#">contract dashboard</a>
</li> <ul class="nav">
</li> <br> <li><a href="displayAgency.php">agencies</a></li>
</div> </div> <li><a href="displaySupplier.php">suppliers</a></li>
<div class="content"> <li><a href="displaySON.php">standing offers</a></li>
<?php <li><a href="displayCategory.php">categories</a></li>
$start = (float) array_sum(explode(' ', microtime())); <li><a href="displayCalendar.php">dates</a></li>
} <li><a href="displayProcurementMethod.php">tenderm</a></li>
  <li><a href="displayConfidentialities.php">confidentiality</a></li>
function include_footer() { <li><a href="displayConsultancies.php">consultancies</a></li>
global $start; <li><a href="displayAmendments.php">amendments</a></li>
$end = (float) array_sum(explode(' ', microtime())); <li><a href="displayMap.php">geo</a></li>
  </ul>
echo ' <footer>' . "Processing time: " . sprintf("%.4f", ($end - $start)) . " seconds" . ' <footer>';  
echo '</div> </div></body> </html>';  
} <form method="get" action="search.php" class="pull-right">
  <input type="text" id="searchKeyword" name="searchKeyword" value="" placeholder="Search" />
include ("graphs.inc.php"); <input type="hidden" id="searchID" name="searchID" value=""/>
?> </form>
   
  </div>
  </div><!-- /topbar-inner -->
  </div><!-- /topbar -->
  </div><!-- /topbar-wrapper -->
  <script type="text/javascript">
   
  var options_xml = {
  script: function (input) { return "search_autosuggest.php?input="+input; },
  varname:"input",
  callback: function (obj) { document.getElementById('searchID').value = obj.id; }
  };