<?php
/******************************************************************************

	Copyright (C) 2006-2007  John Ha

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

******************************************************************************/

if (!class_exists('core')) {

	class core {
	
		function version($file) {
			$data = array();
			$plugin_data = file_get_contents($file);
			$data['plugin-name'] = preg_match("/Plugin Name: *(.*)/i", $plugin_data, $temp) ? preg_replace('/\s+$/', '', $temp[1]) : '';
			$data['plugin-uri'] = preg_match("/Plugin URI: *(.*)/i", $plugin_data, $temp) ? preg_replace('/\s+$/', '', $temp[1]) : '';
			$data['description'] = preg_match("/Description: *(.*)/i", $plugin_data, $temp) ? preg_replace('/\s+$/', '', $temp[1]) : '';
			$data['author-name'] = preg_match("/Author: *(.*)/i", $plugin_data, $temp) ? preg_replace('/\s+$/', '', $temp[1]) : '';
			$data['author-uri'] = preg_match("/Author URI: *(.*)/i", $plugin_data, $temp) ? preg_replace('/\s+$/', '', $temp[1]) : '';
			$data['version'] = preg_match("/Version: *(.*)/i", $plugin_data, $temp) ? preg_replace('/\s+$/', '', $temp[1]) : '';
			return $data;
		}
		
		function urlname($path) {
			if (!isset($_SERVER['DOCUMENT_ROOT']))
				$_SERVER['DOCUMENT_ROOT'] = str_replace('\\', '/', substr(
				$_SERVER['SCRIPT_FILENAME'], 0, 0 - strlen($_SERVER['PHP_SELF'])));
			$path = str_replace('\\', '/', realpath($path));
			return 'http://' . $_SERVER['HTTP_HOST'] . str_replace($_SERVER['DOCUMENT_ROOT'], '', $path);
		}
		
		function core_js($url) {
			global $js_loaded, $wp_version;
			if (!$js_loaded) {
				$js_loaded = true;

				if (function_exists('wp_enqueue_script')) {
					wp_deregister_script('fat');
					wp_enqueue_script('prototype');
					do_action('admin_print_scripts');
					$extra = '';
				} else $extra = 'prototype.lite.js,';

				return '<script src="' . $url . '/core.php?dir=includes&file=' . ($wp_version[0] == '2' ? '' : '') . $extra . 'fat.js,core.js,dom.js,event.js,loader.js,debug.js&type=js" type="text/javascript"></script>' . "\n";
			}
		}
	
		function compress($files, $type, $dir = '') {
			$path = realpath(dirname(__FILE__) . '/../' . $dir);
			$files = explode(',', $files);
			$str = '';
			foreach ($files as $file)
				$str .= @file_get_contents($path . (strpos($path, '/') === false ? '\\' : '/') . $file);
			switch ($type) {
				case 'js':
					header("Content-type: application/x-javascript");
					echo core::comp_js($str);
					break;
				case 'css':
					header("Content-type: text/css; charset=UTF-8");
					echo core::comp_css($str);
					break;
			}
		}
		
		function comp_css($buffer) {
			$buffer = preg_replace('/\/\*(?!-).*?\*\//s', '', $buffer);
			$buffer = preg_replace('/\s*(^|[{},:;\(\)\-]|$)\s*/', '$1', $buffer);
			return $buffer;
		}
		
		function comp_js($buffer) {
			$buffer = preg_replace('/\/\*[^@].*?[^@]\*\//s', '', $buffer);
			$buffer = preg_replace('/(^|[^:])\/\/.*/', '$1', $buffer);
			preg_match_all('/\/.*?\//', $buffer, $matches, PREG_SET_ORDER);
			foreach ($matches as $v) {
				$a[] = $v[0];
			}
			$buffer = preg_replace('/\/.*?\//', '#####', $buffer);
			preg_match_all('/#####/', $buffer, $matches, PREG_SET_ORDER);
			foreach ($matches as $v) {
				$b[] = '/' . $v[0] . '/';
			}
			preg_match_all('/(.*?)(([\'"]).*?\\3)(?!s )/s', $buffer, $matches, PREG_SET_ORDER);
			$remove_whitespace = '/\s*(^|[\\\[\]\?\(\)\!\+\-\*\/<>{}\|%&,:;=~]|$)\s*/';
			$firstbit = '';
			foreach ($matches as $v) {
				$v1 = preg_replace($remove_whitespace, '$1', $v[1]);
				$v1 = preg_replace('/\s+/', ' ', $v1);
				$firstbit .= $v1 . $v[2];
			}
			$lastbit = preg_replace('/^.+([\'"])(.+$)/s', '$2', $buffer);
			$lastbit = preg_replace($remove_whitespace, '$1', $lastbit);
			$buffer = ($a && $b ? preg_replace($b, $a, $firstbit . $lastbit, 1) : $firstbit . $lastbit);
			return $buffer;
		}
		
		function random_string($len = 8) {
			$str = '';
			for($i = 0; $i < $len; $i++) {
				switch(rand(1,3)) {
					case 1: $str .= chr(rand(48,57));  break; //0-9
					case 2: $str .= chr(rand(65,90));  break; //A-Z
					case 3: $str .= chr(rand(97,122)); break; //a-z
				}
			}
			return $str;
		}
		
		function print_array($A) {
			echo "<table border='1'>\n";
			foreach ($A as $ky=>$da) {
				echo "<tr><td valign='top'>$ky</td><td>";
				if (is_array($da)) core::print_array($da);
				else {
					echo "<pre>";
					echo utf8_encode($da);
					echo "</pre>";
				}
				echo "</td></tr>\n";
			}
			echo "</table>\n";
		}

		function array_swap(&$array, $item1, $item2) { 
			$temp=$array[$item2]; 
			$array[$item2]=$array[$item1]; 
			$array[$item1]=$temp; 
		}
		
		// From Inscript by John Godley
		function escape_string ($str) {
			if (get_magic_quotes_gpc())
				$str = stripslashes ($str);
			return mysql_real_escape_string ($str);
		}
	}
}

if ($_GET['file'] && $_GET['type'])
	core::compress($_GET['file'], $_GET['type'], $_GET['dir']);
?>