Routines.php 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Database;
  4. use PhpMyAdmin\Charsets;
  5. use PhpMyAdmin\Charsets\Charset;
  6. use PhpMyAdmin\DatabaseInterface;
  7. use PhpMyAdmin\Html\Generator;
  8. use PhpMyAdmin\Html\MySQLDocumentation;
  9. use PhpMyAdmin\Message;
  10. use PhpMyAdmin\Response;
  11. use PhpMyAdmin\SqlParser\Parser;
  12. use PhpMyAdmin\SqlParser\Statements\CreateStatement;
  13. use PhpMyAdmin\SqlParser\Utils\Routine;
  14. use PhpMyAdmin\Template;
  15. use PhpMyAdmin\Url;
  16. use PhpMyAdmin\Util;
  17. use const ENT_QUOTES;
  18. use function array_merge;
  19. use function count;
  20. use function explode;
  21. use function htmlentities;
  22. use function htmlspecialchars;
  23. use function implode;
  24. use function in_array;
  25. use function is_array;
  26. use function is_string;
  27. use function max;
  28. use function mb_strpos;
  29. use function mb_strtolower;
  30. use function mb_strtoupper;
  31. use function preg_match;
  32. use function sprintf;
  33. use function stripos;
  34. use function substr;
  35. use function trim;
  36. /**
  37. * Functions for routine management.
  38. */
  39. class Routines
  40. {
  41. /** @var array<int, string> */
  42. private $directions = ['IN', 'OUT', 'INOUT'];
  43. /** @var array<int, string> */
  44. private $sqlDataAccess = ['CONTAINS SQL', 'NO SQL', 'READS SQL DATA', 'MODIFIES SQL DATA'];
  45. /** @var array<int, string> */
  46. private $numericOptions = ['UNSIGNED', 'ZEROFILL', 'UNSIGNED ZEROFILL'];
  47. /** @var DatabaseInterface */
  48. private $dbi;
  49. /** @var Template */
  50. private $template;
  51. /** @var Response */
  52. private $response;
  53. /**
  54. * @param DatabaseInterface $dbi DatabaseInterface instance.
  55. * @param Template $template Template instance.
  56. * @param Response $response Response instance.
  57. */
  58. public function __construct(DatabaseInterface $dbi, Template $template, $response)
  59. {
  60. $this->dbi = $dbi;
  61. $this->template = $template;
  62. $this->response = $response;
  63. }
  64. /**
  65. * Handles editor requests for adding or editing an item
  66. *
  67. * @return void
  68. */
  69. public function handleEditor()
  70. {
  71. global $db, $errors;
  72. $errors = $this->handleRequestCreateOrEdit($errors, $db);
  73. /**
  74. * Display a form used to add/edit a routine, if necessary
  75. */
  76. // FIXME: this must be simpler than that
  77. if (! count($errors)
  78. && ( ! empty($_POST['editor_process_add'])
  79. || ! empty($_POST['editor_process_edit'])
  80. || (empty($_REQUEST['add_item']) && empty($_REQUEST['edit_item'])
  81. && empty($_POST['routine_addparameter'])
  82. && empty($_POST['routine_removeparameter'])
  83. && empty($_POST['routine_changetype'])))
  84. ) {
  85. return;
  86. }
  87. // Handle requests to add/remove parameters and changing routine type
  88. // This is necessary when JS is disabled
  89. $operation = '';
  90. if (! empty($_POST['routine_addparameter'])) {
  91. $operation = 'add';
  92. } elseif (! empty($_POST['routine_removeparameter'])) {
  93. $operation = 'remove';
  94. } elseif (! empty($_POST['routine_changetype'])) {
  95. $operation = 'change';
  96. }
  97. // Get the data for the form (if any)
  98. $routine = null;
  99. $mode = null;
  100. $title = null;
  101. if (! empty($_REQUEST['add_item'])) {
  102. $title = __('Add routine');
  103. $routine = $this->getDataFromRequest();
  104. $mode = 'add';
  105. } elseif (! empty($_REQUEST['edit_item'])) {
  106. $title = __('Edit routine');
  107. if (! $operation && ! empty($_GET['item_name'])
  108. && empty($_POST['editor_process_edit'])
  109. ) {
  110. $routine = $this->getDataFromName(
  111. $_GET['item_name'],
  112. $_GET['item_type']
  113. );
  114. if ($routine !== null) {
  115. $routine['item_original_name'] = $routine['item_name'];
  116. $routine['item_original_type'] = $routine['item_type'];
  117. }
  118. } else {
  119. $routine = $this->getDataFromRequest();
  120. }
  121. $mode = 'edit';
  122. }
  123. if ($routine !== null) {
  124. // Show form
  125. $editor = $this->getEditorForm($mode, $operation, $routine);
  126. if ($this->response->isAjax()) {
  127. $this->response->addJSON('message', $editor);
  128. $this->response->addJSON('title', $title);
  129. $this->response->addJSON('paramTemplate', $this->getParameterRow());
  130. $this->response->addJSON('type', $routine['item_type']);
  131. } else {
  132. echo "\n\n<h2>" . $title . "</h2>\n\n" . $editor;
  133. }
  134. exit;
  135. }
  136. $message = __('Error in processing request:') . ' ';
  137. $message .= sprintf(
  138. __(
  139. 'No routine with name %1$s found in database %2$s. '
  140. . 'You might be lacking the necessary privileges to edit this routine.'
  141. ),
  142. htmlspecialchars(
  143. Util::backquote($_REQUEST['item_name'])
  144. ),
  145. htmlspecialchars(Util::backquote($db))
  146. );
  147. $message = Message::error($message);
  148. if ($this->response->isAjax()) {
  149. $this->response->setRequestStatus(false);
  150. $this->response->addJSON('message', $message);
  151. exit;
  152. }
  153. echo $message->getDisplay();
  154. }
  155. /**
  156. * Handle request to create or edit a routine
  157. *
  158. * @param array $errors Errors
  159. * @param string $db DB name
  160. *
  161. * @return array
  162. */
  163. public function handleRequestCreateOrEdit(array $errors, $db)
  164. {
  165. global $message;
  166. if (empty($_POST['editor_process_add'])
  167. && empty($_POST['editor_process_edit'])
  168. ) {
  169. return $errors;
  170. }
  171. $sql_query = '';
  172. $routine_query = $this->getQueryFromRequest();
  173. // set by getQueryFromRequest()
  174. if (! count($errors)) {
  175. // Execute the created query
  176. if (! empty($_POST['editor_process_edit'])) {
  177. $isProcOrFunc = in_array(
  178. $_POST['item_original_type'],
  179. [
  180. 'PROCEDURE',
  181. 'FUNCTION',
  182. ]
  183. );
  184. if (! $isProcOrFunc) {
  185. $errors[] = sprintf(
  186. __('Invalid routine type: "%s"'),
  187. htmlspecialchars($_POST['item_original_type'])
  188. );
  189. } else {
  190. // Backup the old routine, in case something goes wrong
  191. $create_routine = $this->dbi->getDefinition(
  192. $db,
  193. $_POST['item_original_type'],
  194. $_POST['item_original_name']
  195. );
  196. $privilegesBackup = $this->backupPrivileges();
  197. $drop_routine = 'DROP ' . $_POST['item_original_type'] . ' '
  198. . Util::backquote($_POST['item_original_name'])
  199. . ";\n";
  200. $result = $this->dbi->tryQuery($drop_routine);
  201. if (! $result) {
  202. $errors[] = sprintf(
  203. __('The following query has failed: "%s"'),
  204. htmlspecialchars($drop_routine)
  205. )
  206. . '<br>'
  207. . __('MySQL said: ') . $this->dbi->getError();
  208. } else {
  209. [$newErrors, $message] = $this->create(
  210. $routine_query,
  211. $create_routine,
  212. $privilegesBackup
  213. );
  214. if (empty($newErrors)) {
  215. $sql_query = $drop_routine . $routine_query;
  216. } else {
  217. $errors = array_merge($errors, $newErrors);
  218. }
  219. unset($newErrors);
  220. }
  221. }
  222. } else {
  223. // 'Add a new routine' mode
  224. $result = $this->dbi->tryQuery($routine_query);
  225. if (! $result) {
  226. $errors[] = sprintf(
  227. __('The following query has failed: "%s"'),
  228. htmlspecialchars($routine_query)
  229. )
  230. . '<br><br>'
  231. . __('MySQL said: ') . $this->dbi->getError();
  232. } else {
  233. $message = Message::success(
  234. __('Routine %1$s has been created.')
  235. );
  236. $message->addParam(
  237. Util::backquote($_POST['item_name'])
  238. );
  239. $sql_query = $routine_query;
  240. }
  241. }
  242. }
  243. if (count($errors)) {
  244. $message = Message::error(
  245. __(
  246. 'One or more errors have occurred while'
  247. . ' processing your request:'
  248. )
  249. );
  250. $message->addHtml('<ul>');
  251. foreach ($errors as $string) {
  252. $message->addHtml('<li>' . $string . '</li>');
  253. }
  254. $message->addHtml('</ul>');
  255. }
  256. $output = Generator::getMessage($message, $sql_query);
  257. if (! $this->response->isAjax()) {
  258. return $errors;
  259. }
  260. if (! $message->isSuccess()) {
  261. $this->response->setRequestStatus(false);
  262. $this->response->addJSON('message', $output);
  263. exit;
  264. }
  265. $routines = $this->dbi->getRoutines(
  266. $db,
  267. $_POST['item_type'],
  268. $_POST['item_name']
  269. );
  270. $routine = $routines[0];
  271. $this->response->addJSON(
  272. 'name',
  273. htmlspecialchars(
  274. mb_strtoupper($_POST['item_name'])
  275. )
  276. );
  277. $this->response->addJSON('new_row', $this->getRow($routine));
  278. $this->response->addJSON('insert', ! empty($routine));
  279. $this->response->addJSON('message', $output);
  280. $this->response->addJSON('tableType', 'routines');
  281. exit;
  282. }
  283. /**
  284. * Backup the privileges
  285. *
  286. * @return array
  287. */
  288. public function backupPrivileges()
  289. {
  290. if (! $GLOBALS['proc_priv'] || ! $GLOBALS['is_reload_priv']) {
  291. return [];
  292. }
  293. // Backup the Old Privileges before dropping
  294. // if $_POST['item_adjust_privileges'] set
  295. if (! isset($_POST['item_adjust_privileges'])
  296. || empty($_POST['item_adjust_privileges'])
  297. ) {
  298. return [];
  299. }
  300. $privilegesBackupQuery = 'SELECT * FROM ' . Util::backquote(
  301. 'mysql'
  302. )
  303. . '.' . Util::backquote('procs_priv')
  304. . ' where Routine_name = "' . $_POST['item_original_name']
  305. . '" AND Routine_type = "' . $_POST['item_original_type']
  306. . '";';
  307. return $this->dbi->fetchResult(
  308. $privilegesBackupQuery,
  309. 0
  310. );
  311. }
  312. /**
  313. * Create the routine
  314. *
  315. * @param string $routine_query Query to create routine
  316. * @param string $create_routine Query to restore routine
  317. * @param array $privilegesBackup Privileges backup
  318. *
  319. * @return array
  320. */
  321. public function create(
  322. $routine_query,
  323. $create_routine,
  324. array $privilegesBackup
  325. ) {
  326. $result = $this->dbi->tryQuery($routine_query);
  327. if (! $result) {
  328. $errors = [];
  329. $errors[] = sprintf(
  330. __('The following query has failed: "%s"'),
  331. htmlspecialchars($routine_query)
  332. )
  333. . '<br>'
  334. . __('MySQL said: ') . $this->dbi->getError();
  335. // We dropped the old routine,
  336. // but were unable to create the new one
  337. // Try to restore the backup query
  338. $result = $this->dbi->tryQuery($create_routine);
  339. $errors = $this->checkResult($result, $create_routine, $errors);
  340. return [
  341. $errors,
  342. null,
  343. ];
  344. }
  345. // Default value
  346. $resultAdjust = false;
  347. if ($GLOBALS['proc_priv']
  348. && $GLOBALS['is_reload_priv']
  349. ) {
  350. // Insert all the previous privileges
  351. // but with the new name and the new type
  352. foreach ($privilegesBackup as $priv) {
  353. $adjustProcPrivilege = 'INSERT INTO '
  354. . Util::backquote('mysql') . '.'
  355. . Util::backquote('procs_priv')
  356. . ' VALUES("' . $priv[0] . '", "'
  357. . $priv[1] . '", "' . $priv[2] . '", "'
  358. . $_POST['item_name'] . '", "'
  359. . $_POST['item_type'] . '", "'
  360. . $priv[5] . '", "'
  361. . $priv[6] . '", "'
  362. . $priv[7] . '");';
  363. $resultAdjust = $this->dbi->query(
  364. $adjustProcPrivilege
  365. );
  366. }
  367. }
  368. $message = $this->flushPrivileges($resultAdjust);
  369. return [
  370. [],
  371. $message,
  372. ];
  373. }
  374. /**
  375. * Flush privileges and get message
  376. *
  377. * @param bool $flushPrivileges Flush privileges
  378. *
  379. * @return Message
  380. */
  381. public function flushPrivileges($flushPrivileges)
  382. {
  383. if ($flushPrivileges) {
  384. // Flush the Privileges
  385. $flushPrivQuery = 'FLUSH PRIVILEGES;';
  386. $this->dbi->query($flushPrivQuery);
  387. $message = Message::success(
  388. __(
  389. 'Routine %1$s has been modified. Privileges have been adjusted.'
  390. )
  391. );
  392. } else {
  393. $message = Message::success(
  394. __('Routine %1$s has been modified.')
  395. );
  396. }
  397. $message->addParam(
  398. Util::backquote($_POST['item_name'])
  399. );
  400. return $message;
  401. }
  402. /**
  403. * This function will generate the values that are required to
  404. * complete the editor form. It is especially necessary to handle
  405. * the 'Add another parameter', 'Remove last parameter' and
  406. * 'Change routine type' functionalities when JS is disabled.
  407. *
  408. * @return array Data necessary to create the routine editor.
  409. */
  410. public function getDataFromRequest()
  411. {
  412. $retval = [];
  413. $indices = [
  414. 'item_name',
  415. 'item_original_name',
  416. 'item_returnlength',
  417. 'item_returnopts_num',
  418. 'item_returnopts_text',
  419. 'item_definition',
  420. 'item_comment',
  421. 'item_definer',
  422. ];
  423. foreach ($indices as $index) {
  424. $retval[$index] = $_POST[$index] ?? '';
  425. }
  426. $retval['item_type'] = 'PROCEDURE';
  427. $retval['item_type_toggle'] = 'FUNCTION';
  428. if (isset($_REQUEST['item_type']) && $_REQUEST['item_type'] === 'FUNCTION') {
  429. $retval['item_type'] = 'FUNCTION';
  430. $retval['item_type_toggle'] = 'PROCEDURE';
  431. }
  432. $retval['item_original_type'] = 'PROCEDURE';
  433. if (isset($_POST['item_original_type'])
  434. && $_POST['item_original_type'] === 'FUNCTION'
  435. ) {
  436. $retval['item_original_type'] = 'FUNCTION';
  437. }
  438. $retval['item_num_params'] = 0;
  439. $retval['item_param_dir'] = [];
  440. $retval['item_param_name'] = [];
  441. $retval['item_param_type'] = [];
  442. $retval['item_param_length'] = [];
  443. $retval['item_param_opts_num'] = [];
  444. $retval['item_param_opts_text'] = [];
  445. if (isset($_POST['item_param_name'], $_POST['item_param_type'])
  446. && isset($_POST['item_param_length'])
  447. && isset($_POST['item_param_opts_num'])
  448. && isset($_POST['item_param_opts_text'])
  449. && is_array($_POST['item_param_name'])
  450. && is_array($_POST['item_param_type'])
  451. && is_array($_POST['item_param_length'])
  452. && is_array($_POST['item_param_opts_num'])
  453. && is_array($_POST['item_param_opts_text'])
  454. ) {
  455. if ($_POST['item_type'] === 'PROCEDURE') {
  456. $retval['item_param_dir'] = $_POST['item_param_dir'];
  457. foreach ($retval['item_param_dir'] as $key => $value) {
  458. if (in_array($value, $this->directions, true)) {
  459. continue;
  460. }
  461. $retval['item_param_dir'][$key] = '';
  462. }
  463. }
  464. $retval['item_param_name'] = $_POST['item_param_name'];
  465. $retval['item_param_type'] = $_POST['item_param_type'];
  466. foreach ($retval['item_param_type'] as $key => $value) {
  467. if (in_array($value, Util::getSupportedDatatypes(), true)) {
  468. continue;
  469. }
  470. $retval['item_param_type'][$key] = '';
  471. }
  472. $retval['item_param_length'] = $_POST['item_param_length'];
  473. $retval['item_param_opts_num'] = $_POST['item_param_opts_num'];
  474. $retval['item_param_opts_text'] = $_POST['item_param_opts_text'];
  475. $retval['item_num_params'] = max(
  476. count($retval['item_param_name']),
  477. count($retval['item_param_type']),
  478. count($retval['item_param_length']),
  479. count($retval['item_param_opts_num']),
  480. count($retval['item_param_opts_text'])
  481. );
  482. }
  483. $retval['item_returntype'] = '';
  484. if (isset($_POST['item_returntype'])
  485. && in_array($_POST['item_returntype'], Util::getSupportedDatatypes())
  486. ) {
  487. $retval['item_returntype'] = $_POST['item_returntype'];
  488. }
  489. $retval['item_isdeterministic'] = '';
  490. if (isset($_POST['item_isdeterministic'])
  491. && mb_strtolower($_POST['item_isdeterministic']) === 'on'
  492. ) {
  493. $retval['item_isdeterministic'] = " checked='checked'";
  494. }
  495. $retval['item_securitytype_definer'] = '';
  496. $retval['item_securitytype_invoker'] = '';
  497. if (isset($_POST['item_securitytype'])) {
  498. if ($_POST['item_securitytype'] === 'DEFINER') {
  499. $retval['item_securitytype_definer'] = " selected='selected'";
  500. } elseif ($_POST['item_securitytype'] === 'INVOKER') {
  501. $retval['item_securitytype_invoker'] = " selected='selected'";
  502. }
  503. }
  504. $retval['item_sqldataaccess'] = '';
  505. if (isset($_POST['item_sqldataaccess'])
  506. && in_array($_POST['item_sqldataaccess'], $this->sqlDataAccess, true)
  507. ) {
  508. $retval['item_sqldataaccess'] = $_POST['item_sqldataaccess'];
  509. }
  510. return $retval;
  511. }
  512. /**
  513. * This function will generate the values that are required to complete
  514. * the "Edit routine" form given the name of a routine.
  515. *
  516. * @param string $name The name of the routine.
  517. * @param string $type Type of routine (ROUTINE|PROCEDURE)
  518. * @param bool $all Whether to return all data or just the info about parameters.
  519. *
  520. * @return array|null Data necessary to create the routine editor.
  521. */
  522. public function getDataFromName($name, $type, $all = true): ?array
  523. {
  524. global $db;
  525. $retval = [];
  526. // Build and execute the query
  527. $fields = 'SPECIFIC_NAME, ROUTINE_TYPE, DTD_IDENTIFIER, '
  528. . 'ROUTINE_DEFINITION, IS_DETERMINISTIC, SQL_DATA_ACCESS, '
  529. . 'ROUTINE_COMMENT, SECURITY_TYPE';
  530. $where = 'ROUTINE_SCHEMA ' . Util::getCollateForIS() . '='
  531. . "'" . $this->dbi->escapeString($db) . "' "
  532. . "AND SPECIFIC_NAME='" . $this->dbi->escapeString($name) . "'"
  533. . "AND ROUTINE_TYPE='" . $this->dbi->escapeString($type) . "'";
  534. $query = 'SELECT ' . $fields . ' FROM INFORMATION_SCHEMA.ROUTINES WHERE ' . $where . ';';
  535. $routine = $this->dbi->fetchSingleRow($query, 'ASSOC');
  536. if (! $routine) {
  537. return null;
  538. }
  539. // Get required data
  540. $retval['item_name'] = $routine['SPECIFIC_NAME'];
  541. $retval['item_type'] = $routine['ROUTINE_TYPE'];
  542. $definition
  543. = $this->dbi->getDefinition(
  544. $db,
  545. $routine['ROUTINE_TYPE'],
  546. $routine['SPECIFIC_NAME']
  547. );
  548. if ($definition === null) {
  549. return null;
  550. }
  551. $parser = new Parser($definition);
  552. /**
  553. * @var CreateStatement $stmt
  554. */
  555. $stmt = $parser->statements[0];
  556. $params = Routine::getParameters($stmt);
  557. $retval['item_num_params'] = $params['num'];
  558. $retval['item_param_dir'] = $params['dir'];
  559. $retval['item_param_name'] = $params['name'];
  560. $retval['item_param_type'] = $params['type'];
  561. $retval['item_param_length'] = $params['length'];
  562. $retval['item_param_length_arr'] = $params['length_arr'];
  563. $retval['item_param_opts_num'] = $params['opts'];
  564. $retval['item_param_opts_text'] = $params['opts'];
  565. // Get extra data
  566. if (! $all) {
  567. return $retval;
  568. }
  569. if ($retval['item_type'] === 'FUNCTION') {
  570. $retval['item_type_toggle'] = 'PROCEDURE';
  571. } else {
  572. $retval['item_type_toggle'] = 'FUNCTION';
  573. }
  574. $retval['item_returntype'] = '';
  575. $retval['item_returnlength'] = '';
  576. $retval['item_returnopts_num'] = '';
  577. $retval['item_returnopts_text'] = '';
  578. if (! empty($routine['DTD_IDENTIFIER'])) {
  579. $options = [];
  580. foreach ($stmt->return->options->options as $opt) {
  581. $options[] = is_string($opt) ? $opt : $opt['value'];
  582. }
  583. $retval['item_returntype'] = $stmt->return->name;
  584. $retval['item_returnlength'] = implode(',', $stmt->return->parameters);
  585. $retval['item_returnopts_num'] = implode(' ', $options);
  586. $retval['item_returnopts_text'] = implode(' ', $options);
  587. }
  588. $retval['item_definer'] = $stmt->options->has('DEFINER');
  589. $retval['item_definition'] = $routine['ROUTINE_DEFINITION'];
  590. $retval['item_isdeterministic'] = '';
  591. if ($routine['IS_DETERMINISTIC'] === 'YES') {
  592. $retval['item_isdeterministic'] = " checked='checked'";
  593. }
  594. $retval['item_securitytype_definer'] = '';
  595. $retval['item_securitytype_invoker'] = '';
  596. if ($routine['SECURITY_TYPE'] === 'DEFINER') {
  597. $retval['item_securitytype_definer'] = " selected='selected'";
  598. } elseif ($routine['SECURITY_TYPE'] === 'INVOKER') {
  599. $retval['item_securitytype_invoker'] = " selected='selected'";
  600. }
  601. $retval['item_sqldataaccess'] = $routine['SQL_DATA_ACCESS'];
  602. $retval['item_comment'] = $routine['ROUTINE_COMMENT'];
  603. return $retval;
  604. }
  605. /**
  606. * Creates one row for the parameter table used in the routine editor.
  607. *
  608. * @param array $routine Data for the routine returned by
  609. * getDataFromRequest() or getDataFromName()
  610. * @param mixed $index Either a numeric index of the row being processed
  611. * or NULL to create a template row for AJAX request
  612. * @param string $class Class used to hide the direction column, if the
  613. * row is for a stored function.
  614. *
  615. * @return string HTML code of one row of parameter table for the editor.
  616. */
  617. public function getParameterRow(array $routine = [], $index = null, $class = '')
  618. {
  619. if ($index === null) {
  620. // template row for AJAX request
  621. $i = 0;
  622. $index = '%s';
  623. $drop_class = '';
  624. $routine = [
  625. 'item_param_dir' => [0 => ''],
  626. 'item_param_name' => [0 => ''],
  627. 'item_param_type' => [0 => ''],
  628. 'item_param_length' => [0 => ''],
  629. 'item_param_opts_num' => [0 => ''],
  630. 'item_param_opts_text' => [0 => ''],
  631. ];
  632. } elseif (! empty($routine)) {
  633. // regular row for routine editor
  634. $drop_class = ' hide';
  635. $i = $index;
  636. } else {
  637. // No input data. This shouldn't happen,
  638. // but better be safe than sorry.
  639. return '';
  640. }
  641. $allCharsets = Charsets::getCharsets($this->dbi, $GLOBALS['cfg']['Server']['DisableIS']);
  642. $charsets = [];
  643. /** @var Charset $charset */
  644. foreach ($allCharsets as $charset) {
  645. $charsets[] = [
  646. 'name' => $charset->getName(),
  647. 'description' => $charset->getDescription(),
  648. 'is_selected' => $charset->getName() === $routine['item_param_opts_text'][$i],
  649. ];
  650. }
  651. return $this->template->render('database/routines/parameter_row', [
  652. 'class' => $class,
  653. 'index' => $index,
  654. 'param_directions' => $this->directions,
  655. 'param_opts_num' => $this->numericOptions,
  656. 'item_param_dir' => $routine['item_param_dir'][$i] ?? '',
  657. 'item_param_name' => $routine['item_param_name'][$i] ?? '',
  658. 'item_param_length' => $routine['item_param_length'][$i] ?? '',
  659. 'item_param_opts_num' => $routine['item_param_opts_num'][$i] ?? '',
  660. 'supported_datatypes' => Util::getSupportedDatatypes(
  661. true,
  662. $routine['item_param_type'][$i]
  663. ),
  664. 'charsets' => $charsets,
  665. 'drop_class' => $drop_class,
  666. ]);
  667. }
  668. /**
  669. * Displays a form used to add/edit a routine
  670. *
  671. * @param string $mode If the editor will be used to edit a routine
  672. * or add a new one: 'edit' or 'add'.
  673. * @param string $operation If the editor was previously invoked with
  674. * JS turned off, this will hold the name of
  675. * the current operation
  676. * @param array $routine Data for the routine returned by
  677. * getDataFromRequest() or getDataFromName()
  678. *
  679. * @return string HTML code for the editor.
  680. */
  681. public function getEditorForm($mode, $operation, array $routine)
  682. {
  683. global $db, $errors;
  684. // Escape special characters
  685. $need_escape = [
  686. 'item_original_name',
  687. 'item_name',
  688. 'item_returnlength',
  689. 'item_definition',
  690. 'item_definer',
  691. 'item_comment',
  692. ];
  693. foreach ($need_escape as $key => $index) {
  694. $routine[$index] = htmlentities($routine[$index], ENT_QUOTES, 'UTF-8');
  695. }
  696. for ($i = 0; $i < $routine['item_num_params']; $i++) {
  697. $routine['item_param_name'][$i] = htmlentities(
  698. $routine['item_param_name'][$i],
  699. ENT_QUOTES
  700. );
  701. $routine['item_param_length'][$i] = htmlentities(
  702. $routine['item_param_length'][$i],
  703. ENT_QUOTES
  704. );
  705. }
  706. // Handle some logic first
  707. if ($operation === 'change') {
  708. if ($routine['item_type'] === 'PROCEDURE') {
  709. $routine['item_type'] = 'FUNCTION';
  710. $routine['item_type_toggle'] = 'PROCEDURE';
  711. } else {
  712. $routine['item_type'] = 'PROCEDURE';
  713. $routine['item_type_toggle'] = 'FUNCTION';
  714. }
  715. } elseif ($operation === 'add'
  716. || ($routine['item_num_params'] == 0 && $mode === 'add' && ! $errors)
  717. ) {
  718. $routine['item_param_dir'][] = '';
  719. $routine['item_param_name'][] = '';
  720. $routine['item_param_type'][] = '';
  721. $routine['item_param_length'][] = '';
  722. $routine['item_param_opts_num'][] = '';
  723. $routine['item_param_opts_text'][] = '';
  724. $routine['item_num_params']++;
  725. } elseif ($operation === 'remove') {
  726. unset(
  727. $routine['item_param_dir'][$routine['item_num_params'] - 1],
  728. $routine['item_param_name'][$routine['item_num_params'] - 1],
  729. $routine['item_param_type'][$routine['item_num_params'] - 1],
  730. $routine['item_param_length'][$routine['item_num_params'] - 1],
  731. $routine['item_param_opts_num'][$routine['item_num_params'] - 1],
  732. $routine['item_param_opts_text'][$routine['item_num_params'] - 1]
  733. );
  734. $routine['item_num_params']--;
  735. }
  736. $disableRemoveParam = '';
  737. if (! $routine['item_num_params']) {
  738. $disableRemoveParam = " class='isdisableremoveparam_class' disabled=disabled";
  739. }
  740. $original_routine = '';
  741. if ($mode === 'edit') {
  742. $original_routine = "<input name='item_original_name' "
  743. . "type='hidden' "
  744. . "value='" . $routine['item_original_name'] . "'>\n"
  745. . "<input name='item_original_type' "
  746. . "type='hidden' "
  747. . "value='" . $routine['item_original_type'] . "'>\n";
  748. }
  749. $isfunction_class = '';
  750. $isprocedure_class = '';
  751. $isfunction_select = '';
  752. $isprocedure_select = '';
  753. if ($routine['item_type'] === 'PROCEDURE') {
  754. $isfunction_class = ' hide';
  755. $isprocedure_select = " selected='selected'";
  756. } else {
  757. $isprocedure_class = ' hide';
  758. $isfunction_select = " selected='selected'";
  759. }
  760. // Create the output
  761. $retval = '';
  762. $retval .= '<!-- START ' . mb_strtoupper($mode)
  763. . " ROUTINE FORM -->\n\n";
  764. $retval .= '<form class="rte_form" action="' . Url::getFromRoute('/database/routines')
  765. . '" method="post">' . "\n";
  766. $retval .= "<input name='" . $mode . "_item' type='hidden' value='1'>\n";
  767. $retval .= $original_routine;
  768. $retval .= Url::getHiddenInputs($db) . "\n";
  769. $retval .= "<fieldset>\n";
  770. $retval .= '<legend>' . __('Details') . "</legend>\n";
  771. $retval .= '<table class="rte_table table table-borderless table-sm">' . "\n";
  772. $retval .= "<tr>\n";
  773. $retval .= ' <td>' . __('Routine name') . "</td>\n";
  774. $retval .= " <td><input type='text' name='item_name' maxlength='64'\n";
  775. $retval .= " value='" . $routine['item_name'] . "'></td>\n";
  776. $retval .= "</tr>\n";
  777. $retval .= "<tr>\n";
  778. $retval .= ' <td>' . __('Type') . "</td>\n";
  779. $retval .= " <td>\n";
  780. if ($this->response->isAjax()) {
  781. $retval .= " <select name='item_type'>\n"
  782. . "<option value='PROCEDURE'" . $isprocedure_select . ">PROCEDURE</option>\n"
  783. . "<option value='FUNCTION'" . $isfunction_select . ">FUNCTION</option>\n"
  784. . "</select>\n";
  785. } else {
  786. $retval .= "<input name='item_type' type='hidden'"
  787. . " value='" . $routine['item_type'] . "'>\n"
  788. . "<div class='font_weight_bold text-center w-50'>\n"
  789. . $routine['item_type'] . "\n"
  790. . "</div>\n"
  791. . "<input type='submit' name='routine_changetype'\n"
  792. . " value='" . sprintf(__('Change to %s'), $routine['item_type_toggle'])
  793. . "'>\n";
  794. }
  795. $retval .= " </td>\n";
  796. $retval .= "</tr>\n";
  797. $retval .= "<tr>\n";
  798. $retval .= ' <td>' . __('Parameters') . "</td>\n";
  799. $retval .= " <td>\n";
  800. // parameter handling start
  801. $retval .= " <table class='routine_params_table table table-borderless table-sm'>\n";
  802. $retval .= " <thead>\n";
  803. $retval .= " <tr>\n";
  804. $retval .= " <td></td>\n";
  805. $retval .= " <th class='routine_direction_cell" . $isprocedure_class . "'>"
  806. . __('Direction') . "</th>\n";
  807. $retval .= ' <th>' . __('Name') . "</th>\n";
  808. $retval .= ' <th>' . __('Type') . "</th>\n";
  809. $retval .= ' <th>' . __('Length/Values') . "</th>\n";
  810. $retval .= " <th colspan='2'>" . __('Options') . "</th>\n";
  811. $retval .= " <th class='routine_param_remove hide'>&nbsp;</th>\n";
  812. $retval .= ' </tr>';
  813. $retval .= " </thead>\n";
  814. $retval .= " <tbody>\n";
  815. for ($i = 0; $i < $routine['item_num_params']; $i++) { // each parameter
  816. $retval .= $this->getParameterRow($routine, $i, $isprocedure_class);
  817. }
  818. $retval .= " </tbody>\n";
  819. $retval .= ' </table>';
  820. $retval .= ' </td>';
  821. $retval .= '</tr>';
  822. $retval .= '<tr>';
  823. $retval .= ' <td>&nbsp;</td>';
  824. $retval .= ' <td>';
  825. $retval .= ' <input type="button" class="btn btn-primary"';
  826. $retval .= " name='routine_addparameter'";
  827. $retval .= " value='" . __('Add parameter') . "'>";
  828. $retval .= ' <input ' . $disableRemoveParam . '';
  829. $retval .= " type='submit' ";
  830. $retval .= " name='routine_removeparameter'";
  831. $retval .= " value='" . __('Remove last parameter') . "'>";
  832. $retval .= ' </td>';
  833. $retval .= '</tr>';
  834. // parameter handling end
  835. $retval .= "<tr class='routine_return_row" . $isfunction_class . "'>";
  836. $retval .= ' <td>' . __('Return type') . '</td>';
  837. $retval .= " <td><select name='item_returntype'>";
  838. $retval .= Util::getSupportedDatatypes(true, $routine['item_returntype']);
  839. $retval .= ' </select></td>';
  840. $retval .= '</tr>';
  841. $retval .= "<tr class='routine_return_row" . $isfunction_class . "'>";
  842. $retval .= ' <td>' . __('Return length/values') . '</td>';
  843. $retval .= " <td><input type='text' name='item_returnlength'";
  844. $retval .= " value='" . $routine['item_returnlength'] . "'></td>";
  845. $retval .= " <td class='hide no_len'>---</td>";
  846. $retval .= '</tr>';
  847. $retval .= "<tr class='routine_return_row" . $isfunction_class . "'>";
  848. $retval .= ' <td>' . __('Return options') . '</td>';
  849. $retval .= ' <td><div>';
  850. $retval .= '<select lang="en" dir="ltr" name="item_returnopts_text">' . "\n";
  851. $retval .= '<option value="">' . __('Charset') . '</option>' . "\n";
  852. $retval .= '<option value=""></option>' . "\n";
  853. $charsets = Charsets::getCharsets($this->dbi, $GLOBALS['cfg']['Server']['DisableIS']);
  854. /** @var Charset $charset */
  855. foreach ($charsets as $charset) {
  856. $retval .= '<option value="' . $charset->getName()
  857. . '" title="' . $charset->getDescription() . '"'
  858. . ($routine['item_returnopts_text'] == $charset->getName() ? ' selected' : '') . '>'
  859. . $charset->getName() . '</option>' . "\n";
  860. }
  861. $retval .= '</select>' . "\n";
  862. $retval .= ' </div>';
  863. $retval .= " <div><select name='item_returnopts_num'>";
  864. $retval .= " <option value=''></option>";
  865. foreach ($this->numericOptions as $key => $value) {
  866. $selected = '';
  867. if (! empty($routine['item_returnopts_num'])
  868. && $routine['item_returnopts_num'] == $value
  869. ) {
  870. $selected = " selected='selected'";
  871. }
  872. $retval .= '<option' . $selected . '>' . $value . '</option>';
  873. }
  874. $retval .= ' </select></div>';
  875. $retval .= " <div class='hide no_opts'>---</div>";
  876. $retval .= '</td>';
  877. $retval .= '</tr>';
  878. $retval .= '<tr>';
  879. $retval .= ' <td>' . __('Definition') . '</td>';
  880. $retval .= " <td><textarea name='item_definition' rows='15' cols='40'>";
  881. $retval .= $routine['item_definition'];
  882. $retval .= '</textarea></td>';
  883. $retval .= '</tr>';
  884. $retval .= '<tr>';
  885. $retval .= ' <td>' . __('Is deterministic') . '</td>';
  886. $retval .= " <td><input type='checkbox' name='item_isdeterministic'"
  887. . $routine['item_isdeterministic'] . '></td>';
  888. $retval .= '</tr>';
  889. if (isset($_REQUEST['edit_item'])
  890. && ! empty($_REQUEST['edit_item'])
  891. ) {
  892. $retval .= '<tr>';
  893. $retval .= ' <td>' . __('Adjust privileges');
  894. $retval .= MySQLDocumentation::showDocumentation('faq', 'faq6-39');
  895. $retval .= '</td>';
  896. if ($GLOBALS['proc_priv']
  897. && $GLOBALS['is_reload_priv']
  898. ) {
  899. $retval .= " <td><input type='checkbox' "
  900. . "name='item_adjust_privileges' value='1' checked></td>";
  901. } else {
  902. $retval .= " <td><input type='checkbox' "
  903. . "name='item_adjust_privileges' value='1' title='" . __(
  904. 'You do not have sufficient privileges to perform this '
  905. . 'operation; Please refer to the documentation for more '
  906. . 'details'
  907. )
  908. . "' disabled></td>";
  909. }
  910. $retval .= '</tr>';
  911. }
  912. $retval .= '<tr>';
  913. $retval .= ' <td>' . __('Definer') . '</td>';
  914. $retval .= " <td><input type='text' name='item_definer'";
  915. $retval .= " value='" . $routine['item_definer'] . "'></td>";
  916. $retval .= '</tr>';
  917. $retval .= '<tr>';
  918. $retval .= ' <td>' . __('Security type') . '</td>';
  919. $retval .= " <td><select name='item_securitytype'>";
  920. $retval .= " <option value='DEFINER'"
  921. . $routine['item_securitytype_definer'] . '>DEFINER</option>';
  922. $retval .= " <option value='INVOKER'"
  923. . $routine['item_securitytype_invoker'] . '>INVOKER</option>';
  924. $retval .= ' </select></td>';
  925. $retval .= '</tr>';
  926. $retval .= '<tr>';
  927. $retval .= ' <td>' . __('SQL data access') . '</td>';
  928. $retval .= " <td><select name='item_sqldataaccess'>";
  929. foreach ($this->sqlDataAccess as $key => $value) {
  930. $selected = '';
  931. if ($routine['item_sqldataaccess'] == $value) {
  932. $selected = " selected='selected'";
  933. }
  934. $retval .= ' <option' . $selected . '>' . $value . '</option>';
  935. }
  936. $retval .= ' </select></td>';
  937. $retval .= '</tr>';
  938. $retval .= '<tr>';
  939. $retval .= ' <td>' . __('Comment') . '</td>';
  940. $retval .= " <td><input type='text' name='item_comment' maxlength='64'";
  941. $retval .= " value='" . $routine['item_comment'] . "'></td>";
  942. $retval .= '</tr>';
  943. $retval .= '</table>';
  944. $retval .= '</fieldset>';
  945. if ($this->response->isAjax()) {
  946. $retval .= "<input type='hidden' name='editor_process_" . $mode . "'";
  947. $retval .= " value='true'>";
  948. $retval .= "<input type='hidden' name='ajax_request' value='true'>";
  949. } else {
  950. $retval .= "<fieldset class='tblFooters'>";
  951. $retval .= " <input type='submit' name='editor_process_" . $mode . "'";
  952. $retval .= " value='" . __('Go') . "'>";
  953. $retval .= '</fieldset>';
  954. }
  955. $retval .= '</form>';
  956. $retval .= '<!-- END ' . mb_strtoupper($mode) . ' ROUTINE FORM -->';
  957. return $retval;
  958. }
  959. /**
  960. * Set the found errors and build the params
  961. *
  962. * @param string[] $itemParamName The parameter names
  963. * @param string[] $itemParamDir The direction parameter (see $this->directions)
  964. * @param array $itemParamType The parameter type
  965. * @param array $itemParamLength A length or not for the parameter
  966. * @param array $itemParamOpsText An optional charset for the parameter
  967. * @param array $itemParamOpsNum An optional parameter for a $itemParamType NUMBER
  968. * @param string $itemType The item type (PROCEDURE/FUNCTION)
  969. * @param bool $warnedAboutLength A boolean that will be switched if a the length warning is given
  970. */
  971. private function processParamsAndBuild(
  972. array $itemParamName,
  973. array $itemParamDir,
  974. array $itemParamType,
  975. array $itemParamLength,
  976. array $itemParamOpsText,
  977. array $itemParamOpsNum,
  978. string $itemType,
  979. bool &$warnedAboutLength
  980. ): string {
  981. global $errors, $dbi;
  982. $params = '';
  983. $warnedAboutDir = false;
  984. for ($i = 0, $nb = count($itemParamName); $i < $nb; $i++) {
  985. if (empty($itemParamName[$i])
  986. || empty($itemParamType[$i])
  987. ) {
  988. $errors[] = __(
  989. 'You must provide a name and a type for each routine parameter.'
  990. );
  991. break;
  992. }
  993. if ($itemType === 'PROCEDURE'
  994. && ! empty($itemParamDir[$i])
  995. && in_array($itemParamDir[$i], $this->directions)
  996. ) {
  997. $params .= $itemParamDir[$i] . ' '
  998. . Util::backquote($itemParamName[$i])
  999. . ' ' . $itemParamType[$i];
  1000. } elseif ($itemType === 'FUNCTION') {
  1001. $params .= Util::backquote($itemParamName[$i])
  1002. . ' ' . $itemParamType[$i];
  1003. } elseif (! $warnedAboutDir) {
  1004. $warnedAboutDir = true;
  1005. $errors[] = sprintf(
  1006. __('Invalid direction "%s" given for parameter.'),
  1007. htmlspecialchars($itemParamDir[$i])
  1008. );
  1009. }
  1010. if ($itemParamLength[$i] != ''
  1011. && ! preg_match(
  1012. '@^(DATE|TINYBLOB|TINYTEXT|BLOB|TEXT|'
  1013. . 'MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT|'
  1014. . 'SERIAL|BOOLEAN)$@i',
  1015. $itemParamType[$i]
  1016. )
  1017. ) {
  1018. $params .= '(' . $itemParamLength[$i] . ')';
  1019. } elseif ($itemParamLength[$i] == ''
  1020. && preg_match(
  1021. '@^(ENUM|SET|VARCHAR|VARBINARY)$@i',
  1022. $itemParamType[$i]
  1023. )
  1024. ) {
  1025. if (! $warnedAboutLength) {
  1026. $warnedAboutLength = true;
  1027. $errors[] = __(
  1028. 'You must provide length/values for routine parameters'
  1029. . ' of type ENUM, SET, VARCHAR and VARBINARY.'
  1030. );
  1031. }
  1032. }
  1033. if (! empty($itemParamOpsText[$i])) {
  1034. if ($dbi->types->getTypeClass($itemParamType[$i]) === 'CHAR') {
  1035. if (! in_array($itemParamType[$i], ['VARBINARY', 'BINARY'])) {
  1036. $params .= ' CHARSET '
  1037. . mb_strtolower(
  1038. $itemParamOpsText[$i]
  1039. );
  1040. }
  1041. }
  1042. }
  1043. if (! empty($itemParamOpsNum[$i])) {
  1044. if ($dbi->types->getTypeClass($itemParamType[$i]) === 'NUMBER') {
  1045. $params .= ' '
  1046. . mb_strtoupper(
  1047. $itemParamOpsNum[$i]
  1048. );
  1049. }
  1050. }
  1051. if ($i == count($itemParamName) - 1) {
  1052. continue;
  1053. }
  1054. $params .= ', ';
  1055. }
  1056. return $params;
  1057. }
  1058. /**
  1059. * Set the found errors and build the query
  1060. *
  1061. * @param string $query The existing query
  1062. * @param bool $warnedAboutLength If the length warning was given
  1063. */
  1064. private function processFunctionSpecificParameters(
  1065. string $query,
  1066. bool $warnedAboutLength
  1067. ): string {
  1068. global $errors, $dbi;
  1069. $itemReturnType = $_POST['item_returntype'] ?? null;
  1070. if (! empty($itemReturnType)
  1071. && in_array(
  1072. $itemReturnType,
  1073. Util::getSupportedDatatypes()
  1074. )
  1075. ) {
  1076. $query .= 'RETURNS ' . $itemReturnType;
  1077. } else {
  1078. $errors[] = __('You must provide a valid return type for the routine.');
  1079. }
  1080. if (! empty($_POST['item_returnlength'])
  1081. && ! preg_match(
  1082. '@^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|'
  1083. . 'MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT|SERIAL|BOOLEAN)$@i',
  1084. $itemReturnType
  1085. )
  1086. ) {
  1087. $query .= '(' . $_POST['item_returnlength'] . ')';
  1088. } elseif (empty($_POST['item_returnlength'])
  1089. && preg_match(
  1090. '@^(ENUM|SET|VARCHAR|VARBINARY)$@i',
  1091. $itemReturnType
  1092. )
  1093. ) {
  1094. if (! $warnedAboutLength) {
  1095. $errors[] = __(
  1096. 'You must provide length/values for routine parameters'
  1097. . ' of type ENUM, SET, VARCHAR and VARBINARY.'
  1098. );
  1099. }
  1100. }
  1101. if (! empty($_POST['item_returnopts_text'])) {
  1102. if ($dbi->types->getTypeClass($itemReturnType) === 'CHAR') {
  1103. $query .= ' CHARSET '
  1104. . mb_strtolower($_POST['item_returnopts_text']);
  1105. }
  1106. }
  1107. if (! empty($_POST['item_returnopts_num'])) {
  1108. if ($dbi->types->getTypeClass($itemReturnType) === 'NUMBER') {
  1109. $query .= ' '
  1110. . mb_strtoupper($_POST['item_returnopts_num']);
  1111. }
  1112. }
  1113. return $query . ' ';
  1114. }
  1115. /**
  1116. * Composes the query necessary to create a routine from an HTTP request.
  1117. *
  1118. * @return string The CREATE [ROUTINE | PROCEDURE] query.
  1119. */
  1120. public function getQueryFromRequest(): string
  1121. {
  1122. global $errors;
  1123. $itemType = $_POST['item_type'] ?? '';
  1124. $itemDefiner = $_POST['item_definer'] ?? '';
  1125. $itemName = $_POST['item_name'] ?? '';
  1126. $query = 'CREATE ';
  1127. if (! empty($itemDefiner)) {
  1128. if (mb_strpos($itemDefiner, '@') !== false) {
  1129. $arr = explode('@', $itemDefiner);
  1130. $do_backquote = true;
  1131. if (substr($arr[0], 0, 1) === '`'
  1132. && substr($arr[0], -1) === '`'
  1133. ) {
  1134. $do_backquote = false;
  1135. }
  1136. $query .= 'DEFINER=' . Util::backquote($arr[0], $do_backquote);
  1137. $do_backquote = true;
  1138. if (substr($arr[1], 0, 1) === '`'
  1139. && substr($arr[1], -1) === '`'
  1140. ) {
  1141. $do_backquote = false;
  1142. }
  1143. $query .= '@' . Util::backquote($arr[1], $do_backquote) . ' ';
  1144. } else {
  1145. $errors[] = __('The definer must be in the "username@hostname" format!');
  1146. }
  1147. }
  1148. if ($itemType === 'FUNCTION'
  1149. || $itemType === 'PROCEDURE'
  1150. ) {
  1151. $query .= $itemType . ' ';
  1152. } else {
  1153. $errors[] = sprintf(
  1154. __('Invalid routine type: "%s"'),
  1155. htmlspecialchars($itemType)
  1156. );
  1157. }
  1158. if (! empty($itemName)) {
  1159. $query .= Util::backquote($itemName);
  1160. } else {
  1161. $errors[] = __('You must provide a routine name!');
  1162. }
  1163. $warnedAboutLength = false;
  1164. $itemParamName = $_POST['item_param_name'] ?? '';
  1165. $itemParamType = $_POST['item_param_type'] ?? '';
  1166. $itemParamLength = $_POST['item_param_length'] ?? '';
  1167. $itemParamDir = (array) ($_POST['item_param_dir'] ?? []);
  1168. $itemParamOpsText = (array) ($_POST['item_param_opts_text'] ?? []);
  1169. $itemParamOpsNum = (array) ($_POST['item_param_opts_num'] ?? []);
  1170. $params = '';
  1171. if (! empty($itemParamName)
  1172. && ! empty($itemParamType)
  1173. && ! empty($itemParamLength)
  1174. && is_array($itemParamName)
  1175. && is_array($itemParamType)
  1176. && is_array($itemParamLength)
  1177. ) {
  1178. $params = $this->processParamsAndBuild(
  1179. $itemParamName,
  1180. $itemParamDir,
  1181. $itemParamType,
  1182. $itemParamLength,
  1183. $itemParamOpsText,
  1184. $itemParamOpsNum,
  1185. $itemType,
  1186. $warnedAboutLength// Will possibly be modified by the function
  1187. );
  1188. }
  1189. $query .= '(' . $params . ') ';
  1190. if ($itemType === 'FUNCTION') {
  1191. $query = $this->processFunctionSpecificParameters($query, $warnedAboutLength);
  1192. }
  1193. if (! empty($_POST['item_comment'])) {
  1194. $query .= "COMMENT '" . $this->dbi->escapeString($_POST['item_comment'])
  1195. . "' ";
  1196. }
  1197. if (isset($_POST['item_isdeterministic'])) {
  1198. $query .= 'DETERMINISTIC ';
  1199. } else {
  1200. $query .= 'NOT DETERMINISTIC ';
  1201. }
  1202. $itemSqlDataAccess = $_POST['item_sqldataaccess'] ?? '';
  1203. if (! empty($itemSqlDataAccess)
  1204. && in_array($itemSqlDataAccess, $this->sqlDataAccess)
  1205. ) {
  1206. $query .= $itemSqlDataAccess . ' ';
  1207. }
  1208. $itemSecurityType = $_POST['item_securitytype'] ?? '';
  1209. if (! empty($itemSecurityType)) {
  1210. if ($itemSecurityType === 'DEFINER'
  1211. || $itemSecurityType === 'INVOKER'
  1212. ) {
  1213. $query .= 'SQL SECURITY ' . $itemSecurityType . ' ';
  1214. }
  1215. }
  1216. $itemDefinition = $_POST['item_definition'] ?? '';
  1217. if (! empty($itemDefinition)) {
  1218. $query .= $itemDefinition;
  1219. } else {
  1220. $errors[] = __('You must provide a routine definition.');
  1221. }
  1222. return $query;
  1223. }
  1224. /**
  1225. * @see handleExecuteRoutine
  1226. *
  1227. * @param array $routine The routine params
  1228. *
  1229. * @return string[] The SQL queries / SQL query parts
  1230. */
  1231. private function getQueriesFromRoutineForm(array $routine): array
  1232. {
  1233. $queries = [];
  1234. $end_query = [];
  1235. $args = [];
  1236. $all_functions = $this->dbi->types->getAllFunctions();
  1237. for ($i = 0; $i < $routine['item_num_params']; $i++) {
  1238. if (isset($_POST['params'][$routine['item_param_name'][$i]])) {
  1239. $value = $_POST['params'][$routine['item_param_name'][$i]];
  1240. if (is_array($value)) { // is SET type
  1241. $value = implode(',', $value);
  1242. }
  1243. $value = $this->dbi->escapeString($value);
  1244. if (! empty($_POST['funcs'][$routine['item_param_name'][$i]])
  1245. && in_array(
  1246. $_POST['funcs'][$routine['item_param_name'][$i]],
  1247. $all_functions
  1248. )
  1249. ) {
  1250. $queries[] = 'SET @p' . $i . '='
  1251. . $_POST['funcs'][$routine['item_param_name'][$i]]
  1252. . "('" . $value . "');\n";
  1253. } else {
  1254. $queries[] = 'SET @p' . $i . "='" . $value . "';\n";
  1255. }
  1256. $args[] = '@p' . $i;
  1257. } else {
  1258. $args[] = '@p' . $i;
  1259. }
  1260. if ($routine['item_type'] !== 'PROCEDURE') {
  1261. continue;
  1262. }
  1263. if ($routine['item_param_dir'][$i] !== 'OUT'
  1264. && $routine['item_param_dir'][$i] !== 'INOUT'
  1265. ) {
  1266. continue;
  1267. }
  1268. $end_query[] = '@p' . $i . ' AS '
  1269. . Util::backquote($routine['item_param_name'][$i]);
  1270. }
  1271. if ($routine['item_type'] === 'PROCEDURE') {
  1272. $queries[] = 'CALL ' . Util::backquote($routine['item_name'])
  1273. . '(' . implode(', ', $args) . ");\n";
  1274. if (count($end_query)) {
  1275. $queries[] = 'SELECT ' . implode(', ', $end_query) . ";\n";
  1276. }
  1277. } else {
  1278. $queries[] = 'SELECT ' . Util::backquote($routine['item_name'])
  1279. . '(' . implode(', ', $args) . ') '
  1280. . 'AS ' . Util::backquote($routine['item_name'])
  1281. . ";\n";
  1282. }
  1283. return $queries;
  1284. }
  1285. private function handleExecuteRoutine(): void
  1286. {
  1287. global $db;
  1288. // Build the queries
  1289. $routine = $this->getDataFromName(
  1290. $_POST['item_name'],
  1291. $_POST['item_type'],
  1292. false
  1293. );
  1294. if ($routine === null) {
  1295. $message = __('Error in processing request:') . ' ';
  1296. $message .= sprintf(
  1297. __('No routine with name %1$s found in database %2$s.'),
  1298. htmlspecialchars(Util::backquote($_POST['item_name'])),
  1299. htmlspecialchars(Util::backquote($db))
  1300. );
  1301. $message = Message::error($message);
  1302. if ($this->response->isAjax()) {
  1303. $this->response->setRequestStatus(false);
  1304. $this->response->addJSON('message', $message);
  1305. exit;
  1306. }
  1307. echo $message->getDisplay();
  1308. unset($_POST);
  1309. //NOTE: Missing exit ?
  1310. }
  1311. $queries = is_array($routine) ? $this->getQueriesFromRoutineForm($routine) : [];
  1312. // Get all the queries as one SQL statement
  1313. $multiple_query = implode('', $queries);
  1314. $outcome = true;
  1315. $affected = 0;
  1316. // Execute query
  1317. if (! $this->dbi->tryMultiQuery($multiple_query)) {
  1318. $outcome = false;
  1319. }
  1320. // Generate output
  1321. $output = '';
  1322. $nbResultsetToDisplay = 0;
  1323. if ($outcome) {
  1324. // Pass the SQL queries through the "pretty printer"
  1325. $output = Generator::formatSql(implode("\n", $queries));
  1326. // Display results
  1327. $output .= '<fieldset><legend>';
  1328. $output .= sprintf(
  1329. __('Execution results of routine %s'),
  1330. Util::backquote(htmlspecialchars($routine['item_name']))
  1331. );
  1332. $output .= '</legend>';
  1333. do {
  1334. $result = $this->dbi->storeResult();
  1335. $num_rows = $this->dbi->numRows($result);
  1336. if (($result !== false) && ($num_rows > 0)) {
  1337. $output .= '<table class="pma-table"><tr>';
  1338. foreach ($this->dbi->getFieldsMeta($result) as $field) {
  1339. $output .= '<th>';
  1340. $output .= htmlspecialchars($field->name);
  1341. $output .= '</th>';
  1342. }
  1343. $output .= '</tr>';
  1344. while ($row = $this->dbi->fetchAssoc($result)) {
  1345. $output .= '<tr>' . $this->browseRow($row) . '</tr>';
  1346. }
  1347. $output .= '</table>';
  1348. $nbResultsetToDisplay++;
  1349. $affected = $num_rows;
  1350. }
  1351. if (! $this->dbi->moreResults()) {
  1352. break;
  1353. }
  1354. $output .= '<br>';
  1355. $this->dbi->freeResult($result);
  1356. $outcome = $this->dbi->nextResult();
  1357. } while ($outcome);
  1358. }
  1359. if ($outcome) {
  1360. $output .= '</fieldset>';
  1361. $message = __('Your SQL query has been executed successfully.');
  1362. if ($routine['item_type'] === 'PROCEDURE') {
  1363. $message .= '<br>';
  1364. // TODO : message need to be modified according to the
  1365. // output from the routine
  1366. $message .= sprintf(
  1367. _ngettext(
  1368. '%d row affected by the last statement inside the '
  1369. . 'procedure.',
  1370. '%d rows affected by the last statement inside the '
  1371. . 'procedure.',
  1372. (int) $affected
  1373. ),
  1374. $affected
  1375. );
  1376. }
  1377. $message = Message::success($message);
  1378. if ($nbResultsetToDisplay == 0) {
  1379. $notice = __(
  1380. 'MySQL returned an empty result set (i.e. zero rows).'
  1381. );
  1382. $output .= Message::notice($notice)->getDisplay();
  1383. }
  1384. } else {
  1385. $output = '';
  1386. $message = Message::error(
  1387. sprintf(
  1388. __('The following query has failed: "%s"'),
  1389. htmlspecialchars($multiple_query)
  1390. )
  1391. . '<br><br>'
  1392. . __('MySQL said: ') . $this->dbi->getError()
  1393. );
  1394. }
  1395. // Print/send output
  1396. if ($this->response->isAjax()) {
  1397. $this->response->setRequestStatus($message->isSuccess());
  1398. $this->response->addJSON('message', $message->getDisplay() . $output);
  1399. $this->response->addJSON('dialog', false);
  1400. exit;
  1401. }
  1402. echo $message->getDisplay() , $output;
  1403. if ($message->isError()) {
  1404. // At least one query has failed, so shouldn't
  1405. // execute any more queries, so we quit.
  1406. exit;
  1407. }
  1408. unset($_POST);
  1409. // Now deliberately fall through to displaying the routines list
  1410. }
  1411. /**
  1412. * Handles requests for executing a routine
  1413. *
  1414. * @return void
  1415. */
  1416. public function handleExecute()
  1417. {
  1418. global $db;
  1419. /**
  1420. * Handle all user requests other than the default of listing routines
  1421. */
  1422. if (! empty($_POST['execute_routine']) && ! empty($_POST['item_name'])) {
  1423. $this->handleExecuteRoutine();
  1424. } elseif (! empty($_GET['execute_dialog']) && ! empty($_GET['item_name'])) {
  1425. /**
  1426. * Display the execute form for a routine.
  1427. */
  1428. $routine = $this->getDataFromName(
  1429. $_GET['item_name'],
  1430. $_GET['item_type'],
  1431. true
  1432. );
  1433. if ($routine !== null) {
  1434. $form = $this->getExecuteForm($routine);
  1435. if ($this->response->isAjax()) {
  1436. $title = __('Execute routine') . ' ' . Util::backquote(
  1437. htmlentities($_GET['item_name'], ENT_QUOTES)
  1438. );
  1439. $this->response->addJSON('message', $form);
  1440. $this->response->addJSON('title', $title);
  1441. $this->response->addJSON('dialog', true);
  1442. } else {
  1443. echo "\n\n<h2>" . __('Execute routine') . "</h2>\n\n";
  1444. echo $form;
  1445. }
  1446. exit;
  1447. }
  1448. if ($this->response->isAjax()) {
  1449. $message = __('Error in processing request:') . ' ';
  1450. $message .= sprintf(
  1451. __('No routine with name %1$s found in database %2$s.'),
  1452. htmlspecialchars(Util::backquote($_GET['item_name'])),
  1453. htmlspecialchars(Util::backquote($db))
  1454. );
  1455. $message = Message::error($message);
  1456. $this->response->setRequestStatus(false);
  1457. $this->response->addJSON('message', $message);
  1458. exit;
  1459. }
  1460. }
  1461. }
  1462. /**
  1463. * Browse row array
  1464. *
  1465. * @param array $row Columns
  1466. */
  1467. private function browseRow(array $row): ?string
  1468. {
  1469. $output = null;
  1470. foreach ($row as $value) {
  1471. if ($value === null) {
  1472. $value = '<i>NULL</i>';
  1473. } else {
  1474. $value = htmlspecialchars($value);
  1475. }
  1476. $output .= '<td>' . $value . '</td>';
  1477. }
  1478. return $output;
  1479. }
  1480. /**
  1481. * Creates the HTML code that shows the routine execution dialog.
  1482. *
  1483. * @param array $routine Data for the routine returned by
  1484. * getDataFromName()
  1485. *
  1486. * @return string HTML code for the routine execution dialog.
  1487. */
  1488. public function getExecuteForm(array $routine): string
  1489. {
  1490. global $db, $cfg;
  1491. // Escape special characters
  1492. $routine['item_name'] = htmlentities($routine['item_name'], ENT_QUOTES);
  1493. for ($i = 0; $i < $routine['item_num_params']; $i++) {
  1494. $routine['item_param_name'][$i] = htmlentities(
  1495. $routine['item_param_name'][$i],
  1496. ENT_QUOTES
  1497. );
  1498. }
  1499. // Create the output
  1500. $retval = '';
  1501. $retval .= "<!-- START ROUTINE EXECUTE FORM -->\n\n";
  1502. $retval .= '<form action="' . Url::getFromRoute('/database/routines') . '" method="post"' . "\n";
  1503. $retval .= " class='rte_form ajax' onsubmit='return false'>\n";
  1504. $retval .= "<input type='hidden' name='item_name'\n";
  1505. $retval .= " value='" . $routine['item_name'] . "'>\n";
  1506. $retval .= "<input type='hidden' name='item_type'\n";
  1507. $retval .= " value='" . $routine['item_type'] . "'>\n";
  1508. $retval .= Url::getHiddenInputs($db) . "\n";
  1509. $retval .= "<fieldset>\n";
  1510. if (! $this->response->isAjax()) {
  1511. $retval .= '<legend>' . $routine['item_name'] . "</legend>\n";
  1512. $retval .= "<table class='pma-table rte_table'>\n";
  1513. $retval .= "<caption class='tblHeaders'>\n";
  1514. $retval .= __('Routine parameters');
  1515. $retval .= "</caption>\n";
  1516. } else {
  1517. $retval .= '<legend>' . __('Routine parameters') . "</legend>\n";
  1518. $retval .= "<table class='pma-table rte_table'>\n";
  1519. }
  1520. $retval .= "<tr>\n";
  1521. $retval .= '<th>' . __('Name') . "</th>\n";
  1522. $retval .= '<th>' . __('Type') . "</th>\n";
  1523. if ($cfg['ShowFunctionFields']) {
  1524. $retval .= '<th>' . __('Function') . "</th>\n";
  1525. }
  1526. $retval .= '<th>' . __('Value') . "</th>\n";
  1527. $retval .= "</tr>\n";
  1528. // Get a list of data types that are not yet supported.
  1529. $no_support_types = Util::unsupportedDatatypes();
  1530. for ($i = 0; $i < $routine['item_num_params']; $i++) { // Each parameter
  1531. if ($routine['item_type'] === 'PROCEDURE'
  1532. && $routine['item_param_dir'][$i] === 'OUT'
  1533. ) {
  1534. continue;
  1535. }
  1536. $retval .= "\n<tr>\n";
  1537. $retval .= '<td>' . $routine['item_param_name'][$i] . "</td>\n";
  1538. $retval .= '<td>' . $routine['item_param_type'][$i] . "</td>\n";
  1539. if ($cfg['ShowFunctionFields']) {
  1540. $retval .= "<td>\n";
  1541. if (stripos($routine['item_param_type'][$i], 'enum') !== false
  1542. || stripos($routine['item_param_type'][$i], 'set') !== false
  1543. || in_array(
  1544. mb_strtolower($routine['item_param_type'][$i]),
  1545. $no_support_types
  1546. )
  1547. ) {
  1548. $retval .= "--\n";
  1549. } else {
  1550. $field = [
  1551. 'True_Type' => mb_strtolower(
  1552. $routine['item_param_type'][$i]
  1553. ),
  1554. 'Type' => '',
  1555. 'Key' => '',
  1556. 'Field' => '',
  1557. 'Default' => '',
  1558. 'first_timestamp' => false,
  1559. ];
  1560. $retval .= "<select name='funcs["
  1561. . $routine['item_param_name'][$i] . "]'>";
  1562. $retval .= Generator::getFunctionsForField($field, false, []);
  1563. $retval .= '</select>';
  1564. }
  1565. $retval .= "</td>\n";
  1566. }
  1567. // Append a class to date/time fields so that
  1568. // jQuery can attach a datepicker to them
  1569. $class = '';
  1570. if ($routine['item_param_type'][$i] === 'DATETIME'
  1571. || $routine['item_param_type'][$i] === 'TIMESTAMP'
  1572. ) {
  1573. $class = 'datetimefield';
  1574. } elseif ($routine['item_param_type'][$i] === 'DATE') {
  1575. $class = 'datefield';
  1576. }
  1577. $retval .= "<td class='nowrap'>\n";
  1578. if (in_array($routine['item_param_type'][$i], ['ENUM', 'SET'])) {
  1579. if ($routine['item_param_type'][$i] === 'ENUM') {
  1580. $input_type = 'radio';
  1581. } else {
  1582. $input_type = 'checkbox';
  1583. }
  1584. foreach ($routine['item_param_length_arr'][$i] as $value) {
  1585. $value = htmlentities(Util::unQuote($value), ENT_QUOTES);
  1586. $retval .= "<input name='params["
  1587. . $routine['item_param_name'][$i] . "][]' "
  1588. . "value='" . $value . "' type='"
  1589. . $input_type . "'>"
  1590. . $value . "<br>\n";
  1591. }
  1592. } elseif (in_array(
  1593. mb_strtolower($routine['item_param_type'][$i]),
  1594. $no_support_types
  1595. )) {
  1596. $retval .= "\n";
  1597. } else {
  1598. $retval .= "<input class='" . $class . "' type='text' name='params["
  1599. . $routine['item_param_name'][$i] . "]'>\n";
  1600. }
  1601. $retval .= "</td>\n";
  1602. $retval .= "</tr>\n";
  1603. }
  1604. $retval .= "\n</table>\n";
  1605. if (! $this->response->isAjax()) {
  1606. $retval .= "</fieldset>\n\n";
  1607. $retval .= "<fieldset class='tblFooters'>\n";
  1608. $retval .= " <input type='submit' name='execute_routine'\n";
  1609. $retval .= " value='" . __('Go') . "'>\n";
  1610. $retval .= "</fieldset>\n";
  1611. } else {
  1612. $retval .= "<input type='hidden' name='execute_routine' value='true'>";
  1613. $retval .= "<input type='hidden' name='ajax_request' value='true'>";
  1614. }
  1615. $retval .= "</form>\n\n";
  1616. $retval .= "<!-- END ROUTINE EXECUTE FORM -->\n\n";
  1617. return $retval;
  1618. }
  1619. /**
  1620. * Creates the contents for a row in the list of routines
  1621. *
  1622. * @param array $routine An array of routine data
  1623. * @param string $rowClass Additional class
  1624. *
  1625. * @return string HTML code of a row for the list of routines
  1626. */
  1627. public function getRow(array $routine, $rowClass = '')
  1628. {
  1629. global $db, $table;
  1630. $sqlDrop = sprintf(
  1631. 'DROP %s IF EXISTS %s',
  1632. $routine['type'],
  1633. Util::backquote($routine['name'])
  1634. );
  1635. // this is for our purpose to decide whether to
  1636. // show the edit link or not, so we need the DEFINER for the routine
  1637. $where = 'ROUTINE_SCHEMA ' . Util::getCollateForIS() . '='
  1638. . "'" . $this->dbi->escapeString($db) . "' "
  1639. . "AND SPECIFIC_NAME='" . $this->dbi->escapeString($routine['name']) . "'"
  1640. . "AND ROUTINE_TYPE='" . $this->dbi->escapeString($routine['type']) . "'";
  1641. $query = 'SELECT `DEFINER` FROM INFORMATION_SCHEMA.ROUTINES WHERE ' . $where . ';';
  1642. $routineDefiner = $this->dbi->fetchValue($query);
  1643. $currentUser = $this->dbi->getCurrentUser();
  1644. $currentUserIsRoutineDefiner = $currentUser === $routineDefiner;
  1645. // Since editing a procedure involved dropping and recreating, check also for
  1646. // CREATE ROUTINE privilege to avoid lost procedures.
  1647. $hasCreateRoutine = Util::currentUserHasPrivilege('CREATE ROUTINE', $db);
  1648. $hasEditPrivilege = ($hasCreateRoutine && $currentUserIsRoutineDefiner)
  1649. || $this->dbi->isSuperUser();
  1650. $hasExportPrivilege = ($hasCreateRoutine && $currentUserIsRoutineDefiner)
  1651. || $this->dbi->isSuperUser();
  1652. $hasExecutePrivilege = Util::currentUserHasPrivilege('EXECUTE', $db)
  1653. || $currentUserIsRoutineDefiner;
  1654. // There is a problem with Util::currentUserHasPrivilege():
  1655. // it does not detect all kinds of privileges, for example
  1656. // a direct privilege on a specific routine. So, at this point,
  1657. // we show the Execute link, hoping that the user has the correct rights.
  1658. // Also, information_schema might be hiding the ROUTINE_DEFINITION
  1659. // but a routine with no input parameters can be nonetheless executed.
  1660. // Check if the routine has any input parameters. If it does,
  1661. // we will show a dialog to get values for these parameters,
  1662. // otherwise we can execute it directly.
  1663. $definition = $this->dbi->getDefinition(
  1664. $db,
  1665. $routine['type'],
  1666. $routine['name']
  1667. );
  1668. $executeAction = '';
  1669. if ($definition !== null) {
  1670. $parser = new Parser($definition);
  1671. /**
  1672. * @var CreateStatement $stmt
  1673. */
  1674. $stmt = $parser->statements[0];
  1675. $params = Routine::getParameters($stmt);
  1676. if ($hasExecutePrivilege) {
  1677. $executeAction = 'execute_routine';
  1678. for ($i = 0; $i < $params['num']; $i++) {
  1679. if ($routine['type'] === 'PROCEDURE'
  1680. && $params['dir'][$i] === 'OUT'
  1681. ) {
  1682. continue;
  1683. }
  1684. $executeAction = 'execute_dialog';
  1685. break;
  1686. }
  1687. }
  1688. }
  1689. return $this->template->render('database/routines/row', [
  1690. 'db' => $db,
  1691. 'table' => $table,
  1692. 'sql_drop' => $sqlDrop,
  1693. 'routine' => $routine,
  1694. 'row_class' => $rowClass,
  1695. 'has_edit_privilege' => $hasEditPrivilege,
  1696. 'has_export_privilege' => $hasExportPrivilege,
  1697. 'has_execute_privilege' => $hasExecutePrivilege,
  1698. 'execute_action' => $executeAction,
  1699. ]);
  1700. }
  1701. /**
  1702. * @param resource|bool $result Query result
  1703. * @param string $createStatement Query
  1704. * @param array $errors Errors
  1705. *
  1706. * @return array
  1707. */
  1708. private function checkResult($result, $createStatement, array $errors)
  1709. {
  1710. if ($result) {
  1711. return $errors;
  1712. }
  1713. // OMG, this is really bad! We dropped the query,
  1714. // failed to create a new one
  1715. // and now even the backup query does not execute!
  1716. // This should not happen, but we better handle
  1717. // this just in case.
  1718. $errors[] = __('Sorry, we failed to restore the dropped routine.') . '<br>'
  1719. . __('The backed up query was:')
  1720. . '"' . htmlspecialchars($createStatement) . '"<br>'
  1721. . __('MySQL said: ') . $this->dbi->getError();
  1722. return $errors;
  1723. }
  1724. public function export(): void
  1725. {
  1726. global $db;
  1727. if (empty($_GET['export_item']) || empty($_GET['item_name']) || empty($_GET['item_type'])) {
  1728. return;
  1729. }
  1730. if ($_GET['item_type'] !== 'FUNCTION' && $_GET['item_type'] !== 'PROCEDURE') {
  1731. return;
  1732. }
  1733. $routineDefinition = $this->dbi->getDefinition($db, $_GET['item_type'], $_GET['item_name']);
  1734. $exportData = false;
  1735. if ($routineDefinition !== null) {
  1736. $exportData = "DELIMITER $$\n" . $routineDefinition . "$$\nDELIMITER ;\n";
  1737. }
  1738. $itemName = htmlspecialchars(Util::backquote($_GET['item_name']));
  1739. if ($exportData !== false) {
  1740. $exportData = htmlspecialchars(trim($exportData));
  1741. $title = sprintf(__('Export of routine %s'), $itemName);
  1742. if ($this->response->isAjax()) {
  1743. $this->response->addJSON('message', $exportData);
  1744. $this->response->addJSON('title', $title);
  1745. exit;
  1746. }
  1747. $exportData = '<textarea cols="40" rows="15" style="width: 100%;">'
  1748. . $exportData . '</textarea>';
  1749. echo "<fieldset>\n" . '<legend>' . $title . "</legend>\n"
  1750. . $exportData . "</fieldset>\n";
  1751. return;
  1752. }
  1753. $message = sprintf(
  1754. __(
  1755. 'Error in processing request: No routine with name %1$s found in database %2$s.'
  1756. . ' You might be lacking the necessary privileges to view/export this routine.'
  1757. ),
  1758. $itemName,
  1759. htmlspecialchars(Util::backquote($db))
  1760. );
  1761. $message = Message::error($message);
  1762. if ($this->response->isAjax()) {
  1763. $this->response->setRequestStatus(false);
  1764. $this->response->addJSON('message', $message);
  1765. exit;
  1766. }
  1767. echo $message->getDisplay();
  1768. }
  1769. }