@damonsk; * @date: 17/02/2012 * * -- Updates below this line (newest first) -- * 17/02/2012 - New checkLicense function. * 11/01/2012 - Refactored - Removed legacy database code * */ class General { private $metaInfo; private $pageKey; private $pageClean; private $list; private $debug; function db() { static $conn; if (!isset($conn)) { try { $conn = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USERNAME, DB_PASSWORD); } catch(PDOException $ex){ $this->log($ex->getMessage()); } // $this->checkLicense(); // Enable this if required. } return $conn; } function checkLicense(){ $lastCheck = $this->config()->CURRENT_DATE; if (!$lastCheck || ($lastCheck < strtotime("today") || $lastCheck > strtotime("+2 days"))) { $data = @json_decode($this->fetchPage("http://apis.sizzlecreative.co.uk/check.php", true, "site=solfex")); if ($data->status == 'OK') { $this->config()->CURRENT_DATE = time(); $stm = $this->db()->prepare("UPDATE " . DB_PREFIX . "configuration SET configValue = :time WHERE configParam = 'CURRENT_DATE'"); $stm->bindValue(":time", time()); $stm->execute(); } else { die("Unexpected Server Error - 101"); } } } /** * Create config singleton */ function config() { static $config; if (!isset($config)) { $config = new Configuration(); } return $config; } function dateDiff($dt1, $dt2, $timeZone = 'GMT'){ $tZone = new DateTimeZone($timeZone); $dt1 = new DateTime($dt1, $tZone); $dt2 = new DateTime($dt2, $tZone); $ts1 = $dt1->format('Y-m-d'); $ts2 = $dt2->format('Y-m-d'); $diff = abs(strtotime($ts1)-strtotime($ts2)); $diff/= 3600*24; return $diff; } function makeSortUrl($val){ $match = '^sort=\d{1,}^'; if(preg_match($match, $_SERVER['QUERY_STRING'])){ return preg_replace($match, 'sort='.$val, $_SERVER['REQUEST_URI']); } else return $_SERVER['REQUEST_URI']. $this->htmlspecialchars((($_SERVER['QUERY_STRING']) ? '&' : '?')) .'sort='.$val; } function currentSort($default, $secondary){ $match = '^sort='.$default.'^'; if(preg_match($match, $_SERVER['QUERY_STRING'])) return $secondary; else return $default; } function __construct($debug = false){ $this->debug = true; } function toTime($time){ $temp = explode(' ', $time); $date = explode('/', $temp[0]); $time = explode(':', $temp[1]); return mktime($time[0], $time[1], $time[2], $date[1], $date[0], $date[2]); } function fromTime($time){ return date('d-m-Y H:i:s', $time); } function postToTwitter($tweetData){ if($this->isTwitterAvailable()){ $connection = new TwitterOAuth( $this->config()->TW_CONSUMER_KEY, $this->config()->TW_CONSUMER_SECRET, $this->config()->TW_OAUTH_TOKEN, $this->config()->TW_OAUTH_SECRET ); $content = $connection->get('account/verify_credentials'); $connection->post('statuses/update', array('status' => $tweetData)); } } function isTwitterAvailable(){ if( $this->config()->TW_CONSUMER_KEY && $this->config()->TW_CONSUMER_SECRET && $this->config()->TW_OAUTH_TOKEN && $this->config()->TW_OAUTH_SECRET) return true; else return false; } function make_bitly_url($url,$format = 'json',$version = '2.0.1'){ $login = $this->config()->BITLY_LOGIN; $appkey = $this->config()->BITLY_APP_KEY; //create the URL $bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format; echo $bitly . '
'; //get the url //could also use cURL here $response = $this->fetchPage($bitly); echo $response; //parse depending on desired format if(strtolower($format) == 'json') { $json = @json_decode($response,true); return $json['results'][$url]['shortUrl']; } else //xml { $xml = simplexml_load_string($response); return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash; } } function simpleXMLToArray($xml, $flattenValues=true, $flattenAttributes = true, $flattenChildren=true, $valueKey='@value', $attributesKey='@attributes', $childrenKey='@children'){ $return = array(); if(!($xml instanceof SimpleXMLElement)){return $return;} $name = $xml->getName(); $_value = trim((string)$xml); if(strlen($_value)==0){$_value = null;}; if($_value!==null){ if(!$flattenValues){$return[$valueKey] = $_value;} else{$return = $_value;} } $children = array(); $first = true; foreach($xml->children() as $elementName => $child){ $value = $this->simpleXMLToArray($child, $flattenValues, $flattenAttributes, $flattenChildren, $valueKey, $attributesKey, $childrenKey); if(isset($children[$elementName])){ if($first){ $temp = $children[$elementName]; unset($children[$elementName]); $children[$elementName][] = $temp; $first=false; } $children[$elementName][] = $value; } else{ $children[$elementName] = $value; } } if(count($children)>0){ if(!$flattenChildren){$return[$childrenKey] = $children;} else{$return = array_merge($return,$children);} } $attributes = array(); foreach($xml->attributes() as $name=>$value){ $attributes[$name] = trim($value); } if(count($attributes)>0){ if(!$flattenAttributes){$return[$attributesKey] = $attributes;} else{$return = array_merge($return, $attributes);} } return $return; } public function setSplash($text, $error = false){ $_SESSION['splash'] = array('text' => $text, 'error' => $error); } public function getSplash(){ //echo($this->echoArray($_SESSION)); if(($splash = $_SESSION['splash'])){ unset($_SESSION['splash']); return (($splash['error']) ? $this->errorWrapper($splash['text']) : $this->confirmationWrapper($splash['text'])); } } private function confirmationWrapper($text){ return '
×

Confirmation Message

'; } private function errorWrapper($text){ return '
×

Error Message

'; } /** * Use curl to fetch a remote page * @param unknown_type $url * @param unknown_type $ispost * @param unknown_type $params * @param unknown_type $auth */ function fetchPage($url, $ispost = false, $params = false, $auth = false){ if(!$ispost && $params) $url .= '?' . $params; //if not post and params (ie, if it's a get) $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); if(is_array($auth)){ curl_setopt($ch, CURLOPT_USERPWD, $auth['username'] . ':' . $auth['password']); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); } if($ispost){ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); } $result = curl_exec($ch); curl_close($ch); return $result; } public function fieldComment($comment){ return ''.$this->htmlspecialchars($comment).''; } public function requiredFieldMark(){ return '*'; } function getFileContents($file) { $resHandle = @fopen($file, "r"); $strContent = @fread($resHandle, @filesize($file)); @fclose($resHandle); return $strContent; } public function limitString($string, $length){ if(strlen($string)>$length){ return $string = substr($string, 0, $length) . '...'; } else return $string; } /** * Return the post value if post has been sent. * @param String $post * @param String $not * @return */ function postornot($post, $not){ if(isset($post)) return $post; else return $not; } /** * Return htmlspecialchars string * @param String $string * @return String */ public function htmlspecialchars($string){ return htmlspecialchars($string); } /** * Show preformmated array, good for debugging. * @param object $array * @return String */ public function echoArray($array){ if($this->config()->SHOW_DEBUG) return '
' . print_r($array, TRUE) . '
'; } public function log($message){ if($this->debug) $this->echoArray(array('ERROR' => $message)); trigger_error($message); } public function fileNotFound(){ return '

Whoops!Unable to find your requested file

'; } /** * * Takes an array of queries and executes. * @param array $q * @deprecated - Legacy Code */ protected function batchQuery($q){ die($this->echoArray(array('Deprecated function' => debug_backtrace()))); //foreach($q as $query){ // $this->query($query); //} } public function getShortDomain(){ return DOMAIN; } public function redirect($url){ header('Location: '.$url); exit; // important } /** * Check session details to see if a user is logged in. * @return */ function isLoggedIn() { //echo($this->echoArray($_SESSION)); return (($_SESSION['user']['userID']) ? TRUE : FALSE); } } ?>