Aucune description

authorize-application.php 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /**
  3. * Authorize Application Screen
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /** WordPress Administration Bootstrap */
  9. require_once __DIR__ . '/admin.php';
  10. $error = null;
  11. $new_password = '';
  12. // This is the no-js fallback script. Generally this will all be handled by `auth-app.js`.
  13. if ( isset( $_POST['action'] ) && 'authorize_application_password' === $_POST['action'] ) {
  14. check_admin_referer( 'authorize_application_password' );
  15. $success_url = $_POST['success_url'];
  16. $reject_url = $_POST['reject_url'];
  17. $app_name = $_POST['app_name'];
  18. $app_id = $_POST['app_id'];
  19. $redirect = '';
  20. if ( isset( $_POST['reject'] ) ) {
  21. if ( $reject_url ) {
  22. $redirect = $reject_url;
  23. } else {
  24. $redirect = admin_url();
  25. }
  26. } elseif ( isset( $_POST['approve'] ) ) {
  27. $created = WP_Application_Passwords::create_new_application_password(
  28. get_current_user_id(),
  29. array(
  30. 'name' => $app_name,
  31. 'app_id' => $app_id,
  32. )
  33. );
  34. if ( is_wp_error( $created ) ) {
  35. $error = $created;
  36. } else {
  37. list( $new_password ) = $created;
  38. if ( $success_url ) {
  39. $redirect = add_query_arg(
  40. array(
  41. 'site_url' => urlencode( site_url() ),
  42. 'user_login' => urlencode( wp_get_current_user()->user_login ),
  43. 'password' => urlencode( $new_password ),
  44. ),
  45. $success_url
  46. );
  47. }
  48. }
  49. }
  50. if ( $redirect ) {
  51. // Explicitly not using wp_safe_redirect b/c sends to arbitrary domain.
  52. wp_redirect( $redirect );
  53. exit;
  54. }
  55. }
  56. $title = __( 'Authorize Application' );
  57. $app_name = ! empty( $_REQUEST['app_name'] ) ? $_REQUEST['app_name'] : '';
  58. $app_id = ! empty( $_REQUEST['app_id'] ) ? $_REQUEST['app_id'] : '';
  59. $success_url = ! empty( $_REQUEST['success_url'] ) ? $_REQUEST['success_url'] : null;
  60. if ( ! empty( $_REQUEST['reject_url'] ) ) {
  61. $reject_url = $_REQUEST['reject_url'];
  62. } elseif ( $success_url ) {
  63. $reject_url = add_query_arg( 'success', 'false', $success_url );
  64. } else {
  65. $reject_url = null;
  66. }
  67. $user = wp_get_current_user();
  68. $request = compact( 'app_name', 'app_id', 'success_url', 'reject_url' );
  69. $is_valid = wp_is_authorize_application_password_request_valid( $request, $user );
  70. if ( is_wp_error( $is_valid ) ) {
  71. wp_die(
  72. __( 'The Authorize Application request is not allowed.' ) . ' ' . implode( ' ', $is_valid->get_error_messages() ),
  73. __( 'Cannot Authorize Application' )
  74. );
  75. }
  76. if ( wp_is_site_protected_by_basic_auth( 'front' ) ) {
  77. wp_die(
  78. __( 'Your website appears to use Basic Authentication, which is not currently compatible with Application Passwords.' ),
  79. __( 'Cannot Authorize Application' ),
  80. array(
  81. 'response' => 501,
  82. 'link_text' => __( 'Go Back' ),
  83. 'link_url' => $reject_url ? add_query_arg( 'error', 'disabled', $reject_url ) : admin_url(),
  84. )
  85. );
  86. }
  87. if ( ! wp_is_application_passwords_available_for_user( $user ) ) {
  88. if ( wp_is_application_passwords_available() ) {
  89. $message = __( 'Application passwords are not available for your account. Please contact the site administrator for assistance.' );
  90. } else {
  91. $message = __( 'Application passwords are not available.' );
  92. }
  93. wp_die(
  94. $message,
  95. __( 'Cannot Authorize Application' ),
  96. array(
  97. 'response' => 501,
  98. 'link_text' => __( 'Go Back' ),
  99. 'link_url' => $reject_url ? add_query_arg( 'error', 'disabled', $reject_url ) : admin_url(),
  100. )
  101. );
  102. }
  103. wp_enqueue_script( 'auth-app' );
  104. wp_localize_script(
  105. 'auth-app',
  106. 'authApp',
  107. array(
  108. 'site_url' => site_url(),
  109. 'user_login' => $user->user_login,
  110. 'success' => $success_url,
  111. 'reject' => $reject_url ? $reject_url : admin_url(),
  112. )
  113. );
  114. require_once ABSPATH . 'wp-admin/admin-header.php';
  115. ?>
  116. <div class="wrap">
  117. <h1><?php echo esc_html( $title ); ?></h1>
  118. <?php if ( is_wp_error( $error ) ) : ?>
  119. <div class="notice notice-error"><p><?php echo $error->get_error_message(); ?></p></div>
  120. <?php endif; ?>
  121. <div class="card auth-app-card">
  122. <h2 class="title"><?php _e( 'An application would like to connect to your account.' ); ?></h2>
  123. <?php if ( $app_name ) : ?>
  124. <p>
  125. <?php
  126. printf(
  127. /* translators: %s: Application name. */
  128. __( 'Would you like to give the application identifying itself as %s access to your account? You should only do this if you trust the app in question.' ),
  129. '<strong>' . esc_html( $app_name ) . '</strong>'
  130. );
  131. ?>
  132. </p>
  133. <?php else : ?>
  134. <p><?php _e( 'Would you like to give this application access to your account? You should only do this if you trust the app in question.' ); ?></p>
  135. <?php endif; ?>
  136. <?php
  137. if ( is_multisite() ) {
  138. $blogs = get_blogs_of_user( $user->ID, true );
  139. $blogs_count = count( $blogs );
  140. if ( $blogs_count > 1 ) {
  141. ?>
  142. <p>
  143. <?php
  144. printf(
  145. /* translators: 1: URL to my-sites.php, 2: Number of sites the user has. */
  146. _n(
  147. 'This will grant access to <a href="%1$s">the %2$s site in this installation that you have permissions on</a>.',
  148. 'This will grant access to <a href="%1$s">all %2$s sites in this installation that you have permissions on</a>.',
  149. $blogs_count
  150. ),
  151. admin_url( 'my-sites.php' ),
  152. number_format_i18n( $blogs_count )
  153. );
  154. ?>
  155. </p>
  156. <?php
  157. }
  158. }
  159. ?>
  160. <?php if ( $new_password ) : ?>
  161. <div class="notice notice-success notice-alt below-h2">
  162. <p class="application-password-display">
  163. <label for="new-application-password-value">
  164. <?php
  165. printf(
  166. /* translators: %s: Application name. */
  167. esc_html__( 'Your new password for %s is:' ),
  168. '<strong>' . esc_html( $app_name ) . '</strong>'
  169. );
  170. ?>
  171. </label>
  172. <input id="new-application-password-value" type="text" class="code" readonly="readonly" value="<?php esc_attr( WP_Application_Passwords::chunk_password( $new_password ) ); ?>" />
  173. </p>
  174. <p><?php _e( 'Be sure to save this in a safe location. You will not be able to retrieve it.' ); ?></p>
  175. </div>
  176. <?php
  177. /**
  178. * Fires in the Authorize Application Password new password section in the no-JS version.
  179. *
  180. * In most cases, this should be used in combination with the {@see 'wp_application_passwords_approve_app_request_success'}
  181. * action to ensure that both the JS and no-JS variants are handled.
  182. *
  183. * @since 5.6.0
  184. * @since 5.6.1 Corrected action name and signature.
  185. *
  186. * @param string $new_password The newly generated application password.
  187. * @param array $request The array of request data. All arguments are optional and may be empty.
  188. * @param WP_User $user The user authorizing the application.
  189. */
  190. do_action( 'wp_authorize_application_password_form_approved_no_js', $new_password, $request, $user );
  191. ?>
  192. <?php else : ?>
  193. <form action="<?php echo esc_url( admin_url( 'authorize-application.php' ) ); ?>" method="post" class="form-wrap">
  194. <?php wp_nonce_field( 'authorize_application_password' ); ?>
  195. <input type="hidden" name="action" value="authorize_application_password" />
  196. <input type="hidden" name="app_id" value="<?php echo esc_attr( $app_id ); ?>" />
  197. <input type="hidden" name="success_url" value="<?php echo esc_url( $success_url ); ?>" />
  198. <input type="hidden" name="reject_url" value="<?php echo esc_url( $reject_url ); ?>" />
  199. <div class="form-field">
  200. <label for="app_name"><?php _e( 'New Application Password Name' ); ?></label>
  201. <input type="text" id="app_name" name="app_name" value="<?php echo esc_attr( $app_name ); ?>" placeholder="<?php esc_attr_e( 'WordPress App on My Phone' ); ?>" required />
  202. </div>
  203. <?php
  204. /**
  205. * Fires in the Authorize Application Password form before the submit buttons.
  206. *
  207. * @since 5.6.0
  208. *
  209. * @param array $request {
  210. * The array of request data. All arguments are optional and may be empty.
  211. *
  212. * @type string $app_name The suggested name of the application.
  213. * @type string $success_url The url the user will be redirected to after approving the application.
  214. * @type string $reject_url The url the user will be redirected to after rejecting the application.
  215. * }
  216. * @param WP_User $user The user authorizing the application.
  217. */
  218. do_action( 'wp_authorize_application_password_form', $request, $user );
  219. ?>
  220. <?php
  221. submit_button(
  222. __( 'Yes, I approve of this connection.' ),
  223. 'primary',
  224. 'approve',
  225. false,
  226. array(
  227. 'aria-describedby' => 'description-approve',
  228. )
  229. );
  230. ?>
  231. <p class="description" id="description-approve">
  232. <?php
  233. if ( $success_url ) {
  234. printf(
  235. /* translators: %s: The URL the user is being redirected to. */
  236. __( 'You will be sent to %s' ),
  237. '<strong><kbd>' . esc_html(
  238. add_query_arg(
  239. array(
  240. 'site_url' => site_url(),
  241. 'user_login' => $user->user_login,
  242. 'password' => '[------]',
  243. ),
  244. $success_url
  245. )
  246. ) . '</kbd></strong>'
  247. );
  248. } else {
  249. _e( 'You will be given a password to manually enter into the application in question.' );
  250. }
  251. ?>
  252. </p>
  253. <?php
  254. submit_button(
  255. __( 'No, I do not approve of this connection.' ),
  256. 'secondary',
  257. 'reject',
  258. false,
  259. array(
  260. 'aria-describedby' => 'description-reject',
  261. )
  262. );
  263. ?>
  264. <p class="description" id="description-reject">
  265. <?php
  266. if ( $reject_url ) {
  267. printf(
  268. /* translators: %s: The URL the user is being redirected to. */
  269. __( 'You will be sent to %s' ),
  270. '<strong><kbd>' . esc_html( $reject_url ) . '</kbd></strong>'
  271. );
  272. } else {
  273. _e( 'You will be returned to the WordPress Dashboard, and no changes will be made.' );
  274. }
  275. ?>
  276. </p>
  277. </form>
  278. <?php endif; ?>
  279. </div>
  280. </div>
  281. <?php
  282. require_once ABSPATH . 'wp-admin/admin-footer.php';