| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * Redefine the upload action. |
| 30 | * |
| 31 | * @package CRM |
| 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 33 | * $Id$ |
| 34 | * |
| 35 | */ |
| 36 | class CRM_Core_QuickForm_Action_Upload extends CRM_Core_QuickForm_Action { |
| 37 | |
| 38 | /** |
| 39 | * The array of uploaded file names |
| 40 | * @var array |
| 41 | */ |
| 42 | protected $_uploadNames; |
| 43 | |
| 44 | /** |
| 45 | * The directory to store the uploaded files |
| 46 | * @var string |
| 47 | */ |
| 48 | protected $_uploadDir; |
| 49 | |
| 50 | /** |
| 51 | * Class constructor |
| 52 | * |
| 53 | * @param object $stateMachine reference to state machine object |
| 54 | * @param string $uploadDir directory to store the uploaded files |
| 55 | * @param array $uploadNames element names of the various uploadable files |
| 56 | * |
| 57 | * @return \CRM_Core_QuickForm_Action_Upload |
| 58 | @access public |
| 59 | */ |
| 60 | public function __construct(&$stateMachine, $uploadDir, $uploadNames) { |
| 61 | parent::__construct($stateMachine); |
| 62 | |
| 63 | $this->_uploadDir = $uploadDir; |
| 64 | $this->_uploadNames = $uploadNames; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Upload and move the file if valid to the uploaded directory |
| 69 | * |
| 70 | * @param CRM_Core_Form $page the CRM_Core_Form object |
| 71 | * @param object $data the QFC data container |
| 72 | * @param string $pageName the name of the page which index the data container with |
| 73 | * @param string $uploadName the name of the uploaded file |
| 74 | * |
| 75 | * @return void |
| 76 | */ |
| 77 | public function upload(&$page, &$data, $pageName, $uploadName) { |
| 78 | // make sure uploadName exists in the QF array |
| 79 | // else we skip, CRM-3427 |
| 80 | if (empty($uploadName) || |
| 81 | !isset($page->_elementIndex[$uploadName]) |
| 82 | ) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | // get the element containing the upload |
| 87 | $element = &$page->getElement($uploadName); |
| 88 | if ('file' == $element->getType()) { |
| 89 | if ($element->isUploadedFile()) { |
| 90 | // rename the uploaded file with a unique number at the end |
| 91 | $value = $element->getValue(); |
| 92 | |
| 93 | $newName = CRM_Utils_File::makeFileName($value['name']); |
| 94 | $status = $element->moveUploadedFile($this->_uploadDir, $newName); |
| 95 | if (!$status) { |
| 96 | CRM_Core_Error::statusBounce(ts('We could not move the uploaded file %1 to the upload directory %2. Please verify that the \'Temporary Files\' setting points to a valid path which is writable by your web server.', array(1 => $value['name'], 2 => $this->_uploadDir))); |
| 97 | } |
| 98 | if (!empty($data['values'][$pageName][$uploadName]['name'])) { |
| 99 | @unlink($this->_uploadDir . $data['values'][$pageName][$uploadName]); |
| 100 | } |
| 101 | |
| 102 | $data['values'][$pageName][$uploadName] = array( |
| 103 | 'name' => $this->_uploadDir . $newName, |
| 104 | 'type' => $value['type'], |
| 105 | ); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Processes the request. |
| 112 | * |
| 113 | * @param CRM_Core_Form $page CRM_Core_Form the current form-page |
| 114 | * @param string $actionName Current action name, as one Action object can serve multiple actions |
| 115 | * |
| 116 | * @return void |
| 117 | */ |
| 118 | public function perform(&$page, $actionName) { |
| 119 | // like in Action_Next |
| 120 | $page->isFormBuilt() or $page->buildForm(); |
| 121 | |
| 122 | // so this is a brain-seizure moment, so hang tight (real tight!) |
| 123 | // the above buildForm potentially changes the action function with different args |
| 124 | // so basically the rug might have been pulled from us, so we actually just check |
| 125 | // and potentially call the right one |
| 126 | // this allows standalong form uploads to work nicely |
| 127 | $page->controller->_actions['upload']->realPerform($page, $actionName); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @param CRM_Core_Form $page |
| 132 | * @param string $actionName |
| 133 | * |
| 134 | * @return mixed |
| 135 | */ |
| 136 | public function realPerform(&$page, $actionName) { |
| 137 | $pageName = $page->getAttribute('name'); |
| 138 | $data = &$page->controller->container(); |
| 139 | $data['values'][$pageName] = $page->exportValues(); |
| 140 | $data['valid'][$pageName] = $page->validate(); |
| 141 | |
| 142 | if (!$data['valid'][$pageName]) { |
| 143 | return $page->handle('display'); |
| 144 | } |
| 145 | |
| 146 | foreach ($this->_uploadNames as $name) { |
| 147 | $this->upload($page, $data, $pageName, $name); |
| 148 | } |
| 149 | |
| 150 | $state = &$this->_stateMachine->getState($pageName); |
| 151 | if (empty($state)) { |
| 152 | return $page->handle('display'); |
| 153 | } |
| 154 | |
| 155 | // the page is valid, process it before we jump to the next state |
| 156 | $page->mainProcess(); |
| 157 | |
| 158 | // check if destination is set, if so goto destination |
| 159 | $destination = $this->_stateMachine->getDestination(); |
| 160 | if ($destination) { |
| 161 | $destination = urldecode($destination); |
| 162 | CRM_Utils_System::redirect($destination); |
| 163 | } |
| 164 | else { |
| 165 | return $state->handleNextState($page); |
| 166 | } |
| 167 | } |
| 168 | } |