tiki-invite.php 5.19 KB
Newer Older
1
<?php
2
// (c) Copyright 2002-2016 by authors of the Tiki Wiki CMS Groupware Project
3
//
4 5 6
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
7
require_once ('tiki-setup.php');
8 9
$access->check_feature('feature_invite');
$access->check_permission('tiki_p_invite');
10 11 12 13 14 15 16

require_once ('lib/webmail/tikimaillib.php');

@ini_set('max_execution_time', 0); //will not work if safe_mode is on
$prefs['feature_wiki_protect_email'] = 'n'; //not to alter the email

/* csv format: lastname,firstname,mail */
17 18 19 20
/**
 * @param $bloc
 * @return array
 */
21 22
function parsemails_csv($bloc)
{
23 24
	$results=array();
	$lines = preg_split('/[\n\r]+/', $bloc);
25
	foreach ($lines as $line) {
26 27 28 29 30 31 32 33 34 35 36 37
		$l=explode(',', $line);
		$r=array();
		$r['lastname']=trim($l[0]);
		$r['firstname']=trim($l[1]);
		$r['email']=trim($l[2]);
		if (strpos($r['email'], '@') !== FALSE)
			$results[]=$r;
	}
	return $results;
}

/* everything format */
38 39 40 41
/**
 * @param $bloc
 * @return array
 */
42 43
function parsemails_all($bloc)
{
44 45 46
	$bloc=str_replace("\r\n", "\n", $bloc);
	$bloc=str_replace("\n\r", "\n", $bloc);
	$bloc=str_replace("\r", "\n", $bloc);
47
	$mails = preg_split('/[^a-zA-Z0-9@._-]/', $bloc);
48 49

	$results=array();
50
	foreach ($mails as $m) {
51 52 53 54 55 56 57 58 59 60 61 62 63
		$m=trim($m);
		if (strpos($m, '@') === FALSE) continue;
		if (strpos($m, '.') === FALSE) continue;
		$r=array();
		$r['lastname']='';
		$r['firstname']='';
		$r['email']=$m;
		$results[]=$r;
	}
	return $results;
}

$previous=array();
64
$res=$tikilib->query("SELECT * FROM `tiki_invite` ORDER by `ts` DESC");
65 66 67
while (is_array($row = $res->fetchRow())) {
    $row['datetime'] = strftime('%c', $row['ts']);
    $previous[$row['id']] = $row;
68 69 70 71 72
}
$smarty->assign('previous', $previous);

if (isset($_REQUEST['loadprevious']) && !empty($_REQUEST['loadprevious']) && isset($previous[(int)$_REQUEST['loadprevious']])) {
    $prev=$previous[(int)$_REQUEST['loadprevious']];
73
    $res=$tikilib->query("SELECT * FROM `tiki_invited` WHERE id_invite=?", array((int)$_REQUEST['loadprevious']));
74
    $prev_invited="";
75
    while(is_array($row = $res->fetchRow())) $prev_invited.=$row['lastname'].','.$row['firstname'].','.$row['email']."\n";
76

77 78 79 80 81 82
    $_REQUEST['emailslist'] = $prev_invited;
    $_REQUEST['emailslist_format'] = 'csv';
    $_REQUEST['emailsubject'] = $prev['emailsubject'];
    $_REQUEST['emailcontent'] = $prev['emailcontent'];
    $_REQUEST['wikicontent'] = $prev['wikicontent'];
    $_REQUEST['wikipageafter'] = $prev['wikipageafter'];
83
    $_REQUEST['invitegroups'] = explode(',', $prev['groups']);
84 85 86 87
}

$user_details = $userlib->get_user_details($user);
$allgroups = $userlib->get_groups(0, -1, 'groupName_desc', '', '', 'n');
88
$invitegroups=array();
89
foreach ($allgroups['data'] as $agroup)
90 91
    $invitegroups[$agroup['groupName']] = $agroup['groupDesc'];
$smarty->assign("invitegroups", $invitegroups);
92 93 94 95
$smarty->assign("usergroups", $user_details['groups']);


if (isset($_REQUEST['send'])) {
96

97 98 99 100
    $_text=$_REQUEST["emailcontent"];
    $_text=str_replace("\r\n", "\n", $_text);
    $_text=str_replace("\n\r", "\n", $_text);
    $_text=str_replace("\r", "\n", $_text);
101

102
    $mails=$_REQUEST["emailslist"];
103

104
	switch($_REQUEST['emailslist_format']) {
105 106 107 108 109 110
		case 'all':
			$emails=parsemails_all($mails);
			break;
		case 'csv':
			$emails=parsemails_csv($mails);
			break;
111 112
		default: $emails=array();
	}
113

114
    $igroups=$_REQUEST['invitegroups'];
115

116 117
	if (!empty($_REQUEST['confirm'])) {
		$tikilib->query(
118 119 120 121 122 123 124 125 126 127
			"INSERT INTO `tiki_invite` (inviter, groups, ts, emailsubject,emailcontent,wikicontent,wikipageafter) VALUES (?,?,?,?,?,?,?)",
			array(
				$user,
				count($igroups) ? implode(',', $igroups) : null,
				$tikilib->now,
				$_REQUEST['emailsubject'],
				$_REQUEST['emailcontent'],
				$_REQUEST['wikicontent'],
				empty($_REQUEST['wikipageafter']) ? NULL : $_REQUEST['wikipageafter']
			)
128 129
		);
      $res=$tikilib->query("SELECT MAX(id) as `id` FROM `tiki_invite` WHERE `inviter`=? AND `ts`=?", array($user, $tikilib->now));
130
        $row=$res->fetchRow(); $id=$row['id'];
131

132
      foreach ($emails as $m)
133 134 135 136
			$tikilib->query(
				"INSERT INTO `tiki_invited` (id_invite, email, firstname, lastname, used) VALUES (?,?,?,?,?)",
				array($id, $m['email'], $m['firstname'], $m['lastname'], "no")
			);
137

138
		  $_SERVER['SCRIPT_URI'] =  empty($_SERVER['SCRIPT_URI']) ? 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] : $_SERVER['SCRIPT_URI'];
139
        foreach ($emails as $m) {
140 141 142
            $mail = new TikiMail();
            $mail->setFrom($prefs['sender_email']);
            $mail->setSubject($_REQUEST["emailsubject"]);
143 144
            $url=str_replace('tiki-invite.php', 'tiki-invited.php', $_SERVER['SCRIPT_URI'])
                .'?invite='.$id.'&email='.urlencode($m['email']);
145 146 147 148 149 150 151 152
            $text=$_text;
            $text=str_replace('{link}', $url, $text);
            $text=str_replace('{email}', $m['email'], $text);
            $text=str_replace('{firstname}', $m['firstname'], $text);
            $text=str_replace('{lastname}', $m['lastname'], $text);
            $mail->setText($text);
            $mail->send(array($m['email']));
        }
153

154
        $smarty->assign('sentresult', true);
155 156
	}
   $smarty->assign('emails', $emails);
157 158 159
}


160
$smarty->assign('mid', 'tiki-invite.tpl');
161
$smarty->display("tiki.tpl");