Location: PHPKode > scripts > SJS > class.sjs.php
<?php
  /**
   *  SJS -  A PHP JavaScript and CSS security class
   *  Copyright © Benjamin Westphal. 
   *
   *  @what
   *  A PHP class to secure your CSS and JavaScript code.
   *  CSSheets and JavaScript-Code will be rewritten and secure by a 
   *  PHP Query. A Token verify that the access is valid from the browser.
   * 
   * 
   *  @version
   *  1.0
   *
   *  @usage
   *  /// instantiate a new SJS class.
   *  $sjs = new SJS('YourCssOrJavaScriptFile.css.js');
   *
   *  /// Just use $sjs->Assign(); to return the valid link.
   *
   *  @support
   *  via E-mail, when time's available, using: hide@address.com or visit http://benjaminwestphal.de
  */

class SJS {

	/**
	 * Original file or path with file.
	 *
	 * @var string
	 */
	private $sOriginFile ='';
	/**
	 * Hold the original data of the CSS or JS file.
	 *
	 * @var string
	 */
	private $sOriginFileContent ='';
	/**
	 * path to the secured file.
	 *
	 * @var string
	 */
	private $sSecuredFile='';
	/**
	 * Hold the path where the secured files will be placed.
	 *
	 * @var string
	 */
	private $sSecureDir = 'SJS';
	/**
	 * The kind of file.JS or CSS.
	 *
	 * @var string
	 */
	private $sFileType = '';
	/**
	 * Hold the generated token.
	 *
	 * @var string
	 */
	private $sRequestToken =0;
	/**
	 * The errormessage that will be displayed.
	 *
	 * @var unknown_type
	 */
	public 	$sErrorMessage = "Diese Datei ist Urheberrecthlich geschützt und darf nicht verwendet werden.\nSprich : Finger weg!!!\n \n \n \nCopyrights by Smokers (http://benjaminwestphal.de) in association with Murdoc ( http://murdoc.eu ).";
	
	public function __construct($sOriginFile) {
		$this->sOriginFile = $sOriginFile;
		$this->CreateDir();
		if(!file_exists($this->sOriginFile)) {
			echo 'Angegebene Datei nicht vorhanden.';
			exit();
		}
		session_start();
		$this->sRequestToken = md5(rand(0, 1000) . $_SERVER['REQUEST_TIME']);
		$this->DetectFileType();
		
	}
	
	/**
	 * Will detect the right filetype of the given file.
	 *
	 */
	private function DetectFileType() {
		if(preg_match('~\.css$~',basename($this->sOriginFile))) {
			$this->sFileType = 'css';
		}
		if(preg_match('~\.js$~',basename($this->sOriginFile))) {
			$this->sFileType = 'js';
		}
	}
	
	/**
	 * Check if secured files-dir exist and if not,create it.
	 *
	 */
	private function CreateDir() {
		if(!is_dir($this->sSecureDir)) {
			mkdir($this->sSecureDir,4777);
		}
	}
	
	/**
	 * Dequote a given string (url)
	 *
	 * @param string $str
	 * @return string
	 */
	private function dequote($str) { 
		return preg_replace('~^(["\']*)(.*)\1$~', '$2', $str);
	}
	
	/**
	 * Change paths of CSS files, so that there should be no error  ;-)
	 *
	 */
	private function RedesidnPath() {
		$path = dirname($this->sOriginFile);
		if(substr($path, -1) != DIRECTORY_SEPARATOR) $path .= DIRECTORY_SEPARATOR;
		$path = str_replace('\\', '/', $path);
		
		$this->sOriginFileContent = preg_replace('~url\((["\']{0,1})(.*)\1\)~Ue', '"url(\'.' . $path . '" . $this->dequote(stripslashes(\'\\2\')) . "\')"', $this->sOriginFileContent);
	}
	
	/**
	 * Create the secured file
	 *
	 */
	private function CreateNewFile() {
		$handle = fopen ($this->sOriginFile, "rb");
		while (!feof($handle)) {
			$buffer = fgets($handle, 4096);
			$this->sOriginFileContent.= $buffer;
		}
		fclose($handle);
		
		
		$this->sSecuredFile = basename($this->sOriginFile).'.php';
		$handle = fopen($this->sSecureDir.'/'.basename($this->sSecuredFile),'w+');
		if($this->sFileType == "css") {   
			$this->RedesidnPath();
			$code = 
<<<CONTENT
<?php
	session_start(); //gayt =) 
	header('Content-type: text/css');

	if(!(isset(\$_GET['SJSToken']) && isset(\$_SESSION['SJS_TOKEN_{$this->sOriginFile}'])
	&& \$_GET['SJSToken'] == \$_SESSION['SJS_TOKEN_{$this->sOriginFile}']))
		exit('{$this->sErrorMessage}');
	unset(\$_SESSION['SJS_TOKEN_{$this->sOriginFile}']);
?>	
{$this->sOriginFileContent}
CONTENT;
		}
		if($this->sFileType == "js") { 
			$code = 
<<<CONTENT
<?php
	session_start();
	header('Content-type: application/javascript'); // wird atm nur gelöscht wenn der code richtig war - unser if hat keine klammern,ahhhh brainckk :D

	if(!(isset(\$_GET['SJSToken']) && isset(\$_SESSION['SJS_TOKEN_{$this->sOriginFile}'])
	&& \$_GET['SJSToken'] == \$_SESSION['SJS_TOKEN_{$this->sOriginFile}']))
		exit('{$this->sErrorMessage}');
	unset(\$_SESSION['SJS_TOKEN_{$this->sOriginFile}']);
?>	
{$this->sOriginFileContent}
CONTENT;
		}
		
		fwrite($handle,$code);

	}
	
	/**
	 * decide the filetypes and take the right function for the given filetype
	 *
	 */
	public function Assign() {
		if($this->sFileType == 'css') {
			$this->AssignCSS();
		} 
		if($this->sFileType == 'js') {
			$this->AssignJS();
		}
			
	}
	
	/**
	 * Create the JS include link
	 *
	 */
	private function AssignJS() {
		(int) $iFileTimeOrigin = @filemtime($this->sOriginFile) ;
		(int) $iFileTimeNew  = @filemtime($this->sSecureDir.'/'.basename($this->sOriginFile).'.php') ;

		if(!file_exists($this->sSecureDir.'/'.basename($this->sOriginFile).'.php') || $iFileTimeOrigin > $iFileTimeNew ){
			$this->CreateNewFile();
			echo '<script type="text/javascript" src="'.$this->sSecureDir.'/'.$this->sSecuredFile.'?SJSToken='.$this->sRequestToken .'"></script>';
		} 
		else {
			echo '<script type="text/javascript" src="'.$this->sSecureDir.'/'.basename($this->sOriginFile).'.php?SJSToken='.$this->sRequestToken.'"></script>';
		}
	}
	/**
	 * Create the CSS include link
	 *
	 */
	private function AssignCSS() {
		(int) $iFileTimeOrigin = @filemtime($this->sOriginFile) ;
		(int) $iFileTimeNew  = @filemtime($this->sSecureDir.'/'.basename($this->sOriginFile).'.php') ;
		
		
		
		if(!file_exists($this->sSecureDir.'/'.basename($this->sOriginFile).'.php') || $iFileTimeOrigin > $iFileTimeNew ){
			$this->CreateNewFile();
			echo '<link rel="stylesheet" type="text/css" href="'.$this->sSecureDir.'/'.$this->sSecuredFile.'?SJSToken='.$this->sRequestToken .'" />';
		} 
		else {
			echo '<link rel="stylesheet" type="text/css" href="'.$this->sSecureDir.'/'.basename($this->sOriginFile).'.php?SJSToken='.$this->sRequestToken.'" />';
		}
	}
	
	public function __destruct() {
		$_SESSION['SJS_TOKEN_'.$this->sOriginFile] = $this->sRequestToken;
	}
}


?>
Return current item: SJS