rte_routines.lib.php 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Functions for routine management.
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Sets required globals
  13. *
  14. * @return nothing
  15. */
  16. function PMA_RTN_setGlobals()
  17. {
  18. global $param_directions, $param_opts_num, $param_sqldataaccess;
  19. $param_directions = array('IN',
  20. 'OUT',
  21. 'INOUT');
  22. $param_opts_num = array('UNSIGNED',
  23. 'ZEROFILL',
  24. 'UNSIGNED ZEROFILL');
  25. $param_sqldataaccess = array('NO SQL',
  26. 'CONTAINS SQL',
  27. 'READS SQL DATA',
  28. 'MODIFIES SQL DATA');
  29. }
  30. /**
  31. * Main function for the routines functionality
  32. *
  33. * @return nothing
  34. */
  35. function PMA_RTN_main()
  36. {
  37. global $db;
  38. PMA_RTN_setGlobals();
  39. /**
  40. * Process all requests
  41. */
  42. PMA_RTN_handleEditor();
  43. PMA_RTN_handleExecute();
  44. PMA_RTN_handleExport();
  45. /**
  46. * Display a list of available routines
  47. */
  48. $columns = "`SPECIFIC_NAME`, `ROUTINE_NAME`, `ROUTINE_TYPE`, ";
  49. $columns .= "`DTD_IDENTIFIER`, `ROUTINE_DEFINITION`";
  50. $where = "ROUTINE_SCHEMA='" . PMA_Util::sqlAddSlashes($db) . "'";
  51. $items = PMA_DBI_fetch_result(
  52. "SELECT $columns FROM `INFORMATION_SCHEMA`.`ROUTINES` WHERE $where;"
  53. );
  54. echo PMA_RTE_getList('routine', $items);
  55. /**
  56. * Display the form for adding a new routine, if the user has the privileges.
  57. */
  58. echo PMA_RTN_getFooterLinks();
  59. /**
  60. * Display a warning for users with PHP's old "mysql" extension.
  61. */
  62. if ($GLOBALS['cfg']['Server']['extension'] === 'mysql') {
  63. trigger_error(
  64. __(
  65. 'You are using PHP\'s deprecated \'mysql\' extension, '
  66. . 'which is not capable of handling multi queries. '
  67. . '[strong]The execution of some stored routines may fail![/strong] '
  68. . 'Please use the improved \'mysqli\' extension to '
  69. . 'avoid any problems.'
  70. ),
  71. E_USER_WARNING
  72. );
  73. }
  74. } // end PMA_RTN_main()
  75. /**
  76. * This function parses a string containing one parameter of a routine,
  77. * as returned by PMA_RTN_parseAllParameters() and returns an array containing
  78. * the information about this parameter.
  79. *
  80. * @param string $value A string containing one parameter of a routine
  81. *
  82. * @return array Parsed information about the input parameter
  83. */
  84. function PMA_RTN_parseOneParameter($value)
  85. {
  86. global $param_directions;
  87. $retval = array(0 => '',
  88. 1 => '',
  89. 2 => '',
  90. 3 => '',
  91. 4 => '');
  92. $parsed_param = PMA_SQP_parse($value);
  93. $pos = 0;
  94. if (in_array(strtoupper($parsed_param[$pos]['data']), $param_directions)) {
  95. $retval[0] = strtoupper($parsed_param[0]['data']);
  96. $pos++;
  97. }
  98. if ($parsed_param[$pos]['type'] == 'alpha_identifier'
  99. || $parsed_param[$pos]['type'] == 'quote_backtick'
  100. ) {
  101. $retval[1] = PMA_Util::unQuote(
  102. $parsed_param[$pos]['data']
  103. );
  104. $pos++;
  105. }
  106. $depth = 0;
  107. $param_length = '';
  108. $param_opts = array();
  109. for ($i=$pos; $i<$parsed_param['len']; $i++) {
  110. if (($parsed_param[$i]['type'] == 'alpha_columnType'
  111. || $parsed_param[$i]['type'] == 'alpha_functionName') && $depth == 0
  112. ) {
  113. $retval[2] = strtoupper($parsed_param[$i]['data']);
  114. } else if ($parsed_param[$i]['type'] == 'punct_bracket_open_round'
  115. && $depth == 0
  116. ) {
  117. $depth = 1;
  118. } else if ($parsed_param[$i]['type'] == 'punct_bracket_close_round'
  119. && $depth == 1
  120. ) {
  121. $depth = 0;
  122. } else if ($depth == 1) {
  123. $param_length .= $parsed_param[$i]['data'];
  124. } else if ($parsed_param[$i]['type'] == 'alpha_reservedWord'
  125. && strtoupper($parsed_param[$i]['data']) == 'CHARSET' && $depth == 0
  126. ) {
  127. if ($parsed_param[$i+1]['type'] == 'alpha_charset'
  128. || $parsed_param[$i+1]['type'] == 'alpha_identifier'
  129. ) {
  130. $param_opts[] = strtolower($parsed_param[$i+1]['data']);
  131. }
  132. } else if ($parsed_param[$i]['type'] == 'alpha_columnAttrib'
  133. && $depth == 0
  134. ) {
  135. $param_opts[] = strtoupper($parsed_param[$i]['data']);
  136. }
  137. }
  138. $retval[3] = $param_length;
  139. sort($param_opts);
  140. $retval[4] = implode(' ', $param_opts);
  141. return $retval;
  142. } // end PMA_RTN_parseOneParameter()
  143. /**
  144. * This function looks through the contents of a parsed
  145. * SHOW CREATE [PROCEDURE | FUNCTION] query and extracts
  146. * information about the routine's parameters.
  147. *
  148. * @param array $parsed_query Parsed query, returned by by PMA_SQP_parse()
  149. * @param string $routine_type Routine type: 'PROCEDURE' or 'FUNCTION'
  150. *
  151. * @return array Information about the parameteres of a routine.
  152. */
  153. function PMA_RTN_parseAllParameters($parsed_query, $routine_type)
  154. {
  155. $retval = array();
  156. $retval['num'] = 0;
  157. // First get the list of parameters from the query
  158. $buffer = '';
  159. $params = array();
  160. $fetching = false;
  161. $depth = 0;
  162. for ($i=0; $i<$parsed_query['len']; $i++) {
  163. if ($parsed_query[$i]['type'] == 'alpha_reservedWord'
  164. && $parsed_query[$i]['data'] == $routine_type
  165. ) {
  166. $fetching = true;
  167. } else if ($fetching == true
  168. && $parsed_query[$i]['type'] == 'punct_bracket_open_round'
  169. ) {
  170. $depth++;
  171. if ($depth > 1) {
  172. $buffer .= $parsed_query[$i]['data'] . ' ';
  173. }
  174. } else if ($fetching == true
  175. && $parsed_query[$i]['type'] == 'punct_bracket_close_round'
  176. ) {
  177. $depth--;
  178. if ($depth > 0) {
  179. $buffer .= $parsed_query[$i]['data'] . ' ';
  180. } else {
  181. break;
  182. }
  183. } else if ($parsed_query[$i]['type'] == 'punct_listsep' && $depth == 1) {
  184. $params[] = $buffer;
  185. $retval['num']++;
  186. $buffer = '';
  187. } else if ($fetching == true && $depth > 0) {
  188. $buffer .= $parsed_query[$i]['data'] . ' ';
  189. }
  190. }
  191. if (! empty($buffer)) {
  192. $params[] = $buffer;
  193. $retval['num']++;
  194. }
  195. // Now parse each parameter individually
  196. foreach ($params as $key => $value) {
  197. list($retval['dir'][],
  198. $retval['name'][],
  199. $retval['type'][],
  200. $retval['length'][],
  201. $retval['opts'][]) = PMA_RTN_parseOneParameter($value);
  202. }
  203. // Since some indices of $retval may be still undefined, we fill
  204. // them each with an empty array to avoid E_ALL errors in PHP.
  205. foreach (array('dir', 'name', 'type', 'length', 'opts') as $key => $index) {
  206. if (! isset($retval[$index])) {
  207. $retval[$index] = array();
  208. }
  209. }
  210. return $retval;
  211. } // end PMA_RTN_parseAllParameters()
  212. /**
  213. * This function looks through the contents of a parsed
  214. * SHOW CREATE [PROCEDURE | FUNCTION] query and extracts
  215. * information about the routine's definer.
  216. *
  217. * @param array $parsed_query Parsed query, returned by PMA_SQP_parse()
  218. *
  219. * @return string The definer of a routine.
  220. */
  221. function PMA_RTN_parseRoutineDefiner($parsed_query)
  222. {
  223. $retval = '';
  224. $fetching = false;
  225. for ($i=0; $i<$parsed_query['len']; $i++) {
  226. if ($parsed_query[$i]['type'] == 'alpha_reservedWord'
  227. && $parsed_query[$i]['data'] == 'DEFINER'
  228. ) {
  229. $fetching = true;
  230. } else if ($fetching == true
  231. && $parsed_query[$i]['type'] != 'quote_backtick'
  232. && substr($parsed_query[$i]['type'], 0, 5) != 'punct'
  233. ) {
  234. break;
  235. } else if ($fetching == true
  236. && $parsed_query[$i]['type'] == 'quote_backtick'
  237. ) {
  238. $retval .= PMA_Util::unQuote(
  239. $parsed_query[$i]['data']
  240. );
  241. } else if ($fetching == true && $parsed_query[$i]['type'] == 'punct_user') {
  242. $retval .= $parsed_query[$i]['data'];
  243. }
  244. }
  245. return $retval;
  246. } // end PMA_RTN_parseRoutineDefiner()
  247. /**
  248. * Handles editor requests for adding or editing an item
  249. *
  250. * @return Does not return
  251. */
  252. function PMA_RTN_handleEditor()
  253. {
  254. global $_GET, $_POST, $_REQUEST, $GLOBALS, $db, $errors;
  255. if (! empty($_REQUEST['editor_process_add'])
  256. || ! empty($_REQUEST['editor_process_edit'])
  257. ) {
  258. /**
  259. * Handle a request to create/edit a routine
  260. */
  261. $sql_query = '';
  262. $routine_query = PMA_RTN_getQueryFromRequest();
  263. if (! count($errors)) { // set by PMA_RTN_getQueryFromRequest()
  264. // Execute the created query
  265. if (! empty($_REQUEST['editor_process_edit'])) {
  266. if (! in_array($_REQUEST['item_original_type'], array('PROCEDURE', 'FUNCTION'))) {
  267. $errors[] = sprintf(
  268. __('Invalid routine type: "%s"'),
  269. htmlspecialchars($_REQUEST['item_original_type'])
  270. );
  271. } else {
  272. // Backup the old routine, in case something goes wrong
  273. $create_routine = PMA_DBI_get_definition(
  274. $db, $_REQUEST['item_original_type'],
  275. $_REQUEST['item_original_name']
  276. );
  277. $drop_routine = "DROP {$_REQUEST['item_original_type']} "
  278. . PMA_Util::backquote($_REQUEST['item_original_name'])
  279. . ";\n";
  280. $result = PMA_DBI_try_query($drop_routine);
  281. if (! $result) {
  282. $errors[] = sprintf(
  283. __('The following query has failed: "%s"'),
  284. htmlspecialchars($drop_routine)
  285. )
  286. . '<br />'
  287. . __('MySQL said: ') . PMA_DBI_getError(null);
  288. } else {
  289. $result = PMA_DBI_try_query($routine_query);
  290. if (! $result) {
  291. $errors[] = sprintf(
  292. __('The following query has failed: "%s"'),
  293. htmlspecialchars($routine_query)
  294. )
  295. . '<br />'
  296. . __('MySQL said: ') . PMA_DBI_getError(null);
  297. // We dropped the old routine,
  298. // but were unable to create the new one
  299. // Try to restore the backup query
  300. $result = PMA_DBI_try_query($create_routine);
  301. if (! $result) {
  302. // OMG, this is really bad! We dropped the query,
  303. // failed to create a new one
  304. // and now even the backup query does not execute!
  305. // This should not happen, but we better handle
  306. // this just in case.
  307. $errors[] = __(
  308. 'Sorry, we failed to restore'
  309. . ' the dropped routine.'
  310. )
  311. . '<br />'
  312. . __('The backed up query was:')
  313. . "\"" . htmlspecialchars($create_routine) . "\""
  314. . '<br />'
  315. . __('MySQL said: ') . PMA_DBI_getError(null);
  316. }
  317. } else {
  318. $message = PMA_Message::success(
  319. __('Routine %1$s has been modified.')
  320. );
  321. $message->addParam(
  322. PMA_Util::backquote($_REQUEST['item_name'])
  323. );
  324. $sql_query = $drop_routine . $routine_query;
  325. }
  326. }
  327. }
  328. } else {
  329. // 'Add a new routine' mode
  330. $result = PMA_DBI_try_query($routine_query);
  331. if (! $result) {
  332. $errors[] = sprintf(
  333. __('The following query has failed: "%s"'),
  334. htmlspecialchars($routine_query)
  335. )
  336. . '<br /><br />'
  337. . __('MySQL said: ') . PMA_DBI_getError(null);
  338. } else {
  339. $message = PMA_Message::success(
  340. __('Routine %1$s has been created.')
  341. );
  342. $message->addParam(
  343. PMA_Util::backquote($_REQUEST['item_name'])
  344. );
  345. $sql_query = $routine_query;
  346. }
  347. }
  348. }
  349. if (count($errors)) {
  350. $message = PMA_Message::error(
  351. __(
  352. '<b>One or more errors have occured while'
  353. . ' processing your request:</b>'
  354. )
  355. );
  356. $message->addString('<ul>');
  357. foreach ($errors as $string) {
  358. $message->addString('<li>' . $string . '</li>');
  359. }
  360. $message->addString('</ul>');
  361. }
  362. $output = PMA_Util::getMessage($message, $sql_query);
  363. if ($GLOBALS['is_ajax_request']) {
  364. $response = PMA_Response::getInstance();
  365. if ($message->isSuccess()) {
  366. $columns = "`SPECIFIC_NAME`, `ROUTINE_NAME`, `ROUTINE_TYPE`,"
  367. . " `DTD_IDENTIFIER`, `ROUTINE_DEFINITION`";
  368. $where = "ROUTINE_SCHEMA='" . PMA_Util::sqlAddSlashes($db) . "' "
  369. . "AND ROUTINE_NAME='"
  370. . PMA_Util::sqlAddSlashes($_REQUEST['item_name']) . "'"
  371. . "AND ROUTINE_TYPE='"
  372. . PMA_Util::sqlAddSlashes($_REQUEST['item_type']) . "'";
  373. $routine = PMA_DBI_fetch_single_row(
  374. "SELECT $columns FROM `INFORMATION_SCHEMA`.`ROUTINES`"
  375. . " WHERE $where;"
  376. );
  377. $response->addJSON(
  378. 'name', htmlspecialchars(strtoupper($_REQUEST['item_name']))
  379. );
  380. $response->addJSON('new_row', PMA_RTN_getRowForList($routine));
  381. $response->addJSON('insert', ! empty($routine));
  382. $response->addJSON('message', $output);
  383. } else {
  384. $response->isSuccess(false);
  385. $response->addJSON('message', $output);
  386. }
  387. exit;
  388. }
  389. }
  390. /**
  391. * Display a form used to add/edit a routine, if necessary
  392. */
  393. // FIXME: this must be simpler than that
  394. if (count($errors)
  395. || ( empty($_REQUEST['editor_process_add'])
  396. && empty($_REQUEST['editor_process_edit'])
  397. && (! empty($_REQUEST['add_item']) || ! empty($_REQUEST['edit_item'])
  398. || ! empty($_REQUEST['routine_addparameter'])
  399. || ! empty($_REQUEST['routine_removeparameter'])
  400. || ! empty($_REQUEST['routine_changetype'])))
  401. ) {
  402. // Handle requests to add/remove parameters and changing routine type
  403. // This is necessary when JS is disabled
  404. $operation = '';
  405. if (! empty($_REQUEST['routine_addparameter'])) {
  406. $operation = 'add';
  407. } else if (! empty($_REQUEST['routine_removeparameter'])) {
  408. $operation = 'remove';
  409. } else if (! empty($_REQUEST['routine_changetype'])) {
  410. $operation = 'change';
  411. }
  412. // Get the data for the form (if any)
  413. if (! empty($_REQUEST['add_item'])) {
  414. $title = PMA_RTE_getWord('add');
  415. $routine = PMA_RTN_getDataFromRequest();
  416. $mode = 'add';
  417. } else if (! empty($_REQUEST['edit_item'])) {
  418. $title = __("Edit routine");
  419. if (! $operation && ! empty($_REQUEST['item_name'])
  420. && empty($_REQUEST['editor_process_edit'])
  421. ) {
  422. $routine = PMA_RTN_getDataFromName(
  423. $_REQUEST['item_name'], $_REQUEST['item_type']
  424. );
  425. if ($routine !== false) {
  426. $routine['item_original_name'] = $routine['item_name'];
  427. $routine['item_original_type'] = $routine['item_type'];
  428. }
  429. } else {
  430. $routine = PMA_RTN_getDataFromRequest();
  431. }
  432. $mode = 'edit';
  433. }
  434. if ($routine !== false) {
  435. // Show form
  436. $editor = PMA_RTN_getEditorForm($mode, $operation, $routine);
  437. if ($GLOBALS['is_ajax_request']) {
  438. $response = PMA_Response::getInstance();
  439. $response->addJSON('message', $editor);
  440. $response->addJSON('title', $title);
  441. $response->addJSON('param_template', PMA_RTN_getParameterRow());
  442. $response->addJSON('type', $routine['item_type']);
  443. } else {
  444. echo "\n\n<h2>$title</h2>\n\n$editor";
  445. }
  446. exit;
  447. } else {
  448. $message = __('Error in processing request') . ' : ';
  449. $message .= sprintf(
  450. PMA_RTE_getWord('not_found'),
  451. htmlspecialchars(PMA_Util::backquote($_REQUEST['item_name'])),
  452. htmlspecialchars(PMA_Util::backquote($db))
  453. );
  454. $message = PMA_message::error($message);
  455. if ($GLOBALS['is_ajax_request']) {
  456. $response->isSuccess(false);
  457. $response->addJSON('message', $message);
  458. exit;
  459. } else {
  460. $message->display();
  461. }
  462. }
  463. }
  464. } // end PMA_RTN_handleEditor()
  465. /**
  466. * This function will generate the values that are required to
  467. * complete the editor form. It is especially necessary to handle
  468. * the 'Add another parameter', 'Remove last parameter' and
  469. * 'Change routine type' functionalities when JS is disabled.
  470. *
  471. * @return array Data necessary to create the routine editor.
  472. */
  473. function PMA_RTN_getDataFromRequest()
  474. {
  475. global $_REQUEST, $param_directions, $param_sqldataaccess;
  476. $retval = array();
  477. $indices = array('item_name',
  478. 'item_original_name',
  479. 'item_returnlength',
  480. 'item_returnopts_num',
  481. 'item_returnopts_text',
  482. 'item_definition',
  483. 'item_comment',
  484. 'item_definer');
  485. foreach ($indices as $key => $index) {
  486. $retval[$index] = isset($_REQUEST[$index]) ? $_REQUEST[$index] : '';
  487. }
  488. $retval['item_type'] = 'PROCEDURE';
  489. $retval['item_type_toggle'] = 'FUNCTION';
  490. if (isset($_REQUEST['item_type']) && $_REQUEST['item_type'] == 'FUNCTION') {
  491. $retval['item_type'] = 'FUNCTION';
  492. $retval['item_type_toggle'] = 'PROCEDURE';
  493. }
  494. $retval['item_original_type'] = 'PROCEDURE';
  495. if (isset($_REQUEST['item_original_type'])
  496. && $_REQUEST['item_original_type'] == 'FUNCTION'
  497. ) {
  498. $retval['item_original_type'] = 'FUNCTION';
  499. }
  500. $retval['item_num_params'] = 0;
  501. $retval['item_param_dir'] = array();
  502. $retval['item_param_name'] = array();
  503. $retval['item_param_type'] = array();
  504. $retval['item_param_length'] = array();
  505. $retval['item_param_opts_num'] = array();
  506. $retval['item_param_opts_text'] = array();
  507. if ( isset($_REQUEST['item_param_name'])
  508. && isset($_REQUEST['item_param_type'])
  509. && isset($_REQUEST['item_param_length'])
  510. && isset($_REQUEST['item_param_opts_num'])
  511. && isset($_REQUEST['item_param_opts_text'])
  512. && is_array($_REQUEST['item_param_name'])
  513. && is_array($_REQUEST['item_param_type'])
  514. && is_array($_REQUEST['item_param_length'])
  515. && is_array($_REQUEST['item_param_opts_num'])
  516. && is_array($_REQUEST['item_param_opts_text'])
  517. ) {
  518. if ($_REQUEST['item_type'] == 'PROCEDURE') {
  519. $retval['item_param_dir'] = $_REQUEST['item_param_dir'];
  520. foreach ($retval['item_param_dir'] as $key => $value) {
  521. if (! in_array($value, $param_directions, true)) {
  522. $retval['item_param_dir'][$key] = '';
  523. }
  524. }
  525. }
  526. $retval['item_param_name'] = $_REQUEST['item_param_name'];
  527. $retval['item_param_type'] = $_REQUEST['item_param_type'];
  528. foreach ($retval['item_param_type'] as $key => $value) {
  529. if (! in_array($value, PMA_Util::getSupportedDatatypes(), true)) {
  530. $retval['item_param_type'][$key] = '';
  531. }
  532. }
  533. $retval['item_param_length'] = $_REQUEST['item_param_length'];
  534. $retval['item_param_opts_num'] = $_REQUEST['item_param_opts_num'];
  535. $retval['item_param_opts_text'] = $_REQUEST['item_param_opts_text'];
  536. $retval['item_num_params'] = max(
  537. count($retval['item_param_name']),
  538. count($retval['item_param_type']),
  539. count($retval['item_param_length']),
  540. count($retval['item_param_opts_num']),
  541. count($retval['item_param_opts_text'])
  542. );
  543. }
  544. $retval['item_returntype'] = '';
  545. if (isset($_REQUEST['item_returntype'])
  546. && in_array($_REQUEST['item_returntype'], PMA_Util::getSupportedDatatypes())
  547. ) {
  548. $retval['item_returntype'] = $_REQUEST['item_returntype'];
  549. }
  550. $retval['item_isdeterministic'] = '';
  551. if (isset($_REQUEST['item_isdeterministic'])
  552. && strtolower($_REQUEST['item_isdeterministic']) == 'on'
  553. ) {
  554. $retval['item_isdeterministic'] = " checked='checked'";
  555. }
  556. $retval['item_securitytype_definer'] = '';
  557. $retval['item_securitytype_invoker'] = '';
  558. if (isset($_REQUEST['item_securitytype'])) {
  559. if ($_REQUEST['item_securitytype'] === 'DEFINER') {
  560. $retval['item_securitytype_definer'] = " selected='selected'";
  561. } else if ($_REQUEST['item_securitytype'] === 'INVOKER') {
  562. $retval['item_securitytype_invoker'] = " selected='selected'";
  563. }
  564. }
  565. $retval['item_sqldataaccess'] = '';
  566. if (isset($_REQUEST['item_sqldataaccess'])
  567. && in_array($_REQUEST['item_sqldataaccess'], $param_sqldataaccess, true)
  568. ) {
  569. $retval['item_sqldataaccess'] = $_REQUEST['item_sqldataaccess'];
  570. }
  571. return $retval;
  572. } // end function PMA_RTN_getDataFromRequest()
  573. /**
  574. * This function will generate the values that are required to complete
  575. * the "Edit routine" form given the name of a routine.
  576. *
  577. * @param string $name The name of the routine.
  578. * @param string $type Type of routine (ROUTINE|PROCEDURE)
  579. * @param bool $all Whether to return all data or just
  580. * the info about parameters.
  581. *
  582. * @return array Data necessary to create the routine editor.
  583. */
  584. function PMA_RTN_getDataFromName($name, $type, $all = true)
  585. {
  586. global $db;
  587. $retval = array();
  588. // Build and execute the query
  589. $fields = "SPECIFIC_NAME, ROUTINE_TYPE, DTD_IDENTIFIER, "
  590. . "ROUTINE_DEFINITION, IS_DETERMINISTIC, SQL_DATA_ACCESS, "
  591. . "ROUTINE_COMMENT, SECURITY_TYPE";
  592. $where = "ROUTINE_SCHEMA='" . PMA_Util::sqlAddSlashes($db) . "' "
  593. . "AND SPECIFIC_NAME='" . PMA_Util::sqlAddSlashes($name) . "'"
  594. . "AND ROUTINE_TYPE='" . PMA_Util::sqlAddSlashes($type) . "'";
  595. $query = "SELECT $fields FROM INFORMATION_SCHEMA.ROUTINES WHERE $where;";
  596. $routine = PMA_DBI_fetch_single_row($query);
  597. if (! $routine) {
  598. return false;
  599. }
  600. // Get required data
  601. $retval['item_name'] = $routine['SPECIFIC_NAME'];
  602. $retval['item_type'] = $routine['ROUTINE_TYPE'];
  603. $parsed_query = PMA_SQP_parse(
  604. PMA_DBI_get_definition(
  605. $db,
  606. $routine['ROUTINE_TYPE'],
  607. $routine['SPECIFIC_NAME']
  608. )
  609. );
  610. $params = PMA_RTN_parseAllParameters($parsed_query, $routine['ROUTINE_TYPE']);
  611. $retval['item_num_params'] = $params['num'];
  612. $retval['item_param_dir'] = $params['dir'];
  613. $retval['item_param_name'] = $params['name'];
  614. $retval['item_param_type'] = $params['type'];
  615. $retval['item_param_length'] = $params['length'];
  616. $retval['item_param_opts_num'] = $params['opts'];
  617. $retval['item_param_opts_text'] = $params['opts'];
  618. // Get extra data
  619. if ($all) {
  620. if ($retval['item_type'] == 'FUNCTION') {
  621. $retval['item_type_toggle'] = 'PROCEDURE';
  622. } else {
  623. $retval['item_type_toggle'] = 'FUNCTION';
  624. }
  625. $retval['item_returntype'] = '';
  626. $retval['item_returnlength'] = '';
  627. $retval['item_returnopts_num'] = '';
  628. $retval['item_returnopts_text'] = '';
  629. if (! empty($routine['DTD_IDENTIFIER'])) {
  630. if (strlen($routine['DTD_IDENTIFIER']) > 63) {
  631. // If the DTD_IDENTIFIER string from INFORMATION_SCHEMA is
  632. // at least 64 characters, then it may actually have been
  633. // chopped because that column is a varchar(64), so we will
  634. // parse the output of SHOW CREATE query to get accurate
  635. // information about the return variable.
  636. $dtd = '';
  637. $fetching = false;
  638. for ($i=0; $i<$parsed_query['len']; $i++) {
  639. if ($parsed_query[$i]['type'] == 'alpha_reservedWord'
  640. && strtoupper($parsed_query[$i]['data']) == 'RETURNS'
  641. ) {
  642. $fetching = true;
  643. } else if ($fetching == true
  644. && $parsed_query[$i]['type'] == 'alpha_reservedWord'
  645. ) {
  646. // We will not be looking for options such as UNSIGNED
  647. // or ZEROFILL because there is no way that a numeric
  648. // field's DTD_IDENTIFIER can be longer than 64
  649. // characters. We can safely assume that the return
  650. // datatype is either ENUM or SET, so we only look
  651. // for CHARSET.
  652. $word = strtoupper($parsed_query[$i]['data']);
  653. if ($word == 'CHARSET'
  654. && ($parsed_query[$i+1]['type'] == 'alpha_charset'
  655. || $parsed_query[$i+1]['type'] == 'alpha_identifier')
  656. ) {
  657. $dtd .= $word . ' ' . $parsed_query[$i+1]['data'];
  658. }
  659. break;
  660. } else if ($fetching == true) {
  661. $dtd .= $parsed_query[$i]['data'] . ' ';
  662. }
  663. }
  664. $routine['DTD_IDENTIFIER'] = $dtd;
  665. }
  666. $returnparam = PMA_RTN_parseOneParameter($routine['DTD_IDENTIFIER']);
  667. $retval['item_returntype'] = $returnparam[2];
  668. $retval['item_returnlength'] = $returnparam[3];
  669. $retval['item_returnopts_num'] = $returnparam[4];
  670. $retval['item_returnopts_text'] = $returnparam[4];
  671. }
  672. $retval['item_definer'] = PMA_RTN_parseRoutineDefiner($parsed_query);
  673. $retval['item_definition'] = $routine['ROUTINE_DEFINITION'];
  674. $retval['item_isdeterministic'] = '';
  675. if ($routine['IS_DETERMINISTIC'] == 'YES') {
  676. $retval['item_isdeterministic'] = " checked='checked'";
  677. }
  678. $retval['item_securitytype_definer'] = '';
  679. $retval['item_securitytype_invoker'] = '';
  680. if ($routine['SECURITY_TYPE'] == 'DEFINER') {
  681. $retval['item_securitytype_definer'] = " selected='selected'";
  682. } else if ($routine['SECURITY_TYPE'] == 'INVOKER') {
  683. $retval['item_securitytype_invoker'] = " selected='selected'";
  684. }
  685. $retval['item_sqldataaccess'] = $routine['SQL_DATA_ACCESS'];
  686. $retval['item_comment'] = $routine['ROUTINE_COMMENT'];
  687. }
  688. return $retval;
  689. } // PMA_RTN_getDataFromName()
  690. /**
  691. * Creates one row for the parameter table used in the routine editor.
  692. *
  693. * @param array $routine Data for the routine returned by
  694. * PMA_RTN_getDataFromRequest() or
  695. * PMA_RTN_getDataFromName()
  696. * @param mixed $index Either a numeric index of the row being processed
  697. * or NULL to create a template row for AJAX request
  698. * @param string $class Class used to hide the direction column, if the
  699. * row is for a stored function.
  700. *
  701. * @return string HTML code of one row of parameter table for the editor.
  702. */
  703. function PMA_RTN_getParameterRow($routine = array(), $index = null, $class = '')
  704. {
  705. global $param_directions, $param_opts_num, $titles;
  706. if ($index === null) {
  707. // template row for AJAX request
  708. $i = 0;
  709. $index = '%s';
  710. $drop_class = '';
  711. $routine = array(
  712. 'item_param_dir' => array(0 => ''),
  713. 'item_param_name' => array(0 => ''),
  714. 'item_param_type' => array(0 => ''),
  715. 'item_param_length' => array(0 => ''),
  716. 'item_param_opts_num' => array(0 => ''),
  717. 'item_param_opts_text' => array(0 => '')
  718. );
  719. } else if (! empty($routine)) {
  720. // regular row for routine editor
  721. $drop_class = ' hide';
  722. $i = $index;
  723. } else {
  724. // No input data. This shouldn't happen,
  725. // but better be safe than sorry.
  726. return '';
  727. }
  728. // Create the output
  729. $retval = "";
  730. $retval .= " <tr>\n";
  731. $retval .= " <td class='routine_direction_cell$class'>\n";
  732. $retval .= " <select name='item_param_dir[$index]'>\n";
  733. foreach ($param_directions as $key => $value) {
  734. $selected = "";
  735. if (! empty($routine['item_param_dir'][$i])
  736. && $routine['item_param_dir'][$i] == $value
  737. ) {
  738. $selected = " selected='selected'";
  739. }
  740. $retval .= " <option$selected>$value</option>\n";
  741. }
  742. $retval .= " </select>\n";
  743. $retval .= " </td>\n";
  744. $retval .= " <td><input name='item_param_name[$index]' type='text'\n"
  745. . " value='{$routine['item_param_name'][$i]}' /></td>\n";
  746. $retval .= " <td><select name='item_param_type[$index]'>";
  747. $retval .= PMA_Util::getSupportedDatatypes(
  748. true, $routine['item_param_type'][$i]
  749. ) . "\n";
  750. $retval .= " </select></td>\n";
  751. $retval .= " <td>\n";
  752. $retval .= " <input id='item_param_length_$index'\n"
  753. . " name='item_param_length[$index]' type='text'\n"
  754. . " value='{$routine['item_param_length'][$i]}' />\n";
  755. $retval .= " <div class='enum_hint'>\n";
  756. $retval .= " <a href='#' class='open_enum_editor'>\n";
  757. $retval .= " "
  758. . PMA_Util::getImage('b_edit', '', array('title'=>__('ENUM/SET editor')))
  759. . "\n";
  760. $retval .= " </a>\n";
  761. $retval .= " </div>\n";
  762. $retval .= " </td>\n";
  763. $retval .= " <td class='hide no_len'>---</td>\n";
  764. $retval .= " <td class='routine_param_opts_text'>\n";
  765. $retval .= PMA_generateCharsetDropdownBox(
  766. PMA_CSDROPDOWN_CHARSET,
  767. "item_param_opts_text[$index]",
  768. null,
  769. $routine['item_param_opts_text'][$i]
  770. );
  771. $retval .= " </td>\n";
  772. $retval .= " <td class='hide no_opts'>---</td>\n";
  773. $retval .= " <td class='routine_param_opts_num'>\n";
  774. $retval .= " <select name='item_param_opts_num[$index]'>\n";
  775. $retval .= " <option value=''></option>";
  776. foreach ($param_opts_num as $key => $value) {
  777. $selected = "";
  778. if (! empty($routine['item_param_opts_num'][$i])
  779. && $routine['item_param_opts_num'][$i] == $value
  780. ) {
  781. $selected = " selected='selected'";
  782. }
  783. $retval .= "<option$selected>$value</option>";
  784. }
  785. $retval .= "\n </select>\n";
  786. $retval .= " </td>\n";
  787. $retval .= " <td class='routine_param_remove$drop_class'>\n";
  788. $retval .= " <a href='#' class='routine_param_remove_anchor'>\n";
  789. $retval .= " {$titles['Drop']}\n";
  790. $retval .= " </a>\n";
  791. $retval .= " </td>\n";
  792. $retval .= " </tr>\n";
  793. return $retval;
  794. } // end PMA_RTN_getParameterRow()
  795. /**
  796. * Displays a form used to add/edit a routine
  797. *
  798. * @param string $mode If the editor will be used edit a routine
  799. * or add a new one: 'edit' or 'add'.
  800. * @param string $operation If the editor was previously invoked with
  801. * JS turned off, this will hold the name of
  802. * the current operation
  803. * @param array $routine Data for the routine returned by
  804. * PMA_RTN_getDataFromRequest() or
  805. * PMA_RTN_getDataFromName()
  806. *
  807. * @return string HTML code for the editor.
  808. */
  809. function PMA_RTN_getEditorForm($mode, $operation, $routine)
  810. {
  811. global $db, $errors, $param_sqldataaccess, $param_opts_num;
  812. // Escape special characters
  813. $need_escape = array(
  814. 'item_original_name',
  815. 'item_name',
  816. 'item_returnlength',
  817. 'item_definition',
  818. 'item_definer',
  819. 'item_comment'
  820. );
  821. foreach ($need_escape as $key => $index) {
  822. $routine[$index] = htmlentities($routine[$index], ENT_QUOTES, 'UTF-8');
  823. }
  824. for ($i=0; $i<$routine['item_num_params']; $i++) {
  825. $routine['item_param_name'][$i] = htmlentities(
  826. $routine['item_param_name'][$i],
  827. ENT_QUOTES
  828. );
  829. $routine['item_param_length'][$i] = htmlentities(
  830. $routine['item_param_length'][$i],
  831. ENT_QUOTES
  832. );
  833. }
  834. // Handle some logic first
  835. if ($operation == 'change') {
  836. if ($routine['item_type'] == 'PROCEDURE') {
  837. $routine['item_type'] = 'FUNCTION';
  838. $routine['item_type_toggle'] = 'PROCEDURE';
  839. } else {
  840. $routine['item_type'] = 'PROCEDURE';
  841. $routine['item_type_toggle'] = 'FUNCTION';
  842. }
  843. } else if ($operation == 'add'
  844. || ($routine['item_num_params'] == 0 && $mode == 'add' && ! $errors)
  845. ) {
  846. $routine['item_param_dir'][] = '';
  847. $routine['item_param_name'][] = '';
  848. $routine['item_param_type'][] = '';
  849. $routine['item_param_length'][] = '';
  850. $routine['item_param_opts_num'][] = '';
  851. $routine['item_param_opts_text'][] = '';
  852. $routine['item_num_params']++;
  853. } else if ($operation == 'remove') {
  854. unset($routine['item_param_dir'][$routine['item_num_params']-1]);
  855. unset($routine['item_param_name'][$routine['item_num_params']-1]);
  856. unset($routine['item_param_type'][$routine['item_num_params']-1]);
  857. unset($routine['item_param_length'][$routine['item_num_params']-1]);
  858. unset($routine['item_param_opts_num'][$routine['item_num_params']-1]);
  859. unset($routine['item_param_opts_text'][$routine['item_num_params']-1]);
  860. $routine['item_num_params']--;
  861. }
  862. $disable_remove_parameter = '';
  863. if (! $routine['item_num_params']) {
  864. $disable_remove_parameter = " color: gray;' disabled='disabled";
  865. }
  866. $original_routine = '';
  867. if ($mode == 'edit') {
  868. $original_routine = "<input name='item_original_name' "
  869. . "type='hidden' "
  870. . "value='{$routine['item_original_name']}'/>\n"
  871. . "<input name='item_original_type' "
  872. . "type='hidden' "
  873. . "value='{$routine['item_original_type']}'/>\n";
  874. }
  875. $isfunction_class = '';
  876. $isprocedure_class = '';
  877. $isfunction_select = '';
  878. $isprocedure_select = '';
  879. if ($routine['item_type'] == 'PROCEDURE') {
  880. $isfunction_class = ' hide';
  881. $isprocedure_select = " selected='selected'";
  882. } else {
  883. $isprocedure_class = ' hide';
  884. $isfunction_select = " selected='selected'";
  885. }
  886. // Create the output
  887. $retval = "";
  888. $retval .= "<!-- START " . strtoupper($mode) . " ROUTINE FORM -->\n\n";
  889. $retval .= "<form class='rte_form' action='db_routines.php' method='post'>\n";
  890. $retval .= "<input name='{$mode}_item' type='hidden' value='1' />\n";
  891. $retval .= $original_routine;
  892. $retval .= PMA_generate_common_hidden_inputs($db) . "\n";
  893. $retval .= "<fieldset>\n";
  894. $retval .= "<legend>" . __('Details') . "</legend>\n";
  895. $retval .= "<table class='rte_table' style='width: 100%'>\n";
  896. $retval .= "<tr>\n";
  897. $retval .= " <td style='width: 20%;'>" . __('Routine name') . "</td>\n";
  898. $retval .= " <td><input type='text' name='item_name' maxlength='64'\n";
  899. $retval .= " value='{$routine['item_name']}' /></td>\n";
  900. $retval .= "</tr>\n";
  901. $retval .= "<tr>\n";
  902. $retval .= " <td>" . __('Type') . "</td>\n";
  903. $retval .= " <td>\n";
  904. if ($GLOBALS['is_ajax_request']) {
  905. $retval .= " <select name='item_type'>\n"
  906. . "<option value='PROCEDURE'$isprocedure_select>PROCEDURE</option>\n"
  907. . "<option value='FUNCTION'$isfunction_select>FUNCTION</option>\n"
  908. . "</select>\n";
  909. } else {
  910. $retval .= "<input name='item_type' type='hidden'"
  911. . " value='{$routine['item_type']}' />\n"
  912. . "<div style='width: 49%; float: left; text-align: center;"
  913. . " font-weight: bold;'>\n"
  914. . $routine['item_type'] . "\n"
  915. . "</div>\n"
  916. . "<input style='width: 49%;' type='submit' name='routine_changetype'\n"
  917. . " value='" . sprintf(__('Change to %s'), $routine['item_type_toggle'])
  918. . "' />\n";
  919. }
  920. $retval .= " </td>\n";
  921. $retval .= "</tr>\n";
  922. $retval .= "<tr>\n";
  923. $retval .= " <td>" . __('Parameters') . "</td>\n";
  924. $retval .= " <td>\n";
  925. // parameter handling start
  926. $retval .= " <table class='routine_params_table'>\n";
  927. $retval .= " <tr>\n";
  928. $retval .= " <th class='routine_direction_cell$isprocedure_class'>"
  929. . __('Direction') . "</th>\n";
  930. $retval .= " <th>" . __('Name') . "</th>\n";
  931. $retval .= " <th>" . __('Type') . "</th>\n";
  932. $retval .= " <th>" . __('Length/Values') . "</th>\n";
  933. $retval .= " <th colspan='2'>" . __('Options') . "</th>\n";
  934. $retval .= " <th class='routine_param_remove hide'>&nbsp;</th>\n";
  935. $retval .= " </tr>";
  936. for ($i=0; $i<$routine['item_num_params']; $i++) { // each parameter
  937. $retval .= PMA_RTN_getParameterRow($routine, $i, $isprocedure_class);
  938. }
  939. $retval .= " </table>\n";
  940. $retval .= " </td>\n";
  941. $retval .= "</tr>\n";
  942. $retval .= "<tr>\n";
  943. $retval .= " <td>&nbsp;</td>\n";
  944. $retval .= " <td>\n";
  945. $retval .= " <input style='width: 49%;' type='submit' \n";
  946. $retval .= " name='routine_addparameter'\n";
  947. $retval .= " value='" . __('Add parameter') . "' />\n";
  948. $retval .= " <input style='width: 49%;$disable_remove_parameter'\n";
  949. $retval .= " type='submit' \n";
  950. $retval .= " name='routine_removeparameter'\n";
  951. $retval .= " value='" . __('Remove last parameter') . "' />\n";
  952. $retval .= " </td>\n";
  953. $retval .= "</tr>\n";
  954. // parameter handling end
  955. $retval .= "<tr class='routine_return_row$isfunction_class'>\n";
  956. $retval .= " <td>" . __('Return type') . "</td>\n";
  957. $retval .= " <td><select name='item_returntype'>\n";
  958. $retval .= PMA_Util::getSupportedDatatypes(true, $routine['item_returntype'])
  959. . "\n";
  960. $retval .= " </select></td>\n";
  961. $retval .= "</tr>\n";
  962. $retval .= "<tr class='routine_return_row$isfunction_class'>\n";
  963. $retval .= " <td>" . __('Return length/values') . "</td>\n";
  964. $retval .= " <td><input type='text' name='item_returnlength'\n";
  965. $retval .= " value='{$routine['item_returnlength']}' /></td>\n";
  966. $retval .= " <td class='hide no_len'>---</td>\n";
  967. $retval .= "</tr>\n";
  968. $retval .= "<tr class='routine_return_row$isfunction_class'>\n";
  969. $retval .= " <td>" . __('Return options') . "</td>\n";
  970. $retval .= " <td><div>\n";
  971. $retval .= PMA_generateCharsetDropdownBox(
  972. PMA_CSDROPDOWN_CHARSET,
  973. "item_returnopts_text",
  974. null,
  975. $routine['item_returnopts_text']
  976. );
  977. $retval .= "\n </div>\n";
  978. $retval .= " <div><select name='item_returnopts_num'>\n";
  979. $retval .= " <option value=''></option>";
  980. foreach ($param_opts_num as $key => $value) {
  981. $selected = "";
  982. if (! empty($routine['item_returnopts_num'])
  983. && $routine['item_returnopts_num'] == $value
  984. ) {
  985. $selected = " selected='selected'";
  986. }
  987. $retval .= "<option$selected>$value</option>";
  988. }
  989. $retval .= "\n </select></div>\n";
  990. $retval .= " <div class='hide no_opts'>---</div>\n";
  991. $retval .= "</td>\n";
  992. $retval .= "</tr>\n";
  993. $retval .= "<tr>\n";
  994. $retval .= " <td>" . __('Definition') . "</td>\n";
  995. $retval .= " <td><textarea name='item_definition' rows='15' cols='40'>";
  996. $retval .= $routine['item_definition'];
  997. $retval .= "</textarea></td>\n";
  998. $retval .= "</tr>\n";
  999. $retval .= "<tr>\n";
  1000. $retval .= " <td>" . __('Is deterministic') . "</td>\n";
  1001. $retval .= " <td><input type='checkbox' name='item_isdeterministic'"
  1002. . $routine['item_isdeterministic'] . " /></td>\n";
  1003. $retval .= "</tr>\n";
  1004. $retval .= "<tr>\n";
  1005. $retval .= " <td>" . __('Definer') . "</td>\n";
  1006. $retval .= " <td><input type='text' name='item_definer'\n";
  1007. $retval .= " value='{$routine['item_definer']}' /></td>\n";
  1008. $retval .= "</tr>\n";
  1009. $retval .= "<tr>\n";
  1010. $retval .= " <td>" . __('Security type') . "</td>\n";
  1011. $retval .= " <td><select name='item_securitytype'>\n";
  1012. $retval .= " <option value='DEFINER'"
  1013. . $routine['item_securitytype_definer'] . ">DEFINER</option>\n";
  1014. $retval .= " <option value='INVOKER'"
  1015. . $routine['item_securitytype_invoker'] . ">INVOKER</option>\n";
  1016. $retval .= " </select></td>\n";
  1017. $retval .= "</tr>\n";
  1018. $retval .= "<tr>\n";
  1019. $retval .= " <td>" . __('SQL data access') . "</td>\n";
  1020. $retval .= " <td><select name='item_sqldataaccess'>\n";
  1021. foreach ($param_sqldataaccess as $key => $value) {
  1022. $selected = "";
  1023. if ($routine['item_sqldataaccess'] == $value) {
  1024. $selected = " selected='selected'";
  1025. }
  1026. $retval .= " <option$selected>$value</option>\n";
  1027. }
  1028. $retval .= " </select></td>\n";
  1029. $retval .= "</tr>\n";
  1030. $retval .= "<tr>\n";
  1031. $retval .= " <td>" . __('Comment') . "</td>\n";
  1032. $retval .= " <td><input type='text' name='item_comment' maxlength='64'\n";
  1033. $retval .= " value='{$routine['item_comment']}' /></td>\n";
  1034. $retval .= "</tr>\n";
  1035. $retval .= "</table>\n";
  1036. $retval .= "</fieldset>\n";
  1037. if ($GLOBALS['is_ajax_request']) {
  1038. $retval .= "<input type='hidden' name='editor_process_{$mode}'\n";
  1039. $retval .= " value='true' />\n";
  1040. $retval .= "<input type='hidden' name='ajax_request' value='true' />\n";
  1041. } else {
  1042. $retval .= "<fieldset class='tblFooters'>\n";
  1043. $retval .= " <input type='submit' name='editor_process_{$mode}'\n";
  1044. $retval .= " value='" . __('Go') . "' />\n";
  1045. $retval .= "</fieldset>\n";
  1046. }
  1047. $retval .= "</form>\n\n";
  1048. $retval .= "<!-- END " . strtoupper($mode) . " ROUTINE FORM -->\n\n";
  1049. return $retval;
  1050. } // end PMA_RTN_getEditorForm()
  1051. /**
  1052. * Composes the query necessary to create a routine from an HTTP request.
  1053. *
  1054. * @return string The CREATE [ROUTINE | PROCEDURE] query.
  1055. */
  1056. function PMA_RTN_getQueryFromRequest()
  1057. {
  1058. global $_REQUEST, $errors, $param_sqldataaccess, $param_directions;
  1059. $_REQUEST['item_type'] = isset($_REQUEST['item_type'])
  1060. ? $_REQUEST['item_type'] : '';
  1061. $query = 'CREATE ';
  1062. if (! empty($_REQUEST['item_definer'])) {
  1063. if (strpos($_REQUEST['item_definer'], '@') !== false) {
  1064. $arr = explode('@', $_REQUEST['item_definer']);
  1065. $query .= 'DEFINER=' . PMA_Util::backquote($arr[0]);
  1066. $query .= '@' . PMA_Util::backquote($arr[1]) . ' ';
  1067. } else {
  1068. $errors[] = __('The definer must be in the "username@hostname" format');
  1069. }
  1070. }
  1071. if ($_REQUEST['item_type'] == 'FUNCTION'
  1072. || $_REQUEST['item_type'] == 'PROCEDURE'
  1073. ) {
  1074. $query .= $_REQUEST['item_type'] . ' ';
  1075. } else {
  1076. $errors[] = sprintf(
  1077. __('Invalid routine type: "%s"'),
  1078. htmlspecialchars($_REQUEST['item_type'])
  1079. );
  1080. }
  1081. if (! empty($_REQUEST['item_name'])) {
  1082. $query .= PMA_Util::backquote($_REQUEST['item_name']);
  1083. } else {
  1084. $errors[] = __('You must provide a routine name');
  1085. }
  1086. $params = '';
  1087. $warned_about_dir = false;
  1088. $warned_about_name = false;
  1089. $warned_about_length = false;
  1090. if ( ! empty($_REQUEST['item_param_name'])
  1091. && ! empty($_REQUEST['item_param_type'])
  1092. && ! empty($_REQUEST['item_param_length'])
  1093. && is_array($_REQUEST['item_param_name'])
  1094. && is_array($_REQUEST['item_param_type'])
  1095. && is_array($_REQUEST['item_param_length'])
  1096. ) {
  1097. for ($i=0; $i<count($_REQUEST['item_param_name']); $i++) {
  1098. if (! empty($_REQUEST['item_param_name'][$i])
  1099. && ! empty($_REQUEST['item_param_type'][$i])
  1100. ) {
  1101. if ($_REQUEST['item_type'] == 'PROCEDURE'
  1102. && ! empty($_REQUEST['item_param_dir'][$i])
  1103. && in_array($_REQUEST['item_param_dir'][$i], $param_directions)
  1104. ) {
  1105. $params .= $_REQUEST['item_param_dir'][$i] . " "
  1106. . PMA_Util::backquote($_REQUEST['item_param_name'][$i])
  1107. . " " . $_REQUEST['item_param_type'][$i];
  1108. } else if ($_REQUEST['item_type'] == 'FUNCTION') {
  1109. $params .= PMA_Util::backquote($_REQUEST['item_param_name'][$i])
  1110. . " " . $_REQUEST['item_param_type'][$i];
  1111. } else if (! $warned_about_dir) {
  1112. $warned_about_dir = true;
  1113. $errors[] = sprintf(
  1114. __('Invalid direction "%s" given for parameter.'),
  1115. htmlspecialchars($_REQUEST['item_param_dir'][$i])
  1116. );
  1117. }
  1118. if ($_REQUEST['item_param_length'][$i] != ''
  1119. && !preg_match(
  1120. '@^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT|SERIAL|BOOLEAN)$@i',
  1121. $_REQUEST['item_param_type'][$i]
  1122. )
  1123. ) {
  1124. $params .= "(" . $_REQUEST['item_param_length'][$i] . ")";
  1125. } else if ($_REQUEST['item_param_length'][$i] == ''
  1126. && preg_match(
  1127. '@^(ENUM|SET|VARCHAR|VARBINARY)$@i',
  1128. $_REQUEST['item_param_type'][$i]
  1129. )
  1130. ) {
  1131. if (! $warned_about_length) {
  1132. $warned_about_length = true;
  1133. $errors[] = __(
  1134. 'You must provide length/values for routine parameters'
  1135. . ' of type ENUM, SET, VARCHAR and VARBINARY.'
  1136. );
  1137. }
  1138. }
  1139. if (! empty($_REQUEST['item_param_opts_text'][$i])) {
  1140. if ($GLOBALS['PMA_Types']->getTypeClass($_REQUEST['item_param_type'][$i]) == 'CHAR') {
  1141. $params .= ' CHARSET '
  1142. . strtolower($_REQUEST['item_param_opts_text'][$i]);
  1143. }
  1144. }
  1145. if (! empty($_REQUEST['item_param_opts_num'][$i])) {
  1146. if ($GLOBALS['PMA_Types']->getTypeClass($_REQUEST['item_param_type'][$i]) == 'NUMBER') {
  1147. $params .= ' '
  1148. . strtoupper($_REQUEST['item_param_opts_num'][$i]);
  1149. }
  1150. }
  1151. if ($i != count($_REQUEST['item_param_name'])-1) {
  1152. $params .= ", ";
  1153. }
  1154. } else if (! $warned_about_name) {
  1155. $warned_about_name = true;
  1156. $errors[] = __(
  1157. 'You must provide a name and a type for each routine parameter.'
  1158. );
  1159. break;
  1160. }
  1161. }
  1162. }
  1163. $query .= "(" . $params . ") ";
  1164. if ($_REQUEST['item_type'] == 'FUNCTION') {
  1165. if (! empty($_REQUEST['item_returntype'])
  1166. && in_array(
  1167. $_REQUEST['item_returntype'], PMA_Util::getSupportedDatatypes()
  1168. )
  1169. ) {
  1170. $query .= "RETURNS {$_REQUEST['item_returntype']}";
  1171. } else {
  1172. $errors[] = __('You must provide a valid return type for the routine.');
  1173. }
  1174. if (! empty($_REQUEST['item_returnlength'])
  1175. && !preg_match(
  1176. '@^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT|SERIAL|BOOLEAN)$@i',
  1177. $_REQUEST['item_returntype']
  1178. )
  1179. ) {
  1180. $query .= "(" . $_REQUEST['item_returnlength'] . ")";
  1181. } else if (empty($_REQUEST['item_returnlength'])
  1182. && preg_match(
  1183. '@^(ENUM|SET|VARCHAR|VARBINARY)$@i', $_REQUEST['item_returntype']
  1184. )
  1185. ) {
  1186. if (! $warned_about_length) {
  1187. $warned_about_length = true;
  1188. $errors[] = __(
  1189. 'You must provide length/values for routine parameters'
  1190. . ' of type ENUM, SET, VARCHAR and VARBINARY.'
  1191. );
  1192. }
  1193. }
  1194. if (! empty($_REQUEST['item_returnopts_text'])) {
  1195. if ($GLOBALS['PMA_Types']->getTypeClass($_REQUEST['item_returntype']) == 'CHAR') {
  1196. $query .= ' CHARSET '
  1197. . strtolower($_REQUEST['item_returnopts_text']);
  1198. }
  1199. }
  1200. if (! empty($_REQUEST['item_returnopts_num'])) {
  1201. if ($GLOBALS['PMA_Types']->getTypeClass($_REQUEST['item_returntype']) == 'NUMBER') {
  1202. $query .= ' ' . strtoupper($_REQUEST['item_returnopts_num']);
  1203. }
  1204. }
  1205. $query .= ' ';
  1206. }
  1207. if (! empty($_REQUEST['item_comment'])) {
  1208. $query .= "COMMENT '" . PMA_Util::sqlAddslashes($_REQUEST['item_comment'])
  1209. . "' ";
  1210. }
  1211. if (isset($_REQUEST['item_isdeterministic'])) {
  1212. $query .= 'DETERMINISTIC ';
  1213. } else {
  1214. $query .= 'NOT DETERMINISTIC ';
  1215. }
  1216. if (! empty($_REQUEST['item_sqldataaccess'])
  1217. && in_array($_REQUEST['item_sqldataaccess'], $param_sqldataaccess)
  1218. ) {
  1219. $query .= $_REQUEST['item_sqldataaccess'] . ' ';
  1220. }
  1221. if (! empty($_REQUEST['item_securitytype'])) {
  1222. if ($_REQUEST['item_securitytype'] == 'DEFINER'
  1223. || $_REQUEST['item_securitytype'] == 'INVOKER'
  1224. ) {
  1225. $query .= 'SQL SECURITY ' . $_REQUEST['item_securitytype'] . ' ';
  1226. }
  1227. }
  1228. if (! empty($_REQUEST['item_definition'])) {
  1229. $query .= $_REQUEST['item_definition'];
  1230. } else {
  1231. $errors[] = __('You must provide a routine definition.');
  1232. }
  1233. return $query;
  1234. } // end PMA_RTN_getQueryFromRequest()
  1235. /**
  1236. * Handles requests for executing a routine
  1237. *
  1238. * @return Does not return
  1239. */
  1240. function PMA_RTN_handleExecute()
  1241. {
  1242. global $_GET, $_POST, $_REQUEST, $GLOBALS, $db;
  1243. /**
  1244. * Handle all user requests other than the default of listing routines
  1245. */
  1246. if (! empty($_REQUEST['execute_routine']) && ! empty($_REQUEST['item_name'])) {
  1247. // Build the queries
  1248. $routine = PMA_RTN_getDataFromName(
  1249. $_REQUEST['item_name'], $_REQUEST['item_type'], false
  1250. );
  1251. if ($routine !== false) {
  1252. $queries = array();
  1253. $end_query = array();
  1254. $args = array();
  1255. $all_functions = $GLOBALS['PMA_Types']->getAllFunctions();
  1256. for ($i=0; $i<$routine['item_num_params']; $i++) {
  1257. if (isset($_REQUEST['params'][$routine['item_param_name'][$i]])) {
  1258. $value = $_REQUEST['params'][$routine['item_param_name'][$i]];
  1259. if (is_array($value)) { // is SET type
  1260. $value = implode(',', $value);
  1261. }
  1262. $value = PMA_Util::sqlAddSlashes($value);
  1263. if (! empty($_REQUEST['funcs'][$routine['item_param_name'][$i]])
  1264. && in_array(
  1265. $_REQUEST['funcs'][$routine['item_param_name'][$i]],
  1266. $all_functions
  1267. )
  1268. ) {
  1269. $queries[] = "SET @p$i="
  1270. . $_REQUEST['funcs'][$routine['item_param_name'][$i]]
  1271. . "('$value');\n";
  1272. } else {
  1273. $queries[] = "SET @p$i='$value';\n";
  1274. }
  1275. $args[] = "@p$i";
  1276. } else {
  1277. $args[] = "@p$i";
  1278. }
  1279. if ($routine['item_type'] == 'PROCEDURE') {
  1280. if ($routine['item_param_dir'][$i] == 'OUT'
  1281. || $routine['item_param_dir'][$i] == 'INOUT'
  1282. ) {
  1283. $end_query[] = "@p$i AS "
  1284. . PMA_Util::backquote($routine['item_param_name'][$i]);
  1285. }
  1286. }
  1287. }
  1288. if ($routine['item_type'] == 'PROCEDURE') {
  1289. $queries[] = "CALL " . PMA_Util::backquote($routine['item_name'])
  1290. . "(" . implode(', ', $args) . ");\n";
  1291. if (count($end_query)) {
  1292. $queries[] = "SELECT " . implode(', ', $end_query) . ";\n";
  1293. }
  1294. } else {
  1295. $queries[] = "SELECT " . PMA_Util::backquote($routine['item_name'])
  1296. . "(" . implode(', ', $args) . ") "
  1297. . "AS " . PMA_Util::backquote($routine['item_name'])
  1298. . ";\n";
  1299. }
  1300. // Get all the queries as one SQL statement
  1301. $multiple_query = implode("", $queries);
  1302. $outcome = true;
  1303. $affected = 0;
  1304. // Execute query
  1305. if (! PMA_DBI_try_multi_query($multiple_query)) {
  1306. $outcome = false;
  1307. }
  1308. // Generate output
  1309. if ($outcome) {
  1310. // Pass the SQL queries through the "pretty printer"
  1311. $output = '<code class="sql" style="margin-bottom: 1em;">';
  1312. $output .= PMA_SQP_formatHtml(PMA_SQP_parse(implode($queries)));
  1313. $output .= '</code>';
  1314. // Display results
  1315. $output .= "<fieldset><legend>";
  1316. $output .= sprintf(
  1317. __('Execution results of routine %s'),
  1318. PMA_Util::backquote(htmlspecialchars($routine['item_name']))
  1319. );
  1320. $output .= "</legend>";
  1321. $num_of_rusults_set_to_display = 0;
  1322. do {
  1323. $result = PMA_DBI_store_result();
  1324. $num_rows = PMA_DBI_num_rows($result);
  1325. if (($result !== false) && ($num_rows > 0)) {
  1326. $output .= "<table><tr>";
  1327. foreach (PMA_DBI_get_fields_meta($result) as $key => $field) {
  1328. $output .= "<th>";
  1329. $output .= htmlspecialchars($field->name);
  1330. $output .= "</th>";
  1331. }
  1332. $output .= "</tr>";
  1333. $color_class = 'odd';
  1334. while ($row = PMA_DBI_fetch_assoc($result)) {
  1335. $output .= "<tr>";
  1336. foreach ($row as $key => $value) {
  1337. if ($value === null) {
  1338. $value = '<i>NULL</i>';
  1339. } else {
  1340. $value = htmlspecialchars($value);
  1341. }
  1342. $output .= "<td class='" . $color_class . "'>"
  1343. . $value . "</td>";
  1344. }
  1345. $output .= "</tr>";
  1346. $color_class = ($color_class == 'odd') ? 'even' : 'odd';
  1347. }
  1348. $output .= "</table>";
  1349. $num_of_rusults_set_to_display++;
  1350. $affected = $num_rows;
  1351. }
  1352. if (! PMA_DBI_more_results()) {
  1353. break;
  1354. }
  1355. $output .= "<br/>";
  1356. PMA_DBI_free_result($result);
  1357. } while (PMA_DBI_next_result());
  1358. $output .= "</fieldset>";
  1359. $message = __('Your SQL query has been executed successfully');
  1360. if ($routine['item_type'] == 'PROCEDURE') {
  1361. $message .= '<br />';
  1362. // TODO : message need to be modified according to the
  1363. // output from the routine
  1364. $message .= sprintf(
  1365. _ngettext(
  1366. '%d row affected by the last statement inside the procedure',
  1367. '%d rows affected by the last statement inside the procedure',
  1368. $affected
  1369. ),
  1370. $affected
  1371. );
  1372. }
  1373. $message = PMA_message::success($message);
  1374. if ($num_of_rusults_set_to_display == 0) {
  1375. $notice = __(
  1376. 'MySQL returned an empty result set (i.e. zero rows).'
  1377. );
  1378. $output .= PMA_message::notice($notice)->getDisplay();
  1379. }
  1380. } else {
  1381. $output = '';
  1382. $message = PMA_message::error(
  1383. sprintf(
  1384. __('The following query has failed: "%s"'),
  1385. htmlspecialchars($multiple_query)
  1386. )
  1387. . '<br /><br />'
  1388. . __('MySQL said: ') . PMA_DBI_getError(null)
  1389. );
  1390. }
  1391. // Print/send output
  1392. if ($GLOBALS['is_ajax_request']) {
  1393. $response = PMA_Response::getInstance();
  1394. $response->isSuccess($message->isSuccess());
  1395. $response->addJSON('message', $message->getDisplay() . $output);
  1396. $response->addJSON('dialog', false);
  1397. exit;
  1398. } else {
  1399. echo $message->getDisplay() . $output;
  1400. if ($message->isError()) {
  1401. // At least one query has failed, so shouldn't
  1402. // execute any more queries, so we quit.
  1403. exit;
  1404. }
  1405. unset($_POST);
  1406. // Now deliberately fall through to displaying the routines list
  1407. }
  1408. } else {
  1409. $message = __('Error in processing request') . ' : ';
  1410. $message .= sprintf(
  1411. PMA_RTE_getWord('not_found'),
  1412. htmlspecialchars(PMA_Util::backquote($_REQUEST['item_name'])),
  1413. htmlspecialchars(PMA_Util::backquote($db))
  1414. );
  1415. $message = PMA_message::error($message);
  1416. if ($GLOBALS['is_ajax_request']) {
  1417. $response = PMA_Response::getInstance();
  1418. $response->isSuccess(false);
  1419. $response->addJSON('message', $message);
  1420. exit;
  1421. } else {
  1422. echo $message->getDisplay();
  1423. unset($_POST);
  1424. }
  1425. }
  1426. } else if (! empty($_GET['execute_dialog']) && ! empty($_GET['item_name'])) {
  1427. /**
  1428. * Display the execute form for a routine.
  1429. */
  1430. $routine = PMA_RTN_getDataFromName(
  1431. $_GET['item_name'], $_GET['item_type'], true
  1432. );
  1433. if ($routine !== false) {
  1434. $form = PMA_RTN_getExecuteForm($routine);
  1435. if ($GLOBALS['is_ajax_request'] == true) {
  1436. $title = __("Execute routine") . " " . PMA_Util::backquote(
  1437. htmlentities($_GET['item_name'], ENT_QUOTES)
  1438. );
  1439. $response = PMA_Response::getInstance();
  1440. $response->addJSON('message', $form);
  1441. $response->addJSON('title', $title);
  1442. $response->addJSON('dialog', true);
  1443. } else {
  1444. echo "\n\n<h2>" . __("Execute routine") . "</h2>\n\n";
  1445. echo $form;
  1446. }
  1447. exit;
  1448. } else if (($GLOBALS['is_ajax_request'] == true)) {
  1449. $message = __('Error in processing request') . ' : ';
  1450. $message .= sprintf(
  1451. PMA_RTE_getWord('not_found'),
  1452. htmlspecialchars(PMA_Util::backquote($_REQUEST['item_name'])),
  1453. htmlspecialchars(PMA_Util::backquote($db))
  1454. );
  1455. $message = PMA_message::error($message);
  1456. $response = PMA_Response::getInstance();
  1457. $response->isSuccess(false);
  1458. $response->addJSON('message', $message);
  1459. exit;
  1460. }
  1461. }
  1462. }
  1463. /**
  1464. * Creates the HTML code that shows the routine execution dialog.
  1465. *
  1466. * @param array $routine Data for the routine returned by
  1467. * PMA_RTN_getDataFromName()
  1468. *
  1469. * @return string HTML code for the routine execution dialog.
  1470. */
  1471. function PMA_RTN_getExecuteForm($routine)
  1472. {
  1473. global $db, $cfg;
  1474. // Escape special characters
  1475. $routine['item_name'] = htmlentities($routine['item_name'], ENT_QUOTES);
  1476. for ($i=0; $i<$routine['item_num_params']; $i++) {
  1477. $routine['item_param_name'][$i] = htmlentities(
  1478. $routine['item_param_name'][$i],
  1479. ENT_QUOTES
  1480. );
  1481. }
  1482. // Create the output
  1483. $retval = "";
  1484. $retval .= "<!-- START ROUTINE EXECUTE FORM -->\n\n";
  1485. $retval .= "<form action='db_routines.php' method='post' class='rte_form'>\n";
  1486. $retval .= "<input type='hidden' name='item_name'\n";
  1487. $retval .= " value='{$routine['item_name']}' />\n";
  1488. $retval .= "<input type='hidden' name='item_type'\n";
  1489. $retval .= " value='{$routine['item_type']}' />\n";
  1490. $retval .= PMA_generate_common_hidden_inputs($db) . "\n";
  1491. $retval .= "<fieldset>\n";
  1492. if ($GLOBALS['is_ajax_request'] != true) {
  1493. $retval .= "<legend>{$routine['item_name']}</legend>\n";
  1494. $retval .= "<table class='rte_table'>\n";
  1495. $retval .= "<caption class='tblHeaders'>\n";
  1496. $retval .= __('Routine parameters');
  1497. $retval .= "</caption>\n";
  1498. } else {
  1499. $retval .= "<legend>" . __('Routine parameters') . "</legend>\n";
  1500. $retval .= "<table class='rte_table' style='width: 100%;'>\n";
  1501. }
  1502. $retval .= "<tr>\n";
  1503. $retval .= "<th>" . __('Name') . "</th>\n";
  1504. $retval .= "<th>" . __('Type') . "</th>\n";
  1505. if ($cfg['ShowFunctionFields']) {
  1506. $retval .= "<th>" . __('Function') . "</th>\n";
  1507. }
  1508. $retval .= "<th>" . __('Value') . "</th>\n";
  1509. $retval .= "</tr>\n";
  1510. // Get a list of data types that are not yet supported.
  1511. $no_support_types = PMA_Util::unsupportedDatatypes();
  1512. for ($i=0; $i<$routine['item_num_params']; $i++) { // Each parameter
  1513. if ($routine['item_type'] == 'PROCEDURE'
  1514. && $routine['item_param_dir'][$i] == 'OUT'
  1515. ) {
  1516. continue;
  1517. }
  1518. $rowclass = ($i % 2 == 0) ? 'even' : 'odd';
  1519. $retval .= "\n<tr class='$rowclass'>\n";
  1520. $retval .= "<td>{$routine['item_param_name'][$i]}</td>\n";
  1521. $retval .= "<td>{$routine['item_param_type'][$i]}</td>\n";
  1522. if ($cfg['ShowFunctionFields']) {
  1523. $retval .= "<td>\n";
  1524. if (stristr($routine['item_param_type'][$i], 'enum')
  1525. || stristr($routine['item_param_type'][$i], 'set')
  1526. || in_array(
  1527. strtolower($routine['item_param_type'][$i]), $no_support_types
  1528. )
  1529. ) {
  1530. $retval .= "--\n";
  1531. } else {
  1532. $field = array(
  1533. 'True_Type' => strtolower($routine['item_param_type'][$i]),
  1534. 'Type' => '',
  1535. 'Key' => '',
  1536. 'Field' => '',
  1537. 'Default' => '',
  1538. 'first_timestamp' => false
  1539. );
  1540. $retval .= "<select name='funcs[{$routine['item_param_name'][$i]}]'>";
  1541. $retval .= PMA_Util::getFunctionsForField($field, false);
  1542. $retval .= "</select>";
  1543. }
  1544. $retval .= "</td>\n";
  1545. }
  1546. // Append a class to date/time fields so that
  1547. // jQuery can attach a datepicker to them
  1548. $class = '';
  1549. if ($routine['item_param_type'][$i] == 'DATETIME'
  1550. || $routine['item_param_type'][$i] == 'TIMESTAMP'
  1551. ) {
  1552. $class = 'datetimefield';
  1553. } else if ($routine['item_param_type'][$i] == 'DATE') {
  1554. $class = 'datefield';
  1555. }
  1556. $retval .= "<td class='nowrap'>\n";
  1557. if (in_array($routine['item_param_type'][$i], array('ENUM', 'SET'))) {
  1558. $tokens = PMA_SQP_parse($routine['item_param_length'][$i]);
  1559. if ($routine['item_param_type'][$i] == 'ENUM') {
  1560. $input_type = 'radio';
  1561. } else {
  1562. $input_type = 'checkbox';
  1563. }
  1564. for ($j=0; $j<$tokens['len']; $j++) {
  1565. if ($tokens[$j]['type'] != 'punct_listsep') {
  1566. $tokens[$j]['data'] = htmlentities(
  1567. PMA_Util::unquote($tokens[$j]['data']),
  1568. ENT_QUOTES
  1569. );
  1570. $retval .= "<input name='params[{$routine['item_param_name'][$i]}][]' "
  1571. . "value='{$tokens[$j]['data']}' type='$input_type' />"
  1572. . "{$tokens[$j]['data']}<br />\n";
  1573. }
  1574. }
  1575. } else if (in_array(
  1576. strtolower($routine['item_param_type'][$i]), $no_support_types
  1577. )) {
  1578. $retval .= "\n";
  1579. } else {
  1580. $retval .= "<input class='$class' type='text' name='params["
  1581. . $routine['item_param_name'][$i] . "]' />\n";
  1582. }
  1583. $retval .= "</td>\n";
  1584. $retval .= "</tr>\n";
  1585. }
  1586. $retval .= "\n</table>\n";
  1587. if ($GLOBALS['is_ajax_request'] != true) {
  1588. $retval .= "</fieldset>\n\n";
  1589. $retval .= "<fieldset class='tblFooters'>\n";
  1590. $retval .= " <input type='submit' name='execute_routine'\n";
  1591. $retval .= " value='" . __('Go') . "' />\n";
  1592. $retval .= "</fieldset>\n";
  1593. } else {
  1594. $retval .= "<input type='hidden' name='execute_routine' value='true' />";
  1595. $retval .= "<input type='hidden' name='ajax_request' value='true' />";
  1596. }
  1597. $retval .= "</form>\n\n";
  1598. $retval .= "<!-- END ROUTINE EXECUTE FORM -->\n\n";
  1599. return $retval;
  1600. } // end PMA_RTN_getExecuteForm()
  1601. ?>