2009-09-09 20:23:39 -07:00
|
|
|
<?php
|
|
|
|
|
|
2019-03-18 00:53:35 -07:00
|
|
|
/* App Controller */
|
2009-09-09 20:23:39 -07:00
|
|
|
|
|
|
|
|
class AppController extends Controller {
|
|
|
|
|
|
2019-03-18 00:53:35 -07:00
|
|
|
var $components = array('RequestHandler');
|
2009-09-09 20:23:39 -07:00
|
|
|
|
2019-03-18 00:53:35 -07:00
|
|
|
var $uses = array('User');
|
2011-03-05 21:27:20 -08:00
|
|
|
var $helpers = array('Javascript', 'Time', 'Html', 'Form');
|
2010-01-10 15:34:31 -08:00
|
|
|
function beforeFilter() {
|
2010-01-10 10:05:04 -08:00
|
|
|
|
2025-08-18 04:10:33 -07:00
|
|
|
$user = null;
|
|
|
|
|
|
|
|
|
|
// Check if Tailscale authentication is enabled
|
|
|
|
|
if (Configure::read('Tailscale.enabled')) {
|
|
|
|
|
// Check for Tailscale authentication headers
|
|
|
|
|
$tailscaleLogin = isset($_SERVER['HTTP_TAILSCALE_USER_LOGIN']) ? $_SERVER['HTTP_TAILSCALE_USER_LOGIN'] : null;
|
|
|
|
|
$tailscaleName = isset($_SERVER['HTTP_TAILSCALE_USER_NAME']) ? $_SERVER['HTTP_TAILSCALE_USER_NAME'] : null;
|
|
|
|
|
|
|
|
|
|
if ($tailscaleLogin) {
|
|
|
|
|
// Try to find user by email address from Tailscale header
|
|
|
|
|
$user = $this->User->find('first', array(
|
|
|
|
|
'recursive' => 0,
|
|
|
|
|
'conditions' => array('User.email' => $tailscaleLogin)
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
// If user not found and auto-creation is enabled, create a new user
|
|
|
|
|
if (!$user && Configure::read('Tailscale.autoCreateUsers')) {
|
|
|
|
|
// Parse the name
|
|
|
|
|
$firstName = '';
|
|
|
|
|
$lastName = '';
|
|
|
|
|
if ($tailscaleName) {
|
|
|
|
|
$nameParts = explode(' ', $tailscaleName);
|
|
|
|
|
$firstName = $nameParts[0];
|
|
|
|
|
if (count($nameParts) > 1) {
|
|
|
|
|
array_shift($nameParts);
|
|
|
|
|
$lastName = implode(' ', $nameParts);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$userData = array(
|
|
|
|
|
'User' => array(
|
|
|
|
|
'email' => $tailscaleLogin,
|
|
|
|
|
'username' => $tailscaleLogin,
|
|
|
|
|
'first_name' => $firstName,
|
|
|
|
|
'last_name' => $lastName,
|
|
|
|
|
'type' => 'user',
|
|
|
|
|
'access_level' => Configure::read('Tailscale.defaultAccessLevel'),
|
|
|
|
|
'enabled' => 1,
|
|
|
|
|
'by_vault' => 0
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
$this->User->create();
|
|
|
|
|
if ($this->User->save($userData)) {
|
|
|
|
|
$user = $this->User->find('first', array(
|
|
|
|
|
'recursive' => 0,
|
|
|
|
|
'conditions' => array('User.id' => $this->User->id)
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fall back to HTTP basic auth if no Tailscale auth or user not found
|
|
|
|
|
if (!$user && isset($_SERVER["PHP_AUTH_USER"])) {
|
|
|
|
|
$user = $this->User->find('first', array(
|
|
|
|
|
'recursive' => 0,
|
|
|
|
|
'conditions' => array('User.username' => $_SERVER["PHP_AUTH_USER"])
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-18 00:53:35 -07:00
|
|
|
$this->set("currentuser", $user);
|
2010-01-10 10:05:04 -08:00
|
|
|
|
2019-03-18 00:53:35 -07:00
|
|
|
if($this->RequestHandler->isAjax()) {
|
2011-08-11 22:46:26 -07:00
|
|
|
Configure::write('debug', 0);
|
|
|
|
|
}
|
2010-01-10 15:34:31 -08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-27 17:51:39 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if the current logged in user is an admin
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
function isAdmin() {
|
|
|
|
|
$currentuser = $this->getCurrentUser();
|
2011-03-09 23:18:26 -08:00
|
|
|
if($currentuser['access_level'] == 'admin') {
|
2010-12-27 17:51:39 -08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-20 23:58:21 -07:00
|
|
|
|
2011-08-11 22:46:26 -07:00
|
|
|
function isManager() {
|
2011-03-20 23:58:21 -07:00
|
|
|
$currentuser = $this->getCurrentUser();
|
|
|
|
|
if($currentuser['access_level'] == 'manager') {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-12-27 17:51:39 -08:00
|
|
|
/**
|
|
|
|
|
* Read the current logged in user.
|
|
|
|
|
* @return array - the currently logged in user.
|
|
|
|
|
*/
|
|
|
|
|
function getCurrentUser() {
|
2025-08-18 04:10:33 -07:00
|
|
|
$user = null;
|
|
|
|
|
|
|
|
|
|
// Check if Tailscale authentication is enabled
|
|
|
|
|
if (Configure::read('Tailscale.enabled')) {
|
|
|
|
|
$tailscaleLogin = isset($_SERVER['HTTP_TAILSCALE_USER_LOGIN']) ? $_SERVER['HTTP_TAILSCALE_USER_LOGIN'] : null;
|
|
|
|
|
|
|
|
|
|
if ($tailscaleLogin) {
|
|
|
|
|
// Try to find user by email address from Tailscale header
|
|
|
|
|
$user = $this->User->find('first', array(
|
|
|
|
|
'recursive' => 0,
|
|
|
|
|
'conditions' => array('User.email' => $tailscaleLogin)
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fall back to HTTP basic auth if no Tailscale auth or user not found
|
|
|
|
|
if (!$user && isset($_SERVER["PHP_AUTH_USER"])) {
|
|
|
|
|
$user = $this->User->find('first', array(
|
|
|
|
|
'recursive' => 0,
|
|
|
|
|
'conditions' => array('User.username' => $_SERVER["PHP_AUTH_USER"])
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-06 04:16:13 -07:00
|
|
|
return $user;
|
2010-12-27 17:51:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the id of the current user. False if not logged in.
|
|
|
|
|
*/
|
|
|
|
|
function getCurrentUserID() {
|
|
|
|
|
$currentuser = $this->getCurrentUser();
|
|
|
|
|
if($currentuser) {
|
2019-04-06 17:36:25 -07:00
|
|
|
return $currentuser['User']['id'];
|
2010-12-27 17:51:39 -08:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-05-24 02:11:07 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-11 22:46:26 -07:00
|
|
|
function calculateTotals($document, $gst) {
|
2011-05-24 02:11:07 -07:00
|
|
|
$totals = array('subtotal'=>0, 'gst'=>0, 'total'=>0);
|
2011-08-11 22:46:26 -07:00
|
|
|
|
2011-05-24 02:11:07 -07:00
|
|
|
|
|
|
|
|
foreach($document['LineItem'] as $lineitem) {
|
|
|
|
|
if($lineitem['option'] == 1) {
|
|
|
|
|
$totals['subtotal'] = 'TBA';
|
|
|
|
|
$totals['total'] = 'TBA';
|
|
|
|
|
$totals['gst'] = 'TBA';
|
|
|
|
|
return $totals;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$totals['subtotal'] += $lineitem['net_price'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if($gst == 1) {
|
|
|
|
|
$totals['gst'] = 0.1*$totals['subtotal'];
|
|
|
|
|
}
|
|
|
|
|
$totals['total'] = $totals['gst'] + $totals['subtotal'];
|
|
|
|
|
return $totals;
|
|
|
|
|
|
|
|
|
|
}
|
2010-02-16 19:34:17 -08:00
|
|
|
|
2011-08-11 22:46:26 -07:00
|
|
|
function unset_keys($array, $keys) {
|
|
|
|
|
foreach($keys as $key ) {
|
|
|
|
|
$array[$key] = null;
|
|
|
|
|
}
|
|
|
|
|
return $array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function unset_multiple_keys($array, $keys) {
|
|
|
|
|
foreach($array as $index => $item) {
|
|
|
|
|
$array[$index]['id'] = null;
|
|
|
|
|
$array[$index]['document_id'] = null;
|
|
|
|
|
$array[$index]['costing_id'] = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-09-26 20:47:36 -07:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param <type> $year
|
|
|
|
|
* @param <type> $prevYear
|
|
|
|
|
* @return <type>
|
|
|
|
|
*/
|
|
|
|
|
function getFirstDayFY($year,$prevYear = false) {
|
|
|
|
|
if($prevYear == false) {
|
|
|
|
|
return mktime(0,0,0,7,1,$year);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return mktime(0,0,0,7,1,$year-1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param <type> $year
|
|
|
|
|
* @return <int>
|
|
|
|
|
*/
|
|
|
|
|
function getLastDayFY($year) {
|
|
|
|
|
return mktime(23,59,59,6,30,$year);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-16 19:34:17 -08:00
|
|
|
|
2009-09-09 20:23:39 -07:00
|
|
|
|
2010-01-10 10:05:04 -08:00
|
|
|
|
2009-09-09 20:23:39 -07:00
|
|
|
}
|
2012-09-07 18:40:45 -07:00
|
|
|
?>
|