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); } $tmp = tempnam(sys_get_temp_dir(), '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(); } }
public function actionUpload() { if (defined('N2_IMAGE_UPLOAD_DISABLE')) { N2Message::error(n2_('You are not allowed to upload!')); $this->response->error(); } $this->validateToken(); $root = N2Filesystem::getImagesFolder(); $folder = ltrim(rtrim(N2Request::getVar('path', ''), '/'), '/'); $path = N2Filesystem::realpath($root . '/' . $folder); if ($path === false || $path == '') { $folder = preg_replace("/[^A-Za-z0-9]/", '', $folder); if (empty($folder)) { N2Message::error(n2_('Folder is missing!')); $this->response->error(); } else { N2Filesystem::createFolder($root . '/' . $folder); $path = N2Filesystem::realpath($root . '/' . $folder); } } $relativePath = $this->relative($path, $root); if (!$relativePath) { $relativePath = ''; } $response = array('path' => $relativePath); try { if (isset($_FILES) && isset($_FILES['image']) && isset($_FILES['image']['name'])) { $info = pathinfo($_FILES['image']['name']); $fileName = preg_replace('/[^a-zA-Z0-9_-]/', '', $info['filename']); if (strlen($fileName) == 0) { $fileName = ''; } $upload = new N2BulletProof(); $file = $upload->uploadDir($path)->upload($_FILES['image'], $fileName); $response['name'] = basename($file); $response['url'] = N2ImageHelper::dynamic(N2Filesystem::pathToAbsoluteURL($file)); } } catch (Exception $e) { N2Message::error($e->getMessage()); $this->response->error(); } $this->response->respond($response); }