Example #1
0
/**
 * Process an uploaded CSV file to find new recipients.
 *
 * @param array $recipients previous recipients, to prevent duplicates
 * Contains:
 *
 * user_guids => array() existing users
 * emails => array() extra email addresses
 *
 * @return array
 */
function newsletter_process_csv_upload(array $recipients)
{
    // is a file uploaded
    if (get_uploaded_file("csv")) {
        // open the file as CSV
        $fh = fopen($_FILES["csv"]["tmp_name"], "r");
        if (!empty($fh)) {
            $email_column = false;
            // try to find an email column (in the first 2 rows)
            for ($i = 0; $i < 2; $i++) {
                $row = fgetcsv($fh, null, ";", "\"");
                if ($row) {
                    foreach ($row as $index => $field) {
                        if (newsletter_is_email_address($field)) {
                            $email_column = $index;
                            break;
                        }
                    }
                }
            }
            // found an email column
            if ($email_column !== false) {
                $counter = 0;
                // start at the beginning
                if (rewind($fh)) {
                    $row = fgetcsv($fh, null, ";", "\"");
                    while ($row !== false) {
                        // get the email address
                        $email = @$row[$email_column];
                        // make sure it's a valid email address
                        if (newsletter_is_email_address($email)) {
                            $counter++;
                            $exists = false;
                            // is this email address already in the recipients list
                            if (in_array($email, $recipients["emails"])) {
                                $exists = true;
                            } else {
                                // check for an existing user
                                $ia = elgg_set_ignore_access(true);
                                $users = get_user_by_email($email);
                                if (!empty($users)) {
                                    foreach ($users as $user) {
                                        if (in_array($user->getGUID(), $recipients["user_guids"])) {
                                            $exists = true;
                                        }
                                    }
                                }
                                elgg_set_ignore_access($ia);
                            }
                            if ($exists === false) {
                                // email address wasn't added yet
                                // so add to the list
                                $ia = elgg_set_ignore_access(true);
                                $users = get_user_by_email($email);
                                if (!empty($users)) {
                                    $recipients["user_guids"][] = $users[0]->getGUID();
                                } else {
                                    $recipients["emails"][] = $email;
                                }
                                elgg_set_ignore_access($ia);
                            }
                        }
                        // go to the next row
                        $row = fgetcsv($fh, null, ";", "\"");
                    }
                    // done, report the added emails
                    system_message(elgg_echo("newsletter:csv:added", array($counter)));
                }
            } else {
                // no email column found, report this
                system_message(elgg_echo("newsletter:csv:no_email"));
            }
        }
    }
    return $recipients;
}
Example #2
0
                    } else {
                        register_error(elgg_echo('newsletter:action:subscribe:error:unsubscribe'));
                    }
                } else {
                    // subscribe
                    if (newsletter_subscribe_user($user, $entity)) {
                        system_message(elgg_echo('newsletter:action:subscribe:success'));
                    } else {
                        register_error(elgg_echo('newsletter:action:subscribe:error:subscribe'));
                    }
                }
            } else {
                register_error(elgg_echo('error:missing_data'));
            }
        } else {
            if (newsletter_is_email_address($email)) {
                // add the email address to the subscriber list
                if (newsletter_subscribe_email($email, $entity)) {
                    system_message(elgg_echo('newsletter:action:subscribe:success'));
                } else {
                    register_error(elgg_echo('newsletter:action:subscribe:error:subscribe'));
                }
            } else {
                register_error(elgg_echo('registration:notemail'));
            }
        }
    } else {
        register_error(elgg_echo('actionunauthorized'));
    }
} else {
    register_error(elgg_echo('error:missing_data'));
Example #3
0
                $key = strtolower($user->name) . $user->getGUID();
                $result[$key] = newsletter_format_recipient($user);
            }
        }
        if (!elgg_instanceof($entity->getContainerEntity(), "group")) {
            // search for groups
            $options = array("type" => "group", "limit" => $limit, "joins" => array("JOIN " . $dbprefix . "groups_entity ge ON e.guid = ge.guid"), "wheres" => array("(ge.name LIKE '%" . $filtered_query . "%' OR ge.description LIKE '%" . $filtered_query . "%')"));
            $groups = elgg_get_entities($options);
            if (!empty($groups)) {
                foreach ($groups as $group) {
                    $key = strtolower($group->name) . $group->getGUID();
                    $result[$key] = newsletter_format_recipient($group);
                }
            }
        }
        // email input
        if (newsletter_is_email_address($query)) {
            if ($users = get_user_by_email($query)) {
                // found a user with this email address
                $key = strtolower($users[0]->name) . $users[0]->getGUID();
                $result[$key] = newsletter_format_recipient($users[0]);
            } else {
                // no user found
                $result[$query] = newsletter_format_recipient($query);
            }
        }
    }
}
ksort($result);
header("Content-Type: application/json");
echo json_encode(array_values($result));
Example #4
0
/**
 * Validate the custom from email address
 *
 * This also triggers a plugin hook 'from_email', 'newsletter' for other plugins to hook into
 * Supplied params:
 * - email: the email address to validate
 *
 * @param string $from_email the email address to check
 *
 * @return bool
 */
function newsletter_validate_custom_from($from_email)
{
    if (empty($from_email)) {
        // empty is allowed, sending will fallback to container
        return true;
    }
    if (!newsletter_is_email_address($from_email)) {
        // not an email address, always fail
        return false;
    }
    $result = true;
    // check plugin settings domain limitations
    $plugin_setting = elgg_get_plugin_setting('custom_from_domains', 'newsletter');
    if (!empty($plugin_setting)) {
        $result = false;
        $plugin_setting = string_to_tag_array($plugin_setting);
        list(, $domain) = explode('@', $from_email);
        foreach ($plugin_setting as $allowed_domain) {
            if ($domain === $allowed_domain) {
                // custom from is from an allowed domain
                $result = true;
                break;
            }
        }
    }
    // trigger a plugin hook so others are allowed to validate
    return (bool) elgg_trigger_plugin_hook('from_email', 'newsletter', ['email' => $from_email], $result);
}
Example #5
0
                }
            } else {
                $recipient_error = true;
                register_error(elgg_echo("newsletter:action:unsubscribe:error:recipient", array($recipient)));
            }
        }
        // unsubscribe from all
        if (!empty($all)) {
            if (is_numeric($recipient) && ($user = get_user($recipient))) {
                if (newsletter_unsubscribe_all_user($user)) {
                    $forward_url = "";
                    system_message(elgg_echo("newsletter:action:unsubscribe:success:all"));
                } else {
                    register_error(elgg_echo("newsletter:action:unsubscribe:error:all"));
                }
            } elseif (newsletter_is_email_address($recipient)) {
                if (newsletter_unsubscribe_all_email($recipient)) {
                    $forward_url = "";
                    system_message(elgg_echo("newsletter:action:unsubscribe:success:all"));
                } else {
                    register_error(elgg_echo("newsletter:action:unsubscribe:error:all"));
                }
            } elseif (!$recipient_error) {
                register_error(elgg_echo("newsletter:action:unsubscribe:error:recipient", array($recipient)));
            }
        }
    } else {
        register_error(elgg_echo("newsletter:unsubscribe:error:code"));
    }
} else {
    register_error(elgg_echo("InvalidParameterException:MissingParameter"));