aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rwxr-xr-xinclude/ITemplateEngine.php11
-rwxr-xr-x[-rw-r--r--]include/friendica_smarty.php45
-rwxr-xr-x[-rw-r--r--]include/plugin.php21
-rwxr-xr-x[-rw-r--r--]include/template_processor.php30
-rwxr-xr-x[-rw-r--r--]include/text.php63
5 files changed, 107 insertions, 63 deletions
diff --git a/include/ITemplateEngine.php b/include/ITemplateEngine.php
new file mode 100755
index 000000000..53c1845f4
--- /dev/null
+++ b/include/ITemplateEngine.php
@@ -0,0 +1,11 @@
+<?php
+require_once 'boot.php';
+
+
+/**
+ * Interface for template engines
+ */
+interface ITemplateEngine {
+ public function replace_macros($s,$v);
+ public function get_markup_template($file, $root='');
+}
diff --git a/include/friendica_smarty.php b/include/friendica_smarty.php
index b9ed2e982..a4675acd4 100644..100755
--- a/include/friendica_smarty.php
+++ b/include/friendica_smarty.php
@@ -1,7 +1,8 @@
<?php /** @file */
-
+require_once 'include/ITemplateEngine.php';
require_once("library/Smarty/libs/Smarty.class.php");
+
class FriendicaSmarty extends Smarty {
public $filename;
@@ -14,10 +15,10 @@ class FriendicaSmarty extends Smarty {
// setTemplateDir can be set to an array, which Smarty will parse in order.
// The order is thus very important here
- $template_dirs = array('theme' => "view/theme/$theme/tpl/smarty3/");
+ $template_dirs = array('theme' => "view/theme/$theme/tpl/");
if( x($a->theme_info,"extends") )
- $template_dirs = $template_dirs + array('extends' => "view/theme/".$a->theme_info["extends"]."/tpl/smarty3/");
- $template_dirs = $template_dirs + array('base' => 'view/tpl/smarty3/');
+ $template_dirs = $template_dirs + array('extends' => "view/theme/".$a->theme_info["extends"]."/tpl/");
+ $template_dirs = $template_dirs + array('base' => 'view/tpl/');
$this->setTemplateDir($template_dirs);
$this->setCompileDir('view/tpl/smarty3/compiled/');
@@ -41,3 +42,39 @@ class FriendicaSmarty extends Smarty {
+class FriendicaSmartyEngine implements ITemplateEngine {
+ static $name ="smarty3";
+
+ public function __construct(){
+ if(!is_writable('view/tpl/smarty3/')){
+ echo "<b>ERROR:</b> folder <tt>view/tpl/smarty3/</tt> must be writable by webserver."; killme();
+ }
+ }
+
+ // ITemplateEngine interface
+ public function replace_macros($s, $r) {
+ $template = '';
+ if(gettype($s) === 'string') {
+ $template = $s;
+ $s = new FriendicaSmarty();
+ }
+ foreach($r as $key=>$value) {
+ if($key[0] === '$') {
+ $key = substr($key, 1);
+ }
+ $s->assign($key, $value);
+ }
+ return $s->parsed($template);
+ }
+
+ public function get_markup_template($file, $root=''){
+ $template_file = theme_include($file, $root);
+ if($template_file) {
+ $template = new FriendicaSmarty();
+ $template->filename = $template_file;
+
+ return $template;
+ }
+ return "";
+ }
+} \ No newline at end of file
diff --git a/include/plugin.php b/include/plugin.php
index 31427e117..3ef97fba9 100644..100755
--- a/include/plugin.php
+++ b/include/plugin.php
@@ -569,24 +569,9 @@ function get_intltext_template($s) {
function get_markup_template($s, $root = '') {
-
$a = get_app();
-
- $template_eng = $a->get_template_engine();
- if($template_eng === 'internal') {
- $template_file = theme_include($s, $root);
- if($template_file)
- return file_get_contents($template_file);
- }
- else {
- $template_file = theme_include("$template_eng/$s", $root);
-
- if($template_file) {
- $template = new FriendicaSmarty();
- $template->filename = $template_file;
-
- return $template;
- }
- }
+ $t = $a->template_engine();
+ $template = $t->get_markup_template($s, $root);
+ return $template;
}
diff --git a/include/template_processor.php b/include/template_processor.php
index 0b4b4142f..74acc9c67 100644..100755
--- a/include/template_processor.php
+++ b/include/template_processor.php
@@ -1,7 +1,11 @@
-<?php /** @file */
+<?php
+ require_once 'include/ITemplateEngine.php';
+
define ("KEY_NOT_EXISTS", '^R_key_not_Exists^');
- class Template {
+ class Template implements ITemplateEngine {
+ static $name ="internal";
+
var $r;
var $search;
var $replace;
@@ -244,9 +248,13 @@
return $s;
}
-
- public function replace($s, $r) {
- $t1 = dba_timer();
+
+ private function replace($s,$r) {
+ $this->replace_macros($s, $r);
+ }
+
+ // TemplateEngine interface
+ public function replace_macros($s, $r) {
$this->r = $r;
$s = $this->_build_nodes($s);
@@ -265,14 +273,18 @@
$os=$s; $count++;
$s = $this->var_replace($s);
}
- $t3 = dba_timer();
-// logger('macro timer: ' . sprintf('%01.4f %01.4f',$t3 - $t2, $t2 - $t1));
-
return $s;
}
+
+ public function get_markup_template($file, $root='') {
+ $template_file = theme_include($file, $root);
+ if ($template_file) {
+ $content = file_get_contents($template_file);
+ }
+ return $content;
+ }
}
- $t = new Template;
diff --git a/include/text.php b/include/text.php
index c7210010c..3b3620f33 100644..100755
--- a/include/text.php
+++ b/include/text.php
@@ -1,44 +1,23 @@
<?php /** @file */
-// This is our template processor.
-// $s is the string requiring macro substitution.
-// $r is an array of key value pairs (search => replace)
-// returns substituted string.
-
require_once("include/template_processor.php");
+require_once("include/friendica_smarty.php");
-
+/**
+ * This is our template processor
+ *
+ * @param string|FriendicaSmarty $s the string requiring macro substitution,
+ * or an instance of FriendicaSmarty
+ * @param array $r key value pairs (search => replace)
+ * @return string substituted string
+ */
function replace_macros($s,$r) {
- global $t;
-
-// $ts = microtime();
$a = get_app();
- if($a->get_template_engine() === 'smarty3') {
- $output = '';
- if(gettype($s) !== 'NULL') {
- $template = '';
- if(gettype($s) === 'string') {
- $template = $s;
- $s = new FriendicaSmarty();
- }
- foreach($r as $key=>$value) {
- if($key[0] === '$') {
- $key = substr($key, 1);
- }
- $s->assign($key, $value);
- }
- $output = $s->parsed($template);
- }
- }
- else {
- $r = $t->replace($s,$r);
+ $t = $a->template_engine();
+ $output = $t->replace_macros($s,$r);
- $output = template_unescape($r);
- }
-// $tt = microtime() - $ts;
-// $a->page['debug'] .= "$tt <br>\n";
return $output;
}
@@ -71,6 +50,8 @@ function random_string($size = 64,$type = RANDOM_STRING_HEX) {
* They will be replaced with safer brackets. This may be filtered further
* if these are not allowed either.
*
+ * @param string $string Input string
+ * @return string Filtered string
*/
@@ -86,6 +67,13 @@ function notags($string) {
// and allow them to be safely displayed.
+
+/**
+ * use this on "body" or "content" input where angle chars shouldn't be removed,
+ * and allow them to be safely displayed.
+ * @param string $string
+ * @return string
+ */
function escape_tags($string) {
return(htmlspecialchars($string, ENT_COMPAT, 'UTF-8', false));
@@ -97,6 +85,12 @@ function escape_tags($string) {
// used to generate initial passwords
+/**
+ * generate a string that's random, but usually pronounceable.
+ * used to generate initial passwords
+ * @param int $len
+ * @return string
+ */
function autoname($len) {
if($len <= 0)
@@ -172,6 +166,11 @@ function autoname($len) {
// returns escaped text.
+/**
+ * escape text ($str) for XML transport
+ * @param string $str
+ * @return string Escaped text.
+ */
function xmlify($str) {
$buffer = '';