* @license http://www.contenido.org/license/LIZENZ.txt * @link http://www.4fb.de * @link http://www.contenido.org * @since file available since contenido release <= 4.6 * * {@internal * created 2007-01-01, Bj�rn Behrens (HerrB) * modified 2008-06-27, Dominik Ziegler, add security fix * * $Id: include.recipients_import.php 128 2019-07-03 11:58:28Z oldperl $: * }} * */ if (!defined('CON_FRAMEWORK')) { die('Illegal call'); } $oPage = new cPage; $oRecipients = new RecipientCollection; if (is_array($cfg['plugins']['recipients'])) { foreach ($cfg['plugins']['recipients'] as $plugin) { plugin_include("recipients", $plugin . "/" . $plugin . ".php"); } } // Check form data if ($_REQUEST["selDelimiter"] == "") { $_REQUEST["selDelimiter"] = "tab"; } $aFields = array(); $aFieldDetails = array(); $aFields["name"] = strtolower(i18n("Name", "cl_newsletter")); $aFieldDetails["name"]["fieldtype"] = "field"; // field, plugin or group $aFieldDetails["name"]["mandatory"] = false; // true or false $aFieldDetails["name"]["type"] = "string"; // string, boolean or date $aFieldDetails["name"]["link"] = false; // plugin name for plugins, recipient group id for groups $aFieldDetails["name"]["col"] = -1; // Stores column index where this field has been found $aFields["email"] = strtolower(i18n("Mail", "cl_newsletter")); $aFieldDetails["email"]["fieldtype"] = "field"; $aFieldDetails["email"]["mandatory"] = true; $aFieldDetails["email"]["type"] = "string"; $aFieldDetails["email"]["link"] = false; $aFieldDetails["email"]["col"] = -1; $aFields["deactivated"] = strtolower(i18n("Deactivated", "cl_newsletter")); $aFieldDetails["deactivated"]["fieldtype"] = "field"; $aFieldDetails["deactivated"]["mandatory"] = false; $aFieldDetails["deactivated"]["type"] = "boolean"; $aFieldDetails["deactivated"]["link"] = false; $aFieldDetails["deactivated"]["col"] = -1; $aFields["confirmed"] = strtolower(i18n("Confirmed", "cl_newsletter")); $aFieldDetails["confirmed"]["fieldtype"] = "field"; $aFieldDetails["confirmed"]["mandatory"] = false; $aFieldDetails["confirmed"]["type"] = "boolean"; $aFieldDetails["confirmed"]["link"] = false; $aFieldDetails["confirmed"]["col"] = -1; $aFields["confirmeddate"] = strtolower(i18n("Confirmed Date", "cl_newsletter")); $aFieldDetails["confirmeddate"]["fieldtype"] = "field"; $aFieldDetails["confirmeddate"]["mandatory"] = false; $aFieldDetails["confirmeddate"]["type"] = "date"; $aFieldDetails["confirmeddate"]["link"] = false; $aFieldDetails["confirmeddate"]["col"] = -1; $aFields["news_type"] = strtolower(i18n("Message type", "cl_newsletter")); $aFieldDetails["news_type"]["fieldtype"] = "field"; $aFieldDetails["news_type"]["mandatory"] = false; $aFieldDetails["news_type"]["type"] = "boolean"; $aFieldDetails["news_type"]["link"] = false; $aFieldDetails["news_type"]["col"] = -1; // Check out if there are any plugins if (is_array($cfg['plugins']['recipients'])) { foreach ($cfg['plugins']['recipients'] as $sPlugin) { if (function_exists("recipients_" . $sPlugin . "_wantedVariables") && function_exists("recipients_" . $sPlugin . "_canonicalVariables")) { $aPluginTitles = call_user_func("recipients_" . $sPlugin . "_canonicalVariables"); $aPluginFields = call_user_func("recipients_" . $sPlugin . "_wantedVariables"); foreach ($aPluginFields as $sField) { //if ($_REQUEST["ckb".$sField]) { $aFields[$sField] = strtolower(str_replace(" ", "", $aPluginTitles[$sField])); $aFieldDetails[$sField]["fieldtype"] = "plugin"; $aFieldDetails[$sField]["mandatory"] = false; $aFieldDetails[$sField]["type"] = "string"; $aFieldDetails[$sField]["link"] = $sPlugin; $aFieldDetails[$sField]["col"] = -1; //} } } } } // Get groups $oRcpGroups = new RecipientGroupCollection; $oRcpGroups->setWhere("idclient", $client); $oRcpGroups->setWhere("idlang", $lang); $oRcpGroups->setOrder("groupname"); $oRcpGroups->query(); while ($oRcpGroup = $oRcpGroups->next()) { $sField = "g" . $oRcpGroup->get($oRcpGroup->primaryKey); $sGroupName = $oRcpGroup->get("groupname"); $sGroupName = str_replace(" ", "", $sGroupName); $sGroupName = str_replace("\t", "", $sGroupName); $sGroupName = str_replace("\n", "", $sGroupName); $sGroupName = str_replace("\r", "", $sGroupName); $sGroupName = str_replace("\0", "", $sGroupName); $sGroupName = str_replace("\x0B;", "", $sGroupName); // Only PHP5! //$sGroupName = str_replace(str_split(" \t\n\r\0\x0B;"), "", $oRcpGroup->get("groupname")); $aFields[$sField] = strtolower(clHtmlEntities(trim(i18n("Group", "cl_newsletter") . "_" . $sGroupName))); $aFieldDetails[$sField]["fieldtype"] = "group"; $aFieldDetails[$sField]["mandatory"] = false; $aFieldDetails[$sField]["type"] = "string"; $aFieldDetails[$sField]["link"] = $oRcpGroup->get($oRcpGroup->primaryKey); $aFieldDetails[$sField]["col"] = -1; } if ($action == "recipients_import_exec" && $perm->have_perm_area_action("recipients", "recipients_create")) { $_REQUEST["txtData"] = trim(stripslashes($_REQUEST["txtData"])); if ($_REQUEST["txtData"]) { switch ($_REQUEST["selDelimiter"]) { case "semicolon": $sDelimiter = ";"; break; default: $sDelimiter = "\t"; //chr(9); } //echo "
".nl2br(stripslashes($_REQUEST["txtData"]))."
"; $aLines = explode("\n", stripslashes($_REQUEST["txtData"])); $iAdded = 0; $iDublettes = 0; $iInvalid = 0; $iRow = 0; $iCol = 0; $bStop = false; $sMessage = ""; $aMessage = array(); $aInvalidLines = array(); $oGroupMembers = new RecipientGroupMemberCollection; foreach ($aLines as $sLine) { $iRow++; $aParts = explode($sDelimiter, trim($sLine)); if ($iRow == 1) { $aInvalidLines[] = $sLine; foreach ($aParts as $sHeader) { $sKey = array_search(strtolower(clHtmlEntities(trim($sHeader))), $aFields); if ($sKey === false) { $aMessage[] = sprintf(i18n("Given column header '%s' unknown, column ignored", "cl_newsletter"), $sHeader); } else { $aFieldDetails[$sKey]["col"] = $iCol; $iCol++; } } foreach ($aFieldDetails as $sKey => $aDetails) { if ($aDetails["mandatory"] && $aDetails["col"] == -1) { $aMessage[] = sprintf(i18n("Mandatory column '%s' wasn't found, import stopped", "cl_newsletter"), $aDetails[$sKey]); $bStop = true; } } if ($bStop) { exit; } else { $_REQUEST["txtData"] = ""; } } else { $sEMail = trim($aParts[$aFieldDetails["email"]["col"]]); if ($aFieldDetails["name"]["col"] > -1) { $sName = trim($aParts[$aFieldDetails["name"]["col"]]); if ($sName == "") { $sName = $sEMail; } } else { $sName = $sEMail; } if ($sEMail == "") { $aMessage[] = sprintf(i18n("Item with empty mail address found, item ignored (name: %s, row: %s)", "cl_newsletter"), $sName, $iRow); $aInvalidLines[] = $sLine; $iInvalid++; } else if (!isValidMail($sEMail)) { $aMessage[] = sprintf(i18n("Mail address '%s' is invalid, item ignored (row: %s)", "cl_newsletter"), $sEMail, $iRow); $aInvalidLines[] = $sLine; $iInvalid++; } else if ($oRecipients->emailExists($sEMail)) { $aMessage[] = sprintf(i18n("Recipient with mail address '%s' already exists, item skipped (row: %s)", "cl_newsletter"), $sEMail, $iRow); $aInvalidLines[] = $sLine; $iDublettes++; } else { unset($sLine); // Must be $recipient for plugins if ($recipient = $oRecipients->create($sEMail, $sName)) { $iID = $recipient->get($recipient->primaryKey); $iAdded++; unset($aPluginValue); $aPluginValue = array(); foreach ($aFieldDetails as $sKey => $aDetails) { if ($aDetails["col"] > -1) { switch ($aDetails["fieldtype"]) { case "field": switch ($aDetails["type"]) { case "boolean": $sValue = strtolower(trim($aParts[$aDetails["col"]])); // html is only treated as "true", to get html messages for recipients // - quick and dirty... if ($sValue == "yes" || $sValue == i18n("yes", "cl_newsletter") || $sValue == "true" || (is_numeric($sValue) && $sValue > 0) || $sValue == "html") { $recipient->set($sKey, 1); if ($sKey == "confirmed") { // Ensure, that if a recipient is confirmed, a confirmed date // is available. As "confirmeddate" will be set after "confirmed" // a specified confirmeddate will overwrite this default $recipient->set("confirmeddate", date("Y-m-d H:i:s"), false); } } else { $recipient->set($sKey, 0); } break; case "date": // TODO: Check conversion: Result may be unpredictable... $sValue = trim($aParts[$aDetails["col"]]); $recipient->set($sKey, date("Y-m-d H:i:s", strtotime($sValue)), false); break; default: $sValue = trim($aParts[$aDetails["col"]]); $recipient->set($sKey, $sValue); } break; case "plugin": // type may be mentioned here, also, but as plugins currently can't // specify the type, just treat everything as string // There may be plugins which store more than one value per plugin_store- // function. As the plugin_store parameter is an array of values, collect // all values in an array for later storing... unfortunately, that means, // that we have to go through the fields array second time per item *sigh* $aPluginValue[$aDetails["link"]][$sKey] = trim($aParts[$aDetails["col"]]); break; case "group": // Add recipient to group $sValue = strtolower(trim($aParts[$aDetails["col"]])); if ($sValue == "yes" || $sValue == i18n("yes", "cl_newsletter") || $sValue == "true" || (is_numeric($sValue) && $sValue > 0)) { $oGroupMembers->create($aDetails["link"], $iID); } break; } } } // Store all base data $recipient->store(); // Store plugin data (to store plugin data, only, where the column has been found in the data // should be faster than going through all plugins and store mostly empty arrays) $sCurrentPlugin = ""; foreach ($aFieldDetails as $sKey => $aDetails) { if ($aDetails["col"] > -1 && $aDetails["fieldtype"] == "plugin" && $aDetails["link"] !== $sCurrentPlugin) { $sCurrentPlugin = $aDetails["link"]; call_user_func("recipients_" . $sCurrentPlugin . "_store", $aPluginValue[$sCurrentPlugin]); } } } } } } if (count($aInvalidLines) > 1) { $_REQUEST["txtData"] = implode("\n", $aInvalidLines); } if (count($aMessage) > 0) { $sMessage = $notification->returnNotification("warning", implode("
", $aMessage)) . "
"; } $sMessage .= $notification->returnNotification("info", sprintf(i18n("%d recipients added, %d recipients skipped (email already exists) and %d invalid recipients/e-mail adresses ignored. Invalid recipients are shown (if any).", "cl_newsletter"), $iAdded, $iDublettes, $iInvalid)); if ($iAdded > 0) { $oPage->setReload(); } } } $oForm = new UI_Table_Form("properties"); $oForm->setVar("frame", $frame); $oForm->setVar("area", $area); $oForm->setVar("action", "recipients_import_exec"); $oForm->addHeader(i18n("Import recipients", "cl_newsletter")); $oSelDelimiter = new cHTMLSelectElement("selDelimiter"); $aItems = array(); $aItems[] = array("tab", i18n("Tab", "cl_newsletter")); $aItems[] = array("semicolon", i18n("Semicolon", "cl_newsletter")); $oSelDelimiter->autoFill($aItems); $oSelDelimiter->setDefault($_REQUEST["selDelimiter"]); $oForm->add(i18n("Delimiter", "cl_newsletter"), $oSelDelimiter->render()); $oAreaData = new cHTMLTextarea("txtData", $_REQUEST["txtData"], 80, 20); $sInfo = '' . i18n("Import information", "cl_newsletter") . '' . '