-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataURIFile.php
More file actions
109 lines (95 loc) · 2.95 KB
/
DataURIFile.php
File metadata and controls
109 lines (95 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php defined('SYSPATH') OR die('No direct script access.');
/**
* A simple class for Data URI handling.
*
* Initially made for Kohana, but requires only exception rewriting to fit
* anything else.
*
* @author Viesturs Kavacs <kavackys@gmail.com>
* @link https://github.com/kavacky/dataurifile
*/
class DataURIFile {
protected $data_uri = '';
protected $mime_type = null;
protected $data = null;
protected $size = null;
protected $filename = null;
protected $allowed = array(
'size' => 0, // Bytes
'mime_types' => array(),
);
/**
*
*/
public function __construct($data_uri = '') {
$this->data_uri = $data_uri;
}
/**
* @return DataURLFile
* @throws Kohana_Exception
*/
public function decode() {
$parts = array();
if (!preg_match('/data:([^;]*);base64,(.*)/', $this->data_uri, $parts)) {
throw new Kohana_Exception('Invalid Data URI');
}
$this->mime_type = $parts[1];
$this->data = base64_decode($parts[2]);
$this->size = strlen($this->data);
return $this;
}
/**
* @return DataURLFile
* @throws Kohana_Exception
*/
public function check($override = array()) {
$allowed = array_merge($this->allowed, $override);
if ($this->size > $allowed['size']) {
throw new Kohana_Exception(
'Allowed file size exceeded: :has bytes instead of maximum of :allowed',
array(
':has' => $this->size,
':allowed' => $allowed['size'],
)
);
}
if (count($allowed['mime_types']) and !in_array($this->mime_type, $allowed['mime_types'])) {
throw new Kohana_Exception(
'Mime type not allowed: :has (Allowed: :allowed)',
array(
':has' => $this->mime_type,
':allowed' => print_r($allowed['mime_types'], true),
)
);
}
return $this;
}
/**
* @throws Kohana_Exception
*/
public function save($filename, $mode = 0644, $dirmode = 0755) {
$path = dirname($filename);
if (!is_dir($path)) {
if (!mkdir($path, $dirmode, true)) {
throw new Kohana_Exception(
'Cannot create directory :dir',
array(':dir' => Debug::path($path))
);
}
}
if (file_put_contents($filename, $this->data) == false) {
throw new Kohana_Exception(
'Cannot write file :f',
array(':f' => Debug::path($filename))
);
}
if (!chmod($filename, $mode)) {
throw new Kohana_Exception(
'Cannot chmod file :f',
array(':f' => Debug::path($filename))
);
}
$this->filename = $filename;
return $this->filename;
}
}