Permalink
Browse files

Added some feature.

  • Loading branch information...
1 parent d6c3136 commit ef51b3283d70236ce6f6c11138469a04167b39a6 @usualoma committed Mar 4, 2011
Showing with 73 additions and 7 deletions.
  1. +7 −2 README.md
  2. +9 −0 config.yaml
  3. +48 −5 lib/EnhancedInclude.pm
  4. +3 −0 lib/EnhancedInclude/L10N/ja.pm
  5. +6 −0 tmpl/blog_config.tmpl
View
@@ -7,15 +7,20 @@ A Plugin that add some modifiers to mt:Include.
* Include, and cache execution result of command.
* Include, and cache other site's resource.
+## INSTALATION
+
+* Upload plugin's files.
+* Configure blog settings if using "execute" attributes.
+
## SYNOPSIS
### execute
- <mt:Include execute="php" params="$filename","$arg1","$arg2" ttl="3600" />
+ <mt:Include execute="php","$filename","$arg1","$arg2" ttl="3600" />
### url
<mt:Include url="http://localhost/some/part.php" ttl="3600" />
## LICENSE
-Copyright (c) 2011 Taku AMANO
+Copyright (c) 2011 ToI Inc.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
View
@@ -13,6 +13,15 @@ doc_link: http://tec.toi-planning.net/mt/enhancedinclude/manual
#Localization
l10n_class: EnhancedInclude::L10N
+settings:
+ allow_execute:
+ default: ""
+
+blog_config_template: blog_config.tmpl
+
tags:
function:
Include: $EnhancedInclude::EnhancedInclude::_hdlr_include
+ modifier:
+ pipe:
+ handler: $EnhancedInclude::EnhancedInclude::_fltr_pipe
View
@@ -23,10 +23,20 @@
package EnhancedInclude;
use Digest::MD5 qw( md5_hex );
+use IPC::Open2;
use strict;
use warnings;
+sub _get_blog_id {
+ my ($ctx) = @_;
+ $ctx->stash('blog_id') or do {
+ if (my $blog = $ctx->stash('blog')) {
+ $blog->id
+ }
+ };
+}
+
sub _hdlr_include {
my ($ctx, $arg, $cond) = @_;
@@ -45,18 +55,25 @@ sub _hdlr_include {
sub _hdlr_include_execute {
my ($ctx, $arg, $cond) = @_;
+ my $blog_id = &_get_blog_id($ctx)
+ or return '';
+ my $plugin = MT->component('EnhancedInclude');
+ if (! $plugin->get_config_value('allow_execute', 'blog:' . $blog_id)) {
+ return $ctx->error($plugin->translate(
+ '"execute" attribute is not allowed.'
+ ));
+ }
my $exec = $arg->{execute};
- my $cache_key = md5_hex('include_execute::' . $exec);
- my $params = $arg->{params} || [];
- if (! ref $params) {
- $params = [ $params ];
+ if (! ref $exec) {
+ $exec = [ $exec ];
}
+ my $cache_key = md5_hex('include_execute::' . join('', @$exec));
if (my $cached = &_get_cache($cache_key, @_)) {
return $cached;
}
- my $value = do{ open(my $fh, '-|', $exec, @$params); local $/; <$fh> };
+ my $value = do{ open(my $fh, '-|', @$exec); local $/; <$fh> };
&_set_cache($cache_key, $value, @_);
}
@@ -114,4 +131,30 @@ sub _set_cache {
$value;
}
+sub _fltr_pipe {
+ my ($str, $args, $ctx) = @_;
+ if (! ref $args) {
+ $args = [ $args ];
+ }
+ my $plugin = MT->component('EnhancedInclude');
+ my $blog_id = &_get_blog_id($ctx)
+ or return '';
+ if (! $plugin->get_config_value('allow_execute', 'blog:' . $blog_id)) {
+ die $plugin->translate('"execute" attribute is not allowed.');
+ }
+
+ my $pid = open2(my $out, my $in, @$args);
+
+ print($in $str);
+ close($in);
+
+ local $/;
+ my $content = <$out>;
+ close($out);
+
+ waitpid($pid, 0);
+
+ $content;
+}
+
1;
@@ -8,6 +8,9 @@ use vars qw( %Lexicon );
%Lexicon = (
'toi-planning' => 'ToI企画',
+ 'Extends mt:Include' => 'mt:Includeを拡張します。',
+ '"execute" attribute is not allowed.' => '"execute"モディファイアの利用は許可されていません。',
+ 'Allow to use execute' => '"execute"モディファイアの利用を許可する',
);
1;
View
@@ -0,0 +1,6 @@
+<mtapp:setting
+ id="include_allow_execute"
+ label="<__trans phrase="Allow to use execute">"
+>
+<input id="include_allow_execute" name="allow_execute" type="checkbox" value="1" <mt:If name="allow_execute"> checked="checked" </mt:If>" />
+</mtapp:setting>

0 comments on commit ef51b32

Please sign in to comment.