Skip to content

Commit

Permalink
[plugin:attach] fix problem of pagecss by "style.css" upload
Browse files Browse the repository at this point in the history
"style.css" をアプロードすることにより、ページ専用 CSS を適用できる機能が正常に働かなくなっていた問題の修正。

ref. http://xoops.hypweb.net/modules/forum/index.php?post_id=13551

:仕様変更|
~ 以前は、style.css をアップロードした場合は、ページへの添付ファイルとしては扱われず、ページ専用 CSS の設定のみ行われていましたが、今回より style.css として、ページの添付ファイルとしても保存されるようになりました。
~ ただし、ページ専用 CSS とページにファイルとして添付された style.css は全く別のものになりますので、ページの添付ファイルである style.css を削除しても、ページ専用 CSS には影響ありません。
~ 目的としては、ページ専用 CSS にどのような内容が設定されるかを知ることができるように、添付ファイルとしても残すようにしました。
~ また、以前は CSS 中での外部サイト画像の参照部分を一律、除外していましたが、今回より管理者以外の者がアップロードした場合のみ、外部URL等を除外するようにしました。
  • Loading branch information
nao-pon committed Mar 19, 2014
1 parent 230379c commit 3692ba4
Showing 1 changed file with 44 additions and 34 deletions.
78 changes: 44 additions & 34 deletions xoops_trust_path/modules/xpwiki/plugin/attach.inc.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ function plugin_attach_action()
@ unlink($_file['tmp_name']); @ unlink($_file['tmp_name']);
$this->output_json($ret['msg']); $this->output_json($ret['msg']);
} }
$this->output_json(0); // return success $this->output_json(0, '', (!empty($ret['has_json_msg']) && !empty($ret['msg']))? $ret['msg'] : ''); // return success
} }


// normal upload (non d&d) // normal upload (non d&d)
Expand Down Expand Up @@ -482,6 +482,8 @@ function do_upload($page,$fname,$tmpname,$copyright=FALSE,$pass=NULL,$notouch=FA


$overwrite = (!empty($options['overwrite'])); $overwrite = (!empty($options['overwrite']));
$changelog = (isset($options['changelog']))? $options['changelog'] : ''; $changelog = (isset($options['changelog']))? $options['changelog'] : '';
$add_mes = array();
$has_json_msg = false;


// ファイル名の正規化 // ファイル名の正規化
$fname = str_replace("\0", '', $fname); $fname = str_replace("\0", '', $fname);
Expand All @@ -492,36 +494,43 @@ function do_upload($page,$fname,$tmpname,$copyright=FALSE,$pass=NULL,$notouch=FA
// style.css // style.css
if ($fname === 'style.css' && $this->func->is_owner($page)) if ($fname === 'style.css' && $this->func->is_owner($page))
{ {
if ( is_uploaded_file($tmpname) ) if ( is_file($tmpname) )
{ {
$_pagecss_file = $this->cont['CACHE_DIR'].$this->func->get_pgid_by_name($page).".css"; $_pagecss_file = $this->cont['CACHE_DIR'].$this->func->get_pgid_by_name($page).".css";
if (is_file($_pagecss_file)) unlink($_pagecss_file); if (is_file($_pagecss_file)) unlink($_pagecss_file);
if (move_uploaded_file($tmpname,$_pagecss_file)) if ((is_uploaded_file($tmpname) && move_uploaded_file($tmpname,$_pagecss_file)) || @rename($tmpname,$_pagecss_file))
{ {
$this->attach_chmod($_pagecss_file); $this->attach_chmod($_pagecss_file);
// 空のファイルの場合はファイル削除 // 空のファイルの場合はファイル削除
if (!trim(file_get_contents($_pagecss_file))) if (!trim(file_get_contents($_pagecss_file)))
{ {
unlink($_pagecss_file); unlink($_pagecss_file);
return array('result'=>TRUE,'msg'=>$this->root->_attach_messages['msg_unset_css']); return array('result'=>TRUE,'msg'=>$this->root->_attach_messages['msg_unset_css'],'has_json_msg'=>TRUE);
} }
else else
{ {
// 外部ファイルの参照を禁止するなどの書き換え
$_data = file_get_contents($_pagecss_file); $_data = file_get_contents($_pagecss_file);
$_data = preg_replace('#(?:(ht|f)tps?://|boudary)#i', '',$_data); // 管理者以外は外部ファイルの参照を禁止するなどの書き換え
if ($fp = fopen($_pagecss_file,"wb")) if (! $this->root->userinfo['admin']) {
{ $_data = preg_replace('#(?:url\s*\(\s*[\'"]?(?:(?:ht|f)tps?:)?//[^\)]+?\)|@import[^;\r\n]*?;|@import|(?:ht|f)tps?://)#i', '',$_data);
fputs($fp,$_data);
fclose($fp);
} }

if (file_put_contents($_pagecss_file, $_data)) {
return array('result'=>TRUE,'msg'=>$this->root->_attach_messages['msg_set_css']); $add_mes[] = $this->root->_attach_messages['msg_set_css'];
$has_json_msg = true;
}
// 元ファイルを添付ファイルとして保存
if ($tmpname = tempnam($this->cont['CACHE_DIR'], 'atf')) {
file_put_contents($tmpname, $_data);
$overwrite = true;
}
clearstatcache();
} }
} }
else else
{
@unlink($tmpname);
return array('result'=>FALSE,'msg'=>$this->root->_attach_messages['err_exists']); return array('result'=>FALSE,'msg'=>$this->root->_attach_messages['err_exists']);

}
} }
} }


Expand Down Expand Up @@ -580,35 +589,30 @@ function do_upload($page,$fname,$tmpname,$copyright=FALSE,$pass=NULL,$notouch=FA
} while ($obj->exist); } while ($obj->exist);
} else { } else {
$obj = new XpWikiAttachFile($this->xpwiki, $page, $fname); $obj = new XpWikiAttachFile($this->xpwiki, $page, $fname);
if (is_file($obj->filename)) {
unlink($obj->filename);
$_action = "update";
}
} }


if ( is_uploaded_file($tmpname) ) { if ( is_uploaded_file($tmpname) ) {
if ($obj->exist)
{
return array('result'=>FALSE,'msg'=>$this->root->_attach_messages['err_exists']);
}
if (move_uploaded_file($tmpname,$obj->filename)) { if (move_uploaded_file($tmpname,$obj->filename)) {
$this->attach_chmod($obj->filename); $this->attach_chmod($obj->filename);
} else { } else {
return array('result'=>FALSE,'msg'=>$this->root->_attach_messages['err_exists']); return array('result'=>FALSE,'msg'=>$this->root->_attach_messages['err_noexist']);
} }
} else { } else {
if (! is_file($tmpname) || ! filesize($tmpname)) { if (! is_file($tmpname) || ! filesize($tmpname)) {
if (is_file($tmpname)) { if (is_file($tmpname)) {
unlink($tmpname); unlink($tmpname);
} }
return array('result'=>FALSE, 'msg'=>$this->root->_attach_messages['err_exists']); return array('result'=>FALSE, 'msg'=>$this->root->_attach_messages['err_noexist']);
}
if (is_file($obj->filename)) {
unlink($obj->filename);
$_action = "update";
} }
if (rename($tmpname,$obj->filename)) { if (rename($tmpname,$obj->filename)) {
$this->attach_chmod($obj->filename); $this->attach_chmod($obj->filename);
} else { } else {
unlink($tmpname); unlink($tmpname);
return array('result'=>FALSE, 'msg'=>$this->root->_attach_messages['err_exists']); return array('result'=>FALSE, 'msg'=>$this->root->_attach_messages['err_noexist']);
} }
} }


Expand Down Expand Up @@ -653,19 +657,25 @@ function do_upload($page,$fname,$tmpname,$copyright=FALSE,$pass=NULL,$notouch=FA


return array( return array(
'result' => TRUE, 'result' => TRUE,
'msg' => $this->root->_attach_messages['msg_uploaded'], 'msg' => $add_mes? join("\n", $add_mes) : $this->root->_attach_messages['msg_uploaded'],
'name' => $obj->file 'name' => $obj->file,
'has_json_msg' => $has_json_msg
); );
} }


function output_json($msg = 0, $page = '') { function output_json($err = 0, $page = '', $msg = '') {
if (! $msg) { $ret = array();
$this->func->send_json(array('success' => true)); if (! $err) {
$ret['success'] = true;
$err = $msg;
}
if ($err) {
if (! $page) $page = $this->root->vars['refer'];
$err = str_replace('$1', $page, $err);
$err = mb_convert_encoding($err, 'UTF-8', $this->cont['SOURCE_ENCODING']);
$ret['error'] = $err;
} }
if (! $page) $page = $this->root->vars['refer']; $this->func->send_json($ret);
$msg = str_replace('$1', $page, $msg);
$msg = mb_convert_encoding($msg, 'UTF-8', $this->cont['SOURCE_ENCODING']);
$this->func->send_json(array('error' => $msg));
} }


// ref プラグインのソース置換 // ref プラグインのソース置換
Expand Down

0 comments on commit 3692ba4

Please sign in to comment.