Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When uploading files, handle public directory path with or without tr… #81

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions CRM/Gdpr/Form/ContributionPage/TermsAndConditions.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ private function saveTCFile() {
if ($fileElement && !empty($fileElement->_value['name'])) {
$config = CRM_Core_Config::singleton();
$publicUploadDir = $config->imageUploadDir;
$delim = '/';
$publicUploadDir = substr($publicUploadDir, -1) == $delim ? $publicUploadDir : $publicUploadDir . $delim;
$fileInfo = $fileElement->_value;
$pathInfo = pathinfo($fileElement->_value['name']);
if (empty($pathInfo['filename'])) {
Expand All @@ -279,14 +281,14 @@ private function saveTCFile() {
while (!$fileName) {
$suffix = $delta ? '-' . $delta : '';
$testName = $pathInfo['filename'] . $suffix . '.' . $pathInfo['extension'];
if (!file_exists($publicUploadDir . '/' . $testName)) {
if (!file_exists($publicUploadDir . $testName)) {
$fileName = $testName;
}
$delta++;
}
// Move to public uploads directory and create file record.
// This will be referenced in Activity custom field.
$saved = $fileElement->moveUploadedFile($publicUploadDir,$fileName);
$saved = $fileElement->moveUploadedFile($publicUploadDir, $fileName);
if ($saved) {
return $this->getFileUrl($publicUploadDir . $fileName);
}
Expand Down
6 changes: 4 additions & 2 deletions CRM/Gdpr/Form/ManageEvent/TermsAndConditions.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ private function saveTCFile() {
if ($fileElement && !empty($fileElement->_value['name'])) {
$config = CRM_Core_Config::singleton();
$publicUploadDir = $config->imageUploadDir;
$delim = '/';
$publicUploadDir = substr($publicUploadDir, -1) == $delim ? $publicUploadDir : $publicUploadDir . $delim;
$fileInfo = $fileElement->_value;
$pathInfo = pathinfo($fileElement->_value['name']);
if (empty($pathInfo['filename'])) {
Expand All @@ -276,14 +278,14 @@ private function saveTCFile() {
while (!$fileName) {
$suffix = $delta ? '-' . $delta : '';
$testName = $pathInfo['filename'] . $suffix . '.' . $pathInfo['extension'];
if (!file_exists($publicUploadDir . '/' . $testName)) {
if (!file_exists($publicUploadDir . $testName)) {
$fileName = $testName;
}
$delta++;
}
// Move to public uploads directory and create file record.
// This will be referenced in Activity custom field.
$saved = $fileElement->moveUploadedFile($publicUploadDir,$fileName);
$saved = $fileElement->moveUploadedFile($publicUploadDir, $fileName);
if ($saved) {
return $this->getFileUrl($publicUploadDir . $fileName);
}
Expand Down
13 changes: 8 additions & 5 deletions CRM/Gdpr/Form/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function buildQuickForm() {
'cp_tc_enable',
ts('Enable Terms and Conditions for every Contribution Page')
);
//If T+C is enabled for both Events Contribution Pages they will share the
//If T+C is enabled for both Events Contribution Pages they will share the
//following settings.
$this->add(
'select',
Expand Down Expand Up @@ -233,7 +233,7 @@ function postProcess() {
$settings['entity_tc_intro'] = $values['entity_tc_intro'];
// Map the upload file element to setting name.
$upload_elems = array(
'sla_tc_upload' => 'sla_tc',
'sla_tc_upload' => 'sla_tc',
'entity_tc_upload' => 'entity_tc',
);
foreach ($upload_elems as $elem => $setting) {
Expand Down Expand Up @@ -269,7 +269,7 @@ public static function formRule($params, $files) {

/**
* Save an uploaded Terms and Conditions file.
* @return string
* @return string
* Path of the saved file.
*/
private function saveTCFile($element_name) {
Expand All @@ -280,6 +280,8 @@ private function saveTCFile($element_name) {
if ($fileElement && !empty($fileElement->_value['name'])) {
$config = CRM_Core_Config::singleton();
$publicUploadDir = $config->imageUploadDir;
$delim = '/';
$publicUploadDir = substr($publicUploadDir, -1) == $delim ? $publicUploadDir : $publicUploadDir . $delim;
$fileInfo = $fileElement->_value;
$pathInfo = pathinfo($fileElement->_value['name']);
if (empty($pathInfo['filename'])) {
Expand All @@ -288,17 +290,18 @@ private function saveTCFile($element_name) {
// If necessary add a delta to the file name to avoid writing over an existing file.
$delta = 0;
$fileName = '';

while (!$fileName) {
$suffix = $delta ? '-' . $delta : '';
$testName = $pathInfo['filename'] . $suffix . '.' . $pathInfo['extension'];
if (!file_exists($publicUploadDir . '/' . $testName)) {
if (!file_exists($publicUploadDir . $testName)) {
$fileName = $testName;
}
$delta++;
}
// Move to public uploads directory and create file record.
// This will be referenced in Activity custom field.
$saved = $fileElement->moveUploadedFile($publicUploadDir,$fileName);
$saved = $fileElement->moveUploadedFile($publicUploadDir, $fileName);
if ($saved) {
return $this->getFileUrl($publicUploadDir . $fileName);
}
Expand Down