comments plus input validation
comments plus input validation

--- a/include/classes/anyfivecardpokerhand.php
+++ b/include/classes/anyfivecardpokerhand.php
@@ -33,6 +33,12 @@
 		$this->getDeckOfCards ();
 	}
 
+	/**
+	 * Generates a deck of cards
+	 *
+	 * @return array
+	 */
+
 	private function generateDeckOfCards () {
 		foreach (self::$cards as $card) {
 			foreach (self::$types as $type) {
@@ -43,12 +49,25 @@
 		return $this->deckOfCards;
 	}
 
+	/**
+	 * Fetches the deck of cards
+	 *
+	 * @return array
+	 */
+
 	public function getDeckOfCards () {
 		if ( count($this->deckOfCards) == 0 ) {
 			return $this->generateDeckOfCards ();
 		}
 		return $this->deckOfCards;
 	}
+
+	/**
+	 * Generates all card combinations and inserts them in the database
+	 *
+	 * @param bool $debug shows card combinations total while generating
+	 * @return void
+	 */
 
 	public function generateAnyFiveCardPokerHand ($debug=false) {
 		$this->debug = $debug;
@@ -89,6 +108,13 @@
 		}
 	}
 
+	/**
+	 * Determines the card combinations like a pair, two pair, full house, flash, four of a kind
+	 *
+	 * @param array $combo five cards
+	 * @return int
+	 */
+
 	public function getCardCombination ($combo) {
 		$result = array ();
 		foreach ( self::$cards as $card ) {
@@ -102,20 +128,20 @@
 				$result[] = $r;
 			}
 		}
-		if ( count ($result) == 5 ) {
+		if ( count ($result) == 5 ) { // nothing
 			return 0;
-		} elseif ( count ($result) == 4 ) {
+		} elseif ( count ($result) == 4 ) { // four of a kind
 			return 1;
 		} elseif ( count ($result) == 3 ) {
-			if ( count ($result[0])>=3 || count ($result[1])>=3 || count ($result[2])>=3 ) {
+			if ( count ($result[0])>=3 || count ($result[1])>=3 || count ($result[2])>=3 ) { // flash
 				return 3;
-			} else {
+			} else { // two pairs
 				return 2;
 			}
 		} elseif ( count ($result) == 2 ) {
-			if ( count ($result[0])==4 || count ($result[1])==4 ) {
+			if ( count ($result[0])==4 || count ($result[1])==4 ) { // four of a kind
 				return 4;
-			} elseif ( (count ($result[0])==2 || count ($result[1])==3) || (count ($result[0])==3 || count ($result[1])==2) ){
+			} elseif ( (count ($result[0])==2 || count ($result[1])==3) || (count ($result[0])==3 || count ($result[1])==2) ){ // full house
 				return 32;
 			} else {
 				return 10;

--- a/include/classes/dbconnection.php
+++ b/include/classes/dbconnection.php
@@ -16,13 +16,33 @@
 		$this->dbConnect ();
 	}
 
+	/**
+	 * Connects to the database
+	 *
+	 * @return void
+	 */
+
 	private function dbConnect () {
 		$this->db = new mysqli($this->dbData['sql_host'], $this->dbData['sql_user'], $this->dbData['sql_pass'], $this->dbData['sql_db'], $this->dbData['sql_port']);
 	}
 
+	/**
+	 * Process sql query
+	 *
+	 * @param string $sqlQuery database sql query
+	 * @return true|false|result
+	 */
+
 	public function dbQuery ($sqlQuery) {
 		return $this->dbQueryId = $this->db->query ($sqlQuery);
 	}
+
+	/**
+	 * Fetches result from database
+	 *
+	 * @param result $queryId sql query result
+	 * @return array
+	 */
 
 	public function dbFetchArray ($queryId) {
 		if ($queryId == "") {

--- a/include/classes/display.php
+++ b/include/classes/display.php
@@ -1,8 +1,7 @@
 <?php
 
 /**
- * Any five card poker hand display
- *
+ * Any five card poker hand display interface
  *
  * @author      Razvan Stanga <git@razvi.ro>
  */
@@ -16,22 +15,32 @@
 
 		require (appRoot."include/lib/Smarty.class.php");
 		$this->smarty = new Smarty;
-		$this->smarty->compile_check 	= true;
-		$this->smarty->debugging 		= false;
-		$this->smarty->template_dir   	= appRoot."style/tpl";
-		$this->smarty->compile_dir    	= appRoot."style/tpl_c";
+		$this->smarty->compile_check	= true;
+		$this->smarty->debugging		= false;
+		$this->smarty->template_dir		= appRoot."style/tpl";
+		$this->smarty->compile_dir		= appRoot."style/tpl_c";
 	}
+
+	/**
+	 * Collects all data needes to generate the page and shows the page
+	 *
+	 * @param int $page current page
+	 * @param int $hand current hand filter like for of a kind
+	 * @param string $card current card filter like 2 hearts
+	 * @param bool $ajax returnes ony the content with ajax
+	 * @return void
+	 */
 
 	public function page ($page, $hand="", $card="", $ajax=false) {
 
-		$sqlFilter = "WHERE 1=1 ";
+		$sqlFilter = "WHERE 1=1";
 		$params = "";
 		if ($hand != "") {
-			$sqlFilter .= "AND `hand`='".intval ($hand)."'";
-			$params .= "&hand=".intval ($hand);
+			$sqlFilter .= " AND `hand`='".$hand."'";
+			$params .= "&hand=".$hand;
 		}
 		if ($card != "") {
-			$sqlFilter .= "AND FIND_IN_SET('".addslashes ($card)."', `combo`)";
+			$sqlFilter .= " AND FIND_IN_SET('".$card."', `combo`)";
 			$params .= "&card=".urlencode ($card);
 		}
 
@@ -67,6 +76,12 @@
 		}
 	}
 
+	/**
+	 * Stats builder. Fetches info from database like how many full hose combinations are there
+	 *
+	 * @return void
+	 */
+
 	private function buildStats () {
 		$stats = array ();
 		$q = $this->dbConnection->dbQuery ("SELECT COUNT(*) as count, hand FROM `cards` GROUP BY `hand`");
@@ -76,6 +91,18 @@
 		}
 		$this->smarty->assign ("stats", $stats);
 	}
+
+	/**
+	 * Pagination. Returnes an array with the linkes for pagination to be used in template
+	 *
+	 * @param int $totalitems total items
+	 * @param string $link pagination link
+	 * @param int $page current page
+	 * @param int $offset how many items per page
+	 * @param string $pagename GET url param for page
+	 * @param string $params extra params to add to the pagination link
+	 * @return array
+	 */
 
 	private function pagination ($totalitems, $link, $page, $offset, $pagename='page', $params=null) {
 		$pagination = array ();
@@ -130,6 +157,15 @@
 		return $pagination;
 	}
 
+	/**
+	 * Pagination data calculator. Returnes data to be used in sql query and in pagination
+	 *
+	 * @param int $page current page
+	 * @param int $total total items
+	 * @param int $offset how many items per page
+	 * @return array
+	 */
+
 	private function paginationData ($page, $total, $offset=10) {
 		$offset = $offset ? $offset : 1;
 		$ret = array ();

file:a/index.php -> file:b/index.php
--- a/index.php
+++ b/index.php
@@ -8,10 +8,11 @@
 
 $display = new Display;
 
-switch ( $_GET['case'] ) {
-	default:
-		$display->page ($_GET['page'], $_GET['hand'], $_GET['card'], $_GET['ajax']);
-	break;
-}
+$page = filter_var ($_GET['page'], FILTER_VALIDATE_INT);
+$hand = filter_var ($_GET['hand'], FILTER_VALIDATE_INT);
+$card = filter_var ($_GET['card'], FILTER_FLAG_STRIP_HIGH);
+$ajax = filter_var ($_GET['ajax'], FILTER_VALIDATE_BOOLEAN);
+
+$display->page ($page, $hand, $card, $ajax);
 
 ?>

comments