tiki-mailin-code.php 11 KB
Newer Older
1 2
<?php

3
// Copyright (c) 2002-2007, Luis Argerich, Garland Foster, Eduardo Polidor, et. al.
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.

7
//this script may only be included - so its better to die if called directly.
michael_davey's avatar
michael_davey committed
8 9 10
//smarty is not there - we need setup
require_once('tiki-setup.php');  
$access->check_script($_SERVER["SCRIPT_NAME"],basename(__FILE__));
11

12 13
include_once ('lib/mailin/mailinlib.php');

14 15
// require_once ("lib/webmail/pop3.php");
require_once ("lib/webmail/net_pop3.php");
16 17
include_once ("lib/webmail/class.rc4crypt.php");

18 19
include_once ("lib/mail/mimelib.php");
include_once ("lib/webmail/tikimaillib.php");
20
include_once ('lib/wiki/wikilib.php');
21

22
function mailin_check_attachments(&$output, &$out, $page, $user) {
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  global  $wikilib;
  $cnt = 0;
  if (!isset($output["parts"]))
	return;
  for ($it = 0; $it < count($output["parts"]); $it++)
  {
    if (isset($output["parts"][$it]["d_parameters"]["filename"]))
    {
      $attachmentPart = $output["parts"][$it];
      $fileName = $attachmentPart["d_parameters"]["filename"];
	if (isset($attachmentPart["ctype_primary"]))
		$fileType = $attachmentPart["ctype_primary"] ."/". $attachmentPart["ctype_secondary"];
	else
	      $fileType="";
      $fileData = $attachmentPart["body"];
      $fileSize = strlen($fileData);
      $wikilib->wiki_attach_file($page, $fileName, $fileType, $fileSize, $fileData, "attached by mail", $user, "");
      $cnt++;
    }
  }
  $out .= $cnt;
44
  $out .= " attachment(s) added<br />";
45
}
46
function mailin_get_body($output) {
47 48 49 50 51 52 53 54 55 56 57
	if (isset($output['text'][0]))
		$body = $output["text"][0];
	elseif (isset($output['parts'][0]) && isset($output['parts'][0]["text"][0]))
		$body = $output['parts'][0]["text"][0];
	elseif (isset($output['parts'][0]) && isset($output['parts'][0]['parts'][0]) && isset($output['parts'][0]['parts'][0]["text"][0]))
		$body = $output['parts'][0]['parts'][0]["text"][0];
	else
		$body = '';
	return $body;

}
58 59 60 61 62
// The mailin script is used to get / set wiki pages using an email account

// Get a list of ACTIVE emails accounts configured for mailin procedures
$accs = $mailinlib->list_active_mailin_accounts(0, -1, 'account_desc', '');

63 64
//print ('<a href="tiki-admin_mailin.php">Admin</a><br /><br />');
$content = '<br /><br />';
65 66 67

// foreach account
foreach ($accs['data'] as $acc) {
68 69 70 71
  $content .= "<b>Processing account</b><br />";
  $content .= "Account :" . $acc['account'] . "<br />";
  $content .= "Type    :" . $acc['type'] . "<br />";
  $content .= "--------------------------<br />";
72 73 74 75 76 77
  //$pop3 = new POP3($acc["pop"], $acc["username"], $acc["pass"]);
  //$pop3->Open();
	$pop3 = new Net_Pop3();
	$pop3->connect($acc["pop"]);
	$pop3->login($acc["username"], $acc["pass"]);
  $mailsum = $pop3->numMsg();
78 79

  for ($i = 1; $i <= $mailsum; $i++) {
80 81 82 83 84 85
    $aux = $pop3->getParsedHeaders($i);
var_dump($aux);
 		if (!isset($aux["From"])) $aux['From'] = $aux['Return-path'];
		preg_match('/<?([-!#$%&\'*+\.\/0-9=?A-Z^_`a-z{|}~][email protected][-!#$%&\'*+\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\.\/0-9=?A-Z^_`a-z{|}~]+)>?/',$aux["From"],$mail);
		$email_from = $mail[1];
						 
86
    $aux["msgid"] = $i;
87 88 89
    $aux["realmsgid"] = ereg_replace("[<>]","",$aux["Message-ID"]);
    $message = $pop3->getMsg($i);
    $output = mime::decode($message);
90 91
        //mailin_parse_output($output, $parts, 0);

92
    $content .= "Reading a request.<br />From: " . $aux["From"] . "<br />Subject: " . $output['header']['subject'] . "<br />";
93

94 95
    $content .= "sender email: " . $email_from . "<br />";
    $aux["sender"]["user"] = $userlib->get_user_by_email($email_from);
96
    $content .= "sender user: " .  $aux["sender"]["user"] . "<br />";
97 98 99

    $cantUseMailIn = $acc["anonymous"]=='n' && empty($aux["sender"]["user"]);
    if($cantUseMailIn) {
100
      $content .= "Anonymous user acces denied, sending auto-reply to email address:&nbsp;" .  $aux["From"] . "<br />";
101
      $mail = new TikiMail();
102
      $mail->setFrom($acc["account"]);
103
      $c = $prefs['default_mail_charset'];
104 105
      $mail->setHeadCharset($c);
      $mail->setTextCharset($c);
106
      $l = $prefs['language'];
107
      $mail->setSubject(tra('Tiki mail-in auto-reply', $l));
108
      $mail->setSMTPParams($acc["smtp"], $acc["smtpPort"], '', $acc["useAuth"], $acc["username"], $acc["pass"]);
109
      $mail->setText(tra("Sorry, you can't use this feature.", $l));
110
      $res = $mail->send(array($email_from), 'mail');
111
      $content .= "Response sent<br />";
112 113
    } else {
      if (empty($aux["sender"]["user"]))
114
        $aux["sender"]["user"] = $email_from;
115 116
  
      if (empty($aux["sender"]["name"]))
117
        $aux["sender"]["name"] = $email_from;
118
  
119 120
      if ($acc['type'] == 'article-put') {
        // This is used to CREATE articles
121
        $title = trim($output['header']['subject']);
122
  
123
        $msgbody = mailin_get_body($output);
124
        if ($msgbody && isset($acc['discard_after'])) {
125
             $msgbody = preg_replace("/".$acc['discard_after'].".*$/s", "", $msgbody);
126 127 128
        }
        
        $heading = $msgbody;
129
        $topicId = $acc['article_topicId'];
130 131
        $userm = $aux['sender']['user'];
        $authorName = $userm;
132
        $body = '';
mose's avatar
mose committed
133
        $publishDate = $tikilib->now;
134 135
        $cur_time = explode(',', $tikilib->date_format('%Y,%m,%d,%H,%M,%S', $publishDate));
		$expireDate = $tikilib->make_time($cur_time[3], $cur_time[4], $cur_time[5], $cur_time[1], $cur_time[2], $cur_time[0]+1);
136
        $subId = 0;
137
        $type = $acc['article_type'];
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
        $useImage = 'n';
        $image_x = '';
        $image_y = '';
        $imgname = '';
        $imgsize = '';
        $imgtype = '';
        $imgdata = '';
       	$topline = '';
		$subtitle = '';
		$linkto = '';
		$image_caption = '';
		$lang = '';
		$rating = 7;
		$isfloat = 'n';
        
153 154 155 156
		global $artlib;
		if (!is_object($artlib)) {
			include_once('lib/articles/artlib.php');
		}
157
		$subid = $artlib->replace_submission($title, $authorName, $topicId, $useImage, $imgname, $imgsize, $imgtype, $imgdata, $heading, 
158
											$body, $publishDate, $expireDate, $userm, $subId, $image_x, $image_y, $type, 
159 160
											$topline, $subtitle, $linkto, $image_caption, $lang, $rating, $isfloat);

161 162 163 164 165 166 167
		global $tiki_p_autoapprove_submission;
		if ($tiki_p_autoapprove_submission == 'y') {
			$artlib->approve_submission($subid);
			$content .= "Article: $title has been submitted<br />";
		} else {
			$content .= "Article: $title has been created<br />";
		}
168

169 170
      } else {
         if ($acc['type'] == 'wiki') {
171
           $p_page = trim($aux['Subject']);
172 173 174 175 176 177 178 179
           $parts = explode(':', $p_page);
           if (!isset($parts[1])) {
             $parts[1] = $parts[0];
             $parts[0] = 'GET';
           }
           $method = $parts[0];
           $page = $parts[1];
         } else {
180
           $page = trim($aux['Subject']);
181 182 183
         }

      if ($acc['type'] == 'wiki-get' || ($acc['type'] == 'wiki' && $method == "GET")) {
184 185
        // A wiki-get account sends a copy of the page to the sender
        // and also sends the source of the page
186
        $mail = new TikiMail();
187
        $mail->setFrom($acc["account"]);
188
        $c = $prefs['default_mail_charset'];
189 190 191 192
        $mail->setHeadCharset($c);
        $mail->setHtmlCharset($c); 
        $mail->setTextCharset($c);
        $mail->setSMTPParams($acc["smtp"], $acc["smtpPort"], '', $acc["useAuth"], $acc["username"], $acc["pass"]);
193 194
  
        if ($tikilib->page_exists($page)) {
195
          $mail->setSubject($page);
196 197 198
          $info = $tikilib->get_page_info($page);
          $data = $tikilib->parse_data($info["data"]);
          $mail->addAttachment($info['data'], 'source.txt', 'plain/txt');
199
          $mail->setHTML($data, strip_tags($data));
200
        } else {
201
          $l = $prefs['language'];
202 203
          $mail_data = $smarty->fetchLang($l, "mail/mailin_reply_subject.tpl");
          $mail->setSubject($mail_data.$page);
204
        }
205
        $res = $mail->send(array($email_from), 'mail');
206
        $content .= "Response sent<br />";
207 208
      }
  
209
      elseif ($acc['type'] == 'wiki-put' || ($acc['type'] == 'wiki' && $method == "PUT")) {
210
        // This is used to UPDATE wiki pages
211
    
212
        $body = mailin_get_body($output);
213
  
214
        if (isset($acc['discard_after']) && $body) {
215 216
           $body = preg_replace("/".$acc['discard_after'].".*$/s", "", $body);
        }
217
        if (!empty($body)) {
218
          if (!$tikilib->page_exists($page)) {
219
            $content .= "Page: $page has been created<br />";
220
  
mose's avatar
mose committed
221
            $tikilib->create_page($page, 0, $body, $tikilib->now, "Created from " . $acc["account"], $aux["sender"]["user"], '0.0.0.0', '');
222 223 224 225 226 227 228
          } else {
            $tikilib->update_page($page, $body, "Created from " . $acc["account"], $aux["sender"]["user"],
              '0.0.0.0', '');
  
            $content .= "Page: $page has been updated";
          }
        }
229
      mailin_check_attachments($output, $content, $page, $aux["sender"]["user"]);
230 231
      }
  
232
      elseif ($acc['type'] == 'wiki-append' || $acc['type'] == 'wiki-prepend' || ($acc['type'] == 'wiki' && $method == "APPEND") || ($acc['type'] == 'wiki' && $method == "PREPEND")) {
233 234
        // This is used to UPDATE wiki pages
  
235
        $body = mailin_get_body($output);
236
        if ($body && isset($acc['discard_after'])) {
237 238
              $body = preg_replace("/".$acc['discard_after'].".*$/s", "", $body);
          }
239 240 241
  
        if (isset($body)) {
          if (!$tikilib->page_exists($page)) {
242
            $content .= "Page: $page has been created<br />";
243
  
mose's avatar
mose committed
244
            $tikilib->create_page($page, 0, $body, $tikilib->now, "Created from " . $acc["account"], $aux["sender"]["user"], '0.0.0.0', '');
245 246
          } else {
            $info = $tikilib->get_page_info($page);
247 248 249 250 251 252
            if ($acc['type'] == 'wiki-append' || $acc['type'] == 'wiki' && $method == "APPEND")
                  $body = $info['data'] . $body;
            else
                  $body = $body . $info['data'];
            $tikilib->update_page($page, $body,
              "Updated from " . $acc["account"], $aux["sender"]["user"], '0.0.0.0', '');
253 254 255
            $content .= "Page: $page has been updated";
          }
        }
256
      mailin_check_attachments($output, $content, $page, $aux["sender"]["user"]);
257
      }
258 259

      else {
260
          $mail = new TikiMail();
261 262
  
          $mail->setFrom($acc["account"]);
263
          $c = $prefs['default_mail_charset'];
264 265
          $mail->setHeadCharset($c);
          $mail->setTextCharset($c);
266
          $l = $prefs['language'];
267 268
          $mail_data = $smarty->fetchLang($l, "mail/mailin_help_subject.tpl");
          $mail->setSubject($mail_data);
269
          $mail->setSMTPParams($acc["smtp"], $acc["smtpPort"], '', $acc["useAuth"], $acc["username"], $acc["pass"]);
270 271 272
          $smarty->assign('subject', $output['header']['subject']);
          $mail_data = $smarty->fetchLang($l, "mail/mailin_help.tpl");
          $mail->setText($mail_data);
273
          $res = $mail->send(array($email_from), 'mail');
274 275 276 277
        }
      }
    }//end if($cantUseMailIn)
    // Remove the email from the pop3 server
278
    $pop3->deleteMsg($i);
279 280
  }//end for ($i = 1; $i <= $mailsum; $i++)

281
  $pop3->disconnect();
282
//echo $content;
283 284
}//end foreach ($accs['data'] as $acc) {

285
?>