Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * Redefine the upload action. | |
30 | * | |
31 | * @package CRM | |
6b83d5bd | 32 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 | 33 | * $Id$ |
6a488035 TO |
34 | */ |
35 | class CRM_Core_QuickForm_Action_Upload extends CRM_Core_QuickForm_Action { | |
36 | ||
37 | /** | |
d09edf64 | 38 | * The array of uploaded file names. |
6a488035 TO |
39 | * @var array |
40 | */ | |
41 | protected $_uploadNames; | |
42 | ||
43 | /** | |
d09edf64 | 44 | * The directory to store the uploaded files. |
6a488035 TO |
45 | * @var string |
46 | */ | |
47 | protected $_uploadDir; | |
48 | ||
49 | /** | |
5c9ff055 | 50 | * Class constructor. |
6a488035 | 51 | * |
6a0b768e TO |
52 | * @param object $stateMachine |
53 | * Reference to state machine object. | |
54 | * @param string $uploadDir | |
55 | * Directory to store the uploaded files. | |
56 | * @param array $uploadNames | |
57 | * Element names of the various uploadable files. | |
6a488035 | 58 | * |
77b97be7 | 59 | * @return \CRM_Core_QuickForm_Action_Upload |
77b97be7 | 60 | */ |
00be9182 | 61 | public function __construct(&$stateMachine, $uploadDir, $uploadNames) { |
6a488035 TO |
62 | parent::__construct($stateMachine); |
63 | ||
64 | $this->_uploadDir = $uploadDir; | |
65 | $this->_uploadNames = $uploadNames; | |
66 | } | |
67 | ||
68 | /** | |
0965e988 | 69 | * Upload and move the file if valid to the uploaded directory. |
6a488035 | 70 | * |
6a0b768e TO |
71 | * @param CRM_Core_Form $page |
72 | * The CRM_Core_Form object. | |
73 | * @param object $data | |
74 | * The QFC data container. | |
75 | * @param string $pageName | |
76 | * The name of the page which index the data container with. | |
77 | * @param string $uploadName | |
78 | * The name of the uploaded file. | |
6a488035 | 79 | */ |
00be9182 | 80 | public function upload(&$page, &$data, $pageName, $uploadName) { |
6a488035 TO |
81 | // make sure uploadName exists in the QF array |
82 | // else we skip, CRM-3427 | |
83 | if (empty($uploadName) || | |
84 | !isset($page->_elementIndex[$uploadName]) | |
85 | ) { | |
86 | return; | |
87 | } | |
88 | ||
89 | // get the element containing the upload | |
90 | $element = &$page->getElement($uploadName); | |
91 | if ('file' == $element->getType()) { | |
92 | if ($element->isUploadedFile()) { | |
93 | // rename the uploaded file with a unique number at the end | |
94 | $value = $element->getValue(); | |
95 | ||
96 | $newName = CRM_Utils_File::makeFileName($value['name']); | |
97 | $status = $element->moveUploadedFile($this->_uploadDir, $newName); | |
98 | if (!$status) { | |
be2fb01f | 99 | 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.', [ |
353ffa53 | 100 | 1 => $value['name'], |
0965e988 | 101 | 2 => $this->_uploadDir, |
be2fb01f | 102 | ])); |
6a488035 TO |
103 | } |
104 | if (!empty($data['values'][$pageName][$uploadName]['name'])) { | |
105 | @unlink($this->_uploadDir . $data['values'][$pageName][$uploadName]); | |
106 | } | |
107 | ||
be2fb01f | 108 | $value = [ |
6a488035 TO |
109 | 'name' => $this->_uploadDir . $newName, |
110 | 'type' => $value['type'], | |
be2fb01f | 111 | ]; |
9b7bedc5 | 112 | //CRM-19460 handle brackets if present in $uploadName, similar things we do it for all other inputs. |
113 | $value = $element->_prepareValue($value, TRUE); | |
114 | $data['values'][$pageName] = HTML_QuickForm::arrayMerge($data['values'][$pageName], $value); | |
6a488035 TO |
115 | } |
116 | } | |
117 | } | |
118 | ||
119 | /** | |
120 | * Processes the request. | |
121 | * | |
6a0b768e TO |
122 | * @param CRM_Core_Form $page |
123 | * CRM_Core_Form the current form-page. | |
124 | * @param string $actionName | |
125 | * Current action name, as one Action object can serve multiple actions. | |
6a488035 | 126 | */ |
00be9182 | 127 | public function perform(&$page, $actionName) { |
6a488035 TO |
128 | // like in Action_Next |
129 | $page->isFormBuilt() or $page->buildForm(); | |
130 | ||
131 | // so this is a brain-seizure moment, so hang tight (real tight!) | |
132 | // the above buildForm potentially changes the action function with different args | |
133 | // so basically the rug might have been pulled from us, so we actually just check | |
134 | // and potentially call the right one | |
0965e988 | 135 | // this allows standalone form uploads to work nicely |
6a488035 TO |
136 | $page->controller->_actions['upload']->realPerform($page, $actionName); |
137 | } | |
138 | ||
a0ee3941 | 139 | /** |
5c9ff055 EM |
140 | * Real perform. |
141 | * | |
d8689418 | 142 | * @todo document what I do. |
0965e988 | 143 | * |
77fbc665 | 144 | * @param CRM_Core_Form $page |
100fef9d | 145 | * @param string $actionName |
a0ee3941 EM |
146 | * |
147 | * @return mixed | |
148 | */ | |
00be9182 | 149 | public function realPerform(&$page, $actionName) { |
6a488035 TO |
150 | $pageName = $page->getAttribute('name'); |
151 | $data = &$page->controller->container(); | |
152 | $data['values'][$pageName] = $page->exportValues(); | |
153 | $data['valid'][$pageName] = $page->validate(); | |
154 | ||
155 | if (!$data['valid'][$pageName]) { | |
156 | return $page->handle('display'); | |
157 | } | |
158 | ||
159 | foreach ($this->_uploadNames as $name) { | |
160 | $this->upload($page, $data, $pageName, $name); | |
161 | } | |
162 | ||
163 | $state = &$this->_stateMachine->getState($pageName); | |
164 | if (empty($state)) { | |
165 | return $page->handle('display'); | |
166 | } | |
167 | ||
168 | // the page is valid, process it before we jump to the next state | |
169 | $page->mainProcess(); | |
170 | ||
171 | // check if destination is set, if so goto destination | |
172 | $destination = $this->_stateMachine->getDestination(); | |
173 | if ($destination) { | |
174 | $destination = urldecode($destination); | |
175 | CRM_Utils_System::redirect($destination); | |
176 | } | |
177 | else { | |
178 | return $state->handleNextState($page); | |
179 | } | |
180 | } | |
96025800 | 181 | |
6a488035 | 182 | } |