InsertEdit.php 129 KB

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