| 1 |
<?php |
| 2 |
defined('ABSPATH') || die('Restricted Access'); |
| 3 |
|
| 4 |
use AcyMailing\Classes\ConfigurationClass; |
| 5 |
|
| 6 |
/** |
| 7 |
* @param ?mixed $arg |
| 8 |
*/ |
| 9 |
function acym_dump($arg, bool $ajax = false, array $options = []): void |
| 10 |
{ |
| 11 |
$indent = $options['indent'] ?? true; |
| 12 |
$htmlentities = $options['htmlentities'] ?? true; |
| 13 |
|
| 14 |
ob_start(); |
| 15 |
if (is_object($arg) && isset($arg->config)) { |
| 16 |
$safeArg = clone $arg; |
| 17 |
unset($safeArg->config); |
| 18 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_dump -- For debug during support only. |
| 19 |
var_dump($safeArg); |
| 20 |
} else { |
| 21 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_dump -- For debug during support only. |
| 22 |
var_dump($arg); |
| 23 |
} |
| 24 |
$result = ob_get_clean(); |
| 25 |
|
| 26 |
if ($ajax) { |
| 27 |
$logsPath = ACYM_ROOT.ACYM_LOGS_FOLDER.'debug.log'; |
| 28 |
acym_createDir(dirname($logsPath), true, true); |
| 29 |
|
| 30 |
if (($options['clear_file'] ?? false) === true) { |
| 31 |
file_put_contents($logsPath, ''); |
| 32 |
} |
| 33 |
|
| 34 |
file_put_contents($logsPath, $result, FILE_APPEND); |
| 35 |
} else { |
| 36 |
$style = $indent ? 'margin-left: 220px;' : ''; |
| 37 |
echo '<pre style="'.esc_attr($style).'">'; |
| 38 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- For debug during support only. |
| 39 |
echo $htmlentities ? htmlentities($result) : $result; |
| 40 |
echo '</pre>'; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
function acym_debug(bool $file = false, bool $indent = true) |
| 45 |
{ |
| 46 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace -- Debug for support tickets. |
| 47 |
$debug = debug_backtrace(); |
| 48 |
$takenPath = []; |
| 49 |
foreach ($debug as $step) { |
| 50 |
if (empty($step['file']) || empty($step['line'])) continue; |
| 51 |
$takenPath[] = $step['file'].' => '.$step['line']; |
| 52 |
} |
| 53 |
acym_dump(implode("\n", $takenPath), $file, ['indent' => $indent]); |
| 54 |
} |
| 55 |
|
| 56 |
function acym_config(bool $reload = false): ConfigurationClass |
| 57 |
{ |
| 58 |
static $configClass = null; |
| 59 |
if ($configClass === null || $reload) { |
| 60 |
$configClass = new ConfigurationClass(); |
| 61 |
$configClass->load(); |
| 62 |
} |
| 63 |
|
| 64 |
return $configClass; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @param mixed $messages The messages displayed, either a string or an array or strings |
| 69 |
* @param string $type Type of the message: success, error, warning, info |
| 70 |
* @param bool $close Allow or not to close the message zone |
| 71 |
*/ |
| 72 |
function acym_display($messages, string $type = 'success', bool $close = true): void |
| 73 |
{ |
| 74 |
if (empty($messages)) return; |
| 75 |
if (!is_array($messages)) $messages = [$messages]; |
| 76 |
$config = acym_config(); |
| 77 |
$remindme = json_decode($config->get('remindme', '[]'), true); |
| 78 |
foreach ($messages as $id => $message) { |
| 79 |
if (strpos($message, 'acym__do__not__remindme') !== false) { |
| 80 |
preg_match('/title="(.*)"/Ui', $message, $matches); |
| 81 |
if (in_array($matches[1], $remindme)) { |
| 82 |
continue; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
echo '<div class="acym__message grid-x acym__message__'.esc_attr($type).'">'; |
| 87 |
|
| 88 |
if (is_array($message)) $message = implode('</div><div>', $message); |
| 89 |
|
| 90 |
echo '<div class="cell auto"><div>'.$message.'</div></div>'; |
| 91 |
|
| 92 |
if ($close && strpos($message, 'acym__do__not__remindme') === false) { |
| 93 |
echo '<i data-id="'.esc_attr($id).'" class="cell shrink acym__message__close acymicon-close"></i>'; |
| 94 |
} |
| 95 |
echo '</div>'; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
function acym_increasePerf(): int |
| 100 |
{ |
| 101 |
// phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged -- This is for big regex, the default value is 100 000. |
| 102 |
@ini_set('pcre.backtrack_limit', 1000000); |
| 103 |
|
| 104 |
// Increase the max exec time to be able to handle long processes such as the send process |
| 105 |
$maxExecutionTime = (int)ini_get('max_execution_time'); |
| 106 |
|
| 107 |
if (empty($maxExecutionTime) || $maxExecutionTime < 600) { |
| 108 |
// phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged -- Needed for long processes like email sending. |
| 109 |
return set_time_limit(600) ? 600 : $maxExecutionTime; |
| 110 |
} |
| 111 |
|
| 112 |
return $maxExecutionTime; |
| 113 |
} |
| 114 |
|
| 115 |
function acym_session(): void |
| 116 |
{ |
| 117 |
if (empty(session_id()) || session_status() !== PHP_SESSION_ACTIVE) { |
| 118 |
@session_start(); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
function acym_getCID(string $field = ''): int |
| 123 |
{ |
| 124 |
$oneResult = acym_getVar('array', 'cid', [], ''); |
| 125 |
$oneResult = intval(reset($oneResult)); |
| 126 |
if (!empty($oneResult) || empty($field)) { |
| 127 |
return $oneResult; |
| 128 |
} |
| 129 |
|
| 130 |
$oneResult = acym_getVar('int', $field, 0, ''); |
| 131 |
|
| 132 |
return intval($oneResult); |
| 133 |
} |
| 134 |
|
| 135 |
function acym_header(string $header, bool $replace = true): void |
| 136 |
{ |
| 137 |
if (headers_sent()) { |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
header($header, $replace); |
| 142 |
} |
| 143 |
|
| 144 |
function acym_getSocialMedias(): array |
| 145 |
{ |
| 146 |
return json_decode(ACYM_SOCIAL_MEDIA, true); |
| 147 |
} |
| 148 |
|
| 149 |
function acym_isAcyCheckerInstalled(): bool |
| 150 |
{ |
| 151 |
$installed = ACYM_CMS === 'joomla' && acym_isExtensionActive('com_acychecker'); |
| 152 |
|
| 153 |
return $installed || (ACYM_CMS === 'wordpress' && acym_isExtensionActive('acychecker/acychecker.php')); |
| 154 |
} |
| 155 |
|
| 156 |
function acym_getErrorLogFilename(string $prefix = ''): string |
| 157 |
{ |
| 158 |
if (!empty($prefix)) { |
| 159 |
$prefix .= '_'; |
| 160 |
} |
| 161 |
|
| 162 |
return $prefix.'errors.log'; |
| 163 |
} |
| 164 |
|
| 165 |
function acym_logError(string $message, string $prefix = '', int $maxLines = 0) |
| 166 |
{ |
| 167 |
$reportPath = acym_getLogPath(acym_getErrorLogFilename($prefix), true); |
| 168 |
|
| 169 |
if ($maxLines > 0 && file_exists($reportPath)) { |
| 170 |
$lines = file($reportPath); |
| 171 |
if (!empty($lines)) { |
| 172 |
$lines = array_slice($lines, -$maxLines); |
| 173 |
file_put_contents($reportPath, implode("\n", $lines)); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
$lr = "\r\n"; |
| 178 |
file_put_contents( |
| 179 |
$reportPath, |
| 180 |
$lr.acym_getDate(time()).': '.$message, |
| 181 |
FILE_APPEND |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
function acym_isLogFileErrorExist(string $prefix = ''): bool |
| 186 |
{ |
| 187 |
$reportPath = acym_getLogPath(acym_getErrorLogFilename($prefix)); |
| 188 |
|
| 189 |
return file_exists($reportPath); |
| 190 |
} |
| 191 |
|
| 192 |
function acym_getJsonData(): array |
| 193 |
{ |
| 194 |
$rawData = file_get_contents('php://input'); |
| 195 |
$decodedData = @json_decode($rawData, true); |
| 196 |
|
| 197 |
return empty($decodedData) ? [] : $decodedData; |
| 198 |
} |
| 199 |
|
| 200 |
function acym_displayFreeTrialMessage(): void |
| 201 |
{ |
| 202 |
if (!acym_isAdmin()) { |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
$config = acym_config(); |
| 207 |
|
| 208 |
if ($config->get('isTrial', 0) != 1) { |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
$expirationDate = $config->get('expirationdate', 0); |
| 213 |
$href = ACYM_ACYMAILING_WEBSITE.'account/license'; |
| 214 |
if ($config->get('expirationdate', 0) > time()) { |
| 215 |
$days = ceil(($expirationDate - time()) / 86400); |
| 216 |
$buttonFullAccess = '<a target="_blank" class="button button-secondary margin-left-1 shrink margin-bottom-0" href="'.$href.'">'.acym_translation( |
| 217 |
'ACYM_GET_FULL_ACCESS' |
| 218 |
).'</a>'; |
| 219 |
$message = '<span class="shrink">'.acym_translationSprintf('ACYM_FREE_TRIAL_EXPIRATION_X_DAYS', $days).'</span>'; |
| 220 |
$type = 'warning'; |
| 221 |
} else { |
| 222 |
$buttonFullAccess = '<a target="_blank" class="button acym__button__upgrade margin-left-1 shrink margin-bottom-0" href="'.$href.'">'.acym_translation( |
| 223 |
'ACYM_GET_FULL_ACCESS' |
| 224 |
).'</a>'; |
| 225 |
$message = '<span class="shrink">'.acym_translation('ACYM_FREE_TRIAL_ENDED').'</span>'; |
| 226 |
$type = 'error'; |
| 227 |
} |
| 228 |
acym_enqueueMessage('<div class="cell grid-x acym_vcenter">'.$message.$buttonFullAccess.'</div>', $type, false); |
| 229 |
} |
| 230 |
|
| 231 |
function acym_removeDashboardNotification(string $name): void |
| 232 |
{ |
| 233 |
$config = acym_config(); |
| 234 |
$existingNotifications = json_decode($config->get('dashboard_notif', '[]'), true) ?? []; |
| 235 |
|
| 236 |
foreach ($existingNotifications as $key => $existingNotification) { |
| 237 |
if (is_array($existingNotification) && $existingNotification['name'] === $name) { |
| 238 |
unset($existingNotifications[$key]); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
$config->saveConfig(['dashboard_notif' => json_encode($existingNotifications)], false); |
| 243 |
} |
| 244 |
|