web/website/scripts/install-signature-preferences.mjs
Dev 6deb4a2e7d DuneRealms V2 clean baseline
Owner-authorised history reset; current source tree preserved exactly.
2026-11-13 00:05:14 +01:00

12 lines
3.2 KiB
JavaScript

import{readFile,writeFile,copyFile,mkdir}from 'node:fs/promises';
import{constants}from 'node:fs';
const root='/home/dunerealms/apps/integrations/osticket';
await mkdir('/root/dune-signature-backups',{recursive:true,mode:0o700});
async function patch(path,needle,replacement,marker){const source=await readFile(path,'utf8');if(source.includes(marker))return;if(source.split(needle).length!==2)throw Error('Unexpected native signature target: '+path);const backup='/root/dune-signature-backups/'+path.slice(root.length+1).replaceAll('/','_');try{await copyFile(path,backup,constants.COPYFILE_EXCL);}catch(error){if(error.code!=='EEXIST')throw error;}await writeFile(path,source.replace(needle,replacement));}
await patch(root+'/include/class.staff.php'," function updateProfile($vars, &$errors) {\n global $cfg;"," function updateProfile($vars, &$errors) {\n global $cfg;\n // Dune managed signature input guard\n require_once ROOT_DIR.'../managed-signatures.php';\n dune_osticket_lock_signature_vars($this,$vars,$errors);",'// Dune managed signature input guard');
await patch(root+'/include/class.staff.php'," $this->signature = Format::sanitize($vars['signature']);"," // Dune managed signature preference commit\n dune_osticket_lock_signature_vars($this,$vars,$errors,true);\n $this->signature = Format::sanitize($vars['signature']);",'// Dune managed signature preference commit');
const profile=root+'/include/staff/profile.inc.php',source=await readFile(profile,'utf8'),needle=source.match(/<textarea class="richtext no-bar" name="signature"[\s\S]*?<\/textarea>/)?.[0];
if(!source.includes('Dune managed signature preview')){if(!needle)throw Error('Native signature field not found');await patch(profile,needle,"<?php // Dune managed signature preview\nif(($staff->backend??'')==='dunerealms-staff'){require_once ROOT_DIR.'../managed-signatures.php';echo dune_osticket_signature_preferences($staff);}else{ ?>"+needle+"<?php } ?>",'Dune managed signature preview');}
await patch(root+'/scp/profile.php',"$staff=Staff::lookup($thisstaff->getId());","$staff=Staff::lookup($thisstaff->getId());\n// Dune managed closing-only save; native staff.inc validates CSRF first.\nif($_POST&&isset($_POST['dune_signature_save'])&&(int)($_POST['id']??0)===(int)$thisstaff->getId()&&($staff->backend??'')==='dunerealms-staff'){require_once ROOT_DIR.'../managed-signatures.php';try{dune_save_signature_preferences(dune_native_central_id($staff,'dunerealms-staff'),(string)($_POST['dune_signature_closing']??''));$msg='Signature closing saved.';}catch(Throwable $e){$errors['signature']=$e->getMessage();}$_POST=[];}",'// Dune managed closing-only save');
await patch(root+'/include/class.ticket.php'," function postReply($vars, &$errors, $alert=true, $claim=true) {\n global $thisstaff, $cfg;"," function postReply($vars, &$errors, $alert=true, $claim=true) {\n global $thisstaff, $cfg;\n // Dune centrally managed signature always accompanies staff email replies.\n if($thisstaff&&($thisstaff->backend??'')==='dunerealms-staff')$vars['signature']='mine';",'// Dune centrally managed signature always');
console.log('Managed signature editing restricted to closing line; all other profile fields retain native behavior.');