Routines.php 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Database;
  4. use PhpMyAdmin\Charsets;
  5. use PhpMyAdmin\DatabaseInterface;
  6. use PhpMyAdmin\Html\Generator;
  7. use PhpMyAdmin\Message;
  8. use PhpMyAdmin\ResponseRenderer;
  9. use PhpMyAdmin\SqlParser\Parser;
  10. use PhpMyAdmin\SqlParser\Statements\CreateStatement;
  11. use PhpMyAdmin\SqlParser\TokensList;
  12. use PhpMyAdmin\SqlParser\Utils\Routine;
  13. use PhpMyAdmin\Template;
  14. use PhpMyAdmin\Util;
  15. use function __;
  16. use function _ngettext;
  17. use function array_merge;
  18. use function count;
  19. use function explode;
  20. use function htmlentities;
  21. use function htmlspecialchars;
  22. use function implode;
  23. use function in_array;
  24. use function is_array;
  25. use function is_string;
  26. use function max;
  27. use function mb_strtolower;
  28. use function mb_strtoupper;
  29. use function preg_match;
  30. use function sprintf;
  31. use function str_contains;
  32. use function stripos;
  33. use function substr;
  34. use function trim;
  35. use const ENT_QUOTES;
  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 ResponseRenderer */
  52. private $response;
  53. /**
  54. * @param DatabaseInterface $dbi DatabaseInterface instance.
  55. * @param Template $template Template instance.
  56. * @param ResponseRenderer $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. public function handleEditor(): void
  68. {
  69. global $db, $errors;
  70. $errors = $this->handleRequestCreateOrEdit($errors, $db);
  71. /**
  72. * Display a form used to add/edit a routine, if necessary
  73. */
  74. // FIXME: this must be simpler than that
  75. if (
  76. ! count($errors)
  77. && ( ! empty($_POST['editor_process_add'])
  78. || ! empty($_POST['editor_process_edit'])
  79. || (empty($_REQUEST['add_item']) && empty($_REQUEST['edit_item'])
  80. && empty($_POST['routine_addparameter'])
  81. && empty($_POST['routine_removeparameter'])
  82. && empty($_POST['routine_changetype'])))
  83. ) {
  84. return;
  85. }
  86. // Handle requests to add/remove parameters and changing routine type
  87. // This is necessary when JS is disabled
  88. $operation = '';
  89. if (! empty($_POST['routine_addparameter'])) {
  90. $operation = 'add';
  91. } elseif (! empty($_POST['routine_removeparameter'])) {
  92. $operation = 'remove';
  93. } elseif (! empty($_POST['routine_changetype'])) {
  94. $operation = 'change';
  95. }
  96. // Get the data for the form (if any)
  97. $routine = null;
  98. $mode = null;
  99. $title = null;
  100. if (! empty($_REQUEST['add_item'])) {
  101. $title = __('Add routine');
  102. $routine = $this->getDataFromRequest();
  103. $mode = 'add';
  104. } elseif (! empty($_REQUEST['edit_item'])) {
  105. $title = __('Edit routine');
  106. if (! $operation && ! empty($_GET['item_name']) && empty($_POST['editor_process_edit'])) {
  107. $routine = $this->getDataFromName($_GET['item_name'], $_GET['item_type']);
  108. if ($routine !== null) {
  109. $routine['item_original_name'] = $routine['item_name'];
  110. $routine['item_original_type'] = $routine['item_type'];
  111. }
  112. } else {
  113. $routine = $this->getDataFromRequest();
  114. }
  115. $mode = 'edit';
  116. }
  117. if ($routine !== null) {
  118. // Show form
  119. $editor = $this->getEditorForm($mode, $operation, $routine);
  120. if ($this->response->isAjax()) {
  121. $this->response->addJSON('message', $editor);
  122. $this->response->addJSON('title', $title);
  123. $this->response->addJSON('paramTemplate', $this->getParameterRow());
  124. $this->response->addJSON('type', $routine['item_type']);
  125. } else {
  126. echo "\n\n<h2>" . $title . "</h2>\n\n" . $editor;
  127. }
  128. exit;
  129. }
  130. $message = __('Error in processing request:') . ' ';
  131. $message .= sprintf(
  132. __(
  133. 'No routine with name %1$s found in database %2$s. '
  134. . 'You might be lacking the necessary privileges to edit this routine.'
  135. ),
  136. htmlspecialchars(
  137. Util::backquote($_REQUEST['item_name'])
  138. ),
  139. htmlspecialchars(Util::backquote($db))
  140. );
  141. $message = Message::error($message);
  142. if ($this->response->isAjax()) {
  143. $this->response->setRequestStatus(false);
  144. $this->response->addJSON('message', $message);
  145. exit;
  146. }
  147. echo $message->getDisplay();
  148. }
  149. /**
  150. * Handle request to create or edit a routine
  151. *
  152. * @param array $errors Errors
  153. * @param string $db DB name
  154. *
  155. * @return array
  156. */
  157. public function handleRequestCreateOrEdit(array $errors, $db)
  158. {
  159. global $message;
  160. if (empty($_POST['editor_process_add']) && empty($_POST['editor_process_edit'])) {
  161. return $errors;
  162. }
  163. $sql_query = '';
  164. $routine_query = $this->getQueryFromRequest();
  165. // set by getQueryFromRequest()
  166. if (! count($errors)) {
  167. // Execute the created query
  168. if (! empty($_POST['editor_process_edit'])) {
  169. $isProcOrFunc = in_array(
  170. $_POST['item_original_type'],
  171. [
  172. 'PROCEDURE',
  173. 'FUNCTION',
  174. ]
  175. );
  176. if (! $isProcOrFunc) {
  177. $errors[] = sprintf(
  178. __('Invalid routine type: "%s"'),
  179. htmlspecialchars($_POST['item_original_type'])
  180. );
  181. } else {
  182. // Backup the old routine, in case something goes wrong
  183. $create_routine = $this->dbi->getDefinition(
  184. $db,
  185. $_POST['item_original_type'],
  186. $_POST['item_original_name']
  187. );
  188. $privilegesBackup = $this->backupPrivileges();
  189. $drop_routine = 'DROP ' . $_POST['item_original_type'] . ' '
  190. . Util::backquote($_POST['item_original_name'])
  191. . ";\n";
  192. $result = $this->dbi->tryQuery($drop_routine);
  193. if (! $result) {
  194. $errors[] = sprintf(
  195. __('The following query has failed: "%s"'),
  196. htmlspecialchars($drop_routine)
  197. )
  198. . '<br>'
  199. . __('MySQL said: ') . $this->dbi->getError();
  200. } else {
  201. [$newErrors, $message] = $this->create($routine_query, $create_routine, $privilegesBackup);
  202. if (empty($newErrors)) {
  203. $sql_query = $drop_routine . $routine_query;
  204. } else {
  205. $errors = array_merge($errors, $newErrors);
  206. }
  207. unset($newErrors);
  208. }
  209. }
  210. } else {
  211. // 'Add a new routine' mode
  212. $result = $this->dbi->tryQuery($routine_query);
  213. if (! $result) {
  214. $errors[] = sprintf(
  215. __('The following query has failed: "%s"'),
  216. htmlspecialchars($routine_query)
  217. )
  218. . '<br><br>'
  219. . __('MySQL said: ') . $this->dbi->getError();
  220. } else {
  221. $message = Message::success(
  222. __('Routine %1$s has been created.')
  223. );
  224. $message->addParam(
  225. Util::backquote($_POST['item_name'])
  226. );
  227. $sql_query = $routine_query;
  228. }
  229. }
  230. }
  231. if (count($errors)) {
  232. $message = Message::error(
  233. __(
  234. 'One or more errors have occurred while processing your request:'
  235. )
  236. );
  237. $message->addHtml('<ul>');
  238. foreach ($errors as $string) {
  239. $message->addHtml('<li>' . $string . '</li>');
  240. }
  241. $message->addHtml('</ul>');
  242. }
  243. $output = Generator::getMessage($message, $sql_query);
  244. if (! $this->response->isAjax()) {
  245. return $errors;
  246. }
  247. if (! $message->isSuccess()) {
  248. $this->response->setRequestStatus(false);
  249. $this->response->addJSON('message', $output);
  250. exit;
  251. }
  252. $routines = $this->dbi->getRoutines($db, $_POST['item_type'], $_POST['item_name']);
  253. $routine = $routines[0];
  254. $this->response->addJSON(
  255. 'name',
  256. htmlspecialchars(
  257. mb_strtoupper($_POST['item_name'])
  258. )
  259. );
  260. $this->response->addJSON('new_row', $this->getRow($routine));
  261. $this->response->addJSON('insert', ! empty($routine));
  262. $this->response->addJSON('message', $output);
  263. $this->response->addJSON('tableType', 'routines');
  264. exit;
  265. }
  266. /**
  267. * Backup the privileges
  268. *
  269. * @return array
  270. */
  271. public function backupPrivileges()
  272. {
  273. if (! $GLOBALS['proc_priv'] || ! $GLOBALS['is_reload_priv']) {
  274. return [];
  275. }
  276. // Backup the Old Privileges before dropping
  277. // if $_POST['item_adjust_privileges'] set
  278. if (! isset($_POST['item_adjust_privileges']) || empty($_POST['item_adjust_privileges'])) {
  279. return [];
  280. }
  281. $privilegesBackupQuery = 'SELECT * FROM ' . Util::backquote('mysql')
  282. . '.' . Util::backquote('procs_priv')
  283. . ' where Routine_name = "' . $_POST['item_original_name']
  284. . '" AND Routine_type = "' . $_POST['item_original_type']
  285. . '";';
  286. return $this->dbi->fetchResult($privilegesBackupQuery, 0);
  287. }
  288. /**
  289. * Create the routine
  290. *
  291. * @param string $routine_query Query to create routine
  292. * @param string $create_routine Query to restore routine
  293. * @param array $privilegesBackup Privileges backup
  294. *
  295. * @return array
  296. */
  297. public function create(
  298. $routine_query,
  299. $create_routine,
  300. array $privilegesBackup
  301. ) {
  302. $result = $this->dbi->tryQuery($routine_query);
  303. if (! $result) {
  304. $errors = [];
  305. $errors[] = sprintf(
  306. __('The following query has failed: "%s"'),
  307. htmlspecialchars($routine_query)
  308. )
  309. . '<br>'
  310. . __('MySQL said: ') . $this->dbi->getError();
  311. // We dropped the old routine,
  312. // but were unable to create the new one
  313. // Try to restore the backup query
  314. $result = $this->dbi->tryQuery($create_routine);
  315. if (! $result) {
  316. $errors = $this->checkResult($create_routine, $errors);
  317. }
  318. return [
  319. $errors,
  320. null,
  321. ];
  322. }
  323. // Default value
  324. $resultAdjust = false;
  325. if ($GLOBALS['proc_priv'] && $GLOBALS['is_reload_priv']) {
  326. // Insert all the previous privileges
  327. // but with the new name and the new type
  328. foreach ($privilegesBackup as $priv) {
  329. $adjustProcPrivilege = 'INSERT INTO '
  330. . Util::backquote('mysql') . '.'
  331. . Util::backquote('procs_priv')
  332. . ' VALUES("' . $priv[0] . '", "'
  333. . $priv[1] . '", "' . $priv[2] . '", "'
  334. . $_POST['item_name'] . '", "'
  335. . $_POST['item_type'] . '", "'
  336. . $priv[5] . '", "'
  337. . $priv[6] . '", "'
  338. . $priv[7] . '");';
  339. $this->dbi->query($adjustProcPrivilege);
  340. $resultAdjust = true;
  341. }
  342. }
  343. $message = $this->flushPrivileges($resultAdjust);
  344. return [
  345. [],
  346. $message,
  347. ];
  348. }
  349. /**
  350. * Flush privileges and get message
  351. *
  352. * @param bool $flushPrivileges Flush privileges
  353. *
  354. * @return Message
  355. */
  356. public function flushPrivileges($flushPrivileges)
  357. {
  358. if ($flushPrivileges) {
  359. // Flush the Privileges
  360. $this->dbi->tryQuery('FLUSH PRIVILEGES;');
  361. $message = Message::success(
  362. __(
  363. 'Routine %1$s has been modified. Privileges have been adjusted.'
  364. )
  365. );
  366. } else {
  367. $message = Message::success(
  368. __('Routine %1$s has been modified.')
  369. );
  370. }
  371. $message->addParam(
  372. Util::backquote($_POST['item_name'])
  373. );
  374. return $message;
  375. }
  376. /**
  377. * This function will generate the values that are required to
  378. * complete the editor form. It is especially necessary to handle
  379. * the 'Add another parameter', 'Remove last parameter' and
  380. * 'Change routine type' functionalities when JS is disabled.
  381. *
  382. * @return array Data necessary to create the routine editor.
  383. */
  384. public function getDataFromRequest()
  385. {
  386. $retval = [];
  387. $indices = [
  388. 'item_name',
  389. 'item_original_name',
  390. 'item_returnlength',
  391. 'item_returnopts_num',
  392. 'item_returnopts_text',
  393. 'item_definition',
  394. 'item_comment',
  395. 'item_definer',
  396. ];
  397. foreach ($indices as $index) {
  398. $retval[$index] = $_POST[$index] ?? '';
  399. }
  400. $retval['item_type'] = 'PROCEDURE';
  401. $retval['item_type_toggle'] = 'FUNCTION';
  402. if (isset($_POST['item_type']) && $_POST['item_type'] === 'FUNCTION') {
  403. $retval['item_type'] = 'FUNCTION';
  404. $retval['item_type_toggle'] = 'PROCEDURE';
  405. }
  406. $retval['item_original_type'] = 'PROCEDURE';
  407. if (isset($_POST['item_original_type']) && $_POST['item_original_type'] === 'FUNCTION') {
  408. $retval['item_original_type'] = 'FUNCTION';
  409. }
  410. $retval['item_num_params'] = 0;
  411. $retval['item_param_dir'] = [];
  412. $retval['item_param_name'] = [];
  413. $retval['item_param_type'] = [];
  414. $retval['item_param_length'] = [];
  415. $retval['item_param_opts_num'] = [];
  416. $retval['item_param_opts_text'] = [];
  417. if (
  418. isset($_POST['item_param_name'], $_POST['item_param_type'])
  419. && isset($_POST['item_param_length'])
  420. && isset($_POST['item_param_opts_num'])
  421. && isset($_POST['item_param_opts_text'])
  422. && is_array($_POST['item_param_name'])
  423. && is_array($_POST['item_param_type'])
  424. && is_array($_POST['item_param_length'])
  425. && is_array($_POST['item_param_opts_num'])
  426. && is_array($_POST['item_param_opts_text'])
  427. ) {
  428. if ($_POST['item_type'] === 'PROCEDURE') {
  429. $retval['item_param_dir'] = $_POST['item_param_dir'];
  430. foreach ($retval['item_param_dir'] as $key => $value) {
  431. if (in_array($value, $this->directions, true)) {
  432. continue;
  433. }
  434. $retval['item_param_dir'][$key] = '';
  435. }
  436. }
  437. $retval['item_param_name'] = $_POST['item_param_name'];
  438. $retval['item_param_type'] = $_POST['item_param_type'];
  439. foreach ($retval['item_param_type'] as $key => $value) {
  440. if (in_array($value, Util::getSupportedDatatypes(), true)) {
  441. continue;
  442. }
  443. $retval['item_param_type'][$key] = '';
  444. }
  445. $retval['item_param_length'] = $_POST['item_param_length'];
  446. $retval['item_param_opts_num'] = $_POST['item_param_opts_num'];
  447. $retval['item_param_opts_text'] = $_POST['item_param_opts_text'];
  448. $retval['item_num_params'] = max(
  449. count($retval['item_param_name']),
  450. count($retval['item_param_type']),
  451. count($retval['item_param_length']),
  452. count($retval['item_param_opts_num']),
  453. count($retval['item_param_opts_text'])
  454. );
  455. }
  456. $retval['item_returntype'] = '';
  457. if (isset($_POST['item_returntype']) && in_array($_POST['item_returntype'], Util::getSupportedDatatypes())) {
  458. $retval['item_returntype'] = $_POST['item_returntype'];
  459. }
  460. $retval['item_isdeterministic'] = '';
  461. if (isset($_POST['item_isdeterministic']) && mb_strtolower($_POST['item_isdeterministic']) === 'on') {
  462. $retval['item_isdeterministic'] = " checked='checked'";
  463. }
  464. $retval['item_securitytype_definer'] = '';
  465. $retval['item_securitytype_invoker'] = '';
  466. if (isset($_POST['item_securitytype'])) {
  467. if ($_POST['item_securitytype'] === 'DEFINER') {
  468. $retval['item_securitytype_definer'] = " selected='selected'";
  469. } elseif ($_POST['item_securitytype'] === 'INVOKER') {
  470. $retval['item_securitytype_invoker'] = " selected='selected'";
  471. }
  472. }
  473. $retval['item_sqldataaccess'] = '';
  474. if (isset($_POST['item_sqldataaccess']) && in_array($_POST['item_sqldataaccess'], $this->sqlDataAccess, true)) {
  475. $retval['item_sqldataaccess'] = $_POST['item_sqldataaccess'];
  476. }
  477. return $retval;
  478. }
  479. /**
  480. * This function will generate the values that are required to complete
  481. * the "Edit routine" form given the name of a routine.
  482. *
  483. * @param string $name The name of the routine.
  484. * @param string $type Type of routine (ROUTINE|PROCEDURE)
  485. * @param bool $all Whether to return all data or just the info about parameters.
  486. *
  487. * @return array|null Data necessary to create the routine editor.
  488. */
  489. public function getDataFromName($name, $type, $all = true): ?array
  490. {
  491. global $db;
  492. $retval = [];
  493. // Build and execute the query
  494. $fields = 'SPECIFIC_NAME, ROUTINE_TYPE, DTD_IDENTIFIER, '
  495. . 'ROUTINE_DEFINITION, IS_DETERMINISTIC, SQL_DATA_ACCESS, '
  496. . 'ROUTINE_COMMENT, SECURITY_TYPE';
  497. $where = 'ROUTINE_SCHEMA ' . Util::getCollateForIS() . '='
  498. . "'" . $this->dbi->escapeString($db) . "' "
  499. . "AND SPECIFIC_NAME='" . $this->dbi->escapeString($name) . "'"
  500. . "AND ROUTINE_TYPE='" . $this->dbi->escapeString($type) . "'";
  501. $query = 'SELECT ' . $fields . ' FROM INFORMATION_SCHEMA.ROUTINES WHERE ' . $where . ';';
  502. $routine = $this->dbi->fetchSingleRow($query);
  503. if (! $routine) {
  504. return null;
  505. }
  506. // Get required data
  507. $retval['item_name'] = $routine['SPECIFIC_NAME'];
  508. $retval['item_type'] = $routine['ROUTINE_TYPE'];
  509. $definition = $this->dbi->getDefinition($db, $routine['ROUTINE_TYPE'], $routine['SPECIFIC_NAME']);
  510. if ($definition === null) {
  511. return null;
  512. }
  513. $parser = new Parser('DELIMITER $$' . "\n" . $definition);
  514. /**
  515. * @var CreateStatement $stmt
  516. */
  517. $stmt = $parser->statements[0];
  518. // Do not use $routine['ROUTINE_DEFINITION'] because of a MySQL escaping issue: #15370
  519. $body = TokensList::build($stmt->body);
  520. if (empty($body)) {
  521. // Fallback just in case the parser fails
  522. $body = (string) $routine['ROUTINE_DEFINITION'];
  523. }
  524. $params = Routine::getParameters($stmt);
  525. $retval['item_num_params'] = $params['num'];
  526. $retval['item_param_dir'] = $params['dir'];
  527. $retval['item_param_name'] = $params['name'];
  528. $retval['item_param_type'] = $params['type'];
  529. $retval['item_param_length'] = $params['length'];
  530. $retval['item_param_length_arr'] = $params['length_arr'];
  531. $retval['item_param_opts_num'] = $params['opts'];
  532. $retval['item_param_opts_text'] = $params['opts'];
  533. // Get extra data
  534. if (! $all) {
  535. return $retval;
  536. }
  537. if ($retval['item_type'] === 'FUNCTION') {
  538. $retval['item_type_toggle'] = 'PROCEDURE';
  539. } else {
  540. $retval['item_type_toggle'] = 'FUNCTION';
  541. }
  542. $retval['item_returntype'] = '';
  543. $retval['item_returnlength'] = '';
  544. $retval['item_returnopts_num'] = '';
  545. $retval['item_returnopts_text'] = '';
  546. if (! empty($routine['DTD_IDENTIFIER'])) {
  547. $options = [];
  548. foreach ($stmt->return->options->options as $opt) {
  549. $options[] = is_string($opt) ? $opt : $opt['value'];
  550. }
  551. $retval['item_returntype'] = $stmt->return->name;
  552. $retval['item_returnlength'] = implode(',', $stmt->return->parameters);
  553. $retval['item_returnopts_num'] = implode(' ', $options);
  554. $retval['item_returnopts_text'] = implode(' ', $options);
  555. }
  556. $retval['item_definer'] = $stmt->options->has('DEFINER');
  557. $retval['item_definition'] = $body;
  558. $retval['item_isdeterministic'] = '';
  559. if ($routine['IS_DETERMINISTIC'] === 'YES') {
  560. $retval['item_isdeterministic'] = " checked='checked'";
  561. }
  562. $retval['item_securitytype_definer'] = '';
  563. $retval['item_securitytype_invoker'] = '';
  564. if ($routine['SECURITY_TYPE'] === 'DEFINER') {
  565. $retval['item_securitytype_definer'] = " selected='selected'";
  566. } elseif ($routine['SECURITY_TYPE'] === 'INVOKER') {
  567. $retval['item_securitytype_invoker'] = " selected='selected'";
  568. }
  569. $retval['item_sqldataaccess'] = $routine['SQL_DATA_ACCESS'];
  570. $retval['item_comment'] = $routine['ROUTINE_COMMENT'];
  571. return $retval;
  572. }
  573. /**
  574. * Creates one row for the parameter table used in the routine editor.
  575. *
  576. * @param array $routine Data for the routine returned by
  577. * getDataFromRequest() or getDataFromName()
  578. * @param mixed $index Either a numeric index of the row being processed
  579. * or NULL to create a template row for AJAX request
  580. * @param string $class Class used to hide the direction column, if the
  581. * row is for a stored function.
  582. *
  583. * @return string HTML code of one row of parameter table for the editor.
  584. */
  585. public function getParameterRow(array $routine = [], $index = null, $class = '')
  586. {
  587. if ($index === null) {
  588. // template row for AJAX request
  589. $i = 0;
  590. $index = '%s';
  591. $drop_class = '';
  592. $routine = [
  593. 'item_param_dir' => [0 => ''],
  594. 'item_param_name' => [0 => ''],
  595. 'item_param_type' => [0 => ''],
  596. 'item_param_length' => [0 => ''],
  597. 'item_param_opts_num' => [0 => ''],
  598. 'item_param_opts_text' => [0 => ''],
  599. ];
  600. } elseif (! empty($routine)) {
  601. // regular row for routine editor
  602. $drop_class = ' hide';
  603. $i = $index;
  604. } else {
  605. // No input data. This shouldn't happen,
  606. // but better be safe than sorry.
  607. return '';
  608. }
  609. $allCharsets = Charsets::getCharsets($this->dbi, $GLOBALS['cfg']['Server']['DisableIS']);
  610. $charsets = [];
  611. foreach ($allCharsets as $charset) {
  612. $charsets[] = [
  613. 'name' => $charset->getName(),
  614. 'description' => $charset->getDescription(),
  615. 'is_selected' => $charset->getName() === mb_strtolower($routine['item_param_opts_text'][$i]),
  616. ];
  617. }
  618. return $this->template->render('database/routines/parameter_row', [
  619. 'class' => $class,
  620. 'index' => $index,
  621. 'param_directions' => $this->directions,
  622. 'param_opts_num' => $this->numericOptions,
  623. 'item_param_dir' => $routine['item_param_dir'][$i] ?? '',
  624. 'item_param_name' => $routine['item_param_name'][$i] ?? '',
  625. 'item_param_length' => $routine['item_param_length'][$i] ?? '',
  626. 'item_param_opts_num' => $routine['item_param_opts_num'][$i] ?? '',
  627. 'supported_datatypes' => Generator::getSupportedDatatypes(
  628. $this->dbi->types->mapAliasToMysqlType($routine['item_param_type'][$i])
  629. ),
  630. 'charsets' => $charsets,
  631. 'drop_class' => $drop_class,
  632. ]);
  633. }
  634. /**
  635. * Displays a form used to add/edit a routine
  636. *
  637. * @param string $mode If the editor will be used to edit a routine
  638. * or add a new one: 'edit' or 'add'.
  639. * @param string $operation If the editor was previously invoked with
  640. * JS turned off, this will hold the name of
  641. * the current operation
  642. * @param array $routine Data for the routine returned by
  643. * getDataFromRequest() or getDataFromName()
  644. *
  645. * @return string HTML code for the editor.
  646. */
  647. public function getEditorForm($mode, $operation, array $routine)
  648. {
  649. global $db, $errors;
  650. for ($i = 0; $i < $routine['item_num_params']; $i++) {
  651. $routine['item_param_name'][$i] = htmlentities($routine['item_param_name'][$i], ENT_QUOTES);
  652. $routine['item_param_length'][$i] = htmlentities($routine['item_param_length'][$i], ENT_QUOTES);
  653. }
  654. // Handle some logic first
  655. if ($operation === 'change') {
  656. if ($routine['item_type'] === 'PROCEDURE') {
  657. $routine['item_type'] = 'FUNCTION';
  658. $routine['item_type_toggle'] = 'PROCEDURE';
  659. } else {
  660. $routine['item_type'] = 'PROCEDURE';
  661. $routine['item_type_toggle'] = 'FUNCTION';
  662. }
  663. } elseif ($operation === 'add' || ($routine['item_num_params'] == 0 && $mode === 'add' && ! $errors)) {
  664. $routine['item_param_dir'][] = '';
  665. $routine['item_param_name'][] = '';
  666. $routine['item_param_type'][] = '';
  667. $routine['item_param_length'][] = '';
  668. $routine['item_param_opts_num'][] = '';
  669. $routine['item_param_opts_text'][] = '';
  670. $routine['item_num_params']++;
  671. } elseif ($operation === 'remove') {
  672. unset(
  673. $routine['item_param_dir'][$routine['item_num_params'] - 1],
  674. $routine['item_param_name'][$routine['item_num_params'] - 1],
  675. $routine['item_param_type'][$routine['item_num_params'] - 1],
  676. $routine['item_param_length'][$routine['item_num_params'] - 1],
  677. $routine['item_param_opts_num'][$routine['item_num_params'] - 1],
  678. $routine['item_param_opts_text'][$routine['item_num_params'] - 1]
  679. );
  680. $routine['item_num_params']--;
  681. }
  682. $parameterRows = '';
  683. for ($i = 0; $i < $routine['item_num_params']; $i++) {
  684. $parameterRows .= $this->getParameterRow($routine, $i, $routine['item_type'] === 'FUNCTION' ? ' hide' : '');
  685. }
  686. $charsets = Charsets::getCharsets($this->dbi, $GLOBALS['cfg']['Server']['DisableIS']);
  687. return $this->template->render('database/routines/editor_form', [
  688. 'db' => $db,
  689. 'routine' => $routine,
  690. 'is_edit_mode' => $mode === 'edit',
  691. 'is_ajax' => $this->response->isAjax(),
  692. 'parameter_rows' => $parameterRows,
  693. 'charsets' => $charsets,
  694. 'numeric_options' => $this->numericOptions,
  695. 'has_privileges' => $GLOBALS['proc_priv'] && $GLOBALS['is_reload_priv'],
  696. 'sql_data_access' => $this->sqlDataAccess,
  697. ]);
  698. }
  699. /**
  700. * Set the found errors and build the params
  701. *
  702. * @param string[] $itemParamName The parameter names
  703. * @param string[] $itemParamDir The direction parameter (see $this->directions)
  704. * @param array $itemParamType The parameter type
  705. * @param array $itemParamLength A length or not for the parameter
  706. * @param array $itemParamOpsText An optional charset for the parameter
  707. * @param array $itemParamOpsNum An optional parameter for a $itemParamType NUMBER
  708. * @param string $itemType The item type (PROCEDURE/FUNCTION)
  709. * @param bool $warnedAboutLength A boolean that will be switched if a the length warning is given
  710. */
  711. private function processParamsAndBuild(
  712. array $itemParamName,
  713. array $itemParamDir,
  714. array $itemParamType,
  715. array $itemParamLength,
  716. array $itemParamOpsText,
  717. array $itemParamOpsNum,
  718. string $itemType,
  719. bool &$warnedAboutLength
  720. ): string {
  721. global $errors, $dbi;
  722. $params = '';
  723. $warnedAboutDir = false;
  724. for ($i = 0, $nb = count($itemParamName); $i < $nb; $i++) {
  725. if (empty($itemParamName[$i]) || empty($itemParamType[$i])) {
  726. $errors[] = __('You must provide a name and a type for each routine parameter.');
  727. break;
  728. }
  729. if (
  730. $itemType === 'PROCEDURE'
  731. && ! empty($itemParamDir[$i])
  732. && in_array($itemParamDir[$i], $this->directions)
  733. ) {
  734. $params .= $itemParamDir[$i] . ' '
  735. . Util::backquote($itemParamName[$i])
  736. . ' ' . $itemParamType[$i];
  737. } elseif ($itemType === 'FUNCTION') {
  738. $params .= Util::backquote($itemParamName[$i])
  739. . ' ' . $itemParamType[$i];
  740. } elseif (! $warnedAboutDir) {
  741. $warnedAboutDir = true;
  742. $errors[] = sprintf(
  743. __('Invalid direction "%s" given for parameter.'),
  744. htmlspecialchars($itemParamDir[$i])
  745. );
  746. }
  747. if (
  748. $itemParamLength[$i] != ''
  749. && ! preg_match(
  750. '@^(DATE|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT|SERIAL|BOOLEAN)$@i',
  751. $itemParamType[$i]
  752. )
  753. ) {
  754. $params .= '(' . $itemParamLength[$i] . ')';
  755. } elseif (
  756. $itemParamLength[$i] == ''
  757. && preg_match('@^(ENUM|SET|VARCHAR|VARBINARY)$@i', $itemParamType[$i])
  758. ) {
  759. if (! $warnedAboutLength) {
  760. $warnedAboutLength = true;
  761. $errors[] = __(
  762. 'You must provide length/values for routine parameters'
  763. . ' of type ENUM, SET, VARCHAR and VARBINARY.'
  764. );
  765. }
  766. }
  767. if (! empty($itemParamOpsText[$i])) {
  768. if ($dbi->types->getTypeClass($itemParamType[$i]) === 'CHAR') {
  769. if (! in_array($itemParamType[$i], ['VARBINARY', 'BINARY'])) {
  770. $params .= ' CHARSET '
  771. . mb_strtolower($itemParamOpsText[$i]);
  772. }
  773. }
  774. }
  775. if (! empty($itemParamOpsNum[$i])) {
  776. if ($dbi->types->getTypeClass($itemParamType[$i]) === 'NUMBER') {
  777. $params .= ' '
  778. . mb_strtoupper($itemParamOpsNum[$i]);
  779. }
  780. }
  781. if ($i == count($itemParamName) - 1) {
  782. continue;
  783. }
  784. $params .= ', ';
  785. }
  786. return $params;
  787. }
  788. /**
  789. * Set the found errors and build the query
  790. *
  791. * @param string $query The existing query
  792. * @param bool $warnedAboutLength If the length warning was given
  793. */
  794. private function processFunctionSpecificParameters(
  795. string $query,
  796. bool $warnedAboutLength
  797. ): string {
  798. global $errors, $dbi;
  799. $itemReturnType = $_POST['item_returntype'] ?? null;
  800. if (! empty($itemReturnType) && in_array($itemReturnType, Util::getSupportedDatatypes())) {
  801. $query .= 'RETURNS ' . $itemReturnType;
  802. } else {
  803. $errors[] = __('You must provide a valid return type for the routine.');
  804. }
  805. if (
  806. ! empty($_POST['item_returnlength'])
  807. && ! preg_match(
  808. '@^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|'
  809. . 'MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT|SERIAL|BOOLEAN)$@i',
  810. $itemReturnType
  811. )
  812. ) {
  813. $query .= '(' . $_POST['item_returnlength'] . ')';
  814. } elseif (
  815. empty($_POST['item_returnlength'])
  816. && preg_match('@^(ENUM|SET|VARCHAR|VARBINARY)$@i', $itemReturnType)
  817. ) {
  818. if (! $warnedAboutLength) {
  819. $errors[] = __(
  820. 'You must provide length/values for routine parameters of type ENUM, SET, VARCHAR and VARBINARY.'
  821. );
  822. }
  823. }
  824. if (! empty($_POST['item_returnopts_text'])) {
  825. if ($dbi->types->getTypeClass($itemReturnType) === 'CHAR') {
  826. $query .= ' CHARSET '
  827. . mb_strtolower($_POST['item_returnopts_text']);
  828. }
  829. }
  830. if (! empty($_POST['item_returnopts_num'])) {
  831. if ($dbi->types->getTypeClass($itemReturnType) === 'NUMBER') {
  832. $query .= ' '
  833. . mb_strtoupper($_POST['item_returnopts_num']);
  834. }
  835. }
  836. return $query . ' ';
  837. }
  838. /**
  839. * Composes the query necessary to create a routine from an HTTP request.
  840. *
  841. * @return string The CREATE [ROUTINE | PROCEDURE] query.
  842. */
  843. public function getQueryFromRequest(): string
  844. {
  845. global $errors;
  846. $itemType = $_POST['item_type'] ?? '';
  847. $itemDefiner = $_POST['item_definer'] ?? '';
  848. $itemName = $_POST['item_name'] ?? '';
  849. $query = 'CREATE ';
  850. if (! empty($itemDefiner)) {
  851. if (str_contains($itemDefiner, '@')) {
  852. $arr = explode('@', $itemDefiner);
  853. $do_backquote = true;
  854. if (substr($arr[0], 0, 1) === '`' && substr($arr[0], -1) === '`') {
  855. $do_backquote = false;
  856. }
  857. $query .= 'DEFINER=' . Util::backquoteCompat($arr[0], 'NONE', $do_backquote);
  858. $do_backquote = true;
  859. if (substr($arr[1], 0, 1) === '`' && substr($arr[1], -1) === '`') {
  860. $do_backquote = false;
  861. }
  862. $query .= '@' . Util::backquoteCompat($arr[1], 'NONE', $do_backquote) . ' ';
  863. } else {
  864. $errors[] = __('The definer must be in the "username@hostname" format!');
  865. }
  866. }
  867. if ($itemType === 'FUNCTION' || $itemType === 'PROCEDURE') {
  868. $query .= $itemType . ' ';
  869. } else {
  870. $errors[] = sprintf(
  871. __('Invalid routine type: "%s"'),
  872. htmlspecialchars($itemType)
  873. );
  874. }
  875. if (! empty($itemName)) {
  876. $query .= Util::backquote($itemName);
  877. } else {
  878. $errors[] = __('You must provide a routine name!');
  879. }
  880. $warnedAboutLength = false;
  881. $itemParamName = $_POST['item_param_name'] ?? '';
  882. $itemParamType = $_POST['item_param_type'] ?? '';
  883. $itemParamLength = $_POST['item_param_length'] ?? '';
  884. $itemParamDir = (array) ($_POST['item_param_dir'] ?? []);
  885. $itemParamOpsText = (array) ($_POST['item_param_opts_text'] ?? []);
  886. $itemParamOpsNum = (array) ($_POST['item_param_opts_num'] ?? []);
  887. $params = '';
  888. if (
  889. ! empty($itemParamName)
  890. && ! empty($itemParamType)
  891. && ! empty($itemParamLength)
  892. && is_array($itemParamName)
  893. && is_array($itemParamType)
  894. && is_array($itemParamLength)
  895. ) {
  896. $params = $this->processParamsAndBuild(
  897. $itemParamName,
  898. $itemParamDir,
  899. $itemParamType,
  900. $itemParamLength,
  901. $itemParamOpsText,
  902. $itemParamOpsNum,
  903. $itemType,
  904. $warnedAboutLength// Will possibly be modified by the function
  905. );
  906. }
  907. $query .= '(' . $params . ') ';
  908. if ($itemType === 'FUNCTION') {
  909. $query = $this->processFunctionSpecificParameters($query, $warnedAboutLength);
  910. }
  911. if (! empty($_POST['item_comment'])) {
  912. $query .= "COMMENT '" . $this->dbi->escapeString($_POST['item_comment'])
  913. . "' ";
  914. }
  915. if (isset($_POST['item_isdeterministic'])) {
  916. $query .= 'DETERMINISTIC ';
  917. } else {
  918. $query .= 'NOT DETERMINISTIC ';
  919. }
  920. $itemSqlDataAccess = $_POST['item_sqldataaccess'] ?? '';
  921. if (! empty($itemSqlDataAccess) && in_array($itemSqlDataAccess, $this->sqlDataAccess)) {
  922. $query .= $itemSqlDataAccess . ' ';
  923. }
  924. $itemSecurityType = $_POST['item_securitytype'] ?? '';
  925. if (! empty($itemSecurityType)) {
  926. if ($itemSecurityType === 'DEFINER' || $itemSecurityType === 'INVOKER') {
  927. $query .= 'SQL SECURITY ' . $itemSecurityType . ' ';
  928. }
  929. }
  930. $itemDefinition = $_POST['item_definition'] ?? '';
  931. if (! empty($itemDefinition)) {
  932. $query .= $itemDefinition;
  933. } else {
  934. $errors[] = __('You must provide a routine definition.');
  935. }
  936. return $query;
  937. }
  938. /**
  939. * @see handleExecuteRoutine
  940. *
  941. * @param array $routine The routine params
  942. *
  943. * @return string[] The SQL queries / SQL query parts
  944. */
  945. private function getQueriesFromRoutineForm(array $routine): array
  946. {
  947. $queries = [];
  948. $end_query = [];
  949. $args = [];
  950. $all_functions = $this->dbi->types->getAllFunctions();
  951. for ($i = 0; $i < $routine['item_num_params']; $i++) {
  952. if (isset($_POST['params'][$routine['item_param_name'][$i]])) {
  953. $value = $_POST['params'][$routine['item_param_name'][$i]];
  954. if (is_array($value)) { // is SET type
  955. $value = implode(',', $value);
  956. }
  957. $value = $this->dbi->escapeString($value);
  958. if (
  959. ! empty($_POST['funcs'][$routine['item_param_name'][$i]])
  960. && in_array($_POST['funcs'][$routine['item_param_name'][$i]], $all_functions)
  961. ) {
  962. $queries[] = 'SET @p' . $i . '='
  963. . $_POST['funcs'][$routine['item_param_name'][$i]]
  964. . "('" . $value . "');\n";
  965. } else {
  966. $queries[] = 'SET @p' . $i . "='" . $value . "';\n";
  967. }
  968. $args[] = '@p' . $i;
  969. } else {
  970. $args[] = '@p' . $i;
  971. }
  972. if ($routine['item_type'] !== 'PROCEDURE') {
  973. continue;
  974. }
  975. if ($routine['item_param_dir'][$i] !== 'OUT' && $routine['item_param_dir'][$i] !== 'INOUT') {
  976. continue;
  977. }
  978. $end_query[] = '@p' . $i . ' AS '
  979. . Util::backquote($routine['item_param_name'][$i]);
  980. }
  981. if ($routine['item_type'] === 'PROCEDURE') {
  982. $queries[] = 'CALL ' . Util::backquote($routine['item_name'])
  983. . '(' . implode(', ', $args) . ");\n";
  984. if (count($end_query)) {
  985. $queries[] = 'SELECT ' . implode(', ', $end_query) . ";\n";
  986. }
  987. } else {
  988. $queries[] = 'SELECT ' . Util::backquote($routine['item_name'])
  989. . '(' . implode(', ', $args) . ') '
  990. . 'AS ' . Util::backquote($routine['item_name'])
  991. . ";\n";
  992. }
  993. return $queries;
  994. }
  995. private function handleExecuteRoutine(): void
  996. {
  997. global $db;
  998. // Build the queries
  999. $routine = $this->getDataFromName($_POST['item_name'], $_POST['item_type'], false);
  1000. if ($routine === null) {
  1001. $message = __('Error in processing request:') . ' ';
  1002. $message .= sprintf(
  1003. __('No routine with name %1$s found in database %2$s.'),
  1004. htmlspecialchars(Util::backquote($_POST['item_name'])),
  1005. htmlspecialchars(Util::backquote($db))
  1006. );
  1007. $message = Message::error($message);
  1008. if ($this->response->isAjax()) {
  1009. $this->response->setRequestStatus(false);
  1010. $this->response->addJSON('message', $message);
  1011. exit;
  1012. }
  1013. echo $message->getDisplay();
  1014. unset($_POST);
  1015. //NOTE: Missing exit ?
  1016. }
  1017. $queries = is_array($routine) ? $this->getQueriesFromRoutineForm($routine) : [];
  1018. // Get all the queries as one SQL statement
  1019. $multiple_query = implode('', $queries);
  1020. $outcome = true;
  1021. $affected = 0;
  1022. // Execute query
  1023. if (! $this->dbi->tryMultiQuery($multiple_query)) {
  1024. $outcome = false;
  1025. }
  1026. // Generate output
  1027. $output = '';
  1028. $nbResultsetToDisplay = 0;
  1029. if ($outcome) {
  1030. // Pass the SQL queries through the "pretty printer"
  1031. $output = Generator::formatSql(implode("\n", $queries));
  1032. // Display results
  1033. $output .= '<div class="card my-3"><div class="card-header">';
  1034. $output .= sprintf(
  1035. __('Execution results of routine %s'),
  1036. Util::backquote(htmlspecialchars($routine['item_name']))
  1037. );
  1038. $output .= '</div><div class="card-body">';
  1039. do {
  1040. $result = $this->dbi->storeResult();
  1041. if ($result !== false && $result->numRows() > 0) {
  1042. $output .= '<table class="table table-striped w-auto"><tr>';
  1043. foreach ($result->getFieldNames() as $field) {
  1044. $output .= '<th>';
  1045. $output .= htmlspecialchars($field);
  1046. $output .= '</th>';
  1047. }
  1048. $output .= '</tr>';
  1049. foreach ($result as $row) {
  1050. $output .= '<tr>' . $this->browseRow($row) . '</tr>';
  1051. }
  1052. $output .= '</table>';
  1053. $nbResultsetToDisplay++;
  1054. $affected = $result->numRows();
  1055. }
  1056. if (! $this->dbi->moreResults()) {
  1057. break;
  1058. }
  1059. unset($result);
  1060. $outcome = $this->dbi->nextResult();
  1061. } while ($outcome);
  1062. }
  1063. if ($outcome) {
  1064. $output .= '</div></div>';
  1065. $message = __('Your SQL query has been executed successfully.');
  1066. if ($routine['item_type'] === 'PROCEDURE') {
  1067. $message .= '<br>';
  1068. // TODO : message need to be modified according to the
  1069. // output from the routine
  1070. $message .= sprintf(
  1071. _ngettext(
  1072. '%d row affected by the last statement inside the procedure.',
  1073. '%d rows affected by the last statement inside the procedure.',
  1074. (int) $affected
  1075. ),
  1076. $affected
  1077. );
  1078. }
  1079. $message = Message::success($message);
  1080. if ($nbResultsetToDisplay == 0) {
  1081. $notice = __('MySQL returned an empty result set (i.e. zero rows).');
  1082. $output .= Message::notice($notice)->getDisplay();
  1083. }
  1084. } else {
  1085. $output = '';
  1086. $message = Message::error(
  1087. sprintf(
  1088. __('The following query has failed: "%s"'),
  1089. htmlspecialchars($multiple_query)
  1090. )
  1091. . '<br><br>'
  1092. . __('MySQL said: ') . $this->dbi->getError()
  1093. );
  1094. }
  1095. // Print/send output
  1096. if ($this->response->isAjax()) {
  1097. $this->response->setRequestStatus($message->isSuccess());
  1098. $this->response->addJSON('message', $message->getDisplay() . $output);
  1099. $this->response->addJSON('dialog', false);
  1100. exit;
  1101. }
  1102. echo $message->getDisplay() , $output;
  1103. if ($message->isError()) {
  1104. // At least one query has failed, so shouldn't
  1105. // execute any more queries, so we quit.
  1106. exit;
  1107. }
  1108. unset($_POST);
  1109. // Now deliberately fall through to displaying the routines list
  1110. }
  1111. /**
  1112. * Handles requests for executing a routine
  1113. */
  1114. public function handleExecute(): void
  1115. {
  1116. global $db;
  1117. /**
  1118. * Handle all user requests other than the default of listing routines
  1119. */
  1120. if (! empty($_POST['execute_routine']) && ! empty($_POST['item_name'])) {
  1121. $this->handleExecuteRoutine();
  1122. } elseif (! empty($_GET['execute_dialog']) && ! empty($_GET['item_name'])) {
  1123. /**
  1124. * Display the execute form for a routine.
  1125. */
  1126. $routine = $this->getDataFromName($_GET['item_name'], $_GET['item_type'], true);
  1127. if ($routine !== null) {
  1128. $form = $this->getExecuteForm($routine);
  1129. if ($this->response->isAjax()) {
  1130. $title = __('Execute routine') . ' ' . Util::backquote(
  1131. htmlentities($_GET['item_name'], ENT_QUOTES)
  1132. );
  1133. $this->response->addJSON('message', $form);
  1134. $this->response->addJSON('title', $title);
  1135. $this->response->addJSON('dialog', true);
  1136. } else {
  1137. echo "\n\n<h2>" . __('Execute routine') . "</h2>\n\n";
  1138. echo $form;
  1139. }
  1140. exit;
  1141. }
  1142. if ($this->response->isAjax()) {
  1143. $message = __('Error in processing request:') . ' ';
  1144. $message .= sprintf(
  1145. __('No routine with name %1$s found in database %2$s.'),
  1146. htmlspecialchars(Util::backquote($_GET['item_name'])),
  1147. htmlspecialchars(Util::backquote($db))
  1148. );
  1149. $message = Message::error($message);
  1150. $this->response->setRequestStatus(false);
  1151. $this->response->addJSON('message', $message);
  1152. exit;
  1153. }
  1154. }
  1155. }
  1156. /**
  1157. * Browse row array
  1158. *
  1159. * @param array $row Columns
  1160. */
  1161. private function browseRow(array $row): ?string
  1162. {
  1163. $output = null;
  1164. foreach ($row as $value) {
  1165. if ($value === null) {
  1166. $value = '<i>NULL</i>';
  1167. } else {
  1168. $value = htmlspecialchars($value);
  1169. }
  1170. $output .= '<td>' . $value . '</td>';
  1171. }
  1172. return $output;
  1173. }
  1174. /**
  1175. * Creates the HTML code that shows the routine execution dialog.
  1176. *
  1177. * @param array $routine Data for the routine returned by
  1178. * getDataFromName()
  1179. *
  1180. * @return string HTML code for the routine execution dialog.
  1181. */
  1182. public function getExecuteForm(array $routine): string
  1183. {
  1184. global $db, $cfg;
  1185. // Escape special characters
  1186. $routine['item_name'] = htmlentities($routine['item_name'], ENT_QUOTES);
  1187. for ($i = 0; $i < $routine['item_num_params']; $i++) {
  1188. $routine['item_param_name'][$i] = htmlentities($routine['item_param_name'][$i], ENT_QUOTES);
  1189. }
  1190. $no_support_types = Util::unsupportedDatatypes();
  1191. $params = [];
  1192. $params['no_support_types'] = $no_support_types;
  1193. for ($i = 0; $i < $routine['item_num_params']; $i++) {
  1194. if ($routine['item_type'] === 'PROCEDURE' && $routine['item_param_dir'][$i] === 'OUT') {
  1195. continue;
  1196. }
  1197. if ($cfg['ShowFunctionFields']) {
  1198. if (
  1199. stripos($routine['item_param_type'][$i], 'enum') !== false
  1200. || stripos($routine['item_param_type'][$i], 'set') !== false
  1201. || in_array(
  1202. mb_strtolower($routine['item_param_type'][$i]),
  1203. $no_support_types
  1204. )
  1205. ) {
  1206. $params[$i]['generator'] = null;
  1207. } else {
  1208. $field = [
  1209. 'True_Type' => mb_strtolower($routine['item_param_type'][$i]),
  1210. 'Type' => '',
  1211. 'Key' => '',
  1212. 'Field' => '',
  1213. 'Default' => '',
  1214. 'first_timestamp' => false,
  1215. ];
  1216. $generator = Generator::getFunctionsForField($field, false, []);
  1217. $params[$i]['generator'] = $generator;
  1218. }
  1219. }
  1220. if ($routine['item_param_type'][$i] === 'DATETIME' || $routine['item_param_type'][$i] === 'TIMESTAMP') {
  1221. $params[$i]['class'] = 'datetimefield';
  1222. } elseif ($routine['item_param_type'][$i] === 'DATE') {
  1223. $params[$i]['class'] = 'datefield';
  1224. }
  1225. if (in_array($routine['item_param_type'][$i], ['ENUM', 'SET'])) {
  1226. if ($routine['item_param_type'][$i] === 'ENUM') {
  1227. $params[$i]['input_type'] = 'radio';
  1228. } else {
  1229. $params[$i]['input_type'] = 'checkbox';
  1230. }
  1231. foreach ($routine['item_param_length_arr'][$i] as $value) {
  1232. $value = htmlentities(Util::unQuote($value), ENT_QUOTES);
  1233. $params[$i]['htmlentities'][] = $value;
  1234. }
  1235. } elseif (in_array(mb_strtolower($routine['item_param_type'][$i]), $no_support_types)) {
  1236. $params[$i]['input_type'] = null;
  1237. } else {
  1238. $params[$i]['input_type'] = 'text';
  1239. }
  1240. }
  1241. return $this->template->render('database/routines/execute_form', [
  1242. 'db' => $db,
  1243. 'routine' => $routine,
  1244. 'ajax' => $this->response->isAjax(),
  1245. 'show_function_fields' => $cfg['ShowFunctionFields'],
  1246. 'params' => $params,
  1247. ]);
  1248. }
  1249. /**
  1250. * Creates the contents for a row in the list of routines
  1251. *
  1252. * @param array $routine An array of routine data
  1253. * @param string $rowClass Additional class
  1254. *
  1255. * @return string HTML code of a row for the list of routines
  1256. */
  1257. public function getRow(array $routine, $rowClass = '')
  1258. {
  1259. global $db, $table;
  1260. $sqlDrop = sprintf(
  1261. 'DROP %s IF EXISTS %s',
  1262. $routine['type'],
  1263. Util::backquote($routine['name'])
  1264. );
  1265. // this is for our purpose to decide whether to
  1266. // show the edit link or not, so we need the DEFINER for the routine
  1267. $where = 'ROUTINE_SCHEMA ' . Util::getCollateForIS() . '='
  1268. . "'" . $this->dbi->escapeString($db) . "' "
  1269. . "AND SPECIFIC_NAME='" . $this->dbi->escapeString($routine['name']) . "'"
  1270. . "AND ROUTINE_TYPE='" . $this->dbi->escapeString($routine['type']) . "'";
  1271. $query = 'SELECT `DEFINER` FROM INFORMATION_SCHEMA.ROUTINES WHERE ' . $where . ';';
  1272. $routineDefiner = $this->dbi->fetchValue($query);
  1273. $currentUser = $this->dbi->getCurrentUser();
  1274. $currentUserIsRoutineDefiner = $currentUser === $routineDefiner;
  1275. // Since editing a procedure involved dropping and recreating, check also for
  1276. // CREATE ROUTINE privilege to avoid lost procedures.
  1277. $hasCreateRoutine = Util::currentUserHasPrivilege('CREATE ROUTINE', $db);
  1278. $hasEditPrivilege = ($hasCreateRoutine && $currentUserIsRoutineDefiner)
  1279. || $this->dbi->isSuperUser();
  1280. $hasExportPrivilege = ($hasCreateRoutine && $currentUserIsRoutineDefiner)
  1281. || $this->dbi->isSuperUser();
  1282. $hasExecutePrivilege = Util::currentUserHasPrivilege('EXECUTE', $db)
  1283. || $currentUserIsRoutineDefiner;
  1284. // There is a problem with Util::currentUserHasPrivilege():
  1285. // it does not detect all kinds of privileges, for example
  1286. // a direct privilege on a specific routine. So, at this point,
  1287. // we show the Execute link, hoping that the user has the correct rights.
  1288. // Also, information_schema might be hiding the ROUTINE_DEFINITION
  1289. // but a routine with no input parameters can be nonetheless executed.
  1290. // Check if the routine has any input parameters. If it does,
  1291. // we will show a dialog to get values for these parameters,
  1292. // otherwise we can execute it directly.
  1293. $definition = $this->dbi->getDefinition($db, $routine['type'], $routine['name']);
  1294. $executeAction = '';
  1295. if ($definition !== null) {
  1296. $parser = new Parser('DELIMITER $$' . "\n" . $definition);
  1297. /**
  1298. * @var CreateStatement $stmt
  1299. */
  1300. $stmt = $parser->statements[0];
  1301. $params = Routine::getParameters($stmt);
  1302. if ($hasExecutePrivilege) {
  1303. $executeAction = 'execute_routine';
  1304. for ($i = 0; $i < $params['num']; $i++) {
  1305. if ($routine['type'] === 'PROCEDURE' && $params['dir'][$i] === 'OUT') {
  1306. continue;
  1307. }
  1308. $executeAction = 'execute_dialog';
  1309. break;
  1310. }
  1311. }
  1312. }
  1313. return $this->template->render('database/routines/row', [
  1314. 'db' => $db,
  1315. 'table' => $table,
  1316. 'sql_drop' => $sqlDrop,
  1317. 'routine' => $routine,
  1318. 'row_class' => $rowClass,
  1319. 'has_edit_privilege' => $hasEditPrivilege,
  1320. 'has_export_privilege' => $hasExportPrivilege,
  1321. 'has_execute_privilege' => $hasExecutePrivilege,
  1322. 'execute_action' => $executeAction,
  1323. ]);
  1324. }
  1325. /**
  1326. * @param string $createStatement Query
  1327. * @param array $errors Errors
  1328. *
  1329. * @return array
  1330. */
  1331. private function checkResult($createStatement, array $errors)
  1332. {
  1333. // OMG, this is really bad! We dropped the query,
  1334. // failed to create a new one
  1335. // and now even the backup query does not execute!
  1336. // This should not happen, but we better handle
  1337. // this just in case.
  1338. $errors[] = __('Sorry, we failed to restore the dropped routine.') . '<br>'
  1339. . __('The backed up query was:')
  1340. . '"' . htmlspecialchars($createStatement) . '"<br>'
  1341. . __('MySQL said: ') . $this->dbi->getError();
  1342. return $errors;
  1343. }
  1344. public function export(): void
  1345. {
  1346. global $db;
  1347. if (empty($_GET['export_item']) || empty($_GET['item_name']) || empty($_GET['item_type'])) {
  1348. return;
  1349. }
  1350. if ($_GET['item_type'] !== 'FUNCTION' && $_GET['item_type'] !== 'PROCEDURE') {
  1351. return;
  1352. }
  1353. $routineDefinition = $this->dbi->getDefinition($db, $_GET['item_type'], $_GET['item_name']);
  1354. $exportData = false;
  1355. if ($routineDefinition !== null) {
  1356. $exportData = "DELIMITER $$\n" . $routineDefinition . "$$\nDELIMITER ;\n";
  1357. }
  1358. $itemName = htmlspecialchars(Util::backquote($_GET['item_name']));
  1359. if ($exportData !== false) {
  1360. $exportData = htmlspecialchars(trim($exportData));
  1361. $title = sprintf(__('Export of routine %s'), $itemName);
  1362. if ($this->response->isAjax()) {
  1363. $this->response->addJSON('message', $exportData);
  1364. $this->response->addJSON('title', $title);
  1365. exit;
  1366. }
  1367. $output = '<div class="container">';
  1368. $output .= '<h2>' . $title . '</h2>';
  1369. $output .= '<div class="card"><div class="card-body">';
  1370. $output .= '<textarea rows="15" class="form-control">' . $exportData . '</textarea>';
  1371. $output .= '</div></div></div>';
  1372. $this->response->addHTML($output);
  1373. return;
  1374. }
  1375. $message = sprintf(
  1376. __(
  1377. 'Error in processing request: No routine with name %1$s found in database %2$s.'
  1378. . ' You might be lacking the necessary privileges to view/export this routine.'
  1379. ),
  1380. $itemName,
  1381. htmlspecialchars(Util::backquote($db))
  1382. );
  1383. $message = Message::error($message);
  1384. if ($this->response->isAjax()) {
  1385. $this->response->setRequestStatus(false);
  1386. $this->response->addJSON('message', $message);
  1387. exit;
  1388. }
  1389. $this->response->addHTML($message->getDisplay());
  1390. }
  1391. }