From 04d6d5ca99ebfd1cebb8ce06618fb3811fc1a8aa Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 9 Jan 2020 10:55:03 +0100 Subject: phpmyadmin working --- .../classes/Plugins/Auth/AuthenticationConfig.php | 172 ++ .../classes/Plugins/Auth/AuthenticationCookie.php | 964 +++++++ .../classes/Plugins/Auth/AuthenticationHttp.php | 214 ++ .../classes/Plugins/Auth/AuthenticationSignon.php | 282 ++ .../classes/Plugins/AuthenticationPlugin.php | 371 +++ .../classes/Plugins/Export/ExportCodegen.php | 447 +++ .../libraries/classes/Plugins/Export/ExportCsv.php | 347 +++ .../classes/Plugins/Export/ExportExcel.php | 90 + .../classes/Plugins/Export/ExportHtmlword.php | 670 +++++ .../classes/Plugins/Export/ExportJson.php | 295 ++ .../classes/Plugins/Export/ExportLatex.php | 709 +++++ .../classes/Plugins/Export/ExportMediawiki.php | 386 +++ .../libraries/classes/Plugins/Export/ExportOds.php | 345 +++ .../libraries/classes/Plugins/Export/ExportOdt.php | 813 ++++++ .../libraries/classes/Plugins/Export/ExportPdf.php | 395 +++ .../classes/Plugins/Export/ExportPhparray.php | 259 ++ .../libraries/classes/Plugins/Export/ExportSql.php | 2915 ++++++++++++++++++++ .../classes/Plugins/Export/ExportTexytext.php | 624 +++++ .../libraries/classes/Plugins/Export/ExportXml.php | 593 ++++ .../classes/Plugins/Export/ExportYaml.php | 230 ++ .../classes/Plugins/Export/Helpers/Pdf.php | 855 ++++++ .../Plugins/Export/Helpers/TableProperty.php | 277 ++ .../libraries/classes/Plugins/Export/README | 255 ++ .../libraries/classes/Plugins/ExportPlugin.php | 386 +++ .../classes/Plugins/IOTransformationsPlugin.php | 98 + .../classes/Plugins/Import/AbstractImportCsv.php | 94 + .../libraries/classes/Plugins/Import/ImportCsv.php | 818 ++++++ .../libraries/classes/Plugins/Import/ImportLdi.php | 176 ++ .../classes/Plugins/Import/ImportMediawiki.php | 604 ++++ .../libraries/classes/Plugins/Import/ImportOds.php | 427 +++ .../libraries/classes/Plugins/Import/ImportShp.php | 335 +++ .../libraries/classes/Plugins/Import/ImportSql.php | 200 ++ .../libraries/classes/Plugins/Import/ImportXml.php | 375 +++ .../libraries/classes/Plugins/Import/README | 156 ++ .../classes/Plugins/Import/ShapeFileImport.php | 46 + .../classes/Plugins/Import/Upload/UploadApc.php | 83 + .../Plugins/Import/Upload/UploadNoplugin.php | 60 + .../Plugins/Import/Upload/UploadProgress.php | 97 + .../Plugins/Import/Upload/UploadSession.php | 95 + .../libraries/classes/Plugins/ImportPlugin.php | 96 + .../libraries/classes/Plugins/Schema/Dia/Dia.php | 190 ++ .../Plugins/Schema/Dia/DiaRelationSchema.php | 238 ++ .../Plugins/Schema/Dia/RelationStatsDia.php | 228 ++ .../classes/Plugins/Schema/Dia/TableStatsDia.php | 231 ++ .../libraries/classes/Plugins/Schema/Eps/Eps.php | 280 ++ .../Plugins/Schema/Eps/EpsRelationSchema.php | 254 ++ .../Plugins/Schema/Eps/RelationStatsEps.php | 120 + .../classes/Plugins/Schema/Eps/TableStatsEps.php | 183 ++ .../Plugins/Schema/ExportRelationSchema.php | 310 +++ .../libraries/classes/Plugins/Schema/Pdf/Pdf.php | 422 +++ .../Plugins/Schema/Pdf/PdfRelationSchema.php | 798 ++++++ .../Plugins/Schema/Pdf/RelationStatsPdf.php | 163 ++ .../classes/Plugins/Schema/Pdf/TableStatsPdf.php | 233 ++ .../classes/Plugins/Schema/RelationStats.php | 120 + .../libraries/classes/Plugins/Schema/SchemaDia.php | 100 + .../libraries/classes/Plugins/Schema/SchemaEps.php | 101 + .../libraries/classes/Plugins/Schema/SchemaPdf.php | 133 + .../libraries/classes/Plugins/Schema/SchemaSvg.php | 88 + .../Plugins/Schema/Svg/RelationStatsSvg.php | 140 + .../libraries/classes/Plugins/Schema/Svg/Svg.php | 281 ++ .../Plugins/Schema/Svg/SvgRelationSchema.php | 284 ++ .../classes/Plugins/Schema/Svg/TableStatsSvg.php | 204 ++ .../classes/Plugins/Schema/TableStats.php | 208 ++ .../libraries/classes/Plugins/SchemaPlugin.php | 90 + .../Abs/Bool2TextTransformationsPlugin.php | 69 + .../Abs/CodeMirrorEditorTransformationPlugin.php | 75 + .../Abs/DateFormatTransformationsPlugin.php | 158 ++ .../Abs/DownloadTransformationsPlugin.php | 93 + .../Abs/ExternalTransformationsPlugin.php | 160 ++ .../Abs/FormattedTransformationsPlugin.php | 65 + .../Abs/HexTransformationsPlugin.php | 71 + .../Abs/ImageLinkTransformationsPlugin.php | 63 + .../Abs/ImageUploadTransformationsPlugin.php | 121 + .../Abs/InlineTransformationsPlugin.php | 78 + .../Abs/LongToIPv4TransformationsPlugin.php | 66 + .../Abs/PreApPendTransformationsPlugin.php | 68 + .../Abs/RegexValidationTransformationsPlugin.php | 74 + .../Abs/SQLTransformationsPlugin.php | 62 + .../Abs/SubstringTransformationsPlugin.php | 93 + .../Abs/TextFileUploadTransformationsPlugin.php | 103 + .../Abs/TextImageLinkTransformationsPlugin.php | 75 + .../Abs/TextLinkTransformationsPlugin.php | 77 + .../Transformations/Input/Image_JPEG_Upload.php | 44 + .../Input/Text_Plain_FileUpload.php | 43 + .../Input/Text_Plain_Iptobinary.php | 141 + .../Input/Text_Plain_JsonEditor.php | 85 + .../Input/Text_Plain_RegexValidation.php | 44 + .../Transformations/Input/Text_Plain_SqlEditor.php | 85 + .../Transformations/Input/Text_Plain_XmlEditor.php | 85 + .../Output/Application_Octetstream_Download.php | 43 + .../Output/Application_Octetstream_Hex.php | 43 + .../Transformations/Output/Image_JPEG_Inline.php | 43 + .../Transformations/Output/Image_JPEG_Link.php | 43 + .../Transformations/Output/Image_PNG_Inline.php | 43 + .../Output/Text_Octetstream_Sql.php | 43 + .../Output/Text_Plain_Binarytoip.php | 97 + .../Output/Text_Plain_Bool2Text.php | 45 + .../Output/Text_Plain_Dateformat.php | 43 + .../Transformations/Output/Text_Plain_External.php | 43 + .../Output/Text_Plain_Formatted.php | 43 + .../Output/Text_Plain_Imagelink.php | 43 + .../Transformations/Output/Text_Plain_Json.php | 101 + .../Transformations/Output/Text_Plain_Sql.php | 60 + .../Transformations/Output/Text_Plain_Xml.php | 101 + .../classes/Plugins/Transformations/README | 4 + .../classes/Plugins/Transformations/TEMPLATE | 45 + .../Plugins/Transformations/TEMPLATE_ABSTRACT | 73 + .../Plugins/Transformations/Text_Plain_Link.php | 43 + .../Transformations/Text_Plain_Longtoipv4.php | 43 + .../Transformations/Text_Plain_PreApPend.php | 44 + .../Transformations/Text_Plain_Substring.php | 43 + .../classes/Plugins/TransformationsInterface.php | 47 + .../classes/Plugins/TransformationsPlugin.php | 69 + .../classes/Plugins/TwoFactor/Application.php | 162 ++ .../classes/Plugins/TwoFactor/Invalid.php | 68 + .../libraries/classes/Plugins/TwoFactor/Key.php | 213 ++ .../libraries/classes/Plugins/TwoFactor/Simple.php | 68 + .../libraries/classes/Plugins/TwoFactorPlugin.php | 183 ++ .../libraries/classes/Plugins/UploadInterface.php | 35 + 119 files changed, 26125 insertions(+) create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Auth/AuthenticationConfig.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Auth/AuthenticationCookie.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Auth/AuthenticationHttp.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Auth/AuthenticationSignon.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/AuthenticationPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportCodegen.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportCsv.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportExcel.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportHtmlword.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportJson.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportLatex.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportMediawiki.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportOds.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportOdt.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportPdf.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportPhparray.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportSql.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportTexytext.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportXml.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportYaml.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Export/Helpers/Pdf.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Export/Helpers/TableProperty.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Export/README create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/ExportPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/IOTransformationsPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Import/AbstractImportCsv.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportCsv.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportLdi.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportMediawiki.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportOds.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportShp.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportSql.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportXml.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Import/README create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Import/ShapeFileImport.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Import/Upload/UploadApc.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Import/Upload/UploadNoplugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Import/Upload/UploadProgress.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Import/Upload/UploadSession.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/ImportPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/Dia/Dia.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/Dia/DiaRelationSchema.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/Dia/RelationStatsDia.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/Dia/TableStatsDia.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/Eps/Eps.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/Eps/EpsRelationSchema.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/Eps/RelationStatsEps.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/Eps/TableStatsEps.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/ExportRelationSchema.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/Pdf/Pdf.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/Pdf/PdfRelationSchema.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/Pdf/RelationStatsPdf.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/Pdf/TableStatsPdf.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/RelationStats.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/SchemaDia.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/SchemaEps.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/SchemaPdf.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/SchemaSvg.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/Svg/RelationStatsSvg.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/Svg/Svg.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/Svg/SvgRelationSchema.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/Svg/TableStatsSvg.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Schema/TableStats.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/SchemaPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/Bool2TextTransformationsPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/CodeMirrorEditorTransformationPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/DateFormatTransformationsPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/DownloadTransformationsPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/ExternalTransformationsPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/FormattedTransformationsPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/HexTransformationsPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/ImageLinkTransformationsPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/ImageUploadTransformationsPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/InlineTransformationsPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/LongToIPv4TransformationsPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/PreApPendTransformationsPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/RegexValidationTransformationsPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/SQLTransformationsPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/SubstringTransformationsPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/TextFileUploadTransformationsPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/TextImageLinkTransformationsPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/TextLinkTransformationsPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Input/Image_JPEG_Upload.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Input/Text_Plain_FileUpload.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Input/Text_Plain_Iptobinary.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Input/Text_Plain_JsonEditor.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Input/Text_Plain_RegexValidation.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Input/Text_Plain_SqlEditor.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Input/Text_Plain_XmlEditor.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Output/Application_Octetstream_Download.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Output/Application_Octetstream_Hex.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Output/Image_JPEG_Inline.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Output/Image_JPEG_Link.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Output/Image_PNG_Inline.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Output/Text_Octetstream_Sql.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Output/Text_Plain_Binarytoip.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Output/Text_Plain_Bool2Text.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Output/Text_Plain_Dateformat.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Output/Text_Plain_External.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Output/Text_Plain_Formatted.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Output/Text_Plain_Imagelink.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Output/Text_Plain_Json.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Output/Text_Plain_Sql.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Output/Text_Plain_Xml.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/README create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/TEMPLATE create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/TEMPLATE_ABSTRACT create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Text_Plain_Link.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Text_Plain_Longtoipv4.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Text_Plain_PreApPend.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Text_Plain_Substring.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/TransformationsInterface.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/TransformationsPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/TwoFactor/Application.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/TwoFactor/Invalid.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/TwoFactor/Key.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/TwoFactor/Simple.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/TwoFactorPlugin.php create mode 100644 srcs/phpmyadmin/libraries/classes/Plugins/UploadInterface.php (limited to 'srcs/phpmyadmin/libraries/classes/Plugins') diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Auth/AuthenticationConfig.php b/srcs/phpmyadmin/libraries/classes/Plugins/Auth/AuthenticationConfig.php new file mode 100644 index 0000000..7ebd1ae --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Auth/AuthenticationConfig.php @@ -0,0 +1,172 @@ +isAjax()) { + $response->setRequestStatus(false); + // reload_flag removes the token parameter from the URL and reloads + $response->addJSON('reload_flag', '1'); + if (defined('TESTSUITE')) { + return true; + } else { + exit; + } + } + + return true; + } + + /** + * Gets authentication credentials + * + * @return boolean always true + */ + public function readCredentials() + { + if ($GLOBALS['token_provided'] && $GLOBALS['token_mismatch']) { + return false; + } + + $this->user = $GLOBALS['cfg']['Server']['user']; + $this->password = $GLOBALS['cfg']['Server']['password']; + + return true; + } + + /** + * User is not allowed to login to MySQL -> authentication failed + * + * @param string $failure String describing why authentication has failed + * + * @return void + */ + public function showFailure($failure) + { + parent::showFailure($failure); + $conn_error = $GLOBALS['dbi']->getError(); + if (! $conn_error) { + $conn_error = __('Cannot connect: invalid settings.'); + } + + /* HTML header */ + $response = Response::getInstance(); + $response->getFooter() + ->setMinimal(); + $header = $response->getHeader(); + $header->setBodyId('loginform'); + $header->setTitle(__('Access denied!')); + $header->disableMenuAndConsole(); + echo '

+
+

'; + echo sprintf(__('Welcome to %s'), ' phpMyAdmin '); + echo '

+
+
+ + + + + + + ' , "\n"; + if (count($GLOBALS['cfg']['Servers']) > 1) { + // offer a chance to login to other servers if the current one failed + echo '' , "\n"; + echo ' ' , "\n"; + echo '' , "\n"; + } + echo '
'; + if (isset($GLOBALS['allowDeny_forbidden']) + && $GLOBALS['allowDeny_forbidden'] + ) { + trigger_error(__('Access denied!'), E_USER_NOTICE); + } else { + // Check whether user has configured something + if ($GLOBALS['PMA_Config']->source_mtime == 0) { + echo '

' , sprintf( + __( + 'You probably did not create a configuration file.' + . ' You might want to use the %1$ssetup script%2$s to' + . ' create one.' + ), + '', + '' + ) , '

' , "\n"; + } elseif (! isset($GLOBALS['errno']) + || (isset($GLOBALS['errno']) && $GLOBALS['errno'] != 2002) + && $GLOBALS['errno'] != 2003 + ) { + // if we display the "Server not responding" error, do not confuse + // users by telling them they have a settings problem + // (note: it's true that they could have a badly typed host name, + // but anyway the current message tells that the server + // rejected the connection, which is not really what happened) + // 2002 is the error given by mysqli + // 2003 is the error given by mysql + trigger_error( + __( + 'phpMyAdmin tried to connect to the MySQL server, and the' + . ' server rejected the connection. You should check the' + . ' host, username and password in your configuration and' + . ' make sure that they correspond to the information given' + . ' by the administrator of the MySQL server.' + ), + E_USER_WARNING + ); + } + echo Util::mysqlDie( + $conn_error, + '', + true, + '', + false + ); + } + $GLOBALS['error_handler']->dispUserErrors(); + echo '
' , "\n"; + echo '' + , __('Retry to connect') + , '' , "\n"; + echo '
' , "\n"; + echo Select::render(true, true); + echo '
' , "\n"; + if (! defined('TESTSUITE')) { + exit; + } + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Auth/AuthenticationCookie.php b/srcs/phpmyadmin/libraries/classes/Plugins/Auth/AuthenticationCookie.php new file mode 100644 index 0000000..7a794d0 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Auth/AuthenticationCookie.php @@ -0,0 +1,964 @@ +_use_openssl = ! class_exists(Random::class); + } + + /** + * Forces (not)using of openSSL + * + * @param boolean $use The flag + * + * @return void + */ + public function setUseOpenSSL($use) + { + $this->_use_openssl = $use; + } + + /** + * Displays authentication form + * + * this function MUST exit/quit the application + * + * @global string $conn_error the last connection error + * + * @return boolean|void + */ + public function showLoginForm() + { + global $conn_error; + + $response = Response::getInstance(); + + // When sending login modal after session has expired, send the new token explicitly with the response to update the token in all the forms having a hidden token. + $session_expired = isset($_REQUEST['check_timeout']) || isset($_REQUEST['session_timedout']); + if (! $session_expired && $response->loginPage()) { + if (defined('TESTSUITE')) { + return true; + } else { + exit; + } + } + + // When sending login modal after session has expired, send the new token explicitly with the response to update the token in all the forms having a hidden token. + if ($session_expired) { + $response->setRequestStatus(false); + $response->addJSON( + 'new_token', + $_SESSION[' PMA_token '] + ); + } + + // logged_in response parameter is used to check if the login, using the modal was successful after session expiration + if (isset($_REQUEST['session_timedout'])) { + $response->addJSON( + 'logged_in', + 0 + ); + } + + // No recall if blowfish secret is not configured as it would produce + // garbage + if ($GLOBALS['cfg']['LoginCookieRecall'] + && ! empty($GLOBALS['cfg']['blowfish_secret']) + ) { + $default_user = $this->user; + $default_server = $GLOBALS['pma_auth_server']; + $autocomplete = ''; + } else { + $default_user = ''; + $default_server = ''; + // skip the IE autocomplete feature. + $autocomplete = ' autocomplete="off"'; + } + + // wrap the login form in a div which overlays the whole page. + if ($session_expired) { + echo $this->template->render('login/header', [ + 'theme' => $GLOBALS['PMA_Theme'], + 'add_class' => ' modal_form', + 'session_expired' => 1, + ]); + } else { + echo $this->template->render('login/header', [ + 'theme' => $GLOBALS['PMA_Theme'], + 'add_class' => '', + 'session_expired' => 0, + ]); + } + + if ($GLOBALS['cfg']['DBG']['demo']) { + echo '
'; + echo '' , __('phpMyAdmin Demo Server') , ''; + printf( + __( + 'You are using the demo server. You can do anything here, but ' + . 'please do not change root, debian-sys-maint and pma users. ' + . 'More information is available at %s.' + ), + 'demo.phpmyadmin.net' + ); + echo '
'; + } + + // Show error message + if (! empty($conn_error)) { + Message::rawError((string) $conn_error)->display(); + } elseif (isset($_GET['session_expired']) + && intval($_GET['session_expired']) == 1 + ) { + Message::rawError( + __('Your session has expired. Please log in again.') + )->display(); + } + + // Displays the languages form + $language_manager = LanguageManager::getInstance(); + if (empty($GLOBALS['cfg']['Lang']) && $language_manager->hasChoice()) { + echo "
"; + // use fieldset, don't show doc link + echo $language_manager->getSelectorDisplay(new Template(), true, false); + echo '
'; + } + echo ' +
+ +
+
+ '; + echo ''; + + // Add a hidden element session_timedout which is used to check if the user requested login after session expiration + if ($session_expired) { + echo ''; + } + echo __('Log in'); + echo Util::showDocu('index'); + echo ''; + if ($GLOBALS['cfg']['AllowArbitraryServer']) { + echo ' +
+ + +
'; + } + echo '
+ + +
+
+ + +
'; + if (count($GLOBALS['cfg']['Servers']) > 1) { + echo '
+ +
'; + } else { + echo ' '; + } // end if (server choice) + + echo '
'; + + // binds input field with invisible reCaptcha if enabled + if (empty($GLOBALS['cfg']['CaptchaLoginPrivateKey']) + && empty($GLOBALS['cfg']['CaptchaLoginPublicKey']) + ) { + echo ''; + } else { + echo ''; + echo ''; + } + $_form_params = []; + if (! empty($GLOBALS['target'])) { + $_form_params['target'] = $GLOBALS['target']; + } + if (strlen($GLOBALS['db'])) { + $_form_params['db'] = $GLOBALS['db']; + } + if (strlen($GLOBALS['table'])) { + $_form_params['table'] = $GLOBALS['table']; + } + // do not generate a "server" hidden field as we want the "server" + // drop-down to have priority + echo Url::getHiddenInputs($_form_params, '', 0, 'server'); + echo '
+
'; + + if ($GLOBALS['error_handler']->hasDisplayErrors()) { + echo '
'; + $GLOBALS['error_handler']->dispErrors(); + echo '
'; + } + + // close the wrapping div tag, if the request is after session timeout + if ($session_expired) { + echo $this->template->render('login/footer', ['session_expired' => 1]); + } else { + echo $this->template->render('login/footer', ['session_expired' => 0]); + } + + echo Config::renderFooter(); + + if (! defined('TESTSUITE')) { + exit; + } else { + return true; + } + } + + /** + * Gets authentication credentials + * + * this function DOES NOT check authentication - it just checks/provides + * authentication credentials required to connect to the MySQL server + * usually with $GLOBALS['dbi']->connect() + * + * it returns false if something is missing - which usually leads to + * showLoginForm() which displays login form + * + * it returns true if all seems ok which usually leads to auth_set_user() + * + * it directly switches to showFailure() if user inactivity timeout is reached + * + * @return boolean whether we get authentication settings or not + */ + public function readCredentials() + { + global $conn_error; + + // Initialization + /** + * @global $GLOBALS['pma_auth_server'] the user provided server to + * connect to + */ + $GLOBALS['pma_auth_server'] = ''; + + $this->user = $this->password = ''; + $GLOBALS['from_cookie'] = false; + + if (isset($_POST['pma_username']) && strlen($_POST['pma_username']) > 0) { + // Verify Captcha if it is required. + if (! empty($GLOBALS['cfg']['CaptchaLoginPrivateKey']) + && ! empty($GLOBALS['cfg']['CaptchaLoginPublicKey']) + ) { + if (! empty($_POST["g-recaptcha-response"])) { + if (function_exists('curl_init')) { + $reCaptcha = new ReCaptcha\ReCaptcha( + $GLOBALS['cfg']['CaptchaLoginPrivateKey'], + new ReCaptcha\RequestMethod\CurlPost() + ); + } elseif (ini_get('allow_url_fopen')) { + $reCaptcha = new ReCaptcha\ReCaptcha( + $GLOBALS['cfg']['CaptchaLoginPrivateKey'], + new ReCaptcha\RequestMethod\Post() + ); + } else { + $reCaptcha = new ReCaptcha\ReCaptcha( + $GLOBALS['cfg']['CaptchaLoginPrivateKey'], + new ReCaptcha\RequestMethod\SocketPost() + ); + } + + // verify captcha status. + $resp = $reCaptcha->verify( + $_POST["g-recaptcha-response"], + Core::getIp() + ); + + // Check if the captcha entered is valid, if not stop the login. + if ($resp == null || ! $resp->isSuccess()) { + $codes = $resp->getErrorCodes(); + + if (in_array('invalid-json', $codes)) { + $conn_error = __('Failed to connect to the reCAPTCHA service!'); + } else { + $conn_error = __('Entered captcha is wrong, try again!'); + } + return false; + } + } else { + $conn_error = __('Missing reCAPTCHA verification, maybe it has been blocked by adblock?'); + return false; + } + } + + // The user just logged in + $this->user = Core::sanitizeMySQLUser($_POST['pma_username']); + $this->password = isset($_POST['pma_password']) ? $_POST['pma_password'] : ''; + if ($GLOBALS['cfg']['AllowArbitraryServer'] + && isset($_REQUEST['pma_servername']) + ) { + if ($GLOBALS['cfg']['ArbitraryServerRegexp']) { + $parts = explode(' ', $_REQUEST['pma_servername']); + if (count($parts) === 2) { + $tmp_host = $parts[0]; + } else { + $tmp_host = $_REQUEST['pma_servername']; + } + + $match = preg_match( + $GLOBALS['cfg']['ArbitraryServerRegexp'], + $tmp_host + ); + if (! $match) { + $conn_error = __( + 'You are not allowed to log in to this MySQL server!' + ); + return false; + } + } + $GLOBALS['pma_auth_server'] = Core::sanitizeMySQLHost($_REQUEST['pma_servername']); + } + /* Secure current session on login to avoid session fixation */ + Session::secure(); + return true; + } + + // At the end, try to set the $this->user + // and $this->password variables from cookies + + // check cookies + $serverCookie = $GLOBALS['PMA_Config']->getCookie('pmaUser-' . $GLOBALS['server']); + if (empty($serverCookie)) { + return false; + } + + $value = $this->cookieDecrypt( + $serverCookie, + $this->_getEncryptionSecret() + ); + + if ($value === false) { + return false; + } + + $this->user = $value; + // user was never logged in since session start + if (empty($_SESSION['browser_access_time'])) { + return false; + } + + // User inactive too long + $last_access_time = time() - $GLOBALS['cfg']['LoginCookieValidity']; + foreach ($_SESSION['browser_access_time'] as $key => $value) { + if ($value < $last_access_time) { + unset($_SESSION['browser_access_time'][$key]); + } + } + // All sessions expired + if (empty($_SESSION['browser_access_time'])) { + Util::cacheUnset('is_create_db_priv'); + Util::cacheUnset('is_reload_priv'); + Util::cacheUnset('db_to_create'); + Util::cacheUnset('dbs_where_create_table_allowed'); + Util::cacheUnset('dbs_to_test'); + Util::cacheUnset('db_priv'); + Util::cacheUnset('col_priv'); + Util::cacheUnset('table_priv'); + Util::cacheUnset('proc_priv'); + + $this->showFailure('no-activity'); + if (! defined('TESTSUITE')) { + exit; + } else { + return false; + } + } + + // check password cookie + $serverCookie = $GLOBALS['PMA_Config']->getCookie('pmaAuth-' . $GLOBALS['server']); + + if (empty($serverCookie)) { + return false; + } + $value = $this->cookieDecrypt( + $serverCookie, + $this->_getSessionEncryptionSecret() + ); + if ($value === false) { + return false; + } + + $auth_data = json_decode($value, true); + + if (! is_array($auth_data) || ! isset($auth_data['password'])) { + return false; + } + $this->password = $auth_data['password']; + if ($GLOBALS['cfg']['AllowArbitraryServer'] && ! empty($auth_data['server'])) { + $GLOBALS['pma_auth_server'] = $auth_data['server']; + } + + $GLOBALS['from_cookie'] = true; + + return true; + } + + /** + * Set the user and password after last checkings if required + * + * @return boolean always true + */ + public function storeCredentials() + { + global $cfg; + + if ($GLOBALS['cfg']['AllowArbitraryServer'] + && ! empty($GLOBALS['pma_auth_server']) + ) { + /* Allow to specify 'host port' */ + $parts = explode(' ', $GLOBALS['pma_auth_server']); + if (count($parts) === 2) { + $tmp_host = $parts[0]; + $tmp_port = $parts[1]; + } else { + $tmp_host = $GLOBALS['pma_auth_server']; + $tmp_port = ''; + } + if ($cfg['Server']['host'] != $GLOBALS['pma_auth_server']) { + $cfg['Server']['host'] = $tmp_host; + if (! empty($tmp_port)) { + $cfg['Server']['port'] = $tmp_port; + } + } + unset($tmp_host, $tmp_port, $parts); + } + + return parent::storeCredentials(); + } + + /** + * Stores user credentials after successful login. + * + * @return void|bool + */ + public function rememberCredentials() + { + // Name and password cookies need to be refreshed each time + // Duration = one month for username + + $this->storeUsernameCookie($this->user); + + // Duration = as configured + // Do not store password cookie on password change as we will + // set the cookie again after password has been changed + if (! isset($_POST['change_pw'])) { + $this->storePasswordCookie($this->password); + } + // URL where to go: + $redirect_url = './index.php'; + + // any parameters to pass? + $url_params = []; + if (strlen($GLOBALS['db']) > 0) { + $url_params['db'] = $GLOBALS['db']; + } + if (strlen($GLOBALS['table']) > 0) { + $url_params['table'] = $GLOBALS['table']; + } + // any target to pass? + if (! empty($GLOBALS['target']) + && $GLOBALS['target'] != 'index.php' + ) { + $url_params['target'] = $GLOBALS['target']; + } + + // user logged in successfully after session expiration + if (isset($_REQUEST['session_timedout'])) { + $response = Response::getInstance(); + $response->addJSON( + 'logged_in', + 1 + ); + $response->addJSON( + 'success', + 1 + ); + $response->addJSON( + 'new_token', + $_SESSION[' PMA_token '] + ); + + if (! defined('TESTSUITE')) { + exit; + } else { + return false; + } + } + // Set server cookies if required (once per session) and, in this case, + // force reload to ensure the client accepts cookies + if (! $GLOBALS['from_cookie']) { + + /** + * Clear user cache. + */ + Util::clearUserCache(); + + Response::getInstance() + ->disable(); + + Core::sendHeaderLocation( + $redirect_url . Url::getCommonRaw($url_params), + true + ); + if (! defined('TESTSUITE')) { + exit; + } else { + return false; + } + } // end if + + return true; + } + + /** + * Stores username in a cookie. + * + * @param string $username User name + * + * @return void + */ + public function storeUsernameCookie($username) + { + // Name and password cookies need to be refreshed each time + // Duration = one month for username + $GLOBALS['PMA_Config']->setCookie( + 'pmaUser-' . $GLOBALS['server'], + $this->cookieEncrypt( + $username, + $this->_getEncryptionSecret() + ) + ); + } + + /** + * Stores password in a cookie. + * + * @param string $password Password + * + * @return void + */ + public function storePasswordCookie($password) + { + $payload = ['password' => $password]; + if ($GLOBALS['cfg']['AllowArbitraryServer'] && ! empty($GLOBALS['pma_auth_server'])) { + $payload['server'] = $GLOBALS['pma_auth_server']; + } + // Duration = as configured + $GLOBALS['PMA_Config']->setCookie( + 'pmaAuth-' . $GLOBALS['server'], + $this->cookieEncrypt( + json_encode($payload), + $this->_getSessionEncryptionSecret() + ), + null, + (int) $GLOBALS['cfg']['LoginCookieStore'] + ); + } + + /** + * User is not allowed to login to MySQL -> authentication failed + * + * prepares error message and switches to showLoginForm() which display the error + * and the login form + * + * this function MUST exit/quit the application, + * currently done by call to showLoginForm() + * + * @param string $failure String describing why authentication has failed + * + * @return void + */ + public function showFailure($failure) + { + global $conn_error; + + parent::showFailure($failure); + + // Deletes password cookie and displays the login form + $GLOBALS['PMA_Config']->removeCookie('pmaAuth-' . $GLOBALS['server']); + + $conn_error = $this->getErrorMessage($failure); + + $response = Response::getInstance(); + + // needed for PHP-CGI (not need for FastCGI or mod-php) + $response->header('Cache-Control: no-store, no-cache, must-revalidate'); + $response->header('Pragma: no-cache'); + + $this->showLoginForm(); + } + + /** + * Returns blowfish secret or generates one if needed. + * + * @return string + */ + private function _getEncryptionSecret() + { + if (empty($GLOBALS['cfg']['blowfish_secret'])) { + return $this->_getSessionEncryptionSecret(); + } + + return $GLOBALS['cfg']['blowfish_secret']; + } + + /** + * Returns blowfish secret or generates one if needed. + * + * @return string + */ + private function _getSessionEncryptionSecret() + { + if (empty($_SESSION['encryption_key'])) { + if ($this->_use_openssl) { + $_SESSION['encryption_key'] = openssl_random_pseudo_bytes(32); + } else { + $_SESSION['encryption_key'] = Crypt\Random::string(32); + } + } + return $_SESSION['encryption_key']; + } + + /** + * Concatenates secret in order to make it 16 bytes log + * + * This doesn't add any security, just ensures the secret + * is long enough by copying it. + * + * @param string $secret Original secret + * + * @return string + */ + public function enlargeSecret($secret) + { + while (strlen($secret) < 16) { + $secret .= $secret; + } + return substr($secret, 0, 16); + } + + /** + * Derives MAC secret from encryption secret. + * + * @param string $secret the secret + * + * @return string the MAC secret + */ + public function getMACSecret($secret) + { + // Grab first part, up to 16 chars + // The MAC and AES secrets can overlap if original secret is short + $length = strlen($secret); + if ($length > 16) { + return substr($secret, 0, 16); + } + return $this->enlargeSecret( + $length == 1 ? $secret : substr($secret, 0, -1) + ); + } + + /** + * Derives AES secret from encryption secret. + * + * @param string $secret the secret + * + * @return string the AES secret + */ + public function getAESSecret($secret) + { + // Grab second part, up to 16 chars + // The MAC and AES secrets can overlap if original secret is short + $length = strlen($secret); + if ($length > 16) { + return substr($secret, -16); + } + return $this->enlargeSecret( + $length == 1 ? $secret : substr($secret, 1) + ); + } + + /** + * Cleans any SSL errors + * + * This can happen from corrupted cookies, by invalid encryption + * parameters used in older phpMyAdmin versions or by wrong openSSL + * configuration. + * + * In neither case the error is useful to user, but we need to clear + * the error buffer as otherwise the errors would pop up later, for + * example during MySQL SSL setup. + * + * @return void + */ + public function cleanSSLErrors() + { + if (function_exists('openssl_error_string')) { + do { + $hasSslErrors = openssl_error_string(); + } while ($hasSslErrors !== false); + } + } + + /** + * Encryption using openssl's AES or phpseclib's AES + * (phpseclib uses mcrypt when it is available) + * + * @param string $data original data + * @param string $secret the secret + * + * @return string the encrypted result + */ + public function cookieEncrypt($data, $secret) + { + $mac_secret = $this->getMACSecret($secret); + $aes_secret = $this->getAESSecret($secret); + $iv = $this->createIV(); + if ($this->_use_openssl) { + $result = openssl_encrypt( + $data, + 'AES-128-CBC', + $aes_secret, + 0, + $iv + ); + } else { + $cipher = new Crypt\AES(Crypt\Base::MODE_CBC); + $cipher->setIV($iv); + $cipher->setKey($aes_secret); + $result = base64_encode($cipher->encrypt($data)); + } + $this->cleanSSLErrors(); + $iv = base64_encode($iv); + return json_encode( + [ + 'iv' => $iv, + 'mac' => hash_hmac('sha1', $iv . $result, $mac_secret), + 'payload' => $result, + ] + ); + } + + /** + * Decryption using openssl's AES or phpseclib's AES + * (phpseclib uses mcrypt when it is available) + * + * @param string $encdata encrypted data + * @param string $secret the secret + * + * @return string|false original data, false on error + */ + public function cookieDecrypt($encdata, $secret) + { + $data = json_decode($encdata, true); + + if (! is_array($data) || ! isset($data['mac']) || ! isset($data['iv']) || ! isset($data['payload']) + || ! is_string($data['mac']) || ! is_string($data['iv']) || ! is_string($data['payload']) + ) { + return false; + } + + $mac_secret = $this->getMACSecret($secret); + $aes_secret = $this->getAESSecret($secret); + $newmac = hash_hmac('sha1', $data['iv'] . $data['payload'], $mac_secret); + + if (! hash_equals($data['mac'], $newmac)) { + return false; + } + + if ($this->_use_openssl) { + $result = openssl_decrypt( + $data['payload'], + 'AES-128-CBC', + $aes_secret, + 0, + base64_decode($data['iv']) + ); + } else { + $cipher = new Crypt\AES(Crypt\Base::MODE_CBC); + $cipher->setIV(base64_decode($data['iv'])); + $cipher->setKey($aes_secret); + $result = $cipher->decrypt(base64_decode($data['payload'])); + } + $this->cleanSSLErrors(); + return $result; + } + + /** + * Returns size of IV for encryption. + * + * @return int + */ + public function getIVSize() + { + if ($this->_use_openssl) { + return openssl_cipher_iv_length('AES-128-CBC'); + } + return (new Crypt\AES(Crypt\Base::MODE_CBC))->block_size; + } + + /** + * Initialization + * Store the initialization vector because it will be needed for + * further decryption. I don't think necessary to have one iv + * per server so I don't put the server number in the cookie name. + * + * @return string + */ + public function createIV() + { + /* Testsuite shortcut only to allow predictable IV */ + if ($this->_cookie_iv !== null) { + return $this->_cookie_iv; + } + if ($this->_use_openssl) { + return openssl_random_pseudo_bytes( + $this->getIVSize() + ); + } + + return Crypt\Random::string( + $this->getIVSize() + ); + } + + /** + * Sets encryption IV to use + * + * This is for testing only! + * + * @param string $vector The IV + * + * @return void + */ + public function setIV($vector) + { + $this->_cookie_iv = $vector; + } + + /** + * Callback when user changes password. + * + * @param string $password New password to set + * + * @return void + */ + public function handlePasswordChange($password) + { + $this->storePasswordCookie($password); + } + + /** + * Perform logout + * + * @return void + */ + public function logOut() + { + /** @var Config $PMA_Config */ + global $PMA_Config; + + // -> delete password cookie(s) + if ($GLOBALS['cfg']['LoginCookieDeleteAll']) { + foreach ($GLOBALS['cfg']['Servers'] as $key => $val) { + $PMA_Config->removeCookie('pmaAuth-' . $key); + if ($PMA_Config->issetCookie('pmaAuth-' . $key)) { + $PMA_Config->removeCookie('pmaAuth-' . $key); + } + } + } else { + $cookieName = 'pmaAuth-' . $GLOBALS['server']; + $PMA_Config->removeCookie($cookieName); + if ($PMA_Config->issetCookie($cookieName)) { + $PMA_Config->removeCookie($cookieName); + } + } + parent::logOut(); + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Auth/AuthenticationHttp.php b/srcs/phpmyadmin/libraries/classes/Plugins/Auth/AuthenticationHttp.php new file mode 100644 index 0000000..6d735e8 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Auth/AuthenticationHttp.php @@ -0,0 +1,214 @@ +isAjax()) { + $response->setRequestStatus(false); + // reload_flag removes the token parameter from the URL and reloads + $response->addJSON('reload_flag', '1'); + if (defined('TESTSUITE')) { + return true; + } else { + exit; + } + } + + return $this->authForm(); + } + + /** + * Displays authentication form + * + * @return boolean + */ + public function authForm() + { + if (empty($GLOBALS['cfg']['Server']['auth_http_realm'])) { + if (empty($GLOBALS['cfg']['Server']['verbose'])) { + $server_message = $GLOBALS['cfg']['Server']['host']; + } else { + $server_message = $GLOBALS['cfg']['Server']['verbose']; + } + $realm_message = 'phpMyAdmin ' . $server_message; + } else { + $realm_message = $GLOBALS['cfg']['Server']['auth_http_realm']; + } + + $response = Response::getInstance(); + + // remove non US-ASCII to respect RFC2616 + $realm_message = preg_replace('/[^\x20-\x7e]/i', '', $realm_message); + $response->header('WWW-Authenticate: Basic realm="' . $realm_message . '"'); + $response->setHttpResponseCode(401); + + /* HTML header */ + $footer = $response->getFooter(); + $footer->setMinimal(); + $header = $response->getHeader(); + $header->setTitle(__('Access denied!')); + $header->disableMenuAndConsole(); + $header->setBodyId('loginform'); + + $response->addHTML('

'); + $response->addHTML(sprintf(__('Welcome to %s'), ' phpMyAdmin')); + $response->addHTML('

'); + $response->addHTML('

'); + $response->addHTML( + Message::error( + __('Wrong username/password. Access denied.') + ) + ); + $response->addHTML('

'); + + $response->addHTML(Config::renderFooter()); + + if (! defined('TESTSUITE')) { + exit; + } else { + return false; + } + } + + /** + * Gets authentication credentials + * + * @return boolean whether we get authentication settings or not + */ + public function readCredentials() + { + // Grabs the $PHP_AUTH_USER variable + if (isset($GLOBALS['PHP_AUTH_USER'])) { + $this->user = $GLOBALS['PHP_AUTH_USER']; + } + if (empty($this->user)) { + if (Core::getenv('PHP_AUTH_USER')) { + $this->user = Core::getenv('PHP_AUTH_USER'); + } elseif (Core::getenv('REMOTE_USER')) { + // CGI, might be encoded, see below + $this->user = Core::getenv('REMOTE_USER'); + } elseif (Core::getenv('REDIRECT_REMOTE_USER')) { + // CGI, might be encoded, see below + $this->user = Core::getenv('REDIRECT_REMOTE_USER'); + } elseif (Core::getenv('AUTH_USER')) { + // WebSite Professional + $this->user = Core::getenv('AUTH_USER'); + } elseif (Core::getenv('HTTP_AUTHORIZATION')) { + // IIS, might be encoded, see below + $this->user = Core::getenv('HTTP_AUTHORIZATION'); + } elseif (Core::getenv('Authorization')) { + // FastCGI, might be encoded, see below + $this->user = Core::getenv('Authorization'); + } + } + // Grabs the $PHP_AUTH_PW variable + if (isset($GLOBALS['PHP_AUTH_PW'])) { + $this->password = $GLOBALS['PHP_AUTH_PW']; + } + if (empty($this->password)) { + if (Core::getenv('PHP_AUTH_PW')) { + $this->password = Core::getenv('PHP_AUTH_PW'); + } elseif (Core::getenv('REMOTE_PASSWORD')) { + // Apache/CGI + $this->password = Core::getenv('REMOTE_PASSWORD'); + } elseif (Core::getenv('AUTH_PASSWORD')) { + // WebSite Professional + $this->password = Core::getenv('AUTH_PASSWORD'); + } + } + // Sanitize empty password login + if ($this->password === null) { + $this->password = ''; + } + + // Avoid showing the password in phpinfo()'s output + unset($GLOBALS['PHP_AUTH_PW']); + unset($_SERVER['PHP_AUTH_PW']); + + // Decode possibly encoded information (used by IIS/CGI/FastCGI) + // (do not use explode() because a user might have a colon in his password + if (strcmp(substr($this->user, 0, 6), 'Basic ') == 0) { + $usr_pass = base64_decode(substr($this->user, 6)); + if (! empty($usr_pass)) { + $colon = strpos($usr_pass, ':'); + if ($colon) { + $this->user = substr($usr_pass, 0, $colon); + $this->password = substr($usr_pass, $colon + 1); + } + unset($colon); + } + unset($usr_pass); + } + + // sanitize username + $this->user = Core::sanitizeMySQLUser($this->user); + + // User logged out -> ensure the new username is not the same + $old_usr = isset($_REQUEST['old_usr']) ? $_REQUEST['old_usr'] : ''; + if (! empty($old_usr) + && (isset($this->user) && hash_equals($old_usr, $this->user)) + ) { + $this->user = ''; + } + + // Returns whether we get authentication settings or not + return ! empty($this->user); + } + + /** + * User is not allowed to login to MySQL -> authentication failed + * + * @param string $failure String describing why authentication has failed + * + * @return void + */ + public function showFailure($failure) + { + parent::showFailure($failure); + $error = $GLOBALS['dbi']->getError(); + if ($error && $GLOBALS['errno'] != 1045) { + Core::fatalError($error); + } else { + $this->authForm(); + } + } + + /** + * Returns URL for login form. + * + * @return string + */ + public function getLoginFormURL() + { + return './index.php?old_usr=' . $this->user; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Auth/AuthenticationSignon.php b/srcs/phpmyadmin/libraries/classes/Plugins/Auth/AuthenticationSignon.php new file mode 100644 index 0000000..36b1d66 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Auth/AuthenticationSignon.php @@ -0,0 +1,282 @@ +=')) { + session_set_cookie_params($sessionCookieParams); + } + + session_set_cookie_params( + $sessionCookieParams['lifetime'], + $sessionCookieParams['path'], + $sessionCookieParams['domain'], + $sessionCookieParams['secure'], + $sessionCookieParams['httponly'] + ); + } + + /** + * Gets authentication credentials + * + * @return boolean whether we get authentication settings or not + */ + public function readCredentials() + { + /* Check if we're using same signon server */ + $signon_url = $GLOBALS['cfg']['Server']['SignonURL']; + if (isset($_SESSION['LAST_SIGNON_URL']) + && $_SESSION['LAST_SIGNON_URL'] != $signon_url + ) { + return false; + } + + /* Script name */ + $script_name = $GLOBALS['cfg']['Server']['SignonScript']; + + /* Session name */ + $session_name = $GLOBALS['cfg']['Server']['SignonSession']; + + /* Login URL */ + $signon_url = $GLOBALS['cfg']['Server']['SignonURL']; + + /* Current host */ + $single_signon_host = $GLOBALS['cfg']['Server']['host']; + + /* Current port */ + $single_signon_port = $GLOBALS['cfg']['Server']['port']; + + /* No configuration updates */ + $single_signon_cfgupdate = []; + + /* Handle script based auth */ + if (! empty($script_name)) { + if (! @file_exists($script_name)) { + Core::fatalError( + __('Can not find signon authentication script:') + . ' ' . $script_name + ); + } + include $script_name; + + list ($this->user, $this->password) + = get_login_credentials($GLOBALS['cfg']['Server']['user']); + } elseif (isset($_COOKIE[$session_name])) { /* Does session exist? */ + /* End current session */ + $old_session = session_name(); + $old_id = session_id(); + $oldCookieParams = session_get_cookie_params(); + if (! defined('TESTSUITE')) { + session_write_close(); + } + /* Load single signon session */ + if (! defined('TESTSUITE')) { + $this->setCookieParams(); + session_name($session_name); + session_id($_COOKIE[$session_name]); + session_start(); + } + + /* Clear error message */ + unset($_SESSION['PMA_single_signon_error_message']); + + /* Grab credentials if they exist */ + if (isset($_SESSION['PMA_single_signon_user'])) { + $this->user = $_SESSION['PMA_single_signon_user']; + } + if (isset($_SESSION['PMA_single_signon_password'])) { + $this->password = $_SESSION['PMA_single_signon_password']; + } + if (isset($_SESSION['PMA_single_signon_host'])) { + $single_signon_host = $_SESSION['PMA_single_signon_host']; + } + + if (isset($_SESSION['PMA_single_signon_port'])) { + $single_signon_port = $_SESSION['PMA_single_signon_port']; + } + + if (isset($_SESSION['PMA_single_signon_cfgupdate'])) { + $single_signon_cfgupdate = $_SESSION['PMA_single_signon_cfgupdate']; + } + + /* Also get token as it is needed to access subpages */ + if (isset($_SESSION['PMA_single_signon_token'])) { + /* No need to care about token on logout */ + $pma_token = $_SESSION['PMA_single_signon_token']; + } + + /* End single signon session */ + if (! defined('TESTSUITE')) { + session_write_close(); + } + + /* Restart phpMyAdmin session */ + if (! defined('TESTSUITE')) { + $this->setCookieParams($oldCookieParams); + session_name($old_session); + if (! empty($old_id)) { + session_id($old_id); + } + session_start(); + } + + /* Set the single signon host */ + $GLOBALS['cfg']['Server']['host'] = $single_signon_host; + + /* Set the single signon port */ + $GLOBALS['cfg']['Server']['port'] = $single_signon_port; + + /* Configuration update */ + $GLOBALS['cfg']['Server'] = array_merge( + $GLOBALS['cfg']['Server'], + $single_signon_cfgupdate + ); + + /* Restore our token */ + if (! empty($pma_token)) { + $_SESSION[' PMA_token '] = $pma_token; + $_SESSION[' HMAC_secret '] = Util::generateRandom(16); + } + + /** + * Clear user cache. + */ + Util::clearUserCache(); + } + + // Returns whether we get authentication settings or not + if (empty($this->user)) { + unset($_SESSION['LAST_SIGNON_URL']); + + return false; + } + + $_SESSION['LAST_SIGNON_URL'] = $GLOBALS['cfg']['Server']['SignonURL']; + + return true; + } + + /** + * User is not allowed to login to MySQL -> authentication failed + * + * @param string $failure String describing why authentication has failed + * + * @return void + */ + public function showFailure($failure) + { + parent::showFailure($failure); + + /* Session name */ + $session_name = $GLOBALS['cfg']['Server']['SignonSession']; + + /* Does session exist? */ + if (isset($_COOKIE[$session_name])) { + if (! defined('TESTSUITE')) { + /* End current session */ + session_write_close(); + + /* Load single signon session */ + $this->setCookieParams(); + session_name($session_name); + session_id($_COOKIE[$session_name]); + session_start(); + } + + /* Set error message */ + $_SESSION['PMA_single_signon_error_message'] = $this->getErrorMessage($failure); + } + $this->showLoginForm(); + } + + /** + * Returns URL for login form. + * + * @return string + */ + public function getLoginFormURL() + { + return $GLOBALS['cfg']['Server']['SignonURL']; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/AuthenticationPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/AuthenticationPlugin.php new file mode 100644 index 0000000..a275f49 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/AuthenticationPlugin.php @@ -0,0 +1,371 @@ +ipAllowDeny = new IpAllowDeny(); + $this->template = new Template(); + } + + /** + * Displays authentication form + * + * @return boolean + */ + abstract public function showLoginForm(); + + /** + * Gets authentication credentials + * + * @return boolean + */ + abstract public function readCredentials(); + + /** + * Set the user and password after last checkings if required + * + * @return boolean + */ + public function storeCredentials() + { + global $cfg; + + $this->setSessionAccessTime(); + + $cfg['Server']['user'] = $this->user; + $cfg['Server']['password'] = $this->password; + + return true; + } + + /** + * Stores user credentials after successful login. + * + * @return void + */ + public function rememberCredentials() + { + } + + /** + * User is not allowed to login to MySQL -> authentication failed + * + * @param string $failure String describing why authentication has failed + * + * @return void + */ + public function showFailure($failure) + { + Logging::logUser($this->user, $failure); + } + + /** + * Perform logout + * + * @return void + */ + public function logOut() + { + /** @var Config $PMA_Config */ + global $PMA_Config; + + /* Obtain redirect URL (before doing logout) */ + if (! empty($GLOBALS['cfg']['Server']['LogoutURL'])) { + $redirect_url = $GLOBALS['cfg']['Server']['LogoutURL']; + } else { + $redirect_url = $this->getLoginFormURL(); + } + + /* Clear credentials */ + $this->user = ''; + $this->password = ''; + + /* + * Get a logged-in server count in case of LoginCookieDeleteAll is disabled. + */ + $server = 0; + if ($GLOBALS['cfg']['LoginCookieDeleteAll'] === false + && $GLOBALS['cfg']['Server']['auth_type'] == 'cookie' + ) { + foreach ($GLOBALS['cfg']['Servers'] as $key => $val) { + if ($PMA_Config->issetCookie('pmaAuth-' . $key)) { + $server = $key; + } + } + } + + if ($server === 0) { + /* delete user's choices that were stored in session */ + if (! defined('TESTSUITE')) { + session_unset(); + session_destroy(); + } + + /* Redirect to login form (or configured URL) */ + Core::sendHeaderLocation($redirect_url); + } else { + /* Redirect to other autenticated server */ + $_SESSION['partial_logout'] = true; + Core::sendHeaderLocation( + './index.php' . Url::getCommonRaw(['server' => $server]) + ); + } + } + + /** + * Returns URL for login form. + * + * @return string + */ + public function getLoginFormURL() + { + return './index.php'; + } + + /** + * Returns error message for failed authentication. + * + * @param string $failure String describing why authentication has failed + * + * @return string + */ + public function getErrorMessage($failure) + { + if ($failure == 'empty-denied') { + return __( + 'Login without a password is forbidden by configuration' + . ' (see AllowNoPassword)' + ); + } elseif ($failure == 'root-denied' || $failure == 'allow-denied') { + return __('Access denied!'); + } elseif ($failure == 'no-activity') { + return sprintf( + __('No activity within %s seconds; please log in again.'), + intval($GLOBALS['cfg']['LoginCookieValidity']) + ); + } + + $dbi_error = $GLOBALS['dbi']->getError(); + if (! empty($dbi_error)) { + return htmlspecialchars($dbi_error); + } elseif (isset($GLOBALS['errno'])) { + return '#' . $GLOBALS['errno'] . ' ' + . __('Cannot log in to the MySQL server'); + } + + return __('Cannot log in to the MySQL server'); + } + + /** + * Callback when user changes password. + * + * @param string $password New password to set + * + * @return void + */ + public function handlePasswordChange($password) + { + } + + /** + * Store session access time in session. + * + * Tries to workaround PHP 5 session garbage collection which + * looks at the session file's last modified time + * + * @return void + */ + public function setSessionAccessTime() + { + if (isset($_REQUEST['guid'])) { + $guid = (string) $_REQUEST['guid']; + } else { + $guid = 'default'; + } + if (isset($_REQUEST['access_time'])) { + // Ensure access_time is in range <0, LoginCookieValidity + 1> + // to avoid excessive extension of validity. + // + // Negative values can cause session expiry extension + // Too big values can cause overflow and lead to same + $time = time() - min(max(0, intval($_REQUEST['access_time'])), $GLOBALS['cfg']['LoginCookieValidity'] + 1); + } else { + $time = time(); + } + $_SESSION['browser_access_time'][$guid] = $time; + } + + /** + * High level authentication interface + * + * Gets the credentials or shows login form if necessary + * + * @return void + */ + public function authenticate() + { + $success = $this->readCredentials(); + + /* Show login form (this exits) */ + if (! $success) { + /* Force generating of new session */ + Session::secure(); + $this->showLoginForm(); + } + + /* Store credentials (eg. in cookies) */ + $this->storeCredentials(); + /* Check allow/deny rules */ + $this->checkRules(); + } + + /** + * Check configuration defined restrictions for authentication + * + * @return void + */ + public function checkRules() + { + global $cfg; + + // Check IP-based Allow/Deny rules as soon as possible to reject the + // user based on mod_access in Apache + if (isset($cfg['Server']['AllowDeny']) + && isset($cfg['Server']['AllowDeny']['order']) + ) { + $allowDeny_forbidden = false; // default + if ($cfg['Server']['AllowDeny']['order'] == 'allow,deny') { + $allowDeny_forbidden = true; + if ($this->ipAllowDeny->allow()) { + $allowDeny_forbidden = false; + } + if ($this->ipAllowDeny->deny()) { + $allowDeny_forbidden = true; + } + } elseif ($cfg['Server']['AllowDeny']['order'] == 'deny,allow') { + if ($this->ipAllowDeny->deny()) { + $allowDeny_forbidden = true; + } + if ($this->ipAllowDeny->allow()) { + $allowDeny_forbidden = false; + } + } elseif ($cfg['Server']['AllowDeny']['order'] == 'explicit') { + if ($this->ipAllowDeny->allow() && ! $this->ipAllowDeny->deny()) { + $allowDeny_forbidden = false; + } else { + $allowDeny_forbidden = true; + } + } // end if ... elseif ... elseif + + // Ejects the user if banished + if ($allowDeny_forbidden) { + $this->showFailure('allow-denied'); + } + } // end if + + // is root allowed? + if (! $cfg['Server']['AllowRoot'] && $cfg['Server']['user'] == 'root') { + $this->showFailure('root-denied'); + } + + // is a login without password allowed? + if (! $cfg['Server']['AllowNoPassword'] + && $cfg['Server']['password'] === '' + ) { + $this->showFailure('empty-denied'); + } + } + + /** + * Checks whether two factor authentication is active + * for given user and performs it. + * + * @return boolean|void + */ + public function checkTwoFactor() + { + $twofactor = new TwoFactor($this->user); + + /* Do we need to show the form? */ + if ($twofactor->check()) { + return; + } + + $response = Response::getInstance(); + if ($response->loginPage()) { + if (defined('TESTSUITE')) { + return; + } else { + exit; + } + } + echo $this->template->render('login/header', ['theme' => $GLOBALS['PMA_Theme']]); + Message::rawNotice( + __('You have enabled two factor authentication, please confirm your login.') + )->display(); + echo $this->template->render('login/twofactor', [ + 'form' => $twofactor->render(), + 'show_submit' => $twofactor->showSubmit, + ]); + echo $this->template->render('login/footer'); + echo Config::renderFooter(); + if (! defined('TESTSUITE')) { + exit; + } + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportCodegen.php b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportCodegen.php new file mode 100644 index 0000000..8a29538 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportCodegen.php @@ -0,0 +1,447 @@ +initSpecificVariables(); + $this->setProperties(); + } + + /** + * Initialize the local variables that are used for export CodeGen + * + * @return void + */ + protected function initSpecificVariables() + { + $this->_setCgFormats( + [ + "NHibernate C# DO", + "NHibernate XML", + ] + ); + + $this->_setCgHandlers( + [ + "_handleNHibernateCSBody", + "_handleNHibernateXMLBody", + ] + ); + } + + /** + * Sets the export CodeGen properties + * + * @return void + */ + protected function setProperties() + { + $exportPluginProperties = new ExportPluginProperties(); + $exportPluginProperties->setText('CodeGen'); + $exportPluginProperties->setExtension('cs'); + $exportPluginProperties->setMimeType('text/cs'); + $exportPluginProperties->setOptionsText(__('Options')); + + // create the root group that will be the options field for + // $exportPluginProperties + // this will be shown as "Format specific options" + $exportSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // general options main group + $generalOptions = new OptionsPropertyMainGroup("general_opts"); + // create primary items and add them to the group + $leaf = new HiddenPropertyItem("structure_or_data"); + $generalOptions->addProperty($leaf); + $leaf = new SelectPropertyItem( + "format", + __('Format:') + ); + $leaf->setValues($this->_getCgFormats()); + $generalOptions->addProperty($leaf); + // add the main group to the root group + $exportSpecificOptions->addProperty($generalOptions); + + // set the options for the export plugin property item + $exportPluginProperties->setOptions($exportSpecificOptions); + $this->properties = $exportPluginProperties; + } + + /** + * Outputs export header + * + * @return bool Whether it succeeded + */ + public function exportHeader() + { + return true; + } + + /** + * Outputs export footer + * + * @return bool Whether it succeeded + */ + public function exportFooter() + { + return true; + } + + /** + * Outputs database header + * + * @param string $db Database name + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBHeader($db, $db_alias = '') + { + return true; + } + + /** + * Outputs database footer + * + * @param string $db Database name + * + * @return bool Whether it succeeded + */ + public function exportDBFooter($db) + { + return true; + } + + /** + * Outputs CREATE DATABASE statement + * + * @param string $db Database name + * @param string $export_type 'server', 'database', 'table' + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBCreate($db, $export_type, $db_alias = '') + { + return true; + } + + /** + * Outputs the content of a table in NHibernate format + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $sql_query SQL query for obtaining data + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportData( + $db, + $table, + $crlf, + $error_url, + $sql_query, + array $aliases = [] + ) { + $CG_FORMATS = $this->_getCgFormats(); + $CG_HANDLERS = $this->_getCgHandlers(); + + $format = $GLOBALS['codegen_format']; + if (isset($CG_FORMATS[$format])) { + $method = $CG_HANDLERS[$format]; + + return $this->export->outputHandler( + $this->$method($db, $table, $crlf, $aliases) + ); + } + + return $this->export->outputHandler(sprintf("%s is not supported.", $format)); + } + + /** + * Used to make identifiers (from table or database names) + * + * @param string $str name to be converted + * @param bool $ucfirst whether to make the first character uppercase + * + * @return string identifier + */ + public static function cgMakeIdentifier($str, $ucfirst = true) + { + // remove unsafe characters + $str = preg_replace('/[^\p{L}\p{Nl}_]/u', '', $str); + // make sure first character is a letter or _ + if (! preg_match('/^\pL/u', $str)) { + $str = '_' . $str; + } + if ($ucfirst) { + $str = ucfirst($str); + } + + return $str; + } + + /** + * C# Handler + * + * @param string $db database name + * @param string $table table name + * @param string $crlf line separator + * @param array $aliases Aliases of db/table/columns + * + * @return string containing C# code lines, separated by "\n" + */ + private function _handleNHibernateCSBody($db, $table, $crlf, array $aliases = []) + { + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + $lines = []; + + $result = $GLOBALS['dbi']->query( + sprintf( + 'DESC %s.%s', + Util::backquote($db), + Util::backquote($table) + ) + ); + if ($result) { + /** @var TableProperty[] $tableProperties */ + $tableProperties = []; + while ($row = $GLOBALS['dbi']->fetchRow($result)) { + $col_as = $this->getAlias($aliases, $row[0], 'col', $db, $table); + if (! empty($col_as)) { + $row[0] = $col_as; + } + $tableProperties[] = new TableProperty($row); + } + $GLOBALS['dbi']->freeResult($result); + $lines[] = 'using System;'; + $lines[] = 'using System.Collections;'; + $lines[] = 'using System.Collections.Generic;'; + $lines[] = 'using System.Text;'; + $lines[] = 'namespace ' . ExportCodegen::cgMakeIdentifier($db_alias); + $lines[] = '{'; + $lines[] = ' #region ' + . ExportCodegen::cgMakeIdentifier($table_alias); + $lines[] = ' public class ' + . ExportCodegen::cgMakeIdentifier($table_alias); + $lines[] = ' {'; + $lines[] = ' #region Member Variables'; + foreach ($tableProperties as $tableProperty) { + $lines[] = $tableProperty->formatCs( + ' protected #dotNetPrimitiveType# _#name#;' + ); + } + $lines[] = ' #endregion'; + $lines[] = ' #region Constructors'; + $lines[] = ' public ' + . ExportCodegen::cgMakeIdentifier($table_alias) . '() { }'; + $temp = []; + foreach ($tableProperties as $tableProperty) { + if (! $tableProperty->isPK()) { + $temp[] = $tableProperty->formatCs( + '#dotNetPrimitiveType# #name#' + ); + } + } + $lines[] = ' public ' + . ExportCodegen::cgMakeIdentifier($table_alias) + . '(' + . implode(', ', $temp) + . ')'; + $lines[] = ' {'; + foreach ($tableProperties as $tableProperty) { + if (! $tableProperty->isPK()) { + $lines[] = $tableProperty->formatCs( + ' this._#name#=#name#;' + ); + } + } + $lines[] = ' }'; + $lines[] = ' #endregion'; + $lines[] = ' #region Public Properties'; + foreach ($tableProperties as $tableProperty) { + $lines[] = $tableProperty->formatCs( + ' public virtual #dotNetPrimitiveType# #ucfirstName#' + . "\n" + . ' {' . "\n" + . ' get {return _#name#;}' . "\n" + . ' set {_#name#=value;}' . "\n" + . ' }' + ); + } + $lines[] = ' #endregion'; + $lines[] = ' }'; + $lines[] = ' #endregion'; + $lines[] = '}'; + } + + return implode($crlf, $lines); + } + + /** + * XML Handler + * + * @param string $db database name + * @param string $table table name + * @param string $crlf line separator + * @param array $aliases Aliases of db/table/columns + * + * @return string containing XML code lines, separated by "\n" + */ + private function _handleNHibernateXMLBody( + $db, + $table, + $crlf, + array $aliases = [] + ) { + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + $lines = []; + $lines[] = ''; + $lines[] = ''; + $lines[] = ' '; + $result = $GLOBALS['dbi']->query( + sprintf( + "DESC %s.%s", + Util::backquote($db), + Util::backquote($table) + ) + ); + if ($result) { + while ($row = $GLOBALS['dbi']->fetchRow($result)) { + $col_as = $this->getAlias($aliases, $row[0], 'col', $db, $table); + if (! empty($col_as)) { + $row[0] = $col_as; + } + $tableProperty = new TableProperty($row); + if ($tableProperty->isPK()) { + $lines[] = $tableProperty->formatXml( + ' ' . "\n" + . ' ' . "\n" + . ' ' . "\n" + . ' ' + ); + } else { + $lines[] = $tableProperty->formatXml( + ' ' . "\n" + . ' ' . "\n" + . ' ' + ); + } + } + $GLOBALS['dbi']->freeResult($result); + } + $lines[] = ' '; + $lines[] = ''; + + return implode($crlf, $lines); + } + + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Getter for CodeGen formats + * + * @return array + */ + private function _getCgFormats() + { + return $this->_cgFormats; + } + + /** + * Setter for CodeGen formats + * + * @param array $CG_FORMATS contains CodeGen Formats + * + * @return void + */ + private function _setCgFormats(array $CG_FORMATS) + { + $this->_cgFormats = $CG_FORMATS; + } + + /** + * Getter for CodeGen handlers + * + * @return array + */ + private function _getCgHandlers() + { + return $this->_cgHandlers; + } + + /** + * Setter for CodeGen handlers + * + * @param array $CG_HANDLERS contains CodeGen handler methods + * + * @return void + */ + private function _setCgHandlers(array $CG_HANDLERS) + { + $this->_cgHandlers = $CG_HANDLERS; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportCsv.php b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportCsv.php new file mode 100644 index 0000000..8e5a319 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportCsv.php @@ -0,0 +1,347 @@ +setProperties(); + } + + /** + * Sets the export CSV properties + * + * @return void + */ + protected function setProperties() + { + $exportPluginProperties = new ExportPluginProperties(); + $exportPluginProperties->setText('CSV'); + $exportPluginProperties->setExtension('csv'); + $exportPluginProperties->setMimeType('text/comma-separated-values'); + $exportPluginProperties->setOptionsText(__('Options')); + + // create the root group that will be the options field for + // $exportPluginProperties + // this will be shown as "Format specific options" + $exportSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // general options main group + $generalOptions = new OptionsPropertyMainGroup("general_opts"); + // create leaf items and add them to the group + $leaf = new TextPropertyItem( + "separator", + __('Columns separated with:') + ); + $generalOptions->addProperty($leaf); + $leaf = new TextPropertyItem( + "enclosed", + __('Columns enclosed with:') + ); + $generalOptions->addProperty($leaf); + $leaf = new TextPropertyItem( + "escaped", + __('Columns escaped with:') + ); + $generalOptions->addProperty($leaf); + $leaf = new TextPropertyItem( + "terminated", + __('Lines terminated with:') + ); + $generalOptions->addProperty($leaf); + $leaf = new TextPropertyItem( + 'null', + __('Replace NULL with:') + ); + $generalOptions->addProperty($leaf); + $leaf = new BoolPropertyItem( + 'removeCRLF', + __('Remove carriage return/line feed characters within columns') + ); + $generalOptions->addProperty($leaf); + $leaf = new BoolPropertyItem( + 'columns', + __('Put columns names in the first row') + ); + $generalOptions->addProperty($leaf); + $leaf = new HiddenPropertyItem( + 'structure_or_data' + ); + $generalOptions->addProperty($leaf); + // add the main group to the root group + $exportSpecificOptions->addProperty($generalOptions); + + // set the options for the export plugin property item + $exportPluginProperties->setOptions($exportSpecificOptions); + $this->properties = $exportPluginProperties; + } + + /** + * Outputs export header + * + * @return bool Whether it succeeded + */ + public function exportHeader() + { + global $what, $csv_terminated, $csv_separator, $csv_enclosed, $csv_escaped; + //Enable columns names by default for CSV + if ($what == 'csv') { + $GLOBALS['csv_columns'] = 'yes'; + } + // Here we just prepare some values for export + if ($what == 'excel') { + $csv_terminated = "\015\012"; + switch ($GLOBALS['excel_edition']) { + case 'win': + // as tested on Windows with Excel 2002 and Excel 2007 + $csv_separator = ';'; + break; + case 'mac_excel2003': + $csv_separator = ';'; + break; + case 'mac_excel2008': + $csv_separator = ','; + break; + } + $csv_enclosed = '"'; + $csv_escaped = '"'; + if (isset($GLOBALS['excel_columns'])) { + $GLOBALS['csv_columns'] = 'yes'; + } + } else { + if (empty($csv_terminated) + || mb_strtolower($csv_terminated) == 'auto' + ) { + $csv_terminated = $GLOBALS['crlf']; + } else { + $csv_terminated = str_replace( + [ + '\\r', + '\\n', + '\\t', + ], + [ + "\015", + "\012", + "\011", + ], + $csv_terminated + ); + } // end if + $csv_separator = str_replace('\\t', "\011", $csv_separator); + } + + return true; + } + + /** + * Outputs export footer + * + * @return bool Whether it succeeded + */ + public function exportFooter() + { + return true; + } + + /** + * Outputs database header + * + * @param string $db Database name + * @param string $db_alias Alias of db + * + * @return bool Whether it succeeded + */ + public function exportDBHeader($db, $db_alias = '') + { + return true; + } + + /** + * Outputs database footer + * + * @param string $db Database name + * + * @return bool Whether it succeeded + */ + public function exportDBFooter($db) + { + return true; + } + + /** + * Outputs CREATE DATABASE statement + * + * @param string $db Database name + * @param string $export_type 'server', 'database', 'table' + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBCreate($db, $export_type, $db_alias = '') + { + return true; + } + + /** + * Outputs the content of a table in CSV format + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $sql_query SQL query for obtaining data + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportData( + $db, + $table, + $crlf, + $error_url, + $sql_query, + array $aliases = [] + ) { + global $what, $csv_terminated, $csv_separator, $csv_enclosed, $csv_escaped; + + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + + // Gets the data from the database + $result = $GLOBALS['dbi']->query( + $sql_query, + DatabaseInterface::CONNECT_USER, + DatabaseInterface::QUERY_UNBUFFERED + ); + $fields_cnt = $GLOBALS['dbi']->numFields($result); + + // If required, get fields name at the first line + if (isset($GLOBALS['csv_columns'])) { + $schema_insert = ''; + for ($i = 0; $i < $fields_cnt; $i++) { + $col_as = $GLOBALS['dbi']->fieldName($result, $i); + if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) { + $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as]; + } + $col_as = stripslashes($col_as); + if ($csv_enclosed == '') { + $schema_insert .= $col_as; + } else { + $schema_insert .= $csv_enclosed + . str_replace( + $csv_enclosed, + $csv_escaped . $csv_enclosed, + $col_as + ) + . $csv_enclosed; + } + $schema_insert .= $csv_separator; + } // end for + $schema_insert = trim(mb_substr($schema_insert, 0, -1)); + if (! $this->export->outputHandler($schema_insert . $csv_terminated)) { + return false; + } + } // end if + + // Format the data + while ($row = $GLOBALS['dbi']->fetchRow($result)) { + $schema_insert = ''; + for ($j = 0; $j < $fields_cnt; $j++) { + if (! isset($row[$j]) || $row[$j] === null) { + $schema_insert .= $GLOBALS[$what . '_null']; + } elseif ($row[$j] == '0' || $row[$j] != '') { + // always enclose fields + if ($what == 'excel') { + $row[$j] = preg_replace("/\015(\012)?/", "\012", $row[$j]); + } + // remove CRLF characters within field + if (isset($GLOBALS[$what . '_removeCRLF']) + && $GLOBALS[$what . '_removeCRLF'] + ) { + $row[$j] = str_replace( + [ + "\r", + "\n", + ], + "", + $row[$j] + ); + } + if ($csv_enclosed == '') { + $schema_insert .= $row[$j]; + } else { + // also double the escape string if found in the data + if ($csv_escaped != $csv_enclosed) { + $schema_insert .= $csv_enclosed + . str_replace( + $csv_enclosed, + $csv_escaped . $csv_enclosed, + str_replace( + $csv_escaped, + $csv_escaped . $csv_escaped, + $row[$j] + ) + ) + . $csv_enclosed; + } else { + // avoid a problem when escape string equals enclose + $schema_insert .= $csv_enclosed + . str_replace( + $csv_enclosed, + $csv_escaped . $csv_enclosed, + $row[$j] + ) + . $csv_enclosed; + } + } + } else { + $schema_insert .= ''; + } + if ($j < $fields_cnt - 1) { + $schema_insert .= $csv_separator; + } + } // end for + + if (! $this->export->outputHandler($schema_insert . $csv_terminated)) { + return false; + } + } // end while + $GLOBALS['dbi']->freeResult($result); + + return true; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportExcel.php b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportExcel.php new file mode 100644 index 0000000..e778d20 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportExcel.php @@ -0,0 +1,90 @@ +setText('CSV for MS Excel'); + $exportPluginProperties->setExtension('csv'); + $exportPluginProperties->setMimeType('text/comma-separated-values'); + $exportPluginProperties->setOptionsText(__('Options')); + + // create the root group that will be the options field for + // $exportPluginProperties + // this will be shown as "Format specific options" + $exportSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // general options main group + $generalOptions = new OptionsPropertyMainGroup("general_opts"); + // create primary items and add them to the group + $leaf = new TextPropertyItem( + 'null', + __('Replace NULL with:') + ); + $generalOptions->addProperty($leaf); + $leaf = new BoolPropertyItem( + 'removeCRLF', + __('Remove carriage return/line feed characters within columns') + ); + $generalOptions->addProperty($leaf); + $leaf = new BoolPropertyItem( + 'columns', + __('Put columns names in the first row') + ); + $generalOptions->addProperty($leaf); + $leaf = new SelectPropertyItem( + 'edition', + __('Excel edition:') + ); + $leaf->setValues( + [ + 'win' => 'Windows', + 'mac_excel2003' => 'Excel 2003 / Macintosh', + 'mac_excel2008' => 'Excel 2008 / Macintosh', + ] + ); + $generalOptions->addProperty($leaf); + $leaf = new HiddenPropertyItem( + 'structure_or_data' + ); + $generalOptions->addProperty($leaf); + // add the main group to the root group + $exportSpecificOptions->addProperty($generalOptions); + + // set the options for the export plugin property item + $exportPluginProperties->setOptions($exportSpecificOptions); + $this->properties = $exportPluginProperties; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportHtmlword.php b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportHtmlword.php new file mode 100644 index 0000000..cbf4fce --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportHtmlword.php @@ -0,0 +1,670 @@ +setProperties(); + } + + /** + * Sets the export HTML-Word properties + * + * @return void + */ + protected function setProperties() + { + $exportPluginProperties = new ExportPluginProperties(); + $exportPluginProperties->setText('Microsoft Word 2000'); + $exportPluginProperties->setExtension('doc'); + $exportPluginProperties->setMimeType('application/vnd.ms-word'); + $exportPluginProperties->setForceFile(true); + $exportPluginProperties->setOptionsText(__('Options')); + + // create the root group that will be the options field for + // $exportPluginProperties + // this will be shown as "Format specific options" + $exportSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // what to dump (structure/data/both) + $dumpWhat = new OptionsPropertyMainGroup( + "dump_what", + __('Dump table') + ); + // create primary items and add them to the group + $leaf = new RadioPropertyItem("structure_or_data"); + $leaf->setValues( + [ + 'structure' => __('structure'), + 'data' => __('data'), + 'structure_and_data' => __('structure and data'), + ] + ); + $dumpWhat->addProperty($leaf); + // add the main group to the root group + $exportSpecificOptions->addProperty($dumpWhat); + + // data options main group + $dataOptions = new OptionsPropertyMainGroup( + "dump_what", + __('Data dump options') + ); + $dataOptions->setForce('structure'); + // create primary items and add them to the group + $leaf = new TextPropertyItem( + "null", + __('Replace NULL with:') + ); + $dataOptions->addProperty($leaf); + $leaf = new BoolPropertyItem( + "columns", + __('Put columns names in the first row') + ); + $dataOptions->addProperty($leaf); + // add the main group to the root group + $exportSpecificOptions->addProperty($dataOptions); + + // set the options for the export plugin property item + $exportPluginProperties->setOptions($exportSpecificOptions); + $this->properties = $exportPluginProperties; + } + + /** + * Outputs export header + * + * @return bool Whether it succeeded + */ + public function exportHeader() + { + global $charset; + + return $this->export->outputHandler( + ' + + + + + + + ' + ); + } + + /** + * Outputs export footer + * + * @return bool Whether it succeeded + */ + public function exportFooter() + { + return $this->export->outputHandler(''); + } + + /** + * Outputs database header + * + * @param string $db Database name + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBHeader($db, $db_alias = '') + { + if (empty($db_alias)) { + $db_alias = $db; + } + + return $this->export->outputHandler( + '

' . __('Database') . ' ' . htmlspecialchars($db_alias) . '

' + ); + } + + /** + * Outputs database footer + * + * @param string $db Database name + * + * @return bool Whether it succeeded + */ + public function exportDBFooter($db) + { + return true; + } + + /** + * Outputs CREATE DATABASE statement + * + * @param string $db Database name + * @param string $export_type 'server', 'database', 'table' + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBCreate($db, $export_type, $db_alias = '') + { + return true; + } + + /** + * Outputs the content of a table in HTML-Word format + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $sql_query SQL query for obtaining data + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportData( + $db, + $table, + $crlf, + $error_url, + $sql_query, + array $aliases = [] + ) { + global $what; + + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + + if (! $this->export->outputHandler( + '

' + . __('Dumping data for table') . ' ' . htmlspecialchars($table_alias) + . '

' + ) + ) { + return false; + } + if (! $this->export->outputHandler( + '' + ) + ) { + return false; + } + + // Gets the data from the database + $result = $GLOBALS['dbi']->query( + $sql_query, + DatabaseInterface::CONNECT_USER, + DatabaseInterface::QUERY_UNBUFFERED + ); + $fields_cnt = $GLOBALS['dbi']->numFields($result); + + // If required, get fields name at the first line + if (isset($GLOBALS['htmlword_columns'])) { + $schema_insert = ''; + for ($i = 0; $i < $fields_cnt; $i++) { + $col_as = $GLOBALS['dbi']->fieldName($result, $i); + if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) { + $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as]; + } + $col_as = stripslashes($col_as); + $schema_insert .= ''; + } // end for + $schema_insert .= ''; + if (! $this->export->outputHandler($schema_insert)) { + return false; + } + } // end if + + // Format the data + while ($row = $GLOBALS['dbi']->fetchRow($result)) { + $schema_insert = ''; + for ($j = 0; $j < $fields_cnt; $j++) { + if (! isset($row[$j]) || $row[$j] === null) { + $value = $GLOBALS[$what . '_null']; + } elseif ($row[$j] == '0' || $row[$j] != '') { + $value = $row[$j]; + } else { + $value = ''; + } + $schema_insert .= ''; + } // end for + $schema_insert .= ''; + if (! $this->export->outputHandler($schema_insert)) { + return false; + } + } // end while + $GLOBALS['dbi']->freeResult($result); + + return $this->export->outputHandler('
'); + } + + /** + * Returns a stand-in CREATE definition to resolve view dependencies + * + * @param string $db the database name + * @param string $view the view name + * @param string $crlf the end of line sequence + * @param array $aliases Aliases of db/table/columns + * + * @return string resulting definition + */ + public function getTableDefStandIn($db, $view, $crlf, $aliases = []) + { + $schema_insert = '' + . '' + . '' + . '' + . '' + . '' + . ''; + + /** + * Get the unique keys in the view + */ + $unique_keys = []; + $keys = $GLOBALS['dbi']->getTableIndexes($db, $view); + foreach ($keys as $key) { + if ($key['Non_unique'] == 0) { + $unique_keys[] = $key['Column_name']; + } + } + + $columns = $GLOBALS['dbi']->getColumns($db, $view); + foreach ($columns as $column) { + $col_as = $column['Field']; + if (! empty($aliases[$db]['tables'][$view]['columns'][$col_as])) { + $col_as = $aliases[$db]['tables'][$view]['columns'][$col_as]; + } + $schema_insert .= $this->formatOneColumnDefinition( + $column, + $unique_keys, + $col_as + ); + $schema_insert .= ''; + } + + $schema_insert .= '
'; + + return $schema_insert; + } + + /** + * Returns $table's CREATE definition + * + * @param string $db the database name + * @param string $table the table name + * @param bool $do_relation whether to include relation comments + * @param bool $do_comments whether to include the pmadb-style column + * comments as comments in the structure; + * this is deprecated but the parameter is + * left here because export.php calls + * PMA_exportStructure() also for other + * export types which use this parameter + * @param bool $do_mime whether to include mime comments + * at the end + * @param bool $view whether we're handling a view + * @param array $aliases Aliases of db/table/columns + * + * @return string resulting schema + */ + public function getTableDef( + $db, + $table, + $do_relation, + $do_comments, + $do_mime, + $view = false, + array $aliases = [] + ) { + // set $cfgRelation here, because there is a chance that it's modified + // since the class initialization + global $cfgRelation; + + $schema_insert = ''; + + /** + * Gets fields properties + */ + $GLOBALS['dbi']->selectDb($db); + + // Check if we can use Relations + list($res_rel, $have_rel) = $this->relation->getRelationsAndStatus( + $do_relation && ! empty($cfgRelation['relation']), + $db, + $table + ); + + /** + * Displays the table structure + */ + $schema_insert .= ''; + + $schema_insert .= ''; + $schema_insert .= ''; + $schema_insert .= ''; + $schema_insert .= ''; + $schema_insert .= ''; + if ($do_relation && $have_rel) { + $schema_insert .= ''; + } + if ($do_comments) { + $schema_insert .= ''; + $comments = $this->relation->getComments($db, $table); + } + if ($do_mime && $cfgRelation['mimework']) { + $schema_insert .= ''; + $mime_map = $this->transformations->getMime($db, $table, true); + } + $schema_insert .= ''; + + $columns = $GLOBALS['dbi']->getColumns($db, $table); + /** + * Get the unique keys in the table + */ + $unique_keys = []; + $keys = $GLOBALS['dbi']->getTableIndexes($db, $table); + foreach ($keys as $key) { + if ($key['Non_unique'] == 0) { + $unique_keys[] = $key['Column_name']; + } + } + foreach ($columns as $column) { + $col_as = $column['Field']; + if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) { + $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as]; + } + $schema_insert .= $this->formatOneColumnDefinition( + $column, + $unique_keys, + $col_as + ); + $field_name = $column['Field']; + if ($do_relation && $have_rel) { + $schema_insert .= ''; + } + if ($do_comments && $cfgRelation['commwork']) { + $schema_insert .= ''; + } + if ($do_mime && $cfgRelation['mimework']) { + $schema_insert .= ''; + } + + $schema_insert .= ''; + } // end foreach + + $schema_insert .= '
' + . htmlspecialchars( + $this->getRelationString( + $res_rel, + $field_name, + $db, + $aliases + ) + ) + . '' + . (isset($comments[$field_name]) + ? htmlspecialchars($comments[$field_name]) + : '') . '' + . (isset($mime_map[$field_name]) ? + htmlspecialchars( + str_replace('_', '/', $mime_map[$field_name]['mimetype']) + ) + : '') . '
'; + + return $schema_insert; + } + + /** + * Outputs triggers + * + * @param string $db database name + * @param string $table table name + * + * @return string Formatted triggers list + */ + protected function getTriggers($db, $table) + { + $dump = ''; + $dump .= ''; + $dump .= ''; + $dump .= ''; + $dump .= ''; + $dump .= ''; + $dump .= ''; + + $triggers = $GLOBALS['dbi']->getTriggers($db, $table); + + foreach ($triggers as $trigger) { + $dump .= ''; + $dump .= '' + . '' + . '' + . '' + . ''; + } + + $dump .= '
'; + + return $dump; + } + + /** + * Outputs table's structure + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $export_mode 'create_table', 'triggers', 'create_view', + * 'stand_in' + * @param string $export_type 'server', 'database', 'table' + * @param bool $do_relation whether to include relation comments + * @param bool $do_comments whether to include the pmadb-style column + * comments as comments in the structure; + * this is deprecated but the parameter is + * left here because export.php calls + * PMA_exportStructure() also for other + * export types which use this parameter + * @param bool $do_mime whether to include mime comments + * @param bool $dates whether to include creation/update/check dates + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportStructure( + $db, + $table, + $crlf, + $error_url, + $export_mode, + $export_type, + $do_relation = false, + $do_comments = false, + $do_mime = false, + $dates = false, + array $aliases = [] + ) { + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + + $dump = ''; + + switch ($export_mode) { + case 'create_table': + $dump .= '

' + . __('Table structure for table') . ' ' + . htmlspecialchars($table_alias) + . '

'; + $dump .= $this->getTableDef( + $db, + $table, + $do_relation, + $do_comments, + $do_mime, + false, + $aliases + ); + break; + case 'triggers': + $dump = ''; + $triggers = $GLOBALS['dbi']->getTriggers($db, $table); + if ($triggers) { + $dump .= '

' + . __('Triggers') . ' ' . htmlspecialchars($table_alias) + . '

'; + $dump .= $this->getTriggers($db, $table); + } + break; + case 'create_view': + $dump .= '

' + . __('Structure for view') . ' ' . htmlspecialchars($table_alias) + . '

'; + $dump .= $this->getTableDef( + $db, + $table, + $do_relation, + $do_comments, + $do_mime, + true, + $aliases + ); + break; + case 'stand_in': + $dump .= '

' + . __('Stand-in structure for view') . ' ' + . htmlspecialchars($table_alias) + . '

'; + // export a stand-in definition to resolve view dependencies + $dump .= $this->getTableDefStandIn($db, $table, $crlf, $aliases); + } // end switch + + return $this->export->outputHandler($dump); + } + + /** + * Formats the definition for one column + * + * @param array $column info about this column + * @param array $unique_keys unique keys of the table + * @param string $col_alias Column Alias + * + * @return string Formatted column definition + */ + protected function formatOneColumnDefinition( + array $column, + array $unique_keys, + $col_alias = '' + ) { + if (empty($col_alias)) { + $col_alias = $column['Field']; + } + $definition = ''; + + $extracted_columnspec = Util::extractColumnSpec($column['Type']); + + $type = htmlspecialchars($extracted_columnspec['print_type']); + if (empty($type)) { + $type = ' '; + } + + if (! isset($column['Default'])) { + if ($column['Null'] != 'NO') { + $column['Default'] = 'NULL'; + } + } + + $fmt_pre = ''; + $fmt_post = ''; + if (in_array($column['Field'], $unique_keys)) { + $fmt_pre = '' . $fmt_pre; + $fmt_post .= ''; + } + if ($column['Key'] == 'PRI') { + $fmt_pre = '' . $fmt_pre; + $fmt_post .= ''; + } + $definition .= '' . $fmt_pre + . htmlspecialchars($col_alias) . $fmt_post . ''; + $definition .= '' . htmlspecialchars($type) . ''; + $definition .= '' + . (($column['Null'] == '' || $column['Null'] == 'NO') + ? __('No') + : __('Yes')) + . ''; + $definition .= '' + . htmlspecialchars(isset($column['Default']) ? $column['Default'] : '') + . ''; + + return $definition; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportJson.php b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportJson.php new file mode 100644 index 0000000..4ebfbbe --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportJson.php @@ -0,0 +1,295 @@ +setProperties(); + } + + /** + * Encodes the data into JSON + * + * @param mixed $data Data to encode + * + * @return string + */ + public function encode($data) + { + $options = 0; + if (isset($GLOBALS['json_pretty_print']) + && $GLOBALS['json_pretty_print'] + ) { + $options |= JSON_PRETTY_PRINT; + } + if (isset($GLOBALS['json_unicode']) + && $GLOBALS['json_unicode'] + ) { + $options |= JSON_UNESCAPED_UNICODE; + } + return json_encode($data, $options); + } + + /** + * Sets the export JSON properties + * + * @return void + */ + protected function setProperties() + { + $exportPluginProperties = new ExportPluginProperties(); + $exportPluginProperties->setText('JSON'); + $exportPluginProperties->setExtension('json'); + $exportPluginProperties->setMimeType('text/plain'); + $exportPluginProperties->setOptionsText(__('Options')); + + // create the root group that will be the options field for + // $exportPluginProperties + // this will be shown as "Format specific options" + $exportSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // general options main group + $generalOptions = new OptionsPropertyMainGroup("general_opts"); + // create primary items and add them to the group + $leaf = new HiddenPropertyItem("structure_or_data"); + $generalOptions->addProperty($leaf); + + $leaf = new BoolPropertyItem( + 'pretty_print', + __('Output pretty-printed JSON (Use human-readable formatting)') + ); + $generalOptions->addProperty($leaf); + + $leaf = new BoolPropertyItem( + 'unicode', + __('Output unicode characters unescaped') + ); + $generalOptions->addProperty($leaf); + + // add the main group to the root group + $exportSpecificOptions->addProperty($generalOptions); + + // set the options for the export plugin property item + $exportPluginProperties->setOptions($exportSpecificOptions); + $this->properties = $exportPluginProperties; + } + + /** + * Outputs export header + * + * @return bool Whether it succeeded + */ + public function exportHeader() + { + global $crlf; + + $meta = [ + 'type' => 'header', + 'version' => PMA_VERSION, + 'comment' => 'Export to JSON plugin for PHPMyAdmin', + ]; + + return $this->export->outputHandler( + '[' . $crlf . $this->encode($meta) . ',' . $crlf + ); + } + + /** + * Outputs export footer + * + * @return bool Whether it succeeded + */ + public function exportFooter() + { + global $crlf; + + return $this->export->outputHandler(']' . $crlf); + } + + /** + * Outputs database header + * + * @param string $db Database name + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBHeader($db, $db_alias = '') + { + global $crlf; + + if (empty($db_alias)) { + $db_alias = $db; + } + + $meta = [ + 'type' => 'database', + 'name' => $db_alias, + ]; + + return $this->export->outputHandler( + $this->encode($meta) . ',' . $crlf + ); + } + + /** + * Outputs database footer + * + * @param string $db Database name + * + * @return bool Whether it succeeded + */ + public function exportDBFooter($db) + { + return true; + } + + /** + * Outputs CREATE DATABASE statement + * + * @param string $db Database name + * @param string $export_type 'server', 'database', 'table' + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBCreate($db, $export_type, $db_alias = '') + { + return true; + } + + /** + * Outputs the content of a table in JSON format + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $sql_query SQL query for obtaining data + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportData( + $db, + $table, + $crlf, + $error_url, + $sql_query, + array $aliases = [] + ) { + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + + if (! $this->first) { + if (! $this->export->outputHandler(',')) { + return false; + } + } else { + $this->first = false; + } + + $buffer = $this->encode( + [ + 'type' => 'table', + 'name' => $table_alias, + 'database' => $db_alias, + 'data' => "@@DATA@@", + ] + ); + list($header, $footer) = explode('"@@DATA@@"', $buffer); + + if (! $this->export->outputHandler($header . $crlf . '[' . $crlf)) { + return false; + } + + $result = $GLOBALS['dbi']->query( + $sql_query, + DatabaseInterface::CONNECT_USER, + DatabaseInterface::QUERY_UNBUFFERED + ); + $columns_cnt = $GLOBALS['dbi']->numFields($result); + $fieldsMeta = $GLOBALS['dbi']->getFieldsMeta($result); + + $columns = []; + for ($i = 0; $i < $columns_cnt; $i++) { + $col_as = $GLOBALS['dbi']->fieldName($result, $i); + if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) { + $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as]; + } + $columns[$i] = stripslashes($col_as); + } + + $record_cnt = 0; + while ($record = $GLOBALS['dbi']->fetchRow($result)) { + $record_cnt++; + + // Output table name as comment if this is the first record of the table + if ($record_cnt > 1) { + if (! $this->export->outputHandler(',' . $crlf)) { + return false; + } + } + + $data = []; + + for ($i = 0; $i < $columns_cnt; $i++) { + if ($fieldsMeta[$i]->type === 'geometry') { + // export GIS types as hex + $record[$i] = '0x' . bin2hex($record[$i]); + } + $data[$columns[$i]] = $record[$i]; + } + + $encodedData = $this->encode($data); + if (! $encodedData) { + return false; + } + if (! $this->export->outputHandler($encodedData)) { + return false; + } + } + + if (! $this->export->outputHandler($crlf . ']' . $crlf . $footer . $crlf)) { + return false; + } + + $GLOBALS['dbi']->freeResult($result); + + return true; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportLatex.php b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportLatex.php new file mode 100644 index 0000000..e215300 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportLatex.php @@ -0,0 +1,709 @@ +initSpecificVariables(); + $this->setProperties(); + } + + /** + * Initialize the local variables that are used for export Latex + * + * @return void + */ + protected function initSpecificVariables() + { + /* Messages used in default captions */ + $GLOBALS['strLatexContent'] = __('Content of table @TABLE@'); + $GLOBALS['strLatexContinued'] = __('(continued)'); + $GLOBALS['strLatexStructure'] = __('Structure of table @TABLE@'); + } + + /** + * Sets the export Latex properties + * + * @return void + */ + protected function setProperties() + { + global $plugin_param; + $hide_structure = false; + if ($plugin_param['export_type'] == 'table' + && ! $plugin_param['single_table'] + ) { + $hide_structure = true; + } + + $exportPluginProperties = new ExportPluginProperties(); + $exportPluginProperties->setText('LaTeX'); + $exportPluginProperties->setExtension('tex'); + $exportPluginProperties->setMimeType('application/x-tex'); + $exportPluginProperties->setOptionsText(__('Options')); + + // create the root group that will be the options field for + // $exportPluginProperties + // this will be shown as "Format specific options" + $exportSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // general options main group + $generalOptions = new OptionsPropertyMainGroup("general_opts"); + // create primary items and add them to the group + $leaf = new BoolPropertyItem( + "caption", + __('Include table caption') + ); + $generalOptions->addProperty($leaf); + // add the main group to the root group + $exportSpecificOptions->addProperty($generalOptions); + + // what to dump (structure/data/both) main group + $dumpWhat = new OptionsPropertyMainGroup( + "dump_what", + __('Dump table') + ); + // create primary items and add them to the group + $leaf = new RadioPropertyItem("structure_or_data"); + $leaf->setValues( + [ + 'structure' => __('structure'), + 'data' => __('data'), + 'structure_and_data' => __('structure and data'), + ] + ); + $dumpWhat->addProperty($leaf); + // add the main group to the root group + $exportSpecificOptions->addProperty($dumpWhat); + + // structure options main group + if (! $hide_structure) { + $structureOptions = new OptionsPropertyMainGroup( + "structure", + __('Object creation options') + ); + $structureOptions->setForce('data'); + // create primary items and add them to the group + $leaf = new TextPropertyItem( + "structure_caption", + __('Table caption:') + ); + $leaf->setDoc('faq6-27'); + $structureOptions->addProperty($leaf); + $leaf = new TextPropertyItem( + "structure_continued_caption", + __('Table caption (continued):') + ); + $leaf->setDoc('faq6-27'); + $structureOptions->addProperty($leaf); + $leaf = new TextPropertyItem( + "structure_label", + __('Label key:') + ); + $leaf->setDoc('faq6-27'); + $structureOptions->addProperty($leaf); + if (! empty($GLOBALS['cfgRelation']['relation'])) { + $leaf = new BoolPropertyItem( + "relation", + __('Display foreign key relationships') + ); + $structureOptions->addProperty($leaf); + } + $leaf = new BoolPropertyItem( + "comments", + __('Display comments') + ); + $structureOptions->addProperty($leaf); + if (! empty($GLOBALS['cfgRelation']['mimework'])) { + $leaf = new BoolPropertyItem( + "mime", + __('Display media (MIME) types') + ); + $structureOptions->addProperty($leaf); + } + // add the main group to the root group + $exportSpecificOptions->addProperty($structureOptions); + } + + // data options main group + $dataOptions = new OptionsPropertyMainGroup( + "data", + __('Data dump options') + ); + $dataOptions->setForce('structure'); + // create primary items and add them to the group + $leaf = new BoolPropertyItem( + "columns", + __('Put columns names in the first row:') + ); + $dataOptions->addProperty($leaf); + $leaf = new TextPropertyItem( + "data_caption", + __('Table caption:') + ); + $leaf->setDoc('faq6-27'); + $dataOptions->addProperty($leaf); + $leaf = new TextPropertyItem( + "data_continued_caption", + __('Table caption (continued):') + ); + $leaf->setDoc('faq6-27'); + $dataOptions->addProperty($leaf); + $leaf = new TextPropertyItem( + "data_label", + __('Label key:') + ); + $leaf->setDoc('faq6-27'); + $dataOptions->addProperty($leaf); + $leaf = new TextPropertyItem( + 'null', + __('Replace NULL with:') + ); + $dataOptions->addProperty($leaf); + // add the main group to the root group + $exportSpecificOptions->addProperty($dataOptions); + + // set the options for the export plugin property item + $exportPluginProperties->setOptions($exportSpecificOptions); + $this->properties = $exportPluginProperties; + } + + /** + * Outputs export header + * + * @return bool Whether it succeeded + */ + public function exportHeader() + { + global $crlf; + global $cfg; + + $head = '% phpMyAdmin LaTeX Dump' . $crlf + . '% version ' . PMA_VERSION . $crlf + . '% https://www.phpmyadmin.net/' . $crlf + . '%' . $crlf + . '% ' . __('Host:') . ' ' . $cfg['Server']['host']; + if (! empty($cfg['Server']['port'])) { + $head .= ':' . $cfg['Server']['port']; + } + $head .= $crlf + . '% ' . __('Generation Time:') . ' ' + . Util::localisedDate() . $crlf + . '% ' . __('Server version:') . ' ' . $GLOBALS['dbi']->getVersionString() . $crlf + . '% ' . __('PHP Version:') . ' ' . PHP_VERSION . $crlf; + + return $this->export->outputHandler($head); + } + + /** + * Outputs export footer + * + * @return bool Whether it succeeded + */ + public function exportFooter() + { + return true; + } + + /** + * Outputs database header + * + * @param string $db Database name + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBHeader($db, $db_alias = '') + { + if (empty($db_alias)) { + $db_alias = $db; + } + global $crlf; + $head = '% ' . $crlf + . '% ' . __('Database:') . ' \'' . $db_alias . '\'' . $crlf + . '% ' . $crlf; + + return $this->export->outputHandler($head); + } + + /** + * Outputs database footer + * + * @param string $db Database name + * + * @return bool Whether it succeeded + */ + public function exportDBFooter($db) + { + return true; + } + + /** + * Outputs CREATE DATABASE statement + * + * @param string $db Database name + * @param string $export_type 'server', 'database', 'table' + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBCreate($db, $export_type, $db_alias = '') + { + return true; + } + + /** + * Outputs the content of a table in JSON format + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $sql_query SQL query for obtaining data + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportData( + $db, + $table, + $crlf, + $error_url, + $sql_query, + array $aliases = [] + ) { + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + + $result = $GLOBALS['dbi']->tryQuery( + $sql_query, + DatabaseInterface::CONNECT_USER, + DatabaseInterface::QUERY_UNBUFFERED + ); + + $columns_cnt = $GLOBALS['dbi']->numFields($result); + $columns = []; + $columns_alias = []; + for ($i = 0; $i < $columns_cnt; $i++) { + $columns[$i] = $col_as = $GLOBALS['dbi']->fieldName($result, $i); + if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) { + $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as]; + } + $columns_alias[$i] = $col_as; + } + + $buffer = $crlf . '%' . $crlf . '% ' . __('Data:') . ' ' . $table_alias + . $crlf . '%' . $crlf . ' \\begin{longtable}{|'; + + for ($index = 0; $index < $columns_cnt; $index++) { + $buffer .= 'l|'; + } + $buffer .= '} ' . $crlf; + + $buffer .= ' \\hline \\endhead \\hline \\endfoot \\hline ' . $crlf; + if (isset($GLOBALS['latex_caption'])) { + $buffer .= ' \\caption{' + . Util::expandUserString( + $GLOBALS['latex_data_caption'], + [ + 'texEscape', + static::class, + ], + [ + 'table' => $table_alias, + 'database' => $db_alias, + ] + ) + . '} \\label{' + . Util::expandUserString( + $GLOBALS['latex_data_label'], + null, + [ + 'table' => $table_alias, + 'database' => $db_alias, + ] + ) + . '} \\\\'; + } + if (! $this->export->outputHandler($buffer)) { + return false; + } + + // show column names + if (isset($GLOBALS['latex_columns'])) { + $buffer = '\\hline '; + for ($i = 0; $i < $columns_cnt; $i++) { + $buffer .= '\\multicolumn{1}{|c|}{\\textbf{' + . self::texEscape(stripslashes($columns_alias[$i])) . '}} & '; + } + + $buffer = mb_substr($buffer, 0, -2) . '\\\\ \\hline \hline '; + if (! $this->export->outputHandler($buffer . ' \\endfirsthead ' . $crlf)) { + return false; + } + if (isset($GLOBALS['latex_caption'])) { + if (! $this->export->outputHandler( + '\\caption{' + . Util::expandUserString( + $GLOBALS['latex_data_continued_caption'], + [ + 'texEscape', + static::class, + ], + [ + 'table' => $table_alias, + 'database' => $db_alias, + ] + ) + . '} \\\\ ' + ) + ) { + return false; + } + } + if (! $this->export->outputHandler($buffer . '\\endhead \\endfoot' . $crlf)) { + return false; + } + } else { + if (! $this->export->outputHandler('\\\\ \hline')) { + return false; + } + } + + // print the whole table + while ($record = $GLOBALS['dbi']->fetchAssoc($result)) { + $buffer = ''; + // print each row + for ($i = 0; $i < $columns_cnt; $i++) { + if ($record[$columns[$i]] !== null + && isset($record[$columns[$i]]) + ) { + $column_value = self::texEscape( + stripslashes($record[$columns[$i]]) + ); + } else { + $column_value = $GLOBALS['latex_null']; + } + + // last column ... no need for & character + if ($i == ($columns_cnt - 1)) { + $buffer .= $column_value; + } else { + $buffer .= $column_value . " & "; + } + } + $buffer .= ' \\\\ \\hline ' . $crlf; + if (! $this->export->outputHandler($buffer)) { + return false; + } + } + + $buffer = ' \\end{longtable}' . $crlf; + if (! $this->export->outputHandler($buffer)) { + return false; + } + + $GLOBALS['dbi']->freeResult($result); + + return true; + } // end getTableLaTeX + + /** + * Outputs table's structure + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $export_mode 'create_table', 'triggers', 'create_view', + * 'stand_in' + * @param string $export_type 'server', 'database', 'table' + * @param bool $do_relation whether to include relation comments + * @param bool $do_comments whether to include the pmadb-style column + * comments as comments in the structure; + * this is deprecated but the parameter is + * left here because export.php calls + * exportStructure() also for other + * export types which use this parameter + * @param bool $do_mime whether to include mime comments + * @param bool $dates whether to include creation/update/check dates + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportStructure( + $db, + $table, + $crlf, + $error_url, + $export_mode, + $export_type, + $do_relation = false, + $do_comments = false, + $do_mime = false, + $dates = false, + array $aliases = [] + ) { + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + + global $cfgRelation; + + /* We do not export triggers */ + if ($export_mode == 'triggers') { + return true; + } + + /** + * Get the unique keys in the table + */ + $unique_keys = []; + $keys = $GLOBALS['dbi']->getTableIndexes($db, $table); + foreach ($keys as $key) { + if ($key['Non_unique'] == 0) { + $unique_keys[] = $key['Column_name']; + } + } + + /** + * Gets fields properties + */ + $GLOBALS['dbi']->selectDb($db); + + // Check if we can use Relations + list($res_rel, $have_rel) = $this->relation->getRelationsAndStatus( + $do_relation && ! empty($cfgRelation['relation']), + $db, + $table + ); + /** + * Displays the table structure + */ + $buffer = $crlf . '%' . $crlf . '% ' . __('Structure:') . ' ' + . $table_alias . $crlf . '%' . $crlf . ' \\begin{longtable}{'; + if (! $this->export->outputHandler($buffer)) { + return false; + } + + $alignment = '|l|c|c|c|'; + if ($do_relation && $have_rel) { + $alignment .= 'l|'; + } + if ($do_comments) { + $alignment .= 'l|'; + } + if ($do_mime && $cfgRelation['mimework']) { + $alignment .= 'l|'; + } + $buffer = $alignment . '} ' . $crlf; + + $header = ' \\hline '; + $header .= '\\multicolumn{1}{|c|}{\\textbf{' . __('Column') + . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Type') + . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Null') + . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Default') . '}}'; + if ($do_relation && $have_rel) { + $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . __('Links to') . '}}'; + } + if ($do_comments) { + $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . __('Comments') . '}}'; + $comments = $this->relation->getComments($db, $table); + } + if ($do_mime && $cfgRelation['mimework']) { + $header .= ' & \\multicolumn{1}{|c|}{\\textbf{MIME}}'; + $mime_map = $this->transformations->getMime($db, $table, true); + } + + // Table caption for first page and label + if (isset($GLOBALS['latex_caption'])) { + $buffer .= ' \\caption{' + . Util::expandUserString( + $GLOBALS['latex_structure_caption'], + [ + 'texEscape', + static::class, + ], + [ + 'table' => $table_alias, + 'database' => $db_alias, + ] + ) + . '} \\label{' + . Util::expandUserString( + $GLOBALS['latex_structure_label'], + null, + [ + 'table' => $table_alias, + 'database' => $db_alias, + ] + ) + . '} \\\\' . $crlf; + } + $buffer .= $header . ' \\\\ \\hline \\hline' . $crlf + . '\\endfirsthead' . $crlf; + // Table caption on next pages + if (isset($GLOBALS['latex_caption'])) { + $buffer .= ' \\caption{' + . Util::expandUserString( + $GLOBALS['latex_structure_continued_caption'], + [ + 'texEscape', + static::class, + ], + [ + 'table' => $table_alias, + 'database' => $db_alias, + ] + ) + . '} \\\\ ' . $crlf; + } + $buffer .= $header . ' \\\\ \\hline \\hline \\endhead \\endfoot ' . $crlf; + + if (! $this->export->outputHandler($buffer)) { + return false; + } + + $fields = $GLOBALS['dbi']->getColumns($db, $table); + foreach ($fields as $row) { + $extracted_columnspec = Util::extractColumnSpec($row['Type']); + $type = $extracted_columnspec['print_type']; + if (empty($type)) { + $type = ' '; + } + + if (! isset($row['Default'])) { + if ($row['Null'] != 'NO') { + $row['Default'] = 'NULL'; + } + } + + $field_name = $col_as = $row['Field']; + if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) { + $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as]; + } + + $local_buffer = $col_as . "\000" . $type . "\000" + . (($row['Null'] == '' || $row['Null'] == 'NO') + ? __('No') : __('Yes')) + . "\000" . (isset($row['Default']) ? $row['Default'] : ''); + + if ($do_relation && $have_rel) { + $local_buffer .= "\000"; + $local_buffer .= $this->getRelationString( + $res_rel, + $field_name, + $db, + $aliases + ); + } + if ($do_comments && $cfgRelation['commwork']) { + $local_buffer .= "\000"; + if (isset($comments[$field_name])) { + $local_buffer .= $comments[$field_name]; + } + } + if ($do_mime && $cfgRelation['mimework']) { + $local_buffer .= "\000"; + if (isset($mime_map[$field_name])) { + $local_buffer .= str_replace( + '_', + '/', + $mime_map[$field_name]['mimetype'] + ); + } + } + $local_buffer = self::texEscape($local_buffer); + if ($row['Key'] == 'PRI') { + $pos = mb_strpos($local_buffer, "\000"); + $local_buffer = '\\textit{' + . + mb_substr($local_buffer, 0, $pos) + . '}' . + mb_substr($local_buffer, $pos); + } + if (in_array($field_name, $unique_keys)) { + $pos = mb_strpos($local_buffer, "\000"); + $local_buffer = '\\textbf{' + . + mb_substr($local_buffer, 0, $pos) + . '}' . + mb_substr($local_buffer, $pos); + } + $buffer = str_replace("\000", ' & ', $local_buffer); + $buffer .= ' \\\\ \\hline ' . $crlf; + + if (! $this->export->outputHandler($buffer)) { + return false; + } + } // end while + + $buffer = ' \\end{longtable}' . $crlf; + + return $this->export->outputHandler($buffer); + } // end of the 'exportStructure' method + + /** + * Escapes some special characters for use in TeX/LaTeX + * + * @param string $string the string to convert + * + * @return string the converted string with escape codes + */ + public static function texEscape($string) + { + $escape = [ + '$', + '%', + '{', + '}', + '&', + '#', + '_', + '^', + ]; + $cnt_escape = count($escape); + for ($k = 0; $k < $cnt_escape; $k++) { + $string = str_replace($escape[$k], '\\' . $escape[$k], $string); + } + + return $string; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportMediawiki.php b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportMediawiki.php new file mode 100644 index 0000000..9d971cb --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportMediawiki.php @@ -0,0 +1,386 @@ +setProperties(); + } + + /** + * Sets the export MediaWiki properties + * + * @return void + */ + protected function setProperties() + { + $exportPluginProperties = new ExportPluginProperties(); + $exportPluginProperties->setText('MediaWiki Table'); + $exportPluginProperties->setExtension('mediawiki'); + $exportPluginProperties->setMimeType('text/plain'); + $exportPluginProperties->setOptionsText(__('Options')); + + // create the root group that will be the options field for + // $exportPluginProperties + // this will be shown as "Format specific options" + $exportSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // general options main group + $generalOptions = new OptionsPropertyMainGroup( + "general_opts", + __('Dump table') + ); + + // what to dump (structure/data/both) + $subgroup = new OptionsPropertySubgroup( + "dump_table", + __("Dump table") + ); + $leaf = new RadioPropertyItem('structure_or_data'); + $leaf->setValues( + [ + 'structure' => __('structure'), + 'data' => __('data'), + 'structure_and_data' => __('structure and data'), + ] + ); + $subgroup->setSubgroupHeader($leaf); + $generalOptions->addProperty($subgroup); + + // export table name + $leaf = new BoolPropertyItem( + "caption", + __('Export table names') + ); + $generalOptions->addProperty($leaf); + + // export table headers + $leaf = new BoolPropertyItem( + "headers", + __('Export table headers') + ); + $generalOptions->addProperty($leaf); + //add the main group to the root group + $exportSpecificOptions->addProperty($generalOptions); + + // set the options for the export plugin property item + $exportPluginProperties->setOptions($exportSpecificOptions); + $this->properties = $exportPluginProperties; + } + + /** + * Outputs export header + * + * @return bool Whether it succeeded + */ + public function exportHeader() + { + return true; + } + + /** + * Outputs export footer + * + * @return bool Whether it succeeded + */ + public function exportFooter() + { + return true; + } + + /** + * Outputs database header + * + * @param string $db Database name + * @param string $db_alias Alias of db + * + * @return bool Whether it succeeded + */ + public function exportDBHeader($db, $db_alias = '') + { + return true; + } + + /** + * Outputs database footer + * + * @param string $db Database name + * + * @return bool Whether it succeeded + */ + public function exportDBFooter($db) + { + return true; + } + + /** + * Outputs CREATE DATABASE statement + * + * @param string $db Database name + * @param string $export_type 'server', 'database', 'table' + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBCreate($db, $export_type, $db_alias = '') + { + return true; + } + + /** + * Outputs table's structure + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $export_mode 'create_table','triggers','create_view', + * 'stand_in' + * @param string $export_type 'server', 'database', 'table' + * @param bool $do_relation whether to include relation comments + * @param bool $do_comments whether to include the pmadb-style column + * comments as comments in the structure; this is + * deprecated but the parameter is left here + * because export.php calls exportStructure() + * also for other export types which use this + * parameter + * @param bool $do_mime whether to include mime comments + * @param bool $dates whether to include creation/update/check dates + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportStructure( + $db, + $table, + $crlf, + $error_url, + $export_mode, + $export_type, + $do_relation = false, + $do_comments = false, + $do_mime = false, + $dates = false, + array $aliases = [] + ) { + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + + $output = ''; + switch ($export_mode) { + case 'create_table': + $columns = $GLOBALS['dbi']->getColumns($db, $table); + $columns = array_values($columns); + $row_cnt = count($columns); + + // Print structure comment + $output = $this->_exportComment( + "Table structure for " + . Util::backquote($table_alias) + ); + + // Begin the table construction + $output .= "{| class=\"wikitable\" style=\"text-align:center;\"" + . $this->_exportCRLF(); + + // Add the table name + if (isset($GLOBALS['mediawiki_caption'])) { + $output .= "|+'''" . $table_alias . "'''" . $this->_exportCRLF(); + } + + // Add the table headers + if (isset($GLOBALS['mediawiki_headers'])) { + $output .= "|- style=\"background:#ffdead;\"" . $this->_exportCRLF(); + $output .= "! style=\"background:#ffffff\" | " + . $this->_exportCRLF(); + for ($i = 0; $i < $row_cnt; ++$i) { + $col_as = $columns[$i]['Field']; + if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as]) + ) { + $col_as + = $aliases[$db]['tables'][$table]['columns'][$col_as]; + } + $output .= " | " . $col_as . $this->_exportCRLF(); + } + } + + // Add the table structure + $output .= "|-" . $this->_exportCRLF(); + $output .= "! Type" . $this->_exportCRLF(); + for ($i = 0; $i < $row_cnt; ++$i) { + $output .= " | " . $columns[$i]['Type'] . $this->_exportCRLF(); + } + + $output .= "|-" . $this->_exportCRLF(); + $output .= "! Null" . $this->_exportCRLF(); + for ($i = 0; $i < $row_cnt; ++$i) { + $output .= " | " . $columns[$i]['Null'] . $this->_exportCRLF(); + } + + $output .= "|-" . $this->_exportCRLF(); + $output .= "! Default" . $this->_exportCRLF(); + for ($i = 0; $i < $row_cnt; ++$i) { + $output .= " | " . $columns[$i]['Default'] . $this->_exportCRLF(); + } + + $output .= "|-" . $this->_exportCRLF(); + $output .= "! Extra" . $this->_exportCRLF(); + for ($i = 0; $i < $row_cnt; ++$i) { + $output .= " | " . $columns[$i]['Extra'] . $this->_exportCRLF(); + } + + $output .= "|}" . str_repeat($this->_exportCRLF(), 2); + break; + } // end switch + + return $this->export->outputHandler($output); + } + + /** + * Outputs the content of a table in MediaWiki format + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $sql_query SQL query for obtaining data + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportData( + $db, + $table, + $crlf, + $error_url, + $sql_query, + array $aliases = [] + ) { + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + + // Print data comment + $output = $this->_exportComment( + "Table data for " . Util::backquote($table_alias) + ); + + // Begin the table construction + // Use the "wikitable" class for style + // Use the "sortable" class for allowing tables to be sorted by column + $output .= "{| class=\"wikitable sortable\" style=\"text-align:center;\"" + . $this->_exportCRLF(); + + // Add the table name + if (isset($GLOBALS['mediawiki_caption'])) { + $output .= "|+'''" . $table_alias . "'''" . $this->_exportCRLF(); + } + + // Add the table headers + if (isset($GLOBALS['mediawiki_headers'])) { + // Get column names + $column_names = $GLOBALS['dbi']->getColumnNames($db, $table); + + // Add column names as table headers + if ($column_names !== null) { + // Use '|-' for separating rows + $output .= "|-" . $this->_exportCRLF(); + + // Use '!' for separating table headers + foreach ($column_names as $column) { + if (! empty($aliases[$db]['tables'][$table]['columns'][$column]) + ) { + $column + = $aliases[$db]['tables'][$table]['columns'][$column]; + } + $output .= " ! " . $column . "" . $this->_exportCRLF(); + } + } + } + + // Get the table data from the database + $result = $GLOBALS['dbi']->query( + $sql_query, + DatabaseInterface::CONNECT_USER, + DatabaseInterface::QUERY_UNBUFFERED + ); + $fields_cnt = $GLOBALS['dbi']->numFields($result); + + while ($row = $GLOBALS['dbi']->fetchRow($result)) { + $output .= "|-" . $this->_exportCRLF(); + + // Use '|' for separating table columns + for ($i = 0; $i < $fields_cnt; ++$i) { + $output .= " | " . $row[$i] . "" . $this->_exportCRLF(); + } + } + + // End table construction + $output .= "|}" . str_repeat($this->_exportCRLF(), 2); + + return $this->export->outputHandler($output); + } + + /** + * Outputs comments containing info about the exported tables + * + * @param string $text Text of comment + * + * @return string The formatted comment + */ + private function _exportComment($text = '') + { + // see https://www.mediawiki.org/wiki/Help:Formatting + $comment = $this->_exportCRLF(); + $comment .= '' . str_repeat($this->_exportCRLF(), 2); + + return $comment; + } + + /** + * Outputs CRLF + * + * @return string CRLF + */ + private function _exportCRLF() + { + // The CRLF expected by the mediawiki format is "\n" + return "\n"; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportOds.php b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportOds.php new file mode 100644 index 0000000..bc062da --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportOds.php @@ -0,0 +1,345 @@ +setProperties(); + } + + /** + * Sets the export ODS properties + * + * @return void + */ + protected function setProperties() + { + $exportPluginProperties = new ExportPluginProperties(); + $exportPluginProperties->setText('OpenDocument Spreadsheet'); + $exportPluginProperties->setExtension('ods'); + $exportPluginProperties->setMimeType( + 'application/vnd.oasis.opendocument.spreadsheet' + ); + $exportPluginProperties->setForceFile(true); + $exportPluginProperties->setOptionsText(__('Options')); + + // create the root group that will be the options field for + // $exportPluginProperties + // this will be shown as "Format specific options" + $exportSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // general options main group + $generalOptions = new OptionsPropertyMainGroup("general_opts"); + // create primary items and add them to the group + $leaf = new TextPropertyItem( + "null", + __('Replace NULL with:') + ); + $generalOptions->addProperty($leaf); + $leaf = new BoolPropertyItem( + "columns", + __('Put columns names in the first row') + ); + $generalOptions->addProperty($leaf); + $leaf = new HiddenPropertyItem("structure_or_data"); + $generalOptions->addProperty($leaf); + // add the main group to the root group + $exportSpecificOptions->addProperty($generalOptions); + + // set the options for the export plugin property item + $exportPluginProperties->setOptions($exportSpecificOptions); + $this->properties = $exportPluginProperties; + } + + /** + * Outputs export header + * + * @return bool Whether it succeeded + */ + public function exportHeader() + { + $GLOBALS['ods_buffer'] .= '' + . '' + . '' + . '' + . '' + . '/' + . '' + . '/' + . '' + . '' + . '' + . '' + . ':' + . '' + . ':' + . '' + . ' ' + . '' + . '' + . '' + . '' + . '/' + . '' + . '/' + . '' + . ' ' + . '' + . ':' + . '' + . ' ' + . '' + . '' + . '' + . '' + . '' + . '' + . '' + . ''; + + return true; + } + + /** + * Outputs export footer + * + * @return bool Whether it succeeded + */ + public function exportFooter() + { + $GLOBALS['ods_buffer'] .= '' + . '' + . ''; + + return $this->export->outputHandler( + OpenDocument::create( + 'application/vnd.oasis.opendocument.spreadsheet', + $GLOBALS['ods_buffer'] + ) + ); + } + + /** + * Outputs database header + * + * @param string $db Database name + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBHeader($db, $db_alias = '') + { + return true; + } + + /** + * Outputs database footer + * + * @param string $db Database name + * + * @return bool Whether it succeeded + */ + public function exportDBFooter($db) + { + return true; + } + + /** + * Outputs CREATE DATABASE statement + * + * @param string $db Database name + * @param string $export_type 'server', 'database', 'table' + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBCreate($db, $export_type, $db_alias = '') + { + return true; + } + + /** + * Outputs the content of a table in NHibernate format + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $sql_query SQL query for obtaining data + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportData( + $db, + $table, + $crlf, + $error_url, + $sql_query, + array $aliases = [] + ) { + global $what; + + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + // Gets the data from the database + $result = $GLOBALS['dbi']->query( + $sql_query, + DatabaseInterface::CONNECT_USER, + DatabaseInterface::QUERY_UNBUFFERED + ); + $fields_cnt = $GLOBALS['dbi']->numFields($result); + $fields_meta = $GLOBALS['dbi']->getFieldsMeta($result); + $field_flags = []; + for ($j = 0; $j < $fields_cnt; $j++) { + $field_flags[$j] = $GLOBALS['dbi']->fieldFlags($result, $j); + } + + $GLOBALS['ods_buffer'] + .= ''; + + // If required, get fields name at the first line + if (isset($GLOBALS[$what . '_columns'])) { + $GLOBALS['ods_buffer'] .= ''; + for ($i = 0; $i < $fields_cnt; $i++) { + $col_as = $GLOBALS['dbi']->fieldName($result, $i); + if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) { + $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as]; + } + $GLOBALS['ods_buffer'] + .= '' + . '' + . htmlspecialchars( + stripslashes($col_as) + ) + . '' + . ''; + } // end for + $GLOBALS['ods_buffer'] .= ''; + } // end if + + // Format the data + while ($row = $GLOBALS['dbi']->fetchRow($result)) { + $GLOBALS['ods_buffer'] .= ''; + for ($j = 0; $j < $fields_cnt; $j++) { + if ($fields_meta[$j]->type === 'geometry') { + // export GIS types as hex + $row[$j] = '0x' . bin2hex($row[$j]); + } + if (! isset($row[$j]) || $row[$j] === null) { + $GLOBALS['ods_buffer'] + .= '' + . '' + . htmlspecialchars($GLOBALS[$what . '_null']) + . '' + . ''; + } elseif (false !== stripos($field_flags[$j], 'BINARY') + && $fields_meta[$j]->blob + ) { + // ignore BLOB + $GLOBALS['ods_buffer'] + .= '' + . '' + . ''; + } elseif ($fields_meta[$j]->type == "date") { + $GLOBALS['ods_buffer'] + .= '' + . '' + . htmlspecialchars($row[$j]) + . '' + . ''; + } elseif ($fields_meta[$j]->type == "time") { + $GLOBALS['ods_buffer'] + .= '' + . '' + . htmlspecialchars($row[$j]) + . '' + . ''; + } elseif ($fields_meta[$j]->type == "datetime") { + $GLOBALS['ods_buffer'] + .= '' + . '' + . htmlspecialchars($row[$j]) + . '' + . ''; + } elseif (($fields_meta[$j]->numeric + && $fields_meta[$j]->type != 'timestamp' + && ! $fields_meta[$j]->blob) + || $fields_meta[$j]->type == 'real' + ) { + $GLOBALS['ods_buffer'] + .= '' + . '' + . htmlspecialchars($row[$j]) + . '' + . ''; + } else { + $GLOBALS['ods_buffer'] + .= '' + . '' + . htmlspecialchars($row[$j]) + . '' + . ''; + } + } // end for + $GLOBALS['ods_buffer'] .= ''; + } // end while + $GLOBALS['dbi']->freeResult($result); + + $GLOBALS['ods_buffer'] .= ''; + + return true; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportOdt.php b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportOdt.php new file mode 100644 index 0000000..8173afb --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportOdt.php @@ -0,0 +1,813 @@ +setProperties(); + } + + /** + * Sets the export ODT properties + * + * @return void + */ + protected function setProperties() + { + global $plugin_param; + $hide_structure = false; + if ($plugin_param['export_type'] == 'table' + && ! $plugin_param['single_table'] + ) { + $hide_structure = true; + } + + $exportPluginProperties = new ExportPluginProperties(); + $exportPluginProperties->setText('OpenDocument Text'); + $exportPluginProperties->setExtension('odt'); + $exportPluginProperties->setMimeType( + 'application/vnd.oasis.opendocument.text' + ); + $exportPluginProperties->setForceFile(true); + $exportPluginProperties->setOptionsText(__('Options')); + + // create the root group that will be the options field for + // $exportPluginProperties + // this will be shown as "Format specific options" + $exportSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // what to dump (structure/data/both) main group + $dumpWhat = new OptionsPropertyMainGroup( + "general_opts", + __('Dump table') + ); + // create primary items and add them to the group + $leaf = new RadioPropertyItem("structure_or_data"); + $leaf->setValues( + [ + 'structure' => __('structure'), + 'data' => __('data'), + 'structure_and_data' => __('structure and data'), + ] + ); + $dumpWhat->addProperty($leaf); + // add the main group to the root group + $exportSpecificOptions->addProperty($dumpWhat); + + // structure options main group + if (! $hide_structure) { + $structureOptions = new OptionsPropertyMainGroup( + "structure", + __('Object creation options') + ); + $structureOptions->setForce('data'); + // create primary items and add them to the group + if (! empty($GLOBALS['cfgRelation']['relation'])) { + $leaf = new BoolPropertyItem( + "relation", + __('Display foreign key relationships') + ); + $structureOptions->addProperty($leaf); + } + $leaf = new BoolPropertyItem( + "comments", + __('Display comments') + ); + $structureOptions->addProperty($leaf); + if (! empty($GLOBALS['cfgRelation']['mimework'])) { + $leaf = new BoolPropertyItem( + "mime", + __('Display media (MIME) types') + ); + $structureOptions->addProperty($leaf); + } + // add the main group to the root group + $exportSpecificOptions->addProperty($structureOptions); + } + + // data options main group + $dataOptions = new OptionsPropertyMainGroup( + "data", + __('Data dump options') + ); + $dataOptions->setForce('structure'); + // create primary items and add them to the group + $leaf = new BoolPropertyItem( + "columns", + __('Put columns names in the first row') + ); + $dataOptions->addProperty($leaf); + $leaf = new TextPropertyItem( + 'null', + __('Replace NULL with:') + ); + $dataOptions->addProperty($leaf); + // add the main group to the root group + $exportSpecificOptions->addProperty($dataOptions); + + // set the options for the export plugin property item + $exportPluginProperties->setOptions($exportSpecificOptions); + $this->properties = $exportPluginProperties; + } + + /** + * Outputs export header + * + * @return bool Whether it succeeded + */ + public function exportHeader() + { + $GLOBALS['odt_buffer'] .= '' + . '' + . '' + . ''; + + return true; + } + + /** + * Outputs export footer + * + * @return bool Whether it succeeded + */ + public function exportFooter() + { + $GLOBALS['odt_buffer'] .= '' + . '' + . ''; + if (! $this->export->outputHandler( + OpenDocument::create( + 'application/vnd.oasis.opendocument.text', + $GLOBALS['odt_buffer'] + ) + ) + ) { + return false; + } + + return true; + } + + /** + * Outputs database header + * + * @param string $db Database name + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBHeader($db, $db_alias = '') + { + if (empty($db_alias)) { + $db_alias = $db; + } + $GLOBALS['odt_buffer'] + .= '' + . __('Database') . ' ' . htmlspecialchars($db_alias) + . ''; + + return true; + } + + /** + * Outputs database footer + * + * @param string $db Database name + * + * @return bool Whether it succeeded + */ + public function exportDBFooter($db) + { + return true; + } + + /** + * Outputs CREATE DATABASE statement + * + * @param string $db Database name + * @param string $export_type 'server', 'database', 'table' + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBCreate($db, $export_type, $db_alias = '') + { + return true; + } + + /** + * Outputs the content of a table in NHibernate format + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $sql_query SQL query for obtaining data + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportData( + $db, + $table, + $crlf, + $error_url, + $sql_query, + array $aliases = [] + ) { + global $what; + + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + // Gets the data from the database + $result = $GLOBALS['dbi']->query( + $sql_query, + DatabaseInterface::CONNECT_USER, + DatabaseInterface::QUERY_UNBUFFERED + ); + $fields_cnt = $GLOBALS['dbi']->numFields($result); + $fields_meta = $GLOBALS['dbi']->getFieldsMeta($result); + $field_flags = []; + for ($j = 0; $j < $fields_cnt; $j++) { + $field_flags[$j] = $GLOBALS['dbi']->fieldFlags($result, $j); + } + + $GLOBALS['odt_buffer'] + .= '' + . __('Dumping data for table') . ' ' . htmlspecialchars($table_alias) + . '' + . '' + . ''; + + // If required, get fields name at the first line + if (isset($GLOBALS[$what . '_columns'])) { + $GLOBALS['odt_buffer'] .= ''; + for ($i = 0; $i < $fields_cnt; $i++) { + $col_as = $GLOBALS['dbi']->fieldName($result, $i); + if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) { + $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as]; + } + $GLOBALS['odt_buffer'] + .= '' + . '' + . htmlspecialchars( + stripslashes($col_as) + ) + . '' + . ''; + } // end for + $GLOBALS['odt_buffer'] .= ''; + } // end if + + // Format the data + while ($row = $GLOBALS['dbi']->fetchRow($result)) { + $GLOBALS['odt_buffer'] .= ''; + for ($j = 0; $j < $fields_cnt; $j++) { + if ($fields_meta[$j]->type === 'geometry') { + // export GIS types as hex + $row[$j] = '0x' . bin2hex($row[$j]); + } + if (! isset($row[$j]) || $row[$j] === null) { + $GLOBALS['odt_buffer'] + .= '' + . '' + . htmlspecialchars($GLOBALS[$what . '_null']) + . '' + . ''; + } elseif (false !== stripos($field_flags[$j], 'BINARY') + && $fields_meta[$j]->blob + ) { + // ignore BLOB + $GLOBALS['odt_buffer'] + .= '' + . '' + . ''; + } elseif ($fields_meta[$j]->numeric + && $fields_meta[$j]->type != 'timestamp' + && ! $fields_meta[$j]->blob + ) { + $GLOBALS['odt_buffer'] + .= '' + . '' + . htmlspecialchars($row[$j]) + . '' + . ''; + } else { + $GLOBALS['odt_buffer'] + .= '' + . '' + . htmlspecialchars($row[$j]) + . '' + . ''; + } + } // end for + $GLOBALS['odt_buffer'] .= ''; + } // end while + $GLOBALS['dbi']->freeResult($result); + + $GLOBALS['odt_buffer'] .= ''; + + return true; + } + + /** + * Returns a stand-in CREATE definition to resolve view dependencies + * + * @param string $db the database name + * @param string $view the view name + * @param string $crlf the end of line sequence + * @param array $aliases Aliases of db/table/columns + * + * @return string resulting definition + */ + public function getTableDefStandIn($db, $view, $crlf, $aliases = []) + { + $db_alias = $db; + $view_alias = $view; + $this->initAlias($aliases, $db_alias, $view_alias); + /** + * Gets fields properties + */ + $GLOBALS['dbi']->selectDb($db); + + /** + * Displays the table structure + */ + $GLOBALS['odt_buffer'] + .= ''; + $columns_cnt = 4; + $GLOBALS['odt_buffer'] + .= ''; + /* Header */ + $GLOBALS['odt_buffer'] .= '' + . '' + . '' . __('Column') . '' + . '' + . '' + . '' . __('Type') . '' + . '' + . '' + . '' . __('Null') . '' + . '' + . '' + . '' . __('Default') . '' + . '' + . ''; + + $columns = $GLOBALS['dbi']->getColumns($db, $view); + foreach ($columns as $column) { + $col_as = $column['Field'] ?? null; + if (! empty($aliases[$db]['tables'][$view]['columns'][$col_as])) { + $col_as = $aliases[$db]['tables'][$view]['columns'][$col_as]; + } + $GLOBALS['odt_buffer'] .= $this->formatOneColumnDefinition( + $column, + $col_as + ); + $GLOBALS['odt_buffer'] .= ''; + } // end foreach + + $GLOBALS['odt_buffer'] .= ''; + + return ''; + } + + /** + * Returns $table's CREATE definition + * + * @param string $db the database name + * @param string $table the table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param bool $do_relation whether to include relation comments + * @param bool $do_comments whether to include the pmadb-style column + * comments as comments in the structure; + * this is deprecated but the parameter is + * left here because export.php calls + * PMA_exportStructure() also for other + * @param bool $do_mime whether to include mime comments + * @param bool $show_dates whether to include creation/update/check dates + * @param bool $add_semicolon whether to add semicolon and end-of-line at + * the end + * @param bool $view whether we're handling a view + * @param array $aliases Aliases of db/table/columns + * + * @return bool true + */ + public function getTableDef( + $db, + $table, + $crlf, + $error_url, + $do_relation, + $do_comments, + $do_mime, + $show_dates = false, + $add_semicolon = true, + $view = false, + array $aliases = [] + ) { + global $cfgRelation; + + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + /** + * Gets fields properties + */ + $GLOBALS['dbi']->selectDb($db); + + // Check if we can use Relations + list($res_rel, $have_rel) = $this->relation->getRelationsAndStatus( + $do_relation && ! empty($cfgRelation['relation']), + $db, + $table + ); + /** + * Displays the table structure + */ + $GLOBALS['odt_buffer'] .= ''; + $columns_cnt = 4; + if ($do_relation && $have_rel) { + $columns_cnt++; + } + if ($do_comments) { + $columns_cnt++; + } + if ($do_mime && $cfgRelation['mimework']) { + $columns_cnt++; + } + $GLOBALS['odt_buffer'] .= ''; + /* Header */ + $GLOBALS['odt_buffer'] .= '' + . '' + . '' . __('Column') . '' + . '' + . '' + . '' . __('Type') . '' + . '' + . '' + . '' . __('Null') . '' + . '' + . '' + . '' . __('Default') . '' + . ''; + if ($do_relation && $have_rel) { + $GLOBALS['odt_buffer'] .= '' + . '' . __('Links to') . '' + . ''; + } + if ($do_comments) { + $GLOBALS['odt_buffer'] .= '' + . '' . __('Comments') . '' + . ''; + $comments = $this->relation->getComments($db, $table); + } + if ($do_mime && $cfgRelation['mimework']) { + $GLOBALS['odt_buffer'] .= '' + . '' . __('Media (MIME) type') . '' + . ''; + $mime_map = $this->transformations->getMime($db, $table, true); + } + $GLOBALS['odt_buffer'] .= ''; + + $columns = $GLOBALS['dbi']->getColumns($db, $table); + foreach ($columns as $column) { + $col_as = $field_name = $column['Field']; + if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) { + $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as]; + } + $GLOBALS['odt_buffer'] .= $this->formatOneColumnDefinition( + $column, + $col_as + ); + if ($do_relation && $have_rel) { + $foreigner = $this->relation->searchColumnInForeigners($res_rel, $field_name); + if ($foreigner) { + $rtable = $foreigner['foreign_table']; + $rfield = $foreigner['foreign_field']; + if (! empty($aliases[$db]['tables'][$rtable]['columns'][$rfield]) + ) { + $rfield + = $aliases[$db]['tables'][$rtable]['columns'][$rfield]; + } + if (! empty($aliases[$db]['tables'][$rtable]['alias'])) { + $rtable = $aliases[$db]['tables'][$rtable]['alias']; + } + $relation = htmlspecialchars($rtable . ' (' . $rfield . ')'); + $GLOBALS['odt_buffer'] + .= '' + . '' + . htmlspecialchars($relation) + . '' + . ''; + } + } + if ($do_comments) { + if (isset($comments[$field_name])) { + $GLOBALS['odt_buffer'] + .= '' + . '' + . htmlspecialchars($comments[$field_name]) + . '' + . ''; + } else { + $GLOBALS['odt_buffer'] + .= '' + . '' + . ''; + } + } + if ($do_mime && $cfgRelation['mimework']) { + if (isset($mime_map[$field_name])) { + $GLOBALS['odt_buffer'] + .= '' + . '' + . htmlspecialchars( + str_replace('_', '/', $mime_map[$field_name]['mimetype']) + ) + . '' + . ''; + } else { + $GLOBALS['odt_buffer'] + .= '' + . '' + . ''; + } + } + $GLOBALS['odt_buffer'] .= ''; + } // end foreach + + $GLOBALS['odt_buffer'] .= ''; + + return true; + } // end of the '$this->getTableDef()' function + + /** + * Outputs triggers + * + * @param string $db database name + * @param string $table table name + * @param array $aliases Aliases of db/table/columns + * + * @return bool true + */ + protected function getTriggers($db, $table, array $aliases = []) + { + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + $GLOBALS['odt_buffer'] .= '' + . '' + . '' + . '' + . '' . __('Name') . '' + . '' + . '' + . '' . __('Time') . '' + . '' + . '' + . '' . __('Event') . '' + . '' + . '' + . '' . __('Definition') . '' + . '' + . ''; + + $triggers = $GLOBALS['dbi']->getTriggers($db, $table); + + foreach ($triggers as $trigger) { + $GLOBALS['odt_buffer'] .= ''; + $GLOBALS['odt_buffer'] .= '' + . '' + . htmlspecialchars($trigger['name']) + . '' + . ''; + $GLOBALS['odt_buffer'] .= '' + . '' + . htmlspecialchars($trigger['action_timing']) + . '' + . ''; + $GLOBALS['odt_buffer'] .= '' + . '' + . htmlspecialchars($trigger['event_manipulation']) + . '' + . ''; + $GLOBALS['odt_buffer'] .= '' + . '' + . htmlspecialchars($trigger['definition']) + . '' + . ''; + $GLOBALS['odt_buffer'] .= ''; + } + + $GLOBALS['odt_buffer'] .= ''; + + return true; + } + + /** + * Outputs table's structure + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $export_mode 'create_table', 'triggers', 'create_view', + * 'stand_in' + * @param string $export_type 'server', 'database', 'table' + * @param bool $do_relation whether to include relation comments + * @param bool $do_comments whether to include the pmadb-style column + * comments as comments in the structure; + * this is deprecated but the parameter is + * left here because export.php calls + * PMA_exportStructure() also for other + * @param bool $do_mime whether to include mime comments + * @param bool $dates whether to include creation/update/check dates + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportStructure( + $db, + $table, + $crlf, + $error_url, + $export_mode, + $export_type, + $do_relation = false, + $do_comments = false, + $do_mime = false, + $dates = false, + array $aliases = [] + ) { + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + switch ($export_mode) { + case 'create_table': + $GLOBALS['odt_buffer'] + .= '' + . __('Table structure for table') . ' ' . + htmlspecialchars($table_alias) + . ''; + $this->getTableDef( + $db, + $table, + $crlf, + $error_url, + $do_relation, + $do_comments, + $do_mime, + $dates, + true, + false, + $aliases + ); + break; + case 'triggers': + $triggers = $GLOBALS['dbi']->getTriggers($db, $table, $aliases); + if ($triggers) { + $GLOBALS['odt_buffer'] + .= '' + . __('Triggers') . ' ' + . htmlspecialchars($table_alias) + . ''; + $this->getTriggers($db, $table); + } + break; + case 'create_view': + $GLOBALS['odt_buffer'] + .= '' + . __('Structure for view') . ' ' + . htmlspecialchars($table_alias) + . ''; + $this->getTableDef( + $db, + $table, + $crlf, + $error_url, + $do_relation, + $do_comments, + $do_mime, + $dates, + true, + true, + $aliases + ); + break; + case 'stand_in': + $GLOBALS['odt_buffer'] + .= '' + . __('Stand-in structure for view') . ' ' + . htmlspecialchars($table_alias) + . ''; + // export a stand-in definition to resolve view dependencies + $this->getTableDefStandIn($db, $table, $crlf, $aliases); + } // end switch + + return true; + } // end of the '$this->exportStructure' function + + /** + * Formats the definition for one column + * + * @param array $column info about this column + * @param string $col_as column alias + * + * @return string Formatted column definition + */ + protected function formatOneColumnDefinition($column, $col_as = '') + { + if (empty($col_as)) { + $col_as = $column['Field']; + } + $definition = ''; + $definition .= '' + . '' . htmlspecialchars($col_as) . '' + . ''; + + $extracted_columnspec + = Util::extractColumnSpec($column['Type']); + $type = htmlspecialchars($extracted_columnspec['print_type']); + if (empty($type)) { + $type = ' '; + } + + $definition .= '' + . '' . htmlspecialchars($type) . '' + . ''; + if (! isset($column['Default'])) { + if ($column['Null'] != 'NO') { + $column['Default'] = 'NULL'; + } else { + $column['Default'] = ''; + } + } + $definition .= '' + . '' + . (($column['Null'] == '' || $column['Null'] == 'NO') + ? __('No') + : __('Yes')) + . '' + . ''; + $definition .= '' + . '' . htmlspecialchars($column['Default']) . '' + . ''; + + return $definition; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportPdf.php b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportPdf.php new file mode 100644 index 0000000..49e74b3 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportPdf.php @@ -0,0 +1,395 @@ +initSpecificVariables(); + + $this->setProperties(); + } + + /** + * Initialize the local variables that are used for export PDF + * + * @return void + */ + protected function initSpecificVariables() + { + if (! empty($_POST['pdf_report_title'])) { + $this->_setPdfReportTitle($_POST['pdf_report_title']); + } + $this->_setPdf(new Pdf('L', 'pt', 'A3')); + } + + /** + * Sets the export PDF properties + * + * @return void + */ + protected function setProperties() + { + $exportPluginProperties = new ExportPluginProperties(); + $exportPluginProperties->setText('PDF'); + $exportPluginProperties->setExtension('pdf'); + $exportPluginProperties->setMimeType('application/pdf'); + $exportPluginProperties->setForceFile(true); + $exportPluginProperties->setOptionsText(__('Options')); + + // create the root group that will be the options field for + // $exportPluginProperties + // this will be shown as "Format specific options" + $exportSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // general options main group + $generalOptions = new OptionsPropertyMainGroup("general_opts"); + // create primary items and add them to the group + $leaf = new TextPropertyItem( + "report_title", + __('Report title:') + ); + $generalOptions->addProperty($leaf); + // add the group to the root group + $exportSpecificOptions->addProperty($generalOptions); + + // what to dump (structure/data/both) main group + $dumpWhat = new OptionsPropertyMainGroup( + "dump_what", + __('Dump table') + ); + $leaf = new RadioPropertyItem("structure_or_data"); + $leaf->setValues( + [ + 'structure' => __('structure'), + 'data' => __('data'), + 'structure_and_data' => __('structure and data'), + ] + ); + $dumpWhat->addProperty($leaf); + // add the group to the root group + $exportSpecificOptions->addProperty($dumpWhat); + + // set the options for the export plugin property item + $exportPluginProperties->setOptions($exportSpecificOptions); + $this->properties = $exportPluginProperties; + } + + /** + * Outputs export header + * + * @return bool Whether it succeeded + */ + public function exportHeader() + { + $pdf_report_title = $this->_getPdfReportTitle(); + $pdf = $this->_getPdf(); + $pdf->Open(); + + $attr = [ + 'titleFontSize' => 18, + 'titleText' => $pdf_report_title, + ]; + $pdf->setAttributes($attr); + $pdf->setTopMargin(30); + + return true; + } + + /** + * Outputs export footer + * + * @return bool Whether it succeeded + */ + public function exportFooter() + { + $pdf = $this->_getPdf(); + + // instead of $pdf->Output(): + return $this->export->outputHandler($pdf->getPDFData()); + } + + /** + * Outputs database header + * + * @param string $db Database name + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBHeader($db, $db_alias = '') + { + return true; + } + + /** + * Outputs database footer + * + * @param string $db Database name + * + * @return bool Whether it succeeded + */ + public function exportDBFooter($db) + { + return true; + } + + /** + * Outputs CREATE DATABASE statement + * + * @param string $db Database name + * @param string $export_type 'server', 'database', 'table' + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBCreate($db, $export_type, $db_alias = '') + { + return true; + } + + /** + * Outputs the content of a table in NHibernate format + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $sql_query SQL query for obtaining data + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportData( + $db, + $table, + $crlf, + $error_url, + $sql_query, + array $aliases = [] + ) { + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + $pdf = $this->_getPdf(); + $attr = [ + 'currentDb' => $db, + 'currentTable' => $table, + 'dbAlias' => $db_alias, + 'tableAlias' => $table_alias, + 'aliases' => $aliases, + 'purpose' => __('Dumping data'), + ]; + $pdf->setAttributes($attr); + $pdf->mysqlReport($sql_query); + + return true; + } // end of the 'PMA_exportData()' function + + /** + * Outputs table structure + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $export_mode 'create_table', 'triggers', 'create_view', + * 'stand_in' + * @param string $export_type 'server', 'database', 'table' + * @param bool $do_relation whether to include relation comments + * @param bool $do_comments whether to include the pmadb-style column + * comments as comments in the structure; + * this is deprecated but the parameter is + * left here because export.php calls + * PMA_exportStructure() also for other + * export types which use this parameter + * @param bool $do_mime whether to include mime comments + * @param bool $dates whether to include creation/update/check dates + * @param array $aliases aliases for db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportStructure( + $db, + $table, + $crlf, + $error_url, + $export_mode, + $export_type, + $do_relation = false, + $do_comments = false, + $do_mime = false, + $dates = false, + array $aliases = [] + ) { + $db_alias = $db; + $table_alias = $table; + $purpose = null; + $this->initAlias($aliases, $db_alias, $table_alias); + $pdf = $this->_getPdf(); + // getting purpose to show at top + switch ($export_mode) { + case 'create_table': + $purpose = __('Table structure'); + break; + case 'triggers': + $purpose = __('Triggers'); + break; + case 'create_view': + $purpose = __('View structure'); + break; + case 'stand_in': + $purpose = __('Stand in'); + } // end switch + + $attr = [ + 'currentDb' => $db, + 'currentTable' => $table, + 'dbAlias' => $db_alias, + 'tableAlias' => $table_alias, + 'aliases' => $aliases, + 'purpose' => $purpose, + ]; + $pdf->setAttributes($attr); + /** + * comment display set true as presently in pdf + * format, no option is present to take user input. + */ + $do_comments = true; + switch ($export_mode) { + case 'create_table': + $pdf->getTableDef( + $db, + $table, + $do_relation, + $do_comments, + $do_mime, + false, + $aliases + ); + break; + case 'triggers': + $pdf->getTriggers($db, $table); + break; + case 'create_view': + $pdf->getTableDef( + $db, + $table, + $do_relation, + $do_comments, + $do_mime, + false, + $aliases + ); + break; + case 'stand_in': + /* export a stand-in definition to resolve view dependencies + * Yet to develop this function + * $pdf->getTableDefStandIn($db, $table, $crlf); + */ + } // end switch + + return true; + } + + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the PhpMyAdmin\Plugins\Export\Helpers\Pdf instance + * + * @return Pdf + */ + private function _getPdf() + { + return $this->_pdf; + } + + /** + * Instantiates the PhpMyAdmin\Plugins\Export\Helpers\Pdf class + * + * @param Pdf $pdf The instance + * + * @return void + */ + private function _setPdf($pdf) + { + $this->_pdf = $pdf; + } + + /** + * Gets the PDF report title + * + * @return string + */ + private function _getPdfReportTitle() + { + return $this->_pdfReportTitle; + } + + /** + * Sets the PDF report title + * + * @param string $pdfReportTitle PDF report title + * + * @return void + */ + private function _setPdfReportTitle($pdfReportTitle) + { + $this->_pdfReportTitle = $pdfReportTitle; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportPhparray.php b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportPhparray.php new file mode 100644 index 0000000..3fe9bd1 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportPhparray.php @@ -0,0 +1,259 @@ +setProperties(); + } + + /** + * Sets the export PHP Array properties + * + * @return void + */ + protected function setProperties() + { + $exportPluginProperties = new ExportPluginProperties(); + $exportPluginProperties->setText('PHP array'); + $exportPluginProperties->setExtension('php'); + $exportPluginProperties->setMimeType('text/plain'); + $exportPluginProperties->setOptionsText(__('Options')); + + // create the root group that will be the options field for + // $exportPluginProperties + // this will be shown as "Format specific options" + $exportSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // general options main group + $generalOptions = new OptionsPropertyMainGroup("general_opts"); + // create primary items and add them to the group + $leaf = new HiddenPropertyItem("structure_or_data"); + $generalOptions->addProperty($leaf); + // add the main group to the root group + $exportSpecificOptions->addProperty($generalOptions); + + // set the options for the export plugin property item + $exportPluginProperties->setOptions($exportSpecificOptions); + $this->properties = $exportPluginProperties; + } + + /** + * Removes end of comment from a string + * + * @param string $string String to replace + * + * @return string + */ + public function commentString($string) + { + return strtr($string, '*/', '-'); + } + + + /** + * Outputs export header + * + * @return bool Whether it succeeded + */ + public function exportHeader() + { + $this->export->outputHandler( + 'export->outputHandler( + '/**' . $GLOBALS['crlf'] + . ' * Database ' . $this->commentString(Util::backquote($db_alias)) + . $GLOBALS['crlf'] . ' */' . $GLOBALS['crlf'] + ); + + return true; + } + + /** + * Outputs database footer + * + * @param string $db Database name + * + * @return bool Whether it succeeded + */ + public function exportDBFooter($db) + { + return true; + } + + /** + * Outputs CREATE DATABASE statement + * + * @param string $db Database name + * @param string $export_type 'server', 'database', 'table' + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBCreate($db, $export_type, $db_alias = '') + { + return true; + } + + /** + * Outputs the content of a table in PHP array format + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $sql_query SQL query for obtaining data + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportData( + $db, + $table, + $crlf, + $error_url, + $sql_query, + array $aliases = [] + ) { + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + + $result = $GLOBALS['dbi']->query( + $sql_query, + DatabaseInterface::CONNECT_USER, + DatabaseInterface::QUERY_UNBUFFERED + ); + + $columns_cnt = $GLOBALS['dbi']->numFields($result); + $columns = []; + for ($i = 0; $i < $columns_cnt; $i++) { + $col_as = $GLOBALS['dbi']->fieldName($result, $i); + if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) { + $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as]; + } + $columns[$i] = stripslashes($col_as); + } + + // fix variable names (based on + // https://www.php.net/manual/en/language.variables.basics.php) + if (! preg_match( + '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', + $table_alias + ) + ) { + // fix invalid characters in variable names by replacing them with + // underscores + $tablefixed = preg_replace( + '/[^a-zA-Z0-9_\x7f-\xff]/', + '_', + $table_alias + ); + + // variable name must not start with a number or dash... + if (preg_match('/^[a-zA-Z_\x7f-\xff]/', $tablefixed) === 0) { + $tablefixed = '_' . $tablefixed; + } + } else { + $tablefixed = $table; + } + + $buffer = ''; + $record_cnt = 0; + // Output table name as comment + $buffer .= $crlf . '/* ' + . $this->commentString(Util::backquote($db_alias)) . '.' + . $this->commentString(Util::backquote($table_alias)) . ' */' . $crlf; + $buffer .= '$' . $tablefixed . ' = array('; + + while ($record = $GLOBALS['dbi']->fetchRow($result)) { + $record_cnt++; + + if ($record_cnt == 1) { + $buffer .= $crlf . ' array('; + } else { + $buffer .= ',' . $crlf . ' array('; + } + + for ($i = 0; $i < $columns_cnt; $i++) { + $buffer .= var_export($columns[$i], true) + . " => " . var_export($record[$i], true) + . (($i + 1 >= $columns_cnt) ? '' : ','); + } + + $buffer .= ')'; + } + + $buffer .= $crlf . ');' . $crlf; + if (! $this->export->outputHandler($buffer)) { + return false; + } + + $GLOBALS['dbi']->freeResult($result); + + return true; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportSql.php b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportSql.php new file mode 100644 index 0000000..9c5b1a2 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportSql.php @@ -0,0 +1,2915 @@ +setProperties(); + + // Avoids undefined variables, use NULL so isset() returns false + if (! isset($GLOBALS['sql_backquotes'])) { + $GLOBALS['sql_backquotes'] = null; + } + } + + /** + * Sets the export SQL properties + * + * @return void + */ + protected function setProperties() + { + global $plugin_param; + + $hide_sql = false; + $hide_structure = false; + if ($plugin_param['export_type'] == 'table' + && ! $plugin_param['single_table'] + ) { + $hide_structure = true; + $hide_sql = true; + } + + if (! $hide_sql) { + $exportPluginProperties = new ExportPluginProperties(); + $exportPluginProperties->setText('SQL'); + $exportPluginProperties->setExtension('sql'); + $exportPluginProperties->setMimeType('text/x-sql'); + $exportPluginProperties->setOptionsText(__('Options')); + + // create the root group that will be the options field for + // $exportPluginProperties + // this will be shown as "Format specific options" + $exportSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // general options main group + $generalOptions = new OptionsPropertyMainGroup("general_opts"); + + // comments + $subgroup = new OptionsPropertySubgroup("include_comments"); + $leaf = new BoolPropertyItem( + 'include_comments', + __( + 'Display comments (includes info such as export' + . ' timestamp, PHP version, and server version)' + ) + ); + $subgroup->setSubgroupHeader($leaf); + + $leaf = new TextPropertyItem( + 'header_comment', + __('Additional custom header comment (\n splits lines):') + ); + $subgroup->addProperty($leaf); + $leaf = new BoolPropertyItem( + 'dates', + __( + 'Include a timestamp of when databases were created, last' + . ' updated, and last checked' + ) + ); + $subgroup->addProperty($leaf); + if (! empty($GLOBALS['cfgRelation']['relation'])) { + $leaf = new BoolPropertyItem( + 'relation', + __('Display foreign key relationships') + ); + $subgroup->addProperty($leaf); + } + if (! empty($GLOBALS['cfgRelation']['mimework'])) { + $leaf = new BoolPropertyItem( + 'mime', + __('Display media (MIME) types') + ); + $subgroup->addProperty($leaf); + } + $generalOptions->addProperty($subgroup); + + // enclose in a transaction + $leaf = new BoolPropertyItem( + "use_transaction", + __('Enclose export in a transaction') + ); + $leaf->setDoc( + [ + 'programs', + 'mysqldump', + 'option_mysqldump_single-transaction', + ] + ); + $generalOptions->addProperty($leaf); + + // disable foreign key checks + $leaf = new BoolPropertyItem( + "disable_fk", + __('Disable foreign key checks') + ); + $leaf->setDoc( + [ + 'manual_MySQL_Database_Administration', + 'server-system-variables', + 'sysvar_foreign_key_checks', + ] + ); + $generalOptions->addProperty($leaf); + + // export views as tables + $leaf = new BoolPropertyItem( + "views_as_tables", + __('Export views as tables') + ); + $generalOptions->addProperty($leaf); + + // export metadata + $leaf = new BoolPropertyItem( + "metadata", + __('Export metadata') + ); + $generalOptions->addProperty($leaf); + + // compatibility maximization + $compats = $GLOBALS['dbi']->getCompatibilities(); + if (count($compats) > 0) { + $values = []; + foreach ($compats as $val) { + $values[$val] = $val; + } + + $leaf = new SelectPropertyItem( + "compatibility", + __( + 'Database system or older MySQL server to maximize output' + . ' compatibility with:' + ) + ); + $leaf->setValues($values); + $leaf->setDoc( + [ + 'manual_MySQL_Database_Administration', + 'Server_SQL_mode', + ] + ); + $generalOptions->addProperty($leaf); + + unset($values); + } + + // what to dump (structure/data/both) + $subgroup = new OptionsPropertySubgroup( + "dump_table", + __("Dump table") + ); + $leaf = new RadioPropertyItem('structure_or_data'); + $leaf->setValues( + [ + 'structure' => __('structure'), + 'data' => __('data'), + 'structure_and_data' => __('structure and data'), + ] + ); + $subgroup->setSubgroupHeader($leaf); + $generalOptions->addProperty($subgroup); + + // add the main group to the root group + $exportSpecificOptions->addProperty($generalOptions); + + // structure options main group + if (! $hide_structure) { + $structureOptions = new OptionsPropertyMainGroup( + "structure", + __('Object creation options') + ); + $structureOptions->setForce('data'); + + // begin SQL Statements + $subgroup = new OptionsPropertySubgroup(); + $leaf = new MessageOnlyPropertyItem( + 'add_statements', + __('Add statements:') + ); + $subgroup->setSubgroupHeader($leaf); + + // server export options + if ($plugin_param['export_type'] == 'server') { + $leaf = new BoolPropertyItem( + "drop_database", + sprintf(__('Add %s statement'), 'DROP DATABASE IF EXISTS') + ); + $subgroup->addProperty($leaf); + } + + if ($plugin_param['export_type'] == 'database') { + $create_clause = 'CREATE DATABASE / USE'; + $leaf = new BoolPropertyItem( + 'create_database', + sprintf(__('Add %s statement'), $create_clause) + ); + $subgroup->addProperty($leaf); + } + + if ($plugin_param['export_type'] == 'table') { + $drop_clause = $GLOBALS['dbi']->getTable( + $GLOBALS['db'], + $GLOBALS['table'] + )->isView() + ? 'DROP VIEW' + : 'DROP TABLE'; + } else { + $drop_clause = 'DROP TABLE / VIEW / PROCEDURE' + . ' / FUNCTION / EVENT'; + } + + $drop_clause .= ' / TRIGGER'; + + $leaf = new BoolPropertyItem( + 'drop_table', + sprintf(__('Add %s statement'), $drop_clause) + ); + $subgroup->addProperty($leaf); + + $subgroup_create_table = new OptionsPropertySubgroup(); + + // Add table structure option + $leaf = new BoolPropertyItem( + 'create_table', + sprintf(__('Add %s statement'), 'CREATE TABLE') + ); + $subgroup_create_table->setSubgroupHeader($leaf); + + $leaf = new BoolPropertyItem( + 'if_not_exists', + 'IF NOT EXISTS ' . __( + '(less efficient as indexes will be generated during table ' + . 'creation)' + ) + ); + $subgroup_create_table->addProperty($leaf); + + $leaf = new BoolPropertyItem( + 'auto_increment', + sprintf(__('%s value'), 'AUTO_INCREMENT') + ); + $subgroup_create_table->addProperty($leaf); + + $subgroup->addProperty($subgroup_create_table); + + // Add view option + $subgroup_create_view = new OptionsPropertySubgroup(); + $leaf = new BoolPropertyItem( + 'create_view', + sprintf(__('Add %s statement'), 'CREATE VIEW') + ); + $subgroup_create_view->setSubgroupHeader($leaf); + + $leaf = new BoolPropertyItem( + 'view_current_user', + __('Exclude definition of current user') + ); + $subgroup_create_view->addProperty($leaf); + + $leaf = new BoolPropertyItem( + 'or_replace_view', + sprintf(__('%s view'), 'OR REPLACE') + ); + $subgroup_create_view->addProperty($leaf); + + $subgroup->addProperty($subgroup_create_view); + + $leaf = new BoolPropertyItem( + 'procedure_function', + sprintf( + __('Add %s statement'), + 'CREATE PROCEDURE / FUNCTION / EVENT' + ) + ); + $subgroup->addProperty($leaf); + + // Add triggers option + $leaf = new BoolPropertyItem( + 'create_trigger', + sprintf(__('Add %s statement'), 'CREATE TRIGGER') + ); + $subgroup->addProperty($leaf); + + $structureOptions->addProperty($subgroup); + + $leaf = new BoolPropertyItem( + "backquotes", + __( + 'Enclose table and column names with backquotes ' + . '(Protects column and table names formed with' + . ' special characters or keywords)' + ) + ); + + $structureOptions->addProperty($leaf); + + // add the main group to the root group + $exportSpecificOptions->addProperty($structureOptions); + } + + // begin Data options + $dataOptions = new OptionsPropertyMainGroup( + "data", + __('Data creation options') + ); + $dataOptions->setForce('structure'); + $leaf = new BoolPropertyItem( + "truncate", + __('Truncate table before insert') + ); + $dataOptions->addProperty($leaf); + + // begin SQL Statements + $subgroup = new OptionsPropertySubgroup(); + $leaf = new MessageOnlyPropertyItem( + __('Instead of INSERT statements, use:') + ); + $subgroup->setSubgroupHeader($leaf); + + $leaf = new BoolPropertyItem( + "delayed", + __('INSERT DELAYED statements') + ); + $leaf->setDoc( + [ + 'manual_MySQL_Database_Administration', + 'insert_delayed', + ] + ); + $subgroup->addProperty($leaf); + + $leaf = new BoolPropertyItem( + "ignore", + __('INSERT IGNORE statements') + ); + $leaf->setDoc( + [ + 'manual_MySQL_Database_Administration', + 'insert', + ] + ); + $subgroup->addProperty($leaf); + $dataOptions->addProperty($subgroup); + + // Function to use when dumping dat + $leaf = new SelectPropertyItem( + "type", + __('Function to use when dumping data:') + ); + $leaf->setValues( + [ + 'INSERT' => 'INSERT', + 'UPDATE' => 'UPDATE', + 'REPLACE' => 'REPLACE', + ] + ); + $dataOptions->addProperty($leaf); + + /* Syntax to use when inserting data */ + $subgroup = new OptionsPropertySubgroup(); + $leaf = new MessageOnlyPropertyItem( + null, + __('Syntax to use when inserting data:') + ); + $subgroup->setSubgroupHeader($leaf); + $leaf = new RadioPropertyItem( + "insert_syntax", + __('INSERT IGNORE statements') + ); + $leaf->setValues( + [ + 'complete' => __( + 'include column names in every INSERT statement' + . '
      Example: INSERT INTO' + . ' tbl_name (col_A,col_B,col_C) VALUES (1,2,3)' + ), + 'extended' => __( + 'insert multiple rows in every INSERT statement' + . '
      Example: INSERT INTO' + . ' tbl_name VALUES (1,2,3), (4,5,6), (7,8,9)' + ), + 'both' => __( + 'both of the above
      Example:' + . ' INSERT INTO tbl_name (col_A,col_B,col_C) VALUES' + . ' (1,2,3), (4,5,6), (7,8,9)' + ), + 'none' => __( + 'neither of the above
      Example:' + . ' INSERT INTO tbl_name VALUES (1,2,3)' + ), + ] + ); + $subgroup->addProperty($leaf); + $dataOptions->addProperty($subgroup); + + // Max length of query + $leaf = new NumberPropertyItem( + "max_query_size", + __('Maximal length of created query') + ); + $dataOptions->addProperty($leaf); + + // Dump binary columns in hexadecimal + $leaf = new BoolPropertyItem( + "hex_for_binary", + __( + 'Dump binary columns in hexadecimal notation' + . ' (for example, "abc" becomes 0x616263)' + ) + ); + $dataOptions->addProperty($leaf); + + // Dump time in UTC + $leaf = new BoolPropertyItem( + "utc_time", + __( + 'Dump TIMESTAMP columns in UTC (enables TIMESTAMP columns' + . ' to be dumped and reloaded between servers in different' + . ' time zones)' + ) + ); + $dataOptions->addProperty($leaf); + + // add the main group to the root group + $exportSpecificOptions->addProperty($dataOptions); + + // set the options for the export plugin property item + $exportPluginProperties->setOptions($exportSpecificOptions); + $this->properties = $exportPluginProperties; + } + } + + /** + * Generates SQL for routines export + * + * @param string $db Database + * @param array $aliases Aliases of db/table/columns + * @param string $type Type of exported routine + * @param string $name Verbose name of exported routine + * @param array $routines List of routines to export + * @param string $delimiter Delimiter to use in SQL + * + * @return string SQL query + */ + protected function _exportRoutineSQL( + $db, + array $aliases, + $type, + $name, + array $routines, + $delimiter + ) { + global $crlf; + + $text = $this->_exportComment() + . $this->_exportComment($name) + . $this->_exportComment(); + + $used_alias = false; + $proc_query = ''; + + foreach ($routines as $routine) { + if (! empty($GLOBALS['sql_drop_table'])) { + $proc_query .= 'DROP ' . $type . ' IF EXISTS ' + . Util::backquote($routine) + . $delimiter . $crlf; + } + $create_query = $this->replaceWithAliases( + $GLOBALS['dbi']->getDefinition($db, $type, $routine), + $aliases, + $db, + '', + $flag + ); + // One warning per database + if ($flag) { + $used_alias = true; + } + $proc_query .= $create_query . $delimiter . $crlf . $crlf; + } + if ($used_alias) { + $text .= $this->_exportComment( + __('It appears your database uses routines;') + ) + . $this->_exportComment( + __('alias export may not work reliably in all cases.') + ) + . $this->_exportComment(); + } + $text .= $proc_query; + + return $text; + } + + /** + * Exports routines (procedures and functions) + * + * @param string $db Database + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportRoutines($db, array $aliases = []) + { + global $crlf; + + $db_alias = $db; + $this->initAlias($aliases, $db_alias); + + $text = ''; + $delimiter = '$$'; + + $procedure_names = $GLOBALS['dbi'] + ->getProceduresOrFunctions($db, 'PROCEDURE'); + $function_names = $GLOBALS['dbi']->getProceduresOrFunctions($db, 'FUNCTION'); + + if ($procedure_names || $function_names) { + $text .= $crlf + . 'DELIMITER ' . $delimiter . $crlf; + + if ($procedure_names) { + $text .= $this->_exportRoutineSQL( + $db, + $aliases, + 'PROCEDURE', + __('Procedures'), + $procedure_names, + $delimiter + ); + } + + if ($function_names) { + $text .= $this->_exportRoutineSQL( + $db, + $aliases, + 'FUNCTION', + __('Functions'), + $function_names, + $delimiter + ); + } + + $text .= 'DELIMITER ;' . $crlf; + } + + if (! empty($text)) { + return $this->export->outputHandler($text); + } + + return false; + } + + /** + * Possibly outputs comment + * + * @param string $text Text of comment + * + * @return string The formatted comment + */ + private function _exportComment($text = '') + { + if (isset($GLOBALS['sql_include_comments']) + && $GLOBALS['sql_include_comments'] + ) { + // see https://dev.mysql.com/doc/refman/5.0/en/ansi-diff-comments.html + if (empty($text)) { + return '--' . $GLOBALS['crlf']; + } + + $lines = preg_split("/\\r\\n|\\r|\\n/", $text); + $result = []; + foreach ($lines as $line) { + $result[] = '-- ' . $line . $GLOBALS['crlf']; + } + return implode('', $result); + } + + return ''; + } + + /** + * Possibly outputs CRLF + * + * @return string crlf or nothing + */ + private function _possibleCRLF() + { + if (isset($GLOBALS['sql_include_comments']) + && $GLOBALS['sql_include_comments'] + ) { + return $GLOBALS['crlf']; + } + + return ''; + } + + /** + * Outputs export footer + * + * @return bool Whether it succeeded + */ + public function exportFooter() + { + global $crlf; + + $foot = ''; + + if (isset($GLOBALS['sql_disable_fk'])) { + $foot .= 'SET FOREIGN_KEY_CHECKS=1;' . $crlf; + } + + if (isset($GLOBALS['sql_use_transaction'])) { + $foot .= 'COMMIT;' . $crlf; + } + + // restore connection settings + if ($this->_sent_charset) { + $foot .= $crlf + . '/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;' + . $crlf + . '/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;' + . $crlf + . '/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;' + . $crlf; + $this->_sent_charset = false; + } + + /* Restore timezone */ + if (isset($GLOBALS['sql_utc_time']) && $GLOBALS['sql_utc_time']) { + $GLOBALS['dbi']->query('SET time_zone = "' . $GLOBALS['old_tz'] . '"'); + } + + return $this->export->outputHandler($foot); + } + + /** + * Outputs export header. It is the first method to be called, so all + * the required variables are initialized here. + * + * @return bool Whether it succeeded + */ + public function exportHeader() + { + global $crlf, $cfg; + + if (isset($GLOBALS['sql_compatibility'])) { + $tmp_compat = $GLOBALS['sql_compatibility']; + if ($tmp_compat == 'NONE') { + $tmp_compat = ''; + } + $GLOBALS['dbi']->tryQuery('SET SQL_MODE="' . $tmp_compat . '"'); + unset($tmp_compat); + } + $head = $this->_exportComment('phpMyAdmin SQL Dump') + . $this->_exportComment('version ' . PMA_VERSION) + . $this->_exportComment('https://www.phpmyadmin.net/') + . $this->_exportComment(); + $host_string = __('Host:') . ' ' . $cfg['Server']['host']; + if (! empty($cfg['Server']['port'])) { + $host_string .= ':' . $cfg['Server']['port']; + } + $head .= $this->_exportComment($host_string); + $head .= $this->_exportComment( + __('Generation Time:') . ' ' + . Util::localisedDate() + ) + . $this->_exportComment( + __('Server version:') . ' ' . $GLOBALS['dbi']->getVersionString() + ) + . $this->_exportComment(__('PHP Version:') . ' ' . PHP_VERSION) + . $this->_possibleCRLF(); + + if (isset($GLOBALS['sql_header_comment']) + && ! empty($GLOBALS['sql_header_comment']) + ) { + // '\n' is not a newline (like "\n" would be), it's the characters + // backslash and n, as explained on the export interface + $lines = explode('\n', $GLOBALS['sql_header_comment']); + $head .= $this->_exportComment(); + foreach ($lines as $one_line) { + $head .= $this->_exportComment($one_line); + } + $head .= $this->_exportComment(); + } + + if (isset($GLOBALS['sql_disable_fk'])) { + $head .= 'SET FOREIGN_KEY_CHECKS=0;' . $crlf; + } + + // We want exported AUTO_INCREMENT columns to have still same value, + // do this only for recent MySQL exports + if (! isset($GLOBALS['sql_compatibility']) + || $GLOBALS['sql_compatibility'] == 'NONE' + ) { + $head .= 'SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";' . $crlf; + } + + if (isset($GLOBALS['sql_use_transaction'])) { + $head .= 'SET AUTOCOMMIT = 0;' . $crlf + . 'START TRANSACTION;' . $crlf; + } + + /* Change timezone if we should export timestamps in UTC */ + if (isset($GLOBALS['sql_utc_time']) && $GLOBALS['sql_utc_time']) { + $head .= 'SET time_zone = "+00:00";' . $crlf; + $GLOBALS['old_tz'] = $GLOBALS['dbi'] + ->fetchValue('SELECT @@session.time_zone'); + $GLOBALS['dbi']->query('SET time_zone = "+00:00"'); + } + + $head .= $this->_possibleCRLF(); + + if (! empty($GLOBALS['asfile'])) { + // we are saving as file, therefore we provide charset information + // so that a utility like the mysql client can interpret + // the file correctly + if (isset($GLOBALS['charset']) + && isset(Charsets::$mysqlCharsetMap[$GLOBALS['charset']]) + ) { + // we got a charset from the export dialog + $set_names = Charsets::$mysqlCharsetMap[$GLOBALS['charset']]; + } else { + // by default we use the connection charset + $set_names = Charsets::$mysqlCharsetMap['utf-8']; + } + if ($set_names == 'utf8' && $GLOBALS['dbi']->getVersion() > 50503) { + $set_names = 'utf8mb4'; + } + $head .= $crlf + . '/*!40101 SET @OLD_CHARACTER_SET_CLIENT=' + . '@@CHARACTER_SET_CLIENT */;' . $crlf + . '/*!40101 SET @OLD_CHARACTER_SET_RESULTS=' + . '@@CHARACTER_SET_RESULTS */;' . $crlf + . '/*!40101 SET @OLD_COLLATION_CONNECTION=' + . '@@COLLATION_CONNECTION */;' . $crlf + . '/*!40101 SET NAMES ' . $set_names . ' */;' . $crlf . $crlf; + $this->_sent_charset = true; + } + + return $this->export->outputHandler($head); + } + + /** + * Outputs CREATE DATABASE statement + * + * @param string $db Database name + * @param string $export_type 'server', 'database', 'table' + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBCreate($db, $export_type, $db_alias = '') + { + global $crlf; + + if (empty($db_alias)) { + $db_alias = $db; + } + if (isset($GLOBALS['sql_compatibility'])) { + $compat = $GLOBALS['sql_compatibility']; + } else { + $compat = 'NONE'; + } + if (isset($GLOBALS['sql_drop_database'])) { + if (! $this->export->outputHandler( + 'DROP DATABASE IF EXISTS ' + . Util::backquoteCompat( + $db_alias, + $compat, + isset($GLOBALS['sql_backquotes']) + ) + . ';' . $crlf + ) + ) { + return false; + } + } + if ($export_type == 'database' && ! isset($GLOBALS['sql_create_database'])) { + return true; + } + + $create_query = 'CREATE DATABASE IF NOT EXISTS ' + . Util::backquoteCompat( + $db_alias, + $compat, + isset($GLOBALS['sql_backquotes']) + ); + $collation = $GLOBALS['dbi']->getDbCollation($db); + if (mb_strpos($collation, '_')) { + $create_query .= ' DEFAULT CHARACTER SET ' + . mb_substr( + $collation, + 0, + mb_strpos($collation, '_') + ) + . ' COLLATE ' . $collation; + } else { + $create_query .= ' DEFAULT CHARACTER SET ' . $collation; + } + $create_query .= ';' . $crlf; + if (! $this->export->outputHandler($create_query)) { + return false; + } + + return $this->_exportUseStatement($db_alias, $compat); + } + + /** + * Outputs USE statement + * + * @param string $db db to use + * @param string $compat sql compatibility + * + * @return bool Whether it succeeded + */ + private function _exportUseStatement($db, $compat) + { + global $crlf; + + if (isset($GLOBALS['sql_compatibility']) + && $GLOBALS['sql_compatibility'] == 'NONE' + ) { + $result = $this->export->outputHandler( + 'USE ' + . Util::backquoteCompat( + $db, + $compat, + isset($GLOBALS['sql_backquotes']) + ) + . ';' . $crlf + ); + } else { + $result = $this->export->outputHandler('USE ' . $db . ';' . $crlf); + } + + return $result; + } + + /** + * Outputs database header + * + * @param string $db Database name + * @param string $db_alias Alias of db + * + * @return bool Whether it succeeded + */ + public function exportDBHeader($db, $db_alias = '') + { + if (empty($db_alias)) { + $db_alias = $db; + } + if (isset($GLOBALS['sql_compatibility'])) { + $compat = $GLOBALS['sql_compatibility']; + } else { + $compat = 'NONE'; + } + $head = $this->_exportComment() + . $this->_exportComment( + __('Database:') . ' ' + . Util::backquoteCompat( + $db_alias, + $compat, + isset($GLOBALS['sql_backquotes']) + ) + ) + . $this->_exportComment(); + + return $this->export->outputHandler($head); + } + + /** + * Outputs database footer + * + * @param string $db Database name + * + * @return bool Whether it succeeded + */ + public function exportDBFooter($db) + { + global $crlf; + + $result = true; + + //add indexes to the sql dump file + if (isset($GLOBALS['sql_indexes'])) { + $result = $this->export->outputHandler($GLOBALS['sql_indexes']); + unset($GLOBALS['sql_indexes']); + } + //add auto increments to the sql dump file + if (isset($GLOBALS['sql_auto_increments'])) { + $result = $this->export->outputHandler($GLOBALS['sql_auto_increments']); + unset($GLOBALS['sql_auto_increments']); + } + //add constraints to the sql dump file + if (isset($GLOBALS['sql_constraints'])) { + $result = $this->export->outputHandler($GLOBALS['sql_constraints']); + unset($GLOBALS['sql_constraints']); + } + + return $result; + } + + /** + * Exports events + * + * @param string $db Database + * + * @return bool Whether it succeeded + */ + public function exportEvents($db) + { + global $crlf; + + $text = ''; + $delimiter = '$$'; + + $event_names = $GLOBALS['dbi']->fetchResult( + "SELECT EVENT_NAME FROM information_schema.EVENTS WHERE" + . " EVENT_SCHEMA= '" . $GLOBALS['dbi']->escapeString($db) + . "';" + ); + + if ($event_names) { + $text .= $crlf + . "DELIMITER " . $delimiter . $crlf; + + $text .= $this->_exportComment() + . $this->_exportComment(__('Events')) + . $this->_exportComment(); + + foreach ($event_names as $event_name) { + if (! empty($GLOBALS['sql_drop_table'])) { + $text .= "DROP EVENT " + . Util::backquote($event_name) + . $delimiter . $crlf; + } + $text .= $GLOBALS['dbi']->getDefinition($db, 'EVENT', $event_name) + . $delimiter . $crlf . $crlf; + } + + $text .= "DELIMITER ;" . $crlf; + } + + if (! empty($text)) { + return $this->export->outputHandler($text); + } + + return false; + } + + /** + * Exports metadata from Configuration Storage + * + * @param string $db database being exported + * @param string|array $tables table(s) being exported + * @param array $metadataTypes types of metadata to export + * + * @return bool Whether it succeeded + */ + public function exportMetadata( + $db, + $tables, + array $metadataTypes + ) { + $cfgRelation = $this->relation->getRelationsParam(); + if (! isset($cfgRelation['db'])) { + return true; + } + + $comment = $this->_possibleCRLF() + . $this->_possibleCRLF() + . $this->_exportComment() + . $this->_exportComment(__('Metadata')) + . $this->_exportComment(); + if (! $this->export->outputHandler($comment)) { + return false; + } + + if (! $this->_exportUseStatement( + $cfgRelation['db'], + $GLOBALS['sql_compatibility'] + ) + ) { + return false; + } + + $r = true; + if (is_array($tables)) { + // export metadata for each table + foreach ($tables as $table) { + $r &= $this->_exportMetadata($db, $table, $metadataTypes); + } + // export metadata for the database + $r &= $this->_exportMetadata($db, null, $metadataTypes); + } else { + // export metadata for single table + $r &= $this->_exportMetadata($db, $tables, $metadataTypes); + } + + return $r; + } + + /** + * Exports metadata from Configuration Storage + * + * @param string $db database being exported + * @param string $table table being exported + * @param array $metadataTypes types of metadata to export + * + * @return bool Whether it succeeded + */ + private function _exportMetadata( + $db, + $table, + array $metadataTypes + ) { + $cfgRelation = $this->relation->getRelationsParam(); + + if (isset($table)) { + $types = [ + 'column_info' => 'db_name', + 'table_uiprefs' => 'db_name', + 'tracking' => 'db_name', + ]; + } else { + $types = [ + 'bookmark' => 'dbase', + 'relation' => 'master_db', + 'pdf_pages' => 'db_name', + 'savedsearches' => 'db_name', + 'central_columns' => 'db_name', + ]; + } + + $aliases = []; + + $comment = $this->_possibleCRLF() + . $this->_exportComment(); + + if (isset($table)) { + $comment .= $this->_exportComment( + sprintf( + __('Metadata for table %s'), + $table + ) + ); + } else { + $comment .= $this->_exportComment( + sprintf( + __('Metadata for database %s'), + $db + ) + ); + } + + $comment .= $this->_exportComment(); + + if (! $this->export->outputHandler($comment)) { + return false; + } + + foreach ($types as $type => $dbNameColumn) { + if (in_array($type, $metadataTypes) && isset($cfgRelation[$type])) { + // special case, designer pages and their coordinates + if ($type == 'pdf_pages') { + $sql_query = "SELECT `page_nr`, `page_descr` FROM " + . Util::backquote($cfgRelation['db']) + . "." . Util::backquote($cfgRelation[$type]) + . " WHERE " . Util::backquote($dbNameColumn) + . " = '" . $GLOBALS['dbi']->escapeString($db) . "'"; + + $result = $GLOBALS['dbi']->fetchResult( + $sql_query, + 'page_nr', + 'page_descr' + ); + + foreach ($result as $page => $name) { + // insert row for pdf_page + $sql_query_row = "SELECT `db_name`, `page_descr` FROM " + . Util::backquote($cfgRelation['db']) + . "." . Util::backquote( + $cfgRelation[$type] + ) + . " WHERE " . Util::backquote( + $dbNameColumn + ) + . " = '" . $GLOBALS['dbi']->escapeString($db) . "'" + . " AND `page_nr` = '" . intval($page) . "'"; + + if (! $this->exportData( + $cfgRelation['db'], + $cfgRelation[$type], + $GLOBALS['crlf'], + '', + $sql_query_row, + $aliases + ) + ) { + return false; + } + + $lastPage = $GLOBALS['crlf'] + . "SET @LAST_PAGE = LAST_INSERT_ID();" + . $GLOBALS['crlf']; + if (! $this->export->outputHandler($lastPage)) { + return false; + } + + $sql_query_coords = "SELECT `db_name`, `table_name`, " + . "'@LAST_PAGE' AS `pdf_page_number`, `x`, `y` FROM " + . Util::backquote($cfgRelation['db']) + . "." . Util::backquote( + $cfgRelation['table_coords'] + ) + . " WHERE `pdf_page_number` = '" . $page . "'"; + + $GLOBALS['exporting_metadata'] = true; + if (! $this->exportData( + $cfgRelation['db'], + $cfgRelation['table_coords'], + $GLOBALS['crlf'], + '', + $sql_query_coords, + $aliases + ) + ) { + $GLOBALS['exporting_metadata'] = false; + + return false; + } + $GLOBALS['exporting_metadata'] = false; + } + continue; + } + + // remove auto_incrementing id field for some tables + if ($type == 'bookmark') { + $sql_query = "SELECT `dbase`, `user`, `label`, `query` FROM "; + } elseif ($type == 'column_info') { + $sql_query = "SELECT `db_name`, `table_name`, `column_name`," + . " `comment`, `mimetype`, `transformation`," + . " `transformation_options`, `input_transformation`," + . " `input_transformation_options` FROM"; + } elseif ($type == 'savedsearches') { + $sql_query = "SELECT `username`, `db_name`, `search_name`," + . " `search_data` FROM"; + } else { + $sql_query = "SELECT * FROM "; + } + $sql_query .= Util::backquote($cfgRelation['db']) + . '.' . Util::backquote($cfgRelation[$type]) + . " WHERE " . Util::backquote($dbNameColumn) + . " = '" . $GLOBALS['dbi']->escapeString($db) . "'"; + if (isset($table)) { + $sql_query .= " AND `table_name` = '" + . $GLOBALS['dbi']->escapeString($table) . "'"; + } + + if (! $this->exportData( + $cfgRelation['db'], + $cfgRelation[$type], + $GLOBALS['crlf'], + '', + $sql_query, + $aliases + ) + ) { + return false; + } + } + } + + return true; + } + + /** + * Returns a stand-in CREATE definition to resolve view dependencies + * + * @param string $db the database name + * @param string $view the view name + * @param string $crlf the end of line sequence + * @param array $aliases Aliases of db/table/columns + * + * @return string resulting definition + */ + public function getTableDefStandIn($db, $view, $crlf, $aliases = []) + { + $db_alias = $db; + $view_alias = $view; + $this->initAlias($aliases, $db_alias, $view_alias); + $create_query = ''; + if (! empty($GLOBALS['sql_drop_table'])) { + $create_query .= 'DROP VIEW IF EXISTS ' + . Util::backquote($view_alias) + . ';' . $crlf; + } + + $create_query .= 'CREATE TABLE '; + + if (isset($GLOBALS['sql_if_not_exists']) + && $GLOBALS['sql_if_not_exists'] + ) { + $create_query .= 'IF NOT EXISTS '; + } + $create_query .= Util::backquote($view_alias) . ' (' . $crlf; + $tmp = []; + $columns = $GLOBALS['dbi']->getColumnsFull($db, $view); + foreach ($columns as $column_name => $definition) { + $col_alias = $column_name; + if (! empty($aliases[$db]['tables'][$view]['columns'][$col_alias])) { + $col_alias = $aliases[$db]['tables'][$view]['columns'][$col_alias]; + } + $tmp[] = Util::backquote($col_alias) . ' ' . + $definition['Type'] . $crlf; + } + $create_query .= implode(',', $tmp) . ');' . $crlf; + + return $create_query; + } + + /** + * Returns CREATE definition that matches $view's structure + * + * @param string $db the database name + * @param string $view the view name + * @param string $crlf the end of line sequence + * @param bool $add_semicolon whether to add semicolon and end-of-line at + * the end + * @param array $aliases Aliases of db/table/columns + * + * @return string resulting schema + */ + private function _getTableDefForView( + $db, + $view, + $crlf, + $add_semicolon = true, + array $aliases = [] + ) { + $db_alias = $db; + $view_alias = $view; + $this->initAlias($aliases, $db_alias, $view_alias); + $create_query = "CREATE TABLE"; + if (isset($GLOBALS['sql_if_not_exists'])) { + $create_query .= " IF NOT EXISTS "; + } + $create_query .= Util::backquote($view_alias) . "(" . $crlf; + + $columns = $GLOBALS['dbi']->getColumns($db, $view, null, true); + + $firstCol = true; + foreach ($columns as $column) { + $col_alias = $column['Field']; + if (! empty($aliases[$db]['tables'][$view]['columns'][$col_alias])) { + $col_alias = $aliases[$db]['tables'][$view]['columns'][$col_alias]; + } + $extracted_columnspec = Util::extractColumnSpec( + $column['Type'] + ); + + if (! $firstCol) { + $create_query .= "," . $crlf; + } + $create_query .= " " . Util::backquote($col_alias); + $create_query .= " " . $column['Type']; + if ($extracted_columnspec['can_contain_collation'] + && ! empty($column['Collation']) + ) { + $create_query .= " COLLATE " . $column['Collation']; + } + if ($column['Null'] == 'NO') { + $create_query .= " NOT NULL"; + } + if (isset($column['Default'])) { + $create_query .= " DEFAULT '" + . $GLOBALS['dbi']->escapeString($column['Default']) . "'"; + } else { + if ($column['Null'] == 'YES') { + $create_query .= " DEFAULT NULL"; + } + } + if (! empty($column['Comment'])) { + $create_query .= " COMMENT '" + . $GLOBALS['dbi']->escapeString($column['Comment']) . "'"; + } + $firstCol = false; + } + $create_query .= $crlf . ")" . ($add_semicolon ? ';' : '') . $crlf; + + if (isset($GLOBALS['sql_compatibility'])) { + $compat = $GLOBALS['sql_compatibility']; + } else { + $compat = 'NONE'; + } + if ($compat == 'MSSQL') { + $create_query = $this->_makeCreateTableMSSQLCompatible( + $create_query + ); + } + + return $create_query; + } + + /** + * Returns $table's CREATE definition + * + * @param string $db the database name + * @param string $table the table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case + * of error + * @param bool $show_dates whether to include creation/ + * update/check dates + * @param bool $add_semicolon whether to add semicolon and + * end-of-line at the end + * @param bool $view whether we're handling a view + * @param bool $update_indexes_increments whether we need to update + * two global variables + * @param array $aliases Aliases of db/table/columns + * + * @return string resulting schema + */ + public function getTableDef( + $db, + $table, + $crlf, + $error_url, + $show_dates = false, + $add_semicolon = true, + $view = false, + $update_indexes_increments = true, + array $aliases = [] + ) { + global $sql_drop_table, $sql_backquotes, $sql_constraints, + $sql_constraints_query, $sql_indexes, $sql_indexes_query, + $sql_auto_increments, $sql_drop_foreign_keys; + + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + + $schema_create = ''; + $auto_increment = ''; + $new_crlf = $crlf; + + if (isset($GLOBALS['sql_compatibility'])) { + $compat = $GLOBALS['sql_compatibility']; + } else { + $compat = 'NONE'; + } + + // need to use PhpMyAdmin\DatabaseInterface::QUERY_STORE + // with $GLOBALS['dbi']->numRows() in mysqli + $result = $GLOBALS['dbi']->tryQuery( + 'SHOW TABLE STATUS FROM ' . Util::backquote($db) + . ' WHERE Name = \'' . $GLOBALS['dbi']->escapeString((string) $table) . '\'', + DatabaseInterface::CONNECT_USER, + DatabaseInterface::QUERY_STORE + ); + if ($result != false) { + if ($GLOBALS['dbi']->numRows($result) > 0) { + $tmpres = $GLOBALS['dbi']->fetchAssoc($result); + + // Here we optionally add the AUTO_INCREMENT next value, + // but starting with MySQL 5.0.24, the clause is already included + // in SHOW CREATE TABLE so we'll remove it below + if (isset($GLOBALS['sql_auto_increment']) + && ! empty($tmpres['Auto_increment']) + ) { + $auto_increment .= ' AUTO_INCREMENT=' + . $tmpres['Auto_increment'] . ' '; + } + + if ($show_dates + && isset($tmpres['Create_time']) + && ! empty($tmpres['Create_time']) + ) { + $schema_create .= $this->_exportComment( + __('Creation:') . ' ' + . Util::localisedDate( + strtotime($tmpres['Create_time']) + ) + ); + $new_crlf = $this->_exportComment() . $crlf; + } + + if ($show_dates + && isset($tmpres['Update_time']) + && ! empty($tmpres['Update_time']) + ) { + $schema_create .= $this->_exportComment( + __('Last update:') . ' ' + . Util::localisedDate( + strtotime($tmpres['Update_time']) + ) + ); + $new_crlf = $this->_exportComment() . $crlf; + } + + if ($show_dates + && isset($tmpres['Check_time']) + && ! empty($tmpres['Check_time']) + ) { + $schema_create .= $this->_exportComment( + __('Last check:') . ' ' + . Util::localisedDate( + strtotime($tmpres['Check_time']) + ) + ); + $new_crlf = $this->_exportComment() . $crlf; + } + } + $GLOBALS['dbi']->freeResult($result); + } + + $schema_create .= $new_crlf; + + if (! empty($sql_drop_table) + && $GLOBALS['dbi']->getTable($db, $table)->isView() + ) { + $schema_create .= 'DROP VIEW IF EXISTS ' + . Util::backquote($table_alias, $sql_backquotes) . ';' + . $crlf; + } + + // no need to generate a DROP VIEW here, it was done earlier + if (! empty($sql_drop_table) + && ! $GLOBALS['dbi']->getTable($db, $table)->isView() + ) { + $schema_create .= 'DROP TABLE IF EXISTS ' + . Util::backquote($table_alias, $sql_backquotes) . ';' + . $crlf; + } + + // Complete table dump, + // Whether to quote table and column names or not + if ($sql_backquotes) { + $GLOBALS['dbi']->query('SET SQL_QUOTE_SHOW_CREATE = 1'); + } else { + $GLOBALS['dbi']->query('SET SQL_QUOTE_SHOW_CREATE = 0'); + } + + // I don't see the reason why this unbuffered query could cause problems, + // because SHOW CREATE TABLE returns only one row, and we free the + // results below. Nonetheless, we got 2 user reports about this + // (see bug 1562533) so I removed the unbuffered mode. + // $result = $GLOBALS['dbi']->query('SHOW CREATE TABLE ' . backquote($db) + // . '.' . backquote($table), null, DatabaseInterface::QUERY_UNBUFFERED); + // + // Note: SHOW CREATE TABLE, at least in MySQL 5.1.23, does not + // produce a displayable result for the default value of a BIT + // column, nor does the mysqldump command. See MySQL bug 35796 + $GLOBALS['dbi']->tryQuery('USE ' . Util::backquote($db)); + $result = $GLOBALS['dbi']->tryQuery( + 'SHOW CREATE TABLE ' . Util::backquote($db) . '.' + . Util::backquote($table) + ); + // an error can happen, for example the table is crashed + $tmp_error = $GLOBALS['dbi']->getError(); + if ($tmp_error) { + $message = sprintf(__('Error reading structure for table %s:'), "$db.$table"); + $message .= ' ' . $tmp_error; + if (! defined('TESTSUITE')) { + trigger_error($message, E_USER_ERROR); + } + return $this->_exportComment($message); + } + + // Old mode is stored so it can be restored once exporting is done. + $old_mode = Context::$MODE; + + $warning = ''; + if ($result != false && ($row = $GLOBALS['dbi']->fetchRow($result))) { + $create_query = $row[1]; + unset($row); + + // Convert end of line chars to one that we want (note that MySQL + // doesn't return query it will accept in all cases) + if (mb_strpos($create_query, "(\r\n ")) { + $create_query = str_replace("\r\n", $crlf, $create_query); + } elseif (mb_strpos($create_query, "(\n ")) { + $create_query = str_replace("\n", $crlf, $create_query); + } elseif (mb_strpos($create_query, "(\r ")) { + $create_query = str_replace("\r", $crlf, $create_query); + } + + /* + * Drop database name from VIEW creation. + * + * This is a bit tricky, but we need to issue SHOW CREATE TABLE with + * database name, but we don't want name to show up in CREATE VIEW + * statement. + */ + if ($view) { + $create_query = preg_replace( + '/' . preg_quote(Util::backquote($db), '/') . '\./', + '', + $create_query + ); + + // exclude definition of current user + if (isset($GLOBALS['sql_view_current_user'])) { + $create_query = preg_replace( + '/(^|\s)DEFINER=([\S]+)/', + '', + $create_query + ); + } + + // whether to replace existing view or not + if (isset($GLOBALS['sql_or_replace_view'])) { + $create_query = preg_replace( + '/^CREATE/', + 'CREATE OR REPLACE', + $create_query + ); + } + } + + // Substitute aliases in `CREATE` query. + $create_query = $this->replaceWithAliases( + $create_query, + $aliases, + $db, + $table, + $flag + ); + + // One warning per view. + if ($flag && $view) { + $warning = $this->_exportComment() + . $this->_exportComment( + __('It appears your database uses views;') + ) + . $this->_exportComment( + __('alias export may not work reliably in all cases.') + ) + . $this->_exportComment(); + } + + // Adding IF NOT EXISTS, if required. + if (isset($GLOBALS['sql_if_not_exists'])) { + $create_query = preg_replace( + '/^CREATE TABLE/', + 'CREATE TABLE IF NOT EXISTS', + $create_query + ); + } + + // Making the query MSSQL compatible. + if ($compat == 'MSSQL') { + $create_query = $this->_makeCreateTableMSSQLCompatible( + $create_query + ); + } + + // Views have no constraints, indexes, etc. They do not require any + // analysis. + if (! $view) { + if (empty($sql_backquotes)) { + // Option "Enclose table and column names with backquotes" + // was checked. + Context::$MODE |= Context::SQL_MODE_NO_ENCLOSING_QUOTES; + } + + // Using appropriate quotes. + if (($compat === 'MSSQL') || ($sql_backquotes === '"')) { + Context::$MODE |= Context::SQL_MODE_ANSI_QUOTES; + } + } + + /** + * Parser used for analysis. + * + * @var Parser + */ + $parser = new Parser($create_query); + + /** + * `CREATE TABLE` statement. + * + * @var CreateStatement + */ + $statement = $parser->statements[0]; + + if (! empty($statement->entityOptions)) { + $engine = $statement->entityOptions->has('ENGINE'); + } else { + $engine = ''; + } + + /* Avoid operation on ARCHIVE tables as those can not be altered */ + if (! empty($statement->fields) && (empty($engine) || strtoupper($engine) != 'ARCHIVE')) { + + /** + * Fragments containining definition of each constraint. + * + * @var array + */ + $constraints = []; + + /** + * Fragments containining definition of each index. + * + * @var array + */ + $indexes = []; + + /** + * Fragments containining definition of each FULLTEXT index. + * + * @var array + */ + $indexes_fulltext = []; + + /** + * Fragments containining definition of each foreign key that will + * be dropped. + * + * @var array + */ + $dropped = []; + + /** + * Fragment containining definition of the `AUTO_INCREMENT`. + * + * @var array + */ + $auto_increment = []; + + // Scanning each field of the `CREATE` statement to fill the arrays + // above. + // If the field is used in any of the arrays above, it is removed + // from the original definition. + // Also, AUTO_INCREMENT attribute is removed. + /** @var CreateDefinition $field */ + foreach ($statement->fields as $key => $field) { + if ($field->isConstraint) { + // Creating the parts that add constraints. + $constraints[] = $field::build($field); + unset($statement->fields[$key]); + } elseif (! empty($field->key)) { + // Creating the parts that add indexes (must not be + // constraints). + if ($field->key->type === 'FULLTEXT KEY') { + $indexes_fulltext[] = $field->build($field); + unset($statement->fields[$key]); + } else { + if (empty($GLOBALS['sql_if_not_exists'])) { + $indexes[] = str_replace( + 'COMMENT=\'', + 'COMMENT \'', + $field::build($field) + ); + unset($statement->fields[$key]); + } + } + } + + // Creating the parts that drop foreign keys. + if (! empty($field->key)) { + if ($field->key->type === 'FOREIGN KEY') { + $dropped[] = 'FOREIGN KEY ' . Context::escape( + $field->name + ); + unset($statement->fields[$key]); + } + } + + // Dropping AUTO_INCREMENT. + if (! empty($field->options)) { + if ($field->options->has('AUTO_INCREMENT') + && empty($GLOBALS['sql_if_not_exists']) + ) { + $auto_increment[] = $field::build($field); + $field->options->remove('AUTO_INCREMENT'); + } + } + } + + /** + * The header of the `ALTER` statement (`ALTER TABLE tbl`). + * + * @var string + */ + $alter_header = 'ALTER TABLE ' . + Util::backquoteCompat( + $table_alias, + $compat, + $sql_backquotes + ); + + /** + * The footer of the `ALTER` statement (usually ';') + * + * @var string + */ + $alter_footer = ';' . $crlf; + + // Generating constraints-related query. + if (! empty($constraints)) { + $sql_constraints_query = $alter_header . $crlf . ' ADD ' + . implode(',' . $crlf . ' ADD ', $constraints) + . $alter_footer; + + $sql_constraints = $this->generateComment( + $crlf, + $sql_constraints, + __('Constraints for dumped tables'), + __('Constraints for table'), + $table_alias, + $compat + ) . $sql_constraints_query; + } + + // Generating indexes-related query. + $sql_indexes_query = ''; + + if (! empty($indexes)) { + $sql_indexes_query .= $alter_header . $crlf . ' ADD ' + . implode(',' . $crlf . ' ADD ', $indexes) + . $alter_footer; + } + + if (! empty($indexes_fulltext)) { + // InnoDB supports one FULLTEXT index creation at a time. + // So FULLTEXT indexes are created one-by-one after other + // indexes where created. + $sql_indexes_query .= $alter_header . + ' ADD ' . implode( + $alter_footer . $alter_header . ' ADD ', + $indexes_fulltext + ) . $alter_footer; + } + + if (! empty($indexes) || ! empty($indexes_fulltext)) { + $sql_indexes = $this->generateComment( + $crlf, + $sql_indexes, + __('Indexes for dumped tables'), + __('Indexes for table'), + $table_alias, + $compat + ) . $sql_indexes_query; + } + + // Generating drop foreign keys-related query. + if (! empty($dropped)) { + $sql_drop_foreign_keys = $alter_header . $crlf . ' DROP ' + . implode(',' . $crlf . ' DROP ', $dropped) + . $alter_footer; + } + + // Generating auto-increment-related query. + if (! empty($auto_increment) && $update_indexes_increments) { + $sql_auto_increments_query = $alter_header . $crlf . ' MODIFY ' + . implode(',' . $crlf . ' MODIFY ', $auto_increment); + if (isset($GLOBALS['sql_auto_increment']) + && ($statement->entityOptions->has('AUTO_INCREMENT') !== false) + ) { + if (! isset($GLOBALS['table_data']) + || (isset($GLOBALS['table_data']) + && in_array($table, $GLOBALS['table_data'])) + ) { + $sql_auto_increments_query .= ', AUTO_INCREMENT=' + . $statement->entityOptions->has('AUTO_INCREMENT'); + } + } + $sql_auto_increments_query .= ';' . $crlf; + + $sql_auto_increments = $this->generateComment( + $crlf, + $sql_auto_increments, + __('AUTO_INCREMENT for dumped tables'), + __('AUTO_INCREMENT for table'), + $table_alias, + $compat + ) . $sql_auto_increments_query; + } + + // Removing the `AUTO_INCREMENT` attribute from the `CREATE TABLE` + // too. + if (! empty($statement->entityOptions) + && (empty($GLOBALS['sql_if_not_exists']) + || empty($GLOBALS['sql_auto_increment'])) + ) { + $statement->entityOptions->remove('AUTO_INCREMENT'); + } + + // Rebuilding the query. + $create_query = $statement->build(); + } + + $schema_create .= $create_query; + } + + $GLOBALS['dbi']->freeResult($result); + + // Restoring old mode. + Context::$MODE = $old_mode; + + return $warning . $schema_create . ($add_semicolon ? ';' . $crlf : ''); + } // end of the 'getTableDef()' function + + /** + * Returns $table's comments, relations etc. + * + * @param string $db database name + * @param string $table table name + * @param string $crlf end of line sequence + * @param bool $do_relation whether to include relation comments + * @param bool $do_mime whether to include mime comments + * @param array $aliases Aliases of db/table/columns + * + * @return string resulting comments + */ + private function _getTableComments( + $db, + $table, + $crlf, + $do_relation = false, + $do_mime = false, + array $aliases = [] + ) { + global $cfgRelation, $sql_backquotes; + + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + + $schema_create = ''; + + // Check if we can use Relations + list($res_rel, $have_rel) = $this->relation->getRelationsAndStatus( + $do_relation && ! empty($cfgRelation['relation']), + $db, + $table + ); + + if ($do_mime && $cfgRelation['mimework']) { + if (! ($mime_map = $this->transformations->getMime($db, $table, true))) { + unset($mime_map); + } + } + + if (isset($mime_map) && count($mime_map) > 0) { + $schema_create .= $this->_possibleCRLF() + . $this->_exportComment() + . $this->_exportComment( + __('MEDIA (MIME) TYPES FOR TABLE') . ' ' + . Util::backquote($table, $sql_backquotes) . ':' + ); + foreach ($mime_map as $mime_field => $mime) { + $schema_create .= $this->_exportComment( + ' ' + . Util::backquote($mime_field, $sql_backquotes) + ) + . $this->_exportComment( + ' ' + . Util::backquote( + $mime['mimetype'], + $sql_backquotes + ) + ); + } + $schema_create .= $this->_exportComment(); + } + + if ($have_rel) { + $schema_create .= $this->_possibleCRLF() + . $this->_exportComment() + . $this->_exportComment( + __('RELATIONSHIPS FOR TABLE') . ' ' + . Util::backquote($table_alias, $sql_backquotes) + . ':' + ); + + foreach ($res_rel as $rel_field => $rel) { + if ($rel_field != 'foreign_keys_data') { + $rel_field_alias = ! empty( + $aliases[$db]['tables'][$table]['columns'][$rel_field] + ) ? $aliases[$db]['tables'][$table]['columns'][$rel_field] + : $rel_field; + $schema_create .= $this->_exportComment( + ' ' + . Util::backquote( + $rel_field_alias, + $sql_backquotes + ) + ) + . $this->_exportComment( + ' ' + . Util::backquote( + $rel['foreign_table'], + $sql_backquotes + ) + . ' -> ' + . Util::backquote( + $rel['foreign_field'], + $sql_backquotes + ) + ); + } else { + foreach ($rel as $one_key) { + foreach ($one_key['index_list'] as $index => $field) { + $rel_field_alias = ! empty( + $aliases[$db]['tables'][$table]['columns'][$field] + ) ? $aliases[$db]['tables'][$table]['columns'][$field] + : $field; + $schema_create .= $this->_exportComment( + ' ' + . Util::backquote( + $rel_field_alias, + $sql_backquotes + ) + ) + . $this->_exportComment( + ' ' + . Util::backquote( + $one_key['ref_table_name'], + $sql_backquotes + ) + . ' -> ' + . Util::backquote( + $one_key['ref_index_list'][$index], + $sql_backquotes + ) + ); + } + } + } + } + $schema_create .= $this->_exportComment(); + } + + return $schema_create; + } // end of the '_getTableComments()' function + + /** + * Outputs table's structure + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $export_mode 'create_table','triggers','create_view', + * 'stand_in' + * @param string $export_type 'server', 'database', 'table' + * @param bool $relation whether to include relation comments + * @param bool $comments whether to include the pmadb-style column + * comments as comments in the structure; this is + * deprecated but the parameter is left here + * because export.php calls exportStructure() + * also for other export types which use this + * parameter + * @param bool $mime whether to include mime comments + * @param bool $dates whether to include creation/update/check dates + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportStructure( + $db, + $table, + $crlf, + $error_url, + $export_mode, + $export_type, + $relation = false, + $comments = false, + $mime = false, + $dates = false, + array $aliases = [] + ) { + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + if (isset($GLOBALS['sql_compatibility'])) { + $compat = $GLOBALS['sql_compatibility']; + } else { + $compat = 'NONE'; + } + + $formatted_table_name = Util::backquoteCompat( + $table_alias, + $compat, + isset($GLOBALS['sql_backquotes']) + ); + $dump = $this->_possibleCRLF() + . $this->_exportComment(str_repeat('-', 56)) + . $this->_possibleCRLF() + . $this->_exportComment(); + + switch ($export_mode) { + case 'create_table': + $dump .= $this->_exportComment( + __('Table structure for table') . ' ' . $formatted_table_name + ); + $dump .= $this->_exportComment(); + $dump .= $this->getTableDef( + $db, + $table, + $crlf, + $error_url, + $dates, + true, + false, + true, + $aliases + ); + $dump .= $this->_getTableComments( + $db, + $table, + $crlf, + $relation, + $mime, + $aliases + ); + break; + case 'triggers': + $dump = ''; + $delimiter = '$$'; + $triggers = $GLOBALS['dbi']->getTriggers($db, $table, $delimiter); + if ($triggers) { + $dump .= $this->_possibleCRLF() + . $this->_exportComment() + . $this->_exportComment( + __('Triggers') . ' ' . $formatted_table_name + ) + . $this->_exportComment(); + $used_alias = false; + $trigger_query = ''; + foreach ($triggers as $trigger) { + if (! empty($GLOBALS['sql_drop_table'])) { + $trigger_query .= $trigger['drop'] . ';' . $crlf; + } + + $trigger_query .= 'DELIMITER ' . $delimiter . $crlf; + $trigger_query .= $this->replaceWithAliases( + $trigger['create'], + $aliases, + $db, + $table, + $flag + ); + if ($flag) { + $used_alias = true; + } + $trigger_query .= 'DELIMITER ;' . $crlf; + } + // One warning per table. + if ($used_alias) { + $dump .= $this->_exportComment( + __('It appears your table uses triggers;') + ) + . $this->_exportComment( + __('alias export may not work reliably in all cases.') + ) + . $this->_exportComment(); + } + $dump .= $trigger_query; + } + break; + case 'create_view': + if (empty($GLOBALS['sql_views_as_tables'])) { + $dump .= $this->_exportComment( + __('Structure for view') + . ' ' + . $formatted_table_name + ) + . $this->_exportComment(); + // delete the stand-in table previously created (if any) + if ($export_type != 'table') { + $dump .= 'DROP TABLE IF EXISTS ' + . Util::backquote($table_alias) . ';' . $crlf; + } + $dump .= $this->getTableDef( + $db, + $table, + $crlf, + $error_url, + $dates, + true, + true, + true, + $aliases + ); + } else { + $dump .= $this->_exportComment( + sprintf( + __('Structure for view %s exported as a table'), + $formatted_table_name + ) + ) + . $this->_exportComment(); + // delete the stand-in table previously created (if any) + if ($export_type != 'table') { + $dump .= 'DROP TABLE IF EXISTS ' + . Util::backquote($table_alias) . ';' . $crlf; + } + $dump .= $this->_getTableDefForView( + $db, + $table, + $crlf, + true, + $aliases + ); + } + break; + case 'stand_in': + $dump .= $this->_exportComment( + __('Stand-in structure for view') . ' ' . $formatted_table_name + ) + . $this->_exportComment( + __('(See below for the actual view)') + ) + . $this->_exportComment(); + // export a stand-in definition to resolve view dependencies + $dump .= $this->getTableDefStandIn($db, $table, $crlf, $aliases); + } // end switch + + // this one is built by getTableDef() to use in table copy/move + // but not in the case of export + unset($GLOBALS['sql_constraints_query']); + + return $this->export->outputHandler($dump); + } + + /** + * Outputs the content of a table in SQL format + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $sql_query SQL query for obtaining data + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportData( + $db, + $table, + $crlf, + $error_url, + $sql_query, + array $aliases = [] + ) { + global $current_row, $sql_backquotes; + + // Do not export data for merge tables + if ($GLOBALS['dbi']->getTable($db, $table)->isMerge()) { + return true; + } + + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + + if (isset($GLOBALS['sql_compatibility'])) { + $compat = $GLOBALS['sql_compatibility']; + } else { + $compat = 'NONE'; + } + + $formatted_table_name = Util::backquoteCompat( + $table_alias, + $compat, + $sql_backquotes + ); + + // Do not export data for a VIEW, unless asked to export the view as a table + // (For a VIEW, this is called only when exporting a single VIEW) + if ($GLOBALS['dbi']->getTable($db, $table)->isView() + && empty($GLOBALS['sql_views_as_tables']) + ) { + $head = $this->_possibleCRLF() + . $this->_exportComment() + . $this->_exportComment('VIEW ' . $formatted_table_name) + . $this->_exportComment(__('Data:') . ' ' . __('None')) + . $this->_exportComment() + . $this->_possibleCRLF(); + + return $this->export->outputHandler($head); + } + + $result = $GLOBALS['dbi']->tryQuery( + $sql_query, + DatabaseInterface::CONNECT_USER, + DatabaseInterface::QUERY_UNBUFFERED + ); + // a possible error: the table has crashed + $tmp_error = $GLOBALS['dbi']->getError(); + if ($tmp_error) { + $message = sprintf(__('Error reading data for table %s:'), "$db.$table"); + $message .= ' ' . $tmp_error; + if (! defined('TESTSUITE')) { + trigger_error($message, E_USER_ERROR); + } + return $this->export->outputHandler( + $this->_exportComment($message) + ); + } + + if ($result == false) { + $GLOBALS['dbi']->freeResult($result); + + return true; + } + + $fields_cnt = $GLOBALS['dbi']->numFields($result); + + // Get field information + $fields_meta = $GLOBALS['dbi']->getFieldsMeta($result); + $field_flags = []; + for ($j = 0; $j < $fields_cnt; $j++) { + $field_flags[$j] = $GLOBALS['dbi']->fieldFlags($result, $j); + } + + $field_set = []; + for ($j = 0; $j < $fields_cnt; $j++) { + $col_as = $fields_meta[$j]->name; + if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) { + $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as]; + } + $field_set[$j] = Util::backquoteCompat( + $col_as, + $compat, + $sql_backquotes + ); + } + + if (isset($GLOBALS['sql_type']) + && $GLOBALS['sql_type'] == 'UPDATE' + ) { + // update + $schema_insert = 'UPDATE '; + if (isset($GLOBALS['sql_ignore'])) { + $schema_insert .= 'IGNORE '; + } + // avoid EOL blank + $schema_insert .= Util::backquoteCompat( + $table_alias, + $compat, + $sql_backquotes + ) . ' SET'; + } else { + // insert or replace + if (isset($GLOBALS['sql_type']) + && $GLOBALS['sql_type'] == 'REPLACE' + ) { + $sql_command = 'REPLACE'; + } else { + $sql_command = 'INSERT'; + } + + // delayed inserts? + if (isset($GLOBALS['sql_delayed'])) { + $insert_delayed = ' DELAYED'; + } else { + $insert_delayed = ''; + } + + // insert ignore? + if (isset($GLOBALS['sql_type']) + && $GLOBALS['sql_type'] == 'INSERT' + && isset($GLOBALS['sql_ignore']) + ) { + $insert_delayed .= ' IGNORE'; + } + //truncate table before insert + if (isset($GLOBALS['sql_truncate']) + && $GLOBALS['sql_truncate'] + && $sql_command == 'INSERT' + ) { + $truncate = 'TRUNCATE TABLE ' + . Util::backquoteCompat( + $table_alias, + $compat, + $sql_backquotes + ) . ";"; + $truncatehead = $this->_possibleCRLF() + . $this->_exportComment() + . $this->_exportComment( + __('Truncate table before insert') . ' ' + . $formatted_table_name + ) + . $this->_exportComment() + . $crlf; + $this->export->outputHandler($truncatehead); + $this->export->outputHandler($truncate); + } + + // scheme for inserting fields + if ($GLOBALS['sql_insert_syntax'] == 'complete' + || $GLOBALS['sql_insert_syntax'] == 'both' + ) { + $fields = implode(', ', $field_set); + $schema_insert = $sql_command . $insert_delayed . ' INTO ' + . Util::backquoteCompat( + $table_alias, + $compat, + $sql_backquotes + ) + // avoid EOL blank + . ' (' . $fields . ') VALUES'; + } else { + $schema_insert = $sql_command . $insert_delayed . ' INTO ' + . Util::backquoteCompat( + $table_alias, + $compat, + $sql_backquotes + ) + . ' VALUES'; + } + } + + //\x08\\x09, not required + $current_row = 0; + $query_size = 0; + if (($GLOBALS['sql_insert_syntax'] == 'extended' + || $GLOBALS['sql_insert_syntax'] == 'both') + && (! isset($GLOBALS['sql_type']) + || $GLOBALS['sql_type'] != 'UPDATE') + ) { + $separator = ','; + $schema_insert .= $crlf; + } else { + $separator = ';'; + } + + while ($row = $GLOBALS['dbi']->fetchRow($result)) { + if ($current_row == 0) { + $head = $this->_possibleCRLF() + . $this->_exportComment() + . $this->_exportComment( + __('Dumping data for table') . ' ' + . $formatted_table_name + ) + . $this->_exportComment() + . $crlf; + if (! $this->export->outputHandler($head)) { + return false; + } + } + // We need to SET IDENTITY_INSERT ON for MSSQL + if (isset($GLOBALS['sql_compatibility']) + && $GLOBALS['sql_compatibility'] == 'MSSQL' + && $current_row == 0 + ) { + if (! $this->export->outputHandler( + 'SET IDENTITY_INSERT ' + . Util::backquoteCompat( + $table_alias, + $compat, + $sql_backquotes + ) + . ' ON ;' . $crlf + ) + ) { + return false; + } + } + $current_row++; + $values = []; + for ($j = 0; $j < $fields_cnt; $j++) { + // NULL + if (! isset($row[$j]) || $row[$j] === null) { + $values[] = 'NULL'; + } elseif ($fields_meta[$j]->numeric + && $fields_meta[$j]->type != 'timestamp' + && ! $fields_meta[$j]->blob + ) { + // a number + // timestamp is numeric on some MySQL 4.1, BLOBs are + // sometimes numeric + $values[] = $row[$j]; + } elseif (false !== stripos($field_flags[$j], 'BINARY') + && isset($GLOBALS['sql_hex_for_binary']) + ) { + // a true BLOB + // - mysqldump only generates hex data when the --hex-blob + // option is used, for fields having the binary attribute + // no hex is generated + // - a TEXT field returns type blob but a real blob + // returns also the 'binary' flag + + // empty blobs need to be different, but '0' is also empty + // :-( + if (empty($row[$j]) && $row[$j] != '0') { + $values[] = '\'\''; + } else { + $values[] = '0x' . bin2hex($row[$j]); + } + } elseif ($fields_meta[$j]->type == 'bit') { + // detection of 'bit' works only on mysqli extension + $values[] = "b'" . $GLOBALS['dbi']->escapeString( + Util::printableBitValue( + (int) $row[$j], + (int) $fields_meta[$j]->length + ) + ) + . "'"; + } elseif ($fields_meta[$j]->type === 'geometry') { + // export GIS types as hex + $values[] = '0x' . bin2hex($row[$j]); + } elseif (! empty($GLOBALS['exporting_metadata']) + && $row[$j] == '@LAST_PAGE' + ) { + $values[] = '@LAST_PAGE'; + } else { + // something else -> treat as a string + $values[] = '\'' + . $GLOBALS['dbi']->escapeString($row[$j]) + . '\''; + } // end if + } // end for + + // should we make update? + if (isset($GLOBALS['sql_type']) + && $GLOBALS['sql_type'] == 'UPDATE' + ) { + $insert_line = $schema_insert; + for ($i = 0; $i < $fields_cnt; $i++) { + if (0 == $i) { + $insert_line .= ' '; + } + if ($i > 0) { + // avoid EOL blank + $insert_line .= ','; + } + $insert_line .= $field_set[$i] . ' = ' . $values[$i]; + } + + list($tmp_unique_condition, $tmp_clause_is_unique) + = Util::getUniqueCondition( + $result, // handle + $fields_cnt, // fields_cnt + $fields_meta, // fields_meta + $row, // row + false, // force_unique + false, // restrict_to_table + null // analyzed_sql_results + ); + $insert_line .= ' WHERE ' . $tmp_unique_condition; + unset($tmp_unique_condition, $tmp_clause_is_unique); + } else { + // Extended inserts case + if ($GLOBALS['sql_insert_syntax'] == 'extended' + || $GLOBALS['sql_insert_syntax'] == 'both' + ) { + if ($current_row == 1) { + $insert_line = $schema_insert . '(' + . implode(', ', $values) . ')'; + } else { + $insert_line = '(' . implode(', ', $values) . ')'; + $insertLineSize = mb_strlen($insert_line); + $sql_max_size = $GLOBALS['sql_max_query_size']; + if (isset($sql_max_size) + && $sql_max_size > 0 + && $query_size + $insertLineSize > $sql_max_size + ) { + if (! $this->export->outputHandler(';' . $crlf)) { + return false; + } + $query_size = 0; + $current_row = 1; + $insert_line = $schema_insert . $insert_line; + } + } + $query_size += mb_strlen($insert_line); + // Other inserts case + } else { + $insert_line = $schema_insert + . '(' . implode(', ', $values) . ')'; + } + } + unset($values); + + if (! $this->export->outputHandler( + ($current_row == 1 ? '' : $separator . $crlf) + . $insert_line + ) + ) { + return false; + } + } // end while + + if ($current_row > 0) { + if (! $this->export->outputHandler(';' . $crlf)) { + return false; + } + } + + // We need to SET IDENTITY_INSERT OFF for MSSQL + if (isset($GLOBALS['sql_compatibility']) + && $GLOBALS['sql_compatibility'] == 'MSSQL' + && $current_row > 0 + ) { + $outputSucceeded = $this->export->outputHandler( + $crlf . 'SET IDENTITY_INSERT ' + . Util::backquoteCompat( + $table_alias, + $compat, + $sql_backquotes + ) + . ' OFF;' . $crlf + ); + if (! $outputSucceeded) { + return false; + } + } + + $GLOBALS['dbi']->freeResult($result); + + return true; + } // end of the 'exportData()' function + + /** + * Make a create table statement compatible with MSSQL + * + * @param string $create_query MySQL create table statement + * + * @return string MSSQL compatible create table statement + */ + private function _makeCreateTableMSSQLCompatible($create_query) + { + // In MSSQL + // 1. No 'IF NOT EXISTS' in CREATE TABLE + // 2. DATE field doesn't exists, we will use DATETIME instead + // 3. UNSIGNED attribute doesn't exist + // 4. No length on INT, TINYINT, SMALLINT, BIGINT and no precision on + // FLOAT fields + // 5. No KEY and INDEX inside CREATE TABLE + // 6. DOUBLE field doesn't exists, we will use FLOAT instead + + $create_query = preg_replace( + "/^CREATE TABLE IF NOT EXISTS/", + 'CREATE TABLE', + $create_query + ); + // first we need to replace all lines ended with '" DATE ...,\n' + // last preg_replace preserve us from situation with date text + // inside DEFAULT field value + $create_query = preg_replace( + "/\" date DEFAULT NULL(,)?\n/", + '" datetime DEFAULT NULL$1' . "\n", + $create_query + ); + $create_query = preg_replace( + "/\" date NOT NULL(,)?\n/", + '" datetime NOT NULL$1' . "\n", + $create_query + ); + $create_query = preg_replace( + '/" date NOT NULL DEFAULT \'([^\'])/', + '" datetime NOT NULL DEFAULT \'$1', + $create_query + ); + + // next we need to replace all lines ended with ') UNSIGNED ...,' + // last preg_replace preserve us from situation with unsigned text + // inside DEFAULT field value + $create_query = preg_replace( + "/\) unsigned NOT NULL(,)?\n/", + ') NOT NULL$1' . "\n", + $create_query + ); + $create_query = preg_replace( + "/\) unsigned DEFAULT NULL(,)?\n/", + ') DEFAULT NULL$1' . "\n", + $create_query + ); + $create_query = preg_replace( + '/\) unsigned NOT NULL DEFAULT \'([^\'])/', + ') NOT NULL DEFAULT \'$1', + $create_query + ); + + // we need to replace all lines ended with + // '" INT|TINYINT([0-9]{1,}) ...,' last preg_replace preserve us + // from situation with int([0-9]{1,}) text inside DEFAULT field + // value + $create_query = preg_replace( + '/" (int|tinyint|smallint|bigint)\([0-9]+\) DEFAULT NULL(,)?\n/', + '" $1 DEFAULT NULL$2' . "\n", + $create_query + ); + $create_query = preg_replace( + '/" (int|tinyint|smallint|bigint)\([0-9]+\) NOT NULL(,)?\n/', + '" $1 NOT NULL$2' . "\n", + $create_query + ); + $create_query = preg_replace( + '/" (int|tinyint|smallint|bigint)\([0-9]+\) NOT NULL DEFAULT \'([^\'])/', + '" $1 NOT NULL DEFAULT \'$2', + $create_query + ); + + // we need to replace all lines ended with + // '" FLOAT|DOUBLE([0-9,]{1,}) ...,' + // last preg_replace preserve us from situation with + // float([0-9,]{1,}) text inside DEFAULT field value + $create_query = preg_replace( + '/" (float|double)(\([0-9]+,[0-9,]+\))? DEFAULT NULL(,)?\n/', + '" float DEFAULT NULL$3' . "\n", + $create_query + ); + $create_query = preg_replace( + '/" (float|double)(\([0-9,]+,[0-9,]+\))? NOT NULL(,)?\n/', + '" float NOT NULL$3' . "\n", + $create_query + ); + return preg_replace( + '/" (float|double)(\([0-9,]+,[0-9,]+\))? NOT NULL DEFAULT \'([^\'])/', + '" float NOT NULL DEFAULT \'$3', + $create_query + ); + + // @todo remove indexes from CREATE TABLE + } + + /** + * replaces db/table/column names with their aliases + * + * @param string $sql_query SQL query in which aliases are to be substituted + * @param array $aliases Alias information for db/table/column + * @param string $db the database name + * @param string $table the tablename + * @param string $flag the flag denoting whether any replacement was done + * + * @return string query replaced with aliases + */ + public function replaceWithAliases( + $sql_query, + array $aliases, + $db, + $table = '', + &$flag = null + ) { + $flag = false; + + /** + * The parser of this query. + * + * @var Parser $parser + */ + $parser = new Parser($sql_query); + + if (empty($parser->statements[0])) { + return $sql_query; + } + + /** + * The statement that represents the query. + * + * @var CreateStatement $statement + */ + $statement = $parser->statements[0]; + + /** + * Old database name. + * + * @var string $old_database + */ + $old_database = $db; + + // Replacing aliases in `CREATE TABLE` statement. + if ($statement->options->has('TABLE')) { + // Extracting the name of the old database and table from the + // statement to make sure the parameters are corect. + if (! empty($statement->name->database)) { + $old_database = $statement->name->database; + } + + /** + * Old table name. + * + * @var string $old_table + */ + $old_table = $statement->name->table; + + // Finding the aliased database name. + // The database might be empty so we have to add a few checks. + $new_database = null; + if (! empty($statement->name->database)) { + $new_database = $statement->name->database; + if (! empty($aliases[$old_database]['alias'])) { + $new_database = $aliases[$old_database]['alias']; + } + } + + // Finding the aliases table name. + $new_table = $old_table; + if (! empty($aliases[$old_database]['tables'][$old_table]['alias'])) { + $new_table = $aliases[$old_database]['tables'][$old_table]['alias']; + } + + // Replacing new values. + if (($statement->name->database !== $new_database) + || ($statement->name->table !== $new_table) + ) { + $statement->name->database = $new_database; + $statement->name->table = $new_table; + $statement->name->expr = null; // Force rebuild. + $flag = true; + } + + /** @var CreateDefinition $field */ + foreach ($statement->fields as $field) { + // Column name. + if (! empty($field->type)) { + if (! empty($aliases[$old_database]['tables'][$old_table]['columns'][$field->name])) { + $field->name = $aliases[$old_database]['tables'][$old_table]['columns'][$field->name]; + $flag = true; + } + } + + // Key's columns. + if (! empty($field->key)) { + foreach ($field->key->columns as $key => $column) { + if (! empty($aliases[$old_database]['tables'][$old_table]['columns'][$column['name']])) { + $field->key->columns[$key]['name'] = $aliases[$old_database]['tables'][$old_table]['columns'][$column['name']]; + $flag = true; + } + } + } + + // References. + if (! empty($field->references)) { + $ref_table = $field->references->table->table; + // Replacing table. + if (! empty($aliases[$old_database]['tables'][$ref_table]['alias'])) { + $field->references->table->table + = $aliases[$old_database]['tables'][$ref_table]['alias']; + $field->references->table->expr = null; + $flag = true; + } + // Replacing column names. + foreach ($field->references->columns as $key => $column) { + if (! empty($aliases[$old_database]['tables'][$ref_table]['columns'][$column])) { + $field->references->columns[$key] + = $aliases[$old_database]['tables'][$ref_table]['columns'][$column]; + $flag = true; + } + } + } + } + } elseif ($statement->options->has('TRIGGER')) { + // Extracting the name of the old database and table from the + // statement to make sure the parameters are corect. + if (! empty($statement->table->database)) { + $old_database = $statement->table->database; + } + + /** + * Old table name. + * + * @var string $old_table + */ + $old_table = $statement->table->table; + + if (! empty($aliases[$old_database]['tables'][$old_table]['alias'])) { + $statement->table->table + = $aliases[$old_database]['tables'][$old_table]['alias']; + $statement->table->expr = null; // Force rebuild. + $flag = true; + } + } + + if ($statement->options->has('TRIGGER') + || $statement->options->has('PROCEDURE') + || $statement->options->has('FUNCTION') + || $statement->options->has('VIEW') + ) { + // Repalcing the body. + for ($i = 0, $count = count($statement->body); $i < $count; ++$i) { + + /** + * Token parsed at this moment. + * + * @var Token $token + */ + $token = $statement->body[$i]; + + // Replacing only symbols (that are not variables) and unknown + // identifiers. + if (($token->type === Token::TYPE_SYMBOL) + && (! ($token->flags & Token::FLAG_SYMBOL_VARIABLE)) + || (($token->type === Token::TYPE_KEYWORD) + && (! ($token->flags & Token::FLAG_KEYWORD_RESERVED)) + || ($token->type === Token::TYPE_NONE)) + ) { + $alias = $this->getAlias($aliases, $token->value); + if (! empty($alias)) { + // Replacing the token. + $token->token = Context::escape($alias); + $flag = true; + } + } + } + } + + return $statement->build(); + } + + /** + * Generate comment + * + * @param string $crlf Carriage return character + * @param string|null $sql_statement SQL statement + * @param string $comment1 Comment for dumped table + * @param string $comment2 Comment for current table + * @param string $table_alias Table alias + * @param string $compat Compatibility mode + * + * @return string + */ + protected function generateComment( + $crlf, + ?string $sql_statement, + $comment1, + $comment2, + $table_alias, + $compat + ) { + if (! isset($sql_statement)) { + if (isset($GLOBALS['no_constraints_comments'])) { + $sql_statement = ''; + } else { + $sql_statement = $crlf + . $this->_exportComment() + . $this->_exportComment($comment1) + . $this->_exportComment(); + } + } + + // comments for current table + if (! isset($GLOBALS['no_constraints_comments'])) { + $sql_statement .= $crlf + . $this->_exportComment() + . $this->_exportComment( + $comment2 . ' ' . Util::backquoteCompat( + $table_alias, + $compat, + isset($GLOBALS['sql_backquotes']) + ) + ) + . $this->_exportComment(); + } + + return $sql_statement; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportTexytext.php b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportTexytext.php new file mode 100644 index 0000000..a2fdec8 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportTexytext.php @@ -0,0 +1,624 @@ +setProperties(); + } + + /** + * Sets the export Texy! text properties + * + * @return void + */ + protected function setProperties() + { + $exportPluginProperties = new ExportPluginProperties(); + $exportPluginProperties->setText('Texy! text'); + $exportPluginProperties->setExtension('txt'); + $exportPluginProperties->setMimeType('text/plain'); + $exportPluginProperties->setOptionsText(__('Options')); + + // create the root group that will be the options field for + // $exportPluginProperties + // this will be shown as "Format specific options" + $exportSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // what to dump (structure/data/both) main group + $dumpWhat = new OptionsPropertyMainGroup( + "general_opts", + __('Dump table') + ); + // create primary items and add them to the group + $leaf = new RadioPropertyItem("structure_or_data"); + $leaf->setValues( + [ + 'structure' => __('structure'), + 'data' => __('data'), + 'structure_and_data' => __('structure and data'), + ] + ); + $dumpWhat->addProperty($leaf); + // add the main group to the root group + $exportSpecificOptions->addProperty($dumpWhat); + + // data options main group + $dataOptions = new OptionsPropertyMainGroup( + "data", + __('Data dump options') + ); + $dataOptions->setForce('structure'); + // create primary items and add them to the group + $leaf = new BoolPropertyItem( + "columns", + __('Put columns names in the first row') + ); + $dataOptions->addProperty($leaf); + $leaf = new TextPropertyItem( + 'null', + __('Replace NULL with:') + ); + $dataOptions->addProperty($leaf); + // add the main group to the root group + $exportSpecificOptions->addProperty($dataOptions); + + // set the options for the export plugin property item + $exportPluginProperties->setOptions($exportSpecificOptions); + $this->properties = $exportPluginProperties; + } + + /** + * Outputs export header + * + * @return bool Whether it succeeded + */ + public function exportHeader() + { + return true; + } + + /** + * Outputs export footer + * + * @return bool Whether it succeeded + */ + public function exportFooter() + { + return true; + } + + /** + * Outputs database header + * + * @param string $db Database name + * @param string $db_alias Alias of db + * + * @return bool Whether it succeeded + */ + public function exportDBHeader($db, $db_alias = '') + { + if (empty($db_alias)) { + $db_alias = $db; + } + + return $this->export->outputHandler( + '===' . __('Database') . ' ' . $db_alias . "\n\n" + ); + } + + /** + * Outputs database footer + * + * @param string $db Database name + * + * @return bool Whether it succeeded + */ + public function exportDBFooter($db) + { + return true; + } + + /** + * Outputs CREATE DATABASE statement + * + * @param string $db Database name + * @param string $export_type 'server', 'database', 'table' + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBCreate($db, $export_type, $db_alias = '') + { + return true; + } + + /** + * Outputs the content of a table in NHibernate format + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $sql_query SQL query for obtaining data + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportData( + $db, + $table, + $crlf, + $error_url, + $sql_query, + array $aliases = [] + ) { + global $what; + + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + + if (! $this->export->outputHandler( + '== ' . __('Dumping data for table') . ' ' . $table_alias . "\n\n" + ) + ) { + return false; + } + + // Gets the data from the database + $result = $GLOBALS['dbi']->query( + $sql_query, + DatabaseInterface::CONNECT_USER, + DatabaseInterface::QUERY_UNBUFFERED + ); + $fields_cnt = $GLOBALS['dbi']->numFields($result); + + // If required, get fields name at the first line + if (isset($GLOBALS[$what . '_columns'])) { + $text_output = "|------\n"; + for ($i = 0; $i < $fields_cnt; $i++) { + $col_as = $GLOBALS['dbi']->fieldName($result, $i); + if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) { + $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as]; + } + $text_output .= '|' + . htmlspecialchars(stripslashes($col_as)); + } // end for + $text_output .= "\n|------\n"; + if (! $this->export->outputHandler($text_output)) { + return false; + } + } // end if + + // Format the data + while ($row = $GLOBALS['dbi']->fetchRow($result)) { + $text_output = ''; + for ($j = 0; $j < $fields_cnt; $j++) { + if (! isset($row[$j]) || $row[$j] === null) { + $value = $GLOBALS[$what . '_null']; + } elseif ($row[$j] == '0' || $row[$j] != '') { + $value = $row[$j]; + } else { + $value = ' '; + } + $text_output .= '|' + . str_replace( + '|', + '|', + htmlspecialchars($value) + ); + } // end for + $text_output .= "\n"; + if (! $this->export->outputHandler($text_output)) { + return false; + } + } // end while + $GLOBALS['dbi']->freeResult($result); + + return true; + } + + /** + * Returns a stand-in CREATE definition to resolve view dependencies + * + * @param string $db the database name + * @param string $view the view name + * @param string $crlf the end of line sequence + * @param array $aliases Aliases of db/table/columns + * + * @return string resulting definition + */ + public function getTableDefStandIn($db, $view, $crlf, $aliases = []) + { + $text_output = ''; + + /** + * Get the unique keys in the table + */ + $unique_keys = []; + $keys = $GLOBALS['dbi']->getTableIndexes($db, $view); + foreach ($keys as $key) { + if ($key['Non_unique'] == 0) { + $unique_keys[] = $key['Column_name']; + } + } + + /** + * Gets fields properties + */ + $GLOBALS['dbi']->selectDb($db); + + /** + * Displays the table structure + */ + + $text_output .= "|------\n" + . '|' . __('Column') + . '|' . __('Type') + . '|' . __('Null') + . '|' . __('Default') + . "\n|------\n"; + + $columns = $GLOBALS['dbi']->getColumns($db, $view); + foreach ($columns as $column) { + $col_as = $column['Field'] ?? null; + if (! empty($aliases[$db]['tables'][$view]['columns'][$col_as])) { + $col_as = $aliases[$db]['tables'][$view]['columns'][$col_as]; + } + $text_output .= $this->formatOneColumnDefinition( + $column, + $unique_keys, + $col_as + ); + $text_output .= "\n"; + } // end foreach + + return $text_output; + } + + /** + * Returns $table's CREATE definition + * + * @param string $db the database name + * @param string $table the table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param bool $do_relation whether to include relation comments + * @param bool $do_comments whether to include the pmadb-style column + * comments as comments in the structure; + * this is deprecated but the parameter is + * left here because export.php calls + * $this->exportStructure() also for other + * export types which use this parameter + * @param bool $do_mime whether to include mime comments + * @param bool $show_dates whether to include creation/update/check dates + * @param bool $add_semicolon whether to add semicolon and end-of-line + * at the end + * @param bool $view whether we're handling a view + * @param array $aliases Aliases of db/table/columns + * + * @return string resulting schema + */ + public function getTableDef( + $db, + $table, + $crlf, + $error_url, + $do_relation, + $do_comments, + $do_mime, + $show_dates = false, + $add_semicolon = true, + $view = false, + array $aliases = [] + ) { + global $cfgRelation; + + $text_output = ''; + + /** + * Get the unique keys in the table + */ + $unique_keys = []; + $keys = $GLOBALS['dbi']->getTableIndexes($db, $table); + foreach ($keys as $key) { + if ($key['Non_unique'] == 0) { + $unique_keys[] = $key['Column_name']; + } + } + + /** + * Gets fields properties + */ + $GLOBALS['dbi']->selectDb($db); + + // Check if we can use Relations + list($res_rel, $have_rel) = $this->relation->getRelationsAndStatus( + $do_relation && ! empty($cfgRelation['relation']), + $db, + $table + ); + + /** + * Displays the table structure + */ + + $text_output .= "|------\n"; + $text_output .= '|' . __('Column'); + $text_output .= '|' . __('Type'); + $text_output .= '|' . __('Null'); + $text_output .= '|' . __('Default'); + if ($do_relation && $have_rel) { + $text_output .= '|' . __('Links to'); + } + if ($do_comments) { + $text_output .= '|' . __('Comments'); + $comments = $this->relation->getComments($db, $table); + } + if ($do_mime && $cfgRelation['mimework']) { + $text_output .= '|' . __('Media (MIME) type'); + $mime_map = $this->transformations->getMime($db, $table, true); + } + $text_output .= "\n|------\n"; + + $columns = $GLOBALS['dbi']->getColumns($db, $table); + foreach ($columns as $column) { + $col_as = $column['Field']; + if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) { + $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as]; + } + $text_output .= $this->formatOneColumnDefinition( + $column, + $unique_keys, + $col_as + ); + $field_name = $column['Field']; + if ($do_relation && $have_rel) { + $text_output .= '|' . htmlspecialchars( + $this->getRelationString( + $res_rel, + $field_name, + $db, + $aliases + ) + ); + } + if ($do_comments && $cfgRelation['commwork']) { + $text_output .= '|' + . (isset($comments[$field_name]) + ? htmlspecialchars($comments[$field_name]) + : ''); + } + if ($do_mime && $cfgRelation['mimework']) { + $text_output .= '|' + . (isset($mime_map[$field_name]) + ? htmlspecialchars( + str_replace('_', '/', $mime_map[$field_name]['mimetype']) + ) + : ''); + } + + $text_output .= "\n"; + } // end foreach + + return $text_output; + } // end of the '$this->getTableDef()' function + + /** + * Outputs triggers + * + * @param string $db database name + * @param string $table table name + * + * @return string Formatted triggers list + */ + public function getTriggers($db, $table) + { + $dump = "|------\n"; + $dump .= '|' . __('Name'); + $dump .= '|' . __('Time'); + $dump .= '|' . __('Event'); + $dump .= '|' . __('Definition'); + $dump .= "\n|------\n"; + + $triggers = $GLOBALS['dbi']->getTriggers($db, $table); + + foreach ($triggers as $trigger) { + $dump .= '|' . $trigger['name']; + $dump .= '|' . $trigger['action_timing']; + $dump .= '|' . $trigger['event_manipulation']; + $dump .= '|' . + str_replace( + '|', + '|', + htmlspecialchars($trigger['definition']) + ); + $dump .= "\n"; + } + + return $dump; + } + + /** + * Outputs table's structure + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $export_mode 'create_table', 'triggers', 'create_view', + * 'stand_in' + * @param string $export_type 'server', 'database', 'table' + * @param bool $do_relation whether to include relation comments + * @param bool $do_comments whether to include the pmadb-style column + * comments as comments in the structure; + * this is deprecated but the parameter is + * left here because export.php calls + * $this->exportStructure() also for other + * export types which use this parameter + * @param bool $do_mime whether to include mime comments + * @param bool $dates whether to include creation/update/check dates + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportStructure( + $db, + $table, + $crlf, + $error_url, + $export_mode, + $export_type, + $do_relation = false, + $do_comments = false, + $do_mime = false, + $dates = false, + array $aliases = [] + ) { + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + $dump = ''; + + switch ($export_mode) { + case 'create_table': + $dump .= '== ' . __('Table structure for table') . ' ' + . $table_alias . "\n\n"; + $dump .= $this->getTableDef( + $db, + $table, + $crlf, + $error_url, + $do_relation, + $do_comments, + $do_mime, + $dates, + true, + false, + $aliases + ); + break; + case 'triggers': + $dump = ''; + $triggers = $GLOBALS['dbi']->getTriggers($db, $table); + if ($triggers) { + $dump .= '== ' . __('Triggers') . ' ' . $table_alias . "\n\n"; + $dump .= $this->getTriggers($db, $table); + } + break; + case 'create_view': + $dump .= '== ' . __('Structure for view') . ' ' . $table_alias . "\n\n"; + $dump .= $this->getTableDef( + $db, + $table, + $crlf, + $error_url, + $do_relation, + $do_comments, + $do_mime, + $dates, + true, + true, + $aliases + ); + break; + case 'stand_in': + $dump .= '== ' . __('Stand-in structure for view') + . ' ' . $table . "\n\n"; + // export a stand-in definition to resolve view dependencies + $dump .= $this->getTableDefStandIn($db, $table, $crlf, $aliases); + } // end switch + + return $this->export->outputHandler($dump); + } + + /** + * Formats the definition for one column + * + * @param array $column info about this column + * @param array $unique_keys unique keys for this table + * @param string $col_alias Column Alias + * + * @return string Formatted column definition + */ + public function formatOneColumnDefinition( + $column, + $unique_keys, + $col_alias = '' + ) { + if (empty($col_alias)) { + $col_alias = $column['Field']; + } + $extracted_columnspec + = Util::extractColumnSpec($column['Type']); + $type = $extracted_columnspec['print_type']; + if (empty($type)) { + $type = ' '; + } + + if (! isset($column['Default'])) { + if ($column['Null'] != 'NO') { + $column['Default'] = 'NULL'; + } + } + + $fmt_pre = ''; + $fmt_post = ''; + if (in_array($column['Field'], $unique_keys)) { + $fmt_pre = '**' . $fmt_pre; + $fmt_post .= '**'; + } + if ($column['Key'] == 'PRI') { + $fmt_pre = '//' . $fmt_pre; + $fmt_post .= '//'; + } + $definition = '|' + . $fmt_pre . htmlspecialchars($col_alias) . $fmt_post; + $definition .= '|' . htmlspecialchars($type); + $definition .= '|' + . (($column['Null'] == '' || $column['Null'] == 'NO') + ? __('No') : __('Yes')); + $definition .= '|' + . htmlspecialchars( + isset($column['Default']) ? $column['Default'] : '' + ); + + return $definition; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportXml.php b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportXml.php new file mode 100644 index 0000000..db85add --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportXml.php @@ -0,0 +1,593 @@ +setProperties(); + } + + /** + * Initialize the local variables that are used for export XML + * + * @return void + */ + protected function initSpecificVariables() + { + global $table, $tables; + $this->_setTable($table); + if (is_array($tables)) { + $this->_setTables($tables); + } + } + + /** + * Sets the export XML properties + * + * @return void + */ + protected function setProperties() + { + // create the export plugin property item + $exportPluginProperties = new ExportPluginProperties(); + $exportPluginProperties->setText('XML'); + $exportPluginProperties->setExtension('xml'); + $exportPluginProperties->setMimeType('text/xml'); + $exportPluginProperties->setOptionsText(__('Options')); + + // create the root group that will be the options field for + // $exportPluginProperties + // this will be shown as "Format specific options" + $exportSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // general options main group + $generalOptions = new OptionsPropertyMainGroup("general_opts"); + // create primary items and add them to the group + $leaf = new HiddenPropertyItem("structure_or_data"); + $generalOptions->addProperty($leaf); + // add the main group to the root group + $exportSpecificOptions->addProperty($generalOptions); + + // export structure main group + $structure = new OptionsPropertyMainGroup( + "structure", + __('Object creation options (all are recommended)') + ); + + // create primary items and add them to the group + $leaf = new BoolPropertyItem( + "export_events", + __('Events') + ); + $structure->addProperty($leaf); + $leaf = new BoolPropertyItem( + "export_functions", + __('Functions') + ); + $structure->addProperty($leaf); + $leaf = new BoolPropertyItem( + "export_procedures", + __('Procedures') + ); + $structure->addProperty($leaf); + $leaf = new BoolPropertyItem( + "export_tables", + __('Tables') + ); + $structure->addProperty($leaf); + $leaf = new BoolPropertyItem( + "export_triggers", + __('Triggers') + ); + $structure->addProperty($leaf); + $leaf = new BoolPropertyItem( + "export_views", + __('Views') + ); + $structure->addProperty($leaf); + $exportSpecificOptions->addProperty($structure); + + // data main group + $data = new OptionsPropertyMainGroup( + "data", + __('Data dump options') + ); + // create primary items and add them to the group + $leaf = new BoolPropertyItem( + "export_contents", + __('Export contents') + ); + $data->addProperty($leaf); + $exportSpecificOptions->addProperty($data); + + // set the options for the export plugin property item + $exportPluginProperties->setOptions($exportSpecificOptions); + $this->properties = $exportPluginProperties; + } + + /** + * Generates output for SQL defintions of routines + * + * @param string $db Database name + * @param string $type Item type to be used in XML output + * @param string $dbitype Item type used in DBI qieries + * + * @return string XML with definitions + */ + private function _exportRoutines($db, $type, $dbitype) + { + // Export routines + $routines = $GLOBALS['dbi']->getProceduresOrFunctions( + $db, + $dbitype + ); + return $this->_exportDefinitions($db, $type, $dbitype, $routines); + } + + /** + * Generates output for SQL defintions + * + * @param string $db Database name + * @param string $type Item type to be used in XML output + * @param string $dbitype Item type used in DBI qieries + * @param array $names Names of items to export + * + * @return string XML with definitions + */ + private function _exportDefinitions($db, $type, $dbitype, array $names) + { + global $crlf; + + $head = ''; + + if ($names) { + foreach ($names as $name) { + $head .= ' ' . $crlf; + + // Do some formatting + $sql = $GLOBALS['dbi']->getDefinition($db, $dbitype, $name); + $sql = htmlspecialchars(rtrim($sql)); + $sql = str_replace("\n", "\n ", $sql); + + $head .= " " . $sql . $crlf; + $head .= ' ' . $crlf; + } + } + + return $head; + } + + /** + * Outputs export header. It is the first method to be called, so all + * the required variables are initialized here. + * + * @return bool Whether it succeeded + */ + public function exportHeader() + { + $this->initSpecificVariables(); + global $crlf, $cfg, $db; + $table = $this->_getTable(); + $tables = $this->_getTables(); + + $export_struct = isset($GLOBALS['xml_export_functions']) + || isset($GLOBALS['xml_export_procedures']) + || isset($GLOBALS['xml_export_tables']) + || isset($GLOBALS['xml_export_triggers']) + || isset($GLOBALS['xml_export_views']); + $export_data = isset($GLOBALS['xml_export_contents']) ? true : false; + + if ($GLOBALS['output_charset_conversion']) { + $charset = $GLOBALS['charset']; + } else { + $charset = 'utf-8'; + } + + $head = '' . $crlf + . '' . $crlf . $crlf; + + $head .= '' . $crlf; + + if ($export_struct) { + $result = $GLOBALS['dbi']->fetchResult( + 'SELECT `DEFAULT_CHARACTER_SET_NAME`, `DEFAULT_COLLATION_NAME`' + . ' FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME`' + . ' = \'' . $GLOBALS['dbi']->escapeString($db) . '\' LIMIT 1' + ); + $db_collation = $result[0]['DEFAULT_COLLATION_NAME']; + $db_charset = $result[0]['DEFAULT_CHARACTER_SET_NAME']; + + $head .= ' ' . $crlf; + $head .= ' ' . $crlf; + $head .= ' ' . $crlf; + + if ($tables === null) { + $tables = []; + } + + if (count($tables) === 0) { + $tables[] = $table; + } + + foreach ($tables as $table) { + // Export tables and views + $result = $GLOBALS['dbi']->fetchResult( + 'SHOW CREATE TABLE ' . Util::backquote($db) . '.' + . Util::backquote($table), + 0 + ); + $tbl = $result[$table][1]; + + $is_view = $GLOBALS['dbi']->getTable($db, $table) + ->isView(); + + if ($is_view) { + $type = 'view'; + } else { + $type = 'table'; + } + + if ($is_view && ! isset($GLOBALS['xml_export_views'])) { + continue; + } + + if (! $is_view && ! isset($GLOBALS['xml_export_tables'])) { + continue; + } + + $head .= ' ' + . $crlf; + + $tbl = " " . htmlspecialchars($tbl); + $tbl = str_replace("\n", "\n ", $tbl); + + $head .= $tbl . ';' . $crlf; + $head .= ' ' . $crlf; + + if (isset($GLOBALS['xml_export_triggers']) + && $GLOBALS['xml_export_triggers'] + ) { + // Export triggers + $triggers = $GLOBALS['dbi']->getTriggers($db, $table); + if ($triggers) { + foreach ($triggers as $trigger) { + $code = $trigger['create']; + $head .= ' ' . $crlf; + + // Do some formatting + $code = mb_substr(rtrim($code), 0, -3); + $code = " " . htmlspecialchars($code); + $code = str_replace("\n", "\n ", $code); + + $head .= $code . $crlf; + $head .= ' ' . $crlf; + } + + unset($trigger); + unset($triggers); + } + } + } + + if (isset($GLOBALS['xml_export_functions']) + && $GLOBALS['xml_export_functions'] + ) { + $head .= $this->_exportRoutines($db, 'function', 'FUNCTION'); + } + + if (isset($GLOBALS['xml_export_procedures']) + && $GLOBALS['xml_export_procedures'] + ) { + $head .= $this->_exportRoutines($db, 'procedure', 'PROCEDURE'); + } + + if (isset($GLOBALS['xml_export_events']) + && $GLOBALS['xml_export_events'] + ) { + // Export events + $events = $GLOBALS['dbi']->fetchResult( + "SELECT EVENT_NAME FROM information_schema.EVENTS " + . "WHERE EVENT_SCHEMA='" . $GLOBALS['dbi']->escapeString($db) + . "'" + ); + $head .= $this->_exportDefinitions( + $db, + 'event', + 'EVENT', + $events + ); + } + + unset($result); + + $head .= ' ' . $crlf; + $head .= ' ' . $crlf; + + if ($export_data) { + $head .= $crlf; + } + } + + return $this->export->outputHandler($head); + } + + /** + * Outputs export footer + * + * @return bool Whether it succeeded + */ + public function exportFooter() + { + $foot = ''; + + return $this->export->outputHandler($foot); + } + + /** + * Outputs database header + * + * @param string $db Database name + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBHeader($db, $db_alias = '') + { + global $crlf; + + if (empty($db_alias)) { + $db_alias = $db; + } + if (isset($GLOBALS['xml_export_contents']) + && $GLOBALS['xml_export_contents'] + ) { + $head = ' ' . $crlf . ' ' . $crlf; + + return $this->export->outputHandler($head); + } + + return true; + } + + /** + * Outputs database footer + * + * @param string $db Database name + * + * @return bool Whether it succeeded + */ + public function exportDBFooter($db) + { + global $crlf; + + if (isset($GLOBALS['xml_export_contents']) + && $GLOBALS['xml_export_contents'] + ) { + return $this->export->outputHandler(' ' . $crlf); + } + + return true; + } + + /** + * Outputs CREATE DATABASE statement + * + * @param string $db Database name + * @param string $export_type 'server', 'database', 'table' + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBCreate($db, $export_type, $db_alias = '') + { + return true; + } + + /** + * Outputs the content of a table in XML format + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $sql_query SQL query for obtaining data + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportData( + $db, + $table, + $crlf, + $error_url, + $sql_query, + array $aliases = [] + ) { + // Do not export data for merge tables + if ($GLOBALS['dbi']->getTable($db, $table)->isMerge()) { + return true; + } + + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + if (isset($GLOBALS['xml_export_contents']) + && $GLOBALS['xml_export_contents'] + ) { + $result = $GLOBALS['dbi']->query( + $sql_query, + DatabaseInterface::CONNECT_USER, + DatabaseInterface::QUERY_UNBUFFERED + ); + + $columns_cnt = $GLOBALS['dbi']->numFields($result); + $columns = []; + for ($i = 0; $i < $columns_cnt; $i++) { + $columns[$i] = stripslashes($GLOBALS['dbi']->fieldName($result, $i)); + } + unset($i); + + $buffer = ' ' . $crlf; + if (! $this->export->outputHandler($buffer)) { + return false; + } + + while ($record = $GLOBALS['dbi']->fetchRow($result)) { + $buffer = ' ' . $crlf; + for ($i = 0; $i < $columns_cnt; $i++) { + $col_as = $columns[$i]; + if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as]) + ) { + $col_as + = $aliases[$db]['tables'][$table]['columns'][$col_as]; + } + // If a cell is NULL, still export it to preserve + // the XML structure + if (! isset($record[$i]) || $record[$i] === null) { + $record[$i] = 'NULL'; + } + $buffer .= ' ' + . htmlspecialchars((string) $record[$i]) + . '' . $crlf; + } + $buffer .= '
' . $crlf; + + if (! $this->export->outputHandler($buffer)) { + return false; + } + } + $GLOBALS['dbi']->freeResult($result); + } + + return true; + } + + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the table name + * + * @return string + */ + private function _getTable() + { + return $this->_table; + } + + /** + * Sets the table name + * + * @param string $table table name + * + * @return void + */ + private function _setTable($table) + { + $this->_table = $table; + } + + /** + * Gets the table names + * + * @return array + */ + private function _getTables() + { + return $this->_tables; + } + + /** + * Sets the table names + * + * @param array $tables table names + * + * @return void + */ + private function _setTables(array $tables) + { + $this->_tables = $tables; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportYaml.php b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportYaml.php new file mode 100644 index 0000000..971f9c6 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Export/ExportYaml.php @@ -0,0 +1,230 @@ +setProperties(); + } + + /** + * Sets the export YAML properties + * + * @return void + */ + protected function setProperties() + { + $exportPluginProperties = new ExportPluginProperties(); + $exportPluginProperties->setText('YAML'); + $exportPluginProperties->setExtension('yml'); + $exportPluginProperties->setMimeType('text/yaml'); + $exportPluginProperties->setForceFile(true); + $exportPluginProperties->setOptionsText(__('Options')); + + // create the root group that will be the options field for + // $exportPluginProperties + // this will be shown as "Format specific options" + $exportSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // general options main group + $generalOptions = new OptionsPropertyMainGroup("general_opts"); + // create primary items and add them to the group + $leaf = new HiddenPropertyItem("structure_or_data"); + $generalOptions->addProperty($leaf); + // add the main group to the root group + $exportSpecificOptions->addProperty($generalOptions); + + // set the options for the export plugin property item + $exportPluginProperties->setOptions($exportSpecificOptions); + $this->properties = $exportPluginProperties; + } + + /** + * Outputs export header + * + * @return bool Whether it succeeded + */ + public function exportHeader() + { + $this->export->outputHandler( + '%YAML 1.1' . $GLOBALS['crlf'] . '---' . $GLOBALS['crlf'] + ); + + return true; + } + + /** + * Outputs export footer + * + * @return bool Whether it succeeded + */ + public function exportFooter() + { + $this->export->outputHandler('...' . $GLOBALS['crlf']); + + return true; + } + + /** + * Outputs database header + * + * @param string $db Database name + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBHeader($db, $db_alias = '') + { + return true; + } + + /** + * Outputs database footer + * + * @param string $db Database name + * + * @return bool Whether it succeeded + */ + public function exportDBFooter($db) + { + return true; + } + + /** + * Outputs CREATE DATABASE statement + * + * @param string $db Database name + * @param string $export_type 'server', 'database', 'table' + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBCreate($db, $export_type, $db_alias = '') + { + return true; + } + + /** + * Outputs the content of a table in JSON format + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $sql_query SQL query for obtaining data + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportData( + $db, + $table, + $crlf, + $error_url, + $sql_query, + array $aliases = [] + ) { + $db_alias = $db; + $table_alias = $table; + $this->initAlias($aliases, $db_alias, $table_alias); + $result = $GLOBALS['dbi']->query( + $sql_query, + DatabaseInterface::CONNECT_USER, + DatabaseInterface::QUERY_UNBUFFERED + ); + + $columns_cnt = $GLOBALS['dbi']->numFields($result); + $columns = []; + for ($i = 0; $i < $columns_cnt; $i++) { + $col_as = $GLOBALS['dbi']->fieldName($result, $i); + if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) { + $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as]; + } + $columns[$i] = stripslashes($col_as); + } + + $buffer = ''; + $record_cnt = 0; + while ($record = $GLOBALS['dbi']->fetchRow($result)) { + $record_cnt++; + + // Output table name as comment if this is the first record of the table + if ($record_cnt == 1) { + $buffer = '# ' . $db_alias . '.' . $table_alias . $crlf; + $buffer .= '-' . $crlf; + } else { + $buffer = '-' . $crlf; + } + + for ($i = 0; $i < $columns_cnt; $i++) { + if (! isset($record[$i])) { + continue; + } + + if ($record[$i] === null) { + $buffer .= ' ' . $columns[$i] . ': null' . $crlf; + continue; + } + + if (is_numeric($record[$i])) { + $buffer .= ' ' . $columns[$i] . ': ' . $record[$i] . $crlf; + continue; + } + + $record[$i] = str_replace( + [ + '\\', + '"', + "\n", + "\r", + ], + [ + '\\\\', + '\"', + '\n', + '\r', + ], + $record[$i] + ); + $buffer .= ' ' . $columns[$i] . ': "' . $record[$i] . '"' . $crlf; + } + + if (! $this->export->outputHandler($buffer)) { + return false; + } + } + $GLOBALS['dbi']->freeResult($result); + + return true; + } // end getTableYAML +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Export/Helpers/Pdf.php b/srcs/phpmyadmin/libraries/classes/Plugins/Export/Helpers/Pdf.php new file mode 100644 index 0000000..58f1b5a --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Export/Helpers/Pdf.php @@ -0,0 +1,855 @@ +relation = new Relation($GLOBALS['dbi']); + $this->transformations = new Transformations(); + } + + /** + * Add page if needed. + * + * @param float|int $h cell height. Default value: 0 + * @param mixed $y starting y position, leave empty for current + * position + * @param boolean $addpage if true add a page, otherwise only return + * the true/false state + * + * @return boolean true in case of page break, false otherwise. + */ + public function checkPageBreak($h = 0, $y = '', $addpage = true) + { + if (TCPDF_STATIC::empty_string($y)) { + $y = $this->y; + } + $current_page = $this->page; + if ((($y + $h) > $this->PageBreakTrigger) + && (! $this->InFooter) + && $this->AcceptPageBreak() + ) { + if ($addpage) { + //Automatic page break + $x = $this->x; + $this->AddPage($this->CurOrientation); + $this->y = $this->dataY; + $oldpage = $this->page - 1; + + $this_page_orm = $this->pagedim[$this->page]['orm']; + $old_page_orm = $this->pagedim[$oldpage]['orm']; + $this_page_olm = $this->pagedim[$this->page]['olm']; + $old_page_olm = $this->pagedim[$oldpage]['olm']; + if ($this->rtl) { + if ($this_page_orm != $old_page_orm) { + $this->x = $x - ($this_page_orm - $old_page_orm); + } else { + $this->x = $x; + } + } else { + if ($this_page_olm != $old_page_olm) { + $this->x = $x + ($this_page_olm - $old_page_olm); + } else { + $this->x = $x; + } + } + } + + return true; + } + + // account for columns mode + return $current_page != $this->page; + } + + /** + * This method is used to render the page header. + * + * @return void + */ + // @codingStandardsIgnoreLine + public function Header() + { + global $maxY; + // We don't want automatic page breaks while generating header + // as this can lead to infinite recursion as auto generated page + // will want header as well causing another page break + // FIXME: Better approach might be to try to compact the content + $this->SetAutoPageBreak(false); + // Check if header for this page already exists + if (! isset($this->headerset[$this->page])) { + $this->SetY($this->tMargin - ($this->FontSizePt / $this->k) * 5); + $this->cellFontSize = $this->FontSizePt; + $this->SetFont( + PdfLib::PMA_PDF_FONT, + '', + ($this->titleFontSize + ?: $this->FontSizePt) + ); + $this->Cell(0, $this->FontSizePt, $this->titleText, 0, 1, 'C'); + $this->SetFont(PdfLib::PMA_PDF_FONT, '', $this->cellFontSize); + $this->SetY($this->tMargin - ($this->FontSizePt / $this->k) * 2.5); + $this->Cell( + 0, + $this->FontSizePt, + __('Database:') . ' ' . $this->dbAlias . ', ' + . __('Table:') . ' ' . $this->tableAlias . ', ' + . __('Purpose:') . ' ' . $this->purpose, + 0, + 1, + 'L' + ); + $l = $this->lMargin; + foreach ($this->colTitles as $col => $txt) { + $this->SetXY($l, $this->tMargin); + $this->MultiCell( + $this->tablewidths[$col], + $this->FontSizePt, + $txt + ); + $l += $this->tablewidths[$col]; + $maxY = $maxY < $this->GetY() ? $this->GetY() : $maxY; + } + $this->SetXY($this->lMargin, $this->tMargin); + $this->SetFillColor(200, 200, 200); + $l = $this->lMargin; + foreach ($this->colTitles as $col => $txt) { + $this->SetXY($l, $this->tMargin); + $this->Cell( + $this->tablewidths[$col], + $maxY - $this->tMargin, + '', + 1, + 0, + 'L', + 1 + ); + $this->SetXY($l, $this->tMargin); + $this->MultiCell( + $this->tablewidths[$col], + $this->FontSizePt, + $txt, + 0, + 'C' + ); + $l += $this->tablewidths[$col]; + } + $this->SetFillColor(255, 255, 255); + // set headerset + $this->headerset[$this->page] = 1; + } + + $this->dataY = $maxY; + $this->SetAutoPageBreak(true); + } + + /** + * Generate table + * + * @param int $lineheight Height of line + * + * @return void + */ + public function morepagestable($lineheight = 8) + { + // some things to set and 'remember' + $l = $this->lMargin; + $startheight = $h = $this->dataY; + $startpage = $currpage = $this->page; + + // calculate the whole width + $fullwidth = 0; + foreach ($this->tablewidths as $width) { + $fullwidth += $width; + } + + // Now let's start to write the table + $row = 0; + $tmpheight = []; + $maxpage = $this->page; + + while ($data = $GLOBALS['dbi']->fetchRow($this->results)) { + $this->page = $currpage; + // write the horizontal borders + $this->Line($l, $h, $fullwidth + $l, $h); + // write the content and remember the height of the highest col + foreach ($data as $col => $txt) { + $this->page = $currpage; + $this->SetXY($l, $h); + if ($this->tablewidths[$col] > 0) { + $this->MultiCell( + $this->tablewidths[$col], + $lineheight, + $txt, + 0, + $this->colAlign[$col] + ); + $l += $this->tablewidths[$col]; + } + + if (! isset($tmpheight[$row . '-' . $this->page])) { + $tmpheight[$row . '-' . $this->page] = 0; + } + if ($tmpheight[$row . '-' . $this->page] < $this->GetY()) { + $tmpheight[$row . '-' . $this->page] = $this->GetY(); + } + if ($this->page > $maxpage) { + $maxpage = $this->page; + } + unset($data[$col]); + } + + // get the height we were in the last used page + $h = $tmpheight[$row . '-' . $maxpage]; + // set the "pointer" to the left margin + $l = $this->lMargin; + // set the $currpage to the last page + $currpage = $maxpage; + unset($data[$row]); + $row++; + } + // draw the borders + // we start adding a horizontal line on the last page + $this->page = $maxpage; + $this->Line($l, $h, $fullwidth + $l, $h); + // now we start at the top of the document and walk down + for ($i = $startpage; $i <= $maxpage; $i++) { + $this->page = $i; + $l = $this->lMargin; + $t = $i == $startpage ? $startheight : $this->tMargin; + $lh = $i == $maxpage ? $h : $this->h - $this->bMargin; + $this->Line($l, $t, $l, $lh); + foreach ($this->tablewidths as $width) { + $l += $width; + $this->Line($l, $t, $l, $lh); + } + } + // set it to the last page, if not it'll cause some problems + $this->page = $maxpage; + } + + /** + * Sets a set of attributes. + * + * @param array $attr array containing the attributes + * + * @return void + */ + public function setAttributes(array $attr = []) + { + foreach ($attr as $key => $val) { + $this->$key = $val; + } + } + + /** + * Defines the top margin. + * The method can be called before creating the first page. + * + * @param float $topMargin the margin + * + * @return void + */ + public function setTopMargin($topMargin) + { + $this->tMargin = $topMargin; + } + + /** + * Prints triggers + * + * @param string $db database name + * @param string $table table name + * + * @return void + */ + public function getTriggers($db, $table) + { + $triggers = $GLOBALS['dbi']->getTriggers($db, $table); + if ([] === $triggers) { + return; //prevents printing blank trigger list for any table + } + + unset($this->tablewidths); + unset($this->colTitles); + unset($this->titleWidth); + unset($this->colFits); + unset($this->display_column); + unset($this->colAlign); + + /** + * Making table heading + * Keeping column width constant + */ + $this->colTitles[0] = __('Name'); + $this->tablewidths[0] = 90; + $this->colTitles[1] = __('Time'); + $this->tablewidths[1] = 80; + $this->colTitles[2] = __('Event'); + $this->tablewidths[2] = 40; + $this->colTitles[3] = __('Definition'); + $this->tablewidths[3] = 240; + + for ($columns_cnt = 0; $columns_cnt < 4; $columns_cnt++) { + $this->colAlign[$columns_cnt] = 'L'; + $this->display_column[$columns_cnt] = true; + } + + // Starting to fill table with required info + + $this->SetY($this->tMargin); + $this->AddPage(); + $this->SetFont(PdfLib::PMA_PDF_FONT, '', 9); + + $l = $this->lMargin; + $startheight = $h = $this->dataY; + $startpage = $currpage = $this->page; + + // calculate the whole width + $fullwidth = 0; + foreach ($this->tablewidths as $width) { + $fullwidth += $width; + } + + $row = 0; + $tmpheight = []; + $maxpage = $this->page; + $data = []; + + foreach ($triggers as $trigger) { + $data[] = $trigger['name']; + $data[] = $trigger['action_timing']; + $data[] = $trigger['event_manipulation']; + $data[] = $trigger['definition']; + $this->page = $currpage; + // write the horizontal borders + $this->Line($l, $h, $fullwidth + $l, $h); + // write the content and remember the height of the highest col + foreach ($data as $col => $txt) { + $this->page = $currpage; + $this->SetXY($l, $h); + if ($this->tablewidths[$col] > 0) { + $this->MultiCell( + $this->tablewidths[$col], + $this->FontSizePt, + $txt, + 0, + $this->colAlign[$col] + ); + $l += $this->tablewidths[$col]; + } + + if (! isset($tmpheight[$row . '-' . $this->page])) { + $tmpheight[$row . '-' . $this->page] = 0; + } + if ($tmpheight[$row . '-' . $this->page] < $this->GetY()) { + $tmpheight[$row . '-' . $this->page] = $this->GetY(); + } + if ($this->page > $maxpage) { + $maxpage = $this->page; + } + } + // get the height we were in the last used page + $h = $tmpheight[$row . '-' . $maxpage]; + // set the "pointer" to the left margin + $l = $this->lMargin; + // set the $currpage to the last page + $currpage = $maxpage; + unset($data); + $row++; + } + // draw the borders + // we start adding a horizontal line on the last page + $this->page = $maxpage; + $this->Line($l, $h, $fullwidth + $l, $h); + // now we start at the top of the document and walk down + for ($i = $startpage; $i <= $maxpage; $i++) { + $this->page = $i; + $l = $this->lMargin; + $t = $i == $startpage ? $startheight : $this->tMargin; + $lh = $i == $maxpage ? $h : $this->h - $this->bMargin; + $this->Line($l, $t, $l, $lh); + foreach ($this->tablewidths as $width) { + $l += $width; + $this->Line($l, $t, $l, $lh); + } + } + // set it to the last page, if not it'll cause some problems + $this->page = $maxpage; + } + + /** + * Print $table's CREATE definition + * + * @param string $db the database name + * @param string $table the table name + * @param bool $do_relation whether to include relation comments + * @param bool $do_comments whether to include the pmadb-style column + * comments as comments in the structure; + * this is deprecated but the parameter is + * left here because export.php calls + * PMA_exportStructure() also for other + * export types which use this parameter + * @param bool $do_mime whether to include mime comments + * @param bool $view whether we're handling a view + * @param array $aliases aliases of db/table/columns + * + * @return void + */ + public function getTableDef( + $db, + $table, + $do_relation, + $do_comments, + $do_mime, + $view = false, + array $aliases = [] + ) { + // set $cfgRelation here, because there is a chance that it's modified + // since the class initialization + global $cfgRelation; + + unset($this->tablewidths); + unset($this->colTitles); + unset($this->titleWidth); + unset($this->colFits); + unset($this->display_column); + unset($this->colAlign); + + /** + * Gets fields properties + */ + $GLOBALS['dbi']->selectDb($db); + + /** + * All these three checks do_relation, do_comment and do_mime is + * not required. As presently all are set true by default. + * But when, methods to take user input will be developed, + * it will be of use + */ + // Check if we can use Relations + if ($do_relation) { + // Find which tables are related with the current one and write it in + // an array + $res_rel = $this->relation->getForeigners($db, $table); + $have_rel = ! empty($res_rel); + } else { + $have_rel = false; + } // end if + + //column count and table heading + + $this->colTitles[0] = __('Column'); + $this->tablewidths[0] = 90; + $this->colTitles[1] = __('Type'); + $this->tablewidths[1] = 80; + $this->colTitles[2] = __('Null'); + $this->tablewidths[2] = 40; + $this->colTitles[3] = __('Default'); + $this->tablewidths[3] = 120; + + for ($columns_cnt = 0; $columns_cnt < 4; $columns_cnt++) { + $this->colAlign[$columns_cnt] = 'L'; + $this->display_column[$columns_cnt] = true; + } + + if ($do_relation && $have_rel) { + $this->colTitles[$columns_cnt] = __('Links to'); + $this->display_column[$columns_cnt] = true; + $this->colAlign[$columns_cnt] = 'L'; + $this->tablewidths[$columns_cnt] = 120; + $columns_cnt++; + } + if ($do_comments /*&& $cfgRelation['commwork']*/) { + $this->colTitles[$columns_cnt] = __('Comments'); + $this->display_column[$columns_cnt] = true; + $this->colAlign[$columns_cnt] = 'L'; + $this->tablewidths[$columns_cnt] = 120; + $columns_cnt++; + } + if ($do_mime && $cfgRelation['mimework']) { + $this->colTitles[$columns_cnt] = __('Media (MIME) type'); + $this->display_column[$columns_cnt] = true; + $this->colAlign[$columns_cnt] = 'L'; + $this->tablewidths[$columns_cnt] = 120; + $columns_cnt++; + } + + // Starting to fill table with required info + + $this->SetY($this->tMargin); + $this->AddPage(); + $this->SetFont(PdfLib::PMA_PDF_FONT, '', 9); + + // Now let's start to write the table structure + + if ($do_comments) { + $comments = $this->relation->getComments($db, $table); + } + if ($do_mime && $cfgRelation['mimework']) { + $mime_map = $this->transformations->getMime($db, $table, true); + } + + $columns = $GLOBALS['dbi']->getColumns($db, $table); + + // some things to set and 'remember' + $l = $this->lMargin; + $startheight = $h = $this->dataY; + $startpage = $currpage = $this->page; + // calculate the whole width + $fullwidth = 0; + foreach ($this->tablewidths as $width) { + $fullwidth += $width; + } + + $row = 0; + $tmpheight = []; + $maxpage = $this->page; + $data = []; + + // fun begin + foreach ($columns as $column) { + $extracted_columnspec + = Util::extractColumnSpec($column['Type']); + + $type = $extracted_columnspec['print_type']; + if (empty($type)) { + $type = ' '; + } + + if (! isset($column['Default'])) { + if ($column['Null'] != 'NO') { + $column['Default'] = 'NULL'; + } + } + $data[] = $column['Field']; + $data[] = $type; + $data[] = $column['Null'] == '' || $column['Null'] == 'NO' + ? 'No' + : 'Yes'; + $data[] = isset($column['Default']) ? $column['Default'] : ''; + + $field_name = $column['Field']; + + if ($do_relation && $have_rel) { + $data[] = isset($res_rel[$field_name]) + ? $res_rel[$field_name]['foreign_table'] + . ' (' . $res_rel[$field_name]['foreign_field'] + . ')' + : ''; + } + if ($do_comments) { + $data[] = isset($comments[$field_name]) + ? $comments[$field_name] + : ''; + } + if ($do_mime) { + $data[] = isset($mime_map[$field_name]) + ? $mime_map[$field_name]['mimetype'] + : ''; + } + + $this->page = $currpage; + // write the horizontal borders + $this->Line($l, $h, $fullwidth + $l, $h); + // write the content and remember the height of the highest col + foreach ($data as $col => $txt) { + $this->page = $currpage; + $this->SetXY($l, $h); + if ($this->tablewidths[$col] > 0) { + $this->MultiCell( + $this->tablewidths[$col], + $this->FontSizePt, + $txt, + 0, + $this->colAlign[$col] + ); + $l += $this->tablewidths[$col]; + } + + if (! isset($tmpheight[$row . '-' . $this->page])) { + $tmpheight[$row . '-' . $this->page] = 0; + } + if ($tmpheight[$row . '-' . $this->page] < $this->GetY()) { + $tmpheight[$row . '-' . $this->page] = $this->GetY(); + } + if ($this->page > $maxpage) { + $maxpage = $this->page; + } + } + + // get the height we were in the last used page + $h = $tmpheight[$row . '-' . $maxpage]; + // set the "pointer" to the left margin + $l = $this->lMargin; + // set the $currpage to the last page + $currpage = $maxpage; + unset($data); + $row++; + } + // draw the borders + // we start adding a horizontal line on the last page + $this->page = $maxpage; + $this->Line($l, $h, $fullwidth + $l, $h); + // now we start at the top of the document and walk down + for ($i = $startpage; $i <= $maxpage; $i++) { + $this->page = $i; + $l = $this->lMargin; + $t = $i == $startpage ? $startheight : $this->tMargin; + $lh = $i == $maxpage ? $h : $this->h - $this->bMargin; + $this->Line($l, $t, $l, $lh); + foreach ($this->tablewidths as $width) { + $l += $width; + $this->Line($l, $t, $l, $lh); + } + } + // set it to the last page, if not it'll cause some problems + $this->page = $maxpage; + } + + /** + * MySQL report + * + * @param string $query Query to execute + * + * @return void + */ + public function mysqlReport($query) + { + unset($this->tablewidths); + unset($this->colTitles); + unset($this->titleWidth); + unset($this->colFits); + unset($this->display_column); + unset($this->colAlign); + + /** + * Pass 1 for column widths + */ + $this->results = $GLOBALS['dbi']->query( + $query, + DatabaseInterface::CONNECT_USER, + DatabaseInterface::QUERY_UNBUFFERED + ); + $this->numFields = $GLOBALS['dbi']->numFields($this->results); + $this->fields = $GLOBALS['dbi']->getFieldsMeta($this->results); + + // sColWidth = starting col width (an average size width) + $availableWidth = $this->w - $this->lMargin - $this->rMargin; + $this->sColWidth = $availableWidth / $this->numFields; + $totalTitleWidth = 0; + + // loop through results header and set initial + // col widths/ titles/ alignment + // if a col title is less than the starting col width, + // reduce that column size + $colFits = []; + $titleWidth = []; + for ($i = 0; $i < $this->numFields; $i++) { + $col_as = $this->fields[$i]->name; + $db = $this->currentDb; + $table = $this->currentTable; + if (! empty($this->aliases[$db]['tables'][$table]['columns'][$col_as])) { + $col_as = $this->aliases[$db]['tables'][$table]['columns'][$col_as]; + } + $stringWidth = $this->GetStringWidth($col_as) + 6; + // save the real title's width + $titleWidth[$i] = $stringWidth; + $totalTitleWidth += $stringWidth; + + // set any column titles less than the start width to + // the column title width + if ($stringWidth < $this->sColWidth) { + $colFits[$i] = $stringWidth; + } + $this->colTitles[$i] = $col_as; + $this->display_column[$i] = true; + + switch ($this->fields[$i]->type) { + case 'int': + $this->colAlign[$i] = 'R'; + break; + case 'blob': + case 'tinyblob': + case 'mediumblob': + case 'longblob': + /** + * @todo do not deactivate completely the display + * but show the field's name and [BLOB] + */ + if (false !== stripos($this->fields[$i]->flags, 'BINARY')) { + $this->display_column[$i] = false; + unset($this->colTitles[$i]); + } + $this->colAlign[$i] = 'L'; + break; + default: + $this->colAlign[$i] = 'L'; + } + } + + // title width verification + if ($totalTitleWidth > $availableWidth) { + $adjustingMode = true; + } else { + $adjustingMode = false; + // we have enough space for all the titles at their + // original width so use the true title's width + foreach ($titleWidth as $key => $val) { + $colFits[$key] = $val; + } + } + + // loop through the data; any column whose contents + // is greater than the column size is resized + /** + * @todo force here a LIMIT to avoid reading all rows + */ + while ($row = $GLOBALS['dbi']->fetchRow($this->results)) { + foreach ($colFits as $key => $val) { + $stringWidth = $this->GetStringWidth($row[$key]) + 6; + if ($adjustingMode && ($stringWidth > $this->sColWidth)) { + // any column whose data's width is bigger than + // the start width is now discarded + unset($colFits[$key]); + } else { + // if data's width is bigger than the current column width, + // enlarge the column (but avoid enlarging it if the + // data's width is very big) + if ($stringWidth > $val + && $stringWidth < ($this->sColWidth * 3) + ) { + $colFits[$key] = $stringWidth; + } + } + } + } + + $totAlreadyFitted = 0; + foreach ($colFits as $key => $val) { + // set fitted columns to smallest size + $this->tablewidths[$key] = $val; + // to work out how much (if any) space has been freed up + $totAlreadyFitted += $val; + } + + if ($adjustingMode) { + $surplus = (count($colFits) * $this->sColWidth) - $totAlreadyFitted; + $surplusToAdd = $surplus / ($this->numFields - count($colFits)); + } else { + $surplusToAdd = 0; + } + + for ($i = 0; $i < $this->numFields; $i++) { + if (! array_key_exists($i, $colFits)) { + $this->tablewidths[$i] = $this->sColWidth + $surplusToAdd; + } + if ($this->display_column[$i] == false) { + $this->tablewidths[$i] = 0; + } + } + + ksort($this->tablewidths); + + $GLOBALS['dbi']->freeResult($this->results); + + // Pass 2 + + $this->results = $GLOBALS['dbi']->query( + $query, + DatabaseInterface::CONNECT_USER, + DatabaseInterface::QUERY_UNBUFFERED + ); + $this->SetY($this->tMargin); + $this->AddPage(); + $this->SetFont(PdfLib::PMA_PDF_FONT, '', 9); + $this->morepagestable($this->FontSizePt); + $GLOBALS['dbi']->freeResult($this->results); + } // end of mysqlReport function +} // end of Pdf class diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Export/Helpers/TableProperty.php b/srcs/phpmyadmin/libraries/classes/Plugins/Export/Helpers/TableProperty.php new file mode 100644 index 0000000..8991251 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Export/Helpers/TableProperty.php @@ -0,0 +1,277 @@ +name = trim((string) $row[0]); + $this->type = trim((string) $row[1]); + $this->nullable = trim((string) $row[2]); + $this->key = trim((string) $row[3]); + $this->defaultValue = trim((string) $row[4]); + $this->ext = trim((string) $row[5]); + } + + /** + * Gets the pure type + * + * @return string type + */ + public function getPureType() + { + $pos = mb_strpos($this->type, "("); + if ($pos > 0) { + return mb_substr($this->type, 0, $pos); + } + return $this->type; + } + + /** + * Tells whether the key is null or not + * + * @return string true if the key is not null, false otherwise + */ + public function isNotNull() + { + return $this->nullable === "NO" ? "true" : "false"; + } + + /** + * Tells whether the key is unique or not + * + * @return string "true" if the key is unique, "false" otherwise + */ + public function isUnique(): string + { + return ($this->key === "PRI" || $this->key === "UNI") ? "true" : "false"; + } + + /** + * Gets the .NET primitive type + * + * @return string type + */ + public function getDotNetPrimitiveType() + { + if (mb_strpos($this->type, "int") === 0) { + return "int"; + } + if (mb_strpos($this->type, "longtext") === 0) { + return "string"; + } + if (mb_strpos($this->type, "long") === 0) { + return "long"; + } + if (mb_strpos($this->type, "char") === 0) { + return "string"; + } + if (mb_strpos($this->type, "varchar") === 0) { + return "string"; + } + if (mb_strpos($this->type, "text") === 0) { + return "string"; + } + if (mb_strpos($this->type, "tinyint") === 0) { + return "bool"; + } + if (mb_strpos($this->type, "datetime") === 0) { + return "DateTime"; + } + return "unknown"; + } + + /** + * Gets the .NET object type + * + * @return string type + */ + public function getDotNetObjectType() + { + if (mb_strpos($this->type, "int") === 0) { + return "Int32"; + } + if (mb_strpos($this->type, "longtext") === 0) { + return "String"; + } + if (mb_strpos($this->type, "long") === 0) { + return "Long"; + } + if (mb_strpos($this->type, "char") === 0) { + return "String"; + } + if (mb_strpos($this->type, "varchar") === 0) { + return "String"; + } + if (mb_strpos($this->type, "text") === 0) { + return "String"; + } + if (mb_strpos($this->type, "tinyint") === 0) { + return "Boolean"; + } + if (mb_strpos($this->type, "datetime") === 0) { + return "DateTime"; + } + return "Unknown"; + } + + /** + * Gets the index name + * + * @return string containing the name of the index + */ + public function getIndexName() + { + if (strlen($this->key) > 0) { + return "index=\"" + . htmlspecialchars($this->name, ENT_COMPAT, 'UTF-8') + . "\""; + } + return ""; + } + + /** + * Tells whether the key is primary or not + * + * @return bool true if the key is primary, false otherwise + */ + public function isPK(): bool + { + return $this->key === "PRI"; + } + + /** + * Formats a string for C# + * + * @param string $text string to be formatted + * + * @return string formatted text + */ + public function formatCs($text) + { + $text = str_replace( + '#name#', + ExportCodegen::cgMakeIdentifier($this->name, false), + $text + ); + return $this->format($text); + } + + /** + * Formats a string for XML + * + * @param string $text string to be formatted + * + * @return string formatted text + */ + public function formatXml($text) + { + $text = str_replace( + [ + '#name#', + '#indexName#', + ], + [ + htmlspecialchars($this->name, ENT_COMPAT, 'UTF-8'), + $this->getIndexName(), + ], + $text + ); + return $this->format($text); + } + + /** + * Formats a string + * + * @param string $text string to be formatted + * + * @return string formatted text + */ + public function format($text) + { + $text = str_replace( + [ + '#ucfirstName#', + '#dotNetPrimitiveType#', + '#dotNetObjectType#', + '#type#', + '#notNull#', + '#unique#', + ], + [ + ExportCodegen::cgMakeIdentifier($this->name), + $this->getDotNetPrimitiveType(), + $this->getDotNetObjectType(), + $this->getPureType(), + $this->isNotNull(), + $this->isUnique(), + ], + $text + ); + return $text; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Export/README b/srcs/phpmyadmin/libraries/classes/Plugins/Export/README new file mode 100644 index 0000000..5ada85b --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Export/README @@ -0,0 +1,255 @@ +This directory holds export plugins for phpMyAdmin. Any new plugin should +basically follow the structure presented here. Official plugins need to +have str* messages with their definition in language files, but if you build +some plugins for your use, you can directly use texts in plugin. + +setProperties(); + } + + // optional - declare global variables and use getters later + /** + * Initialize the local variables that are used specific for export SQL + * + * @global type $global_variable_name + * [..] + * + * @return void + */ + protected function initSpecificVariables() + { + global $global_variable_name; + $this->_setGlobalVariableName($global_variable_name); + } + + /** + * Sets the export plugin properties. + * Called in the constructor. + * + * @return void + */ + protected function setProperties() + { + $exportPluginProperties = new PhpMyAdmin\Properties\Plugins\ExportPluginProperties(); + $exportPluginProperties->setText('[name]'); // the name of your plug-in + $exportPluginProperties->setExtension('[ext]'); // extension this plug-in can handle + $exportPluginProperties->setOptionsText(__('Options')); + + // create the root group that will be the options field for + // $exportPluginProperties + // this will be shown as "Format specific options" + $exportSpecificOptions = new PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // general options main group + $generalOptions = new PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup( + "general_opts" + ); + + // optional : + // create primary items and add them to the group + // type - one of the classes listed in libraries/properties/options/items/ + // name - form element name + // text - description in GUI + // size - size of text element + // len - maximal size of input + // values - possible values of the item + $leaf = new PhpMyAdmin\Properties\Options\Items\RadioPropertyItem( + "structure_or_data" + ); + $leaf->setValues( + array( + 'structure' => __('structure'), + 'data' => __('data'), + 'structure_and_data' => __('structure and data') + ) + ); + $generalOptions->addProperty($leaf); + + // add the main group to the root group + $exportSpecificOptions->addProperty($generalOptions); + + // set the options for the export plugin property item + $exportPluginProperties->setOptions($exportSpecificOptions); + $this->properties = $exportPluginProperties; + } + + /** + * Outputs export header + * + * @return bool Whether it succeeded + */ + public function exportHeader () + { + // implementation + return true; + } + + /** + * Outputs export footer + * + * @return bool Whether it succeeded + */ + public function exportFooter () + { + // implementation + return true; + } + + /** + * Outputs database header + * + * @param string $db Database name + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBHeader ($db, $db_alias = '') + { + // implementation + return true; + } + + /** + * Outputs database footer + * + * @param string $db Database name + * + * @return bool Whether it succeeded + */ + public function exportDBFooter ($db) + { + // implementation + return true; + } + + /** + * Outputs CREATE DATABASE statement + * + * @param string $db Database name + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + public function exportDBCreate($db, $db_alias = '') + { + // implementation + return true; + } + + /** + * Outputs the content of a table in [Name] format + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $sql_query SQL query for obtaining data + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportData( + $db, $table, $crlf, $error_url, $sql_query, $aliases = array() + ) { + // implementation; + return true; + } + + // optional - implement other methods defined in PhpMyAdmin\Plugins\ExportPlugin.class.php: + // - exportRoutines() + // - exportStructure() + // - getTableDefStandIn() + // - getTriggers() + + // optional - implement other private methods in order to avoid + // having huge methods or avoid duplicate code. Make use of them + // as well as of the getters and setters declared both here + // and in the PhpMyAdmin\Plugins\ExportPlugin class + + + // optional: + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + + /** + * Getter description + * + * @return type + */ + private function _getMyOptionalVariable() + { + return $this->_myOptionalVariable; + } + + /** + * Setter description + * + * @param type $my_optional_variable description + * + * @return void + */ + private function _setMyOptionalVariable($my_optional_variable) + { + $this->_myOptionalVariable = $my_optional_variable; + } + + /** + * Getter description + * + * @return type + */ + private function _getGlobalVariableName() + { + return $this->_globalVariableName; + } + + /** + * Setter description + * + * @param type $global_variable_name description + * + * @return void + */ + private function _setGlobalVariableName($global_variable_name) + { + $this->_globalVariableName = $global_variable_name; + } +} +?> diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/ExportPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/ExportPlugin.php new file mode 100644 index 0000000..a81a826 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/ExportPlugin.php @@ -0,0 +1,386 @@ +relation = new Relation($GLOBALS['dbi']); + $this->export = new Export($GLOBALS['dbi']); + $this->transformations = new Transformations(); + } + + /** + * Outputs export header + * + * @return bool Whether it succeeded + */ + abstract public function exportHeader(); + + /** + * Outputs export footer + * + * @return bool Whether it succeeded + */ + abstract public function exportFooter(); + + /** + * Outputs database header + * + * @param string $db Database name + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + abstract public function exportDBHeader($db, $db_alias = ''); + + /** + * Outputs database footer + * + * @param string $db Database name + * + * @return bool Whether it succeeded + */ + abstract public function exportDBFooter($db); + + /** + * Outputs CREATE DATABASE statement + * + * @param string $db Database name + * @param string $export_type 'server', 'database', 'table' + * @param string $db_alias Aliases of db + * + * @return bool Whether it succeeded + */ + abstract public function exportDBCreate($db, $export_type, $db_alias = ''); + + /** + * Outputs the content of a table + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $sql_query SQL query for obtaining data + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + abstract public function exportData( + $db, + $table, + $crlf, + $error_url, + $sql_query, + array $aliases = [] + ); + + /** + * The following methods are used in export.php or in db_operations.php, + * but they are not implemented by all export plugins + */ + + /** + * Exports routines (procedures and functions) + * + * @param string $db Database + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportRoutines($db, array $aliases = []) + { + } + + /** + * Exports events + * + * @param string $db Database + * + * @return bool Whether it succeeded + */ + public function exportEvents($db) + { + } + + /** + * Outputs table's structure + * + * @param string $db database name + * @param string $table table name + * @param string $crlf the end of line sequence + * @param string $error_url the url to go back in case of error + * @param string $export_mode 'create_table','triggers','create_view', + * 'stand_in' + * @param string $export_type 'server', 'database', 'table' + * @param bool $relation whether to include relation comments + * @param bool $comments whether to include the pmadb-style column comments + * as comments in the structure; this is deprecated + * but the parameter is left here because export.php + * calls exportStructure() also for other export + * types which use this parameter + * @param bool $mime whether to include mime comments + * @param bool $dates whether to include creation/update/check dates + * @param array $aliases Aliases of db/table/columns + * + * @return bool Whether it succeeded + */ + public function exportStructure( + $db, + $table, + $crlf, + $error_url, + $export_mode, + $export_type, + $relation = false, + $comments = false, + $mime = false, + $dates = false, + array $aliases = [] + ) { + } + + /** + * Exports metadata from Configuration Storage + * + * @param string $db database being exported + * @param string|array $tables table(s) being exported + * @param array $metadataTypes types of metadata to export + * + * @return bool Whether it succeeded + */ + public function exportMetadata( + $db, + $tables, + array $metadataTypes + ) { + } + + /** + * Returns a stand-in CREATE definition to resolve view dependencies + * + * @param string $db the database name + * @param string $view the view name + * @param string $crlf the end of line sequence + * @param array $aliases Aliases of db/table/columns + * + * @return string resulting definition + */ + public function getTableDefStandIn($db, $view, $crlf, $aliases = []) + { + } + + /** + * Outputs triggers + * + * @param string $db database name + * @param string $table table name + * + * @return string Formatted triggers list + */ + protected function getTriggers($db, $table) + { + } + + /** + * Initialize the specific variables for each export plugin + * + * @return void + */ + protected function initSpecificVariables() + { + } + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the export specific format plugin properties + * + * @return ExportPluginProperties + */ + public function getProperties() + { + return $this->properties; + } + + /** + * Sets the export plugins properties and is implemented by each export + * plugin + * + * @return void + */ + abstract protected function setProperties(); + + /** + * The following methods are implemented here so that they + * can be used by all export plugin without overriding it. + * Note: If you are creating a export plugin then don't include + * below methods unless you want to override them. + */ + + /** + * Initialize aliases + * + * @param array $aliases Alias information for db/table/column + * @param string $db the database + * @param string $table the table + * + * @return void + */ + public function initAlias($aliases, &$db, &$table = null) + { + if (! empty($aliases[$db]['tables'][$table]['alias'])) { + $table = $aliases[$db]['tables'][$table]['alias']; + } + if (! empty($aliases[$db]['alias'])) { + $db = $aliases[$db]['alias']; + } + } + + /** + * Search for alias of a identifier. + * + * @param array $aliases Alias information for db/table/column + * @param string $id the identifier to be searched + * @param string $type db/tbl/col or any combination of them + * representing what to be searched + * @param string $db the database in which search is to be done + * @param string $tbl the table in which search is to be done + * + * @return string alias of the identifier if found or '' + */ + public function getAlias(array $aliases, $id, $type = 'dbtblcol', $db = '', $tbl = '') + { + if (! empty($db) && isset($aliases[$db])) { + $aliases = [ + $db => $aliases[$db], + ]; + } + // search each database + foreach ($aliases as $db_key => $db) { + // check if id is database and has alias + if (false !== stripos($type, 'db') + && $db_key === $id + && ! empty($db['alias']) + ) { + return $db['alias']; + } + if (empty($db['tables'])) { + continue; + } + if (! empty($tbl) && isset($db['tables'][$tbl])) { + $db['tables'] = [ + $tbl => $db['tables'][$tbl], + ]; + } + // search each of its tables + foreach ($db['tables'] as $table_key => $table) { + // check if id is table and has alias + if (false !== stripos($type, 'tbl') + && $table_key === $id + && ! empty($table['alias']) + ) { + return $table['alias']; + } + if (empty($table['columns'])) { + continue; + } + // search each of its columns + foreach ($table['columns'] as $col_key => $col) { + // check if id is column + if (false !== stripos($type, 'col') + && $col_key === $id + && ! empty($col) + ) { + return $col; + } + } + } + } + + return ''; + } + + /** + * Gives the relation string and + * also substitutes with alias if required + * in this format: + * [Foreign Table] ([Foreign Field]) + * + * @param array $res_rel the foreigners array + * @param string $field_name the field name + * @param string $db the field name + * @param array $aliases Alias information for db/table/column + * + * @return string the Relation string + */ + public function getRelationString( + array $res_rel, + $field_name, + $db, + array $aliases = [] + ) { + $relation = ''; + $foreigner = $this->relation->searchColumnInForeigners($res_rel, $field_name); + if ($foreigner) { + $ftable = $foreigner['foreign_table']; + $ffield = $foreigner['foreign_field']; + if (! empty($aliases[$db]['tables'][$ftable]['columns'][$ffield])) { + $ffield = $aliases[$db]['tables'][$ftable]['columns'][$ffield]; + } + if (! empty($aliases[$db]['tables'][$ftable]['alias'])) { + $ftable = $aliases[$db]['tables'][$ftable]['alias']; + } + $relation = $ftable . ' (' . $ffield . ')'; + } + + return $relation; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/IOTransformationsPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/IOTransformationsPlugin.php new file mode 100644 index 0000000..3652eba --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/IOTransformationsPlugin.php @@ -0,0 +1,98 @@ +error; + } + + /** + * Returns the success status + * + * @return bool + */ + public function isSuccess() + { + return $this->success; + } + + /** + * Resets the object properties + * + * @return void + */ + public function reset() + { + $this->success = true; + $this->error = ''; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Import/AbstractImportCsv.php b/srcs/phpmyadmin/libraries/classes/Plugins/Import/AbstractImportCsv.php new file mode 100644 index 0000000..5c5a250 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Import/AbstractImportCsv.php @@ -0,0 +1,94 @@ +setOptionsText(__('Options')); + + // create the root group that will be the options field for + // $importPluginProperties + // this will be shown as "Format specific options" + $importSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // general options main group + $generalOptions = new OptionsPropertyMainGroup("general_opts"); + + // create common items and add them to the group + $leaf = new BoolPropertyItem( + "replace", + __( + 'Update data when duplicate keys found on import (add ON DUPLICATE ' + . 'KEY UPDATE)' + ) + ); + $generalOptions->addProperty($leaf); + $leaf = new TextPropertyItem( + "terminated", + __('Columns separated with:') + ); + $leaf->setSize(2); + $generalOptions->addProperty($leaf); + $leaf = new TextPropertyItem( + "enclosed", + __('Columns enclosed with:') + ); + $leaf->setSize(2); + $leaf->setLen(2); + $generalOptions->addProperty($leaf); + $leaf = new TextPropertyItem( + "escaped", + __('Columns escaped with:') + ); + $leaf->setSize(2); + $leaf->setLen(2); + $generalOptions->addProperty($leaf); + $leaf = new TextPropertyItem( + "new_line", + __('Lines terminated with:') + ); + $leaf->setSize(2); + $generalOptions->addProperty($leaf); + + // add the main group to the root group + $importSpecificOptions->addProperty($generalOptions); + + // set the options for the import plugin property item + $importPluginProperties->setOptions($importSpecificOptions); + $this->properties = $importPluginProperties; + + return $generalOptions; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportCsv.php b/srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportCsv.php new file mode 100644 index 0000000..e5494a9 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportCsv.php @@ -0,0 +1,818 @@ +setProperties(); + } + + /** + * Sets the import plugin properties. + * Called in the constructor. + * + * @return void + */ + protected function setProperties() + { + $this->_setAnalyze(false); + + if ($GLOBALS['plugin_param'] !== 'table') { + $this->_setAnalyze(true); + } + + $generalOptions = parent::setProperties(); + $this->properties->setText('CSV'); + $this->properties->setExtension('csv'); + + if ($GLOBALS['plugin_param'] !== 'table') { + $leaf = new TextPropertyItem( + "new_tbl_name", + __( + 'Name of the new table (optional):' + ) + ); + $generalOptions->addProperty($leaf); + + if ($GLOBALS['plugin_param'] === 'server') { + $leaf = new TextPropertyItem( + "new_db_name", + __( + 'Name of the new database (optional):' + ) + ); + $generalOptions->addProperty($leaf); + } + + $leaf = new NumberPropertyItem( + "partial_import", + __( + 'Import these many number of rows (optional):' + ) + ); + $generalOptions->addProperty($leaf); + + $leaf = new BoolPropertyItem( + "col_names", + __( + 'The first line of the file contains the table column names' + . ' (if this is unchecked, the first line will become part' + . ' of the data)' + ) + ); + $generalOptions->addProperty($leaf); + } else { + $leaf = new NumberPropertyItem( + "partial_import", + __( + 'Import these many number of rows (optional):' + ) + ); + $generalOptions->addProperty($leaf); + + $hint = new Message( + __( + 'If the data in each row of the file is not' + . ' in the same order as in the database, list the corresponding' + . ' column names here. Column names must be separated by commas' + . ' and not enclosed in quotations.' + ) + ); + $leaf = new TextPropertyItem( + "columns", + __('Column names:') . ' ' . Util::showHint($hint) + ); + $generalOptions->addProperty($leaf); + } + + $leaf = new BoolPropertyItem( + "ignore", + __('Do not abort on INSERT error') + ); + $generalOptions->addProperty($leaf); + } + + /** + * Handles the whole import logic + * + * @param array $sql_data 2-element array with sql data + * + * @return void + */ + public function doImport(array &$sql_data = []) + { + global $db, $table, $csv_terminated, $csv_enclosed, $csv_escaped, + $csv_new_line, $csv_columns, $err_url, $import_file_name; + // $csv_replace and $csv_ignore should have been here, + // but we use directly from $_POST + global $error, $timeout_passed, $finished, $message; + + $import_file_name = basename($import_file_name, ".csv"); + $import_file_name = mb_strtolower($import_file_name); + $import_file_name = preg_replace("/[^a-zA-Z0-9_]/", "_", $import_file_name); + + $replacements = [ + '\\n' => "\n", + '\\t' => "\t", + '\\r' => "\r", + ]; + $csv_terminated = strtr($csv_terminated, $replacements); + $csv_enclosed = strtr($csv_enclosed, $replacements); + $csv_escaped = strtr($csv_escaped, $replacements); + $csv_new_line = strtr($csv_new_line, $replacements); + + $param_error = false; + if (strlen($csv_terminated) === 0) { + $message = Message::error( + __('Invalid parameter for CSV import: %s') + ); + $message->addParam(__('Columns terminated with')); + $error = true; + $param_error = true; + // The default dialog of MS Excel when generating a CSV produces a + // semi-colon-separated file with no chance of specifying the + // enclosing character. Thus, users who want to import this file + // tend to remove the enclosing character on the Import dialog. + // I could not find a test case where having no enclosing characters + // confuses this script. + // But the parser won't work correctly with strings so we allow just + // one character. + } elseif (mb_strlen($csv_enclosed) > 1) { + $message = Message::error( + __('Invalid parameter for CSV import: %s') + ); + $message->addParam(__('Columns enclosed with')); + $error = true; + $param_error = true; + // I could not find a test case where having no escaping characters + // confuses this script. + // But the parser won't work correctly with strings so we allow just + // one character. + } elseif (mb_strlen($csv_escaped) > 1) { + $message = Message::error( + __('Invalid parameter for CSV import: %s') + ); + $message->addParam(__('Columns escaped with')); + $error = true; + $param_error = true; + } elseif (mb_strlen($csv_new_line) != 1 + && $csv_new_line != 'auto' + ) { + $message = Message::error( + __('Invalid parameter for CSV import: %s') + ); + $message->addParam(__('Lines terminated with')); + $error = true; + $param_error = true; + } + + // If there is an error in the parameters entered, + // indicate that immediately. + if ($param_error) { + Util::mysqlDie( + $message->getMessage(), + '', + false, + $err_url + ); + } + + $buffer = ''; + $required_fields = 0; + $sql_template = ''; + $fields = []; + if (! $this->_getAnalyze()) { + $sql_template = 'INSERT'; + if (isset($_POST['csv_ignore'])) { + $sql_template .= ' IGNORE'; + } + $sql_template .= ' INTO ' . Util::backquote($table); + + $tmp_fields = $GLOBALS['dbi']->getColumns($db, $table); + + if (empty($csv_columns)) { + $fields = $tmp_fields; + } else { + $sql_template .= ' ('; + $fields = []; + $tmp = preg_split('/,( ?)/', $csv_columns); + foreach ($tmp as $key => $val) { + if (count($fields) > 0) { + $sql_template .= ', '; + } + /* Trim also `, if user already included backquoted fields */ + $val = trim($val, " \t\r\n\0\x0B`"); + $found = false; + foreach ($tmp_fields as $field) { + if ($field['Field'] == $val) { + $found = true; + break; + } + } + if (! $found) { + $message = Message::error( + __( + 'Invalid column (%s) specified! Ensure that columns' + . ' names are spelled correctly, separated by commas' + . ', and not enclosed in quotes.' + ) + ); + $message->addParam($val); + $error = true; + break; + } + if (isset($field)) { + $fields[] = $field; + } + $sql_template .= Util::backquote($val); + } + $sql_template .= ') '; + } + + $required_fields = count($fields); + + $sql_template .= ' VALUES ('; + } + + // Defaults for parser + $i = 0; + $len = 0; + $lastlen = null; + $line = 1; + $lasti = -1; + $values = []; + $csv_finish = false; + $max_lines = 0; // defaults to 0 (get all the lines) + + // If we get a negative value, probably someone changed min value attribute in DOM or there is an integer overflow, whatever be the case, get all the lines + if (isset($_REQUEST['csv_partial_import']) && $_REQUEST['csv_partial_import'] > 0) { + $max_lines = $_REQUEST['csv_partial_import']; + } + $max_lines_constraint = $max_lines+1; + // if the first row has to be counted as column names, include one more row in the max lines + if (isset($_REQUEST['csv_col_names'])) { + $max_lines_constraint++; + } + + $tempRow = []; + $rows = []; + $col_names = []; + $tables = []; + + $col_count = 0; + $max_cols = 0; + $csv_terminated_len = mb_strlen($csv_terminated); + while (! ($finished && $i >= $len) && ! $error && ! $timeout_passed) { + $data = $this->import->getNextChunk(); + if ($data === false) { + // subtract data we didn't handle yet and stop processing + $GLOBALS['offset'] -= strlen($buffer); + break; + } elseif ($data !== true) { + // Append new data to buffer + $buffer .= $data; + unset($data); + + // Force a trailing new line at EOF to prevent parsing problems + if ($finished && $buffer) { + $finalch = mb_substr($buffer, -1); + if ($csv_new_line == 'auto' + && $finalch != "\r" + && $finalch != "\n" + ) { + $buffer .= "\n"; + } elseif ($csv_new_line != 'auto' + && $finalch != $csv_new_line + ) { + $buffer .= $csv_new_line; + } + } + + // Do not parse string when we're not at the end + // and don't have new line inside + if (($csv_new_line == 'auto' + && mb_strpos($buffer, "\r") === false + && mb_strpos($buffer, "\n") === false) + || ($csv_new_line != 'auto' + && mb_strpos($buffer, $csv_new_line) === false) + ) { + continue; + } + } + + // Current length of our buffer + $len = mb_strlen($buffer); + // Currently parsed char + + $ch = mb_substr($buffer, $i, 1); + if ($csv_terminated_len > 1 && $ch == $csv_terminated[0]) { + $ch = $this->readCsvTerminatedString( + $buffer, + $ch, + $i, + $csv_terminated_len + ); + $i += $csv_terminated_len - 1; + } + while ($i < $len) { + // Deadlock protection + if ($lasti == $i && $lastlen == $len) { + $message = Message::error( + __('Invalid format of CSV input on line %d.') + ); + $message->addParam($line); + $error = true; + break; + } + $lasti = $i; + $lastlen = $len; + + // This can happen with auto EOL and \r at the end of buffer + if (! $csv_finish) { + // Grab empty field + if ($ch == $csv_terminated) { + if ($i == $len - 1) { + break; + } + $values[] = ''; + $i++; + $ch = mb_substr($buffer, $i, 1); + if ($csv_terminated_len > 1 && $ch == $csv_terminated[0]) { + $ch = $this->readCsvTerminatedString( + $buffer, + $ch, + $i, + $csv_terminated_len + ); + $i += $csv_terminated_len - 1; + } + continue; + } + + // Grab one field + $fallbacki = $i; + if ($ch == $csv_enclosed) { + if ($i == $len - 1) { + break; + } + $need_end = true; + $i++; + $ch = mb_substr($buffer, $i, 1); + if ($csv_terminated_len > 1 && $ch == $csv_terminated[0]) { + $ch = $this->readCsvTerminatedString( + $buffer, + $ch, + $i, + $csv_terminated_len + ); + $i += $csv_terminated_len - 1; + } + } else { + $need_end = false; + } + $fail = false; + $value = ''; + while (($need_end + && ($ch != $csv_enclosed + || $csv_enclosed == $csv_escaped)) + || (! $need_end + && ! ($ch == $csv_terminated + || $ch == $csv_new_line + || ($csv_new_line == 'auto' + && ($ch == "\r" || $ch == "\n")))) + ) { + if ($ch == $csv_escaped) { + if ($i == $len - 1) { + $fail = true; + break; + } + $i++; + $ch = mb_substr($buffer, $i, 1); + if ($csv_terminated_len > 1 + && $ch == $csv_terminated[0] + ) { + $ch = $this->readCsvTerminatedString( + $buffer, + $ch, + $i, + $csv_terminated_len + ); + $i += $csv_terminated_len - 1; + } + if ($csv_enclosed == $csv_escaped + && ($ch == $csv_terminated + || $ch == $csv_new_line + || ($csv_new_line == 'auto' + && ($ch == "\r" || $ch == "\n"))) + ) { + break; + } + } + $value .= $ch; + if ($i == $len - 1) { + if (! $finished) { + $fail = true; + } + break; + } + $i++; + $ch = mb_substr($buffer, $i, 1); + if ($csv_terminated_len > 1 && $ch == $csv_terminated[0]) { + $ch = $this->readCsvTerminatedString( + $buffer, + $ch, + $i, + $csv_terminated_len + ); + $i += $csv_terminated_len - 1; + } + } + + // unquoted NULL string + if (false === $need_end && $value === 'NULL') { + $value = null; + } + + if ($fail) { + $i = $fallbacki; + $ch = mb_substr($buffer, $i, 1); + if ($csv_terminated_len > 1 && $ch == $csv_terminated[0]) { + $i += $csv_terminated_len - 1; + } + break; + } + // Need to strip trailing enclosing char? + if ($need_end && $ch == $csv_enclosed) { + if ($finished && $i == $len - 1) { + $ch = null; + } elseif ($i == $len - 1) { + $i = $fallbacki; + $ch = mb_substr($buffer, $i, 1); + if ($csv_terminated_len > 1 + && $ch == $csv_terminated[0] + ) { + $i += $csv_terminated_len - 1; + } + break; + } else { + $i++; + $ch = mb_substr($buffer, $i, 1); + if ($csv_terminated_len > 1 + && $ch == $csv_terminated[0] + ) { + $ch = $this->readCsvTerminatedString( + $buffer, + $ch, + $i, + $csv_terminated_len + ); + $i += $csv_terminated_len - 1; + } + } + } + // Are we at the end? + if ($ch == $csv_new_line + || ($csv_new_line == 'auto' && ($ch == "\r" || $ch == "\n")) + || ($finished && $i == $len - 1) + ) { + $csv_finish = true; + } + // Go to next char + if ($ch == $csv_terminated) { + if ($i == $len - 1) { + $i = $fallbacki; + $ch = mb_substr($buffer, $i, 1); + if ($csv_terminated_len > 1 + && $ch == $csv_terminated[0] + ) { + $i += $csv_terminated_len - 1; + } + break; + } + $i++; + $ch = mb_substr($buffer, $i, 1); + if ($csv_terminated_len > 1 + && $ch == $csv_terminated[0] + ) { + $ch = $this->readCsvTerminatedString( + $buffer, + $ch, + $i, + $csv_terminated_len + ); + $i += $csv_terminated_len - 1; + } + } + // If everything went okay, store value + $values[] = $value; + } + + // End of line + if ($csv_finish + || $ch == $csv_new_line + || ($csv_new_line == 'auto' && ($ch == "\r" || $ch == "\n")) + ) { + if ($csv_new_line == 'auto' && $ch == "\r") { // Handle "\r\n" + if ($i >= ($len - 2) && ! $finished) { + break; // We need more data to decide new line + } + if (mb_substr($buffer, $i + 1, 1) == "\n") { + $i++; + } + } + // We didn't parse value till the end of line, so there was + // empty one + if (! $csv_finish) { + $values[] = ''; + } + + if ($this->_getAnalyze()) { + foreach ($values as $val) { + $tempRow[] = $val; + ++$col_count; + } + + if ($col_count > $max_cols) { + $max_cols = $col_count; + } + $col_count = 0; + + $rows[] = $tempRow; + $tempRow = []; + } else { + // Do we have correct count of values? + if (count($values) != $required_fields) { + // Hack for excel + if ($values[count($values) - 1] == ';') { + unset($values[count($values) - 1]); + } else { + $message = Message::error( + __( + 'Invalid column count in CSV input' + . ' on line %d.' + ) + ); + $message->addParam($line); + $error = true; + break; + } + } + + $first = true; + $sql = $sql_template; + foreach ($values as $key => $val) { + if (! $first) { + $sql .= ', '; + } + if ($val === null) { + $sql .= 'NULL'; + } else { + $sql .= '\'' + . $GLOBALS['dbi']->escapeString($val) + . '\''; + } + + $first = false; + } + $sql .= ')'; + if (isset($_POST['csv_replace'])) { + $sql .= " ON DUPLICATE KEY UPDATE "; + foreach ($fields as $field) { + $fieldName = Util::backquote( + $field['Field'] + ); + $sql .= $fieldName . " = VALUES(" . $fieldName + . "), "; + } + $sql = rtrim($sql, ', '); + } + + /** + * @todo maybe we could add original line to verbose + * SQL in comment + */ + $this->import->runQuery($sql, $sql, $sql_data); + } + + $line++; + $csv_finish = false; + $values = []; + $buffer = mb_substr($buffer, $i + 1); + $len = mb_strlen($buffer); + $i = 0; + $lasti = -1; + $ch = mb_substr($buffer, 0, 1); + if ($max_lines > 0 && $line == $max_lines_constraint) { + $finished = 1; + break; + } + } + } // End of parser loop + if ($max_lines > 0 && $line == $max_lines_constraint) { + $finished = 1; + break; + } + } // End of import loop + + if ($this->_getAnalyze()) { + /* Fill out all rows */ + $num_rows = count($rows); + for ($i = 0; $i < $num_rows; ++$i) { + for ($j = count($rows[$i]); $j < $max_cols; ++$j) { + $rows[$i][] = 'NULL'; + } + } + + if (isset($_REQUEST['csv_col_names'])) { + $col_names = array_splice($rows, 0, 1); + $col_names = $col_names[0]; + // MySQL column names can't end with a space character. + foreach ($col_names as $key => $col_name) { + $col_names[$key] = rtrim($col_name); + } + } + + if ((isset($col_names) && count($col_names) != $max_cols) + || ! isset($col_names) + ) { + // Fill out column names + for ($i = 0; $i < $max_cols; ++$i) { + $col_names[] = 'COL ' . ($i + 1); + } + } + + // get new table name, if user didn't provide one, set the default name + if (isset($_REQUEST['csv_new_tbl_name']) + && strlen($_REQUEST['csv_new_tbl_name']) > 0 + ) { + $tbl_name = $_REQUEST['csv_new_tbl_name']; + } elseif (mb_strlen((string) $db)) { + $result = $GLOBALS['dbi']->fetchResult('SHOW TABLES'); + + // logic to get table name from filename + // if no table then use filename as table name + if (count($result) === 0) { + $tbl_name = $import_file_name; + } else { + // check to see if {filename} as table exist + $name_array = preg_grep("/{$import_file_name}/isU", $result); + // if no use filename as table name + if (count($name_array) === 0) { + $tbl_name = $import_file_name; + } else { + // check if {filename}_ as table exist + $name_array = preg_grep("/{$import_file_name}_/isU", $result); + $tbl_name = $import_file_name . "_" . (count($name_array) + 1); + } + } + } else { + $tbl_name = $import_file_name; + } + + $tables[] = [ + $tbl_name, + $col_names, + $rows, + ]; + + /* Obtain the best-fit MySQL types for each column */ + $analyses = []; + $analyses[] = $this->import->analyzeTable($tables[0]); + + /** + * string $db_name (no backquotes) + * + * array $table = array(table_name, array() column_names, array()() rows) + * array $tables = array of "$table"s + * + * array $analysis = array(array() column_types, array() column_sizes) + * array $analyses = array of "$analysis"s + * + * array $create = array of SQL strings + * + * array $options = an associative array of options + */ + + /* Set database name to the currently selected one, if applicable, + * Otherwise, check if user provided the database name in the request, + * if not, set the default name + */ + if (isset($_REQUEST['csv_new_db_name']) + && strlen($_REQUEST['csv_new_db_name']) > 0 + ) { + $newDb = $_REQUEST['csv_new_db_name']; + } else { + $result = $GLOBALS['dbi']->fetchResult('SHOW DATABASES'); + if (! is_array($result)) { + $result = []; + } + $newDb = 'CSV_DB ' . (count($result) + 1); + } + list($db_name, $options) = $this->getDbnameAndOptions($db, $newDb); + + /* Non-applicable parameters */ + $create = null; + + /* Created and execute necessary SQL statements from data */ + $this->import->buildSql($db_name, $tables, $analyses, $create, $options, $sql_data); + + unset($tables); + unset($analyses); + } + + // Commit any possible data in buffers + $this->import->runQuery('', '', $sql_data); + + if (count($values) != 0 && ! $error) { + $message = Message::error( + __('Invalid format of CSV input on line %d.') + ); + $message->addParam($line); + $error = true; + } + } + + /** + * Read the expected column_separated_with String of length + * $csv_terminated_len from the $buffer + * into variable $ch and return the read string $ch + * + * @param string $buffer The original string buffer read from + * csv file + * @param string $ch Partially read "column Separated with" + * string, also used to return after + * reading length equal $csv_terminated_len + * @param int $i Current read counter of buffer string + * @param int $csv_terminated_len The length of "column separated with" + * String + * + * @return string + */ + public function readCsvTerminatedString($buffer, $ch, $i, $csv_terminated_len) + { + for ($j = 0; $j < $csv_terminated_len - 1; $j++) { + $i++; + $ch .= mb_substr($buffer, $i, 1); + } + + return $ch; + } + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Returns true if the table should be analyzed, false otherwise + * + * @return bool + */ + private function _getAnalyze() + { + return $this->_analyze; + } + + /** + * Sets to true if the table should be analyzed, false otherwise + * + * @param bool $analyze status + * + * @return void + */ + private function _setAnalyze($analyze) + { + $this->_analyze = $analyze; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportLdi.php b/srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportLdi.php new file mode 100644 index 0000000..91260c5 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportLdi.php @@ -0,0 +1,176 @@ +setProperties(); + } + + /** + * Sets the import plugin properties. + * Called in the constructor. + * + * @return void + */ + protected function setProperties() + { + if ($GLOBALS['cfg']['Import']['ldi_local_option'] == 'auto') { + $GLOBALS['cfg']['Import']['ldi_local_option'] = false; + + $result = $GLOBALS['dbi']->tryQuery( + 'SELECT @@local_infile;' + ); + if ($result != false && $GLOBALS['dbi']->numRows($result) > 0) { + $tmp = $GLOBALS['dbi']->fetchRow($result); + if ($tmp[0] == 'ON') { + $GLOBALS['cfg']['Import']['ldi_local_option'] = true; + } + } + $GLOBALS['dbi']->freeResult($result); + unset($result); + } + + $generalOptions = parent::setProperties(); + $this->properties->setText('CSV using LOAD DATA'); + $this->properties->setExtension('ldi'); + + $leaf = new TextPropertyItem( + "columns", + __('Column names: ') + ); + $generalOptions->addProperty($leaf); + + $leaf = new BoolPropertyItem( + "ignore", + __('Do not abort on INSERT error') + ); + $generalOptions->addProperty($leaf); + + $leaf = new BoolPropertyItem( + "local_option", + __('Use LOCAL keyword') + ); + $generalOptions->addProperty($leaf); + } + + /** + * Handles the whole import logic + * + * @param array $sql_data 2-element array with sql data + * + * @return void + */ + public function doImport(array &$sql_data = []) + { + global $finished, $import_file, $charset_conversion, $table; + global $ldi_local_option, $ldi_replace, $ldi_ignore, $ldi_terminated, + $ldi_enclosed, $ldi_escaped, $ldi_new_line, $skip_queries, $ldi_columns; + + $compression = $GLOBALS['import_handle']->getCompression(); + + if ($import_file == 'none' + || $compression != 'none' + || $charset_conversion + ) { + // We handle only some kind of data! + $GLOBALS['message'] = Message::error( + __('This plugin does not support compressed imports!') + ); + $GLOBALS['error'] = true; + + return; + } + + $sql = 'LOAD DATA'; + if (isset($ldi_local_option)) { + $sql .= ' LOCAL'; + } + $sql .= ' INFILE \'' . $GLOBALS['dbi']->escapeString($import_file) + . '\''; + if (isset($ldi_replace)) { + $sql .= ' REPLACE'; + } elseif (isset($ldi_ignore)) { + $sql .= ' IGNORE'; + } + $sql .= ' INTO TABLE ' . Util::backquote($table); + + if (strlen((string) $ldi_terminated) > 0) { + $sql .= ' FIELDS TERMINATED BY \'' . $ldi_terminated . '\''; + } + if (strlen((string) $ldi_enclosed) > 0) { + $sql .= ' ENCLOSED BY \'' + . $GLOBALS['dbi']->escapeString($ldi_enclosed) . '\''; + } + if (strlen((string) $ldi_escaped) > 0) { + $sql .= ' ESCAPED BY \'' + . $GLOBALS['dbi']->escapeString($ldi_escaped) . '\''; + } + if (strlen((string) $ldi_new_line) > 0) { + if ($ldi_new_line == 'auto') { + $ldi_new_line + = (PHP_EOL == "\n") + ? '\n' + : '\r\n'; + } + $sql .= ' LINES TERMINATED BY \'' . $ldi_new_line . '\''; + } + if ($skip_queries > 0) { + $sql .= ' IGNORE ' . $skip_queries . ' LINES'; + $skip_queries = 0; + } + if (strlen((string) $ldi_columns) > 0) { + $sql .= ' ('; + $tmp = preg_split('/,( ?)/', $ldi_columns); + $cnt_tmp = count($tmp); + for ($i = 0; $i < $cnt_tmp; $i++) { + if ($i > 0) { + $sql .= ', '; + } + /* Trim also `, if user already included backquoted fields */ + $sql .= Util::backquote( + trim($tmp[$i], " \t\r\n\0\x0B`") + ); + } // end for + $sql .= ')'; + } + + $this->import->runQuery($sql, $sql, $sql_data); + $this->import->runQuery('', '', $sql_data); + $finished = true; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportMediawiki.php b/srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportMediawiki.php new file mode 100644 index 0000000..9723daf --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportMediawiki.php @@ -0,0 +1,604 @@ +setProperties(); + } + + /** + * Sets the import plugin properties. + * Called in the constructor. + * + * @return void + */ + protected function setProperties() + { + $this->_setAnalyze(false); + if ($GLOBALS['plugin_param'] !== 'table') { + $this->_setAnalyze(true); + } + + $importPluginProperties = new ImportPluginProperties(); + $importPluginProperties->setText(__('MediaWiki Table')); + $importPluginProperties->setExtension('txt'); + $importPluginProperties->setMimeType('text/plain'); + $importPluginProperties->setOptions([]); + $importPluginProperties->setOptionsText(__('Options')); + + $this->properties = $importPluginProperties; + } + + /** + * Handles the whole import logic + * + * @param array $sql_data 2-element array with sql data + * + * @return void + */ + public function doImport(array &$sql_data = []) + { + global $error, $timeout_passed, $finished; + + // Defaults for parser + + // The buffer that will be used to store chunks read from the imported file + $buffer = ''; + + // Used as storage for the last part of the current chunk data + // Will be appended to the first line of the next chunk, if there is one + $last_chunk_line = ''; + + // Remembers whether the current buffer line is part of a comment + $inside_comment = false; + // Remembers whether the current buffer line is part of a data comment + $inside_data_comment = false; + // Remembers whether the current buffer line is part of a structure comment + $inside_structure_comment = false; + + // MediaWiki only accepts "\n" as row terminator + $mediawiki_new_line = "\n"; + + // Initialize the name of the current table + $cur_table_name = ""; + + while (! $finished && ! $error && ! $timeout_passed) { + $data = $this->import->getNextChunk(); + + if ($data === false) { + // Subtract data we didn't handle yet and stop processing + $GLOBALS['offset'] -= mb_strlen($buffer); + break; + } elseif ($data !== true) { + // Append new data to buffer + $buffer = $data; + unset($data); + // Don't parse string if we're not at the end + // and don't have a new line inside + if (mb_strpos($buffer, $mediawiki_new_line) === false) { + continue; + } + } + + // Because of reading chunk by chunk, the first line from the buffer + // contains only a portion of an actual line from the imported file. + // Therefore, we have to append it to the last line from the previous + // chunk. If we are at the first chunk, $last_chunk_line should be empty. + $buffer = $last_chunk_line . $buffer; + + // Process the buffer line by line + $buffer_lines = explode($mediawiki_new_line, $buffer); + + $full_buffer_lines_count = count($buffer_lines); + // If the reading is not finalised, the final line of the current chunk + // will not be complete + if (! $finished) { + $last_chunk_line = $buffer_lines[--$full_buffer_lines_count]; + } + + for ($line_nr = 0; $line_nr < $full_buffer_lines_count; ++$line_nr) { + $cur_buffer_line = trim($buffer_lines[$line_nr]); + + // If the line is empty, go to the next one + if ($cur_buffer_line === '') { + continue; + } + + $first_character = $cur_buffer_line[0]; + $matches = []; + + // Check beginning of comment + if (! strcmp(mb_substr($cur_buffer_line, 0, 4), "") + ) { + // Only data comments are closed. The structure comments + // will be closed when a data comment begins (in order to + // skip structure tables) + if ($inside_data_comment) { + $inside_data_comment = false; + } + + // End comments that are not related to table structure + if (! $inside_structure_comment) { + $inside_comment = false; + } + } else { + // Check table name + $match_table_name = []; + if (preg_match( + "/^Table data for `(.*)`$/", + $cur_buffer_line, + $match_table_name + ) + ) { + $cur_table_name = $match_table_name[1]; + $inside_data_comment = true; + + $inside_structure_comment + = $this->_mngInsideStructComm( + $inside_structure_comment + ); + } elseif (preg_match( + "/^Table structure for `(.*)`$/", + $cur_buffer_line, + $match_table_name + ) + ) { + // The structure comments will be ignored + $inside_structure_comment = true; + } + } + continue; + } elseif (preg_match('/^\{\|(.*)$/', $cur_buffer_line, $matches)) { + // Check start of table + + // This will store all the column info on all rows from + // the current table read from the buffer + $cur_temp_table = []; + + // Will be used as storage for the current row in the buffer + // Once all its columns are read, it will be added to + // $cur_temp_table and then it will be emptied + $cur_temp_line = []; + + // Helps us differentiate the header columns + // from the normal columns + $in_table_header = false; + // End processing because the current line does not + // contain any column information + } elseif (mb_substr($cur_buffer_line, 0, 2) === '|-' + || mb_substr($cur_buffer_line, 0, 2) === '|+' + || mb_substr($cur_buffer_line, 0, 2) === '|}' + ) { + // Check begin row or end table + + // Add current line to the values storage + if (! empty($cur_temp_line)) { + // If the current line contains header cells + // ( marked with '!' ), + // it will be marked as table header + if ($in_table_header) { + // Set the header columns + $cur_temp_table_headers = $cur_temp_line; + } else { + // Normal line, add it to the table + $cur_temp_table[] = $cur_temp_line; + } + } + + // Empty the temporary buffer + $cur_temp_line = []; + + // No more processing required at the end of the table + if (mb_substr($cur_buffer_line, 0, 2) === '|}') { + $current_table = [ + $cur_table_name, + $cur_temp_table_headers, + $cur_temp_table, + ]; + + // Import the current table data into the database + $this->_importDataOneTable($current_table, $sql_data); + + // Reset table name + $cur_table_name = ""; + } + // What's after the row tag is now only attributes + } elseif (($first_character === '|') || ($first_character === '!')) { + // Check cell elements + + // Header cells + if ($first_character === '!') { + // Mark as table header, but treat as normal row + $cur_buffer_line = str_replace('!!', '||', $cur_buffer_line); + // Will be used to set $cur_temp_line as table header + $in_table_header = true; + } else { + $in_table_header = false; + } + + // Loop through each table cell + $cells = $this->_explodeMarkup($cur_buffer_line); + foreach ($cells as $cell) { + $cell = $this->_getCellData($cell); + + // Delete the beginning of the column, if there is one + $cell = trim($cell); + $col_start_chars = [ + "|", + "!", + ]; + foreach ($col_start_chars as $col_start_char) { + $cell = $this->_getCellContent($cell, $col_start_char); + } + + // Add the cell to the row + $cur_temp_line[] = $cell; + } // foreach $cells + } else { + // If it's none of the above, then the current line has a bad + // format + $message = Message::error( + __('Invalid format of mediawiki input on line:
%s.') + ); + $message->addParam($cur_buffer_line); + $error = true; + } + } // End treating full buffer lines + } // while - finished parsing buffer + } + + /** + * Imports data from a single table + * + * @param array $table containing all table info: + * $table[0] - string + * containing table name + * $table[1] - array[] of + * table headers $table[2] - + * array[][] of table content + * rows + * + * @param array $sql_data 2-element array with sql data + * + * @global bool $analyze whether to scan for column types + * + * @return void + */ + private function _importDataOneTable(array $table, array &$sql_data) + { + $analyze = $this->_getAnalyze(); + if ($analyze) { + // Set the table name + $this->_setTableName($table[0]); + + // Set generic names for table headers if they don't exist + $this->_setTableHeaders($table[1], $table[2][0]); + + // Create the tables array to be used in Import::buildSql() + $tables = []; + $tables[] = [ + $table[0], + $table[1], + $table[2], + ]; + + // Obtain the best-fit MySQL types for each column + $analyses = []; + $analyses[] = $this->import->analyzeTable($tables[0]); + + $this->_executeImportTables($tables, $analyses, $sql_data); + } + + // Commit any possible data in buffers + $this->import->runQuery('', '', $sql_data); + } + + /** + * Sets the table name + * + * @param string $table_name reference to the name of the table + * + * @return void + */ + private function _setTableName(&$table_name) + { + if (empty($table_name)) { + $result = $GLOBALS['dbi']->fetchResult('SHOW TABLES'); + // todo check if the name below already exists + $table_name = 'TABLE ' . (count($result) + 1); + } + } + + /** + * Set generic names for table headers, if they don't exist + * + * @param array $table_headers reference to the array containing the headers + * of a table + * @param array $table_row array containing the first content row + * + * @return void + */ + private function _setTableHeaders(array &$table_headers, array $table_row) + { + if (empty($table_headers)) { + // The first table row should contain the number of columns + // If they are not set, generic names will be given (COL 1, COL 2, etc) + $num_cols = count($table_row); + for ($i = 0; $i < $num_cols; ++$i) { + $table_headers[$i] = 'COL ' . ($i + 1); + } + } + } + + /** + * Sets the database name and additional options and calls Import::buildSql() + * Used in PMA_importDataAllTables() and $this->_importDataOneTable() + * + * @param array $tables structure: + * array( + * array(table_name, array() column_names, array()() + * rows) + * ) + * @param array $analyses structure: + * $analyses = array( + * array(array() column_types, array() column_sizes) + * ) + * @param array $sql_data 2-element array with sql data + * + * @global string $db name of the database to import in + * + * @return void + */ + private function _executeImportTables(array &$tables, array &$analyses, array &$sql_data) + { + global $db; + + // $db_name : The currently selected database name, if applicable + // No backquotes + // $options : An associative array of options + list($db_name, $options) = $this->getDbnameAndOptions($db, 'mediawiki_DB'); + + // Array of SQL strings + // Non-applicable parameters + $create = null; + + // Create and execute necessary SQL statements from data + $this->import->buildSql($db_name, $tables, $analyses, $create, $options, $sql_data); + } + + /** + * Replaces all instances of the '||' separator between delimiters + * in a given string + * + * @param string $replace the string to be replaced with + * @param string $subject the text to be replaced + * + * @return string with replacements + */ + private function _delimiterReplace($replace, $subject) + { + // String that will be returned + $cleaned = ""; + // Possible states of current character + $inside_tag = false; + $inside_attribute = false; + // Attributes can be declared with either " or ' + $start_attribute_character = false; + + // The full separator is "||"; + // This remembers if the previous character was '|' + $partial_separator = false; + + // Parse text char by char + for ($i = 0, $iMax = strlen($subject); $i < $iMax; $i++) { + $cur_char = $subject[$i]; + // Check for separators + if ($cur_char == '|') { + // If we're not inside a tag, then this is part of a real separator, + // so we append it to the current segment + if (! $inside_attribute) { + $cleaned .= $cur_char; + if ($partial_separator) { + $inside_tag = false; + $inside_attribute = false; + } + } elseif ($partial_separator) { + // If we are inside a tag, we replace the current char with + // the placeholder and append that to the current segment + $cleaned .= $replace; + } + + // If the previous character was also '|', then this ends a + // full separator. If not, this may be the beginning of one + $partial_separator = ! $partial_separator; + } else { + // If we're inside a tag attribute and the current character is + // not '|', but the previous one was, it means that the single '|' + // was not appended, so we append it now + if ($partial_separator && $inside_attribute) { + $cleaned .= "|"; + } + // If the char is different from "|", no separator can be formed + $partial_separator = false; + + // any other character should be appended to the current segment + $cleaned .= $cur_char; + + if ($cur_char == '<' && ! $inside_attribute) { + // start of a tag + $inside_tag = true; + } elseif ($cur_char == '>' && ! $inside_attribute) { + // end of a tag + $inside_tag = false; + } elseif (($cur_char == '"' || $cur_char == "'") && $inside_tag) { + // start or end of an attribute + if (! $inside_attribute) { + $inside_attribute = true; + // remember the attribute`s declaration character (" or ') + $start_attribute_character = $cur_char; + } else { + if ($cur_char == $start_attribute_character) { + $inside_attribute = false; + // unset attribute declaration character + $start_attribute_character = false; + } + } + } + } + } // end for each character in $subject + + return $cleaned; + } + + /** + * Separates a string into items, similarly to explode + * Uses the '||' separator (which is standard in the mediawiki format) + * and ignores any instances of it inside markup tags + * Used in parsing buffer lines containing data cells + * + * @param string $text text to be split + * + * @return array + */ + private function _explodeMarkup($text) + { + $separator = "||"; + $placeholder = "\x00"; + + // Remove placeholder instances + $text = str_replace($placeholder, '', $text); + + // Replace instances of the separator inside HTML-like + // tags with the placeholder + $cleaned = $this->_delimiterReplace($placeholder, $text); + // Explode, then put the replaced separators back in + $items = explode($separator, $cleaned); + foreach ($items as $i => $str) { + $items[$i] = str_replace($placeholder, $separator, $str); + } + + return $items; + } + + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Returns true if the table should be analyzed, false otherwise + * + * @return bool + */ + private function _getAnalyze() + { + return $this->_analyze; + } + + /** + * Sets to true if the table should be analyzed, false otherwise + * + * @param bool $analyze status + * + * @return void + */ + private function _setAnalyze($analyze) + { + $this->_analyze = $analyze; + } + + /** + * Get cell + * + * @param string $cell Cell + * + * @return mixed + */ + private function _getCellData($cell) + { + // A cell could contain both parameters and data + $cell_data = explode('|', $cell, 2); + + // A '|' inside an invalid link should not + // be mistaken as delimiting cell parameters + if (mb_strpos($cell_data[0], '[[') === false) { + return $cell; + } + + if (count($cell_data) === 1) { + return $cell_data[0]; + } + + return $cell_data[1]; + } + + /** + * Manage $inside_structure_comment + * + * @param boolean $inside_structure_comment Value to test + * + * @return bool + */ + private function _mngInsideStructComm($inside_structure_comment) + { + // End ignoring structure rows + if ($inside_structure_comment) { + $inside_structure_comment = false; + } + + return $inside_structure_comment; + } + + /** + * Get cell content + * + * @param string $cell Cell + * @param string $col_start_char Start char + * + * @return string + */ + private function _getCellContent($cell, $col_start_char) + { + if (mb_strpos($cell, $col_start_char) === 0) { + $cell = trim(mb_substr($cell, 1)); + } + + return $cell; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportOds.php b/srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportOds.php new file mode 100644 index 0000000..8e68692 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportOds.php @@ -0,0 +1,427 @@ +setProperties(); + } + + /** + * Sets the import plugin properties. + * Called in the constructor. + * + * @return void + */ + protected function setProperties() + { + $importPluginProperties = new ImportPluginProperties(); + $importPluginProperties->setText('OpenDocument Spreadsheet'); + $importPluginProperties->setExtension('ods'); + $importPluginProperties->setOptionsText(__('Options')); + + // create the root group that will be the options field for + // $importPluginProperties + // this will be shown as "Format specific options" + $importSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // general options main group + $generalOptions = new OptionsPropertyMainGroup("general_opts"); + // create primary items and add them to the group + $leaf = new BoolPropertyItem( + "col_names", + __( + 'The first line of the file contains the table column names' + . ' (if this is unchecked, the first line will become part' + . ' of the data)' + ) + ); + $generalOptions->addProperty($leaf); + $leaf = new BoolPropertyItem( + "empty_rows", + __('Do not import empty rows') + ); + $generalOptions->addProperty($leaf); + $leaf = new BoolPropertyItem( + "recognize_percentages", + __( + 'Import percentages as proper decimals (ex. 12.00% to .12)' + ) + ); + $generalOptions->addProperty($leaf); + $leaf = new BoolPropertyItem( + "recognize_currency", + __('Import currencies (ex. $5.00 to 5.00)') + ); + $generalOptions->addProperty($leaf); + + // add the main group to the root group + $importSpecificOptions->addProperty($generalOptions); + + // set the options for the import plugin property item + $importPluginProperties->setOptions($importSpecificOptions); + $this->properties = $importPluginProperties; + } + + /** + * Handles the whole import logic + * + * @param array $sql_data 2-element array with sql data + * + * @return void + */ + public function doImport(array &$sql_data = []) + { + global $db, $error, $timeout_passed, $finished; + + $i = 0; + $len = 0; + $buffer = ""; + + /** + * Read in the file via Import::getNextChunk so that + * it can process compressed files + */ + while (! ($finished && $i >= $len) && ! $error && ! $timeout_passed) { + $data = $this->import->getNextChunk(); + if ($data === false) { + /* subtract data we didn't handle yet and stop processing */ + $GLOBALS['offset'] -= strlen($buffer); + break; + } elseif ($data !== true) { + /* Append new data to buffer */ + $buffer .= $data; + unset($data); + } + } + + unset($data); + + /** + * Disable loading of external XML entities. + */ + libxml_disable_entity_loader(); + + /** + * Load the XML string + * + * The option LIBXML_COMPACT is specified because it can + * result in increased performance without the need to + * alter the code in any way. It's basically a freebee. + */ + $xml = @simplexml_load_string($buffer, "SimpleXMLElement", LIBXML_COMPACT); + + unset($buffer); + + if ($xml === false) { + $sheets = []; + $GLOBALS['message'] = Message::error( + __( + 'The XML file specified was either malformed or incomplete.' + . ' Please correct the issue and try again.' + ) + ); + $GLOBALS['error'] = true; + } else { + /** @var SimpleXMLElement $root */ + $root = $xml->children('office', true)->{'body'}->{'spreadsheet'}; + if (empty($root)) { + $sheets = []; + $GLOBALS['message'] = Message::error( + __('Could not parse OpenDocument Spreadsheet!') + ); + $GLOBALS['error'] = true; + } else { + $sheets = $root->children('table', true); + } + } + + $tables = []; + + $max_cols = 0; + + $col_count = 0; + $col_names = []; + + $tempRow = []; + $tempRows = []; + $rows = []; + + /* Iterate over tables */ + /** @var SimpleXMLElement $sheet */ + foreach ($sheets as $sheet) { + $col_names_in_first_row = isset($_REQUEST['ods_col_names']); + + /* Iterate over rows */ + /** @var SimpleXMLElement $row */ + foreach ($sheet as $row) { + $type = $row->getName(); + if (strcmp('table-row', $type)) { + continue; + } + /* Iterate over columns */ + $cellCount = count($row); + $a = 0; + /** @var SimpleXMLElement $cell */ + foreach ($row as $cell) { + $a++; + $text = $cell->children('text', true); + $cell_attrs = $cell->attributes('office', true); + + if (count($text) != 0) { + $attr = $cell->attributes('table', true); + $num_repeat = (int) $attr['number-columns-repeated']; + $num_iterations = $num_repeat ?: 1; + + for ($k = 0; $k < $num_iterations; $k++) { + $value = $this->getValue($cell_attrs, $text); + if (! $col_names_in_first_row) { + $tempRow[] = $value; + } else { + // MySQL column names can't end with a space + // character. + $col_names[] = rtrim($value); + } + + ++$col_count; + } + continue; + } + + // skip empty repeats in the last row + if ($a == $cellCount) { + continue; + } + + $attr = $cell->attributes('table', true); + $num_null = (int) $attr['number-columns-repeated']; + + if ($num_null) { + if (! $col_names_in_first_row) { + for ($i = 0; $i < $num_null; ++$i) { + $tempRow[] = 'NULL'; + ++$col_count; + } + } else { + for ($i = 0; $i < $num_null; ++$i) { + $col_names[] = $this->import->getColumnAlphaName( + $col_count + 1 + ); + ++$col_count; + } + } + } else { + if (! $col_names_in_first_row) { + $tempRow[] = 'NULL'; + } else { + $col_names[] = $this->import->getColumnAlphaName( + $col_count + 1 + ); + } + + ++$col_count; + } + } //Endforeach + + /* Find the widest row */ + if ($col_count > $max_cols) { + $max_cols = $col_count; + } + + /* Don't include a row that is full of NULL values */ + if (! $col_names_in_first_row) { + if ($_REQUEST['ods_empty_rows']) { + foreach ($tempRow as $cell) { + if (strcmp('NULL', $cell)) { + $tempRows[] = $tempRow; + break; + } + } + } else { + $tempRows[] = $tempRow; + } + } + + $col_count = 0; + $col_names_in_first_row = false; + $tempRow = []; + } + + /* Skip over empty sheets */ + if (count($tempRows) == 0 || count($tempRows[0]) === 0) { + $col_names = []; + $tempRow = []; + $tempRows = []; + continue; + } + + /** + * Fill out each row as necessary to make + * every one exactly as wide as the widest + * row. This included column names. + */ + + /* Fill out column names */ + for ($i = count($col_names); $i < $max_cols; ++$i) { + $col_names[] = $this->import->getColumnAlphaName($i + 1); + } + + /* Fill out all rows */ + $num_rows = count($tempRows); + for ($i = 0; $i < $num_rows; ++$i) { + for ($j = count($tempRows[$i]); $j < $max_cols; ++$j) { + $tempRows[$i][] = 'NULL'; + } + } + + /* Store the table name so we know where to place the row set */ + $tbl_attr = $sheet->attributes('table', true); + $tables[] = [(string) $tbl_attr['name']]; + + /* Store the current sheet in the accumulator */ + $rows[] = [ + (string) $tbl_attr['name'], + $col_names, + $tempRows, + ]; + $tempRows = []; + $col_names = []; + $max_cols = 0; + } + + unset($tempRow); + unset($tempRows); + unset($col_names); + unset($sheets); + unset($xml); + + /** + * Bring accumulated rows into the corresponding table + */ + $num_tables = count($tables); + for ($i = 0; $i < $num_tables; ++$i) { + $num_rows = count($rows); + for ($j = 0; $j < $num_rows; ++$j) { + if (strcmp($tables[$i][Import::TBL_NAME], $rows[$j][Import::TBL_NAME])) { + continue; + } + + if (! isset($tables[$i][Import::COL_NAMES])) { + $tables[$i][] = $rows[$j][Import::COL_NAMES]; + } + + $tables[$i][Import::ROWS] = $rows[$j][Import::ROWS]; + } + } + + /* No longer needed */ + unset($rows); + + /* Obtain the best-fit MySQL types for each column */ + $analyses = []; + + $len = count($tables); + for ($i = 0; $i < $len; ++$i) { + $analyses[] = $this->import->analyzeTable($tables[$i]); + } + + /** + * string $db_name (no backquotes) + * + * array $table = array(table_name, array() column_names, array()() rows) + * array $tables = array of "$table"s + * + * array $analysis = array(array() column_types, array() column_sizes) + * array $analyses = array of "$analysis"s + * + * array $create = array of SQL strings + * + * array $options = an associative array of options + */ + + /* Set database name to the currently selected one, if applicable */ + list($db_name, $options) = $this->getDbnameAndOptions($db, 'ODS_DB'); + + /* Non-applicable parameters */ + $create = null; + + /* Created and execute necessary SQL statements from data */ + $this->import->buildSql($db_name, $tables, $analyses, $create, $options, $sql_data); + + unset($tables); + unset($analyses); + + /* Commit any possible data in buffers */ + $this->import->runQuery('', '', $sql_data); + } + + /** + * Get value + * + * @param array $cell_attrs Cell attributes + * @param array $text Texts + * + * @return float|string + */ + protected function getValue($cell_attrs, $text) + { + if ($_REQUEST['ods_recognize_percentages'] + && ! strcmp( + 'percentage', + (string) $cell_attrs['value-type'] + ) + ) { + $value = (double) $cell_attrs['value']; + + return $value; + } elseif ($_REQUEST['ods_recognize_currency'] + && ! strcmp('currency', (string) $cell_attrs['value-type']) + ) { + $value = (double) $cell_attrs['value']; + + return $value; + } + + /* We need to concatenate all paragraphs */ + $values = []; + foreach ($text as $paragraph) { + $values[] = (string) $paragraph; + } + $value = implode("\n", $values); + + return $value; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportShp.php b/srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportShp.php new file mode 100644 index 0000000..614cb9e --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportShp.php @@ -0,0 +1,335 @@ +setProperties(); + if (extension_loaded('zip')) { + $this->zipExtension = new ZipExtension(); + } + } + + /** + * Sets the import plugin properties. + * Called in the constructor. + * + * @return void + */ + protected function setProperties() + { + $importPluginProperties = new ImportPluginProperties(); + $importPluginProperties->setText(__('ESRI Shape File')); + $importPluginProperties->setExtension('shp'); + $importPluginProperties->setOptions([]); + $importPluginProperties->setOptionsText(__('Options')); + + $this->properties = $importPluginProperties; + } + + /** + * Handles the whole import logic + * + * @param array $sql_data 2-element array with sql data + * + * @return void + */ + public function doImport(array &$sql_data = []) + { + global $db, $error, $finished, + $import_file, $local_import_file, $message; + + $GLOBALS['finished'] = false; + + $compression = $GLOBALS['import_handle']->getCompression(); + + $shp = new ShapeFileImport(1); + // If the zip archive has more than one file, + // get the correct content to the buffer from .shp file. + if ($compression == 'application/zip' + && $this->zipExtension->getNumberOfFiles($import_file) > 1 + ) { + if ($GLOBALS['import_handle']->openZip('/^.*\.shp$/i') === false) { + $message = Message::error( + __('There was an error importing the ESRI shape file: "%s".') + ); + $message->addParam($GLOBALS['import_handle']->getError()); + + return; + } + } + + $temp_dbf_file = false; + // We need dbase extension to handle .dbf file + if (extension_loaded('dbase')) { + $temp = $GLOBALS['PMA_Config']->getTempDir('shp'); + // If we can extract the zip archive to 'TempDir' + // and use the files in it for import + if ($compression == 'application/zip' && $temp !== null) { + $dbf_file_name = $this->zipExtension->findFile( + $import_file, + '/^.*\.dbf$/i' + ); + // If the corresponding .dbf file is in the zip archive + if ($dbf_file_name) { + // Extract the .dbf file and point to it. + $extracted = $this->zipExtension->extract( + $import_file, + $dbf_file_name + ); + if ($extracted !== false) { + // remove filename extension, e.g. + // dresden_osm.shp/gis.osm_transport_a_v06.dbf + // to + // dresden_osm.shp/gis.osm_transport_a_v06 + $path_parts = pathinfo($dbf_file_name); + $dbf_file_name = $path_parts['dirname'] . '/' . $path_parts['filename']; + + // sanitize filename + $dbf_file_name = Sanitize::sanitizeFilename($dbf_file_name, true); + + // concat correct filename and extension + $dbf_file_path = $temp . '/' . $dbf_file_name . '.dbf'; + + if (file_put_contents($dbf_file_path, $extracted, LOCK_EX) !== false) { + $temp_dbf_file = true; + + // Replace the .dbf with .*, as required by the bsShapeFiles library. + $shp->FileName = substr($dbf_file_path, 0, -4) . '.*'; + } + } + } + } elseif (! empty($local_import_file) + && ! empty($GLOBALS['cfg']['UploadDir']) + && $compression == 'none' + ) { + // If file is in UploadDir, use .dbf file in the same UploadDir + // to load extra data. + // Replace the .shp with .*, + // so the bsShapeFiles library correctly locates .dbf file. + $file_name = mb_substr( + $import_file, + 0, + mb_strlen($import_file) - 4 + ) . '.*'; + $shp->FileName = $file_name; + } + } + + // It should load data before file being deleted + $shp->loadFromFile(''); + + // Delete the .dbf file extracted to 'TempDir' + if ($temp_dbf_file + && isset($dbf_file_path) + && @file_exists($dbf_file_path) + ) { + unlink($dbf_file_path); + } + + if ($shp->lastError != '') { + $error = true; + $message = Message::error( + __('There was an error importing the ESRI shape file: "%s".') + ); + $message->addParam($shp->lastError); + + return; + } + + switch ($shp->shapeType) { + // ESRI Null Shape + case 0: + break; + // ESRI Point + case 1: + $gis_type = 'point'; + break; + // ESRI PolyLine + case 3: + $gis_type = 'multilinestring'; + break; + // ESRI Polygon + case 5: + $gis_type = 'multipolygon'; + break; + // ESRI MultiPoint + case 8: + $gis_type = 'multipoint'; + break; + default: + $error = true; + $message = Message::error( + __('MySQL Spatial Extension does not support ESRI type "%s".') + ); + $message->addParam($shp->getShapeName()); + return; + } + + if (isset($gis_type)) { + /** @var GisMultiLineString|GisMultiPoint|GisPoint|GisPolygon $gis_obj */ + $gis_obj = GisFactory::factory($gis_type); + } else { + $gis_obj = null; + } + + $num_rows = count($shp->records); + // If .dbf file is loaded, the number of extra data columns + $num_data_cols = $shp->getDBFHeader() !== null ? count($shp->getDBFHeader()) : 0; + + $rows = []; + $col_names = []; + if ($num_rows != 0) { + foreach ($shp->records as $record) { + $tempRow = []; + if ($gis_obj == null) { + $tempRow[] = null; + } else { + $tempRow[] = "GeomFromText('" + . $gis_obj->getShape($record->SHPData) . "')"; + } + + if ($shp->getDBFHeader() !== null) { + foreach ($shp->getDBFHeader() as $c) { + $cell = trim((string) $record->DBFData[$c[0]]); + + if (! strcmp($cell, '')) { + $cell = 'NULL'; + } + + $tempRow[] = $cell; + } + } + $rows[] = $tempRow; + } + } + + if (count($rows) === 0) { + $error = true; + $message = Message::error( + __('The imported file does not contain any data!') + ); + + return; + } + + // Column names for spatial column and the rest of the columns, + // if they are available + $col_names[] = 'SPATIAL'; + for ($n = 0; $n < $num_data_cols; $n++) { + $col_names[] = $shp->getDBFHeader()[$n][0]; + } + + // Set table name based on the number of tables + if (strlen((string) $db) > 0) { + $result = $GLOBALS['dbi']->fetchResult('SHOW TABLES'); + $table_name = 'TABLE ' . (count($result) + 1); + } else { + $table_name = 'TBL_NAME'; + } + $tables = [ + [ + $table_name, + $col_names, + $rows, + ], + ]; + + // Use data from shape file to chose best-fit MySQL types for each column + $analyses = []; + $analyses[] = $this->import->analyzeTable($tables[0]); + + $table_no = 0; + $spatial_col = 0; + $analyses[$table_no][Import::TYPES][$spatial_col] = Import::GEOMETRY; + $analyses[$table_no][Import::FORMATTEDSQL][$spatial_col] = true; + + // Set database name to the currently selected one, if applicable + if (strlen((string) $db) > 0) { + $db_name = $db; + $options = ['create_db' => false]; + } else { + $db_name = 'SHP_DB'; + $options = null; + } + + // Created and execute necessary SQL statements from data + $null_param = null; + $this->import->buildSql($db_name, $tables, $analyses, $null_param, $options, $sql_data); + + unset($tables); + unset($analyses); + + $finished = true; + $error = false; + + // Commit any possible data in buffers + $this->import->runQuery('', '', $sql_data); + } + + /** + * Returns specified number of bytes from the buffer. + * Buffer automatically fetches next chunk of data when the buffer + * falls short. + * Sets $eof when $GLOBALS['finished'] is set and the buffer falls short. + * + * @param int $length number of bytes + * + * @return string + */ + public static function readFromBuffer($length) + { + global $buffer, $eof; + + $import = new Import(); + + if (strlen((string) $buffer) < $length) { + if ($GLOBALS['finished']) { + $eof = true; + } else { + $buffer .= $import->getNextChunk(); + } + } + $result = substr($buffer, 0, $length); + $buffer = substr($buffer, $length); + + return $result; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportSql.php b/srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportSql.php new file mode 100644 index 0000000..599db37 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportSql.php @@ -0,0 +1,200 @@ +setProperties(); + } + + /** + * Sets the import plugin properties. + * Called in the constructor. + * + * @return void + */ + protected function setProperties() + { + $importPluginProperties = new ImportPluginProperties(); + $importPluginProperties->setText('SQL'); + $importPluginProperties->setExtension('sql'); + $importPluginProperties->setOptionsText(__('Options')); + + $compats = $GLOBALS['dbi']->getCompatibilities(); + if (count($compats) > 0) { + $values = []; + foreach ($compats as $val) { + $values[$val] = $val; + } + + // create the root group that will be the options field for + // $importPluginProperties + // this will be shown as "Format specific options" + $importSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // general options main group + $generalOptions = new OptionsPropertyMainGroup("general_opts"); + // create primary items and add them to the group + $leaf = new SelectPropertyItem( + "compatibility", + __('SQL compatibility mode:') + ); + $leaf->setValues($values); + $leaf->setDoc( + [ + 'manual_MySQL_Database_Administration', + 'Server_SQL_mode', + ] + ); + $generalOptions->addProperty($leaf); + $leaf = new BoolPropertyItem( + "no_auto_value_on_zero", + __('Do not use AUTO_INCREMENT for zero values') + ); + $leaf->setDoc( + [ + 'manual_MySQL_Database_Administration', + 'Server_SQL_mode', + 'sqlmode_no_auto_value_on_zero', + ] + ); + $generalOptions->addProperty($leaf); + + // add the main group to the root group + $importSpecificOptions->addProperty($generalOptions); + // set the options for the import plugin property item + $importPluginProperties->setOptions($importSpecificOptions); + } + + $this->properties = $importPluginProperties; + } + + /** + * Handles the whole import logic + * + * @param array $sql_data 2-element array with sql data + * + * @return void + */ + public function doImport(array &$sql_data = []) + { + global $error, $timeout_passed; + + // Handle compatibility options. + $this->_setSQLMode($GLOBALS['dbi'], $_REQUEST); + + $bq = new BufferedQuery(); + if (isset($_POST['sql_delimiter'])) { + $bq->setDelimiter($_POST['sql_delimiter']); + } + + /** + * Will be set in Import::getNextChunk(). + * + * @global bool $GLOBALS ['finished'] + */ + $GLOBALS['finished'] = false; + + while ((! $error) && (! $timeout_passed)) { + // Getting the first statement, the remaining data and the last + // delimiter. + $statement = $bq->extract(); + + // If there is no full statement, we are looking for more data. + if (empty($statement)) { + // Importing new data. + $newData = $this->import->getNextChunk(); + + // Subtract data we didn't handle yet and stop processing. + if ($newData === false) { + $GLOBALS['offset'] -= mb_strlen($bq->query); + break; + } + + // Checking if the input buffer has finished. + if ($newData === true) { + $GLOBALS['finished'] = true; + break; + } + + // Convert CR (but not CRLF) to LF otherwise all queries may + // not get executed on some platforms. + $bq->query .= preg_replace("/\r($|[^\n])/", "\n$1", $newData); + + continue; + } + + // Executing the query. + $this->import->runQuery($statement, $statement, $sql_data); + } + + // Extracting remaining statements. + while (! $error && ! $timeout_passed && ! empty($bq->query)) { + $statement = $bq->extract(true); + if (! empty($statement)) { + $this->import->runQuery($statement, $statement, $sql_data); + } + } + + // Finishing. + $this->import->runQuery('', '', $sql_data); + } + + /** + * Handle compatibility options + * + * @param DatabaseInterface $dbi Database interface + * @param array $request Request array + * + * @return void + */ + private function _setSQLMode($dbi, array $request) + { + $sql_modes = []; + if (isset($request['sql_compatibility']) + && 'NONE' != $request['sql_compatibility'] + ) { + $sql_modes[] = $request['sql_compatibility']; + } + if (isset($request['sql_no_auto_value_on_zero'])) { + $sql_modes[] = 'NO_AUTO_VALUE_ON_ZERO'; + } + if (count($sql_modes) > 0) { + $dbi->tryQuery( + 'SET SQL_MODE="' . implode(',', $sql_modes) . '"' + ); + } + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportXml.php b/srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportXml.php new file mode 100644 index 0000000..833d4e4 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Import/ImportXml.php @@ -0,0 +1,375 @@ +setProperties(); + } + + /** + * Sets the import plugin properties. + * Called in the constructor. + * + * @return void + */ + protected function setProperties() + { + $importPluginProperties = new ImportPluginProperties(); + $importPluginProperties->setText(__('XML')); + $importPluginProperties->setExtension('xml'); + $importPluginProperties->setMimeType('text/xml'); + $importPluginProperties->setOptions([]); + $importPluginProperties->setOptionsText(__('Options')); + + $this->properties = $importPluginProperties; + } + + /** + * Handles the whole import logic + * + * @param array $sql_data 2-element array with sql data + * + * @return void + */ + public function doImport(array &$sql_data = []) + { + global $error, $timeout_passed, $finished, $db; + + $i = 0; + $len = 0; + $buffer = ""; + + /** + * Read in the file via Import::getNextChunk so that + * it can process compressed files + */ + while (! ($finished && $i >= $len) && ! $error && ! $timeout_passed) { + $data = $this->import->getNextChunk(); + if ($data === false) { + /* subtract data we didn't handle yet and stop processing */ + $GLOBALS['offset'] -= strlen($buffer); + break; + } elseif ($data !== true) { + /* Append new data to buffer */ + $buffer .= $data; + unset($data); + } + } + + unset($data); + + /** + * Disable loading of external XML entities. + */ + libxml_disable_entity_loader(); + + /** + * Load the XML string + * + * The option LIBXML_COMPACT is specified because it can + * result in increased performance without the need to + * alter the code in any way. It's basically a freebee. + */ + $xml = @simplexml_load_string($buffer, "SimpleXMLElement", LIBXML_COMPACT); + + unset($buffer); + + /** + * The XML was malformed + */ + if ($xml === false) { + Message::error( + __( + 'The XML file specified was either malformed or incomplete.' + . ' Please correct the issue and try again.' + ) + ) + ->display(); + unset($xml); + $GLOBALS['finished'] = false; + + return; + } + + /** + * Table accumulator + */ + $tables = []; + /** + * Row accumulator + */ + $rows = []; + + /** + * Temp arrays + */ + $tempRow = []; + $tempCells = []; + + /** + * CREATE code included (by default: no) + */ + $struct_present = false; + + /** + * Analyze the data in each table + */ + $namespaces = $xml->getNamespaces(true); + + /** + * Get the database name, collation and charset + */ + $db_attr = $xml->children(isset($namespaces['pma']) ? $namespaces['pma'] : null) + ->{'structure_schemas'}->{'database'}; + + if ($db_attr instanceof SimpleXMLElement) { + $db_attr = $db_attr->attributes(); + $db_name = (string) $db_attr['name']; + $collation = (string) $db_attr['collation']; + $charset = (string) $db_attr['charset']; + } else { + /** + * If the structure section is not present + * get the database name from the data section + */ + $db_attr = $xml->children() + ->attributes(); + $db_name = (string) $db_attr['name']; + $collation = null; + $charset = null; + } + + /** + * The XML was malformed + */ + if ($db_name === null) { + Message::error( + __( + 'The XML file specified was either malformed or incomplete.' + . ' Please correct the issue and try again.' + ) + ) + ->display(); + unset($xml); + $GLOBALS['finished'] = false; + + return; + } + + /** + * Retrieve the structure information + */ + if (isset($namespaces['pma'])) { + /** + * Get structures for all tables + * + * @var SimpleXMLElement $struct + */ + $struct = $xml->children($namespaces['pma']); + + $create = []; + + /** @var SimpleXMLElement $val1 */ + foreach ($struct as $val1) { + /** @var SimpleXMLElement $val2 */ + foreach ($val1 as $val2) { + // Need to select the correct database for the creation of + // tables, views, triggers, etc. + /** + * @todo Generating a USE here blocks importing of a table + * into another database. + */ + $attrs = $val2->attributes(); + $create[] = "USE " + . Util::backquote( + $attrs["name"] + ); + + foreach ($val2 as $val3) { + /** + * Remove the extra cosmetic spacing + */ + $val3 = str_replace(" ", "", (string) $val3); + $create[] = $val3; + } + } + } + + $struct_present = true; + } + + /** + * Move down the XML tree to the actual data + */ + $xml = $xml->children() + ->children(); + + $data_present = false; + + /** + * Only attempt to analyze/collect data if there is data present + */ + if ($xml && @count($xml->children())) { + $data_present = true; + + /** + * Process all database content + */ + foreach ($xml as $v1) { + $tbl_attr = $v1->attributes(); + + $isInTables = false; + $num_tables = count($tables); + for ($i = 0; $i < $num_tables; ++$i) { + if (! strcmp($tables[$i][Import::TBL_NAME], (string) $tbl_attr['name'])) { + $isInTables = true; + break; + } + } + + if (! $isInTables) { + $tables[] = [(string) $tbl_attr['name']]; + } + + foreach ($v1 as $v2) { + $row_attr = $v2->attributes(); + if (! array_search((string) $row_attr['name'], $tempRow)) { + $tempRow[] = (string) $row_attr['name']; + } + $tempCells[] = (string) $v2; + } + + $rows[] = [ + (string) $tbl_attr['name'], + $tempRow, + $tempCells, + ]; + + $tempRow = []; + $tempCells = []; + } + + unset($tempRow); + unset($tempCells); + unset($xml); + + /** + * Bring accumulated rows into the corresponding table + */ + $num_tables = count($tables); + for ($i = 0; $i < $num_tables; ++$i) { + $num_rows = count($rows); + for ($j = 0; $j < $num_rows; ++$j) { + if (! strcmp($tables[$i][Import::TBL_NAME], $rows[$j][Import::TBL_NAME])) { + if (! isset($tables[$i][Import::COL_NAMES])) { + $tables[$i][] = $rows[$j][Import::COL_NAMES]; + } + + $tables[$i][Import::ROWS][] = $rows[$j][Import::ROWS]; + } + } + } + + unset($rows); + + if (! $struct_present) { + $analyses = []; + + $len = count($tables); + for ($i = 0; $i < $len; ++$i) { + $analyses[] = $this->import->analyzeTable($tables[$i]); + } + } + } + + unset($xml); + unset($tempCells); + unset($rows); + + /** + * Only build SQL from data if there is data present + */ + if ($data_present) { + /** + * Set values to NULL if they were not present + * to maintain Import::buildSql() call integrity + */ + if (! isset($analyses)) { + $analyses = null; + if (! $struct_present) { + $create = null; + } + } + } + + /** + * string $db_name (no backquotes) + * + * array $table = array(table_name, array() column_names, array()() rows) + * array $tables = array of "$table"s + * + * array $analysis = array(array() column_types, array() column_sizes) + * array $analyses = array of "$analysis"s + * + * array $create = array of SQL strings + * + * array $options = an associative array of options + */ + + /* Set database name to the currently selected one, if applicable */ + if (strlen((string) $db)) { + /* Override the database name in the XML file, if one is selected */ + $db_name = $db; + $options = ['create_db' => false]; + } else { + if ($db_name === null) { + $db_name = 'XML_DB'; + } + + /* Set database collation/charset */ + $options = [ + 'db_collation' => $collation, + 'db_charset' => $charset, + ]; + } + + /* Created and execute necessary SQL statements from data */ + $this->import->buildSql($db_name, $tables, $analyses, $create, $options, $sql_data); + + unset($analyses); + unset($tables); + unset($create); + + /* Commit any possible data in buffers */ + $this->import->runQuery('', '', $sql_data); + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Import/README b/srcs/phpmyadmin/libraries/classes/Plugins/Import/README new file mode 100644 index 0000000..e240556 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Import/README @@ -0,0 +1,156 @@ +This directory holds import plugins for phpMyAdmin. Any new plugin should +basically follow the structure presented here. The messages must use our +gettext mechanism, see https://wiki.phpmyadmin.net/pma/Gettext_for_developers. + +setProperties(); + } + + /** + * Sets the import plugin properties. + * Called in the constructor. + * + * @return void + */ + protected function setProperties() + { + $importPluginProperties = new PhpMyAdmin\Properties\Plugins\ImportPluginProperties(); + $importPluginProperties->setText('[name]'); // the name of your plug-in + $importPluginProperties->setExtension('[ext]'); // extension this plug-in can handle + $importPluginProperties->setOptionsText(__('Options')); + + // create the root group that will be the options field for + // $importPluginProperties + // this will be shown as "Format specific options" + $importSpecificOptions = new + PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // general options main group + $generalOptions = new PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup( + "general_opts" + ); + + // optional : + // create primary items and add them to the group + // type - one of the classes listed in libraries/properties/options/items/ + // name - form element name + // text - description in GUI + // size - size of text element + // len - maximal size of input + // values - possible values of the item + $leaf = new PhpMyAdmin\Properties\Options\Items\RadioPropertyItem( + "structure_or_data" + ); + $leaf->setValues( + array( + 'structure' => __('structure'), + 'data' => __('data'), + 'structure_and_data' => __('structure and data') + ) + ); + $generalOptions->addProperty($leaf); + + // add the main group to the root group + $importSpecificOptions->addProperty($generalOptions); + + // set the options for the import plugin property item + $importPluginProperties->setOptions($importSpecificOptions); + $this->properties = $importPluginProperties; + } + + /** + * Handles the whole import logic + * + * @param array &$sql_data 2-element array with sql data + * + * @return void + */ + public function doImport(&$sql_data = array()) + { + // get globals (others are optional) + global $error, $timeout_passed, $finished; + + $buffer = ''; + while (! ($finished && $i >= $len) && ! $error && ! $timeout_passed) { + $data = $this->import->getNextChunk(); + if ($data === false) { + // subtract data we didn't handle yet and stop processing + $GLOBALS['offset'] -= strlen($buffer); + break; + } elseif ($data === true) { + // Handle rest of buffer + } else { + // Append new data to buffer + $buffer .= $data; + } + // PARSE $buffer here, post sql queries using: + $this->import->runQuery($sql, $verbose_sql_with_comments, $sql_data); + } // End of import loop + // Commit any possible data in buffers + $this->import->runQuery('', '', $sql_data); + } + + + // optional: + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + + /** + * Getter description + * + * @return type + */ + private function _getMyOptionalVariable() + { + return $this->_myOptionalVariable; + } + + /** + * Setter description + * + * @param type $my_optional_variable description + * + * @return void + */ + private function _setMyOptionalVariable($my_optional_variable) + { + $this->_myOptionalVariable = $my_optional_variable; + } +} +?> diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Import/ShapeFileImport.php b/srcs/phpmyadmin/libraries/classes/Plugins/Import/ShapeFileImport.php new file mode 100644 index 0000000..8af3f90 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Import/ShapeFileImport.php @@ -0,0 +1,46 @@ + $id, + 'finished' => false, + 'percent' => 0, + 'total' => 0, + 'complete' => 0, + 'plugin' => UploadApc::getIdKey(), + ]; + } + $ret = $_SESSION[$SESSION_KEY][$id]; + + if (! ImportAjax::apcCheck() || $ret['finished']) { + return $ret; + } + $status = apc_fetch('upload_' . $id); + + if ($status) { + $ret['finished'] = (bool) $status['done']; + $ret['total'] = $status['total']; + $ret['complete'] = $status['current']; + + if ($ret['total'] > 0) { + $ret['percent'] = $ret['complete'] / $ret['total'] * 100; + } + + if ($ret['percent'] == 100) { + $ret['finished'] = (bool) true; + } + + $_SESSION[$SESSION_KEY][$id] = $ret; + } + + return $ret; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Import/Upload/UploadNoplugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/Import/Upload/UploadNoplugin.php new file mode 100644 index 0000000..087b1e1 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Import/Upload/UploadNoplugin.php @@ -0,0 +1,60 @@ + $id, + 'finished' => false, + 'percent' => 0, + 'total' => 0, + 'complete' => 0, + 'plugin' => UploadNoplugin::getIdKey(), + ]; + } + return $_SESSION[$SESSION_KEY][$id]; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Import/Upload/UploadProgress.php b/srcs/phpmyadmin/libraries/classes/Plugins/Import/Upload/UploadProgress.php new file mode 100644 index 0000000..13578e0 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Import/Upload/UploadProgress.php @@ -0,0 +1,97 @@ + $id, + 'finished' => false, + 'percent' => 0, + 'total' => 0, + 'complete' => 0, + 'plugin' => UploadProgress::getIdKey(), + ]; + } + $ret = $_SESSION[$SESSION_KEY][$id]; + + if (! ImportAjax::progressCheck() || $ret['finished']) { + return $ret; + } + + $status = null; + if (function_exists('uploadprogress_get_info')) { + $status = uploadprogress_get_info($id); + } + + if ($status) { + if ($status['bytes_uploaded'] == $status['bytes_total']) { + $ret['finished'] = true; + } else { + $ret['finished'] = false; + } + $ret['total'] = $status['bytes_total']; + $ret['complete'] = $status['bytes_uploaded']; + + if ($ret['total'] > 0) { + $ret['percent'] = $ret['complete'] / $ret['total'] * 100; + } + } else { + $ret = [ + 'id' => $id, + 'finished' => true, + 'percent' => 100, + 'total' => $ret['total'], + 'complete' => $ret['total'], + 'plugin' => UploadProgress::getIdKey(), + ]; + } + + $_SESSION[$SESSION_KEY][$id] = $ret; + + return $ret; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Import/Upload/UploadSession.php b/srcs/phpmyadmin/libraries/classes/Plugins/Import/Upload/UploadSession.php new file mode 100644 index 0000000..133b11a --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Import/Upload/UploadSession.php @@ -0,0 +1,95 @@ + $id, + 'finished' => false, + 'percent' => 0, + 'total' => 0, + 'complete' => 0, + 'plugin' => UploadSession::getIdKey(), + ]; + } + $ret = $_SESSION[$SESSION_KEY][$id]; + + if (! ImportAjax::sessionCheck() || $ret['finished']) { + return $ret; + } + + $status = false; + $sessionkey = ini_get('session.upload_progress.prefix') . $id; + + if (isset($_SESSION[$sessionkey])) { + $status = $_SESSION[$sessionkey]; + } + + if ($status) { + $ret['finished'] = $status['done']; + $ret['total'] = $status['content_length']; + $ret['complete'] = $status['bytes_processed']; + + if ($ret['total'] > 0) { + $ret['percent'] = $ret['complete'] / $ret['total'] * 100; + } + } else { + $ret = [ + 'id' => $id, + 'finished' => true, + 'percent' => 100, + 'total' => $ret['total'], + 'complete' => $ret['total'], + 'plugin' => UploadSession::getIdKey(), + ]; + } + + $_SESSION[$SESSION_KEY][$id] = $ret; + + return $ret; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/ImportPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/ImportPlugin.php new file mode 100644 index 0000000..788bd42 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/ImportPlugin.php @@ -0,0 +1,96 @@ +import = new Import(); + } + + /** + * Handles the whole import logic + * + * @param array $sql_data 2-element array with sql data + * + * @return void + */ + abstract public function doImport(array &$sql_data = []); + + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the import specific format plugin properties + * + * @return ImportPluginProperties + */ + public function getProperties() + { + return $this->properties; + } + + /** + * Sets the export plugins properties and is implemented by each import + * plugin + * + * @return void + */ + abstract protected function setProperties(); + + /** + * Define DB name and options + * + * @param string $currentDb DB + * @param string $defaultDb Default DB name + * + * @return array DB name and options (an associative array of options) + */ + protected function getDbnameAndOptions($currentDb, $defaultDb) + { + if (strlen((string) $currentDb) > 0) { + $db_name = $currentDb; + $options = ['create_db' => false]; + } else { + $db_name = $defaultDb; + $options = null; + } + + return [ + $db_name, + $options, + ]; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Dia/Dia.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Dia/Dia.php new file mode 100644 index 0000000..b9941a5 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Dia/Dia.php @@ -0,0 +1,190 @@ +openMemory(); + /* + * Set indenting using three spaces, + * so output is formatted + */ + $this->setIndent(true); + $this->setIndentString(' '); + /* + * Create the XML document + */ + $this->startDocument('1.0', 'UTF-8'); + } + + /** + * Starts Dia Document + * + * dia document starts by first initializing dia:diagram tag + * then dia:diagramdata contains all the attributes that needed + * to define the document, then finally a Layer starts which + * holds all the objects. + * + * @param string $paper the size of the paper/document + * @param float $topMargin top margin of the paper/document in cm + * @param float $bottomMargin bottom margin of the paper/document in cm + * @param float $leftMargin left margin of the paper/document in cm + * @param float $rightMargin right margin of the paper/document in cm + * @param string $orientation orientation of the document, portrait or landscape + * + * @return void + * + * @access public + * @see XMLWriter::startElement(),XMLWriter::writeAttribute(), + * XMLWriter::writeRaw() + */ + public function startDiaDoc( + $paper, + $topMargin, + $bottomMargin, + $leftMargin, + $rightMargin, + $orientation + ) { + if ($orientation == 'P') { + $isPortrait = 'true'; + } else { + $isPortrait = 'false'; + } + $this->startElement('dia:diagram'); + $this->writeAttribute('xmlns:dia', 'http://www.lysator.liu.se/~alla/dia/'); + $this->startElement('dia:diagramdata'); + $this->writeRaw( + ' + + + + + + + + + #' . $paper . '# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ' + ); + $this->endElement(); + $this->startElement('dia:layer'); + $this->writeAttribute('name', 'Background'); + $this->writeAttribute('visible', 'true'); + $this->writeAttribute('active', 'true'); + } + + /** + * Ends Dia Document + * + * @return void + * @access public + * @see XMLWriter::endElement(),XMLWriter::endDocument() + */ + public function endDiaDoc() + { + $this->endElement(); + $this->endDocument(); + } + + /** + * Output Dia Document for download + * + * @param string $fileName name of the dia document + * + * @return void + * @access public + * @see XMLWriter::flush() + */ + public function showOutput($fileName) + { + if (ob_get_clean()) { + ob_end_clean(); + } + $output = $this->flush(); + Response::getInstance()->disable(); + Core::downloadHeader( + $fileName, + 'application/x-dia-diagram', + strlen($output) + ); + print $output; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Dia/DiaRelationSchema.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Dia/DiaRelationSchema.php new file mode 100644 index 0000000..ee04f0d --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Dia/DiaRelationSchema.php @@ -0,0 +1,238 @@ +setShowColor(isset($_REQUEST['dia_show_color'])); + $this->setShowKeys(isset($_REQUEST['dia_show_keys'])); + $this->setOrientation($_REQUEST['dia_orientation']); + $this->setPaper($_REQUEST['dia_paper']); + + $this->diagram->startDiaDoc( + $this->paper, + $this->_topMargin, + $this->_bottomMargin, + $this->_leftMargin, + $this->_rightMargin, + $this->orientation + ); + + $alltables = $this->getTablesFromRequest(); + + foreach ($alltables as $table) { + if (! isset($this->_tables[$table])) { + $this->_tables[$table] = new TableStatsDia( + $this->diagram, + $this->db, + $table, + $this->pageNumber, + $this->showKeys, + $this->offline + ); + } + } + + $seen_a_relation = false; + foreach ($alltables as $one_table) { + $exist_rel = $this->relation->getForeigners($this->db, $one_table, '', 'both'); + if (! $exist_rel) { + continue; + } + + $seen_a_relation = true; + foreach ($exist_rel as $master_field => $rel) { + /* put the foreign table on the schema only if selected + * by the user + * (do not use array_search() because we would have to + * to do a === false and this is not PHP3 compatible) + */ + if ($master_field != 'foreign_keys_data') { + if (in_array($rel['foreign_table'], $alltables)) { + $this->_addRelation( + $one_table, + $master_field, + $rel['foreign_table'], + $rel['foreign_field'], + $this->showKeys + ); + } + continue; + } + + foreach ($rel as $one_key) { + if (! in_array($one_key['ref_table_name'], $alltables)) { + continue; + } + + foreach ($one_key['index_list'] as $index => $one_field) { + $this->_addRelation( + $one_table, + $one_field, + $one_key['ref_table_name'], + $one_key['ref_index_list'][$index], + $this->showKeys + ); + } + } + } + } + $this->_drawTables(); + + if ($seen_a_relation) { + $this->_drawRelations(); + } + $this->diagram->endDiaDoc(); + } + + /** + * Output Dia Document for download + * + * @return void + * @access public + */ + public function showOutput() + { + $this->diagram->showOutput($this->getFileName('.dia')); + } + + /** + * Defines relation objects + * + * @param string $masterTable The master table name + * @param string $masterField The relation field in the master table + * @param string $foreignTable The foreign table name + * @param string $foreignField The relation field in the foreign table + * @param bool $showKeys Whether to display ONLY keys or not + * + * @return void + * + * @access private + * @see TableStatsDia::__construct(),RelationStatsDia::__construct() + */ + private function _addRelation( + $masterTable, + $masterField, + $foreignTable, + $foreignField, + $showKeys + ) { + if (! isset($this->_tables[$masterTable])) { + $this->_tables[$masterTable] = new TableStatsDia( + $this->diagram, + $this->db, + $masterTable, + $this->pageNumber, + $showKeys + ); + } + if (! isset($this->_tables[$foreignTable])) { + $this->_tables[$foreignTable] = new TableStatsDia( + $this->diagram, + $this->db, + $foreignTable, + $this->pageNumber, + $showKeys + ); + } + $this->_relations[] = new RelationStatsDia( + $this->diagram, + $this->_tables[$masterTable], + $masterField, + $this->_tables[$foreignTable], + $foreignField + ); + } + + /** + * Draws relation references + * + * connects master table's master field to + * foreign table's foreign field using Dia object + * type Database - Reference + * + * @return void + * + * @access private + * @see RelationStatsDia::relationDraw() + */ + private function _drawRelations() + { + foreach ($this->_relations as $relation) { + $relation->relationDraw($this->showColor); + } + } + + /** + * Draws tables + * + * Tables are generated using Dia object type Database - Table + * primary fields are underlined and bold in tables + * + * @return void + * + * @access private + * @see TableStatsDia::tableDraw() + */ + private function _drawTables() + { + foreach ($this->_tables as $table) { + $table->tableDraw($this->showColor); + } + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Dia/RelationStatsDia.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Dia/RelationStatsDia.php new file mode 100644 index 0000000..bd44532 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Dia/RelationStatsDia.php @@ -0,0 +1,228 @@ +diagram = $diagram; + $src_pos = $this->_getXy($master_table, $master_field); + $dest_pos = $this->_getXy($foreign_table, $foreign_field); + $this->srcConnPointsLeft = $src_pos[0]; + $this->srcConnPointsRight = $src_pos[1]; + $this->destConnPointsLeft = $dest_pos[0]; + $this->destConnPointsRight = $dest_pos[1]; + $this->masterTablePos = $src_pos[2]; + $this->foreignTablePos = $dest_pos[2]; + $this->masterTableId = $master_table->tableId; + $this->foreignTableId = $foreign_table->tableId; + } + + /** + * Each Table object have connection points + * which is used to connect to other objects in Dia + * we detect the position of key in fields and + * then determines its left and right connection + * points. + * + * @param TableStatsDia $table The current table name + * @param string $column The relation column name + * + * @return array Table right,left connection points and key position + * + * @access private + */ + private function _getXy($table, $column) + { + $pos = array_search($column, $table->fields); + // left, right, position + $value = 12; + if ($pos != 0) { + return [ + $pos + $value + $pos, + $pos + $value + $pos + 1, + $pos, + ]; + } + return [ + $pos + $value, + $pos + $value + 1, + $pos, + ]; + } + + /** + * Draws relation references + * + * connects master table's master field to foreign table's + * foreign field using Dia object type Database - Reference + * Dia object is used to generate the XML of Dia Document. + * Database reference Object and their attributes are involved + * in the combination of displaying Database - reference on Dia Document. + * + * @param boolean $showColor Whether to use one color per relation or not + * if showColor is true then an array of $listOfColors + * will be used to choose the random colors for + * references lines. we can change/add more colors to + * this + * + * @return boolean|void + * + * @access public + * @see PDF + */ + public function relationDraw($showColor) + { + ++DiaRelationSchema::$objectId; + /* + * if source connection points and destination connection + * points are same then return it false and don't draw that + * relation + */ + if ($this->srcConnPointsRight == $this->destConnPointsRight) { + if ($this->srcConnPointsLeft == $this->destConnPointsLeft) { + return false; + } + } + + if ($showColor) { + $listOfColors = [ + 'FF0000', + '000099', + '00FF00', + ]; + shuffle($listOfColors); + $this->referenceColor = '#' . $listOfColors[0] . ''; + } else { + $this->referenceColor = '#000000'; + } + + $this->diagram->writeRaw( + ' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #1# + + + #n# + + + + + + + + + + + + ' + ); + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Dia/TableStatsDia.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Dia/TableStatsDia.php new file mode 100644 index 0000000..b3ae5a0 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Dia/TableStatsDia.php @@ -0,0 +1,231 @@ +tableId = ++DiaRelationSchema::$objectId; + } + + /** + * Displays an error when the table cannot be found. + * + * @return void + */ + protected function showMissingTableError() + { + ExportRelationSchema::dieSchema( + $this->pageNumber, + "DIA", + sprintf(__('The %s table doesn\'t exist!'), $this->tableName) + ); + } + + /** + * Do draw the table + * + * Tables are generated using object type Database - Table + * primary fields are underlined in tables. Dia object + * is used to generate the XML of Dia Document. Database Table + * Object and their attributes are involved in the combination + * of displaying Database - Table on Dia Document. + * + * @param boolean $showColor Whether to show color for tables text or not + * if showColor is true then an array of $listOfColors + * will be used to choose the random colors for tables + * text we can change/add more colors to this array + * + * @return void + * + * @access public + * @see Dia + */ + public function tableDraw($showColor) + { + if ($showColor) { + $listOfColors = [ + 'FF0000', + '000099', + '00FF00', + ]; + shuffle($listOfColors); + $this->tableColor = '#' . $listOfColors[0] . ''; + } else { + $this->tableColor = '#000000'; + } + + $factor = 0.1; + + $this->diagram->startElement('dia:object'); + $this->diagram->writeAttribute('type', 'Database - Table'); + $this->diagram->writeAttribute('version', '0'); + $this->diagram->writeAttribute('id', '' . $this->tableId . ''); + $this->diagram->writeRaw( + ' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #' . $this->tableName . '# + + + ## + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ' + ); + + $this->diagram->startElement('dia:attribute'); + $this->diagram->writeAttribute('name', 'attributes'); + + foreach ($this->fields as $field) { + $this->diagram->writeRaw( + ' + + #' . $field . '# + + + ## + + + ## + ' + ); + unset($pm); + $pm = 'false'; + if (in_array($field, $this->primary)) { + $pm = 'true'; + } + if ($field == $this->displayfield) { + $pm = 'false'; + } + $this->diagram->writeRaw( + ' + + + + + + + + + ' + ); + } + $this->diagram->endElement(); + $this->diagram->endElement(); + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Eps/Eps.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Eps/Eps.php new file mode 100644 index 0000000..0679709 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Eps/Eps.php @@ -0,0 +1,280 @@ +stringCommands = ""; + $this->stringCommands .= "%!PS-Adobe-3.0 EPSF-3.0 \n"; + } + + /** + * Set document title + * + * @param string $value sets the title text + * + * @return void + */ + public function setTitle($value) + { + $this->stringCommands .= '%%Title: ' . $value . "\n"; + } + + /** + * Set document author + * + * @param string $value sets the author + * + * @return void + */ + public function setAuthor($value) + { + $this->stringCommands .= '%%Creator: ' . $value . "\n"; + } + + /** + * Set document creation date + * + * @param string $value sets the date + * + * @return void + */ + public function setDate($value) + { + $this->stringCommands .= '%%CreationDate: ' . $value . "\n"; + } + + /** + * Set document orientation + * + * @param string $orientation sets the orientation + * + * @return void + */ + public function setOrientation($orientation) + { + $this->stringCommands .= "%%PageOrder: Ascend \n"; + if ($orientation == "L") { + $orientation = "Landscape"; + $this->stringCommands .= '%%Orientation: ' . $orientation . "\n"; + } else { + $orientation = "Portrait"; + $this->stringCommands .= '%%Orientation: ' . $orientation . "\n"; + } + $this->stringCommands .= "%%EndComments \n"; + $this->stringCommands .= "%%Pages 1 \n"; + $this->stringCommands .= "%%BoundingBox: 72 150 144 170 \n"; + } + + /** + * Set the font and size + * + * font can be set whenever needed in EPS + * + * @param string $value sets the font name e.g Arial + * @param integer $size sets the size of the font e.g 10 + * + * @return void + */ + public function setFont($value, $size) + { + $this->font = $value; + $this->fontSize = $size; + $this->stringCommands .= "/" . $value . " findfont % Get the basic font\n"; + $this->stringCommands .= "" + . $size . " scalefont % Scale the font to $size points\n"; + $this->stringCommands + .= "setfont % Make it the current font\n"; + } + + /** + * Get the font + * + * @return string return the font name e.g Arial + */ + public function getFont() + { + return $this->font; + } + + /** + * Get the font Size + * + * @return string return the size of the font e.g 10 + */ + public function getFontSize() + { + return $this->fontSize; + } + + /** + * Draw the line + * + * drawing the lines from x,y source to x,y destination and set the + * width of the line. lines helps in showing relationships of tables + * + * @param integer $x_from The x_from attribute defines the start + * left position of the element + * @param integer $y_from The y_from attribute defines the start + * right position of the element + * @param integer $x_to The x_to attribute defines the end + * left position of the element + * @param integer $y_to The y_to attribute defines the end + * right position of the element + * @param integer $lineWidth Sets the width of the line e.g 2 + * + * @return void + */ + public function line( + $x_from = 0, + $y_from = 0, + $x_to = 0, + $y_to = 0, + $lineWidth = 0 + ) { + $this->stringCommands .= $lineWidth . " setlinewidth \n"; + $this->stringCommands .= $x_from . ' ' . $y_from . " moveto \n"; + $this->stringCommands .= $x_to . ' ' . $y_to . " lineto \n"; + $this->stringCommands .= "stroke \n"; + } + + /** + * Draw the rectangle + * + * drawing the rectangle from x,y source to x,y destination and set the + * width of the line. rectangles drawn around the text shown of fields + * + * @param integer $x_from The x_from attribute defines the start + * left position of the element + * @param integer $y_from The y_from attribute defines the start + * right position of the element + * @param integer $x_to The x_to attribute defines the end + * left position of the element + * @param integer $y_to The y_to attribute defines the end + * right position of the element + * @param integer $lineWidth Sets the width of the line e.g 2 + * + * @return void + */ + public function rect($x_from, $y_from, $x_to, $y_to, $lineWidth) + { + $this->stringCommands .= $lineWidth . " setlinewidth \n"; + $this->stringCommands .= "newpath \n"; + $this->stringCommands .= $x_from . " " . $y_from . " moveto \n"; + $this->stringCommands .= "0 " . $y_to . " rlineto \n"; + $this->stringCommands .= $x_to . " 0 rlineto \n"; + $this->stringCommands .= "0 -" . $y_to . " rlineto \n"; + $this->stringCommands .= "closepath \n"; + $this->stringCommands .= "stroke \n"; + } + + /** + * Set the current point + * + * The moveto operator takes two numbers off the stack and treats + * them as x and y coordinates to which to move. The coordinates + * specified become the current point. + * + * @param integer $x The x attribute defines the left position of the element + * @param integer $y The y attribute defines the right position of the element + * + * @return void + */ + public function moveTo($x, $y) + { + $this->stringCommands .= $x . ' ' . $y . " moveto \n"; + } + + /** + * Output/Display the text + * + * @param string $text The string to be displayed + * + * @return void + */ + public function show($text) + { + $this->stringCommands .= '(' . $text . ") show \n"; + } + + /** + * Output the text at specified co-ordinates + * + * @param string $text String to be displayed + * @param integer $x X attribute defines the left position of the element + * @param integer $y Y attribute defines the right position of the element + * + * @return void + */ + public function showXY($text, $x, $y) + { + $this->moveTo($x, $y); + $this->show($text); + } + + /** + * Ends EPS Document + * + * @return void + */ + public function endEpsDoc() + { + $this->stringCommands .= "showpage \n"; + } + + /** + * Output EPS Document for download + * + * @param string $fileName name of the eps document + * + * @return void + */ + public function showOutput($fileName) + { + // if(ob_get_clean()){ + //ob_end_clean(); + //} + $output = $this->stringCommands; + Response::getInstance() + ->disable(); + Core::downloadHeader( + $fileName, + 'image/x-eps', + strlen($output) + ); + print $output; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Eps/EpsRelationSchema.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Eps/EpsRelationSchema.php new file mode 100644 index 0000000..238af1b --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Eps/EpsRelationSchema.php @@ -0,0 +1,254 @@ +setShowColor(isset($_REQUEST['eps_show_color'])); + $this->setShowKeys(isset($_REQUEST['eps_show_keys'])); + $this->setTableDimension(isset($_REQUEST['eps_show_table_dimension'])); + $this->setAllTablesSameWidth(isset($_REQUEST['eps_all_tables_same_width'])); + $this->setOrientation($_REQUEST['eps_orientation']); + + $this->diagram->setTitle( + sprintf( + __('Schema of the %s database - Page %s'), + $this->db, + $this->pageNumber + ) + ); + $this->diagram->setAuthor('phpMyAdmin ' . PMA_VERSION); + $this->diagram->setDate(date("j F Y, g:i a")); + $this->diagram->setOrientation($this->orientation); + $this->diagram->setFont('Verdana', '10'); + + $alltables = $this->getTablesFromRequest(); + + foreach ($alltables as $table) { + if (! isset($this->_tables[$table])) { + $this->_tables[$table] = new TableStatsEps( + $this->diagram, + $this->db, + $table, + $this->diagram->getFont(), + $this->diagram->getFontSize(), + $this->pageNumber, + $this->_tablewidth, + $this->showKeys, + $this->tableDimension, + $this->offline + ); + } + + if ($this->sameWide) { + $this->_tables[$table]->width = $this->_tablewidth; + } + } + + $seen_a_relation = false; + foreach ($alltables as $one_table) { + $exist_rel = $this->relation->getForeigners($this->db, $one_table, '', 'both'); + if (! $exist_rel) { + continue; + } + + $seen_a_relation = true; + foreach ($exist_rel as $master_field => $rel) { + /* put the foreign table on the schema only if selected + * by the user + * (do not use array_search() because we would have to + * to do a === false and this is not PHP3 compatible) + */ + if ($master_field != 'foreign_keys_data') { + if (in_array($rel['foreign_table'], $alltables)) { + $this->_addRelation( + $one_table, + $this->diagram->getFont(), + $this->diagram->getFontSize(), + $master_field, + $rel['foreign_table'], + $rel['foreign_field'], + $this->tableDimension + ); + } + continue; + } + + foreach ($rel as $one_key) { + if (! in_array($one_key['ref_table_name'], $alltables)) { + continue; + } + + foreach ($one_key['index_list'] as $index => $one_field) { + $this->_addRelation( + $one_table, + $this->diagram->getFont(), + $this->diagram->getFontSize(), + $one_field, + $one_key['ref_table_name'], + $one_key['ref_index_list'][$index], + $this->tableDimension + ); + } + } + } + } + if ($seen_a_relation) { + $this->_drawRelations(); + } + + $this->_drawTables(); + $this->diagram->endEpsDoc(); + } + + /** + * Output Eps Document for download + * + * @return void + */ + public function showOutput() + { + $this->diagram->showOutput($this->getFileName('.eps')); + } + + /** + * Defines relation objects + * + * @param string $masterTable The master table name + * @param string $font The font + * @param int $fontSize The font size + * @param string $masterField The relation field in the master table + * @param string $foreignTable The foreign table name + * @param string $foreignField The relation field in the foreign table + * @param boolean $tableDimension Whether to display table position or not + * + * @return void + * + * @see _setMinMax,Table_Stats_Eps::__construct(), + * PhpMyAdmin\Plugins\Schema\Eps\RelationStatsEps::__construct() + */ + private function _addRelation( + $masterTable, + $font, + $fontSize, + $masterField, + $foreignTable, + $foreignField, + $tableDimension + ) { + if (! isset($this->_tables[$masterTable])) { + $this->_tables[$masterTable] = new TableStatsEps( + $this->diagram, + $this->db, + $masterTable, + $font, + $fontSize, + $this->pageNumber, + $this->_tablewidth, + false, + $tableDimension + ); + } + if (! isset($this->_tables[$foreignTable])) { + $this->_tables[$foreignTable] = new TableStatsEps( + $this->diagram, + $this->db, + $foreignTable, + $font, + $fontSize, + $this->pageNumber, + $this->_tablewidth, + false, + $tableDimension + ); + } + $this->_relations[] = new RelationStatsEps( + $this->diagram, + $this->_tables[$masterTable], + $masterField, + $this->_tables[$foreignTable], + $foreignField + ); + } + + /** + * Draws relation arrows and lines connects master table's master field to + * foreign table's foreign field + * + * @return void + * + * @see Relation_Stats_Eps::relationDraw() + */ + private function _drawRelations() + { + foreach ($this->_relations as $relation) { + $relation->relationDraw(); + } + } + + /** + * Draws tables + * + * @return void + * + * @see Table_Stats_Eps::Table_Stats_tableDraw() + */ + private function _drawTables() + { + foreach ($this->_tables as $table) { + $table->tableDraw($this->showColor); + } + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Eps/RelationStatsEps.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Eps/RelationStatsEps.php new file mode 100644 index 0000000..4c50131 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Eps/RelationStatsEps.php @@ -0,0 +1,120 @@ +wTick = 10; + parent::__construct( + $diagram, + $master_table, + $master_field, + $foreign_table, + $foreign_field + ); + $this->ySrc += 10; + $this->yDest += 10; + } + + /** + * draws relation links and arrows + * shows foreign key relations + * + * @see PMA_EPS + * + * @return void + */ + public function relationDraw() + { + // draw a line like -- to foreign field + $this->diagram->line( + $this->xSrc, + $this->ySrc, + $this->xSrc + $this->srcDir * $this->wTick, + $this->ySrc, + 1 + ); + // draw a line like -- to master field + $this->diagram->line( + $this->xDest + $this->destDir * $this->wTick, + $this->yDest, + $this->xDest, + $this->yDest, + 1 + ); + // draw a line that connects to master field line and foreign field line + $this->diagram->line( + $this->xSrc + $this->srcDir * $this->wTick, + $this->ySrc, + $this->xDest + $this->destDir * $this->wTick, + $this->yDest, + 1 + ); + $root2 = 2 * sqrt(2); + $this->diagram->line( + $this->xSrc + $this->srcDir * $this->wTick * 0.75, + $this->ySrc, + $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick, + $this->ySrc + $this->wTick / $root2, + 1 + ); + $this->diagram->line( + $this->xSrc + $this->srcDir * $this->wTick * 0.75, + $this->ySrc, + $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick, + $this->ySrc - $this->wTick / $root2, + 1 + ); + $this->diagram->line( + $this->xDest + $this->destDir * $this->wTick / 2, + $this->yDest, + $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick, + $this->yDest + $this->wTick / $root2, + 1 + ); + $this->diagram->line( + $this->xDest + $this->destDir * $this->wTick / 2, + $this->yDest, + $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick, + $this->yDest - $this->wTick / $root2, + 1 + ); + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Eps/TableStatsEps.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Eps/TableStatsEps.php new file mode 100644 index 0000000..904d96a --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Eps/TableStatsEps.php @@ -0,0 +1,183 @@ +_setHeightTable($fontSize); + // setWidth must me after setHeight, because title + // can include table height which changes table width + $this->_setWidthTable($font, $fontSize); + if ($same_wide_width < $this->width) { + $same_wide_width = $this->width; + } + } + + /** + * Displays an error when the table cannot be found. + * + * @return void + */ + protected function showMissingTableError() + { + ExportRelationSchema::dieSchema( + $this->pageNumber, + "EPS", + sprintf(__('The %s table doesn\'t exist!'), $this->tableName) + ); + } + + /** + * Sets the width of the table + * + * @param string $font The font name + * @param integer $fontSize The font size + * + * @return void + * + * @see PMA_EPS + */ + private function _setWidthTable($font, $fontSize) + { + foreach ($this->fields as $field) { + $this->width = max( + $this->width, + $this->font->getStringWidth($field, $font, (int) $fontSize) + ); + } + $this->width += $this->font->getStringWidth( + ' ', + $font, + (int) $fontSize + ); + /* + * it is unknown what value must be added, because + * table title is affected by the table width value + */ + while ($this->width + < $this->font->getStringWidth( + $this->getTitle(), + $font, + (int) $fontSize + )) { + $this->width += 7; + } + } + + /** + * Sets the height of the table + * + * @param integer $fontSize The font size + * + * @return void + */ + private function _setHeightTable($fontSize) + { + $this->heightCell = $fontSize + 4; + $this->height = (count($this->fields) + 1) * $this->heightCell; + } + + /** + * Draw the table + * + * @param boolean $showColor Whether to display color + * + * @return void + * + * @see PMA_EPS,PMA_EPS::line,PMA_EPS::rect + */ + public function tableDraw($showColor) + { + $this->diagram->rect( + $this->x, + $this->y + 12, + $this->width, + $this->heightCell, + 1 + ); + $this->diagram->showXY($this->getTitle(), $this->x + 5, $this->y + 14); + foreach ($this->fields as $field) { + $this->currentCell += $this->heightCell; + $this->diagram->rect( + $this->x, + $this->y + 12 + $this->currentCell, + $this->width, + $this->heightCell, + 1 + ); + $this->diagram->showXY( + $field, + $this->x + 5, + $this->y + 14 + $this->currentCell + ); + } + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/ExportRelationSchema.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/ExportRelationSchema.php new file mode 100644 index 0000000..c5209c4 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/ExportRelationSchema.php @@ -0,0 +1,310 @@ +db = $db; + $this->diagram = $diagram; + $this->setPageNumber($_REQUEST['page_number']); + $this->setOffline(isset($_REQUEST['offline_export'])); + $this->relation = new Relation($GLOBALS['dbi']); + } + + /** + * Set Page Number + * + * @param integer $value Page Number of the document to be created + * + * @return void + */ + public function setPageNumber($value) + { + $this->pageNumber = intval($value); + } + + /** + * Returns the schema page number + * + * @return integer schema page number + */ + public function getPageNumber() + { + return $this->pageNumber; + } + + /** + * Sets showColor + * + * @param boolean $value whether to show colors + * + * @return void + */ + public function setShowColor($value) + { + $this->showColor = $value; + } + + /** + * Returns whether to show colors + * + * @return boolean whether to show colors + */ + public function isShowColor() + { + return $this->showColor; + } + + /** + * Set Table Dimension + * + * @param boolean $value show table co-ordinates or not + * + * @return void + */ + public function setTableDimension($value) + { + $this->tableDimension = $value; + } + + /** + * Returns whether to show table dimensions + * + * @return boolean whether to show table dimensions + */ + public function isTableDimension() + { + return $this->tableDimension; + } + + /** + * Set same width of All Tables + * + * @param boolean $value set same width of all tables or not + * + * @return void + */ + public function setAllTablesSameWidth($value) + { + $this->sameWide = $value; + } + + /** + * Returns whether to use same width for all tables or not + * + * @return boolean whether to use same width for all tables or not + */ + public function isAllTableSameWidth() + { + return $this->sameWide; + } + + /** + * Set Show only keys + * + * @param boolean $value show only keys or not + * + * @return void + * + * @access public + */ + public function setShowKeys($value) + { + $this->showKeys = $value; + } + + /** + * Returns whether to show keys + * + * @return boolean whether to show keys + */ + public function isShowKeys() + { + return $this->showKeys; + } + + /** + * Set Orientation + * + * @param string $value Orientation will be portrait or landscape + * + * @return void + * + * @access public + */ + public function setOrientation($value) + { + $this->orientation = $value == 'P' ? 'P' : 'L'; + } + + /** + * Returns orientation + * + * @return string orientation + */ + public function getOrientation() + { + return $this->orientation; + } + + /** + * Set type of paper + * + * @param string $value paper type can be A4 etc + * + * @return void + * + * @access public + */ + public function setPaper($value) + { + $this->paper = $value; + } + + /** + * Returns the paper size + * + * @return string paper size + */ + public function getPaper() + { + return $this->paper; + } + + /** + * Set whether the document is generated from client side DB + * + * @param boolean $value offline or not + * + * @return void + * + * @access public + */ + public function setOffline($value) + { + $this->offline = $value; + } + + /** + * Returns whether the client side database is used + * + * @return boolean + * + * @access public + */ + public function isOffline() + { + return $this->offline; + } + + /** + * Get the table names from the request + * + * @return array an array of table names + */ + protected function getTablesFromRequest() + { + $tables = []; + if (isset($_POST['t_tbl'])) { + foreach ($_POST['t_tbl'] as $table) { + $tables[] = rawurldecode($table); + } + } + return $tables; + } + + /** + * Returns the file name + * + * @param String $extension file extension + * + * @return string file name + */ + protected function getFileName($extension) + { + $filename = $this->db . $extension; + // Get the name of this page to use as filename + if ($this->pageNumber != -1 && ! $this->offline) { + $_name_sql = 'SELECT page_descr FROM ' + . Util::backquote($GLOBALS['cfgRelation']['db']) . '.' + . Util::backquote($GLOBALS['cfgRelation']['pdf_pages']) + . ' WHERE page_nr = ' . $this->pageNumber; + $_name_rs = $this->relation->queryAsControlUser($_name_sql); + $_name_row = $GLOBALS['dbi']->fetchRow($_name_rs); + $filename = $_name_row[0] . $extension; + } + + return $filename; + } + + /** + * Displays an error message + * + * @param integer $pageNumber ID of the chosen page + * @param string $type Schema Type + * @param string $error_message The error message + * + * @access public + * + * @return void + */ + public static function dieSchema($pageNumber, $type = '', $error_message = '') + { + echo "

" , __("SCHEMA ERROR: ") , $type , "

" , "\n"; + if (! empty($error_message)) { + $error_message = htmlspecialchars($error_message); + } + echo '

' , "\n"; + echo ' ' , $error_message , "\n"; + echo '

' , "\n"; + echo '' , __('Back') , ''; + echo "\n"; + exit; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Pdf/Pdf.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Pdf/Pdf.php new file mode 100644 index 0000000..e02a953 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Pdf/Pdf.php @@ -0,0 +1,422 @@ +_pageNumber = $pageNumber; + $this->_withDoc = $withDoc; + $this->_db = $db; + $this->relation = new Relation($GLOBALS['dbi']); + } + + /** + * Sets the value for margins + * + * @param float $c_margin margin + * + * @return void + */ + public function setCMargin($c_margin) + { + $this->cMargin = $c_margin; + } + + /** + * Sets the scaling factor, defines minimum coordinates and margins + * + * @param float|int $scale The scaling factor + * @param float|int $xMin The minimum X coordinate + * @param float|int $yMin The minimum Y coordinate + * @param float|int $leftMargin The left margin + * @param float|int $topMargin The top margin + * + * @return void + */ + public function setScale( + $scale = 1, + $xMin = 0, + $yMin = 0, + $leftMargin = -1, + $topMargin = -1 + ) { + $this->scale = $scale; + $this->_xMin = $xMin; + $this->_yMin = $yMin; + if ($this->leftMargin != -1) { + $this->leftMargin = $leftMargin; + } + if ($this->topMargin != -1) { + $this->topMargin = $topMargin; + } + } + + /** + * Outputs a scaled cell + * + * @param float|int $w The cell width + * @param float|int $h The cell height + * @param string $txt The text to output + * @param mixed $border Whether to add borders or not + * @param integer $ln Where to put the cursor once the output is done + * @param string $align Align mode + * @param integer $fill Whether to fill the cell with a color or not + * @param string $link Link + * + * @return void + * + * @see TCPDF::Cell() + */ + public function cellScale( + $w, + $h = 0, + $txt = '', + $border = 0, + $ln = 0, + $align = '', + $fill = 0, + $link = '' + ) { + $h /= $this->scale; + $w /= $this->scale; + $this->Cell($w, $h, $txt, $border, $ln, $align, $fill, $link); + } + + /** + * Draws a scaled line + * + * @param float $x1 The horizontal position of the starting point + * @param float $y1 The vertical position of the starting point + * @param float $x2 The horizontal position of the ending point + * @param float $y2 The vertical position of the ending point + * + * @return void + * + * @see TCPDF::Line() + */ + public function lineScale($x1, $y1, $x2, $y2) + { + $x1 = ($x1 - $this->_xMin) / $this->scale + $this->leftMargin; + $y1 = ($y1 - $this->_yMin) / $this->scale + $this->topMargin; + $x2 = ($x2 - $this->_xMin) / $this->scale + $this->leftMargin; + $y2 = ($y2 - $this->_yMin) / $this->scale + $this->topMargin; + $this->Line($x1, $y1, $x2, $y2); + } + + /** + * Sets x and y scaled positions + * + * @param float $x The x position + * @param float $y The y position + * + * @return void + * + * @see TCPDF::SetXY() + */ + public function setXyScale($x, $y) + { + $x = ($x - $this->_xMin) / $this->scale + $this->leftMargin; + $y = ($y - $this->_yMin) / $this->scale + $this->topMargin; + $this->SetXY($x, $y); + } + + /** + * Sets the X scaled positions + * + * @param float $x The x position + * + * @return void + * + * @see TCPDF::SetX() + */ + public function setXScale($x) + { + $x = ($x - $this->_xMin) / $this->scale + $this->leftMargin; + $this->SetX($x); + } + + /** + * Sets the scaled font size + * + * @param float $size The font size (in points) + * + * @return void + * + * @see TCPDF::SetFontSize() + */ + public function setFontSizeScale($size) + { + // Set font size in points + $size /= $this->scale; + $this->SetFontSize($size); + } + + /** + * Sets the scaled line width + * + * @param float $width The line width + * + * @return void + * + * @see TCPDF::SetLineWidth() + */ + public function setLineWidthScale($width) + { + $width /= $this->scale; + $this->SetLineWidth($width); + } + + /** + * This method is used to render the page header. + * + * @return void + * + * @see TCPDF::Header() + */ + // @codingStandardsIgnoreLine + public function Header() + { + // We only show this if we find something in the new pdf_pages table + + // This function must be named "Header" to work with the TCPDF library + if ($this->_withDoc) { + if ($this->_offline || $this->_pageNumber == -1) { + $pg_name = __("PDF export page"); + } else { + $test_query = 'SELECT * FROM ' + . Util::backquote($GLOBALS['cfgRelation']['db']) . '.' + . Util::backquote($GLOBALS['cfgRelation']['pdf_pages']) + . ' WHERE db_name = \'' . $GLOBALS['dbi']->escapeString($this->_db) + . '\' AND page_nr = \'' . $this->_pageNumber . '\''; + $test_rs = $this->relation->queryAsControlUser($test_query); + $pages = @$GLOBALS['dbi']->fetchAssoc($test_rs); + $pg_name = ucfirst($pages['page_descr']); + } + + $this->SetFont($this->_ff, 'B', 14); + $this->Cell(0, 6, $pg_name, 'B', 1, 'C'); + $this->SetFont($this->_ff, ''); + $this->Ln(); + } + } + + /** + * This function must be named "Footer" to work with the TCPDF library + * + * @return void + * + * @see PDF::Footer() + */ + // @codingStandardsIgnoreLine + public function Footer() + { + if ($this->_withDoc) { + parent::Footer(); + } + } + + /** + * Sets widths + * + * @param array $w array of widths + * + * @return void + */ + public function setWidths(array $w) + { + // column widths + $this->widths = $w; + } + + /** + * Generates table row. + * + * @param array $data Data for table + * @param array $links Links for table cells + * + * @return void + */ + public function row(array $data, array $links) + { + // line height + $nb = 0; + $data_cnt = count($data); + for ($i = 0; $i < $data_cnt; $i++) { + $nb = max($nb, $this->numLines($this->widths[$i], $data[$i])); + } + $il = $this->FontSize; + $h = ($il + 1) * $nb; + // page break if necessary + $this->checkPageBreak($h); + // draw the cells + $data_cnt = count($data); + for ($i = 0; $i < $data_cnt; $i++) { + $w = $this->widths[$i]; + // save current position + $x = $this->GetX(); + $y = $this->GetY(); + // draw the border + $this->Rect($x, $y, $w, $h); + if (isset($links[$i])) { + $this->Link($x, $y, $w, $h, $links[$i]); + } + // print text + $this->MultiCell($w, $il + 1, $data[$i], 0, 'L'); + // go to right side + $this->SetXY($x + $w, $y); + } + // go to line + $this->Ln($h); + } + + /** + * Compute number of lines used by a multicell of width w + * + * @param int $w width + * @param string $txt text + * + * @return int + */ + public function numLines($w, $txt) + { + $cw = &$this->CurrentFont['cw']; + if ($w == 0) { + $w = $this->w - $this->rMargin - $this->x; + } + $wmax = ($w - 2 * $this->cMargin) * 1000 / $this->FontSize; + $s = str_replace("\r", '', $txt); + $nb = strlen($s); + if ($nb > 0 && $s[$nb - 1] == "\n") { + $nb--; + } + $sep = -1; + $i = 0; + $j = 0; + $l = 0; + $nl = 1; + while ($i < $nb) { + $c = $s[$i]; + if ($c == "\n") { + $i++; + $sep = -1; + $j = $i; + $l = 0; + $nl++; + continue; + } + if ($c == ' ') { + $sep = $i; + } + $l += isset($cw[mb_ord($c)]) ? $cw[mb_ord($c)] : 0 ; + if ($l > $wmax) { + if ($sep == -1) { + if ($i == $j) { + $i++; + } + } else { + $i = $sep + 1; + } + $sep = -1; + $j = $i; + $l = 0; + $nl++; + } else { + $i++; + } + } + return $nl; + } + + /** + * Set whether the document is generated from client side DB + * + * @param string $value whether offline + * + * @return void + * + * @access private + */ + public function setOffline($value) + { + $this->_offline = $value; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Pdf/PdfRelationSchema.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Pdf/PdfRelationSchema.php new file mode 100644 index 0000000..fa67885 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Pdf/PdfRelationSchema.php @@ -0,0 +1,798 @@ +transformations = new Transformations(); + + $this->setShowGrid(isset($_REQUEST['pdf_show_grid'])); + $this->setShowColor(isset($_REQUEST['pdf_show_color'])); + $this->setShowKeys(isset($_REQUEST['pdf_show_keys'])); + $this->setTableDimension(isset($_REQUEST['pdf_show_table_dimension'])); + $this->setAllTablesSameWidth(isset($_REQUEST['pdf_all_tables_same_width'])); + $this->setWithDataDictionary(isset($_REQUEST['pdf_with_doc'])); + $this->setTableOrder($_REQUEST['pdf_table_order']); + $this->setOrientation($_REQUEST['pdf_orientation']); + $this->setPaper($_REQUEST['pdf_paper']); + + // Initializes a new document + parent::__construct( + $db, + new Pdf( + $this->orientation, + 'mm', + $this->paper, + $this->pageNumber, + $this->_withDoc, + $db + ) + ); + $this->diagram->SetTitle( + sprintf( + __('Schema of the %s database'), + $this->db + ) + ); + $this->diagram->setCMargin(0); + $this->diagram->Open(); + $this->diagram->SetAutoPageBreak('auto'); + $this->diagram->setOffline($this->offline); + + $alltables = $this->getTablesFromRequest(); + if ($this->getTableOrder() == 'name_asc') { + sort($alltables); + } elseif ($this->getTableOrder() == 'name_desc') { + rsort($alltables); + } + + if ($this->_withDoc) { + $this->diagram->SetAutoPageBreak('auto', 15); + $this->diagram->setCMargin(1); + $this->dataDictionaryDoc($alltables); + $this->diagram->SetAutoPageBreak('auto'); + $this->diagram->setCMargin(0); + } + + $this->diagram->AddPage(); + + if ($this->_withDoc) { + $this->diagram->SetLink($this->diagram->PMA_links['RT']['-'], -1); + $this->diagram->Bookmark(__('Relational schema')); + $this->diagram->setAlias('{00}', $this->diagram->PageNo()); + $this->_topMargin = 28; + $this->_bottomMargin = 28; + } + + /* snip */ + foreach ($alltables as $table) { + if (! isset($this->_tables[$table])) { + $this->_tables[$table] = new TableStatsPdf( + $this->diagram, + $this->db, + $table, + null, + $this->pageNumber, + $this->_tablewidth, + $this->showKeys, + $this->tableDimension, + $this->offline + ); + } + if ($this->sameWide) { + $this->_tables[$table]->width = $this->_tablewidth; + } + $this->_setMinMax($this->_tables[$table]); + } + + // Defines the scale factor + $innerWidth = $this->diagram->getPageWidth() - $this->_rightMargin + - $this->_leftMargin; + $innerHeight = $this->diagram->getPageHeight() - $this->_topMargin + - $this->_bottomMargin; + $this->_scale = ceil( + max( + ($this->_xMax - $this->_xMin) / $innerWidth, + ($this->_yMax - $this->_yMin) / $innerHeight + ) * 100 + ) / 100; + + $this->diagram->setScale( + $this->_scale, + $this->_xMin, + $this->_yMin, + $this->_leftMargin, + $this->_topMargin + ); + // Builds and save the PDF document + $this->diagram->setLineWidthScale(0.1); + + if ($this->_showGrid) { + $this->diagram->SetFontSize(10); + $this->_strokeGrid(); + } + $this->diagram->setFontSizeScale(14); + // previous logic was checking master tables and foreign tables + // but I think that looping on every table of the pdf page as a master + // and finding its foreigns is OK (then we can support innodb) + $seen_a_relation = false; + foreach ($alltables as $one_table) { + $exist_rel = $this->relation->getForeigners($this->db, $one_table, '', 'both'); + if (! $exist_rel) { + continue; + } + + $seen_a_relation = true; + foreach ($exist_rel as $master_field => $rel) { + // put the foreign table on the schema only if selected + // by the user + // (do not use array_search() because we would have to + // to do a === false and this is not PHP3 compatible) + if ($master_field != 'foreign_keys_data') { + if (in_array($rel['foreign_table'], $alltables)) { + $this->_addRelation( + $one_table, + $master_field, + $rel['foreign_table'], + $rel['foreign_field'] + ); + } + continue; + } + + foreach ($rel as $one_key) { + if (! in_array($one_key['ref_table_name'], $alltables)) { + continue; + } + + foreach ($one_key['index_list'] as $index => $one_field) { + $this->_addRelation( + $one_table, + $one_field, + $one_key['ref_table_name'], + $one_key['ref_index_list'][$index] + ); + } + } + } // end while + } // end while + + if ($seen_a_relation) { + $this->_drawRelations(); + } + $this->_drawTables(); + } + + /** + * Set Show Grid + * + * @param boolean $value show grid of the document or not + * + * @return void + */ + public function setShowGrid($value) + { + $this->_showGrid = $value; + } + + /** + * Returns whether to show grid + * + * @return boolean whether to show grid + */ + public function isShowGrid() + { + return $this->_showGrid; + } + + /** + * Set Data Dictionary + * + * @param boolean $value show selected database data dictionary or not + * + * @return void + */ + public function setWithDataDictionary($value) + { + $this->_withDoc = $value; + } + + /** + * Return whether to show selected database data dictionary or not + * + * @return boolean whether to show selected database data dictionary or not + */ + public function isWithDataDictionary() + { + return $this->_withDoc; + } + + /** + * Sets the order of the table in data dictionary + * + * @param string $value table order + * + * @return void + */ + public function setTableOrder($value) + { + $this->_tableOrder = $value; + } + + /** + * Returns the order of the table in data dictionary + * + * @return string table order + */ + public function getTableOrder() + { + return $this->_tableOrder; + } + + /** + * Output Pdf Document for download + * + * @return void + */ + public function showOutput() + { + $this->diagram->download($this->getFileName('.pdf')); + } + + /** + * Sets X and Y minimum and maximum for a table cell + * + * @param TableStatsPdf $table The table name of which sets XY co-ordinates + * + * @return void + */ + private function _setMinMax($table) + { + $this->_xMax = max($this->_xMax, $table->x + $table->width); + $this->_yMax = max($this->_yMax, $table->y + $table->height); + $this->_xMin = min($this->_xMin, $table->x); + $this->_yMin = min($this->_yMin, $table->y); + } + + /** + * Defines relation objects + * + * @param string $masterTable The master table name + * @param string $masterField The relation field in the master table + * @param string $foreignTable The foreign table name + * @param string $foreignField The relation field in the foreign table + * + * @return void + * + * @see _setMinMax + */ + private function _addRelation( + $masterTable, + $masterField, + $foreignTable, + $foreignField + ) { + if (! isset($this->_tables[$masterTable])) { + $this->_tables[$masterTable] = new TableStatsPdf( + $this->diagram, + $this->db, + $masterTable, + null, + $this->pageNumber, + $this->_tablewidth, + $this->showKeys, + $this->tableDimension + ); + $this->_setMinMax($this->_tables[$masterTable]); + } + if (! isset($this->_tables[$foreignTable])) { + $this->_tables[$foreignTable] = new TableStatsPdf( + $this->diagram, + $this->db, + $foreignTable, + null, + $this->pageNumber, + $this->_tablewidth, + $this->showKeys, + $this->tableDimension + ); + $this->_setMinMax($this->_tables[$foreignTable]); + } + $this->relations[] = new RelationStatsPdf( + $this->diagram, + $this->_tables[$masterTable], + $masterField, + $this->_tables[$foreignTable], + $foreignField + ); + } + + /** + * Draws the grid + * + * @return void + * + * @see PMA_Schema_PDF + */ + private function _strokeGrid() + { + $gridSize = 10; + $labelHeight = 4; + $labelWidth = 5; + if ($this->_withDoc) { + $topSpace = 6; + $bottomSpace = 15; + } else { + $topSpace = 0; + $bottomSpace = 0; + } + + $this->diagram->SetMargins(0, 0); + $this->diagram->SetDrawColor(200, 200, 200); + // Draws horizontal lines + $innerHeight = $this->diagram->getPageHeight() - $topSpace - $bottomSpace; + for ($l = 0, $size = intval($innerHeight / $gridSize); $l <= $size; $l++) { + $this->diagram->line( + 0, + $l * $gridSize + $topSpace, + $this->diagram->getPageWidth(), + $l * $gridSize + $topSpace + ); + // Avoid duplicates + if ($l > 0 + && $l <= intval(($innerHeight - $labelHeight) / $gridSize) + ) { + $this->diagram->SetXY(0, $l * $gridSize + $topSpace); + $label = (string) sprintf( + '%.0f', + ($l * $gridSize + $topSpace - $this->_topMargin) + * $this->_scale + $this->_yMin + ); + $this->diagram->Cell($labelWidth, $labelHeight, ' ' . $label); + } // end if + } // end for + // Draws vertical lines + for ($j = 0, $size = intval($this->diagram->getPageWidth() / $gridSize); $j <= $size; $j++) { + $this->diagram->line( + $j * $gridSize, + $topSpace, + $j * $gridSize, + $this->diagram->getPageHeight() - $bottomSpace + ); + $this->diagram->SetXY($j * $gridSize, $topSpace); + $label = (string) sprintf( + '%.0f', + ($j * $gridSize - $this->_leftMargin) * $this->_scale + $this->_xMin + ); + $this->diagram->Cell($labelWidth, $labelHeight, $label); + } + } + + /** + * Draws relation arrows + * + * @return void + * + * @see Relation_Stats_Pdf::relationdraw() + */ + private function _drawRelations() + { + $i = 0; + foreach ($this->relations as $relation) { + $relation->relationDraw($this->showColor, $i); + $i++; + } + } + + /** + * Draws tables + * + * @return void + * + * @see Table_Stats_Pdf::tableDraw() + */ + private function _drawTables() + { + foreach ($this->_tables as $table) { + $table->tableDraw(null, $this->_withDoc, $this->showColor); + } + } + + /** + * Generates data dictionary pages. + * + * @param array $alltables Tables to document. + * + * @return void + */ + public function dataDictionaryDoc(array $alltables) + { + // TOC + $this->diagram->AddPage($this->orientation); + $this->diagram->Cell(0, 9, __('Table of contents'), 1, 0, 'C'); + $this->diagram->Ln(15); + $i = 1; + foreach ($alltables as $table) { + $this->diagram->PMA_links['doc'][$table]['-'] + = $this->diagram->AddLink(); + $this->diagram->SetX(10); + // $this->diagram->Ln(1); + $this->diagram->Cell( + 0, + 6, + __('Page number:') . ' {' . sprintf("%02d", $i) . '}', + 0, + 0, + 'R', + 0, + $this->diagram->PMA_links['doc'][$table]['-'] + ); + $this->diagram->SetX(10); + $this->diagram->Cell( + 0, + 6, + $i . ' ' . $table, + 0, + 1, + 'L', + 0, + $this->diagram->PMA_links['doc'][$table]['-'] + ); + // $this->diagram->Ln(1); + $fields = $GLOBALS['dbi']->getColumns($this->db, $table); + foreach ($fields as $row) { + $this->diagram->SetX(20); + $field_name = $row['Field']; + $this->diagram->PMA_links['doc'][$table][$field_name] + = $this->diagram->AddLink(); + //$this->diagram->Cell( + // 0, 6, $field_name, 0, 1, + // 'L', 0, $this->diagram->PMA_links['doc'][$table][$field_name] + //); + } + $i++; + } + $this->diagram->PMA_links['RT']['-'] = $this->diagram->AddLink(); + $this->diagram->SetX(10); + $this->diagram->Cell( + 0, + 6, + __('Page number:') . ' {00}', + 0, + 0, + 'R', + 0, + $this->diagram->PMA_links['RT']['-'] + ); + $this->diagram->SetX(10); + $this->diagram->Cell( + 0, + 6, + $i . ' ' . __('Relational schema'), + 0, + 1, + 'L', + 0, + $this->diagram->PMA_links['RT']['-'] + ); + $z = 0; + foreach ($alltables as $table) { + $z++; + $this->diagram->SetAutoPageBreak(true, 15); + $this->diagram->AddPage($this->orientation); + $this->diagram->Bookmark($table); + $this->diagram->setAlias( + '{' . sprintf("%02d", $z) . '}', + $this->diagram->PageNo() + ); + $this->diagram->PMA_links['RT'][$table]['-'] + = $this->diagram->AddLink(); + $this->diagram->SetLink( + $this->diagram->PMA_links['doc'][$table]['-'], + -1 + ); + $this->diagram->SetFont($this->_ff, 'B', 18); + $this->diagram->Cell( + 0, + 8, + $z . ' ' . $table, + 1, + 1, + 'C', + 0, + $this->diagram->PMA_links['RT'][$table]['-'] + ); + $this->diagram->SetFont($this->_ff, '', 8); + $this->diagram->Ln(); + + $cfgRelation = $this->relation->getRelationsParam(); + $comments = $this->relation->getComments($this->db, $table); + if ($cfgRelation['mimework']) { + $mime_map = $this->transformations->getMime($this->db, $table, true); + } + + /** + * Gets table information + */ + $showtable = $GLOBALS['dbi']->getTable($this->db, $table) + ->getStatusInfo(); + $show_comment = isset($showtable['Comment']) + ? $showtable['Comment'] + : ''; + $create_time = isset($showtable['Create_time']) + ? Util::localisedDate( + strtotime($showtable['Create_time']) + ) + : ''; + $update_time = isset($showtable['Update_time']) + ? Util::localisedDate( + strtotime($showtable['Update_time']) + ) + : ''; + $check_time = isset($showtable['Check_time']) + ? Util::localisedDate( + strtotime($showtable['Check_time']) + ) + : ''; + + /** + * Gets fields properties + */ + $columns = $GLOBALS['dbi']->getColumns($this->db, $table); + + // Find which tables are related with the current one and write it in + // an array + $res_rel = $this->relation->getForeigners($this->db, $table); + + /** + * Displays the comments of the table if MySQL >= 3.23 + */ + + $break = false; + if (! empty($show_comment)) { + $this->diagram->Cell( + 0, + 3, + __('Table comments:') . ' ' . $show_comment, + 0, + 1 + ); + $break = true; + } + + if (! empty($create_time)) { + $this->diagram->Cell( + 0, + 3, + __('Creation:') . ' ' . $create_time, + 0, + 1 + ); + $break = true; + } + + if (! empty($update_time)) { + $this->diagram->Cell( + 0, + 3, + __('Last update:') . ' ' . $update_time, + 0, + 1 + ); + $break = true; + } + + if (! empty($check_time)) { + $this->diagram->Cell( + 0, + 3, + __('Last check:') . ' ' . $check_time, + 0, + 1 + ); + $break = true; + } + + if ($break == true) { + $this->diagram->Cell(0, 3, '', 0, 1); + $this->diagram->Ln(); + } + + $this->diagram->SetFont($this->_ff, 'B'); + if (isset($this->orientation) && $this->orientation == 'L') { + $this->diagram->Cell(25, 8, __('Column'), 1, 0, 'C'); + $this->diagram->Cell(20, 8, __('Type'), 1, 0, 'C'); + $this->diagram->Cell(20, 8, __('Attributes'), 1, 0, 'C'); + $this->diagram->Cell(10, 8, __('Null'), 1, 0, 'C'); + $this->diagram->Cell(20, 8, __('Default'), 1, 0, 'C'); + $this->diagram->Cell(25, 8, __('Extra'), 1, 0, 'C'); + $this->diagram->Cell(45, 8, __('Links to'), 1, 0, 'C'); + + if ($this->paper == 'A4') { + $comments_width = 67; + } else { + // this is really intended for 'letter' + /** + * @todo find optimal width for all formats + */ + $comments_width = 50; + } + $this->diagram->Cell($comments_width, 8, __('Comments'), 1, 0, 'C'); + $this->diagram->Cell(45, 8, 'MIME', 1, 1, 'C'); + $this->diagram->setWidths( + [ + 25, + 20, + 20, + 10, + 20, + 25, + 45, + $comments_width, + 45, + ] + ); + } else { + $this->diagram->Cell(20, 8, __('Column'), 1, 0, 'C'); + $this->diagram->Cell(20, 8, __('Type'), 1, 0, 'C'); + $this->diagram->Cell(20, 8, __('Attributes'), 1, 0, 'C'); + $this->diagram->Cell(10, 8, __('Null'), 1, 0, 'C'); + $this->diagram->Cell(15, 8, __('Default'), 1, 0, 'C'); + $this->diagram->Cell(15, 8, __('Extra'), 1, 0, 'C'); + $this->diagram->Cell(30, 8, __('Links to'), 1, 0, 'C'); + $this->diagram->Cell(30, 8, __('Comments'), 1, 0, 'C'); + $this->diagram->Cell(30, 8, 'MIME', 1, 1, 'C'); + $this->diagram->setWidths([20, 20, 20, 10, 15, 15, 30, 30, 30]); + } + $this->diagram->SetFont($this->_ff, ''); + + foreach ($columns as $row) { + $extracted_columnspec + = Util::extractColumnSpec($row['Type']); + $type = $extracted_columnspec['print_type']; + $attribute = $extracted_columnspec['attribute']; + if (! isset($row['Default'])) { + if ($row['Null'] != '' && $row['Null'] != 'NO') { + $row['Default'] = 'NULL'; + } + } + $field_name = $row['Field']; + // $this->diagram->Ln(); + $this->diagram->PMA_links['RT'][$table][$field_name] + = $this->diagram->AddLink(); + $this->diagram->Bookmark($field_name, 1, -1); + $this->diagram->SetLink( + $this->diagram->PMA_links['doc'][$table][$field_name], + -1 + ); + $foreigner = $this->relation->searchColumnInForeigners($res_rel, $field_name); + + $linksTo = ''; + if ($foreigner) { + $linksTo = '-> '; + if ($foreigner['foreign_db'] != $this->db) { + $linksTo .= $foreigner['foreign_db'] . '.'; + } + $linksTo .= $foreigner['foreign_table'] + . '.' . $foreigner['foreign_field']; + + if (isset($foreigner['on_update'])) { // not set for internal + $linksTo .= "\n" . 'ON UPDATE ' . $foreigner['on_update']; + $linksTo .= "\n" . 'ON DELETE ' . $foreigner['on_delete']; + } + } + + $diagram_row = [ + $field_name, + $type, + $attribute, + ($row['Null'] == '' || $row['Null'] == 'NO') + ? __('No') + : __('Yes'), + isset($row['Default']) ? $row['Default'] : '', + $row['Extra'], + $linksTo, + isset($comments[$field_name]) + ? $comments[$field_name] + : '', + isset($mime_map) && isset($mime_map[$field_name]) + ? str_replace('_', '/', $mime_map[$field_name]['mimetype']) + : '', + ]; + $links = []; + $links[0] = $this->diagram->PMA_links['RT'][$table][$field_name]; + if ($foreigner + && isset($this->diagram->PMA_links['doc'][$foreigner['foreign_table']][$foreigner['foreign_field']]) + ) { + $links[6] = $this->diagram->PMA_links['doc'][$foreigner['foreign_table']][$foreigner['foreign_field']]; + } else { + unset($links[6]); + } + $this->diagram->row($diagram_row, $links); + } // end foreach + $this->diagram->SetFont($this->_ff, '', 14); + } //end each + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Pdf/RelationStatsPdf.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Pdf/RelationStatsPdf.php new file mode 100644 index 0000000..b422ce5 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Pdf/RelationStatsPdf.php @@ -0,0 +1,163 @@ +wTick = 5; + parent::__construct( + $diagram, + $master_table, + $master_field, + $foreign_table, + $foreign_field + ); + } + + /** + * draws relation links and arrows shows foreign key relations + * + * @param boolean $showColor Whether to use one color per relation or not + * @param integer $i The id of the link to draw + * + * @access public + * + * @return void + * + * @see Pdf + */ + public function relationDraw($showColor, $i) + { + if ($showColor) { + $d = $i % 6; + $j = ($i - $d) / 6; + $j %= 4; + $j++; + $case = [ + [ + 1, + 0, + 0, + ], + [ + 0, + 1, + 0, + ], + [ + 0, + 0, + 1, + ], + [ + 1, + 1, + 0, + ], + [ + 1, + 0, + 1, + ], + [ + 0, + 1, + 1, + ], + ]; + list ($a, $b, $c) = $case[$d]; + $e = (1 - ($j - 1) / 6); + $this->diagram->SetDrawColor($a * 255 * $e, $b * 255 * $e, $c * 255 * $e); + } else { + $this->diagram->SetDrawColor(0); + } + $this->diagram->setLineWidthScale(0.2); + $this->diagram->lineScale( + $this->xSrc, + $this->ySrc, + $this->xSrc + $this->srcDir * $this->wTick, + $this->ySrc + ); + $this->diagram->lineScale( + $this->xDest + $this->destDir * $this->wTick, + $this->yDest, + $this->xDest, + $this->yDest + ); + $this->diagram->setLineWidthScale(0.1); + $this->diagram->lineScale( + $this->xSrc + $this->srcDir * $this->wTick, + $this->ySrc, + $this->xDest + $this->destDir * $this->wTick, + $this->yDest + ); + /* + * Draws arrows -> + */ + $root2 = 2 * sqrt(2); + $this->diagram->lineScale( + $this->xSrc + $this->srcDir * $this->wTick * 0.75, + $this->ySrc, + $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick, + $this->ySrc + $this->wTick / $root2 + ); + $this->diagram->lineScale( + $this->xSrc + $this->srcDir * $this->wTick * 0.75, + $this->ySrc, + $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick, + $this->ySrc - $this->wTick / $root2 + ); + + $this->diagram->lineScale( + $this->xDest + $this->destDir * $this->wTick / 2, + $this->yDest, + $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick, + $this->yDest + $this->wTick / $root2 + ); + $this->diagram->lineScale( + $this->xDest + $this->destDir * $this->wTick / 2, + $this->yDest, + $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick, + $this->yDest - $this->wTick / $root2 + ); + $this->diagram->SetDrawColor(0); + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Pdf/TableStatsPdf.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Pdf/TableStatsPdf.php new file mode 100644 index 0000000..999894c --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Pdf/TableStatsPdf.php @@ -0,0 +1,233 @@ +heightCell = 6; + $this->_setHeight(); + /* + * setWidth must me after setHeight, because title + * can include table height which changes table width + */ + $this->_setWidth($fontSize); + if ($sameWideWidth < $this->width) { + $sameWideWidth = $this->width; + } + } + + /** + * Displays an error when the table cannot be found. + * + * @return void + */ + protected function showMissingTableError() + { + ExportRelationSchema::dieSchema( + $this->pageNumber, + "PDF", + sprintf(__('The %s table doesn\'t exist!'), $this->tableName) + ); + } + + /** + * Returns title of the current table, + * title can have the dimensions of the table + * + * @return string + */ + protected function getTitle() + { + $ret = ''; + if ($this->tableDimension) { + $ret = sprintf('%.0fx%0.f', $this->width, $this->height); + } + + return $ret . ' ' . $this->tableName; + } + + /** + * Sets the width of the table + * + * @param integer $fontSize The font size + * + * @access private + * + * @return void + * + * @see PMA_Schema_PDF + */ + private function _setWidth($fontSize) + { + foreach ($this->fields as $field) { + $this->width = max($this->width, $this->diagram->GetStringWidth($field)); + } + $this->width += $this->diagram->GetStringWidth(' '); + $this->diagram->SetFont($this->_ff, 'B', $fontSize); + /* + * it is unknown what value must be added, because + * table title is affected by the table width value + */ + while ($this->width < $this->diagram->GetStringWidth($this->getTitle())) { + $this->width += 5; + } + $this->diagram->SetFont($this->_ff, '', $fontSize); + } + + /** + * Sets the height of the table + * + * @return void + * + * @access private + */ + private function _setHeight() + { + $this->height = (count($this->fields) + 1) * $this->heightCell; + } + + /** + * Do draw the table + * + * @param integer $fontSize The font size + * @param boolean $withDoc Whether to include links to documentation + * @param boolean|integer $setColor Whether to display color + * + * @access public + * + * @return void + * + * @see PMA_Schema_PDF + */ + public function tableDraw($fontSize, $withDoc, $setColor = 0) + { + $this->diagram->setXyScale($this->x, $this->y); + $this->diagram->SetFont($this->_ff, 'B', $fontSize); + if ($setColor) { + $this->diagram->SetTextColor(200); + $this->diagram->SetFillColor(0, 0, 128); + } + if ($withDoc) { + $this->diagram->SetLink( + $this->diagram->PMA_links['RT'][$this->tableName]['-'], + -1 + ); + } else { + $this->diagram->PMA_links['doc'][$this->tableName]['-'] = ''; + } + + $this->diagram->cellScale( + $this->width, + $this->heightCell, + $this->getTitle(), + 1, + 1, + 'C', + $setColor, + $this->diagram->PMA_links['doc'][$this->tableName]['-'] + ); + $this->diagram->setXScale($this->x); + $this->diagram->SetFont($this->_ff, '', $fontSize); + $this->diagram->SetTextColor(0); + $this->diagram->SetFillColor(255); + + foreach ($this->fields as $field) { + if ($setColor) { + if (in_array($field, $this->primary)) { + $this->diagram->SetFillColor(215, 121, 123); + } + if ($field == $this->displayfield) { + $this->diagram->SetFillColor(142, 159, 224); + } + } + if ($withDoc) { + $this->diagram->SetLink( + $this->diagram->PMA_links['RT'][$this->tableName][$field], + -1 + ); + } else { + $this->diagram->PMA_links['doc'][$this->tableName][$field] = ''; + } + + $this->diagram->cellScale( + $this->width, + $this->heightCell, + ' ' . $field, + 1, + 1, + 'L', + $setColor, + $this->diagram->PMA_links['doc'][$this->tableName][$field] + ); + $this->diagram->setXScale($this->x); + $this->diagram->SetFillColor(255); + } + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/RelationStats.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/RelationStats.php new file mode 100644 index 0000000..848fbf4 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/RelationStats.php @@ -0,0 +1,120 @@ +diagram = $diagram; + + $src_pos = $this->_getXy($master_table, $master_field); + $dest_pos = $this->_getXy($foreign_table, $foreign_field); + /* + * [0] is x-left + * [1] is x-right + * [2] is y + */ + $src_left = $src_pos[0] - $this->wTick; + $src_right = $src_pos[1] + $this->wTick; + $dest_left = $dest_pos[0] - $this->wTick; + $dest_right = $dest_pos[1] + $this->wTick; + + $d1 = abs($src_left - $dest_left); + $d2 = abs($src_right - $dest_left); + $d3 = abs($src_left - $dest_right); + $d4 = abs($src_right - $dest_right); + $d = min($d1, $d2, $d3, $d4); + + if ($d == $d1) { + $this->xSrc = $src_pos[0]; + $this->srcDir = -1; + $this->xDest = $dest_pos[0]; + $this->destDir = -1; + } elseif ($d == $d2) { + $this->xSrc = $src_pos[1]; + $this->srcDir = 1; + $this->xDest = $dest_pos[0]; + $this->destDir = -1; + } elseif ($d == $d3) { + $this->xSrc = $src_pos[0]; + $this->srcDir = -1; + $this->xDest = $dest_pos[1]; + $this->destDir = 1; + } else { + $this->xSrc = $src_pos[1]; + $this->srcDir = 1; + $this->xDest = $dest_pos[1]; + $this->destDir = 1; + } + $this->ySrc = $src_pos[2]; + $this->yDest = $dest_pos[2]; + } + + /** + * Gets arrows coordinates + * + * @param TableStats $table The table + * @param string $column The relation column name + * + * @return array Arrows coordinates + * + * @access private + */ + private function _getXy($table, $column) + { + $pos = array_search($column, $table->fields); + + // x_left, x_right, y + return [ + $table->x, + $table->x + $table->width, + $table->y + ($pos + 1.5) * $table->heightCell, + ]; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/SchemaDia.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/SchemaDia.php new file mode 100644 index 0000000..8c328d5 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/SchemaDia.php @@ -0,0 +1,100 @@ +setProperties(); + } + + /** + * Sets the schema export Dia properties + * + * @return void + */ + protected function setProperties() + { + $schemaPluginProperties = new SchemaPluginProperties(); + $schemaPluginProperties->setText('Dia'); + $schemaPluginProperties->setExtension('dia'); + $schemaPluginProperties->setMimeType('application/dia'); + + // create the root group that will be the options field for + // $schemaPluginProperties + // this will be shown as "Format specific options" + $exportSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // specific options main group + $specificOptions = new OptionsPropertyMainGroup("general_opts"); + // add options common to all plugins + $this->addCommonOptions($specificOptions); + + $leaf = new SelectPropertyItem( + "orientation", + __('Orientation') + ); + $leaf->setValues( + [ + 'L' => __('Landscape'), + 'P' => __('Portrait'), + ] + ); + $specificOptions->addProperty($leaf); + + $leaf = new SelectPropertyItem( + "paper", + __('Paper size') + ); + $leaf->setValues($this->getPaperSizeArray()); + $specificOptions->addProperty($leaf); + + // add the main group to the root group + $exportSpecificOptions->addProperty($specificOptions); + + // set the options for the schema export plugin property item + $schemaPluginProperties->setOptions($exportSpecificOptions); + $this->properties = $schemaPluginProperties; + } + + /** + * Exports the schema into DIA format. + * + * @param string $db database name + * + * @return bool Whether it succeeded + */ + public function exportSchema($db) + { + $export = new DiaRelationSchema($db); + $export->showOutput(); + return true; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/SchemaEps.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/SchemaEps.php new file mode 100644 index 0000000..4218376 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/SchemaEps.php @@ -0,0 +1,101 @@ +setProperties(); + } + + /** + * Sets the schema export EPS properties + * + * @return void + */ + protected function setProperties() + { + $schemaPluginProperties = new SchemaPluginProperties(); + $schemaPluginProperties->setText('EPS'); + $schemaPluginProperties->setExtension('eps'); + $schemaPluginProperties->setMimeType('application/eps'); + + // create the root group that will be the options field for + // $schemaPluginProperties + // this will be shown as "Format specific options" + $exportSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // specific options main group + $specificOptions = new OptionsPropertyMainGroup("general_opts"); + // add options common to all plugins + $this->addCommonOptions($specificOptions); + + // create leaf items and add them to the group + $leaf = new BoolPropertyItem( + 'all_tables_same_width', + __('Same width for all tables') + ); + $specificOptions->addProperty($leaf); + + $leaf = new SelectPropertyItem( + "orientation", + __('Orientation') + ); + $leaf->setValues( + [ + 'L' => __('Landscape'), + 'P' => __('Portrait'), + ] + ); + $specificOptions->addProperty($leaf); + + // add the main group to the root group + $exportSpecificOptions->addProperty($specificOptions); + + // set the options for the schema export plugin property item + $schemaPluginProperties->setOptions($exportSpecificOptions); + $this->properties = $schemaPluginProperties; + } + + /** + * Exports the schema into EPS format. + * + * @param string $db database name + * + * @return bool Whether it succeeded + */ + public function exportSchema($db) + { + $export = new EpsRelationSchema($db); + $export->showOutput(); + return true; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/SchemaPdf.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/SchemaPdf.php new file mode 100644 index 0000000..c57fcee --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/SchemaPdf.php @@ -0,0 +1,133 @@ +setProperties(); + } + + /** + * Sets the schema export PDF properties + * + * @return void + */ + protected function setProperties() + { + $schemaPluginProperties = new SchemaPluginProperties(); + $schemaPluginProperties->setText('PDF'); + $schemaPluginProperties->setExtension('pdf'); + $schemaPluginProperties->setMimeType('application/pdf'); + + // create the root group that will be the options field for + // $schemaPluginProperties + // this will be shown as "Format specific options" + $exportSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // specific options main group + $specificOptions = new OptionsPropertyMainGroup("general_opts"); + // add options common to all plugins + $this->addCommonOptions($specificOptions); + + // create leaf items and add them to the group + $leaf = new BoolPropertyItem( + 'all_tables_same_width', + __('Same width for all tables') + ); + $specificOptions->addProperty($leaf); + + $leaf = new SelectPropertyItem( + "orientation", + __('Orientation') + ); + $leaf->setValues( + [ + 'L' => __('Landscape'), + 'P' => __('Portrait'), + ] + ); + $specificOptions->addProperty($leaf); + + $leaf = new SelectPropertyItem( + "paper", + __('Paper size') + ); + $leaf->setValues($this->getPaperSizeArray()); + $specificOptions->addProperty($leaf); + + $leaf = new BoolPropertyItem( + 'show_grid', + __('Show grid') + ); + $specificOptions->addProperty($leaf); + + $leaf = new BoolPropertyItem( + 'with_doc', + __('Data dictionary') + ); + $specificOptions->addProperty($leaf); + + $leaf = new SelectPropertyItem( + "table_order", + __('Order of the tables') + ); + $leaf->setValues( + [ + '' => __('None'), + 'name_asc' => __('Name (Ascending)'), + 'name_desc' => __('Name (Descending)'), + ] + ); + $specificOptions->addProperty($leaf); + + // add the main group to the root group + $exportSpecificOptions->addProperty($specificOptions); + + // set the options for the schema export plugin property item + $schemaPluginProperties->setOptions($exportSpecificOptions); + $this->properties = $schemaPluginProperties; + } + + /** + * Exports the schema into PDF format. + * + * @param string $db database name + * + * @return bool Whether it succeeded + */ + public function exportSchema($db) + { + $export = new PdfRelationSchema($db); + $export->showOutput(); + return true; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/SchemaSvg.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/SchemaSvg.php new file mode 100644 index 0000000..9466f64 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/SchemaSvg.php @@ -0,0 +1,88 @@ +setProperties(); + } + + /** + * Sets the schema export SVG properties + * + * @return void + */ + protected function setProperties() + { + $schemaPluginProperties = new SchemaPluginProperties(); + $schemaPluginProperties->setText('SVG'); + $schemaPluginProperties->setExtension('svg'); + $schemaPluginProperties->setMimeType('application/svg'); + + // create the root group that will be the options field for + // $schemaPluginProperties + // this will be shown as "Format specific options" + $exportSpecificOptions = new OptionsPropertyRootGroup( + "Format Specific Options" + ); + + // specific options main group + $specificOptions = new OptionsPropertyMainGroup("general_opts"); + // add options common to all plugins + $this->addCommonOptions($specificOptions); + + // create leaf items and add them to the group + $leaf = new BoolPropertyItem( + 'all_tables_same_width', + __('Same width for all tables') + ); + $specificOptions->addProperty($leaf); + + // add the main group to the root group + $exportSpecificOptions->addProperty($specificOptions); + + // set the options for the schema export plugin property item + $schemaPluginProperties->setOptions($exportSpecificOptions); + $this->properties = $schemaPluginProperties; + } + + /** + * Exports the schema into SVG format. + * + * @param string $db database name + * + * @return bool Whether it succeeded + */ + public function exportSchema($db) + { + $export = new SvgRelationSchema($db); + $export->showOutput(); + return true; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Svg/RelationStatsSvg.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Svg/RelationStatsSvg.php new file mode 100644 index 0000000..2e323fc --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Svg/RelationStatsSvg.php @@ -0,0 +1,140 @@ +wTick = 10; + parent::__construct( + $diagram, + $master_table, + $master_field, + $foreign_table, + $foreign_field + ); + } + + /** + * draws relation links and arrows shows foreign key relations + * + * @param boolean $showColor Whether to use one color per relation or not + * + * @return void + * @access public + * + * @see PMA_SVG + */ + public function relationDraw($showColor) + { + if ($showColor) { + $listOfColors = [ + '#c00', + '#bbb', + '#333', + '#cb0', + '#0b0', + '#0bf', + '#b0b', + ]; + shuffle($listOfColors); + $color = $listOfColors[0]; + } else { + $color = '#333'; + } + + $this->diagram->printElementLine( + 'line', + $this->xSrc, + $this->ySrc, + $this->xSrc + $this->srcDir * $this->wTick, + $this->ySrc, + 'stroke:' . $color . ';stroke-width:1;' + ); + $this->diagram->printElementLine( + 'line', + $this->xDest + $this->destDir * $this->wTick, + $this->yDest, + $this->xDest, + $this->yDest, + 'stroke:' . $color . ';stroke-width:1;' + ); + $this->diagram->printElementLine( + 'line', + $this->xSrc + $this->srcDir * $this->wTick, + $this->ySrc, + $this->xDest + $this->destDir * $this->wTick, + $this->yDest, + 'stroke:' . $color . ';stroke-width:1;' + ); + $root2 = 2 * sqrt(2); + $this->diagram->printElementLine( + 'line', + $this->xSrc + $this->srcDir * $this->wTick * 0.75, + $this->ySrc, + $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick, + $this->ySrc + $this->wTick / $root2, + 'stroke:' . $color . ';stroke-width:2;' + ); + $this->diagram->printElementLine( + 'line', + $this->xSrc + $this->srcDir * $this->wTick * 0.75, + $this->ySrc, + $this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick, + $this->ySrc - $this->wTick / $root2, + 'stroke:' . $color . ';stroke-width:2;' + ); + $this->diagram->printElementLine( + 'line', + $this->xDest + $this->destDir * $this->wTick / 2, + $this->yDest, + $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick, + $this->yDest + $this->wTick / $root2, + 'stroke:' . $color . ';stroke-width:2;' + ); + $this->diagram->printElementLine( + 'line', + $this->xDest + $this->destDir * $this->wTick / 2, + $this->yDest, + $this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick, + $this->yDest - $this->wTick / $root2, + 'stroke:' . $color . ';stroke-width:2;' + ); + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Svg/Svg.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Svg/Svg.php new file mode 100644 index 0000000..4404574 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Svg/Svg.php @@ -0,0 +1,281 @@ +openMemory(); + /* + * Set indenting using three spaces, + * so output is formatted + */ + + $this->setIndent(true); + $this->setIndentString(' '); + /* + * Create the XML document + */ + + $this->startDocument('1.0', 'UTF-8'); + $this->startDtd( + 'svg', + '-//W3C//DTD SVG 1.1//EN', + 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd' + ); + $this->endDtd(); + } + + /** + * Set document title + * + * @param string $value sets the title text + * + * @return void + */ + public function setTitle($value) + { + $this->title = $value; + } + + /** + * Set document author + * + * @param string $value sets the author + * + * @return void + */ + public function setAuthor($value) + { + $this->author = $value; + } + + /** + * Set document font + * + * @param string $value sets the font e.g Arial, Sans-serif etc + * + * @return void + */ + public function setFont($value) + { + $this->font = $value; + } + + /** + * Get document font + * + * @return string returns the font name + */ + public function getFont() + { + return $this->font; + } + + /** + * Set document font size + * + * @param integer $value sets the font size in pixels + * + * @return void + */ + public function setFontSize($value) + { + $this->fontSize = $value; + } + + /** + * Get document font size + * + * @return integer returns the font size + */ + public function getFontSize() + { + return $this->fontSize; + } + + /** + * Starts RelationStatsSvg Document + * + * svg document starts by first initializing svg tag + * which contains all the attributes and namespace that needed + * to define the svg document + * + * @param integer $width total width of the RelationStatsSvg document + * @param integer $height total height of the RelationStatsSvg document + * @param integer $x min-x of the view box + * @param integer $y min-y of the view box + * + * @return void + * + * @see XMLWriter::startElement(),XMLWriter::writeAttribute() + */ + public function startSvgDoc($width, $height, $x = 0, $y = 0) + { + $this->startElement('svg'); + + if (! is_int($width)) { + $width = intval($width); + } + + if (! is_int($height)) { + $height = intval($height); + } + + if ($x != 0 || $y != 0) { + $this->writeAttribute('viewBox', "$x $y $width $height"); + } + $this->writeAttribute('width', ($width - $x) . 'px'); + $this->writeAttribute('height', ($height - $y) . 'px'); + $this->writeAttribute('xmlns', 'http://www.w3.org/2000/svg'); + $this->writeAttribute('version', '1.1'); + } + + /** + * Ends RelationStatsSvg Document + * + * @return void + * @see XMLWriter::endElement(),XMLWriter::endDocument() + */ + public function endSvgDoc() + { + $this->endElement(); + $this->endDocument(); + } + + /** + * output RelationStatsSvg Document + * + * svg document prompted to the user for download + * RelationStatsSvg document saved in .svg extension and can be + * easily changeable by using any svg IDE + * + * @param string $fileName file name + * + * @return void + * @see XMLWriter::startElement(),XMLWriter::writeAttribute() + */ + public function showOutput($fileName) + { + //ob_get_clean(); + $output = $this->flush(); + Response::getInstance()->disable(); + Core::downloadHeader( + $fileName, + 'image/svg+xml', + strlen($output) + ); + print $output; + } + + /** + * Draws RelationStatsSvg elements + * + * SVG has some predefined shape elements like rectangle & text + * and other elements who have x,y co-ordinates are drawn. + * specify their width and height and can give styles too. + * + * @param string $name RelationStatsSvg element name + * @param int $x The x attr defines the left position of the element + * (e.g. x="0" places the element 0 pixels from the + * left of the browser window) + * @param integer $y The y attribute defines the top position of the + * element (e.g. y="0" places the element 0 pixels + * from the top of the browser window) + * @param int|string $width The width attribute defines the width the element + * @param int|string $height The height attribute defines the height the element + * @param string|null $text The text attribute defines the text the element + * @param string $styles The style attribute defines the style the element + * styles can be defined like CSS styles + * + * @return void + * + * @see XMLWriter::startElement(), XMLWriter::writeAttribute(), + * XMLWriter::text(), XMLWriter::endElement() + */ + public function printElement( + $name, + $x, + $y, + $width = '', + $height = '', + ?string $text = '', + $styles = '' + ) { + $this->startElement($name); + $this->writeAttribute('width', (string) $width); + $this->writeAttribute('height', (string) $height); + $this->writeAttribute('x', (string) $x); + $this->writeAttribute('y', (string) $y); + $this->writeAttribute('style', (string) $styles); + if (isset($text)) { + $this->writeAttribute('font-family', (string) $this->font); + $this->writeAttribute('font-size', $this->fontSize . 'px'); + $this->text($text); + } + $this->endElement(); + } + + /** + * Draws RelationStatsSvg Line element + * + * RelationStatsSvg line element is drawn for connecting the tables. + * arrows are also drawn by specify its start and ending + * co-ordinates + * + * @param string $name RelationStatsSvg element name i.e line + * @param integer $x1 Defines the start of the line on the x-axis + * @param integer $y1 Defines the start of the line on the y-axis + * @param integer $x2 Defines the end of the line on the x-axis + * @param integer $y2 Defines the end of the line on the y-axis + * @param string $styles The style attribute defines the style the element + * styles can be defined like CSS styles + * + * @return void + * + * @see XMLWriter::startElement(), XMLWriter::writeAttribute(), + * XMLWriter::endElement() + */ + public function printElementLine($name, $x1, $y1, $x2, $y2, $styles) + { + $this->startElement($name); + $this->writeAttribute('x1', $x1); + $this->writeAttribute('y1', $y1); + $this->writeAttribute('x2', $x2); + $this->writeAttribute('y2', $y2); + $this->writeAttribute('style', $styles); + $this->endElement(); + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Svg/SvgRelationSchema.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Svg/SvgRelationSchema.php new file mode 100644 index 0000000..9a18b6e --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Svg/SvgRelationSchema.php @@ -0,0 +1,284 @@ +setShowColor(isset($_REQUEST['svg_show_color'])); + $this->setShowKeys(isset($_REQUEST['svg_show_keys'])); + $this->setTableDimension(isset($_REQUEST['svg_show_table_dimension'])); + $this->setAllTablesSameWidth(isset($_REQUEST['svg_all_tables_same_width'])); + + $this->diagram->setTitle( + sprintf( + __('Schema of the %s database - Page %s'), + $this->db, + $this->pageNumber + ) + ); + $this->diagram->SetAuthor('phpMyAdmin ' . PMA_VERSION); + $this->diagram->setFont('Arial'); + $this->diagram->setFontSize(16); + + $alltables = $this->getTablesFromRequest(); + + foreach ($alltables as $table) { + if (! isset($this->_tables[$table])) { + $this->_tables[$table] = new TableStatsSvg( + $this->diagram, + $this->db, + $table, + $this->diagram->getFont(), + $this->diagram->getFontSize(), + $this->pageNumber, + $this->_tablewidth, + $this->showKeys, + $this->tableDimension, + $this->offline + ); + } + + if ($this->sameWide) { + $this->_tables[$table]->width = &$this->_tablewidth; + } + $this->_setMinMax($this->_tables[$table]); + } + + $border = 15; + $this->diagram->startSvgDoc( + $this->_xMax + $border, + $this->_yMax + $border, + $this->_xMin - $border, + $this->_yMin - $border + ); + + $seen_a_relation = false; + foreach ($alltables as $one_table) { + $exist_rel = $this->relation->getForeigners($this->db, $one_table, '', 'both'); + if (! $exist_rel) { + continue; + } + + $seen_a_relation = true; + foreach ($exist_rel as $master_field => $rel) { + /* put the foreign table on the schema only if selected + * by the user + * (do not use array_search() because we would have to + * to do a === false and this is not PHP3 compatible) + */ + if ($master_field != 'foreign_keys_data') { + if (in_array($rel['foreign_table'], $alltables)) { + $this->_addRelation( + $one_table, + $this->diagram->getFont(), + $this->diagram->getFontSize(), + $master_field, + $rel['foreign_table'], + $rel['foreign_field'], + $this->tableDimension + ); + } + continue; + } + + foreach ($rel as $one_key) { + if (! in_array($one_key['ref_table_name'], $alltables)) { + continue; + } + + foreach ($one_key['index_list'] as $index => $one_field) { + $this->_addRelation( + $one_table, + $this->diagram->getFont(), + $this->diagram->getFontSize(), + $one_field, + $one_key['ref_table_name'], + $one_key['ref_index_list'][$index], + $this->tableDimension + ); + } + } + } + } + if ($seen_a_relation) { + $this->_drawRelations(); + } + + $this->_drawTables(); + $this->diagram->endSvgDoc(); + } + + /** + * Output RelationStatsSvg Document for download + * + * @return void + */ + public function showOutput() + { + $this->diagram->showOutput($this->getFileName('.svg')); + } + + /** + * Sets X and Y minimum and maximum for a table cell + * + * @param TableStatsSvg $table The table + * + * @return void + */ + private function _setMinMax($table) + { + $this->_xMax = max($this->_xMax, $table->x + $table->width); + $this->_yMax = max($this->_yMax, $table->y + $table->height); + $this->_xMin = min($this->_xMin, $table->x); + $this->_yMin = min($this->_yMin, $table->y); + } + + /** + * Defines relation objects + * + * @param string $masterTable The master table name + * @param string $font The font face + * @param int $fontSize Font size + * @param string $masterField The relation field in the master table + * @param string $foreignTable The foreign table name + * @param string $foreignField The relation field in the foreign table + * @param boolean $tableDimension Whether to display table position or not + * + * @return void + * + * @see _setMinMax,Table_Stats_Svg::__construct(), + * PhpMyAdmin\Plugins\Schema\Svg\RelationStatsSvg::__construct() + */ + private function _addRelation( + $masterTable, + $font, + $fontSize, + $masterField, + $foreignTable, + $foreignField, + $tableDimension + ) { + if (! isset($this->_tables[$masterTable])) { + $this->_tables[$masterTable] = new TableStatsSvg( + $this->diagram, + $this->db, + $masterTable, + $font, + $fontSize, + $this->pageNumber, + $this->_tablewidth, + false, + $tableDimension + ); + $this->_setMinMax($this->_tables[$masterTable]); + } + if (! isset($this->_tables[$foreignTable])) { + $this->_tables[$foreignTable] = new TableStatsSvg( + $this->diagram, + $this->db, + $foreignTable, + $font, + $fontSize, + $this->pageNumber, + $this->_tablewidth, + false, + $tableDimension + ); + $this->_setMinMax($this->_tables[$foreignTable]); + } + $this->_relations[] = new RelationStatsSvg( + $this->diagram, + $this->_tables[$masterTable], + $masterField, + $this->_tables[$foreignTable], + $foreignField + ); + } + + /** + * Draws relation arrows and lines + * connects master table's master field to + * foreign table's foreign field + * + * @return void + * + * @see Relation_Stats_Svg::relationDraw() + */ + private function _drawRelations() + { + foreach ($this->_relations as $relation) { + $relation->relationDraw($this->showColor); + } + } + + /** + * Draws tables + * + * @return void + * + * @see Table_Stats_Svg::Table_Stats_tableDraw() + */ + private function _drawTables() + { + foreach ($this->_tables as $table) { + $table->tableDraw($this->showColor); + } + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Svg/TableStatsSvg.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Svg/TableStatsSvg.php new file mode 100644 index 0000000..13b1a82 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/Svg/TableStatsSvg.php @@ -0,0 +1,204 @@ +_setHeightTable($fontSize); + // setWidth must me after setHeight, because title + // can include table height which changes table width + $this->_setWidthTable($font, $fontSize); + if ($same_wide_width < $this->width) { + $same_wide_width = $this->width; + } + } + + /** + * Displays an error when the table cannot be found. + * + * @return void + */ + protected function showMissingTableError() + { + ExportRelationSchema::dieSchema( + $this->pageNumber, + "SVG", + sprintf(__('The %s table doesn\'t exist!'), $this->tableName) + ); + } + + /** + * Sets the width of the table + * + * @param string $font The font size + * @param integer $fontSize The font size + * + * @return void + * @access private + * + * @see PMA_SVG + */ + private function _setWidthTable($font, $fontSize) + { + foreach ($this->fields as $field) { + $this->width = max( + $this->width, + $this->font->getStringWidth($field, $font, $fontSize) + ); + } + $this->width += $this->font->getStringWidth(' ', $font, $fontSize); + + /* + * it is unknown what value must be added, because + * table title is affected by the table width value + */ + while ($this->width + < $this->font->getStringWidth($this->getTitle(), $font, $fontSize) + ) { + $this->width += 7; + } + } + + /** + * Sets the height of the table + * + * @param integer $fontSize font size + * + * @return void + */ + private function _setHeightTable($fontSize) + { + $this->heightCell = $fontSize + 4; + $this->height = (count($this->fields) + 1) * $this->heightCell; + } + + /** + * draw the table + * + * @param boolean $showColor Whether to display color + * + * @access public + * @return void + * + * @see PMA_SVG,PMA_SVG::printElement + */ + public function tableDraw($showColor) + { + $this->diagram->printElement( + 'rect', + $this->x, + $this->y, + $this->width, + $this->heightCell, + null, + 'fill:#007;stroke:black;' + ); + $this->diagram->printElement( + 'text', + $this->x + 5, + $this->y + 14, + $this->width, + $this->heightCell, + $this->getTitle(), + 'fill:#fff;' + ); + foreach ($this->fields as $field) { + $this->currentCell += $this->heightCell; + $fillColor = 'none'; + if ($showColor) { + if (in_array($field, $this->primary)) { + $fillColor = '#aea'; + } + if ($field == $this->displayfield) { + $fillColor = 'none'; + } + } + $this->diagram->printElement( + 'rect', + $this->x, + $this->y + $this->currentCell, + $this->width, + $this->heightCell, + null, + 'fill:' . $fillColor . ';stroke:black;' + ); + $this->diagram->printElement( + 'text', + $this->x + 5, + $this->y + 14 + $this->currentCell, + $this->width, + $this->heightCell, + $field, + 'fill:black;' + ); + } + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Schema/TableStats.php b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/TableStats.php new file mode 100644 index 0000000..a3d3b5c --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Schema/TableStats.php @@ -0,0 +1,208 @@ +diagram = $diagram; + $this->db = $db; + $this->pageNumber = $pageNumber; + $this->tableName = $tableName; + + $this->showKeys = $showKeys; + $this->tableDimension = $tableDimension; + + $this->offline = $offline; + + $this->relation = new Relation($GLOBALS['dbi']); + $this->font = new Font(); + + // checks whether the table exists + // and loads fields + $this->validateTableAndLoadFields(); + // load table coordinates + $this->loadCoordinates(); + // loads display field + $this->loadDisplayField(); + // loads primary keys + $this->loadPrimaryKey(); + } + + /** + * Validate whether the table exists. + * + * @return void + */ + protected function validateTableAndLoadFields() + { + $sql = 'DESCRIBE ' . Util::backquote($this->tableName); + $result = $GLOBALS['dbi']->tryQuery( + $sql, + DatabaseInterface::CONNECT_USER, + DatabaseInterface::QUERY_STORE + ); + if (! $result || ! $GLOBALS['dbi']->numRows($result)) { + $this->showMissingTableError(); + } + + if ($this->showKeys) { + $indexes = Index::getFromTable($this->tableName, $this->db); + $all_columns = []; + foreach ($indexes as $index) { + $all_columns = array_merge( + $all_columns, + array_flip(array_keys($index->getColumns())) + ); + } + $this->fields = array_keys($all_columns); + } else { + while ($row = $GLOBALS['dbi']->fetchRow($result)) { + $this->fields[] = $row[0]; + } + } + } + + /** + * Displays an error when the table cannot be found. + * + * @return void + * @abstract + */ + abstract protected function showMissingTableError(); + + /** + * Loads coordinates of a table + * + * @return void + */ + protected function loadCoordinates() + { + if (isset($_POST['t_h'])) { + foreach ($_POST['t_h'] as $key => $value) { + $db = rawurldecode($_POST['t_db'][$key]); + $tbl = rawurldecode($_POST['t_tbl'][$key]); + if ($this->db . '.' . $this->tableName === $db . '.' . $tbl) { + $this->x = (double) $_POST['t_x'][$key]; + $this->y = (double) $_POST['t_y'][$key]; + break; + } + } + } + } + + /** + * Loads the table's display field + * + * @return void + */ + protected function loadDisplayField() + { + $this->displayfield = $this->relation->getDisplayField($this->db, $this->tableName); + } + + /** + * Loads the PRIMARY key. + * + * @return void + */ + protected function loadPrimaryKey() + { + $result = $GLOBALS['dbi']->query( + 'SHOW INDEX FROM ' . Util::backquote($this->tableName) . ';', + DatabaseInterface::CONNECT_USER, + DatabaseInterface::QUERY_STORE + ); + if ($GLOBALS['dbi']->numRows($result) > 0) { + while ($row = $GLOBALS['dbi']->fetchAssoc($result)) { + if ($row['Key_name'] == 'PRIMARY') { + $this->primary[] = $row['Column_name']; + } + } + } + } + + /** + * Returns title of the current table, + * title can have the dimensions/co-ordinates of the table + * + * @return string title of the current table + */ + protected function getTitle() + { + return ($this->tableDimension + ? sprintf('%.0fx%0.f', $this->width, $this->heightCell) + : '' + ) + . ' ' . $this->tableName; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/SchemaPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/SchemaPlugin.php new file mode 100644 index 0000000..12cbd40 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/SchemaPlugin.php @@ -0,0 +1,90 @@ +properties; + } + + /** + * Sets the export plugins properties and is implemented by + * each schema export plugin + * + * @return void + */ + abstract protected function setProperties(); + + /** + * Exports the schema into the specified format. + * + * @param string $db database name + * + * @return bool Whether it succeeded + */ + abstract public function exportSchema($db); + + /** + * Adds export options common to all plugins. + * + * @param OptionsPropertyMainGroup $propertyGroup property group + * + * @return void + */ + protected function addCommonOptions(OptionsPropertyMainGroup $propertyGroup) + { + $leaf = new BoolPropertyItem('show_color', __('Show color')); + $propertyGroup->addProperty($leaf); + $leaf = new BoolPropertyItem('show_keys', __('Only show keys')); + $propertyGroup->addProperty($leaf); + } + + /** + * Returns the array of paper sizes + * + * @return array array of paper sizes + */ + protected function getPaperSizeArray() + { + $ret = []; + foreach ($GLOBALS['cfg']['PDFPageSizes'] as $val) { + $ret[$val] = $val; + } + + return $ret; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/Bool2TextTransformationsPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/Bool2TextTransformationsPlugin.php new file mode 100644 index 0000000..696aa64 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/Bool2TextTransformationsPlugin.php @@ -0,0 +1,69 @@ +getOptions($options, $cfg['DefaultTransformations']['Bool2Text']); + + if ($buffer == '0') { + return $options[1]; // return false label + } + + return $options[0]; // or true one if nonzero + } + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the transformation name of the specific plugin + * + * @return string + */ + public static function getName() + { + return "Bool2Text"; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/CodeMirrorEditorTransformationPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/CodeMirrorEditorTransformationPlugin.php new file mode 100644 index 0000000..bbd1fff --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/CodeMirrorEditorTransformationPlugin.php @@ -0,0 +1,75 @@ +'; + } + $class = 'transform_' . strtolower(static::getName()) . '_editor'; + $html .= ''; + + return $html; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/DateFormatTransformationsPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/DateFormatTransformationsPlugin.php new file mode 100644 index 0000000..32ed494 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/DateFormatTransformationsPlugin.php @@ -0,0 +1,158 @@ +getOptions($options, $cfg['DefaultTransformations']['DateFormat']); + + // further operations on $buffer using the $options[] array. + $options[2] = mb_strtolower($options[2]); + + if (empty($options[1])) { + if ($options[2] == 'local') { + $options[1] = __('%B %d, %Y at %I:%M %p'); + } else { + $options[1] = 'Y-m-d H:i:s'; + } + } + + $timestamp = -1; + + // INT columns will be treated as UNIX timestamps + // and need to be detected before the verification for + // MySQL TIMESTAMP + if ($meta->type == 'int') { + $timestamp = $buffer; + + // Detect TIMESTAMP(6 | 8 | 10 | 12 | 14) + // TIMESTAMP (2 | 4) not supported here. + // (Note: prior to MySQL 4.1, TIMESTAMP has a display size + // for example TIMESTAMP(8) means YYYYMMDD) + } else { + if (preg_match('/^(\d{2}){3,7}$/', $buffer)) { + if (mb_strlen($buffer) == 14 || mb_strlen($buffer) == 8) { + $offset = 4; + } else { + $offset = 2; + } + + $aDate = []; + $aDate['year'] = (int) mb_substr($buffer, 0, $offset); + $aDate['month'] = (int) mb_substr($buffer, $offset, 2); + $aDate['day'] = (int) mb_substr($buffer, $offset + 2, 2); + $aDate['hour'] = (int) mb_substr($buffer, $offset + 4, 2); + $aDate['minute'] = (int) mb_substr($buffer, $offset + 6, 2); + $aDate['second'] = (int) mb_substr($buffer, $offset + 8, 2); + + if (checkdate($aDate['month'], $aDate['day'], $aDate['year'])) { + $timestamp = mktime( + $aDate['hour'], + $aDate['minute'], + $aDate['second'], + $aDate['month'], + $aDate['day'], + $aDate['year'] + ); + } + // If all fails, assume one of the dozens of valid strtime() syntaxes + // (https://www.gnu.org/manual/tar-1.12/html_chapter/tar_7.html) + } else { + if (preg_match('/^[0-9]\d{1,9}$/', $buffer)) { + $timestamp = (int) $buffer; + } else { + $timestamp = strtotime($buffer); + } + } + } + + // If all above failed, maybe it's a Unix timestamp already? + if ($timestamp < 0 && preg_match('/^[1-9]\d{1,9}$/', $buffer)) { + $timestamp = $buffer; + } + + // Reformat a valid timestamp + if ($timestamp >= 0) { + $timestamp -= (int) $options[0] * 60 * 60; + $source = $buffer; + if ($options[2] == 'local') { + $text = Util::localisedDate( + $timestamp, + $options[1] + ); + } elseif ($options[2] == 'utc') { + $text = gmdate($options[1], $timestamp); + } else { + $text = 'INVALID DATE TYPE'; + } + return '' . htmlspecialchars((string) $text) . ''; + } + + return htmlspecialchars((string) $buffer); + } + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the transformation name of the specific plugin + * + * @return string + */ + public static function getName() + { + return "Date Format"; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/DownloadTransformationsPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/DownloadTransformationsPlugin.php new file mode 100644 index 0000000..d6c21d6 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/DownloadTransformationsPlugin.php @@ -0,0 +1,93 @@ + $val) { + if ($val->name == $options[1]) { + $pos = $key; + break; + } + } + if (isset($pos)) { + $cn = $row[$pos]; + } + } + if (empty($cn)) { + $cn = 'binary_file.dat'; + } + } + + return sprintf( + '%s', + $options['wrapper_link'], + htmlspecialchars(urlencode($cn)), + htmlspecialchars($cn), + htmlspecialchars($cn) + ); + } + + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the transformation name of the specific plugin + * + * @return string + */ + public static function getName() + { + return "Download"; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/ExternalTransformationsPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/ExternalTransformationsPlugin.php new file mode 100644 index 0000000..006ee86 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/ExternalTransformationsPlugin.php @@ -0,0 +1,160 @@ +getOptions( + $options, + $cfg['DefaultTransformations']['External'] + ); + + if (isset($allowed_programs[$options[0]])) { + $program = $allowed_programs[$options[0]]; + } else { + $program = $allowed_programs[0]; + } + + // needs PHP >= 4.3.0 + $newstring = ''; + $descriptorspec = [ + 0 => [ + "pipe", + "r", + ], + 1 => [ + "pipe", + "w", + ], + ]; + $process = proc_open($program . ' ' . $options[1], $descriptorspec, $pipes); + if (is_resource($process)) { + fwrite($pipes[0], $buffer); + fclose($pipes[0]); + + while (! feof($pipes[1])) { + $newstring .= fgets($pipes[1], 1024); + } + fclose($pipes[1]); + // we don't currently use the return value + proc_close($process); + } + + if ($options[2] == 1 || $options[2] == '2') { + $retstring = htmlspecialchars($newstring); + } else { + $retstring = $newstring; + } + + return $retstring; + } + + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the transformation name of the specific plugin + * + * @return string + */ + public static function getName() + { + return "External"; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/FormattedTransformationsPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/FormattedTransformationsPlugin.php new file mode 100644 index 0000000..b40ffe3 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/FormattedTransformationsPlugin.php @@ -0,0 +1,65 @@ +'; + } + + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the transformation name of the specific plugin + * + * @return string + */ + public static function getName() + { + return "Formatted"; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/HexTransformationsPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/HexTransformationsPlugin.php new file mode 100644 index 0000000..69ce92d --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/HexTransformationsPlugin.php @@ -0,0 +1,71 @@ +getOptions($options, $cfg['DefaultTransformations']['Hex']); + $options[0] = intval($options[0]); + + if ($options[0] < 1) { + return bin2hex($buffer); + } else { + return chunk_split(bin2hex($buffer), $options[0], ' '); + } + } + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the transformation name of the specific plugin + * + * @return string + */ + public static function getName() + { + return "Hex"; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/ImageLinkTransformationsPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/ImageLinkTransformationsPlugin.php new file mode 100644 index 0000000..efcfdc5 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/ImageLinkTransformationsPlugin.php @@ -0,0 +1,63 @@ +[BLOB]'; + } + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the transformation name of the specific plugin + * + * @return string + */ + public static function getName() + { + return "ImageLink"; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/ImageUploadTransformationsPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/ImageUploadTransformationsPlugin.php new file mode 100644 index 0000000..0e6c0fd --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/ImageUploadTransformationsPlugin.php @@ -0,0 +1,121 @@ +'; + $html .= ''; + $src = 'transformation_wrapper.php' . $options['wrapper_link']; + } + $html .= ''
+            . __('Image preview here') . ''; + $html .= '
'; + + return $html; + } + + /** + * Returns the array of scripts (filename) required for plugin + * initialization and handling + * + * @return array javascripts to be included + */ + public function getScripts() + { + return [ + 'transformations/image_upload.js', + ]; + } + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the transformation name of the specific plugin + * + * @return string + */ + public static function getName() + { + return "Image upload"; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/InlineTransformationsPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/InlineTransformationsPlugin.php new file mode 100644 index 0000000..102dce9 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/InlineTransformationsPlugin.php @@ -0,0 +1,78 @@ +getOptions($options, $cfg['DefaultTransformations']['Inline']); + + if (PMA_IS_GD2) { + return '[' . htmlspecialchars($buffer) . ']'; + } else { + return '[' . htmlspecialchars($buffer) . ']'; + } + } + + + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the transformation name of the specific plugin + * + * @return string + */ + public static function getName() + { + return "Inline"; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/LongToIPv4TransformationsPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/LongToIPv4TransformationsPlugin.php new file mode 100644 index 0000000..03767dc --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/LongToIPv4TransformationsPlugin.php @@ -0,0 +1,66 @@ + 4294967295) { + return htmlspecialchars($buffer); + } + + return long2ip((int) $buffer); + } + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the transformation name of the specific plugin + * + * @return string + */ + public static function getName() + { + return "Long To IPv4"; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/PreApPendTransformationsPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/PreApPendTransformationsPlugin.php new file mode 100644 index 0000000..3dbf9d8 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/PreApPendTransformationsPlugin.php @@ -0,0 +1,68 @@ +getOptions($options, $cfg['DefaultTransformations']['PreApPend']); + + //just prepend and/or append the options to the original text + return htmlspecialchars($options[0]) . htmlspecialchars($buffer) + . htmlspecialchars($options[1]); + } + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the transformation name of the specific plugin + * + * @return string + */ + public static function getName() + { + return "PreApPend"; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/RegexValidationTransformationsPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/RegexValidationTransformationsPlugin.php new file mode 100644 index 0000000..33f0ccd --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/RegexValidationTransformationsPlugin.php @@ -0,0 +1,74 @@ +reset(); + if (! empty($options[0]) && ! preg_match($options[0], $buffer)) { + $this->success = false; + $this->error = sprintf( + __('Validation failed for the input string %s.'), + htmlspecialchars($buffer) + ); + } + + return $buffer; + } + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the transformation name of the specific plugin + * + * @return string + */ + public static function getName() + { + return "Regex Validation"; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/SQLTransformationsPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/SQLTransformationsPlugin.php new file mode 100644 index 0000000..616a24e --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/SQLTransformationsPlugin.php @@ -0,0 +1,62 @@ +getOptions($options, $cfg['DefaultTransformations']['Substring']); + + if ($options[1] != 'all') { + $newtext = mb_substr( + $buffer, + $options[0], + $options[1] + ); + } else { + $newtext = mb_substr($buffer, $options[0]); + } + + $length = mb_strlen($newtext); + $baselength = mb_strlen($buffer); + if ($length != $baselength) { + if ($options[0] != 0) { + $newtext = $options[2] . $newtext; + } + + if (($length + (int) $options[0]) != $baselength) { + $newtext .= $options[2]; + } + } + + return htmlspecialchars($newtext); + } + + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the transformation name of the specific plugin + * + * @return string + */ + public static function getName() + { + return "Substring"; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/TextFileUploadTransformationsPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/TextFileUploadTransformationsPlugin.php new file mode 100644 index 0000000..76021d8 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/TextFileUploadTransformationsPlugin.php @@ -0,0 +1,103 @@ +'; + $html .= ''; + } + $html .= ''; + + return $html; + } + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the transformation name of the specific plugin + * + * @return string + */ + public static function getName() + { + return "Text file upload"; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/TextImageLinkTransformationsPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/TextImageLinkTransformationsPlugin.php new file mode 100644 index 0000000..71fdd6a --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/TextImageLinkTransformationsPlugin.php @@ -0,0 +1,75 @@ +getOptions($options, $cfg['DefaultTransformations']['TextImageLink']); + $url = $options[0] . $buffer; + /* Do not allow javascript links */ + if (! Sanitize::checkLink($url, true, true)) { + return htmlspecialchars($url); + } + return '' + . htmlspecialchars($buffer) . ''; + } + + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the transformation name of the specific plugin + * + * @return string + */ + public static function getName() + { + return "Image Link"; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/TextLinkTransformationsPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/TextLinkTransformationsPlugin.php new file mode 100644 index 0000000..e29ff2c --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Abs/TextLinkTransformationsPlugin.php @@ -0,0 +1,77 @@ +getOptions($options, $cfg['DefaultTransformations']['TextLink']); + $url = (isset($options[0]) ? $options[0] : '') . ((isset($options[2]) && $options[2]) ? '' : $buffer); + /* Do not allow javascript links */ + if (! Sanitize::checkLink($url, true, true)) { + return htmlspecialchars($url); + } + return '' + . htmlspecialchars(isset($options[1]) ? $options[1] : $buffer) + . ''; + } + + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the transformation name of the specific plugin + * + * @return string + */ + public static function getName() + { + return "TextLink"; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Input/Image_JPEG_Upload.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Input/Image_JPEG_Upload.php new file mode 100644 index 0000000..903bfd2 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Input/Image_JPEG_Upload.php @@ -0,0 +1,44 @@ +'; + } + $class = 'transform_IPToBin'; + $html .= ''; + + return $html; + } + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the transformation name of the plugin + * + * @return string + */ + public static function getName() + { + return "IPv4/IPv6 To Binary"; + } + + /** + * Gets the plugin`s MIME type + * + * @return string + */ + public static function getMIMEType() + { + return "Text"; + } + + /** + * Gets the plugin`s MIME subtype + * + * @return string + */ + public static function getMIMESubtype() + { + return "Plain"; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Input/Text_Plain_JsonEditor.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Input/Text_Plain_JsonEditor.php new file mode 100644 index 0000000..17d8cf6 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Input/Text_Plain_JsonEditor.php @@ -0,0 +1,85 @@ +getHeader() + ->getScripts(); + $scripts->addFile('vendor/codemirror/lib/codemirror.js'); + $scripts->addFile('vendor/codemirror/mode/javascript/javascript.js'); + $scripts->addFile('vendor/codemirror/addon/runmode/runmode.js'); + $scripts->addFile('transformations/json.js'); + } + } + + /** + * Gets the transformation description of the specific plugin + * + * @return string + */ + public static function getInfo() + { + return __( + 'Formats text as JSON with syntax highlighting.' + ); + } + + /** + * Does the actual work of each specific transformations plugin. + * + * @param string $buffer text to be transformed + * @param array $options transformation options + * @param stdClass|null $meta meta information + * + * @return string + */ + public function applyTransformation($buffer, array $options = [], ?stdClass $meta = null) + { + return '
' . "\n"
+        . htmlspecialchars($buffer) . "\n"
+        . '
'; + } + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the plugin`s MIME type + * + * @return string + */ + public static function getMIMEType() + { + return "Text"; + } + + /** + * Gets the plugin`s MIME subtype + * + * @return string + */ + public static function getMIMESubtype() + { + return "Plain"; + } + + /** + * Gets the transformation name of the specific plugin + * + * @return string + */ + public static function getName() + { + return "JSON"; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Output/Text_Plain_Sql.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Output/Text_Plain_Sql.php new file mode 100644 index 0000000..2c39e03 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Output/Text_Plain_Sql.php @@ -0,0 +1,60 @@ +getHeader() + ->getScripts(); + $scripts->addFile('vendor/codemirror/lib/codemirror.js'); + $scripts->addFile('vendor/codemirror/mode/sql/sql.js'); + $scripts->addFile('vendor/codemirror/addon/runmode/runmode.js'); + $scripts->addFile('functions.js'); + } + } + + /** + * Gets the plugin`s MIME type + * + * @return string + */ + public static function getMIMEType() + { + return "Text"; + } + + /** + * Gets the plugin`s MIME subtype + * + * @return string + */ + public static function getMIMESubtype() + { + return "Plain"; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Output/Text_Plain_Xml.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Output/Text_Plain_Xml.php new file mode 100644 index 0000000..fc06744 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Output/Text_Plain_Xml.php @@ -0,0 +1,101 @@ +getHeader() + ->getScripts(); + $scripts->addFile('vendor/codemirror/lib/codemirror.js'); + $scripts->addFile('vendor/codemirror/mode/xml/xml.js'); + $scripts->addFile('vendor/codemirror/addon/runmode/runmode.js'); + $scripts->addFile('transformations/xml.js'); + } + } + + /** + * Gets the transformation description of the specific plugin + * + * @return string + */ + public static function getInfo() + { + return __( + 'Formats text as XML with syntax highlighting.' + ); + } + + /** + * Does the actual work of each specific transformations plugin. + * + * @param string $buffer text to be transformed + * @param array $options transformation options + * @param stdClass|null $meta meta information + * + * @return string + */ + public function applyTransformation($buffer, array $options = [], ?stdClass $meta = null) + { + return '
' . "\n"
+        . htmlspecialchars($buffer) . "\n"
+        . '
'; + } + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + /** + * Gets the plugin`s MIME type + * + * @return string + */ + public static function getMIMEType() + { + return "Text"; + } + + /** + * Gets the plugin`s MIME subtype + * + * @return string + */ + public static function getMIMESubtype() + { + return "Plain"; + } + + /** + * Gets the transformation name of the specific plugin + * + * @return string + */ + public static function getName() + { + return "XML"; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/README b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/README new file mode 100644 index 0000000..7d7a125 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/README @@ -0,0 +1,4 @@ +TRANSFORMATION USAGE (Garvin Hicking, ) +==================== + +See the documentation for complete instructions on how to use transformation plugins. diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/TEMPLATE b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/TEMPLATE new file mode 100644 index 0000000..7d2b2ff --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/TEMPLATE @@ -0,0 +1,45 @@ + diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/TEMPLATE_ABSTRACT b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/TEMPLATE_ABSTRACT new file mode 100644 index 0000000..4087ac9 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/TEMPLATE_ABSTRACT @@ -0,0 +1,73 @@ +mimetype contains the original MimeType of the field (i.e. 'text/plain', 'image/jpeg' etc.) + + return $buffer; + } + + /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */ + + + /** + * Gets the TransformationName of the specific plugin + * + * @return string + */ + public static function getName() + { + return "[TransformationName]"; + } +} +?> diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Text_Plain_Link.php b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Text_Plain_Link.php new file mode 100644 index 0000000..298919b --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/Transformations/Text_Plain_Link.php @@ -0,0 +1,43 @@ + $value) { + if (isset($options[$key]) && $options[$key] !== '') { + $result[$key] = $options[$key]; + } else { + $result[$key] = $value; + } + } + + return $result; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/TwoFactor/Application.php b/srcs/phpmyadmin/libraries/classes/Plugins/TwoFactor/Application.php new file mode 100644 index 0000000..15e99b9 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/TwoFactor/Application.php @@ -0,0 +1,162 @@ +_google2fa = new Google2FA(); + } else { + $this->_google2fa = new Google2FA(new SvgImageBackEnd()); + } + $this->_google2fa->setWindow(8); + if (! isset($this->_twofactor->config['settings']['secret'])) { + $this->_twofactor->config['settings']['secret'] = ''; + } + } + + /** + * Get any property of this class + * + * @param string $property name of the property + * + * @return mixed|void if property exist, value of the relevant property + */ + public function __get($property) + { + switch ($property) { + case 'google2fa': + return $this->_google2fa; + } + } + + /** + * Checks authentication, returns true on success + * + * @return boolean + * @throws IncompatibleWithGoogleAuthenticatorException + * @throws InvalidCharactersException + * @throws SecretKeyTooShortException + */ + public function check() + { + $this->_provided = false; + if (! isset($_POST['2fa_code'])) { + return false; + } + $this->_provided = true; + return $this->_google2fa->verifyKey( + $this->_twofactor->config['settings']['secret'], + $_POST['2fa_code'] + ); + } + + /** + * Renders user interface to enter two-factor authentication + * + * @return string HTML code + */ + public function render() + { + return $this->template->render('login/twofactor/application'); + } + + /** + * Renders user interface to configure two-factor authentication + * + * @return string HTML code + */ + public function setup() + { + $secret = $this->_twofactor->config['settings']['secret']; + $inlineUrl = $this->_google2fa->getQRCodeInline( + 'phpMyAdmin (' . $this->getAppId(false) . ')', + $this->_twofactor->user, + $secret + ); + return $this->template->render('login/twofactor/application_configure', [ + 'image' => $inlineUrl, + 'secret' => $secret, + 'has_imagick' => extension_loaded('imagick'), + ]); + } + + /** + * Performs backend configuration + * + * @return boolean + * @throws IncompatibleWithGoogleAuthenticatorException + * @throws InvalidCharactersException + * @throws SecretKeyTooShortException + */ + public function configure() + { + if (! isset($_SESSION['2fa_application_key'])) { + $_SESSION['2fa_application_key'] = $this->_google2fa->generateSecretKey(); + } + $this->_twofactor->config['settings']['secret'] = $_SESSION['2fa_application_key']; + + $result = $this->check(); + if ($result) { + unset($_SESSION['2fa_application_key']); + } + return $result; + } + + /** + * Get user visible name + * + * @return string + */ + public static function getName() + { + return __('Authentication Application (2FA)'); + } + + /** + * Get user visible description + * + * @return string + */ + public static function getDescription() + { + return __('Provides authentication using HOTP and TOTP applications such as FreeOTP, Google Authenticator or Authy.'); + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/TwoFactor/Invalid.php b/srcs/phpmyadmin/libraries/classes/Plugins/TwoFactor/Invalid.php new file mode 100644 index 0000000..31fffc0 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/TwoFactor/Invalid.php @@ -0,0 +1,68 @@ +template->render('login/twofactor/invalid'); + } + + /** + * Get user visible name + * + * @return string + */ + public static function getName() + { + return 'Invalid two-factor authentication'; + } + + /** + * Get user visible description + * + * @return string + */ + public static function getDescription() + { + return 'Error fallback only!'; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/TwoFactor/Key.php b/srcs/phpmyadmin/libraries/classes/Plugins/TwoFactor/Key.php new file mode 100644 index 0000000..cbb118c --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/TwoFactor/Key.php @@ -0,0 +1,213 @@ +_twofactor->config['settings']['registrations'])) { + $this->_twofactor->config['settings']['registrations'] = []; + } + } + + /** + * Returns array of U2F registration objects + * + * @return array + */ + public function getRegistrations() + { + $result = []; + foreach ($this->_twofactor->config['settings']['registrations'] as $index => $data) { + $reg = new stdClass(); + $reg->keyHandle = $data['keyHandle']; + $reg->publicKey = $data['publicKey']; + $reg->certificate = $data['certificate']; + $reg->counter = $data['counter']; + $reg->index = $index; + $result[] = $reg; + } + return $result; + } + + /** + * Checks authentication, returns true on success + * + * @return boolean + */ + public function check() + { + $this->_provided = false; + if (! isset($_POST['u2f_authentication_response']) || ! isset($_SESSION['authenticationRequest'])) { + return false; + } + $this->_provided = true; + try { + $response = json_decode($_POST['u2f_authentication_response']); + if ($response === null) { + return false; + } + $authentication = U2FServer::authenticate( + $_SESSION['authenticationRequest'], + $this->getRegistrations(), + $response + ); + $this->_twofactor->config['settings']['registrations'][$authentication->index]['counter'] = $authentication->counter; + $this->_twofactor->save(); + return true; + } catch (U2FException $e) { + $this->_message = $e->getMessage(); + return false; + } + } + + /** + * Loads needed javascripts into the page + * + * @return void + */ + public function loadScripts() + { + $response = Response::getInstance(); + $scripts = $response->getHeader()->getScripts(); + $scripts->addFile('vendor/u2f-api-polyfill.js'); + $scripts->addFile('u2f.js'); + } + + /** + * Renders user interface to enter two-factor authentication + * + * @return string HTML code + */ + public function render() + { + $request = U2FServer::makeAuthentication( + $this->getRegistrations(), + $this->getAppId(true) + ); + $_SESSION['authenticationRequest'] = $request; + $this->loadScripts(); + return $this->template->render('login/twofactor/key', [ + 'request' => json_encode($request), + 'is_https' => $GLOBALS['PMA_Config']->isHttps(), + ]); + } + + /** + * Renders user interface to configure two-factor authentication + * + * @return string HTML code + * @throws U2FException + * @throws Throwable + * @throws Twig_Error_Loader + * @throws Twig_Error_Runtime + * @throws Twig_Error_Syntax + */ + public function setup() + { + $registrationData = U2FServer::makeRegistration( + $this->getAppId(true), + $this->getRegistrations() + ); + $_SESSION['registrationRequest'] = $registrationData['request']; + + $this->loadScripts(); + return $this->template->render('login/twofactor/key_configure', [ + 'request' => json_encode($registrationData['request']), + 'signatures' => json_encode($registrationData['signatures']), + 'is_https' => $GLOBALS['PMA_Config']->isHttps(), + ]); + } + + /** + * Performs backend configuration + * + * @return boolean + */ + public function configure() + { + $this->_provided = false; + if (! isset($_POST['u2f_registration_response']) || ! isset($_SESSION['registrationRequest'])) { + return false; + } + $this->_provided = true; + try { + $response = json_decode($_POST['u2f_registration_response']); + if ($response === null) { + return false; + } + $registration = U2FServer::register( + $_SESSION['registrationRequest'], + $response + ); + $this->_twofactor->config['settings']['registrations'][] = [ + 'keyHandle' => $registration->getKeyHandle(), + 'publicKey' => $registration->getPublicKey(), + 'certificate' => $registration->getCertificate(), + 'counter' => $registration->getCounter(), + ]; + return true; + } catch (U2FException $e) { + $this->_message = $e->getMessage(); + return false; + } + } + + /** + * Get user visible name + * + * @return string + */ + public static function getName() + { + return __('Hardware Security Key (FIDO U2F)'); + } + + /** + * Get user visible description + * + * @return string + */ + public static function getDescription() + { + return __('Provides authentication using hardware security tokens supporting FIDO U2F.'); + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/TwoFactor/Simple.php b/srcs/phpmyadmin/libraries/classes/Plugins/TwoFactor/Simple.php new file mode 100644 index 0000000..721d602 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/TwoFactor/Simple.php @@ -0,0 +1,68 @@ +template->render('login/twofactor/simple'); + } + + /** + * Get user visible name + * + * @return string + */ + public static function getName() + { + return __('Simple two-factor authentication'); + } + + /** + * Get user visible description + * + * @return string + */ + public static function getDescription() + { + return __('For testing purposes only!'); + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/TwoFactorPlugin.php b/srcs/phpmyadmin/libraries/classes/Plugins/TwoFactorPlugin.php new file mode 100644 index 0000000..23534a1 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/TwoFactorPlugin.php @@ -0,0 +1,183 @@ +_twofactor = $twofactor; + $this->_provided = false; + $this->_message = ''; + $this->template = new Template(); + } + + /** + * Returns authentication error message + * + * @return string + */ + public function getError() + { + if ($this->_provided) { + if (! empty($this->_message)) { + return Message::rawError( + sprintf(__('Two-factor authentication failed: %s'), $this->_message) + )->getDisplay(); + } + return Message::rawError( + __('Two-factor authentication failed.') + )->getDisplay(); + } + return ''; + } + + /** + * Checks authentication, returns true on success + * + * @return boolean + */ + public function check() + { + return true; + } + + /** + * Renders user interface to enter two-factor authentication + * + * @return string HTML code + */ + public function render() + { + return ''; + } + + /** + * Renders user interface to configure two-factor authentication + * + * @return string HTML code + */ + public function setup() + { + return ''; + } + + /** + * Performs backend configuration + * + * @return boolean + */ + public function configure() + { + return true; + } + + /** + * Get user visible name + * + * @return string + */ + public static function getName() + { + return __('No Two-Factor Authentication'); + } + + /** + * Get user visible description + * + * @return string + */ + public static function getDescription() + { + return __('Login using password only.'); + } + + /** + * Return an applicaiton ID + * + * Either hostname or hostname with scheme. + * + * @param boolean $return_url Whether to generate URL + * + * @return string + */ + public function getAppId($return_url) + { + /** @var Config $PMA_Config */ + global $PMA_Config; + + $url = $PMA_Config->get('PmaAbsoluteUri'); + $parsed = []; + if (! empty($url)) { + $parsed = parse_url($url); + } + if (empty($parsed['scheme'])) { + $parsed['scheme'] = $PMA_Config->isHttps() ? 'https' : 'http'; + } + if (empty($parsed['host'])) { + $parsed['host'] = Core::getenv('HTTP_HOST'); + } + if ($return_url) { + return $parsed['scheme'] . '://' . $parsed['host'] . (! empty($parsed['port']) ? ':' . $parsed['port'] : ''); + } else { + return $parsed['host']; + } + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Plugins/UploadInterface.php b/srcs/phpmyadmin/libraries/classes/Plugins/UploadInterface.php new file mode 100644 index 0000000..3839aab --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Plugins/UploadInterface.php @@ -0,0 +1,35 @@ +upload plugins + * + * @package PhpMyAdmin + */ +declare(strict_types=1); + +namespace PhpMyAdmin\Plugins; + +/** + * Provides a common interface that will have to implemented by all of the + * import->upload plugins. + * + * @package PhpMyAdmin + */ +interface UploadInterface +{ + /** + * Gets the specific upload ID Key + * + * @return string ID Key + */ + public static function getIdKey(); + + /** + * Returns upload status. + * + * @param string $id upload id + * + * @return array|null + */ + public static function getUploadStatus($id); +} -- cgit