Aviary.php
2.23 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
<?php
class N2SystemBackendAviaryControllerAjax extends N2BackendControllerAjax {
public function actionGetHighResolutionAuth() {
N2Loader::import('libraries.image.aviary');
$this->response->respond(array(
'highResolutionAuth' => N2ImageAviary::getHighResolutionAuth()
));
}
public function actionSaveImage() {
$this->validateToken();
N2Loader::import('libraries.image.aviary');
$image = N2Request::getVar('aviaryUrl');
$this->validateVariable(!empty($image), 'image');
require_once dirname(__FILE__) . '/Browse.php';
$root = N2Filesystem::getImagesFolder();
$folder = 'aviary';
$path = N2Filesystem::realpath($root . '/' . $folder);
if ($path === false || $path == '') {
N2Filesystem::createFolder($root . '/' . $folder);
$path = N2Filesystem::realpath($root . '/' . $folder);
}
$folder = sys_get_temp_dir();
if (!is_writable($folder)) {
$folder = N2Filesystem::getNotWebCachePath();
}
$tmp = tempnam($folder, 'image-');
file_put_contents($tmp, file_get_contents($image));
$src = null;
// Set variables for storage
// fix file filename for query strings
preg_match('/([^\?]+)\.(jpe?g|gif|png)\b/i', $image, $matches);
$file_array['name'] = basename($matches[1]);
$file_array['tmp_name'] = $tmp;
$file_array['size'] = filesize($tmp);
$file_array['error'] = 0;
try {
$fileName = preg_replace('/[^a-zA-Z0-9_-]/', '', $file_array['name']);
$upload = new N2BulletProof();
$file = $upload->uploadDir($path)
->upload($file_array, $fileName);
$src = N2ImageHelper::dynamic(N2Filesystem::pathToAbsoluteURL($file));
} catch (Exception $e) {
N2Message::error($e->getMessage());
$this->response->error();
}
if ($src) {
$this->response->respond(array(
'image' => $src
));
} else {
N2Message::error(sprintf(n2_('Unexpected error: %s'), $image));
$this->response->error();
}
}
}