first draft, only one (main) page
This commit is contained in:
265
inc/resume.php
Normal file
265
inc/resume.php
Normal file
@@ -0,0 +1,265 @@
|
||||
<?php
|
||||
|
||||
class Resume extends PhpObject
|
||||
{
|
||||
//Resume constants
|
||||
const VERSION = '1.0.0';
|
||||
const DEFAULT_PAGE = 'home';
|
||||
const LOG_FILE = 'log';
|
||||
const PIC_PATH = 'images/pic.png';
|
||||
const PUBLIC_KEY_LENGTH = 13;
|
||||
const MAX_REQUEST_TIME = 10;
|
||||
|
||||
//Mask constants
|
||||
const RANDOM_TAG_PREFIX = 'uniqid_';
|
||||
const LOC_API_KEY = Settings::LOC_API_KEY;
|
||||
|
||||
//Variables
|
||||
private $oClassManagement;
|
||||
private $sLang;
|
||||
private $oTranslator;
|
||||
|
||||
private $asMasks;
|
||||
private $iLoadTime;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param ClassManagement $oClassManagement
|
||||
* @param string $sLang
|
||||
*/
|
||||
public function __construct($oClassManagement, $sLang='')
|
||||
{
|
||||
parent::__construct(__CLASS__, Settings::DEBUG);
|
||||
$this->oClassManagement = $oClassManagement;
|
||||
|
||||
//Browser <> PHP <> MySql synchronization
|
||||
date_default_timezone_set(Settings::TIMEZONE);
|
||||
ini_set('default_charset', Settings::TEXT_ENC);
|
||||
header('Content-Type: text/html; charset='.Settings::TEXT_ENC);
|
||||
mb_internal_encoding(Settings::TEXT_ENC);
|
||||
mb_http_output(Settings::TEXT_ENC);
|
||||
mb_http_input(Settings::TEXT_ENC);
|
||||
mb_language('uni');
|
||||
mb_regex_encoding(Settings::TEXT_ENC);
|
||||
|
||||
$this->oClassManagement->incClass('mask');
|
||||
$this->oClassManagement->incClass('translator');
|
||||
|
||||
$this->setLanguage($sLang);
|
||||
$this->setMasks();
|
||||
$this->setPageMasks(array('index'));
|
||||
$this->setLoadTime();
|
||||
$this->oTranslator = new Translator($this->getLanguage());
|
||||
}
|
||||
|
||||
private function getLanguage()
|
||||
{
|
||||
return $this->sLang;
|
||||
}
|
||||
|
||||
private function setLanguage($sLang='')
|
||||
{
|
||||
if($sLang!='') $this->sLang = $sLang;
|
||||
else
|
||||
{
|
||||
//$_SERVER['REMOTE_ADDR'] = '193.106.178.41'; //Test Spain
|
||||
//$_SERVER['REMOTE_ADDR'] = '160.92.167.193'; //Test France
|
||||
//$_SERVER['REMOTE_ADDR'] = '74.125.230.216'; //Test US
|
||||
$asIpInfo = json_decode(file_get_contents('http://api.ipinfodb.com/v3/ip-country/?key='.self::LOC_API_KEY.'&format=json&ip='.$_SERVER['REMOTE_ADDR']), true);
|
||||
if($asIpInfo['statusCode'] == 'OK') $this->sLang = $asIpInfo['countryCode'];
|
||||
}
|
||||
}
|
||||
|
||||
private function setLoadTime()
|
||||
{
|
||||
$this->iLoadTime = time();
|
||||
}
|
||||
|
||||
private function getLoadTime()
|
||||
{
|
||||
return $this->iLoadTime;
|
||||
}
|
||||
|
||||
private function setMasks()
|
||||
{
|
||||
//List all available masks
|
||||
$asMaskPaths = glob(Mask::getMaskFile('*'));
|
||||
$this->asMasks = array_map('basename', $asMaskPaths, array_fill(1, count($asMaskPaths), Mask::MASK_EXT));
|
||||
}
|
||||
|
||||
private function setPageMasks($asNonMasks)
|
||||
{
|
||||
//Exclude structure pages
|
||||
foreach($this->asMasks as $sMask)
|
||||
{
|
||||
$iIndex = array_search($sMask, $asNonMasks);
|
||||
if($iIndex === false) $this->asPageMasks[] = $sMask;
|
||||
}
|
||||
}
|
||||
|
||||
private function isAccessiblePage($sPageName)
|
||||
{
|
||||
return in_array($sPageName, $this->asPageMasks);
|
||||
}
|
||||
|
||||
public function getPage($sPageName, $bForceNoJson=false, $asForceTags=array())
|
||||
{
|
||||
$oResult = null;
|
||||
if(in_array($sPageName, $this->asMasks))
|
||||
{
|
||||
//Create Mask
|
||||
$oMask = new Mask($sPageName);
|
||||
|
||||
//Fill in with translated texts
|
||||
$asTags = $oMask->getTags();
|
||||
$iRandPrefixLen = mb_strlen(self::RANDOM_TAG_PREFIX);
|
||||
foreach($asTags as $sTag)
|
||||
{
|
||||
$sTagValue = '';
|
||||
if(array_key_exists($sTag, $asForceTags))
|
||||
{
|
||||
$sTagValue = $asForceTags[$sTag];
|
||||
}
|
||||
elseif(mb_substr($sTag, 0, $iRandPrefixLen) == self::RANDOM_TAG_PREFIX)
|
||||
{
|
||||
$sTagValue = uniqid();
|
||||
}
|
||||
else
|
||||
{
|
||||
$sTagValue = $this->oTranslator->getTranslation($sTag);
|
||||
}
|
||||
$oMask->setTag($sTag, $sTagValue);
|
||||
}
|
||||
|
||||
//Page specific postprocessing
|
||||
$asVars = array();
|
||||
switch($sPageName)
|
||||
{
|
||||
case 'index':
|
||||
$oMask->setTag('encoding', Settings::TEXT_ENC);
|
||||
break;
|
||||
}
|
||||
|
||||
//Get final mask
|
||||
$oResult = $oMask->getMask();
|
||||
|
||||
//Add title if this page is one of the site page
|
||||
if($this->isAccessiblePage($sPageName))
|
||||
{
|
||||
$oResult = array('title'=>$sPageName, 'page'=>$oResult, 'vars'=>$asVars);
|
||||
}
|
||||
}
|
||||
return (!$bForceNoJson && $this->isAccessiblePage($sPageName))?self::jsonExport($oResult):$oResult;
|
||||
}
|
||||
|
||||
public function getJavascript()
|
||||
{
|
||||
//Build picture key
|
||||
$sPublicKey = uniqid();
|
||||
$sSecretKey = $this->getLoadTime();
|
||||
list($iWidth, $iHeight) = getimagesize(self::PIC_PATH);
|
||||
file_put_contents(self::LOG_FILE, $sPublicKey.$sSecretKey."\n", FILE_APPEND);
|
||||
|
||||
//Display javascript functions
|
||||
$asResult = array();
|
||||
$asResult[] = "var cConfigPage = 'index.php'";
|
||||
$asResult[] = "var s = '$sPublicKey';";
|
||||
$asResult[] = "var iPicWidth = $iWidth;";
|
||||
$asResult[] = "var iPicHeight = $iHeight;";
|
||||
$asResult[] = file_get_contents('jquery/jquery.functions.js');
|
||||
return implode("\n", $asResult);
|
||||
}
|
||||
|
||||
public function getPic($sSerial)
|
||||
{
|
||||
if($this->checkSerial($sSerial))
|
||||
{
|
||||
header('Content-Type: image/jpeg');
|
||||
return file_get_contents(self::PIC_PATH);
|
||||
}
|
||||
else
|
||||
{
|
||||
header('HTTP/1.1 403 Forbidden');
|
||||
}
|
||||
}
|
||||
|
||||
public function sendEmail($sName, $sEmail, $sSubject, $sMsg)
|
||||
{
|
||||
$sResult = '';
|
||||
if($sName!='' && $sEmail!='' && $sSubject!='' && $sMsg!='')
|
||||
{
|
||||
//Message
|
||||
$sHtmlMessage = 'From: '.$sName."<br />".
|
||||
'Email: '.$sEmail."<br /><br />".
|
||||
'Subject: '.$sSubject."<br />".
|
||||
'Message: <br /><br />'.str_replace("\n", '<br />', $sMsg);
|
||||
$sPlainMessage = strip_tags(str_replace('<br />', "\n", $sHtmlMessage));
|
||||
|
||||
//Email
|
||||
$iBoundary = uniqid("HTMLEMAIL");
|
||||
$sHeaders = 'From: Contact CV <www-data@lutran.fr>'."\r\n".
|
||||
'Reply-To: Contact CV <www-data@lutran.fr>'."\r\n".
|
||||
'Cc: Julien Lutran <julien@lutran.fr>'."\r\n".
|
||||
'MIME-Version: 1.0'."\r\n".
|
||||
'Content-Type: multipart/alternative;'.
|
||||
'boundary = '.$iBoundary."\r\n\r\n".
|
||||
'MIME encoded Message'.
|
||||
'--'.$iBoundary."\r\n".
|
||||
'Content-Type: text/plain; charset=UTF-8'."\r\n".
|
||||
'Content-Transfer-Encoding: base64'."\r\n\r\n".
|
||||
chunk_split(base64_encode($sPlainMessage)).
|
||||
'--'.$iBoundary."\r\n".
|
||||
'Content-Type: text/html; charset=UTF-8'."\r\n".
|
||||
'Content-Transfer-Encoding: base64'."\r\n\r\n".
|
||||
chunk_split(base64_encode($sHtmlMessage));
|
||||
|
||||
//Store in case email fails
|
||||
@file_put_contents('log.html', '<br />----<br /><br />'.$sHtmlMessage, FILE_APPEND);
|
||||
|
||||
//Send
|
||||
if(mail('julien.lutran@gmail.com', 'julien.lutran.fr - Contact Me Message', '', $sHeaders))
|
||||
{
|
||||
$sResult = 'ok';
|
||||
}
|
||||
else
|
||||
{
|
||||
$sResult = 'An unknown error occured.';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$sResult = 'An error occured: Some fields were empty.';
|
||||
}
|
||||
return $sResult;
|
||||
}
|
||||
|
||||
private function checkSerial($sSerial)
|
||||
{
|
||||
$bResult = false;
|
||||
if(strlen($sSerial)==self::PUBLIC_KEY_LENGTH && strpos($this->getAppPath(), $_SERVER['HTTP_REFERER'])===0)
|
||||
{
|
||||
$sFileContent = file_get_contents(self::LOG_FILE);
|
||||
$asKeys = array_filter(explode("\n", $sFileContent));
|
||||
foreach($asKeys as $sKey)
|
||||
{
|
||||
$iOffset = $this->getLoadTime() - substr($sKey, self::PUBLIC_KEY_LENGTH);
|
||||
if($sSerial == substr($sKey, 0, self::PUBLIC_KEY_LENGTH) && $iOffset < self::MAX_REQUEST_TIME)
|
||||
{
|
||||
$bResult = true;
|
||||
file_put_contents(self::LOG_FILE, str_replace($sKey."\n", '', $sFileContent));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $bResult;
|
||||
}
|
||||
|
||||
private static function getAppPath()
|
||||
{
|
||||
$sAppPath = 'http://'.str_replace(array('http://', 'https://'), '', $_SERVER['SERVER_NAME'].dirname($_SERVER['SCRIPT_NAME']));
|
||||
$sAppPath = $sAppPath.(substr($sAppPath, -1)!='/'?'/':'');
|
||||
return $sAppPath;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user