@@ -304,7 +304,7 @@ function plugin_attach_action()
304304 @ unlink ($ _file ['tmp_name ' ]);
305305 $ this ->output_json ($ ret ['msg ' ]);
306306 }
307- $ this ->output_json (0 ); // return success
307+ $ this ->output_json (0 , '' , (! empty ( $ ret [ ' has_json_msg ' ]) && ! empty ( $ ret [ ' msg ' ]))? $ ret [ ' msg ' ] : '' ); // return success
308308 }
309309
310310 // normal upload (non d&d)
@@ -482,6 +482,8 @@ function do_upload($page,$fname,$tmpname,$copyright=FALSE,$pass=NULL,$notouch=FA
482482
483483 $ overwrite = (!empty ($ options ['overwrite ' ]));
484484 $ changelog = (isset ($ options ['changelog ' ]))? $ options ['changelog ' ] : '' ;
485+ $ add_mes = array ();
486+ $ has_json_msg = false ;
485487
486488 // ファイル名の正規化
487489 $ fname = str_replace ("\0" , '' , $ fname );
@@ -492,36 +494,43 @@ function do_upload($page,$fname,$tmpname,$copyright=FALSE,$pass=NULL,$notouch=FA
492494 // style.css
493495 if ($ fname === 'style.css ' && $ this ->func ->is_owner ($ page ))
494496 {
495- if ( is_uploaded_file ($ tmpname ) )
497+ if ( is_file ($ tmpname ) )
496498 {
497499 $ _pagecss_file = $ this ->cont ['CACHE_DIR ' ].$ this ->func ->get_pgid_by_name ($ page ).".css " ;
498500 if (is_file ($ _pagecss_file )) unlink ($ _pagecss_file );
499- if (move_uploaded_file ($ tmpname ,$ _pagecss_file ))
501+ if (( is_uploaded_file ( $ tmpname ) && move_uploaded_file ( $ tmpname , $ _pagecss_file )) || @ rename ($ tmpname ,$ _pagecss_file ))
500502 {
501503 $ this ->attach_chmod ($ _pagecss_file );
502504 // 空のファイルの場合はファイル削除
503505 if (!trim (file_get_contents ($ _pagecss_file )))
504506 {
505507 unlink ($ _pagecss_file );
506- return array ('result ' =>TRUE ,'msg ' =>$ this ->root ->_attach_messages ['msg_unset_css ' ]);
508+ return array ('result ' =>TRUE ,'msg ' =>$ this ->root ->_attach_messages ['msg_unset_css ' ], ' has_json_msg ' => TRUE );
507509 }
508510 else
509511 {
510- // 外部ファイルの参照を禁止するなどの書き換え
511512 $ _data = file_get_contents ($ _pagecss_file );
512- $ _data = preg_replace ('#(?:(ht|f)tps?://|boudary)#i ' , '' ,$ _data );
513- if ($ fp = fopen ($ _pagecss_file ,"wb " ))
514- {
515- fputs ($ fp ,$ _data );
516- fclose ($ fp );
513+ // 管理者以外は外部ファイルの参照を禁止するなどの書き換え
514+ if (! $ this ->root ->userinfo ['admin ' ]) {
515+ $ _data = preg_replace ('#(?:url\s*\(\s*[ \'"]?(?:(?:ht|f)tps?:)?//[^\)]+?\)|@import[^;\r\n]*?;|@import|(?:ht|f)tps?://)#i ' , '' ,$ _data );
517516 }
518-
519- return array ('result ' =>TRUE ,'msg ' =>$ this ->root ->_attach_messages ['msg_set_css ' ]);
517+ if (file_put_contents ($ _pagecss_file , $ _data )) {
518+ $ add_mes [] = $ this ->root ->_attach_messages ['msg_set_css ' ];
519+ $ has_json_msg = true ;
520+ }
521+ // 元ファイルを添付ファイルとして保存
522+ if ($ tmpname = tempnam ($ this ->cont ['CACHE_DIR ' ], 'atf ' )) {
523+ file_put_contents ($ tmpname , $ _data );
524+ $ overwrite = true ;
525+ }
526+ clearstatcache ();
520527 }
521528 }
522529 else
530+ {
531+ @unlink ($ tmpname );
523532 return array ('result ' =>FALSE ,'msg ' =>$ this ->root ->_attach_messages ['err_exists ' ]);
524-
533+ }
525534 }
526535 }
527536
@@ -580,35 +589,30 @@ function do_upload($page,$fname,$tmpname,$copyright=FALSE,$pass=NULL,$notouch=FA
580589 } while ($ obj ->exist );
581590 } else {
582591 $ obj = new XpWikiAttachFile ($ this ->xpwiki , $ page , $ fname );
592+ if (is_file ($ obj ->filename )) {
593+ unlink ($ obj ->filename );
594+ $ _action = "update " ;
595+ }
583596 }
584597
585598 if ( is_uploaded_file ($ tmpname ) ) {
586- if ($ obj ->exist )
587- {
588- return array ('result ' =>FALSE ,'msg ' =>$ this ->root ->_attach_messages ['err_exists ' ]);
589- }
590-
591599 if (move_uploaded_file ($ tmpname ,$ obj ->filename )) {
592600 $ this ->attach_chmod ($ obj ->filename );
593601 } else {
594- return array ('result ' =>FALSE ,'msg ' =>$ this ->root ->_attach_messages ['err_exists ' ]);
602+ return array ('result ' =>FALSE ,'msg ' =>$ this ->root ->_attach_messages ['err_noexist ' ]);
595603 }
596604 } else {
597605 if (! is_file ($ tmpname ) || ! filesize ($ tmpname )) {
598606 if (is_file ($ tmpname )) {
599607 unlink ($ tmpname );
600608 }
601- return array ('result ' =>FALSE , 'msg ' =>$ this ->root ->_attach_messages ['err_exists ' ]);
602- }
603- if (is_file ($ obj ->filename )) {
604- unlink ($ obj ->filename );
605- $ _action = "update " ;
609+ return array ('result ' =>FALSE , 'msg ' =>$ this ->root ->_attach_messages ['err_noexist ' ]);
606610 }
607611 if (rename ($ tmpname ,$ obj ->filename )) {
608612 $ this ->attach_chmod ($ obj ->filename );
609613 } else {
610614 unlink ($ tmpname );
611- return array ('result ' =>FALSE , 'msg ' =>$ this ->root ->_attach_messages ['err_exists ' ]);
615+ return array ('result ' =>FALSE , 'msg ' =>$ this ->root ->_attach_messages ['err_noexist ' ]);
612616 }
613617 }
614618
@@ -653,19 +657,25 @@ function do_upload($page,$fname,$tmpname,$copyright=FALSE,$pass=NULL,$notouch=FA
653657
654658 return array (
655659 'result ' => TRUE ,
656- 'msg ' => $ this ->root ->_attach_messages ['msg_uploaded ' ],
657- 'name ' => $ obj ->file
660+ 'msg ' => $ add_mes ? join ("\n" , $ add_mes ) : $ this ->root ->_attach_messages ['msg_uploaded ' ],
661+ 'name ' => $ obj ->file ,
662+ 'has_json_msg ' => $ has_json_msg
658663 );
659664 }
660665
661- function output_json ($ msg = 0 , $ page = '' ) {
662- if (! $ msg ) {
663- $ this ->func ->send_json (array ('success ' => true ));
666+ function output_json ($ err = 0 , $ page = '' , $ msg = '' ) {
667+ $ ret = array ();
668+ if (! $ err ) {
669+ $ ret ['success ' ] = true ;
670+ $ err = $ msg ;
671+ }
672+ if ($ err ) {
673+ if (! $ page ) $ page = $ this ->root ->vars ['refer ' ];
674+ $ err = str_replace ('$1 ' , $ page , $ err );
675+ $ err = mb_convert_encoding ($ err , 'UTF-8 ' , $ this ->cont ['SOURCE_ENCODING ' ]);
676+ $ ret ['error ' ] = $ err ;
664677 }
665- if (! $ page ) $ page = $ this ->root ->vars ['refer ' ];
666- $ msg = str_replace ('$1 ' , $ page , $ msg );
667- $ msg = mb_convert_encoding ($ msg , 'UTF-8 ' , $ this ->cont ['SOURCE_ENCODING ' ]);
668- $ this ->func ->send_json (array ('error ' => $ msg ));
678+ $ this ->func ->send_json ($ ret );
669679 }
670680
671681 // ref プラグインのソース置換
0 commit comments