PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 1.07.11
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v1.07.11
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / controllers / FrmXMLController.php

FrmXMLController.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 1.07.11, at classes/controllers/FrmXMLController.php

326 lines 12.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( !defined('ABSPATH') ) die('You are not allowed to call this page directly.');
3
4 class FrmXMLController{
5 public static function load_hooks(){
6 add_action('admin_menu', 'FrmXMLController::menu', 41);
7 add_action('wp_ajax_frm_export_xml', 'FrmXMLController::export_xml');
8 }
9
10 public static function menu() {
11 add_submenu_page('formidable', 'Formidable | Import/Export', 'Import/Export', 'frm_edit_forms', 'formidable-import', 'FrmXMLController::route');
12 }
13
14 public static function add_default_templates() {
15 if ( !function_exists( 'libxml_disable_entity_loader' ) ){
16 // XML import is not enabled on your server
17 return;
18 }
19
20 include_once(FrmAppHelper::plugin_path() .'/classes/helpers/FrmXMLHelper.php');
21
22 $set_err = libxml_use_internal_errors(true);
23 $loader = libxml_disable_entity_loader( true );
24
25 $files = apply_filters('frm_default_templates_files', array(FrmAppHelper::plugin_path() .'/classes/views/xml/default-templates.xml'));
26
27 foreach ( (array) $files as $file) {
28 $result = FrmXMLHelper::import_xml($file);
29 unset($file);
30 }
31 if(is_wp_error($result))
32 $errors[] = $result->get_error_message();
33 else if($result)
34 $message = $result;
35
36 unset($files);
37
38 libxml_use_internal_errors( $set_err );
39 libxml_disable_entity_loader( $loader );
40 }
41
42 public static function route() {
43 $action = isset($_REQUEST['frm_action']) ? 'frm_action' : 'action';
44 $action = FrmAppHelper::get_param($action);
45 if($action == 'import_xml') {
46 return self::import_xml();
47 } else if($action == 'export_xml') {
48 return self::export_xml();
49 } else {
50 if ( apply_filters('frm_xml_route', true, $action) ){
51 return self::form();
52 }
53 }
54 }
55
56 public static function form($errors = array(), $message = '') {
57 //wp_enqueue_script('jquery-chosen');
58 //wp_enqueue_style('formidable');
59
60 $frm_form = new FrmForm();
61 $forms = $frm_form->getAll("status is NULL OR status = '' OR status = 'published'", ' ORDER BY name');
62 unset($frm_form);
63
64 $export_types = apply_filters('frm_xml_export_types',
65 array('forms' => __('Forms', 'formidable'))
66 );
67
68 $export_format = apply_filters('frm_export_formats', array(
69 'xml' => array( 'name' => 'XML', 'support' => 'forms', 'count' => 'multiple'),
70 ));
71
72 global $frmpro_settings;
73 $csv_format = $frmpro_settings ? $frmpro_settings->csv_format : 'UTF-8';
74
75 include(FrmAppHelper::plugin_path() .'/classes/views/xml/import_form.php');
76 }
77
78 public static function import_xml() {
79 $errors = array();
80 $message = '';
81
82 if ( !current_user_can('frm_edit_forms') || ! isset($_POST['import-xml']) || ! wp_verify_nonce($_POST['import-xml'], 'import-xml-nonce') ) {
83 global $frm_settings;
84 $errors[] = $frm_settings->admin_permission;
85 self::form($errors);
86 return;
87 }
88
89 if ( !isset($_FILES) || !isset($_FILES['frm_import_file']) || empty($_FILES['frm_import_file']['name']) || (int)$_FILES['frm_import_file']['size'] < 1) {
90 $errors[] = __( 'Oops, you didn\'t select a file.', 'formidable' );
91 self::form($errors);
92 return;
93 }
94
95 $file = $_FILES['frm_import_file']['tmp_name'];
96
97 if ( !is_uploaded_file($file) ) {
98 unset($file);
99 $errors[] = __( 'The file does not exist, please try again.', 'formidable' );
100 self::form($errors);
101 return;
102 }
103
104 //add_filter('upload_mimes', 'FrmXMLController::allow_mime');
105
106 $export_format = apply_filters('frm_export_formats', array(
107 'xml' => array( 'name' => 'XML', 'support' => 'forms', 'count' => 'multiple'),
108 ));
109
110 $file_type = strtolower(pathinfo($_FILES['frm_import_file']['name'], PATHINFO_EXTENSION));
111 if ( $file_type != 'xml' && isset($export_format[$file_type]) ) {
112 // allow other file types to be imported
113 do_action('frm_before_import_'. $file_type );
114 return;
115 }
116 unset($file_type);
117
118 //$media_id = FrmProAppHelper::upload_file('frm_import_file');
119 //if(is_numeric($media_id)){
120
121 if ( !function_exists( 'libxml_disable_entity_loader' ) ) {
122 $errors[] = __('XML import is not enabled on your server.', 'formidable');
123 self::form($errors);
124 return;
125 }
126
127 include_once(FrmAppHelper::plugin_path() .'/classes/helpers/FrmXMLHelper.php');
128
129 $set_err = libxml_use_internal_errors(true);
130 $loader = libxml_disable_entity_loader( true );
131
132 $result = FrmXMLHelper::import_xml($file);
133 if ( is_wp_error($result) ) {
134 $errors[] = $result->get_error_message();
135 } else if ( $result ) {
136 if ( is_array($result) ) {
137 $t_strings = array(
138 'imported' => __('Imported', 'formidable'),
139 'updated' => __('Updated', 'formidable'),
140 );
141
142 $message = '<ul>';
143 foreach ( $result as $type => $results ) {
144 if ( !isset($t_strings[$type]) ) {
145 // only print imported and updated
146 continue;
147 }
148
149 $s_message = array();
150 foreach ( $results as $k => $m ) {
151 if ( $m ) {
152 $strings = array(
153 'forms' => sprintf(_n( '%1$s Form', '%1$s Forms', $m, 'formidable' ), $m ),
154 'fields' => sprintf(_n( '%1$s Field', '%1$s Fields', $m, 'formidable' ), $m),
155 'items' => sprintf(_n( '%1$s Entry', '%1$s Entries', $m, 'formidable' ), $m),
156 'views' => sprintf(_n( '%1$s View', '%1$s Views', $m, 'formidable' ), $m),
157 'posts' => sprintf(_n( '%1$s Post', '%1$s Posts', $m, 'formidable' ), $m),
158 'terms' => sprintf(_n( '%1$s Term', '%1$s Terms', $m, 'formidable' ), $m),
159 );
160
161 $s_message[] = isset($strings[$k]) ? $strings[$k] : $t_strings[$type] .' '. $m .' '. ucfirst($k);
162 }
163 unset($k);
164 unset($m);
165 }
166
167 if ( !empty($s_message) ) {
168 $message .= '<li><strong>'. $t_strings[$type] .':</strong> ';
169 $message .= implode(', ', $s_message);
170 $message .= '</li>';
171 }
172
173 }
174
175 if ( $message == '<ul>' ) {
176 $message = '';
177 $errors[] = __('Nothing was imported or updated', 'formidable');
178 } else {
179 $message .= '</ul>';
180 }
181 } else {
182 $message = $result;
183 }
184 }
185
186 unset($file);
187
188 libxml_use_internal_errors( $set_err );
189 libxml_disable_entity_loader( $loader );
190 //}else{
191 // foreach ($media_id->errors as $error)
192 // echo $error[0];
193 //}
194
195 self::form($errors, $message);
196 }
197
198 public static function export_xml() {
199 if ( !current_user_can('frm_edit_forms') ) {
200 global $frm_settings;
201 echo $frm_settings->admin_permission;
202 die();
203 }
204
205 if (isset($_POST['frm_export_forms'])) {
206 $ids = $_POST['frm_export_forms'];
207 } else {
208 $ids = array();
209 }
210
211 if ( isset($_POST['type']) ){
212 $type = $_POST['type'];
213 }
214
215 $format = isset($_POST['format']) ? $_POST['format'] : 'xml';
216
217 if ( !headers_sent() && (!isset($type) || !$type) ) {
218 wp_redirect(admin_url('admin.php?page=formidable-import'));
219 die();
220 }
221
222 if ( $format == 'xml' ) {
223 self::generate_xml($type, compact('ids'));
224 } else {
225 do_action('frm_export_format_'. $format, compact('ids'));
226 }
227
228 die();
229 }
230
231 public static function export_xml_direct($controller = 'forms', $ids = false) {
232 if ( !current_user_can('frm_edit_forms') ) {
233 global $frm_settings;
234 wp_die($frm_settings->admin_permission);
235 }
236 $is_template = FrmAppHelper::get_param('is_template', false);
237 self::generate_xml($controller, compact('ids', 'is_template'));
238 die();
239 }
240
241 public static function generate_xml($type, $args = array() ) {
242 global $wpdb;
243
244 $type = (array)$type;
245 $tables = array(
246 'items' => $wpdb->prefix .'frm_items',
247 'forms' => $wpdb->prefix .'frm_forms',
248 'views' => $wpdb->posts
249 );
250
251 $defaults = array('ids' => false);
252 $args = wp_parse_args( $args, $defaults );
253
254 $sitename = sanitize_key( get_bloginfo( 'name' ) );
255
256 if ( ! empty($sitename) ) $sitename .= '.';
257 $filename = $sitename . 'formidable.' . date( 'Y-m-d' ) . '.xml';
258
259 header( 'Content-Description: File Transfer' );
260 header( 'Content-Disposition: attachment; filename=' . $filename );
261 header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
262
263 //make sure ids are numeric
264 if(is_array($args['ids']) && !empty($args['ids']))
265 $args['ids'] = implode(',', array_filter( $args['ids'], 'is_numeric' ));
266
267 $records = array();
268
269 foreach($type as $tb_type){
270 $where = $join = '';
271 $table = $tables[$tb_type];
272
273 $select = "$table.id";
274
275 if($tb_type == 'forms'){
276 //add forms
277 $where = $wpdb->prepare( "$table.status != %s" , 'draft' );
278 if ( $args['ids'] )
279 $where .= " AND $table.id IN (". $args['ids'] .")";
280
281 } else if($tb_type == 'items') {
282 //$join = "INNER JOIN {$wpdb->prefix}frm_item_metas im ON ($table.id = im.item_id)";
283 if ( $args['ids'] ) {
284 $where = "$table.form_id IN (". $args['ids'] .")";
285 }
286 } else {
287 $select = "$table.ID";
288 $join = "INNER JOIN $wpdb->postmeta pm ON (pm.post_id=$table.ID)";
289 $where = "pm.meta_key='frm_form_id' AND pm.meta_value ";
290 if ( empty($args['ids']) ) {
291 $where .= "> 0";
292 } else {
293 $where .= "IN (". $args['ids'] .")";
294 }
295 }
296
297 if(!empty($where))
298 $where = "WHERE ". $where;
299
300 $records[$tb_type] = $wpdb->get_col( "SELECT $select FROM $table $join $where" );
301 unset($tb_type);
302 }
303
304 include_once(FrmAppHelper::plugin_path() .'/classes/helpers/FrmXMLHelper.php');
305
306 $frm_field = new FrmField();
307
308 echo '<?xml version="1.0" encoding="' . get_bloginfo('charset') . "\" ?>\n";
309 include(FrmAppHelper::plugin_path() .'/classes/views/xml/xml.php');
310 }
311
312 function allow_mime($mimes) {
313 if ( !isset($mimes['csv']) ) {
314 // allow csv files
315 $mimes['csv'] = 'text/csv';
316 }
317
318 if ( !isset($mimes['xml']) ) {
319 // allow xml
320 $mimes['xml'] = 'text/xml';
321 }
322
323 return $mimes;
324 }
325
326 }