01_FHEMWEB.pm 146 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626
  1. ##############################################
  2. # $Id: 01_FHEMWEB.pm 15328 2017-10-27 10:51:17Z rudolfkoenig $
  3. package main;
  4. use strict;
  5. use warnings;
  6. use TcpServerUtils;
  7. use HttpUtils;
  8. use Time::HiRes qw(gettimeofday);
  9. #########################
  10. # Forward declaration
  11. sub FW_IconURL($);
  12. sub FW_addContent(;$);
  13. sub FW_addToWritebuffer($$@);
  14. sub FW_answerCall($);
  15. sub FW_dev2image($;$);
  16. sub FW_devState($$@);
  17. sub FW_digestCgi($);
  18. sub FW_directNotify($@);
  19. sub FW_doDetail($);
  20. sub FW_fatal($);
  21. sub FW_fileList($;$);
  22. sub FW_htmlEscape($);
  23. sub FW_iconName($);
  24. sub FW_iconPath($);
  25. sub FW_logWrapper($);
  26. sub FW_makeEdit($$$);
  27. sub FW_makeImage(@);
  28. sub FW_makeTable($$$@);
  29. sub FW_makeTableFromArray($$@);
  30. sub FW_pF($@);
  31. sub FW_pH(@);
  32. sub FW_pHPlain(@);
  33. sub FW_pO(@);
  34. sub FW_parseColumns($);
  35. sub FW_readIcons($);
  36. sub FW_readIconsFrom($$);
  37. sub FW_returnFileAsStream($$$$$);
  38. sub FW_roomOverview($);
  39. #sub FW_roomStatesForInform($$); # Forum 30515
  40. sub FW_select($$$$$@);
  41. sub FW_serveSpecial($$$$);
  42. sub FW_showRoom();
  43. sub FW_style($$);
  44. sub FW_submit($$@);
  45. sub FW_textfield($$$);
  46. sub FW_textfieldv($$$$);
  47. sub FW_updateHashes();
  48. sub FW_visibleDevices(;$);
  49. sub FW_widgetOverride($$);
  50. sub FW_Read($$);
  51. use vars qw($FW_dir); # base directory for web server
  52. use vars qw($FW_icondir); # icon base directory
  53. use vars qw($FW_cssdir); # css directory
  54. use vars qw($FW_gplotdir);# gplot directory
  55. use vars qw($MW_dir); # moddir (./FHEM), needed by edit Files in new
  56. # structure
  57. use vars qw($FW_ME); # webname (default is fhem), used by 97_GROUP/weblink
  58. use vars qw($FW_CSRF); # CSRF Token or empty
  59. use vars qw($FW_ss); # is smallscreen, needed by 97_GROUP/95_VIEW
  60. use vars qw($FW_tp); # is touchpad (iPad / etc)
  61. use vars qw($FW_sp); # stylesheetPrefix
  62. # global variables, also used by 97_GROUP/95_VIEW/95_FLOORPLAN
  63. use vars qw(%FW_types); # device types,
  64. use vars qw($FW_RET); # Returned data (html)
  65. use vars qw($FW_RETTYPE); # image/png or the like
  66. use vars qw($FW_wname); # Web instance
  67. use vars qw($FW_subdir); # Sub-path in URL, used by FLOORPLAN/weblink
  68. use vars qw(%FW_pos); # scroll position
  69. use vars qw($FW_cname); # Current connection name
  70. use vars qw(%FW_hiddenroom); # hash of hidden rooms, used by weblink
  71. use vars qw($FW_plotmode);# Global plot mode (WEB attribute), used by SVG
  72. use vars qw($FW_plotsize);# Global plot size (WEB attribute), used by SVG
  73. use vars qw(%FW_webArgs); # all arguments specified in the GET
  74. use vars qw(@FW_fhemwebjs);# List of fhemweb*js scripts to load
  75. use vars qw($FW_fhemwebjs);# List of fhemweb*js scripts to load
  76. use vars qw($FW_detail); # currently selected device for detail view
  77. use vars qw($FW_cmdret); # Returned data by the fhem call
  78. use vars qw($FW_room); # currently selected room
  79. use vars qw($FW_formmethod);
  80. use vars qw(%FW_visibleDeviceHash);
  81. use vars qw(@FW_httpheader); # HTTP header, line by line
  82. use vars qw(%FW_httpheader); # HTTP header, as hash
  83. use vars qw($FW_userAgent); # user agent string
  84. $FW_formmethod = "post";
  85. my $FW_zlib_checked;
  86. my $FW_use_zlib = 1;
  87. my $FW_use_sha = 0;
  88. my $FW_activateInform = 0;
  89. my $FW_lastWebName = ""; # Name of last FHEMWEB instance, for caching
  90. my $FW_lastHashUpdate = 0;
  91. my $FW_httpRetCode = "";
  92. my %FW_csrfTokenCache;
  93. #########################
  94. # As we are _not_ multithreaded, it is safe to use global variables.
  95. # Note: for delivering SVG plots we fork
  96. my $FW_data; # Filecontent from browser when editing a file
  97. my %FW_icons; # List of icons
  98. my @FW_iconDirs; # Directory search order for icons
  99. my $FW_RETTYPE; # image/png or the like
  100. my %FW_rooms; # hash of all rooms
  101. my @FW_roomsArr; # ordered list of rooms
  102. my %FW_groups; # hash of all groups
  103. my %FW_types; # device types, for sorting
  104. my %FW_hiddengroup;# hash of hidden groups
  105. my $FW_inform;
  106. my $FW_XHR; # Data only answer, no HTML
  107. my $FW_id=""; # id of current page
  108. my $FW_jsonp; # jasonp answer (sending function calls to the client)
  109. my $FW_headerlines; #
  110. my $FW_chash; # client fhem hash
  111. my $FW_encoding="UTF-8";
  112. my $FW_styleStamp=time();
  113. #####################################
  114. sub
  115. FHEMWEB_Initialize($)
  116. {
  117. my ($hash) = @_;
  118. $hash->{ReadFn} = "FW_Read";
  119. $hash->{GetFn} = "FW_Get";
  120. $hash->{SetFn} = "FW_Set";
  121. $hash->{AttrFn} = "FW_Attr";
  122. $hash->{DefFn} = "FW_Define";
  123. $hash->{UndefFn} = "FW_Undef";
  124. $hash->{NotifyFn}= ($init_done ? "FW_Notify" : "FW_SecurityCheck");
  125. $hash->{AsyncOutputFn} = "FW_AsyncOutput";
  126. $hash->{ActivateInformFn} = "FW_ActivateInform";
  127. no warnings 'qw';
  128. my @attrList = qw(
  129. CORS:0,1
  130. HTTPS:1,0
  131. CssFiles
  132. JavaScripts
  133. SVGcache:1,0
  134. addHtmlTitle:1,0
  135. addStateEvent
  136. csrfToken
  137. csrfTokenHTTPHeader:0,1
  138. alarmTimeout
  139. allowedCommands
  140. allowfrom
  141. basicAuth
  142. basicAuthMsg
  143. closeConn:1,0
  144. column
  145. confirmDelete:0,1
  146. confirmJSError:0,1
  147. defaultRoom
  148. deviceOverview:always,iconOnly,onClick,never
  149. editConfig:1,0
  150. editFileList:textField-long
  151. endPlotNow:1,0
  152. endPlotToday:1,0
  153. fwcompress:0,1
  154. hiddengroup
  155. hiddengroupRegexp
  156. hiddenroom
  157. hiddenroomRegexp
  158. iconPath
  159. longpoll:0,1,websocket
  160. longpollSVG:1,0
  161. menuEntries
  162. mainInputLength
  163. nameDisplay
  164. ploteditor:always,onClick,never
  165. plotfork:1,0
  166. plotmode:gnuplot-scroll,gnuplot-scroll-svg,SVG
  167. plotEmbed:0,1
  168. plotsize
  169. plotWeekStartDay:0,1,2,3,4,5,6
  170. nrAxis
  171. redirectCmds:0,1
  172. refresh
  173. reverseLogs:0,1
  174. roomIcons
  175. sortRooms
  176. showUsedFiles:0,1
  177. sslVersion
  178. smallscreen:unused
  179. smallscreenCommands:0,1
  180. stylesheetPrefix
  181. title
  182. touchpad:unused
  183. viewport
  184. webname
  185. );
  186. use warnings 'qw';
  187. $hash->{AttrList} = join(" ", @attrList);
  188. ###############
  189. # Initialize internal structures
  190. map { addToAttrList($_) } ( "webCmd", "webCmdLabel:textField-long", "icon",
  191. "cmdIcon", "devStateIcon", "widgetOverride", "sortby", "devStateStyle");
  192. InternalTimer(time()+60, "FW_closeInactiveClients", 0, 0);
  193. $FW_dir = "$attr{global}{modpath}/www";
  194. $FW_icondir = "$FW_dir/images";
  195. $FW_cssdir = "$FW_dir/pgm2";
  196. $FW_gplotdir = "$FW_dir/gplot";
  197. if(opendir(DH, "$FW_dir/pgm2")) {
  198. $FW_fhemwebjs = join(",", map { $_ = ~m/^fhemweb_(.*).js$/; $1 }
  199. grep { /fhemweb_(.*).js$/ }
  200. readdir(DH));
  201. @FW_fhemwebjs = ("fhemweb.js");
  202. closedir(DH);
  203. }
  204. $data{webCmdFn}{"~"} = "FW_widgetFallbackFn"; # Should be the last
  205. if($init_done) { # reload workaround
  206. foreach my $pe ("fhemSVG", "openautomation", "default") {
  207. FW_readIcons($pe);
  208. }
  209. }
  210. }
  211. #####################################
  212. sub
  213. FW_SecurityCheck($$)
  214. {
  215. my ($ntfy, $dev) = @_;
  216. return if($dev->{NAME} ne "global" ||
  217. !grep(m/^INITIALIZED$/, @{$dev->{CHANGED}}));
  218. my $motd = AttrVal("global", "motd", "");
  219. if($motd =~ "^SecurityCheck") {
  220. my @list1 = devspec2array("TYPE=FHEMWEB");
  221. my @list2 = devspec2array("TYPE=allowed");
  222. my @list3;
  223. for my $l (@list1) { # This is a hack, as hardcoded to basicAuth
  224. next if(!$defs{$l});
  225. my $fnd = 0;
  226. for my $a (@list2) {
  227. next if(!$defs{$a});
  228. my $vf = AttrVal($a, "validFor","");
  229. $fnd = 1 if($vf && ($vf =~ m/\b$l\b/) && AttrVal($a, "basicAuth",""));
  230. }
  231. push @list3, $l if(!$fnd);
  232. }
  233. $motd .= (join(",", sort @list3).
  234. " has no associated allowed device with basicAuth.\n")
  235. if(@list3);
  236. $attr{global}{motd} = $motd;
  237. }
  238. $modules{FHEMWEB}{NotifyFn}= "FW_Notify";
  239. return;
  240. }
  241. #####################################
  242. sub
  243. FW_Define($$)
  244. {
  245. my ($hash, $def) = @_;
  246. my ($name, $type, $port, $global) = split("[ \t]+", $def);
  247. return "Usage: define <name> FHEMWEB [IPV6:]<tcp-portnr> [global]"
  248. if($port !~ m/^(IPV6:)?\d+$/ || ($global && $global ne "global"));
  249. foreach my $pe ("fhemSVG", "openautomation", "default") {
  250. FW_readIcons($pe);
  251. }
  252. my $ret = TcpServer_Open($hash, $port, $global);
  253. # Make sure that fhem only runs once
  254. if($ret && !$init_done) {
  255. Log3 $hash, 1, "$ret. Exiting.";
  256. exit(1);
  257. }
  258. $hash->{CSRFTOKEN} = $FW_csrfTokenCache{$name};
  259. if(!defined($hash->{CSRFTOKEN})) { # preserve over rereadcfg
  260. InternalTimer(1, sub(){
  261. if($featurelevel >= 5.8 && !AttrVal($name, "csrfToken", undef)) {
  262. my ($x,$y) = gettimeofday();
  263. ($defs{$name}{CSRFTOKEN}="csrf_".(rand($y)*rand($x))) =~s/[^a-z_0-9]//g;
  264. $FW_csrfTokenCache{$name} = $hash->{CSRFTOKEN};
  265. }
  266. }, $hash, 0);
  267. }
  268. return $ret;
  269. }
  270. #####################################
  271. sub
  272. FW_Undef($$)
  273. {
  274. my ($hash, $arg) = @_;
  275. my $ret = TcpServer_Close($hash);
  276. if($hash->{inform}) {
  277. %FW_visibleDeviceHash = FW_visibleDevices();
  278. delete($logInform{$hash->{NAME}});
  279. }
  280. return $ret;
  281. }
  282. #####################################
  283. sub
  284. FW_Read($$)
  285. {
  286. my ($hash, $reread) = @_;
  287. my $name = $hash->{NAME};
  288. if($hash->{SERVERSOCKET}) { # Accept and create a child
  289. my $nhash = TcpServer_Accept($hash, "FHEMWEB");
  290. return if(!$nhash);
  291. my $wt = AttrVal($name, "alarmTimeout", undef);
  292. $nhash->{ALARMTIMEOUT} = $wt if($wt);
  293. $nhash->{CD}->blocking(0);
  294. return;
  295. }
  296. $FW_chash = $hash;
  297. $FW_wname = $hash->{SNAME};
  298. $FW_cname = $name;
  299. $FW_subdir = "";
  300. my $c = $hash->{CD};
  301. if(!$FW_zlib_checked) {
  302. $FW_zlib_checked = 1;
  303. $FW_use_zlib = AttrVal($FW_wname, "fwcompress", 1);
  304. if($FW_use_zlib) {
  305. eval { require Compress::Zlib; };
  306. if($@) {
  307. $FW_use_zlib = 0;
  308. Log3 $FW_wname, 1, $@;
  309. Log3 $FW_wname, 1,
  310. "$FW_wname: Can't load Compress::Zlib, deactivating compression";
  311. $attr{$FW_wname}{fwcompress} = 0;
  312. }
  313. }
  314. }
  315. if(!$reread) {
  316. # Data from HTTP Client
  317. my $buf;
  318. my $ret = sysread($c, $buf, 1024);
  319. if(!defined($ret) && $! == EWOULDBLOCK ){
  320. $hash->{wantWrite} = 1
  321. if(TcpServer_WantWrite($hash));
  322. return;
  323. } elsif(!$ret) { # 0==EOF, undef=error
  324. CommandDelete(undef, $name);
  325. Log3 $FW_wname, 4, "Connection closed for $name: ".
  326. (defined($ret) ? 'EOF' : $!);
  327. return;
  328. }
  329. $hash->{BUF} .= $buf;
  330. if($hash->{SSL} && $c->can('pending')) {
  331. while($c->pending()) {
  332. sysread($c, $buf, 1024);
  333. $hash->{BUF} .= $buf;
  334. }
  335. }
  336. }
  337. if($hash->{websocket}) { # Work in Progress (Forum #59713)
  338. my $fin = (ord(substr($hash->{BUF},0,1)) & 0x80)?1:0;
  339. my $op = (ord(substr($hash->{BUF},0,1)) & 0x0F);
  340. my $mask = (ord(substr($hash->{BUF},1,1)) & 0x80)?1:0;
  341. my $len = (ord(substr($hash->{BUF},1,1)) & 0x7F);
  342. my $i = 2;
  343. if( $len == 126 ) {
  344. $len = unpack( 'n', substr($hash->{BUF},$i,2) );
  345. $i += 2;
  346. } elsif( $len == 127 ) {
  347. $len = unpack( 'q', substr($hash->{BUF},$i,8) );
  348. $i += 8;
  349. }
  350. if( $mask ) {
  351. $mask = substr($hash->{BUF},$i,4);
  352. $i += 4;
  353. }
  354. my $data = substr($hash->{BUF}, $i, $len);
  355. #for( my $i = 0; $i < $len; $i++ ) {
  356. # substr( $data, $i, 1, substr( $data, $i, 1, ) ^ substr($mask, $i% , 1) );
  357. #}
  358. #Log 1, "Received via websocket: ".unpack("H*",$data);
  359. return;
  360. }
  361. if(!$hash->{HDR}) {
  362. return if($hash->{BUF} !~ m/^(.*?)(\n\n|\r\n\r\n)(.*)$/s);
  363. $hash->{HDR} = $1;
  364. $hash->{BUF} = $3;
  365. if($hash->{HDR} =~ m/Content-Length:\s*([^\r\n]*)/si) {
  366. $hash->{CONTENT_LENGTH} = $1;
  367. }
  368. }
  369. my $POSTdata = "";
  370. if($hash->{CONTENT_LENGTH}) {
  371. return if(length($hash->{BUF})<$hash->{CONTENT_LENGTH});
  372. $POSTdata = substr($hash->{BUF}, 0, $hash->{CONTENT_LENGTH});
  373. $hash->{BUF} = substr($hash->{BUF}, $hash->{CONTENT_LENGTH});
  374. }
  375. @FW_httpheader = split(/[\r\n]+/, $hash->{HDR});
  376. %FW_httpheader = map {
  377. my ($k,$v) = split(/: */, $_, 2);
  378. $k =~ s/(\w+)/\u$1/g; # Forum #39203
  379. $k=>(defined($v) ? $v : 1);
  380. } @FW_httpheader;
  381. delete($hash->{HDR});
  382. my @origin = grep /Origin/i, @FW_httpheader;
  383. $FW_headerlines = (AttrVal($FW_wname, "CORS", 0) ?
  384. (($#origin<0) ? "": "Access-Control-Allow-".$origin[0]."\r\n").
  385. "Access-Control-Allow-Methods: GET POST OPTIONS\r\n".
  386. "Access-Control-Allow-Headers: Origin, Authorization, Accept\r\n".
  387. "Access-Control-Allow-Credentials: true\r\n".
  388. "Access-Control-Max-Age:86400\r\n".
  389. "Access-Control-Expose-Headers: X-FHEM-csrfToken\r\n": "");
  390. $FW_headerlines .= "X-FHEM-csrfToken: $defs{$FW_wname}{CSRFTOKEN}\r\n"
  391. if(defined($defs{$FW_wname}{CSRFTOKEN}) &&
  392. AttrVal($FW_wname, "csrfTokenHTTPHeader", 1));
  393. #########################
  394. # Return 200 for OPTIONS or 405 for unsupported method
  395. my ($method, $arg, $httpvers) = split(" ", $FW_httpheader[0], 3)
  396. if($FW_httpheader[0]);
  397. $method = "" if(!$method);
  398. if($method !~ m/^(GET|POST)$/i){
  399. my $retCode = ($method eq "OPTIONS") ? "200 OK" : "405 Method Not Allowed";
  400. TcpServer_WriteBlocking($FW_chash,
  401. "HTTP/1.1 $retCode\r\n" .
  402. $FW_headerlines.
  403. "Content-Length: 0\r\n\r\n");
  404. delete $hash->{CONTENT_LENGTH};
  405. FW_Read($hash, 1) if($hash->{BUF});
  406. Log 3, "$FW_cname: unsupported HTTP method $method, rejecting it."
  407. if($retCode ne "200 OK");
  408. FW_closeConn($hash);
  409. return;
  410. }
  411. #############################
  412. # AUTH
  413. if(!defined($FW_chash->{Authenticated})) {
  414. my $ret = Authenticate($FW_chash, \%FW_httpheader);
  415. if($ret == 0) {
  416. $FW_chash->{Authenticated} = 0; # not needed
  417. } elsif($ret == 1) {
  418. $FW_chash->{Authenticated} = 1; # ok
  419. # Need to send set-cookie (if set) after succesful authentication
  420. my $ah = $FW_chash->{".httpAuthHeader"};
  421. $FW_headerlines .= $ah if($ah);
  422. delete $FW_chash->{".httpAuthHeader"};
  423. } else {
  424. my $ah = $FW_chash->{".httpAuthHeader"};
  425. TcpServer_WriteBlocking($hash,
  426. ($ah ? $ah : "").
  427. $FW_headerlines.
  428. "Content-Length: 0\r\n\r\n");
  429. delete $hash->{CONTENT_LENGTH};
  430. FW_Read($hash, 1) if($hash->{BUF});
  431. return;
  432. }
  433. } else {
  434. my $ah = $FW_chash->{".httpAuthHeader"};
  435. $FW_headerlines .= $ah if($ah);
  436. }
  437. #############################
  438. my $now = time();
  439. $arg .= "&".$POSTdata if($POSTdata);
  440. delete $hash->{CONTENT_LENGTH};
  441. $hash->{LASTACCESS} = $now;
  442. if($FW_use_sha && $method eq 'GET' &&
  443. $FW_httpheader{Connection} && $FW_httpheader{Connection} =~ /Upgrade/i) {
  444. my $shastr = Digest::SHA::sha1_base64($FW_httpheader{'Sec-WebSocket-Key'}.
  445. "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
  446. TcpServer_WriteBlocking($FW_chash,
  447. "HTTP/1.1 101 Switching Protocols\r\n" .
  448. "Upgrade: websocket\r\n" .
  449. "Connection: Upgrade\r\n" .
  450. "Sec-WebSocket-Accept:$shastr=\r\n".
  451. "\r\n" );
  452. $FW_chash->{websocket} = 1;
  453. my $me = $FW_chash;
  454. my ($cmd, $cmddev) = FW_digestCgi($arg);
  455. if($FW_id) {
  456. $me->{FW_ID} = $FW_id;
  457. $me->{canAsyncOutput} = 1;
  458. }
  459. FW_initInform($me, 0) if($FW_inform);
  460. return -1;
  461. }
  462. $FW_userAgent = $FW_httpheader{"User-Agent"};
  463. $arg = "" if(!defined($arg));
  464. Log3 $FW_wname, 4, "$name $method $arg; BUFLEN:".length($hash->{BUF});
  465. $FW_ME = "/" . AttrVal($FW_wname, "webname", "fhem");
  466. my $pf = AttrVal($FW_wname, "plotfork", undef);
  467. if($pf) { # 0 disables
  468. # Process SVG rendering as a parallel process
  469. my $p = $data{FWEXT};
  470. if(grep { $p->{$_}{FORKABLE} && $arg =~ m+^$FW_ME$_+ } keys %{$p}) {
  471. my $pid = fhemFork();
  472. if($pid) { # success, parent
  473. use constant PRIO_PROCESS => 0;
  474. setpriority(PRIO_PROCESS, $pid, getpriority(PRIO_PROCESS,$pid) + $pf)
  475. if($^O !~ m/Win/);
  476. # a) while child writes a new request might arrive if client uses
  477. # pipelining or
  478. # b) parent doesn't know about ssl-session changes due to child writing
  479. # to socket
  480. # -> have to close socket in parent... so that its only used in this
  481. # child.
  482. TcpServer_Disown( $hash );
  483. delete($defs{$name});
  484. delete($attr{$name});
  485. FW_Read($hash, 1) if($hash->{BUF});
  486. return;
  487. } elsif(defined($pid)){ # child
  488. delete $hash->{BUF};
  489. $hash->{isChild} = 1;
  490. } # fork failed and continue in parent
  491. }
  492. }
  493. $FW_httpRetCode = "200 OK";
  494. my $cacheable = FW_answerCall($arg);
  495. if($cacheable == -1) {
  496. FW_closeConn($hash);
  497. return;
  498. }
  499. my $compressed = "";
  500. if($FW_RETTYPE =~ m/(text|xml|json|svg|script)/i &&
  501. ($FW_httpheader{"Accept-Encoding"} &&
  502. $FW_httpheader{"Accept-Encoding"} =~ m/gzip/) &&
  503. $FW_use_zlib) {
  504. utf8::encode($FW_RET)
  505. if(utf8::is_utf8($FW_RET) && $FW_RET =~ m/[^\x00-\xFF]/ );
  506. eval { $FW_RET = Compress::Zlib::memGzip($FW_RET); };
  507. if($@) {
  508. Log 1, "memGzip: $@"; $FW_RET=""; #Forum #29939
  509. } else {
  510. $compressed = "Content-Encoding: gzip\r\n";
  511. }
  512. }
  513. my $length = length($FW_RET);
  514. my $expires = ($cacheable?
  515. ("Expires: ".FmtDateTimeRFC1123($now+900)."\r\n") : "");
  516. Log3 $FW_wname, 4,
  517. "$FW_wname: $arg / RL:$length / $FW_RETTYPE / $compressed / $expires";
  518. if( ! FW_addToWritebuffer($hash,
  519. "HTTP/1.1 $FW_httpRetCode\r\n" .
  520. "Content-Length: $length\r\n" .
  521. $expires . $compressed . $FW_headerlines .
  522. "Content-Type: $FW_RETTYPE\r\n\r\n" .
  523. $FW_RET, "FW_closeConn", 1) ){
  524. Log3 $name, 4, "Closing connection $name due to full buffer in FW_Read"
  525. if(!$hash->{isChild});
  526. FW_closeConn($hash);
  527. TcpServer_Close($hash, 1);
  528. }
  529. }
  530. sub
  531. FW_initInform($$)
  532. {
  533. my ($me, $longpoll) = @_;
  534. if($FW_inform =~ /type=/) {
  535. foreach my $kv (split(";", $FW_inform)) {
  536. my ($key,$value) = split("=", $kv, 2);
  537. $me->{inform}{$key} = $value;
  538. }
  539. } else { # Compatibility mode
  540. $me->{inform}{type} = ($FW_room ? "status" : "raw");
  541. $me->{inform}{filter} = ($FW_room ? $FW_room : ".*");
  542. }
  543. my $filter = $me->{inform}{filter};
  544. $filter = "NAME=.*" if($filter eq "room=all");
  545. $filter = "room!=.+" if($filter eq "room=Unsorted");
  546. my %h = map { $_ => 1 } devspec2array($filter);
  547. $h{global} = 1 if( $me->{inform}{addglobal} );
  548. $h{"#FHEMWEB:$FW_wname"} = 1;
  549. $me->{inform}{devices} = \%h;
  550. %FW_visibleDeviceHash = FW_visibleDevices();
  551. # NTFY_ORDER is larger than the normal order (50-)
  552. $me->{NTFY_ORDER} = $FW_cname; # else notifyfn won't be called
  553. %ntfyHash = ();
  554. $me->{inform}{since} = time()-5
  555. if(!defined($me->{inform}{since}) || $me->{inform}{since} !~ m/^\d+$/);
  556. my $sinceTimestamp = FmtDateTime($me->{inform}{since});
  557. if($longpoll) {
  558. TcpServer_WriteBlocking($me,
  559. "HTTP/1.1 200 OK\r\n".
  560. $FW_headerlines.
  561. "Content-Type: application/octet-stream; charset=$FW_encoding\r\n\r\n".
  562. FW_roomStatesForInform($me, $sinceTimestamp));
  563. } else { # websocket
  564. FW_addToWritebuffer($me,
  565. FW_roomStatesForInform($me, $sinceTimestamp));
  566. }
  567. if($FW_id && $defs{$FW_wname}{asyncOutput}) {
  568. my $data = $defs{$FW_wname}{asyncOutput}{$FW_id};
  569. if($data) {
  570. FW_addToWritebuffer($me, $data."\n");
  571. delete $defs{$FW_wname}{asyncOutput}{$FW_id};
  572. }
  573. }
  574. if($me->{inform}{withLog}) {
  575. $logInform{$me->{NAME}} = "FW_logInform";
  576. } else {
  577. delete($logInform{$me->{NAME}});
  578. }
  579. }
  580. sub
  581. FW_addToWritebuffer($$@)
  582. {
  583. my ($hash, $txt, $callback, $nolimit) = @_;
  584. if( $hash->{websocket} ) {
  585. my $len = length($txt);
  586. if( $len < 126 ) {
  587. $txt = chr(0x81) . chr($len) . $txt;
  588. } else {
  589. if ( $len < 65536 ) {
  590. $txt = chr(0x81) . chr(0x7E) . pack('n', $len) . $txt;
  591. } else {
  592. $txt = chr(0x81) . chr(0x7F) . chr(0x00) . chr(0x00) . chr(0x00) . chr(0x00) . pack('N', $len) . $txt;
  593. }
  594. }
  595. }
  596. return addToWritebuffer($hash, $txt, $callback, $nolimit);
  597. }
  598. sub
  599. FW_AsyncOutput($$)
  600. {
  601. my ($hash, $ret) = @_;
  602. return if(!$hash || !$hash->{FW_ID});
  603. if( $ret =~ m/^<html>(.*)<\/html>$/s ) {
  604. $ret = $1;
  605. } else {
  606. $ret =~ s/&/&amp;/g;
  607. $ret =~ s/'/&apos;/g;
  608. $ret =~ s/</&lt;/g;
  609. $ret =~ s/>/&gt;/g;
  610. $ret = "<pre>$ret</pre>" if($ret =~ m/\n/ );
  611. $ret =~ s/\n/<br>/g;
  612. }
  613. # find the longpoll connection with the same fw_id as the page that was the
  614. # origin of the get command
  615. my $found = 0;
  616. my $data = FW_longpollInfo('JSON',
  617. "#FHEMWEB:$FW_wname","FW_okDialog('$ret')","");
  618. foreach my $d (keys %defs ) {
  619. my $chash = $defs{$d};
  620. next if( $chash->{TYPE} ne 'FHEMWEB' );
  621. next if( !$chash->{inform} );
  622. next if( !$chash->{FW_ID} || $chash->{FW_ID} ne $hash->{FW_ID} );
  623. FW_addToWritebuffer($chash, $data."\n");
  624. $found = 1;
  625. last;
  626. }
  627. $defs{$FW_wname}{asyncOutput}{$hash->{FW_ID}} = $data if( !$found );
  628. return undef;
  629. }
  630. sub
  631. FW_closeConn($)
  632. {
  633. my ($hash) = @_;
  634. if(!$hash->{inform} && !$hash->{BUF}) { # Forum #41125
  635. my $cc = AttrVal($hash->{SNAME}, "closeConn",
  636. $FW_userAgent && $FW_userAgent=~m/(iPhone|iPad|iPod)/);
  637. if(!$FW_httpheader{Connection} || $cc) {
  638. TcpServer_Close($hash, 1);
  639. }
  640. }
  641. POSIX::exit(0) if($hash->{isChild});
  642. FW_Read($hash, 1) if($hash->{BUF});
  643. }
  644. ###########################
  645. sub
  646. FW_serveSpecial($$$$)
  647. {
  648. my ($file,$ext,$dir,$cacheable)= @_;
  649. $file =~ s,\.\./,,g; # little bit of security
  650. $file = "$FW_sp$file" if($ext eq "css" && -f "$dir/$FW_sp$file.$ext");
  651. $FW_RETTYPE = ext2MIMEType($ext);
  652. my $fname = ($ext ? "$file.$ext" : $file);
  653. return FW_returnFileAsStream("$dir/$fname", "", $FW_RETTYPE, 0, $cacheable);
  654. }
  655. sub
  656. FW_answerCall($)
  657. {
  658. my ($arg) = @_;
  659. my $me=$defs{$FW_cname}; # cache, else rereadcfg will delete us
  660. $FW_RET = "";
  661. $FW_RETTYPE = "text/html; charset=$FW_encoding";
  662. $FW_CSRF = (defined($defs{$FW_wname}{CSRFTOKEN}) ?
  663. "&fwcsrf=".$defs{$FW_wname}{CSRFTOKEN} : "");
  664. $MW_dir = "$attr{global}{modpath}/FHEM";
  665. $FW_sp = AttrVal($FW_wname, "stylesheetPrefix", "");
  666. $FW_ss = ($FW_sp =~ m/smallscreen/);
  667. $FW_tp = ($FW_sp =~ m/smallscreen|touchpad/);
  668. @FW_iconDirs = grep { $_ } split(":", AttrVal($FW_wname, "iconPath",
  669. "$FW_sp:default:fhemSVG:openautomation"));
  670. if($arg =~ m,$FW_ME/floorplan/([a-z0-9.:_]+),i) { # FLOORPLAN: special icondir
  671. unshift @FW_iconDirs, $1;
  672. FW_readIcons($1);
  673. }
  674. # /icons/... => current state of ...
  675. # also used for static images: unintended, but too late to change
  676. my ($dir1, $dirN, $ofile) = ($1, $2, $3)
  677. if($arg =~ m,^$FW_ME/([^/]*)(.*/)([^/]*)$,);
  678. if($arg =~ m,\brobots.txt$,) {
  679. Log3 $FW_wname, 1, "NOTE: $FW_wname is probed by a search engine";
  680. $FW_RETTYPE = "text/plain; charset=$FW_encoding";
  681. FW_pO "User-agent: *\r";
  682. FW_pO "Disallow: *\r";
  683. return 0;
  684. } elsif($arg =~ m,^$FW_ME/icons/(.*)$,) {
  685. my ($icon,$cacheable) = (urlDecode($1), 1);
  686. my $iconPath = FW_iconPath($icon);
  687. # if we do not have the icon, we convert the device state to the icon name
  688. if(!$iconPath) {
  689. my ($img, $link, $isHtml) = FW_dev2image($icon);
  690. $cacheable = 0;
  691. return 0 if(!$img);
  692. $iconPath = FW_iconPath($img);
  693. if($iconPath =~ m/\.svg$/i) {
  694. $FW_RETTYPE = ext2MIMEType("svg");
  695. FW_pO FW_makeImage($img, $img);
  696. return 0;
  697. }
  698. } elsif($iconPath =~ m/\.svg$/i && $icon=~ m/@/) {
  699. $FW_RETTYPE = ext2MIMEType("svg");
  700. FW_pO FW_makeImage($icon, $icon);
  701. return 0;
  702. }
  703. $iconPath =~ m/(.*)\.([^.]*)/;
  704. return FW_serveSpecial($1, $2, $FW_icondir, $cacheable);
  705. } elsif($dir1 && !$data{FWEXT}{"/$dir1"}) {
  706. my $dir = "$dir1$dirN";
  707. my $ext = "";
  708. $dir =~ s,/$,,;
  709. $dir =~ s/\.\.//g;
  710. $dir =~ s,www/,,g; # Want commandref.html to work from file://...
  711. my $file = urlDecode($ofile); # 69164
  712. $file =~ s/\?.*//; # Remove timestamp of CSS reloader
  713. if($file =~ m/^(.*)\.([^.]*)$/) {
  714. $file = $1; $ext = $2;
  715. }
  716. my $ldir = "$FW_dir/$dir";
  717. $ldir = "$FW_dir/pgm2" if($dir eq "css" || $dir eq "js"); # FLOORPLAN compat
  718. $ldir = "$attr{global}{modpath}/docs" if($dir eq "docs");
  719. # pgm2 check is for jquery-ui images
  720. my $static = ($ext =~ m/(css|js|png|jpg)/i || $dir =~ m/^pgm2/);
  721. my $fname = ($ext ? "$file.$ext" : $file);
  722. return FW_serveSpecial($file, $ext, $ldir, ($arg =~ m/nocache/) ? 0 : 1)
  723. if(-r "$ldir/$fname" || $static); # no return for FLOORPLAN
  724. $arg = "/$dir/$ofile";
  725. } elsif($arg =~ m/^$FW_ME(.*)/s) {
  726. $arg = $1; # The stuff behind FW_ME, continue to check for commands/FWEXT
  727. } else {
  728. Log3 $FW_wname, 4, "$FW_wname: redirecting $arg to $FW_ME";
  729. TcpServer_WriteBlocking($me,
  730. "HTTP/1.1 302 Found\r\n".
  731. "Content-Length: 0\r\n".
  732. $FW_headerlines.
  733. "Location: $FW_ME\r\n\r\n");
  734. FW_closeConn($FW_chash);
  735. return -1;
  736. }
  737. $FW_plotmode = AttrVal($FW_wname, "plotmode", "SVG");
  738. $FW_plotsize = AttrVal($FW_wname, "plotsize", $FW_ss ? "480,160" :
  739. $FW_tp ? "640,160" : "800,160");
  740. my ($cmd, $cmddev) = FW_digestCgi($arg);
  741. if($cmd && $FW_CSRF) {
  742. my $supplied = defined($FW_webArgs{fwcsrf}) ? $FW_webArgs{fwcsrf} : "";
  743. my $want = $defs{$FW_wname}{CSRFTOKEN};
  744. if($supplied ne $want) {
  745. Log3 $FW_wname, 3, "FHEMWEB $FW_wname CSRF error: $supplied ne $want ".
  746. "for client $FW_chash->{NAME}. ".
  747. "For details see the csrfToken FHEMWEB attribute.";
  748. $FW_httpRetCode = "400 Bad Request";
  749. return 0;
  750. }
  751. }
  752. if( $FW_id ) {
  753. $me->{FW_ID} = $FW_id;
  754. $me->{canAsyncOutput} = 1;
  755. }
  756. if($FW_inform) { # Longpoll header
  757. FW_initInform($me, 1);
  758. return -1;
  759. }
  760. my $docmd = 0;
  761. $docmd = 1 if($cmd &&
  762. $cmd !~ /^showlog/ &&
  763. $cmd !~ /^style / &&
  764. $cmd !~ /^edit/);
  765. #If we are in XHR or json mode, execute the command directly
  766. if($FW_XHR || $FW_jsonp) {
  767. $FW_cmdret = $docmd ? FW_fC($cmd, $cmddev) : undef;
  768. $FW_RETTYPE = $FW_chash->{contenttype} ?
  769. $FW_chash->{contenttype} : "text/plain; charset=$FW_encoding";
  770. delete($FW_chash->{contenttype});
  771. if($FW_jsonp) {
  772. $FW_cmdret =~ s/'/\\'/g;
  773. # Escape newlines in JavaScript string
  774. $FW_cmdret =~ s/\n/\\\n/g;
  775. FW_pO "$FW_jsonp('$FW_cmdret');";
  776. } else {
  777. $FW_cmdret = FW_addLinks($FW_cmdret) if($FW_webArgs{addLinks});
  778. FW_pO $FW_cmdret;
  779. }
  780. return 0;
  781. }
  782. ##############################
  783. # FHEMWEB extensions (FLOORPLOAN, SVG_WriteGplot, etc)
  784. my $FW_contentFunc;
  785. if(defined($data{FWEXT})) {
  786. foreach my $k (sort keys %{$data{FWEXT}}) {
  787. my $h = $data{FWEXT}{$k};
  788. next if($arg !~ m/^$k/);
  789. $FW_contentFunc = $h->{CONTENTFUNC};
  790. next if($h !~ m/HASH/ || !$h->{FUNC});
  791. #Returns undef as FW_RETTYPE if it already sent a HTTP header
  792. no strict "refs";
  793. ($FW_RETTYPE, $FW_RET) = &{$h->{FUNC}}($arg);
  794. if(defined($FW_RETTYPE) && $FW_RETTYPE =~ m,text/html,) {
  795. my $dataAttr = FW_dataAttr();
  796. $FW_RET =~ s/<body/<body $dataAttr/;
  797. }
  798. use strict "refs";
  799. return defined($FW_RETTYPE) ? 0 : -1;
  800. }
  801. }
  802. #Now execute the command
  803. $FW_cmdret = undef;
  804. if($docmd) {
  805. $FW_cmdret = FW_fC($cmd, $cmddev);
  806. if($cmd =~ m/^define +([^ ]+) /) { # "redirect" after define to details
  807. $FW_detail = $1;
  808. }
  809. elsif($cmd =~ m/^copy +([^ ]+) +([^ ]+)/) { # redirect define to details
  810. $FW_detail = $2;
  811. }
  812. }
  813. # Redirect after a command, to clean the browser URL window
  814. if($docmd && !defined($FW_cmdret) && AttrVal($FW_wname, "redirectCmds", 1)) {
  815. my $tgt = $FW_ME;
  816. if($FW_detail) { $tgt .= "?detail=$FW_detail&fw_id=$FW_id" }
  817. elsif($FW_room) { $tgt .= "?room=".urlEncode($FW_room)."&fw_id=$FW_id" }
  818. else { $tgt .= "?fw_id=$FW_id" }
  819. TcpServer_WriteBlocking($me,
  820. "HTTP/1.1 302 Found\r\n".
  821. "Content-Length: 0\r\n". $FW_headerlines.
  822. "Location: $tgt\r\n".
  823. "\r\n");
  824. return -1;
  825. }
  826. if($FW_lastWebName ne $FW_wname || $FW_lastHashUpdate != $lastDefChange) {
  827. FW_updateHashes();
  828. $FW_lastWebName = $FW_wname;
  829. $FW_lastHashUpdate = $lastDefChange;
  830. }
  831. my $hsh = "Home, Sweet Home";
  832. my $t = AttrVal($FW_wname, "title", AttrVal("global", "title", $hsh));
  833. $t = eval $t if($t =~ m/^{.*}$/s); # Forum #48668
  834. $t = $hsh if(!defined($t));
  835. FW_pO '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" '.
  836. '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
  837. FW_pO '<html xmlns="http://www.w3.org/1999/xhtml">';
  838. FW_pO "<head root=\"$FW_ME\">\n<title>$t</title>";
  839. FW_pO '<link rel="shortcut icon" href="'.FW_IconURL("favicon").'" />';
  840. FW_pO "<meta charset=\"$FW_encoding\">"; # Forum 28666
  841. FW_pO "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">";#Forum 18316
  842. # Enable WebApps
  843. if($FW_tp || $FW_ss) {
  844. my $icon = FW_iconPath("fhemicon_ios.png");
  845. $icon = $FW_ME."/images/".($icon ? $icon : "default/fhemicon_ios.png");
  846. my $viewport = '';
  847. if($FW_ss) {
  848. my $stf = $FW_userAgent =~ m/iPad|iPhone|iPod/ ? ",shrink-to-fit=no" :"";
  849. $viewport = "initial-scale=1.0,user-scalable=1$stf";
  850. } elsif($FW_tp) {
  851. $viewport = "width=768";
  852. }
  853. $viewport = AttrVal($FW_wname, "viewport", $viewport);
  854. FW_pO '<meta name="viewport" content="'.$viewport.'"/>' if ($viewport);
  855. FW_pO '<meta name="apple-mobile-web-app-capable" content="yes"/>';
  856. FW_pO '<meta name="mobile-web-app-capable" content="yes"/>'; # Forum #36183
  857. FW_pO '<link rel="apple-touch-icon" href="'.$icon.'"/>';
  858. FW_pO '<link rel="shortcut-icon" href="'.$icon.'"/>';
  859. }
  860. if(!$FW_detail) {
  861. my $rf = AttrVal($FW_wname, "refresh", "");
  862. FW_pO "<meta http-equiv=\"refresh\" content=\"$rf\">" if($rf);
  863. }
  864. ########################
  865. # CSS
  866. my $cssTemplate = "<link href=\"$FW_ME/%s\" rel=\"stylesheet\"/>";
  867. FW_pO sprintf($cssTemplate, "pgm2/style.css?v=$FW_styleStamp");
  868. FW_pO sprintf($cssTemplate, "pgm2/jquery-ui.min.css");
  869. map { FW_pO sprintf($cssTemplate, $_); }
  870. split(" ", AttrVal($FW_wname, "CssFiles", ""));
  871. ########################
  872. # JavaScripts
  873. my $jsTemplate =
  874. '<script attr=\'%s\' type="text/javascript" src="%s"></script>';
  875. FW_pO sprintf($jsTemplate, "", "$FW_ME/pgm2/jquery.min.js");
  876. FW_pO sprintf($jsTemplate, "", "$FW_ME/pgm2/jquery-ui.min.js");
  877. my (%jsNeg, @jsList); # jsNeg was used to exclude automatically loaded files
  878. map { $_ =~ m/^-(.*)$/ ? $jsNeg{$1} = 1 : push(@jsList, $_); }
  879. split(" ", AttrVal($FW_wname, "JavaScripts", ""));
  880. map { FW_pO sprintf($jsTemplate, "", "$FW_ME/pgm2/$_") if(!$jsNeg{$_}); }
  881. @FW_fhemwebjs;
  882. #######################
  883. # "Own" JavaScripts + their Attributes
  884. map {
  885. my $n = $_; $n =~ s+.*/++; $n =~ s/.js$//; $n =~ s/fhem_//; $n .= "Param";
  886. FW_pO sprintf($jsTemplate, AttrVal($FW_wname, $n, ""), "$FW_ME/$_");
  887. } @jsList;
  888. ########################
  889. # FW Extensions
  890. if(defined($data{FWEXT})) {
  891. foreach my $k (sort keys %{$data{FWEXT}}) {
  892. my $h = $data{FWEXT}{$k};
  893. next if($h !~ m/HASH/ || !$h->{SCRIPT} || $h->{SCRIPT} =~ m+pgm2/jquery+);
  894. my $script = $h->{SCRIPT};
  895. $script = ($script =~ m,^/,) ? "$FW_ME$script" : "$FW_ME/pgm2/$script";
  896. FW_pO sprintf($jsTemplate, "", $script);
  897. }
  898. }
  899. my $csrf= ($FW_CSRF ? "fwcsrf='$defs{$FW_wname}{CSRFTOKEN}'" : "");
  900. my $gen = 'generated="'.(time()-1).'"';
  901. my $lp = 'longpoll="'.AttrVal($FW_wname,"longpoll",1).'"';
  902. $FW_id = $FW_chash->{NR} if( !$FW_id );
  903. my $dataAttr = FW_dataAttr();
  904. FW_pO "</head>\n<body name='$t' fw_id='$FW_id' $gen $lp $csrf $dataAttr>";
  905. if($FW_activateInform) {
  906. $cmd = "style eventMonitor $FW_activateInform";
  907. $FW_cmdret = undef;
  908. $FW_activateInform = "";
  909. }
  910. FW_roomOverview($cmd);
  911. if(defined($FW_cmdret)) {
  912. $FW_detail = "";
  913. $FW_room = "";
  914. if( $FW_cmdret =~ m/^<html>(.*)<\/html>$/s ) {
  915. $FW_cmdret = $1;
  916. } else { # "linkify" output (e.g. for list)
  917. $FW_cmdret = FW_addLinks(FW_htmlEscape($FW_cmdret));
  918. $FW_cmdret =~ s/:\S+//g if($FW_cmdret =~ m/unknown.*choose one of/i);
  919. $FW_cmdret = "<pre>$FW_cmdret</pre>" if($FW_cmdret =~ m/\n/);
  920. }
  921. FW_addContent();
  922. if($FW_ss) {
  923. FW_pO "<div class=\"tiny\">$FW_cmdret</div>";
  924. } else {
  925. FW_pO $FW_cmdret;
  926. }
  927. FW_pO "</div>";
  928. }
  929. if($FW_contentFunc) {
  930. no strict "refs";
  931. my $ret = &{$FW_contentFunc}($arg);
  932. use strict "refs";
  933. return $ret if($ret);
  934. }
  935. if($cmd =~ m/^style /) { FW_style($cmd,undef); }
  936. elsif($FW_detail) { FW_doDetail($FW_detail); }
  937. elsif($FW_room) { FW_showRoom(); }
  938. elsif(!defined($FW_cmdret) &&
  939. !$FW_contentFunc) {
  940. $FW_room = AttrVal($FW_wname, "defaultRoom", '');
  941. if($FW_room ne '') {
  942. FW_showRoom();
  943. } else {
  944. my $motd = AttrVal("global","motd","none");
  945. if($motd ne "none") {
  946. $motd =~ s/\n/<br>/g;
  947. FW_addContent(">$motd</div");
  948. }
  949. }
  950. }
  951. FW_pO "</body></html>";
  952. return 0;
  953. }
  954. sub
  955. FW_dataAttr()
  956. {
  957. return
  958. "data-confirmDelete='" .AttrVal($FW_wname,"confirmDelete", 1)."' ".
  959. "data-confirmJSError='".AttrVal($FW_wname,"confirmJSError",1)."' ".
  960. "data-addHtmlTitle='" .AttrVal($FW_wname,"addHtmlTitle", 1)."' ".
  961. "data-availableJs='$FW_fhemwebjs' ".
  962. "data-webName='$FW_wname '";
  963. }
  964. sub
  965. FW_addContent(;$)
  966. {
  967. my $add = ($_[0] ? " $_[0]" : "");
  968. FW_pO "<div id='content' $add>";
  969. }
  970. sub
  971. FW_addLinks($)
  972. {
  973. return undef if(!defined($_[0]));
  974. my @lines = split( /\n/, $_[0]); # Adding links
  975. my $ret = "";
  976. foreach my $line (@lines) {
  977. $ret .= "\n" if( $ret );
  978. foreach my $word ( split( / /, $line ) ) {
  979. $word = "<a href=\"$FW_ME$FW_subdir?detail=$word\">$word</a>"
  980. if( $defs{$word} );
  981. $ret .= "$word ";
  982. }
  983. }
  984. return $ret;
  985. }
  986. ###########################
  987. # Digest CGI parameters
  988. sub
  989. FW_digestCgi($)
  990. {
  991. my ($arg) = @_;
  992. my (%arg, %val, %dev);
  993. my ($cmd, $c) = ("","","");
  994. %FW_pos = ();
  995. $FW_room = "";
  996. $FW_detail = "";
  997. $FW_XHR = undef;
  998. $FW_id = "";
  999. $FW_jsonp = undef;
  1000. $FW_inform = undef;
  1001. %FW_webArgs = ();
  1002. #Remove (nongreedy) everything including the first '?'
  1003. $arg =~ s,^.*?[?],,;
  1004. foreach my $pv (split("&", $arg)) {
  1005. next if($pv eq ""); # happens when post forgot to set FW_ME
  1006. $pv =~ s/\+/ /g;
  1007. $pv =~ s/%([\dA-F][\dA-F])/chr(hex($1))/ige;
  1008. my ($p,$v) = split("=",$pv, 2);
  1009. $v = "" if(!defined($v));
  1010. # Multiline: escape the NL for fhem
  1011. $v =~ s/[\r]//g if($v && $p && $p ne "data");
  1012. $FW_webArgs{$p} = $v;
  1013. if($p eq "detail") { $FW_detail = $v; }
  1014. if($p eq "room") { $FW_room = $v; }
  1015. if($p eq "cmd") { $cmd = $v; }
  1016. if($p =~ m/^arg\.(.*)$/) { $arg{$1} = $v; }
  1017. if($p =~ m/^val\.(.*)$/) { $val{$1} = ($val{$1} ? $val{$1}.",$v" : $v) }
  1018. if($p =~ m/^dev\.(.*)$/) { $dev{$1} = $v; }
  1019. if($p =~ m/^cmd\.(.*)$/) { $cmd = $v; $c = $1; }
  1020. if($p eq "pos") { %FW_pos = split(/[=;]/, $v); }
  1021. if($p eq "data") { $FW_data = $v; }
  1022. if($p eq "XHR") { $FW_XHR = 1; }
  1023. if($p eq "fw_id") { $FW_id = $v; }
  1024. if($p eq "jsonp") { $FW_jsonp = $v; }
  1025. if($p eq "inform") { $FW_inform = $v; }
  1026. }
  1027. $cmd.=" $dev{$c}" if(defined($dev{$c}));
  1028. $cmd.=" $arg{$c}" if(defined($arg{$c}) &&
  1029. ($arg{$c} ne "state" || $cmd !~ m/^set/));
  1030. $cmd.=" $val{$c}" if(defined($val{$c}));
  1031. #replace unicode newline symbol \u2424 with real newline
  1032. my $nl = chr(226) . chr(144) . chr(164);
  1033. $cmd =~ s/$nl/\n/g;
  1034. return ($cmd, $c);
  1035. }
  1036. #####################
  1037. # create FW_rooms && FW_types
  1038. sub
  1039. FW_updateHashes()
  1040. {
  1041. %FW_rooms = (); # Make a room hash
  1042. %FW_groups = (); # Make a group hash
  1043. %FW_types = (); # Needed for type sorting
  1044. my $hre = AttrVal($FW_wname, "hiddenroomRegexp", "");
  1045. foreach my $d (keys %defs ) {
  1046. next if(IsIgnored($d));
  1047. foreach my $r (split(",", AttrVal($d, "room", "Unsorted"))) {
  1048. next if($hre && $r =~ m/$hre/);
  1049. $FW_rooms{$r}{$d} = 1;
  1050. }
  1051. foreach my $r (split(",", AttrVal($d, "group", ""))) {
  1052. $FW_groups{$r}{$d} = 1;
  1053. }
  1054. my $t = AttrVal($d, "subType", $defs{$d}{TYPE});
  1055. $t = AttrVal($d, "model", $t) if($t && $t eq "unknown"); # RKO: ???
  1056. $FW_types{$d} = $t;
  1057. }
  1058. $FW_room = AttrVal($FW_detail, "room", "Unsorted") if($FW_detail);
  1059. if(AttrVal($FW_wname, "sortRooms", "")) { # Slow!
  1060. my @sortBy = split( " ", AttrVal( $FW_wname, "sortRooms", "" ) );
  1061. my %sHash;
  1062. map { $sHash{$_} = FW_roomIdx(\@sortBy,$_) } keys %FW_rooms;
  1063. @FW_roomsArr = sort { $sHash{$a} cmp $sHash{$b} } keys %FW_rooms;
  1064. } else {
  1065. @FW_roomsArr = sort keys %FW_rooms;
  1066. }
  1067. }
  1068. ##############################
  1069. sub
  1070. FW_makeTable($$$@)
  1071. {
  1072. my($title, $name, $hash, $cmd) = (@_);
  1073. return if(!$hash || !int(keys %{$hash}));
  1074. my $class = lc($title);
  1075. $class =~ s/[^A-Za-z]/_/g;
  1076. FW_pO "<div class='makeTable wide ".lc($title)."'>";
  1077. FW_pO $title;
  1078. FW_pO "<table class=\"block wide $class\">";
  1079. my $si = AttrVal("global", "showInternalValues", 0);
  1080. my $row = 1;
  1081. foreach my $n (sort keys %{$hash}) {
  1082. next if(!$si && $n =~ m/^\./); # Skip "hidden" Values
  1083. my $val = $hash->{$n};
  1084. $val = "" if(!defined($val));
  1085. $val = $hash->{$n}{NAME} # Exception
  1086. if($n eq "IODev" && ref($val) eq "HASH" && defined($hash->{$n}{NAME}));
  1087. my $r = ref($val);
  1088. next if($r && ($r ne "HASH" || !defined($hash->{$n}{VAL})));
  1089. FW_pF "<tr class=\"%s\">", ($row&1)?"odd":"even";
  1090. $row++;
  1091. if($n eq "DEF" && !$FW_hiddenroom{input}) {
  1092. FW_makeEdit($name, $n, $val);
  1093. } else {
  1094. FW_pO "<td><div class=\"dname\" data-name=\"$name\">$n</div></td>";
  1095. if(ref($val)) { #handle readings
  1096. my ($v, $t) = ($val->{VAL}, $val->{TIME});
  1097. if($v =~ m,^<html>(.*)</html>$,) {
  1098. $v = $1;
  1099. } else {
  1100. $v = FW_htmlEscape($v);
  1101. }
  1102. if($FW_ss) {
  1103. $t = ($t ? "<br><div class=\"tiny\">$t</div>" : "");
  1104. FW_pO "<td><div class=\"dval\">$v$t</div></td>";
  1105. } else {
  1106. $t = "" if(!$t);
  1107. FW_pO "<td><div class=\"dval\" informId=\"$name-$n\">$v</div></td>";
  1108. FW_pO "<td><div informId=\"$name-$n-ts\">$t</div></td>";
  1109. }
  1110. } else {
  1111. $val = FW_htmlEscape($val);
  1112. my $tattr = "informId=\"$name-$n\" class=\"dval\"";
  1113. # if possible provide some links
  1114. if ($n eq "room"){
  1115. FW_pO "<td><div $tattr>".
  1116. join(",", map { FW_pH("room=$_",$_,0,"",1,1) } split(",",$val)).
  1117. "</div></td>";
  1118. } elsif ($n =~ m/^fp_(.*)/ && $defs{$1}){ #special for Floorplan
  1119. FW_pH "detail=$1", $val,1;
  1120. } elsif ($modules{$val} ) {
  1121. FW_pH "cmd=list%20TYPE=$val", $val,1;
  1122. } else {
  1123. $val = "<pre>$val</pre>" if($val =~ m/\n/ && $title eq "Attributes");
  1124. FW_pO "<td><div $tattr>".
  1125. join(",", map { ($_ ne $name && $defs{$_}) ?
  1126. FW_pH( "detail=$_", $_ ,0,"",1,1) : $_ } split(",",$val)).
  1127. "</div></td>";
  1128. }
  1129. }
  1130. }
  1131. FW_pH "cmd.$name=$cmd $name $n&amp;detail=$name", $cmd, 1
  1132. if($cmd && !$FW_ss);
  1133. FW_pO "</tr>";
  1134. }
  1135. FW_pO "</table>";
  1136. FW_pO "</div>";
  1137. }
  1138. ##############################
  1139. # Used only for set or attr lists.
  1140. sub
  1141. FW_detailSelect(@)
  1142. {
  1143. my ($d, $cmd, $list, $param) = @_;
  1144. return "" if(!$list || $FW_hiddenroom{input});
  1145. my %al = map { s/:.*//;$_ => 1 } split(" ", $list);
  1146. my @al = sort keys %al; # remove duplicate items in list
  1147. my $selEl = (defined($al[0]) ? $al[0] : " ");
  1148. $selEl = $1 if($list =~ m/([^ ]*):slider,/); # promote a slider if available
  1149. $selEl = "room" if($list =~ m/room:/);
  1150. $list =~ s/"/&quot;/g;
  1151. my $ret ="";
  1152. my $psc = AttrVal("global", "perlSyntaxCheck", ($featurelevel>5.7) ? 1 : 0);
  1153. $ret .= "<div class='makeSelect' dev=\"$d\" cmd=\"$cmd\" list=\"$list\">";
  1154. $ret .= "<form method=\"$FW_formmethod\" ".
  1155. "action=\"$FW_ME$FW_subdir\" autocomplete=\"off\">";
  1156. $ret .= FW_hidden("detail", $d);
  1157. $ret .= FW_hidden("dev.$cmd$d", $d.($param ? " $param":""));
  1158. $ret .= FW_submit("cmd.$cmd$d", $cmd, $cmd.($psc?" psc":""));
  1159. $ret .= "<div class=\"$cmd downText\">&nbsp;$d&nbsp;".
  1160. ($param ? "&nbsp;$param":"")."</div>";
  1161. $ret .= FW_select("sel_$cmd$d","arg.$cmd$d",\@al, $selEl, $cmd);
  1162. $ret .= FW_textfield("val.$cmd$d", 30, $cmd);
  1163. $ret .= "</form></div>";
  1164. return $ret;
  1165. }
  1166. ##############################
  1167. sub
  1168. FW_doDetail($)
  1169. {
  1170. my ($d) = @_;
  1171. return if($FW_hiddenroom{detail});
  1172. return if(!defined($defs{$d}));
  1173. my $h = $defs{$d};
  1174. my $t = $h->{TYPE};
  1175. $t = "MISSING" if(!defined($t));
  1176. FW_addContent();
  1177. if($FW_ss) {
  1178. my $webCmd = AttrVal($d, "webCmd", undef);
  1179. if($webCmd) {
  1180. FW_pO "<table class=\"webcmd\">";
  1181. foreach my $cmd (split(":", $webCmd)) {
  1182. FW_pO "<tr>";
  1183. FW_pH "cmd.$d=set $d $cmd&detail=$d", $cmd, 1, "col1";
  1184. FW_pO "</tr>";
  1185. }
  1186. FW_pO "</table>";
  1187. }
  1188. }
  1189. FW_pO "<table><tr><td>";
  1190. if(!$modules{$t}{FW_detailFn} || $modules{$t}{FW_deviceOverview}) {
  1191. my $show = AttrVal($FW_wname, "deviceOverview", "always");
  1192. if( $show ne 'never' ) {
  1193. my %extPage = ();
  1194. if( $show eq 'iconOnly' ) {
  1195. my ($allSets, $cmdlist, $txt) = FW_devState($d, $FW_room, \%extPage);
  1196. FW_pO "<div informId='$d'".
  1197. ($FW_tp?"":" style='float:right'").">$txt</div>";
  1198. } else {
  1199. my $nameDisplay = AttrVal($FW_wname,"nameDisplay",undef);
  1200. my %usuallyAtEnd = ();
  1201. my $style = "";
  1202. if( $show eq 'onClick' ) {
  1203. my $pgm = "Javascript:" .
  1204. "s=document.getElementById('ddtable').style;".
  1205. "s.display = s.display=='none' ? 'block' : 'none';".
  1206. "s=document.getElementById('ddisp').style;".
  1207. "s.display = s.display=='none' ? 'block' : 'none';";
  1208. FW_pO "<div id=\"ddisp\"><br><a style=\"cursor:pointer\" ".
  1209. "onClick=\"$pgm\">Show DeviceOverview</a><br><br></div>";
  1210. $style = 'style="display:none"';
  1211. }
  1212. FW_pO "<div $style id=\"ddtable\" class='makeTable wide'>";
  1213. FW_pO "DeviceOverview";
  1214. FW_pO "<table class=\"block wide\">";
  1215. FW_makeDeviceLine($d,1,\%extPage,$nameDisplay,\%usuallyAtEnd);
  1216. FW_pO "</table></div>";
  1217. }
  1218. }
  1219. }
  1220. if($modules{$t}{FW_detailFn}) {
  1221. no strict "refs";
  1222. my $txt = &{$modules{$t}{FW_detailFn}}($FW_wname, $d, $FW_room);
  1223. FW_pO "</td></tr><tr><td>$txt<br>" if(defined($txt));
  1224. use strict "refs";
  1225. }
  1226. FW_pO FW_detailSelect($d, "set", FW_widgetOverride($d, getAllSets($d)));
  1227. FW_pO FW_detailSelect($d, "get", FW_widgetOverride($d, getAllGets($d)));
  1228. FW_makeTable("Internals", $d, $h);
  1229. FW_makeTable("Readings", $d, $h->{READINGS});
  1230. my $attrList = getAllAttr($d);
  1231. my $roomList = "multiple,".join(",",
  1232. sort map { $_ =~ s/ /#/g ;$_} keys %FW_rooms);
  1233. my $groupList = "multiple,".join(",",
  1234. sort map { $_ =~ s/ /#/g ;$_} keys %FW_groups);
  1235. $attrList =~ s/room /room:$roomList /;
  1236. $attrList =~ s/group /group:$groupList /;
  1237. $attrList = FW_widgetOverride($d, $attrList);
  1238. $attrList =~ s/\\/\\\\/g;
  1239. $attrList =~ s/'/\\'/g;
  1240. FW_pO FW_detailSelect($d, "attr", $attrList);
  1241. FW_makeTable("Attributes", $d, $attr{$d}, "deleteattr");
  1242. FW_makeTableFromArray("Probably associated with", "assoc", getPawList($d));
  1243. FW_pO "</td></tr></table>";
  1244. my ($link, $txt, $td, $class, $doRet,$nonl) = @_;
  1245. FW_pH "cmd=style iconFor $d", "Select icon", undef, "detLink iconFor";
  1246. FW_pH "cmd=style showDSI $d", "Extend devStateIcon", undef, "detLink showDSI";
  1247. FW_pH "cmd=rawDef $d", "Raw definition", undef, "detLink rawDef";
  1248. FW_pH "cmd=delete $d", "Delete this device ($d)", undef, "detLink delDev"
  1249. if($d ne "global");
  1250. my $sfx = AttrVal("global", "language", "EN");
  1251. $sfx = ($sfx eq "EN" ? "" : "_$sfx");
  1252. FW_pH "$FW_ME/docs/commandref${sfx}.html#${t}", "Device specific help",
  1253. undef, "detLink devSpecHelp";
  1254. FW_pO "<br><br>";
  1255. FW_pO "</div>";
  1256. }
  1257. ##############################
  1258. sub
  1259. FW_makeTableFromArray($$@) {
  1260. my ($txt,$class,@obj) = @_;
  1261. if (@obj>0) {
  1262. my $row=1;
  1263. FW_pO "<div class='makeTable wide'>";
  1264. FW_pO "$txt";
  1265. FW_pO "<table class=\"block wide $class\">";
  1266. foreach (sort @obj) {
  1267. FW_pF "<tr class=\"%s\"><td>", (($row++)&1)?"odd":"even";
  1268. FW_pH "detail=$_", $_;
  1269. FW_pO "</td><td>";
  1270. FW_pO $defs{$_}{STATE} if(defined($defs{$_}{STATE}));
  1271. FW_pO "</td><td>";
  1272. FW_pH "cmd=list TYPE=$defs{$_}{TYPE}", $defs{$_}{TYPE};
  1273. FW_pO "</td>";
  1274. FW_pO "</tr>";
  1275. }
  1276. FW_pO "</table></div>";
  1277. }
  1278. }
  1279. sub
  1280. FW_roomIdx($$)
  1281. {
  1282. my ($arr,$v) = @_;
  1283. my ($index) = grep { $v =~ /^$arr->[$_]$/ } 0..$#$arr;
  1284. if( !defined($index) ) {
  1285. $index = 9999;
  1286. } else {
  1287. $index = sprintf( "%03i", $index );
  1288. }
  1289. return "$index-$v";
  1290. }
  1291. ##############
  1292. # Header, Zoom-Icons & list of rooms at the left.
  1293. sub
  1294. FW_roomOverview($)
  1295. {
  1296. my ($cmd) = @_;
  1297. %FW_hiddenroom = ();
  1298. foreach my $r (split(",",AttrVal($FW_wname, "hiddenroom", ""))) {
  1299. $FW_hiddenroom{$r} = 1;
  1300. }
  1301. ##############
  1302. # LOGO
  1303. my $hasMenuScroll;
  1304. if($FW_detail && $FW_ss) {
  1305. $FW_room = AttrVal($FW_detail, "room", undef);
  1306. $FW_room = $1 if($FW_room && $FW_room =~ m/^([^,]*),/);
  1307. $FW_room = "" if(!$FW_room);
  1308. FW_pO(FW_pHPlain("room=$FW_room",
  1309. "<div id=\"back\">" . FW_makeImage("back") . "</div>"));
  1310. FW_pO "<div id=\"menu\">$FW_detail details</div>";
  1311. return;
  1312. } else {
  1313. $hasMenuScroll = 1;
  1314. FW_pO '<div id="menuScrollArea">';
  1315. FW_pH "", '<div id="logo"></div>';
  1316. }
  1317. ##############
  1318. # MENU
  1319. my (@list1, @list2);
  1320. push(@list1, ""); push(@list2, "");
  1321. if(!$FW_hiddenroom{save} && !$FW_hiddenroom{"Save config"}) {
  1322. push(@list1, "Save config");
  1323. push(@list2, "$FW_ME?cmd=save");
  1324. push(@list1, ""); push(@list2, "");
  1325. }
  1326. ########################
  1327. # Show FW Extensions in the menu
  1328. if(defined($data{FWEXT})) {
  1329. my $cnt = 0;
  1330. foreach my $k (sort keys %{$data{FWEXT}}) {
  1331. my $h = $data{FWEXT}{$k};
  1332. next if($h !~ m/HASH/ || !$h->{LINK} || !$h->{NAME});
  1333. next if($FW_hiddenroom{$h->{NAME}});
  1334. push(@list1, $h->{NAME});
  1335. push(@list2, $FW_ME ."/".$h->{LINK});
  1336. $cnt++;
  1337. }
  1338. if($cnt > 0) {
  1339. push(@list1, ""); push(@list2, "");
  1340. }
  1341. }
  1342. $FW_room = "" if(!$FW_room);
  1343. ##########################
  1344. # Rooms and other links
  1345. foreach my $r (@FW_roomsArr) {
  1346. next if($r eq "hidden" || $FW_hiddenroom{$r});
  1347. $FW_room = AttrVal($FW_wname, "defaultRoom", $r)
  1348. if(!$FW_room && $FW_ss);
  1349. my $lr = $r;
  1350. $lr =~ s/</&lt;/g;
  1351. $lr =~ s/>/&gt;/g;
  1352. push @list1, $lr;
  1353. push @list2, "$FW_ME?room=".urlEncode($r);
  1354. }
  1355. my $sfx = AttrVal("global", "language", "EN");
  1356. $sfx = ($sfx eq "EN" ? "" : "_$sfx");
  1357. my @list = (
  1358. "Everything", "$FW_ME?room=all",
  1359. "", "",
  1360. "Commandref", "$FW_ME/docs/commandref${sfx}.html",
  1361. "Remote doc", "http://fhem.de/fhem.html#Documentation",
  1362. "Edit files", "$FW_ME?cmd=style%20list",
  1363. "Select style", "$FW_ME?cmd=style%20select",
  1364. "Event monitor", "$FW_ME?cmd=style%20eventMonitor",
  1365. "", "");
  1366. my $lastname = ","; # Avoid double "".
  1367. my $lfn = "Logfile";
  1368. if($defs{$lfn}) { # Add the current Logfile to the list if defined
  1369. my @l = FW_fileList($defs{$lfn}{logfile},1);
  1370. my $fn = pop @l;
  1371. splice @list, 4,0, ("Logfile",
  1372. "$FW_ME/FileLog_logWrapper?dev=$lfn&type=text&file=$fn");
  1373. }
  1374. my @me = split(",", AttrVal($FW_wname, "menuEntries", ""));
  1375. push @list, @me, "", "" if(@me);
  1376. for(my $idx = 0; $idx < @list; $idx+= 2) {
  1377. next if($FW_hiddenroom{$list[$idx]} || $list[$idx] eq $lastname);
  1378. push @list1, $list[$idx];
  1379. push @list2, $list[$idx+1];
  1380. $lastname = $list[$idx];
  1381. }
  1382. FW_pO "<div id=\"menu\">";
  1383. FW_pO "<table>";
  1384. if($FW_ss) { # Make a selection sensitive dropdown list
  1385. FW_pO "<tr><td><select OnChange=\"location.href=" .
  1386. "this.options[this.selectedIndex].value\">";
  1387. foreach(my $idx = 0; $idx < @list1; $idx++) {
  1388. next if(!$list1[$idx]);
  1389. my $sel = ($list1[$idx] eq $FW_room ? " selected=\"selected\"" : "");
  1390. my $csrf = ($list2[$idx] =~ m/cmd=/ ? $FW_CSRF : '');
  1391. FW_pO "<option value='$list2[$idx]$csrf'$sel>$list1[$idx]</option>";
  1392. }
  1393. FW_pO "</select></td>";
  1394. FW_pO "</tr>";
  1395. } else {
  1396. my $tblnr = 1;
  1397. foreach(my $idx = 0; $idx < @list1; $idx++) {
  1398. my ($l1, $l2) = ($list1[$idx], $list2[$idx]);
  1399. if(!$l1) {
  1400. FW_pO "</table></td></tr>" if($idx);
  1401. if($idx<int(@list1)-1) {
  1402. FW_pO "<tr><td><table class=\"room roomBlock$tblnr\">";
  1403. $tblnr++;
  1404. }
  1405. } else {
  1406. FW_pF "<tr%s>", $l1 eq $FW_room ? " class=\"sel\"" : "";
  1407. my $class = "menu_$l1";
  1408. $class =~ s/[^A-Z0-9]/_/gi;
  1409. # image tag if we have an icon, else empty
  1410. my $icoName = "ico$l1";
  1411. map { my ($n,$v) = split(":",$_); $icoName=$v if($l1 =~ m/^$n$/); }
  1412. split(" ", AttrVal($FW_wname, "roomIcons", ""));
  1413. my $icon = FW_iconName($icoName) ?
  1414. FW_makeImage($icoName,$icoName,"icon")."&nbsp;" : "";
  1415. if($l1 eq "Save config") {
  1416. $l1 .= '</a> <a id="saveCheck" class="changed" style="visibility:'.
  1417. (int(@structChangeHist) ? 'visible' : 'hidden').'">?';
  1418. }
  1419. # Force external browser if FHEMWEB is installed as an offline app.
  1420. my $target = ''; # Forum 33066, 39854
  1421. $target = 'target="_blank"' if($l2 =~ s/^$FW_ME\/\+/$FW_ME\//);
  1422. $target = 'target="_blank"' if($l2 =~ m/commandref|fhem.de.fhem.html/);
  1423. if($l2 =~ m/.html$/ || $l2 =~ m/^(http|javascript)/ || length($target)){
  1424. FW_pO "<td><div><a href=\"$l2\" $target >$icon$l1</a></div></td>";
  1425. } else {
  1426. FW_pH $l2, "$icon$l1", 1, $class;
  1427. }
  1428. FW_pO "</tr>";
  1429. }
  1430. }
  1431. }
  1432. FW_pO "</table>";
  1433. FW_pO "</div>";
  1434. FW_pO "</div>" if($hasMenuScroll);
  1435. ##############
  1436. # HEADER
  1437. FW_pO "<div id=\"hdr\">";
  1438. FW_pO '<table border="0" class="header"><tr><td style="padding:0">';
  1439. FW_pO "<form method=\"$FW_formmethod\" action=\"$FW_ME\">";
  1440. FW_pO FW_hidden("fw_id", $FW_id) if($FW_id);
  1441. FW_pO FW_hidden("room", $FW_room) if($FW_room);
  1442. FW_pO FW_hidden("fwcsrf", $defs{$FW_wname}{CSRFTOKEN}) if($FW_CSRF);
  1443. FW_pO FW_textfield("cmd",
  1444. AttrVal($FW_wname, "mainInputLength", $FW_ss ? 25 : 40), "maininput");
  1445. FW_pO "</form>";
  1446. FW_pO "</td></tr></table>";
  1447. FW_pO "</div>";
  1448. }
  1449. sub
  1450. FW_alias($)
  1451. {
  1452. my ($d) = @_;
  1453. if($FW_room) {
  1454. return AttrVal($d, "alias_$FW_room", AttrVal($d, "alias", $d));
  1455. } else {
  1456. return AttrVal($d, "alias", $d);
  1457. }
  1458. }
  1459. sub
  1460. FW_makeDeviceLine($$$$$)
  1461. {
  1462. my ($d,$row,$extPage,$nameDisplay,$usuallyAtEnd) = @_;
  1463. my $rf = ($FW_room ? "&amp;room=$FW_room" : ""); # stay in the room
  1464. FW_pF "\n<tr class=\"%s\">", ($row&1)?"odd":"even";
  1465. my $devName = FW_alias($d);
  1466. if(defined($nameDisplay)) {
  1467. my ($DEVICE, $ALIAS) = ($d, $devName);
  1468. $devName = eval $nameDisplay;
  1469. }
  1470. my $icon = AttrVal($d, "icon", "");
  1471. $icon = FW_makeImage($icon,$icon,"icon") . "&nbsp;" if($icon);
  1472. if($FW_hiddenroom{detail}) {
  1473. FW_pO "<td><div class=\"col1\">$icon$devName</div></td>"
  1474. if(!$usuallyAtEnd->{$d});
  1475. } else {
  1476. FW_pH "detail=$d", "$icon$devName", 1, "col1" if(!$usuallyAtEnd->{$d});
  1477. }
  1478. my ($allSets, $cmdlist, $txt) = FW_devState($d, $rf, $extPage);
  1479. if($cmdlist) {
  1480. my $cl2 = $cmdlist; $cl2 =~ s/ [^:]*//g; $cl2 =~ s/:/ /g; # Forum #74053
  1481. $allSets = "$allSets $cl2";
  1482. }
  1483. $allSets = FW_widgetOverride($d, $allSets);
  1484. my $colSpan = ($usuallyAtEnd->{$d} ? ' colspan="2"' : '');
  1485. FW_pO "<td informId=\"$d\"$colSpan>$txt</td>";
  1486. ######
  1487. # Commands, slider, dropdown
  1488. my $smallscreenCommands = AttrVal($FW_wname, "smallscreenCommands", "");
  1489. if((!$FW_ss || $smallscreenCommands) && $cmdlist) {
  1490. my @a = split("[: ]", AttrVal($d, "cmdIcon", ""));
  1491. Log 1, "ERROR: bad cmdIcon definition for $d" if(@a % 2);
  1492. my %cmdIcon = @a;
  1493. my @cl = split(":", $cmdlist);
  1494. my @wcl = split(":", AttrVal($d, "webCmdLabel", ""));
  1495. my $nRows;
  1496. $nRows = split("\n", AttrVal($d, "webCmdLabel", "")) if(@wcl);
  1497. @wcl = () if(@wcl != @cl); # some safety
  1498. for(my $i1=0; $i1<@cl; $i1++) {
  1499. my $cmd = $cl[$i1];
  1500. my $htmlTxt;
  1501. my @c = split(' ', $cmd); # @c==0 if $cmd==" ";
  1502. if(int(@c) && $allSets && $allSets =~ m/\b$c[0]:([^ ]*)/) {
  1503. my $values = $1;
  1504. foreach my $fn (sort keys %{$data{webCmdFn}}) {
  1505. no strict "refs";
  1506. $htmlTxt = &{$data{webCmdFn}{$fn}}($FW_wname,
  1507. $d, $FW_room, $cmd, $values);
  1508. use strict "refs";
  1509. last if(defined($htmlTxt));
  1510. }
  1511. }
  1512. if($htmlTxt) {
  1513. $htmlTxt =~ s,^<td[^>]*>(.*)</td>$,$1,;
  1514. } else {
  1515. my $nCmd = $cmdIcon{$cmd} ?
  1516. FW_makeImage($cmdIcon{$cmd},$cmd,"webCmd") : $cmd;
  1517. $htmlTxt = FW_pH "cmd.$d=set $d $cmd$rf", $nCmd, 0, "", 1, 1;
  1518. }
  1519. if(@wcl > $i1) {
  1520. if($nRows > 1) {
  1521. FW_pO "<td><table class='wide'><tr>" if($i1 == 0);
  1522. FW_pO "<td>$wcl[$i1]</td><td>$htmlTxt</td>";
  1523. FW_pO "</tr><tr>" if($wcl[$i1] =~ m/\n/);
  1524. FW_pO "</tr></table></td>" if($i1 == @cl-1);
  1525. } else {
  1526. FW_pO "<td><div class='col3'>$wcl[$i1]$ htmlTxt</div></td>";
  1527. }
  1528. } else {
  1529. FW_pO "<td><div class='col3'>$htmlTxt</div></td>";
  1530. }
  1531. }
  1532. }
  1533. FW_pO "</tr>";
  1534. }
  1535. sub
  1536. FW_sortIndex($)
  1537. {
  1538. my ($d) = @_;
  1539. return $d if(!$attr{$d});
  1540. my $val = $attr{$d}{sortby};
  1541. if($val) {
  1542. if($val =~ m/^{.*}/) {
  1543. my %specials=("%NAME" => $d);
  1544. my $exec = EvalSpecials($val, %specials);
  1545. return AnalyzePerlCommand($FW_chash, $exec);
  1546. }
  1547. return lc($val);
  1548. }
  1549. if($FW_room) {
  1550. $val = $attr{$d}{"alias_$FW_room"};
  1551. return $val if($val);
  1552. }
  1553. $val = $attr{$d}{"alias"};
  1554. return $val if($val);
  1555. return $d;
  1556. }
  1557. ########################
  1558. # Show the overview of devices in one room
  1559. # room can be a room, all or Unsorted
  1560. sub
  1561. FW_showRoom()
  1562. {
  1563. return if(!$FW_room);
  1564. %FW_hiddengroup = ();
  1565. foreach my $r (split(",",AttrVal($FW_wname, "hiddengroup", ""))) {
  1566. $FW_hiddengroup{$r} = 1;
  1567. }
  1568. my $hge = AttrVal($FW_wname, "hiddengroupRegexp", undef);
  1569. FW_pO "<form method=\"$FW_formmethod\" ". # Why do we need a form here?
  1570. "action=\"$FW_ME\" autocomplete=\"off\">";
  1571. FW_addContent("room='$FW_room'");
  1572. FW_pO "<table class=\"roomoverview\">"; # Need for equal width of subtables
  1573. # array of all device names in the room (exception weblinks without group
  1574. # attribute)
  1575. my @devs= grep { (($FW_rooms{$FW_room} && $FW_rooms{$FW_room}{$_}) ||
  1576. $FW_room eq "all") && !IsIgnored($_) } keys %defs;
  1577. my (%group, @atEnds, %usuallyAtEnd, %sortIndex);
  1578. foreach my $dev (@devs) {
  1579. if($modules{$defs{$dev}{TYPE}}{FW_atPageEnd}) {
  1580. $usuallyAtEnd{$dev} = 1;
  1581. if(!AttrVal($dev, "group", undef)) {
  1582. $sortIndex{$dev} = FW_sortIndex($dev);
  1583. push @atEnds, $dev;
  1584. next;
  1585. }
  1586. }
  1587. next if(!$FW_types{$dev}); # FHEMWEB connection, missed due to caching
  1588. foreach my $grp (split(",", AttrVal($dev, "group", $FW_types{$dev}))) {
  1589. next if($FW_hiddengroup{$grp});
  1590. next if($hge && $grp =~ m/$hge/);
  1591. $sortIndex{$dev} = FW_sortIndex($dev);
  1592. $group{$grp}{$dev} = 1;
  1593. }
  1594. }
  1595. # row counter
  1596. my $row=1;
  1597. my %extPage = ();
  1598. my $nameDisplay = AttrVal($FW_wname,"nameDisplay",undef);
  1599. my ($columns, $maxc) = FW_parseColumns(\%group);
  1600. FW_pO "<tr class=\"column\">" if($maxc != -1);
  1601. for(my $col=1; $col < ($maxc==-1 ? 2 : $maxc); $col++) {
  1602. FW_pO "<td><table class=\"column tblcol_$col\">" if($maxc != -1);
  1603. # iterate over the distinct groups
  1604. foreach my $g (sort { $maxc==-1 ?
  1605. $a cmp $b :
  1606. ($columns->{$a} ? $columns->{$a}->[0] : 99) <=>
  1607. ($columns->{$b} ? $columns->{$b}->[0] : 99) } keys %group) {
  1608. next if($maxc != -1 && (!$columns->{$g} || $columns->{$g}->[1] != $col));
  1609. #################
  1610. # Check if there is a device of this type in the room
  1611. FW_pO "\n<tr><td><div class=\"devType\">$g</div></td></tr>";
  1612. FW_pO "<tr><td>";
  1613. FW_pO "<table class=\"block wide\" id=\"TYPE_$g\">";
  1614. foreach my $d (sort { $sortIndex{$a} cmp $sortIndex{$b} }
  1615. keys %{$group{$g}}) {
  1616. my $type = $defs{$d}{TYPE};
  1617. $extPage{group} = $g;
  1618. FW_makeDeviceLine($d,$row,\%extPage,$nameDisplay,\%usuallyAtEnd);
  1619. if($modules{$type}{FW_addDetailToSummary}) {
  1620. no strict "refs";
  1621. my $txt = &{$modules{$type}{FW_detailFn}}($FW_wname, $d, $FW_room);
  1622. use strict "refs";
  1623. if(defined($txt)) {
  1624. FW_pO "<tr class='".($row&1?"odd":"even").
  1625. "'><td colspan='50'>$txt</td></tr>";
  1626. }
  1627. }
  1628. $row++;
  1629. }
  1630. FW_pO "</table>";
  1631. FW_pO "</td></tr>";
  1632. }
  1633. FW_pO "</table></td>" if($maxc != -1); # Column
  1634. }
  1635. FW_pO "</tr>" if($maxc != -1);
  1636. FW_pO "</table><br>";
  1637. # Now the "atEnds"
  1638. foreach my $d (sort { $sortIndex{$a} cmp $sortIndex{$b} } @atEnds) {
  1639. no strict "refs";
  1640. $extPage{group} = "atEnd";
  1641. FW_pO &{$modules{$defs{$d}{TYPE}}{FW_summaryFn}}($FW_wname, $d,
  1642. $FW_room, \%extPage);
  1643. use strict "refs";
  1644. }
  1645. FW_pO "</div>";
  1646. FW_pO "</form>";
  1647. }
  1648. # Room1:col1group1,col1group2|col2group1,col2group2 Room2:...
  1649. sub
  1650. FW_parseColumns($)
  1651. {
  1652. my ($aGroup) = @_;
  1653. my %columns;
  1654. my $colNo = -1;
  1655. foreach my $roomgroup (split("[ \t\r\n]+", AttrVal($FW_wname,"column",""))) {
  1656. my ($room, $groupcolumn)=split(":",$roomgroup,2);
  1657. $room =~ s/%20/ /g; # Space
  1658. next if(!defined($groupcolumn) || $FW_room !~ m/^$room$/);
  1659. $colNo = 1;
  1660. my @grouplist = keys %$aGroup;
  1661. my %handled;
  1662. foreach my $groups (split(/\|/,$groupcolumn)) {
  1663. my $lineNo = 1;
  1664. foreach my $group (split(",",$groups)) {
  1665. $group =~ s/%20/ /g; # Forum #33612
  1666. $group = "^$group\$"; #71381
  1667. foreach my $g (grep /$group/ ,@grouplist) {
  1668. next if($handled{$g});
  1669. $handled{$g} = 1;
  1670. $columns{$g} = [$lineNo++, $colNo]; #23212
  1671. }
  1672. }
  1673. $colNo++;
  1674. }
  1675. last;
  1676. }
  1677. return (\%columns, $colNo);
  1678. }
  1679. #################
  1680. # return a sorted list of actual files for a given regexp
  1681. sub
  1682. FW_fileList($;$)
  1683. {
  1684. my ($fname,$mtime) = @_;
  1685. $fname =~ m,^(.*)/([^/]*)$,; # Split into dir and file
  1686. my ($dir,$re) = ($1, $2);
  1687. return $fname if(!$re);
  1688. $dir =~ s/%L/$attr{global}{logdir}/g # %L present and log directory defined
  1689. if($dir =~ m/%/ && $attr{global}{logdir});
  1690. $re =~ s/%./[A-Za-z0-9]*/g; # logfile magic (%Y, etc)
  1691. my @ret;
  1692. return @ret if(!opendir(DH, $dir));
  1693. while(my $f = readdir(DH)) {
  1694. next if($f !~ m,^$re$, || $f eq "99_Utils.pm");
  1695. push(@ret, $f);
  1696. }
  1697. closedir(DH);
  1698. return sort { (CORE::stat("$dir/$a"))[9] <=> (CORE::stat("$dir/$b"))[9] }
  1699. @ret if($mtime);
  1700. @ret = cfgDB_FW_fileList($dir,$re,@ret) if (configDBUsed());
  1701. return sort @ret;
  1702. }
  1703. ###################################
  1704. # Stream big files in chunks, to avoid bloating ourselves.
  1705. # This is a "terminal" function, no data can be appended after it is called.
  1706. sub
  1707. FW_outputChunk($$$)
  1708. {
  1709. my ($hash, $buf, $d) = @_;
  1710. $buf = $d->deflate($buf) if($d);
  1711. if( length($buf) ){
  1712. TcpServer_WriteBlocking($hash, sprintf("%x\r\n",length($buf)) .$buf."\r\n");
  1713. }
  1714. }
  1715. sub
  1716. FW_returnFileAsStream($$$$$)
  1717. {
  1718. my ($path, $suffix, $type, $doEsc, $cacheable) = @_;
  1719. my $etag;
  1720. if($cacheable) {
  1721. #Check for If-None-Match header (ETag)
  1722. my $if_none_match = $FW_httpheader{"If-None-Match"};
  1723. $if_none_match =~ s/"(.*)"/$1/ if($if_none_match);
  1724. $etag = (stat($path))[9]; #mtime
  1725. if(defined($etag) && defined($if_none_match) && $etag eq $if_none_match) {
  1726. my $now = time();
  1727. my $rsp = "Date: ".FmtDateTimeRFC1123($now)."\r\n".
  1728. "ETag: $etag\r\n".
  1729. "Expires: ".FmtDateTimeRFC1123($now+900)."\r\n";
  1730. Log3 $FW_wname, 4, "$FW_chash->{NAME} => 304 Not Modified";
  1731. TcpServer_WriteBlocking($FW_chash,"HTTP/1.1 304 Not Modified\r\n".
  1732. $rsp . $FW_headerlines . "\r\n");
  1733. return -1;
  1734. }
  1735. }
  1736. if(!open(FH, $path)) {
  1737. Log3 $FW_wname, 4, "FHEMWEB $FW_wname $path: $!";
  1738. TcpServer_WriteBlocking($FW_chash,
  1739. "HTTP/1.1 404 Not Found\r\n".
  1740. "Content-Length:0\r\n\r\n");
  1741. FW_closeConn($FW_chash);
  1742. return -1;
  1743. }
  1744. binmode(FH) if($type !~ m/text/); # necessary for Windows
  1745. my $sz = -s $path;
  1746. $etag = defined($etag) ? "ETag: \"$etag\"\r\n" : "";
  1747. my $expires = $cacheable ? ("Expires: ".gmtime(time()+900)." GMT\r\n"): "";
  1748. my $compr = ($FW_httpheader{"Accept-Encoding"} &&
  1749. $FW_httpheader{"Accept-Encoding"} =~ m/gzip/ && $FW_use_zlib) ?
  1750. "Content-Encoding: gzip\r\n" : "";
  1751. TcpServer_WriteBlocking($FW_chash, "HTTP/1.1 200 OK\r\n".
  1752. $compr . $expires . $FW_headerlines . $etag .
  1753. "Transfer-Encoding: chunked\r\n" .
  1754. "Content-Type: $type; charset=$FW_encoding\r\n\r\n");
  1755. my $d = Compress::Zlib::deflateInit(-WindowBits=>31) if($compr);
  1756. FW_outputChunk($FW_chash, $FW_RET, $d);
  1757. FW_outputChunk($FW_chash,
  1758. "<a href='#end_of_file'>jump to the end</a><br><br>", $d)
  1759. if($doEsc && $sz > 2048);
  1760. my $buf;
  1761. while(sysread(FH, $buf, 2048)) {
  1762. if($doEsc) { # FileLog special
  1763. $buf =~ s/</&lt;/g;
  1764. $buf =~ s/>/&gt;/g;
  1765. }
  1766. FW_outputChunk($FW_chash, $buf, $d);
  1767. }
  1768. close(FH);
  1769. FW_outputChunk($FW_chash, "<br/><a name='end_of_file'></a>".
  1770. "<a href='#top'>jump to the top</a><br/><br/>", $d)
  1771. if($doEsc && $sz > 2048);
  1772. FW_outputChunk($FW_chash, $suffix, $d);
  1773. if($compr) {
  1774. $buf = $d->flush();
  1775. if($buf){
  1776. TcpServer_WriteBlocking($FW_chash,
  1777. sprintf("%x\r\n",length($buf)) .$buf."\r\n");
  1778. }
  1779. }
  1780. TcpServer_WriteBlocking($FW_chash, "0\r\n\r\n");
  1781. FW_closeConn($FW_chash);
  1782. return -1;
  1783. }
  1784. ##################
  1785. sub
  1786. FW_fatal($)
  1787. {
  1788. my ($msg) = @_;
  1789. FW_pO "<html><body>$msg</body></html>";
  1790. }
  1791. ##################
  1792. sub
  1793. FW_hidden($$)
  1794. {
  1795. my ($n, $v) = @_;
  1796. return "<input type=\"hidden\" name=\"$n\" value=\"$v\"/>";
  1797. }
  1798. ##################
  1799. # Generate a select field with option list
  1800. sub
  1801. FW_select($$$$$@)
  1802. {
  1803. my ($id, $name, $valueArray, $selected, $class, $jSelFn) = @_;
  1804. $jSelFn = ($jSelFn ? "onchange=\"$jSelFn\"" : "");
  1805. $id =~ s/\./_/g if($id); # to avoid problems in JS DOM Search
  1806. $id = ($id ? "id=\"$id\" informId=\"$id\"" : "");
  1807. my $s = "<select $jSelFn $id name=\"$name\" class=\"$class\">";
  1808. foreach my $v (@{$valueArray}) {
  1809. if(defined($selected) && $v eq $selected) {
  1810. $s .= "<option selected=\"selected\" value='$v'>$v</option>\n";
  1811. } else {
  1812. $s .= "<option value='$v'>$v</option>\n";
  1813. }
  1814. }
  1815. $s .= "</select>";
  1816. return $s;
  1817. }
  1818. ##################
  1819. sub
  1820. FW_textfieldv($$$$)
  1821. {
  1822. my ($n, $z, $class, $value) = @_;
  1823. my $v;
  1824. $v=" value='$value'" if(defined($value));
  1825. return if($FW_hiddenroom{input});
  1826. my $s = "<input type='text' name='$n' class='$class' size='$z'$v ".
  1827. "autocorrect='off' autocapitalize='off'/>";
  1828. return $s;
  1829. }
  1830. sub
  1831. FW_textfield($$$)
  1832. {
  1833. return FW_textfieldv($_[0], $_[1], $_[2], "");
  1834. }
  1835. ##################
  1836. sub
  1837. FW_submit($$@)
  1838. {
  1839. my ($n, $v, $class) = @_;
  1840. $class = ($class ? "class=\"$class\"" : "");
  1841. my $s ="<input type=\"submit\" name=\"$n\" value=\"$v\" $class/>";
  1842. $s = FW_hidden("fwcsrf", $defs{$FW_wname}{CSRFTOKEN}).$s if($FW_CSRF);
  1843. return $s;
  1844. }
  1845. ##################
  1846. sub
  1847. FW_displayFileList($@)
  1848. {
  1849. my ($heading,@files)= @_;
  1850. my $hid = lc($heading);
  1851. $hid =~ s/[^A-Za-z]/_/g;
  1852. FW_pO "<div class=\"fileList $hid\">$heading</div>";
  1853. FW_pO "<table class=\"block fileList\">";
  1854. my $cfgDB = "";
  1855. my $row = 0;
  1856. foreach my $f (@files) {
  1857. $cfgDB = ($f =~ s,\.configDB$,,);
  1858. $cfgDB = ($cfgDB) ? "configDB" : "";
  1859. FW_pO "<tr class=\"" . ($row?"odd":"even") . "\">";
  1860. FW_pH "cmd=style edit $f $cfgDB", $f, 1;
  1861. FW_pO "</tr>";
  1862. $row = ($row+1)%2;
  1863. }
  1864. FW_pO "</table>";
  1865. FW_pO "<br>";
  1866. }
  1867. ##################
  1868. sub
  1869. FW_fileNameToPath($)
  1870. {
  1871. my $name = shift;
  1872. $attr{global}{configfile} =~ m,([^/]*)$,;
  1873. my $cfgFileName = $1;
  1874. if($name eq $cfgFileName) {
  1875. return $attr{global}{configfile};
  1876. } elsif($name =~ m/.*(js|css|_defs.svg)$/) {
  1877. return "$FW_cssdir/$name";
  1878. } elsif($name =~ m/.*(png|svg)$/) {
  1879. my $d="";
  1880. map { $d = $_ if(!$d && -d "$FW_icondir/$_") } @FW_iconDirs;
  1881. return "$FW_icondir/$d/$name";
  1882. } elsif($name =~ m/.*gplot$/) {
  1883. return "$FW_gplotdir/$name";
  1884. } else {
  1885. return "$MW_dir/$name";
  1886. }
  1887. }
  1888. ##################
  1889. # List/Edit/Save css and gnuplot files
  1890. sub
  1891. FW_style($$)
  1892. {
  1893. my ($cmd, $msg) = @_;
  1894. my @a = split(" ", $cmd);
  1895. return if(!Authorized($FW_chash, "cmd", $a[0]));
  1896. my $start = '><table><tr><td';
  1897. my $end = "</td></tr></table></div>";
  1898. if($a[1] eq "list") {
  1899. FW_addContent($start);
  1900. FW_pO "$msg<br><br>" if($msg);
  1901. $attr{global}{configfile} =~ m,([^/]*)$,;
  1902. my $cfgFileName = $1;
  1903. FW_displayFileList("config file", $cfgFileName)
  1904. if(!configDBUsed());
  1905. my $efl = AttrVal($FW_wname, 'editFileList',
  1906. "Own modules and helper files:\$MW_dir:^(.*sh|[0-9][0-9].*Util.*pm|".
  1907. ".*cfg|.*\.holiday|myUtilsTemplate.pm|.*layout)\$\n".
  1908. "Gplot files:\$FW_gplotdir:^.*gplot\$\n".
  1909. "Styles:\$FW_cssdir:^.*(css|svg)\$");
  1910. foreach my $l (split(/[\r\n]/, $efl)) {
  1911. my ($t, $v, $re) = split(":", $l, 3);
  1912. $v = eval $v;
  1913. my @fList;
  1914. if($v eq $FW_gplotdir && AttrVal($FW_wname,'showUsedFiles',0)) {
  1915. @fList = defInfo('TYPE=SVG','GPLOTFILE');
  1916. @fList = map { "$_.gplot" } @fList;
  1917. @fList = map { "$_.configDB" } @fList if configDBUsed();
  1918. } else {
  1919. @fList = FW_fileList("$v/$re");
  1920. }
  1921. FW_displayFileList($t, @fList);
  1922. }
  1923. FW_pO $end;
  1924. } elsif($a[1] eq "select") {
  1925. my @fl = grep { $_ !~ m/(floorplan|dashboard)/ }
  1926. FW_fileList("$FW_cssdir/.*style.css");
  1927. FW_addContent($start);
  1928. FW_pO "<table class=\"block fileList\">";
  1929. my $row = 0;
  1930. foreach my $file (@fl) {
  1931. next if($file =~ m/svg_/);
  1932. $file =~ s/style.css//;
  1933. $file = "default" if($file eq "");
  1934. FW_pO "<tr class=\"" . ($row?"odd":"even") . "\">";
  1935. FW_pH "cmd=style set $file", "$file", 1;
  1936. FW_pO "</tr>";
  1937. $row = ($row+1)%2;
  1938. }
  1939. FW_pO "</table>$end";
  1940. } elsif($a[1] eq "set") {
  1941. if($a[2] eq "default") {
  1942. CommandDeleteAttr(undef, "$FW_wname stylesheetPrefix");
  1943. } else {
  1944. CommandAttr(undef, "$FW_wname stylesheetPrefix $a[2]");
  1945. }
  1946. $FW_styleStamp = time();
  1947. $FW_RET =~ s,/style.css\?v=\d+,/style.css?v=$FW_styleStamp,;
  1948. FW_addContent($start);
  1949. FW_pO "Reload the page in the browser.$end";
  1950. } elsif($a[1] eq "edit") {
  1951. my $fileName = $a[2];
  1952. my $data = "";
  1953. my $cfgDB = defined($a[3]) ? $a[3] : "";
  1954. my $forceType = ($cfgDB eq 'configDB') ? $cfgDB : "file";
  1955. $fileName =~ s,.*/,,g; # Little bit of security
  1956. my $filePath = FW_fileNameToPath($fileName);
  1957. my($err, @content) = FileRead({FileName=>$filePath, ForceType=>$forceType});
  1958. if($err) {
  1959. FW_addContent(">$err</div");
  1960. return;
  1961. }
  1962. $data = join("\n", @content);
  1963. $data =~ s/&/&amp;/g;
  1964. $attr{global}{configfile} =~ m,([^/]*)$,;
  1965. my $readOnly = (AttrVal($FW_wname, "editConfig", ($1 ne $fileName)) ?
  1966. "" : "readonly");
  1967. my $ncols = $FW_ss ? 40 : 80;
  1968. FW_addContent();
  1969. FW_pO "<form method=\"$FW_formmethod\">";
  1970. if($readOnly) {
  1971. FW_pO "You can enable saving this file by setting the editConfig ";
  1972. FW_pO "attribute, but read the documentation first for the side effects.";
  1973. FW_pO "<br><br>";
  1974. } else {
  1975. FW_pO FW_submit("save", "Save $fileName");
  1976. FW_pO "&nbsp;&nbsp;";
  1977. FW_pO FW_submit("saveAs", "Save as");
  1978. FW_pO FW_textfieldv("saveName", 30, "saveName", $fileName);
  1979. FW_pO "<br><br>";
  1980. }
  1981. FW_pO FW_hidden("cmd", "style save $fileName $cfgDB");
  1982. FW_pO "<textarea $readOnly name=\"data\" cols=\"$ncols\" rows=\"30\">" .
  1983. "$data</textarea>";
  1984. FW_pO "</form>";
  1985. FW_pO "</div>";
  1986. } elsif($a[1] eq "save") {
  1987. my $fileName = $a[2];
  1988. my $cfgDB = defined($a[3]) ? $a[3] : "";
  1989. $fileName = $FW_webArgs{saveName}
  1990. if($FW_webArgs{saveAs} && $FW_webArgs{saveName});
  1991. $fileName =~ s,.*/,,g; # Little bit of security
  1992. my $filePath = FW_fileNameToPath($fileName);
  1993. my $isImg = ($fileName =~ m,\.(svg|png)$,i);
  1994. my $forceType = ($cfgDB eq 'configDB' && !$isImg) ? $cfgDB : "file";
  1995. $FW_data =~ s/\r//g if(!$isImg);
  1996. my $err;
  1997. if($fileName =~ m,\.png$,) {
  1998. $err = FileWrite({FileName=>$filePath,ForceType=>$forceType,NoNL=>1},
  1999. $FW_data);
  2000. } else {
  2001. $err = FileWrite({ FileName=>$filePath, ForceType=>$forceType },
  2002. split("\n", $FW_data));
  2003. }
  2004. if($err) {
  2005. FW_addContent(">$filePath: $!</div");
  2006. return;
  2007. }
  2008. my $ret = FW_fC("rereadcfg") if($filePath eq $attr{global}{configfile});
  2009. $ret = FW_fC("reload $fileName") if($fileName =~ m,\.pm$,);
  2010. $ret = FW_Set("","","rereadicons") if($isImg);
  2011. DoTrigger("global", "FILEWRITE $filePath", 1) if(!$ret); # Forum #32592
  2012. $ret = ($ret ? "<h3>ERROR:</h3><b>$ret</b>" :
  2013. "Saved the file $fileName to $forceType");
  2014. FW_style("style list", $ret);
  2015. $ret = "";
  2016. } elsif($a[1] eq "iconFor") {
  2017. FW_iconTable("iconFor", "icon", "style setIF $a[2] %s", undef);
  2018. } elsif($a[1] eq "setIF") {
  2019. FW_fC("attr $a[2] icon $a[3]");
  2020. FW_doDetail($a[2]);
  2021. } elsif($a[1] eq "showDSI") {
  2022. FW_iconTable("devStateIcon", "",
  2023. "style addDSI $a[2] %s", "Enter value/regexp for STATE");
  2024. } elsif($a[1] eq "addDSI") {
  2025. my $dsi = AttrVal($a[2], "devStateIcon", "");
  2026. $dsi .= " " if($dsi);
  2027. FW_fC("attr $a[2] devStateIcon $dsi$FW_data:$a[3]");
  2028. FW_doDetail($a[2]);
  2029. } elsif($a[1] eq "eventMonitor") {
  2030. FW_pO "<script type=\"text/javascript\" src=\"$FW_ME/pgm2/console.js\">".
  2031. "</script>";
  2032. FW_addContent();
  2033. my $filter = $a[2] ? ($a[2] eq "log" ? "global" : $a[2]) : ".*";
  2034. FW_pO "Events (Filter: <a href=\"#\" id=\"eventFilter\">$filter</a>) ".
  2035. "&nbsp;&nbsp;<span class='fhemlog'>FHEM log ".
  2036. "<input id='eventWithLog' type='checkbox'".
  2037. ($a[2] && $a[2] eq "log" ? " checked":"")."></span>".
  2038. "&nbsp;&nbsp;<button id='eventReset'>Reset</button><br><br>\n";
  2039. FW_pO "<div id=\"console\"></div>";
  2040. FW_pO "</div>";
  2041. }
  2042. }
  2043. sub
  2044. FW_iconTable($$$$)
  2045. {
  2046. my ($name, $class, $cmdFmt, $textfield) = @_;
  2047. my %icoList = ();
  2048. foreach my $style (@FW_iconDirs) {
  2049. foreach my $imgName (sort keys %{$FW_icons{$style}}) {
  2050. $imgName =~ s/\.[^.]*$//; # Cut extension
  2051. next if(!$FW_icons{$style}{$imgName}); # Dont cut it twice: FS20.on.png
  2052. next if($FW_icons{$style}{$imgName} !~ m/$imgName/); # Skip alias
  2053. next if($imgName=~m+^(weather/|shutter.*big|fhemicon|favicon|ws_.*_kl)+);
  2054. next if($imgName=~m+^(dashboardicons)+);
  2055. $icoList{$imgName} = 1;
  2056. }
  2057. }
  2058. FW_addContent();
  2059. FW_pO "<form method=\"$FW_formmethod\">";
  2060. FW_pO "Filter:&nbsp;".FW_textfieldv("icon-filter",20,"iconTable","")."<br>";
  2061. if($textfield) {
  2062. FW_pO "$textfield:&nbsp;".FW_textfieldv("data",20,"iconTable",".*")."<br>";
  2063. }
  2064. foreach my $i (sort keys %icoList) {
  2065. FW_pF "<button title='%s' type='submit' class='dist' name='cmd' ".
  2066. "value='$cmdFmt'>%s</button>", $i, $i, FW_makeImage($i,$i,$class);
  2067. }
  2068. FW_pO "</form>";
  2069. FW_pO "</div>";
  2070. }
  2071. ##################
  2072. # print (append) to output
  2073. sub
  2074. FW_pO(@)
  2075. {
  2076. my $arg = shift;
  2077. return if(!defined($arg));
  2078. $FW_RET .= $arg;
  2079. $FW_RET .= "\n";
  2080. }
  2081. #################
  2082. # add href
  2083. sub
  2084. FW_pH(@)
  2085. {
  2086. my ($link, $txt, $td, $class, $doRet,$nonl) = @_;
  2087. my $ret;
  2088. $link .= $FW_CSRF if($link =~ m/cmd/);
  2089. $link = ($link =~ m,^/,) ? $link : "$FW_ME$FW_subdir?$link";
  2090. # Using onclick, as href starts safari in a webapp.
  2091. # Known issue: the pointer won't change
  2092. if($FW_ss || $FW_tp) {
  2093. $ret = "<a onClick=\"location.href='$link'\">$txt</a>";
  2094. } else {
  2095. $ret = "<a href=\"$link\">$txt</a>";
  2096. }
  2097. #actually 'div' should be removed if no class is defined
  2098. # as I can't check all code for consistancy I add nonl instead
  2099. $class = ($class)?" class=\"$class\"":"";
  2100. $ret = "<div$class>$ret</div>" if (!$nonl);
  2101. $ret = "<td>$ret</td>" if($td);
  2102. return $ret if($doRet);
  2103. FW_pO $ret;
  2104. }
  2105. #################
  2106. # href without class/div, returned as a string
  2107. sub
  2108. FW_pHPlain(@)
  2109. {
  2110. my ($link, $txt, $td) = @_;
  2111. $link = "?$link" if($link !~ m+^/+);
  2112. my $ret = "";
  2113. $ret .= "<td>" if($td);
  2114. $link .= $FW_CSRF;
  2115. if($FW_ss || $FW_tp) {
  2116. $ret .= "<a onClick=\"location.href='$FW_ME$FW_subdir$link'\">$txt</a>";
  2117. } else {
  2118. $ret .= "<a href=\"$FW_ME$FW_subdir$link\">$txt</a>";
  2119. }
  2120. $ret .= "</td>" if($td);
  2121. return $ret;
  2122. }
  2123. ##############################
  2124. sub
  2125. FW_makeImage(@)
  2126. {
  2127. my ($name, $txt, $class)= @_;
  2128. $txt = $name if(!defined($txt));
  2129. $class = "" if(!$class);
  2130. $class = "$class $name";
  2131. $class =~ s/\./_/g;
  2132. $class =~ s/@/ /g;
  2133. my $p = FW_iconPath($name);
  2134. return $name if(!$p);
  2135. if($p =~ m/\.svg$/i) {
  2136. if(open(FH, "$FW_icondir/$p")) {
  2137. my $data;
  2138. do {
  2139. $data = <FH>;
  2140. if(!defined($data)) {
  2141. Log 1, "$FW_icondir/$p is not useable";
  2142. return "";
  2143. }
  2144. } until( $data =~ m/^<svg/ );
  2145. $data .= join("", <FH>);
  2146. close(FH);
  2147. $data =~ s/[\r\n]/ /g;
  2148. $data =~ s/ *$//g;
  2149. $data =~ s/<svg/<svg class="$class" data-txt="$txt"/; #52967
  2150. $name =~ m/(@.*)$/;
  2151. my $col = $1 if($1);
  2152. if($col) {
  2153. $col =~ s/@//;
  2154. $col = "#$col" if($col =~ m/^([A-F0-9]{6})$/);
  2155. $data =~ s/fill="#000000"/fill="$col"/g;
  2156. $data =~ s/fill:#000000/fill:$col/g;
  2157. $data =~ s/FHEM_COLOR/$col/g;
  2158. } else {
  2159. $data =~ s/fill="#000000"//g;
  2160. $data =~ s/fill:#000000//g;
  2161. $data =~ s/FHEM_COLOR//g;
  2162. }
  2163. return $data;
  2164. } else {
  2165. return $name;
  2166. }
  2167. } else {
  2168. $class = "class='$class'" if($class);
  2169. $p = urlEncodePath($p);
  2170. return "<img $class src=\"$FW_ME/images/$p\" alt=\"$txt\" title=\"$txt\">";
  2171. }
  2172. }
  2173. ####
  2174. sub
  2175. FW_IconURL($)
  2176. {
  2177. my ($name)= @_;
  2178. return "$FW_ME/icons/$name";
  2179. }
  2180. ##################
  2181. # print formatted
  2182. sub
  2183. FW_pF($@)
  2184. {
  2185. my $fmt = shift;
  2186. $FW_RET .= sprintf $fmt, @_;
  2187. }
  2188. ##################
  2189. # fhem command
  2190. sub
  2191. FW_fC($@)
  2192. {
  2193. my ($cmd, $unique) = @_;
  2194. my $ret;
  2195. if($unique) {
  2196. $ret = AnalyzeCommand($FW_chash, $cmd);
  2197. } else {
  2198. $ret = AnalyzeCommandChain($FW_chash, $cmd);
  2199. }
  2200. return $ret;
  2201. }
  2202. sub
  2203. FW_Attr(@)
  2204. {
  2205. my ($type, $devName, $attrName, @param) = @_;
  2206. my $hash = $defs{$devName};
  2207. my $sP = "stylesheetPrefix";
  2208. my $retMsg;
  2209. if($type eq "set" && $attrName eq "HTTPS") {
  2210. TcpServer_SetSSL($hash);
  2211. }
  2212. if($type eq "set") { # Converting styles
  2213. if($attrName eq "smallscreen" || $attrName eq "touchpad") {
  2214. $attr{$devName}{$sP} = $attrName;
  2215. $retMsg="$devName: attribute $attrName deprecated, converted to $sP";
  2216. $param[0] = $attrName; $attrName = $sP;
  2217. }
  2218. }
  2219. if($attrName eq $sP) {
  2220. # AttrFn is called too early, we have to set/del the attr here
  2221. if($type eq "set") {
  2222. $attr{$devName}{$sP} = (defined($param[0]) ? $param[0] : "default");
  2223. FW_readIcons($attr{$devName}{$sP});
  2224. } else {
  2225. delete $attr{$devName}{$sP};
  2226. }
  2227. }
  2228. if(($attrName eq "allowedCommands" ||
  2229. $attrName eq "basicAuth" ||
  2230. $attrName eq "basicAuthMsg")
  2231. && $type eq "set") {
  2232. my $aName = "allowed_$devName";
  2233. my $exists = ($defs{$aName} ? 1 : 0);
  2234. AnalyzeCommand(undef, "defmod $aName allowed");
  2235. AnalyzeCommand(undef, "attr $aName validFor $devName");
  2236. AnalyzeCommand(undef, "attr $aName $attrName ".join(" ",@param));
  2237. return "$devName: ".($exists ? "modifying":"creating").
  2238. " device $aName for attribute $attrName";
  2239. }
  2240. if($attrName eq "iconPath" && $type eq "set") {
  2241. foreach my $pe (split(":", $param[0])) {
  2242. $pe =~ s+\.\.++g;
  2243. FW_readIcons($pe);
  2244. }
  2245. }
  2246. if($attrName eq "JavaScripts" && $type eq "set") { # create some attributes
  2247. my (%a, @add);
  2248. map { $a{$_} = 1 } split(" ", $modules{FHEMWEB}{AttrList});
  2249. map {
  2250. $_ =~ s+.*/++; $_ =~ s/.js$//; $_ =~ s/fhem_//; $_ .= "Param";
  2251. push @add, $_ if(!$a{$_} && $_ !~ m/^-/);
  2252. } split(" ", $param[0]);
  2253. $modules{FHEMWEB}{AttrList} .= " ".join(" ",@add) if(@add);
  2254. }
  2255. if($attrName eq "csrfToken") {
  2256. return undef if($FW_csrfTokenCache{$devName} && !$init_done);
  2257. my $csrf = $param[0];
  2258. if($type eq "del" || $csrf eq "random") {
  2259. my ($x,$y) = gettimeofday();
  2260. ($csrf = "csrf_".(rand($y)*rand($x))) =~ s/[^a-z_0-9]//g;
  2261. }
  2262. if($csrf eq "none") {
  2263. delete($hash->{CSRFTOKEN});
  2264. delete($FW_csrfTokenCache{$devName});
  2265. } else {
  2266. $hash->{CSRFTOKEN} = $csrf;
  2267. $FW_csrfTokenCache{$devName} = $hash->{CSRFTOKEN};
  2268. }
  2269. }
  2270. if($attrName eq "longpoll" && $type eq "set" && $param[0] eq "websocket") {
  2271. eval { require Digest::SHA; };
  2272. if($@) {
  2273. Log3 $FW_wname, 1, $@;
  2274. return "$devName: Can't load Digest::SHA, no websocket";
  2275. return -1;
  2276. }
  2277. $FW_use_sha = 1;
  2278. }
  2279. return $retMsg;
  2280. }
  2281. # recursion starts at $FW_icondir/$dir
  2282. # filenames are relative to $FW_icondir
  2283. sub
  2284. FW_readIconsFrom($$)
  2285. {
  2286. my ($dir,$subdir)= @_;
  2287. my $ldir = ($subdir ? "$dir/$subdir" : $dir);
  2288. my @entries;
  2289. if(opendir(DH, "$FW_icondir/$ldir")) {
  2290. @entries= sort readdir(DH); # assures order: .gif .ico .jpg .png .svg
  2291. closedir(DH);
  2292. }
  2293. foreach my $entry (@entries) {
  2294. if( -d "$FW_icondir/$ldir/$entry" ) { # directory -> recurse
  2295. FW_readIconsFrom($dir, $subdir ? "$subdir/$entry" : $entry)
  2296. unless($entry eq "." || $entry eq ".." || $entry eq ".svn");
  2297. } else {
  2298. if($entry =~ m/^iconalias.txt$/i && open(FH, "$FW_icondir/$ldir/$entry")){
  2299. while(my $l = <FH>) {
  2300. chomp($l);
  2301. my @a = split(" ", $l);
  2302. next if($l =~ m/^#/ || @a < 2);
  2303. $FW_icons{$dir}{$a[0]} = $a[1];
  2304. }
  2305. close(FH);
  2306. } elsif($entry =~ m/(gif|ico|jpg|png|jpeg|svg)$/i) {
  2307. my $filename = $subdir ? "$subdir/$entry" : $entry;
  2308. $FW_icons{$dir}{$filename} = $filename;
  2309. my $tag = $filename; # Add it without extension too
  2310. $tag =~ s/\.[^.]*$//;
  2311. $FW_icons{$dir}{$tag} = $filename;
  2312. }
  2313. }
  2314. }
  2315. $FW_icons{$dir}{""} = 1; # Do not check empty directories again.
  2316. }
  2317. sub
  2318. FW_readIcons($)
  2319. {
  2320. my ($dir)= @_;
  2321. return if($FW_icons{$dir});
  2322. FW_readIconsFrom($dir, "");
  2323. }
  2324. # check if the icon exists, and if yes, returns its "logical" name;
  2325. sub
  2326. FW_iconName($)
  2327. {
  2328. my ($oname)= @_;
  2329. return undef if(!defined($oname));
  2330. my $name = $oname;
  2331. $name =~ s/@.*//;
  2332. foreach my $pe (@FW_iconDirs) {
  2333. return $oname if($pe && $FW_icons{$pe} && $FW_icons{$pe}{$name});
  2334. }
  2335. return undef;
  2336. }
  2337. # returns the physical absolute path relative for the logical path
  2338. # examples:
  2339. # FS20.on -> dark/FS20.on.png
  2340. # weather/sunny -> default/weather/sunny.gif
  2341. sub
  2342. FW_iconPath($)
  2343. {
  2344. my ($name) = @_;
  2345. $name =~ s/@.*//;
  2346. foreach my $pe (@FW_iconDirs) {
  2347. return "$pe/$FW_icons{$pe}{$name}"
  2348. if($pe && $FW_icons{$pe} && $FW_icons{$pe}{$name});
  2349. }
  2350. return undef;
  2351. }
  2352. sub
  2353. FW_dev2image($;$)
  2354. {
  2355. my ($name, $state) = @_;
  2356. my $d = $defs{$name};
  2357. return "" if(!$name || !$d);
  2358. my $type = $d->{TYPE};
  2359. $state = $d->{STATE} if(!defined($state));
  2360. return "" if(!$type || !defined($state));
  2361. my $model = AttrVal($name, "model", "");
  2362. my (undef, $rstate) = ReplaceEventMap($name, [undef, $state], 0);
  2363. my ($icon, $rlink);
  2364. my $devStateIcon = AttrVal($name, "devStateIcon", undef);
  2365. if(defined($devStateIcon) && $devStateIcon =~ m/^{.*}$/) {
  2366. my ($html, $link) = eval $devStateIcon;
  2367. Log3 $FW_wname, 1, "devStateIcon $name: $@" if($@);
  2368. return ($html, $link, 1) if(defined($html) && $html =~ m/^<.*>$/s);
  2369. $devStateIcon = $html;
  2370. }
  2371. if(defined($devStateIcon)) {
  2372. my @list = split(" ", $devStateIcon);
  2373. foreach my $l (@list) {
  2374. my ($re, $iconName, $link) = split(":", $l, 3);
  2375. if(defined($re) && $state =~ m/^$re$/) {
  2376. if(defined($iconName) && $iconName eq "") {
  2377. $rlink = $link;
  2378. last;
  2379. }
  2380. if(defined($iconName) && defined(FW_iconName($iconName))) {
  2381. return ($iconName, $link, 0);
  2382. } else {
  2383. return ($state, $link, 1);
  2384. }
  2385. }
  2386. }
  2387. }
  2388. $state =~ s/ .*//; # Want to be able to have icons for "on-for-timer xxx"
  2389. $icon = FW_iconName("$name.$state") if(!$icon); # lamp.Aus.png
  2390. $icon = FW_iconName("$name.$rstate") if(!$icon); # lamp.on.png
  2391. $icon = FW_iconName($name) if(!$icon); # lamp.png
  2392. $icon = FW_iconName("$model.$state") if(!$icon && $model); # fs20st.off.png
  2393. $icon = FW_iconName($model) if(!$icon && $model); # fs20st.png
  2394. $icon = FW_iconName("$type.$state") if(!$icon); # FS20.Aus.png
  2395. $icon = FW_iconName("$type.$rstate") if(!$icon); # FS20.on.png
  2396. $icon = FW_iconName($type) if(!$icon); # FS20.png
  2397. $icon = FW_iconName($state) if(!$icon); # Aus.png
  2398. $icon = FW_iconName($rstate) if(!$icon); # on.png
  2399. return ($icon, $rlink, 0);
  2400. }
  2401. sub
  2402. FW_makeEdit($$$)
  2403. {
  2404. my ($name, $n, $val) = @_;
  2405. # Toggle Edit-Window visibility script.
  2406. my $psc = AttrVal("global", "perlSyntaxCheck", ($featurelevel>5.7) ? 1 : 0);
  2407. FW_pO "<td>";
  2408. FW_pO "<a id=\"DEFa\" style=\"cursor:pointer\">$n</a>";
  2409. FW_pO "</td>";
  2410. $val =~ s,\\\n,\n,g;
  2411. $val = FW_htmlEscape($val);
  2412. my $eval = $val;
  2413. $eval = "<pre>$eval</pre>" if($eval =~ m/\n/);
  2414. FW_pO "<td>";
  2415. FW_pO "<div class=\"dval\" id=\"disp\">$eval</div>";
  2416. FW_pO "</td>";
  2417. FW_pO "</tr><tr><td colspan=\"2\">";
  2418. FW_pO "<div id=\"edit\" style=\"display:none\">";
  2419. FW_pO "<form method=\"$FW_formmethod\">";
  2420. FW_pO FW_hidden("detail", $name);
  2421. my $cmd = "modify";
  2422. my $ncols = $FW_ss ? 30 : 60;
  2423. FW_pO "<textarea name=\"val.${cmd}$name\" ".
  2424. "cols=\"$ncols\" rows=\"10\">$val</textarea>";
  2425. FW_pO "<br>" . FW_submit("cmd.${cmd}$name", "$cmd $name",($psc?"psc":""));
  2426. FW_pO "</form></div>";
  2427. FW_pO "</td>";
  2428. }
  2429. sub
  2430. FW_longpollInfo($@)
  2431. {
  2432. my $fmt = shift;
  2433. if($fmt && $fmt eq "JSON") {
  2434. my @a;
  2435. map { my $x = $_; #Forum 57377, ASCII 0-19 \ "
  2436. $x=~ s/([\x00-\x1f\x22\x5c\x7f])/sprintf '\u%04x', ord($1)/ge;
  2437. push @a,$x; } @_;
  2438. return '["'.join('","', @a).'"]';
  2439. } else {
  2440. return join('<<', @_);
  2441. }
  2442. }
  2443. sub
  2444. FW_roomStatesForInform($$)
  2445. {
  2446. my ($me, $sinceTimestamp ) = @_;
  2447. return "" if($me->{inform}{type} !~ m/status/);
  2448. my %extPage = ();
  2449. my @data;
  2450. foreach my $dn (keys %{$me->{inform}{devices}}) {
  2451. next if(!defined($defs{$dn}));
  2452. my $t = $defs{$dn}{TYPE};
  2453. next if(!$t || $modules{$t}{FW_atPageEnd});
  2454. my $lastChanged = OldTimestamp( $dn );
  2455. next if(!defined($lastChanged) || $lastChanged lt $sinceTimestamp);
  2456. my ($allSet, $cmdlist, $txt) = FW_devState($dn, "", \%extPage);
  2457. if($defs{$dn} && $defs{$dn}{STATE} && $defs{$dn}{TYPE} ne "weblink") {
  2458. push @data,
  2459. FW_longpollInfo($me->{inform}{fmt}, $dn, $defs{$dn}{STATE}, $txt);
  2460. }
  2461. }
  2462. my $data = join("\n", map { s/\n/ /gm; $_ } @data)."\n";
  2463. return $data;
  2464. }
  2465. sub
  2466. FW_logInform($$)
  2467. {
  2468. my ($me, $msg) = @_; # _NO_ Log3 here!
  2469. my $ntfy = $defs{$me};
  2470. if(!$ntfy) {
  2471. delete $logInform{$me};
  2472. return;
  2473. }
  2474. $msg = FW_htmlEscape($msg);
  2475. if(!FW_addToWritebuffer($ntfy, "<div class='fhemlog'>$msg</div>") ){
  2476. TcpServer_Close($ntfy, 1);
  2477. delete $logInform{$me};
  2478. }
  2479. }
  2480. sub
  2481. FW_Notify($$)
  2482. {
  2483. my ($ntfy, $dev) = @_;
  2484. my $h = $ntfy->{inform};
  2485. return undef if(!$h);
  2486. my $isStatus = ($h->{type} =~ m/status/);
  2487. my $events;
  2488. my $dn = $dev->{NAME};
  2489. if($dn eq "global" && $isStatus) {
  2490. my $vs = int(@structChangeHist) ? 'visible' : 'hidden';
  2491. my $data = FW_longpollInfo($h->{fmt},
  2492. "#FHEMWEB:$ntfy->{NAME}","\$('#saveCheck').css('visibility','$vs')","");
  2493. FW_addToWritebuffer($ntfy, $data."\n");
  2494. if($dev->{CHANGED}) {
  2495. $dn = $1 if($dev->{CHANGED}->[0] =~ m/^MODIFIED (.*)$/);
  2496. if($dev->{CHANGED}->[0] =~ m/^ATTR ([^ ]+) ([^ ]+) (.*)$/s) {
  2497. $dn = $1;
  2498. my @a = ("$2: $3");
  2499. $events = \@a;
  2500. }
  2501. }
  2502. }
  2503. if($dn eq $ntfy->{SNAME} &&
  2504. $dev->{CHANGED} &&
  2505. $dev->{CHANGED}->[0] =~ m/^JS(#([^:]*))?:(.*)$/) {
  2506. my $data = $3;
  2507. return if( $2 && $ntfy->{PEER} !~ m/$2/ );
  2508. $data = FW_longpollInfo($h->{fmt}, "#FHEMWEB:$ntfy->{NAME}",$data,"");
  2509. FW_addToWritebuffer($ntfy, $data."\n");
  2510. return;
  2511. }
  2512. return undef if($isStatus && !$h->{devices}{$dn});
  2513. my @data;
  2514. my %extPage;
  2515. my $isRaw = ($h->{type} =~ m/raw/);
  2516. $events = deviceEvents($dev, AttrVal($FW_wname, "addStateEvent",!$isRaw))
  2517. if(!$events);
  2518. if($isStatus) {
  2519. # Why is saving this stuff needed? FLOORPLAN?
  2520. my @old = ($FW_wname, $FW_ME, $FW_ss, $FW_tp, $FW_subdir);
  2521. $FW_wname = $ntfy->{SNAME};
  2522. $FW_ME = "/" . AttrVal($FW_wname, "webname", "fhem");
  2523. $FW_subdir = ($h->{iconPath} ? "/floorplan/$h->{iconPath}" : ""); # 47864
  2524. $FW_sp = AttrVal($FW_wname, "stylesheetPrefix", 0);
  2525. $FW_ss = ($FW_sp =~ m/smallscreen/);
  2526. $FW_tp = ($FW_sp =~ m/smallscreen|touchpad/);
  2527. @FW_iconDirs = grep { $_ } split(":", AttrVal($FW_wname, "iconPath",
  2528. "$FW_sp:default:fhemSVG:openautomation"));
  2529. if($h->{iconPath}) {
  2530. unshift @FW_iconDirs, $h->{iconPath};
  2531. FW_readIcons($h->{iconPath});
  2532. }
  2533. if( !$modules{$defs{$dn}{TYPE}}{FW_atPageEnd} ) {
  2534. my ($allSet, $cmdlist, $txt) = FW_devState($dn, "", \%extPage);
  2535. ($FW_wname, $FW_ME, $FW_ss, $FW_tp, $FW_subdir) = @old;
  2536. push @data, FW_longpollInfo($h->{fmt}, $dn, $dev->{STATE}, $txt);
  2537. }
  2538. #Add READINGS
  2539. if($events) { # It gets deleted sometimes (?)
  2540. my $tn = TimeNow();
  2541. my $max = int(@{$events});
  2542. for(my $i = 0; $i < $max; $i++) {
  2543. if($events->[$i] !~ /: /) {
  2544. if($dev->{NAME} eq 'global') { # Forum #47634
  2545. my($type,$args) = split(' ', $events->[$i], 2);
  2546. $args = "" if(!defined($args)); # global SAVE
  2547. push @data, FW_longpollInfo($h->{fmt}, "$dn-$type", $args, $args);
  2548. }
  2549. next; #ignore 'set' commands
  2550. }
  2551. my ($readingName,$readingVal) = split(": ",$events->[$i],2);
  2552. next if($readingName !~ m/^[A-Za-z\d_\.\-\/:]+$/); # Forum #70608,70844
  2553. push @data, FW_longpollInfo($h->{fmt},
  2554. "$dn-$readingName", $readingVal,$readingVal);
  2555. push @data, FW_longpollInfo($h->{fmt}, "$dn-$readingName-ts", $tn, $tn);
  2556. }
  2557. }
  2558. }
  2559. if($isRaw) {
  2560. if($events) { # It gets deleted sometimes (?)
  2561. my $tn = TimeNow();
  2562. if($attr{global}{mseclog}) {
  2563. my ($seconds, $microseconds) = gettimeofday();
  2564. $tn .= sprintf(".%03d", $microseconds/1000);
  2565. }
  2566. my $max = int(@{$events});
  2567. my $dt = $dev->{TYPE};
  2568. for(my $i = 0; $i < $max; $i++) {
  2569. my $line = "$tn $dt $dn ".$events->[$i]."<br>";
  2570. eval {
  2571. my $ok;
  2572. if($h->{filterType} && $h->{filterType} eq "notify") {
  2573. $ok = ($dn =~ m/^$h->{filter}$/ ||
  2574. "$dn:$events->[$i]" =~ m/^$h->{filter}$/) ;
  2575. } else {
  2576. $ok = ($line =~ m/$h->{filter}/) ;
  2577. }
  2578. push @data,$line if($ok);
  2579. }
  2580. }
  2581. }
  2582. }
  2583. if(@data){
  2584. if(!FW_addToWritebuffer($ntfy,
  2585. join("\n", map { s/\n/ /gm; $_ } @data)."\n") ){
  2586. my $name = $ntfy->{NAME};
  2587. Log3 $name, 4, "Closing connection $name due to full buffer in FW_Notify";
  2588. TcpServer_Close($ntfy, 1);
  2589. }
  2590. }
  2591. return undef;
  2592. }
  2593. sub
  2594. FW_directNotify($@) # Notify without the event overhead (Forum #31293)
  2595. {
  2596. my $filter;
  2597. if($_[0] =~ m/^FILTER=(.*)/) {
  2598. $filter = "^$1\$";
  2599. shift;
  2600. }
  2601. my $dev = $_[0];
  2602. foreach my $ntfy (values(%defs)) {
  2603. next if(!$ntfy->{TYPE} ||
  2604. $ntfy->{TYPE} ne "FHEMWEB" ||
  2605. !$ntfy->{inform} ||
  2606. !$ntfy->{inform}{devices}{$dev} ||
  2607. $ntfy->{inform}{type} ne "status");
  2608. next if($filter && $ntfy->{inform}{filter} !~ m/$filter/);
  2609. if(!FW_addToWritebuffer($ntfy,
  2610. FW_longpollInfo($ntfy->{inform}{fmt}, @_)."\n")) {
  2611. my $name = $ntfy->{NAME};
  2612. Log3 $name, 4, "Closing connection $name due to full buffer in FW_Notify";
  2613. TcpServer_Close($ntfy, 1);
  2614. }
  2615. }
  2616. }
  2617. ###################
  2618. # Compute the state (==second) column
  2619. sub
  2620. FW_devState($$@)
  2621. {
  2622. my ($d, $rf, $extPage) = @_;
  2623. my ($hasOnOff, $link);
  2624. my $cmdList = AttrVal($d, "webCmd", "");
  2625. my $allSets = FW_widgetOverride($d, getAllSets($d));
  2626. my $state = $defs{$d}{STATE};
  2627. $state = "" if(!defined($state));
  2628. $hasOnOff = ($allSets =~ m/(^| )on(:[^ ]*)?( |$)/ &&
  2629. $allSets =~ m/(^| )off(:[^ ]*)?( |$)/);
  2630. my $txt = $state;
  2631. my $dsi = ($attr{$d} && ($attr{$d}{stateFormat} || $attr{$d}{devStateIcon}));
  2632. if(AttrVal($d, "showtime", undef)) {
  2633. my $v = $defs{$d}{READINGS}{state}{TIME};
  2634. $txt = $v if(defined($v));
  2635. } elsif(!$dsi && $allSets =~ m/\bdesired-temp:/) {
  2636. $txt = "$1 &deg;C" if($txt =~ m/^measured-temp: (.*)/); # FHT fix
  2637. $cmdList = "desired-temp" if(!$cmdList);
  2638. } elsif(!$dsi && $allSets =~ m/\bdesiredTemperature:/) {
  2639. $txt = ReadingsVal($d, "temperature", ""); # ignores stateFormat!!!
  2640. $txt =~ s/ .*//;
  2641. $txt .= "&deg;C";
  2642. $cmdList = "desiredTemperature" if(!$cmdList);
  2643. } else {
  2644. my ($icon, $isHtml);
  2645. ($icon, $link, $isHtml) = FW_dev2image($d);
  2646. $txt = ($isHtml ? $icon : FW_makeImage($icon, $state)) if($icon);
  2647. my $cmdlist = (defined($link) ? $link : "");
  2648. my $h = "";
  2649. foreach my $cmd (split(":", $cmdlist)) {
  2650. my $htmlTxt;
  2651. my @c = split(' ', $cmd); # @c==0 if $cmd==" ";
  2652. if(int(@c) && $allSets && $allSets =~ m/\b$c[0]:([^ ]*)/) {
  2653. my $values = $1;
  2654. foreach my $fn (sort keys %{$data{webCmdFn}}) {
  2655. no strict "refs";
  2656. $htmlTxt = &{$data{webCmdFn}{$fn}}($FW_wname,
  2657. $d, $FW_room, $cmd, $values);
  2658. use strict "refs";
  2659. last if(defined($htmlTxt));
  2660. }
  2661. }
  2662. if( $htmlTxt ) {
  2663. $h .= "<p>$htmlTxt</p>";
  2664. }
  2665. }
  2666. if( $h ) {
  2667. $link = undef;
  2668. $h =~ s/'/\\"/g;
  2669. $txt = "<a onClick='FW_okDialog(\"$h\",this)'\>$txt</a>";
  2670. } else {
  2671. $link = "cmd.$d=set $d $link" if(defined($link));
  2672. }
  2673. }
  2674. if($hasOnOff) {
  2675. # Have to cover: "on:An off:Aus", "A0:Aus AI:An Aus:off An:on"
  2676. my $on = ReplaceEventMap($d, "on", 1);
  2677. my $off = ReplaceEventMap($d, "off", 1);
  2678. $link = "cmd.$d=set $d " . ($state eq $on ? $off : $on) if(!defined($link));
  2679. $cmdList = "$on:$off" if(!$cmdList);
  2680. }
  2681. if(defined($link)) { # Have command to execute
  2682. my $room = AttrVal($d, "room", undef);
  2683. if($room) {
  2684. if($FW_room && $room =~ m/\b$FW_room\b/) {
  2685. $room = $FW_room;
  2686. } else {
  2687. $room =~ s/,.*//;
  2688. }
  2689. $link .= "&room=".urlEncode($room);
  2690. }
  2691. $txt = "<a href=\"$FW_ME$FW_subdir?$link$rf$FW_CSRF\">$txt</a>"
  2692. if($link !~ m/ noFhemwebLink\b/);
  2693. }
  2694. my $style = AttrVal($d, "devStateStyle", "");
  2695. $state =~ s/"//g;
  2696. $txt = "<div id=\"$d\" $style title=\"$state\" class=\"col2\">$txt</div>";
  2697. my $type = $defs{$d}{TYPE};
  2698. my $sfn = $modules{$type}{FW_summaryFn};
  2699. if($sfn) {
  2700. if(!defined($extPage)) {
  2701. my %hash;
  2702. $extPage = \%hash;
  2703. }
  2704. no strict "refs";
  2705. my $newtxt = &{$sfn}($FW_wname, $d, $FW_room, $extPage);
  2706. use strict "refs";
  2707. $txt = $newtxt if(defined($newtxt)); # As specified
  2708. }
  2709. return ($allSets, $cmdList, $txt);
  2710. }
  2711. sub
  2712. FW_Get($@)
  2713. {
  2714. my ($hash, @a) = @_;
  2715. my $arg = (defined($a[1]) ? $a[1] : "");
  2716. if($arg eq "icon") {
  2717. return "need one icon as argument" if(int(@a) != 3);
  2718. my $ofn = $FW_wname;
  2719. $FW_wname = $hash->{NAME};
  2720. my $icon = FW_iconPath($a[2]);
  2721. $FW_wname = $ofn;
  2722. return defined($icon) ? "$FW_icondir/$icon" : "no such icon";
  2723. } elsif($arg eq "pathlist") {
  2724. return "web server root: $FW_dir\n".
  2725. "icon directory: $FW_icondir\n".
  2726. "css directory: $FW_cssdir\n".
  2727. "gplot directory: $FW_gplotdir";
  2728. } else {
  2729. return "Unknown argument $arg choose one of icon pathlist:noArg";
  2730. }
  2731. }
  2732. #####################################
  2733. sub
  2734. FW_Set($@)
  2735. {
  2736. my ($hash, @a) = @_;
  2737. my %cmd = ("rereadicons" => 1, "clearSvgCache" => 1);
  2738. return "no set value specified" if(@a < 2);
  2739. return ("Unknown argument $a[1], choose one of ".
  2740. join(" ", map { "$_:noArg" } sort keys %cmd))
  2741. if(!$cmd{$a[1]});
  2742. if($a[1] eq "rereadicons") {
  2743. my @dirs = keys %FW_icons;
  2744. %FW_icons = ();
  2745. foreach my $d (@dirs) {
  2746. FW_readIcons($d);
  2747. }
  2748. }
  2749. if($a[1] eq "clearSvgCache") {
  2750. my $cDir = "$FW_dir/SVGcache";
  2751. if(opendir(DH, $cDir)) {
  2752. map { my $n="$cDir/$_"; unlink($n) if(-f $n); } readdir(DH);
  2753. closedir(DH);
  2754. } else {
  2755. return "Can't open $cDir: $!";
  2756. }
  2757. }
  2758. return undef;
  2759. }
  2760. #####################################
  2761. sub
  2762. FW_closeInactiveClients()
  2763. {
  2764. my $now = time();
  2765. foreach my $dev (keys %defs) {
  2766. next if(!$defs{$dev}{TYPE} || $defs{$dev}{TYPE} ne "FHEMWEB" ||
  2767. !$defs{$dev}{LASTACCESS} || $defs{$dev}{inform} ||
  2768. ($now - $defs{$dev}{LASTACCESS}) < 60);
  2769. Log3 $FW_wname, 4, "Closing inactive connection $dev";
  2770. FW_Undef($defs{$dev}, "");
  2771. delete $defs{$dev};
  2772. delete $attr{$dev};
  2773. }
  2774. InternalTimer($now+60, "FW_closeInactiveClients", 0, 0);
  2775. }
  2776. sub
  2777. FW_htmlEscape($)
  2778. {
  2779. my ($txt) = @_;
  2780. $txt =~ s/&/&amp;/g;
  2781. $txt =~ s/</&lt;/g;
  2782. $txt =~ s/>/&gt;/g;
  2783. # $txt =~ s/\n/<br>/g;
  2784. return $txt;
  2785. }
  2786. ###########################
  2787. # Widgets START
  2788. sub
  2789. FW_widgetFallbackFn()
  2790. {
  2791. my ($FW_wname, $d, $FW_room, $cmd, $values) = @_;
  2792. # webCmd "temp 30" should remain text
  2793. # noArg is needed for fhem.cfg.demo / Cinema
  2794. return "" if(!$values || $values eq "noArg");
  2795. my($reading) = split( ' ', $cmd, 2 );
  2796. my $current;
  2797. if($cmd eq "desired-temp" || $cmd eq "desiredTemperature") {
  2798. $current = ReadingsVal($d, $cmd, 20);
  2799. $current =~ s/ .*//; # Cut off Celsius
  2800. $current = sprintf("%2.1f", int(2*$current)/2) if($current =~ m/[0-9.-]/);
  2801. } else {
  2802. $current = ReadingsVal($d, $reading, undef);
  2803. if( !defined($current) ) {
  2804. $reading = 'state';
  2805. $current = Value($d);
  2806. }
  2807. $current =~ s/$cmd //;
  2808. $current = ReplaceEventMap($d, $current, 1);
  2809. }
  2810. return "<td><div class='fhemWidget' cmd='$cmd' reading='$reading' ".
  2811. "dev='$d' arg='$values' current='$current'></div></td>";
  2812. }
  2813. # Widgets END
  2814. ###########################
  2815. sub
  2816. FW_visibleDevices(;$)
  2817. {
  2818. my($FW_wname) = @_;
  2819. my %devices = ();
  2820. foreach my $d (sort keys %defs) {
  2821. next if(!defined($defs{$d}));
  2822. my $h = $defs{$d};
  2823. next if(!$h->{TEMPORARY});
  2824. next if($h->{TYPE} ne "FHEMWEB");
  2825. next if(defined($FW_wname) && $h->{SNAME} ne $FW_wname);
  2826. next if(!defined($h->{inform}));
  2827. @devices{ keys %{$h->{inform}->{devices}} } =
  2828. values %{$h->{inform}->{devices}};
  2829. }
  2830. return %devices;
  2831. }
  2832. sub
  2833. FW_ActivateInform($;$)
  2834. {
  2835. my ($cl, $arg) = @_;
  2836. $FW_activateInform = ($arg ? $arg : 1);
  2837. }
  2838. sub
  2839. FW_widgetOverride($$)
  2840. {
  2841. my ($d, $str) = @_;
  2842. return $str if(!$str);
  2843. my $da = AttrVal($d, "widgetOverride", "");
  2844. my $fa = AttrVal($FW_wname, "widgetOverride", "");
  2845. return $str if(!$da && !$fa);
  2846. my @list;
  2847. push @list, split(" ", $fa) if($fa);
  2848. push @list, split(" ", $da) if($da);
  2849. foreach my $na (@list) {
  2850. my ($n,$a) = split(":", $na, 2);
  2851. $str =~ s/\b($n)\b(:[^ ]*)?/$1:$a/g;
  2852. }
  2853. return $str;
  2854. }
  2855. 1;
  2856. =pod
  2857. =item helper
  2858. =item summary HTTP Server and FHEM Frontend
  2859. =item summary_DE HTTP Server und FHEM Frontend
  2860. =begin html
  2861. <a name="FHEMWEB"></a>
  2862. <h3>FHEMWEB</h3>
  2863. <ul>
  2864. FHEMWEB is the builtin web-frontend, it also implements a simple web
  2865. server (optionally with Basic-Auth and HTTPS).
  2866. <br> <br>
  2867. <a name="FHEMWEBdefine"></a>
  2868. <b>Define</b>
  2869. <ul>
  2870. <code>define &lt;name&gt; FHEMWEB &lt;tcp-portnr&gt; [global]</code>
  2871. <br><br>
  2872. Enable the webfrontend on port &lt;tcp-portnr&gt;. If global is specified,
  2873. then requests from all interfaces (not only localhost / 127.0.0.1) are
  2874. serviced.<br>
  2875. To enable listening on IPV6 see the comments <a href="#telnet">here</a>.
  2876. <br>
  2877. </ul>
  2878. <br>
  2879. <a name="FHEMWEBset"></a>
  2880. <b>Set</b>
  2881. <ul>
  2882. <li>rereadicons<br>
  2883. reads the names of the icons from the icon path. Use after adding or
  2884. deleting icons.
  2885. </li>
  2886. <li>clearSvgCache<br>
  2887. delete all files found in the www/SVGcache directory, which is used to
  2888. cache SVG data, if the SVGcache attribute is set.
  2889. </li>
  2890. </ul>
  2891. <br>
  2892. <a name="FHEMWEBget"></a>
  2893. <b>Get</b>
  2894. <ul>
  2895. <li>icon &lt;logical icon&gt;<br>
  2896. returns the absolute path to the logical icon. Example:
  2897. <ul>
  2898. <code>get myFHEMWEB icon FS20.on<br>
  2899. /data/Homeautomation/fhem/FHEM/FS20.on.png
  2900. </code>
  2901. </ul>
  2902. </li>
  2903. <li>pathlist<br>
  2904. return FHEMWEB specific directories, where files for given types are
  2905. located
  2906. <br><br>
  2907. </ul>
  2908. <a name="FHEMWEBattr"></a>
  2909. <b>Attributes</b>
  2910. <ul>
  2911. <a name="addHtmlTitle"></a>
  2912. <li>addHtmlTitle<br>
  2913. If set to 0, do not add a title Attribute to the set/get/attr detail
  2914. widgets. This might be necessary for some screenreaders. Default is 1.
  2915. </li><br>
  2916. <li><a href="#addStateEvent">addStateEvent</a></li><br>
  2917. <li>alias_&lt;RoomName&gt;<br>
  2918. If you define a userattr alias_&lt;RoomName&gt; and set this attribute
  2919. for a device assgined to &lt;RoomName&gt;, then this value will be used
  2920. when displaying &lt;RoomName&gt;.<br>
  2921. Note: you can use the userattr alias_.* to allow all rooms, but in this
  2922. case the attribute dropdown in the device detail view won't work for the
  2923. alias_.* attributes.
  2924. </li><br>
  2925. <li><a href="#allowfrom">allowfrom</a></li>
  2926. </li><br>
  2927. <li>allowedCommands, basicAuth, basicAuthMsg<br>
  2928. Please create these attributes for the corresponding <a
  2929. href="#allowed">allowed</a> device, they are deprecated for the FHEMWEB
  2930. instance from now on.
  2931. </li><br>
  2932. <a name="closeConn"></a>
  2933. <li>closeConn<br>
  2934. If set, a TCP Connection will only serve one HTTP request. Seems to
  2935. solve problems on iOS9 for WebApp startup.
  2936. </li><br>
  2937. <a name="column"></a>
  2938. <li>column<br>
  2939. Allows to display more than one column per room overview, by specifying
  2940. the groups for the columns. Example:<br>
  2941. <ul><code>
  2942. attr WEB column LivingRoom:FS20,notify|FHZ,notify DiningRoom:FS20|FHZ
  2943. </code></ul>
  2944. In this example in the LivingRoom the FS20 and the notify group is in
  2945. the first column, the FHZ and the notify in the second.<br>
  2946. Notes: some elements like SVG plots and readingsGroup can only be part of
  2947. a column if they are part of a <a href="#group">group</a>.
  2948. This attribute can be used to sort the groups in a room, just specify
  2949. the groups in one column.
  2950. Space in the room and group name has to be written as %20 for this
  2951. attribute. Both the room name and the groups are regular expressions.
  2952. </li>
  2953. <br>
  2954. <a name="confirmDelete"></a>
  2955. <li>confirmDelete<br>
  2956. confirm delete actions with a dialog. Default is 1, set it to 0 to
  2957. disable the feature.
  2958. </li>
  2959. <br>
  2960. <a name="confirmJSError"></a>
  2961. <li>confirmJSError<br>
  2962. JavaScript errors are reported in a dialog as default.
  2963. Set this attribute to 0 to disable the reporting.
  2964. </li>
  2965. <br>
  2966. <a name="CORS"></a>
  2967. <li>CORS<br>
  2968. If set to 1, FHEMWEB will supply a "Cross origin resource sharing"
  2969. header, see the wikipedia for details.
  2970. </li>
  2971. <br>
  2972. <a name="csrfToken"></a>
  2973. <li>csrfToken<br>
  2974. If set, FHEMWEB requires the value of this attribute as fwcsrf Parameter
  2975. for each command. It is used as countermeasure for Cross Site Resource
  2976. Forgery attacks. If the value is random, then a random number will be
  2977. generated on each FHEMWEB start. If it is set to the literal string
  2978. none, no token is expected. Default is random for featurelevel 5.8 and
  2979. greater, and none for featurelevel below 5.8 </li><br>
  2980. <a name="csrfTokenHTTPHeader"></a>
  2981. <li>csrfTokenHTTPHeader<br>
  2982. If set (default), FHEMWEB sends the token with the X-FHEM-csrfToken HTTP
  2983. header, which is used by some clients. Set it to 0 to switch it off, as
  2984. a measurre against shodan.io like FHEM-detection.</li><br>
  2985. <a name="CssFiles"></a>
  2986. <li>CssFiles<br>
  2987. Space separated list of .css files to be included. The filenames
  2988. are relative to the www directory. Example:
  2989. <ul><code>
  2990. attr WEB CssFiles pgm2/mystyle.css
  2991. </code></ul>
  2992. </li><br>
  2993. <a name="cmdIcon"></a>
  2994. <li>cmdIcon<br>
  2995. Space separated list of cmd:iconName pairs. If set, the webCmd text is
  2996. replaced with the icon. An easy method to set this value is to use
  2997. "Extend devStateIcon" in the detail-view, and copy its value.<br>
  2998. Example:<ul>
  2999. attr lamp cmdIcon on:control_centr_arrow_up off:control_centr_arrow_down
  3000. </ul>
  3001. </li><br>
  3002. <a name="defaultRoom"></a>
  3003. <li>defaultRoom<br>
  3004. show the specified room if no room selected, e.g. on execution of some
  3005. commands. If set hides the <a href="#motd">motd</a>. Example:<br>
  3006. attr WEB defaultRoom Zentrale
  3007. </li>
  3008. <br>
  3009. <a name="devStateIcon"></a>
  3010. <li>devStateIcon<br>
  3011. First form:<br>
  3012. <ul>
  3013. Space separated list of regexp:icon-name:cmd triples, icon-name and cmd
  3014. may be empty.<br>
  3015. If the state of the device matches regexp, then icon-name will be
  3016. displayed as the status icon in the room, and (if specified) clicking
  3017. on the icon executes cmd. If fhem cannot find icon-name, then the
  3018. status text will be displayed.
  3019. Example:<br>
  3020. <ul>
  3021. attr lamp devStateIcon on:closed off:open<br>
  3022. attr lamp devStateIcon on::A0 off::AI<br>
  3023. attr lamp devStateIcon .*:noIcon<br>
  3024. </ul>
  3025. Note: if the image is referencing an SVG icon, then you can use the
  3026. @colorname suffix to color the image. E.g.:<br>
  3027. <ul>
  3028. attr Fax devStateIcon on:control_building_empty@red
  3029. off:control_building_filled:278727
  3030. </ul>
  3031. If the cmd is noFhemwebLink, then no HTML-link will be generated, i.e.
  3032. nothing will happen when clicking on the icon or text.
  3033. </ul>
  3034. Second form:<br>
  3035. <ul>
  3036. Perl code enclosed in {}. If the code returns undef, then the default
  3037. icon is used, if it retuns a string enclosed in <>, then it is
  3038. interpreted as an html string. Else the string is interpreted as a
  3039. devStateIcon of the first fom, see above.
  3040. Example:<br>
  3041. {'&lt;div
  3042. style="width:32px;height:32px;background-color:green"&gt;&lt;/div&gt;'}
  3043. </ul>
  3044. </li>
  3045. <br>
  3046. <a name="devStateStyle"></a>
  3047. <li>devStateStyle<br>
  3048. Specify an HTML style for the given device, e.g.:<br>
  3049. <ul>
  3050. attr sensor devStateStyle style="text-align:left;;font-weight:bold;;"<br>
  3051. </ul>
  3052. </li>
  3053. <br>
  3054. <li>deviceOverview<br>
  3055. Configures if the device line from the room view (device icon, state
  3056. icon and webCmds/cmdIcons) should also be shown in the device detail
  3057. view. Can be set to always, onClick, iconOnly or never. Default is
  3058. always.
  3059. </li><br>
  3060. <a name="editConfig"></a>
  3061. <li>editConfig<br>
  3062. If this FHEMWEB attribute is set to 1, then you will be able to edit
  3063. the FHEM configuration file (fhem.cfg) in the "Edit files" section.
  3064. After saving this file a rereadcfg is executed automatically, which has
  3065. a lot of side effects.<br>
  3066. </li><br>
  3067. <a name="editFileList"></a>
  3068. <li>editFileList<br>
  3069. Specify the list of Files shown in "Edit Files" section. It is a
  3070. newline separated list of triples, the first is the Title, the next is
  3071. the directory to search for, the third the regular expression. Default
  3072. is:
  3073. <ul>
  3074. <code>
  3075. Own modules and helper files:$MW_dir:^(.*sh|[0-9][0-9].*Util.*pm|.*cfg|.*holiday|myUtilsTemplate.pm|.*layout)$<br>
  3076. Gplot files:$FW_gplotdir:^.*gplot$<br>
  3077. Styles:$FW_cssdir:^.*(css|svg)$<br>
  3078. </code>
  3079. </ul>
  3080. NOTE: The directory spec is not flexible: all .js/.css/_defs.svg files
  3081. come from www/pgm2 ($FW_cssdir), .gplot files from $FW_gplotdir
  3082. (www/gplot), everything else from $MW_dir (FHEM).
  3083. </li><br>
  3084. <a name="endPlotNow"></a>
  3085. <li>endPlotNow<br>
  3086. If this FHEMWEB attribute is set to 1, then day and hour plots will
  3087. end at current time. Else the whole day, the 6 hour period starting at
  3088. 0, 6, 12 or 18 hour or the whole hour will be shown. This attribute
  3089. is not used if the SVG has the attribute startDate defined.<br>
  3090. </li><br>
  3091. <a name="endPlotToday"></a>
  3092. <li>endPlotToday<br>
  3093. If this FHEMWEB attribute is set to 1, then week and month plots will
  3094. end today. Else the current week or the current month will be shown.
  3095. <br>
  3096. </li><br>
  3097. <a name="fwcompress"></a>
  3098. <li>fwcompress<br>
  3099. Enable compressing the HTML data (default is 1, i.e. yes, use 0 to switch it off).
  3100. </li>
  3101. <br>
  3102. <a name="hiddengroup"></a>
  3103. <li>hiddengroup<br>
  3104. Comma separated list of groups to "hide", i.e. not to show in any room
  3105. of this FHEMWEB instance.<br>
  3106. Example: attr WEBtablet hiddengroup FileLog,dummy,at,notify
  3107. </li>
  3108. <br>
  3109. <a name="hiddengroupRegexp"></a>
  3110. <li>hiddengroupRegexp<br>
  3111. One regexp for the same purpose as hiddengroup.
  3112. </li>
  3113. <br>
  3114. <a name="hiddenroom"></a>
  3115. <li>hiddenroom<br>
  3116. Comma separated list of rooms to "hide", i.e. not to show. Special
  3117. values are input, detail and save, in which case the input areas, link
  3118. to the detailed views or save button is hidden (although each aspect
  3119. still can be addressed through URL manipulation).<br>
  3120. The list can also contain values from the additional "Howto/Wiki/FAQ"
  3121. block.
  3122. </li>
  3123. <br>
  3124. <a name="hiddenroomRegexp"></a>
  3125. <li>hiddenroomRegexp<br>
  3126. One regexp for the same purpose as hiddenroom. Example:
  3127. <ul>
  3128. attr WEB hiddenroomRegexp .*config
  3129. </ul>
  3130. Note: the special values input, detail and save cannot be specified
  3131. with hiddenroomRegexp.
  3132. </li>
  3133. <br>
  3134. <a name="HTTPS"></a>
  3135. <li>HTTPS<br>
  3136. Enable HTTPS connections. This feature requires the perl module
  3137. IO::Socket::SSL, to be installed with cpan -i IO::Socket::SSL or
  3138. apt-get install libio-socket-ssl-perl; OSX and the FritzBox-7390
  3139. already have this module.<br>
  3140. A local certificate has to be generated into a directory called certs,
  3141. this directory <b>must</b> be in the <a href="#modpath">modpath</a>
  3142. directory, at the same level as the FHEM directory.
  3143. <ul>
  3144. mkdir certs<br>
  3145. cd certs<br>
  3146. openssl req -new -x509 -nodes -out server-cert.pem -days 3650 -keyout server-key.pem
  3147. </ul>
  3148. <br>
  3149. </li>
  3150. <a name="icon"></a>
  3151. <li>icon<br>
  3152. Set the icon for a device in the room overview. There is an
  3153. icon-chooser in FHEMWEB to ease this task. Setting icons for the room
  3154. itself is indirect: there must exist an icon with the name
  3155. ico<ROOMNAME>.png in the iconPath.
  3156. </li>
  3157. <br>
  3158. <a name="iconPath"></a>
  3159. <li>iconPath<br>
  3160. colon separated list of directories where the icons are read from.
  3161. The directories start in the fhem/www/images directory. The default is
  3162. $styleSheetPrefix:default:fhemSVG:openautomation<br>
  3163. Set it to fhemSVG:openautomation to get only SVG images.
  3164. </li>
  3165. <br>
  3166. <a name="JavaScripts"></a>
  3167. <li>JavaScripts<br>
  3168. Space separated list of JavaScript files to be included. The filenames
  3169. are relative to the www directory. For each file an additional
  3170. user-settable FHEMWEB attribute will be created, to pass parameters to
  3171. the script. The name of this additional attribute gets the Param
  3172. suffix, directory and the fhem_ prefix will be deleted. Example:
  3173. <ul><code>
  3174. attr WEB JavaScripts codemirror/fhem_codemirror.js<br>
  3175. attr WEB codemirrorParam { "theme":"blackboard", "lineNumbers":true }
  3176. </code></ul>
  3177. </li><br>
  3178. <a name="longpoll"></a>
  3179. <li>longpoll<br>
  3180. Affects devices states in the room overview only.<br>
  3181. In this mode status update is refreshed more or less instantaneously,
  3182. and state change (on/off only) is done without requesting a complete
  3183. refresh from the server.
  3184. Default is on.
  3185. </li>
  3186. <br>
  3187. <a name="longpollSVG"></a>
  3188. <li>longpollSVG<br>
  3189. Reloads an SVG weblink, if an event should modify its content. Since
  3190. an exact determination of the affected events is too complicated, we
  3191. need some help from the definition in the .gplot file: the filter used
  3192. there (second parameter if the source is FileLog) must either contain
  3193. only the deviceName or have the form deviceName.event or deviceName.*.
  3194. This is always the case when using the <a href="#plotEditor">Plot
  3195. editor</a>. The SVG will be reloaded for <b>any</b> event triggered by
  3196. this deviceName. Default is off.
  3197. </li>
  3198. <br>
  3199. <a name="mainInputLength"></a>
  3200. <li>mainInputLength<br>
  3201. length of the maininput text widget in characters (decimal number).
  3202. </li>
  3203. <br>
  3204. <a name="menuEntries"></a>
  3205. <li>menuEntries<br>
  3206. Comma separated list of name,html-link pairs to display in the
  3207. left-side list. Example:<br>
  3208. attr WEB menuEntries fhem.de,http://fhem.de,culfw.de,http://culfw.de<br>
  3209. attr WEB menuEntries
  3210. AlarmOn,http://fhemhost:8083/fhem?cmd=set%20alarm%20on<br>
  3211. </li>
  3212. <br>
  3213. <a name="nameDisplay"></a>
  3214. <li>nameDisplay<br>
  3215. The argument is perl code, which is executed for each single device in
  3216. the room to determine the name displayed. $DEVICE is the name of the
  3217. current device, and $ALIAS is the value of the alias attribute or the
  3218. name of the device, if no alias is set. E.g. you can add a a global
  3219. userattr named alias_hu for the Hungarian translation, and specify
  3220. nameDisplay for the hungarian FHEMWEB instance as
  3221. <ul>
  3222. AttrVal($DEVICE, "alias_hu", $ALIAS)
  3223. </ul>
  3224. </li>
  3225. <br>
  3226. <a name="nrAxis"></a>
  3227. <li>nrAxis<br>
  3228. the number of axis for which space should be reserved on the left and
  3229. right sides of a plot and optionaly how many axes should realy be used
  3230. on each side, separated by comma: left,right[,useLeft,useRight]. You
  3231. can set individual numbers by setting the nrAxis of the SVG. Default is
  3232. 1,1.
  3233. </li><br>
  3234. <a name="ploteditor"></a>
  3235. <li>ploteditor<br>
  3236. Configures if the <a href="#plotEditor">Plot editor</a> should be shown
  3237. in the SVG detail view.
  3238. Can be set to always, onClick or never. Default is always.
  3239. </li><br>
  3240. <a name="plotEmbed"></a>
  3241. <li>plotEmbed 0<br>
  3242. SVG plots are rendered as part of &lt;embed&gt; tags, as in the past
  3243. this was the only way to display SVG, and it allows to render them in
  3244. parallel, see plotfork.
  3245. Setting plotEmbed to 0 will render SVG in-place, but as a side-effect
  3246. makes the plotfork attribute meaningless.<br>
  3247. </li><br>
  3248. <a name="plotfork"></a>
  3249. <li>plotfork [&lt;&Delta;p&gt;]<br>
  3250. If set to a nonzero value, run part of the processing (e.g. <a
  3251. href="#SVG">SVG</a> plot generation or <a href="#RSS">RSS</a> feeds) in
  3252. parallel processes. Actually, child processes are forked whose
  3253. priorities are the FHEM process' priority plus &Delta;p.
  3254. Higher values mean lower priority. e.g. use &Delta;p= 10 to renice the
  3255. child processes and provide more CPU power to the main FHEM process.
  3256. &Delta;p is optional and defaults to 0.<br>
  3257. Note: do not use it
  3258. on Windows and on systems with small memory footprint.
  3259. </li><br>
  3260. <a name="plotmode"></a>
  3261. <li>plotmode<br>
  3262. Specifies how to generate the plots:
  3263. <ul>
  3264. <li>SVG<br>
  3265. The plots are created with the <a href="#SVG">SVG</a> module.
  3266. This is the default.</li>
  3267. <li>gnuplot-scroll<br>
  3268. The plots are created with the gnuplot program. The gnuplot
  3269. output terminal PNG is assumed. Scrolling to historical values
  3270. is also possible, just like with SVG.</li>
  3271. <li>gnuplot-scroll-svg<br>
  3272. Like gnuplot-scroll, but the output terminal SVG is assumed.</li>
  3273. </ul>
  3274. </li><br>
  3275. <a name="plotsize"></a>
  3276. <li>plotsize<br>
  3277. the default size of the plot, in pixels, separated by comma:
  3278. width,height. You can set individual sizes by setting the plotsize of
  3279. the SVG. Default is 800,160 for desktop, and 480,160 for
  3280. smallscreen.
  3281. </li><br>
  3282. <a name="plotWeekStartDay"></a>
  3283. <li>plotWeekStartDay<br>
  3284. Start the week-zoom of the SVG plots with this day.
  3285. 0 is Sunday, 1 is Monday, etc.<br>
  3286. </li><br>
  3287. <a name="redirectCmds"></a>
  3288. <li>redirectCmds<br>
  3289. Clear the browser URL window after issuing the command by redirecting
  3290. the browser, as a reload for the same site might have unintended
  3291. side-effects. Default is 1 (enabled). Disable it by setting this
  3292. attribute to 0 if you want to study the command syntax, in order to
  3293. communicate with FHEMWEB.
  3294. </li>
  3295. <br>
  3296. <a name="refresh"></a>
  3297. <li>refresh<br>
  3298. If set, a http-equiv="refresh" entry will be genererated with the given
  3299. argument (i.e. the browser will reload the page after the given
  3300. seconds).
  3301. </li><br>
  3302. <a name="reverseLogs"></a>
  3303. <li>reverseLogs<br>
  3304. Display the lines from the logfile in a reversed order, newest on the
  3305. top, so that you dont have to scroll down to look at the latest entries.
  3306. Note: enabling this attribute will prevent FHEMWEB from streaming
  3307. logfiles, resulting in a considerably increased memory consumption
  3308. (about 6 times the size of the file on the disk).
  3309. </li>
  3310. <br>
  3311. <a name="roomIcons"></a>
  3312. <li>roomIcons<br>
  3313. Space separated list of room:icon pairs, to override the default
  3314. behaviour of showing an icon, if there is one with the name of
  3315. "icoRoomName". This is the correct way to remove the icon for the room
  3316. Everything, or to set one for rooms with / in the name (e.g.
  3317. Anlagen/EDV). The first part is treated as regexp, so space is
  3318. represented by a dot. Example:<br>
  3319. attr WEB roomIcons Anlagen.EDV:icoEverything
  3320. </li>
  3321. <br>
  3322. <a name="smallscreenCommands"></a>
  3323. <li>smallscreenCommands<br>
  3324. If set to 1, commands, slider and dropdown menues will appear in
  3325. smallscreen landscape mode.
  3326. </li><br>
  3327. <a name="sortby"></a>
  3328. <li>sortby<br>
  3329. Take the value of this attribute when sorting the devices in the room
  3330. overview instead of the alias, or if that is missing the devicename
  3331. itself. If the sortby value is enclosed in {} than it is evaluated as a
  3332. perl expression. $NAME is set to the device name.
  3333. </li>
  3334. <br>
  3335. <a name="showUsedFiles"></a>
  3336. <li>showUsedFiles<br>
  3337. In the Edit files section, show only the used files.
  3338. Note: currently this is only working for the "Gplot files" section.
  3339. </li>
  3340. <br>
  3341. <a name="sortRooms"></a>
  3342. <li>sortRooms<br>
  3343. Space separated list of rooms to override the default sort order of the
  3344. room links. As the rooms in this attribute are actually regexps, space
  3345. in the roomname has to be specified as dot (.).
  3346. Example:<br>
  3347. attr WEB sortRooms DG OG EG Keller
  3348. </li>
  3349. <br>
  3350. <li>sslVersion<br>
  3351. See the global attribute sslVersion.
  3352. </li><br>
  3353. <a name="stylesheetPrefix"></a>
  3354. <li>stylesheetPrefix<br>
  3355. prefix for the files style.css, svg_style.css and svg_defs.svg. If the
  3356. file with the prefix is missing, the default file (without prefix) will
  3357. be used. These files have to be placed into the FHEM directory, and can
  3358. be selected directly from the "Select style" FHEMWEB menu entry. Example:
  3359. <ul>
  3360. attr WEB stylesheetPrefix dark<br>
  3361. <br>
  3362. Referenced files:<br>
  3363. <ul>
  3364. darksvg_defs.svg<br>
  3365. darksvg_style.css<br>
  3366. darkstyle.css<br>
  3367. </ul>
  3368. <br>
  3369. </ul>
  3370. <b>Note:</b>if the argument contains the string smallscreen or touchpad,
  3371. then FHEMWEB will optimize the layout/access for small screen size (i.e.
  3372. smartphones) or touchpad devices (i.e. tablets)<br>
  3373. The default configuration installs 3 FHEMWEB instances: port 8083 for
  3374. desktop browsers, port 8084 for smallscreen, and 8085 for touchpad.<br>
  3375. If touchpad or smallscreen is specified, then WebApp support is
  3376. activated: After viewing the site on the iPhone or iPad in Safari, you
  3377. can add a link to the home-screen to get full-screen support. Links are
  3378. rendered differently in this mode to avoid switching back to the "normal"
  3379. browser.
  3380. </li>
  3381. <br>
  3382. <a name="SVGcache"></a>
  3383. <li>SVGcache<br>
  3384. if set, cache plots which won't change any more (the end-date is prior
  3385. to the current timestamp). The files are written to the www/SVGcache
  3386. directory. Default is off.<br>
  3387. See also the clearSvgCache command for clearing the cache.
  3388. </li><br>
  3389. <a name="title"></a>
  3390. <li>title<br>
  3391. Sets the title of the page. If enclosed in {} the content is evaluated.
  3392. </li><br>
  3393. <a name="viewport"></a>
  3394. <li>viewport<br>
  3395. Sets the &quot;viewport&quot; attribute in the HTML header. This can for
  3396. example be used to force the width of the page or disable zooming.<br>
  3397. Example: attr WEB viewport
  3398. width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no
  3399. </li><br>
  3400. <a name="webCmd"></a>
  3401. <li>webCmd<br>
  3402. Colon separated list of commands to be shown in the room overview for a
  3403. certain device. Has no effect on smallscreen devices, see the
  3404. devStateIcon command for an alternative.<br>
  3405. Example:
  3406. <ul>
  3407. attr lamp webCmd on:off:on-for-timer 10<br>
  3408. </ul>
  3409. <br>
  3410. The first specified command is looked up in the "set device ?" list
  3411. (see the <a href="#setList">setList</a> attribute for dummy devices).
  3412. If <b>there</b> it contains some known modifiers (colon, followed
  3413. by a comma separated list), then a different widget will be displayed.
  3414. See also the widgetOverride attribute below. Examples:
  3415. <ul>
  3416. define d1 dummy<br>
  3417. attr d1 webCmd state<br>
  3418. attr d1 readingList state<br>
  3419. attr d1 setList state:on,off<br><br>
  3420. define d2 dummy<br>
  3421. attr d2 webCmd state<br>
  3422. attr d2 readingList state<br>
  3423. attr d2 setList state:slider,0,1,10<br><br>
  3424. define d3 dummy<br>
  3425. attr d3 webCmd state<br>
  3426. attr d3 readingList state<br>
  3427. attr d3 setList state:time<br>
  3428. </ul>
  3429. If the command is state, then the value will be used as a command.<br>
  3430. Note: this is an attribute for the displayed device, not for the FHEMWEB
  3431. instance.
  3432. </li>
  3433. <br>
  3434. <a name="webCmdLabel"></a>
  3435. <li>webCmdLabel<br>
  3436. Colon separated list of labels, used to prefix each webCmd. The number
  3437. of labels must exactly match the number of webCmds. To implement
  3438. multiple rows, insert a return character after the text and before the
  3439. colon.</li></br>
  3440. <a name="webname"></a>
  3441. <li>webname<br>
  3442. Path after the http://hostname:port/ specification. Defaults to fhem,
  3443. i.e the default http address is http://localhost:8083/fhem
  3444. </li><br>
  3445. <a name="widgetOverride"></a>
  3446. <li>widgetOverride<br>
  3447. Space separated list of name:modifier pairs, to override the widget
  3448. for a set/get/attribute specified by the module author.
  3449. Following is the list of known modifiers:
  3450. <ul>
  3451. <!-- INSERT_DOC_FROM: www/pgm2/fhemweb.*.js -->
  3452. </ul>
  3453. </li>
  3454. <br>
  3455. </ul>
  3456. </ul>
  3457. =end html
  3458. =begin html_DE
  3459. <a name="FHEMWEB"></a>
  3460. <h3>FHEMWEB</h3>
  3461. <ul>
  3462. FHEMWEB ist das default WEB-Frontend, es implementiert auch einen einfachen
  3463. Webserver (optional mit Basic-Auth und HTTPS).
  3464. <br> <br>
  3465. <a name="FHEMWEBdefine"></a>
  3466. <b>Define</b>
  3467. <ul>
  3468. <code>define &lt;name&gt; FHEMWEB &lt;tcp-portnr&gt; [global]</code>
  3469. <br><br>
  3470. Aktiviert das Webfrontend auf dem Port &lt;tcp-portnr&gt;. Mit dem
  3471. Parameter global werden Anfragen von allen Netzwerkschnittstellen
  3472. akzeptiert (nicht nur vom localhost / 127.0.0.1) . <br>
  3473. Informationen f&uuml;r den Betrieb mit IPv6 finden Sie <a
  3474. href="#telnet">hier</a>.<br>
  3475. </ul>
  3476. <br>
  3477. <a name="FHEMWEBset"></a>
  3478. <b>Set</b>
  3479. <ul>
  3480. <li>rereadicons<br>
  3481. Damit wird die Liste der Icons neu eingelesen, f&uuml;r den Fall, dass
  3482. Sie Icons l&ouml;schen oder hinzuf&uuml;gen.
  3483. </li>
  3484. <li>clearSvgCache<br>
  3485. Im Verzeichnis www/SVGcache werden SVG Daten zwischengespeichert, wenn
  3486. das Attribut SVGcache gesetzt ist. Mit diesem Befehl leeren Sie diesen
  3487. Zwischenspeicher.
  3488. </li>
  3489. </ul>
  3490. <br>
  3491. <a name="FHEMWEBget"></a>
  3492. <b>Get</b>
  3493. <ul>
  3494. <li>icon &lt;logical icon&gt;<br>
  3495. Liefert den absoluten Pfad des (logischen) Icons zur&uuml;ck. Beispiel:
  3496. <ul>
  3497. <code>get myFHEMWEB icon FS20.on<br>
  3498. /data/Homeautomation/fhem/FHEM/FS20.on.png
  3499. </code>
  3500. </ul></li>
  3501. <li>pathlist<br>
  3502. Zeigt diejenigen Verzeichnisse an, in welchen die verschiedenen Dateien
  3503. f&uuml;r FHEMWEB liegen.
  3504. </li>
  3505. <br><br>
  3506. </ul>
  3507. <a name="FHEMWEBattr"></a>
  3508. <b>Attribute</b>
  3509. <ul>
  3510. <a name="addHtmlTitle"></a>
  3511. <li>addHtmlTitle<br>
  3512. Falls der Wert 0 ist, wird bei den set/get/attr Parametern in der
  3513. DetailAnsicht der Ger&auml;te kein title Attribut gesetzt. Das is bei
  3514. manchen Screenreadern erforderlich. Die Voreinstellung ist 1.
  3515. </li><br>
  3516. <li><a href="#addStateEvent">addStateEvent</a></li><br>
  3517. <li>alias_&lt;RoomName&gt;<br>
  3518. Falls man das Attribut alias_&lt;RoomName&gt; definiert, und dieses
  3519. Attribut f&uuml;r ein Ger&auml;t setzt, dann wird dieser Wert bei
  3520. Anzeige von &lt;RoomName&gt; verwendet.<br>
  3521. Achtung: man kann im userattr auch alias_.* verwenden um alle
  3522. m&ouml;glichen R&auml;ume abzudecken, in diesem Fall wird aber die
  3523. Attributauswahl in der Detailansicht f&uuml;r alias_.* nicht
  3524. funktionieren.
  3525. </li><br>
  3526. <li><a href="#allowfrom">allowfrom</a>
  3527. </li><br>
  3528. <li>allowedCommands, basicAuth, basicAuthMsg<br>
  3529. Diese Attribute m&uuml;ssen ab sofort bei dem passenden <a
  3530. href="#allowed">allowed</a> Ger&auml;t angelegt werden, und sind
  3531. f&uuml;r eine FHEMWEB Instanz unerw&uuml;nscht.
  3532. </li><br>
  3533. <a name="closeConn"></a>
  3534. <li>closeConn<br>
  3535. Falls gesetzt, wird pro TCP Verbindung nur ein HTTP Request
  3536. durchgef&uuml;hrt. F&uuml;r iOS9 WebApp startups scheint es zu helfen.
  3537. </li><br>
  3538. <a name="cmdIcon"></a>
  3539. <li>cmdIcon<br>
  3540. Leerzeichen getrennte Auflistung von cmd:iconName Paaren.
  3541. Falls gesetzt, wird das webCmd text durch den icon gesetzt.
  3542. Am einfachsten setzt man cmdIcon indem man "Extend devStateIcon" im
  3543. Detail-Ansicht verwendet, und den Wert nach cmdIcon kopiert.<br>
  3544. Beispiel:<ul>
  3545. attr lamp cmdIcon on:control_centr_arrow_up off:control_centr_arrow_down
  3546. </ul>
  3547. </li><br>
  3548. <a name="column"></a>
  3549. <li>column<br>
  3550. Damit werden mehrere Spalten f&uuml;r einen Raum angezeigt, indem
  3551. sie verschiedene Gruppen Spalten zuordnen. Beispiel:<br>
  3552. <ul><code>
  3553. attr WEB column LivingRoom:FS20,notify|FHZ,notify DiningRoom:FS20|FHZ
  3554. </code></ul>
  3555. In diesem Beispiel werden im Raum LivingRoom die FS20 sowie die notify
  3556. Gruppe in der ersten Spalte, die FHZ und das notify in der zweiten
  3557. Spalte angezeigt.<br>
  3558. Anmerkungen: einige Elemente, wie SVG Plots und readingsGroup
  3559. k&ouml;nnen nur dann Teil einer Spalte sein wenn sie in <a
  3560. href="#group">group</a> stehen. Dieses Attribut kann man zum sortieren
  3561. der Gruppen auch dann verwenden, wenn man nur eine Spalte hat.
  3562. Leerzeichen im Raum- und Gruppennamen sind f&uuml;r dieses Attribut als
  3563. %20 zu schreiben. Raum- und Gruppenspezifikation ist jeweils ein
  3564. %regul&auml;rer Ausdruck.
  3565. </li><br>
  3566. <a name="confirmDelete"></a>
  3567. <li>confirmDelete<br>
  3568. L&ouml;schaktionen weden mit einem Dialog best&auml;tigt.
  3569. Falls dieses Attribut auf 0 gesetzt ist, entf&auml;llt das.
  3570. </li>
  3571. <br>
  3572. <a name="confirmJSError"></a>
  3573. <li>confirmJSError<br>
  3574. JavaScript Fehler werden per Voreinstellung in einem Dialog gemeldet.
  3575. Durch setzen dieses Attributes auf 0 werden solche Fehler nicht
  3576. gemeldet.
  3577. </li>
  3578. <br>
  3579. <a name="CORS"></a>
  3580. <li>CORS<br>
  3581. Wenn auf 1 gestellt, wird FHEMWEB einen "Cross origin resource sharing"
  3582. Header bereitstellen, n&auml;heres siehe Wikipedia.
  3583. </li><br>
  3584. <a name="csrfToken"></a>
  3585. <li>csrfToken<br>
  3586. Falls gesetzt, wird der Wert des Attributes als fwcsrf Parameter bei
  3587. jedem &uuml;ber FHEMWEB abgesetzten Kommando verlangt, es dient zum
  3588. Schutz von Cross Site Resource Forgery Angriffen.
  3589. Falls der Wert random ist, dann wird ein Zufallswert beim jeden FHEMWEB
  3590. Start neu generiert, falls er none ist, dann wird kein Parameter
  3591. verlangt. Default ist random f&uuml;r featurelevel 5.8 und
  3592. gr&ouml;&szlig;er, und none f&uuml;r featurelevel kleiner 5.8
  3593. </li><br>
  3594. <a name="csrfTokenHTTPHeader"></a>
  3595. <li>csrfTokenHTTPHeader<br>
  3596. Falls gesetzt (Voreinstellung), FHEMWEB sendet im HTTP Header den
  3597. csrfToken als X-FHEM-csrfToken, das wird von manchen FHEM-Clients
  3598. benutzt. Mit 0 kann man das abstellen, um Sites wie shodan.io die
  3599. Erkennung von FHEM zu erschweren.</li><br>
  3600. <a name="CssFiles"></a>
  3601. <li>CssFiles<br>
  3602. Leerzeichen getrennte Liste von .css Dateien, die geladen werden.
  3603. Die Dateinamen sind relativ zum www Verzeichnis anzugeben. Beispiel:
  3604. <ul><code>
  3605. attr WEB CssFiles pgm2/mystyle.css
  3606. </code></ul>
  3607. </li><br>
  3608. <a name="defaultRoom"></a>
  3609. <li>defaultRoom<br>
  3610. Zeigt den angegebenen Raum an falls kein Raum explizit ausgew&auml;hlt
  3611. wurde. Achtung: falls gesetzt, wird motd nicht mehr angezeigt.
  3612. Beispiel:<br>
  3613. attr WEB defaultRoom Zentrale
  3614. </li><br>
  3615. <a name="devStateIcon"></a>
  3616. <li>devStateIcon<br>
  3617. Erste Variante:<br>
  3618. <ul>
  3619. Leerzeichen getrennte Auflistung von regexp:icon-name:cmd
  3620. Dreierp&auml;rchen, icon-name und cmd d&uuml;rfen leer sein.<br>
  3621. Wenn der Zustand des Ger&auml;tes mit der regexp &uuml;bereinstimmt,
  3622. wird als icon-name das entsprechende Status Icon angezeigt, und (falls
  3623. definiert), l&ouml;st ein Klick auf das Icon das entsprechende cmd aus.
  3624. Wenn fhem icon-name nicht finden kann, wird der Status als Text
  3625. angezeigt.
  3626. Beispiel:<br>
  3627. <ul>
  3628. attr lamp devStateIcon on:closed off:open<br>
  3629. attr lamp devStateIcon on::A0 off::AI<br>
  3630. attr lamp devStateIcon .*:noIcon<br>
  3631. </ul>
  3632. Anmerkung: Wenn das Icon ein SVG Bild ist, kann das @colorname Suffix
  3633. verwendet werden um das Icon einzuf&auml;rben. Z.B.:<br>
  3634. <ul>
  3635. attr Fax devStateIcon on:control_building_empty@red
  3636. off:control_building_filled:278727
  3637. </ul>
  3638. Falls cmd noFhemwebLink ist, dann wird kein HTML-Link generiert, d.h.
  3639. es passiert nichts, wenn man auf das Icon/Text klickt.
  3640. </ul>
  3641. Zweite Variante:<br>
  3642. <ul>
  3643. Perl regexp eingeschlossen in {}. Wenn der Code undef
  3644. zur&uuml;ckliefert, wird das Standard Icon verwendet; wird ein String
  3645. in <> zur&uuml;ck geliefert, wird dieser als HTML String interpretiert.
  3646. Andernfalls wird der String als devStateIcon gem&auml;&szlig; der
  3647. ersten Variante interpretiert, siehe oben. Beispiel:<br>
  3648. {'&lt;div style="width:32px;height:32px;background-color:green"&gt;&lt;/div&gt;'}
  3649. </ul>
  3650. </li><br>
  3651. <a name="devStateStyle"></a>
  3652. <li>devStateStyle<br>
  3653. F&uuml;r ein best. Ger&auml;t einen best. HTML-Style benutzen.
  3654. Beispiel:<br>
  3655. <ul>
  3656. attr sensor devStateStyle style="text-align:left;;font-weight:bold;;"<br>
  3657. </ul>
  3658. </li><br>
  3659. <li>deviceOverview<br>
  3660. Gibt an ob die Darstellung aus der Raum-Ansicht (Zeile mit
  3661. Ger&uuml;teicon, Stateicon und webCmds/cmdIcons) auch in der
  3662. Detail-Ansicht angezeigt werden soll. Kann auf always, onClick,
  3663. iconOnly oder never gesetzt werden. Der Default ist always.
  3664. </li><br>
  3665. <a name="editConfig"></a>
  3666. <li>editConfig<br>
  3667. Falls dieses FHEMWEB Attribut (auf 1) gesetzt ist, dann kann man die
  3668. FHEM Konfigurationsdatei in dem "Edit files" Abschnitt bearbeiten. Beim
  3669. Speichern dieser Datei wird automatisch rereadcfg ausgefuehrt, was
  3670. diverse Nebeneffekte hat.<br>
  3671. </li><br>
  3672. <a name="editFileList"></a>
  3673. <li>editFileList<br>
  3674. Definiert die Liste der angezeigten Dateien in der "Edit Files" Abschnitt.
  3675. Es ist eine Newline getrennte Liste von Tripeln bestehend aus Titel,
  3676. Verzeichnis f&uuml;r die Suche, und Regexp. Die Voreinstellung ist:
  3677. <ul>
  3678. <code>
  3679. Own modules and helper files:$MW_dir:^(.*sh|[0-9][0-9].*Util.*pm|.*cfg|.*holiday|myUtilsTemplate.pm|.*layout)$<br>
  3680. Gplot files:$FW_gplotdir:^.*gplot$<br>
  3681. Styles:$FW_cssdir:^.*(css|svg)$<br>
  3682. </code>
  3683. </ul>
  3684. Achtung: die Verzeichnis Angabe ist nicht flexibel: alle
  3685. .js/.css/_defs.svg Dateien sind in www/pgm2 ($FW_cssdir), .gplot
  3686. Dateien in $FW_gplotdir (www/gplot), alles andere in $MW_dir (FHEM).
  3687. </li><br>
  3688. <a name="endPlotNow"></a>
  3689. <li>endPlotNow<br>
  3690. Wenn Sie dieses FHEMWEB Attribut auf 1 setzen, werden Tages und
  3691. Stunden-Plots zur aktuellen Zeit beendet. (&Auml;hnlich wie
  3692. endPlotToday, nur eben min&uuml;tlich).
  3693. Ansonsten wird der gesamte Tag oder eine 6 Stunden Periode (0, 6, 12,
  3694. 18 Stunde) gezeigt. Dieses Attribut wird nicht verwendet, wenn das SVG
  3695. Attribut startDate benutzt wird.<br>
  3696. </li><br>
  3697. <a name="endPlotToday"></a>
  3698. <li>endPlotToday<br>
  3699. Wird dieses FHEMWEB Attribut gesetzt, so enden Wochen- bzw. Monatsplots
  3700. am aktuellen Tag, sonst wird die aktuelle Woche/Monat angezeigt.
  3701. </li><br>
  3702. <a name="fwcompress"></a>
  3703. <li>fwcompress<br>
  3704. Aktiviert die HTML Datenkompression (Standard ist 1, also ja, 0 stellt
  3705. die Kompression aus).
  3706. </li><br>
  3707. <a name="hiddengroup"></a>
  3708. <li>hiddengroup<br>
  3709. Wie hiddenroom (siehe unten), jedoch auf Ger&auml;tegruppen bezogen.
  3710. <br>
  3711. Beispiel: attr WEBtablet hiddengroup FileLog,dummy,at,notify
  3712. </li><br>
  3713. <a name="hiddengroupRegexp"></a>
  3714. <li>hiddengroupRegexp<br>
  3715. Ein regul&auml;rer Ausdruck, um Gruppen zu verstecken.
  3716. </li>
  3717. <br>
  3718. <a name="hiddenroom"></a>
  3719. <li>hiddenroom<br>
  3720. Eine Komma getrennte Liste, um R&auml;ume zu verstecken, d.h. nicht
  3721. anzuzeigen. Besondere Werte sind input, detail und save. In diesem
  3722. Fall werden diverse Eingabefelder ausgeblendent. Durch direktes Aufrufen
  3723. der URL sind diese R&auml;ume weiterhin erreichbar!<br>
  3724. Ebenso k&ouml;nnen Eintr&auml;ge in den Logfile/Commandref/etc Block
  3725. versteckt werden. </li><br>
  3726. <a name="hiddenroomRegexp"></a>
  3727. <li>hiddenroomRegexp<br>
  3728. Ein regul&auml;rer Ausdruck, um R&auml;ume zu verstecken. Beispiel:
  3729. <ul>
  3730. attr WEB hiddenroomRegexp .*config
  3731. </ul>
  3732. Achtung: die besonderen Werte input, detail und save m&uuml;ssen mit
  3733. hiddenroom spezifiziert werden.
  3734. </li>
  3735. <br>
  3736. <a name="HTTPS"></a>
  3737. <li>HTTPS<br>
  3738. Erm&ouml;glicht HTTPS Verbindungen. Es werden die Perl Module
  3739. IO::Socket::SSL ben&ouml;tigt, installierbar mit cpan -i
  3740. IO::Socket::SSL oder apt-get install libio-socket-ssl-perl; (OSX und
  3741. die FritzBox-7390 haben dieses Modul schon installiert.)<br>
  3742. Ein lokales Zertifikat muss im Verzeichis certs erzeugt werden.
  3743. Dieses Verzeichnis <b>muss</b> im <a href="#modpath">modpath</a>
  3744. angegeben werden, also auf der gleichen Ebene wie das FHEM Verzeichnis.
  3745. Beispiel:
  3746. <ul>
  3747. mkdir certs<br>
  3748. cd certs<br>
  3749. openssl req -new -x509 -nodes -out server-cert.pem -days 3650 -keyout
  3750. server-key.pem
  3751. </ul>
  3752. <br>
  3753. </li>
  3754. <a name="icon"></a>
  3755. <li>icon<br>
  3756. Damit definiert man ein Icon f&uuml;r die einzelnen Ger&auml;te in der
  3757. Raum&uuml;bersicht. Es gibt einen passenden Link in der Detailansicht
  3758. um das zu vereinfachen. Um ein Bild f&uuml;r die R&auml;ume selbst zu
  3759. definieren muss ein Icon mit dem Namen ico&lt;Raumname&gt;.png im
  3760. iconPath existieren (oder man verwendet roomIcons, s.u.)
  3761. </li><br>
  3762. <a name="iconPath"></a>
  3763. <li>iconPath<br>
  3764. Durch Doppelpunkt getrennte Aufz&auml;hlung der Verzeichnisse, in
  3765. welchen nach Icons gesucht wird. Die Verzeichnisse m&uuml;ssen unter
  3766. fhem/www/images angelegt sein. Standardeinstellung ist:
  3767. $styleSheetPrefix:default:fhemSVG:openautomation<br>
  3768. Setzen Sie den Wert auf fhemSVG:openautomation um nur SVG Bilder zu
  3769. benutzen.
  3770. </li><br>
  3771. <a name="JavaScripts"></a>
  3772. <li>JavaScripts<br>
  3773. Leerzeichen getrennte Liste von JavaScript Dateien, die geladen werden.
  3774. Die Dateinamen sind relativ zum www Verzeichnis anzugeben. F&uuml;r
  3775. jede Datei wird ein zus&auml;tzliches Attribut angelegt, damit der
  3776. Benutzer dem Skript Parameter weiterreichen kann. Bei diesem
  3777. Attributnamen werden Verzeichnisname und fhem_ Pr&auml;fix entfernt
  3778. und Param als Suffix hinzugef&uuml;gt. Beispiel:
  3779. <ul><code>
  3780. attr WEB JavaScripts codemirror/fhem_codemirror.js<br>
  3781. attr WEB codemirrorParam { "theme":"blackboard", "lineNumbers":true }
  3782. </code></ul>
  3783. </li><br>
  3784. <a name="longpoll"></a>
  3785. <li>longpoll<br>
  3786. Dies betrifft die Aktualisierung der Ger&auml;testati in der
  3787. Weboberfl&auml;che. Ist longpoll aktiviert, werden
  3788. Status&auml;nderungen sofort im Browser dargestellt. ohne die Seite
  3789. manuell neu laden zu m&uuml;ssen. Standard ist aktiviert.
  3790. </li><br>
  3791. <a name="longpollSVG"></a>
  3792. <li>longpollSVG<br>
  3793. L&auml;dt SVG Instanzen erneut, falls ein Ereignis dessen Inhalt
  3794. &auml;ndert. Funktioniert nur, falls die dazugeh&ouml;rige Definition
  3795. der Quelle in der .gplot Datei folgenden Form hat: deviceName.Event
  3796. bzw. deviceName.*. Wenn man den <a href="#plotEditor">Plot Editor</a>
  3797. benutzt, ist das &uuml;brigens immer der Fall. Die SVG Datei wird bei
  3798. <b>jedem</b> ausl&ouml;senden Event dieses Ger&auml;tes neu geladen.
  3799. Die Voreinstellung ist aus.
  3800. </li><br>
  3801. <a name="mainInputLength"></a>
  3802. <li>mainInputLength<br>
  3803. L&auml;nge des maininput Eingabefeldes (Anzahl der Buchstaben,
  3804. Ganzzahl).
  3805. </li> <br>
  3806. <a name="menuEntries"></a>
  3807. <li>menuEntries<br>
  3808. Komma getrennte Liste; diese Links werden im linken Men&uuml; angezeigt.
  3809. Beispiel:<br>
  3810. attr WEB menuEntries fhem.de,http://fhem.de,culfw.de,http://culfw.de<br>
  3811. attr WEB menuEntries
  3812. AlarmOn,http://fhemhost:8083/fhem?cmd=set%20alarm%20on<br>
  3813. </li><br>
  3814. <a name="nameDisplay"></a>
  3815. <li>nameDisplay<br>
  3816. Das Argument ist Perl-Code, was f&uuml;r jedes Ger&auml;t in der
  3817. Raum-&Uuml;bersicht ausgef&uuml;hrt wird, um den angezeigten Namen zu
  3818. berechnen. Dabei kann man die Variable $DEVICE f&uuml;r den aktuellen
  3819. Ger&auml;tenamen, und $ALIAS f&uuml;r den aktuellen alias bzw. Name,
  3820. falls alias nicht gesetzt ist, verwenden. Z.Bsp. f&uuml;r eine FHEMWEB
  3821. Instanz mit ungarischer Anzeige f&uuml;gt man ein global userattr
  3822. alias_hu hinzu, und man setzt nameDisplay f&uuml;r diese FHEMWEB
  3823. Instanz auf dem Wert:
  3824. <ul>
  3825. AttrVal($DEVICE, "alias_hu", $ALIAS)
  3826. </ul>
  3827. </li>
  3828. <br>
  3829. <a name="nrAxis"></a>
  3830. <li>nrAxis<br>
  3831. (bei mehrfach-Y-Achsen im SVG-Plot) Die Darstellung der Y Achsen
  3832. ben&ouml;tigt Platz. Hierdurch geben Sie an wie viele Achsen Sie
  3833. links,rechts [useLeft,useRight] ben&ouml;tigen. Default ist 1,1 (also 1
  3834. Achse links, 1 Achse rechts).
  3835. </li><br>
  3836. <a name="ploteditor"></a>
  3837. <li>ploteditor<br>
  3838. Gibt an ob der <a href="#plotEditor">Plot Editor</a> in der SVG detail
  3839. ansicht angezeigt werden soll. Kann auf always, onClick oder never
  3840. gesetzt werden. Der Default ist always.
  3841. </li><br>
  3842. <a name="plotEmbed"></a>
  3843. <li>plotEmbed 0<br>
  3844. SVG Grafiken werden als Teil der &lt;embed&gt; Tags dargestellt, da
  3845. fr&uuml;her das der einzige Weg war SVG darzustellen, weiterhin
  3846. erlaubt es das parallele Berechnen via plotfork (s.o.)
  3847. Falls plotEmbed auf 0 gesetzt wird, dann werden die SVG Grafiken als
  3848. Teil der HTML-Seite generiert, was leider das plotfork Attribut
  3849. wirkungslos macht.
  3850. </li><br>
  3851. <a name="plotfork"></a>
  3852. <li>plotfork<br>
  3853. Normalerweise wird die Ploterstellung im Hauptprozess ausgef&uuml;hrt,
  3854. FHEM wird w&auml;rend dieser Zeit nicht auf andere Ereignisse
  3855. reagieren.
  3856. Falls dieses Attribut auf einen nicht 0 Wert gesetzt ist, dann wird die
  3857. Berechnung in weitere Prozesse ausgelagert. Das kann die Berechnung auf
  3858. Rechnern mit mehreren Prozessoren beschleunigen, allerdings kann es auf
  3859. Rechnern mit wenig Speicher (z.Bsp. FRITZ!Box 7390) zum automatischen
  3860. Abschuss des FHEM Prozesses durch das OS f&uuml;hren.
  3861. </li><br>
  3862. <a name="plotmode"></a>
  3863. <li>plotmode<br>
  3864. Spezifiziert, wie Plots erzeugt werden sollen:
  3865. <ul>
  3866. <li>SVG<br>
  3867. Die Plots werden mit Hilfe des <a href="#SVG">SVG</a> Moduls als SVG
  3868. Grafik gerendert. Das ist die Standardeinstellung.</li>
  3869. <li>gnuplot-scroll<br>
  3870. Die plots werden mit dem Programm gnuplot erstellt. Das output
  3871. terminal ist PNG. Der einfache Zugriff auf historische Daten
  3872. ist m&ouml;glich (analog SVG).
  3873. </li>
  3874. <li>gnuplot-scroll-svg<br>
  3875. Wie gnuplot-scroll, aber als output terminal wird SVG angenommen.
  3876. </li>
  3877. </ul>
  3878. </li><br>
  3879. <a name="plotsize"></a>
  3880. <li>plotsize<br>
  3881. gibt die Standardbildgr&ouml;&szlig;e aller erzeugten Plots an als
  3882. Breite,H&ouml;he an. Um einem individuellen Plot die Gr&ouml;&szlig;e zu
  3883. &auml;ndern muss dieses Attribut bei der entsprechenden SVG Instanz
  3884. gesetzt werden. Default sind 800,160 f&uuml;r Desktop und 480,160
  3885. f&uuml;r Smallscreen
  3886. </li><br>
  3887. <a name="plotWeekStartDay"></a>
  3888. <li>plotWeekStartDay<br>
  3889. Starte das Plot in der Wochen-Ansicht mit diesem Tag.
  3890. 0 ist Sonntag, 1 ist Montag, usw.
  3891. </li><br>
  3892. <a name="redirectCmds"></a>
  3893. <li>redirectCmds<br>
  3894. Damit wird das URL Eingabefeld des Browser nach einem Befehl geleert.
  3895. Standard ist eingeschaltet (1), ausschalten kann man es durch
  3896. setzen des Attributs auf 0, z.Bsp. um den Syntax der Kommunikation mit
  3897. FHEMWEB zu untersuchen.
  3898. </li><br>
  3899. <a name="refresh"></a>
  3900. <li>refresh<br>
  3901. Damit erzeugen Sie auf den ausgegebenen Webseiten einen automatischen
  3902. Refresh, z.B. nach 5 Sekunden.
  3903. </li><br>
  3904. <a name="reverseLogs"></a>
  3905. <li>reverseLogs<br>
  3906. Damit wird das Logfile umsortiert, die neuesten Eintr&auml;ge stehen
  3907. oben. Der Vorteil ist, dass man nicht runterscrollen muss um den
  3908. neuesten Eintrag zu sehen, der Nachteil dass FHEM damit deutlich mehr
  3909. Hauptspeicher ben&ouml;tigt, etwa 6 mal so viel, wie das Logfile auf
  3910. dem Datentr&auml;ger gro&szlig; ist. Das kann auf Systemen mit wenig
  3911. Speicher (FRITZ!Box) zum Terminieren des FHEM Prozesses durch das
  3912. Betriebssystem f&uuml;hren.
  3913. </li><br>
  3914. <a name="roomIcons"></a>
  3915. <li>roomIcons<br>
  3916. Leerzeichen getrennte Liste von room:icon Zuordnungen
  3917. Der erste Teil wird als regexp interpretiert, daher muss ein
  3918. Leerzeichen als Punkt geschrieben werden. Beispiel:<br>
  3919. attr WEB roomIcons Anlagen.EDV:icoEverything
  3920. </li><br>
  3921. <a name="sortby"></a>
  3922. <li>sortby<br>
  3923. Der Wert dieses Attributs wird zum sortieren von Ger&auml;ten in
  3924. R&auml;umen verwendet, sonst w&auml;re es der Alias oder, wenn keiner
  3925. da ist, der Ger&auml;tename selbst. Falls der Wert des sortby
  3926. Attributes in {} eingeschlossen ist, dann wird er als ein perl Ausdruck
  3927. evaluiert. $NAME wird auf dem Ger&auml;tenamen gesetzt.
  3928. </li><br>
  3929. <a name="showUsedFiles"></a>
  3930. <li>showUsedFiles<br>
  3931. Zeige nur die verwendeten Dateien in der "Edit files" Abschnitt.
  3932. Achtung: aktuell ist das nur f&uuml;r den "Gplot files" Abschnitt
  3933. implementiert.
  3934. </li>
  3935. <br>
  3936. <a name="sortRooms"></a>
  3937. <li>sortRooms<br>
  3938. Durch Leerzeichen getrennte Liste von R&auml;umen, um deren Reihenfolge
  3939. zu definieren.
  3940. Da die R&auml;ume in diesem Attribut als Regexp interpretiert werden,
  3941. sind Leerzeichen im Raumnamen als Punkt (.) zu hinterlegen.
  3942. Beispiel:<br>
  3943. attr WEB sortRooms DG OG EG Keller
  3944. </li><br>
  3945. <a name="smallscreenCommands"></a>
  3946. <li>smallscreenCommands<br>
  3947. Falls auf 1 gesetzt werden Kommandos, Slider und Dropdown Men&uuml;s im
  3948. Smallscreen Landscape Modus angezeigt.
  3949. </li><br>
  3950. <li>sslVersion<br>
  3951. Siehe das global Attribut sslVersion.
  3952. </li><br>
  3953. <a name="stylesheetPrefix"></a>
  3954. <li>stylesheetPrefix<br>
  3955. Pr&auml;fix f&uuml;r die Dateien style.css, svg_style.css und
  3956. svg_defs.svg. Wenn die Datei mit dem Pr&auml;fix fehlt, wird die Default
  3957. Datei (ohne Pr&auml;fix) verwendet. Diese Dateien m&uuml;ssen im FHEM
  3958. Ordner liegen und k&ouml;nnen direkt mit "Select style" im FHEMWEB
  3959. Men&uuml;eintrag ausgew&auml;hlt werden. Beispiel:
  3960. <ul>
  3961. attr WEB stylesheetPrefix dark<br>
  3962. <br>
  3963. Referenzdateien:<br>
  3964. <ul>
  3965. darksvg_defs.svg<br>
  3966. darksvg_style.css<br>
  3967. darkstyle.css<br>
  3968. </ul>
  3969. <br>
  3970. </ul>
  3971. <b>Anmerkung:</b>Wenn der Parametername smallscreen oder touchpad
  3972. enth&auml;lt, wird FHEMWEB das Layout/den Zugriff f&uuml;r entsprechende
  3973. Ger&auml;te (Smartphones oder Touchpads) optimieren<br>
  3974. Standardm&auml;&szlig;ig werden 3 FHEMWEB Instanzen aktiviert: Port 8083
  3975. f&uuml;r Desktop Browser, Port 8084 f&uuml;r Smallscreen, und 8085
  3976. f&uuml;r Touchpad.<br>
  3977. Wenn touchpad oder smallscreen benutzt werden, wird WebApp support
  3978. aktiviert: Nachdem Sie eine Seite am iPhone oder iPad mit Safari
  3979. angesehen haben, k&ouml;nnen Sie einen Link auf den Homescreen anlegen um
  3980. die Seite im Fullscreen Modus zu sehen. Links werden in diesem Modus
  3981. anders gerendert, um ein "Zur&uuml;ckfallen" in den "normalen" Browser zu
  3982. verhindern.
  3983. </li><br>
  3984. <a name="SVGcache"></a>
  3985. <li>SVGcache<br>
  3986. Plots die sich nicht mehr &auml;ndern, werden im SVGCache Verzeichnis
  3987. (www/SVGcache) gespeichert, um die erneute, rechenintensive
  3988. Berechnung der Grafiken zu vermeiden. Default ist 0, d.h. aus.<br>
  3989. Siehe den clearSvgCache Befehl um diese Daten zu l&ouml;schen.
  3990. </li><br>
  3991. <a name="title"></a>
  3992. <li>title<br>
  3993. Setzt den Titel der Seite. Falls in {} eingeschlossen, dann wird es
  3994. als Perl Ausdruck evaluiert.
  3995. </li><br>
  3996. <a name="viewport"></a>
  3997. <li>viewport<br>
  3998. Setzt das &quot;viewport&quot; Attribut im HTML Header. Das kann benutzt
  3999. werden um z.B. die Breite fest vorzugeben oder Zoomen zu verhindern.<br>
  4000. Beispiel: attr WEB viewport
  4001. width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no
  4002. </li><br>
  4003. <a name="webCmd"></a>
  4004. <li>webCmd<br>
  4005. Durch Doppelpunkte getrennte Auflistung von Befehlen, die f&uuml;r ein
  4006. bestimmtes Ger&auml;t gelten sollen. Funktioniert nicht mit
  4007. smallscreen, ein Ersatz daf&uuml;r ist der devStateIcon Befehl.<br>
  4008. Beispiel:
  4009. <ul>
  4010. attr lamp webCmd on:off:on-for-timer 10<br>
  4011. </ul>
  4012. <br>
  4013. Der erste angegebene Befehl wird in der "set device ?" list
  4014. nachgeschlagen (Siehe das <a href="#setList">setList</a> Attrib
  4015. f&uuml;r Dummy Ger&auml;te). Wenn <b>dort</b> bekannte Modifier sind,
  4016. wird ein anderes Widget angezeigt. Siehe auch widgetOverride.<br>
  4017. Wenn der Befehl state ist, wird der Wert als Kommando interpretiert.<br>
  4018. Beispiele:
  4019. <ul>
  4020. define d1 dummy<br>
  4021. attr d1 webCmd state<br>
  4022. attr d1 setList state:on,off<br>
  4023. define d2 dummy<br>
  4024. attr d2 webCmd state<br>
  4025. attr d2 setList state:slider,0,1,10<br>
  4026. define d3 dummy<br>
  4027. attr d3 webCmd state<br>
  4028. attr d3 setList state:time<br>
  4029. </ul>
  4030. Anmerkung: dies ist ein Attribut f&uuml;r das anzuzeigende Ger&auml;t,
  4031. nicht f&uuml;r die FHEMWEBInstanz.
  4032. </li><br>
  4033. <a name="webCmdLabel"></a>
  4034. <li>webCmdLabel<br>
  4035. Durch Doppelpunkte getrennte Auflistung von Texten, die vor dem
  4036. jeweiligen webCmd angezeigt werden. Der Anzahl der Texte muss exakt den
  4037. Anzahl der webCmds entsprechen. Um mehrzeilige Anzeige zu realisieren,
  4038. kann ein Return nach dem Text und vor dem Doppelpunkt eingefuehrt
  4039. werden.</li><br>
  4040. <a name="webname"></a>
  4041. <li>webname<br>
  4042. Der Pfad nach http://hostname:port/ . Standard ist fhem,
  4043. so ist die Standard HTTP Adresse http://localhost:8083/fhem
  4044. </li><br>
  4045. <a name="widgetOverride"></a>
  4046. <li>widgetOverride<br>
  4047. Leerzeichen separierte Liste von Name/Modifier Paaren, mit dem man den
  4048. vom Modulautor f&uuml;r einen bestimmten Parameter (Set/Get/Attribut)
  4049. vorgesehene Widgets &auml;ndern kann. Folgendes ist die Liste der
  4050. bekannten Modifier:
  4051. <ul>
  4052. <!-- INSERT_DOC_FROM: www/pgm2/fhemweb.*.js -->
  4053. </ul></li>
  4054. </ul>
  4055. </ul>
  4056. =end html_DE
  4057. =cut