function validate($data, &$failed = null) { $ret = parent::validate($data); if ($this->has_file) { try { $errors = array(); foreach ($_FILES as $k => $v) { if (isset($v['error']) && $v['error'] == UPLOAD_ERR_NO_FILE) { if ($this->must) { $errors[] = '请选择上传文件!'; break; } continue; } $fileExt = Helper_Upload::fileExt($v['name']); if (!in_array(strtolower($fileExt), Helper_Upload::getTypes())) { $errors[] = '上传文件的类型不符合要求'; } if ($v['size'] > Helper_Upload::getSize()) { $errors[] = '上传文件的大小超过限制'; } } if (empty($errors)) { return $ret; } $this[$this->has_file]->invalidate(implode(', ', $errors)); return false; } catch (Exception $ex) { $this[$this->has_file]->invalidate($ex->getMessage()); return false; } } else { return $ret; } }
public static function setAllow($allow_type, $max_size) { self::$maxFileSize = intval($max_size) * 1024 * 1024; if (!is_array($allow_type)) { self::$allowFileTypes = explode(',', $allow_size); } else { self::$allowFileTypes = $allow_size; } }
public function upload_imagesAction() { $request = $this->getRequest(); if (JO_Session::get('user[user_id]')) { if (JO_Session::get('upload_from_file')) { @unlink(BASE_PATH . JO_Session::get('upload_from_file')); JO_Session::clear('upload_from_file'); JO_Session::clear('upload_from_file_name'); } $image = $request->getFile('file'); if (!$image) { $this->view->error = $this->translate('There is no file selected'); } else { $temporary = '/cache/review/'; $upload_folder = BASE_PATH . $temporary; $upload = new Helper_Upload(); $files_delete = glob($upload_folder . '*.*'); if ($files_delete) { for ($i = 0; $i < min(count($files_delete), 250); $i++) { if (filemtime($files_delete[$i]) + 86400 < time()) { @unlink($files_delete[$i]); } } } $upload->setFile($image)->setExtension(array('.jpg', '.jpeg', '.png', '.gif'))->setUploadDir($upload_folder); $new_name = md5(time() . serialize($image)); if ($upload->upload($new_name)) { $info = $upload->getFileInfo(); if ($info) { $config_image_minimum_size = (int) Helper_Config::get('config_image_minimum_size'); if (!$config_image_minimum_size) { $config_image_minimum_size = 80; } if (isset($info['image_dimension']['x']) && $info['image_dimension']['x'] >= $config_image_minimum_size && $info['image_dimension']['y'] >= $config_image_minimum_size) { $this->view->from_url = WM_Router::create($request->getBaseUrl() . '?controller=addpin_fromfile&action=stepone'); $this->view->success = 1; JO_Session::set('upload_from_file', $temporary . $info['name']); JO_Session::set('upload_from_file_name', $image['name']); } else { @unlink($upload_folder . $info['name']); $this->view->error = sprintf($this->translate('Photo size must be larger width and height of %s px'), $config_image_minimum_size); } } else { $this->view->error = $this->translate('An unknown error'); } } else { $this->view->error = $upload->getError(); } } } else { $this->view->location = WM_Router::create($request->getBaseUrl() . '?controller=users&action=login'); } echo $this->renderScript('json'); }
public function upload_imagesAction() { $request = $this->getRequest(); if (JO_Session::get('upload_from_file')) { @unlink(BASE_PATH . JO_Session::get('upload_from_file')); JO_Session::clear('upload_from_file'); JO_Session::clear('upload_from_file_name'); } $image = $request->getFile('file'); if (!$image) { $this->view->error = $this->translate('There is no file selected'); } else { $temporary = '/cache/review/'; $upload_folder = BASE_PATH . $temporary; $upload = new Helper_Upload(); $upload->setFile($image)->setExtension(array('.jpg', '.jpeg', '.png', '.gif'))->setUploadDir($upload_folder); $new_name = md5(time() . serialize($image)); if ($upload->upload($new_name)) { $info = $upload->getFileInfo(); if ($info) { $this->view->from_url = WM_Router::create($request->getBaseUrl() . '?controller=addpin&action=fromfile'); // $this->view->file = $image['name']; // $this->view->full_path = $temporary . $info['name']; $this->view->success = 1; //$this->view->render('upload_images', 'addpin'); JO_Session::set('upload_from_file', $temporary . $info['name']); JO_Session::set('upload_from_file_name', $image['name']); } else { $this->view->error = $this->translate('An unknown error'); } } else { $this->view->error = $upload->getError(); } } $this->noViewRenderer(true); echo $this->renderScript('json'); }
public function uploadAction() { $this->noViewRenderer(true); $request = $this->getRequest(); $response = $this->getResponse(); $page = (int) $request->getRequest('page'); if ($page < 1) { $page = 1; } $callback = $request->getRequest('callback'); if (!preg_match('/^([a-z0-9_.]{1,})$/', $callback)) { $callback = false; } if (isset($_POST['token']) && $_POST['token'] == md5($_POST['userId'])) { $_SESSION['token'] = $_POST['token']; JO_Session::set('token', $_POST['token']); // $token = $request->getRequest('token'); // $user_id = $request->getRequest('userId'); // $folderName = $request->getRequest('folderName'); // $categoryId = $request->getRequest('categoryId'); // // $token = $request->getRequest('token'); //$user_id = $request->getRequest('userId'); // error_log("token " .$token); // error_log("user " . md5($user_id)); // error_log("session " . $_SESSION['token']) ; // if (isset($token) && $token == md5($user_id)) // { // $_SESSION['token'] = $token; $return = array(); //print_r("files " . var_dump($_FILES)) ; //print_r("request " .var_dump($_REQUEST)); //error_log("1file name " . $_FILES["file"]["tmp_name"] . " uploads " . $_REQUEST["image"]); //error_log("2file name " . $_FILES["uploadedfile"]["name"] . " uploads " . $_REQUEST["image"]); //$this->view->form_action = WM_Router::create( $request->getBaseUrl() . '?controller=addpin&action=upload_images' ); //$this->view->upload_action = WM_Router::create( $request->getBaseUrl() . '?controller=addpin&action=upload_imagesView' ); //$this->view->popup_main_box = $this->view->render('fromfile','addpin'); if (JO_Session::get('upload_from_file')) { @unlink(BASE_PATH . JO_Session::get('upload_from_file')); JO_Session::clear('upload_from_file'); JO_Session::clear('upload_from_file_name'); } $image = $request->getFile('file'); if (!$image) { $return = array('error' => 10, 'description' => $this->translate('There is no file selected')); } else { $temporary = '/cache/review/'; $upload_folder = BASE_PATH . $temporary; $upload = new Helper_Upload(); $upload->setFile($image)->setExtension(array('.jpg', '.jpeg', '.png', '.gif'))->setUploadDir($upload_folder); $new_name = md5(time() . serialize($image)); if ($upload->upload($new_name)) { $info = $upload->getFileInfo(); if ($info) { $this->view->from_url = WM_Router::create($request->getBaseUrl() . '?controller=addpin&action=fromfile'); // $this->view->file = $image['name']; // $this->view->full_path = $temporary . $info['name']; $this->view->success = 1; //$this->view->render('upload_images', 'addpin'); JO_Session::set('upload_from_file', $temporary . $info['name']); JO_Session::set('upload_from_file_name', $image['name']); } else { $return = array('error' => 11, 'description' => $this->translate('An unknown error')); } } else { $return = array('error' => 12, 'description' => $upload->getError()); } } if ($request->isPost()) { $result = Model_Pins::create(array('title' => $request->getPost('title'), 'from' => '', 'image' => BASE_PATH . JO_Session::get('upload_from_file'), 'is_video' => $request->getPost('is_video'), 'is_article' => $request->getPost('is_article'), 'description' => $request->getPost('message'), 'price' => $request->getPost('price'), 'board_id' => $request->getPost('board_id'), 'user_id' => $request->getPost('userId'))); if ($result) { Model_History::addHistory(0, Model_History::ADDPIN, $result); if (JO_Registry::get('isMobile')) { //$this->redirect('/'); } $session_user = JO_Session::get('user[user_id]'); $group = Model_Boards::isGroupBoard($request->getPost('board_id')); if ($group) { $users = explode(',', $group); foreach ($users as $user_id) { if ($user_id != $session_user) { $user_data = Model_Users::getUser($user_id); if ($user_data && $user_data['email_interval'] == 1 && $user_data['groups_pin_email']) { $this->view->user_info = $user_data; $this->view->profile_href = WM_Router::create($request->getBaseUrl() . '?controller=users&action=profile&user_id=' . JO_Session::get('user[user_id]')); $this->view->full_name = JO_Session::get('user[firstname]') . ' ' . JO_Session::get('user[lastname]'); $this->view->pin_href = WM_Router::create($request->getBaseUrl() . '?controller=pin&pin_id=' . $result); $board_info = Model_Boards::getBoard($request->getPost('board_id')); if ($board_info) { $this->view->board_title = $board_info['title']; $this->view->board_href = WM_Router::create($request->getBaseUrl() . '?controller=boards&action=view&user_id=' . $board_info['user_id'] . '&board_id=' . $board_info['board_id']); } Model_Email::send($user_data['email'], JO_Registry::get('noreply_mail'), JO_Session::get('user[firstname]') . ' ' . JO_Session::get('user[lastname]') . ' ' . $this->translate('added new pin to a group board'), $this->view->render('group_board', 'mail')); } } } } $this->view->pin_url = WM_Router::create($request->getBaseUrl() . '?controller=pin&pin_id=' . $result); $this->view->popup_main_box = $this->view->render('success', 'addpin'); if (JO_Session::get('upload_from_file')) { @unlink(BASE_PATH . JO_Session::get('upload_from_file')); JO_Session::clear('upload_from_file'); JO_Session::clear('upload_from_file_name'); } } } /* //$_FILES-> name type tmp_name error size //'image' => BASE_PATH . JO_Session::get('upload_from_file'), if( $request->isPost() ) { $this->view->form_action = WM_Router::create( $request->getBaseUrl() . '?controller=addpin&action=upload_images' ); $this->view->upload_action = WM_Router::create( $request->getBaseUrl() . '?controller=addpin&action=upload_imagesView' ); $this->view->popup_main_box = $this->view->render('fromfile','addpin'); $url_m = $request->getPost('image'); if(strpos($url_m, '.jpg?')) { $url_m = explode('?', $url_m); $url_m = $url_m[0]; } error_log("3file name " . $_FILES["file"]["tmp_name"] . " url_m " . $url_m); $url_m = $_FILES; $result = Model_Pins::create(array( 'title' => $request->getPost('title'), 'from' => $request->getPost('from'), 'image' => $url_m, 'is_video' => 0, //$request->getPost('is_video'), 'is_article' => 0, //$request->getPost('is_article'), 'description' => $request->getPost('message'), 'price' => $request->getPost('price'), 'board_id' => $request->getPost('board_id') )); if($result) { Model_History::addHistory(JO_Session::get('user[user_id]'), Model_History::ADDPIN, $result); $session_user = JO_Session::get('user[user_id]'); $group = Model_Boards::isGroupBoard($request->getPost('board_id')); if($group) { $users = explode(',',$group); foreach($users AS $user_id) { if($user_id != $session_user) { $user_data = Model_Users::getUser($user_id); if($user_data && $user_data['email_interval'] == 1 && $user_data['groups_pin_email']) { $this->view->user_info = $user_data; $this->view->profile_href = WM_Router::create($request->getBaseUrl() . '?controller=users&action=profile&user_id=' . JO_Session::get('user[user_id]')); $this->view->full_name = JO_Session::get('user[firstname]') . ' ' . JO_Session::get('user[lastname]'); $this->view->pin_href = WM_Router::create( $request->getBaseUrl() . '?controller=pin&pin_id=' . $result ); $board_info = Model_Boards::getBoard($request->getPost('board_id')); if($board_info) { $this->view->board_title = $board_info['title']; $this->view->board_href = WM_Router::create($request->getBaseUrl() . '?controller=boards&action=view&user_id=' . $board_info['user_id'] . '&board_id=' . $board_info['board_id']); } Model_Email::send( $user_data['email'], JO_Registry::get('noreply_mail'), JO_Session::get('user[firstname]') . ' ' . JO_Session::get('user[lastname]') . ' ' . $this->translate('added new pin to a group board'), $this->view->render('group_board', 'mail') ); } } } } $this->view->pin_url = WM_Router::create( $request->getBaseUrl() . '?controller=pin&pin_id=' . $result ); $this->view->popup_main_box = $this->view->render('success','addpin'); } } */ } else { //no existe la sesión / no existe el dato recibido por post / el token no es igual. $return = array('error' => 401, 'description' => $this->translate('wrong token')); } if ($callback) { $return = $callback . '(' . JO_Json::encode($return) . ')'; } else { $response->addHeader('Cache-Control: no-cache, must-revalidate'); $response->addHeader('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); $response->addHeader('Content-type: application/json'); $return = JO_Json::encode($return); } $response->appendBody($return); }