$curl_handle - resource - Required - The cURL handle resource that represents the in-progress transfer.$file_handle - resource - Required - The file handle resource that represents the file on the local file system.$length - integer - Required - The length in kilobytes of the data chunk that was transferred.array('ClassName', 'MethodName').$curl_handle - resource - Required - The cURL handle resource that represents the in-progress transfer.$length - integer - Required - The length in kilobytes of the data chunk that was transferred.array('ClassName', 'MethodName').callback - string|array - Optional - The string name of a function to pass the response data to. If this is a method, pass an array where the [0] index is the class and the [1] index is the method name.limit - integer - Optional - The number of simultaneous requests to make. This can be useful for scaling around slow server responses. Defaults to trusting cURLs judgement as to how many to use.
* // create a new instance of Services_JSON
* $json = new Services_JSON();
*
* // convert a complexe value to JSON notation, and send it to the browser
* $value = array('foo', 'bar', array(1, 2, 'baz'), array(3, array(4)));
* $output = $json->encode($value);
*
* print($output);
* // prints: ["foo","bar",[1,2,"baz"],[3,[4]]]
*
* // accept incoming POST data, assumed to be in JSON notation
* $input = file_get_contents('php://input', 1000000);
* $value = $json->decode($input);
*
*/
class Services_JSON
{
/**
* constructs a new JSON instance
*
* @param int $use object behavior flags; combine with boolean-OR
*
* possible values:
* - SERVICES_JSON_LOOSE_TYPE: loose typing.
* "{...}" syntax creates associative arrays
* instead of objects in decode().
* - SERVICES_JSON_SUPPRESS_ERRORS: error suppression.
* Values which can't be encoded (e.g. resources)
* appear as NULL instead of throwing errors.
* By default, a deeply-nested resource will
* bubble up with an error, so all return values
* from encode() should be checked with isError()
*/
function Services_JSON($use = 0)
{
$this->use = $use;
}
/**
* convert a string from one UTF-16 char to one UTF-8 char
*
* Normally should be handled by mb_convert_encoding, but
* provides a slower PHP-only method for installations
* that lack the multibye string extension.
*
* @param string $utf16 UTF-16 character
* @return string UTF-8 character
* @access private
*/
function utf162utf8($utf16)
{
// oh please oh please oh please oh please oh please
if(function_exists('mb_convert_encoding')) {
return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
}
$bytes = (ord($utf16[0]) << 8) | ord($utf16[1]);
switch(true) {
case ((0x7F & $bytes) == $bytes):
// this case should never be reached, because we are in ASCII range
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return chr(0x7F & $bytes);
case (0x07FF & $bytes) == $bytes:
// return a 2-byte UTF-8 character
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return chr(0xC0 | (($bytes >> 6) & 0x1F))
. chr(0x80 | ($bytes & 0x3F));
case (0xFFFF & $bytes) == $bytes:
// return a 3-byte UTF-8 character
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return chr(0xE0 | (($bytes >> 12) & 0x0F))
. chr(0x80 | (($bytes >> 6) & 0x3F))
. chr(0x80 | ($bytes & 0x3F));
}
// ignoring UTF-32 for now, sorry
return '';
}
/**
* convert a string from one UTF-8 char to one UTF-16 char
*
* Normally should be handled by mb_convert_encoding, but
* provides a slower PHP-only method for installations
* that lack the multibye string extension.
*
* @param string $utf8 UTF-8 character
* @return string UTF-16 character
* @access private
*/
function utf82utf16($utf8)
{
// oh please oh please oh please oh please oh please
if(function_exists('mb_convert_encoding')) {
return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
}
switch(strlen($utf8)) {
case 1:
// this case should never be reached, because we are in ASCII range
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return $utf8;
case 2:
// return a UTF-16 character from a 2-byte UTF-8 char
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return chr(0x07 & (ord($utf8[0]) >> 2))
. chr((0xC0 & (ord($utf8[0]) << 6))
| (0x3F & ord($utf8[1])));
case 3:
// return a UTF-16 character from a 3-byte UTF-8 char
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
return chr((0xF0 & (ord($utf8[0]) << 4))
| (0x0F & (ord($utf8[1]) >> 2)))
. chr((0xC0 & (ord($utf8[1]) << 6))
| (0x7F & ord($utf8[2])));
}
// ignoring UTF-32 for now, sorry
return '';
}
/**
* encodes an arbitrary variable into JSON format (and sends JSON Header)
*
* @param mixed $var any number, boolean, string, array, or object to be encoded.
* see argument 1 to Services_JSON() above for array-parsing behavior.
* if var is a strng, note that encode() always expects it
* to be in ASCII or UTF-8 format!
*
* @return mixed JSON string representation of input var or an error if a problem occurs
* @access public
*/
function encode($var)
{
header('Content-type: application/json');
return $this->_encode($var);
}
/**
* encodes an arbitrary variable into JSON format without JSON Header - warning - may allow CSS!!!!)
*
* @param mixed $var any number, boolean, string, array, or object to be encoded.
* see argument 1 to Services_JSON() above for array-parsing behavior.
* if var is a strng, note that encode() always expects it
* to be in ASCII or UTF-8 format!
*
* @return mixed JSON string representation of input var or an error if a problem occurs
* @access public
*/
function encodeUnsafe($var)
{
return $this->_encode($var);
}
/**
* PRIVATE CODE that does the work of encodes an arbitrary variable into JSON format
*
* @param mixed $var any number, boolean, string, array, or object to be encoded.
* see argument 1 to Services_JSON() above for array-parsing behavior.
* if var is a strng, note that encode() always expects it
* to be in ASCII or UTF-8 format!
*
* @return mixed JSON string representation of input var or an error if a problem occurs
* @access public
*/
function _encode($var)
{
switch (gettype($var)) {
case 'boolean':
return $var ? 'true' : 'false';
case 'NULL':
return 'null';
case 'integer':
return (int) $var;
case 'double':
case 'float':
return (float) $var;
case 'string':
// STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT
$ascii = '';
$strlen_var = strlen($var);
/*
* Iterate over every character in the string,
* escaping with a slash or encoding to UTF-8 where necessary
*/
for ($c = 0; $c < $strlen_var; ++$c) {
$ord_var_c = ord($var[$c]);
switch (true) {
case $ord_var_c == 0x08:
$ascii .= '\b';
break;
case $ord_var_c == 0x09:
$ascii .= '\t';
break;
case $ord_var_c == 0x0A:
$ascii .= '\n';
break;
case $ord_var_c == 0x0C:
$ascii .= '\f';
break;
case $ord_var_c == 0x0D:
$ascii .= '\r';
break;
case $ord_var_c == 0x22:
case $ord_var_c == 0x2F:
case $ord_var_c == 0x5C:
// double quote, slash, slosh
$ascii .= '\\'.$var[$c];
break;
case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
// characters U-00000000 - U-0000007F (same as ASCII)
$ascii .= $var[$c];
break;
case (($ord_var_c & 0xE0) == 0xC0):
// characters U-00000080 - U-000007FF, mask 110XXXXX
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
if ($c+1 >= $strlen_var) {
$c += 1;
$ascii .= '?';
break;
}
$char = pack('C*', $ord_var_c, ord($var[$c + 1]));
$c += 1;
$utf16 = $this->utf82utf16($char);
$ascii .= sprintf('\u%04s', bin2hex($utf16));
break;
case (($ord_var_c & 0xF0) == 0xE0):
if ($c+2 >= $strlen_var) {
$c += 2;
$ascii .= '?';
break;
}
// characters U-00000800 - U-0000FFFF, mask 1110XXXX
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$char = pack('C*', $ord_var_c,
@ord($var[$c + 1]),
@ord($var[$c + 2]));
$c += 2;
$utf16 = $this->utf82utf16($char);
$ascii .= sprintf('\u%04s', bin2hex($utf16));
break;
case (($ord_var_c & 0xF8) == 0xF0):
if ($c+3 >= $strlen_var) {
$c += 3;
$ascii .= '?';
break;
}
// characters U-00010000 - U-001FFFFF, mask 11110XXX
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$char = pack('C*', $ord_var_c,
ord($var[$c + 1]),
ord($var[$c + 2]),
ord($var[$c + 3]));
$c += 3;
$utf16 = $this->utf82utf16($char);
$ascii .= sprintf('\u%04s', bin2hex($utf16));
break;
case (($ord_var_c & 0xFC) == 0xF8):
// characters U-00200000 - U-03FFFFFF, mask 111110XX
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
if ($c+4 >= $strlen_var) {
$c += 4;
$ascii .= '?';
break;
}
$char = pack('C*', $ord_var_c,
ord($var[$c + 1]),
ord($var[$c + 2]),
ord($var[$c + 3]),
ord($var[$c + 4]));
$c += 4;
$utf16 = $this->utf82utf16($char);
$ascii .= sprintf('\u%04s', bin2hex($utf16));
break;
case (($ord_var_c & 0xFE) == 0xFC):
if ($c+5 >= $strlen_var) {
$c += 5;
$ascii .= '?';
break;
}
// characters U-04000000 - U-7FFFFFFF, mask 1111110X
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$char = pack('C*', $ord_var_c,
ord($var[$c + 1]),
ord($var[$c + 2]),
ord($var[$c + 3]),
ord($var[$c + 4]),
ord($var[$c + 5]));
$c += 5;
$utf16 = $this->utf82utf16($char);
$ascii .= sprintf('\u%04s', bin2hex($utf16));
break;
}
}
return '"'.$ascii.'"';
case 'array':
/*
* As per JSON spec if any array key is not an integer
* we must treat the the whole array as an object. We
* also try to catch a sparsely populated associative
* array with numeric keys here because some JS engines
* will create an array with empty indexes up to
* max_index which can cause memory issues and because
* the keys, which may be relevant, will be remapped
* otherwise.
*
* As per the ECMA and JSON specification an object may
* have any string as a property. Unfortunately due to
* a hole in the ECMA specification if the key is a
* ECMA reserved word or starts with a digit the
* parameter is only accessible using ECMAScript's
* bracket notation.
*/
// treat as a JSON object
if (is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) {
$properties = array_map(array($this, 'name_value'),
array_keys($var),
array_values($var));
foreach($properties as $property) {
if(Services_JSON::isError($property)) {
return $property;
}
}
return '{' . join(',', $properties) . '}';
}
// treat it like a regular array
$elements = array_map(array($this, '_encode'), $var);
foreach($elements as $element) {
if(Services_JSON::isError($element)) {
return $element;
}
}
return '[' . join(',', $elements) . ']';
case 'object':
$vars = get_object_vars($var);
$properties = array_map(array($this, 'name_value'),
array_keys($vars),
array_values($vars));
foreach($properties as $property) {
if(Services_JSON::isError($property)) {
return $property;
}
}
return '{' . join(',', $properties) . '}';
default:
return ($this->use & SERVICES_JSON_SUPPRESS_ERRORS)
? 'null'
: new Services_JSON_Error(gettype($var)." can not be encoded as JSON string");
}
}
/**
* array-walking function for use in generating JSON-formatted name-value pairs
*
* @param string $name name of key to use
* @param mixed $value reference to an array element to be encoded
*
* @return string JSON-formatted name-value pair, like '"name":value'
* @access private
*/
function name_value($name, $value)
{
$encoded_value = $this->_encode($value);
if(Services_JSON::isError($encoded_value)) {
return $encoded_value;
}
return $this->_encode(strval($name)) . ':' . $encoded_value;
}
/**
* reduce a string by removing leading and trailing comments and whitespace
*
* @param $str string string value to strip of comments and whitespace
*
* @return string string value stripped of comments and whitespace
* @access private
*/
function reduce_string($str)
{
$str = preg_replace(array(
// eliminate single line comments in '// ...' form
'#^\s*//(.+)$#m',
// eliminate multi-line comments in '/* ... */' form, at start of string
'#^\s*/\*(.+)\*/#Us',
// eliminate multi-line comments in '/* ... */' form, at end of string
'#/\*(.+)\*/\s*$#Us'
), '', $str);
// eliminate extraneous space
return trim($str);
}
/**
* decodes a JSON string into appropriate variable
*
* @param string $str JSON-formatted string
*
* @return mixed number, boolean, string, array, or object
* corresponding to given JSON input string.
* See argument 1 to Services_JSON() above for object-output behavior.
* Note that decode() always returns strings
* in ASCII or UTF-8 format!
* @access public
*/
function decode($str)
{
$str = $this->reduce_string($str);
switch (strtolower($str)) {
case 'true':
return true;
case 'false':
return false;
case 'null':
return null;
default:
$m = array();
if (is_numeric($str)) {
// Lookie-loo, it's a number
// This would work on its own, but I'm trying to be
// good about returning integers where appropriate:
// return (float)$str;
// Return float or int, as appropriate
return ((float)$str == (integer)$str)
? (integer)$str
: (float)$str;
} elseif (preg_match('/^("|\').*(\1)$/s', $str, $m) && $m[1] == $m[2]) {
// STRINGS RETURNED IN UTF-8 FORMAT
$delim = substr($str, 0, 1);
$chrs = substr($str, 1, -1);
$utf8 = '';
$strlen_chrs = strlen($chrs);
for ($c = 0; $c < $strlen_chrs; ++$c) {
$substr_chrs_c_2 = substr($chrs, $c, 2);
$ord_chrs_c = ord($chrs[$c]);
switch (true) {
case $substr_chrs_c_2 == '\b':
$utf8 .= chr(0x08);
++$c;
break;
case $substr_chrs_c_2 == '\t':
$utf8 .= chr(0x09);
++$c;
break;
case $substr_chrs_c_2 == '\n':
$utf8 .= chr(0x0A);
++$c;
break;
case $substr_chrs_c_2 == '\f':
$utf8 .= chr(0x0C);
++$c;
break;
case $substr_chrs_c_2 == '\r':
$utf8 .= chr(0x0D);
++$c;
break;
case $substr_chrs_c_2 == '\\"':
case $substr_chrs_c_2 == '\\\'':
case $substr_chrs_c_2 == '\\\\':
case $substr_chrs_c_2 == '\\/':
if (($delim == '"' && $substr_chrs_c_2 != '\\\'') ||
($delim == "'" && $substr_chrs_c_2 != '\\"')) {
$utf8 .= $chrs[++$c];
}
break;
case preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $c, 6)):
// single, escaped unicode character
$utf16 = chr(hexdec(substr($chrs, ($c + 2), 2)))
. chr(hexdec(substr($chrs, ($c + 4), 2)));
$utf8 .= $this->utf162utf8($utf16);
$c += 5;
break;
case ($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F):
$utf8 .= $chrs[$c];
break;
case ($ord_chrs_c & 0xE0) == 0xC0:
// characters U-00000080 - U-000007FF, mask 110XXXXX
//see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$utf8 .= substr($chrs, $c, 2);
++$c;
break;
case ($ord_chrs_c & 0xF0) == 0xE0:
// characters U-00000800 - U-0000FFFF, mask 1110XXXX
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$utf8 .= substr($chrs, $c, 3);
$c += 2;
break;
case ($ord_chrs_c & 0xF8) == 0xF0:
// characters U-00010000 - U-001FFFFF, mask 11110XXX
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$utf8 .= substr($chrs, $c, 4);
$c += 3;
break;
case ($ord_chrs_c & 0xFC) == 0xF8:
// characters U-00200000 - U-03FFFFFF, mask 111110XX
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$utf8 .= substr($chrs, $c, 5);
$c += 4;
break;
case ($ord_chrs_c & 0xFE) == 0xFC:
// characters U-04000000 - U-7FFFFFFF, mask 1111110X
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
$utf8 .= substr($chrs, $c, 6);
$c += 5;
break;
}
}
return $utf8;
} elseif (preg_match('/^\[.*\]$/s', $str) || preg_match('/^\{.*\}$/s', $str)) {
// array, or object notation
if ($str[0] == '[') {
$stk = array(SERVICES_JSON_IN_ARR);
$arr = array();
} else {
if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
$stk = array(SERVICES_JSON_IN_OBJ);
$obj = array();
} else {
$stk = array(SERVICES_JSON_IN_OBJ);
$obj = new stdClass();
}
}
array_push($stk, array('what' => SERVICES_JSON_SLICE,
'where' => 0,
'delim' => false));
$chrs = substr($str, 1, -1);
$chrs = $this->reduce_string($chrs);
if ($chrs == '') {
if (reset($stk) == SERVICES_JSON_IN_ARR) {
return $arr;
} else {
return $obj;
}
}
//print("\nparsing {$chrs}\n");
$strlen_chrs = strlen($chrs);
for ($c = 0; $c <= $strlen_chrs; ++$c) {
$top = end($stk);
$substr_chrs_c_2 = substr($chrs, $c, 2);
if (($c == $strlen_chrs) || (($chrs[$c] == ',') && ($top['what'] == SERVICES_JSON_SLICE))) {
// found a comma that is not inside a string, array, etc.,
// OR we've reached the end of the character list
$slice = substr($chrs, $top['where'], ($c - $top['where']));
array_push($stk, array('what' => SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false));
//print("Found split at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
if (reset($stk) == SERVICES_JSON_IN_ARR) {
// we are in an array, so just push an element onto the stack
array_push($arr, $this->decode($slice));
} elseif (reset($stk) == SERVICES_JSON_IN_OBJ) {
// we are in an object, so figure
// out the property name and set an
// element in an associative array,
// for now
$parts = array();
if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?$/Uis', $slice, $parts)) {
// "name":value pair
$key = $this->decode($parts[1]);
$val = $this->decode($parts[2]);
if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
$obj[$key] = $val;
} else {
$obj->$key = $val;
}
} elseif (preg_match('/^\s*(\w+)\s*:\s*(\S.*),?$/Uis', $slice, $parts)) {
// name:value pair, where name is unquoted
$key = $parts[1];
$val = $this->decode($parts[2]);
if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
$obj[$key] = $val;
} else {
$obj->$key = $val;
}
}
}
} elseif ((($chrs[$c] == '"') || ($chrs[$c] == "'")) && ($top['what'] != SERVICES_JSON_IN_STR)) {
// found a quote, and we are not inside a string
array_push($stk, array('what' => SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs[$c]));
//print("Found start of string at {$c}\n");
} elseif (($chrs[$c] == $top['delim']) &&
($top['what'] == SERVICES_JSON_IN_STR) &&
((strlen(substr($chrs, 0, $c)) - strlen(rtrim(substr($chrs, 0, $c), '\\'))) % 2 != 1)) {
// found a quote, we're in a string, and it's not escaped
// we know that it's not escaped becase there is _not_ an
// odd number of backslashes at the end of the string so far
array_pop($stk);
//print("Found end of string at {$c}: ".substr($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n");
} elseif (($chrs[$c] == '[') &&
in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
// found a left-bracket, and we are in an array, object, or slice
array_push($stk, array('what' => SERVICES_JSON_IN_ARR, 'where' => $c, 'delim' => false));
//print("Found start of array at {$c}\n");
} elseif (($chrs[$c] == ']') && ($top['what'] == SERVICES_JSON_IN_ARR)) {
// found a right-bracket, and we're in an array
array_pop($stk);
//print("Found end of array at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
} elseif (($chrs[$c] == '{') &&
in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
// found a left-brace, and we are in an array, object, or slice
array_push($stk, array('what' => SERVICES_JSON_IN_OBJ, 'where' => $c, 'delim' => false));
//print("Found start of object at {$c}\n");
} elseif (($chrs[$c] == '}') && ($top['what'] == SERVICES_JSON_IN_OBJ)) {
// found a right-brace, and we're in an object
array_pop($stk);
//print("Found end of object at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
} elseif (($substr_chrs_c_2 == '/*') &&
in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
// found a comment start, and we are in an array, object, or slice
array_push($stk, array('what' => SERVICES_JSON_IN_CMT, 'where' => $c, 'delim' => false));
$c++;
//print("Found start of comment at {$c}\n");
} elseif (($substr_chrs_c_2 == '*/') && ($top['what'] == SERVICES_JSON_IN_CMT)) {
// found a comment end, and we're in one now
array_pop($stk);
$c++;
for ($i = $top['where']; $i <= $c; ++$i)
$chrs = substr_replace($chrs, ' ', $i, 1);
//print("Found end of comment at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
}
}
if (reset($stk) == SERVICES_JSON_IN_ARR) {
return $arr;
} elseif (reset($stk) == SERVICES_JSON_IN_OBJ) {
return $obj;
}
}
}
}
/**
* @todo Ultimately, this should just call PEAR::isError()
*/
function isError($data, $code = null)
{
if (class_exists('pear')) {
return PEAR::isError($data, $code);
} elseif (is_object($data) && (get_class($data) == 'services_json_error' ||
is_subclass_of($data, 'services_json_error'))) {
return true;
}
return false;
}
}
if (class_exists('PEAR_Error')) {
class Services_JSON_Error extends PEAR_Error
{
function Services_JSON_Error($message = 'unknown error', $code = null,
$mode = null, $options = null, $userinfo = null)
{
parent::PEAR_Error($message, $code, $mode, $options, $userinfo);
}
}
} else {
/**
* @todo Ultimately, this class shall be descended from PEAR_Error
*/
class Services_JSON_Error
{
function Services_JSON_Error($message = 'unknown error', $code = null,
$mode = null, $options = null, $userinfo = null)
{
}
}
}
endif;
/**
* 为了兼容低版本的php而增加的函数集
* json从5.2.0开始支持,wordpress 2.9开始提供json函数的兼容性代码
*/
if ( !function_exists('json_encode') ) {
function json_encode( $string ) {
global $wp_json;
if ( !is_a($wp_json, 'Services_JSON') ) {
$wp_json = new Services_JSON();
}
return $wp_json->encodeUnsafe( $string );
}
}
if ( !function_exists('json_decode') ) {
function json_decode( $string, $assoc_array = false ) {
global $wp_json;
if ( !is_a($wp_json, 'Services_JSON') ) {
$wp_json = new Services_JSON();
}
$res = $wp_json->decode( $string );
if ( $assoc_array )
$res = _json_decode_object_helper( $res );
return $res;
}
function _json_decode_object_helper($data) {
if ( is_object($data) )
$data = get_object_vars($data);
return is_array($data) ? array_map(__FUNCTION__, $data) : $data;
}
}
================================================
FILE: BlockComment/Plugin.php
================================================
comment = array('BlockComment_Plugin', 'filter');
Typecho_Plugin::factory('Widget_Feedback')->trackback = array('BlockComment_Plugin', 'filter');
Typecho_Plugin::factory('Widget_XmlRpc')->pingback = array('BlockComment_Plugin', 'filter');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$hosts = new Typecho_Widget_Helper_Form_Element_Textarea('hosts', NULL, NULL,
_t('地址列表'), _t('每行单个地址,请仔细匹配以免误封杀'));
$form->addInput($hosts);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 标记评论状态时的插件接口
*
* @access public
* @param array $comment 评论数据的结构体
* @param Typecho_Widget $commentWidget 评论组件
* @param string $status 评论状态
* @return void
*/
public static function mark($comment, $commentWidget, $status)
{
if ('spam' == $comment['status'] && $status != 'spam') {
self::filter($comment, $commentWidget, NULL, 'submit-ham');
} else if ('spam' != $comment['status'] && $status == 'spam') {
self::filter($comment, $commentWidget, NULL, 'submit-spam');
}
}
/**
* 评论过滤器
*
* @access public
* @param array $comment 评论结构
* @param Typecho_Widget $post 被评论的文章
* @param array $result 返回的结果上下文
* @param string $api api地址
* @return void
*/
public static function filter($comment, $post, $result, $api = 'comment-check')
{
$comment = empty($result) ? $comment : $result;
$options = Typecho_Widget::widget('Widget_Options');
$hosts = $options->plugin('BlockComment')->hosts;
$data = array(
'blog' => $options->siteUrl,
'user_ip' => $comment['ip'],
'user_agent' => $comment['agent'],
'referrer' => Typecho_Request::getInstance()->getReferer(),
'permalink' => $post->permalink,
'comment_type' => $comment['type'],
'comment_author' => $comment['author'],
'comment_author_email' => $comment['mail'],
'comment_author_url' => $comment['url'],
'comment_content' => $comment['text']
);
foreach(split("\n", $hosts) as $key => $value){
$value = trim($value);
if (strlen($value)) {
$regex = sprintf("/^%s/i", preg_quote($value));
// 如果提交者符合指定的 IP,则扔进垃圾评论中
if (preg_match($regex, $data['user_ip'])) {
$comment['status'] = 'spam';
break;
}
}
}
return $comment;
}
}
================================================
FILE: ConnectToTwitter/OAuth.php
================================================
key = $key;
$this->secret = $secret;
$this->callback_url = $callback_url;
}/*}}}*/
function __toString() {/*{{{*/
return "OAuthConsumer[key=$this->key,secret=$this->secret]";
}/*}}}*/
}/*}}}*/
class OAuthToken {/*{{{*/
// access tokens and request tokens
public $key;
public $secret;
/**
* key = the token
* secret = the token secret
*/
function __construct($key, $secret) {/*{{{*/
$this->key = $key;
$this->secret = $secret;
}/*}}}*/
/**
* generates the basic string serialization of a token that a server
* would respond to request_token and access_token calls with
*/
function to_string() {/*{{{*/
return "oauth_token=" . OAuthUtil::urlencode_rfc3986($this->key) .
"&oauth_token_secret=" . OAuthUtil::urlencode_rfc3986($this->secret);
}/*}}}*/
function __toString() {/*{{{*/
return $this->to_string();
}/*}}}*/
}/*}}}*/
class OAuthSignatureMethod {/*{{{*/
public function check_signature(&$request, $consumer, $token, $signature) {
$built = $this->build_signature($request, $consumer, $token);
return $built == $signature;
}
}/*}}}*/
class OAuthSignatureMethod_HMAC_SHA1 extends OAuthSignatureMethod {/*{{{*/
function get_name() {/*{{{*/
return "HMAC-SHA1";
}/*}}}*/
public function build_signature($request, $consumer, $token) {/*{{{*/
$base_string = $request->get_signature_base_string();
$request->base_string = $base_string;
$key_parts = array(
$consumer->secret,
($token) ? $token->secret : ""
);
$key_parts = OAuthUtil::urlencode_rfc3986($key_parts);
$key = implode('&', $key_parts);
return base64_encode( hash_hmac('sha1', $base_string, $key, true));
}/*}}}*/
}/*}}}*/
class OAuthSignatureMethod_PLAINTEXT extends OAuthSignatureMethod {/*{{{*/
public function get_name() {/*{{{*/
return "PLAINTEXT";
}/*}}}*/
public function build_signature($request, $consumer, $token) {/*{{{*/
$sig = array(
OAuthUtil::urlencode_rfc3986($consumer->secret)
);
if ($token) {
array_push($sig, OAuthUtil::urlencode_rfc3986($token->secret));
} else {
array_push($sig, '');
}
$raw = implode("&", $sig);
// for debug purposes
$request->base_string = $raw;
return OAuthUtil::urlencode_rfc3986($raw);
}/*}}}*/
}/*}}}*/
class OAuthSignatureMethod_RSA_SHA1 extends OAuthSignatureMethod {/*{{{*/
public function get_name() {/*{{{*/
return "RSA-SHA1";
}/*}}}*/
protected function fetch_public_cert(&$request) {/*{{{*/
// not implemented yet, ideas are:
// (1) do a lookup in a table of trusted certs keyed off of consumer
// (2) fetch via http using a url provided by the requester
// (3) some sort of specific discovery code based on request
//
// either way should return a string representation of the certificate
throw Exception("fetch_public_cert not implemented");
}/*}}}*/
protected function fetch_private_cert(&$request) {/*{{{*/
// not implemented yet, ideas are:
// (1) do a lookup in a table of trusted certs keyed off of consumer
//
// either way should return a string representation of the certificate
throw Exception("fetch_private_cert not implemented");
}/*}}}*/
public function build_signature(&$request, $consumer, $token) {/*{{{*/
$base_string = $request->get_signature_base_string();
$request->base_string = $base_string;
// Fetch the private key cert based on the request
$cert = $this->fetch_private_cert($request);
// Pull the private key ID from the certificate
$privatekeyid = openssl_get_privatekey($cert);
// Sign using the key
$ok = openssl_sign($base_string, $signature, $privatekeyid);
// Release the key resource
openssl_free_key($privatekeyid);
return base64_encode($signature);
} /*}}}*/
public function check_signature(&$request, $consumer, $token, $signature) {/*{{{*/
$decoded_sig = base64_decode($signature);
$base_string = $request->get_signature_base_string();
// Fetch the public key cert based on the request
$cert = $this->fetch_public_cert($request);
// Pull the public key ID from the certificate
$publickeyid = openssl_get_publickey($cert);
// Check the computed signature against the one passed in the query
$ok = openssl_verify($base_string, $decoded_sig, $publickeyid);
// Release the key resource
openssl_free_key($publickeyid);
return $ok == 1;
} /*}}}*/
}/*}}}*/
class OAuthRequest {/*{{{*/
private $parameters;
private $http_method;
private $http_url;
// for debug purposes
public $base_string;
public static $version = '1.0';
function __construct($http_method, $http_url, $parameters=NULL) {/*{{{*/
@$parameters or $parameters = array();
$this->parameters = $parameters;
$this->http_method = $http_method;
$this->http_url = $http_url;
}/*}}}*/
/**
* attempt to build up a request from what was passed to the server
*/
public static function from_request($http_method=NULL, $http_url=NULL, $parameters=NULL) {/*{{{*/
$scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on") ? 'http' : 'https';
@$http_url or $http_url = $scheme . '://' . $_SERVER['HTTP_HOST'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
@$http_method or $http_method = $_SERVER['REQUEST_METHOD'];
$request_headers = OAuthRequest::get_headers();
// let the library user override things however they'd like, if they know
// which parameters to use then go for it, for example XMLRPC might want to
// do this
if ($parameters) {
$req = new OAuthRequest($http_method, $http_url, $parameters);
} else {
// collect request parameters from query string (GET) and post-data (POST) if appropriate (note: POST vars have priority)
$req_parameters = $_GET;
if ($http_method == "POST" && @strstr($request_headers["Content-Type"], "application/x-www-form-urlencoded") ) {
$req_parameters = array_merge($req_parameters, $_POST);
}
// next check for the auth header, we need to do some extra stuff
// if that is the case, namely suck in the parameters from GET or POST
// so that we can include them in the signature
if (@substr($request_headers['Authorization'], 0, 6) == "OAuth ") {
$header_parameters = OAuthRequest::split_header($request_headers['Authorization']);
$parameters = array_merge($req_parameters, $header_parameters);
$req = new OAuthRequest($http_method, $http_url, $parameters);
} else $req = new OAuthRequest($http_method, $http_url, $req_parameters);
}
return $req;
}/*}}}*/
/**
* pretty much a helper function to set up the request
*/
public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=NULL) {/*{{{*/
@$parameters or $parameters = array();
$defaults = array("oauth_version" => OAuthRequest::$version,
"oauth_nonce" => OAuthRequest::generate_nonce(),
"oauth_timestamp" => OAuthRequest::generate_timestamp(),
"oauth_consumer_key" => $consumer->key);
$parameters = array_merge($defaults, $parameters);
if ($token) {
$parameters['oauth_token'] = $token->key;
}
return new OAuthRequest($http_method, $http_url, $parameters);
}/*}}}*/
public function set_parameter($name, $value) {/*{{{*/
$this->parameters[$name] = $value;
}/*}}}*/
public function get_parameter($name) {/*{{{*/
return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
}/*}}}*/
public function get_parameters() {/*{{{*/
return $this->parameters;
}/*}}}*/
/**
* Returns the normalized parameters of the request
*
* This will be all (except oauth_signature) parameters,
* sorted first by key, and if duplicate keys, then by
* value.
*
* The returned string will be all the key=value pairs
* concated by &.
*
* @return string
*/
public function get_signable_parameters() {/*{{{*/
// Grab all parameters
$params = $this->parameters;
// Remove oauth_signature if present
if (isset($params['oauth_signature'])) {
unset($params['oauth_signature']);
}
// Urlencode both keys and values
$keys = OAuthUtil::urlencode_rfc3986(array_keys($params));
$values = OAuthUtil::urlencode_rfc3986(array_values($params));
$params = array_combine($keys, $values);
// Sort by keys (natsort)
uksort($params, 'strcmp');
// Generate key=value pairs
$pairs = array();
foreach ($params as $key=>$value ) {
if (is_array($value)) {
// If the value is an array, it's because there are multiple
// with the same key, sort them, then add all the pairs
natsort($value);
foreach ($value as $v2) {
$pairs[] = $key . '=' . $v2;
}
} else {
$pairs[] = $key . '=' . $value;
}
}
// Return the pairs, concated with &
return implode('&', $pairs);
}/*}}}*/
/**
* Returns the base string of this request
*
* The base string defined as the method, the url
* and the parameters (normalized), each urlencoded
* and the concated with &.
*/
public function get_signature_base_string() {/*{{{*/
$parts = array(
$this->get_normalized_http_method(),
$this->get_normalized_http_url(),
$this->get_signable_parameters()
);
$parts = OAuthUtil::urlencode_rfc3986($parts);
return implode('&', $parts);
}/*}}}*/
/**
* just uppercases the http method
*/
public function get_normalized_http_method() {/*{{{*/
return strtoupper($this->http_method);
}/*}}}*/
/**
* parses the url and rebuilds it to be
* scheme://host/path
*/
public function get_normalized_http_url() {/*{{{*/
$parts = parse_url($this->http_url);
$port = @$parts['port'];
$scheme = $parts['scheme'];
$host = $parts['host'];
$path = @$parts['path'];
$port or $port = ($scheme == 'https') ? '443' : '80';
if (($scheme == 'https' && $port != '443')
|| ($scheme == 'http' && $port != '80')) {
$host = "$host:$port";
}
return "$scheme://$host$path";
}/*}}}*/
/**
* builds a url usable for a GET request
*/
public function to_url() {/*{{{*/
$out = $this->get_normalized_http_url() . "?";
$out .= $this->to_postdata();
return $out;
}/*}}}*/
/**
* builds the data one would send in a POST request
*
* TODO(morten.fangel):
* this function might be easily replaced with http_build_query()
* and corrections for rfc3986 compatibility.. but not sure
*/
public function to_postdata() {/*{{{*/
$total = array();
foreach ($this->parameters as $k => $v) {
if (is_array($v)) {
foreach ($v as $va) {
$total[] = OAuthUtil::urlencode_rfc3986($k) . "[]=" . OAuthUtil::urlencode_rfc3986($va);
}
} else {
$total[] = OAuthUtil::urlencode_rfc3986($k) . "=" . OAuthUtil::urlencode_rfc3986($v);
}
}
$out = implode("&", $total);
return $out;
}/*}}}*/
/**
* builds the Authorization: header
*/
public function to_header() {/*{{{*/
$out ='Authorization: OAuth realm=""';
$total = array();
foreach ($this->parameters as $k => $v) {
if (substr($k, 0, 5) != "oauth") continue;
if (is_array($v)) throw new OAuthException('Arrays not supported in headers');
$out .= ',' . OAuthUtil::urlencode_rfc3986($k) . '="' . OAuthUtil::urlencode_rfc3986($v) . '"';
}
return $out;
}/*}}}*/
public function __toString() {/*{{{*/
return $this->to_url();
}/*}}}*/
public function sign_request($signature_method, $consumer, $token) {/*{{{*/
$this->set_parameter("oauth_signature_method", $signature_method->get_name());
$signature = $this->build_signature($signature_method, $consumer, $token);
$this->set_parameter("oauth_signature", $signature);
}/*}}}*/
public function build_signature($signature_method, $consumer, $token) {/*{{{*/
$signature = $signature_method->build_signature($this, $consumer, $token);
return $signature;
}/*}}}*/
/**
* util function: current timestamp
*/
private static function generate_timestamp() {/*{{{*/
return time();
}/*}}}*/
/**
* util function: current nonce
*/
private static function generate_nonce() {/*{{{*/
$mt = microtime();
$rand = mt_rand();
return md5($mt . $rand); // md5s look nicer than numbers
}/*}}}*/
/**
* util function for turning the Authorization: header into
* parameters, has to do some unescaping
*/
private static function split_header($header) {/*{{{*/
$pattern = '/(([-_a-z]*)=("([^"]*)"|([^,]*)),?)/';
$offset = 0;
$params = array();
while (preg_match($pattern, $header, $matches, PREG_OFFSET_CAPTURE, $offset) > 0) {
$match = $matches[0];
$header_name = $matches[2][0];
$header_content = (isset($matches[5])) ? $matches[5][0] : $matches[4][0];
$params[$header_name] = OAuthUtil::urldecode_rfc3986( $header_content );
$offset = $match[1] + strlen($match[0]);
}
if (isset($params['realm'])) {
unset($params['realm']);
}
return $params;
}/*}}}*/
/**
* helper to try to sort out headers for people who aren't running apache
*/
private static function get_headers() {/*{{{*/
if (function_exists('apache_request_headers')) {
// we need this to get the actual Authorization: header
// because apache tends to tell us it doesn't exist
return apache_request_headers();
}
// otherwise we don't have apache and are just going to have to hope
// that $_SERVER actually contains what we need
$out = array();
foreach ($_SERVER as $key => $value) {
if (substr($key, 0, 5) == "HTTP_") {
// this is chaos, basically it is just there to capitalize the first
// letter of every word that is not an initial HTTP and strip HTTP
// code from przemek
$key = str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($key, 5)))));
$out[$key] = $value;
}
}
return $out;
}/*}}}*/
}/*}}}*/
class OAuthServer {/*{{{*/
protected $timestamp_threshold = 300; // in seconds, five minutes
protected $version = 1.0; // hi blaine
protected $signature_methods = array();
protected $data_store;
function __construct($data_store) {/*{{{*/
$this->data_store = $data_store;
}/*}}}*/
public function add_signature_method($signature_method) {/*{{{*/
$this->signature_methods[$signature_method->get_name()] =
$signature_method;
}/*}}}*/
// high level functions
/**
* process a request_token request
* returns the request token on success
*/
public function fetch_request_token(&$request) {/*{{{*/
$this->get_version($request);
$consumer = $this->get_consumer($request);
// no token required for the initial token request
$token = NULL;
$this->check_signature($request, $consumer, $token);
$new_token = $this->data_store->new_request_token($consumer);
return $new_token;
}/*}}}*/
/**
* process an access_token request
* returns the access token on success
*/
public function fetch_access_token(&$request) {/*{{{*/
$this->get_version($request);
$consumer = $this->get_consumer($request);
// requires authorized request token
$token = $this->get_token($request, $consumer, "request");
$this->check_signature($request, $consumer, $token);
$new_token = $this->data_store->new_access_token($token, $consumer);
return $new_token;
}/*}}}*/
/**
* verify an api call, checks all the parameters
*/
public function verify_request(&$request) {/*{{{*/
$this->get_version($request);
$consumer = $this->get_consumer($request);
$token = $this->get_token($request, $consumer, "access");
$this->check_signature($request, $consumer, $token);
return array($consumer, $token);
}/*}}}*/
// Internals from here
/**
* version 1
*/
private function get_version(&$request) {/*{{{*/
$version = $request->get_parameter("oauth_version");
if (!$version) {
$version = 1.0;
}
if ($version && $version != $this->version) {
throw new OAuthException("OAuth version '$version' not supported");
}
return $version;
}/*}}}*/
/**
* figure out the signature with some defaults
*/
private function get_signature_method(&$request) {/*{{{*/
$signature_method =
@$request->get_parameter("oauth_signature_method");
if (!$signature_method) {
$signature_method = "PLAINTEXT";
}
if (!in_array($signature_method,
array_keys($this->signature_methods))) {
throw new OAuthException(
"Signature method '$signature_method' not supported try one of the following: " . implode(", ", array_keys($this->signature_methods))
);
}
return $this->signature_methods[$signature_method];
}/*}}}*/
/**
* try to find the consumer for the provided request's consumer key
*/
private function get_consumer(&$request) {/*{{{*/
$consumer_key = @$request->get_parameter("oauth_consumer_key");
if (!$consumer_key) {
throw new OAuthException("Invalid consumer key");
}
$consumer = $this->data_store->lookup_consumer($consumer_key);
if (!$consumer) {
throw new OAuthException("Invalid consumer");
}
return $consumer;
}/*}}}*/
/**
* try to find the token for the provided request's token key
*/
private function get_token(&$request, $consumer, $token_type="access") {/*{{{*/
$token_field = @$request->get_parameter('oauth_token');
$token = $this->data_store->lookup_token(
$consumer, $token_type, $token_field
);
if (!$token) {
throw new OAuthException("Invalid $token_type token: $token_field");
}
return $token;
}/*}}}*/
/**
* all-in-one function to check the signature on a request
* should guess the signature method appropriately
*/
private function check_signature(&$request, $consumer, $token) {/*{{{*/
// this should probably be in a different method
$timestamp = @$request->get_parameter('oauth_timestamp');
$nonce = @$request->get_parameter('oauth_nonce');
$this->check_timestamp($timestamp);
$this->check_nonce($consumer, $token, $nonce, $timestamp);
$signature_method = $this->get_signature_method($request);
$signature = $request->get_parameter('oauth_signature');
$valid_sig = $signature_method->check_signature(
$request,
$consumer,
$token,
$signature
);
if (!$valid_sig) {
throw new OAuthException("Invalid signature");
}
}/*}}}*/
/**
* check that the timestamp is new enough
*/
private function check_timestamp($timestamp) {/*{{{*/
// verify that timestamp is recentish
$now = time();
if ($now - $timestamp > $this->timestamp_threshold) {
throw new OAuthException("Expired timestamp, yours $timestamp, ours $now");
}
}/*}}}*/
/**
* check that the nonce is not repeated
*/
private function check_nonce($consumer, $token, $nonce, $timestamp) {/*{{{*/
// verify that the nonce is uniqueish
$found = $this->data_store->lookup_nonce($consumer, $token, $nonce, $timestamp);
if ($found) {
throw new OAuthException("Nonce already used: $nonce");
}
}/*}}}*/
}/*}}}*/
class OAuthDataStore {/*{{{*/
function lookup_consumer($consumer_key) {/*{{{*/
// implement me
}/*}}}*/
function lookup_token($consumer, $token_type, $token) {/*{{{*/
// implement me
}/*}}}*/
function lookup_nonce($consumer, $token, $nonce, $timestamp) {/*{{{*/
// implement me
}/*}}}*/
function new_request_token($consumer) {/*{{{*/
// return a new token attached to this consumer
}/*}}}*/
function new_access_token($token, $consumer) {/*{{{*/
// return a new access token attached to this consumer
// for the user associated with this token if the request token
// is authorized
// should also invalidate the request token
}/*}}}*/
}/*}}}*/
/* A very naive dbm-based oauth storage
*/
class SimpleOAuthDataStore extends OAuthDataStore {/*{{{*/
private $dbh;
function __construct($path = "oauth.gdbm") {/*{{{*/
$this->dbh = dba_popen($path, 'c', 'gdbm');
}/*}}}*/
function __destruct() {/*{{{*/
dba_close($this->dbh);
}/*}}}*/
function lookup_consumer($consumer_key) {/*{{{*/
$rv = dba_fetch("consumer_$consumer_key", $this->dbh);
if ($rv === FALSE) {
return NULL;
}
$obj = unserialize($rv);
if (!($obj instanceof OAuthConsumer)) {
return NULL;
}
return $obj;
}/*}}}*/
function lookup_token($consumer, $token_type, $token) {/*{{{*/
$rv = dba_fetch("${token_type}_${token}", $this->dbh);
if ($rv === FALSE) {
return NULL;
}
$obj = unserialize($rv);
if (!($obj instanceof OAuthToken)) {
return NULL;
}
return $obj;
}/*}}}*/
function lookup_nonce($consumer, $token, $nonce, $timestamp) {/*{{{*/
if (dba_exists("nonce_$nonce", $this->dbh)) {
return TRUE;
} else {
dba_insert("nonce_$nonce", "1", $this->dbh);
return FALSE;
}
}/*}}}*/
function new_token($consumer, $type="request") {/*{{{*/
$key = md5(time());
$secret = time() + time();
$token = new OAuthToken($key, md5(md5($secret)));
if (!dba_insert("${type}_$key", serialize($token), $this->dbh)) {
throw new OAuthException("doooom!");
}
return $token;
}/*}}}*/
function new_request_token($consumer) {/*{{{*/
return $this->new_token($consumer, "request");
}/*}}}*/
function new_access_token($token, $consumer) {/*{{{*/
$token = $this->new_token($consumer, 'access');
dba_delete("request_" . $token->key, $this->dbh);
return $token;
}/*}}}*/
}/*}}}*/
class OAuthUtil {/*{{{*/
public static function urlencode_rfc3986($input) {/*{{{*/
if (is_array($input)) {
return array_map(array('OAuthUtil','urlencode_rfc3986'), $input);
} else if (is_scalar($input)) {
return str_replace('+', ' ',
str_replace('%7E', '~', rawurlencode($input)));
} else {
return '';
}
}/*}}}*/
// This decode function isn't taking into consideration the above
// modifications to the encoding process. However, this method doesn't
// seem to be used anywhere so leaving it as is.
public static function urldecode_rfc3986($string) {/*{{{*/
return rawurldecode($string);
}/*}}}*/
}/*}}}*/
================================================
FILE: ConnectToTwitter/Plugin.php
================================================
finishComment = array('ConnectToTwitter_Plugin', 'postToTwitter');
Typecho_Plugin::factory('Widget_Archive')->beforeRender = array('ConnectToTwitter_Plugin', 'initComment');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate() {
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$consumerKey = new Typecho_Widget_Helper_Form_Element_Text('consumerKey', NULL, '',
_t('Consumer Key'), _t('Your application consumer key from Twitter.com. '));
$form->addInput($consumerKey->addRule('required', _t('You must give the Consumer Key from Twitter.com')));
$consumerSecret = new Typecho_Widget_Helper_Form_Element_Text('consumerSecret', NULL, '',
_t('Consumer Secret'), _t('Your application consumer secret from Twitter.com. '));
$form->addInput($consumerSecret->addRule('required', _t('You must give the Consumer Key from Twitter.com')));
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
public static function initComment($api)
{
session_start();
$options = Typecho_Widget::widget('Widget_Options');
$config = $options->plugin('ConnectToTwitter');
//发送请求到twitter
if(isset($api->request->connect_to_twitter))
{
$to = new TwitterOAuth($config->consumerKey, $config->consumerSecret);
$tok = $to->getRequestToken();
Typecho_Cookie::set('oauth_request_token', $tok['oauth_token']);
Typecho_Cookie::set('oauth_request_token_secret', $tok['oauth_token_secret']);
/* Build the authorization URL */
$request_link = $to->getAuthorizeURL($tok['oauth_token']);
header('Location:'.$request_link);
}
//从twitter返回
if(isset($api->request->oauth_token)) {
if(Typecho_Cookie::get('oauth_request_token') && Typecho_Cookie::get('oauth_request_token_secret'))
{
$to = new TwitterOAuth($config->consumerKey, $config->consumerSecret, Typecho_Cookie::get('oauth_request_token'), Typecho_Cookie::get('oauth_request_token_secret'));
$tok = $to->getAccessToken();
Typecho_Cookie::set('oauth_access_token', $tok['oauth_token'], time()+60*60*24*30);
Typecho_Cookie::set('oauth_access_token_secret', $tok['oauth_token_secret'], time()+60*60*24*30);
$info_json = $to->OAuthRequest('https://twitter.com/account/verify_credentials.json', array(), 'GET');
$info = Typecho_Json::decode($info_json, true);
self::twitterLogin($info, $api);
}
}
}
//登录,暂时做为setcookie,以后要和用户帐号相关联
public static function twitterLogin($info, $api)
{
if (!empty($info['screen_name'])) {
Typecho_Cookie::set('__typecho_remember_author', $info['screen_name'], time()+60*60*24*30);
}
if (!empty($info['url'])) {
Typecho_Cookie::set('__typecho_remember_url', $info['url'], time()+60*60*24*30);
}
}
//发送信息到twitter
public static function postToTwitter($api)
{
if(Typecho_Cookie::get('oauth_access_token') && Typecho_Cookie::get('oauth_access_token_secret') && $api->request->post_to_twitter) {
$options = Typecho_Widget::widget('Widget_Options');
$config = $options->plugin('ConnectToTwitter');
$to = new TwitterOAuth($config->consumerKey, $config->consumerSecret, Typecho_Cookie::get('oauth_access_token'), Typecho_Cookie::get('oauth_access_token_secret'));
$url_array = array();
$url_array = explode('?', $api->request->getReferer());
$url = $url_array[0] . '#comment-' . $api->coid;
$post = $api->text . ' ( from ' . $url . ' ) ';
$twitter = $to->OAuthRequest('https://twitter.com/statuses/update.xml', array('status' => $post), 'POST');
}
return $comment;
}
function showButton()
{
if(Typecho_Cookie::get('oauth_access_token') && Typecho_Cookie::get('oauth_access_token_secret')) {
echo '';
} else {
echo '';
}
}
}
================================================
FILE: ConnectToTwitter/twitterOAuth.php
================================================
http_status; }
function lastAPICall() { return $this->last_api_call; }
/**
* construct TwitterOAuth object
*/
function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {/*{{{*/
$this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
$this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
if (!empty($oauth_token) && !empty($oauth_token_secret)) {
$this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
} else {
$this->token = NULL;
}
}/*}}}*/
/**
* Get a request_token from Twitter
*
* @returns a key/value array containing oauth_token and oauth_token_secret
*/
function getRequestToken() {/*{{{*/
$r = $this->oAuthRequest($this->requestTokenURL());
$token = $this->oAuthParseResponse($r);
$this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
return $token;
}/*}}}*/
/**
* Parse a URL-encoded OAuth response
*
* @return a key/value array
*/
function oAuthParseResponse($responseString) {
$r = array();
foreach (explode('&', $responseString) as $param) {
$pair = explode('=', $param, 2);
if (count($pair) != 2) continue;
$r[urldecode($pair[0])] = urldecode($pair[1]);
}
return $r;
}
/**
* Get the authorize URL
*
* @returns a string
*/
function getAuthorizeURL($token) {/*{{{*/
if (is_array($token)) $token = $token['oauth_token'];
return $this->authorizeURL() . '?oauth_token=' . $token;
}/*}}}*/
/**
* Exchange the request token and secret for an access token and
* secret, to sign API calls.
*
* @returns array("oauth_token" => the access token,
* "oauth_token_secret" => the access secret)
*/
function getAccessToken($token = NULL) {/*{{{*/
$r = $this->oAuthRequest($this->accessTokenURL());
$token = $this->oAuthParseResponse($r);
$this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
return $token;
}/*}}}*/
/**
* Format and sign an OAuth / API request
*/
function oAuthRequest($url, $args = array(), $method = NULL) {/*{{{*/
if (empty($method)) $method = empty($args) ? "GET" : "POST";
$req = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $args);
$req->sign_request($this->sha1_method, $this->consumer, $this->token);
switch ($method) {
case 'GET': return $this->http($req->to_url());
case 'POST': return $this->http($req->get_normalized_http_url(), $req->to_postdata());
}
}/*}}}*/
/**
* Make an HTTP request
*
* @return API results
*/
function http($url, $post_data = null) {/*{{{*/
$ch = curl_init();
if (defined("CURL_CA_BUNDLE_PATH")) curl_setopt($ch, CURLOPT_CAINFO, CURL_CA_BUNDLE_PATH);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//////////////////////////////////////////////////
///// Set to 1 to verify Twitter's SSL Cert //////
//////////////////////////////////////////////////
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
if (isset($post_data)) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
$response = curl_exec($ch);
$this->http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$this->last_api_call = $url;
curl_close ($ch);
return $response;
}/*}}}*/
}/*}}}*/
================================================
FILE: Creole/Creole_Wiki.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Creole_Wiki.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* The baseline abstract parser class.
*/
require_once 'Parse.inc.php';
/**
* The baseline abstract render class.
*/
require_once 'Render.inc.php';
/**
* Parse structured wiki text and render into arbitrary formats such as XHTML.
*
* This is the "master" class for handling the management and convenience
* functions to transform Wiki-formatted text.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* $parseConf = array(
* 'Include' => array(
* 'base' => '/path/to/scripts/'
* )
* );
*
*
* Note that most default rules do not need any parsing configuration.
*
* @access public
*
* @var array
*
*/
public $parseConf = array();
/**
*
* Custom configuration for rules at the rendering stage.
*
* Because rendering may be different for each target format, the
* first-level element in this array is always a format name (e.g.,
* 'Xhtml').
*
* Within that first level element, the subsequent elements match the
* $parseConf format. That is, the sub-key is the rendering rule name,
* and the sub-value is an array of key-value configuration pairs
* corresponding to the $conf property in the target rendering rule.
*
* @access public
*
* @var array
*
*/
public $renderConf = array(
'Docbook' => array(),
'Latex' => array(),
'Pdf' => array(),
'Plain' => array(),
'Rtf' => array(),
'Xhtml' => array()
);
/**
*
* Custom configuration for the output format itself.
*
* Even though Text_Wiki will render the tokens from parsed text,
* the format itself may require some configuration. For example,
* RTF needs to know font names and sizes, PDF requires page layout
* information, and DocBook needs a section hierarchy. This array
* matches the $conf property of the the format-level renderer
* (e.g., Text_Wiki_Render_Xhtml).
*
* In this array, the key is the rendering format name, and the value is
* an array of key-value configuration pairs corresponding to the $conf
* property in the rendering format rule.
*
* @access public
*
* @var array
*
*/
public $formatConf = array(
'Docbook' => array(),
'Latex' => array(),
'Pdf' => array(),
'Plain' => array(),
'Rtf' => array(),
'Xhtml' => array()
);
/**
*
* The delimiter for token numbers of parsed elements in source text.
*
* @access public
*
* @var string
*
*/
public $delim = "\xFF";
/**
*
* The tokens generated by rules as the source text is parsed.
*
* As Text_Wiki applies rule classes to the source text, it will
* replace portions of the text with a delimited token number. This
* is the array of those tokens, representing the replaced text and
* any options set by the parser for that replaced text.
*
* The tokens array is sequential; each element is itself a sequential
* array where element 0 is the name of the rule that generated the
* token, and element 1 is an associative array where the key is an
* option name and the value is an option value.
*
* @access private
*
* @var array
*
*/
public $tokens = array();
/**
* How many tokens generated pro rules.
*
* Intended to load only necessary render objects
*
* @access private
* @var array
*/
private $_countRulesTokens = array();
/**
*
* The source text to which rules will be applied.
*
* This text will be transformed in-place, which means that it will
* change as the rules are applied.
*
* @access public
*
* @var string
*
*/
public $source = '';
/**
* The output text
*
* @var string
*/
protected $output = '';
/**
*
* Array of rule parsers.
*
* Text_Wiki creates one instance of every rule that is applied to
* the source text; this array holds those instances. The array key
* is the rule name, and the array value is an instance of the rule
* class.
*
* @access private
*
* @var array
*
*/
protected $parseObj = array();
/**
*
* Array of rule renderers.
*
* Text_Wiki creates one instance of every rule that is applied to
* the source text; this array holds those instances. The array key
* is the rule name, and the array value is an instance of the rule
* class.
*
* @access private
*
* @var array
*
*/
protected $renderObj = array();
/**
*
* Array of format renderers.
*
* @access private
*
* @var array
*
*/
protected $formatObj = array();
/**
*
* Array of paths to search, in order, for parsing and rendering rules.
*
* @access private
*
* @var array
*
*/
protected $path = array(
'parse' => array(),
'render' => array()
);
/**
*
* The directory separator character.
*
* @access private
*
* @var string
*
*/
private $_dirSep = DIRECTORY_SEPARATOR;
/**
* Temporary configuration variable
*
* @var string
*/
protected $renderingType = 'preg';
/**
* Stack of rendering callbacks
*
* @var Array
*/
private $_renderCallbacks = array();
/**
* Current output block
*
* @var string
*/
private $_block;
/**
* A stack of blocks
*
* @param Array
*/
private $_blocks;
/**
*
* Constructor.
*
* **DEPRECATED**
* Please use the singleton() or factory() methods.
*
* @access public
*
* @param array $rules The set of rules to load for this object. Defaults
* to null, which will load the default ruleset for this parser.
*/
function __construct($rules = null)
{
if (is_array($rules)) {
$this->rules = array();
foreach ($rules as $rule) {
$this->rules[] = ucfirst($rule);
}
}
/*
$this->addPath(
'parse',
$this->fixPath(dirname(__FILE__)) . 'Wiki/Parse/Default/'
);
*/
$this->addPath(
'parse', $this->fixPath(dirname(__FILE__) . '/Parse/')
);
$this->addPath(
'render',
$this->fixPath(dirname(__FILE__) . '/Render/' )
);
$this->renderingType = 'char';
$this->setRenderConf('xhtml', 'center', 'css', 'center');
$this->setRenderConf('xhtml', 'url', 'target', null);
}
/**
* Singleton.
*
* This avoids instantiating multiple Text_Wiki instances where a number
* of objects are required in one call, e.g. to save memory in a
* CMS invironment where several parsers are required in a single page.
*
* $single = & singleton();
*
* or
*
* $single = & singleton('Parser', array('Prefilter', 'Delimiter', 'Code', 'Function',
* 'Html', 'Raw', 'Include', 'Embed', 'Anchor', 'Heading', 'Toc', 'Horiz',
* 'Break', 'Blockquote', 'List', 'Deflist', 'Table', 'Image', 'Phplookup',
* 'Center', 'Newline', 'Paragraph', 'Url', 'Freelink', 'Interwiki', 'Wikilink',
* 'Colortext', 'Strong', 'Bold', 'Emphasis', 'Italic', 'Underline', 'Tt',
* 'Superscript', 'Subscript', 'Revise', 'Tighten'));
*
* Call using a subset of this list. The order of passing rulesets in the
* $rules array is important!
*
* After calling this, call $single->setParseConf(), setRenderConf() or setFormatConf()
* as usual for a constructed object of this class.
*
* The internal static array of singleton objects has no index on the parser
* rules, the only index is on the parser name. So if you call this multiple
* times with different rules but the same parser name, you will get the same
* static parser object each time.
*
* @access public
* @static
* @since Method available since Release 1.1.0
* @param string $parser The parser to be used (defaults to 'Default').
* @param array $rules The set of rules to instantiate the object. This
* will only be used when the first call to singleton is made, if included
* in further calls it will be effectively ignored.
* @return &object a reference to the Text_Wiki unique instantiation.
*/
/*
public function &singleton($parser = 'Default', $rules = null)
{
static $only = array();
if (!isset($only[$parser])) {
$ret = & Text_Wiki::factory($parser, $rules);
if (Text_Wiki::isError($ret)) {
return $ret;
}
$only[$parser] =& $ret;
}
return $only[$parser];
}
*/
/**
* Returns a Text_Wiki Parser class for the specified parser.
*
* @access public
* @static
* @param string $parser The name of the parse to instantiate
* you need to have Text_Wiki_XXX installed to use $parser = 'XXX', it's E_FATAL
* @param array $rules The rules to pass into the constructor
* {@see Text_Wiki::singleton} for a list of rules
* @return Text_Wiki a Parser object extended from Text_Wiki
*/
/*
public function &factory($parser = 'Default', $rules = null)
{
$class = 'Text_Wiki_' . ucfirst(strtolower($parser));
$file = str_replace('_', '/', $class).'.php';
if (!class_exists($class)) {
require_once $file;
if (!class_exists($class)) {
return Text_Wiki::error(
'Class ' . $class . ' does not exist after requiring '. $file .
', install package ' . $class . "\n");
}
}
$obj =& new $class($rules);
return $obj;
}
*/
/**
*
* Set parser configuration for a specific rule and key.
*
* @access public
*
* @param string $rule The parse rule to set config for.
*
* @param array|string $arg1 The full config array to use for the
* parse rule, or a conf key in that array.
*
* @param string $arg2 The config value for the key.
*
* @return void
*
*/
public function setParseConf($rule, $arg1, $arg2 = null)
{
$rule = ucwords(strtolower($rule));
if (! isset($this->parseConf[$rule])) {
$this->parseConf[$rule] = array();
}
// if first arg is an array, use it as the entire
// conf array for the rule. otherwise, treat arg1
// as a key and arg2 as a value for the rule conf.
if (is_array($arg1)) {
$this->parseConf[$rule] = $arg1;
} else {
$this->parseConf[$rule][$arg1] = $arg2;
}
}
/**
*
* Get parser configuration for a specific rule and key.
*
* @access public
*
* @param string $rule The parse rule to get config for.
*
* @param string $key A key in the conf array; if null,
* returns the entire conf array.
*
* @return mixed The whole conf array if no key is specified,
* or the specific conf key value.
*
*/
public function getParseConf($rule, $key = null)
{
$rule = ucwords(strtolower($rule));
// the rule does not exist
if (! isset($this->parseConf[$rule])) {
return null;
}
// no key requested, return the whole array
if (is_null($key)) {
return $this->parseConf[$rule];
}
// does the requested key exist?
if (isset($this->parseConf[$rule][$key])) {
// yes, return that value
return $this->parseConf[$rule][$key];
} else {
// no
return null;
}
}
/**
*
* Set renderer configuration for a specific format, rule, and key.
*
* @access public
*
* @param string $format The render format to set config for.
*
* @param string $rule The render rule to set config for in the format.
*
* @param array|string $arg1 The config array, or the config key
* within the render rule.
*
* @param string $arg2 The config value for the key.
*
* @return void
*
*/
function setRenderConf($format, $rule, $arg1, $arg2 = null)
{
$format = ucwords(strtolower($format));
$rule = ucwords(strtolower($rule));
if (! isset($this->renderConf[$format])) {
$this->renderConf[$format] = array();
}
if (! isset($this->renderConf[$format][$rule])) {
$this->renderConf[$format][$rule] = array();
}
// if first arg is an array, use it as the entire
// conf array for the render rule. otherwise, treat arg1
// as a key and arg2 as a value for the render rule conf.
if (is_array($arg1)) {
$this->renderConf[$format][$rule] = $arg1;
} else {
$this->renderConf[$format][$rule][$arg1] = $arg2;
}
}
/**
*
* Get renderer configuration for a specific format, rule, and key.
*
* @access public
*
* @param string $format The render format to get config for.
*
* @param string $rule The render format rule to get config for.
*
* @param string $key A key in the conf array; if null,
* returns the entire conf array.
*
* @return mixed The whole conf array if no key is specified,
* or the specific conf key value.
*
*/
function getRenderConf($format, $rule, $key = null)
{
$format = ucwords(strtolower($format));
$rule = ucwords(strtolower($rule));
if (! isset($this->renderConf[$format]) ||
! isset($this->renderConf[$format][$rule])) {
return null;
}
// no key requested, return the whole array
if (is_null($key)) {
return $this->renderConf[$format][$rule];
}
// does the requested key exist?
if (isset($this->renderConf[$format][$rule][$key])) {
// yes, return that value
return $this->renderConf[$format][$rule][$key];
} else {
// no
return null;
}
}
/**
*
* Set format configuration for a specific rule and key.
*
* @access public
*
* @param string $format The format to set config for.
*
* @param string $key The config key within the format.
*
* @param string $val The config value for the key.
*
* @return void
*
*/
function setFormatConf($format, $arg1, $arg2 = null)
{
if (! is_array($this->formatConf[$format])) {
$this->formatConf[$format] = array();
}
// if first arg is an array, use it as the entire
// conf array for the format. otherwise, treat arg1
// as a key and arg2 as a value for the format conf.
if (is_array($arg1)) {
$this->formatConf[$format] = $arg1;
} else {
$this->formatConf[$format][$arg1] = $arg2;
}
}
/**
*
* Get configuration for a specific format and key.
*
* @access public
*
* @param string $format The format to get config for.
*
* @param mixed $key A key in the conf array; if null,
* returns the entire conf array.
*
* @return mixed The whole conf array if no key is specified,
* or the specific conf key value.
*
*/
function getFormatConf($format, $key = null)
{
// the format does not exist
if (! isset($this->formatConf[$format])) {
return null;
}
// no key requested, return the whole array
if (is_null($key)) {
return $this->formatConf[$format];
}
// does the requested key exist?
if (isset($this->formatConf[$format][$key])) {
// yes, return that value
return $this->formatConf[$format][$key];
} else {
// no
return null;
}
}
/**
*
* Inserts a rule into to the rule set.
*
* @access public
*
* @param string $name The name of the rule. Should be different from
* all other keys in the rule set.
*
* @param string $tgt The rule after which to insert this new rule. By
* default (null) the rule is inserted at the end; if set to '', inserts
* at the beginning.
*
* @return void
*
*/
function insertRule($name, $tgt = null)
{
$name = ucwords(strtolower($name));
if (! is_null($tgt)) {
$tgt = ucwords(strtolower($tgt));
}
// does the rule name to be inserted already exist?
if (in_array($name, $this->rules)) {
// yes, return
return null;
}
// the target name is not null, and not '', but does not exist
// in the list of rules. this means we're trying to insert after
// a target key, but the target key isn't there.
if (! is_null($tgt) && $tgt != '' &&
! in_array($tgt, $this->rules)) {
return false;
}
// if $tgt is null, insert at the end. We know this is at the
// end (instead of resetting an existing rule) becuase we exited
// at the top of this method if the rule was already in place.
if (is_null($tgt)) {
$this->rules[] = $name;
return true;
}
// save a copy of the current rules, then reset the rule set
// so we can insert in the proper place later.
// where to insert the rule?
if ($tgt == '') {
// insert at the beginning
array_unshift($this->rules, $name);
return true;
}
// insert after the named rule
$tmp = $this->rules;
$this->rules = array();
foreach ($tmp as $val) {
$this->rules[] = $val;
if ($val == $tgt) {
$this->rules[] = $name;
}
}
return true;
}
/**
*
* Delete (remove or unset) a rule from the $rules property.
*
* @access public
*
* @param string $rule The name of the rule to remove.
*
* @return void
*
*/
function deleteRule($name)
{
$name = ucwords(strtolower($name));
$key = array_search($name, $this->rules);
if ($key !== false) {
unset($this->rules[$key]);
}
}
/**
*
* Change from one rule to another in-place.
*
* @access public
*
* @param string $old The name of the rule to change from.
*
* @param string $new The name of the rule to change to.
*
* @return void
*
*/
function changeRule($old, $new)
{
$old = ucwords(strtolower($old));
$new = ucwords(strtolower($new));
$key = array_search($old, $this->rules);
if ($key !== false) {
// delete the new name , case it was already there
$this->deleteRule($new);
$this->rules[$key] = $new;
}
}
/**
*
* Enables a rule so that it is applied when parsing.
*
* @access public
*
* @param string $rule The name of the rule to enable.
*
* @return void
*
*/
function enableRule($name)
{
$name = ucwords(strtolower($name));
$key = array_search($name, $this->disable);
if ($key !== false) {
unset($this->disable[$key]);
}
}
/**
*
* Disables a rule so that it is not applied when parsing.
*
* @access public
*
* @param string $rule The name of the rule to disable.
*
* @return void
*
*/
function disableRule($name)
{
$name = ucwords(strtolower($name));
$key = array_search($name, $this->disable);
if ($key === false) {
$this->disable[] = $name;
}
}
/**
*
* Parses and renders the text passed to it, and returns the results.
*
* First, the method parses the source text, applying rules to the
* text as it goes. These rules will modify the source text
* in-place, replacing some text with delimited tokens (and
* populating the $this->tokens array as it goes).
*
* Next, the method renders the in-place tokens into the requested
* output format.
*
* Finally, the method returns the transformed text. Note that the
* source text is transformed in place; once it is transformed, it is
* no longer the same as the original source text.
*
* @access public
*
* @param string $text The source text to which wiki rules should be
* applied, both for parsing and for rendering.
*
* @param string $format The target output format, typically 'xhtml'.
* If a rule does not support a given format, the output from that
* rule is rule-specific.
*
* @return string The transformed wiki text.
*
*/
function transform($text, $format = 'Xhtml')
{
$this->parse($text);
return $this->render($format);
}
/**
*
* Sets the $_source text property, then parses it in place and
* retains tokens in the $_tokens array property.
*
* @access public
*
* @param string $text The source text to which wiki rules should be
* applied, both for parsing and for rendering.
*
* @return void
*
*/
function parse($text)
{
// set the object property for the source text
$this->source = $text;
// reset the tokens.
$this->tokens = array();
$this->_countRulesTokens = array();
// apply the parse() method of each requested rule to the source
// text.
foreach ($this->rules as $name) {
// do not parse the rules listed in $disable
if (! in_array($name, $this->disable)) {
// load the parsing object
$this->loadParseObj($name);
// load may have failed; only parse if
// an object is in the array now
if (is_object($this->parseObj[$name])) {
$this->parseObj[$name]->parse();
}
}
}
}
/**
*
* Renders tokens back into the source text, based on the requested format.
*
* @access public
*
* @param string $format The target output format, typically 'xhtml'.
* If a rule does not support a given format, the output from that
* rule is rule-specific.
*
* @return string The transformed wiki text.
*
*/
function render($format = 'Xhtml')
{
// the rendering method we're going to use from each rule
$format = ucwords(strtolower($format));
// the eventual output text
$this->output = '';
// when passing through the parsed source text, keep track of when
// we are in a delimited section
$in_delim = false;
// when in a delimited section, capture the token key number
$key = '';
// load the format object, or crap out if we can't find it
$result = $this->loadFormatObj($format);
if ($this->isError($result)) {
return $result;
}
/*
* hunked by feelinglucky..
// pre-rendering activity
if (is_object($this->formatObj[$format])) {
$this->output .= $this->formatObj[$format]->pre();
}
*/
// load the render objects
foreach (array_keys($this->_countRulesTokens) as $rule) {
$this->loadRenderObj($format, $rule);
}
if ($this->renderingType == 'preg') {
$this->output = preg_replace_callback('/'.$this->delim.'(\d+)'.$this->delim.'/',
array(&$this, '_renderToken'),
$this->source);
/*
//Damn strtok()! Why does it "skip" empty parts of the string. It's useless now!
} elseif ($this->renderingType == 'strtok') {
echo ''.htmlentities($this->source).''; $t = strtok($this->source, $this->delim); $inToken = true; $i = 0; while ($t !== false) { echo 'Token: '.$i.'
"'.htmlentities($t).'"
"; } // ending if ($type == 'end') { return $pad . "\n"; } } } ?> ================================================ FILE: Creole/Render/Xhtml/Bold.php ================================================ * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 * @version CVS: $Id: Bold.php 182 2008-09-14 15:56:00Z i.feelinglucky $ * @link http://pear.php.net/package/Text_Wiki */ /** * This class renders bold text in XHTML. * * @category Text * @package Text_Wiki * @author Paul M. Jones
'css_code' => null, // class for generic
'css_php' => null, // class for PHP
'css_html' => null, // class for HTML
'css_filename' => null // class for optional filename
);
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
$text = $options['text'];
$attr = $options['attr'];
$type = strtolower($attr['type']);
$css = $this->formatConf(' class="%s"', 'css');
$css_code = $this->formatConf(' class="%s"', 'css_code');
$css_php = $this->formatConf(' class="%s"', 'css_php');
$css_html = $this->formatConf(' class="%s"', 'css_html');
$css_filename = $this->formatConf(' class="%s"', 'css_filename');
if ($type == 'php') {
if (substr($options['text'], 0, 5) != '"; // ... tags)
ob_start();
highlight_string($text);
$text = ob_get_contents();
ob_end_clean();
// replace
tags with simple newlines.
// replace non-breaking space with simple spaces.
// translate HTML and color to XHTML and style.
// courtesy of research by A. Kalin :-).
$map = array(
'
' => "\n",
' ' => ' ',
' '' => '',
'color="' => 'style="color:'
);
$text = strtr($text, $map);
// get rid of the last newline inside the code block
// (becuase higlight_string puts one there)
if (substr($text, -8) == "\n") {
$text = substr($text, 0, -8) . "";
}
// replace all tags with classed tags
if ($css_php) {
$text = str_replace('', "", $text);
}
// done
$text = "$text
";
} elseif ($type == 'html' || $type == 'xhtml') {
// HTML code example:
// add opening and closing tags,
// convert tabs to four spaces,
// convert entities.
$text = str_replace("\t", " ", $text);
$text = "\n$text\n";
$text = $this->textEncode($text);
$text = "$text
";
} else {
// generic code example:
// convert tabs to four spaces,
// convert entities.
$text = str_replace("\t", " ", $text);
$text = $this->textEncode($text);
$text = "$text
";
}
if ($css_filename && isset($attr['filename'])) {
$text = "" .
$attr['filename'] . '' . $text;
}
return "\n$text\n\n";
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Colortext.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Colortext.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders colored text in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Colortext extends Text_Wiki_Render {
var $colors = array(
'aqua',
'black',
'blue',
'fuchsia',
'gray',
'green',
'lime',
'maroon',
'navy',
'olive',
'purple',
'red',
'silver',
'teal',
'white',
'yellow'
);
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
$type = $options['type'];
$color = $options['color'];
if (! in_array($color, $this->colors) && $color{0} != '#') {
$color = '#' . $color;
}
if ($type == 'start') {
return "";
}
if ($options['type'] == 'end') {
return '';
}
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Deflist.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Deflist.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders definition lists in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Deflist extends Text_Wiki_Render {
var $conf = array(
'css_dl' => null,
'css_dt' => null,
'css_dd' => null
);
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
$type = $options['type'];
$pad = " ";
switch ($type) {
case 'list_start':
$css = $this->formatConf(' class="%s"', 'css_dl');
return "\n";
break;
case 'list_end':
return "
\n\n";
break;
case 'term_start':
$css = $this->formatConf(' class="%s"', 'css_dt');
return $pad . "";
break;
case 'term_end':
return " \n";
break;
case 'narr_start':
$css = $this->formatConf(' class="%s"', 'css_dd');
return $pad . $pad . "";
break;
case 'narr_end':
return " \n";
break;
default:
return '';
}
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Delete.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Delete.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders underlined text in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Delete extends Text_Wiki_Render {
var $conf = array(
'css' => null
);
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
if ($options['type'] == 'start') {
//$css = $this->formatConf(' class="%s"', 'css');
//return "";
//return "";
return "";
}
if ($options['type'] == 'end') {
return '';
}
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Delimiter.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Delimiter.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class set back the replaced delimiters in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Delimiter extends Text_Wiki_Render {
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
return $options['text'];
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Embed.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Embed.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class replaces the embedded php output in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Embed extends Text_Wiki_Render {
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
return $options['text'];
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Emphasis.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Emphasis.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders emphasized text in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Emphasis extends Text_Wiki_Render {
var $conf = array(
'css' => null
);
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
if ($options['type'] == 'start') {
$css = $this->formatConf(' class="%s"', 'css');
return "";
}
if ($options['type'] == 'end') {
return '';
}
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Font.php
================================================
* @copyright 2005 bertrand Gugger
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Font.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* Font rule render class (used for BBCode)
*
* @category Text
* @package Text_Wiki
* @author Bertrand Gugger
* @copyright 2005 bertrand Gugger
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
* @see Text_Wiki::Text_Wiki_Render()
*/
class Text_Wiki_Render_Xhtml_Font extends Text_Wiki_Render {
/* var $size = array(
'xx-small',
'x-small',
'small',
'medium',
'large',
'x-large',
'xx-large',
'larger',
'smaller'
);
var $units = array(
'em',
'ex',
'px',
'in',
'cm',
'mm',
'pt',
'pc'
);
*/
/**
* Renders a token into text matching the requested format.
* process the font size option
*
* @access public
* @param array $options The "options" portion of the token (second element).
* @return string The text rendered from the token options.
*/
function token($options)
{
if ($options['type'] == 'end') {
return ' ';
}
if ($options['type'] != 'start') {
return '';
}
$ret = '';
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Freelink.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Freelink.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* The wikilink render class.
*/
require_once 'Text/Wiki/Render/Xhtml/Wikilink.php';
/**
* This class renders free links in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Freelink extends Text_Wiki_Render_Xhtml_Wikilink {
// renders identically to wikilinks, only the parsing is different :-)
}
?>
================================================
FILE: Creole/Render/Xhtml/Function.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Function.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders a function description in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Function extends Text_Wiki_Render {
var $conf = array(
// list separator for params and throws
'list_sep' => ', ',
// the "main" format string
'format_main' => '%access %return %name ( %params ) %throws',
// the looped format string for required params
'format_param' => '%type %descr',
// the looped format string for params with default values
'format_paramd' => '[%type %descr default %default]',
// the looped format string for throws
'format_throws' => 'throws %type %descr'
);
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
extract($options); // name, access, return, params, throws
// build the baseline output
$output = $this->conf['format_main'];
$output = str_replace('%access', $this->textEncode($access), $output);
$output = str_replace('%return', $this->textEncode($return), $output);
$output = str_replace('%name', $this->textEncode($name), $output);
// build the set of params
$list = array();
foreach ($params as $key => $val) {
// is there a default value?
if ($val['default']) {
$tmp = $this->conf['format_paramd'];
} else {
$tmp = $this->conf['format_param'];
}
// add the param elements
$tmp = str_replace('%type', $this->textEncode($val['type']), $tmp);
$tmp = str_replace('%descr', $this->textEncode($val['descr']), $tmp);
$tmp = str_replace('%default', $this->textEncode($val['default']), $tmp);
$list[] = $tmp;
}
// insert params into output
$tmp = implode($this->conf['list_sep'], $list);
$output = str_replace('%params', $tmp, $output);
// build the set of throws
$list = array();
foreach ($throws as $key => $val) {
$tmp = $this->conf['format_throws'];
$tmp = str_replace('%type', $this->textEncode($val['type']), $tmp);
$tmp = str_replace('%descr', $this->textEncode($val['descr']), $tmp);
$list[] = $tmp;
}
// insert throws into output
$tmp = implode($this->conf['list_sep'], $list);
$output = str_replace('%throws', $tmp, $output);
// close the div and return the output
$output .= ' ';
return "\n$output\n\n";
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Heading.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Heading.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders headings in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Heading extends Text_Wiki_Render {
var $conf = array(
'css_h1' => null,
'css_h2' => null,
'css_h3' => null,
'css_h4' => null,
'css_h5' => null,
'css_h6' => null
);
function token($options)
{
$collapse = null;
static $jsOutput = false;
// get nice variable names (id, type, level)
extract($options);
switch($type) {
case 'start':
//$css = $this->formatConf(' class="%s"', "css_h$level");
return '';
//return '';
case 'end':
return ' ';
}
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Horiz.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Horiz.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders an horizontal bar in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Horiz extends Text_Wiki_Render {
var $conf = array(
'css' => null
);
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
$css = $this->formatConf(' class="%s"', 'css');
return "
\n";
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Html.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Html.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders preformated html in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Html extends Text_Wiki_Render {
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
return $options['text'];
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Image.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Image.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class inserts an image in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Image extends Text_Wiki_Render {
var $conf = array(
'base' => null,
'url_base' => null,
'css' => null,
'css_link' => null
);
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
// note the image source
$src = $options['src'];
// is the source a local file or URL?
if (strpos($src, '://') === false) {
// the source refers to a local file.
// add the URL base to it.
$src = $this->getConf('base', '/') . $src;
}
// stephane@metacites.net
// is the image clickable?
if (isset($options['attr']['link'])) {
// yes, the image is clickable.
// are we linked to a URL or a wiki page?
if (strpos($options['attr']['link'], '://')) {
// it's a URL, prefix the URL base
$href = $this->getConf('url_base') . $options['attr']['link'];
} else {
// it's a WikiPage; assume it exists.
/** @todo This needs to honor sprintf wikilinks (pmjones) */
/** @todo This needs to honor interwiki (pmjones) */
/** @todo This needs to honor freelinks (pmjones) */
$href = $this->wiki->getRenderConf('xhtml', 'wikilink', 'view_url') .
$options['attr']['link'];
}
} else {
// image is not clickable.
$href = null;
}
// unset so it won't show up as an attribute
unset($options['attr']['link']);
// stephane@metacites.net -- 25/07/2004
// use CSS for all alignment
if (isset($options['attr']['align'])) {
// make sure we have a style attribute
if (!isset($options['attr']['style'])) {
// no style, set up a blank one
$options['attr']['style'] = '';
} else {
// style exists, add a space
$options['attr']['style'] .= ' ';
}
if ($options['attr']['align'] == 'center') {
// add a "center" style to the existing style.
$options['attr']['style'] .=
'display: block; margin-left: auto; margin-right: auto;';
} else {
// add a float style to the existing style
$options['attr']['style'] .=
'float: '.$options['attr']['align'];
}
// unset so it won't show up as an attribute
unset($options['attr']['align']);
}
// stephane@metacites.net -- 25/07/2004
// try to guess width and height
if (! isset($options['attr']['width']) &&
! isset($options['attr']['height'])) {
// does the source refer to a local file or a URL?
if (strpos($src,'://')) {
// is a URL link
$imageFile = $src;
} elseif ($src[0] == '.') {
// reg at dav-muz dot net -- 2005-03-07
// is a local file on relative path.
$imageFile = $src; # ...don't do anything because it's perfect!
} else {
// is a local file on absolute path.
$imageFile = $_SERVER['DOCUMENT_ROOT'] . $src;
}
// attempt to get the image size
$imageSize = @getimagesize($imageFile);
if (is_array($imageSize)) {
$options['attr']['width'] = $imageSize[0];
$options['attr']['height'] = $imageSize[1];
}
}
// start the HTML output
$output = '
formatConf(' class="%s"', 'css');
// add the attributes to the output, and be sure to
// track whether or not we find an "alt" attribute
$alt = false;
foreach ($options['attr'] as $key => $val) {
// track the 'alt' attribute
if (strtolower($key) == 'alt') {
$alt = true;
}
// the 'class' attribute overrides the CSS class conf
if (strtolower($key) == 'class') {
$css = null;
}
$key = $this->textEncode($key);
$val = $this->textEncode($val);
$output .= " $key=\"$val\"";
}
// always add an "alt" attribute per Stephane Solliec
if (! $alt) {
$alt = $this->textEncode(basename($options['src']));
$output .= " alt=\"$alt\"";
}
// end the image tag with the automatic CSS class (if any)
$output .= "$css />";
// was the image clickable?
if ($href) {
// yes, add the href and return
$href = $this->textEncode($href);
$css = $this->formatConf(' class="%s"', 'css_link');
$output = "$output";
}
return $output;
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Include.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Include.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders included maekup in XHTML. (empty)
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Include extends Text_Wiki_Render {
function token()
{
return '';
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Interwiki.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Interwiki.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders inter wikis links in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Interwiki extends Text_Wiki_Render {
var $conf = array(
'sites' => array(
'MeatBall' => 'http://www.usemod.com/cgi-bin/mb.pl?%s',
'Advogato' => 'http://advogato.org/%s',
'Wiki' => 'http://c2.com/cgi/wiki?%s'
),
'target' => '_blank',
'css' => null
);
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
$text = $options['text'];
if (isset($options['url'])) {
// calculated by the parser (e.g. Mediawiki)
$href = $options['url'];
} else {
$site = $options['site'];
// toggg 2006/02/05 page name must be url encoded (e.g. may contain spaces)
$page = $this->urlEncode($options['page']);
if (isset($this->conf['sites'][$site])) {
$href = $this->conf['sites'][$site];
} else {
return $text;
}
// old form where page is at end,
// or new form with %s placeholder for sprintf()?
if (strpos($href, '%s') === false) {
// use the old form
$href = $href . $page;
} else {
// use the new form
$href = sprintf($href, $page);
}
}
// allow for alternative targets
$target = $this->getConf('target');
// build base link
$css = $this->formatConf(' class="%s"', 'css');
$text = $this->textEncode($text);
$output = "textEncode($target);
$output .= " onclick=\"window.open(this.href, '$target');";
$output .= " return false;\"";
}
$output .= ">$text";
return $output;
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Italic.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Italic.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders italic text in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Italic extends Text_Wiki_Render {
var $conf = array(
'css' => null
);
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
if ($options['type'] == 'start') {
$css = $this->formatConf(' class="%s"', 'css');
return "";
}
if ($options['type'] == 'end') {
return '';
}
}
}
?>
================================================
FILE: Creole/Render/Xhtml/List.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: List.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders bullet and ordered lists in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_List extends Text_Wiki_Render {
var $conf = array(
'css_ol' => null,
'css_ol_li' => null,
'css_ul' => null,
'css_ul_li' => null
);
/**
*
* Renders a token into text matching the requested format.
*
* This rendering method is syntactically and semantically compliant
* with XHTML 1.1 in that sub-lists are part of the previous list item.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
// make nice variables (type, level, count)
extract($options);
// set up indenting so that the results look nice; we do this
// in two steps to avoid str_pad mathematics. ;-)
$pad = str_pad('', $level, "\t");
$pad = str_replace("\t", ' ', $pad);
switch ($type) {
case 'bullet_list_start':
// build the base HTML
$css = $this->formatConf(' class="%s"', 'css_ul');
$html = "";
/*
// if this is the opening block for the list,
// put an extra newline in front of it so the
// output looks nice.
if ($level == 0) {
$html = "\n$html";
}
*/
// done!
return $html;
break;
case 'bullet_list_end':
// build the base HTML
$html = "\n$pad
";
// if this is the closing block for the list,
// put extra newlines after it so the output
// looks nice.
if ($level == 0) {
$html .= "\n\n";
}
// done!
return $html;
break;
case 'number_list_start':
if (isset($format)) {
$format = ' type="' . $format . '"';
} else {
$format = '';
}
// build the base HTML
$css = $this->formatConf(' class="%s"', 'css_ol');
$html = "";
/*
// if this is the opening block for the list,
// put an extra newline in front of it so the
// output looks nice.
if ($level == 0) {
$html = "\n$html";
}
*/
// done!
return $html;
break;
case 'number_list_end':
// build the base HTML
$html = "\n$pad
";
// if this is the closing block for the list,
// put extra newlines after it so the output
// looks nice.
if ($level == 0) {
$html .= "\n\n";
}
// done!
return $html;
break;
case 'bullet_item_start':
case 'number_item_start':
// pick the proper CSS class
if ($type == 'bullet_item_start') {
$css = $this->formatConf(' class="%s"', 'css_ul_li');
} else {
$css = $this->formatConf(' class="%s"', 'css_ol_li');
}
// build the base HTML
$html = "\n$pad";
// for the very first item in the list, do nothing.
// but for additional items, be sure to close the
// previous item.
if ($count > 0) {
$html = " $html";
}
// done!
return $html;
break;
case 'bullet_item_end':
case 'number_item_end':
default:
// ignore item endings and all other types.
// item endings are taken care of by the other types
// depending on their place in the list.
return '';
break;
}
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Newline.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Newline.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders new lines in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Newline extends Text_Wiki_Render {
function token($options)
{
return "
\n";
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Page.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Page.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders page markers in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Page extends Text_Wiki_Render {
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
return 'PAGE MARKER HERE*&^%$#^$%*PAGEMARKERHERE';
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Paragraph.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Paragraph.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders paragraphs in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Paragraph extends Text_Wiki_Render {
var $conf = array(
'css' => null
);
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
extract($options); //type
if ($type == 'start') {
$css = $this->formatConf(' class="%s"', 'css');
return "";
}
if ($type == 'end') {
return "
\n\n";
}
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Phplookup.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Phplookup.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders a link to php functions description in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Phplookup extends Text_Wiki_Render {
var $conf = array(
'target' => '_blank',
'css' => null
);
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
$text = trim($options['text']);
$css = $this->formatConf(' class="%s"', 'css');
// start the html
$output = "getConf('target', '');
if ($target && $target != '_self') {
// use a "popup" window. this is XHTML compliant, suggested by
// Aaron Kalin. uses the $target as the new window name.
$target = $this->textEncode($target);
$output .= " onclick=\"window.open(this.href, '$target');";
$output .= " return false;\"";
}
// take off the final parens for functions
if (substr($text, -2) == '()') {
$q = substr($text, 0, -2);
} else {
$q = $text;
}
// toggg 2006/02/05 page name must be url encoded (e.g. may contain spaces)
$q = $this->urlEncode($q);
$text = $this->textEncode($text);
// finish and return
$output .= " href=\"http://php.net/$q\">$text";
return $output;
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Plugin.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Plugin.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders wiki plugins in XHTML. (empty)
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Plugin extends Text_Wiki_Render {
/**
*
* Renders a token into text matching the requested format.
* Plugins produce wiki markup so are processed by parsing, no tokens produced
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
return '';
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Prefilter.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Prefilter.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class implements a Text_Wiki_Render_Xhtml to "pre-filter" source text so
* that line endings are consistently \n, lines ending in a backslash \
* are concatenated with the next line, and tabs are converted to spaces.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Prefilter extends Text_Wiki_Render {
function token()
{
return '';
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Preformatted.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Preformatted.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders preformated text in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Preformatted extends Text_Wiki_Render {
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
$text = $this->textEncode($options['text']);
return ''.$text.'
';
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Raw.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Raw.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders not processed blocks in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Raw extends Text_Wiki_Render {
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
return $this->textEncode($options['text']);
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Revise.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Revise.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders revision marks in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Revise extends Text_Wiki_Render {
var $conf = array(
'css_ins' => null,
'css_del' => null
);
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
if ($options['type'] == 'del_start') {
$css = $this->formatConf(' class="%s"', 'css_del');
return "";
}
if ($options['type'] == 'del_end') {
return "";
}
if ($options['type'] == 'ins_start') {
$css = $this->formatConf(' class="%s"', 'css_ins');
return "";
}
if ($options['type'] == 'ins_end') {
return "";
}
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Smiley.php
================================================
* @copyright 2005 bertrand Gugger
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Smiley.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* Smiley rule Xhtml render class
*
* @category Text
* @package Text_Wiki
* @author Bertrand Gugger
* @copyright 2005 bertrand Gugger
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
* @see Text_Wiki::Text_Wiki_Render()
*/
class Text_Wiki_Render_Xhtml_Smiley extends Text_Wiki_Render {
/**
* Configuration keys for this rule
* 'prefix' => the path to smileys images inclusive file name prefix,
* starts with '/' ==> abolute reference
* if no file names prefix but some folder, terminates with '/'
* 'extension' => the file extension (inclusive '.'), e.g. :
* if prefix 'smileys/icon_' and extension '.gif'
* ':)' whose name is 'smile' will give relative file 'smileys/icon_smile.gif'
* if prefix '/image/smileys/' and extension '.png': absolute '/image/smileys/smile.gif'
* 'css' => optional style applied to smileys
*
* @access public
* @var array 'config-key' => mixed config-value
*/
var $conf = array(
'prefix' => 'images/smiles/icon_',
'extension' => '.gif',
'css' => null
);
/**
* Renders a token into text matching the requested format.
* process the Smileys
*
* @access public
* @param array $options The "options" portion of the token (second element).
* @return string The text rendered from the token options.
*/
function token($options)
{
$imageFile = $this->getConf('prefix') . $options['name'] . $this->getConf('extension');
// attempt to get the image size
$imageSize = @getimagesize($imageFile);
// return the HTML output
return '
formatConf(' class="%s"', 'css') . ' />';
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Specialchar.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Specialchar.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders special characters in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_SpecialChar extends Text_Wiki_Render {
var $types = array('~bs~' => '\',
'~hs~' => ' ',
'~amp~' => '&',
'~ldq~' => '“',
'~rdq~' => '”',
'~lsq~' => '‘',
'~rsq~' => '’',
'~c~' => '©',
'~--~' => '—',
'" -- "' => '—',
'" -- "' => '—',
'~lt~' => '<',
'~gt~' => '>');
function token($options)
{
if (isset($this->types[$options['char']])) {
return $this->types[$options['char']];
} else {
return ''.substr($options['char'], 1, -1).';';
}
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Strong.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Strong.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders text marked as strong in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Strong extends Text_Wiki_Render {
var $conf = array(
'css' => null
);
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
if ($options['type'] == 'start') {
$css = $this->formatConf(' class="%s"', 'css');
return "";
}
if ($options['type'] == 'end') {
return '';
}
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Subscript.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Subscript.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders subscript text in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Subscript extends Text_Wiki_Render {
var $conf = array(
'css' => null
);
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
if ($options['type'] == 'start') {
$css = $this->formatConf(' class="%s"', 'css');
return "";
}
if ($options['type'] == 'end') {
return '';
}
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Superscript.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Superscript.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders superscript text in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Superscript extends Text_Wiki_Render {
var $conf = array(
'css' => null
);
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
if ($options['type'] == 'start') {
$css = $this->formatConf(' class="%s"', 'css');
return "";
}
if ($options['type'] == 'end') {
return '';
}
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Table.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Table.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders tables in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Table extends Text_Wiki_Render {
var $conf = array(
'css_table' => null,
'css_caption' => null,
'css_tr' => null,
'css_th' => null,
'css_td' => null
);
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
// make nice variable names (type, attr, span)
$span = $rowspan = 1;
extract($options);
// free format
$format = isset($format) ? ' '. $format : '';
$pad = ' ';
switch ($type) {
case 'table_start':
$css = $this->formatConf(' class="%s"', 'css_table');
return "\n\n\n";
break;
case 'table_end':
return "
\n\n";
break;
case 'caption_start':
$css = $this->formatConf(' class="%s"', 'css_caption');
return "\n";
break;
case 'caption_end':
return " \n";
break;
case 'row_start':
$css = $this->formatConf(' class="%s"', 'css_tr');
return "$pad\n";
break;
case 'row_end':
return "$pad \n";
break;
case 'cell_start':
// base html
$html = $pad . $pad;
// is this a TH or TD cell?
if ($attr == 'header') {
// start a header cell
$css = $this->formatConf(' class="%s"', 'css_th');
$html .= "formatConf(' class="%s"', 'css_td');
$html .= " 1) {
$html .= " colspan=\"$span\"";
}
// add the row span
if ($rowspan > 1) {
$html .= " rowspan=\"$rowspan\"";
}
// add alignment
if ($attr != 'header' && $attr != '') {
$html .= " style=\"text-align: $attr;\"";
}
// done!
$html .= "$format>";
return $html;
break;
case 'cell_end':
if ($attr == 'header') {
return "\n";
} else {
return " \n";
}
break;
default:
return '';
}
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Tighten.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Tighten.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class makes the tightening in XHTML. (empty)
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Tighten extends Text_Wiki_Render {
function token()
{
return '';
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Titlebar.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Titlebar.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders a title bar in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Titlebar extends Text_Wiki_Render {
var $conf = array(
'css' => 'titlebar'
);
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
if ($options['type'] == 'start') {
$css = $this->formatConf(' class="%s"', 'css');
return "";
}
if ($options['type'] == 'end') {
return '';
}
}
}
?>
================================================
FILE: Creole/Render/Xhtml/Toc.php
================================================
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Toc.php 182 2008-09-14 15:56:00Z i.feelinglucky $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class inserts a table of content in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Toc extends Text_Wiki_Render {
var $conf = array(
'css_list' => null,
'css_item' => null,
'title' => 'Table of Contents',
'div_id' => 'toc',
'collapse' => true
);
var $min = 2;
/**
*
* Renders a token into text matching the requested format.
*
* @access public
*
* @param array $options The "options" portion of the token (second
* element).
*
* @return string The text rendered from the token options.
*
*/
function token($options)
{
// type, id, level, count, attr
extract($options);
switch ($type) {
case 'list_start':
$css = $this->getConf('css_list');
$html = '';
// collapse div within a table?
if ($this->getConf('collapse')) {
$html .= '';
$html .= "\n";
}
// add the div, class, and id
$html .= 'getConf('div_id');
if ($div_id) {
$html .= " id=\"$div_id\"";
}
// add the title, and done
$html .= '>';
$html .= $this->getConf('title');
return $html;
break;
case 'list_end':
if ($this->getConf('collapse')) {
return "\n\n
\n\n";
} else {
return "\n
* $Spyc = new Spyc;
* $array = $Spyc->load($file);
*
* or:
*
* $array = Spyc::YAMLLoad($file);
*
* or:
*
* $array = spyc_load_file($file);
*
* @package Spyc
*/
class Spyc {
// SETTINGS
const REMPTY = "\0\0\0\0\0";
/**
* Setting this to true will force YAMLDump to enclose any string value in
* quotes. False by default.
*
* @var bool
*/
public $setting_dump_force_quotes = false;
/**
* Setting this to true will forse YAMLLoad to use syck_load function when
* possible. False by default.
* @var bool
*/
public $setting_use_syck_is_possible = false;
/**#@+
* @access private
* @var mixed
*/
private $_dumpIndent;
private $_dumpWordWrap;
private $_containsGroupAnchor = false;
private $_containsGroupAlias = false;
private $path;
private $result;
private $LiteralPlaceHolder = '___YAML_Literal_Block___';
private $SavedGroups = array();
private $indent;
/**
* Path modifier that should be applied after adding current element.
* @var array
*/
private $delayedPath = array();
/**#@+
* @access public
* @var mixed
*/
public $_nodeId;
/**
* Load a valid YAML string to Spyc.
* @param string $input
* @return array
*/
public function load ($input) {
return $this->__loadString($input);
}
/**
* Load a valid YAML file to Spyc.
* @param string $file
* @return array
*/
public function loadFile ($file) {
return $this->__load($file);
}
/**
* Load YAML into a PHP array statically
*
* The load method, when supplied with a YAML stream (string or file),
* will do its best to convert YAML in a file into a PHP array. Pretty
* simple.
* Usage:
*
* $array = Spyc::YAMLLoad('lucky.yaml');
* print_r($array);
*
* @access public
* @return array
* @param string $input Path of YAML file or string containing YAML
*/
public static function YAMLLoad($input) {
$Spyc = new Spyc;
return $Spyc->__load($input);
}
/**
* Load a string of YAML into a PHP array statically
*
* The load method, when supplied with a YAML string, will do its best
* to convert YAML in a string into a PHP array. Pretty simple.
*
* Note: use this function if you don't want files from the file system
* loaded and processed as YAML. This is of interest to people concerned
* about security whose input is from a string.
*
* Usage:
*
* $array = Spyc::YAMLLoadString("---\n0: hello world\n");
* print_r($array);
*
* @access public
* @return array
* @param string $input String containing YAML
*/
public static function YAMLLoadString($input) {
$Spyc = new Spyc;
return $Spyc->__loadString($input);
}
/**
* Dump YAML from PHP array statically
*
* The dump method, when supplied with an array, will do its best
* to convert the array into friendly YAML. Pretty simple. Feel free to
* save the returned string as nothing.yaml and pass it around.
*
* Oh, and you can decide how big the indent is and what the wordwrap
* for folding is. Pretty cool -- just pass in 'false' for either if
* you want to use the default.
*
* Indent's default is 2 spaces, wordwrap's default is 40 characters. And
* you can turn off wordwrap by passing in 0.
*
* @access public
* @return string
* @param array $array PHP array
* @param int $indent Pass in false to use the default, which is 2
* @param int $wordwrap Pass in 0 for no wordwrap, false for default (40)
* @param int $no_opening_dashes Do not start YAML file with "---\n"
*/
public static function YAMLDump($array, $indent = false, $wordwrap = false, $no_opening_dashes = false) {
$spyc = new Spyc;
return $spyc->dump($array, $indent, $wordwrap, $no_opening_dashes);
}
/**
* Dump PHP array to YAML
*
* The dump method, when supplied with an array, will do its best
* to convert the array into friendly YAML. Pretty simple. Feel free to
* save the returned string as tasteful.yaml and pass it around.
*
* Oh, and you can decide how big the indent is and what the wordwrap
* for folding is. Pretty cool -- just pass in 'false' for either if
* you want to use the default.
*
* Indent's default is 2 spaces, wordwrap's default is 40 characters. And
* you can turn off wordwrap by passing in 0.
*
* @access public
* @return string
* @param array $array PHP array
* @param int $indent Pass in false to use the default, which is 2
* @param int $wordwrap Pass in 0 for no wordwrap, false for default (40)
*/
public function dump($array,$indent = false,$wordwrap = false, $no_opening_dashes = false) {
// Dumps to some very clean YAML. We'll have to add some more features
// and options soon. And better support for folding.
// New features and options.
if ($indent === false or !is_numeric($indent)) {
$this->_dumpIndent = 2;
} else {
$this->_dumpIndent = $indent;
}
if ($wordwrap === false or !is_numeric($wordwrap)) {
$this->_dumpWordWrap = 40;
} else {
$this->_dumpWordWrap = $wordwrap;
}
// New YAML document
$string = "";
if (!$no_opening_dashes) $string = "---\n";
// Start at the base of the array and move through it.
if ($array) {
$array = (array)$array;
$previous_key = -1;
foreach ($array as $key => $value) {
if (!isset($first_key)) $first_key = $key;
$string .= $this->_yamlize($key,$value,0,$previous_key, $first_key, $array);
$previous_key = $key;
}
}
return $string;
}
/**
* Attempts to convert a key / value array item to YAML
* @access private
* @return string
* @param $key The name of the key
* @param $value The value of the item
* @param $indent The indent of the current node
*/
private function _yamlize($key,$value,$indent, $previous_key = -1, $first_key = 0, $source_array = null) {
if (is_array($value)) {
if (empty ($value))
return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key, $source_array);
// It has children. What to do?
// Make it the right kind of item
$string = $this->_dumpNode($key, self::REMPTY, $indent, $previous_key, $first_key, $source_array);
// Add the indent
$indent += $this->_dumpIndent;
// Yamlize the array
$string .= $this->_yamlizeArray($value,$indent);
} elseif (!is_array($value)) {
// It doesn't have children. Yip.
$string = $this->_dumpNode($key, $value, $indent, $previous_key, $first_key, $source_array);
}
return $string;
}
/**
* Attempts to convert an array to YAML
* @access private
* @return string
* @param $array The array you want to convert
* @param $indent The indent of the current level
*/
private function _yamlizeArray($array,$indent) {
if (is_array($array)) {
$string = '';
$previous_key = -1;
foreach ($array as $key => $value) {
if (!isset($first_key)) $first_key = $key;
$string .= $this->_yamlize($key, $value, $indent, $previous_key, $first_key, $array);
$previous_key = $key;
}
return $string;
} else {
return false;
}
}
/**
* Returns YAML from a key and a value
* @access private
* @return string
* @param $key The name of the key
* @param $value The value of the item
* @param $indent The indent of the current node
*/
private function _dumpNode($key, $value, $indent, $previous_key = -1, $first_key = 0, $source_array = null) {
// do some folding here, for blocks
if (is_string ($value) && ((strpos($value,"\n") !== false || strpos($value,": ") !== false || strpos($value,"- ") !== false ||
strpos($value,"*") !== false || strpos($value,"#") !== false || strpos($value,"<") !== false || strpos($value,">") !== false || strpos ($value, ' ') !== false ||
strpos($value,"[") !== false || strpos($value,"]") !== false || strpos($value,"{") !== false || strpos($value,"}") !== false) || strpos($value,"&") !== false || strpos($value, "'") !== false || strpos($value, "!") === 0 ||
substr ($value, -1, 1) == ':')
) {
$value = $this->_doLiteralBlock($value,$indent);
} else {
$value = $this->_doFolding($value,$indent);
}
if ($value === array()) $value = '[ ]';
if ($value === "") $value = '""';
if (self::isTranslationWord($value)) {
$value = $this->_doLiteralBlock($value, $indent);
}
if (trim ($value) != $value)
$value = $this->_doLiteralBlock($value,$indent);
if (is_bool($value)) {
$value = $value ? "true" : "false";
}
if ($value === null) $value = 'null';
if ($value === "'" . self::REMPTY . "'") $value = null;
$spaces = str_repeat(' ',$indent);
//if (is_int($key) && $key - 1 == $previous_key && $first_key===0) {
if (is_array ($source_array) && array_keys($source_array) === range(0, count($source_array) - 1)) {
// It's a sequence
$string = $spaces.'- '.$value."\n";
} else {
// if ($first_key===0) throw new Exception('Keys are all screwy. The first one was zero, now it\'s "'. $key .'"');
// It's mapped
if (strpos($key, ":") !== false || strpos($key, "#") !== false) { $key = '"' . $key . '"'; }
$string = rtrim ($spaces.$key.': '.$value)."\n";
}
return $string;
}
/**
* Creates a literal block for dumping
* @access private
* @return string
* @param $value
* @param $indent int The value of the indent
*/
private function _doLiteralBlock($value,$indent) {
if ($value === "\n") return '\n';
if (strpos($value, "\n") === false && strpos($value, "'") === false) {
return sprintf ("'%s'", $value);
}
if (strpos($value, "\n") === false && strpos($value, '"') === false) {
return sprintf ('"%s"', $value);
}
$exploded = explode("\n",$value);
$newValue = '|';
$indent += $this->_dumpIndent;
$spaces = str_repeat(' ',$indent);
foreach ($exploded as $line) {
$newValue .= "\n" . $spaces . ($line);
}
return $newValue;
}
/**
* Folds a string of text, if necessary
* @access private
* @return string
* @param $value The string you wish to fold
*/
private function _doFolding($value,$indent) {
// Don't do anything if wordwrap is set to 0
if ($this->_dumpWordWrap !== 0 && is_string ($value) && strlen($value) > $this->_dumpWordWrap) {
$indent += $this->_dumpIndent;
$indent = str_repeat(' ',$indent);
$wrapped = wordwrap($value,$this->_dumpWordWrap,"\n$indent");
$value = ">\n".$indent.$wrapped;
} else {
if ($this->setting_dump_force_quotes && is_string ($value) && $value !== self::REMPTY)
$value = '"' . $value . '"';
if (is_numeric($value) && is_string($value))
$value = '"' . $value . '"';
}
return $value;
}
private function isTrueWord($value) {
$words = self::getTranslations(array('true', 'on', 'yes', 'y'));
return in_array($value, $words, true);
}
private function isFalseWord($value) {
$words = self::getTranslations(array('false', 'off', 'no', 'n'));
return in_array($value, $words, true);
}
private function isNullWord($value) {
$words = self::getTranslations(array('null', '~'));
return in_array($value, $words, true);
}
private function isTranslationWord($value) {
return (
self::isTrueWord($value) ||
self::isFalseWord($value) ||
self::isNullWord($value)
);
}
/**
* Coerce a string into a native type
* Reference: http://yaml.org/type/bool.html
* TODO: Use only words from the YAML spec.
* @access private
* @param $value The value to coerce
*/
private function coerceValue(&$value) {
if (self::isTrueWord($value)) {
$value = true;
} else if (self::isFalseWord($value)) {
$value = false;
} else if (self::isNullWord($value)) {
$value = null;
}
}
/**
* Given a set of words, perform the appropriate translations on them to
* match the YAML 1.1 specification for type coercing.
* @param $words The words to translate
* @access private
*/
private static function getTranslations(array $words) {
$result = array();
foreach ($words as $i) {
$result = array_merge($result, array(ucfirst($i), strtoupper($i), strtolower($i)));
}
return $result;
}
// LOADING FUNCTIONS
private function __load($input) {
$Source = $this->loadFromSource($input);
return $this->loadWithSource($Source);
}
private function __loadString($input) {
$Source = $this->loadFromString($input);
return $this->loadWithSource($Source);
}
private function loadWithSource($Source) {
if (empty ($Source)) return array();
if ($this->setting_use_syck_is_possible && function_exists ('syck_load')) {
$array = syck_load (implode ("\n", $Source));
return is_array($array) ? $array : array();
}
$this->path = array();
$this->result = array();
$cnt = count($Source);
for ($i = 0; $i < $cnt; $i++) {
$line = $Source[$i];
$this->indent = strlen($line) - strlen(ltrim($line));
$tempPath = $this->getParentPathByIndent($this->indent);
$line = self::stripIndent($line, $this->indent);
if (self::isComment($line)) continue;
if (self::isEmpty($line)) continue;
$this->path = $tempPath;
$literalBlockStyle = self::startsLiteralBlock($line);
if ($literalBlockStyle) {
$line = rtrim ($line, $literalBlockStyle . " \n");
$literalBlock = '';
$line .= ' '.$this->LiteralPlaceHolder;
$literal_block_indent = strlen($Source[$i+1]) - strlen(ltrim($Source[$i+1]));
while (++$i < $cnt && $this->literalBlockContinues($Source[$i], $this->indent)) {
$literalBlock = $this->addLiteralLine($literalBlock, $Source[$i], $literalBlockStyle, $literal_block_indent);
}
$i--;
}
// Strip out comments
if (strpos ($line, '#')) {
$line = preg_replace('/\s*#([^"\']+)$/','',$line);
}
while (++$i < $cnt && self::greedilyNeedNextLine($line)) {
$line = rtrim ($line, " \n\t\r") . ' ' . ltrim ($Source[$i], " \t");
}
$i--;
$lineArray = $this->_parseLine($line);
if ($literalBlockStyle)
$lineArray = $this->revertLiteralPlaceHolder ($lineArray, $literalBlock);
$this->addArray($lineArray, $this->indent);
foreach ($this->delayedPath as $indent => $delayedPath)
$this->path[$indent] = $delayedPath;
$this->delayedPath = array();
}
return $this->result;
}
private function loadFromSource ($input) {
if (!empty($input) && strpos($input, "\n") === false && file_exists($input))
$input = file_get_contents($input);
return $this->loadFromString($input);
}
private function loadFromString ($input) {
$lines = explode("\n",$input);
foreach ($lines as $k => $_) {
$lines[$k] = rtrim ($_, "\r");
}
return $lines;
}
/**
* Parses YAML code and returns an array for a node
* @access private
* @return array
* @param string $line A line from the YAML file
*/
private function _parseLine($line) {
if (!$line) return array();
$line = trim($line);
if (!$line) return array();
$array = array();
$group = $this->nodeContainsGroup($line);
if ($group) {
$this->addGroup($line, $group);
$line = $this->stripGroup ($line, $group);
}
if ($this->startsMappedSequence($line))
return $this->returnMappedSequence($line);
if ($this->startsMappedValue($line))
return $this->returnMappedValue($line);
if ($this->isArrayElement($line))
return $this->returnArrayElement($line);
if ($this->isPlainArray($line))
return $this->returnPlainArray($line);
return $this->returnKeyValuePair($line);
}
/**
* Finds the type of the passed value, returns the value as the new type.
* @access private
* @param string $value
* @return mixed
*/
private function _toType($value) {
if ($value === '') return "";
$first_character = $value[0];
$last_character = substr($value, -1, 1);
$is_quoted = false;
do {
if (!$value) break;
if ($first_character != '"' && $first_character != "'") break;
if ($last_character != '"' && $last_character != "'") break;
$is_quoted = true;
} while (0);
if ($is_quoted) {
$value = str_replace('\n', "\n", $value);
return strtr(substr ($value, 1, -1), array ('\\"' => '"', '\'\'' => '\'', '\\\'' => '\''));
}
if (strpos($value, ' #') !== false && !$is_quoted)
$value = preg_replace('/\s+#(.+)$/','',$value);
if ($first_character == '[' && $last_character == ']') {
// Take out strings sequences and mappings
$innerValue = trim(substr ($value, 1, -1));
if ($innerValue === '') return array();
$explode = $this->_inlineEscape($innerValue);
// Propagate value array
$value = array();
foreach ($explode as $v) {
$value[] = $this->_toType($v);
}
return $value;
}
if (strpos($value,': ')!==false && $first_character != '{') {
$array = explode(': ',$value);
$key = trim($array[0]);
array_shift($array);
$value = trim(implode(': ',$array));
$value = $this->_toType($value);
return array($key => $value);
}
if ($first_character == '{' && $last_character == '}') {
$innerValue = trim(substr ($value, 1, -1));
if ($innerValue === '') return array();
// Inline Mapping
// Take out strings sequences and mappings
$explode = $this->_inlineEscape($innerValue);
// Propagate value array
$array = array();
foreach ($explode as $v) {
$SubArr = $this->_toType($v);
if (empty($SubArr)) continue;
if (is_array ($SubArr)) {
$array[key($SubArr)] = $SubArr[key($SubArr)]; continue;
}
$array[] = $SubArr;
}
return $array;
}
if ($value == 'null' || $value == 'NULL' || $value == 'Null' || $value == '' || $value == '~') {
return null;
}
if ( is_numeric($value) && preg_match ('/^(-|)[1-9]+[0-9]*$/', $value) ){
$intvalue = (int)$value;
if ($intvalue != PHP_INT_MAX)
$value = $intvalue;
return $value;
}
$this->coerceValue($value);
if (is_numeric($value)) {
if ($value === '0') return 0;
if (rtrim ($value, 0) === $value)
$value = (float)$value;
return $value;
}
return $value;
}
/**
* Used in inlines to check for more inlines or quoted strings
* @access private
* @return array
*/
private function _inlineEscape($inline) {
// There's gotta be a cleaner way to do this...
// While pure sequences seem to be nesting just fine,
// pure mappings and mappings with sequences inside can't go very
// deep. This needs to be fixed.
$seqs = array();
$maps = array();
$saved_strings = array();
$saved_empties = array();
// Check for empty strings
$regex = '/("")|(\'\')/';
if (preg_match_all($regex,$inline,$strings)) {
$saved_empties = $strings[0];
$inline = preg_replace($regex,'YAMLEmpty',$inline);
}
unset($regex);
// Check for strings
$regex = '/(?:(")|(?:\'))((?(1)[^"]+|[^\']+))(?(1)"|\')/';
if (preg_match_all($regex,$inline,$strings)) {
$saved_strings = $strings[0];
$inline = preg_replace($regex,'YAMLString',$inline);
}
unset($regex);
// echo $inline;
$i = 0;
do {
// Check for sequences
while (preg_match('/\[([^{}\[\]]+)\]/U',$inline,$matchseqs)) {
$seqs[] = $matchseqs[0];
$inline = preg_replace('/\[([^{}\[\]]+)\]/U', ('YAMLSeq' . (count($seqs) - 1) . 's'), $inline, 1);
}
// Check for mappings
while (preg_match('/{([^\[\]{}]+)}/U',$inline,$matchmaps)) {
$maps[] = $matchmaps[0];
$inline = preg_replace('/{([^\[\]{}]+)}/U', ('YAMLMap' . (count($maps) - 1) . 's'), $inline, 1);
}
if ($i++ >= 10) break;
} while (strpos ($inline, '[') !== false || strpos ($inline, '{') !== false);
$explode = explode(',',$inline);
$explode = array_map('trim', $explode);
$stringi = 0; $i = 0;
while (1) {
// Re-add the sequences
if (!empty($seqs)) {
foreach ($explode as $key => $value) {
if (strpos($value,'YAMLSeq') !== false) {
foreach ($seqs as $seqk => $seq) {
$explode[$key] = str_replace(('YAMLSeq'.$seqk.'s'),$seq,$value);
$value = $explode[$key];
}
}
}
}
// Re-add the mappings
if (!empty($maps)) {
foreach ($explode as $key => $value) {
if (strpos($value,'YAMLMap') !== false) {
foreach ($maps as $mapk => $map) {
$explode[$key] = str_replace(('YAMLMap'.$mapk.'s'), $map, $value);
$value = $explode[$key];
}
}
}
}
// Re-add the strings
if (!empty($saved_strings)) {
foreach ($explode as $key => $value) {
while (strpos($value,'YAMLString') !== false) {
$explode[$key] = preg_replace('/YAMLString/',$saved_strings[$stringi],$value, 1);
unset($saved_strings[$stringi]);
++$stringi;
$value = $explode[$key];
}
}
}
// Re-add the empties
if (!empty($saved_empties)) {
foreach ($explode as $key => $value) {
while (strpos($value,'YAMLEmpty') !== false) {
$explode[$key] = preg_replace('/YAMLEmpty/', '', $value, 1);
$value = $explode[$key];
}
}
}
$finished = true;
foreach ($explode as $key => $value) {
if (strpos($value,'YAMLSeq') !== false) {
$finished = false; break;
}
if (strpos($value,'YAMLMap') !== false) {
$finished = false; break;
}
if (strpos($value,'YAMLString') !== false) {
$finished = false; break;
}
if (strpos($value,'YAMLEmpty') !== false) {
$finished = false; break;
}
}
if ($finished) break;
$i++;
if ($i > 10)
break; // Prevent infinite loops.
}
return $explode;
}
private function literalBlockContinues ($line, $lineIndent) {
if (!trim($line)) return true;
if (strlen($line) - strlen(ltrim($line)) > $lineIndent) return true;
return false;
}
private function referenceContentsByAlias ($alias) {
do {
if (!isset($this->SavedGroups[$alias])) { echo "Bad group name: $alias."; break; }
$groupPath = $this->SavedGroups[$alias];
$value = $this->result;
foreach ($groupPath as $k) {
$value = $value[$k];
}
} while (false);
return $value;
}
private function addArrayInline ($array, $indent) {
$CommonGroupPath = $this->path;
if (empty ($array)) return false;
foreach ($array as $k => $_) {
$this->addArray(array($k => $_), $indent);
$this->path = $CommonGroupPath;
}
return true;
}
private function addArray ($incoming_data, $incoming_indent) {
// print_r ($incoming_data);
if (count ($incoming_data) > 1)
return $this->addArrayInline ($incoming_data, $incoming_indent);
$key = key ($incoming_data);
$value = isset($incoming_data[$key]) ? $incoming_data[$key] : null;
if ($key === '__!YAMLZero') $key = '0';
if ($incoming_indent == 0 && !$this->_containsGroupAlias && !$this->_containsGroupAnchor) { // Shortcut for root-level values.
if ($key || $key === '' || $key === '0') {
$this->result[$key] = $value;
} else {
$this->result[] = $value; end ($this->result); $key = key ($this->result);
}
$this->path[$incoming_indent] = $key;
return;
}
$history = array();
// Unfolding inner array tree.
$history[] = $_arr = $this->result;
foreach ($this->path as $k) {
$history[] = $_arr = $_arr[$k];
}
if ($this->_containsGroupAlias) {
$value = $this->referenceContentsByAlias($this->_containsGroupAlias);
$this->_containsGroupAlias = false;
}
// Adding string or numeric key to the innermost level or $this->arr.
if (is_string($key) && $key == '<<') {
if (!is_array ($_arr)) { $_arr = array (); }
$_arr = array_merge ($_arr, $value);
} else if ($key || $key === '' || $key === '0') {
if (!is_array ($_arr))
$_arr = array ($key=>$value);
else
$_arr[$key] = $value;
} else {
if (!is_array ($_arr)) { $_arr = array ($value); $key = 0; }
else { $_arr[] = $value; end ($_arr); $key = key ($_arr); }
}
$reverse_path = array_reverse($this->path);
$reverse_history = array_reverse ($history);
$reverse_history[0] = $_arr;
$cnt = count($reverse_history) - 1;
for ($i = 0; $i < $cnt; $i++) {
$reverse_history[$i+1][$reverse_path[$i]] = $reverse_history[$i];
}
$this->result = $reverse_history[$cnt];
$this->path[$incoming_indent] = $key;
if ($this->_containsGroupAnchor) {
$this->SavedGroups[$this->_containsGroupAnchor] = $this->path;
if (is_array ($value)) {
$k = key ($value);
if (!is_int ($k)) {
$this->SavedGroups[$this->_containsGroupAnchor][$incoming_indent + 2] = $k;
}
}
$this->_containsGroupAnchor = false;
}
}
private static function startsLiteralBlock ($line) {
$lastChar = substr (trim($line), -1);
if ($lastChar != '>' && $lastChar != '|') return false;
if ($lastChar == '|') return $lastChar;
// HTML tags should not be counted as literal blocks.
if (preg_match ('#<.*?>$#', $line)) return false;
return $lastChar;
}
private static function greedilyNeedNextLine($line) {
$line = trim ($line);
if (!strlen($line)) return false;
if (substr ($line, -1, 1) == ']') return false;
if ($line[0] == '[') return true;
if (preg_match ('#^[^:]+?:\s*\[#', $line)) return true;
return false;
}
private function addLiteralLine ($literalBlock, $line, $literalBlockStyle, $indent = -1) {
$line = self::stripIndent($line, $indent);
if ($literalBlockStyle !== '|') {
$line = self::stripIndent($line);
}
$line = rtrim ($line, "\r\n\t ") . "\n";
if ($literalBlockStyle == '|') {
return $literalBlock . $line;
}
if (strlen($line) == 0)
return rtrim($literalBlock, ' ') . "\n";
if ($line == "\n" && $literalBlockStyle == '>') {
return rtrim ($literalBlock, " \t") . "\n";
}
if ($line != "\n")
$line = trim ($line, "\r\n ") . " ";
return $literalBlock . $line;
}
function revertLiteralPlaceHolder ($lineArray, $literalBlock) {
foreach ($lineArray as $k => $_) {
if (is_array($_))
$lineArray[$k] = $this->revertLiteralPlaceHolder ($_, $literalBlock);
else if (substr($_, -1 * strlen ($this->LiteralPlaceHolder)) == $this->LiteralPlaceHolder)
$lineArray[$k] = rtrim ($literalBlock, " \r\n");
}
return $lineArray;
}
private static function stripIndent ($line, $indent = -1) {
if ($indent == -1) $indent = strlen($line) - strlen(ltrim($line));
return substr ($line, $indent);
}
private function getParentPathByIndent ($indent) {
if ($indent == 0) return array();
$linePath = $this->path;
do {
end($linePath); $lastIndentInParentPath = key($linePath);
if ($indent <= $lastIndentInParentPath) array_pop ($linePath);
} while ($indent <= $lastIndentInParentPath);
return $linePath;
}
private function clearBiggerPathValues ($indent) {
if ($indent == 0) $this->path = array();
if (empty ($this->path)) return true;
foreach ($this->path as $k => $_) {
if ($k > $indent) unset ($this->path[$k]);
}
return true;
}
private static function isComment ($line) {
if (!$line) return false;
if ($line[0] == '#') return true;
if (trim($line, " \r\n\t") == '---') return true;
return false;
}
private static function isEmpty ($line) {
return (trim ($line) === '');
}
private function isArrayElement ($line) {
if (!$line || !is_scalar($line)) return false;
if (substr($line, 0, 2) != '- ') return false;
if (strlen ($line) > 3)
if (substr($line,0,3) == '---') return false;
return true;
}
private function isHashElement ($line) {
return strpos($line, ':');
}
private function isLiteral ($line) {
if ($this->isArrayElement($line)) return false;
if ($this->isHashElement($line)) return false;
return true;
}
private static function unquote ($value) {
if (!$value) return $value;
if (!is_string($value)) return $value;
if ($value[0] == '\'') return trim ($value, '\'');
if ($value[0] == '"') return trim ($value, '"');
return $value;
}
private function startsMappedSequence ($line) {
return (substr($line, 0, 2) == '- ' && substr ($line, -1, 1) == ':');
}
private function returnMappedSequence ($line) {
$array = array();
$key = self::unquote(trim(substr($line,1,-1)));
$array[$key] = array();
$this->delayedPath = array(strpos ($line, $key) + $this->indent => $key);
return array($array);
}
private function checkKeysInValue($value) {
if (strchr('[{"\'', $value[0]) === false) {
if (strchr($value, ': ') !== false) {
throw new Exception('Too many keys: '.$value);
}
}
}
private function returnMappedValue ($line) {
$this->checkKeysInValue($line);
$array = array();
$key = self::unquote (trim(substr($line,0,-1)));
$array[$key] = '';
return $array;
}
private function startsMappedValue ($line) {
return (substr ($line, -1, 1) == ':');
}
private function isPlainArray ($line) {
return ($line[0] == '[' && substr ($line, -1, 1) == ']');
}
private function returnPlainArray ($line) {
return $this->_toType($line);
}
private function returnKeyValuePair ($line) {
$array = array();
$key = '';
if (strpos ($line, ': ')) {
// It's a key/value pair most likely
// If the key is in double quotes pull it out
if (($line[0] == '"' || $line[0] == "'") && preg_match('/^(["\'](.*)["\'](\s)*:)/',$line,$matches)) {
$value = trim(str_replace($matches[1],'',$line));
$key = $matches[2];
} else {
// Do some guesswork as to the key and the value
$explode = explode(': ', $line);
$key = trim(array_shift($explode));
$value = trim(implode(': ', $explode));
$this->checkKeysInValue($value);
}
// Set the type of the value. Int, string, etc
$value = $this->_toType($value);
if ($key === '0') $key = '__!YAMLZero';
$array[$key] = $value;
} else {
$array = array ($line);
}
return $array;
}
private function returnArrayElement ($line) {
if (strlen($line) <= 1) return array(array()); // Weird %)
$array = array();
$value = trim(substr($line,1));
$value = $this->_toType($value);
if ($this->isArrayElement($value)) {
$value = $this->returnArrayElement($value);
}
$array[] = $value;
return $array;
}
private function nodeContainsGroup ($line) {
$symbolsForReference = 'A-z0-9_\-';
if (strpos($line, '&') === false && strpos($line, '*') === false) return false; // Please die fast ;-)
if ($line[0] == '&' && preg_match('/^(&['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1];
if ($line[0] == '*' && preg_match('/^(\*['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1];
if (preg_match('/(&['.$symbolsForReference.']+)$/', $line, $matches)) return $matches[1];
if (preg_match('/(\*['.$symbolsForReference.']+$)/', $line, $matches)) return $matches[1];
if (preg_match ('#^\s*<<\s*:\s*(\*[^\s]+).*$#', $line, $matches)) return $matches[1];
return false;
}
private function addGroup ($line, $group) {
if ($group[0] == '&') $this->_containsGroupAnchor = substr ($group, 1);
if ($group[0] == '*') $this->_containsGroupAlias = substr ($group, 1);
//print_r ($this->path);
}
private function stripGroup ($line, $group) {
$line = trim(str_replace($group, '', $line));
return $line;
}
}
// Enable use of Spyc from command line
// The syntax is the following: php Spyc.php spyc.yaml
do {
if (PHP_SAPI != 'cli') break;
if (empty ($_SERVER['argc']) || $_SERVER['argc'] < 2) break;
if (empty ($_SERVER['PHP_SELF']) || FALSE === strpos ($_SERVER['PHP_SELF'], 'Spyc.php') ) break;
$file = $argv[1];
echo json_encode (spyc_load_file ($file));
} while (0);
================================================
FILE: GoogleAnalytics/Plugin.php
================================================
footer = array('GoogleAnalytics_Plugin', 'render');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
/** 分类名称 */
$account = new Typecho_Widget_Helper_Form_Element_Text('account', NULL, 'UA-XXXXXXX-X', _t('Google Analytics 帐号'), _t('此帐号可在 GA 管理平台查询;格式为 UA-XXXXXXX-X 。'));
$form->addInput($account);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 插件实现方法
*
* @access public
* @return void
*/
public static function render()
{
$account = Typecho_Widget::widget('Widget_Options')->plugin('GoogleAnalytics')->account;
echo "";
}
}
================================================
FILE: GoogleCodePrettify/Plugin.php
================================================
contentEx = array('GoogleCodePrettify_Plugin', 'parse');
Typecho_Plugin::factory('Widget_Abstract_Contents')->excerptEx = array('GoogleCodePrettify_Plugin', 'parse');
Typecho_Plugin::factory('Widget_Abstract_Comments')->contentEx = array('GoogleCodePrettify_Plugin', 'parse');
Typecho_Plugin::factory('Widget_Archive')->header = array('GoogleCodePrettify_Plugin', 'header');
Typecho_Plugin::factory('Widget_Archive')->footer = array('GoogleCodePrettify_Plugin', 'footer');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form){}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 输出头部css
*
* @access public
* @param unknown $header
* @return unknown
*/
public static function header() {
$cssUrl = Helper::options()->pluginUrl . '/GoogleCodePrettify/src/prettify.css';
echo '';
}
/**
* 输出尾部js
*
* @access public
* @param unknown $header
* @return unknown
*/
public static function footer() {
$jsUrl = Helper::options()->pluginUrl . '/GoogleCodePrettify/src/prettify.js';
echo '';
echo '';
}
/**
* 解析
*
* @access public
* @param array $matches 解析值
* @return string
*/
public static function parseCallback($matches)
{
$language = trim($matches[2]);
$map = array(
'js' => 'javascript',
'as' => 'actionscript',
'as3' => 'actionscript3'
);
if (!empty($language) && isset($map[$language])) {
$language = $map[$language];
}
$source = '
| ';
return $source . $numberItem . $sourceItem . '
* This file could be used by goodle code to allow syntax highlight for * Virtual AGC SVN repository or if you don't want to commonize * the header for the agc/aea html assembly listing. * * @author ohommes@alumni.cmu.edu */ PR.registerLangHandler( PR.createSimpleLexer( [ // A line comment that starts with ; [PR.PR_COMMENT, /^#[^\r\n]*/, null, '#'], // Whitespace [PR.PR_PLAIN, /^[\t\n\r \xA0]+/, null, '\t\n\r \xA0'], // A double quoted, possibly multi-line, string. [PR.PR_STRING, /^\"(?:[^\"\\]|\\[\s\S])*(?:\"|$)/, null, '"'] ], [ [PR.PR_KEYWORD, /^(?:ADS|AD|AUG|BZF|BZMF|CAE|CAF|CA|CCS|COM|CS|DAS|DCA|DCOM|DCS|DDOUBL|DIM|DOUBLE|DTCB|DTCF|DV|DXCH|EDRUPT|EXTEND|INCR|INDEX|NDX|INHINT|LXCH|MASK|MSK|MP|MSU|NOOP|OVSK|QXCH|RAND|READ|RELINT|RESUME|RETURN|ROR|RXOR|SQUARE|SU|TCR|TCAA|OVSK|TCF|TC|TS|WAND|WOR|WRITE|XCH|XLQ|XXALQ|ZL|ZQ|ADD|ADZ|SUB|SUZ|MPY|MPR|MPZ|DVP|COM|ABS|CLA|CLZ|LDQ|STO|STQ|ALS|LLS|LRS|TRA|TSQ|TMI|TOV|AXT|TIX|DLY|INP|OUT)\s/,null], [PR.PR_TYPE, /^(?:-?GENADR|=MINUS|2BCADR|VN|BOF|MM|-?2CADR|-?[1-6]DNADR|ADRES|BBCON|[SE]?BANK\=?|BLOCK|BNKSUM|E?CADR|COUNT\*?|2?DEC\*?|-?DNCHAN|-?DNPTR|EQUALS|ERASE|MEMORY|2?OCT|REMADR|SETLOC|SUBRO|ORG|BSS|BES|SYN|EQU|DEFINE|END)\s/,null], // A single quote possibly followed by a word that optionally ends with // = ! or ?. [PR.PR_LITERAL, /^\'(?:-*(?:\w|\\[\x21-\x7e])(?:[\w-]*|\\[\x21-\x7e])[=!?]?)?/], // Any word including labels that optionally ends with = ! or ?. [PR.PR_PLAIN, /^-*(?:[!-z_]|\\[\x21-\x7e])(?:[\w-]*|\\[\x21-\x7e])[=!?]?/i], // A printable non-space non-special character [PR.PR_PUNCTUATION, /^[^\w\t\n\r \xA0()\"\\\';]+/] ]), ['apollo', 'agc', 'aea']); ================================================ FILE: GoogleCodePrettify/src/lang-css.js ================================================ // Copyright (C) 2009 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * @fileoverview * Registers a language handler for CSS. * * * To use, include prettify.js and this file in your HTML page. * Then put your code in an HTML tag like *
* * * http://www.w3.org/TR/CSS21/grammar.html Section G2 defines the lexical * grammar. This scheme does not recognize keywords containing escapes. * * @author mikesamuel@gmail.com */ PR.registerLangHandler( PR.createSimpleLexer( [ // The space production(my lisp code)* The lang-cl class identifies the language as common lisp. * This file supports the following language extensions: * lang-cl - Common Lisp * lang-el - Emacs Lisp * lang-lisp - Lisp * lang-scm - Scheme * * * I used http://www.informatik.uni-freiburg.de/~thiemann/haskell/haskell98-report-html/syntax-iso.html * as the basis, but ignore the way the ncomment production nests since this * makes the lexical grammar irregular. It might be possible to support * ncomments using the lookbehind filter. * * * @author mikesamuel@gmail.com */ PR.registerLangHandler( PR.createSimpleLexer( [ // Whitespace // whitechar -> newline | vertab | space | tab | uniWhite // newline -> return linefeed | return | linefeed | formfeed [PR.PR_PLAIN, /^[\t\n\x0B\x0C\r ]+/, null, '\t\n\x0B\x0C\r '], // Single line double and single-quoted strings. // char -> ' (graphic<' | \> | space | escape<\&>) ' // string -> " {graphic<" | \> | space | escape | gap}" // escape -> \ ( charesc | ascii | decimal | o octal // | x hexadecimal ) // charesc -> a | b | f | n | r | t | v | \ | " | ' | & [PR.PR_STRING, /^\"(?:[^\"\\\n\x0C\r]|\\[\s\S])*(?:\"|$)/, null, '"'], [PR.PR_STRING, /^\'(?:[^\'\\\n\x0C\r]|\\[^&])\'?/, null, "'"], // decimal -> digit{digit} // octal -> octit{octit} // hexadecimal -> hexit{hexit} // integer -> decimal // | 0o octal | 0O octal // | 0x hexadecimal | 0X hexadecimal // float -> decimal . decimal [exponent] // | decimal exponent // exponent -> (e | E) [+ | -] decimal [PR.PR_LITERAL, /^(?:0o[0-7]+|0x[\da-f]+|\d+(?:\.\d+)?(?:e[+\-]?\d+)?)/i, null, '0123456789'] ], [ // Haskell does not have a regular lexical grammar due to the nested // ncomment. // comment -> dashes [ any
(my lisp code)* The lang-cl class identifies the language as common lisp. * This file supports the following language extensions: * lang-cl - Common Lisp * lang-el - Emacs Lisp * lang-lisp - Lisp * lang-scm - Scheme * * * I used http://www.devincook.com/goldparser/doc/meta-language/grammar-LISP.htm * as the basis, but added line comments that start with ; and changed the atom * production to disallow unquoted semicolons. * * "Name" = 'LISP' * "Author" = 'John McCarthy' * "Version" = 'Minimal' * "About" = 'LISP is an abstract language that organizes ALL' * | 'data around "lists".' * * "Start Symbol" = [s-Expression] * * {Atom Char} = {Printable} - {Whitespace} - [()"\''] * * Atom = ( {Atom Char} | '\'{Printable} )+ * * [s-Expression] ::= [Quote] Atom * | [Quote] '(' [Series] ')' * | [Quote] '(' [s-Expression] '.' [s-Expression] ')' * * [Series] ::= [s-Expression] [Series] * | * * [Quote] ::= '' !Quote = do not evaluate * | * * * I used Practical Common Lisp as * the basis for the reserved word list. * * * @author mikesamuel@gmail.com */ PR.registerLangHandler( PR.createSimpleLexer( [ ['opn', /^\(/, null, '('], ['clo', /^\)/, null, ')'], // A line comment that starts with ; [PR.PR_COMMENT, /^;[^\r\n]*/, null, ';'], // Whitespace [PR.PR_PLAIN, /^[\t\n\r \xA0]+/, null, '\t\n\r \xA0'], // A double quoted, possibly multi-line, string. [PR.PR_STRING, /^\"(?:[^\"\\]|\\[\s\S])*(?:\"|$)/, null, '"'] ], [ [PR.PR_KEYWORD, /^(?:block|c[ad]+r|catch|cons|defun|do|eq|eql|equal|equalp|eval-when|flet|format|go|if|labels|lambda|let|load-time-value|locally|macrolet|multiple-value-call|nil|progn|progv|quote|require|return-from|setq|symbol-macrolet|t|tagbody|the|throw|unwind)\b/, null], [PR.PR_LITERAL, /^[+\-]?(?:0x[0-9a-f]+|\d+\/\d+|(?:\.\d+|\d+(?:\.\d*)?)(?:[ed][+\-]?\d+)?)/i], // A single quote possibly followed by a word that optionally ends with // = ! or ?. [PR.PR_LITERAL, /^\'(?:-*(?:\w|\\[\x21-\x7e])(?:[\w-]*|\\[\x21-\x7e])[=!?]?)?/], // A word that optionally ends with = ! or ?. [PR.PR_PLAIN, /^-*(?:[a-z_]|\\[\x21-\x7e])(?:[\w-]*|\\[\x21-\x7e])[=!?]?/i], // A printable non-space non-special character [PR.PR_PUNCTUATION, /^[^\w\t\n\r \xA0()\"\\\';]+/] ]), ['cl', 'el', 'lisp', 'scm']); ================================================ FILE: GoogleCodePrettify/src/lang-lua.js ================================================ // Copyright (C) 2008 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * @fileoverview * Registers a language handler for Lua. * * * To use, include prettify.js and this file in your HTML page. * Then put your code in an HTML tag like *
(my Lua code)* * * I used http://www.lua.org/manual/5.1/manual.html#2.1 * Because of the long-bracket concept used in strings and comments, Lua does * not have a regular lexical grammar, but luckily it fits within the space * of irregular grammars supported by javascript regular expressions. * * @author mikesamuel@gmail.com */ PR.registerLangHandler( PR.createSimpleLexer( [ // Whitespace [PR.PR_PLAIN, /^[\t\n\r \xA0]+/, null, '\t\n\r \xA0'], // A double or single quoted, possibly multi-line, string. [PR.PR_STRING, /^(?:\"(?:[^\"\\]|\\[\s\S])*(?:\"|$)|\'(?:[^\'\\]|\\[\s\S])*(?:\'|$))/, null, '"\''] ], [ // A comment is either a line comment that starts with two dashes, or // two dashes preceding a long bracketed block. [PR.PR_COMMENT, /^--(?:\[(=*)\[[\s\S]*?(?:\]\1\]|$)|[^\r\n]*)/], // A long bracketed block not preceded by -- is a string. [PR.PR_STRING, /^\[(=*)\[[\s\S]*?(?:\]\1\]|$)/], [PR.PR_KEYWORD, /^(?:and|break|do|else|elseif|end|false|for|function|if|in|local|nil|not|or|repeat|return|then|true|until|while)\b/, null], // A number is a hex integer literal, a decimal real literal, or in // scientific notation. [PR.PR_LITERAL, /^[+-]?(?:0x[\da-f]+|(?:(?:\.\d+|\d+(?:\.\d*)?)(?:e[+\-]?\d+)?))/i], // An identifier [PR.PR_PLAIN, /^[a-z_]\w*/i], // A run of punctuation [PR.PR_PUNCTUATION, /^[^\w\t\n\r \xA0][^\w\t\n\r \xA0\"\'\-\+=]*/] ]), ['lua']); ================================================ FILE: GoogleCodePrettify/src/lang-ml.js ================================================ // Copyright (C) 2008 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * @fileoverview * Registers a language handler for OCaml, SML, F# and similar languages. * * Based on the lexical grammar at * http://research.microsoft.com/fsharp/manual/spec2.aspx#_Toc202383715 * * @author mikesamuel@gmail.com */ PR.registerLangHandler( PR.createSimpleLexer( [ // Whitespace is made up of spaces, tabs and newline characters. [PR.PR_PLAIN, /^[\t\n\r \xA0]+/, null, '\t\n\r \xA0'], // #if ident/#else/#endif directives delimit conditional compilation // sections [PR.PR_COMMENT, /^#(?:if[\t\n\r \xA0]+(?:[a-z_$][\w\']*|``[^\r\n\t`]*(?:``|$))|else|endif|light)/i, null, '#'], // A double or single quoted, possibly multi-line, string. // F# allows escaped newlines in strings. [PR.PR_STRING, /^(?:\"(?:[^\"\\]|\\[\s\S])*(?:\"|$)|\'(?:[^\'\\]|\\[\s\S])*(?:\'|$))/, null, '"\''] ], [ // Block comments are delimited by (* and *) and may be // nested. Single-line comments begin with // and extend to // the end of a line. // TODO: (*...*) comments can be nested. This does not handle that. [PR.PR_COMMENT, /^(?:\/\/[^\r\n]*|\(\*[\s\S]*?\*\))/], [PR.PR_KEYWORD, /^(?:abstract|and|as|assert|begin|class|default|delegate|do|done|downcast|downto|elif|else|end|exception|extern|false|finally|for|fun|function|if|in|inherit|inline|interface|internal|lazy|let|match|member|module|mutable|namespace|new|null|of|open|or|override|private|public|rec|return|static|struct|then|to|true|try|type|upcast|use|val|void|when|while|with|yield|asr|land|lor|lsl|lsr|lxor|mod|sig|atomic|break|checked|component|const|constraint|constructor|continue|eager|event|external|fixed|functor|global|include|method|mixin|object|parallel|process|protected|pure|sealed|trait|virtual|volatile)\b/], // A number is a hex integer literal, a decimal real literal, or in // scientific notation. [PR.PR_LITERAL, /^[+\-]?(?:0x[\da-f]+|(?:(?:\.\d+|\d+(?:\.\d*)?)(?:e[+\-]?\d+)?))/i], [PR.PR_PLAIN, /^(?:[a-z_]\w*[!?#]?|``[^\r\n\t`]*(?:``|$))/i], // A printable non-space non-special character [PR.PR_PUNCTUATION, /^[^\t\n\r \xA0\"\'\w]+/] ]), ['fs', 'ml']); ================================================ FILE: GoogleCodePrettify/src/lang-proto.js ================================================ // Copyright (C) 2006 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * @fileoverview * Registers a language handler for Protocol Buffers as described at * http://code.google.com/p/protobuf/. * * Based on the lexical grammar at * http://research.microsoft.com/fsharp/manual/spec2.aspx#_Toc202383715 * * @author mikesamuel@gmail.com */ PR.registerLangHandler(PR.sourceDecorator({ keywords: ( 'bool bytes default double enum extend extensions false fixed32 ' + 'fixed64 float group import int32 int64 max message option ' + 'optional package repeated required returns rpc service ' + 'sfixed32 sfixed64 sint32 sint64 string syntax to true uint32 ' + 'uint64'), cStyleComments: true }), ['proto']); ================================================ FILE: GoogleCodePrettify/src/lang-sql.js ================================================ // Copyright (C) 2008 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * @fileoverview * Registers a language handler for SQL. * * * To use, include prettify.js and this file in your HTML page. * Then put your code in an HTML tag like *
(my SQL code)* * * http://savage.net.au/SQL/sql-99.bnf.html is the basis for the grammar, and * http://msdn.microsoft.com/en-us/library/aa238507(SQL.80).aspx as the basis * for the keyword list. * * @author mikesamuel@gmail.com */ PR.registerLangHandler( PR.createSimpleLexer( [ // Whitespace [PR.PR_PLAIN, /^[\t\n\r \xA0]+/, null, '\t\n\r \xA0'], // A double or single quoted, possibly multi-line, string. [PR.PR_STRING, /^(?:"(?:[^\"\\]|\\.)*"|'(?:[^\'\\]|\\.)*')/, null, '"\''] ], [ // A comment is either a line comment that starts with two dashes, or // two dashes preceding a long bracketed block. [PR.PR_COMMENT, /^(?:--[^\r\n]*|\/\*[\s\S]*?(?:\*\/|$))/], [PR.PR_KEYWORD, /^(?:ADD|ALL|ALTER|AND|ANY|AS|ASC|AUTHORIZATION|BACKUP|BEGIN|BETWEEN|BREAK|BROWSE|BULK|BY|CASCADE|CASE|CHECK|CHECKPOINT|CLOSE|CLUSTERED|COALESCE|COLLATE|COLUMN|COMMIT|COMPUTE|CONSTRAINT|CONTAINS|CONTAINSTABLE|CONTINUE|CONVERT|CREATE|CROSS|CURRENT|CURRENT_DATE|CURRENT_TIME|CURRENT_TIMESTAMP|CURRENT_USER|CURSOR|DATABASE|DBCC|DEALLOCATE|DECLARE|DEFAULT|DELETE|DENY|DESC|DISK|DISTINCT|DISTRIBUTED|DOUBLE|DROP|DUMMY|DUMP|ELSE|END|ERRLVL|ESCAPE|EXCEPT|EXEC|EXECUTE|EXISTS|EXIT|FETCH|FILE|FILLFACTOR|FOR|FOREIGN|FREETEXT|FREETEXTTABLE|FROM|FULL|FUNCTION|GOTO|GRANT|GROUP|HAVING|HOLDLOCK|IDENTITY|IDENTITYCOL|IDENTITY_INSERT|IF|IN|INDEX|INNER|INSERT|INTERSECT|INTO|IS|JOIN|KEY|KILL|LEFT|LIKE|LINENO|LOAD|NATIONAL|NOCHECK|NONCLUSTERED|NOT|NULL|NULLIF|OF|OFF|OFFSETS|ON|OPEN|OPENDATASOURCE|OPENQUERY|OPENROWSET|OPENXML|OPTION|OR|ORDER|OUTER|OVER|PERCENT|PLAN|PRECISION|PRIMARY|PRINT|PROC|PROCEDURE|PUBLIC|RAISERROR|READ|READTEXT|RECONFIGURE|REFERENCES|REPLICATION|RESTORE|RESTRICT|RETURN|REVOKE|RIGHT|ROLLBACK|ROWCOUNT|ROWGUIDCOL|RULE|SAVE|SCHEMA|SELECT|SESSION_USER|SET|SETUSER|SHUTDOWN|SOME|STATISTICS|SYSTEM_USER|TABLE|TEXTSIZE|THEN|TO|TOP|TRAN|TRANSACTION|TRIGGER|TRUNCATE|TSEQUAL|UNION|UNIQUE|UPDATE|UPDATETEXT|USE|USER|VALUES|VARYING|VIEW|WAITFOR|WHEN|WHERE|WHILE|WITH|WRITETEXT)(?=[^\w-]|$)/i, null], // A number is a hex integer literal, a decimal real literal, or in // scientific notation. [PR.PR_LITERAL, /^[+-]?(?:0x[\da-f]+|(?:(?:\.\d+|\d+(?:\.\d*)?)(?:e[+\-]?\d+)?))/i], // An identifier [PR.PR_PLAIN, /^[a-z_][\w-]*/i], // A run of punctuation [PR.PR_PUNCTUATION, /^[^\w\t\n\r \xA0\"\'][^\w\t\n\r \xA0+\-\"\']*/] ]), ['sql']); ================================================ FILE: GoogleCodePrettify/src/lang-vb.js ================================================ // Copyright (C) 2009 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * @fileoverview * Registers a language handler for various flavors of basic. * * * To use, include prettify.js and this file in your HTML page. * Then put your code in an HTML tag like * * * * http://msdn.microsoft.com/en-us/library/aa711638(VS.71).aspx defines the * visual basic grammar lexical grammar. * * @author mikesamuel@gmail.com */ PR.registerLangHandler( PR.createSimpleLexer( [ // Whitespace [PR.PR_PLAIN, /^[\t\n\r \xA0\u2028\u2029]+/, null, '\t\n\r \xA0\u2028\u2029'], // A double quoted string with quotes escaped by doubling them. // A single character can be suffixed with C. [PR.PR_STRING, /^(?:[\"\u201C\u201D](?:[^\"\u201C\u201D]|[\"\u201C\u201D]{2})(?:[\"\u201C\u201D]c|$)|[\"\u201C\u201D](?:[^\"\u201C\u201D]|[\"\u201C\u201D]{2})*(?:[\"\u201C\u201D]|$))/i, null, '"\u201C\u201D'], // A comment starts with a single quote and runs until the end of the // line. [PR.PR_COMMENT, /^[\'\u2018\u2019][^\r\n\u2028\u2029]*/, null, '\'\u2018\u2019'] ], [ [PR.PR_KEYWORD, /^(?:AddHandler|AddressOf|Alias|And|AndAlso|Ansi|As|Assembly|Auto|Boolean|ByRef|Byte|ByVal|Call|Case|Catch|CBool|CByte|CChar|CDate|CDbl|CDec|Char|CInt|Class|CLng|CObj|Const|CShort|CSng|CStr|CType|Date|Decimal|Declare|Default|Delegate|Dim|DirectCast|Do|Double|Each|Else|ElseIf|End|EndIf|Enum|Erase|Error|Event|Exit|Finally|For|Friend|Function|Get|GetType|GoSub|GoTo|Handles|If|Implements|Imports|In|Inherits|Integer|Interface|Is|Let|Lib|Like|Long|Loop|Me|Mod|Module|MustInherit|MustOverride|MyBase|MyClass|Namespace|New|Next|Not|NotInheritable|NotOverridable|Object|On|Option|Optional|Or|OrElse|Overloads|Overridable|Overrides|ParamArray|Preserve|Private|Property|Protected|Public|RaiseEvent|ReadOnly|ReDim|RemoveHandler|Resume|Return|Select|Set|Shadows|Shared|Short|Single|Static|Step|Stop|String|Structure|Sub|SyncLock|Then|Throw|To|Try|TypeOf|Unicode|Until|Variant|Wend|When|While|With|WithEvents|WriteOnly|Xor|EndIf|GoSub|Let|Variant|Wend)\b/i, null], // A second comment form [PR.PR_COMMENT, /^REM[^\r\n\u2028\u2029]*/i], // A boolean, numeric, or date literal. [PR.PR_LITERAL, /^(?:True\b|False\b|Nothing\b|\d+(?:E[+\-]?\d+[FRD]?|[FRDSIL])?|(?:&H[0-9A-F]+|&O[0-7]+)[SIL]?|\d*\.\d+(?:E[+\-]?\d+)?[FRD]?|#\s+(?:\d+[\-\/]\d+[\-\/]\d+(?:\s+\d+:\d+(?::\d+)?(\s*(?:AM|PM))?)?|\d+:\d+(?::\d+)?(\s*(?:AM|PM))?)\s+#)/i], // An identifier? [PR.PR_PLAIN, /^(?:(?:[a-z]|_\w)\w*|\[(?:[a-z]|_\w)\w*\])/i], // A run of punctuation [PR.PR_PUNCTUATION, /^[^\w\t\n\r \"\'\[\]\xA0\u2018\u2019\u201C\u201D\u2028\u2029]+/], // Square brackets [PR.PR_PUNCTUATION, /^(?:\[|\])/] ]), ['vb', 'vbs']); ================================================ FILE: GoogleCodePrettify/src/lang-wiki.js ================================================ // Copyright (C) 2009 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * @fileoverview * Registers a language handler for Wiki pages. * * Based on WikiSyntax at http://code.google.com/p/support/wiki/WikiSyntax * * @author mikesamuel@gmail.com */ PR.registerLangHandler( PR.createSimpleLexer( [ // Whitespace [PR.PR_PLAIN, /^[\t \xA0a-gi-z0-9]+/, null, '\t \xA0abcdefgijklmnopqrstuvwxyz0123456789'], // Wiki formatting [PR.PR_PUNCTUATION, /^[=*~\^\[\]]+/, null, '=*~^[]'] ], [ // Meta-info like #summary, #labels, etc. ['lang-wiki.meta', /(?:^^|\r\n?|\n)(#[a-z]+)\b/], // A WikiWord [PR.PR_LITERAL, /^(?:[A-Z][a-z][a-z0-9]+[A-Z][a-z][a-zA-Z0-9]+)\b/ ], // A preformatted block in an unknown language ['lang-', /^\{\{\{([\s\S]+?)\}\}\}/], // A block of source code in an unknown language ['lang-', /^`([^\r\n`]+)`/], // An inline URL. [PR.PR_STRING, /^https?:\/\/[^\/?#\s]*(?:\/[^?#\s]*)?(?:\?[^#\s]*)?(?:#\S*)?/i], [PR.PR_PLAIN, /^(?:\r\n|[\s\S])[^#=*~^A-Zh\{`\[\r\n]*/] ]), ['wiki']); PR.registerLangHandler( PR.createSimpleLexer([[PR.PR_KEYWORD, /^#[a-z]+/i, null, '#']], []), ['wiki.meta']); ================================================ FILE: GoogleCodePrettify/src/prettify.css ================================================ /* Pretty printing styles. Used with prettify.js. */ .str { color: #B1D631; font-style: italic; } .kwd { color: #527AA2; } .com { color: #666; font-style: italic; } .typ { color: #FAF4C6; } .lit { color: #527AA2; } .pun { color: #FF8613; } .pln { color: #FAF4C6; } .tag { color: #527AA2; } .atn { color: #FAF4C6; } .atv { color: #B1D631; } .dec { color: #FAF4C6; } table.prettyprint-table { padding: 2px; border: 1px solid #000; background: #222; color: #eee; font-size: 13px; margin: 0; font: 12px/1.5 'andale mono','lucida console',monospace; } table.prettyprint-table pre, table.prettyprint-table table {margin: 0; background: #222; border: none; font-size: 13px;} pre.prettyprint tr:hover td {background: #333} table.prettyprint-table td.number {color: #666; font-family: "Courier New",Courier,monospace } .prettyprint-box {width: 100%; display: block; overflow-x: auto} table.prettyprint-table td {padding: 2px 4px} @media print { .str { color: #060; } .kwd { color: #006; font-weight: bold; } .com { color: #600; font-style: italic; } .typ { color: #404; font-weight: bold; } .lit { color: #044; } .pun { color: #440; } .pln { color: #000; } .tag { color: #006; font-weight: bold; } .atn { color: #404; } .atv { color: #060; } } ================================================ FILE: GoogleCodePrettify/src/prettify.js ================================================ // Copyright (C) 2006 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * @fileoverview * some functions for browser-side pretty printing of code contained in html. *
* * For a fairly comprehensive set of languages see the * README * file that came with this source. At a minimum, the lexer should work on a * number of languages including C and friends, Java, Python, Bash, SQL, HTML, * XML, CSS, Javascript, and Makefiles. It works passably on Ruby, PHP and Awk * and a subset of Perl, but, because of commenting conventions, doesn't work on * Smalltalk, Lisp-like, or CAML-like languages without an explicit lang class. *
* Usage:
} and {@code } tags in your source with
* {@code class=prettyprint.}
* You can also use the (html deprecated) {@code } tag, but the pretty
* printer needs to do more substantial DOM manipulations to support that, so
* some css styles may not be preserved.
* } or {@code } element to specify the
* language, as in {@code }. Any class that
* starts with "lang-" followed by a file extension, specifies the file type.
* See the "lang-*.js" files in this directory for code that implements
* per-language file handlers.
*
* Change log:
* cbeust, 2006/08/22
*
* Java annotations (start with "@") are now captured as literals ("lit")
*
* @requires console
* @overrides window
*/
// JSLint declarations
/*global console, document, navigator, setTimeout, window */
/**
* Split {@code prettyPrint} into multiple timeouts so as not to interfere with
* UI events.
* If set to {@code false}, {@code prettyPrint()} is synchronous.
*/
window['PR_SHOULD_USE_CONTINUATION'] = true;
/** the number of characters between tab columns */
window['PR_TAB_WIDTH'] = 8;
/** Walks the DOM returning a properly escaped version of innerHTML.
* @param {Node} node
* @param {Array.} out output buffer that receives chunks of HTML.
*/
window['PR_normalizedHtml']
/** Contains functions for creating and registering new language handlers.
* @type {Object}
*/
= window['PR']
/** Pretty print a chunk of code.
*
* @param {string} sourceCodeHtml code as html
* @return {string} code as html, but prettier
*/
= window['prettyPrintOne']
/** Find all the {@code } and {@code } tags in the DOM with
* {@code class=prettyprint} and prettify them.
* @param {Function?} opt_whenDone if specified, called when the last entry
* has been finished.
*/
= window['prettyPrint'] = void 0;
/** browser detection. @extern @returns false if not IE, otherwise the major version. */
window['_pr_isIE6'] = function () {
var ieVersion = navigator && navigator.userAgent &&
navigator.userAgent.match(/\bMSIE ([678])\./);
ieVersion = ieVersion ? +ieVersion[1] : false;
window['_pr_isIE6'] = function () { return ieVersion; };
return ieVersion;
};
(function () {
// Keyword lists for various languages.
var FLOW_CONTROL_KEYWORDS =
"break continue do else for if return while ";
var C_KEYWORDS = FLOW_CONTROL_KEYWORDS + "auto case char const default " +
"double enum extern float goto int long register short signed sizeof " +
"static struct switch typedef union unsigned void volatile ";
var COMMON_KEYWORDS = C_KEYWORDS + "catch class delete false import " +
"new operator private protected public this throw true try typeof ";
var CPP_KEYWORDS = COMMON_KEYWORDS + "alignof align_union asm axiom bool " +
"concept concept_map const_cast constexpr decltype " +
"dynamic_cast explicit export friend inline late_check " +
"mutable namespace nullptr reinterpret_cast static_assert static_cast " +
"template typeid typename using virtual wchar_t where ";
var JAVA_KEYWORDS = COMMON_KEYWORDS +
"abstract boolean byte extends final finally implements import " +
"instanceof null native package strictfp super synchronized throws " +
"transient ";
var CSHARP_KEYWORDS = JAVA_KEYWORDS +
"as base by checked decimal delegate descending event " +
"fixed foreach from group implicit in interface internal into is lock " +
"object out override orderby params partial readonly ref sbyte sealed " +
"stackalloc string select uint ulong unchecked unsafe ushort var ";
var JSCRIPT_KEYWORDS = COMMON_KEYWORDS +
"debugger eval export function get null set undefined var with " +
"Infinity NaN ";
var PERL_KEYWORDS = "caller delete die do dump elsif eval exit foreach for " +
"goto if import last local my next no our print package redo require " +
"sub undef unless until use wantarray while BEGIN END ";
var PYTHON_KEYWORDS = FLOW_CONTROL_KEYWORDS + "and as assert class def del " +
"elif except exec finally from global import in is lambda " +
"nonlocal not or pass print raise try with yield " +
"False True None ";
var RUBY_KEYWORDS = FLOW_CONTROL_KEYWORDS + "alias and begin case class def" +
" defined elsif end ensure false in module next nil not or redo rescue " +
"retry self super then true undef unless until when yield BEGIN END ";
var SH_KEYWORDS = FLOW_CONTROL_KEYWORDS + "case done elif esac eval fi " +
"function in local set then until ";
var ALL_KEYWORDS = (
CPP_KEYWORDS + CSHARP_KEYWORDS + JSCRIPT_KEYWORDS + PERL_KEYWORDS +
PYTHON_KEYWORDS + RUBY_KEYWORDS + SH_KEYWORDS);
// token style names. correspond to css classes
/** token style for a string literal */
var PR_STRING = 'str';
/** token style for a keyword */
var PR_KEYWORD = 'kwd';
/** token style for a comment */
var PR_COMMENT = 'com';
/** token style for a type */
var PR_TYPE = 'typ';
/** token style for a literal value. e.g. 1, null, true. */
var PR_LITERAL = 'lit';
/** token style for a punctuation string. */
var PR_PUNCTUATION = 'pun';
/** token style for a punctuation string. */
var PR_PLAIN = 'pln';
/** token style for an sgml tag. */
var PR_TAG = 'tag';
/** token style for a markup declaration such as a DOCTYPE. */
var PR_DECLARATION = 'dec';
/** token style for embedded source. */
var PR_SOURCE = 'src';
/** token style for an sgml attribute name. */
var PR_ATTRIB_NAME = 'atn';
/** token style for an sgml attribute value. */
var PR_ATTRIB_VALUE = 'atv';
/**
* A class that indicates a section of markup that is not code, e.g. to allow
* embedding of line numbers within code listings.
*/
var PR_NOCODE = 'nocode';
/** A set of tokens that can precede a regular expression literal in
* javascript.
* http://www.mozilla.org/js/language/js20/rationale/syntax.html has the full
* list, but I've removed ones that might be problematic when seen in
* languages that don't support regular expression literals.
*
* Specifically, I've removed any keywords that can't precede a regexp
* literal in a syntactically legal javascript program, and I've removed the
* "in" keyword since it's not a keyword in many languages, and might be used
* as a count of inches.
*
*
The link a above does not accurately describe EcmaScript rules since
* it fails to distinguish between (a=++/b/i) and (a++/b/i) but it works
* very well in practice.
*
* @private
*/
var REGEXP_PRECEDER_PATTERN = function () {
var preceders = [
"!", "!=", "!==", "#", "%", "%=", "&", "&&", "&&=",
"&=", "(", "*", "*=", /* "+", */ "+=", ",", /* "-", */ "-=",
"->", /*".", "..", "...", handled below */ "/", "/=", ":", "::", ";",
"<", "<<", "<<=", "<=", "=", "==", "===", ">",
">=", ">>", ">>=", ">>>", ">>>=", "?", "@", "[",
"^", "^=", "^^", "^^=", "{", "|", "|=", "||",
"||=", "~" /* handles =~ and !~ */,
"break", "case", "continue", "delete",
"do", "else", "finally", "instanceof",
"return", "throw", "try", "typeof"
];
var pattern = '(?:^^|[+-]';
for (var i = 0; i < preceders.length; ++i) {
pattern += '|' + preceders[i].replace(/([^=<>:&a-z])/g, '\\$1');
}
pattern += ')\\s*'; // matches at end, and matches empty string
return pattern;
// CAVEAT: this does not properly handle the case where a regular
// expression immediately follows another since a regular expression may
// have flags for case-sensitivity and the like. Having regexp tokens
// adjacent is not valid in any language I'm aware of, so I'm punting.
// TODO: maybe style special characters inside a regexp as punctuation.
}();
// Define regexps here so that the interpreter doesn't have to create an
// object each time the function containing them is called.
// The language spec requires a new object created even if you don't access
// the $1 members.
var pr_amp = /&/g;
var pr_lt = //g;
var pr_quot = /\"/g;
/** like textToHtml but escapes double quotes to be attribute safe. */
function attribToHtml(str) {
return str.replace(pr_amp, '&')
.replace(pr_lt, '<')
.replace(pr_gt, '>')
.replace(pr_quot, '"');
}
/** escapest html special characters to html. */
function textToHtml(str) {
return str.replace(pr_amp, '&')
.replace(pr_lt, '<')
.replace(pr_gt, '>');
}
var pr_ltEnt = /</g;
var pr_gtEnt = />/g;
var pr_aposEnt = /'/g;
var pr_quotEnt = /"/g;
var pr_ampEnt = /&/g;
var pr_nbspEnt = / /g;
/** unescapes html to plain text. */
function htmlToText(html) {
var pos = html.indexOf('&');
if (pos < 0) { return html; }
// Handle numeric entities specially. We can't use functional substitution
// since that doesn't work in older versions of Safari.
// These should be rare since most browsers convert them to normal chars.
for (--pos; (pos = html.indexOf('', pos + 1)) >= 0;) {
var end = html.indexOf(';', pos);
if (end >= 0) {
var num = html.substring(pos + 3, end);
var radix = 10;
if (num && num.charAt(0) === 'x') {
num = num.substring(1);
radix = 16;
}
var codePoint = parseInt(num, radix);
if (!isNaN(codePoint)) {
html = (html.substring(0, pos) + String.fromCharCode(codePoint) +
html.substring(end + 1));
}
}
}
return html.replace(pr_ltEnt, '<')
.replace(pr_gtEnt, '>')
.replace(pr_aposEnt, "'")
.replace(pr_quotEnt, '"')
.replace(pr_nbspEnt, ' ')
.replace(pr_ampEnt, '&');
}
/** is the given node's innerHTML normally unescaped? */
function isRawContent(node) {
return 'XMP' === node.tagName;
}
var newlineRe = /[\r\n]/g;
/**
* Are newlines and adjacent spaces significant in the given node's innerHTML?
*/
function isPreformatted(node, content) {
// PRE means preformatted, and is a very common case, so don't create
// unnecessary computed style objects.
if ('PRE' === node.tagName) { return true; }
if (!newlineRe.test(content)) { return true; } // Don't care
var whitespace = '';
// For disconnected nodes, IE has no currentStyle.
if (node.currentStyle) {
whitespace = node.currentStyle.whiteSpace;
} else if (window.getComputedStyle) {
// Firefox makes a best guess if node is disconnected whereas Safari
// returns the empty string.
whitespace = window.getComputedStyle(node, null).whiteSpace;
}
return !whitespace || whitespace === 'pre';
}
function normalizedHtml(node, out) {
switch (node.nodeType) {
case 1: // an element
var name = node.tagName.toLowerCase();
out.push('<', name);
for (var i = 0; i < node.attributes.length; ++i) {
var attr = node.attributes[i];
if (!attr.specified) { continue; }
out.push(' ');
normalizedHtml(attr, out);
}
out.push('>');
for (var child = node.firstChild; child; child = child.nextSibling) {
normalizedHtml(child, out);
}
if (node.firstChild || !/^(?:br|link|img)$/.test(name)) {
out.push('<\/', name, '>');
}
break;
case 2: // an attribute
out.push(node.name.toLowerCase(), '="', attribToHtml(node.value), '"');
break;
case 3: case 4: // text
out.push(textToHtml(node.nodeValue));
break;
}
}
/**
* Given a group of {@link RegExp}s, returns a {@code RegExp} that globally
* matches the union o the sets o strings matched d by the input RegExp.
* Since it matches globally, if the input strings have a start-of-input
* anchor (/^.../), it is ignored for the purposes of unioning.
* @param {Array.} regexs non multiline, non-global regexs.
* @return {RegExp} a global regex.
*/
function combinePrefixPatterns(regexs) {
var capturedGroupIndex = 0;
var needToFoldCase = false;
var ignoreCase = false;
for (var i = 0, n = regexs.length; i < n; ++i) {
var regex = regexs[i];
if (regex.ignoreCase) {
ignoreCase = true;
} else if (/[a-z]/i.test(regex.source.replace(
/\\u[0-9a-f]{4}|\\x[0-9a-f]{2}|\\[^ux]/gi, ''))) {
needToFoldCase = true;
ignoreCase = false;
break;
}
}
function decodeEscape(charsetPart) {
if (charsetPart.charAt(0) !== '\\') { return charsetPart.charCodeAt(0); }
switch (charsetPart.charAt(1)) {
case 'b': return 8;
case 't': return 9;
case 'n': return 0xa;
case 'v': return 0xb;
case 'f': return 0xc;
case 'r': return 0xd;
case 'u': case 'x':
return parseInt(charsetPart.substring(2), 16)
|| charsetPart.charCodeAt(1);
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7':
return parseInt(charsetPart.substring(1), 8);
default: return charsetPart.charCodeAt(1);
}
}
function encodeEscape(charCode) {
if (charCode < 0x20) {
return (charCode < 0x10 ? '\\x0' : '\\x') + charCode.toString(16);
}
var ch = String.fromCharCode(charCode);
if (ch === '\\' || ch === '-' || ch === '[' || ch === ']') {
ch = '\\' + ch;
}
return ch;
}
function caseFoldCharset(charSet) {
var charsetParts = charSet.substring(1, charSet.length - 1).match(
new RegExp(
'\\\\u[0-9A-Fa-f]{4}'
+ '|\\\\x[0-9A-Fa-f]{2}'
+ '|\\\\[0-3][0-7]{0,2}'
+ '|\\\\[0-7]{1,2}'
+ '|\\\\[\\s\\S]'
+ '|-'
+ '|[^-\\\\]',
'g'));
var groups = [];
var ranges = [];
var inverse = charsetParts[0] === '^';
for (var i = inverse ? 1 : 0, n = charsetParts.length; i < n; ++i) {
var p = charsetParts[i];
switch (p) {
case '\\B': case '\\b':
case '\\D': case '\\d':
case '\\S': case '\\s':
case '\\W': case '\\w':
groups.push(p);
continue;
}
var start = decodeEscape(p);
var end;
if (i + 2 < n && '-' === charsetParts[i + 1]) {
end = decodeEscape(charsetParts[i + 2]);
i += 2;
} else {
end = start;
}
ranges.push([start, end]);
// If the range might intersect letters, then expand it.
if (!(end < 65 || start > 122)) {
if (!(end < 65 || start > 90)) {
ranges.push([Math.max(65, start) | 32, Math.min(end, 90) | 32]);
}
if (!(end < 97 || start > 122)) {
ranges.push([Math.max(97, start) & ~32, Math.min(end, 122) & ~32]);
}
}
}
// [[1, 10], [3, 4], [8, 12], [14, 14], [16, 16], [17, 17]]
// -> [[1, 12], [14, 14], [16, 17]]
ranges.sort(function (a, b) { return (a[0] - b[0]) || (b[1] - a[1]); });
var consolidatedRanges = [];
var lastRange = [NaN, NaN];
for (var i = 0; i < ranges.length; ++i) {
var range = ranges[i];
if (range[0] <= lastRange[1] + 1) {
lastRange[1] = Math.max(lastRange[1], range[1]);
} else {
consolidatedRanges.push(lastRange = range);
}
}
var out = ['['];
if (inverse) { out.push('^'); }
out.push.apply(out, groups);
for (var i = 0; i < consolidatedRanges.length; ++i) {
var range = consolidatedRanges[i];
out.push(encodeEscape(range[0]));
if (range[1] > range[0]) {
if (range[1] + 1 > range[0]) { out.push('-'); }
out.push(encodeEscape(range[1]));
}
}
out.push(']');
return out.join('');
}
function allowAnywhereFoldCaseAndRenumberGroups(regex) {
// Split into character sets, escape sequences, punctuation strings
// like ('(', '(?:', ')', '^'), and runs of characters that do not
// include any of the above.
var parts = regex.source.match(
new RegExp(
'(?:'
+ '\\[(?:[^\\x5C\\x5D]|\\\\[\\s\\S])*\\]' // a character set
+ '|\\\\u[A-Fa-f0-9]{4}' // a unicode escape
+ '|\\\\x[A-Fa-f0-9]{2}' // a hex escape
+ '|\\\\[0-9]+' // a back-reference or octal escape
+ '|\\\\[^ux0-9]' // other escape sequence
+ '|\\(\\?[:!=]' // start of a non-capturing group
+ '|[\\(\\)\\^]' // start/emd of a group, or line start
+ '|[^\\x5B\\x5C\\(\\)\\^]+' // run of other characters
+ ')',
'g'));
var n = parts.length;
// Maps captured group numbers to the number they will occupy in
// the output or to -1 if that has not been determined, or to
// undefined if they need not be capturing in the output.
var capturedGroups = [];
// Walk over and identify back references to build the capturedGroups
// mapping.
for (var i = 0, groupIndex = 0; i < n; ++i) {
var p = parts[i];
if (p === '(') {
// groups are 1-indexed, so max group index is count of '('
++groupIndex;
} else if ('\\' === p.charAt(0)) {
var decimalValue = +p.substring(1);
if (decimalValue && decimalValue <= groupIndex) {
capturedGroups[decimalValue] = -1;
}
}
}
// Renumber groups and reduce capturing groups to non-capturing groups
// where possible.
for (var i = 1; i < capturedGroups.length; ++i) {
if (-1 === capturedGroups[i]) {
capturedGroups[i] = ++capturedGroupIndex;
}
}
for (var i = 0, groupIndex = 0; i < n; ++i) {
var p = parts[i];
if (p === '(') {
++groupIndex;
if (capturedGroups[groupIndex] === undefined) {
parts[i] = '(?:';
}
} else if ('\\' === p.charAt(0)) {
var decimalValue = +p.substring(1);
if (decimalValue && decimalValue <= groupIndex) {
parts[i] = '\\' + capturedGroups[groupIndex];
}
}
}
// Remove any prefix anchors so that the output will match anywhere.
// ^^ really does mean an anchored match though.
for (var i = 0, groupIndex = 0; i < n; ++i) {
if ('^' === parts[i] && '^' !== parts[i + 1]) { parts[i] = ''; }
}
// Expand letters to groupts to handle mixing of case-sensitive and
// case-insensitive patterns if necessary.
if (regex.ignoreCase && needToFoldCase) {
for (var i = 0; i < n; ++i) {
var p = parts[i];
var ch0 = p.charAt(0);
if (p.length >= 2 && ch0 === '[') {
parts[i] = caseFoldCharset(p);
} else if (ch0 !== '\\') {
// TODO: handle letters in numeric escapes.
parts[i] = p.replace(
/[a-zA-Z]/g,
function (ch) {
var cc = ch.charCodeAt(0);
return '[' + String.fromCharCode(cc & ~32, cc | 32) + ']';
});
}
}
}
return parts.join('');
}
var rewritten = [];
for (var i = 0, n = regexs.length; i < n; ++i) {
var regex = regexs[i];
if (regex.global || regex.multiline) { throw new Error('' + regex); }
rewritten.push(
'(?:' + allowAnywhereFoldCaseAndRenumberGroups(regex) + ')');
}
return new RegExp(rewritten.join('|'), ignoreCase ? 'gi' : 'g');
}
var PR_innerHtmlWorks = null;
function getInnerHtml(node) {
// inner html is hopelessly broken in Safari 2.0.4 when the content is
// an html description of well formed XML and the containing tag is a PRE
// tag, so we detect that case and emulate innerHTML.
if (null === PR_innerHtmlWorks) {
var testNode = document.createElement('PRE');
testNode.appendChild(
document.createTextNode('\n '));
PR_innerHtmlWorks = !/)[\r\n]+/g, '$1')
.replace(/(?:[\r\n]+[ \t]*)+/g, ' ');
}
return content;
}
var out = [];
for (var child = node.firstChild; child; child = child.nextSibling) {
normalizedHtml(child, out);
}
return out.join('');
}
/** returns a function that expand tabs to spaces. This function can be fed
* successive chunks of text, and will maintain its own internal state to
* keep track of how tabs are expanded.
* @return {function (string) : string} a function that takes
* plain text and return the text with tabs expanded.
* @private
*/
function makeTabExpander(tabWidth) {
var SPACES = ' ';
var charInLine = 0;
return function (plainText) {
// walk over each character looking for tabs and newlines.
// On tabs, expand them. On newlines, reset charInLine.
// Otherwise increment charInLine
var out = null;
var pos = 0;
for (var i = 0, n = plainText.length; i < n; ++i) {
var ch = plainText.charAt(i);
switch (ch) {
case '\t':
if (!out) { out = []; }
out.push(plainText.substring(pos, i));
// calculate how much space we need in front of this part
// nSpaces is the amount of padding -- the number of spaces needed
// to move us to the next column, where columns occur at factors of
// tabWidth.
var nSpaces = tabWidth - (charInLine % tabWidth);
charInLine += nSpaces;
for (; nSpaces >= 0; nSpaces -= SPACES.length) {
out.push(SPACES.substring(0, nSpaces));
}
pos = i + 1;
break;
case '\n':
charInLine = 0;
break;
default:
++charInLine;
}
}
if (!out) { return plainText; }
out.push(plainText.substring(pos));
return out.join('');
};
}
var pr_chunkPattern = new RegExp(
'[^<]+' // A run of characters other than '<'
+ '|<\!--[\\s\\S]*?--\>' // an HTML comment
+ '|' // a CDATA section
// a probable tag that should not be highlighted
+ '|<\/?[a-zA-Z](?:[^>\"\']|\'[^\']*\'|\"[^\"]*\")*>'
+ '|<', // A '<' that does not begin a larger chunk
'g');
var pr_commentPrefix = /^<\!--/;
var pr_cdataPrefix = /^) into their textual equivalent.
*
* @param {string} s html where whitespace is considered significant.
* @return {Object} source code and extracted tags.
* @private
*/
function extractTags(s) {
// since the pattern has the 'g' modifier and defines no capturing groups,
// this will return a list of all chunks which we then classify and wrap as
// PR_Tokens
var matches = s.match(pr_chunkPattern);
var sourceBuf = [];
var sourceBufLen = 0;
var extractedTags = [];
if (matches) {
for (var i = 0, n = matches.length; i < n; ++i) {
var match = matches[i];
if (match.length > 1 && match.charAt(0) === '<') {
if (pr_commentPrefix.test(match)) { continue; }
if (pr_cdataPrefix.test(match)) {
// strip CDATA prefix and suffix. Don't unescape since it's CDATA
sourceBuf.push(match.substring(9, match.length - 3));
sourceBufLen += match.length - 12;
} else if (pr_brPrefix.test(match)) {
//
tags are lexically significant so convert them to text.
// This is undone later.
sourceBuf.push('\n');
++sourceBufLen;
} else {
if (match.indexOf(PR_NOCODE) >= 0 && isNoCodeTag(match)) {
// A will start a section that should be
// ignored. Continue walking the list until we see a matching end
// tag.
var name = match.match(pr_tagNameRe)[2];
var depth = 1;
var j;
end_tag_loop:
for (j = i + 1; j < n; ++j) {
var name2 = matches[j].match(pr_tagNameRe);
if (name2 && name2[2] === name) {
if (name2[1] === '/') {
if (--depth === 0) { break end_tag_loop; }
} else {
++depth;
}
}
}
if (j < n) {
extractedTags.push(
sourceBufLen, matches.slice(i, j + 1).join(''));
i = j;
} else { // Ignore unclosed sections.
extractedTags.push(sourceBufLen, match);
}
} else {
extractedTags.push(sourceBufLen, match);
}
}
} else {
var literalText = htmlToText(match);
sourceBuf.push(literalText);
sourceBufLen += literalText.length;
}
}
}
return { source: sourceBuf.join(''), tags: extractedTags };
}
/** True if the given tag contains a class attribute with the nocode class. */
function isNoCodeTag(tag) {
return !!tag
// First canonicalize the representation of attributes
.replace(/\s(\w+)\s*=\s*(?:\"([^\"]*)\"|'([^\']*)'|(\S+))/g,
' $1="$2$3$4"')
// Then look for the attribute we want.
.match(/[cC][lL][aA][sS][sS]=\"[^\"]*\bnocode\b/);
}
/**
* Apply the given language handler to sourceCode and add the resulting
* decorations to out.
* @param {number} basePos the index of sourceCode within the chunk of source
* whose decorations are already present on out.
*/
function appendDecorations(basePos, sourceCode, langHandler, out) {
if (!sourceCode) { return; }
var job = {
source: sourceCode,
basePos: basePos
};
langHandler(job);
out.push.apply(out, job.decorations);
}
/** Given triples of [style, pattern, context] returns a lexing function,
* The lexing function interprets the patterns to find token boundaries and
* returns a decoration list of the form
* [index_0, style_0, index_1, style_1, ..., index_n, style_n]
* where index_n is an index into the sourceCode, and style_n is a style
* constant like PR_PLAIN. index_n-1 <= index_n, and style_n-1 applies to
* all characters in sourceCode[index_n-1:index_n].
*
* The stylePatterns is a list whose elements have the form
* [style : string, pattern : RegExp, DEPRECATED, shortcut : string].
*
* Style is a style constant like PR_PLAIN, or can be a string of the
* form 'lang-FOO', where FOO is a language extension describing the
* language of the portion of the token in $1 after pattern executes.
* E.g., if style is 'lang-lisp', and group 1 contains the text
* '(hello (world))', then that portion of the token will be passed to the
* registered lisp handler for formatting.
* The text before and after group 1 will be restyled using this decorator
* so decorators should take care that this doesn't result in infinite
* recursion. For example, the HTML lexer rule for SCRIPT elements looks
* something like ['lang-js', /<[s]cript>(.+?)<\/script>/]. This may match
* '';
$mark = Typecho_Widget::widget('Widget_Options')->plugin('LaTex')->mark;
echo '';
}
}
================================================
FILE: LaTex/latex.js
================================================
(function(global) {
var previousLatex = global.latex;
global.latex = {
parse: function(mark) {
if (!mark) {
return;
}
var $ = document.querySelectorAll.bind(document);
var nodes = $('code.lang-' + mark);
for (var i = 0, l = nodes.length; i < l; i++) {
var node = nodes[i];
var latex_image = document.createElement("img");
latex_image.src = "http://latex.codecogs.com/png.latex?" + node.innerHTML;
// replace with image
var parent = node.parentNode;
parent.insertBefore(latex_image, node);
parent.removeChild(node);
}
},
noConflict: function() {
global.latex = previousLatex;
return this;
}
}
})(this);
================================================
FILE: MagikeToTypecho/Action.php
================================================
widget('Widget_Options');
$dbConfig = $options->plugin('MagikeToTypecho');
/** 初始化一个db */
if (Typecho_Db_Adapter_Mysql::isAvailable()) {
$db = new Typecho_Db('Mysql', $dbConfig->prefix);
} else {
$db = new Typecho_Db('Pdo_Mysql', $dbConfig->prefix);
}
/** 只读即可 */
$db->addServer(array (
'host' => $dbConfig->host,
'user' => $dbConfig->user,
'password' => $dbConfig->password,
'charset' => 'utf8',
'port' => $dbConfig->port,
'database' => $dbConfig->database
), Typecho_Db::READ);
/** 删除当前内容 */
$masterDb = Typecho_Db::get();
$this->widget('Widget_Abstract_Contents')->to($contents)->delete($masterDb->sql()->where('1 = 1'));
$this->widget('Widget_Abstract_Comments')->to($comments)->delete($masterDb->sql()->where('1 = 1'));
$this->widget('Widget_Abstract_Metas')->to($metas)->delete($masterDb->sql()->where('1 = 1'));
$this->widget('Widget_Contents_Post_Edit')->to($edit);
$this->widget('Widget_Abstract_Users')->to($users)->delete($masterDb->sql()->where('uid <> 1'));
$masterDb->query($masterDb->delete('table.relationships')->where('1 = 1'));
$userId = $this->widget('Widget_User')->uid;
/** 转换用户 */
$rows = $db->fetchAll($db->select()->from('table.users'));
foreach ($rows as $row) {
if (1 != $row['user_id']) {
$users->insert(array(
'uid' => $row['user_id'],
'name' => $row['user_name'],
'password' => $row['user_password'],
'mail' => $row['user_mail'],
'url' => $row['user_url'],
'screenName'=> $row['user_nick'],
'created' => strtotime($row['user_register']),
'group' => array_search($row['user_group'], $this->widget('Widget_User')->groups)
));
}
}
/** 转换全局变量 */
$rows = $db->fetchAll($db->select()->from('table.statics'));
$static = array();
foreach ($rows as $row) {
$static[$row['static_name']] = $row['static_value'];
}
/** 转换文件 */
$files = $db->fetchAll($db->select()->from('table.files'));
if (!is_dir(__TYPECHO_ROOT_DIR__ . '/usr/uploads/')) {
mkdir(__TYPECHO_ROOT_DIR__ . '/usr/uploads/', 0766);
}
$pattern = array();
$replace = array();
foreach ($files as $file) {
$path = __TYPECHO_ROOT_DIR__ . '/data/upload/' . substr($file['file_guid'], 0, 2) . '/' .
substr($file['file_guid'], 2, 2) . '/' . $file['file_guid'];
if (file_exists($path)) {
$file['file_time'] = empty($file['file_time']) ? $options->gmtTime : $file['file_time'];
$year = idate('Y', $file['file_time']);
$month = idate('m', $file['file_time']);
$day = idate('d', $file['file_time']);
if (!is_dir(__TYPECHO_ROOT_DIR__ . "/usr/uploads/{$year}")) {
mkdir(__TYPECHO_ROOT_DIR__ . "/usr/uploads/{$year}", 0766);
}
if (!is_dir(__TYPECHO_ROOT_DIR__ . "/usr/uploads/{$year}/{$month}")) {
mkdir(__TYPECHO_ROOT_DIR__ . "/usr/uploads/{$year}/{$month}", 0766);
}
if (!is_dir(__TYPECHO_ROOT_DIR__ . "/usr/uploads/{$year}/{$month}/{$day}")) {
mkdir(__TYPECHO_ROOT_DIR__ . "/usr/uploads/{$year}/{$month}/{$day}", 0766);
}
$parts = explode('.', $file['file_name']);
$ext = array_pop($parts);
copy($path, __TYPECHO_ROOT_DIR__ . "/usr/uploads/{$year}/{$month}/{$day}/{$file['file_id']}.{$ext}");
$new = Typecho_Common::url("/usr/uploads/{$year}/{$month}/{$day}/{$file['file_id']}.{$ext}", $options->siteUrl);
$old = Typecho_Common::url("/res/{$file['file_id']}/{$file['file_name']}", $static['siteurl'] . '/index.php');
$pattern[] = '/' . str_replace('\/index\.php', '(\/index\.php)?', preg_quote($old, '/')) . '/is';
$replace[] = $new;
}
}
/** 转换评论 */
$i = 1;
while (true) {
$result = $db->query($db->select()->from('table.comments')
->order('comment_id', Typecho_Db::SORT_ASC)->page($i, 100));
$j = 0;
while ($row = $db->fetchRow($result)) {
$comments->insert(array(
'coid' => $row['comment_id'],
'cid' => $row['post_id'],
'created' => $row['comment_date'],
'author' => $row['comment_user'],
'authorId' => $row['user_id'],
'ownerId' => $userId,
'mail' => $row['comment_email'],
'url' => $row['comment_homepage'],
'ip' => $row['comment_ip'],
'agent' => $row['comment_agent'],
'text' => $row['comment_text'],
'type' => $row['comment_type'],
'status' => $row['comment_publish'],
'parent' => $row['comment_parent']
));
$j ++;
unset($row);
}
if ($j < 100) {
break;
}
$i ++;
unset($result);
}
/** 转换分类 */
$cats = $db->fetchAll($db->select()->from('table.categories'));
foreach ($cats as $cat) {
$metas->insert(array(
'mid' => $cat['category_id'],
'name' => $cat['category_name'],
'slug' => $cat['category_postname'],
'description' => $cat['category_describe'],
'count' => 0,
'type' => 'category',
'order' => $cat['category_sort']
));
}
/** 转换内容 */
$i = 1;
while (true) {
$result = $db->query($db->select()->from('table.posts')
->order('post_id', Typecho_Db::SORT_ASC)->page($i, 100));
$j = 0;
while ($row = $db->fetchRow($result)) {
$row['post_content'] = preg_replace(
array("/\s*/is", "/\s*<\/p>\s*/is", "/\s*
\s*/is",
"/\s*<(div|blockquote|pre|table|ol|ul)>/is", "/<\/(div|blockquote|pre|table|ol|ul)>\s*/is"),
array('', "\n\n", "\n", "\n\n<\\1>", "\\1>\n\n"),
$row['post_content']);
$contents->insert(array(
'cid' => $row['post_id'],
'title' => $row['post_title'],
'slug' => $row['post_name'],
'created' => $row['post_time'],
'modified' => $row['post_edit_time'],
'text' => preg_replace($pattern, $replace, $row['post_content']),
'order' => 0,
'authorId' => $row['user_id'],
'template' => NULL,
'type' => $row['post_is_page'] ? 'page' : 'post',
'status' => $row['post_is_draft'] ? 'draft' : 'publish',
'password' => $row['post_password'],
'commentsNum' => $row['post_comment_num'],
'allowComment' => $row['post_allow_comment'],
'allowFeed' => $row['post_allow_feed'],
'allowPing' => $row['post_allow_ping']
));
/** 插入分类关系 */
$edit->setCategories($row['post_id'], array($row['category_id']), !$row['post_is_draft']);
/** 设置标签 */
$edit->setTags($row['post_id'], $row['post_tags'], !$row['post_is_draft']);
$j ++;
unset($row);
}
if ($j < 100) {
break;
}
$i ++;
unset($result);
}
$this->widget('Widget_Notice')->set(_t("数据已经转换完成"), NULL, 'success');
$this->response->goBack();
}
public function action()
{
$this->widget('Widget_User')->pass('administrator');
$this->on($this->request->isPost())->doImport();
}
}
================================================
FILE: MagikeToTypecho/Plugin.php
================================================
' . _t('%s 目录不可写, 可能会导致附件转换不成功', __TYPECHO_ROOT_DIR__ . '/usr/uploads/') . '';
}
Helper::addPanel(1, 'MagikeToTypecho/panel.php', _t('从Magike导入数据'), _t('从Magike导入数据'), 'administrator');
Helper::addAction('magike-to-typecho', 'MagikeToTypecho_Action');
return _t('请在插件设置里设置 Magike 所在的数据库参数') . $error;
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate()
{
Helper::removeAction('magike-to-typecho');
Helper::removePanel(1, 'MagikeToTypecho/panel.php');
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$host = new Typecho_Widget_Helper_Form_Element_Text('host', NULL, 'localhost',
_t('数据库地址'), _t('请填写 Magike 所在的数据库地址'));
$form->addInput($host->addRule('required', _t('必须填写一个数据库地址')));
$port = new Typecho_Widget_Helper_Form_Element_Text('port', NULL, '3306',
_t('数据库端口'), _t('Magike 所在的数据库服务器端口'));
$port->input->setAttribute('class', 'mini');
$form->addInput($port->addRule('required', _t('必须填写数据库端口'))
->addRule('isInteger', _t('端口号必须是纯数字')));
$user = new Typecho_Widget_Helper_Form_Element_Text('user', NULL, 'root',
_t('数据库用户名'));
$form->addInput($user->addRule('required', _t('必须填写数据库用户名')));
$password = new Typecho_Widget_Helper_Form_Element_Password('password', NULL, NULL,
_t('数据库密码'));
$form->addInput($password);
$database = new Typecho_Widget_Helper_Form_Element_Text('database', NULL, 'magike',
_t('数据库名称'), _t('Magike 所在的数据库名称'));
$form->addInput($database->addRule('required', _t('您必须填写数据库名称')));
$prefix = new Typecho_Widget_Helper_Form_Element_Text('prefix', NULL, 'mg_',
_t('表前缀'), _t('所有 Magike 数据表的前缀'));
$form->addInput($prefix->addRule('required', _t('您必须填写表前缀')));
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
}
================================================
FILE: MagikeToTypecho/panel.php
================================================
plugin('MagikeToTypecho');
/** 初始化一个db */
if (Typecho_Db_Adapter_Mysql::isAvailable()) {
$magikeDb = new Typecho_Db('Mysql', $dbConfig->prefix);
} else {
$magikeDb = new Typecho_Db('Pdo_Mysql', $dbConfig->prefix);
}
/** 只读即可 */
$magikeDb->addServer(array (
'host' => $dbConfig->host,
'user' => $dbConfig->user,
'password' => $dbConfig->password,
'charset' => 'utf8',
'port' => $dbConfig->port,
'database' => $dbConfig->database
), Typecho_Db::READ);
$rows = $magikeDb->fetchAll($magikeDb->select()->from('table.statics'));
$static = array();
foreach ($rows as $row) {
$static[$row['static_name']] = $row['static_value'];
}
} catch (Typecho_Db_Exception $e) {
$success = false;
}
include 'header.php';
include 'menu.php';
?>
================================================
FILE: MathJax.php
================================================
footer = array('MathJax', 'footer');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate()
{}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form){}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 输出尾部js
*
* @access public
* @param unknown $footer
* @return unknown
*/
public static function footer() {
echo '';
}
}
================================================
FILE: PageToLinks.php
================================================
fetchObject($db->select('text')->from('table.contents')
->where('slug = ?', $slug)->limit(1));
if (!$contents) {
return;
}
$text = $contents->text;
$cats = preg_split("/<\/(ol|ul)>/i", $text);
foreach ($cats as $cat) {
$item = trim($cat);
if ($item) {
$matches = array_map('trim', preg_split("/<(ol|ul)[^>]*>/i", $item));
if (2 == count($matches)) {
list ($title, $list) = $matches;
echo "<$tag>" . strip_tags($title) . "$tag>";
echo "<$listTag>" . $list . "$listTag>";
}
}
}
}
}
================================================
FILE: PostToQzone/Plugin.php
================================================
insert =
array('PostToQzone_Plugin', 'publish');
if(!extension_loaded("sockets")){
throw new Typecho_Plugin_Exception(_t('对不起, 您的主机不支持socket扩展, 无法正常使用此功能'));
}
return _t('请配置您的qq号码以及密码,以便发布文章到Qzone');
}
/**
* deactivate
*
* @static
* @access public
* @return void
*/
public static function deactivate()
{
}
/**
* 插件配置面板
*
* @param Typecho_Widget_Helper_Form $form
* @static
* @access public
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$qq = new Typecho_Widget_Helper_Form_Element_Text('qq', NULL, NULL,
_t('qq号码'), _t('请填写您的qq号码'));
$qq->addRule('isInteger', _t('qq号码必须是纯数字'));
$form->addInput($qq->addRule('required', _t('必须填写一个qq号码')));
$psw = new Typecho_Widget_Helper_Form_Element_Password('psw', NULL, NULL,
_t('qq邮箱密码'), _t('请填写您的qq邮箱密码'));
$form->addInput($psw->addRule('required', _t('必须填写一个qq邮箱密码')));
$title = new Typecho_Widget_Helper_Form_Element_Text('title', NULL, '{post_title}',
_t('标题模板'), _t('请填写您的标题模板'));
$form->addInput($title->addRule('required', _t('必须填写一个标题模板')));
$content = new Typecho_Widget_Helper_Form_Element_Textarea('content', NULL, '{post_content}',
_t('内容模板'), _t('请填写您的内容模板'));
$form->addInput($content->addRule('required', _t('必须填写一个内容模板')));
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 发送文章到qzone
*
* @param mixed $contents 文章结构体
* @access public
* @return mixed $contents 处理后的文章结构体
*/
public function publish($contents)
{
//todo:增加一个选项,如果选择发送的qzone的话再发到qzone
$options = Typecho_Widget::widget('Widget_Options');
$config = $options->plugin('PostToQzone');
$config = postToQzoneDefault($config);
if($config->qq > 1000 && !empty($contents['title']) && !empty($contents['text'])){
$post_content = str_replace('{post_content}', $contents['text'], $config->content);
$post_content = str_replace('{post_title}', $contents['title'], $post_content);
$post_title = str_replace('{post_title}', $contents['title'], $config->title);
$m=new Mailer($config->qq,$config->psw);
$m->Halo($post_title,$post_content);
}
return $contents;
}
}
function postToQzoneDefault($config){
if(strpos($config->title,'{post_title}') === false){
$config->title = '{post_title}';
}
if(strpos($config->content,'{post_content}') === false){
$config->content = '{post_content}';
}
return $config;
}
class Mailer extends PHPMailer
{
var $qq=null;
function Mailer($qq,$psw) {
$this->qq=$qq;
$this->From = "{$qq}@qq.com";
$this->FromName = $qq;
$this->Host = "smtp.qq.com";
$this->Mailer = "smtp";
$this->WordWrap = 75;
$this->CharSet = Typecho_Widget::widget('Widget_Options')->charset;
$this->Encoding = 'base64';
$this->SMTPAuth = true;
$this->IsHTML(true);
$this->Username = $qq;
$this->Password = $psw;
}
function Halo($subject,$body){
$this->AddAddress("{$this->qq}@qzone.qq.com", "{$this->qq}@qzone.qq.com");
$this->Subject = $subject;
$this->Body = $body;
return $this->Send();
}
}
class Crypter
{
var $key;
function Crypter($clave){
$this->key = $clave;
}
function keyED($txt) {
$encrypt_key = md5($this->key);
$ctr=0;
$tmp = "";
for ($i=0;$ikeyED($tmp));
}
function decrypt($txt) {
$txt = $this->keyED(base64_decode($txt));
$tmp = "";
for ($i=0;$iContentType = 'text/html';
} else {
$this->ContentType = 'text/plain';
}
}
/**
* Sets Mailer to send message using SMTP.
* @return void
*/
function IsSMTP() {
$this->Mailer = 'smtp';
}
/**
* Sets Mailer to send message using PHP mail() function.
* @return void
*/
function IsMail() {
$this->Mailer = 'mail';
}
/**
* Sets Mailer to send message using the $Sendmail program.
* @return void
*/
function IsSendmail() {
$this->Mailer = 'sendmail';
}
/**
* Sets Mailer to send message using the qmail MTA.
* @return void
*/
function IsQmail() {
$this->Sendmail = '/var/qmail/bin/sendmail';
$this->Mailer = 'sendmail';
}
/////////////////////////////////////////////////
// METHODS, RECIPIENTS
/////////////////////////////////////////////////
/**
* Adds a "To" address.
* @param string $address
* @param string $name
* @return void
*/
function AddAddress($address, $name = '') {
$cur = count($this->to);
$this->to[$cur][0] = trim($address);
$this->to[$cur][1] = $name;
}
/**
* Adds a "Cc" address. Note: this function works
* with the SMTP mailer on win32, not with the "mail"
* mailer.
* @param string $address
* @param string $name
* @return void
*/
function AddCC($address, $name = '') {
$cur = count($this->cc);
$this->cc[$cur][0] = trim($address);
$this->cc[$cur][1] = $name;
}
/**
* Adds a "Bcc" address. Note: this function works
* with the SMTP mailer on win32, not with the "mail"
* mailer.
* @param string $address
* @param string $name
* @return void
*/
function AddBCC($address, $name = '') {
$cur = count($this->bcc);
$this->bcc[$cur][0] = trim($address);
$this->bcc[$cur][1] = $name;
}
/**
* Adds a "Reply-To" address.
* @param string $address
* @param string $name
* @return void
*/
function AddReplyTo($address, $name = '') {
$cur = count($this->ReplyTo);
$this->ReplyTo[$cur][0] = trim($address);
$this->ReplyTo[$cur][1] = $name;
}
/////////////////////////////////////////////////
// METHODS, MAIL SENDING
/////////////////////////////////////////////////
/**
* Creates message and assigns Mailer. If the message is
* not sent successfully then it returns false. Use the ErrorInfo
* variable to view description of the error.
* @return bool
*/
function Send() {
$header = '';
$body = '';
$result = true;
if((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
$this->SetError($this->Lang('provide_address'));
return false;
}
/* Set whether the message is multipart/alternative */
if(!empty($this->AltBody)) {
$this->ContentType = 'multipart/alternative';
}
$this->error_count = 0; // reset errors
$this->SetMessageType();
$header .= $this->CreateHeader();
$body = $this->CreateBody();
if($body == '') {
return false;
}
/* Choose the mailer */
switch($this->Mailer) {
case 'sendmail':
$result = $this->SendmailSend($header, $body);
break;
case 'smtp':
$result = $this->SmtpSend($header, $body);
break;
case 'mail':
$result = $this->MailSend($header, $body);
break;
default:
$result = $this->MailSend($header, $body);
break;
//$this->SetError($this->Mailer . $this->Lang('mailer_not_supported'));
//$result = false;
//break;
}
return $result;
}
/**
* Sends mail using the $Sendmail program.
* @access private
* @return bool
*/
function SendmailSend($header, $body) {
if ($this->Sender != '') {
$sendmail = sprintf("%s -oi -f %s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
} else {
$sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail));
}
if(!@$mail = popen($sendmail, 'w')) {
$this->SetError($this->Lang('execute') . $this->Sendmail);
return false;
}
fputs($mail, $header);
fputs($mail, $body);
$result = pclose($mail);
if (version_compare(phpversion(), '4.2.3') == -1) {
$result = $result >> 8 & 0xFF;
}
if($result != 0) {
$this->SetError($this->Lang('execute') . $this->Sendmail);
return false;
}
return true;
}
/**
* Sends mail using the PHP mail() function.
* @access private
* @return bool
*/
function MailSend($header, $body) {
$to = '';
for($i = 0; $i < count($this->to); $i++) {
if($i != 0) { $to .= ', '; }
$to .= $this->AddrFormat($this->to[$i]);
}
$toArr = split(',', $to);
$params = sprintf("-oi -f %s", $this->Sender);
if ($this->Sender != '' && strlen(ini_get('safe_mode')) < 1) {
$old_from = ini_get('sendmail_from');
ini_set('sendmail_from', $this->Sender);
if ($this->SingleTo === true && count($toArr) > 1) {
foreach ($toArr as $key => $val) {
$rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
}
} else {
$rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
}
} else {
if ($this->SingleTo === true && count($toArr) > 1) {
foreach ($toArr as $key => $val) {
$rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
}
} else {
$rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header);
}
}
if (isset($old_from)) {
ini_set('sendmail_from', $old_from);
}
if(!$rt) {
$this->SetError($this->Lang('instantiate'));
return false;
}
return true;
}
/**
* Sends mail via SMTP using PhpSMTP (Author:
* Chris Ryan). Returns bool. Returns false if there is a
* bad MAIL FROM, RCPT, or DATA input.
* @access private
* @return bool
*/
function SmtpSend($header, $body) {
include_once($this->PluginDir . 'smtp.php');
$error = '';
$bad_rcpt = array();
if(!$this->SmtpConnect()) {
return false;
}
$smtp_from = ($this->Sender == '') ? $this->From : $this->Sender;
if(!$this->smtp->Mail($smtp_from)) {
$error = $this->Lang('from_failed') . $smtp_from;
$this->SetError($error);
$this->smtp->Reset();
return false;
}
/* Attempt to send attach all recipients */
for($i = 0; $i < count($this->to); $i++) {
if(!$this->smtp->Recipient($this->to[$i][0])) {
$bad_rcpt[] = $this->to[$i][0];
}
}
for($i = 0; $i < count($this->cc); $i++) {
if(!$this->smtp->Recipient($this->cc[$i][0])) {
$bad_rcpt[] = $this->cc[$i][0];
}
}
for($i = 0; $i < count($this->bcc); $i++) {
if(!$this->smtp->Recipient($this->bcc[$i][0])) {
$bad_rcpt[] = $this->bcc[$i][0];
}
}
if(count($bad_rcpt) > 0) { // Create error message
for($i = 0; $i < count($bad_rcpt); $i++) {
if($i != 0) {
$error .= ', ';
}
$error .= $bad_rcpt[$i];
}
$error = $this->Lang('recipients_failed') . $error;
$this->SetError($error);
$this->smtp->Reset();
return false;
}
if(!$this->smtp->Data($header . $body)) {
$this->SetError($this->Lang('data_not_accepted'));
$this->smtp->Reset();
return false;
}
if($this->SMTPKeepAlive == true) {
$this->smtp->Reset();
} else {
$this->SmtpClose();
}
return true;
}
/**
* Initiates a connection to an SMTP server. Returns false if the
* operation failed.
* @access private
* @return bool
*/
function SmtpConnect() {
if($this->smtp == NULL) {
$this->smtp = new SMTP();
}
$this->smtp->do_debug = $this->SMTPDebug;
$hosts = explode(';', $this->Host);
$index = 0;
$connection = ($this->smtp->Connected());
/* Retry while there is no connection */
while($index < count($hosts) && $connection == false) {
$hostinfo = array();
if(eregi('^(.+):([0-9]+)$', $hosts[$index], $hostinfo)) {
$host = $hostinfo[1];
$port = $hostinfo[2];
} else {
$host = $hosts[$index];
$port = $this->Port;
}
if($this->smtp->Connect(((!empty($this->SMTPSecure))?$this->SMTPSecure.'://':'').$host, $port, $this->Timeout)) {
if ($this->Helo != '') {
$this->smtp->Hello($this->Helo);
} else {
$this->smtp->Hello($this->ServerHostname());
}
$connection = true;
if($this->SMTPAuth) {
if(!$this->smtp->Authenticate($this->Username, $this->Password)) {
$this->SetError($this->Lang('authenticate'));
$this->smtp->Reset();
$connection = false;
}
}
}
$index++;
}
if(!$connection) {
$this->SetError($this->Lang('connect_host'));
}
return $connection;
}
/**
* Closes the active SMTP session if one exists.
* @return void
*/
function SmtpClose() {
if($this->smtp != NULL) {
if($this->smtp->Connected()) {
$this->smtp->Quit();
$this->smtp->Close();
}
}
}
/**
* Sets the language for all class error messages. Returns false
* if it cannot load the language file. The default language type
* is English.
* @param string $lang_type Type of language (e.g. Portuguese: "br")
* @param string $lang_path Path to the language file directory
* @access public
* @return bool
*/
function SetLanguage($lang_type, $lang_path = 'language/') {
if(file_exists($lang_path.'phpmailer.lang-'.$lang_type.'.php')) {
include($lang_path.'phpmailer.lang-'.$lang_type.'.php');
} elseif (file_exists($lang_path.'phpmailer.lang-en.php')) {
include($lang_path.'phpmailer.lang-en.php');
} else {
$this->SetError('Could not load language file');
return false;
}
$this->language = $PHPMAILER_LANG;
return true;
}
/////////////////////////////////////////////////
// METHODS, MESSAGE CREATION
/////////////////////////////////////////////////
/**
* Creates recipient headers.
* @access private
* @return string
*/
function AddrAppend($type, $addr) {
$addr_str = $type . ': ';
$addr_str .= $this->AddrFormat($addr[0]);
if(count($addr) > 1) {
for($i = 1; $i < count($addr); $i++) {
$addr_str .= ', ' . $this->AddrFormat($addr[$i]);
}
}
$addr_str .= $this->LE;
return $addr_str;
}
/**
* Formats an address correctly.
* @access private
* @return string
*/
function AddrFormat($addr) {
if(empty($addr[1])) {
$formatted = $this->SecureHeader($addr[0]);
} else {
$formatted = $this->EncodeHeader($this->SecureHeader($addr[1]), 'phrase') . " <" . $this->SecureHeader($addr[0]) . ">";
}
return $formatted;
}
/**
* Wraps message for use with mailers that do not
* automatically perform wrapping and for quoted-printable.
* Original written by philippe.
* @access private
* @return string
*/
function WrapText($message, $length, $qp_mode = false) {
$soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
// If utf-8 encoding is used, we will need to make sure we don't
// split multibyte characters when we wrap
$is_utf8 = (strtolower($this->CharSet) == "utf-8");
$message = $this->FixEOL($message);
if (substr($message, -1) == $this->LE) {
$message = substr($message, 0, -1);
}
$line = explode($this->LE, $message);
$message = '';
for ($i=0 ;$i < count($line); $i++) {
$line_part = explode(' ', $line[$i]);
$buf = '';
for ($e = 0; $e $length)) {
$space_left = $length - strlen($buf) - 1;
if ($e != 0) {
if ($space_left > 20) {
$len = $space_left;
if ($is_utf8) {
$len = $this->UTF8CharBoundary($word, $len);
} elseif (substr($word, $len - 1, 1) == "=") {
$len--;
} elseif (substr($word, $len - 2, 1) == "=") {
$len -= 2;
}
$part = substr($word, 0, $len);
$word = substr($word, $len);
$buf .= ' ' . $part;
$message .= $buf . sprintf("=%s", $this->LE);
} else {
$message .= $buf . $soft_break;
}
$buf = '';
}
while (strlen($word) > 0) {
$len = $length;
if ($is_utf8) {
$len = $this->UTF8CharBoundary($word, $len);
} elseif (substr($word, $len - 1, 1) == "=") {
$len--;
} elseif (substr($word, $len - 2, 1) == "=") {
$len -= 2;
}
$part = substr($word, 0, $len);
$word = substr($word, $len);
if (strlen($word) > 0) {
$message .= $part . sprintf("=%s", $this->LE);
} else {
$buf = $part;
}
}
} else {
$buf_o = $buf;
$buf .= ($e == 0) ? $word : (' ' . $word);
if (strlen($buf) > $length and $buf_o != '') {
$message .= $buf_o . $soft_break;
$buf = $word;
}
}
}
$message .= $buf . $this->LE;
}
return $message;
}
/**
* Finds last character boundary prior to maxLength in a utf-8
* quoted (printable) encoded string.
* Original written by Colin Brown.
* @access private
* @param string $encodedText utf-8 QP text
* @param int $maxLength find last character boundary prior to this length
* @return int
*/
function UTF8CharBoundary($encodedText, $maxLength) {
$foundSplitPos = false;
$lookBack = 3;
while (!$foundSplitPos) {
$lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack);
$encodedCharPos = strpos($lastChunk, "=");
if ($encodedCharPos !== false) {
// Found start of encoded character byte within $lookBack block.
// Check the encoded byte value (the 2 chars after the '=')
$hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2);
$dec = hexdec($hex);
if ($dec < 128) { // Single byte character.
// If the encoded char was found at pos 0, it will fit
// otherwise reduce maxLength to start of the encoded char
$maxLength = ($encodedCharPos == 0) ? $maxLength :
$maxLength - ($lookBack - $encodedCharPos);
$foundSplitPos = true;
} elseif ($dec >= 192) { // First byte of a multi byte character
// Reduce maxLength to split at start of character
$maxLength = $maxLength - ($lookBack - $encodedCharPos);
$foundSplitPos = true;
} elseif ($dec < 192) { // Middle byte of a multi byte character, look further back
$lookBack += 3;
}
} else {
// No encoded character found
$foundSplitPos = true;
}
}
return $maxLength;
}
/**
* Set the body wrapping.
* @access private
* @return void
*/
function SetWordWrap() {
if($this->WordWrap < 1) {
return;
}
switch($this->message_type) {
case 'alt':
/* fall through */
case 'alt_attachments':
$this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
break;
default:
$this->Body = $this->WrapText($this->Body, $this->WordWrap);
break;
}
}
/**
* Assembles message header.
* @access private
* @return string
*/
function CreateHeader() {
$result = '';
/* Set the boundaries */
$uniq_id = md5(uniqid(time()));
$this->boundary[1] = 'b1_' . $uniq_id;
$this->boundary[2] = 'b2_' . $uniq_id;
$result .= $this->HeaderLine('Date', $this->RFCDate());
if($this->Sender == '') {
$result .= $this->HeaderLine('Return-Path', trim($this->From));
} else {
$result .= $this->HeaderLine('Return-Path', trim($this->Sender));
}
/* To be created automatically by mail() */
if($this->Mailer != 'mail') {
if(count($this->to) > 0) {
$result .= $this->AddrAppend('To', $this->to);
} elseif (count($this->cc) == 0) {
$result .= $this->HeaderLine('To', 'undisclosed-recipients:;');
}
if(count($this->cc) > 0) {
$result .= $this->AddrAppend('Cc', $this->cc);
}
}
$from = array();
$from[0][0] = trim($this->From);
$from[0][1] = $this->FromName;
$result .= $this->AddrAppend('From', $from);
/* sendmail and mail() extract Cc from the header before sending */
if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->cc) > 0)) {
$result .= $this->AddrAppend('Cc', $this->cc);
}
/* sendmail and mail() extract Bcc from the header before sending */
if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->bcc) > 0)) {
$result .= $this->AddrAppend('Bcc', $this->bcc);
}
if(count($this->ReplyTo) > 0) {
$result .= $this->AddrAppend('Reply-To', $this->ReplyTo);
}
/* mail() sets the subject itself */
if($this->Mailer != 'mail') {
$result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject)));
}
if($this->MessageID != '') {
$result .= $this->HeaderLine('Message-ID',$this->MessageID);
} else {
$result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
}
$result .= $this->HeaderLine('X-Priority', $this->Priority);
$result .= $this->HeaderLine('X-Mailer', 'PHPMailer (phpmailer.sourceforge.net) [version ' . $this->Version . ']');
if($this->ConfirmReadingTo != '') {
$result .= $this->HeaderLine('Disposition-Notification-To', '<' . trim($this->ConfirmReadingTo) . '>');
}
// Add custom headers
for($index = 0; $index < count($this->CustomHeader); $index++) {
$result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]), $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
}
if (!$this->sign_key_file) {
$result .= $this->HeaderLine('MIME-Version', '1.0');
$result .= $this->GetMailMIME();
}
return $result;
}
/**
* Returns the message MIME.
* @access private
* @return string
*/
function GetMailMIME() {
$result = '';
switch($this->message_type) {
case 'plain':
$result .= $this->HeaderLine('Content-Transfer-Encoding', $this->Encoding);
$result .= sprintf("Content-Type: %s; charset=\"%s\"", $this->ContentType, $this->CharSet);
break;
case 'attachments':
/* fall through */
case 'alt_attachments':
if($this->InlineImageExists()){
$result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s", 'multipart/related', $this->LE, $this->LE, $this->boundary[1], $this->LE);
} else {
$result .= $this->HeaderLine('Content-Type', 'multipart/mixed;');
$result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
}
break;
case 'alt':
$result .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
$result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
break;
}
if($this->Mailer != 'mail') {
$result .= $this->LE.$this->LE;
}
return $result;
}
/**
* Assembles the message body. Returns an empty string on failure.
* @access private
* @return string
*/
function CreateBody() {
$result = '';
if ($this->sign_key_file) {
$result .= $this->GetMailMIME();
}
$this->SetWordWrap();
switch($this->message_type) {
case 'alt':
$result .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
$result .= $this->EncodeString($this->AltBody, $this->Encoding);
$result .= $this->LE.$this->LE;
$result .= $this->GetBoundary($this->boundary[1], '', 'text/html', '');
$result .= $this->EncodeString($this->Body, $this->Encoding);
$result .= $this->LE.$this->LE;
$result .= $this->EndBoundary($this->boundary[1]);
break;
case 'plain':
$result .= $this->EncodeString($this->Body, $this->Encoding);
break;
case 'attachments':
$result .= $this->GetBoundary($this->boundary[1], '', '', '');
$result .= $this->EncodeString($this->Body, $this->Encoding);
$result .= $this->LE;
$result .= $this->AttachAll();
break;
case 'alt_attachments':
$result .= sprintf("--%s%s", $this->boundary[1], $this->LE);
$result .= sprintf("Content-Type: %s;%s" . "\tboundary=\"%s\"%s", 'multipart/alternative', $this->LE, $this->boundary[2], $this->LE.$this->LE);
$result .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '') . $this->LE; // Create text body
$result .= $this->EncodeString($this->AltBody, $this->Encoding);
$result .= $this->LE.$this->LE;
$result .= $this->GetBoundary($this->boundary[2], '', 'text/html', '') . $this->LE; // Create the HTML body
$result .= $this->EncodeString($this->Body, $this->Encoding);
$result .= $this->LE.$this->LE;
$result .= $this->EndBoundary($this->boundary[2]);
$result .= $this->AttachAll();
break;
}
if($this->IsError()) {
$result = '';
} else if ($this->sign_key_file) {
$file = tempnam("", "mail");
$fp = fopen($file, "w");
fwrite($fp, $result);
fclose($fp);
$signed = tempnam("", "signed");
if (@openssl_pkcs7_sign($file, $signed, "file://".$this->sign_key_file, array("file://".$this->sign_key_file, $this->sign_key_pass), null)) {
$fp = fopen($signed, "r");
$result = fread($fp, filesize($this->sign_key_file));
fclose($fp);
} else {
$this->SetError($this->Lang("signing").openssl_error_string());
$result = '';
}
unlink($file);
unlink($signed);
}
return $result;
}
/**
* Returns the start of a message boundary.
* @access private
*/
function GetBoundary($boundary, $charSet, $contentType, $encoding) {
$result = '';
if($charSet == '') {
$charSet = $this->CharSet;
}
if($contentType == '') {
$contentType = $this->ContentType;
}
if($encoding == '') {
$encoding = $this->Encoding;
}
$result .= $this->TextLine('--' . $boundary);
$result .= sprintf("Content-Type: %s; charset = \"%s\"", $contentType, $charSet);
$result .= $this->LE;
$result .= $this->HeaderLine('Content-Transfer-Encoding', $encoding);
$result .= $this->LE;
return $result;
}
/**
* Returns the end of a message boundary.
* @access private
*/
function EndBoundary($boundary) {
return $this->LE . '--' . $boundary . '--' . $this->LE;
}
/**
* Sets the message type.
* @access private
* @return void
*/
function SetMessageType() {
if(count($this->attachment) < 1 && strlen($this->AltBody) < 1) {
$this->message_type = 'plain';
} else {
if(count($this->attachment) > 0) {
$this->message_type = 'attachments';
}
if(strlen($this->AltBody) > 0 && count($this->attachment) < 1) {
$this->message_type = 'alt';
}
if(strlen($this->AltBody) > 0 && count($this->attachment) > 0) {
$this->message_type = 'alt_attachments';
}
}
}
/* Returns a formatted header line.
* @access private
* @return string
*/
function HeaderLine($name, $value) {
return $name . ': ' . $value . $this->LE;
}
/**
* Returns a formatted mail line.
* @access private
* @return string
*/
function TextLine($value) {
return $value . $this->LE;
}
/////////////////////////////////////////////////
// CLASS METHODS, ATTACHMENTS
/////////////////////////////////////////////////
/**
* Adds an attachment from a path on the filesystem.
* Returns false if the file could not be found
* or accessed.
* @param string $path Path to the attachment.
* @param string $name Overrides the attachment name.
* @param string $encoding File encoding (see $Encoding).
* @param string $type File extension (MIME) type.
* @return bool
*/
function AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
if(!@is_file($path)) {
$this->SetError($this->Lang('file_access') . $path);
return false;
}
$filename = basename($path);
if($name == '') {
$name = $filename;
}
$cur = count($this->attachment);
$this->attachment[$cur][0] = $path;
$this->attachment[$cur][1] = $filename;
$this->attachment[$cur][2] = $name;
$this->attachment[$cur][3] = $encoding;
$this->attachment[$cur][4] = $type;
$this->attachment[$cur][5] = false; // isStringAttachment
$this->attachment[$cur][6] = 'attachment';
$this->attachment[$cur][7] = 0;
return true;
}
/**
* Attaches all fs, string, and binary attachments to the message.
* Returns an empty string on failure.
* @access private
* @return string
*/
function AttachAll() {
/* Return text of body */
$mime = array();
/* Add all attachments */
for($i = 0; $i < count($this->attachment); $i++) {
/* Check for string attachment */
$bString = $this->attachment[$i][5];
if ($bString) {
$string = $this->attachment[$i][0];
} else {
$path = $this->attachment[$i][0];
}
$filename = $this->attachment[$i][1];
$name = $this->attachment[$i][2];
$encoding = $this->attachment[$i][3];
$type = $this->attachment[$i][4];
$disposition = $this->attachment[$i][6];
$cid = $this->attachment[$i][7];
$mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE);
$mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $name, $this->LE);
$mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
if($disposition == 'inline') {
$mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
}
$mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", $disposition, $name, $this->LE.$this->LE);
/* Encode as string attachment */
if($bString) {
$mime[] = $this->EncodeString($string, $encoding);
if($this->IsError()) {
return '';
}
$mime[] = $this->LE.$this->LE;
} else {
$mime[] = $this->EncodeFile($path, $encoding);
if($this->IsError()) {
return '';
}
$mime[] = $this->LE.$this->LE;
}
}
$mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE);
return join('', $mime);
}
/**
* Encodes attachment in requested format. Returns an
* empty string on failure.
* @access private
* @return string
*/
function EncodeFile ($path, $encoding = 'base64') {
if(!@$fd = fopen($path, 'rb')) {
$this->SetError($this->Lang('file_open') . $path);
return '';
}
$magic_quotes = get_magic_quotes_runtime();
set_magic_quotes_runtime(0);
$file_buffer = fread($fd, filesize($path));
$file_buffer = $this->EncodeString($file_buffer, $encoding);
fclose($fd);
set_magic_quotes_runtime($magic_quotes);
return $file_buffer;
}
/**
* Encodes string to requested format. Returns an
* empty string on failure.
* @access private
* @return string
*/
function EncodeString ($str, $encoding = 'base64') {
$encoded = '';
switch(strtolower($encoding)) {
case 'base64':
/* chunk_split is found in PHP >= 3.0.6 */
$encoded = chunk_split(base64_encode($str), 76, $this->LE);
break;
case '7bit':
case '8bit':
$encoded = $this->FixEOL($str);
if (substr($encoded, -(strlen($this->LE))) != $this->LE)
$encoded .= $this->LE;
break;
case 'binary':
$encoded = $str;
break;
case 'quoted-printable':
$encoded = $this->EncodeQP($str);
break;
default:
$this->SetError($this->Lang('encoding') . $encoding);
break;
}
return $encoded;
}
/**
* Encode a header string to best of Q, B, quoted or none.
* @access private
* @return string
*/
function EncodeHeader ($str, $position = 'text') {
$x = 0;
switch (strtolower($position)) {
case 'phrase':
if (!preg_match('/[\200-\377]/', $str)) {
/* Can't use addslashes as we don't know what value has magic_quotes_sybase. */
$encoded = addcslashes($str, "\0..\37\177\\\"");
if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) {
return ($encoded);
} else {
return ("\"$encoded\"");
}
}
$x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
break;
case 'comment':
$x = preg_match_all('/[()"]/', $str, $matches);
/* Fall-through */
case 'text':
default:
$x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
break;
}
if ($x == 0) {
return ($str);
}
$maxlen = 75 - 7 - strlen($this->CharSet);
/* Try to select the encoding which should produce the shortest output */
if (strlen($str)/3 < $x) {
$encoding = 'B';
if (function_exists('mb_strlen') && $this->HasMultiBytes($str)) {
// Use a custom function which correctly encodes and wraps long
// multibyte strings without breaking lines within a character
$encoded = $this->Base64EncodeWrapMB($str);
} else {
$encoded = base64_encode($str);
$maxlen -= $maxlen % 4;
$encoded = trim(chunk_split($encoded, $maxlen, "\n"));
}
} else {
$encoding = 'Q';
$encoded = $this->EncodeQ($str, $position);
$encoded = $this->WrapText($encoded, $maxlen, true);
$encoded = str_replace('='.$this->LE, "\n", trim($encoded));
}
$encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
$encoded = trim(str_replace("\n", $this->LE, $encoded));
return $encoded;
}
/**
* Checks if a string contains multibyte characters.
* @access private
* @param string $str multi-byte text to wrap encode
* @return bool
*/
function HasMultiBytes($str) {
if (function_exists('mb_strlen')) {
return (strlen($str) > mb_strlen($str, $this->CharSet));
} else { // Assume no multibytes (we can't handle without mbstring functions anyway)
return False;
}
}
/**
* Correctly encodes and wraps long multibyte strings for mail headers
* without breaking lines within a character.
* Adapted from a function by paravoid at http://uk.php.net/manual/en/function.mb-encode-mimeheader.php
* @access private
* @param string $str multi-byte text to wrap encode
* @return string
*/
function Base64EncodeWrapMB($str) {
$start = "=?".$this->CharSet."?B?";
$end = "?=";
$encoded = "";
$mb_length = mb_strlen($str, $this->CharSet);
// Each line must have length <= 75, including $start and $end
$length = 75 - strlen($start) - strlen($end);
// Average multi-byte ratio
$ratio = $mb_length / strlen($str);
// Base64 has a 4:3 ratio
$offset = $avgLength = floor($length * $ratio * .75);
for ($i = 0; $i < $mb_length; $i += $offset) {
$lookBack = 0;
do {
$offset = $avgLength - $lookBack;
$chunk = mb_substr($str, $i, $offset, $this->CharSet);
$chunk = base64_encode($chunk);
$lookBack++;
}
while (strlen($chunk) > $length);
$encoded .= $chunk . $this->LE;
}
// Chomp the last linefeed
$encoded = substr($encoded, 0, -strlen($this->LE));
return $encoded;
}
/**
* Encode string to quoted-printable.
* @access private
* @return string
*/
function EncodeQP( $input = '', $line_max = 76, $space_conv = false ) {
$hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
$lines = preg_split('/(?:\r\n|\r|\n)/', $input);
$eol = "\r\n";
$escape = '=';
$output = '';
while( list(, $line) = each($lines) ) {
$linlen = strlen($line);
$newline = '';
for($i = 0; $i < $linlen; $i++) {
$c = substr( $line, $i, 1 );
$dec = ord( $c );
if ( ( $i == 0 ) && ( $dec == 46 ) ) { // convert first point in the line into =2E
$c = '=2E';
}
if ( $dec == 32 ) {
if ( $i == ( $linlen - 1 ) ) { // convert space at eol only
$c = '=20';
} else if ( $space_conv ) {
$c = '=20';
}
} elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
$h2 = floor($dec/16);
$h1 = floor($dec%16);
$c = $escape.$hex[$h2].$hex[$h1];
}
if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
$output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
$newline = '';
// check if newline first character will be point or not
if ( $dec == 46 ) {
$c = '=2E';
}
}
$newline .= $c;
} // end of for
$output .= $newline.$eol;
} // end of while
return trim($output);
}
/**
* Encode string to q encoding.
* @access private
* @return string
*/
function EncodeQ ($str, $position = 'text') {
/* There should not be any EOL in the string */
$encoded = preg_replace("[\r\n]", '', $str);
switch (strtolower($position)) {
case 'phrase':
$encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
break;
case 'comment':
$encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
case 'text':
default:
/* Replace every high ascii, control =, ? and _ characters */
$encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
"'='.sprintf('%02X', ord('\\1'))", $encoded);
break;
}
/* Replace every spaces to _ (more readable than =20) */
$encoded = str_replace(' ', '_', $encoded);
return $encoded;
}
/**
* Adds a string or binary attachment (non-filesystem) to the list.
* This method can be used to attach ascii or binary data,
* such as a BLOB record from a database.
* @param string $string String attachment data.
* @param string $filename Name of the attachment.
* @param string $encoding File encoding (see $Encoding).
* @param string $type File extension (MIME) type.
* @return void
*/
function AddStringAttachment($string, $filename, $encoding = 'base64', $type = 'application/octet-stream') {
/* Append to $attachment array */
$cur = count($this->attachment);
$this->attachment[$cur][0] = $string;
$this->attachment[$cur][1] = $filename;
$this->attachment[$cur][2] = $filename;
$this->attachment[$cur][3] = $encoding;
$this->attachment[$cur][4] = $type;
$this->attachment[$cur][5] = true; // isString
$this->attachment[$cur][6] = 'attachment';
$this->attachment[$cur][7] = 0;
}
/**
* Adds an embedded attachment. This can include images, sounds, and
* just about any other document. Make sure to set the $type to an
* image type. For JPEG images use "image/jpeg" and for GIF images
* use "image/gif".
* @param string $path Path to the attachment.
* @param string $cid Content ID of the attachment. Use this to identify
* the Id for accessing the image in an HTML form.
* @param string $name Overrides the attachment name.
* @param string $encoding File encoding (see $Encoding).
* @param string $type File extension (MIME) type.
* @return bool
*/
function AddEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
if(!@is_file($path)) {
$this->SetError($this->Lang('file_access') . $path);
return false;
}
$filename = basename($path);
if($name == '') {
$name = $filename;
}
/* Append to $attachment array */
$cur = count($this->attachment);
$this->attachment[$cur][0] = $path;
$this->attachment[$cur][1] = $filename;
$this->attachment[$cur][2] = $name;
$this->attachment[$cur][3] = $encoding;
$this->attachment[$cur][4] = $type;
$this->attachment[$cur][5] = false;
$this->attachment[$cur][6] = 'inline';
$this->attachment[$cur][7] = $cid;
return true;
}
/**
* Returns true if an inline attachment is present.
* @access private
* @return bool
*/
function InlineImageExists() {
$result = false;
for($i = 0; $i < count($this->attachment); $i++) {
if($this->attachment[$i][6] == 'inline') {
$result = true;
break;
}
}
return $result;
}
/////////////////////////////////////////////////
// CLASS METHODS, MESSAGE RESET
/////////////////////////////////////////////////
/**
* Clears all recipients assigned in the TO array. Returns void.
* @return void
*/
function ClearAddresses() {
$this->to = array();
}
/**
* Clears all recipients assigned in the CC array. Returns void.
* @return void
*/
function ClearCCs() {
$this->cc = array();
}
/**
* Clears all recipients assigned in the BCC array. Returns void.
* @return void
*/
function ClearBCCs() {
$this->bcc = array();
}
/**
* Clears all recipients assigned in the ReplyTo array. Returns void.
* @return void
*/
function ClearReplyTos() {
$this->ReplyTo = array();
}
/**
* Clears all recipients assigned in the TO, CC and BCC
* array. Returns void.
* @return void
*/
function ClearAllRecipients() {
$this->to = array();
$this->cc = array();
$this->bcc = array();
}
/**
* Clears all previously set filesystem, string, and binary
* attachments. Returns void.
* @return void
*/
function ClearAttachments() {
$this->attachment = array();
}
/**
* Clears all custom headers. Returns void.
* @return void
*/
function ClearCustomHeaders() {
$this->CustomHeader = array();
}
/////////////////////////////////////////////////
// CLASS METHODS, MISCELLANEOUS
/////////////////////////////////////////////////
/**
* Adds the error message to the error container.
* Returns void.
* @access private
* @return void
*/
function SetError($msg) {
$this->error_count++;
$this->ErrorInfo = $msg;
}
/**
* Returns the proper RFC 822 formatted date.
* @access private
* @return string
*/
function RFCDate() {
$tz = date('Z');
$tzs = ($tz < 0) ? '-' : '+';
$tz = abs($tz);
$tz = (int)($tz/3600)*100 + ($tz%3600)/60;
$result = sprintf("%s %s%04d", date('D, j M Y H:i:s'), $tzs, $tz);
return $result;
}
/**
* Returns the appropriate server variable. Should work with both
* PHP 4.1.0+ as well as older versions. Returns an empty string
* if nothing is found.
* @access private
* @return mixed
*/
function ServerVar($varName) {
global $HTTP_SERVER_VARS;
global $HTTP_ENV_VARS;
if(!isset($_SERVER)) {
$_SERVER = $HTTP_SERVER_VARS;
if(!isset($_SERVER['REMOTE_ADDR'])) {
$_SERVER = $HTTP_ENV_VARS; // must be Apache
}
}
if(isset($_SERVER[$varName])) {
return $_SERVER[$varName];
} else {
return '';
}
}
/**
* Returns the server hostname or 'localhost.localdomain' if unknown.
* @access private
* @return string
*/
function ServerHostname() {
if ($this->Hostname != '') {
$result = $this->Hostname;
} elseif ($this->ServerVar('SERVER_NAME') != '') {
$result = $this->ServerVar('SERVER_NAME');
} else {
$result = 'localhost.localdomain';
}
return $result;
}
/**
* Returns a message in the appropriate language.
* @access private
* @return string
*/
function Lang($key) {
if(count($this->language) < 1) {
$this->SetLanguage('en'); // set the default language
}
if(isset($this->language[$key])) {
return $this->language[$key];
} else {
return 'Language string failed to load: ' . $key;
}
}
/**
* Returns true if an error occurred.
* @return bool
*/
function IsError() {
return ($this->error_count > 0);
}
/**
* Changes every end of line from CR or LF to CRLF.
* @access private
* @return string
*/
function FixEOL($str) {
$str = str_replace("\r\n", "\n", $str);
$str = str_replace("\r", "\n", $str);
$str = str_replace("\n", $this->LE, $str);
return $str;
}
/**
* Adds a custom header.
* @return void
*/
function AddCustomHeader($custom_header) {
$this->CustomHeader[] = explode(':', $custom_header, 2);
}
/**
* Evaluates the message and returns modifications for inline images and backgrounds
* @access public
* @return $message
*/
function MsgHTML($message,$basedir='') {
preg_match_all("/(src|background)=\"(.*)\"/Ui", $message, $images);
if(isset($images[2])) {
foreach($images[2] as $i => $url) {
// do not change urls for absolute images (thanks to corvuscorax)
if (!preg_match('/^[A-z][A-z]*:\/\//',$url)) {
$filename = basename($url);
$directory = dirname($url);
($directory == '.')?$directory='':'';
$cid = 'cid:' . md5($filename);
$fileParts = split("\.", $filename);
$ext = $fileParts[1];
$mimeType = $this->_mime_types($ext);
if ( strlen($basedir) > 1 && substr($basedir,-1) != '/') { $basedir .= '/'; }
if ( strlen($directory) > 1 && substr($basedir,-1) != '/') { $directory .= '/'; }
$this->AddEmbeddedImage($basedir.$directory.$filename, md5($filename), $filename, 'base64', $mimeType);
if ( $this->AddEmbeddedImage($basedir.$directory.$filename, md5($filename), $filename, 'base64',$mimeType) ) {
$message = preg_replace("/".$images[1][$i]."=\"".preg_quote($url, '/')."\"/Ui", $images[1][$i]."=\"".$cid."\"", $message);
}
}
}
}
$this->IsHTML(true);
$this->Body = $message;
$textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s','',$message)));
if ( !empty($textMsg) && empty($this->AltBody) ) {
$this->AltBody = $textMsg;
}
if ( empty($this->AltBody) ) {
$this->AltBody = 'To view this email message, open the email in with HTML compatibility!' . "\n\n";
}
}
/**
* Gets the mime type of the embedded or inline image
* @access private
* @return mime type of ext
*/
function _mime_types($ext = '') {
$mimes = array(
'hqx' => 'application/mac-binhex40',
'cpt' => 'application/mac-compactpro',
'doc' => 'application/msword',
'bin' => 'application/macbinary',
'dms' => 'application/octet-stream',
'lha' => 'application/octet-stream',
'lzh' => 'application/octet-stream',
'exe' => 'application/octet-stream',
'class' => 'application/octet-stream',
'psd' => 'application/octet-stream',
'so' => 'application/octet-stream',
'sea' => 'application/octet-stream',
'dll' => 'application/octet-stream',
'oda' => 'application/oda',
'pdf' => 'application/pdf',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
'smi' => 'application/smil',
'smil' => 'application/smil',
'mif' => 'application/vnd.mif',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'wbxml' => 'application/vnd.wap.wbxml',
'wmlc' => 'application/vnd.wap.wmlc',
'dcr' => 'application/x-director',
'dir' => 'application/x-director',
'dxr' => 'application/x-director',
'dvi' => 'application/x-dvi',
'gtar' => 'application/x-gtar',
'php' => 'application/x-httpd-php',
'php4' => 'application/x-httpd-php',
'php3' => 'application/x-httpd-php',
'phtml' => 'application/x-httpd-php',
'phps' => 'application/x-httpd-php-source',
'js' => 'application/x-javascript',
'swf' => 'application/x-shockwave-flash',
'sit' => 'application/x-stuffit',
'tar' => 'application/x-tar',
'tgz' => 'application/x-tar',
'xhtml' => 'application/xhtml+xml',
'xht' => 'application/xhtml+xml',
'zip' => 'application/zip',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'mpga' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'aif' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
'ram' => 'audio/x-pn-realaudio',
'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin',
'ra' => 'audio/x-realaudio',
'rv' => 'video/vnd.rn-realvideo',
'wav' => 'audio/x-wav',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'png' => 'image/png',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'css' => 'text/css',
'html' => 'text/html',
'htm' => 'text/html',
'shtml' => 'text/html',
'txt' => 'text/plain',
'text' => 'text/plain',
'log' => 'text/plain',
'rtx' => 'text/richtext',
'rtf' => 'text/rtf',
'xml' => 'text/xml',
'xsl' => 'text/xml',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'avi' => 'video/x-msvideo',
'movie' => 'video/x-sgi-movie',
'doc' => 'application/msword',
'word' => 'application/msword',
'xl' => 'application/excel',
'eml' => 'message/rfc822'
);
return ( ! isset($mimes[strtolower($ext)])) ? 'application/octet-stream' : $mimes[strtolower($ext)];
}
/**
* Set (or reset) Class Objects (variables)
*
* Usage Example:
* $page->set('X-Priority', '3');
*
* @access public
* @param string $name Parameter Name
* @param mixed $value Parameter Value
* NOTE: will not work with arrays, there are no arrays to set/reset
*/
function set ( $name, $value = '' ) {
if ( isset($this->$name) ) {
$this->$name = $value;
} else {
$this->SetError('Cannot set or reset variable ' . $name);
return false;
}
}
/**
* Read a file from a supplied filename and return it.
*
* @access public
* @param string $filename Parameter File Name
*/
function getFile($filename) {
$return = '';
if ($fp = fopen($filename, 'rb')) {
while (!feof($fp)) {
$return .= fread($fp, 1024);
}
fclose($fp);
return $return;
} else {
return false;
}
}
/**
* Strips newlines to prevent header injection.
* @access private
* @param string $str String
* @return string
*/
function SecureHeader($str) {
$str = trim($str);
$str = str_replace("\r", "", $str);
$str = str_replace("\n", "", $str);
return $str;
}
/**
* Set the private key file and password to sign the message.
*
* @access public
* @param string $key_filename Parameter File Name
* @param string $key_pass Password for private key
*/
function Sign($key_filename, $key_pass) {
$this->sign_key_file = $key_filename;
$this->sign_key_pass = $key_pass;
}
}
?>
================================================
FILE: PostToQzone/smtp.php
================================================
smtp_conn = 0;
$this->error = null;
$this->helo_rply = null;
$this->do_debug = 0;
}
/*************************************************************
* CONNECTION FUNCTIONS *
***********************************************************/
/**
* Connect to the server specified on the port specified.
* If the port is not specified use the default SMTP_PORT.
* If tval is specified then a connection will try and be
* established with the server for that number of seconds.
* If tval is not specified the default is 30 seconds to
* try on the connection.
*
* SMTP CODE SUCCESS: 220
* SMTP CODE FAILURE: 421
* @access public
* @return bool
*/
function Connect($host,$port=0,$tval=30) {
# set the error val to null so there is no confusion
$this->error = null;
# make sure we are __not__ connected
if($this->connected()) {
# ok we are connected! what should we do?
# for now we will just give an error saying we
# are already connected
$this->error = array("error" => "Already connected to a server");
return false;
}
if(empty($port)) {
$port = $this->SMTP_PORT;
}
#connect to the smtp server
$this->smtp_conn = fsockopen($host, # the host of the server
$port, # the port to use
$errno, # error number if any
$errstr, # error message if any
$tval); # give up after ? secs
# verify we connected properly
if(empty($this->smtp_conn)) {
$this->error = array("error" => "Failed to connect to server",
"errno" => $errno,
"errstr" => $errstr);
if($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $this->error["error"] .
": $errstr ($errno)" . $this->CRLF;
}
return false;
}
# sometimes the SMTP server takes a little longer to respond
# so we will give it a longer timeout for the first read
// Windows still does not have support for this timeout function
if(substr(PHP_OS, 0, 3) != "WIN")
socket_set_timeout($this->smtp_conn, $tval, 0);
# get any announcement stuff
$announce = $this->get_lines();
# set the timeout of any socket functions at 1/10 of a second
//if(function_exists("socket_set_timeout"))
// socket_set_timeout($this->smtp_conn, 0, 100000);
if($this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . $this->CRLF . $announce;
}
return true;
}
/**
* Performs SMTP authentication. Must be run after running the
* Hello() method. Returns true if successfully authenticated.
* @access public
* @return bool
*/
function Authenticate($username, $password) {
// Start authentication
fputs($this->smtp_conn,"AUTH LOGIN" . $this->CRLF);
$rply = $this->get_lines();
$code = substr($rply,0,3);
if($code != 334) {
$this->error =
array("error" => "AUTH not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr($rply,4));
if($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $this->error["error"] .
": " . $rply . $this->CRLF;
}
return false;
}
// Send encoded username
fputs($this->smtp_conn, base64_encode($username) . $this->CRLF);
$rply = $this->get_lines();
$code = substr($rply,0,3);
if($code != 334) {
$this->error =
array("error" => "Username not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr($rply,4));
if($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $this->error["error"] .
": " . $rply . $this->CRLF;
}
return false;
}
// Send encoded password
fputs($this->smtp_conn, base64_encode($password) . $this->CRLF);
$rply = $this->get_lines();
$code = substr($rply,0,3);
if($code != 235) {
$this->error =
array("error" => "Password not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr($rply,4));
if($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $this->error["error"] .
": " . $rply . $this->CRLF;
}
return false;
}
return true;
}
/**
* Returns true if connected to a server otherwise false
* @access private
* @return bool
*/
function Connected() {
if(!empty($this->smtp_conn)) {
$sock_status = socket_get_status($this->smtp_conn);
if($sock_status["eof"]) {
# hmm this is an odd situation... the socket is
# valid but we are not connected anymore
if($this->do_debug >= 1) {
echo "SMTP -> NOTICE:" . $this->CRLF .
"EOF caught while checking if connected";
}
$this->Close();
return false;
}
return true; # everything looks good
}
return false;
}
/**
* Closes the socket and cleans up the state of the class.
* It is not considered good to use this function without
* first trying to use QUIT.
* @access public
* @return void
*/
function Close() {
$this->error = null; # so there is no confusion
$this->helo_rply = null;
if(!empty($this->smtp_conn)) {
# close the connection and cleanup
fclose($this->smtp_conn);
$this->smtp_conn = 0;
}
}
/***************************************************************
* SMTP COMMANDS *
*************************************************************/
/**
* Issues a data command and sends the msg_data to the server
* finializing the mail transaction. $msg_data is the message
* that is to be send with the headers. Each header needs to be
* on a single line followed by a with the message headers
* and the message body being seperated by and additional .
*
* Implements rfc 821: DATA
*
* SMTP CODE INTERMEDIATE: 354
* [data]
* .
* SMTP CODE SUCCESS: 250
* SMTP CODE FAILURE: 552,554,451,452
* SMTP CODE FAILURE: 451,554
* SMTP CODE ERROR : 500,501,503,421
* @access public
* @return bool
*/
function Data($msg_data) {
$this->error = null; # so no confusion is caused
if(!$this->connected()) {
$this->error = array(
"error" => "Called Data() without being connected");
return false;
}
fputs($this->smtp_conn,"DATA" . $this->CRLF);
$rply = $this->get_lines();
$code = substr($rply,0,3);
if($this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
}
if($code != 354) {
$this->error =
array("error" => "DATA command not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr($rply,4));
if($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $this->error["error"] .
": " . $rply . $this->CRLF;
}
return false;
}
# the server is ready to accept data!
# according to rfc 821 we should not send more than 1000
# including the CRLF
# characters on a single line so we will break the data up
# into lines by \r and/or \n then if needed we will break
# each of those into smaller lines to fit within the limit.
# in addition we will be looking for lines that start with
# a period '.' and append and additional period '.' to that
# line. NOTE: this does not count towards are limit.
# normalize the line breaks so we know the explode works
$msg_data = str_replace("\r\n","\n",$msg_data);
$msg_data = str_replace("\r","\n",$msg_data);
$lines = explode("\n",$msg_data);
# we need to find a good way to determine is headers are
# in the msg_data or if it is a straight msg body
# currently I am assuming rfc 822 definitions of msg headers
# and if the first field of the first line (':' sperated)
# does not contain a space then it _should_ be a header
# and we can process all lines before a blank "" line as
# headers.
$field = substr($lines[0],0,strpos($lines[0],":"));
$in_headers = false;
if(!empty($field) && !strstr($field," ")) {
$in_headers = true;
}
$max_line_length = 998; # used below; set here for ease in change
while(list(,$line) = @each($lines)) {
$lines_out = null;
if($line == "" && $in_headers) {
$in_headers = false;
}
# ok we need to break this line up into several
# smaller lines
while(strlen($line) > $max_line_length) {
$pos = strrpos(substr($line,0,$max_line_length)," ");
# Patch to fix DOS attack
if(!$pos) {
$pos = $max_line_length - 1;
}
$lines_out[] = substr($line,0,$pos);
$line = substr($line,$pos + 1);
# if we are processing headers we need to
# add a LWSP-char to the front of the new line
# rfc 822 on long msg headers
if($in_headers) {
$line = "\t" . $line;
}
}
$lines_out[] = $line;
# now send the lines to the server
while(list(,$line_out) = @each($lines_out)) {
if(strlen($line_out) > 0)
{
if(substr($line_out, 0, 1) == ".") {
$line_out = "." . $line_out;
}
}
fputs($this->smtp_conn,$line_out . $this->CRLF);
}
}
# ok all the message data has been sent so lets get this
# over with aleady
fputs($this->smtp_conn, $this->CRLF . "." . $this->CRLF);
$rply = $this->get_lines();
$code = substr($rply,0,3);
if($this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
}
if($code != 250) {
$this->error =
array("error" => "DATA not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr($rply,4));
if($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $this->error["error"] .
": " . $rply . $this->CRLF;
}
return false;
}
return true;
}
/**
* Expand takes the name and asks the server to list all the
* people who are members of the _list_. Expand will return
* back and array of the result or false if an error occurs.
* Each value in the array returned has the format of:
* [ ]
* The definition of is defined in rfc 821
*
* Implements rfc 821: EXPN
*
* SMTP CODE SUCCESS: 250
* SMTP CODE FAILURE: 550
* SMTP CODE ERROR : 500,501,502,504,421
* @access public
* @return string array
*/
function Expand($name) {
$this->error = null; # so no confusion is caused
if(!$this->connected()) {
$this->error = array(
"error" => "Called Expand() without being connected");
return false;
}
fputs($this->smtp_conn,"EXPN " . $name . $this->CRLF);
$rply = $this->get_lines();
$code = substr($rply,0,3);
if($this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
}
if($code != 250) {
$this->error =
array("error" => "EXPN not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr($rply,4));
if($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $this->error["error"] .
": " . $rply . $this->CRLF;
}
return false;
}
# parse the reply and place in our array to return to user
$entries = explode($this->CRLF,$rply);
while(list(,$l) = @each($entries)) {
$list[] = substr($l,4);
}
return $list;
}
/**
* Sends the HELO command to the smtp server.
* This makes sure that we and the server are in
* the same known state.
*
* Implements from rfc 821: HELO
*
* SMTP CODE SUCCESS: 250
* SMTP CODE ERROR : 500, 501, 504, 421
* @access public
* @return bool
*/
function Hello($host="") {
$this->error = null; # so no confusion is caused
if(!$this->connected()) {
$this->error = array(
"error" => "Called Hello() without being connected");
return false;
}
# if a hostname for the HELO was not specified determine
# a suitable one to send
if(empty($host)) {
# we need to determine some sort of appopiate default
# to send to the server
$host = "localhost";
}
// Send extended hello first (RFC 2821)
if(!$this->SendHello("EHLO", $host))
{
if(!$this->SendHello("HELO", $host))
return false;
}
return true;
}
/**
* Sends a HELO/EHLO command.
* @access private
* @return bool
*/
function SendHello($hello, $host) {
fputs($this->smtp_conn, $hello . " " . $host . $this->CRLF);
$rply = $this->get_lines();
$code = substr($rply,0,3);
if($this->do_debug >= 2) {
echo "SMTP -> FROM SERVER: " . $this->CRLF . $rply;
}
if($code != 250) {
$this->error =
array("error" => $hello . " not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr($rply,4));
if($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $this->error["error"] .
": " . $rply . $this->CRLF;
}
return false;
}
$this->helo_rply = $rply;
return true;
}
/**
* Gets help information on the keyword specified. If the keyword
* is not specified then returns generic help, ussually contianing
* A list of keywords that help is available on. This function
* returns the results back to the user. It is up to the user to
* handle the returned data. If an error occurs then false is
* returned with $this->error set appropiately.
*
* Implements rfc 821: HELP [ ]
*
* SMTP CODE SUCCESS: 211,214
* SMTP CODE ERROR : 500,501,502,504,421
* @access public
* @return string
*/
function Help($keyword="") {
$this->error = null; # to avoid confusion
if(!$this->connected()) {
$this->error = array(
"error" => "Called Help() without being connected");
return false;
}
$extra = "";
if(!empty($keyword)) {
$extra = " " . $keyword;
}
fputs($this->smtp_conn,"HELP" . $extra . $this->CRLF);
$rply = $this->get_lines();
$code = substr($rply,0,3);
if($this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
}
if($code != 211 && $code != 214) {
$this->error =
array("error" => "HELP not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr($rply,4));
if($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $this->error["error"] .
": " . $rply . $this->CRLF;
}
return false;
}
return $rply;
}
/**
* Starts a mail transaction from the email address specified in
* $from. Returns true if successful or false otherwise. If True
* the mail transaction is started and then one or more Recipient
* commands may be called followed by a Data command.
*
* Implements rfc 821: MAIL FROM:
*
* SMTP CODE SUCCESS: 250
* SMTP CODE SUCCESS: 552,451,452
* SMTP CODE SUCCESS: 500,501,421
* @access public
* @return bool
*/
function Mail($from) {
$this->error = null; # so no confusion is caused
if(!$this->connected()) {
$this->error = array(
"error" => "Called Mail() without being connected");
return false;
}
$useVerp = ($this->do_verp ? "XVERP" : "");
fputs($this->smtp_conn,"MAIL FROM:<" . $from . ">" . $useVerp . $this->CRLF);
$rply = $this->get_lines();
$code = substr($rply,0,3);
if($this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
}
if($code != 250) {
$this->error =
array("error" => "MAIL not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr($rply,4));
if($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $this->error["error"] .
": " . $rply . $this->CRLF;
}
return false;
}
return true;
}
/**
* Sends the command NOOP to the SMTP server.
*
* Implements from rfc 821: NOOP
*
* SMTP CODE SUCCESS: 250
* SMTP CODE ERROR : 500, 421
* @access public
* @return bool
*/
function Noop() {
$this->error = null; # so no confusion is caused
if(!$this->connected()) {
$this->error = array(
"error" => "Called Noop() without being connected");
return false;
}
fputs($this->smtp_conn,"NOOP" . $this->CRLF);
$rply = $this->get_lines();
$code = substr($rply,0,3);
if($this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
}
if($code != 250) {
$this->error =
array("error" => "NOOP not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr($rply,4));
if($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $this->error["error"] .
": " . $rply . $this->CRLF;
}
return false;
}
return true;
}
/**
* Sends the quit command to the server and then closes the socket
* if there is no error or the $close_on_error argument is true.
*
* Implements from rfc 821: QUIT
*
* SMTP CODE SUCCESS: 221
* SMTP CODE ERROR : 500
* @access public
* @return bool
*/
function Quit($close_on_error=true) {
$this->error = null; # so there is no confusion
if(!$this->connected()) {
$this->error = array(
"error" => "Called Quit() without being connected");
return false;
}
# send the quit command to the server
fputs($this->smtp_conn,"quit" . $this->CRLF);
# get any good-bye messages
$byemsg = $this->get_lines();
if($this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . $this->CRLF . $byemsg;
}
$rval = true;
$e = null;
$code = substr($byemsg,0,3);
if($code != 221) {
# use e as a tmp var cause Close will overwrite $this->error
$e = array("error" => "SMTP server rejected quit command",
"smtp_code" => $code,
"smtp_rply" => substr($byemsg,4));
$rval = false;
if($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $e["error"] . ": " .
$byemsg . $this->CRLF;
}
}
if(empty($e) || $close_on_error) {
$this->Close();
}
return $rval;
}
/**
* Sends the command RCPT to the SMTP server with the TO: argument of $to.
* Returns true if the recipient was accepted false if it was rejected.
*
* Implements from rfc 821: RCPT TO:
*
* SMTP CODE SUCCESS: 250,251
* SMTP CODE FAILURE: 550,551,552,553,450,451,452
* SMTP CODE ERROR : 500,501,503,421
* @access public
* @return bool
*/
function Recipient($to) {
$this->error = null; # so no confusion is caused
if(!$this->connected()) {
$this->error = array(
"error" => "Called Recipient() without being connected");
return false;
}
fputs($this->smtp_conn,"RCPT TO:<" . $to . ">" . $this->CRLF);
$rply = $this->get_lines();
$code = substr($rply,0,3);
if($this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
}
if($code != 250 && $code != 251) {
$this->error =
array("error" => "RCPT not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr($rply,4));
if($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $this->error["error"] .
": " . $rply . $this->CRLF;
}
return false;
}
return true;
}
/**
* Sends the RSET command to abort and transaction that is
* currently in progress. Returns true if successful false
* otherwise.
*
* Implements rfc 821: RSET
*
* SMTP CODE SUCCESS: 250
* SMTP CODE ERROR : 500,501,504,421
* @access public
* @return bool
*/
function Reset() {
$this->error = null; # so no confusion is caused
if(!$this->connected()) {
$this->error = array(
"error" => "Called Reset() without being connected");
return false;
}
fputs($this->smtp_conn,"RSET" . $this->CRLF);
$rply = $this->get_lines();
$code = substr($rply,0,3);
if($this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
}
if($code != 250) {
$this->error =
array("error" => "RSET failed",
"smtp_code" => $code,
"smtp_msg" => substr($rply,4));
if($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $this->error["error"] .
": " . $rply . $this->CRLF;
}
return false;
}
return true;
}
/**
* Starts a mail transaction from the email address specified in
* $from. Returns true if successful or false otherwise. If True
* the mail transaction is started and then one or more Recipient
* commands may be called followed by a Data command. This command
* will send the message to the users terminal if they are logged
* in.
*
* Implements rfc 821: SEND FROM:
*
* SMTP CODE SUCCESS: 250
* SMTP CODE SUCCESS: 552,451,452
* SMTP CODE SUCCESS: 500,501,502,421
* @access public
* @return bool
*/
function Send($from) {
$this->error = null; # so no confusion is caused
if(!$this->connected()) {
$this->error = array(
"error" => "Called Send() without being connected");
return false;
}
fputs($this->smtp_conn,"SEND FROM:" . $from . $this->CRLF);
$rply = $this->get_lines();
$code = substr($rply,0,3);
if($this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
}
if($code != 250) {
$this->error =
array("error" => "SEND not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr($rply,4));
if($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $this->error["error"] .
": " . $rply . $this->CRLF;
}
return false;
}
return true;
}
/**
* Starts a mail transaction from the email address specified in
* $from. Returns true if successful or false otherwise. If True
* the mail transaction is started and then one or more Recipient
* commands may be called followed by a Data command. This command
* will send the message to the users terminal if they are logged
* in and send them an email.
*
* Implements rfc 821: SAML FROM:
*
* SMTP CODE SUCCESS: 250
* SMTP CODE SUCCESS: 552,451,452
* SMTP CODE SUCCESS: 500,501,502,421
* @access public
* @return bool
*/
function SendAndMail($from) {
$this->error = null; # so no confusion is caused
if(!$this->connected()) {
$this->error = array(
"error" => "Called SendAndMail() without being connected");
return false;
}
fputs($this->smtp_conn,"SAML FROM:" . $from . $this->CRLF);
$rply = $this->get_lines();
$code = substr($rply,0,3);
if($this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
}
if($code != 250) {
$this->error =
array("error" => "SAML not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr($rply,4));
if($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $this->error["error"] .
": " . $rply . $this->CRLF;
}
return false;
}
return true;
}
/**
* Starts a mail transaction from the email address specified in
* $from. Returns true if successful or false otherwise. If True
* the mail transaction is started and then one or more Recipient
* commands may be called followed by a Data command. This command
* will send the message to the users terminal if they are logged
* in or mail it to them if they are not.
*
* Implements rfc 821: SOML FROM:
*
* SMTP CODE SUCCESS: 250
* SMTP CODE SUCCESS: 552,451,452
* SMTP CODE SUCCESS: 500,501,502,421
* @access public
* @return bool
*/
function SendOrMail($from) {
$this->error = null; # so no confusion is caused
if(!$this->connected()) {
$this->error = array(
"error" => "Called SendOrMail() without being connected");
return false;
}
fputs($this->smtp_conn,"SOML FROM:" . $from . $this->CRLF);
$rply = $this->get_lines();
$code = substr($rply,0,3);
if($this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
}
if($code != 250) {
$this->error =
array("error" => "SOML not accepted from server",
"smtp_code" => $code,
"smtp_msg" => substr($rply,4));
if($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $this->error["error"] .
": " . $rply . $this->CRLF;
}
return false;
}
return true;
}
/**
* This is an optional command for SMTP that this class does not
* support. This method is here to make the RFC821 Definition
* complete for this class and __may__ be implimented in the future
*
* Implements from rfc 821: TURN
*
* SMTP CODE SUCCESS: 250
* SMTP CODE FAILURE: 502
* SMTP CODE ERROR : 500, 503
* @access public
* @return bool
*/
function Turn() {
$this->error = array("error" => "This method, TURN, of the SMTP ".
"is not implemented");
if($this->do_debug >= 1) {
echo "SMTP -> NOTICE: " . $this->error["error"] . $this->CRLF;
}
return false;
}
/**
* Verifies that the name is recognized by the server.
* Returns false if the name could not be verified otherwise
* the response from the server is returned.
*
* Implements rfc 821: VRFY
*
* SMTP CODE SUCCESS: 250,251
* SMTP CODE FAILURE: 550,551,553
* SMTP CODE ERROR : 500,501,502,421
* @access public
* @return int
*/
function Verify($name) {
$this->error = null; # so no confusion is caused
if(!$this->connected()) {
$this->error = array(
"error" => "Called Verify() without being connected");
return false;
}
fputs($this->smtp_conn,"VRFY " . $name . $this->CRLF);
$rply = $this->get_lines();
$code = substr($rply,0,3);
if($this->do_debug >= 2) {
echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
}
if($code != 250 && $code != 251) {
$this->error =
array("error" => "VRFY failed on name '$name'",
"smtp_code" => $code,
"smtp_msg" => substr($rply,4));
if($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $this->error["error"] .
": " . $rply . $this->CRLF;
}
return false;
}
return $rply;
}
/*******************************************************************
* INTERNAL FUNCTIONS *
******************************************************************/
/**
* Read in as many lines as possible
* either before eof or socket timeout occurs on the operation.
* With SMTP we can tell if we have more lines to read if the
* 4th character is '-' symbol. If it is a space then we don't
* need to read anything else.
* @access private
* @return string
*/
function get_lines() {
$data = "";
while($str = @fgets($this->smtp_conn,515)) {
if($this->do_debug >= 4) {
echo "SMTP -> get_lines(): \$data was \"$data\"" .
$this->CRLF;
echo "SMTP -> get_lines(): \$str is \"$str\"" .
$this->CRLF;
}
$data .= $str;
if($this->do_debug >= 4) {
echo "SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF;
}
# if the 4th character is a space then we are done reading
# so just break the loop
if(substr($str,3,1) == " ") { break; }
}
return $data;
}
}
?>
================================================
FILE: README.md
================================================
plugins
=======
Typecho插件列表
================================================
FILE: SaeUpload/Plugin.php
================================================
Sina App Engine专用的文件上传插件,使用Storage做持久化存储。
*
* @package SaeUpload
* @author Kimi
* @version 1.0.0 Beta
* @link http://www.ccvita.com/491.html
*/
class SaeUpload_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('Widget_Upload')->uploadHandle = array('SaeUpload_Plugin', 'uploadHandle');
Typecho_Plugin::factory('Widget_Upload')->modifyHandle = array('SaeUpload_Plugin', 'modifyHandle');
Typecho_Plugin::factory('Widget_Upload')->deleteHandle = array('SaeUpload_Plugin', 'deleteHandle');
Typecho_Plugin::factory('Widget_Upload')->attachmentHandle = array('SaeUpload_Plugin', 'attachmentHandle');
Typecho_Plugin::factory('Widget_Upload')->attachmentDataHandle = array('SaeUpload_Plugin', 'attachmentDataHandle');
return _t('请您在 Sina App Engine控制面板 中创建Storage的Domain: 名称固定为 typechoupload');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$domainName = new Typecho_Widget_Helper_Form_Element_Text('saestoragedomain', NULL, 'typechoupload',
_t('Domain名称'), _t('请您在 Sina App Engine控制面板 中创建Storage的Domain: 名称固定为 typechoupload'));
$form->addInput($domainName->addRule(array('SaeUpload_Plugin', 'validateDomainName'), _t('Domain名称错误,或者未上传文件!')));
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 验证Sina App Engine Storage中DomainName是否存在
*
* @access public
* @param string $domainName domainName
* @return boolean
*/
public static function validateDomainName($domainName)
{
return true;
/*
$stor = new SaeStorage();
$ret = $stor->getFilesNum($domainName);
if ($ret) {
return true;
} else {
return false;
}
*/
}
/**
* 上传文件处理函数
*
* @access public
* @param array $file 上传的文件
* @return mixed
*/
public static function uploadHandle($file)
{
if (empty($file['name'])) {
return false;
}
$fileName = preg_split("(\/|\\|:)", $file['name']);
$file['name'] = array_pop($fileName);
//获取扩展名
$ext = '';
$part = explode('.', $file['name']);
if (($length = count($part)) > 1) {
$ext = strtolower($part[$length - 1]);
}
if (!self::checkFileType($ext)) {
return false;
}
//获取文件名
$fileName = sprintf('%u', crc32(uniqid())) . '.' . $ext;
$path = $path . '/' . $fileName;//add for mkdir
$stor = new SaeStorage();
$options = Typecho_Widget::widget('Widget_Options');
$SaeStorageDomain = $options->plugin('SaeUpload')->saestoragedomain;
if (isset($file['tmp_name'])) {
//移动上传文件
if (!$path = $stor->upload($SaeStorageDomain,$fileName,$file['tmp_name'])) {
return false;
}
} else if (isset($file['bits'])) {
//直接写入文件
if (!$path = $stor->write($SaeStorageDomain,$fileName,$file['bits'])) {
return false;
}
} else {
return false;
}
if (!isset($file['size'])) {
$attr = $stor->getAttr($SaeStorageDomain,$fileName,array('length'));
$file['size'] = $attr['length'];
}
//返回相对存储路径
return array(
'name' => $file['name'],
'path' => $fileName,
'size' => $file['size'],
'type' => $ext,
'mime' => Typecho_Common::mimeContentType($path)
);
}
/**
* 修改文件处理函数
*
* @access public
* @param array $content 老文件
* @param array $file 新上传的文件
* @return mixed
*/
public static function modifyHandle($content, $file)
{
if (empty($file['name'])) {
return false;
}
$fileName = preg_split("(\/|\\|:)", $file['name']);
$file['name'] = array_pop($fileName);
//获取扩展名
$ext = '';
$part = explode('.', $file['name']);
if (($length = count($part)) > 1) {
$ext = strtolower($part[$length - 1]);
}
if ($content['attachment']->type != $ext) {
return false;
}
//获取文件名
$fileName = $content['attachment']->path;
$path = $path . '/' . $fileName;//add for mkdir
$stor = new SaeStorage();
$options = Typecho_Widget::widget('Widget_Options');
$SaeStorageDomain = $options->plugin('SaeUpload')->saestoragedomain;
if (isset($file['tmp_name'])) {
//移动上传文件
if (!$path = $stor->upload($SaeStorageDomain,$fileName,$file['tmp_name'])) {
return false;
}
} else if (isset($file['bits'])) {
//直接写入文件
if (!$path = $stor->write($SaeStorageDomain,$fileName,$file['bits'])) {
return false;
}
} else {
return false;
}
if (!isset($file['size'])) {
$attr = $stor->getAttr($SaeStorageDomain,$fileName,array('length'));
$file['size'] = $attr['length'];
}
//返回相对存储路径
return array(
'name' => $content['attachment']->name,
'path' => $content['attachment']->path,
'size' => $file['size'],
'type' => $content['attachment']->type,
'mime' => $content['attachment']->mime
);
}
/**
* 删除文件
*
* @access public
* @param array $content 文件相关信息
* @return string
*/
public static function deleteHandle(array $content)
{
$stor = new SaeStorage();
$options = Typecho_Widget::widget('Widget_Options');
$SaeStorageDomain = $options->plugin('SaeUpload')->saestoragedomain;
return $stor->delete($SaeStorageDomain,$content['attachment']->path);
}
/**
* 获取实际文件绝对访问路径
*
* @access public
* @param array $content 文件相关信息
* @return string
*/
public static function attachmentHandle(array $content)
{
$stor = new SaeStorage();
$options = Typecho_Widget::widget('Widget_Options');
$SaeStorageDomain = $options->plugin('SaeUpload')->saestoragedomain;
return $stor->getUrl($SaeStorageDomain,$content['attachment']->path);
}
/**
* 获取实际文件数据
*
* @access public
* @param array $content
* @return string
*/
public static function attachmentDataHandle(array $content)
{
$stor = new SaeStorage();
$options = Typecho_Widget::widget('Widget_Options');
$SaeStorageDomain = $options->plugin('SaeUpload')->saestoragedomain;
return $stor->read($SaeStorageDomain,$content['attachment']->path);
}
/**
* 检查文件名
*
* @access private
* @param string $ext 扩展名
* @return boolean
*/
public static function checkFileType($ext)
{
$options = Typecho_Widget::widget('Widget_Options');
return in_array($ext, $options->allowedAttachmentTypes);
}
}
================================================
FILE: ShareCode/Plugin.php
================================================
content = array('ShareCode_Plugin', 'parse');
Typecho_Plugin::factory('Widget_Abstract_Contents')->excerpt = array('ShareCode_Plugin', 'parse');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form){}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 解析内容
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function parse($value, $lastResult)
{
$value = empty($lastResult) ? $value : $lastResult;
$regex = '/\[embed_snipt:(.*?)]/i';
preg_match_all( $regex, $value, $matches);
$count = count($matches[0]);
for($i = 0;$i < $count;$i++) {
$url = $matches[1][$i];
$url = '';
$value = str_replace($matches[0][$i], $url, $value);
}
return $value;
}
}
================================================
FILE: SimpleCode.php
================================================
contentEx = array('SimpleCode', 'parse');
Typecho_Plugin::factory('Widget_Abstract_Contents')->excerptEx = array('SimpleCode', 'parse');
Typecho_Plugin::factory('Widget_Abstract_Comments')->contentEx = array('SimpleCode', 'parse');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form){}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 解析
*
* @access public
* @param array $matches 解析值
* @return string
*/
public static function parseCallback($matches)
{
return highlight_string(trim($matches[2]), true);
}
/**
* 插件实现方法
*
* @access public
* @return void
*/
public static function parse($text, $widget, $lastResult)
{
$text = empty($lastResult) ? $text : $lastResult;
if ($widget instanceof Widget_Archive || $widget instanceof Widget_Abstract_Comments) {
return preg_replace_callback("/]*)>(.*?)<\/code>/is", array('SimpleCode', 'parseCallback'), $text);
} else {
return $text;
}
}
}
================================================
FILE: Textile2/Plugin.php
================================================
PHP implementation of Brad Choate's Textile 2. It is feature compatible with the MovableType plugin. Does not play well with the Markdown, Textile, or Textile 2 plugins that ship with WordPress. Packaged by Adam Gessaman.
*
* @package Textile 2 (Improved)
* @author Jim Riggs
* @version 2.1.1
* @dependence 9.9.2-*
* @link http://jimandlissa.com/project/textilephp
*/
require('Textile2/Textile.php');
class Textile2_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('Widget_Abstract_Contents')->excerpt = array('Textile2_Plugin', 'parse');
Typecho_Plugin::factory('Widget_Abstract_Contents')->content = array('Textile2_Plugin', 'parse');
Typecho_Plugin::factory('Widget_Abstract_Comments')->content = array('Textile2_Plugin', 'parse');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$version = new Typecho_Widget_Helper_Form_Element_Radio('version',
array('MTTextile' => 'MTTextile - includes Brad Choates\' extensions.',
'Textile' => 'Textile for the Textile purist.'), 'MTTextile',
'Textile Flavor');
$form->addInput($version->multiMode());
$filters = new Typecho_Widget_Helper_Form_Element_Checkbox('filters',
array('SmartyPants' => 'Apply SmartyPants (provides em and en dashes, and other typographic niceities)',
'EducateQuotes' => 'Apply Texturize (applies curly quotes)'),
array('SmartyPants', 'EducateQuotes'), 'Text Filters');
$form->addInput($filters->multiMode());
$headerOffset = new Typecho_Widget_Helper_Form_Element_Select('headerOffset',
array('0 (.h1 = .h1)', '1 (.h1 = .h2)', '2 (.h1 = .h3)', '3 (.h1 = .h4)', '4 (.h1 = .h5)', '5 (.h1 = .h6)'),
0, 'Header Offset');
$form->addInput($headerOffset);
$parsing = new Typecho_Widget_Helper_Form_Element_Checkbox('parsing',
array('ClearLines' => 'Strip extra spaces from the end of each line.',
'PreserveSpaces' => 'Change double-spaces to the HTML entity for an em-space (&8195;).'),
NULL, 'Parsing Options');
$form->addInput($parsing->multiMode());
$inputEncoding = new Typecho_Widget_Helper_Form_Element_Text('inputEncoding', NULL, Helper::options()->charset,
_t('Input Character Encoding'));
$inputEncoding->input->setAttribute('class', 'mini');
$form->addInput($inputEncoding);
$encoding = new Typecho_Widget_Helper_Form_Element_Text('encoding', NULL, Helper::options()->charset,
_t('Output Character Encoding'));
$encoding->input->setAttribute('class', 'mini');
$form->addInput($encoding);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 插件实现方法
*
* @access public
* @return void
*/
public static function parse($text, $widget, $lastResult)
{
$text = empty($lastResult) ? $text : $lastResult;
$settings = Helper::options()->plugin('Textile2');
if ($settings->version == 'Textile') {
$textile = new Textile;
} else {
$textile = new MTLikeTextile;
}
$textile->options['head_offset'] = $settings->headerOffset;
$textile->options['char_encoding'] = $settings->encoding;
$textile->options['input_encoding'] = $settings->inputEncoding;
$textile->options['do_quotes'] = $settings->filters && in_array('EducateQuotes', $settings->filters);
$textile->options['smarty_mode'] = $settings->filters && in_array('SmartyPants', $settings->filters);
$textile->options['trim_spaces'] = $settings->parsing && in_array('ClearLines', $settings->parsing);
$textile->options['preserve_spaces'] = $settings->parsing && in_array('PreserveSpaces', $settings->parsing);
return $textile->process($text);
}
}
================================================
FILE: Textile2/Textile.php
================================================
arrays of replacement blocks
* of text that are temporary removed from the input text to avoid
* processing. Different functions use this replacement
* functionality, and each shifts its own replacement array into
* position 0 and removes it when finished. This avoids having
* several replacement variables and/or functions clobbering
* eachothers' replacement blocks.
*
* @private
*/
var $repl = array();
/**
* The @c array containing temporary strings used in
* replacement callbacks. *JHR*
*
* @private
*/
var $tmp = array();
/**
* Instantiates a new Textile object. Optional options
* can be passed to initialize the object. Attributes for the
* options key are the same as the get/set method names
* documented here.
*
* @param $options The @c array specifying the options to use for
* this object.
*
* @public
*/
function Textile($options = array()) {
$this->options = $options;
$this->options['filters'] = ($this->options['filters'] ? $this->options['filters'] : array());
$this->options['charset'] = ($this->options['charset'] ? $this->options['charset'] : 'iso-8859-1');
$this->options['char_encoding'] = (isset($this->options['char_encoding']) ? $this->options['char_encoding'] : 1);
$this->options['do_quotes'] = (isset($this->options['do_quotes']) ? $this->options['do_quotes'] : 1);
$this->options['trim_spaces'] = (isset($this->options['trim_spaces']) ? $this->options['trim_spaces'] : 0);
$this->options['smarty_mode'] = (isset($this->options['smarty_mode']) ? $this->options['smarty_mode'] : 1);
$this->options['preserve_spaces'] = (isset($this->options['preserve_spaces']) ? $this->options['preserve_spaaces'] : 0);
$this->options['head_offset'] = (isset($this->options['head_offset']) ? $this->options['head_offset'] : 0);
if (is_array($this->options['css'])) {
$this->css($this->options['css']);
}
$this->options['macros'] = ($this->options['macros'] ? $this->options['macros'] : $this->default_macros());
if (isset($this->options['flavor'])) {
$this->flavor($this->options['flavor']);
} else {
$this->flavor('xhtml1/css');
}
$this->_create_re();
} // function Textile
// getter/setter methods...
/**
* Used to set Textile attributes. Attribute names are the same
* as the get/set method names documented here.
*
* @param $opt A @c string specifying the name of the option to
* change or an @c array specifying options and values.
* @param $value The value for the provided option name.
*
* @public
*/
function set($opt, $value = NULL) {
if (is_array($opt)) {
foreach ($opt as $opt => $value) {
$this->set($opt, $value);
}
} else {
// the following options have special set methods
// that activate upon setting:
if ($opt == 'charset') {
$this->charset($value);
} elseif ($opt == 'css') {
$this->css($value);
} elseif ($opt == 'flavor') {
$this->flavor($value);
} else {
$this->options[$opt] = $value;
}
}
} // function set
/**
* Used to get Textile attributes. Attribute names are the same
* as the get/set method names documented here.
*
* @param $opt A @c string specifying the name of the option to get.
*
* @return The value for the provided option.
*
* @public
*/
function get($opt) {
return $this->options[$opt];
} // function get
/**
* Gets or sets the "disable html" control, which allows you to
* prevent HTML tags from being used within the text processed.
* Any HTML tags encountered will be removed if disable html is
* enabled. Default behavior is to allow HTML.
*
* @param $disable_html If provided, a @c bool indicating whether or
* not this object should disable HTML.
*
* @return A true value if this object disables HTML; a false value
* otherwise.
*
* @public
*/
function disable_html($disable_html = NULL) {
if ($disable_html != NULL) {
$this->options['disable_html'] = $disable_html;
}
return ($this->options['disable_html'] ? $this->options['disable_html'] : 0);
} // function disable_html
/**
* Gets or sets the relative heading offset, which allows you to
* change the heading level used within the text processed. For
* example, if the heading offset is '2' and the text contains an
* 'h1' block, an \ block will be output.
*
* @param $head_offset If provided, an @c integer specifying the
* heading offset for this object.
*
* @return An @c integer containing the heading offset for this
* object.
*
* @public
*/
function head_offset($head_offset = NULL) {
if ($head_offset != NULL) {
$this->options['head_offset'] = $head_offset;
}
return ($this->options['head_offset'] ? $this->options['head_offset'] : 0);
} // function head_offset
/**
* Assigns the HTML flavor of output from Textile. Currently
* these are the valid choices: html, xhtml (behaves like "xhtml1"),
* xhtml1, xhtml2. Default flavor is "xhtml1".
*
* Note that the xhtml2 flavor support is experimental and incomplete
* (and will remain that way until the XHTML 2.0 draft becomes a
* proper recommendation).
*
* @param $flavor If provided, a @c string specifying the flavor to
* be used for this object.
*
* @return A @c string containing the flavor for this object.
*
* @public
*/
function flavor($flavor = NULL) {
if ($flavor != NULL) {
$this->options['flavor'] = $flavor;
if (preg_match('/^xhtml(\d)?(\D|$)/', $flavor, $matches)) {
if ($matches[1] == '2') {
$this->options['_line_open'] = '';
$this->options['_line_close'] = ' ';
$this->options['_blockcode_open'] = '';
$this->options['_blockcode_close'] = ' ';
$this->options['css_mode'] = 1;
} else {
// xhtml 1.x
$this->options['_line_open'] = '';
$this->options['_line_close'] = '
';
$this->options['_blockcode_open'] = '';
$this->options['_blockcode_close'] = '
';
$this->options['css_mode'] = 1;
}
} elseif (preg_match('/^html/', $flavor)) {
$this->options['_line_open'] = '';
$this->options['_line_close'] = '
';
$this->options['_blockcode_open'] = '';
$this->options['_blockcode_close'] = '
';
$this->options['css_mode'] = preg_match('/\/css/', $flavor);
}
if ($this->options['css_mode'] && !isset($this->options['css'])) { $this->_css_defaults(); }
}
return $this->options['flavor'];
} // function flavor
/**
* Gets or sets the css support for Textile. If css is enabled,
* Textile will emit CSS rules. You may pass a 1 or 0 to enable
* or disable CSS behavior altogether. If you pass an associative array,
* you may assign the CSS class names that are used by
* Textile. The following key names for such an array are
* recognized:
*
*
* - class_align_right
*
* defaults to 'right'
*
* - class_align_left
*
* defaults to 'left'
*
* - class_align_center
*
* defaults to 'center'
*
* - class_align_top
*
* defaults to 'top'
*
* - class_align_bottom
*
* defaults to 'bottom'
*
* - class_align_middle
*
* defaults to 'middle'
*
* - class_align_justify
*
* defaults to 'justify'
*
* - class_caps
*
* defaults to 'caps'
*
* - class_footnote
*
* defaults to 'footnote'
*
* - id_footnote_prefix
*
* defaults to 'fn'
*
*
*
* @param $css If provided, either a @c bool indicating whether or
* not this object should use css or an associative @c array
* specifying class names to use.
*
* @return Either an associative @c array containing class names
* used by this object, or a true or false value indicating
* whether or not this object uses css.
*
* @public
*/
function css($css = NULL) {
if ($css != NULL) {
if (is_array($css)) {
$this->options['css'] = $css;
$this->options['css_mode'] = 1;
} else {
$this->options['css_mode'] = $css;
if ($this->options['css_mode'] && !isset($this->options['css'])) { $this->_css_defaults(); }
}
}
return ($this->options['css_mode'] ? $this->options['css'] : 0);
} // function css
/**
* Gets or sets the character set targetted for publication.
* At this time, Textile only changes its behavior
* if the 'utf-8' character set is assigned.
*
* Specifically, if utf-8 is requested, any special characters
* created by Textile will be output as native utf-8 characters
* rather than HTML entities.
*
* @param $charset If provided, a @c string specifying the
* characater set to be used for this object.
*
* @return A @c string containing the character set for this object.
*
* @public
*/
function charset($charset = NULL) {
if ($charset != NULL) {
$this->options['charset'] = $charset;
if (preg_match('/^utf-?8$/i', $this->options['charset'])) {
$this->char_encoding(0);
} else {
$this->char_encoding(1);
}
}
return $this->options['charset'];
} // function charset
/**
* Gets or sets the physical file path to root of document files.
* This path is utilized when images are referenced and size
* calculations are needed (the getimagesize() function is used to read
* the image dimensions).
*
* @param $docroot If provided, a @c string specifying the document
* root to use for this object.
*
* @return A @c string containing the docroot for this object.
*
* @public
*/
function docroot($docroot = NULL) {
if ($docroot != NULL) {
$this->options['docroot'] = $docroot;
}
return $this->options['docroot'];
} // function docroot
/**
* Gets or sets the 'trim spaces' control flag. If enabled, this
* will clear any lines that have only spaces on them (the newline
* itself will remain).
*
* @param $trim_spaces If provided, a @c bool indicating whether or
* not this object should trim spaces.
*
* @return A true value if this object trims spaces; a false value
* otherwise.
*
* @public
*/
function trim_spaces($trim_spaces = NULL) {
if ($trim_spaces != NULL) {
$this->options['trim_spaces'] = $trim_spaces;
}
return $this->options['trim_spaces'];
} // function trim_spaces
/**
* Gets or sets a parameter that is passed to filters.
*
* @param $filter_param If provided, a parameter that this object
* should pass to filters.
*
* @return The parameter this object passes to filters.
*
* @public
*/
function filter_param($filter_param = NULL) {
if ($filter_param != NULL) {
$this->options['filter_param'] = $filter_param;
}
return $this->options['filter_param'];
} // function filter_param
/**
* Gets or sets the 'preserve spaces' control flag. If enabled, this
* will replace any double spaces within the paragraph data with the
* \  HTML entity (wide space). The default is 0. Spaces will
* pass through to the browser unchanged and render as a single space.
* Note that this setting has no effect on spaces within \,
* \ blocks or \
";
}
}
================================================
FILE: TinyMCE/tiny_mce/langs/typecho.js
================================================
/** nothing to do, just sleep...Zzz... */
================================================
FILE: TinyMCE/tiny_mce/langs.php
================================================
setContentType('text/javascript');
?>
tinyMCE.addI18n({typecho:{
common:{
edit_confirm:"",
apply:"",
insert:"",
update:"",
cancel:"",
close:"",
browse:"",
class_name:"",
not_set:"",
clipboard_msg:"",
clipboard_no_support:"",
popup_blocked:"",
invalid_data:"",
more_colors:""
},
contextmenu:{
align:"",
left:"",
center:"",
right:"",
full:""
},
insertdatetime:{
date_fmt:"",
time_fmt:"",
insertdate_desc:"",
inserttime_desc:"",
months_long:"",
months_short:"",
day_long:"",
day_short:""
},
print:{
print_desc:""
},
preview:{
preview_desc:""
},
directionality:{
ltr_desc:"",
rtl_desc:""
},
layer:{
insertlayer_desc:"",
forward_desc:"",
backward_desc:"",
absolute_desc:"",
content:""
},
save:{
save_desc:"",
cancel_desc:""
},
nonbreaking:{
nonbreaking_desc:""
},
iespell:{
iespell_desc:"",
download:""
},
advhr:{
advhr_desc:""
},
emotions:{
emotions_desc:""
},
searchreplace:{
search_desc:"",
replace_desc:""
},
advimage:{
image_desc:""
},
advlink:{
link_desc:""
},
xhtmlxtras:{
cite_desc:"",
abbr_desc:"",
acronym_desc:"",
del_desc:"",
ins_desc:"",
attribs_desc:""
},
style:{
desc:""
},
paste:{
paste_text_desc:"",
paste_word_desc:"",
selectall_desc:""
},
paste_dlg:{
text_title:"",
text_linebreaks:"",
word_title:""
},
table:{
desc:"",
row_before_desc:"",
row_after_desc:"",
delete_row_desc:"",
col_before_desc:"",
col_after_desc:"",
delete_col_desc:"",
split_cells_desc:"",
merge_cells_desc:"",
row_desc:"",
cell_desc:"",
props_desc:"",
paste_row_before_desc:"",
paste_row_after_desc:"",
cut_row_desc:"",
copy_row_desc:"",
del:"",
row:"",
col:"",
cell:""
},
autosave:{
unload_msg:""
},
fullscreen:{
desc:""
},
media:{
desc:"",
edit:""
},
fullpage:{
desc:""
},
template:{
desc:""
},
visualchars:{
desc:""
},
spellchecker:{
desc:"",
menu:"",
ignore_word:"",
ignore_words:"",
langs:"",
wait:"",
sug:"",
no_sug:"",
no_mpell:""
},
morebreak:{
desc:""
}}});
tinyMCE.addI18n('typecho.advanced',{
style_select:"",
font_size:"",
fontdefault:"",
block:"",
paragraph:"",
div:"",
address:"",
pre:"",
h1:"",
h2:"",
h3:"",
h4:"",
h5:"",
h6:"",
blockquote:"",
code:"",
samp:"",
dt:"",
dd:"",
bold_desc:"",
italic_desc:"",
underline_desc:"",
striketrough_desc:"",
justifyleft_desc:"",
justifycenter_desc:"",
justifyright_desc:"",
justifyfull_desc:"",
bullist_desc:"",
numlist_desc:"",
outdent_desc:"",
indent_desc:"",
undo_desc:"",
redo_desc:"",
link_desc:"",
unlink_desc:"",
image_desc:"",
cleanup_desc:"",
code_desc:"",
sub_desc:"",
sup_desc:"",
hr_desc:"",
removeformat_desc:"",
custom1_desc:"",
forecolor_desc:"",
backcolor_desc:"",
charmap_desc:"",
visualaid_desc:"",
anchor_desc:"",
cut_desc:"",
copy_desc:"",
paste_desc:"",
image_props_desc:"",
newdocument_desc:"",
help_desc:"",
blockquote_desc:"",
clipboard_msg:"",
path:"",
newdocument:"",
toolbar_focus:"",
more_colors:"",
colorpicker_delta_height: 30,
image_delta_height: 30,
link_delta_height: -20,
link_delta_width: 10
});
tinyMCE.addI18n('typecho.advanced_dlg',{
about_title:"",
about_general:"",
about_help:"",
about_license:"",
about_plugins:"",
about_plugin:"",
about_author:"",
about_version:"",
about_loaded:"",
anchor_title:"",
anchor_name:"",
code_title:"",
code_wordwrap:"",
colorpicker_title:"",
colorpicker_picker_tab:"",
colorpicker_picker_title:"",
colorpicker_palette_tab:"",
colorpicker_palette_title:"",
colorpicker_named_tab:"",
colorpicker_named_title:"",
colorpicker_color:"",
colorpicker_name:"",
charmap_title:"",
image_title:"",
image_src:"",
image_alt:"",
image_list:"",
image_border:"",
image_dimensions:"",
image_vspace:"",
image_hspace:"",
image_align:"",
image_align_baseline:"",
image_align_top:"",
image_align_middle:"",
image_align_bottom:"",
image_align_texttop:"",
image_align_textbottom:"",
image_align_left:"",
image_align_right:"",
link_title:"",
link_url:"",
link_target:"",
link_target_same:"",
link_target_blank:"",
link_titlefield:"",
link_is_email:"",
link_is_external:"",
link_list:""
});
tinyMCE.addI18n('typecho.media_dlg',{
title:"",
general:"",
advanced:"",
file:"",
list:"",
size:"",
preview:"",
constrain_proportions:"",
type:"",
id:"",
name:"",
class_name:"",
vspace:"",
hspace:"",
play:"",
loop:"",
menu:"",
quality:"",
scale:"",
align:"",
salign:"",
wmode:"",
bgcolor:"",
base:"",
flashvars:"",
liveconnect:"",
autohref:"",
cache:"",
hidden:"",
controller:"",
kioskmode:"",
playeveryframe:"",
targetcache:"",
correction:"",
enablejavascript:"",
starttime:"",
endtime:"",
href:"",
qtsrcchokespeed:"",
target:"",
volume:"",
autostart:"",
enabled:"",
fullscreen:"",
invokeurls:"",
mute:"",
stretchtofit:"",
windowlessvideo:"",
balance:"",
baseurl:"",
captioningid:"",
currentmarker:"",
currentposition:"",
defaultframe:"",
playcount:"",
rate:"",
uimode:"",
flash_options:"",
qt_options:"",
wmp_options:"",
rmp_options:"",
shockwave_options:"",
autogotourl:"",
center:"",
imagestatus:"",
maintainaspect:"",
nojava:"",
prefetch:"",
shuffle:"",
console:"",
numloop:"",
controls:"",
scriptcallbacks:"",
swstretchstyle:"",
swstretchhalign:"",
swstretchvalign:"",
sound:"",
progress:"",
qtsrc:"",
qt_stream_warn:"",
align_top:"",
align_right:"",
align_bottom:"",
align_left:"",
align_center:"",
align_top_left:"",
align_top_right:"",
align_bottom_left:"",
align_bottom_right:"",
flv_options:"",
flv_scalemode:"",
flv_buffer:"",
flv_startimage:"",
flv_starttime:"",
flv_defaultvolume:"",
flv_hiddengui:"",
flv_autostart:"",
flv_loop:"",
flv_showscalemodes:"",
flv_smoothvideo:"",
flv_jscallback:""
});
/** offset */
tinyMCE.addI18n('typecho.media',{
delta_height:40
});
================================================
FILE: TinyMCE/tiny_mce/plugins/coder/editor_plugin.js
================================================
/**
* $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
*
* @author Moxiecode
* @copyright Copyright 2004-2008, Moxiecode Systems AB, All rights reserved.
*/
(function() {
tinymce.create('tinymce.plugins.CoderPlugin', {
init : function(ed, url) {
ed.onClick.add(function(ed, e) {
e = e.target;
if (e.nodeName === 'CODE' || e.nodeName === 'PRE' || e.className.indexOf("typecho-plugin") >= 0)
ed.selection.select(e);
});
ed.onBeforeSetContent.add(function(ed, o) {
var _replace = function (g, a, b, c) {
c = c.trim().replace(/( |<|>|\r\n|\r|\n)/g, function (e) {
switch (e) {
case "<":
return "<";
case ">":
return ">";
case "\r\n":
case "\r":
case "\n":
return '
';
case " ":
return ' ';
default:
return;
}
});
return '<' + a + b + '>' + c + '' + a + '>';
};
o.content = o.content.replace(/<(code)([^>]*)>([\s\S]*?)<\/(code)>/ig, _replace);
o.content = o.content.replace(/<(pre)([^>]*)>([\s\S]*?)<\/(pre)>/ig, _replace);
});
/*
ed.onPostProcess.add(function(ed, o) {
if (o.get) {
o.content = o.content.replace(/