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
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* 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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* 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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* Objects of type {@link ViewableData} can have an "escaping type",
|
2014-08-15 08:53:05 +02:00
|
|
|
* which determines if they are automatically escaped before output by {@link SSViewer}.
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage misc
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2010-05-25 06:19:38 +02:00
|
|
|
class Convert {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-20 05:09:50 +02:00
|
|
|
/**
|
|
|
|
* Convert a value to be suitable for an XML attribute.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-05-20 05:09:50 +02:00
|
|
|
* @param array|string $val String to escape, or array of strings
|
|
|
|
* @return array|string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function raw2att($val) {
|
2010-12-16 23:55:17 +01:00
|
|
|
return self::raw2xml($val);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2010-12-16 23:55:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a value to be suitable for an HTML attribute.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-12-16 23:55:17 +01:00
|
|
|
* @param string|array $val String to escape, or array of strings
|
|
|
|
* @return array|string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function raw2htmlatt($val) {
|
2010-12-16 23:55:17 +01:00
|
|
|
return self::raw2att($val);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-05-26 01:01:46 +02:00
|
|
|
* Convert a value to be suitable for an HTML ID attribute. Replaces non
|
|
|
|
* supported characters with a space.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @see http://www.w3.org/TR/REC-html40/types.html#type-cdata
|
2013-05-26 01:01:46 +02:00
|
|
|
*
|
2009-05-20 05:09:50 +02:00
|
|
|
* @param array|string $val String to escape, or array of strings
|
2013-05-26 01:01:46 +02:00
|
|
|
*
|
2009-05-20 05:09:50 +02:00
|
|
|
* @return array|string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function raw2htmlname($val) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(is_array($val)) {
|
2013-05-26 01:01:46 +02:00
|
|
|
foreach($val as $k => $v) {
|
|
|
|
$val[$k] = self::raw2htmlname($v);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $val;
|
|
|
|
} else {
|
|
|
|
return self::raw2att($val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a value to be suitable for an HTML ID attribute. Replaces non
|
|
|
|
* supported characters with an underscore.
|
|
|
|
*
|
|
|
|
* @see http://www.w3.org/TR/REC-html40/types.html#type-cdata
|
|
|
|
*
|
|
|
|
* @param array|string $val String to escape, or array of strings
|
|
|
|
*
|
|
|
|
* @return array|string
|
|
|
|
*/
|
|
|
|
public static function raw2htmlid($val) {
|
|
|
|
if(is_array($val)) {
|
|
|
|
foreach($val as $k => $v) {
|
|
|
|
$val[$k] = self::raw2htmlid($v);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $val;
|
|
|
|
} else {
|
2013-05-26 01:01:46 +02:00
|
|
|
return trim(preg_replace(
|
|
|
|
'/_+/', '_', preg_replace('/[^a-zA-Z0-9\-_:.]+/','_', $val)),
|
|
|
|
'_'
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-01-05 07:19:48 +01:00
|
|
|
/**
|
|
|
|
* Ensure that text is properly escaped for XML.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
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
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public 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 {
|
2010-12-16 23:55:17 +01:00
|
|
|
return htmlspecialchars($val, ENT_QUOTES, 'UTF-8');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +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
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function raw2js($val) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(is_array($val)) {
|
|
|
|
foreach($val as $k => $v) $val[$k] = self::raw2js($v);
|
|
|
|
return $val;
|
|
|
|
} else {
|
2014-04-02 06:19:14 +02:00
|
|
|
return str_replace(
|
|
|
|
// Intercepts some characters such as <, >, and & which can interfere
|
|
|
|
array("\\", '"', "\n", "\r", "'", "<", ">", "&"),
|
|
|
|
array("\\\\", '\"', '\n', '\r', "\\'", "\\x3c", "\\x3e", "\\x26"),
|
|
|
|
$val
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2012-03-30 05:18:14 +02:00
|
|
|
|
2008-08-09 08:46:48 +02:00
|
|
|
/**
|
2017-01-23 20:59:08 +01:00
|
|
|
* Encode a value as a JSON encoded string. You can optionally pass a bitmask of
|
|
|
|
* JSON constants as options through to the encode function.
|
2008-08-09 08:46:48 +02:00
|
|
|
*
|
2017-01-23 20:59:08 +01:00
|
|
|
* @param mixed $val Value to be encoded
|
|
|
|
* @param int $options Optional bitmask of JSON constants
|
|
|
|
* @return string JSON encoded string
|
2008-08-09 08:46:48 +02:00
|
|
|
*/
|
2017-01-23 20:59:08 +01:00
|
|
|
public static function raw2json($val, $options = 0) {
|
|
|
|
return json_encode($val, $options);
|
2008-08-09 08:46:48 +02:00
|
|
|
}
|
2012-03-30 05:18:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode an array as a JSON encoded string.
|
2017-01-23 20:59:08 +01:00
|
|
|
* This is an alias to {@link raw2json()}
|
2012-03-30 05:18:14 +02:00
|
|
|
*
|
2017-01-23 20:59:08 +01:00
|
|
|
* @param array $val Array to convert
|
|
|
|
* @param int $options Optional bitmask of JSON constants
|
|
|
|
* @return string JSON encoded string
|
2012-03-30 05:18:14 +02:00
|
|
|
*/
|
2017-01-23 20:59:08 +01:00
|
|
|
public static function array2json($val, $options = 0) {
|
|
|
|
return self::raw2json($val, $options);
|
2012-03-30 05:18:14 +02:00
|
|
|
}
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Safely encodes a value (or list of values) using the current database's
|
|
|
|
* safe string encoding method
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param mixed|array $val Input value, or list of values as an array
|
|
|
|
* @param boolean $quoted Flag indicating whether the value should be safely
|
2014-08-15 08:53:05 +02:00
|
|
|
* quoted, instead of only being escaped. By default this function will
|
2013-06-21 00:32:08 +02:00
|
|
|
* only escape the string (false).
|
|
|
|
* @return string|array Safely encoded value in the same format as the input
|
|
|
|
*/
|
|
|
|
public static function raw2sql($val, $quoted = false) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(is_array($val)) {
|
2013-06-21 00:32:08 +02:00
|
|
|
foreach($val as $k => $v) {
|
|
|
|
$val[$k] = self::raw2sql($v, $quoted);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
return $val;
|
|
|
|
} else {
|
2013-06-21 00:32:08 +02:00
|
|
|
if($quoted) {
|
|
|
|
return DB::get_conn()->quoteString($val);
|
|
|
|
} else {
|
|
|
|
return DB::get_conn()->escapeString($val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Safely encodes a SQL symbolic identifier (or list of identifiers), such as a database,
|
|
|
|
* table, or column name. Supports encoding of multi identfiers separated by
|
|
|
|
* a delimiter (e.g. ".")
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string|array $identifier The identifier to escape. E.g. 'SiteTree.Title'
|
|
|
|
* @param string $separator The string that delimits subsequent identifiers
|
|
|
|
* @return string|array The escaped identifier. E.g. '"SiteTree"."Title"'
|
|
|
|
*/
|
|
|
|
public static function symbol2sql($identifier, $separator = '.') {
|
|
|
|
if(is_array($identifier)) {
|
|
|
|
foreach($identifier as $k => $v) {
|
|
|
|
$identifier[$k] = self::symbol2sql($v, $separator);
|
|
|
|
}
|
|
|
|
return $identifier;
|
|
|
|
} else {
|
|
|
|
return DB::get_conn()->escapeIdentifier($identifier, $separator);
|
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
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function xml2raw($val) {
|
2007-07-19 12:40:28 +02:00
|
|
|
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);
|
2010-12-16 23:55:17 +01:00
|
|
|
else return html_entity_decode($val, ENT_QUOTES, 'UTF-8');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2012-03-30 05:18:14 +02:00
|
|
|
|
2008-08-09 09:03:24 +02:00
|
|
|
/**
|
2010-05-25 05:53:58 +02:00
|
|
|
* Convert a JSON encoded string into an object.
|
2008-08-09 09:03:24 +02:00
|
|
|
*
|
|
|
|
* @param string $val
|
2012-03-30 05:23:55 +02:00
|
|
|
* @return object|boolean
|
2008-08-09 09:03:24 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function json2obj($val) {
|
2012-03-30 05:18:14 +02:00
|
|
|
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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
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
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function json2array($val) {
|
2012-03-30 05:18:14 +02:00
|
|
|
return json_decode($val, true);
|
2008-08-09 09:03:24 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-16 13:49:51 +02:00
|
|
|
/**
|
2010-12-20 04:18:51 +01:00
|
|
|
* Converts an XML string to a PHP array
|
2015-03-20 05:21:59 +01:00
|
|
|
* See http://phpsecurity.readthedocs.org/en/latest/Injection-Attacks.html#xml-external-entity-injection
|
2010-12-20 04:18:51 +01:00
|
|
|
*
|
2011-10-07 22:57:04 +02:00
|
|
|
* @uses recursiveXMLToArray()
|
2015-03-20 05:21:59 +01:00
|
|
|
* @param string $val
|
|
|
|
* @param boolean $disableDoctypes Disables the use of DOCTYPE, and will trigger an error if encountered.
|
|
|
|
* false by default.
|
|
|
|
* @param boolean $disableExternals Disables the loading of external entities. false by default.
|
2010-12-20 04:18:51 +01:00
|
|
|
* @return array
|
2008-10-16 13:49:51 +02:00
|
|
|
*/
|
2015-03-20 05:21:59 +01:00
|
|
|
public static function xml2array($val, $disableDoctypes = false, $disableExternals = false) {
|
|
|
|
// Check doctype
|
|
|
|
if($disableDoctypes && preg_match('/\<\!DOCTYPE.+]\>/', $val)) {
|
|
|
|
throw new InvalidArgumentException('XML Doctype parsing disabled');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable external entity loading
|
|
|
|
if($disableExternals) $oldVal = libxml_disable_entity_loader($disableExternals);
|
|
|
|
try {
|
|
|
|
$xml = new SimpleXMLElement($val);
|
|
|
|
$result = self::recursiveXMLToArray($xml);
|
|
|
|
} catch(Exception $ex) {
|
|
|
|
if($disableExternals) libxml_disable_entity_loader($oldVal);
|
|
|
|
throw $ex;
|
|
|
|
}
|
|
|
|
if($disableExternals) libxml_disable_entity_loader($oldVal);
|
|
|
|
return $result;
|
2008-08-25 01:39:32 +02:00
|
|
|
}
|
2009-05-20 06:18:28 +02:00
|
|
|
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Convert a XML string to a PHP array recursively. Do not
|
2010-12-20 04:18:51 +01:00
|
|
|
* call this function directly, Please use {@link Convert::xml2array()}
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-12-20 04:18:51 +01:00
|
|
|
* @param SimpleXMLElement
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-12-20 04:18:51 +01:00
|
|
|
* @return mixed
|
2009-05-20 06:18:28 +02:00
|
|
|
*/
|
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;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-20 06:18:28 +02:00
|
|
|
return (string) $xml;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Create a link if the string is a valid URL
|
2010-12-20 04:18:51 +01:00
|
|
|
*
|
2015-07-28 03:54:49 +02:00
|
|
|
* @param string $string The string to linkify
|
|
|
|
* @return string A link to the URL if string is a URL
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function linkIfMatch($string) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if( preg_match( '/^[a-z+]+\:\/\/[a-zA-Z0-9$-_.+?&=!*\'()%]+$/', $string ) )
|
|
|
|
return "<a style=\"white-space: nowrap\" href=\"$string\">$string</a>";
|
|
|
|
else
|
|
|
|
return $string;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Simple conversion of HTML to plaintext.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-09-25 06:04:48 +02:00
|
|
|
* @param string $data Input data
|
|
|
|
* @param bool $preserveLinks
|
2015-07-28 03:54:49 +02:00
|
|
|
* @param int $wordWrap
|
|
|
|
* @param array $config
|
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2014-09-25 06:04:48 +02:00
|
|
|
public static function html2raw($data, $preserveLinks = false, $wordWrap = 0, $config = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$defaultConfig = array(
|
|
|
|
'PreserveLinks' => false,
|
|
|
|
'ReplaceBoldAsterisk' => true,
|
|
|
|
'CompressWhitespace' => true,
|
|
|
|
'ReplaceImagesWithAlt' => true,
|
|
|
|
);
|
|
|
|
if(isset($config)) {
|
|
|
|
$config = array_merge($defaultConfig,$config);
|
|
|
|
} else {
|
|
|
|
$config = $defaultConfig;
|
|
|
|
}
|
|
|
|
|
2012-02-14 19:55:52 +01:00
|
|
|
$data = preg_replace("/<style([^A-Za-z0-9>][^>]*)?>.*?<\/style[^>]*>/is","", $data);
|
|
|
|
$data = preg_replace("/<script([^A-Za-z0-9>][^>]*)?>.*?<\/script[^>]*>/is","", $data);
|
2010-10-15 05:51:55 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($config['ReplaceBoldAsterisk']) {
|
2010-10-15 05:51:55 +02:00
|
|
|
$data = preg_replace('%<(strong|b)( [^>]*)?>|</(strong|b)>%i','*',$data);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Expand hyperlinks
|
2009-06-08 02:11:22 +02:00
|
|
|
if(!$preserveLinks && !$config['PreserveLinks']) {
|
2012-12-20 01:40:42 +01:00
|
|
|
$data = preg_replace_callback('/<a[^>]*href\s*=\s*"([^"]*)">(.*?)<\/a>/i', function($matches) {
|
|
|
|
return Convert::html2raw($matches[2]) . "[$matches[1]]";
|
|
|
|
}, $data);
|
|
|
|
$data = preg_replace_callback('/<a[^>]*href\s*=\s*([^ ]*)>(.*?)<\/a>/i', function($matches) {
|
|
|
|
return Convert::html2raw($matches[2]) . "[$matches[1]]";
|
|
|
|
}, $data);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Replace images with their alt tags
|
|
|
|
if($config['ReplaceImagesWithAlt']) {
|
2012-02-27 22:14:02 +01:00
|
|
|
$data = preg_replace('/<img[^>]*alt *= *"([^"]*)"[^>]*>/i', ' \\1 ', $data);
|
|
|
|
$data = preg_replace('/<img[^>]*alt *= *([^ ]*)[^>]*>/i', ' \\1 ', $data);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Compress whitespace
|
|
|
|
if($config['CompressWhitespace']) {
|
2012-02-27 22:14:02 +01:00
|
|
|
$data = preg_replace("/\s+/", " ", $data);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Parse newline tags
|
2012-02-27 22:14:02 +01:00
|
|
|
$data = preg_replace("/\s*<[Hh][1-6]([^A-Za-z0-9>][^>]*)?> */", "\n\n", $data);
|
|
|
|
$data = preg_replace("/\s*<[Pp]([^A-Za-z0-9>][^>]*)?> */", "\n\n", $data);
|
|
|
|
$data = preg_replace("/\s*<[Dd][Ii][Vv]([^A-Za-z0-9>][^>]*)?> */", "\n\n", $data);
|
|
|
|
$data = preg_replace("/\n\n\n+/", "\n\n", $data);
|
|
|
|
|
|
|
|
$data = preg_replace("/<[Bb][Rr]([^A-Za-z0-9>][^>]*)?> */", "\n", $data);
|
|
|
|
$data = preg_replace("/<[Tt][Rr]([^A-Za-z0-9>][^>]*)?> */", "\n", $data);
|
2012-02-28 00:44:27 +01:00
|
|
|
$data = preg_replace("/<\/[Tt][Dd]([^A-Za-z0-9>][^>]*)?> */", " ", $data);
|
2007-07-19 12:40:28 +02:00
|
|
|
$data = preg_replace('/<\/p>/i', "\n\n", $data );
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Replace HTML entities
|
2014-09-25 06:04:48 +02:00
|
|
|
$data = html_entity_decode($data, ENT_QUOTES, 'UTF-8');
|
2007-07-19 12:40:28 +02:00
|
|
|
// Remove all tags (but optionally keep links)
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// 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>');
|
|
|
|
}
|
2014-09-25 06:04:48 +02:00
|
|
|
|
|
|
|
// Wrap
|
|
|
|
if($wordWrap) $data = wordwrap(trim($data), $wordWrap);
|
|
|
|
return trim($data);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-02-27 22:14:02 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* 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)
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param $data string
|
|
|
|
* @return string
|
|
|
|
* @see http://www.ietf.org/rfc/rfc1738.txt
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function raw2mailto($data) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return str_ireplace(
|
|
|
|
array("\n",'?','=',' ','(',')','&','@','"','\'',';'),
|
|
|
|
array('%0A','%3F','%3D','%20','%28','%29','%26','%40','%22','%27','%3B'),
|
|
|
|
$data
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-02-21 22:53:58 +01:00
|
|
|
/**
|
|
|
|
* Convert a string (normally a title) to a string suitable for using in
|
2011-11-14 12:26:51 +01:00
|
|
|
* urls and other html attributes. Uses {@link URLSegmentFilter}.
|
2011-02-21 22:53:58 +01:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param string
|
2011-02-21 22:53:58 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function raw2url($title) {
|
2012-04-04 16:59:30 +02:00
|
|
|
$f = URLSegmentFilter::create();
|
2011-11-14 12:26:51 +01:00
|
|
|
return $f->filter($title);
|
2011-02-21 22:53:58 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-16 06:10:54 +02:00
|
|
|
/**
|
|
|
|
* Normalises newline sequences to conform to (an) OS specific format.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-10-16 06:10:54 +02:00
|
|
|
* @param string $data Text containing potentially mixed formats of newline
|
|
|
|
* sequences including \r, \r\n, \n, or unicode newline characters
|
|
|
|
* @param string $nl The newline sequence to normalise to. Defaults to that
|
|
|
|
* specified by the current OS
|
2015-07-28 03:54:49 +02:00
|
|
|
* @return string
|
2012-10-16 06:10:54 +02:00
|
|
|
*/
|
|
|
|
public static function nl2os($data, $nl = PHP_EOL) {
|
|
|
|
return preg_replace('~\R~u', $nl, $data);
|
|
|
|
}
|
2015-07-28 03:54:49 +02:00
|
|
|
|
|
|
|
/**
|
2016-01-06 00:34:58 +01:00
|
|
|
* Encode a value into a string that can be used as part of a filename.
|
2015-07-28 03:54:49 +02:00
|
|
|
* All string data must be UTF-8 encoded.
|
|
|
|
*
|
|
|
|
* @param mixed $val Value to be encoded
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function base64url_encode($val) {
|
|
|
|
return rtrim(strtr(base64_encode(json_encode($val)), '+/', '~_'), '=');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode a value that was encoded with Convert::base64url_encode.
|
|
|
|
*
|
|
|
|
* @param string $val Value to be decoded
|
|
|
|
* @return mixed Original value
|
|
|
|
*/
|
|
|
|
public static function base64url_decode($val) {
|
|
|
|
return json_decode(
|
|
|
|
base64_decode(str_pad(strtr($val, '~_', '+/'), strlen($val) % 4, '=', STR_PAD_RIGHT)),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
2012-02-27 22:14:02 +01:00
|
|
|
}
|