InsertEdit.php 129 KB

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