ar_http_filesRegistry::remove()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 2
dl 0
loc 20
rs 8.9777
c 0
b 0
f 0
ccs 0
cts 19
cp 0
crap 42
1
<?php
2
	ar_pinp::allow( 'ar_http_files');
3
	ar_pinp::allow( 'ar_http_filesRegistry');
4
5
	class ar_http_files extends arBase {
6
7
		protected static $store = null;
8
9
		protected static function getStore( $store = null ) {
10
			if ( !$store ) {
11
				$store = self::$store;
12
			}
13
			if ( !$store ) {
14
				self::$store =  ar('loader/session')->get();
15
				if ( !self::$store ) {
16
					self::$store = ar('loader/session')->start();
17
				}
18
				$store = self::$store;
19
			}
20
			return $store;
21
		}
22
23
		public static function ls() {
0 ignored issues
show
Coding Style introduced by
ls uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
24
			return $_FILES;
25
		}
26
27
		public static function getFile( $filename ) {
0 ignored issues
show
Coding Style introduced by
getFile uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
28
			// return opened file if exists, null otherwise
29
			$filename = (string) $filename;
30
			$file = null;
31
			$info = $_FILES[$filename];
32
			if ( $info && is_uploaded_file( $info['tmp_name'] ) ) {
33
				$fp = fopen( $info['tmp_name'], 'r+b' );
34
				$file = new ar_content_filesFile( $fp );
35
			}
36
			return $file;
37
		}
38
39
		public static function registry( $store = null ) {
40
			$store = self::getStore( $store );
41
			return new ar_http_filesRegistry( $store );
42
		}
43
44
	}
45
46
	class ar_http_filesRegistry extends arBase {
47
48
		protected $store = null;
49
50
		public function __construct( $store ) {
51
			$this->store = $store;
52
		}
53
54
		public function ls() {
55
			return $this->store->getvar('registeredFiles');
56
		}
57
58
		public function getFile( $filename, $nls = 'none' ) {
59
			$filename = (string) $filename;
60
			$nls = (string) $nls;
61
			$registeredFiles = $this->ls();
62
			$info = $registeredFiles[$filename][$nls];
63
			if ($info && $info['temp']) {
64
				// check before removing
65
				$tempfile = preg_replace( "|[\\\/]|", "", $info['temp'] );
66
				$tempOb = ar::context()->getObject();
67
				$tempfile = $tempOb->store->get_config('files').'temp/'.$tempfile;
68
				$resource = fopen( $tempfile, 'r+b' );
69
				return new ar_content_filesFile( $resource );
70
			} else {
71
				return null;
72
			}
73
		}
74
75
		public function getInfo( $filename, $nls = 'none' ) {
76
			$filename = (string) $filename;
77
			$nls = (string) $nls;
78
			$registeredFiles = $this->ls();
79
			$info = $registeredFiles[$filename][$nls];
80
			if ( $info ) {
81
				return $info;
82
			} else {
83
				return ar('error')->raiseError( 'No info for file '.$filename.' found', 502 );
84
			}
85
		}
86
87
		public function putFile( $filename, $nls = 'none', $originalName = null ) {
88
			$filename = (string) $filename;
89
			$nls = (string) $nls;
90
			if ( !isset($originalName) ) {
91
				$originalName = $filename;
92
			}
93
			$originalName = (string) $originalName;
94
			$registeredFiles = (array) $this->store->getvar('registeredFiles');
95
			$error = '';
96
			$fileInfo = ldRegisterFile( $originalName, $error );
97
			if (!$error) {
98
				$cleanInfo = array(
99
					'name' => $fileInfo[ $originalName ],
100
					'temp' => $fileInfo[ $originalName.'_temp' ],
101
					'size' => $fileInfo[ $originalName.'_size' ],
102
					'type' => $fileInfo[ $originalName.'_type' ]
103
				);
104
				$registeredFiles[ $filename ][ $nls ] = $cleanInfo;
105
				$this->store->putvar('registeredFiles', $registeredFiles );
106
				$result = $cleanInfo;
107
			} else {
108
				$result = ar('error')->raiseError( $error, 501 );
109
			}
110
			return $result;
111
		}
112
113
		public function remove( $filename, $nls = 'none') {
114
			$filename = (string) $filename;
115
			$nls = (string) $nls;
116
			$registeredFiles = $this->ls();
117
			$info = $registeredFiles[$filename][$nls];
118
			unset($registeredFiles[$filename][$nls]);
119
			if (!$registeredFiles[$filename]) {
120
				unset($registeredFiles[$filename]);
121
			}
122
			$this->store->putvar('registeredFiles', $registeredFiles );
123
			if ($info && $info[$filename.'_temp']) {
124
				// check before removing
125
				$tempfile = preg_replace( "|[\\\/]|", "", $info[$filename.'_temp'] );
126
				$tempOb = ar::context()->getObject();
127
				$tempfile = $tempOb->store->get_config('files').'temp/'.$tempfile;
128
				if ($tempfile && file_exists($tempfile) ) {
129
					unlink($tempfile);
130
				}
131
			}
132
		}
133
134
	}
135