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;
} $totale = 0;
  foreach ($a as $key => $value)
$startYear = 2007; $totale += array_sum_all($value);
$year = filter_var($_REQUEST['year'], FILTER_SANITIZE_NUMBER_INT); return $totale;
if ($year != "") }
$yearQ = "YEAR(publishDate) = " . $year . " AND ";  
  // magic query modifiers
$standardQ = ' "childCN" is null '; // AND YEAR(contractStart) >= 2007 AND YEAR(contractStart) <= 2010'; $agency = filter_var($_REQUEST['agency'], FILTER_SANITIZE_STRING);
$start = 0.0; if ($agency != "")
  $agencyQ = "agencyName = '" . $agency . "' AND ";
function include_header($title) {  
global $start; $supplier = filter_var($_REQUEST['supplier'], FILTER_SANITIZE_STRING);
?> if ($supplier != "") {
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" $supplierParts = explode("-", $supplier);
"http://www.w3.org/TR/html4/strict.dtd"> $supplierName = "%" . $supplierParts[1] . "%";
<html> $supplierABN = $supplierParts[0];
<head> if ($supplierParts[0] > 0)
<title>Contract Dashboard - <?php echo $title; ?></title> $supplierQ = ' "supplierABN" = :supplierABN AND ';
<link rel="stylesheet" type="text/css" href="bootstrap.min.css"> else
<!-- Le HTML5 shim, for IE6-8 support of HTML elements --> $supplierQ = ' "supplierName" ILIKE :supplierName AND ';
<!--[if lt IE 9]> }
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>  
<![endif]--> $startYear = 2007;
<script type="text/javascript" src="lib/bsn.AutoSuggest_2.1.3_comp.js" charset="utf-8"></script> $year = filter_var($_REQUEST['year'], FILTER_SANITIZE_NUMBER_INT);
<link rel="stylesheet" href="autosuggest_inquisitor.css" type="text/css" media="screen" charset="utf-8" /> if ($year != "") {
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> $yearQ = 'extract(year from "contractStart") = ' . $year . " AND ";
<script type="text/javascript"> }
$(document).ready(function() $standardQ = ' "childCN" is null '; // AND YEAR(contractStart) >= 2007 AND YEAR(contractStart) <= 2010';
{ $start = 0.0;
//hide the all of the element with class msg_body  
$(".msg_body").hide(); function local_url() {
//toggle the componenet with class msg_body return "http://" . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\') . "/";
$(".msg_head").click(function() }
{  
$(this).next(".msg_body").slideToggle(600); function include_header($title) {
}); global $start;
}); ?>
</script> <!DOCTYPE html>
  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" version="XHTML+RDFa 1.1"
<style type="text/css" title="currentStyle"> xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
@import "media/css/demo_table.css"; xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
</style> xmlns:gr="http://purl.org/goodrelations/v1#"
<script type="text/javascript" language="javascript" src="media/js/jquery.dataTables.js"></script> xmlns:dc="http://purl.org/dc/terms/"
<script type="text/javascript" language="javascript" src="lib/bootstrap-dropdown.js"></script> xmlns:pc="http://purl.org/procurement#"
<script type="text/javascript" charset="utf-8"> xmlns:unspsc="http://www.ksl.stanford.edu/projects/DAML/UNSPSC.daml#"
jQuery.fn.dataTableExt.aTypes.unshift( xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
function ( sData ) xmlns:pcdt="http://purl.org/procurement/public-contracts-datatypes#"
{ prefix="rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
var sValidChars = "0123456789.-,"; rdfs: http://www.w3.org/2000/01/rdf-schema#
var Char; gr: http://purl.org/goodrelations/v1#
  dcterms: http://purl.org/dc/terms/
/* Check the numeric part */ pc: http://purl.org/procurement/public-contracts#
for ( i=1 ; i<sData.length ; i++ ) cpv: http://purl.org/weso/pscs/cpv/2008/resource/
{ unspsc: http://www.ksl.stanford.edu/projects/DAML/UNSPSC.daml#
Char = sData.charAt(i); v: http://www.w3.org/2006/vcard/ns#
if (sValidChars.indexOf(Char) == -1) payment: http://reference.data.gov.uk/def/payment#
{ br: http://purl.org/business-register#
return null; xsd: http://www.w3.org/2001/XMLSchema#
} pcdt: http://purl.org/procurement/public-contracts-datatypes#">
} <head>
  <title><?php echo $title; ?> - Contract Dashboard</title>
/* Check prefixed by currency */ <link rel="stylesheet" type="text/css" href="bootstrap.min.css">
if ( sData.charAt(0) == '$' || sData.charAt(0) == '£' ) <link rel="stylesheet" type="text/css" href="bootstrap-responsive.css">
{ <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
return 'currency'; <!--[if lt IE 9]>
} <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
return null; <![endif]-->
} <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" />
jQuery.fn.dataTableExt.oSort['currency-asc'] = function(a,b) { <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
/* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */ <script type="text/javascript">
var x = a == "-" ? 0 : a.replace( /,/g, "" ); $(document).ready(function()
var y = b == "-" ? 0 : b.replace( /,/g, "" ); {
  //hide the all of the element with class msg_body
/* Remove the currency sign */ $(".msg_body").hide();
x = x.substring( 1 ); //toggle the componenet with class msg_body
y = y.substring( 1 ); $(".msg_head").click(function()
  {
/* Parse and return */ $(this).next(".msg_body").slideToggle(600);
x = parseFloat( x ); });
y = parseFloat( y ); });
return x - y; </script>
};  
  <style type="text/css" title="currentStyle">
jQuery.fn.dataTableExt.oSort['currency-desc'] = function(a,b) { @import "media/css/demo_table.css";
/* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */ </style>
var x = a == "-" ? 0 : a.replace( /,/g, "" ); <script type="text/javascript" language="javascript" src="media/js/jquery.dataTables.js"></script>
var y = b == "-" ? 0 : b.replace( /,/g, "" ); <script type="text/javascript" language="javascript" src="lib/bootstrap-dropdown.js"></script>
  <script type="text/javascript" charset="utf-8">
/* Remove the currency sign */ jQuery.fn.dataTableExt.aTypes.unshift(
x = x.substring( 1 ); function ( sData )
y = y.substring( 1 ); {
  var sValidChars = "0123456789.-,";
/* Parse and return */ var Char;
x = parseFloat( x );  
y = parseFloat( y ); /* Check the numeric part */
return y - x; for ( i=1 ; i<sData.length ; i++ )
}; {
$(document).ready(function() { Char = sData.charAt(i);
$('table').dataTable(); if (sValidChars.indexOf(Char) == -1)
} ); {
</script> return null;
<link type="text/css" rel="stylesheet" href="style.css"> }
</head> }
<body>  
<div class="topbar"> /* Check prefixed by currency */
<div class="topbar-inner"> if ( sData.charAt(0) == '$' || sData.charAt(0) == '£' )
<div class="container-fluid"> {
<a class="brand" href="#">contract dashboard</a> return 'currency';
<ul class="nav"> }
<li><a href="displayAgency.php">agencies</a></li> return null;
<li><a href="displaySupplier.php">suppliers</a></li> }
<li><a href="displayCategory.php">categories</a></li> );
<li><a href="displayCalendar.php">time periods</a></li> jQuery.fn.dataTableExt.oSort['currency-asc'] = function(a,b) {
<!-- <li class="dropdown"> /* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
<a href="#" class="dropdown-toggle">metrics</a> var x = a == "-" ? 0 : a.replace( /,/g, "" );
<ul class="dropdown-menu">--> var y = b == "-" ? 0 : b.replace( /,/g, "" );
<li><a href="displayProcurementMethod.php">tenderm</a></li>  
<li><a href="displayConfidentialities.php">confidentiality</a></li> /* Remove the currency sign */
<li><a href="displayConsultancies.php">consultancies</a></li> x = x.substring( 1 );
<li><a href="displayAmendments.php">amendments</a></li> y = y.substring( 1 );
<li><a href="displayMap.php">geo</a></li>  
<!-- </ul> /* Parse and return */
</li>--> x = parseFloat( x );
</ul> y = parseFloat( y );
  return x - y;
  };
<form method="post" action="search.php" class="pull-right">  
<input type="text" id="searchKeyword" name="searchKeyword" value="" placeholder="Search" /> jQuery.fn.dataTableExt.oSort['currency-desc'] = function(a,b) {
<input type="hidden" id="searchID" name="searchID" value=""/> /* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
</form> var x = a == "-" ? 0 : a.replace( /,/g, "" );
  var y = b == "-" ? 0 : b.replace( /,/g, "" );
</div>  
</div><!-- /topbar-inner --> /* Remove the currency sign */
</div><!-- /topbar --> x = x.substring( 1 );
</div><!-- /topbar-wrapper --> y = y.substring( 1 );
<script type="text/javascript">  
  /* Parse and return */
var options_xml = { x = parseFloat( x );
script: function (input) { return "search_autosuggest.php?input="+input; }, y = parseFloat( y );
varname:"input", return y - x;
callback: function (obj) { document.getElementById('searchID').value = obj.id; } };
}; $(document).ready(function() {
var as_xml = new bsn.AutoSuggest('searchKeyword', options_xml); $('table').dataTable();
</script> } );
<div class="container-fluid"> </script>
<div class="sidebar"> <link type="text/css" rel="stylesheet" href="style.css">
<div class="well"> </head>
Filter by:<li> <body>
<li>year <div class="navbar">
<li><li>2008</li> <div class="navbar-inner">
</li> <div class="container-fluid">
</li> <a class="brand" href="#">contract dashboard</a>
</li> <br> <ul class="nav">
</div> </div> <li><a href="displayAgency.php">agencies</a></li>
<div class="content"> <li><a href="displaySupplier.php">suppliers</a></li>
<?php <li><a href="displaySON.php">standing offers</a></li>
$start = (float) array_sum(explode(' ', microtime())); <li><a href="displayCategory.php">categories</a></li>
} <li><a href="displayCalendar.php">dates</a></li>
  <li><a href="displayProcurementMethod.php">tenderm</a></li>
function include_footer() { <li><a href="displayConfidentialities.php">confidentiality</a></li>
global $start; <li><a href="displayConsultancies.php">consultancies</a></li>
$end = (float) array_sum(explode(' ', microtime())); <li><a href="displayAmendments.php">amendments</a></li>
  <li><a href="displayMap.php">geo</a></li>
echo ' <footer>' . "Processing time: " . sprintf("%.4f", ($end - $start)) . " seconds" . ' <footer>'; </ul>
echo '</div> </div></body> </html>';  
}  
  <form method="get" action="search.php" class="pull-right">
include ("graphs.inc.php"); <input type="text" id="searchKeyword" name="searchKeyword" value="" placeholder="Search" />
?> <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; }