pla.php 290 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978
  1. <?php
  2. if (!isset($_COOKIE["username"])) {
  3. exit;
  4. }
  5. //
  6. // Project: phpLiteAdmin (http://www.phpliteadmin.org/)
  7. // Version: 1.9.7.1
  8. // Summary: PHP-based admin tool to manage SQLite2 and SQLite3 databases on the web
  9. // Last updated: 2016-12-14
  10. // Developers:
  11. // Dane Iracleous (daneiracleous@gmail.com)
  12. // Ian Aldrighetti (ian.aldrighetti@gmail.com)
  13. // George Flanagin & Digital Gaslight, Inc (george@digitalgaslight.com)
  14. // Christopher Kramer (crazy4chrissi@gmail.com, http://en.christosoft.de)
  15. // Ayman Teryaki (http://havalite.com)
  16. // Dreadnaut (dreadnaut@gmail.com, http://dreadnaut.altervista.org)
  17. //
  18. //
  19. // Copyright (C) 2016, phpLiteAdmin
  20. //
  21. // This program is free software: you can redistribute it and/or modify
  22. // it under the terms of the GNU General Public License as published by
  23. // the Free Software Foundation, either version 3 of the License, or
  24. // (at your option) any later version.
  25. //
  26. // This program is distributed in the hope that it will be useful,
  27. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. // GNU General Public License for more details.
  30. //
  31. // You should have received a copy of the GNU General Public License
  32. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  33. //
  34. // ////////////////////////////////////////////////////////////////////////
  35. //
  36. // Please report any bugs you may encounter to our issue tracker here:
  37. // https://bitbucket.org/phpliteadmin/public/issues?status=new&status=open
  38. //
  39. // This is sample configuration file
  40. //
  41. // You can configure phpliteadmin in one of 2 ways:
  42. // 1. Rename phpliteadmin.config.sample.php to phpliteadmin.config.php and change parameters in there.
  43. // You can set only your custom settings in phpliteadmin.config.php. All other settings will be set to defaults.
  44. // 2. Change parameters directly in main phpliteadmin.php file
  45. //
  46. // Please see https://bitbucket.org/phpliteadmin/public/wiki/Configuration for more details
  47. //password to gain access
  48. $password = 'admin';
  49. //directory relative to this file to search for databases (if false, manually list databases in the $databases variable)
  50. $directory = '.';
  51. //whether or not to scan the subdirectories of the above directory infinitely deep
  52. $subdirectories = false;
  53. //if the above $directory variable is set to false, you must specify the databases manually in an array as the next variable
  54. //if any of the databases do not exist as they are referenced by their path, they will be created automatically
  55. $databases = array(
  56. array(
  57. 'path' => 'database1.sqlite',
  58. 'name' => 'Database 1',
  59. ),
  60. array(
  61. 'path' => 'database2.sqlite',
  62. 'name' => 'Database 2',
  63. ),
  64. );
  65. /* ---- Interface settings ---- */
  66. // Theme! If you want to change theme, save the CSS file in same folder of phpliteadmin or in folder "themes"
  67. $theme = 'phpliteadmin.css';
  68. // the default language! If you want to change it, save the language file in same folder of phpliteadmin or in folder "languages"
  69. // More about localizations (downloads, how to translate etc.): https://bitbucket.org/phpliteadmin/public/wiki/Localization
  70. $language = 'en';
  71. // set default number of rows. You need to relog after changing the number
  72. $rowsNum = 30;
  73. // reduce string characters by a number bigger than 10
  74. $charsNum = 300;
  75. // maximum number of SQL queries to save in the history
  76. $maxSavedQueries = 10;
  77. /* ---- Custom functions ---- */
  78. //a list of custom functions that can be applied to columns in the databases
  79. //make sure to define every function below if it is not a core PHP function
  80. $custom_functions = array(
  81. 'md5', 'sha1', 'time', 'strtotime',
  82. // add the names of your custom functions to this array
  83. /* 'leet_text', */
  84. );
  85. // define your custom functions here
  86. /*
  87. function leet_text($value)
  88. {
  89. return strtr($value, 'eaAsSOl', '344zZ01');
  90. }
  91. */
  92. /* ---- Advanced options ---- */
  93. //changing the following variable allows multiple phpLiteAdmin installs to work under the same domain.
  94. $cookie_name = 'pla3412';
  95. //whether or not to put the app in debug mode where errors are outputted
  96. $debug = false;
  97. // the user is allowed to create databases with only these extensions
  98. $allowed_extensions = array('db', 'db3', 'sqlite', 'sqlite3');
  99. // English language-texts.
  100. // Read our wiki on how to translate: https://bitbucket.org/phpliteadmin/public/wiki/Localization
  101. $lang = array(
  102. "direction" => "LTR",
  103. "date_format" => 'g:ia \o\n F j, Y (T)', // see http://php.net/manual/en/function.date.php for what the letters stand for
  104. "ver" => "version",
  105. "for" => "for",
  106. "to" => "to",
  107. "go" => "Go",
  108. "yes" => "Yes",
  109. "no" => "No",
  110. "sql" => "SQL",
  111. "csv" => "CSV",
  112. "csv_tbl" => "Table that CSV pertains to",
  113. "srch" => "Search",
  114. "srch_again" => "Do Another Search",
  115. "login" => "Log In",
  116. "logout" => "Logout",
  117. "view" => "View",
  118. "confirm" => "Confirm",
  119. "cancel" => "Cancel",
  120. "save_as" => "Save As",
  121. "options" => "Options",
  122. "no_opt" => "No options",
  123. "help" => "Help",
  124. "installed" => "installed",
  125. "not_installed" => "not installed",
  126. "done" => "done",
  127. "insert" => "Insert",
  128. "export" => "Export",
  129. "import" => "Import",
  130. "rename" => "Rename",
  131. "empty" => "Empty",
  132. "drop" => "Drop",
  133. "tbl" => "Table",
  134. "chart" => "Chart",
  135. "err" => "ERROR",
  136. "act" => "Action",
  137. "rec" => "Records",
  138. "col" => "Column",
  139. "cols" => "Columns",
  140. "rows" => "row(s)",
  141. "edit" => "Edit",
  142. "del" => "Delete",
  143. "add" => "Add",
  144. "backup" => "Backup database file",
  145. "before" => "Before",
  146. "after" => "After",
  147. "passwd" => "Password",
  148. "passwd_incorrect" => "Incorrect password.",
  149. "chk_ext" => "Checking supported SQLite PHP extensions",
  150. "autoincrement" => "Autoincrement",
  151. "not_null" => "Not NULL",
  152. "attention" => "Attention",
  153. "none" => "None",
  154. "as_defined" => "As defined",
  155. "expression" => "Expression",
  156. "sqlite_ext" => "SQLite extension",
  157. "sqlite_ext_support" => "It appears that none of the supported SQLite library extensions are available in your installation of PHP. You may not use %s until you install at least one of them.",
  158. "sqlite_v" => "SQLite version",
  159. "sqlite_v_error" => "It appears that your database is of SQLite version %s but your installation of PHP does not contain the necessary extensions to handle this version. To fix the problem, either delete the database and allow %s to create it automatically or recreate it manually as SQLite version %s.",
  160. "report_issue" => "The problem cannot be diagnosed properly. Please file an issue report at",
  161. "sqlite_limit" => "Due to the limitations of SQLite, only the field name and data type can be modified.",
  162. "php_v" => "PHP version",
  163. "new_version" => "There is a new version!",
  164. "db_dump" => "database dump",
  165. "db_f" => "database file",
  166. "db_ch" => "Change Database",
  167. "db_event" => "Database Event",
  168. "db_name" => "Database name",
  169. "db_rename" => "Rename Database",
  170. "db_renamed" => "Database '%s' has been renamed to",
  171. "db_del" => "Delete Database",
  172. "db_path" => "Path to database",
  173. "db_size" => "Size of database",
  174. "db_mod" => "Database last modified",
  175. "db_create" => "Create New Database",
  176. "db_vac" => "The database, '%s', has been VACUUMed.",
  177. "db_not_writeable" => "The database, '%s', does not exist and cannot be created because the containing directory, '%s', is not writable. The application is unusable until you make it writable.",
  178. "db_setup" => "There was a problem setting up your database, %s. An attempt will be made to find out what's going on so you can fix the problem more easily",
  179. "db_exists" => "A database, other file or directory of the name '%s' already exists.",
  180. "exported" => "Exported",
  181. "struct" => "Structure",
  182. "struct_for" => "structure for",
  183. "on_tbl" => "on table",
  184. "data_dump" => "Data dump for",
  185. "backup_hint" => "Hint: To backup your database, the easiest way is to %s.",
  186. "backup_hint_linktext" => "download the database-file",
  187. "total_rows" => "a total of %s rows",
  188. "total" => "Total",
  189. "not_dir" => "The directory you specified to scan for databases does not exist or is not a directory.",
  190. "bad_php_directive" => "It appears that the PHP directive, 'register_globals' is enabled. This is bad. You need to disable it before continuing.",
  191. "page_gen" => "Page generated in %s seconds.",
  192. "powered" => "Powered by",
  193. "free_software" => "This is free software.",
  194. "please_donate" => "Please donate.",
  195. "remember" => "Remember me",
  196. "no_db" => "Welcome to %s. It appears that you have selected to scan a directory for databases to manage. However, %s could not find any valid SQLite databases. You may use the form below to create your first database.",
  197. "no_db2" => "The directory you specified does not contain any existing databases to manage, and the directory is not writable. This means you can't create any new databases using %s. Either make the directory writable or manually upload databases to the directory.",
  198. "create" => "Create",
  199. "created" => "has been created",
  200. "create_tbl" => "Create new table",
  201. "create_tbl_db" => "Create new table on database",
  202. "create_trigger" => "Creating new trigger on table",
  203. "create_index" => "Creating new index on table",
  204. "create_index1" => "Create Index",
  205. "create_view" => "Create new view on database",
  206. "trigger" => "Trigger",
  207. "triggers" => "Triggers",
  208. "trigger_name" => "Trigger name",
  209. "trigger_act" => "Trigger Action",
  210. "trigger_step" => "Trigger Steps (semicolon terminated)",
  211. "when_exp" => "WHEN expression (type expression without 'WHEN')",
  212. "index" => "Index",
  213. "indexes" => "Indexes",
  214. "index_name" => "Index name",
  215. "name" => "Name",
  216. "unique" => "Unique",
  217. "seq_no" => "Seq. No.",
  218. "emptied" => "has been emptied",
  219. "dropped" => "has been dropped",
  220. "renamed" => "has been renamed to",
  221. "altered" => "has been altered successfully",
  222. "inserted" => "inserted",
  223. "deleted" => "deleted",
  224. "affected" => "affected",
  225. "blank_index" => "Index name must not be blank.",
  226. "one_index" => "You must specify at least one index column.",
  227. "docu" => "Documentation",
  228. "license" => "License",
  229. "proj_site" => "Project Site",
  230. "bug_report" => "This may be a bug that needs to be reported at",
  231. "return" => "Return",
  232. "browse" => "Browse",
  233. "fld" => "Field",
  234. "fld_num" => "Number of Fields",
  235. "fields" => "Fields",
  236. "type" => "Type",
  237. "operator" => "Operator",
  238. "val" => "Value",
  239. "update" => "Update",
  240. "comments" => "Comments",
  241. "specify_fields" => "You must specify the number of table fields.",
  242. "specify_tbl" => "You must specify a table name.",
  243. "specify_col" => "You must specify a column.",
  244. "tbl_exists" => "Table of the same name already exists.",
  245. "show" => "Show",
  246. "show_rows" => "Showing %s row(s). ",
  247. "showing" => "Showing",
  248. "showing_rows" => "Showing rows",
  249. "query_time" => "(Query took %s sec)",
  250. "syntax_err" => "There is a problem with the syntax of your query (Query was not executed)",
  251. "run_sql" => "Run SQL query/queries on database '%s'",
  252. "recent_queries" => "Recent Queries",
  253. "full_texts" => "Show full texts",
  254. "no_full_texts" => "Shorten long texts",
  255. "ques_empty" => "Are you sure you want to empty the table '%s'?",
  256. "ques_drop" => "Are you sure you want to drop the table '%s'?",
  257. "ques_drop_view" => "Are you sure you want to drop the view '%s'?",
  258. "ques_del_rows" => "Are you sure you want to delete row(s) %s from table '%s'?",
  259. "ques_del_db" => "Are you sure you want to delete the database '%s'?",
  260. "ques_column_delete" => "Are you sure you want to delete column(s) %s from table '%s'?",
  261. "ques_del_index" => "Are you sure you want to delete index '%s'?",
  262. "ques_del_trigger" => "Are you sure you want to delete trigger '%s'?",
  263. "ques_primarykey_add" => "Are you sure you want to add a primary key for the column(s) %s in table '%s'?",
  264. "export_struct" => "Export with structure",
  265. "export_data" => "Export with data",
  266. "add_drop" => "Add DROP TABLE",
  267. "add_transact" => "Add TRANSACTION",
  268. "fld_terminated" => "Fields terminated by",
  269. "fld_enclosed" => "Fields enclosed by",
  270. "fld_escaped" => "Fields escaped by",
  271. "fld_names" => "Field names in first row",
  272. "rep_null" => "Replace NULL by",
  273. "rem_crlf" => "Remove CRLF characters within fields",
  274. "put_fld" => "Put field names in first row",
  275. "null_represent" => "NULL represented by",
  276. "import_suc" => "Import was successful.",
  277. "import_into" => "Import into",
  278. "import_f" => "File to import",
  279. "rename_tbl" => "Rename table '%s' to",
  280. "rows_records" => "row(s) starting from record # ",
  281. "rows_aff" => "row(s) affected. ",
  282. "as_a" => "as a",
  283. "readonly_tbl" => "'%s' is a view, which means it is a SELECT statement treated as a read-only table. You may not edit or insert records.",
  284. "chk_all" => "Check All",
  285. "unchk_all" => "Uncheck All",
  286. "with_sel" => "With Selected",
  287. "no_tbl" => "No table in database.",
  288. "no_chart" => "If you can read this, it means the chart could not be generated. The data you are trying to view may not be appropriate for a chart.",
  289. "no_rows" => "There are no rows in the table for the range you selected.",
  290. "no_sel" => "You did not select anything.",
  291. "chart_type" => "Chart Type",
  292. "chart_bar" => "Bar Chart",
  293. "chart_pie" => "Pie Chart",
  294. "chart_line" => "Line Chart",
  295. "lbl" => "Labels",
  296. "empty_tbl" => "This table is empty.",
  297. "click" => "Click here",
  298. "insert_rows" => "to insert rows.",
  299. "restart_insert" => "Restart insertion with ",
  300. "ignore" => "Ignore",
  301. "func" => "Function",
  302. "new_insert" => "Insert As New Row",
  303. "save_ch" => "Save Changes",
  304. "def_val" => "Default Value",
  305. "prim_key" => "Primary Key",
  306. "tbl_end" => "field(s) at end of table",
  307. "query_used_table" => "Query used to create this table",
  308. "query_used_view" => "Query used to create this view",
  309. "create_index2" => "Create an index on",
  310. "create_trigger2" => "Create a new trigger",
  311. "new_fld" => "Adding new field(s) to table '%s'",
  312. "add_flds" => "Add Fields",
  313. "edit_col" => "Editing column '%s'",
  314. "vac" => "Vacuum",
  315. "vac_desc" => "Large databases sometimes need to be VACUUMed to reduce their footprint on the server. Click the button below to VACUUM the database '%s'.",
  316. "event" => "Event",
  317. "each_row" => "For Each Row",
  318. "define_index" => "Define index properties",
  319. "dup_val" => "Duplicate values",
  320. "allow" => "Allowed",
  321. "not_allow" => "Not Allowed",
  322. "asc" => "Ascending",
  323. "desc" => "Descending",
  324. "warn0" => "You have been warned.",
  325. "warn_passwd" => "You are using the default password, which can be dangerous. You can change it easily at the top of %s.",
  326. "warn_dumbass" => "You didn't change the value dumbass ;-)",
  327. "counting_skipped" => "Counting of records has been skipped for some tables because your database is comparably big and some tables don't have primary keys assigned to them so counting might be slow. Add a primary key to these tables or %sforce counting%s.",
  328. "sel_state" => "Select Statement",
  329. "delimit" => "Delimiter",
  330. "back_top" => "Back to Top",
  331. "choose_f" => "Choose File",
  332. "instead" => "Instead of",
  333. "define_in_col" => "Define index column(s)",
  334. "delete_only_managed" => "You can only delete databases managed by this tool!",
  335. "rename_only_managed" => "You can only rename databases managed by this tool!",
  336. "db_moved_outside" => "You either tried to move the database into a directory where it cannot be managed anylonger, or the check if you did this failed because of missing rights.",
  337. "extension_not_allowed" => "The extension you provided is not within the list of allowed extensions. Please use one of the following extensions",
  338. "add_allowed_extension" => "You can add extensions to this list by adding your extension to \$allowed_extensions in the configuration.",
  339. "directory_not_writable" => "The database-file itself is writable, but to write into it, the containing directory needs to be writable as well. This is because SQLite puts temporary files in there for locking.",
  340. "tbl_inexistent" => "Table %s does not exist",
  341. // errors that can happen when ALTER TABLE fails. You don't necessarily have to translate these.
  342. "alter_failed" => "Altering of Table %s failed",
  343. "alter_tbl_name_not_replacable" => "could not replace the table name with the temporary one",
  344. "alter_no_def" => "no ALTER definition",
  345. "alter_parse_failed" => "failed to parse ALTER definition",
  346. "alter_action_not_recognized" => "ALTER action could not be recognized",
  347. "alter_no_add_col" => "no column to add detected in ALTER statement",
  348. "alter_pattern_mismatch" => "Pattern did not match on your original CREATE TABLE statement",
  349. "alter_col_not_recognized" => "could not recognize new or old column name",
  350. "alter_unknown_operation" => "Unknown ALTER operation!",
  351. /* Help documentation */
  352. "help_doc" => "Help Documentation",
  353. "help1" => "SQLite Library Extensions",
  354. "help1_x" => "%s uses PHP library extensions that allow interaction with SQLite databases. Currently, %s supports PDO, SQLite3, and SQLiteDatabase. Both PDO and SQLite3 deal with version 3 of SQLite, while SQLiteDatabase deals with version 2. So, if your PHP installation includes more than one SQLite library extension, PDO and SQLite3 will take precedence to make use of the better technology. However, if you have existing databases that are of version 2 of SQLite, %s will be forced to use SQLiteDatabase for only those databases. Not all databases need to be of the same version. During the database creation, however, the most advanced extension will be used.",
  355. "help2" => "Creating a New Database",
  356. "help2_x" => "When you create a new database, the name you entered will be appended with the appropriate file extension (.db, .db3, .sqlite, etc.) if you do not include it yourself. The database will be created in the directory you specified as the \$directory variable.",
  357. "help3" => "Tables vs. Views",
  358. "help3_x" => "On the main database page, there is a list of tables and views. Since views are read-only, certain operations will be disabled. These disabled operations will be apparent by their omission in the location where they should appear on the row for a view. If you want to change the data for a view, you need to drop that view and create a new view with the appropriate SELECT statement that queries other existing tables. For more information, see <a href='http://en.wikipedia.org/wiki/View_(database)' target='_blank'>http://en.wikipedia.org/wiki/View_(database)</a>",
  359. "help4" => "Writing a Select Statement for a New View",
  360. "help4_x" => "When you create a new view, you must write an SQL SELECT statement that it will use as its data. A view is simply a read-only table that can be accessed and queried like a regular table, except it cannot be modified through insertion, column editing, or row editing. It is only used for conveniently fetching data.",
  361. "help5" => "Export Structure to SQL File",
  362. "help5_x" => "During the process for exporting to an SQL file, you may choose to include the queries that create the table and columns.",
  363. "help6" => "Export Data to SQL File",
  364. "help6_x" => "During the process for exporting to an SQL file, you may choose to include the queries that populate the table(s) with the current records of the table(s).",
  365. "help7" => "Add Drop Table to Exported SQL File",
  366. "help7_x" => "During the process for exporting to an SQL file, you may choose to include queries to DROP the existing tables before adding them so that problems do not occur when trying to create tables that already exist.",
  367. "help8" => "Add Transaction to Exported SQL File",
  368. "help8_x" => "During the process for exporting to an SQL file, you may choose to wrap the queries around a TRANSACTION so that if an error occurs at any time during the importation process using the exported file, the database can be reverted to its previous state, preventing partially updated data from populating the database.",
  369. "help9" => "Add Comments to Exported SQL File",
  370. "help9_x" => "During the process for exporting to an SQL file, you may choose to include comments that explain each step of the process so that a human can better understand what is happening.",
  371. "help10" => "Partial Indexes",
  372. "help10_x" => "Partial indexes are indexes over a subset of the rows of a table specified by a WHERE clause. Note this requires at least SQLite 3.8.0 and database files with partial indexes won't be readable or writable by older versions. See the <a href='https://www.sqlite.org/partialindex.html' target='_blank'>SQLite documentation.</a>",
  373. );
  374. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  375. //there is no reason for the average user to edit anything below this comment
  376. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  377. //- Initialization
  378. // load optional configuration file
  379. $config_filename = './phpliteadmin.config.php';
  380. if (is_readable($config_filename)) {
  381. include_once $config_filename;
  382. }
  383. //constants 1
  384. define("PROJECT", "phpLiteAdmin");
  385. define("VERSION", "1.9.7.1");
  386. define("PAGE", basename(__FILE__));
  387. define("FORCETYPE", false); //force the extension that will be used (set to false in almost all circumstances except debugging)
  388. define("SYSTEMPASSWORD", $password); // Makes things easier.
  389. define('PROJECT_URL', 'http://www.phpliteadmin.org/');
  390. define('DONATE_URL', 'http://www.phpliteadmin.org/donate/');
  391. define('VERSION_CHECK_URL', 'https://www.phpliteadmin.org/current_version.php');
  392. define('PROJECT_BUGTRACKER_LINK', '<a href="https://bitbucket.org/phpliteadmin/public/issues?status=new&status=open" target="_blank">https://bitbucket.org/phpliteadmin/public/issues?status=new&status=open</a>');
  393. define('PROJECT_INSTALL_LINK', '<a href="https://bitbucket.org/phpliteadmin/public/wiki/Installation" target="_blank">https://bitbucket.org/phpliteadmin/public/wiki/Installation</a>');
  394. // Resource output (css and javascript files)
  395. // we get out of the main code as soon as possible, without inizializing the session
  396. if (isset($_GET['resource'])) {
  397. Resources::output($_GET['resource']);
  398. exit();
  399. }
  400. // don't mess with this - required for the login session
  401. ini_set('session.cookie_httponly', '1');
  402. session_start();
  403. // generate CSRF token
  404. if (empty($_SESSION['token'])) {
  405. if (function_exists('mcrypt_create_iv')) {
  406. $_SESSION['token'] = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
  407. } else {
  408. $_SESSION['token'] = bin2hex(openssl_random_pseudo_bytes(32));
  409. }
  410. }
  411. $token = $_SESSION['token'];
  412. $token_html = '<input type="hidden" name="token" value="' . $token . '" />';
  413. // checking CSRF token
  414. if ($_SERVER['REQUEST_METHOD'] === 'POST' || isset($_GET['download'])) // all POST forms need tokens! downloads are protected as well
  415. {
  416. if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['token'])) {
  417. $check_token = $_POST['token'];
  418. } elseif ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['token'])) {
  419. $check_token = $_GET['token'];
  420. }
  421. if (!isset($check_token)) {
  422. die("CSRF token missing");
  423. } elseif ((function_exists('hash_equals') && !hash_equals($_SESSION['token'], $check_token)) ||
  424. (!function_exists('hash_equals') && $_SESSION['token'] !== $check_token)) // yes, timing attacks might be possible here. update your php ;)
  425. {
  426. die("CSRF token is wrong - please try to login again");
  427. }
  428. }
  429. if ($debug == true) {
  430. ini_set("display_errors", 1);
  431. error_reporting(E_STRICT | E_ALL);
  432. } else {
  433. @ini_set("display_errors", 0);
  434. }
  435. // start the timer to record page load time
  436. $pageTimer = new MicroTimer();
  437. // load language file
  438. if ($language != 'en') {
  439. $temp_lang = $lang;
  440. if (is_file('languages/lang_' . $language . '.php')) {
  441. include 'languages/lang_' . $language . '.php';
  442. } elseif (is_file('lang_' . $language . '.php')) {
  443. include 'lang_' . $language . '.php';
  444. }
  445. $lang = array_merge($temp_lang, $lang);
  446. unset($temp_lang);
  447. }
  448. // version-number added so after updating, old session-data is not used anylonger
  449. // cookies names cannot contain symbols, except underscores
  450. define("COOKIENAME", preg_replace('/[^a-zA-Z0-9_]/', '_', $cookie_name . '_' . VERSION));
  451. // stripslashes if MAGIC QUOTES is turned on
  452. // This is only a workaround. Please better turn off magic quotes!
  453. // This code is from http://php.net/manual/en/security.magicquotes.disabling.php
  454. if (get_magic_quotes_gpc()) {
  455. $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
  456. while (list($key, $val) = each($process)) {
  457. foreach ($val as $k => $v) {
  458. unset($process[$key][$k]);
  459. if (is_array($v)) {
  460. $process[$key][stripslashes($k)] = $v;
  461. $process[] = &$process[$key][stripslashes($k)];
  462. } else {
  463. $process[$key][stripslashes($k)] = stripslashes($v);
  464. }
  465. }
  466. }
  467. unset($process);
  468. }
  469. //data types array
  470. $sqlite_datatypes = array("INTEGER", "REAL", "TEXT", "BLOB", "NUMERIC", "BOOLEAN", "DATETIME");
  471. //available SQLite functions array (don't add anything here or there will be problems)
  472. $sqlite_functions = array("abs", "hex", "length", "lower", "ltrim", "random", "round", "rtrim", "trim", "typeof", "upper");
  473. //- Support functions
  474. //function that allows SQL delimiter to be ignored inside comments or strings
  475. function explode_sql($delimiter, $sql)
  476. {
  477. $ign = array('"' => '"', "'" => "'", "/*" => "*/", "--" => "\n"); // Ignore sequences.
  478. $out = array();
  479. $last = 0;
  480. $slen = strlen($sql);
  481. $dlen = strlen($delimiter);
  482. $i = 0;
  483. while ($i < $slen) {
  484. // Split on delimiter
  485. if ($slen - $i >= $dlen && substr($sql, $i, $dlen) == $delimiter) {
  486. array_push($out, substr($sql, $last, $i - $last));
  487. $last = $i + $dlen;
  488. $i += $dlen;
  489. continue;
  490. }
  491. // Eat comments and string literals
  492. foreach ($ign as $start => $end) {
  493. $ilen = strlen($start);
  494. if ($slen - $i >= $ilen && substr($sql, $i, $ilen) == $start) {
  495. $i += strlen($start);
  496. $elen = strlen($end);
  497. while ($i < $slen) {
  498. if ($slen - $i >= $elen && substr($sql, $i, $elen) == $end) {
  499. // SQL comment characters can be escaped by doubling the character. This recognizes and skips those.
  500. if ($start == $end && $slen - $i >= $elen * 2 && substr($sql, $i, $elen * 2) == $end . $end) {
  501. $i += $elen * 2;
  502. continue;
  503. } else {
  504. $i += $elen;
  505. continue 3;
  506. }
  507. }
  508. $i++;
  509. }
  510. continue 2;
  511. }
  512. }
  513. $i++;
  514. }
  515. if ($last < $slen) {
  516. array_push($out, substr($sql, $last, $slen - $last));
  517. }
  518. return $out;
  519. }
  520. //function to scan entire directory tree and subdirectories
  521. function dir_tree($dir)
  522. {
  523. $path = '';
  524. $stack[] = $dir;
  525. while ($stack) {
  526. $thisdir = array_pop($stack);
  527. if ($dircont = scandir($thisdir)) {
  528. $i = 0;
  529. while (isset($dircont[$i])) {
  530. if ($dircont[$i] !== '.' && $dircont[$i] !== '..') {
  531. $current_file = $thisdir . DIRECTORY_SEPARATOR . $dircont[$i];
  532. if (is_file($current_file)) {
  533. $path[] = $thisdir . DIRECTORY_SEPARATOR . $dircont[$i];
  534. } elseif (is_dir($current_file)) {
  535. $path[] = $thisdir . DIRECTORY_SEPARATOR . $dircont[$i];
  536. $stack[] = $current_file;
  537. }
  538. }
  539. $i++;
  540. }
  541. }
  542. }
  543. return $path;
  544. }
  545. //the function echo the help [?] links to the documentation
  546. function helpLink($name)
  547. {
  548. global $lang;
  549. return "<a href='?help=1' onclick='openHelp(\"" . $name . "\"); return false;' class='helpq' title='" . $lang['help'] . ": " . $name . "' target='_blank'><span>[?]</span></a>";
  550. }
  551. // function to encode value into HTML just like htmlentities, but with adjusted default settings
  552. function htmlencode($value, $flags = ENT_QUOTES, $encoding = "UTF-8")
  553. {
  554. return htmlentities($value, $flags, $encoding);
  555. }
  556. // 22 August 2011: gkf added this function to support display of
  557. // default values in the form used to INSERT new data.
  558. function deQuoteSQL($s)
  559. {
  560. return trim(trim($s), "'");
  561. }
  562. // reduce string chars
  563. function subString($str)
  564. {
  565. global $charsNum;
  566. if ($charsNum > 10 && (!isset($_SESSION[COOKIENAME . 'fulltexts']) || !$_SESSION[COOKIENAME . 'fulltexts']) && strlen($str) > $charsNum) {
  567. $str = substr($str, 0, $charsNum) . '...';
  568. }
  569. return $str;
  570. }
  571. // checks the (new) name of a database file
  572. function checkDbName($name)
  573. {
  574. global $allowed_extensions;
  575. $info = pathinfo($name);
  576. if (isset($info['extension']) && !in_array($info['extension'], $allowed_extensions)) {
  577. return false;
  578. } else {
  579. return (!is_file($name) && !is_dir($name));
  580. }
  581. }
  582. // check whether a path is a db managed by this tool
  583. // requires that $databases is already filled!
  584. // returns the key of the db if managed, false otherwise.
  585. function isManagedDB($path)
  586. {
  587. global $databases;
  588. foreach ($databases as $db_key => $database) {
  589. if ($path == $database['path']) {
  590. // a db we manage. Thats okay.
  591. // return the key.
  592. return $db_key;
  593. }
  594. }
  595. // not a db we manage!
  596. return false;
  597. }
  598. // from a typename of a colun, get the type of the column's affinty
  599. // see http://www.sqlite.org/datatype3.html section 2.1 for rules
  600. function get_type_affinity($type)
  601. {
  602. if (preg_match("/INT/i", $type)) {
  603. return "INTEGER";
  604. } else if (preg_match("/(?:CHAR|CLOB|TEXT)/i", $type)) {
  605. return "TEXT";
  606. } else if (preg_match("/BLOB/i", $type) || $type == "") {
  607. return "NONE";
  608. } else if (preg_match("/(?:REAL|FLOA|DOUB)/i", $type)) {
  609. return "REAL";
  610. } else {
  611. return "NUMERIC";
  612. }
  613. }
  614. //- Check user authentication, login and logout
  615. $auth = new Authorization(); //create authorization object
  616. // check if user has attempted to log out
  617. if (isset($_POST['logout'])) {
  618. $auth->revoke();
  619. }
  620. // check if user has attempted to log in
  621. else if (isset($_POST['login']) && isset($_POST['password'])) {
  622. $auth->attemptGrant($_POST['password'], isset($_POST['remember']));
  623. }
  624. //- Actions on database files and bulk data
  625. if ($auth->isAuthorized()) {
  626. //- Create a new database
  627. if (isset($_POST['new_dbname'])) {
  628. if ($_POST['new_dbname'] == '') {
  629. // TODO: Display an error message (do NOT echo here. echo below in the html-body!)
  630. } else {
  631. $str = preg_replace('@[^\w-.]@', '', $_POST['new_dbname']);
  632. $dbname = $str;
  633. $dbpath = $str;
  634. if (checkDbName($dbname)) {
  635. $tdata = array();
  636. $tdata['name'] = $dbname;
  637. $tdata['path'] = $directory . DIRECTORY_SEPARATOR . $dbpath;
  638. if (isset($_POST['new_dbtype'])) {
  639. $tdata['type'] = $_POST['new_dbtype'];
  640. } else {
  641. $tdata['type'] = 3;
  642. }
  643. $td = new Database($tdata);
  644. $td->query("VACUUM");
  645. } else {
  646. if (is_file($dbname) || is_dir($dbname)) {
  647. $dbexists = true;
  648. } else {
  649. $extension_not_allowed = true;
  650. }
  651. }
  652. }
  653. }
  654. //- Scan a directory for databases
  655. if ($directory !== false) {
  656. if ($directory[strlen($directory) - 1] == DIRECTORY_SEPARATOR) //if user has a trailing slash in the directory, remove it
  657. {
  658. $directory = substr($directory, 0, strlen($directory) - 1);
  659. }
  660. if (is_dir($directory)) //make sure the directory is valid
  661. {
  662. if ($subdirectories === true) {
  663. $arr = dir_tree($directory);
  664. } else {
  665. $arr = scandir($directory);
  666. }
  667. $databases = array();
  668. $j = 0;
  669. for ($i = 0; $i < sizeof($arr); $i++) //iterate through all the files in the databases
  670. {
  671. if ($subdirectories === false) {
  672. $arr[$i] = $directory . DIRECTORY_SEPARATOR . $arr[$i];
  673. }
  674. if (@!is_file($arr[$i])) {
  675. continue;
  676. }
  677. $con = file_get_contents($arr[$i], null, null, 0, 60);
  678. if (strpos($con, "** This file contains an SQLite 2.1 database **", 0) !== false || strpos($con, "SQLite format 3", 0) !== false) {
  679. $databases[$j]['path'] = $arr[$i];
  680. if ($subdirectories === false) {
  681. $databases[$j]['name'] = basename($arr[$i]);
  682. } else {
  683. $databases[$j]['name'] = $arr[$i];
  684. }
  685. $databases[$j]['writable'] = is_writable($databases[$j]['path']);
  686. $databases[$j]['writable_dir'] = is_writable(dirname($databases[$j]['path']));
  687. $databases[$j]['readable'] = is_readable($databases[$j]['path']);
  688. $j++;
  689. }
  690. }
  691. // 22 August 2011: gkf fixed bug #50.
  692. sort($databases);
  693. if (isset($tdata)) {
  694. foreach ($databases as $db_id => $database) {
  695. if ($database['path'] == $tdata['path']) {
  696. $_SESSION[COOKIENAME . 'currentDB'] = $database;
  697. break;
  698. }
  699. }
  700. }
  701. } else //the directory is not valid - display error and exit
  702. {
  703. echo "<div class='confirm' style='margin:20px;'>" . $lang['not_dir'] . "</div>";
  704. exit();
  705. }
  706. } else {
  707. for ($i = 0; $i < sizeof($databases); $i++) {
  708. if (!file_exists($databases[$i]['path'])) {
  709. // the file does not exist and will be created when clicked, if permissions allow to
  710. $databases[$i]['writable'] = is_writable(dirname($databases[$i]['path']));
  711. $databases[$i]['writable_dir'] = is_writable(dirname($databases[$i]['path']));
  712. $databases[$i]['readable'] = is_writable(dirname($databases[$i]['path']));
  713. } else {
  714. $databases[$i]['writable'] = is_writable($databases[$i]['path']);
  715. $databases[$i]['writable_dir'] = is_writable(dirname($databases[$i]['path']));
  716. $databases[$i]['readable'] = is_readable($databases[$i]['path']);
  717. }
  718. }
  719. sort($databases);
  720. }
  721. // we now have the $databases array set. Check whethet currentDB is a managed Db (is in this array)
  722. if (isset($_SESSION[COOKIENAME . 'currentDB']) && isManagedDB($_SESSION[COOKIENAME . 'currentDB']['path']) === false) {
  723. unset($_SESSION[COOKIENAME . 'currentDB']);
  724. }
  725. //- Delete an existing database
  726. if (isset($_GET['database_delete'])) {
  727. $dbpath = $_POST['database_delete'];
  728. // check whether $dbpath really is a db we manage
  729. $checkDB = isManagedDB($dbpath);
  730. if ($checkDB !== false) {
  731. unlink($dbpath);
  732. unset($_SESSION[COOKIENAME . 'currentDB']);
  733. unset($databases[$checkDB]);
  734. } else {
  735. die($lang['err'] . ': ' . $lang['delete_only_managed']);
  736. }
  737. }
  738. //- Rename an existing database
  739. if (isset($_GET['database_rename'])) {
  740. $oldpath = $_POST['oldname'];
  741. $newpath = $_POST['newname'];
  742. $oldpath_parts = pathinfo($oldpath);
  743. $newpath_parts = pathinfo($newpath);
  744. // only rename?
  745. $newpath = $oldpath_parts['dirname'] . DIRECTORY_SEPARATOR . basename($_POST['newname']);
  746. if ($newpath != $_POST['newname'] && $subdirectories) {
  747. // it seems that the file should not only be renamed but additionally moved.
  748. // we need to make sure it stays within $directory...
  749. $new_realpath = realpath($newpath_parts['dirname']) . DIRECTORY_SEPARATOR;
  750. $directory_realpath = realpath($directory) . DIRECTORY_SEPARATOR;
  751. if (strpos($new_realpath, $directory_realpath) === 0) {
  752. // its okay, the new directory is within $directory
  753. $newpath = $_POST['newname'];
  754. } else {
  755. die($lang['err'] . ': ' . $lang['db_moved_outside']);
  756. }
  757. }
  758. if (checkDbName($newpath)) {
  759. $checkDB = isManagedDB($oldpath);
  760. if ($checkDB !== false) {
  761. rename($oldpath, $newpath);
  762. $databases[$checkDB]['path'] = $newpath;
  763. $databases[$checkDB]['name'] = basename($newpath);
  764. $_SESSION[COOKIENAME . 'currentDB'] = $databases[$checkDB];
  765. $justrenamed = true;
  766. } else {
  767. die($lang['err'] . ': ' . $lang['rename_only_managed']);
  768. }
  769. } else {
  770. if (is_file($newpath) || is_dir($newpath)) {
  771. $dbexists = true;
  772. } else {
  773. $extension_not_allowed = true;
  774. }
  775. }
  776. }
  777. //- Export (download a dump) an existing database
  778. if (isset($_POST['export'])) {
  779. $export_filename = str_replace(array("\r", "\n"), '', $_POST['filename']); // against http header injection (php < 5.1.2 only)
  780. if ($_POST['export_type'] == "sql") {
  781. header('Content-Type: text/sql');
  782. header('Content-Disposition: attachment; filename="' . $export_filename . '.' . $_POST['export_type'] . '";');
  783. if (isset($_POST['tables'])) {
  784. $tables = $_POST['tables'];
  785. } else {
  786. $tables = array();
  787. $tables[0] = $_POST['single_table'];
  788. }
  789. $drop = isset($_POST['drop']);
  790. $structure = isset($_POST['structure']);
  791. $data = isset($_POST['data']);
  792. $transaction = isset($_POST['transaction']);
  793. $comments = isset($_POST['comments']);
  794. $db = new Database($_SESSION[COOKIENAME . 'currentDB']);
  795. echo $db->export_sql($tables, $drop, $structure, $data, $transaction, $comments);
  796. } else if ($_POST['export_type'] == "csv") {
  797. header("Content-type: application/csv");
  798. header('Content-Disposition: attachment; filename="' . $export_filename . '.' . $_POST['export_type'] . '";');
  799. header("Pragma: no-cache");
  800. header("Expires: 0");
  801. if (isset($_POST['tables'])) {
  802. $tables = $_POST['tables'];
  803. } else {
  804. $tables = array();
  805. $tables[0] = $_POST['single_table'];
  806. }
  807. $field_terminate = $_POST['export_csv_fieldsterminated'];
  808. $field_enclosed = $_POST['export_csv_fieldsenclosed'];
  809. $field_escaped = $_POST['export_csv_fieldsescaped'];
  810. $null = $_POST['export_csv_replacenull'];
  811. $crlf = isset($_POST['export_csv_crlf']);
  812. $fields_in_first_row = isset($_POST['export_csv_fieldnames']);
  813. $db = new Database($_SESSION[COOKIENAME . 'currentDB']);
  814. echo $db->export_csv($tables, $field_terminate, $field_enclosed, $field_escaped, $null, $crlf, $fields_in_first_row);
  815. }
  816. exit();
  817. }
  818. //- Import a file into an existing database
  819. if (isset($_POST['import'])) {
  820. $db = new Database($_SESSION[COOKIENAME . 'currentDB']);
  821. $db->registerUserFunction($custom_functions);
  822. if ($_POST['import_type'] == "sql") {
  823. $data = file_get_contents($_FILES["file"]["tmp_name"]);
  824. $importSuccess = $db->import_sql($data);
  825. } else {
  826. $field_terminate = $_POST['import_csv_fieldsterminated'];
  827. $field_enclosed = $_POST['import_csv_fieldsenclosed'];
  828. $field_escaped = $_POST['import_csv_fieldsescaped'];
  829. $null = $_POST['import_csv_replacenull'];
  830. $fields_in_first_row = isset($_POST['import_csv_fieldnames']);
  831. $importSuccess = $db->import_csv($_FILES["file"]["tmp_name"], $_POST['single_table'], $field_terminate, $field_enclosed, $field_escaped, $null, $fields_in_first_row);
  832. }
  833. }
  834. //- Download (backup) a database file (as SQLite file, not as dump)
  835. if (isset($_GET['download']) && isManagedDB($_GET['download']) !== false) {
  836. header("Content-type: application/octet-stream");
  837. header('Content-Disposition: attachment; filename="' . basename($_GET['download']) . '";');
  838. header("Pragma: no-cache");
  839. header("Expires: 0");
  840. readfile($_GET['download']);
  841. exit;
  842. }
  843. }
  844. //- HTML: output starts here
  845. header('Content-Type: text/html; charset=utf-8');
  846. ?>
  847. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  848. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  849. <head>
  850. <!-- Copyright <?php echo date("Y") . ' ' . PROJECT . ' (' . PROJECT_URL . ')'; ?> -->
  851. <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
  852. <link rel="shortcut icon" href="?resource=favicon" />
  853. <title><?php echo PROJECT ?></title>
  854. <?php
  855. //- HTML: css/theme include
  856. if (isset($_GET['theme'])) {
  857. $theme = basename($_GET['theme']);
  858. }
  859. // allow themes to be dropped in subfolder "themes"
  860. if (is_file('themes/' . $theme)) {
  861. $theme = 'themes/' . $theme;
  862. }
  863. if (file_exists($theme))
  864. // an external stylesheet exists - import it
  865. {
  866. echo "<link href='{$theme}' rel='stylesheet' type='text/css' />", PHP_EOL;
  867. } else
  868. // only use the default stylesheet if an external one does not exist
  869. {
  870. echo "<link href='?resource=css' rel='stylesheet' type='text/css' />", PHP_EOL;
  871. }
  872. // HTML: output help text, then exit
  873. if (isset($_GET['help'])) {
  874. //help section array
  875. $help = array
  876. (
  877. $lang['help1'] => sprintf($lang['help1_x'], PROJECT, PROJECT, PROJECT), $lang['help2'] => $lang['help2_x'], $lang['help3'] => $lang['help3_x'],
  878. $lang['help4'] => $lang['help4_x'], $lang['help5'] => $lang['help5_x'], $lang['help6'] => $lang['help6_x'],
  879. $lang['help7'] => $lang['help7_x'], $lang['help8'] => $lang['help8_x'], $lang['help9'] => $lang['help9_x'], $lang['help10'] => $lang['help10_x'],
  880. );
  881. ?>
  882. </head>
  883. <body style="direction:<?php echo $lang['direction']; ?>;">
  884. <div id='help_container'>
  885. <?php
  886. echo "<div class='help_list'>";
  887. echo "<span style='font-size:18px;'>" . PROJECT . " v" . VERSION . " " . $lang['help_doc'] . "</span><br/><br/>";
  888. foreach ((array) $help as $key => $val) {
  889. echo "<a href='#" . $key . "'>" . $key . "</a><br/>";
  890. }
  891. echo "</div>";
  892. echo "<br/><br/>";
  893. foreach ((array) $help as $key => $val) {
  894. echo "<div class='help_outer'>";
  895. echo "<a class='headd' name='" . $key . "'>" . $key . "</a>";
  896. echo "<div class='help_inner'>";
  897. echo $val;
  898. echo "</div>";
  899. echo "<a class='help_top' href='#top'>" . $lang['back_top'] . "</a>";
  900. echo "</div>";
  901. }
  902. ?>
  903. </div>
  904. </body>
  905. </html>
  906. <?php
  907. exit();
  908. }
  909. //- Javascript include
  910. ?>
  911. <!-- JavaScript Support -->
  912. <script type='text/javascript' src='?resource=javascript'></script>
  913. </head>
  914. <body style="direction:<?php echo $lang['direction']; ?>;">
  915. <?php
  916. if (ini_get("register_globals") == "on" || ini_get("register_globals") == "1") //check whether register_globals is turned on - if it is, we need to not continue
  917. {
  918. echo "<div class='confirm' style='margin:20px;'>" . $lang['bad_php_directive'] . "</div>";
  919. echo "</body></html>";
  920. exit();
  921. }
  922. //- HTML: login screen if not authorized, exit
  923. if (!$auth->isAuthorized()) {
  924. echo "<div id='loginBox'>";
  925. echo "<h1><span id='logo'>" . PROJECT . "</span> <span id='version'>v" . VERSION . "</span></h1>";
  926. echo "<div style='padding:15px; text-align:center;'>";
  927. if ($auth->isFailedLogin()) {
  928. echo "<span class='warning'>" . $lang['passwd_incorrect'] . "</span><br/><br/>";
  929. }
  930. echo "<form action='" . PAGE . "' method='post'>";
  931. echo $token_html;
  932. echo $lang['passwd'] . ": <input type='password' name='password'/><br/>";
  933. echo "<label><input type='checkbox' name='remember' value='yes' checked='checked'/> " . $lang['remember'] . "</label><br/><br/>";
  934. echo "<input type='submit' value='" . $lang['login'] . "' class='btn'/>";
  935. echo "<input type='hidden' name='login' value='true' />";
  936. echo "</form>";
  937. echo "</div>";
  938. echo "</div>";
  939. echo "<br/>";
  940. echo "<div style='text-align:center;'>";
  941. echo "<span style='font-size:11px;'>" . $lang['powered'] . " <a href='" . PROJECT_URL . "' target='_blank' style='font-size:11px;'>" . PROJECT . "</a> | ";
  942. printf($lang['page_gen'], $pageTimer);
  943. echo "</span></div>";
  944. echo "</body></html>";
  945. exit();
  946. }
  947. //- User is authorized, display the main application
  948. //- Select database (from session or first available)
  949. if (!isset($_SESSION[COOKIENAME . 'currentDB']) && count($databases) > 0) {
  950. //set the current database to the first existing one in the array (default)
  951. $_SESSION[COOKIENAME . 'currentDB'] = reset($databases);
  952. }
  953. if (sizeof($databases) > 0) {
  954. $currentDB = $_SESSION[COOKIENAME . 'currentDB'];
  955. } else // the database array is empty, offer to create a new database
  956. {
  957. //- HTML: form to create a new database, exit
  958. if ($directory !== false && is_writable($directory)) {
  959. echo "<div class='confirm' style='margin:20px;'>";
  960. printf($lang['no_db'], PROJECT, PROJECT);
  961. echo "</div>";
  962. if (isset($extension_not_allowed)) {
  963. echo "<div class='confirm' style='margin:10px 20px;'>";
  964. echo $lang['err'] . ': ' . $lang['extension_not_allowed'] . ': ';
  965. echo implode(', ', array_map('htmlencode', $allowed_extensions));
  966. echo '<br />' . $lang['add_allowed_extension'];
  967. echo "</div><br/>";
  968. }
  969. echo "<fieldset style='margin:15px;'><legend><b>" . $lang['db_create'] . "</b></legend>";
  970. echo "<form name='create_database' method='post' action='" . PAGE . "'>";
  971. echo $token_html;
  972. echo "<input type='text' name='new_dbname' style='width:150px;'/> ";
  973. if (class_exists('SQLiteDatabase') && (class_exists('SQLite3') || class_exists('PDO'))) {
  974. echo "<select name='new_dbtype' class='newDbType'>";
  975. echo "<option value='3'>SQLite 3</option>";
  976. echo "<option value='2'>SQLite 2</option>";
  977. echo "</select>";
  978. }
  979. echo "<input type='submit' value='" . $lang['create'] . "' class='btn'/>";
  980. echo "</form>";
  981. echo "</fieldset>";
  982. } else {
  983. echo "<div class='confirm' style='margin:20px;'>";
  984. echo $lang['err'] . ": " . sprintf($lang['no_db2'], PROJECT);
  985. echo "</div><br/>";
  986. }
  987. exit();
  988. }
  989. //- Switch to a different database with drop-down menu
  990. if (isset($_POST['database_switch'])) {
  991. foreach ($databases as $db_id => $database) {
  992. if ($database['path'] == $_POST['database_switch']) {
  993. $_SESSION[COOKIENAME . "currentDB"] = $database;
  994. break;
  995. }
  996. }
  997. $currentDB = $_SESSION[COOKIENAME . 'currentDB'];
  998. } else if (isset($_GET['switchdb'])) {
  999. foreach ($databases as $db_id => $database) {
  1000. if ($database['path'] == $_GET['switchdb']) {
  1001. $_SESSION[COOKIENAME . "currentDB"] = $database;
  1002. break;
  1003. }
  1004. }
  1005. $currentDB = $_SESSION[COOKIENAME . 'currentDB'];
  1006. }
  1007. if (isset($_SESSION[COOKIENAME . 'currentDB']) && in_array($_SESSION[COOKIENAME . 'currentDB'], $databases)) {
  1008. $currentDB = $_SESSION[COOKIENAME . 'currentDB'];
  1009. }
  1010. //- Open database (creates a Database object)
  1011. $db = new Database($currentDB); //create the Database object
  1012. $db->registerUserFunction($custom_functions);
  1013. // collect parameters early, just once
  1014. $target_table = isset($_GET['table']) ? $_GET['table'] : null;
  1015. //- Switch on $_GET['action'] for operations without output
  1016. if (isset($_GET['action']) && isset($_GET['confirm'])) {
  1017. switch ($_GET['action']) {
  1018. //- Table actions
  1019. //- Create table (=table_create)
  1020. case "table_create":
  1021. $num = intval($_POST['rows']);
  1022. $name = $_POST['tablename'];
  1023. $primary_keys = array();
  1024. for ($i = 0; $i < $num; $i++) {
  1025. if ($_POST[$i . '_field'] != "" && isset($_POST[$i . '_primarykey'])) {
  1026. $primary_keys[] = $_POST[$i . '_field'];
  1027. }
  1028. }
  1029. $query = "CREATE TABLE " . $db->quote($name) . " (";
  1030. for ($i = 0; $i < $num; $i++) {
  1031. if ($_POST[$i . '_field'] != "") {
  1032. $query .= $db->quote($_POST[$i . '_field']) . " ";
  1033. $query .= $_POST[$i . '_type'] . " ";
  1034. if (isset($_POST[$i . '_primarykey'])) {
  1035. if (count($primary_keys) == 1) {
  1036. $query .= "PRIMARY KEY ";
  1037. if (isset($_POST[$i . '_autoincrement']) && $db->getType() != "SQLiteDatabase") {
  1038. $query .= "AUTOINCREMENT ";
  1039. }
  1040. }
  1041. $query .= "NOT NULL ";
  1042. }
  1043. if (!isset($_POST[$i . '_primarykey']) && isset($_POST[$i . '_notnull'])) {
  1044. $query .= "NOT NULL ";
  1045. }
  1046. if ($_POST[$i . '_defaultoption'] != 'defined' && $_POST[$i . '_defaultoption'] != 'none' && $_POST[$i . '_defaultoption'] != 'expr') {
  1047. $query .= "DEFAULT " . $_POST[$i . '_defaultoption'] . " ";
  1048. } elseif ($_POST[$i . '_defaultoption'] == 'expr') {
  1049. $query .= "DEFAULT (" . $_POST[$i . '_defaultvalue'] . ") ";
  1050. } elseif (isset($_POST[$i . '_defaultvalue']) && $_POST[$i . '_defaultoption'] == 'defined') {
  1051. $typeAffinity = get_type_affinity($_POST[$i . '_type']);
  1052. if (($typeAffinity == "INTEGER" || $typeAffinity == "REAL" || $typeAffinity == "NUMERIC") && is_numeric($_POST[$i . '_defaultvalue'])) {
  1053. $query .= "DEFAULT " . $_POST[$i . '_defaultvalue'] . " ";
  1054. } else {
  1055. $query .= "DEFAULT " . $db->quote($_POST[$i . '_defaultvalue']) . " ";
  1056. }
  1057. }
  1058. $query = substr($query, 0, sizeof($query) - 2);
  1059. $query .= ", ";
  1060. }
  1061. }
  1062. if (count($primary_keys) > 1) {
  1063. $compound_key = "";
  1064. foreach ($primary_keys as $primary_key) {
  1065. $compound_key .= ($compound_key == "" ? "" : ", ") . $db->quote($primary_key);
  1066. }
  1067. $query .= "PRIMARY KEY (" . $compound_key . "), ";
  1068. }
  1069. $query = substr($query, 0, sizeof($query) - 3);
  1070. $query .= ")";
  1071. $result = $db->query($query);
  1072. if ($result === false) {
  1073. $error = true;
  1074. }
  1075. $completed = $lang['tbl'] . " '" . htmlencode($_POST['tablename']) . "' " . $lang['created'] . ".<br/><span style='font-size:11px;'>" . htmlencode($query) . "</span>";
  1076. $backlinkParameters = "&amp;action=column_view&amp;table=" . urlencode($name);
  1077. break;
  1078. //- Empty table (=table_empty)
  1079. case "table_empty":
  1080. $query = "DELETE FROM " . $db->quote_id($_POST['tablename']);
  1081. $result = $db->query($query);
  1082. if ($result === false) {
  1083. $error = true;
  1084. }
  1085. $query = "VACUUM";
  1086. $result = $db->query($query);
  1087. if ($result === false) {
  1088. $error = true;
  1089. }
  1090. $completed = $lang['tbl'] . " '" . htmlencode($_POST['tablename']) . "' " . $lang['emptied'] . ".<br/><span style='font-size:11px;'>" . htmlencode($query) . "</span>";
  1091. $backlinkParameters = "&amp;action=row_view&amp;table=" . urlencode($name);
  1092. break;
  1093. //- Create view (=view_create)
  1094. case "view_create":
  1095. $query = "CREATE VIEW " . $db->quote($_POST['viewname']) . " AS " . $_POST['select'];
  1096. $result = $db->query($query);
  1097. if ($result === false) {
  1098. $error = true;
  1099. }
  1100. $completed = $lang['view'] . " '" . htmlencode($_POST['viewname']) . "' " . $lang['created'] . ".<br/><span style='font-size:11px;'>" . htmlencode($query) . "</span>";
  1101. $backlinkParameters = "&amp;action=column_view&amp;table=" . urlencode($_POST['viewname']);
  1102. break;
  1103. //- Drop table (=table_drop)
  1104. case "table_drop":
  1105. $query = "DROP TABLE " . $db->quote_id($_POST['tablename']);
  1106. $result = $db->query($query);
  1107. if ($result === false) {
  1108. $error = true;
  1109. }
  1110. $completed = $lang['tbl'] . " '" . htmlencode($_POST['tablename']) . "' " . $lang['dropped'] . ".";
  1111. $backlinkParameters = "";
  1112. break;
  1113. //- Drop view (=view_drop)
  1114. case "view_drop":
  1115. $query = "DROP VIEW " . $db->quote_id($_POST['viewname']);
  1116. $result = $db->query($query);
  1117. if ($result === false) {
  1118. $error = true;
  1119. }
  1120. $completed = $lang['view'] . " '" . htmlencode($_POST['viewname']) . "' " . $lang['dropped'] . ".";
  1121. $backlinkParameters = "";
  1122. break;
  1123. //- Rename table (=table_rename)
  1124. case "table_rename":
  1125. $query = "ALTER TABLE " . $db->quote_id($_POST['oldname']) . " RENAME TO " . $db->quote($_POST['newname']);
  1126. if ($db->getVersion() == 3) {
  1127. $result = $db->query($query, true);
  1128. } else {
  1129. $result = $db->query($query, false);
  1130. }
  1131. if ($result === false) {
  1132. $error = true;
  1133. }
  1134. $completed = $lang['tbl'] . " '" . htmlencode($_POST['oldname']) . "' " . $lang['renamed'] . " '" . htmlencode($_POST['newname']) . "'.<br/><span style='font-size:11px;'>" . htmlencode($query) . "</span>";
  1135. $backlinkParameters = "&amp;action=row_view&amp;table=" . urlencode($_POST['newname']);
  1136. break;
  1137. //- Row actions
  1138. //- Create row (=row_create)
  1139. case "row_create":
  1140. $completed = "";
  1141. $num = $_POST['numRows'];
  1142. $fields = explode(":", $_POST['fields']);
  1143. $z = 0;
  1144. $query = "PRAGMA table_info(" . $db->quote_id($target_table) . ")";
  1145. $result = $db->selectArray($query);
  1146. for ($i = 0; $i < $num; $i++) {
  1147. if (!isset($_POST[$i . ":ignore"])) {
  1148. $query_cols = "";
  1149. $query_vals = "";
  1150. $all_default = true;
  1151. for ($j = 0; $j < sizeof($fields); $j++) {
  1152. if ($result[$j]['name'] != $fields[$j]) {
  1153. die($lang['err'] . ' - schema missmatch');
  1154. }
  1155. $null = isset($_POST[$i . ":" . $j . "_null"]);
  1156. if (!$null) {
  1157. $value = $_POST[$i . ":" . $j];
  1158. } else {
  1159. $value = "";
  1160. }
  1161. if ($value === $result[$j]['dflt_value']) {
  1162. // if the value is the default value, skip it
  1163. continue;
  1164. } else {
  1165. $all_default = false;
  1166. }
  1167. $query_cols .= $db->quote_id($fields[$j]) . ",";
  1168. $type = $result[$j]['type'];
  1169. $typeAffinity = get_type_affinity($type);
  1170. $function = $_POST["function_" . $i . "_" . $j];
  1171. if ($function != "") {
  1172. $query_vals .= $function . "(";
  1173. }
  1174. if (($typeAffinity == "TEXT" || $typeAffinity == "NONE") && !$null) {
  1175. $query_vals .= $db->quote($value);
  1176. } elseif (($typeAffinity == "INTEGER" || $typeAffinity == "REAL" || $typeAffinity == "NUMERIC") && $value == "") {
  1177. $query_vals .= "NULL";
  1178. } elseif ($null) {
  1179. $query_vals .= "NULL";
  1180. } else {
  1181. $query_vals .= $db->quote($value);
  1182. }
  1183. if ($function != "") {
  1184. $query_vals .= ")";
  1185. }
  1186. $query_vals .= ",";
  1187. }
  1188. $query = "INSERT INTO " . $db->quote_id($target_table);
  1189. if (!$all_default) {
  1190. $query_cols = substr($query_cols, 0, strlen($query_cols) - 1);
  1191. $query_vals = substr($query_vals, 0, strlen($query_vals) - 1);
  1192. $query .= " (" . $query_cols . ") VALUES (" . $query_vals . ")";
  1193. } else {
  1194. $query .= " DEFAULT VALUES";
  1195. }
  1196. $result1 = $db->query($query);
  1197. if ($result1 === false) {
  1198. $error = true;
  1199. }
  1200. $completed .= "<span style='font-size:11px;'>" . htmlencode($query) . "</span><br/>";
  1201. $z++;
  1202. }
  1203. }
  1204. $completed = $z . " " . $lang['rows'] . " " . $lang['inserted'] . ".<br/><br/>" . $completed;
  1205. $backlinkParameters = "&amp;action=column_view&amp;table=" . urlencode($target_table);
  1206. break;
  1207. //- Delete row (=row_delete)
  1208. case "row_delete":
  1209. $pks = json_decode($_GET['pk']);
  1210. $query = "DELETE FROM " . $db->quote_id($target_table) . " WHERE (" . $db->wherePK($target_table, json_decode($pks[0])) . ")";
  1211. for ($i = 1; $i < sizeof($pks); $i++) {
  1212. $query .= " OR (" . $db->wherePK($target_table, json_decode($pks[$i])) . ")";
  1213. }
  1214. $result = $db->query($query);
  1215. if ($result === false) {
  1216. $error = true;
  1217. }
  1218. $completed = sizeof($pks) . " " . $lang['rows'] . " " . $lang['deleted'] . ".<br/><span style='font-size:11px;'>" . htmlencode($query) . "</span>";
  1219. $backlinkParameters = "&amp;action=row_view&amp;table=" . urlencode($target_table);
  1220. break;
  1221. //- Edit row (=row_edit)
  1222. case "row_edit":
  1223. $pks = json_decode($_GET['pk']);
  1224. $fields = explode(":", $_POST['fieldArray']);
  1225. $z = 0;
  1226. $query = "PRAGMA table_info(" . $db->quote_id($target_table) . ")";
  1227. $result = $db->selectArray($query);
  1228. if (isset($_POST['new_row'])) {
  1229. $completed = "";
  1230. } else {
  1231. $completed = sizeof($pks) . " " . $lang['rows'] . " " . $lang['affected'] . ".<br/><br/>";
  1232. }
  1233. for ($i = 0; $i < sizeof($pks); $i++) {
  1234. if (isset($_POST['new_row'])) {
  1235. $query_cols = "";
  1236. $query_vals = "";
  1237. $all_default = true;
  1238. for ($j = 0; $j < sizeof($fields); $j++) {
  1239. if ($result[$j]['name'] != $fields[$j]) {
  1240. die($lang['err'] . ' - schema missmatch');
  1241. }
  1242. $null = isset($_POST[$j . "_null"][$i]);
  1243. if (!$null) {
  1244. $value = $_POST[$j][$i];
  1245. } else {
  1246. $value = "";
  1247. }
  1248. if ($value === $result[$j]['dflt_value']) {
  1249. // if the value is the default value, skip it
  1250. continue;
  1251. } else {
  1252. $all_default = false;
  1253. }
  1254. $query_cols .= $db->quote_id($fields[$j]) . ",";
  1255. $type = $result[$j]['type'];
  1256. $typeAffinity = get_type_affinity($type);
  1257. $function = $_POST["function_" . $j][$i];
  1258. if ($function != "") {
  1259. $query_vals .= $function . "(";
  1260. }
  1261. if (($typeAffinity == "TEXT" || $typeAffinity == "NONE") && !$null) {
  1262. $query_vals .= $db->quote($value);
  1263. } elseif (($typeAffinity == "INTEGER" || $typeAffinity == "REAL" || $typeAffinity == "NUMERIC") && $value == "") {
  1264. $query_vals .= "NULL";
  1265. } elseif ($null) {
  1266. $query_vals .= "NULL";
  1267. } else {
  1268. $query_vals .= $db->quote($value);
  1269. }
  1270. if ($function != "") {
  1271. $query_vals .= ")";
  1272. }
  1273. $query_vals .= ",";
  1274. }
  1275. $query = "INSERT INTO " . $db->quote_id($target_table);
  1276. if (!$all_default) {
  1277. $query_cols = substr($query_cols, 0, strlen($query_cols) - 1);
  1278. $query_vals = substr($query_vals, 0, strlen($query_vals) - 1);
  1279. $query .= " (" . $query_cols . ") VALUES (" . $query_vals . ")";
  1280. } else {
  1281. $query .= " DEFAULT VALUES";
  1282. }
  1283. $result1 = $db->query($query);
  1284. if ($result1 === false) {
  1285. $error = true;
  1286. }
  1287. $z++;
  1288. } else {
  1289. $query = "UPDATE " . $db->quote_id($target_table) . " SET ";
  1290. for ($j = 0; $j < sizeof($fields); $j++) {
  1291. $function = $_POST["function_" . $j][$i];
  1292. $null = isset($_POST[$j . "_null"][$i]);
  1293. $query .= $db->quote_id($fields[$j]) . "=";
  1294. if ($function != "") {
  1295. $query .= $function . "(";
  1296. }
  1297. if ($null) {
  1298. $query .= "NULL";
  1299. } else {
  1300. $query .= $db->quote($_POST[$j][$i]);
  1301. }
  1302. if ($function != "") {
  1303. $query .= ")";
  1304. }
  1305. $query .= ", ";
  1306. }
  1307. $query = substr($query, 0, sizeof($query) - 3);
  1308. $query .= " WHERE " . $db->wherePK($target_table, json_decode($pks[$i]));
  1309. $result1 = $db->query($query);
  1310. if ($result1 === false) {
  1311. $error = true;
  1312. }
  1313. }
  1314. $completed .= "<span style='font-size:11px;'>" . htmlencode($query) . "</span><br/>";
  1315. }
  1316. if (isset($_POST['new_row'])) {
  1317. $completed = $z . " " . $lang['rows'] . " " . $lang['inserted'] . ".<br/><br/>" . $completed;
  1318. }
  1319. $backlinkParameters = "&amp;action=row_view&amp;table=" . urlencode($target_table);
  1320. break;
  1321. //- Column actions
  1322. //- Create column (=column_create)
  1323. case "column_create":
  1324. $num = intval($_POST['rows']);
  1325. for ($i = 0; $i < $num; $i++) {
  1326. if ($_POST[$i . '_field'] != "") {
  1327. $query = "ALTER TABLE " . $db->quote_id($target_table) . " ADD " . $db->quote($_POST[$i . '_field']) . " ";
  1328. $query .= $_POST[$i . '_type'] . " ";
  1329. if (isset($_POST[$i . '_primarykey'])) {
  1330. $query .= "PRIMARY KEY ";
  1331. }
  1332. if (isset($_POST[$i . '_notnull'])) {
  1333. $query .= "NOT NULL ";
  1334. }
  1335. if ($_POST[$i . '_defaultoption'] != 'defined' && $_POST[$i . '_defaultoption'] != 'none' && $_POST[$i . '_defaultoption'] != 'expr') {
  1336. $query .= "DEFAULT " . $_POST[$i . '_defaultoption'] . " ";
  1337. } elseif ($_POST[$i . '_defaultoption'] == 'expr') {
  1338. $query .= "DEFAULT (" . $_POST[$i . '_defaultvalue'] . ") ";
  1339. } elseif (isset($_POST[$i . '_defaultvalue']) && $_POST[$i . '_defaultoption'] == 'defined') {
  1340. $typeAffinity = get_type_affinity($_POST[$i . '_type']);
  1341. if (($typeAffinity == "INTEGER" || $typeAffinity == "REAL" || $typeAffinity == "NUMERIC") && is_numeric($_POST[$i . '_defaultvalue'])) {
  1342. $query .= "DEFAULT " . $_POST[$i . '_defaultvalue'] . " ";
  1343. } else {
  1344. $query .= "DEFAULT " . $db->quote($_POST[$i . '_defaultvalue']) . " ";
  1345. }
  1346. }
  1347. if ($db->getVersion() == 3 &&
  1348. ($_POST[$i . '_defaultoption'] == 'defined' || $_POST[$i . '_defaultoption'] == 'none' || $_POST[$i . '_defaultoption'] == 'NULL')
  1349. // Sqlite3 cannot add columns with default values that are not constant
  1350. && !isset($_POST[$i . '_primarykey'])
  1351. // sqlite3 cannot add primary key columns
  1352. && (!isset($_POST[$i . '_notnull']) || $_POST[$i . '_defaultoption'] != 'none')
  1353. // SQLite3 cannot add NOT NULL columns without DEFAULT even if the table is empty
  1354. )
  1355. // use SQLITE3 ALTER TABLE ADD COLUMN
  1356. {
  1357. $result = $db->query($query, true);
  1358. } else
  1359. // use ALTER TABLE workaround
  1360. {
  1361. $result = $db->query($query, false);
  1362. }
  1363. if ($result === false) {
  1364. $error = true;
  1365. }
  1366. }
  1367. }
  1368. $completed = $lang['tbl'] . " '" . htmlencode($target_table) . "' " . $lang['altered'] . ".";
  1369. $backlinkParameters = "&amp;action=column_view&amp;table=" . urlencode($target_table);
  1370. break;
  1371. //- Delete column (=column_delete)
  1372. case "column_delete":
  1373. $pks = explode(":", $_GET['pk']);
  1374. $query = "ALTER TABLE " . $db->quote_id($target_table) . ' DROP ' . $db->quote_id($pks[0]);
  1375. for ($i = 1; $i < sizeof($pks); $i++) {
  1376. $query .= ", DROP " . $db->quote_id($pks[$i]);
  1377. }
  1378. $result = $db->query($query);
  1379. if ($result === false) {
  1380. $error = true;
  1381. }
  1382. $completed = $lang['tbl'] . " '" . htmlencode($target_table) . "' " . $lang['altered'] . ".";
  1383. $backlinkParameters = "&amp;action=column_view&amp;table=" . urlencode($target_table);
  1384. break;
  1385. //- Add a primary key (=primarykey_add)
  1386. case "primarykey_add":
  1387. $pks = explode(":", $_GET['pk']);
  1388. $query = "ALTER TABLE " . $db->quote_id($target_table) . ' ADD PRIMARY KEY (' . $db->quote_id($pks[0]);
  1389. for ($i = 1; $i < sizeof($pks); $i++) {
  1390. $query .= ", " . $db->quote_id($pks[$i]);
  1391. }
  1392. $query .= ")";
  1393. $result = $db->query($query);
  1394. if ($result === false) {
  1395. $error = true;
  1396. }
  1397. $completed = $lang['tbl'] . " '" . htmlencode($target_table) . "' " . $lang['altered'] . ".";
  1398. $backlinkParameters = "&amp;action=column_view&amp;table=" . urlencode($target_table);
  1399. break;
  1400. //- Edit column (=column_edit)
  1401. case "column_edit":
  1402. $query = "ALTER TABLE " . $db->quote_id($target_table) . ' CHANGE ' . $db->quote_id($_POST['oldvalue']) . " " . $db->quote($_POST['0_field']) . " " . $_POST['0_type'];
  1403. $result = $db->query($query);
  1404. if ($result === false) {
  1405. $error = true;
  1406. }
  1407. $completed = $lang['tbl'] . " '" . htmlencode($target_table) . "' " . $lang['altered'] . ".";
  1408. $backlinkParameters = "&amp;action=column_view&amp;table=" . urlencode($target_table);
  1409. break;
  1410. //- Delete trigger (=trigger_delete)
  1411. case "trigger_delete":
  1412. $query = "DROP TRIGGER " . $db->quote_id($_GET['pk']);
  1413. $result = $db->query($query);
  1414. if ($result === false) {
  1415. $error = true;
  1416. }
  1417. $completed = $lang['trigger'] . " '" . htmlencode($_GET['pk']) . "' " . $lang['deleted'] . ".<br/><span style='font-size:11px;'>" . htmlencode($query) . "</span>";
  1418. $backlinkParameters = "&amp;action=column_view&amp;table=" . urlencode($target_table);
  1419. break;
  1420. //- Delete index (=index_delete)
  1421. case "index_delete":
  1422. $query = "DROP INDEX " . $db->quote_id($_GET['pk']);
  1423. $result = $db->query($query);
  1424. if ($result === false) {
  1425. $error = true;
  1426. }
  1427. $completed = $lang['index'] . " '" . htmlencode($_GET['pk']) . "' " . $lang['deleted'] . ".<br/><span style='font-size:11px;'>" . htmlencode($query) . "</span>";
  1428. $backlinkParameters = "&amp;action=column_view&amp;table=" . urlencode($target_table);
  1429. break;
  1430. //- Create trigger (=trigger_create)
  1431. case "trigger_create":
  1432. $str = "CREATE TRIGGER " . $db->quote($_POST['trigger_name']);
  1433. if ($_POST['beforeafter'] != "") {
  1434. $str .= " " . $_POST['beforeafter'];
  1435. }
  1436. $str .= " " . $_POST['event'] . " ON " . $db->quote_id($target_table);
  1437. if (isset($_POST['foreachrow'])) {
  1438. $str .= " FOR EACH ROW";
  1439. }
  1440. if ($_POST['whenexpression'] != "") {
  1441. $str .= " WHEN " . $_POST['whenexpression'];
  1442. }
  1443. $str .= " BEGIN";
  1444. $str .= " " . $_POST['triggersteps'];
  1445. $str .= " END";
  1446. $query = $str;
  1447. $result = $db->query($query);
  1448. if ($result === false) {
  1449. $error = true;
  1450. }
  1451. $completed = $lang['trigger'] . " " . $lang['created'] . ".<br/><span style='font-size:11px;'>" . htmlencode($query) . "</span>";
  1452. $backlinkParameters = "&amp;action=column_view&amp;table=" . urlencode($target_table);
  1453. break;
  1454. //- Create index (=index_create)
  1455. case "index_create":
  1456. $num = $_POST['num'];
  1457. if ($_POST['name'] == "") {
  1458. $completed = $lang['blank_index'];
  1459. } else if ($_POST['0_field'] == "") {
  1460. $completed = $lang['one_index'];
  1461. } else {
  1462. $str = "CREATE ";
  1463. if ($_POST['duplicate'] == "no") {
  1464. $str .= "UNIQUE ";
  1465. }
  1466. $str .= "INDEX " . $db->quote($_POST['name']) . " ON " . $db->quote_id($target_table) . " (";
  1467. $str .= $db->quote_id($_POST['0_field']) . $_POST['0_order'];
  1468. for ($i = 1; $i < $num; $i++) {
  1469. if ($_POST[$i . '_field'] != "") {
  1470. $str .= ", " . $db->quote_id($_POST[$i . '_field']) . $_POST[$i . '_order'];
  1471. }
  1472. }
  1473. $str .= ")";
  1474. if (isset($_POST['where']) && $_POST['where'] != '') {
  1475. $str .= " WHERE " . $_POST['where'];
  1476. }
  1477. $query = $str;
  1478. $result = $db->query($query);
  1479. if ($result === false) {
  1480. $error = true;
  1481. }
  1482. $completed = $lang['index'] . " " . $lang['created'] . ".<br/><span style='font-size:11px;'>" . htmlencode($query) . "</span>";
  1483. }
  1484. $backlinkParameters = "&amp;action=column_view&amp;table=" . urlencode($target_table);
  1485. break;
  1486. }
  1487. }
  1488. // are we working on a view? let's check once here
  1489. $target_table_type = $target_table ? $db->getTypeOfTable($target_table) : null;
  1490. //- HTML: sidebar
  1491. echo '<table class="body_tbl" width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top" class="left_td" style="width:100px; padding:9px 2px 9px 9px;">';
  1492. echo "<div id='leftNav'>";
  1493. echo "<h1><a href='" . PAGE . "'>";
  1494. echo "<span id='logo'>" . PROJECT . "</span> <span id='version'>v" . VERSION . "</span>";
  1495. echo "</a></h1>";
  1496. echo "<div id='headerlinks'>";
  1497. echo "<a href='javascript:void' onclick='openHelp(\"top\");'>" . $lang['docu'] . "</a> | ";
  1498. echo "<a href='http://www.gnu.org/licenses/gpl.html' target='_blank'>" . $lang['license'] . "</a> | ";
  1499. echo "<a href='" . PROJECT_URL . "' target='_blank'>" . $lang['proj_site'] . "</a>";
  1500. echo "</div>";
  1501. //- HTML: database list
  1502. $db->print_db_list();
  1503. echo "<fieldset style='margin:15px;'><legend>";
  1504. echo "<a href='" . PAGE . "'";
  1505. if (!$target_table) {
  1506. echo " class='active_table'";
  1507. }
  1508. $name = $currentDB['name'];
  1509. if (strlen($name) > 25) {
  1510. $name = "..." . substr($name, strlen($name) - 22, 22);
  1511. }
  1512. echo ">" . htmlencode($name) . "</a>";
  1513. echo "</legend>";
  1514. //- HTML: table list
  1515. $query = "SELECT type, name FROM sqlite_master WHERE type='table' OR type='view' ORDER BY name";
  1516. $result = $db->selectArray($query);
  1517. $j = 0;
  1518. for ($i = 0; $i < sizeof($result); $i++) {
  1519. if (substr($result[$i]['name'], 0, 7) != "sqlite_" && $result[$i]['name'] != "") {
  1520. echo "<span class='sidebar_table'>[" . $lang[$result[$i]['type'] == 'table' ? 'tbl' : 'view'] . "]</span> ";
  1521. echo "<a href='?action=row_view&amp;table=" . urlencode($result[$i]['name']) . "'";
  1522. if ($target_table == $result[$i]['name']) {
  1523. echo " class='active_table'";
  1524. }
  1525. echo ">" . htmlencode($result[$i]['name']) . "</a><br/>";
  1526. $j++;
  1527. }
  1528. }
  1529. if ($j == 0) {
  1530. echo $lang['no_tbl'];
  1531. }
  1532. echo "</fieldset>";
  1533. //- HTML: form to create a new database
  1534. if ($directory !== false && is_writable($directory)) {
  1535. echo "<fieldset style='margin:15px;'><legend><b>" . $lang['db_create'] . "</b> " . helpLink($lang['help2']) . "</legend>";
  1536. echo "<form name='create_database' method='post' action='" . PAGE . "'>";
  1537. echo $token_html;
  1538. echo "<input type='text' name='new_dbname' style='width:150px;'/>";
  1539. if (class_exists('SQLiteDatabase') && (class_exists('SQLite3') || class_exists('PDO'))) {
  1540. echo "<select name='new_dbtype' class='newDbType'>";
  1541. echo "<option value='3'>SQLite 3</option>";
  1542. echo "<option value='2'>SQLite 2</option>";
  1543. echo "</select>";
  1544. }
  1545. echo "<input type='submit' value='" . $lang['create'] . "' class='btn'/>";
  1546. echo "</form>";
  1547. echo "</fieldset>";
  1548. }
  1549. echo "<div style='text-align:center;'>";
  1550. echo "<form action='" . PAGE . "' method='post'>";
  1551. echo $token_html;
  1552. echo "<input type='submit' value='" . $lang['logout'] . "' name='logout' class='btn'/>";
  1553. echo "</form>";
  1554. echo "</div>";
  1555. echo "</div>";
  1556. echo '</td><td valign="top" id="main_column" class="right_td" style="padding:9px 2px 9px 9px;">';
  1557. //- HTML: breadcrumb navigation
  1558. echo "<a href='" . PAGE . "'>" . htmlencode($currentDB['name']) . "</a>";
  1559. if ($target_table) {
  1560. echo " &rarr; <a href='?table=" . urlencode($target_table) . "&amp;action=row_view'>" . htmlencode($target_table) . "</a>";
  1561. }
  1562. echo "<br/><br/>";
  1563. //- HTML: confirmation panel
  1564. //if the user has performed some action, show the resulting message
  1565. if (isset($_GET['confirm'])) {
  1566. echo "<div id='main'>";
  1567. echo "<div class='confirm'>";
  1568. if (isset($error) && $error) //an error occured during the action, so show an error message
  1569. {
  1570. echo $lang['err'] . ": " . htmlencode($db->getError()) . "<br/>" . $lang['bug_report'] . ' ' . PROJECT_BUGTRACKER_LINK;
  1571. } else //action was performed successfully - show success message
  1572. {
  1573. echo $completed;
  1574. }
  1575. echo "</div>";
  1576. if ($_GET['action'] == "row_delete" || $_GET['action'] == "row_create" || $_GET['action'] == "row_edit") {
  1577. echo "<br/><br/><a href='?table=" . urlencode($target_table) . "&amp;action=row_view'>" . $lang['return'] . "</a>";
  1578. } else if ($_GET['action'] == "column_create" || $_GET['action'] == "column_delete" || $_GET['action'] == "column_edit" || $_GET['action'] == "index_create" || $_GET['action'] == "index_delete" || $_GET['action'] == "trigger_delete" || $_GET['action'] == "trigger_create") {
  1579. echo "<br/><br/><a href='?table=" . urlencode($target_table) . "&amp;action=column_view'>" . $lang['return'] . "</a>";
  1580. } else {
  1581. echo "<br/><br/><a href='" . PAGE . (isset($backlinkParameters) ? "?" . $backlinkParameters : '') . "'>" . $lang['return'] . "</a>";
  1582. }
  1583. echo "</div>";
  1584. }
  1585. //- Show the various tab views for a table
  1586. if (!isset($_GET['confirm']) && $target_table && isset($_GET['action']) && ($_GET['action'] == "table_export" || $_GET['action'] == "table_import" || $_GET['action'] == "table_sql" || $_GET['action'] == "row_view" || $_GET['action'] == "row_create" || $_GET['action'] == "column_view" || $_GET['action'] == "table_rename" || $_GET['action'] == "table_search" || $_GET['action'] == "table_triggers")) {
  1587. //- HTML: tabs for tables
  1588. if ($target_table_type == 'table') {
  1589. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=row_view' ";
  1590. if ($_GET['action'] == "row_view") {
  1591. echo "class='tab_pressed'";
  1592. } else {
  1593. echo "class='tab'";
  1594. }
  1595. echo ">" . $lang['browse'] . "</a>";
  1596. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=column_view' ";
  1597. if ($_GET['action'] == "column_view") {
  1598. echo "class='tab_pressed'";
  1599. } else {
  1600. echo "class='tab'";
  1601. }
  1602. echo ">" . $lang['struct'] . "</a>";
  1603. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=table_sql' ";
  1604. if ($_GET['action'] == "table_sql") {
  1605. echo "class='tab_pressed'";
  1606. } else {
  1607. echo "class='tab'";
  1608. }
  1609. echo ">" . $lang['sql'] . "</a>";
  1610. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=table_search' ";
  1611. if ($_GET['action'] == "table_search") {
  1612. echo "class='tab_pressed'";
  1613. } else {
  1614. echo "class='tab'";
  1615. }
  1616. echo ">" . $lang['srch'] . "</a>";
  1617. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=row_create' ";
  1618. if ($_GET['action'] == "row_create") {
  1619. echo "class='tab_pressed'";
  1620. } else {
  1621. echo "class='tab'";
  1622. }
  1623. echo ">" . $lang['insert'] . "</a>";
  1624. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=table_export' ";
  1625. if ($_GET['action'] == "table_export") {
  1626. echo "class='tab_pressed'";
  1627. } else {
  1628. echo "class='tab'";
  1629. }
  1630. echo ">" . $lang['export'] . "</a>";
  1631. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=table_import' ";
  1632. if ($_GET['action'] == "table_import") {
  1633. echo "class='tab_pressed'";
  1634. } else {
  1635. echo "class='tab'";
  1636. }
  1637. echo ">" . $lang['import'] . "</a>";
  1638. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=table_rename' ";
  1639. if ($_GET['action'] == "table_rename") {
  1640. echo "class='tab_pressed'";
  1641. } else {
  1642. echo "class='tab'";
  1643. }
  1644. echo ">" . $lang['rename'] . "</a>";
  1645. echo "<a href='?action=table_empty&amp;table=" . urlencode($target_table) . "' ";
  1646. echo "class='tab empty'";
  1647. echo ">" . $lang['empty'] . "</a>";
  1648. echo "<a href='?action=table_drop&amp;table=" . urlencode($target_table) . "' ";
  1649. echo "class='tab drop'";
  1650. echo ">" . $lang['drop'] . "</a>";
  1651. echo "<div style='clear:both;'></div>";
  1652. } else
  1653. //- HTML: tabs for views
  1654. {
  1655. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=row_view' ";
  1656. if ($_GET['action'] == "row_view") {
  1657. echo "class='tab_pressed'";
  1658. } else {
  1659. echo "class='tab'";
  1660. }
  1661. echo ">" . $lang['browse'] . "</a>";
  1662. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=column_view' ";
  1663. if ($_GET['action'] == "column_view") {
  1664. echo "class='tab_pressed'";
  1665. } else {
  1666. echo "class='tab'";
  1667. }
  1668. echo ">" . $lang['struct'] . "</a>";
  1669. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=table_sql' ";
  1670. if ($_GET['action'] == "table_sql") {
  1671. echo "class='tab_pressed'";
  1672. } else {
  1673. echo "class='tab'";
  1674. }
  1675. echo ">" . $lang['sql'] . "</a>";
  1676. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=table_search' ";
  1677. if ($_GET['action'] == "table_search") {
  1678. echo "class='tab_pressed'";
  1679. } else {
  1680. echo "class='tab'";
  1681. }
  1682. echo ">" . $lang['srch'] . "</a>";
  1683. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=table_export' ";
  1684. if ($_GET['action'] == "table_export") {
  1685. echo "class='tab_pressed'";
  1686. } else {
  1687. echo "class='tab'";
  1688. }
  1689. echo ">" . $lang['export'] . "</a>";
  1690. echo "<a href='?action=view_drop&amp;table=" . urlencode($target_table) . "' ";
  1691. echo "class='tab drop'";
  1692. echo ">" . $lang['drop'] . "</a>";
  1693. echo "<div style='clear:both;'></div>";
  1694. }
  1695. }
  1696. //- Switch on $_GET['action'] for operations with output
  1697. if (isset($_GET['action']) && !isset($_GET['confirm'])) {
  1698. echo "<div id='main'>";
  1699. switch ($_GET['action']) {
  1700. //- Table actions
  1701. //- Create table (=table_create)
  1702. case "table_create":
  1703. $query = "SELECT name FROM sqlite_master WHERE type='table' AND name=" . $db->quote($_POST['tablename']);
  1704. $results = $db->selectArray($query);
  1705. if (sizeof($results) > 0) {
  1706. $exists = true;
  1707. } else {
  1708. $exists = false;
  1709. }
  1710. echo "<h2>" . $lang['create_tbl'] . ": '" . htmlencode($_POST['tablename']) . "'</h2>";
  1711. if ($_POST['tablefields'] == "" || intval($_POST['tablefields']) <= 0) {
  1712. echo $lang['specify_fields'];
  1713. } else if ($_POST['tablename'] == "") {
  1714. echo $lang['specify_tbl'];
  1715. } else if ($exists) {
  1716. echo $lang['tbl_exists'];
  1717. } else {
  1718. $num = intval($_POST['tablefields']);
  1719. $name = $_POST['tablename'];
  1720. echo "<form action='?action=table_create&amp;confirm=1' method='post'>";
  1721. echo $token_html;
  1722. echo "<input type='hidden' name='tablename' value='" . htmlencode($name) . "'/>";
  1723. echo "<input type='hidden' name='rows' value='" . $num . "'/>";
  1724. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  1725. echo "<tr>";
  1726. $headings = array($lang['fld'], $lang['type'], $lang['prim_key']);
  1727. if ($db->getType() != "SQLiteDatabase") {
  1728. $headings[] = $lang['autoincrement'];
  1729. }
  1730. $headings[] = $lang['not_null'];
  1731. $headings[] = $lang['def_val'];
  1732. for ($k = 0; $k < count($headings); $k++) {
  1733. echo "<td class='tdheader'>" . $headings[$k] . "</td>";
  1734. }
  1735. echo "</tr>";
  1736. for ($i = 0; $i < $num; $i++) {
  1737. $tdWithClass = "<td class='td" . ($i % 2 ? "1" : "2") . "'>";
  1738. echo "<tr>";
  1739. echo $tdWithClass;
  1740. echo "<input type='text' name='" . $i . "_field' style='width:200px;'/>";
  1741. echo "</td>";
  1742. echo $tdWithClass;
  1743. echo "<select name='" . $i . "_type' id='i" . $i . "_type' onchange='toggleAutoincrement(" . $i . ");'>";
  1744. foreach ($sqlite_datatypes as $t) {
  1745. echo "<option value='" . htmlencode($t) . "'>" . htmlencode($t) . "</option>";
  1746. }
  1747. echo "</select>";
  1748. echo "</td>";
  1749. echo $tdWithClass;
  1750. echo "<label><input type='checkbox' name='" . $i . "_primarykey' id='i" . $i . "_primarykey' onclick='toggleNull(" . $i . "); toggleAutoincrement(" . $i . ");'/> " . $lang['yes'] . "</label>";
  1751. echo "</td>";
  1752. if ($db->getType() != "SQLiteDatabase") {
  1753. echo $tdWithClass;
  1754. echo "<label><input type='checkbox' name='" . $i . "_autoincrement' id='i" . $i . "_autoincrement'/> " . $lang['yes'] . "</label>";
  1755. echo "</td>";
  1756. }
  1757. echo $tdWithClass;
  1758. echo "<label><input type='checkbox' name='" . $i . "_notnull' id='i" . $i . "_notnull'/> " . $lang['yes'] . "</label>";
  1759. echo "</td>";
  1760. echo $tdWithClass;
  1761. echo "<select name='" . $i . "_defaultoption' id='i" . $i . "_defaultoption' onchange=\"if(this.value!='defined' && this.value!='expr') document.getElementById('i" . $i . "_defaultvalue').value='';\">";
  1762. echo "<option value='none'>" . $lang['none'] . "</option><option value='defined'>" . $lang['as_defined'] . ":</option><option>NULL</option><option>CURRENT_TIME</option><option>CURRENT_DATE</option><option>CURRENT_TIMESTAMP</option><option value='expr'>" . $lang['expression'] . ":</option>";
  1763. echo "</select>";
  1764. echo "<input type='text' name='" . $i . "_defaultvalue' id='i" . $i . "_defaultvalue' style='width:100px;' onchange=\"if(document.getElementById('i" . $i . "_defaultoption').value!='expr') document.getElementById('i" . $i . "_defaultoption').value='defined';\"/>";
  1765. echo "</td>";
  1766. echo "</tr>";
  1767. }
  1768. echo "<tr>";
  1769. echo "<td class='tdheader' style='text-align:right;' colspan='6'>";
  1770. echo "<input type='submit' value='" . $lang['create'] . "' class='btn'/> ";
  1771. echo "<a href='" . PAGE . "'>" . $lang['cancel'] . "</a>";
  1772. echo "</td>";
  1773. echo "</tr>";
  1774. echo "</table>";
  1775. echo "</form>";
  1776. if ($db->getType() != "SQLiteDatabase") {
  1777. echo "<script type='text/javascript'>window.onload=initAutoincrement;</script>";
  1778. }
  1779. }
  1780. break;
  1781. //- Perform SQL query on table (=table_sql)
  1782. case "table_sql":
  1783. if (isset($_POST['query']) && $_POST['query'] != "") {
  1784. $delimiter = $_POST['delimiter'];
  1785. $queryStr = $_POST['queryval'];
  1786. //save the queries in history if necessary
  1787. if ($maxSavedQueries != 0 && $maxSavedQueries != false) {
  1788. if (!isset($_SESSION['query_history'])) {
  1789. $_SESSION['query_history'] = array();
  1790. }
  1791. $_SESSION['query_history'][md5(strtolower($queryStr))] = $queryStr;
  1792. if (sizeof($_SESSION['query_history']) > $maxSavedQueries) {
  1793. array_shift($_SESSION['query_history']);
  1794. }
  1795. }
  1796. $query = explode_sql($delimiter, $queryStr); //explode the query string into individual queries based on the delimiter
  1797. for ($i = 0; $i < sizeof($query); $i++) //iterate through the queries exploded by the delimiter
  1798. {
  1799. if (str_replace(" ", "", str_replace("\n", "", str_replace("\r", "", $query[$i]))) != "") //make sure this query is not an empty string
  1800. {
  1801. $queryTimer = new MicroTimer();
  1802. $table_result = $db->query($query[$i]);
  1803. echo "<div class='confirm'>";
  1804. echo "<b>" . htmlencode($query[$i]) . "</b>";
  1805. if ($table_result === null || $table_result === false) {
  1806. echo "<br /><b>" . $lang['err'] . ": " . htmlencode($db->getError()) . "</b></div>";
  1807. }
  1808. echo "</div><br/>";
  1809. if ($row = $db->fetch($table_result, 'assoc')) {
  1810. $headers = array_keys($row);
  1811. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  1812. echo "<tr>";
  1813. for ($j = 0; $j < sizeof($headers); $j++) {
  1814. echo "<td class='tdheader'>";
  1815. echo htmlencode($headers[$j]);
  1816. echo "</td>";
  1817. }
  1818. echo "</tr>";
  1819. $rowCount = 0;
  1820. for (; $rowCount == 0 || $row = $db->fetch($table_result, 'assoc'); $rowCount++) {
  1821. $tdWithClass = "<td class='td" . ($rowCount % 2 ? "1" : "2") . "'>";
  1822. echo "<tr>";
  1823. for ($z = 0; $z < sizeof($headers); $z++) {
  1824. echo $tdWithClass;
  1825. if ($row[$headers[$z]] === "") {
  1826. echo "&nbsp;";
  1827. } elseif ($row[$headers[$z]] === null) {
  1828. echo "<i class='null'>NULL</i>";
  1829. } else {
  1830. echo htmlencode(subString($row[$headers[$z]]));
  1831. }
  1832. echo "</td>";
  1833. }
  1834. echo "</tr>";
  1835. }
  1836. $queryTimer->stop();
  1837. echo "</table><br/><br/>";
  1838. if ($table_result !== null && $table_result !== false) {
  1839. echo "<div class='confirm' style='margin-bottom: 2em'>";
  1840. if ($rowCount > 0 || $db->getAffectedRows() == 0) {
  1841. printf($lang['show_rows'], $rowCount);
  1842. }
  1843. if ($db->getAffectedRows() > 0 || $rowCount == 0) {
  1844. echo $db->getAffectedRows() . " " . $lang['rows_aff'] . " ";
  1845. }
  1846. printf($lang['query_time'], $queryTimer);
  1847. echo "</div>";
  1848. }
  1849. }
  1850. }
  1851. }
  1852. } else {
  1853. $delimiter = ";";
  1854. $queryStr = "SELECT * FROM " . $db->quote_id($target_table) . " WHERE 1";
  1855. }
  1856. echo "<fieldset>";
  1857. echo "<legend><b>" . sprintf($lang['run_sql'], htmlencode($db->getName())) . "</b></legend>";
  1858. echo "<form action='?table=" . urlencode($target_table) . "&amp;action=table_sql' method='post'>";
  1859. echo $token_html;
  1860. if (isset($_SESSION['query_history']) && sizeof($_SESSION['query_history']) > 0) {
  1861. echo "<b>" . $lang['recent_queries'] . "</b><ul>";
  1862. foreach ($_SESSION['query_history'] as $key => $value) {
  1863. echo "<li><a onclick='document.getElementById(\"queryval\").value = this.textContent; return false;' href='#'>" . htmlencode($value) . "</a></li>";
  1864. }
  1865. echo "</ul><br/><br/>";
  1866. }
  1867. echo "<div style='float:left; width:70%;'>";
  1868. echo "<textarea style='width:97%; height:300px;' name='queryval' id='queryval' cols='50' rows='8'>" . htmlencode($queryStr) . "</textarea>";
  1869. echo "</div>";
  1870. echo "<div style='float:left; width:28%; padding-left:10px;'>";
  1871. echo $lang['fields'] . "<br/>";
  1872. echo "<select multiple='multiple' style='width:100%;' id='fieldcontainer'>";
  1873. $query = "PRAGMA table_info(" . $db->quote_id($target_table) . ")";
  1874. $result = $db->selectArray($query);
  1875. for ($i = 0; $i < sizeof($result); $i++) {
  1876. echo "<option value='" . htmlencode($result[$i][1]) . "'>" . htmlencode($result[$i][1]) . "</option>";
  1877. }
  1878. echo "</select>";
  1879. echo "<input type='button' value='&lt;&lt;' onclick='moveFields();' class='btn'/>";
  1880. echo "</div>";
  1881. echo "<div style='clear:both;'></div>";
  1882. echo $lang['delimit'] . " <input type='text' name='delimiter' value='" . htmlencode($delimiter) . "' style='width:50px;'/> ";
  1883. echo "<input type='submit' name='query' value='" . $lang['go'] . "' class='btn'/>";
  1884. echo "</form>";
  1885. echo "</fieldset>";
  1886. break;
  1887. //- Empty table (=table_empty)
  1888. case "table_empty":
  1889. echo "<form action='?action=table_empty&amp;confirm=1' method='post'>";
  1890. echo $token_html;
  1891. echo "<input type='hidden' name='tablename' value='" . htmlencode($target_table) . "'/>";
  1892. echo "<div class='confirm'>";
  1893. echo sprintf($lang['ques_empty'], htmlencode($target_table)) . "<br/><br/>";
  1894. echo "<input type='submit' value='" . $lang['confirm'] . "' class='btn'/> ";
  1895. echo "<a href='" . PAGE . "'>" . $lang['cancel'] . "</a>";
  1896. echo "</div>";
  1897. break;
  1898. //- Drop table (=table_drop)
  1899. case "table_drop":
  1900. echo "<form action='?action=table_drop&amp;confirm=1' method='post'>";
  1901. echo $token_html;
  1902. echo "<input type='hidden' name='tablename' value='" . htmlencode($target_table) . "'/>";
  1903. echo "<div class='confirm'>";
  1904. echo sprintf($lang['ques_drop'], htmlencode($target_table)) . "<br/><br/>";
  1905. echo "<input type='submit' value='" . $lang['confirm'] . "' class='btn'/> ";
  1906. echo "<a href='" . PAGE . "'>" . $lang['cancel'] . "</a>";
  1907. echo "</div>";
  1908. break;
  1909. //- Drop view (=view_drop)
  1910. case "view_drop":
  1911. echo "<form action='?action=view_drop&amp;confirm=1' method='post'>";
  1912. echo $token_html;
  1913. echo "<input type='hidden' name='viewname' value='" . htmlencode($target_table) . "'/>";
  1914. echo "<div class='confirm'>";
  1915. echo sprintf($lang['ques_drop_view'], htmlencode($target_table)) . "<br/><br/>";
  1916. echo "<input type='submit' value='" . $lang['confirm'] . "' class='btn'/> ";
  1917. echo "<a href='" . PAGE . "'>" . $lang['cancel'] . "</a>";
  1918. echo "</div>";
  1919. break;
  1920. //- Export table (=table_export)
  1921. case "table_export":
  1922. echo "<form method='post' action='" . PAGE . "'>";
  1923. echo $token_html;
  1924. echo "<fieldset style='float:left; width:260px; margin-right:20px;'><legend><b>" . $lang['export'] . "</b></legend>";
  1925. echo "<input type='hidden' value='" . htmlencode($target_table) . "' name='single_table'/>";
  1926. echo "<label><input type='radio' name='export_type' checked='checked' value='sql' onclick='toggleExports(\"sql\");'/> " . $lang['sql'] . "</label>";
  1927. echo "<br/><label><input type='radio' name='export_type' value='csv' onclick='toggleExports(\"csv\");'/> " . $lang['csv'] . "</label>";
  1928. echo "</fieldset>";
  1929. echo "<fieldset style='float:left; max-width:350px;' id='exportoptions_sql'><legend><b>" . $lang['options'] . "</b></legend>";
  1930. echo "<label><input type='checkbox' checked='checked' name='structure'/> " . $lang['export_struct'] . "</label> " . helpLink($lang['help5']) . "<br/>";
  1931. echo "<label><input type='checkbox' checked='checked' name='data'/> " . $lang['export_data'] . "</label> " . helpLink($lang['help6']) . "<br/>";
  1932. echo "<label><input type='checkbox' name='drop'/> " . $lang['add_drop'] . "</label> " . helpLink($lang['help7']) . "<br/>";
  1933. echo "<label><input type='checkbox' checked='checked' name='transaction'/> " . $lang['add_transact'] . "</label> " . helpLink($lang['help8']) . "<br/>";
  1934. echo "<label><input type='checkbox' checked='checked' name='comments'/> " . $lang['comments'] . "</label> " . helpLink($lang['help9']) . "<br/>";
  1935. echo "</fieldset>";
  1936. echo "<fieldset style='float:left; max-width:350px; display:none;' id='exportoptions_csv'><legend><b>" . $lang['options'] . "</b></legend>";
  1937. echo "<div style='float:left;'>" . $lang['fld_terminated'] . "</div>";
  1938. echo "<input type='text' value=';' name='export_csv_fieldsterminated' style='float:right;'/>";
  1939. echo "<div style='clear:both;'></div>";
  1940. echo "<div style='float:left;'>" . $lang['fld_enclosed'] . "</div>";
  1941. echo "<input type='text' value='\"' name='export_csv_fieldsenclosed' style='float:right;'/>";
  1942. echo "<div style='clear:both;'></div>";
  1943. echo "<div style='float:left;'>" . $lang['fld_escaped'] . "</div>";
  1944. echo "<input type='text' value='\' name='export_csv_fieldsescaped' style='float:right;'/>";
  1945. echo "<div style='clear:both;'></div>";
  1946. echo "<div style='float:left;'>" . $lang['rep_null'] . "</div>";
  1947. echo "<input type='text' value='NULL' name='export_csv_replacenull' style='float:right;'/>";
  1948. echo "<div style='clear:both;'></div>";
  1949. echo "<label><input type='checkbox' name='export_csv_crlf'/> " . $lang['rem_crlf'] . "</label><br/>";
  1950. echo "<label><input type='checkbox' checked='checked' name='export_csv_fieldnames'/> " . $lang['put_fld'] . "</label>";
  1951. echo "</fieldset>";
  1952. echo "<div style='clear:both;'></div>";
  1953. echo "<br/><br/>";
  1954. echo "<fieldset><legend><b>" . $lang['save_as'] . "</b></legend>";
  1955. $file = pathinfo($db->getPath());
  1956. $name = $file['filename'];
  1957. echo "<input type='text' name='filename' value='" . htmlencode($name) . "_" . htmlencode($target_table) . "_" . date("Y-m-d") . ".dump' style='width:400px;'/> <input type='submit' name='export' value='" . $lang['export'] . "' class='btn'/>";
  1958. echo "</fieldset>";
  1959. echo "</form>";
  1960. echo "<div class='confirm' style='margin-top: 2em'>" . sprintf($lang['backup_hint'], "<a href='?download=" . urlencode($currentDB['path']) . "&amp;token=" . urlencode($token) . "' title='" . $lang['backup'] . "'>" . $lang["backup_hint_linktext"] . "</a>") . "</div>";
  1961. break;
  1962. //- Import table (=table_import)
  1963. case "table_import":
  1964. if (isset($_POST['import'])) {
  1965. echo "<div class='confirm'>";
  1966. if ($importSuccess === true) {
  1967. echo $lang['import_suc'];
  1968. } else {
  1969. echo $lang['err'] . ': ' . htmlencode($importSuccess);
  1970. }
  1971. echo "</div><br/>";
  1972. }
  1973. echo "<form method='post' action='?table=" . urlencode($target_table) . "&amp;action=table_import' enctype='multipart/form-data'>";
  1974. echo $token_html;
  1975. echo "<fieldset style='float:left; width:260px; margin-right:20px;'><legend><b>" . $lang['import_into'] . " " . htmlencode($target_table) . "</b></legend>";
  1976. echo "<label><input type='radio' name='import_type' checked='checked' value='sql' onclick='toggleImports(\"sql\");'/> " . $lang['sql'] . "</label>";
  1977. echo "<br/><label><input type='radio' name='import_type' value='csv' onclick='toggleImports(\"csv\");'/> " . $lang['csv'] . "</label>";
  1978. echo "</fieldset>";
  1979. echo "<fieldset style='float:left; max-width:350px;' id='importoptions_sql'><legend><b>" . $lang['options'] . "</b></legend>";
  1980. echo $lang['no_opt'];
  1981. echo "</fieldset>";
  1982. echo "<fieldset style='float:left; max-width:350px; display:none;' id='importoptions_csv'><legend><b>" . $lang['options'] . "</b></legend>";
  1983. echo "<input type='hidden' value='" . htmlencode($target_table) . "' name='single_table'/>";
  1984. echo "<div style='float:left;'>" . $lang['fld_terminated'] . "</div>";
  1985. echo "<input type='text' value=';' name='import_csv_fieldsterminated' style='float:right;'/>";
  1986. echo "<div style='clear:both;'>";
  1987. echo "<div style='float:left;'>" . $lang['fld_enclosed'] . "</div>";
  1988. echo "<input type='text' value='\"' name='import_csv_fieldsenclosed' style='float:right;'/>";
  1989. echo "<div style='clear:both;'>";
  1990. echo "<div style='float:left;'>" . $lang['fld_escaped'] . "</div>";
  1991. echo "<input type='text' value='\' name='import_csv_fieldsescaped' style='float:right;'/>";
  1992. echo "<div style='clear:both;'>";
  1993. echo "<div style='float:left;'>" . $lang['rep_null'] . "</div>";
  1994. echo "<input type='text' value='NULL' name='import_csv_replacenull' style='float:right;'/>";
  1995. echo "<div style='clear:both;'>";
  1996. echo "<label><input type='checkbox' checked='checked' name='import_csv_fieldnames'/> " . $lang['fld_names'] . "</label>";
  1997. echo "</fieldset>";
  1998. echo "<div style='clear:both;'></div>";
  1999. echo "<br/><br/>";
  2000. echo "<fieldset><legend><b>" . $lang['import_f'] . "</b></legend>";
  2001. echo "<input type='file' value='" . $lang['choose_f'] . "' name='file' style='background-color:transparent; border-style:none;'/> <input type='submit' value='" . $lang['import'] . "' name='import' class='btn'/>";
  2002. echo "</fieldset>";
  2003. break;
  2004. //- Rename table (=table_rename)
  2005. case "table_rename":
  2006. echo "<form action='?action=table_rename&amp;confirm=1' method='post'>";
  2007. echo $token_html;
  2008. echo "<input type='hidden' name='oldname' value='" . htmlencode($target_table) . "'/>";
  2009. printf($lang['rename_tbl'], htmlencode($target_table));
  2010. echo " <input type='text' name='newname' style='width:200px;'/> <input type='submit' value='" . $lang['rename'] . "' name='rename' class='btn'/>";
  2011. echo "</form>";
  2012. break;
  2013. //- Search table (=table_search)
  2014. case "table_search":
  2015. $searchValues = array();
  2016. if (isset($_GET['done'])) {
  2017. $query = "PRAGMA table_info(" . $db->quote_id($target_table) . ")";
  2018. $result = $db->selectArray($query);
  2019. $primary_key = $db->getPrimaryKey($target_table);
  2020. $j = 0;
  2021. $arr = array();
  2022. for ($i = 0; $i < sizeof($result); $i++) {
  2023. $field = $result[$i][1];
  2024. $field_index = str_replace(" ", "_", $field);
  2025. $operator = $_POST[$field_index . ":operator"];
  2026. $value = $_POST[$field_index];
  2027. if ($value != "" || $operator == "!= ''" || $operator == "= ''" || $operator == 'IS NULL' || $operator == 'IS NOT NULL') {
  2028. if ($operator == "= ''" || $operator == "!= ''" || $operator == 'IS NULL' || $operator == 'IS NOT NULL') {
  2029. $arr[$j] = $db->quote_id($field) . " " . $operator;
  2030. } else {
  2031. if ($operator == "LIKE%") {
  2032. $operator = "LIKE";
  2033. if (!preg_match('/(^%)|(%$)/', $value)) {
  2034. $value = '%' . $value . '%';
  2035. }
  2036. $searchValues[$field] = array($value);
  2037. $value_quoted = $db->quote($value);
  2038. } elseif ($operator == 'IN' || $operator == 'NOT IN') {
  2039. $value = trim($value, '() ');
  2040. $values = explode(',', $value);
  2041. $values = array_map('trim', $values, array_fill(0, count($values), ' \'"'));
  2042. if ($operator == 'IN') {
  2043. $searchValues[$field] = $values;
  2044. }
  2045. $values = array_map(array($db, 'quote'), $values);
  2046. $value_quoted = '(' . implode(', ', $values) . ')';
  2047. } else {
  2048. $searchValues[$field] = array($value);
  2049. $value_quoted = $db->quote($value);
  2050. }
  2051. $arr[$j] = $db->quote_id($field) . " " . $operator . " " . $value_quoted;
  2052. }
  2053. $j++;
  2054. }
  2055. }
  2056. $query = "SELECT *";
  2057. // select the primary key column(s) last (ROWID if there is no PK).
  2058. // this will be used to identify rows, e.g. when editing/deleting rows
  2059. $primary_key = $db->getPrimaryKey($target_table);
  2060. foreach ($primary_key as $pk) {
  2061. $query .= ', ' . $db->quote_id($pk);
  2062. $query .= ', typeof(' . $db->quote_id($pk) . ')';
  2063. }
  2064. $query .= " FROM " . $db->quote_id($target_table);
  2065. $whereTo = '';
  2066. if (sizeof($arr) > 0) {
  2067. $whereTo .= " WHERE " . $arr[0];
  2068. for ($i = 1; $i < sizeof($arr); $i++) {
  2069. $whereTo .= " AND " . $arr[$i];
  2070. }
  2071. }
  2072. $query .= $whereTo;
  2073. $query_disp = "SELECT * FROM " . $db->quote_id($target_table) . $whereTo;
  2074. $queryTimer = new MicroTimer();
  2075. $arr = $db->selectArray($query);
  2076. $queryTimer->stop();
  2077. echo "<div class='confirm'>";
  2078. echo "<b>";
  2079. if ($arr !== false) {
  2080. $affected = sizeof($arr);
  2081. echo $lang['showing'] . " " . $affected . " " . $lang['rows'] . ". ";
  2082. printf($lang['query_time'], $queryTimer);
  2083. echo "</b><br/>";
  2084. } else {
  2085. echo $lang['err'] . ": " . htmlencode($db->getError()) . ".</b><br/>" . $lang['bug_report'] . ' ' . PROJECT_BUGTRACKER_LINK . '<br/>';
  2086. }
  2087. echo "<span style='font-size:11px;'>" . htmlencode($query_disp) . "</span>";
  2088. echo "</div><br/>";
  2089. if (sizeof($arr) > 0) {
  2090. if ($target_table_type == 'view') {
  2091. echo sprintf($lang['readonly_tbl'], htmlencode($target_table)) . " <a href='http://en.wikipedia.org/wiki/View_(database)' target='_blank'>http://en.wikipedia.org/wiki/View_(database)</a>";
  2092. echo "<br/><br/>";
  2093. }
  2094. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  2095. echo "<tr>";
  2096. if ($target_table_type == 'table') {
  2097. echo "<td colspan='2' class='tdheader' style='text-align:center'>";
  2098. #todo: make sure the search keywords are kept
  2099. #echo "<a href='?action=table_search&amp;done=1&amp;table=".$target_table."&amp;fulltexts=".($_SESSION[COOKIENAME.'fulltexts']?0:1)."' title='".$lang[($_SESSION[COOKIENAME.'fulltexts']?'no_full_texts':'full_texts')]."'>";
  2100. #echo "<b>&".($_SESSION[COOKIENAME.'fulltexts']?'r':'l')."arr;</b> T <b>&".($_SESSION[COOKIENAME.'fulltexts']?'l':'r')."arr;</b></a>";
  2101. echo "</td>";
  2102. }
  2103. $header = array();
  2104. for ($j = 0; $j < sizeof($result); $j++) {
  2105. $headers[$j] = $result[$j]['name'];
  2106. echo "<td class='tdheader'>";
  2107. echo htmlencode($headers[$j]);
  2108. echo "</td>";
  2109. }
  2110. echo "</tr>";
  2111. $pkFirstCol = sizeof($result) + 1;
  2112. for ($j = 0; $j < sizeof($arr); $j++) {
  2113. // -g-> $pk will always be the last columns in each row of the array because we are doing "SELECT *, PK_1, typeof(PK_1), PK2, typeof(PK_2), ... FROM ..."
  2114. $pk_arr = array();
  2115. for ($col = $pkFirstCol; array_key_exists($col, $arr[$j]); $col = $col + 2) {
  2116. // in $col we have the type and in $col-1 the value
  2117. if ($arr[$j][$col] == 'integer' || $arr[$j][$col] == 'real')
  2118. // json encode as int or float, not string
  2119. {
  2120. $pk_arr[] = $arr[$j][$col - 1] + 0;
  2121. } else
  2122. // encode as json string
  2123. {
  2124. $pk_arr[] = $arr[$j][$col - 1];
  2125. }
  2126. }
  2127. $pk = json_encode($pk_arr);
  2128. $tdWithClass = "<td class='td" . ($j % 2 ? "1" : "2") . "'>";
  2129. echo "<tr>";
  2130. if ($target_table_type == 'table') {
  2131. echo $tdWithClass . "<a href='?table=" . urlencode($target_table) . "&amp;action=row_editordelete&amp;pk=" . urlencode($pk) . "&amp;type=edit' title='" . $lang['edit'] . "' class='edit'><span>" . $lang['edit'] . "</span></a></td>";
  2132. echo $tdWithClass . "<a href='?table=" . urlencode($target_table) . "&amp;action=row_editordelete&amp;pk=" . urlencode($pk) . "&amp;type=delete' title='" . $lang['del'] . "' class='delete'><span>" . $lang['del'] . "</span></a></td>";
  2133. }
  2134. for ($z = 0; $z < sizeof($result); $z++) {
  2135. echo $tdWithClass;
  2136. $fldResult = $arr[$j][$headers[$z]];
  2137. if (isset($searchValues[$headers[$z]]) && is_array($searchValues[$headers[$z]])) {
  2138. foreach ($searchValues[$headers[$z]] as $searchValue) {
  2139. $foundVal = str_replace('%', '', $searchValue);
  2140. $fldResult = str_ireplace($foundVal, '[fnd]' . $foundVal . '[/fnd]', $fldResult);
  2141. // we replace with [fnd] first because we need to htmlencode _afterwards_ without breaking the found-markers
  2142. // htmlencoing _before_ would mean we might highlight stuff inside of htmlcode thus breaking it
  2143. }
  2144. }
  2145. echo str_replace(array('[fnd]', '[/fnd]'), array('<u class="found">', '</u>'), htmlencode($fldResult));
  2146. echo "</td>";
  2147. }
  2148. echo "</tr>";
  2149. }
  2150. echo "</table><br/><br/>";
  2151. }
  2152. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=table_search'>" . $lang['srch_again'] . "</a>";
  2153. } else {
  2154. $query = "PRAGMA table_info(" . $db->quote_id($target_table) . ")";
  2155. $result = $db->selectArray($query);
  2156. echo "<form action='?table=" . urlencode($target_table) . "&amp;action=table_search&amp;done=1' method='post'>";
  2157. echo $token_html;
  2158. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  2159. echo "<tr>";
  2160. echo "<td class='tdheader'>" . $lang['fld'] . "</td>";
  2161. echo "<td class='tdheader'>" . $lang['type'] . "</td>";
  2162. echo "<td class='tdheader'>" . $lang['operator'] . "</td>";
  2163. echo "<td class='tdheader'>" . $lang['val'] . "</td>";
  2164. echo "</tr>";
  2165. for ($i = 0; $i < sizeof($result); $i++) {
  2166. $field = $result[$i][1];
  2167. $type = $result[$i]['type'];
  2168. $typeAffinity = get_type_affinity($type);
  2169. $tdWithClass = "<td class='td" . ($i % 2 ? "1" : "2") . "'>";
  2170. $tdWithClassLeft = "<td class='td" . ($i % 2 ? "1" : "2") . "' style='text-align:left;'>";
  2171. echo "<tr>";
  2172. echo $tdWithClassLeft;
  2173. echo htmlencode($field);
  2174. echo "</td>";
  2175. echo $tdWithClassLeft;
  2176. echo htmlencode($type);
  2177. echo "</td>";
  2178. echo $tdWithClassLeft;
  2179. echo "<select name='" . htmlencode($field) . ":operator' onchange='checkLike(\"" . htmlencode($field) . "_search\", this.options[this.selectedIndex].value); '>";
  2180. echo "<option value='='>=</option>";
  2181. if ($typeAffinity == "INTEGER" || $typeAffinity == "REAL" || $typeAffinity == "NUMERIC") {
  2182. echo "<option value='&gt;'>&gt;</option>";
  2183. echo "<option value='&gt;='>&gt;=</option>";
  2184. echo "<option value='&lt;'>&lt;</option>";
  2185. echo "<option value='&lt;='>&lt;=</option>";
  2186. } else if ($typeAffinity == "TEXT" || $typeAffinity == "NONE") {
  2187. echo "<option value='= '''>= ''</option>";
  2188. echo "<option value='!= '''>!= ''</option>";
  2189. }
  2190. echo "<option value='!='>!=</option>";
  2191. if ($typeAffinity == "TEXT" || $typeAffinity == "NONE") {
  2192. echo "<option value='LIKE' selected='selected'>LIKE</option>";
  2193. } else {
  2194. echo "<option value='LIKE'>LIKE</option>";
  2195. }
  2196. echo "<option value='LIKE%'>LIKE %...%</option>";
  2197. echo "<option value='NOT LIKE'>NOT LIKE</option>";
  2198. echo "<option value='IN'>IN (..., ...)</option>";
  2199. echo "<option value='NOT IN'>NOT IN (..., ...)</option>";
  2200. echo "<option value='IS NULL'>IS NULL</option>";
  2201. echo "<option value='IS NOT NULL'>IS NOT NULL</option>";
  2202. echo "</select>";
  2203. echo "</td>";
  2204. echo $tdWithClassLeft;
  2205. if ($typeAffinity == "INTEGER" || $typeAffinity == "REAL" || $typeAffinity == "NUMERIC") {
  2206. echo "<input type='text' id='" . htmlencode($field) . "_search' name='" . htmlencode($field) . "'/>";
  2207. } else {
  2208. echo "<textarea id='" . htmlencode($field) . "_search' name='" . htmlencode($field) . "' rows='1' cols='60'></textarea>";
  2209. }
  2210. echo "</td>";
  2211. echo "</tr>";
  2212. }
  2213. echo "<tr>";
  2214. echo "<td class='tdheader' style='text-align:right;' colspan='4'>";
  2215. echo "<input type='submit' value='" . $lang['srch'] . "' class='btn'/>";
  2216. echo "</td>";
  2217. echo "</tr>";
  2218. echo "</table>";
  2219. echo "</form>";
  2220. }
  2221. break;
  2222. //- Row actions
  2223. //- View row (=row_view)
  2224. case "row_view":
  2225. if (!isset($_POST['startRow'])) {
  2226. $_POST['startRow'] = 0;
  2227. }
  2228. if (isset($_POST['numRows'])) {
  2229. $_SESSION[COOKIENAME . 'numRows'] = intval($_POST['numRows']);
  2230. }
  2231. if (!isset($_SESSION[COOKIENAME . 'numRows'])) {
  2232. $_SESSION[COOKIENAME . 'numRows'] = $rowsNum;
  2233. }
  2234. if (isset($_GET['fulltexts'])) {
  2235. $_SESSION[COOKIENAME . 'fulltexts'] = $_GET['fulltexts'];
  2236. }
  2237. if (!isset($_SESSION[COOKIENAME . 'fulltexts'])) {
  2238. $_SESSION[COOKIENAME . 'fulltexts'] = false;
  2239. }
  2240. if (isset($_SESSION[COOKIENAME . 'currentTable']) && $_SESSION[COOKIENAME . 'currentTable'] != $target_table) {
  2241. unset($_SESSION[COOKIENAME . 'sortRows']);
  2242. unset($_SESSION[COOKIENAME . 'orderRows']);
  2243. }
  2244. if (isset($_POST['viewtype'])) {
  2245. $_SESSION[COOKIENAME . 'viewtype'] = $_POST['viewtype'];
  2246. }
  2247. $rowCount = $db->numRows($target_table);
  2248. $lastPage = intval($rowCount / $_SESSION[COOKIENAME . 'numRows']);
  2249. $remainder = intval($rowCount % $_SESSION[COOKIENAME . 'numRows']);
  2250. if ($remainder == 0) {
  2251. $remainder = $_SESSION[COOKIENAME . 'numRows'];
  2252. }
  2253. //- HTML: pagination buttons
  2254. echo "<div style=''>";
  2255. //previous button
  2256. if ($_POST['startRow'] > 0) {
  2257. echo "<div style='float:left;'>";
  2258. echo "<form action='?action=row_view&amp;table=" . urlencode($target_table) . "' method='post'>";
  2259. echo $token_html;
  2260. echo "<input type='hidden' name='startRow' value='0'/>";
  2261. echo "<input type='hidden' name='numRows' value='" . $_SESSION[COOKIENAME . 'numRows'] . "'/> ";
  2262. echo "<input type='submit' value='&larr;&larr;' name='previous' class='btn'/> ";
  2263. echo "</form>";
  2264. echo "</div>";
  2265. echo "<div style='float:left; overflow:hidden; margin-right:20px;'>";
  2266. echo "<form action='?action=row_view&amp;table=" . urlencode($target_table) . "' method='post'>";
  2267. echo $token_html;
  2268. echo "<input type='hidden' name='startRow' value='" . max(0, intval($_POST['startRow'] - $_SESSION[COOKIENAME . 'numRows'])) . "'/>";
  2269. echo "<input type='hidden' name='numRows' value='" . $_SESSION[COOKIENAME . 'numRows'] . "'/> ";
  2270. echo "<input type='submit' value='&larr;' name='previous_full' class='btn'/> ";
  2271. echo "</form>";
  2272. echo "</div>";
  2273. }
  2274. //show certain number buttons
  2275. echo "<div style='float:left;'>";
  2276. echo "<form action='?action=row_view&amp;table=" . urlencode($target_table) . "' method='post'>";
  2277. echo $token_html;
  2278. echo "<input type='submit' value='" . $lang['show'] . " : ' name='show' class='btn'/> ";
  2279. echo "<input type='text' name='numRows' style='width:50px;' value='" . $_SESSION[COOKIENAME . 'numRows'] . "'/> ";
  2280. echo $lang['rows_records'];
  2281. if (intval($_POST['startRow'] + $_SESSION[COOKIENAME . 'numRows']) < $rowCount) {
  2282. echo "<input type='text' name='startRow' style='width:90px;' value='" . intval($_POST['startRow'] + $_SESSION[COOKIENAME . 'numRows']) . "'/>";
  2283. } else {
  2284. echo "<input type='text' name='startRow' style='width:90px;' value='0'/> ";
  2285. }
  2286. echo $lang['as_a'];
  2287. echo " <select name='viewtype'>";
  2288. if (!isset($_SESSION[COOKIENAME . 'viewtype']) || $_SESSION[COOKIENAME . 'viewtype'] == "table") {
  2289. echo "<option value='table' selected='selected'>" . $lang['tbl'] . "</option>";
  2290. echo "<option value='chart'>" . $lang['chart'] . "</option>";
  2291. } else {
  2292. echo "<option value='table'>" . $lang['tbl'] . "</option>";
  2293. echo "<option value='chart' selected='selected'>" . $lang['chart'] . "</option>";
  2294. }
  2295. echo "</select>";
  2296. echo "</form>";
  2297. echo "</div>";
  2298. //next button
  2299. if (intval($_POST['startRow'] + $_SESSION[COOKIENAME . 'numRows']) < $rowCount) {
  2300. echo "<div style='float:left; margin-left:20px; '>";
  2301. echo "<form action='?action=row_view&amp;table=" . urlencode($target_table) . "' method='post'>";
  2302. echo $token_html;
  2303. echo "<input type='hidden' name='startRow' value='" . intval($_POST['startRow'] + $_SESSION[COOKIENAME . 'numRows']) . "'/>";
  2304. echo "<input type='hidden' name='numRows' value='" . $_SESSION[COOKIENAME . 'numRows'] . "'/> ";
  2305. echo "<input type='submit' value='&rarr;' name='next' class='btn'/> ";
  2306. echo "</form>";
  2307. echo "</div>";
  2308. echo "<div style='float:left; '>";
  2309. echo "<form action='?action=row_view&amp;table=" . urlencode($target_table) . "' method='post'>";
  2310. echo $token_html;
  2311. echo "<input type='hidden' name='startRow' value='" . intval($rowCount - $remainder) . "'/>";
  2312. echo "<input type='hidden' name='numRows' value='" . $_SESSION[COOKIENAME . 'numRows'] . "'/> ";
  2313. echo "<input type='submit' value='&rarr;&rarr;' name='next_full' class='btn'/> ";
  2314. echo "</form>";
  2315. echo "</div>";
  2316. }
  2317. echo "<div style='clear:both;'></div>";
  2318. echo "</div>";
  2319. //- Query execution
  2320. if (!isset($_GET['sort'])) {
  2321. $_GET['sort'] = null;
  2322. }
  2323. if (!isset($_GET['order'])) {
  2324. $_GET['order'] = null;
  2325. }
  2326. $numRows = $_SESSION[COOKIENAME . 'numRows'];
  2327. $startRow = $_POST['startRow'];
  2328. if (isset($_GET['sort'])) {
  2329. $_SESSION[COOKIENAME . 'sortRows'] = $_GET['sort'];
  2330. $_SESSION[COOKIENAME . 'currentTable'] = $target_table;
  2331. }
  2332. if (isset($_GET['order'])) {
  2333. $_SESSION[COOKIENAME . 'orderRows'] = $_GET['order'];
  2334. $_SESSION[COOKIENAME . 'currentTable'] = $target_table;
  2335. }
  2336. $_SESSION[COOKIENAME . 'numRows'] = $numRows;
  2337. $query = "SELECT * ";
  2338. // select the primary key column(s) last (ROWID if there is no PK).
  2339. // this will be used to identify rows, e.g. when editing/deleting rows
  2340. $primary_key = $db->getPrimaryKey($target_table);
  2341. foreach ($primary_key as $pk) {
  2342. $query .= ', ' . $db->quote_id($pk);
  2343. $query .= ', typeof(' . $db->quote_id($pk) . ')';
  2344. }
  2345. $query .= " FROM " . $db->quote_id($target_table);
  2346. $queryDisp = "SELECT * FROM " . $db->quote_id($target_table);
  2347. $queryCount = "SELECT MIN(COUNT(*)," . $numRows . ") AS count FROM " . $db->quote_id($target_table);
  2348. $queryAdd = "";
  2349. if (isset($_SESSION[COOKIENAME . 'sortRows'])) {
  2350. $queryAdd .= " ORDER BY " . $db->quote_id($_SESSION[COOKIENAME . 'sortRows']);
  2351. }
  2352. if (isset($_SESSION[COOKIENAME . 'orderRows'])) {
  2353. $queryAdd .= " " . $_SESSION[COOKIENAME . 'orderRows'];
  2354. }
  2355. $queryAdd .= " LIMIT " . $startRow . ", " . $numRows;
  2356. $query .= $queryAdd;
  2357. $queryDisp .= $queryAdd;
  2358. $resultRows = $db->select($queryCount);
  2359. $resultRows = $resultRows['count'];
  2360. //- Show results
  2361. if ($resultRows > 0) {
  2362. $queryTimer = new MicroTimer();
  2363. $table_result = $db->query($query);
  2364. $queryTimer->stop();
  2365. echo "<br/><div class='confirm'>";
  2366. echo "<b>" . $lang['showing_rows'] . " " . $startRow . " - " . ($startRow + $resultRows - 1) . ", " . $lang['total'] . ": " . $rowCount . " ";
  2367. printf($lang['query_time'], $queryTimer);
  2368. echo "</b><br/>";
  2369. echo "<span style='font-size:11px;'>" . htmlencode($queryDisp) . "</span>";
  2370. echo "</div><br/>";
  2371. if ($target_table_type == 'view') {
  2372. echo sprintf($lang['readonly_tbl'], htmlencode($target_table)) . " <a href='http://en.wikipedia.org/wiki/View_(database)' target='_blank'>http://en.wikipedia.org/wiki/View_(database)</a>";
  2373. echo "<br/><br/>";
  2374. }
  2375. $query = "PRAGMA table_info(" . $db->quote_id($target_table) . ")";
  2376. $result = $db->selectArray($query);
  2377. $pkFirstCol = sizeof($result) + 1;
  2378. //- Table view
  2379. if (!isset($_SESSION[COOKIENAME . 'viewtype']) || $_SESSION[COOKIENAME . 'viewtype'] == "table") {
  2380. echo "<form action='?action=row_editordelete&amp;table=" . urlencode($target_table) . "' method='post' name='checkForm'>";
  2381. echo $token_html;
  2382. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  2383. echo "<tr>";
  2384. if ($target_table_type == 'table') {
  2385. echo "<td colspan='3' class='tdheader' style='text-align:center'>";
  2386. echo "<a href='?action=row_view&amp;table=" . $target_table . "&amp;fulltexts=" . ($_SESSION[COOKIENAME . 'fulltexts'] ? 0 : 1) . "' title='" . $lang[($_SESSION[COOKIENAME . 'fulltexts'] ? 'no_full_texts' : 'full_texts')] . "'>";
  2387. echo "<b>&" . ($_SESSION[COOKIENAME . 'fulltexts'] ? 'r' : 'l') . "arr;</b> T <b>&" . ($_SESSION[COOKIENAME . 'fulltexts'] ? 'l' : 'r') . "arr;</b></a>";
  2388. echo "</td>";
  2389. }
  2390. for ($i = 0; $i < sizeof($result); $i++) {
  2391. echo "<td class='tdheader'>";
  2392. echo "<a href='?action=row_view&amp;table=" . urlencode($target_table) . "&amp;sort=" . urlencode($result[$i]['name']);
  2393. if (isset($_SESSION[COOKIENAME . 'sortRows'])) {
  2394. $orderTag = ($_SESSION[COOKIENAME . 'sortRows'] == $result[$i]['name'] && $_SESSION[COOKIENAME . 'orderRows'] == "ASC") ? "DESC" : "ASC";
  2395. } else {
  2396. $orderTag = "ASC";
  2397. }
  2398. echo "&amp;order=" . $orderTag;
  2399. echo "'>" . htmlencode($result[$i]['name']) . "</a>";
  2400. if (isset($_SESSION[COOKIENAME . 'sortRows']) && $_SESSION[COOKIENAME . 'sortRows'] == $result[$i]['name']) {
  2401. echo (($_SESSION[COOKIENAME . 'orderRows'] == "ASC") ? " <b>&uarr;</b>" : " <b>&darr;</b>");
  2402. }
  2403. echo "</td>";
  2404. }
  2405. echo "</tr>";
  2406. for ($i = 0; $row = $db->fetch($table_result); $i++) {
  2407. // -g-> $pk will always be the last columns in each row of the array because we are doing "SELECT *, PK_1, typeof(PK_1), PK2, typeof(PK_2), ... FROM ..."
  2408. $pk_arr = array();
  2409. for ($col = $pkFirstCol; array_key_exists($col, $row); $col = $col + 2) {
  2410. // in $col we have the type and in $col-1 the value
  2411. if ($row[$col] == 'integer' || $row[$col] == 'real')
  2412. // json encode as int or float, not string
  2413. {
  2414. $pk_arr[] = $row[$col - 1] + 0;
  2415. } else
  2416. // encode as json string
  2417. {
  2418. $pk_arr[] = $row[$col - 1];
  2419. }
  2420. }
  2421. $pk = json_encode($pk_arr);
  2422. $tdWithClass = "<td class='td" . ($i % 2 ? "1" : "2") . "'>";
  2423. $tdWithClassLeft = "<td class='td" . ($i % 2 ? "1" : "2") . "' style='text-align:left;'>";
  2424. echo "<tr>";
  2425. if ($target_table_type == 'table') {
  2426. echo $tdWithClass;
  2427. echo "<input type='checkbox' name='check[]' value='" . htmlencode($pk) . "' id='check_" . htmlencode($i) . "'/>";
  2428. echo "</td>";
  2429. echo $tdWithClass;
  2430. // -g-> Here, we need to put the PK in as the link for both the edit and delete.
  2431. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=row_editordelete&amp;pk=" . urlencode($pk) . "&amp;type=edit' title='" . $lang['edit'] . "' class='edit'><span>" . $lang['edit'] . "</span></a>";
  2432. echo "</td>";
  2433. echo $tdWithClass;
  2434. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=row_editordelete&amp;pk=" . urlencode($pk) . "&amp;type=delete' title='" . $lang['del'] . "' class='delete'><span>" . $lang['del'] . "</span></a>";
  2435. echo "</td>";
  2436. }
  2437. for ($j = 0; $j < sizeof($result); $j++) {
  2438. $typeAffinity = get_type_affinity($result[$j]['type']);
  2439. if ($typeAffinity == "INTEGER" || $typeAffinity == "REAL" || $typeAffinity == "NUMERIC") {
  2440. echo $tdWithClass;
  2441. } else {
  2442. echo $tdWithClassLeft;
  2443. }
  2444. if ($row[$j] === "") {
  2445. echo "&nbsp;";
  2446. } elseif ($row[$j] === null) {
  2447. echo "<i class='null'>NULL</i>";
  2448. } else {
  2449. echo htmlencode(subString($row[$j]));
  2450. }
  2451. echo "</td>";
  2452. }
  2453. echo "</tr>";
  2454. }
  2455. echo "</table>";
  2456. if ($target_table_type == 'table') {
  2457. echo "<a onclick='checkAll()'>" . $lang['chk_all'] . "</a> / <a onclick='uncheckAll()'>" . $lang['unchk_all'] . "</a> <i>" . $lang['with_sel'] . ":</i> ";
  2458. echo "<select name='type'>";
  2459. echo "<option value='edit'>" . $lang['edit'] . "</option>";
  2460. echo "<option value='delete'>" . $lang['del'] . "</option>";
  2461. echo "</select> ";
  2462. echo "<input type='submit' value='" . $lang['go'] . "' name='massGo' class='btn'/>";
  2463. }
  2464. echo "</form>";
  2465. } else
  2466. //- Chart view
  2467. {
  2468. if (!isset($_SESSION[COOKIENAME . $target_table . 'chartlabels'])) {
  2469. // No label-column set. Try to pick a text-column as label-column.
  2470. for ($i = 0; $i < sizeof($result); $i++) {
  2471. if (get_type_affinity($result[$i]['type']) == 'TEXT') {
  2472. $_SESSION[COOKIENAME . $target_table . 'chartlabels'] = $i;
  2473. break;
  2474. }
  2475. }
  2476. }
  2477. if (!isset($_SESSION[COOKIENAME . 'chartlabels']))
  2478. // no text column found, use the first column
  2479. {
  2480. $_SESSION[COOKIENAME . 'chartlabels'] = 0;
  2481. }
  2482. if (!isset($_SESSION[COOKIENAME . $target_table . 'chartvalues'])) {
  2483. // No value-column set. Pick the first numeric column if possible.
  2484. // If not possible, pick the first column that is not the label-column.
  2485. $potential_value_column = null;
  2486. for ($i = 0; $i < sizeof($result); $i++) {
  2487. if ($potential_value_column === null && $i != $_SESSION[COOKIENAME . $target_table . 'chartlabels'])
  2488. // the first column (of any type) that is not the label-column
  2489. {
  2490. $potential_value_column = $i;
  2491. }
  2492. // check if the col is numeric
  2493. $typeAffinity = get_type_affinity($result[$i]['type']);
  2494. if ($typeAffinity == 'INTEGER' || $typeAffinity == 'REAL' || $typeAffinity == 'NUMERIC') {
  2495. // this is defined as a numeric column, so prefer this as a value column over $potential_value_column
  2496. $_SESSION[COOKIENAME . $target_table . 'chartvalues'] = $i;
  2497. break;
  2498. }
  2499. }
  2500. if (!isset($_SESSION[COOKIENAME . $target_table . 'chartvalues'])) {
  2501. // we did not find a numeric column
  2502. if ($potential_value_column !== null)
  2503. // use the $potential_value_column, i.e. the second column which is not the label-column
  2504. {
  2505. $_SESSION[COOKIENAME . $target_table . 'chartvalues'] = $potential_value_column;
  2506. } else
  2507. // it's hopeless, there is only 1 column
  2508. {
  2509. $_SESSION[COOKIENAME . $target_table . 'chartvalues'] = 0;
  2510. }
  2511. }
  2512. }
  2513. if (!isset($_SESSION[COOKIENAME . 'charttype'])) {
  2514. $_SESSION[COOKIENAME . 'charttype'] = 'bar';
  2515. }
  2516. if (isset($_POST['chartsettings'])) {
  2517. $_SESSION[COOKIENAME . 'charttype'] = $_POST['charttype'];
  2518. $_SESSION[COOKIENAME . $target_table . 'chartlabels'] = $_POST['chartlabels'];
  2519. $_SESSION[COOKIENAME . $target_table . 'chartvalues'] = $_POST['chartvalues'];
  2520. }
  2521. //- Chart javascript code
  2522. ?>
  2523. <script type='text/javascript' src='https://www.google.com/jsapi'></script>
  2524. <script type='text/javascript'>
  2525. google.load('visualization', '1.0', {'packages':['corechart']});
  2526. google.setOnLoadCallback(drawChart);
  2527. function drawChart()
  2528. {
  2529. var data = new google.visualization.DataTable();
  2530. data.addColumn('string', '<?php echo $result[$_SESSION[COOKIENAME . $target_table . 'chartlabels']]['name']; ?>');
  2531. data.addColumn('number', '<?php echo $result[$_SESSION[COOKIENAME . $target_table . 'chartvalues']]['name']; ?>');
  2532. data.addRows([
  2533. <?php
  2534. for ($i = 0; $row = $db->fetch($table_result); $i++) {
  2535. $label = str_replace("'", "", htmlencode($row[$_SESSION[COOKIENAME . $target_table . 'chartlabels']]));
  2536. $value = htmlencode($row[$_SESSION[COOKIENAME . $target_table . 'chartvalues']]);
  2537. if ($value == null || $value == "") {
  2538. $value = 0;
  2539. }
  2540. echo "['" . $label . "', " . $value . "]";
  2541. if ($i < $resultRows - 1) {
  2542. echo ",";
  2543. }
  2544. }
  2545. $height = ($resultRows + 1) * 30;
  2546. if ($height > 1000) {
  2547. $height = 1000;
  2548. } else if ($height < 300) {
  2549. $height = 300;
  2550. }
  2551. if ($_SESSION[COOKIENAME . 'charttype'] == "pie") {
  2552. $height = 800;
  2553. }
  2554. ?>
  2555. ]);
  2556. var chartWidth = document.getElementById("main_column").offsetWidth - document.getElementById("chartsettingsbox").offsetWidth - 100;
  2557. if(chartWidth>1000)
  2558. chartWidth = 1000;
  2559. var options =
  2560. {
  2561. 'width':chartWidth,
  2562. 'height':<?php echo $height; ?>,
  2563. 'title':'<?php echo $result[$_SESSION[COOKIENAME . $target_table . 'chartlabels']]['name'] . " vs " . $result[$_SESSION[COOKIENAME . $target_table . 'chartvalues']]['name']; ?>'
  2564. };
  2565. <?php
  2566. if ($_SESSION[COOKIENAME . 'charttype'] == "bar") {
  2567. echo "var chart = new google.visualization.BarChart(document.getElementById('chart_div'));";
  2568. } else if ($_SESSION[COOKIENAME . 'charttype'] == "pie") {
  2569. echo "var chart = new google.visualization.PieChart(document.getElementById('chart_div'));";
  2570. } else {
  2571. echo "var chart = new google.visualization.LineChart(document.getElementById('chart_div'));";
  2572. }
  2573. ?>
  2574. chart.draw(data, options);
  2575. }
  2576. </script>
  2577. <div id="chart_div" style="float:left;"><?php echo $lang['no_chart']; ?></div>
  2578. <?php
  2579. echo "<fieldset style='float:right; text-align:center;' id='chartsettingsbox'><legend><b>Chart Settings</b></legend>";
  2580. echo "<form action='?action=row_view&amp;table=" . urlencode($target_table) . "' method='post'>";
  2581. echo $token_html;
  2582. echo $lang['chart_type'] . ": <select name='charttype'>";
  2583. echo "<option value='bar'";
  2584. if ($_SESSION[COOKIENAME . 'charttype'] == "bar") {
  2585. echo " selected='selected'";
  2586. }
  2587. echo ">" . $lang['chart_bar'] . "</option>";
  2588. echo "<option value='pie'";
  2589. if ($_SESSION[COOKIENAME . 'charttype'] == "pie") {
  2590. echo " selected='selected'";
  2591. }
  2592. echo ">" . $lang['chart_pie'] . "</option>";
  2593. echo "<option value='line'";
  2594. if ($_SESSION[COOKIENAME . 'charttype'] == "line") {
  2595. echo " selected='selected'";
  2596. }
  2597. echo ">" . $lang['chart_line'] . "</option>";
  2598. echo "</select>";
  2599. echo "<br/><br/>";
  2600. echo $lang['lbl'] . ": <select name='chartlabels'>";
  2601. for ($i = 0; $i < sizeof($result); $i++) {
  2602. if (isset($_SESSION[COOKIENAME . $target_table . 'chartlabels']) && $_SESSION[COOKIENAME . $target_table . 'chartlabels'] == $i) {
  2603. echo "<option value='" . $i . "' selected='selected'>" . htmlencode($result[$i]['name']) . "</option>";
  2604. } else {
  2605. echo "<option value='" . $i . "'>" . htmlencode($result[$i]['name']) . "</option>";
  2606. }
  2607. }
  2608. echo "</select>";
  2609. echo "<br/><br/>";
  2610. echo $lang['val'] . ": <select name='chartvalues'>";
  2611. for ($i = 0; $i < sizeof($result); $i++) {
  2612. if (isset($_SESSION[COOKIENAME . $target_table . 'chartvalues']) && $_SESSION[COOKIENAME . $target_table . 'chartvalues'] == $i) {
  2613. echo "<option value='" . $i . "' selected='selected'>" . htmlencode($result[$i]['name']) . "</option>";
  2614. } else {
  2615. echo "<option value='" . $i . "'>" . htmlencode($result[$i]['name']) . "</option>";
  2616. }
  2617. }
  2618. echo "</select>";
  2619. echo "<br/><br/>";
  2620. echo "<input type='submit' name='chartsettings' value='" . $lang['update'] . "' class='btn'/>";
  2621. echo "</form>";
  2622. echo "</fieldset>";
  2623. echo "<div style='clear:both;'></div>";
  2624. //end chart view
  2625. }
  2626. } else if ($rowCount > 0) //no rows - do nothing
  2627. {
  2628. echo "<br/><br/>" . $lang['no_rows'];
  2629. } elseif ($target_table_type == 'table') {
  2630. echo "<br/><br/>" . $lang['empty_tbl'] . " <a href='?table=" . urlencode($target_table) . "&amp;action=row_create'>" . $lang['click'] . "</a> " . $lang['insert_rows'];
  2631. }
  2632. break;
  2633. //- Create new row (=row_create)
  2634. case "row_create":
  2635. $fieldStr = "";
  2636. echo "<form action='?table=" . urlencode($target_table) . "&amp;action=row_create' method='post'>";
  2637. echo $token_html;
  2638. echo $lang['restart_insert'];
  2639. echo " <select name='num'>";
  2640. for ($i = 1; $i <= 40; $i++) {
  2641. if (isset($_POST['num']) && $_POST['num'] == $i) {
  2642. echo "<option value='" . $i . "' selected='selected'>" . $i . "</option>";
  2643. } else {
  2644. echo "<option value='" . $i . "'>" . $i . "</option>";
  2645. }
  2646. }
  2647. echo "</select> ";
  2648. echo $lang['rows'];
  2649. echo " <input type='submit' value='" . $lang['go'] . "' class='btn'/>";
  2650. echo "</form>";
  2651. echo "<br/>";
  2652. $query = "PRAGMA table_info(" . $db->quote_id($target_table) . ")";
  2653. $result = $db->selectArray($query);
  2654. echo "<form action='?table=" . urlencode($target_table) . "&amp;action=row_create&amp;confirm=1' method='post'>";
  2655. echo $token_html;
  2656. if (isset($_POST['num'])) {
  2657. $num = $_POST['num'];
  2658. } else {
  2659. $num = 1;
  2660. }
  2661. echo "<input type='hidden' name='numRows' value='" . $num . "'/>";
  2662. for ($j = 0; $j < $num; $j++) {
  2663. if ($j > 0) {
  2664. echo "<label><input type='checkbox' value='ignore' name='" . $j . ":ignore' id='row_" . $j . "_ignore' checked='checked'/> " . $lang['ignore'] . "</label><br/>";
  2665. }
  2666. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  2667. echo "<tr>";
  2668. echo "<td class='tdheader'>" . $lang['fld'] . "</td>";
  2669. echo "<td class='tdheader'>" . $lang['type'] . "</td>";
  2670. echo "<td class='tdheader'>" . $lang['func'] . "</td>";
  2671. echo "<td class='tdheader'>Null</td>";
  2672. echo "<td class='tdheader'>" . $lang['val'] . "</td>";
  2673. echo "</tr>";
  2674. for ($i = 0; $i < sizeof($result); $i++) {
  2675. $field = $result[$i]['name'];
  2676. if ($j == 0) {
  2677. $fieldStr .= ":" . $field;
  2678. }
  2679. $type = strtolower($result[$i]['type']);
  2680. $typeAffinity = get_type_affinity($type);
  2681. $tdWithClass = "<td class='td" . ($i % 2 ? "1" : "2") . "'>";
  2682. $tdWithClassLeft = "<td class='td" . ($i % 2 ? "1" : "2") . "' style='text-align:left;'>";
  2683. echo "<tr>";
  2684. echo $tdWithClassLeft;
  2685. echo htmlencode($field);
  2686. echo "</td>";
  2687. echo $tdWithClassLeft;
  2688. echo htmlencode($type);
  2689. echo "</td>";
  2690. echo $tdWithClassLeft;
  2691. echo "<select name='function_" . $j . "_" . $i . "' onchange='notNull(\"row_" . $j . "_field_" . $i . "_null\");'>";
  2692. echo "<option value=''>&nbsp;</option>";
  2693. foreach (array_merge($sqlite_functions, $custom_functions) as $f) {
  2694. echo "<option value='" . htmlencode($f) . "'>" . htmlencode($f) . "</option>";
  2695. }
  2696. echo "</select>";
  2697. echo "</td>";
  2698. //we need to have a column dedicated to nulls -di
  2699. echo $tdWithClassLeft;
  2700. if ($result[$i]['notnull'] == 0) {
  2701. if ($result[$i]['dflt_value'] === "NULL") {
  2702. echo "<input type='checkbox' name='" . $j . ":" . $i . "_null' id='row_" . $j . "_field_" . $i . "_null' checked='checked' onclick='disableText(this, \"row_" . $j . "_field_" . $i . "_value\");'/>";
  2703. } else {
  2704. echo "<input type='checkbox' name='" . $j . ":" . $i . "_null' id='row_" . $j . "_field_" . $i . "_null' onclick='disableText(this, \"row_" . $j . "_field_" . $i . "_value\");'/>";
  2705. }
  2706. }
  2707. echo "</td>";
  2708. echo $tdWithClassLeft;
  2709. if ($result[$i]['dflt_value'] === "NULL") {
  2710. $dflt_value = "";
  2711. } else {
  2712. $dflt_value = htmlencode(deQuoteSQL($result[$i]['dflt_value']));
  2713. }
  2714. if ($typeAffinity == "INTEGER" || $typeAffinity == "REAL" || $typeAffinity == "NUMERIC") {
  2715. echo "<input type='text' id='row_" . $j . "_field_" . $i . "_value' name='" . $j . ":" . $i . "' value='" . $dflt_value . "' onblur='changeIgnore(this, \"row_" . $j . "_ignore\");' onclick='notNull(\"row_" . $j . "_field_" . $i . "_null\");'/>";
  2716. } else {
  2717. echo "<textarea id='row_" . $j . "_field_" . $i . "_value' name='" . $j . ":" . $i . "' rows='5' cols='60' onclick='notNull(\"row_" . $j . "_field_" . $i . "_null\");' onblur='changeIgnore(this, \"row_" . $j . "_ignore\");'>" . $dflt_value . "</textarea>";
  2718. }
  2719. echo "</td>";
  2720. echo "</tr>";
  2721. }
  2722. echo "<tr>";
  2723. echo "<td class='tdheader' style='text-align:right;' colspan='5'>";
  2724. echo "<input type='submit' value='" . $lang['insert'] . "' class='btn'/>";
  2725. echo "</td>";
  2726. echo "</tr>";
  2727. echo "</table><br/>";
  2728. }
  2729. $fieldStr = substr($fieldStr, 1);
  2730. echo "<input type='hidden' name='fields' value='" . htmlencode($fieldStr) . "'/>";
  2731. echo "</form>";
  2732. break;
  2733. //- Edit or delete row (=row_editordelete)
  2734. case "row_editordelete":
  2735. if (isset($_POST['check'])) {
  2736. $pks = $_POST['check'];
  2737. } else if (isset($_GET['pk'])) {
  2738. $pks = array($_GET['pk']);
  2739. } else {
  2740. $pks[0] = "";
  2741. }
  2742. $str = $pks[0];
  2743. for ($i = 1; $i < sizeof($pks); $i++) {
  2744. $str .= ", " . $pks[$i];
  2745. }
  2746. if ($str == "") //nothing was selected so show an error
  2747. {
  2748. echo "<div class='confirm'>";
  2749. echo $lang['err'] . ": " . $lang['no_sel'];
  2750. echo "</div>";
  2751. echo "<br/><br/><a href='?table=" . urlencode($target_table) . "&amp;action=row_view'>" . $lang['return'] . "</a>";
  2752. } else {
  2753. if ((isset($_POST['type']) && $_POST['type'] == "edit") || (isset($_GET['type']) && $_GET['type'] == "edit")) //edit
  2754. {
  2755. echo "<form action='?table=" . urlencode($target_table) . "&amp;action=row_edit&amp;confirm=1&amp;pk=" . urlencode(json_encode($pks)) . "' method='post'>";
  2756. echo $token_html;
  2757. $query = "PRAGMA table_info(" . $db->quote_id($target_table) . ")";
  2758. $result = $db->selectArray($query);
  2759. //build the POST array of fields
  2760. $fieldStr = $result[0][1];
  2761. for ($j = 1; $j < sizeof($result); $j++) {
  2762. $fieldStr .= ":" . $result[$j][1];
  2763. }
  2764. $primary_key = $db->getPrimaryKey($target_table);
  2765. echo "<input type='hidden' name='fieldArray' value='" . htmlencode($fieldStr) . "'/>";
  2766. for ($j = 0; $j < sizeof($pks); $j++) {
  2767. $query = "SELECT * FROM " . $db->quote_id($target_table) . " WHERE " . $db->wherePK($target_table, json_decode($pks[$j]));
  2768. $result1 = $db->select($query);
  2769. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  2770. echo "<tr>";
  2771. echo "<td class='tdheader'>" . $lang['fld'] . "</td>";
  2772. echo "<td class='tdheader'>" . $lang['type'] . "</td>";
  2773. echo "<td class='tdheader'>" . $lang['func'] . "</td>";
  2774. echo "<td class='tdheader'>Null</td>";
  2775. echo "<td class='tdheader'>" . $lang['val'] . "</td>";
  2776. echo "</tr>";
  2777. for ($i = 0; $i < sizeof($result); $i++) {
  2778. $field = $result[$i][1];
  2779. $type = $result[$i]['type'];
  2780. $typeAffinity = get_type_affinity($type);
  2781. $value = $result1[$i];
  2782. $tdWithClass = "<td class='td" . ($i % 2 ? "1" : "2") . "'>";
  2783. $tdWithClassLeft = "<td class='td" . ($i % 2 ? "1" : "2") . "' style='text-align:left;'>";
  2784. echo "<tr>";
  2785. echo $tdWithClass;
  2786. echo htmlencode($field);
  2787. echo "</td>";
  2788. echo $tdWithClass;
  2789. echo htmlencode($type);
  2790. echo "</td>";
  2791. echo $tdWithClassLeft;
  2792. echo "<select name='function_" . $i . "[]' onchange='notNull(\"" . $j . ":" . $i . "_null\");'>";
  2793. echo "<option value=''></option>";
  2794. foreach (array_merge($sqlite_functions, $custom_functions) as $f) {
  2795. echo "<option value='" . htmlencode($f) . "'>" . htmlencode($f) . "</option>";
  2796. }
  2797. echo "</select>";
  2798. echo "</td>";
  2799. echo $tdWithClassLeft;
  2800. if ($result[$i][3] == 0) {
  2801. if ($value === null) {
  2802. echo "<input type='checkbox' name='" . $i . "_null[]' id='" . $j . ":" . $i . "_null' checked='checked'/>";
  2803. } else {
  2804. echo "<input type='checkbox' name='" . $i . "_null[]' id='" . $j . ":" . $i . "_null'/>";
  2805. }
  2806. }
  2807. echo "</td>";
  2808. echo $tdWithClassLeft;
  2809. if ($typeAffinity == "INTEGER" || $typeAffinity == "REAL" || $typeAffinity == "NUMERIC") {
  2810. echo "<input type='text' name='" . $i . "[]' value='" . htmlencode($value) . "' onblur='changeIgnore(this, \"" . $j . "\", \"" . $j . ":" . $i . "_null\")' />";
  2811. } else {
  2812. echo "<textarea name='" . $i . "[]' rows='1' cols='60' class='" . htmlencode($field) . "_textarea' onblur='changeIgnore(this, \"" . $j . "\", \"" . $j . ":" . $i . "_null\")'>" . htmlencode($value) . "</textarea>";
  2813. }
  2814. echo "</td>";
  2815. echo "</tr>";
  2816. }
  2817. echo "<tr>";
  2818. echo "<td class='tdheader' style='text-align:right;' colspan='5'>";
  2819. // Note: the 'Save changes' button must be first in the code so it is the one used when submitting the form with the Enter key (issue #215)
  2820. echo "<input type='submit' value='" . $lang['save_ch'] . "' class='btn'/> ";
  2821. echo "<input type='submit' name='new_row' value='" . $lang['new_insert'] . "' class='btn'/> ";
  2822. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=row_view'>" . $lang['cancel'] . "</a>";
  2823. echo "</td>";
  2824. echo "</tr>";
  2825. echo "</table>";
  2826. echo "<br/>";
  2827. }
  2828. echo "</form>";
  2829. } else //delete
  2830. {
  2831. echo "<form action='?table=" . urlencode($target_table) . "&amp;action=row_delete&amp;confirm=1&amp;pk=" . urlencode(json_encode($pks)) . "' method='post'>";
  2832. echo $token_html;
  2833. echo "<div class='confirm'>";
  2834. printf($lang['ques_del_rows'], htmlencode($str), htmlencode($target_table));
  2835. echo "<br/><br/>";
  2836. echo "<input type='submit' value='" . $lang['confirm'] . "' class='btn'/> ";
  2837. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=row_view'>" . $lang['cancel'] . "</a>";
  2838. echo "</div>";
  2839. }
  2840. }
  2841. break;
  2842. //- Column actions
  2843. //- View table structure (=column_view)
  2844. case "column_view":
  2845. $query = "PRAGMA table_info(" . $db->quote_id($target_table) . ")";
  2846. $result = $db->selectArray($query);
  2847. echo "<form action='?table=" . urlencode($target_table) . "&amp;action=column_confirm' method='post' name='checkForm'>";
  2848. echo $token_html;
  2849. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  2850. echo "<tr>";
  2851. if ($target_table_type == 'table') {
  2852. echo "<td colspan='3'></td>";
  2853. }
  2854. echo "<td class='tdheader'>" . $lang['col'] . " #</td>";
  2855. echo "<td class='tdheader'>" . $lang['fld'] . "</td>";
  2856. echo "<td class='tdheader'>" . $lang['type'] . "</td>";
  2857. echo "<td class='tdheader'>" . $lang['not_null'] . "</td>";
  2858. echo "<td class='tdheader'>" . $lang['def_val'] . "</td>";
  2859. echo "<td class='tdheader'>" . $lang['prim_key'] . "</td>";
  2860. echo "</tr>";
  2861. $noPrimaryKey = true;
  2862. for ($i = 0; $i < sizeof($result); $i++) {
  2863. $colVal = $result[$i][0];
  2864. $fieldVal = $result[$i][1];
  2865. $typeVal = $result[$i]['type'];
  2866. $notnullVal = $result[$i][3];
  2867. $defaultVal = $result[$i][4];
  2868. $primarykeyVal = $result[$i][5];
  2869. if (intval($notnullVal) != 0) {
  2870. $notnullVal = $lang['yes'];
  2871. } else {
  2872. $notnullVal = $lang['no'];
  2873. }
  2874. if (intval($primarykeyVal) != 0) {
  2875. $primarykeyVal = $lang['yes'];
  2876. $noPrimaryKey = false;
  2877. } else {
  2878. $primarykeyVal = $lang['no'];
  2879. }
  2880. $tdWithClass = "<td class='td" . ($i % 2 ? "1" : "2") . "'>";
  2881. $tdWithClassLeft = "<td class='td" . ($i % 2 ? "1" : "2") . "' style='text-align:left;'>";
  2882. echo "<tr>";
  2883. if ($target_table_type == 'table') {
  2884. echo $tdWithClass;
  2885. echo "<input type='checkbox' name='check[]' value='" . htmlencode($fieldVal) . "' id='check_" . $i . "'/>";
  2886. echo "</td>";
  2887. echo $tdWithClass;
  2888. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=column_edit&amp;pk=" . urlencode($fieldVal) . "' title='" . $lang['edit'] . "' class='edit'><span>" . $lang['edit'] . "</span></a>";
  2889. echo "</td>";
  2890. echo $tdWithClass;
  2891. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=column_confirm&amp;action2=column_delete&amp;pk=" . urlencode($fieldVal) . "' title='" . $lang['del'] . "' class='delete'><span>" . $lang['del'] . "</span></a>";
  2892. echo "</td>";
  2893. }
  2894. echo $tdWithClass;
  2895. echo htmlencode($colVal);
  2896. echo "</td>";
  2897. echo $tdWithClassLeft;
  2898. echo htmlencode($fieldVal);
  2899. echo "</td>";
  2900. echo $tdWithClassLeft;
  2901. echo htmlencode($typeVal);
  2902. echo "</td>";
  2903. echo $tdWithClassLeft;
  2904. echo htmlencode($notnullVal);
  2905. echo "</td>";
  2906. echo $tdWithClassLeft;
  2907. if ($defaultVal === null) {
  2908. echo "<i class='null'>" . $lang['none'] . "</i>";
  2909. } elseif ($defaultVal === "NULL") {
  2910. echo "<i class='null'>NULL</i>";
  2911. } else {
  2912. echo htmlencode($defaultVal);
  2913. }
  2914. echo "</td>";
  2915. echo $tdWithClassLeft;
  2916. echo htmlencode($primarykeyVal);
  2917. echo "</td>";
  2918. echo "</tr>";
  2919. }
  2920. echo "</table>";
  2921. if ($target_table_type == 'table') {
  2922. echo "<a onclick='checkAll()'>" . $lang['chk_all'] . "</a> / <a onclick='uncheckAll()'>" . $lang['unchk_all'] . "</a> <i>" . $lang['with_sel'] . ":</i> ";
  2923. echo "<select name='action2'>";
  2924. //echo "<option value='edit'>".$lang['edit']."</option>";
  2925. echo "<option value='column_delete'>" . $lang['del'] . "</option>";
  2926. if ($noPrimaryKey) {
  2927. echo "<option value='primarykey_add'>" . $lang['prim_key'] . "</option>";
  2928. }
  2929. echo "</select> ";
  2930. echo "<input type='submit' value='" . $lang['go'] . "' name='massGo' class='btn'/>";
  2931. }
  2932. echo "</form>";
  2933. if ($target_table_type == 'table') {
  2934. echo "<br/>";
  2935. echo "<form action='?table=" . urlencode($target_table) . "&amp;action=column_create' method='post'>";
  2936. echo $token_html;
  2937. echo "<input type='hidden' name='tablename' value='" . htmlencode($target_table) . "'/>";
  2938. echo $lang['add'] . " <input type='text' name='tablefields' style='width:30px;' value='1'/> " . $lang['tbl_end'] . " <input type='submit' value='" . $lang['go'] . "' name='addfields' class='btn'/>";
  2939. echo "</form>";
  2940. }
  2941. $query = "SELECT sql FROM sqlite_master WHERE name=" . $db->quote($target_table);
  2942. $master = $db->selectArray($query);
  2943. echo "<br/>";
  2944. echo "<br/>";
  2945. echo "<div class='confirm'>";
  2946. echo "<b>" . $lang['query_used_' . $target_table_type] . "</b><br/>";
  2947. echo "<span style='font-size:11px;'>" . htmlencode($master[0]['sql']) . "</span>";
  2948. echo "</div>";
  2949. echo "<br/>";
  2950. if ($target_table_type != 'view') {
  2951. echo "<br/><hr/><br/>";
  2952. //$query = "SELECT * FROM sqlite_master WHERE type='index' AND tbl_name='".$target_table."'";
  2953. $query = "PRAGMA index_list(" . $db->quote_id($target_table) . ")";
  2954. $result = $db->selectArray($query);
  2955. if (sizeof($result) > 0) {
  2956. echo "<h2>" . $lang['indexes'] . ":</h2>";
  2957. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  2958. echo "<tr>";
  2959. echo "<td colspan='1'>";
  2960. echo "</td>";
  2961. echo "<td class='tdheader'>" . $lang['name'] . "</td>";
  2962. echo "<td class='tdheader'>" . $lang['unique'] . "</td>";
  2963. echo "<td class='tdheader'>" . $lang['seq_no'] . "</td>";
  2964. echo "<td class='tdheader'>" . $lang['col'] . " #</td>";
  2965. echo "<td class='tdheader'>" . $lang['fld'] . "</td>";
  2966. echo "</tr>";
  2967. for ($i = 0; $i < sizeof($result); $i++) {
  2968. if ($result[$i]['unique'] == 0) {
  2969. $unique = $lang['no'];
  2970. } else {
  2971. $unique = $lang['yes'];
  2972. }
  2973. $query = "PRAGMA index_info(" . $db->quote_id($result[$i]['name']) . ")";
  2974. $info = $db->selectArray($query);
  2975. $span = sizeof($info);
  2976. $tdWithClass = "<td class='td" . ($i % 2 ? "1" : "2") . "'>";
  2977. $tdWithClassLeft = "<td class='td" . ($i % 2 ? "1" : "2") . "' style='text-align:left;'>";
  2978. $tdWithClassSpan = "<td class='td" . ($i % 2 ? "1" : "2") . "' rowspan='" . $span . "'>";
  2979. $tdWithClassLeftSpan = "<td class='td" . ($i % 2 ? "1" : "2") . "' style='text-align:left;' rowspan='" . $span . "'>";
  2980. echo "<tr>";
  2981. echo $tdWithClassSpan;
  2982. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=index_delete&amp;pk=" . urlencode($result[$i]['name']) . "' title='" . $lang['del'] . "' class='delete'><span>" . $lang['del'] . "</span></a>";
  2983. echo "</td>";
  2984. echo $tdWithClassLeftSpan;
  2985. echo $result[$i]['name'];
  2986. echo "</td>";
  2987. echo $tdWithClassLeftSpan;
  2988. echo $unique;
  2989. echo "</td>";
  2990. for ($j = 0; $j < $span; $j++) {
  2991. if ($j != 0) {
  2992. echo "<tr>";
  2993. }
  2994. echo $tdWithClassLeft;
  2995. echo htmlencode($info[$j]['seqno']);
  2996. echo "</td>";
  2997. echo $tdWithClassLeft;
  2998. echo htmlencode($info[$j]['cid']);
  2999. echo "</td>";
  3000. echo $tdWithClassLeft;
  3001. echo htmlencode($info[$j]['name']);
  3002. echo "</td>";
  3003. echo "</tr>";
  3004. }
  3005. }
  3006. echo "</table><br/><br/>";
  3007. }
  3008. $query = "SELECT * FROM sqlite_master WHERE type='trigger' AND tbl_name=" . $db->quote($target_table) . " ORDER BY name";
  3009. $result = $db->selectArray($query);
  3010. //print_r($result);
  3011. if (sizeof($result) > 0) {
  3012. echo "<h2>" . $lang['triggers'] . ":</h2>";
  3013. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  3014. echo "<tr>";
  3015. echo "<td colspan='1'>";
  3016. echo "</td>";
  3017. echo "<td class='tdheader'>" . $lang['name'] . "</td>";
  3018. echo "<td class='tdheader'>" . $lang['sql'] . "</td>";
  3019. echo "</tr>";
  3020. for ($i = 0; $i < sizeof($result); $i++) {
  3021. $tdWithClass = "<td class='td" . ($i % 2 ? "1" : "2") . "'>";
  3022. echo "<tr>";
  3023. echo $tdWithClass;
  3024. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=trigger_delete&amp;pk=" . urlencode($result[$i]['name']) . "' title='" . $lang['del'] . "' class='delete'><span>" . $lang['del'] . "</span></a>";
  3025. echo "</td>";
  3026. echo $tdWithClass;
  3027. echo htmlencode($result[$i]['name']);
  3028. echo "</td>";
  3029. echo $tdWithClass;
  3030. echo htmlencode($result[$i]['sql']);
  3031. echo "</td>";
  3032. }
  3033. echo "</table><br/><br/>";
  3034. }
  3035. echo "<form action='?table=" . urlencode($target_table) . "&amp;action=index_create' method='post'>";
  3036. echo $token_html;
  3037. echo "<input type='hidden' name='tablename' value='" . htmlencode($target_table) . "'/>";
  3038. echo "<br/><div class='tdheader'>";
  3039. echo $lang['create_index2'] . " <input type='text' name='numcolumns' style='width:30px;' value='1'/> " . $lang['cols'] . " <input type='submit' value='" . $lang['go'] . "' name='addindex' class='btn'/>";
  3040. echo "</div>";
  3041. echo "</form>";
  3042. echo "<form action='?table=" . urlencode($target_table) . "&amp;action=trigger_create' method='post'>";
  3043. echo $token_html;
  3044. echo "<input type='hidden' name='tablename' value='" . htmlencode($target_table) . "'/>";
  3045. echo "<br/><div class='tdheader'>";
  3046. echo $lang['create_trigger2'] . " <input type='submit' value='" . $lang['go'] . "' name='addindex' class='btn'/>";
  3047. echo "</div>";
  3048. echo "</form>";
  3049. }
  3050. break;
  3051. //- Create column (=column_create)
  3052. case "column_create":
  3053. echo "<h2>" . sprintf($lang['new_fld'], htmlencode($_POST['tablename'])) . "</h2>";
  3054. if ($_POST['tablefields'] == "" || intval($_POST['tablefields']) <= 0) {
  3055. echo $lang['specify_fields'];
  3056. } else if ($_POST['tablename'] == "") {
  3057. echo $lang['specify_tbl'];
  3058. } else {
  3059. $num = intval($_POST['tablefields']);
  3060. $name = $_POST['tablename'];
  3061. echo "<form action='?table=" . urlencode($_POST['tablename']) . "&amp;action=column_create&amp;confirm=1' method='post'>";
  3062. echo $token_html;
  3063. echo "<input type='hidden' name='tablename' value='" . htmlencode($name) . "'/>";
  3064. echo "<input type='hidden' name='rows' value='" . $num . "'/>";
  3065. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  3066. echo "<tr>";
  3067. $headings = array($lang["fld"], $lang["type"], $lang["prim_key"]);
  3068. if ($db->getType() != "SQLiteDatabase") {
  3069. $headings[] = $lang["autoincrement"];
  3070. }
  3071. $headings[] = $lang["not_null"];
  3072. $headings[] = $lang["def_val"];
  3073. for ($k = 0; $k < count($headings); $k++) {
  3074. echo "<td class='tdheader'>" . $headings[$k] . "</td>";
  3075. }
  3076. echo "</tr>";
  3077. for ($i = 0; $i < $num; $i++) {
  3078. $tdWithClass = "<td class='td" . ($i % 2 ? "1" : "2") . "'>";
  3079. echo "<tr>";
  3080. echo $tdWithClass;
  3081. echo "<input type='text' name='" . $i . "_field' style='width:200px;'/>";
  3082. echo "</td>";
  3083. echo $tdWithClass;
  3084. echo "<select name='" . $i . "_type' id='i" . $i . "_type' onchange='toggleAutoincrement(" . $i . ");'>";
  3085. foreach ($sqlite_datatypes as $t) {
  3086. echo "<option value='" . htmlencode($t) . "'>" . htmlencode($t) . "</option>";
  3087. }
  3088. echo "</select>";
  3089. echo "</td>";
  3090. echo $tdWithClass;
  3091. echo "<label><input type='checkbox' name='" . $i . "_primarykey'/> " . $lang['yes'] . "</label>";
  3092. echo "</td>";
  3093. if ($db->getType() != "SQLiteDatabase") {
  3094. echo $tdWithClass;
  3095. echo "<label><input type='checkbox' name='" . $i . "_autoincrement' id='i" . $i . "_autoincrement'/> " . $lang['yes'] . "</label>";
  3096. echo "</td>";
  3097. }
  3098. echo $tdWithClass;
  3099. echo "<label><input type='checkbox' name='" . $i . "_notnull'/> " . $lang['yes'] . "</label>";
  3100. echo "</td>";
  3101. echo $tdWithClass;
  3102. echo "<select name='" . $i . "_defaultoption' id='i" . $i . "_defaultoption' onchange=\"if(this.value!='defined' && this.value!='expr') document.getElementById('i" . $i . "_defaultvalue').value='';\">";
  3103. echo "<option value='none'>" . $lang['none'] . "</option><option value='defined'>" . $lang['as_defined'] . ":</option><option>NULL</option><option>CURRENT_TIME</option><option>CURRENT_DATE</option><option>CURRENT_TIMESTAMP</option><option value='expr'>" . $lang['expression'] . ":</option>";
  3104. echo "</select>";
  3105. echo "<input type='text' name='" . $i . "_defaultvalue' id='i" . $i . "_defaultvalue' style='width:100px;' onchange=\"if(document.getElementById('i" . $i . "_defaultoption').value!='expr') document.getElementById('i" . $i . "_defaultoption').value='defined';\"/>";
  3106. echo "</td>";
  3107. echo "</tr>";
  3108. }
  3109. echo "<tr>";
  3110. echo "<td class='tdheader' style='text-align:right;' colspan='6'>";
  3111. echo "<input type='submit' value='" . $lang['add_flds'] . "' class='btn'/> ";
  3112. echo "<a href='?table=" . urlencode($_POST['tablename']) . "&amp;action=column_view'>" . $lang['cancel'] . "</a>";
  3113. echo "</td>";
  3114. echo "</tr>";
  3115. echo "</table>";
  3116. echo "</form>";
  3117. }
  3118. break;
  3119. //- Delete column (=column_confirm)
  3120. case "column_confirm":
  3121. if (isset($_POST['check'])) {
  3122. $pks = $_POST['check'];
  3123. } elseif (isset($_GET['pk'])) {
  3124. $pks = array($_GET['pk']);
  3125. } else {
  3126. $pks = array();
  3127. }
  3128. if (sizeof($pks) == 0) //nothing was selected so show an error
  3129. {
  3130. echo "<div class='confirm'>";
  3131. echo $lang['err'] . ": " . $lang['no_sel'];
  3132. echo "</div>";
  3133. echo "<br/><br/><a href='?table=" . urlencode($target_table) . "&amp;action=column_view'>" . $lang['return'] . "</a>";
  3134. } else {
  3135. $str = $pks[0];
  3136. $pkVal = $pks[0];
  3137. for ($i = 1; $i < sizeof($pks); $i++) {
  3138. $str .= ", " . $pks[$i];
  3139. $pkVal .= ":" . $pks[$i];
  3140. }
  3141. echo "<form action='?table=" . urlencode($target_table) . "&amp;action=" . urlencode($_REQUEST['action2']) . "&amp;confirm=1&amp;pk=" . urlencode($pkVal) . "' method='post'>";
  3142. echo $token_html;
  3143. echo "<div class='confirm'>";
  3144. printf($lang['ques_' . $_REQUEST['action2']], htmlencode($str), htmlencode($target_table));
  3145. echo "<br/><br/>";
  3146. echo "<input type='submit' value='" . $lang['confirm'] . "' class='btn'/> ";
  3147. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=column_view'>" . $lang['cancel'] . "</a>";
  3148. echo "</div>";
  3149. }
  3150. break;
  3151. //- Edit column (=column_edit)
  3152. case "column_edit":
  3153. echo "<h2>" . sprintf($lang['edit_col'], htmlencode($_GET['pk'])) . " " . $lang['on_tbl'] . " '" . htmlencode($target_table) . "'</h2>";
  3154. echo $lang['sqlite_limit'] . "<br/><br/>";
  3155. if (!isset($_GET['pk'])) {
  3156. echo $lang['specify_col'];
  3157. } else if (!$target_table) {
  3158. echo $lang['specify_tbl'];
  3159. } else {
  3160. $query = "PRAGMA table_info(" . $db->quote_id($target_table) . ")";
  3161. $result = $db->selectArray($query);
  3162. for ($i = 0; $i < sizeof($result); $i++) {
  3163. if ($result[$i][1] == $_GET['pk']) {
  3164. $colVal = $result[$i][0];
  3165. $fieldVal = $result[$i][1];
  3166. $typeVal = $result[$i]['type'];
  3167. $notnullVal = $result[$i][3];
  3168. $defaultVal = $result[$i][4];
  3169. $primarykeyVal = $result[$i][5];
  3170. break;
  3171. }
  3172. }
  3173. $name = $target_table;
  3174. echo "<form action='?table=" . urlencode($name) . "&amp;action=column_edit&amp;confirm=1' method='post'>";
  3175. echo $token_html;
  3176. echo "<input type='hidden' name='tablename' value='" . htmlencode($name) . "'/>";
  3177. echo "<input type='hidden' name='oldvalue' value='" . htmlencode($_GET['pk']) . "'/>";
  3178. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  3179. echo "<tr>";
  3180. //$headings = array("Field", "Type", "Primary Key", "Autoincrement", "Not NULL", "Default Value");
  3181. $headings = array($lang["fld"], $lang["type"]);
  3182. for ($k = 0; $k < count($headings); $k++) {
  3183. echo "<td class='tdheader'>" . $headings[$k] . "</td>";
  3184. }
  3185. echo "</tr>";
  3186. $i = 0;
  3187. $tdWithClass = "<td class='td" . ($i % 2 ? "1" : "2") . "'>";
  3188. echo "<tr>";
  3189. echo $tdWithClass;
  3190. echo "<input type='text' name='" . $i . "_field' style='width:200px;' value='" . htmlencode($fieldVal) . "'/>";
  3191. echo "</td>";
  3192. echo $tdWithClass;
  3193. echo "<select name='" . $i . "_type' id='i" . $i . "_type' onchange='toggleAutoincrement(" . $i . ");'>";
  3194. if (!in_array($typeVal, $sqlite_datatypes)) {
  3195. echo "<option value='" . htmlencode($typeVal) . "' selected='selected'>" . htmlencode($typeVal) . "</option>";
  3196. }
  3197. foreach ($sqlite_datatypes as $t) {
  3198. if ($t == $typeVal) {
  3199. echo "<option value='" . htmlencode($t) . "' selected='selected'>" . htmlencode($t) . "</option>";
  3200. } else {
  3201. echo "<option value='" . htmlencode($t) . "'>" . htmlencode($t) . "</option>";
  3202. }
  3203. }
  3204. echo "</select>";
  3205. echo "</td>";
  3206. /*
  3207. echo $tdWithClass;
  3208. if($primarykeyVal)
  3209. echo "<input type='checkbox' name='".$i."_primarykey' checked='checked'/> Yes";
  3210. else
  3211. echo "<input type='checkbox' name='".$i."_primarykey'/> Yes";
  3212. echo "</td>";
  3213. echo $tdWithClass;
  3214. if(1==2)
  3215. echo "<input type='checkbox' name='".$i."_autoincrement' id='".$i."_autoincrement' checked='checked'/> Yes";
  3216. else
  3217. echo "<input type='checkbox' name='".$i."_autoincrement' id='".$i."_autoincrement'/> Yes";
  3218. echo "</td>";
  3219. echo $tdWithClass;
  3220. if($notnullVal)
  3221. echo "<input type='checkbox' name='".$i."_notnull' checked='checked'/> Yes";
  3222. else
  3223. echo "<input type='checkbox' name='".$i."_notnull'/> Yes";
  3224. echo "</td>";
  3225. echo $tdWithClass;
  3226. echo "<input type='text' name='".$i."_defaultvalue' value='".$defaultVal."' style='width:100px;'/>";
  3227. echo "</td>";
  3228. */
  3229. echo "</tr>";
  3230. echo "<tr>";
  3231. echo "<td class='tdheader' style='text-align:right;' colspan='6'>";
  3232. echo "<input type='submit' value='" . $lang['save_ch'] . "' class='btn'/> ";
  3233. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=column_view'>" . $lang['cancel'] . "</a>";
  3234. echo "</td>";
  3235. echo "</tr>";
  3236. echo "</table>";
  3237. echo "</form>";
  3238. }
  3239. break;
  3240. //- Delete index (=index_delete)
  3241. case "index_delete":
  3242. echo "<form action='?table=" . urlencode($target_table) . "&amp;action=index_delete&amp;pk=" . urlencode($_GET['pk']) . "&amp;confirm=1' method='post'>";
  3243. echo $token_html;
  3244. echo "<div class='confirm'>";
  3245. echo sprintf($lang['ques_del_index'], htmlencode($_GET['pk'])) . "<br/><br/>";
  3246. echo "<input type='submit' value='" . $lang['confirm'] . "' class='btn'/> ";
  3247. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=column_view'>" . $lang['cancel'] . "</a>";
  3248. echo "</div>";
  3249. echo "</form>";
  3250. break;
  3251. //- Delete trigger (=trigger_delete)
  3252. case "trigger_delete":
  3253. echo "<form action='?table=" . urlencode($target_table) . "&amp;action=trigger_delete&amp;pk=" . urlencode($_GET['pk']) . "&amp;confirm=1' method='post'>";
  3254. echo $token_html;
  3255. echo "<div class='confirm'>";
  3256. echo sprintf($lang['ques_del_trigger'], htmlencode($_GET['pk'])) . "<br/><br/>";
  3257. echo "<input type='submit' value='" . $lang['confirm'] . "' class='btn'/> ";
  3258. echo "<a href='?table=" . urlencode($target_table) . "&amp;action=column_view'>" . $lang['cancel'] . "</a>";
  3259. echo "</div>";
  3260. echo "</form>";
  3261. break;
  3262. //- Create trigger (=trigger_create)
  3263. case "trigger_create":
  3264. echo "<h2>" . $lang['create_trigger'] . " '" . htmlencode($_POST['tablename']) . "'</h2>";
  3265. if ($_POST['tablename'] == "") {
  3266. echo $lang['specify_tbl'];
  3267. } else {
  3268. echo "<form action='?table=" . urlencode($_POST['tablename']) . "&amp;action=trigger_create&amp;confirm=1' method='post'>";
  3269. echo $token_html;
  3270. echo $lang['trigger_name'] . ": <input type='text' name='trigger_name'/><br/><br/>";
  3271. echo "<fieldset><legend>" . $lang['db_event'] . "</legend>";
  3272. echo $lang['before'] . "/" . $lang['after'] . ": ";
  3273. echo "<select name='beforeafter'>";
  3274. echo "<option value=''></option>";
  3275. echo "<option value='BEFORE'>" . $lang['before'] . "</option>";
  3276. echo "<option value='AFTER'>" . $lang['after'] . "</option>";
  3277. echo "<option value='INSTEAD OF'>" . $lang['instead'] . "</option>";
  3278. echo "</select>";
  3279. echo "<br/><br/>";
  3280. echo $lang['event'] . ": ";
  3281. echo "<select name='event'>";
  3282. echo "<option value='DELETE'>" . $lang['del'] . "</option>";
  3283. echo "<option value='INSERT'>" . $lang['insert'] . "</option>";
  3284. echo "<option value='UPDATE'>" . $lang['update'] . "</option>";
  3285. echo "</select>";
  3286. echo "</fieldset><br/><br/>";
  3287. echo "<fieldset><legend>" . $lang['trigger_act'] . "</legend>";
  3288. echo "<label><input type='checkbox' name='foreachrow'/> " . $lang['each_row'] . "</label><br/><br/>";
  3289. echo $lang['when_exp'] . ":<br/>";
  3290. echo "<textarea name='whenexpression' style='width:500px; height:100px;' rows='8' cols='50'></textarea>";
  3291. echo "<br/><br/>";
  3292. echo $lang['trigger_step'] . ":<br/>";
  3293. echo "<textarea name='triggersteps' style='width:500px; height:100px;' rows='8' cols='50'></textarea>";
  3294. echo "</fieldset><br/><br/>";
  3295. echo "<input type='submit' value='" . $lang['create_trigger2'] . "' class='btn'/> ";
  3296. echo "<a href='?table=" . urlencode($_POST['tablename']) . "&amp;action=column_view'>" . $lang['cancel'] . "</a>";
  3297. echo "</form>";
  3298. }
  3299. break;
  3300. //- Create index (=index_create)
  3301. case "index_create":
  3302. echo "<h2>" . $lang['create_index'] . " '" . htmlencode($_POST['tablename']) . "'</h2>";
  3303. if ($_POST['numcolumns'] == "" || intval($_POST['numcolumns']) <= 0) {
  3304. echo $lang['specify_fields'];
  3305. } else if ($_POST['tablename'] == "") {
  3306. echo $lang['specify_tbl'];
  3307. } else {
  3308. echo "<form action='?table=" . urlencode($_POST['tablename']) . "&amp;action=index_create&amp;confirm=1' method='post'>";
  3309. echo $token_html;
  3310. $num = intval($_POST['numcolumns']);
  3311. $query = "PRAGMA table_info(" . $db->quote_id($_POST['tablename']) . ")";
  3312. $result = $db->selectArray($query);
  3313. echo "<fieldset><legend>" . $lang['define_index'] . "</legend>";
  3314. echo "<label for='index_name'>" . $lang['index_name'] . ":</label> <input type='text' name='name' id='index_name'/><br/>";
  3315. echo "<label for='index_duplicate'>" . $lang['dup_val'] . ":</label>";
  3316. echo "<select name='duplicate' id='index_duplicate'>";
  3317. echo "<option value='yes'>" . $lang['allow'] . "</option>";
  3318. echo "<option value='no'>" . $lang['not_allow'] . "</option>";
  3319. echo "</select><br/>";
  3320. if (version_compare($db->getSQLiteVersion(), '3.8.0') >= 0) {
  3321. echo "<label for='index_where'>WHERE:</label> <input type='text' name='where' id='index_where'/> " . helpLink($lang['help10']);
  3322. }
  3323. echo "</fieldset>";
  3324. echo "<br/>";
  3325. echo "<fieldset><legend>" . $lang['define_in_col'] . "</legend>";
  3326. for ($i = 0; $i < $num; $i++) {
  3327. echo "<select name='" . $i . "_field'>";
  3328. echo "<option value=''>--" . $lang['ignore'] . "--</option>";
  3329. for ($j = 0; $j < sizeof($result); $j++) {
  3330. echo "<option value='" . htmlencode($result[$j][1]) . "'>" . htmlencode($result[$j][1]) . "</option>";
  3331. }
  3332. echo "</select> ";
  3333. echo "<select name='" . $i . "_order'>";
  3334. echo "<option value=''></option>";
  3335. echo "<option value=' ASC'>" . $lang['asc'] . "</option>";
  3336. echo "<option value=' DESC'>" . $lang['desc'] . "</option>";
  3337. echo "</select><br/>";
  3338. }
  3339. echo "</fieldset>";
  3340. echo "<br/><br/>";
  3341. echo "<input type='hidden' name='num' value='" . $num . "'/>";
  3342. echo "<input type='submit' value='" . $lang['create_index1'] . "' class='btn'/> ";
  3343. echo "<a href='?table=" . urlencode($_POST['tablename']) . "&amp;action=column_view'>" . $lang['cancel'] . "</a>";
  3344. echo "</form>";
  3345. }
  3346. break;
  3347. }
  3348. echo "</div>";
  3349. }
  3350. $view = "structure";
  3351. //- HMTL: tabs for databases
  3352. if (!$target_table && !isset($_GET['confirm']) && (!isset($_GET['action']) || (isset($_GET['action']) && $_GET['action'] != "table_create"))) //the absence of these fields means we are viewing the database homepage
  3353. {
  3354. $view = isset($_GET['view']) ? $_GET['view'] : 'structure';
  3355. echo "<a href='?view=structure' ";
  3356. if ($view == "structure") {
  3357. echo "class='tab_pressed'";
  3358. } else {
  3359. echo "class='tab'";
  3360. }
  3361. echo ">" . $lang['struct'] . "</a>";
  3362. echo "<a href='?view=sql' ";
  3363. if ($view == "sql") {
  3364. echo "class='tab_pressed'";
  3365. } else {
  3366. echo "class='tab'";
  3367. }
  3368. echo ">" . $lang['sql'] . "</a>";
  3369. echo "<a href='?view=export' ";
  3370. if ($view == "export") {
  3371. echo "class='tab_pressed'";
  3372. } else {
  3373. echo "class='tab'";
  3374. }
  3375. echo ">" . $lang['export'] . "</a>";
  3376. echo "<a href='?view=import' ";
  3377. if ($view == "import") {
  3378. echo "class='tab_pressed'";
  3379. } else {
  3380. echo "class='tab'";
  3381. }
  3382. echo ">" . $lang['import'] . "</a>";
  3383. echo "<a href='?view=vacuum' ";
  3384. if ($view == "vacuum") {
  3385. echo "class='tab_pressed'";
  3386. } else {
  3387. echo "class='tab'";
  3388. }
  3389. echo ">" . $lang['vac'] . "</a>";
  3390. if ($directory !== false && is_writable($directory)) {
  3391. echo "<a href='?view=rename' ";
  3392. if ($view == "rename") {
  3393. echo "class='tab_pressed'";
  3394. } else {
  3395. echo "class='tab'";
  3396. }
  3397. echo ">" . $lang['db_rename'] . "</a>";
  3398. echo "<a href='?view=delete' title='" . $lang['db_del'] . "' ";
  3399. if ($view == "delete") {
  3400. echo "class='tab_pressed delete_db'";
  3401. } else {
  3402. echo "class='tab delete_db'";
  3403. }
  3404. echo "><span>" . $lang['db_del'] . "</span></a>";
  3405. }
  3406. echo "<div style='clear:both;'></div>";
  3407. echo "<div id='main'>";
  3408. //- Switch on $view (actually a series of if-else)
  3409. if ($view == "structure") {
  3410. //- Database structure, shows all the tables (=structure)
  3411. if (isset($dbexists)) {
  3412. echo "<div class='confirm' style='margin:10px 20px;'>";
  3413. echo $lang['err'] . ': ' . sprintf($lang['db_exists'], htmlencode($dbname));
  3414. echo "</div><br/>";
  3415. }
  3416. if ($db->isWritable() && !$db->isDirWritable()) {
  3417. echo "<div class='confirm' style='margin:10px 20px;'>";
  3418. echo $lang['attention'] . ': ' . $lang['directory_not_writable'];
  3419. echo "</div><br/>";
  3420. }
  3421. if (isset($extension_not_allowed)) {
  3422. echo "<div class='confirm' style='margin:10px 20px;'>";
  3423. echo $lang['extension_not_allowed'] . ': ';
  3424. echo implode(', ', array_map('htmlencode', $allowed_extensions));
  3425. echo '<br />' . $lang['add_allowed_extension'];
  3426. echo "</div><br/>";
  3427. }
  3428. if ($auth->isPasswordDefault()) {
  3429. echo "<div class='confirm' style='margin:20px 0px;'>";
  3430. echo sprintf($lang['warn_passwd'], (is_readable('phpliteadmin.config.php') ? 'phpliteadmin.config.php' : PAGE)) . "<br />" . $lang['warn0'];
  3431. echo "</div>";
  3432. }
  3433. echo "<b>" . $lang['db_name'] . "</b>: " . htmlencode($db->getName()) . "<br/>";
  3434. echo "<b>" . $lang['db_path'] . "</b>: " . htmlencode($db->getPath()) . "<br/>";
  3435. echo "<b>" . $lang['db_size'] . "</b>: " . $db->getSize() . " KB<br/>";
  3436. echo "<b>" . $lang['db_mod'] . "</b>: " . $db->getDate() . "<br/>";
  3437. echo "<b>" . $lang['sqlite_v'] . "</b>: " . $db->getSQLiteVersion() . "<br/>";
  3438. echo "<b>" . $lang['sqlite_ext'] . "</b> " . helpLink($lang['help1']) . ": " . $db->getType() . "<br/>";
  3439. echo "<b>" . $lang['php_v'] . "</b>: " . phpversion() . "<br/>";
  3440. echo "<b>" . PROJECT . " " . $lang["ver"] . "</b>: " . VERSION;
  3441. echo " <a href='" . PROJECT_URL . "' target='_blank' id='oldVersion' style='display: none;' class='warning'>" . $lang['new_version'] . "</a><br/><br/>";
  3442. echo "<script type='text/javascript'>checkVersion('" . VERSION . "','" . VERSION_CHECK_URL . "');</script>";
  3443. if (isset($_GET['sort']) && ($_GET['sort'] == 'type' || $_GET['sort'] == 'name')) {
  3444. $_SESSION[COOKIENAME . 'sortTables'] = $_GET['sort'];
  3445. }
  3446. if (isset($_GET['order']) && ($_GET['order'] == 'ASC' || $_GET['order'] == 'DESC')) {
  3447. $_SESSION[COOKIENAME . 'orderTables'] = $_GET['order'];
  3448. }
  3449. $query = "SELECT type, name FROM sqlite_master WHERE (type='table' OR type='view') AND name!='' AND name NOT LIKE 'sqlite_%'";
  3450. $queryAdd = "";
  3451. if (isset($_SESSION[COOKIENAME . 'sortTables'])) {
  3452. $queryAdd .= " ORDER BY " . $db->quote_id($_SESSION[COOKIENAME . 'sortTables']);
  3453. } else {
  3454. $queryAdd .= " ORDER BY \"name\"";
  3455. }
  3456. if (isset($_SESSION[COOKIENAME . 'orderTables'])) {
  3457. $queryAdd .= " " . $_SESSION[COOKIENAME . 'orderTables'];
  3458. }
  3459. $query .= $queryAdd;
  3460. $result = $db->selectArray($query);
  3461. if (sizeof($result) == 0) {
  3462. echo $lang['no_tbl'] . "<br/><br/>";
  3463. } else {
  3464. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  3465. echo "<tr>";
  3466. echo "<td class='tdheader'>";
  3467. echo "<a href='?sort=type";
  3468. if (isset($_SESSION[COOKIENAME . 'sortTables'])) {
  3469. $orderTag = ($_SESSION[COOKIENAME . 'sortTables'] == "type" && $_SESSION[COOKIENAME . 'orderTables'] == "ASC") ? "DESC" : "ASC";
  3470. } else {
  3471. $orderTag = "ASC";
  3472. }
  3473. echo "&amp;order=" . $orderTag;
  3474. echo "'>" . $lang['type'] . "</a> " . helpLink($lang['help3']);
  3475. if (isset($_SESSION[COOKIENAME . 'sortTables']) && $_SESSION[COOKIENAME . 'sortTables'] == "type") {
  3476. echo (($_SESSION[COOKIENAME . 'orderTables'] == "ASC") ? " <b>&uarr;</b>" : " <b>&darr;</b>");
  3477. }
  3478. echo "</td>";
  3479. echo "<td class='tdheader'>";
  3480. echo "<a href='?sort=name";
  3481. if (isset($_SESSION[COOKIENAME . 'sortTables'])) {
  3482. $orderTag = ($_SESSION[COOKIENAME . 'sortTables'] == "name" && $_SESSION[COOKIENAME . 'orderTables'] == "ASC") ? "DESC" : "ASC";
  3483. } else {
  3484. $orderTag = "ASC";
  3485. }
  3486. echo "&amp;order=" . $orderTag;
  3487. echo "'>" . $lang['name'] . "</a>";
  3488. if (isset($_SESSION[COOKIENAME . 'sortTables']) && $_SESSION[COOKIENAME . 'sortTables'] == "name") {
  3489. echo (($_SESSION[COOKIENAME . 'orderTables'] == "ASC") ? " <b>&uarr;</b>" : " <b>&darr;</b>");
  3490. }
  3491. echo "</td>";
  3492. echo "<td class='tdheader' colspan='10'>" . $lang['act'] . "</td>";
  3493. echo "<td class='tdheader'>" . $lang['rec'] . "</td>";
  3494. echo "</tr>";
  3495. $totalRecords = 0;
  3496. $skippedTables = false;
  3497. for ($i = 0; $i < sizeof($result); $i++) {
  3498. $records = $db->numRows($result[$i]['name'], (!isset($_GET['forceCount'])));
  3499. if ($records == '?') {
  3500. $skippedTables = true;
  3501. $records = "<a href='?forceCount=1'>?</a>";
  3502. } else {
  3503. $totalRecords += $records;
  3504. }
  3505. $tdWithClass = "<td class='td" . ($i % 2 ? "1" : "2") . "'>";
  3506. $tdWithClassLeft = "<td class='td" . ($i % 2 ? "1" : "2") . "' style='text-align:left;'>";
  3507. if ($result[$i]['type'] == "table") {
  3508. echo "<tr>";
  3509. echo $tdWithClassLeft;
  3510. echo $lang['tbl'];
  3511. echo "</td>";
  3512. echo $tdWithClassLeft;
  3513. echo "<a href='?table=" . urlencode($result[$i]['name']) . "&amp;action=row_view'>" . htmlencode($result[$i]['name']) . "</a>";
  3514. echo "</td>";
  3515. echo $tdWithClass;
  3516. echo "<a href='?table=" . urlencode($result[$i]['name']) . "&amp;action=row_view'>" . $lang['browse'] . "</a>";
  3517. echo "</td>";
  3518. echo $tdWithClass;
  3519. echo "<a href='?table=" . urlencode($result[$i]['name']) . "&amp;action=column_view'>" . $lang['struct'] . "</a>";
  3520. echo "</td>";
  3521. echo $tdWithClass;
  3522. echo "<a href='?table=" . urlencode($result[$i]['name']) . "&amp;action=table_sql'>" . $lang['sql'] . "</a>";
  3523. echo "</td>";
  3524. echo $tdWithClass;
  3525. echo "<a href='?table=" . urlencode($result[$i]['name']) . "&amp;action=table_search'>" . $lang['srch'] . "</a>";
  3526. echo "</td>";
  3527. echo $tdWithClass;
  3528. echo "<a href='?table=" . urlencode($result[$i]['name']) . "&amp;action=row_create'>" . $lang['insert'] . "</a>";
  3529. echo "</td>";
  3530. echo $tdWithClass;
  3531. echo "<a href='?table=" . urlencode($result[$i]['name']) . "&amp;action=table_export'>" . $lang['export'] . "</a>";
  3532. echo "</td>";
  3533. echo $tdWithClass;
  3534. echo "<a href='?table=" . urlencode($result[$i]['name']) . "&amp;action=table_import'>" . $lang['import'] . "</a>";
  3535. echo "</td>";
  3536. echo $tdWithClass;
  3537. echo "<a href='?table=" . urlencode($result[$i]['name']) . "&amp;action=table_rename'>" . $lang['rename'] . "</a>";
  3538. echo "</td>";
  3539. echo $tdWithClass;
  3540. echo "<a href='?table=" . urlencode($result[$i]['name']) . "&amp;action=table_empty' class='empty'>" . $lang['empty'] . "</a>";
  3541. echo "</td>";
  3542. echo $tdWithClass;
  3543. echo "<a href='?table=" . urlencode($result[$i]['name']) . "&amp;action=table_drop' class='drop'>" . $lang['drop'] . "</a>";
  3544. echo "</td>";
  3545. echo $tdWithClass;
  3546. echo $records;
  3547. echo "</td>";
  3548. echo "</tr>";
  3549. } else {
  3550. echo "<tr>";
  3551. echo $tdWithClassLeft;
  3552. echo $lang['view'];
  3553. echo "</td>";
  3554. echo $tdWithClassLeft;
  3555. echo "<a href='?table=" . urlencode($result[$i]['name']) . "&amp;action=row_view'>" . htmlencode($result[$i]['name']) . "</a>";
  3556. echo "</td>";
  3557. echo $tdWithClass;
  3558. echo "<a href='?table=" . urlencode($result[$i]['name']) . "&amp;action=row_view'>" . $lang['browse'] . "</a>";
  3559. echo "</td>";
  3560. echo $tdWithClass;
  3561. echo "<a href='?table=" . urlencode($result[$i]['name']) . "&amp;action=column_view'>" . $lang['struct'] . "</a>";
  3562. echo "</td>";
  3563. echo $tdWithClass;
  3564. echo "<a href='?table=" . urlencode($result[$i]['name']) . "&amp;action=table_sql'>" . $lang['sql'] . "</a>";
  3565. echo "</td>";
  3566. echo $tdWithClass;
  3567. echo "<a href='?table=" . urlencode($result[$i]['name']) . "&amp;action=table_search'>" . $lang['srch'] . "</a>";
  3568. echo "</td>";
  3569. echo $tdWithClass;
  3570. echo "";
  3571. echo "</td>";
  3572. echo $tdWithClass;
  3573. echo "<a href='?table=" . urlencode($result[$i]['name']) . "&amp;action=table_export'>" . $lang['export'] . "</a>";
  3574. echo "</td>";
  3575. echo $tdWithClass;
  3576. echo "";
  3577. echo "</td>";
  3578. echo $tdWithClass;
  3579. echo "";
  3580. echo "</td>";
  3581. echo $tdWithClass;
  3582. echo "";
  3583. echo "</td>";
  3584. echo $tdWithClass;
  3585. echo "<a href='?table=" . urlencode($result[$i]['name']) . "&amp;action=view_drop' class='drop'>" . $lang['drop'] . "</a>";
  3586. echo "</td>";
  3587. echo $tdWithClass;
  3588. echo $records;
  3589. echo "</td>";
  3590. echo "</tr>";
  3591. }
  3592. }
  3593. echo "<tr>";
  3594. echo "<td class='tdheader' colspan='12'>" . sizeof($result) . " " . $lang['total'] . "</td>";
  3595. echo "<td class='tdheader' colspan='1' style='text-align:right;'>" . $totalRecords . ($skippedTables ? " <a href='?forceCount=1'>+ ?</a>" : "") . "</td>";
  3596. echo "</tr>";
  3597. echo "</table>";
  3598. echo "<br/>";
  3599. if ($skippedTables) {
  3600. echo "<div class='confirm' style='margin-bottom:20px;'>" . sprintf($lang["counting_skipped"], "<a href='?forceCount=1'>", "</a>") . "</div>";
  3601. }
  3602. }
  3603. echo "<fieldset>";
  3604. echo "<legend><b>" . $lang['create_tbl_db'] . " '" . htmlencode($db->getName()) . "'</b></legend>";
  3605. echo "<form action='?action=table_create' method='post'>";
  3606. echo $token_html;
  3607. echo $lang['name'] . ": <input type='text' name='tablename' style='width:200px;'/> ";
  3608. echo $lang['fld_num'] . ": <input type='text' name='tablefields' style='width:90px;'/> ";
  3609. echo "<input type='submit' name='createtable' value='" . $lang['go'] . "' class='btn'/>";
  3610. echo "</form>";
  3611. echo "</fieldset>";
  3612. echo "<br/>";
  3613. echo "<fieldset>";
  3614. echo "<legend><b>" . $lang['create_view'] . " '" . htmlencode($db->getName()) . "'</b></legend>";
  3615. echo "<form action='?action=view_create&amp;confirm=1' method='post'>";
  3616. echo $token_html;
  3617. echo $lang['name'] . ": <input type='text' name='viewname' style='width:200px;'/> ";
  3618. echo $lang['sel_state'] . " " . helpLink($lang['help4']) . ": <input type='text' name='select' style='width:400px;'/> ";
  3619. echo "<input type='submit' name='createtable' value='" . $lang['go'] . "' class='btn'/>";
  3620. echo "</form>";
  3621. echo "</fieldset>";
  3622. } else if ($view == "sql") {
  3623. //- Database SQL editor (=sql)
  3624. if (isset($_POST['query']) && $_POST['query'] != "") {
  3625. $delimiter = $_POST['delimiter'];
  3626. $queryStr = $_POST['queryval'];
  3627. //save the queries in history if necessary
  3628. if ($maxSavedQueries != 0 && $maxSavedQueries != false) {
  3629. if (!isset($_SESSION['query_history'])) {
  3630. $_SESSION['query_history'] = array();
  3631. }
  3632. $_SESSION['query_history'][md5(strtolower($queryStr))] = $queryStr;
  3633. if (sizeof($_SESSION['query_history']) > $maxSavedQueries) {
  3634. array_shift($_SESSION['query_history']);
  3635. }
  3636. }
  3637. $query = explode_sql($delimiter, $queryStr); //explode the query string into individual queries based on the delimiter
  3638. for ($i = 0; $i < sizeof($query); $i++) //iterate through the queries exploded by the delimiter
  3639. {
  3640. if (str_replace(" ", "", str_replace("\n", "", str_replace("\r", "", $query[$i]))) != "") //make sure this query is not an empty string
  3641. {
  3642. $queryTimer = new MicroTimer();
  3643. $table_result = $db->query($query[$i]);
  3644. echo "<div class='confirm'>";
  3645. echo "<b>" . htmlencode($query[$i]) . "</b>";
  3646. if ($table_result === null || $table_result === false) {
  3647. echo "<br /><b>" . $lang['err'] . ": " . htmlencode($db->getError()) . "</b></div>";
  3648. }
  3649. echo "</div><br/>";
  3650. if ($row = $db->fetch($table_result, 'assoc')) {
  3651. $headers = array_keys($row);
  3652. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  3653. echo "<tr>";
  3654. for ($j = 0; $j < sizeof($headers); $j++) {
  3655. echo "<td class='tdheader'>";
  3656. echo htmlencode($headers[$j]);
  3657. echo "</td>";
  3658. }
  3659. echo "</tr>";
  3660. $rowCount = 0;
  3661. for (; $rowCount == 0 || $row = $db->fetch($table_result, 'assoc'); $rowCount++) {
  3662. $tdWithClass = "<td class='td" . ($rowCount % 2 ? "1" : "2") . "'>";
  3663. echo "<tr>";
  3664. for ($z = 0; $z < sizeof($headers); $z++) {
  3665. echo $tdWithClass;
  3666. if ($row[$headers[$z]] === "") {
  3667. echo "&nbsp;";
  3668. } elseif ($row[$headers[$z]] === null) {
  3669. echo "<i class='null'>NULL</i>";
  3670. } else {
  3671. echo htmlencode(subString($row[$headers[$z]]));
  3672. }
  3673. echo "</td>";
  3674. }
  3675. echo "</tr>";
  3676. }
  3677. $queryTimer->stop();
  3678. echo "</table><br/><br/>";
  3679. if ($table_result !== null && $table_result !== false) {
  3680. echo "<div class='confirm' style='margin-bottom: 2em'>";
  3681. if ($rowCount > 0 || $db->getAffectedRows() == 0) {
  3682. printf($lang['show_rows'], $rowCount);
  3683. }
  3684. if ($db->getAffectedRows() > 0 || $rowCount == 0) {
  3685. echo $db->getAffectedRows() . " " . $lang['rows_aff'] . " ";
  3686. }
  3687. printf($lang['query_time'], $queryTimer);
  3688. echo "</div>";
  3689. }
  3690. }
  3691. }
  3692. }
  3693. } else {
  3694. $delimiter = ";";
  3695. $queryStr = "";
  3696. }
  3697. echo "<fieldset>";
  3698. echo "<legend><b>" . sprintf($lang['run_sql'], htmlencode($db->getName())) . "</b></legend>";
  3699. echo "<form action='?view=sql' method='post'>";
  3700. echo $token_html;
  3701. if (isset($_SESSION['query_history']) && sizeof($_SESSION['query_history']) > 0) {
  3702. echo "<b>" . $lang['recent_queries'] . "</b><ul>";
  3703. foreach ($_SESSION['query_history'] as $key => $value) {
  3704. echo "<li><a onclick='document.getElementById(\"queryval\").value = this.textContent; return false;' href='#'>" . htmlencode($value) . "</a></li>";
  3705. }
  3706. echo "</ul><br/><br/>";
  3707. }
  3708. echo "<textarea style='width:100%; height:300px;' name='queryval' id='queryval' cols='50' rows='8'>" . htmlencode($queryStr) . "</textarea>";
  3709. echo $lang['delimit'] . " <input type='text' name='delimiter' value='" . htmlencode($delimiter) . "' style='width:50px;'/> ";
  3710. echo "<input type='submit' name='query' value='" . $lang['go'] . "' class='btn'/>";
  3711. echo "</form>";
  3712. echo "</fieldset>";
  3713. } else if ($view == "vacuum") {
  3714. //- Vacuum database confirmation (=vacuum)
  3715. if (isset($_POST['vacuum'])) {
  3716. $query = "VACUUM";
  3717. $db->query($query);
  3718. echo "<div class='confirm'>";
  3719. printf($lang['db_vac'], htmlencode($db->getName()));
  3720. echo "</div><br/>";
  3721. }
  3722. echo "<form method='post' action='?view=vacuum'>";
  3723. echo $token_html;
  3724. printf($lang['vac_desc'], htmlencode($db->getName()));
  3725. echo "<br/><br/>";
  3726. echo "<input type='submit' value='" . $lang['vac'] . "' name='vacuum' class='btn'/>";
  3727. echo "</form>";
  3728. } else if ($view == "export") {
  3729. //- Export view (=export)
  3730. echo "<form method='post' action='?view=export'>";
  3731. echo $token_html;
  3732. echo "<fieldset style='float:left; width:260px; margin-right:20px;'><legend><b>" . $lang['export'] . "</b></legend>";
  3733. echo "<select multiple='multiple' size='10' style='width:240px;' name='tables[]'>";
  3734. $query = "SELECT name FROM sqlite_master WHERE type='table' OR type='view' ORDER BY name";
  3735. $result = $db->selectArray($query);
  3736. for ($i = 0; $i < sizeof($result); $i++) {
  3737. if (substr($result[$i]['name'], 0, 7) != "sqlite_" && $result[$i]['name'] != "") {
  3738. echo "<option value='" . htmlencode($result[$i]['name']) . "' selected='selected'>" . htmlencode($result[$i]['name']) . "</option>";
  3739. }
  3740. }
  3741. echo "</select>";
  3742. echo "<br/><br/>";
  3743. echo "<label><input type='radio' name='export_type' checked='checked' value='sql' onclick='toggleExports(\"sql\");'/> " . $lang['sql'] . "</label>";
  3744. echo "<br/><label><input type='radio' name='export_type' value='csv' onclick='toggleExports(\"csv\");'/> " . $lang['csv'] . "</label>";
  3745. echo "</fieldset>";
  3746. echo "<fieldset style='float:left; max-width:350px;' id='exportoptions_sql'><legend><b>" . $lang['options'] . "</b></legend>";
  3747. echo "<label><input type='checkbox' checked='checked' name='structure'/> " . $lang['export_struct'] . "</label> " . helpLink($lang['help5']) . "<br/>";
  3748. echo "<label><input type='checkbox' checked='checked' name='data'/> " . $lang['export_data'] . "</label> " . helpLink($lang['help6']) . "<br/>";
  3749. echo "<label><input type='checkbox' name='drop'/> " . $lang['add_drop'] . "</label> " . helpLink($lang['help7']) . "<br/>";
  3750. echo "<label><input type='checkbox' checked='checked' name='transaction'/> " . $lang['add_transact'] . "</label> " . helpLink($lang['help8']) . "<br/>";
  3751. echo "<label><input type='checkbox' checked='checked' name='comments'/> " . $lang['comments'] . "</label> " . helpLink($lang['help9']) . "<br/>";
  3752. echo "</fieldset>";
  3753. echo "<fieldset style='float:left; max-width:350px; display:none;' id='exportoptions_csv'><legend><b>" . $lang['options'] . "</b></legend>";
  3754. echo "<div style='float:left;'>" . $lang['fld_terminated'] . "</div>";
  3755. echo "<input type='text' value=';' name='export_csv_fieldsterminated' style='float:right;'/>";
  3756. echo "<div style='clear:both;'>";
  3757. echo "<div style='float:left;'>" . $lang['fld_enclosed'] . "</div>";
  3758. echo "<input type='text' value='\"' name='export_csv_fieldsenclosed' style='float:right;'/>";
  3759. echo "<div style='clear:both;'>";
  3760. echo "<div style='float:left;'>" . $lang['fld_escaped'] . "</div>";
  3761. echo "<input type='text' value='\' name='export_csv_fieldsescaped' style='float:right;'/>";
  3762. echo "<div style='clear:both;'>";
  3763. echo "<div style='float:left;'>" . $lang['rep_null'] . "</div>";
  3764. echo "<input type='text' value='NULL' name='export_csv_replacenull' style='float:right;'/>";
  3765. echo "<div style='clear:both;'>";
  3766. echo "<label><input type='checkbox' name='export_csv_crlf'/> " . $lang['rem_crlf'] . "</label><br/>";
  3767. echo "<label><input type='checkbox' checked='checked' name='export_csv_fieldnames'/> " . $lang['put_fld'] . "</label>";
  3768. echo "</fieldset>";
  3769. echo "<div style='clear:both;'></div>";
  3770. echo "<br/><br/>";
  3771. echo "<fieldset><legend><b>" . $lang['save_as'] . "</b></legend>";
  3772. $file = pathinfo($db->getPath());
  3773. $name = $file['filename'];
  3774. echo "<input type='text' name='filename' value='" . htmlencode($name) . "_" . date("Y-m-d") . ".dump' style='width:400px;'/> <input type='submit' name='export' value='" . $lang['export'] . "' class='btn'/>";
  3775. echo "</fieldset>";
  3776. echo "</form>";
  3777. echo "<div class='confirm' style='margin-top: 2em'>" . sprintf($lang['backup_hint'], "<a href='?download=" . urlencode($currentDB['path']) . "&amp;token=" . urlencode($token) . "' title='" . $lang['backup'] . "'>" . $lang["backup_hint_linktext"] . "</a>") . "</div>";
  3778. } else if ($view == "import") {
  3779. //- Import view (=import)
  3780. if (isset($_POST['import'])) {
  3781. echo "<div class='confirm'>";
  3782. if ($importSuccess === true) {
  3783. echo $lang['import_suc'];
  3784. } else {
  3785. echo $importSuccess;
  3786. }
  3787. echo "</div><br/>";
  3788. }
  3789. echo "<form method='post' action='?view=import' enctype='multipart/form-data'>";
  3790. echo $token_html;
  3791. echo "<fieldset style='float:left; width:260px; margin-right:20px;'><legend><b>" . $lang['import'] . "</b></legend>";
  3792. echo "<label><input type='radio' name='import_type' checked='checked' value='sql' onclick='toggleImports(\"sql\");'/> " . $lang['sql'] . "</label>";
  3793. echo "<br/><label><input type='radio' name='import_type' value='csv' onclick='toggleImports(\"csv\");'/> " . $lang['csv'] . "</label>";
  3794. echo "</fieldset>";
  3795. echo "<fieldset style='float:left; max-width:350px;' id='importoptions_sql'><legend><b>" . $lang['options'] . "</b></legend>";
  3796. echo $lang['no_opt'];
  3797. echo "</fieldset>";
  3798. echo "<fieldset style='float:left; max-width:350px; display:none;' id='importoptions_csv'><legend><b>" . $lang['options'] . "</b></legend>";
  3799. echo "<div style='float:left;'>" . $lang['csv_tbl'] . "</div>";
  3800. echo "<select name='single_table' style='float:right;'>";
  3801. $query = "SELECT name FROM sqlite_master WHERE type='table' OR type='view' ORDER BY name";
  3802. $result = $db->selectArray($query);
  3803. for ($i = 0; $i < sizeof($result); $i++) {
  3804. if (substr($result[$i]['name'], 0, 7) != "sqlite_" && $result[$i]['name'] != "") {
  3805. echo "<option value='" . htmlencode($result[$i]['name']) . "'>" . htmlencode($result[$i]['name']) . "</option>";
  3806. }
  3807. }
  3808. echo "</select>";
  3809. echo "<div style='clear:both;'>";
  3810. echo "<div style='float:left;'>" . $lang['fld_terminated'] . "</div>";
  3811. echo "<input type='text' value=';' name='import_csv_fieldsterminated' style='float:right;'/>";
  3812. echo "<div style='clear:both;'>";
  3813. echo "<div style='float:left;'>" . $lang['fld_enclosed'] . "</div>";
  3814. echo "<input type='text' value='\"' name='import_csv_fieldsenclosed' style='float:right;'/>";
  3815. echo "<div style='clear:both;'>";
  3816. echo "<div style='float:left;'>" . $lang['fld_escaped'] . "</div>";
  3817. echo "<input type='text' value='\' name='import_csv_fieldsescaped' style='float:right;'/>";
  3818. echo "<div style='clear:both;'>";
  3819. echo "<div style='float:left;'>" . $lang['null_represent'] . "</div>";
  3820. echo "<input type='text' value='NULL' name='import_csv_replacenull' style='float:right;'/>";
  3821. echo "<div style='clear:both;'>";
  3822. echo "<label><input type='checkbox' checked='checked' name='import_csv_fieldnames'/> " . $lang['fld_names'] . "</label>";
  3823. echo "</fieldset>";
  3824. echo "<div style='clear:both;'></div>";
  3825. echo "<br/><br/>";
  3826. echo "<fieldset><legend><b>" . $lang['import_f'] . "</b></legend>";
  3827. echo "<input type='file' value='" . $lang['choose_f'] . "' name='file' style='background-color:transparent; border-style:none;'/> <input type='submit' value='" . $lang['import'] . "' name='import' class='btn'/>";
  3828. echo "</fieldset>";
  3829. } else if ($view == "rename") {
  3830. //- Rename database confirmation (=rename)
  3831. if (isset($extension_not_allowed)) {
  3832. echo "<div class='confirm'>";
  3833. echo $lang['extension_not_allowed'] . ': ';
  3834. echo implode(', ', array_map('htmlencode', $allowed_extensions));
  3835. echo '<br />' . $lang['add_allowed_extension'];
  3836. echo "</div><br/>";
  3837. }
  3838. if (isset($dbexists)) {
  3839. echo "<div class='confirm'>";
  3840. if ($oldpath == $newpath) {
  3841. echo $lang['err'] . ": " . $lang['warn_dumbass'];
  3842. } else {
  3843. echo $lang['err'] . ": ";
  3844. printf($lang['db_exists'], htmlencode($newpath));
  3845. }
  3846. echo "</div><br/>";
  3847. }
  3848. if (isset($justrenamed)) {
  3849. echo "<div class='confirm'>";
  3850. printf($lang['db_renamed'], htmlencode($oldpath));
  3851. echo " '" . htmlencode($newpath) . "'.";
  3852. echo "</div><br/>";
  3853. }
  3854. echo "<form action='?view=rename&amp;database_rename=1' method='post'>";
  3855. echo $token_html;
  3856. echo "<input type='hidden' name='oldname' value='" . htmlencode($db->getPath()) . "'/>";
  3857. echo $lang['db_rename'] . " '" . htmlencode($db->getPath()) . "' " . $lang['to'] . " <input type='text' name='newname' style='width:200px;' value='" . htmlencode($db->getPath()) . "'/> <input type='submit' value='" . $lang['rename'] . "' name='rename' class='btn'/>";
  3858. echo "</form>";
  3859. } else if ($view == "delete") {
  3860. //- Delete database confirmation (=delete)
  3861. echo "<form action='?database_delete=1' method='post'>";
  3862. echo $token_html;
  3863. echo "<div class='confirm'>";
  3864. echo sprintf($lang['ques_del_db'], htmlencode($db->getPath())) . "<br/><br/>";
  3865. echo "<input name='database_delete' value='" . htmlencode($db->getPath()) . "' type='hidden'/>";
  3866. echo "<input type='submit' value='" . $lang['confirm'] . "' class='btn'/> ";
  3867. echo "<a href='" . PAGE . "'>" . $lang['cancel'] . "</a>";
  3868. echo "</div>";
  3869. echo "</form>";
  3870. }
  3871. echo "</div>";
  3872. }
  3873. //- HTML: page footer
  3874. echo "<br/>";
  3875. echo "<span style='font-size:11px;'>" . $lang['powered'] . " <a href='" . PROJECT_URL . "' target='_blank' style='font-size:11px;'>" . PROJECT . "</a> | ";
  3876. echo $lang['free_software'] . " <a href='" . DONATE_URL . "' target='_blank' style='font-size:11px;'>" . $lang['please_donate'] . "</a> | ";
  3877. printf($lang['page_gen'], $pageTimer);
  3878. echo "</span>";
  3879. echo "</td></tr></table>";
  3880. $db->close(); //close the database
  3881. echo "</body>";
  3882. echo "</html>";
  3883. //- End of main code
  3884. // Authorization class
  3885. // Maintains user's logged-in state and security of application
  3886. //
  3887. class Authorization
  3888. {
  3889. private $authorized;
  3890. private $login_failed;
  3891. private $system_password_encrypted;
  3892. public function __construct()
  3893. {
  3894. // the salt and password encrypting is probably unnecessary protection but is done just
  3895. // for the sake of being very secure
  3896. if (!isset($_SESSION[COOKIENAME . '_salt']) && !isset($_COOKIE[COOKIENAME . '_salt'])) {
  3897. // create a random salt for this session if a cookie doesn't already exist for it
  3898. $_SESSION[COOKIENAME . '_salt'] = self::generateSalt(20);
  3899. } else if (!isset($_SESSION[COOKIENAME . '_salt']) && isset($_COOKIE[COOKIENAME . '_salt'])) {
  3900. // session doesn't exist, but cookie does so grab it
  3901. $_SESSION[COOKIENAME . '_salt'] = $_COOKIE[COOKIENAME . '_salt'];
  3902. }
  3903. // salted and encrypted password used for checking
  3904. $this->system_password_encrypted = md5(SYSTEMPASSWORD . "_" . $_SESSION[COOKIENAME . '_salt']);
  3905. $this->authorized =
  3906. // no password
  3907. SYSTEMPASSWORD == ''
  3908. // correct password stored in session
  3909. || isset($_SESSION[COOKIENAME . 'password']) && $_SESSION[COOKIENAME . 'password'] == $this->system_password_encrypted
  3910. // correct password stored in cookie
  3911. || isset($_COOKIE[COOKIENAME]) && isset($_COOKIE[COOKIENAME . '_salt']) && md5(SYSTEMPASSWORD . "_" . $_COOKIE[COOKIENAME . '_salt']) == $_COOKIE[COOKIENAME];
  3912. }
  3913. public function attemptGrant($password, $remember)
  3914. {
  3915. if ($password == SYSTEMPASSWORD) {
  3916. if ($remember) {
  3917. // user wants to be remembered, so set a cookie
  3918. $expire = time() + 60 * 60 * 24 * 30; //set expiration to 1 month from now
  3919. setcookie(COOKIENAME, $this->system_password_encrypted, $expire, null, null, null, true);
  3920. setcookie(COOKIENAME . "_salt", $_SESSION[COOKIENAME . '_salt'], $expire, null, null, null, true);
  3921. } else {
  3922. // user does not want to be remembered, so destroy any potential cookies
  3923. setcookie(COOKIENAME, "", time() - 86400, null, null, null, true);
  3924. setcookie(COOKIENAME . "_salt", "", time() - 86400, null, null, null, true);
  3925. unset($_COOKIE[COOKIENAME]);
  3926. unset($_COOKIE[COOKIENAME . '_salt']);
  3927. }
  3928. $_SESSION[COOKIENAME . 'password'] = $this->system_password_encrypted;
  3929. $this->authorized = true;
  3930. return true;
  3931. }
  3932. $this->login_failed = true;
  3933. return false;
  3934. }
  3935. public function revoke()
  3936. {
  3937. //destroy everything - cookies and session vars
  3938. setcookie(COOKIENAME, "", time() - 86400, null, null, null, true);
  3939. setcookie(COOKIENAME . "_salt", "", time() - 86400, null, null, null, true);
  3940. unset($_COOKIE[COOKIENAME]);
  3941. unset($_COOKIE[COOKIENAME . '_salt']);
  3942. session_unset();
  3943. session_destroy();
  3944. $this->authorized = false;
  3945. }
  3946. public function isAuthorized()
  3947. {
  3948. return $this->authorized;
  3949. }
  3950. public function isFailedLogin()
  3951. {
  3952. return $this->login_failed;
  3953. }
  3954. public function isPasswordDefault()
  3955. {
  3956. return SYSTEMPASSWORD == 'admin';
  3957. }
  3958. private static function generateSalt($saltSize)
  3959. {
  3960. $set = 'ABCDEFGHiJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  3961. $setLast = strlen($set) - 1;
  3962. $salt = '';
  3963. while ($saltSize-- > 0) {
  3964. $salt .= $set[mt_rand(0, $setLast)];
  3965. }
  3966. return $salt;
  3967. }
  3968. }
  3969. // Database class
  3970. // Generic database abstraction class to manage interaction with database without worrying about SQLite vs. PHP versions
  3971. //
  3972. class Database
  3973. {
  3974. protected $db; //reference to the DB object
  3975. protected $type; //the extension for PHP that handles SQLite
  3976. protected $data;
  3977. protected $lastResult;
  3978. protected $alterError;
  3979. public function __construct($data)
  3980. {
  3981. global $lang;
  3982. $this->data = $data;
  3983. try
  3984. {
  3985. if (!file_exists($this->data["path"]) && !is_writable(dirname($this->data["path"]))) //make sure the containing directory is writable if the database does not exist
  3986. {
  3987. echo "<div class='confirm' style='margin:20px;'>";
  3988. printf($lang['db_not_writeable'], htmlencode($this->data["path"]), htmlencode(dirname($this->data["path"])));
  3989. echo "<form action='" . PAGE . "' method='post'>";
  3990. echo '<input type="hidden" name="token" value="' . $_SESSION['token'] . '" />';
  3991. echo "<input type='submit' value='Log Out' name='" . $lang['logout'] . "' class='btn'/>";
  3992. echo "</form>";
  3993. echo "</div><br/>";
  3994. exit();
  3995. }
  3996. $ver = $this->getVersion();
  3997. switch (true) {
  3998. case ((!isset($data['type']) || $data['type'] != 2) && (FORCETYPE == "PDO" || (FORCETYPE == false && class_exists("PDO") && ($ver == -1 || $ver == 3)))):
  3999. $this->db = new PDO("" . $this->data['path']);
  4000. if ($this->db != null) {
  4001. $this->type = "PDO";
  4002. break;
  4003. }
  4004. case ((!isset($data['type']) || $data['type'] != 2) && (FORCETYPE == "SQLite3" || (FORCETYPE == false && class_exists("SQLite3") && ($ver == -1 || $ver == 3)))):
  4005. $this->db = new SQLite3($this->data['path']);
  4006. if ($this->db != null) {
  4007. $this->type = "SQLite3";
  4008. break;
  4009. }
  4010. case (FORCETYPE == "SQLiteDatabase" || (FORCETYPE == false && class_exists("SQLiteDatabase") && ($ver == -1 || $ver == 2))):
  4011. $this->db = new SQLiteDatabase($this->data['path']);
  4012. if ($this->db != null) {
  4013. $this->type = "SQLiteDatabase";
  4014. break;
  4015. }
  4016. default:
  4017. $this->showError();
  4018. exit();
  4019. }
  4020. $this->query("PRAGMA foreign_keys = ON");
  4021. } catch (Exception $e) {
  4022. $this->showError();
  4023. exit();
  4024. }
  4025. }
  4026. public function registerUserFunction($ids)
  4027. {
  4028. // in case a single function id was passed
  4029. if (is_string($ids)) {
  4030. $ids = array($ids);
  4031. }
  4032. if ($this->type == 'PDO') {
  4033. foreach ($ids as $id) {
  4034. $this->db->sqliteCreateFunction($id, $id, 1);
  4035. }
  4036. } else { // type is Sqlite3 or SQLiteDatabase
  4037. foreach ($ids as $id) {
  4038. $this->db->createFunction($id, $id, 1);
  4039. }
  4040. }
  4041. }
  4042. public function getError()
  4043. {
  4044. if ($this->alterError != '') {
  4045. $error = $this->alterError;
  4046. $this->alterError = "";
  4047. return $error;
  4048. } else if ($this->type == "PDO") {
  4049. $e = $this->db->errorInfo();
  4050. return $e[2];
  4051. } else if ($this->type == "SQLite3") {
  4052. return $this->db->lastErrorMsg();
  4053. } else {
  4054. return sqlite_error_string($this->db->lastError());
  4055. }
  4056. }
  4057. public function showError()
  4058. {
  4059. global $lang;
  4060. $classPDO = class_exists("PDO");
  4061. $classSQLite3 = class_exists("SQLite3");
  4062. $classSQLiteDatabase = class_exists("SQLiteDatabase");
  4063. if ($classPDO) // PDO is there, check if the SQLite driver for PDO is missing
  4064. {
  4065. $PDOSqliteDriver = (in_array("sqlite", PDO::getAvailableDrivers()));
  4066. } else {
  4067. $PDOSqliteDriver = false;
  4068. }
  4069. echo "<div class='confirm' style='margin:20px;'>";
  4070. printf($lang['db_setup'], $this->getPath());
  4071. echo ".<br/><br/><i>" . $lang['chk_ext'] . "...<br/><br/>";
  4072. echo "<b>PDO</b>: " . ($classPDO ? $lang['installed'] : $lang['not_installed']) . "<br/>";
  4073. echo "<b>PDO SQLite Driver</b>: " . ($PDOSqliteDriver ? $lang['installed'] : $lang['not_installed']) . "<br/>";
  4074. echo "<b>SQLite3</b>: " . ($classSQLite3 ? $lang['installed'] : $lang['not_installed']) . "<br/>";
  4075. echo "<b>SQLiteDatabase</b>: " . ($classSQLiteDatabase ? $lang['installed'] : $lang['not_installed']) . "<br/>";
  4076. echo "<br/>..." . $lang['done'] . ".</i><br/><br/>";
  4077. if (!$classPDO && !$classSQLite3 && !$classSQLiteDatabase) {
  4078. printf($lang['sqlite_ext_support'], PROJECT);
  4079. } else {
  4080. if (!$classPDO && !$classSQLite3 && $this->getVersion() == 3) {
  4081. printf($lang['sqlite_v_error'], 3, PROJECT, 2);
  4082. } else if (!$classSQLiteDatabase && $this->getVersion() == 2) {
  4083. printf($lang['sqlite_v_error'], 2, PROJECT, 3);
  4084. } else {
  4085. echo $lang['report_issue'] . ' ' . PROJECT_BUGTRACKER_LINK . '.';
  4086. }
  4087. }
  4088. echo "<p>See " . PROJECT_INSTALL_LINK . " for help.</p>";
  4089. $this->print_db_list();
  4090. echo "</div>";
  4091. }
  4092. // print the list of databases
  4093. public function print_db_list()
  4094. {
  4095. global $databases, $lang;
  4096. echo "<fieldset style='margin:15px;' class='databaseList'><legend><b>" . $lang['db_ch'] . "</b></legend>";
  4097. if (sizeof($databases) < 10) //if there aren't a lot of databases, just show them as a list of links instead of drop down menu
  4098. {
  4099. $i = 0;
  4100. foreach ($databases as $database) {
  4101. $i++;
  4102. $name = $database['name'];
  4103. if (strlen($name) > 25) {
  4104. $name = "..." . substr($name, strlen($name) - 22, 22);
  4105. }
  4106. echo '[' . ($database['readable'] ? 'r' : ' ') . ($database['writable'] && $database['writable_dir'] ? 'w' : ' ') . ']&nbsp;';
  4107. if ($database == $_SESSION[COOKIENAME . 'currentDB']) {
  4108. echo "<a href='?switchdb=" . urlencode($database['path']) . "' class='active_db'>" . htmlencode($name) . "</a>&nbsp;&nbsp;<a href='?download=" . urlencode($database['path']) . "&amp;token=" . urlencode($_SESSION['token']) . "' title='" . $lang['backup'] . "'>[&darr;]</a>";
  4109. } else {
  4110. echo "<a href='?switchdb=" . urlencode($database['path']) . "'>" . htmlencode($name) . "</a>&nbsp;&nbsp;<a href='?download=" . urlencode($database['path']) . "&amp;token=" . urlencode($_SESSION['token']) . "' title='" . $lang['backup'] . "'>[&darr;]</a>";
  4111. }
  4112. if ($i < sizeof($databases)) {
  4113. echo "<br/>";
  4114. }
  4115. }
  4116. } else //there are a lot of databases - show a drop down menu
  4117. {
  4118. echo "<form action='" . PAGE . "' method='post'>";
  4119. echo '<input type="hidden" name="token" value="' . $_SESSION['token'] . '" />';
  4120. echo "<select name='database_switch'>";
  4121. foreach ($databases as $database) {
  4122. $perms_string = htmlencode('[' . ($database['readable'] ? 'r' : ' ') . ($database['writable'] && $database['writable_dir'] ? 'w' : ' ') . '] ');
  4123. if ($database == $_SESSION[COOKIENAME . 'currentDB']) {
  4124. echo "<option value='" . htmlencode($database['path']) . "' selected='selected'>" . $perms_string . htmlencode($database['name']) . "</option>";
  4125. } else {
  4126. echo "<option value='" . htmlencode($database['path']) . "'>" . $perms_string . htmlencode($database['name']) . "</option>";
  4127. }
  4128. }
  4129. echo "</select> ";
  4130. echo "<input type='submit' value='" . $lang['go'] . "' class='btn'>";
  4131. echo "</form>";
  4132. }
  4133. echo "</fieldset>";
  4134. }
  4135. public function __destruct()
  4136. {
  4137. if ($this->db) {
  4138. $this->close();
  4139. }
  4140. }
  4141. //get the exact PHP extension being used for SQLite
  4142. public function getType()
  4143. {
  4144. return $this->type;
  4145. }
  4146. // get the version of the SQLite library
  4147. public function getSQLiteVersion()
  4148. {
  4149. $queryVersion = $this->select("SELECT sqlite_version() AS sqlite_version");
  4150. return $queryVersion['sqlite_version'];
  4151. }
  4152. //get the name of the database
  4153. public function getName()
  4154. {
  4155. return $this->data["name"];
  4156. }
  4157. //get the filename of the database
  4158. public function getPath()
  4159. {
  4160. return $this->data["path"];
  4161. }
  4162. //is the db-file writable?
  4163. public function isWritable()
  4164. {
  4165. return $this->data["writable"];
  4166. }
  4167. //is the db-folder writable?
  4168. public function isDirWritable()
  4169. {
  4170. return $this->data["writable_dir"];
  4171. }
  4172. //get the version of the database
  4173. public function getVersion()
  4174. {
  4175. if (file_exists($this->data['path'])) //make sure file exists before getting its contents
  4176. {
  4177. $content = strtolower(file_get_contents($this->data['path'], null, null, 0, 40)); //get the first 40 characters of the database file
  4178. $p = strpos($content, "** this file contains an sqlite 2"); //this text is at the beginning of every SQLite2 database
  4179. if ($p !== false) //the text is found - this is version 2
  4180. {
  4181. return 2;
  4182. } else {
  4183. return 3;
  4184. }
  4185. } else //return -1 to indicate that it does not exist and needs to be created
  4186. {
  4187. return -1;
  4188. }
  4189. }
  4190. //get the size of the database (in KB)
  4191. public function getSize()
  4192. {
  4193. return round(filesize($this->data["path"]) * 0.0009765625, 1);
  4194. }
  4195. //get the last modified time of database
  4196. public function getDate()
  4197. {
  4198. global $lang;
  4199. return date($lang['date_format'], filemtime($this->data['path']));
  4200. }
  4201. //get number of affected rows from last query
  4202. public function getAffectedRows()
  4203. {
  4204. if ($this->type == "PDO") {
  4205. if (!is_object($this->lastResult))
  4206. // in case it was an alter table statement, there is no lastResult object
  4207. {
  4208. return 0;
  4209. } else {
  4210. return $this->lastResult->rowCount();
  4211. }
  4212. } else if ($this->type == "SQLite3") {
  4213. return $this->db->changes();
  4214. } else if ($this->type == "SQLiteDatabase") {
  4215. return $this->db->changes();
  4216. }
  4217. }
  4218. public function getTypeOfTable($table)
  4219. {
  4220. $result = $this->select("SELECT `type` FROM `sqlite_master` WHERE `name`=" . $this->quote($table), 'assoc');
  4221. return $result['type'];
  4222. }
  4223. public function close()
  4224. {
  4225. if ($this->type == "PDO") {
  4226. $this->db = null;
  4227. } else if ($this->type == "SQLite3") {
  4228. $this->db->close();
  4229. } else if ($this->type == "SQLiteDatabase") {
  4230. $this->db = null;
  4231. }
  4232. }
  4233. public function beginTransaction()
  4234. {
  4235. $this->query("BEGIN");
  4236. }
  4237. public function commitTransaction()
  4238. {
  4239. $this->query("COMMIT");
  4240. }
  4241. public function rollbackTransaction()
  4242. {
  4243. $this->query("ROLLBACK");
  4244. }
  4245. //generic query wrapper
  4246. //returns false on error and the query result on success
  4247. public function query($query, $ignoreAlterCase = false)
  4248. {
  4249. global $debug;
  4250. if (strtolower(substr(ltrim($query), 0, 5)) == 'alter' && $ignoreAlterCase == false) //this query is an ALTER query - call the necessary function
  4251. {
  4252. preg_match("/^\s*ALTER\s+TABLE\s+\"((?:[^\"]|\"\")+)\"\s+(.*)$/i", $query, $matches);
  4253. if (!isset($matches[1]) || !isset($matches[2])) {
  4254. if ($debug) {
  4255. echo "<span title='" . htmlencode($query) . "' onclick='this.innerHTML=\"" . htmlencode(str_replace('"', '\"', $query)) . "\"' style='cursor:pointer'>SQL?</span><br />";
  4256. }
  4257. return false;
  4258. }
  4259. $tablename = str_replace('""', '"', $matches[1]);
  4260. $alterdefs = $matches[2];
  4261. if ($debug) {
  4262. echo "ALTER TABLE QUERY=(" . htmlencode($query) . "), tablename=($tablename), alterdefs=($alterdefs)<br />";
  4263. }
  4264. $result = $this->alterTable($tablename, $alterdefs);
  4265. } else //this query is normal - proceed as normal
  4266. {
  4267. $result = $this->db->query($query);
  4268. if ($debug) {
  4269. echo "<span title='" . htmlencode($query) . "' onclick='this.innerHTML=\"" . htmlencode(str_replace('"', '\"', $query)) . "\"' style='cursor:pointer'>SQL?</span><br />";
  4270. }
  4271. }
  4272. if ($result === false) {
  4273. return false;
  4274. }
  4275. $this->lastResult = $result;
  4276. return $result;
  4277. }
  4278. //wrapper for an INSERT and returns the ID of the inserted row
  4279. public function insert($query)
  4280. {
  4281. $result = $this->query($query);
  4282. if ($this->type == "PDO") {
  4283. return $this->db->lastInsertId();
  4284. } else if ($this->type == "SQLite3") {
  4285. return $this->db->lastInsertRowID();
  4286. } else if ($this->type == "SQLiteDatabase") {
  4287. return $this->db->lastInsertRowid();
  4288. }
  4289. }
  4290. //returns an array for SELECT
  4291. public function select($query, $mode = "both")
  4292. {
  4293. $result = $this->query($query);
  4294. if (!$result) //make sure the result is valid
  4295. {
  4296. return null;
  4297. }
  4298. if ($this->type == "PDO") {
  4299. if ($mode == "assoc") {
  4300. $mode = PDO::FETCH_ASSOC;
  4301. } else if ($mode == "num") {
  4302. $mode = PDO::FETCH_NUM;
  4303. } else {
  4304. $mode = PDO::FETCH_BOTH;
  4305. }
  4306. return $result->fetch($mode);
  4307. } else if ($this->type == "SQLite3") {
  4308. if ($mode == "assoc") {
  4309. $mode = SQLITE3_ASSOC;
  4310. } else if ($mode == "num") {
  4311. $mode = SQLITE3_NUM;
  4312. } else {
  4313. $mode = SQLITE3_BOTH;
  4314. }
  4315. return $result->fetchArray($mode);
  4316. } else if ($this->type == "SQLiteDatabase") {
  4317. if ($mode == "assoc") {
  4318. $mode = SQLITE_ASSOC;
  4319. } else if ($mode == "num") {
  4320. $mode = SQLITE_NUM;
  4321. } else {
  4322. $mode = SQLITE_BOTH;
  4323. }
  4324. return $result->fetch($mode);
  4325. }
  4326. }
  4327. //returns an array of arrays after doing a SELECT
  4328. public function selectArray($query, $mode = "both")
  4329. {
  4330. $result = $this->query($query);
  4331. //make sure the result is valid
  4332. if ($result === false || $result === null) {
  4333. return null;
  4334. }
  4335. // error
  4336. if (!is_object($result)) // no rows returned
  4337. {
  4338. return array();
  4339. }
  4340. if ($this->type == "PDO") {
  4341. if ($mode == "assoc") {
  4342. $mode = PDO::FETCH_ASSOC;
  4343. } else if ($mode == "num") {
  4344. $mode = PDO::FETCH_NUM;
  4345. } else {
  4346. $mode = PDO::FETCH_BOTH;
  4347. }
  4348. return $result->fetchAll($mode);
  4349. } else if ($this->type == "SQLite3") {
  4350. if ($mode == "assoc") {
  4351. $mode = SQLITE3_ASSOC;
  4352. } else if ($mode == "num") {
  4353. $mode = SQLITE3_NUM;
  4354. } else {
  4355. $mode = SQLITE3_BOTH;
  4356. }
  4357. $arr = array();
  4358. $i = 0;
  4359. while ($res = $result->fetchArray($mode)) {
  4360. $arr[$i] = $res;
  4361. $i++;
  4362. }
  4363. return $arr;
  4364. } else if ($this->type == "SQLiteDatabase") {
  4365. if ($mode == "assoc") {
  4366. $mode = SQLITE_ASSOC;
  4367. } else if ($mode == "num") {
  4368. $mode = SQLITE_NUM;
  4369. } else {
  4370. $mode = SQLITE_BOTH;
  4371. }
  4372. return $result->fetchAll($mode);
  4373. }
  4374. }
  4375. //returns an array of the next row in $result
  4376. public function fetch($result, $mode = "both")
  4377. {
  4378. //make sure the result is valid
  4379. if ($result === false || $result === null) {
  4380. return null;
  4381. }
  4382. // error
  4383. if (!is_object($result)) // no rows returned
  4384. {
  4385. return array();
  4386. }
  4387. if ($this->type == "PDO") {
  4388. if ($mode == "assoc") {
  4389. $mode = PDO::FETCH_ASSOC;
  4390. } else if ($mode == "num") {
  4391. $mode = PDO::FETCH_NUM;
  4392. } else {
  4393. $mode = PDO::FETCH_BOTH;
  4394. }
  4395. return $result->fetch($mode);
  4396. } else if ($this->type == "SQLite3") {
  4397. if ($mode == "assoc") {
  4398. $mode = SQLITE3_ASSOC;
  4399. } else if ($mode == "num") {
  4400. $mode = SQLITE3_NUM;
  4401. } else {
  4402. $mode = SQLITE3_BOTH;
  4403. }
  4404. return $result->fetchArray($mode);
  4405. } else if ($this->type == "SQLiteDatabase") {
  4406. if ($mode == "assoc") {
  4407. $mode = SQLITE_ASSOC;
  4408. } else if ($mode == "num") {
  4409. $mode = SQLITE_NUM;
  4410. } else {
  4411. $mode = SQLITE_BOTH;
  4412. }
  4413. return $result->fetch($mode);
  4414. }
  4415. }
  4416. // SQlite supports multiple ways of surrounding names in quotes:
  4417. // single-quotes, double-quotes, backticks, square brackets.
  4418. // As sqlite does not keep this strict, we also need to be flexible here.
  4419. // This function generates a regex that matches any of the possibilities.
  4420. private function sqlite_surroundings_preg($name, $preg_quote = true, $notAllowedCharsIfNone = "'\"", $notAllowedName = false)
  4421. {
  4422. if ($name == "*" || $name == "+") {
  4423. if ($notAllowedName !== false && $preg_quote) {
  4424. $notAllowedName = preg_quote($notAllowedName, "/");
  4425. }
  4426. // use possesive quantifiers to save memory
  4427. // (There is a bug in PCRE starting in 8.13 and fixed in PCRE 8.36
  4428. // why we can't use posesive quantifiers - See issue #310).
  4429. if (version_compare(strstr(constant('PCRE_VERSION'), ' ', true), '8.36', '>=') ||
  4430. version_compare(strstr(constant('PCRE_VERSION'), ' ', true), '8.12', '<=')) {
  4431. $posessive = '+';
  4432. } else {
  4433. $posessive = '';
  4434. }
  4435. $nameSingle = ($notAllowedName !== false ? "(?!" . $notAllowedName . "')" : "") . "(?:[^']$name+|'')$name" . $posessive;
  4436. $nameDouble = ($notAllowedName !== false ? "(?!" . $notAllowedName . "\")" : "") . "(?:[^\"]$name+|\"\")$name" . $posessive;
  4437. $nameBacktick = ($notAllowedName !== false ? "(?!" . $notAllowedName . "`)" : "") . "(?:[^`]$name+|``)$name" . $posessive;
  4438. $nameSquare = ($notAllowedName !== false ? "(?!" . $notAllowedName . "\])" : "") . "(?:[^\]]$name+|\]\])$name" . $posessive;
  4439. $nameNo = ($notAllowedName !== false ? "(?!" . $notAllowedName . "\s)" : "") . "[^" . $notAllowedCharsIfNone . "]$name";
  4440. } else {
  4441. if ($preg_quote) {
  4442. $name = preg_quote($name, "/");
  4443. }
  4444. $nameSingle = str_replace("'", "''", $name);
  4445. $nameDouble = str_replace('"', '""', $name);
  4446. $nameBacktick = str_replace('`', '``', $name);
  4447. $nameSquare = str_replace(']', ']]', $name);
  4448. $nameNo = $name;
  4449. }
  4450. $preg = "(?:'" . $nameSingle . "'|" . // single-quote surrounded or not in quotes (correct SQL for values/new names)
  4451. $nameNo . "|" . // not surrounded (correct SQL if not containing reserved words, spaces or some special chars)
  4452. "\"" . $nameDouble . "\"|" . // double-quote surrounded (correct SQL for identifiers)
  4453. "`" . $nameBacktick . "`|" . // backtick surrounded (MySQL-Style)
  4454. "\[" . $nameSquare . "\])"; // square-bracket surrounded (MS Access/SQL server-Style)
  4455. return $preg;
  4456. }
  4457. // Returns the last PREG error as a string, '' if no error occured
  4458. private function getPregError()
  4459. {
  4460. $error = preg_last_error();
  4461. switch ($error) {
  4462. case PREG_NO_ERROR:return 'No error';
  4463. case PREG_INTERNAL_ERROR:return 'There is an internal error!';
  4464. case PREG_BACKTRACK_LIMIT_ERROR:return 'Backtrack limit was exhausted!';
  4465. case PREG_RECURSION_LIMIT_ERROR:return 'Recursion limit was exhausted!';
  4466. case PREG_BAD_UTF8_ERROR:return 'Bad UTF8 error!';
  4467. // PREG_BAD_UTF8_OFFSET_ERROR is introduced in PHP 5.3.0, which is not yet required by PLA, so we use its value 5 instead so long
  4468. case 5:return 'Bad UTF8 offset error!';
  4469. default:return 'Unknown Error';
  4470. }
  4471. }
  4472. // function that is called for an alter table statement in a query
  4473. // code borrowed with permission from http://code.jenseng.com/db/
  4474. // this has been completely debugged / rewritten by Christopher Kramer
  4475. public function alterTable($table, $alterdefs)
  4476. {
  4477. global $debug, $lang;
  4478. $this->alterError = "";
  4479. $errormsg = sprintf($lang['alter_failed'], htmlencode($table)) . ' - ';
  4480. if ($debug) {
  4481. echo "ALTER TABLE: table=($table), alterdefs=($alterdefs), PCRE version=(" . PCRE_VERSION . ")<hr /><br />";
  4482. }
  4483. if ($alterdefs != '') {
  4484. $recreateQueries = array();
  4485. $resultArr = $this->selectArray("SELECT sql,name,type FROM sqlite_master WHERE tbl_name = " . $this->quote($table));
  4486. if (sizeof($resultArr) < 1) {
  4487. $this->alterError = $errormsg . sprintf($lang['tbl_inexistent'], htmlencode($table));
  4488. if ($debug) {
  4489. echo "ERROR: unknown table<hr /><br />";
  4490. }
  4491. return false;
  4492. }
  4493. for ($i = 0; $i < sizeof($resultArr); $i++) {
  4494. $row = $resultArr[$i];
  4495. if ($row['type'] != 'table') {
  4496. if ($row['sql'] != '') {
  4497. // store the CREATE statements of triggers and indexes to recreate them later
  4498. $recreateQueries[] = $row;
  4499. if ($debug) {
  4500. echo "recreate=(" . $row['sql'] . ";)<br />";
  4501. }
  4502. }
  4503. } else {
  4504. // ALTER the table
  4505. $tmpname = 't' . time();
  4506. $origsql = $row['sql'];
  4507. $preg_remove_create_table = "/^\s*+CREATE\s++TABLE\s++" . $this->sqlite_surroundings_preg($table) . "\s*+(\(.*+)$/is";
  4508. $origsql_no_create = preg_replace($preg_remove_create_table, '$1', $origsql, 1);
  4509. if ($debug) {
  4510. echo "origsql=($origsql)<br />preg_remove_create_table=($preg_remove_create_table)<br />";
  4511. }
  4512. if ($origsql_no_create == $origsql) {
  4513. $this->alterError = $errormsg . $lang['alter_tbl_name_not_replacable'];
  4514. if ($debug) {
  4515. echo "ERROR: could not get rid of CREATE TABLE<hr />";
  4516. }
  4517. return false;
  4518. }
  4519. $createtemptableSQL = "CREATE TABLE " . $this->quote($tmpname) . " " . $origsql_no_create;
  4520. if ($debug) {
  4521. echo "createtemptableSQL=($createtemptableSQL)<br />";
  4522. }
  4523. $createindexsql = array();
  4524. $preg_alter_part = "/(?:DROP(?! PRIMARY KEY)|ADD(?! PRIMARY KEY)|CHANGE|RENAME TO|ADD PRIMARY KEY|DROP PRIMARY KEY)" // the ALTER command
  4525. . "(?:"
  4526. . "\s+\(" . $this->sqlite_surroundings_preg("+", false, "\"'\[`)") . "+\)" // stuff in brackets (in case of ADD PRIMARY KEY)
  4527. . "|" // or
  4528. . "\s+" . $this->sqlite_surroundings_preg("+", false, ",'\"\[`") // column names and stuff like this
  4529. . ")*/i";
  4530. if ($debug) {
  4531. echo "preg_alter_part=(" . $preg_alter_part . ")<br />";
  4532. }
  4533. preg_match_all($preg_alter_part, $alterdefs, $matches);
  4534. $defs = $matches[0];
  4535. $get_oldcols_query = "PRAGMA table_info(" . $this->quote_id($table) . ")";
  4536. $result_oldcols = $this->selectArray($get_oldcols_query);
  4537. $newcols = array();
  4538. $coltypes = array();
  4539. $primarykey = array();
  4540. foreach ($result_oldcols as $column_info) {
  4541. $newcols[$column_info['name']] = $column_info['name'];
  4542. $coltypes[$column_info['name']] = $column_info['type'];
  4543. if ($column_info['pk']) {
  4544. $primarykey[] = $column_info['name'];
  4545. }
  4546. }
  4547. $newcolumns = '';
  4548. $oldcolumns = '';
  4549. reset($newcols);
  4550. while (list($key, $val) = each($newcols)) {
  4551. $newcolumns .= ($newcolumns ? ', ' : '') . $this->quote_id($val);
  4552. $oldcolumns .= ($oldcolumns ? ', ' : '') . $this->quote_id($key);
  4553. }
  4554. $copytotempsql = 'INSERT INTO ' . $this->quote_id($tmpname) . '(' . $newcolumns . ') SELECT ' . $oldcolumns . ' FROM ' . $this->quote_id($table);
  4555. $dropoldsql = 'DROP TABLE ' . $this->quote_id($table);
  4556. $createtesttableSQL = $createtemptableSQL;
  4557. if (count($defs) < 1) {
  4558. $this->alterError = $errormsg . $lang['alter_no_def'];
  4559. if ($debug) {
  4560. echo "ERROR: defs&lt;1<hr /><br />";
  4561. }
  4562. return false;
  4563. }
  4564. foreach ($defs as $def) {
  4565. if ($debug) {
  4566. echo "<hr />def=$def<br />";
  4567. }
  4568. $preg_parse_def =
  4569. "/^(DROP(?! PRIMARY KEY)|ADD(?! PRIMARY KEY)|CHANGE|RENAME TO|ADD PRIMARY KEY|DROP PRIMARY KEY)" // $matches[1]: command
  4570. . "(?:" // this is either
  4571. . "(?:\s+\((.+)\)\s*$)" // anything in brackets (for ADD PRIMARY KEY)
  4572. // then $matches[2] is what there is in brackets
  4573. . "|" // OR:
  4574. . "(?:\s+\"((?:[^\"]|\"\")+)\"|\s+'((?:[^']|'')+)')" // (first) column name, either in single or double quotes
  4575. // in case of RENAME TO, it is the new a table name
  4576. // $matches[3] will be the column/table name without the quotes if double quoted
  4577. // $matches[4] will be the column/table name without the quotes if single quoted
  4578. . "(" // $matches[5]: anything after the column name
  4579. . "(?:\s+'((?:[^']|'')+)')?" // $matches[6] (optional): a second column name surrounded with single quotes
  4580. // (the match does not contain the quotes)
  4581. . "\s+"
  4582. . "((?:[A-Z]+\s*)+(?:\(\s*[+-]?\s*[0-9]+(?:\s*,\s*[+-]?\s*[0-9]+)?\s*\))?)\s*" // $matches[7]: a type name
  4583. . ".*" .
  4584. ")"
  4585. . "?\s*$"
  4586. . ")?\s*$/i"; // in case of DROP PRIMARY KEY, there is nothing after the command
  4587. if ($debug) {
  4588. echo "preg_parse_def=$preg_parse_def<br />";
  4589. }
  4590. $parse_def = preg_match($preg_parse_def, $def, $matches);
  4591. if ($parse_def === false) {
  4592. $this->alterError = $errormsg . $lang['alter_parse_failed'];
  4593. if ($debug) {
  4594. echo "ERROR: !parse_def<hr /><br />";
  4595. }
  4596. return false;
  4597. }
  4598. if (!isset($matches[1])) {
  4599. $this->alterError = $errormsg . $lang['alter_action_not_recognized'];
  4600. if ($debug) {
  4601. echo "ERROR: !isset(matches[1])<hr /><br />";
  4602. }
  4603. return false;
  4604. }
  4605. $action = strtolower($matches[1]);
  4606. if (($action == 'add' || $action == 'rename to') && isset($matches[4]) && $matches[4] != '') {
  4607. $column = str_replace("''", "'", $matches[4]);
  4608. }
  4609. // enclosed in ''
  4610. elseif ($action == 'add primary key' && isset($matches[2]) && $matches[2] != '') {
  4611. $column = $matches[2];
  4612. } elseif ($action == 'drop primary key') {
  4613. $column = '';
  4614. }
  4615. // DROP PRIMARY KEY has no column definition
  4616. elseif (isset($matches[3]) && $matches[3] != '') {
  4617. $column = str_replace('""', '"', $matches[3]);
  4618. }
  4619. // enclosed in ""
  4620. else {
  4621. $column = '';
  4622. }
  4623. $column_escaped = str_replace("'", "''", $column);
  4624. if ($debug) {
  4625. echo "action=($action), column=($column), column_escaped=($column_escaped)<br />";
  4626. }
  4627. /* we build a regex that devides the CREATE TABLE statement parts:
  4628. Part example Group Explanation
  4629. 1. CREATE TABLE t... ( $1
  4630. 2. 'col1' ..., 'col2' ..., 'colN' ..., $3 (with col1-colN being columns that are not changed and listed before the col to change)
  4631. 3. 'colX' ..., (with colX being the column to change/drop)
  4632. 4. 'colX+1' ..., ..., 'colK') $5 (with colX+1-colK being columns after the column to change/drop)
  4633. */
  4634. $preg_create_table = "\s*+(CREATE\s++TABLE\s++" . preg_quote($this->quote($tmpname), "/") . "\s*+\()"; // This is group $1 (keep unchanged)
  4635. $preg_column_definiton = "\s*+" . $this->sqlite_surroundings_preg("+", true, " '\"\[`,", $column) . "(?:\s*+" . $this->sqlite_surroundings_preg("*", false, "'\",`\[ ") . ")++"; // catches a complete column definition, even if it is
  4636. // 'column' TEXT NOT NULL DEFAULT 'we have a comma, here and a double ''quote!'
  4637. // this definition does NOT match columns with the column name $column
  4638. if ($debug) {
  4639. echo "preg_column_definition=(" . $preg_column_definiton . ")<br />";
  4640. }
  4641. $preg_columns_before = // columns before the one changed/dropped (keep)
  4642. "(?:" .
  4643. "(" . // group $2. Keep this one unchanged!
  4644. "(?:" .
  4645. "$preg_column_definiton,\s*+" . // column definition + comma
  4646. ")*" . // there might be any number of such columns here
  4647. $preg_column_definiton . // last column definition
  4648. ")" . // end of group $2
  4649. ",\s*+" // the last comma of the last column before the column to change. Do not keep it!
  4650. . ")?"; // there might be no columns before
  4651. if ($debug) {
  4652. echo "preg_columns_before=(" . $preg_columns_before . ")<br />";
  4653. }
  4654. $preg_columns_after = "(,\s*(.+))?"; // the columns after the column to drop. This is group $3 (drop) or $4(change) (keep!)
  4655. // we could remove the comma using $6 instead of $5, but then we might have no comma at all.
  4656. // Keeping it leaves a problem if we drop the first column, so we fix that case in another regex.
  4657. $table_new = $table;
  4658. switch ($action) {
  4659. case 'add':
  4660. if ($column == '') {
  4661. $this->alterError = $errormsg . ' (add) - ' . $lang['alter_no_add_col'];
  4662. return false;
  4663. }
  4664. $new_col_definition = "'$column_escaped' " . (isset($matches[5]) ? $matches[5] : '');
  4665. $preg_pattern_add = "/^" . $preg_create_table . // the CREATE TABLE statement ($1)
  4666. "((?:(?!,\s*(?:PRIMARY\s+KEY\s*\(|CONSTRAINT\s|UNIQUE\s*\(|CHECK\s*\(|FOREIGN\s+KEY\s*\()).)*)" . // column definitions ($2)
  4667. "(.*)\\)\s*$/si"; // table-constraints like PRIMARY KEY(a,b) ($3) and the closing bracket
  4668. // append the column definiton in the CREATE TABLE statement
  4669. $newSQL = preg_replace($preg_pattern_add, '$1$2, ' . strtr($new_col_definition, array('\\' => '\\\\', '$' => '\$')) . ' $3', $createtesttableSQL) . ')';
  4670. $preg_error = $this->getPregError();
  4671. if ($debug) {
  4672. echo $createtesttableSQL . "<hr /><br />";
  4673. echo $newSQL . "<hr /><br />";
  4674. echo $preg_pattern_add . "<hr /><br />";
  4675. }
  4676. if ($newSQL == $createtesttableSQL) // pattern did not match, so column adding did not succed
  4677. {
  4678. $this->alterError = $errormsg . ' (add) - ' . $lang['alter_pattern_mismatch'] . '. PREG ERROR: ' . $preg_error;
  4679. return false;
  4680. }
  4681. $createtesttableSQL = $newSQL;
  4682. break;
  4683. case 'change':
  4684. if (!isset($matches[6]) || !isset($matches[7])) {
  4685. $this->alterError = $errormsg . ' (change) - ' . $lang['alter_col_not_recognized'];
  4686. return false;
  4687. }
  4688. $new_col_name = $matches[6];
  4689. $new_col_type = $matches[7];
  4690. $new_col_definition = "'$new_col_name' $new_col_type";
  4691. $preg_column_to_change = "\s*" . $this->sqlite_surroundings_preg($column) . "(?:\s+" . preg_quote($coltypes[$column]) . ")?(\s+(?:" . $this->sqlite_surroundings_preg("*", false, ",'\"`\[") . ")+)?";
  4692. // replace this part (we want to change this column)
  4693. // group $3 contains the column constraints (keep!). the name & data type is replaced.
  4694. $preg_pattern_change = "/^" . $preg_create_table . $preg_columns_before . $preg_column_to_change . $preg_columns_after . "\s*\\)\s*$/s";
  4695. // replace the column definiton in the CREATE TABLE statement
  4696. $newSQL = preg_replace($preg_pattern_change, '$1$2,' . strtr($new_col_definition, array('\\' => '\\\\', '$' => '\$')) . '$3$4)', $createtesttableSQL);
  4697. $preg_error = $this->getPregError();
  4698. // remove comma at the beginning if the first column is changed
  4699. // probably somebody is able to put this into the first regex (using lookahead probably).
  4700. $newSQL = preg_replace("/^\s*(CREATE\s+TABLE\s+" . preg_quote($this->quote($tmpname), "/") . "\s+\(),\s*/", '$1', $newSQL);
  4701. if ($debug) {
  4702. echo "preg_column_to_change=(" . $preg_column_to_change . ")<hr /><br />";
  4703. echo $createtesttableSQL . "<hr /><br />";
  4704. echo $newSQL . "<hr /><br />";
  4705. echo $preg_pattern_change . "<hr /><br />";
  4706. }
  4707. if ($newSQL == $createtesttableSQL || $newSQL == "") // pattern did not match, so column removal did not succed
  4708. {
  4709. $this->alterError = $errormsg . ' (change) - ' . $lang['alter_pattern_mismatch'] . '. PREG ERROR: ' . $preg_error;
  4710. return false;
  4711. }
  4712. $createtesttableSQL = $newSQL;
  4713. $newcols[$column] = str_replace("''", "'", $new_col_name);
  4714. break;
  4715. case 'drop':
  4716. $preg_column_to_drop = "\s*" . $this->sqlite_surroundings_preg($column) . "\s+(?:" . $this->sqlite_surroundings_preg("*", false, ",'\"\[`") . ")+"; // delete this part (we want to drop this column)
  4717. $preg_pattern_drop = "/^" . $preg_create_table . $preg_columns_before . $preg_column_to_drop . $preg_columns_after . "\s*\\)\s*$/s";
  4718. // remove the column out of the CREATE TABLE statement
  4719. $newSQL = preg_replace($preg_pattern_drop, '$1$2$3)', $createtesttableSQL);
  4720. $preg_error = $this->getPregError();
  4721. // remove comma at the beginning if the first column is removed
  4722. // probably somebody is able to put this into the first regex (using lookahead probably).
  4723. $newSQL = preg_replace("/^\s*(CREATE\s+TABLE\s+" . preg_quote($this->quote($tmpname), "/") . "\s+\(),\s*/", '$1', $newSQL);
  4724. if ($debug) {
  4725. echo $createtesttableSQL . "<hr /><br />";
  4726. echo $newSQL . "<hr /><br />";
  4727. echo $preg_pattern_drop . "<hr /><br />";
  4728. }
  4729. if ($newSQL == $createtesttableSQL || $newSQL == "") // pattern did not match, so column removal did not succed
  4730. {
  4731. $this->alterError = $errormsg . ' (drop) - ' . $lang['alter_pattern_mismatch'] . '. PREG ERROR: ' . $preg_error;
  4732. return false;
  4733. }
  4734. $createtesttableSQL = $newSQL;
  4735. unset($newcols[$column]);
  4736. break;
  4737. case 'rename to':
  4738. // don't change column definition at all
  4739. $newSQL = $createtesttableSQL;
  4740. // only change the name of the table
  4741. $table_new = $column;
  4742. break;
  4743. case 'add primary key':
  4744. // we want to add a primary key for the column(s) stored in $column
  4745. $newSQL = preg_replace("/\)\s*$/", ", PRIMARY KEY (" . $column . ") )", $createtesttableSQL);
  4746. $createtesttableSQL = $newSQL;
  4747. break;
  4748. case 'drop primary key':
  4749. // we want to drop the primary key
  4750. if ($debug) {
  4751. echo "DROP";
  4752. }
  4753. if (sizeof($primarykey) == 1) {
  4754. // if not compound primary key, might be a column constraint -> try removal
  4755. $column = $primarykey[0];
  4756. if ($debug) {
  4757. echo "<br>Trying to drop column constraint for column $column <br>";
  4758. }
  4759. /*
  4760. TODO: This does not work yet:
  4761. CREATE TABLE 't12' ('t1' INTEGER CONSTRAINT "bla" NOT NULL CONSTRAINT 'pk' PRIMARY KEY ); ALTER TABLE "t12" DROP PRIMARY KEY
  4762. This does: ! !
  4763. CREATE TABLE 't12' ('t1' INTEGER CONSTRAINT bla NOT NULL CONSTRAINT 'pk' PRIMARY KEY ); ALTER TABLE "t12" DROP PRIMARY KEY
  4764. */
  4765. $preg_column_to_change = "(\s*" . $this->sqlite_surroundings_preg($column) . ")" . // column ($3)
  4766. "(?:" . // opt. type and column constraints
  4767. "(\s+(?:" . $this->sqlite_surroundings_preg("(?:[^PC,'\"`\[]|P(?!RIMARY\s+KEY)|" .
  4768. "C(?!ONSTRAINT\s+" . $this->sqlite_surroundings_preg("+", false, " ,'\"\[`") . "\s+PRIMARY\s+KEY))", false, ",'\"`\[") . ")*)" . // column constraints before PRIMARY KEY ($3)
  4769. // primary key constraint (remove this!):
  4770. "(?:CONSTRAINT\s+" . $this->sqlite_surroundings_preg("+", false, " ,'\"\[`") . "\s+)?" .
  4771. "PRIMARY\s+KEY" .
  4772. "(?:\s+(?:ASC|DESC))?" .
  4773. "(?:\s+ON\s+CONFLICT\s+(?:ROLLBACK|ABORT|FAIL|IGNORE|REPLACE))?" .
  4774. "(?:\s+AUTOINCREMENT)?" .
  4775. "((?:" . $this->sqlite_surroundings_preg("*", false, ",'\"`\[") . ")*)" . // column constraints after PRIMARY KEY ($4)
  4776. ")";
  4777. // replace this part (we want to change this column)
  4778. // group $3 (column) $4 (constraints before) and $5 (constraints after) contain the part to keep
  4779. $preg_pattern_change = "/^" . $preg_create_table . $preg_columns_before . $preg_column_to_change . $preg_columns_after . "\s*\\)\s*$/si";
  4780. // replace the column definiton in the CREATE TABLE statement
  4781. $newSQL = preg_replace($preg_pattern_change, '$1$2,$3$4$5$6)', $createtesttableSQL);
  4782. // remove comma at the beginning if the first column is changed
  4783. // probably somebody is able to put this into the first regex (using lookahead probably).
  4784. $newSQL = preg_replace("/^\s*(CREATE\s+TABLE\s+" . preg_quote($this->quote($tmpname), "/") . "\s+\(),\s*/", '$1', $newSQL);
  4785. if ($debug) {
  4786. echo "preg_column_to_change=(" . $preg_column_to_change . ")<hr /><br />";
  4787. echo $createtesttableSQL . "<hr /><br />";
  4788. echo $newSQL . "<hr /><br />";
  4789. echo $preg_pattern_change . "<hr /><br />";
  4790. }
  4791. if ($newSQL != $createtesttableSQL && $newSQL != "") // pattern did match, so PRIMARY KEY constraint removed :)
  4792. {
  4793. $createtesttableSQL = $newSQL;
  4794. if ($debug) {
  4795. echo "<br>SUCCEEDED<br>";
  4796. }
  4797. } else {
  4798. if ($debug) {
  4799. echo "NO LUCK";
  4800. }
  4801. // TODO: try removing table constraint
  4802. return false;
  4803. }
  4804. $createtesttableSQL = $newSQL;
  4805. } else
  4806. // TODO: Try removing table constraint
  4807. {
  4808. return false;
  4809. }
  4810. break;
  4811. default:
  4812. if ($debug) {
  4813. echo 'ERROR: unknown alter operation!<hr /><br />';
  4814. }
  4815. $this->alterError = $errormsg . $lang['alter_unknown_operation'];
  4816. return false;
  4817. }
  4818. }
  4819. $droptempsql = 'DROP TABLE ' . $this->quote_id($tmpname);
  4820. $createnewtableSQL = "CREATE TABLE " . $this->quote($table_new) . " " . preg_replace("/^\s*CREATE\s+TABLE\s+'?" . str_replace("'", "''", preg_quote($tmpname, "/")) . "'?\s+(.*)$/is", '$1', $createtesttableSQL, 1);
  4821. $newcolumns = '';
  4822. $oldcolumns = '';
  4823. reset($newcols);
  4824. while (list($key, $val) = each($newcols)) {
  4825. $newcolumns .= ($newcolumns ? ', ' : '') . $this->quote_id($val);
  4826. $oldcolumns .= ($oldcolumns ? ', ' : '') . $this->quote_id($key);
  4827. }
  4828. $copytonewsql = 'INSERT INTO ' . $this->quote_id($table_new) . '(' . $newcolumns . ') SELECT ' . $oldcolumns . ' FROM ' . $this->quote_id($tmpname);
  4829. }
  4830. }
  4831. $alter_transaction = 'BEGIN; ';
  4832. $alter_transaction .= $createtemptableSQL . '; '; //create temp table
  4833. $alter_transaction .= $copytotempsql . '; '; //copy to table
  4834. $alter_transaction .= $dropoldsql . '; '; //drop old table
  4835. $alter_transaction .= $createnewtableSQL . '; '; //recreate original table
  4836. $alter_transaction .= $copytonewsql . '; '; //copy back to original table
  4837. $alter_transaction .= $droptempsql . '; '; //drop temp table
  4838. $preg_index = "/^\s*(CREATE\s+(?:UNIQUE\s+)?INDEX\s+(?:" . $this->sqlite_surroundings_preg("+", false, " '\"\[`") . "\s*)*ON\s+)(" . $this->sqlite_surroundings_preg($table) . ")(\s*\((?:" . $this->sqlite_surroundings_preg("+", false, " '\"\[`") . "\s*)*\)\s*)\s*$/i";
  4839. foreach ($recreateQueries as $recreate_query) {
  4840. if ($recreate_query['type'] == 'index') {
  4841. // this is an index. We need to make sure the index is not on a column that we drop. If it is, we drop the index as well.
  4842. $indexInfos = $this->selectArray('PRAGMA index_info(' . $this->quote_id($recreate_query['name']) . ')');
  4843. foreach ($indexInfos as $indexInfo) {
  4844. if (!isset($newcols[$indexInfo['name']])) {
  4845. if ($debug) {
  4846. echo 'Not recreating the following index: <hr /><br />' . htmlencode($recreate_query['sql']) . '<hr /><br />';
  4847. }
  4848. // Index on a column that was dropped. Skip recreation.
  4849. continue 2;
  4850. }
  4851. }
  4852. }
  4853. // TODO: In case we renamed a column on which there is an index, we need to recreate the index with the column name adjusted.
  4854. // recreate triggers / indexes
  4855. if ($table == $table_new) {
  4856. // we had no RENAME TO, so we can recreate indexes/triggers just like the original ones
  4857. $alter_transaction .= $recreate_query['sql'] . ';';
  4858. } else {
  4859. // we had a RENAME TO, so we need to exchange the table-name in the CREATE-SQL of triggers & indexes
  4860. switch ($recreate_query['type']) {
  4861. case 'index':
  4862. $recreate_queryIndex = preg_replace($preg_index, '$1' . $this->quote_id(strtr($table_new, array('\\' => '\\\\', '$' => '\$'))) . '$3 ', $recreate_query['sql']);
  4863. if ($recreate_queryIndex != $recreate_query['sql'] && $recreate_queryIndex != null) {
  4864. $alter_transaction .= $recreate_queryIndex . ';';
  4865. } else {
  4866. // the CREATE INDEX regex did not match. this normally should not happen
  4867. if ($debug) {
  4868. echo 'ERROR: CREATE INDEX regex did not match!?<hr /><br />';
  4869. }
  4870. // just try to recreate the index originally (will fail most likely)
  4871. $alter_transaction .= $recreate_query['sql'] . ';';
  4872. }
  4873. break;
  4874. case 'trigger':
  4875. // TODO: IMPLEMENT
  4876. $alter_transaction .= $recreate_query['sql'] . ';';
  4877. break;
  4878. default:
  4879. if ($debug) {
  4880. echo 'ERROR: Unknown type ' . htmlencode($recreate_query['type']) . '<hr /><br />';
  4881. }
  4882. $alter_transaction .= $recreate_query['sql'] . ';';
  4883. }
  4884. }
  4885. }
  4886. $alter_transaction .= 'COMMIT;';
  4887. if ($debug) {
  4888. echo $alter_transaction;
  4889. }
  4890. return $this->multiQuery($alter_transaction);
  4891. }
  4892. }
  4893. //multiple query execution
  4894. //returns true on success, false otherwise. Use getError() to fetch the error.
  4895. public function multiQuery($query)
  4896. {
  4897. if ($this->type == "PDO") {
  4898. $success = $this->db->exec($query);
  4899. } else if ($this->type == "SQLite3") {
  4900. $success = $this->db->exec($query);
  4901. } else {
  4902. $success = $this->db->queryExec($query, $error);
  4903. }
  4904. return $success;
  4905. }
  4906. // checks whether a table has a primary key
  4907. public function hasPrimaryKey($table)
  4908. {
  4909. $query = "PRAGMA table_info(" . $this->quote_id($table) . ")";
  4910. $table_info = $this->selectArray($query);
  4911. foreach ($table_info as $row_id => $row_data) {
  4912. if ($row_data['pk']) {
  4913. return true;
  4914. }
  4915. }
  4916. return false;
  4917. }
  4918. // Returns an array of columns by which rows can be uniquely adressed.
  4919. // For tables with a rowid column, this is always array('rowid')
  4920. // for tables without rowid, this is an array of the primary key columns.
  4921. public function getPrimaryKey($table)
  4922. {
  4923. $primary_key = array();
  4924. // check if this table has a rowid
  4925. $getRowID = $this->select('SELECT ROWID FROM ' . $this->quote_id($table) . ' LIMIT 0,1');
  4926. if (isset($getRowID[0]))
  4927. // it has, so we prefer addressing rows by rowid
  4928. {
  4929. return array('rowid');
  4930. } else {
  4931. // the table is without rowid, so use the primary key
  4932. $query = "PRAGMA table_info(" . $this->quote_id($table) . ")";
  4933. $table_info = $this->selectArray($query);
  4934. foreach ($table_info as $row_id => $row_data) {
  4935. if ($row_data['pk']) {
  4936. $primary_key[] = $row_data['name'];
  4937. }
  4938. }
  4939. }
  4940. return $primary_key;
  4941. }
  4942. // selects a row by a given key $pk, which is an array of values
  4943. // for the columns by which a row can be adressed (rowid or primary key)
  4944. public function wherePK($table, $pk)
  4945. {
  4946. $where = "";
  4947. $primary_key = $this->getPrimaryKey($table);
  4948. foreach ($primary_key as $pk_index => $column) {
  4949. if ($where != "") {
  4950. $where .= " AND ";
  4951. }
  4952. $where .= $this->quote_id($column) . ' = ';
  4953. if (is_int($pk[$pk_index]) || is_float($pk[$pk_index])) {
  4954. $where .= $pk[$pk_index];
  4955. } else {
  4956. $where .= $this->quote($pk[$pk_index]);
  4957. }
  4958. }
  4959. return $where;
  4960. }
  4961. //get number of rows in table
  4962. public function numRows($table, $dontTakeLong = false)
  4963. {
  4964. // as Count(*) can be slow on huge tables without PK,
  4965. // if $dontTakeLong is set and the size is > 2MB only count() if there is a PK
  4966. if (!$dontTakeLong || $this->getSize() <= 2000 || $this->hasPrimaryKey($table)) {
  4967. $result = $this->select("SELECT Count(*) FROM " . $this->quote_id($table));
  4968. return $result[0];
  4969. } else {
  4970. return '?';
  4971. }
  4972. }
  4973. //correctly escape a string to be injected into an SQL query
  4974. public function quote($value)
  4975. {
  4976. if ($this->type == "PDO") {
  4977. // PDO quote() escapes and adds quotes
  4978. return $this->db->quote($value);
  4979. } else if ($this->type == "SQLite3") {
  4980. return "'" . $this->db->escapeString($value) . "'";
  4981. } else {
  4982. return "'" . sqlite_escape_string($value) . "'";
  4983. }
  4984. }
  4985. //correctly escape an identifier (column / table / trigger / index name) to be injected into an SQL query
  4986. public function quote_id($value)
  4987. {
  4988. // double-quotes need to be escaped by doubling them
  4989. $value = str_replace('"', '""', $value);
  4990. return '"' . $value . '"';
  4991. }
  4992. //import sql
  4993. //returns true on success, error message otherwise
  4994. public function import_sql($query)
  4995. {
  4996. $import = $this->multiQuery($query);
  4997. if (!$import) {
  4998. return $this->getError();
  4999. } else {
  5000. return true;
  5001. }
  5002. }
  5003. //import csv
  5004. //returns true on success, error message otherwise
  5005. public function import_csv($filename, $table, $field_terminate, $field_enclosed, $field_escaped, $null, $fields_in_first_row)
  5006. {
  5007. // CSV import implemented by Christopher Kramer - http://www.christosoft.de
  5008. $csv_handle = fopen($filename, 'r');
  5009. $csv_insert = "BEGIN;\n";
  5010. $csv_number_of_rows = 0;
  5011. // PHP requires enclosure defined, but has no problem if it was not used
  5012. if ($field_enclosed == "") {
  5013. $field_enclosed = '"';
  5014. }
  5015. // PHP requires escaper defined
  5016. if ($field_escaped == "") {
  5017. $field_escaped = '\\';
  5018. }
  5019. while ($csv_handle !== false && !feof($csv_handle)) {
  5020. $csv_data = fgetcsv($csv_handle, 0, $field_terminate, $field_enclosed, $field_escaped);
  5021. if ($csv_data[0] != null || count($csv_data) > 1) {
  5022. $csv_number_of_rows++;
  5023. if ($fields_in_first_row && $csv_number_of_rows == 1) {
  5024. $fields_in_first_row = false;
  5025. continue;
  5026. }
  5027. $csv_col_number = count($csv_data);
  5028. $csv_insert .= "INSERT INTO " . $this->quote_id($table) . " VALUES (";
  5029. foreach ($csv_data as $csv_col => $csv_cell) {
  5030. if ($csv_cell == $null) {
  5031. $csv_insert .= "NULL";
  5032. } else {
  5033. $csv_insert .= $this->quote($csv_cell);
  5034. }
  5035. if ($csv_col == $csv_col_number - 2 && $csv_data[$csv_col + 1] == '') {
  5036. // the CSV row ends with the separator (like old phpliteadmin exported)
  5037. break;
  5038. }
  5039. if ($csv_col < $csv_col_number - 1) {
  5040. $csv_insert .= ",";
  5041. }
  5042. }
  5043. $csv_insert .= ");\n";
  5044. if ($csv_number_of_rows > 5000) {
  5045. $csv_insert .= "COMMIT;\nBEGIN;\n";
  5046. $csv_number_of_rows = 0;
  5047. }
  5048. }
  5049. }
  5050. if ($csv_handle === false) {
  5051. return "Error reading CSV file";
  5052. } else {
  5053. $csv_insert .= "COMMIT;";
  5054. fclose($csv_handle);
  5055. $import = $this->multiQuery($csv_insert);
  5056. if (!$import) {
  5057. return $this->getError();
  5058. } else {
  5059. return true;
  5060. }
  5061. }
  5062. }
  5063. //export csv
  5064. public function export_csv($tables, $field_terminate, $field_enclosed, $field_escaped, $null, $crlf, $fields_in_first_row)
  5065. {
  5066. @set_time_limit(-1);
  5067. $query = "SELECT * FROM sqlite_master WHERE type='table' or type='view' ORDER BY type DESC";
  5068. $result = $this->selectArray($query);
  5069. for ($i = 0; $i < sizeof($result); $i++) {
  5070. $valid = false;
  5071. for ($j = 0; $j < sizeof($tables); $j++) {
  5072. if ($result[$i]['tbl_name'] == $tables[$j]) {
  5073. $valid = true;
  5074. }
  5075. }
  5076. if ($valid) {
  5077. $query = "PRAGMA table_info(" . $this->quote_id($result[$i]['tbl_name']) . ")";
  5078. $temp = $this->selectArray($query);
  5079. $cols = array();
  5080. for ($z = 0; $z < sizeof($temp); $z++) {
  5081. $cols[$z] = $temp[$z][1];
  5082. }
  5083. if ($fields_in_first_row) {
  5084. for ($z = 0; $z < sizeof($cols); $z++) {
  5085. echo $field_enclosed . $cols[$z] . $field_enclosed;
  5086. // do not terminate the last column!
  5087. if ($z < sizeof($cols) - 1) {
  5088. echo $field_terminate;
  5089. }
  5090. }
  5091. echo "\r\n";
  5092. }
  5093. $query = "SELECT * FROM " . $this->quote_id($result[$i]['tbl_name']);
  5094. $table_result = $this->query($query);
  5095. $firstRow = true;
  5096. while ($row = $this->fetch($table_result, "assoc")) {
  5097. if (!$firstRow) {
  5098. echo "\r\n";
  5099. } else {
  5100. $firstRow = false;
  5101. }
  5102. for ($y = 0; $y < sizeof($cols); $y++) {
  5103. $cell = $row[$cols[$y]];
  5104. if ($crlf) {
  5105. $cell = str_replace("\n", "", $cell);
  5106. $cell = str_replace("\r", "", $cell);
  5107. }
  5108. $cell = str_replace($field_terminate, $field_escaped . $field_terminate, $cell);
  5109. $cell = str_replace($field_enclosed, $field_escaped . $field_enclosed, $cell);
  5110. // do not enclose NULLs
  5111. if ($cell == null) {
  5112. echo $null;
  5113. } else {
  5114. echo $field_enclosed . $cell . $field_enclosed;
  5115. }
  5116. // do not terminate the last column!
  5117. if ($y < sizeof($cols) - 1) {
  5118. echo $field_terminate;
  5119. }
  5120. }
  5121. }
  5122. if ($i < sizeof($result) - 1) {
  5123. echo "\r\n";
  5124. }
  5125. }
  5126. }
  5127. }
  5128. //export sql
  5129. public function export_sql($tables, $drop, $structure, $data, $transaction, $comments)
  5130. {
  5131. global $lang;
  5132. @set_time_limit(-1);
  5133. if ($comments) {
  5134. echo "----\r\n";
  5135. echo "-- " . PROJECT . " " . $lang['db_dump'] . " (" . PROJECT_URL . ")\r\n";
  5136. echo "-- " . PROJECT . " " . $lang['ver'] . ": " . VERSION . "\r\n";
  5137. echo "-- " . $lang['exported'] . ": " . date($lang['date_format']) . "\r\n";
  5138. echo "-- " . $lang['db_f'] . ": " . $this->getPath() . "\r\n";
  5139. echo "----\r\n";
  5140. }
  5141. $query = "SELECT * FROM sqlite_master WHERE type='table' OR type='index' OR type='view' OR type='trigger' ORDER BY type='trigger', type='index', type='view', type='table'";
  5142. $result = $this->selectArray($query);
  5143. if ($transaction) {
  5144. echo "BEGIN TRANSACTION;\r\n";
  5145. }
  5146. //iterate through each table
  5147. for ($i = 0; $i < sizeof($result); $i++) {
  5148. $valid = false;
  5149. for ($j = 0; $j < sizeof($tables); $j++) {
  5150. if ($result[$i]['tbl_name'] == $tables[$j]) {
  5151. $valid = true;
  5152. }
  5153. }
  5154. if ($valid) {
  5155. if ($drop) {
  5156. if ($comments) {
  5157. echo "\r\n----\r\n";
  5158. echo "-- " . $lang['drop'] . " " . $result[$i]['type'] . " " . $lang['for'] . " " . $result[$i]['name'] . "\r\n";
  5159. echo "----\r\n";
  5160. }
  5161. echo "DROP " . strtoupper($result[$i]['type']) . " IF EXISTS " . $this->quote_id($result[$i]['name']) . ";\r\n";
  5162. }
  5163. if ($structure) {
  5164. if ($comments) {
  5165. echo "\r\n----\r\n";
  5166. if ($result[$i]['type'] == "table" || $result[$i]['type'] == "view") {
  5167. echo "-- " . ucfirst($result[$i]['type']) . " " . $lang['struct_for'] . " " . $result[$i]['tbl_name'] . "\r\n";
  5168. } else // index or trigger
  5169. {
  5170. echo "-- " . $lang['struct_for'] . " " . $result[$i]['type'] . " " . $result[$i]['name'] . " " . $lang['on_tbl'] . " " . $result[$i]['tbl_name'] . "\r\n";
  5171. }
  5172. echo "----\r\n";
  5173. }
  5174. echo $result[$i]['sql'] . ";\r\n";
  5175. }
  5176. if ($data && $result[$i]['type'] == "table") {
  5177. $query = "SELECT * FROM " . $this->quote_id($result[$i]['tbl_name']);
  5178. $table_result = $this->query($query, "assoc");
  5179. if ($comments) {
  5180. $numRows = $this->numRows($result[$i]['tbl_name']);
  5181. echo "\r\n----\r\n";
  5182. echo "-- " . $lang['data_dump'] . " " . $result[$i]['tbl_name'] . ", " . sprintf($lang['total_rows'], $numRows) . "\r\n";
  5183. echo "----\r\n";
  5184. }
  5185. $query = "PRAGMA table_info(" . $this->quote_id($result[$i]['tbl_name']) . ")";
  5186. $temp = $this->selectArray($query);
  5187. $cols = array();
  5188. $cols_quoted = array();
  5189. for ($z = 0; $z < sizeof($temp); $z++) {
  5190. $cols[$z] = $temp[$z][1];
  5191. $cols_quoted[$z] = $this->quote_id($temp[$z][1]);
  5192. }
  5193. while ($row = $this->fetch($table_result)) {
  5194. $vals = array();
  5195. for ($y = 0; $y < sizeof($cols); $y++) {
  5196. if ($row[$cols[$y]] === null) {
  5197. $vals[$cols[$y]] = 'NULL';
  5198. } else {
  5199. $vals[$cols[$y]] = $this->quote($row[$cols[$y]]);
  5200. }
  5201. }
  5202. echo "INSERT INTO " . $this->quote_id($result[$i]['tbl_name']) . " (" . implode(",", $cols_quoted) . ") VALUES (" . implode(",", $vals) . ");\r\n";
  5203. }
  5204. }
  5205. }
  5206. }
  5207. if ($transaction) {
  5208. echo "COMMIT;\r\n";
  5209. }
  5210. }
  5211. }
  5212. // class MicroTimer (issue #146)
  5213. // wraps calls to microtime(), calculating the elapsed time and rounding output
  5214. //
  5215. class MicroTimer
  5216. {
  5217. private $startTime, $stopTime;
  5218. // creates and starts a timer
  5219. public function __construct()
  5220. {
  5221. $this->startTime = microtime(true);
  5222. }
  5223. // stops a timer
  5224. public function stop()
  5225. {
  5226. $this->stopTime = microtime(true);
  5227. }
  5228. // returns the number of seconds from the timer's creation, or elapsed
  5229. // between creation and call to ->stop()
  5230. public function elapsed()
  5231. {
  5232. if ($this->stopTime) {
  5233. return round($this->stopTime - $this->startTime, 4);
  5234. }
  5235. return round(microtime(true) - $this->startTime, 4);
  5236. }
  5237. // called when using a MicroTimer object as a string
  5238. public function __toString()
  5239. {
  5240. return (string) $this->elapsed();
  5241. }
  5242. }
  5243. // class Resources (issue #157)
  5244. // outputs secondary files, such as css and javascript
  5245. // data is stored gzipped (gzencode) and encoded (base64_encode)
  5246. //
  5247. class Resources
  5248. {
  5249. // set this to the file containing getInternalResource;
  5250. // currently unused in split mode; set to __FILE__ for built PLA.
  5251. public static $embedding_file = __FILE__;
  5252. private static $_resources = array(
  5253. 'css' => array(
  5254. 'mime' => 'text/css',
  5255. 'data' => 'resources/phpliteadmin.css',
  5256. ),
  5257. 'javascript' => array(
  5258. 'mime' => 'text/javascript',
  5259. 'data' => 'resources/phpliteadmin.js',
  5260. ),
  5261. 'favicon' => array(
  5262. 'mime' => 'image/x-icon',
  5263. 'data' => 'resources/favicon.ico',
  5264. 'base64' => 'true',
  5265. ),
  5266. );
  5267. // outputs the specified resource, if defined in this class.
  5268. // the main script should do no further output after calling this function.
  5269. public static function output($resource)
  5270. {
  5271. if (isset(self::$_resources[$resource])) {
  5272. $res = &self::$_resources[$resource];
  5273. if (function_exists('getInternalResource') && $data = getInternalResource($res['data'])) {
  5274. $filename = self::$embedding_file;
  5275. } else {
  5276. $filename = $res['data'];
  5277. }
  5278. // use last-modified time as etag; etag must be quoted
  5279. $etag = '"' . filemtime($filename) . '"';
  5280. // check headers for matching etag; if etag hasn't changed, use the cached version
  5281. if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
  5282. header('HTTP/1.0 304 Not Modified');
  5283. return;
  5284. }
  5285. header('Etag: ' . $etag);
  5286. // cache file for at most 30 days
  5287. header('Cache-control: max-age=2592000');
  5288. // output resource
  5289. header('Content-type: ' . $res['mime']);
  5290. if (isset($data)) {
  5291. if (isset($res['base64'])) {
  5292. echo base64_decode($data);
  5293. } else {
  5294. echo $data;
  5295. }
  5296. } else {
  5297. readfile($filename);
  5298. }
  5299. }
  5300. }
  5301. }
  5302. // returns data from internal resources, available in single-file mode
  5303. function getInternalResource($res)
  5304. {
  5305. $resources = array('resources/phpliteadmin.css' => array(0 => 0, 1 => 4047), 'resources/phpliteadmin.js' => array(0 => 4047, 1 => 4169), 'resources/favicon.ico' => array(0 => 8216, 1 => 1448));
  5306. if (isset($resources[$res]) && $f = fopen(__FILE__, 'r')) {
  5307. fseek($f, __COMPILER_HALT_OFFSET__ + $resources[$res][0]);
  5308. $data = fread($f, $resources[$res][1]);
  5309. fclose($f);
  5310. return $data;
  5311. }
  5312. return false;
  5313. }
  5314. // resources embedded below, do not edit!
  5315. __halt_compiler() ?>body{margin:0px;padding:0px;font-family:Arial,Helvetica,sans-serif;font-size:14px;color:#000;background-color:#e0ebf6;overflow:auto}.body_tbl td{padding:9px 2px 9px 9px}.left_td{width:100px}a{color:#03F;text-decoration:none;cursor:pointer}a:hover{color:#06F}hr{height:1px;border:0;color:#bbb;background-color:#bbb;width:100%}h1{margin:0px;padding:5px;font-size:24px;background-color:#f3cece;text-align:center;color:#000;border-top-left-radius:5px;border-top-right-radius:5px;-moz-border-radius-topleft:5px;-moz-border-radius-topright:5px}#headerlinks{text-align:center;margin-bottom:10px;padding:5px 15px;border-color:#03F;border-width:1px;border-style:solid;border-left-style:none;border-right-style:none;font-size:12px;background-color:#e0ebf6;font-weight:bold}h1 #version{color:#000;font-size:16px}h1 #logo{color:#000}h2{margin:0px;padding:0px;font-size:14px;margin-bottom:20px}input,select,textarea{font-family:Arial,Helvetica,sans-serif;background-color:#eaeaea;color:#03F;border-color:#03F;border-style:solid;border-width:1px;margin:5px;border-radius:5px;-moz-border-radius:5px;padding:3px}input.btn{cursor:pointer}input.btn:hover{background-color:#ccc}fieldset label{min-width:200px;display:block;float:left}fieldset{padding:15px;border-color:#03F;border-width:1px;border-style:solid;border-radius:5px;-moz-border-radius:5px;background-color:#f9f9f9}#container{padding:10px}#leftNav{min-width:250px;padding:0px;border-color:#03F;border-width:1px;border-style:solid;background-color:#FFF;padding-bottom:15px;border-radius:5px;-moz-border-radius:5px}.databaseList select{max-width:200px}.viewTable tr td{padding:1px}#loginBox{width:500px;margin-left:auto;margin-right:auto;margin-top:50px;border-color:#03F;border-width:1px;border-style:solid;background-color:#FFF;border-radius:5px;-moz-border-radius:5px}#main{border-color:#03F;border-width:1px;border-style:solid;padding:15px;background-color:#FFF;border-bottom-left-radius:5px;border-bottom-right-radius:5px;border-top-right-radius:5px;-moz-border-radius-bottomleft:5px;-moz-border-radius-bottomright:5px;-moz-border-radius-topright:5px}.td1{background-color:#f9e3e3;text-align:right;font-size:12px;padding-left:10px;padding-right:10px}.td2{background-color:#f3cece;text-align:right;font-size:12px;padding-left:10px;padding-right:10px}.tdheader{border-color:#03F;border-width:1px;border-style:solid;font-weight:bold;font-size:12px;padding-left:10px;padding-right:10px;background-color:#e0ebf6;border-radius:5px;-moz-border-radius:5px}.confirm{border-color:#03F;border-width:1px;border-style:dashed;padding:15px;background-color:#e0ebf6}.tab{display:block;padding:5px;padding-right:8px;padding-left:8px;border-color:#03F;border-width:1px;border-style:solid;margin-right:5px;float:left;border-bottom-style:none;position:relative;top:1px;padding-bottom:4px;background-color:#eaeaea;border-top-left-radius:5px;border-top-right-radius:5px;-moz-border-radius-topleft:5px;-moz-border-radius-topright:5px}.tab_pressed{display:block;padding:5px;padding-right:8px;padding-left:8px;border-color:#03F;border-width:1px;border-style:solid;margin-right:5px;float:left;border-bottom-style:none;position:relative;top:1px;background-color:#FFF;cursor:default;border-top-left-radius:5px;border-top-right-radius:5px;-moz-border-radius-topleft:5px;-moz-border-radius-topright:5px}.helpq{font-size:11px;font-weight:normal}#help_container{padding:0px;font-size:12px;margin-left:auto;margin-right:auto;background-color:#fff}.help_outer{background-color:#FFF;padding:0px;height:300px;position:relative}.help_list{padding:10px;height:auto}.headd{font-size:14px;font-weight:bold;display:block;padding:10px;background-color:#e0ebf6;border-color:#03F;border-width:1px;border-style:solid;border-left-style:none;border-right-style:none}.help_inner{padding:10px}.help_top{display:block;position:absolute;right:10px;bottom:10px}.warning,.delete,.empty,.drop,.delete_db{color:red}.sidebar_table{font-size:11px}.active_table,.active_db{text-decoration:underline}.null{color:#888}.found{background:#FF0;text-decoration:none}
  5316. function initAutoincrement()
  5317. {var i=0;while(document.getElementById('i'+i+'_autoincrement')!=undefined)
  5318. {document.getElementById('i'+i+'_autoincrement').disabled=true;i++;}}
  5319. function toggleAutoincrement(i)
  5320. {var type=document.getElementById('i'+i+'_type');var primarykey=document.getElementById('i'+i+'_primarykey');var autoincrement=document.getElementById('i'+i+'_autoincrement');if(!autoincrement)return false;if(type.value=='INTEGER'&&primarykey.checked)
  5321. autoincrement.disabled=false;else
  5322. {autoincrement.disabled=true;autoincrement.checked=false;}}
  5323. function toggleNull(i)
  5324. {var pk=document.getElementById('i'+i+'_primarykey');var notnull=document.getElementById('i'+i+'_notnull');if(pk.checked)
  5325. {notnull.disabled=true;notnull.checked=true;}
  5326. else
  5327. {notnull.disabled=false;}}
  5328. function checkAll(field)
  5329. {var i=0;while(document.getElementById('check_'+i)!=undefined)
  5330. {document.getElementById('check_'+i).checked=true;i++;}}
  5331. function uncheckAll(field)
  5332. {var i=0;while(document.getElementById('check_'+i)!=undefined)
  5333. {document.getElementById('check_'+i).checked=false;i++;}}
  5334. function changeIgnore(area,e,u)
  5335. {if(area.value!="")
  5336. {if(document.getElementById(e)!=undefined)
  5337. document.getElementById(e).checked=false;if(document.getElementById(u)!=undefined)
  5338. document.getElementById(u).checked=false;}}
  5339. function moveFields()
  5340. {var fields=document.getElementById("fieldcontainer");var selected=[];for(var i=0;i<fields.options.length;i++)
  5341. if(fields.options[i].selected)
  5342. selected.push(fields.options[i].value);for(var i=0;i<selected.length;i++)
  5343. insertAtCaret("queryval",'"'+selected[i].replace(/"/g,'""')+'"');}
  5344. function insertAtCaret(areaId,text)
  5345. {var txtarea=document.getElementById(areaId);var scrollPos=txtarea.scrollTop;var strPos=0;var br=((txtarea.selectionStart||txtarea.selectionStart=='0')?"ff":(document.selection?"ie":false));if(br=="ie")
  5346. {txtarea.focus();var range=document.selection.createRange();range.moveStart('character',-txtarea.value.length);strPos=range.text.length;}
  5347. else if(br=="ff")
  5348. strPos=txtarea.selectionStart;var front=(txtarea.value).substring(0,strPos);var back=(txtarea.value).substring(strPos,txtarea.value.length);txtarea.value=front+text+back;strPos=strPos+text.length;if(br=="ie")
  5349. {txtarea.focus();var range=document.selection.createRange();range.moveStart('character',-txtarea.value.length);range.moveStart('character',strPos);range.moveEnd('character',0);range.select();}
  5350. else if(br=="ff")
  5351. {txtarea.selectionStart=strPos;txtarea.selectionEnd=strPos;txtarea.focus();}
  5352. txtarea.scrollTop=scrollPos;}
  5353. function notNull(checker)
  5354. {document.getElementById(checker).checked=false;}
  5355. function disableText(checker,textie)
  5356. {if(checker.checked)
  5357. {document.getElementById(textie).value="";document.getElementById(textie).disabled=true;}
  5358. else
  5359. {document.getElementById(textie).disabled=false;}}
  5360. function toggleExports(val)
  5361. {document.getElementById("exportoptions_sql").style.display="none";document.getElementById("exportoptions_csv").style.display="none";document.getElementById("exportoptions_"+val).style.display="block";}
  5362. function toggleImports(val)
  5363. {document.getElementById("importoptions_sql").style.display="none";document.getElementById("importoptions_csv").style.display="none";document.getElementById("importoptions_"+val).style.display="block";}
  5364. function openHelp(section)
  5365. {PopupCenter('?help=1#'+section,"Help Section");}
  5366. var helpsec=false;function PopupCenter(pageURL,title)
  5367. {helpsec=window.open(pageURL,title,"toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=300");}
  5368. function checkLike(srchField,selOpt)
  5369. {if(selOpt=="LIKE%"){var textArea=document.getElementById(srchField);textArea.value="%"+textArea.value+"%";}}
  5370. function createCORSRequest(method,url)
  5371. {var xhr=new XMLHttpRequest();if("withCredentials"in xhr)
  5372. {xhr.open(method,url,true);}
  5373. else if(typeof XDomainRequest!="undefined")
  5374. {xhr=new XDomainRequest();xhr.open(method,url);}
  5375. else
  5376. {xhr=null;}
  5377. return xhr;}
  5378. function checkVersion(installed,url)
  5379. {var xhr=createCORSRequest('GET',url);if(!xhr)
  5380. return false;xhr.onload=function()
  5381. {if(xhr.responseText.split("\n").indexOf(installed)==-1)
  5382. {document.getElementById('oldVersion').style.display='inline';}};xhr.send();}AAABAAEAEBAAAAEAIAAoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADwoKZQAAABMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABEMDJMAAAAdAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAASDg7BAAAASAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAETDw9CCQkJ1QUFBb4AAABjAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADgsLVgAAAO8YExP/AAAA7QAAALEAAAARAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABQNDUsAAADJGBMT/xgTE/8AAAD/AAAAuAAAAA8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZERE8DQoKwhgTE/8QDQ2sGBMT/xgTE/8AAAA/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAwICHkAAAD/EQwMzQAAAMIAAAD/AAAA7gAAAGEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOCwtWAAAA8RgTE/8IBQW1AAAA/wAAAP8AAADlAAAAHQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA0KCsEAAAD/EQ8PzAAAAMkAAAD/AAAA/wAAAHoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABDAAAA/xgTE/8TDw/FAAAA8gAAAP8AAADqAAAAJgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAALUAAAD/GBMT/wAAALkAAAD/AAAA/wAAAIkAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAfAAAA4wAAAP8GBgbFAAAA2QAAAP8AAADGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAF4AAAD5AAAA/RgTE/8AAAD/AAAA0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQQAAAOIAAAD/AAAA/wAAAHYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAjAAAArgAAAG4AAAAAAAAAAAAAAAAAAAAA