|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Abstract Class for manupulating a server |
|
4
|
|
|
* |
|
5
|
|
|
* @package Jam |
|
6
|
|
|
* @author Ivan Kerin |
|
7
|
|
|
* @copyright (c) 2011-2012 Despark Ltd. |
|
8
|
|
|
* @license http://creativecommons.org/licenses/by-sa/3.0/legalcode |
|
9
|
|
|
*/ |
|
10
|
|
|
abstract class Kohana_Upload_Server |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string default instance name |
|
14
|
|
|
*/ |
|
15
|
|
|
public static $default = 'default'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array Database instances |
|
19
|
|
|
*/ |
|
20
|
|
|
public static $instances = array(); |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Get a singleton Upload_Server instance. If configuration is not specified, |
|
24
|
|
|
* it will be loaded from the Upload_Server configuration file using the same |
|
25
|
|
|
* group as the name. |
|
26
|
|
|
* |
|
27
|
|
|
* // Load the default server |
|
28
|
|
|
* $server = Upload_Server::instance(); |
|
29
|
|
|
* |
|
30
|
|
|
* // Create a custom configured instance |
|
31
|
|
|
* $server = Upload_Server::instance('custom', $config); |
|
32
|
|
|
* |
|
33
|
|
|
* @param string instance name |
|
34
|
|
|
* @param array configuration parameters |
|
35
|
|
|
* @return Database |
|
36
|
|
|
*/ |
|
37
|
5 |
|
public static function instance($name = NULL, array $config = NULL) |
|
38
|
|
|
{ |
|
39
|
5 |
|
if ($name === NULL) |
|
40
|
5 |
|
{ |
|
41
|
|
|
// Use the default instance name |
|
42
|
|
|
$name = Upload_Server::$default; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
5 |
|
if ( ! isset(Upload_Server::$instances[$name])) |
|
46
|
5 |
|
{ |
|
47
|
2 |
|
if ($config === NULL) |
|
48
|
2 |
|
{ |
|
49
|
|
|
// Load the configuration for this database |
|
50
|
2 |
|
$config = Arr::get(Kohana::$config->load('jam.upload.servers'), $name); |
|
|
|
|
|
|
51
|
2 |
|
} |
|
52
|
|
|
|
|
53
|
2 |
|
$validation = Validation::factory($config) |
|
54
|
2 |
|
->rule('type', 'not_empty') |
|
55
|
2 |
|
->rule('params', 'not_empty') |
|
56
|
2 |
|
->rule('params', 'is_array'); |
|
57
|
|
|
|
|
58
|
2 |
|
if ( ! $validation->check()) |
|
59
|
2 |
|
throw new Kohana_Exception('Upload server config had errors: :errors', |
|
60
|
|
|
array(':errors' => join(', ', $validation->errors('upload_server')))); |
|
61
|
|
|
|
|
62
|
2 |
|
$server = 'server_'.$validation['type']; |
|
63
|
2 |
|
Upload_Server::$instances[$name] = Upload_Server::$server($validation['params']); |
|
64
|
2 |
|
} |
|
65
|
|
|
|
|
66
|
5 |
|
return Upload_Server::$instances[$name]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
public static function server_local(array $params = array()) |
|
70
|
|
|
{ |
|
71
|
2 |
|
$validation = Validation::factory($params) |
|
72
|
2 |
|
->rule('path', 'not_empty') |
|
73
|
2 |
|
->rule('path', 'is_dir') |
|
74
|
2 |
|
->rule('web', 'not_empty') |
|
75
|
2 |
|
->rule('url_type', 'in_array', array(':value', array(Flex\Storage\Server::URL_HTTP, Flex\Storage\Server::URL_SSL, Flex\Storage\Server::URL_STREAMING))); |
|
76
|
|
|
|
|
77
|
2 |
|
if ( ! $validation->check()) |
|
78
|
2 |
|
throw new Kohana_Exception('Upload server local params had errors: :errors', |
|
79
|
|
|
array(':errors' => join(', ', Arr::flatten($validation->errors())))); |
|
80
|
|
|
|
|
81
|
2 |
|
$server = new Flex\Storage\Server_Local($validation['path'], $validation['web']); |
|
82
|
|
|
|
|
83
|
2 |
|
if (isset($validation['url_type'])) |
|
84
|
2 |
|
{ |
|
85
|
|
|
$server->url_type($validation['url_type']); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
return $server; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public static function server_rackspace(array $params = array()) |
|
92
|
|
|
{ |
|
93
|
|
|
$validation = Validation::factory($params) |
|
94
|
|
|
->rule('username', 'not_empty') |
|
95
|
|
|
->rule('api_key', 'not_empty') |
|
96
|
|
|
->rule('container', 'not_empty') |
|
97
|
|
|
->rule('url_type', 'in_array', array(':value', array(Flex\Storage\Server::URL_HTTP, Flex\Storage\Server::URL_SSL, Flex\Storage\Server::URL_STREAMING))) |
|
98
|
|
|
->rule('cdn_uri', 'url') |
|
99
|
|
|
->rule('cdn_ssl', 'url') |
|
100
|
|
|
->rule('cdn_streaming', 'url'); |
|
101
|
|
|
|
|
102
|
|
|
if ( ! $validation->check()) |
|
103
|
|
|
throw new Kohana_Exception('Upload server local params had errors: :errors', |
|
104
|
|
|
array(':errors' => join(', ', $validation->errors('upload_server')))); |
|
105
|
|
|
|
|
106
|
|
|
$server = new Flex\Storage\Server_Rackspace($validation['container'], $validation['region'], array( |
|
107
|
|
|
'username' => $validation['username'], |
|
108
|
|
|
'apiKey' => $validation['api_key'], |
|
109
|
|
|
)); |
|
110
|
|
|
|
|
111
|
|
|
foreach (array('cdn_uri', 'cdn_ssl', 'cdn_streaming', 'url_type') as $param) |
|
112
|
|
|
{ |
|
113
|
|
|
if (isset($validation[$param])) |
|
114
|
|
|
{ |
|
115
|
|
|
$server->$param($validation[$param]); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $server; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public static function server_local_fallback(array $params = array()) |
|
123
|
|
|
{ |
|
124
|
|
|
$validation = Validation::factory($params) |
|
125
|
|
|
->rule('path', 'not_empty') |
|
126
|
|
|
->rule('fallback', 'not_empty') |
|
127
|
|
|
->rule('path', 'is_dir') |
|
128
|
|
|
->rule('url_type', 'in_array', array(':value', array(Flex\Storage\Server::URL_HTTP, Flex\Storage\Server::URL_SSL, Flex\Storage\Server::URL_STREAMING))) |
|
129
|
|
|
->rule('web', 'not_empty'); |
|
130
|
|
|
|
|
131
|
|
|
if ( ! $validation->check()) |
|
132
|
|
|
throw new Kohana_Exception('Upload server local params had errors: :errors', |
|
133
|
|
|
array(':errors' => join(', ', $validation->errors('upload_server')))); |
|
134
|
|
|
|
|
135
|
|
|
$server = new Flex\Storage\Server_Local_Fallback($validation['path'], $validation['web']); |
|
136
|
|
|
$server->fallback(Upload_Server::instance($validation['fallback'])); |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
if (isset($validation['url_type'])) |
|
139
|
|
|
{ |
|
140
|
|
|
$server->url_type($validation['url_type']); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $server; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public static function server_local_placeholdit(array $params = array()) |
|
147
|
|
|
{ |
|
148
|
|
|
$validation = Validation::factory($params) |
|
149
|
|
|
->rule('path', 'not_empty') |
|
150
|
|
|
->rule('path', 'is_dir') |
|
151
|
|
|
->rule('placeholder', 'url') |
|
152
|
|
|
->rule('url_type', 'in_array', array(':value', array(Flex\Storage\Server::URL_HTTP, Flex\Storage\Server::URL_SSL, Flex\Storage\Server::URL_STREAMING))) |
|
153
|
|
|
->rule('web', 'not_empty'); |
|
154
|
|
|
|
|
155
|
|
|
if ( ! $validation->check()) |
|
156
|
|
|
throw new Kohana_Exception('Upload server local params had errors: :errors', |
|
157
|
|
|
array(':errors' => join(', ', $validation->errors('upload_server')))); |
|
158
|
|
|
|
|
159
|
|
|
$server = new Flex\Storage\Server_Local_Placeholdit($validation['path'], $validation['web']); |
|
160
|
|
|
|
|
161
|
|
|
if (isset($validation['placeholder'])) { |
|
162
|
|
|
$server->placeholder($validation['placeholder']); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
if (isset($validation['url_type'])) |
|
166
|
|
|
{ |
|
167
|
|
|
$server->url_type($validation['url_type']); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return $server; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: