<?php
/**
* Smarty4phpCMS - Plugin für phpCMS
*
* @author: Martin Jahn <hide@address.com>
* @license: GPL
* @version: 0.1
* @package smarty4phpCMS
*
* Das Plugin ermoeglicht es, die Ausgabe beliebiger Contentfelder zu manipulieren.
* Dazu stellt das Plugin sogenannte Modifier zur Verfuegung, mit denen die Art und Weise der
* Manipulation eingestellt wird. Die genaue Modifiersyntax ist equivalent zu der der Smarty-Modifier
* und kann unter http://smarty.php.net/manual/de/language.modifiers.php nachgelesen werden.
*
* Dieses Plugin/Skript veroeffentliche ich so wie es ist und gebe deshalb auch keinerlei Garantie,
* dass es richtig funktioniert. Ausserdem uebernehme ich keinerlei Haftung fuer eventuelle durch
* die Anwendung des Plugins/Skriptes entstehenden Schaeden.
*
* Falls du Verbesserungsvorschlaege oder Wuensche bezueglich des Skriptes hast melde dich bei
* Martin Jahn <hide@address.com>
*
* Viel Spass
*
* Dieses Script ist kostenlos für private und kommerzielle Nuzung.
* Es wird unter den Bedingungen der GNU General Public License, wie von der Free
* Software Foundation herausgegeben, verteilt. Eine Kopie der GPL sollte im Zip-File
* enthalten sein. Das Script darf (im Rahmen der GPL) nach belieben genutzt, weiter-
* gegeben und modifiziert werden. Dieser Copyright-Hinweis muss jedoch in jedem Fall
* unverändert im Script belassen werden.
*
* Ich übernehme keinerlei Verantwortung für Schäden, die durch die Nutzung dieses
* Scripts entstehen. Bei Fehlern würde ich jedoch gerne informiert werden, damit
* ich diese ausbügeln kann. Auch für den Fall, dass Ihr Verbesserungen vornehmt,
* Verbesserungsvorschläge oder -wünsche habt, wäre ich dankbar für eine Benachrichtigung.
*
*
* Anleitung:
*
* 1. Das Plugin wird in einer Templatedatei statisch eingebunden
* {PLUGIN FILE="$plugindir/smarty4phpCMS.php" TYPE="STATIC"}
* 2. Die zur Verf?gung stehenden Tags werden in der Dokumentation von Smarty beschrieben
* http://smarty.php.net/manual/de/language.modifiers.php
*
**/
/**
* this class contains all modifier-function
*
* these function are called statically by the plugin
**/
class modifier4phpCMS {
function smarty_modifier_capitalize($string)
{
return ucwords($string);
}
function smarty_modifier_cat($string, $cat)
{
return $string . $cat;
}
function smarty_modifier_count_characters($string, $include_spaces = false) {
if ($include_spaces) {
return(strlen($string));
}
return preg_match_all("/[^\s]/",$string, $match);
}
function smarty_modifier_count_paragraphs($string) {
// count \r or \n characters
return count(preg_split('/[\r\n]+/', $string));
}
function smarty_modifier_count_sentences($string) {
// find periods with a word before but not after.
return preg_match_all('/[^\s]\.(?!\w)/', $string, $match);
}
function smarty_modifier_count_words($string)
{
// split text by ' ',\r,\n,\f,\t
$split_array = preg_split('/\s+/',$string);
// count matches that contain alphanumerics
$word_count = preg_grep('/[a-zA-Z0-9\\x80-\\xff]/', $split_array);
return count($word_count);
}
function smarty_modifier_date_format($string, $format="%b %e, %Y", $default_date=null)
{
$string = trim ($string);
if($string != '') {
return strftime($format, modifier4phpCMS::smarty_make_timestamp($string));
} elseif (isset($default_date) && $default_date != '') {
return strftime($format, modifier4phpCMS::smarty_make_timestamp($default_date));
} else {
return strftime($format, modifier4phpCMS::smarty_make_timestamp('now'));
}
}
function smarty_modifier_default($string, $default = '')
{
if (!isset($string) || $string === '')
return $default;
else
return $string;
}
function smarty_modifier_escape($string, $esc_type = 'html')
{
switch ($esc_type) {
case 'html':
return htmlspecialchars($string, ENT_QUOTES);
case 'htmlall':
return htmlentities($string, ENT_QUOTES);
case 'url':
return urlencode($string);
case 'quotes':
// escape unescaped single quotes
return preg_replace("%(?<!\\\\)'%", "\\'", $string);
case 'hex':
// escape every character into hex
$return = '';
for ($x=0; $x < strlen($string); $x++) {
$return .= '%' . bin2hex($string[$x]);
}
return $return;
case 'hexentity':
$return = '';
for ($x=0; $x < strlen($string); $x++) {
$return .= '&#x' . bin2hex($string[$x]) . ';';
}
return $return;
case 'javascript':
// escape quotes and backslashes and newlines
return strtr($string, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n'));
default:
return $string;
}
}
function smarty_modifier_indent($string,$chars=4,$char=" ")
{
return preg_replace('!^!m',str_repeat($char,$chars),$string);
}
function smarty_modifier_lower($string)
{
return strtolower($string);
}
function smarty_modifier_nl2br($string)
{
return nl2br($string);
}
function smarty_modifier_regex_replace($string, $search, $replace)
{
return preg_replace($search, $replace, $string);
}
function smarty_modifier_replace($string, $search, $replace)
{
return str_replace($search, $replace, $string);
}
function smarty_modifier_spacify($string, $spacify_char = ' ')
{
return implode($spacify_char,
preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY));
}
function smarty_modifier_string_format($string, $format)
{
return sprintf($format, $string);
}
function smarty_modifier_strip($text, $replace = ' ')
{
return preg_replace('!\s+!', $replace, $text);
}
function smarty_modifier_strip_tags($string, $replace_with_space = true) {
if ($replace_with_space) {
return preg_replace('!<[^>]*?>!', ' ', $string);
}
return strip_tags($string);
}
function smarty_modifier_truncate($string, $length = 80, $etc = '...', $break_words = false) {
if ($length == 0) {
return '';
}
if (strlen($string) <= $length) {
return $string;
}
$length -= strlen($etc);
if (!$break_words) {
$string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1));
}
return substr($string, 0, $length).$etc;
}
function smarty_modifier_upper($string) {
return strtoupper($string);
}
function smarty_modifier_wordwrap($string,$length=80,$break="\n",$cut=false) {
return wordwrap($string,$length,$break,$cut);
}
function smarty_make_timestamp($string)
{
$string = trim($string);
if(empty($string)) {
$string = "now";
}
$time = strtotime($string);
if (is_numeric($time) && $time != -1)
return $time;
// is mysql timestamp format of YYYYMMDDHHMMSS?
if (preg_match('/^\d{14}$/', $string)) {
$time = mktime(substr($string,8,2),substr($string,10,2),substr($string,12,2),
substr($string,4,2),substr($string,6,2),substr($string,0,4));
return $time;
}
// couldn't recognize it, try to return a time
$time = (int) $string;
if ($time > 0)
return $time;
else
return time();
}
}
class smartyTemplate {
var $name = '';
var $path = '';
function smartyTemplate($filename) {
global $PAGE, $DEFAULTS;
$this->content = new File($filename);
$this->path = dirname($filename);
$this->name = basename($filename);
}
function LineReplacer($menu, $needle, $value) {
$temp = stristr($menu, $needle);
if($temp) {
$PartOne = substr($menu, 0, strpos($menu, $needle));
$PartTwo = substr($menu, strpos($menu, $needle) + strlen($needle));
if(stristr($PartTwo, $needle)) {
$PartTwo = $this->LineReplacer($PartTwo, $needle, $value);
}
$menu = $PartOne.$value.$PartTwo;
}
return $menu;
}
function ReplaceEntry($Menu, $FieldName, $FieldValue) {
global $DEFAULTS;
$MenuCount = count($Menu);
$needle = $DEFAULTS->START_FIELD.$FieldName.$DEFAULTS->STOP_FIELD;
for($i = 0; $i < $MenuCount; $i++) {
$Menu[$i] = $this->LineReplacer($Menu[$i], $needle, $FieldValue);
}
return $Menu;
}
function ExtractPath($FIELD) {
global $PHP, $DEFAULTS;
// extracting Path from "Field" over the "FILE" attribute
$TempPath = $PHP->ExtractValue($FIELD, 'FILE');
if(substr($TempPath, 0, 1) <> '/' AND strtoupper(substr($TempPath, 0, 7)) <> 'HTTP://') {
if(strtoupper(substr($TempPath, 0, 5)) == '$HOME') {
$Path = $DEFAULTS->DOCUMENT_ROOT.$DEFAULTS->PROJECT_HOME.substr($TempPath, 5);
} elseif(strtoupper(substr($TempPath, 0, 10)) == '$PLUGINDIR') {
$Path = $DEFAULTS->DOCUMENT_ROOT.$DEFAULTS->PLUGINDIR.substr($TempPath, 10);
} else {
$Path = $this->path.'/'.$TempPath;
}
} elseif(substr($TempPath, 0, 1) == '/') {
$Path = $DEFAULTS->DOCUMENT_ROOT.$TempPath;
} else {
$Path = $TempPath;
}
return $Path;
}
function ReturnEmty($PartOne, $PartTwo) {
global $DEFAULTS;
if(!stristr($PartTwo, $DEFAULTS->START_FIELD)) {
$ReturnArray[0] = $PartOne.$PartTwo;
} else {
$AddArray = $this->DoField($PartTwo);
$CountAdd = count($AddArray);
for($k = 0; $k < $CountAdd; $k++) {
$ReturnArray[$k] = $AddArray[$k];
}
$ReturnArray[0] = $PartOne.$ReturnArray[0];
}
$ReturnArray = $DEFAULTS->TEMPLATE->PreParse($ReturnArray);
return $ReturnArray;
}
function ReturnFull($PartOne, $buffer, $PartTwo) {
global $DEFAULTS;
$Count = count($buffer);
for($i = 0; $i < $Count; $i++) {
if(isset($buffer[$i])) {
$ReturnArray[$i] = $buffer[$i];
} else {
$ReturnArray[$i] = '';
}
}
if(isset($ReturnArray[0])) {
$ReturnArray[0] = $PartOne.$ReturnArray[0];
} else {
$ReturnArray[0] = $PartOne;
}
if(!stristr($PartTwo, $DEFAULTS->START_FIELD)) {
$ReturnArray[$Count - 1] = $ReturnArray[$Count - 1].$PartTwo;
} else {
$AddArray = $this->DoField($PartTwo);
$CountAdd = count($AddArray);
if(isset($ReturnArray[$i])) {
$ReturnArray[$i] = $ReturnArray[$i].$AddArray[0];
} else {
$ReturnArray[$i] = $AddArray[0];
}
for($k = 1; $k < $CountAdd; $k++) {
$i++;
$ReturnArray[$i] = $AddArray[$k];
}
}
$ReturnArray = $this->PreParse($ReturnArray);
return $ReturnArray;
}
function DoField($Line) {
global
$DEFAULTS,
$tagfields,
$HELPER;
unset($ReturnBuffer);
unset($buffer);
list($PartOne, $FIELD, $PartTwo) = $HELPER->SplitLine($Line, $DEFAULTS->START_FIELD, $DEFAULTS->STOP_FIELD);
//echo $FIELD,':<br />';
if(strtoupper(substr($FIELD, 0, 14)) == 'TEMPLATE FILE=') {
if(isset($aTemplate)) {
unset($aTemplate);
}
$aTemplate = new smartyTemplate($this->ExtractPath($FIELD));
$buffer = $aTemplate->content->lines;
$ReturnBuffer = $this->ReturnFull($PartOne, $buffer, $PartTwo);
return $ReturnBuffer;
}
$tagfields[] = $FIELD;
}
function PreParse($lines) {
global $DEFAULTS;
$NewLines = array();
$ArrayCount = count($lines);
$i = 0;
$nl = 0;
while($i < $ArrayCount) {
// if no field -> continue
$temp = strstr($lines[$i], $DEFAULTS->START_FIELD);
if(!$temp) {
$NewLines[$nl] = $lines[$i];
$i++;
$nl++;
continue;
}
$Replace = $this->DoField($lines[$i]);
$CountRep = count($Replace);
for($k = 0; $k < $CountRep; $k++) {
if(!isset($Replace[$k])) {
continue;
}
$NewLines[$nl] = $Replace[$k];
$nl++;
}
$i++;
}
return $NewLines;
}
}
global $tagfields;
$tagfields = array();
$A = new smartyTemplate($DEFAULTS->TEMPLATE->path.'/'.$DEFAULTS->TEMPLATE->name);
$A->PreParse($A->content->lines);
/**
* Zwischenstand:
* alle vorkommende Felder im Template wurden erkannt und extrahiert
* nun werden alle Feldnamen gefiltert (Plugins, Men?s, Script usw fliegen raus)
**/
$fields = array();
$fieldname = '';
foreach ($tagfields as $value) {
if (!stristr($value, '|')) {
continue;
}
$call = explode('|', $value);
$fieldname = array_shift($call);
if (!isset($PageContent->{$fieldname})) {
//continue;
}
/**
* Verarbeitung des Feldbefehles
**/
$buffer = '';
if (isset($PageContent->{$fieldname})) {
$buffer = join('', $PageContent->{$fieldname});
}
foreach ($call as $filter) {
$params = explode(':',$filter);
$filtername = $params[0];
$params[0] = '';
eval('$buffer = modifier4phpCMS::smarty_modifier_'.$filtername.' ($buffer'.join(', ', $params).');');
}
$PageContent->{$value} = explode("\n", $buffer);
}
unset($A, $tagfields, $fieldname, $fields);
?>