InsertEdit.php 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626
  1. <?php
  2. /**
  3. * set of functions with the insert/edit features in pma
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin;
  7. use PhpMyAdmin\Controllers\Table\ChangeController;
  8. use PhpMyAdmin\Html\Generator;
  9. use PhpMyAdmin\Plugins\TransformationsPlugin;
  10. use const ENT_COMPAT;
  11. use const PASSWORD_DEFAULT;
  12. use function array_fill;
  13. use function array_flip;
  14. use function array_merge;
  15. use function array_values;
  16. use function bin2hex;
  17. use function class_exists;
  18. use function count;
  19. use function current;
  20. use function date;
  21. use function defined;
  22. use function explode;
  23. use function htmlspecialchars;
  24. use function implode;
  25. use function in_array;
  26. use function is_array;
  27. use function is_file;
  28. use function is_numeric;
  29. use function is_string;
  30. use function max;
  31. use function mb_stripos;
  32. use function mb_strlen;
  33. use function mb_strpos;
  34. use function mb_strstr;
  35. use function mb_substr;
  36. use function md5;
  37. use function method_exists;
  38. use function min;
  39. use function password_hash;
  40. use function preg_match;
  41. use function preg_replace;
  42. use function sprintf;
  43. use function str_replace;
  44. use function stripcslashes;
  45. use function stripslashes;
  46. use function strlen;
  47. use function strpos;
  48. use function substr;
  49. use function time;
  50. use function trim;
  51. /**
  52. * PhpMyAdmin\InsertEdit class
  53. */
  54. class InsertEdit
  55. {
  56. /**
  57. * DatabaseInterface instance
  58. *
  59. * @var DatabaseInterface
  60. */
  61. private $dbi;
  62. /** @var Relation */
  63. private $relation;
  64. /** @var Transformations */
  65. private $transformations;
  66. /** @var FileListing */
  67. private $fileListing;
  68. /** @var Template */
  69. public $template;
  70. /**
  71. * @param DatabaseInterface $dbi DatabaseInterface instance
  72. */
  73. public function __construct(DatabaseInterface $dbi)
  74. {
  75. $this->dbi = $dbi;
  76. $this->relation = new Relation($this->dbi);
  77. $this->transformations = new Transformations();
  78. $this->fileListing = new FileListing();
  79. $this->template = new Template();
  80. }
  81. /**
  82. * Retrieve form parameters for insert/edit form
  83. *
  84. * @param string $db name of the database
  85. * @param string $table name of the table
  86. * @param array|null $where_clauses where clauses
  87. * @param array $where_clause_array array of where clauses
  88. * @param string $err_url error url
  89. *
  90. * @return array array of insert/edit form parameters
  91. */
  92. public function getFormParametersForInsertForm(
  93. $db,
  94. $table,
  95. ?array $where_clauses,
  96. array $where_clause_array,
  97. $err_url
  98. ) {
  99. $_form_params = [
  100. 'db' => $db,
  101. 'table' => $table,
  102. 'goto' => $GLOBALS['goto'],
  103. 'err_url' => $err_url,
  104. 'sql_query' => $_POST['sql_query'],
  105. ];
  106. if (isset($where_clauses)) {
  107. foreach ($where_clause_array as $key_id => $where_clause) {
  108. $_form_params['where_clause[' . $key_id . ']'] = trim($where_clause);
  109. }
  110. }
  111. if (isset($_POST['clause_is_unique'])) {
  112. $_form_params['clause_is_unique'] = $_POST['clause_is_unique'];
  113. }
  114. return $_form_params;
  115. }
  116. /**
  117. * Creates array of where clauses
  118. *
  119. * @param array|string|null $where_clause where clause
  120. *
  121. * @return array whereClauseArray array of where clauses
  122. */
  123. private function getWhereClauseArray($where_clause)
  124. {
  125. if (! isset($where_clause)) {
  126. return [];
  127. }
  128. if (is_array($where_clause)) {
  129. return $where_clause;
  130. }
  131. return [0 => $where_clause];
  132. }
  133. /**
  134. * Analysing where clauses array
  135. *
  136. * @param array $where_clause_array array of where clauses
  137. * @param string $table name of the table
  138. * @param string $db name of the database
  139. *
  140. * @return array $where_clauses, $result, $rows, $found_unique_key
  141. */
  142. private function analyzeWhereClauses(
  143. array $where_clause_array,
  144. $table,
  145. $db
  146. ) {
  147. $rows = [];
  148. $result = [];
  149. $where_clauses = [];
  150. $found_unique_key = false;
  151. foreach ($where_clause_array as $key_id => $where_clause) {
  152. $local_query = 'SELECT * FROM '
  153. . Util::backquote($db) . '.'
  154. . Util::backquote($table)
  155. . ' WHERE ' . $where_clause . ';';
  156. $result[$key_id] = $this->dbi->query(
  157. $local_query,
  158. DatabaseInterface::CONNECT_USER,
  159. DatabaseInterface::QUERY_STORE
  160. );
  161. $rows[$key_id] = $this->dbi->fetchAssoc($result[$key_id]);
  162. $where_clauses[$key_id] = str_replace('\\', '\\\\', $where_clause);
  163. $has_unique_condition = $this->showEmptyResultMessageOrSetUniqueCondition(
  164. $rows,
  165. $key_id,
  166. $where_clause_array,
  167. $local_query,
  168. $result
  169. );
  170. if (! $has_unique_condition) {
  171. continue;
  172. }
  173. $found_unique_key = true;
  174. }
  175. return [
  176. $where_clauses,
  177. $result,
  178. $rows,
  179. $found_unique_key,
  180. ];
  181. }
  182. /**
  183. * Show message for empty result or set the unique_condition
  184. *
  185. * @param array $rows MySQL returned rows
  186. * @param string $key_id ID in current key
  187. * @param array $where_clause_array array of where clauses
  188. * @param string $local_query query performed
  189. * @param array $result MySQL result handle
  190. *
  191. * @return bool
  192. */
  193. private function showEmptyResultMessageOrSetUniqueCondition(
  194. array $rows,
  195. $key_id,
  196. array $where_clause_array,
  197. $local_query,
  198. array $result
  199. ) {
  200. $has_unique_condition = false;
  201. // No row returned
  202. if (! $rows[$key_id]) {
  203. unset($rows[$key_id], $where_clause_array[$key_id]);
  204. Response::getInstance()->addHTML(
  205. Generator::getMessage(
  206. __('MySQL returned an empty result set (i.e. zero rows).'),
  207. $local_query
  208. )
  209. );
  210. /**
  211. * @todo not sure what should be done at this point, but we must not
  212. * exit if we want the message to be displayed
  213. */
  214. } else {// end if (no row returned)
  215. $meta = $this->dbi->getFieldsMeta($result[$key_id]);
  216. [$unique_condition, $tmp_clause_is_unique] = Util::getUniqueCondition(
  217. $result[$key_id],
  218. count($meta),
  219. $meta,
  220. $rows[$key_id],
  221. true
  222. );
  223. if (! empty($unique_condition)) {
  224. $has_unique_condition = true;
  225. }
  226. unset($unique_condition, $tmp_clause_is_unique);
  227. }
  228. return $has_unique_condition;
  229. }
  230. /**
  231. * No primary key given, just load first row
  232. *
  233. * @param string $table name of the table
  234. * @param string $db name of the database
  235. *
  236. * @return array containing $result and $rows arrays
  237. */
  238. private function loadFirstRow($table, $db)
  239. {
  240. $result = $this->dbi->query(
  241. 'SELECT * FROM ' . Util::backquote($db)
  242. . '.' . Util::backquote($table) . ' LIMIT 1;',
  243. DatabaseInterface::CONNECT_USER,
  244. DatabaseInterface::QUERY_STORE
  245. );
  246. // Can be a string on some old configuration storage settings
  247. $rows = array_fill(0, (int) $GLOBALS['cfg']['InsertRows'], false);
  248. return [
  249. $result,
  250. $rows,
  251. ];
  252. }
  253. /**
  254. * Add some url parameters
  255. *
  256. * @param array $url_params containing $db and $table as url parameters
  257. * @param array $where_clause_array where clauses array
  258. *
  259. * @return array Add some url parameters to $url_params array and return it
  260. */
  261. public function urlParamsInEditMode(
  262. array $url_params,
  263. array $where_clause_array
  264. ): array {
  265. foreach ($where_clause_array as $where_clause) {
  266. $url_params['where_clause'] = trim($where_clause);
  267. }
  268. if (! empty($_POST['sql_query'])) {
  269. $url_params['sql_query'] = $_POST['sql_query'];
  270. }
  271. return $url_params;
  272. }
  273. /**
  274. * Show type information or function selectors in Insert/Edit
  275. *
  276. * @param string $which function|type
  277. * @param array $url_params containing url parameters
  278. * @param bool $is_show whether to show the element in $which
  279. *
  280. * @return string an HTML snippet
  281. */
  282. public function showTypeOrFunction($which, array $url_params, $is_show)
  283. {
  284. $params = [];
  285. switch ($which) {
  286. case 'function':
  287. $params['ShowFunctionFields'] = ($is_show ? 0 : 1);
  288. $params['ShowFieldTypesInDataEditView']
  289. = $GLOBALS['cfg']['ShowFieldTypesInDataEditView'];
  290. break;
  291. case 'type':
  292. $params['ShowFieldTypesInDataEditView'] = ($is_show ? 0 : 1);
  293. $params['ShowFunctionFields']
  294. = $GLOBALS['cfg']['ShowFunctionFields'];
  295. break;
  296. }
  297. $params['goto'] = Url::getFromRoute('/sql');
  298. $this_url_params = array_merge($url_params, $params);
  299. if (! $is_show) {
  300. return ' : <a href="' . Url::getFromRoute('/table/change') . '" data-post="'
  301. . Url::getCommon($this_url_params, '') . '">'
  302. . $this->showTypeOrFunctionLabel($which)
  303. . '</a>';
  304. }
  305. return '<th><a href="' . Url::getFromRoute('/table/change') . '" data-post="'
  306. . Url::getCommon($this_url_params, '')
  307. . '" title="' . __('Hide') . '">'
  308. . $this->showTypeOrFunctionLabel($which)
  309. . '</a></th>';
  310. }
  311. /**
  312. * Show type information or function selectors labels in Insert/Edit
  313. *
  314. * @param string $which function|type
  315. *
  316. * @return string|null an HTML snippet
  317. */
  318. private function showTypeOrFunctionLabel($which)
  319. {
  320. switch ($which) {
  321. case 'function':
  322. return __('Function');
  323. case 'type':
  324. return __('Type');
  325. }
  326. return null;
  327. }
  328. /**
  329. * Analyze the table column array
  330. *
  331. * @param array $column description of column in given table
  332. * @param array $comments_map comments for every column that has a comment
  333. * @param bool $timestamp_seen whether a timestamp has been seen
  334. *
  335. * @return array description of column in given table
  336. */
  337. private function analyzeTableColumnsArray(
  338. array $column,
  339. array $comments_map,
  340. $timestamp_seen
  341. ) {
  342. $column['Field_html'] = htmlspecialchars($column['Field']);
  343. $column['Field_md5'] = md5($column['Field']);
  344. // True_Type contains only the type (stops at first bracket)
  345. $column['True_Type'] = preg_replace('@\(.*@s', '', $column['Type']);
  346. $column['len'] = preg_match('@float|double@', $column['Type']) ? 100 : -1;
  347. $column['Field_title'] = $this->getColumnTitle($column, $comments_map);
  348. $column['is_binary'] = $this->isColumn(
  349. $column,
  350. [
  351. 'binary',
  352. 'varbinary',
  353. ]
  354. );
  355. $column['is_blob'] = $this->isColumn(
  356. $column,
  357. [
  358. 'blob',
  359. 'tinyblob',
  360. 'mediumblob',
  361. 'longblob',
  362. ]
  363. );
  364. $column['is_char'] = $this->isColumn(
  365. $column,
  366. [
  367. 'char',
  368. 'varchar',
  369. ]
  370. );
  371. [$column['pma_type'], $column['wrap'], $column['first_timestamp']]
  372. = $this->getEnumSetAndTimestampColumns($column, $timestamp_seen);
  373. return $column;
  374. }
  375. /**
  376. * Retrieve the column title
  377. *
  378. * @param array $column description of column in given table
  379. * @param array $comments_map comments for every column that has a comment
  380. *
  381. * @return string column title
  382. */
  383. private function getColumnTitle(array $column, array $comments_map)
  384. {
  385. if (isset($comments_map[$column['Field']])) {
  386. return '<span style="border-bottom: 1px dashed black;" title="'
  387. . htmlspecialchars($comments_map[$column['Field']]) . '">'
  388. . $column['Field_html'] . '</span>';
  389. }
  390. return $column['Field_html'];
  391. }
  392. /**
  393. * check whether the column is of a certain type
  394. * the goal is to ensure that types such as "enum('one','two','binary',..)"
  395. * or "enum('one','two','varbinary',..)" are not categorized as binary
  396. *
  397. * @param array $column description of column in given table
  398. * @param array $types the types to verify
  399. *
  400. * @return bool whether the column's type if one of the $types
  401. */
  402. public function isColumn(array $column, array $types)
  403. {
  404. foreach ($types as $one_type) {
  405. if (mb_stripos($column['Type'], $one_type) === 0) {
  406. return true;
  407. }
  408. }
  409. return false;
  410. }
  411. /**
  412. * Retrieve set, enum, timestamp table columns
  413. *
  414. * @param array $column description of column in given table
  415. * @param bool $timestamp_seen whether a timestamp has been seen
  416. *
  417. * @return array $column['pma_type'], $column['wrap'], $column['first_timestamp']
  418. */
  419. private function getEnumSetAndTimestampColumns(array $column, $timestamp_seen)
  420. {
  421. $column['first_timestamp'] = false;
  422. switch ($column['True_Type']) {
  423. case 'set':
  424. $column['pma_type'] = 'set';
  425. $column['wrap'] = '';
  426. break;
  427. case 'enum':
  428. $column['pma_type'] = 'enum';
  429. $column['wrap'] = '';
  430. break;
  431. case 'timestamp':
  432. if (! $timestamp_seen) { // can only occur once per table
  433. $column['first_timestamp'] = true;
  434. }
  435. $column['pma_type'] = $column['Type'];
  436. $column['wrap'] = ' nowrap';
  437. break;
  438. default:
  439. $column['pma_type'] = $column['Type'];
  440. $column['wrap'] = ' nowrap';
  441. break;
  442. }
  443. return [
  444. $column['pma_type'],
  445. $column['wrap'],
  446. $column['first_timestamp'],
  447. ];
  448. }
  449. /**
  450. * The function column
  451. * We don't want binary data to be destroyed
  452. * Note: from the MySQL manual: "BINARY doesn't affect how the column is
  453. * stored or retrieved" so it does not mean that the contents is binary
  454. *
  455. * @param array $column description of column in given table
  456. * @param bool $is_upload upload or no
  457. * @param string $column_name_appendix the name attribute
  458. * @param string $onChangeClause onchange clause for fields
  459. * @param array $no_support_types list of datatypes that are not (yet)
  460. * handled by PMA
  461. * @param int $tabindex_for_function +3000
  462. * @param int $tabindex tab index
  463. * @param int $idindex id index
  464. * @param bool $insert_mode insert mode or edit mode
  465. * @param bool $readOnly is column read only or not
  466. * @param array $foreignData foreign key data
  467. *
  468. * @return string an html snippet
  469. */
  470. private function getFunctionColumn(
  471. array $column,
  472. $is_upload,
  473. $column_name_appendix,
  474. $onChangeClause,
  475. array $no_support_types,
  476. $tabindex_for_function,
  477. $tabindex,
  478. $idindex,
  479. $insert_mode,
  480. $readOnly,
  481. array $foreignData
  482. ): string {
  483. $html_output = '';
  484. if (($GLOBALS['cfg']['ProtectBinary'] === 'blob'
  485. && $column['is_blob'] && ! $is_upload)
  486. || ($GLOBALS['cfg']['ProtectBinary'] === 'all'
  487. && $column['is_binary'])
  488. || ($GLOBALS['cfg']['ProtectBinary'] === 'noblob'
  489. && $column['is_binary'])
  490. ) {
  491. $html_output .= '<td class="text-center">' . __('Binary') . '</td>' . "\n";
  492. } elseif ($readOnly
  493. || mb_strstr($column['True_Type'], 'enum')
  494. || mb_strstr($column['True_Type'], 'set')
  495. || in_array($column['pma_type'], $no_support_types)
  496. ) {
  497. $html_output .= '<td class="text-center">--</td>' . "\n";
  498. } else {
  499. $html_output .= '<td>' . "\n";
  500. $html_output .= '<select name="funcs' . $column_name_appendix . '"'
  501. . ' ' . $onChangeClause
  502. . ' tabindex="' . ($tabindex + $tabindex_for_function) . '"'
  503. . ' id="field_' . $idindex . '_1">';
  504. $html_output .= Generator::getFunctionsForField(
  505. $column,
  506. $insert_mode,
  507. $foreignData
  508. ) . "\n";
  509. $html_output .= '</select>' . "\n";
  510. $html_output .= '</td>' . "\n";
  511. }
  512. return $html_output;
  513. }
  514. /**
  515. * The null column
  516. *
  517. * @param array $column description of column in given table
  518. * @param string $column_name_appendix the name attribute
  519. * @param bool $real_null_value is column value null or not null
  520. * @param int $tabindex tab index
  521. * @param int $tabindex_for_null +6000
  522. * @param int $idindex id index
  523. * @param string $vkey [multi_edit]['row_id']
  524. * @param array $foreigners keys into foreign fields
  525. * @param array $foreignData data about the foreign keys
  526. * @param bool $readOnly is column read only or not
  527. *
  528. * @return string an html snippet
  529. */
  530. private function getNullColumn(
  531. array $column,
  532. $column_name_appendix,
  533. $real_null_value,
  534. $tabindex,
  535. $tabindex_for_null,
  536. $idindex,
  537. $vkey,
  538. array $foreigners,
  539. array $foreignData,
  540. $readOnly
  541. ) {
  542. if ($column['Null'] !== 'YES' || $readOnly) {
  543. return "<td></td>\n";
  544. }
  545. $html_output = '';
  546. $html_output .= '<td>' . "\n";
  547. $html_output .= '<input type="hidden" name="fields_null_prev'
  548. . $column_name_appendix . '"';
  549. if ($real_null_value && ! $column['first_timestamp']) {
  550. $html_output .= ' value="on"';
  551. }
  552. $html_output .= '>' . "\n";
  553. $html_output .= '<input type="checkbox" class="checkbox_null" tabindex="'
  554. . ($tabindex + $tabindex_for_null) . '"'
  555. . ' name="fields_null' . $column_name_appendix . '"';
  556. if ($real_null_value) {
  557. $html_output .= ' checked="checked"';
  558. }
  559. $html_output .= ' id="field_' . $idindex . '_2">';
  560. // nullify_code is needed by the js nullify() function
  561. $nullify_code = $this->getNullifyCodeForNullColumn(
  562. $column,
  563. $foreigners,
  564. $foreignData
  565. );
  566. // to be able to generate calls to nullify() in jQuery
  567. $html_output .= '<input type="hidden" class="nullify_code" name="nullify_code'
  568. . $column_name_appendix . '" value="' . $nullify_code . '">';
  569. $html_output .= '<input type="hidden" class="hashed_field" name="hashed_field'
  570. . $column_name_appendix . '" value="' . $column['Field_md5'] . '">';
  571. $html_output .= '<input type="hidden" class="multi_edit" name="multi_edit'
  572. . $column_name_appendix . '" value="' . Sanitize::escapeJsString($vkey) . '">';
  573. $html_output .= '</td>' . "\n";
  574. return $html_output;
  575. }
  576. /**
  577. * Retrieve the nullify code for the null column
  578. *
  579. * @param array $column description of column in given table
  580. * @param array $foreigners keys into foreign fields
  581. * @param array $foreignData data about the foreign keys
  582. */
  583. private function getNullifyCodeForNullColumn(
  584. array $column,
  585. array $foreigners,
  586. array $foreignData
  587. ): string {
  588. $foreigner = $this->relation->searchColumnInForeigners($foreigners, $column['Field']);
  589. if (mb_strstr($column['True_Type'], 'enum')) {
  590. if (mb_strlen((string) $column['Type']) > 20) {
  591. $nullify_code = '1';
  592. } else {
  593. $nullify_code = '2';
  594. }
  595. } elseif (mb_strstr($column['True_Type'], 'set')) {
  596. $nullify_code = '3';
  597. } elseif (! empty($foreigners)
  598. && ! empty($foreigner)
  599. && $foreignData['foreign_link'] == false
  600. ) {
  601. // foreign key in a drop-down
  602. $nullify_code = '4';
  603. } elseif (! empty($foreigners)
  604. && ! empty($foreigner)
  605. && $foreignData['foreign_link'] == true
  606. ) {
  607. // foreign key with a browsing icon
  608. $nullify_code = '6';
  609. } else {
  610. $nullify_code = '5';
  611. }
  612. return $nullify_code;
  613. }
  614. /**
  615. * Get the HTML elements for value column in insert form
  616. * (here, "column" is used in the sense of HTML column in HTML table)
  617. *
  618. * @param array $column description of column in given table
  619. * @param string $backup_field hidden input field
  620. * @param string $column_name_appendix the name attribute
  621. * @param string $onChangeClause onchange clause for fields
  622. * @param int $tabindex tab index
  623. * @param int $tabindex_for_value offset for the values tabindex
  624. * @param int $idindex id index
  625. * @param string $data description of the column field
  626. * @param string $special_chars special characters
  627. * @param array $foreignData data about the foreign keys
  628. * @param array $paramTableDbArray array containing $table and $db
  629. * @param int $rownumber the row number
  630. * @param string $text_dir text direction
  631. * @param string $special_chars_encoded replaced char if the string starts
  632. * with a \r\n pair (0x0d0a) add an extra \n
  633. * @param string $vkey [multi_edit]['row_id']
  634. * @param bool $is_upload is upload or not
  635. * @param int $biggest_max_file_size 0 integer
  636. * @param string $default_char_editing default char editing mode which is stored
  637. * in the config.inc.php script
  638. * @param array $no_support_types list of datatypes that are not (yet)
  639. * handled by PMA
  640. * @param array $gis_data_types list of GIS data types
  641. * @param array $extracted_columnspec associative array containing type,
  642. * spec_in_brackets and possibly
  643. * enum_set_values (another array)
  644. * @param bool $readOnly is column read only or not
  645. *
  646. * @return string an html snippet
  647. */
  648. private function getValueColumn(
  649. array $column,
  650. $backup_field,
  651. $column_name_appendix,
  652. $onChangeClause,
  653. $tabindex,
  654. $tabindex_for_value,
  655. $idindex,
  656. $data,
  657. $special_chars,
  658. array $foreignData,
  659. array $paramTableDbArray,
  660. $rownumber,
  661. $text_dir,
  662. $special_chars_encoded,
  663. $vkey,
  664. $is_upload,
  665. $biggest_max_file_size,
  666. $default_char_editing,
  667. array $no_support_types,
  668. array $gis_data_types,
  669. array $extracted_columnspec,
  670. $readOnly
  671. ) {
  672. // HTML5 data-* attribute data-type
  673. $data_type = $this->dbi->types->getTypeClass($column['True_Type']);
  674. $html_output = '';
  675. if ($foreignData['foreign_link'] == true) {
  676. $html_output .= $this->getForeignLink(
  677. $column,
  678. $backup_field,
  679. $column_name_appendix,
  680. $onChangeClause,
  681. $tabindex,
  682. $tabindex_for_value,
  683. $idindex,
  684. $data,
  685. $paramTableDbArray,
  686. $rownumber,
  687. $readOnly
  688. );
  689. } elseif (is_array($foreignData['disp_row'])) {
  690. $html_output .= $this->dispRowForeignData(
  691. $column,
  692. $backup_field,
  693. $column_name_appendix,
  694. $onChangeClause,
  695. $tabindex,
  696. $tabindex_for_value,
  697. $idindex,
  698. $data,
  699. $foreignData,
  700. $readOnly
  701. );
  702. } elseif ((
  703. $GLOBALS['cfg']['LongtextDoubleTextarea']
  704. && mb_strstr($column['pma_type'], 'longtext'))
  705. || mb_strstr($column['pma_type'], 'json')
  706. ) {
  707. $html_output .= $this->getTextarea(
  708. $column,
  709. $backup_field,
  710. $column_name_appendix,
  711. $onChangeClause,
  712. $tabindex,
  713. $tabindex_for_value,
  714. $idindex,
  715. $text_dir,
  716. $special_chars_encoded,
  717. $data_type,
  718. $readOnly
  719. );
  720. } elseif (mb_strstr($column['pma_type'], 'text')) {
  721. $html_output .= $this->getTextarea(
  722. $column,
  723. $backup_field,
  724. $column_name_appendix,
  725. $onChangeClause,
  726. $tabindex,
  727. $tabindex_for_value,
  728. $idindex,
  729. $text_dir,
  730. $special_chars_encoded,
  731. $data_type,
  732. $readOnly
  733. );
  734. $html_output .= "\n";
  735. if (mb_strlen($special_chars) > 32000) {
  736. $html_output .= "</td>\n";
  737. $html_output .= '<td>' . __(
  738. 'Because of its length,<br> this column might not be editable.'
  739. );
  740. }
  741. } elseif ($column['pma_type'] === 'enum') {
  742. $html_output .= $this->getPmaTypeEnum(
  743. $column,
  744. $backup_field,
  745. $column_name_appendix,
  746. $extracted_columnspec,
  747. $onChangeClause,
  748. $tabindex,
  749. $tabindex_for_value,
  750. $idindex,
  751. $data,
  752. $readOnly
  753. );
  754. } elseif ($column['pma_type'] === 'set') {
  755. $html_output .= $this->getPmaTypeSet(
  756. $column,
  757. $extracted_columnspec,
  758. $backup_field,
  759. $column_name_appendix,
  760. $onChangeClause,
  761. $tabindex,
  762. $tabindex_for_value,
  763. $idindex,
  764. $data,
  765. $readOnly
  766. );
  767. } elseif ($column['is_binary'] || $column['is_blob']) {
  768. $html_output .= $this->getBinaryAndBlobColumn(
  769. $column,
  770. $data,
  771. $special_chars,
  772. $biggest_max_file_size,
  773. $backup_field,
  774. $column_name_appendix,
  775. $onChangeClause,
  776. $tabindex,
  777. $tabindex_for_value,
  778. $idindex,
  779. $text_dir,
  780. $special_chars_encoded,
  781. $vkey,
  782. $is_upload,
  783. $readOnly
  784. );
  785. } elseif (! in_array($column['pma_type'], $no_support_types)) {
  786. $html_output .= $this->getValueColumnForOtherDatatypes(
  787. $column,
  788. $default_char_editing,
  789. $backup_field,
  790. $column_name_appendix,
  791. $onChangeClause,
  792. $tabindex,
  793. $special_chars,
  794. $tabindex_for_value,
  795. $idindex,
  796. $text_dir,
  797. $special_chars_encoded,
  798. $data,
  799. $extracted_columnspec,
  800. $readOnly
  801. );
  802. }
  803. if (in_array($column['pma_type'], $gis_data_types)) {
  804. $html_output .= $this->getHtmlForGisDataTypes();
  805. }
  806. return $html_output;
  807. }
  808. /**
  809. * Get HTML for foreign link in insert form
  810. *
  811. * @param array $column description of column in given table
  812. * @param string $backup_field hidden input field
  813. * @param string $column_name_appendix the name attribute
  814. * @param string $onChangeClause onchange clause for fields
  815. * @param int $tabindex tab index
  816. * @param int $tabindex_for_value offset for the values tabindex
  817. * @param int $idindex id index
  818. * @param string $data data to edit
  819. * @param array $paramTableDbArray array containing $table and $db
  820. * @param int $rownumber the row number
  821. * @param bool $readOnly is column read only or not
  822. *
  823. * @return string an html snippet
  824. */
  825. private function getForeignLink(
  826. array $column,
  827. $backup_field,
  828. $column_name_appendix,
  829. $onChangeClause,
  830. $tabindex,
  831. $tabindex_for_value,
  832. $idindex,
  833. $data,
  834. array $paramTableDbArray,
  835. $rownumber,
  836. $readOnly
  837. ) {
  838. [$table, $db] = $paramTableDbArray;
  839. $html_output = '';
  840. $html_output .= $backup_field . "\n";
  841. $html_output .= '<input type="hidden" name="fields_type'
  842. . $column_name_appendix . '" value="foreign">';
  843. $html_output .= '<input type="text" name="fields' . $column_name_appendix . '" '
  844. . 'class="textfield" '
  845. . $onChangeClause . ' '
  846. . ($readOnly ? 'readonly="readonly" ' : '')
  847. . 'tabindex="' . ($tabindex + $tabindex_for_value) . '" '
  848. . 'id="field_' . $idindex . '_3" '
  849. . 'value="' . htmlspecialchars($data) . '">';
  850. $html_output .= '<a class="ajax browse_foreign" href="'
  851. . Url::getFromRoute('/browse-foreigners')
  852. . '" data-post="'
  853. . Url::getCommon(
  854. [
  855. 'db' => $db,
  856. 'table' => $table,
  857. 'field' => $column['Field'],
  858. 'rownumber' => $rownumber,
  859. 'data' => $data,
  860. ],
  861. ''
  862. ) . '">'
  863. . Generator::getIcon('b_browse', __('Browse foreign values')) . '</a>';
  864. return $html_output;
  865. }
  866. /**
  867. * Get HTML to display foreign data
  868. *
  869. * @param array $column description of column in given table
  870. * @param string $backup_field hidden input field
  871. * @param string $column_name_appendix the name attribute
  872. * @param string $onChangeClause onchange clause for fields
  873. * @param int $tabindex tab index
  874. * @param int $tabindex_for_value offset for the values tabindex
  875. * @param int $idindex id index
  876. * @param string $data data to edit
  877. * @param array $foreignData data about the foreign keys
  878. * @param bool $readOnly is display read only or not
  879. *
  880. * @return string an html snippet
  881. */
  882. private function dispRowForeignData(
  883. $column,
  884. $backup_field,
  885. $column_name_appendix,
  886. $onChangeClause,
  887. $tabindex,
  888. $tabindex_for_value,
  889. $idindex,
  890. $data,
  891. array $foreignData,
  892. $readOnly
  893. ) {
  894. $html_output = '';
  895. $html_output .= $backup_field . "\n";
  896. $html_output .= '<input type="hidden"'
  897. . ' name="fields_type' . $column_name_appendix . '"';
  898. if ($column['is_binary']) {
  899. $html_output .= ' value="hex">';
  900. } else {
  901. $html_output .= ' value="foreign">';
  902. }
  903. $html_output .= '<select name="fields' . $column_name_appendix . '"'
  904. . ' ' . $onChangeClause
  905. . ' class="textfield"'
  906. . ($readOnly ? ' disabled' : '')
  907. . ' tabindex="' . ($tabindex + $tabindex_for_value) . '"'
  908. . ' id="field_' . $idindex . '_3">';
  909. $html_output .= $this->relation->foreignDropdown(
  910. $foreignData['disp_row'],
  911. $foreignData['foreign_field'],
  912. $foreignData['foreign_display'],
  913. $data,
  914. $GLOBALS['cfg']['ForeignKeyMaxLimit']
  915. );
  916. $html_output .= '</select>';
  917. //Add hidden input, as disabled <select> input does not included in POST.
  918. if ($readOnly) {
  919. $html_output .= '<input name="fields' . $column_name_appendix . '"'
  920. . ' type="hidden" value="' . htmlspecialchars($data) . '">';
  921. }
  922. return $html_output;
  923. }
  924. /**
  925. * Get HTML textarea for insert form
  926. *
  927. * @param array $column column information
  928. * @param string $backup_field hidden input field
  929. * @param string $column_name_appendix the name attribute
  930. * @param string $onChangeClause onchange clause for fields
  931. * @param int $tabindex tab index
  932. * @param int $tabindex_for_value offset for the values tabindex
  933. * @param int $idindex id index
  934. * @param string $text_dir text direction
  935. * @param string $special_chars_encoded replaced char if the string starts
  936. * with a \r\n pair (0x0d0a) add an extra \n
  937. * @param string $data_type the html5 data-* attribute type
  938. * @param bool $readOnly is column read only or not
  939. *
  940. * @return string an html snippet
  941. */
  942. private function getTextarea(
  943. array $column,
  944. $backup_field,
  945. $column_name_appendix,
  946. $onChangeClause,
  947. $tabindex,
  948. $tabindex_for_value,
  949. $idindex,
  950. $text_dir,
  951. $special_chars_encoded,
  952. $data_type,
  953. $readOnly
  954. ) {
  955. $the_class = '';
  956. $textAreaRows = $GLOBALS['cfg']['TextareaRows'];
  957. $textareaCols = $GLOBALS['cfg']['TextareaCols'];
  958. if ($column['is_char']) {
  959. /**
  960. * @todo clarify the meaning of the "textfield" class and explain
  961. * why character columns have the "char" class instead
  962. */
  963. $the_class = 'char charField';
  964. $textAreaRows = max($GLOBALS['cfg']['CharTextareaRows'], 7);
  965. $textareaCols = $GLOBALS['cfg']['CharTextareaCols'];
  966. $extracted_columnspec = Util::extractColumnSpec(
  967. $column['Type']
  968. );
  969. $maxlength = $extracted_columnspec['spec_in_brackets'];
  970. } elseif ($GLOBALS['cfg']['LongtextDoubleTextarea']
  971. && mb_strstr($column['pma_type'], 'longtext')
  972. ) {
  973. $textAreaRows = $GLOBALS['cfg']['TextareaRows'] * 2;
  974. $textareaCols = $GLOBALS['cfg']['TextareaCols'] * 2;
  975. }
  976. return $backup_field . "\n"
  977. . '<textarea name="fields' . $column_name_appendix . '"'
  978. . ' class="' . $the_class . '"'
  979. . ($readOnly ? ' readonly="readonly"' : '')
  980. . (isset($maxlength) ? ' data-maxlength="' . $maxlength . '"' : '')
  981. . ' rows="' . $textAreaRows . '"'
  982. . ' cols="' . $textareaCols . '"'
  983. . ' dir="' . $text_dir . '"'
  984. . ' id="field_' . $idindex . '_3"'
  985. . (! empty($onChangeClause) ? ' ' . $onChangeClause : '')
  986. . ' tabindex="' . ($tabindex + $tabindex_for_value) . '"'
  987. . ' data-type="' . $data_type . '">'
  988. . $special_chars_encoded
  989. . '</textarea>';
  990. }
  991. /**
  992. * Get HTML for enum type
  993. *
  994. * @param array $column description of column in given table
  995. * @param string $backup_field hidden input field
  996. * @param string $column_name_appendix the name attribute
  997. * @param array $extracted_columnspec associative array containing type,
  998. * spec_in_brackets and possibly
  999. * enum_set_values (another array)
  1000. * @param string $onChangeClause onchange clause for fields
  1001. * @param int $tabindex tab index
  1002. * @param int $tabindex_for_value offset for the values tabindex
  1003. * @param int $idindex id index
  1004. * @param mixed $data data to edit
  1005. * @param bool $readOnly is column read only or not
  1006. *
  1007. * @return string an html snippet
  1008. */
  1009. private function getPmaTypeEnum(
  1010. array $column,
  1011. $backup_field,
  1012. $column_name_appendix,
  1013. array $extracted_columnspec,
  1014. $onChangeClause,
  1015. $tabindex,
  1016. $tabindex_for_value,
  1017. $idindex,
  1018. $data,
  1019. $readOnly
  1020. ) {
  1021. $html_output = '';
  1022. if (! isset($column['values'])) {
  1023. $column['values'] = $this->getColumnEnumValues(
  1024. $column,
  1025. $extracted_columnspec
  1026. );
  1027. }
  1028. $column_enum_values = $column['values'];
  1029. $html_output .= '<input type="hidden" name="fields_type'
  1030. . $column_name_appendix . '" value="enum">';
  1031. $html_output .= "\n" . ' ' . $backup_field . "\n";
  1032. if (mb_strlen($column['Type']) > 20) {
  1033. $html_output .= $this->getDropDownDependingOnLength(
  1034. $column,
  1035. $column_name_appendix,
  1036. $onChangeClause,
  1037. $tabindex,
  1038. $tabindex_for_value,
  1039. $idindex,
  1040. $data,
  1041. $column_enum_values,
  1042. $readOnly
  1043. );
  1044. } else {
  1045. $html_output .= $this->getRadioButtonDependingOnLength(
  1046. $column_name_appendix,
  1047. $onChangeClause,
  1048. $tabindex,
  1049. $column,
  1050. $tabindex_for_value,
  1051. $idindex,
  1052. $data,
  1053. $column_enum_values,
  1054. $readOnly
  1055. );
  1056. }
  1057. return $html_output;
  1058. }
  1059. /**
  1060. * Get column values
  1061. *
  1062. * @param array $column description of column in given table
  1063. * @param array $extracted_columnspec associative array containing type,
  1064. * spec_in_brackets and possibly enum_set_values
  1065. * (another array)
  1066. *
  1067. * @return array column values as an associative array
  1068. */
  1069. private function getColumnEnumValues(array $column, array $extracted_columnspec)
  1070. {
  1071. $column['values'] = [];
  1072. foreach ($extracted_columnspec['enum_set_values'] as $val) {
  1073. $column['values'][] = [
  1074. 'plain' => $val,
  1075. 'html' => htmlspecialchars($val),
  1076. ];
  1077. }
  1078. return $column['values'];
  1079. }
  1080. /**
  1081. * Get HTML drop down for more than 20 string length
  1082. *
  1083. * @param array $column description of column in given table
  1084. * @param string $column_name_appendix the name attribute
  1085. * @param string $onChangeClause onchange clause for fields
  1086. * @param int $tabindex tab index
  1087. * @param int $tabindex_for_value offset for the values tabindex
  1088. * @param int $idindex id index
  1089. * @param string $data data to edit
  1090. * @param array $column_enum_values $column['values']
  1091. * @param bool $readOnly is column read only or not
  1092. *
  1093. * @return string an html snippet
  1094. */
  1095. private function getDropDownDependingOnLength(
  1096. array $column,
  1097. $column_name_appendix,
  1098. $onChangeClause,
  1099. $tabindex,
  1100. $tabindex_for_value,
  1101. $idindex,
  1102. $data,
  1103. array $column_enum_values,
  1104. $readOnly
  1105. ) {
  1106. $html_output = '<select name="fields' . $column_name_appendix . '"'
  1107. . ' ' . $onChangeClause
  1108. . ' class="textfield"'
  1109. . ' tabindex="' . ($tabindex + $tabindex_for_value) . '"'
  1110. . ($readOnly ? ' disabled' : '')
  1111. . ' id="field_' . $idindex . '_3">';
  1112. $html_output .= '<option value="">&nbsp;</option>' . "\n";
  1113. $selected_html = '';
  1114. foreach ($column_enum_values as $enum_value) {
  1115. $html_output .= '<option value="' . $enum_value['html'] . '"';
  1116. if ($data == $enum_value['plain']
  1117. || ($data == ''
  1118. && (! isset($_POST['where_clause']) || $column['Null'] !== 'YES')
  1119. && isset($column['Default'])
  1120. && $enum_value['plain'] == $column['Default'])
  1121. ) {
  1122. $html_output .= ' selected="selected"';
  1123. $selected_html = $enum_value['html'];
  1124. }
  1125. $html_output .= '>' . $enum_value['html'] . '</option>' . "\n";
  1126. }
  1127. $html_output .= '</select>';
  1128. //Add hidden input, as disabled <select> input does not included in POST.
  1129. if ($readOnly) {
  1130. $html_output .= '<input name="fields' . $column_name_appendix . '"'
  1131. . ' type="hidden" value="' . $selected_html . '">';
  1132. }
  1133. return $html_output;
  1134. }
  1135. /**
  1136. * Get HTML radio button for less than 20 string length
  1137. *
  1138. * @param string $column_name_appendix the name attribute
  1139. * @param string $onChangeClause onchange clause for fields
  1140. * @param int $tabindex tab index
  1141. * @param array $column description of column in given table
  1142. * @param int $tabindex_for_value offset for the values tabindex
  1143. * @param int $idindex id index
  1144. * @param string $data data to edit
  1145. * @param array $column_enum_values $column['values']
  1146. * @param bool $readOnly is column read only or not
  1147. *
  1148. * @return string an html snippet
  1149. */
  1150. private function getRadioButtonDependingOnLength(
  1151. $column_name_appendix,
  1152. $onChangeClause,
  1153. $tabindex,
  1154. array $column,
  1155. $tabindex_for_value,
  1156. $idindex,
  1157. $data,
  1158. array $column_enum_values,
  1159. $readOnly
  1160. ) {
  1161. $j = 0;
  1162. $html_output = '';
  1163. foreach ($column_enum_values as $enum_value) {
  1164. $html_output .= ' '
  1165. . '<input type="radio" name="fields' . $column_name_appendix . '"'
  1166. . ' class="textfield"'
  1167. . ' value="' . $enum_value['html'] . '"'
  1168. . ' id="field_' . $idindex . '_3_' . $j . '"'
  1169. . ' ' . $onChangeClause;
  1170. if ($data == $enum_value['plain']
  1171. || ($data == ''
  1172. && (! isset($_POST['where_clause']) || $column['Null'] !== 'YES')
  1173. && isset($column['Default'])
  1174. && $enum_value['plain'] == $column['Default'])
  1175. ) {
  1176. $html_output .= ' checked="checked"';
  1177. } elseif ($readOnly) {
  1178. $html_output .= ' disabled';
  1179. }
  1180. $html_output .= ' tabindex="' . ($tabindex + $tabindex_for_value) . '">';
  1181. $html_output .= '<label for="field_' . $idindex . '_3_' . $j . '">'
  1182. . $enum_value['html'] . '</label>' . "\n";
  1183. $j++;
  1184. }
  1185. return $html_output;
  1186. }
  1187. /**
  1188. * Get the HTML for 'set' pma type
  1189. *
  1190. * @param array $column description of column in given table
  1191. * @param array $extracted_columnspec associative array containing type,
  1192. * spec_in_brackets and possibly
  1193. * enum_set_values (another array)
  1194. * @param string $backup_field hidden input field
  1195. * @param string $column_name_appendix the name attribute
  1196. * @param string $onChangeClause onchange clause for fields
  1197. * @param int $tabindex tab index
  1198. * @param int $tabindex_for_value offset for the values tabindex
  1199. * @param int $idindex id index
  1200. * @param string $data description of the column field
  1201. * @param bool $readOnly is column read only or not
  1202. *
  1203. * @return string an html snippet
  1204. */
  1205. private function getPmaTypeSet(
  1206. array $column,
  1207. array $extracted_columnspec,
  1208. $backup_field,
  1209. $column_name_appendix,
  1210. $onChangeClause,
  1211. $tabindex,
  1212. $tabindex_for_value,
  1213. $idindex,
  1214. $data,
  1215. $readOnly
  1216. ) {
  1217. [$column_set_values, $select_size] = $this->getColumnSetValueAndSelectSize(
  1218. $column,
  1219. $extracted_columnspec
  1220. );
  1221. $vset = array_flip(explode(',', $data));
  1222. $html_output = $backup_field . "\n";
  1223. $html_output .= '<input type="hidden" name="fields_type'
  1224. . $column_name_appendix . '" value="set">';
  1225. $html_output .= '<select name="fields' . $column_name_appendix . '[]"'
  1226. . ' class="textfield"'
  1227. . ($readOnly ? ' disabled' : '')
  1228. . ' size="' . $select_size . '"'
  1229. . ' multiple="multiple"'
  1230. . ' ' . $onChangeClause
  1231. . ' tabindex="' . ($tabindex + $tabindex_for_value) . '"'
  1232. . ' id="field_' . $idindex . '_3">';
  1233. $selected_html = '';
  1234. foreach ($column_set_values as $column_set_value) {
  1235. $html_output .= '<option value="' . $column_set_value['html'] . '"';
  1236. if (isset($vset[$column_set_value['plain']])) {
  1237. $html_output .= ' selected="selected"';
  1238. $selected_html = $column_set_value['html'];
  1239. }
  1240. $html_output .= '>' . $column_set_value['html'] . '</option>' . "\n";
  1241. }
  1242. $html_output .= '</select>';
  1243. //Add hidden input, as disabled <select> input does not included in POST.
  1244. if ($readOnly) {
  1245. $html_output .= '<input name="fields' . $column_name_appendix . '[]"'
  1246. . ' type="hidden" value="' . $selected_html . '">';
  1247. }
  1248. return $html_output;
  1249. }
  1250. /**
  1251. * Retrieve column 'set' value and select size
  1252. *
  1253. * @param array $column description of column in given table
  1254. * @param array $extracted_columnspec associative array containing type,
  1255. * spec_in_brackets and possibly enum_set_values
  1256. * (another array)
  1257. *
  1258. * @return array $column['values'], $column['select_size']
  1259. */
  1260. private function getColumnSetValueAndSelectSize(
  1261. array $column,
  1262. array $extracted_columnspec
  1263. ) {
  1264. if (! isset($column['values'])) {
  1265. $column['values'] = [];
  1266. foreach ($extracted_columnspec['enum_set_values'] as $val) {
  1267. $column['values'][] = [
  1268. 'plain' => $val,
  1269. 'html' => htmlspecialchars($val),
  1270. ];
  1271. }
  1272. $column['select_size'] = min(4, count($column['values']));
  1273. }
  1274. return [
  1275. $column['values'],
  1276. $column['select_size'],
  1277. ];
  1278. }
  1279. /**
  1280. * Get HTML for binary and blob column
  1281. *
  1282. * @param array $column description of column in given table
  1283. * @param string|null $data data to edit
  1284. * @param string $special_chars special characters
  1285. * @param int $biggest_max_file_size biggest max file size for uploading
  1286. * @param string $backup_field hidden input field
  1287. * @param string $column_name_appendix the name attribute
  1288. * @param string $onChangeClause onchange clause for fields
  1289. * @param int $tabindex tab index
  1290. * @param int $tabindex_for_value offset for the values tabindex
  1291. * @param int $idindex id index
  1292. * @param string $text_dir text direction
  1293. * @param string $special_chars_encoded replaced char if the string starts
  1294. * with a \r\n pair (0x0d0a) add an
  1295. * extra \n
  1296. * @param string $vkey [multi_edit]['row_id']
  1297. * @param bool $is_upload is upload or not
  1298. * @param bool $readOnly is column read only or not
  1299. *
  1300. * @return string an html snippet
  1301. */
  1302. private function getBinaryAndBlobColumn(
  1303. array $column,
  1304. ?string $data,
  1305. $special_chars,
  1306. $biggest_max_file_size,
  1307. $backup_field,
  1308. $column_name_appendix,
  1309. $onChangeClause,
  1310. $tabindex,
  1311. $tabindex_for_value,
  1312. $idindex,
  1313. $text_dir,
  1314. $special_chars_encoded,
  1315. $vkey,
  1316. $is_upload,
  1317. $readOnly
  1318. ) {
  1319. $html_output = '';
  1320. // Add field type : Protected or Hexadecimal
  1321. $fields_type_html = '<input type="hidden" name="fields_type'
  1322. . $column_name_appendix . '" value="%s">';
  1323. // Default value : hex
  1324. $fields_type_val = 'hex';
  1325. if (($GLOBALS['cfg']['ProtectBinary'] === 'blob' && $column['is_blob'])
  1326. || ($GLOBALS['cfg']['ProtectBinary'] === 'all')
  1327. || ($GLOBALS['cfg']['ProtectBinary'] === 'noblob' && ! $column['is_blob'])
  1328. ) {
  1329. $html_output .= __('Binary - do not edit');
  1330. if (isset($data)) {
  1331. $data_size = Util::formatByteDown(
  1332. mb_strlen(stripslashes($data)),
  1333. 3,
  1334. 1
  1335. );
  1336. $html_output .= ' (' . $data_size[0] . ' ' . $data_size[1] . ')';
  1337. unset($data_size);
  1338. }
  1339. $fields_type_val = 'protected';
  1340. $html_output .= '<input type="hidden" name="fields'
  1341. . $column_name_appendix . '" value="">';
  1342. } elseif ($column['is_blob']
  1343. || ($column['len'] > $GLOBALS['cfg']['LimitChars'])
  1344. ) {
  1345. $html_output .= "\n" . $this->getTextarea(
  1346. $column,
  1347. $backup_field,
  1348. $column_name_appendix,
  1349. $onChangeClause,
  1350. $tabindex,
  1351. $tabindex_for_value,
  1352. $idindex,
  1353. $text_dir,
  1354. $special_chars_encoded,
  1355. 'HEX',
  1356. $readOnly
  1357. );
  1358. } else {
  1359. // field size should be at least 4 and max $GLOBALS['cfg']['LimitChars']
  1360. $fieldsize = min(max($column['len'], 4), $GLOBALS['cfg']['LimitChars']);
  1361. $html_output .= "\n" . $backup_field . "\n" . $this->getHtmlInput(
  1362. $column,
  1363. $column_name_appendix,
  1364. $special_chars,
  1365. $fieldsize,
  1366. $onChangeClause,
  1367. $tabindex,
  1368. $tabindex_for_value,
  1369. $idindex,
  1370. 'HEX',
  1371. $readOnly
  1372. );
  1373. }
  1374. $html_output .= sprintf($fields_type_html, $fields_type_val);
  1375. if ($is_upload && $column['is_blob'] && ! $readOnly) {
  1376. // We don't want to prevent users from using
  1377. // browser's default drag-drop feature on some page(s),
  1378. // so we add noDragDrop class to the input
  1379. $html_output .= '<br>'
  1380. . '<input type="file"'
  1381. . ' name="fields_upload' . $vkey . '[' . $column['Field_md5'] . ']"'
  1382. . ' class="textfield noDragDrop" id="field_' . $idindex . '_3" size="10"'
  1383. . ' ' . $onChangeClause . '>&nbsp;';
  1384. [$html_out] = $this->getMaxUploadSize(
  1385. $column,
  1386. $biggest_max_file_size
  1387. );
  1388. $html_output .= $html_out;
  1389. }
  1390. if (! empty($GLOBALS['cfg']['UploadDir']) && ! $readOnly) {
  1391. $html_output .= $this->getSelectOptionForUpload($vkey, $column);
  1392. }
  1393. return $html_output;
  1394. }
  1395. /**
  1396. * Get HTML input type
  1397. *
  1398. * @param array $column description of column in given table
  1399. * @param string $column_name_appendix the name attribute
  1400. * @param string $special_chars special characters
  1401. * @param int $fieldsize html field size
  1402. * @param string $onChangeClause onchange clause for fields
  1403. * @param int $tabindex tab index
  1404. * @param int $tabindex_for_value offset for the values tabindex
  1405. * @param int $idindex id index
  1406. * @param string $data_type the html5 data-* attribute type
  1407. * @param bool $readOnly is column read only or not
  1408. *
  1409. * @return string an html snippet
  1410. */
  1411. private function getHtmlInput(
  1412. array $column,
  1413. $column_name_appendix,
  1414. $special_chars,
  1415. $fieldsize,
  1416. $onChangeClause,
  1417. $tabindex,
  1418. $tabindex_for_value,
  1419. $idindex,
  1420. $data_type,
  1421. $readOnly
  1422. ) {
  1423. $input_type = 'text';
  1424. // do not use the 'date' or 'time' types here; they have no effect on some
  1425. // browsers and create side effects (see bug #4218)
  1426. $the_class = 'textfield';
  1427. // verify True_Type which does not contain the parentheses and length
  1428. if (! $readOnly) {
  1429. if ($column['True_Type'] === 'date') {
  1430. $the_class .= ' datefield';
  1431. } elseif ($column['True_Type'] === 'time') {
  1432. $the_class .= ' timefield';
  1433. } elseif ($column['True_Type'] === 'datetime'
  1434. || $column['True_Type'] === 'timestamp'
  1435. ) {
  1436. $the_class .= ' datetimefield';
  1437. }
  1438. }
  1439. $input_min_max = false;
  1440. if (in_array($column['True_Type'], $this->dbi->types->getIntegerTypes())) {
  1441. $extracted_columnspec = Util::extractColumnSpec(
  1442. $column['Type']
  1443. );
  1444. $is_unsigned = $extracted_columnspec['unsigned'];
  1445. $min_max_values = $this->dbi->types->getIntegerRange(
  1446. $column['True_Type'],
  1447. ! $is_unsigned
  1448. );
  1449. $input_min_max = 'min="' . $min_max_values[0] . '" '
  1450. . 'max="' . $min_max_values[1] . '"';
  1451. $data_type = 'INT';
  1452. }
  1453. return '<input type="' . $input_type . '"'
  1454. . ' name="fields' . $column_name_appendix . '"'
  1455. . ' value="' . $special_chars . '" size="' . $fieldsize . '"'
  1456. . (isset($column['is_char']) && $column['is_char']
  1457. ? ' data-maxlength="' . $fieldsize . '"'
  1458. : '')
  1459. . ($readOnly ? ' readonly="readonly"' : '')
  1460. . ($input_min_max !== false ? ' ' . $input_min_max : '')
  1461. . ' data-type="' . $data_type . '"'
  1462. . ($input_type === 'time' ? ' step="1"' : '')
  1463. . ' class="' . $the_class . '" ' . $onChangeClause
  1464. . ' tabindex="' . ($tabindex + $tabindex_for_value) . '"'
  1465. . ' id="field_' . $idindex . '_3">';
  1466. }
  1467. /**
  1468. * Get HTML select option for upload
  1469. *
  1470. * @param string $vkey [multi_edit]['row_id']
  1471. * @param array $column description of column in given table
  1472. *
  1473. * @return string|null an html snippet
  1474. */
  1475. private function getSelectOptionForUpload($vkey, array $column)
  1476. {
  1477. $files = $this->fileListing->getFileSelectOptions(
  1478. Util::userDir($GLOBALS['cfg']['UploadDir'])
  1479. );
  1480. if ($files === false) {
  1481. return '<span style="color:red">' . __('Error') . '</span><br>' . "\n"
  1482. . __('The directory you set for upload work cannot be reached.') . "\n";
  1483. }
  1484. if (! empty($files)) {
  1485. return "<br>\n"
  1486. . '<i>' . __('Or') . '</i> '
  1487. . __('web server upload directory:') . '<br>' . "\n"
  1488. . '<select size="1" name="fields_uploadlocal'
  1489. . $vkey . '[' . $column['Field_md5'] . ']">' . "\n"
  1490. . '<option value="" selected="selected"></option>' . "\n"
  1491. . $files
  1492. . '</select>' . "\n";
  1493. }
  1494. return null;
  1495. }
  1496. /**
  1497. * Retrieve the maximum upload file size
  1498. *
  1499. * @param array $column description of column in given table
  1500. * @param int $biggest_max_file_size biggest max file size for uploading
  1501. *
  1502. * @return array an html snippet and $biggest_max_file_size
  1503. */
  1504. private function getMaxUploadSize(array $column, $biggest_max_file_size)
  1505. {
  1506. // find maximum upload size, based on field type
  1507. /**
  1508. * @todo with functions this is not so easy, as you can basically
  1509. * process any data with function like MD5
  1510. */
  1511. global $max_upload_size;
  1512. $max_field_sizes = [
  1513. 'tinyblob' => '256',
  1514. 'blob' => '65536',
  1515. 'mediumblob' => '16777216',
  1516. 'longblob' => '4294967296',// yeah, really
  1517. ];
  1518. $this_field_max_size = $max_upload_size; // from PHP max
  1519. if ($this_field_max_size > $max_field_sizes[$column['pma_type']]) {
  1520. $this_field_max_size = $max_field_sizes[$column['pma_type']];
  1521. }
  1522. $html_output
  1523. = Util::getFormattedMaximumUploadSize(
  1524. $this_field_max_size
  1525. ) . "\n";
  1526. // do not generate here the MAX_FILE_SIZE, because we should
  1527. // put only one in the form to accommodate the biggest field
  1528. if ($this_field_max_size > $biggest_max_file_size) {
  1529. $biggest_max_file_size = $this_field_max_size;
  1530. }
  1531. return [
  1532. $html_output,
  1533. $biggest_max_file_size,
  1534. ];
  1535. }
  1536. /**
  1537. * Get HTML for the Value column of other datatypes
  1538. * (here, "column" is used in the sense of HTML column in HTML table)
  1539. *
  1540. * @param array $column description of column in given table
  1541. * @param string $default_char_editing default char editing mode which is stored
  1542. * in the config.inc.php script
  1543. * @param string $backup_field hidden input field
  1544. * @param string $column_name_appendix the name attribute
  1545. * @param string $onChangeClause onchange clause for fields
  1546. * @param int $tabindex tab index
  1547. * @param string $special_chars special characters
  1548. * @param int $tabindex_for_value offset for the values tabindex
  1549. * @param int $idindex id index
  1550. * @param string $text_dir text direction
  1551. * @param string $special_chars_encoded replaced char if the string starts
  1552. * with a \r\n pair (0x0d0a) add an extra \n
  1553. * @param string $data data to edit
  1554. * @param array $extracted_columnspec associative array containing type,
  1555. * spec_in_brackets and possibly
  1556. * enum_set_values (another array)
  1557. * @param bool $readOnly is column read only or not
  1558. *
  1559. * @return string an html snippet
  1560. */
  1561. private function getValueColumnForOtherDatatypes(
  1562. array $column,
  1563. $default_char_editing,
  1564. $backup_field,
  1565. $column_name_appendix,
  1566. $onChangeClause,
  1567. $tabindex,
  1568. $special_chars,
  1569. $tabindex_for_value,
  1570. $idindex,
  1571. $text_dir,
  1572. $special_chars_encoded,
  1573. $data,
  1574. array $extracted_columnspec,
  1575. $readOnly
  1576. ) {
  1577. // HTML5 data-* attribute data-type
  1578. $data_type = $this->dbi->types->getTypeClass($column['True_Type']);
  1579. $fieldsize = $this->getColumnSize($column, $extracted_columnspec);
  1580. $html_output = $backup_field . "\n";
  1581. if ($column['is_char']
  1582. && ($GLOBALS['cfg']['CharEditing'] === 'textarea'
  1583. || mb_strpos($data, "\n") !== false)
  1584. ) {
  1585. $html_output .= "\n";
  1586. $GLOBALS['cfg']['CharEditing'] = $default_char_editing;
  1587. $html_output .= $this->getTextarea(
  1588. $column,
  1589. $backup_field,
  1590. $column_name_appendix,
  1591. $onChangeClause,
  1592. $tabindex,
  1593. $tabindex_for_value,
  1594. $idindex,
  1595. $text_dir,
  1596. $special_chars_encoded,
  1597. $data_type,
  1598. $readOnly
  1599. );
  1600. } else {
  1601. $html_output .= $this->getHtmlInput(
  1602. $column,
  1603. $column_name_appendix,
  1604. $special_chars,
  1605. $fieldsize,
  1606. $onChangeClause,
  1607. $tabindex,
  1608. $tabindex_for_value,
  1609. $idindex,
  1610. $data_type,
  1611. $readOnly
  1612. );
  1613. if (preg_match('/(VIRTUAL|PERSISTENT|GENERATED)/', $column['Extra'])
  1614. && strpos($column['Extra'], 'DEFAULT_GENERATED') === false
  1615. ) {
  1616. $html_output .= '<input type="hidden" name="virtual'
  1617. . $column_name_appendix . '" value="1">';
  1618. }
  1619. if ($column['Extra'] === 'auto_increment') {
  1620. $html_output .= '<input type="hidden" name="auto_increment'
  1621. . $column_name_appendix . '" value="1">';
  1622. }
  1623. if (substr($column['pma_type'], 0, 9) === 'timestamp') {
  1624. $html_output .= '<input type="hidden" name="fields_type'
  1625. . $column_name_appendix . '" value="timestamp">';
  1626. }
  1627. if (substr($column['pma_type'], 0, 4) === 'date') {
  1628. $type = substr($column['pma_type'], 0, 8) === 'datetime' ? 'datetime' : 'date';
  1629. $html_output .= '<input type="hidden" name="fields_type'
  1630. . $column_name_appendix . '" value="' . $type . '">';
  1631. }
  1632. if ($column['True_Type'] === 'bit') {
  1633. $html_output .= '<input type="hidden" name="fields_type'
  1634. . $column_name_appendix . '" value="bit">';
  1635. }
  1636. }
  1637. return $html_output;
  1638. }
  1639. /**
  1640. * Get the field size
  1641. *
  1642. * @param array $column description of column in given table
  1643. * @param array $extracted_columnspec associative array containing type,
  1644. * spec_in_brackets and possibly enum_set_values
  1645. * (another array)
  1646. *
  1647. * @return int field size
  1648. */
  1649. private function getColumnSize(array $column, array $extracted_columnspec)
  1650. {
  1651. if ($column['is_char']) {
  1652. $fieldsize = $extracted_columnspec['spec_in_brackets'];
  1653. if ($fieldsize > $GLOBALS['cfg']['MaxSizeForInputField']) {
  1654. /**
  1655. * This case happens for CHAR or VARCHAR columns which have
  1656. * a size larger than the maximum size for input field.
  1657. */
  1658. $GLOBALS['cfg']['CharEditing'] = 'textarea';
  1659. }
  1660. } else {
  1661. /**
  1662. * This case happens for example for INT or DATE columns;
  1663. * in these situations, the value returned in $column['len']
  1664. * seems appropriate.
  1665. */
  1666. $fieldsize = $column['len'];
  1667. }
  1668. return min(
  1669. max($fieldsize, $GLOBALS['cfg']['MinSizeForInputField']),
  1670. $GLOBALS['cfg']['MaxSizeForInputField']
  1671. );
  1672. }
  1673. /**
  1674. * Get HTML for gis data types
  1675. *
  1676. * @return string an html snippet
  1677. */
  1678. private function getHtmlForGisDataTypes()
  1679. {
  1680. $edit_str = Generator::getIcon('b_edit', __('Edit/Insert'));
  1681. return '<span class="open_gis_editor">'
  1682. . Generator::linkOrButton(
  1683. '#',
  1684. $edit_str,
  1685. [],
  1686. '_blank'
  1687. )
  1688. . '</span>';
  1689. }
  1690. /**
  1691. * get html for continue insertion form
  1692. *
  1693. * @param string $table name of the table
  1694. * @param string $db name of the database
  1695. * @param array $where_clause_array array of where clauses
  1696. * @param string $err_url error url
  1697. *
  1698. * @return string an html snippet
  1699. */
  1700. public function getContinueInsertionForm(
  1701. $table,
  1702. $db,
  1703. array $where_clause_array,
  1704. $err_url
  1705. ) {
  1706. return $this->template->render('table/insert/continue_insertion_form', [
  1707. 'db' => $db,
  1708. 'table' => $table,
  1709. 'where_clause_array' => $where_clause_array,
  1710. 'err_url' => $err_url,
  1711. 'goto' => $GLOBALS['goto'],
  1712. 'sql_query' => $_POST['sql_query'] ?? null,
  1713. 'has_where_clause' => isset($_POST['where_clause']),
  1714. 'insert_rows_default' => $GLOBALS['cfg']['InsertRows'],
  1715. ]);
  1716. }
  1717. /**
  1718. * Get action panel
  1719. *
  1720. * @param array|null $where_clause where clause
  1721. * @param string $after_insert insert mode, e.g. new_insert, same_insert
  1722. * @param int $tabindex tab index
  1723. * @param int $tabindex_for_value offset for the values tabindex
  1724. * @param bool $found_unique_key boolean variable for unique key
  1725. *
  1726. * @return string an html snippet
  1727. */
  1728. public function getActionsPanel(
  1729. $where_clause,
  1730. $after_insert,
  1731. $tabindex,
  1732. $tabindex_for_value,
  1733. $found_unique_key
  1734. ) {
  1735. $html_output = '<fieldset id="actions_panel">'
  1736. . '<table cellpadding="5" cellspacing="0" class="pma-table tdblock w-100">'
  1737. . '<tr>'
  1738. . '<td class="nowrap vmiddle">'
  1739. . $this->getSubmitTypeDropDown($where_clause, $tabindex, $tabindex_for_value)
  1740. . "\n";
  1741. $html_output .= '</td>'
  1742. . '<td class="vmiddle">'
  1743. . '&nbsp;&nbsp;&nbsp;<strong>'
  1744. . __('and then') . '</strong>&nbsp;&nbsp;&nbsp;'
  1745. . '</td>'
  1746. . '<td class="nowrap vmiddle">'
  1747. . $this->getAfterInsertDropDown(
  1748. $where_clause,
  1749. $after_insert,
  1750. $found_unique_key
  1751. )
  1752. . '</td>'
  1753. . '</tr>';
  1754. $html_output .= '<tr>'
  1755. . $this->getSubmitAndResetButtonForActionsPanel($tabindex, $tabindex_for_value)
  1756. . '</tr>'
  1757. . '</table>'
  1758. . '</fieldset>';
  1759. return $html_output;
  1760. }
  1761. /**
  1762. * Get a HTML drop down for submit types
  1763. *
  1764. * @param array|null $where_clause where clause
  1765. * @param int $tabindex tab index
  1766. * @param int $tabindex_for_value offset for the values tabindex
  1767. *
  1768. * @return string an html snippet
  1769. */
  1770. private function getSubmitTypeDropDown(
  1771. $where_clause,
  1772. $tabindex,
  1773. $tabindex_for_value
  1774. ) {
  1775. $html_output = '<select name="submit_type" class="control_at_footer" tabindex="'
  1776. . ($tabindex + $tabindex_for_value + 1) . '">';
  1777. if (isset($where_clause)) {
  1778. $html_output .= '<option value="save">' . __('Save') . '</option>';
  1779. }
  1780. $html_output .= '<option value="insert">'
  1781. . __('Insert as new row')
  1782. . '</option>'
  1783. . '<option value="insertignore">'
  1784. . __('Insert as new row and ignore errors')
  1785. . '</option>'
  1786. . '<option value="showinsert">'
  1787. . __('Show insert query')
  1788. . '</option>'
  1789. . '</select>';
  1790. return $html_output;
  1791. }
  1792. /**
  1793. * Get HTML drop down for after insert
  1794. *
  1795. * @param array|null $where_clause where clause
  1796. * @param string $after_insert insert mode, e.g. new_insert, same_insert
  1797. * @param bool $found_unique_key boolean variable for unique key
  1798. *
  1799. * @return string an html snippet
  1800. */
  1801. private function getAfterInsertDropDown($where_clause, $after_insert, $found_unique_key)
  1802. {
  1803. $html_output = '<select name="after_insert" class="control_at_footer">'
  1804. . '<option value="back" '
  1805. . ($after_insert === 'back' ? 'selected="selected"' : '') . '>'
  1806. . __('Go back to previous page') . '</option>'
  1807. . '<option value="new_insert" '
  1808. . ($after_insert === 'new_insert' ? 'selected="selected"' : '') . '>'
  1809. . __('Insert another new row') . '</option>';
  1810. if (isset($where_clause)) {
  1811. $html_output .= '<option value="same_insert" '
  1812. . ($after_insert === 'same_insert' ? 'selected="selected"' : '') . '>'
  1813. . __('Go back to this page') . '</option>';
  1814. // If we have just numeric primary key, we can also edit next
  1815. // in 2.8.2, we were looking for `field_name` = numeric_value
  1816. //if (preg_match('@^[\s]*`[^`]*` = [0-9]+@', $where_clause)) {
  1817. // in 2.9.0, we are looking for `table_name`.`field_name` = numeric_value
  1818. $is_numeric = false;
  1819. if (! is_array($where_clause)) {
  1820. $where_clause = [$where_clause];
  1821. }
  1822. for ($i = 0, $nb = count($where_clause); $i < $nb; $i++) {
  1823. // preg_match() returns 1 if there is a match
  1824. $is_numeric = (preg_match(
  1825. '@^[\s]*`[^`]*`[\.]`[^`]*` = [0-9]+@',
  1826. $where_clause[$i]
  1827. ) == 1);
  1828. if ($is_numeric === true) {
  1829. break;
  1830. }
  1831. }
  1832. if ($found_unique_key && $is_numeric) {
  1833. $html_output .= '<option value="edit_next" '
  1834. . ($after_insert === 'edit_next' ? 'selected="selected"' : '') . '>'
  1835. . __('Edit next row') . '</option>';
  1836. }
  1837. }
  1838. return $html_output . '</select>';
  1839. }
  1840. /**
  1841. * get Submit button and Reset button for action panel
  1842. *
  1843. * @param int $tabindex tab index
  1844. * @param int $tabindex_for_value offset for the values tabindex
  1845. *
  1846. * @return string an html snippet
  1847. */
  1848. private function getSubmitAndResetButtonForActionsPanel($tabindex, $tabindex_for_value)
  1849. {
  1850. return '<td>'
  1851. . Generator::showHint(
  1852. __(
  1853. 'Use TAB key to move from value to value,'
  1854. . ' or CTRL+arrows to move anywhere.'
  1855. )
  1856. )
  1857. . '</td>'
  1858. . '<td colspan="3" class="right vmiddle">'
  1859. . '<input type="button" class="btn btn-secondary preview_sql" value="' . __('Preview SQL') . '"'
  1860. . ' tabindex="' . ($tabindex + $tabindex_for_value + 6) . '">'
  1861. . '<input type="reset" class="btn btn-secondary control_at_footer" value="' . __('Reset') . '"'
  1862. . ' tabindex="' . ($tabindex + $tabindex_for_value + 7) . '">'
  1863. . '<input type="submit" class="btn btn-primary control_at_footer" value="' . __('Go') . '"'
  1864. . ' tabindex="' . ($tabindex + $tabindex_for_value + 8) . '" id="buttonYes">'
  1865. . '</td>';
  1866. }
  1867. /**
  1868. * Get table head and table foot for insert row table
  1869. *
  1870. * @param array $url_params url parameters
  1871. *
  1872. * @return string an html snippet
  1873. */
  1874. private function getHeadAndFootOfInsertRowTable(array $url_params)
  1875. {
  1876. $html_output = '<div class="responsivetable">'
  1877. . '<table class="pma-table insertRowTable topmargin">'
  1878. . '<thead>'
  1879. . '<tr>'
  1880. . '<th>' . __('Column') . '</th>';
  1881. if ($GLOBALS['cfg']['ShowFieldTypesInDataEditView']) {
  1882. $html_output .= $this->showTypeOrFunction('type', $url_params, true);
  1883. }
  1884. if ($GLOBALS['cfg']['ShowFunctionFields']) {
  1885. $html_output .= $this->showTypeOrFunction('function', $url_params, true);
  1886. }
  1887. $html_output .= '<th>' . __('Null') . '</th>'
  1888. . '<th class="fillPage">' . __('Value') . '</th>'
  1889. . '</tr>'
  1890. . '</thead>'
  1891. . ' <tfoot>'
  1892. . '<tr>'
  1893. . '<th colspan="5" class="tblFooters right">'
  1894. . '<input class="btn btn-primary" type="submit" value="' . __('Go') . '">'
  1895. . '</th>'
  1896. . '</tr>'
  1897. . '</tfoot>';
  1898. return $html_output;
  1899. }
  1900. /**
  1901. * Prepares the field value and retrieve special chars, backup field and data array
  1902. *
  1903. * @param array $current_row a row of the table
  1904. * @param array $column description of column in given table
  1905. * @param array $extracted_columnspec associative array containing type,
  1906. * spec_in_brackets and possibly
  1907. * enum_set_values (another array)
  1908. * @param bool $real_null_value whether column value null or not null
  1909. * @param array $gis_data_types list of GIS data types
  1910. * @param string $column_name_appendix string to append to column name in input
  1911. * @param bool $as_is use the data as is, used in repopulating
  1912. *
  1913. * @return array $real_null_value, $data, $special_chars, $backup_field,
  1914. * $special_chars_encoded
  1915. */
  1916. private function getSpecialCharsAndBackupFieldForExistingRow(
  1917. array $current_row,
  1918. array $column,
  1919. array $extracted_columnspec,
  1920. $real_null_value,
  1921. array $gis_data_types,
  1922. $column_name_appendix,
  1923. $as_is
  1924. ) {
  1925. $special_chars_encoded = '';
  1926. $data = null;
  1927. // (we are editing)
  1928. if (! isset($current_row[$column['Field']])) {
  1929. $real_null_value = true;
  1930. $current_row[$column['Field']] = '';
  1931. $special_chars = '';
  1932. $data = $current_row[$column['Field']];
  1933. } elseif ($column['True_Type'] === 'bit') {
  1934. $special_chars = $as_is
  1935. ? $current_row[$column['Field']]
  1936. : Util::printableBitValue(
  1937. (int) $current_row[$column['Field']],
  1938. (int) $extracted_columnspec['spec_in_brackets']
  1939. );
  1940. } elseif ((substr($column['True_Type'], 0, 9) === 'timestamp'
  1941. || $column['True_Type'] === 'datetime'
  1942. || $column['True_Type'] === 'time')
  1943. && (mb_strpos($current_row[$column['Field']], '.') !== false)
  1944. ) {
  1945. $current_row[$column['Field']] = $as_is
  1946. ? $current_row[$column['Field']]
  1947. : Util::addMicroseconds(
  1948. $current_row[$column['Field']]
  1949. );
  1950. $special_chars = htmlspecialchars($current_row[$column['Field']], ENT_COMPAT);
  1951. } elseif (in_array($column['True_Type'], $gis_data_types)) {
  1952. // Convert gis data to Well Know Text format
  1953. $current_row[$column['Field']] = $as_is
  1954. ? $current_row[$column['Field']]
  1955. : Util::asWKT(
  1956. $current_row[$column['Field']],
  1957. true
  1958. );
  1959. $special_chars = htmlspecialchars($current_row[$column['Field']], ENT_COMPAT);
  1960. } else {
  1961. // special binary "characters"
  1962. if ($column['is_binary']
  1963. || ($column['is_blob'] && $GLOBALS['cfg']['ProtectBinary'] !== 'all')
  1964. ) {
  1965. $current_row[$column['Field']] = $as_is
  1966. ? $current_row[$column['Field']]
  1967. : bin2hex(
  1968. $current_row[$column['Field']]
  1969. );
  1970. }
  1971. $special_chars = htmlspecialchars($current_row[$column['Field']], ENT_COMPAT);
  1972. //We need to duplicate the first \n or otherwise we will lose
  1973. //the first newline entered in a VARCHAR or TEXT column
  1974. $special_chars_encoded
  1975. = Util::duplicateFirstNewline($special_chars);
  1976. $data = $current_row[$column['Field']];
  1977. }
  1978. //when copying row, it is useful to empty auto-increment column
  1979. // to prevent duplicate key error
  1980. if (isset($_POST['default_action'])
  1981. && $_POST['default_action'] === 'insert'
  1982. ) {
  1983. if ($column['Key'] === 'PRI'
  1984. && mb_strpos($column['Extra'], 'auto_increment') !== false
  1985. ) {
  1986. $data = $special_chars_encoded = $special_chars = null;
  1987. }
  1988. }
  1989. // If a timestamp field value is not included in an update
  1990. // statement MySQL auto-update it to the current timestamp;
  1991. // however, things have changed since MySQL 4.1, so
  1992. // it's better to set a fields_prev in this situation
  1993. $backup_field = '<input type="hidden" name="fields_prev'
  1994. . $column_name_appendix . '" value="'
  1995. . htmlspecialchars($current_row[$column['Field']], ENT_COMPAT) . '">';
  1996. return [
  1997. $real_null_value,
  1998. $special_chars_encoded,
  1999. $special_chars,
  2000. $data,
  2001. $backup_field,
  2002. ];
  2003. }
  2004. /**
  2005. * display default values
  2006. *
  2007. * @param array $column description of column in given table
  2008. * @param bool $real_null_value whether column value null or not null
  2009. *
  2010. * @return array $real_null_value, $data, $special_chars,
  2011. * $backup_field, $special_chars_encoded
  2012. */
  2013. private function getSpecialCharsAndBackupFieldForInsertingMode(
  2014. array $column,
  2015. $real_null_value
  2016. ) {
  2017. if (! isset($column['Default'])) {
  2018. $column['Default'] = '';
  2019. $real_null_value = true;
  2020. $data = '';
  2021. } else {
  2022. $data = $column['Default'];
  2023. }
  2024. $trueType = $column['True_Type'];
  2025. if ($trueType === 'bit') {
  2026. $special_chars = Util::convertBitDefaultValue(
  2027. $column['Default']
  2028. );
  2029. } elseif (substr($trueType, 0, 9) === 'timestamp'
  2030. || $trueType === 'datetime'
  2031. || $trueType === 'time'
  2032. ) {
  2033. $special_chars = Util::addMicroseconds($column['Default']);
  2034. } elseif ($trueType === 'binary' || $trueType === 'varbinary') {
  2035. $special_chars = bin2hex($column['Default']);
  2036. } elseif (substr($trueType, -4) === 'text') {
  2037. $textDefault = substr($column['Default'], 1, -1);
  2038. $special_chars = stripcslashes($textDefault !== false ? $textDefault : $column['Default']);
  2039. } else {
  2040. $special_chars = htmlspecialchars($column['Default']);
  2041. }
  2042. $backup_field = '';
  2043. $special_chars_encoded = Util::duplicateFirstNewline(
  2044. $special_chars
  2045. );
  2046. return [
  2047. $real_null_value,
  2048. $data,
  2049. $special_chars,
  2050. $backup_field,
  2051. $special_chars_encoded,
  2052. ];
  2053. }
  2054. /**
  2055. * Prepares the update/insert of a row
  2056. *
  2057. * @return array $loop_array, $using_key, $is_insert, $is_insertignore
  2058. */
  2059. public function getParamsForUpdateOrInsert()
  2060. {
  2061. if (isset($_POST['where_clause'])) {
  2062. // we were editing something => use the WHERE clause
  2063. $loop_array = is_array($_POST['where_clause'])
  2064. ? $_POST['where_clause']
  2065. : [$_POST['where_clause']];
  2066. $using_key = true;
  2067. $is_insert = isset($_POST['submit_type'])
  2068. && ($_POST['submit_type'] === 'insert'
  2069. || $_POST['submit_type'] === 'showinsert'
  2070. || $_POST['submit_type'] === 'insertignore');
  2071. } else {
  2072. // new row => use indexes
  2073. $loop_array = [];
  2074. if (! empty($_POST['fields'])) {
  2075. foreach ($_POST['fields']['multi_edit'] as $key => $dummy) {
  2076. $loop_array[] = $key;
  2077. }
  2078. }
  2079. $using_key = false;
  2080. $is_insert = true;
  2081. }
  2082. $is_insertignore = isset($_POST['submit_type'])
  2083. && $_POST['submit_type'] === 'insertignore';
  2084. return [
  2085. $loop_array,
  2086. $using_key,
  2087. $is_insert,
  2088. $is_insertignore,
  2089. ];
  2090. }
  2091. /**
  2092. * Check wether insert row mode and if so include tbl_changen script and set
  2093. * global variables.
  2094. *
  2095. * @return void
  2096. */
  2097. public function isInsertRow()
  2098. {
  2099. global $containerBuilder;
  2100. if (! isset($_POST['insert_rows'])
  2101. || ! is_numeric($_POST['insert_rows'])
  2102. || $_POST['insert_rows'] == $GLOBALS['cfg']['InsertRows']
  2103. ) {
  2104. return;
  2105. }
  2106. $GLOBALS['cfg']['InsertRows'] = $_POST['insert_rows'];
  2107. $response = Response::getInstance();
  2108. $header = $response->getHeader();
  2109. $scripts = $header->getScripts();
  2110. $scripts->addFile('vendor/jquery/additional-methods.js');
  2111. $scripts->addFile('table/change.js');
  2112. if (! defined('TESTSUITE')) {
  2113. /** @var ChangeController $controller */
  2114. $controller = $containerBuilder->get(ChangeController::class);
  2115. $controller->index();
  2116. exit;
  2117. }
  2118. }
  2119. /**
  2120. * set $_SESSION for edit_next
  2121. *
  2122. * @param string $one_where_clause one where clause from where clauses array
  2123. *
  2124. * @return void
  2125. */
  2126. public function setSessionForEditNext($one_where_clause)
  2127. {
  2128. $local_query = 'SELECT * FROM ' . Util::backquote($GLOBALS['db'])
  2129. . '.' . Util::backquote($GLOBALS['table']) . ' WHERE '
  2130. . str_replace('` =', '` >', $one_where_clause) . ' LIMIT 1;';
  2131. $res = $this->dbi->query($local_query);
  2132. $row = $this->dbi->fetchRow($res);
  2133. $meta = $this->dbi->getFieldsMeta($res);
  2134. // must find a unique condition based on unique key,
  2135. // not a combination of all fields
  2136. [$unique_condition, $clause_is_unique] = Util::getUniqueCondition(
  2137. $res,
  2138. count($meta),
  2139. $meta,
  2140. $row ?? [],
  2141. true
  2142. );
  2143. if (! empty($unique_condition)) {
  2144. $_SESSION['edit_next'] = $unique_condition;
  2145. }
  2146. unset($unique_condition, $clause_is_unique);
  2147. }
  2148. /**
  2149. * set $goto_include variable for different cases and retrieve like,
  2150. * if $GLOBALS['goto'] empty, if $goto_include previously not defined
  2151. * and new_insert, same_insert, edit_next
  2152. *
  2153. * @param string|false $goto_include store some script for include, otherwise it is
  2154. * boolean false
  2155. *
  2156. * @return string|false
  2157. */
  2158. public function getGotoInclude($goto_include)
  2159. {
  2160. $valid_options = [
  2161. 'new_insert',
  2162. 'same_insert',
  2163. 'edit_next',
  2164. ];
  2165. if (isset($_POST['after_insert'])
  2166. && in_array($_POST['after_insert'], $valid_options)
  2167. ) {
  2168. $goto_include = '/table/change';
  2169. } elseif (! empty($GLOBALS['goto'])) {
  2170. if (! preg_match('@^[a-z_]+\.php$@', $GLOBALS['goto'])) {
  2171. // this should NOT happen
  2172. //$GLOBALS['goto'] = false;
  2173. if ($GLOBALS['goto'] === 'index.php?route=/sql') {
  2174. $goto_include = '/sql';
  2175. } else {
  2176. $goto_include = false;
  2177. }
  2178. } else {
  2179. $goto_include = $GLOBALS['goto'];
  2180. }
  2181. if ($GLOBALS['goto'] === 'index.php?route=/database/sql' && strlen($GLOBALS['table']) > 0) {
  2182. $GLOBALS['table'] = '';
  2183. }
  2184. }
  2185. if (! $goto_include) {
  2186. if (strlen($GLOBALS['table']) === 0) {
  2187. $goto_include = '/database/sql';
  2188. } else {
  2189. $goto_include = '/table/sql';
  2190. }
  2191. }
  2192. return $goto_include;
  2193. }
  2194. /**
  2195. * Defines the url to return in case of failure of the query
  2196. *
  2197. * @param array $url_params url parameters
  2198. *
  2199. * @return string error url for query failure
  2200. */
  2201. public function getErrorUrl(array $url_params)
  2202. {
  2203. if (isset($_POST['err_url'])) {
  2204. return $_POST['err_url'];
  2205. }
  2206. return Url::getFromRoute('/table/change', $url_params);
  2207. }
  2208. /**
  2209. * Builds the sql query
  2210. *
  2211. * @param bool $is_insertignore $_POST['submit_type'] === 'insertignore'
  2212. * @param array $query_fields column names array
  2213. * @param array $value_sets array of query values
  2214. *
  2215. * @return array of query
  2216. */
  2217. public function buildSqlQuery($is_insertignore, array $query_fields, array $value_sets)
  2218. {
  2219. if ($is_insertignore) {
  2220. $insert_command = 'INSERT IGNORE ';
  2221. } else {
  2222. $insert_command = 'INSERT ';
  2223. }
  2224. return [
  2225. $insert_command . 'INTO '
  2226. . Util::backquote($GLOBALS['table'])
  2227. . ' (' . implode(', ', $query_fields) . ') VALUES ('
  2228. . implode('), (', $value_sets) . ')',
  2229. ];
  2230. }
  2231. /**
  2232. * Executes the sql query and get the result, then move back to the calling page
  2233. *
  2234. * @param array $url_params url parameters array
  2235. * @param array $query built query from buildSqlQuery()
  2236. *
  2237. * @return array $url_params, $total_affected_rows, $last_messages
  2238. * $warning_messages, $error_messages, $return_to_sql_query
  2239. */
  2240. public function executeSqlQuery(array $url_params, array $query)
  2241. {
  2242. $return_to_sql_query = '';
  2243. if (! empty($GLOBALS['sql_query'])) {
  2244. $url_params['sql_query'] = $GLOBALS['sql_query'];
  2245. $return_to_sql_query = $GLOBALS['sql_query'];
  2246. }
  2247. $GLOBALS['sql_query'] = implode('; ', $query) . ';';
  2248. // to ensure that the query is displayed in case of
  2249. // "insert as new row" and then "insert another new row"
  2250. $GLOBALS['display_query'] = $GLOBALS['sql_query'];
  2251. $total_affected_rows = 0;
  2252. $last_messages = [];
  2253. $warning_messages = [];
  2254. $error_messages = [];
  2255. foreach ($query as $single_query) {
  2256. if (isset($_POST['submit_type']) && $_POST['submit_type'] === 'showinsert') {
  2257. $last_messages[] = Message::notice(__('Showing SQL query'));
  2258. continue;
  2259. }
  2260. if ($GLOBALS['cfg']['IgnoreMultiSubmitErrors']) {
  2261. $result = $this->dbi->tryQuery($single_query);
  2262. } else {
  2263. $result = $this->dbi->query($single_query);
  2264. }
  2265. if (! $result) {
  2266. $error_messages[] = $this->dbi->getError();
  2267. } else {
  2268. $tmp = @$this->dbi->affectedRows();
  2269. if ($tmp) {
  2270. $total_affected_rows += $tmp;
  2271. }
  2272. unset($tmp);
  2273. $insert_id = $this->dbi->insertId();
  2274. if ($insert_id != 0) {
  2275. // insert_id is id of FIRST record inserted in one insert, so if we
  2276. // inserted multiple rows, we had to increment this
  2277. if ($total_affected_rows > 0) {
  2278. $insert_id += $total_affected_rows - 1;
  2279. }
  2280. $last_message = Message::notice(__('Inserted row id: %1$d'));
  2281. $last_message->addParam($insert_id);
  2282. $last_messages[] = $last_message;
  2283. }
  2284. $this->dbi->freeResult($result);
  2285. }
  2286. $warning_messages = $this->getWarningMessages();
  2287. }
  2288. return [
  2289. $url_params,
  2290. $total_affected_rows,
  2291. $last_messages,
  2292. $warning_messages,
  2293. $error_messages,
  2294. $return_to_sql_query,
  2295. ];
  2296. }
  2297. /**
  2298. * get the warning messages array
  2299. *
  2300. * @return array
  2301. */
  2302. private function getWarningMessages()
  2303. {
  2304. $warning_essages = [];
  2305. foreach ($this->dbi->getWarnings() as $warning) {
  2306. $warning_essages[] = Message::sanitize(
  2307. $warning['Level'] . ': #' . $warning['Code'] . ' ' . $warning['Message']
  2308. );
  2309. }
  2310. return $warning_essages;
  2311. }
  2312. /**
  2313. * Column to display from the foreign table?
  2314. *
  2315. * @param string $where_comparison string that contain relation field value
  2316. * @param array $map all Relations to foreign tables for a given
  2317. * table or optionally a given column in a table
  2318. * @param string $relation_field relation field
  2319. *
  2320. * @return string display value from the foreign table
  2321. */
  2322. public function getDisplayValueForForeignTableColumn(
  2323. $where_comparison,
  2324. array $map,
  2325. $relation_field
  2326. ) {
  2327. $foreigner = $this->relation->searchColumnInForeigners($map, $relation_field);
  2328. if (! is_array($foreigner)) {
  2329. return '';
  2330. }
  2331. $display_field = $this->relation->getDisplayField(
  2332. $foreigner['foreign_db'],
  2333. $foreigner['foreign_table']
  2334. );
  2335. // Field to display from the foreign table?
  2336. if (is_string($display_field) && strlen($display_field) > 0) {
  2337. $dispsql = 'SELECT ' . Util::backquote($display_field)
  2338. . ' FROM ' . Util::backquote($foreigner['foreign_db'])
  2339. . '.' . Util::backquote($foreigner['foreign_table'])
  2340. . ' WHERE ' . Util::backquote($foreigner['foreign_field'])
  2341. . $where_comparison;
  2342. $dispresult = $this->dbi->tryQuery(
  2343. $dispsql,
  2344. DatabaseInterface::CONNECT_USER,
  2345. DatabaseInterface::QUERY_STORE
  2346. );
  2347. if ($dispresult && $this->dbi->numRows($dispresult) > 0) {
  2348. [$dispval] = $this->dbi->fetchRow($dispresult);
  2349. } else {
  2350. $dispval = '';
  2351. }
  2352. if ($dispresult) {
  2353. $this->dbi->freeResult($dispresult);
  2354. }
  2355. return $dispval;
  2356. }
  2357. return '';
  2358. }
  2359. /**
  2360. * Display option in the cell according to user choices
  2361. *
  2362. * @param array $map all Relations to foreign tables for a given
  2363. * table or optionally a given column in a table
  2364. * @param string $relation_field relation field
  2365. * @param string $where_comparison string that contain relation field value
  2366. * @param string $dispval display value from the foreign table
  2367. * @param string $relation_field_value relation field value
  2368. *
  2369. * @return string HTML <a> tag
  2370. */
  2371. public function getLinkForRelationalDisplayField(
  2372. array $map,
  2373. $relation_field,
  2374. $where_comparison,
  2375. $dispval,
  2376. $relation_field_value
  2377. ) {
  2378. $foreigner = $this->relation->searchColumnInForeigners($map, $relation_field);
  2379. if (! is_array($foreigner)) {
  2380. return '';
  2381. }
  2382. if ($_SESSION['tmpval']['relational_display'] === 'K') {
  2383. // user chose "relational key" in the display options, so
  2384. // the title contains the display field
  2385. $title = ! empty($dispval)
  2386. ? ' title="' . htmlspecialchars($dispval) . '"'
  2387. : '';
  2388. } else {
  2389. $title = ' title="' . htmlspecialchars($relation_field_value) . '"';
  2390. }
  2391. $sqlQuery = 'SELECT * FROM '
  2392. . Util::backquote($foreigner['foreign_db'])
  2393. . '.' . Util::backquote($foreigner['foreign_table'])
  2394. . ' WHERE ' . Util::backquote($foreigner['foreign_field'])
  2395. . $where_comparison;
  2396. $_url_params = [
  2397. 'db' => $foreigner['foreign_db'],
  2398. 'table' => $foreigner['foreign_table'],
  2399. 'pos' => '0',
  2400. 'sql_signature' => Core::signSqlQuery($sqlQuery),
  2401. 'sql_query' => $sqlQuery,
  2402. ];
  2403. $output = '<a href="' . Url::getFromRoute('/sql', $_url_params) . '"' . $title . '>';
  2404. if ($_SESSION['tmpval']['relational_display'] === 'D') {
  2405. // user chose "relational display field" in the
  2406. // display options, so show display field in the cell
  2407. $output .= ! empty($dispval) ? htmlspecialchars($dispval) : '';
  2408. } else {
  2409. // otherwise display data in the cell
  2410. $output .= htmlspecialchars($relation_field_value);
  2411. }
  2412. $output .= '</a>';
  2413. return $output;
  2414. }
  2415. /**
  2416. * Transform edited values
  2417. *
  2418. * @param string $db db name
  2419. * @param string $table table name
  2420. * @param array $transformation mimetypes for all columns of a table
  2421. * [field_name][field_key]
  2422. * @param array $edited_values transform columns list and new values
  2423. * @param string $file file containing the transformation plugin
  2424. * @param string $column_name column name
  2425. * @param array $extra_data extra data array
  2426. * @param string $type the type of transformation
  2427. *
  2428. * @return array
  2429. */
  2430. public function transformEditedValues(
  2431. $db,
  2432. $table,
  2433. array $transformation,
  2434. array &$edited_values,
  2435. $file,
  2436. $column_name,
  2437. array $extra_data,
  2438. $type
  2439. ) {
  2440. $include_file = 'libraries/classes/Plugins/Transformations/' . $file;
  2441. if (is_file($include_file)) {
  2442. // $cfg['SaveCellsAtOnce'] = true; JS code sends an array
  2443. $whereClause = is_array($_POST['where_clause']) ? $_POST['where_clause'][0] : $_POST['where_clause'];
  2444. $_url_params = [
  2445. 'db' => $db,
  2446. 'table' => $table,
  2447. 'where_clause_sign' => Core::signSqlQuery($whereClause),
  2448. 'where_clause' => $whereClause,
  2449. 'transform_key' => $column_name,
  2450. ];
  2451. $transform_options = $this->transformations->getOptions(
  2452. $transformation[$type . '_options'] ?? ''
  2453. );
  2454. $transform_options['wrapper_link'] = Url::getCommon($_url_params);
  2455. $transform_options['wrapper_params'] = $_url_params;
  2456. $class_name = $this->transformations->getClassName($include_file);
  2457. if (class_exists($class_name)) {
  2458. /** @var TransformationsPlugin $transformation_plugin */
  2459. $transformation_plugin = new $class_name();
  2460. foreach ($edited_values as $cell_index => $curr_cell_edited_values) {
  2461. if (! isset($curr_cell_edited_values[$column_name])) {
  2462. continue;
  2463. }
  2464. $edited_values[$cell_index][$column_name]
  2465. = $extra_data['transformations'][$cell_index]
  2466. = $transformation_plugin->applyTransformation(
  2467. $curr_cell_edited_values[$column_name],
  2468. $transform_options
  2469. );
  2470. }
  2471. }
  2472. }
  2473. return $extra_data;
  2474. }
  2475. /**
  2476. * Get current value in multi edit mode
  2477. *
  2478. * @param array $multi_edit_funcs multiple edit functions array
  2479. * @param array $multi_edit_salt multiple edit array with encryption salt
  2480. * @param array $gis_from_text_functions array that contains gis from text functions
  2481. * @param string $current_value current value in the column
  2482. * @param array $gis_from_wkb_functions initially $val is $multi_edit_columns[$key]
  2483. * @param array $func_optional_param array('RAND','UNIX_TIMESTAMP')
  2484. * @param array $func_no_param array of set of string
  2485. * @param string $key an md5 of the column name
  2486. *
  2487. * @return string
  2488. */
  2489. public function getCurrentValueAsAnArrayForMultipleEdit(
  2490. $multi_edit_funcs,
  2491. $multi_edit_salt,
  2492. $gis_from_text_functions,
  2493. $current_value,
  2494. $gis_from_wkb_functions,
  2495. $func_optional_param,
  2496. $func_no_param,
  2497. $key
  2498. ) {
  2499. if (empty($multi_edit_funcs[$key])) {
  2500. return $current_value;
  2501. }
  2502. if ($multi_edit_funcs[$key] === 'PHP_PASSWORD_HASH') {
  2503. /**
  2504. * @see https://github.com/vimeo/psalm/issues/3350
  2505. *
  2506. * @psalm-suppress InvalidArgument
  2507. */
  2508. $hash = password_hash($current_value, PASSWORD_DEFAULT);
  2509. return "'" . $hash . "'";
  2510. }
  2511. if ($multi_edit_funcs[$key] === 'UUID') {
  2512. /* This way user will know what UUID new row has */
  2513. $uuid = $this->dbi->fetchValue('SELECT UUID()');
  2514. return "'" . $uuid . "'";
  2515. }
  2516. if ((in_array($multi_edit_funcs[$key], $gis_from_text_functions)
  2517. && substr($current_value, 0, 3) == "'''")
  2518. || in_array($multi_edit_funcs[$key], $gis_from_wkb_functions)
  2519. ) {
  2520. // Remove enclosing apostrophes
  2521. $current_value = mb_substr($current_value, 1, -1);
  2522. // Remove escaping apostrophes
  2523. $current_value = str_replace("''", "'", $current_value);
  2524. // Remove backslash-escaped apostrophes
  2525. $current_value = str_replace("\'", "'", $current_value);
  2526. return $multi_edit_funcs[$key] . '(' . $current_value . ')';
  2527. }
  2528. if (! in_array($multi_edit_funcs[$key], $func_no_param)
  2529. || ($current_value != "''"
  2530. && in_array($multi_edit_funcs[$key], $func_optional_param))
  2531. ) {
  2532. if ((isset($multi_edit_salt[$key])
  2533. && ($multi_edit_funcs[$key] === 'AES_ENCRYPT'
  2534. || $multi_edit_funcs[$key] === 'AES_DECRYPT'))
  2535. || (! empty($multi_edit_salt[$key])
  2536. && ($multi_edit_funcs[$key] === 'DES_ENCRYPT'
  2537. || $multi_edit_funcs[$key] === 'DES_DECRYPT'
  2538. || $multi_edit_funcs[$key] === 'ENCRYPT'))
  2539. ) {
  2540. return $multi_edit_funcs[$key] . '(' . $current_value . ",'"
  2541. . $this->dbi->escapeString($multi_edit_salt[$key]) . "')";
  2542. }
  2543. return $multi_edit_funcs[$key] . '(' . $current_value . ')';
  2544. }
  2545. return $multi_edit_funcs[$key] . '()';
  2546. }
  2547. /**
  2548. * Get query values array and query fields array for insert and update in multi edit
  2549. *
  2550. * @param array $multi_edit_columns_name multiple edit columns name array
  2551. * @param array $multi_edit_columns_null multiple edit columns null array
  2552. * @param string $current_value current value in the column in loop
  2553. * @param array $multi_edit_columns_prev multiple edit previous columns array
  2554. * @param array $multi_edit_funcs multiple edit functions array
  2555. * @param bool $is_insert boolean value whether insert or not
  2556. * @param array $query_values SET part of the sql query
  2557. * @param array $query_fields array of query fields
  2558. * @param string $current_value_as_an_array current value in the column
  2559. * as an array
  2560. * @param array $value_sets array of valu sets
  2561. * @param string $key an md5 of the column name
  2562. * @param array $multi_edit_columns_null_prev array of multiple edit columns
  2563. * null previous
  2564. *
  2565. * @return array ($query_values, $query_fields)
  2566. */
  2567. public function getQueryValuesForInsertAndUpdateInMultipleEdit(
  2568. $multi_edit_columns_name,
  2569. $multi_edit_columns_null,
  2570. $current_value,
  2571. $multi_edit_columns_prev,
  2572. $multi_edit_funcs,
  2573. $is_insert,
  2574. $query_values,
  2575. $query_fields,
  2576. $current_value_as_an_array,
  2577. $value_sets,
  2578. $key,
  2579. $multi_edit_columns_null_prev
  2580. ) {
  2581. // i n s e r t
  2582. if ($is_insert) {
  2583. // no need to add column into the valuelist
  2584. if (strlen($current_value_as_an_array) > 0) {
  2585. $query_values[] = $current_value_as_an_array;
  2586. // first inserted row so prepare the list of fields
  2587. if (empty($value_sets)) {
  2588. $query_fields[] = Util::backquote(
  2589. $multi_edit_columns_name[$key]
  2590. );
  2591. }
  2592. }
  2593. } elseif (! empty($multi_edit_columns_null_prev[$key])
  2594. && ! isset($multi_edit_columns_null[$key])
  2595. ) {
  2596. // u p d a t e
  2597. // field had the null checkbox before the update
  2598. // field no longer has the null checkbox
  2599. $query_values[]
  2600. = Util::backquote($multi_edit_columns_name[$key])
  2601. . ' = ' . $current_value_as_an_array;
  2602. } elseif (! (empty($multi_edit_funcs[$key])
  2603. && isset($multi_edit_columns_prev[$key])
  2604. && (($current_value === "'" . $this->dbi->escapeString($multi_edit_columns_prev[$key]) . "'")
  2605. || ($current_value === '0x' . $multi_edit_columns_prev[$key])))
  2606. && ! empty($current_value)
  2607. ) {
  2608. // avoid setting a field to NULL when it's already NULL
  2609. // (field had the null checkbox before the update
  2610. // field still has the null checkbox)
  2611. if (empty($multi_edit_columns_null_prev[$key])
  2612. || empty($multi_edit_columns_null[$key])
  2613. ) {
  2614. $query_values[]
  2615. = Util::backquote($multi_edit_columns_name[$key])
  2616. . ' = ' . $current_value_as_an_array;
  2617. }
  2618. }
  2619. return [
  2620. $query_values,
  2621. $query_fields,
  2622. ];
  2623. }
  2624. /**
  2625. * Get the current column value in the form for different data types
  2626. *
  2627. * @param string|false $possibly_uploaded_val uploaded file content
  2628. * @param string $key an md5 of the column name
  2629. * @param array|null $multi_edit_columns_type array of multi edit column types
  2630. * @param string $current_value current column value in the form
  2631. * @param array|null $multi_edit_auto_increment multi edit auto increment
  2632. * @param int $rownumber index of where clause array
  2633. * @param array $multi_edit_columns_name multi edit column names array
  2634. * @param array $multi_edit_columns_null multi edit columns null array
  2635. * @param array $multi_edit_columns_null_prev multi edit columns previous null
  2636. * @param bool $is_insert whether insert or not
  2637. * @param bool $using_key whether editing or new row
  2638. * @param string $where_clause where clause
  2639. * @param string $table table name
  2640. * @param array $multi_edit_funcs multiple edit functions array
  2641. *
  2642. * @return string current column value in the form
  2643. */
  2644. public function getCurrentValueForDifferentTypes(
  2645. $possibly_uploaded_val,
  2646. $key,
  2647. ?array $multi_edit_columns_type,
  2648. $current_value,
  2649. ?array $multi_edit_auto_increment,
  2650. $rownumber,
  2651. $multi_edit_columns_name,
  2652. $multi_edit_columns_null,
  2653. $multi_edit_columns_null_prev,
  2654. $is_insert,
  2655. $using_key,
  2656. $where_clause,
  2657. $table,
  2658. $multi_edit_funcs
  2659. ) {
  2660. // Fetch the current values of a row to use in case we have a protected field
  2661. if ($is_insert
  2662. && $using_key && isset($multi_edit_columns_type)
  2663. && is_array($multi_edit_columns_type) && ! empty($where_clause)
  2664. ) {
  2665. $protected_row = $this->dbi->fetchSingleRow(
  2666. 'SELECT * FROM ' . Util::backquote($table)
  2667. . ' WHERE ' . $where_clause . ';'
  2668. );
  2669. }
  2670. if ($possibly_uploaded_val !== false) {
  2671. $current_value = $possibly_uploaded_val;
  2672. } elseif (! empty($multi_edit_funcs[$key])) {
  2673. $current_value = "'" . $this->dbi->escapeString($current_value)
  2674. . "'";
  2675. } else {
  2676. // c o l u m n v a l u e i n t h e f o r m
  2677. if (isset($multi_edit_columns_type[$key])) {
  2678. $type = $multi_edit_columns_type[$key];
  2679. } else {
  2680. $type = '';
  2681. }
  2682. if ($type !== 'protected' && $type !== 'set' && strlen($current_value) === 0) {
  2683. // best way to avoid problems in strict mode
  2684. // (works also in non-strict mode)
  2685. if (isset($multi_edit_auto_increment, $multi_edit_auto_increment[$key])) {
  2686. $current_value = 'NULL';
  2687. } else {
  2688. $current_value = "''";
  2689. }
  2690. } elseif ($type === 'set') {
  2691. if (! empty($_POST['fields']['multi_edit'][$rownumber][$key])) {
  2692. $current_value = implode(
  2693. ',',
  2694. $_POST['fields']['multi_edit'][$rownumber][$key]
  2695. );
  2696. $current_value = "'"
  2697. . $this->dbi->escapeString($current_value) . "'";
  2698. } else {
  2699. $current_value = "''";
  2700. }
  2701. } elseif ($type === 'protected') {
  2702. // here we are in protected mode (asked in the config)
  2703. // so tbl_change has put this special value in the
  2704. // columns array, so we do not change the column value
  2705. // but we can still handle column upload
  2706. // when in UPDATE mode, do not alter field's contents. When in INSERT
  2707. // mode, insert empty field because no values were submitted.
  2708. // If protected blobs where set, insert original fields content.
  2709. if (! empty($protected_row[$multi_edit_columns_name[$key]])) {
  2710. $current_value = '0x'
  2711. . bin2hex($protected_row[$multi_edit_columns_name[$key]]);
  2712. } else {
  2713. $current_value = '';
  2714. }
  2715. } elseif ($type === 'hex') {
  2716. if (substr($current_value, 0, 2) != '0x') {
  2717. $current_value = '0x' . $current_value;
  2718. }
  2719. } elseif ($type === 'bit') {
  2720. $current_value = preg_replace('/[^01]/', '0', $current_value);
  2721. $current_value = "b'" . $this->dbi->escapeString($current_value)
  2722. . "'";
  2723. } elseif (! ($type === 'datetime' || $type === 'timestamp' || $type === 'date')
  2724. || ($current_value !== 'CURRENT_TIMESTAMP'
  2725. && $current_value !== 'current_timestamp()')
  2726. ) {
  2727. $current_value = "'" . $this->dbi->escapeString($current_value)
  2728. . "'";
  2729. }
  2730. // Was the Null checkbox checked for this field?
  2731. // (if there is a value, we ignore the Null checkbox: this could
  2732. // be possible if Javascript is disabled in the browser)
  2733. if (! empty($multi_edit_columns_null[$key])
  2734. && ($current_value == "''" || $current_value == '')
  2735. ) {
  2736. $current_value = 'NULL';
  2737. }
  2738. // The Null checkbox was unchecked for this field
  2739. if (empty($current_value)
  2740. && ! empty($multi_edit_columns_null_prev[$key])
  2741. && ! isset($multi_edit_columns_null[$key])
  2742. ) {
  2743. $current_value = "''";
  2744. }
  2745. }
  2746. return $current_value;
  2747. }
  2748. /**
  2749. * Check whether inline edited value can be truncated or not,
  2750. * and add additional parameters for extra_data array if needed
  2751. *
  2752. * @param string $db Database name
  2753. * @param string $table Table name
  2754. * @param string $column_name Column name
  2755. * @param array $extra_data Extra data for ajax response
  2756. *
  2757. * @return void
  2758. */
  2759. public function verifyWhetherValueCanBeTruncatedAndAppendExtraData(
  2760. $db,
  2761. $table,
  2762. $column_name,
  2763. array &$extra_data
  2764. ) {
  2765. $extra_data['isNeedToRecheck'] = false;
  2766. $sql_for_real_value = 'SELECT ' . Util::backquote($table) . '.'
  2767. . Util::backquote($column_name)
  2768. . ' FROM ' . Util::backquote($db) . '.'
  2769. . Util::backquote($table)
  2770. . ' WHERE ' . $_POST['where_clause'][0];
  2771. $result = $this->dbi->tryQuery($sql_for_real_value);
  2772. $fields_meta = $this->dbi->getFieldsMeta($result);
  2773. $meta = $fields_meta[0];
  2774. $row = $this->dbi->fetchRow($result);
  2775. if ($row) {
  2776. $new_value = $row[0];
  2777. if ((substr($meta->type, 0, 9) === 'timestamp')
  2778. || ($meta->type === 'datetime')
  2779. || ($meta->type === 'time')
  2780. ) {
  2781. $new_value = Util::addMicroseconds($new_value);
  2782. } elseif (mb_strpos($meta->flags, 'binary') !== false) {
  2783. $new_value = '0x' . bin2hex($new_value);
  2784. }
  2785. $extra_data['isNeedToRecheck'] = true;
  2786. $extra_data['truncatableFieldValue'] = $new_value;
  2787. }
  2788. $this->dbi->freeResult($result);
  2789. }
  2790. /**
  2791. * Function to get the columns of a table
  2792. *
  2793. * @param string $db current db
  2794. * @param string $table current table
  2795. *
  2796. * @return array
  2797. */
  2798. public function getTableColumns($db, $table)
  2799. {
  2800. $this->dbi->selectDb($db);
  2801. return array_values($this->dbi->getColumns($db, $table, null, true));
  2802. }
  2803. /**
  2804. * Function to determine Insert/Edit rows
  2805. *
  2806. * @param string $where_clause where clause
  2807. * @param string $db current database
  2808. * @param string $table current table
  2809. *
  2810. * @return array
  2811. */
  2812. public function determineInsertOrEdit($where_clause, $db, $table): array
  2813. {
  2814. if (isset($_POST['where_clause'])) {
  2815. $where_clause = $_POST['where_clause'];
  2816. }
  2817. if (isset($_SESSION['edit_next'])) {
  2818. $where_clause = $_SESSION['edit_next'];
  2819. unset($_SESSION['edit_next']);
  2820. $after_insert = 'edit_next';
  2821. }
  2822. if (isset($_POST['ShowFunctionFields'])) {
  2823. $GLOBALS['cfg']['ShowFunctionFields'] = $_POST['ShowFunctionFields'];
  2824. }
  2825. if (isset($_POST['ShowFieldTypesInDataEditView'])) {
  2826. $GLOBALS['cfg']['ShowFieldTypesInDataEditView']
  2827. = $_POST['ShowFieldTypesInDataEditView'];
  2828. }
  2829. if (isset($_POST['after_insert'])) {
  2830. $after_insert = $_POST['after_insert'];
  2831. }
  2832. if (isset($where_clause)) {
  2833. // we are editing
  2834. $insert_mode = false;
  2835. $where_clause_array = $this->getWhereClauseArray($where_clause);
  2836. [$where_clauses, $result, $rows, $found_unique_key]
  2837. = $this->analyzeWhereClauses(
  2838. $where_clause_array,
  2839. $table,
  2840. $db
  2841. );
  2842. } else {
  2843. // we are inserting
  2844. $insert_mode = true;
  2845. $where_clause = null;
  2846. [$result, $rows] = $this->loadFirstRow($table, $db);
  2847. $where_clauses = null;
  2848. $where_clause_array = [];
  2849. $found_unique_key = false;
  2850. }
  2851. // Copying a row - fetched data will be inserted as a new row,
  2852. // therefore the where clause is needless.
  2853. if (isset($_POST['default_action'])
  2854. && $_POST['default_action'] === 'insert'
  2855. ) {
  2856. $where_clause = $where_clauses = null;
  2857. }
  2858. return [
  2859. $insert_mode,
  2860. $where_clause,
  2861. $where_clause_array,
  2862. $where_clauses,
  2863. $result,
  2864. $rows,
  2865. $found_unique_key,
  2866. $after_insert ?? null,
  2867. ];
  2868. }
  2869. /**
  2870. * Function to get comments for the table columns
  2871. *
  2872. * @param string $db current database
  2873. * @param string $table current table
  2874. *
  2875. * @return array comments for columns
  2876. */
  2877. public function getCommentsMap($db, $table)
  2878. {
  2879. $comments_map = [];
  2880. if ($GLOBALS['cfg']['ShowPropertyComments']) {
  2881. $comments_map = $this->relation->getComments($db, $table);
  2882. }
  2883. return $comments_map;
  2884. }
  2885. /**
  2886. * Function to get URL parameters
  2887. *
  2888. * @param string $db current database
  2889. * @param string $table current table
  2890. *
  2891. * @return array url parameters
  2892. */
  2893. public function getUrlParameters($db, $table)
  2894. {
  2895. global $goto;
  2896. /**
  2897. * @todo check if we could replace by "db_|tbl_" - please clarify!?
  2898. */
  2899. $url_params = [
  2900. 'db' => $db,
  2901. 'sql_query' => $_POST['sql_query'],
  2902. ];
  2903. if (strpos($goto, 'tbl_') === 0 || strpos($goto, 'index.php?route=/table') === 0) {
  2904. $url_params['table'] = $table;
  2905. }
  2906. return $url_params;
  2907. }
  2908. /**
  2909. * Function to get html for the gis editor div
  2910. *
  2911. * @return string
  2912. */
  2913. public function getHtmlForGisEditor()
  2914. {
  2915. return '<div id="gis_editor"></div>'
  2916. . '<div id="popup_background"></div>'
  2917. . '<br>';
  2918. }
  2919. /**
  2920. * Function to get html for the ignore option in insert mode
  2921. *
  2922. * @param int $row_id row id
  2923. * @param bool $checked ignore option is checked or not
  2924. *
  2925. * @return string
  2926. */
  2927. public function getHtmlForIgnoreOption($row_id, $checked = true)
  2928. {
  2929. return '<input type="checkbox"'
  2930. . ($checked ? ' checked="checked"' : '')
  2931. . ' name="insert_ignore_' . $row_id . '"'
  2932. . ' id="insert_ignore_' . $row_id . '">'
  2933. . '<label for="insert_ignore_' . $row_id . '">'
  2934. . __('Ignore')
  2935. . '</label><br>' . "\n";
  2936. }
  2937. /**
  2938. * Function to get html for the function option
  2939. *
  2940. * @param array $column column
  2941. * @param string $column_name_appendix column name appendix
  2942. */
  2943. private function getHtmlForFunctionOption(array $column, $column_name_appendix): string
  2944. {
  2945. return '<tr class="noclick">'
  2946. . '<td '
  2947. . 'class="text-center">'
  2948. . $column['Field_title']
  2949. . '<input type="hidden" name="fields_name' . $column_name_appendix
  2950. . '" value="' . $column['Field_html'] . '">'
  2951. . '</td>';
  2952. }
  2953. /**
  2954. * Function to get html for the column type
  2955. *
  2956. * @param array $column column
  2957. *
  2958. * @return string
  2959. */
  2960. private function getHtmlForInsertEditColumnType(array $column)
  2961. {
  2962. return '<td class="text-center' . $column['wrap'] . '">'
  2963. . '<span class="column_type" dir="ltr">' . $column['pma_type'] . '</span>'
  2964. . '</td>';
  2965. }
  2966. /**
  2967. * Function to get html for the insert edit form header
  2968. *
  2969. * @param bool $has_blob_field whether has blob field
  2970. * @param bool $is_upload whether is upload
  2971. *
  2972. * @return string
  2973. */
  2974. public function getHtmlForInsertEditFormHeader($has_blob_field, $is_upload)
  2975. {
  2976. $html_output = '<form id="insertForm" class="lock-page ';
  2977. if ($has_blob_field && $is_upload) {
  2978. $html_output .= 'disableAjax';
  2979. }
  2980. $html_output .= '" method="post" action="' . Url::getFromRoute('/table/replace') . '" name="insertForm" ';
  2981. if ($is_upload) {
  2982. $html_output .= ' enctype="multipart/form-data"';
  2983. }
  2984. $html_output .= '>';
  2985. return $html_output;
  2986. }
  2987. /**
  2988. * Function to get html for each insert/edit column
  2989. *
  2990. * @param array $table_columns table columns
  2991. * @param int $column_number column index in table_columns
  2992. * @param array $comments_map comments map
  2993. * @param bool $timestamp_seen whether timestamp seen
  2994. * @param array $current_result current result
  2995. * @param string $chg_evt_handler javascript change event handler
  2996. * @param string $jsvkey javascript validation key
  2997. * @param string $vkey validation key
  2998. * @param bool $insert_mode whether insert mode
  2999. * @param array $current_row current row
  3000. * @param int $o_rows row offset
  3001. * @param int $tabindex tab index
  3002. * @param int $columns_cnt columns count
  3003. * @param bool $is_upload whether upload
  3004. * @param int $tabindex_for_function tab index offset for function
  3005. * @param array $foreigners foreigners
  3006. * @param int $tabindex_for_null tab index offset for null
  3007. * @param int $tabindex_for_value tab index offset for value
  3008. * @param string $table table
  3009. * @param string $db database
  3010. * @param int $row_id row id
  3011. * @param int $biggest_max_file_size biggest max file size
  3012. * @param string $default_char_editing default char editing mode which is stored
  3013. * in the config.inc.php script
  3014. * @param string $text_dir text direction
  3015. * @param array $repopulate the data to be repopulated
  3016. * @param array $column_mime the mime information of column
  3017. * @param string $where_clause the where clause
  3018. *
  3019. * @return string
  3020. */
  3021. private function getHtmlForInsertEditFormColumn(
  3022. array $table_columns,
  3023. $column_number,
  3024. array $comments_map,
  3025. $timestamp_seen,
  3026. $current_result,
  3027. $chg_evt_handler,
  3028. $jsvkey,
  3029. $vkey,
  3030. $insert_mode,
  3031. array $current_row,
  3032. &$o_rows,
  3033. &$tabindex,
  3034. $columns_cnt,
  3035. $is_upload,
  3036. $tabindex_for_function,
  3037. array $foreigners,
  3038. $tabindex_for_null,
  3039. $tabindex_for_value,
  3040. $table,
  3041. $db,
  3042. $row_id,
  3043. $biggest_max_file_size,
  3044. $default_char_editing,
  3045. $text_dir,
  3046. array $repopulate,
  3047. array $column_mime,
  3048. $where_clause
  3049. ) {
  3050. $column = $table_columns[$column_number];
  3051. $readOnly = false;
  3052. if (! isset($column['processed'])) {
  3053. $column = $this->analyzeTableColumnsArray(
  3054. $column,
  3055. $comments_map,
  3056. $timestamp_seen
  3057. );
  3058. }
  3059. $as_is = false;
  3060. if (! empty($repopulate) && ! empty($current_row)) {
  3061. $current_row[$column['Field']] = $repopulate[$column['Field_md5']];
  3062. $as_is = true;
  3063. }
  3064. $extracted_columnspec
  3065. = Util::extractColumnSpec($column['Type']);
  3066. if ($column['len'] === -1) {
  3067. $column['len'] = $this->dbi->fieldLen(
  3068. $current_result,
  3069. $column_number
  3070. );
  3071. // length is unknown for geometry fields,
  3072. // make enough space to edit very simple WKTs
  3073. if ($column['len'] === -1) {
  3074. $column['len'] = 30;
  3075. }
  3076. }
  3077. //Call validation when the form submitted...
  3078. $onChangeClause = $chg_evt_handler
  3079. . "=\"return verificationsAfterFieldChange('"
  3080. . Sanitize::escapeJsString($column['Field_md5']) . "', '"
  3081. . Sanitize::escapeJsString($jsvkey) . "','" . $column['pma_type'] . "')\"";
  3082. // Use an MD5 as an array index to avoid having special characters
  3083. // in the name attribute (see bug #1746964 )
  3084. $column_name_appendix = $vkey . '[' . $column['Field_md5'] . ']';
  3085. if ($column['Type'] === 'datetime' && ! isset($column['Default']) && $insert_mode) {
  3086. $column['Default'] = date('Y-m-d H:i:s', time());
  3087. }
  3088. $html_output = $this->getHtmlForFunctionOption(
  3089. $column,
  3090. $column_name_appendix
  3091. );
  3092. if ($GLOBALS['cfg']['ShowFieldTypesInDataEditView']) {
  3093. $html_output .= $this->getHtmlForInsertEditColumnType($column);
  3094. }
  3095. // Get a list of GIS data types.
  3096. $gis_data_types = Util::getGISDatatypes();
  3097. // Prepares the field value
  3098. $real_null_value = false;
  3099. $special_chars_encoded = '';
  3100. if (! empty($current_row)) {
  3101. // (we are editing)
  3102. [
  3103. $real_null_value,
  3104. $special_chars_encoded,
  3105. $special_chars,
  3106. $data,
  3107. $backup_field,
  3108. ]
  3109. = $this->getSpecialCharsAndBackupFieldForExistingRow(
  3110. $current_row,
  3111. $column,
  3112. $extracted_columnspec,
  3113. $real_null_value,
  3114. $gis_data_types,
  3115. $column_name_appendix,
  3116. $as_is
  3117. );
  3118. } else {
  3119. // (we are inserting)
  3120. // display default values
  3121. $tmp = $column;
  3122. if (isset($repopulate[$column['Field_md5']])) {
  3123. $tmp['Default'] = $repopulate[$column['Field_md5']];
  3124. }
  3125. [
  3126. $real_null_value,
  3127. $data,
  3128. $special_chars,
  3129. $backup_field,
  3130. $special_chars_encoded,
  3131. ]
  3132. = $this->getSpecialCharsAndBackupFieldForInsertingMode(
  3133. $tmp,
  3134. $real_null_value
  3135. );
  3136. unset($tmp);
  3137. }
  3138. $idindex = ($o_rows * $columns_cnt) + $column_number + 1;
  3139. $tabindex = $idindex;
  3140. // Get a list of data types that are not yet supported.
  3141. $no_support_types = Util::unsupportedDatatypes();
  3142. // The function column
  3143. // -------------------
  3144. $foreignData = $this->relation->getForeignData(
  3145. $foreigners,
  3146. $column['Field'],
  3147. false,
  3148. '',
  3149. ''
  3150. );
  3151. if ($GLOBALS['cfg']['ShowFunctionFields']) {
  3152. $html_output .= $this->getFunctionColumn(
  3153. $column,
  3154. $is_upload,
  3155. $column_name_appendix,
  3156. $onChangeClause,
  3157. $no_support_types,
  3158. $tabindex_for_function,
  3159. $tabindex,
  3160. $idindex,
  3161. $insert_mode,
  3162. $readOnly,
  3163. $foreignData
  3164. );
  3165. }
  3166. // The null column
  3167. // ---------------
  3168. $html_output .= $this->getNullColumn(
  3169. $column,
  3170. $column_name_appendix,
  3171. $real_null_value,
  3172. $tabindex,
  3173. $tabindex_for_null,
  3174. $idindex,
  3175. $vkey,
  3176. $foreigners,
  3177. $foreignData,
  3178. $readOnly
  3179. );
  3180. // The value column (depends on type)
  3181. // ----------------
  3182. // See bug #1667887 for the reason why we don't use the maxlength
  3183. // HTML attribute
  3184. //add data attributes "no of decimals" and "data type"
  3185. $no_decimals = 0;
  3186. $type = current(explode('(', $column['pma_type']));
  3187. if (preg_match('/\(([^()]+)\)/', $column['pma_type'], $match)) {
  3188. $match[0] = trim($match[0], '()');
  3189. $no_decimals = $match[0];
  3190. }
  3191. $html_output .= '<td data-type="' . $type . '" data-decimals="'
  3192. . $no_decimals . '">' . "\n";
  3193. // Will be used by js/table/change.js to set the default value
  3194. // for the "Continue insertion" feature
  3195. $html_output .= '<span class="default_value hide">'
  3196. . $special_chars . '</span>';
  3197. // Check input transformation of column
  3198. $transformed_html = '';
  3199. if (! empty($column_mime['input_transformation'])) {
  3200. $file = $column_mime['input_transformation'];
  3201. $include_file = 'libraries/classes/Plugins/Transformations/' . $file;
  3202. if (is_file($include_file)) {
  3203. $class_name = $this->transformations->getClassName($include_file);
  3204. if (class_exists($class_name)) {
  3205. $transformation_plugin = new $class_name();
  3206. $transformation_options = $this->transformations->getOptions(
  3207. $column_mime['input_transformation_options']
  3208. );
  3209. $_url_params = [
  3210. 'db' => $db,
  3211. 'table' => $table,
  3212. 'transform_key' => $column['Field'],
  3213. 'where_clause_sign' => Core::signSqlQuery($where_clause),
  3214. 'where_clause' => $where_clause,
  3215. ];
  3216. $transformation_options['wrapper_link'] = Url::getCommon($_url_params);
  3217. $transformation_options['wrapper_params'] = $_url_params;
  3218. $current_value = '';
  3219. if (isset($current_row[$column['Field']])) {
  3220. $current_value = $current_row[$column['Field']];
  3221. }
  3222. if (method_exists($transformation_plugin, 'getInputHtml')) {
  3223. $transformed_html = $transformation_plugin->getInputHtml(
  3224. $column,
  3225. $row_id,
  3226. $column_name_appendix,
  3227. $transformation_options,
  3228. $current_value,
  3229. $text_dir,
  3230. $tabindex,
  3231. $tabindex_for_value,
  3232. $idindex
  3233. );
  3234. }
  3235. if (method_exists($transformation_plugin, 'getScripts')) {
  3236. $GLOBALS['plugin_scripts'] = array_merge(
  3237. $GLOBALS['plugin_scripts'],
  3238. $transformation_plugin->getScripts()
  3239. );
  3240. }
  3241. }
  3242. }
  3243. }
  3244. if (! empty($transformed_html)) {
  3245. $html_output .= $transformed_html;
  3246. } else {
  3247. $html_output .= $this->getValueColumn(
  3248. $column,
  3249. $backup_field,
  3250. $column_name_appendix,
  3251. $onChangeClause,
  3252. $tabindex,
  3253. $tabindex_for_value,
  3254. $idindex,
  3255. $data,
  3256. $special_chars,
  3257. $foreignData,
  3258. [
  3259. $table,
  3260. $db,
  3261. ],
  3262. $row_id,
  3263. $text_dir,
  3264. $special_chars_encoded,
  3265. $vkey,
  3266. $is_upload,
  3267. $biggest_max_file_size,
  3268. $default_char_editing,
  3269. $no_support_types,
  3270. $gis_data_types,
  3271. $extracted_columnspec,
  3272. $readOnly
  3273. );
  3274. }
  3275. return $html_output;
  3276. }
  3277. /**
  3278. * Function to get html for each insert/edit row
  3279. *
  3280. * @param array $url_params url parameters
  3281. * @param array $table_columns table columns
  3282. * @param array $comments_map comments map
  3283. * @param bool $timestamp_seen whether timestamp seen
  3284. * @param array $current_result current result
  3285. * @param string $chg_evt_handler javascript change event handler
  3286. * @param string $jsvkey javascript validation key
  3287. * @param string $vkey validation key
  3288. * @param bool $insert_mode whether insert mode
  3289. * @param array $current_row current row
  3290. * @param int $o_rows row offset
  3291. * @param int $tabindex tab index
  3292. * @param int $columns_cnt columns count
  3293. * @param bool $is_upload whether upload
  3294. * @param int $tabindex_for_function tab index offset for function
  3295. * @param array $foreigners foreigners
  3296. * @param int $tabindex_for_null tab index offset for null
  3297. * @param int $tabindex_for_value tab index offset for value
  3298. * @param string $table table
  3299. * @param string $db database
  3300. * @param int $row_id row id
  3301. * @param int $biggest_max_file_size biggest max file size
  3302. * @param string $text_dir text direction
  3303. * @param array $repopulate the data to be repopulated
  3304. * @param array $where_clause_array the array of where clauses
  3305. *
  3306. * @return string
  3307. */
  3308. public function getHtmlForInsertEditRow(
  3309. array $url_params,
  3310. array $table_columns,
  3311. array $comments_map,
  3312. $timestamp_seen,
  3313. $current_result,
  3314. $chg_evt_handler,
  3315. $jsvkey,
  3316. $vkey,
  3317. $insert_mode,
  3318. array $current_row,
  3319. &$o_rows,
  3320. &$tabindex,
  3321. $columns_cnt,
  3322. $is_upload,
  3323. $tabindex_for_function,
  3324. array $foreigners,
  3325. $tabindex_for_null,
  3326. $tabindex_for_value,
  3327. $table,
  3328. $db,
  3329. $row_id,
  3330. $biggest_max_file_size,
  3331. $text_dir,
  3332. array $repopulate,
  3333. array $where_clause_array
  3334. ) {
  3335. $html_output = $this->getHeadAndFootOfInsertRowTable($url_params)
  3336. . '<tbody>';
  3337. //store the default value for CharEditing
  3338. $default_char_editing = $GLOBALS['cfg']['CharEditing'];
  3339. $mime_map = $this->transformations->getMime($db, $table);
  3340. $where_clause = '';
  3341. if (isset($where_clause_array[$row_id])) {
  3342. $where_clause = $where_clause_array[$row_id];
  3343. }
  3344. for ($column_number = 0; $column_number < $columns_cnt; $column_number++) {
  3345. $table_column = $table_columns[$column_number];
  3346. $column_mime = [];
  3347. if (isset($mime_map[$table_column['Field']])) {
  3348. $column_mime = $mime_map[$table_column['Field']];
  3349. }
  3350. $virtual = [
  3351. 'VIRTUAL',
  3352. 'PERSISTENT',
  3353. 'VIRTUAL GENERATED',
  3354. 'STORED GENERATED',
  3355. ];
  3356. if (in_array($table_column['Extra'], $virtual)) {
  3357. continue;
  3358. }
  3359. $html_output .= $this->getHtmlForInsertEditFormColumn(
  3360. $table_columns,
  3361. $column_number,
  3362. $comments_map,
  3363. $timestamp_seen,
  3364. $current_result,
  3365. $chg_evt_handler,
  3366. $jsvkey,
  3367. $vkey,
  3368. $insert_mode,
  3369. $current_row,
  3370. $o_rows,
  3371. $tabindex,
  3372. $columns_cnt,
  3373. $is_upload,
  3374. $tabindex_for_function,
  3375. $foreigners,
  3376. $tabindex_for_null,
  3377. $tabindex_for_value,
  3378. $table,
  3379. $db,
  3380. $row_id,
  3381. $biggest_max_file_size,
  3382. $default_char_editing,
  3383. $text_dir,
  3384. $repopulate,
  3385. $column_mime,
  3386. $where_clause
  3387. );
  3388. }
  3389. $o_rows++;
  3390. return $html_output . ' </tbody>'
  3391. . '</table></div><br>'
  3392. . '<div class="clearfloat"></div>';
  3393. }
  3394. }