Tag: class
BBCode php parser class
by z3n on Jul.10, 2010, under Coding, Tips & Hints
This class is based on jquery’s markitup plugin function, i did some code compressions and added youtube handler to it. If you need base_defines function, just search the site.
class BBCodeParser {
private $text;
public function __construct() {
base_defines(array(
"EMOTICONS_DIR" => "/img/emoticons/"
));
}
public function escape($s) {
$this->text = strip_tags($this->text);
$code = $s[1];
$code = htmlspecialchars($code);
$code = str_replace("[", "[", $code);
$code = str_replace("]", "]", $code);
return '<pre><code>'.$code.'</code></pre>';
}
public function removeBr($s) {
return str_replace("<br />", "", $s[0]);
}
public function parse($text) {
$this->text = trim($text);
$this->text = preg_replace(array(
'/\[b\](.*?)\[\/b\]/ms',
'/\[i\](.*?)\[\/i\]/ms',
'/\[u\](.*?)\[\/u\]/ms',
'/\[img\](.*?)\[\/img\]/ms',
'/\[email\](.*?)\[\/email\]/ms',
'/\[url\="?(.*?)"?\](.*?)\[\/url\]/ms',
'/\[size\="?(.*?)"?\](.*?)\[\/size\]/ms',
'/\[youtube\](.*?)\[\/youtube\]/ms',
'/\[color\="?(.*?)"?\](.*?)\[\/color\]/ms',
'/\[quote](.*?)\[\/quote\]/ms',
'/\[list\=(.*?)\](.*?)\[\/list\]/ms',
'/\[list\](.*?)\[\/list\]/ms',
'/\[\*\]\s?(.*?)\n/ms'
),array(
'<strong>\1</strong>',
'<em>\1</em>',
'<u>\1</u>',
'<img src="\1" alt="\1" />',
'<a href="mailto:\1">\1</a>',
'<a href="\1">\2</a>',
'<span style="font-size:\1%">\2</span>',
'<object width="450" height="350"><param name="movie" value="\1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="\1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="450" height="350"></embed></object>',
'<span style="color:\1">\2</span>',
'<blockquote>\1</blockquote>',
'<ol start="\1">\2</ol>',
'<ul>\1</ul>',
'<li>\1</li>'
),
str_replace(array(
':)',
':D',
':o',
':p',
':P',
':(',
';)'
),array(
'<img alt=":)" src="'.EMOTICONS_DIR.'emoticon-happy.png" />',
'<img alt=":D" src="'.EMOTICONS_DIR.'emoticon-smile.png" />',
'<img alt=":o" src="'.EMOTICONS_DIR.'emoticon-surprised.png" />',
'<img alt=":p" src="'.EMOTICONS_DIR.'emoticon-tongue.png" />',
'<img alt=":P" src="'.EMOTICONS_DIR.'emoticon-tongue.png" />',
'<img alt=":(" src="'.EMOTICONS_DIR.'emoticon-unhappy.png" />',
'<img alt=";)" src="'.EMOTICONS_DIR.'emoticon-wink.png" />'
),
preg_replace_callback(
'/\[code\](.*?)\[\/code\]/ms',
array(&$this, "escape"), // http://www.php.net/manual/en/function.preg-replace-callback.php#90849
//"BBCodeParser::escape", // http://www.php.net/manual/en/function.preg-replace-callback.php#92548
$this->text
)
)
);
// paragraphs
$this->text = str_replace("\r", "", $this->text);
$this->text = "<p>".preg_replace("/(\n){2,}/", "</p><p>", $this->text)."</p>";
$this->text = nl2br($this->text);
// clean some tags to remain strict
// not very elegant, but it works. No time to do better ;)
$this->text = preg_replace_callback('/<pre>(.*?)<\/pre>/ms', array(&$this,"removeBr"), $this->text);
$this->text = preg_replace('/<p><pre>(.*?)<\/pre><\/p>/ms', "<pre>\\1</pre>", $this->text);
$this->text = preg_replace_callback('/<ul>(.*?)<\/ul>/ms', array(&$this,"removeBr"), $this->text);
$this->text = preg_replace('/<p><ul>(.*?)<\/ul><\/p>/ms', "<ul>\\1</ul>", $this->text);
return $this->text;
}
}
JSLint php class
by z3n on Jul.07, 2010, under Coding, Tips & Hints
Problem:
How to lint check a javascript using php?
Solution:
// (c) z3n - R1V1@100707 - www.overflow.biz - rodrigo.orph@gmail.com
// Based on the original by Matthias Miller (http://www.JavaScriptLint.com/)
class JSLEngine {
private $_binarypath; // jlint exec
private $_confpath; // config path
private $fn; // temp filename (not used outside class)
private $r; // jlint output
private $has_errors=0; // error flag
public function __construct($binarypath="", $confpath="") {
// default paths
base_defines(array(
"jslint_binary_path" => _fn_fix(dirname(dirname(dirname(__FILE__)))."/3rd/jsl-0.3.0/jsl.exe"),
"jslint_conf_path" => _fn_fix(dirname(dirname(dirname(__FILE__)))."/3rd/jsl-0.3.0/jsl.default.conf")
));
// startup
$this->_binarypath = $binarypath == "" ? jslint_binary_path : $binarypath;
$this->_confpath = $confpath == "" ? jslint_conf_path : $confpath;
}
public function __destruct() {
if ($this->fn != null && file_exists($this->fn))
unlink($this->fn);
}
/* returns error on failure; returns true on success */
public function Lint($code) {
if (!$this->_launchLintBinary($code, $output))
die('The JavaScript Lint online service is currently unavailable.');
// store lint
$this->r=$output;
$output=explode("\n",$output); // break lines
$x=$output[count($output)-2]; // X error(s), X warning(s) (total lines -2)
$x=trim(substr($x,0,strpos($x," ")));
if ($x > 0) { // has errors
$this->has_errors=1;
return false;
} else { // clean
$this->has_errors=0;
return true;
}
}
/* assumes path and that SERVER_SOFTWARE env is set */
private function _launchLintBinary($input, &$output) {
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w")
);
$this->fn=_fn_fix(dirname(__FILE__).'/tmp.js');
file_put_contents($this->fn,$input);
/* launch process */
$path = PHP_OS == "WINNT" ? $this->_binarypath : escapeshellcmd($this->_binarypath);
$path.= ' --nologo --conf '.escapeshellarg($this->_confpath).' --process '.escapeshellarg($this->fn);
$process = proc_open($path, $descriptorspec, $pipes);
if (!is_resource($process))
return false;
$output = '';
while (!feof($pipes[1]))
$output .= fgets($pipes[1], 1024);
fclose($pipes[1]);
fclose($pipes[2]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($process);
return true;
}
public function output() {
return $this->r;
}
}
Helper functions:
function _fn_fix($fn,$force="") { // v1.02
if (strpos($fn,"://") === false) {
if ((PHP_OS == "WINNT" && $force == "") || $force == "WINNT")
$fn=str_replace("/","\\",$fn);
else
$fn=str_replace("\\","/",$fn);
}
if (strpos($fn,":/") !== false && strpos($fn,"://") === false)
$fn=substr($fn,2);
return $fn;
}
function base_defines($x) { // define default
foreach ($x as $k => $v) {
if (!defined($k)) {
define($k,$v);
}
}
}
Usage:
$lint=new JSLEngine();
if (!$lint->Lint($js)) {
echo "bad js code! full output:\n";
echo $lint->output();
}
Required:
jslint binaries from http://www.JavaScriptLint.com/
Make sure you set the default path on the __construct, so you don’t need to keep setting it on every call.