InsertEdit.php 129 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471
  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($_REQUEST['clause_is_unique'])) {
  78. $_form_params['clause_is_unique'] = $_REQUEST['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'
  264. . Url::getCommon($this_url_params) . '">'
  265. . $this->showTypeOrFunctionLabel($which)
  266. . '</a>';
  267. }
  268. return '<th><a href="tbl_change.php'
  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'
  807. . Url::getCommon(
  808. array(
  809. 'db' => $db,
  810. 'table' => $table,
  811. 'field' => $column['Field'],
  812. 'rownumber' => $rownumber,
  813. 'data' => $data
  814. )
  815. ) . '">'
  816. . str_replace("'", "\'", $titles['Browse']) . '</a>';
  817. return $html_output;
  818. }
  819. /**
  820. * Get HTML to display foreign data
  821. *
  822. * @param string $backup_field hidden input field
  823. * @param string $column_name_appendix the name attribute
  824. * @param string $onChangeClause onchange clause for fields
  825. * @param integer $tabindex tab index
  826. * @param integer $tabindex_for_value offset for the values tabindex
  827. * @param integer $idindex id index
  828. * @param string $data data to edit
  829. * @param array $foreignData data about the foreign keys
  830. * @param boolean $readOnly is display read only or not
  831. *
  832. * @return string an html snippet
  833. */
  834. private function dispRowForeignData(
  835. $backup_field,
  836. $column_name_appendix,
  837. $onChangeClause,
  838. $tabindex,
  839. $tabindex_for_value,
  840. $idindex,
  841. $data,
  842. array $foreignData,
  843. $readOnly
  844. ) {
  845. $html_output = '';
  846. $html_output .= $backup_field . "\n";
  847. $html_output .= '<input type="hidden"'
  848. . ' name="fields_type' . $column_name_appendix . '"'
  849. . ' value="foreign" />';
  850. $html_output .= '<select name="fields' . $column_name_appendix . '"'
  851. . ' ' . $onChangeClause
  852. . ' class="textfield"'
  853. . ($readOnly ? ' disabled' : '')
  854. . ' tabindex="' . ($tabindex + $tabindex_for_value) . '"'
  855. . ' id="field_' . $idindex . '_3">';
  856. $html_output .= $this->relation->foreignDropdown(
  857. $foreignData['disp_row'],
  858. $foreignData['foreign_field'],
  859. $foreignData['foreign_display'],
  860. $data,
  861. $GLOBALS['cfg']['ForeignKeyMaxLimit']
  862. );
  863. $html_output .= '</select>';
  864. //Add hidden input, as disabled <select> input does not included in POST.
  865. if ($readOnly) {
  866. $html_output .= '<input name="fields' . $column_name_appendix . '"'
  867. . ' type="hidden" value="' . htmlspecialchars($data) . '">';
  868. }
  869. return $html_output;
  870. }
  871. /**
  872. * Get HTML textarea for insert form
  873. *
  874. * @param array $column column information
  875. * @param string $backup_field hidden input field
  876. * @param string $column_name_appendix the name attribute
  877. * @param string $onChangeClause onchange clause for fields
  878. * @param integer $tabindex tab index
  879. * @param integer $tabindex_for_value offset for the values tabindex
  880. * @param integer $idindex id index
  881. * @param string $text_dir text direction
  882. * @param string $special_chars_encoded replaced char if the string starts
  883. * with a \r\n pair (0x0d0a) add an extra \n
  884. * @param string $data_type the html5 data-* attribute type
  885. * @param boolean $readOnly is column read only or not
  886. *
  887. * @return string an html snippet
  888. */
  889. private function getTextarea(
  890. array $column,
  891. $backup_field,
  892. $column_name_appendix,
  893. $onChangeClause,
  894. $tabindex,
  895. $tabindex_for_value,
  896. $idindex,
  897. $text_dir,
  898. $special_chars_encoded,
  899. $data_type,
  900. $readOnly
  901. ) {
  902. $the_class = '';
  903. $textAreaRows = $GLOBALS['cfg']['TextareaRows'];
  904. $textareaCols = $GLOBALS['cfg']['TextareaCols'];
  905. if ($column['is_char']) {
  906. /**
  907. * @todo clarify the meaning of the "textfield" class and explain
  908. * why character columns have the "char" class instead
  909. */
  910. $the_class = 'char';
  911. $textAreaRows = $GLOBALS['cfg']['CharTextareaRows'];
  912. $textareaCols = $GLOBALS['cfg']['CharTextareaCols'];
  913. $extracted_columnspec = Util::extractColumnSpec(
  914. $column['Type']
  915. );
  916. $maxlength = $extracted_columnspec['spec_in_brackets'];
  917. } elseif ($GLOBALS['cfg']['LongtextDoubleTextarea']
  918. && mb_strstr($column['pma_type'], 'longtext')
  919. ) {
  920. $textAreaRows = $GLOBALS['cfg']['TextareaRows'] * 2;
  921. $textareaCols = $GLOBALS['cfg']['TextareaCols'] * 2;
  922. }
  923. $html_output = $backup_field . "\n"
  924. . '<textarea name="fields' . $column_name_appendix . '"'
  925. . ' class="' . $the_class . '"'
  926. . ($readOnly ? ' readonly="readonly"' : '')
  927. . (isset($maxlength) ? ' data-maxlength="' . $maxlength . '"' : '')
  928. . ' rows="' . $textAreaRows . '"'
  929. . ' cols="' . $textareaCols . '"'
  930. . ' dir="' . $text_dir . '"'
  931. . ' id="field_' . ($idindex) . '_3"'
  932. . (! empty($onChangeClause) ? ' ' . $onChangeClause : '')
  933. . ' tabindex="' . ($tabindex + $tabindex_for_value) . '"'
  934. . ' data-type="' . $data_type . '">'
  935. . $special_chars_encoded
  936. . '</textarea>';
  937. return $html_output;
  938. }
  939. /**
  940. * Get HTML for enum type
  941. *
  942. * @param array $column description of column in given table
  943. * @param string $backup_field hidden input field
  944. * @param string $column_name_appendix the name attribute
  945. * @param array $extracted_columnspec associative array containing type,
  946. * spec_in_brackets and possibly
  947. * enum_set_values (another array)
  948. * @param string $onChangeClause onchange clause for fields
  949. * @param integer $tabindex tab index
  950. * @param integer $tabindex_for_value offset for the values tabindex
  951. * @param integer $idindex id index
  952. * @param mixed $data data to edit
  953. * @param boolean $readOnly is column read only or not
  954. *
  955. * @return string an html snippet
  956. */
  957. private function getPmaTypeEnum(
  958. array $column,
  959. $backup_field,
  960. $column_name_appendix,
  961. array $extracted_columnspec,
  962. $onChangeClause,
  963. $tabindex,
  964. $tabindex_for_value,
  965. $idindex,
  966. $data,
  967. $readOnly
  968. ) {
  969. $html_output = '';
  970. if (! isset($column['values'])) {
  971. $column['values'] = $this->getColumnEnumValues(
  972. $column,
  973. $extracted_columnspec
  974. );
  975. }
  976. $column_enum_values = $column['values'];
  977. $html_output .= '<input type="hidden" name="fields_type'
  978. . $column_name_appendix . '" value="enum" />';
  979. $html_output .= "\n" . ' ' . $backup_field . "\n";
  980. if (mb_strlen($column['Type']) > 20) {
  981. $html_output .= $this->getDropDownDependingOnLength(
  982. $column,
  983. $column_name_appendix,
  984. $onChangeClause,
  985. $tabindex,
  986. $tabindex_for_value,
  987. $idindex,
  988. $data,
  989. $column_enum_values,
  990. $readOnly
  991. );
  992. } else {
  993. $html_output .= $this->getRadioButtonDependingOnLength(
  994. $column_name_appendix,
  995. $onChangeClause,
  996. $tabindex,
  997. $column,
  998. $tabindex_for_value,
  999. $idindex,
  1000. $data,
  1001. $column_enum_values,
  1002. $readOnly
  1003. );
  1004. }
  1005. return $html_output;
  1006. }
  1007. /**
  1008. * Get column values
  1009. *
  1010. * @param array $column description of column in given table
  1011. * @param array $extracted_columnspec associative array containing type,
  1012. * spec_in_brackets and possibly enum_set_values
  1013. * (another array)
  1014. *
  1015. * @return array column values as an associative array
  1016. */
  1017. private function getColumnEnumValues(array $column, array $extracted_columnspec)
  1018. {
  1019. $column['values'] = array();
  1020. foreach ($extracted_columnspec['enum_set_values'] as $val) {
  1021. $column['values'][] = array(
  1022. 'plain' => $val,
  1023. 'html' => htmlspecialchars($val),
  1024. );
  1025. }
  1026. return $column['values'];
  1027. }
  1028. /**
  1029. * Get HTML drop down for more than 20 string length
  1030. *
  1031. * @param array $column description of column in given table
  1032. * @param string $column_name_appendix the name attribute
  1033. * @param string $onChangeClause onchange clause for fields
  1034. * @param integer $tabindex tab index
  1035. * @param integer $tabindex_for_value offset for the values tabindex
  1036. * @param integer $idindex id index
  1037. * @param string $data data to edit
  1038. * @param array $column_enum_values $column['values']
  1039. * @param boolean $readOnly is column read only or not
  1040. *
  1041. * @return string an html snippet
  1042. */
  1043. private function getDropDownDependingOnLength(
  1044. array $column,
  1045. $column_name_appendix,
  1046. $onChangeClause,
  1047. $tabindex,
  1048. $tabindex_for_value,
  1049. $idindex,
  1050. $data,
  1051. array $column_enum_values,
  1052. $readOnly
  1053. ) {
  1054. $html_output = '<select name="fields' . $column_name_appendix . '"'
  1055. . ' ' . $onChangeClause
  1056. . ' class="textfield"'
  1057. . ' tabindex="' . ($tabindex + $tabindex_for_value) . '"'
  1058. . ($readOnly ? ' disabled' : '')
  1059. . ' id="field_' . ($idindex) . '_3">';
  1060. $html_output .= '<option value="">&nbsp;</option>' . "\n";
  1061. $selected_html = '';
  1062. foreach ($column_enum_values as $enum_value) {
  1063. $html_output .= '<option value="' . $enum_value['html'] . '"';
  1064. if ($data == $enum_value['plain']
  1065. || ($data == ''
  1066. && (! isset($_REQUEST['where_clause']) || $column['Null'] != 'YES')
  1067. && isset($column['Default'])
  1068. && $enum_value['plain'] == $column['Default'])
  1069. ) {
  1070. $html_output .= ' selected="selected"';
  1071. $selected_html = $enum_value['html'];
  1072. }
  1073. $html_output .= '>' . $enum_value['html'] . '</option>' . "\n";
  1074. }
  1075. $html_output .= '</select>';
  1076. //Add hidden input, as disabled <select> input does not included in POST.
  1077. if ($readOnly) {
  1078. $html_output .= '<input name="fields' . $column_name_appendix . '"'
  1079. . ' type="hidden" value="' . $selected_html . '">';
  1080. }
  1081. return $html_output;
  1082. }
  1083. /**
  1084. * Get HTML radio button for less than 20 string length
  1085. *
  1086. * @param string $column_name_appendix the name attribute
  1087. * @param string $onChangeClause onchange clause for fields
  1088. * @param integer $tabindex tab index
  1089. * @param array $column description of column in given table
  1090. * @param integer $tabindex_for_value offset for the values tabindex
  1091. * @param integer $idindex id index
  1092. * @param string $data data to edit
  1093. * @param array $column_enum_values $column['values']
  1094. * @param boolean $readOnly is column read only or not
  1095. *
  1096. * @return string an html snippet
  1097. */
  1098. private function getRadioButtonDependingOnLength(
  1099. $column_name_appendix,
  1100. $onChangeClause,
  1101. $tabindex,
  1102. array $column,
  1103. $tabindex_for_value,
  1104. $idindex,
  1105. $data,
  1106. array $column_enum_values,
  1107. $readOnly
  1108. ) {
  1109. $j = 0;
  1110. $html_output = '';
  1111. foreach ($column_enum_values as $enum_value) {
  1112. $html_output .= ' '
  1113. . '<input type="radio" name="fields' . $column_name_appendix . '"'
  1114. . ' class="textfield"'
  1115. . ' value="' . $enum_value['html'] . '"'
  1116. . ' id="field_' . ($idindex) . '_3_' . $j . '"'
  1117. . ' ' . $onChangeClause;
  1118. if ($data == $enum_value['plain']
  1119. || ($data == ''
  1120. && (! isset($_REQUEST['where_clause']) || $column['Null'] != 'YES')
  1121. && isset($column['Default'])
  1122. && $enum_value['plain'] == $column['Default'])
  1123. ) {
  1124. $html_output .= ' checked="checked"';
  1125. } elseif ($readOnly) {
  1126. $html_output .= ' disabled';
  1127. }
  1128. $html_output .= ' tabindex="' . ($tabindex + $tabindex_for_value) . '" />';
  1129. $html_output .= '<label for="field_' . $idindex . '_3_' . $j . '">'
  1130. . $enum_value['html'] . '</label>' . "\n";
  1131. $j++;
  1132. }
  1133. return $html_output;
  1134. }
  1135. /**
  1136. * Get the HTML for 'set' pma type
  1137. *
  1138. * @param array $column description of column in given table
  1139. * @param array $extracted_columnspec associative array containing type,
  1140. * spec_in_brackets and possibly
  1141. * enum_set_values (another array)
  1142. * @param string $backup_field hidden input field
  1143. * @param string $column_name_appendix the name attribute
  1144. * @param string $onChangeClause onchange clause for fields
  1145. * @param integer $tabindex tab index
  1146. * @param integer $tabindex_for_value offset for the values tabindex
  1147. * @param integer $idindex id index
  1148. * @param string $data description of the column field
  1149. * @param boolean $readOnly is column read only or not
  1150. *
  1151. * @return string an html snippet
  1152. */
  1153. private function getPmaTypeSet(
  1154. array $column,
  1155. array $extracted_columnspec,
  1156. $backup_field,
  1157. $column_name_appendix,
  1158. $onChangeClause,
  1159. $tabindex,
  1160. $tabindex_for_value,
  1161. $idindex,
  1162. $data,
  1163. $readOnly
  1164. ) {
  1165. list($column_set_values, $select_size) = $this->getColumnSetValueAndSelectSize(
  1166. $column,
  1167. $extracted_columnspec
  1168. );
  1169. $vset = array_flip(explode(',', $data));
  1170. $html_output = $backup_field . "\n";
  1171. $html_output .= '<input type="hidden" name="fields_type'
  1172. . $column_name_appendix . '" value="set" />';
  1173. $html_output .= '<select name="fields' . $column_name_appendix . '[]' . '"'
  1174. . ' class="textfield"'
  1175. . ($readOnly ? ' disabled' : '')
  1176. . ' size="' . $select_size . '"'
  1177. . ' multiple="multiple"'
  1178. . ' ' . $onChangeClause
  1179. . ' tabindex="' . ($tabindex + $tabindex_for_value) . '"'
  1180. . ' id="field_' . ($idindex) . '_3">';
  1181. $selected_html = '';
  1182. foreach ($column_set_values as $column_set_value) {
  1183. $html_output .= '<option value="' . $column_set_value['html'] . '"';
  1184. if (isset($vset[$column_set_value['plain']])) {
  1185. $html_output .= ' selected="selected"';
  1186. $selected_html = $column_set_value['html'];
  1187. }
  1188. $html_output .= '>' . $column_set_value['html'] . '</option>' . "\n";
  1189. }
  1190. $html_output .= '</select>';
  1191. //Add hidden input, as disabled <select> input does not included in POST.
  1192. if ($readOnly) {
  1193. $html_output .= '<input name="fields' . $column_name_appendix . '[]' . '"'
  1194. . ' type="hidden" value="' . $selected_html . '">';
  1195. }
  1196. return $html_output;
  1197. }
  1198. /**
  1199. * Retrieve column 'set' value and select size
  1200. *
  1201. * @param array $column description of column in given table
  1202. * @param array $extracted_columnspec associative array containing type,
  1203. * spec_in_brackets and possibly enum_set_values
  1204. * (another array)
  1205. *
  1206. * @return array $column['values'], $column['select_size']
  1207. */
  1208. private function getColumnSetValueAndSelectSize(
  1209. array $column,
  1210. array $extracted_columnspec
  1211. ) {
  1212. if (! isset($column['values'])) {
  1213. $column['values'] = array();
  1214. foreach ($extracted_columnspec['enum_set_values'] as $val) {
  1215. $column['values'][] = array(
  1216. 'plain' => $val,
  1217. 'html' => htmlspecialchars($val),
  1218. );
  1219. }
  1220. $column['select_size'] = min(4, count($column['values']));
  1221. }
  1222. return array($column['values'], $column['select_size']);
  1223. }
  1224. /**
  1225. * Get HTML for binary and blob column
  1226. *
  1227. * @param array $column description of column in given table
  1228. * @param string $data data to edit
  1229. * @param string $special_chars special characters
  1230. * @param integer $biggest_max_file_size biggest max file size for uploading
  1231. * @param string $backup_field hidden input field
  1232. * @param string $column_name_appendix the name attribute
  1233. * @param string $onChangeClause onchange clause for fields
  1234. * @param integer $tabindex tab index
  1235. * @param integer $tabindex_for_value offset for the values tabindex
  1236. * @param integer $idindex id index
  1237. * @param string $text_dir text direction
  1238. * @param string $special_chars_encoded replaced char if the string starts
  1239. * with a \r\n pair (0x0d0a) add an extra \n
  1240. * @param string $vkey [multi_edit]['row_id']
  1241. * @param boolean $is_upload is upload or not
  1242. * @param boolean $readOnly is column read only or not
  1243. *
  1244. * @return string an html snippet
  1245. */
  1246. private function getBinaryAndBlobColumn(
  1247. array $column,
  1248. $data,
  1249. $special_chars,
  1250. $biggest_max_file_size,
  1251. $backup_field,
  1252. $column_name_appendix,
  1253. $onChangeClause,
  1254. $tabindex,
  1255. $tabindex_for_value,
  1256. $idindex,
  1257. $text_dir,
  1258. $special_chars_encoded,
  1259. $vkey,
  1260. $is_upload,
  1261. $readOnly
  1262. ) {
  1263. $html_output = '';
  1264. // Add field type : Protected or Hexadecimal
  1265. $fields_type_html = '<input type="hidden" name="fields_type'
  1266. . $column_name_appendix . '" value="%s" />';
  1267. // Default value : hex
  1268. $fields_type_val = 'hex';
  1269. if (($GLOBALS['cfg']['ProtectBinary'] === 'blob' && $column['is_blob'])
  1270. || ($GLOBALS['cfg']['ProtectBinary'] === 'all')
  1271. || ($GLOBALS['cfg']['ProtectBinary'] === 'noblob' && !$column['is_blob'])
  1272. ) {
  1273. $html_output .= __('Binary - do not edit');
  1274. if (isset($data)) {
  1275. $data_size = Util::formatByteDown(
  1276. mb_strlen(stripslashes($data)),
  1277. 3,
  1278. 1
  1279. );
  1280. $html_output .= ' (' . $data_size[0] . ' ' . $data_size[1] . ')';
  1281. unset($data_size);
  1282. }
  1283. $fields_type_val = 'protected';
  1284. $html_output .= '<input type="hidden" name="fields'
  1285. . $column_name_appendix . '" value="" />';
  1286. } elseif ($column['is_blob']
  1287. || ($column['len'] > $GLOBALS['cfg']['LimitChars'])
  1288. ) {
  1289. $html_output .= "\n" . $this->getTextarea(
  1290. $column,
  1291. $backup_field,
  1292. $column_name_appendix,
  1293. $onChangeClause,
  1294. $tabindex,
  1295. $tabindex_for_value,
  1296. $idindex,
  1297. $text_dir,
  1298. $special_chars_encoded,
  1299. 'HEX',
  1300. $readOnly
  1301. );
  1302. } else {
  1303. // field size should be at least 4 and max $GLOBALS['cfg']['LimitChars']
  1304. $fieldsize = min(max($column['len'], 4), $GLOBALS['cfg']['LimitChars']);
  1305. $html_output .= "\n" . $backup_field . "\n" . $this->getHtmlInput(
  1306. $column,
  1307. $column_name_appendix,
  1308. $special_chars,
  1309. $fieldsize,
  1310. $onChangeClause,
  1311. $tabindex,
  1312. $tabindex_for_value,
  1313. $idindex,
  1314. 'HEX',
  1315. $readOnly
  1316. );
  1317. }
  1318. $html_output .= sprintf($fields_type_html, $fields_type_val);
  1319. if ($is_upload && $column['is_blob'] && !$readOnly) {
  1320. // We don't want to prevent users from using
  1321. // browser's default drag-drop feature on some page(s),
  1322. // so we add noDragDrop class to the input
  1323. $html_output .= '<br />'
  1324. . '<input type="file"'
  1325. . ' name="fields_upload' . $vkey . '[' . $column['Field_md5'] . ']"'
  1326. . ' class="textfield noDragDrop" id="field_' . $idindex . '_3" size="10"'
  1327. . ' ' . $onChangeClause . '/>&nbsp;';
  1328. list($html_out,) = $this->getMaxUploadSize(
  1329. $column,
  1330. $biggest_max_file_size
  1331. );
  1332. $html_output .= $html_out;
  1333. }
  1334. if (!empty($GLOBALS['cfg']['UploadDir']) && !$readOnly) {
  1335. $html_output .= $this->getSelectOptionForUpload($vkey, $column);
  1336. }
  1337. return $html_output;
  1338. }
  1339. /**
  1340. * Get HTML input type
  1341. *
  1342. * @param array $column description of column in given table
  1343. * @param string $column_name_appendix the name attribute
  1344. * @param string $special_chars special characters
  1345. * @param integer $fieldsize html field size
  1346. * @param string $onChangeClause onchange clause for fields
  1347. * @param integer $tabindex tab index
  1348. * @param integer $tabindex_for_value offset for the values tabindex
  1349. * @param integer $idindex id index
  1350. * @param string $data_type the html5 data-* attribute type
  1351. * @param boolean $readOnly is column read only or not
  1352. *
  1353. * @return string an html snippet
  1354. */
  1355. private function getHtmlInput(
  1356. array $column,
  1357. $column_name_appendix,
  1358. $special_chars,
  1359. $fieldsize,
  1360. $onChangeClause,
  1361. $tabindex,
  1362. $tabindex_for_value,
  1363. $idindex,
  1364. $data_type,
  1365. $readOnly
  1366. ) {
  1367. $input_type = 'text';
  1368. // do not use the 'date' or 'time' types here; they have no effect on some
  1369. // browsers and create side effects (see bug #4218)
  1370. $the_class = 'textfield';
  1371. // verify True_Type which does not contain the parentheses and length
  1372. if ($readOnly) {
  1373. //NOOP. Disable date/timepicker
  1374. } elseif ($column['True_Type'] === 'date') {
  1375. $the_class .= ' datefield';
  1376. } elseif ($column['True_Type'] === 'time') {
  1377. $the_class .= ' timefield';
  1378. } elseif ($column['True_Type'] === 'datetime'
  1379. || $column['True_Type'] === 'timestamp'
  1380. ) {
  1381. $the_class .= ' datetimefield';
  1382. }
  1383. $input_min_max = false;
  1384. if (in_array($column['True_Type'], $this->dbi->types->getIntegerTypes())) {
  1385. $extracted_columnspec = Util::extractColumnSpec(
  1386. $column['Type']
  1387. );
  1388. $is_unsigned = $extracted_columnspec['unsigned'];
  1389. $min_max_values = $this->dbi->types->getIntegerRange(
  1390. $column['True_Type'],
  1391. ! $is_unsigned
  1392. );
  1393. $input_min_max = 'min="' . $min_max_values[0] . '" '
  1394. . 'max="' . $min_max_values[1] . '"';
  1395. $data_type = 'INT';
  1396. }
  1397. return '<input type="' . $input_type . '"'
  1398. . ' name="fields' . $column_name_appendix . '"'
  1399. . ' value="' . $special_chars . '" size="' . $fieldsize . '"'
  1400. . ((isset($column['is_char']) && $column['is_char'])
  1401. ? ' data-maxlength="' . $fieldsize . '"'
  1402. : '')
  1403. . ($readOnly ? ' readonly="readonly"' : '')
  1404. . ($input_min_max !== false ? ' ' . $input_min_max : '')
  1405. . ' data-type="' . $data_type . '"'
  1406. . ($input_type === 'time' ? ' step="1"' : '')
  1407. . ' class="' . $the_class . '" ' . $onChangeClause
  1408. . ' tabindex="' . ($tabindex + $tabindex_for_value) . '"'
  1409. . ' id="field_' . ($idindex) . '_3" />';
  1410. }
  1411. /**
  1412. * Get HTML select option for upload
  1413. *
  1414. * @param string $vkey [multi_edit]['row_id']
  1415. * @param array $column description of column in given table
  1416. *
  1417. * @return string|void an html snippet
  1418. */
  1419. private function getSelectOptionForUpload($vkey, array $column)
  1420. {
  1421. $files = FileListing::getFileSelectOptions(
  1422. Util::userDir($GLOBALS['cfg']['UploadDir'])
  1423. );
  1424. if ($files === false) {
  1425. return '<span style="color:red">' . __('Error') . '</span><br />' . "\n"
  1426. . __('The directory you set for upload work cannot be reached.') . "\n";
  1427. } elseif (!empty($files)) {
  1428. return "<br />\n"
  1429. . '<i>' . __('Or') . '</i>' . ' '
  1430. . __('web server upload directory:') . '<br />' . "\n"
  1431. . '<select size="1" name="fields_uploadlocal'
  1432. . $vkey . '[' . $column['Field_md5'] . ']">' . "\n"
  1433. . '<option value="" selected="selected"></option>' . "\n"
  1434. . $files
  1435. . '</select>' . "\n";
  1436. }
  1437. return null;
  1438. }
  1439. /**
  1440. * Retrieve the maximum upload file size
  1441. *
  1442. * @param array $column description of column in given table
  1443. * @param integer $biggest_max_file_size biggest max file size for uploading
  1444. *
  1445. * @return array an html snippet and $biggest_max_file_size
  1446. */
  1447. private function getMaxUploadSize(array $column, $biggest_max_file_size)
  1448. {
  1449. // find maximum upload size, based on field type
  1450. /**
  1451. * @todo with functions this is not so easy, as you can basically
  1452. * process any data with function like MD5
  1453. */
  1454. global $max_upload_size;
  1455. $max_field_sizes = array(
  1456. 'tinyblob' => '256',
  1457. 'blob' => '65536',
  1458. 'mediumblob' => '16777216',
  1459. 'longblob' => '4294967296' // yeah, really
  1460. );
  1461. $this_field_max_size = $max_upload_size; // from PHP max
  1462. if ($this_field_max_size > $max_field_sizes[$column['pma_type']]) {
  1463. $this_field_max_size = $max_field_sizes[$column['pma_type']];
  1464. }
  1465. $html_output
  1466. = Util::getFormattedMaximumUploadSize(
  1467. $this_field_max_size
  1468. ) . "\n";
  1469. // do not generate here the MAX_FILE_SIZE, because we should
  1470. // put only one in the form to accommodate the biggest field
  1471. if ($this_field_max_size > $biggest_max_file_size) {
  1472. $biggest_max_file_size = $this_field_max_size;
  1473. }
  1474. return array($html_output, $biggest_max_file_size);
  1475. }
  1476. /**
  1477. * Get HTML for the Value column of other datatypes
  1478. * (here, "column" is used in the sense of HTML column in HTML table)
  1479. *
  1480. * @param array $column description of column in given table
  1481. * @param string $default_char_editing default char editing mode which is stored
  1482. * in the config.inc.php script
  1483. * @param string $backup_field hidden input field
  1484. * @param string $column_name_appendix the name attribute
  1485. * @param string $onChangeClause onchange clause for fields
  1486. * @param integer $tabindex tab index
  1487. * @param string $special_chars special characters
  1488. * @param integer $tabindex_for_value offset for the values tabindex
  1489. * @param integer $idindex id index
  1490. * @param string $text_dir text direction
  1491. * @param string $special_chars_encoded replaced char if the string starts
  1492. * with a \r\n pair (0x0d0a) add an extra \n
  1493. * @param string $data data to edit
  1494. * @param array $extracted_columnspec associative array containing type,
  1495. * spec_in_brackets and possibly
  1496. * enum_set_values (another array)
  1497. * @param boolean $readOnly is column read only or not
  1498. *
  1499. * @return string an html snippet
  1500. */
  1501. private function getValueColumnForOtherDatatypes(
  1502. array $column,
  1503. $default_char_editing,
  1504. $backup_field,
  1505. $column_name_appendix,
  1506. $onChangeClause,
  1507. $tabindex,
  1508. $special_chars,
  1509. $tabindex_for_value,
  1510. $idindex,
  1511. $text_dir,
  1512. $special_chars_encoded,
  1513. $data,
  1514. array $extracted_columnspec,
  1515. $readOnly
  1516. ) {
  1517. // HTML5 data-* attribute data-type
  1518. $data_type = $this->dbi->types->getTypeClass($column['True_Type']);
  1519. $fieldsize = $this->getColumnSize($column, $extracted_columnspec);
  1520. $html_output = $backup_field . "\n";
  1521. if ($column['is_char']
  1522. && ($GLOBALS['cfg']['CharEditing'] == 'textarea'
  1523. || mb_strpos($data, "\n") !== false)
  1524. ) {
  1525. $html_output .= "\n";
  1526. $GLOBALS['cfg']['CharEditing'] = $default_char_editing;
  1527. $html_output .= $this->getTextarea(
  1528. $column,
  1529. $backup_field,
  1530. $column_name_appendix,
  1531. $onChangeClause,
  1532. $tabindex,
  1533. $tabindex_for_value,
  1534. $idindex,
  1535. $text_dir,
  1536. $special_chars_encoded,
  1537. $data_type,
  1538. $readOnly
  1539. );
  1540. } else {
  1541. $html_output .= $this->getHtmlInput(
  1542. $column,
  1543. $column_name_appendix,
  1544. $special_chars,
  1545. $fieldsize,
  1546. $onChangeClause,
  1547. $tabindex,
  1548. $tabindex_for_value,
  1549. $idindex,
  1550. $data_type,
  1551. $readOnly
  1552. );
  1553. $virtual = array(
  1554. 'VIRTUAL', 'PERSISTENT', 'VIRTUAL GENERATED', 'STORED GENERATED'
  1555. );
  1556. if (in_array($column['Extra'], $virtual)) {
  1557. $html_output .= '<input type="hidden" name="virtual'
  1558. . $column_name_appendix . '" value="1" />';
  1559. }
  1560. if ($column['Extra'] == 'auto_increment') {
  1561. $html_output .= '<input type="hidden" name="auto_increment'
  1562. . $column_name_appendix . '" value="1" />';
  1563. }
  1564. if (substr($column['pma_type'], 0, 9) == 'timestamp') {
  1565. $html_output .= '<input type="hidden" name="fields_type'
  1566. . $column_name_appendix . '" value="timestamp" />';
  1567. }
  1568. if (substr($column['pma_type'], 0, 8) == 'datetime') {
  1569. $html_output .= '<input type="hidden" name="fields_type'
  1570. . $column_name_appendix . '" value="datetime" />';
  1571. }
  1572. if ($column['True_Type'] == 'bit') {
  1573. $html_output .= '<input type="hidden" name="fields_type'
  1574. . $column_name_appendix . '" value="bit" />';
  1575. }
  1576. if ($column['pma_type'] == 'date'
  1577. || $column['pma_type'] == 'datetime'
  1578. || substr($column['pma_type'], 0, 9) == 'timestamp'
  1579. ) {
  1580. // the _3 suffix points to the date field
  1581. // the _2 suffix points to the corresponding NULL checkbox
  1582. // in dateFormat, 'yy' means the year with 4 digits
  1583. }
  1584. }
  1585. return $html_output;
  1586. }
  1587. /**
  1588. * Get the field size
  1589. *
  1590. * @param array $column description of column in given table
  1591. * @param array $extracted_columnspec associative array containing type,
  1592. * spec_in_brackets and possibly enum_set_values
  1593. * (another array)
  1594. *
  1595. * @return integer field size
  1596. */
  1597. private function getColumnSize(array $column, array $extracted_columnspec)
  1598. {
  1599. if ($column['is_char']) {
  1600. $fieldsize = $extracted_columnspec['spec_in_brackets'];
  1601. if ($fieldsize > $GLOBALS['cfg']['MaxSizeForInputField']) {
  1602. /**
  1603. * This case happens for CHAR or VARCHAR columns which have
  1604. * a size larger than the maximum size for input field.
  1605. */
  1606. $GLOBALS['cfg']['CharEditing'] = 'textarea';
  1607. }
  1608. } else {
  1609. /**
  1610. * This case happens for example for INT or DATE columns;
  1611. * in these situations, the value returned in $column['len']
  1612. * seems appropriate.
  1613. */
  1614. $fieldsize = $column['len'];
  1615. }
  1616. return min(
  1617. max($fieldsize, $GLOBALS['cfg']['MinSizeForInputField']),
  1618. $GLOBALS['cfg']['MaxSizeForInputField']
  1619. );
  1620. }
  1621. /**
  1622. * Get HTML for gis data types
  1623. *
  1624. * @return string an html snippet
  1625. */
  1626. private function getHtmlForGisDataTypes()
  1627. {
  1628. $edit_str = Util::getIcon('b_edit', __('Edit/Insert'));
  1629. return '<span class="open_gis_editor">'
  1630. . Util::linkOrButton(
  1631. '#',
  1632. $edit_str,
  1633. array(),
  1634. '_blank'
  1635. )
  1636. . '</span>';
  1637. }
  1638. /**
  1639. * get html for continue insertion form
  1640. *
  1641. * @param string $table name of the table
  1642. * @param string $db name of the database
  1643. * @param array $where_clause_array array of where clauses
  1644. * @param string $err_url error url
  1645. *
  1646. * @return string an html snippet
  1647. */
  1648. public function getContinueInsertionForm(
  1649. $table,
  1650. $db,
  1651. array $where_clause_array,
  1652. $err_url
  1653. ) {
  1654. return Template::get('table/insert/continue_insertion_form')->render([
  1655. 'db' => $db,
  1656. 'table' => $table,
  1657. 'where_clause_array' => $where_clause_array,
  1658. 'err_url' => $err_url,
  1659. 'goto' => $GLOBALS['goto'],
  1660. 'sql_query' => isset($_POST['sql_query']) ? $_POST['sql_query'] : null,
  1661. 'has_where_clause' => isset($_REQUEST['where_clause']),
  1662. 'insert_rows_default' => $GLOBALS['cfg']['InsertRows'],
  1663. ]);
  1664. }
  1665. /**
  1666. * Get action panel
  1667. *
  1668. * @param array|null $where_clause where clause
  1669. * @param string $after_insert insert mode, e.g. new_insert, same_insert
  1670. * @param integer $tabindex tab index
  1671. * @param integer $tabindex_for_value offset for the values tabindex
  1672. * @param boolean $found_unique_key boolean variable for unique key
  1673. *
  1674. * @return string an html snippet
  1675. */
  1676. public function getActionsPanel(
  1677. $where_clause,
  1678. $after_insert,
  1679. $tabindex,
  1680. $tabindex_for_value,
  1681. $found_unique_key
  1682. ) {
  1683. $html_output = '<fieldset id="actions_panel">'
  1684. . '<table cellpadding="5" cellspacing="0" class="tdblock width100">'
  1685. . '<tr>'
  1686. . '<td class="nowrap vmiddle">'
  1687. . $this->getSubmitTypeDropDown($where_clause, $tabindex, $tabindex_for_value)
  1688. . "\n";
  1689. $html_output .= '</td>'
  1690. . '<td class="vmiddle">'
  1691. . '&nbsp;&nbsp;&nbsp;<strong>'
  1692. . __('and then') . '</strong>&nbsp;&nbsp;&nbsp;'
  1693. . '</td>'
  1694. . '<td class="nowrap vmiddle">'
  1695. . $this->getAfterInsertDropDown(
  1696. $where_clause,
  1697. $after_insert,
  1698. $found_unique_key
  1699. )
  1700. . '</td>'
  1701. . '</tr>';
  1702. $html_output .='<tr>'
  1703. . $this->getSubmitAndResetButtonForActionsPanel($tabindex, $tabindex_for_value)
  1704. . '</tr>'
  1705. . '</table>'
  1706. . '</fieldset>';
  1707. return $html_output;
  1708. }
  1709. /**
  1710. * Get a HTML drop down for submit types
  1711. *
  1712. * @param array|null $where_clause where clause
  1713. * @param integer $tabindex tab index
  1714. * @param integer $tabindex_for_value offset for the values tabindex
  1715. *
  1716. * @return string an html snippet
  1717. */
  1718. private function getSubmitTypeDropDown(
  1719. $where_clause,
  1720. $tabindex,
  1721. $tabindex_for_value
  1722. ) {
  1723. $html_output = '<select name="submit_type" class="control_at_footer" tabindex="'
  1724. . ($tabindex + $tabindex_for_value + 1) . '">';
  1725. if (isset($where_clause)) {
  1726. $html_output .= '<option value="save">' . __('Save') . '</option>';
  1727. }
  1728. $html_output .= '<option value="insert">'
  1729. . __('Insert as new row')
  1730. . '</option>'
  1731. . '<option value="insertignore">'
  1732. . __('Insert as new row and ignore errors')
  1733. . '</option>'
  1734. . '<option value="showinsert">'
  1735. . __('Show insert query')
  1736. . '</option>'
  1737. . '</select>';
  1738. return $html_output;
  1739. }
  1740. /**
  1741. * Get HTML drop down for after insert
  1742. *
  1743. * @param array|null $where_clause where clause
  1744. * @param string $after_insert insert mode, e.g. new_insert, same_insert
  1745. * @param boolean $found_unique_key boolean variable for unique key
  1746. *
  1747. * @return string an html snippet
  1748. */
  1749. private function getAfterInsertDropDown($where_clause, $after_insert, $found_unique_key)
  1750. {
  1751. $html_output = '<select name="after_insert" class="control_at_footer">'
  1752. . '<option value="back" '
  1753. . ($after_insert == 'back' ? 'selected="selected"' : '') . '>'
  1754. . __('Go back to previous page') . '</option>'
  1755. . '<option value="new_insert" '
  1756. . ($after_insert == 'new_insert' ? 'selected="selected"' : '') . '>'
  1757. . __('Insert another new row') . '</option>';
  1758. if (isset($where_clause)) {
  1759. $html_output .= '<option value="same_insert" '
  1760. . ($after_insert == 'same_insert' ? 'selected="selected"' : '') . '>'
  1761. . __('Go back to this page') . '</option>';
  1762. // If we have just numeric primary key, we can also edit next
  1763. // in 2.8.2, we were looking for `field_name` = numeric_value
  1764. //if (preg_match('@^[\s]*`[^`]*` = [0-9]+@', $where_clause)) {
  1765. // in 2.9.0, we are looking for `table_name`.`field_name` = numeric_value
  1766. $is_numeric = false;
  1767. if (! is_array($where_clause)) {
  1768. $where_clause = array($where_clause);
  1769. }
  1770. for ($i = 0, $nb = count($where_clause); $i < $nb; $i++) {
  1771. // preg_match() returns 1 if there is a match
  1772. $is_numeric = (preg_match(
  1773. '@^[\s]*`[^`]*`[\.]`[^`]*` = [0-9]+@',
  1774. $where_clause[$i]
  1775. ) == 1);
  1776. if ($is_numeric === true) {
  1777. break;
  1778. }
  1779. }
  1780. if ($found_unique_key && $is_numeric) {
  1781. $html_output .= '<option value="edit_next" '
  1782. . ($after_insert == 'edit_next' ? 'selected="selected"' : '') . '>'
  1783. . __('Edit next row') . '</option>';
  1784. }
  1785. }
  1786. $html_output .= '</select>';
  1787. return $html_output;
  1788. }
  1789. /**
  1790. * get Submit button and Reset button for action panel
  1791. *
  1792. * @param integer $tabindex tab index
  1793. * @param integer $tabindex_for_value offset for the values tabindex
  1794. *
  1795. * @return string an html snippet
  1796. */
  1797. private function getSubmitAndResetButtonForActionsPanel($tabindex, $tabindex_for_value)
  1798. {
  1799. return '<td>'
  1800. . Util::showHint(
  1801. __(
  1802. 'Use TAB key to move from value to value,'
  1803. . ' or CTRL+arrows to move anywhere.'
  1804. )
  1805. )
  1806. . '</td>'
  1807. . '<td colspan="3" class="right vmiddle">'
  1808. . '<input type="submit" class="control_at_footer" value="' . __('Go') . '"'
  1809. . ' tabindex="' . ($tabindex + $tabindex_for_value + 6) . '" id="buttonYes" />'
  1810. . '<input type="button" class="preview_sql" value="' . __('Preview SQL') . '"'
  1811. . ' tabindex="' . ($tabindex + $tabindex_for_value + 7) . '" />'
  1812. . '<input type="reset" class="control_at_footer" value="' . __('Reset') . '"'
  1813. . ' tabindex="' . ($tabindex + $tabindex_for_value + 8) . '" />'
  1814. . '</td>';
  1815. }
  1816. /**
  1817. * Get table head and table foot for insert row table
  1818. *
  1819. * @param array $url_params url parameters
  1820. *
  1821. * @return string an html snippet
  1822. */
  1823. private function getHeadAndFootOfInsertRowTable(array $url_params)
  1824. {
  1825. $html_output = '<div class="responsivetable">'
  1826. . '<table class="insertRowTable topmargin">'
  1827. . '<thead>'
  1828. . '<tr>'
  1829. . '<th>' . __('Column') . '</th>';
  1830. if ($GLOBALS['cfg']['ShowFieldTypesInDataEditView']) {
  1831. $html_output .= $this->showTypeOrFunction('type', $url_params, true);
  1832. }
  1833. if ($GLOBALS['cfg']['ShowFunctionFields']) {
  1834. $html_output .= $this->showTypeOrFunction('function', $url_params, true);
  1835. }
  1836. $html_output .= '<th>' . __('Null') . '</th>'
  1837. . '<th>' . __('Value') . '</th>'
  1838. . '</tr>'
  1839. . '</thead>'
  1840. . ' <tfoot>'
  1841. . '<tr>'
  1842. . '<th colspan="5" class="tblFooters right">'
  1843. . '<input type="submit" value="' . __('Go') . '" />'
  1844. . '</th>'
  1845. . '</tr>'
  1846. . '</tfoot>';
  1847. return $html_output;
  1848. }
  1849. /**
  1850. * Prepares the field value and retrieve special chars, backup field and data array
  1851. *
  1852. * @param array $current_row a row of the table
  1853. * @param array $column description of column in given table
  1854. * @param array $extracted_columnspec associative array containing type,
  1855. * spec_in_brackets and possibly
  1856. * enum_set_values (another array)
  1857. * @param boolean $real_null_value whether column value null or not null
  1858. * @param array $gis_data_types list of GIS data types
  1859. * @param string $column_name_appendix string to append to column name in input
  1860. * @param bool $as_is use the data as is, used in repopulating
  1861. *
  1862. * @return array $real_null_value, $data, $special_chars, $backup_field,
  1863. * $special_chars_encoded
  1864. */
  1865. private function getSpecialCharsAndBackupFieldForExistingRow(
  1866. array $current_row,
  1867. array $column,
  1868. array $extracted_columnspec,
  1869. $real_null_value,
  1870. array $gis_data_types,
  1871. $column_name_appendix,
  1872. $as_is
  1873. ) {
  1874. $special_chars_encoded = '';
  1875. $data = null;
  1876. // (we are editing)
  1877. if (!isset($current_row[$column['Field']])) {
  1878. $real_null_value = true;
  1879. $current_row[$column['Field']] = '';
  1880. $special_chars = '';
  1881. $data = $current_row[$column['Field']];
  1882. } elseif ($column['True_Type'] == 'bit') {
  1883. $special_chars = $as_is
  1884. ? $current_row[$column['Field']]
  1885. : Util::printableBitValue(
  1886. $current_row[$column['Field']],
  1887. $extracted_columnspec['spec_in_brackets']
  1888. );
  1889. } elseif ((substr($column['True_Type'], 0, 9) == 'timestamp'
  1890. || $column['True_Type'] == 'datetime'
  1891. || $column['True_Type'] == 'time')
  1892. && (mb_strpos($current_row[$column['Field']], ".") !== false)
  1893. ) {
  1894. $current_row[$column['Field']] = $as_is
  1895. ? $current_row[$column['Field']]
  1896. : Util::addMicroseconds(
  1897. $current_row[$column['Field']]
  1898. );
  1899. $special_chars = htmlspecialchars($current_row[$column['Field']]);
  1900. } elseif (in_array($column['True_Type'], $gis_data_types)) {
  1901. // Convert gis data to Well Know Text format
  1902. $current_row[$column['Field']] = $as_is
  1903. ? $current_row[$column['Field']]
  1904. : Util::asWKT(
  1905. $current_row[$column['Field']],
  1906. true
  1907. );
  1908. $special_chars = htmlspecialchars($current_row[$column['Field']]);
  1909. } else {
  1910. // special binary "characters"
  1911. if ($column['is_binary']
  1912. || ($column['is_blob'] && $GLOBALS['cfg']['ProtectBinary'] !== 'all')
  1913. ) {
  1914. $current_row[$column['Field']] = $as_is
  1915. ? $current_row[$column['Field']]
  1916. : bin2hex(
  1917. $current_row[$column['Field']]
  1918. );
  1919. } // end if
  1920. $special_chars = htmlspecialchars($current_row[$column['Field']]);
  1921. //We need to duplicate the first \n or otherwise we will lose
  1922. //the first newline entered in a VARCHAR or TEXT column
  1923. $special_chars_encoded
  1924. = Util::duplicateFirstNewline($special_chars);
  1925. $data = $current_row[$column['Field']];
  1926. } // end if... else...
  1927. //when copying row, it is useful to empty auto-increment column
  1928. // to prevent duplicate key error
  1929. if (isset($_REQUEST['default_action'])
  1930. && $_REQUEST['default_action'] === 'insert'
  1931. ) {
  1932. if ($column['Key'] === 'PRI'
  1933. && mb_strpos($column['Extra'], 'auto_increment') !== false
  1934. ) {
  1935. $data = $special_chars_encoded = $special_chars = null;
  1936. }
  1937. }
  1938. // If a timestamp field value is not included in an update
  1939. // statement MySQL auto-update it to the current timestamp;
  1940. // however, things have changed since MySQL 4.1, so
  1941. // it's better to set a fields_prev in this situation
  1942. $backup_field = '<input type="hidden" name="fields_prev'
  1943. . $column_name_appendix . '" value="'
  1944. . htmlspecialchars($current_row[$column['Field']]) . '" />';
  1945. return array(
  1946. $real_null_value,
  1947. $special_chars_encoded,
  1948. $special_chars,
  1949. $data,
  1950. $backup_field
  1951. );
  1952. }
  1953. /**
  1954. * display default values
  1955. *
  1956. * @param array $column description of column in given table
  1957. * @param boolean $real_null_value whether column value null or not null
  1958. *
  1959. * @return array $real_null_value, $data, $special_chars,
  1960. * $backup_field, $special_chars_encoded
  1961. */
  1962. private function getSpecialCharsAndBackupFieldForInsertingMode(
  1963. array $column,
  1964. $real_null_value
  1965. ) {
  1966. if (! isset($column['Default'])) {
  1967. $column['Default'] = '';
  1968. $real_null_value = true;
  1969. $data = '';
  1970. } else {
  1971. $data = $column['Default'];
  1972. }
  1973. $trueType = $column['True_Type'];
  1974. if ($trueType == 'bit') {
  1975. $special_chars = Util::convertBitDefaultValue(
  1976. $column['Default']
  1977. );
  1978. } elseif (substr($trueType, 0, 9) == 'timestamp'
  1979. || $trueType == 'datetime'
  1980. || $trueType == 'time'
  1981. ) {
  1982. $special_chars = Util::addMicroseconds($column['Default']);
  1983. } elseif ($trueType == 'binary' || $trueType == 'varbinary') {
  1984. $special_chars = bin2hex($column['Default']);
  1985. } else {
  1986. $special_chars = htmlspecialchars($column['Default']);
  1987. }
  1988. $backup_field = '';
  1989. $special_chars_encoded = Util::duplicateFirstNewline(
  1990. $special_chars
  1991. );
  1992. return array(
  1993. $real_null_value, $data, $special_chars,
  1994. $backup_field, $special_chars_encoded
  1995. );
  1996. }
  1997. /**
  1998. * Prepares the update/insert of a row
  1999. *
  2000. * @return array $loop_array, $using_key, $is_insert, $is_insertignore
  2001. */
  2002. public function getParamsForUpdateOrInsert()
  2003. {
  2004. if (isset($_REQUEST['where_clause'])) {
  2005. // we were editing something => use the WHERE clause
  2006. $loop_array = is_array($_REQUEST['where_clause'])
  2007. ? $_REQUEST['where_clause']
  2008. : array($_REQUEST['where_clause']);
  2009. $using_key = true;
  2010. $is_insert = isset($_REQUEST['submit_type'])
  2011. && ($_REQUEST['submit_type'] == 'insert'
  2012. || $_REQUEST['submit_type'] == 'showinsert'
  2013. || $_REQUEST['submit_type'] == 'insertignore');
  2014. } else {
  2015. // new row => use indexes
  2016. $loop_array = array();
  2017. if (! empty($_REQUEST['fields'])) {
  2018. foreach ($_REQUEST['fields']['multi_edit'] as $key => $dummy) {
  2019. $loop_array[] = $key;
  2020. }
  2021. }
  2022. $using_key = false;
  2023. $is_insert = true;
  2024. }
  2025. $is_insertignore = isset($_REQUEST['submit_type'])
  2026. && $_REQUEST['submit_type'] == 'insertignore';
  2027. return array($loop_array, $using_key, $is_insert, $is_insertignore);
  2028. }
  2029. /**
  2030. * Check wether insert row mode and if so include tbl_changen script and set
  2031. * global variables.
  2032. *
  2033. * @return void
  2034. */
  2035. public function isInsertRow()
  2036. {
  2037. if (isset($_REQUEST['insert_rows'])
  2038. && is_numeric($_REQUEST['insert_rows'])
  2039. && $_REQUEST['insert_rows'] != $GLOBALS['cfg']['InsertRows']
  2040. ) {
  2041. $GLOBALS['cfg']['InsertRows'] = $_REQUEST['insert_rows'];
  2042. $response = Response::getInstance();
  2043. $header = $response->getHeader();
  2044. $scripts = $header->getScripts();
  2045. $scripts->addFile('vendor/jquery/additional-methods.js');
  2046. $scripts->addFile('tbl_change.js');
  2047. if (!defined('TESTSUITE')) {
  2048. include 'tbl_change.php';
  2049. exit;
  2050. }
  2051. }
  2052. }
  2053. /**
  2054. * set $_SESSION for edit_next
  2055. *
  2056. * @param string $one_where_clause one where clause from where clauses array
  2057. *
  2058. * @return void
  2059. */
  2060. public function setSessionForEditNext($one_where_clause)
  2061. {
  2062. $local_query = 'SELECT * FROM ' . Util::backquote($GLOBALS['db'])
  2063. . '.' . Util::backquote($GLOBALS['table']) . ' WHERE '
  2064. . str_replace('` =', '` >', $one_where_clause) . ' LIMIT 1;';
  2065. $res = $this->dbi->query($local_query);
  2066. $row = $this->dbi->fetchRow($res);
  2067. $meta = $this->dbi->getFieldsMeta($res);
  2068. // must find a unique condition based on unique key,
  2069. // not a combination of all fields
  2070. list($unique_condition, $clause_is_unique)
  2071. = Util::getUniqueCondition(
  2072. $res, // handle
  2073. count($meta), // fields_cnt
  2074. $meta, // fields_meta
  2075. $row, // row
  2076. true, // force_unique
  2077. false, // restrict_to_table
  2078. null // analyzed_sql_results
  2079. );
  2080. if (! empty($unique_condition)) {
  2081. $_SESSION['edit_next'] = $unique_condition;
  2082. }
  2083. unset($unique_condition, $clause_is_unique);
  2084. }
  2085. /**
  2086. * set $goto_include variable for different cases and retrieve like,
  2087. * if $GLOBALS['goto'] empty, if $goto_include previously not defined
  2088. * and new_insert, same_insert, edit_next
  2089. *
  2090. * @param string $goto_include store some script for include, otherwise it is
  2091. * boolean false
  2092. *
  2093. * @return string $goto_include
  2094. */
  2095. public function getGotoInclude($goto_include)
  2096. {
  2097. $valid_options = array('new_insert', 'same_insert', 'edit_next');
  2098. if (isset($_REQUEST['after_insert'])
  2099. && in_array($_REQUEST['after_insert'], $valid_options)
  2100. ) {
  2101. $goto_include = 'tbl_change.php';
  2102. } elseif (! empty($GLOBALS['goto'])) {
  2103. if (! preg_match('@^[a-z_]+\.php$@', $GLOBALS['goto'])) {
  2104. // this should NOT happen
  2105. //$GLOBALS['goto'] = false;
  2106. $goto_include = false;
  2107. } else {
  2108. $goto_include = $GLOBALS['goto'];
  2109. }
  2110. if ($GLOBALS['goto'] == 'db_sql.php' && strlen($GLOBALS['table']) > 0) {
  2111. $GLOBALS['table'] = '';
  2112. }
  2113. }
  2114. if (! $goto_include) {
  2115. if (strlen($GLOBALS['table']) === 0) {
  2116. $goto_include = 'db_sql.php';
  2117. } else {
  2118. $goto_include = 'tbl_sql.php';
  2119. }
  2120. }
  2121. return $goto_include;
  2122. }
  2123. /**
  2124. * Defines the url to return in case of failure of the query
  2125. *
  2126. * @param array $url_params url parameters
  2127. *
  2128. * @return string error url for query failure
  2129. */
  2130. public function getErrorUrl(array $url_params)
  2131. {
  2132. if (isset($_REQUEST['err_url'])) {
  2133. return $_REQUEST['err_url'];
  2134. }
  2135. return 'tbl_change.php' . Url::getCommon($url_params);
  2136. }
  2137. /**
  2138. * Builds the sql query
  2139. *
  2140. * @param boolean $is_insertignore $_REQUEST['submit_type'] == 'insertignore'
  2141. * @param array $query_fields column names array
  2142. * @param array $value_sets array of query values
  2143. *
  2144. * @return array of query
  2145. */
  2146. public function buildSqlQuery($is_insertignore, array $query_fields, array $value_sets)
  2147. {
  2148. if ($is_insertignore) {
  2149. $insert_command = 'INSERT IGNORE ';
  2150. } else {
  2151. $insert_command = 'INSERT ';
  2152. }
  2153. $query = array(
  2154. $insert_command . 'INTO '
  2155. . Util::backquote($GLOBALS['table'])
  2156. . ' (' . implode(', ', $query_fields) . ') VALUES ('
  2157. . implode('), (', $value_sets) . ')'
  2158. );
  2159. unset($insert_command, $query_fields);
  2160. return $query;
  2161. }
  2162. /**
  2163. * Executes the sql query and get the result, then move back to the calling page
  2164. *
  2165. * @param array $url_params url parameters array
  2166. * @param array $query built query from buildSqlQuery()
  2167. *
  2168. * @return array $url_params, $total_affected_rows, $last_messages
  2169. * $warning_messages, $error_messages, $return_to_sql_query
  2170. */
  2171. public function executeSqlQuery(array $url_params, array $query)
  2172. {
  2173. $return_to_sql_query = '';
  2174. if (! empty($GLOBALS['sql_query'])) {
  2175. $url_params['sql_query'] = $GLOBALS['sql_query'];
  2176. $return_to_sql_query = $GLOBALS['sql_query'];
  2177. }
  2178. $GLOBALS['sql_query'] = implode('; ', $query) . ';';
  2179. // to ensure that the query is displayed in case of
  2180. // "insert as new row" and then "insert another new row"
  2181. $GLOBALS['display_query'] = $GLOBALS['sql_query'];
  2182. $total_affected_rows = 0;
  2183. $last_messages = array();
  2184. $warning_messages = array();
  2185. $error_messages = array();
  2186. foreach ($query as $single_query) {
  2187. if ($_REQUEST['submit_type'] == 'showinsert') {
  2188. $last_messages[] = Message::notice(__('Showing SQL query'));
  2189. continue;
  2190. }
  2191. if ($GLOBALS['cfg']['IgnoreMultiSubmitErrors']) {
  2192. $result = $this->dbi->tryQuery($single_query);
  2193. } else {
  2194. $result = $this->dbi->query($single_query);
  2195. }
  2196. if (! $result) {
  2197. $error_messages[] = $this->dbi->getError();
  2198. } else {
  2199. // The next line contains a real assignment, it's not a typo
  2200. if ($tmp = @$this->dbi->affectedRows()) {
  2201. $total_affected_rows += $tmp;
  2202. }
  2203. unset($tmp);
  2204. $insert_id = $this->dbi->insertId();
  2205. if ($insert_id != 0) {
  2206. // insert_id is id of FIRST record inserted in one insert, so if we
  2207. // inserted multiple rows, we had to increment this
  2208. if ($total_affected_rows > 0) {
  2209. $insert_id = $insert_id + $total_affected_rows - 1;
  2210. }
  2211. $last_message = Message::notice(__('Inserted row id: %1$d'));
  2212. $last_message->addParam($insert_id);
  2213. $last_messages[] = $last_message;
  2214. }
  2215. $this->dbi->freeResult($result);
  2216. }
  2217. $warning_messages = $this->getWarningMessages();
  2218. }
  2219. return array(
  2220. $url_params,
  2221. $total_affected_rows,
  2222. $last_messages,
  2223. $warning_messages,
  2224. $error_messages,
  2225. $return_to_sql_query
  2226. );
  2227. }
  2228. /**
  2229. * get the warning messages array
  2230. *
  2231. * @return array $warning_essages
  2232. */
  2233. private function getWarningMessages()
  2234. {
  2235. $warning_essages = array();
  2236. foreach ($this->dbi->getWarnings() as $warning) {
  2237. $warning_essages[] = Message::sanitize(
  2238. $warning['Level'] . ': #' . $warning['Code'] . ' ' . $warning['Message']
  2239. );
  2240. }
  2241. return $warning_essages;
  2242. }
  2243. /**
  2244. * Column to display from the foreign table?
  2245. *
  2246. * @param string $where_comparison string that contain relation field value
  2247. * @param array $map all Relations to foreign tables for a given
  2248. * table or optionally a given column in a table
  2249. * @param string $relation_field relation field
  2250. *
  2251. * @return string $dispval display value from the foreign table
  2252. */
  2253. public function getDisplayValueForForeignTableColumn(
  2254. $where_comparison,
  2255. array $map,
  2256. $relation_field
  2257. ) {
  2258. $foreigner = $this->relation->searchColumnInForeigners($map, $relation_field);
  2259. $display_field = $this->relation->getDisplayField(
  2260. $foreigner['foreign_db'],
  2261. $foreigner['foreign_table']
  2262. );
  2263. // Field to display from the foreign table?
  2264. if (isset($display_field) && strlen($display_field) > 0) {
  2265. $dispsql = 'SELECT ' . Util::backquote($display_field)
  2266. . ' FROM ' . Util::backquote($foreigner['foreign_db'])
  2267. . '.' . Util::backquote($foreigner['foreign_table'])
  2268. . ' WHERE ' . Util::backquote($foreigner['foreign_field'])
  2269. . $where_comparison;
  2270. $dispresult = $this->dbi->tryQuery(
  2271. $dispsql,
  2272. DatabaseInterface::CONNECT_USER,
  2273. DatabaseInterface::QUERY_STORE
  2274. );
  2275. if ($dispresult && $this->dbi->numRows($dispresult) > 0) {
  2276. list($dispval) = $this->dbi->fetchRow($dispresult, 0);
  2277. } else {
  2278. $dispval = '';
  2279. }
  2280. if ($dispresult) {
  2281. $this->dbi->freeResult($dispresult);
  2282. }
  2283. return $dispval;
  2284. }
  2285. return '';
  2286. }
  2287. /**
  2288. * Display option in the cell according to user choices
  2289. *
  2290. * @param array $map all Relations to foreign tables for a given
  2291. * table or optionally a given column in a table
  2292. * @param string $relation_field relation field
  2293. * @param string $where_comparison string that contain relation field value
  2294. * @param string $dispval display value from the foreign table
  2295. * @param string $relation_field_value relation field value
  2296. *
  2297. * @return string $output HTML <a> tag
  2298. */
  2299. public function getLinkForRelationalDisplayField(
  2300. array $map,
  2301. $relation_field,
  2302. $where_comparison,
  2303. $dispval,
  2304. $relation_field_value
  2305. ) {
  2306. $foreigner = $this->relation->searchColumnInForeigners($map, $relation_field);
  2307. if ('K' == $_SESSION['tmpval']['relational_display']) {
  2308. // user chose "relational key" in the display options, so
  2309. // the title contains the display field
  2310. $title = (! empty($dispval))
  2311. ? ' title="' . htmlspecialchars($dispval) . '"'
  2312. : '';
  2313. } else {
  2314. $title = ' title="' . htmlspecialchars($relation_field_value) . '"';
  2315. }
  2316. $_url_params = array(
  2317. 'db' => $foreigner['foreign_db'],
  2318. 'table' => $foreigner['foreign_table'],
  2319. 'pos' => '0',
  2320. 'sql_query' => 'SELECT * FROM '
  2321. . Util::backquote($foreigner['foreign_db'])
  2322. . '.' . Util::backquote($foreigner['foreign_table'])
  2323. . ' WHERE ' . Util::backquote($foreigner['foreign_field'])
  2324. . $where_comparison
  2325. );
  2326. $output = '<a href="sql.php'
  2327. . Url::getCommon($_url_params) . '"' . $title . '>';
  2328. if ('D' == $_SESSION['tmpval']['relational_display']) {
  2329. // user chose "relational display field" in the
  2330. // display options, so show display field in the cell
  2331. $output .= (!empty($dispval)) ? htmlspecialchars($dispval) : '';
  2332. } else {
  2333. // otherwise display data in the cell
  2334. $output .= htmlspecialchars($relation_field_value);
  2335. }
  2336. $output .= '</a>';
  2337. return $output;
  2338. }
  2339. /**
  2340. * Transform edited values
  2341. *
  2342. * @param string $db db name
  2343. * @param string $table table name
  2344. * @param array $transformation mimetypes for all columns of a table
  2345. * [field_name][field_key]
  2346. * @param array &$edited_values transform columns list and new values
  2347. * @param string $file file containing the transformation plugin
  2348. * @param string $column_name column name
  2349. * @param array $extra_data extra data array
  2350. * @param string $type the type of transformation
  2351. *
  2352. * @return array $extra_data
  2353. */
  2354. public function transformEditedValues(
  2355. $db,
  2356. $table,
  2357. array $transformation,
  2358. array &$edited_values,
  2359. $file,
  2360. $column_name,
  2361. array $extra_data,
  2362. $type
  2363. ) {
  2364. $include_file = 'libraries/classes/Plugins/Transformations/' . $file;
  2365. if (is_file($include_file)) {
  2366. include_once $include_file;
  2367. $_url_params = array(
  2368. 'db' => $db,
  2369. 'table' => $table,
  2370. 'where_clause' => $_REQUEST['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. /** @var TransformationsPlugin $transformation_plugin */
  2381. $transformation_plugin = new $class_name();
  2382. foreach ($edited_values as $cell_index => $curr_cell_edited_values) {
  2383. if (isset($curr_cell_edited_values[$column_name])) {
  2384. $edited_values[$cell_index][$column_name]
  2385. = $extra_data['transformations'][$cell_index]
  2386. = $transformation_plugin->applyTransformation(
  2387. $curr_cell_edited_values[$column_name],
  2388. $transform_options,
  2389. ''
  2390. );
  2391. }
  2392. } // end of loop for each transformation cell
  2393. }
  2394. return $extra_data;
  2395. }
  2396. /**
  2397. * Get current value in multi edit mode
  2398. *
  2399. * @param array $multi_edit_funcs multiple edit functions array
  2400. * @param array $multi_edit_salt multiple edit array with encryption salt
  2401. * @param array $gis_from_text_functions array that contains gis from text functions
  2402. * @param string $current_value current value in the column
  2403. * @param array $gis_from_wkb_functions initially $val is $multi_edit_columns[$key]
  2404. * @param array $func_optional_param array('RAND','UNIX_TIMESTAMP')
  2405. * @param array $func_no_param array of set of string
  2406. * @param string $key an md5 of the column name
  2407. *
  2408. * @return array $cur_value
  2409. */
  2410. public function getCurrentValueAsAnArrayForMultipleEdit(
  2411. $multi_edit_funcs,
  2412. $multi_edit_salt,
  2413. $gis_from_text_functions,
  2414. $current_value,
  2415. $gis_from_wkb_functions,
  2416. $func_optional_param,
  2417. $func_no_param,
  2418. $key
  2419. ) {
  2420. if (empty($multi_edit_funcs[$key])) {
  2421. return $current_value;
  2422. } elseif ('UUID' === $multi_edit_funcs[$key]) {
  2423. /* This way user will know what UUID new row has */
  2424. $uuid = $this->dbi->fetchValue('SELECT UUID()');
  2425. return "'" . $uuid . "'";
  2426. } elseif ((in_array($multi_edit_funcs[$key], $gis_from_text_functions)
  2427. && substr($current_value, 0, 3) == "'''")
  2428. || in_array($multi_edit_funcs[$key], $gis_from_wkb_functions)
  2429. ) {
  2430. // Remove enclosing apostrophes
  2431. $current_value = mb_substr($current_value, 1, -1);
  2432. // Remove escaping apostrophes
  2433. $current_value = str_replace("''", "'", $current_value);
  2434. return $multi_edit_funcs[$key] . '(' . $current_value . ')';
  2435. } elseif (! in_array($multi_edit_funcs[$key], $func_no_param)
  2436. || ($current_value != "''"
  2437. && in_array($multi_edit_funcs[$key], $func_optional_param))
  2438. ) {
  2439. if ((isset($multi_edit_salt[$key])
  2440. && ($multi_edit_funcs[$key] == "AES_ENCRYPT"
  2441. || $multi_edit_funcs[$key] == "AES_DECRYPT"))
  2442. || (! empty($multi_edit_salt[$key])
  2443. && ($multi_edit_funcs[$key] == "DES_ENCRYPT"
  2444. || $multi_edit_funcs[$key] == "DES_DECRYPT"
  2445. || $multi_edit_funcs[$key] == "ENCRYPT"))
  2446. ) {
  2447. return $multi_edit_funcs[$key] . '(' . $current_value . ",'"
  2448. . $this->dbi->escapeString($multi_edit_salt[$key]) . "')";
  2449. }
  2450. return $multi_edit_funcs[$key] . '(' . $current_value . ')';
  2451. }
  2452. return $multi_edit_funcs[$key] . '()';
  2453. }
  2454. /**
  2455. * Get query values array and query fields array for insert and update in multi edit
  2456. *
  2457. * @param array $multi_edit_columns_name multiple edit columns name array
  2458. * @param array $multi_edit_columns_null multiple edit columns null array
  2459. * @param string $current_value current value in the column in loop
  2460. * @param array $multi_edit_columns_prev multiple edit previous columns array
  2461. * @param array $multi_edit_funcs multiple edit functions array
  2462. * @param boolean $is_insert boolean value whether insert or not
  2463. * @param array $query_values SET part of the sql query
  2464. * @param array $query_fields array of query fields
  2465. * @param string $current_value_as_an_array current value in the column
  2466. * as an array
  2467. * @param array $value_sets array of valu sets
  2468. * @param string $key an md5 of the column name
  2469. * @param array $multi_edit_columns_null_prev array of multiple edit columns
  2470. * null previous
  2471. *
  2472. * @return array ($query_values, $query_fields)
  2473. */
  2474. public function getQueryValuesForInsertAndUpdateInMultipleEdit(
  2475. $multi_edit_columns_name,
  2476. $multi_edit_columns_null,
  2477. $current_value,
  2478. $multi_edit_columns_prev,
  2479. $multi_edit_funcs,
  2480. $is_insert,
  2481. $query_values,
  2482. $query_fields,
  2483. $current_value_as_an_array,
  2484. $value_sets,
  2485. $key,
  2486. $multi_edit_columns_null_prev
  2487. ) {
  2488. // i n s e r t
  2489. if ($is_insert) {
  2490. // no need to add column into the valuelist
  2491. if (strlen($current_value_as_an_array) > 0) {
  2492. $query_values[] = $current_value_as_an_array;
  2493. // first inserted row so prepare the list of fields
  2494. if (empty($value_sets)) {
  2495. $query_fields[] = Util::backquote(
  2496. $multi_edit_columns_name[$key]
  2497. );
  2498. }
  2499. }
  2500. } elseif (! empty($multi_edit_columns_null_prev[$key])
  2501. && ! isset($multi_edit_columns_null[$key])
  2502. ) {
  2503. // u p d a t e
  2504. // field had the null checkbox before the update
  2505. // field no longer has the null checkbox
  2506. $query_values[]
  2507. = Util::backquote($multi_edit_columns_name[$key])
  2508. . ' = ' . $current_value_as_an_array;
  2509. } elseif (empty($multi_edit_funcs[$key])
  2510. && isset($multi_edit_columns_prev[$key])
  2511. && (("'" . $this->dbi->escapeString($multi_edit_columns_prev[$key]) . "'" === $current_value)
  2512. || ('0x' . $multi_edit_columns_prev[$key] === $current_value))
  2513. ) {
  2514. // No change for this column and no MySQL function is used -> next column
  2515. } elseif (! empty($current_value)) {
  2516. // avoid setting a field to NULL when it's already NULL
  2517. // (field had the null checkbox before the update
  2518. // field still has the null checkbox)
  2519. if (empty($multi_edit_columns_null_prev[$key])
  2520. || empty($multi_edit_columns_null[$key])
  2521. ) {
  2522. $query_values[]
  2523. = Util::backquote($multi_edit_columns_name[$key])
  2524. . ' = ' . $current_value_as_an_array;
  2525. }
  2526. }
  2527. return array($query_values, $query_fields);
  2528. }
  2529. /**
  2530. * Get the current column value in the form for different data types
  2531. *
  2532. * @param string|false $possibly_uploaded_val uploaded file content
  2533. * @param string $key an md5 of the column name
  2534. * @param array $multi_edit_columns_type array of multi edit column types
  2535. * @param string $current_value current column value in the form
  2536. * @param array $multi_edit_auto_increment multi edit auto increment
  2537. * @param integer $rownumber index of where clause array
  2538. * @param array $multi_edit_columns_name multi edit column names array
  2539. * @param array $multi_edit_columns_null multi edit columns null array
  2540. * @param array $multi_edit_columns_null_prev multi edit columns previous null
  2541. * @param boolean $is_insert whether insert or not
  2542. * @param boolean $using_key whether editing or new row
  2543. * @param string $where_clause where clause
  2544. * @param string $table table name
  2545. * @param array $multi_edit_funcs multiple edit functions array
  2546. *
  2547. * @return string $current_value current column value in the form
  2548. */
  2549. public function getCurrentValueForDifferentTypes(
  2550. $possibly_uploaded_val,
  2551. $key,
  2552. $multi_edit_columns_type,
  2553. $current_value,
  2554. $multi_edit_auto_increment,
  2555. $rownumber,
  2556. $multi_edit_columns_name,
  2557. $multi_edit_columns_null,
  2558. $multi_edit_columns_null_prev,
  2559. $is_insert,
  2560. $using_key,
  2561. $where_clause,
  2562. $table,
  2563. $multi_edit_funcs
  2564. ) {
  2565. // Fetch the current values of a row to use in case we have a protected field
  2566. if ($is_insert
  2567. && $using_key && isset($multi_edit_columns_type)
  2568. && is_array($multi_edit_columns_type) && !empty($where_clause)
  2569. ) {
  2570. $protected_row = $this->dbi->fetchSingleRow(
  2571. 'SELECT * FROM ' . Util::backquote($table)
  2572. . ' WHERE ' . $where_clause . ';'
  2573. );
  2574. }
  2575. if (false !== $possibly_uploaded_val) {
  2576. $current_value = $possibly_uploaded_val;
  2577. } elseif (! empty($multi_edit_funcs[$key])) {
  2578. $current_value = "'" . $this->dbi->escapeString($current_value)
  2579. . "'";
  2580. } else {
  2581. // c o l u m n v a l u e i n t h e f o r m
  2582. if (isset($multi_edit_columns_type[$key])) {
  2583. $type = $multi_edit_columns_type[$key];
  2584. } else {
  2585. $type = '';
  2586. }
  2587. if ($type != 'protected' && $type != 'set' && strlen($current_value) === 0) {
  2588. // best way to avoid problems in strict mode
  2589. // (works also in non-strict mode)
  2590. if (isset($multi_edit_auto_increment)
  2591. && isset($multi_edit_auto_increment[$key])
  2592. ) {
  2593. $current_value = 'NULL';
  2594. } else {
  2595. $current_value = "''";
  2596. }
  2597. } elseif ($type == 'set') {
  2598. if (! empty($_REQUEST['fields']['multi_edit'][$rownumber][$key])) {
  2599. $current_value = implode(
  2600. ',',
  2601. $_REQUEST['fields']['multi_edit'][$rownumber][$key]
  2602. );
  2603. $current_value = "'"
  2604. . $this->dbi->escapeString($current_value) . "'";
  2605. } else {
  2606. $current_value = "''";
  2607. }
  2608. } elseif ($type == 'protected') {
  2609. // here we are in protected mode (asked in the config)
  2610. // so tbl_change has put this special value in the
  2611. // columns array, so we do not change the column value
  2612. // but we can still handle column upload
  2613. // when in UPDATE mode, do not alter field's contents. When in INSERT
  2614. // mode, insert empty field because no values were submitted.
  2615. // If protected blobs where set, insert original fields content.
  2616. if (! empty($protected_row[$multi_edit_columns_name[$key]])) {
  2617. $current_value = '0x'
  2618. . bin2hex($protected_row[$multi_edit_columns_name[$key]]);
  2619. } else {
  2620. $current_value = '';
  2621. }
  2622. } elseif ($type === 'hex') {
  2623. if (substr($current_value, 0, 2) != '0x') {
  2624. $current_value = '0x' . $current_value;
  2625. }
  2626. } elseif ($type == 'bit') {
  2627. $current_value = preg_replace('/[^01]/', '0', $current_value);
  2628. $current_value = "b'" . $this->dbi->escapeString($current_value)
  2629. . "'";
  2630. } elseif (! ($type == 'datetime' || $type == 'timestamp')
  2631. || ($current_value != 'CURRENT_TIMESTAMP'
  2632. && $current_value != 'current_timestamp()')
  2633. ) {
  2634. $current_value = "'" . $this->dbi->escapeString($current_value)
  2635. . "'";
  2636. }
  2637. // Was the Null checkbox checked for this field?
  2638. // (if there is a value, we ignore the Null checkbox: this could
  2639. // be possible if Javascript is disabled in the browser)
  2640. if (! empty($multi_edit_columns_null[$key])
  2641. && ($current_value == "''" || $current_value == '')
  2642. ) {
  2643. $current_value = 'NULL';
  2644. }
  2645. // The Null checkbox was unchecked for this field
  2646. if (empty($current_value)
  2647. && ! empty($multi_edit_columns_null_prev[$key])
  2648. && ! isset($multi_edit_columns_null[$key])
  2649. ) {
  2650. $current_value = "''";
  2651. }
  2652. } // end else (column value in the form)
  2653. return $current_value;
  2654. }
  2655. /**
  2656. * Check whether inline edited value can be truncated or not,
  2657. * and add additional parameters for extra_data array if needed
  2658. *
  2659. * @param string $db Database name
  2660. * @param string $table Table name
  2661. * @param string $column_name Column name
  2662. * @param array &$extra_data Extra data for ajax response
  2663. *
  2664. * @return void
  2665. */
  2666. public function verifyWhetherValueCanBeTruncatedAndAppendExtraData(
  2667. $db,
  2668. $table,
  2669. $column_name,
  2670. array &$extra_data
  2671. ) {
  2672. $extra_data['isNeedToRecheck'] = false;
  2673. $sql_for_real_value = 'SELECT ' . Util::backquote($table) . '.'
  2674. . Util::backquote($column_name)
  2675. . ' FROM ' . Util::backquote($db) . '.'
  2676. . Util::backquote($table)
  2677. . ' WHERE ' . $_REQUEST['where_clause'][0];
  2678. $result = $this->dbi->tryQuery($sql_for_real_value);
  2679. $fields_meta = $this->dbi->getFieldsMeta($result);
  2680. $meta = $fields_meta[0];
  2681. if ($row = $this->dbi->fetchRow($result)) {
  2682. $new_value = $row[0];
  2683. if ((substr($meta->type, 0, 9) == 'timestamp')
  2684. || ($meta->type == 'datetime')
  2685. || ($meta->type == 'time')
  2686. ) {
  2687. $new_value = Util::addMicroseconds($new_value);
  2688. } elseif (mb_strpos($meta->flags, 'binary') !== false) {
  2689. $new_value = '0x' . bin2hex($new_value);
  2690. }
  2691. $extra_data['isNeedToRecheck'] = true;
  2692. $extra_data['truncatableFieldValue'] = $new_value;
  2693. }
  2694. $this->dbi->freeResult($result);
  2695. }
  2696. /**
  2697. * Function to get the columns of a table
  2698. *
  2699. * @param string $db current db
  2700. * @param string $table current table
  2701. *
  2702. * @return array
  2703. */
  2704. public function getTableColumns($db, $table)
  2705. {
  2706. $this->dbi->selectDb($db);
  2707. return array_values($this->dbi->getColumns($db, $table, null, true));
  2708. }
  2709. /**
  2710. * Function to determine Insert/Edit rows
  2711. *
  2712. * @param string $where_clause where clause
  2713. * @param string $db current database
  2714. * @param string $table current table
  2715. *
  2716. * @return mixed
  2717. */
  2718. public function determineInsertOrEdit($where_clause, $db, $table)
  2719. {
  2720. if (isset($_REQUEST['where_clause'])) {
  2721. $where_clause = $_REQUEST['where_clause'];
  2722. }
  2723. if (isset($_SESSION['edit_next'])) {
  2724. $where_clause = $_SESSION['edit_next'];
  2725. unset($_SESSION['edit_next']);
  2726. $after_insert = 'edit_next';
  2727. }
  2728. if (isset($_REQUEST['ShowFunctionFields'])) {
  2729. $GLOBALS['cfg']['ShowFunctionFields'] = $_REQUEST['ShowFunctionFields'];
  2730. }
  2731. if (isset($_REQUEST['ShowFieldTypesInDataEditView'])) {
  2732. $GLOBALS['cfg']['ShowFieldTypesInDataEditView']
  2733. = $_REQUEST['ShowFieldTypesInDataEditView'];
  2734. }
  2735. if (isset($_REQUEST['after_insert'])) {
  2736. $after_insert = $_REQUEST['after_insert'];
  2737. }
  2738. if (isset($where_clause)) {
  2739. // we are editing
  2740. $insert_mode = false;
  2741. $where_clause_array = $this->getWhereClauseArray($where_clause);
  2742. list($where_clauses, $result, $rows, $found_unique_key)
  2743. = $this->analyzeWhereClauses(
  2744. $where_clause_array,
  2745. $table,
  2746. $db
  2747. );
  2748. } else {
  2749. // we are inserting
  2750. $insert_mode = true;
  2751. $where_clause = null;
  2752. list($result, $rows) = $this->loadFirstRow($table, $db);
  2753. $where_clauses = null;
  2754. $where_clause_array = array();
  2755. $found_unique_key = false;
  2756. }
  2757. // Copying a row - fetched data will be inserted as a new row,
  2758. // therefore the where clause is needless.
  2759. if (isset($_REQUEST['default_action'])
  2760. && $_REQUEST['default_action'] === 'insert'
  2761. ) {
  2762. $where_clause = $where_clauses = null;
  2763. }
  2764. return array(
  2765. $insert_mode, $where_clause, $where_clause_array, $where_clauses,
  2766. $result, $rows, $found_unique_key,
  2767. isset($after_insert) ? $after_insert : null
  2768. );
  2769. }
  2770. /**
  2771. * Function to get comments for the table columns
  2772. *
  2773. * @param string $db current database
  2774. * @param string $table current table
  2775. *
  2776. * @return array $comments_map comments for columns
  2777. */
  2778. public function getCommentsMap($db, $table)
  2779. {
  2780. $comments_map = array();
  2781. if ($GLOBALS['cfg']['ShowPropertyComments']) {
  2782. $comments_map = $this->relation->getComments($db, $table);
  2783. }
  2784. return $comments_map;
  2785. }
  2786. /**
  2787. * Function to get URL parameters
  2788. *
  2789. * @param string $db current database
  2790. * @param string $table current table
  2791. *
  2792. * @return array $url_params url parameters
  2793. */
  2794. public function getUrlParameters($db, $table)
  2795. {
  2796. /**
  2797. * @todo check if we could replace by "db_|tbl_" - please clarify!?
  2798. */
  2799. $url_params = array(
  2800. 'db' => $db,
  2801. 'sql_query' => $_POST['sql_query']
  2802. );
  2803. if (preg_match('@^tbl_@', $GLOBALS['goto'])) {
  2804. $url_params['table'] = $table;
  2805. }
  2806. return $url_params;
  2807. }
  2808. /**
  2809. * Function to get html for the gis editor div
  2810. *
  2811. * @return string
  2812. */
  2813. public function getHtmlForGisEditor()
  2814. {
  2815. return '<div id="gis_editor"></div>'
  2816. . '<div id="popup_background"></div>'
  2817. . '<br />';
  2818. }
  2819. /**
  2820. * Function to get html for the ignore option in insert mode
  2821. *
  2822. * @param int $row_id row id
  2823. * @param bool $checked ignore option is checked or not
  2824. *
  2825. * @return string
  2826. */
  2827. public function getHtmlForIgnoreOption($row_id, $checked = true)
  2828. {
  2829. return '<input type="checkbox"'
  2830. . ($checked ? ' checked="checked"' : '')
  2831. . ' name="insert_ignore_' . $row_id . '"'
  2832. . ' id="insert_ignore_' . $row_id . '" />'
  2833. . '<label for="insert_ignore_' . $row_id . '">'
  2834. . __('Ignore')
  2835. . '</label><br />' . "\n";
  2836. }
  2837. /**
  2838. * Function to get html for the function option
  2839. *
  2840. * @param array $column column
  2841. * @param string $column_name_appendix column name appendix
  2842. *
  2843. * @return String
  2844. */
  2845. private function getHtmlForFunctionOption(array $column, $column_name_appendix)
  2846. {
  2847. return '<tr class="noclick">'
  2848. . '<td '
  2849. . 'class="center">'
  2850. . $column['Field_title']
  2851. . '<input type="hidden" name="fields_name' . $column_name_appendix
  2852. . '" value="' . $column['Field_html'] . '"/>'
  2853. . '</td>';
  2854. }
  2855. /**
  2856. * Function to get html for the column type
  2857. *
  2858. * @param array $column column
  2859. *
  2860. * @return string
  2861. */
  2862. private function getHtmlForInsertEditColumnType(array $column)
  2863. {
  2864. return '<td class="center' . $column['wrap'] . '">'
  2865. . '<span class="column_type" dir="ltr">' . $column['pma_type'] . '</span>'
  2866. . '</td>';
  2867. }
  2868. /**
  2869. * Function to get html for the insert edit form header
  2870. *
  2871. * @param bool $has_blob_field whether has blob field
  2872. * @param bool $is_upload whether is upload
  2873. *
  2874. * @return string
  2875. */
  2876. public function getHtmlForInsertEditFormHeader($has_blob_field, $is_upload)
  2877. {
  2878. $html_output ='<form id="insertForm" class="lock-page ';
  2879. if ($has_blob_field && $is_upload) {
  2880. $html_output .='disableAjax';
  2881. }
  2882. $html_output .='" method="post" action="tbl_replace.php" name="insertForm" ';
  2883. if ($is_upload) {
  2884. $html_output .= ' enctype="multipart/form-data"';
  2885. }
  2886. $html_output .= '>';
  2887. return $html_output;
  2888. }
  2889. /**
  2890. * Function to get html for each insert/edit column
  2891. *
  2892. * @param array $table_columns table columns
  2893. * @param int $column_number column index in table_columns
  2894. * @param array $comments_map comments map
  2895. * @param bool $timestamp_seen whether timestamp seen
  2896. * @param array $current_result current result
  2897. * @param string $chg_evt_handler javascript change event handler
  2898. * @param string $jsvkey javascript validation key
  2899. * @param string $vkey validation key
  2900. * @param bool $insert_mode whether insert mode
  2901. * @param array $current_row current row
  2902. * @param int &$o_rows row offset
  2903. * @param int &$tabindex tab index
  2904. * @param int $columns_cnt columns count
  2905. * @param bool $is_upload whether upload
  2906. * @param int $tabindex_for_function tab index offset for function
  2907. * @param array $foreigners foreigners
  2908. * @param int $tabindex_for_null tab index offset for null
  2909. * @param int $tabindex_for_value tab index offset for value
  2910. * @param string $table table
  2911. * @param string $db database
  2912. * @param int $row_id row id
  2913. * @param array $titles titles
  2914. * @param int $biggest_max_file_size biggest max file size
  2915. * @param string $default_char_editing default char editing mode which is stored
  2916. * in the config.inc.php script
  2917. * @param string $text_dir text direction
  2918. * @param array $repopulate the data to be repopulated
  2919. * @param array $column_mime the mime information of column
  2920. * @param string $where_clause the where clause
  2921. *
  2922. * @return string
  2923. */
  2924. private function getHtmlForInsertEditFormColumn(
  2925. array $table_columns,
  2926. $column_number,
  2927. array $comments_map,
  2928. $timestamp_seen,
  2929. $current_result,
  2930. $chg_evt_handler,
  2931. $jsvkey,
  2932. $vkey,
  2933. $insert_mode,
  2934. array $current_row,
  2935. &$o_rows,
  2936. &$tabindex,
  2937. $columns_cnt,
  2938. $is_upload,
  2939. $tabindex_for_function,
  2940. array $foreigners,
  2941. $tabindex_for_null,
  2942. $tabindex_for_value,
  2943. $table,
  2944. $db,
  2945. $row_id,
  2946. array $titles,
  2947. $biggest_max_file_size,
  2948. $default_char_editing,
  2949. $text_dir,
  2950. array $repopulate,
  2951. array $column_mime,
  2952. $where_clause
  2953. ) {
  2954. $column = $table_columns[$column_number];
  2955. $readOnly = false;
  2956. if (! $this->userHasColumnPrivileges($column, $insert_mode)) {
  2957. $readOnly = true;
  2958. }
  2959. if (! isset($column['processed'])) {
  2960. $column = $this->analyzeTableColumnsArray(
  2961. $column,
  2962. $comments_map,
  2963. $timestamp_seen
  2964. );
  2965. }
  2966. $as_is = false;
  2967. if (!empty($repopulate) && !empty($current_row)) {
  2968. $current_row[$column['Field']] = $repopulate[$column['Field_md5']];
  2969. $as_is = true;
  2970. }
  2971. $extracted_columnspec
  2972. = Util::extractColumnSpec($column['Type']);
  2973. if (-1 === $column['len']) {
  2974. $column['len'] = $this->dbi->fieldLen(
  2975. $current_result,
  2976. $column_number
  2977. );
  2978. // length is unknown for geometry fields,
  2979. // make enough space to edit very simple WKTs
  2980. if (-1 === $column['len']) {
  2981. $column['len'] = 30;
  2982. }
  2983. }
  2984. //Call validation when the form submitted...
  2985. $onChangeClause = $chg_evt_handler
  2986. . "=\"return verificationsAfterFieldChange('"
  2987. . Sanitize::escapeJsString($column['Field_md5']) . "', '"
  2988. . Sanitize::escapeJsString($jsvkey) . "','" . $column['pma_type'] . "')\"";
  2989. // Use an MD5 as an array index to avoid having special characters
  2990. // in the name attribute (see bug #1746964 )
  2991. $column_name_appendix = $vkey . '[' . $column['Field_md5'] . ']';
  2992. if ($column['Type'] === 'datetime'
  2993. && ! isset($column['Default'])
  2994. && ! is_null($column['Default'])
  2995. && $insert_mode
  2996. ) {
  2997. $column['Default'] = date('Y-m-d H:i:s', time());
  2998. }
  2999. $html_output = $this->getHtmlForFunctionOption(
  3000. $column,
  3001. $column_name_appendix
  3002. );
  3003. if ($GLOBALS['cfg']['ShowFieldTypesInDataEditView']) {
  3004. $html_output .= $this->getHtmlForInsertEditColumnType($column);
  3005. } //End if
  3006. // Get a list of GIS data types.
  3007. $gis_data_types = Util::getGISDatatypes();
  3008. // Prepares the field value
  3009. $real_null_value = false;
  3010. $special_chars_encoded = '';
  3011. if (!empty($current_row)) {
  3012. // (we are editing)
  3013. list(
  3014. $real_null_value, $special_chars_encoded, $special_chars,
  3015. $data, $backup_field
  3016. )
  3017. = $this->getSpecialCharsAndBackupFieldForExistingRow(
  3018. $current_row,
  3019. $column,
  3020. $extracted_columnspec,
  3021. $real_null_value,
  3022. $gis_data_types,
  3023. $column_name_appendix,
  3024. $as_is
  3025. );
  3026. } else {
  3027. // (we are inserting)
  3028. // display default values
  3029. $tmp = $column;
  3030. if (isset($repopulate[$column['Field_md5']])) {
  3031. $tmp['Default'] = $repopulate[$column['Field_md5']];
  3032. }
  3033. list($real_null_value, $data, $special_chars, $backup_field,
  3034. $special_chars_encoded
  3035. )
  3036. = $this->getSpecialCharsAndBackupFieldForInsertingMode(
  3037. $tmp,
  3038. $real_null_value
  3039. );
  3040. unset($tmp);
  3041. }
  3042. $idindex = ($o_rows * $columns_cnt) + $column_number + 1;
  3043. $tabindex = $idindex;
  3044. // Get a list of data types that are not yet supported.
  3045. $no_support_types = Util::unsupportedDatatypes();
  3046. // The function column
  3047. // -------------------
  3048. $foreignData = $this->relation->getForeignData(
  3049. $foreigners,
  3050. $column['Field'],
  3051. false,
  3052. '',
  3053. ''
  3054. );
  3055. if ($GLOBALS['cfg']['ShowFunctionFields']) {
  3056. $html_output .= $this->getFunctionColumn(
  3057. $column,
  3058. $is_upload,
  3059. $column_name_appendix,
  3060. $onChangeClause,
  3061. $no_support_types,
  3062. $tabindex_for_function,
  3063. $tabindex,
  3064. $idindex,
  3065. $insert_mode,
  3066. $readOnly,
  3067. $foreignData
  3068. );
  3069. }
  3070. // The null column
  3071. // ---------------
  3072. $html_output .= $this->getNullColumn(
  3073. $column,
  3074. $column_name_appendix,
  3075. $real_null_value,
  3076. $tabindex,
  3077. $tabindex_for_null,
  3078. $idindex,
  3079. $vkey,
  3080. $foreigners,
  3081. $foreignData,
  3082. $readOnly
  3083. );
  3084. // The value column (depends on type)
  3085. // ----------------
  3086. // See bug #1667887 for the reason why we don't use the maxlength
  3087. // HTML attribute
  3088. //add data attributes "no of decimals" and "data type"
  3089. $no_decimals = 0;
  3090. $type = current(explode("(", $column['pma_type']));
  3091. if (preg_match('/\(([^()]+)\)/', $column['pma_type'], $match)) {
  3092. $match[0] = trim($match[0], '()');
  3093. $no_decimals = $match[0];
  3094. }
  3095. $html_output .= '<td' . ' data-type="' . $type . '"' . ' data-decimals="'
  3096. . $no_decimals . '">' . "\n";
  3097. // Will be used by js/tbl_change.js to set the default value
  3098. // for the "Continue insertion" feature
  3099. $html_output .= '<span class="default_value hide">'
  3100. . $special_chars . '</span>';
  3101. // Check input transformation of column
  3102. $transformed_html = '';
  3103. if (!empty($column_mime['input_transformation'])) {
  3104. $file = $column_mime['input_transformation'];
  3105. $include_file = 'libraries/classes/Plugins/Transformations/' . $file;
  3106. if (is_file($include_file)) {
  3107. include_once $include_file;
  3108. $class_name = Transformations::getClassName($include_file);
  3109. $transformation_plugin = new $class_name();
  3110. $transformation_options = Transformations::getOptions(
  3111. $column_mime['input_transformation_options']
  3112. );
  3113. $_url_params = array(
  3114. 'db' => $db,
  3115. 'table' => $table,
  3116. 'transform_key' => $column['Field'],
  3117. 'where_clause' => $where_clause
  3118. );
  3119. $transformation_options['wrapper_link']
  3120. = Url::getCommon($_url_params);
  3121. $current_value = '';
  3122. if (isset($current_row[$column['Field']])) {
  3123. $current_value = $current_row[$column['Field']];
  3124. }
  3125. if (method_exists($transformation_plugin, 'getInputHtml')) {
  3126. $transformed_html = $transformation_plugin->getInputHtml(
  3127. $column,
  3128. $row_id,
  3129. $column_name_appendix,
  3130. $transformation_options,
  3131. $current_value,
  3132. $text_dir,
  3133. $tabindex,
  3134. $tabindex_for_value,
  3135. $idindex
  3136. );
  3137. }
  3138. if (method_exists($transformation_plugin, 'getScripts')) {
  3139. $GLOBALS['plugin_scripts'] = array_merge(
  3140. $GLOBALS['plugin_scripts'],
  3141. $transformation_plugin->getScripts()
  3142. );
  3143. }
  3144. }
  3145. }
  3146. if (!empty($transformed_html)) {
  3147. $html_output .= $transformed_html;
  3148. } else {
  3149. $html_output .= $this->getValueColumn(
  3150. $column,
  3151. $backup_field,
  3152. $column_name_appendix,
  3153. $onChangeClause,
  3154. $tabindex,
  3155. $tabindex_for_value,
  3156. $idindex,
  3157. $data,
  3158. $special_chars,
  3159. $foreignData,
  3160. array($table, $db),
  3161. $row_id,
  3162. $titles,
  3163. $text_dir,
  3164. $special_chars_encoded,
  3165. $vkey,
  3166. $is_upload,
  3167. $biggest_max_file_size,
  3168. $default_char_editing,
  3169. $no_support_types,
  3170. $gis_data_types,
  3171. $extracted_columnspec,
  3172. $readOnly
  3173. );
  3174. }
  3175. return $html_output;
  3176. }
  3177. /**
  3178. * Function to get html for each insert/edit row
  3179. *
  3180. * @param array $url_params url parameters
  3181. * @param array $table_columns table columns
  3182. * @param array $comments_map comments map
  3183. * @param bool $timestamp_seen whether timestamp seen
  3184. * @param array $current_result current result
  3185. * @param string $chg_evt_handler javascript change event handler
  3186. * @param string $jsvkey javascript validation key
  3187. * @param string $vkey validation key
  3188. * @param bool $insert_mode whether insert mode
  3189. * @param array $current_row current row
  3190. * @param int &$o_rows row offset
  3191. * @param int &$tabindex tab index
  3192. * @param int $columns_cnt columns count
  3193. * @param bool $is_upload whether upload
  3194. * @param int $tabindex_for_function tab index offset for function
  3195. * @param array $foreigners foreigners
  3196. * @param int $tabindex_for_null tab index offset for null
  3197. * @param int $tabindex_for_value tab index offset for value
  3198. * @param string $table table
  3199. * @param string $db database
  3200. * @param int $row_id row id
  3201. * @param array $titles titles
  3202. * @param int $biggest_max_file_size biggest max file size
  3203. * @param string $text_dir text direction
  3204. * @param array $repopulate the data to be repopulated
  3205. * @param array $where_clause_array the array of where clauses
  3206. *
  3207. * @return string
  3208. */
  3209. public function getHtmlForInsertEditRow(
  3210. array $url_params,
  3211. array $table_columns,
  3212. array $comments_map,
  3213. $timestamp_seen,
  3214. $current_result,
  3215. $chg_evt_handler,
  3216. $jsvkey,
  3217. $vkey,
  3218. $insert_mode,
  3219. array $current_row,
  3220. &$o_rows,
  3221. &$tabindex,
  3222. $columns_cnt,
  3223. $is_upload,
  3224. $tabindex_for_function,
  3225. array $foreigners,
  3226. $tabindex_for_null,
  3227. $tabindex_for_value,
  3228. $table,
  3229. $db,
  3230. $row_id,
  3231. array $titles,
  3232. $biggest_max_file_size,
  3233. $text_dir,
  3234. array $repopulate,
  3235. array $where_clause_array
  3236. ) {
  3237. $html_output = $this->getHeadAndFootOfInsertRowTable($url_params)
  3238. . '<tbody>';
  3239. //store the default value for CharEditing
  3240. $default_char_editing = $GLOBALS['cfg']['CharEditing'];
  3241. $mime_map = Transformations::getMIME($db, $table);
  3242. $where_clause = '';
  3243. if (isset($where_clause_array[$row_id])) {
  3244. $where_clause = $where_clause_array[$row_id];
  3245. }
  3246. for ($column_number = 0; $column_number < $columns_cnt; $column_number++) {
  3247. $table_column = $table_columns[$column_number];
  3248. $column_mime = array();
  3249. if (isset($mime_map[$table_column['Field']])) {
  3250. $column_mime = $mime_map[$table_column['Field']];
  3251. }
  3252. $html_output .= $this->getHtmlForInsertEditFormColumn(
  3253. $table_columns,
  3254. $column_number,
  3255. $comments_map,
  3256. $timestamp_seen,
  3257. $current_result,
  3258. $chg_evt_handler,
  3259. $jsvkey,
  3260. $vkey,
  3261. $insert_mode,
  3262. $current_row,
  3263. $o_rows,
  3264. $tabindex,
  3265. $columns_cnt,
  3266. $is_upload,
  3267. $tabindex_for_function,
  3268. $foreigners,
  3269. $tabindex_for_null,
  3270. $tabindex_for_value,
  3271. $table,
  3272. $db,
  3273. $row_id,
  3274. $titles,
  3275. $biggest_max_file_size,
  3276. $default_char_editing,
  3277. $text_dir,
  3278. $repopulate,
  3279. $column_mime,
  3280. $where_clause
  3281. );
  3282. } // end for
  3283. $o_rows++;
  3284. $html_output .= ' </tbody>'
  3285. . '</table></div><br />'
  3286. . '<div class="clearfloat"></div>';
  3287. return $html_output;
  3288. }
  3289. /**
  3290. * Returns whether the user has necessary insert/update privileges for the column
  3291. *
  3292. * @param array $table_column array of column details
  3293. * @param bool $insert_mode whether on insert mode
  3294. *
  3295. * @return boolean whether user has necessary privileges
  3296. */
  3297. private function userHasColumnPrivileges(array $table_column, $insert_mode)
  3298. {
  3299. $privileges = $table_column['Privileges'];
  3300. return ($insert_mode && strstr($privileges, 'insert') !== false)
  3301. || (! $insert_mode && strstr($privileges, 'update') !== false);
  3302. }
  3303. }