Results.php 201 KB

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