Code_Manager.php
4 years ago
Code_Manager_Dashboard.php
4 years ago
Code_Manager_Export.php
4 years ago
Code_Manager_Form.php
4 years ago
Code_Manager_Import.php
4 years ago
Code_Manager_Import_File.php
4 years ago
Code_Manager_List.php
4 years ago
Code_Manager_List_View.php
4 years ago
Code_Manager_Model.php
4 years ago
Code_Manager_Preview.php
4 years ago
Code_Manager_Settings.php
4 years ago
Code_Manager_Tabs.php
4 years ago
Message_Box.php
4 years ago
WP_List_Table.php
4 years ago
Code_Manager_Import_File.php
83 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Code_Manager { |
| 4 | |
| 5 | /** |
| 6 | * Class Code_Manager_Import_File |
| 7 | * |
| 8 | * Performs import, called from Code_Manager_Import. |
| 9 | * |
| 10 | * @author Peter Schulz |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | class Code_Manager_Import_File { |
| 14 | |
| 15 | protected $file_pointer; |
| 16 | |
| 17 | public function __construct( $file_path ) { |
| 18 | $this->file_pointer = fopen( $file_path, 'rb' ); |
| 19 | } |
| 20 | |
| 21 | public function __destruct() { |
| 22 | fclose( $this->file_pointer ); |
| 23 | } |
| 24 | |
| 25 | public function import() { |
| 26 | global $wpdb; |
| 27 | |
| 28 | $suppress = $wpdb->suppress_errors(); |
| 29 | $rows = 0; |
| 30 | $rows_failed = 0; |
| 31 | |
| 32 | if ( false !== $this->file_pointer ) { |
| 33 | while ( ! feof( $this->file_pointer ) ) { |
| 34 | $sql = fgets( $this->file_pointer ); |
| 35 | if ( '--' !== substr( $sql, 0, 2 ) ) { |
| 36 | if ( ";\n" === substr( $sql, -2 ) ) { |
| 37 | $rows ++; |
| 38 | // Write file content to array for security check |
| 39 | $dml_check = explode( ' ', substr( trim( $sql ), 0, 150 ) ); |
| 40 | if ( ! isset( $dml_check[0] ) || ! isset( $dml_check[1] ) ) { |
| 41 | // No content. |
| 42 | $rows_failed ++; |
| 43 | } else { |
| 44 | // Check first two words (must be insert into, no other statements allowed) |
| 45 | if ( strtolower( $dml_check[0] . $dml_check[1] ) !== 'insertinto' ) { |
| 46 | // Only insert allowed |
| 47 | $rows_failed ++; |
| 48 | } else { |
| 49 | // Check table name (only insert into code manager table allowed) |
| 50 | if ( ! stristr( $dml_check[2], '{wp_prefix}' . Code_Manager_Model::BASE_TABLE_NAME ) ) { |
| 51 | $rows_failed ++; |
| 52 | } else { |
| 53 | // Insert row |
| 54 | if ( false === $wpdb->query( str_replace( '{wp_prefix}', $wpdb->prefix, $sql ) ) ) { |
| 55 | $rows_failed ++; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | $wpdb->suppress_errors( $suppress ); |
| 66 | |
| 67 | $msg = "Imported " . ( $rows - $rows_failed ) . " rows"; |
| 68 | if ( $rows_failed > 0 ) { |
| 69 | $msg .= " ($rows_failed failed)."; |
| 70 | } else { |
| 71 | $msg .= "."; |
| 72 | } |
| 73 | $msg = new Message_Box( |
| 74 | [ |
| 75 | 'message_text' => $msg, |
| 76 | ] |
| 77 | ); |
| 78 | $msg->box(); |
| 79 | } |
| 80 | |
| 81 | } |
| 82 | |
| 83 | } |