Results.php 201 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Hold the PhpMyAdmin\Display\Results class
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin\Display;
  9. use PhpMyAdmin\Core;
  10. use PhpMyAdmin\DatabaseInterface;
  11. use PhpMyAdmin\Index;
  12. use PhpMyAdmin\Message;
  13. use PhpMyAdmin\Plugins\Transformations\Text_Plain_Link;
  14. use PhpMyAdmin\Relation;
  15. use PhpMyAdmin\Response;
  16. use PhpMyAdmin\Sanitize;
  17. use PhpMyAdmin\Sql;
  18. use PhpMyAdmin\SqlParser\Utils\Query;
  19. use PhpMyAdmin\Table;
  20. use PhpMyAdmin\Template;
  21. use PhpMyAdmin\Transformations;
  22. use PhpMyAdmin\Url;
  23. use PhpMyAdmin\Util;
  24. /**
  25. * Handle all the functionalities related to displaying results
  26. * of sql queries, stored procedure, browsing sql processes or
  27. * displaying binary log.
  28. *
  29. * @package PhpMyAdmin
  30. */
  31. class Results
  32. {
  33. // Define constants
  34. const NO_EDIT_OR_DELETE = 'nn';
  35. const UPDATE_ROW = 'ur';
  36. const DELETE_ROW = 'dr';
  37. const KILL_PROCESS = 'kp';
  38. const POSITION_LEFT = 'left';
  39. const POSITION_RIGHT = 'right';
  40. const POSITION_BOTH = 'both';
  41. const POSITION_NONE = 'none';
  42. const PLACE_TOP_DIRECTION_DROPDOWN = 'top_direction_dropdown';
  43. const PLACE_BOTTOM_DIRECTION_DROPDOWN = 'bottom_direction_dropdown';
  44. const DISPLAY_FULL_TEXT = 'F';
  45. const DISPLAY_PARTIAL_TEXT = 'P';
  46. const HEADER_FLIP_TYPE_AUTO = 'auto';
  47. const HEADER_FLIP_TYPE_CSS = 'css';
  48. const HEADER_FLIP_TYPE_FAKE = 'fake';
  49. const DATE_FIELD = 'date';
  50. const DATETIME_FIELD = 'datetime';
  51. const TIMESTAMP_FIELD = 'timestamp';
  52. const TIME_FIELD = 'time';
  53. const STRING_FIELD = 'string';
  54. const GEOMETRY_FIELD = 'geometry';
  55. const BLOB_FIELD = 'BLOB';
  56. const BINARY_FIELD = 'BINARY';
  57. const RELATIONAL_KEY = 'K';
  58. const RELATIONAL_DISPLAY_COLUMN = 'D';
  59. const GEOMETRY_DISP_GEOM = 'GEOM';
  60. const GEOMETRY_DISP_WKT = 'WKT';
  61. const GEOMETRY_DISP_WKB = 'WKB';
  62. const SMART_SORT_ORDER = 'SMART';
  63. const ASCENDING_SORT_DIR = 'ASC';
  64. const DESCENDING_SORT_DIR = 'DESC';
  65. const TABLE_TYPE_INNO_DB = 'InnoDB';
  66. const ALL_ROWS = 'all';
  67. const QUERY_TYPE_SELECT = 'SELECT';
  68. const ROUTINE_PROCEDURE = 'procedure';
  69. const ROUTINE_FUNCTION = 'function';
  70. const ACTION_LINK_CONTENT_ICONS = 'icons';
  71. const ACTION_LINK_CONTENT_TEXT = 'text';
  72. // Declare global fields
  73. /** array with properties of the class */
  74. private $_property_array = array(
  75. /** string Database name */
  76. 'db' => null,
  77. /** string Table name */
  78. 'table' => null,
  79. /** string the URL to go back in case of errors */
  80. 'goto' => null,
  81. /** string the SQL query */
  82. 'sql_query' => null,
  83. /**
  84. * integer the total number of rows returned by the SQL query without any
  85. * appended "LIMIT" clause programmatically
  86. */
  87. 'unlim_num_rows' => null,
  88. /** array meta information about fields */
  89. 'fields_meta' => null,
  90. /** boolean */
  91. 'is_count' => null,
  92. /** integer */
  93. 'is_export' => null,
  94. /** boolean */
  95. 'is_func' => null,
  96. /** integer */
  97. 'is_analyse' => null,
  98. /** integer the total number of rows returned by the SQL query */
  99. 'num_rows' => null,
  100. /** integer the total number of fields returned by the SQL query */
  101. 'fields_cnt' => null,
  102. /** double time taken for execute the SQL query */
  103. 'querytime' => null,
  104. /** string path for theme images directory */
  105. 'pma_theme_image' => null,
  106. /** string */
  107. 'text_dir' => null,
  108. /** boolean */
  109. 'is_maint' => null,
  110. /** boolean */
  111. 'is_explain' => null,
  112. /** boolean */
  113. 'is_show' => null,
  114. /** boolean */
  115. 'is_browse_distinct' => null,
  116. /** array table definitions */
  117. 'showtable' => null,
  118. /** string */
  119. 'printview' => null,
  120. /** string URL query */
  121. 'url_query' => null,
  122. /** array column names to highlight */
  123. 'highlight_columns' => null,
  124. /** array holding various display information */
  125. 'display_params' => null,
  126. /** array mime types information of fields */
  127. 'mime_map' => null,
  128. /** boolean */
  129. 'editable' => null,
  130. /** random unique ID to distinguish result set */
  131. 'unique_id' => null,
  132. /** where clauses for each row, each table in the row */
  133. 'whereClauseMap' => array(),
  134. );
  135. /**
  136. * This variable contains the column transformation information
  137. * for some of the system databases.
  138. * One element of this array represent all relevant columns in all tables in
  139. * one specific database
  140. */
  141. public $transformation_info;
  142. /**
  143. * @var Relation $relation
  144. */
  145. private $relation;
  146. /**
  147. * Get any property of this class
  148. *
  149. * @param string $property name of the property
  150. *
  151. * @return mixed|void if property exist, value of the relevant property
  152. */
  153. public function __get($property)
  154. {
  155. if (array_key_exists($property, $this->_property_array)) {
  156. return $this->_property_array[$property];
  157. }
  158. }
  159. /**
  160. * Set values for any property of this class
  161. *
  162. * @param string $property name of the property
  163. * @param mixed $value value to set
  164. *
  165. * @return void
  166. */
  167. public function __set($property, $value)
  168. {
  169. if (array_key_exists($property, $this->_property_array)) {
  170. $this->_property_array[$property] = $value;
  171. }
  172. }
  173. /**
  174. * Constructor for PhpMyAdmin\Display\Results class
  175. *
  176. * @param string $db the database name
  177. * @param string $table the table name
  178. * @param string $goto the URL to go back in case of errors
  179. * @param string $sql_query the SQL query
  180. *
  181. * @access public
  182. */
  183. public function __construct($db, $table, $goto, $sql_query)
  184. {
  185. $this->relation = new Relation();
  186. $this->_setDefaultTransformations();
  187. $this->__set('db', $db);
  188. $this->__set('table', $table);
  189. $this->__set('goto', $goto);
  190. $this->__set('sql_query', $sql_query);
  191. $this->__set('unique_id', rand());
  192. }
  193. /**
  194. * Sets default transformations for some columns
  195. *
  196. * @return void
  197. */
  198. private function _setDefaultTransformations()
  199. {
  200. $json_highlighting_data = array(
  201. 'libraries/classes/Plugins/Transformations/Output/Text_Plain_Json.php',
  202. 'PhpMyAdmin\Plugins\Transformations\Output\Text_Plain_Json',
  203. 'Text_Plain'
  204. );
  205. $sql_highlighting_data = array(
  206. 'libraries/classes/Plugins/Transformations/Output/Text_Plain_Sql.php',
  207. 'PhpMyAdmin\Plugins\Transformations\Output\Text_Plain_Sql',
  208. 'Text_Plain'
  209. );
  210. $blob_sql_highlighting_data = array(
  211. 'libraries/classes/Plugins/Transformations/Output/Text_Octetstream_Sql.php',
  212. 'PhpMyAdmin\Plugins\Transformations\Output\Text_Octetstream_Sql',
  213. 'Text_Octetstream'
  214. );
  215. $link_data = array(
  216. 'libraries/classes/Plugins/Transformations/Text_Plain_Link.php',
  217. 'PhpMyAdmin\Plugins\Transformations\Text_Plain_Link',
  218. 'Text_Plain'
  219. );
  220. $this->transformation_info = array(
  221. 'information_schema' => array(
  222. 'events' => array(
  223. 'event_definition' => $sql_highlighting_data
  224. ),
  225. 'processlist' => array(
  226. 'info' => $sql_highlighting_data
  227. ),
  228. 'routines' => array(
  229. 'routine_definition' => $sql_highlighting_data
  230. ),
  231. 'triggers' => array(
  232. 'action_statement' => $sql_highlighting_data
  233. ),
  234. 'views' => array(
  235. 'view_definition' => $sql_highlighting_data
  236. )
  237. ),
  238. 'mysql' => array(
  239. 'event' => array(
  240. 'body' => $blob_sql_highlighting_data,
  241. 'body_utf8' => $blob_sql_highlighting_data
  242. ),
  243. 'general_log' => array(
  244. 'argument' => $sql_highlighting_data
  245. ),
  246. 'help_category' => array(
  247. 'url' => $link_data
  248. ),
  249. 'help_topic' => array(
  250. 'example' => $sql_highlighting_data,
  251. 'url' => $link_data
  252. ),
  253. 'proc' => array(
  254. 'param_list' => $blob_sql_highlighting_data,
  255. 'returns' => $blob_sql_highlighting_data,
  256. 'body' => $blob_sql_highlighting_data,
  257. 'body_utf8' => $blob_sql_highlighting_data
  258. ),
  259. 'slow_log' => array(
  260. 'sql_text' => $sql_highlighting_data
  261. )
  262. )
  263. );
  264. $cfgRelation = $this->relation->getRelationsParam();
  265. if ($cfgRelation['db']) {
  266. $this->transformation_info[$cfgRelation['db']] = array();
  267. $relDb = &$this->transformation_info[$cfgRelation['db']];
  268. if (! empty($cfgRelation['history'])) {
  269. $relDb[$cfgRelation['history']] = array(
  270. 'sqlquery' => $sql_highlighting_data
  271. );
  272. }
  273. if (! empty($cfgRelation['bookmark'])) {
  274. $relDb[$cfgRelation['bookmark']] = array(
  275. 'query' => $sql_highlighting_data
  276. );
  277. }
  278. if (! empty($cfgRelation['tracking'])) {
  279. $relDb[$cfgRelation['tracking']] = array(
  280. 'schema_sql' => $sql_highlighting_data,
  281. 'data_sql' => $sql_highlighting_data
  282. );
  283. }
  284. if (! empty($cfgRelation['favorite'])) {
  285. $relDb[$cfgRelation['favorite']] = array(
  286. 'tables' => $json_highlighting_data
  287. );
  288. }
  289. if (! empty($cfgRelation['recent'])) {
  290. $relDb[$cfgRelation['recent']] = array(
  291. 'tables' => $json_highlighting_data
  292. );
  293. }
  294. if (! empty($cfgRelation['savedsearches'])) {
  295. $relDb[$cfgRelation['savedsearches']] = array(
  296. 'search_data' => $json_highlighting_data
  297. );
  298. }
  299. if (! empty($cfgRelation['designer_settings'])) {
  300. $relDb[$cfgRelation['designer_settings']] = array(
  301. 'settings_data' => $json_highlighting_data
  302. );
  303. }
  304. if (! empty($cfgRelation['table_uiprefs'])) {
  305. $relDb[$cfgRelation['table_uiprefs']] = array(
  306. 'prefs' => $json_highlighting_data
  307. );
  308. }
  309. if (! empty($cfgRelation['userconfig'])) {
  310. $relDb[$cfgRelation['userconfig']] = array(
  311. 'config_data' => $json_highlighting_data
  312. );
  313. }
  314. if (! empty($cfgRelation['export_templates'])) {
  315. $relDb[$cfgRelation['export_templates']] = array(
  316. 'template_data' => $json_highlighting_data
  317. );
  318. }
  319. }
  320. }
  321. /**
  322. * Set properties which were not initialized at the constructor
  323. *
  324. * @param integer $unlim_num_rows the total number of rows returned by
  325. * the SQL query without any appended
  326. * "LIMIT" clause programmatically
  327. * @param array $fields_meta meta information about fields
  328. * @param boolean $is_count statement is SELECT COUNT
  329. * @param integer $is_export statement contains INTO OUTFILE
  330. * @param boolean $is_func statement contains a function like SUM()
  331. * @param integer $is_analyse statement contains PROCEDURE ANALYSE
  332. * @param integer $num_rows total no. of rows returned by SQL query
  333. * @param integer $fields_cnt total no.of fields returned by SQL query
  334. * @param double $querytime time taken for execute the SQL query
  335. * @param string $pmaThemeImage path for theme images directory
  336. * @param string $text_dir text direction
  337. * @param boolean $is_maint statement contains a maintenance command
  338. * @param boolean $is_explain statement contains EXPLAIN
  339. * @param boolean $is_show statement contains SHOW
  340. * @param array $showtable table definitions
  341. * @param string $printview print view was requested
  342. * @param string $url_query URL query
  343. * @param boolean $editable whether the results set is editable
  344. * @param boolean $is_browse_dist whether browsing distinct values
  345. *
  346. * @return void
  347. *
  348. * @see sql.php
  349. */
  350. public function setProperties(
  351. $unlim_num_rows, $fields_meta, $is_count, $is_export, $is_func,
  352. $is_analyse, $num_rows, $fields_cnt, $querytime, $pmaThemeImage, $text_dir,
  353. $is_maint, $is_explain, $is_show, $showtable, $printview, $url_query,
  354. $editable, $is_browse_dist
  355. ) {
  356. $this->__set('unlim_num_rows', $unlim_num_rows);
  357. $this->__set('fields_meta', $fields_meta);
  358. $this->__set('is_count', $is_count);
  359. $this->__set('is_export', $is_export);
  360. $this->__set('is_func', $is_func);
  361. $this->__set('is_analyse', $is_analyse);
  362. $this->__set('num_rows', $num_rows);
  363. $this->__set('fields_cnt', $fields_cnt);
  364. $this->__set('querytime', $querytime);
  365. $this->__set('pma_theme_image', $pmaThemeImage);
  366. $this->__set('text_dir', $text_dir);
  367. $this->__set('is_maint', $is_maint);
  368. $this->__set('is_explain', $is_explain);
  369. $this->__set('is_show', $is_show);
  370. $this->__set('showtable', $showtable);
  371. $this->__set('printview', $printview);
  372. $this->__set('url_query', $url_query);
  373. $this->__set('editable', $editable);
  374. $this->__set('is_browse_distinct', $is_browse_dist);
  375. } // end of the 'setProperties()' function
  376. /**
  377. * Defines the parts to display for a print view
  378. *
  379. * @param array $displayParts the parts to display
  380. *
  381. * @return array $displayParts the modified display parts
  382. *
  383. * @access private
  384. *
  385. */
  386. private function _setDisplayPartsForPrintView(array $displayParts)
  387. {
  388. // set all elements to false!
  389. $displayParts['edit_lnk'] = self::NO_EDIT_OR_DELETE; // no edit link
  390. $displayParts['del_lnk'] = self::NO_EDIT_OR_DELETE; // no delete link
  391. $displayParts['sort_lnk'] = (string) '0';
  392. $displayParts['nav_bar'] = (string) '0';
  393. $displayParts['bkm_form'] = (string) '0';
  394. $displayParts['text_btn'] = (string) '0';
  395. $displayParts['pview_lnk'] = (string) '0';
  396. return $displayParts;
  397. }
  398. /**
  399. * Defines the parts to display for a SHOW statement
  400. *
  401. * @param array $displayParts the parts to display
  402. *
  403. * @return array $displayParts the modified display parts
  404. *
  405. * @access private
  406. *
  407. */
  408. private function _setDisplayPartsForShow(array $displayParts)
  409. {
  410. preg_match(
  411. '@^SHOW[[:space:]]+(VARIABLES|(FULL[[:space:]]+)?'
  412. . 'PROCESSLIST|STATUS|TABLE|GRANTS|CREATE|LOGS|DATABASES|FIELDS'
  413. . ')@i',
  414. $this->__get('sql_query'), $which
  415. );
  416. $bIsProcessList = isset($which[1]);
  417. if ($bIsProcessList) {
  418. $str = ' ' . strtoupper($which[1]);
  419. $bIsProcessList = $bIsProcessList
  420. && strpos($str, 'PROCESSLIST') > 0;
  421. }
  422. if ($bIsProcessList) {
  423. // no edit link
  424. $displayParts['edit_lnk'] = self::NO_EDIT_OR_DELETE;
  425. // "kill process" type edit link
  426. $displayParts['del_lnk'] = self::KILL_PROCESS;
  427. } else {
  428. // Default case -> no links
  429. // no edit link
  430. $displayParts['edit_lnk'] = self::NO_EDIT_OR_DELETE;
  431. // no delete link
  432. $displayParts['del_lnk'] = self::NO_EDIT_OR_DELETE;
  433. }
  434. // Other settings
  435. $displayParts['sort_lnk'] = (string) '0';
  436. $displayParts['nav_bar'] = (string) '0';
  437. $displayParts['bkm_form'] = (string) '1';
  438. $displayParts['text_btn'] = (string) '1';
  439. $displayParts['pview_lnk'] = (string) '1';
  440. return $displayParts;
  441. }
  442. /**
  443. * Defines the parts to display for statements not related to data
  444. *
  445. * @param array $displayParts the parts to display
  446. *
  447. * @return array $displayParts the modified display parts
  448. *
  449. * @access private
  450. *
  451. */
  452. private function _setDisplayPartsForNonData(array $displayParts)
  453. {
  454. // Statement is a "SELECT COUNT", a
  455. // "CHECK/ANALYZE/REPAIR/OPTIMIZE/CHECKSUM", an "EXPLAIN" one or
  456. // contains a "PROC ANALYSE" part
  457. $displayParts['edit_lnk'] = self::NO_EDIT_OR_DELETE; // no edit link
  458. $displayParts['del_lnk'] = self::NO_EDIT_OR_DELETE; // no delete link
  459. $displayParts['sort_lnk'] = (string) '0';
  460. $displayParts['nav_bar'] = (string) '0';
  461. $displayParts['bkm_form'] = (string) '1';
  462. if ($this->__get('is_maint')) {
  463. $displayParts['text_btn'] = (string) '1';
  464. } else {
  465. $displayParts['text_btn'] = (string) '0';
  466. }
  467. $displayParts['pview_lnk'] = (string) '1';
  468. return $displayParts;
  469. }
  470. /**
  471. * Defines the parts to display for other statements (probably SELECT)
  472. *
  473. * @param array $displayParts the parts to display
  474. *
  475. * @return array $displayParts the modified display parts
  476. *
  477. * @access private
  478. *
  479. */
  480. private function _setDisplayPartsForSelect(array $displayParts)
  481. {
  482. // Other statements (ie "SELECT" ones) -> updates
  483. // $displayParts['edit_lnk'], $displayParts['del_lnk'] and
  484. // $displayParts['text_btn'] (keeps other default values)
  485. $fields_meta = $this->__get('fields_meta');
  486. $prev_table = '';
  487. $displayParts['text_btn'] = (string) '1';
  488. $number_of_columns = $this->__get('fields_cnt');
  489. for ($i = 0; $i < $number_of_columns; $i++) {
  490. $is_link = ($displayParts['edit_lnk'] != self::NO_EDIT_OR_DELETE)
  491. || ($displayParts['del_lnk'] != self::NO_EDIT_OR_DELETE)
  492. || ($displayParts['sort_lnk'] != '0');
  493. // Displays edit/delete/sort/insert links?
  494. if ($is_link
  495. && $prev_table != ''
  496. && $fields_meta[$i]->table != ''
  497. && $fields_meta[$i]->table != $prev_table
  498. ) {
  499. // don't display links
  500. $displayParts['edit_lnk'] = self::NO_EDIT_OR_DELETE;
  501. $displayParts['del_lnk'] = self::NO_EDIT_OR_DELETE;
  502. /**
  503. * @todo May be problematic with same field names
  504. * in two joined table.
  505. */
  506. // $displayParts['sort_lnk'] = (string) '0';
  507. if ($displayParts['text_btn'] == '1') {
  508. break;
  509. }
  510. } // end if
  511. // Always display print view link
  512. $displayParts['pview_lnk'] = (string) '1';
  513. if ($fields_meta[$i]->table != '') {
  514. $prev_table = $fields_meta[$i]->table;
  515. }
  516. } // end for
  517. if ($prev_table == '') { // no table for any of the columns
  518. // don't display links
  519. $displayParts['edit_lnk'] = self::NO_EDIT_OR_DELETE;
  520. $displayParts['del_lnk'] = self::NO_EDIT_OR_DELETE;
  521. }
  522. return $displayParts;
  523. }
  524. /**
  525. * Defines the parts to display for the results of a SQL query
  526. * and the total number of rows
  527. *
  528. * @param array $displayParts the parts to display (see a few
  529. * lines above for explanations)
  530. *
  531. * @return array the first element is an array with explicit indexes
  532. * for all the display elements
  533. * the second element is the total number of rows returned
  534. * by the SQL query without any programmatically appended
  535. * LIMIT clause (just a copy of $unlim_num_rows if it exists,
  536. * else computed inside this function)
  537. *
  538. *
  539. * @access private
  540. *
  541. * @see getTable()
  542. */
  543. private function _setDisplayPartsAndTotal(array $displayParts)
  544. {
  545. $the_total = 0;
  546. // 1. Following variables are needed for use in isset/empty or
  547. // use with array indexes or safe use in foreach
  548. $db = $this->__get('db');
  549. $table = $this->__get('table');
  550. $unlim_num_rows = $this->__get('unlim_num_rows');
  551. $num_rows = $this->__get('num_rows');
  552. $printview = $this->__get('printview');
  553. // 2. Updates the display parts
  554. if ($printview == '1') {
  555. $displayParts = $this->_setDisplayPartsForPrintView($displayParts);
  556. } elseif ($this->__get('is_count') || $this->__get('is_analyse')
  557. || $this->__get('is_maint') || $this->__get('is_explain')
  558. ) {
  559. $displayParts = $this->_setDisplayPartsForNonData($displayParts);
  560. } elseif ($this->__get('is_show')) {
  561. $displayParts = $this->_setDisplayPartsForShow($displayParts);
  562. } else {
  563. $displayParts = $this->_setDisplayPartsForSelect($displayParts);
  564. } // end if..elseif...else
  565. // 3. Gets the total number of rows if it is unknown
  566. if (isset($unlim_num_rows) && $unlim_num_rows != '') {
  567. $the_total = $unlim_num_rows;
  568. } elseif ((($displayParts['nav_bar'] == '1')
  569. || ($displayParts['sort_lnk'] == '1'))
  570. && (strlen($db) > 0 && strlen($table) > 0)
  571. ) {
  572. $the_total = $GLOBALS['dbi']->getTable($db, $table)->countRecords();
  573. }
  574. // if for COUNT query, number of rows returned more than 1
  575. // (may be being used GROUP BY)
  576. if ($this->__get('is_count') && isset($num_rows) && $num_rows > 1) {
  577. $displayParts['nav_bar'] = (string) '1';
  578. $displayParts['sort_lnk'] = (string) '1';
  579. }
  580. // 4. If navigation bar or sorting fields names URLs should be
  581. // displayed but there is only one row, change these settings to
  582. // false
  583. if ($displayParts['nav_bar'] == '1' || $displayParts['sort_lnk'] == '1') {
  584. // - Do not display sort links if less than 2 rows.
  585. // - For a VIEW we (probably) did not count the number of rows
  586. // so don't test this number here, it would remove the possibility
  587. // of sorting VIEW results.
  588. $_table = new Table($table, $db);
  589. if (isset($unlim_num_rows)
  590. && ($unlim_num_rows < 2)
  591. && ! $_table->isView()
  592. ) {
  593. $displayParts['sort_lnk'] = (string) '0';
  594. }
  595. } // end if (3)
  596. return array($displayParts, $the_total);
  597. } // end of the 'setDisplayPartsAndTotal()' function
  598. /**
  599. * Return true if we are executing a query in the form of
  600. * "SELECT * FROM <a table> ..."
  601. *
  602. * @param array $analyzed_sql_results analyzed sql results
  603. *
  604. * @return boolean
  605. *
  606. * @access private
  607. *
  608. * @see _getTableHeaders(), _getColumnParams()
  609. */
  610. private function _isSelect(array $analyzed_sql_results)
  611. {
  612. return ! ($this->__get('is_count')
  613. || $this->__get('is_export')
  614. || $this->__get('is_func')
  615. || $this->__get('is_analyse'))
  616. && !empty($analyzed_sql_results['select_from'])
  617. && !empty($analyzed_sql_results['statement']->from)
  618. && (count($analyzed_sql_results['statement']->from) == 1)
  619. && !empty($analyzed_sql_results['statement']->from[0]->table);
  620. }
  621. /**
  622. * Get a navigation button
  623. *
  624. * @param string $caption iconic caption for button
  625. * @param string $title text for button
  626. * @param integer $pos position for next query
  627. * @param string $html_sql_query query ready for display
  628. * @param boolean $back whether 'begin' or 'previous'
  629. * @param string $onsubmit optional onsubmit clause
  630. * @param string $input_for_real_end optional hidden field for special treatment
  631. * @param string $onclick optional onclick clause
  632. *
  633. * @return string html content
  634. *
  635. * @access private
  636. *
  637. * @see _getMoveBackwardButtonsForTableNavigation(),
  638. * _getMoveForwardButtonsForTableNavigation()
  639. */
  640. private function _getTableNavigationButton(
  641. $caption,
  642. $title,
  643. $pos,
  644. $html_sql_query,
  645. $back,
  646. $onsubmit = '',
  647. $input_for_real_end = '',
  648. $onclick = ''
  649. ) {
  650. $caption_output = '';
  651. if ($back) {
  652. if (Util::showIcons('TableNavigationLinksMode')) {
  653. $caption_output .= $caption;
  654. }
  655. if (Util::showText('TableNavigationLinksMode')) {
  656. $caption_output .= '&nbsp;' . $title;
  657. }
  658. } else {
  659. if (Util::showText('TableNavigationLinksMode')) {
  660. $caption_output .= $title;
  661. }
  662. if (Util::showIcons('TableNavigationLinksMode')) {
  663. $caption_output .= '&nbsp;' . $caption;
  664. }
  665. }
  666. return Template::get('display/results/table_navigation_button')->render([
  667. 'db' => $this->__get('db'),
  668. 'table' => $this->__get('table'),
  669. 'sql_query' => $html_sql_query,
  670. 'pos' => $pos,
  671. 'is_browse_distinct' => $this->__get('is_browse_distinct'),
  672. 'goto' => $this->__get('goto'),
  673. 'input_for_real_end' => $input_for_real_end,
  674. 'caption_output' => $caption_output,
  675. 'title' => $title,
  676. 'onsubmit' => $onsubmit,
  677. 'onclick' => $onclick,
  678. ]);
  679. }
  680. /**
  681. * Possibly return a page selector for table navigation
  682. *
  683. * @param string $table_navigation_html the current navigation HTML
  684. *
  685. * @return array ($table_navigation_html, $nbTotalPage)
  686. *
  687. * @access private
  688. *
  689. */
  690. private function _getHtmlPageSelector($table_navigation_html)
  691. {
  692. $pageNow = @floor(
  693. $_SESSION['tmpval']['pos']
  694. / $_SESSION['tmpval']['max_rows']
  695. ) + 1;
  696. $nbTotalPage = @ceil(
  697. $this->__get('unlim_num_rows')
  698. / $_SESSION['tmpval']['max_rows']
  699. );
  700. if ($nbTotalPage > 1) {
  701. $table_navigation_html .= '<td>';
  702. $_url_params = array(
  703. 'db' => $this->__get('db'),
  704. 'table' => $this->__get('table'),
  705. 'sql_query' => $this->__get('sql_query'),
  706. 'goto' => $this->__get('goto'),
  707. 'is_browse_distinct' => $this->__get('is_browse_distinct'),
  708. );
  709. //<form> to keep the form alignment of button < and <<
  710. // and also to know what to execute when the selector changes
  711. $table_navigation_html .= '<form action="sql.php" method="post">';
  712. $table_navigation_html .= Url::getHiddenInputs($_url_params);
  713. $table_navigation_html .= Util::pageselector(
  714. 'pos',
  715. $_SESSION['tmpval']['max_rows'],
  716. $pageNow, $nbTotalPage, 200, 5, 5, 20, 10
  717. );
  718. $table_navigation_html .= '</form>'
  719. . '</td>';
  720. }
  721. return array($table_navigation_html, $nbTotalPage);
  722. }
  723. /**
  724. * Get a navigation bar to browse among the results of a SQL query
  725. *
  726. * @param integer $pos_next the offset for the "next" page
  727. * @param integer $pos_prev the offset for the "previous" page
  728. * @param boolean $is_innodb whether its InnoDB or not
  729. * @param string $sort_by_key_html the sort by key dialog
  730. *
  731. * @return string html content
  732. *
  733. * @access private
  734. *
  735. * @see _getTable()
  736. */
  737. private function _getTableNavigation(
  738. $pos_next, $pos_prev, $is_innodb, $sort_by_key_html
  739. ) {
  740. $table_navigation_html = '';
  741. // here, using htmlentities() would cause problems if the query
  742. // contains accented characters
  743. $html_sql_query = htmlspecialchars($this->__get('sql_query'));
  744. // Navigation bar
  745. $table_navigation_html
  746. .= '<table class="navigation nospacing nopadding print_ignore">'
  747. . '<tr>'
  748. . '<td class="navigation_separator"></td>';
  749. // Move to the beginning or to the previous page
  750. if ($_SESSION['tmpval']['pos']
  751. && ($_SESSION['tmpval']['max_rows'] != self::ALL_ROWS)
  752. ) {
  753. $table_navigation_html
  754. .= $this->_getMoveBackwardButtonsForTableNavigation(
  755. $html_sql_query, $pos_prev
  756. );
  757. } // end move back
  758. $nbTotalPage = 1;
  759. //page redirection
  760. // (unless we are showing all records)
  761. if ($_SESSION['tmpval']['max_rows'] != self::ALL_ROWS) {
  762. list(
  763. $table_navigation_html,
  764. $nbTotalPage
  765. ) = $this->_getHtmlPageSelector($table_navigation_html);
  766. }
  767. $showing_all = false;
  768. if ($_SESSION['tmpval']['max_rows'] == self::ALL_ROWS) {
  769. $showing_all = true;
  770. }
  771. // Move to the next page or to the last one
  772. if ($this->__get('unlim_num_rows') === false // view with unknown number of rows
  773. || ($_SESSION['tmpval']['max_rows'] != self::ALL_ROWS
  774. && $_SESSION['tmpval']['pos'] + $_SESSION['tmpval']['max_rows'] < $this->__get('unlim_num_rows')
  775. && $this->__get('num_rows') >= $_SESSION['tmpval']['max_rows'])
  776. ) {
  777. $table_navigation_html
  778. .= $this->_getMoveForwardButtonsForTableNavigation(
  779. $html_sql_query, $pos_next, $is_innodb
  780. );
  781. } // end move toward
  782. // show separator if pagination happen
  783. if ($nbTotalPage > 1) {
  784. $table_navigation_html
  785. .= '<td><div class="navigation_separator">|</div></td>';
  786. }
  787. // Display the "Show all" button if allowed
  788. if ($GLOBALS['cfg']['ShowAll'] || ($this->__get('unlim_num_rows') <= 500) ) {
  789. $table_navigation_html .= $this->_getShowAllCheckboxForTableNavigation(
  790. $showing_all, $html_sql_query
  791. );
  792. $table_navigation_html
  793. .= '<td><div class="navigation_separator">|</div></td>';
  794. } // end show all
  795. $table_navigation_html .= '<td>'
  796. . '<div class="save_edited hide">'
  797. . '<input type="submit" value="' . __('Save edited data') . '" />'
  798. . '<div class="navigation_separator">|</div>'
  799. . '</div>'
  800. . '</td>'
  801. . '<td>'
  802. . '<div class="restore_column hide">'
  803. . '<input type="submit" value="' . __('Restore column order') . '" />'
  804. . '<div class="navigation_separator">|</div>'
  805. . '</div>'
  806. . '</td>';
  807. // if displaying a VIEW, $unlim_num_rows could be zero because
  808. // of $cfg['MaxExactCountViews']; in this case, avoid passing
  809. // the 5th parameter to checkFormElementInRange()
  810. // (this means we can't validate the upper limit
  811. $table_navigation_html .= '<td class="navigation_goto">';
  812. $table_navigation_html .= '<form action="sql.php" method="post" '
  813. . 'onsubmit="return '
  814. . '(checkFormElementInRange('
  815. . 'this, '
  816. . '\'session_max_rows\', '
  817. . '\''
  818. . str_replace('\'', '\\\'', __('%d is not valid row number.'))
  819. . '\', '
  820. . '1)'
  821. . ' &amp;&amp; '
  822. . 'checkFormElementInRange('
  823. . 'this, '
  824. . '\'pos\', '
  825. . '\''
  826. . str_replace('\'', '\\\'', __('%d is not valid row number.'))
  827. . '\', '
  828. . '0'
  829. . (($this->__get('unlim_num_rows') > 0)
  830. ? ', ' . ($this->__get('unlim_num_rows') - 1)
  831. : ''
  832. )
  833. . ')'
  834. . ')'
  835. . '">';
  836. $table_navigation_html .= Url::getHiddenInputs(
  837. $this->__get('db'), $this->__get('table')
  838. );
  839. $table_navigation_html .= $this->_getAdditionalFieldsForTableNavigation(
  840. $html_sql_query
  841. );
  842. $table_navigation_html .= '</form>'
  843. . '</td>'
  844. . '<td class="navigation_separator"></td>'
  845. . '<td class="largescreenonly">'
  846. . '<span>' . __('Filter rows') . ':</span>'
  847. . '<input type="text" class="filter_rows"'
  848. . ' placeholder="' . __('Search this table') . '"'
  849. . ' data-for="' . $this->__get('unique_id') . '" />'
  850. . '</td>';
  851. $table_navigation_html .= '<td class="largescreenonly">' . $sort_by_key_html . '</td>';
  852. $table_navigation_html .= '<td class="navigation_separator"></td>'
  853. . '</tr>'
  854. . '</table>';
  855. return $table_navigation_html;
  856. } // end of the '_getTableNavigation()' function
  857. /**
  858. * Prepare move backward buttons - previous and first
  859. *
  860. * @param string $html_sql_query the sql encoded by html special characters
  861. * @param integer $pos_prev the offset for the "previous" page
  862. *
  863. * @return string html content
  864. *
  865. * @access private
  866. *
  867. * @see _getTableNavigation()
  868. */
  869. private function _getMoveBackwardButtonsForTableNavigation(
  870. $html_sql_query, $pos_prev
  871. ) {
  872. return $this->_getTableNavigationButton(
  873. '&lt;&lt;', _pgettext('First page', 'Begin'), 0, $html_sql_query, true
  874. )
  875. . $this->_getTableNavigationButton(
  876. '&lt;', _pgettext('Previous page', 'Previous'), $pos_prev,
  877. $html_sql_query, true
  878. );
  879. } // end of the '_getMoveBackwardButtonsForTableNavigation()' function
  880. /**
  881. * Prepare Show All checkbox for table navigation
  882. *
  883. * @param bool $showing_all whether all rows are shown currently
  884. * @param string $html_sql_query the sql encoded by html special characters
  885. *
  886. * @return string html content
  887. *
  888. * @access private
  889. *
  890. * @see _getTableNavigation()
  891. */
  892. private function _getShowAllCheckboxForTableNavigation(
  893. $showing_all, $html_sql_query
  894. ) {
  895. return Template::get('display/results/show_all_checkbox')->render([
  896. 'db' => $this->__get('db'),
  897. 'table' => $this->__get('table'),
  898. 'is_browse_distinct' => $this->__get('is_browse_distinct'),
  899. 'goto' => $this->__get('goto'),
  900. 'unique_id' => $this->__get('unique_id'),
  901. 'html_sql_query' => $html_sql_query,
  902. 'showing_all' => $showing_all,
  903. 'max_rows' => intval($GLOBALS['cfg']['MaxRows']),
  904. ]);
  905. } // end of the '_getShowAllButtonForTableNavigation()' function
  906. /**
  907. * Prepare move forward buttons - next and last
  908. *
  909. * @param string $html_sql_query the sql encoded by htmlspecialchars()
  910. * @param integer $pos_next the offset for the "next" page
  911. * @param boolean $is_innodb whether it's InnoDB or not
  912. *
  913. * @return string $buttons_html html content
  914. *
  915. * @access private
  916. *
  917. * @see _getTableNavigation()
  918. */
  919. private function _getMoveForwardButtonsForTableNavigation(
  920. $html_sql_query, $pos_next, $is_innodb
  921. ) {
  922. // display the Next button
  923. $buttons_html = $this->_getTableNavigationButton(
  924. '&gt;',
  925. _pgettext('Next page', 'Next'),
  926. $pos_next,
  927. $html_sql_query,
  928. false
  929. );
  930. // prepare some options for the End button
  931. if ($is_innodb
  932. && $this->__get('unlim_num_rows') > $GLOBALS['cfg']['MaxExactCount']
  933. ) {
  934. $input_for_real_end = '<input id="real_end_input" type="hidden" '
  935. . 'name="find_real_end" value="1" />';
  936. // no backquote around this message
  937. $onclick = '';
  938. } else {
  939. $input_for_real_end = $onclick = '';
  940. }
  941. $maxRows = $_SESSION['tmpval']['max_rows'];
  942. $onsubmit = 'onsubmit="return '
  943. . (($_SESSION['tmpval']['pos']
  944. + $maxRows
  945. < $this->__get('unlim_num_rows')
  946. && $this->__get('num_rows') >= $maxRows)
  947. ? 'true'
  948. : 'false') . '"';
  949. // display the End button
  950. $buttons_html .= $this->_getTableNavigationButton(
  951. '&gt;&gt;',
  952. _pgettext('Last page', 'End'),
  953. @((ceil(
  954. $this->__get('unlim_num_rows')
  955. / $_SESSION['tmpval']['max_rows']
  956. )- 1) * $maxRows),
  957. $html_sql_query, false, $onsubmit, $input_for_real_end, $onclick
  958. );
  959. return $buttons_html;
  960. } // end of the '_getMoveForwardButtonsForTableNavigation()' function
  961. /**
  962. * Prepare fields for table navigation
  963. * Number of rows
  964. *
  965. * @param string $sqlQuery the sql encoded by htmlspecialchars()
  966. *
  967. * @return string html content
  968. *
  969. * @access private
  970. *
  971. * @see _getTableNavigation()
  972. */
  973. private function _getAdditionalFieldsForTableNavigation($sqlQuery)
  974. {
  975. $numberOfRowsPlaceholder = null;
  976. if ($_SESSION['tmpval']['max_rows'] == self::ALL_ROWS) {
  977. $numberOfRowsPlaceholder = __('All');
  978. }
  979. $numberOfRowsChoices = array(
  980. '25' => 25,
  981. '50' => 50,
  982. '100' => 100,
  983. '250' => 250,
  984. '500' => 500
  985. );
  986. return Template::get('display/results/additional_fields')->render([
  987. 'goto' => $this->__get('goto'),
  988. 'is_browse_distinct' => $this->__get('is_browse_distinct'),
  989. 'sql_query' => $sqlQuery,
  990. 'number_of_rows_choices' => $numberOfRowsChoices,
  991. 'number_of_rows_placeholder' => $numberOfRowsPlaceholder,
  992. 'pos' => $_SESSION['tmpval']['pos'],
  993. 'max_rows' => $_SESSION['tmpval']['max_rows'],
  994. ]);
  995. }
  996. /**
  997. * Get the headers of the results table, for all of the columns
  998. *
  999. * @param array $displayParts which elements to display
  1000. * @param array $analyzed_sql_results analyzed sql results
  1001. * @param array $sort_expression sort expression
  1002. * @param array $sort_expression_nodirection sort expression
  1003. * without direction
  1004. * @param array $sort_direction sort direction
  1005. * @param boolean $is_limited_display with limited operations
  1006. * or not
  1007. * @param string $unsorted_sql_query query without the sort part
  1008. *
  1009. * @return string html content
  1010. *
  1011. * @access private
  1012. *
  1013. * @see getTableHeaders()
  1014. */
  1015. private function _getTableHeadersForColumns(
  1016. array $displayParts, array $analyzed_sql_results, array $sort_expression,
  1017. array $sort_expression_nodirection, array $sort_direction, $is_limited_display,
  1018. $unsorted_sql_query
  1019. ) {
  1020. $html = '';
  1021. // required to generate sort links that will remember whether the
  1022. // "Show all" button has been clicked
  1023. $sql_md5 = md5($this->__get('sql_query'));
  1024. $session_max_rows = $is_limited_display
  1025. ? 0
  1026. : $_SESSION['tmpval']['query'][$sql_md5]['max_rows'];
  1027. // Following variable are needed for use in isset/empty or
  1028. // use with array indexes/safe use in the for loop
  1029. $highlight_columns = $this->__get('highlight_columns');
  1030. $fields_meta = $this->__get('fields_meta');
  1031. // Prepare Display column comments if enabled
  1032. // ($GLOBALS['cfg']['ShowBrowseComments']).
  1033. $comments_map = $this->_getTableCommentsArray($analyzed_sql_results);
  1034. list($col_order, $col_visib) = $this->_getColumnParams(
  1035. $analyzed_sql_results
  1036. );
  1037. // optimize: avoid calling a method on each iteration
  1038. $number_of_columns = $this->__get('fields_cnt');
  1039. for ($j = 0; $j < $number_of_columns; $j++) {
  1040. // assign $i with the appropriate column order
  1041. $i = $col_order ? $col_order[$j] : $j;
  1042. // See if this column should get highlight because it's used in the
  1043. // where-query.
  1044. $name = $fields_meta[$i]->name;
  1045. $condition_field = (isset($highlight_columns[$name])
  1046. || isset($highlight_columns[Util::backquote($name)]))
  1047. ? true
  1048. : false;
  1049. // Prepare comment-HTML-wrappers for each row, if defined/enabled.
  1050. $comments = $this->_getCommentForRow($comments_map, $fields_meta[$i]);
  1051. $display_params = $this->__get('display_params');
  1052. if (($displayParts['sort_lnk'] == '1') && ! $is_limited_display) {
  1053. list($order_link, $sorted_header_html)
  1054. = $this->_getOrderLinkAndSortedHeaderHtml(
  1055. $fields_meta[$i], $sort_expression,
  1056. $sort_expression_nodirection, $i, $unsorted_sql_query,
  1057. $session_max_rows, $comments,
  1058. $sort_direction, $col_visib,
  1059. $col_visib[$j]
  1060. );
  1061. $html .= $sorted_header_html;
  1062. $display_params['desc'][] = ' <th '
  1063. . 'class="draggable'
  1064. . ($condition_field ? ' condition' : '')
  1065. . '" data-column="' . htmlspecialchars($fields_meta[$i]->name)
  1066. . '">' . "\n" . $order_link . $comments . ' </th>' . "\n";
  1067. } else {
  1068. // Results can't be sorted
  1069. $html
  1070. .= $this->_getDraggableClassForNonSortableColumns(
  1071. $col_visib, $col_visib[$j], $condition_field,
  1072. $fields_meta[$i], $comments
  1073. );
  1074. $display_params['desc'][] = ' <th '
  1075. . 'class="draggable'
  1076. . ($condition_field ? ' condition"' : '')
  1077. . '" data-column="' . htmlspecialchars($fields_meta[$i]->name)
  1078. . '">' . ' '
  1079. . htmlspecialchars($fields_meta[$i]->name)
  1080. . $comments . ' </th>';
  1081. } // end else
  1082. $this->__set('display_params', $display_params);
  1083. } // end for
  1084. return $html;
  1085. }
  1086. /**
  1087. * Get the headers of the results table
  1088. *
  1089. * @param array &$displayParts which elements to display
  1090. * @param array $analyzed_sql_results analyzed sql results
  1091. * @param string $unsorted_sql_query the unsorted sql query
  1092. * @param array $sort_expression sort expression
  1093. * @param array|string $sort_expression_nodirection sort expression
  1094. * without direction
  1095. * @param array $sort_direction sort direction
  1096. * @param boolean $is_limited_display with limited operations
  1097. * or not
  1098. *
  1099. * @return string html content
  1100. *
  1101. * @access private
  1102. *
  1103. * @see getTable()
  1104. */
  1105. private function _getTableHeaders(
  1106. array &$displayParts, array $analyzed_sql_results, $unsorted_sql_query,
  1107. array $sort_expression = array(), $sort_expression_nodirection = '',
  1108. array $sort_direction = array(), $is_limited_display = false
  1109. ) {
  1110. $table_headers_html = '';
  1111. // Needed for use in isset/empty or
  1112. // use with array indexes/safe use in foreach
  1113. $printview = $this->__get('printview');
  1114. $display_params = $this->__get('display_params');
  1115. // Output data needed for grid editing
  1116. $table_headers_html .= '<input class="save_cells_at_once" type="hidden"'
  1117. . ' value="' . $GLOBALS['cfg']['SaveCellsAtOnce'] . '" />'
  1118. . '<div class="common_hidden_inputs">'
  1119. . Url::getHiddenInputs(
  1120. $this->__get('db'), $this->__get('table')
  1121. )
  1122. . '</div>';
  1123. // Output data needed for column reordering and show/hide column
  1124. $table_headers_html .= $this->_getDataForResettingColumnOrder($analyzed_sql_results);
  1125. $display_params['emptypre'] = 0;
  1126. $display_params['emptyafter'] = 0;
  1127. $display_params['textbtn'] = '';
  1128. $full_or_partial_text_link = null;
  1129. $this->__set('display_params', $display_params);
  1130. // Display options (if we are not in print view)
  1131. if (! (isset($printview) && ($printview == '1')) && ! $is_limited_display) {
  1132. $table_headers_html .= $this->_getOptionsBlock();
  1133. // prepare full/partial text button or link
  1134. $full_or_partial_text_link = $this->_getFullOrPartialTextButtonOrLink();
  1135. }
  1136. // Start of form for multi-rows edit/delete/export
  1137. $table_headers_html .= $this->_getFormForMultiRowOperations(
  1138. $displayParts['del_lnk']
  1139. );
  1140. // 1. Set $colspan and generate html with full/partial
  1141. // text button or link
  1142. list($colspan, $button_html)
  1143. = $this->_getFieldVisibilityParams(
  1144. $displayParts, $full_or_partial_text_link
  1145. );
  1146. $table_headers_html .= $button_html;
  1147. // 2. Displays the fields' name
  1148. // 2.0 If sorting links should be used, checks if the query is a "JOIN"
  1149. // statement (see 2.1.3)
  1150. // See if we have to highlight any header fields of a WHERE query.
  1151. // Uses SQL-Parser results.
  1152. $this->_setHighlightedColumnGlobalField($analyzed_sql_results);
  1153. // Get the headers for all of the columns
  1154. $table_headers_html .= $this->_getTableHeadersForColumns(
  1155. $displayParts, $analyzed_sql_results, $sort_expression,
  1156. $sort_expression_nodirection, $sort_direction,
  1157. $is_limited_display, $unsorted_sql_query
  1158. );
  1159. // Display column at rightside - checkboxes or empty column
  1160. if (! $printview) {
  1161. $table_headers_html .= $this->_getColumnAtRightSide(
  1162. $displayParts, $full_or_partial_text_link, $colspan
  1163. );
  1164. }
  1165. $table_headers_html .= '</tr>' . '</thead>';
  1166. return $table_headers_html;
  1167. } // end of the '_getTableHeaders()' function
  1168. /**
  1169. * Prepare unsorted sql query and sort by key drop down
  1170. *
  1171. * @param array $analyzed_sql_results analyzed sql results
  1172. * @param array|null $sort_expression sort expression
  1173. *
  1174. * @return array two element array - $unsorted_sql_query, $drop_down_html
  1175. *
  1176. * @access private
  1177. *
  1178. * @see _getTableHeaders()
  1179. */
  1180. private function _getUnsortedSqlAndSortByKeyDropDown(
  1181. array $analyzed_sql_results, array $sort_expression
  1182. ) {
  1183. $drop_down_html = '';
  1184. $unsorted_sql_query = Query::replaceClause(
  1185. $analyzed_sql_results['statement'],
  1186. $analyzed_sql_results['parser']->list,
  1187. 'ORDER BY',
  1188. ''
  1189. );
  1190. // Data is sorted by indexes only if it there is only one table.
  1191. if ($this->_isSelect($analyzed_sql_results)) {
  1192. // grab indexes data:
  1193. $indexes = Index::getFromTable(
  1194. $this->__get('table'),
  1195. $this->__get('db')
  1196. );
  1197. // do we have any index?
  1198. if (! empty($indexes)) {
  1199. $drop_down_html = $this->_getSortByKeyDropDown(
  1200. $indexes, $sort_expression,
  1201. $unsorted_sql_query
  1202. );
  1203. }
  1204. }
  1205. return array($unsorted_sql_query, $drop_down_html);
  1206. } // end of the '_getUnsortedSqlAndSortByKeyDropDown()' function
  1207. /**
  1208. * Prepare sort by key dropdown - html code segment
  1209. *
  1210. * @param Index[] $indexes the indexes of the table for sort criteria
  1211. * @param array|null $sort_expression the sort expression
  1212. * @param string $unsorted_sql_query the unsorted sql query
  1213. *
  1214. * @return string $drop_down_html html content
  1215. *
  1216. * @access private
  1217. *
  1218. * @see _getTableHeaders()
  1219. */
  1220. private function _getSortByKeyDropDown(
  1221. $indexes, array $sort_expression, $unsorted_sql_query
  1222. ) {
  1223. $drop_down_html = '';
  1224. $drop_down_html .= '<form action="sql.php" method="post" ' .
  1225. 'class="print_ignore">' . "\n"
  1226. . Url::getHiddenInputs(
  1227. $this->__get('db'), $this->__get('table')
  1228. )
  1229. . Url::getHiddenFields(array('sort_by_key' => '1'))
  1230. . __('Sort by key')
  1231. . ': <select name="sql_query" class="autosubmit">' . "\n";
  1232. $used_index = false;
  1233. $local_order = (is_array($sort_expression) ? implode(', ',$sort_expression) : '');
  1234. foreach ($indexes as $index) {
  1235. $asc_sort = '`'
  1236. . implode('` ASC, `', array_keys($index->getColumns()))
  1237. . '` ASC';
  1238. $desc_sort = '`'
  1239. . implode('` DESC, `', array_keys($index->getColumns()))
  1240. . '` DESC';
  1241. $used_index = $used_index
  1242. || ($local_order == $asc_sort)
  1243. || ($local_order == $desc_sort);
  1244. if (preg_match(
  1245. '@(.*)([[:space:]](LIMIT (.*)|PROCEDURE (.*)|'
  1246. . 'FOR UPDATE|LOCK IN SHARE MODE))@is',
  1247. $unsorted_sql_query, $my_reg
  1248. )) {
  1249. $unsorted_sql_query_first_part = $my_reg[1];
  1250. $unsorted_sql_query_second_part = $my_reg[2];
  1251. } else {
  1252. $unsorted_sql_query_first_part = $unsorted_sql_query;
  1253. $unsorted_sql_query_second_part = '';
  1254. }
  1255. $drop_down_html .= '<option value="'
  1256. . htmlspecialchars(
  1257. $unsorted_sql_query_first_part . "\n"
  1258. . ' ORDER BY ' . $asc_sort
  1259. . $unsorted_sql_query_second_part
  1260. )
  1261. . '"' . ($local_order == $asc_sort
  1262. ? ' selected="selected"'
  1263. : '')
  1264. . '>' . htmlspecialchars($index->getName()) . ' (ASC)</option>';
  1265. $drop_down_html .= '<option value="'
  1266. . htmlspecialchars(
  1267. $unsorted_sql_query_first_part . "\n"
  1268. . ' ORDER BY ' . $desc_sort
  1269. . $unsorted_sql_query_second_part
  1270. )
  1271. . '"' . ($local_order == $desc_sort
  1272. ? ' selected="selected"'
  1273. : '')
  1274. . '>' . htmlspecialchars($index->getName()) . ' (DESC)</option>';
  1275. }
  1276. $drop_down_html .= '<option value="' . htmlspecialchars($unsorted_sql_query)
  1277. . '"' . ($used_index ? '' : ' selected="selected"') . '>' . __('None')
  1278. . '</option>'
  1279. . '</select>' . "\n"
  1280. . '</form>' . "\n";
  1281. return $drop_down_html;
  1282. } // end of the '_getSortByKeyDropDown()' function
  1283. /**
  1284. * Set column span, row span and prepare html with full/partial
  1285. * text button or link
  1286. *
  1287. * @param array &$displayParts which elements to display
  1288. * @param string $full_or_partial_text_link full/partial link or text button
  1289. *
  1290. * @return array 2 element array - $colspan, $button_html
  1291. *
  1292. * @access private
  1293. *
  1294. * @see _getTableHeaders()
  1295. */
  1296. private function _getFieldVisibilityParams(
  1297. array &$displayParts, $full_or_partial_text_link
  1298. ) {
  1299. $button_html = '';
  1300. $display_params = $this->__get('display_params');
  1301. // 1. Displays the full/partial text button (part 1)...
  1302. $button_html .= '<thead><tr>' . "\n";
  1303. $colspan = (($displayParts['edit_lnk'] != self::NO_EDIT_OR_DELETE)
  1304. && ($displayParts['del_lnk'] != self::NO_EDIT_OR_DELETE))
  1305. ? ' colspan="4"'
  1306. : '';
  1307. // ... before the result table
  1308. if ((($displayParts['edit_lnk'] == self::NO_EDIT_OR_DELETE)
  1309. && ($displayParts['del_lnk'] == self::NO_EDIT_OR_DELETE))
  1310. && ($displayParts['text_btn'] == '1')
  1311. ) {
  1312. $display_params['emptypre']
  1313. = (($displayParts['edit_lnk'] != self::NO_EDIT_OR_DELETE)
  1314. && ($displayParts['del_lnk'] != self::NO_EDIT_OR_DELETE)) ? 4 : 0;
  1315. } elseif ((($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_LEFT)
  1316. || ($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_BOTH))
  1317. && ($displayParts['text_btn'] == '1')
  1318. ) {
  1319. // ... at the left column of the result table header if possible
  1320. // and required
  1321. $display_params['emptypre']
  1322. = (($displayParts['edit_lnk'] != self::NO_EDIT_OR_DELETE)
  1323. && ($displayParts['del_lnk'] != self::NO_EDIT_OR_DELETE)) ? 4 : 0;
  1324. $button_html .= '<th class="column_action print_ignore" ' . $colspan
  1325. . '>' . $full_or_partial_text_link . '</th>';
  1326. } elseif ((($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_LEFT)
  1327. || ($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_BOTH))
  1328. && (($displayParts['edit_lnk'] != self::NO_EDIT_OR_DELETE)
  1329. || ($displayParts['del_lnk'] != self::NO_EDIT_OR_DELETE))
  1330. ) {
  1331. // ... elseif no button, displays empty(ies) col(s) if required
  1332. $display_params['emptypre']
  1333. = (($displayParts['edit_lnk'] != self::NO_EDIT_OR_DELETE)
  1334. && ($displayParts['del_lnk'] != self::NO_EDIT_OR_DELETE)) ? 4 : 0;
  1335. $button_html .= '<td ' . $colspan . '></td>';
  1336. } elseif (($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_NONE)) {
  1337. // ... elseif display an empty column if the actions links are
  1338. // disabled to match the rest of the table
  1339. $button_html .= '<th class="column_action"></th>';
  1340. }
  1341. $this->__set('display_params', $display_params);
  1342. return array($colspan, $button_html);
  1343. } // end of the '_getFieldVisibilityParams()' function
  1344. /**
  1345. * Get table comments as array
  1346. *
  1347. * @param array $analyzed_sql_results analyzed sql results
  1348. *
  1349. * @return array $comments_map table comments
  1350. *
  1351. * @access private
  1352. *
  1353. * @see _getTableHeaders()
  1354. */
  1355. private function _getTableCommentsArray(array $analyzed_sql_results)
  1356. {
  1357. if ((!$GLOBALS['cfg']['ShowBrowseComments'])
  1358. || (empty($analyzed_sql_results['statement']->from))
  1359. ) {
  1360. return array();
  1361. }
  1362. $ret = array();
  1363. foreach ($analyzed_sql_results['statement']->from as $field) {
  1364. if (empty($field->table)) {
  1365. continue;
  1366. }
  1367. $ret[$field->table] = $this->relation->getComments(
  1368. empty($field->database) ? $this->__get('db') : $field->database,
  1369. $field->table
  1370. );
  1371. }
  1372. return $ret;
  1373. } // end of the '_getTableCommentsArray()' function
  1374. /**
  1375. * Set global array for store highlighted header fields
  1376. *
  1377. * @param array $analyzed_sql_results analyzed sql results
  1378. *
  1379. * @return void
  1380. *
  1381. * @access private
  1382. *
  1383. * @see _getTableHeaders()
  1384. */
  1385. private function _setHighlightedColumnGlobalField(array $analyzed_sql_results)
  1386. {
  1387. $highlight_columns = array();
  1388. if (!empty($analyzed_sql_results['statement']->where)) {
  1389. foreach ($analyzed_sql_results['statement']->where as $expr) {
  1390. foreach ($expr->identifiers as $identifier) {
  1391. $highlight_columns[$identifier] = 'true';
  1392. }
  1393. }
  1394. }
  1395. $this->__set('highlight_columns', $highlight_columns);
  1396. } // end of the '_setHighlightedColumnGlobalField()' function
  1397. /**
  1398. * Prepare data for column restoring and show/hide
  1399. *
  1400. * @param array $analyzed_sql_results analyzed sql results
  1401. *
  1402. * @return string $data_html html content
  1403. *
  1404. * @access private
  1405. *
  1406. * @see _getTableHeaders()
  1407. */
  1408. private function _getDataForResettingColumnOrder(array $analyzed_sql_results)
  1409. {
  1410. if (! $this->_isSelect($analyzed_sql_results)) {
  1411. return '';
  1412. }
  1413. // generate the column order, if it is set
  1414. list($col_order, $col_visib) = $this->_getColumnParams(
  1415. $analyzed_sql_results
  1416. );
  1417. $data_html = '';
  1418. if ($col_order) {
  1419. $data_html .= '<input class="col_order" type="hidden" value="'
  1420. . implode(',', $col_order) . '" />';
  1421. }
  1422. if ($col_visib) {
  1423. $data_html .= '<input class="col_visib" type="hidden" value="'
  1424. . implode(',', $col_visib) . '" />';
  1425. }
  1426. // generate table create time
  1427. $table = new Table($this->__get('table'), $this->__get('db'));
  1428. if (! $table->isView()) {
  1429. $data_html .= '<input class="table_create_time" type="hidden" value="'
  1430. . $GLOBALS['dbi']->getTable(
  1431. $this->__get('db'), $this->__get('table')
  1432. )->getStatusInfo('Create_time')
  1433. . '" />';
  1434. }
  1435. return $data_html;
  1436. } // end of the '_getDataForResettingColumnOrder()' function
  1437. /**
  1438. * Prepare option fields block
  1439. *
  1440. * @return string html content
  1441. *
  1442. * @access private
  1443. *
  1444. * @see _getTableHeaders()
  1445. */
  1446. private function _getOptionsBlock()
  1447. {
  1448. if(isset($_SESSION['tmpval']['possible_as_geometry']) && $_SESSION['tmpval']['possible_as_geometry'] == false) {
  1449. if($_SESSION['tmpval']['geoOption'] == self::GEOMETRY_DISP_GEOM) {
  1450. $_SESSION['tmpval']['geoOption'] = self::GEOMETRY_DISP_WKT;
  1451. }
  1452. }
  1453. return Template::get('display/results/options_block')->render([
  1454. 'unique_id' => $this->__get('unique_id'),
  1455. 'geo_option' => $_SESSION['tmpval']['geoOption'],
  1456. 'hide_transformation' => $_SESSION['tmpval']['hide_transformation'],
  1457. 'display_blob' => $_SESSION['tmpval']['display_blob'],
  1458. 'display_binary' => $_SESSION['tmpval']['display_binary'],
  1459. 'relational_display' => $_SESSION['tmpval']['relational_display'],
  1460. 'displaywork' => $GLOBALS['cfgRelation']['displaywork'],
  1461. 'relwork' => $GLOBALS['cfgRelation']['relwork'],
  1462. 'possible_as_geometry' => $_SESSION['tmpval']['possible_as_geometry'],
  1463. 'pftext' => $_SESSION['tmpval']['pftext'],
  1464. 'db' => $this->__get('db'),
  1465. 'table' => $this->__get('table'),
  1466. 'sql_query' => $this->__get('sql_query'),
  1467. 'goto' => $this->__get('goto'),
  1468. ]);
  1469. }
  1470. /**
  1471. * Get full/partial text button or link
  1472. *
  1473. * @return string html content
  1474. *
  1475. * @access private
  1476. *
  1477. * @see _getTableHeaders()
  1478. */
  1479. private function _getFullOrPartialTextButtonOrLink()
  1480. {
  1481. $url_params_full_text = array(
  1482. 'db' => $this->__get('db'),
  1483. 'table' => $this->__get('table'),
  1484. 'sql_query' => $this->__get('sql_query'),
  1485. 'goto' => $this->__get('goto'),
  1486. 'full_text_button' => 1
  1487. );
  1488. if ($_SESSION['tmpval']['pftext'] == self::DISPLAY_FULL_TEXT) {
  1489. // currently in fulltext mode so show the opposite link
  1490. $tmp_image_file = $this->__get('pma_theme_image') . 's_partialtext.png';
  1491. $tmp_txt = __('Partial texts');
  1492. $url_params_full_text['pftext'] = self::DISPLAY_PARTIAL_TEXT;
  1493. } else {
  1494. $tmp_image_file = $this->__get('pma_theme_image') . 's_fulltext.png';
  1495. $tmp_txt = __('Full texts');
  1496. $url_params_full_text['pftext'] = self::DISPLAY_FULL_TEXT;
  1497. }
  1498. $tmp_image = '<img class="fulltext" src="' . $tmp_image_file . '" alt="'
  1499. . $tmp_txt . '" title="' . $tmp_txt . '" />';
  1500. $tmp_url = 'sql.php' . Url::getCommon($url_params_full_text);
  1501. return Util::linkOrButton($tmp_url, $tmp_image);
  1502. } // end of the '_getFullOrPartialTextButtonOrLink()' function
  1503. /**
  1504. * Prepare html form for multi row operations
  1505. *
  1506. * @param string $deleteLink the delete link of current row
  1507. *
  1508. * @return string $form_html html content
  1509. *
  1510. * @access private
  1511. *
  1512. * @see _getTableHeaders()
  1513. */
  1514. private function _getFormForMultiRowOperations($deleteLink)
  1515. {
  1516. return Template::get('display/results/multi_row_operations_form')->render([
  1517. 'delete_link' => $deleteLink,
  1518. 'delete_row' => self::DELETE_ROW,
  1519. 'kill_process' => self::KILL_PROCESS,
  1520. 'unique_id' => $this->__get('unique_id'),
  1521. 'db' => $this->__get('db'),
  1522. 'table' => $this->__get('table'),
  1523. ]);
  1524. }
  1525. /**
  1526. * Get comment for row
  1527. *
  1528. * @param array $commentsMap comments array
  1529. * @param array $fieldsMeta set of field properties
  1530. *
  1531. * @return string html content
  1532. *
  1533. * @access private
  1534. *
  1535. * @see _getTableHeaders()
  1536. */
  1537. private function _getCommentForRow(array $commentsMap, $fieldsMeta)
  1538. {
  1539. return Template::get('display/results/comment_for_row')->render([
  1540. 'comments_map' => $commentsMap,
  1541. 'fields_meta' => $fieldsMeta,
  1542. 'limit_chars' => $GLOBALS['cfg']['LimitChars'],
  1543. ]);
  1544. }
  1545. /**
  1546. * Prepare parameters and html for sorted table header fields
  1547. *
  1548. * @param array $fields_meta set of field properties
  1549. * @param array $sort_expression sort expression
  1550. * @param array $sort_expression_nodirection sort expression without direction
  1551. * @param integer $column_index the index of the column
  1552. * @param string $unsorted_sql_query the unsorted sql query
  1553. * @param integer $session_max_rows maximum rows resulted by sql
  1554. * @param string $comments comment for row
  1555. * @param array $sort_direction sort direction
  1556. * @param boolean $col_visib column is visible(false)
  1557. * array column isn't visible(string array)
  1558. * @param string $col_visib_j element of $col_visib array
  1559. *
  1560. * @return array 2 element array - $order_link, $sorted_header_html
  1561. *
  1562. * @access private
  1563. *
  1564. * @see _getTableHeaders()
  1565. */
  1566. private function _getOrderLinkAndSortedHeaderHtml(
  1567. $fields_meta, array $sort_expression, array $sort_expression_nodirection,
  1568. $column_index, $unsorted_sql_query, $session_max_rows,
  1569. $comments, array $sort_direction, $col_visib, $col_visib_j
  1570. ) {
  1571. $sorted_header_html = '';
  1572. // Checks if the table name is required; it's the case
  1573. // for a query with a "JOIN" statement and if the column
  1574. // isn't aliased, or in queries like
  1575. // SELECT `1`.`master_field` , `2`.`master_field`
  1576. // FROM `PMA_relation` AS `1` , `PMA_relation` AS `2`
  1577. $sort_tbl = (isset($fields_meta->table)
  1578. && strlen($fields_meta->table) > 0
  1579. && $fields_meta->orgname == $fields_meta->name)
  1580. ? Util::backquote(
  1581. $fields_meta->table
  1582. ) . '.'
  1583. : '';
  1584. $name_to_use_in_sort = $fields_meta->name;
  1585. // Generates the orderby clause part of the query which is part
  1586. // of URL
  1587. list($single_sort_order, $multi_sort_order, $order_img)
  1588. = $this->_getSingleAndMultiSortUrls(
  1589. $sort_expression, $sort_expression_nodirection, $sort_tbl,
  1590. $name_to_use_in_sort, $sort_direction, $fields_meta, $column_index
  1591. );
  1592. if (preg_match(
  1593. '@(.*)([[:space:]](LIMIT (.*)|PROCEDURE (.*)|FOR UPDATE|'
  1594. . 'LOCK IN SHARE MODE))@is',
  1595. $unsorted_sql_query, $regs3
  1596. )) {
  1597. $single_sorted_sql_query = $regs3[1] . $single_sort_order . $regs3[2];
  1598. $multi_sorted_sql_query = $regs3[1] . $multi_sort_order . $regs3[2];
  1599. } else {
  1600. $single_sorted_sql_query = $unsorted_sql_query . $single_sort_order;
  1601. $multi_sorted_sql_query = $unsorted_sql_query . $multi_sort_order;
  1602. }
  1603. $_single_url_params = array(
  1604. 'db' => $this->__get('db'),
  1605. 'table' => $this->__get('table'),
  1606. 'sql_query' => $single_sorted_sql_query,
  1607. 'session_max_rows' => $session_max_rows,
  1608. 'is_browse_distinct' => $this->__get('is_browse_distinct'),
  1609. );
  1610. $_multi_url_params = array(
  1611. 'db' => $this->__get('db'),
  1612. 'table' => $this->__get('table'),
  1613. 'sql_query' => $multi_sorted_sql_query,
  1614. 'session_max_rows' => $session_max_rows,
  1615. 'is_browse_distinct' => $this->__get('is_browse_distinct'),
  1616. );
  1617. $single_order_url = 'sql.php' . Url::getCommon($_single_url_params);
  1618. $multi_order_url = 'sql.php' . Url::getCommon($_multi_url_params);
  1619. // Displays the sorting URL
  1620. // enable sort order swapping for image
  1621. $order_link = $this->_getSortOrderLink(
  1622. $order_img, $fields_meta, $single_order_url, $multi_order_url
  1623. );
  1624. $sorted_header_html .= $this->_getDraggableClassForSortableColumns(
  1625. $col_visib, $col_visib_j,
  1626. $fields_meta, $order_link, $comments
  1627. );
  1628. return array($order_link, $sorted_header_html);
  1629. } // end of the '_getOrderLinkAndSortedHeaderHtml()' function
  1630. /**
  1631. * Prepare parameters and html for sorted table header fields
  1632. *
  1633. * @param array $sort_expression sort expression
  1634. * @param array $sort_expression_nodirection sort expression without direction
  1635. * @param string $sort_tbl The name of the table to which
  1636. * the current column belongs to
  1637. * @param string $name_to_use_in_sort The current column under
  1638. * consideration
  1639. * @param array $sort_direction sort direction
  1640. * @param array $fields_meta set of field properties
  1641. * @param integer $column_index The index number to current column
  1642. *
  1643. * @return array 3 element array - $single_sort_order, $sort_order, $order_img
  1644. *
  1645. * @access private
  1646. *
  1647. * @see _getOrderLinkAndSortedHeaderHtml()
  1648. */
  1649. private function _getSingleAndMultiSortUrls(
  1650. array $sort_expression, array $sort_expression_nodirection, $sort_tbl,
  1651. $name_to_use_in_sort, array $sort_direction, $fields_meta, $column_index
  1652. ) {
  1653. $sort_order = "";
  1654. // Check if the current column is in the order by clause
  1655. $is_in_sort = $this->_isInSorted(
  1656. $sort_expression, $sort_expression_nodirection,
  1657. $sort_tbl, $name_to_use_in_sort
  1658. );
  1659. $current_name = $name_to_use_in_sort;
  1660. if ($sort_expression_nodirection[0] == '' || !$is_in_sort) {
  1661. $special_index = $sort_expression_nodirection[0] == ''
  1662. ? 0
  1663. : count($sort_expression_nodirection);
  1664. $sort_expression_nodirection[$special_index]
  1665. = Util::backquote(
  1666. $current_name
  1667. );
  1668. $sort_direction[$special_index] = (preg_match(
  1669. '@time|date@i',
  1670. $fields_meta->type
  1671. )) ? self::DESCENDING_SORT_DIR : self::ASCENDING_SORT_DIR;
  1672. }
  1673. $sort_expression_nodirection = array_filter($sort_expression_nodirection);
  1674. $single_sort_order = null;
  1675. foreach ($sort_expression_nodirection as $index=>$expression) {
  1676. // check if this is the first clause,
  1677. // if it is then we have to add "order by"
  1678. $is_first_clause = ($index == 0);
  1679. $name_to_use_in_sort = $expression;
  1680. $sort_tbl_new = $sort_tbl;
  1681. // Test to detect if the column name is a standard name
  1682. // Standard name has the table name prefixed to the column name
  1683. if (mb_strpos($name_to_use_in_sort, '.') !== false) {
  1684. $matches = explode('.', $name_to_use_in_sort);
  1685. // Matches[0] has the table name
  1686. // Matches[1] has the column name
  1687. $name_to_use_in_sort = $matches[1];
  1688. $sort_tbl_new = $matches[0];
  1689. }
  1690. // $name_to_use_in_sort might contain a space due to
  1691. // formatting of function expressions like "COUNT(name )"
  1692. // so we remove the space in this situation
  1693. $name_to_use_in_sort = str_replace(' )', ')', $name_to_use_in_sort);
  1694. $name_to_use_in_sort = str_replace('``', '`', $name_to_use_in_sort);
  1695. $name_to_use_in_sort = trim($name_to_use_in_sort, '`');
  1696. // If this the first column name in the order by clause add
  1697. // order by clause to the column name
  1698. $query_head = $is_first_clause ? "\nORDER BY " : "";
  1699. // Again a check to see if the given column is a aggregate column
  1700. if (mb_strpos($name_to_use_in_sort, '(') !== false) {
  1701. $sort_order .= $query_head . $name_to_use_in_sort . ' ' ;
  1702. } else {
  1703. if (strlen($sort_tbl_new) > 0) {
  1704. $sort_tbl_new .= ".";
  1705. }
  1706. $sort_order .= $query_head . $sort_tbl_new
  1707. . Util::backquote(
  1708. $name_to_use_in_sort
  1709. ) . ' ' ;
  1710. }
  1711. // For a special case where the code generates two dots between
  1712. // column name and table name.
  1713. $sort_order = preg_replace("/\.\./", ".", $sort_order);
  1714. // Incase this is the current column save $single_sort_order
  1715. if ($current_name == $name_to_use_in_sort) {
  1716. if (mb_strpos($current_name, '(') !== false) {
  1717. $single_sort_order = "\n" . 'ORDER BY ' . Util::backquote($current_name) . ' ';
  1718. } else {
  1719. $single_sort_order = "\n" . 'ORDER BY ' . $sort_tbl
  1720. . Util::backquote(
  1721. $current_name
  1722. ) . ' ';
  1723. }
  1724. if ($is_in_sort) {
  1725. list($single_sort_order, $order_img)
  1726. = $this->_getSortingUrlParams(
  1727. $sort_direction, $single_sort_order, $index
  1728. );
  1729. } else {
  1730. $single_sort_order .= strtoupper($sort_direction[$index]);
  1731. }
  1732. }
  1733. if ($current_name == $name_to_use_in_sort && $is_in_sort) {
  1734. // We need to generate the arrow button and related html
  1735. list($sort_order, $order_img) = $this->_getSortingUrlParams(
  1736. $sort_direction, $sort_order, $index
  1737. );
  1738. $order_img .= " <small>" . ($index + 1) . "</small>";
  1739. } else {
  1740. $sort_order .= strtoupper($sort_direction[$index]);
  1741. }
  1742. // Separate columns by a comma
  1743. $sort_order .= ", ";
  1744. unset($name_to_use_in_sort);
  1745. }
  1746. // remove the comma from the last column name in the newly
  1747. // constructed clause
  1748. $sort_order = mb_substr(
  1749. $sort_order,
  1750. 0,
  1751. mb_strlen($sort_order) - 2
  1752. );
  1753. if (empty($order_img)) {
  1754. $order_img = '';
  1755. }
  1756. return array($single_sort_order, $sort_order, $order_img);
  1757. }
  1758. /**
  1759. * Check whether the column is sorted
  1760. *
  1761. * @param array $sort_expression sort expression
  1762. * @param array $sort_expression_nodirection sort expression without direction
  1763. * @param string $sort_tbl the table name
  1764. * @param string $name_to_use_in_sort the sorting column name
  1765. *
  1766. * @return boolean $is_in_sort the column sorted or not
  1767. *
  1768. * @access private
  1769. *
  1770. * @see _getTableHeaders()
  1771. */
  1772. private function _isInSorted(
  1773. array $sort_expression, array $sort_expression_nodirection, $sort_tbl,
  1774. $name_to_use_in_sort
  1775. ) {
  1776. $index_in_expression = 0;
  1777. foreach ($sort_expression_nodirection as $index => $clause) {
  1778. if (mb_strpos($clause, '.') !== false) {
  1779. $fragments = explode('.', $clause);
  1780. $clause2 = $fragments[0] . "." . str_replace('`', '', $fragments[1]);
  1781. } else {
  1782. $clause2 = $sort_tbl . str_replace('`', '', $clause);
  1783. }
  1784. if ($clause2 === $sort_tbl . $name_to_use_in_sort) {
  1785. $index_in_expression = $index;
  1786. break;
  1787. }
  1788. }
  1789. if (empty($sort_expression[$index_in_expression])) {
  1790. $is_in_sort = false;
  1791. } else {
  1792. // Field name may be preceded by a space, or any number
  1793. // of characters followed by a dot (tablename.fieldname)
  1794. // so do a direct comparison for the sort expression;
  1795. // this avoids problems with queries like
  1796. // "SELECT id, count(id)..." and clicking to sort
  1797. // on id or on count(id).
  1798. // Another query to test this:
  1799. // SELECT p.*, FROM_UNIXTIME(p.temps) FROM mytable AS p
  1800. // (and try clicking on each column's header twice)
  1801. $noSortTable = empty($sort_tbl) || mb_strpos(
  1802. $sort_expression_nodirection[$index_in_expression], $sort_tbl
  1803. ) === false;
  1804. $noOpenParenthesis = mb_strpos(
  1805. $sort_expression_nodirection[$index_in_expression], '('
  1806. ) === false;
  1807. if (! empty($sort_tbl) && $noSortTable && $noOpenParenthesis) {
  1808. $new_sort_expression_nodirection = $sort_tbl
  1809. . $sort_expression_nodirection[$index_in_expression];
  1810. } else {
  1811. $new_sort_expression_nodirection
  1812. = $sort_expression_nodirection[$index_in_expression];
  1813. }
  1814. //Back quotes are removed in next comparison, so remove them from value
  1815. //to compare.
  1816. $name_to_use_in_sort = str_replace('`', '', $name_to_use_in_sort);
  1817. $is_in_sort = false;
  1818. $sort_name = str_replace('`', '', $sort_tbl) . $name_to_use_in_sort;
  1819. if ($sort_name == str_replace('`', '', $new_sort_expression_nodirection)
  1820. || $sort_name == str_replace('`', '', $sort_expression_nodirection[$index_in_expression])
  1821. ) {
  1822. $is_in_sort = true;
  1823. }
  1824. }
  1825. return $is_in_sort;
  1826. } // end of the '_isInSorted()' function
  1827. /**
  1828. * Get sort url parameters - sort order and order image
  1829. *
  1830. * @param array $sort_direction the sort direction
  1831. * @param string $sort_order the sorting order
  1832. * @param integer $index the index of sort direction array.
  1833. *
  1834. * @return array 2 element array - $sort_order, $order_img
  1835. *
  1836. * @access private
  1837. *
  1838. * @see _getSingleAndMultiSortUrls()
  1839. */
  1840. private function _getSortingUrlParams(array $sort_direction, $sort_order, $index)
  1841. {
  1842. if (strtoupper(trim($sort_direction[$index])) == self::DESCENDING_SORT_DIR) {
  1843. $sort_order .= ' ASC';
  1844. $order_img = ' ' . Util::getImage(
  1845. 's_desc', __('Descending'),
  1846. array('class' => "soimg", 'title' => '')
  1847. );
  1848. $order_img .= ' ' . Util::getImage(
  1849. 's_asc', __('Ascending'),
  1850. array('class' => "soimg hide", 'title' => '')
  1851. );
  1852. } else {
  1853. $sort_order .= ' DESC';
  1854. $order_img = ' ' . Util::getImage(
  1855. 's_asc', __('Ascending'),
  1856. array('class' => "soimg", 'title' => '')
  1857. );
  1858. $order_img .= ' ' . Util::getImage(
  1859. 's_desc', __('Descending'),
  1860. array('class' => "soimg hide", 'title' => '')
  1861. );
  1862. }
  1863. return array($sort_order, $order_img);
  1864. } // end of the '_getSortingUrlParams()' function
  1865. /**
  1866. * Get sort order link
  1867. *
  1868. * @param string $order_img the sort order image
  1869. * @param array $fields_meta set of field properties
  1870. * @param string $order_url the url for sort
  1871. * @param string $multi_order_url the url for sort
  1872. *
  1873. * @return string the sort order link
  1874. *
  1875. * @access private
  1876. *
  1877. * @see _getTableHeaders()
  1878. */
  1879. private function _getSortOrderLink(
  1880. $order_img, $fields_meta, $order_url, $multi_order_url
  1881. ) {
  1882. $order_link_params = array(
  1883. 'class' => 'sortlink'
  1884. );
  1885. $order_link_content = htmlspecialchars($fields_meta->name);
  1886. $inner_link_content = $order_link_content . $order_img
  1887. . '<input type="hidden" value="' . $multi_order_url . '" />';
  1888. return Util::linkOrButton(
  1889. $order_url, $inner_link_content, $order_link_params
  1890. );
  1891. } // end of the '_getSortOrderLink()' function
  1892. /**
  1893. * Check if the column contains numeric data. If yes, then set the
  1894. * column header's alignment right
  1895. *
  1896. * @param array $fields_meta set of field properties
  1897. * @param array &$th_class array containing classes
  1898. *
  1899. * @return void
  1900. *
  1901. * @see _getDraggableClassForSortableColumns()
  1902. */
  1903. private function _getClassForNumericColumnType($fields_meta, array &$th_class)
  1904. {
  1905. if (preg_match(
  1906. '@int|decimal|float|double|real|bit|boolean|serial@i',
  1907. $fields_meta->type
  1908. )) {
  1909. $th_class[] = 'right';
  1910. }
  1911. }
  1912. /**
  1913. * Prepare columns to draggable effect for sortable columns
  1914. *
  1915. * @param boolean $col_visib the column is visible (false)
  1916. * array the column is not visible (string array)
  1917. * @param string $col_visib_j element of $col_visib array
  1918. * @param array $fields_meta set of field properties
  1919. * @param string $order_link the order link
  1920. * @param string $comments the comment for the column
  1921. *
  1922. * @return string $draggable_html html content
  1923. *
  1924. * @access private
  1925. *
  1926. * @see _getTableHeaders()
  1927. */
  1928. private function _getDraggableClassForSortableColumns(
  1929. $col_visib, $col_visib_j, $fields_meta,
  1930. $order_link, $comments
  1931. ) {
  1932. $draggable_html = '<th';
  1933. $th_class = array();
  1934. $th_class[] = 'draggable';
  1935. $this->_getClassForNumericColumnType($fields_meta, $th_class);
  1936. if ($col_visib && !$col_visib_j) {
  1937. $th_class[] = 'hide';
  1938. }
  1939. $th_class[] = 'column_heading';
  1940. if ($GLOBALS['cfg']['BrowsePointerEnable'] == true) {
  1941. $th_class[] = 'pointer';
  1942. }
  1943. if ($GLOBALS['cfg']['BrowseMarkerEnable'] == true) {
  1944. $th_class[] = 'marker';
  1945. }
  1946. $draggable_html .= ' class="' . implode(' ', $th_class) . '"';
  1947. $draggable_html .= ' data-column="' . htmlspecialchars($fields_meta->name)
  1948. . '">' . $order_link . $comments . '</th>';
  1949. return $draggable_html;
  1950. } // end of the '_getDraggableClassForSortableColumns()' function
  1951. /**
  1952. * Prepare columns to draggable effect for non sortable columns
  1953. *
  1954. * @param boolean $col_visib the column is visible (false)
  1955. * array the column is not visible (string array)
  1956. * @param string $col_visib_j element of $col_visib array
  1957. * @param boolean $condition_field whether to add CSS class condition
  1958. * @param array $fields_meta set of field properties
  1959. * @param string $comments the comment for the column
  1960. *
  1961. * @return string $draggable_html html content
  1962. *
  1963. * @access private
  1964. *
  1965. * @see _getTableHeaders()
  1966. */
  1967. private function _getDraggableClassForNonSortableColumns(
  1968. $col_visib, $col_visib_j, $condition_field,
  1969. $fields_meta, $comments
  1970. ) {
  1971. $draggable_html = '<th';
  1972. $th_class = array();
  1973. $th_class[] = 'draggable';
  1974. $this->_getClassForNumericColumnType($fields_meta, $th_class);
  1975. if ($col_visib && !$col_visib_j) {
  1976. $th_class[] = 'hide';
  1977. }
  1978. if ($condition_field) {
  1979. $th_class[] = 'condition';
  1980. }
  1981. $draggable_html .= ' class="' . implode(' ', $th_class) . '"';
  1982. $draggable_html .= ' data-column="'
  1983. . htmlspecialchars($fields_meta->name) . '">';
  1984. $draggable_html .= htmlspecialchars($fields_meta->name);
  1985. $draggable_html .= "\n" . $comments . '</th>';
  1986. return $draggable_html;
  1987. } // end of the '_getDraggableClassForNonSortableColumns()' function
  1988. /**
  1989. * Prepare column to show at right side - check boxes or empty column
  1990. *
  1991. * @param array &$displayParts which elements to display
  1992. * @param string $full_or_partial_text_link full/partial link or text button
  1993. * @param string $colspan column span of table header
  1994. *
  1995. * @return string html content
  1996. *
  1997. * @access private
  1998. *
  1999. * @see _getTableHeaders()
  2000. */
  2001. private function _getColumnAtRightSide(
  2002. array &$displayParts, $full_or_partial_text_link, $colspan
  2003. ) {
  2004. $right_column_html = '';
  2005. $display_params = $this->__get('display_params');
  2006. // Displays the needed checkboxes at the right
  2007. // column of the result table header if possible and required...
  2008. if ((($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_RIGHT)
  2009. || ($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_BOTH))
  2010. && (($displayParts['edit_lnk'] != self::NO_EDIT_OR_DELETE)
  2011. || ($displayParts['del_lnk'] != self::NO_EDIT_OR_DELETE))
  2012. && ($displayParts['text_btn'] == '1')
  2013. ) {
  2014. $display_params['emptyafter']
  2015. = (($displayParts['edit_lnk'] != self::NO_EDIT_OR_DELETE)
  2016. && ($displayParts['del_lnk'] != self::NO_EDIT_OR_DELETE)) ? 4 : 1;
  2017. $right_column_html .= "\n"
  2018. . '<th class="column_action print_ignore" ' . $colspan . '>'
  2019. . $full_or_partial_text_link
  2020. . '</th>';
  2021. } elseif ((($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_LEFT)
  2022. || ($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_BOTH))
  2023. && (($displayParts['edit_lnk'] == self::NO_EDIT_OR_DELETE)
  2024. && ($displayParts['del_lnk'] == self::NO_EDIT_OR_DELETE))
  2025. && (! isset($GLOBALS['is_header_sent']) || ! $GLOBALS['is_header_sent'])
  2026. ) {
  2027. // ... elseif no button, displays empty columns if required
  2028. // (unless coming from Browse mode print view)
  2029. $display_params['emptyafter']
  2030. = (($displayParts['edit_lnk'] != self::NO_EDIT_OR_DELETE)
  2031. && ($displayParts['del_lnk'] != self::NO_EDIT_OR_DELETE)) ? 4 : 1;
  2032. $right_column_html .= "\n" . '<td class="print_ignore" ' . $colspan
  2033. . '></td>';
  2034. }
  2035. $this->__set('display_params', $display_params);
  2036. return $right_column_html;
  2037. } // end of the '_getColumnAtRightSide()' function
  2038. /**
  2039. * Prepares the display for a value
  2040. *
  2041. * @param string $class class of table cell
  2042. * @param bool $conditionField whether to add CSS class condition
  2043. * @param string $value value to display
  2044. *
  2045. * @return string the td
  2046. *
  2047. * @access private
  2048. *
  2049. * @see _getDataCellForGeometryColumns(),
  2050. * _getDataCellForNonNumericColumns()
  2051. */
  2052. private function _buildValueDisplay($class, $conditionField, $value)
  2053. {
  2054. return Template::get('display/results/value_display')->render([
  2055. 'class' => $class,
  2056. 'condition_field' => $conditionField,
  2057. 'value' => $value,
  2058. ]);
  2059. }
  2060. /**
  2061. * Prepares the display for a null value
  2062. *
  2063. * @param string $class class of table cell
  2064. * @param bool $conditionField whether to add CSS class condition
  2065. * @param object $meta the meta-information about this field
  2066. * @param string $align cell alignment
  2067. *
  2068. * @return string the td
  2069. *
  2070. * @access private
  2071. *
  2072. * @see _getDataCellForNumericColumns(),
  2073. * _getDataCellForGeometryColumns(),
  2074. * _getDataCellForNonNumericColumns()
  2075. */
  2076. private function _buildNullDisplay($class, $conditionField, $meta, $align = '')
  2077. {
  2078. $classes = $this->_addClass($class, $conditionField, $meta, '');
  2079. return Template::get('display/results/null_display')->render([
  2080. 'align' => $align,
  2081. 'meta' => $meta,
  2082. 'classes' => $classes,
  2083. ]);
  2084. }
  2085. /**
  2086. * Prepares the display for an empty value
  2087. *
  2088. * @param string $class class of table cell
  2089. * @param bool $conditionField whether to add CSS class condition
  2090. * @param object $meta the meta-information about this field
  2091. * @param string $align cell alignment
  2092. *
  2093. * @return string the td
  2094. *
  2095. * @access private
  2096. *
  2097. * @see _getDataCellForNumericColumns(),
  2098. * _getDataCellForGeometryColumns(),
  2099. * _getDataCellForNonNumericColumns()
  2100. */
  2101. private function _buildEmptyDisplay($class, $conditionField, $meta, $align = '')
  2102. {
  2103. $classes = $this->_addClass($class, $conditionField, $meta, 'nowrap');
  2104. return Template::get('display/results/empty_display')->render([
  2105. 'align' => $align,
  2106. 'classes' => $classes,
  2107. ]);
  2108. }
  2109. /**
  2110. * Adds the relevant classes.
  2111. *
  2112. * @param string $class class of table cell
  2113. * @param bool $condition_field whether to add CSS class
  2114. * condition
  2115. * @param object $meta the meta-information about the
  2116. * field
  2117. * @param string $nowrap avoid wrapping
  2118. * @param bool $is_field_truncated is field truncated (display ...)
  2119. * @param object|string $transformation_plugin transformation plugin.
  2120. * Can also be the default function:
  2121. * Core::mimeDefaultFunction
  2122. * @param string $default_function default transformation function
  2123. *
  2124. * @return string the list of classes
  2125. *
  2126. * @access private
  2127. *
  2128. * @see _buildNullDisplay(), _getRowData()
  2129. */
  2130. private function _addClass(
  2131. $class, $condition_field, $meta, $nowrap, $is_field_truncated = false,
  2132. $transformation_plugin = '', $default_function = ''
  2133. ) {
  2134. $classes = array(
  2135. $class,
  2136. $nowrap,
  2137. );
  2138. if (isset($meta->mimetype)) {
  2139. $classes[] = preg_replace('/\//', '_', $meta->mimetype);
  2140. }
  2141. if ($condition_field) {
  2142. $classes[] = 'condition';
  2143. }
  2144. if ($is_field_truncated) {
  2145. $classes[] = 'truncated';
  2146. }
  2147. $mime_map = $this->__get('mime_map');
  2148. $orgFullColName = $this->__get('db') . '.' . $meta->orgtable
  2149. . '.' . $meta->orgname;
  2150. if ($transformation_plugin != $default_function
  2151. || !empty($mime_map[$orgFullColName]['input_transformation'])
  2152. ) {
  2153. $classes[] = 'transformed';
  2154. }
  2155. // Define classes to be added to this data field based on the type of data
  2156. $matches = array(
  2157. 'enum' => 'enum',
  2158. 'set' => 'set',
  2159. 'binary' => 'hex',
  2160. );
  2161. foreach ($matches as $key => $value) {
  2162. if (mb_strpos($meta->flags, $key) !== false) {
  2163. $classes[] = $value;
  2164. }
  2165. }
  2166. if (mb_strpos($meta->type, 'bit') !== false) {
  2167. $classes[] = 'bit';
  2168. }
  2169. return implode(' ', $classes);
  2170. } // end of the '_addClass()' function
  2171. /**
  2172. * Prepare the body of the results table
  2173. *
  2174. * @param integer &$dt_result the link id associated to the query
  2175. * which results have to be displayed which
  2176. * results have to be displayed
  2177. * @param array &$displayParts which elements to display
  2178. * @param array $map the list of relations
  2179. * @param array $analyzed_sql_results analyzed sql results
  2180. * @param boolean $is_limited_display with limited operations or not
  2181. *
  2182. * @return string $table_body_html html content
  2183. *
  2184. * @global array $row current row data
  2185. *
  2186. * @access private
  2187. *
  2188. * @see getTable()
  2189. */
  2190. private function _getTableBody(
  2191. &$dt_result, array &$displayParts, array $map, array $analyzed_sql_results,
  2192. $is_limited_display = false
  2193. ) {
  2194. global $row; // mostly because of browser transformations,
  2195. // to make the row-data accessible in a plugin
  2196. $table_body_html = '';
  2197. // query without conditions to shorten URLs when needed, 200 is just
  2198. // guess, it should depend on remaining URL length
  2199. $url_sql_query = $this->_getUrlSqlQuery($analyzed_sql_results);
  2200. $display_params = $this->__get('display_params');
  2201. if (! is_array($map)) {
  2202. $map = array();
  2203. }
  2204. $row_no = 0;
  2205. $display_params['edit'] = array();
  2206. $display_params['copy'] = array();
  2207. $display_params['delete'] = array();
  2208. $display_params['data'] = array();
  2209. $display_params['row_delete'] = array();
  2210. $this->__set('display_params', $display_params);
  2211. // name of the class added to all grid editable elements;
  2212. // if we don't have all the columns of a unique key in the result set,
  2213. // do not permit grid editing
  2214. if ($is_limited_display || ! $this->__get('editable')) {
  2215. $grid_edit_class = '';
  2216. } else {
  2217. switch ($GLOBALS['cfg']['GridEditing']) {
  2218. case 'double-click':
  2219. // trying to reduce generated HTML by using shorter
  2220. // classes like click1 and click2
  2221. $grid_edit_class = 'grid_edit click2';
  2222. break;
  2223. case 'click':
  2224. $grid_edit_class = 'grid_edit click1';
  2225. break;
  2226. default: // 'disabled'
  2227. $grid_edit_class = '';
  2228. break;
  2229. }
  2230. }
  2231. // prepare to get the column order, if available
  2232. list($col_order, $col_visib) = $this->_getColumnParams(
  2233. $analyzed_sql_results
  2234. );
  2235. // Correction University of Virginia 19991216 in the while below
  2236. // Previous code assumed that all tables have keys, specifically that
  2237. // the phpMyAdmin GUI should support row delete/edit only for such
  2238. // tables.
  2239. // Although always using keys is arguably the prescribed way of
  2240. // defining a relational table, it is not required. This will in
  2241. // particular be violated by the novice.
  2242. // We want to encourage phpMyAdmin usage by such novices. So the code
  2243. // below has been changed to conditionally work as before when the
  2244. // table being displayed has one or more keys; but to display
  2245. // delete/edit options correctly for tables without keys.
  2246. $whereClauseMap = $this->__get('whereClauseMap');
  2247. while ($row = $GLOBALS['dbi']->fetchRow($dt_result)) {
  2248. // add repeating headers
  2249. if ((($row_no != 0) && ($_SESSION['tmpval']['repeat_cells'] != 0))
  2250. && !($row_no % $_SESSION['tmpval']['repeat_cells'])
  2251. ) {
  2252. $table_body_html .= $this->_getRepeatingHeaders(
  2253. $display_params
  2254. );
  2255. }
  2256. $tr_class = array();
  2257. if ($GLOBALS['cfg']['BrowsePointerEnable'] != true) {
  2258. $tr_class[] = 'nopointer';
  2259. }
  2260. if ($GLOBALS['cfg']['BrowseMarkerEnable'] != true) {
  2261. $tr_class[] = 'nomarker';
  2262. }
  2263. // pointer code part
  2264. $classes = (empty($tr_class) ? ' ' : 'class="' . implode(' ', $tr_class) . '"');
  2265. $table_body_html .= '<tr ' . $classes . ' >';
  2266. // 1. Prepares the row
  2267. // In print view these variable needs to be initialized
  2268. $del_url = $del_str = $edit_anchor_class
  2269. = $edit_str = $js_conf = $copy_url = $copy_str = $edit_url = null;
  2270. // 1.2 Defines the URLs for the modify/delete link(s)
  2271. if (($displayParts['edit_lnk'] != self::NO_EDIT_OR_DELETE)
  2272. || ($displayParts['del_lnk'] != self::NO_EDIT_OR_DELETE)
  2273. ) {
  2274. // Results from a "SELECT" statement -> builds the
  2275. // WHERE clause to use in links (a unique key if possible)
  2276. /**
  2277. * @todo $where_clause could be empty, for example a table
  2278. * with only one field and it's a BLOB; in this case,
  2279. * avoid to display the delete and edit links
  2280. */
  2281. list($where_clause, $clause_is_unique, $condition_array)
  2282. = Util::getUniqueCondition(
  2283. $dt_result, // handle
  2284. $this->__get('fields_cnt'), // fields_cnt
  2285. $this->__get('fields_meta'), // fields_meta
  2286. $row, // row
  2287. false, // force_unique
  2288. $this->__get('table'), // restrict_to_table
  2289. $analyzed_sql_results // analyzed_sql_results
  2290. );
  2291. $whereClauseMap[$row_no][$this->__get('table')] = $where_clause;
  2292. $this->__set('whereClauseMap', $whereClauseMap);
  2293. $where_clause_html = htmlspecialchars($where_clause);
  2294. // 1.2.1 Modify link(s) - update row case
  2295. if ($displayParts['edit_lnk'] == self::UPDATE_ROW) {
  2296. list($edit_url, $copy_url, $edit_str, $copy_str,
  2297. $edit_anchor_class)
  2298. = $this->_getModifiedLinks(
  2299. $where_clause,
  2300. $clause_is_unique, $url_sql_query
  2301. );
  2302. } // end if (1.2.1)
  2303. // 1.2.2 Delete/Kill link(s)
  2304. list($del_url, $del_str, $js_conf)
  2305. = $this->_getDeleteAndKillLinks(
  2306. $where_clause, $clause_is_unique,
  2307. $url_sql_query, $displayParts['del_lnk'],
  2308. $row
  2309. );
  2310. // 1.3 Displays the links at left if required
  2311. if (($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_LEFT)
  2312. || ($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_BOTH)
  2313. ) {
  2314. $table_body_html .= $this->_getPlacedLinks(
  2315. self::POSITION_LEFT, $del_url, $displayParts, $row_no,
  2316. $where_clause, $where_clause_html, $condition_array,
  2317. $edit_url, $copy_url, $edit_anchor_class,
  2318. $edit_str, $copy_str, $del_str, $js_conf
  2319. );
  2320. } elseif ($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_NONE) {
  2321. $table_body_html .= $this->_getPlacedLinks(
  2322. self::POSITION_NONE, $del_url, $displayParts, $row_no,
  2323. $where_clause, $where_clause_html, $condition_array,
  2324. $edit_url, $copy_url, $edit_anchor_class,
  2325. $edit_str, $copy_str, $del_str, $js_conf
  2326. );
  2327. } // end if (1.3)
  2328. } // end if (1)
  2329. // 2. Displays the rows' values
  2330. if (is_null($this->__get('mime_map'))) {
  2331. $this->_setMimeMap();
  2332. }
  2333. $table_body_html .= $this->_getRowValues(
  2334. $dt_result,
  2335. $row,
  2336. $row_no,
  2337. $col_order,
  2338. $map,
  2339. $grid_edit_class,
  2340. $col_visib,
  2341. $url_sql_query,
  2342. $analyzed_sql_results
  2343. );
  2344. // 3. Displays the modify/delete links on the right if required
  2345. if (($displayParts['edit_lnk'] != self::NO_EDIT_OR_DELETE)
  2346. || ($displayParts['del_lnk'] != self::NO_EDIT_OR_DELETE)
  2347. ) {
  2348. if (($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_RIGHT)
  2349. || ($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_BOTH)
  2350. ) {
  2351. $table_body_html .= $this->_getPlacedLinks(
  2352. self::POSITION_RIGHT, $del_url, $displayParts, $row_no,
  2353. $where_clause, $where_clause_html, $condition_array,
  2354. $edit_url, $copy_url, $edit_anchor_class,
  2355. $edit_str, $copy_str, $del_str, $js_conf
  2356. );
  2357. }
  2358. } // end if (3)
  2359. $table_body_html .= '</tr>';
  2360. $table_body_html .= "\n";
  2361. $row_no++;
  2362. } // end while
  2363. return $table_body_html;
  2364. } // end of the '_getTableBody()' function
  2365. /**
  2366. * Sets the MIME details of the columns in the results set
  2367. *
  2368. * @return void
  2369. */
  2370. private function _setMimeMap()
  2371. {
  2372. $fields_meta = $this->__get('fields_meta');
  2373. $mimeMap = array();
  2374. $added = array();
  2375. for ($currentColumn = 0;
  2376. $currentColumn < $this->__get('fields_cnt');
  2377. ++$currentColumn) {
  2378. $meta = $fields_meta[$currentColumn];
  2379. $orgFullTableName = $this->__get('db') . '.' . $meta->orgtable;
  2380. if ($GLOBALS['cfgRelation']['commwork']
  2381. && $GLOBALS['cfgRelation']['mimework']
  2382. && $GLOBALS['cfg']['BrowseMIME']
  2383. && ! $_SESSION['tmpval']['hide_transformation']
  2384. && empty($added[$orgFullTableName])
  2385. ) {
  2386. $mimeMap = array_merge(
  2387. $mimeMap,
  2388. Transformations::getMIME($this->__get('db'), $meta->orgtable, false, true)
  2389. );
  2390. $added[$orgFullTableName] = true;
  2391. }
  2392. }
  2393. // special browser transformation for some SHOW statements
  2394. if ($this->__get('is_show')
  2395. && ! $_SESSION['tmpval']['hide_transformation']
  2396. ) {
  2397. preg_match(
  2398. '@^SHOW[[:space:]]+(VARIABLES|(FULL[[:space:]]+)?'
  2399. . 'PROCESSLIST|STATUS|TABLE|GRANTS|CREATE|LOGS|DATABASES|FIELDS'
  2400. . ')@i',
  2401. $this->__get('sql_query'), $which
  2402. );
  2403. if (isset($which[1])) {
  2404. $str = ' ' . strtoupper($which[1]);
  2405. $isShowProcessList = strpos($str, 'PROCESSLIST') > 0;
  2406. if ($isShowProcessList) {
  2407. $mimeMap['..Info'] = array(
  2408. 'mimetype' => 'Text_Plain',
  2409. 'transformation' => 'output/Text_Plain_Sql.php',
  2410. );
  2411. }
  2412. $isShowCreateTable = preg_match(
  2413. '@CREATE[[:space:]]+TABLE@i', $this->__get('sql_query')
  2414. );
  2415. if ($isShowCreateTable) {
  2416. $mimeMap['..Create Table'] = array(
  2417. 'mimetype' => 'Text_Plain',
  2418. 'transformation' => 'output/Text_Plain_Sql.php',
  2419. );
  2420. }
  2421. }
  2422. }
  2423. $this->__set('mime_map', $mimeMap);
  2424. }
  2425. /**
  2426. * Get the values for one data row
  2427. *
  2428. * @param integer &$dt_result the link id associated to
  2429. * the query which results
  2430. * have to be displayed which
  2431. * results have to be
  2432. * displayed
  2433. * @param array $row current row data
  2434. * @param integer $row_no the index of current row
  2435. * @param array|boolean $col_order the column order false when
  2436. * a property not found false
  2437. * when a property not found
  2438. * @param array $map the list of relations
  2439. * @param string $grid_edit_class the class for all editable
  2440. * columns
  2441. * @param boolean|array|string $col_visib column is visible(false);
  2442. * column isn't visible(string
  2443. * array)
  2444. * @param string $url_sql_query the analyzed sql query
  2445. * @param array $analyzed_sql_results analyzed sql results
  2446. *
  2447. * @return string $row_values_html html content
  2448. *
  2449. * @access private
  2450. *
  2451. * @see _getTableBody()
  2452. */
  2453. private function _getRowValues(
  2454. &$dt_result, array $row, $row_no, $col_order, array $map,
  2455. $grid_edit_class, $col_visib,
  2456. $url_sql_query, array $analyzed_sql_results
  2457. ) {
  2458. $row_values_html = '';
  2459. // Following variable are needed for use in isset/empty or
  2460. // use with array indexes/safe use in foreach
  2461. $sql_query = $this->__get('sql_query');
  2462. $fields_meta = $this->__get('fields_meta');
  2463. $highlight_columns = $this->__get('highlight_columns');
  2464. $mime_map = $this->__get('mime_map');
  2465. $row_info = $this->_getRowInfoForSpecialLinks($row, $col_order);
  2466. $whereClauseMap = $this->__get('whereClauseMap');
  2467. $columnCount = $this->__get('fields_cnt');
  2468. for ($currentColumn = 0;
  2469. $currentColumn < $columnCount;
  2470. ++$currentColumn) {
  2471. // assign $i with appropriate column order
  2472. $i = $col_order ? $col_order[$currentColumn] : $currentColumn;
  2473. $meta = $fields_meta[$i];
  2474. $orgFullColName
  2475. = $this->__get('db') . '.' . $meta->orgtable . '.' . $meta->orgname;
  2476. $not_null_class = $meta->not_null ? 'not_null' : '';
  2477. $relation_class = isset($map[$meta->name]) ? 'relation' : '';
  2478. $hide_class = ($col_visib && ! $col_visib[$currentColumn])
  2479. ? 'hide'
  2480. : '';
  2481. $grid_edit = $meta->orgtable != '' ? $grid_edit_class : '';
  2482. // handle datetime-related class, for grid editing
  2483. $field_type_class
  2484. = $this->_getClassForDateTimeRelatedFields($meta->type);
  2485. $is_field_truncated = false;
  2486. // combine all the classes applicable to this column's value
  2487. $class = $this->_getClassesForColumn(
  2488. $grid_edit, $not_null_class, $relation_class,
  2489. $hide_class, $field_type_class
  2490. );
  2491. // See if this column should get highlight because it's used in the
  2492. // where-query.
  2493. $condition_field = (isset($highlight_columns)
  2494. && (isset($highlight_columns[$meta->name])
  2495. || isset($highlight_columns[Util::backquote($meta->name)])))
  2496. ? true
  2497. : false;
  2498. // Wrap MIME-transformations. [MIME]
  2499. $default_function = [Core::class, 'mimeDefaultFunction']; // default_function
  2500. $transformation_plugin = $default_function;
  2501. $transform_options = array();
  2502. if ($GLOBALS['cfgRelation']['mimework']
  2503. && $GLOBALS['cfg']['BrowseMIME']
  2504. ) {
  2505. if (isset($mime_map[$orgFullColName]['mimetype'])
  2506. && !empty($mime_map[$orgFullColName]['transformation'])
  2507. ) {
  2508. $file = $mime_map[$orgFullColName]['transformation'];
  2509. $include_file = 'libraries/classes/Plugins/Transformations/' . $file;
  2510. if (@file_exists($include_file)) {
  2511. $class_name = Transformations::getClassName($include_file);
  2512. if (class_exists($class_name)) {
  2513. // todo add $plugin_manager
  2514. $plugin_manager = null;
  2515. $transformation_plugin = new $class_name(
  2516. $plugin_manager
  2517. );
  2518. $transform_options = Transformations::getOptions(
  2519. isset(
  2520. $mime_map[$orgFullColName]
  2521. ['transformation_options']
  2522. )
  2523. ? $mime_map[$orgFullColName]
  2524. ['transformation_options']
  2525. : ''
  2526. );
  2527. $meta->mimetype = str_replace(
  2528. '_', '/',
  2529. $mime_map[$orgFullColName]['mimetype']
  2530. );
  2531. }
  2532. } // end if file_exists
  2533. } // end if transformation is set
  2534. } // end if mime/transformation works.
  2535. // Check whether the field needs to display with syntax highlighting
  2536. $dbLower = mb_strtolower($this->__get('db'));
  2537. $tblLower = mb_strtolower($meta->orgtable);
  2538. $nameLower = mb_strtolower($meta->orgname);
  2539. if (! empty($this->transformation_info[$dbLower][$tblLower][$nameLower])
  2540. && (trim($row[$i]) != '')
  2541. && ! $_SESSION['tmpval']['hide_transformation']
  2542. ) {
  2543. include_once $this->transformation_info
  2544. [$dbLower][$tblLower][$nameLower][0];
  2545. $transformation_plugin = new $this->transformation_info
  2546. [$dbLower][$tblLower][$nameLower][1](null);
  2547. $transform_options = Transformations::getOptions(
  2548. isset($mime_map[$orgFullColName]['transformation_options'])
  2549. ? $mime_map[$orgFullColName]['transformation_options']
  2550. : ''
  2551. );
  2552. $meta->mimetype = str_replace(
  2553. '_', '/',
  2554. $this->transformation_info[$dbLower]
  2555. [mb_strtolower($meta->orgtable)]
  2556. [mb_strtolower($meta->orgname)][2]
  2557. );
  2558. }
  2559. // Check for the predefined fields need to show as link in schemas
  2560. include_once 'libraries/special_schema_links.inc.php';
  2561. if (isset($GLOBALS['special_schema_links'])
  2562. && (! empty($GLOBALS['special_schema_links'][$dbLower][$tblLower][$nameLower]))
  2563. ) {
  2564. $linking_url = $this->_getSpecialLinkUrl(
  2565. $row[$i], $row_info, mb_strtolower($meta->orgname)
  2566. );
  2567. $transformation_plugin = new Text_Plain_Link();
  2568. $transform_options = array(
  2569. 0 => $linking_url,
  2570. 2 => true
  2571. );
  2572. $meta->mimetype = str_replace(
  2573. '_', '/',
  2574. 'Text/Plain'
  2575. );
  2576. }
  2577. /*
  2578. * The result set can have columns from more than one table,
  2579. * this is why we have to check for the unique conditions
  2580. * related to this table; however getUniqueCondition() is
  2581. * costly and does not need to be called if we already know
  2582. * the conditions for the current table.
  2583. */
  2584. if (! isset($whereClauseMap[$row_no][$meta->orgtable])) {
  2585. $unique_conditions = Util::getUniqueCondition(
  2586. $dt_result, // handle
  2587. $this->__get('fields_cnt'), // fields_cnt
  2588. $this->__get('fields_meta'), // fields_meta
  2589. $row, // row
  2590. false, // force_unique
  2591. $meta->orgtable, // restrict_to_table
  2592. $analyzed_sql_results // analyzed_sql_results
  2593. );
  2594. $whereClauseMap[$row_no][$meta->orgtable] = $unique_conditions[0];
  2595. }
  2596. $_url_params = array(
  2597. 'db' => $this->__get('db'),
  2598. 'table' => $meta->orgtable,
  2599. 'where_clause' => $whereClauseMap[$row_no][$meta->orgtable],
  2600. 'transform_key' => $meta->orgname
  2601. );
  2602. if (! empty($sql_query)) {
  2603. $_url_params['sql_query'] = $url_sql_query;
  2604. }
  2605. $transform_options['wrapper_link'] = Url::getCommon($_url_params);
  2606. $display_params = $this->__get('display_params');
  2607. // in some situations (issue 11406), numeric returns 1
  2608. // even for a string type
  2609. // for decimal numeric is returning 1
  2610. // have to improve logic
  2611. if (($meta->numeric == 1 && $meta->type != 'string') || $meta->type == 'real') {
  2612. // n u m e r i c
  2613. $display_params['data'][$row_no][$i]
  2614. = $this->_getDataCellForNumericColumns(
  2615. $row[$i],
  2616. $class,
  2617. $condition_field,
  2618. $meta,
  2619. $map,
  2620. $is_field_truncated,
  2621. $analyzed_sql_results,
  2622. $transformation_plugin,
  2623. $default_function,
  2624. $transform_options
  2625. );
  2626. } elseif ($meta->type == self::GEOMETRY_FIELD) {
  2627. // g e o m e t r y
  2628. // Remove 'grid_edit' from $class as we do not allow to
  2629. // inline-edit geometry data.
  2630. $class = str_replace('grid_edit', '', $class);
  2631. $display_params['data'][$row_no][$i]
  2632. = $this->_getDataCellForGeometryColumns(
  2633. $row[$i],
  2634. $class,
  2635. $meta,
  2636. $map,
  2637. $_url_params,
  2638. $condition_field,
  2639. $transformation_plugin,
  2640. $default_function,
  2641. $transform_options,
  2642. $analyzed_sql_results
  2643. );
  2644. } else {
  2645. // n o t n u m e r i c
  2646. $display_params['data'][$row_no][$i]
  2647. = $this->_getDataCellForNonNumericColumns(
  2648. $row[$i],
  2649. $class,
  2650. $meta,
  2651. $map,
  2652. $_url_params,
  2653. $condition_field,
  2654. $transformation_plugin,
  2655. $default_function,
  2656. $transform_options,
  2657. $is_field_truncated,
  2658. $analyzed_sql_results,
  2659. $dt_result,
  2660. $i
  2661. );
  2662. }
  2663. // output stored cell
  2664. $row_values_html .= $display_params['data'][$row_no][$i];
  2665. if (isset($display_params['rowdata'][$i][$row_no])) {
  2666. $display_params['rowdata'][$i][$row_no]
  2667. .= $display_params['data'][$row_no][$i];
  2668. } else {
  2669. $display_params['rowdata'][$i][$row_no]
  2670. = $display_params['data'][$row_no][$i];
  2671. }
  2672. $this->__set('display_params', $display_params);
  2673. } // end for
  2674. return $row_values_html;
  2675. } // end of the '_getRowValues()' function
  2676. /**
  2677. * Get link for display special schema links
  2678. *
  2679. * @param string $column_value column value
  2680. * @param array $row_info information about row
  2681. * @param string $field_name column name
  2682. *
  2683. * @return string generated link
  2684. */
  2685. private function _getSpecialLinkUrl($column_value, array $row_info, $field_name)
  2686. {
  2687. $linking_url_params = array();
  2688. $link_relations = $GLOBALS['special_schema_links']
  2689. [mb_strtolower($this->__get('db'))]
  2690. [mb_strtolower($this->__get('table'))]
  2691. [$field_name];
  2692. if (! is_array($link_relations['link_param'])) {
  2693. $linking_url_params[$link_relations['link_param']] = $column_value;
  2694. } else {
  2695. // Consider only the case of creating link for column field
  2696. // sql query that needs to be passed as url param
  2697. $sql = 'SELECT `' . $column_value . '` FROM `'
  2698. . $row_info[$link_relations['link_param'][1]] . '`.`'
  2699. . $row_info[$link_relations['link_param'][2]] . '`';
  2700. $linking_url_params[$link_relations['link_param'][0]] = $sql;
  2701. }
  2702. $divider = strpos($link_relations['default_page'], '?') ? '&' : '?';
  2703. if (empty($link_relations['link_dependancy_params'])) {
  2704. return $link_relations['default_page']
  2705. . Url::getCommonRaw($linking_url_params, $divider);
  2706. }
  2707. foreach ($link_relations['link_dependancy_params'] as $new_param) {
  2708. // If param_info is an array, set the key and value
  2709. // from that array
  2710. if (is_array($new_param['param_info'])) {
  2711. $linking_url_params[$new_param['param_info'][0]]
  2712. = $new_param['param_info'][1];
  2713. continue;
  2714. }
  2715. $linking_url_params[$new_param['param_info']]
  2716. = $row_info[mb_strtolower($new_param['column_name'])];
  2717. // Special case 1 - when executing routines, according
  2718. // to the type of the routine, url param changes
  2719. if (empty($row_info['routine_type'])) {
  2720. continue;
  2721. }
  2722. }
  2723. return $link_relations['default_page']
  2724. . Url::getCommonRaw($linking_url_params, $divider);
  2725. }
  2726. /**
  2727. * Prepare row information for display special links
  2728. *
  2729. * @param array $row current row data
  2730. * @param array|boolean $col_order the column order
  2731. *
  2732. * @return array $row_info associative array with column nama -> value
  2733. */
  2734. private function _getRowInfoForSpecialLinks(array $row, $col_order)
  2735. {
  2736. $row_info = array();
  2737. $fields_meta = $this->__get('fields_meta');
  2738. for ($n = 0; $n < $this->__get('fields_cnt'); ++$n) {
  2739. $m = $col_order ? $col_order[$n] : $n;
  2740. $row_info[mb_strtolower($fields_meta[$m]->orgname)]
  2741. = $row[$m];
  2742. }
  2743. return $row_info;
  2744. }
  2745. /**
  2746. * Get url sql query without conditions to shorten URLs
  2747. *
  2748. * @param array $analyzed_sql_results analyzed sql results
  2749. *
  2750. * @return string $url_sql analyzed sql query
  2751. *
  2752. * @access private
  2753. *
  2754. * @see _getTableBody()
  2755. */
  2756. private function _getUrlSqlQuery(array $analyzed_sql_results)
  2757. {
  2758. if (($analyzed_sql_results['querytype'] != 'SELECT')
  2759. || (mb_strlen($this->__get('sql_query')) < 200)
  2760. ) {
  2761. return $this->__get('sql_query');
  2762. }
  2763. $query = 'SELECT ' . Query::getClause(
  2764. $analyzed_sql_results['statement'],
  2765. $analyzed_sql_results['parser']->list,
  2766. 'SELECT'
  2767. );
  2768. $from_clause = Query::getClause(
  2769. $analyzed_sql_results['statement'],
  2770. $analyzed_sql_results['parser']->list,
  2771. 'FROM'
  2772. );
  2773. if (!empty($from_clause)) {
  2774. $query .= ' FROM ' . $from_clause;
  2775. }
  2776. return $query;
  2777. } // end of the '_getUrlSqlQuery()' function
  2778. /**
  2779. * Get column order and column visibility
  2780. *
  2781. * @param array $analyzed_sql_results analyzed sql results
  2782. *
  2783. * @return array 2 element array - $col_order, $col_visib
  2784. *
  2785. * @access private
  2786. *
  2787. * @see _getTableBody()
  2788. */
  2789. private function _getColumnParams(array $analyzed_sql_results)
  2790. {
  2791. if ($this->_isSelect($analyzed_sql_results)) {
  2792. $pmatable = new Table($this->__get('table'), $this->__get('db'));
  2793. $col_order = $pmatable->getUiProp(Table::PROP_COLUMN_ORDER);
  2794. /* Validate the value */
  2795. if ($col_order !== false) {
  2796. $fields_cnt = $this->__get('fields_cnt');
  2797. foreach ($col_order as $value) {
  2798. if ($value >= $fields_cnt) {
  2799. $pmatable->removeUiProp(Table::PROP_COLUMN_ORDER);
  2800. $fields_cnt = false;
  2801. }
  2802. }
  2803. }
  2804. $col_visib = $pmatable->getUiProp(Table::PROP_COLUMN_VISIB);
  2805. } else {
  2806. $col_order = false;
  2807. $col_visib = false;
  2808. }
  2809. return array($col_order, $col_visib);
  2810. } // end of the '_getColumnParams()' function
  2811. /**
  2812. * Get HTML for repeating headers
  2813. *
  2814. * @param array $display_params holds various display info
  2815. *
  2816. * @return string $header_html html content
  2817. *
  2818. * @access private
  2819. *
  2820. * @see _getTableBody()
  2821. */
  2822. private function _getRepeatingHeaders(
  2823. array $display_params
  2824. ) {
  2825. $header_html = '<tr>' . "\n";
  2826. if ($display_params['emptypre'] > 0) {
  2827. $header_html .= ' <th colspan="'
  2828. . $display_params['emptypre'] . '">'
  2829. . "\n" . ' &nbsp;</th>' . "\n";
  2830. } elseif ($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_NONE) {
  2831. $header_html .= ' <th></th>' . "\n";
  2832. }
  2833. foreach ($display_params['desc'] as $val) {
  2834. $header_html .= $val;
  2835. }
  2836. if ($display_params['emptyafter'] > 0) {
  2837. $header_html
  2838. .= ' <th colspan="' . $display_params['emptyafter']
  2839. . '">'
  2840. . "\n" . ' &nbsp;</th>' . "\n";
  2841. }
  2842. $header_html .= '</tr>' . "\n";
  2843. return $header_html;
  2844. } // end of the '_getRepeatingHeaders()' function
  2845. /**
  2846. * Get modified links
  2847. *
  2848. * @param string $where_clause the where clause of the sql
  2849. * @param boolean $clause_is_unique the unique condition of clause
  2850. * @param string $url_sql_query the analyzed sql query
  2851. *
  2852. * @return array 5 element array - $edit_url, $copy_url,
  2853. * $edit_str, $copy_str, $edit_anchor_class
  2854. *
  2855. * @access private
  2856. *
  2857. * @see _getTableBody()
  2858. */
  2859. private function _getModifiedLinks(
  2860. $where_clause, $clause_is_unique, $url_sql_query
  2861. ) {
  2862. $_url_params = array(
  2863. 'db' => $this->__get('db'),
  2864. 'table' => $this->__get('table'),
  2865. 'where_clause' => $where_clause,
  2866. 'clause_is_unique' => $clause_is_unique,
  2867. 'sql_query' => $url_sql_query,
  2868. 'goto' => 'sql.php',
  2869. );
  2870. $edit_url = 'tbl_change.php'
  2871. . Url::getCommon(
  2872. $_url_params + array('default_action' => 'update')
  2873. );
  2874. $copy_url = 'tbl_change.php'
  2875. . Url::getCommon(
  2876. $_url_params + array('default_action' => 'insert')
  2877. );
  2878. $edit_str = $this->_getActionLinkContent(
  2879. 'b_edit', __('Edit')
  2880. );
  2881. $copy_str = $this->_getActionLinkContent(
  2882. 'b_insrow', __('Copy')
  2883. );
  2884. // Class definitions required for grid editing jQuery scripts
  2885. $edit_anchor_class = "edit_row_anchor";
  2886. if ($clause_is_unique == 0) {
  2887. $edit_anchor_class .= ' nonunique';
  2888. }
  2889. return array($edit_url, $copy_url, $edit_str, $copy_str, $edit_anchor_class);
  2890. } // end of the '_getModifiedLinks()' function
  2891. /**
  2892. * Get delete and kill links
  2893. *
  2894. * @param string $where_clause the where clause of the sql
  2895. * @param boolean $clause_is_unique the unique condition of clause
  2896. * @param string $url_sql_query the analyzed sql query
  2897. * @param string $del_lnk the delete link of current row
  2898. * @param array $row the current row
  2899. *
  2900. * @return array 3 element array
  2901. * $del_url, $del_str, $js_conf
  2902. *
  2903. * @access private
  2904. *
  2905. * @see _getTableBody()
  2906. */
  2907. private function _getDeleteAndKillLinks(
  2908. $where_clause, $clause_is_unique, $url_sql_query, $del_lnk, array $row
  2909. ) {
  2910. $goto = $this->__get('goto');
  2911. if ($del_lnk == self::DELETE_ROW) { // delete row case
  2912. $_url_params = array(
  2913. 'db' => $this->__get('db'),
  2914. 'table' => $this->__get('table'),
  2915. 'sql_query' => $url_sql_query,
  2916. 'message_to_show' => __('The row has been deleted.'),
  2917. 'goto' => (empty($goto) ? 'tbl_sql.php' : $goto),
  2918. );
  2919. $lnk_goto = 'sql.php' . Url::getCommonRaw($_url_params);
  2920. $del_query = 'DELETE FROM '
  2921. . Util::backquote($this->__get('table'))
  2922. . ' WHERE ' . $where_clause .
  2923. ($clause_is_unique ? '' : ' LIMIT 1');
  2924. $_url_params = array(
  2925. 'db' => $this->__get('db'),
  2926. 'table' => $this->__get('table'),
  2927. 'sql_query' => $del_query,
  2928. 'message_to_show' => __('The row has been deleted.'),
  2929. 'goto' => $lnk_goto,
  2930. );
  2931. $del_url = 'sql.php' . Url::getCommon($_url_params);
  2932. $js_conf = 'DELETE FROM ' . Sanitize::jsFormat($this->__get('table'))
  2933. . ' WHERE ' . Sanitize::jsFormat($where_clause, false)
  2934. . ($clause_is_unique ? '' : ' LIMIT 1');
  2935. $del_str = $this->_getActionLinkContent('b_drop', __('Delete'));
  2936. } elseif ($del_lnk == self::KILL_PROCESS) { // kill process case
  2937. $_url_params = array(
  2938. 'db' => $this->__get('db'),
  2939. 'table' => $this->__get('table'),
  2940. 'sql_query' => $url_sql_query,
  2941. 'goto' => 'index.php',
  2942. );
  2943. $lnk_goto = 'sql.php' . Url::getCommonRaw($_url_params);
  2944. $kill = $GLOBALS['dbi']->getKillQuery($row[0]);
  2945. $_url_params = array(
  2946. 'db' => 'mysql',
  2947. 'sql_query' => $kill,
  2948. 'goto' => $lnk_goto,
  2949. );
  2950. $del_url = 'sql.php' . Url::getCommon($_url_params);
  2951. $js_conf = $kill;
  2952. $del_str = Util::getIcon(
  2953. 'b_drop', __('Kill')
  2954. );
  2955. } else {
  2956. $del_url = $del_str = $js_conf = null;
  2957. }
  2958. return array($del_url, $del_str, $js_conf);
  2959. } // end of the '_getDeleteAndKillLinks()' function
  2960. /**
  2961. * Get content inside the table row action links (Edit/Copy/Delete)
  2962. *
  2963. * @param string $icon The name of the file to get
  2964. * @param string $display_text The text displaying after the image icon
  2965. *
  2966. * @return string
  2967. *
  2968. * @access private
  2969. *
  2970. * @see _getModifiedLinks(), _getDeleteAndKillLinks()
  2971. */
  2972. private function _getActionLinkContent($icon, $display_text)
  2973. {
  2974. $linkContent = '';
  2975. if (isset($GLOBALS['cfg']['RowActionType'])
  2976. && $GLOBALS['cfg']['RowActionType'] == self::ACTION_LINK_CONTENT_ICONS
  2977. ) {
  2978. $linkContent .= '<span class="nowrap">'
  2979. . Util::getImage(
  2980. $icon, $display_text
  2981. )
  2982. . '</span>';
  2983. } elseif (isset($GLOBALS['cfg']['RowActionType'])
  2984. && $GLOBALS['cfg']['RowActionType'] == self::ACTION_LINK_CONTENT_TEXT
  2985. ) {
  2986. $linkContent .= '<span class="nowrap">' . $display_text . '</span>';
  2987. } else {
  2988. $linkContent .= Util::getIcon(
  2989. $icon, $display_text
  2990. );
  2991. }
  2992. return $linkContent;
  2993. }
  2994. /**
  2995. * Prepare placed links
  2996. *
  2997. * @param string $dir the direction of links should place
  2998. * @param string $del_url the url for delete row
  2999. * @param array $displayParts which elements to display
  3000. * @param integer $row_no the index of current row
  3001. * @param string $where_clause the where clause of the sql
  3002. * @param string $where_clause_html the html encoded where clause
  3003. * @param array $condition_array array of keys (primary, unique, condition)
  3004. * @param string $edit_url the url for edit row
  3005. * @param string $copy_url the url for copy row
  3006. * @param string $edit_anchor_class the class for html element for edit
  3007. * @param string $edit_str the label for edit row
  3008. * @param string $copy_str the label for copy row
  3009. * @param string $del_str the label for delete row
  3010. * @param string $js_conf text for the JS confirmation
  3011. *
  3012. * @return string html content
  3013. *
  3014. * @access private
  3015. *
  3016. * @see _getTableBody()
  3017. */
  3018. private function _getPlacedLinks(
  3019. $dir, $del_url, array $displayParts, $row_no, $where_clause, $where_clause_html,
  3020. array $condition_array, $edit_url, $copy_url,
  3021. $edit_anchor_class, $edit_str, $copy_str, $del_str, $js_conf
  3022. ) {
  3023. if (! isset($js_conf)) {
  3024. $js_conf = '';
  3025. }
  3026. return $this->_getCheckboxAndLinks(
  3027. $dir, $del_url, $displayParts,
  3028. $row_no, $where_clause, $where_clause_html, $condition_array,
  3029. $edit_url, $copy_url, $edit_anchor_class,
  3030. $edit_str, $copy_str, $del_str, $js_conf
  3031. );
  3032. } // end of the '_getPlacedLinks()' function
  3033. /**
  3034. * Get the combined classes for a column
  3035. *
  3036. * @param string $grid_edit_class the class for all editable columns
  3037. * @param string $not_null_class the class for not null columns
  3038. * @param string $relation_class the class for relations in a column
  3039. * @param string $hide_class the class for visibility of a column
  3040. * @param string $field_type_class the class related to type of the field
  3041. *
  3042. * @return string $class the combined classes
  3043. *
  3044. * @access private
  3045. *
  3046. * @see _getTableBody()
  3047. */
  3048. private function _getClassesForColumn(
  3049. $grid_edit_class, $not_null_class, $relation_class,
  3050. $hide_class, $field_type_class
  3051. ) {
  3052. $class = 'data ' . $grid_edit_class . ' ' . $not_null_class . ' '
  3053. . $relation_class . ' ' . $hide_class . ' ' . $field_type_class;
  3054. return $class;
  3055. } // end of the '_getClassesForColumn()' function
  3056. /**
  3057. * Get class for datetime related fields
  3058. *
  3059. * @param string $type the type of the column field
  3060. *
  3061. * @return string $field_type_class the class for the column
  3062. *
  3063. * @access private
  3064. *
  3065. * @see _getTableBody()
  3066. */
  3067. private function _getClassForDateTimeRelatedFields($type)
  3068. {
  3069. if ((substr($type, 0, 9) == self::TIMESTAMP_FIELD)
  3070. || ($type == self::DATETIME_FIELD)
  3071. ) {
  3072. $field_type_class = 'datetimefield';
  3073. } elseif ($type == self::DATE_FIELD) {
  3074. $field_type_class = 'datefield';
  3075. } elseif ($type == self::TIME_FIELD) {
  3076. $field_type_class = 'timefield';
  3077. } elseif ($type == self::STRING_FIELD) {
  3078. $field_type_class = 'text';
  3079. } else {
  3080. $field_type_class = '';
  3081. }
  3082. return $field_type_class;
  3083. } // end of the '_getClassForDateTimeRelatedFields()' function
  3084. /**
  3085. * Prepare data cell for numeric type fields
  3086. *
  3087. * @param string $column the column's value
  3088. * @param string $class the html class for column
  3089. * @param boolean $condition_field the column should highlighted
  3090. * or not
  3091. * @param object $meta the meta-information about this
  3092. * field
  3093. * @param array $map the list of relations
  3094. * @param boolean $is_field_truncated the condition for blob data
  3095. * replacements
  3096. * @param array $analyzed_sql_results the analyzed query
  3097. * @param object|string $transformation_plugin the name of transformation plugin
  3098. * @param string $default_function the default transformation
  3099. * function
  3100. * @param array $transform_options the transformation parameters
  3101. *
  3102. * @return string $cell the prepared cell, html content
  3103. *
  3104. * @access private
  3105. *
  3106. * @see _getTableBody()
  3107. */
  3108. private function _getDataCellForNumericColumns(
  3109. $column, $class, $condition_field, $meta, array $map, $is_field_truncated,
  3110. array $analyzed_sql_results, $transformation_plugin, $default_function,
  3111. array $transform_options
  3112. ) {
  3113. if (! isset($column) || is_null($column)) {
  3114. $cell = $this->_buildNullDisplay(
  3115. 'right ' . $class, $condition_field, $meta, ''
  3116. );
  3117. } elseif ($column != '') {
  3118. $nowrap = ' nowrap';
  3119. $where_comparison = ' = ' . $column;
  3120. $cell = $this->_getRowData(
  3121. 'right ' . $class, $condition_field,
  3122. $analyzed_sql_results, $meta, $map, $column,
  3123. $transformation_plugin, $default_function, $nowrap,
  3124. $where_comparison, $transform_options,
  3125. $is_field_truncated, ''
  3126. );
  3127. } else {
  3128. $cell = $this->_buildEmptyDisplay(
  3129. 'right ' . $class, $condition_field, $meta, ''
  3130. );
  3131. }
  3132. return $cell;
  3133. } // end of the '_getDataCellForNumericColumns()' function
  3134. /**
  3135. * Get data cell for geometry type fields
  3136. *
  3137. * @param string $column the relevant column in data row
  3138. * @param string $class the html class for column
  3139. * @param object $meta the meta-information about
  3140. * this field
  3141. * @param array $map the list of relations
  3142. * @param array $_url_params the parameters for generate url
  3143. * @param boolean $condition_field the column should highlighted
  3144. * or not
  3145. * @param object|string $transformation_plugin the name of transformation
  3146. * function
  3147. * @param string $default_function the default transformation
  3148. * function
  3149. * @param string $transform_options the transformation parameters
  3150. * @param array $analyzed_sql_results the analyzed query
  3151. *
  3152. * @return string $cell the prepared data cell, html content
  3153. *
  3154. * @access private
  3155. *
  3156. * @see _getTableBody()
  3157. */
  3158. private function _getDataCellForGeometryColumns(
  3159. $column, $class, $meta, array $map, array $_url_params, $condition_field,
  3160. $transformation_plugin, $default_function, $transform_options,
  3161. array $analyzed_sql_results
  3162. ) {
  3163. if (! isset($column) || is_null($column)) {
  3164. $cell = $this->_buildNullDisplay($class, $condition_field, $meta);
  3165. return $cell;
  3166. }
  3167. if ($column == '') {
  3168. $cell = $this->_buildEmptyDisplay($class, $condition_field, $meta);
  3169. return $cell;
  3170. }
  3171. // Display as [GEOMETRY - (size)]
  3172. if ($_SESSION['tmpval']['geoOption'] == self::GEOMETRY_DISP_GEOM) {
  3173. $geometry_text = $this->_handleNonPrintableContents(
  3174. strtoupper(self::GEOMETRY_FIELD), $column, $transformation_plugin,
  3175. $transform_options, $default_function, $meta, $_url_params
  3176. );
  3177. $cell = $this->_buildValueDisplay(
  3178. $class, $condition_field, $geometry_text
  3179. );
  3180. return $cell;
  3181. }
  3182. if ($_SESSION['tmpval']['geoOption'] == self::GEOMETRY_DISP_WKT) {
  3183. // Prepare in Well Known Text(WKT) format.
  3184. $where_comparison = ' = ' . $column;
  3185. // Convert to WKT format
  3186. $wktval = Util::asWKT($column);
  3187. list(
  3188. $is_field_truncated,
  3189. $wktval,
  3190. // skip 3rd param
  3191. ) = $this->_getPartialText($wktval);
  3192. $cell = $this->_getRowData(
  3193. $class, $condition_field, $analyzed_sql_results, $meta, $map,
  3194. $wktval, $transformation_plugin, $default_function, '',
  3195. $where_comparison, $transform_options,
  3196. $is_field_truncated, ''
  3197. );
  3198. return $cell;
  3199. }
  3200. // Prepare in Well Known Binary (WKB) format.
  3201. if ($_SESSION['tmpval']['display_binary']) {
  3202. $where_comparison = ' = ' . $column;
  3203. $wkbval = substr(bin2hex($column), 8);
  3204. list(
  3205. $is_field_truncated,
  3206. $wkbval,
  3207. // skip 3rd param
  3208. ) = $this->_getPartialText($wkbval);
  3209. $cell = $this->_getRowData(
  3210. $class, $condition_field,
  3211. $analyzed_sql_results, $meta, $map, $wkbval,
  3212. $transformation_plugin, $default_function, '',
  3213. $where_comparison, $transform_options,
  3214. $is_field_truncated, ''
  3215. );
  3216. return $cell;
  3217. }
  3218. $wkbval = $this->_handleNonPrintableContents(
  3219. self::BINARY_FIELD, $column, $transformation_plugin,
  3220. $transform_options, $default_function, $meta,
  3221. $_url_params
  3222. );
  3223. $cell = $this->_buildValueDisplay(
  3224. $class, $condition_field, $wkbval
  3225. );
  3226. return $cell;
  3227. } // end of the '_getDataCellForGeometryColumns()' function
  3228. /**
  3229. * Get data cell for non numeric type fields
  3230. *
  3231. * @param string $column the relevant column in data row
  3232. * @param string $class the html class for column
  3233. * @param object $meta the meta-information about
  3234. * the field
  3235. * @param array $map the list of relations
  3236. * @param array $_url_params the parameters for generate
  3237. * url
  3238. * @param boolean $condition_field the column should highlighted
  3239. * or not
  3240. * @param object|string $transformation_plugin the name of transformation
  3241. * function
  3242. * @param string $default_function the default transformation
  3243. * function
  3244. * @param string $transform_options the transformation parameters
  3245. * @param boolean $is_field_truncated is data truncated due to
  3246. * LimitChars
  3247. * @param array $analyzed_sql_results the analyzed query
  3248. * @param integer &$dt_result the link id associated to
  3249. * the query which results
  3250. * have to be displayed
  3251. * @param integer $col_index the column index
  3252. *
  3253. * @return string $cell the prepared data cell, html content
  3254. *
  3255. * @access private
  3256. *
  3257. * @see _getTableBody()
  3258. */
  3259. private function _getDataCellForNonNumericColumns(
  3260. $column, $class, $meta, array $map, array $_url_params, $condition_field,
  3261. $transformation_plugin, $default_function, $transform_options,
  3262. $is_field_truncated, array $analyzed_sql_results, &$dt_result, $col_index
  3263. ) {
  3264. $original_length = 0;
  3265. $is_analyse = $this->__get('is_analyse');
  3266. $field_flags = $GLOBALS['dbi']->fieldFlags($dt_result, $col_index);
  3267. $bIsText = gettype($transformation_plugin) === 'object'
  3268. && strpos($transformation_plugin->getMIMEtype(), 'Text')
  3269. === false;
  3270. // disable inline grid editing
  3271. // if binary fields are protected
  3272. // or transformation plugin is of non text type
  3273. // such as image
  3274. if ((stristr($field_flags, self::BINARY_FIELD)
  3275. && ($GLOBALS['cfg']['ProtectBinary'] === 'all'
  3276. || ($GLOBALS['cfg']['ProtectBinary'] === 'noblob'
  3277. && !stristr($meta->type, self::BLOB_FIELD))
  3278. || ($GLOBALS['cfg']['ProtectBinary'] === 'blob'
  3279. && stristr($meta->type, self::BLOB_FIELD))))
  3280. || $bIsText
  3281. ) {
  3282. $class = str_replace('grid_edit', '', $class);
  3283. }
  3284. if (! isset($column) || is_null($column)) {
  3285. $cell = $this->_buildNullDisplay($class, $condition_field, $meta);
  3286. return $cell;
  3287. }
  3288. if ($column == '') {
  3289. $cell = $this->_buildEmptyDisplay($class, $condition_field, $meta);
  3290. return $cell;
  3291. }
  3292. // Cut all fields to $GLOBALS['cfg']['LimitChars']
  3293. // (unless it's a link-type transformation or binary)
  3294. if (!(gettype($transformation_plugin) === "object"
  3295. && strpos($transformation_plugin->getName(), 'Link') !== false)
  3296. && !stristr($field_flags, self::BINARY_FIELD)
  3297. ) {
  3298. list(
  3299. $is_field_truncated,
  3300. $column,
  3301. $original_length
  3302. ) = $this->_getPartialText($column);
  3303. }
  3304. $formatted = false;
  3305. if (isset($meta->_type) && $meta->_type === MYSQLI_TYPE_BIT) {
  3306. $column = Util::printableBitValue(
  3307. $column, $meta->length
  3308. );
  3309. // some results of PROCEDURE ANALYSE() are reported as
  3310. // being BINARY but they are quite readable,
  3311. // so don't treat them as BINARY
  3312. } elseif (stristr($field_flags, self::BINARY_FIELD)
  3313. && !(isset($is_analyse) && $is_analyse)
  3314. ) {
  3315. // we show the BINARY or BLOB message and field's size
  3316. // (or maybe use a transformation)
  3317. $binary_or_blob = self::BLOB_FIELD;
  3318. if ($meta->type === self::STRING_FIELD) {
  3319. $binary_or_blob = self::BINARY_FIELD;
  3320. }
  3321. $column = $this->_handleNonPrintableContents(
  3322. $binary_or_blob, $column, $transformation_plugin,
  3323. $transform_options, $default_function,
  3324. $meta, $_url_params, $is_field_truncated
  3325. );
  3326. $class = $this->_addClass(
  3327. $class, $condition_field, $meta, '',
  3328. $is_field_truncated, $transformation_plugin, $default_function
  3329. );
  3330. $result = strip_tags($column);
  3331. // disable inline grid editing
  3332. // if binary or blob data is not shown
  3333. if (stristr($result, $binary_or_blob)) {
  3334. $class = str_replace('grid_edit', '', $class);
  3335. }
  3336. $formatted = true;
  3337. }
  3338. if ($formatted) {
  3339. $cell = $this->_buildValueDisplay(
  3340. $class, $condition_field, $column
  3341. );
  3342. return $cell;
  3343. }
  3344. // transform functions may enable no-wrapping:
  3345. $function_nowrap = 'applyTransformationNoWrap';
  3346. $bool_nowrap = (($default_function != $transformation_plugin)
  3347. && function_exists($transformation_plugin->$function_nowrap()))
  3348. ? $transformation_plugin->$function_nowrap($transform_options)
  3349. : false;
  3350. // do not wrap if date field type
  3351. $nowrap = (preg_match('@DATE|TIME@i', $meta->type)
  3352. || $bool_nowrap) ? ' nowrap' : '';
  3353. $where_comparison = ' = \''
  3354. . $GLOBALS['dbi']->escapeString($column)
  3355. . '\'';
  3356. $cell = $this->_getRowData(
  3357. $class, $condition_field,
  3358. $analyzed_sql_results, $meta, $map, $column,
  3359. $transformation_plugin, $default_function, $nowrap,
  3360. $where_comparison, $transform_options,
  3361. $is_field_truncated, $original_length
  3362. );
  3363. return $cell;
  3364. } // end of the '_getDataCellForNonNumericColumns()' function
  3365. /**
  3366. * Checks the posted options for viewing query results
  3367. * and sets appropriate values in the session.
  3368. *
  3369. * @todo make maximum remembered queries configurable
  3370. * @todo move/split into SQL class!?
  3371. * @todo currently this is called twice unnecessary
  3372. * @todo ignore LIMIT and ORDER in query!?
  3373. *
  3374. * @return void
  3375. *
  3376. * @access public
  3377. *
  3378. * @see sql.php file
  3379. */
  3380. public function setConfigParamsForDisplayTable()
  3381. {
  3382. $sql_md5 = md5($this->__get('sql_query'));
  3383. $query = array();
  3384. if (isset($_SESSION['tmpval']['query'][$sql_md5])) {
  3385. $query = $_SESSION['tmpval']['query'][$sql_md5];
  3386. }
  3387. $query['sql'] = $this->__get('sql_query');
  3388. if (empty($query['repeat_cells'])) {
  3389. $query['repeat_cells'] = $GLOBALS['cfg']['RepeatCells'];
  3390. }
  3391. // as this is a form value, the type is always string so we cannot
  3392. // use Core::isValid($_POST['session_max_rows'], 'integer')
  3393. if (Core::isValid($_POST['session_max_rows'], 'numeric')) {
  3394. $query['max_rows'] = (int)$_POST['session_max_rows'];
  3395. unset($_POST['session_max_rows']);
  3396. } elseif ($_POST['session_max_rows'] == self::ALL_ROWS) {
  3397. $query['max_rows'] = self::ALL_ROWS;
  3398. unset($_POST['session_max_rows']);
  3399. } elseif (empty($query['max_rows'])) {
  3400. $query['max_rows'] = intval($GLOBALS['cfg']['MaxRows']);
  3401. }
  3402. if (Core::isValid($_REQUEST['pos'], 'numeric')) {
  3403. $query['pos'] = $_REQUEST['pos'];
  3404. unset($_REQUEST['pos']);
  3405. } elseif (empty($query['pos'])) {
  3406. $query['pos'] = 0;
  3407. }
  3408. if (Core::isValid(
  3409. $_REQUEST['pftext'],
  3410. array(
  3411. self::DISPLAY_PARTIAL_TEXT, self::DISPLAY_FULL_TEXT
  3412. )
  3413. )
  3414. ) {
  3415. $query['pftext'] = $_REQUEST['pftext'];
  3416. unset($_REQUEST['pftext']);
  3417. } elseif (empty($query['pftext'])) {
  3418. $query['pftext'] = self::DISPLAY_PARTIAL_TEXT;
  3419. }
  3420. if (Core::isValid(
  3421. $_REQUEST['relational_display'],
  3422. array(
  3423. self::RELATIONAL_KEY, self::RELATIONAL_DISPLAY_COLUMN
  3424. )
  3425. )
  3426. ) {
  3427. $query['relational_display'] = $_REQUEST['relational_display'];
  3428. unset($_REQUEST['relational_display']);
  3429. } elseif (empty($query['relational_display'])) {
  3430. // The current session value has priority over a
  3431. // change via Settings; this change will be apparent
  3432. // starting from the next session
  3433. $query['relational_display'] = $GLOBALS['cfg']['RelationalDisplay'];
  3434. }
  3435. if (Core::isValid(
  3436. $_REQUEST['geoOption'],
  3437. array(
  3438. self::GEOMETRY_DISP_WKT, self::GEOMETRY_DISP_WKB,
  3439. self::GEOMETRY_DISP_GEOM
  3440. )
  3441. )
  3442. ) {
  3443. $query['geoOption'] = $_REQUEST['geoOption'];
  3444. unset($_REQUEST['geoOption']);
  3445. } elseif (empty($query['geoOption'])) {
  3446. $query['geoOption'] = self::GEOMETRY_DISP_GEOM;
  3447. }
  3448. if (isset($_REQUEST['display_binary'])) {
  3449. $query['display_binary'] = true;
  3450. unset($_REQUEST['display_binary']);
  3451. } elseif (isset($_REQUEST['display_options_form'])) {
  3452. // we know that the checkbox was unchecked
  3453. unset($query['display_binary']);
  3454. } elseif (isset($_REQUEST['full_text_button'])) {
  3455. // do nothing to keep the value that is there in the session
  3456. } else {
  3457. // selected by default because some operations like OPTIMIZE TABLE
  3458. // and all queries involving functions return "binary" contents,
  3459. // according to low-level field flags
  3460. $query['display_binary'] = true;
  3461. }
  3462. if (isset($_REQUEST['display_blob'])) {
  3463. $query['display_blob'] = true;
  3464. unset($_REQUEST['display_blob']);
  3465. } elseif (isset($_REQUEST['display_options_form'])) {
  3466. // we know that the checkbox was unchecked
  3467. unset($query['display_blob']);
  3468. }
  3469. if (isset($_REQUEST['hide_transformation'])) {
  3470. $query['hide_transformation'] = true;
  3471. unset($_REQUEST['hide_transformation']);
  3472. } elseif (isset($_REQUEST['display_options_form'])) {
  3473. // we know that the checkbox was unchecked
  3474. unset($query['hide_transformation']);
  3475. }
  3476. // move current query to the last position, to be removed last
  3477. // so only least executed query will be removed if maximum remembered
  3478. // queries limit is reached
  3479. unset($_SESSION['tmpval']['query'][$sql_md5]);
  3480. $_SESSION['tmpval']['query'][$sql_md5] = $query;
  3481. // do not exceed a maximum number of queries to remember
  3482. if (count($_SESSION['tmpval']['query']) > 10) {
  3483. array_shift($_SESSION['tmpval']['query']);
  3484. //echo 'deleting one element ...';
  3485. }
  3486. // populate query configuration
  3487. $_SESSION['tmpval']['pftext']
  3488. = $query['pftext'];
  3489. $_SESSION['tmpval']['relational_display']
  3490. = $query['relational_display'];
  3491. $_SESSION['tmpval']['geoOption']
  3492. = $query['geoOption'];
  3493. $_SESSION['tmpval']['display_binary'] = isset(
  3494. $query['display_binary']
  3495. );
  3496. $_SESSION['tmpval']['display_blob'] = isset(
  3497. $query['display_blob']
  3498. );
  3499. $_SESSION['tmpval']['hide_transformation'] = isset(
  3500. $query['hide_transformation']
  3501. );
  3502. $_SESSION['tmpval']['pos']
  3503. = $query['pos'];
  3504. $_SESSION['tmpval']['max_rows']
  3505. = $query['max_rows'];
  3506. $_SESSION['tmpval']['repeat_cells']
  3507. = $query['repeat_cells'];
  3508. }
  3509. /**
  3510. * Prepare a table of results returned by a SQL query.
  3511. *
  3512. * @param integer &$dt_result the link id associated to the query
  3513. * which results have to be displayed
  3514. * @param array &$displayParts the parts to display
  3515. * @param array $analyzed_sql_results analyzed sql results
  3516. * @param boolean $is_limited_display With limited operations or not
  3517. *
  3518. * @return string $table_html Generated HTML content for resulted table
  3519. *
  3520. * @access public
  3521. *
  3522. * @see sql.php file
  3523. */
  3524. public function getTable(
  3525. &$dt_result, array &$displayParts, array $analyzed_sql_results,
  3526. $is_limited_display = false
  3527. ) {
  3528. /**
  3529. * The statement this table is built for.
  3530. * @var \PhpMyAdmin\SqlParser\Statements\SelectStatement
  3531. */
  3532. if (isset($analyzed_sql_results['statement'])) {
  3533. $statement = $analyzed_sql_results['statement'];
  3534. } else {
  3535. $statement = null;
  3536. }
  3537. $table_html = '';
  3538. // Following variable are needed for use in isset/empty or
  3539. // use with array indexes/safe use in foreach
  3540. $fields_meta = $this->__get('fields_meta');
  3541. $showtable = $this->__get('showtable');
  3542. $printview = $this->__get('printview');
  3543. /**
  3544. * @todo move this to a central place
  3545. * @todo for other future table types
  3546. */
  3547. $is_innodb = (isset($showtable['Type'])
  3548. && $showtable['Type'] == self::TABLE_TYPE_INNO_DB);
  3549. $sql = new Sql();
  3550. if ($is_innodb && $sql->isJustBrowsing($analyzed_sql_results, true)) {
  3551. // "j u s t b r o w s i n g"
  3552. $pre_count = '~';
  3553. $after_count = Util::showHint(
  3554. Sanitize::sanitize(
  3555. __('May be approximate. See [doc@faq3-11]FAQ 3.11[/doc].')
  3556. )
  3557. );
  3558. } else {
  3559. $pre_count = '';
  3560. $after_count = '';
  3561. }
  3562. // 1. ----- Prepares the work -----
  3563. // 1.1 Gets the information about which functionalities should be
  3564. // displayed
  3565. list(
  3566. $displayParts,
  3567. $total
  3568. ) = $this->_setDisplayPartsAndTotal($displayParts);
  3569. // 1.2 Defines offsets for the next and previous pages
  3570. if ($displayParts['nav_bar'] == '1') {
  3571. list($pos_next, $pos_prev) = $this->_getOffsets();
  3572. } // end if
  3573. // 1.3 Extract sorting expressions.
  3574. // we need $sort_expression and $sort_expression_nodirection
  3575. // even if there are many table references
  3576. $sort_expression = array();
  3577. $sort_expression_nodirection = array();
  3578. $sort_direction = array();
  3579. if (!is_null($statement) && !empty($statement->order)) {
  3580. foreach ($statement->order as $o) {
  3581. $sort_expression[] = $o->expr->expr . ' ' . $o->type;
  3582. $sort_expression_nodirection[] = $o->expr->expr;
  3583. $sort_direction[] = $o->type;
  3584. }
  3585. } else {
  3586. $sort_expression[] = '';
  3587. $sort_expression_nodirection[] = '';
  3588. $sort_direction[] = '';
  3589. }
  3590. $number_of_columns = count($sort_expression_nodirection);
  3591. // 1.4 Prepares display of first and last value of the sorted column
  3592. $sorted_column_message = '';
  3593. for ( $i = 0; $i < $number_of_columns; $i++ ) {
  3594. $sorted_column_message .= $this->_getSortedColumnMessage(
  3595. $dt_result, $sort_expression_nodirection[$i]
  3596. );
  3597. }
  3598. // 2. ----- Prepare to display the top of the page -----
  3599. // 2.1 Prepares a messages with position information
  3600. if (($displayParts['nav_bar'] == '1') && isset($pos_next)) {
  3601. $message = $this->_setMessageInformation(
  3602. $sorted_column_message,
  3603. $analyzed_sql_results,
  3604. $total,
  3605. $pos_next,
  3606. $pre_count,
  3607. $after_count
  3608. );
  3609. $table_html .= Util::getMessage(
  3610. $message, $this->__get('sql_query'), 'success'
  3611. );
  3612. } elseif ((!isset($printview) || ($printview != '1')) && !$is_limited_display) {
  3613. $table_html .= Util::getMessage(
  3614. __('Your SQL query has been executed successfully.'),
  3615. $this->__get('sql_query'), 'success'
  3616. );
  3617. }
  3618. // 2.3 Prepare the navigation bars
  3619. if (strlen($this->__get('table')) === 0) {
  3620. if ($analyzed_sql_results['querytype'] == 'SELECT') {
  3621. // table does not always contain a real table name,
  3622. // for example in MySQL 5.0.x, the query SHOW STATUS
  3623. // returns STATUS as a table name
  3624. $this->__set('table', $fields_meta[0]->table);
  3625. } else {
  3626. $this->__set('table', '');
  3627. }
  3628. }
  3629. // can the result be sorted?
  3630. if ($displayParts['sort_lnk'] == '1' && ! is_null($analyzed_sql_results['statement'])) {
  3631. // At this point, $sort_expression is an array
  3632. list($unsorted_sql_query, $sort_by_key_html)
  3633. = $this->_getUnsortedSqlAndSortByKeyDropDown(
  3634. $analyzed_sql_results, $sort_expression
  3635. );
  3636. } else {
  3637. $sort_by_key_html = $unsorted_sql_query = '';
  3638. }
  3639. if (($displayParts['nav_bar'] == '1') && !is_null($statement) && (empty($statement->limit))) {
  3640. $table_html .= $this->_getPlacedTableNavigations(
  3641. $pos_next, $pos_prev, self::PLACE_TOP_DIRECTION_DROPDOWN,
  3642. $is_innodb, $sort_by_key_html
  3643. );
  3644. }
  3645. // 2b ----- Get field references from Database -----
  3646. // (see the 'relation' configuration variable)
  3647. // initialize map
  3648. $map = array();
  3649. $target = array();
  3650. if (!is_null($statement) && !empty($statement->from)) {
  3651. foreach ($statement->from as $field) {
  3652. if (!empty($field->table)) {
  3653. $target[] = $field->table;
  3654. }
  3655. }
  3656. }
  3657. if (strlen($this->__get('table')) > 0) {
  3658. // This method set the values for $map array
  3659. $this->_setParamForLinkForeignKeyRelatedTables($map);
  3660. // Coming from 'Distinct values' action of structure page
  3661. // We manipulate relations mechanism to show a link to related rows.
  3662. if ($this->__get('is_browse_distinct')) {
  3663. $map[$fields_meta[1]->name] = array(
  3664. $this->__get('table'),
  3665. $fields_meta[1]->name,
  3666. '',
  3667. $this->__get('db')
  3668. );
  3669. }
  3670. } // end if
  3671. // end 2b
  3672. // 3. ----- Prepare the results table -----
  3673. if ($is_limited_display) {
  3674. $table_html .= "<br>";
  3675. }
  3676. $table_html .= $this->_getTableHeaders(
  3677. $displayParts,
  3678. $analyzed_sql_results,
  3679. $unsorted_sql_query,
  3680. $sort_expression,
  3681. $sort_expression_nodirection,
  3682. $sort_direction,
  3683. $is_limited_display
  3684. );
  3685. $table_html .= '<tbody>' . "\n";
  3686. $table_html .= $this->_getTableBody(
  3687. $dt_result,
  3688. $displayParts,
  3689. $map,
  3690. $analyzed_sql_results,
  3691. $is_limited_display
  3692. );
  3693. $this->__set('display_params', null);
  3694. $table_html .= '</tbody>' . "\n" . '</table></div>';
  3695. // 4. ----- Prepares the link for multi-fields edit and delete
  3696. if ($displayParts['del_lnk'] == self::DELETE_ROW
  3697. && $displayParts['del_lnk'] != self::KILL_PROCESS
  3698. ) {
  3699. $table_html .= $this->_getMultiRowOperationLinks(
  3700. $dt_result,
  3701. $analyzed_sql_results,
  3702. $displayParts['del_lnk']
  3703. );
  3704. }
  3705. // 5. ----- Get the navigation bar at the bottom if required -----
  3706. if (($displayParts['nav_bar'] == '1') && !is_null($statement) && empty($statement->limit)) {
  3707. $table_html .= $this->_getPlacedTableNavigations(
  3708. $pos_next, $pos_prev, self::PLACE_BOTTOM_DIRECTION_DROPDOWN,
  3709. $is_innodb, $sort_by_key_html
  3710. );
  3711. } elseif (! isset($printview) || ($printview != '1')) {
  3712. $table_html .= "\n" . '<br /><br />' . "\n";
  3713. }
  3714. // 6. ----- Prepare "Query results operations"
  3715. if ((! isset($printview) || ($printview != '1')) && ! $is_limited_display) {
  3716. $table_html .= $this->_getResultsOperations(
  3717. $displayParts, $analyzed_sql_results
  3718. );
  3719. }
  3720. return $table_html;
  3721. } // end of the 'getTable()' function
  3722. /**
  3723. * Get offsets for next page and previous page
  3724. *
  3725. * @return array array with two elements - $pos_next, $pos_prev
  3726. *
  3727. * @access private
  3728. *
  3729. * @see getTable()
  3730. */
  3731. private function _getOffsets()
  3732. {
  3733. if ($_SESSION['tmpval']['max_rows'] == self::ALL_ROWS) {
  3734. $pos_next = 0;
  3735. $pos_prev = 0;
  3736. } else {
  3737. $pos_next = $_SESSION['tmpval']['pos']
  3738. + $_SESSION['tmpval']['max_rows'];
  3739. $pos_prev = $_SESSION['tmpval']['pos']
  3740. - $_SESSION['tmpval']['max_rows'];
  3741. if ($pos_prev < 0) {
  3742. $pos_prev = 0;
  3743. }
  3744. }
  3745. return array($pos_next, $pos_prev);
  3746. } // end of the '_getOffsets()' function
  3747. /**
  3748. * Prepare sorted column message
  3749. *
  3750. * @param integer &$dt_result the link id associated to the
  3751. * query which results have to
  3752. * be displayed
  3753. * @param string $sort_expression_nodirection sort expression without direction
  3754. *
  3755. * @return string html content
  3756. * null if not found sorted column
  3757. *
  3758. * @access private
  3759. *
  3760. * @see getTable()
  3761. */
  3762. private function _getSortedColumnMessage(
  3763. &$dt_result, $sort_expression_nodirection
  3764. ) {
  3765. $fields_meta = $this->__get('fields_meta'); // To use array indexes
  3766. if (empty($sort_expression_nodirection)) {
  3767. return null;
  3768. }
  3769. if (mb_strpos($sort_expression_nodirection, '.') === false) {
  3770. $sort_table = $this->__get('table');
  3771. $sort_column = $sort_expression_nodirection;
  3772. } else {
  3773. list($sort_table, $sort_column)
  3774. = explode('.', $sort_expression_nodirection);
  3775. }
  3776. $sort_table = Util::unQuote($sort_table);
  3777. $sort_column = Util::unQuote($sort_column);
  3778. // find the sorted column index in row result
  3779. // (this might be a multi-table query)
  3780. $sorted_column_index = false;
  3781. foreach ($fields_meta as $key => $meta) {
  3782. if (($meta->table == $sort_table) && ($meta->name == $sort_column)) {
  3783. $sorted_column_index = $key;
  3784. break;
  3785. }
  3786. }
  3787. if ($sorted_column_index === false) {
  3788. return null;
  3789. }
  3790. // fetch first row of the result set
  3791. $row = $GLOBALS['dbi']->fetchRow($dt_result);
  3792. // initializing default arguments
  3793. $default_function = [Core::class, 'mimeDefaultFunction'];
  3794. $transformation_plugin = $default_function;
  3795. $transform_options = array();
  3796. // check for non printable sorted row data
  3797. $meta = $fields_meta[$sorted_column_index];
  3798. if (stristr($meta->type, self::BLOB_FIELD)
  3799. || ($meta->type == self::GEOMETRY_FIELD)
  3800. ) {
  3801. $column_for_first_row = $this->_handleNonPrintableContents(
  3802. $meta->type, $row[$sorted_column_index],
  3803. $transformation_plugin, $transform_options,
  3804. $default_function, $meta
  3805. );
  3806. } else {
  3807. $column_for_first_row = $row[$sorted_column_index];
  3808. }
  3809. $column_for_first_row = mb_strtoupper(
  3810. mb_substr(
  3811. $column_for_first_row, 0, $GLOBALS['cfg']['LimitChars']
  3812. ) . '...'
  3813. );
  3814. // fetch last row of the result set
  3815. $GLOBALS['dbi']->dataSeek($dt_result, $this->__get('num_rows') - 1);
  3816. $row = $GLOBALS['dbi']->fetchRow($dt_result);
  3817. // check for non printable sorted row data
  3818. $meta = $fields_meta[$sorted_column_index];
  3819. if (stristr($meta->type, self::BLOB_FIELD)
  3820. || ($meta->type == self::GEOMETRY_FIELD)
  3821. ) {
  3822. $column_for_last_row = $this->_handleNonPrintableContents(
  3823. $meta->type, $row[$sorted_column_index],
  3824. $transformation_plugin, $transform_options,
  3825. $default_function, $meta
  3826. );
  3827. } else {
  3828. $column_for_last_row = $row[$sorted_column_index];
  3829. }
  3830. $column_for_last_row = mb_strtoupper(
  3831. mb_substr(
  3832. $column_for_last_row, 0, $GLOBALS['cfg']['LimitChars']
  3833. ) . '...'
  3834. );
  3835. // reset to first row for the loop in _getTableBody()
  3836. $GLOBALS['dbi']->dataSeek($dt_result, 0);
  3837. // we could also use here $sort_expression_nodirection
  3838. return ' [' . htmlspecialchars($sort_column)
  3839. . ': <strong>' . htmlspecialchars($column_for_first_row) . ' - '
  3840. . htmlspecialchars($column_for_last_row) . '</strong>]';
  3841. } // end of the '_getSortedColumnMessage()' function
  3842. /**
  3843. * Set the content that needs to be shown in message
  3844. *
  3845. * @param string $sorted_column_message the message for sorted column
  3846. * @param array $analyzed_sql_results the analyzed query
  3847. * @param integer $total the total number of rows returned by
  3848. * the SQL query without any
  3849. * programmatically appended LIMIT clause
  3850. * @param integer $pos_next the offset for next page
  3851. * @param string $pre_count the string renders before row count
  3852. * @param string $after_count the string renders after row count
  3853. *
  3854. * @return Message $message an object of Message
  3855. *
  3856. * @access private
  3857. *
  3858. * @see getTable()
  3859. */
  3860. private function _setMessageInformation(
  3861. $sorted_column_message, array $analyzed_sql_results, $total,
  3862. $pos_next, $pre_count, $after_count
  3863. ) {
  3864. $unlim_num_rows = $this->__get('unlim_num_rows'); // To use in isset()
  3865. if (!empty($analyzed_sql_results['statement']->limit)) {
  3866. $first_shown_rec = $analyzed_sql_results['statement']->limit->offset;
  3867. $row_count = $analyzed_sql_results['statement']->limit->rowCount;
  3868. if ($row_count < $total) {
  3869. $last_shown_rec = $first_shown_rec + $row_count - 1;
  3870. } else {
  3871. $last_shown_rec = $first_shown_rec + $total - 1;
  3872. }
  3873. } elseif (($_SESSION['tmpval']['max_rows'] == self::ALL_ROWS)
  3874. || ($pos_next > $total)
  3875. ) {
  3876. $first_shown_rec = $_SESSION['tmpval']['pos'];
  3877. $last_shown_rec = $total - 1;
  3878. } else {
  3879. $first_shown_rec = $_SESSION['tmpval']['pos'];
  3880. $last_shown_rec = $pos_next - 1;
  3881. }
  3882. $table = new Table($this->__get('table'), $this->__get('db'));
  3883. if ($table->isView()
  3884. && ($total == $GLOBALS['cfg']['MaxExactCountViews'])
  3885. ) {
  3886. $message = Message::notice(
  3887. __(
  3888. 'This view has at least this number of rows. '
  3889. . 'Please refer to %sdocumentation%s.'
  3890. )
  3891. );
  3892. $message->addParam('[doc@cfg_MaxExactCount]');
  3893. $message->addParam('[/doc]');
  3894. $message_view_warning = Util::showHint($message);
  3895. } else {
  3896. $message_view_warning = false;
  3897. }
  3898. $message = Message::success(__('Showing rows %1s - %2s'));
  3899. $message->addParam($first_shown_rec);
  3900. if ($message_view_warning !== false) {
  3901. $message->addParamHtml('... ' . $message_view_warning);
  3902. } else {
  3903. $message->addParam($last_shown_rec);
  3904. }
  3905. $message->addText('(');
  3906. if ($message_view_warning === false) {
  3907. if (isset($unlim_num_rows) && ($unlim_num_rows != $total)) {
  3908. $message_total = Message::notice(
  3909. $pre_count . __('%1$d total, %2$d in query')
  3910. );
  3911. $message_total->addParam($total);
  3912. $message_total->addParam($unlim_num_rows);
  3913. } else {
  3914. $message_total = Message::notice($pre_count . __('%d total'));
  3915. $message_total->addParam($total);
  3916. }
  3917. if (!empty($after_count)) {
  3918. $message_total->addHtml($after_count);
  3919. }
  3920. $message->addMessage($message_total, '');
  3921. $message->addText(', ', '');
  3922. }
  3923. $message_qt = Message::notice(__('Query took %01.4f seconds.') . ')');
  3924. $message_qt->addParam($this->__get('querytime'));
  3925. $message->addMessage($message_qt, '');
  3926. if (! is_null($sorted_column_message)) {
  3927. $message->addHtml($sorted_column_message, '');
  3928. }
  3929. return $message;
  3930. } // end of the '_setMessageInformation()' function
  3931. /**
  3932. * Set the value of $map array for linking foreign key related tables
  3933. *
  3934. * @param array &$map the list of relations
  3935. *
  3936. * @return void
  3937. *
  3938. * @access private
  3939. *
  3940. * @see getTable()
  3941. */
  3942. private function _setParamForLinkForeignKeyRelatedTables(array &$map)
  3943. {
  3944. // To be able to later display a link to the related table,
  3945. // we verify both types of relations: either those that are
  3946. // native foreign keys or those defined in the phpMyAdmin
  3947. // configuration storage. If no PMA storage, we won't be able
  3948. // to use the "column to display" notion (for example show
  3949. // the name related to a numeric id).
  3950. $exist_rel = $this->relation->getForeigners(
  3951. $this->__get('db'), $this->__get('table'), '', self::POSITION_BOTH
  3952. );
  3953. if (! empty($exist_rel)) {
  3954. foreach ($exist_rel as $master_field => $rel) {
  3955. if ($master_field != 'foreign_keys_data') {
  3956. $display_field = $this->relation->getDisplayField(
  3957. $rel['foreign_db'], $rel['foreign_table']
  3958. );
  3959. $map[$master_field] = array(
  3960. $rel['foreign_table'],
  3961. $rel['foreign_field'],
  3962. $display_field,
  3963. $rel['foreign_db']
  3964. );
  3965. } else {
  3966. foreach ($rel as $key => $one_key) {
  3967. foreach ($one_key['index_list'] as $index => $one_field) {
  3968. $display_field = $this->relation->getDisplayField(
  3969. isset($one_key['ref_db_name'])
  3970. ? $one_key['ref_db_name']
  3971. : $GLOBALS['db'],
  3972. $one_key['ref_table_name']
  3973. );
  3974. $map[$one_field] = array(
  3975. $one_key['ref_table_name'],
  3976. $one_key['ref_index_list'][$index],
  3977. $display_field,
  3978. isset($one_key['ref_db_name'])
  3979. ? $one_key['ref_db_name']
  3980. : $GLOBALS['db']
  3981. );
  3982. }
  3983. }
  3984. }
  3985. } // end while
  3986. } // end if
  3987. } // end of the '_setParamForLinkForeignKeyRelatedTables()' function
  3988. /**
  3989. * Prepare multi field edit/delete links
  3990. *
  3991. * @param integer &$dt_result the link id associated to the query which
  3992. * results have to be displayed
  3993. * @param array $analyzed_sql_results analyzed sql results
  3994. * @param string $del_link the display element - 'del_link'
  3995. *
  3996. * @return string $links_html html content
  3997. *
  3998. * @access private
  3999. *
  4000. * @see getTable()
  4001. */
  4002. private function _getMultiRowOperationLinks(
  4003. &$dt_result, array $analyzed_sql_results, $del_link
  4004. ) {
  4005. $links_html = '<div class="print_ignore" >';
  4006. $url_query = $this->__get('url_query');
  4007. $delete_text = ($del_link == self::DELETE_ROW) ? __('Delete') : __('Kill');
  4008. $links_html .= '<img class="selectallarrow" width="38" height="22"'
  4009. . ' src="' . $this->__get('pma_theme_image') . 'arrow_'
  4010. . $this->__get('text_dir') . '.png' . '"'
  4011. . ' alt="' . __('With selected:') . '" />';
  4012. $links_html .= '<input type="checkbox" '
  4013. . 'id="resultsForm_' . $this->__get('unique_id') . '_checkall" '
  4014. . 'class="checkall_box" title="' . __('Check all') . '" /> '
  4015. . '<label for="resultsForm_' . $this->__get('unique_id') . '_checkall">'
  4016. . __('Check all') . '</label> '
  4017. . '<i style="margin-left: 2em">' . __('With selected:') . '</i>' . "\n";
  4018. $links_html .= Util::getButtonOrImage(
  4019. 'submit_mult', 'mult_submit',
  4020. __('Edit'), 'b_edit', 'edit'
  4021. );
  4022. $links_html .= Util::getButtonOrImage(
  4023. 'submit_mult', 'mult_submit',
  4024. __('Copy'), 'b_insrow', 'copy'
  4025. );
  4026. $links_html .= Util::getButtonOrImage(
  4027. 'submit_mult', 'mult_submit',
  4028. $delete_text, 'b_drop', 'delete'
  4029. );
  4030. if ($analyzed_sql_results['querytype'] == 'SELECT') {
  4031. $links_html .= Util::getButtonOrImage(
  4032. 'submit_mult', 'mult_submit',
  4033. __('Export'), 'b_tblexport', 'export'
  4034. );
  4035. }
  4036. $links_html .= "</div>\n";
  4037. $links_html .= '<input type="hidden" name="sql_query"'
  4038. . ' value="' . htmlspecialchars($this->__get('sql_query')) . '" />'
  4039. . "\n";
  4040. if (! empty($url_query)) {
  4041. $links_html .= '<input type="hidden" name="url_query"'
  4042. . ' value="' . $url_query . '" />' . "\n";
  4043. }
  4044. // fetch last row of the result set
  4045. $GLOBALS['dbi']->dataSeek($dt_result, $this->__get('num_rows') - 1);
  4046. $row = $GLOBALS['dbi']->fetchRow($dt_result);
  4047. // @see DbiMysqi::fetchRow & DatabaseInterface::fetchRow
  4048. if (! is_array($row)) {
  4049. $row = array();
  4050. }
  4051. // $clause_is_unique is needed by getTable() to generate the proper param
  4052. // in the multi-edit and multi-delete form
  4053. list($where_clause, $clause_is_unique, $condition_array)
  4054. = Util::getUniqueCondition(
  4055. $dt_result, // handle
  4056. $this->__get('fields_cnt'), // fields_cnt
  4057. $this->__get('fields_meta'), // fields_meta
  4058. $row, // row
  4059. false, // force_unique
  4060. false, // restrict_to_table
  4061. $analyzed_sql_results // analyzed_sql_results
  4062. );
  4063. unset($where_clause, $condition_array);
  4064. // reset to first row for the loop in _getTableBody()
  4065. $GLOBALS['dbi']->dataSeek($dt_result, 0);
  4066. $links_html .= '<input type="hidden" name="clause_is_unique"'
  4067. . ' value="' . $clause_is_unique . '" />' . "\n";
  4068. $links_html .= '</form>' . "\n";
  4069. return $links_html;
  4070. } // end of the '_getMultiRowOperationLinks()' function
  4071. /**
  4072. * Prepare table navigation bar at the top or bottom
  4073. *
  4074. * @param integer $pos_next the offset for the "next" page
  4075. * @param integer $pos_prev the offset for the "previous" page
  4076. * @param string $place the place to show navigation
  4077. * @param boolean $is_innodb whether its InnoDB or not
  4078. * @param string $sort_by_key_html the sort by key dialog
  4079. *
  4080. * @return string html content of navigation bar
  4081. *
  4082. * @access private
  4083. *
  4084. * @see _getTable()
  4085. */
  4086. private function _getPlacedTableNavigations(
  4087. $pos_next, $pos_prev, $place, $is_innodb, $sort_by_key_html
  4088. ) {
  4089. $navigation_html = '';
  4090. if ($place == self::PLACE_BOTTOM_DIRECTION_DROPDOWN) {
  4091. $navigation_html .= '<br />' . "\n";
  4092. }
  4093. $navigation_html .= $this->_getTableNavigation(
  4094. $pos_next, $pos_prev, $is_innodb, $sort_by_key_html
  4095. );
  4096. if ($place == self::PLACE_TOP_DIRECTION_DROPDOWN) {
  4097. $navigation_html .= "\n";
  4098. }
  4099. return $navigation_html;
  4100. } // end of the '_getPlacedTableNavigations()' function
  4101. /**
  4102. * Generates HTML to display the Create view in span tag
  4103. *
  4104. * @param array $analyzed_sql_results analyzed sql results
  4105. * @param string $url_query String with URL Parameters
  4106. *
  4107. * @return string
  4108. *
  4109. * @access private
  4110. *
  4111. * @see _getResultsOperations()
  4112. */
  4113. private function _getLinkForCreateView(array $analyzed_sql_results, $url_query)
  4114. {
  4115. $results_operations_html = '';
  4116. if (empty($analyzed_sql_results['procedure'])) {
  4117. $results_operations_html .= '<span>'
  4118. . Util::linkOrButton(
  4119. 'view_create.php' . $url_query,
  4120. Util::getIcon(
  4121. 'b_view_add', __('Create view'), true
  4122. ),
  4123. array('class' => 'create_view ajax')
  4124. )
  4125. . '</span>' . "\n";
  4126. }
  4127. return $results_operations_html;
  4128. }
  4129. /**
  4130. * Calls the _getResultsOperations with $only_view as true
  4131. *
  4132. * @param array $analyzed_sql_results analyzed sql results
  4133. *
  4134. * @return string
  4135. *
  4136. * @access public
  4137. *
  4138. */
  4139. public function getCreateViewQueryResultOp(array $analyzed_sql_results)
  4140. {
  4141. $results_operations_html = '';
  4142. //calling to _getResultOperations with a fake $displayParts
  4143. //and setting only_view parameter to be true to generate just view
  4144. $results_operations_html .= $this->_getResultsOperations(
  4145. array(),
  4146. $analyzed_sql_results,
  4147. true
  4148. );
  4149. return $results_operations_html;
  4150. }
  4151. /**
  4152. * Get copy to clipboard links for results operations
  4153. *
  4154. * @return string $html
  4155. *
  4156. * @access private
  4157. */
  4158. private function _getCopytoclipboardLinks()
  4159. {
  4160. $html = Util::linkOrButton(
  4161. '#',
  4162. Util::getIcon(
  4163. 'b_insrow', __('Copy to clipboard'), true
  4164. ),
  4165. array('id' => 'copyToClipBoard')
  4166. );
  4167. return $html;
  4168. }
  4169. /**
  4170. * Get printview links for results operations
  4171. *
  4172. * @return string $html
  4173. *
  4174. * @access private
  4175. */
  4176. private function _getPrintviewLinks()
  4177. {
  4178. $html = Util::linkOrButton(
  4179. '#',
  4180. Util::getIcon(
  4181. 'b_print', __('Print'), true
  4182. ),
  4183. array('id' => 'printView'),
  4184. 'print_view'
  4185. );
  4186. return $html;
  4187. }
  4188. /**
  4189. * Get operations that are available on results.
  4190. *
  4191. * @param array $displayParts the parts to display
  4192. * @param array $analyzed_sql_results analyzed sql results
  4193. * @param boolean $only_view Whether to show only view
  4194. *
  4195. * @return string $results_operations_html html content
  4196. *
  4197. * @access private
  4198. *
  4199. * @see getTable()
  4200. */
  4201. private function _getResultsOperations(
  4202. array $displayParts, array $analyzed_sql_results, $only_view = false
  4203. ) {
  4204. global $printview;
  4205. $results_operations_html = '';
  4206. $fields_meta = $this->__get('fields_meta'); // To safe use in foreach
  4207. $header_shown = false;
  4208. $header = '<fieldset class="print_ignore" ><legend>'
  4209. . __('Query results operations') . '</legend>';
  4210. $_url_params = array(
  4211. 'db' => $this->__get('db'),
  4212. 'table' => $this->__get('table'),
  4213. 'printview' => '1',
  4214. 'sql_query' => $this->__get('sql_query'),
  4215. );
  4216. $url_query = Url::getCommon($_url_params);
  4217. if (!$header_shown) {
  4218. $results_operations_html .= $header;
  4219. $header_shown = true;
  4220. }
  4221. // if empty result set was produced we need to
  4222. // show only view and not other options
  4223. if ($only_view) {
  4224. $results_operations_html .= $this->_getLinkForCreateView(
  4225. $analyzed_sql_results, $url_query
  4226. );
  4227. if ($header_shown) {
  4228. $results_operations_html .= '</fieldset><br />';
  4229. }
  4230. return $results_operations_html;
  4231. }
  4232. // Displays "printable view" link if required
  4233. if ($displayParts['pview_lnk'] == '1') {
  4234. $results_operations_html .= $this->_getPrintviewLinks();
  4235. $results_operations_html .= $this->_getCopytoclipboardLinks();
  4236. } // end displays "printable view"
  4237. // Export link
  4238. // (the url_query has extra parameters that won't be used to export)
  4239. // (the single_table parameter is used in Export::getDisplay()
  4240. // to hide the SQL and the structure export dialogs)
  4241. // If the parser found a PROCEDURE clause
  4242. // (most probably PROCEDURE ANALYSE()) it makes no sense to
  4243. // display the Export link).
  4244. if (($analyzed_sql_results['querytype'] == self::QUERY_TYPE_SELECT)
  4245. && ! isset($printview)
  4246. && empty($analyzed_sql_results['procedure'])
  4247. ) {
  4248. if (count($analyzed_sql_results['select_tables']) == 1) {
  4249. $_url_params['single_table'] = 'true';
  4250. }
  4251. if (! $header_shown) {
  4252. $results_operations_html .= $header;
  4253. $header_shown = true;
  4254. }
  4255. $_url_params['unlim_num_rows'] = $this->__get('unlim_num_rows');
  4256. /**
  4257. * At this point we don't know the table name; this can happen
  4258. * for example with a query like
  4259. * SELECT bike_code FROM (SELECT bike_code FROM bikes) tmp
  4260. * As a workaround we set in the table parameter the name of the
  4261. * first table of this database, so that tbl_export.php and
  4262. * the script it calls do not fail
  4263. */
  4264. if (empty($_url_params['table']) && ! empty($_url_params['db'])) {
  4265. $_url_params['table'] = $GLOBALS['dbi']->fetchValue("SHOW TABLES");
  4266. /* No result (probably no database selected) */
  4267. if ($_url_params['table'] === false) {
  4268. unset($_url_params['table']);
  4269. }
  4270. }
  4271. $results_operations_html .= Util::linkOrButton(
  4272. 'tbl_export.php' . Url::getCommon($_url_params),
  4273. Util::getIcon(
  4274. 'b_tblexport', __('Export'), true
  4275. )
  4276. )
  4277. . "\n";
  4278. // prepare chart
  4279. $results_operations_html .= Util::linkOrButton(
  4280. 'tbl_chart.php' . Url::getCommon($_url_params),
  4281. Util::getIcon(
  4282. 'b_chart', __('Display chart'), true
  4283. )
  4284. )
  4285. . "\n";
  4286. // prepare GIS chart
  4287. $geometry_found = false;
  4288. // If at least one geometry field is found
  4289. foreach ($fields_meta as $meta) {
  4290. if ($meta->type == self::GEOMETRY_FIELD) {
  4291. $geometry_found = true;
  4292. break;
  4293. }
  4294. }
  4295. if ($geometry_found) {
  4296. $results_operations_html
  4297. .= Util::linkOrButton(
  4298. 'tbl_gis_visualization.php'
  4299. . Url::getCommon($_url_params),
  4300. Util::getIcon(
  4301. 'b_globe',
  4302. __('Visualize GIS data'),
  4303. true
  4304. )
  4305. )
  4306. . "\n";
  4307. }
  4308. }
  4309. // CREATE VIEW
  4310. /**
  4311. *
  4312. * @todo detect privileges to create a view
  4313. * (but see 2006-01-19 note in PhpMyAdmin\Display\CreateTable,
  4314. * I think we cannot detect db-specific privileges reliably)
  4315. * Note: we don't display a Create view link if we found a PROCEDURE clause
  4316. */
  4317. if (!$header_shown) {
  4318. $results_operations_html .= $header;
  4319. $header_shown = true;
  4320. }
  4321. $results_operations_html .= $this->_getLinkForCreateView(
  4322. $analyzed_sql_results, $url_query
  4323. );
  4324. if ($header_shown) {
  4325. $results_operations_html .= '</fieldset><br />';
  4326. }
  4327. return $results_operations_html;
  4328. } // end of the '_getResultsOperations()' function
  4329. /**
  4330. * Verifies what to do with non-printable contents (binary or BLOB)
  4331. * in Browse mode.
  4332. *
  4333. * @param string $category BLOB|BINARY|GEOMETRY
  4334. * @param string $content the binary content
  4335. * @param mixed $transformation_plugin transformation plugin.
  4336. * Can also be the default function:
  4337. * Core::mimeDefaultFunction
  4338. * @param string $transform_options transformation parameters
  4339. * @param string $default_function default transformation function
  4340. * @param object $meta the meta-information about the field
  4341. * @param array $url_params parameters that should go to the
  4342. * download link
  4343. * @param boolean &$is_truncated the result is truncated or not
  4344. *
  4345. * @return mixed string or float
  4346. *
  4347. * @access private
  4348. *
  4349. * @see _getDataCellForGeometryColumns(),
  4350. * _getDataCellForNonNumericColumns(),
  4351. * _getSortedColumnMessage()
  4352. */
  4353. private function _handleNonPrintableContents(
  4354. $category, $content, $transformation_plugin, $transform_options,
  4355. $default_function, $meta, array $url_params = array(), &$is_truncated = null
  4356. ) {
  4357. $is_truncated = false;
  4358. $result = '[' . $category;
  4359. if (isset($content)) {
  4360. $size = strlen($content);
  4361. $display_size = Util::formatByteDown($size, 3, 1);
  4362. $result .= ' - ' . $display_size[0] . ' ' . $display_size[1];
  4363. } else {
  4364. $result .= ' - NULL';
  4365. $size = 0;
  4366. }
  4367. $result .= ']';
  4368. // if we want to use a text transformation on a BLOB column
  4369. if (gettype($transformation_plugin) === "object") {
  4370. $posMimeOctetstream = strpos(
  4371. $transformation_plugin->getMIMESubtype(),
  4372. 'Octetstream'
  4373. );
  4374. $posMimeText = strpos($transformation_plugin->getMIMEtype(), 'Text');
  4375. if ($posMimeOctetstream
  4376. || $posMimeText !== false
  4377. ) {
  4378. // Applying Transformations on hex string of binary data
  4379. // seems more appropriate
  4380. $result = pack("H*", bin2hex($content));
  4381. }
  4382. }
  4383. if ($size <= 0) {
  4384. return($result);
  4385. }
  4386. if ($default_function != $transformation_plugin) {
  4387. $result = $transformation_plugin->applyTransformation(
  4388. $result,
  4389. $transform_options,
  4390. $meta
  4391. );
  4392. return($result);
  4393. }
  4394. $result = $default_function($result, array(), $meta);
  4395. if (($_SESSION['tmpval']['display_binary']
  4396. && $meta->type === self::STRING_FIELD)
  4397. || ($_SESSION['tmpval']['display_blob']
  4398. && stristr($meta->type, self::BLOB_FIELD))
  4399. ) {
  4400. // in this case, restart from the original $content
  4401. if (mb_check_encoding($content, 'utf-8')
  4402. && !preg_match('/[\x00-\x08\x0B\x0C\x0E-\x1F\x80-\x9F]/u', $content)
  4403. ) {
  4404. // show as text if it's valid utf-8
  4405. $result = htmlspecialchars($content);
  4406. } else {
  4407. $result = '0x' . bin2hex($content);
  4408. }
  4409. list(
  4410. $is_truncated,
  4411. $result,
  4412. // skip 3rd param
  4413. ) = $this->_getPartialText($result);
  4414. }
  4415. /* Create link to download */
  4416. // in PHP < 5.5, empty() only checks variables
  4417. $tmpdb = $this->__get('db');
  4418. if (count($url_params) > 0
  4419. && (!empty($tmpdb) && !empty($meta->orgtable))
  4420. ) {
  4421. $result = '<a href="tbl_get_field.php'
  4422. . Url::getCommon($url_params)
  4423. . '" class="disableAjax">'
  4424. . $result . '</a>';
  4425. }
  4426. return($result);
  4427. } // end of the '_handleNonPrintableContents()' function
  4428. /**
  4429. * Retrieves the associated foreign key info for a data cell
  4430. *
  4431. * @param array $map the list of relations
  4432. * @param object $meta the meta-information about the field
  4433. * @param string $where_comparison data for the where clause
  4434. *
  4435. * @return string formatted data
  4436. *
  4437. * @access private
  4438. *
  4439. */
  4440. private function _getFromForeign(array $map, $meta, $where_comparison)
  4441. {
  4442. $dispsql = 'SELECT '
  4443. . Util::backquote($map[$meta->name][2])
  4444. . ' FROM '
  4445. . Util::backquote($map[$meta->name][3])
  4446. . '.'
  4447. . Util::backquote($map[$meta->name][0])
  4448. . ' WHERE '
  4449. . Util::backquote($map[$meta->name][1])
  4450. . $where_comparison;
  4451. $dispresult = $GLOBALS['dbi']->tryQuery(
  4452. $dispsql,
  4453. DatabaseInterface::CONNECT_USER,
  4454. DatabaseInterface::QUERY_STORE
  4455. );
  4456. if ($dispresult && $GLOBALS['dbi']->numRows($dispresult) > 0) {
  4457. list($dispval) = $GLOBALS['dbi']->fetchRow($dispresult, 0);
  4458. } else {
  4459. $dispval = __('Link not found!');
  4460. }
  4461. $GLOBALS['dbi']->freeResult($dispresult);
  4462. return $dispval;
  4463. }
  4464. /**
  4465. * Prepares the displayable content of a data cell in Browse mode,
  4466. * taking into account foreign key description field and transformations
  4467. *
  4468. * @param string $class css classes for the td element
  4469. * @param bool $condition_field whether the column is a part of
  4470. * the where clause
  4471. * @param array $analyzed_sql_results the analyzed query
  4472. * @param object $meta the meta-information about the
  4473. * field
  4474. * @param array $map the list of relations
  4475. * @param string $data data
  4476. * @param object|string $transformation_plugin transformation plugin.
  4477. * Can also be the default function:
  4478. * Core::mimeDefaultFunction
  4479. * @param string $default_function default function
  4480. * @param string $nowrap 'nowrap' if the content should
  4481. * not be wrapped
  4482. * @param string $where_comparison data for the where clause
  4483. * @param array $transform_options options for transformation
  4484. * @param bool $is_field_truncated whether the field is truncated
  4485. * @param string $original_length of a truncated column, or ''
  4486. *
  4487. * @return string formatted data
  4488. *
  4489. * @access private
  4490. *
  4491. * @see _getDataCellForNumericColumns(), _getDataCellForGeometryColumns(),
  4492. * _getDataCellForNonNumericColumns(),
  4493. *
  4494. */
  4495. private function _getRowData(
  4496. $class, $condition_field, array $analyzed_sql_results, $meta, array $map, $data,
  4497. $transformation_plugin, $default_function, $nowrap, $where_comparison,
  4498. array $transform_options, $is_field_truncated, $original_length=''
  4499. ) {
  4500. $relational_display = $_SESSION['tmpval']['relational_display'];
  4501. $printview = $this->__get('printview');
  4502. $decimals = isset($meta->decimals) ? $meta->decimals : '-1';
  4503. $result = '<td data-decimals="' . $decimals . '"'
  4504. . ' data-type="' . $meta->type . '"';
  4505. if (! empty($original_length)) {
  4506. // cannot use data-original-length
  4507. $result .= ' data-originallength="' . $original_length . '"';
  4508. }
  4509. $result .= ' class="'
  4510. . $this->_addClass(
  4511. $class, $condition_field, $meta, $nowrap,
  4512. $is_field_truncated, $transformation_plugin, $default_function
  4513. )
  4514. . '">';
  4515. if (!empty($analyzed_sql_results['statement']->expr)) {
  4516. foreach ($analyzed_sql_results['statement']->expr as $expr) {
  4517. if ((empty($expr->alias)) || (empty($expr->column))) {
  4518. continue;
  4519. }
  4520. if (strcasecmp($meta->name, $expr->alias) == 0) {
  4521. $meta->name = $expr->column;
  4522. }
  4523. }
  4524. }
  4525. if (isset($map[$meta->name])) {
  4526. // Field to display from the foreign table?
  4527. if (isset($map[$meta->name][2])
  4528. && strlen($map[$meta->name][2]) > 0
  4529. ) {
  4530. $dispval = $this->_getFromForeign(
  4531. $map, $meta, $where_comparison
  4532. );
  4533. } else {
  4534. $dispval = '';
  4535. } // end if... else...
  4536. if (isset($printview) && ($printview == '1')) {
  4537. $result .= ($transformation_plugin != $default_function
  4538. ? $transformation_plugin->applyTransformation(
  4539. $data,
  4540. $transform_options,
  4541. $meta
  4542. )
  4543. : $default_function($data)
  4544. )
  4545. . ' <code>[-&gt;' . $dispval . ']</code>';
  4546. } else {
  4547. if ($relational_display == self::RELATIONAL_KEY) {
  4548. // user chose "relational key" in the display options, so
  4549. // the title contains the display field
  4550. $title = (! empty($dispval))
  4551. ? htmlspecialchars($dispval)
  4552. : '';
  4553. } else {
  4554. $title = htmlspecialchars($data);
  4555. }
  4556. $_url_params = array(
  4557. 'db' => $map[$meta->name][3],
  4558. 'table' => $map[$meta->name][0],
  4559. 'pos' => '0',
  4560. 'sql_query' => 'SELECT * FROM '
  4561. . Util::backquote($map[$meta->name][3]) . '.'
  4562. . Util::backquote($map[$meta->name][0])
  4563. . ' WHERE '
  4564. . Util::backquote($map[$meta->name][1])
  4565. . $where_comparison,
  4566. );
  4567. if ($transformation_plugin != $default_function) {
  4568. // always apply a transformation on the real data,
  4569. // not on the display field
  4570. $message = $transformation_plugin->applyTransformation(
  4571. $data,
  4572. $transform_options,
  4573. $meta
  4574. );
  4575. } else {
  4576. if ($relational_display == self::RELATIONAL_DISPLAY_COLUMN
  4577. && ! empty($map[$meta->name][2])
  4578. ) {
  4579. // user chose "relational display field" in the
  4580. // display options, so show display field in the cell
  4581. $message = $default_function($dispval);
  4582. } else {
  4583. // otherwise display data in the cell
  4584. $message = $default_function($data);
  4585. }
  4586. }
  4587. $tag_params = array('title' => $title);
  4588. if (strpos($class, 'grid_edit') !== false) {
  4589. $tag_params['class'] = 'ajax';
  4590. }
  4591. $result .= Util::linkOrButton(
  4592. 'sql.php' . Url::getCommon($_url_params),
  4593. $message, $tag_params
  4594. );
  4595. }
  4596. } else {
  4597. $result .= ($transformation_plugin != $default_function
  4598. ? $transformation_plugin->applyTransformation(
  4599. $data,
  4600. $transform_options,
  4601. $meta
  4602. )
  4603. : $default_function($data)
  4604. );
  4605. }
  4606. $result .= '</td>' . "\n";
  4607. return $result;
  4608. } // end of the '_getRowData()' function
  4609. /**
  4610. * Prepares a checkbox for multi-row submits
  4611. *
  4612. * @param string $del_url delete url
  4613. * @param array $displayParts array with explicit indexes for all
  4614. * the display elements
  4615. * @param string $row_no the row number
  4616. * @param string $where_clause_html url encoded where clause
  4617. * @param array $condition_array array of conditions in the where clause
  4618. * @param string $id_suffix suffix for the id
  4619. * @param string $class css classes for the td element
  4620. *
  4621. * @return string the generated HTML
  4622. *
  4623. * @access private
  4624. *
  4625. * @see _getTableBody(), _getCheckboxAndLinks()
  4626. */
  4627. private function _getCheckboxForMultiRowSubmissions(
  4628. $del_url, array $displayParts, $row_no, $where_clause_html, array $condition_array,
  4629. $id_suffix, $class
  4630. ) {
  4631. $ret = '';
  4632. if (! empty($del_url) && $displayParts['del_lnk'] != self::KILL_PROCESS) {
  4633. $ret .= '<td ';
  4634. if (! empty($class)) {
  4635. $ret .= 'class="' . $class . '"';
  4636. }
  4637. $ret .= ' class="center print_ignore">'
  4638. . '<input type="checkbox" id="id_rows_to_delete'
  4639. . $row_no . $id_suffix
  4640. . '" name="rows_to_delete[' . $row_no . ']"'
  4641. . ' class="multi_checkbox checkall"'
  4642. . ' value="' . $where_clause_html . '" '
  4643. . ' />'
  4644. . '<input type="hidden" class="condition_array" value="'
  4645. . htmlspecialchars(json_encode($condition_array)) . '" />'
  4646. . ' </td>';
  4647. }
  4648. return $ret;
  4649. } // end of the '_getCheckboxForMultiRowSubmissions()' function
  4650. /**
  4651. * Prepares an Edit link
  4652. *
  4653. * @param string $edit_url edit url
  4654. * @param string $class css classes for td element
  4655. * @param string $edit_str text for the edit link
  4656. * @param string $where_clause where clause
  4657. * @param string $where_clause_html url encoded where clause
  4658. *
  4659. * @return string the generated HTML
  4660. *
  4661. * @access private
  4662. *
  4663. * @see _getTableBody(), _getCheckboxAndLinks()
  4664. */
  4665. private function _getEditLink(
  4666. $edit_url, $class, $edit_str, $where_clause, $where_clause_html
  4667. ) {
  4668. $ret = '';
  4669. if (! empty($edit_url)) {
  4670. $ret .= '<td class="' . $class . ' center print_ignore" '
  4671. . ' ><span class="nowrap">'
  4672. . Util::linkOrButton($edit_url, $edit_str);
  4673. /*
  4674. * Where clause for selecting this row uniquely is provided as
  4675. * a hidden input. Used by jQuery scripts for handling grid editing
  4676. */
  4677. if (! empty($where_clause)) {
  4678. $ret .= '<input type="hidden" class="where_clause" value ="'
  4679. . $where_clause_html . '" />';
  4680. }
  4681. $ret .= '</span></td>';
  4682. }
  4683. return $ret;
  4684. } // end of the '_getEditLink()' function
  4685. /**
  4686. * Prepares an Copy link
  4687. *
  4688. * @param string $copy_url copy url
  4689. * @param string $copy_str text for the copy link
  4690. * @param string $where_clause where clause
  4691. * @param string $where_clause_html url encoded where clause
  4692. * @param string $class css classes for the td element
  4693. *
  4694. * @return string the generated HTML
  4695. *
  4696. * @access private
  4697. *
  4698. * @see _getTableBody(), _getCheckboxAndLinks()
  4699. */
  4700. private function _getCopyLink(
  4701. $copy_url, $copy_str, $where_clause, $where_clause_html, $class
  4702. ) {
  4703. $ret = '';
  4704. if (! empty($copy_url)) {
  4705. $ret .= '<td class="';
  4706. if (! empty($class)) {
  4707. $ret .= $class . ' ';
  4708. }
  4709. $ret .= 'center print_ignore" ' . ' ><span class="nowrap">'
  4710. . Util::linkOrButton($copy_url, $copy_str);
  4711. /*
  4712. * Where clause for selecting this row uniquely is provided as
  4713. * a hidden input. Used by jQuery scripts for handling grid editing
  4714. */
  4715. if (! empty($where_clause)) {
  4716. $ret .= '<input type="hidden" class="where_clause" value="'
  4717. . $where_clause_html . '" />';
  4718. }
  4719. $ret .= '</span></td>';
  4720. }
  4721. return $ret;
  4722. } // end of the '_getCopyLink()' function
  4723. /**
  4724. * Prepares a Delete link
  4725. *
  4726. * @param string $del_url delete url
  4727. * @param string $del_str text for the delete link
  4728. * @param string $js_conf text for the JS confirmation
  4729. * @param string $class css classes for the td element
  4730. *
  4731. * @return string the generated HTML
  4732. *
  4733. * @access private
  4734. *
  4735. * @see _getTableBody(), _getCheckboxAndLinks()
  4736. */
  4737. private function _getDeleteLink($del_url, $del_str, $js_conf, $class)
  4738. {
  4739. $ret = '';
  4740. if (empty($del_url)) {
  4741. return $ret;
  4742. }
  4743. $ret .= '<td class="';
  4744. if (! empty($class)) {
  4745. $ret .= $class . ' ';
  4746. }
  4747. $ajax = Response::getInstance()->isAjax() ? ' ajax' : '';
  4748. $ret .= 'center print_ignore" ' . ' >'
  4749. . Util::linkOrButton(
  4750. $del_url,
  4751. $del_str,
  4752. array('class' => 'delete_row requireConfirm' . $ajax)
  4753. )
  4754. . '<div class="hide">' . $js_conf . '</div>'
  4755. . '</td>';
  4756. return $ret;
  4757. } // end of the '_getDeleteLink()' function
  4758. /**
  4759. * Prepare checkbox and links at some position (left or right)
  4760. * (only called for horizontal mode)
  4761. *
  4762. * @param string $position the position of the checkbox and links
  4763. * @param string $del_url delete url
  4764. * @param array $displayParts array with explicit indexes for all the
  4765. * display elements
  4766. * @param string $row_no row number
  4767. * @param string $where_clause where clause
  4768. * @param string $where_clause_html url encoded where clause
  4769. * @param array $condition_array array of conditions in the where clause
  4770. * @param string $edit_url edit url
  4771. * @param string $copy_url copy url
  4772. * @param string $class css classes for the td elements
  4773. * @param string $edit_str text for the edit link
  4774. * @param string $copy_str text for the copy link
  4775. * @param string $del_str text for the delete link
  4776. * @param string $js_conf text for the JS confirmation
  4777. *
  4778. * @return string the generated HTML
  4779. *
  4780. * @access private
  4781. *
  4782. * @see _getPlacedLinks()
  4783. */
  4784. private function _getCheckboxAndLinks(
  4785. $position, $del_url, array $displayParts, $row_no, $where_clause,
  4786. $where_clause_html, array $condition_array,
  4787. $edit_url, $copy_url, $class, $edit_str, $copy_str, $del_str, $js_conf
  4788. ) {
  4789. $ret = '';
  4790. if ($position == self::POSITION_LEFT) {
  4791. $ret .= $this->_getCheckboxForMultiRowSubmissions(
  4792. $del_url, $displayParts, $row_no, $where_clause_html,
  4793. $condition_array, '_left', ''
  4794. );
  4795. $ret .= $this->_getEditLink(
  4796. $edit_url, $class, $edit_str, $where_clause, $where_clause_html
  4797. );
  4798. $ret .= $this->_getCopyLink(
  4799. $copy_url, $copy_str, $where_clause, $where_clause_html, ''
  4800. );
  4801. $ret .= $this->_getDeleteLink($del_url, $del_str, $js_conf, '');
  4802. } elseif ($position == self::POSITION_RIGHT) {
  4803. $ret .= $this->_getDeleteLink($del_url, $del_str, $js_conf, '');
  4804. $ret .= $this->_getCopyLink(
  4805. $copy_url, $copy_str, $where_clause, $where_clause_html, ''
  4806. );
  4807. $ret .= $this->_getEditLink(
  4808. $edit_url, $class, $edit_str, $where_clause, $where_clause_html
  4809. );
  4810. $ret .= $this->_getCheckboxForMultiRowSubmissions(
  4811. $del_url, $displayParts, $row_no, $where_clause_html,
  4812. $condition_array, '_right', ''
  4813. );
  4814. } else { // $position == self::POSITION_NONE
  4815. $ret .= $this->_getCheckboxForMultiRowSubmissions(
  4816. $del_url, $displayParts, $row_no, $where_clause_html,
  4817. $condition_array, '_left', ''
  4818. );
  4819. }
  4820. return $ret;
  4821. } // end of the '_getCheckboxAndLinks()' function
  4822. /**
  4823. * Truncates given string based on LimitChars configuration
  4824. * and Session pftext variable
  4825. * (string is truncated only if necessary)
  4826. *
  4827. * @param string $str string to be truncated
  4828. *
  4829. * @return mixed
  4830. *
  4831. * @access private
  4832. *
  4833. * @see _handleNonPrintableContents(), _getDataCellForGeometryColumns(),
  4834. * _getDataCellForNonNumericColumns
  4835. */
  4836. private function _getPartialText($str)
  4837. {
  4838. $original_length = mb_strlen($str);
  4839. if ($original_length > $GLOBALS['cfg']['LimitChars']
  4840. && $_SESSION['tmpval']['pftext'] === self::DISPLAY_PARTIAL_TEXT
  4841. ) {
  4842. $str = mb_substr(
  4843. $str, 0, $GLOBALS['cfg']['LimitChars']
  4844. ) . '...';
  4845. $truncated = true;
  4846. } else {
  4847. $truncated = false;
  4848. }
  4849. return array($truncated, $str, $original_length);
  4850. }
  4851. }