2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Library of conversion functions, implemented as static methods.
|
|
|
|
*
|
|
|
|
* The methods are all of the form (format)2(format), where the format is one of
|
|
|
|
*
|
|
|
|
* raw: A UTF8 string
|
|
|
|
* attr: A UTF8 string suitable for inclusion in an HTML attribute
|
|
|
|
* js: A UTF8 string suitable for inclusion in a double-quoted javascript string.
|
|
|
|
*
|
|
|
|
* array: A PHP associative array
|
|
|
|
* json: JavaScript object notation
|
|
|
|
*
|
|
|
|
* html: HTML source suitable for use in a page or email
|
|
|
|
* text: Plain-text content, suitable for display to a user as-is, or insertion in a plaintext email.
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
|
|
|
* Objects of type {@link ViewableData} can have an "escaping type",
|
|
|
|
* which determines if they are automatically escaped before output by {@link SSViewer}.
|
|
|
|
*
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage misc
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class Convert extends Object {
|
|
|
|
|
2009-05-20 05:09:50 +02:00
|
|
|
/**
|
|
|
|
* Convert a value to be suitable for an XML attribute.
|
|
|
|
*
|
|
|
|
* @param array|string $val String to escape, or array of strings
|
|
|
|
* @return array|string
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function raw2att($val) {
|
|
|
|
if(is_array($val)) {
|
|
|
|
foreach($val as $k => $v) $val[$k] = self::raw2att($v);
|
|
|
|
return $val;
|
|
|
|
} else {
|
2009-07-02 00:27:18 +02:00
|
|
|
return str_replace(array('&','"',"'",'<','>'), array('&','"',''','<','>'), $val);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-05-20 05:09:50 +02:00
|
|
|
* Convert a value to be suitable for an HTML attribute.
|
|
|
|
*
|
|
|
|
* This is useful for converting human readable values into
|
|
|
|
* a value suitable for an ID or NAME attribute.
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @see http://www.w3.org/TR/REC-html40/types.html#type-cdata
|
2009-05-20 05:09:50 +02:00
|
|
|
* @uses Convert::raw2att()
|
|
|
|
* @param array|string $val String to escape, or array of strings
|
|
|
|
* @return array|string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
static function raw2htmlatt($val) {
|
|
|
|
if(is_array($val)) {
|
2009-05-20 05:09:50 +02:00
|
|
|
foreach($val as $k => $v) $val[$k] = self::raw2htmlatt($v);
|
2007-07-19 12:40:28 +02:00
|
|
|
return $val;
|
|
|
|
} else {
|
2009-05-20 05:09:50 +02:00
|
|
|
$val = self::raw2att($val);
|
|
|
|
$val = preg_replace('/[^a-zA-Z0-9\-_]*/', '', $val);
|
2007-07-19 12:40:28 +02:00
|
|
|
return $val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-05 07:19:48 +01:00
|
|
|
/**
|
|
|
|
* Ensure that text is properly escaped for XML.
|
2009-05-20 05:09:50 +02:00
|
|
|
*
|
|
|
|
* @see http://www.w3.org/TR/REC-xml/#dt-escape
|
2009-01-05 07:19:48 +01:00
|
|
|
* @param array|string $val String to escape, or array of strings
|
|
|
|
* @return array|string
|
|
|
|
*/
|
|
|
|
static function raw2xml($val) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(is_array($val)) {
|
|
|
|
foreach($val as $k => $v) $val[$k] = self::raw2xml($v);
|
|
|
|
return $val;
|
|
|
|
} else {
|
2009-07-02 00:27:18 +02:00
|
|
|
return str_replace(array('&','<','>',"\n",'"',"'"), array('&','<','>','<br />','"','''), $val);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2009-01-05 07:19:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that text is properly escaped for Javascript.
|
|
|
|
*
|
|
|
|
* @param array|string $val String to escape, or array of strings
|
|
|
|
* @return array|string
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function raw2js($val) {
|
|
|
|
if(is_array($val)) {
|
|
|
|
foreach($val as $k => $v) $val[$k] = self::raw2js($v);
|
|
|
|
return $val;
|
|
|
|
} else {
|
2009-01-05 07:19:48 +01:00
|
|
|
return str_replace(array("\\", '"', "\n", "\r", "'"), array("\\\\", '\"', '\n', '\r', "\\'"), $val);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-09 08:46:48 +02:00
|
|
|
/**
|
2009-05-20 05:09:50 +02:00
|
|
|
* Uses the PHP 5.2 native json_encode function if available,
|
2008-08-09 08:46:48 +02:00
|
|
|
* otherwise falls back to the Services_JSON class.
|
|
|
|
*
|
|
|
|
* @see http://pear.php.net/pepr/pepr-proposal-show.php?id=198
|
2008-10-16 13:49:51 +02:00
|
|
|
* @uses Director::baseFolder()
|
|
|
|
* @uses Services_JSON
|
2008-08-09 08:46:48 +02:00
|
|
|
*
|
|
|
|
* @param mixed $val
|
|
|
|
* @return string JSON safe string
|
|
|
|
*/
|
|
|
|
static function raw2json($val) {
|
2008-08-09 09:03:24 +02:00
|
|
|
if(function_exists('json_encode')) {
|
2008-08-09 08:46:48 +02:00
|
|
|
return json_encode($val);
|
|
|
|
} else {
|
2010-03-04 00:05:39 +01:00
|
|
|
require_once(Director::baseFolder() . '/sapphire/thirdparty/json/JSON.php');
|
2008-08-09 08:46:48 +02:00
|
|
|
$json = new Services_JSON();
|
|
|
|
return $json->encode($val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static function raw2sql($val) {
|
|
|
|
if(is_array($val)) {
|
|
|
|
foreach($val as $k => $v) $val[$k] = self::raw2sql($v);
|
|
|
|
return $val;
|
|
|
|
} else {
|
2009-03-11 22:44:10 +01:00
|
|
|
return DB::getConn()->addslashes($val);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-05-20 05:09:50 +02:00
|
|
|
* Convert XML to raw text.
|
2008-10-16 13:49:51 +02:00
|
|
|
* @uses html2raw()
|
2007-07-19 12:40:28 +02:00
|
|
|
* @todo Currently &#xxx; entries are stripped; they should be converted
|
|
|
|
*/
|
|
|
|
static function xml2raw($val) {
|
|
|
|
if(is_array($val)) {
|
|
|
|
foreach($val as $k => $v) $val[$k] = self::xml2raw($v);
|
|
|
|
return $val;
|
|
|
|
} else {
|
2009-05-20 05:09:50 +02:00
|
|
|
// More complex text needs to use html2raw instead
|
2007-07-19 12:40:28 +02:00
|
|
|
if(strpos($val,'<') !== false) return self::html2raw($val);
|
|
|
|
|
2009-07-02 00:27:18 +02:00
|
|
|
$converted = str_replace(array('&','<','>','"',''', '''), array('&','<','>','"',"'", "'"), $val);
|
2009-05-20 05:09:50 +02:00
|
|
|
$converted = ereg_replace('&#[0-9]+;', '', $converted);
|
|
|
|
return $converted;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2008-10-16 13:58:31 +02:00
|
|
|
|
2010-03-04 00:05:39 +01:00
|
|
|
/**
|
|
|
|
* Uses the PHP 5.2 native json_encode function if available,
|
|
|
|
* otherwise falls back to the Services_JSON class.
|
|
|
|
*
|
2010-03-04 00:10:30 +01:00
|
|
|
* @see http://pear.php.net/pepr/pepr-proposal-show.php?id=198
|
|
|
|
* @uses Director::baseFolder()
|
|
|
|
* @uses Services_JSON
|
|
|
|
*
|
2010-03-04 00:05:39 +01:00
|
|
|
* @param array $val Array to convert
|
|
|
|
* @return string JSON encoded string
|
|
|
|
*/
|
|
|
|
static function array2json($val) {
|
|
|
|
if(function_exists('json_encode')) {
|
|
|
|
return json_encode($val);
|
|
|
|
} else {
|
|
|
|
require_once(Director::baseFolder() . '/sapphire/thirdparty/json/JSON.php');
|
|
|
|
$json = new Services_JSON();
|
|
|
|
return $json->encode($val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-09 09:03:24 +02:00
|
|
|
/**
|
2009-05-20 05:09:50 +02:00
|
|
|
* Uses the PHP 5.2 native json_decode function if available,
|
2008-08-09 09:03:24 +02:00
|
|
|
* otherwise falls back to the Services_JSON class.
|
|
|
|
*
|
|
|
|
* @see http://pear.php.net/pepr/pepr-proposal-show.php?id=198
|
2010-03-04 00:10:30 +01:00
|
|
|
* @uses Director::baseFolder()
|
|
|
|
* @uses Services_JSON
|
2008-08-09 09:03:24 +02:00
|
|
|
*
|
|
|
|
* @param string $val
|
|
|
|
* @return mixed JSON safe string
|
|
|
|
*/
|
|
|
|
static function json2obj($val) {
|
2010-03-04 00:05:39 +01:00
|
|
|
if(function_exists('json_decode')) {
|
|
|
|
return json_decode($val);
|
|
|
|
} else {
|
|
|
|
require_once(Director::baseFolder() . '/sapphire/thirdparty/json/JSON.php');
|
|
|
|
$json = new Services_JSON();
|
|
|
|
return $json->decode($val);
|
|
|
|
}
|
2008-08-09 09:03:24 +02:00
|
|
|
}
|
|
|
|
|
2008-10-16 13:49:51 +02:00
|
|
|
/**
|
2009-05-21 00:52:33 +02:00
|
|
|
* Convert a JSON string into an array.
|
|
|
|
*
|
2008-10-16 13:49:51 +02:00
|
|
|
* @uses json2obj
|
2009-05-21 00:52:33 +02:00
|
|
|
* @param string $val JSON string to convert
|
|
|
|
* @return array|boolean
|
2008-10-16 13:49:51 +02:00
|
|
|
*/
|
2008-08-09 09:03:24 +02:00
|
|
|
static function json2array($val) {
|
|
|
|
$json = self::json2obj($val);
|
2009-05-21 00:52:33 +02:00
|
|
|
if(!$json) return false;
|
|
|
|
|
2008-08-09 09:03:24 +02:00
|
|
|
$arr = array();
|
|
|
|
foreach($json as $k => $v) {
|
|
|
|
$arr[$k] = $v;
|
|
|
|
}
|
2009-05-21 00:52:33 +02:00
|
|
|
|
2008-08-09 09:03:24 +02:00
|
|
|
return $arr;
|
|
|
|
}
|
|
|
|
|
2008-10-16 13:49:51 +02:00
|
|
|
/**
|
|
|
|
* @uses recursiveXMLToArray()
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function xml2array($val) {
|
2008-08-09 09:03:24 +02:00
|
|
|
$xml = new SimpleXMLElement($val);
|
2008-08-25 01:39:32 +02:00
|
|
|
return self::recursiveXMLToArray($xml);
|
|
|
|
}
|
2009-05-20 06:18:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function recursively run from {@link Convert::xml2array()}
|
|
|
|
* @uses SimpleXMLElement
|
|
|
|
*/
|
2008-10-16 13:49:51 +02:00
|
|
|
protected static function recursiveXMLToArray($xml) {
|
2009-07-16 05:44:15 +02:00
|
|
|
if(is_object($xml) && get_class($xml) == 'SimpleXMLElement') {
|
2009-05-20 06:18:28 +02:00
|
|
|
$attributes = $xml->attributes();
|
|
|
|
foreach($attributes as $k => $v) {
|
|
|
|
if($v) $a[$k] = (string) $v;
|
|
|
|
}
|
|
|
|
$x = $xml;
|
|
|
|
$xml = get_object_vars($xml);
|
|
|
|
}
|
|
|
|
if(is_array($xml)) {
|
|
|
|
if(count($xml) == 0) return (string) $x; // for CDATA
|
|
|
|
foreach($xml as $key => $value) {
|
|
|
|
$r[$key] = self::recursiveXMLToArray($value);
|
|
|
|
}
|
|
|
|
if(isset($a)) $r['@'] = $a; // Attributes
|
|
|
|
return $r;
|
|
|
|
}
|
|
|
|
return (string) $xml;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a link if the string is a valid URL
|
|
|
|
* @param string The string to linkify
|
|
|
|
* @return A link to the URL if string is a URL
|
|
|
|
*/
|
|
|
|
static function linkIfMatch($string) {
|
|
|
|
if( preg_match( '/^[a-z+]+\:\/\/[a-zA-Z0-9$-_.+?&=!*\'()%]+$/', $string ) )
|
|
|
|
return "<a style=\"white-space: nowrap\" href=\"$string\">$string</a>";
|
|
|
|
else
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple conversion of HTML to plaintext.
|
|
|
|
*
|
|
|
|
* @param $data string
|
|
|
|
* @param $preserveLinks boolean
|
|
|
|
* @param $wordwrap array
|
|
|
|
*/
|
|
|
|
static function html2raw($data, $preserveLinks = false, $wordWrap = 60, $config = null) {
|
|
|
|
$defaultConfig = array(
|
|
|
|
'PreserveLinks' => false,
|
|
|
|
'ReplaceBoldAsterisk' => true,
|
|
|
|
'CompressWhitespace' => true,
|
|
|
|
'ReplaceImagesWithAlt' => true,
|
|
|
|
);
|
|
|
|
if(isset($config)) {
|
|
|
|
$config = array_merge($defaultConfig,$config);
|
|
|
|
} else {
|
|
|
|
$config = $defaultConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sTRIp style and script
|
|
|
|
/* $data = eregi_replace("<style(^A-Za-z0-9>][^>]*)?>.*</style[^>]*>","", $data);*/
|
|
|
|
/* $data = eregi_replace("<script(^A-Za-z0-9>][^>]*)?>.*</script[^>]*>","", $data);*/
|
|
|
|
|
|
|
|
$data = preg_replace("/<style(^A-Za-z0-9>][^>]*)?>.*?<\/style[^>]*>/i","", $data);
|
|
|
|
$data = preg_replace("/<script(^A-Za-z0-9>][^>]*)?>.*?<\/script[^>]*>/i","", $data);
|
|
|
|
// TODO Deal with attributes inside tags
|
|
|
|
if($config['ReplaceBoldAsterisk']) {
|
|
|
|
$data = str_ireplace(
|
|
|
|
array('<strong>','</strong>','<b>','</b>'),
|
|
|
|
'*',
|
|
|
|
$data
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// Expand hyperlinks
|
2009-06-08 02:11:22 +02:00
|
|
|
if(!$preserveLinks && !$config['PreserveLinks']) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$data = preg_replace('/<a[^>]*href\s*=\s*"([^"]*)">(.*?)<\/a>/ie', "Convert::html2raw('\\2').'[\\1]'", $data);
|
|
|
|
$data = preg_replace('/<a[^>]*href\s*=\s*([^ ]*)>(.*?)<\/a>/ie', "Convert::html2raw('\\2').'[\\1]'", $data);
|
|
|
|
|
|
|
|
/* $data = eregi_replace('<a[^>]*href *= *"([^"]*)">([^<>]*)</a>', '\\2 [\\1]', $data); */
|
|
|
|
/* $data = eregi_replace('<a[^>]*href *= *([^ ]*)>([^<>]*)</a>', '\\2 [\\1]', $data); */
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace images with their alt tags
|
|
|
|
if($config['ReplaceImagesWithAlt']) {
|
|
|
|
$data = eregi_replace('<img[^>]*alt *= *"([^"]*)"[^>]*>', ' \\1 ', $data);
|
|
|
|
$data = eregi_replace('<img[^>]*alt *= *([^ ]*)[^>]*>', ' \\1 ', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compress whitespace
|
|
|
|
if($config['CompressWhitespace']) {
|
|
|
|
$data = ereg_replace("[\n\r\t ]+", " ", $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse newline tags
|
|
|
|
$data = ereg_replace("[ \n\r\t]*<[Hh][1-6]([^A-Za-z0-9>][^>]*)?> *", "\n\n", $data);
|
|
|
|
$data = ereg_replace("[ \n\r\t]*<[Pp]([^A-Za-z0-9>][^>]*)?> *", "\n\n", $data);
|
|
|
|
$data = ereg_replace("[ \n\r\t]*<[Dd][Ii][Vv]([^A-Za-z0-9>][^>]*)?> *", "\n\n", $data);
|
|
|
|
$data = ereg_replace("\n\n\n+","\n\n", $data);
|
|
|
|
|
|
|
|
$data = ereg_replace("<[Bb][Rr]([^A-Za-z0-9>][^>]*)?> *", "\n", $data);
|
|
|
|
$data = ereg_replace("<[Tt][Rr]([^A-Za-z0-9>][^>]*)?> *", "\n", $data);
|
|
|
|
$data = ereg_replace("</[Tt][Dd]([^A-Za-z0-9>][^>]*)?> *", " ", $data);
|
|
|
|
$data = preg_replace('/<\/p>/i', "\n\n", $data );
|
|
|
|
|
|
|
|
|
|
|
|
// Replace HTML entities
|
2008-10-25 08:05:27 +02:00
|
|
|
//$data = preg_replace("/&#([0-9]+);/e", 'chr(\1)', $data);
|
|
|
|
//$data = str_replace(array("<",">","&"," "), array("<", ">", "&", " "), $data);
|
|
|
|
$data = html_entity_decode($data, ENT_COMPAT , 'UTF-8');
|
2007-07-19 12:40:28 +02:00
|
|
|
// Remove all tags (but optionally keep links)
|
|
|
|
|
|
|
|
// strip_tags seemed to be restricting the length of the output
|
|
|
|
// arbitrarily. This essentially does the same thing.
|
2009-06-08 02:11:22 +02:00
|
|
|
if(!$preserveLinks && !$config['PreserveLinks']) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$data = preg_replace('/<\/?[^>]*>/','', $data);
|
|
|
|
} else {
|
|
|
|
$data = strip_tags($data, '<a>');
|
|
|
|
}
|
|
|
|
return trim(wordwrap(trim($data), $wordWrap));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* There are no real specifications on correctly encoding mailto-links,
|
|
|
|
* but this seems to be compatible with most of the user-agents.
|
|
|
|
* Does nearly the same as rawurlencode().
|
|
|
|
* Please only encode the values, not the whole url, e.g.
|
|
|
|
* "mailto:test@test.com?subject=" . Convert::raw2mailto($subject)
|
|
|
|
*
|
|
|
|
* @param $data string
|
|
|
|
* @return string
|
|
|
|
* @see http://www.ietf.org/rfc/rfc1738.txt
|
|
|
|
*/
|
|
|
|
static function raw2mailto($data) {
|
|
|
|
return str_ireplace(
|
|
|
|
array("\n",'?','=',' ','(',')','&','@','"','\'',';'),
|
|
|
|
array('%0A','%3F','%3D','%20','%28','%29','%26','%40','%22','%27','%3B'),
|
|
|
|
$data
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-03-04 00:05:39 +01:00
|
|
|
}
|