InsertEdit.php 130 KB

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