Permalink
Browse files

Add statistics to the system

Store everything.
  • Loading branch information...
1 parent 26ad423 commit a4bbc268331f6867fab25787537faff4b4c46033 @michaelw90 committed Apr 13, 2012
Showing with 130 additions and 5 deletions.
  1. +106 −0 Stats.class.php
  2. +24 −5 image.php
View
106 Stats.class.php
@@ -0,0 +1,106 @@
+<?php
+/**
+ * Simple Stats Class
+ * @author Michael Wright @MichaelW90
+ */
+
+class Stats {
+
+ /**
+ * @var string
+ */
+ private $_file = 'baselineStats.store';
+
+ /**
+ * @var string
+ */
+ private $_path = '';
+
+ /**
+ * construct()
+ */
+ public function __construct(){
+ $this -> _path = $_SERVER['DOCUMENT_ROOT'] . '/../' . $this -> _file;
+
+ $createCheck = $this -> checkCreate();
+ if($createCheck !== TRUE)
+ var_dump($createCheck);
+ }
+
+ /**
+ * store()
+ * @param array $tore
+ * @return boolean|string
+ */
+ public function store($store){
+ $result = $this -> getStats();
+ if($result === FALSE){
+ return 'Error getting stats';
+ }else{
+ $result = $result;
+ }
+ $result['total']++;
+
+ foreach($store as $key => $item){
+ if(!isset($result[$key][$item])){
+ $result[$key][$item] = 0;
+ }
+ $result[$key][$item]++;
+ }
+
+ $this -> putStats($result);
+
+ return TRUE;
+ }
+
+ /**
+ * checkCreate()
+ * @return boolean | string
+ */
+ private function checkCreate(){
+ $template = array(
+ 'total' => 0
+ );
+ if(!file_exists($this -> _path)){
+ if(!$this -> putStats($template)){
+ return 'Error calling file_put_contents()';
+ }
+ }else if(!$result = $this -> getStats(false)){
+ if($result == ''){
+ return $this -> putStats($template);
+ }
+ }
+ return TRUE;
+ }
+
+ /**
+ * getStats()
+ * @param boolean $unserialize
+ * @return array|string
+ */
+ public function getStats($unserialize = true){
+ if(!$result = file_get_contents($this -> _path)){
+ return FALSE;
+ }
+ if($unserialize)
+ return unserialize($result);
+ else
+ return $result;
+ }
+
+ /**
+ * putStats()
+ * @param array $array
+ * @return boolean
+ */
+ private function putStats($array){
+ $array = serialize($array);
+ if(!$result = @file_put_contents($this -> _path, $array)){
+ return FALSE;
+ }
+ return TRUE;
+ }
+
+}
+
+
View
29 image.php
@@ -1,7 +1,7 @@
<?php
/**
* Baseline Image Creator
- * @author Michael Wright <michael@wserver.co.uk>
+ * @author Michael Wright @MichaelW90
*/
// Height settings
@@ -10,9 +10,12 @@
'currentHeight' => 10, // default to 10px height
'minHeight' => 1 // ensure baseline is bigger than 1px
);
+$store = array();
// Check for provided hex colour
if(isset($_GET['hex'])){
+ // Add hex to list of things to store in stats
+ $store['hex'] = $_GET['hex'];
// Split hex colour into parts
preg_match_all("/[a-f0-9]{2}/i", $_GET['hex'], $colour);
// Check we have a 3 parts to the hex
@@ -30,19 +33,35 @@
// Check for provided R, G or B values
}else if(isset($_GET['r']) && $_GET['r'] <= 255 && $_GET['r'] >= 0 && isset($_GET['g']) && $_GET['g'] <= 255 && $_GET['g'] >= 0 && isset($_GET['b']) && $_GET['b'] <= 255 && $_GET['b'] >= 0){
$colour = array($_GET['r'], $_GET['g'], $_GET['b']);
-
+ // Setup array of what to store in stats
+ $store = array(
+ 'r' => $_GET['r'],
+ 'g' => $_GET['g'],
+ 'b' => $_GET['b']
+ );
// Use default colour if all fail
}else{
$colour = $settings['colour'];
}
+// Default to 10 px height if they set no height.
+$settings['currentHeight'] = (isset($_GET['height']) && $_GET['height'] > $settings['minHeight']? $_GET['height'] : $settings['currentHeight']);
+if($settings['currentHeight'] != $_GET['height']){
+ // If the height is not the default height, store it.
+ $store['height'] = $settings['currentHeight'];
+}
+
+// Stats class include
+if(!include_once('./Stats.class.php'))
+ die('Error including Stats.class.php');
+// Instantiate instance of class
+$stats = new Stats();
+// Store the stats!
+$stats -> store($store);
// Set the content-type to png
header("Content-type: image/png");
-// Default to 10 px height if they set no height.
-$settings['currentHeight'] = (isset($_GET['height']) && $_GET['height'] > $settings['minHeight']? $_GET['height'] : $settings['currentHeight']);
-
// Create an image at the right dimensions.
$im = imagecreate(4, $settings['currentHeight']);

0 comments on commit a4bbc26

Please sign in to comment.