<?php
/**
* @version 2.2
* Parse the URL then get necessary values and create common strings.
*
* reserved $_GET parameters that are checked for every page
* cols = number of thumbnail columns per page
* date = date for searching
* f = item id for file viewing
* i = item id for viewing
* loc = location for searching
* mode = mode (edit/view)
* n = page number
* p = page (dates, admin, login)
* rows = number of thumbnail rows per page
* tags = tags for searching
* people = people for searching
* user = user/contributor for searching
*
* @since 2.0
* @package kwalbum
*/
class URLParser
{
public $pageName;
public $numColumns, $numRows;
private $id, $idQuery;
private $date, $dateString, $dateQuery;
private $location, $locationQuery;
private $user, $userQuery;
private $tags, $tagString, $tagQuery;
private $people, $peopleString, $peopleQuery;
private $thumbnailQuery;
private $orderByField, $orderByDirection;
private $title;
private $itemNumber, $totalItems;
private $pageNumber, $totalPages;
private $extendedURL;
function URLParser()
{
// set 'tags' if the query string does not have any variable=value relations
if (false === strpos($_SERVER['QUERY_STRING'], '='))
$_GET['tags'] = urldecode($_SERVER['QUERY_STRING']);
if (isset($_GET['cols']) and 0 < ($i = (int)$_GET['cols']))
$this->numColumns = $i;
else
$this->numColumns = THUMB_COLS;
if (isset($_GET['rows']) and 0 < ($i = (int)$_GET['rows']))
$this->numRows = $i;
else
$this->numRows = THUMB_ROWS;
$this->orderByField = 'ItemOrderBy';
$this->orderByDirection = 'ASC';
}
public function GetPageName()
{
global $DB;
if (!$this->pageName)
{
$this->pageName = $DB->FilterString(@$_GET['p']);
if (empty($this->pageName))
{
if (!$this->GetId())
{
if (0 < strlen($_SERVER['QUERY_STRING']))
$this->pageName = 'Thumbnails';
else
$this->pageName = 'Index';
} else
$this->pageName = 'Item';
}
}
return $this->pageName;
}
public function GetId()
{
if (!isset ($this->id))
$this->id = (int) @$_GET['i'];
$this->idQuery = "itemID = '$this->id'";
return $this->id;
}
// set $date and $dateQuery if the page is not "Dates" and a valid date exists in the URL
public function GetURLDate()
{
if (isset ($this->date))
return $this->date;
if (!empty ($_GET['date']) and 'Dates' != @$_GET['p'])
{
$newDate = split('-', $_GET['date']);
$year = (int) $newDate[0];
$month = (int) @$newDate[1];
$day = (int) @$newDate[2];
if (1800 <= $year and 12 >= $month and 32 >= $day)
{
if (0 == $month)
$month = $day = '00';
$this->date = "$year-$month-$day";
if (0 == $month)
{
$this->dateString = $year;
$this->dateQuery = "ItemOrderBy >= '$this->date 00:00:00' AND ItemOrderBy <= '$year-12-31 23:59:59'";
} else
if (0 == $day)
{
$this->dateString = date('F Y', strtotime("$year-$month-1"));
$this->dateQuery = "ItemOrderBy >= '$this->date 00:00:00' AND ItemOrderBy <= '$year-$month-32 23:59:59'";
} else{
$this->dateString = date('F j, Y', strtotime($this->date));
$this->dateQuery = "ItemOrderBy >= '$this->date 00:00:00' AND ItemOrderBy <= '$this->date 23:59:59'";
}
} else
{
$this->date = null;
$this->dateString = null;
$this->dateQuery = null;
}
return $this->date;
}
}
public function GetDateString()
{
if (!isset($this->dateString))
$this->GetURLDate();
return $this->dateString;
}
// set $location and $locationQuery if the page is not "Locations"
public function GetLocation()
{
global $DB;
if (!isset ($this->location))
{
if (!empty ($_GET['loc']) and 'Locations' != @$_GET['p'])
{
$this->location = $DB->FilterString($_GET['loc']);
$query = "SELECT LocationId" .
" FROM " . LOCATION_TABLE .
" WHERE Location='$this->location'";
$result = $DB->Query($query);
if (0 == $result->num_rows)
$id = 0;
else
{
$row = $result->fetch_array();
$id = $row[0];
}
$this->locationQuery = "ItemLocationIdFk='$id'";
} else
{
$this->location = null;
$this->locationQuery = null;
}
}
return $this->location;
}
// set $user and $userQuery if the page is not "Users"
public function GetUser()
{
global $DB;
if (!isset ($this->user))
{
if (!empty ($_GET['user']) and 'Users' != @$_GET['p'])
{
$this->user = $DB->FilterString($_GET['user']);
$query = "SELECT UserId" .
" FROM " . USER_TABLE .
" WHERE UserTitle='$this->user'";
$result = $DB->Query($query);
if (0 == $result->num_rows)
$id = 0;
else
{
$row = $result->fetch_array();
$id = $row[0];
}
$this->userQuery = "ItemUserIdFk='$id'";
} else
{
$this->user = null;
$this->userQuery = null;
}
}
return $this->user;
}
// set $tags as an array of tags
public function GetTags()
{
global $DB;
if (!isset ($this->tags))
{
$tagString = '';
if (!empty ($_GET['tags']))
{
$tags = explode(',', $_GET['tags']);
$size = count($tags);
for($i = 0; $i < $size; $i++)
{
$tags[$i] = $DB->FilterString(trim($tags[$i]));
if (empty($tags[$i]))
unset($tags[$i]);
else
$tagString .= ($tagString ? ',' : null).$tags[$i];
}
$this->tags = $tags;
}
else
$this->tags = array();
$this->tagString = $tagString;
}
return $this->tags;
}
// set $tagString
public function GetTagString()
{
if (!isset ($this->tagString))
$this->GetTags();
return $this->tagString;
}
// set $people as an array of people
public function GetPeople()
{
global $DB;
if (!isset ($this->people))
{
$peopleString = '';
if (!empty ($_GET['people']))
{
$people = explode(',', $_GET['people']);
$size = count($people);
for($i = 0; $i < $size; $i++)
{
$people[$i] = $DB->FilterString(trim($people[$i]));
if (empty($people[$i]))
unset($people[$i]);
else
$peopleString .= ($peopleString ? ',' : null).$people[$i];
}
$this->people = $people;
}
else
$this->people = array();
$this->peopleString = $peopleString;
}
return $this->people;
}
// set $peopleString
public function GetPeopleString()
{
if (!isset ($this->peopleString))
$this->GetPeople();
return $this->peopleString;
}
public function GetItemNumber()
{
if (!isset($this->itemNumber))
{
if ($id = $this->GetId())
{
global $DB;
$whereQuery = $this->GetWhereQuery(false);
$query = $whereQuery.($whereQuery ? ' AND ' : null).
"($this->orderByField ".('ASC' == $this->orderByDirection ? '<' : '>')." (SELECT $this->orderByField FROM ".ITEM_TABLE." WHERE ItemId= '$id') OR ($this->orderByField = (SELECT $this->orderByField FROM ".ITEM_TABLE." WHERE ItemId= '$id') AND ItemId <= '$id'))";
$number = $DB->GetCount(ITEM_TABLE, $query);
}
else
$number = 0;
$this->itemNumber = $number;
}
return $this->itemNumber;
}
public function GetPageNumber()
{
if (!isset($this->pageNumber))
{
global $DB;
$pageNumber = (int)@$_GET['n'];
if (!$pageNumber and $id = $this->GetId())
{
$count = $this->GetItemNumber();
$pageNumber = ceil($count/($this->numColumns*$this->numRows));
}
else
if (1 > $pageNumber)
$pageNumber = ceil($this->GetTotalItems()/($this->numColumns*$this->numRows));
$this->pageNumber = $pageNumber;
}
return $this->pageNumber;
}
public function GetTotalItems()
{
if (!isset($this->totalItems))
{
global $DB;
$this->totalItems = $DB->GetCount(ITEM_TABLE, $this->GetWhereQuery(false));
}
return $this->totalItems;
}
public function GetTotalPages()
{
if (!isset($this->totalPages))
{
global $DB;
$count = $this->GetTotalItems();
$this->totalPages = ceil($count/($this->numColumns*$this->numRows));
}
return $this->totalPages;
}
public function GetPageNumberLinks($max = 5)
{
$firstPage = 1;
$lastPage = $this->GetTotalPages();
if (0 == $lastPage)
return '0 Pages';
$currentPage = $this->GetPageNumber();
if (1 > $currentPage)
$currentPage = 0;
if ($currentPage > $lastPage)
$currentPage = $lastPage+1;
if ($currentPage > $max)
$startPage = $currentPage-$max;
else
$startPage = 1;
if ($currentPage < $lastPage-$max)
$endPage = $currentPage+$max;
else
$endPage = $lastPage;
$url = $this->GetExtendedURL().'n';
if (1 == $currentPage)
$html = 'First Page';
else
$html = "<a href='$url=1&'>First Page</a>";
for ($i = $startPage; $i <= $endPage; $i++)
if ($i == $currentPage)
$html .= " - [<b>$i</b>]";
else
$html .= " - <a href='$url=$i&'>$i</a>";
if ($lastPage == $currentPage)
$html .= ' - Last Page';
else
$html .= " - <a href='$url=$lastPage&'>Last Page</a>";
return $html;
}
public function GetPrevPageLink()
{
$prevNumber = $this->GetPageNumber()-1;
if (0 < $prevNumber)
$html = '<a href="'.$this->GetExtendedURL().'n='.$prevNumber.'&">Previous Page</a>';
else
$html = 'Previous Page';
return $html;
}
public function GetNextPageLink()
{
$nextNumber = $this->GetPageNumber()+1;
if ($this->GetTotalPages() >= $nextNumber)
$html = '<a href="'.$this->GetExtendedURL().'n='.$nextNumber.'&">Next Page</a>';
else
$html = 'Next Page';
return $html;
}
public function GetTitle()
{
if (!$this->title)
{
$title = MAIN_TITLE;
if ($this->GetUser())
$title .= ': '.$this->GetUser();
if ($this->GetLocation())
$title .= ': '.$this->GetLocation();
if ($this->GetDateString())
$title .= ': '.$this->GetDateString();
if ($this->GetTags())
$title .= ': '.$this->GetTagString();
$this->title = $title;
}
return $this->title;
}
public function GetExtendedURL($page = null)
{
if (null != $page or !isset($this->extendedURL))
{
$url = (($string = $this->GetURLDate() and $page != 'Dates') ? 'date='.$string : null);
$url .= (($string = $this->GetUser() and $page != 'Users') ? ($url ? '&' : null).'user='.$string : null);
$url .= (($string = $this->GetLocation() and $page != 'Locations') ? ($url ? '&' : null).'loc='.$string : null);
$url .= (($string = $this->GetTagString() and $page != 'Tags') ? ($url ? '&' : null).'tags='.$string : null);
$url .= (($string = $this->GetPeopleString()) ? ($url ? '&' : null).'people='.$string : null);
$url .= ((THUMB_COLS != $this->numColumns) ? ($url ? '&' : null).'cols='.$this->numColumns : null);
$url .= ((THUMB_ROWS != $this->numRows) ? ($url ? '&' : null).'rows='.$this->numRows : null);
$url = PAGE_URL.($url ? $url.'&' : '');
if (null == $page)
$this->extendedURL = $url;
}
else
$url = $this->extendedURL;
return $url;
}
public function SetExtendedURL($url)
{
$this->extendedURL = $url;
}
// --- Queries: full and partial ---
public function GetIdQuery()
{
if (!isset ($this->idQuery))
$this->GetId();
return $this->idQuery;
}
public function GetDateQuery()
{
if (!isset ($this->dateQuery))
$this->GetURLDate();
return $this->dateQuery;
}
public function GetLocationQuery()
{
if (!isset ($this->locationQuery))
$this->GetLocation();
return $this->locationQuery;
}
public function GetUserQuery()
{
if (!isset ($this->userQuery))
$this->GetUser();
return $this->userQuery;
}
public function GetTagQuery()
{
if (!isset ($this->tagQuery))
{
$query = '';
$tags = $this->GetTags();
$i = 0;
foreach($tags as $tag)
$query .= ($i++ > 0 ? ' AND ' : null).
" 0 < (SELECT count(*) FROM ".
TAG_MAP_TABLE.", ".TAG_TABLE.
" WHERE Tag='$tag' AND TagIdFk=TagId AND ItemIdFk=ItemId)";
$this->tagQuery = $query;
}
return $this->tagQuery;
}
public function GetPeopleQuery()
{
if (!isset ($this->peopleQuery))
{
$query = '';
$people = $this->GetPeople();
$i = 0;
foreach($people as $person)
$query .= ($i++ > 0 ? ' AND ' : null).
" 0 < (SELECT count(*) FROM ".
PEOPLE_MAP_TABLE.", ".PEOPLE_TABLE.
" WHERE PeopleName='$person' AND PeopleIdFk=PeopleId AND ItemIdFk=ItemId)";
$this->peopleQuery = $query;
}
return $this->peopleQuery;
}
/**
* @since 2.1.2
*/
public function SetOrderByField($str)
{
$this->orderByField = $str;
}
public function GetOrderByField()
{
return $this->orderByField;
}
public function SetOrderByDirection($str)
{
$this->orderByDirection = $str;
}
public function GetOrderByDirection()
{
return $this->orderByDirection;
}
/**
* @since 2.1.2
*/
public function SetWhereQuery($query)
{
$this->whereQuery = $query;
}
/**
* @version 2.2
* @param true/false if "WHERE" should be included in the returned string
* @return string that includes all URL parameters in the where query and
* user permissions
* @since 2.0
*/
public function GetWhereQuery($includeWhere = true)
{
if (!isset ($this->whereQuery))
{
$whereQuery = $this->GetUserQuery();
$query = $this->GetDateQuery();
if ($whereQuery and $query)
$whereQuery .= ' AND ';
$whereQuery .= $query;
$query = $this->GetLocationQuery();
if ($whereQuery and $query)
$whereQuery .= ' AND ';
$whereQuery .= $query;
$query = $this->GetTagQuery();
if ($whereQuery and $query)
$whereQuery .= ' AND ';
$whereQuery .= $query;
$query = $this->GetPeopleQuery();
if ($whereQuery and $query)
$whereQuery .= ' AND ';
$whereQuery .= $query;
if ($whereQuery and USER_CAN_VIEW_QUERY)
$whereQuery .= ' AND ';
$whereQuery .= USER_CAN_VIEW_QUERY;
$this->whereQuery = $whereQuery;
}
//echo $this->whereQuery.'<br><br>';
if ($this->whereQuery and $includeWhere)
return ' WHERE '.$this->whereQuery;
return $this->whereQuery;
}
/**
* Create a query string to get all info required to generate thumbnails for
* the current page
*
* @version 2.1.2
* @since 2.0
*/
public function GetThumbnailQuery()
{
if (!isset ($this->thumbnailQuery))
{
$this->thumbnailQuery = 'SELECT ItemId, ItemPath, ItemFilename,' .
' ItemDate, ItemTime, ItemTypeId, ItemDescription, ItemHasComments' .
' FROM ' . ITEM_TABLE . $this->GetWhereQuery().
" ORDER BY $this->orderByField $this->orderByDirection, ItemId ASC" .
' LIMIT '.($this->numColumns*$this->numRows).
' OFFSET '.($this->numColumns*$this->numRows*($this->GetPageNumber()-1));
}
return $this->thumbnailQuery;
}
public function GetPrevItemQuery($orderByValue)
{
$whereQuery = $this->GetWhereQuery();
if ('DESC' == $this->orderByDirection)
{
$direction = 'ASC';
$gtlt = '>';
}
else
{
$direction = 'DESC';
$gtlt = '<';
}
$query = 'SELECT ItemId, ItemPath, ItemFilename, ItemTypeId' .
' FROM ' . ITEM_TABLE . $whereQuery.($whereQuery ? ' AND ' : ' WHERE ') .
" ($this->orderByField $gtlt '$orderByValue' OR ($this->orderByField = '$orderByValue' AND ItemId < '$this->id'))" .
" ORDER BY $this->orderByField $direction, ItemId DESC" .
' LIMIT 1';
return $query;
}
public function GetNextItemQuery($orderByValue)
{
$whereQuery = $this->GetWhereQuery();
if ('DESC' == $this->orderByDirection)
{
$direction = 'DESC';
$gtlt = '<';
}
else
{
$direction = 'ASC';
$gtlt = '>';
}
$query = 'SELECT ItemId, ItemPath, ItemFilename, ItemTypeId' .
' FROM ' . ITEM_TABLE . $whereQuery.($whereQuery ? ' AND ' : ' WHERE ') .
" ($this->orderByField $gtlt '$orderByValue' OR ($this->orderByField = '$orderByValue' AND ItemId > '$this->id'))" .
" ORDER BY $this->orderByField $direction, ItemId ASC" .
' LIMIT 1';
return $query;
}
}
?>