//end while } if ($debug) { echo "dbg: desbloqueo este registro\n"; } Model::query('UPDATE mailer_content SET blocked = 0 WHERE id = ?', array($mailing->id)); } else { if ($debug) { echo "dbg: FALLO\n"; } } if ($debug) { echo "dbg: FIN, tiempo de ejecución total " . round(microtime(true) - $itime, 2) . " segundos para enviar {$total_users} emails, ratio medio " . round($total_users / (microtime(true) - $itime), 2) . " emails/segundo\n"; } // limpiamos antiguos procesados Sender::cleanOld(); /** * Comprueba si se está ejecutando un proceso cli-sendmail.php con un pid determinado * @param [type] $pid [description] * @return [type] [description] */ function check_pid($args, $pid = null) { $filter = escapeshellarg(escapeshellcmd(__DIR__ . "/cli-sendmail.php {$args}")); $order = "ps x | grep {$filter} | grep -v grep | awk '{ print \$1 }'"; // $order = "pgrep -f $filter"; $lines = shell_exec($order); if ($lines) { // echo "[$pid] ";print_r($lines);print_r($order); if ($pid) { $lines = array_map('trim', explode("\n", $lines));
public static function process($action = 'list', $id = null, $filters = array()) { switch ($action) { case 'init': if ($_SERVER['REQUEST_METHOD'] == 'POST') { // plantilla $template = $_POST['template']; // destinatarios if ($_POST['test']) { $users = Boletin::getTesters(); } elseif ($template == 33) { // los destinatarios de newsletter $users = Boletin::getReceivers(); } elseif ($template == 35) { // los destinatarios para testear a subscriptores $users = Boletin::getReceivers(); } // sin idiomas $nolang = $_POST['nolang']; if ($nolang) { $receivers[LANG] = $users; } else { // separamos destinatarios en idiomas $receivers = array(); foreach ($users as $usr) { if (empty($usr->lang) || $usr->lang == LANG) { $receivers[LANG][] = $usr; } else { $receivers[$usr->lang][] = $usr; } } } // idiomas que vamos a enviar $langs = array_keys($receivers); // para cada idioma foreach ($langs as $lang) { // destinatarios $recipients = $receivers[$lang]; // datos de la plantilla $tpl = Template::get($template, $lang); // contenido de newsletter $content = $template == 33 ? Boletin::getContent($tpl->text, $lang) : ($content = $tpl->text); // asunto $subject = $tpl->title; // creamos instancia $sql = "INSERT INTO mail (id, email, html, template) VALUES ('', :email, :html, :template)"; $values = array(':email' => 'any', ':html' => $content, ':template' => $template); $query = \Goteo\Core\Model::query($sql, $values); $mailId = \Goteo\Core\Model::insertId(); // inicializamos el envío if (Sender::initiateSending($mailId, $subject, $recipients, true)) { // ok... } else { Message::Error('No se ha podido iniciar el mailing con asunto "' . $subject . '"'); } } } throw new Redirection('/admin/newsletter'); break; case 'activate': if (Sender::activateSending($id)) { Message::Info('Se ha activado un nuevo envío automático'); } else { Message::Error('No se pudo activar el envío. Iniciar de nuevo'); } throw new Redirection('/admin/newsletter'); break; case 'detail': $mailing = Sender::getSending($id); $list = Sender::getDetail($id, $filters['show']); return new View('view/admin/index.html.php', array('folder' => 'newsletter', 'file' => 'detail', 'detail' => $filters['show'], 'mailing' => $mailing, 'list' => $list)); break; default: $list = Sender::getMailings(); return new View('view/admin/index.html.php', array('folder' => 'newsletter', 'file' => 'list', 'list' => $list)); } }
die("Se necesita un identificador de sender como argumento del script!"); } $list = array(); $sql = "SELECT\n mailer_send.id,\n mailer_send.user,\n mailer_send.name,\n mailer_send.email,\n mailer_content.id as mailing_id\n FROM mailer_send\n RIGHT JOIN mailer_content ON mailer_content.id=mailer_send.mailing AND mailer_content.active=1\n WHERE mailer_send.id = ?\n AND mailer_send.sended IS NULL\n AND mailer_send.blocked IS NULL\n "; if ($query = Model::query($sql, array($id))) { $user = $query->fetchObject(); } if (!is_object($user)) { die("No se ha encontrado un usuario válido para el mailer_send.id={$id}\n"); } //si estamos aqui sabemos que el usuari es valido i el mailing tambien if ($debug) { echo "dbg: Fecha inicio " . date("Y-m-d H:i:s") . "\n"; } // cogemos el siguiente envío a tratar $mailing = Sender::getSending($user->mailing_id); // print_r($mailing); // si no está activa fin if (!$mailing->active) { die("Mailing {$user->mailing_id} inactivo!\n"); } // cogemos el contenido y la plantilla desde el historial $query = Model::query('SELECT html, template FROM mail WHERE id = ?', array($mailing->mail)); $data = $query->fetch(\PDO::FETCH_ASSOC); $content = $data['html']; $template = $data['template']; if (empty($content)) { die("Mailing {$user->mailing_id} sin contenido!\n"); } if ($debug) { echo "dbg: Bloqueando registro {$user->id} ({$user->email}) mailing: {$user->mailing_id}\n";
/** * Realiza el envio masivo a participantees o cofinanciadores * * @param type $option 'messegers' || 'rewards' * @param type $project Instancia del proyecto de trabajo * @return boolean */ public static function process_mailing($option, $project) { $who = array(); // verificar que hay mensaje if (empty($_POST['message'])) { Message::Error(Text::get('dashboard-investors-mail-text-required')); return false; } else { $msg_content = nl2br(\strip_tags($_POST['message'])); } // si a todos los participantes if ($option == 'messegers' && !empty($_POST['msg_all'])) { // a todos los participantes foreach (Model\Message::getMessegers($project->id) as $messeger => $msgData) { if ($messeger == $project->owner) { continue; } $who[$messeger] = $messeger; // unset($msgData); // los datos del mensaje del participante no se usan } } elseif ($option == 'rewards' && !empty($_POST['msg_all'])) { // a todos los cofinanciadores foreach (Model\Invest::investors($project->id, false, true) as $user => $investor) { // no duplicar $who[$investor->user] = $investor->user; } } elseif (!empty($_POST['msg_user'])) { // a usuario individual $who[$_POST['msg_user']] = $_POST['msg_user']; } elseif ($option == 'rewards') { $msg_rewards = array(); // estos son msg_reward-[rewardId], a un grupo de recompensa foreach ($_POST as $key => $value) { $parts = explode('-', $key); if ($parts[0] == 'msg_reward' && $value == 1) { $msg_rewards[] = $parts[1]; } } // para cada recompensa foreach ($msg_rewards as $reward) { foreach (Model\Invest::choosed($reward) as $user) { $who[$user] = $user; } } } // no hay destinatarios if (count($who) == 0) { Message::Error(Text::get('dashboard-investors-mail-nowho')); return false; } // obtener contenido // segun destinatarios $allsome = explode('/', Text::get('regular-allsome')); $enviandoa = !empty($_POST['msg_all']) ? $allsome[0] : $allsome[1]; if ($option == 'messegers') { Message::Info(Text::get('dashboard-messegers-mail-sendto', $enviandoa)); } else { Message::Info(Text::get('dashboard-investors-mail-sendto', $enviandoa)); } // Obtenemos la plantilla para asunto y contenido $template = Template::get(2); // Sustituimos los datos if (!empty($_POST['subject'])) { $subject = $_POST['subject']; } else { $subject = str_replace('%PROJECTNAME%', $project->name, $template->title); } $remite = $project->name . ' ' . Text::get('regular-from') . ' '; $remite .= NODE_ID != GOTEO_NODE ? NODE_NAME : GOTEO_MAIL_NAME; $search = array('%MESSAGE%', '%PROJECTNAME%', '%PROJECTURL%', '%OWNERURL%', '%OWNERNAME%'); $replace = array($msg_content, $project->name, SITE_URL . "/project/" . $project->id, SITE_URL . "/user/profile/" . $project->owner, $project->user->name); $content = \str_replace($search, $replace, $template->text); // para usar el proceso Sender: // - $who debe ser compatible con el formato $receivers // (falta nombre e email), sacarlo con getMini $receivers = array(); foreach ($who as $userId) { $user = Model\User::getMini($userId); $user->user = $user->id; $receivers[] = $user; } // - en la plantilla hay que cambiar %NAME% por %USERNAME% para que sender reemplace // - // - se crea un registro de tabla mail $sql = "INSERT INTO mail (id, email, html, template, node) VALUES ('', :email, :html, :template, :node)"; $values = array(':email' => 'any', ':html' => $content, ':template' => $template->id, ':node' => \GOTEO_NODE); $query = \Goteo\Core\Model::query($sql, $values); $mailId = \Goteo\Core\Model::insertId(); // - se usa el metodo initializeSending para grabar el envío (parametro para autoactivar) // , también metemos el reply y repplyName (remitente) en la instancia de envío if (\Goteo\Library\Sender::initiateSending($mailId, $subject, $receivers, 1, $project->user->email, $remite)) { Message::Info(Text::get('dashboard-investors-mail-sended', 'la cola de envíos')); // cambiar este texto } else { Message::Error(Text::get('dashboard-investors-mail-fail', 'la cola de envíos')); // cambiar este texto } return true; }
public static function process($action = 'list', $id = null, $filters = array()) { // año fiscal $year = Model\User\Donor::$currYear; $year0 = $year; $year1 = $year - 1; $errors = array(); $node = isset($_SESSION['admin_node']) ? $_SESSION['admin_node'] : \GOTEO_NODE; // Valores de filtro $interests = Model\User\Interest::getAll(); $status = Model\Project::status(); $methods = Model\Invest::methods(); $types = array('investor' => 'Cofinanciadores', 'owner' => 'Autores', 'user' => 'Usuarios'); $roles = array('admin' => 'Administrador', 'checker' => 'Revisor', 'translator' => 'Traductor'); // una variable de sesion para mantener los datos de todo esto if (!isset($_SESSION['mailing'])) { $_SESSION['mailing'] = array(); } switch ($action) { case 'edit': $_SESSION['mailing']['receivers'] = array(); $values = array(); $sqlFields = ''; $sqlInner = ''; $sqlFilter = ''; // cargamos los destiantarios //---------------------------- // por tipo de usuario switch ($filters['type']) { case 'investor': $sqlInner .= "INNER JOIN invest\n ON invest.user = user.id\n AND (invest.status = 0 OR invest.status = 1 OR invest.status = 3 OR invest.status = 4)\n INNER JOIN project\n ON project.id = invest.project\n "; $sqlFields .= ", project.name as project"; $sqlFields .= ", project.id as projectId"; break; case 'owner': $sqlInner .= "INNER JOIN project\n ON project.owner = user.id\n "; $sqlFields .= ", project.name as project"; $sqlFields .= ", project.id as projectId"; break; default: break; } $_SESSION['mailing']['filters_txt'] = 'los <strong>' . $types[$filters['type']] . '</strong> '; if (!empty($filters['project']) && !empty($sqlInner)) { $sqlFilter .= " AND project.name LIKE (:project) "; $values[':project'] = '%' . $filters['project'] . '%'; $_SESSION['mailing']['filters_txt'] .= 'de proyectos que su nombre contenga <strong>\'' . $filters['project'] . '\'</strong> '; } elseif (empty($filters['project']) && !empty($sqlInner)) { $_SESSION['mailing']['filters_txt'] .= 'de cualquier proyecto '; } if (isset($filters['status']) && $filters['status'] > -1 && !empty($sqlInner)) { $sqlFilter .= "AND project.status = :status "; $values[':status'] = $filters['status']; $_SESSION['mailing']['filters_txt'] .= 'en estado <strong>' . $status[$filters['status']] . '</strong> '; } elseif ($filters['status'] < 0 && !empty($sqlInner)) { $_SESSION['mailing']['filters_txt'] .= 'en cualquier estado '; } if ($filters['type'] == 'investor') { if (!empty($filters['method']) && !empty($sqlInner)) { $sqlFilter .= "AND invest.method = :method "; $values[':method'] = $filters['method']; $_SESSION['mailing']['filters_txt'] .= 'mediante <strong>' . $methods[$filters['method']] . '</strong> '; } elseif (empty($filters['method']) && !empty($sqlInner)) { $_SESSION['mailing']['filters_txt'] .= 'mediante cualquier metodo '; } } if (!empty($filters['interest'])) { $sqlInner .= "INNER JOIN user_interest\n ON user_interest.user = user.id\n AND user_interest.interest = :interest\n "; $values[':interest'] = $filters['interest']; if ($filters['interest'] == 15) { $_SESSION['mailing']['filters_txt'] .= 'del grupo de testeo '; } else { $_SESSION['mailing']['filters_txt'] .= 'interesados en fin <strong>' . $interests[$filters['interest']] . '</strong> '; } } if (!empty($filters['role'])) { $sqlInner .= "INNER JOIN user_role\n ON user_role.user_id = user.id\n AND user_role.role_id = :role\n "; $values[':role'] = $filters['role']; $_SESSION['mailing']['filters_txt'] .= 'que sean <strong>' . $roles[$filters['role']] . '</strong> '; } if (!empty($filters['name'])) { $sqlFilter .= " AND ( user.name LIKE (:name) OR user.email LIKE (:name) ) "; $values[':name'] = '%' . $filters['name'] . '%'; $_SESSION['mailing']['filters_txt'] .= 'que su nombre o email contenga <strong>\'' . $filters['name'] . '\'</strong> '; } if (!empty($filters['donant'])) { if ($filters['type'] == 'investor') { $sqlFilter .= " AND invest.resign = 1\n AND invest.status IN (1, 3)\n AND invest.charged >= '{$year0}-01-01'\n AND invest.charged < '{$year1}-01-01'\n AND (project.passed IS NOT NULL AND project.passed != '0000-00-00')\n "; $_SESSION['mailing']['filters_txt'] .= 'que haya hecho algun donativo '; } else { Message::Error('Solo se filtran donantes si se envia "A los: Cofinanciadores"'); } } if ($node != \GOTEO_NODE) { $sqlFilter .= " AND user.node = :node"; $values[':node'] = $node; if (!empty($sqlInner)) { $sqlFilter .= " AND project.node = :node"; } } $sql = "SELECT\n user.id as id,\n user.id as user,\n user.name as name,\n user.email as email\n {$sqlFields}\n FROM user\n {$sqlInner}\n WHERE user.active = 1\n {$sqlFilter}\n GROUP BY user.id\n ORDER BY user.name ASC\n "; // die('<pre>'.$sql . '<br />'.print_r($values, 1).'</pre>'); if ($query = Model\User::query($sql, $values)) { foreach ($query->fetchAll(\PDO::FETCH_OBJ) as $receiver) { $_SESSION['mailing']['receivers'][$receiver->id] = $receiver; } } else { Message::Error('Fallo el SQL!!!!! <br />' . $sql . '<pre>' . print_r($values, 1) . '</pre>'); } // si no hay destinatarios, salta a la lista con mensaje de error if (empty($_SESSION['mailing']['receivers'])) { Message::Error('No se han encontrado destinatarios para ' . $_SESSION['mailing']['filters_txt']); throw new Redirection('/admin/mailing/list'); } // si hay, mostramos el formulario de envio return new View('view/admin/index.html.php', array('folder' => 'mailing', 'file' => 'edit', 'filters' => $filters, 'interests' => $interests, 'status' => $status, 'types' => $types, 'roles' => $roles)); break; case 'send': // die(\trace($_POST)); $URL = NODE_ID != GOTEO_NODE ? NODE_URL : SITE_URL; // Enviando contenido recibido a destinatarios recibidos $receivers = array(); $subject = $_POST['subject']; $templateId = !empty($_POST['template']) ? $_POST['template'] : 11; $content = \str_replace('%SITEURL%', $URL, $_POST['content']); // quito usuarios desmarcados foreach ($_SESSION['mailing']['receivers'] as $usr => $userData) { $errors = array(); $campo = 'receiver_' . $usr; if (!isset($_POST[$campo])) { $_SESSION['mailing']['receivers'][$usr]->ok = null; } else { $receivers[] = $userData; } } // montamos el mailing // - se crea un registro de tabla mail $sql = "INSERT INTO mail (id, email, html, template, node) VALUES ('', :email, :html, :template, :node)"; $values = array(':email' => 'any', ':html' => $content, ':template' => $templateId, ':node' => $node); $query = \Goteo\Core\Model::query($sql, $values); $mailId = \Goteo\Core\Model::insertId(); // - se usa el metodo initializeSending para grabar el envío (parametro para autoactivar) // - initiateSending ($mailId, $subject, $receivers, $autoactive = 0) if (\Goteo\Library\Sender::initiateSending($mailId, $subject, $receivers, 1)) { $ok = true; // Evento Feed $log = new Feed(); $log->populate('comunicación masiva a usuarios (admin)', '/admin/mailing', \vsprintf("El admin %s ha iniciado una %s a %s", array(Feed::item('user', $_SESSION['user']->name, $_SESSION['user']->id), Feed::item('relevant', 'Comunicacion masiva'), $_SESSION['mailing']['filters_txt']))); $log->doAdmin('admin'); unset($log); } else { $ok = false; // Evento Feed $log = new Feed(); $log->populate('comunicación masiva a usuarios (admin)', '/admin/mailing', \vsprintf("El admin %s le ha %s una %s a %s", array(Feed::item('user', $_SESSION['user']->name, $_SESSION['user']->id), Feed::item('relevant', 'fallado'), Feed::item('relevant', 'Comunicacion masiva'), $_SESSION['mailing']['filters_txt']))); $log->doAdmin('admin'); unset($log); } return new View('view/admin/index.html.php', array('folder' => 'mailing', 'file' => 'send', 'subject' => $subject, 'interests' => $interests, 'status' => $status, 'methods' => $methods, 'types' => $types, 'roles' => $roles, 'users' => $receivers, 'ok' => $ok)); break; } return new View('view/admin/index.html.php', array('folder' => 'mailing', 'file' => 'list', 'interests' => $interests, 'status' => $status, 'methods' => $methods, 'types' => $types, 'roles' => $roles, 'filters' => $filters)); }