/addons/DynamicMTML.pack/php/dynamicmtml.php
PHP | 1587 lines | 1505 code | 57 blank | 25 comment | 327 complexity | 3cbca6e74dc9b30fa7dd59fa20263891 MD5 | raw file
Possible License(s): MIT, GPL-2.0
Large files files are truncated, but you can click here to view the full file
- <?php
- class DynamicMTML {
- var $mt;
- var $ctx;
- var $__stash;
- var $db;
- var $config;
- var $cfg_file;
- var $mode;
- var $mod_rewrite;
- var $blog;
- var $blog_id;
- var $user;
- var $user_cookie;
- var $session;
- var $url;
- var $root;
- var $is_secure;
- var $file;
- var $base;
- var $path;
- var $static_path;
- var $script;
- var $request;
- var $cache;
- var $cache_dir;
- var $conditional;
- var $force_compile;
- var $param;
- var $query_string;
- var $contenttype;
- var $extension;
- var $build_type;
- var $result_type;
- var $fileinfo;
- var $template;
- var $basename;
- var $filemtime;
- var $cache_saved;
- var $text;
- var $contents;
- var $templates_c;
- var $preview;
- var $protocol;
- var $remote_ip;
- var $request_method;
- var $entry_ids_published;
- var $plugin_path;
- protected $debugging = FALSE;
- function init ( $args = array() ) {
- require_once( 'MTUtil.php' );
- $this->protocol = $_SERVER[ 'SERVER_PROTOCOL' ];
- $this->remote_ip = $_SERVER[ 'REMOTE_ADDR' ];
- if (! $this->request_method ) {
- $this->request_method = $_SERVER[ 'REQUEST_METHOD' ];
- }
- $this->static_path = $this->config( 'StaticWebPath' );
- foreach ( $args as $key => $val ) {
- $this->$key = $args[ $key ];
- if ( $key === 'param' ) {
- $this->query_string = $args[ $key ];
- }
- }
- if (! $mode = $this->get_param( '__mode' ) ) {
- $mode = 'default';
- }
- $this->mode = $mode;
- $this->entry_ids_published = array();
- }
- function set_context ( &$mt, &$ctx, $blog_id = NULL ) {
- $this->stash( 'mt', $mt );
- $this->stash( 'ctx', $ctx );
- if ( isset( $ctx ) ) {
- $ctx->stash( 'bootstrapper', $this );
- }
- $mt->db()->set_names( $mt );
- $this->stash( 'db', $mt->db() );
- $blog = $ctx->stash( 'blog' );
- if (! isset( $blog ) ) {
- $blog = $this->stash( 'blog' );
- }
- if (! isset( $blog ) ) {
- // if (! $blog_id ) {
- // $blog_id = $this->stash( 'blog_id' );
- // }
- if (! $blog_id ) {
- // $blog = $this->load( 'Blog',
- // array( 'class' => 'website', 'blog' ),
- // array( 'limit' => 1,
- // 'sort_order' => 'ascend',
- // 'sort' => 'id' ) );
- // $blog_id = $blog->id;
- } else {
- $blog = $mt->db()->fetch_blog( $blog_id );
- }
- }
- $templates_c = NULL;
- $cache = NULL;
- if ( isset( $blog ) ) {
- $ctx->stash( 'blog', $blog );
- $ctx->stash( 'blog_id', $blog_id );
- $this->stash( 'blog', $blog );
- $this->stash( 'blog_id', $blog_id );
- $site_path = $blog->site_path();
- $self = $site_path;
- $templates_c = $self . DIRECTORY_SEPARATOR . 'templates_c';
- $cache = $self . DIRECTORY_SEPARATOR . 'cache';
- } else {
- $self = $this->root . dirname( $_SERVER[ 'PHP_SELF' ] );
- $templates_c = $self . DIRECTORY_SEPARATOR . 'templates_c';
- $cache = $self . DIRECTORY_SEPARATOR . 'cache';
- }
- if (! is_dir( $templates_c ) ) {
- mkdir( $templates_c, 0755 );
- }
- $this->stash( 'templates_c', $templates_c );
- $cache = $self . DIRECTORY_SEPARATOR . 'cache';
- if (! is_dir( $cache ) ) {
- mkdir( $cache, 0755 );
- }
- $powercms_files_dir = NULL;
- if (! $powercms_files_dir = $mt->config( 'PowerCMSFilesDir' ) ) {
- $powercms_files_dir = dirname( $this->cfg_file ) . DIRECTORY_SEPARATOR . 'powercms_files';
- }
- $powercms_files_dir = preg_replace( "/DIRECTORY_SEPARATOR$/", '', $powercms_files_dir );
- if (! is_dir( $powercms_files_dir ) ) {
- mkdir( $powercms_files_dir, 0755 );
- }
- $this->stash( 'powercms_files_dir', $powercms_files_dir );
- $powercms_cache = $powercms_files_dir . DIRECTORY_SEPARATOR . 'cache';
- if (! is_dir( $powercms_cache ) ) {
- mkdir( $powercms_cache, 0755 );
- }
- $language = $mt->config( 'DefaultLanguage' );
- $l10n_dir = $this->stash( 'l10n_dir' );
- if ( is_array( $l10n_dir ) ) {
- foreach ( $l10n_dir as $plugin_l10n_dir ) {
- if ( is_dir( $plugin_l10n_dir ) ) {
- $l10n_file = $plugin_l10n_dir . DIRECTORY_SEPARATOR . 'l10n_' . $language . '.php';
- if ( file_exists( $l10n_file ) ) {
- require( $l10n_file );
- if ( isset( $Lexicon ) && is_array( $Lexicon ) ) {
- $this->add_lexicon( $language, $Lexicon );
- unset( $Lexicon );
- }
- }
- }
- }
- }
- if (! function_exists( 'is_valid_email' ) ) {
- require_once( dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'mt_util.php' );
- }
- }
- function user () {
- if (! $timeout = $this->config( 'UserSessionTimeout' ) ) {
- $timeout = 14400;
- }
- $author = $this->get_author( $this->ctx, $timeout );
- return $author;
- }
- function mt () {
- if ( $this->mt ) return $this->mt;
- global $mt;
- if ( $mt ) {
- return $this->stash( 'mt', $mt );
- }
- if ( $this->ctx() ) {
- return $this->stash( 'mt', $this->ctx()->mt );
- }
- global $ctx;
- if ( $ctx ) {
- return $this->stash( $ctx->mt );
- }
- return $this;
- }
- function ctx () {
- if ( $this->ctx ) return $this->ctx;
- global $ctx;
- if ( $ctx ) {
- return $this->stash( 'ctx', $ctx );
- }
- }
- function db () {
- if ( $this->db ) return $this->db;
- if ( $this->ctx() ) {
- return $this->stash( 'db', $this->ctx()->mt->db() );
- }
- }
- function init_mt ( &$mt, &$ctx, $blog_id ) {
- if ( is_object( $blog_id ) ) {
- $blog = $blog_id;
- $blog_id = $blog->id;
- } else {
- if ( ( $this->stash( 'blog_id' ) ) != $blog_id ) {
- $blog = $this->load( 'Blog', $blog_id );
- }
- }
- if ( (! $mt->blog_id() ) || ( $mt->blog_id() != $blog_id ) ) {
- $mt->init( $blog_id, $this->cfg_file );
- $mt->db()->set_names( $mt );
- $ctx->stash( 'blog', $blog );
- $ctx->stash( 'blog_id', $blog_id );
- $this->stash( 'blog', $blog );
- $this->stash( 'blog_id', $blog_id );
- }
- }
- function user_cookie () {
- return $this->user_cookie;
- }
- function blog ( $id = NULL ) {
- $blog_id = $id;
- $mt = $this->mt();
- if (! $blog_id ) {
- if ( $blog = $this->blog ) {
- return $blog;
- }
- $blog_id = $mt->blog_id();
- }
- if (! $mt->db() ) {
- return NULL;
- }
- if (! $blog_id ) {
- return NULL;
- }
- $blog = $mt->db()->fetch_blog( $blog_id );
- if (! $blog ) {
- $blog = $this->load( 'Blog', $blog_id );
- }
- if (! $blog ) {
- $terms = array( 'id' => $blog_id, 'class' => array( 'website', 'blog' ) );
- $blog = $this->load( 'Blog', $terms, array( 'limit' => 1 ) );
- if (! $blog ) {
- $ctx = $this->ctx();
- if ( $ctx ) {
- if ( $ctx->stash( 'blog' ) ) {
- $blog = $ctx->stash( 'blog' );
- }
- }
- }
- }
- if (! $id ) {
- $this->stash( 'blog', $blog );
- $ctx->stash( 'blog', $blog );
- if ( $blog ) {
- $this->stash( 'blog_id', $blog->id );
- $ctx->stash( 'blog_id', $blog->id );
- }
- }
- return $blog;
- }
- function blog_id () {
- if ( $this->blog_id ) {
- return $this->blog_id;
- }
- $mt = $this->mt();
- return $this->stash( 'blog_id', $mt->blog_id() );
- }
- function mode () {
- return $this->mode;
- }
- function base () {
- return $this->base;
- }
- function path () {
- return $this->path;
- }
- function remote_ip () {
- return $this->remote_ip;
- }
- function is_secure () {
- return $this->is_secure;
- }
- function query_string () {
- if ( $query_string = $this->query_string ) {
- return $query_string;
- }
- if ( $params = $this->param() ) {
- $params_array = array();
- if ( is_array( $params ) ) {
- foreach ( $params as $key => $value ) {
- array_push( $params_array, "$key=$value" );
- }
- if ( $params_array ) {
- return join( '&', $params_array );
- }
- }
- }
- }
- function static_path () {
- return $this->static_path;
- }
- function request_method () {
- return $this->request_method;
- }
- function log ( $msg = NULL, $args = NULL ) {
- if ( ( is_array( $msg ) ) || ( is_object( $msg ) ) ) {
- ob_start();
- var_dump( $msg );
- $msg = ob_get_contents();
- ob_end_clean();
- }
- require_once( 'dynamicmtml.util.php' );
- require_once( 'class.mt_log.php' );
- $_log = new Log;
- $_log->message = $msg;
- if ( $user = $this->user() ) {
- $_log->author_id = $user->id;
- $_log->created_by = $user->id;
- } else {
- $_log->author_id = 0;
- }
- if ( $args && __is_hash( $args ) ) {
- foreach ( $args as $key => $val ) {
- if ( $_log->has_column( $key ) ) {
- $_log->$key = $val;
- }
- }
- }
- $_log->ip = $this->remote_ip;
- if (! $_log->class ) {
- $_log->class = 'system';
- }
- if (! $_log->level ) {
- $_log->level = 1;
- }
- if (! $_log->blog_id ) {
- if ( $blog = $this->blog ) {
- $_log->blog_id = $blog->id;
- }
- }
- if (! $_log->blog_id ) {
- $_log->blog_id = 0;
- }
- $ts = gmdate( "YmdHis" );
- $_log->created_on = $ts;
- $_log->modified_on = $ts;
- $_log->Save();
- }
- function cache ( $key, $val = NULL ) {
- return $this->stash( $key, $val );
- }
- function stash ( $key, $val = NULL ) {
- if ( isset ( $val ) ) {
- $this->$key = $val;
- }
- if ( isset( $this->$key ) ) {
- return $this->$key;
- }
- return NULL;
- }
- function param ( $param = NULL ) {
- $to_encoding = $this->config( 'PublishCharset' );
- $to_encoding or $to_encoding = 'UTF-8';
- if ( $param ) {
- if ( $value = $this->get_param( $param ) ) {
- if (! is_array( $value ) ) {
- $from_encoding = mb_detect_encoding( $value, 'UTF-8,EUC-JP,SJIS,JIS' );
- $value = mb_convert_encoding( $value, $to_encoding, $from_encoding );
- return $value;
- } else {
- $new_array = array();
- foreach ( $value as $val ) {
- $from_encoding = mb_detect_encoding( $val, 'UTF-8,EUC-JP,SJIS,JIS' );
- $val = mb_convert_encoding( $val, $to_encoding, $from_encoding );
- array_push ( $new_array, $val );
- }
- return $new_array;
- }
- }
- } else {
- $vars = $_REQUEST;
- $params = array();
- foreach ( $vars as $key => $value ) {
- if ( $_GET[ $key ] || $_POST[ $key ] ) {
- if ( is_string( $value ) ) {
- $from_encoding = mb_detect_encoding( $value, 'UTF-8,EUC-JP,SJIS,JIS' );
- $value = mb_convert_encoding( $value, $to_encoding, $from_encoding );
- }
- $params[ $key ] = $value;
- }
- }
- return $params;
- }
- }
- function config ( $id, $value = NULL ) {
- $orig_id = $id;
- $id = strtolower( $id );
- if ( isset( $value ) ) {
- $this->config[ $id ] = $value;
- return $value;
- }
- if ( isset( $this->config[ $id ] ) ) {
- return $this->config[ $id ];
- }
- $plugins_config = $this->stash( 'plugins_config' );
- if ( is_array( $plugins_config ) ) {
- foreach ( $plugins_config as $key => $val ) {
- if ( isset( $val[ 'config_settings' ] ) ) {
- if ( isset( $val[ 'config_settings' ][ $id ] ) ) {
- if ( isset( $val[ 'config_settings' ][ $id ][ 'default' ] ) ) {
- $this->config[ $id ] = $val[ 'config_settings' ][ $id ][ 'default' ];
- return $val[ 'config_settings' ][ $id ][ 'default' ];
- break;
- }
- }
- if ( isset( $val[ 'config_settings' ][ $orig_id ] ) ) {
- if ( isset( $val[ 'config_settings' ][ $orig_id ][ 'default' ] ) ) {
- $this->config[ $id ] = $val[ 'config_settings' ][ $orig_id ][ 'default' ];
- return $val[ 'config_settings' ][ $orig_id ][ 'default' ];
- break;
- }
- }
- }
- }
- }
- return NULL;
- }
- function configure ( $file = NULL ) {
- // if ( isset( $this->config ) ) return $config;
- $this->cfg_file = $file;
- $cfg = array();
- if ( file_exists( $file ) ) {
- if ( $fp = file( $file ) ) {
- foreach ( $fp as $line ) {
- if (! preg_match('/^\s*\#/i', $line ) ) {
- if ( preg_match( '/^\s*(\S+)\s+(.*)$/', $line, $regs ) ) {
- $key = strtolower( trim( $regs[1] ) );
- $value = trim( $regs[2] );
- if ( $key === 'pluginpath' ) {
- $cfg[ $key ][] = $value;
- } else {
- $cfg[ $key ] = $value;
- }
- }
- }
- }
- } else {
- // die( "Unable to open configuration file $file" );
- }
- }
- $cfg[ 'mtdir' ] = realpath( dirname( $file ) );
- $cfg[ 'phpdir' ] = $cfg[ 'mtdir' ] . DIRECTORY_SEPARATOR . 'php';
- $cfg[ 'phplibdir' ] = $cfg[ 'phpdir' ] . DIRECTORY_SEPARATOR . 'lib';
- $cfg[ 'powercmsdir' ] = realpath( dirname( __FILE__ ) );
- $this->config =& $cfg;
- isset( $cfg[ 'pluginpath' ] ) or
- $cfg[ 'pluginpath' ] = array( $this->config( 'MTDir' ) . DIRECTORY_SEPARATOR . 'plugins' );
- $cfg[ 'pluginpath' ][] = $this->config( 'MTDir' ) . DIRECTORY_SEPARATOR . 'addons';
- if ( strtoupper( substr( PHP_OS, 0, 3 ) === 'WIN' ) ) {
- $path_sep = ';';
- } else {
- $path_sep = ':';
- }
- ini_set( 'include_path',
- $cfg[ 'powercmsdir' ] . $path_sep .
- $cfg[ 'powercmsdir' ] . DIRECTORY_SEPARATOR . "callbacks" . $path_sep .
- $cfg[ 'phpdir' ] . DIRECTORY_SEPARATOR . "lib" . $path_sep .
- $cfg[ 'phpdir' ] . DIRECTORY_SEPARATOR . "extlib" . $path_sep .
- $cfg[ 'phpdir' ] . DIRECTORY_SEPARATOR . "extlib" . DIRECTORY_SEPARATOR . "smarty" . DIRECTORY_SEPARATOR . "libs" . $path_sep .
- $cfg[ 'phpdir' ] . DIRECTORY_SEPARATOR . "extlib" . DIRECTORY_SEPARATOR . "smarty" . DIRECTORY_SEPARATOR . "libs" . DIRECTORY_SEPARATOR . 'plugins' . $path_sep .
- $cfg[ 'phpdir' ] . DIRECTORY_SEPARATOR . "extlib" . DIRECTORY_SEPARATOR . "adodb5" . $path_sep .
- $cfg[ 'phpdir' ] . DIRECTORY_SEPARATOR . "extlib" . DIRECTORY_SEPARATOR . "FirePHPCore" . $path_sep .
- ini_get( 'include_path' )
- );
- $this->init_plugin_dir();
- if (! $powercms_files_dir = $this->config( 'PowerCMSFilesDir' ) ) {
- $powercms_files_dir = dirname( $this->cfg_file ) . DIRECTORY_SEPARATOR . 'powercms_files';
- }
- $powercms_files_dir = $this->__add_slash( $powercms_files_dir, FALSE );
- $this->stash( 'powercms_files_dir', $powercms_files_dir );
- $this->run_callbacks( 'init_request' );
- }
- function get_args () {
- $args = get_class_vars( 'DynamicMTML' );
- foreach ( $args as $key => $val ) {
- $args[ $key ] = $this->$key;
- }
- return $args;
- }
- function file () {
- return $this->file;
- }
- function url () {
- return $this->url;
- }
- function cache_filename ( $blog_id, $file, $param ) {
- $basename = "blog_id_{$blog_id}_" . md5( $file );
- if ( $param ) {
- $basename .= '_' . md5( $param );
- }
- $pinfo = pathinfo( $file );
- $extension = NULL;
- if ( isset ( $pinfo[ 'extension' ] ) ) {
- $extension = $pinfo[ 'extension' ];
- }
- $cache = $this->cache_dir . DIRECTORY_SEPARATOR . $basename . '.' . $extension;
- return $cache;
- }
- function get_smarty_template ( &$ctx, $data = NULL, $basename, $filemtime = NULL ) {
- $template = $ctx->_get_compile_path( 'var:' . $basename );
- if (! isset( $data ) ) {
- $blog = $ctx->stash( 'blog' );
- // if ( isset ( $blog ) ) {
- // $template = $blog->site_path() . DIRECTORY_SEPARATOR . $template;
- // } else {
- if (! $this->no_database ) {
- $ctx->force_compile = TRUE;
- }
- $template = dirname( $this->templates_c ) . DIRECTORY_SEPARATOR . $template;
- // }
- }
- if ( $filemtime && file_exists( $template ) ) {
- if ( $filemtime > filemtime( $template ) ) {
- $ctx->force_compile = TRUE;
- }
- }
- return $template;
- }
- function init_plugin_dir () {
- if ( $this->stash( 'init_plugin_dir' ) ) {
- return;
- }
- require_once( 'dynamicmtml.util.php' );
- require_once( 'class.dynamicmtml_plugin.php' );
- $plugins_config = array(); // Config from config.yaml or config.php
- // 'dynamicmtml' => array( 'id' => 'DynamicMTML', ...)
- $plugins_dir_path = array(); // 'dynamicmtml' => '/path/to/DynamicMTML.pack'
- $callback_dir = array(); // '/path/to/DynamicMTML.pack/php/callbacks', ...
- $l10n_dir = array(); // '/path/to/DynamicMTML.pack/php/l10n', ...
- $publisher_dir = array(); // '/path/to/DynamicMTML.pack/php/publishers', ...
- $callbacks = array(); // array ( 'post_init' => array( 'pluginkey' => 'routine_name' ), ...
- $blocks = array(); // array ( 'tagname' => array( 'pluginkey' => 'hdlr_name' ), ...
- $functions = array(); // array ( 'tagname' => array( 'pluginkey' => 'hdlr_name' ), ...
- $modifiers = array(); // array ( 'tagname' => array( 'pluginkey' => 'hdlr_name' ), ...
- $pluginpath = $this->config( 'pluginpath' );
- $spyc = __cat_file( array( dirname( __FILE__ ), 'extlib', 'spyc', 'spyc.php' ) );
- if ( file_exists( $spyc ) ) {
- require_once( $spyc );
- } else {
- $spyc = NULL;
- }
- sort( $pluginpath );
- foreach ( $pluginpath as $plugin_dir ) {
- if ( is_dir( $plugin_dir ) ) {
- if ( $dh = opendir( $plugin_dir ) ) {
- while ( ( $dir = readdir( $dh ) ) !== FALSE ) {
- if (! preg_match ( '/^\./', $dir ) ) {
- $plugin_base = $plugin_dir . DIRECTORY_SEPARATOR . $dir;
- $plugin = NULL;
- $config = array();
- if ( is_dir( $plugin_base ) ) {
- $split_dirs = explode( DIRECTORY_SEPARATOR, $plugin_base );
- $plugin_class = $split_dirs[ count( $split_dirs ) - 1 ];
- $plugin_key = strtolower( $split_dirs[ count( $split_dirs ) - 1 ] );
- if ( preg_match( '/\.pack$/', $plugin_class ) ) {
- $plugin_class = strtr( $plugin_class, '.', '_' );
- $plugin_key = preg_replace( '/\.pack$/', '', $plugin_key );
- }
- $plugins_dir_path[ $plugin_key ] = $plugin_base;
- $plugin_php_dir = $plugin_base . DIRECTORY_SEPARATOR . 'php';
- $config_php = $plugin_php_dir . DIRECTORY_SEPARATOR . 'config.php';
- $config_yaml = $plugin_base . DIRECTORY_SEPARATOR . 'config.yaml';
- if ( file_exists( $config_yaml ) ) {
- if ( $spyc ) {
- $config = Spyc::YAMLLoad( $config_yaml );
- if ( $config ) {
- $config[ 'plugin_path' ] = $plugin_base;
- if (! file_exists( $config_php ) ) {
- $config = $this->__adjust_callbacks( $config );
- }
- $plugins_config[ $plugin_key ] = $config;
- } else {
- $config = array();
- }
- }
- }
- if ( is_dir( $plugin_php_dir ) ) {
- if ( file_exists( $config_php ) ) {
- require_once( $config_php );
- $plugin_registry = NULL;
- if ( class_exists( $plugin_class ) ) {
- $plugin = new $plugin_class;
- $plugin->app = $this;
- if ( isset( $plugin->registry ) ) {
- $plugin_registry = $plugin->registry;
- }
- // $config = $plugin->registry;
- } else {
- $cfg_key = 'mt_plugin_' . $plugin_key;
- if ( isset( $$cfg_key ) ) {
- $plugin_registry = $$cfg_key;
- }
- // $config = $$cfg_key;
- }
- if ( $plugin_registry ) {
- if (! $config ) {
- $config = $plugin_registry;
- } else {
- foreach ( $plugin_registry as $key => $val ) {
- $config[ $key ] = $val;
- }
- }
- }
- $config = $this->__adjust_callbacks( $config );
- $config[ 'plugin_path' ] = $plugin_base;
- $config[ 'plugin' ] = $plugin;
- // $plugins_config[ $plugin_key ] = $config;
- }
- if ( $config ) {
- if ( isset( $config[ 'callbacks' ] ) ) {
- $cb = $config[ 'callbacks' ];
- foreach ( $cb as $key => $val ) {
- if ( isset( $callbacks[ $key ] ) ) {
- $cbs = $callbacks[ $key ]; //array
- $cbs[ $plugin_key ] = $val;
- $callbacks[ $key ] = $cbs;
- } else {
- $callbacks[ $key ] = array( $plugin_key => $val );
- }
- }
- }
- if ( isset( $plugin ) ) {
- if ( is_object( $plugin ) ) {
- foreach ( $config as $cfg => $value ) {
- $plugin->$cfg = $value;
- }
- $plugin->app = $this;
- }
- }
- $config[ 'plugin_key' ] = $plugin_key;
- $plugins_config[ $plugin_key ] = $config;
- if ( isset( $config[ 'tags' ] ) ) {
- $this->__adjust_tags( $config, $blocks, $functions, $modifiers );
- }
- }
- $plugin_callback_dir = $plugin_php_dir . DIRECTORY_SEPARATOR . 'callbacks';
- if ( is_dir( $plugin_callback_dir ) ) {
- array_push( $callback_dir, $plugin_callback_dir );
- }
- $plugin_l10n_dir = $plugin_php_dir . DIRECTORY_SEPARATOR . 'l10n';
- if ( is_dir( $plugin_l10n_dir ) ) {
- array_push( $l10n_dir, $plugin_l10n_dir );
- }
- $plugin_publisher_dir = $plugin_php_dir . DIRECTORY_SEPARATOR . 'publishers';
- if ( is_dir( $plugin_publisher_dir ) ) {
- array_push( $publisher_dir, $plugin_publisher_dir );
- }
- }
- }
- }
- }
- }
- }
- }
- $this->stash( 'plugins_config', $plugins_config );
- $this->stash( 'plugins_directories', $plugins_dir_path );
- $this->stash( 'callback_dir', $callback_dir );
- $this->stash( 'l10n_dir', $l10n_dir );
- $this->stash( 'publisher_dir', $publisher_dir );
- $this->stash( 'plugins_callbacks', $callbacks );
- $this->stash( 'block_tags', $blocks );
- $this->stash( 'function_tags', $functions );
- $this->stash( 'modifiers', $modifiers );
- $this->stash( 'init_plugin_dir', 1 );
- }
- function __adjust_tags ( $config, &$blocks, &$functions, &$modifiers ) {
- $plugin_key = $config[ 'plugin_key' ];
- if ( isset( $config[ 'tags' ] ) ) {
- $config_tags = $config[ 'tags' ];
- if ( is_array( $config_tags ) ) {
- foreach ( $config_tags as $kind => $tags ) {
- if ( is_array( $tags ) ) {
- foreach ( $tags as $tag => $funk ) {
- $tag = strtolower( $tag );
- if ( preg_match( '/::/', $funk ) ) {
- $path = explode( '::', $funk );
- $funk = $path[ count( $path ) - 1 ];
- }
- if ( $kind === 'block' ) {
- $tag = preg_replace( '/\?$/', '', $tag );
- $blocks[ $tag ] = array( $plugin_key => $funk );
- } elseif ( $kind === 'function' ) {
- $functions[ $tag ] = array( $plugin_key => $funk );
- } elseif ( $kind === 'modifier' ) {
- $modifiers[ $tag ] = array( $plugin_key => $funk );
- }
- }
- }
- }
- }
- }
- }
- function __adjust_callbacks ( $config ) {
- if ( isset( $config[ 'callbacks' ] ) ) {
- $new_config = array();
- $cb = $config[ 'callbacks' ];
- foreach ( $cb as $key => $val ) {
- if ( is_array( $val ) ) {
- if ( isset( $val[ 'handler' ] ) ) {
- $val = $val[ 'handler' ];
- } else {
- $val = NULL;
- }
- }
- if ( $val ) {
- if ( preg_match( '/::/', $val ) ) {
- $path = explode( '::', $val );
- $val = $path[ count( $path ) - 1 ];
- }
- $new_config[ $key ] = $val;
- }
- }
- if ( $new_config ) {
- $config[ 'callbacks' ] = $new_config;
- } else {
- unset( $config[ 'callbacks' ] );
- }
- }
- return $config;
- }
- function run_tasks ( $task = NULL, $prefix = 'tasks', $sleep = NULL ) {
- $do;
- require_once( 'dynamicmtml.util.php' );
- $mt_dir = dirname( $this->cfg_file );
- $mt_dir = preg_replace( '/[^A-Za-z0-9]+/', '_', $mt_dir );
- $lock_name = "mt-tasks-{$mt_dir}.lock";
- $temp_dir = $this->config( 'TempDir' );
- if (! $temp_dir ) {
- $temp_dir = __cat_dir( $this->powercms_files_dir, 'lock' ) ;
- }
- $lock_file = __cat_file( $temp_dir, $lock_name );
- if ( file_exists( $lock_file ) ) {
- $mtime = filemtime( $lock_file );
- $frequency = time() - $mtime;
- if ( $frequency < 86400 ) {
- $msg = $this->translate( 'The process is skipped the lock file [_1] exists.', $lock_file );
- $args = array( 'categoey' => $prefix,
- 'level' => 4,
- 'class' => 'system',
- );
- $this->log( $msg, $args );
- return 0;
- }
- }
- $plugins_config = $this->stash( 'plugins_config' );
- $do_tasks = array();
- if (! isset( $plugins_config ) ) {
- return 0;
- }
- if (! is_array( $plugins_config ) ) {
- return 0;
- }
- if (! touch( $lock_file ) ) {
- $msg = $this->translate( 'Unable to secure lock for executing system tasks. Make sure your TempDir location ([_1]) is writable.', $temp_dir );
- $args = array( 'categoey' => $prefix,
- 'level' => 4,
- 'class' => 'system',
- );
- $this->log( $msg, $args );
- return 0;
- }
- foreach ( $plugins_config as $config ) {
- if ( isset( $config[ $prefix ] ) ) {
- $plugins_tasks = $config[ $prefix ];
- foreach ( $plugins_tasks as $plugin_key => $settings ) {
- if ( (! $task ) || ( $task === $plugin_key ) ) {
- $label = isset ( $settings[ 'label' ] ) ?
- $label = $this->translate( $settings[ 'label' ] ) : $label = $plugin_key;
- $plugin = NULL;
- $code = NULL;
- $plugin = $config[ 'plugin' ];
- if ( $plugin && is_object( $plugin ) ) {
- $code = $settings[ 'code' ];
- if ( $code ) {
- if ( preg_match( '/::/', $code ) ) {
- $path = explode( '::', $code );
- $code = $path[ count( $path ) - 1 ];
- }
- if (! method_exists( $plugin, $code ) ) {
- $plugin = NULL;
- }
- } else {
- $plugin = NULL;
- }
- }
- if (! $plugin ) {
- $plugin = __cat_file( $config[ 'plugin_path' ],
- array( 'php', $prefix, "{$plugin_key}.php" ) );
- }
- if ( $plugin ) {
- if ( $do ) {
- if ( $sleep ) sleep ( $sleep );
- }
- if ( $prefix === 'tasks' ) {
- $frequency = isset ( $settings[ 'frequency' ] ) ?
- $frequency = $settings[ 'frequency' ] : $frequency = 86400;
- if ( $plugin_key === 'FuturePost' ) {
- $futurepostfrequency = $this->config( 'FuturePostFrequency' );
- if ( $futurepostfrequency ) {
- $frequency = $futurepostfrequency;
- $frequency *= 60;
- }
- }
- $session = $this->get_by_key( 'Session', array( 'id' => "Task:{$plugin_key}",
- 'kind' => 'PT' ) );
- $start = $session->start;
- if ( (! $start ) || ( ( time() - $start ) > $frequency ) ) {
- $run_task = FALSE;
- $task_error = FALSE;
- if ( is_object ( $plugin ) ) {
- if ( $code ) {
- try {
- if ( $do = $plugin->$code( $this ) ) {
- array_push( $do_tasks, $label );
- }
- $run_task = TRUE;
- } catch ( Exception $e ) {
- $task_error = $e->getMessage();
- }
- }
- } else {
- if ( file_exists( $plugin ) ) {
- require_once( $plugin );
- }
- $function = "{$prefix}_{$plugin_key}";
- if ( function_exists( $function ) ) {
- try {
- if ( $do = $function( $this ) ) {
- array_push( $do_tasks, $label );
- }
- $run_task = TRUE;
- } catch ( Exception $e ) {
- $task_error = $e->getMessage();
- }
- }
- }
- if ( $run_task ) {
- $session->start = time();
- if ( $session->_saved ) {
- $session->Update();
- } else {
- $session->Save();
- }
- }
- }
- } elseif ( $prefix === 'task_workers' ) {
- $class = isset ( $settings[ 'class' ] ) ?
- $class = $settings[ 'class' ] : $class = NULL;
- $jobs = $this->load( 'Ts_Job', array( 'name' => $class ) );
- if ( is_object ( $plugin ) ) {
- if ( $code && $jobs ) {
- try {
- if ( $do = $plugin->$code( $this, $jobs ) ) {
- array_push( $do_tasks, $label );
- }
- $run_task = TRUE;
- } catch ( Exception $e ) {
- $task_error = $e->getMessage();
- }
- }
- } else {
- if ( file_exists( $plugin ) ) {
- require_once( $plugin );
- }
- $function = "{$prefix}_{$plugin_key}";
- if ( function_exists( $function ) ) {
- if ( $jobs ) {
- try {
- if ( $do = $function( $this, $jobs ) ) {
- array_push( $do_tasks, $label );
- }
- $run_task = TRUE;
- } catch ( Exception $e ) {
- $task_error = $e->getMessage();
- }
- }
- }
- }
- }
- if ( $task_error ) {
- $msg = $this->translate( 'Error during task \'[_1]\': [_2]', array( $label, $task_error ) );
- $args = array( 'categoey' => $prefix,
- 'level' => 4,
- 'class' => 'system',
- );
- $this->log( $msg, $args );
- }
- }
- }
- }
- }
- }
- if ( $do_tasks ) {
- $msg = $this->translate( 'Scheduled Tasks Update' );
- $ts = gmdate( "Y-m-d H:i:s" );
- $metadata = "[{$ts}] " . $this->translate( 'The following tasks were run:' );
- $ts = gmdate( "Y-m-d H:i:s" );
- $metadata .= ' ' . join( ', ', $do_tasks );
- $args = array( 'categoey' => $prefix,
- 'level' => 1,
- 'class' => 'system',
- 'metadata' => $metadata,
- );
- $this->log( $msg, $args );
- }
- if ( file_exists( $lock_file ) ) {
- unlink ( $lock_file );
- }
- return $do;
- }
- function run_workers ( $sleep = 5, $worker = NULL ) {
- return $this->run_tasks( $worker, 'task_workers', $sleep );
- }
- public function &context () {
- // TODO:
- require_once( 'dynamicmtml.util.php' );
- $ctx = $this->ctx;
- if ( isset( $ctx ) ) return $ctx;
- $mtphpdir = $this->config( 'PHPDir' );
- $mtlibdir = $this->config( 'PHPLibDir' );
- require_once( 'MTViewer.php' );
- $ctx = new MTViewer( $this );
- $ctx->mt =& $this;
- $ctx->compile_check = 1;
- $ctx->caching = FALSE;
- $ctx->plugins_dir[] = $mtlibdir;
- $ctx->plugins_dir[] = $mtphpdir . DIRECTORY_SEPARATOR . "plugins";
- if ( $this->debugging ) {
- $ctx->debugging_ctrl = 'URL';
- $ctx->debug_tpl = __cat_file( $mtphpdir, array( 'extlib', 'smarty', 'libs', 'debug.tpl' ) );
- }
- return $ctx;
- }
- function run_callbacks ( $callback, $mt = NULL, &$ctx = NULL, &$args = NULL, &$content = NULL ) {
- $class = get_class( $this );
- $callback_dir = array();
- $plugins_callbacks = array();
- if ( $class != 'DynamicMTML' ) {
- $callback_dir = $this->app->stash( 'callback_dir' );
- $plugins_callbacks = $this->app->stash( 'plugins_callbacks' );
- } else {
- $callback_dir = $this->stash( 'callback_dir' );
- $plugins_callbacks = $this->stash( 'plugins_callbacks' );
- }
- $do_filter = TRUE;
- if (! $args ) {
- $args = $this->get_args();
- }
- $orig_contenttype = NULL;
- if ( isset ( $args ) ) {
- if ( isset( $args[ 'contenttype' ] ) ) {
- $orig_contenttype = $args[ 'contenttype' ];
- }
- }
- if ( isset( $ctx ) ) {
- $ctx->stash( 'bootstrapper', $this );
- }
- if ( is_array( $plugins_callbacks ) ) {
- if ( isset( $plugins_callbacks[ $callback ] ) ) {
- $callbacks = $plugins_callbacks[ $callback ];
- if ( $callbacks ) {
- foreach ( $callbacks as $plugin => $method ) {
- $component = $this->component( $plugin );
- if ( method_exists( $component, $method ) ) {
- $res = $component->$method( $mt, $ctx, $args, $content );
- if (! $res ) {
- $do_filter = FALSE;
- }
- if ( isset ( $mt ) ) {
- $this->set_args_all( $args );
- }
- }
- }
- }
- }
- }
- if ( is_array( $callback_dir ) ) {
- foreach ( $callback_dir as $plugin_callback_dir ) {
- if ( is_dir( $plugin_callback_dir ) ) {
- $dirs = explode( DIRECTORY_SEPARATOR, $plugin_callback_dir );
- $plugin = strtolower( $dirs[ count( $dirs ) - 3 ] );
- $function = $plugin . '_' . $callback;
- $function = strtr( $function, '.', '_' );
- $require = $plugin_callback_dir . DIRECTORY_SEPARATOR . $function . '.php';
- if ( file_exists( $require ) ) {
- require_once $require;
- $res = $function( $mt, $ctx, $args, $content );
- if (! $res ) {
- $do_filter = FALSE;
- }
- if ( isset ( $mt ) ) {
- $this->set_args_all( $args );
- }
- }
- }
- }
- }
- if ( isset( $ctx ) ) {
- if ( isset( $orig_contenttype ) ) {
- if ( $orig_contenttype != $args[ 'contenttype' ] ) {
- $ctx->stash( 'content_type', $args[ 'contenttype' ] );
- }
- }
- $ctx->stash( 'bootstrapper', $this );
- }
- return $do_filter;
- }
- function set_args_all ( &$args ) {
- foreach ( $args as $key => $val ) {
- $args[ $key ] = $this->stash( $key );
- global $$key;
- $$key = $args[ $key ];
- }
- }
- function set_args ( $args, $param, $set = NULL ) {
- if ( isset( $set ) ) {
- global $$set;
- $$set = $args[ $param ];
- } else {
- global $$param;
- $$param = $args[ $param ];
- }
- }
- function echo_file_get_contents ( $file, $limit = 524288, $buff = 8192 ) {
- if ( filesize( $file ) < $limit ) {
- echo file_get_contents( $file );
- } else {
- $fp = fopen( $file, "r" );
- while (! feof( $fp ) ) {
- echo fread( $fp, $buff );
- }
- fclose( $fp );
- }
- }
- function adjust_file ( $file, $indexes, $original, $replace ) {
- if ( DIRECTORY_SEPARATOR != '/' ) {
- $file = str_replace( '\\\\', '\\', $file );
- } else {
- $file = str_replace( '//', '/', $file );
- }
- $file = strtr( $file, '../', '' );
- if ( DIRECTORY_SEPARATOR != '/' ) {
- $file = strtr( $file, '/', DIRECTORY_SEPARATOR );
- }
- if ( is_dir ( $file ) ) {
- foreach ( explode( ',', $indexes ) as $index ) {
- if ( file_exists( $file . $index ) ) {
- $file = $file . $index;
- break;
- } elseif ( file_exists( urldecode( $file ) . $index ) ) {
- $file = urldecode( $file ) . $index;
- break;
- }
- }
- } elseif ( file_exists( $file ) ) {
- $file = $file;
- } elseif ( file_exists( urldecode( $file ) ) ) {
- $file = urldecode( $file );
- }
- if ( $original && $replace ) {
- $file = preg_replace( '/' . preg_quote( $original, '/' ) . '/', $replace, $file );
- }
- if ( preg_match( '/' . preg_quote( DIRECTORY_SEPARATOR, '/' ) . '$/', $file ) ) {
- $file = preg_replace( '/\/$/', '', $file );
- if ( is_dir ( $file ) ) {
- $file = $file . DIRECTORY_SEPARATOR . 'index.html';
- }
- }
- $file = urldecode( $file );
- if ( DIRECTORY_SEPARATOR != '/' ) {
- $file = strtr( $file, '\\\\', '\\' );
- } else {
- $file = strtr( $file, '//', '/' );
- }
- return $file;
- }
- function check_excludes ( $file, $excludes = 'php', $mt_dir = NULL, $static_path = NULL ) {
- $self = $_SERVER[ 'SCRIPT_FILENAME' ];
- $self = preg_quote( $self, '/' );
- if ( preg_match( "/^$self/i", $file ) ) {
- exit();
- }
- if ( $mt_dir && $static_path ) {
- $mt_dir = $this->__add_slash( $mt_dir );
- $check_mt = preg_quote( $mt_dir, '/' );
- $check_static = preg_quote( $static_path, '/' );
- if ( preg_match( "/^$check_mt/i", $file ) ) {
- if (! preg_match( "/^$check_static/i", $file ) ) {
- exit();
- }
- }
- }
- $basename = explode(DIRECTORY_SEPARATOR, $file);
- $basename = end($basename);
- if ($basename[0] === '.') {
- exit();
- }
- foreach ( explode( ',', $excludes ) as $extension ) {
- if ( preg_match( "/\.$extension\$/", $basename ) ) {
- exit();
- }
- }
- }
- function __add_slash ( $path, $add = TRUE ) {
- $sep = preg_quote( DIRECTORY_SEPARATOR, '/' );
- $path = preg_replace( "/$sep$/", '', $path );
- if ( $add ) {
- $path .= DIRECTORY_SEPARATOR;
- }
- return $path;
- }
- function include_php ( $file ) {
- if ( file_exists( $file ) ) {
- if ( preg_match( "/\.php$/", $file ) ) {
- include( $file );
- exit();
- }
- }
- }
- function get_mime_type ( $extension ) {
- $extension = preg_replace( '/^\./', '', $extension );
- if ( isset( $_SERVER[ 'HTTP_USER_AGENT' ] ) ) {
- if ( preg_match( '/\ADoCoMo\/2\.0 /', $_SERVER[ 'HTTP_USER_AGENT' ] ) ) {
- if ( $extension === 'html' ) {
- return 'application/xhtml+xml';
- }
- }
- }
- $mime_type = array (
- 'css' => 'text/css',
- 'html' => 'text/html',
- 'mtml' => 'text/html',
- 'xhtml' => 'application/xhtml+xml',
- 'htm' => 'text/html',
- 'txt' => 'text/plain',
- 'rtx' => 'text/richtext',
- 'tsv' => 'text/tab-separated-values',
- 'csv' => 'text/csv',
- 'hdml' => 'text/x-hdml; charset=Shift_JIS',
- 'xml' => 'application/xml',
- 'rdf' => 'application/rss+xml',
- 'xsl' => 'text/xsl',
- 'mpeg' => 'video/mpeg',
- 'mpg' => 'video/mpeg',
- 'mpe' => 'video/mpeg',
- 'qt' => 'video/quicktime',
- 'avi' => 'video/x-msvideo',
- 'movie' => 'video/x-sgi-movie',
- 'qt' => 'video/quicktime',
- 'ice' => 'x-conference/…
Large files files are truncated, but you can click here to view the full file