| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\Controllers\Users; |
| 4 |
|
| 5 |
use AcyMailing\Classes\MailpoetClass; |
| 6 |
use AcyMailing\Classes\UserClass; |
| 7 |
use AcyMailing\Helpers\EntitySelectHelper; |
| 8 |
use AcyMailing\Helpers\ExportHelper; |
| 9 |
use AcyMailing\Helpers\ImportHelper; |
| 10 |
use AcyMailing\Helpers\TabHelper; |
| 11 |
|
| 12 |
trait Import |
| 13 |
{ |
| 14 |
public function import(): void |
| 15 |
{ |
| 16 |
acym_setVar('layout', 'import'); |
| 17 |
|
| 18 |
$userClass = new UserClass(); |
| 19 |
|
| 20 |
// Get tables from database |
| 21 |
$tables = acym_getTables(); |
| 22 |
$arrayTables = []; |
| 23 |
foreach ($tables as $tableName) { |
| 24 |
$arrayTables[$tableName] = $tableName; |
| 25 |
} |
| 26 |
|
| 27 |
global $acymCmsUserVars; |
| 28 |
|
| 29 |
$nbUsersCMS = acym_loadResult('SELECT count('.$acymCmsUserVars->id.') FROM '.$acymCmsUserVars->table); |
| 30 |
if ('wordpress' === ACYM_CMS && is_multisite()) { |
| 31 |
$nbUsersCMS = acym_loadResult( |
| 32 |
'SELECT count('.$acymCmsUserVars->id.') |
| 33 |
FROM '.$acymCmsUserVars->table.' AS user JOIN #__usermeta AS `meta` ON `user`.'.$acymCmsUserVars->id.'=`meta`.`user_id` |
| 34 |
WHERE `meta`.`meta_key`=\'#__capabilities\'' |
| 35 |
); |
| 36 |
} |
| 37 |
$data = [ |
| 38 |
'tab' => new TabHelper(), |
| 39 |
'nbUsersAcymailing' => $userClass->getCountTotalUsers(), |
| 40 |
'nbUsersCMS' => $nbUsersCMS, |
| 41 |
'tables' => $arrayTables, |
| 42 |
'entitySelect' => new EntitySelectHelper(), |
| 43 |
'importHelper' => new ImportHelper(), |
| 44 |
]; |
| 45 |
|
| 46 |
if (ACYM_CMS === 'wordpress' && acym_isExtensionActive('mailpoet/mailpoet.php')) { |
| 47 |
$this->prepareMailPoetList($data); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
$this->breadcrumb[acym_translation('ACYM_IMPORT')] = acym_completeLink('users&task=import'); |
| 52 |
$data['menuClass'] = $this->menuClass; |
| 53 |
|
| 54 |
parent::display($data); |
| 55 |
} |
| 56 |
|
| 57 |
private function prepareMailPoetList(array &$data): void |
| 58 |
{ |
| 59 |
$mailpoetClass = new MailpoetClass(); |
| 60 |
$data['mailpoet_list'] = $mailpoetClass->getAllLists(); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
public function doImport(): void |
| 65 |
{ |
| 66 |
wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); |
| 67 |
|
| 68 |
$function = acym_getVar('cmd', 'import_from'); |
| 69 |
$allowedImportModes = acym_isAdmin() ? ['file', 'textarea', 'cms', 'mailpoet', 'contact'] : ['file', 'textarea']; |
| 70 |
|
| 71 |
// The table name comes from the request, this mode can read any data of the site |
| 72 |
if (acym_hasAdminPermissions()) { |
| 73 |
$allowedImportModes[] = 'database'; |
| 74 |
} |
| 75 |
|
| 76 |
if (!in_array($function, $allowedImportModes)) { |
| 77 |
die('Access denied for this import method'); |
| 78 |
} |
| 79 |
|
| 80 |
$importHelper = new ImportHelper(); |
| 81 |
|
| 82 |
if (empty($function) || !$importHelper->$function()) { |
| 83 |
$this->import(); |
| 84 |
|
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
if (in_array($function, ['file', 'textarea'])) { |
| 89 |
$importFile = ACYM_MEDIA.'import'.DS.acym_getVar('cmd', 'acym_import_filename'); |
| 90 |
if (file_exists($importFile)) { |
| 91 |
$importContent = file_get_contents($importFile); |
| 92 |
} |
| 93 |
|
| 94 |
if (empty($importContent)) { |
| 95 |
acym_enqueueMessage(acym_translation('ACYM_EMPTY_TEXTAREA'), 'error'); |
| 96 |
$this->import(); |
| 97 |
} else { |
| 98 |
acym_setVar('layout', 'genericimport'); |
| 99 |
$this->breadcrumb[acym_translation('ACYM_IMPORT')] = acym_completeLink('users&task=import'); |
| 100 |
parent::display(); |
| 101 |
} |
| 102 |
} else { |
| 103 |
$this->listing(); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
public function ajaxEncoding(): void |
| 108 |
{ |
| 109 |
acym_setVar('layout', 'ajaxencoding'); |
| 110 |
|
| 111 |
$data = []; |
| 112 |
|
| 113 |
ob_start(); |
| 114 |
parent::display($data); |
| 115 |
|
| 116 |
$data = [ |
| 117 |
'preview' => ob_get_clean(), |
| 118 |
]; |
| 119 |
|
| 120 |
acym_sendAjaxResponse('', $data); |
| 121 |
} |
| 122 |
|
| 123 |
public function finalizeImport(): void |
| 124 |
{ |
| 125 |
wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); |
| 126 |
|
| 127 |
$importHelper = new ImportHelper(); |
| 128 |
$importHelper->finalizeImport(); |
| 129 |
|
| 130 |
$this->listing(); |
| 131 |
} |
| 132 |
|
| 133 |
public function downloadImport(): void |
| 134 |
{ |
| 135 |
$filename = acym_getVar('cmd', 'filename'); |
| 136 |
if (!file_exists(ACYM_MEDIA.'import'.DS.$filename.'.csv')) { |
| 137 |
return; |
| 138 |
} |
| 139 |
$exportHelper = new ExportHelper(); |
| 140 |
$exportHelper->setDownloadHeaders($filename); |
| 141 |
|
| 142 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Downloading a file. |
| 143 |
echo file_get_contents(ACYM_MEDIA.'import'.DS.$filename.'.csv'); |
| 144 |
exit; |
| 145 |
} |
| 146 |
|
| 147 |
public function getColumnsFromTable(): void |
| 148 |
{ |
| 149 |
$tableName = acym_secureDBColumn(acym_getVar('string', 'tablename', '')); |
| 150 |
if (empty($tableName)) { |
| 151 |
exit; |
| 152 |
} |
| 153 |
|
| 154 |
echo '<option value=""></option>'; |
| 155 |
|
| 156 |
$columns = acym_getColumns($tableName, false, false); |
| 157 |
foreach ($columns as $oneColumn) { |
| 158 |
echo '<option value="'.esc_attr($oneColumn).'">'.esc_html($oneColumn).'</option>'; |
| 159 |
} |
| 160 |
|
| 161 |
exit; |
| 162 |
} |
| 163 |
} |
| 164 |
|