api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api.include.php 429KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499
  1. <?php
  2. /**
  3. * PHP-CRUD-API v2 License: MIT
  4. * Maurits van der Schee: maurits@vdschee.nl
  5. * https://github.com/mevdschee/php-crud-api
  6. *
  7. * Dependencies:
  8. * - vendor/psr/*: PHP-FIG
  9. * https://github.com/php-fig
  10. * - vendor/nyholm/*: Tobias Nyholm
  11. * https://github.com/Nyholm
  12. **/
  13. // file: vendor/psr/http-factory/src/RequestFactoryInterface.php
  14. namespace Psr\Http\Message {
  15. interface RequestFactoryInterface
  16. {
  17. /**
  18. * Create a new request.
  19. *
  20. * @param string $method The HTTP method associated with the request.
  21. * @param UriInterface|string $uri The URI associated with the request. If
  22. * the value is a string, the factory MUST create a UriInterface
  23. * instance based on it.
  24. *
  25. * @return RequestInterface
  26. */
  27. public function createRequest(string $method, $uri): RequestInterface;
  28. }
  29. }
  30. // file: vendor/psr/http-factory/src/ResponseFactoryInterface.php
  31. namespace Psr\Http\Message {
  32. interface ResponseFactoryInterface
  33. {
  34. /**
  35. * Create a new response.
  36. *
  37. * @param int $code HTTP status code; defaults to 200
  38. * @param string $reasonPhrase Reason phrase to associate with status code
  39. * in generated response; if none is provided implementations MAY use
  40. * the defaults as suggested in the HTTP specification.
  41. *
  42. * @return ResponseInterface
  43. */
  44. public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface;
  45. }
  46. }
  47. // file: vendor/psr/http-factory/src/ServerRequestFactoryInterface.php
  48. namespace Psr\Http\Message {
  49. interface ServerRequestFactoryInterface
  50. {
  51. /**
  52. * Create a new server request.
  53. *
  54. * Note that server-params are taken precisely as given - no parsing/processing
  55. * of the given values is performed, and, in particular, no attempt is made to
  56. * determine the HTTP method or URI, which must be provided explicitly.
  57. *
  58. * @param string $method The HTTP method associated with the request.
  59. * @param UriInterface|string $uri The URI associated with the request. If
  60. * the value is a string, the factory MUST create a UriInterface
  61. * instance based on it.
  62. * @param array $serverParams Array of SAPI parameters with which to seed
  63. * the generated request instance.
  64. *
  65. * @return ServerRequestInterface
  66. */
  67. public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface;
  68. }
  69. }
  70. // file: vendor/psr/http-factory/src/StreamFactoryInterface.php
  71. namespace Psr\Http\Message {
  72. interface StreamFactoryInterface
  73. {
  74. /**
  75. * Create a new stream from a string.
  76. *
  77. * The stream SHOULD be created with a temporary resource.
  78. *
  79. * @param string $content String content with which to populate the stream.
  80. *
  81. * @return StreamInterface
  82. */
  83. public function createStream(string $content = ''): StreamInterface;
  84. /**
  85. * Create a stream from an existing file.
  86. *
  87. * The file MUST be opened using the given mode, which may be any mode
  88. * supported by the `fopen` function.
  89. *
  90. * The `$filename` MAY be any string supported by `fopen()`.
  91. *
  92. * @param string $filename Filename or stream URI to use as basis of stream.
  93. * @param string $mode Mode with which to open the underlying filename/stream.
  94. *
  95. * @return StreamInterface
  96. * @throws \RuntimeException If the file cannot be opened.
  97. * @throws \InvalidArgumentException If the mode is invalid.
  98. */
  99. public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface;
  100. /**
  101. * Create a new stream from an existing resource.
  102. *
  103. * The stream MUST be readable and may be writable.
  104. *
  105. * @param resource $resource PHP resource to use as basis of stream.
  106. *
  107. * @return StreamInterface
  108. */
  109. public function createStreamFromResource($resource): StreamInterface;
  110. }
  111. }
  112. // file: vendor/psr/http-factory/src/UploadedFileFactoryInterface.php
  113. namespace Psr\Http\Message {
  114. interface UploadedFileFactoryInterface
  115. {
  116. /**
  117. * Create a new uploaded file.
  118. *
  119. * If a size is not provided it will be determined by checking the size of
  120. * the file.
  121. *
  122. * @see http://php.net/manual/features.file-upload.post-method.php
  123. * @see http://php.net/manual/features.file-upload.errors.php
  124. *
  125. * @param StreamInterface $stream Underlying stream representing the
  126. * uploaded file content.
  127. * @param int $size in bytes
  128. * @param int $error PHP file upload error
  129. * @param string $clientFilename Filename as provided by the client, if any.
  130. * @param string $clientMediaType Media type as provided by the client, if any.
  131. *
  132. * @return UploadedFileInterface
  133. *
  134. * @throws \InvalidArgumentException If the file resource is not readable.
  135. */
  136. public function createUploadedFile(
  137. StreamInterface $stream,
  138. int $size = null,
  139. int $error = \UPLOAD_ERR_OK,
  140. string $clientFilename = null,
  141. string $clientMediaType = null
  142. ): UploadedFileInterface;
  143. }
  144. }
  145. // file: vendor/psr/http-factory/src/UriFactoryInterface.php
  146. namespace Psr\Http\Message {
  147. interface UriFactoryInterface
  148. {
  149. /**
  150. * Create a new URI.
  151. *
  152. * @param string $uri
  153. *
  154. * @return UriInterface
  155. *
  156. * @throws \InvalidArgumentException If the given URI cannot be parsed.
  157. */
  158. public function createUri(string $uri = ''): UriInterface;
  159. }
  160. }
  161. // file: vendor/psr/http-message/src/MessageInterface.php
  162. namespace Psr\Http\Message {
  163. /**
  164. * HTTP messages consist of requests from a client to a server and responses
  165. * from a server to a client. This interface defines the methods common to
  166. * each.
  167. *
  168. * Messages are considered immutable; all methods that might change state MUST
  169. * be implemented such that they retain the internal state of the current
  170. * message and return an instance that contains the changed state.
  171. *
  172. * @link http://www.ietf.org/rfc/rfc7230.txt
  173. * @link http://www.ietf.org/rfc/rfc7231.txt
  174. */
  175. interface MessageInterface
  176. {
  177. /**
  178. * Retrieves the HTTP protocol version as a string.
  179. *
  180. * The string MUST contain only the HTTP version number (e.g., "1.1", "1.0").
  181. *
  182. * @return string HTTP protocol version.
  183. */
  184. public function getProtocolVersion();
  185. /**
  186. * Return an instance with the specified HTTP protocol version.
  187. *
  188. * The version string MUST contain only the HTTP version number (e.g.,
  189. * "1.1", "1.0").
  190. *
  191. * This method MUST be implemented in such a way as to retain the
  192. * immutability of the message, and MUST return an instance that has the
  193. * new protocol version.
  194. *
  195. * @param string $version HTTP protocol version
  196. * @return static
  197. */
  198. public function withProtocolVersion($version);
  199. /**
  200. * Retrieves all message header values.
  201. *
  202. * The keys represent the header name as it will be sent over the wire, and
  203. * each value is an array of strings associated with the header.
  204. *
  205. * // Represent the headers as a string
  206. * foreach ($message->getHeaders() as $name => $values) {
  207. * echo $name . ": " . implode(", ", $values);
  208. * }
  209. *
  210. * // Emit headers iteratively:
  211. * foreach ($message->getHeaders() as $name => $values) {
  212. * foreach ($values as $value) {
  213. * header(sprintf('%s: %s', $name, $value), false);
  214. * }
  215. * }
  216. *
  217. * While header names are not case-sensitive, getHeaders() will preserve the
  218. * exact case in which headers were originally specified.
  219. *
  220. * @return string[][] Returns an associative array of the message's headers. Each
  221. * key MUST be a header name, and each value MUST be an array of strings
  222. * for that header.
  223. */
  224. public function getHeaders();
  225. /**
  226. * Checks if a header exists by the given case-insensitive name.
  227. *
  228. * @param string $name Case-insensitive header field name.
  229. * @return bool Returns true if any header names match the given header
  230. * name using a case-insensitive string comparison. Returns false if
  231. * no matching header name is found in the message.
  232. */
  233. public function hasHeader($name);
  234. /**
  235. * Retrieves a message header value by the given case-insensitive name.
  236. *
  237. * This method returns an array of all the header values of the given
  238. * case-insensitive header name.
  239. *
  240. * If the header does not appear in the message, this method MUST return an
  241. * empty array.
  242. *
  243. * @param string $name Case-insensitive header field name.
  244. * @return string[] An array of string values as provided for the given
  245. * header. If the header does not appear in the message, this method MUST
  246. * return an empty array.
  247. */
  248. public function getHeader($name);
  249. /**
  250. * Retrieves a comma-separated string of the values for a single header.
  251. *
  252. * This method returns all of the header values of the given
  253. * case-insensitive header name as a string concatenated together using
  254. * a comma.
  255. *
  256. * NOTE: Not all header values may be appropriately represented using
  257. * comma concatenation. For such headers, use getHeader() instead
  258. * and supply your own delimiter when concatenating.
  259. *
  260. * If the header does not appear in the message, this method MUST return
  261. * an empty string.
  262. *
  263. * @param string $name Case-insensitive header field name.
  264. * @return string A string of values as provided for the given header
  265. * concatenated together using a comma. If the header does not appear in
  266. * the message, this method MUST return an empty string.
  267. */
  268. public function getHeaderLine($name);
  269. /**
  270. * Return an instance with the provided value replacing the specified header.
  271. *
  272. * While header names are case-insensitive, the casing of the header will
  273. * be preserved by this function, and returned from getHeaders().
  274. *
  275. * This method MUST be implemented in such a way as to retain the
  276. * immutability of the message, and MUST return an instance that has the
  277. * new and/or updated header and value.
  278. *
  279. * @param string $name Case-insensitive header field name.
  280. * @param string|string[] $value Header value(s).
  281. * @return static
  282. * @throws \InvalidArgumentException for invalid header names or values.
  283. */
  284. public function withHeader($name, $value);
  285. /**
  286. * Return an instance with the specified header appended with the given value.
  287. *
  288. * Existing values for the specified header will be maintained. The new
  289. * value(s) will be appended to the existing list. If the header did not
  290. * exist previously, it will be added.
  291. *
  292. * This method MUST be implemented in such a way as to retain the
  293. * immutability of the message, and MUST return an instance that has the
  294. * new header and/or value.
  295. *
  296. * @param string $name Case-insensitive header field name to add.
  297. * @param string|string[] $value Header value(s).
  298. * @return static
  299. * @throws \InvalidArgumentException for invalid header names or values.
  300. */
  301. public function withAddedHeader($name, $value);
  302. /**
  303. * Return an instance without the specified header.
  304. *
  305. * Header resolution MUST be done without case-sensitivity.
  306. *
  307. * This method MUST be implemented in such a way as to retain the
  308. * immutability of the message, and MUST return an instance that removes
  309. * the named header.
  310. *
  311. * @param string $name Case-insensitive header field name to remove.
  312. * @return static
  313. */
  314. public function withoutHeader($name);
  315. /**
  316. * Gets the body of the message.
  317. *
  318. * @return StreamInterface Returns the body as a stream.
  319. */
  320. public function getBody();
  321. /**
  322. * Return an instance with the specified message body.
  323. *
  324. * The body MUST be a StreamInterface object.
  325. *
  326. * This method MUST be implemented in such a way as to retain the
  327. * immutability of the message, and MUST return a new instance that has the
  328. * new body stream.
  329. *
  330. * @param StreamInterface $body Body.
  331. * @return static
  332. * @throws \InvalidArgumentException When the body is not valid.
  333. */
  334. public function withBody(StreamInterface $body);
  335. }
  336. }
  337. // file: vendor/psr/http-message/src/RequestInterface.php
  338. namespace Psr\Http\Message {
  339. /**
  340. * Representation of an outgoing, client-side request.
  341. *
  342. * Per the HTTP specification, this interface includes properties for
  343. * each of the following:
  344. *
  345. * - Protocol version
  346. * - HTTP method
  347. * - URI
  348. * - Headers
  349. * - Message body
  350. *
  351. * During construction, implementations MUST attempt to set the Host header from
  352. * a provided URI if no Host header is provided.
  353. *
  354. * Requests are considered immutable; all methods that might change state MUST
  355. * be implemented such that they retain the internal state of the current
  356. * message and return an instance that contains the changed state.
  357. */
  358. interface RequestInterface extends MessageInterface
  359. {
  360. /**
  361. * Retrieves the message's request target.
  362. *
  363. * Retrieves the message's request-target either as it will appear (for
  364. * clients), as it appeared at request (for servers), or as it was
  365. * specified for the instance (see withRequestTarget()).
  366. *
  367. * In most cases, this will be the origin-form of the composed URI,
  368. * unless a value was provided to the concrete implementation (see
  369. * withRequestTarget() below).
  370. *
  371. * If no URI is available, and no request-target has been specifically
  372. * provided, this method MUST return the string "/".
  373. *
  374. * @return string
  375. */
  376. public function getRequestTarget();
  377. /**
  378. * Return an instance with the specific request-target.
  379. *
  380. * If the request needs a non-origin-form request-target — e.g., for
  381. * specifying an absolute-form, authority-form, or asterisk-form —
  382. * this method may be used to create an instance with the specified
  383. * request-target, verbatim.
  384. *
  385. * This method MUST be implemented in such a way as to retain the
  386. * immutability of the message, and MUST return an instance that has the
  387. * changed request target.
  388. *
  389. * @link http://tools.ietf.org/html/rfc7230#section-5.3 (for the various
  390. * request-target forms allowed in request messages)
  391. * @param mixed $requestTarget
  392. * @return static
  393. */
  394. public function withRequestTarget($requestTarget);
  395. /**
  396. * Retrieves the HTTP method of the request.
  397. *
  398. * @return string Returns the request method.
  399. */
  400. public function getMethod();
  401. /**
  402. * Return an instance with the provided HTTP method.
  403. *
  404. * While HTTP method names are typically all uppercase characters, HTTP
  405. * method names are case-sensitive and thus implementations SHOULD NOT
  406. * modify the given string.
  407. *
  408. * This method MUST be implemented in such a way as to retain the
  409. * immutability of the message, and MUST return an instance that has the
  410. * changed request method.
  411. *
  412. * @param string $method Case-sensitive method.
  413. * @return static
  414. * @throws \InvalidArgumentException for invalid HTTP methods.
  415. */
  416. public function withMethod($method);
  417. /**
  418. * Retrieves the URI instance.
  419. *
  420. * This method MUST return a UriInterface instance.
  421. *
  422. * @link http://tools.ietf.org/html/rfc3986#section-4.3
  423. * @return UriInterface Returns a UriInterface instance
  424. * representing the URI of the request.
  425. */
  426. public function getUri();
  427. /**
  428. * Returns an instance with the provided URI.
  429. *
  430. * This method MUST update the Host header of the returned request by
  431. * default if the URI contains a host component. If the URI does not
  432. * contain a host component, any pre-existing Host header MUST be carried
  433. * over to the returned request.
  434. *
  435. * You can opt-in to preserving the original state of the Host header by
  436. * setting `$preserveHost` to `true`. When `$preserveHost` is set to
  437. * `true`, this method interacts with the Host header in the following ways:
  438. *
  439. * - If the Host header is missing or empty, and the new URI contains
  440. * a host component, this method MUST update the Host header in the returned
  441. * request.
  442. * - If the Host header is missing or empty, and the new URI does not contain a
  443. * host component, this method MUST NOT update the Host header in the returned
  444. * request.
  445. * - If a Host header is present and non-empty, this method MUST NOT update
  446. * the Host header in the returned request.
  447. *
  448. * This method MUST be implemented in such a way as to retain the
  449. * immutability of the message, and MUST return an instance that has the
  450. * new UriInterface instance.
  451. *
  452. * @link http://tools.ietf.org/html/rfc3986#section-4.3
  453. * @param UriInterface $uri New request URI to use.
  454. * @param bool $preserveHost Preserve the original state of the Host header.
  455. * @return static
  456. */
  457. public function withUri(UriInterface $uri, $preserveHost = false);
  458. }
  459. }
  460. // file: vendor/psr/http-message/src/ResponseInterface.php
  461. namespace Psr\Http\Message {
  462. /**
  463. * Representation of an outgoing, server-side response.
  464. *
  465. * Per the HTTP specification, this interface includes properties for
  466. * each of the following:
  467. *
  468. * - Protocol version
  469. * - Status code and reason phrase
  470. * - Headers
  471. * - Message body
  472. *
  473. * Responses are considered immutable; all methods that might change state MUST
  474. * be implemented such that they retain the internal state of the current
  475. * message and return an instance that contains the changed state.
  476. */
  477. interface ResponseInterface extends MessageInterface
  478. {
  479. /**
  480. * Gets the response status code.
  481. *
  482. * The status code is a 3-digit integer result code of the server's attempt
  483. * to understand and satisfy the request.
  484. *
  485. * @return int Status code.
  486. */
  487. public function getStatusCode();
  488. /**
  489. * Return an instance with the specified status code and, optionally, reason phrase.
  490. *
  491. * If no reason phrase is specified, implementations MAY choose to default
  492. * to the RFC 7231 or IANA recommended reason phrase for the response's
  493. * status code.
  494. *
  495. * This method MUST be implemented in such a way as to retain the
  496. * immutability of the message, and MUST return an instance that has the
  497. * updated status and reason phrase.
  498. *
  499. * @link http://tools.ietf.org/html/rfc7231#section-6
  500. * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
  501. * @param int $code The 3-digit integer result code to set.
  502. * @param string $reasonPhrase The reason phrase to use with the
  503. * provided status code; if none is provided, implementations MAY
  504. * use the defaults as suggested in the HTTP specification.
  505. * @return static
  506. * @throws \InvalidArgumentException For invalid status code arguments.
  507. */
  508. public function withStatus($code, $reasonPhrase = '');
  509. /**
  510. * Gets the response reason phrase associated with the status code.
  511. *
  512. * Because a reason phrase is not a required element in a response
  513. * status line, the reason phrase value MAY be null. Implementations MAY
  514. * choose to return the default RFC 7231 recommended reason phrase (or those
  515. * listed in the IANA HTTP Status Code Registry) for the response's
  516. * status code.
  517. *
  518. * @link http://tools.ietf.org/html/rfc7231#section-6
  519. * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
  520. * @return string Reason phrase; must return an empty string if none present.
  521. */
  522. public function getReasonPhrase();
  523. }
  524. }
  525. // file: vendor/psr/http-message/src/ServerRequestInterface.php
  526. namespace Psr\Http\Message {
  527. /**
  528. * Representation of an incoming, server-side HTTP request.
  529. *
  530. * Per the HTTP specification, this interface includes properties for
  531. * each of the following:
  532. *
  533. * - Protocol version
  534. * - HTTP method
  535. * - URI
  536. * - Headers
  537. * - Message body
  538. *
  539. * Additionally, it encapsulates all data as it has arrived to the
  540. * application from the CGI and/or PHP environment, including:
  541. *
  542. * - The values represented in $_SERVER.
  543. * - Any cookies provided (generally via $_COOKIE)
  544. * - Query string arguments (generally via $_GET, or as parsed via parse_str())
  545. * - Upload files, if any (as represented by $_FILES)
  546. * - Deserialized body parameters (generally from $_POST)
  547. *
  548. * $_SERVER values MUST be treated as immutable, as they represent application
  549. * state at the time of request; as such, no methods are provided to allow
  550. * modification of those values. The other values provide such methods, as they
  551. * can be restored from $_SERVER or the request body, and may need treatment
  552. * during the application (e.g., body parameters may be deserialized based on
  553. * content type).
  554. *
  555. * Additionally, this interface recognizes the utility of introspecting a
  556. * request to derive and match additional parameters (e.g., via URI path
  557. * matching, decrypting cookie values, deserializing non-form-encoded body
  558. * content, matching authorization headers to users, etc). These parameters
  559. * are stored in an "attributes" property.
  560. *
  561. * Requests are considered immutable; all methods that might change state MUST
  562. * be implemented such that they retain the internal state of the current
  563. * message and return an instance that contains the changed state.
  564. */
  565. interface ServerRequestInterface extends RequestInterface
  566. {
  567. /**
  568. * Retrieve server parameters.
  569. *
  570. * Retrieves data related to the incoming request environment,
  571. * typically derived from PHP's $_SERVER superglobal. The data IS NOT
  572. * REQUIRED to originate from $_SERVER.
  573. *
  574. * @return array
  575. */
  576. public function getServerParams();
  577. /**
  578. * Retrieve cookies.
  579. *
  580. * Retrieves cookies sent by the client to the server.
  581. *
  582. * The data MUST be compatible with the structure of the $_COOKIE
  583. * superglobal.
  584. *
  585. * @return array
  586. */
  587. public function getCookieParams();
  588. /**
  589. * Return an instance with the specified cookies.
  590. *
  591. * The data IS NOT REQUIRED to come from the $_COOKIE superglobal, but MUST
  592. * be compatible with the structure of $_COOKIE. Typically, this data will
  593. * be injected at instantiation.
  594. *
  595. * This method MUST NOT update the related Cookie header of the request
  596. * instance, nor related values in the server params.
  597. *
  598. * This method MUST be implemented in such a way as to retain the
  599. * immutability of the message, and MUST return an instance that has the
  600. * updated cookie values.
  601. *
  602. * @param array $cookies Array of key/value pairs representing cookies.
  603. * @return static
  604. */
  605. public function withCookieParams(array $cookies);
  606. /**
  607. * Retrieve query string arguments.
  608. *
  609. * Retrieves the deserialized query string arguments, if any.
  610. *
  611. * Note: the query params might not be in sync with the URI or server
  612. * params. If you need to ensure you are only getting the original
  613. * values, you may need to parse the query string from `getUri()->getQuery()`
  614. * or from the `QUERY_STRING` server param.
  615. *
  616. * @return array
  617. */
  618. public function getQueryParams();
  619. /**
  620. * Return an instance with the specified query string arguments.
  621. *
  622. * These values SHOULD remain immutable over the course of the incoming
  623. * request. They MAY be injected during instantiation, such as from PHP's
  624. * $_GET superglobal, or MAY be derived from some other value such as the
  625. * URI. In cases where the arguments are parsed from the URI, the data
  626. * MUST be compatible with what PHP's parse_str() would return for
  627. * purposes of how duplicate query parameters are handled, and how nested
  628. * sets are handled.
  629. *
  630. * Setting query string arguments MUST NOT change the URI stored by the
  631. * request, nor the values in the server params.
  632. *
  633. * This method MUST be implemented in such a way as to retain the
  634. * immutability of the message, and MUST return an instance that has the
  635. * updated query string arguments.
  636. *
  637. * @param array $query Array of query string arguments, typically from
  638. * $_GET.
  639. * @return static
  640. */
  641. public function withQueryParams(array $query);
  642. /**
  643. * Retrieve normalized file upload data.
  644. *
  645. * This method returns upload metadata in a normalized tree, with each leaf
  646. * an instance of Psr\Http\Message\UploadedFileInterface.
  647. *
  648. * These values MAY be prepared from $_FILES or the message body during
  649. * instantiation, or MAY be injected via withUploadedFiles().
  650. *
  651. * @return array An array tree of UploadedFileInterface instances; an empty
  652. * array MUST be returned if no data is present.
  653. */
  654. public function getUploadedFiles();
  655. /**
  656. * Create a new instance with the specified uploaded files.
  657. *
  658. * This method MUST be implemented in such a way as to retain the
  659. * immutability of the message, and MUST return an instance that has the
  660. * updated body parameters.
  661. *
  662. * @param array $uploadedFiles An array tree of UploadedFileInterface instances.
  663. * @return static
  664. * @throws \InvalidArgumentException if an invalid structure is provided.
  665. */
  666. public function withUploadedFiles(array $uploadedFiles);
  667. /**
  668. * Retrieve any parameters provided in the request body.
  669. *
  670. * If the request Content-Type is either application/x-www-form-urlencoded
  671. * or multipart/form-data, and the request method is POST, this method MUST
  672. * return the contents of $_POST.
  673. *
  674. * Otherwise, this method may return any results of deserializing
  675. * the request body content; as parsing returns structured content, the
  676. * potential types MUST be arrays or objects only. A null value indicates
  677. * the absence of body content.
  678. *
  679. * @return null|array|object The deserialized body parameters, if any.
  680. * These will typically be an array or object.
  681. */
  682. public function getParsedBody();
  683. /**
  684. * Return an instance with the specified body parameters.
  685. *
  686. * These MAY be injected during instantiation.
  687. *
  688. * If the request Content-Type is either application/x-www-form-urlencoded
  689. * or multipart/form-data, and the request method is POST, use this method
  690. * ONLY to inject the contents of $_POST.
  691. *
  692. * The data IS NOT REQUIRED to come from $_POST, but MUST be the results of
  693. * deserializing the request body content. Deserialization/parsing returns
  694. * structured data, and, as such, this method ONLY accepts arrays or objects,
  695. * or a null value if nothing was available to parse.
  696. *
  697. * As an example, if content negotiation determines that the request data
  698. * is a JSON payload, this method could be used to create a request
  699. * instance with the deserialized parameters.
  700. *
  701. * This method MUST be implemented in such a way as to retain the
  702. * immutability of the message, and MUST return an instance that has the
  703. * updated body parameters.
  704. *
  705. * @param null|array|object $data The deserialized body data. This will
  706. * typically be in an array or object.
  707. * @return static
  708. * @throws \InvalidArgumentException if an unsupported argument type is
  709. * provided.
  710. */
  711. public function withParsedBody($data);
  712. /**
  713. * Retrieve attributes derived from the request.
  714. *
  715. * The request "attributes" may be used to allow injection of any
  716. * parameters derived from the request: e.g., the results of path
  717. * match operations; the results of decrypting cookies; the results of
  718. * deserializing non-form-encoded message bodies; etc. Attributes
  719. * will be application and request specific, and CAN be mutable.
  720. *
  721. * @return array Attributes derived from the request.
  722. */
  723. public function getAttributes();
  724. /**
  725. * Retrieve a single derived request attribute.
  726. *
  727. * Retrieves a single derived request attribute as described in
  728. * getAttributes(). If the attribute has not been previously set, returns
  729. * the default value as provided.
  730. *
  731. * This method obviates the need for a hasAttribute() method, as it allows
  732. * specifying a default value to return if the attribute is not found.
  733. *
  734. * @see getAttributes()
  735. * @param string $name The attribute name.
  736. * @param mixed $default Default value to return if the attribute does not exist.
  737. * @return mixed
  738. */
  739. public function getAttribute($name, $default = null);
  740. /**
  741. * Return an instance with the specified derived request attribute.
  742. *
  743. * This method allows setting a single derived request attribute as
  744. * described in getAttributes().
  745. *
  746. * This method MUST be implemented in such a way as to retain the
  747. * immutability of the message, and MUST return an instance that has the
  748. * updated attribute.
  749. *
  750. * @see getAttributes()
  751. * @param string $name The attribute name.
  752. * @param mixed $value The value of the attribute.
  753. * @return static
  754. */
  755. public function withAttribute($name, $value);
  756. /**
  757. * Return an instance that removes the specified derived request attribute.
  758. *
  759. * This method allows removing a single derived request attribute as
  760. * described in getAttributes().
  761. *
  762. * This method MUST be implemented in such a way as to retain the
  763. * immutability of the message, and MUST return an instance that removes
  764. * the attribute.
  765. *
  766. * @see getAttributes()
  767. * @param string $name The attribute name.
  768. * @return static
  769. */
  770. public function withoutAttribute($name);
  771. }
  772. }
  773. // file: vendor/psr/http-message/src/StreamInterface.php
  774. namespace Psr\Http\Message {
  775. /**
  776. * Describes a data stream.
  777. *
  778. * Typically, an instance will wrap a PHP stream; this interface provides
  779. * a wrapper around the most common operations, including serialization of
  780. * the entire stream to a string.
  781. */
  782. interface StreamInterface
  783. {
  784. /**
  785. * Reads all data from the stream into a string, from the beginning to end.
  786. *
  787. * This method MUST attempt to seek to the beginning of the stream before
  788. * reading data and read the stream until the end is reached.
  789. *
  790. * Warning: This could attempt to load a large amount of data into memory.
  791. *
  792. * This method MUST NOT raise an exception in order to conform with PHP's
  793. * string casting operations.
  794. *
  795. * @see http://php.net/manual/en/language.oop5.magic.php#object.tostring
  796. * @return string
  797. */
  798. public function __toString();
  799. /**
  800. * Closes the stream and any underlying resources.
  801. *
  802. * @return void
  803. */
  804. public function close();
  805. /**
  806. * Separates any underlying resources from the stream.
  807. *
  808. * After the stream has been detached, the stream is in an unusable state.
  809. *
  810. * @return resource|null Underlying PHP stream, if any
  811. */
  812. public function detach();
  813. /**
  814. * Get the size of the stream if known.
  815. *
  816. * @return int|null Returns the size in bytes if known, or null if unknown.
  817. */
  818. public function getSize();
  819. /**
  820. * Returns the current position of the file read/write pointer
  821. *
  822. * @return int Position of the file pointer
  823. * @throws \RuntimeException on error.
  824. */
  825. public function tell();
  826. /**
  827. * Returns true if the stream is at the end of the stream.
  828. *
  829. * @return bool
  830. */
  831. public function eof();
  832. /**
  833. * Returns whether or not the stream is seekable.
  834. *
  835. * @return bool
  836. */
  837. public function isSeekable();
  838. /**
  839. * Seek to a position in the stream.
  840. *
  841. * @link http://www.php.net/manual/en/function.fseek.php
  842. * @param int $offset Stream offset
  843. * @param int $whence Specifies how the cursor position will be calculated
  844. * based on the seek offset. Valid values are identical to the built-in
  845. * PHP $whence values for `fseek()`. SEEK_SET: Set position equal to
  846. * offset bytes SEEK_CUR: Set position to current location plus offset
  847. * SEEK_END: Set position to end-of-stream plus offset.
  848. * @throws \RuntimeException on failure.
  849. */
  850. public function seek($offset, $whence = SEEK_SET);
  851. /**
  852. * Seek to the beginning of the stream.
  853. *
  854. * If the stream is not seekable, this method will raise an exception;
  855. * otherwise, it will perform a seek(0).
  856. *
  857. * @see seek()
  858. * @link http://www.php.net/manual/en/function.fseek.php
  859. * @throws \RuntimeException on failure.
  860. */
  861. public function rewind();
  862. /**
  863. * Returns whether or not the stream is writable.
  864. *
  865. * @return bool
  866. */
  867. public function isWritable();
  868. /**
  869. * Write data to the stream.
  870. *
  871. * @param string $string The string that is to be written.
  872. * @return int Returns the number of bytes written to the stream.
  873. * @throws \RuntimeException on failure.
  874. */
  875. public function write($string);
  876. /**
  877. * Returns whether or not the stream is readable.
  878. *
  879. * @return bool
  880. */
  881. public function isReadable();
  882. /**
  883. * Read data from the stream.
  884. *
  885. * @param int $length Read up to $length bytes from the object and return
  886. * them. Fewer than $length bytes may be returned if underlying stream
  887. * call returns fewer bytes.
  888. * @return string Returns the data read from the stream, or an empty string
  889. * if no bytes are available.
  890. * @throws \RuntimeException if an error occurs.
  891. */
  892. public function read($length);
  893. /**
  894. * Returns the remaining contents in a string
  895. *
  896. * @return string
  897. * @throws \RuntimeException if unable to read or an error occurs while
  898. * reading.
  899. */
  900. public function getContents();
  901. /**
  902. * Get stream metadata as an associative array or retrieve a specific key.
  903. *
  904. * The keys returned are identical to the keys returned from PHP's
  905. * stream_get_meta_data() function.
  906. *
  907. * @link http://php.net/manual/en/function.stream-get-meta-data.php
  908. * @param string $key Specific metadata to retrieve.
  909. * @return array|mixed|null Returns an associative array if no key is
  910. * provided. Returns a specific key value if a key is provided and the
  911. * value is found, or null if the key is not found.
  912. */
  913. public function getMetadata($key = null);
  914. }
  915. }
  916. // file: vendor/psr/http-message/src/UploadedFileInterface.php
  917. namespace Psr\Http\Message {
  918. /**
  919. * Value object representing a file uploaded through an HTTP request.
  920. *
  921. * Instances of this interface are considered immutable; all methods that
  922. * might change state MUST be implemented such that they retain the internal
  923. * state of the current instance and return an instance that contains the
  924. * changed state.
  925. */
  926. interface UploadedFileInterface
  927. {
  928. /**
  929. * Retrieve a stream representing the uploaded file.
  930. *
  931. * This method MUST return a StreamInterface instance, representing the
  932. * uploaded file. The purpose of this method is to allow utilizing native PHP
  933. * stream functionality to manipulate the file upload, such as
  934. * stream_copy_to_stream() (though the result will need to be decorated in a
  935. * native PHP stream wrapper to work with such functions).
  936. *
  937. * If the moveTo() method has been called previously, this method MUST raise
  938. * an exception.
  939. *
  940. * @return StreamInterface Stream representation of the uploaded file.
  941. * @throws \RuntimeException in cases when no stream is available or can be
  942. * created.
  943. */
  944. public function getStream();
  945. /**
  946. * Move the uploaded file to a new location.
  947. *
  948. * Use this method as an alternative to move_uploaded_file(). This method is
  949. * guaranteed to work in both SAPI and non-SAPI environments.
  950. * Implementations must determine which environment they are in, and use the
  951. * appropriate method (move_uploaded_file(), rename(), or a stream
  952. * operation) to perform the operation.
  953. *
  954. * $targetPath may be an absolute path, or a relative path. If it is a
  955. * relative path, resolution should be the same as used by PHP's rename()
  956. * function.
  957. *
  958. * The original file or stream MUST be removed on completion.
  959. *
  960. * If this method is called more than once, any subsequent calls MUST raise
  961. * an exception.
  962. *
  963. * When used in an SAPI environment where $_FILES is populated, when writing
  964. * files via moveTo(), is_uploaded_file() and move_uploaded_file() SHOULD be
  965. * used to ensure permissions and upload status are verified correctly.
  966. *
  967. * If you wish to move to a stream, use getStream(), as SAPI operations
  968. * cannot guarantee writing to stream destinations.
  969. *
  970. * @see http://php.net/is_uploaded_file
  971. * @see http://php.net/move_uploaded_file
  972. * @param string $targetPath Path to which to move the uploaded file.
  973. * @throws \InvalidArgumentException if the $targetPath specified is invalid.
  974. * @throws \RuntimeException on any error during the move operation, or on
  975. * the second or subsequent call to the method.
  976. */
  977. public function moveTo($targetPath);
  978. /**
  979. * Retrieve the file size.
  980. *
  981. * Implementations SHOULD return the value stored in the "size" key of
  982. * the file in the $_FILES array if available, as PHP calculates this based
  983. * on the actual size transmitted.
  984. *
  985. * @return int|null The file size in bytes or null if unknown.
  986. */
  987. public function getSize();
  988. /**
  989. * Retrieve the error associated with the uploaded file.
  990. *
  991. * The return value MUST be one of PHP's UPLOAD_ERR_XXX constants.
  992. *
  993. * If the file was uploaded successfully, this method MUST return
  994. * UPLOAD_ERR_OK.
  995. *
  996. * Implementations SHOULD return the value stored in the "error" key of
  997. * the file in the $_FILES array.
  998. *
  999. * @see http://php.net/manual/en/features.file-upload.errors.php
  1000. * @return int One of PHP's UPLOAD_ERR_XXX constants.
  1001. */
  1002. public function getError();
  1003. /**
  1004. * Retrieve the filename sent by the client.
  1005. *
  1006. * Do not trust the value returned by this method. A client could send
  1007. * a malicious filename with the intention to corrupt or hack your
  1008. * application.
  1009. *
  1010. * Implementations SHOULD return the value stored in the "name" key of
  1011. * the file in the $_FILES array.
  1012. *
  1013. * @return string|null The filename sent by the client or null if none
  1014. * was provided.
  1015. */
  1016. public function getClientFilename();
  1017. /**
  1018. * Retrieve the media type sent by the client.
  1019. *
  1020. * Do not trust the value returned by this method. A client could send
  1021. * a malicious media type with the intention to corrupt or hack your
  1022. * application.
  1023. *
  1024. * Implementations SHOULD return the value stored in the "type" key of
  1025. * the file in the $_FILES array.
  1026. *
  1027. * @return string|null The media type sent by the client or null if none
  1028. * was provided.
  1029. */
  1030. public function getClientMediaType();
  1031. }
  1032. }
  1033. // file: vendor/psr/http-message/src/UriInterface.php
  1034. namespace Psr\Http\Message {
  1035. /**
  1036. * Value object representing a URI.
  1037. *
  1038. * This interface is meant to represent URIs according to RFC 3986 and to
  1039. * provide methods for most common operations. Additional functionality for
  1040. * working with URIs can be provided on top of the interface or externally.
  1041. * Its primary use is for HTTP requests, but may also be used in other
  1042. * contexts.
  1043. *
  1044. * Instances of this interface are considered immutable; all methods that
  1045. * might change state MUST be implemented such that they retain the internal
  1046. * state of the current instance and return an instance that contains the
  1047. * changed state.
  1048. *
  1049. * Typically the Host header will be also be present in the request message.
  1050. * For server-side requests, the scheme will typically be discoverable in the
  1051. * server parameters.
  1052. *
  1053. * @link http://tools.ietf.org/html/rfc3986 (the URI specification)
  1054. */
  1055. interface UriInterface
  1056. {
  1057. /**
  1058. * Retrieve the scheme component of the URI.
  1059. *
  1060. * If no scheme is present, this method MUST return an empty string.
  1061. *
  1062. * The value returned MUST be normalized to lowercase, per RFC 3986
  1063. * Section 3.1.
  1064. *
  1065. * The trailing ":" character is not part of the scheme and MUST NOT be
  1066. * added.
  1067. *
  1068. * @see https://tools.ietf.org/html/rfc3986#section-3.1
  1069. * @return string The URI scheme.
  1070. */
  1071. public function getScheme();
  1072. /**
  1073. * Retrieve the authority component of the URI.
  1074. *
  1075. * If no authority information is present, this method MUST return an empty
  1076. * string.
  1077. *
  1078. * The authority syntax of the URI is:
  1079. *
  1080. * <pre>
  1081. * [user-info@]host[:port]
  1082. * </pre>
  1083. *
  1084. * If the port component is not set or is the standard port for the current
  1085. * scheme, it SHOULD NOT be included.
  1086. *
  1087. * @see https://tools.ietf.org/html/rfc3986#section-3.2
  1088. * @return string The URI authority, in "[user-info@]host[:port]" format.
  1089. */
  1090. public function getAuthority();
  1091. /**
  1092. * Retrieve the user information component of the URI.
  1093. *
  1094. * If no user information is present, this method MUST return an empty
  1095. * string.
  1096. *
  1097. * If a user is present in the URI, this will return that value;
  1098. * additionally, if the password is also present, it will be appended to the
  1099. * user value, with a colon (":") separating the values.
  1100. *
  1101. * The trailing "@" character is not part of the user information and MUST
  1102. * NOT be added.
  1103. *
  1104. * @return string The URI user information, in "username[:password]" format.
  1105. */
  1106. public function getUserInfo();
  1107. /**
  1108. * Retrieve the host component of the URI.
  1109. *
  1110. * If no host is present, this method MUST return an empty string.
  1111. *
  1112. * The value returned MUST be normalized to lowercase, per RFC 3986
  1113. * Section 3.2.2.
  1114. *
  1115. * @see http://tools.ietf.org/html/rfc3986#section-3.2.2
  1116. * @return string The URI host.
  1117. */
  1118. public function getHost();
  1119. /**
  1120. * Retrieve the port component of the URI.
  1121. *
  1122. * If a port is present, and it is non-standard for the current scheme,
  1123. * this method MUST return it as an integer. If the port is the standard port
  1124. * used with the current scheme, this method SHOULD return null.
  1125. *
  1126. * If no port is present, and no scheme is present, this method MUST return
  1127. * a null value.
  1128. *
  1129. * If no port is present, but a scheme is present, this method MAY return
  1130. * the standard port for that scheme, but SHOULD return null.
  1131. *
  1132. * @return null|int The URI port.
  1133. */
  1134. public function getPort();
  1135. /**
  1136. * Retrieve the path component of the URI.
  1137. *
  1138. * The path can either be empty or absolute (starting with a slash) or
  1139. * rootless (not starting with a slash). Implementations MUST support all
  1140. * three syntaxes.
  1141. *
  1142. * Normally, the empty path "" and absolute path "/" are considered equal as
  1143. * defined in RFC 7230 Section 2.7.3. But this method MUST NOT automatically
  1144. * do this normalization because in contexts with a trimmed base path, e.g.
  1145. * the front controller, this difference becomes significant. It's the task
  1146. * of the user to handle both "" and "/".
  1147. *
  1148. * The value returned MUST be percent-encoded, but MUST NOT double-encode
  1149. * any characters. To determine what characters to encode, please refer to
  1150. * RFC 3986, Sections 2 and 3.3.
  1151. *
  1152. * As an example, if the value should include a slash ("/") not intended as
  1153. * delimiter between path segments, that value MUST be passed in encoded
  1154. * form (e.g., "%2F") to the instance.
  1155. *
  1156. * @see https://tools.ietf.org/html/rfc3986#section-2
  1157. * @see https://tools.ietf.org/html/rfc3986#section-3.3
  1158. * @return string The URI path.
  1159. */
  1160. public function getPath();
  1161. /**
  1162. * Retrieve the query string of the URI.
  1163. *
  1164. * If no query string is present, this method MUST return an empty string.
  1165. *
  1166. * The leading "?" character is not part of the query and MUST NOT be
  1167. * added.
  1168. *
  1169. * The value returned MUST be percent-encoded, but MUST NOT double-encode
  1170. * any characters. To determine what characters to encode, please refer to
  1171. * RFC 3986, Sections 2 and 3.4.
  1172. *
  1173. * As an example, if a value in a key/value pair of the query string should
  1174. * include an ampersand ("&") not intended as a delimiter between values,
  1175. * that value MUST be passed in encoded form (e.g., "%26") to the instance.
  1176. *
  1177. * @see https://tools.ietf.org/html/rfc3986#section-2
  1178. * @see https://tools.ietf.org/html/rfc3986#section-3.4
  1179. * @return string The URI query string.
  1180. */
  1181. public function getQuery();
  1182. /**
  1183. * Retrieve the fragment component of the URI.
  1184. *
  1185. * If no fragment is present, this method MUST return an empty string.
  1186. *
  1187. * The leading "#" character is not part of the fragment and MUST NOT be
  1188. * added.
  1189. *
  1190. * The value returned MUST be percent-encoded, but MUST NOT double-encode
  1191. * any characters. To determine what characters to encode, please refer to
  1192. * RFC 3986, Sections 2 and 3.5.
  1193. *
  1194. * @see https://tools.ietf.org/html/rfc3986#section-2
  1195. * @see https://tools.ietf.org/html/rfc3986#section-3.5
  1196. * @return string The URI fragment.
  1197. */
  1198. public function getFragment();
  1199. /**
  1200. * Return an instance with the specified scheme.
  1201. *
  1202. * This method MUST retain the state of the current instance, and return
  1203. * an instance that contains the specified scheme.
  1204. *
  1205. * Implementations MUST support the schemes "http" and "https" case
  1206. * insensitively, and MAY accommodate other schemes if required.
  1207. *
  1208. * An empty scheme is equivalent to removing the scheme.
  1209. *
  1210. * @param string $scheme The scheme to use with the new instance.
  1211. * @return static A new instance with the specified scheme.
  1212. * @throws \InvalidArgumentException for invalid or unsupported schemes.
  1213. */
  1214. public function withScheme($scheme);
  1215. /**
  1216. * Return an instance with the specified user information.
  1217. *
  1218. * This method MUST retain the state of the current instance, and return
  1219. * an instance that contains the specified user information.
  1220. *
  1221. * Password is optional, but the user information MUST include the
  1222. * user; an empty string for the user is equivalent to removing user
  1223. * information.
  1224. *
  1225. * @param string $user The user name to use for authority.
  1226. * @param null|string $password The password associated with $user.
  1227. * @return static A new instance with the specified user information.
  1228. */
  1229. public function withUserInfo($user, $password = null);
  1230. /**
  1231. * Return an instance with the specified host.
  1232. *
  1233. * This method MUST retain the state of the current instance, and return
  1234. * an instance that contains the specified host.
  1235. *
  1236. * An empty host value is equivalent to removing the host.
  1237. *
  1238. * @param string $host The hostname to use with the new instance.
  1239. * @return static A new instance with the specified host.
  1240. * @throws \InvalidArgumentException for invalid hostnames.
  1241. */
  1242. public function withHost($host);
  1243. /**
  1244. * Return an instance with the specified port.
  1245. *
  1246. * This method MUST retain the state of the current instance, and return
  1247. * an instance that contains the specified port.
  1248. *
  1249. * Implementations MUST raise an exception for ports outside the
  1250. * established TCP and UDP port ranges.
  1251. *
  1252. * A null value provided for the port is equivalent to removing the port
  1253. * information.
  1254. *
  1255. * @param null|int $port The port to use with the new instance; a null value
  1256. * removes the port information.
  1257. * @return static A new instance with the specified port.
  1258. * @throws \InvalidArgumentException for invalid ports.
  1259. */
  1260. public function withPort($port);
  1261. /**
  1262. * Return an instance with the specified path.
  1263. *
  1264. * This method MUST retain the state of the current instance, and return
  1265. * an instance that contains the specified path.
  1266. *
  1267. * The path can either be empty or absolute (starting with a slash) or
  1268. * rootless (not starting with a slash). Implementations MUST support all
  1269. * three syntaxes.
  1270. *
  1271. * If the path is intended to be domain-relative rather than path relative then
  1272. * it must begin with a slash ("/"). Paths not starting with a slash ("/")
  1273. * are assumed to be relative to some base path known to the application or
  1274. * consumer.
  1275. *
  1276. * Users can provide both encoded and decoded path characters.
  1277. * Implementations ensure the correct encoding as outlined in getPath().
  1278. *
  1279. * @param string $path The path to use with the new instance.
  1280. * @return static A new instance with the specified path.
  1281. * @throws \InvalidArgumentException for invalid paths.
  1282. */
  1283. public function withPath($path);
  1284. /**
  1285. * Return an instance with the specified query string.
  1286. *
  1287. * This method MUST retain the state of the current instance, and return
  1288. * an instance that contains the specified query string.
  1289. *
  1290. * Users can provide both encoded and decoded query characters.
  1291. * Implementations ensure the correct encoding as outlined in getQuery().
  1292. *
  1293. * An empty query string value is equivalent to removing the query string.
  1294. *
  1295. * @param string $query The query string to use with the new instance.
  1296. * @return static A new instance with the specified query string.
  1297. * @throws \InvalidArgumentException for invalid query strings.
  1298. */
  1299. public function withQuery($query);
  1300. /**
  1301. * Return an instance with the specified URI fragment.
  1302. *
  1303. * This method MUST retain the state of the current instance, and return
  1304. * an instance that contains the specified URI fragment.
  1305. *
  1306. * Users can provide both encoded and decoded fragment characters.
  1307. * Implementations ensure the correct encoding as outlined in getFragment().
  1308. *
  1309. * An empty fragment value is equivalent to removing the fragment.
  1310. *
  1311. * @param string $fragment The fragment to use with the new instance.
  1312. * @return static A new instance with the specified fragment.
  1313. */
  1314. public function withFragment($fragment);
  1315. /**
  1316. * Return the string representation as a URI reference.
  1317. *
  1318. * Depending on which components of the URI are present, the resulting
  1319. * string is either a full URI or relative reference according to RFC 3986,
  1320. * Section 4.1. The method concatenates the various components of the URI,
  1321. * using the appropriate delimiters:
  1322. *
  1323. * - If a scheme is present, it MUST be suffixed by ":".
  1324. * - If an authority is present, it MUST be prefixed by "//".
  1325. * - The path can be concatenated without delimiters. But there are two
  1326. * cases where the path has to be adjusted to make the URI reference
  1327. * valid as PHP does not allow to throw an exception in __toString():
  1328. * - If the path is rootless and an authority is present, the path MUST
  1329. * be prefixed by "/".
  1330. * - If the path is starting with more than one "/" and no authority is
  1331. * present, the starting slashes MUST be reduced to one.
  1332. * - If a query is present, it MUST be prefixed by "?".
  1333. * - If a fragment is present, it MUST be prefixed by "#".
  1334. *
  1335. * @see http://tools.ietf.org/html/rfc3986#section-4.1
  1336. * @return string
  1337. */
  1338. public function __toString();
  1339. }
  1340. }
  1341. // file: vendor/psr/http-server-handler/src/RequestHandlerInterface.php
  1342. namespace Psr\Http\Server {
  1343. use Psr\Http\Message\ResponseInterface;
  1344. use Psr\Http\Message\ServerRequestInterface;
  1345. /**
  1346. * Handles a server request and produces a response.
  1347. *
  1348. * An HTTP request handler process an HTTP request in order to produce an
  1349. * HTTP response.
  1350. */
  1351. interface RequestHandlerInterface
  1352. {
  1353. /**
  1354. * Handles a request and produces a response.
  1355. *
  1356. * May call other collaborating code to generate the response.
  1357. */
  1358. public function handle(ServerRequestInterface $request): ResponseInterface;
  1359. }
  1360. }
  1361. // file: vendor/psr/http-server-middleware/src/MiddlewareInterface.php
  1362. namespace Psr\Http\Server {
  1363. use Psr\Http\Message\ResponseInterface;
  1364. use Psr\Http\Message\ServerRequestInterface;
  1365. /**
  1366. * Participant in processing a server request and response.
  1367. *
  1368. * An HTTP middleware component participates in processing an HTTP message:
  1369. * by acting on the request, generating the response, or forwarding the
  1370. * request to a subsequent middleware and possibly acting on its response.
  1371. */
  1372. interface MiddlewareInterface
  1373. {
  1374. /**
  1375. * Process an incoming server request.
  1376. *
  1377. * Processes an incoming server request in order to produce a response.
  1378. * If unable to produce the response itself, it may delegate to the provided
  1379. * request handler to do so.
  1380. */
  1381. public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface;
  1382. }
  1383. }
  1384. // file: vendor/nyholm/psr7/src/Factory/Psr17Factory.php
  1385. namespace Nyholm\Psr7\Factory {
  1386. use Nyholm\Psr7\{Request, Response, ServerRequest, Stream, UploadedFile, Uri};
  1387. use Psr\Http\Message\{RequestFactoryInterface, RequestInterface, ResponseFactoryInterface, ResponseInterface, ServerRequestFactoryInterface, ServerRequestInterface, StreamFactoryInterface, StreamInterface, UploadedFileFactoryInterface, UploadedFileInterface, UriFactoryInterface, UriInterface};
  1388. /**
  1389. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  1390. * @author Martijn van der Ven <martijn@vanderven.se>
  1391. */
  1392. final class Psr17Factory implements RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface, UriFactoryInterface
  1393. {
  1394. public function createRequest(string $method, $uri): RequestInterface
  1395. {
  1396. return new Request($method, $uri);
  1397. }
  1398. public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
  1399. {
  1400. if (2 > \func_num_args()) {
  1401. // This will make the Response class to use a custom reasonPhrase
  1402. $reasonPhrase = null;
  1403. }
  1404. return new Response($code, [], null, '1.1', $reasonPhrase);
  1405. }
  1406. public function createStream(string $content = ''): StreamInterface
  1407. {
  1408. return Stream::create($content);
  1409. }
  1410. public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
  1411. {
  1412. $resource = @\fopen($filename, $mode);
  1413. if (false === $resource) {
  1414. if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'])) {
  1415. throw new \InvalidArgumentException('The mode ' . $mode . ' is invalid.');
  1416. }
  1417. throw new \RuntimeException('The file ' . $filename . ' cannot be opened.');
  1418. }
  1419. return Stream::create($resource);
  1420. }
  1421. public function createStreamFromResource($resource): StreamInterface
  1422. {
  1423. return Stream::create($resource);
  1424. }
  1425. public function createUploadedFile(StreamInterface $stream, int $size = null, int $error = \UPLOAD_ERR_OK, string $clientFilename = null, string $clientMediaType = null): UploadedFileInterface
  1426. {
  1427. if (null === $size) {
  1428. $size = $stream->getSize();
  1429. }
  1430. return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType);
  1431. }
  1432. public function createUri(string $uri = ''): UriInterface
  1433. {
  1434. return new Uri($uri);
  1435. }
  1436. public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
  1437. {
  1438. return new ServerRequest($method, $uri, [], null, '1.1', $serverParams);
  1439. }
  1440. }
  1441. }
  1442. // file: vendor/nyholm/psr7/src/MessageTrait.php
  1443. namespace Nyholm\Psr7 {
  1444. use Psr\Http\Message\StreamInterface;
  1445. /**
  1446. * Trait implementing functionality common to requests and responses.
  1447. *
  1448. * @author Michael Dowling and contributors to guzzlehttp/psr7
  1449. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  1450. * @author Martijn van der Ven <martijn@vanderven.se>
  1451. *
  1452. * @internal should not be used outside of Nyholm/Psr7 as it does not fall under our BC promise
  1453. */
  1454. trait MessageTrait
  1455. {
  1456. /** @var array Map of all registered headers, as original name => array of values */
  1457. private $headers = [];
  1458. /** @var array Map of lowercase header name => original name at registration */
  1459. private $headerNames = [];
  1460. /** @var string */
  1461. private $protocol = '1.1';
  1462. /** @var StreamInterface|null */
  1463. private $stream;
  1464. public function getProtocolVersion(): string
  1465. {
  1466. return $this->protocol;
  1467. }
  1468. public function withProtocolVersion($version): self
  1469. {
  1470. if ($this->protocol === $version) {
  1471. return $this;
  1472. }
  1473. $new = clone $this;
  1474. $new->protocol = $version;
  1475. return $new;
  1476. }
  1477. public function getHeaders(): array
  1478. {
  1479. return $this->headers;
  1480. }
  1481. public function hasHeader($header): bool
  1482. {
  1483. return isset($this->headerNames[\strtolower($header)]);
  1484. }
  1485. public function getHeader($header): array
  1486. {
  1487. $header = \strtolower($header);
  1488. if (!isset($this->headerNames[$header])) {
  1489. return [];
  1490. }
  1491. $header = $this->headerNames[$header];
  1492. return $this->headers[$header];
  1493. }
  1494. public function getHeaderLine($header): string
  1495. {
  1496. return \implode(', ', $this->getHeader($header));
  1497. }
  1498. public function withHeader($header, $value): self
  1499. {
  1500. $value = $this->validateAndTrimHeader($header, $value);
  1501. $normalized = \strtolower($header);
  1502. $new = clone $this;
  1503. if (isset($new->headerNames[$normalized])) {
  1504. unset($new->headers[$new->headerNames[$normalized]]);
  1505. }
  1506. $new->headerNames[$normalized] = $header;
  1507. $new->headers[$header] = $value;
  1508. return $new;
  1509. }
  1510. public function withAddedHeader($header, $value): self
  1511. {
  1512. if (!\is_string($header) || '' === $header) {
  1513. throw new \InvalidArgumentException('Header name must be an RFC 7230 compatible string.');
  1514. }
  1515. $new = clone $this;
  1516. $new->setHeaders([$header => $value]);
  1517. return $new;
  1518. }
  1519. public function withoutHeader($header): self
  1520. {
  1521. $normalized = \strtolower($header);
  1522. if (!isset($this->headerNames[$normalized])) {
  1523. return $this;
  1524. }
  1525. $header = $this->headerNames[$normalized];
  1526. $new = clone $this;
  1527. unset($new->headers[$header], $new->headerNames[$normalized]);
  1528. return $new;
  1529. }
  1530. public function getBody(): StreamInterface
  1531. {
  1532. if (null === $this->stream) {
  1533. $this->stream = Stream::create('');
  1534. }
  1535. return $this->stream;
  1536. }
  1537. public function withBody(StreamInterface $body): self
  1538. {
  1539. if ($body === $this->stream) {
  1540. return $this;
  1541. }
  1542. $new = clone $this;
  1543. $new->stream = $body;
  1544. return $new;
  1545. }
  1546. private function setHeaders(array $headers) /*:void*/
  1547. {
  1548. foreach ($headers as $header => $value) {
  1549. $value = $this->validateAndTrimHeader($header, $value);
  1550. $normalized = \strtolower($header);
  1551. if (isset($this->headerNames[$normalized])) {
  1552. $header = $this->headerNames[$normalized];
  1553. $this->headers[$header] = \array_merge($this->headers[$header], $value);
  1554. } else {
  1555. $this->headerNames[$normalized] = $header;
  1556. $this->headers[$header] = $value;
  1557. }
  1558. }
  1559. }
  1560. /**
  1561. * Make sure the header complies with RFC 7230.
  1562. *
  1563. * Header names must be a non-empty string consisting of token characters.
  1564. *
  1565. * Header values must be strings consisting of visible characters with all optional
  1566. * leading and trailing whitespace stripped. This method will always strip such
  1567. * optional whitespace. Note that the method does not allow folding whitespace within
  1568. * the values as this was deprecated for almost all instances by the RFC.
  1569. *
  1570. * header-field = field-name ":" OWS field-value OWS
  1571. * field-name = 1*( "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." / "^"
  1572. * / "_" / "`" / "|" / "~" / %x30-39 / ( %x41-5A / %x61-7A ) )
  1573. * OWS = *( SP / HTAB )
  1574. * field-value = *( ( %x21-7E / %x80-FF ) [ 1*( SP / HTAB ) ( %x21-7E / %x80-FF ) ] )
  1575. *
  1576. * @see https://tools.ietf.org/html/rfc7230#section-3.2.4
  1577. */
  1578. private function validateAndTrimHeader($header, $values): array
  1579. {
  1580. if (!\is_string($header) || 1 !== \preg_match("@^[!#$%&'*+.^_`|~0-9A-Za-z-]+$@", $header)) {
  1581. throw new \InvalidArgumentException('Header name must be an RFC 7230 compatible string.');
  1582. }
  1583. if (!\is_array($values)) {
  1584. // This is simple, just one value.
  1585. if ((!\is_numeric($values) && !\is_string($values)) || 1 !== \preg_match("@^[ \t\x21-\x7E\x80-\xFF]*$@", (string) $values)) {
  1586. throw new \InvalidArgumentException('Header values must be RFC 7230 compatible strings.');
  1587. }
  1588. return [\trim((string) $values, " \t")];
  1589. }
  1590. if (empty($values)) {
  1591. throw new \InvalidArgumentException('Header values must be a string or an array of strings, empty array given.');
  1592. }
  1593. // Assert Non empty array
  1594. $returnValues = [];
  1595. foreach ($values as $v) {
  1596. if ((!\is_numeric($v) && !\is_string($v)) || 1 !== \preg_match("@^[ \t\x21-\x7E\x80-\xFF]*$@", (string) $v)) {
  1597. throw new \InvalidArgumentException('Header values must be RFC 7230 compatible strings.');
  1598. }
  1599. $returnValues[] = \trim((string) $v, " \t");
  1600. }
  1601. return $returnValues;
  1602. }
  1603. }
  1604. }
  1605. // file: vendor/nyholm/psr7/src/Request.php
  1606. namespace Nyholm\Psr7 {
  1607. use Psr\Http\Message\{RequestInterface, StreamInterface, UriInterface};
  1608. /**
  1609. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  1610. * @author Martijn van der Ven <martijn@vanderven.se>
  1611. */
  1612. final class Request implements RequestInterface
  1613. {
  1614. use MessageTrait;
  1615. use RequestTrait;
  1616. /**
  1617. * @param string $method HTTP method
  1618. * @param string|UriInterface $uri URI
  1619. * @param array $headers Request headers
  1620. * @param string|resource|StreamInterface|null $body Request body
  1621. * @param string $version Protocol version
  1622. */
  1623. public function __construct(string $method, $uri, array $headers = [], $body = null, string $version = '1.1')
  1624. {
  1625. if (!($uri instanceof UriInterface)) {
  1626. $uri = new Uri($uri);
  1627. }
  1628. $this->method = $method;
  1629. $this->uri = $uri;
  1630. $this->setHeaders($headers);
  1631. $this->protocol = $version;
  1632. if (!$this->hasHeader('Host')) {
  1633. $this->updateHostFromUri();
  1634. }
  1635. // If we got no body, defer initialization of the stream until Request::getBody()
  1636. if ('' !== $body && null !== $body) {
  1637. $this->stream = Stream::create($body);
  1638. }
  1639. }
  1640. }
  1641. }
  1642. // file: vendor/nyholm/psr7/src/RequestTrait.php
  1643. namespace Nyholm\Psr7 {
  1644. use Psr\Http\Message\UriInterface;
  1645. /**
  1646. * @author Michael Dowling and contributors to guzzlehttp/psr7
  1647. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  1648. * @author Martijn van der Ven <martijn@vanderven.se>
  1649. *
  1650. * @internal should not be used outside of Nyholm/Psr7 as it does not fall under our BC promise
  1651. */
  1652. trait RequestTrait
  1653. {
  1654. /** @var string */
  1655. private $method;
  1656. /** @var string|null */
  1657. private $requestTarget;
  1658. /** @var UriInterface|null */
  1659. private $uri;
  1660. public function getRequestTarget(): string
  1661. {
  1662. if (null !== $this->requestTarget) {
  1663. return $this->requestTarget;
  1664. }
  1665. if ('' === $target = $this->uri->getPath()) {
  1666. $target = '/';
  1667. }
  1668. if ('' !== $this->uri->getQuery()) {
  1669. $target .= '?' . $this->uri->getQuery();
  1670. }
  1671. return $target;
  1672. }
  1673. public function withRequestTarget($requestTarget): self
  1674. {
  1675. if (\preg_match('#\s#', $requestTarget)) {
  1676. throw new \InvalidArgumentException('Invalid request target provided; cannot contain whitespace');
  1677. }
  1678. $new = clone $this;
  1679. $new->requestTarget = $requestTarget;
  1680. return $new;
  1681. }
  1682. public function getMethod(): string
  1683. {
  1684. return $this->method;
  1685. }
  1686. public function withMethod($method): self
  1687. {
  1688. if (!\is_string($method)) {
  1689. throw new \InvalidArgumentException('Method must be a string');
  1690. }
  1691. $new = clone $this;
  1692. $new->method = $method;
  1693. return $new;
  1694. }
  1695. public function getUri(): UriInterface
  1696. {
  1697. return $this->uri;
  1698. }
  1699. public function withUri(UriInterface $uri, $preserveHost = false): self
  1700. {
  1701. if ($uri === $this->uri) {
  1702. return $this;
  1703. }
  1704. $new = clone $this;
  1705. $new->uri = $uri;
  1706. if (!$preserveHost || !$this->hasHeader('Host')) {
  1707. $new->updateHostFromUri();
  1708. }
  1709. return $new;
  1710. }
  1711. private function updateHostFromUri() /*:void*/
  1712. {
  1713. if ('' === $host = $this->uri->getHost()) {
  1714. return;
  1715. }
  1716. if (null !== ($port = $this->uri->getPort())) {
  1717. $host .= ':' . $port;
  1718. }
  1719. if (isset($this->headerNames['host'])) {
  1720. $header = $this->headerNames['host'];
  1721. } else {
  1722. $this->headerNames['host'] = $header = 'Host';
  1723. }
  1724. // Ensure Host is the first header.
  1725. // See: http://tools.ietf.org/html/rfc7230#section-5.4
  1726. $this->headers = [$header => [$host]] + $this->headers;
  1727. }
  1728. }
  1729. }
  1730. // file: vendor/nyholm/psr7/src/Response.php
  1731. namespace Nyholm\Psr7 {
  1732. use Psr\Http\Message\{ResponseInterface, StreamInterface};
  1733. /**
  1734. * @author Michael Dowling and contributors to guzzlehttp/psr7
  1735. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  1736. * @author Martijn van der Ven <martijn@vanderven.se>
  1737. */
  1738. final class Response implements ResponseInterface
  1739. {
  1740. use MessageTrait;
  1741. /** @var array Map of standard HTTP status code/reason phrases */
  1742. /*private*/ const PHRASES = [
  1743. 100 => 'Continue', 101 => 'Switching Protocols', 102 => 'Processing',
  1744. 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 207 => 'Multi-status', 208 => 'Already Reported',
  1745. 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 306 => 'Switch Proxy', 307 => 'Temporary Redirect',
  1746. 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Time-out', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large', 415 => 'Unsupported Media Type', 416 => 'Requested range not satisfiable', 417 => 'Expectation Failed', 418 => 'I\'m a teapot', 422 => 'Unprocessable Entity', 423 => 'Locked', 424 => 'Failed Dependency', 425 => 'Unordered Collection', 426 => 'Upgrade Required', 428 => 'Precondition Required', 429 => 'Too Many Requests', 431 => 'Request Header Fields Too Large', 451 => 'Unavailable For Legal Reasons',
  1747. 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Time-out', 505 => 'HTTP Version not supported', 506 => 'Variant Also Negotiates', 507 => 'Insufficient Storage', 508 => 'Loop Detected', 511 => 'Network Authentication Required',
  1748. ];
  1749. /** @var string */
  1750. private $reasonPhrase = '';
  1751. /** @var int */
  1752. private $statusCode;
  1753. /**
  1754. * @param int $status Status code
  1755. * @param array $headers Response headers
  1756. * @param string|resource|StreamInterface|null $body Response body
  1757. * @param string $version Protocol version
  1758. * @param string|null $reason Reason phrase (when empty a default will be used based on the status code)
  1759. */
  1760. public function __construct(int $status = 200, array $headers = [], $body = null, string $version = '1.1', string $reason = null)
  1761. {
  1762. // If we got no body, defer initialization of the stream until Response::getBody()
  1763. if ('' !== $body && null !== $body) {
  1764. $this->stream = Stream::create($body);
  1765. }
  1766. $this->statusCode = $status;
  1767. $this->setHeaders($headers);
  1768. if (null === $reason && isset(self::PHRASES[$this->statusCode])) {
  1769. $this->reasonPhrase = self::PHRASES[$status];
  1770. } else {
  1771. $this->reasonPhrase = $reason ?? '';
  1772. }
  1773. $this->protocol = $version;
  1774. }
  1775. public function getStatusCode(): int
  1776. {
  1777. return $this->statusCode;
  1778. }
  1779. public function getReasonPhrase(): string
  1780. {
  1781. return $this->reasonPhrase;
  1782. }
  1783. public function withStatus($code, $reasonPhrase = ''): self
  1784. {
  1785. if (!\is_int($code) && !\is_string($code)) {
  1786. throw new \InvalidArgumentException('Status code has to be an integer');
  1787. }
  1788. $code = (int) $code;
  1789. if ($code < 100 || $code > 599) {
  1790. throw new \InvalidArgumentException('Status code has to be an integer between 100 and 599');
  1791. }
  1792. $new = clone $this;
  1793. $new->statusCode = $code;
  1794. if ((null === $reasonPhrase || '' === $reasonPhrase) && isset(self::PHRASES[$new->statusCode])) {
  1795. $reasonPhrase = self::PHRASES[$new->statusCode];
  1796. }
  1797. $new->reasonPhrase = $reasonPhrase;
  1798. return $new;
  1799. }
  1800. }
  1801. }
  1802. // file: vendor/nyholm/psr7/src/ServerRequest.php
  1803. namespace Nyholm\Psr7 {
  1804. use Psr\Http\Message\{ServerRequestInterface, StreamInterface, UploadedFileInterface, UriInterface};
  1805. /**
  1806. * @author Michael Dowling and contributors to guzzlehttp/psr7
  1807. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  1808. * @author Martijn van der Ven <martijn@vanderven.se>
  1809. */
  1810. final class ServerRequest implements ServerRequestInterface
  1811. {
  1812. use MessageTrait;
  1813. use RequestTrait;
  1814. /** @var array */
  1815. private $attributes = [];
  1816. /** @var array */
  1817. private $cookieParams = [];
  1818. /** @var array|object|null */
  1819. private $parsedBody;
  1820. /** @var array */
  1821. private $queryParams = [];
  1822. /** @var array */
  1823. private $serverParams;
  1824. /** @var UploadedFileInterface[] */
  1825. private $uploadedFiles = [];
  1826. /**
  1827. * @param string $method HTTP method
  1828. * @param string|UriInterface $uri URI
  1829. * @param array $headers Request headers
  1830. * @param string|resource|StreamInterface|null $body Request body
  1831. * @param string $version Protocol version
  1832. * @param array $serverParams Typically the $_SERVER superglobal
  1833. */
  1834. public function __construct(string $method, $uri, array $headers = [], $body = null, string $version = '1.1', array $serverParams = [])
  1835. {
  1836. $this->serverParams = $serverParams;
  1837. if (!($uri instanceof UriInterface)) {
  1838. $uri = new Uri($uri);
  1839. }
  1840. $this->method = $method;
  1841. $this->uri = $uri;
  1842. $this->setHeaders($headers);
  1843. $this->protocol = $version;
  1844. if (!$this->hasHeader('Host')) {
  1845. $this->updateHostFromUri();
  1846. }
  1847. // If we got no body, defer initialization of the stream until ServerRequest::getBody()
  1848. if ('' !== $body && null !== $body) {
  1849. $this->stream = Stream::create($body);
  1850. }
  1851. }
  1852. public function getServerParams(): array
  1853. {
  1854. return $this->serverParams;
  1855. }
  1856. public function getUploadedFiles(): array
  1857. {
  1858. return $this->uploadedFiles;
  1859. }
  1860. public function withUploadedFiles(array $uploadedFiles)
  1861. {
  1862. $new = clone $this;
  1863. $new->uploadedFiles = $uploadedFiles;
  1864. return $new;
  1865. }
  1866. public function getCookieParams(): array
  1867. {
  1868. return $this->cookieParams;
  1869. }
  1870. public function withCookieParams(array $cookies)
  1871. {
  1872. $new = clone $this;
  1873. $new->cookieParams = $cookies;
  1874. return $new;
  1875. }
  1876. public function getQueryParams(): array
  1877. {
  1878. return $this->queryParams;
  1879. }
  1880. public function withQueryParams(array $query)
  1881. {
  1882. $new = clone $this;
  1883. $new->queryParams = $query;
  1884. return $new;
  1885. }
  1886. public function getParsedBody()
  1887. {
  1888. return $this->parsedBody;
  1889. }
  1890. public function withParsedBody($data)
  1891. {
  1892. if (!\is_array($data) && !\is_object($data) && null !== $data) {
  1893. throw new \InvalidArgumentException('First parameter to withParsedBody MUST be object, array or null');
  1894. }
  1895. $new = clone $this;
  1896. $new->parsedBody = $data;
  1897. return $new;
  1898. }
  1899. public function getAttributes(): array
  1900. {
  1901. return $this->attributes;
  1902. }
  1903. public function getAttribute($attribute, $default = null)
  1904. {
  1905. if (false === \array_key_exists($attribute, $this->attributes)) {
  1906. return $default;
  1907. }
  1908. return $this->attributes[$attribute];
  1909. }
  1910. public function withAttribute($attribute, $value): self
  1911. {
  1912. $new = clone $this;
  1913. $new->attributes[$attribute] = $value;
  1914. return $new;
  1915. }
  1916. public function withoutAttribute($attribute): self
  1917. {
  1918. if (false === \array_key_exists($attribute, $this->attributes)) {
  1919. return $this;
  1920. }
  1921. $new = clone $this;
  1922. unset($new->attributes[$attribute]);
  1923. return $new;
  1924. }
  1925. }
  1926. }
  1927. // file: vendor/nyholm/psr7/src/Stream.php
  1928. namespace Nyholm\Psr7 {
  1929. use Psr\Http\Message\StreamInterface;
  1930. /**
  1931. * @author Michael Dowling and contributors to guzzlehttp/psr7
  1932. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  1933. * @author Martijn van der Ven <martijn@vanderven.se>
  1934. */
  1935. final class Stream implements StreamInterface
  1936. {
  1937. /** @var resource|null A resource reference */
  1938. private $stream;
  1939. /** @var bool */
  1940. private $seekable;
  1941. /** @var bool */
  1942. private $readable;
  1943. /** @var bool */
  1944. private $writable;
  1945. /** @var array|mixed|void|null */
  1946. private $uri;
  1947. /** @var int|null */
  1948. private $size;
  1949. /** @var array Hash of readable and writable stream types */
  1950. /*private*/ const READ_WRITE_HASH = [
  1951. 'read' => [
  1952. 'r' => true, 'w+' => true, 'r+' => true, 'x+' => true, 'c+' => true,
  1953. 'rb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true,
  1954. 'c+b' => true, 'rt' => true, 'w+t' => true, 'r+t' => true,
  1955. 'x+t' => true, 'c+t' => true, 'a+' => true,
  1956. ],
  1957. 'write' => [
  1958. 'w' => true, 'w+' => true, 'rw' => true, 'r+' => true, 'x+' => true,
  1959. 'c+' => true, 'wb' => true, 'w+b' => true, 'r+b' => true,
  1960. 'x+b' => true, 'c+b' => true, 'w+t' => true, 'r+t' => true,
  1961. 'x+t' => true, 'c+t' => true, 'a' => true, 'a+' => true,
  1962. ],
  1963. ];
  1964. private function __construct()
  1965. {
  1966. }
  1967. /**
  1968. * Creates a new PSR-7 stream.
  1969. *
  1970. * @param string|resource|StreamInterface $body
  1971. *
  1972. * @return StreamInterface
  1973. *
  1974. * @throws \InvalidArgumentException
  1975. */
  1976. public static function create($body = ''): StreamInterface
  1977. {
  1978. if ($body instanceof StreamInterface) {
  1979. return $body;
  1980. }
  1981. if (\is_string($body)) {
  1982. $resource = \fopen('php://temp', 'rw+');
  1983. \fwrite($resource, $body);
  1984. $body = $resource;
  1985. }
  1986. if (\is_resource($body)) {
  1987. $new = new self();
  1988. $new->stream = $body;
  1989. $meta = \stream_get_meta_data($new->stream);
  1990. $new->seekable = $meta['seekable'] && 0 === \fseek($new->stream, 0, \SEEK_CUR);
  1991. $new->readable = isset(self::READ_WRITE_HASH['read'][$meta['mode']]);
  1992. $new->writable = isset(self::READ_WRITE_HASH['write'][$meta['mode']]);
  1993. $new->uri = $new->getMetadata('uri');
  1994. return $new;
  1995. }
  1996. throw new \InvalidArgumentException('First argument to Stream::create() must be a string, resource or StreamInterface.');
  1997. }
  1998. /**
  1999. * Closes the stream when the destructed.
  2000. */
  2001. public function __destruct()
  2002. {
  2003. $this->close();
  2004. }
  2005. public function __toString(): string
  2006. {
  2007. try {
  2008. if ($this->isSeekable()) {
  2009. $this->seek(0);
  2010. }
  2011. return $this->getContents();
  2012. } catch (\Exception $e) {
  2013. return '';
  2014. }
  2015. }
  2016. public function close() /*:void*/
  2017. {
  2018. if (isset($this->stream)) {
  2019. if (\is_resource($this->stream)) {
  2020. \fclose($this->stream);
  2021. }
  2022. $this->detach();
  2023. }
  2024. }
  2025. public function detach()
  2026. {
  2027. if (!isset($this->stream)) {
  2028. return null;
  2029. }
  2030. $result = $this->stream;
  2031. unset($this->stream);
  2032. $this->size = $this->uri = null;
  2033. $this->readable = $this->writable = $this->seekable = false;
  2034. return $result;
  2035. }
  2036. public function getSize() /*:?int*/
  2037. {
  2038. if (null !== $this->size) {
  2039. return $this->size;
  2040. }
  2041. if (!isset($this->stream)) {
  2042. return null;
  2043. }
  2044. // Clear the stat cache if the stream has a URI
  2045. if ($this->uri) {
  2046. \clearstatcache(true, $this->uri);
  2047. }
  2048. $stats = \fstat($this->stream);
  2049. if (isset($stats['size'])) {
  2050. $this->size = $stats['size'];
  2051. return $this->size;
  2052. }
  2053. return null;
  2054. }
  2055. public function tell(): int
  2056. {
  2057. if (false === $result = \ftell($this->stream)) {
  2058. throw new \RuntimeException('Unable to determine stream position');
  2059. }
  2060. return $result;
  2061. }
  2062. public function eof(): bool
  2063. {
  2064. return !$this->stream || \feof($this->stream);
  2065. }
  2066. public function isSeekable(): bool
  2067. {
  2068. return $this->seekable;
  2069. }
  2070. public function seek($offset, $whence = \SEEK_SET) /*:void*/
  2071. {
  2072. if (!$this->seekable) {
  2073. throw new \RuntimeException('Stream is not seekable');
  2074. }
  2075. if (-1 === \fseek($this->stream, $offset, $whence)) {
  2076. throw new \RuntimeException('Unable to seek to stream position ' . $offset . ' with whence ' . \var_export($whence, true));
  2077. }
  2078. }
  2079. public function rewind() /*:void*/
  2080. {
  2081. $this->seek(0);
  2082. }
  2083. public function isWritable(): bool
  2084. {
  2085. return $this->writable;
  2086. }
  2087. public function write($string): int
  2088. {
  2089. if (!$this->writable) {
  2090. throw new \RuntimeException('Cannot write to a non-writable stream');
  2091. }
  2092. // We can't know the size after writing anything
  2093. $this->size = null;
  2094. if (false === $result = \fwrite($this->stream, $string)) {
  2095. throw new \RuntimeException('Unable to write to stream');
  2096. }
  2097. return $result;
  2098. }
  2099. public function isReadable(): bool
  2100. {
  2101. return $this->readable;
  2102. }
  2103. public function read($length): string
  2104. {
  2105. if (!$this->readable) {
  2106. throw new \RuntimeException('Cannot read from non-readable stream');
  2107. }
  2108. return \fread($this->stream, $length);
  2109. }
  2110. public function getContents(): string
  2111. {
  2112. if (!isset($this->stream)) {
  2113. throw new \RuntimeException('Unable to read stream contents');
  2114. }
  2115. if (false === $contents = \stream_get_contents($this->stream)) {
  2116. throw new \RuntimeException('Unable to read stream contents');
  2117. }
  2118. return $contents;
  2119. }
  2120. public function getMetadata($key = null)
  2121. {
  2122. if (!isset($this->stream)) {
  2123. return $key ? null : [];
  2124. }
  2125. $meta = \stream_get_meta_data($this->stream);
  2126. if (null === $key) {
  2127. return $meta;
  2128. }
  2129. return $meta[$key] ?? null;
  2130. }
  2131. }
  2132. }
  2133. // file: vendor/nyholm/psr7/src/UploadedFile.php
  2134. namespace Nyholm\Psr7 {
  2135. use Psr\Http\Message\{StreamInterface, UploadedFileInterface};
  2136. /**
  2137. * @author Michael Dowling and contributors to guzzlehttp/psr7
  2138. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  2139. * @author Martijn van der Ven <martijn@vanderven.se>
  2140. */
  2141. final class UploadedFile implements UploadedFileInterface
  2142. {
  2143. /** @var array */
  2144. /*private*/ const ERRORS = [
  2145. \UPLOAD_ERR_OK => 1,
  2146. \UPLOAD_ERR_INI_SIZE => 1,
  2147. \UPLOAD_ERR_FORM_SIZE => 1,
  2148. \UPLOAD_ERR_PARTIAL => 1,
  2149. \UPLOAD_ERR_NO_FILE => 1,
  2150. \UPLOAD_ERR_NO_TMP_DIR => 1,
  2151. \UPLOAD_ERR_CANT_WRITE => 1,
  2152. \UPLOAD_ERR_EXTENSION => 1,
  2153. ];
  2154. /** @var string */
  2155. private $clientFilename;
  2156. /** @var string */
  2157. private $clientMediaType;
  2158. /** @var int */
  2159. private $error;
  2160. /** @var string|null */
  2161. private $file;
  2162. /** @var bool */
  2163. private $moved = false;
  2164. /** @var int */
  2165. private $size;
  2166. /** @var StreamInterface|null */
  2167. private $stream;
  2168. /**
  2169. * @param StreamInterface|string|resource $streamOrFile
  2170. * @param int $size
  2171. * @param int $errorStatus
  2172. * @param string|null $clientFilename
  2173. * @param string|null $clientMediaType
  2174. */
  2175. public function __construct($streamOrFile, $size, $errorStatus, $clientFilename = null, $clientMediaType = null)
  2176. {
  2177. if (false === \is_int($errorStatus) || !isset(self::ERRORS[$errorStatus])) {
  2178. throw new \InvalidArgumentException('Upload file error status must be an integer value and one of the "UPLOAD_ERR_*" constants.');
  2179. }
  2180. if (false === \is_int($size)) {
  2181. throw new \InvalidArgumentException('Upload file size must be an integer');
  2182. }
  2183. if (null !== $clientFilename && !\is_string($clientFilename)) {
  2184. throw new \InvalidArgumentException('Upload file client filename must be a string or null');
  2185. }
  2186. if (null !== $clientMediaType && !\is_string($clientMediaType)) {
  2187. throw new \InvalidArgumentException('Upload file client media type must be a string or null');
  2188. }
  2189. $this->error = $errorStatus;
  2190. $this->size = $size;
  2191. $this->clientFilename = $clientFilename;
  2192. $this->clientMediaType = $clientMediaType;
  2193. if (\UPLOAD_ERR_OK === $this->error) {
  2194. // Depending on the value set file or stream variable.
  2195. if (\is_string($streamOrFile)) {
  2196. $this->file = $streamOrFile;
  2197. } elseif (\is_resource($streamOrFile)) {
  2198. $this->stream = Stream::create($streamOrFile);
  2199. } elseif ($streamOrFile instanceof StreamInterface) {
  2200. $this->stream = $streamOrFile;
  2201. } else {
  2202. throw new \InvalidArgumentException('Invalid stream or file provided for UploadedFile');
  2203. }
  2204. }
  2205. }
  2206. /**
  2207. * @throws \RuntimeException if is moved or not ok
  2208. */
  2209. private function validateActive() /*:void*/
  2210. {
  2211. if (\UPLOAD_ERR_OK !== $this->error) {
  2212. throw new \RuntimeException('Cannot retrieve stream due to upload error');
  2213. }
  2214. if ($this->moved) {
  2215. throw new \RuntimeException('Cannot retrieve stream after it has already been moved');
  2216. }
  2217. }
  2218. public function getStream(): StreamInterface
  2219. {
  2220. $this->validateActive();
  2221. if ($this->stream instanceof StreamInterface) {
  2222. return $this->stream;
  2223. }
  2224. $resource = \fopen($this->file, 'r');
  2225. return Stream::create($resource);
  2226. }
  2227. public function moveTo($targetPath) /*:void*/
  2228. {
  2229. $this->validateActive();
  2230. if (!\is_string($targetPath) || '' === $targetPath) {
  2231. throw new \InvalidArgumentException('Invalid path provided for move operation; must be a non-empty string');
  2232. }
  2233. if (null !== $this->file) {
  2234. $this->moved = 'cli' === \PHP_SAPI ? \rename($this->file, $targetPath) : \move_uploaded_file($this->file, $targetPath);
  2235. } else {
  2236. $stream = $this->getStream();
  2237. if ($stream->isSeekable()) {
  2238. $stream->rewind();
  2239. }
  2240. // Copy the contents of a stream into another stream until end-of-file.
  2241. $dest = Stream::create(\fopen($targetPath, 'w'));
  2242. while (!$stream->eof()) {
  2243. if (!$dest->write($stream->read(1048576))) {
  2244. break;
  2245. }
  2246. }
  2247. $this->moved = true;
  2248. }
  2249. if (false === $this->moved) {
  2250. throw new \RuntimeException(\sprintf('Uploaded file could not be moved to %s', $targetPath));
  2251. }
  2252. }
  2253. public function getSize(): int
  2254. {
  2255. return $this->size;
  2256. }
  2257. public function getError(): int
  2258. {
  2259. return $this->error;
  2260. }
  2261. public function getClientFilename() /*:?string*/
  2262. {
  2263. return $this->clientFilename;
  2264. }
  2265. public function getClientMediaType() /*:?string*/
  2266. {
  2267. return $this->clientMediaType;
  2268. }
  2269. }
  2270. }
  2271. // file: vendor/nyholm/psr7/src/Uri.php
  2272. namespace Nyholm\Psr7 {
  2273. use Psr\Http\Message\UriInterface;
  2274. /**
  2275. * PSR-7 URI implementation.
  2276. *
  2277. * @author Michael Dowling
  2278. * @author Tobias Schultze
  2279. * @author Matthew Weier O'Phinney
  2280. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  2281. * @author Martijn van der Ven <martijn@vanderven.se>
  2282. */
  2283. final class Uri implements UriInterface
  2284. {
  2285. /*private*/ const SCHEMES = ['http' => 80, 'https' => 443];
  2286. /*private*/ const CHAR_UNRESERVED = 'a-zA-Z0-9_\-\.~';
  2287. /*private*/ const CHAR_SUB_DELIMS = '!\$&\'\(\)\*\+,;=';
  2288. /** @var string Uri scheme. */
  2289. private $scheme = '';
  2290. /** @var string Uri user info. */
  2291. private $userInfo = '';
  2292. /** @var string Uri host. */
  2293. private $host = '';
  2294. /** @var int|null Uri port. */
  2295. private $port;
  2296. /** @var string Uri path. */
  2297. private $path = '';
  2298. /** @var string Uri query string. */
  2299. private $query = '';
  2300. /** @var string Uri fragment. */
  2301. private $fragment = '';
  2302. public function __construct(string $uri = '')
  2303. {
  2304. if ('' !== $uri) {
  2305. if (false === $parts = \parse_url($uri)) {
  2306. throw new \InvalidArgumentException("Unable to parse URI: $uri");
  2307. }
  2308. // Apply parse_url parts to a URI.
  2309. $this->scheme = isset($parts['scheme']) ? \strtolower($parts['scheme']) : '';
  2310. $this->userInfo = $parts['user'] ?? '';
  2311. $this->host = isset($parts['host']) ? \strtolower($parts['host']) : '';
  2312. $this->port = isset($parts['port']) ? $this->filterPort($parts['port']) : null;
  2313. $this->path = isset($parts['path']) ? $this->filterPath($parts['path']) : '';
  2314. $this->query = isset($parts['query']) ? $this->filterQueryAndFragment($parts['query']) : '';
  2315. $this->fragment = isset($parts['fragment']) ? $this->filterQueryAndFragment($parts['fragment']) : '';
  2316. if (isset($parts['pass'])) {
  2317. $this->userInfo .= ':' . $parts['pass'];
  2318. }
  2319. }
  2320. }
  2321. public function __toString(): string
  2322. {
  2323. return self::createUriString($this->scheme, $this->getAuthority(), $this->path, $this->query, $this->fragment);
  2324. }
  2325. public function getScheme(): string
  2326. {
  2327. return $this->scheme;
  2328. }
  2329. public function getAuthority(): string
  2330. {
  2331. if ('' === $this->host) {
  2332. return '';
  2333. }
  2334. $authority = $this->host;
  2335. if ('' !== $this->userInfo) {
  2336. $authority = $this->userInfo . '@' . $authority;
  2337. }
  2338. if (null !== $this->port) {
  2339. $authority .= ':' . $this->port;
  2340. }
  2341. return $authority;
  2342. }
  2343. public function getUserInfo(): string
  2344. {
  2345. return $this->userInfo;
  2346. }
  2347. public function getHost(): string
  2348. {
  2349. return $this->host;
  2350. }
  2351. public function getPort() /*:?int*/
  2352. {
  2353. return $this->port;
  2354. }
  2355. public function getPath(): string
  2356. {
  2357. return $this->path;
  2358. }
  2359. public function getQuery(): string
  2360. {
  2361. return $this->query;
  2362. }
  2363. public function getFragment(): string
  2364. {
  2365. return $this->fragment;
  2366. }
  2367. public function withScheme($scheme): self
  2368. {
  2369. if (!\is_string($scheme)) {
  2370. throw new \InvalidArgumentException('Scheme must be a string');
  2371. }
  2372. if ($this->scheme === $scheme = \strtolower($scheme)) {
  2373. return $this;
  2374. }
  2375. $new = clone $this;
  2376. $new->scheme = $scheme;
  2377. $new->port = $new->filterPort($new->port);
  2378. return $new;
  2379. }
  2380. public function withUserInfo($user, $password = null): self
  2381. {
  2382. $info = $user;
  2383. if (null !== $password && '' !== $password) {
  2384. $info .= ':' . $password;
  2385. }
  2386. if ($this->userInfo === $info) {
  2387. return $this;
  2388. }
  2389. $new = clone $this;
  2390. $new->userInfo = $info;
  2391. return $new;
  2392. }
  2393. public function withHost($host): self
  2394. {
  2395. if (!\is_string($host)) {
  2396. throw new \InvalidArgumentException('Host must be a string');
  2397. }
  2398. if ($this->host === $host = \strtolower($host)) {
  2399. return $this;
  2400. }
  2401. $new = clone $this;
  2402. $new->host = $host;
  2403. return $new;
  2404. }
  2405. public function withPort($port): self
  2406. {
  2407. if ($this->port === $port = $this->filterPort($port)) {
  2408. return $this;
  2409. }
  2410. $new = clone $this;
  2411. $new->port = $port;
  2412. return $new;
  2413. }
  2414. public function withPath($path): self
  2415. {
  2416. if ($this->path === $path = $this->filterPath($path)) {
  2417. return $this;
  2418. }
  2419. $new = clone $this;
  2420. $new->path = $path;
  2421. return $new;
  2422. }
  2423. public function withQuery($query): self
  2424. {
  2425. if ($this->query === $query = $this->filterQueryAndFragment($query)) {
  2426. return $this;
  2427. }
  2428. $new = clone $this;
  2429. $new->query = $query;
  2430. return $new;
  2431. }
  2432. public function withFragment($fragment): self
  2433. {
  2434. if ($this->fragment === $fragment = $this->filterQueryAndFragment($fragment)) {
  2435. return $this;
  2436. }
  2437. $new = clone $this;
  2438. $new->fragment = $fragment;
  2439. return $new;
  2440. }
  2441. /**
  2442. * Create a URI string from its various parts.
  2443. */
  2444. private static function createUriString(string $scheme, string $authority, string $path, string $query, string $fragment): string
  2445. {
  2446. $uri = '';
  2447. if ('' !== $scheme) {
  2448. $uri .= $scheme . ':';
  2449. }
  2450. if ('' !== $authority) {
  2451. $uri .= '//' . $authority;
  2452. }
  2453. if ('' !== $path) {
  2454. if ('/' !== $path[0]) {
  2455. if ('' !== $authority) {
  2456. // If the path is rootless and an authority is present, the path MUST be prefixed by "/"
  2457. $path = '/' . $path;
  2458. }
  2459. } elseif (isset($path[1]) && '/' === $path[1]) {
  2460. if ('' === $authority) {
  2461. // If the path is starting with more than one "/" and no authority is present, the
  2462. // starting slashes MUST be reduced to one.
  2463. $path = '/' . \ltrim($path, '/');
  2464. }
  2465. }
  2466. $uri .= $path;
  2467. }
  2468. if ('' !== $query) {
  2469. $uri .= '?' . $query;
  2470. }
  2471. if ('' !== $fragment) {
  2472. $uri .= '#' . $fragment;
  2473. }
  2474. return $uri;
  2475. }
  2476. /**
  2477. * Is a given port non-standard for the current scheme?
  2478. */
  2479. private static function isNonStandardPort(string $scheme, int $port): bool
  2480. {
  2481. return !isset(self::SCHEMES[$scheme]) || $port !== self::SCHEMES[$scheme];
  2482. }
  2483. private function filterPort($port) /*:?int*/
  2484. {
  2485. if (null === $port) {
  2486. return null;
  2487. }
  2488. $port = (int) $port;
  2489. if (0 > $port || 0xffff < $port) {
  2490. throw new \InvalidArgumentException(\sprintf('Invalid port: %d. Must be between 0 and 65535', $port));
  2491. }
  2492. return self::isNonStandardPort($this->scheme, $port) ? $port : null;
  2493. }
  2494. private function filterPath($path): string
  2495. {
  2496. if (!\is_string($path)) {
  2497. throw new \InvalidArgumentException('Path must be a string');
  2498. }
  2499. return \preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/]++|%(?![A-Fa-f0-9]{2}))/', [__CLASS__, 'rawurlencodeMatchZero'], $path);
  2500. }
  2501. private function filterQueryAndFragment($str): string
  2502. {
  2503. if (!\is_string($str)) {
  2504. throw new \InvalidArgumentException('Query and fragment must be a string');
  2505. }
  2506. return \preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/\?]++|%(?![A-Fa-f0-9]{2}))/', [__CLASS__, 'rawurlencodeMatchZero'], $str);
  2507. }
  2508. private static function rawurlencodeMatchZero(array $match): string
  2509. {
  2510. return \rawurlencode($match[0]);
  2511. }
  2512. }
  2513. }
  2514. // file: vendor/nyholm/psr7-server/src/ServerRequestCreator.php
  2515. namespace Nyholm\Psr7Server {
  2516. use Psr\Http\Message\ServerRequestFactoryInterface;
  2517. use Psr\Http\Message\ServerRequestInterface;
  2518. use Psr\Http\Message\StreamFactoryInterface;
  2519. use Psr\Http\Message\StreamInterface;
  2520. use Psr\Http\Message\UploadedFileFactoryInterface;
  2521. use Psr\Http\Message\UploadedFileInterface;
  2522. use Psr\Http\Message\UriFactoryInterface;
  2523. use Psr\Http\Message\UriInterface;
  2524. /**
  2525. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  2526. * @author Martijn van der Ven <martijn@vanderven.se>
  2527. */
  2528. final class ServerRequestCreator implements ServerRequestCreatorInterface
  2529. {
  2530. private $serverRequestFactory;
  2531. private $uriFactory;
  2532. private $uploadedFileFactory;
  2533. private $streamFactory;
  2534. public function __construct(
  2535. ServerRequestFactoryInterface $serverRequestFactory,
  2536. UriFactoryInterface $uriFactory,
  2537. UploadedFileFactoryInterface $uploadedFileFactory,
  2538. StreamFactoryInterface $streamFactory
  2539. ) {
  2540. $this->serverRequestFactory = $serverRequestFactory;
  2541. $this->uriFactory = $uriFactory;
  2542. $this->uploadedFileFactory = $uploadedFileFactory;
  2543. $this->streamFactory = $streamFactory;
  2544. }
  2545. /**
  2546. * {@inheritdoc}
  2547. */
  2548. public function fromGlobals(): ServerRequestInterface
  2549. {
  2550. $server = $_SERVER;
  2551. if (false === isset($server['REQUEST_METHOD'])) {
  2552. $server['REQUEST_METHOD'] = 'GET';
  2553. }
  2554. $headers = \function_exists('getallheaders') ? getallheaders() : static::getHeadersFromServer($_SERVER);
  2555. return $this->fromArrays($server, $headers, $_COOKIE, $_GET, $_POST, $_FILES, \fopen('php://input', 'r') ?: null);
  2556. }
  2557. /**
  2558. * {@inheritdoc}
  2559. */
  2560. public function fromArrays(array $server, array $headers = [], array $cookie = [], array $get = [], array $post = [], array $files = [], $body = null): ServerRequestInterface
  2561. {
  2562. $method = $this->getMethodFromEnv($server);
  2563. $uri = $this->getUriFromEnvWithHTTP($server);
  2564. $protocol = isset($server['SERVER_PROTOCOL']) ? \str_replace('HTTP/', '', $server['SERVER_PROTOCOL']) : '1.1';
  2565. $serverRequest = $this->serverRequestFactory->createServerRequest($method, $uri, $server);
  2566. foreach ($headers as $name => $value) {
  2567. $serverRequest = $serverRequest->withAddedHeader($name, $value);
  2568. }
  2569. $serverRequest = $serverRequest
  2570. ->withProtocolVersion($protocol)
  2571. ->withCookieParams($cookie)
  2572. ->withQueryParams($get)
  2573. ->withParsedBody($post)
  2574. ->withUploadedFiles($this->normalizeFiles($files));
  2575. if (null === $body) {
  2576. return $serverRequest;
  2577. }
  2578. if (\is_resource($body)) {
  2579. $body = $this->streamFactory->createStreamFromResource($body);
  2580. } elseif (\is_string($body)) {
  2581. $body = $this->streamFactory->createStream($body);
  2582. } elseif (!$body instanceof StreamInterface) {
  2583. throw new \InvalidArgumentException('The $body parameter to ServerRequestCreator::fromArrays must be string, resource or StreamInterface');
  2584. }
  2585. return $serverRequest->withBody($body);
  2586. }
  2587. /**
  2588. * Implementation from Zend\Diactoros\marshalHeadersFromSapi().
  2589. */
  2590. public static function getHeadersFromServer(array $server): array
  2591. {
  2592. $headers = [];
  2593. foreach ($server as $key => $value) {
  2594. // Apache prefixes environment variables with REDIRECT_
  2595. // if they are added by rewrite rules
  2596. if (0 === \strpos($key, 'REDIRECT_')) {
  2597. $key = \substr($key, 9);
  2598. // We will not overwrite existing variables with the
  2599. // prefixed versions, though
  2600. if (\array_key_exists($key, $server)) {
  2601. continue;
  2602. }
  2603. }
  2604. if ($value && 0 === \strpos($key, 'HTTP_')) {
  2605. $name = \strtr(\strtolower(\substr($key, 5)), '_', '-');
  2606. $headers[$name] = $value;
  2607. continue;
  2608. }
  2609. if ($value && 0 === \strpos($key, 'CONTENT_')) {
  2610. $name = 'content-'.\strtolower(\substr($key, 8));
  2611. $headers[$name] = $value;
  2612. continue;
  2613. }
  2614. }
  2615. return $headers;
  2616. }
  2617. private function getMethodFromEnv(array $environment): string
  2618. {
  2619. if (false === isset($environment['REQUEST_METHOD'])) {
  2620. throw new \InvalidArgumentException('Cannot determine HTTP method');
  2621. }
  2622. return $environment['REQUEST_METHOD'];
  2623. }
  2624. private function getUriFromEnvWithHTTP(array $environment): UriInterface
  2625. {
  2626. $uri = $this->createUriFromArray($environment);
  2627. if (empty($uri->getScheme())) {
  2628. $uri = $uri->withScheme('http');
  2629. }
  2630. return $uri;
  2631. }
  2632. /**
  2633. * Return an UploadedFile instance array.
  2634. *
  2635. * @param array $files A array which respect $_FILES structure
  2636. *
  2637. * @return UploadedFileInterface[]
  2638. *
  2639. * @throws \InvalidArgumentException for unrecognized values
  2640. */
  2641. private function normalizeFiles(array $files): array
  2642. {
  2643. $normalized = [];
  2644. foreach ($files as $key => $value) {
  2645. if ($value instanceof UploadedFileInterface) {
  2646. $normalized[$key] = $value;
  2647. } elseif (\is_array($value) && isset($value['tmp_name'])) {
  2648. $normalized[$key] = $this->createUploadedFileFromSpec($value);
  2649. } elseif (\is_array($value)) {
  2650. $normalized[$key] = $this->normalizeFiles($value);
  2651. } else {
  2652. throw new \InvalidArgumentException('Invalid value in files specification');
  2653. }
  2654. }
  2655. return $normalized;
  2656. }
  2657. /**
  2658. * Create and return an UploadedFile instance from a $_FILES specification.
  2659. *
  2660. * If the specification represents an array of values, this method will
  2661. * delegate to normalizeNestedFileSpec() and return that return value.
  2662. *
  2663. * @param array $value $_FILES struct
  2664. *
  2665. * @return array|UploadedFileInterface
  2666. */
  2667. private function createUploadedFileFromSpec(array $value)
  2668. {
  2669. if (\is_array($value['tmp_name'])) {
  2670. return $this->normalizeNestedFileSpec($value);
  2671. }
  2672. try {
  2673. $stream = $this->streamFactory->createStreamFromFile($value['tmp_name']);
  2674. } catch (\RuntimeException $e) {
  2675. $stream = $this->streamFactory->createStream();
  2676. }
  2677. return $this->uploadedFileFactory->createUploadedFile(
  2678. $stream,
  2679. (int) $value['size'],
  2680. (int) $value['error'],
  2681. $value['name'],
  2682. $value['type']
  2683. );
  2684. }
  2685. /**
  2686. * Normalize an array of file specifications.
  2687. *
  2688. * Loops through all nested files and returns a normalized array of
  2689. * UploadedFileInterface instances.
  2690. *
  2691. * @param array $files
  2692. *
  2693. * @return UploadedFileInterface[]
  2694. */
  2695. private function normalizeNestedFileSpec(array $files = []): array
  2696. {
  2697. $normalizedFiles = [];
  2698. foreach (\array_keys($files['tmp_name']) as $key) {
  2699. $spec = [
  2700. 'tmp_name' => $files['tmp_name'][$key],
  2701. 'size' => $files['size'][$key],
  2702. 'error' => $files['error'][$key],
  2703. 'name' => $files['name'][$key],
  2704. 'type' => $files['type'][$key],
  2705. ];
  2706. $normalizedFiles[$key] = $this->createUploadedFileFromSpec($spec);
  2707. }
  2708. return $normalizedFiles;
  2709. }
  2710. /**
  2711. * Create a new uri from server variable.
  2712. *
  2713. * @param array $server typically $_SERVER or similar structure
  2714. */
  2715. private function createUriFromArray(array $server): UriInterface
  2716. {
  2717. $uri = $this->uriFactory->createUri('');
  2718. if (isset($server['HTTP_X_FORWARDED_PROTO'])) {
  2719. $uri = $uri->withScheme($server['HTTP_X_FORWARDED_PROTO']);
  2720. } else {
  2721. if (isset($server['REQUEST_SCHEME'])) {
  2722. $uri = $uri->withScheme($server['REQUEST_SCHEME']);
  2723. } elseif (isset($server['HTTPS'])) {
  2724. $uri = $uri->withScheme('on' === $server['HTTPS'] ? 'https' : 'http');
  2725. }
  2726. if (isset($server['SERVER_PORT'])) {
  2727. $uri = $uri->withPort($server['SERVER_PORT']);
  2728. }
  2729. }
  2730. if (isset($server['HTTP_HOST'])) {
  2731. if (1 === \preg_match('/^(.+)\:(\d+)$/', $server['HTTP_HOST'], $matches)) {
  2732. $uri = $uri->withHost($matches[1])->withPort($matches[2]);
  2733. } else {
  2734. $uri = $uri->withHost($server['HTTP_HOST']);
  2735. }
  2736. } elseif (isset($server['SERVER_NAME'])) {
  2737. $uri = $uri->withHost($server['SERVER_NAME']);
  2738. }
  2739. if (isset($server['REQUEST_URI'])) {
  2740. $uri = $uri->withPath(\current(\explode('?', $server['REQUEST_URI'])));
  2741. }
  2742. if (isset($server['QUERY_STRING'])) {
  2743. $uri = $uri->withQuery($server['QUERY_STRING']);
  2744. }
  2745. return $uri;
  2746. }
  2747. }
  2748. }
  2749. // file: vendor/nyholm/psr7-server/src/ServerRequestCreatorInterface.php
  2750. namespace Nyholm\Psr7Server {
  2751. use Psr\Http\Message\ServerRequestInterface;
  2752. use Psr\Http\Message\StreamInterface;
  2753. /**
  2754. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  2755. * @author Martijn van der Ven <martijn@vanderven.se>
  2756. */
  2757. interface ServerRequestCreatorInterface
  2758. {
  2759. /**
  2760. * Create a new server request from the current environment variables.
  2761. * Defaults to a GET request to minimise the risk of an \InvalidArgumentException.
  2762. * Includes the current request headers as supplied by the server through `getallheaders()`.
  2763. * If `getallheaders()` is unavailable on the current server it will fallback to its own `getHeadersFromServer()` method.
  2764. * Defaults to php://input for the request body.
  2765. *
  2766. * @throws \InvalidArgumentException if no valid method or URI can be determined
  2767. */
  2768. public function fromGlobals(): ServerRequestInterface;
  2769. /**
  2770. * Create a new server request from a set of arrays.
  2771. *
  2772. * @param array $server typically $_SERVER or similar structure
  2773. * @param array $headers typically the output of getallheaders() or similar structure
  2774. * @param array $cookie typically $_COOKIE or similar structure
  2775. * @param array $get typically $_GET or similar structure
  2776. * @param array $post typically $_POST or similar structure
  2777. * @param array $files typically $_FILES or similar structure
  2778. * @param StreamInterface|resource|string|null $body Typically stdIn
  2779. *
  2780. * @throws \InvalidArgumentException if no valid method or URI can be determined
  2781. */
  2782. public function fromArrays(
  2783. array $server,
  2784. array $headers = [],
  2785. array $cookie = [],
  2786. array $get = [],
  2787. array $post = [],
  2788. array $files = [],
  2789. $body = null
  2790. ): ServerRequestInterface;
  2791. /**
  2792. * Get parsed headers from ($_SERVER) array.
  2793. *
  2794. * @param array $server typically $_SERVER or similar structure
  2795. *
  2796. * @return array
  2797. */
  2798. public static function getHeadersFromServer(array $server): array;
  2799. }
  2800. }
  2801. // file: src/Tqdev/PhpCrudApi/Cache/Cache.php
  2802. namespace Tqdev\PhpCrudApi\Cache {
  2803. interface Cache
  2804. {
  2805. public function set(string $key, string $value, int $ttl = 0): bool;
  2806. public function get(string $key): string;
  2807. public function clear(): bool;
  2808. }
  2809. }
  2810. // file: src/Tqdev/PhpCrudApi/Cache/CacheFactory.php
  2811. namespace Tqdev\PhpCrudApi\Cache {
  2812. class CacheFactory
  2813. {
  2814. public static function create(string $type, string $prefix, string $config): Cache
  2815. {
  2816. switch ($type) {
  2817. case 'TempFile':
  2818. $cache = new TempFileCache($prefix, $config);
  2819. break;
  2820. case 'Redis':
  2821. $cache = new RedisCache($prefix, $config);
  2822. break;
  2823. case 'Memcache':
  2824. $cache = new MemcacheCache($prefix, $config);
  2825. break;
  2826. case 'Memcached':
  2827. $cache = new MemcachedCache($prefix, $config);
  2828. break;
  2829. default:
  2830. $cache = new NoCache();
  2831. }
  2832. return $cache;
  2833. }
  2834. }
  2835. }
  2836. // file: src/Tqdev/PhpCrudApi/Cache/MemcacheCache.php
  2837. namespace Tqdev\PhpCrudApi\Cache {
  2838. class MemcacheCache implements Cache
  2839. {
  2840. protected $prefix;
  2841. protected $memcache;
  2842. public function __construct(string $prefix, string $config)
  2843. {
  2844. $this->prefix = $prefix;
  2845. if ($config == '') {
  2846. $address = 'localhost';
  2847. $port = 11211;
  2848. } elseif (strpos($config, ':') === false) {
  2849. $address = $config;
  2850. $port = 11211;
  2851. } else {
  2852. list($address, $port) = explode(':', $config);
  2853. }
  2854. $this->memcache = $this->create();
  2855. $this->memcache->addServer($address, $port);
  2856. }
  2857. protected function create() /*: \Memcache*/
  2858. {
  2859. return new \Memcache();
  2860. }
  2861. public function set(string $key, string $value, int $ttl = 0): bool
  2862. {
  2863. return $this->memcache->set($this->prefix . $key, $value, 0, $ttl);
  2864. }
  2865. public function get(string $key): string
  2866. {
  2867. return $this->memcache->get($this->prefix . $key) ?: '';
  2868. }
  2869. public function clear(): bool
  2870. {
  2871. return $this->memcache->flush();
  2872. }
  2873. }
  2874. }
  2875. // file: src/Tqdev/PhpCrudApi/Cache/MemcachedCache.php
  2876. namespace Tqdev\PhpCrudApi\Cache {
  2877. class MemcachedCache extends MemcacheCache
  2878. {
  2879. protected function create() /*: \Memcached*/
  2880. {
  2881. return new \Memcached();
  2882. }
  2883. public function set(string $key, string $value, int $ttl = 0): bool
  2884. {
  2885. return $this->memcache->set($this->prefix . $key, $value, $ttl);
  2886. }
  2887. }
  2888. }
  2889. // file: src/Tqdev/PhpCrudApi/Cache/NoCache.php
  2890. namespace Tqdev\PhpCrudApi\Cache {
  2891. class NoCache implements Cache
  2892. {
  2893. public function __construct()
  2894. {
  2895. }
  2896. public function set(string $key, string $value, int $ttl = 0): bool
  2897. {
  2898. return true;
  2899. }
  2900. public function get(string $key): string
  2901. {
  2902. return '';
  2903. }
  2904. public function clear(): bool
  2905. {
  2906. return true;
  2907. }
  2908. }
  2909. }
  2910. // file: src/Tqdev/PhpCrudApi/Cache/RedisCache.php
  2911. namespace Tqdev\PhpCrudApi\Cache {
  2912. class RedisCache implements Cache
  2913. {
  2914. protected $prefix;
  2915. protected $redis;
  2916. public function __construct(string $prefix, string $config)
  2917. {
  2918. $this->prefix = $prefix;
  2919. if ($config == '') {
  2920. $config = '127.0.0.1';
  2921. }
  2922. $params = explode(':', $config, 6);
  2923. if (isset($params[3])) {
  2924. $params[3] = null;
  2925. }
  2926. $this->redis = new \Redis();
  2927. call_user_func_array(array($this->redis, 'pconnect'), $params);
  2928. }
  2929. public function set(string $key, string $value, int $ttl = 0): bool
  2930. {
  2931. return $this->redis->set($this->prefix . $key, $value, $ttl);
  2932. }
  2933. public function get(string $key): string
  2934. {
  2935. return $this->redis->get($this->prefix . $key) ?: '';
  2936. }
  2937. public function clear(): bool
  2938. {
  2939. return $this->redis->flushDb();
  2940. }
  2941. }
  2942. }
  2943. // file: src/Tqdev/PhpCrudApi/Cache/TempFileCache.php
  2944. namespace Tqdev\PhpCrudApi\Cache {
  2945. class TempFileCache implements Cache
  2946. {
  2947. const SUFFIX = 'cache';
  2948. private $path;
  2949. private $segments;
  2950. public function __construct(string $prefix, string $config)
  2951. {
  2952. $this->segments = [];
  2953. $s = DIRECTORY_SEPARATOR;
  2954. $ps = PATH_SEPARATOR;
  2955. if ($config == '') {
  2956. $this->path = sys_get_temp_dir() . $s . $prefix . self::SUFFIX;
  2957. } elseif (strpos($config, $ps) === false) {
  2958. $this->path = $config;
  2959. } else {
  2960. list($path, $segments) = explode($ps, $config);
  2961. $this->path = $path;
  2962. $this->segments = explode(',', $segments);
  2963. }
  2964. if (file_exists($this->path) && is_dir($this->path)) {
  2965. $this->clean($this->path, array_filter($this->segments), strlen(md5('')), false);
  2966. }
  2967. }
  2968. private function getFileName(string $key): string
  2969. {
  2970. $s = DIRECTORY_SEPARATOR;
  2971. $md5 = md5($key);
  2972. $filename = rtrim($this->path, $s) . $s;
  2973. $i = 0;
  2974. foreach ($this->segments as $segment) {
  2975. $filename .= substr($md5, $i, $segment) . $s;
  2976. $i += $segment;
  2977. }
  2978. $filename .= substr($md5, $i);
  2979. return $filename;
  2980. }
  2981. public function set(string $key, string $value, int $ttl = 0): bool
  2982. {
  2983. $filename = $this->getFileName($key);
  2984. $dirname = dirname($filename);
  2985. if (!file_exists($dirname)) {
  2986. if (!mkdir($dirname, 0755, true)) {
  2987. return false;
  2988. }
  2989. }
  2990. $string = $ttl . '|' . $value;
  2991. return $this->filePutContents($filename, $string) !== false;
  2992. }
  2993. private function filePutContents($filename, $string)
  2994. {
  2995. return file_put_contents($filename, $string, LOCK_EX);
  2996. }
  2997. private function fileGetContents($filename)
  2998. {
  2999. $file = fopen($filename, 'rb');
  3000. if ($file === false) {
  3001. return false;
  3002. }
  3003. $lock = flock($file, LOCK_SH);
  3004. if (!$lock) {
  3005. fclose($file);
  3006. return false;
  3007. }
  3008. $string = '';
  3009. while (!feof($file)) {
  3010. $string .= fread($file, 8192);
  3011. }
  3012. flock($file, LOCK_UN);
  3013. fclose($file);
  3014. return $string;
  3015. }
  3016. private function getString($filename): string
  3017. {
  3018. $data = $this->fileGetContents($filename);
  3019. if ($data === false) {
  3020. return '';
  3021. }
  3022. if (strpos($data, '|') === false) {
  3023. return '';
  3024. }
  3025. list($ttl, $string) = explode('|', $data, 2);
  3026. if ($ttl > 0 && time() - filemtime($filename) > $ttl) {
  3027. return '';
  3028. }
  3029. return $string;
  3030. }
  3031. public function get(string $key): string
  3032. {
  3033. $filename = $this->getFileName($key);
  3034. if (!file_exists($filename)) {
  3035. return '';
  3036. }
  3037. $string = $this->getString($filename);
  3038. if ($string == null) {
  3039. return '';
  3040. }
  3041. return $string;
  3042. }
  3043. private function clean(string $path, array $segments, int $len, bool $all) /*: void*/
  3044. {
  3045. $entries = scandir($path);
  3046. foreach ($entries as $entry) {
  3047. if ($entry === '.' || $entry === '..') {
  3048. continue;
  3049. }
  3050. $filename = $path . DIRECTORY_SEPARATOR . $entry;
  3051. if (count($segments) == 0) {
  3052. if (strlen($entry) != $len) {
  3053. continue;
  3054. }
  3055. if (file_exists($filename) && is_file($filename)) {
  3056. if ($all || $this->getString($filename) == null) {
  3057. @unlink($filename);
  3058. }
  3059. }
  3060. } else {
  3061. if (strlen($entry) != $segments[0]) {
  3062. continue;
  3063. }
  3064. if (file_exists($filename) && is_dir($filename)) {
  3065. $this->clean($filename, array_slice($segments, 1), $len - $segments[0], $all);
  3066. @rmdir($filename);
  3067. }
  3068. }
  3069. }
  3070. }
  3071. public function clear(): bool
  3072. {
  3073. if (!file_exists($this->path) || !is_dir($this->path)) {
  3074. return false;
  3075. }
  3076. $this->clean($this->path, array_filter($this->segments), strlen(md5('')), true);
  3077. return true;
  3078. }
  3079. }
  3080. }
  3081. // file: src/Tqdev/PhpCrudApi/Column/Reflection/ReflectedColumn.php
  3082. namespace Tqdev\PhpCrudApi\Column\Reflection {
  3083. use Tqdev\PhpCrudApi\Database\GenericReflection;
  3084. class ReflectedColumn implements \JsonSerializable
  3085. {
  3086. const DEFAULT_LENGTH = 255;
  3087. const DEFAULT_PRECISION = 19;
  3088. const DEFAULT_SCALE = 4;
  3089. private $name;
  3090. private $type;
  3091. private $length;
  3092. private $precision;
  3093. private $scale;
  3094. private $nullable;
  3095. private $pk;
  3096. private $fk;
  3097. public function __construct(string $name, string $type, int $length, int $precision, int $scale, bool $nullable, bool $pk, string $fk)
  3098. {
  3099. $this->name = $name;
  3100. $this->type = $type;
  3101. $this->length = $length;
  3102. $this->precision = $precision;
  3103. $this->scale = $scale;
  3104. $this->nullable = $nullable;
  3105. $this->pk = $pk;
  3106. $this->fk = $fk;
  3107. $this->sanitize();
  3108. }
  3109. private static function parseColumnType(string $columnType, int &$length, int &$precision, int &$scale) /*: void*/
  3110. {
  3111. if (!$columnType) {
  3112. return;
  3113. }
  3114. $pos = strpos($columnType, '(');
  3115. if ($pos) {
  3116. $dataSize = rtrim(substr($columnType, $pos + 1), ')');
  3117. if ($length) {
  3118. $length = (int) $dataSize;
  3119. } else {
  3120. $pos = strpos($dataSize, ',');
  3121. if ($pos) {
  3122. $precision = (int) substr($dataSize, 0, $pos);
  3123. $scale = (int) substr($dataSize, $pos + 1);
  3124. } else {
  3125. $precision = (int) $dataSize;
  3126. $scale = 0;
  3127. }
  3128. }
  3129. }
  3130. }
  3131. private static function getDataSize(int $length, int $precision, int $scale): string
  3132. {
  3133. $dataSize = '';
  3134. if ($length) {
  3135. $dataSize = $length;
  3136. } elseif ($precision) {
  3137. if ($scale) {
  3138. $dataSize = $precision . ',' . $scale;
  3139. } else {
  3140. $dataSize = $precision;
  3141. }
  3142. }
  3143. return $dataSize;
  3144. }
  3145. public static function fromReflection(GenericReflection $reflection, array $columnResult): ReflectedColumn
  3146. {
  3147. $name = $columnResult['COLUMN_NAME'];
  3148. $dataType = $columnResult['DATA_TYPE'];
  3149. $length = (int) $columnResult['CHARACTER_MAXIMUM_LENGTH'];
  3150. $precision = (int) $columnResult['NUMERIC_PRECISION'];
  3151. $scale = (int) $columnResult['NUMERIC_SCALE'];
  3152. $columnType = $columnResult['COLUMN_TYPE'];
  3153. self::parseColumnType($columnType, $length, $precision, $scale);
  3154. $dataSize = self::getDataSize($length, $precision, $scale);
  3155. $type = $reflection->toJdbcType($dataType, $dataSize);
  3156. $nullable = in_array(strtoupper($columnResult['IS_NULLABLE']), ['TRUE', 'YES', 'T', 'Y', '1']);
  3157. $pk = false;
  3158. $fk = '';
  3159. return new ReflectedColumn($name, $type, $length, $precision, $scale, $nullable, $pk, $fk);
  3160. }
  3161. public static function fromJson(/* object */$json): ReflectedColumn
  3162. {
  3163. $name = $json->name;
  3164. $type = $json->type;
  3165. $length = isset($json->length) ? (int) $json->length : 0;
  3166. $precision = isset($json->precision) ? (int) $json->precision : 0;
  3167. $scale = isset($json->scale) ? (int) $json->scale : 0;
  3168. $nullable = isset($json->nullable) ? (bool) $json->nullable : false;
  3169. $pk = isset($json->pk) ? (bool) $json->pk : false;
  3170. $fk = isset($json->fk) ? $json->fk : '';
  3171. return new ReflectedColumn($name, $type, $length, $precision, $scale, $nullable, $pk, $fk);
  3172. }
  3173. private function sanitize()
  3174. {
  3175. $this->length = $this->hasLength() ? $this->getLength() : 0;
  3176. $this->precision = $this->hasPrecision() ? $this->getPrecision() : 0;
  3177. $this->scale = $this->hasScale() ? $this->getScale() : 0;
  3178. }
  3179. public function getName(): string
  3180. {
  3181. return $this->name;
  3182. }
  3183. public function getNullable(): bool
  3184. {
  3185. return $this->nullable;
  3186. }
  3187. public function getType(): string
  3188. {
  3189. return $this->type;
  3190. }
  3191. public function getLength(): int
  3192. {
  3193. return $this->length ?: self::DEFAULT_LENGTH;
  3194. }
  3195. public function getPrecision(): int
  3196. {
  3197. return $this->precision ?: self::DEFAULT_PRECISION;
  3198. }
  3199. public function getScale(): int
  3200. {
  3201. return $this->scale ?: self::DEFAULT_SCALE;
  3202. }
  3203. public function hasLength(): bool
  3204. {
  3205. return in_array($this->type, ['varchar', 'varbinary']);
  3206. }
  3207. public function hasPrecision(): bool
  3208. {
  3209. return $this->type == 'decimal';
  3210. }
  3211. public function hasScale(): bool
  3212. {
  3213. return $this->type == 'decimal';
  3214. }
  3215. public function isBinary(): bool
  3216. {
  3217. return in_array($this->type, ['blob', 'varbinary']);
  3218. }
  3219. public function isBoolean(): bool
  3220. {
  3221. return $this->type == 'boolean';
  3222. }
  3223. public function isGeometry(): bool
  3224. {
  3225. return $this->type == 'geometry';
  3226. }
  3227. public function isInteger(): bool
  3228. {
  3229. return in_array($this->type, ['integer', 'bigint', 'smallint', 'tinyint']);
  3230. }
  3231. public function setPk($value) /*: void*/
  3232. {
  3233. $this->pk = $value;
  3234. }
  3235. public function getPk(): bool
  3236. {
  3237. return $this->pk;
  3238. }
  3239. public function setFk($value) /*: void*/
  3240. {
  3241. $this->fk = $value;
  3242. }
  3243. public function getFk(): string
  3244. {
  3245. return $this->fk;
  3246. }
  3247. public function serialize()
  3248. {
  3249. return [
  3250. 'name' => $this->name,
  3251. 'type' => $this->type,
  3252. 'length' => $this->length,
  3253. 'precision' => $this->precision,
  3254. 'scale' => $this->scale,
  3255. 'nullable' => $this->nullable,
  3256. 'pk' => $this->pk,
  3257. 'fk' => $this->fk,
  3258. ];
  3259. }
  3260. public function jsonSerialize()
  3261. {
  3262. return array_filter($this->serialize());
  3263. }
  3264. }
  3265. }
  3266. // file: src/Tqdev/PhpCrudApi/Column/Reflection/ReflectedDatabase.php
  3267. namespace Tqdev\PhpCrudApi\Column\Reflection {
  3268. use Tqdev\PhpCrudApi\Database\GenericReflection;
  3269. class ReflectedDatabase implements \JsonSerializable
  3270. {
  3271. private $tableTypes;
  3272. public function __construct(array $tableTypes)
  3273. {
  3274. $this->tableTypes = $tableTypes;
  3275. }
  3276. public static function fromReflection(GenericReflection $reflection): ReflectedDatabase
  3277. {
  3278. $tableTypes = [];
  3279. foreach ($reflection->getTables() as $table) {
  3280. $tableName = $table['TABLE_NAME'];
  3281. $tableType = $table['TABLE_TYPE'];
  3282. if (in_array($tableName, $reflection->getIgnoredTables())) {
  3283. continue;
  3284. }
  3285. $tableTypes[$tableName] = $tableType;
  3286. }
  3287. return new ReflectedDatabase($tableTypes);
  3288. }
  3289. public static function fromJson(/* object */$json): ReflectedDatabase
  3290. {
  3291. $tableTypes = (array) $json->tables;
  3292. return new ReflectedDatabase($tableTypes);
  3293. }
  3294. public function hasTable(string $tableName): bool
  3295. {
  3296. return isset($this->tableTypes[$tableName]);
  3297. }
  3298. public function getType(string $tableName): string
  3299. {
  3300. return isset($this->tableTypes[$tableName]) ? $this->tableTypes[$tableName] : '';
  3301. }
  3302. public function getTableNames(): array
  3303. {
  3304. return array_keys($this->tableTypes);
  3305. }
  3306. public function removeTable(string $tableName): bool
  3307. {
  3308. if (!isset($this->tableTypes[$tableName])) {
  3309. return false;
  3310. }
  3311. unset($this->tableTypes[$tableName]);
  3312. return true;
  3313. }
  3314. public function serialize()
  3315. {
  3316. return [
  3317. 'tables' => $this->tableTypes,
  3318. ];
  3319. }
  3320. public function jsonSerialize()
  3321. {
  3322. return $this->serialize();
  3323. }
  3324. }
  3325. }
  3326. // file: src/Tqdev/PhpCrudApi/Column/Reflection/ReflectedTable.php
  3327. namespace Tqdev\PhpCrudApi\Column\Reflection {
  3328. use Tqdev\PhpCrudApi\Database\GenericReflection;
  3329. class ReflectedTable implements \JsonSerializable
  3330. {
  3331. private $name;
  3332. private $type;
  3333. private $columns;
  3334. private $pk;
  3335. private $fks;
  3336. public function __construct(string $name, string $type, array $columns)
  3337. {
  3338. $this->name = $name;
  3339. $this->type = $type;
  3340. // set columns
  3341. $this->columns = [];
  3342. foreach ($columns as $column) {
  3343. $columnName = $column->getName();
  3344. $this->columns[$columnName] = $column;
  3345. }
  3346. // set primary key
  3347. $this->pk = null;
  3348. foreach ($columns as $column) {
  3349. if ($column->getPk() == true) {
  3350. $this->pk = $column;
  3351. }
  3352. }
  3353. // set foreign keys
  3354. $this->fks = [];
  3355. foreach ($columns as $column) {
  3356. $columnName = $column->getName();
  3357. $referencedTableName = $column->getFk();
  3358. if ($referencedTableName != '') {
  3359. $this->fks[$columnName] = $referencedTableName;
  3360. }
  3361. }
  3362. }
  3363. public static function fromReflection(GenericReflection $reflection, string $name, string $type): ReflectedTable
  3364. {
  3365. // set columns
  3366. $columns = [];
  3367. foreach ($reflection->getTableColumns($name, $type) as $tableColumn) {
  3368. $column = ReflectedColumn::fromReflection($reflection, $tableColumn);
  3369. $columns[$column->getName()] = $column;
  3370. }
  3371. // set primary key
  3372. $columnName = false;
  3373. if ($type == 'view') {
  3374. $columnName = 'id';
  3375. } else {
  3376. $columnNames = $reflection->getTablePrimaryKeys($name);
  3377. if (count($columnNames) == 1) {
  3378. $columnName = $columnNames[0];
  3379. }
  3380. }
  3381. if ($columnName && isset($columns[$columnName])) {
  3382. $pk = $columns[$columnName];
  3383. $pk->setPk(true);
  3384. }
  3385. // set foreign keys
  3386. if ($type == 'view') {
  3387. $tables = $reflection->getTables();
  3388. foreach ($columns as $columnName => $column) {
  3389. if (substr($columnName, -3) == '_id') {
  3390. foreach ($tables as $table) {
  3391. $tableName = $table['TABLE_NAME'];
  3392. $suffix = $tableName . '_id';
  3393. if (substr($columnName, -1 * strlen($suffix)) == $suffix) {
  3394. $column->setFk($tableName);
  3395. }
  3396. }
  3397. }
  3398. }
  3399. } else {
  3400. $fks = $reflection->getTableForeignKeys($name);
  3401. foreach ($fks as $columnName => $table) {
  3402. $columns[$columnName]->setFk($table);
  3403. }
  3404. }
  3405. return new ReflectedTable($name, $type, array_values($columns));
  3406. }
  3407. public static function fromJson( /* object */$json): ReflectedTable
  3408. {
  3409. $name = $json->name;
  3410. $type = isset($json->type) ? $json->type : 'table';
  3411. $columns = [];
  3412. if (isset($json->columns) && is_array($json->columns)) {
  3413. foreach ($json->columns as $column) {
  3414. $columns[] = ReflectedColumn::fromJson($column);
  3415. }
  3416. }
  3417. return new ReflectedTable($name, $type, $columns);
  3418. }
  3419. public function hasColumn(string $columnName): bool
  3420. {
  3421. return isset($this->columns[$columnName]);
  3422. }
  3423. public function hasPk(): bool
  3424. {
  3425. return $this->pk != null;
  3426. }
  3427. public function getPk() /*: ?ReflectedColumn */
  3428. {
  3429. return $this->pk;
  3430. }
  3431. public function getName(): string
  3432. {
  3433. return $this->name;
  3434. }
  3435. public function getType(): string
  3436. {
  3437. return $this->type;
  3438. }
  3439. public function getColumnNames(): array
  3440. {
  3441. return array_keys($this->columns);
  3442. }
  3443. public function getColumn($columnName): ReflectedColumn
  3444. {
  3445. return $this->columns[$columnName];
  3446. }
  3447. public function getFksTo(string $tableName): array
  3448. {
  3449. $columns = array();
  3450. foreach ($this->fks as $columnName => $referencedTableName) {
  3451. if ($tableName == $referencedTableName && !is_null($this->columns[$columnName])) {
  3452. $columns[] = $this->columns[$columnName];
  3453. }
  3454. }
  3455. return $columns;
  3456. }
  3457. public function removeColumn(string $columnName): bool
  3458. {
  3459. if (!isset($this->columns[$columnName])) {
  3460. return false;
  3461. }
  3462. unset($this->columns[$columnName]);
  3463. return true;
  3464. }
  3465. public function serialize()
  3466. {
  3467. return [
  3468. 'name' => $this->name,
  3469. 'type' => $this->type,
  3470. 'columns' => array_values($this->columns),
  3471. ];
  3472. }
  3473. public function jsonSerialize()
  3474. {
  3475. return $this->serialize();
  3476. }
  3477. }
  3478. }
  3479. // file: src/Tqdev/PhpCrudApi/Column/DefinitionService.php
  3480. namespace Tqdev\PhpCrudApi\Column {
  3481. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedColumn;
  3482. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
  3483. use Tqdev\PhpCrudApi\Database\GenericDB;
  3484. class DefinitionService
  3485. {
  3486. private $db;
  3487. private $reflection;
  3488. public function __construct(GenericDB $db, ReflectionService $reflection)
  3489. {
  3490. $this->db = $db;
  3491. $this->reflection = $reflection;
  3492. }
  3493. public function updateTable(string $tableName, /* object */ $changes): bool
  3494. {
  3495. $table = $this->reflection->getTable($tableName);
  3496. $newTable = ReflectedTable::fromJson((object) array_merge((array) $table->jsonSerialize(), (array) $changes));
  3497. if ($table->getName() != $newTable->getName()) {
  3498. if (!$this->db->definition()->renameTable($table->getName(), $newTable->getName())) {
  3499. return false;
  3500. }
  3501. }
  3502. return true;
  3503. }
  3504. public function updateColumn(string $tableName, string $columnName, /* object */ $changes): bool
  3505. {
  3506. $table = $this->reflection->getTable($tableName);
  3507. $column = $table->getColumn($columnName);
  3508. // remove constraints on other column
  3509. $newColumn = ReflectedColumn::fromJson((object) array_merge((array) $column->jsonSerialize(), (array) $changes));
  3510. if ($newColumn->getPk() != $column->getPk() && $table->hasPk()) {
  3511. $oldColumn = $table->getPk();
  3512. if ($oldColumn->getName() != $columnName) {
  3513. $oldColumn->setPk(false);
  3514. if (!$this->db->definition()->removeColumnPrimaryKey($table->getName(), $oldColumn->getName(), $oldColumn)) {
  3515. return false;
  3516. }
  3517. }
  3518. }
  3519. // remove constraints
  3520. $newColumn = ReflectedColumn::fromJson((object) array_merge((array) $column->jsonSerialize(), ['pk' => false, 'fk' => false]));
  3521. if ($newColumn->getPk() != $column->getPk() && !$newColumn->getPk()) {
  3522. if (!$this->db->definition()->removeColumnPrimaryKey($table->getName(), $column->getName(), $newColumn)) {
  3523. return false;
  3524. }
  3525. }
  3526. if ($newColumn->getFk() != $column->getFk() && !$newColumn->getFk()) {
  3527. if (!$this->db->definition()->removeColumnForeignKey($table->getName(), $column->getName(), $newColumn)) {
  3528. return false;
  3529. }
  3530. }
  3531. // name and type
  3532. $newColumn = ReflectedColumn::fromJson((object) array_merge((array) $column->jsonSerialize(), (array) $changes));
  3533. $newColumn->setPk(false);
  3534. $newColumn->setFk('');
  3535. if ($newColumn->getName() != $column->getName()) {
  3536. if (!$this->db->definition()->renameColumn($table->getName(), $column->getName(), $newColumn)) {
  3537. return false;
  3538. }
  3539. }
  3540. if (
  3541. $newColumn->getType() != $column->getType() ||
  3542. $newColumn->getLength() != $column->getLength() ||
  3543. $newColumn->getPrecision() != $column->getPrecision() ||
  3544. $newColumn->getScale() != $column->getScale()
  3545. ) {
  3546. if (!$this->db->definition()->retypeColumn($table->getName(), $newColumn->getName(), $newColumn)) {
  3547. return false;
  3548. }
  3549. }
  3550. if ($newColumn->getNullable() != $column->getNullable()) {
  3551. if (!$this->db->definition()->setColumnNullable($table->getName(), $newColumn->getName(), $newColumn)) {
  3552. return false;
  3553. }
  3554. }
  3555. // add constraints
  3556. $newColumn = ReflectedColumn::fromJson((object) array_merge((array) $column->jsonSerialize(), (array) $changes));
  3557. if ($newColumn->getFk()) {
  3558. if (!$this->db->definition()->addColumnForeignKey($table->getName(), $newColumn->getName(), $newColumn)) {
  3559. return false;
  3560. }
  3561. }
  3562. if ($newColumn->getPk()) {
  3563. if (!$this->db->definition()->addColumnPrimaryKey($table->getName(), $newColumn->getName(), $newColumn)) {
  3564. return false;
  3565. }
  3566. }
  3567. return true;
  3568. }
  3569. public function addTable(/* object */$definition)
  3570. {
  3571. $newTable = ReflectedTable::fromJson($definition);
  3572. if (!$this->db->definition()->addTable($newTable)) {
  3573. return false;
  3574. }
  3575. return true;
  3576. }
  3577. public function addColumn(string $tableName, /* object */ $definition)
  3578. {
  3579. $newColumn = ReflectedColumn::fromJson($definition);
  3580. if (!$this->db->definition()->addColumn($tableName, $newColumn)) {
  3581. return false;
  3582. }
  3583. if ($newColumn->getFk()) {
  3584. if (!$this->db->definition()->addColumnForeignKey($tableName, $newColumn->getName(), $newColumn)) {
  3585. return false;
  3586. }
  3587. }
  3588. if ($newColumn->getPk()) {
  3589. if (!$this->db->definition()->addColumnPrimaryKey($tableName, $newColumn->getName(), $newColumn)) {
  3590. return false;
  3591. }
  3592. }
  3593. return true;
  3594. }
  3595. public function removeTable(string $tableName)
  3596. {
  3597. if (!$this->db->definition()->removeTable($tableName)) {
  3598. return false;
  3599. }
  3600. return true;
  3601. }
  3602. public function removeColumn(string $tableName, string $columnName)
  3603. {
  3604. $table = $this->reflection->getTable($tableName);
  3605. $newColumn = $table->getColumn($columnName);
  3606. if ($newColumn->getPk()) {
  3607. $newColumn->setPk(false);
  3608. if (!$this->db->definition()->removeColumnPrimaryKey($table->getName(), $newColumn->getName(), $newColumn)) {
  3609. return false;
  3610. }
  3611. }
  3612. if ($newColumn->getFk()) {
  3613. $newColumn->setFk("");
  3614. if (!$this->db->definition()->removeColumnForeignKey($tableName, $columnName, $newColumn)) {
  3615. return false;
  3616. }
  3617. }
  3618. if (!$this->db->definition()->removeColumn($tableName, $columnName)) {
  3619. return false;
  3620. }
  3621. return true;
  3622. }
  3623. }
  3624. }
  3625. // file: src/Tqdev/PhpCrudApi/Column/ReflectionService.php
  3626. namespace Tqdev\PhpCrudApi\Column {
  3627. use Tqdev\PhpCrudApi\Cache\Cache;
  3628. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedDatabase;
  3629. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
  3630. use Tqdev\PhpCrudApi\Database\GenericDB;
  3631. class ReflectionService
  3632. {
  3633. private $db;
  3634. private $cache;
  3635. private $ttl;
  3636. private $database;
  3637. private $tables;
  3638. public function __construct(GenericDB $db, Cache $cache, int $ttl)
  3639. {
  3640. $this->db = $db;
  3641. $this->cache = $cache;
  3642. $this->ttl = $ttl;
  3643. $this->database = null;
  3644. $this->tables = [];
  3645. }
  3646. private function database(): ReflectedDatabase
  3647. {
  3648. if ($this->database) {
  3649. return $this->database;
  3650. }
  3651. $this->database = $this->loadDatabase(true);
  3652. return $this->database;
  3653. }
  3654. private function loadDatabase(bool $useCache): ReflectedDatabase
  3655. {
  3656. $key = sprintf('%s-ReflectedDatabase', $this->db->getCacheKey());
  3657. $data = $useCache ? $this->cache->get($key) : '';
  3658. if ($data != '') {
  3659. $database = ReflectedDatabase::fromJson(json_decode(gzuncompress($data)));
  3660. } else {
  3661. $database = ReflectedDatabase::fromReflection($this->db->reflection());
  3662. $data = gzcompress(json_encode($database, JSON_UNESCAPED_UNICODE));
  3663. $this->cache->set($key, $data, $this->ttl);
  3664. }
  3665. return $database;
  3666. }
  3667. private function loadTable(string $tableName, bool $useCache): ReflectedTable
  3668. {
  3669. $key = sprintf('%s-ReflectedTable(%s)', $this->db->getCacheKey(), $tableName);
  3670. $data = $useCache ? $this->cache->get($key) : '';
  3671. if ($data != '') {
  3672. $table = ReflectedTable::fromJson(json_decode(gzuncompress($data)));
  3673. } else {
  3674. $tableType = $this->database()->getType($tableName);
  3675. $table = ReflectedTable::fromReflection($this->db->reflection(), $tableName, $tableType);
  3676. $data = gzcompress(json_encode($table, JSON_UNESCAPED_UNICODE));
  3677. $this->cache->set($key, $data, $this->ttl);
  3678. }
  3679. return $table;
  3680. }
  3681. public function refreshTables()
  3682. {
  3683. $this->database = $this->loadDatabase(false);
  3684. }
  3685. public function refreshTable(string $tableName)
  3686. {
  3687. $this->tables[$tableName] = $this->loadTable($tableName, false);
  3688. }
  3689. public function hasTable(string $tableName): bool
  3690. {
  3691. return $this->database()->hasTable($tableName);
  3692. }
  3693. public function getType(string $tableName): string
  3694. {
  3695. return $this->database()->getType($tableName);
  3696. }
  3697. public function getTable(string $tableName): ReflectedTable
  3698. {
  3699. if (!isset($this->tables[$tableName])) {
  3700. $this->tables[$tableName] = $this->loadTable($tableName, true);
  3701. }
  3702. return $this->tables[$tableName];
  3703. }
  3704. public function getTableNames(): array
  3705. {
  3706. return $this->database()->getTableNames();
  3707. }
  3708. public function removeTable(string $tableName): bool
  3709. {
  3710. unset($this->tables[$tableName]);
  3711. return $this->database()->removeTable($tableName);
  3712. }
  3713. }
  3714. }
  3715. // file: src/Tqdev/PhpCrudApi/Controller/CacheController.php
  3716. namespace Tqdev\PhpCrudApi\Controller {
  3717. use Psr\Http\Message\ResponseInterface;
  3718. use Psr\Http\Message\ServerRequestInterface;
  3719. use Tqdev\PhpCrudApi\Cache\Cache;
  3720. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  3721. class CacheController
  3722. {
  3723. private $cache;
  3724. private $responder;
  3725. public function __construct(Router $router, Responder $responder, Cache $cache)
  3726. {
  3727. $router->register('GET', '/cache/clear', array($this, 'clear'));
  3728. $this->cache = $cache;
  3729. $this->responder = $responder;
  3730. }
  3731. public function clear(ServerRequestInterface $request): ResponseInterface
  3732. {
  3733. return $this->responder->success($this->cache->clear());
  3734. }
  3735. }
  3736. }
  3737. // file: src/Tqdev/PhpCrudApi/Controller/ColumnController.php
  3738. namespace Tqdev\PhpCrudApi\Controller {
  3739. use Psr\Http\Message\ResponseInterface;
  3740. use Psr\Http\Message\ServerRequestInterface;
  3741. use Tqdev\PhpCrudApi\Column\DefinitionService;
  3742. use Tqdev\PhpCrudApi\Column\ReflectionService;
  3743. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  3744. use Tqdev\PhpCrudApi\Record\ErrorCode;
  3745. use Tqdev\PhpCrudApi\RequestUtils;
  3746. class ColumnController
  3747. {
  3748. private $responder;
  3749. private $reflection;
  3750. private $definition;
  3751. public function __construct(Router $router, Responder $responder, ReflectionService $reflection, DefinitionService $definition)
  3752. {
  3753. $router->register('GET', '/columns', array($this, 'getDatabase'));
  3754. $router->register('GET', '/columns/*', array($this, 'getTable'));
  3755. $router->register('GET', '/columns/*/*', array($this, 'getColumn'));
  3756. $router->register('PUT', '/columns/*', array($this, 'updateTable'));
  3757. $router->register('PUT', '/columns/*/*', array($this, 'updateColumn'));
  3758. $router->register('POST', '/columns', array($this, 'addTable'));
  3759. $router->register('POST', '/columns/*', array($this, 'addColumn'));
  3760. $router->register('DELETE', '/columns/*', array($this, 'removeTable'));
  3761. $router->register('DELETE', '/columns/*/*', array($this, 'removeColumn'));
  3762. $this->responder = $responder;
  3763. $this->reflection = $reflection;
  3764. $this->definition = $definition;
  3765. }
  3766. public function getDatabase(ServerRequestInterface $request): ResponseInterface
  3767. {
  3768. $tables = [];
  3769. foreach ($this->reflection->getTableNames() as $table) {
  3770. $tables[] = $this->reflection->getTable($table);
  3771. }
  3772. $database = ['tables' => $tables];
  3773. return $this->responder->success($database);
  3774. }
  3775. public function getTable(ServerRequestInterface $request): ResponseInterface
  3776. {
  3777. $tableName = RequestUtils::getPathSegment($request, 2);
  3778. if (!$this->reflection->hasTable($tableName)) {
  3779. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
  3780. }
  3781. $table = $this->reflection->getTable($tableName);
  3782. return $this->responder->success($table);
  3783. }
  3784. public function getColumn(ServerRequestInterface $request): ResponseInterface
  3785. {
  3786. $tableName = RequestUtils::getPathSegment($request, 2);
  3787. $columnName = RequestUtils::getPathSegment($request, 3);
  3788. if (!$this->reflection->hasTable($tableName)) {
  3789. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
  3790. }
  3791. $table = $this->reflection->getTable($tableName);
  3792. if (!$table->hasColumn($columnName)) {
  3793. return $this->responder->error(ErrorCode::COLUMN_NOT_FOUND, $columnName);
  3794. }
  3795. $column = $table->getColumn($columnName);
  3796. return $this->responder->success($column);
  3797. }
  3798. public function updateTable(ServerRequestInterface $request): ResponseInterface
  3799. {
  3800. $tableName = RequestUtils::getPathSegment($request, 2);
  3801. if (!$this->reflection->hasTable($tableName)) {
  3802. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
  3803. }
  3804. $success = $this->definition->updateTable($tableName, $request->getParsedBody());
  3805. if ($success) {
  3806. $this->reflection->refreshTables();
  3807. }
  3808. return $this->responder->success($success);
  3809. }
  3810. public function updateColumn(ServerRequestInterface $request): ResponseInterface
  3811. {
  3812. $tableName = RequestUtils::getPathSegment($request, 2);
  3813. $columnName = RequestUtils::getPathSegment($request, 3);
  3814. if (!$this->reflection->hasTable($tableName)) {
  3815. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
  3816. }
  3817. $table = $this->reflection->getTable($tableName);
  3818. if (!$table->hasColumn($columnName)) {
  3819. return $this->responder->error(ErrorCode::COLUMN_NOT_FOUND, $columnName);
  3820. }
  3821. $success = $this->definition->updateColumn($tableName, $columnName, $request->getParsedBody());
  3822. if ($success) {
  3823. $this->reflection->refreshTable($tableName);
  3824. }
  3825. return $this->responder->success($success);
  3826. }
  3827. public function addTable(ServerRequestInterface $request): ResponseInterface
  3828. {
  3829. $tableName = $request->getParsedBody()->name;
  3830. if ($this->reflection->hasTable($tableName)) {
  3831. return $this->responder->error(ErrorCode::TABLE_ALREADY_EXISTS, $tableName);
  3832. }
  3833. $success = $this->definition->addTable($request->getParsedBody());
  3834. if ($success) {
  3835. $this->reflection->refreshTables();
  3836. }
  3837. return $this->responder->success($success);
  3838. }
  3839. public function addColumn(ServerRequestInterface $request): ResponseInterface
  3840. {
  3841. $tableName = RequestUtils::getPathSegment($request, 2);
  3842. if (!$this->reflection->hasTable($tableName)) {
  3843. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
  3844. }
  3845. $columnName = $request->getParsedBody()->name;
  3846. $table = $this->reflection->getTable($tableName);
  3847. if ($table->hasColumn($columnName)) {
  3848. return $this->responder->error(ErrorCode::COLUMN_ALREADY_EXISTS, $columnName);
  3849. }
  3850. $success = $this->definition->addColumn($tableName, $request->getParsedBody());
  3851. if ($success) {
  3852. $this->reflection->refreshTable($tableName);
  3853. }
  3854. return $this->responder->success($success);
  3855. }
  3856. public function removeTable(ServerRequestInterface $request): ResponseInterface
  3857. {
  3858. $tableName = RequestUtils::getPathSegment($request, 2);
  3859. if (!$this->reflection->hasTable($tableName)) {
  3860. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
  3861. }
  3862. $success = $this->definition->removeTable($tableName);
  3863. if ($success) {
  3864. $this->reflection->refreshTables();
  3865. }
  3866. return $this->responder->success($success);
  3867. }
  3868. public function removeColumn(ServerRequestInterface $request): ResponseInterface
  3869. {
  3870. $tableName = RequestUtils::getPathSegment($request, 2);
  3871. $columnName = RequestUtils::getPathSegment($request, 3);
  3872. if (!$this->reflection->hasTable($tableName)) {
  3873. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
  3874. }
  3875. $table = $this->reflection->getTable($tableName);
  3876. if (!$table->hasColumn($columnName)) {
  3877. return $this->responder->error(ErrorCode::COLUMN_NOT_FOUND, $columnName);
  3878. }
  3879. $success = $this->definition->removeColumn($tableName, $columnName);
  3880. if ($success) {
  3881. $this->reflection->refreshTable($tableName);
  3882. }
  3883. return $this->responder->success($success);
  3884. }
  3885. }
  3886. }
  3887. // file: src/Tqdev/PhpCrudApi/Controller/GeoJsonController.php
  3888. namespace Tqdev\PhpCrudApi\Controller {
  3889. use Psr\Http\Message\ResponseInterface;
  3890. use Psr\Http\Message\ServerRequestInterface;
  3891. use Tqdev\PhpCrudApi\GeoJson\GeoJsonService;
  3892. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  3893. use Tqdev\PhpCrudApi\Record\ErrorCode;
  3894. use Tqdev\PhpCrudApi\RequestUtils;
  3895. class GeoJsonController
  3896. {
  3897. private $service;
  3898. private $responder;
  3899. public function __construct(Router $router, Responder $responder, GeoJsonService $service)
  3900. {
  3901. $router->register('GET', '/geojson/*', array($this, '_list'));
  3902. $router->register('GET', '/geojson/*/*', array($this, 'read'));
  3903. $this->service = $service;
  3904. $this->responder = $responder;
  3905. }
  3906. public function _list(ServerRequestInterface $request): ResponseInterface
  3907. {
  3908. $table = RequestUtils::getPathSegment($request, 2);
  3909. $params = RequestUtils::getParams($request);
  3910. if (!$this->service->hasTable($table)) {
  3911. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  3912. }
  3913. return $this->responder->success($this->service->_list($table, $params));
  3914. }
  3915. public function read(ServerRequestInterface $request): ResponseInterface
  3916. {
  3917. $table = RequestUtils::getPathSegment($request, 2);
  3918. if (!$this->service->hasTable($table)) {
  3919. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  3920. }
  3921. if ($this->service->getType($table) != 'table') {
  3922. return $this->responder->error(ErrorCode::OPERATION_NOT_SUPPORTED, __FUNCTION__);
  3923. }
  3924. $id = RequestUtils::getPathSegment($request, 3);
  3925. $params = RequestUtils::getParams($request);
  3926. if (strpos($id, ',') !== false) {
  3927. $ids = explode(',', $id);
  3928. $result = (object) array('type' => 'FeatureCollection', 'features' => array());
  3929. for ($i = 0; $i < count($ids); $i++) {
  3930. array_push($result->features, $this->service->read($table, $ids[$i], $params));
  3931. }
  3932. return $this->responder->success($result);
  3933. } else {
  3934. $response = $this->service->read($table, $id, $params);
  3935. if ($response === null) {
  3936. return $this->responder->error(ErrorCode::RECORD_NOT_FOUND, $id);
  3937. }
  3938. return $this->responder->success($response);
  3939. }
  3940. }
  3941. }
  3942. }
  3943. // file: src/Tqdev/PhpCrudApi/Controller/JsonResponder.php
  3944. namespace Tqdev\PhpCrudApi\Controller {
  3945. use Psr\Http\Message\ResponseInterface;
  3946. use Tqdev\PhpCrudApi\Record\Document\ErrorDocument;
  3947. use Tqdev\PhpCrudApi\Record\ErrorCode;
  3948. use Tqdev\PhpCrudApi\ResponseFactory;
  3949. use Tqdev\PhpCrudApi\ResponseUtils;
  3950. class JsonResponder implements Responder
  3951. {
  3952. private $debug;
  3953. public function __construct(bool $debug)
  3954. {
  3955. $this->debug = $debug;
  3956. }
  3957. public function error(int $error, string $argument, $details = null): ResponseInterface
  3958. {
  3959. $document = new ErrorDocument(new ErrorCode($error), $argument, $details);
  3960. return ResponseFactory::fromObject($document->getStatus(), $document);
  3961. }
  3962. public function success($result): ResponseInterface
  3963. {
  3964. return ResponseFactory::fromObject(ResponseFactory::OK, $result);
  3965. }
  3966. public function exception($exception): ResponseInterface
  3967. {
  3968. $document = ErrorDocument::fromException($exception);
  3969. $response = ResponseFactory::fromObject($document->getStatus(), $document);
  3970. if ($this->debug) {
  3971. $response = ResponseUtils::addExceptionHeaders($response, $exception);
  3972. }
  3973. return $response;
  3974. }
  3975. public function multi($results): ResponseInterface
  3976. {
  3977. $documents = array();
  3978. $errors = array();
  3979. $success = true;
  3980. foreach ($results as $i => $result) {
  3981. if ($result instanceof \Throwable) {
  3982. $documents[$i] = null;
  3983. $errors[$i] = ErrorDocument::fromException($result);
  3984. $success = false;
  3985. } else {
  3986. $documents[$i] = $result;
  3987. $errors[$i] = new ErrorDocument(new ErrorCode(0), '', null);
  3988. }
  3989. }
  3990. $status = $success ? ResponseFactory::OK : ResponseFactory::FAILED_DEPENDENCY;
  3991. $document = $success ? $documents : $errors;
  3992. $response = ResponseFactory::fromObject($status, $document);
  3993. foreach ($results as $i => $result) {
  3994. if ($result instanceof \Throwable) {
  3995. if ($this->debug) {
  3996. $response = ResponseUtils::addExceptionHeaders($response, $result);
  3997. }
  3998. }
  3999. }
  4000. return $response;
  4001. }
  4002. }
  4003. }
  4004. // file: src/Tqdev/PhpCrudApi/Controller/OpenApiController.php
  4005. namespace Tqdev\PhpCrudApi\Controller {
  4006. use Psr\Http\Message\ResponseInterface;
  4007. use Psr\Http\Message\ServerRequestInterface;
  4008. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  4009. use Tqdev\PhpCrudApi\OpenApi\OpenApiService;
  4010. class OpenApiController
  4011. {
  4012. private $openApi;
  4013. private $responder;
  4014. public function __construct(Router $router, Responder $responder, OpenApiService $openApi)
  4015. {
  4016. $router->register('GET', '/openapi', array($this, 'openapi'));
  4017. $this->openApi = $openApi;
  4018. $this->responder = $responder;
  4019. }
  4020. public function openapi(ServerRequestInterface $request): ResponseInterface
  4021. {
  4022. return $this->responder->success($this->openApi->get());
  4023. }
  4024. }
  4025. }
  4026. // file: src/Tqdev/PhpCrudApi/Controller/RecordController.php
  4027. namespace Tqdev\PhpCrudApi\Controller {
  4028. use Psr\Http\Message\ResponseInterface;
  4029. use Psr\Http\Message\ServerRequestInterface;
  4030. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  4031. use Tqdev\PhpCrudApi\Record\ErrorCode;
  4032. use Tqdev\PhpCrudApi\Record\RecordService;
  4033. use Tqdev\PhpCrudApi\RequestUtils;
  4034. class RecordController
  4035. {
  4036. private $service;
  4037. private $responder;
  4038. public function __construct(Router $router, Responder $responder, RecordService $service)
  4039. {
  4040. $router->register('GET', '/records/*', array($this, '_list'));
  4041. $router->register('POST', '/records/*', array($this, 'create'));
  4042. $router->register('GET', '/records/*/*', array($this, 'read'));
  4043. $router->register('PUT', '/records/*/*', array($this, 'update'));
  4044. $router->register('DELETE', '/records/*/*', array($this, 'delete'));
  4045. $router->register('PATCH', '/records/*/*', array($this, 'increment'));
  4046. $this->service = $service;
  4047. $this->responder = $responder;
  4048. }
  4049. public function _list(ServerRequestInterface $request): ResponseInterface
  4050. {
  4051. $table = RequestUtils::getPathSegment($request, 2);
  4052. $params = RequestUtils::getParams($request);
  4053. if (!$this->service->hasTable($table)) {
  4054. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  4055. }
  4056. return $this->responder->success($this->service->_list($table, $params));
  4057. }
  4058. public function read(ServerRequestInterface $request): ResponseInterface
  4059. {
  4060. $table = RequestUtils::getPathSegment($request, 2);
  4061. if (!$this->service->hasTable($table)) {
  4062. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  4063. }
  4064. $id = RequestUtils::getPathSegment($request, 3);
  4065. $params = RequestUtils::getParams($request);
  4066. if (strpos($id, ',') !== false) {
  4067. $ids = explode(',', $id);
  4068. $argumentLists = array();
  4069. for ($i = 0; $i < count($ids); $i++) {
  4070. $argumentLists[] = array($table, $ids[$i], $params);
  4071. }
  4072. return $this->responder->multi($this->multiCall([$this->service, 'read'], $argumentLists));
  4073. } else {
  4074. $response = $this->service->read($table, $id, $params);
  4075. if ($response === null) {
  4076. return $this->responder->error(ErrorCode::RECORD_NOT_FOUND, $id);
  4077. }
  4078. return $this->responder->success($response);
  4079. }
  4080. }
  4081. private function multiCall(callable $method, array $argumentLists): array
  4082. {
  4083. $result = array();
  4084. $success = true;
  4085. $this->service->beginTransaction();
  4086. foreach ($argumentLists as $arguments) {
  4087. try {
  4088. $result[] = call_user_func_array($method, $arguments);
  4089. } catch (\Throwable $e) {
  4090. $success = false;
  4091. $result[] = $e;
  4092. }
  4093. }
  4094. if ($success) {
  4095. $this->service->commitTransaction();
  4096. } else {
  4097. $this->service->rollBackTransaction();
  4098. }
  4099. return $result;
  4100. }
  4101. public function create(ServerRequestInterface $request): ResponseInterface
  4102. {
  4103. $table = RequestUtils::getPathSegment($request, 2);
  4104. if (!$this->service->hasTable($table)) {
  4105. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  4106. }
  4107. if ($this->service->getType($table) != 'table') {
  4108. return $this->responder->error(ErrorCode::OPERATION_NOT_SUPPORTED, __FUNCTION__);
  4109. }
  4110. $record = $request->getParsedBody();
  4111. if ($record === null) {
  4112. return $this->responder->error(ErrorCode::HTTP_MESSAGE_NOT_READABLE, '');
  4113. }
  4114. $params = RequestUtils::getParams($request);
  4115. if (is_array($record)) {
  4116. $argumentLists = array();
  4117. foreach ($record as $r) {
  4118. $argumentLists[] = array($table, $r, $params);
  4119. }
  4120. return $this->responder->multi($this->multiCall([$this->service, 'create'], $argumentLists));
  4121. } else {
  4122. return $this->responder->success($this->service->create($table, $record, $params));
  4123. }
  4124. }
  4125. public function update(ServerRequestInterface $request): ResponseInterface
  4126. {
  4127. $table = RequestUtils::getPathSegment($request, 2);
  4128. if (!$this->service->hasTable($table)) {
  4129. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  4130. }
  4131. if ($this->service->getType($table) != 'table') {
  4132. return $this->responder->error(ErrorCode::OPERATION_NOT_SUPPORTED, __FUNCTION__);
  4133. }
  4134. $id = RequestUtils::getPathSegment($request, 3);
  4135. $params = RequestUtils::getParams($request);
  4136. $record = $request->getParsedBody();
  4137. if ($record === null) {
  4138. return $this->responder->error(ErrorCode::HTTP_MESSAGE_NOT_READABLE, '');
  4139. }
  4140. $ids = explode(',', $id);
  4141. if (is_array($record)) {
  4142. if (count($ids) != count($record)) {
  4143. return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id);
  4144. }
  4145. $argumentLists = array();
  4146. for ($i = 0; $i < count($ids); $i++) {
  4147. $argumentLists[] = array($table, $ids[$i], $record[$i], $params);
  4148. }
  4149. return $this->responder->multi($this->multiCall([$this->service, 'update'], $argumentLists));
  4150. } else {
  4151. if (count($ids) != 1) {
  4152. return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id);
  4153. }
  4154. return $this->responder->success($this->service->update($table, $id, $record, $params));
  4155. }
  4156. }
  4157. public function delete(ServerRequestInterface $request): ResponseInterface
  4158. {
  4159. $table = RequestUtils::getPathSegment($request, 2);
  4160. if (!$this->service->hasTable($table)) {
  4161. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  4162. }
  4163. if ($this->service->getType($table) != 'table') {
  4164. return $this->responder->error(ErrorCode::OPERATION_NOT_SUPPORTED, __FUNCTION__);
  4165. }
  4166. $id = RequestUtils::getPathSegment($request, 3);
  4167. $params = RequestUtils::getParams($request);
  4168. $ids = explode(',', $id);
  4169. if (count($ids) > 1) {
  4170. $argumentLists = array();
  4171. for ($i = 0; $i < count($ids); $i++) {
  4172. $argumentLists[] = array($table, $ids[$i], $params);
  4173. }
  4174. return $this->responder->multi($this->multiCall([$this->service, 'delete'], $argumentLists));
  4175. } else {
  4176. return $this->responder->success($this->service->delete($table, $id, $params));
  4177. }
  4178. }
  4179. public function increment(ServerRequestInterface $request): ResponseInterface
  4180. {
  4181. $table = RequestUtils::getPathSegment($request, 2);
  4182. if (!$this->service->hasTable($table)) {
  4183. return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
  4184. }
  4185. if ($this->service->getType($table) != 'table') {
  4186. return $this->responder->error(ErrorCode::OPERATION_NOT_SUPPORTED, __FUNCTION__);
  4187. }
  4188. $id = RequestUtils::getPathSegment($request, 3);
  4189. $record = $request->getParsedBody();
  4190. if ($record === null) {
  4191. return $this->responder->error(ErrorCode::HTTP_MESSAGE_NOT_READABLE, '');
  4192. }
  4193. $params = RequestUtils::getParams($request);
  4194. $ids = explode(',', $id);
  4195. if (is_array($record)) {
  4196. if (count($ids) != count($record)) {
  4197. return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id);
  4198. }
  4199. $argumentLists = array();
  4200. for ($i = 0; $i < count($ids); $i++) {
  4201. $argumentLists[] = array($table, $ids[$i], $record[$i], $params);
  4202. }
  4203. return $this->responder->multi($this->multiCall([$this->service, 'increment'], $argumentLists));
  4204. } else {
  4205. if (count($ids) != 1) {
  4206. return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id);
  4207. }
  4208. return $this->responder->success($this->service->increment($table, $id, $record, $params));
  4209. }
  4210. }
  4211. }
  4212. }
  4213. // file: src/Tqdev/PhpCrudApi/Controller/Responder.php
  4214. namespace Tqdev\PhpCrudApi\Controller {
  4215. use Psr\Http\Message\ResponseInterface;
  4216. interface Responder
  4217. {
  4218. public function error(int $error, string $argument, $details = null): ResponseInterface;
  4219. public function success($result): ResponseInterface;
  4220. }
  4221. }
  4222. // file: src/Tqdev/PhpCrudApi/Database/ColumnConverter.php
  4223. namespace Tqdev\PhpCrudApi\Database {
  4224. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedColumn;
  4225. class ColumnConverter
  4226. {
  4227. private $driver;
  4228. public function __construct(string $driver)
  4229. {
  4230. $this->driver = $driver;
  4231. }
  4232. public function convertColumnValue(ReflectedColumn $column): string
  4233. {
  4234. if ($column->isBoolean()) {
  4235. switch ($this->driver) {
  4236. case 'mysql':
  4237. return "IFNULL(IF(?,TRUE,FALSE),NULL)";
  4238. case 'pgsql':
  4239. return "?";
  4240. case 'sqlsrv':
  4241. return "?";
  4242. }
  4243. }
  4244. if ($column->isBinary()) {
  4245. switch ($this->driver) {
  4246. case 'mysql':
  4247. return "FROM_BASE64(?)";
  4248. case 'pgsql':
  4249. return "decode(?, 'base64')";
  4250. case 'sqlsrv':
  4251. return "CONVERT(XML, ?).value('.','varbinary(max)')";
  4252. }
  4253. }
  4254. if ($column->isGeometry()) {
  4255. switch ($this->driver) {
  4256. case 'mysql':
  4257. case 'pgsql':
  4258. return "ST_GeomFromText(?)";
  4259. case 'sqlsrv':
  4260. return "geometry::STGeomFromText(?,0)";
  4261. }
  4262. }
  4263. return '?';
  4264. }
  4265. public function convertColumnName(ReflectedColumn $column, $value): string
  4266. {
  4267. if ($column->isBinary()) {
  4268. switch ($this->driver) {
  4269. case 'mysql':
  4270. return "TO_BASE64($value) as $value";
  4271. case 'pgsql':
  4272. return "encode($value::bytea, 'base64') as $value";
  4273. case 'sqlsrv':
  4274. return "CASE WHEN $value IS NULL THEN NULL ELSE (SELECT CAST($value as varbinary(max)) FOR XML PATH(''), BINARY BASE64) END as $value";
  4275. }
  4276. }
  4277. if ($column->isGeometry()) {
  4278. switch ($this->driver) {
  4279. case 'mysql':
  4280. case 'pgsql':
  4281. return "ST_AsText($value) as $value";
  4282. case 'sqlsrv':
  4283. return "REPLACE($value.STAsText(),' (','(') as $value";
  4284. }
  4285. }
  4286. return $value;
  4287. }
  4288. }
  4289. }
  4290. // file: src/Tqdev/PhpCrudApi/Database/ColumnsBuilder.php
  4291. namespace Tqdev\PhpCrudApi\Database {
  4292. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedColumn;
  4293. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
  4294. class ColumnsBuilder
  4295. {
  4296. private $driver;
  4297. private $converter;
  4298. public function __construct(string $driver)
  4299. {
  4300. $this->driver = $driver;
  4301. $this->converter = new ColumnConverter($driver);
  4302. }
  4303. public function getOffsetLimit(int $offset, int $limit): string
  4304. {
  4305. if ($limit < 0 || $offset < 0) {
  4306. return '';
  4307. }
  4308. switch ($this->driver) {
  4309. case 'mysql':
  4310. return " LIMIT $offset, $limit";
  4311. case 'pgsql':
  4312. return " LIMIT $limit OFFSET $offset";
  4313. case 'sqlsrv':
  4314. return " OFFSET $offset ROWS FETCH NEXT $limit ROWS ONLY";
  4315. case 'sqlite':
  4316. return " LIMIT $limit OFFSET $offset";
  4317. }
  4318. }
  4319. private function quoteColumnName(ReflectedColumn $column): string
  4320. {
  4321. return '"' . $column->getName() . '"';
  4322. }
  4323. public function getOrderBy(ReflectedTable $table, array $columnOrdering): string
  4324. {
  4325. if (count($columnOrdering) == 0) {
  4326. return '';
  4327. }
  4328. $results = array();
  4329. foreach ($columnOrdering as $i => list($columnName, $ordering)) {
  4330. $column = $table->getColumn($columnName);
  4331. $quotedColumnName = $this->quoteColumnName($column);
  4332. $results[] = $quotedColumnName . ' ' . $ordering;
  4333. }
  4334. return ' ORDER BY ' . implode(',', $results);
  4335. }
  4336. public function getSelect(ReflectedTable $table, array $columnNames): string
  4337. {
  4338. $results = array();
  4339. foreach ($columnNames as $columnName) {
  4340. $column = $table->getColumn($columnName);
  4341. $quotedColumnName = $this->quoteColumnName($column);
  4342. $quotedColumnName = $this->converter->convertColumnName($column, $quotedColumnName);
  4343. $results[] = $quotedColumnName;
  4344. }
  4345. return implode(',', $results);
  4346. }
  4347. public function getInsert(ReflectedTable $table, array $columnValues): string
  4348. {
  4349. $columns = array();
  4350. $values = array();
  4351. foreach ($columnValues as $columnName => $columnValue) {
  4352. $column = $table->getColumn($columnName);
  4353. $quotedColumnName = $this->quoteColumnName($column);
  4354. $columns[] = $quotedColumnName;
  4355. $columnValue = $this->converter->convertColumnValue($column);
  4356. $values[] = $columnValue;
  4357. }
  4358. $columnsSql = '(' . implode(',', $columns) . ')';
  4359. $valuesSql = '(' . implode(',', $values) . ')';
  4360. $outputColumn = $this->quoteColumnName($table->getPk());
  4361. switch ($this->driver) {
  4362. case 'mysql':
  4363. return "$columnsSql VALUES $valuesSql";
  4364. case 'pgsql':
  4365. return "$columnsSql VALUES $valuesSql RETURNING $outputColumn";
  4366. case 'sqlsrv':
  4367. return "$columnsSql OUTPUT INSERTED.$outputColumn VALUES $valuesSql";
  4368. case 'sqlite':
  4369. return "$columnsSql VALUES $valuesSql";
  4370. }
  4371. }
  4372. public function getUpdate(ReflectedTable $table, array $columnValues): string
  4373. {
  4374. $results = array();
  4375. foreach ($columnValues as $columnName => $columnValue) {
  4376. $column = $table->getColumn($columnName);
  4377. $quotedColumnName = $this->quoteColumnName($column);
  4378. $columnValue = $this->converter->convertColumnValue($column);
  4379. $results[] = $quotedColumnName . '=' . $columnValue;
  4380. }
  4381. return implode(',', $results);
  4382. }
  4383. public function getIncrement(ReflectedTable $table, array $columnValues): string
  4384. {
  4385. $results = array();
  4386. foreach ($columnValues as $columnName => $columnValue) {
  4387. if (!is_numeric($columnValue)) {
  4388. continue;
  4389. }
  4390. $column = $table->getColumn($columnName);
  4391. $quotedColumnName = $this->quoteColumnName($column);
  4392. $columnValue = $this->converter->convertColumnValue($column);
  4393. $results[] = $quotedColumnName . '=' . $quotedColumnName . '+' . $columnValue;
  4394. }
  4395. return implode(',', $results);
  4396. }
  4397. }
  4398. }
  4399. // file: src/Tqdev/PhpCrudApi/Database/ConditionsBuilder.php
  4400. namespace Tqdev\PhpCrudApi\Database {
  4401. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedColumn;
  4402. use Tqdev\PhpCrudApi\Record\Condition\AndCondition;
  4403. use Tqdev\PhpCrudApi\Record\Condition\ColumnCondition;
  4404. use Tqdev\PhpCrudApi\Record\Condition\Condition;
  4405. use Tqdev\PhpCrudApi\Record\Condition\NoCondition;
  4406. use Tqdev\PhpCrudApi\Record\Condition\NotCondition;
  4407. use Tqdev\PhpCrudApi\Record\Condition\OrCondition;
  4408. use Tqdev\PhpCrudApi\Record\Condition\SpatialCondition;
  4409. class ConditionsBuilder
  4410. {
  4411. private $driver;
  4412. public function __construct(string $driver)
  4413. {
  4414. $this->driver = $driver;
  4415. }
  4416. private function getConditionSql(Condition $condition, array &$arguments): string
  4417. {
  4418. if ($condition instanceof AndCondition) {
  4419. return $this->getAndConditionSql($condition, $arguments);
  4420. }
  4421. if ($condition instanceof OrCondition) {
  4422. return $this->getOrConditionSql($condition, $arguments);
  4423. }
  4424. if ($condition instanceof NotCondition) {
  4425. return $this->getNotConditionSql($condition, $arguments);
  4426. }
  4427. if ($condition instanceof SpatialCondition) {
  4428. return $this->getSpatialConditionSql($condition, $arguments);
  4429. }
  4430. if ($condition instanceof ColumnCondition) {
  4431. return $this->getColumnConditionSql($condition, $arguments);
  4432. }
  4433. throw new \Exception('Unknown Condition: ' . get_class($condition));
  4434. }
  4435. private function getAndConditionSql(AndCondition $and, array &$arguments): string
  4436. {
  4437. $parts = [];
  4438. foreach ($and->getConditions() as $condition) {
  4439. $parts[] = $this->getConditionSql($condition, $arguments);
  4440. }
  4441. return '(' . implode(' AND ', $parts) . ')';
  4442. }
  4443. private function getOrConditionSql(OrCondition $or, array &$arguments): string
  4444. {
  4445. $parts = [];
  4446. foreach ($or->getConditions() as $condition) {
  4447. $parts[] = $this->getConditionSql($condition, $arguments);
  4448. }
  4449. return '(' . implode(' OR ', $parts) . ')';
  4450. }
  4451. private function getNotConditionSql(NotCondition $not, array &$arguments): string
  4452. {
  4453. $condition = $not->getCondition();
  4454. return '(NOT ' . $this->getConditionSql($condition, $arguments) . ')';
  4455. }
  4456. private function quoteColumnName(ReflectedColumn $column): string
  4457. {
  4458. return '"' . $column->getName() . '"';
  4459. }
  4460. private function escapeLikeValue(string $value): string
  4461. {
  4462. return addcslashes($value, '%_');
  4463. }
  4464. private function getColumnConditionSql(ColumnCondition $condition, array &$arguments): string
  4465. {
  4466. $column = $this->quoteColumnName($condition->getColumn());
  4467. $operator = $condition->getOperator();
  4468. $value = $condition->getValue();
  4469. switch ($operator) {
  4470. case 'cs':
  4471. $sql = "$column LIKE ?";
  4472. $arguments[] = '%' . $this->escapeLikeValue($value) . '%';
  4473. break;
  4474. case 'sw':
  4475. $sql = "$column LIKE ?";
  4476. $arguments[] = $this->escapeLikeValue($value) . '%';
  4477. break;
  4478. case 'ew':
  4479. $sql = "$column LIKE ?";
  4480. $arguments[] = '%' . $this->escapeLikeValue($value);
  4481. break;
  4482. case 'eq':
  4483. $sql = "$column = ?";
  4484. $arguments[] = $value;
  4485. break;
  4486. case 'lt':
  4487. $sql = "$column < ?";
  4488. $arguments[] = $value;
  4489. break;
  4490. case 'le':
  4491. $sql = "$column <= ?";
  4492. $arguments[] = $value;
  4493. break;
  4494. case 'ge':
  4495. $sql = "$column >= ?";
  4496. $arguments[] = $value;
  4497. break;
  4498. case 'gt':
  4499. $sql = "$column > ?";
  4500. $arguments[] = $value;
  4501. break;
  4502. case 'bt':
  4503. $parts = explode(',', $value, 2);
  4504. $count = count($parts);
  4505. if ($count == 2) {
  4506. $sql = "($column >= ? AND $column <= ?)";
  4507. $arguments[] = $parts[0];
  4508. $arguments[] = $parts[1];
  4509. } else {
  4510. $sql = "FALSE";
  4511. }
  4512. break;
  4513. case 'in':
  4514. $parts = explode(',', $value);
  4515. $count = count($parts);
  4516. if ($count > 0) {
  4517. $qmarks = implode(',', str_split(str_repeat('?', $count)));
  4518. $sql = "$column IN ($qmarks)";
  4519. for ($i = 0; $i < $count; $i++) {
  4520. $arguments[] = $parts[$i];
  4521. }
  4522. } else {
  4523. $sql = "FALSE";
  4524. }
  4525. break;
  4526. case 'is':
  4527. $sql = "$column IS NULL";
  4528. break;
  4529. }
  4530. return $sql;
  4531. }
  4532. private function getSpatialFunctionName(string $operator): string
  4533. {
  4534. switch ($operator) {
  4535. case 'co':
  4536. return 'ST_Contains';
  4537. case 'cr':
  4538. return 'ST_Crosses';
  4539. case 'di':
  4540. return 'ST_Disjoint';
  4541. case 'eq':
  4542. return 'ST_Equals';
  4543. case 'in':
  4544. return 'ST_Intersects';
  4545. case 'ov':
  4546. return 'ST_Overlaps';
  4547. case 'to':
  4548. return 'ST_Touches';
  4549. case 'wi':
  4550. return 'ST_Within';
  4551. case 'ic':
  4552. return 'ST_IsClosed';
  4553. case 'is':
  4554. return 'ST_IsSimple';
  4555. case 'iv':
  4556. return 'ST_IsValid';
  4557. }
  4558. }
  4559. private function hasSpatialArgument(string $operator): bool
  4560. {
  4561. return in_array($operator, ['ic', 'is', 'iv']) ? false : true;
  4562. }
  4563. private function getSpatialFunctionCall(string $functionName, string $column, bool $hasArgument): string
  4564. {
  4565. switch ($this->driver) {
  4566. case 'mysql':
  4567. case 'pgsql':
  4568. $argument = $hasArgument ? 'ST_GeomFromText(?)' : '';
  4569. return "$functionName($column, $argument)=TRUE";
  4570. case 'sqlsrv':
  4571. $functionName = str_replace('ST_', 'ST', $functionName);
  4572. $argument = $hasArgument ? 'geometry::STGeomFromText(?,0)' : '';
  4573. return "$column.$functionName($argument)=1";
  4574. case 'sqlite':
  4575. $argument = $hasArgument ? '?' : '0';
  4576. return "$functionName($column, $argument)=1";
  4577. }
  4578. }
  4579. private function getSpatialConditionSql(ColumnCondition $condition, array &$arguments): string
  4580. {
  4581. $column = $this->quoteColumnName($condition->getColumn());
  4582. $operator = $condition->getOperator();
  4583. $value = $condition->getValue();
  4584. $functionName = $this->getSpatialFunctionName($operator);
  4585. $hasArgument = $this->hasSpatialArgument($operator);
  4586. $sql = $this->getSpatialFunctionCall($functionName, $column, $hasArgument);
  4587. if ($hasArgument) {
  4588. $arguments[] = $value;
  4589. }
  4590. return $sql;
  4591. }
  4592. public function getWhereClause(Condition $condition, array &$arguments): string
  4593. {
  4594. if ($condition instanceof NoCondition) {
  4595. return '';
  4596. }
  4597. return ' WHERE ' . $this->getConditionSql($condition, $arguments);
  4598. }
  4599. }
  4600. }
  4601. // file: src/Tqdev/PhpCrudApi/Database/DataConverter.php
  4602. namespace Tqdev\PhpCrudApi\Database {
  4603. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedColumn;
  4604. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
  4605. class DataConverter
  4606. {
  4607. private $driver;
  4608. public function __construct(string $driver)
  4609. {
  4610. $this->driver = $driver;
  4611. }
  4612. private function convertRecordValue($conversion, $value)
  4613. {
  4614. $args = explode('|', $conversion);
  4615. $type = array_shift($args);
  4616. switch ($type) {
  4617. case 'boolean':
  4618. return $value ? true : false;
  4619. case 'integer':
  4620. return (int) $value;
  4621. case 'float':
  4622. return (float) $value;
  4623. case 'decimal':
  4624. return number_format($value, $args[0], '.', '');
  4625. }
  4626. return $value;
  4627. }
  4628. private function getRecordValueConversion(ReflectedColumn $column): string
  4629. {
  4630. if (in_array($this->driver, ['mysql', 'sqlsrv', 'sqlite']) && $column->isBoolean()) {
  4631. return 'boolean';
  4632. }
  4633. if (in_array($this->driver, ['sqlsrv', 'sqlite']) && in_array($column->getType(), ['integer', 'bigint'])) {
  4634. return 'integer';
  4635. }
  4636. if (in_array($this->driver, ['sqlite', 'pgsql']) && in_array($column->getType(), ['float', 'double'])) {
  4637. return 'float';
  4638. }
  4639. if (in_array($this->driver, ['sqlite']) && in_array($column->getType(), ['decimal'])) {
  4640. return 'decimal|' . $column->getScale();
  4641. }
  4642. return 'none';
  4643. }
  4644. public function convertRecords(ReflectedTable $table, array $columnNames, array &$records) /*: void*/
  4645. {
  4646. foreach ($columnNames as $columnName) {
  4647. $column = $table->getColumn($columnName);
  4648. $conversion = $this->getRecordValueConversion($column);
  4649. if ($conversion != 'none') {
  4650. foreach ($records as $i => $record) {
  4651. $value = $records[$i][$columnName];
  4652. if ($value === null) {
  4653. continue;
  4654. }
  4655. $records[$i][$columnName] = $this->convertRecordValue($conversion, $value);
  4656. }
  4657. }
  4658. }
  4659. }
  4660. private function convertInputValue($conversion, $value)
  4661. {
  4662. switch ($conversion) {
  4663. case 'boolean':
  4664. return $value ? 1 : 0;
  4665. case 'base64url_to_base64':
  4666. return str_pad(strtr($value, '-_', '+/'), ceil(strlen($value) / 4) * 4, '=', STR_PAD_RIGHT);
  4667. }
  4668. return $value;
  4669. }
  4670. private function getInputValueConversion(ReflectedColumn $column): string
  4671. {
  4672. if ($column->isBoolean()) {
  4673. return 'boolean';
  4674. }
  4675. if ($column->isBinary()) {
  4676. return 'base64url_to_base64';
  4677. }
  4678. return 'none';
  4679. }
  4680. public function convertColumnValues(ReflectedTable $table, array &$columnValues) /*: void*/
  4681. {
  4682. $columnNames = array_keys($columnValues);
  4683. foreach ($columnNames as $columnName) {
  4684. $column = $table->getColumn($columnName);
  4685. $conversion = $this->getInputValueConversion($column);
  4686. if ($conversion != 'none') {
  4687. $value = $columnValues[$columnName];
  4688. if ($value !== null) {
  4689. $columnValues[$columnName] = $this->convertInputValue($conversion, $value);
  4690. }
  4691. }
  4692. }
  4693. }
  4694. }
  4695. }
  4696. // file: src/Tqdev/PhpCrudApi/Database/GenericDB.php
  4697. namespace Tqdev\PhpCrudApi\Database {
  4698. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
  4699. use Tqdev\PhpCrudApi\Middleware\Communication\VariableStore;
  4700. use Tqdev\PhpCrudApi\Record\Condition\ColumnCondition;
  4701. use Tqdev\PhpCrudApi\Record\Condition\Condition;
  4702. class GenericDB
  4703. {
  4704. private $driver;
  4705. private $address;
  4706. private $port;
  4707. private $database;
  4708. private $tables;
  4709. private $username;
  4710. private $password;
  4711. private $pdo;
  4712. private $reflection;
  4713. private $definition;
  4714. private $conditions;
  4715. private $columns;
  4716. private $converter;
  4717. private function getDsn(): string
  4718. {
  4719. switch ($this->driver) {
  4720. case 'mysql':
  4721. return "$this->driver:host=$this->address;port=$this->port;dbname=$this->database;charset=utf8mb4";
  4722. case 'pgsql':
  4723. return "$this->driver:host=$this->address port=$this->port dbname=$this->database options='--client_encoding=UTF8'";
  4724. case 'sqlsrv':
  4725. return "$this->driver:Server=$this->address,$this->port;Database=$this->database";
  4726. case 'sqlite':
  4727. return "$this->driver:$this->address";
  4728. }
  4729. }
  4730. private function getCommands(): array
  4731. {
  4732. switch ($this->driver) {
  4733. case 'mysql':
  4734. return [
  4735. 'SET SESSION sql_warnings=1;',
  4736. 'SET NAMES utf8mb4;',
  4737. 'SET SESSION sql_mode = "ANSI,TRADITIONAL";',
  4738. ];
  4739. case 'pgsql':
  4740. return [
  4741. "SET NAMES 'UTF8';",
  4742. ];
  4743. case 'sqlsrv':
  4744. return [];
  4745. case 'sqlite':
  4746. return [
  4747. 'PRAGMA foreign_keys = on;',
  4748. ];
  4749. }
  4750. }
  4751. private function getOptions(): array
  4752. {
  4753. $options = array(
  4754. \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
  4755. \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
  4756. );
  4757. switch ($this->driver) {
  4758. case 'mysql':
  4759. return $options + [
  4760. \PDO::ATTR_EMULATE_PREPARES => false,
  4761. \PDO::MYSQL_ATTR_FOUND_ROWS => true,
  4762. \PDO::ATTR_PERSISTENT => true,
  4763. ];
  4764. case 'pgsql':
  4765. return $options + [
  4766. \PDO::ATTR_EMULATE_PREPARES => false,
  4767. \PDO::ATTR_PERSISTENT => true,
  4768. ];
  4769. case 'sqlsrv':
  4770. return $options + [
  4771. \PDO::SQLSRV_ATTR_DIRECT_QUERY => false,
  4772. \PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE => true,
  4773. ];
  4774. case 'sqlite':
  4775. return $options + [];
  4776. }
  4777. }
  4778. private function initPdo(): bool
  4779. {
  4780. if ($this->pdo) {
  4781. $result = $this->pdo->reconstruct($this->getDsn(), $this->username, $this->password, $this->getOptions());
  4782. } else {
  4783. $this->pdo = new LazyPdo($this->getDsn(), $this->username, $this->password, $this->getOptions());
  4784. $result = true;
  4785. }
  4786. $commands = $this->getCommands();
  4787. foreach ($commands as $command) {
  4788. $this->pdo->addInitCommand($command);
  4789. }
  4790. $this->reflection = new GenericReflection($this->pdo, $this->driver, $this->database, $this->tables);
  4791. $this->definition = new GenericDefinition($this->pdo, $this->driver, $this->database, $this->tables);
  4792. $this->conditions = new ConditionsBuilder($this->driver);
  4793. $this->columns = new ColumnsBuilder($this->driver);
  4794. $this->converter = new DataConverter($this->driver);
  4795. return $result;
  4796. }
  4797. public function __construct(string $driver, string $address, int $port, string $database, array $tables, string $username, string $password)
  4798. {
  4799. $this->driver = $driver;
  4800. $this->address = $address;
  4801. $this->port = $port;
  4802. $this->database = $database;
  4803. $this->tables = $tables;
  4804. $this->username = $username;
  4805. $this->password = $password;
  4806. $this->initPdo();
  4807. }
  4808. public function reconstruct(string $driver, string $address, int $port, string $database, array $tables, string $username, string $password): bool
  4809. {
  4810. if ($driver) {
  4811. $this->driver = $driver;
  4812. }
  4813. if ($address) {
  4814. $this->address = $address;
  4815. }
  4816. if ($port) {
  4817. $this->port = $port;
  4818. }
  4819. if ($database) {
  4820. $this->database = $database;
  4821. }
  4822. if ($tables) {
  4823. $this->tables = $tables;
  4824. }
  4825. if ($username) {
  4826. $this->username = $username;
  4827. }
  4828. if ($password) {
  4829. $this->password = $password;
  4830. }
  4831. return $this->initPdo();
  4832. }
  4833. public function pdo(): LazyPdo
  4834. {
  4835. return $this->pdo;
  4836. }
  4837. public function reflection(): GenericReflection
  4838. {
  4839. return $this->reflection;
  4840. }
  4841. public function definition(): GenericDefinition
  4842. {
  4843. return $this->definition;
  4844. }
  4845. public function beginTransaction() /*: void*/
  4846. {
  4847. $this->pdo->beginTransaction();
  4848. }
  4849. public function commitTransaction() /*: void*/
  4850. {
  4851. $this->pdo->commit();
  4852. }
  4853. public function rollBackTransaction() /*: void*/
  4854. {
  4855. $this->pdo->rollBack();
  4856. }
  4857. private function addMiddlewareConditions(string $tableName, Condition $condition): Condition
  4858. {
  4859. $condition1 = VariableStore::get("authorization.conditions.$tableName");
  4860. if ($condition1) {
  4861. $condition = $condition->_and($condition1);
  4862. }
  4863. $condition2 = VariableStore::get("multiTenancy.conditions.$tableName");
  4864. if ($condition2) {
  4865. $condition = $condition->_and($condition2);
  4866. }
  4867. return $condition;
  4868. }
  4869. public function createSingle(ReflectedTable $table, array $columnValues) /*: ?String*/
  4870. {
  4871. $this->converter->convertColumnValues($table, $columnValues);
  4872. $insertColumns = $this->columns->getInsert($table, $columnValues);
  4873. $tableName = $table->getName();
  4874. $pkName = $table->getPk()->getName();
  4875. $parameters = array_values($columnValues);
  4876. $sql = 'INSERT INTO "' . $tableName . '" ' . $insertColumns;
  4877. $stmt = $this->query($sql, $parameters);
  4878. // return primary key value if specified in the input
  4879. if (isset($columnValues[$pkName])) {
  4880. return $columnValues[$pkName];
  4881. }
  4882. // work around missing "returning" or "output" in mysql
  4883. switch ($this->driver) {
  4884. case 'mysql':
  4885. $stmt = $this->query('SELECT LAST_INSERT_ID()', []);
  4886. break;
  4887. case 'sqlite':
  4888. $stmt = $this->query('SELECT LAST_INSERT_ROWID()', []);
  4889. break;
  4890. }
  4891. $pkValue = $stmt->fetchColumn(0);
  4892. if ($this->driver == 'sqlsrv' && $table->getPk()->getType() == 'bigint') {
  4893. return (int) $pkValue;
  4894. }
  4895. if ($this->driver == 'sqlite' && in_array($table->getPk()->getType(), ['integer', 'bigint'])) {
  4896. return (int) $pkValue;
  4897. }
  4898. return $pkValue;
  4899. }
  4900. public function selectSingle(ReflectedTable $table, array $columnNames, string $id) /*: ?array*/
  4901. {
  4902. $selectColumns = $this->columns->getSelect($table, $columnNames);
  4903. $tableName = $table->getName();
  4904. $condition = new ColumnCondition($table->getPk(), 'eq', $id);
  4905. $condition = $this->addMiddlewareConditions($tableName, $condition);
  4906. $parameters = array();
  4907. $whereClause = $this->conditions->getWhereClause($condition, $parameters);
  4908. $sql = 'SELECT ' . $selectColumns . ' FROM "' . $tableName . '" ' . $whereClause;
  4909. $stmt = $this->query($sql, $parameters);
  4910. $record = $stmt->fetch() ?: null;
  4911. if ($record === null) {
  4912. return null;
  4913. }
  4914. $records = array($record);
  4915. $this->converter->convertRecords($table, $columnNames, $records);
  4916. return $records[0];
  4917. }
  4918. public function selectMultiple(ReflectedTable $table, array $columnNames, array $ids): array
  4919. {
  4920. if (count($ids) == 0) {
  4921. return [];
  4922. }
  4923. $selectColumns = $this->columns->getSelect($table, $columnNames);
  4924. $tableName = $table->getName();
  4925. $condition = new ColumnCondition($table->getPk(), 'in', implode(',', $ids));
  4926. $condition = $this->addMiddlewareConditions($tableName, $condition);
  4927. $parameters = array();
  4928. $whereClause = $this->conditions->getWhereClause($condition, $parameters);
  4929. $sql = 'SELECT ' . $selectColumns . ' FROM "' . $tableName . '" ' . $whereClause;
  4930. $stmt = $this->query($sql, $parameters);
  4931. $records = $stmt->fetchAll();
  4932. $this->converter->convertRecords($table, $columnNames, $records);
  4933. return $records;
  4934. }
  4935. public function selectCount(ReflectedTable $table, Condition $condition): int
  4936. {
  4937. $tableName = $table->getName();
  4938. $condition = $this->addMiddlewareConditions($tableName, $condition);
  4939. $parameters = array();
  4940. $whereClause = $this->conditions->getWhereClause($condition, $parameters);
  4941. $sql = 'SELECT COUNT(*) FROM "' . $tableName . '"' . $whereClause;
  4942. $stmt = $this->query($sql, $parameters);
  4943. return $stmt->fetchColumn(0);
  4944. }
  4945. public function selectAll(ReflectedTable $table, array $columnNames, Condition $condition, array $columnOrdering, int $offset, int $limit): array
  4946. {
  4947. if ($limit == 0) {
  4948. return array();
  4949. }
  4950. $selectColumns = $this->columns->getSelect($table, $columnNames);
  4951. $tableName = $table->getName();
  4952. $condition = $this->addMiddlewareConditions($tableName, $condition);
  4953. $parameters = array();
  4954. $whereClause = $this->conditions->getWhereClause($condition, $parameters);
  4955. $orderBy = $this->columns->getOrderBy($table, $columnOrdering);
  4956. $offsetLimit = $this->columns->getOffsetLimit($offset, $limit);
  4957. $sql = 'SELECT ' . $selectColumns . ' FROM "' . $tableName . '"' . $whereClause . $orderBy . $offsetLimit;
  4958. $stmt = $this->query($sql, $parameters);
  4959. $records = $stmt->fetchAll();
  4960. $this->converter->convertRecords($table, $columnNames, $records);
  4961. return $records;
  4962. }
  4963. public function updateSingle(ReflectedTable $table, array $columnValues, string $id)
  4964. {
  4965. if (count($columnValues) == 0) {
  4966. return 0;
  4967. }
  4968. $this->converter->convertColumnValues($table, $columnValues);
  4969. $updateColumns = $this->columns->getUpdate($table, $columnValues);
  4970. $tableName = $table->getName();
  4971. $condition = new ColumnCondition($table->getPk(), 'eq', $id);
  4972. $condition = $this->addMiddlewareConditions($tableName, $condition);
  4973. $parameters = array_values($columnValues);
  4974. $whereClause = $this->conditions->getWhereClause($condition, $parameters);
  4975. $sql = 'UPDATE "' . $tableName . '" SET ' . $updateColumns . $whereClause;
  4976. $stmt = $this->query($sql, $parameters);
  4977. return $stmt->rowCount();
  4978. }
  4979. public function deleteSingle(ReflectedTable $table, string $id)
  4980. {
  4981. $tableName = $table->getName();
  4982. $condition = new ColumnCondition($table->getPk(), 'eq', $id);
  4983. $condition = $this->addMiddlewareConditions($tableName, $condition);
  4984. $parameters = array();
  4985. $whereClause = $this->conditions->getWhereClause($condition, $parameters);
  4986. $sql = 'DELETE FROM "' . $tableName . '" ' . $whereClause;
  4987. $stmt = $this->query($sql, $parameters);
  4988. return $stmt->rowCount();
  4989. }
  4990. public function incrementSingle(ReflectedTable $table, array $columnValues, string $id)
  4991. {
  4992. if (count($columnValues) == 0) {
  4993. return 0;
  4994. }
  4995. $this->converter->convertColumnValues($table, $columnValues);
  4996. $updateColumns = $this->columns->getIncrement($table, $columnValues);
  4997. $tableName = $table->getName();
  4998. $condition = new ColumnCondition($table->getPk(), 'eq', $id);
  4999. $condition = $this->addMiddlewareConditions($tableName, $condition);
  5000. $parameters = array_values($columnValues);
  5001. $whereClause = $this->conditions->getWhereClause($condition, $parameters);
  5002. $sql = 'UPDATE "' . $tableName . '" SET ' . $updateColumns . $whereClause;
  5003. $stmt = $this->query($sql, $parameters);
  5004. return $stmt->rowCount();
  5005. }
  5006. private function query(string $sql, array $parameters): \PDOStatement
  5007. {
  5008. $stmt = $this->pdo->prepare($sql);
  5009. //echo "- $sql -- " . json_encode($parameters, JSON_UNESCAPED_UNICODE) . "\n";
  5010. $stmt->execute($parameters);
  5011. return $stmt;
  5012. }
  5013. public function getCacheKey(): string
  5014. {
  5015. return md5(json_encode([
  5016. $this->driver,
  5017. $this->address,
  5018. $this->port,
  5019. $this->database,
  5020. $this->tables,
  5021. $this->username,
  5022. ]));
  5023. }
  5024. }
  5025. }
  5026. // file: src/Tqdev/PhpCrudApi/Database/GenericDefinition.php
  5027. namespace Tqdev\PhpCrudApi\Database {
  5028. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedColumn;
  5029. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
  5030. use Tqdev\PhpCrudApi\Database\LazyPdo;
  5031. class GenericDefinition
  5032. {
  5033. private $pdo;
  5034. private $driver;
  5035. private $database;
  5036. private $typeConverter;
  5037. private $reflection;
  5038. public function __construct(LazyPdo $pdo, string $driver, string $database, array $tables)
  5039. {
  5040. $this->pdo = $pdo;
  5041. $this->driver = $driver;
  5042. $this->database = $database;
  5043. $this->typeConverter = new TypeConverter($driver);
  5044. $this->reflection = new GenericReflection($pdo, $driver, $database, $tables);
  5045. }
  5046. private function quote(string $identifier): string
  5047. {
  5048. return '"' . str_replace('"', '', $identifier) . '"';
  5049. }
  5050. public function getColumnType(ReflectedColumn $column, bool $update): string
  5051. {
  5052. if ($this->driver == 'pgsql' && !$update && $column->getPk() && $this->canAutoIncrement($column)) {
  5053. return 'serial';
  5054. }
  5055. $type = $this->typeConverter->fromJdbc($column->getType());
  5056. if ($column->hasPrecision() && $column->hasScale()) {
  5057. $size = '(' . $column->getPrecision() . ',' . $column->getScale() . ')';
  5058. } elseif ($column->hasPrecision()) {
  5059. $size = '(' . $column->getPrecision() . ')';
  5060. } elseif ($column->hasLength()) {
  5061. $size = '(' . $column->getLength() . ')';
  5062. } else {
  5063. $size = '';
  5064. }
  5065. $null = $this->getColumnNullType($column, $update);
  5066. $auto = $this->getColumnAutoIncrement($column, $update);
  5067. return $type . $size . $null . $auto;
  5068. }
  5069. private function getPrimaryKey(string $tableName): string
  5070. {
  5071. $pks = $this->reflection->getTablePrimaryKeys($tableName);
  5072. if (count($pks) == 1) {
  5073. return $pks[0];
  5074. }
  5075. return "";
  5076. }
  5077. private function canAutoIncrement(ReflectedColumn $column): bool
  5078. {
  5079. return in_array($column->getType(), ['integer', 'bigint']);
  5080. }
  5081. private function getColumnAutoIncrement(ReflectedColumn $column, bool $update): string
  5082. {
  5083. if (!$this->canAutoIncrement($column)) {
  5084. return '';
  5085. }
  5086. switch ($this->driver) {
  5087. case 'mysql':
  5088. return $column->getPk() ? ' AUTO_INCREMENT' : '';
  5089. case 'pgsql':
  5090. case 'sqlsrv':
  5091. return $column->getPk() ? ' IDENTITY(1,1)' : '';
  5092. case 'sqlite':
  5093. return $column->getPk() ? ' AUTOINCREMENT' : '';
  5094. }
  5095. }
  5096. private function getColumnNullType(ReflectedColumn $column, bool $update): string
  5097. {
  5098. if ($this->driver == 'pgsql' && $update) {
  5099. return '';
  5100. }
  5101. return $column->getNullable() ? ' NULL' : ' NOT NULL';
  5102. }
  5103. private function getTableRenameSQL(string $tableName, string $newTableName): string
  5104. {
  5105. $p1 = $this->quote($tableName);
  5106. $p2 = $this->quote($newTableName);
  5107. switch ($this->driver) {
  5108. case 'mysql':
  5109. return "RENAME TABLE $p1 TO $p2";
  5110. case 'pgsql':
  5111. return "ALTER TABLE $p1 RENAME TO $p2";
  5112. case 'sqlsrv':
  5113. return "EXEC sp_rename $p1, $p2";
  5114. case 'sqlite':
  5115. return "ALTER TABLE $p1 RENAME TO $p2";
  5116. }
  5117. }
  5118. private function getColumnRenameSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  5119. {
  5120. $p1 = $this->quote($tableName);
  5121. $p2 = $this->quote($columnName);
  5122. $p3 = $this->quote($newColumn->getName());
  5123. switch ($this->driver) {
  5124. case 'mysql':
  5125. $p4 = $this->getColumnType($newColumn, true);
  5126. return "ALTER TABLE $p1 CHANGE $p2 $p3 $p4";
  5127. case 'pgsql':
  5128. return "ALTER TABLE $p1 RENAME COLUMN $p2 TO $p3";
  5129. case 'sqlsrv':
  5130. $p4 = $this->quote($tableName . '.' . $columnName);
  5131. return "EXEC sp_rename $p4, $p3, 'COLUMN'";
  5132. case 'sqlite':
  5133. return "ALTER TABLE $p1 RENAME COLUMN $p2 TO $p3";
  5134. }
  5135. }
  5136. private function getColumnRetypeSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  5137. {
  5138. $p1 = $this->quote($tableName);
  5139. $p2 = $this->quote($columnName);
  5140. $p3 = $this->quote($newColumn->getName());
  5141. $p4 = $this->getColumnType($newColumn, true);
  5142. switch ($this->driver) {
  5143. case 'mysql':
  5144. return "ALTER TABLE $p1 CHANGE $p2 $p3 $p4";
  5145. case 'pgsql':
  5146. return "ALTER TABLE $p1 ALTER COLUMN $p3 TYPE $p4";
  5147. case 'sqlsrv':
  5148. return "ALTER TABLE $p1 ALTER COLUMN $p3 $p4";
  5149. }
  5150. }
  5151. private function getSetColumnNullableSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  5152. {
  5153. $p1 = $this->quote($tableName);
  5154. $p2 = $this->quote($columnName);
  5155. $p3 = $this->quote($newColumn->getName());
  5156. $p4 = $this->getColumnType($newColumn, true);
  5157. switch ($this->driver) {
  5158. case 'mysql':
  5159. return "ALTER TABLE $p1 CHANGE $p2 $p3 $p4";
  5160. case 'pgsql':
  5161. $p5 = $newColumn->getNullable() ? 'DROP NOT NULL' : 'SET NOT NULL';
  5162. return "ALTER TABLE $p1 ALTER COLUMN $p2 $p5";
  5163. case 'sqlsrv':
  5164. return "ALTER TABLE $p1 ALTER COLUMN $p2 $p4";
  5165. }
  5166. }
  5167. private function getSetColumnPkConstraintSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  5168. {
  5169. $p1 = $this->quote($tableName);
  5170. $p2 = $this->quote($columnName);
  5171. $p3 = $this->quote($tableName . '_pkey');
  5172. switch ($this->driver) {
  5173. case 'mysql':
  5174. $p4 = $newColumn->getPk() ? "ADD PRIMARY KEY ($p2)" : 'DROP PRIMARY KEY';
  5175. return "ALTER TABLE $p1 $p4";
  5176. case 'pgsql':
  5177. case 'sqlsrv':
  5178. $p4 = $newColumn->getPk() ? "ADD CONSTRAINT $p3 PRIMARY KEY ($p2)" : "DROP CONSTRAINT $p3";
  5179. return "ALTER TABLE $p1 $p4";
  5180. }
  5181. }
  5182. private function getSetColumnPkSequenceSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  5183. {
  5184. $p1 = $this->quote($tableName);
  5185. $p2 = $this->quote($columnName);
  5186. $p3 = $this->quote($tableName . '_' . $columnName . '_seq');
  5187. switch ($this->driver) {
  5188. case 'mysql':
  5189. return "select 1";
  5190. case 'pgsql':
  5191. return $newColumn->getPk() ? "CREATE SEQUENCE $p3 OWNED BY $p1.$p2" : "DROP SEQUENCE $p3";
  5192. case 'sqlsrv':
  5193. return $newColumn->getPk() ? "CREATE SEQUENCE $p3" : "DROP SEQUENCE $p3";
  5194. }
  5195. }
  5196. private function getSetColumnPkSequenceStartSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  5197. {
  5198. $p1 = $this->quote($tableName);
  5199. $p2 = $this->quote($columnName);
  5200. switch ($this->driver) {
  5201. case 'mysql':
  5202. return "select 1";
  5203. case 'pgsql':
  5204. $p3 = $this->pdo->quote($tableName . '_' . $columnName . '_seq');
  5205. return "SELECT setval($p3, (SELECT max($p2)+1 FROM $p1));";
  5206. case 'sqlsrv':
  5207. $p3 = $this->quote($tableName . '_' . $columnName . '_seq');
  5208. $p4 = $this->pdo->query("SELECT max($p2)+1 FROM $p1")->fetchColumn();
  5209. return "ALTER SEQUENCE $p3 RESTART WITH $p4";
  5210. }
  5211. }
  5212. private function getSetColumnPkDefaultSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  5213. {
  5214. $p1 = $this->quote($tableName);
  5215. $p2 = $this->quote($columnName);
  5216. switch ($this->driver) {
  5217. case 'mysql':
  5218. $p3 = $this->quote($newColumn->getName());
  5219. $p4 = $this->getColumnType($newColumn, true);
  5220. return "ALTER TABLE $p1 CHANGE $p2 $p3 $p4";
  5221. case 'pgsql':
  5222. if ($newColumn->getPk()) {
  5223. $p3 = $this->pdo->quote($tableName . '_' . $columnName . '_seq');
  5224. $p4 = "SET DEFAULT nextval($p3)";
  5225. } else {
  5226. $p4 = 'DROP DEFAULT';
  5227. }
  5228. return "ALTER TABLE $p1 ALTER COLUMN $p2 $p4";
  5229. case 'sqlsrv':
  5230. $p3 = $this->quote($tableName . '_' . $columnName . '_seq');
  5231. $p4 = $this->quote($tableName . '_' . $columnName . '_def');
  5232. if ($newColumn->getPk()) {
  5233. return "ALTER TABLE $p1 ADD CONSTRAINT $p4 DEFAULT NEXT VALUE FOR $p3 FOR $p2";
  5234. } else {
  5235. return "ALTER TABLE $p1 DROP CONSTRAINT $p4";
  5236. }
  5237. }
  5238. }
  5239. private function getAddColumnFkConstraintSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  5240. {
  5241. $p1 = $this->quote($tableName);
  5242. $p2 = $this->quote($columnName);
  5243. $p3 = $this->quote($tableName . '_' . $columnName . '_fkey');
  5244. $p4 = $this->quote($newColumn->getFk());
  5245. $p5 = $this->quote($this->getPrimaryKey($newColumn->getFk()));
  5246. return "ALTER TABLE $p1 ADD CONSTRAINT $p3 FOREIGN KEY ($p2) REFERENCES $p4 ($p5)";
  5247. }
  5248. private function getRemoveColumnFkConstraintSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  5249. {
  5250. $p1 = $this->quote($tableName);
  5251. $p2 = $this->quote($tableName . '_' . $columnName . '_fkey');
  5252. switch ($this->driver) {
  5253. case 'mysql':
  5254. return "ALTER TABLE $p1 DROP FOREIGN KEY $p2";
  5255. case 'pgsql':
  5256. case 'sqlsrv':
  5257. return "ALTER TABLE $p1 DROP CONSTRAINT $p2";
  5258. }
  5259. }
  5260. private function getAddTableSQL(ReflectedTable $newTable): string
  5261. {
  5262. $tableName = $newTable->getName();
  5263. $p1 = $this->quote($tableName);
  5264. $fields = [];
  5265. $constraints = [];
  5266. foreach ($newTable->getColumnNames() as $columnName) {
  5267. $pkColumn = $this->getPrimaryKey($tableName);
  5268. $newColumn = $newTable->getColumn($columnName);
  5269. $f1 = $this->quote($columnName);
  5270. $f2 = $this->getColumnType($newColumn, false);
  5271. $f3 = $this->quote($tableName . '_' . $columnName . '_fkey');
  5272. $f4 = $this->quote($newColumn->getFk());
  5273. $f5 = $this->quote($this->getPrimaryKey($newColumn->getFk()));
  5274. $f6 = $this->quote($tableName . '_' . $pkColumn . '_pkey');
  5275. if ($this->driver == 'sqlite') {
  5276. if ($newColumn->getPk()) {
  5277. $f2 = str_replace('NULL', 'NULL PRIMARY KEY', $f2);
  5278. }
  5279. $fields[] = "$f1 $f2";
  5280. if ($newColumn->getFk()) {
  5281. $constraints[] = "FOREIGN KEY ($f1) REFERENCES $f4 ($f5)";
  5282. }
  5283. } else {
  5284. $fields[] = "$f1 $f2";
  5285. if ($newColumn->getPk()) {
  5286. $constraints[] = "CONSTRAINT $f6 PRIMARY KEY ($f1)";
  5287. }
  5288. if ($newColumn->getFk()) {
  5289. $constraints[] = "CONSTRAINT $f3 FOREIGN KEY ($f1) REFERENCES $f4 ($f5)";
  5290. }
  5291. }
  5292. }
  5293. $p2 = implode(',', array_merge($fields, $constraints));
  5294. return "CREATE TABLE $p1 ($p2);";
  5295. }
  5296. private function getAddColumnSQL(string $tableName, ReflectedColumn $newColumn): string
  5297. {
  5298. $p1 = $this->quote($tableName);
  5299. $p2 = $this->quote($newColumn->getName());
  5300. $p3 = $this->getColumnType($newColumn, false);
  5301. switch ($this->driver) {
  5302. case 'mysql':
  5303. case 'pgsql':
  5304. return "ALTER TABLE $p1 ADD COLUMN $p2 $p3";
  5305. case 'sqlsrv':
  5306. return "ALTER TABLE $p1 ADD $p2 $p3";
  5307. case 'sqlite':
  5308. return "ALTER TABLE $p1 ADD COLUMN $p2 $p3";
  5309. }
  5310. }
  5311. private function getRemoveTableSQL(string $tableName): string
  5312. {
  5313. $p1 = $this->quote($tableName);
  5314. switch ($this->driver) {
  5315. case 'mysql':
  5316. case 'pgsql':
  5317. return "DROP TABLE $p1 CASCADE;";
  5318. case 'sqlsrv':
  5319. return "DROP TABLE $p1;";
  5320. case 'sqlite':
  5321. return "DROP TABLE $p1;";
  5322. }
  5323. }
  5324. private function getRemoveColumnSQL(string $tableName, string $columnName): string
  5325. {
  5326. $p1 = $this->quote($tableName);
  5327. $p2 = $this->quote($columnName);
  5328. switch ($this->driver) {
  5329. case 'mysql':
  5330. case 'pgsql':
  5331. return "ALTER TABLE $p1 DROP COLUMN $p2 CASCADE;";
  5332. case 'sqlsrv':
  5333. return "ALTER TABLE $p1 DROP COLUMN $p2;";
  5334. case 'sqlite':
  5335. return "ALTER TABLE $p1 DROP COLUMN $p2;";
  5336. }
  5337. }
  5338. public function renameTable(string $tableName, string $newTableName)
  5339. {
  5340. $sql = $this->getTableRenameSQL($tableName, $newTableName);
  5341. return $this->query($sql, []);
  5342. }
  5343. public function renameColumn(string $tableName, string $columnName, ReflectedColumn $newColumn)
  5344. {
  5345. $sql = $this->getColumnRenameSQL($tableName, $columnName, $newColumn);
  5346. return $this->query($sql, []);
  5347. }
  5348. public function retypeColumn(string $tableName, string $columnName, ReflectedColumn $newColumn)
  5349. {
  5350. $sql = $this->getColumnRetypeSQL($tableName, $columnName, $newColumn);
  5351. return $this->query($sql, []);
  5352. }
  5353. public function setColumnNullable(string $tableName, string $columnName, ReflectedColumn $newColumn)
  5354. {
  5355. $sql = $this->getSetColumnNullableSQL($tableName, $columnName, $newColumn);
  5356. return $this->query($sql, []);
  5357. }
  5358. public function addColumnPrimaryKey(string $tableName, string $columnName, ReflectedColumn $newColumn)
  5359. {
  5360. $sql = $this->getSetColumnPkConstraintSQL($tableName, $columnName, $newColumn);
  5361. $this->query($sql, []);
  5362. if ($this->canAutoIncrement($newColumn)) {
  5363. $sql = $this->getSetColumnPkSequenceSQL($tableName, $columnName, $newColumn);
  5364. $this->query($sql, []);
  5365. $sql = $this->getSetColumnPkSequenceStartSQL($tableName, $columnName, $newColumn);
  5366. $this->query($sql, []);
  5367. $sql = $this->getSetColumnPkDefaultSQL($tableName, $columnName, $newColumn);
  5368. $this->query($sql, []);
  5369. }
  5370. return true;
  5371. }
  5372. public function removeColumnPrimaryKey(string $tableName, string $columnName, ReflectedColumn $newColumn)
  5373. {
  5374. if ($this->canAutoIncrement($newColumn)) {
  5375. $sql = $this->getSetColumnPkDefaultSQL($tableName, $columnName, $newColumn);
  5376. $this->query($sql, []);
  5377. $sql = $this->getSetColumnPkSequenceSQL($tableName, $columnName, $newColumn);
  5378. $this->query($sql, []);
  5379. }
  5380. $sql = $this->getSetColumnPkConstraintSQL($tableName, $columnName, $newColumn);
  5381. $this->query($sql, []);
  5382. return true;
  5383. }
  5384. public function addColumnForeignKey(string $tableName, string $columnName, ReflectedColumn $newColumn)
  5385. {
  5386. $sql = $this->getAddColumnFkConstraintSQL($tableName, $columnName, $newColumn);
  5387. return $this->query($sql, []);
  5388. }
  5389. public function removeColumnForeignKey(string $tableName, string $columnName, ReflectedColumn $newColumn)
  5390. {
  5391. $sql = $this->getRemoveColumnFkConstraintSQL($tableName, $columnName, $newColumn);
  5392. return $this->query($sql, []);
  5393. }
  5394. public function addTable(ReflectedTable $newTable)
  5395. {
  5396. $sql = $this->getAddTableSQL($newTable);
  5397. return $this->query($sql, []);
  5398. }
  5399. public function addColumn(string $tableName, ReflectedColumn $newColumn)
  5400. {
  5401. $sql = $this->getAddColumnSQL($tableName, $newColumn);
  5402. return $this->query($sql, []);
  5403. }
  5404. public function removeTable(string $tableName)
  5405. {
  5406. $sql = $this->getRemoveTableSQL($tableName);
  5407. return $this->query($sql, []);
  5408. }
  5409. public function removeColumn(string $tableName, string $columnName)
  5410. {
  5411. $sql = $this->getRemoveColumnSQL($tableName, $columnName);
  5412. return $this->query($sql, []);
  5413. }
  5414. private function query(string $sql, array $arguments): bool
  5415. {
  5416. $stmt = $this->pdo->prepare($sql);
  5417. // echo "- $sql -- " . json_encode($arguments) . "\n";
  5418. return $stmt->execute($arguments);
  5419. }
  5420. }
  5421. }
  5422. // file: src/Tqdev/PhpCrudApi/Database/GenericReflection.php
  5423. namespace Tqdev\PhpCrudApi\Database {
  5424. use Tqdev\PhpCrudApi\Database\LazyPdo;
  5425. class GenericReflection
  5426. {
  5427. private $pdo;
  5428. private $driver;
  5429. private $database;
  5430. private $tables;
  5431. private $typeConverter;
  5432. public function __construct(LazyPdo $pdo, string $driver, string $database, array $tables)
  5433. {
  5434. $this->pdo = $pdo;
  5435. $this->driver = $driver;
  5436. $this->database = $database;
  5437. $this->tables = $tables;
  5438. $this->typeConverter = new TypeConverter($driver);
  5439. }
  5440. public function getIgnoredTables(): array
  5441. {
  5442. switch ($this->driver) {
  5443. case 'mysql':
  5444. return [];
  5445. case 'pgsql':
  5446. return ['spatial_ref_sys', 'raster_columns', 'raster_overviews', 'geography_columns', 'geometry_columns'];
  5447. case 'sqlsrv':
  5448. return [];
  5449. case 'sqlite':
  5450. return [];
  5451. }
  5452. }
  5453. private function getTablesSQL(): string
  5454. {
  5455. switch ($this->driver) {
  5456. case 'mysql':
  5457. return 'SELECT "TABLE_NAME", "TABLE_TYPE" FROM "INFORMATION_SCHEMA"."TABLES" WHERE "TABLE_TYPE" IN (\'BASE TABLE\' , \'VIEW\') AND "TABLE_SCHEMA" = ? ORDER BY BINARY "TABLE_NAME"';
  5458. case 'pgsql':
  5459. return 'SELECT c.relname as "TABLE_NAME", c.relkind as "TABLE_TYPE" FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN (\'r\', \'v\') AND n.nspname <> \'pg_catalog\' AND n.nspname <> \'information_schema\' AND n.nspname !~ \'^pg_toast\' AND pg_catalog.pg_table_is_visible(c.oid) AND \'\' <> ? ORDER BY "TABLE_NAME";';
  5460. case 'sqlsrv':
  5461. return 'SELECT o.name as "TABLE_NAME", o.xtype as "TABLE_TYPE" FROM sysobjects o WHERE o.xtype IN (\'U\', \'V\') ORDER BY "TABLE_NAME"';
  5462. case 'sqlite':
  5463. return 'SELECT t.name as "TABLE_NAME", t.type as "TABLE_TYPE" FROM sqlite_master t WHERE t.type IN (\'table\', \'view\') AND \'\' <> ? ORDER BY "TABLE_NAME"';
  5464. }
  5465. }
  5466. private function getTableColumnsSQL(): string
  5467. {
  5468. switch ($this->driver) {
  5469. case 'mysql':
  5470. return 'SELECT "COLUMN_NAME", "IS_NULLABLE", "DATA_TYPE", "CHARACTER_MAXIMUM_LENGTH" as "CHARACTER_MAXIMUM_LENGTH", "NUMERIC_PRECISION", "NUMERIC_SCALE", "COLUMN_TYPE" FROM "INFORMATION_SCHEMA"."COLUMNS" WHERE "TABLE_NAME" = ? AND "TABLE_SCHEMA" = ? ORDER BY "ORDINAL_POSITION"';
  5471. case 'pgsql':
  5472. return 'SELECT a.attname AS "COLUMN_NAME", case when a.attnotnull then \'NO\' else \'YES\' end as "IS_NULLABLE", pg_catalog.format_type(a.atttypid, -1) as "DATA_TYPE", case when a.atttypmod < 0 then NULL else a.atttypmod-4 end as "CHARACTER_MAXIMUM_LENGTH", case when a.atttypid != 1700 then NULL else ((a.atttypmod - 4) >> 16) & 65535 end as "NUMERIC_PRECISION", case when a.atttypid != 1700 then NULL else (a.atttypmod - 4) & 65535 end as "NUMERIC_SCALE", \'\' AS "COLUMN_TYPE" FROM pg_attribute a JOIN pg_class pgc ON pgc.oid = a.attrelid WHERE pgc.relname = ? AND \'\' <> ? AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum;';
  5473. case 'sqlsrv':
  5474. return 'SELECT c.name AS "COLUMN_NAME", c.is_nullable AS "IS_NULLABLE", t.Name AS "DATA_TYPE", (c.max_length/2) AS "CHARACTER_MAXIMUM_LENGTH", c.precision AS "NUMERIC_PRECISION", c.scale AS "NUMERIC_SCALE", \'\' AS "COLUMN_TYPE" FROM sys.columns c INNER JOIN sys.types t ON c.user_type_id = t.user_type_id WHERE c.object_id = OBJECT_ID(?) AND \'\' <> ? ORDER BY c.column_id';
  5475. case 'sqlite':
  5476. return 'SELECT "name" AS "COLUMN_NAME", case when "notnull"==1 then \'no\' else \'yes\' end as "IS_NULLABLE", lower("type") AS "DATA_TYPE", 2147483647 AS "CHARACTER_MAXIMUM_LENGTH", 0 AS "NUMERIC_PRECISION", 0 AS "NUMERIC_SCALE", \'\' AS "COLUMN_TYPE" FROM pragma_table_info(?) WHERE \'\' <> ? ORDER BY "cid"';
  5477. }
  5478. }
  5479. private function getTablePrimaryKeysSQL(): string
  5480. {
  5481. switch ($this->driver) {
  5482. case 'mysql':
  5483. return 'SELECT "COLUMN_NAME" FROM "INFORMATION_SCHEMA"."KEY_COLUMN_USAGE" WHERE "CONSTRAINT_NAME" = \'PRIMARY\' AND "TABLE_NAME" = ? AND "TABLE_SCHEMA" = ?';
  5484. case 'pgsql':
  5485. return 'SELECT a.attname AS "COLUMN_NAME" FROM pg_attribute a JOIN pg_constraint c ON (c.conrelid, c.conkey[1]) = (a.attrelid, a.attnum) JOIN pg_class pgc ON pgc.oid = a.attrelid WHERE pgc.relname = ? AND \'\' <> ? AND c.contype = \'p\'';
  5486. case 'sqlsrv':
  5487. return 'SELECT c.NAME as "COLUMN_NAME" FROM sys.key_constraints kc inner join sys.objects t on t.object_id = kc.parent_object_id INNER JOIN sys.index_columns ic ON kc.parent_object_id = ic.object_id and kc.unique_index_id = ic.index_id INNER JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id WHERE kc.type = \'PK\' and t.object_id = OBJECT_ID(?) and \'\' <> ?';
  5488. case 'sqlite':
  5489. return 'SELECT "name" as "COLUMN_NAME" FROM pragma_table_info(?) WHERE "pk"=1 AND \'\' <> ?';
  5490. }
  5491. }
  5492. private function getTableForeignKeysSQL(): string
  5493. {
  5494. switch ($this->driver) {
  5495. case 'mysql':
  5496. return 'SELECT "COLUMN_NAME", "REFERENCED_TABLE_NAME" FROM "INFORMATION_SCHEMA"."KEY_COLUMN_USAGE" WHERE "REFERENCED_TABLE_NAME" IS NOT NULL AND "TABLE_NAME" = ? AND "TABLE_SCHEMA" = ?';
  5497. case 'pgsql':
  5498. return 'SELECT a.attname AS "COLUMN_NAME", c.confrelid::regclass::text AS "REFERENCED_TABLE_NAME" FROM pg_attribute a JOIN pg_constraint c ON (c.conrelid, c.conkey[1]) = (a.attrelid, a.attnum) JOIN pg_class pgc ON pgc.oid = a.attrelid WHERE pgc.relname = ? AND \'\' <> ? AND c.contype = \'f\'';
  5499. case 'sqlsrv':
  5500. return 'SELECT COL_NAME(fc.parent_object_id, fc.parent_column_id) AS "COLUMN_NAME", OBJECT_NAME (f.referenced_object_id) AS "REFERENCED_TABLE_NAME" FROM sys.foreign_keys AS f INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id WHERE f.parent_object_id = OBJECT_ID(?) and \'\' <> ?';
  5501. case 'sqlite':
  5502. return 'SELECT "from" AS "COLUMN_NAME", "table" AS "REFERENCED_TABLE_NAME" FROM pragma_foreign_key_list(?) WHERE \'\' <> ?';
  5503. }
  5504. }
  5505. public function getDatabaseName(): string
  5506. {
  5507. return $this->database;
  5508. }
  5509. public function getTables(): array
  5510. {
  5511. $sql = $this->getTablesSQL();
  5512. $results = $this->query($sql, [$this->database]);
  5513. $tables = $this->tables;
  5514. $results = array_filter($results, function ($v) use ($tables) {
  5515. return !$tables || in_array($v['TABLE_NAME'], $tables);
  5516. });
  5517. foreach ($results as &$result) {
  5518. $map = [];
  5519. switch ($this->driver) {
  5520. case 'mysql':
  5521. $map = ['BASE TABLE' => 'table', 'VIEW' => 'view'];
  5522. break;
  5523. case 'pgsql':
  5524. $map = ['r' => 'table', 'v' => 'view'];
  5525. break;
  5526. case 'sqlsrv':
  5527. $map = ['U' => 'table', 'V' => 'view'];
  5528. break;
  5529. case 'sqlite':
  5530. $map = ['table' => 'table', 'view' => 'view'];
  5531. break;
  5532. }
  5533. $result['TABLE_TYPE'] = $map[trim($result['TABLE_TYPE'])];
  5534. }
  5535. return $results;
  5536. }
  5537. public function getTableColumns(string $tableName, string $type): array
  5538. {
  5539. $sql = $this->getTableColumnsSQL();
  5540. $results = $this->query($sql, [$tableName, $this->database]);
  5541. if ($type == 'view') {
  5542. foreach ($results as &$result) {
  5543. $result['IS_NULLABLE'] = false;
  5544. }
  5545. }
  5546. if ($this->driver == 'mysql') {
  5547. foreach ($results as &$result) {
  5548. // mysql does not properly reflect display width of types
  5549. preg_match('|([a-z]+)(\(([0-9]+)(,([0-9]+))?\))?|', $result['DATA_TYPE'], $matches);
  5550. $result['DATA_TYPE'] = $matches[1];
  5551. if (!$result['CHARACTER_MAXIMUM_LENGTH']) {
  5552. if (isset($matches[3])) {
  5553. $result['NUMERIC_PRECISION'] = $matches[3];
  5554. }
  5555. if (isset($matches[5])) {
  5556. $result['NUMERIC_SCALE'] = $matches[5];
  5557. }
  5558. }
  5559. }
  5560. }
  5561. if ($this->driver == 'sqlite') {
  5562. foreach ($results as &$result) {
  5563. // sqlite does not properly reflect display width of types
  5564. preg_match('|([a-z]+)(\(([0-9]+)(,([0-9]+))?\))?|', $result['DATA_TYPE'], $matches);
  5565. if (isset($matches[1])) {
  5566. $result['DATA_TYPE'] = $matches[1];
  5567. } else {
  5568. $result['DATA_TYPE'] = 'integer';
  5569. }
  5570. if (isset($matches[5])) {
  5571. $result['NUMERIC_PRECISION'] = $matches[3];
  5572. $result['NUMERIC_SCALE'] = $matches[5];
  5573. } else if (isset($matches[3])) {
  5574. $result['CHARACTER_MAXIMUM_LENGTH'] = $matches[3];
  5575. }
  5576. }
  5577. }
  5578. return $results;
  5579. }
  5580. public function getTablePrimaryKeys(string $tableName): array
  5581. {
  5582. $sql = $this->getTablePrimaryKeysSQL();
  5583. $results = $this->query($sql, [$tableName, $this->database]);
  5584. $primaryKeys = [];
  5585. foreach ($results as $result) {
  5586. $primaryKeys[] = $result['COLUMN_NAME'];
  5587. }
  5588. return $primaryKeys;
  5589. }
  5590. public function getTableForeignKeys(string $tableName): array
  5591. {
  5592. $sql = $this->getTableForeignKeysSQL();
  5593. $results = $this->query($sql, [$tableName, $this->database]);
  5594. $foreignKeys = [];
  5595. foreach ($results as $result) {
  5596. $foreignKeys[$result['COLUMN_NAME']] = $result['REFERENCED_TABLE_NAME'];
  5597. }
  5598. return $foreignKeys;
  5599. }
  5600. public function toJdbcType(string $type, string $size): string
  5601. {
  5602. return $this->typeConverter->toJdbc($type, $size);
  5603. }
  5604. private function query(string $sql, array $parameters): array
  5605. {
  5606. $stmt = $this->pdo->prepare($sql);
  5607. //echo "- $sql -- " . json_encode($parameters, JSON_UNESCAPED_UNICODE) . "\n";
  5608. $stmt->execute($parameters);
  5609. return $stmt->fetchAll();
  5610. }
  5611. }
  5612. }
  5613. // file: src/Tqdev/PhpCrudApi/Database/LazyPdo.php
  5614. namespace Tqdev\PhpCrudApi\Database {
  5615. class LazyPdo extends \PDO
  5616. {
  5617. private $dsn;
  5618. private $user;
  5619. private $password;
  5620. private $options;
  5621. private $commands;
  5622. private $pdo = null;
  5623. public function __construct(string $dsn, /*?string*/ $user = null, /*?string*/ $password = null, array $options = array())
  5624. {
  5625. $this->dsn = $dsn;
  5626. $this->user = $user;
  5627. $this->password = $password;
  5628. $this->options = $options;
  5629. $this->commands = array();
  5630. // explicitly NOT calling super::__construct
  5631. }
  5632. public function addInitCommand(string $command)/*: void*/
  5633. {
  5634. $this->commands[] = $command;
  5635. }
  5636. private function pdo()
  5637. {
  5638. if (!$this->pdo) {
  5639. $this->pdo = new \PDO($this->dsn, $this->user, $this->password, $this->options);
  5640. foreach ($this->commands as $command) {
  5641. $this->pdo->query($command);
  5642. }
  5643. }
  5644. return $this->pdo;
  5645. }
  5646. public function reconstruct(string $dsn, /*?string*/ $user = null, /*?string*/ $password = null, array $options = array()): bool
  5647. {
  5648. $this->dsn = $dsn;
  5649. $this->user = $user;
  5650. $this->password = $password;
  5651. $this->options = $options;
  5652. $this->commands = array();
  5653. if ($this->pdo) {
  5654. $this->pdo = null;
  5655. return true;
  5656. }
  5657. return false;
  5658. }
  5659. public function inTransaction(): bool
  5660. {
  5661. // Do not call parent method if there is no pdo object
  5662. return $this->pdo && parent::inTransaction();
  5663. }
  5664. public function setAttribute($attribute, $value): bool
  5665. {
  5666. if ($this->pdo) {
  5667. return $this->pdo()->setAttribute($attribute, $value);
  5668. }
  5669. $this->options[$attribute] = $value;
  5670. return true;
  5671. }
  5672. public function getAttribute($attribute): mixed
  5673. {
  5674. return $this->pdo()->getAttribute($attribute);
  5675. }
  5676. public function beginTransaction(): bool
  5677. {
  5678. return $this->pdo()->beginTransaction();
  5679. }
  5680. public function commit(): bool
  5681. {
  5682. return $this->pdo()->commit();
  5683. }
  5684. public function rollBack(): bool
  5685. {
  5686. return $this->pdo()->rollBack();
  5687. }
  5688. public function errorCode(): mixed
  5689. {
  5690. return $this->pdo()->errorCode();
  5691. }
  5692. public function errorInfo(): array
  5693. {
  5694. return $this->pdo()->errorInfo();
  5695. }
  5696. public function exec($query): int
  5697. {
  5698. return $this->pdo()->exec($query);
  5699. }
  5700. public function prepare($statement, $options = array())
  5701. {
  5702. return $this->pdo()->prepare($statement, $options);
  5703. }
  5704. public function quote($string, $parameter_type = null): string
  5705. {
  5706. return $this->pdo()->quote($string, $parameter_type);
  5707. }
  5708. public function lastInsertId(/* ?string */$name = null): string
  5709. {
  5710. return $this->pdo()->lastInsertId($name);
  5711. }
  5712. public function query($query, /* ?int */$fetchMode = null, ...$fetchModeArgs): \PDOStatement
  5713. {
  5714. return call_user_func_array(array($this->pdo(), 'query'), func_get_args());
  5715. }
  5716. }
  5717. }
  5718. // file: src/Tqdev/PhpCrudApi/Database/TypeConverter.php
  5719. namespace Tqdev\PhpCrudApi\Database {
  5720. class TypeConverter
  5721. {
  5722. private $driver;
  5723. public function __construct(string $driver)
  5724. {
  5725. $this->driver = $driver;
  5726. }
  5727. private $fromJdbc = [
  5728. 'mysql' => [
  5729. 'clob' => 'longtext',
  5730. 'boolean' => 'tinyint(1)',
  5731. 'blob' => 'longblob',
  5732. 'timestamp' => 'datetime',
  5733. ],
  5734. 'pgsql' => [
  5735. 'clob' => 'text',
  5736. 'blob' => 'bytea',
  5737. 'float' => 'real',
  5738. 'double' => 'double precision',
  5739. 'varbinary' => 'bytea',
  5740. ],
  5741. 'sqlsrv' => [
  5742. 'boolean' => 'bit',
  5743. 'varchar' => 'nvarchar',
  5744. 'clob' => 'ntext',
  5745. 'blob' => 'image',
  5746. 'time' => 'time(0)',
  5747. 'timestamp' => 'datetime2(0)',
  5748. 'double' => 'float',
  5749. 'float' => 'real',
  5750. ],
  5751. ];
  5752. private $toJdbc = [
  5753. 'simplified' => [
  5754. 'char' => 'varchar',
  5755. 'longvarchar' => 'clob',
  5756. 'nchar' => 'varchar',
  5757. 'nvarchar' => 'varchar',
  5758. 'longnvarchar' => 'clob',
  5759. 'binary' => 'varbinary',
  5760. 'longvarbinary' => 'blob',
  5761. 'tinyint' => 'integer',
  5762. 'smallint' => 'integer',
  5763. 'real' => 'float',
  5764. 'numeric' => 'decimal',
  5765. 'nclob' => 'clob',
  5766. 'time_with_timezone' => 'time',
  5767. 'timestamp_with_timezone' => 'timestamp',
  5768. ],
  5769. 'mysql' => [
  5770. 'tinyint(1)' => 'boolean',
  5771. 'bit(1)' => 'boolean',
  5772. 'tinyblob' => 'blob',
  5773. 'mediumblob' => 'blob',
  5774. 'longblob' => 'blob',
  5775. 'tinytext' => 'clob',
  5776. 'mediumtext' => 'clob',
  5777. 'longtext' => 'clob',
  5778. 'text' => 'clob',
  5779. 'mediumint' => 'integer',
  5780. 'int' => 'integer',
  5781. 'polygon' => 'geometry',
  5782. 'point' => 'geometry',
  5783. 'datetime' => 'timestamp',
  5784. 'year' => 'integer',
  5785. 'enum' => 'varchar',
  5786. 'set' => 'varchar',
  5787. 'json' => 'clob',
  5788. ],
  5789. 'pgsql' => [
  5790. 'bigserial' => 'bigint',
  5791. 'bit varying' => 'bit',
  5792. 'box' => 'geometry',
  5793. 'bytea' => 'blob',
  5794. 'bpchar' => 'char',
  5795. 'character varying' => 'varchar',
  5796. 'character' => 'char',
  5797. 'cidr' => 'varchar',
  5798. 'circle' => 'geometry',
  5799. 'double precision' => 'double',
  5800. 'inet' => 'integer',
  5801. //'interval [ fields ]'
  5802. 'json' => 'clob',
  5803. 'jsonb' => 'clob',
  5804. 'line' => 'geometry',
  5805. 'lseg' => 'geometry',
  5806. 'macaddr' => 'varchar',
  5807. 'money' => 'decimal',
  5808. 'path' => 'geometry',
  5809. 'point' => 'geometry',
  5810. 'polygon' => 'geometry',
  5811. 'real' => 'float',
  5812. 'serial' => 'integer',
  5813. 'text' => 'clob',
  5814. 'time without time zone' => 'time',
  5815. 'time with time zone' => 'time_with_timezone',
  5816. 'timestamp without time zone' => 'timestamp',
  5817. 'timestamp with time zone' => 'timestamp_with_timezone',
  5818. //'tsquery'=
  5819. //'tsvector'
  5820. //'txid_snapshot'
  5821. 'uuid' => 'char',
  5822. 'xml' => 'clob',
  5823. ],
  5824. // source: https://docs.microsoft.com/en-us/sql/connect/jdbc/using-basic-data-types?view=sql-server-2017
  5825. 'sqlsrv' => [
  5826. 'varbinary()' => 'blob',
  5827. 'bit' => 'boolean',
  5828. 'datetime' => 'timestamp',
  5829. 'datetime2' => 'timestamp',
  5830. 'float' => 'double',
  5831. 'image' => 'blob',
  5832. 'int' => 'integer',
  5833. 'money' => 'decimal',
  5834. 'ntext' => 'clob',
  5835. 'smalldatetime' => 'timestamp',
  5836. 'smallmoney' => 'decimal',
  5837. 'text' => 'clob',
  5838. 'timestamp' => 'binary',
  5839. 'udt' => 'varbinary',
  5840. 'uniqueidentifier' => 'char',
  5841. 'xml' => 'clob',
  5842. ],
  5843. 'sqlite' => [
  5844. 'tinytext' => 'clob',
  5845. 'text' => 'clob',
  5846. 'mediumtext' => 'clob',
  5847. 'longtext' => 'clob',
  5848. 'mediumint' => 'integer',
  5849. 'int' => 'integer',
  5850. 'bigint' => 'bigint',
  5851. 'int2' => 'smallint',
  5852. 'int4' => 'integer',
  5853. 'int8' => 'bigint',
  5854. 'double precision' => 'double',
  5855. 'datetime' => 'timestamp'
  5856. ],
  5857. ];
  5858. // source: https://docs.oracle.com/javase/9/docs/api/java/sql/Types.html
  5859. private $valid = [
  5860. //'array' => true,
  5861. 'bigint' => true,
  5862. 'binary' => true,
  5863. 'bit' => true,
  5864. 'blob' => true,
  5865. 'boolean' => true,
  5866. 'char' => true,
  5867. 'clob' => true,
  5868. //'datalink' => true,
  5869. 'date' => true,
  5870. 'decimal' => true,
  5871. //'distinct' => true,
  5872. 'double' => true,
  5873. 'float' => true,
  5874. 'integer' => true,
  5875. //'java_object' => true,
  5876. 'longnvarchar' => true,
  5877. 'longvarbinary' => true,
  5878. 'longvarchar' => true,
  5879. 'nchar' => true,
  5880. 'nclob' => true,
  5881. //'null' => true,
  5882. 'numeric' => true,
  5883. 'nvarchar' => true,
  5884. //'other' => true,
  5885. 'real' => true,
  5886. //'ref' => true,
  5887. //'ref_cursor' => true,
  5888. //'rowid' => true,
  5889. 'smallint' => true,
  5890. //'sqlxml' => true,
  5891. //'struct' => true,
  5892. 'time' => true,
  5893. 'time_with_timezone' => true,
  5894. 'timestamp' => true,
  5895. 'timestamp_with_timezone' => true,
  5896. 'tinyint' => true,
  5897. 'varbinary' => true,
  5898. 'varchar' => true,
  5899. // extra:
  5900. 'geometry' => true,
  5901. ];
  5902. public function toJdbc(string $type, string $size): string
  5903. {
  5904. $jdbcType = strtolower($type);
  5905. if (isset($this->toJdbc[$this->driver]["$jdbcType($size)"])) {
  5906. $jdbcType = $this->toJdbc[$this->driver]["$jdbcType($size)"];
  5907. }
  5908. if (isset($this->toJdbc[$this->driver][$jdbcType])) {
  5909. $jdbcType = $this->toJdbc[$this->driver][$jdbcType];
  5910. }
  5911. if (isset($this->toJdbc['simplified'][$jdbcType])) {
  5912. $jdbcType = $this->toJdbc['simplified'][$jdbcType];
  5913. }
  5914. if (!isset($this->valid[$jdbcType])) {
  5915. //throw new \Exception("Unsupported type '$jdbcType' for driver '$this->driver'");
  5916. $jdbcType = 'clob';
  5917. }
  5918. return $jdbcType;
  5919. }
  5920. public function fromJdbc(string $type): string
  5921. {
  5922. $jdbcType = strtolower($type);
  5923. if (isset($this->fromJdbc[$this->driver][$jdbcType])) {
  5924. $jdbcType = $this->fromJdbc[$this->driver][$jdbcType];
  5925. }
  5926. return $jdbcType;
  5927. }
  5928. }
  5929. }
  5930. // file: src/Tqdev/PhpCrudApi/GeoJson/Feature.php
  5931. namespace Tqdev\PhpCrudApi\GeoJson {
  5932. class Feature implements \JsonSerializable
  5933. {
  5934. private $id;
  5935. private $properties;
  5936. private $geometry;
  5937. public function __construct($id, array $properties, /*?Geometry*/ $geometry)
  5938. {
  5939. $this->id = $id;
  5940. $this->properties = $properties;
  5941. $this->geometry = $geometry;
  5942. }
  5943. public function serialize()
  5944. {
  5945. return [
  5946. 'type' => 'Feature',
  5947. 'id' => $this->id,
  5948. 'properties' => $this->properties,
  5949. 'geometry' => $this->geometry,
  5950. ];
  5951. }
  5952. public function jsonSerialize()
  5953. {
  5954. return $this->serialize();
  5955. }
  5956. }
  5957. }
  5958. // file: src/Tqdev/PhpCrudApi/GeoJson/FeatureCollection.php
  5959. namespace Tqdev\PhpCrudApi\GeoJson {
  5960. class FeatureCollection implements \JsonSerializable
  5961. {
  5962. private $features;
  5963. private $results;
  5964. public function __construct(array $features, int $results)
  5965. {
  5966. $this->features = $features;
  5967. $this->results = $results;
  5968. }
  5969. public function serialize()
  5970. {
  5971. return [
  5972. 'type' => 'FeatureCollection',
  5973. 'features' => $this->features,
  5974. 'results' => $this->results,
  5975. ];
  5976. }
  5977. public function jsonSerialize()
  5978. {
  5979. return array_filter($this->serialize(), function ($v) {
  5980. return $v !== 0;
  5981. });
  5982. }
  5983. }
  5984. }
  5985. // file: src/Tqdev/PhpCrudApi/GeoJson/GeoJsonService.php
  5986. namespace Tqdev\PhpCrudApi\GeoJson {
  5987. use Tqdev\PhpCrudApi\Column\ReflectionService;
  5988. use Tqdev\PhpCrudApi\GeoJson\FeatureCollection;
  5989. use Tqdev\PhpCrudApi\Record\RecordService;
  5990. class GeoJsonService
  5991. {
  5992. private $reflection;
  5993. private $records;
  5994. public function __construct(ReflectionService $reflection, RecordService $records)
  5995. {
  5996. $this->reflection = $reflection;
  5997. $this->records = $records;
  5998. }
  5999. public function hasTable(string $table): bool
  6000. {
  6001. return $this->reflection->hasTable($table);
  6002. }
  6003. public function getType(string $table): string
  6004. {
  6005. return $this->reflection->getType($table);
  6006. }
  6007. private function getGeometryColumnName(string $tableName, array &$params): string
  6008. {
  6009. $geometryParam = isset($params['geometry']) ? $params['geometry'][0] : '';
  6010. $table = $this->reflection->getTable($tableName);
  6011. $geometryColumnName = '';
  6012. foreach ($table->getColumnNames() as $columnName) {
  6013. if ($geometryParam && $geometryParam != $columnName) {
  6014. continue;
  6015. }
  6016. $column = $table->getColumn($columnName);
  6017. if ($column->isGeometry()) {
  6018. $geometryColumnName = $columnName;
  6019. break;
  6020. }
  6021. }
  6022. if ($geometryColumnName) {
  6023. $params['mandatory'][] = $tableName . "." . $geometryColumnName;
  6024. }
  6025. return $geometryColumnName;
  6026. }
  6027. private function setBoudingBoxFilter(string $geometryColumnName, array &$params)
  6028. {
  6029. $boundingBox = isset($params['bbox']) ? $params['bbox'][0] : '';
  6030. if ($boundingBox) {
  6031. $c = explode(',', $boundingBox);
  6032. if (!isset($params['filter'])) {
  6033. $params['filter'] = array();
  6034. }
  6035. $params['filter'][] = "$geometryColumnName,sin,POLYGON(($c[0] $c[1],$c[2] $c[1],$c[2] $c[3],$c[0] $c[3],$c[0] $c[1]))";
  6036. }
  6037. $tile = isset($params['tile']) ? $params['tile'][0] : '';
  6038. if ($tile) {
  6039. $zxy = explode(',', $tile);
  6040. if (count($zxy) == 3) {
  6041. list($z, $x, $y) = $zxy;
  6042. $c = array();
  6043. $c = array_merge($c, $this->convertTileToLatLonOfUpperLeftCorner($z, $x, $y));
  6044. $c = array_merge($c, $this->convertTileToLatLonOfUpperLeftCorner($z, $x + 1, $y + 1));
  6045. $params['filter'][] = "$geometryColumnName,sin,POLYGON(($c[0] $c[1],$c[2] $c[1],$c[2] $c[3],$c[0] $c[3],$c[0] $c[1]))";
  6046. }
  6047. }
  6048. }
  6049. private function convertTileToLatLonOfUpperLeftCorner($z, $x, $y): array
  6050. {
  6051. $n = pow(2, $z);
  6052. $lon = $x / $n * 360.0 - 180.0;
  6053. $lat = rad2deg(atan(sinh(pi() * (1 - 2 * $y / $n))));
  6054. return [$lon, $lat];
  6055. }
  6056. private function convertRecordToFeature(/*object*/$record, string $primaryKeyColumnName, string $geometryColumnName)
  6057. {
  6058. $id = null;
  6059. if ($primaryKeyColumnName) {
  6060. $id = $record[$primaryKeyColumnName];
  6061. }
  6062. $geometry = null;
  6063. if (isset($record[$geometryColumnName])) {
  6064. $geometry = Geometry::fromWkt($record[$geometryColumnName]);
  6065. }
  6066. $properties = array_diff_key($record, [$primaryKeyColumnName => true, $geometryColumnName => true]);
  6067. return new Feature($id, $properties, $geometry);
  6068. }
  6069. private function getPrimaryKeyColumnName(string $tableName, array &$params): string
  6070. {
  6071. $primaryKeyColumn = $this->reflection->getTable($tableName)->getPk();
  6072. if (!$primaryKeyColumn) {
  6073. return '';
  6074. }
  6075. $primaryKeyColumnName = $primaryKeyColumn->getName();
  6076. $params['mandatory'][] = $tableName . "." . $primaryKeyColumnName;
  6077. return $primaryKeyColumnName;
  6078. }
  6079. public function _list(string $tableName, array $params): FeatureCollection
  6080. {
  6081. $geometryColumnName = $this->getGeometryColumnName($tableName, $params);
  6082. $this->setBoudingBoxFilter($geometryColumnName, $params);
  6083. $primaryKeyColumnName = $this->getPrimaryKeyColumnName($tableName, $params);
  6084. $records = $this->records->_list($tableName, $params);
  6085. $features = array();
  6086. foreach ($records->getRecords() as $record) {
  6087. $features[] = $this->convertRecordToFeature($record, $primaryKeyColumnName, $geometryColumnName);
  6088. }
  6089. return new FeatureCollection($features, $records->getResults());
  6090. }
  6091. public function read(string $tableName, string $id, array $params): Feature
  6092. {
  6093. $geometryColumnName = $this->getGeometryColumnName($tableName, $params);
  6094. $primaryKeyColumnName = $this->getPrimaryKeyColumnName($tableName, $params);
  6095. $record = $this->records->read($tableName, $id, $params);
  6096. return $this->convertRecordToFeature($record, $primaryKeyColumnName, $geometryColumnName);
  6097. }
  6098. }
  6099. }
  6100. // file: src/Tqdev/PhpCrudApi/GeoJson/Geometry.php
  6101. namespace Tqdev\PhpCrudApi\GeoJson {
  6102. class Geometry implements \JsonSerializable
  6103. {
  6104. private $type;
  6105. private $geometry;
  6106. public static $types = [
  6107. "Point",
  6108. "MultiPoint",
  6109. "LineString",
  6110. "MultiLineString",
  6111. "Polygon",
  6112. "MultiPolygon",
  6113. //"GeometryCollection",
  6114. ];
  6115. public function __construct(string $type, array $coordinates)
  6116. {
  6117. $this->type = $type;
  6118. $this->coordinates = $coordinates;
  6119. }
  6120. public static function fromWkt(string $wkt): Geometry
  6121. {
  6122. $bracket = strpos($wkt, '(');
  6123. $type = strtoupper(trim(substr($wkt, 0, $bracket)));
  6124. $supported = false;
  6125. foreach (Geometry::$types as $typeName) {
  6126. if (strtoupper($typeName) == $type) {
  6127. $type = $typeName;
  6128. $supported = true;
  6129. }
  6130. }
  6131. if (!$supported) {
  6132. throw new \Exception('Geometry type not supported: ' . $type);
  6133. }
  6134. $coordinates = substr($wkt, $bracket);
  6135. if (substr($type, -5) != 'Point' || ($type == 'MultiPoint' && $coordinates[1] != '(')) {
  6136. $coordinates = preg_replace('|([0-9\-\.]+ )+([0-9\-\.]+)|', '[\1\2]', $coordinates);
  6137. }
  6138. $coordinates = str_replace(['(', ')', ', ', ' '], ['[', ']', ',', ','], $coordinates);
  6139. $coordinates = json_decode($coordinates);
  6140. if (!$coordinates) {
  6141. throw new \Exception('Could not decode WKT: ' . $wkt);
  6142. }
  6143. return new Geometry($type, $coordinates);
  6144. }
  6145. public function serialize()
  6146. {
  6147. return [
  6148. 'type' => $this->type,
  6149. 'coordinates' => $this->coordinates,
  6150. ];
  6151. }
  6152. public function jsonSerialize()
  6153. {
  6154. return $this->serialize();
  6155. }
  6156. }
  6157. }
  6158. // file: src/Tqdev/PhpCrudApi/Middleware/Base/Middleware.php
  6159. namespace Tqdev\PhpCrudApi\Middleware\Base {
  6160. use Psr\Http\Server\MiddlewareInterface;
  6161. use Tqdev\PhpCrudApi\Controller\Responder;
  6162. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  6163. abstract class Middleware implements MiddlewareInterface
  6164. {
  6165. protected $next;
  6166. protected $responder;
  6167. private $properties;
  6168. public function __construct(Router $router, Responder $responder, array $properties)
  6169. {
  6170. $router->load($this);
  6171. $this->responder = $responder;
  6172. $this->properties = $properties;
  6173. }
  6174. protected function getArrayProperty(string $key, string $default): array
  6175. {
  6176. return array_filter(array_map('trim', explode(',', $this->getProperty($key, $default))));
  6177. }
  6178. protected function getMapProperty(string $key, string $default): array
  6179. {
  6180. $pairs = $this->getArrayProperty($key, $default);
  6181. $result = array();
  6182. foreach ($pairs as $pair) {
  6183. if (strpos($pair, ':')) {
  6184. list($k, $v) = explode(':', $pair, 2);
  6185. $result[trim($k)] = trim($v);
  6186. } else {
  6187. $result[] = trim($pair);
  6188. }
  6189. }
  6190. return $result;
  6191. }
  6192. protected function getProperty(string $key, $default)
  6193. {
  6194. return isset($this->properties[$key]) ? $this->properties[$key] : $default;
  6195. }
  6196. }
  6197. }
  6198. // file: src/Tqdev/PhpCrudApi/Middleware/Communication/VariableStore.php
  6199. namespace Tqdev\PhpCrudApi\Middleware\Communication {
  6200. class VariableStore
  6201. {
  6202. public static $values = array();
  6203. public static function get(string $key)
  6204. {
  6205. if (isset(self::$values[$key])) {
  6206. return self::$values[$key];
  6207. }
  6208. return null;
  6209. }
  6210. public static function set(string $key, /* object */ $value)
  6211. {
  6212. self::$values[$key] = $value;
  6213. }
  6214. }
  6215. }
  6216. // file: src/Tqdev/PhpCrudApi/Middleware/Router/Router.php
  6217. namespace Tqdev\PhpCrudApi\Middleware\Router {
  6218. use Psr\Http\Message\ResponseInterface;
  6219. use Psr\Http\Message\ServerRequestInterface;
  6220. use Psr\Http\Server\RequestHandlerInterface;
  6221. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  6222. interface Router extends RequestHandlerInterface
  6223. {
  6224. public function register(string $method, string $path, array $handler);
  6225. public function load(Middleware $middleware);
  6226. public function route(ServerRequestInterface $request): ResponseInterface;
  6227. }
  6228. }
  6229. // file: src/Tqdev/PhpCrudApi/Middleware/Router/SimpleRouter.php
  6230. namespace Tqdev\PhpCrudApi\Middleware\Router {
  6231. use Psr\Http\Message\ResponseInterface;
  6232. use Psr\Http\Message\ServerRequestInterface;
  6233. use Tqdev\PhpCrudApi\Cache\Cache;
  6234. use Tqdev\PhpCrudApi\Controller\Responder;
  6235. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  6236. use Tqdev\PhpCrudApi\Record\ErrorCode;
  6237. use Tqdev\PhpCrudApi\Record\PathTree;
  6238. use Tqdev\PhpCrudApi\RequestUtils;
  6239. use Tqdev\PhpCrudApi\ResponseUtils;
  6240. class SimpleRouter implements Router
  6241. {
  6242. private $basePath;
  6243. private $responder;
  6244. private $cache;
  6245. private $ttl;
  6246. private $registration;
  6247. private $routes;
  6248. private $routeHandlers;
  6249. private $middlewares;
  6250. public function __construct(string $basePath, Responder $responder, Cache $cache, int $ttl)
  6251. {
  6252. $this->basePath = rtrim($this->detectBasePath($basePath), '/');
  6253. $this->responder = $responder;
  6254. $this->cache = $cache;
  6255. $this->ttl = $ttl;
  6256. $this->registration = true;
  6257. $this->routes = $this->loadPathTree();
  6258. $this->routeHandlers = [];
  6259. $this->middlewares = array();
  6260. }
  6261. private function detectBasePath(string $basePath): string
  6262. {
  6263. if ($basePath) {
  6264. return $basePath;
  6265. }
  6266. if (isset($_SERVER['REQUEST_URI'])) {
  6267. $fullPath = urldecode(explode('?', $_SERVER['REQUEST_URI'])[0]);
  6268. if (isset($_SERVER['PATH_INFO'])) {
  6269. $path = $_SERVER['PATH_INFO'];
  6270. if (substr($fullPath, -1 * strlen($path)) == $path) {
  6271. return substr($fullPath, 0, -1 * strlen($path));
  6272. }
  6273. }
  6274. if ('/' . basename(__FILE__) == $fullPath) {
  6275. return $fullPath;
  6276. }
  6277. }
  6278. return '/';
  6279. }
  6280. private function loadPathTree(): PathTree
  6281. {
  6282. $data = $this->cache->get('PathTree');
  6283. if ($data != '') {
  6284. $tree = PathTree::fromJson(json_decode(gzuncompress($data)));
  6285. $this->registration = false;
  6286. } else {
  6287. $tree = new PathTree();
  6288. }
  6289. return $tree;
  6290. }
  6291. public function register(string $method, string $path, array $handler)
  6292. {
  6293. $routeNumber = count($this->routeHandlers);
  6294. $this->routeHandlers[$routeNumber] = $handler;
  6295. if ($this->registration) {
  6296. $path = trim($path, '/');
  6297. $parts = array();
  6298. if ($path) {
  6299. $parts = explode('/', $path);
  6300. }
  6301. array_unshift($parts, $method);
  6302. $this->routes->put($parts, $routeNumber);
  6303. }
  6304. }
  6305. public function load(Middleware $middleware) /*: void*/
  6306. {
  6307. array_push($this->middlewares, $middleware);
  6308. }
  6309. public function route(ServerRequestInterface $request): ResponseInterface
  6310. {
  6311. if ($this->registration) {
  6312. $data = gzcompress(json_encode($this->routes, JSON_UNESCAPED_UNICODE));
  6313. $this->cache->set('PathTree', $data, $this->ttl);
  6314. }
  6315. return $this->handle($request);
  6316. }
  6317. private function getRouteNumbers(ServerRequestInterface $request): array
  6318. {
  6319. $method = strtoupper($request->getMethod());
  6320. $path = array();
  6321. $segment = $method;
  6322. for ($i = 1; strlen($segment) > 0; $i++) {
  6323. array_push($path, $segment);
  6324. $segment = RequestUtils::getPathSegment($request, $i);
  6325. }
  6326. return $this->routes->match($path);
  6327. }
  6328. private function removeBasePath(ServerRequestInterface $request): ServerRequestInterface
  6329. {
  6330. $path = $request->getUri()->getPath();
  6331. if (substr($path, 0, strlen($this->basePath)) == $this->basePath) {
  6332. $path = substr($path, strlen($this->basePath));
  6333. $request = $request->withUri($request->getUri()->withPath($path));
  6334. }
  6335. return $request;
  6336. }
  6337. public function getBasePath(): string
  6338. {
  6339. return $this->basePath;
  6340. }
  6341. public function handle(ServerRequestInterface $request): ResponseInterface
  6342. {
  6343. $request = $this->removeBasePath($request);
  6344. if (count($this->middlewares)) {
  6345. $handler = array_pop($this->middlewares);
  6346. return $handler->process($request, $this);
  6347. }
  6348. $routeNumbers = $this->getRouteNumbers($request);
  6349. if (count($routeNumbers) == 0) {
  6350. return $this->responder->error(ErrorCode::ROUTE_NOT_FOUND, $request->getUri()->getPath());
  6351. }
  6352. try {
  6353. $response = call_user_func($this->routeHandlers[$routeNumbers[0]], $request);
  6354. } catch (\Throwable $exception) {
  6355. $response = $this->responder->exception($exception);
  6356. }
  6357. return $response;
  6358. }
  6359. }
  6360. }
  6361. // file: src/Tqdev/PhpCrudApi/Middleware/AjaxOnlyMiddleware.php
  6362. namespace Tqdev\PhpCrudApi\Middleware {
  6363. use Psr\Http\Message\ResponseInterface;
  6364. use Psr\Http\Message\ServerRequestInterface;
  6365. use Psr\Http\Server\RequestHandlerInterface;
  6366. use Tqdev\PhpCrudApi\Controller\Responder;
  6367. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  6368. use Tqdev\PhpCrudApi\Record\ErrorCode;
  6369. use Tqdev\PhpCrudApi\RequestUtils;
  6370. class AjaxOnlyMiddleware extends Middleware
  6371. {
  6372. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  6373. {
  6374. $method = $request->getMethod();
  6375. $excludeMethods = $this->getArrayProperty('excludeMethods', 'OPTIONS,GET');
  6376. if (!in_array($method, $excludeMethods)) {
  6377. $headerName = $this->getProperty('headerName', 'X-Requested-With');
  6378. $headerValue = $this->getProperty('headerValue', 'XMLHttpRequest');
  6379. if ($headerValue != RequestUtils::getHeader($request, $headerName)) {
  6380. return $this->responder->error(ErrorCode::ONLY_AJAX_REQUESTS_ALLOWED, $method);
  6381. }
  6382. }
  6383. return $next->handle($request);
  6384. }
  6385. }
  6386. }
  6387. // file: src/Tqdev/PhpCrudApi/Middleware/AuthorizationMiddleware.php
  6388. namespace Tqdev\PhpCrudApi\Middleware {
  6389. use Psr\Http\Message\ResponseInterface;
  6390. use Psr\Http\Message\ServerRequestInterface;
  6391. use Psr\Http\Server\RequestHandlerInterface;
  6392. use Tqdev\PhpCrudApi\Column\ReflectionService;
  6393. use Tqdev\PhpCrudApi\Controller\Responder;
  6394. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  6395. use Tqdev\PhpCrudApi\Middleware\Communication\VariableStore;
  6396. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  6397. use Tqdev\PhpCrudApi\Record\ErrorCode;
  6398. use Tqdev\PhpCrudApi\Record\FilterInfo;
  6399. use Tqdev\PhpCrudApi\RequestUtils;
  6400. class AuthorizationMiddleware extends Middleware
  6401. {
  6402. private $reflection;
  6403. public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection)
  6404. {
  6405. parent::__construct($router, $responder, $properties);
  6406. $this->reflection = $reflection;
  6407. }
  6408. private function handleColumns(string $operation, string $tableName) /*: void*/
  6409. {
  6410. $columnHandler = $this->getProperty('columnHandler', '');
  6411. if ($columnHandler) {
  6412. $table = $this->reflection->getTable($tableName);
  6413. foreach ($table->getColumnNames() as $columnName) {
  6414. $allowed = call_user_func($columnHandler, $operation, $tableName, $columnName);
  6415. if (!$allowed) {
  6416. $table->removeColumn($columnName);
  6417. }
  6418. }
  6419. }
  6420. }
  6421. private function handleTable(string $operation, string $tableName) /*: void*/
  6422. {
  6423. if (!$this->reflection->hasTable($tableName)) {
  6424. return;
  6425. }
  6426. $allowed = true;
  6427. $tableHandler = $this->getProperty('tableHandler', '');
  6428. if ($tableHandler) {
  6429. $allowed = call_user_func($tableHandler, $operation, $tableName);
  6430. }
  6431. if (!$allowed) {
  6432. $this->reflection->removeTable($tableName);
  6433. } else {
  6434. $this->handleColumns($operation, $tableName);
  6435. }
  6436. }
  6437. private function handleRecords(string $operation, string $tableName) /*: void*/
  6438. {
  6439. if (!$this->reflection->hasTable($tableName)) {
  6440. return;
  6441. }
  6442. $recordHandler = $this->getProperty('recordHandler', '');
  6443. if ($recordHandler) {
  6444. $query = call_user_func($recordHandler, $operation, $tableName);
  6445. $filters = new FilterInfo();
  6446. $table = $this->reflection->getTable($tableName);
  6447. $query = str_replace('][]=', ']=', str_replace('=', '[]=', $query));
  6448. parse_str($query, $params);
  6449. $condition = $filters->getCombinedConditions($table, $params);
  6450. VariableStore::set("authorization.conditions.$tableName", $condition);
  6451. }
  6452. }
  6453. private function pathHandler(string $path) /*: bool*/
  6454. {
  6455. $pathHandler = $this->getProperty('pathHandler', '');
  6456. return $pathHandler ? call_user_func($pathHandler, $path) : true;
  6457. }
  6458. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  6459. {
  6460. $path = RequestUtils::getPathSegment($request, 1);
  6461. if (!$this->pathHandler($path)) {
  6462. return $this->responder->error(ErrorCode::ROUTE_NOT_FOUND, $request->getUri()->getPath());
  6463. }
  6464. $operation = RequestUtils::getOperation($request);
  6465. $tableNames = RequestUtils::getTableNames($request, $this->reflection);
  6466. foreach ($tableNames as $tableName) {
  6467. $this->handleTable($operation, $tableName);
  6468. if ($path == 'records') {
  6469. $this->handleRecords($operation, $tableName);
  6470. }
  6471. }
  6472. if ($path == 'openapi') {
  6473. VariableStore::set('authorization.tableHandler', $this->getProperty('tableHandler', ''));
  6474. VariableStore::set('authorization.columnHandler', $this->getProperty('columnHandler', ''));
  6475. }
  6476. return $next->handle($request);
  6477. }
  6478. }
  6479. }
  6480. // file: src/Tqdev/PhpCrudApi/Middleware/BasicAuthMiddleware.php
  6481. namespace Tqdev\PhpCrudApi\Middleware {
  6482. use Psr\Http\Message\ResponseInterface;
  6483. use Psr\Http\Message\ServerRequestInterface;
  6484. use Psr\Http\Server\RequestHandlerInterface;
  6485. use Tqdev\PhpCrudApi\Controller\Responder;
  6486. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  6487. use Tqdev\PhpCrudApi\Record\ErrorCode;
  6488. use Tqdev\PhpCrudApi\RequestUtils;
  6489. class BasicAuthMiddleware extends Middleware
  6490. {
  6491. private function hasCorrectPassword(string $username, string $password, array &$passwords): bool
  6492. {
  6493. $hash = isset($passwords[$username]) ? $passwords[$username] : false;
  6494. if ($hash && password_verify($password, $hash)) {
  6495. if (password_needs_rehash($hash, PASSWORD_DEFAULT)) {
  6496. $passwords[$username] = password_hash($password, PASSWORD_DEFAULT);
  6497. }
  6498. return true;
  6499. }
  6500. return false;
  6501. }
  6502. private function getValidUsername(string $username, string $password, string $passwordFile): string
  6503. {
  6504. $passwords = $this->readPasswords($passwordFile);
  6505. $valid = $this->hasCorrectPassword($username, $password, $passwords);
  6506. $this->writePasswords($passwordFile, $passwords);
  6507. return $valid ? $username : '';
  6508. }
  6509. private function readPasswords(string $passwordFile): array
  6510. {
  6511. $passwords = [];
  6512. $passwordLines = file($passwordFile);
  6513. foreach ($passwordLines as $passwordLine) {
  6514. if (strpos($passwordLine, ':') !== false) {
  6515. list($username, $hash) = explode(':', trim($passwordLine), 2);
  6516. if (strlen($hash) > 0 && $hash[0] != '$') {
  6517. $hash = password_hash($hash, PASSWORD_DEFAULT);
  6518. }
  6519. $passwords[$username] = $hash;
  6520. }
  6521. }
  6522. return $passwords;
  6523. }
  6524. private function writePasswords(string $passwordFile, array $passwords): bool
  6525. {
  6526. $success = false;
  6527. $passwordFileContents = '';
  6528. foreach ($passwords as $username => $hash) {
  6529. $passwordFileContents .= "$username:$hash\n";
  6530. }
  6531. if (file_get_contents($passwordFile) != $passwordFileContents) {
  6532. $success = file_put_contents($passwordFile, $passwordFileContents) !== false;
  6533. }
  6534. return $success;
  6535. }
  6536. private function getAuthorizationCredentials(ServerRequestInterface $request): string
  6537. {
  6538. if (isset($_SERVER['PHP_AUTH_USER'])) {
  6539. return $_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW'];
  6540. }
  6541. $header = RequestUtils::getHeader($request, 'Authorization');
  6542. $parts = explode(' ', trim($header), 2);
  6543. if (count($parts) != 2) {
  6544. return '';
  6545. }
  6546. if ($parts[0] != 'Basic') {
  6547. return '';
  6548. }
  6549. return base64_decode(strtr($parts[1], '-_', '+/'));
  6550. }
  6551. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  6552. {
  6553. if (session_status() == PHP_SESSION_NONE) {
  6554. if (!headers_sent()) {
  6555. $sessionName = $this->getProperty('sessionName', '');
  6556. if ($sessionName) {
  6557. session_name($sessionName);
  6558. }
  6559. session_start();
  6560. }
  6561. }
  6562. $credentials = $this->getAuthorizationCredentials($request);
  6563. if ($credentials) {
  6564. list($username, $password) = array('', '');
  6565. if (strpos($credentials, ':') !== false) {
  6566. list($username, $password) = explode(':', $credentials, 2);
  6567. }
  6568. $passwordFile = $this->getProperty('passwordFile', '.htpasswd');
  6569. $validUser = $this->getValidUsername($username, $password, $passwordFile);
  6570. $_SESSION['username'] = $validUser;
  6571. if (!$validUser) {
  6572. return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username);
  6573. }
  6574. if (!headers_sent()) {
  6575. session_regenerate_id();
  6576. }
  6577. }
  6578. if (!isset($_SESSION['username']) || !$_SESSION['username']) {
  6579. $authenticationMode = $this->getProperty('mode', 'required');
  6580. if ($authenticationMode == 'required') {
  6581. $response = $this->responder->error(ErrorCode::AUTHENTICATION_REQUIRED, '');
  6582. $realm = $this->getProperty('realm', 'Username and password required');
  6583. $response = $response->withHeader('WWW-Authenticate', "Basic realm=\"$realm\"");
  6584. return $response;
  6585. }
  6586. }
  6587. return $next->handle($request);
  6588. }
  6589. }
  6590. }
  6591. // file: src/Tqdev/PhpCrudApi/Middleware/CorsMiddleware.php
  6592. namespace Tqdev\PhpCrudApi\Middleware {
  6593. use Psr\Http\Message\ResponseInterface;
  6594. use Psr\Http\Message\ServerRequestInterface;
  6595. use Psr\Http\Server\RequestHandlerInterface;
  6596. use Tqdev\PhpCrudApi\Controller\Responder;
  6597. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  6598. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  6599. use Tqdev\PhpCrudApi\Record\ErrorCode;
  6600. use Tqdev\PhpCrudApi\ResponseFactory;
  6601. use Tqdev\PhpCrudApi\ResponseUtils;
  6602. class CorsMiddleware extends Middleware
  6603. {
  6604. private $debug;
  6605. public function __construct(Router $router, Responder $responder, array $properties, bool $debug)
  6606. {
  6607. parent::__construct($router, $responder, $properties);
  6608. $this->debug = $debug;
  6609. }
  6610. private function isOriginAllowed(string $origin, string $allowedOrigins): bool
  6611. {
  6612. $found = false;
  6613. foreach (explode(',', $allowedOrigins) as $allowedOrigin) {
  6614. $hostname = preg_quote(strtolower(trim($allowedOrigin)));
  6615. $regex = '/^' . str_replace('\*', '.*', $hostname) . '$/';
  6616. if (preg_match($regex, $origin)) {
  6617. $found = true;
  6618. break;
  6619. }
  6620. }
  6621. return $found;
  6622. }
  6623. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  6624. {
  6625. $method = $request->getMethod();
  6626. $origin = count($request->getHeader('Origin')) ? $request->getHeader('Origin')[0] : '';
  6627. $allowedOrigins = $this->getProperty('allowedOrigins', '*');
  6628. if ($origin && !$this->isOriginAllowed($origin, $allowedOrigins)) {
  6629. $response = $this->responder->error(ErrorCode::ORIGIN_FORBIDDEN, $origin);
  6630. } elseif ($method == 'OPTIONS') {
  6631. $response = ResponseFactory::fromStatus(ResponseFactory::OK);
  6632. $allowHeaders = $this->getProperty('allowHeaders', 'Content-Type, X-XSRF-TOKEN, X-Authorization');
  6633. if ($this->debug) {
  6634. $allowHeaders = implode(', ', array_filter([$allowHeaders, 'X-Exception-Name, X-Exception-Message, X-Exception-File']));
  6635. }
  6636. if ($allowHeaders) {
  6637. $response = $response->withHeader('Access-Control-Allow-Headers', $allowHeaders);
  6638. }
  6639. $allowMethods = $this->getProperty('allowMethods', 'OPTIONS, GET, PUT, POST, DELETE, PATCH');
  6640. if ($allowMethods) {
  6641. $response = $response->withHeader('Access-Control-Allow-Methods', $allowMethods);
  6642. }
  6643. $allowCredentials = $this->getProperty('allowCredentials', 'true');
  6644. if ($allowCredentials) {
  6645. $response = $response->withHeader('Access-Control-Allow-Credentials', $allowCredentials);
  6646. }
  6647. $maxAge = $this->getProperty('maxAge', '1728000');
  6648. if ($maxAge) {
  6649. $response = $response->withHeader('Access-Control-Max-Age', $maxAge);
  6650. }
  6651. $exposeHeaders = $this->getProperty('exposeHeaders', '');
  6652. if ($this->debug) {
  6653. $exposeHeaders = implode(', ', array_filter([$exposeHeaders, 'X-Exception-Name, X-Exception-Message, X-Exception-File']));
  6654. }
  6655. if ($exposeHeaders) {
  6656. $response = $response->withHeader('Access-Control-Expose-Headers', $exposeHeaders);
  6657. }
  6658. } else {
  6659. $response = null;
  6660. try {
  6661. $response = $next->handle($request);
  6662. } catch (\Throwable $e) {
  6663. $response = $this->responder->error(ErrorCode::ERROR_NOT_FOUND, $e->getMessage());
  6664. if ($this->debug) {
  6665. $response = ResponseUtils::addExceptionHeaders($response, $e);
  6666. }
  6667. }
  6668. }
  6669. if ($origin) {
  6670. $allowCredentials = $this->getProperty('allowCredentials', 'true');
  6671. if ($allowCredentials) {
  6672. $response = $response->withHeader('Access-Control-Allow-Credentials', $allowCredentials);
  6673. }
  6674. $response = $response->withHeader('Access-Control-Allow-Origin', $origin);
  6675. }
  6676. return $response;
  6677. }
  6678. }
  6679. }
  6680. // file: src/Tqdev/PhpCrudApi/Middleware/CustomizationMiddleware.php
  6681. namespace Tqdev\PhpCrudApi\Middleware {
  6682. use Psr\Http\Message\ResponseInterface;
  6683. use Psr\Http\Message\ServerRequestInterface;
  6684. use Psr\Http\Server\RequestHandlerInterface;
  6685. use Tqdev\PhpCrudApi\Column\ReflectionService;
  6686. use Tqdev\PhpCrudApi\Controller\Responder;
  6687. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  6688. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  6689. use Tqdev\PhpCrudApi\RequestUtils;
  6690. class CustomizationMiddleware extends Middleware
  6691. {
  6692. private $reflection;
  6693. public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection)
  6694. {
  6695. parent::__construct($router, $responder, $properties);
  6696. $this->reflection = $reflection;
  6697. }
  6698. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  6699. {
  6700. $operation = RequestUtils::getOperation($request);
  6701. $tableName = RequestUtils::getPathSegment($request, 2);
  6702. $beforeHandler = $this->getProperty('beforeHandler', '');
  6703. $environment = (object) array();
  6704. if ($beforeHandler !== '') {
  6705. $result = call_user_func($beforeHandler, $operation, $tableName, $request, $environment);
  6706. $request = $result ?: $request;
  6707. }
  6708. $response = $next->handle($request);
  6709. $afterHandler = $this->getProperty('afterHandler', '');
  6710. if ($afterHandler !== '') {
  6711. $result = call_user_func($afterHandler, $operation, $tableName, $response, $environment);
  6712. $response = $result ?: $response;
  6713. }
  6714. return $response;
  6715. }
  6716. }
  6717. }
  6718. // file: src/Tqdev/PhpCrudApi/Middleware/DbAuthMiddleware.php
  6719. namespace Tqdev\PhpCrudApi\Middleware {
  6720. use Psr\Http\Message\ResponseInterface;
  6721. use Psr\Http\Message\ServerRequestInterface;
  6722. use Psr\Http\Server\RequestHandlerInterface;
  6723. use Tqdev\PhpCrudApi\Column\ReflectionService;
  6724. use Tqdev\PhpCrudApi\Controller\Responder;
  6725. use Tqdev\PhpCrudApi\Database\GenericDB;
  6726. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  6727. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  6728. use Tqdev\PhpCrudApi\Record\Condition\ColumnCondition;
  6729. use Tqdev\PhpCrudApi\Record\ErrorCode;
  6730. use Tqdev\PhpCrudApi\Record\OrderingInfo;
  6731. use Tqdev\PhpCrudApi\RequestUtils;
  6732. class DbAuthMiddleware extends Middleware
  6733. {
  6734. private $reflection;
  6735. private $db;
  6736. private $ordering;
  6737. public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection, GenericDB $db)
  6738. {
  6739. parent::__construct($router, $responder, $properties);
  6740. $this->reflection = $reflection;
  6741. $this->db = $db;
  6742. $this->ordering = new OrderingInfo();
  6743. }
  6744. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  6745. {
  6746. if (session_status() == PHP_SESSION_NONE) {
  6747. if (!headers_sent()) {
  6748. $sessionName = $this->getProperty('sessionName', '');
  6749. if ($sessionName) {
  6750. session_name($sessionName);
  6751. }
  6752. session_start();
  6753. }
  6754. }
  6755. $path = RequestUtils::getPathSegment($request, 1);
  6756. $method = $request->getMethod();
  6757. if ($method == 'POST' && in_array($path, ['login', 'register', 'password'])) {
  6758. $body = $request->getParsedBody();
  6759. $username = isset($body->username) ? $body->username : '';
  6760. $password = isset($body->password) ? $body->password : '';
  6761. $newPassword = isset($body->newPassword) ? $body->newPassword : '';
  6762. $tableName = $this->getProperty('usersTable', 'users');
  6763. $table = $this->reflection->getTable($tableName);
  6764. $usernameColumnName = $this->getProperty('usernameColumn', 'username');
  6765. $usernameColumn = $table->getColumn($usernameColumnName);
  6766. $passwordColumnName = $this->getProperty('passwordColumn', 'password');
  6767. $passwordLength = $this->getProperty('passwordLength', '12');
  6768. $pkName = $table->getPk()->getName();
  6769. $registerUser = $this->getProperty('registerUser', '');
  6770. $condition = new ColumnCondition($usernameColumn, 'eq', $username);
  6771. $returnedColumns = $this->getProperty('returnedColumns', '');
  6772. if (!$returnedColumns) {
  6773. $columnNames = $table->getColumnNames();
  6774. } else {
  6775. $columnNames = array_map('trim', explode(',', $returnedColumns));
  6776. $columnNames[] = $passwordColumnName;
  6777. $columnNames[] = $pkName;
  6778. $columnNames = array_values(array_unique($columnNames));
  6779. }
  6780. $columnOrdering = $this->ordering->getDefaultColumnOrdering($table);
  6781. if ($path == 'register') {
  6782. if (!$registerUser) {
  6783. return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username);
  6784. }
  6785. if (strlen($password) < $passwordLength) {
  6786. return $this->responder->error(ErrorCode::PASSWORD_TOO_SHORT, $passwordLength);
  6787. }
  6788. $users = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, 0, 1);
  6789. if (!empty($users)) {
  6790. return $this->responder->error(ErrorCode::USER_ALREADY_EXIST, $username);
  6791. }
  6792. $data = json_decode($registerUser, true);
  6793. $data = is_array($data) ? $data : [];
  6794. $data[$usernameColumnName] = $username;
  6795. $data[$passwordColumnName] = password_hash($password, PASSWORD_DEFAULT);
  6796. $this->db->createSingle($table, $data);
  6797. $users = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, 0, 1);
  6798. foreach ($users as $user) {
  6799. unset($user[$passwordColumnName]);
  6800. return $this->responder->success($user);
  6801. }
  6802. return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username);
  6803. }
  6804. if ($path == 'login') {
  6805. $users = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, 0, 1);
  6806. foreach ($users as $user) {
  6807. if (password_verify($password, $user[$passwordColumnName]) == 1) {
  6808. if (!headers_sent()) {
  6809. session_regenerate_id(true);
  6810. }
  6811. unset($user[$passwordColumnName]);
  6812. $_SESSION['user'] = $user;
  6813. return $this->responder->success($user);
  6814. }
  6815. }
  6816. return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username);
  6817. }
  6818. if ($path == 'password') {
  6819. if ($username != ($_SESSION['user'][$usernameColumnName] ?? '')) {
  6820. return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username);
  6821. }
  6822. if (strlen($newPassword) < $passwordLength) {
  6823. return $this->responder->error(ErrorCode::PASSWORD_TOO_SHORT, $passwordLength);
  6824. }
  6825. $users = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, 0, 1);
  6826. foreach ($users as $user) {
  6827. if (password_verify($password, $user[$passwordColumnName]) == 1) {
  6828. if (!headers_sent()) {
  6829. session_regenerate_id(true);
  6830. }
  6831. $data = [$passwordColumnName => password_hash($newPassword, PASSWORD_DEFAULT)];
  6832. $this->db->updateSingle($table, $data, $user[$pkName]);
  6833. unset($user[$passwordColumnName]);
  6834. return $this->responder->success($user);
  6835. }
  6836. }
  6837. return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username);
  6838. }
  6839. }
  6840. if ($method == 'POST' && $path == 'logout') {
  6841. if (isset($_SESSION['user'])) {
  6842. $user = $_SESSION['user'];
  6843. unset($_SESSION['user']);
  6844. if (session_status() != PHP_SESSION_NONE) {
  6845. session_destroy();
  6846. }
  6847. return $this->responder->success($user);
  6848. }
  6849. return $this->responder->error(ErrorCode::AUTHENTICATION_REQUIRED, '');
  6850. }
  6851. if ($method == 'GET' && $path == 'me') {
  6852. if (isset($_SESSION['user'])) {
  6853. return $this->responder->success($_SESSION['user']);
  6854. }
  6855. return $this->responder->error(ErrorCode::AUTHENTICATION_REQUIRED, '');
  6856. }
  6857. if (!isset($_SESSION['user']) || !$_SESSION['user']) {
  6858. $authenticationMode = $this->getProperty('mode', 'required');
  6859. if ($authenticationMode == 'required') {
  6860. return $this->responder->error(ErrorCode::AUTHENTICATION_REQUIRED, '');
  6861. }
  6862. }
  6863. return $next->handle($request);
  6864. }
  6865. }
  6866. }
  6867. // file: src/Tqdev/PhpCrudApi/Middleware/FirewallMiddleware.php
  6868. namespace Tqdev\PhpCrudApi\Middleware {
  6869. use Psr\Http\Message\ResponseInterface;
  6870. use Psr\Http\Message\ServerRequestInterface;
  6871. use Psr\Http\Server\RequestHandlerInterface;
  6872. use Tqdev\PhpCrudApi\Controller\Responder;
  6873. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  6874. use Tqdev\PhpCrudApi\Record\ErrorCode;
  6875. class FirewallMiddleware extends Middleware
  6876. {
  6877. private function ipMatch(string $ip, string $cidr): bool
  6878. {
  6879. if (strpos($cidr, '/') !== false) {
  6880. list($subnet, $mask) = explode('/', trim($cidr));
  6881. if ((ip2long($ip) & ~((1 << (32 - $mask)) - 1)) == ip2long($subnet)) {
  6882. return true;
  6883. }
  6884. } else {
  6885. if (ip2long($ip) == ip2long($cidr)) {
  6886. return true;
  6887. }
  6888. }
  6889. return false;
  6890. }
  6891. private function isIpAllowed(string $ipAddress, string $allowedIpAddresses): bool
  6892. {
  6893. foreach (explode(',', $allowedIpAddresses) as $allowedIp) {
  6894. if ($this->ipMatch($ipAddress, $allowedIp)) {
  6895. return true;
  6896. }
  6897. }
  6898. return false;
  6899. }
  6900. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  6901. {
  6902. $reverseProxy = $this->getProperty('reverseProxy', '');
  6903. if ($reverseProxy) {
  6904. $ipAddress = array_pop(explode(',', $request->getHeader('X-Forwarded-For')));
  6905. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  6906. $ipAddress = $_SERVER['REMOTE_ADDR'];
  6907. } else {
  6908. $ipAddress = '127.0.0.1';
  6909. }
  6910. $allowedIpAddresses = $this->getProperty('allowedIpAddresses', '');
  6911. if (!$this->isIpAllowed($ipAddress, $allowedIpAddresses)) {
  6912. $response = $this->responder->error(ErrorCode::TEMPORARY_OR_PERMANENTLY_BLOCKED, '');
  6913. } else {
  6914. $response = $next->handle($request);
  6915. }
  6916. return $response;
  6917. }
  6918. }
  6919. }
  6920. // file: src/Tqdev/PhpCrudApi/Middleware/IpAddressMiddleware.php
  6921. namespace Tqdev\PhpCrudApi\Middleware {
  6922. use Psr\Http\Message\ResponseInterface;
  6923. use Psr\Http\Message\ServerRequestInterface;
  6924. use Psr\Http\Server\RequestHandlerInterface;
  6925. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
  6926. use Tqdev\PhpCrudApi\Column\ReflectionService;
  6927. use Tqdev\PhpCrudApi\Controller\Responder;
  6928. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  6929. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  6930. use Tqdev\PhpCrudApi\RequestUtils;
  6931. class IpAddressMiddleware extends Middleware
  6932. {
  6933. private $reflection;
  6934. public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection)
  6935. {
  6936. parent::__construct($router, $responder, $properties);
  6937. $this->reflection = $reflection;
  6938. }
  6939. private function callHandler($record, string $operation, ReflectedTable $table) /*: object */
  6940. {
  6941. $context = (array) $record;
  6942. $columnNames = $this->getProperty('columns', '');
  6943. if ($columnNames) {
  6944. foreach (explode(',', $columnNames) as $columnName) {
  6945. if ($table->hasColumn($columnName)) {
  6946. if ($operation == 'create') {
  6947. $context[$columnName] = $_SERVER['REMOTE_ADDR'];
  6948. } else {
  6949. unset($context[$columnName]);
  6950. }
  6951. }
  6952. }
  6953. }
  6954. return (object) $context;
  6955. }
  6956. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  6957. {
  6958. $operation = RequestUtils::getOperation($request);
  6959. if (in_array($operation, ['create', 'update', 'increment'])) {
  6960. $tableNames = $this->getProperty('tables', '');
  6961. $tableName = RequestUtils::getPathSegment($request, 2);
  6962. if (!$tableNames || in_array($tableName, explode(',', $tableNames))) {
  6963. if ($this->reflection->hasTable($tableName)) {
  6964. $record = $request->getParsedBody();
  6965. if ($record !== null) {
  6966. $table = $this->reflection->getTable($tableName);
  6967. if (is_array($record)) {
  6968. foreach ($record as &$r) {
  6969. $r = $this->callHandler($r, $operation, $table);
  6970. }
  6971. } else {
  6972. $record = $this->callHandler($record, $operation, $table);
  6973. }
  6974. $request = $request->withParsedBody($record);
  6975. }
  6976. }
  6977. }
  6978. }
  6979. return $next->handle($request);
  6980. }
  6981. }
  6982. }
  6983. // file: src/Tqdev/PhpCrudApi/Middleware/JoinLimitsMiddleware.php
  6984. namespace Tqdev\PhpCrudApi\Middleware {
  6985. use Psr\Http\Message\ResponseInterface;
  6986. use Psr\Http\Message\ServerRequestInterface;
  6987. use Psr\Http\Server\RequestHandlerInterface;
  6988. use Tqdev\PhpCrudApi\Column\ReflectionService;
  6989. use Tqdev\PhpCrudApi\Controller\Responder;
  6990. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  6991. use Tqdev\PhpCrudApi\Middleware\Communication\VariableStore;
  6992. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  6993. use Tqdev\PhpCrudApi\RequestUtils;
  6994. class JoinLimitsMiddleware extends Middleware
  6995. {
  6996. private $reflection;
  6997. public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection)
  6998. {
  6999. parent::__construct($router, $responder, $properties);
  7000. $this->reflection = $reflection;
  7001. }
  7002. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  7003. {
  7004. $operation = RequestUtils::getOperation($request);
  7005. $params = RequestUtils::getParams($request);
  7006. if (in_array($operation, ['read', 'list']) && isset($params['join'])) {
  7007. $maxDepth = (int) $this->getProperty('depth', '3');
  7008. $maxTables = (int) $this->getProperty('tables', '10');
  7009. $maxRecords = (int) $this->getProperty('records', '1000');
  7010. $tableCount = 0;
  7011. $joinPaths = array();
  7012. for ($i = 0; $i < count($params['join']); $i++) {
  7013. $joinPath = array();
  7014. $tables = explode(',', $params['join'][$i]);
  7015. for ($depth = 0; $depth < min($maxDepth, count($tables)); $depth++) {
  7016. array_push($joinPath, $tables[$depth]);
  7017. $tableCount += 1;
  7018. if ($tableCount == $maxTables) {
  7019. break;
  7020. }
  7021. }
  7022. array_push($joinPaths, implode(',', $joinPath));
  7023. if ($tableCount == $maxTables) {
  7024. break;
  7025. }
  7026. }
  7027. $params['join'] = $joinPaths;
  7028. $request = RequestUtils::setParams($request, $params);
  7029. VariableStore::set("joinLimits.maxRecords", $maxRecords);
  7030. }
  7031. return $next->handle($request);
  7032. }
  7033. }
  7034. }
  7035. // file: src/Tqdev/PhpCrudApi/Middleware/JwtAuthMiddleware.php
  7036. namespace Tqdev\PhpCrudApi\Middleware {
  7037. use Psr\Http\Message\ResponseInterface;
  7038. use Psr\Http\Message\ServerRequestInterface;
  7039. use Psr\Http\Server\RequestHandlerInterface;
  7040. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  7041. use Tqdev\PhpCrudApi\Record\ErrorCode;
  7042. use Tqdev\PhpCrudApi\RequestUtils;
  7043. class JwtAuthMiddleware extends Middleware
  7044. {
  7045. private function getVerifiedClaims(string $token, int $time, int $leeway, int $ttl, array $secrets, array $requirements): array
  7046. {
  7047. $algorithms = array(
  7048. 'HS256' => 'sha256',
  7049. 'HS384' => 'sha384',
  7050. 'HS512' => 'sha512',
  7051. 'RS256' => 'sha256',
  7052. 'RS384' => 'sha384',
  7053. 'RS512' => 'sha512',
  7054. );
  7055. $token = explode('.', $token);
  7056. if (count($token) < 3) {
  7057. return array();
  7058. }
  7059. $header = json_decode(base64_decode(strtr($token[0], '-_', '+/')), true);
  7060. $kid = 0;
  7061. if (isset($header['kid'])) {
  7062. $kid = $header['kid'];
  7063. }
  7064. if (!isset($secrets[$kid])) {
  7065. return array();
  7066. }
  7067. $secret = $secrets[$kid];
  7068. if ($header['typ'] != 'JWT') {
  7069. return array();
  7070. }
  7071. $algorithm = $header['alg'];
  7072. if (!isset($algorithms[$algorithm])) {
  7073. return array();
  7074. }
  7075. if (!empty($requirements['alg']) && !in_array($algorithm, $requirements['alg'])) {
  7076. return array();
  7077. }
  7078. $hmac = $algorithms[$algorithm];
  7079. $signature = base64_decode(strtr($token[2], '-_', '+/'));
  7080. $data = "$token[0].$token[1]";
  7081. switch ($algorithm[0]) {
  7082. case 'H':
  7083. $hash = hash_hmac($hmac, $data, $secret, true);
  7084. $equals = hash_equals($hash, $signature);
  7085. if (!$equals) {
  7086. return array();
  7087. }
  7088. break;
  7089. case 'R':
  7090. $equals = openssl_verify($data, $signature, $secret, $hmac) == 1;
  7091. if (!$equals) {
  7092. return array();
  7093. }
  7094. break;
  7095. }
  7096. $claims = json_decode(base64_decode(strtr($token[1], '-_', '+/')), true);
  7097. if (!$claims) {
  7098. return array();
  7099. }
  7100. foreach ($requirements as $field => $values) {
  7101. if (!empty($values)) {
  7102. if ($field != 'alg') {
  7103. if (!isset($claims[$field]) || !in_array($claims[$field], $values)) {
  7104. return array();
  7105. }
  7106. }
  7107. }
  7108. }
  7109. if (isset($claims['nbf']) && $time + $leeway < $claims['nbf']) {
  7110. return array();
  7111. }
  7112. if (isset($claims['iat']) && $time + $leeway < $claims['iat']) {
  7113. return array();
  7114. }
  7115. if (isset($claims['exp']) && $time - $leeway > $claims['exp']) {
  7116. return array();
  7117. }
  7118. if (isset($claims['iat']) && !isset($claims['exp'])) {
  7119. if ($time - $leeway > $claims['iat'] + $ttl) {
  7120. return array();
  7121. }
  7122. }
  7123. return $claims;
  7124. }
  7125. private function getClaims(string $token): array
  7126. {
  7127. $time = (int) $this->getProperty('time', time());
  7128. $leeway = (int) $this->getProperty('leeway', '5');
  7129. $ttl = (int) $this->getProperty('ttl', '30');
  7130. $secrets = $this->getMapProperty('secrets', '');
  7131. if (!$secrets) {
  7132. $secrets = [$this->getProperty('secret', '')];
  7133. }
  7134. $requirements = array(
  7135. 'alg' => $this->getArrayProperty('algorithms', ''),
  7136. 'aud' => $this->getArrayProperty('audiences', ''),
  7137. 'iss' => $this->getArrayProperty('issuers', ''),
  7138. );
  7139. return $this->getVerifiedClaims($token, $time, $leeway, $ttl, $secrets, $requirements);
  7140. }
  7141. private function getAuthorizationToken(ServerRequestInterface $request): string
  7142. {
  7143. $headerName = $this->getProperty('header', 'X-Authorization');
  7144. $headerValue = RequestUtils::getHeader($request, $headerName);
  7145. $parts = explode(' ', trim($headerValue), 2);
  7146. if (count($parts) != 2) {
  7147. return '';
  7148. }
  7149. if ($parts[0] != 'Bearer') {
  7150. return '';
  7151. }
  7152. return $parts[1];
  7153. }
  7154. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  7155. {
  7156. if (session_status() == PHP_SESSION_NONE) {
  7157. if (!headers_sent()) {
  7158. $sessionName = $this->getProperty('sessionName', '');
  7159. if ($sessionName) {
  7160. session_name($sessionName);
  7161. }
  7162. session_start();
  7163. }
  7164. }
  7165. $token = $this->getAuthorizationToken($request);
  7166. if ($token) {
  7167. $claims = $this->getClaims($token);
  7168. $_SESSION['claims'] = $claims;
  7169. if (empty($claims)) {
  7170. return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, 'JWT');
  7171. }
  7172. if (!headers_sent()) {
  7173. session_regenerate_id();
  7174. }
  7175. }
  7176. if (empty($_SESSION['claims'])) {
  7177. $authenticationMode = $this->getProperty('mode', 'required');
  7178. if ($authenticationMode == 'required') {
  7179. return $this->responder->error(ErrorCode::AUTHENTICATION_REQUIRED, '');
  7180. }
  7181. }
  7182. return $next->handle($request);
  7183. }
  7184. }
  7185. }
  7186. // file: src/Tqdev/PhpCrudApi/Middleware/MultiTenancyMiddleware.php
  7187. namespace Tqdev\PhpCrudApi\Middleware {
  7188. use Psr\Http\Message\ResponseInterface;
  7189. use Psr\Http\Message\ServerRequestInterface;
  7190. use Psr\Http\Server\RequestHandlerInterface;
  7191. use Tqdev\PhpCrudApi\Column\ReflectionService;
  7192. use Tqdev\PhpCrudApi\Controller\Responder;
  7193. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  7194. use Tqdev\PhpCrudApi\Middleware\Communication\VariableStore;
  7195. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  7196. use Tqdev\PhpCrudApi\Record\Condition\ColumnCondition;
  7197. use Tqdev\PhpCrudApi\Record\Condition\Condition;
  7198. use Tqdev\PhpCrudApi\Record\Condition\NoCondition;
  7199. use Tqdev\PhpCrudApi\RequestUtils;
  7200. class MultiTenancyMiddleware extends Middleware
  7201. {
  7202. private $reflection;
  7203. public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection)
  7204. {
  7205. parent::__construct($router, $responder, $properties);
  7206. $this->reflection = $reflection;
  7207. }
  7208. private function getCondition(string $tableName, array $pairs): Condition
  7209. {
  7210. $condition = new NoCondition();
  7211. $table = $this->reflection->getTable($tableName);
  7212. foreach ($pairs as $k => $v) {
  7213. $condition = $condition->_and(new ColumnCondition($table->getColumn($k), 'eq', $v));
  7214. }
  7215. return $condition;
  7216. }
  7217. private function getPairs($handler, string $operation, string $tableName): array
  7218. {
  7219. $result = array();
  7220. $pairs = call_user_func($handler, $operation, $tableName) ?: [];
  7221. $table = $this->reflection->getTable($tableName);
  7222. foreach ($pairs as $k => $v) {
  7223. if ($table->hasColumn($k)) {
  7224. $result[$k] = $v;
  7225. }
  7226. }
  7227. return $result;
  7228. }
  7229. private function handleRecord(ServerRequestInterface $request, string $operation, array $pairs): ServerRequestInterface
  7230. {
  7231. $record = $request->getParsedBody();
  7232. if ($record === null) {
  7233. return $request;
  7234. }
  7235. $multi = is_array($record);
  7236. $records = $multi ? $record : [$record];
  7237. foreach ($records as &$record) {
  7238. foreach ($pairs as $column => $value) {
  7239. if ($operation == 'create') {
  7240. $record->$column = $value;
  7241. } else {
  7242. if (isset($record->$column)) {
  7243. unset($record->$column);
  7244. }
  7245. }
  7246. }
  7247. }
  7248. return $request->withParsedBody($multi ? $records : $records[0]);
  7249. }
  7250. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  7251. {
  7252. $handler = $this->getProperty('handler', '');
  7253. if ($handler !== '') {
  7254. $path = RequestUtils::getPathSegment($request, 1);
  7255. if ($path == 'records') {
  7256. $operation = RequestUtils::getOperation($request);
  7257. $tableNames = RequestUtils::getTableNames($request, $this->reflection);
  7258. foreach ($tableNames as $i => $tableName) {
  7259. if (!$this->reflection->hasTable($tableName)) {
  7260. continue;
  7261. }
  7262. $pairs = $this->getPairs($handler, $operation, $tableName);
  7263. if ($i == 0) {
  7264. if (in_array($operation, ['create', 'update', 'increment'])) {
  7265. $request = $this->handleRecord($request, $operation, $pairs);
  7266. }
  7267. }
  7268. $condition = $this->getCondition($tableName, $pairs);
  7269. VariableStore::set("multiTenancy.conditions.$tableName", $condition);
  7270. }
  7271. }
  7272. }
  7273. return $next->handle($request);
  7274. }
  7275. }
  7276. }
  7277. // file: src/Tqdev/PhpCrudApi/Middleware/PageLimitsMiddleware.php
  7278. namespace Tqdev\PhpCrudApi\Middleware {
  7279. use Psr\Http\Message\ResponseInterface;
  7280. use Psr\Http\Message\ServerRequestInterface;
  7281. use Psr\Http\Server\RequestHandlerInterface;
  7282. use Tqdev\PhpCrudApi\Column\ReflectionService;
  7283. use Tqdev\PhpCrudApi\Controller\Responder;
  7284. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  7285. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  7286. use Tqdev\PhpCrudApi\Record\ErrorCode;
  7287. use Tqdev\PhpCrudApi\RequestUtils;
  7288. class PageLimitsMiddleware extends Middleware
  7289. {
  7290. private $reflection;
  7291. public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection)
  7292. {
  7293. parent::__construct($router, $responder, $properties);
  7294. $this->reflection = $reflection;
  7295. }
  7296. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  7297. {
  7298. $operation = RequestUtils::getOperation($request);
  7299. if ($operation == 'list') {
  7300. $params = RequestUtils::getParams($request);
  7301. $maxPage = (int) $this->getProperty('pages', '100');
  7302. if (isset($params['page']) && $params['page'] && $maxPage > 0) {
  7303. if (strpos($params['page'][0], ',') === false) {
  7304. $page = $params['page'][0];
  7305. } else {
  7306. list($page, $size) = explode(',', $params['page'][0], 2);
  7307. }
  7308. if ($page > $maxPage) {
  7309. return $this->responder->error(ErrorCode::PAGINATION_FORBIDDEN, '');
  7310. }
  7311. }
  7312. $maxSize = (int) $this->getProperty('records', '1000');
  7313. if (!isset($params['size']) || !$params['size'] && $maxSize > 0) {
  7314. $params['size'] = array($maxSize);
  7315. } else {
  7316. $params['size'] = array(min($params['size'][0], $maxSize));
  7317. }
  7318. $request = RequestUtils::setParams($request, $params);
  7319. }
  7320. return $next->handle($request);
  7321. }
  7322. }
  7323. }
  7324. // file: src/Tqdev/PhpCrudApi/Middleware/ReconnectMiddleware.php
  7325. namespace Tqdev\PhpCrudApi\Middleware {
  7326. use Psr\Http\Message\ResponseInterface;
  7327. use Psr\Http\Message\ServerRequestInterface;
  7328. use Psr\Http\Server\RequestHandlerInterface;
  7329. use Tqdev\PhpCrudApi\Column\ReflectionService;
  7330. use Tqdev\PhpCrudApi\Controller\Responder;
  7331. use Tqdev\PhpCrudApi\Database\GenericDB;
  7332. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  7333. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  7334. class ReconnectMiddleware extends Middleware
  7335. {
  7336. private $reflection;
  7337. private $db;
  7338. public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection, GenericDB $db)
  7339. {
  7340. parent::__construct($router, $responder, $properties);
  7341. $this->reflection = $reflection;
  7342. $this->db = $db;
  7343. }
  7344. private function getDriver(): string
  7345. {
  7346. $driverHandler = $this->getProperty('driverHandler', '');
  7347. if ($driverHandler) {
  7348. return call_user_func($driverHandler);
  7349. }
  7350. return '';
  7351. }
  7352. private function getAddress(): string
  7353. {
  7354. $addressHandler = $this->getProperty('addressHandler', '');
  7355. if ($addressHandler) {
  7356. return call_user_func($addressHandler);
  7357. }
  7358. return '';
  7359. }
  7360. private function getPort(): int
  7361. {
  7362. $portHandler = $this->getProperty('portHandler', '');
  7363. if ($portHandler) {
  7364. return call_user_func($portHandler);
  7365. }
  7366. return 0;
  7367. }
  7368. private function getDatabase(): string
  7369. {
  7370. $databaseHandler = $this->getProperty('databaseHandler', '');
  7371. if ($databaseHandler) {
  7372. return call_user_func($databaseHandler);
  7373. }
  7374. return '';
  7375. }
  7376. private function getTables(): array
  7377. {
  7378. $tablesHandler = $this->getProperty('tablesHandler', '');
  7379. if ($tablesHandler) {
  7380. return call_user_func($tablesHandler);
  7381. }
  7382. return [];
  7383. }
  7384. private function getUsername(): string
  7385. {
  7386. $usernameHandler = $this->getProperty('usernameHandler', '');
  7387. if ($usernameHandler) {
  7388. return call_user_func($usernameHandler);
  7389. }
  7390. return '';
  7391. }
  7392. private function getPassword(): string
  7393. {
  7394. $passwordHandler = $this->getProperty('passwordHandler', '');
  7395. if ($passwordHandler) {
  7396. return call_user_func($passwordHandler);
  7397. }
  7398. return '';
  7399. }
  7400. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  7401. {
  7402. $driver = $this->getDriver();
  7403. $address = $this->getAddress();
  7404. $port = $this->getPort();
  7405. $database = $this->getDatabase();
  7406. $tables = $this->getTables();
  7407. $username = $this->getUsername();
  7408. $password = $this->getPassword();
  7409. if ($driver || $address || $port || $database || $tables || $username || $password) {
  7410. $this->db->reconstruct($driver, $address, $port, $database, $tables, $username, $password);
  7411. }
  7412. return $next->handle($request);
  7413. }
  7414. }
  7415. }
  7416. // file: src/Tqdev/PhpCrudApi/Middleware/SanitationMiddleware.php
  7417. namespace Tqdev\PhpCrudApi\Middleware {
  7418. use Psr\Http\Message\ResponseInterface;
  7419. use Psr\Http\Message\ServerRequestInterface;
  7420. use Psr\Http\Server\RequestHandlerInterface;
  7421. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
  7422. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedColumn;
  7423. use Tqdev\PhpCrudApi\Column\ReflectionService;
  7424. use Tqdev\PhpCrudApi\Controller\Responder;
  7425. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  7426. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  7427. use Tqdev\PhpCrudApi\RequestUtils;
  7428. class SanitationMiddleware extends Middleware
  7429. {
  7430. private $reflection;
  7431. public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection)
  7432. {
  7433. parent::__construct($router, $responder, $properties);
  7434. $this->reflection = $reflection;
  7435. }
  7436. private function callHandler($handler, $record, string $operation, ReflectedTable $table) /*: object */
  7437. {
  7438. $context = (array) $record;
  7439. $tableName = $table->getName();
  7440. foreach ($context as $columnName => &$value) {
  7441. if ($table->hasColumn($columnName)) {
  7442. $column = $table->getColumn($columnName);
  7443. $value = call_user_func($handler, $operation, $tableName, $column->serialize(), $value);
  7444. $value = $this->sanitizeType($table, $column, $value);
  7445. }
  7446. }
  7447. return (object) $context;
  7448. }
  7449. private function sanitizeType(ReflectedTable $table, ReflectedColumn $column, $value)
  7450. {
  7451. $tables = $this->getArrayProperty('tables', 'all');
  7452. $types = $this->getArrayProperty('types', 'all');
  7453. if (
  7454. (in_array('all', $tables) || in_array($table->getName(), $tables)) &&
  7455. (in_array('all', $types) || in_array($column->getType(), $types))
  7456. ) {
  7457. if (is_null($value)) {
  7458. return $value;
  7459. }
  7460. if (is_string($value)) {
  7461. $newValue = null;
  7462. switch ($column->getType()) {
  7463. case 'integer':
  7464. case 'bigint':
  7465. $newValue = filter_var(trim($value), FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
  7466. break;
  7467. case 'decimal':
  7468. $newValue = filter_var(trim($value), FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE);
  7469. if (is_float($newValue)) {
  7470. $newValue = number_format($newValue, $column->getScale(), '.', '');
  7471. }
  7472. break;
  7473. case 'float':
  7474. case 'double':
  7475. $newValue = filter_var(trim($value), FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE);
  7476. break;
  7477. case 'boolean':
  7478. $newValue = filter_var(trim($value), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
  7479. break;
  7480. case 'date':
  7481. $time = strtotime(trim($value));
  7482. if ($time !== false) {
  7483. $newValue = date('Y-m-d', $time);
  7484. }
  7485. break;
  7486. case 'time':
  7487. $time = strtotime(trim($value));
  7488. if ($time !== false) {
  7489. $newValue = date('H:i:s', $time);
  7490. }
  7491. break;
  7492. case 'timestamp':
  7493. $time = strtotime(trim($value));
  7494. if ($time !== false) {
  7495. $newValue = date('Y-m-d H:i:s', $time);
  7496. }
  7497. break;
  7498. case 'blob':
  7499. case 'varbinary':
  7500. // allow base64url format
  7501. $newValue = strtr(trim($value), '-_', '+/');
  7502. break;
  7503. case 'clob':
  7504. case 'varchar':
  7505. $newValue = $value;
  7506. break;
  7507. case 'geometry':
  7508. $newValue = trim($value);
  7509. break;
  7510. }
  7511. if (!is_null($newValue)) {
  7512. $value = $newValue;
  7513. }
  7514. } else {
  7515. switch ($column->getType()) {
  7516. case 'integer':
  7517. case 'bigint':
  7518. if (is_float($value)) {
  7519. $value = (int) round($value);
  7520. }
  7521. break;
  7522. case 'decimal':
  7523. if (is_float($value) || is_int($value)) {
  7524. $value = number_format((float) $value, $column->getScale(), '.', '');
  7525. }
  7526. break;
  7527. }
  7528. }
  7529. // post process
  7530. }
  7531. return $value;
  7532. }
  7533. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  7534. {
  7535. $operation = RequestUtils::getOperation($request);
  7536. if (in_array($operation, ['create', 'update', 'increment'])) {
  7537. $tableName = RequestUtils::getPathSegment($request, 2);
  7538. if ($this->reflection->hasTable($tableName)) {
  7539. $record = $request->getParsedBody();
  7540. if ($record !== null) {
  7541. $handler = $this->getProperty('handler', '');
  7542. if ($handler !== '') {
  7543. $table = $this->reflection->getTable($tableName);
  7544. if (is_array($record)) {
  7545. foreach ($record as &$r) {
  7546. $r = $this->callHandler($handler, $r, $operation, $table);
  7547. }
  7548. } else {
  7549. $record = $this->callHandler($handler, $record, $operation, $table);
  7550. }
  7551. $request = $request->withParsedBody($record);
  7552. }
  7553. }
  7554. }
  7555. }
  7556. return $next->handle($request);
  7557. }
  7558. }
  7559. }
  7560. // file: src/Tqdev/PhpCrudApi/Middleware/SslRedirectMiddleware.php
  7561. namespace Tqdev\PhpCrudApi\Middleware {
  7562. use Psr\Http\Message\ResponseInterface;
  7563. use Psr\Http\Message\ServerRequestInterface;
  7564. use Psr\Http\Server\RequestHandlerInterface;
  7565. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  7566. use Tqdev\PhpCrudApi\ResponseFactory;
  7567. class SslRedirectMiddleware extends Middleware
  7568. {
  7569. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  7570. {
  7571. $uri = $request->getUri();
  7572. $scheme = $uri->getScheme();
  7573. if ($scheme == 'http') {
  7574. $uri = $request->getUri();
  7575. $uri = $uri->withScheme('https');
  7576. $response = ResponseFactory::fromStatus(301);
  7577. $response = $response->withHeader('Location', $uri->__toString());
  7578. } else {
  7579. $response = $next->handle($request);
  7580. }
  7581. return $response;
  7582. }
  7583. }
  7584. }
  7585. // file: src/Tqdev/PhpCrudApi/Middleware/ValidationMiddleware.php
  7586. namespace Tqdev\PhpCrudApi\Middleware {
  7587. use Psr\Http\Message\ResponseInterface;
  7588. use Psr\Http\Message\ServerRequestInterface;
  7589. use Psr\Http\Server\RequestHandlerInterface;
  7590. use Tqdev\PhpCrudApi\Column\ReflectionService;
  7591. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
  7592. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedColumn;
  7593. use Tqdev\PhpCrudApi\Controller\Responder;
  7594. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  7595. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  7596. use Tqdev\PhpCrudApi\Record\ErrorCode;
  7597. use Tqdev\PhpCrudApi\RequestUtils;
  7598. class ValidationMiddleware extends Middleware
  7599. {
  7600. private $reflection;
  7601. public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection)
  7602. {
  7603. parent::__construct($router, $responder, $properties);
  7604. $this->reflection = $reflection;
  7605. }
  7606. private function callHandler($handler, $record, string $operation, ReflectedTable $table) /*: ResponseInterface?*/
  7607. {
  7608. $context = (array) $record;
  7609. $details = array();
  7610. $tableName = $table->getName();
  7611. foreach ($context as $columnName => $value) {
  7612. if ($table->hasColumn($columnName)) {
  7613. $column = $table->getColumn($columnName);
  7614. $valid = call_user_func($handler, $operation, $tableName, $column->serialize(), $value, $context);
  7615. if ($valid === true || $valid === '') {
  7616. $valid = $this->validateType($table, $column, $value);
  7617. }
  7618. if ($valid !== true && $valid !== '') {
  7619. $details[$columnName] = $valid;
  7620. }
  7621. }
  7622. }
  7623. if (count($details) > 0) {
  7624. return $this->responder->error(ErrorCode::INPUT_VALIDATION_FAILED, $tableName, $details);
  7625. }
  7626. return null;
  7627. }
  7628. private function validateType(ReflectedTable $table, ReflectedColumn $column, $value)
  7629. {
  7630. $tables = $this->getArrayProperty('tables', 'all');
  7631. $types = $this->getArrayProperty('types', 'all');
  7632. if (
  7633. (in_array('all', $tables) || in_array($table->getName(), $tables)) &&
  7634. (in_array('all', $types) || in_array($column->getType(), $types))
  7635. ) {
  7636. if (is_null($value)) {
  7637. return ($column->getNullable() ? true : "cannot be null");
  7638. }
  7639. if (is_string($value)) {
  7640. // check for whitespace
  7641. switch ($column->getType()) {
  7642. case 'varchar':
  7643. case 'clob':
  7644. break;
  7645. default:
  7646. if (strlen(trim($value)) != strlen($value)) {
  7647. return 'illegal whitespace';
  7648. }
  7649. break;
  7650. }
  7651. // try to parse
  7652. switch ($column->getType()) {
  7653. case 'integer':
  7654. case 'bigint':
  7655. if (
  7656. filter_var($value, FILTER_SANITIZE_NUMBER_INT) !== $value ||
  7657. filter_var($value, FILTER_VALIDATE_INT) === false
  7658. ) {
  7659. return 'invalid integer';
  7660. }
  7661. break;
  7662. case 'decimal':
  7663. if (strpos($value, '.') !== false) {
  7664. list($whole, $decimals) = explode('.', ltrim($value, '-'), 2);
  7665. } else {
  7666. list($whole, $decimals) = array(ltrim($value, '-'), '');
  7667. }
  7668. if (strlen($whole) > 0 && !ctype_digit($whole)) {
  7669. return 'invalid decimal';
  7670. }
  7671. if (strlen($decimals) > 0 && !ctype_digit($decimals)) {
  7672. return 'invalid decimal';
  7673. }
  7674. if (strlen($whole) > $column->getPrecision() - $column->getScale()) {
  7675. return 'decimal too large';
  7676. }
  7677. if (strlen($decimals) > $column->getScale()) {
  7678. return 'decimal too precise';
  7679. }
  7680. break;
  7681. case 'float':
  7682. case 'double':
  7683. if (
  7684. filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT) !== $value ||
  7685. filter_var($value, FILTER_VALIDATE_FLOAT) === false
  7686. ) {
  7687. return 'invalid float';
  7688. }
  7689. break;
  7690. case 'boolean':
  7691. if (!in_array(strtolower($value), array('true', 'false'))) {
  7692. return 'invalid boolean';
  7693. }
  7694. break;
  7695. case 'date':
  7696. if (date_create_from_format('Y-m-d', $value) === false) {
  7697. return 'invalid date';
  7698. }
  7699. break;
  7700. case 'time':
  7701. if (date_create_from_format('H:i:s', $value) === false) {
  7702. return 'invalid time';
  7703. }
  7704. break;
  7705. case 'timestamp':
  7706. if (date_create_from_format('Y-m-d H:i:s', $value) === false) {
  7707. return 'invalid timestamp';
  7708. }
  7709. break;
  7710. case 'clob':
  7711. case 'varchar':
  7712. if ($column->hasLength() && mb_strlen($value, 'UTF-8') > $column->getLength()) {
  7713. return 'string too long';
  7714. }
  7715. break;
  7716. case 'blob':
  7717. case 'varbinary':
  7718. if (base64_decode($value, true) === false) {
  7719. return 'invalid base64';
  7720. }
  7721. if ($column->hasLength() && strlen(base64_decode($value)) > $column->getLength()) {
  7722. return 'string too long';
  7723. }
  7724. break;
  7725. case 'geometry':
  7726. // no checks yet
  7727. break;
  7728. }
  7729. } else { // check non-string types
  7730. switch ($column->getType()) {
  7731. case 'integer':
  7732. case 'bigint':
  7733. if (!is_int($value)) {
  7734. return 'invalid integer';
  7735. }
  7736. break;
  7737. case 'float':
  7738. case 'double':
  7739. if (!is_float($value) && !is_int($value)) {
  7740. return 'invalid float';
  7741. }
  7742. break;
  7743. case 'boolean':
  7744. if (!is_bool($value) && ($value !== 0) && ($value !== 1)) {
  7745. return 'invalid boolean';
  7746. }
  7747. break;
  7748. default:
  7749. return 'invalid ' . $column->getType();
  7750. }
  7751. }
  7752. // extra checks
  7753. switch ($column->getType()) {
  7754. case 'integer': // 4 byte signed
  7755. $value = filter_var($value, FILTER_VALIDATE_INT);
  7756. if ($value > 2147483647 || $value < -2147483648) {
  7757. return 'invalid integer';
  7758. }
  7759. break;
  7760. }
  7761. }
  7762. return (true);
  7763. }
  7764. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  7765. {
  7766. $operation = RequestUtils::getOperation($request);
  7767. if (in_array($operation, ['create', 'update', 'increment'])) {
  7768. $tableName = RequestUtils::getPathSegment($request, 2);
  7769. if ($this->reflection->hasTable($tableName)) {
  7770. $record = $request->getParsedBody();
  7771. if ($record !== null) {
  7772. $handler = $this->getProperty('handler', '');
  7773. if ($handler !== '') {
  7774. $table = $this->reflection->getTable($tableName);
  7775. if (is_array($record)) {
  7776. foreach ($record as $r) {
  7777. $response = $this->callHandler($handler, $r, $operation, $table);
  7778. if ($response !== null) {
  7779. return $response;
  7780. }
  7781. }
  7782. } else {
  7783. $response = $this->callHandler($handler, $record, $operation, $table);
  7784. if ($response !== null) {
  7785. return $response;
  7786. }
  7787. }
  7788. }
  7789. }
  7790. }
  7791. }
  7792. return $next->handle($request);
  7793. }
  7794. }
  7795. }
  7796. // file: src/Tqdev/PhpCrudApi/Middleware/XmlMiddleware.php
  7797. namespace Tqdev\PhpCrudApi\Middleware {
  7798. use Psr\Http\Message\ResponseInterface;
  7799. use Psr\Http\Message\ServerRequestInterface;
  7800. use Psr\Http\Server\RequestHandlerInterface;
  7801. use Tqdev\PhpCrudApi\Column\ReflectionService;
  7802. use Tqdev\PhpCrudApi\Controller\Responder;
  7803. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  7804. use Tqdev\PhpCrudApi\Middleware\Router\Router;
  7805. use Tqdev\PhpCrudApi\RequestUtils;
  7806. use Tqdev\PhpCrudApi\ResponseFactory;
  7807. class XmlMiddleware extends Middleware
  7808. {
  7809. private $reflection;
  7810. public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection)
  7811. {
  7812. parent::__construct($router, $responder, $properties);
  7813. $this->reflection = $reflection;
  7814. }
  7815. private function json2xml($json, $types = 'null,boolean,number,string,object,array')
  7816. {
  7817. $a = json_decode($json);
  7818. $d = new \DOMDocument();
  7819. $c = $d->createElement("root");
  7820. $d->appendChild($c);
  7821. $t = function ($v) {
  7822. $type = gettype($v);
  7823. switch ($type) {
  7824. case 'integer':
  7825. return 'number';
  7826. case 'double':
  7827. return 'number';
  7828. default:
  7829. return strtolower($type);
  7830. }
  7831. };
  7832. $ts = explode(',', $types);
  7833. $f = function ($f, $c, $a, $s = false) use ($t, $d, $ts) {
  7834. if (in_array($t($a), $ts)) {
  7835. $c->setAttribute('type', $t($a));
  7836. }
  7837. if ($t($a) != 'array' && $t($a) != 'object') {
  7838. if ($t($a) == 'boolean') {
  7839. $c->appendChild($d->createTextNode($a ? 'true' : 'false'));
  7840. } else {
  7841. $c->appendChild($d->createTextNode($a));
  7842. }
  7843. } else {
  7844. foreach ($a as $k => $v) {
  7845. if ($k == '__type' && $t($a) == 'object') {
  7846. $c->setAttribute('__type', $v);
  7847. } else {
  7848. if ($t($v) == 'object') {
  7849. $ch = $c->appendChild($d->createElementNS(null, $s ? 'item' : $k));
  7850. $f($f, $ch, $v);
  7851. } else if ($t($v) == 'array') {
  7852. $ch = $c->appendChild($d->createElementNS(null, $s ? 'item' : $k));
  7853. $f($f, $ch, $v, true);
  7854. } else {
  7855. $va = $d->createElementNS(null, $s ? 'item' : $k);
  7856. if ($t($v) == 'boolean') {
  7857. $va->appendChild($d->createTextNode($v ? 'true' : 'false'));
  7858. } else {
  7859. $va->appendChild($d->createTextNode($v));
  7860. }
  7861. $ch = $c->appendChild($va);
  7862. if (in_array($t($v), $ts)) {
  7863. $ch->setAttribute('type', $t($v));
  7864. }
  7865. }
  7866. }
  7867. }
  7868. }
  7869. };
  7870. $f($f, $c, $a, $t($a) == 'array');
  7871. return $d->saveXML($d->documentElement);
  7872. }
  7873. private function xml2json($xml)
  7874. {
  7875. $o = @simplexml_load_string($xml);
  7876. if ($o===false) {
  7877. return null;
  7878. }
  7879. $a = @dom_import_simplexml($o);
  7880. if (!$a) {
  7881. return null;
  7882. }
  7883. $t = function ($v) {
  7884. $t = $v->getAttribute('type');
  7885. $txt = $v->firstChild->nodeType == XML_TEXT_NODE;
  7886. return $t ?: ($txt ? 'string' : 'object');
  7887. };
  7888. $f = function ($f, $a) use ($t) {
  7889. $c = null;
  7890. if ($t($a) == 'null') {
  7891. $c = null;
  7892. } else if ($t($a) == 'boolean') {
  7893. $b = substr(strtolower($a->textContent), 0, 1);
  7894. $c = in_array($b, array('1', 't'));
  7895. } else if ($t($a) == 'number') {
  7896. $c = $a->textContent + 0;
  7897. } else if ($t($a) == 'string') {
  7898. $c = $a->textContent;
  7899. } else if ($t($a) == 'object') {
  7900. $c = array();
  7901. if ($a->getAttribute('__type')) {
  7902. $c['__type'] = $a->getAttribute('__type');
  7903. }
  7904. for ($i = 0; $i < $a->childNodes->length; $i++) {
  7905. $v = $a->childNodes[$i];
  7906. $c[$v->nodeName] = $f($f, $v);
  7907. }
  7908. $c = (object) $c;
  7909. } else if ($t($a) == 'array') {
  7910. $c = array();
  7911. for ($i = 0; $i < $a->childNodes->length; $i++) {
  7912. $v = $a->childNodes[$i];
  7913. $c[$i] = $f($f, $v);
  7914. }
  7915. }
  7916. return $c;
  7917. };
  7918. $c = $f($f, $a);
  7919. return json_encode($c);
  7920. }
  7921. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  7922. {
  7923. parse_str($request->getUri()->getQuery(), $params);
  7924. $isXml = isset($params['format']) && $params['format'] == 'xml';
  7925. if ($isXml) {
  7926. $body = $request->getBody()->getContents();
  7927. if ($body) {
  7928. $json = $this->xml2json($body);
  7929. $request = $request->withParsedBody(json_decode($json));
  7930. }
  7931. }
  7932. $response = $next->handle($request);
  7933. if ($isXml) {
  7934. $body = $response->getBody()->getContents();
  7935. if ($body) {
  7936. $types = implode(',', $this->getArrayProperty('types', 'null,array'));
  7937. if ($types == '' || $types == 'all') {
  7938. $xml = $this->json2xml($body);
  7939. } else {
  7940. $xml = $this->json2xml($body, $types);
  7941. }
  7942. $response = ResponseFactory::fromXml(ResponseFactory::OK, $xml);
  7943. }
  7944. }
  7945. return $response;
  7946. }
  7947. }
  7948. }
  7949. // file: src/Tqdev/PhpCrudApi/Middleware/XsrfMiddleware.php
  7950. namespace Tqdev\PhpCrudApi\Middleware {
  7951. use Psr\Http\Message\ResponseInterface;
  7952. use Psr\Http\Message\ServerRequestInterface;
  7953. use Psr\Http\Server\RequestHandlerInterface;
  7954. use Tqdev\PhpCrudApi\Controller\Responder;
  7955. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  7956. use Tqdev\PhpCrudApi\Record\ErrorCode;
  7957. class XsrfMiddleware extends Middleware
  7958. {
  7959. private function getToken(): string
  7960. {
  7961. $cookieName = $this->getProperty('cookieName', 'XSRF-TOKEN');
  7962. if (isset($_COOKIE[$cookieName])) {
  7963. $token = $_COOKIE[$cookieName];
  7964. } else {
  7965. $secure = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on';
  7966. $token = bin2hex(random_bytes(8));
  7967. if (!headers_sent()) {
  7968. setcookie($cookieName, $token, 0, '', '', $secure);
  7969. }
  7970. }
  7971. return $token;
  7972. }
  7973. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  7974. {
  7975. $token = $this->getToken();
  7976. $method = $request->getMethod();
  7977. $excludeMethods = $this->getArrayProperty('excludeMethods', 'OPTIONS,GET');
  7978. if (!in_array($method, $excludeMethods)) {
  7979. $headerName = $this->getProperty('headerName', 'X-XSRF-TOKEN');
  7980. if ($token != $request->getHeader($headerName)) {
  7981. return $this->responder->error(ErrorCode::BAD_OR_MISSING_XSRF_TOKEN, '');
  7982. }
  7983. }
  7984. return $next->handle($request);
  7985. }
  7986. }
  7987. }
  7988. // file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiBuilder.php
  7989. namespace Tqdev\PhpCrudApi\OpenApi {
  7990. use Tqdev\PhpCrudApi\Column\ReflectionService;
  7991. use Tqdev\PhpCrudApi\OpenApi\OpenApiDefinition;
  7992. class OpenApiBuilder
  7993. {
  7994. private $openapi;
  7995. private $records;
  7996. private $columns;
  7997. private $builders;
  7998. public function __construct(ReflectionService $reflection, array $base, array $controllers, array $builders)
  7999. {
  8000. $this->openapi = new OpenApiDefinition($base);
  8001. $this->records = in_array('records', $controllers) ? new OpenApiRecordsBuilder($this->openapi, $reflection) : null;
  8002. $this->columns = in_array('columns', $controllers) ? new OpenApiColumnsBuilder($this->openapi) : null;
  8003. $this->builders = array();
  8004. foreach ($builders as $className) {
  8005. $this->builders[] = new $className($this->openapi, $reflection);
  8006. }
  8007. }
  8008. private function getServerUrl(): string
  8009. {
  8010. $protocol = @$_SERVER['HTTP_X_FORWARDED_PROTO'] ?: @$_SERVER['REQUEST_SCHEME'] ?: ((isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") ? "https" : "http");
  8011. $port = @intval($_SERVER['HTTP_X_FORWARDED_PORT']) ?: @intval($_SERVER["SERVER_PORT"]) ?: (($protocol === 'https') ? 443 : 80);
  8012. $host = @explode(":", $_SERVER['HTTP_HOST'])[0] ?: @$_SERVER['SERVER_NAME'] ?: @$_SERVER['SERVER_ADDR'];
  8013. $port = ($protocol === 'https' && $port === 443) || ($protocol === 'http' && $port === 80) ? '' : ':' . $port;
  8014. $path = @trim(substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '/openapi')), '/');
  8015. return sprintf('%s://%s%s/%s', $protocol, $host, $port, $path);
  8016. }
  8017. public function build(): OpenApiDefinition
  8018. {
  8019. $this->openapi->set("openapi", "3.0.0");
  8020. if (!$this->openapi->has("servers") && isset($_SERVER['REQUEST_URI'])) {
  8021. $this->openapi->set("servers|0|url", $this->getServerUrl());
  8022. }
  8023. if ($this->records) {
  8024. $this->records->build();
  8025. }
  8026. if ($this->columns) {
  8027. $this->columns->build();
  8028. }
  8029. foreach ($this->builders as $builder) {
  8030. $builder->build();
  8031. }
  8032. return $this->openapi;
  8033. }
  8034. }
  8035. }
  8036. // file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiColumnsBuilder.php
  8037. namespace Tqdev\PhpCrudApi\OpenApi {
  8038. use Tqdev\PhpCrudApi\OpenApi\OpenApiDefinition;
  8039. class OpenApiColumnsBuilder
  8040. {
  8041. private $openapi;
  8042. private $operations = [
  8043. 'database' => [
  8044. 'read' => 'get',
  8045. ],
  8046. 'table' => [
  8047. 'create' => 'post',
  8048. 'read' => 'get',
  8049. 'update' => 'put', //rename
  8050. 'delete' => 'delete',
  8051. ],
  8052. 'column' => [
  8053. 'create' => 'post',
  8054. 'read' => 'get',
  8055. 'update' => 'put',
  8056. 'delete' => 'delete',
  8057. ]
  8058. ];
  8059. public function __construct(OpenApiDefinition $openapi)
  8060. {
  8061. $this->openapi = $openapi;
  8062. }
  8063. public function build() /*: void*/
  8064. {
  8065. $this->setPaths();
  8066. $this->openapi->set("components|responses|boolSuccess|description", "boolean indicating success or failure");
  8067. $this->openapi->set("components|responses|boolSuccess|content|application/json|schema|type", "boolean");
  8068. $this->setComponentSchema();
  8069. $this->setComponentResponse();
  8070. $this->setComponentRequestBody();
  8071. $this->setComponentParameters();
  8072. foreach (array_keys($this->operations) as $index => $type) {
  8073. $this->setTag($index, $type);
  8074. }
  8075. }
  8076. private function setPaths() /*: void*/
  8077. {
  8078. foreach (array_keys($this->operations) as $type) {
  8079. foreach ($this->operations[$type] as $operation => $method) {
  8080. $parameters = [];
  8081. switch ($type) {
  8082. case 'database':
  8083. $path = '/columns';
  8084. break;
  8085. case 'table':
  8086. $path = $operation == 'create' ? '/columns' : '/columns/{table}';
  8087. break;
  8088. case 'column':
  8089. $path = $operation == 'create' ? '/columns/{table}' : '/columns/{table}/{column}';
  8090. break;
  8091. }
  8092. if (strpos($path, '{table}')) {
  8093. $parameters[] = 'table';
  8094. }
  8095. if (strpos($path, '{column}')) {
  8096. $parameters[] = 'column';
  8097. }
  8098. foreach ($parameters as $p => $parameter) {
  8099. $this->openapi->set("paths|$path|$method|parameters|$p|\$ref", "#/components/parameters/$parameter");
  8100. }
  8101. $operationType = $operation . ucfirst($type);
  8102. if (in_array($operation, ['create', 'update'])) {
  8103. $this->openapi->set("paths|$path|$method|requestBody|\$ref", "#/components/requestBodies/$operationType");
  8104. }
  8105. $this->openapi->set("paths|$path|$method|tags|0", "$type");
  8106. $this->openapi->set("paths|$path|$method|operationId", "$operation" . "_" . "$type");
  8107. if ($operationType == 'updateTable') {
  8108. $this->openapi->set("paths|$path|$method|description", "rename table");
  8109. } else {
  8110. $this->openapi->set("paths|$path|$method|description", "$operation $type");
  8111. }
  8112. switch ($operation) {
  8113. case 'read':
  8114. $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/$operationType");
  8115. break;
  8116. case 'create':
  8117. case 'update':
  8118. case 'delete':
  8119. $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/boolSuccess");
  8120. break;
  8121. }
  8122. }
  8123. }
  8124. }
  8125. private function setComponentSchema() /*: void*/
  8126. {
  8127. foreach (array_keys($this->operations) as $type) {
  8128. foreach (array_keys($this->operations[$type]) as $operation) {
  8129. if ($operation == 'delete') {
  8130. continue;
  8131. }
  8132. $operationType = $operation . ucfirst($type);
  8133. $prefix = "components|schemas|$operationType";
  8134. $this->openapi->set("$prefix|type", "object");
  8135. switch ($type) {
  8136. case 'database':
  8137. $this->openapi->set("$prefix|properties|tables|type", 'array');
  8138. $this->openapi->set("$prefix|properties|tables|items|\$ref", "#/components/schemas/readTable");
  8139. break;
  8140. case 'table':
  8141. if ($operation == 'update') {
  8142. $this->openapi->set("$prefix|required", ['name']);
  8143. $this->openapi->set("$prefix|properties|name|type", 'string');
  8144. } else {
  8145. $this->openapi->set("$prefix|properties|name|type", 'string');
  8146. if ($operation == 'read') {
  8147. $this->openapi->set("$prefix|properties|type|type", 'string');
  8148. }
  8149. $this->openapi->set("$prefix|properties|columns|type", 'array');
  8150. $this->openapi->set("$prefix|properties|columns|items|\$ref", "#/components/schemas/readColumn");
  8151. }
  8152. break;
  8153. case 'column':
  8154. $this->openapi->set("$prefix|required", ['name', 'type']);
  8155. $this->openapi->set("$prefix|properties|name|type", 'string');
  8156. $this->openapi->set("$prefix|properties|type|type", 'string');
  8157. $this->openapi->set("$prefix|properties|length|type", 'integer');
  8158. $this->openapi->set("$prefix|properties|length|format", "int64");
  8159. $this->openapi->set("$prefix|properties|precision|type", 'integer');
  8160. $this->openapi->set("$prefix|properties|precision|format", "int64");
  8161. $this->openapi->set("$prefix|properties|scale|type", 'integer');
  8162. $this->openapi->set("$prefix|properties|scale|format", "int64");
  8163. $this->openapi->set("$prefix|properties|nullable|type", 'boolean');
  8164. $this->openapi->set("$prefix|properties|pk|type", 'boolean');
  8165. $this->openapi->set("$prefix|properties|fk|type", 'string');
  8166. break;
  8167. }
  8168. }
  8169. }
  8170. }
  8171. private function setComponentResponse() /*: void*/
  8172. {
  8173. foreach (array_keys($this->operations) as $type) {
  8174. foreach (array_keys($this->operations[$type]) as $operation) {
  8175. if ($operation != 'read') {
  8176. continue;
  8177. }
  8178. $operationType = $operation . ucfirst($type);
  8179. $this->openapi->set("components|responses|$operationType|description", "single $type record");
  8180. $this->openapi->set("components|responses|$operationType|content|application/json|schema|\$ref", "#/components/schemas/$operationType");
  8181. }
  8182. }
  8183. }
  8184. private function setComponentRequestBody() /*: void*/
  8185. {
  8186. foreach (array_keys($this->operations) as $type) {
  8187. foreach (array_keys($this->operations[$type]) as $operation) {
  8188. if (!in_array($operation, ['create', 'update'])) {
  8189. continue;
  8190. }
  8191. $operationType = $operation . ucfirst($type);
  8192. $this->openapi->set("components|requestBodies|$operationType|description", "single $type record");
  8193. $this->openapi->set("components|requestBodies|$operationType|content|application/json|schema|\$ref", "#/components/schemas/$operationType");
  8194. }
  8195. }
  8196. }
  8197. private function setComponentParameters() /*: void*/
  8198. {
  8199. $this->openapi->set("components|parameters|table|name", "table");
  8200. $this->openapi->set("components|parameters|table|in", "path");
  8201. $this->openapi->set("components|parameters|table|schema|type", "string");
  8202. $this->openapi->set("components|parameters|table|description", "table name");
  8203. $this->openapi->set("components|parameters|table|required", true);
  8204. $this->openapi->set("components|parameters|column|name", "column");
  8205. $this->openapi->set("components|parameters|column|in", "path");
  8206. $this->openapi->set("components|parameters|column|schema|type", "string");
  8207. $this->openapi->set("components|parameters|column|description", "column name");
  8208. $this->openapi->set("components|parameters|column|required", true);
  8209. }
  8210. private function setTag(int $index, string $type) /*: void*/
  8211. {
  8212. $this->openapi->set("tags|$index|name", "$type");
  8213. $this->openapi->set("tags|$index|description", "$type operations");
  8214. }
  8215. }
  8216. }
  8217. // file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiDefinition.php
  8218. namespace Tqdev\PhpCrudApi\OpenApi {
  8219. class OpenApiDefinition implements \JsonSerializable
  8220. {
  8221. private $root;
  8222. public function __construct(array $base)
  8223. {
  8224. $this->root = $base;
  8225. }
  8226. public function set(string $path, $value) /*: void*/
  8227. {
  8228. $parts = explode('|', trim($path, '|'));
  8229. $current = &$this->root;
  8230. while (count($parts) > 0) {
  8231. $part = array_shift($parts);
  8232. if (!isset($current[$part])) {
  8233. $current[$part] = [];
  8234. }
  8235. $current = &$current[$part];
  8236. }
  8237. $current = $value;
  8238. }
  8239. public function has(string $path): bool
  8240. {
  8241. $parts = explode('|', trim($path, '|'));
  8242. $current = &$this->root;
  8243. while (count($parts) > 0) {
  8244. $part = array_shift($parts);
  8245. if (!isset($current[$part])) {
  8246. return false;
  8247. }
  8248. $current = &$current[$part];
  8249. }
  8250. return true;
  8251. }
  8252. public function jsonSerialize()
  8253. {
  8254. return $this->root;
  8255. }
  8256. }
  8257. }
  8258. // file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiRecordsBuilder.php
  8259. namespace Tqdev\PhpCrudApi\OpenApi {
  8260. use Tqdev\PhpCrudApi\Column\ReflectionService;
  8261. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedColumn;
  8262. use Tqdev\PhpCrudApi\Middleware\Communication\VariableStore;
  8263. use Tqdev\PhpCrudApi\OpenApi\OpenApiDefinition;
  8264. class OpenApiRecordsBuilder
  8265. {
  8266. private $openapi;
  8267. private $reflection;
  8268. private $operations = [
  8269. 'list' => 'get',
  8270. 'create' => 'post',
  8271. 'read' => 'get',
  8272. 'update' => 'put',
  8273. 'delete' => 'delete',
  8274. 'increment' => 'patch',
  8275. ];
  8276. private $types = [
  8277. 'integer' => ['type' => 'integer', 'format' => 'int32'],
  8278. 'bigint' => ['type' => 'integer', 'format' => 'int64'],
  8279. 'varchar' => ['type' => 'string'],
  8280. 'clob' => ['type' => 'string', 'format' => 'large-string'], //custom format
  8281. 'varbinary' => ['type' => 'string', 'format' => 'byte'],
  8282. 'blob' => ['type' => 'string', 'format' => 'large-byte'], //custom format
  8283. 'decimal' => ['type' => 'string', 'format' => 'decimal'], //custom format
  8284. 'float' => ['type' => 'number', 'format' => 'float'],
  8285. 'double' => ['type' => 'number', 'format' => 'double'],
  8286. 'date' => ['type' => 'string', 'format' => 'date'],
  8287. 'time' => ['type' => 'string', 'format' => 'time'], //custom format
  8288. 'timestamp' => ['type' => 'string', 'format' => 'date-time'],
  8289. 'geometry' => ['type' => 'string', 'format' => 'geometry'], //custom format
  8290. 'boolean' => ['type' => 'boolean'],
  8291. ];
  8292. public function __construct(OpenApiDefinition $openapi, ReflectionService $reflection)
  8293. {
  8294. $this->openapi = $openapi;
  8295. $this->reflection = $reflection;
  8296. }
  8297. private function getAllTableReferences(): array
  8298. {
  8299. $tableReferences = array();
  8300. foreach ($this->reflection->getTableNames() as $tableName) {
  8301. $table = $this->reflection->getTable($tableName);
  8302. foreach ($table->getColumnNames() as $columnName) {
  8303. $column = $table->getColumn($columnName);
  8304. $referencedTableName = $column->getFk();
  8305. if ($referencedTableName) {
  8306. if (!isset($tableReferences[$referencedTableName])) {
  8307. $tableReferences[$referencedTableName] = array();
  8308. }
  8309. $tableReferences[$referencedTableName][] = "$tableName.$columnName";
  8310. }
  8311. }
  8312. }
  8313. return $tableReferences;
  8314. }
  8315. public function build() /*: void*/
  8316. {
  8317. $tableNames = $this->reflection->getTableNames();
  8318. foreach ($tableNames as $tableName) {
  8319. $this->setPath($tableName);
  8320. }
  8321. $this->openapi->set("components|responses|pk_integer|description", "inserted primary key value (integer)");
  8322. $this->openapi->set("components|responses|pk_integer|content|application/json|schema|type", "integer");
  8323. $this->openapi->set("components|responses|pk_integer|content|application/json|schema|format", "int64");
  8324. $this->openapi->set("components|responses|pk_string|description", "inserted primary key value (string)");
  8325. $this->openapi->set("components|responses|pk_string|content|application/json|schema|type", "string");
  8326. $this->openapi->set("components|responses|pk_string|content|application/json|schema|format", "uuid");
  8327. $this->openapi->set("components|responses|rows_affected|description", "number of rows affected (integer)");
  8328. $this->openapi->set("components|responses|rows_affected|content|application/json|schema|type", "integer");
  8329. $this->openapi->set("components|responses|rows_affected|content|application/json|schema|format", "int64");
  8330. $tableReferences = $this->getAllTableReferences();
  8331. foreach ($tableNames as $tableName) {
  8332. $references = isset($tableReferences[$tableName]) ? $tableReferences[$tableName] : array();
  8333. $this->setComponentSchema($tableName, $references);
  8334. $this->setComponentResponse($tableName);
  8335. $this->setComponentRequestBody($tableName);
  8336. }
  8337. $this->setComponentParameters();
  8338. foreach ($tableNames as $index => $tableName) {
  8339. $this->setTag($index, $tableName);
  8340. }
  8341. }
  8342. private function isOperationOnTableAllowed(string $operation, string $tableName): bool
  8343. {
  8344. $tableHandler = VariableStore::get('authorization.tableHandler');
  8345. if (!$tableHandler) {
  8346. return true;
  8347. }
  8348. return (bool) call_user_func($tableHandler, $operation, $tableName);
  8349. }
  8350. private function isOperationOnColumnAllowed(string $operation, string $tableName, string $columnName): bool
  8351. {
  8352. $columnHandler = VariableStore::get('authorization.columnHandler');
  8353. if (!$columnHandler) {
  8354. return true;
  8355. }
  8356. return (bool) call_user_func($columnHandler, $operation, $tableName, $columnName);
  8357. }
  8358. private function setPath(string $tableName) /*: void*/
  8359. {
  8360. $table = $this->reflection->getTable($tableName);
  8361. $type = $table->getType();
  8362. $pk = $table->getPk();
  8363. $pkName = $pk ? $pk->getName() : '';
  8364. foreach ($this->operations as $operation => $method) {
  8365. if (!$pkName && $operation != 'list') {
  8366. continue;
  8367. }
  8368. if ($type != 'table' && $operation != 'list') {
  8369. continue;
  8370. }
  8371. if (!$this->isOperationOnTableAllowed($operation, $tableName)) {
  8372. continue;
  8373. }
  8374. $parameters = [];
  8375. if (in_array($operation, ['list', 'create'])) {
  8376. $path = sprintf('/records/%s', $tableName);
  8377. if ($operation == 'list') {
  8378. $parameters = ['filter', 'include', 'exclude', 'order', 'size', 'page', 'join'];
  8379. }
  8380. } else {
  8381. $path = sprintf('/records/%s/{id}', $tableName);
  8382. if ($operation == 'read') {
  8383. $parameters = ['pk', 'include', 'exclude', 'join'];
  8384. } else {
  8385. $parameters = ['pk'];
  8386. }
  8387. }
  8388. foreach ($parameters as $p => $parameter) {
  8389. $this->openapi->set("paths|$path|$method|parameters|$p|\$ref", "#/components/parameters/$parameter");
  8390. }
  8391. if (in_array($operation, ['create', 'update', 'increment'])) {
  8392. $this->openapi->set("paths|$path|$method|requestBody|\$ref", "#/components/requestBodies/$operation-" . rawurlencode($tableName));
  8393. }
  8394. $this->openapi->set("paths|$path|$method|tags|0", "$tableName");
  8395. $this->openapi->set("paths|$path|$method|operationId", "$operation" . "_" . "$tableName");
  8396. $this->openapi->set("paths|$path|$method|description", "$operation $tableName");
  8397. switch ($operation) {
  8398. case 'list':
  8399. $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/$operation-" . rawurlencode($tableName));
  8400. break;
  8401. case 'create':
  8402. if ($pk->getType() == 'integer') {
  8403. $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/pk_integer");
  8404. } else {
  8405. $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/pk_string");
  8406. }
  8407. break;
  8408. case 'read':
  8409. $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/$operation-" . rawurlencode($tableName));
  8410. break;
  8411. case 'update':
  8412. case 'delete':
  8413. case 'increment':
  8414. $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/rows_affected");
  8415. break;
  8416. }
  8417. }
  8418. }
  8419. private function getPattern(ReflectedColumn $column): string
  8420. {
  8421. switch ($column->getType()) {
  8422. case 'integer':
  8423. $n = strlen(pow(2, 31));
  8424. return '^-?[0-9]{1,' . $n . '}$';
  8425. case 'bigint':
  8426. $n = strlen(pow(2, 63));
  8427. return '^-?[0-9]{1,' . $n . '}$';
  8428. case 'varchar':
  8429. $l = $column->getLength();
  8430. return '^.{0,' . $l . '}$';
  8431. case 'clob':
  8432. return '^.*$';
  8433. case 'varbinary':
  8434. $l = $column->getLength();
  8435. $b = (int) 4 * ceil($l / 3);
  8436. return '^[A-Za-z0-9+/]{0,' . $b . '}=*$';
  8437. case 'blob':
  8438. return '^[A-Za-z0-9+/]*=*$';
  8439. case 'decimal':
  8440. $p = $column->getPrecision();
  8441. $s = $column->getScale();
  8442. return '^-?[0-9]{1,' . ($p - $s) . '}(\.[0-9]{1,' . $s . '})?$';
  8443. case 'float':
  8444. return '^-?[0-9]+(\.[0-9]+)?([eE]-?[0-9]+)?$';
  8445. case 'double':
  8446. return '^-?[0-9]+(\.[0-9]+)?([eE]-?[0-9]+)?$';
  8447. case 'date':
  8448. return '^[0-9]{4}-[0-9]{2}-[0-9]{2}$';
  8449. case 'time':
  8450. return '^[0-9]{2}:[0-9]{2}:[0-9]{2}$';
  8451. case 'timestamp':
  8452. return '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$';
  8453. return '';
  8454. case 'geometry':
  8455. return '^(POINT|LINESTRING|POLYGON|MULTIPOINT|MULTILINESTRING|MULTIPOLYGON)\s*\(.*$';
  8456. case 'boolean':
  8457. return '^(true|false)$';
  8458. }
  8459. return '';
  8460. }
  8461. private function setComponentSchema(string $tableName, array $references) /*: void*/
  8462. {
  8463. $table = $this->reflection->getTable($tableName);
  8464. $type = $table->getType();
  8465. $pk = $table->getPk();
  8466. $pkName = $pk ? $pk->getName() : '';
  8467. foreach ($this->operations as $operation => $method) {
  8468. if (!$pkName && $operation != 'list') {
  8469. continue;
  8470. }
  8471. if ($type == 'view' && !in_array($operation, array('read', 'list'))) {
  8472. continue;
  8473. }
  8474. if ($type == 'view' && !$pkName && $operation == 'read') {
  8475. continue;
  8476. }
  8477. if ($operation == 'delete') {
  8478. continue;
  8479. }
  8480. if (!$this->isOperationOnTableAllowed($operation, $tableName)) {
  8481. continue;
  8482. }
  8483. if ($operation == 'list') {
  8484. $this->openapi->set("components|schemas|$operation-$tableName|type", "object");
  8485. $this->openapi->set("components|schemas|$operation-$tableName|properties|results|type", "integer");
  8486. $this->openapi->set("components|schemas|$operation-$tableName|properties|results|format", "int64");
  8487. $this->openapi->set("components|schemas|$operation-$tableName|properties|records|type", "array");
  8488. $prefix = "components|schemas|$operation-$tableName|properties|records|items";
  8489. } else {
  8490. $prefix = "components|schemas|$operation-$tableName";
  8491. }
  8492. $this->openapi->set("$prefix|type", "object");
  8493. foreach ($table->getColumnNames() as $columnName) {
  8494. if (!$this->isOperationOnColumnAllowed($operation, $tableName, $columnName)) {
  8495. continue;
  8496. }
  8497. $column = $table->getColumn($columnName);
  8498. $properties = $this->types[$column->getType()];
  8499. $properties['maxLength'] = $column->hasLength() ? $column->getLength() : 0;
  8500. $properties['nullable'] = $column->getNullable();
  8501. $properties['pattern'] = $this->getPattern($column);
  8502. foreach ($properties as $key => $value) {
  8503. if ($value) {
  8504. $this->openapi->set("$prefix|properties|$columnName|$key", $value);
  8505. }
  8506. }
  8507. if ($column->getPk()) {
  8508. $this->openapi->set("$prefix|properties|$columnName|x-primary-key", true);
  8509. $this->openapi->set("$prefix|properties|$columnName|x-referenced", $references);
  8510. }
  8511. $fk = $column->getFk();
  8512. if ($fk) {
  8513. $this->openapi->set("$prefix|properties|$columnName|x-references", $fk);
  8514. }
  8515. }
  8516. }
  8517. }
  8518. private function setComponentResponse(string $tableName) /*: void*/
  8519. {
  8520. $table = $this->reflection->getTable($tableName);
  8521. $type = $table->getType();
  8522. $pk = $table->getPk();
  8523. $pkName = $pk ? $pk->getName() : '';
  8524. foreach (['list', 'read'] as $operation) {
  8525. if (!$pkName && $operation != 'list') {
  8526. continue;
  8527. }
  8528. if ($type != 'table' && $operation != 'list') {
  8529. continue;
  8530. }
  8531. if (!$this->isOperationOnTableAllowed($operation, $tableName)) {
  8532. continue;
  8533. }
  8534. if ($operation == 'list') {
  8535. $this->openapi->set("components|responses|$operation-$tableName|description", "list of $tableName records");
  8536. } else {
  8537. $this->openapi->set("components|responses|$operation-$tableName|description", "single $tableName record");
  8538. }
  8539. $this->openapi->set("components|responses|$operation-$tableName|content|application/json|schema|\$ref", "#/components/schemas/$operation-" . rawurlencode($tableName));
  8540. }
  8541. }
  8542. private function setComponentRequestBody(string $tableName) /*: void*/
  8543. {
  8544. $table = $this->reflection->getTable($tableName);
  8545. $type = $table->getType();
  8546. $pk = $table->getPk();
  8547. $pkName = $pk ? $pk->getName() : '';
  8548. if ($pkName && $type == 'table') {
  8549. foreach (['create', 'update', 'increment'] as $operation) {
  8550. if (!$this->isOperationOnTableAllowed($operation, $tableName)) {
  8551. continue;
  8552. }
  8553. $this->openapi->set("components|requestBodies|$operation-$tableName|description", "single $tableName record");
  8554. $this->openapi->set("components|requestBodies|$operation-$tableName|content|application/json|schema|\$ref", "#/components/schemas/$operation-" . rawurlencode($tableName));
  8555. }
  8556. }
  8557. }
  8558. private function setComponentParameters() /*: void*/
  8559. {
  8560. $this->openapi->set("components|parameters|pk|name", "id");
  8561. $this->openapi->set("components|parameters|pk|in", "path");
  8562. $this->openapi->set("components|parameters|pk|schema|type", "string");
  8563. $this->openapi->set("components|parameters|pk|description", "primary key value");
  8564. $this->openapi->set("components|parameters|pk|required", true);
  8565. $this->openapi->set("components|parameters|filter|name", "filter");
  8566. $this->openapi->set("components|parameters|filter|in", "query");
  8567. $this->openapi->set("components|parameters|filter|schema|type", "array");
  8568. $this->openapi->set("components|parameters|filter|schema|items|type", "string");
  8569. $this->openapi->set("components|parameters|filter|description", "Filters to be applied. Each filter consists of a column, an operator and a value (comma separated). Example: id,eq,1");
  8570. $this->openapi->set("components|parameters|filter|required", false);
  8571. $this->openapi->set("components|parameters|include|name", "include");
  8572. $this->openapi->set("components|parameters|include|in", "query");
  8573. $this->openapi->set("components|parameters|include|schema|type", "string");
  8574. $this->openapi->set("components|parameters|include|description", "Columns you want to include in the output (comma separated). Example: posts.*,categories.name");
  8575. $this->openapi->set("components|parameters|include|required", false);
  8576. $this->openapi->set("components|parameters|exclude|name", "exclude");
  8577. $this->openapi->set("components|parameters|exclude|in", "query");
  8578. $this->openapi->set("components|parameters|exclude|schema|type", "string");
  8579. $this->openapi->set("components|parameters|exclude|description", "Columns you want to exclude from the output (comma separated). Example: posts.content");
  8580. $this->openapi->set("components|parameters|exclude|required", false);
  8581. $this->openapi->set("components|parameters|order|name", "order");
  8582. $this->openapi->set("components|parameters|order|in", "query");
  8583. $this->openapi->set("components|parameters|order|schema|type", "array");
  8584. $this->openapi->set("components|parameters|order|schema|items|type", "string");
  8585. $this->openapi->set("components|parameters|order|description", "Column you want to sort on and the sort direction (comma separated). Example: id,desc");
  8586. $this->openapi->set("components|parameters|order|required", false);
  8587. $this->openapi->set("components|parameters|size|name", "size");
  8588. $this->openapi->set("components|parameters|size|in", "query");
  8589. $this->openapi->set("components|parameters|size|schema|type", "string");
  8590. $this->openapi->set("components|parameters|size|description", "Maximum number of results (for top lists). Example: 10");
  8591. $this->openapi->set("components|parameters|size|required", false);
  8592. $this->openapi->set("components|parameters|page|name", "page");
  8593. $this->openapi->set("components|parameters|page|in", "query");
  8594. $this->openapi->set("components|parameters|page|schema|type", "string");
  8595. $this->openapi->set("components|parameters|page|description", "Page number and page size (comma separated). Example: 1,10");
  8596. $this->openapi->set("components|parameters|page|required", false);
  8597. $this->openapi->set("components|parameters|join|name", "join");
  8598. $this->openapi->set("components|parameters|join|in", "query");
  8599. $this->openapi->set("components|parameters|join|schema|type", "array");
  8600. $this->openapi->set("components|parameters|join|schema|items|type", "string");
  8601. $this->openapi->set("components|parameters|join|description", "Paths (comma separated) to related entities that you want to include. Example: comments,users");
  8602. $this->openapi->set("components|parameters|join|required", false);
  8603. }
  8604. private function setTag(int $index, string $tableName) /*: void*/
  8605. {
  8606. $this->openapi->set("tags|$index|name", "$tableName");
  8607. $this->openapi->set("tags|$index|description", "$tableName operations");
  8608. }
  8609. }
  8610. }
  8611. // file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiService.php
  8612. namespace Tqdev\PhpCrudApi\OpenApi {
  8613. use Tqdev\PhpCrudApi\Column\ReflectionService;
  8614. use Tqdev\PhpCrudApi\OpenApi\OpenApiBuilder;
  8615. class OpenApiService
  8616. {
  8617. private $builder;
  8618. public function __construct(ReflectionService $reflection, array $base, array $controllers, array $customBuilders)
  8619. {
  8620. $this->builder = new OpenApiBuilder($reflection, $base, $controllers, $customBuilders);
  8621. }
  8622. public function get(): OpenApiDefinition
  8623. {
  8624. return $this->builder->build();
  8625. }
  8626. }
  8627. }
  8628. // file: src/Tqdev/PhpCrudApi/Record/Condition/AndCondition.php
  8629. namespace Tqdev\PhpCrudApi\Record\Condition {
  8630. class AndCondition extends Condition
  8631. {
  8632. private $conditions;
  8633. public function __construct(Condition $condition1, Condition $condition2)
  8634. {
  8635. $this->conditions = [$condition1, $condition2];
  8636. }
  8637. public function _and(Condition $condition): Condition
  8638. {
  8639. if ($condition instanceof NoCondition) {
  8640. return $this;
  8641. }
  8642. $this->conditions[] = $condition;
  8643. return $this;
  8644. }
  8645. public function getConditions(): array
  8646. {
  8647. return $this->conditions;
  8648. }
  8649. public static function fromArray(array $conditions): Condition
  8650. {
  8651. $condition = new NoCondition();
  8652. foreach ($conditions as $c) {
  8653. $condition = $condition->_and($c);
  8654. }
  8655. return $condition;
  8656. }
  8657. }
  8658. }
  8659. // file: src/Tqdev/PhpCrudApi/Record/Condition/ColumnCondition.php
  8660. namespace Tqdev\PhpCrudApi\Record\Condition {
  8661. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedColumn;
  8662. class ColumnCondition extends Condition
  8663. {
  8664. private $column;
  8665. private $operator;
  8666. private $value;
  8667. public function __construct(ReflectedColumn $column, string $operator, string $value)
  8668. {
  8669. $this->column = $column;
  8670. $this->operator = $operator;
  8671. $this->value = $value;
  8672. }
  8673. public function getColumn(): ReflectedColumn
  8674. {
  8675. return $this->column;
  8676. }
  8677. public function getOperator(): string
  8678. {
  8679. return $this->operator;
  8680. }
  8681. public function getValue(): string
  8682. {
  8683. return $this->value;
  8684. }
  8685. }
  8686. }
  8687. // file: src/Tqdev/PhpCrudApi/Record/Condition/Condition.php
  8688. namespace Tqdev\PhpCrudApi\Record\Condition {
  8689. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
  8690. abstract class Condition
  8691. {
  8692. public function _and(Condition $condition): Condition
  8693. {
  8694. if ($condition instanceof NoCondition) {
  8695. return $this;
  8696. }
  8697. return new AndCondition($this, $condition);
  8698. }
  8699. public function _or(Condition $condition): Condition
  8700. {
  8701. if ($condition instanceof NoCondition) {
  8702. return $this;
  8703. }
  8704. return new OrCondition($this, $condition);
  8705. }
  8706. public function _not(): Condition
  8707. {
  8708. return new NotCondition($this);
  8709. }
  8710. public static function fromString(ReflectedTable $table, string $value): Condition
  8711. {
  8712. $condition = new NoCondition();
  8713. $parts = explode(',', $value, 3);
  8714. if (count($parts) < 2) {
  8715. return $condition;
  8716. }
  8717. if (count($parts) < 3) {
  8718. $parts[2] = '';
  8719. }
  8720. $field = $table->getColumn($parts[0]);
  8721. $command = $parts[1];
  8722. $negate = false;
  8723. $spatial = false;
  8724. if (strlen($command) > 2) {
  8725. if (substr($command, 0, 1) == 'n') {
  8726. $negate = true;
  8727. $command = substr($command, 1);
  8728. }
  8729. if (substr($command, 0, 1) == 's') {
  8730. $spatial = true;
  8731. $command = substr($command, 1);
  8732. }
  8733. }
  8734. if ($spatial) {
  8735. if (in_array($command, ['co', 'cr', 'di', 'eq', 'in', 'ov', 'to', 'wi', 'ic', 'is', 'iv'])) {
  8736. $condition = new SpatialCondition($field, $command, $parts[2]);
  8737. }
  8738. } else {
  8739. if (in_array($command, ['cs', 'sw', 'ew', 'eq', 'lt', 'le', 'ge', 'gt', 'bt', 'in', 'is'])) {
  8740. $condition = new ColumnCondition($field, $command, $parts[2]);
  8741. }
  8742. }
  8743. if ($negate) {
  8744. $condition = $condition->_not();
  8745. }
  8746. return $condition;
  8747. }
  8748. }
  8749. }
  8750. // file: src/Tqdev/PhpCrudApi/Record/Condition/NoCondition.php
  8751. namespace Tqdev\PhpCrudApi\Record\Condition {
  8752. class NoCondition extends Condition
  8753. {
  8754. public function _and(Condition $condition): Condition
  8755. {
  8756. return $condition;
  8757. }
  8758. public function _or(Condition $condition): Condition
  8759. {
  8760. return $condition;
  8761. }
  8762. public function _not(): Condition
  8763. {
  8764. return $this;
  8765. }
  8766. }
  8767. }
  8768. // file: src/Tqdev/PhpCrudApi/Record/Condition/NotCondition.php
  8769. namespace Tqdev\PhpCrudApi\Record\Condition {
  8770. class NotCondition extends Condition
  8771. {
  8772. private $condition;
  8773. public function __construct(Condition $condition)
  8774. {
  8775. $this->condition = $condition;
  8776. }
  8777. public function getCondition(): Condition
  8778. {
  8779. return $this->condition;
  8780. }
  8781. }
  8782. }
  8783. // file: src/Tqdev/PhpCrudApi/Record/Condition/OrCondition.php
  8784. namespace Tqdev\PhpCrudApi\Record\Condition {
  8785. class OrCondition extends Condition
  8786. {
  8787. private $conditions;
  8788. public function __construct(Condition $condition1, Condition $condition2)
  8789. {
  8790. $this->conditions = [$condition1, $condition2];
  8791. }
  8792. public function _or(Condition $condition): Condition
  8793. {
  8794. if ($condition instanceof NoCondition) {
  8795. return $this;
  8796. }
  8797. $this->conditions[] = $condition;
  8798. return $this;
  8799. }
  8800. public function getConditions(): array
  8801. {
  8802. return $this->conditions;
  8803. }
  8804. public static function fromArray(array $conditions): Condition
  8805. {
  8806. $condition = new NoCondition();
  8807. foreach ($conditions as $c) {
  8808. $condition = $condition->_or($c);
  8809. }
  8810. return $condition;
  8811. }
  8812. }
  8813. }
  8814. // file: src/Tqdev/PhpCrudApi/Record/Condition/SpatialCondition.php
  8815. namespace Tqdev\PhpCrudApi\Record\Condition {
  8816. class SpatialCondition extends ColumnCondition
  8817. {
  8818. }
  8819. }
  8820. // file: src/Tqdev/PhpCrudApi/Record/Document/ErrorDocument.php
  8821. namespace Tqdev\PhpCrudApi\Record\Document {
  8822. use Tqdev\PhpCrudApi\Record\ErrorCode;
  8823. class ErrorDocument implements \JsonSerializable
  8824. {
  8825. public $errorCode;
  8826. public $argument;
  8827. public $details;
  8828. public function __construct(ErrorCode $errorCode, string $argument, $details)
  8829. {
  8830. $this->errorCode = $errorCode;
  8831. $this->argument = $argument;
  8832. $this->details = $details;
  8833. }
  8834. public function getStatus(): int
  8835. {
  8836. return $this->errorCode->getStatus();
  8837. }
  8838. public function getCode(): int
  8839. {
  8840. return $this->errorCode->getCode();
  8841. }
  8842. public function getMessage(): string
  8843. {
  8844. return $this->errorCode->getMessage($this->argument);
  8845. }
  8846. public function serialize()
  8847. {
  8848. return [
  8849. 'code' => $this->getCode(),
  8850. 'message' => $this->getMessage(),
  8851. 'details' => $this->details,
  8852. ];
  8853. }
  8854. public function jsonSerialize()
  8855. {
  8856. return array_filter($this->serialize(), function($v) {return $v!==null;});
  8857. }
  8858. public static function fromException(\Throwable $exception)
  8859. {
  8860. $document = new ErrorDocument(new ErrorCode(ErrorCode::ERROR_NOT_FOUND), $exception->getMessage(), null);
  8861. if ($exception instanceof \PDOException) {
  8862. if (strpos(strtolower($exception->getMessage()), 'duplicate') !== false) {
  8863. $document = new ErrorDocument(new ErrorCode(ErrorCode::DUPLICATE_KEY_EXCEPTION), '', null);
  8864. } elseif (strpos(strtolower($exception->getMessage()), 'unique constraint') !== false) {
  8865. $document = new ErrorDocument(new ErrorCode(ErrorCode::DUPLICATE_KEY_EXCEPTION), '', null);
  8866. } elseif (strpos(strtolower($exception->getMessage()), 'default value') !== false) {
  8867. $document = new ErrorDocument(new ErrorCode(ErrorCode::DATA_INTEGRITY_VIOLATION), '', null);
  8868. } elseif (strpos(strtolower($exception->getMessage()), 'allow nulls') !== false) {
  8869. $document = new ErrorDocument(new ErrorCode(ErrorCode::DATA_INTEGRITY_VIOLATION), '', null);
  8870. } elseif (strpos(strtolower($exception->getMessage()), 'constraint') !== false) {
  8871. $document = new ErrorDocument(new ErrorCode(ErrorCode::DATA_INTEGRITY_VIOLATION), '', null);
  8872. } else {
  8873. $document = new ErrorDocument(new ErrorCode(ErrorCode::ERROR_NOT_FOUND), '', null);
  8874. }
  8875. }
  8876. return $document;
  8877. }
  8878. }
  8879. }
  8880. // file: src/Tqdev/PhpCrudApi/Record/Document/ListDocument.php
  8881. namespace Tqdev\PhpCrudApi\Record\Document {
  8882. class ListDocument implements \JsonSerializable
  8883. {
  8884. private $records;
  8885. private $results;
  8886. public function __construct(array $records, int $results)
  8887. {
  8888. $this->records = $records;
  8889. $this->results = $results;
  8890. }
  8891. public function getRecords(): array
  8892. {
  8893. return $this->records;
  8894. }
  8895. public function getResults(): int
  8896. {
  8897. return $this->results;
  8898. }
  8899. public function serialize()
  8900. {
  8901. return [
  8902. 'records' => $this->records,
  8903. 'results' => $this->results,
  8904. ];
  8905. }
  8906. public function jsonSerialize()
  8907. {
  8908. return array_filter($this->serialize(), function ($v) {
  8909. return $v !== 0;
  8910. });
  8911. }
  8912. }
  8913. }
  8914. // file: src/Tqdev/PhpCrudApi/Record/ColumnIncluder.php
  8915. namespace Tqdev\PhpCrudApi\Record {
  8916. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
  8917. class ColumnIncluder
  8918. {
  8919. private function isMandatory(string $tableName, string $columnName, array $params): bool
  8920. {
  8921. return isset($params['mandatory']) && in_array($tableName . "." . $columnName, $params['mandatory']);
  8922. }
  8923. private function select(
  8924. string $tableName,
  8925. bool $primaryTable,
  8926. array $params,
  8927. string $paramName,
  8928. array $columnNames,
  8929. bool $include
  8930. ): array {
  8931. if (!isset($params[$paramName])) {
  8932. return $columnNames;
  8933. }
  8934. $columns = array();
  8935. foreach (explode(',', $params[$paramName][0]) as $columnName) {
  8936. $columns[$columnName] = true;
  8937. }
  8938. $result = array();
  8939. foreach ($columnNames as $columnName) {
  8940. $match = isset($columns['*.*']);
  8941. if (!$match) {
  8942. $match = isset($columns[$tableName . '.*']) || isset($columns[$tableName . '.' . $columnName]);
  8943. }
  8944. if ($primaryTable && !$match) {
  8945. $match = isset($columns['*']) || isset($columns[$columnName]);
  8946. }
  8947. if ($match) {
  8948. if ($include || $this->isMandatory($tableName, $columnName, $params)) {
  8949. $result[] = $columnName;
  8950. }
  8951. } else {
  8952. if (!$include || $this->isMandatory($tableName, $columnName, $params)) {
  8953. $result[] = $columnName;
  8954. }
  8955. }
  8956. }
  8957. return $result;
  8958. }
  8959. public function getNames(ReflectedTable $table, bool $primaryTable, array $params): array
  8960. {
  8961. $tableName = $table->getName();
  8962. $results = $table->getColumnNames();
  8963. $results = $this->select($tableName, $primaryTable, $params, 'include', $results, true);
  8964. $results = $this->select($tableName, $primaryTable, $params, 'exclude', $results, false);
  8965. return $results;
  8966. }
  8967. public function getValues(ReflectedTable $table, bool $primaryTable, /* object */ $record, array $params): array
  8968. {
  8969. $results = array();
  8970. $columnNames = $this->getNames($table, $primaryTable, $params);
  8971. foreach ($columnNames as $columnName) {
  8972. if (property_exists($record, $columnName)) {
  8973. $results[$columnName] = $record->$columnName;
  8974. }
  8975. }
  8976. return $results;
  8977. }
  8978. }
  8979. }
  8980. // file: src/Tqdev/PhpCrudApi/Record/ErrorCode.php
  8981. namespace Tqdev\PhpCrudApi\Record {
  8982. use Tqdev\PhpCrudApi\ResponseFactory;
  8983. class ErrorCode
  8984. {
  8985. private $code;
  8986. private $message;
  8987. private $status;
  8988. const ERROR_NOT_FOUND = 9999;
  8989. const ROUTE_NOT_FOUND = 1000;
  8990. const TABLE_NOT_FOUND = 1001;
  8991. const ARGUMENT_COUNT_MISMATCH = 1002;
  8992. const RECORD_NOT_FOUND = 1003;
  8993. const ORIGIN_FORBIDDEN = 1004;
  8994. const COLUMN_NOT_FOUND = 1005;
  8995. const TABLE_ALREADY_EXISTS = 1006;
  8996. const COLUMN_ALREADY_EXISTS = 1007;
  8997. const HTTP_MESSAGE_NOT_READABLE = 1008;
  8998. const DUPLICATE_KEY_EXCEPTION = 1009;
  8999. const DATA_INTEGRITY_VIOLATION = 1010;
  9000. const AUTHENTICATION_REQUIRED = 1011;
  9001. const AUTHENTICATION_FAILED = 1012;
  9002. const INPUT_VALIDATION_FAILED = 1013;
  9003. const OPERATION_FORBIDDEN = 1014;
  9004. const OPERATION_NOT_SUPPORTED = 1015;
  9005. const TEMPORARY_OR_PERMANENTLY_BLOCKED = 1016;
  9006. const BAD_OR_MISSING_XSRF_TOKEN = 1017;
  9007. const ONLY_AJAX_REQUESTS_ALLOWED = 1018;
  9008. const PAGINATION_FORBIDDEN = 1019;
  9009. const USER_ALREADY_EXIST = 1020;
  9010. const PASSWORD_TOO_SHORT = 1021;
  9011. private $values = [
  9012. 0000 => ["Success", ResponseFactory::OK],
  9013. 1000 => ["Route '%s' not found", ResponseFactory::NOT_FOUND],
  9014. 1001 => ["Table '%s' not found", ResponseFactory::NOT_FOUND],
  9015. 1002 => ["Argument count mismatch in '%s'", ResponseFactory::UNPROCESSABLE_ENTITY],
  9016. 1003 => ["Record '%s' not found", ResponseFactory::NOT_FOUND],
  9017. 1004 => ["Origin '%s' is forbidden", ResponseFactory::FORBIDDEN],
  9018. 1005 => ["Column '%s' not found", ResponseFactory::NOT_FOUND],
  9019. 1006 => ["Table '%s' already exists", ResponseFactory::CONFLICT],
  9020. 1007 => ["Column '%s' already exists", ResponseFactory::CONFLICT],
  9021. 1008 => ["Cannot read HTTP message", ResponseFactory::UNPROCESSABLE_ENTITY],
  9022. 1009 => ["Duplicate key exception", ResponseFactory::CONFLICT],
  9023. 1010 => ["Data integrity violation", ResponseFactory::CONFLICT],
  9024. 1011 => ["Authentication required", ResponseFactory::UNAUTHORIZED],
  9025. 1012 => ["Authentication failed for '%s'", ResponseFactory::FORBIDDEN],
  9026. 1013 => ["Input validation failed for '%s'", ResponseFactory::UNPROCESSABLE_ENTITY],
  9027. 1014 => ["Operation forbidden", ResponseFactory::FORBIDDEN],
  9028. 1015 => ["Operation '%s' not supported", ResponseFactory::METHOD_NOT_ALLOWED],
  9029. 1016 => ["Temporary or permanently blocked", ResponseFactory::FORBIDDEN],
  9030. 1017 => ["Bad or missing XSRF token", ResponseFactory::FORBIDDEN],
  9031. 1018 => ["Only AJAX requests allowed for '%s'", ResponseFactory::FORBIDDEN],
  9032. 1019 => ["Pagination forbidden", ResponseFactory::FORBIDDEN],
  9033. 1020 => ["User '%s' already exists", ResponseFactory::CONFLICT],
  9034. 1021 => ["Password too short (<%d characters)", ResponseFactory::UNPROCESSABLE_ENTITY],
  9035. 9999 => ["%s", ResponseFactory::INTERNAL_SERVER_ERROR],
  9036. ];
  9037. public function __construct(int $code)
  9038. {
  9039. if (!isset($this->values[$code])) {
  9040. $code = 9999;
  9041. }
  9042. $this->code = $code;
  9043. $this->message = $this->values[$code][0];
  9044. $this->status = $this->values[$code][1];
  9045. }
  9046. public function getCode(): int
  9047. {
  9048. return $this->code;
  9049. }
  9050. public function getMessage(string $argument): string
  9051. {
  9052. return sprintf($this->message, $argument);
  9053. }
  9054. public function getStatus(): int
  9055. {
  9056. return $this->status;
  9057. }
  9058. }
  9059. }
  9060. // file: src/Tqdev/PhpCrudApi/Record/FilterInfo.php
  9061. namespace Tqdev\PhpCrudApi\Record {
  9062. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
  9063. use Tqdev\PhpCrudApi\Record\Condition\AndCondition;
  9064. use Tqdev\PhpCrudApi\Record\Condition\Condition;
  9065. use Tqdev\PhpCrudApi\Record\Condition\NoCondition;
  9066. use Tqdev\PhpCrudApi\Record\Condition\OrCondition;
  9067. class FilterInfo
  9068. {
  9069. private function getConditionsAsPathTree(ReflectedTable $table, array $params): PathTree
  9070. {
  9071. $conditions = new PathTree();
  9072. foreach ($params as $key => $filters) {
  9073. if (substr($key, 0, 6) == 'filter') {
  9074. preg_match_all('/\d+|\D+/', substr($key, 6), $matches);
  9075. $path = $matches[0];
  9076. foreach ($filters as $filter) {
  9077. $condition = Condition::fromString($table, $filter);
  9078. if (($condition instanceof NoCondition) == false) {
  9079. $conditions->put($path, $condition);
  9080. }
  9081. }
  9082. }
  9083. }
  9084. return $conditions;
  9085. }
  9086. private function combinePathTreeOfConditions(PathTree $tree): Condition
  9087. {
  9088. $andConditions = $tree->getValues();
  9089. $and = AndCondition::fromArray($andConditions);
  9090. $orConditions = [];
  9091. foreach ($tree->getKeys() as $p) {
  9092. $orConditions[] = $this->combinePathTreeOfConditions($tree->get($p));
  9093. }
  9094. $or = OrCondition::fromArray($orConditions);
  9095. return $and->_and($or);
  9096. }
  9097. public function getCombinedConditions(ReflectedTable $table, array $params): Condition
  9098. {
  9099. return $this->combinePathTreeOfConditions($this->getConditionsAsPathTree($table, $params));
  9100. }
  9101. }
  9102. }
  9103. // file: src/Tqdev/PhpCrudApi/Record/HabtmValues.php
  9104. namespace Tqdev\PhpCrudApi\Record {
  9105. class HabtmValues
  9106. {
  9107. public $pkValues;
  9108. public $fkValues;
  9109. public function __construct(array $pkValues, array $fkValues)
  9110. {
  9111. $this->pkValues = $pkValues;
  9112. $this->fkValues = $fkValues;
  9113. }
  9114. }
  9115. }
  9116. // file: src/Tqdev/PhpCrudApi/Record/OrderingInfo.php
  9117. namespace Tqdev\PhpCrudApi\Record {
  9118. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
  9119. class OrderingInfo
  9120. {
  9121. public function getColumnOrdering(ReflectedTable $table, array $params): array
  9122. {
  9123. $fields = array();
  9124. if (isset($params['order'])) {
  9125. foreach ($params['order'] as $order) {
  9126. $parts = explode(',', $order, 3);
  9127. $columnName = $parts[0];
  9128. if (!$table->hasColumn($columnName)) {
  9129. continue;
  9130. }
  9131. $ascending = 'ASC';
  9132. if (count($parts) > 1) {
  9133. if (substr(strtoupper($parts[1]), 0, 4) == "DESC") {
  9134. $ascending = 'DESC';
  9135. }
  9136. }
  9137. $fields[] = [$columnName, $ascending];
  9138. }
  9139. }
  9140. if (count($fields) == 0) {
  9141. return $this->getDefaultColumnOrdering($table);
  9142. }
  9143. return $fields;
  9144. }
  9145. public function getDefaultColumnOrdering(ReflectedTable $table): array
  9146. {
  9147. $fields = array();
  9148. $pk = $table->getPk();
  9149. if ($pk) {
  9150. $fields[] = [$pk->getName(), 'ASC'];
  9151. } else {
  9152. foreach ($table->getColumnNames() as $columnName) {
  9153. $fields[] = [$columnName, 'ASC'];
  9154. }
  9155. }
  9156. return $fields;
  9157. }
  9158. }
  9159. }
  9160. // file: src/Tqdev/PhpCrudApi/Record/PaginationInfo.php
  9161. namespace Tqdev\PhpCrudApi\Record {
  9162. class PaginationInfo
  9163. {
  9164. public $DEFAULT_PAGE_SIZE = 20;
  9165. public function hasPage(array $params): bool
  9166. {
  9167. return isset($params['page']);
  9168. }
  9169. public function getPageOffset(array $params): int
  9170. {
  9171. $offset = 0;
  9172. $pageSize = $this->getPageSize($params);
  9173. if (isset($params['page'])) {
  9174. foreach ($params['page'] as $page) {
  9175. $parts = explode(',', $page, 2);
  9176. $page = intval($parts[0]) - 1;
  9177. $offset = $page * $pageSize;
  9178. }
  9179. }
  9180. return $offset;
  9181. }
  9182. private function getPageSize(array $params): int
  9183. {
  9184. $pageSize = $this->DEFAULT_PAGE_SIZE;
  9185. if (isset($params['page'])) {
  9186. foreach ($params['page'] as $page) {
  9187. $parts = explode(',', $page, 2);
  9188. if (count($parts) > 1) {
  9189. $pageSize = intval($parts[1]);
  9190. }
  9191. }
  9192. }
  9193. return $pageSize;
  9194. }
  9195. public function getResultSize(array $params): int
  9196. {
  9197. $numberOfRows = -1;
  9198. if (isset($params['size'])) {
  9199. foreach ($params['size'] as $size) {
  9200. $numberOfRows = intval($size);
  9201. }
  9202. }
  9203. return $numberOfRows;
  9204. }
  9205. public function getPageLimit(array $params): int
  9206. {
  9207. $pageLimit = -1;
  9208. if ($this->hasPage($params)) {
  9209. $pageLimit = $this->getPageSize($params);
  9210. }
  9211. $resultSize = $this->getResultSize($params);
  9212. if ($resultSize >= 0) {
  9213. if ($pageLimit >= 0) {
  9214. $pageLimit = min($pageLimit, $resultSize);
  9215. } else {
  9216. $pageLimit = $resultSize;
  9217. }
  9218. }
  9219. return $pageLimit;
  9220. }
  9221. }
  9222. }
  9223. // file: src/Tqdev/PhpCrudApi/Record/PathTree.php
  9224. namespace Tqdev\PhpCrudApi\Record {
  9225. class PathTree implements \JsonSerializable
  9226. {
  9227. const WILDCARD = '*';
  9228. private $tree;
  9229. public function __construct(/* object */&$tree = null)
  9230. {
  9231. if (!$tree) {
  9232. $tree = $this->newTree();
  9233. }
  9234. $this->tree = &$tree;
  9235. }
  9236. public function newTree()
  9237. {
  9238. return (object) ['values' => [], 'branches' => (object) []];
  9239. }
  9240. public function getKeys(): array
  9241. {
  9242. $branches = (array) $this->tree->branches;
  9243. return array_keys($branches);
  9244. }
  9245. public function getValues(): array
  9246. {
  9247. return $this->tree->values;
  9248. }
  9249. public function get(string $key): PathTree
  9250. {
  9251. if (!isset($this->tree->branches->$key)) {
  9252. return null;
  9253. }
  9254. return new PathTree($this->tree->branches->$key);
  9255. }
  9256. public function put(array $path, $value)
  9257. {
  9258. $tree = &$this->tree;
  9259. foreach ($path as $key) {
  9260. if (!isset($tree->branches->$key)) {
  9261. $tree->branches->$key = $this->newTree();
  9262. }
  9263. $tree = &$tree->branches->$key;
  9264. }
  9265. $tree->values[] = $value;
  9266. }
  9267. public function match(array $path): array
  9268. {
  9269. $star = self::WILDCARD;
  9270. $tree = &$this->tree;
  9271. foreach ($path as $key) {
  9272. if (isset($tree->branches->$key)) {
  9273. $tree = &$tree->branches->$key;
  9274. } elseif (isset($tree->branches->$star)) {
  9275. $tree = &$tree->branches->$star;
  9276. } else {
  9277. return [];
  9278. }
  9279. }
  9280. return $tree->values;
  9281. }
  9282. public static function fromJson(/* object */$tree): PathTree
  9283. {
  9284. return new PathTree($tree);
  9285. }
  9286. public function jsonSerialize()
  9287. {
  9288. return $this->tree;
  9289. }
  9290. }
  9291. }
  9292. // file: src/Tqdev/PhpCrudApi/Record/RecordService.php
  9293. namespace Tqdev\PhpCrudApi\Record {
  9294. use Tqdev\PhpCrudApi\Column\ReflectionService;
  9295. use Tqdev\PhpCrudApi\Database\GenericDB;
  9296. use Tqdev\PhpCrudApi\Record\Document\ListDocument;
  9297. class RecordService
  9298. {
  9299. private $db;
  9300. private $reflection;
  9301. private $columns;
  9302. private $joiner;
  9303. private $filters;
  9304. private $ordering;
  9305. private $pagination;
  9306. public function __construct(GenericDB $db, ReflectionService $reflection)
  9307. {
  9308. $this->db = $db;
  9309. $this->reflection = $reflection;
  9310. $this->columns = new ColumnIncluder();
  9311. $this->joiner = new RelationJoiner($reflection, $this->columns);
  9312. $this->filters = new FilterInfo();
  9313. $this->ordering = new OrderingInfo();
  9314. $this->pagination = new PaginationInfo();
  9315. }
  9316. private function sanitizeRecord(string $tableName, /* object */ $record, string $id)
  9317. {
  9318. $keyset = array_keys((array) $record);
  9319. foreach ($keyset as $key) {
  9320. if (!$this->reflection->getTable($tableName)->hasColumn($key)) {
  9321. unset($record->$key);
  9322. }
  9323. }
  9324. if ($id != '') {
  9325. $pk = $this->reflection->getTable($tableName)->getPk();
  9326. foreach ($this->reflection->getTable($tableName)->getColumnNames() as $key) {
  9327. $field = $this->reflection->getTable($tableName)->getColumn($key);
  9328. if ($field->getName() == $pk->getName()) {
  9329. unset($record->$key);
  9330. }
  9331. }
  9332. }
  9333. }
  9334. public function hasTable(string $table): bool
  9335. {
  9336. return $this->reflection->hasTable($table);
  9337. }
  9338. public function getType(string $table): string
  9339. {
  9340. return $this->reflection->getType($table);
  9341. }
  9342. public function beginTransaction() /*: void*/
  9343. {
  9344. $this->db->beginTransaction();
  9345. }
  9346. public function commitTransaction() /*: void*/
  9347. {
  9348. $this->db->commitTransaction();
  9349. }
  9350. public function rollBackTransaction() /*: void*/
  9351. {
  9352. $this->db->rollBackTransaction();
  9353. }
  9354. public function create(string $tableName, /* object */ $record, array $params) /*: ?int*/
  9355. {
  9356. $this->sanitizeRecord($tableName, $record, '');
  9357. $table = $this->reflection->getTable($tableName);
  9358. $columnValues = $this->columns->getValues($table, true, $record, $params);
  9359. return $this->db->createSingle($table, $columnValues);
  9360. }
  9361. public function read(string $tableName, string $id, array $params) /*: ?object*/
  9362. {
  9363. $table = $this->reflection->getTable($tableName);
  9364. $this->joiner->addMandatoryColumns($table, $params);
  9365. $columnNames = $this->columns->getNames($table, true, $params);
  9366. $record = $this->db->selectSingle($table, $columnNames, $id);
  9367. if ($record == null) {
  9368. return null;
  9369. }
  9370. $records = array($record);
  9371. $this->joiner->addJoins($table, $records, $params, $this->db);
  9372. return $records[0];
  9373. }
  9374. public function update(string $tableName, string $id, /* object */ $record, array $params) /*: ?int*/
  9375. {
  9376. $this->sanitizeRecord($tableName, $record, $id);
  9377. $table = $this->reflection->getTable($tableName);
  9378. $columnValues = $this->columns->getValues($table, true, $record, $params);
  9379. return $this->db->updateSingle($table, $columnValues, $id);
  9380. }
  9381. public function delete(string $tableName, string $id, array $params) /*: ?int*/
  9382. {
  9383. $table = $this->reflection->getTable($tableName);
  9384. return $this->db->deleteSingle($table, $id);
  9385. }
  9386. public function increment(string $tableName, string $id, /* object */ $record, array $params) /*: ?int*/
  9387. {
  9388. $this->sanitizeRecord($tableName, $record, $id);
  9389. $table = $this->reflection->getTable($tableName);
  9390. $columnValues = $this->columns->getValues($table, true, $record, $params);
  9391. return $this->db->incrementSingle($table, $columnValues, $id);
  9392. }
  9393. public function _list(string $tableName, array $params): ListDocument
  9394. {
  9395. $table = $this->reflection->getTable($tableName);
  9396. $this->joiner->addMandatoryColumns($table, $params);
  9397. $columnNames = $this->columns->getNames($table, true, $params);
  9398. $condition = $this->filters->getCombinedConditions($table, $params);
  9399. $columnOrdering = $this->ordering->getColumnOrdering($table, $params);
  9400. if (!$this->pagination->hasPage($params)) {
  9401. $offset = 0;
  9402. $limit = $this->pagination->getPageLimit($params);
  9403. $count = 0;
  9404. } else {
  9405. $offset = $this->pagination->getPageOffset($params);
  9406. $limit = $this->pagination->getPageLimit($params);
  9407. $count = $this->db->selectCount($table, $condition);
  9408. }
  9409. $records = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, $offset, $limit);
  9410. $this->joiner->addJoins($table, $records, $params, $this->db);
  9411. return new ListDocument($records, $count);
  9412. }
  9413. }
  9414. }
  9415. // file: src/Tqdev/PhpCrudApi/Record/RelationJoiner.php
  9416. namespace Tqdev\PhpCrudApi\Record {
  9417. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
  9418. use Tqdev\PhpCrudApi\Column\ReflectionService;
  9419. use Tqdev\PhpCrudApi\Database\GenericDB;
  9420. use Tqdev\PhpCrudApi\Middleware\Communication\VariableStore;
  9421. use Tqdev\PhpCrudApi\Record\Condition\ColumnCondition;
  9422. use Tqdev\PhpCrudApi\Record\Condition\OrCondition;
  9423. class RelationJoiner
  9424. {
  9425. private $reflection;
  9426. private $ordering;
  9427. private $columns;
  9428. public function __construct(ReflectionService $reflection, ColumnIncluder $columns)
  9429. {
  9430. $this->reflection = $reflection;
  9431. $this->ordering = new OrderingInfo();
  9432. $this->columns = $columns;
  9433. }
  9434. public function addMandatoryColumns(ReflectedTable $table, array &$params) /*: void*/
  9435. {
  9436. if (!isset($params['join']) || !isset($params['include'])) {
  9437. return;
  9438. }
  9439. $params['mandatory'] = array();
  9440. foreach ($params['join'] as $tableNames) {
  9441. $t1 = $table;
  9442. foreach (explode(',', $tableNames) as $tableName) {
  9443. if (!$this->reflection->hasTable($tableName)) {
  9444. continue;
  9445. }
  9446. $t2 = $this->reflection->getTable($tableName);
  9447. $fks1 = $t1->getFksTo($t2->getName());
  9448. $t3 = $this->hasAndBelongsToMany($t1, $t2);
  9449. if ($t3 != null || count($fks1) > 0) {
  9450. $params['mandatory'][] = $t2->getName() . '.' . $t2->getPk()->getName();
  9451. }
  9452. foreach ($fks1 as $fk) {
  9453. $params['mandatory'][] = $t1->getName() . '.' . $fk->getName();
  9454. }
  9455. $fks2 = $t2->getFksTo($t1->getName());
  9456. if ($t3 != null || count($fks2) > 0) {
  9457. $params['mandatory'][] = $t1->getName() . '.' . $t1->getPk()->getName();
  9458. }
  9459. foreach ($fks2 as $fk) {
  9460. $params['mandatory'][] = $t2->getName() . '.' . $fk->getName();
  9461. }
  9462. $t1 = $t2;
  9463. }
  9464. }
  9465. }
  9466. private function getJoinsAsPathTree(array $params): PathTree
  9467. {
  9468. $joins = new PathTree();
  9469. if (isset($params['join'])) {
  9470. foreach ($params['join'] as $tableNames) {
  9471. $path = array();
  9472. foreach (explode(',', $tableNames) as $tableName) {
  9473. if (!$this->reflection->hasTable($tableName)) {
  9474. continue;
  9475. }
  9476. $t = $this->reflection->getTable($tableName);
  9477. if ($t != null) {
  9478. $path[] = $t->getName();
  9479. }
  9480. }
  9481. $joins->put($path, true);
  9482. }
  9483. }
  9484. return $joins;
  9485. }
  9486. public function addJoins(ReflectedTable $table, array &$records, array $params, GenericDB $db) /*: void*/
  9487. {
  9488. $joins = $this->getJoinsAsPathTree($params);
  9489. $this->addJoinsForTables($table, $joins, $records, $params, $db);
  9490. }
  9491. private function hasAndBelongsToMany(ReflectedTable $t1, ReflectedTable $t2) /*: ?ReflectedTable*/
  9492. {
  9493. foreach ($this->reflection->getTableNames() as $tableName) {
  9494. $t3 = $this->reflection->getTable($tableName);
  9495. if (count($t3->getFksTo($t1->getName())) > 0 && count($t3->getFksTo($t2->getName())) > 0) {
  9496. return $t3;
  9497. }
  9498. }
  9499. return null;
  9500. }
  9501. private function addJoinsForTables(ReflectedTable $t1, PathTree $joins, array &$records, array $params, GenericDB $db)
  9502. {
  9503. foreach ($joins->getKeys() as $t2Name) {
  9504. $t2 = $this->reflection->getTable($t2Name);
  9505. $belongsTo = count($t1->getFksTo($t2->getName())) > 0;
  9506. $hasMany = count($t2->getFksTo($t1->getName())) > 0;
  9507. if (!$belongsTo && !$hasMany) {
  9508. $t3 = $this->hasAndBelongsToMany($t1, $t2);
  9509. } else {
  9510. $t3 = null;
  9511. }
  9512. $hasAndBelongsToMany = ($t3 != null);
  9513. $newRecords = array();
  9514. $fkValues = null;
  9515. $pkValues = null;
  9516. $habtmValues = null;
  9517. if ($belongsTo) {
  9518. $fkValues = $this->getFkEmptyValues($t1, $t2, $records);
  9519. $this->addFkRecords($t2, $fkValues, $params, $db, $newRecords);
  9520. }
  9521. if ($hasMany) {
  9522. $pkValues = $this->getPkEmptyValues($t1, $records);
  9523. $this->addPkRecords($t1, $t2, $pkValues, $params, $db, $newRecords);
  9524. }
  9525. if ($hasAndBelongsToMany) {
  9526. $habtmValues = $this->getHabtmEmptyValues($t1, $t2, $t3, $db, $records);
  9527. $this->addFkRecords($t2, $habtmValues->fkValues, $params, $db, $newRecords);
  9528. }
  9529. $this->addJoinsForTables($t2, $joins->get($t2Name), $newRecords, $params, $db);
  9530. if ($fkValues != null) {
  9531. $this->fillFkValues($t2, $newRecords, $fkValues);
  9532. $this->setFkValues($t1, $t2, $records, $fkValues);
  9533. }
  9534. if ($pkValues != null) {
  9535. $this->fillPkValues($t1, $t2, $newRecords, $pkValues);
  9536. $this->setPkValues($t1, $t2, $records, $pkValues);
  9537. }
  9538. if ($habtmValues != null) {
  9539. $this->fillFkValues($t2, $newRecords, $habtmValues->fkValues);
  9540. $this->setHabtmValues($t1, $t2, $records, $habtmValues);
  9541. }
  9542. }
  9543. }
  9544. private function getFkEmptyValues(ReflectedTable $t1, ReflectedTable $t2, array $records): array
  9545. {
  9546. $fkValues = array();
  9547. $fks = $t1->getFksTo($t2->getName());
  9548. foreach ($fks as $fk) {
  9549. $fkName = $fk->getName();
  9550. foreach ($records as $record) {
  9551. if (isset($record[$fkName])) {
  9552. $fkValue = $record[$fkName];
  9553. $fkValues[$fkValue] = null;
  9554. }
  9555. }
  9556. }
  9557. return $fkValues;
  9558. }
  9559. private function addFkRecords(ReflectedTable $t2, array $fkValues, array $params, GenericDB $db, array &$records) /*: void*/
  9560. {
  9561. $columnNames = $this->columns->getNames($t2, false, $params);
  9562. $fkIds = array_keys($fkValues);
  9563. foreach ($db->selectMultiple($t2, $columnNames, $fkIds) as $record) {
  9564. $records[] = $record;
  9565. }
  9566. }
  9567. private function fillFkValues(ReflectedTable $t2, array $fkRecords, array &$fkValues) /*: void*/
  9568. {
  9569. $pkName = $t2->getPk()->getName();
  9570. foreach ($fkRecords as $fkRecord) {
  9571. $pkValue = $fkRecord[$pkName];
  9572. $fkValues[$pkValue] = $fkRecord;
  9573. }
  9574. }
  9575. private function setFkValues(ReflectedTable $t1, ReflectedTable $t2, array &$records, array $fkValues) /*: void*/
  9576. {
  9577. $fks = $t1->getFksTo($t2->getName());
  9578. foreach ($fks as $fk) {
  9579. $fkName = $fk->getName();
  9580. foreach ($records as $i => $record) {
  9581. if (isset($record[$fkName])) {
  9582. $key = $record[$fkName];
  9583. $records[$i][$fkName] = $fkValues[$key];
  9584. }
  9585. }
  9586. }
  9587. }
  9588. private function getPkEmptyValues(ReflectedTable $t1, array $records): array
  9589. {
  9590. $pkValues = array();
  9591. $pkName = $t1->getPk()->getName();
  9592. foreach ($records as $record) {
  9593. $key = $record[$pkName];
  9594. $pkValues[$key] = array();
  9595. }
  9596. return $pkValues;
  9597. }
  9598. private function addPkRecords(ReflectedTable $t1, ReflectedTable $t2, array $pkValues, array $params, GenericDB $db, array &$records) /*: void*/
  9599. {
  9600. $fks = $t2->getFksTo($t1->getName());
  9601. $columnNames = $this->columns->getNames($t2, false, $params);
  9602. $pkValueKeys = implode(',', array_keys($pkValues));
  9603. $conditions = array();
  9604. foreach ($fks as $fk) {
  9605. $conditions[] = new ColumnCondition($fk, 'in', $pkValueKeys);
  9606. }
  9607. $condition = OrCondition::fromArray($conditions);
  9608. $columnOrdering = array();
  9609. $limit = VariableStore::get("joinLimits.maxRecords") ?: -1;
  9610. if ($limit != -1) {
  9611. $columnOrdering = $this->ordering->getDefaultColumnOrdering($t2);
  9612. }
  9613. foreach ($db->selectAll($t2, $columnNames, $condition, $columnOrdering, 0, $limit) as $record) {
  9614. $records[] = $record;
  9615. }
  9616. }
  9617. private function fillPkValues(ReflectedTable $t1, ReflectedTable $t2, array $pkRecords, array &$pkValues) /*: void*/
  9618. {
  9619. $fks = $t2->getFksTo($t1->getName());
  9620. foreach ($fks as $fk) {
  9621. $fkName = $fk->getName();
  9622. foreach ($pkRecords as $pkRecord) {
  9623. $key = $pkRecord[$fkName];
  9624. if (isset($pkValues[$key])) {
  9625. $pkValues[$key][] = $pkRecord;
  9626. }
  9627. }
  9628. }
  9629. }
  9630. private function setPkValues(ReflectedTable $t1, ReflectedTable $t2, array &$records, array $pkValues) /*: void*/
  9631. {
  9632. $pkName = $t1->getPk()->getName();
  9633. $t2Name = $t2->getName();
  9634. foreach ($records as $i => $record) {
  9635. $key = $record[$pkName];
  9636. $records[$i][$t2Name] = $pkValues[$key];
  9637. }
  9638. }
  9639. private function getHabtmEmptyValues(ReflectedTable $t1, ReflectedTable $t2, ReflectedTable $t3, GenericDB $db, array $records): HabtmValues
  9640. {
  9641. $pkValues = $this->getPkEmptyValues($t1, $records);
  9642. $fkValues = array();
  9643. $fk1 = $t3->getFksTo($t1->getName())[0];
  9644. $fk2 = $t3->getFksTo($t2->getName())[0];
  9645. $fk1Name = $fk1->getName();
  9646. $fk2Name = $fk2->getName();
  9647. $columnNames = array($fk1Name, $fk2Name);
  9648. $pkIds = implode(',', array_keys($pkValues));
  9649. $condition = new ColumnCondition($t3->getColumn($fk1Name), 'in', $pkIds);
  9650. $columnOrdering = array();
  9651. $limit = VariableStore::get("joinLimits.maxRecords") ?: -1;
  9652. if ($limit != -1) {
  9653. $columnOrdering = $this->ordering->getDefaultColumnOrdering($t3);
  9654. }
  9655. $records = $db->selectAll($t3, $columnNames, $condition, $columnOrdering, 0, $limit);
  9656. foreach ($records as $record) {
  9657. $val1 = $record[$fk1Name];
  9658. $val2 = $record[$fk2Name];
  9659. $pkValues[$val1][] = $val2;
  9660. $fkValues[$val2] = null;
  9661. }
  9662. return new HabtmValues($pkValues, $fkValues);
  9663. }
  9664. private function setHabtmValues(ReflectedTable $t1, ReflectedTable $t2, array &$records, HabtmValues $habtmValues) /*: void*/
  9665. {
  9666. $pkName = $t1->getPk()->getName();
  9667. $t2Name = $t2->getName();
  9668. foreach ($records as $i => $record) {
  9669. $key = $record[$pkName];
  9670. $val = array();
  9671. $fks = $habtmValues->pkValues[$key];
  9672. foreach ($fks as $fk) {
  9673. $val[] = $habtmValues->fkValues[$fk];
  9674. }
  9675. $records[$i][$t2Name] = $val;
  9676. }
  9677. }
  9678. }
  9679. }
  9680. // file: src/Tqdev/PhpCrudApi/Api.php
  9681. namespace Tqdev\PhpCrudApi {
  9682. use Psr\Http\Message\ResponseInterface;
  9683. use Psr\Http\Message\ServerRequestInterface;
  9684. use Psr\Http\Server\RequestHandlerInterface;
  9685. use Tqdev\PhpCrudApi\Cache\CacheFactory;
  9686. use Tqdev\PhpCrudApi\Column\DefinitionService;
  9687. use Tqdev\PhpCrudApi\Column\ReflectionService;
  9688. use Tqdev\PhpCrudApi\Controller\CacheController;
  9689. use Tqdev\PhpCrudApi\Controller\ColumnController;
  9690. use Tqdev\PhpCrudApi\Controller\GeoJsonController;
  9691. use Tqdev\PhpCrudApi\Controller\JsonResponder;
  9692. use Tqdev\PhpCrudApi\Controller\OpenApiController;
  9693. use Tqdev\PhpCrudApi\Controller\RecordController;
  9694. use Tqdev\PhpCrudApi\Database\GenericDB;
  9695. use Tqdev\PhpCrudApi\GeoJson\GeoJsonService;
  9696. use Tqdev\PhpCrudApi\Middleware\AuthorizationMiddleware;
  9697. use Tqdev\PhpCrudApi\Middleware\BasicAuthMiddleware;
  9698. use Tqdev\PhpCrudApi\Middleware\CorsMiddleware;
  9699. use Tqdev\PhpCrudApi\Middleware\CustomizationMiddleware;
  9700. use Tqdev\PhpCrudApi\Middleware\DbAuthMiddleware;
  9701. use Tqdev\PhpCrudApi\Middleware\FirewallMiddleware;
  9702. use Tqdev\PhpCrudApi\Middleware\IpAddressMiddleware;
  9703. use Tqdev\PhpCrudApi\Middleware\JoinLimitsMiddleware;
  9704. use Tqdev\PhpCrudApi\Middleware\JwtAuthMiddleware;
  9705. use Tqdev\PhpCrudApi\Middleware\MultiTenancyMiddleware;
  9706. use Tqdev\PhpCrudApi\Middleware\PageLimitsMiddleware;
  9707. use Tqdev\PhpCrudApi\Middleware\ReconnectMiddleware;
  9708. use Tqdev\PhpCrudApi\Middleware\Router\SimpleRouter;
  9709. use Tqdev\PhpCrudApi\Middleware\SanitationMiddleware;
  9710. use Tqdev\PhpCrudApi\Middleware\SslRedirectMiddleware;
  9711. use Tqdev\PhpCrudApi\Middleware\ValidationMiddleware;
  9712. use Tqdev\PhpCrudApi\Middleware\XmlMiddleware;
  9713. use Tqdev\PhpCrudApi\Middleware\XsrfMiddleware;
  9714. use Tqdev\PhpCrudApi\OpenApi\OpenApiService;
  9715. use Tqdev\PhpCrudApi\Record\ErrorCode;
  9716. use Tqdev\PhpCrudApi\Record\RecordService;
  9717. use Tqdev\PhpCrudApi\ResponseUtils;
  9718. class Api implements RequestHandlerInterface
  9719. {
  9720. private $router;
  9721. private $responder;
  9722. private $debug;
  9723. public function __construct(Config $config)
  9724. {
  9725. $db = new GenericDB(
  9726. $config->getDriver(),
  9727. $config->getAddress(),
  9728. $config->getPort(),
  9729. $config->getDatabase(),
  9730. $config->getTables(),
  9731. $config->getUsername(),
  9732. $config->getPassword()
  9733. );
  9734. $prefix = sprintf('phpcrudapi-%s-', substr(md5(__FILE__), 0, 8));
  9735. $cache = CacheFactory::create($config->getCacheType(), $prefix, $config->getCachePath());
  9736. $reflection = new ReflectionService($db, $cache, $config->getCacheTime());
  9737. $responder = new JsonResponder($config->getDebug());
  9738. $router = new SimpleRouter($config->getBasePath(), $responder, $cache, $config->getCacheTime());
  9739. foreach ($config->getMiddlewares() as $middleware => $properties) {
  9740. switch ($middleware) {
  9741. case 'sslRedirect':
  9742. new SslRedirectMiddleware($router, $responder, $properties);
  9743. break;
  9744. case 'cors':
  9745. new CorsMiddleware($router, $responder, $properties, $config->getDebug());
  9746. break;
  9747. case 'firewall':
  9748. new FirewallMiddleware($router, $responder, $properties);
  9749. break;
  9750. case 'basicAuth':
  9751. new BasicAuthMiddleware($router, $responder, $properties);
  9752. break;
  9753. case 'jwtAuth':
  9754. new JwtAuthMiddleware($router, $responder, $properties);
  9755. break;
  9756. case 'dbAuth':
  9757. new DbAuthMiddleware($router, $responder, $properties, $reflection, $db);
  9758. break;
  9759. case 'reconnect':
  9760. new ReconnectMiddleware($router, $responder, $properties, $reflection, $db);
  9761. break;
  9762. case 'validation':
  9763. new ValidationMiddleware($router, $responder, $properties, $reflection);
  9764. break;
  9765. case 'ipAddress':
  9766. new IpAddressMiddleware($router, $responder, $properties, $reflection);
  9767. break;
  9768. case 'sanitation':
  9769. new SanitationMiddleware($router, $responder, $properties, $reflection);
  9770. break;
  9771. case 'multiTenancy':
  9772. new MultiTenancyMiddleware($router, $responder, $properties, $reflection);
  9773. break;
  9774. case 'authorization':
  9775. new AuthorizationMiddleware($router, $responder, $properties, $reflection);
  9776. break;
  9777. case 'xsrf':
  9778. new XsrfMiddleware($router, $responder, $properties);
  9779. break;
  9780. case 'pageLimits':
  9781. new PageLimitsMiddleware($router, $responder, $properties, $reflection);
  9782. break;
  9783. case 'joinLimits':
  9784. new JoinLimitsMiddleware($router, $responder, $properties, $reflection);
  9785. break;
  9786. case 'customization':
  9787. new CustomizationMiddleware($router, $responder, $properties, $reflection);
  9788. break;
  9789. case 'xml':
  9790. new XmlMiddleware($router, $responder, $properties, $reflection);
  9791. break;
  9792. }
  9793. }
  9794. foreach ($config->getControllers() as $controller) {
  9795. switch ($controller) {
  9796. case 'records':
  9797. $records = new RecordService($db, $reflection);
  9798. new RecordController($router, $responder, $records);
  9799. break;
  9800. case 'columns':
  9801. $definition = new DefinitionService($db, $reflection);
  9802. new ColumnController($router, $responder, $reflection, $definition);
  9803. break;
  9804. case 'cache':
  9805. new CacheController($router, $responder, $cache);
  9806. break;
  9807. case 'openapi':
  9808. $openApi = new OpenApiService($reflection, $config->getOpenApiBase(), $config->getControllers(), $config->getCustomOpenApiBuilders());
  9809. new OpenApiController($router, $responder, $openApi);
  9810. break;
  9811. case 'geojson':
  9812. $records = new RecordService($db, $reflection);
  9813. $geoJson = new GeoJsonService($reflection, $records);
  9814. new GeoJsonController($router, $responder, $geoJson);
  9815. break;
  9816. }
  9817. }
  9818. foreach ($config->getCustomControllers() as $className) {
  9819. if (class_exists($className)) {
  9820. $records = new RecordService($db, $reflection);
  9821. new $className($router, $responder, $records);
  9822. }
  9823. }
  9824. $this->router = $router;
  9825. $this->responder = $responder;
  9826. $this->debug = $config->getDebug();
  9827. }
  9828. private function parseBody(string $body) /*: ?object*/
  9829. {
  9830. $first = substr($body, 0, 1);
  9831. if ($first == '[' || $first == '{') {
  9832. $object = json_decode($body);
  9833. $causeCode = json_last_error();
  9834. if ($causeCode !== JSON_ERROR_NONE) {
  9835. $object = null;
  9836. }
  9837. } else {
  9838. parse_str($body, $input);
  9839. foreach ($input as $key => $value) {
  9840. if (substr($key, -9) == '__is_null') {
  9841. $input[substr($key, 0, -9)] = null;
  9842. unset($input[$key]);
  9843. }
  9844. }
  9845. $object = (object) $input;
  9846. }
  9847. return $object;
  9848. }
  9849. private function addParsedBody(ServerRequestInterface $request): ServerRequestInterface
  9850. {
  9851. $parsedBody = $request->getParsedBody();
  9852. if ($parsedBody) {
  9853. $request = $this->applyParsedBodyHack($request);
  9854. } else {
  9855. $body = $request->getBody();
  9856. if ($body->isReadable()) {
  9857. if ($body->isSeekable()) {
  9858. $body->rewind();
  9859. }
  9860. $contents = $body->getContents();
  9861. if ($body->isSeekable()) {
  9862. $body->rewind();
  9863. }
  9864. if ($contents) {
  9865. $parsedBody = $this->parseBody($contents);
  9866. $request = $request->withParsedBody($parsedBody);
  9867. }
  9868. }
  9869. }
  9870. return $request;
  9871. }
  9872. private function applyParsedBodyHack(ServerRequestInterface $request): ServerRequestInterface
  9873. {
  9874. $parsedBody = $request->getParsedBody();
  9875. if (is_array($parsedBody)) { // is it really?
  9876. $contents = json_encode($parsedBody);
  9877. $parsedBody = $this->parseBody($contents);
  9878. $request = $request->withParsedBody($parsedBody);
  9879. }
  9880. return $request;
  9881. }
  9882. public function handle(ServerRequestInterface $request): ResponseInterface
  9883. {
  9884. return $this->router->route($this->addParsedBody($request));
  9885. }
  9886. }
  9887. }
  9888. // file: src/Tqdev/PhpCrudApi/Config.php
  9889. namespace Tqdev\PhpCrudApi {
  9890. class Config
  9891. {
  9892. private $values = [
  9893. 'driver' => null,
  9894. 'address' => 'localhost',
  9895. 'port' => null,
  9896. 'username' => null,
  9897. 'password' => null,
  9898. 'database' => null,
  9899. 'tables' => '',
  9900. 'middlewares' => 'cors,errors',
  9901. 'controllers' => 'records,geojson,openapi',
  9902. 'customControllers' => '',
  9903. 'customOpenApiBuilders' => '',
  9904. 'cacheType' => 'TempFile',
  9905. 'cachePath' => '',
  9906. 'cacheTime' => 10,
  9907. 'debug' => false,
  9908. 'basePath' => '',
  9909. 'openApiBase' => '{"info":{"title":"PHP-CRUD-API","version":"1.0.0"}}',
  9910. ];
  9911. private function getDefaultDriver(array $values): string
  9912. {
  9913. if (isset($values['driver'])) {
  9914. return $values['driver'];
  9915. }
  9916. return 'mysql';
  9917. }
  9918. private function getDefaultPort(string $driver): int
  9919. {
  9920. switch ($driver) {
  9921. case 'mysql':
  9922. return 3306;
  9923. case 'pgsql':
  9924. return 5432;
  9925. case 'sqlsrv':
  9926. return 1433;
  9927. case 'sqlite':
  9928. return 0;
  9929. }
  9930. }
  9931. private function getDefaultAddress(string $driver): string
  9932. {
  9933. switch ($driver) {
  9934. case 'mysql':
  9935. return 'localhost';
  9936. case 'pgsql':
  9937. return 'localhost';
  9938. case 'sqlsrv':
  9939. return 'localhost';
  9940. case 'sqlite':
  9941. return 'data.db';
  9942. }
  9943. }
  9944. private function getDriverDefaults(string $driver): array
  9945. {
  9946. return [
  9947. 'driver' => $driver,
  9948. 'address' => $this->getDefaultAddress($driver),
  9949. 'port' => $this->getDefaultPort($driver),
  9950. ];
  9951. }
  9952. private function applyEnvironmentVariables(array $values): array
  9953. {
  9954. $newValues = array();
  9955. foreach ($values as $key => $value) {
  9956. $environmentKey = 'PHP_CRUD_API_' . strtoupper(preg_replace('/(?<!^)[A-Z]/', '_$0', str_replace('.', '_', $key)));
  9957. $newValues[$key] = getenv($environmentKey, true) ?: $value;
  9958. }
  9959. return $newValues;
  9960. }
  9961. public function __construct(array $values)
  9962. {
  9963. $driver = $this->getDefaultDriver($values);
  9964. $defaults = $this->getDriverDefaults($driver);
  9965. $newValues = array_merge($this->values, $defaults, $values);
  9966. $newValues = $this->parseMiddlewares($newValues);
  9967. $diff = array_diff_key($newValues, $this->values);
  9968. if (!empty($diff)) {
  9969. $key = array_keys($diff)[0];
  9970. throw new \Exception("Config has invalid value '$key'");
  9971. }
  9972. $newValues = $this->applyEnvironmentVariables($newValues);
  9973. $this->values = $newValues;
  9974. }
  9975. private function parseMiddlewares(array $values): array
  9976. {
  9977. $newValues = array();
  9978. $properties = array();
  9979. $middlewares = array_map('trim', explode(',', $values['middlewares']));
  9980. foreach ($middlewares as $middleware) {
  9981. $properties[$middleware] = [];
  9982. }
  9983. foreach ($values as $key => $value) {
  9984. if (strpos($key, '.') === false) {
  9985. $newValues[$key] = $value;
  9986. } else {
  9987. list($middleware, $key2) = explode('.', $key, 2);
  9988. if (isset($properties[$middleware])) {
  9989. $properties[$middleware][$key2] = $value;
  9990. } else {
  9991. throw new \Exception("Config has invalid value '$key'");
  9992. }
  9993. }
  9994. }
  9995. $newValues['middlewares'] = array_reverse($properties, true);
  9996. return $newValues;
  9997. }
  9998. public function getDriver(): string
  9999. {
  10000. return $this->values['driver'];
  10001. }
  10002. public function getAddress(): string
  10003. {
  10004. return $this->values['address'];
  10005. }
  10006. public function getPort(): int
  10007. {
  10008. return $this->values['port'];
  10009. }
  10010. public function getUsername(): string
  10011. {
  10012. return $this->values['username'];
  10013. }
  10014. public function getPassword(): string
  10015. {
  10016. return $this->values['password'];
  10017. }
  10018. public function getDatabase(): string
  10019. {
  10020. return $this->values['database'];
  10021. }
  10022. public function getTables(): array
  10023. {
  10024. return array_filter(array_map('trim', explode(',', $this->values['tables'])));
  10025. }
  10026. public function getMiddlewares(): array
  10027. {
  10028. return $this->values['middlewares'];
  10029. }
  10030. public function getControllers(): array
  10031. {
  10032. return array_filter(array_map('trim', explode(',', $this->values['controllers'])));
  10033. }
  10034. public function getCustomControllers(): array
  10035. {
  10036. return array_filter(array_map('trim', explode(',', $this->values['customControllers'])));
  10037. }
  10038. public function getCustomOpenApiBuilders(): array
  10039. {
  10040. return array_filter(array_map('trim', explode(',', $this->values['customOpenApiBuilders'])));
  10041. }
  10042. public function getCacheType(): string
  10043. {
  10044. return $this->values['cacheType'];
  10045. }
  10046. public function getCachePath(): string
  10047. {
  10048. return $this->values['cachePath'];
  10049. }
  10050. public function getCacheTime(): int
  10051. {
  10052. return $this->values['cacheTime'];
  10053. }
  10054. public function getDebug(): bool
  10055. {
  10056. return $this->values['debug'];
  10057. }
  10058. public function getBasePath(): string
  10059. {
  10060. return $this->values['basePath'];
  10061. }
  10062. public function getOpenApiBase(): array
  10063. {
  10064. return json_decode($this->values['openApiBase'], true);
  10065. }
  10066. }
  10067. }
  10068. // file: src/Tqdev/PhpCrudApi/RequestFactory.php
  10069. namespace Tqdev\PhpCrudApi {
  10070. use Nyholm\Psr7\Factory\Psr17Factory;
  10071. use Nyholm\Psr7Server\ServerRequestCreator;
  10072. use Psr\Http\Message\ServerRequestInterface;
  10073. class RequestFactory
  10074. {
  10075. public static function fromGlobals(): ServerRequestInterface
  10076. {
  10077. $psr17Factory = new Psr17Factory();
  10078. $creator = new ServerRequestCreator($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
  10079. $serverRequest = $creator->fromGlobals();
  10080. $stream = $psr17Factory->createStreamFromFile('php://input');
  10081. $serverRequest = $serverRequest->withBody($stream);
  10082. return $serverRequest;
  10083. }
  10084. public static function fromString(string $request): ServerRequestInterface
  10085. {
  10086. $parts = explode("\n\n", trim($request), 2);
  10087. $lines = explode("\n", $parts[0]);
  10088. $first = explode(' ', trim(array_shift($lines)), 2);
  10089. $method = $first[0];
  10090. $body = isset($parts[1]) ? $parts[1] : '';
  10091. $url = isset($first[1]) ? $first[1] : '';
  10092. $psr17Factory = new Psr17Factory();
  10093. $serverRequest = $psr17Factory->createServerRequest($method, $url);
  10094. foreach ($lines as $line) {
  10095. list($key, $value) = explode(':', $line, 2);
  10096. $serverRequest = $serverRequest->withAddedHeader($key, $value);
  10097. }
  10098. if ($body) {
  10099. $stream = $psr17Factory->createStream($body);
  10100. $stream->rewind();
  10101. $serverRequest = $serverRequest->withBody($stream);
  10102. }
  10103. return $serverRequest;
  10104. }
  10105. }
  10106. }
  10107. // file: src/Tqdev/PhpCrudApi/RequestUtils.php
  10108. namespace Tqdev\PhpCrudApi {
  10109. use Psr\Http\Message\ServerRequestInterface;
  10110. use Tqdev\PhpCrudApi\Column\ReflectionService;
  10111. class RequestUtils
  10112. {
  10113. public static function setParams(ServerRequestInterface $request, array $params): ServerRequestInterface
  10114. {
  10115. $query = preg_replace('|%5B[0-9]+%5D=|', '=', http_build_query($params));
  10116. return $request->withUri($request->getUri()->withQuery($query));
  10117. }
  10118. public static function getHeader(ServerRequestInterface $request, string $header): string
  10119. {
  10120. $headers = $request->getHeader($header);
  10121. return isset($headers[0]) ? $headers[0] : '';
  10122. }
  10123. public static function getParams(ServerRequestInterface $request): array
  10124. {
  10125. $params = array();
  10126. $query = $request->getUri()->getQuery();
  10127. //$query = str_replace('][]=', ']=', str_replace('=', '[]=', $query));
  10128. $query = str_replace('%5D%5B%5D=', '%5D=', str_replace('=', '%5B%5D=', $query));
  10129. parse_str($query, $params);
  10130. return $params;
  10131. }
  10132. public static function getPathSegment(ServerRequestInterface $request, int $part): string
  10133. {
  10134. $path = $request->getUri()->getPath();
  10135. $pathSegments = explode('/', rtrim($path, '/'));
  10136. if ($part < 0 || $part >= count($pathSegments)) {
  10137. return '';
  10138. }
  10139. return urldecode($pathSegments[$part]);
  10140. }
  10141. public static function getOperation(ServerRequestInterface $request): string
  10142. {
  10143. $method = $request->getMethod();
  10144. $path = RequestUtils::getPathSegment($request, 1);
  10145. $hasPk = RequestUtils::getPathSegment($request, 3) != '';
  10146. switch ($path) {
  10147. case 'openapi':
  10148. return 'document';
  10149. case 'columns':
  10150. return $method == 'get' ? 'reflect' : 'remodel';
  10151. case 'geojson':
  10152. case 'records':
  10153. switch ($method) {
  10154. case 'POST':
  10155. return 'create';
  10156. case 'GET':
  10157. return $hasPk ? 'read' : 'list';
  10158. case 'PUT':
  10159. return 'update';
  10160. case 'DELETE':
  10161. return 'delete';
  10162. case 'PATCH':
  10163. return 'increment';
  10164. }
  10165. }
  10166. return 'unknown';
  10167. }
  10168. private static function getJoinTables(string $tableName, array $parameters): array
  10169. {
  10170. $uniqueTableNames = array();
  10171. $uniqueTableNames[$tableName] = true;
  10172. if (isset($parameters['join'])) {
  10173. foreach ($parameters['join'] as $parameter) {
  10174. $tableNames = explode(',', trim($parameter));
  10175. foreach ($tableNames as $tableName) {
  10176. $uniqueTableNames[$tableName] = true;
  10177. }
  10178. }
  10179. }
  10180. return array_keys($uniqueTableNames);
  10181. }
  10182. public static function getTableNames(ServerRequestInterface $request, ReflectionService $reflection): array
  10183. {
  10184. $path = RequestUtils::getPathSegment($request, 1);
  10185. $tableName = RequestUtils::getPathSegment($request, 2);
  10186. $allTableNames = $reflection->getTableNames();
  10187. switch ($path) {
  10188. case 'openapi':
  10189. return $allTableNames;
  10190. case 'columns':
  10191. return $tableName ? [$tableName] : $allTableNames;
  10192. case 'records':
  10193. return self::getJoinTables($tableName, RequestUtils::getParams($request));
  10194. }
  10195. return $allTableNames;
  10196. }
  10197. }
  10198. }
  10199. // file: src/Tqdev/PhpCrudApi/ResponseFactory.php
  10200. namespace Tqdev\PhpCrudApi {
  10201. use Nyholm\Psr7\Factory\Psr17Factory;
  10202. use Psr\Http\Message\ResponseInterface;
  10203. class ResponseFactory
  10204. {
  10205. const OK = 200;
  10206. const MOVED_PERMANENTLY = 301;
  10207. const FOUND = 302;
  10208. const UNAUTHORIZED = 401;
  10209. const FORBIDDEN = 403;
  10210. const NOT_FOUND = 404;
  10211. const METHOD_NOT_ALLOWED = 405;
  10212. const CONFLICT = 409;
  10213. const UNPROCESSABLE_ENTITY = 422;
  10214. const FAILED_DEPENDENCY = 424;
  10215. const INTERNAL_SERVER_ERROR = 500;
  10216. public static function fromXml(int $status, string $xml): ResponseInterface
  10217. {
  10218. return self::from($status, 'text/xml', $xml);
  10219. }
  10220. public static function fromCsv(int $status, string $csv): ResponseInterface
  10221. {
  10222. return self::from($status, 'text/csv', $csv);
  10223. }
  10224. public static function fromHtml(int $status, string $html): ResponseInterface
  10225. {
  10226. return self::from($status, 'text/html', $html);
  10227. }
  10228. public static function fromObject(int $status, $body): ResponseInterface
  10229. {
  10230. $content = json_encode($body, JSON_UNESCAPED_UNICODE);
  10231. return self::from($status, 'application/json', $content);
  10232. }
  10233. public static function from(int $status, string $contentType, string $content): ResponseInterface
  10234. {
  10235. $psr17Factory = new Psr17Factory();
  10236. $response = $psr17Factory->createResponse($status);
  10237. $stream = $psr17Factory->createStream($content);
  10238. $stream->rewind();
  10239. $response = $response->withBody($stream);
  10240. $response = $response->withHeader('Content-Type', $contentType . '; charset=utf-8');
  10241. $response = $response->withHeader('Content-Length', strlen($content));
  10242. return $response;
  10243. }
  10244. public static function fromStatus(int $status): ResponseInterface
  10245. {
  10246. $psr17Factory = new Psr17Factory();
  10247. return $psr17Factory->createResponse($status);
  10248. }
  10249. }
  10250. }
  10251. // file: src/Tqdev/PhpCrudApi/ResponseUtils.php
  10252. namespace Tqdev\PhpCrudApi {
  10253. use Psr\Http\Message\ResponseInterface;
  10254. class ResponseUtils
  10255. {
  10256. public static function output(ResponseInterface $response)
  10257. {
  10258. $status = $response->getStatusCode();
  10259. $headers = $response->getHeaders();
  10260. $body = $response->getBody()->getContents();
  10261. http_response_code($status);
  10262. foreach ($headers as $key => $values) {
  10263. foreach ($values as $value) {
  10264. header("$key: $value");
  10265. }
  10266. }
  10267. echo $body;
  10268. }
  10269. public static function addExceptionHeaders(ResponseInterface $response, \Throwable $e): ResponseInterface
  10270. {
  10271. $response = $response->withHeader('X-Exception-Name', get_class($e));
  10272. $response = $response->withHeader('X-Exception-Message', preg_replace('|\n|', ' ', trim($e->getMessage())));
  10273. $response = $response->withHeader('X-Exception-File', $e->getFile() . ':' . $e->getLine());
  10274. return $response;
  10275. }
  10276. public static function toString(ResponseInterface $response): string
  10277. {
  10278. $status = $response->getStatusCode();
  10279. $headers = $response->getHeaders();
  10280. $body = $response->getBody()->getContents();
  10281. $str = "$status\n";
  10282. foreach ($headers as $key => $values) {
  10283. foreach ($values as $value) {
  10284. $str .= "$key: $value\n";
  10285. }
  10286. }
  10287. if ($body !== '') {
  10288. $str .= "\n";
  10289. $str .= "$body\n";
  10290. }
  10291. return $str;
  10292. }
  10293. }
  10294. }