| 1 | <?php |
| 2 | namespace WordPressdotorg\Plugin_Directory\API\Routes; |
| 3 | use WordPressdotorg\Plugin_Directory\Plugin_Directory; |
| 4 | use WordPressdotorg\Plugin_Directory\API\Base; |
| 5 | |
| 6 | /** |
| 7 | * SVN is a fascinating system, plugins.svn.wordpress.org is a fascinating implementation of it. |
| 8 | * |
| 9 | * We need to generate the SVN access file from the database, so that the SVN server knows who has permission. |
| 10 | * |
| 11 | * This API is not designed for public usage. |
| 12 | * |
| 13 | * @package WordPressdotorg_Plugin_Directory |
| 14 | */ |
| 15 | class SVN_Access extends Base { |
| 16 | |
| 17 | private $svn_access; |
| 18 | private $svn_access_table; |
| 19 | |
| 20 | function __construct() { |
| 21 | $this->svn_access = array(); |
| 22 | $this->svn_access_table = 'plugin_2_svn_access'; |
| 23 | |
| 24 | register_rest_route( 'plugins/v1', '/svn-access', array( |
| 25 | 'methods' => \WP_REST_Server::READABLE, |
| 26 | 'callback' => array( $this, 'generate_svn_access' ), |
| 27 | 'permission_callback' => array( $this, 'permission_check_internal_api_bearer' ), |
| 28 | ) ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Generates and prints the SVN access file for plugins.svn. |
| 33 | * |
| 34 | * Rather than returning a value, the file is echo'd directly to STDOUT, so it can be piped |
| 35 | * directly into a file. It exit()'s immediately. |
| 36 | * |
| 37 | * @param \WP_REST_Request $request The Rest API Request. |
| 38 | * |
| 39 | * @return bool false This method will return false if the SVN access file couldn't be generated. |
| 40 | */ |
| 41 | public function generate_svn_access( $request ) { |
| 42 | $this->load_svn_access(); |
| 43 | |
| 44 | if ( empty( $this->svn_access ) ) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | foreach ( $this->svn_access as $slug => $users ) { |
| 49 | $slug = ltrim( $slug, '/' ); |
| 50 | echo "\n[/$slug]\n"; |
| 51 | |
| 52 | foreach ( $users as $user => $access ) { |
| 53 | echo "$user = $access\n"; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | exit(); |
| 58 | } |
| 59 | |
| 60 | private function load_svn_access() { |
| 61 | global $wpdb; |
| 62 | |
| 63 | $svn_access = (array) $wpdb->get_results( "SELECT * FROM {$this->svn_access_table}" ); |
| 64 | |
| 65 | foreach ( $svn_access as $svn_access ) { |
| 66 | if ( ! isset( $this->svn_access[ $svn_access->path ] ) ) { |
| 67 | $this->svn_access[ $svn_access->path ] = array(); |
| 68 | } |
| 69 | |
| 70 | $this->svn_access[ $svn_access->path ][ $svn_access->user ] = $svn_access->access; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | |
| 75 | } |