Completed
Push — master ( 01dd68...b9c4ee )
by Jamie
03:37
created

FrmXMLController::form()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 24
rs 8.9714
cc 2
eloc 15
nc 2
nop 2
1
<?php
2
3
class FrmXMLController {
4
5
    public static function menu() {
6
        add_submenu_page('formidable', 'Formidable | '. __( 'Import/Export', 'formidable' ), __( 'Import/Export', 'formidable' ), 'frm_edit_forms', 'formidable-import', 'FrmXMLController::route');
7
    }
8
9
    public static function add_default_templates() {
10
		if ( ! function_exists( 'libxml_disable_entity_loader' ) ) {
11
    		// XML import is not enabled on your server
12
    		return;
13
    	}
14
15
        $set_err = libxml_use_internal_errors(true);
16
        $loader = libxml_disable_entity_loader( true );
17
18
		$files = apply_filters( 'frm_default_templates_files', array( FrmAppHelper::plugin_path() . '/classes/views/xml/default-templates.xml' ) );
19
20
        foreach ( (array) $files as $file ) {
21
            FrmXMLHelper::import_xml($file);
22
            unset($file);
23
        }
24
        /*
25
        if(is_wp_error($result))
26
            $errors[] = $result->get_error_message();
27
        else if($result)
28
            $message = $result;
29
        */
30
31
        unset( $files );
32
33
        libxml_use_internal_errors( $set_err );
34
    	libxml_disable_entity_loader( $loader );
35
    }
36
37
    public static function route() {
38
        $action = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action';
1 ignored issue
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
39
		$action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' );
40
		if ( $action == 'import_xml' ) {
41
            return self::import_xml();
42
		} else if ( $action == 'export_xml' ) {
43
            return self::export_xml();
44
        } else {
45
            if ( apply_filters( 'frm_xml_route', true, $action ) ) {
46
                return self::form();
47
            }
48
        }
49
    }
50
51
    public static function form( $errors = array(), $message = '' ) {
52
		$where = array(
53
			'parent_form_id' => array( null, 0 ),
54
			'status' => array( null, '', 'published' )
55
		);
56
		$forms = FrmForm::getAll( $where, 'name' );
57
58
        $export_types = apply_filters( 'frm_xml_export_types',
59
            array( 'forms' => __( 'Forms', 'formidable' ) )
60
        );
61
62
        $export_format = apply_filters( 'frm_export_formats', array(
63
            'xml' => array( 'name' => 'XML', 'support' => 'forms', 'count' => 'multiple' ),
64
        ) );
65
66
        if ( FrmAppHelper::pro_is_installed() ) {
67
            $frmpro_settings = new FrmProSettings();
68
            $csv_format = $frmpro_settings->csv_format;
69
        } else {
70
            $csv_format = 'UTF-8';
71
        }
72
73
        include(FrmAppHelper::plugin_path() .'/classes/views/xml/import_form.php');
74
    }
75
76
    public static function import_xml() {
77
        $errors = array();
78
        $message = '';
79
80
        $permission_error = FrmAppHelper::permission_nonce_error('frm_edit_forms', 'import-xml', 'import-xml-nonce');
81
        if ( $permission_error !== false ) {
82
            $errors[] = $permission_error;
83
            self::form($errors);
84
            return;
85
        }
86
87
        if ( ! isset($_FILES) || ! isset($_FILES['frm_import_file']) || empty($_FILES['frm_import_file']['name']) || (int) $_FILES['frm_import_file']['size'] < 1 ) {
88
            $errors[] = __( 'Oops, you didn\'t select a file.', 'formidable' );
89
            self::form($errors);
90
            return;
91
        }
92
93
        $file = $_FILES['frm_import_file']['tmp_name'];
94
95
        if ( ! is_uploaded_file( $file ) ) {
96
            unset($file);
97
            $errors[] = __( 'The file does not exist, please try again.', 'formidable' );
98
            self::form($errors);
99
            return;
100
        }
101
102
        //add_filter('upload_mimes', 'FrmXMLController::allow_mime');
103
104
        $export_format = apply_filters('frm_export_formats', array(
105
			'xml' => array( 'name' => 'XML', 'support' => 'forms', 'count' => 'multiple' ),
106
		) );
107
108
        $file_type = strtolower(pathinfo($_FILES['frm_import_file']['name'], PATHINFO_EXTENSION));
109
        if ( $file_type != 'xml' && isset( $export_format[ $file_type ] ) ) {
110
            // allow other file types to be imported
111
            do_action('frm_before_import_'. $file_type );
112
            return;
113
        }
114
        unset($file_type);
115
116
        //$media_id = FrmProAppHelper::upload_file('frm_import_file');
117
118
		if ( ! function_exists( 'libxml_disable_entity_loader' ) ) {
119
			$errors[] = __( 'XML import is not enabled on your server.', 'formidable' );
120
			self::form( $errors );
121
			return;
122
		}
123
124
		$set_err = libxml_use_internal_errors( true );
125
		$loader = libxml_disable_entity_loader( true );
126
127
		$result = FrmXMLHelper::import_xml( $file );
128
		FrmXMLHelper::parse_message( $result, $message, $errors );
129
130
		unset( $file );
131
132
		libxml_use_internal_errors( $set_err );
133
		libxml_disable_entity_loader( $loader );
134
135
        self::form($errors, $message);
136
    }
137
138
    public static function export_xml() {
139
        $error = FrmAppHelper::permission_nonce_error( 'frm_edit_forms', 'export-xml', 'export-xml-nonce' );
140
        if ( ! empty($error) ) {
141
            wp_die( $error );
142
        }
143
144
		$ids = FrmAppHelper::get_post_param( 'frm_export_forms', array() );
145
		$type = FrmAppHelper::get_post_param( 'type', array() );
146
		$format = FrmAppHelper::get_post_param( 'format', 'xml', 'sanitize_title' );
147
148
        if ( ! headers_sent() && ! $type ) {
149
            wp_redirect( esc_url_raw( admin_url( 'admin.php?page=formidable-import' ) ) );
150
            die();
151
        }
152
153
        if ( $format == 'xml' ) {
154
            self::generate_xml($type, compact('ids'));
155
        } else {
156
            do_action('frm_export_format_'. $format, compact('ids'));
157
        }
158
159
        wp_die();
160
    }
161
162
	public static function generate_xml( $type, $args = array() ) {
163
    	global $wpdb;
164
165
	    $type = (array) $type;
166
        if ( in_array( 'items', $type) && ! in_array( 'forms', $type) ) {
167
            // make sure the form is included if there are entries
168
            $type[] = 'forms';
169
        }
170
171
	    if ( in_array( 'forms', $type) ) {
172
            // include actions with forms
173
	        $type[] = 'actions';
174
	    }
175
176
	    $tables = array(
177
	        'items'     => $wpdb->prefix .'frm_items',
178
	        'forms'     => $wpdb->prefix .'frm_forms',
179
	        'posts'     => $wpdb->posts,
180
	        'styles'    => $wpdb->posts,
181
	        'actions'   => $wpdb->posts,
182
	    );
183
184
		$defaults = array( 'ids' => false );
185
	    $args = wp_parse_args( $args, $defaults );
186
187
        $sitename = sanitize_key( get_bloginfo( 'name' ) );
188
189
    	if ( ! empty( $sitename ) ) {
190
			$sitename .= '.';
191
		}
192
    	$filename = $sitename . 'formidable.' . date( 'Y-m-d' ) . '.xml';
193
194
    	header( 'Content-Description: File Transfer' );
195
    	header( 'Content-Disposition: attachment; filename=' . $filename );
196
    	header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
197
198
        //make sure ids are numeric
199
    	if ( is_array( $args['ids'] ) && ! empty( $args['ids'] ) ) {
200
	        $args['ids'] = array_filter( $args['ids'], 'is_numeric' );
201
	    }
202
203
	    $records = array();
204
205
		foreach ( $type as $tb_type ) {
206
            $where = array();
207
			$join = '';
208
            $table = $tables[ $tb_type ];
209
210
            $select = $table .'.id';
211
            $query_vars = array();
212
213
            switch ( $tb_type ) {
214
                case 'forms':
215
                    //add forms
216
                    if ( $args['ids'] ) {
217
						$where[] = array( 'or' => 1, $table . '.id' => $args['ids'], $table .'.parent_form_id' => $args['ids'] );
218
                	} else {
219
						$where[ $table . '.status !' ] = 'draft';
220
                	}
221
                break;
222
                case 'actions':
223
                    $select = $table .'.ID';
224
					$where['post_type'] = FrmFormActionsController::$action_post_type;
225
                    if ( ! empty($args['ids']) ) {
226
						$where['menu_order'] = $args['ids'];
227
                    }
228
                break;
229
                case 'items':
230
                    //$join = "INNER JOIN {$wpdb->prefix}frm_item_metas im ON ($table.id = im.item_id)";
231
                    if ( $args['ids'] ) {
232
						$where[ $table . '.form_id' ] = $args['ids'];
233
                    }
234
                break;
235
                case 'styles':
236
                    // Loop through all exported forms and get their selected style IDs
237
                    $form_ids = $args['ids'];
238
                    $style_ids = array();
239
                    foreach ( $form_ids as $form_id ) {
240
                        $form_data = FrmForm::getOne( $form_id );
241
                        // For forms that have not been updated while running 2.0, check if custom_style is set
242
                        if ( isset( $form_data->options['custom_style'] ) ) {
243
                            $style_ids[] = $form_data->options['custom_style'];
244
                        }
245
                        unset( $form_id, $form_data );
246
                    }
247
                    $select = $table .'.ID';
248
                    $where['post_type'] = 'frm_styles';
249
250
                    // Only export selected styles
251
                    if ( ! empty( $style_ids ) ) {
252
                        $where['ID'] = $style_ids;
253
                    }
254
                break;
255
                default:
256
                    $select = $table .'.ID';
257
                    $join = ' INNER JOIN ' . $wpdb->postmeta . ' pm ON (pm.post_id=' . $table . '.ID)';
258
                    $where['pm.meta_key'] = 'frm_form_id';
259
260
                    if ( empty($args['ids']) ) {
261
                        $where['pm.meta_value >'] = 1;
262
                    } else {
263
                        $where['pm.meta_value'] = $args['ids'];
264
                    }
265
                break;
266
            }
267
268
			$records[ $tb_type ] = FrmDb::get_col( $table . $join, $where, $select );
269
            unset($tb_type);
270
        }
271
272
		echo '<?xml version="1.0" encoding="' . esc_attr( get_bloginfo('charset') ) . "\" ?>\n";
273
        include(FrmAppHelper::plugin_path() .'/classes/views/xml/xml.php');
274
    }
275
276
	public static function allow_mime( $mimes ) {
277
        if ( ! isset( $mimes['csv'] ) ) {
278
            // allow csv files
279
            $mimes['csv'] = 'text/csv';
280
        }
281
282
        if ( ! isset( $mimes['xml'] ) ) {
283
            // allow xml
284
            $mimes['xml'] = 'text/xml';
285
        }
286
287
        return $mimes;
288
    }
289
}
290