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