summaryrefslogtreecommitdiffstats
path: root/system/libraries
diff options
context:
space:
mode:
authoradmin <devnull@localhost>2006-10-01 21:02:29 +0200
committeradmin <devnull@localhost>2006-10-01 21:02:29 +0200
commit24dd7e71d2587c4290c85bc85e0e181b29ab0539 (patch)
tree7455c1464ee2d1f4808261643dc8a727be3f8957 /system/libraries
parenta59c2a1ad6d12ae38dc36ff7f163468463961b4d (diff)
Diffstat (limited to 'system/libraries')
-rw-r--r--system/libraries/Controller.php21
-rw-r--r--system/libraries/Unit_test.php331
-rw-r--r--system/libraries/Zip.php152
3 files changed, 117 insertions, 387 deletions
diff --git a/system/libraries/Controller.php b/system/libraries/Controller.php
index 51f455023..c02074fca 100644
--- a/system/libraries/Controller.php
+++ b/system/libraries/Controller.php
@@ -80,6 +80,13 @@ class Controller extends CI_Base {
{
// Prep the class name
$class = strtolower(str_replace(EXT, '', $class));
+
+ // Bug fix for backward compat.
+ // Kill this at some point in the future
+ if ($class == 'unit_test')
+ {
+ $class = 'unit';
+ }
// Is this a class extension request?
if (substr($class, 0, 3) == 'my_')
@@ -168,9 +175,8 @@ class Controller extends CI_Base {
* @return null
*/
function _ci_init_class($class, $prefix = '', $config = NULL)
- {
+ {
// Is there an associated config file for this class?
-
if ($config == NULL)
{
if (file_exists(APPPATH.'config/'.$class.EXT))
@@ -185,22 +191,19 @@ class Controller extends CI_Base {
}
else
{
- $name = $prefix.ucfirst($class);
+ $name = $prefix.$class;
}
-
- $remap = array(
- 'Unit_test' => 'unit'
- );
$varname = ( ! isset($remap[$class])) ? $class : $remap[$class];
-
+ $varname = strtolower($varname);
+
// Instantiate the class
if ($config !== NULL)
{
$this->$varname = new $name($config);
}
else
- {
+ {
$this->$varname = new $name;
}
}
diff --git a/system/libraries/Unit_test.php b/system/libraries/Unit_test.php
deleted file mode 100644
index 0df78c253..000000000
--- a/system/libraries/Unit_test.php
+++ /dev/null
@@ -1,331 +0,0 @@
-<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
-/**
- * Code Igniter
- *
- * An open source application development framework for PHP 4.3.2 or newer
- *
- * @package CodeIgniter
- * @author Rick Ellis
- * @copyright Copyright (c) 2006, pMachine, Inc.
- * @license http://www.codeignitor.com/user_guide/license.html
- * @link http://www.codeigniter.com
- * @since Version 1.3.1
- * @filesource
- */
-
-// ------------------------------------------------------------------------
-
-/**
- * Unit Testing Class
- *
- * Simple testing class
- *
- * @package CodeIgniter
- * @subpackage Libraries
- * @category UnitTesting
- * @author Rick Ellis
- * @link http://www.codeigniter.com/user_guide/libraries/uri.html
- */
-class CI_Unit {
-
- var $active = TRUE;
- var $results = array();
- var $strict = FALSE;
- var $_template = NULL;
- var $_template_rows = NULL;
-
- function CI_Unit()
- {
- log_message('debug', "Unit Testing Class Initialized");
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Run the tests
- *
- * Runs the supplied tests
- *
- * @access public
- * @param mixed
- * @param mixed
- * @param string
- * @return string
- */
- function run($test, $expected = TRUE, $test_name = 'undefined')
- {
- if ($this->active == FALSE)
- return;
-
- if (in_array($expected, array('is_string', 'is_bool', 'is_true', 'is_false', 'is_int', 'is_numeric', 'is_float', 'is_double', 'is_array', 'is_null'), TRUE))
- {
- $expected = str_replace('is_float', 'is_double', $expected);
- $result = ($expected($test)) ? TRUE : FALSE;
- $extype = str_replace(array('true', 'false'), 'bool', str_replace('is_', '', $expected));
- }
- else
- {
- if ($this->strict == TRUE)
- $result = ($test === $expected) ? TRUE : FALSE;
- else
- $result = ($test == $expected) ? TRUE : FALSE;
-
- $extype = gettype($expected);
- }
-
- $back = $this->_backtrace();
-
- $report[] = array (
- 'test_name' => $test_name,
- 'test_datatype' => gettype($test),
- 'res_datatype' => $extype,
- 'result' => ($result === TRUE) ? 'passed' : 'failed',
- 'file' => $back['file'],
- 'line' => $back['line']
- );
-
- $this->results[] = $report;
-
- return($this->report($this->result($report)));
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Generate a report
- *
- * Displays a table with the test data
- *
- * @access public
- * @return string
- */
- function report($result = array())
- {
- if (count($result) == 0)
- {
- $result = $this->result();
- }
-
- $this->_parse_template();
-
- $r = '';
- foreach ($result as $res)
- {
- $table = '';
-
- foreach ($res as $key => $val)
- {
- $temp = $this->_template_rows;
- $temp = str_replace('{item}', $key, $temp);
- $temp = str_replace('{result}', $val, $temp);
- $table .= $temp;
- }
-
- $r .= str_replace('{rows}', $table, $this->_template);
- }
-
- return $r;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Use strict comparison
- *
- * Causes the evaluation to use === rather then ==
- *
- * @access public
- * @param bool
- * @return null
- */
- function use_strict($state = TRUE)
- {
- $this->strict = ($state == FALSE) ? FALSE : TRUE;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Make Unit testing active
- *
- * Enables/disables unit testing
- *
- * @access public
- * @param bool
- * @return null
- */
- function active($state = TRUE)
- {
- $this->active = ($state == FALSE) ? FALSE : TRUE;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Result Array
- *
- * Returns the raw result data
- *
- * @access public
- * @return array
- */
- function result($results = array())
- {
- $obj =& get_instance();
- $obj->load->language('unit_test');
-
- if (count($results) == 0)
- {
- $results = $this->results;
- }
-
- $retval = array();
- foreach ($results as $result)
- {
- $temp = array();
- foreach ($result as $key => $val)
- {
- if (is_array($val))
- {
- foreach ($val as $k => $v)
- {
- if (FALSE !== ($line = $obj->lang->line(strtolower('ut_'.$v))))
- {
- $v = $line;
- }
- $temp[$obj->lang->line('ut_'.$k)] = $v;
- }
- }
- else
- {
- if (FALSE !== ($line = $obj->lang->line(strtolower('ut_'.$val))))
- {
- $val = $line;
- }
- $temp[$obj->lang->line('ut_'.$key)] = $val;
- }
- }
-
- $retval[] = $temp;
- }
-
- return $retval;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Set the template
- *
- * This lets us set the template to be used to display results
- *
- * @access public
- * @params string
- * @return void
- */
- function set_template($template)
- {
- $this->_template = $template;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Generate a backtrace
- *
- * This lets us show file names and line numbers
- *
- * @access private
- * @return array
- */
- function _backtrace()
- {
- if (function_exists('debug_backtrace'))
- {
- $back = debug_backtrace();
-
- $file = ( ! isset($back['1']['file'])) ? '' : $back['1']['file'];
- $line = ( ! isset($back['1']['line'])) ? '' : $back['1']['line'];
-
- return array('file' => $file, 'line' => $line);
- }
- return array('file' => 'Unknown', 'line' => 'Unknown');
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Get Default Template
- *
- * @access private
- * @return string
- */
- function _default_template()
- {
- $this->_template = '
- <div style="margin:15px;background-color:#ccc;">
- <table border="0" cellpadding="4" cellspacing="1" style="width:100%;">
- {rows}
- </table></div>';
-
- $this->_template_rows = '
- <tr>
- <td style="background-color:#fff;width:140px;font-size:12px;font-weight:bold;">{item}</td>
- <td style="background-color:#fff;font-size:12px;">{result}</td>
- </tr>
- ';
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Parse Template
- *
- * Harvests the data within the template {pseudo-variables}
- *
- * @access private
- * @return void
- */
- function _parse_template()
- {
- if ( ! is_null($this->_template_rows))
- {
- return;
- }
-
- if (is_null($this->_template))
- {
- $this->_default_template();
- return;
- }
-
- if ( ! preg_match("/\{rows\}(.*?)\{\/rows\}/si", $this->_template, $match))
- {
- $this->_default_template();
- return;
- }
-
- $this->_template_rows = $match['1'];
- $this->_template = str_replace($match['0'], '{rows}', $this->_template);
- }
-
-}
-// END Unit_test Class
-
-/**
- * Helper functions to test boolean true/false
- *
- *
- * @access private
- * @return bool
- */
-function is_true($test)
-{
- return (is_bool($test) AND $test === TRUE) ? TRUE : FALSE;
-}
-function is_false($test)
-{
- return (is_bool($test) AND $test === FALSE) ? TRUE : FALSE;
-}
-
-?> \ No newline at end of file
diff --git a/system/libraries/Zip.php b/system/libraries/Zip.php
index aa264386d..2ffed9fe2 100644
--- a/system/libraries/Zip.php
+++ b/system/libraries/Zip.php
@@ -18,9 +18,11 @@
/**
* Zip Compression Class
*
- * This class is based on a library aquired at Zend:
+ * This class is based on a library I found at Zend:
* http://www.zend.com/codex.php?id=696&single=1
*
+ * The original library is a little rough around the edges so I
+ * refactored it and added several additional methods -- Rick Ellis
*
* @package CodeIgniter
* @subpackage Libraries
@@ -28,12 +30,18 @@
* @author Rick Ellis
* @link http://www.codeigniter.com/user_guide/general/encryption.html
*/
-class Zip {
-
+class CI_Zip {
+
+ var $zipfile = '';
var $zipdata = array();
var $directory = array();
var $offset = 0;
- var $zipfile = '';
+
+ function CI_Zip()
+ {
+ log_message('debug', "Zip Compression Class Initialized");
+ }
+
/**
* Add Directory
@@ -41,13 +49,33 @@ class Zip {
* Lets you add a virtual directory into which you can place files.
*
* @access public
+ * @param mixed the directory name. Can be string or array
+ * @return void
+ */
+ function add_dir($directory)
+ {
+ foreach ((array)$directory as $dir)
+ {
+ if ( ! preg_match("|.+/$|", $dir))
+ {
+ $dir .= '/';
+ }
+
+ $this->_add_dir($dir);
+ }
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Add Directory
+ *
+ * @access private
* @param string the directory name
* @return void
*/
- function add_dir($dir)
+ function _add_dir($dir)
{
- $this->zipfile = '';
-
$dir = str_replace("\\", "/", $dir);
$this->zipdata[] = "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00"
@@ -78,27 +106,50 @@ class Zip {
$this->offset = $newoffset;
$this->directory[] = $record;
- }
-
+ }
+
// --------------------------------------------------------------------
/**
- * Add File
+ * Add Data to Zip
*
* Lets you add files to the archive. If the path is included
* in the filename it will be placed within a directory. Make
* sure you use add_dir() first to create the folder.
*
* @access public
- * @param string the file name
- * @param string the data to be encoded
+ * @param mixed
+ * @param string
* @return void
*/
- function add_file($filename, $data)
+ function add_data($filepath, $data = NULL)
{
- $this->zipfile = '';
+ if (is_array($filepath))
+ {
+ foreach ($filepath as $path => $data)
+ {
+ $this->_add_data($path, $data);
+ }
+ }
+ else
+ {
+ $this->_add_data($filepath, $data);
+ }
+ }
- $filename = str_replace("\\", "/", $filename);
+ // --------------------------------------------------------------------
+
+ /**
+ * Add Data to Zip
+ *
+ * @access private
+ * @param string the file name/path
+ * @param string the data to be encoded
+ * @return void
+ */
+ function _add_data($filepath, $data)
+ {
+ $filepath = str_replace("\\", "/", $filepath);
$oldlen = strlen($data);
$crc32 = crc32($data);
@@ -111,9 +162,9 @@ class Zip {
.pack('V', $crc32)
.pack('V', $newlen)
.pack('V', $oldlen)
- .pack('v', strlen($filename))
+ .pack('v', strlen($filepath))
.pack('v', 0)
- .$filename
+ .$filepath
.$gzdata
.pack('V', $crc32)
.pack('V', $newlen)
@@ -125,7 +176,7 @@ class Zip {
.pack('V', $crc32)
.pack('V', $newlen)
.pack('V', $oldlen)
- .pack('v', strlen($filename))
+ .pack('v', strlen($filepath))
.pack('v', 0)
.pack('v', 0)
.pack('v', 0)
@@ -134,26 +185,7 @@ class Zip {
.pack('V', $this->offset);
$this->offset = $newoffset;
- $this->directory[] = $record.$filename;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Read the content of a file
- *
- * @access public
- * @param string the file path
- * @return string
- */
- function read_file($filepath)
- {
- if ( ! file_exists($filepath))
- {
- return FALSE;
- }
-
- return file_get_contents($filepath);
+ $this->directory[] = $record.$filepath;
}
// --------------------------------------------------------------------
@@ -166,11 +198,19 @@ class Zip {
*/
function get_zip()
{
+ // We cache the zip data so multiple calls
+ // do not require recompiling
if ($this->zipfile != '')
{
return $this->zipfile;
}
+ // Is there any data to return?
+ if (count($this->zipdata) == 0)
+ {
+ return FALSE;
+ }
+
$data = implode('', $this->zipdata);
$dir = implode('', $this->directory);
@@ -180,7 +220,7 @@ class Zip {
.pack('V', strlen($dir))
.pack('V', strlen($data))
."\x00\x00";
-
+
return $this->zipfile;
}
@@ -196,15 +236,15 @@ class Zip {
* @param string the data to be encoded
* @return bool
*/
- function write_file($filename, $data)
+ function write_zip($filepath)
{
- if ( ! ($fp = fopen($filename, "wb")))
+ if ( ! ($fp = fopen($filepath, "wb")))
{
return FALSE;
}
flock($fp, LOCK_EX);
- fwrite($fp, $data);
+ fwrite($fp, $this->get_zip());
flock($fp, LOCK_UN);
fclose($fp);
@@ -216,13 +256,12 @@ class Zip {
/**
* Download
*
- *
* @access public
* @param string the file name
* @param string the data to be encoded
* @return bool
*/
- function download($filename, $data)
+ function download($filename)
{
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
{
@@ -232,7 +271,7 @@ class Zip {
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
- header("Content-Length: ".strlen($data));
+ header("Content-Length: ".strlen($this->get_zip()));
}
else
{
@@ -241,10 +280,29 @@ class Zip {
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
- header("Content-Length: ".strlen($data));
+ header("Content-Length: ".strlen($this->get_zip()));
}
- echo $data;
+ echo $this->get_zip();
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Initialize Data
+ *
+ * Lets you clear current zip data. Useful if you need to create
+ * multiple zips with different data.
+ *
+ * @access public
+ * @return void
+ */
+ function clear_data()
+ {
+ $this->zipfile = '';
+ $this->zipdata = array();
+ $this->directory = array();
+ $this->offset = array();
}
}