Results.php 202 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687
  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. // PHP 7.4 fix for accessing array offset on bool
  1041. $col_visib_current = is_array($col_visib) && isset($col_visib[$j]) ? $col_visib[$j] : null;
  1042. // assign $i with the appropriate column order
  1043. $i = $col_order ? $col_order[$j] : $j;
  1044. // See if this column should get highlight because it's used in the
  1045. // where-query.
  1046. $name = $fields_meta[$i]->name;
  1047. $condition_field = (isset($highlight_columns[$name])
  1048. || isset($highlight_columns[Util::backquote($name)]))
  1049. ? true
  1050. : false;
  1051. // Prepare comment-HTML-wrappers for each row, if defined/enabled.
  1052. $comments = $this->_getCommentForRow($comments_map, $fields_meta[$i]);
  1053. $display_params = $this->__get('display_params');
  1054. if (($displayParts['sort_lnk'] == '1') && ! $is_limited_display) {
  1055. list($order_link, $sorted_header_html)
  1056. = $this->_getOrderLinkAndSortedHeaderHtml(
  1057. $fields_meta[$i], $sort_expression,
  1058. $sort_expression_nodirection, $i, $unsorted_sql_query,
  1059. $session_max_rows, $comments,
  1060. $sort_direction, $col_visib,
  1061. $col_visib_current
  1062. );
  1063. $html .= $sorted_header_html;
  1064. $display_params['desc'][] = ' <th '
  1065. . 'class="draggable'
  1066. . ($condition_field ? ' condition' : '')
  1067. . '" data-column="' . htmlspecialchars($fields_meta[$i]->name)
  1068. . '">' . "\n" . $order_link . $comments . ' </th>' . "\n";
  1069. } else {
  1070. // Results can't be sorted
  1071. $html
  1072. .= $this->_getDraggableClassForNonSortableColumns(
  1073. $col_visib, $col_visib_current, $condition_field,
  1074. $fields_meta[$i], $comments
  1075. );
  1076. $display_params['desc'][] = ' <th '
  1077. . 'class="draggable'
  1078. . ($condition_field ? ' condition"' : '')
  1079. . '" data-column="' . htmlspecialchars($fields_meta[$i]->name)
  1080. . '">' . ' '
  1081. . htmlspecialchars($fields_meta[$i]->name)
  1082. . $comments . ' </th>';
  1083. } // end else
  1084. $this->__set('display_params', $display_params);
  1085. } // end for
  1086. return $html;
  1087. }
  1088. /**
  1089. * Get the headers of the results table
  1090. *
  1091. * @param array &$displayParts which elements to display
  1092. * @param array $analyzed_sql_results analyzed sql results
  1093. * @param string $unsorted_sql_query the unsorted sql query
  1094. * @param array $sort_expression sort expression
  1095. * @param array|string $sort_expression_nodirection sort expression
  1096. * without direction
  1097. * @param array $sort_direction sort direction
  1098. * @param boolean $is_limited_display with limited operations
  1099. * or not
  1100. *
  1101. * @return string html content
  1102. *
  1103. * @access private
  1104. *
  1105. * @see getTable()
  1106. */
  1107. private function _getTableHeaders(
  1108. array &$displayParts, array $analyzed_sql_results, $unsorted_sql_query,
  1109. array $sort_expression = array(), $sort_expression_nodirection = '',
  1110. array $sort_direction = array(), $is_limited_display = false
  1111. ) {
  1112. $table_headers_html = '';
  1113. // Needed for use in isset/empty or
  1114. // use with array indexes/safe use in foreach
  1115. $printview = $this->__get('printview');
  1116. $display_params = $this->__get('display_params');
  1117. // Output data needed for grid editing
  1118. $table_headers_html .= '<input class="save_cells_at_once" type="hidden"'
  1119. . ' value="' . $GLOBALS['cfg']['SaveCellsAtOnce'] . '" />'
  1120. . '<div class="common_hidden_inputs">'
  1121. . Url::getHiddenInputs(
  1122. $this->__get('db'), $this->__get('table')
  1123. )
  1124. . '</div>';
  1125. // Output data needed for column reordering and show/hide column
  1126. $table_headers_html .= $this->_getDataForResettingColumnOrder($analyzed_sql_results);
  1127. $display_params['emptypre'] = 0;
  1128. $display_params['emptyafter'] = 0;
  1129. $display_params['textbtn'] = '';
  1130. $full_or_partial_text_link = null;
  1131. $this->__set('display_params', $display_params);
  1132. // Display options (if we are not in print view)
  1133. if (! (isset($printview) && ($printview == '1')) && ! $is_limited_display) {
  1134. $table_headers_html .= $this->_getOptionsBlock();
  1135. // prepare full/partial text button or link
  1136. $full_or_partial_text_link = $this->_getFullOrPartialTextButtonOrLink();
  1137. }
  1138. // Start of form for multi-rows edit/delete/export
  1139. $table_headers_html .= $this->_getFormForMultiRowOperations(
  1140. $displayParts['del_lnk']
  1141. );
  1142. // 1. Set $colspan and generate html with full/partial
  1143. // text button or link
  1144. list($colspan, $button_html)
  1145. = $this->_getFieldVisibilityParams(
  1146. $displayParts, $full_or_partial_text_link
  1147. );
  1148. $table_headers_html .= $button_html;
  1149. // 2. Displays the fields' name
  1150. // 2.0 If sorting links should be used, checks if the query is a "JOIN"
  1151. // statement (see 2.1.3)
  1152. // See if we have to highlight any header fields of a WHERE query.
  1153. // Uses SQL-Parser results.
  1154. $this->_setHighlightedColumnGlobalField($analyzed_sql_results);
  1155. // Get the headers for all of the columns
  1156. $table_headers_html .= $this->_getTableHeadersForColumns(
  1157. $displayParts, $analyzed_sql_results, $sort_expression,
  1158. $sort_expression_nodirection, $sort_direction,
  1159. $is_limited_display, $unsorted_sql_query
  1160. );
  1161. // Display column at rightside - checkboxes or empty column
  1162. if (! $printview) {
  1163. $table_headers_html .= $this->_getColumnAtRightSide(
  1164. $displayParts, $full_or_partial_text_link, $colspan
  1165. );
  1166. }
  1167. $table_headers_html .= '</tr>' . '</thead>';
  1168. return $table_headers_html;
  1169. } // end of the '_getTableHeaders()' function
  1170. /**
  1171. * Prepare unsorted sql query and sort by key drop down
  1172. *
  1173. * @param array $analyzed_sql_results analyzed sql results
  1174. * @param array|null $sort_expression sort expression
  1175. *
  1176. * @return array two element array - $unsorted_sql_query, $drop_down_html
  1177. *
  1178. * @access private
  1179. *
  1180. * @see _getTableHeaders()
  1181. */
  1182. private function _getUnsortedSqlAndSortByKeyDropDown(
  1183. array $analyzed_sql_results, array $sort_expression
  1184. ) {
  1185. $drop_down_html = '';
  1186. $unsorted_sql_query = Query::replaceClause(
  1187. $analyzed_sql_results['statement'],
  1188. $analyzed_sql_results['parser']->list,
  1189. 'ORDER BY',
  1190. ''
  1191. );
  1192. // Data is sorted by indexes only if it there is only one table.
  1193. if ($this->_isSelect($analyzed_sql_results)) {
  1194. // grab indexes data:
  1195. $indexes = Index::getFromTable(
  1196. $this->__get('table'),
  1197. $this->__get('db')
  1198. );
  1199. // do we have any index?
  1200. if (! empty($indexes)) {
  1201. $drop_down_html = $this->_getSortByKeyDropDown(
  1202. $indexes, $sort_expression,
  1203. $unsorted_sql_query
  1204. );
  1205. }
  1206. }
  1207. return array($unsorted_sql_query, $drop_down_html);
  1208. } // end of the '_getUnsortedSqlAndSortByKeyDropDown()' function
  1209. /**
  1210. * Prepare sort by key dropdown - html code segment
  1211. *
  1212. * @param Index[] $indexes the indexes of the table for sort criteria
  1213. * @param array|null $sort_expression the sort expression
  1214. * @param string $unsorted_sql_query the unsorted sql query
  1215. *
  1216. * @return string $drop_down_html html content
  1217. *
  1218. * @access private
  1219. *
  1220. * @see _getTableHeaders()
  1221. */
  1222. private function _getSortByKeyDropDown(
  1223. $indexes, array $sort_expression, $unsorted_sql_query
  1224. ) {
  1225. $drop_down_html = '';
  1226. $drop_down_html .= '<form action="sql.php" method="post" ' .
  1227. 'class="print_ignore">' . "\n"
  1228. . Url::getHiddenInputs(
  1229. $this->__get('db'), $this->__get('table')
  1230. )
  1231. . Url::getHiddenFields(array('sort_by_key' => '1'))
  1232. . __('Sort by key')
  1233. . ': <select name="sql_query" class="autosubmit">' . "\n";
  1234. $used_index = false;
  1235. $local_order = (is_array($sort_expression) ? implode(', ',$sort_expression) : '');
  1236. foreach ($indexes as $index) {
  1237. $asc_sort = '`'
  1238. . implode('` ASC, `', array_keys($index->getColumns()))
  1239. . '` ASC';
  1240. $desc_sort = '`'
  1241. . implode('` DESC, `', array_keys($index->getColumns()))
  1242. . '` DESC';
  1243. $used_index = $used_index
  1244. || ($local_order == $asc_sort)
  1245. || ($local_order == $desc_sort);
  1246. if (preg_match(
  1247. '@(.*)([[:space:]](LIMIT (.*)|PROCEDURE (.*)|'
  1248. . 'FOR UPDATE|LOCK IN SHARE MODE))@is',
  1249. $unsorted_sql_query, $my_reg
  1250. )) {
  1251. $unsorted_sql_query_first_part = $my_reg[1];
  1252. $unsorted_sql_query_second_part = $my_reg[2];
  1253. } else {
  1254. $unsorted_sql_query_first_part = $unsorted_sql_query;
  1255. $unsorted_sql_query_second_part = '';
  1256. }
  1257. $drop_down_html .= '<option value="'
  1258. . htmlspecialchars(
  1259. $unsorted_sql_query_first_part . "\n"
  1260. . ' ORDER BY ' . $asc_sort
  1261. . $unsorted_sql_query_second_part
  1262. )
  1263. . '"' . ($local_order == $asc_sort
  1264. ? ' selected="selected"'
  1265. : '')
  1266. . '>' . htmlspecialchars($index->getName()) . ' (ASC)</option>';
  1267. $drop_down_html .= '<option value="'
  1268. . htmlspecialchars(
  1269. $unsorted_sql_query_first_part . "\n"
  1270. . ' ORDER BY ' . $desc_sort
  1271. . $unsorted_sql_query_second_part
  1272. )
  1273. . '"' . ($local_order == $desc_sort
  1274. ? ' selected="selected"'
  1275. : '')
  1276. . '>' . htmlspecialchars($index->getName()) . ' (DESC)</option>';
  1277. }
  1278. $drop_down_html .= '<option value="' . htmlspecialchars($unsorted_sql_query)
  1279. . '"' . ($used_index ? '' : ' selected="selected"') . '>' . __('None')
  1280. . '</option>'
  1281. . '</select>' . "\n"
  1282. . '</form>' . "\n";
  1283. return $drop_down_html;
  1284. } // end of the '_getSortByKeyDropDown()' function
  1285. /**
  1286. * Set column span, row span and prepare html with full/partial
  1287. * text button or link
  1288. *
  1289. * @param array &$displayParts which elements to display
  1290. * @param string $full_or_partial_text_link full/partial link or text button
  1291. *
  1292. * @return array 2 element array - $colspan, $button_html
  1293. *
  1294. * @access private
  1295. *
  1296. * @see _getTableHeaders()
  1297. */
  1298. private function _getFieldVisibilityParams(
  1299. array &$displayParts, $full_or_partial_text_link
  1300. ) {
  1301. $button_html = '';
  1302. $display_params = $this->__get('display_params');
  1303. // 1. Displays the full/partial text button (part 1)...
  1304. $button_html .= '<thead><tr>' . "\n";
  1305. $colspan = (($displayParts['edit_lnk'] != self::NO_EDIT_OR_DELETE)
  1306. && ($displayParts['del_lnk'] != self::NO_EDIT_OR_DELETE))
  1307. ? ' colspan="4"'
  1308. : '';
  1309. // ... before the result table
  1310. if ((($displayParts['edit_lnk'] == self::NO_EDIT_OR_DELETE)
  1311. && ($displayParts['del_lnk'] == self::NO_EDIT_OR_DELETE))
  1312. && ($displayParts['text_btn'] == '1')
  1313. ) {
  1314. $display_params['emptypre']
  1315. = (($displayParts['edit_lnk'] != self::NO_EDIT_OR_DELETE)
  1316. && ($displayParts['del_lnk'] != self::NO_EDIT_OR_DELETE)) ? 4 : 0;
  1317. } elseif ((($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_LEFT)
  1318. || ($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_BOTH))
  1319. && ($displayParts['text_btn'] == '1')
  1320. ) {
  1321. // ... at the left column of the result table header if possible
  1322. // and required
  1323. $display_params['emptypre']
  1324. = (($displayParts['edit_lnk'] != self::NO_EDIT_OR_DELETE)
  1325. && ($displayParts['del_lnk'] != self::NO_EDIT_OR_DELETE)) ? 4 : 0;
  1326. $button_html .= '<th class="column_action print_ignore" ' . $colspan
  1327. . '>' . $full_or_partial_text_link . '</th>';
  1328. } elseif ((($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_LEFT)
  1329. || ($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_BOTH))
  1330. && (($displayParts['edit_lnk'] != self::NO_EDIT_OR_DELETE)
  1331. || ($displayParts['del_lnk'] != self::NO_EDIT_OR_DELETE))
  1332. ) {
  1333. // ... elseif no button, displays empty(ies) col(s) if required
  1334. $display_params['emptypre']
  1335. = (($displayParts['edit_lnk'] != self::NO_EDIT_OR_DELETE)
  1336. && ($displayParts['del_lnk'] != self::NO_EDIT_OR_DELETE)) ? 4 : 0;
  1337. $button_html .= '<td ' . $colspan . '></td>';
  1338. } elseif (($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_NONE)) {
  1339. // ... elseif display an empty column if the actions links are
  1340. // disabled to match the rest of the table
  1341. $button_html .= '<th class="column_action"></th>';
  1342. }
  1343. $this->__set('display_params', $display_params);
  1344. return array($colspan, $button_html);
  1345. } // end of the '_getFieldVisibilityParams()' function
  1346. /**
  1347. * Get table comments as array
  1348. *
  1349. * @param array $analyzed_sql_results analyzed sql results
  1350. *
  1351. * @return array $comments_map table comments
  1352. *
  1353. * @access private
  1354. *
  1355. * @see _getTableHeaders()
  1356. */
  1357. private function _getTableCommentsArray(array $analyzed_sql_results)
  1358. {
  1359. if ((!$GLOBALS['cfg']['ShowBrowseComments'])
  1360. || (empty($analyzed_sql_results['statement']->from))
  1361. ) {
  1362. return array();
  1363. }
  1364. $ret = array();
  1365. foreach ($analyzed_sql_results['statement']->from as $field) {
  1366. if (empty($field->table)) {
  1367. continue;
  1368. }
  1369. $ret[$field->table] = $this->relation->getComments(
  1370. empty($field->database) ? $this->__get('db') : $field->database,
  1371. $field->table
  1372. );
  1373. }
  1374. return $ret;
  1375. } // end of the '_getTableCommentsArray()' function
  1376. /**
  1377. * Set global array for store highlighted header fields
  1378. *
  1379. * @param array $analyzed_sql_results analyzed sql results
  1380. *
  1381. * @return void
  1382. *
  1383. * @access private
  1384. *
  1385. * @see _getTableHeaders()
  1386. */
  1387. private function _setHighlightedColumnGlobalField(array $analyzed_sql_results)
  1388. {
  1389. $highlight_columns = array();
  1390. if (!empty($analyzed_sql_results['statement']->where)) {
  1391. foreach ($analyzed_sql_results['statement']->where as $expr) {
  1392. foreach ($expr->identifiers as $identifier) {
  1393. $highlight_columns[$identifier] = 'true';
  1394. }
  1395. }
  1396. }
  1397. $this->__set('highlight_columns', $highlight_columns);
  1398. } // end of the '_setHighlightedColumnGlobalField()' function
  1399. /**
  1400. * Prepare data for column restoring and show/hide
  1401. *
  1402. * @param array $analyzed_sql_results analyzed sql results
  1403. *
  1404. * @return string $data_html html content
  1405. *
  1406. * @access private
  1407. *
  1408. * @see _getTableHeaders()
  1409. */
  1410. private function _getDataForResettingColumnOrder(array $analyzed_sql_results)
  1411. {
  1412. if (! $this->_isSelect($analyzed_sql_results)) {
  1413. return '';
  1414. }
  1415. // generate the column order, if it is set
  1416. list($col_order, $col_visib) = $this->_getColumnParams(
  1417. $analyzed_sql_results
  1418. );
  1419. $data_html = '';
  1420. if ($col_order) {
  1421. $data_html .= '<input class="col_order" type="hidden" value="'
  1422. . implode(',', $col_order) . '" />';
  1423. }
  1424. if ($col_visib) {
  1425. $data_html .= '<input class="col_visib" type="hidden" value="'
  1426. . implode(',', $col_visib) . '" />';
  1427. }
  1428. // generate table create time
  1429. $table = new Table($this->__get('table'), $this->__get('db'));
  1430. if (! $table->isView()) {
  1431. $data_html .= '<input class="table_create_time" type="hidden" value="'
  1432. . $GLOBALS['dbi']->getTable(
  1433. $this->__get('db'), $this->__get('table')
  1434. )->getStatusInfo('Create_time')
  1435. . '" />';
  1436. }
  1437. return $data_html;
  1438. } // end of the '_getDataForResettingColumnOrder()' function
  1439. /**
  1440. * Prepare option fields block
  1441. *
  1442. * @return string html content
  1443. *
  1444. * @access private
  1445. *
  1446. * @see _getTableHeaders()
  1447. */
  1448. private function _getOptionsBlock()
  1449. {
  1450. if(isset($_SESSION['tmpval']['possible_as_geometry']) && $_SESSION['tmpval']['possible_as_geometry'] == false) {
  1451. if($_SESSION['tmpval']['geoOption'] == self::GEOMETRY_DISP_GEOM) {
  1452. $_SESSION['tmpval']['geoOption'] = self::GEOMETRY_DISP_WKT;
  1453. }
  1454. }
  1455. return Template::get('display/results/options_block')->render([
  1456. 'unique_id' => $this->__get('unique_id'),
  1457. 'geo_option' => $_SESSION['tmpval']['geoOption'],
  1458. 'hide_transformation' => $_SESSION['tmpval']['hide_transformation'],
  1459. 'display_blob' => $_SESSION['tmpval']['display_blob'],
  1460. 'display_binary' => $_SESSION['tmpval']['display_binary'],
  1461. 'relational_display' => $_SESSION['tmpval']['relational_display'],
  1462. 'displaywork' => $GLOBALS['cfgRelation']['displaywork'],
  1463. 'relwork' => $GLOBALS['cfgRelation']['relwork'],
  1464. 'possible_as_geometry' => $_SESSION['tmpval']['possible_as_geometry'],
  1465. 'pftext' => $_SESSION['tmpval']['pftext'],
  1466. 'db' => $this->__get('db'),
  1467. 'table' => $this->__get('table'),
  1468. 'sql_query' => $this->__get('sql_query'),
  1469. 'goto' => $this->__get('goto'),
  1470. ]);
  1471. }
  1472. /**
  1473. * Get full/partial text button or link
  1474. *
  1475. * @return string html content
  1476. *
  1477. * @access private
  1478. *
  1479. * @see _getTableHeaders()
  1480. */
  1481. private function _getFullOrPartialTextButtonOrLink()
  1482. {
  1483. $url_params_full_text = array(
  1484. 'db' => $this->__get('db'),
  1485. 'table' => $this->__get('table'),
  1486. 'sql_query' => $this->__get('sql_query'),
  1487. 'goto' => $this->__get('goto'),
  1488. 'full_text_button' => 1
  1489. );
  1490. if ($_SESSION['tmpval']['pftext'] == self::DISPLAY_FULL_TEXT) {
  1491. // currently in fulltext mode so show the opposite link
  1492. $tmp_image_file = $this->__get('pma_theme_image') . 's_partialtext.png';
  1493. $tmp_txt = __('Partial texts');
  1494. $url_params_full_text['pftext'] = self::DISPLAY_PARTIAL_TEXT;
  1495. } else {
  1496. $tmp_image_file = $this->__get('pma_theme_image') . 's_fulltext.png';
  1497. $tmp_txt = __('Full texts');
  1498. $url_params_full_text['pftext'] = self::DISPLAY_FULL_TEXT;
  1499. }
  1500. $tmp_image = '<img class="fulltext" src="' . $tmp_image_file . '" alt="'
  1501. . $tmp_txt . '" title="' . $tmp_txt . '" />';
  1502. return Util::linkOrButton('sql.php', $url_params_full_text, $tmp_image);
  1503. } // end of the '_getFullOrPartialTextButtonOrLink()' function
  1504. /**
  1505. * Prepare html form for multi row operations
  1506. *
  1507. * @param string $deleteLink the delete link of current row
  1508. *
  1509. * @return string $form_html html content
  1510. *
  1511. * @access private
  1512. *
  1513. * @see _getTableHeaders()
  1514. */
  1515. private function _getFormForMultiRowOperations($deleteLink)
  1516. {
  1517. return Template::get('display/results/multi_row_operations_form')->render([
  1518. 'delete_link' => $deleteLink,
  1519. 'delete_row' => self::DELETE_ROW,
  1520. 'kill_process' => self::KILL_PROCESS,
  1521. 'unique_id' => $this->__get('unique_id'),
  1522. 'db' => $this->__get('db'),
  1523. 'table' => $this->__get('table'),
  1524. ]);
  1525. }
  1526. /**
  1527. * Get comment for row
  1528. *
  1529. * @param array $commentsMap comments array
  1530. * @param array $fieldsMeta set of field properties
  1531. *
  1532. * @return string html content
  1533. *
  1534. * @access private
  1535. *
  1536. * @see _getTableHeaders()
  1537. */
  1538. private function _getCommentForRow(array $commentsMap, $fieldsMeta)
  1539. {
  1540. return Template::get('display/results/comment_for_row')->render([
  1541. 'comments_map' => $commentsMap,
  1542. 'fields_meta' => $fieldsMeta,
  1543. 'limit_chars' => $GLOBALS['cfg']['LimitChars'],
  1544. ]);
  1545. }
  1546. /**
  1547. * Prepare parameters and html for sorted table header fields
  1548. *
  1549. * @param array $fields_meta set of field properties
  1550. * @param array $sort_expression sort expression
  1551. * @param array $sort_expression_nodirection sort expression without direction
  1552. * @param integer $column_index the index of the column
  1553. * @param string $unsorted_sql_query the unsorted sql query
  1554. * @param integer $session_max_rows maximum rows resulted by sql
  1555. * @param string $comments comment for row
  1556. * @param array $sort_direction sort direction
  1557. * @param boolean $col_visib column is visible(false)
  1558. * array column isn't visible(string array)
  1559. * @param string $col_visib_j element of $col_visib array
  1560. *
  1561. * @return array 2 element array - $order_link, $sorted_header_html
  1562. *
  1563. * @access private
  1564. *
  1565. * @see _getTableHeaders()
  1566. */
  1567. private function _getOrderLinkAndSortedHeaderHtml(
  1568. $fields_meta, array $sort_expression, array $sort_expression_nodirection,
  1569. $column_index, $unsorted_sql_query, $session_max_rows,
  1570. $comments, array $sort_direction, $col_visib, $col_visib_j
  1571. ) {
  1572. $sorted_header_html = '';
  1573. // Checks if the table name is required; it's the case
  1574. // for a query with a "JOIN" statement and if the column
  1575. // isn't aliased, or in queries like
  1576. // SELECT `1`.`master_field` , `2`.`master_field`
  1577. // FROM `PMA_relation` AS `1` , `PMA_relation` AS `2`
  1578. $sort_tbl = (isset($fields_meta->table)
  1579. && strlen($fields_meta->table) > 0
  1580. && $fields_meta->orgname == $fields_meta->name)
  1581. ? Util::backquote(
  1582. $fields_meta->table
  1583. ) . '.'
  1584. : '';
  1585. $name_to_use_in_sort = $fields_meta->name;
  1586. // Generates the orderby clause part of the query which is part
  1587. // of URL
  1588. list($single_sort_order, $multi_sort_order, $order_img)
  1589. = $this->_getSingleAndMultiSortUrls(
  1590. $sort_expression, $sort_expression_nodirection, $sort_tbl,
  1591. $name_to_use_in_sort, $sort_direction, $fields_meta, $column_index
  1592. );
  1593. if (preg_match(
  1594. '@(.*)([[:space:]](LIMIT (.*)|PROCEDURE (.*)|FOR UPDATE|'
  1595. . 'LOCK IN SHARE MODE))@is',
  1596. $unsorted_sql_query, $regs3
  1597. )) {
  1598. $single_sorted_sql_query = $regs3[1] . $single_sort_order . $regs3[2];
  1599. $multi_sorted_sql_query = $regs3[1] . $multi_sort_order . $regs3[2];
  1600. } else {
  1601. $single_sorted_sql_query = $unsorted_sql_query . $single_sort_order;
  1602. $multi_sorted_sql_query = $unsorted_sql_query . $multi_sort_order;
  1603. }
  1604. $_single_url_params = array(
  1605. 'db' => $this->__get('db'),
  1606. 'table' => $this->__get('table'),
  1607. 'sql_query' => $single_sorted_sql_query,
  1608. 'sql_signature' => Core::signSqlQuery($single_sorted_sql_query),
  1609. 'session_max_rows' => $session_max_rows,
  1610. 'is_browse_distinct' => $this->__get('is_browse_distinct'),
  1611. );
  1612. $_multi_url_params = array(
  1613. 'db' => $this->__get('db'),
  1614. 'table' => $this->__get('table'),
  1615. 'sql_query' => $multi_sorted_sql_query,
  1616. 'sql_signature' => Core::signSqlQuery($multi_sorted_sql_query),
  1617. 'session_max_rows' => $session_max_rows,
  1618. 'is_browse_distinct' => $this->__get('is_browse_distinct'),
  1619. );
  1620. // Displays the sorting URL
  1621. // enable sort order swapping for image
  1622. $order_link = $this->_getSortOrderLink(
  1623. $order_img, $fields_meta, $_single_url_params, $_multi_url_params
  1624. );
  1625. $sorted_header_html .= $this->_getDraggableClassForSortableColumns(
  1626. $col_visib, $col_visib_j,
  1627. $fields_meta, $order_link, $comments
  1628. );
  1629. return array($order_link, $sorted_header_html);
  1630. } // end of the '_getOrderLinkAndSortedHeaderHtml()' function
  1631. /**
  1632. * Prepare parameters and html for sorted table header fields
  1633. *
  1634. * @param array $sort_expression sort expression
  1635. * @param array $sort_expression_nodirection sort expression without direction
  1636. * @param string $sort_tbl The name of the table to which
  1637. * the current column belongs to
  1638. * @param string $name_to_use_in_sort The current column under
  1639. * consideration
  1640. * @param array $sort_direction sort direction
  1641. * @param array $fields_meta set of field properties
  1642. * @param integer $column_index The index number to current column
  1643. *
  1644. * @return array 3 element array - $single_sort_order, $sort_order, $order_img
  1645. *
  1646. * @access private
  1647. *
  1648. * @see _getOrderLinkAndSortedHeaderHtml()
  1649. */
  1650. private function _getSingleAndMultiSortUrls(
  1651. array $sort_expression, array $sort_expression_nodirection, $sort_tbl,
  1652. $name_to_use_in_sort, array $sort_direction, $fields_meta, $column_index
  1653. ) {
  1654. $sort_order = "";
  1655. // Check if the current column is in the order by clause
  1656. $is_in_sort = $this->_isInSorted(
  1657. $sort_expression, $sort_expression_nodirection,
  1658. $sort_tbl, $name_to_use_in_sort
  1659. );
  1660. $current_name = $name_to_use_in_sort;
  1661. if ($sort_expression_nodirection[0] == '' || !$is_in_sort) {
  1662. $special_index = $sort_expression_nodirection[0] == ''
  1663. ? 0
  1664. : count($sort_expression_nodirection);
  1665. $sort_expression_nodirection[$special_index]
  1666. = Util::backquote(
  1667. $current_name
  1668. );
  1669. $sort_direction[$special_index] = (preg_match(
  1670. '@time|date@i',
  1671. $fields_meta->type
  1672. )) ? self::DESCENDING_SORT_DIR : self::ASCENDING_SORT_DIR;
  1673. }
  1674. $sort_expression_nodirection = array_filter($sort_expression_nodirection);
  1675. $single_sort_order = null;
  1676. foreach ($sort_expression_nodirection as $index=>$expression) {
  1677. // check if this is the first clause,
  1678. // if it is then we have to add "order by"
  1679. $is_first_clause = ($index == 0);
  1680. $name_to_use_in_sort = $expression;
  1681. $sort_tbl_new = $sort_tbl;
  1682. // Test to detect if the column name is a standard name
  1683. // Standard name has the table name prefixed to the column name
  1684. if (mb_strpos($name_to_use_in_sort, '.') !== false) {
  1685. $matches = explode('.', $name_to_use_in_sort);
  1686. // Matches[0] has the table name
  1687. // Matches[1] has the column name
  1688. $name_to_use_in_sort = $matches[1];
  1689. $sort_tbl_new = $matches[0];
  1690. }
  1691. // $name_to_use_in_sort might contain a space due to
  1692. // formatting of function expressions like "COUNT(name )"
  1693. // so we remove the space in this situation
  1694. $name_to_use_in_sort = str_replace(' )', ')', $name_to_use_in_sort);
  1695. $name_to_use_in_sort = str_replace('``', '`', $name_to_use_in_sort);
  1696. $name_to_use_in_sort = trim($name_to_use_in_sort, '`');
  1697. // If this the first column name in the order by clause add
  1698. // order by clause to the column name
  1699. $query_head = $is_first_clause ? "\nORDER BY " : "";
  1700. // Again a check to see if the given column is a aggregate column
  1701. if (mb_strpos($name_to_use_in_sort, '(') !== false) {
  1702. $sort_order .= $query_head . $name_to_use_in_sort . ' ' ;
  1703. } else {
  1704. if (strlen($sort_tbl_new) > 0) {
  1705. $sort_tbl_new .= ".";
  1706. }
  1707. $sort_order .= $query_head . $sort_tbl_new
  1708. . Util::backquote(
  1709. $name_to_use_in_sort
  1710. ) . ' ' ;
  1711. }
  1712. // For a special case where the code generates two dots between
  1713. // column name and table name.
  1714. $sort_order = preg_replace("/\.\./", ".", $sort_order);
  1715. // Incase this is the current column save $single_sort_order
  1716. if ($current_name == $name_to_use_in_sort) {
  1717. if (mb_strpos($current_name, '(') !== false) {
  1718. $single_sort_order = "\n" . 'ORDER BY ' . Util::backquote($current_name) . ' ';
  1719. } else {
  1720. $single_sort_order = "\n" . 'ORDER BY ' . $sort_tbl
  1721. . Util::backquote(
  1722. $current_name
  1723. ) . ' ';
  1724. }
  1725. if ($is_in_sort) {
  1726. list($single_sort_order, $order_img)
  1727. = $this->_getSortingUrlParams(
  1728. $sort_direction, $single_sort_order, $index
  1729. );
  1730. } else {
  1731. $single_sort_order .= strtoupper($sort_direction[$index]);
  1732. }
  1733. }
  1734. if ($current_name == $name_to_use_in_sort && $is_in_sort) {
  1735. // We need to generate the arrow button and related html
  1736. list($sort_order, $order_img) = $this->_getSortingUrlParams(
  1737. $sort_direction, $sort_order, $index
  1738. );
  1739. $order_img .= " <small>" . ($index + 1) . "</small>";
  1740. } else {
  1741. $sort_order .= strtoupper($sort_direction[$index]);
  1742. }
  1743. // Separate columns by a comma
  1744. $sort_order .= ", ";
  1745. unset($name_to_use_in_sort);
  1746. }
  1747. // remove the comma from the last column name in the newly
  1748. // constructed clause
  1749. $sort_order = mb_substr(
  1750. $sort_order,
  1751. 0,
  1752. mb_strlen($sort_order) - 2
  1753. );
  1754. if (empty($order_img)) {
  1755. $order_img = '';
  1756. }
  1757. return array($single_sort_order, $sort_order, $order_img);
  1758. }
  1759. /**
  1760. * Check whether the column is sorted
  1761. *
  1762. * @param array $sort_expression sort expression
  1763. * @param array $sort_expression_nodirection sort expression without direction
  1764. * @param string $sort_tbl the table name
  1765. * @param string $name_to_use_in_sort the sorting column name
  1766. *
  1767. * @return boolean $is_in_sort the column sorted or not
  1768. *
  1769. * @access private
  1770. *
  1771. * @see _getTableHeaders()
  1772. */
  1773. private function _isInSorted(
  1774. array $sort_expression, array $sort_expression_nodirection, $sort_tbl,
  1775. $name_to_use_in_sort
  1776. ) {
  1777. $index_in_expression = 0;
  1778. foreach ($sort_expression_nodirection as $index => $clause) {
  1779. if (mb_strpos($clause, '.') !== false) {
  1780. $fragments = explode('.', $clause);
  1781. $clause2 = $fragments[0] . "." . str_replace('`', '', $fragments[1]);
  1782. } else {
  1783. $clause2 = $sort_tbl . str_replace('`', '', $clause);
  1784. }
  1785. if ($clause2 === $sort_tbl . $name_to_use_in_sort) {
  1786. $index_in_expression = $index;
  1787. break;
  1788. }
  1789. }
  1790. if (empty($sort_expression[$index_in_expression])) {
  1791. $is_in_sort = false;
  1792. } else {
  1793. // Field name may be preceded by a space, or any number
  1794. // of characters followed by a dot (tablename.fieldname)
  1795. // so do a direct comparison for the sort expression;
  1796. // this avoids problems with queries like
  1797. // "SELECT id, count(id)..." and clicking to sort
  1798. // on id or on count(id).
  1799. // Another query to test this:
  1800. // SELECT p.*, FROM_UNIXTIME(p.temps) FROM mytable AS p
  1801. // (and try clicking on each column's header twice)
  1802. $noSortTable = empty($sort_tbl) || mb_strpos(
  1803. $sort_expression_nodirection[$index_in_expression], $sort_tbl
  1804. ) === false;
  1805. $noOpenParenthesis = mb_strpos(
  1806. $sort_expression_nodirection[$index_in_expression], '('
  1807. ) === false;
  1808. if (! empty($sort_tbl) && $noSortTable && $noOpenParenthesis) {
  1809. $new_sort_expression_nodirection = $sort_tbl
  1810. . $sort_expression_nodirection[$index_in_expression];
  1811. } else {
  1812. $new_sort_expression_nodirection
  1813. = $sort_expression_nodirection[$index_in_expression];
  1814. }
  1815. //Back quotes are removed in next comparison, so remove them from value
  1816. //to compare.
  1817. $name_to_use_in_sort = str_replace('`', '', $name_to_use_in_sort);
  1818. $is_in_sort = false;
  1819. $sort_name = str_replace('`', '', $sort_tbl) . $name_to_use_in_sort;
  1820. if ($sort_name == str_replace('`', '', $new_sort_expression_nodirection)
  1821. || $sort_name == str_replace('`', '', $sort_expression_nodirection[$index_in_expression])
  1822. ) {
  1823. $is_in_sort = true;
  1824. }
  1825. }
  1826. return $is_in_sort;
  1827. } // end of the '_isInSorted()' function
  1828. /**
  1829. * Get sort url parameters - sort order and order image
  1830. *
  1831. * @param array $sort_direction the sort direction
  1832. * @param string $sort_order the sorting order
  1833. * @param integer $index the index of sort direction array.
  1834. *
  1835. * @return array 2 element array - $sort_order, $order_img
  1836. *
  1837. * @access private
  1838. *
  1839. * @see _getSingleAndMultiSortUrls()
  1840. */
  1841. private function _getSortingUrlParams(array $sort_direction, $sort_order, $index)
  1842. {
  1843. if (strtoupper(trim($sort_direction[$index])) == self::DESCENDING_SORT_DIR) {
  1844. $sort_order .= ' ASC';
  1845. $order_img = ' ' . Util::getImage(
  1846. 's_desc', __('Descending'),
  1847. array('class' => "soimg", 'title' => '')
  1848. );
  1849. $order_img .= ' ' . Util::getImage(
  1850. 's_asc', __('Ascending'),
  1851. array('class' => "soimg hide", 'title' => '')
  1852. );
  1853. } else {
  1854. $sort_order .= ' DESC';
  1855. $order_img = ' ' . Util::getImage(
  1856. 's_asc', __('Ascending'),
  1857. array('class' => "soimg", 'title' => '')
  1858. );
  1859. $order_img .= ' ' . Util::getImage(
  1860. 's_desc', __('Descending'),
  1861. array('class' => "soimg hide", 'title' => '')
  1862. );
  1863. }
  1864. return array($sort_order, $order_img);
  1865. } // end of the '_getSortingUrlParams()' function
  1866. /**
  1867. * Get sort order link
  1868. *
  1869. * @param string $order_img the sort order image
  1870. * @param array $fields_meta set of field properties
  1871. * @param array $order_url_params the url params for sort
  1872. * @param array $multi_order_url_params the url params for sort
  1873. *
  1874. * @return string the sort order link
  1875. *
  1876. * @access private
  1877. *
  1878. * @see _getTableHeaders()
  1879. */
  1880. private function _getSortOrderLink(
  1881. $order_img, $fields_meta, $order_url_params, $multi_order_url_params
  1882. ) {
  1883. $order_link_params = array(
  1884. 'class' => 'sortlink'
  1885. );
  1886. $order_link_content = htmlspecialchars($fields_meta->name);
  1887. $inner_link_content = $order_link_content . $order_img
  1888. . '<input type="hidden" value="sql.php'
  1889. . Url::getCommon($multi_order_url_params, '?', false)
  1890. . '" />';
  1891. return Util::linkOrButton(
  1892. 'sql.php', $order_url_params, $inner_link_content, $order_link_params
  1893. );
  1894. } // end of the '_getSortOrderLink()' function
  1895. /**
  1896. * Check if the column contains numeric data. If yes, then set the
  1897. * column header's alignment right
  1898. *
  1899. * @param array $fields_meta set of field properties
  1900. * @param array &$th_class array containing classes
  1901. *
  1902. * @return void
  1903. *
  1904. * @see _getDraggableClassForSortableColumns()
  1905. */
  1906. private function _getClassForNumericColumnType($fields_meta, array &$th_class)
  1907. {
  1908. if (preg_match(
  1909. '@int|decimal|float|double|real|bit|boolean|serial@i',
  1910. $fields_meta->type
  1911. )) {
  1912. $th_class[] = 'right';
  1913. }
  1914. }
  1915. /**
  1916. * Prepare columns to draggable effect for sortable columns
  1917. *
  1918. * @param boolean $col_visib the column is visible (false)
  1919. * array the column is not visible (string array)
  1920. * @param string $col_visib_j element of $col_visib array
  1921. * @param array $fields_meta set of field properties
  1922. * @param string $order_link the order link
  1923. * @param string $comments the comment for the column
  1924. *
  1925. * @return string $draggable_html html content
  1926. *
  1927. * @access private
  1928. *
  1929. * @see _getTableHeaders()
  1930. */
  1931. private function _getDraggableClassForSortableColumns(
  1932. $col_visib, $col_visib_j, $fields_meta,
  1933. $order_link, $comments
  1934. ) {
  1935. $draggable_html = '<th';
  1936. $th_class = array();
  1937. $th_class[] = 'draggable';
  1938. $this->_getClassForNumericColumnType($fields_meta, $th_class);
  1939. if ($col_visib && !$col_visib_j) {
  1940. $th_class[] = 'hide';
  1941. }
  1942. $th_class[] = 'column_heading';
  1943. if ($GLOBALS['cfg']['BrowsePointerEnable'] == true) {
  1944. $th_class[] = 'pointer';
  1945. }
  1946. if ($GLOBALS['cfg']['BrowseMarkerEnable'] == true) {
  1947. $th_class[] = 'marker';
  1948. }
  1949. $draggable_html .= ' class="' . implode(' ', $th_class) . '"';
  1950. $draggable_html .= ' data-column="' . htmlspecialchars($fields_meta->name)
  1951. . '">' . $order_link . $comments . '</th>';
  1952. return $draggable_html;
  1953. } // end of the '_getDraggableClassForSortableColumns()' function
  1954. /**
  1955. * Prepare columns to draggable effect for non sortable columns
  1956. *
  1957. * @param boolean $col_visib the column is visible (false)
  1958. * array the column is not visible (string array)
  1959. * @param string $col_visib_j element of $col_visib array
  1960. * @param boolean $condition_field whether to add CSS class condition
  1961. * @param array $fields_meta set of field properties
  1962. * @param string $comments the comment for the column
  1963. *
  1964. * @return string $draggable_html html content
  1965. *
  1966. * @access private
  1967. *
  1968. * @see _getTableHeaders()
  1969. */
  1970. private function _getDraggableClassForNonSortableColumns(
  1971. $col_visib, $col_visib_j, $condition_field,
  1972. $fields_meta, $comments
  1973. ) {
  1974. $draggable_html = '<th';
  1975. $th_class = array();
  1976. $th_class[] = 'draggable';
  1977. $this->_getClassForNumericColumnType($fields_meta, $th_class);
  1978. if ($col_visib && !$col_visib_j) {
  1979. $th_class[] = 'hide';
  1980. }
  1981. if ($condition_field) {
  1982. $th_class[] = 'condition';
  1983. }
  1984. $draggable_html .= ' class="' . implode(' ', $th_class) . '"';
  1985. $draggable_html .= ' data-column="'
  1986. . htmlspecialchars($fields_meta->name) . '">';
  1987. $draggable_html .= htmlspecialchars($fields_meta->name);
  1988. $draggable_html .= "\n" . $comments . '</th>';
  1989. return $draggable_html;
  1990. } // end of the '_getDraggableClassForNonSortableColumns()' function
  1991. /**
  1992. * Prepare column to show at right side - check boxes or empty column
  1993. *
  1994. * @param array &$displayParts which elements to display
  1995. * @param string $full_or_partial_text_link full/partial link or text button
  1996. * @param string $colspan column span of table header
  1997. *
  1998. * @return string html content
  1999. *
  2000. * @access private
  2001. *
  2002. * @see _getTableHeaders()
  2003. */
  2004. private function _getColumnAtRightSide(
  2005. array &$displayParts, $full_or_partial_text_link, $colspan
  2006. ) {
  2007. $right_column_html = '';
  2008. $display_params = $this->__get('display_params');
  2009. // Displays the needed checkboxes at the right
  2010. // column of the result table header if possible and required...
  2011. if ((($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_RIGHT)
  2012. || ($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_BOTH))
  2013. && (($displayParts['edit_lnk'] != self::NO_EDIT_OR_DELETE)
  2014. || ($displayParts['del_lnk'] != self::NO_EDIT_OR_DELETE))
  2015. && ($displayParts['text_btn'] == '1')
  2016. ) {
  2017. $display_params['emptyafter']
  2018. = (($displayParts['edit_lnk'] != self::NO_EDIT_OR_DELETE)
  2019. && ($displayParts['del_lnk'] != self::NO_EDIT_OR_DELETE)) ? 4 : 1;
  2020. $right_column_html .= "\n"
  2021. . '<th class="column_action print_ignore" ' . $colspan . '>'
  2022. . $full_or_partial_text_link
  2023. . '</th>';
  2024. } elseif ((($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_LEFT)
  2025. || ($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_BOTH))
  2026. && (($displayParts['edit_lnk'] == self::NO_EDIT_OR_DELETE)
  2027. && ($displayParts['del_lnk'] == self::NO_EDIT_OR_DELETE))
  2028. && (! isset($GLOBALS['is_header_sent']) || ! $GLOBALS['is_header_sent'])
  2029. ) {
  2030. // ... elseif no button, displays empty columns if required
  2031. // (unless coming from Browse mode print view)
  2032. $display_params['emptyafter']
  2033. = (($displayParts['edit_lnk'] != self::NO_EDIT_OR_DELETE)
  2034. && ($displayParts['del_lnk'] != self::NO_EDIT_OR_DELETE)) ? 4 : 1;
  2035. $right_column_html .= "\n" . '<td class="print_ignore" ' . $colspan
  2036. . '></td>';
  2037. }
  2038. $this->__set('display_params', $display_params);
  2039. return $right_column_html;
  2040. } // end of the '_getColumnAtRightSide()' function
  2041. /**
  2042. * Prepares the display for a value
  2043. *
  2044. * @param string $class class of table cell
  2045. * @param bool $conditionField whether to add CSS class condition
  2046. * @param string $value value to display
  2047. *
  2048. * @return string the td
  2049. *
  2050. * @access private
  2051. *
  2052. * @see _getDataCellForGeometryColumns(),
  2053. * _getDataCellForNonNumericColumns()
  2054. */
  2055. private function _buildValueDisplay($class, $conditionField, $value)
  2056. {
  2057. return Template::get('display/results/value_display')->render([
  2058. 'class' => $class,
  2059. 'condition_field' => $conditionField,
  2060. 'value' => $value,
  2061. ]);
  2062. }
  2063. /**
  2064. * Prepares the display for a null value
  2065. *
  2066. * @param string $class class of table cell
  2067. * @param bool $conditionField whether to add CSS class condition
  2068. * @param object $meta the meta-information about this field
  2069. * @param string $align cell alignment
  2070. *
  2071. * @return string the td
  2072. *
  2073. * @access private
  2074. *
  2075. * @see _getDataCellForNumericColumns(),
  2076. * _getDataCellForGeometryColumns(),
  2077. * _getDataCellForNonNumericColumns()
  2078. */
  2079. private function _buildNullDisplay($class, $conditionField, $meta, $align = '')
  2080. {
  2081. $classes = $this->_addClass($class, $conditionField, $meta, '');
  2082. return Template::get('display/results/null_display')->render([
  2083. 'align' => $align,
  2084. 'meta' => $meta,
  2085. 'classes' => $classes,
  2086. ]);
  2087. }
  2088. /**
  2089. * Prepares the display for an empty value
  2090. *
  2091. * @param string $class class of table cell
  2092. * @param bool $conditionField whether to add CSS class condition
  2093. * @param object $meta the meta-information about this field
  2094. * @param string $align cell alignment
  2095. *
  2096. * @return string the td
  2097. *
  2098. * @access private
  2099. *
  2100. * @see _getDataCellForNumericColumns(),
  2101. * _getDataCellForGeometryColumns(),
  2102. * _getDataCellForNonNumericColumns()
  2103. */
  2104. private function _buildEmptyDisplay($class, $conditionField, $meta, $align = '')
  2105. {
  2106. $classes = $this->_addClass($class, $conditionField, $meta, 'nowrap');
  2107. return Template::get('display/results/empty_display')->render([
  2108. 'align' => $align,
  2109. 'classes' => $classes,
  2110. ]);
  2111. }
  2112. /**
  2113. * Adds the relevant classes.
  2114. *
  2115. * @param string $class class of table cell
  2116. * @param bool $condition_field whether to add CSS class
  2117. * condition
  2118. * @param object $meta the meta-information about the
  2119. * field
  2120. * @param string $nowrap avoid wrapping
  2121. * @param bool $is_field_truncated is field truncated (display ...)
  2122. * @param object|string $transformation_plugin transformation plugin.
  2123. * Can also be the default function:
  2124. * Core::mimeDefaultFunction
  2125. * @param string $default_function default transformation function
  2126. *
  2127. * @return string the list of classes
  2128. *
  2129. * @access private
  2130. *
  2131. * @see _buildNullDisplay(), _getRowData()
  2132. */
  2133. private function _addClass(
  2134. $class, $condition_field, $meta, $nowrap, $is_field_truncated = false,
  2135. $transformation_plugin = '', $default_function = ''
  2136. ) {
  2137. $classes = array(
  2138. $class,
  2139. $nowrap,
  2140. );
  2141. if (isset($meta->mimetype)) {
  2142. $classes[] = preg_replace('/\//', '_', $meta->mimetype);
  2143. }
  2144. if ($condition_field) {
  2145. $classes[] = 'condition';
  2146. }
  2147. if ($is_field_truncated) {
  2148. $classes[] = 'truncated';
  2149. }
  2150. $mime_map = $this->__get('mime_map');
  2151. $orgFullColName = $this->__get('db') . '.' . $meta->orgtable
  2152. . '.' . $meta->orgname;
  2153. if ($transformation_plugin != $default_function
  2154. || !empty($mime_map[$orgFullColName]['input_transformation'])
  2155. ) {
  2156. $classes[] = 'transformed';
  2157. }
  2158. // Define classes to be added to this data field based on the type of data
  2159. $matches = array(
  2160. 'enum' => 'enum',
  2161. 'set' => 'set',
  2162. 'binary' => 'hex',
  2163. );
  2164. foreach ($matches as $key => $value) {
  2165. if (mb_strpos($meta->flags, $key) !== false) {
  2166. $classes[] = $value;
  2167. }
  2168. }
  2169. if (mb_strpos($meta->type, 'bit') !== false) {
  2170. $classes[] = 'bit';
  2171. }
  2172. return implode(' ', $classes);
  2173. } // end of the '_addClass()' function
  2174. /**
  2175. * Prepare the body of the results table
  2176. *
  2177. * @param integer &$dt_result the link id associated to the query
  2178. * which results have to be displayed which
  2179. * results have to be displayed
  2180. * @param array &$displayParts which elements to display
  2181. * @param array $map the list of relations
  2182. * @param array $analyzed_sql_results analyzed sql results
  2183. * @param boolean $is_limited_display with limited operations or not
  2184. *
  2185. * @return string $table_body_html html content
  2186. *
  2187. * @global array $row current row data
  2188. *
  2189. * @access private
  2190. *
  2191. * @see getTable()
  2192. */
  2193. private function _getTableBody(
  2194. &$dt_result, array &$displayParts, array $map, array $analyzed_sql_results,
  2195. $is_limited_display = false
  2196. ) {
  2197. global $row; // mostly because of browser transformations,
  2198. // to make the row-data accessible in a plugin
  2199. $table_body_html = '';
  2200. // query without conditions to shorten URLs when needed, 200 is just
  2201. // guess, it should depend on remaining URL length
  2202. $url_sql_query = $this->_getUrlSqlQuery($analyzed_sql_results);
  2203. $display_params = $this->__get('display_params');
  2204. if (! is_array($map)) {
  2205. $map = array();
  2206. }
  2207. $row_no = 0;
  2208. $display_params['edit'] = array();
  2209. $display_params['copy'] = array();
  2210. $display_params['delete'] = array();
  2211. $display_params['data'] = array();
  2212. $display_params['row_delete'] = array();
  2213. $this->__set('display_params', $display_params);
  2214. // name of the class added to all grid editable elements;
  2215. // if we don't have all the columns of a unique key in the result set,
  2216. // do not permit grid editing
  2217. if ($is_limited_display || ! $this->__get('editable')) {
  2218. $grid_edit_class = '';
  2219. } else {
  2220. switch ($GLOBALS['cfg']['GridEditing']) {
  2221. case 'double-click':
  2222. // trying to reduce generated HTML by using shorter
  2223. // classes like click1 and click2
  2224. $grid_edit_class = 'grid_edit click2';
  2225. break;
  2226. case 'click':
  2227. $grid_edit_class = 'grid_edit click1';
  2228. break;
  2229. default: // 'disabled'
  2230. $grid_edit_class = '';
  2231. break;
  2232. }
  2233. }
  2234. // prepare to get the column order, if available
  2235. list($col_order, $col_visib) = $this->_getColumnParams(
  2236. $analyzed_sql_results
  2237. );
  2238. // Correction University of Virginia 19991216 in the while below
  2239. // Previous code assumed that all tables have keys, specifically that
  2240. // the phpMyAdmin GUI should support row delete/edit only for such
  2241. // tables.
  2242. // Although always using keys is arguably the prescribed way of
  2243. // defining a relational table, it is not required. This will in
  2244. // particular be violated by the novice.
  2245. // We want to encourage phpMyAdmin usage by such novices. So the code
  2246. // below has been changed to conditionally work as before when the
  2247. // table being displayed has one or more keys; but to display
  2248. // delete/edit options correctly for tables without keys.
  2249. $whereClauseMap = $this->__get('whereClauseMap');
  2250. while ($row = $GLOBALS['dbi']->fetchRow($dt_result)) {
  2251. // add repeating headers
  2252. if ((($row_no != 0) && ($_SESSION['tmpval']['repeat_cells'] != 0))
  2253. && !($row_no % $_SESSION['tmpval']['repeat_cells'])
  2254. ) {
  2255. $table_body_html .= $this->_getRepeatingHeaders(
  2256. $display_params
  2257. );
  2258. }
  2259. $tr_class = array();
  2260. if ($GLOBALS['cfg']['BrowsePointerEnable'] != true) {
  2261. $tr_class[] = 'nopointer';
  2262. }
  2263. if ($GLOBALS['cfg']['BrowseMarkerEnable'] != true) {
  2264. $tr_class[] = 'nomarker';
  2265. }
  2266. // pointer code part
  2267. $classes = (empty($tr_class) ? ' ' : 'class="' . implode(' ', $tr_class) . '"');
  2268. $table_body_html .= '<tr ' . $classes . ' >';
  2269. // 1. Prepares the row
  2270. // In print view these variable needs to be initialized
  2271. $del_url = $del_str = $edit_anchor_class = $editCopyUrlParams = $delUrlParams
  2272. = $edit_str = $js_conf = $copy_url = $copy_str = $edit_url = null;
  2273. // 1.2 Defines the URLs for the modify/delete link(s)
  2274. if (($displayParts['edit_lnk'] != self::NO_EDIT_OR_DELETE)
  2275. || ($displayParts['del_lnk'] != self::NO_EDIT_OR_DELETE)
  2276. ) {
  2277. // Results from a "SELECT" statement -> builds the
  2278. // WHERE clause to use in links (a unique key if possible)
  2279. /**
  2280. * @todo $where_clause could be empty, for example a table
  2281. * with only one field and it's a BLOB; in this case,
  2282. * avoid to display the delete and edit links
  2283. */
  2284. list($where_clause, $clause_is_unique, $condition_array)
  2285. = Util::getUniqueCondition(
  2286. $dt_result, // handle
  2287. $this->__get('fields_cnt'), // fields_cnt
  2288. $this->__get('fields_meta'), // fields_meta
  2289. $row, // row
  2290. false, // force_unique
  2291. $this->__get('table'), // restrict_to_table
  2292. $analyzed_sql_results // analyzed_sql_results
  2293. );
  2294. $whereClauseMap[$row_no][$this->__get('table')] = $where_clause;
  2295. $this->__set('whereClauseMap', $whereClauseMap);
  2296. $where_clause_html = htmlspecialchars($where_clause);
  2297. // 1.2.1 Modify link(s) - update row case
  2298. if ($displayParts['edit_lnk'] == self::UPDATE_ROW) {
  2299. list($edit_url, $copy_url, $edit_str, $copy_str,
  2300. $edit_anchor_class, $editCopyUrlParams)
  2301. = $this->_getModifiedLinks(
  2302. $where_clause,
  2303. $clause_is_unique, $url_sql_query
  2304. );
  2305. } // end if (1.2.1)
  2306. // 1.2.2 Delete/Kill link(s)
  2307. list($del_url, $del_str, $js_conf, $delUrlParams)
  2308. = $this->_getDeleteAndKillLinks(
  2309. $where_clause, $clause_is_unique,
  2310. $url_sql_query, $displayParts['del_lnk'],
  2311. $row
  2312. );
  2313. // 1.3 Displays the links at left if required
  2314. if (($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_LEFT)
  2315. || ($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_BOTH)
  2316. ) {
  2317. $table_body_html .= $this->_getPlacedLinks(
  2318. self::POSITION_LEFT, $del_url, $displayParts, $row_no,
  2319. $where_clause, $where_clause_html, $condition_array,
  2320. $edit_url, $copy_url, $edit_anchor_class,
  2321. $edit_str, $copy_str, $del_str, $js_conf, $editCopyUrlParams, $delUrlParams
  2322. );
  2323. } elseif ($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_NONE) {
  2324. $table_body_html .= $this->_getPlacedLinks(
  2325. self::POSITION_NONE, $del_url, $displayParts, $row_no,
  2326. $where_clause, $where_clause_html, $condition_array,
  2327. $edit_url, $copy_url, $edit_anchor_class,
  2328. $edit_str, $copy_str, $del_str, $js_conf, $editCopyUrlParams, $delUrlParams
  2329. );
  2330. } // end if (1.3)
  2331. } // end if (1)
  2332. // 2. Displays the rows' values
  2333. if (is_null($this->__get('mime_map'))) {
  2334. $this->_setMimeMap();
  2335. }
  2336. $table_body_html .= $this->_getRowValues(
  2337. $dt_result,
  2338. $row,
  2339. $row_no,
  2340. $col_order,
  2341. $map,
  2342. $grid_edit_class,
  2343. $col_visib,
  2344. $url_sql_query,
  2345. $analyzed_sql_results
  2346. );
  2347. // 3. Displays the modify/delete links on the right if required
  2348. if (($displayParts['edit_lnk'] != self::NO_EDIT_OR_DELETE)
  2349. || ($displayParts['del_lnk'] != self::NO_EDIT_OR_DELETE)
  2350. ) {
  2351. if (($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_RIGHT)
  2352. || ($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_BOTH)
  2353. ) {
  2354. $table_body_html .= $this->_getPlacedLinks(
  2355. self::POSITION_RIGHT, $del_url, $displayParts, $row_no,
  2356. $where_clause, $where_clause_html, $condition_array,
  2357. $edit_url, $copy_url, $edit_anchor_class,
  2358. $edit_str, $copy_str, $del_str, $js_conf, $editCopyUrlParams, $delUrlParams
  2359. );
  2360. }
  2361. } // end if (3)
  2362. $table_body_html .= '</tr>';
  2363. $table_body_html .= "\n";
  2364. $row_no++;
  2365. } // end while
  2366. return $table_body_html;
  2367. } // end of the '_getTableBody()' function
  2368. /**
  2369. * Sets the MIME details of the columns in the results set
  2370. *
  2371. * @return void
  2372. */
  2373. private function _setMimeMap()
  2374. {
  2375. $fields_meta = $this->__get('fields_meta');
  2376. $mimeMap = array();
  2377. $added = array();
  2378. for ($currentColumn = 0;
  2379. $currentColumn < $this->__get('fields_cnt');
  2380. ++$currentColumn) {
  2381. $meta = $fields_meta[$currentColumn];
  2382. $orgFullTableName = $this->__get('db') . '.' . $meta->orgtable;
  2383. if ($GLOBALS['cfgRelation']['commwork']
  2384. && $GLOBALS['cfgRelation']['mimework']
  2385. && $GLOBALS['cfg']['BrowseMIME']
  2386. && ! $_SESSION['tmpval']['hide_transformation']
  2387. && empty($added[$orgFullTableName])
  2388. ) {
  2389. $mimeMap = array_merge(
  2390. $mimeMap,
  2391. Transformations::getMIME($this->__get('db'), $meta->orgtable, false, true)
  2392. );
  2393. $added[$orgFullTableName] = true;
  2394. }
  2395. }
  2396. // special browser transformation for some SHOW statements
  2397. if ($this->__get('is_show')
  2398. && ! $_SESSION['tmpval']['hide_transformation']
  2399. ) {
  2400. preg_match(
  2401. '@^SHOW[[:space:]]+(VARIABLES|(FULL[[:space:]]+)?'
  2402. . 'PROCESSLIST|STATUS|TABLE|GRANTS|CREATE|LOGS|DATABASES|FIELDS'
  2403. . ')@i',
  2404. $this->__get('sql_query'), $which
  2405. );
  2406. if (isset($which[1])) {
  2407. $str = ' ' . strtoupper($which[1]);
  2408. $isShowProcessList = strpos($str, 'PROCESSLIST') > 0;
  2409. if ($isShowProcessList) {
  2410. $mimeMap['..Info'] = array(
  2411. 'mimetype' => 'Text_Plain',
  2412. 'transformation' => 'output/Text_Plain_Sql.php',
  2413. );
  2414. }
  2415. $isShowCreateTable = preg_match(
  2416. '@CREATE[[:space:]]+TABLE@i', $this->__get('sql_query')
  2417. );
  2418. if ($isShowCreateTable) {
  2419. $mimeMap['..Create Table'] = array(
  2420. 'mimetype' => 'Text_Plain',
  2421. 'transformation' => 'output/Text_Plain_Sql.php',
  2422. );
  2423. }
  2424. }
  2425. }
  2426. $this->__set('mime_map', $mimeMap);
  2427. }
  2428. /**
  2429. * Get the values for one data row
  2430. *
  2431. * @param integer &$dt_result the link id associated to
  2432. * the query which results
  2433. * have to be displayed which
  2434. * results have to be
  2435. * displayed
  2436. * @param array $row current row data
  2437. * @param integer $row_no the index of current row
  2438. * @param array|boolean $col_order the column order false when
  2439. * a property not found false
  2440. * when a property not found
  2441. * @param array $map the list of relations
  2442. * @param string $grid_edit_class the class for all editable
  2443. * columns
  2444. * @param boolean|array|string $col_visib column is visible(false);
  2445. * column isn't visible(string
  2446. * array)
  2447. * @param string $url_sql_query the analyzed sql query
  2448. * @param array $analyzed_sql_results analyzed sql results
  2449. *
  2450. * @return string $row_values_html html content
  2451. *
  2452. * @access private
  2453. *
  2454. * @see _getTableBody()
  2455. */
  2456. private function _getRowValues(
  2457. &$dt_result, array $row, $row_no, $col_order, array $map,
  2458. $grid_edit_class, $col_visib,
  2459. $url_sql_query, array $analyzed_sql_results
  2460. ) {
  2461. $row_values_html = '';
  2462. // Following variable are needed for use in isset/empty or
  2463. // use with array indexes/safe use in foreach
  2464. $sql_query = $this->__get('sql_query');
  2465. $fields_meta = $this->__get('fields_meta');
  2466. $highlight_columns = $this->__get('highlight_columns');
  2467. $mime_map = $this->__get('mime_map');
  2468. $row_info = $this->_getRowInfoForSpecialLinks($row, $col_order);
  2469. $whereClauseMap = $this->__get('whereClauseMap');
  2470. $columnCount = $this->__get('fields_cnt');
  2471. for ($currentColumn = 0;
  2472. $currentColumn < $columnCount;
  2473. ++$currentColumn) {
  2474. // assign $i with appropriate column order
  2475. $i = $col_order ? $col_order[$currentColumn] : $currentColumn;
  2476. $meta = $fields_meta[$i];
  2477. $orgFullColName
  2478. = $this->__get('db') . '.' . $meta->orgtable . '.' . $meta->orgname;
  2479. $not_null_class = $meta->not_null ? 'not_null' : '';
  2480. $relation_class = isset($map[$meta->name]) ? 'relation' : '';
  2481. $hide_class = ($col_visib && ! $col_visib[$currentColumn])
  2482. ? 'hide'
  2483. : '';
  2484. $grid_edit = $meta->orgtable != '' ? $grid_edit_class : '';
  2485. // handle datetime-related class, for grid editing
  2486. $field_type_class
  2487. = $this->_getClassForDateTimeRelatedFields($meta->type);
  2488. $is_field_truncated = false;
  2489. // combine all the classes applicable to this column's value
  2490. $class = $this->_getClassesForColumn(
  2491. $grid_edit, $not_null_class, $relation_class,
  2492. $hide_class, $field_type_class
  2493. );
  2494. // See if this column should get highlight because it's used in the
  2495. // where-query.
  2496. $condition_field = (isset($highlight_columns)
  2497. && (isset($highlight_columns[$meta->name])
  2498. || isset($highlight_columns[Util::backquote($meta->name)])))
  2499. ? true
  2500. : false;
  2501. // Wrap MIME-transformations. [MIME]
  2502. $default_function = [Core::class, 'mimeDefaultFunction']; // default_function
  2503. $transformation_plugin = $default_function;
  2504. $transform_options = array();
  2505. if ($GLOBALS['cfgRelation']['mimework']
  2506. && $GLOBALS['cfg']['BrowseMIME']
  2507. ) {
  2508. if (isset($mime_map[$orgFullColName]['mimetype'])
  2509. && !empty($mime_map[$orgFullColName]['transformation'])
  2510. ) {
  2511. $file = $mime_map[$orgFullColName]['transformation'];
  2512. $include_file = 'libraries/classes/Plugins/Transformations/' . $file;
  2513. if (@file_exists($include_file)) {
  2514. $class_name = Transformations::getClassName($include_file);
  2515. if (class_exists($class_name)) {
  2516. // todo add $plugin_manager
  2517. $plugin_manager = null;
  2518. $transformation_plugin = new $class_name(
  2519. $plugin_manager
  2520. );
  2521. $transform_options = Transformations::getOptions(
  2522. isset(
  2523. $mime_map[$orgFullColName]
  2524. ['transformation_options']
  2525. )
  2526. ? $mime_map[$orgFullColName]
  2527. ['transformation_options']
  2528. : ''
  2529. );
  2530. $meta->mimetype = str_replace(
  2531. '_', '/',
  2532. $mime_map[$orgFullColName]['mimetype']
  2533. );
  2534. }
  2535. } // end if file_exists
  2536. } // end if transformation is set
  2537. } // end if mime/transformation works.
  2538. // Check whether the field needs to display with syntax highlighting
  2539. $dbLower = mb_strtolower($this->__get('db'));
  2540. $tblLower = mb_strtolower($meta->orgtable);
  2541. $nameLower = mb_strtolower($meta->orgname);
  2542. if (! empty($this->transformation_info[$dbLower][$tblLower][$nameLower])
  2543. && (trim($row[$i]) != '')
  2544. && ! $_SESSION['tmpval']['hide_transformation']
  2545. ) {
  2546. include_once $this->transformation_info
  2547. [$dbLower][$tblLower][$nameLower][0];
  2548. $transformation_plugin = new $this->transformation_info
  2549. [$dbLower][$tblLower][$nameLower][1](null);
  2550. $transform_options = Transformations::getOptions(
  2551. isset($mime_map[$orgFullColName]['transformation_options'])
  2552. ? $mime_map[$orgFullColName]['transformation_options']
  2553. : ''
  2554. );
  2555. $meta->mimetype = str_replace(
  2556. '_', '/',
  2557. $this->transformation_info[$dbLower]
  2558. [mb_strtolower($meta->orgtable)]
  2559. [mb_strtolower($meta->orgname)][2]
  2560. );
  2561. }
  2562. // Check for the predefined fields need to show as link in schemas
  2563. include_once 'libraries/special_schema_links.inc.php';
  2564. if (isset($GLOBALS['special_schema_links'])
  2565. && (! empty($GLOBALS['special_schema_links'][$dbLower][$tblLower][$nameLower]))
  2566. ) {
  2567. $linking_url = $this->_getSpecialLinkUrl(
  2568. $row[$i], $row_info, mb_strtolower($meta->orgname)
  2569. );
  2570. $transformation_plugin = new Text_Plain_Link();
  2571. $transform_options = array(
  2572. 0 => $linking_url,
  2573. 2 => true
  2574. );
  2575. $meta->mimetype = str_replace(
  2576. '_', '/',
  2577. 'Text/Plain'
  2578. );
  2579. }
  2580. /*
  2581. * The result set can have columns from more than one table,
  2582. * this is why we have to check for the unique conditions
  2583. * related to this table; however getUniqueCondition() is
  2584. * costly and does not need to be called if we already know
  2585. * the conditions for the current table.
  2586. */
  2587. if (! isset($whereClauseMap[$row_no][$meta->orgtable])) {
  2588. $unique_conditions = Util::getUniqueCondition(
  2589. $dt_result, // handle
  2590. $this->__get('fields_cnt'), // fields_cnt
  2591. $this->__get('fields_meta'), // fields_meta
  2592. $row, // row
  2593. false, // force_unique
  2594. $meta->orgtable, // restrict_to_table
  2595. $analyzed_sql_results // analyzed_sql_results
  2596. );
  2597. $whereClauseMap[$row_no][$meta->orgtable] = $unique_conditions[0];
  2598. }
  2599. $_url_params = array(
  2600. 'db' => $this->__get('db'),
  2601. 'table' => $meta->orgtable,
  2602. 'where_clause_sign' => Core::signSqlQuery($whereClauseMap[$row_no][$meta->orgtable]),
  2603. 'where_clause' => $whereClauseMap[$row_no][$meta->orgtable],
  2604. 'transform_key' => $meta->orgname
  2605. );
  2606. if (! empty($sql_query)) {
  2607. $_url_params['sql_query'] = $url_sql_query;
  2608. }
  2609. $transform_options['wrapper_link'] = Url::getCommon($_url_params);
  2610. $display_params = $this->__get('display_params');
  2611. // in some situations (issue 11406), numeric returns 1
  2612. // even for a string type
  2613. // for decimal numeric is returning 1
  2614. // have to improve logic
  2615. if (($meta->numeric == 1 && $meta->type != 'string') || $meta->type == 'real') {
  2616. // n u m e r i c
  2617. $display_params['data'][$row_no][$i]
  2618. = $this->_getDataCellForNumericColumns(
  2619. $row[$i],
  2620. $class,
  2621. $condition_field,
  2622. $meta,
  2623. $map,
  2624. $is_field_truncated,
  2625. $analyzed_sql_results,
  2626. $transformation_plugin,
  2627. $default_function,
  2628. $transform_options
  2629. );
  2630. } elseif ($meta->type == self::GEOMETRY_FIELD) {
  2631. // g e o m e t r y
  2632. // Remove 'grid_edit' from $class as we do not allow to
  2633. // inline-edit geometry data.
  2634. $class = str_replace('grid_edit', '', $class);
  2635. $display_params['data'][$row_no][$i]
  2636. = $this->_getDataCellForGeometryColumns(
  2637. $row[$i],
  2638. $class,
  2639. $meta,
  2640. $map,
  2641. $_url_params,
  2642. $condition_field,
  2643. $transformation_plugin,
  2644. $default_function,
  2645. $transform_options,
  2646. $analyzed_sql_results
  2647. );
  2648. } else {
  2649. // n o t n u m e r i c
  2650. $display_params['data'][$row_no][$i]
  2651. = $this->_getDataCellForNonNumericColumns(
  2652. $row[$i],
  2653. $class,
  2654. $meta,
  2655. $map,
  2656. $_url_params,
  2657. $condition_field,
  2658. $transformation_plugin,
  2659. $default_function,
  2660. $transform_options,
  2661. $is_field_truncated,
  2662. $analyzed_sql_results,
  2663. $dt_result,
  2664. $i
  2665. );
  2666. }
  2667. // output stored cell
  2668. $row_values_html .= $display_params['data'][$row_no][$i];
  2669. if (isset($display_params['rowdata'][$i][$row_no])) {
  2670. $display_params['rowdata'][$i][$row_no]
  2671. .= $display_params['data'][$row_no][$i];
  2672. } else {
  2673. $display_params['rowdata'][$i][$row_no]
  2674. = $display_params['data'][$row_no][$i];
  2675. }
  2676. $this->__set('display_params', $display_params);
  2677. } // end for
  2678. return $row_values_html;
  2679. } // end of the '_getRowValues()' function
  2680. /**
  2681. * Get link for display special schema links
  2682. *
  2683. * @param string $column_value column value
  2684. * @param array $row_info information about row
  2685. * @param string $field_name column name
  2686. *
  2687. * @return string generated link
  2688. */
  2689. private function _getSpecialLinkUrl($column_value, array $row_info, $field_name)
  2690. {
  2691. $linking_url_params = array();
  2692. $link_relations = $GLOBALS['special_schema_links']
  2693. [mb_strtolower($this->__get('db'))]
  2694. [mb_strtolower($this->__get('table'))]
  2695. [$field_name];
  2696. if (! is_array($link_relations['link_param'])) {
  2697. $linking_url_params[$link_relations['link_param']] = $column_value;
  2698. } else {
  2699. // Consider only the case of creating link for column field
  2700. // sql query that needs to be passed as url param
  2701. $sql = 'SELECT `' . $column_value . '` FROM `'
  2702. . $row_info[$link_relations['link_param'][1]] . '`.`'
  2703. . $row_info[$link_relations['link_param'][2]] . '`';
  2704. $linking_url_params[$link_relations['link_param'][0]] = $sql;
  2705. }
  2706. $divider = strpos($link_relations['default_page'], '?') ? '&' : '?';
  2707. if (empty($link_relations['link_dependancy_params'])) {
  2708. return $link_relations['default_page']
  2709. . Url::getCommonRaw($linking_url_params, $divider);
  2710. }
  2711. foreach ($link_relations['link_dependancy_params'] as $new_param) {
  2712. // If param_info is an array, set the key and value
  2713. // from that array
  2714. if (is_array($new_param['param_info'])) {
  2715. $linking_url_params[$new_param['param_info'][0]]
  2716. = $new_param['param_info'][1];
  2717. continue;
  2718. }
  2719. $linking_url_params[$new_param['param_info']]
  2720. = $row_info[mb_strtolower($new_param['column_name'])];
  2721. // Special case 1 - when executing routines, according
  2722. // to the type of the routine, url param changes
  2723. if (empty($row_info['routine_type'])) {
  2724. continue;
  2725. }
  2726. }
  2727. return $link_relations['default_page']
  2728. . Url::getCommonRaw($linking_url_params, $divider);
  2729. }
  2730. /**
  2731. * Prepare row information for display special links
  2732. *
  2733. * @param array $row current row data
  2734. * @param array|boolean $col_order the column order
  2735. *
  2736. * @return array $row_info associative array with column nama -> value
  2737. */
  2738. private function _getRowInfoForSpecialLinks(array $row, $col_order)
  2739. {
  2740. $row_info = array();
  2741. $fields_meta = $this->__get('fields_meta');
  2742. for ($n = 0; $n < $this->__get('fields_cnt'); ++$n) {
  2743. $m = $col_order ? $col_order[$n] : $n;
  2744. $row_info[mb_strtolower($fields_meta[$m]->orgname)]
  2745. = $row[$m];
  2746. }
  2747. return $row_info;
  2748. }
  2749. /**
  2750. * Get url sql query without conditions to shorten URLs
  2751. *
  2752. * @param array $analyzed_sql_results analyzed sql results
  2753. *
  2754. * @return string $url_sql analyzed sql query
  2755. *
  2756. * @access private
  2757. *
  2758. * @see _getTableBody()
  2759. */
  2760. private function _getUrlSqlQuery(array $analyzed_sql_results)
  2761. {
  2762. if (($analyzed_sql_results['querytype'] != 'SELECT')
  2763. || (mb_strlen($this->__get('sql_query')) < 200)
  2764. ) {
  2765. return $this->__get('sql_query');
  2766. }
  2767. $query = 'SELECT ' . Query::getClause(
  2768. $analyzed_sql_results['statement'],
  2769. $analyzed_sql_results['parser']->list,
  2770. 'SELECT'
  2771. );
  2772. $from_clause = Query::getClause(
  2773. $analyzed_sql_results['statement'],
  2774. $analyzed_sql_results['parser']->list,
  2775. 'FROM'
  2776. );
  2777. if (!empty($from_clause)) {
  2778. $query .= ' FROM ' . $from_clause;
  2779. }
  2780. return $query;
  2781. } // end of the '_getUrlSqlQuery()' function
  2782. /**
  2783. * Get column order and column visibility
  2784. *
  2785. * @param array $analyzed_sql_results analyzed sql results
  2786. *
  2787. * @return array 2 element array - $col_order, $col_visib
  2788. *
  2789. * @access private
  2790. *
  2791. * @see _getTableBody()
  2792. */
  2793. private function _getColumnParams(array $analyzed_sql_results)
  2794. {
  2795. if ($this->_isSelect($analyzed_sql_results)) {
  2796. $pmatable = new Table($this->__get('table'), $this->__get('db'));
  2797. $col_order = $pmatable->getUiProp(Table::PROP_COLUMN_ORDER);
  2798. /* Validate the value */
  2799. if ($col_order !== false) {
  2800. $fields_cnt = $this->__get('fields_cnt');
  2801. foreach ($col_order as $value) {
  2802. if ($value >= $fields_cnt) {
  2803. $pmatable->removeUiProp(Table::PROP_COLUMN_ORDER);
  2804. $fields_cnt = false;
  2805. }
  2806. }
  2807. }
  2808. $col_visib = $pmatable->getUiProp(Table::PROP_COLUMN_VISIB);
  2809. } else {
  2810. $col_order = false;
  2811. $col_visib = false;
  2812. }
  2813. return array($col_order, $col_visib);
  2814. } // end of the '_getColumnParams()' function
  2815. /**
  2816. * Get HTML for repeating headers
  2817. *
  2818. * @param array $display_params holds various display info
  2819. *
  2820. * @return string $header_html html content
  2821. *
  2822. * @access private
  2823. *
  2824. * @see _getTableBody()
  2825. */
  2826. private function _getRepeatingHeaders(
  2827. array $display_params
  2828. ) {
  2829. $header_html = '<tr>' . "\n";
  2830. if ($display_params['emptypre'] > 0) {
  2831. $header_html .= ' <th colspan="'
  2832. . $display_params['emptypre'] . '">'
  2833. . "\n" . ' &nbsp;</th>' . "\n";
  2834. } elseif ($GLOBALS['cfg']['RowActionLinks'] == self::POSITION_NONE) {
  2835. $header_html .= ' <th></th>' . "\n";
  2836. }
  2837. foreach ($display_params['desc'] as $val) {
  2838. $header_html .= $val;
  2839. }
  2840. if ($display_params['emptyafter'] > 0) {
  2841. $header_html
  2842. .= ' <th colspan="' . $display_params['emptyafter']
  2843. . '">'
  2844. . "\n" . ' &nbsp;</th>' . "\n";
  2845. }
  2846. $header_html .= '</tr>' . "\n";
  2847. return $header_html;
  2848. } // end of the '_getRepeatingHeaders()' function
  2849. /**
  2850. * Get modified links
  2851. *
  2852. * @param string $where_clause the where clause of the sql
  2853. * @param boolean $clause_is_unique the unique condition of clause
  2854. * @param string $url_sql_query the analyzed sql query
  2855. *
  2856. * @return array 5 element array - $edit_url, $copy_url,
  2857. * $edit_str, $copy_str, $edit_anchor_class
  2858. *
  2859. * @access private
  2860. *
  2861. * @see _getTableBody()
  2862. */
  2863. private function _getModifiedLinks(
  2864. $where_clause, $clause_is_unique, $url_sql_query
  2865. ) {
  2866. $_url_params = array(
  2867. 'db' => $this->__get('db'),
  2868. 'table' => $this->__get('table'),
  2869. 'where_clause' => $where_clause,
  2870. 'clause_is_unique' => $clause_is_unique,
  2871. 'sql_query' => $url_sql_query,
  2872. 'goto' => 'sql.php',
  2873. );
  2874. $edit_url = 'tbl_change.php';
  2875. $copy_url = 'tbl_change.php';
  2876. $edit_str = $this->_getActionLinkContent(
  2877. 'b_edit', __('Edit')
  2878. );
  2879. $copy_str = $this->_getActionLinkContent(
  2880. 'b_insrow', __('Copy')
  2881. );
  2882. // Class definitions required for grid editing jQuery scripts
  2883. $edit_anchor_class = "edit_row_anchor";
  2884. if ($clause_is_unique == 0) {
  2885. $edit_anchor_class .= ' nonunique';
  2886. }
  2887. return array($edit_url, $copy_url, $edit_str, $copy_str, $edit_anchor_class, $_url_params);
  2888. } // end of the '_getModifiedLinks()' function
  2889. /**
  2890. * Get delete and kill links
  2891. *
  2892. * @param string $where_clause the where clause of the sql
  2893. * @param boolean $clause_is_unique the unique condition of clause
  2894. * @param string $url_sql_query the analyzed sql query
  2895. * @param string $del_lnk the delete link of current row
  2896. * @param array $row the current row
  2897. *
  2898. * @return array 3 element array
  2899. * $del_url, $del_str, $js_conf
  2900. *
  2901. * @access private
  2902. *
  2903. * @see _getTableBody()
  2904. */
  2905. private function _getDeleteAndKillLinks(
  2906. $where_clause, $clause_is_unique, $url_sql_query, $del_lnk, array $row
  2907. ) {
  2908. $goto = $this->__get('goto');
  2909. if ($del_lnk == self::DELETE_ROW) { // delete row case
  2910. $_url_params = array(
  2911. 'db' => $this->__get('db'),
  2912. 'table' => $this->__get('table'),
  2913. 'sql_query' => $url_sql_query,
  2914. 'message_to_show' => __('The row has been deleted.'),
  2915. 'goto' => (empty($goto) ? 'tbl_sql.php' : $goto),
  2916. );
  2917. $lnk_goto = 'sql.php' . Url::getCommonRaw($_url_params);
  2918. $del_query = 'DELETE FROM '
  2919. . Util::backquote($this->__get('table'))
  2920. . ' WHERE ' . $where_clause .
  2921. ($clause_is_unique ? '' : ' LIMIT 1');
  2922. $_url_params = array(
  2923. 'db' => $this->__get('db'),
  2924. 'table' => $this->__get('table'),
  2925. 'sql_query' => $del_query,
  2926. 'message_to_show' => __('The row has been deleted.'),
  2927. 'goto' => $lnk_goto,
  2928. );
  2929. $del_url = 'sql.php';
  2930. $js_conf = 'DELETE FROM ' . Sanitize::jsFormat($this->__get('table'))
  2931. . ' WHERE ' . Sanitize::jsFormat($where_clause, false)
  2932. . ($clause_is_unique ? '' : ' LIMIT 1');
  2933. $del_str = $this->_getActionLinkContent('b_drop', __('Delete'));
  2934. } elseif ($del_lnk == self::KILL_PROCESS) { // kill process case
  2935. $_url_params = array(
  2936. 'db' => $this->__get('db'),
  2937. 'table' => $this->__get('table'),
  2938. 'sql_query' => $url_sql_query,
  2939. 'goto' => 'index.php',
  2940. );
  2941. $lnk_goto = 'sql.php' . Url::getCommonRaw($_url_params);
  2942. $kill = $GLOBALS['dbi']->getKillQuery($row[0]);
  2943. $_url_params = array(
  2944. 'db' => 'mysql',
  2945. 'sql_query' => $kill,
  2946. 'goto' => $lnk_goto,
  2947. );
  2948. $del_url = 'sql.php';
  2949. $js_conf = $kill;
  2950. $del_str = Util::getIcon(
  2951. 'b_drop', __('Kill')
  2952. );
  2953. } else {
  2954. $del_url = $del_str = $js_conf = $_url_params = null;
  2955. }
  2956. return array($del_url, $del_str, $js_conf, $_url_params);
  2957. } // end of the '_getDeleteAndKillLinks()' function
  2958. /**
  2959. * Get content inside the table row action links (Edit/Copy/Delete)
  2960. *
  2961. * @param string $icon The name of the file to get
  2962. * @param string $display_text The text displaying after the image icon
  2963. *
  2964. * @return string
  2965. *
  2966. * @access private
  2967. *
  2968. * @see _getModifiedLinks(), _getDeleteAndKillLinks()
  2969. */
  2970. private function _getActionLinkContent($icon, $display_text)
  2971. {
  2972. $linkContent = '';
  2973. if (isset($GLOBALS['cfg']['RowActionType'])
  2974. && $GLOBALS['cfg']['RowActionType'] == self::ACTION_LINK_CONTENT_ICONS
  2975. ) {
  2976. $linkContent .= '<span class="nowrap">'
  2977. . Util::getImage(
  2978. $icon, $display_text
  2979. )
  2980. . '</span>';
  2981. } elseif (isset($GLOBALS['cfg']['RowActionType'])
  2982. && $GLOBALS['cfg']['RowActionType'] == self::ACTION_LINK_CONTENT_TEXT
  2983. ) {
  2984. $linkContent .= '<span class="nowrap">' . $display_text . '</span>';
  2985. } else {
  2986. $linkContent .= Util::getIcon(
  2987. $icon, $display_text
  2988. );
  2989. }
  2990. return $linkContent;
  2991. }
  2992. /**
  2993. * Prepare placed links
  2994. *
  2995. * @param string $dir the direction of links should place
  2996. * @param string $del_url the url for delete row
  2997. * @param array $displayParts which elements to display
  2998. * @param integer $row_no the index of current row
  2999. * @param string $where_clause the where clause of the sql
  3000. * @param string $where_clause_html the html encoded where clause
  3001. * @param array $condition_array array of keys (primary, unique, condition)
  3002. * @param string $edit_url the url for edit row
  3003. * @param string $copy_url the url for copy row
  3004. * @param string $edit_anchor_class the class for html element for edit
  3005. * @param string $edit_str the label for edit row
  3006. * @param string $copy_str the label for copy row
  3007. * @param string $del_str the label for delete row
  3008. * @param string $js_conf text for the JS confirmation
  3009. * @param array $editCopyUrlParams URL parameters
  3010. * @param array $delUrlParams URL parameters
  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, $editCopyUrlParams, $delUrlParams
  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, $editCopyUrlParams, $delUrlParams
  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, $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. $displayedColumn,
  3190. // skip 3rd param
  3191. ) = $this->_getPartialText($wktval);
  3192. $cell = $this->_getRowData(
  3193. $class, $condition_field, $analyzed_sql_results, $meta, $map,
  3194. $wktval, $displayedColumn, $transformation_plugin,
  3195. $default_function, '',
  3196. $where_comparison, $transform_options,
  3197. $is_field_truncated, ''
  3198. );
  3199. return $cell;
  3200. }
  3201. // Prepare in Well Known Binary (WKB) format.
  3202. if ($_SESSION['tmpval']['display_binary']) {
  3203. $where_comparison = ' = ' . $column;
  3204. $wkbval = substr(bin2hex($column), 8);
  3205. list(
  3206. $is_field_truncated,
  3207. $displayedColumn,
  3208. // skip 3rd param
  3209. ) = $this->_getPartialText($wkbval);
  3210. $cell = $this->_getRowData(
  3211. $class, $condition_field,
  3212. $analyzed_sql_results, $meta, $map, $wkbval, $displayedColumn,
  3213. $transformation_plugin, $default_function, '',
  3214. $where_comparison, $transform_options,
  3215. $is_field_truncated, ''
  3216. );
  3217. return $cell;
  3218. }
  3219. $wkbval = $this->_handleNonPrintableContents(
  3220. self::BINARY_FIELD, $column, $transformation_plugin,
  3221. $transform_options, $default_function, $meta,
  3222. $_url_params
  3223. );
  3224. $cell = $this->_buildValueDisplay(
  3225. $class, $condition_field, $wkbval
  3226. );
  3227. return $cell;
  3228. } // end of the '_getDataCellForGeometryColumns()' function
  3229. /**
  3230. * Get data cell for non numeric type fields
  3231. *
  3232. * @param string $column the relevant column in data row
  3233. * @param string $class the html class for column
  3234. * @param object $meta the meta-information about
  3235. * the field
  3236. * @param array $map the list of relations
  3237. * @param array $_url_params the parameters for generate
  3238. * url
  3239. * @param boolean $condition_field the column should highlighted
  3240. * or not
  3241. * @param object|string $transformation_plugin the name of transformation
  3242. * function
  3243. * @param string $default_function the default transformation
  3244. * function
  3245. * @param string $transform_options the transformation parameters
  3246. * @param boolean $is_field_truncated is data truncated due to
  3247. * LimitChars
  3248. * @param array $analyzed_sql_results the analyzed query
  3249. * @param integer &$dt_result the link id associated to
  3250. * the query which results
  3251. * have to be displayed
  3252. * @param integer $col_index the column index
  3253. *
  3254. * @return string $cell the prepared data cell, html content
  3255. *
  3256. * @access private
  3257. *
  3258. * @see _getTableBody()
  3259. */
  3260. private function _getDataCellForNonNumericColumns(
  3261. $column, $class, $meta, array $map, array $_url_params, $condition_field,
  3262. $transformation_plugin, $default_function, $transform_options,
  3263. $is_field_truncated, array $analyzed_sql_results, &$dt_result, $col_index
  3264. ) {
  3265. $original_length = 0;
  3266. $is_analyse = $this->__get('is_analyse');
  3267. $field_flags = $GLOBALS['dbi']->fieldFlags($dt_result, $col_index);
  3268. $bIsText = gettype($transformation_plugin) === 'object'
  3269. && strpos($transformation_plugin->getMIMEtype(), 'Text')
  3270. === false;
  3271. // disable inline grid editing
  3272. // if binary fields are protected
  3273. // or transformation plugin is of non text type
  3274. // such as image
  3275. if ((stristr($field_flags, self::BINARY_FIELD)
  3276. && ($GLOBALS['cfg']['ProtectBinary'] === 'all'
  3277. || ($GLOBALS['cfg']['ProtectBinary'] === 'noblob'
  3278. && !stristr($meta->type, self::BLOB_FIELD))
  3279. || ($GLOBALS['cfg']['ProtectBinary'] === 'blob'
  3280. && stristr($meta->type, self::BLOB_FIELD))))
  3281. || $bIsText
  3282. ) {
  3283. $class = str_replace('grid_edit', '', $class);
  3284. }
  3285. if (! isset($column) || is_null($column)) {
  3286. $cell = $this->_buildNullDisplay($class, $condition_field, $meta);
  3287. return $cell;
  3288. }
  3289. if ($column == '') {
  3290. $cell = $this->_buildEmptyDisplay($class, $condition_field, $meta);
  3291. return $cell;
  3292. }
  3293. // Cut all fields to $GLOBALS['cfg']['LimitChars']
  3294. // (unless it's a link-type transformation or binary)
  3295. $displayedColumn = $column;
  3296. if (!(gettype($transformation_plugin) === "object"
  3297. && strpos($transformation_plugin->getName(), 'Link') !== false)
  3298. && !stristr($field_flags, self::BINARY_FIELD)
  3299. ) {
  3300. list(
  3301. $is_field_truncated,
  3302. $column,
  3303. $original_length
  3304. ) = $this->_getPartialText($column);
  3305. }
  3306. $formatted = false;
  3307. if (isset($meta->_type) && $meta->_type === MYSQLI_TYPE_BIT) {
  3308. $displayedColumn = Util::printableBitValue(
  3309. $displayedColumn, $meta->length
  3310. );
  3311. // some results of PROCEDURE ANALYSE() are reported as
  3312. // being BINARY but they are quite readable,
  3313. // so don't treat them as BINARY
  3314. } elseif (stristr($field_flags, self::BINARY_FIELD)
  3315. && !(isset($is_analyse) && $is_analyse)
  3316. ) {
  3317. // we show the BINARY or BLOB message and field's size
  3318. // (or maybe use a transformation)
  3319. $binary_or_blob = self::BLOB_FIELD;
  3320. if ($meta->type === self::STRING_FIELD) {
  3321. $binary_or_blob = self::BINARY_FIELD;
  3322. }
  3323. $displayedColumn = $this->_handleNonPrintableContents(
  3324. $binary_or_blob, $displayedColumn, $transformation_plugin,
  3325. $transform_options, $default_function,
  3326. $meta, $_url_params, $is_field_truncated
  3327. );
  3328. $class = $this->_addClass(
  3329. $class, $condition_field, $meta, '',
  3330. $is_field_truncated, $transformation_plugin, $default_function
  3331. );
  3332. $result = strip_tags($column);
  3333. // disable inline grid editing
  3334. // if binary or blob data is not shown
  3335. if (stristr($result, $binary_or_blob)) {
  3336. $class = str_replace('grid_edit', '', $class);
  3337. }
  3338. $formatted = true;
  3339. }
  3340. if ($formatted) {
  3341. $cell = $this->_buildValueDisplay(
  3342. $class, $condition_field, $displayedColumn
  3343. );
  3344. return $cell;
  3345. }
  3346. // transform functions may enable no-wrapping:
  3347. $function_nowrap = 'applyTransformationNoWrap';
  3348. $bool_nowrap = (($default_function != $transformation_plugin)
  3349. && function_exists($transformation_plugin->$function_nowrap()))
  3350. ? $transformation_plugin->$function_nowrap($transform_options)
  3351. : false;
  3352. // do not wrap if date field type
  3353. $nowrap = (preg_match('@DATE|TIME@i', $meta->type)
  3354. || $bool_nowrap) ? ' nowrap' : '';
  3355. $where_comparison = ' = \''
  3356. . $GLOBALS['dbi']->escapeString($column)
  3357. . '\'';
  3358. $cell = $this->_getRowData(
  3359. $class, $condition_field,
  3360. $analyzed_sql_results, $meta, $map, $column, $displayedColumn,
  3361. $transformation_plugin, $default_function, $nowrap,
  3362. $where_comparison, $transform_options,
  3363. $is_field_truncated, $original_length
  3364. );
  3365. return $cell;
  3366. } // end of the '_getDataCellForNonNumericColumns()' function
  3367. /**
  3368. * Checks the posted options for viewing query results
  3369. * and sets appropriate values in the session.
  3370. *
  3371. * @todo make maximum remembered queries configurable
  3372. * @todo move/split into SQL class!?
  3373. * @todo currently this is called twice unnecessary
  3374. * @todo ignore LIMIT and ORDER in query!?
  3375. *
  3376. * @return void
  3377. *
  3378. * @access public
  3379. *
  3380. * @see sql.php file
  3381. */
  3382. public function setConfigParamsForDisplayTable()
  3383. {
  3384. $sql_md5 = md5($this->__get('sql_query'));
  3385. $query = array();
  3386. if (isset($_SESSION['tmpval']['query'][$sql_md5])) {
  3387. $query = $_SESSION['tmpval']['query'][$sql_md5];
  3388. }
  3389. $query['sql'] = $this->__get('sql_query');
  3390. if (empty($query['repeat_cells'])) {
  3391. $query['repeat_cells'] = $GLOBALS['cfg']['RepeatCells'];
  3392. }
  3393. // as this is a form value, the type is always string so we cannot
  3394. // use Core::isValid($_POST['session_max_rows'], 'integer')
  3395. if (Core::isValid($_POST['session_max_rows'], 'numeric')) {
  3396. $query['max_rows'] = (int)$_POST['session_max_rows'];
  3397. unset($_POST['session_max_rows']);
  3398. } elseif ($_POST['session_max_rows'] == self::ALL_ROWS) {
  3399. $query['max_rows'] = self::ALL_ROWS;
  3400. unset($_POST['session_max_rows']);
  3401. } elseif (empty($query['max_rows'])) {
  3402. $query['max_rows'] = intval($GLOBALS['cfg']['MaxRows']);
  3403. }
  3404. if (Core::isValid($_REQUEST['pos'], 'numeric')) {
  3405. $query['pos'] = $_REQUEST['pos'];
  3406. unset($_REQUEST['pos']);
  3407. } elseif (empty($query['pos'])) {
  3408. $query['pos'] = 0;
  3409. }
  3410. if (Core::isValid(
  3411. $_REQUEST['pftext'],
  3412. array(
  3413. self::DISPLAY_PARTIAL_TEXT, self::DISPLAY_FULL_TEXT
  3414. )
  3415. )
  3416. ) {
  3417. $query['pftext'] = $_REQUEST['pftext'];
  3418. unset($_REQUEST['pftext']);
  3419. } elseif (empty($query['pftext'])) {
  3420. $query['pftext'] = self::DISPLAY_PARTIAL_TEXT;
  3421. }
  3422. if (Core::isValid(
  3423. $_REQUEST['relational_display'],
  3424. array(
  3425. self::RELATIONAL_KEY, self::RELATIONAL_DISPLAY_COLUMN
  3426. )
  3427. )
  3428. ) {
  3429. $query['relational_display'] = $_REQUEST['relational_display'];
  3430. unset($_REQUEST['relational_display']);
  3431. } elseif (empty($query['relational_display'])) {
  3432. // The current session value has priority over a
  3433. // change via Settings; this change will be apparent
  3434. // starting from the next session
  3435. $query['relational_display'] = $GLOBALS['cfg']['RelationalDisplay'];
  3436. }
  3437. if (Core::isValid(
  3438. $_REQUEST['geoOption'],
  3439. array(
  3440. self::GEOMETRY_DISP_WKT, self::GEOMETRY_DISP_WKB,
  3441. self::GEOMETRY_DISP_GEOM
  3442. )
  3443. )
  3444. ) {
  3445. $query['geoOption'] = $_REQUEST['geoOption'];
  3446. unset($_REQUEST['geoOption']);
  3447. } elseif (empty($query['geoOption'])) {
  3448. $query['geoOption'] = self::GEOMETRY_DISP_GEOM;
  3449. }
  3450. if (isset($_REQUEST['display_binary'])) {
  3451. $query['display_binary'] = true;
  3452. unset($_REQUEST['display_binary']);
  3453. } elseif (isset($_REQUEST['display_options_form'])) {
  3454. // we know that the checkbox was unchecked
  3455. unset($query['display_binary']);
  3456. } elseif (isset($_REQUEST['full_text_button'])) {
  3457. // do nothing to keep the value that is there in the session
  3458. } else {
  3459. // selected by default because some operations like OPTIMIZE TABLE
  3460. // and all queries involving functions return "binary" contents,
  3461. // according to low-level field flags
  3462. $query['display_binary'] = true;
  3463. }
  3464. if (isset($_REQUEST['display_blob'])) {
  3465. $query['display_blob'] = true;
  3466. unset($_REQUEST['display_blob']);
  3467. } elseif (isset($_REQUEST['display_options_form'])) {
  3468. // we know that the checkbox was unchecked
  3469. unset($query['display_blob']);
  3470. }
  3471. if (isset($_REQUEST['hide_transformation'])) {
  3472. $query['hide_transformation'] = true;
  3473. unset($_REQUEST['hide_transformation']);
  3474. } elseif (isset($_REQUEST['display_options_form'])) {
  3475. // we know that the checkbox was unchecked
  3476. unset($query['hide_transformation']);
  3477. }
  3478. // move current query to the last position, to be removed last
  3479. // so only least executed query will be removed if maximum remembered
  3480. // queries limit is reached
  3481. unset($_SESSION['tmpval']['query'][$sql_md5]);
  3482. $_SESSION['tmpval']['query'][$sql_md5] = $query;
  3483. // do not exceed a maximum number of queries to remember
  3484. if (count($_SESSION['tmpval']['query']) > 10) {
  3485. array_shift($_SESSION['tmpval']['query']);
  3486. //echo 'deleting one element ...';
  3487. }
  3488. // populate query configuration
  3489. $_SESSION['tmpval']['pftext']
  3490. = $query['pftext'];
  3491. $_SESSION['tmpval']['relational_display']
  3492. = $query['relational_display'];
  3493. $_SESSION['tmpval']['geoOption']
  3494. = $query['geoOption'];
  3495. $_SESSION['tmpval']['display_binary'] = isset(
  3496. $query['display_binary']
  3497. );
  3498. $_SESSION['tmpval']['display_blob'] = isset(
  3499. $query['display_blob']
  3500. );
  3501. $_SESSION['tmpval']['hide_transformation'] = isset(
  3502. $query['hide_transformation']
  3503. );
  3504. $_SESSION['tmpval']['pos']
  3505. = $query['pos'];
  3506. $_SESSION['tmpval']['max_rows']
  3507. = $query['max_rows'];
  3508. $_SESSION['tmpval']['repeat_cells']
  3509. = $query['repeat_cells'];
  3510. }
  3511. /**
  3512. * Prepare a table of results returned by a SQL query.
  3513. *
  3514. * @param integer &$dt_result the link id associated to the query
  3515. * which results have to be displayed
  3516. * @param array &$displayParts the parts to display
  3517. * @param array $analyzed_sql_results analyzed sql results
  3518. * @param boolean $is_limited_display With limited operations or not
  3519. *
  3520. * @return string $table_html Generated HTML content for resulted table
  3521. *
  3522. * @access public
  3523. *
  3524. * @see sql.php file
  3525. */
  3526. public function getTable(
  3527. &$dt_result, array &$displayParts, array $analyzed_sql_results,
  3528. $is_limited_display = false
  3529. ) {
  3530. /**
  3531. * The statement this table is built for.
  3532. * @var \PhpMyAdmin\SqlParser\Statements\SelectStatement
  3533. */
  3534. if (isset($analyzed_sql_results['statement'])) {
  3535. $statement = $analyzed_sql_results['statement'];
  3536. } else {
  3537. $statement = null;
  3538. }
  3539. $table_html = '';
  3540. // Following variable are needed for use in isset/empty or
  3541. // use with array indexes/safe use in foreach
  3542. $fields_meta = $this->__get('fields_meta');
  3543. $showtable = $this->__get('showtable');
  3544. $printview = $this->__get('printview');
  3545. /**
  3546. * @todo move this to a central place
  3547. * @todo for other future table types
  3548. */
  3549. $is_innodb = (isset($showtable['Type'])
  3550. && $showtable['Type'] == self::TABLE_TYPE_INNO_DB);
  3551. $sql = new Sql();
  3552. if ($is_innodb && $sql->isJustBrowsing($analyzed_sql_results, true)) {
  3553. // "j u s t b r o w s i n g"
  3554. $pre_count = '~';
  3555. $after_count = Util::showHint(
  3556. Sanitize::sanitize(
  3557. __('May be approximate. See [doc@faq3-11]FAQ 3.11[/doc].')
  3558. )
  3559. );
  3560. } else {
  3561. $pre_count = '';
  3562. $after_count = '';
  3563. }
  3564. // 1. ----- Prepares the work -----
  3565. // 1.1 Gets the information about which functionalities should be
  3566. // displayed
  3567. list(
  3568. $displayParts,
  3569. $total
  3570. ) = $this->_setDisplayPartsAndTotal($displayParts);
  3571. // 1.2 Defines offsets for the next and previous pages
  3572. if ($displayParts['nav_bar'] == '1') {
  3573. list($pos_next, $pos_prev) = $this->_getOffsets();
  3574. } // end if
  3575. // 1.3 Extract sorting expressions.
  3576. // we need $sort_expression and $sort_expression_nodirection
  3577. // even if there are many table references
  3578. $sort_expression = array();
  3579. $sort_expression_nodirection = array();
  3580. $sort_direction = array();
  3581. if (!is_null($statement) && !empty($statement->order)) {
  3582. foreach ($statement->order as $o) {
  3583. $sort_expression[] = $o->expr->expr . ' ' . $o->type;
  3584. $sort_expression_nodirection[] = $o->expr->expr;
  3585. $sort_direction[] = $o->type;
  3586. }
  3587. } else {
  3588. $sort_expression[] = '';
  3589. $sort_expression_nodirection[] = '';
  3590. $sort_direction[] = '';
  3591. }
  3592. $number_of_columns = count($sort_expression_nodirection);
  3593. // 1.4 Prepares display of first and last value of the sorted column
  3594. $sorted_column_message = '';
  3595. for ( $i = 0; $i < $number_of_columns; $i++ ) {
  3596. $sorted_column_message .= $this->_getSortedColumnMessage(
  3597. $dt_result, $sort_expression_nodirection[$i]
  3598. );
  3599. }
  3600. // 2. ----- Prepare to display the top of the page -----
  3601. // 2.1 Prepares a messages with position information
  3602. if (($displayParts['nav_bar'] == '1') && isset($pos_next)) {
  3603. $message = $this->_setMessageInformation(
  3604. $sorted_column_message,
  3605. $analyzed_sql_results,
  3606. $total,
  3607. $pos_next,
  3608. $pre_count,
  3609. $after_count
  3610. );
  3611. $table_html .= Util::getMessage(
  3612. $message, $this->__get('sql_query'), 'success'
  3613. );
  3614. } elseif ((!isset($printview) || ($printview != '1')) && !$is_limited_display) {
  3615. $table_html .= Util::getMessage(
  3616. __('Your SQL query has been executed successfully.'),
  3617. $this->__get('sql_query'), 'success'
  3618. );
  3619. }
  3620. // 2.3 Prepare the navigation bars
  3621. if (strlen($this->__get('table')) === 0) {
  3622. if ($analyzed_sql_results['querytype'] == 'SELECT') {
  3623. // table does not always contain a real table name,
  3624. // for example in MySQL 5.0.x, the query SHOW STATUS
  3625. // returns STATUS as a table name
  3626. $this->__set('table', $fields_meta[0]->table);
  3627. } else {
  3628. $this->__set('table', '');
  3629. }
  3630. }
  3631. // can the result be sorted?
  3632. if ($displayParts['sort_lnk'] == '1' && ! is_null($analyzed_sql_results['statement'])) {
  3633. // At this point, $sort_expression is an array
  3634. list($unsorted_sql_query, $sort_by_key_html)
  3635. = $this->_getUnsortedSqlAndSortByKeyDropDown(
  3636. $analyzed_sql_results, $sort_expression
  3637. );
  3638. } else {
  3639. $sort_by_key_html = $unsorted_sql_query = '';
  3640. }
  3641. if (($displayParts['nav_bar'] == '1') && !is_null($statement) && (empty($statement->limit))) {
  3642. $table_html .= $this->_getPlacedTableNavigations(
  3643. $pos_next, $pos_prev, self::PLACE_TOP_DIRECTION_DROPDOWN,
  3644. $is_innodb, $sort_by_key_html
  3645. );
  3646. }
  3647. // 2b ----- Get field references from Database -----
  3648. // (see the 'relation' configuration variable)
  3649. // initialize map
  3650. $map = array();
  3651. $target = array();
  3652. if (!is_null($statement) && !empty($statement->from)) {
  3653. foreach ($statement->from as $field) {
  3654. if (!empty($field->table)) {
  3655. $target[] = $field->table;
  3656. }
  3657. }
  3658. }
  3659. if (strlen($this->__get('table')) > 0) {
  3660. // This method set the values for $map array
  3661. $this->_setParamForLinkForeignKeyRelatedTables($map);
  3662. // Coming from 'Distinct values' action of structure page
  3663. // We manipulate relations mechanism to show a link to related rows.
  3664. if ($this->__get('is_browse_distinct')) {
  3665. $map[$fields_meta[1]->name] = array(
  3666. $this->__get('table'),
  3667. $fields_meta[1]->name,
  3668. '',
  3669. $this->__get('db')
  3670. );
  3671. }
  3672. } // end if
  3673. // end 2b
  3674. // 3. ----- Prepare the results table -----
  3675. if ($is_limited_display) {
  3676. $table_html .= "<br>";
  3677. }
  3678. $table_html .= $this->_getTableHeaders(
  3679. $displayParts,
  3680. $analyzed_sql_results,
  3681. $unsorted_sql_query,
  3682. $sort_expression,
  3683. $sort_expression_nodirection,
  3684. $sort_direction,
  3685. $is_limited_display
  3686. );
  3687. $table_html .= '<tbody>' . "\n";
  3688. $table_html .= $this->_getTableBody(
  3689. $dt_result,
  3690. $displayParts,
  3691. $map,
  3692. $analyzed_sql_results,
  3693. $is_limited_display
  3694. );
  3695. $this->__set('display_params', null);
  3696. $table_html .= '</tbody>' . "\n" . '</table></div>';
  3697. // 4. ----- Prepares the link for multi-fields edit and delete
  3698. if ($displayParts['del_lnk'] == self::DELETE_ROW
  3699. && $displayParts['del_lnk'] != self::KILL_PROCESS
  3700. ) {
  3701. $table_html .= $this->_getMultiRowOperationLinks(
  3702. $dt_result,
  3703. $analyzed_sql_results,
  3704. $displayParts['del_lnk']
  3705. );
  3706. }
  3707. // 5. ----- Get the navigation bar at the bottom if required -----
  3708. if (($displayParts['nav_bar'] == '1') && !is_null($statement) && empty($statement->limit)) {
  3709. $table_html .= $this->_getPlacedTableNavigations(
  3710. $pos_next, $pos_prev, self::PLACE_BOTTOM_DIRECTION_DROPDOWN,
  3711. $is_innodb, $sort_by_key_html
  3712. );
  3713. } elseif (! isset($printview) || ($printview != '1')) {
  3714. $table_html .= "\n" . '<br /><br />' . "\n";
  3715. }
  3716. // 6. ----- Prepare "Query results operations"
  3717. if ((! isset($printview) || ($printview != '1')) && ! $is_limited_display) {
  3718. $table_html .= $this->_getResultsOperations(
  3719. $displayParts, $analyzed_sql_results
  3720. );
  3721. }
  3722. return $table_html;
  3723. } // end of the 'getTable()' function
  3724. /**
  3725. * Get offsets for next page and previous page
  3726. *
  3727. * @return array array with two elements - $pos_next, $pos_prev
  3728. *
  3729. * @access private
  3730. *
  3731. * @see getTable()
  3732. */
  3733. private function _getOffsets()
  3734. {
  3735. if ($_SESSION['tmpval']['max_rows'] == self::ALL_ROWS) {
  3736. $pos_next = 0;
  3737. $pos_prev = 0;
  3738. } else {
  3739. $pos_next = $_SESSION['tmpval']['pos']
  3740. + $_SESSION['tmpval']['max_rows'];
  3741. $pos_prev = $_SESSION['tmpval']['pos']
  3742. - $_SESSION['tmpval']['max_rows'];
  3743. if ($pos_prev < 0) {
  3744. $pos_prev = 0;
  3745. }
  3746. }
  3747. return array($pos_next, $pos_prev);
  3748. } // end of the '_getOffsets()' function
  3749. /**
  3750. * Prepare sorted column message
  3751. *
  3752. * @param integer &$dt_result the link id associated to the
  3753. * query which results have to
  3754. * be displayed
  3755. * @param string $sort_expression_nodirection sort expression without direction
  3756. *
  3757. * @return string html content
  3758. * null if not found sorted column
  3759. *
  3760. * @access private
  3761. *
  3762. * @see getTable()
  3763. */
  3764. private function _getSortedColumnMessage(
  3765. &$dt_result, $sort_expression_nodirection
  3766. ) {
  3767. $fields_meta = $this->__get('fields_meta'); // To use array indexes
  3768. if (empty($sort_expression_nodirection)) {
  3769. return null;
  3770. }
  3771. if (mb_strpos($sort_expression_nodirection, '.') === false) {
  3772. $sort_table = $this->__get('table');
  3773. $sort_column = $sort_expression_nodirection;
  3774. } else {
  3775. list($sort_table, $sort_column)
  3776. = explode('.', $sort_expression_nodirection);
  3777. }
  3778. $sort_table = Util::unQuote($sort_table);
  3779. $sort_column = Util::unQuote($sort_column);
  3780. // find the sorted column index in row result
  3781. // (this might be a multi-table query)
  3782. $sorted_column_index = false;
  3783. foreach ($fields_meta as $key => $meta) {
  3784. if (($meta->table == $sort_table) && ($meta->name == $sort_column)) {
  3785. $sorted_column_index = $key;
  3786. break;
  3787. }
  3788. }
  3789. if ($sorted_column_index === false) {
  3790. return null;
  3791. }
  3792. // fetch first row of the result set
  3793. $row = $GLOBALS['dbi']->fetchRow($dt_result);
  3794. // initializing default arguments
  3795. $default_function = [Core::class, 'mimeDefaultFunction'];
  3796. $transformation_plugin = $default_function;
  3797. $transform_options = array();
  3798. // check for non printable sorted row data
  3799. $meta = $fields_meta[$sorted_column_index];
  3800. if (stristr($meta->type, self::BLOB_FIELD)
  3801. || ($meta->type == self::GEOMETRY_FIELD)
  3802. ) {
  3803. $column_for_first_row = $this->_handleNonPrintableContents(
  3804. $meta->type, $row[$sorted_column_index],
  3805. $transformation_plugin, $transform_options,
  3806. $default_function, $meta
  3807. );
  3808. } else {
  3809. $column_for_first_row = $row[$sorted_column_index];
  3810. }
  3811. $column_for_first_row = mb_strtoupper(
  3812. mb_substr(
  3813. $column_for_first_row, 0, $GLOBALS['cfg']['LimitChars']
  3814. ) . '...'
  3815. );
  3816. // fetch last row of the result set
  3817. $GLOBALS['dbi']->dataSeek($dt_result, $this->__get('num_rows') - 1);
  3818. $row = $GLOBALS['dbi']->fetchRow($dt_result);
  3819. // check for non printable sorted row data
  3820. $meta = $fields_meta[$sorted_column_index];
  3821. if (stristr($meta->type, self::BLOB_FIELD)
  3822. || ($meta->type == self::GEOMETRY_FIELD)
  3823. ) {
  3824. $column_for_last_row = $this->_handleNonPrintableContents(
  3825. $meta->type, $row[$sorted_column_index],
  3826. $transformation_plugin, $transform_options,
  3827. $default_function, $meta
  3828. );
  3829. } else {
  3830. $column_for_last_row = $row[$sorted_column_index];
  3831. }
  3832. $column_for_last_row = mb_strtoupper(
  3833. mb_substr(
  3834. $column_for_last_row, 0, $GLOBALS['cfg']['LimitChars']
  3835. ) . '...'
  3836. );
  3837. // reset to first row for the loop in _getTableBody()
  3838. $GLOBALS['dbi']->dataSeek($dt_result, 0);
  3839. // we could also use here $sort_expression_nodirection
  3840. return ' [' . htmlspecialchars($sort_column)
  3841. . ': <strong>' . htmlspecialchars($column_for_first_row) . ' - '
  3842. . htmlspecialchars($column_for_last_row) . '</strong>]';
  3843. } // end of the '_getSortedColumnMessage()' function
  3844. /**
  3845. * Set the content that needs to be shown in message
  3846. *
  3847. * @param string $sorted_column_message the message for sorted column
  3848. * @param array $analyzed_sql_results the analyzed query
  3849. * @param integer $total the total number of rows returned by
  3850. * the SQL query without any
  3851. * programmatically appended LIMIT clause
  3852. * @param integer $pos_next the offset for next page
  3853. * @param string $pre_count the string renders before row count
  3854. * @param string $after_count the string renders after row count
  3855. *
  3856. * @return Message $message an object of Message
  3857. *
  3858. * @access private
  3859. *
  3860. * @see getTable()
  3861. */
  3862. private function _setMessageInformation(
  3863. $sorted_column_message, array $analyzed_sql_results, $total,
  3864. $pos_next, $pre_count, $after_count
  3865. ) {
  3866. $unlim_num_rows = $this->__get('unlim_num_rows'); // To use in isset()
  3867. if (!empty($analyzed_sql_results['statement']->limit)) {
  3868. $first_shown_rec = $analyzed_sql_results['statement']->limit->offset;
  3869. $row_count = $analyzed_sql_results['statement']->limit->rowCount;
  3870. if ($row_count < $total) {
  3871. $last_shown_rec = $first_shown_rec + $row_count - 1;
  3872. } else {
  3873. $last_shown_rec = $first_shown_rec + $total - 1;
  3874. }
  3875. } elseif (($_SESSION['tmpval']['max_rows'] == self::ALL_ROWS)
  3876. || ($pos_next > $total)
  3877. ) {
  3878. $first_shown_rec = $_SESSION['tmpval']['pos'];
  3879. $last_shown_rec = $total - 1;
  3880. } else {
  3881. $first_shown_rec = $_SESSION['tmpval']['pos'];
  3882. $last_shown_rec = $pos_next - 1;
  3883. }
  3884. $table = new Table($this->__get('table'), $this->__get('db'));
  3885. if ($table->isView()
  3886. && ($total == $GLOBALS['cfg']['MaxExactCountViews'])
  3887. ) {
  3888. $message = Message::notice(
  3889. __(
  3890. 'This view has at least this number of rows. '
  3891. . 'Please refer to %sdocumentation%s.'
  3892. )
  3893. );
  3894. $message->addParam('[doc@cfg_MaxExactCount]');
  3895. $message->addParam('[/doc]');
  3896. $message_view_warning = Util::showHint($message);
  3897. } else {
  3898. $message_view_warning = false;
  3899. }
  3900. $message = Message::success(__('Showing rows %1s - %2s'));
  3901. $message->addParam($first_shown_rec);
  3902. if ($message_view_warning !== false) {
  3903. $message->addParamHtml('... ' . $message_view_warning);
  3904. } else {
  3905. $message->addParam($last_shown_rec);
  3906. }
  3907. $message->addText('(');
  3908. if ($message_view_warning === false) {
  3909. if (isset($unlim_num_rows) && ($unlim_num_rows != $total)) {
  3910. $message_total = Message::notice(
  3911. $pre_count . __('%1$d total, %2$d in query')
  3912. );
  3913. $message_total->addParam($total);
  3914. $message_total->addParam($unlim_num_rows);
  3915. } else {
  3916. $message_total = Message::notice($pre_count . __('%d total'));
  3917. $message_total->addParam($total);
  3918. }
  3919. if (!empty($after_count)) {
  3920. $message_total->addHtml($after_count);
  3921. }
  3922. $message->addMessage($message_total, '');
  3923. $message->addText(', ', '');
  3924. }
  3925. $message_qt = Message::notice(__('Query took %01.4f seconds.') . ')');
  3926. $message_qt->addParam($this->__get('querytime'));
  3927. $message->addMessage($message_qt, '');
  3928. if (! is_null($sorted_column_message)) {
  3929. $message->addHtml($sorted_column_message, '');
  3930. }
  3931. return $message;
  3932. } // end of the '_setMessageInformation()' function
  3933. /**
  3934. * Set the value of $map array for linking foreign key related tables
  3935. *
  3936. * @param array &$map the list of relations
  3937. *
  3938. * @return void
  3939. *
  3940. * @access private
  3941. *
  3942. * @see getTable()
  3943. */
  3944. private function _setParamForLinkForeignKeyRelatedTables(array &$map)
  3945. {
  3946. // To be able to later display a link to the related table,
  3947. // we verify both types of relations: either those that are
  3948. // native foreign keys or those defined in the phpMyAdmin
  3949. // configuration storage. If no PMA storage, we won't be able
  3950. // to use the "column to display" notion (for example show
  3951. // the name related to a numeric id).
  3952. $exist_rel = $this->relation->getForeigners(
  3953. $this->__get('db'), $this->__get('table'), '', self::POSITION_BOTH
  3954. );
  3955. if (! empty($exist_rel)) {
  3956. foreach ($exist_rel as $master_field => $rel) {
  3957. if ($master_field != 'foreign_keys_data') {
  3958. $display_field = $this->relation->getDisplayField(
  3959. $rel['foreign_db'], $rel['foreign_table']
  3960. );
  3961. $map[$master_field] = array(
  3962. $rel['foreign_table'],
  3963. $rel['foreign_field'],
  3964. $display_field,
  3965. $rel['foreign_db']
  3966. );
  3967. } else {
  3968. foreach ($rel as $key => $one_key) {
  3969. foreach ($one_key['index_list'] as $index => $one_field) {
  3970. $display_field = $this->relation->getDisplayField(
  3971. isset($one_key['ref_db_name'])
  3972. ? $one_key['ref_db_name']
  3973. : $GLOBALS['db'],
  3974. $one_key['ref_table_name']
  3975. );
  3976. $map[$one_field] = array(
  3977. $one_key['ref_table_name'],
  3978. $one_key['ref_index_list'][$index],
  3979. $display_field,
  3980. isset($one_key['ref_db_name'])
  3981. ? $one_key['ref_db_name']
  3982. : $GLOBALS['db']
  3983. );
  3984. }
  3985. }
  3986. }
  3987. } // end while
  3988. } // end if
  3989. } // end of the '_setParamForLinkForeignKeyRelatedTables()' function
  3990. /**
  3991. * Prepare multi field edit/delete links
  3992. *
  3993. * @param integer &$dt_result the link id associated to the query which
  3994. * results have to be displayed
  3995. * @param array $analyzed_sql_results analyzed sql results
  3996. * @param string $del_link the display element - 'del_link'
  3997. *
  3998. * @return string $links_html html content
  3999. *
  4000. * @access private
  4001. *
  4002. * @see getTable()
  4003. */
  4004. private function _getMultiRowOperationLinks(
  4005. &$dt_result, array $analyzed_sql_results, $del_link
  4006. ) {
  4007. $links_html = '<div class="print_ignore" >';
  4008. $url_query = $this->__get('url_query');
  4009. $delete_text = ($del_link == self::DELETE_ROW) ? __('Delete') : __('Kill');
  4010. $links_html .= '<img class="selectallarrow" width="38" height="22"'
  4011. . ' src="' . $this->__get('pma_theme_image') . 'arrow_'
  4012. . $this->__get('text_dir') . '.png' . '"'
  4013. . ' alt="' . __('With selected:') . '" />';
  4014. $links_html .= '<input type="checkbox" '
  4015. . 'id="resultsForm_' . $this->__get('unique_id') . '_checkall" '
  4016. . 'class="checkall_box" title="' . __('Check all') . '" /> '
  4017. . '<label for="resultsForm_' . $this->__get('unique_id') . '_checkall">'
  4018. . __('Check all') . '</label> '
  4019. . '<i style="margin-left: 2em">' . __('With selected:') . '</i>' . "\n";
  4020. $links_html .= Util::getButtonOrImage(
  4021. 'submit_mult', 'mult_submit',
  4022. __('Edit'), 'b_edit', 'edit'
  4023. );
  4024. $links_html .= Util::getButtonOrImage(
  4025. 'submit_mult', 'mult_submit',
  4026. __('Copy'), 'b_insrow', 'copy'
  4027. );
  4028. $links_html .= Util::getButtonOrImage(
  4029. 'submit_mult', 'mult_submit',
  4030. $delete_text, 'b_drop', 'delete'
  4031. );
  4032. if ($analyzed_sql_results['querytype'] == 'SELECT') {
  4033. $links_html .= Util::getButtonOrImage(
  4034. 'submit_mult', 'mult_submit',
  4035. __('Export'), 'b_tblexport', 'export'
  4036. );
  4037. }
  4038. $links_html .= "</div>\n";
  4039. $links_html .= '<input type="hidden" name="sql_query"'
  4040. . ' value="' . htmlspecialchars($this->__get('sql_query')) . '" />'
  4041. . "\n";
  4042. if (! empty($url_query)) {
  4043. $links_html .= '<input type="hidden" name="url_query"'
  4044. . ' value="' . $url_query . '" />' . "\n";
  4045. }
  4046. // fetch last row of the result set
  4047. $GLOBALS['dbi']->dataSeek($dt_result, $this->__get('num_rows') - 1);
  4048. $row = $GLOBALS['dbi']->fetchRow($dt_result);
  4049. // @see DbiMysqi::fetchRow & DatabaseInterface::fetchRow
  4050. if (! is_array($row)) {
  4051. $row = array();
  4052. }
  4053. // $clause_is_unique is needed by getTable() to generate the proper param
  4054. // in the multi-edit and multi-delete form
  4055. list($where_clause, $clause_is_unique, $condition_array)
  4056. = Util::getUniqueCondition(
  4057. $dt_result, // handle
  4058. $this->__get('fields_cnt'), // fields_cnt
  4059. $this->__get('fields_meta'), // fields_meta
  4060. $row, // row
  4061. false, // force_unique
  4062. false, // restrict_to_table
  4063. $analyzed_sql_results // analyzed_sql_results
  4064. );
  4065. unset($where_clause, $condition_array);
  4066. // reset to first row for the loop in _getTableBody()
  4067. $GLOBALS['dbi']->dataSeek($dt_result, 0);
  4068. $links_html .= '<input type="hidden" name="clause_is_unique"'
  4069. . ' value="' . $clause_is_unique . '" />' . "\n";
  4070. $links_html .= '</form>' . "\n";
  4071. return $links_html;
  4072. } // end of the '_getMultiRowOperationLinks()' function
  4073. /**
  4074. * Prepare table navigation bar at the top or bottom
  4075. *
  4076. * @param integer $pos_next the offset for the "next" page
  4077. * @param integer $pos_prev the offset for the "previous" page
  4078. * @param string $place the place to show navigation
  4079. * @param boolean $is_innodb whether its InnoDB or not
  4080. * @param string $sort_by_key_html the sort by key dialog
  4081. *
  4082. * @return string html content of navigation bar
  4083. *
  4084. * @access private
  4085. *
  4086. * @see _getTable()
  4087. */
  4088. private function _getPlacedTableNavigations(
  4089. $pos_next, $pos_prev, $place, $is_innodb, $sort_by_key_html
  4090. ) {
  4091. $navigation_html = '';
  4092. if ($place == self::PLACE_BOTTOM_DIRECTION_DROPDOWN) {
  4093. $navigation_html .= '<br />' . "\n";
  4094. }
  4095. $navigation_html .= $this->_getTableNavigation(
  4096. $pos_next, $pos_prev, $is_innodb, $sort_by_key_html
  4097. );
  4098. if ($place == self::PLACE_TOP_DIRECTION_DROPDOWN) {
  4099. $navigation_html .= "\n";
  4100. }
  4101. return $navigation_html;
  4102. } // end of the '_getPlacedTableNavigations()' function
  4103. /**
  4104. * Generates HTML to display the Create view in span tag
  4105. *
  4106. * @param array $analyzed_sql_results analyzed sql results
  4107. * @param array $url_query URL Parameters
  4108. *
  4109. * @return string
  4110. *
  4111. * @access private
  4112. *
  4113. * @see _getResultsOperations()
  4114. */
  4115. private function _getLinkForCreateView(array $analyzed_sql_results, $urlParams)
  4116. {
  4117. $results_operations_html = '';
  4118. if (empty($analyzed_sql_results['procedure'])) {
  4119. $results_operations_html .= '<span>'
  4120. . Util::linkOrButton(
  4121. 'view_create.php',
  4122. $urlParams,
  4123. Util::getIcon(
  4124. 'b_view_add', __('Create view'), true
  4125. ),
  4126. array('class' => 'create_view ajax')
  4127. )
  4128. . '</span>' . "\n";
  4129. }
  4130. return $results_operations_html;
  4131. }
  4132. /**
  4133. * Calls the _getResultsOperations with $only_view as true
  4134. *
  4135. * @param array $analyzed_sql_results analyzed sql results
  4136. *
  4137. * @return string
  4138. *
  4139. * @access public
  4140. *
  4141. */
  4142. public function getCreateViewQueryResultOp(array $analyzed_sql_results)
  4143. {
  4144. $results_operations_html = '';
  4145. //calling to _getResultOperations with a fake $displayParts
  4146. //and setting only_view parameter to be true to generate just view
  4147. $results_operations_html .= $this->_getResultsOperations(
  4148. array(),
  4149. $analyzed_sql_results,
  4150. true
  4151. );
  4152. return $results_operations_html;
  4153. }
  4154. /**
  4155. * Get copy to clipboard links for results operations
  4156. *
  4157. * @return string $html
  4158. *
  4159. * @access private
  4160. */
  4161. private function _getCopytoclipboardLinks()
  4162. {
  4163. $html = Util::linkOrButton(
  4164. '#',
  4165. null,
  4166. Util::getIcon(
  4167. 'b_insrow', __('Copy to clipboard'), true
  4168. ),
  4169. array('id' => 'copyToClipBoard')
  4170. );
  4171. return $html;
  4172. }
  4173. /**
  4174. * Get printview links for results operations
  4175. *
  4176. * @return string $html
  4177. *
  4178. * @access private
  4179. */
  4180. private function _getPrintviewLinks()
  4181. {
  4182. $html = Util::linkOrButton(
  4183. '#',
  4184. null,
  4185. Util::getIcon(
  4186. 'b_print', __('Print'), true
  4187. ),
  4188. array('id' => 'printView'),
  4189. 'print_view'
  4190. );
  4191. return $html;
  4192. }
  4193. /**
  4194. * Get operations that are available on results.
  4195. *
  4196. * @param array $displayParts the parts to display
  4197. * @param array $analyzed_sql_results analyzed sql results
  4198. * @param boolean $only_view Whether to show only view
  4199. *
  4200. * @return string $results_operations_html html content
  4201. *
  4202. * @access private
  4203. *
  4204. * @see getTable()
  4205. */
  4206. private function _getResultsOperations(
  4207. array $displayParts, array $analyzed_sql_results, $only_view = false
  4208. ) {
  4209. global $printview;
  4210. $results_operations_html = '';
  4211. $fields_meta = $this->__get('fields_meta'); // To safe use in foreach
  4212. $header_shown = false;
  4213. $header = '<fieldset class="print_ignore" ><legend>'
  4214. . __('Query results operations') . '</legend>';
  4215. $_url_params = array(
  4216. 'db' => $this->__get('db'),
  4217. 'table' => $this->__get('table'),
  4218. 'printview' => '1',
  4219. 'sql_query' => $this->__get('sql_query'),
  4220. );
  4221. if (!$header_shown) {
  4222. $results_operations_html .= $header;
  4223. $header_shown = true;
  4224. }
  4225. // if empty result set was produced we need to
  4226. // show only view and not other options
  4227. if ($only_view) {
  4228. $results_operations_html .= $this->_getLinkForCreateView(
  4229. $analyzed_sql_results, $_url_params
  4230. );
  4231. if ($header_shown) {
  4232. $results_operations_html .= '</fieldset><br />';
  4233. }
  4234. return $results_operations_html;
  4235. }
  4236. // Displays "printable view" link if required
  4237. if ($displayParts['pview_lnk'] == '1') {
  4238. $results_operations_html .= $this->_getPrintviewLinks();
  4239. $results_operations_html .= $this->_getCopytoclipboardLinks();
  4240. } // end displays "printable view"
  4241. // Export link
  4242. // (the url_query has extra parameters that won't be used to export)
  4243. // (the single_table parameter is used in Export::getDisplay()
  4244. // to hide the SQL and the structure export dialogs)
  4245. // If the parser found a PROCEDURE clause
  4246. // (most probably PROCEDURE ANALYSE()) it makes no sense to
  4247. // display the Export link).
  4248. if (($analyzed_sql_results['querytype'] == self::QUERY_TYPE_SELECT)
  4249. && ! isset($printview)
  4250. && empty($analyzed_sql_results['procedure'])
  4251. ) {
  4252. if (count($analyzed_sql_results['select_tables']) == 1) {
  4253. $_url_params['single_table'] = 'true';
  4254. }
  4255. if (! $header_shown) {
  4256. $results_operations_html .= $header;
  4257. $header_shown = true;
  4258. }
  4259. $_url_params['unlim_num_rows'] = $this->__get('unlim_num_rows');
  4260. /**
  4261. * At this point we don't know the table name; this can happen
  4262. * for example with a query like
  4263. * SELECT bike_code FROM (SELECT bike_code FROM bikes) tmp
  4264. * As a workaround we set in the table parameter the name of the
  4265. * first table of this database, so that tbl_export.php and
  4266. * the script it calls do not fail
  4267. */
  4268. if (empty($_url_params['table']) && ! empty($_url_params['db'])) {
  4269. $_url_params['table'] = $GLOBALS['dbi']->fetchValue("SHOW TABLES");
  4270. /* No result (probably no database selected) */
  4271. if ($_url_params['table'] === false) {
  4272. unset($_url_params['table']);
  4273. }
  4274. }
  4275. $results_operations_html .= Util::linkOrButton(
  4276. 'tbl_export.php',
  4277. $_url_params,
  4278. Util::getIcon(
  4279. 'b_tblexport', __('Export'), true
  4280. )
  4281. )
  4282. . "\n";
  4283. // prepare chart
  4284. $results_operations_html .= Util::linkOrButton(
  4285. 'tbl_chart.php',
  4286. $_url_params,
  4287. Util::getIcon(
  4288. 'b_chart', __('Display chart'), true
  4289. )
  4290. )
  4291. . "\n";
  4292. // prepare GIS chart
  4293. $geometry_found = false;
  4294. // If at least one geometry field is found
  4295. foreach ($fields_meta as $meta) {
  4296. if ($meta->type == self::GEOMETRY_FIELD) {
  4297. $geometry_found = true;
  4298. break;
  4299. }
  4300. }
  4301. if ($geometry_found) {
  4302. $results_operations_html
  4303. .= Util::linkOrButton(
  4304. 'tbl_gis_visualization.php',
  4305. $_url_params,
  4306. Util::getIcon(
  4307. 'b_globe',
  4308. __('Visualize GIS data'),
  4309. true
  4310. )
  4311. )
  4312. . "\n";
  4313. }
  4314. }
  4315. // CREATE VIEW
  4316. /**
  4317. *
  4318. * @todo detect privileges to create a view
  4319. * (but see 2006-01-19 note in PhpMyAdmin\Display\CreateTable,
  4320. * I think we cannot detect db-specific privileges reliably)
  4321. * Note: we don't display a Create view link if we found a PROCEDURE clause
  4322. */
  4323. if (!$header_shown) {
  4324. $results_operations_html .= $header;
  4325. $header_shown = true;
  4326. }
  4327. $results_operations_html .= $this->_getLinkForCreateView(
  4328. $analyzed_sql_results, $_url_params
  4329. );
  4330. if ($header_shown) {
  4331. $results_operations_html .= '</fieldset><br />';
  4332. }
  4333. return $results_operations_html;
  4334. } // end of the '_getResultsOperations()' function
  4335. /**
  4336. * Verifies what to do with non-printable contents (binary or BLOB)
  4337. * in Browse mode.
  4338. *
  4339. * @param string $category BLOB|BINARY|GEOMETRY
  4340. * @param string $content the binary content
  4341. * @param mixed $transformation_plugin transformation plugin.
  4342. * Can also be the default function:
  4343. * Core::mimeDefaultFunction
  4344. * @param string $transform_options transformation parameters
  4345. * @param string $default_function default transformation function
  4346. * @param object $meta the meta-information about the field
  4347. * @param array $url_params parameters that should go to the
  4348. * download link
  4349. * @param boolean &$is_truncated the result is truncated or not
  4350. *
  4351. * @return mixed string or float
  4352. *
  4353. * @access private
  4354. *
  4355. * @see _getDataCellForGeometryColumns(),
  4356. * _getDataCellForNonNumericColumns(),
  4357. * _getSortedColumnMessage()
  4358. */
  4359. private function _handleNonPrintableContents(
  4360. $category, $content, $transformation_plugin, $transform_options,
  4361. $default_function, $meta, array $url_params = array(), &$is_truncated = null
  4362. ) {
  4363. $is_truncated = false;
  4364. $result = '[' . $category;
  4365. if (isset($content)) {
  4366. $size = strlen($content);
  4367. $display_size = Util::formatByteDown($size, 3, 1);
  4368. $result .= ' - ' . $display_size[0] . ' ' . $display_size[1];
  4369. } else {
  4370. $result .= ' - NULL';
  4371. $size = 0;
  4372. }
  4373. $result .= ']';
  4374. // if we want to use a text transformation on a BLOB column
  4375. if (gettype($transformation_plugin) === "object") {
  4376. $posMimeOctetstream = strpos(
  4377. $transformation_plugin->getMIMESubtype(),
  4378. 'Octetstream'
  4379. );
  4380. $posMimeText = strpos($transformation_plugin->getMIMEtype(), 'Text');
  4381. if ($posMimeOctetstream
  4382. || $posMimeText !== false
  4383. ) {
  4384. // Applying Transformations on hex string of binary data
  4385. // seems more appropriate
  4386. $result = pack("H*", bin2hex($content));
  4387. }
  4388. }
  4389. if ($size <= 0) {
  4390. return($result);
  4391. }
  4392. if ($default_function != $transformation_plugin) {
  4393. $result = $transformation_plugin->applyTransformation(
  4394. $result,
  4395. $transform_options,
  4396. $meta
  4397. );
  4398. return($result);
  4399. }
  4400. $result = $default_function($result, array(), $meta);
  4401. if (($_SESSION['tmpval']['display_binary']
  4402. && $meta->type === self::STRING_FIELD)
  4403. || ($_SESSION['tmpval']['display_blob']
  4404. && stristr($meta->type, self::BLOB_FIELD))
  4405. ) {
  4406. // in this case, restart from the original $content
  4407. if (mb_check_encoding($content, 'utf-8')
  4408. && !preg_match('/[\x00-\x08\x0B\x0C\x0E-\x1F\x80-\x9F]/u', $content)
  4409. ) {
  4410. // show as text if it's valid utf-8
  4411. $result = htmlspecialchars($content);
  4412. } else {
  4413. $result = '0x' . bin2hex($content);
  4414. }
  4415. list(
  4416. $is_truncated,
  4417. $result,
  4418. // skip 3rd param
  4419. ) = $this->_getPartialText($result);
  4420. }
  4421. /* Create link to download */
  4422. // in PHP < 5.5, empty() only checks variables
  4423. $tmpdb = $this->__get('db');
  4424. if (count($url_params) > 0
  4425. && (!empty($tmpdb) && !empty($meta->orgtable))
  4426. ) {
  4427. $url_params['where_clause_sign'] = Core::signSqlQuery($url_params['where_clause']);
  4428. $result = '<a href="tbl_get_field.php'
  4429. . Url::getCommon($url_params)
  4430. . '" class="disableAjax">'
  4431. . $result . '</a>';
  4432. }
  4433. return($result);
  4434. } // end of the '_handleNonPrintableContents()' function
  4435. /**
  4436. * Retrieves the associated foreign key info for a data cell
  4437. *
  4438. * @param array $map the list of relations
  4439. * @param object $meta the meta-information about the field
  4440. * @param string $where_comparison data for the where clause
  4441. *
  4442. * @return string formatted data
  4443. *
  4444. * @access private
  4445. *
  4446. */
  4447. private function _getFromForeign(array $map, $meta, $where_comparison)
  4448. {
  4449. $dispsql = 'SELECT '
  4450. . Util::backquote($map[$meta->name][2])
  4451. . ' FROM '
  4452. . Util::backquote($map[$meta->name][3])
  4453. . '.'
  4454. . Util::backquote($map[$meta->name][0])
  4455. . ' WHERE '
  4456. . Util::backquote($map[$meta->name][1])
  4457. . $where_comparison;
  4458. $dispresult = $GLOBALS['dbi']->tryQuery(
  4459. $dispsql,
  4460. DatabaseInterface::CONNECT_USER,
  4461. DatabaseInterface::QUERY_STORE
  4462. );
  4463. if ($dispresult && $GLOBALS['dbi']->numRows($dispresult) > 0) {
  4464. list($dispval) = $GLOBALS['dbi']->fetchRow($dispresult, 0);
  4465. } else {
  4466. $dispval = __('Link not found!');
  4467. }
  4468. $GLOBALS['dbi']->freeResult($dispresult);
  4469. return $dispval;
  4470. }
  4471. /**
  4472. * Prepares the displayable content of a data cell in Browse mode,
  4473. * taking into account foreign key description field and transformations
  4474. *
  4475. * @param string $class css classes for the td element
  4476. * @param bool $condition_field whether the column is a part of
  4477. * the where clause
  4478. * @param array $analyzed_sql_results the analyzed query
  4479. * @param object $meta the meta-information about the
  4480. * field
  4481. * @param array $map the list of relations
  4482. * @param string $data data
  4483. * @param string $displayedData data that will be displayed (maybe be chunked)
  4484. * @param object|string $transformation_plugin transformation plugin.
  4485. * Can also be the default function:
  4486. * Core::mimeDefaultFunction
  4487. * @param string $default_function default function
  4488. * @param string $nowrap 'nowrap' if the content should
  4489. * not be wrapped
  4490. * @param string $where_comparison data for the where clause
  4491. * @param array $transform_options options for transformation
  4492. * @param bool $is_field_truncated whether the field is truncated
  4493. * @param string $original_length of a truncated column, or ''
  4494. *
  4495. * @return string formatted data
  4496. *
  4497. * @access private
  4498. *
  4499. * @see _getDataCellForNumericColumns(), _getDataCellForGeometryColumns(),
  4500. * _getDataCellForNonNumericColumns(),
  4501. *
  4502. */
  4503. private function _getRowData(
  4504. $class, $condition_field, array $analyzed_sql_results, $meta,
  4505. array $map, $data, $displayedData,
  4506. $transformation_plugin, $default_function, $nowrap, $where_comparison,
  4507. array $transform_options, $is_field_truncated, $original_length=''
  4508. ) {
  4509. $relational_display = $_SESSION['tmpval']['relational_display'];
  4510. $printview = $this->__get('printview');
  4511. $decimals = isset($meta->decimals) ? $meta->decimals : '-1';
  4512. $result = '<td data-decimals="' . $decimals . '"'
  4513. . ' data-type="' . $meta->type . '"';
  4514. if (! empty($original_length)) {
  4515. // cannot use data-original-length
  4516. $result .= ' data-originallength="' . $original_length . '"';
  4517. }
  4518. $result .= ' class="'
  4519. . $this->_addClass(
  4520. $class, $condition_field, $meta, $nowrap,
  4521. $is_field_truncated, $transformation_plugin, $default_function
  4522. )
  4523. . '">';
  4524. if (!empty($analyzed_sql_results['statement']->expr)) {
  4525. foreach ($analyzed_sql_results['statement']->expr as $expr) {
  4526. if ((empty($expr->alias)) || (empty($expr->column))) {
  4527. continue;
  4528. }
  4529. if (strcasecmp($meta->name, $expr->alias) == 0) {
  4530. $meta->name = $expr->column;
  4531. }
  4532. }
  4533. }
  4534. if (isset($map[$meta->name])) {
  4535. // Field to display from the foreign table?
  4536. if (isset($map[$meta->name][2])
  4537. && strlen($map[$meta->name][2]) > 0
  4538. ) {
  4539. $dispval = $this->_getFromForeign(
  4540. $map, $meta, $where_comparison
  4541. );
  4542. } else {
  4543. $dispval = '';
  4544. } // end if... else...
  4545. if (isset($printview) && ($printview == '1')) {
  4546. $result .= ($transformation_plugin != $default_function
  4547. ? $transformation_plugin->applyTransformation(
  4548. $data,
  4549. $transform_options,
  4550. $meta
  4551. )
  4552. : $default_function($data)
  4553. )
  4554. . ' <code>[-&gt;' . $dispval . ']</code>';
  4555. } else {
  4556. if ($relational_display == self::RELATIONAL_KEY) {
  4557. // user chose "relational key" in the display options, so
  4558. // the title contains the display field
  4559. $title = (! empty($dispval))
  4560. ? htmlspecialchars($dispval)
  4561. : '';
  4562. } else {
  4563. $title = htmlspecialchars($data);
  4564. }
  4565. $sqlQuery = 'SELECT * FROM '
  4566. . Util::backquote($map[$meta->name][3]) . '.'
  4567. . Util::backquote($map[$meta->name][0])
  4568. . ' WHERE '
  4569. . Util::backquote($map[$meta->name][1])
  4570. . $where_comparison;
  4571. $_url_params = array(
  4572. 'db' => $map[$meta->name][3],
  4573. 'table' => $map[$meta->name][0],
  4574. 'pos' => '0',
  4575. 'sql_signature' => Core::signSqlQuery($sqlQuery),
  4576. 'sql_query' => $sqlQuery,
  4577. );
  4578. if ($transformation_plugin != $default_function) {
  4579. // always apply a transformation on the real data,
  4580. // not on the display field
  4581. $displayedData = $transformation_plugin->applyTransformation(
  4582. $data,
  4583. $transform_options,
  4584. $meta
  4585. );
  4586. } else {
  4587. if ($relational_display == self::RELATIONAL_DISPLAY_COLUMN
  4588. && ! empty($map[$meta->name][2])
  4589. ) {
  4590. // user chose "relational display field" in the
  4591. // display options, so show display field in the cell
  4592. $displayedData = $default_function($dispval);
  4593. } else {
  4594. // otherwise display data in the cell
  4595. $displayedData = $default_function($displayedData);
  4596. }
  4597. }
  4598. $tag_params = array('title' => $title);
  4599. if (strpos($class, 'grid_edit') !== false) {
  4600. $tag_params['class'] = 'ajax';
  4601. }
  4602. $result .= Util::linkOrButton(
  4603. 'sql.php', $_url_params,
  4604. $displayedData, $tag_params
  4605. );
  4606. }
  4607. } else {
  4608. $result .= ($transformation_plugin != $default_function
  4609. ? $transformation_plugin->applyTransformation(
  4610. $data,
  4611. $transform_options,
  4612. $meta
  4613. )
  4614. : $default_function($data)
  4615. );
  4616. }
  4617. $result .= '</td>' . "\n";
  4618. return $result;
  4619. } // end of the '_getRowData()' function
  4620. /**
  4621. * Prepares a checkbox for multi-row submits
  4622. *
  4623. * @param string $del_url delete url
  4624. * @param array $displayParts array with explicit indexes for all
  4625. * the display elements
  4626. * @param string $row_no the row number
  4627. * @param string $where_clause_html url encoded where clause
  4628. * @param array $condition_array array of conditions in the where clause
  4629. * @param string $id_suffix suffix for the id
  4630. * @param string $class css classes for the td element
  4631. *
  4632. * @return string the generated HTML
  4633. *
  4634. * @access private
  4635. *
  4636. * @see _getTableBody(), _getCheckboxAndLinks()
  4637. */
  4638. private function _getCheckboxForMultiRowSubmissions(
  4639. $del_url, array $displayParts, $row_no, $where_clause_html, array $condition_array,
  4640. $id_suffix, $class
  4641. ) {
  4642. $ret = '';
  4643. if (! empty($del_url) && $displayParts['del_lnk'] != self::KILL_PROCESS) {
  4644. $ret .= '<td ';
  4645. if (! empty($class)) {
  4646. $ret .= 'class="' . $class . '"';
  4647. }
  4648. $ret .= ' class="center print_ignore">'
  4649. . '<input type="checkbox" id="id_rows_to_delete'
  4650. . $row_no . $id_suffix
  4651. . '" name="rows_to_delete[' . $row_no . ']"'
  4652. . ' class="multi_checkbox checkall"'
  4653. . ' value="' . $where_clause_html . '" '
  4654. . ' />'
  4655. . '<input type="hidden" class="condition_array" value="'
  4656. . htmlspecialchars(json_encode($condition_array)) . '" />'
  4657. . ' </td>';
  4658. }
  4659. return $ret;
  4660. } // end of the '_getCheckboxForMultiRowSubmissions()' function
  4661. /**
  4662. * Prepares an Edit link
  4663. *
  4664. * @param string $edit_url edit url
  4665. * @param array $urlParams URL parameters
  4666. * @param string $class css classes for td element
  4667. * @param string $edit_str text for the edit link
  4668. * @param string $where_clause where clause
  4669. * @param string $where_clause_html url encoded where clause
  4670. *
  4671. * @return string the generated HTML
  4672. *
  4673. * @access private
  4674. *
  4675. * @see _getTableBody(), _getCheckboxAndLinks()
  4676. */
  4677. private function _getEditLink(
  4678. $edit_url, $urlParams, $class, $edit_str, $where_clause, $where_clause_html
  4679. ) {
  4680. $ret = '';
  4681. if (! empty($edit_url)) {
  4682. $ret .= '<td class="' . $class . ' center print_ignore" '
  4683. . ' ><span class="nowrap">'
  4684. . Util::linkOrButton($edit_url, $urlParams, $edit_str);
  4685. /*
  4686. * Where clause for selecting this row uniquely is provided as
  4687. * a hidden input. Used by jQuery scripts for handling grid editing
  4688. */
  4689. if (! empty($where_clause)) {
  4690. $ret .= '<input type="hidden" class="where_clause" value ="'
  4691. . $where_clause_html . '" />';
  4692. }
  4693. $ret .= '</span></td>';
  4694. }
  4695. return $ret;
  4696. } // end of the '_getEditLink()' function
  4697. /**
  4698. * Prepares an Copy link
  4699. *
  4700. * @param string $copy_url copy url
  4701. * @param array $urlParams URL parameters
  4702. * @param string $copy_str text for the copy link
  4703. * @param string $where_clause where clause
  4704. * @param string $where_clause_html url encoded where clause
  4705. * @param string $class css classes for the td element
  4706. *
  4707. * @return string the generated HTML
  4708. *
  4709. * @access private
  4710. *
  4711. * @see _getTableBody(), _getCheckboxAndLinks()
  4712. */
  4713. private function _getCopyLink(
  4714. $copy_url, $urlParams, $copy_str, $where_clause, $where_clause_html, $class
  4715. ) {
  4716. $ret = '';
  4717. if (! empty($copy_url)) {
  4718. $ret .= '<td class="';
  4719. if (! empty($class)) {
  4720. $ret .= $class . ' ';
  4721. }
  4722. $ret .= 'center print_ignore" ' . ' ><span class="nowrap">'
  4723. . Util::linkOrButton($copy_url, $urlParams, $copy_str);
  4724. /*
  4725. * Where clause for selecting this row uniquely is provided as
  4726. * a hidden input. Used by jQuery scripts for handling grid editing
  4727. */
  4728. if (! empty($where_clause)) {
  4729. $ret .= '<input type="hidden" class="where_clause" value="'
  4730. . $where_clause_html . '" />';
  4731. }
  4732. $ret .= '</span></td>';
  4733. }
  4734. return $ret;
  4735. } // end of the '_getCopyLink()' function
  4736. /**
  4737. * Prepares a Delete link
  4738. *
  4739. * @param string $del_url delete url
  4740. * @param array $delUrlParams URL parameters
  4741. * @param string $del_str text for the delete link
  4742. * @param string $js_conf text for the JS confirmation
  4743. * @param string $class css classes for the td element
  4744. *
  4745. * @return string the generated HTML
  4746. *
  4747. * @access private
  4748. *
  4749. * @see _getTableBody(), _getCheckboxAndLinks()
  4750. */
  4751. private function _getDeleteLink($del_url, $delUrlParams, $del_str, $js_conf, $class)
  4752. {
  4753. $ret = '';
  4754. if (empty($del_url)) {
  4755. return $ret;
  4756. }
  4757. $ret .= '<td class="';
  4758. if (! empty($class)) {
  4759. $ret .= $class . ' ';
  4760. }
  4761. $ajax = Response::getInstance()->isAjax() ? ' ajax' : '';
  4762. $ret .= 'center print_ignore" ' . ' >'
  4763. . Util::linkOrButton(
  4764. $del_url,
  4765. $delUrlParams,
  4766. $del_str,
  4767. array('class' => 'delete_row requireConfirm' . $ajax)
  4768. )
  4769. . '<div class="hide">' . $js_conf . '</div>'
  4770. . '</td>';
  4771. return $ret;
  4772. } // end of the '_getDeleteLink()' function
  4773. /**
  4774. * Prepare checkbox and links at some position (left or right)
  4775. * (only called for horizontal mode)
  4776. *
  4777. * @param string $position the position of the checkbox and links
  4778. * @param string $del_url delete url
  4779. * @param array $displayParts array with explicit indexes for all the
  4780. * display elements
  4781. * @param string $row_no row number
  4782. * @param string $where_clause where clause
  4783. * @param string $where_clause_html url encoded where clause
  4784. * @param array $condition_array array of conditions in the where clause
  4785. * @param string $edit_url edit url
  4786. * @param string $copy_url copy url
  4787. * @param string $class css classes for the td elements
  4788. * @param string $edit_str text for the edit link
  4789. * @param string $copy_str text for the copy link
  4790. * @param string $del_str text for the delete link
  4791. * @param string $js_conf text for the JS confirmation
  4792. * @param array $editCopyUrlParams URL parameters
  4793. * @param array $delUrlParams URL parameters
  4794. *
  4795. * @return string the generated HTML
  4796. *
  4797. * @access private
  4798. *
  4799. * @see _getPlacedLinks()
  4800. */
  4801. private function _getCheckboxAndLinks(
  4802. $position, $del_url, array $displayParts, $row_no, $where_clause,
  4803. $where_clause_html, array $condition_array,
  4804. $edit_url, $copy_url, $class, $edit_str, $copy_str, $del_str, $js_conf, $editCopyUrlParams, $delUrlParams
  4805. ) {
  4806. $ret = '';
  4807. $editUrlParams = $editCopyUrlParams + array('default_action' => 'update');
  4808. $copyUrlParams = $editCopyUrlParams + array('default_action' => 'insert');
  4809. if ($position == self::POSITION_LEFT) {
  4810. $ret .= $this->_getCheckboxForMultiRowSubmissions(
  4811. $del_url . Url::getCommon($delUrlParams), $displayParts, $row_no, $where_clause_html,
  4812. $condition_array, '_left', ''
  4813. );
  4814. $ret .= $this->_getEditLink(
  4815. $edit_url, $editUrlParams, $class, $edit_str, $where_clause, $where_clause_html
  4816. );
  4817. $ret .= $this->_getCopyLink(
  4818. $copy_url, $copyUrlParams, $copy_str, $where_clause, $where_clause_html, ''
  4819. );
  4820. $ret .= $this->_getDeleteLink($del_url, $delUrlParams, $del_str, $js_conf, '');
  4821. } elseif ($position == self::POSITION_RIGHT) {
  4822. $ret .= $this->_getDeleteLink($del_url, $delUrlParams, $del_str, $js_conf, '');
  4823. $ret .= $this->_getCopyLink(
  4824. $copy_url, $copyUrlParams, $copy_str, $where_clause, $where_clause_html, ''
  4825. );
  4826. $ret .= $this->_getEditLink(
  4827. $edit_url, $editUrlParams, $class, $edit_str, $where_clause, $where_clause_html
  4828. );
  4829. $ret .= $this->_getCheckboxForMultiRowSubmissions(
  4830. $del_url . Url::getCommon($delUrlParams), $displayParts, $row_no, $where_clause_html,
  4831. $condition_array, '_right', ''
  4832. );
  4833. } else { // $position == self::POSITION_NONE
  4834. $ret .= $this->_getCheckboxForMultiRowSubmissions(
  4835. $del_url . Url::getCommon($delUrlParams), $displayParts, $row_no, $where_clause_html,
  4836. $condition_array, '_left', ''
  4837. );
  4838. }
  4839. return $ret;
  4840. } // end of the '_getCheckboxAndLinks()' function
  4841. /**
  4842. * Truncates given string based on LimitChars configuration
  4843. * and Session pftext variable
  4844. * (string is truncated only if necessary)
  4845. *
  4846. * @param string $str string to be truncated
  4847. *
  4848. * @return mixed
  4849. *
  4850. * @access private
  4851. *
  4852. * @see _handleNonPrintableContents(), _getDataCellForGeometryColumns(),
  4853. * _getDataCellForNonNumericColumns
  4854. */
  4855. private function _getPartialText($str)
  4856. {
  4857. $original_length = mb_strlen($str);
  4858. if ($original_length > $GLOBALS['cfg']['LimitChars']
  4859. && $_SESSION['tmpval']['pftext'] === self::DISPLAY_PARTIAL_TEXT
  4860. ) {
  4861. $str = mb_substr(
  4862. $str, 0, $GLOBALS['cfg']['LimitChars']
  4863. ) . '...';
  4864. $truncated = true;
  4865. } else {
  4866. $truncated = false;
  4867. }
  4868. return array($truncated, $str, $original_length);
  4869. }
  4870. }