Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Echo Off
- SetLocal EnableDelayedExpansion
- :: Инициализация и проверки
- Set StartedAt=%Time%
- Set Data=%*
- :Next
- For /F "tokens=1,* delims=;" %%A In ("%Data%") Do (
- For /F "tokens=1,2 delims==" %%C In ("%%A") Do (Set $%%C=%%D)
- If Not "%%B"=="" Set Data=%%B&GoTo :Next
- )
- If Not Defined H-Cookie Set "H-Cookie=dummy"
- Set H-Cookie|Findstr /C:"<" /C:">" /C:"&" /C:"|" /C:^"\^"^" /C:"(" /C:")" /C:"\^">nul&&(Call HTTP302Header err_badcookie&Exit)
- For %%A In ("%H-Cookie:;=" "%") Do For /F "tokens=1,* delims== " %%A In ("%%~A") Do Set "C-%%A=%%B"
- If Not Defined C-Code Set "LoginStatus=err_nosession" & GoTo :Back
- For %%A In (User Access LastSeen) Do Set "%%A="
- For /F "tokens=1-3 delims=|" %%A In ('Call GetUserInfo %H-REMOTE_ADDR% %C-Code%') Do (
- Set "User=%%A"
- Set "Access=%%B"
- Set "LastSeen=%%C"
- )
- If Not Defined Access Set "LoginStatus=err_nosession" & GoTo :Back
- If %Access% LSS 5 Set "LoginStatus=err_noaccess" & GoTo :Back
- Set RDPUser=Username
- Set RDPPassword=Password
- Set PrintServerIP=192.168.0.201
- Set DB=%__ServerRoot%\data\printlog.db
- Set Table=printlog-%date:~3%
- Set Logs=%__ServerRoot%\data\printlogs
- Set Names[]="Вася Пупкин","Антон Безымянный","Иван Иванов","Васисуалий Шишкин","John Doe"
- rem NetBIOS, IPv4 или IPv6
- Set Fingerprints[]="PC-1=Вася Пупкин",^
- "192.168.0.201=Вася Пупкин",^
- "PC-2=Антон Безымянный",^
- "[fe80::b42b:9127:da45:f59a]=Антон Безымянный",^
- "192.168.0.202=Антон Безымянный",^
- "PC-3=Иван Иванов",^
- "[fe80::54c9:58d0:96c0:d848]=Иван Иванов",^
- "[fe80::d82c:38a3:8667:9eb]=Иван Иванов",^
- "192.168.0.203=Иван Иванов",^
- "PC-4=Васисуалий Шишкин",^
- "[fe80::3c4c:209a:9b62:56ad]=Васисуалий Шишкин",^
- "192.168.0.204=Васисуалий Шишкин",^
- "DIRECTOR=John Doe",^
- "[fe80::d92f:f9a6:10b9:fcbd]=John Doe",^
- "192.168.0.200=John Doe"
- rem Клиентский скрипт копирует логи через $D
- psexec -accepteula -n 3 -u %RDPUser% -p %RDPPassword% -s \\%PrintServerIP% cmd /c printlog >nul 2>nul
- Call :CheckDatabase
- Call :GetDatabaseTables
- Call :GetLastTimestamp
- If Defined $page If Defined table[%$page%] (
- Set Table=!table[%$page%]!
- Set active_table=%$page%
- )
- For /F "tokens=2 delims=-" %%A In ("%Table%") Do Set LogVisibleDate=%%A
- :AddLogsToDB
- For /F "skip=2 tokens=1,3-4,6-7 delims=," %%A In ('type "%Logs%\monthly.log"^|sort /R') Do (
- For /F "delims=" %%T In ('Call TimeToUnix %%A') Do Set timestamp=%%T
- If !timestamp! LEQ !Last! GoTo :break
- rem Set "author=%%E"
- Set docname=%%~D
- Set docname=!docname:^|=!
- Set docname=!docname:^<=!
- Set docname=!docname:^>=!
- Set docname=!docname:^&=!
- For /F "delims=" %%N In ('Echo !docname!^|recode cp1251..utf-8') Do Set document=%%N
- For %%X In (%Fingerprints[]%) Do (
- For /F "tokens=1,* delims==" %%M In ("%%~X") Do (
- If "%%E" == "%%~M" Set "author=%%~N"
- )
- )
- Echo INSERT INTO `%Table%` ^(timestamp, time, pages, copies, document, author^) VALUES ^(!timestamp!, "%%A", %%B, %%C, "!document!", "!author!"^);|sqlite3 "%DB%"
- )
- :break
- :Stats
- Set Users=0
- Set Total.pages=0
- For %%N In (%Names[]%) Do (
- Set /A Users+=1
- Set User_!Users!.name=%%~N
- Set User_!Users!.documents=0
- Set User_!Users!.pages=0
- For /F "tokens=1-2 delims=|" %%A In ('Echo SELECT `pages`^, `copies` FROM `%Table%` WHERE `author` like "%%~N"^;^|sqlite3 "%DB%"') Do (
- Set /A User_!Users!.documents+=1
- Set /A User_!Users!.pages+=%%A*%%B
- Set /A Total.pages+=%%A*%%B
- )
- )
- For /F "delims=" %%A In ('GetLocalPath "%~dp0"') Do Set "LocalPath=%%A"
- rem Call RecompileTemplates
- Call HTTP200Header
- Call Include HTMLHeader "Title:Статистика печати за %LogVisibleDate%"
- Call Include Menu "cpanel:true;login:true;Username:%User%;Self:%LocalPath%;Code:%C-Code%"
- Call :MakeJSON
- Call Include-rawJSON PrintLog "%JSONFile%"
- For /F "delims=" %%A In ('LoadTimer %StartedAt%') Do (Set ExecTime=%%A)
- Call Include Footer "PerformanceData:%ExecTime%;ExecutableName:%~nx0;ServerVersion:%__OWS_version%"
- Call Include HTMLEnd
- Exit
- :MakeJSON
- Set JSONFile=%Temp%\printlog_%random%%C-Code%.json
- <Nul Set /P Echo="{"Users":[">"%JSONFile%"
- For /L %%X In (1,1,!Users!) Do (
- <Nul Set /P Echo="{"
- <Nul Set /P Echo=""ID":"%%X","Name":"!User_%%X.name!","Docs":"!User_%%X.documents!","Pages":"!User_%%X.pages!""
- If "!Users!"=="%%X" (<Nul Set /P Echo="}") Else (<Nul Set /P Echo="},")
- )>>"%JSONFile%"
- <Nul Set /P Echo="],"TotalPages":"!Total.pages!",">>"%JSONFile%"
- Set i=0
- Set Docs=0
- For /F "delims=" %%X In ('Echo SELECT `id` FROM `%Table%`^;^|sqlite3 "%DB%"') Do (Set /A Docs+=1)
- <Nul Set /P Echo=""Documents":[">>"%JSONFile%"
- For /F "tokens=1-5 delims=|" %%A In ('Echo SELECT `time`^, `author`^, `pages`^, `copies`^, `document` FROM `%Table%` ORDER BY `timestamp` DESC^;^|sqlite3 "%DB%"') Do (
- Set /A i+=1
- <Nul Set /P Echo="{"
- <Nul Set /P Echo=""Timestamp":"%%A","User":"%%B","Pages":"%%C","Copies":"%%D","Filename":"%%E""
- If "!Docs!"=="!i!" (<Nul Set /P Echo="}") Else (<Nul Set /P Echo="},")
- )>>"%JSONFile%"
- <Nul Set /P Echo="],"LogPages":"%table[index]%","ActiveLogPage":"%active_table%","LogVisibleDate":"%LogVisibleDate%","Session":"%C-Code%"}">>"%JSONFile%"
- Exit /B
- :CheckDatabase
- :: Проверяем наличие БД и создаём при надобности
- Echo CREATE TABLE IF NOT EXISTS `%Table%` (^
- `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,^
- `timestamp` BIGINT,^
- `time` TEXT,^
- `pages` INT,^
- `copies` INT,^
- `document` TEXT,^
- `author` TEXT^
- );|sqlite3 "%DB%"
- Exit /B
- :GetDatabaseTables
- Set table[index]=0
- Set active_table=1
- For /F "delims= tokens=*" %%A In ('Echo SELECT name FROM sqlite_master WHERE name LIKE "printlog-%%" AND type ^= "table" ORDER BY name ASC^;^|sqlite3 "%DB%"') Do (
- Set /A table[index]+=1
- Set table[!table[index]!]=%%A
- If "%%A" == "%Table%" Set active_table=!table[index]!
- )
- Exit /B
- :GetLastTimestamp
- Set "Last="
- For /F "delims=" %%A In ('Echo SELECT `timestamp` FROM `%Table%` ORDER BY `timestamp` DESC LIMIT 1^;^|sqlite3 "%DB%"') Do (Set Last=%%A)
- If Not Defined Last Set Last=1400000000
- Exit /B
- :Back
- :: Логгируем ошибки и редиректим
- If "%LoginStatus%"=="err_noaccess" Echo [%Date% %Time:~,8%] [IP:%H-REMOTE_ADDR%] tried to execute "%~nx0" with access level "%Access%">>"%__ServerRoot%\%__AccessLogFile%"
- If "%LoginStatus%"=="err_nosession" Echo [%Date% %Time:~,8%] Can't validate session token [Code:"%C-Code%"] for access [IP:%H-REMOTE_ADDR%] to "%~nx0">>"%__ServerRoot%\%__AccessLogFile%"
- If "%LoginStatus%"=="err_nosession" Call HTTP302Header-nosession
- If "%LoginStatus%"=="err_noaccess" Call HTTP302Header err_noaccess
- Exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement