Documentation TYPO3 par Ameos |
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 1999-2004 Kasper Skaarhoj (kasperYYYY@typo3.com) 00006 * All rights reserved 00007 * 00008 * This script is part of the TYPO3 project. The TYPO3 project is 00009 * free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * The GNU General Public License can be found at 00015 * http://www.gnu.org/copyleft/gpl.html. 00016 * A copy is found in the textfile GPL.txt and important notices to the license 00017 * from the author is found in LICENSE.txt distributed with these scripts. 00018 * 00019 * 00020 * This script is distributed in the hope that it will be useful, 00021 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00023 * GNU General Public License for more details. 00024 * 00025 * This copyright notice MUST APPEAR in all copies of the script! 00026 ***************************************************************/ 00086 class t3lib_dmailer extends t3lib_htmlmail { 00087 var $sendPerCycle =50; 00088 var $logArray =array(); 00089 var $massend_id_lists = array(); 00090 var $flag_html = 0; 00091 var $flag_plain = 0; 00092 00097 function dmailer_prepare($row) { 00098 $sys_dmail_uid = $row['uid']; 00099 if($row["encoding"] == "base64") { 00100 $this->useBase64(); 00101 } 00102 $this->theParts = unserialize($row['mailContent']); 00103 $this->messageid = $this->theParts['messageid']; 00104 $this->subject = $row['subject']; 00105 $this->from_email = $row['from_email']; 00106 $this->from_name = ($row['from_name']) ? $row['from_name'] : ''; 00107 $this->replyto_email = ($row['replyto_email']) ? $row['replyto_email'] : ''; 00108 $this->replyto_name = ($row['replyto_name']) ? $row['replyto_name'] : ''; 00109 $this->organisation = ($row['organisation']) ? $row['organisation'] : ''; 00110 $this->priority = t3lib_div::intInRange($row['priority'],1,5); 00111 $this->mailer = 'TYPO3 Direct Mail module'; 00112 00113 $this->dmailer['sectionBoundary'] = '<!--DMAILER_SECTION_BOUNDARY'; 00114 $this->dmailer['html_content'] = base64_decode($this->theParts['html']['content']); 00115 $this->dmailer['plain_content'] = base64_decode($this->theParts['plain']['content']); 00116 $this->dmailer['messageID'] = $this->messageid; 00117 $this->dmailer['sys_dmail_uid'] = $sys_dmail_uid; 00118 $this->dmailer['sys_dmail_rec'] = $row; 00119 00120 $this->dmailer['boundaryParts_html'] = explode($this->dmailer['sectionBoundary'], '_END-->'.$this->dmailer['html_content']); 00121 while(list($bKey,$bContent)=each($this->dmailer['boundaryParts_html'])) { 00122 $this->dmailer['boundaryParts_html'][$bKey] = explode('-->',$bContent,2); 00123 // Now, analyzing which media files are used in this part of the mail: 00124 $mediaParts = explode('cid:part',$this->dmailer['boundaryParts_html'][$bKey][1]); 00125 reset($mediaParts); 00126 next($mediaParts); 00127 while(list(,$part)=each($mediaParts)) { 00128 $this->dmailer['boundaryParts_html'][$bKey]['mediaList'].=','.strtok($part,'.'); 00129 } 00130 } 00131 $this->dmailer['boundaryParts_plain'] = explode($this->dmailer['sectionBoundary'], '_END-->'.$this->dmailer['plain_content']); 00132 while(list($bKey,$bContent)=each($this->dmailer['boundaryParts_plain'])) { 00133 $this->dmailer['boundaryParts_plain'][$bKey] = explode('-->',$bContent,2); 00134 } 00135 00136 $this->flag_html = $this->theParts['html']['content'] ? 1 : 0; 00137 $this->flag_plain = $this->theParts['plain']['content'] ? 1 : 0; 00138 } 00139 00147 function dmailer_sendAdvanced($recipRow,$tableNameChar) { 00148 $returnCode=0; 00149 if ($recipRow['email']) { 00150 $midRidId = 'MID'.$this->dmailer['sys_dmail_uid'].'_'.$tableNameChar.$recipRow['uid']; 00151 $uniqMsgId = md5(microtime()).'_'.$midRidId; 00152 $rowFieldsArray = explode(',', 'uid,name,title,email,phone,www,address,company,city,zip,country,fax,firstname'); 00153 $uppercaseFieldsArray = explode(',', 'name,firstname'); 00154 $authCode = t3lib_div::stdAuthCode($recipRow['uid']); 00155 $this->mediaList=''; 00156 if ($this->flag_html && $recipRow['module_sys_dmail_html']) { 00157 $tempContent_HTML = $this->dmailer_getBoundaryParts($this->dmailer['boundaryParts_html'],$recipRow['module_sys_dmail_category']); 00158 reset($rowFieldsArray); 00159 while(list(,$substField)=each($rowFieldsArray)) { 00160 $tempContent_HTML = str_replace('###USER_'.$substField.'###', $recipRow[$substField], $tempContent_HTML); 00161 } 00162 reset($uppercaseFieldsArray); 00163 while(list(,$substField)=each($uppercaseFieldsArray)) { 00164 $tempContent_HTML = str_replace('###USER_'.strtoupper($substField).'###', strtoupper($recipRow[$substField]), $tempContent_HTML); 00165 } 00166 $tempContent_HTML = str_replace('###SYS_TABLE_NAME###', $tableNameChar, $tempContent_HTML); // Put in the tablename of the userinformation 00167 $tempContent_HTML = str_replace('###SYS_MAIL_ID###', $this->dmailer['sys_dmail_uid'], $tempContent_HTML); // Put in the uid of the mail-record 00168 $tempContent_HTML = str_replace('###SYS_AUTHCODE###', $authCode, $tempContent_HTML); 00169 $tempContent_HTML = str_replace($this->dmailer['messageID'], $uniqMsgId, $tempContent_HTML); // Put in the unique message id in HTML-code 00170 $this->theParts['html']['content'] = $this->encodeMsg($tempContent_HTML); 00171 $returnCode|=1; 00172 } else $this->theParts['html']['content'] = ''; 00173 00174 // Plain 00175 if ($this->flag_plain) { 00176 $tempContent_Plain = $this->dmailer_getBoundaryParts($this->dmailer['boundaryParts_plain'],$recipRow['module_sys_dmail_category']); 00177 reset($rowFieldsArray); 00178 while(list(,$substField)=each($rowFieldsArray)) { 00179 $tempContent_Plain = str_replace('###USER_'.$substField.'###', $recipRow[$substField], $tempContent_Plain); 00180 } 00181 reset($uppercaseFieldsArray); 00182 while(list(,$substField)=each($uppercaseFieldsArray)) { 00183 $tempContent_Plain = str_replace('###USER_'.strtoupper($substField).'###', strtoupper($recipRow[$substField]), $tempContent_Plain); 00184 } 00185 $tempContent_Plain = str_replace('###SYS_TABLE_NAME###', $tableNameChar, $tempContent_Plain); // Put in the tablename of the userinformation 00186 $tempContent_Plain = str_replace('###SYS_MAIL_ID###', $this->dmailer['sys_dmail_uid'], $tempContent_Plain); // Put in the uid of the mail-record 00187 $tempContent_Plain = str_replace('###SYS_AUTHCODE###', $authCode, $tempContent_Plain); 00188 00189 if (trim($this->dmailer['sys_dmail_rec']['long_link_rdct_url'])) { 00190 $tempContent_Plain = t3lib_div::substUrlsInPlainText($tempContent_Plain,$this->dmailer['sys_dmail_rec']['long_link_mode']?'all':'76',trim($this->dmailer['sys_dmail_rec']['long_link_rdct_url'])); 00191 } 00192 00193 $this->theParts['plain']['content'] = $this->encodeMsg($tempContent_Plain); 00194 $returnCode|=2; 00195 } else $this->theParts['plain']['content'] = ''; 00196 00197 // Set content 00198 $this->Xid = $midRidId.'-'.md5($midRidId); 00199 $this->returnPath = str_replace('###XID###',$midRidId,$this->dmailer['sys_dmail_rec']['return_path']); 00200 00201 $this->part=0; 00202 $this->setHeaders(); 00203 $this->setContent(); 00204 $this->setRecipient($recipRow['email']); 00205 00206 $this->message = str_replace($this->dmailer['messageID'], $uniqMsgId, $this->message); // Put in the unique message id in whole message body 00207 $this->sendtheMail(); 00208 } 00209 return $returnCode; 00210 } 00211 00218 function dmailer_sendSimple($addressList) { 00219 $this->useBase64(); 00220 if ($this->theParts['html']['content']) { 00221 $this->theParts['html']['content'] = $this->encodeMsg($this->dmailer_getBoundaryParts($this->dmailer['boundaryParts_html'],-1)); 00222 } else $this->theParts['html']['content'] = ''; 00223 if ($this->theParts['plain']['content']) { 00224 $this->theParts['plain']['content'] = $this->encodeMsg($this->dmailer_getBoundaryParts($this->dmailer['boundaryParts_plain'],-1)); 00225 } else $this->theParts['plain']['content'] = ''; 00226 00227 $this->setHeaders(); 00228 $this->setContent(); 00229 $this->setRecipient($addressList); 00230 $this->sendtheMail(); 00231 return true; 00232 } 00233 00241 function dmailer_getBoundaryParts($cArray,$userCategories) { 00242 $userCategories = intval($userCategories); 00243 reset($cArray); 00244 $returnVal=''; 00245 while(list(,$cP)=each($cArray)) { 00246 $key=substr($cP[0],1); 00247 if ($key=='END' || !$key || $userCategories<0 || (intval($key) & $userCategories)>0) { 00248 $returnVal.=$cP[1]; 00249 $this->mediaList.=$cP['mediaList']; 00250 } 00251 } 00252 return $returnVal; 00253 } 00254 00263 function dmailer_masssend($query_info,$table,$mid) { 00264 $enableFields['tt_address']='NOT tt_address.deleted AND NOT tt_address.hidden'; 00265 $enableFields['fe_users']='NOT fe_users.deleted AND NOT fe_users.disable'; 00266 $tKey = substr($table,0,1); 00267 $begin=intval($this->dmailer_howManySendMails($mid,$tKey)); 00268 if ($query_info[$table]) { 00269 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($table.'.*', $table, $enableFields[$table].' AND ('.$query_info[$table].')', '', 'tstamp DESC', intval($begin).','.$this->sendPerCycle); // This way, we select newest edited records first. So if any record is added or changed in between, it'll end on top and do no harm 00270 if ($GLOBALS['TYPO3_DB']->sql_error()) { 00271 die ($GLOBALS['TYPO3_DB']->sql_error()); 00272 } 00273 $numRows = $GLOBALS['TYPO3_DB']->sql_num_rows($res); 00274 $cc=0; 00275 while($recipRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { 00276 if (!$this->dmailer_isSend($mid,$recipRow['uid'],$tKey)) { 00277 $pt = t3lib_div::milliseconds(); 00278 if ($recipRow['telephone']) $recipRow['phone'] = $recipRow['telephone']; // Compensation for the fact that fe_users has the field, 'telephone' instead of 'phone' 00279 $recipRow['firstname']=strtok(trim($recipRow['name']),' '); 00280 00281 $rC = $this->dmailer_sendAdvanced($recipRow,$tKey); 00282 $this->dmailer_addToMailLog($mid,$tKey.'_'.$recipRow['uid'],strlen($this->message),t3lib_div::milliseconds()-$pt,$rC); 00283 } 00284 $cc++; 00285 } 00286 $this->logArray[]='Sending '.$cc.' mails to table '.$table; 00287 if ($numRows < $this->sendPerCycle) return true; 00288 } 00289 return false; 00290 } 00291 00299 function dmailer_masssend_list($query_info,$mid) { 00300 $enableFields['tt_address']='NOT tt_address.deleted AND NOT tt_address.hidden'; 00301 $enableFields['fe_users']='NOT fe_users.deleted AND NOT fe_users.disable'; 00302 00303 $c=0; 00304 $returnVal=true; 00305 if (is_array($query_info['id_lists'])) { 00306 reset($query_info['id_lists']); 00307 while(list($table,$listArr)=each($query_info['id_lists'])) { 00308 if (is_array($listArr)) { 00309 $ct=0; 00310 // FInd tKey 00311 if ($table=='tt_address' || $table=='fe_users') { 00312 $tKey = substr($table,0,1); 00313 } elseif ($table=='PLAINLIST') { 00314 $tKey='P'; 00315 } else {$tKey='u';} 00316 00317 // Send mails 00318 $sendIds=$this->dmailer_getSentMails($mid,$tKey); 00319 if ($table=='PLAINLIST') { 00320 $sendIdsArr=explode(',',$sendIds); 00321 reset($listArr); 00322 while(list($kval,$recipRow)=each($listArr)) { 00323 $kval++; 00324 if (!in_array($kval,$sendIdsArr)) { 00325 if ($c>=$this->sendPerCycle) {$returnVal = false; break;} // We are NOT finished! 00326 $recipRow['uid']=$kval; 00327 $this->shipOfMail($mid,$recipRow,$tKey); 00328 $ct++; 00329 $c++; 00330 } 00331 } 00332 } else { 00333 $idList = implode(',',$listArr); 00334 if ($idList) { 00335 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($table.'.*', $table, 'uid IN ('.$idList.') AND uid NOT IN ('.($sendIds?$sendIds:0).') AND '.($enableFields[$table]?$enableFields[$table]:'1=1'), '', '', $this->sendPerCycle+1); 00336 if ($GLOBALS['TYPO3_DB']->sql_error()) {die ($GLOBALS['TYPO3_DB']->sql_error());} 00337 while($recipRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { 00338 if ($c>=$this->sendPerCycle) {$returnVal = false; break;} // We are NOT finished! 00339 $this->shipOfMail($mid,$recipRow,$tKey); 00340 $ct++; 00341 $c++; 00342 } 00343 } 00344 } 00345 $this->logArray[]='Sending '.$ct.' mails to table '.$table; 00346 } 00347 } 00348 } 00349 return $returnVal; 00350 } 00351 00360 function shipOfMail($mid,$recipRow,$tKey) { 00361 if (!$this->dmailer_isSend($mid,$recipRow['uid'],$tKey)) { 00362 $pt = t3lib_div::milliseconds(); 00363 $recipRow=$this->convertFields($recipRow); 00364 00365 // debug('->'.$recipRow['uid'],1); 00366 $rC=$this->dmailer_sendAdvanced($recipRow,$tKey); 00367 $this->dmailer_addToMailLog($mid,$tKey.'_'.$recipRow['uid'],strlen($this->message),t3lib_div::milliseconds()-$pt,$rC); 00368 } 00369 } 00370 00377 function convertFields($recipRow) { 00378 if ($recipRow['telephone']) $recipRow['phone'] = $recipRow['telephone']; // Compensation for the fact that fe_users has the field, 'telephone' instead of 'phone' 00379 $recipRow['firstname']=trim(strtok(trim($recipRow['name']),' ')); 00380 if (strlen($recipRow['firstname'])<2 || ereg('[^[:alnum:]]$',$recipRow['firstname'])) $recipRow['firstname']=$recipRow['name']; // Firstname must be more that 1 character 00381 if (!trim($recipRow['firstname'])) $recipRow['firstname']=$recipRow['email']; 00382 return $recipRow; 00383 } 00384 00392 function dmailer_setBeginEnd($mid,$key) { 00393 $GLOBALS['TYPO3_DB']->exec_UPDATEquery('sys_dmail', 'uid='.intval($mid), array('scheduled_'.$key => time())); 00394 00395 switch($key) { 00396 case 'begin': 00397 $subject = 'DMAILER mid:'.$mid.' JOB BEGIN'; 00398 $message = ': '.date('d-m-y h:i:s'); 00399 break; 00400 case 'end': 00401 $subject = 'DMAILER mid:'.$mid.' JOB END'; 00402 $message = ': '.date('d-m-y h:i:s'); 00403 break; 00404 } 00405 $this->logArray[] = $subject.': '.$message; 00406 mail($this->from_email, $subject, $message); 00407 } 00408 00416 function dmailer_howManySendMails($mid,$rtbl='') { 00417 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*)', 'sys_dmail_maillog', 'mid='.intval($mid).' AND response_type=0'.($rtbl ? ' AND rtbl="'.$GLOBALS['TYPO3_DB']->quoteStr($rtbl, 'sys_dmail_maillog').'"' : '')); 00418 $row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res); 00419 return $row[0]; 00420 } 00421 00430 function dmailer_isSend($mid,$rid,$rtbl) { 00431 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', 'sys_dmail_maillog', 'rid='.intval($rid).' AND rtbl="'.$GLOBALS['TYPO3_DB']->quoteStr($rtbl, 'sys_dmail_maillog').'" AND mid='.intval($mid).' AND response_type=0'); 00432 return $GLOBALS['TYPO3_DB']->sql_num_rows($res); 00433 } 00434 00442 function dmailer_getSentMails($mid,$rtbl) { 00443 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('rid', 'sys_dmail_maillog', 'mid='.intval($mid).' AND rtbl="'.$GLOBALS['TYPO3_DB']->quoteStr($rtbl, 'sys_dmail_maillog').'" AND response_type=0'); 00444 $list = array(); 00445 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { 00446 $list[] = $row['rid']; 00447 } 00448 return implode(',', $list); 00449 } 00450 00461 function dmailer_addToMailLog($mid,$rid,$size,$parsetime,$html) { 00462 $temp_recip = explode('_',$rid); 00463 00464 $insertFields = array( 00465 'mid' => intval($mid), 00466 'rtbl' => $temp_recip[0], 00467 'rid' => intval($temp_recip[1]), 00468 'tstamp' => time(), 00469 'url' => '', 00470 'size' => $size, 00471 'parsetime' => $parsetime, 00472 'html_sent' => intval($html) 00473 ); 00474 00475 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_dmail_maillog', $insertFields); 00476 } 00477 00483 function runcron() { 00484 $pt = t3lib_div::milliseconds(); 00485 00486 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'sys_dmail', 'scheduled!=0 AND scheduled<'.time().' AND scheduled_end=0', '', 'scheduled'); 00487 if ($GLOBALS['TYPO3_DB']->sql_error()) { 00488 die ($GLOBALS['TYPO3_DB']->sql_error()); 00489 } 00490 $this->logArray[]='Invoked at '.date('h:i:s d-m-Y'); 00491 00492 if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { 00493 $this->logArray[]='sys_dmail record '.$row['uid'].", '".$row['subject']."' processed..."; 00494 $this->dmailer_prepare($row); 00495 $query_info=unserialize($row['query_info']); 00496 if (!$row['scheduled_begin']) {$this->dmailer_setBeginEnd($row['uid'],'begin');} 00497 /* 00498 $finished = $this->dmailer_masssend($query_info,'tt_address',$row['uid']); 00499 if ($finished) { 00500 $finished = $this->dmailer_masssend($query_info,'fe_users',$row['uid']); 00501 }*/ 00502 $finished = $this->dmailer_masssend_list($query_info,$row['uid']); 00503 00504 if ($finished) {$this->dmailer_setBeginEnd($row['uid'],'end');} 00505 } else { 00506 $this->logArray[]='Nothing to do.'; 00507 } 00508 00509 $parsetime=t3lib_div::milliseconds()-$pt; 00510 $this->logArray[]='Ending, parsetime: '.$parsetime.' ms';; 00511 } 00512 } 00513 00514 00515 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_dmailer.php']) { 00516 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_dmailer.php']); 00517 } 00518 ?>