PluginProbe
Redirection / 2.7
Redirection v2.7
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / models / file-io.php

file-io.php in Redirection 2.7, at models/file-io.php

89 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 abstract class Red_FileIO {
4 public static function create( $type ) {
5 $exporter = false;
6
7 if ( $type === 'rss' ) {
8 include_once dirname( dirname( __FILE__ ) ).'/fileio/rss.php';
9 $exporter = new Red_Rss_File();
10 } elseif ( $type === 'csv' ) {
11 include_once dirname( dirname( __FILE__ ) ).'/fileio/csv.php';
12 $exporter = new Red_Csv_File();
13 } elseif ( $type === 'apache' ) {
14 include_once dirname( dirname( __FILE__ ) ).'/fileio/apache.php';
15 $exporter = new Red_Apache_File();
16 } elseif ( $type === 'nginx' ) {
17 include_once dirname( dirname( __FILE__ ) ).'/fileio/nginx.php';
18 $exporter = new Red_Nginx_File();
19 } elseif ( $type === 'json' ) {
20 include_once dirname( dirname( __FILE__ ) ).'/fileio/json.php';
21 $exporter = new Red_Json_File();
22 }
23
24 return $exporter;
25 }
26
27 public static function import( $group_id, $file ) {
28 $parts = pathinfo( $file['name'] );
29 $extension = isset( $parts['extension'] ) ? $parts['extension'] : '';
30
31 if ( $extension === 'csv' ) {
32 include_once dirname( dirname( __FILE__ ) ).'/fileio/csv.php';
33 $importer = new Red_Csv_File();
34 $data = '';
35 } else if ( $extension === 'json' ) {
36 include_once dirname( dirname( __FILE__ ) ).'/fileio/json.php';
37 $importer = new Red_Json_File();
38 $data = @file_get_contents( $file['tmp_name'] );
39 } else {
40 include_once dirname( dirname( __FILE__ ) ).'/fileio/apache.php';
41 $importer = new Red_Apache_File();
42 $data = @file_get_contents( $file['tmp_name'] );
43 }
44
45 if ( $extension !== 'json' ) {
46 $group = Red_Group::get( $group_id );
47 if ( ! $group ) {
48 return false;
49 }
50 }
51
52 return $importer->load( $group_id, $file['tmp_name'], $data );
53 }
54
55 public function force_download() {
56 header( 'Cache-Control: no-cache, must-revalidate' );
57 header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
58 }
59
60 public static function export( $module_name_or_id, $format ) {
61 if ( $module_name_or_id === 'all' || $module_name_or_id === 0 ) {
62 $groups = Red_Group::get_all();
63 $items = Red_Item::get_all();
64 } else {
65 $module_name_or_id = is_numeric( $module_name_or_id ) ? $module_name_or_id : Red_Module::get_id_for_name( $module_name_or_id );
66 $module = Red_Module::get( intval( $module_name_or_id, 10 ) );
67
68 if ( $module ) {
69 $groups = Red_Group::get_all_for_module( $module->get_id() );
70 $items = Red_Item::get_all_for_module( $module->get_id() );
71 }
72 }
73
74 $exporter = self::create( $format );
75 if ( $exporter && $items && $groups ) {
76 return array(
77 'data' => $exporter->get_data( $items, $groups ),
78 'total' => count( $items ),
79 'exporter' => $exporter,
80 );
81 }
82
83 return false;
84 }
85
86 abstract function get_data( array $items, array $groups );
87 abstract function load( $group, $filename, $data );
88 }
89