「上書き保存(UAC対応)」の版間の差分
ページの作成:「== 概要 == UAC に対応した「上書き保存」マクロです. == 変更履歴 == * 2013/1/27 初版 == 注意 == * 若干の副作用(制限)があります...」 |
バグ修正 |
||
| 1行目: | 1行目: | ||
== バグ == | |||
'''2013/2/1 以前までのコードにはセキュリティリスクのあるバグが含まれていました.'''<br> | |||
申し訳ありません. | |||
=== バグ詳細 === | |||
* 現象<br>ファイルのアクセス権限が書き換えられる.<br>結果として管理者権限がなくてもファイルを書き換えることが可能となる.<br>(実行時のユーザのアクセス権限がフルアクセスになる) | |||
* 条件<br>TEMP がユーザフォルダ配下にある(デフォルト状態) | |||
== 概要 == | == 概要 == | ||
UAC に対応した「上書き保存」マクロです. | UAC に対応した「上書き保存」マクロです. | ||
| 4行目: | 13行目: | ||
== 変更履歴 == | == 変更履歴 == | ||
* 2013/1/27 初版 | * 2013/1/27 初版 | ||
* 2013/2/2 バグ修正&UAC判定の強化 | |||
== 注意 == | == 注意 == | ||
| 22行目: | 32行目: | ||
* 改行コードは固定 | * 改行コードは固定 | ||
* 「外部でファイルが変更されています」のメッセージが表示される | * 「外部でファイルが変更されています」のメッセージが表示される | ||
* UAC 制御画面でキャンセルしても編集マーク(*)が消える(実際には保存されていない) | |||
<source lang="javascript"> | <source lang="javascript"> | ||
#title="上書き保存 | #title="上書き保存" | ||
// ----------------------------------------------------------------------------- | // ----------------------------------------------------------------------------- | ||
| 34行目: | 45行目: | ||
// ----------------------------------------------------------------------------- | // ----------------------------------------------------------------------------- | ||
var NEWLINE_CODE = "\r\n" | var NEWLINE_CODE = "\r\n" | ||
// | // 『無題』は通常処理 | ||
if (! | if (!Document.Name) { | ||
Document.Save(); | Document.Save(); | ||
Quit(); | Quit(); | ||
} | } | ||
// | // 変更がない場合は保存しない | ||
if (!Document. | if (Document.Saved) { | ||
Quit(); | |||
} | |||
// ファイルがない場合は通常処理(Mery準拠) | |||
var fso = new ActiveXObject("Scripting.FileSystemObject"); | |||
if (!fso.FileExists(Document.FullName)) { | |||
Document.Save(); | |||
Quit(); | Quit(); | ||
} | } | ||
// | |||
if ( | // 書き込み禁止か取得 | ||
var file = fso.GetFile(Document.FullName); | |||
if (file.Attributes & 0x01) { | |||
Alert("このファイルは読み取り専用です"); | |||
Quit(); | Quit(); | ||
} | } | ||
// | // UAC の判定 | ||
if (!IsNeedUAC(Document.FullName)) { | |||
Document.Save(); | |||
Quit(); | |||
if ( | |||
} | } | ||
// | // 一旦別のファイルに保存して,UAC対応コピー処理 | ||
var shell = new ActiveXObject("Wscript.Shell"); | var shell = new ActiveXObject("Wscript.Shell"); | ||
var folder = shell.ExpandEnvironmentStrings("%TEMP%"); | var folder = shell.ExpandEnvironmentStrings("%TEMP%"); | ||
var tempFilePath = fso.BuildPath(folder, Document.Name); | var tempFilePath = fso.BuildPath(folder, Document.Name); | ||
SaveToFile(tempFilePath, Document); | SaveToFile(tempFilePath, Document); | ||
var appShell = new ActiveXObject("Shell.Application"); | var appShell = new ActiveXObject("Shell.Application"); | ||
appShell.ShellExecute("cmd.exe", '/c | appShell.ShellExecute("cmd.exe", '/c COPY /Y "' + tempFilePath +'" "' + Document.FullName + '" & DEL "' + tempFilePath + '"', "", "runas", 2); | ||
UpdateSavedMark(true); | |||
//============================================================================== | |||
// 関数定義 | |||
// UAC が必要かを判定 | // UAC が必要かを判定 | ||
function | function IsNeedUAC(path) { | ||
if (!path) { | if (!path) { | ||
return false; | return false; | ||
} | |||
// 書き込み可能か確認 | |||
var fso = new ActiveXObject("Scripting.FileSystemObject"); | |||
try { | |||
fso.OpenTextFile(path, 8).Close(); | |||
return false; | |||
} catch (e) { | |||
// 排他制御中でも同じ結果を返すが判別できない | |||
} | } | ||
| 83行目: | 111行目: | ||
var oLocator = new ActiveXObject("WBemScripting.SWbemLocator"); | var oLocator = new ActiveXObject("WBemScripting.SWbemLocator"); | ||
var oService = oLocator.ConnectServer(); | var oService = oLocator.ConnectServer(); | ||
var os = new Enumerator(oService.ExecQuery("SELECT *FROM Win32_OperatingSystem")); | var os = new Enumerator(oService.ExecQuery("SELECT * FROM Win32_OperatingSystem")); | ||
if (Number(os.item().Version.substring(0, 3)) < 6.0) { | if (Number(os.item().Version.substring(0, 3)) < 6.0) { | ||
return false; | return false; | ||
} | } | ||
// | // ファイルの権限を確認 | ||
var SE_DACL_AUTO_INHERITED = 0x0400; | |||
if ( | var permission = new Enumerator(oService.ExecQuery("SELECT * FROM Win32_LogicalFileSecuritySetting Where Path='" + path.replace(/\\/g, "\\\\") + "'")); | ||
if (!permission.atEnd() && permission.item().ControlFlags & SE_DACL_AUTO_INHERITED) { | |||
return true; | return true; | ||
} | } | ||
| 99行目: | 128行目: | ||
// ファイル保存 | // ファイル保存 | ||
function SaveToFile(path, doc) { | function SaveToFile(path, doc) { | ||
var charset = "utf-8"; | |||
switch (doc.Encoding) { | |||
case meEncodingEUC: charset = "euc-JP"; break; | |||
case meEncodingShiftJIS: charset = "shift-jis"; break; | |||
case meEncodingUTF16LE: | |||
case meEncodingUTF16BE: charset = "unicode"; break; | |||
case meEncodingUTF7: charset = "utf-7"; break; | |||
case meEncodingUTF8WithSignature: | |||
case meEncodingUTF8WithoutSignature: charset = "utf-8"; break; | |||
} | } | ||
var adodb = new ActiveXObject("ADODB.Stream"); | var adodb = new ActiveXObject("ADODB.Stream"); | ||
adodb.Charset = charset; | adodb.Charset = charset; | ||
| 118行目: | 146行目: | ||
// BOM 削除処理 | // BOM 削除処理 | ||
if ( | if (doc.Encoding == meEncodingUTF8WithoutSignature) { | ||
adodb.Position = 0; | adodb.Position = 0; | ||
adodb.Type = 1; | adodb.Type = 1; | ||
| 132行目: | 160行目: | ||
adodb.Close(); | adodb.Close(); | ||
}; | }; | ||
// マークの更新 | |||
// すぐに反映されないので対処 | |||
function UpdateSavedMark(isSaved) { | |||
if (Document.Saved != isSaved) { | |||
Document.Saved = isSaved; | |||
var sel = Document.Selection; | |||
if (sel.GetActivePointX(mePosLogical) == 1 && sel.GetActivePointY(mePosLogical) == 1) { | |||
sel.CharLeft(); | |||
} else { | |||
sel.CharLeft(); | |||
sel.CharRight(); | |||
} | |||
} | |||
} | |||
</source> | </source> | ||
| 149行目: | 192行目: | ||
// ----------------------------------------------------------------------------- | // ----------------------------------------------------------------------------- | ||
// | // 『無題』は通常処理 | ||
if (! | if (!Document.Name) { | ||
Document.Save(); | Document.Save(); | ||
Quit(); | Quit(); | ||
} | } | ||
// | // 変更がない場合は保存しない | ||
if (!Document. | if (Document.Saved) { | ||
Quit(); | |||
} | |||
// ファイルがない場合は通常処理(Mery準拠) | |||
var fso = new ActiveXObject("Scripting.FileSystemObject"); | |||
if (!fso.FileExists(Document.FullName)) { | |||
Document.Save(); | |||
Quit(); | |||
} | |||
// 書き込み禁止か取得 | |||
var file = fso.GetFile(Document.FullName); | |||
if (file.Attributes & 0x01) { | |||
Alert("このファイルは読み取り専用です"); | |||
Quit(); | Quit(); | ||
} | } | ||
// | |||
if (Document. | // UAC の判定 | ||
if (!IsNeedUAC(Document.FullName)) { | |||
Document.Save(); | |||
Quit(); | Quit(); | ||
} | } | ||
var shell = new ActiveXObject("Wscript.Shell"); | var shell = new ActiveXObject("Wscript.Shell"); | ||
var folder = shell.ExpandEnvironmentStrings("%TEMP%"); | var folder = shell.ExpandEnvironmentStrings("%TEMP%"); | ||
var tempFilePath = fso.BuildPath(folder, Document.Name); | var tempFilePath = fso.BuildPath(folder, Document.Name); | ||
| 181行目: | 239行目: | ||
Document.Save(tempFilePath); | Document.Save(tempFilePath); | ||
var appShell = new ActiveXObject("Shell.Application"); | var appShell = new ActiveXObject("Shell.Application"); | ||
appShell.ShellExecute("cmd.exe", '/c | appShell.ShellExecute("cmd.exe", '/c COPY /Y "' + tempFilePath +'" "' + filePath + '" & DEL "' + tempFilePath + '"', "", "runas", 2); | ||
// | // ファイルムーブの完了待ち(最大 1 秒) | ||
for (var i=0; i<50; i++) { | for (var i=0; i<50; i++) { | ||
Sleep(20); | Sleep(20); | ||
| 201行目: | 259行目: | ||
doc.Selection.SetAnchorPoint(mePosLogical, bx, by); | doc.Selection.SetAnchorPoint(mePosLogical, bx, by); | ||
ScrollX = sx; ScrollY = sy; | ScrollX = sx; ScrollY = sy; | ||
//============================================================================== | |||
// 関数定義 | |||
// UAC が必要かを判定 | // UAC が必要かを判定 | ||
function | function IsNeedUAC(path) { | ||
if (!path) { | if (!path) { | ||
return false; | return false; | ||
} | |||
// 書き込み可能か確認 | |||
var fso = new ActiveXObject("Scripting.FileSystemObject"); | |||
try { | |||
fso.OpenTextFile(path, 8).Close(); | |||
return false; | |||
} catch (e) { | |||
// 排他制御中でも同じ結果を返すが判別できない | |||
} | } | ||
| 212行目: | 283行目: | ||
var oLocator = new ActiveXObject("WBemScripting.SWbemLocator"); | var oLocator = new ActiveXObject("WBemScripting.SWbemLocator"); | ||
var oService = oLocator.ConnectServer(); | var oService = oLocator.ConnectServer(); | ||
var os = new Enumerator(oService.ExecQuery("SELECT *FROM Win32_OperatingSystem")); | var os = new Enumerator(oService.ExecQuery("SELECT * FROM Win32_OperatingSystem")); | ||
if (Number(os.item().Version.substring(0, 3)) < 6.0) { | if (Number(os.item().Version.substring(0, 3)) < 6.0) { | ||
return false; | return false; | ||
} | } | ||
// | // ファイルの権限を確認 | ||
var SE_DACL_AUTO_INHERITED = 0x0400; | |||
if ( | var permission = new Enumerator(oService.ExecQuery("SELECT * FROM Win32_LogicalFileSecuritySetting Where Path='" + path.replace(/\\/g, "\\\\") + "'")); | ||
if (!permission.atEnd() && permission.item().ControlFlags & SE_DACL_AUTO_INHERITED) { | |||
return true; | return true; | ||
} | } | ||
スポンサーリンク