*** empty log message ***
[Packages/TYPO3.CMS.git] / misc / 3.0.0 / oldscripts / tipafriendLib.inc
1 <?php
2 /***************************************************************
3 *  Copyright notice
4 *  
5 *  (c) 1999-2003 Kasper Skårhøj (kasper@typo3.com)
6 *  All rights reserved
7 *
8 *  This script is part of the Typo3 project. The Typo3 project is 
9 *  free software; you can redistribute it and/or modify
10 *  it under the terms of the GNU General Public License as published by
11 *  the Free Software Foundation; either version 2 of the License, or
12 *  (at your option) any later version.
13
14 *  The GNU General Public License can be found at
15 *  http://www.gnu.org/copyleft/gpl.html.
16 *  A copy is found in the textfile GPL.txt and important notices to the license 
17 *  from the author is found in LICENSE.txt distributed with these scripts.
18 *
19
20 *  This script is distributed in the hope that it will be useful,
21 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 *  GNU General Public License for more details.
24 *
25 *  This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28  * tipafriendLib.inc
29  * 
30  * Creates a Tip-a-Friend form.
31  * 
32  * TypoScript config:
33  * - See static_template "plugin.tipafriend"
34  * - See TS_ref.pdf
35  *
36  * @author      Kasper Skårhøj <kasper@typo3.com>
37  */
38
39 class user_tipafriend {
40         var $cObj;              // The backReference to the mother cObj object set at call time
41
42         /**
43          * Main news function.
44          * Call it from a USER cObject with 'userFunc = user_guest->main_tipafriend'
45          */
46         function main_tipafriend($content,$conf)        {
47                 $this->conf = $conf;
48                 $this->config["code"] = $this->cObj->stdWrap($this->conf["code"],$this->conf["code."]);
49                 
50                         // template is read.
51                 $this->templateCode = $this->cObj->fileResource($this->conf["templateFile"]);
52
53                         // globally substituted markers, fonts and colors.      
54                 $splitMark = md5(microtime());
55                 $globalMarkerArray=array();
56                 list($globalMarkerArray["###GW1B###"],$globalMarkerArray["###GW1E###"]) = explode($splitMark,$this->cObj->stdWrap($splitMark,$conf["wrap1."]));
57                 list($globalMarkerArray["###GW2B###"],$globalMarkerArray["###GW2E###"]) = explode($splitMark,$this->cObj->stdWrap($splitMark,$conf["wrap2."]));
58                 $globalMarkerArray["###GC1###"] = $this->cObj->stdWrap($conf["color1"],$conf["color1."]);
59                 $globalMarkerArray["###GC2###"] = $this->cObj->stdWrap($conf["color2"],$conf["color2."]);
60                 $globalMarkerArray["###GC3###"] = $this->cObj->stdWrap($conf["color3"],$conf["color3."]);
61
62                         // Substitute Global Marker Array
63                 $this->templateCode= $this->cObj->substituteMarkerArray($this->templateCode, $globalMarkerArray);
64
65                         // TYpoLink
66                 $this->typolink_conf = $this->conf["typolink."];
67                 $this->typolink_conf["additionalParams"] = $this->cObj->stdWrap($this->typolink_conf["additionalParams"],$this->typolink_conf["additionalParams."]);
68                 unset($this->typolink_conf["additionalParams."]);
69
70                 $codes=t3lib_div::trimExplode(",", $this->config["code"]?$this->config["code"]:$this->conf["defaultCode"],1);
71                 if (!count($codes))     $codes=array("");
72                 while(list(,$theCode)=each($codes))     {
73                         $theCode = (string)strtoupper(trim($theCode));
74                         $this->theCode = $theCode;
75                         switch($theCode)        {
76                                 case "TIPFORM":
77                                         $content=$this->tipform();
78                                 break;
79                                 case "TIPLINK":
80                                         $content=$this->tiplink();
81                                 break;
82                                 default:
83                                         $langKey = strtoupper($GLOBALS["TSFE"]->config["config"]["language"]);
84                                         $helpTemplate = $this->cObj->fileResource("media/scripts/tipafriend_help.tmpl");
85
86                                                 // Get language version
87                                         $helpTemplate_lang="";
88                                         if ($langKey)   {$helpTemplate_lang = $this->cObj->getSubpart($helpTemplate,"###TEMPLATE_".$langKey."###");}
89                                         $helpTemplate = $helpTemplate_lang ? $helpTemplate_lang : $this->cObj->getSubpart($helpTemplate,"###TEMPLATE_DEFAULT###");
90
91                                                 // Markers and substitution:
92                                         $markerArray["###CODE###"] = $this->theCode;
93                                         $content.=$this->cObj->substituteMarkerArray($helpTemplate,$markerArray);
94                                 break;
95                         }
96                 }
97                 return $content;
98         }
99         function tipform()      {
100                 $GLOBALS["TSFE"]->set_no_cache();
101
102                 $tipData = t3lib_div::slashArray(t3lib_div::GPvar("TIPFORM"),"strip");
103                 $tipData["recipient"] = $this->getRecipients($tipData["recipient"]);
104                 list($tipData["email"]) = explode(",",$this->getRecipients($tipData["email"]));
105                 $url = t3lib_div::GPvar("tipUrl");
106
107                         // Preparing markers
108                 $wrappedSubpartArray=array();
109                 $subpartArray=array();
110                 
111                 $markerArray=array();
112                 $markerArray["###FORM_URL###"]=t3lib_div::getIndpEnv("REQUEST_URI");
113                 $markerArray["###URL###"]=$url;
114                 $markerArray["###URL_ENCODED###"]=rawurlencode($url);
115                 $markerArray["###URL_SPECIALCHARS###"]=htmlspecialchars($url);
116                 
117                 $wrappedSubpartArray["###LINK###"]=array('<a href="'.$url.'">','</a>');
118
119                         // validation
120                 $error=0;
121                 $sent=0;
122                 if (t3lib_div::GPvar("sendTip"))        {
123                         if ($this->validate($tipData))  {
124                                 $this->sendTip($tipData,$url);
125                                 $sent=1;
126                         } else {
127                                 $error=1;
128                         }
129                 }
130
131                         // Display form
132                 if ($sent)      {
133                         $subpart = $this->cObj->getSubpart($this->templateCode,"###TEMPLATE_TIPFORM_SENT###");
134
135                         $markerArray["###RECIPIENT###"]=htmlspecialchars($tipData["recipient"]);
136
137                         $content= $this->cObj->substituteMarkerArrayCached($subpart,$markerArray,$subpartArray,$wrappedSubpartArray);
138                 } else {
139                         $subpart = $this->cObj->getSubpart($this->templateCode,"###TEMPLATE_TIPFORM###");
140         
141                         $markerArray["###MESSAGE###"]=htmlspecialchars($tipData["message"]);
142                         $markerArray["###RECIPIENT###"]=htmlspecialchars($tipData["recipient"]);
143                         $markerArray["###YOUR_EMAIL###"]=htmlspecialchars($tipData["email"]);
144                         $markerArray["###YOUR_NAME###"]=htmlspecialchars($tipData["name"]);
145                         $markerArray["###HTML_MESSAGE###"]=$tipData["html_message"] ? "checked" : "";
146         
147                         if (!$error)    {
148                                 $subpartArray["###ERROR_MSG###"]="";
149                         }
150                 
151 //                      debug($markerArray);
152         
153                                 // Substitute
154                         $content= $this->cObj->substituteMarkerArrayCached($subpart,$markerArray,$subpartArray,$wrappedSubpartArray);
155                 }
156                 return $content;
157         }
158         function validate($tipData)     {
159                 if (    
160                         trim($tipData["name"]) &&
161                         $tipData["email"] &&
162                         $tipData["recipient"]
163                         ) {
164                                 return 1;
165                         }
166         }
167         function getRecipients($emails) {
168                 $emailArr = split("[, ;]",$emails);
169                 reset($emailArr);
170                 $listArr=array();
171                 while(list(,$email)=each($emailArr))    {
172                         $email = trim($email);
173                         if ($email && t3lib_div::validEmail($email))    {
174                                 $listArr[] = $email;
175                         }
176                 }
177                 return implode(",",$listArr);
178         }
179         function sendTip($tipData,$url) {
180                         // Get template
181                 $subpart = $this->cObj->getSubpart($this->templateCode,"###TEMPLATE_EMAIL###");
182
183                         // Set markers
184                 $markerArray["###MESSAGE###"]=htmlspecialchars($tipData["message"]);
185                 $markerArray["###RECIPIENT###"]=htmlspecialchars($tipData["recipient"]);
186                 $markerArray["###YOUR_EMAIL###"]=htmlspecialchars($tipData["email"]);
187                 $markerArray["###YOUR_NAME###"]=htmlspecialchars($tipData["name"]);
188                 $markerArray["###URL###"]=$url;
189
190                         // Substitute in template
191                 $content= $this->cObj->substituteMarkerArrayCached($subpart,$markerArray,$subpartArray,$wrappedSubpartArray);
192                 
193                         // Set subject, conten and headers
194                 $headers=array();
195                 $headers[]="FROM: ".$tipData["name"]." <".$tipData["email"].">";
196                 list($subject,$plain_message) = explode(chr(10),trim($content),2);
197         
198
199                         // HTML
200                 $cls=t3lib_div::makeInstanceClassName("t3lib_htmlmail");
201
202                 if (class_exists($cls) && $this->conf["htmlmail"] && $tipData["html_message"])  {       // If htmlmail lib is included, then generate a nice HTML-email
203                         $Typo3_htmlmail = t3lib_div::makeInstance("t3lib_htmlmail");
204                         $Typo3_htmlmail->start();
205                         $Typo3_htmlmail->useBase64();
206
207                         $Typo3_htmlmail->subject = $subject;
208                         $Typo3_htmlmail->from_email = $tipData["email"];
209                         $Typo3_htmlmail->from_name = $tipData["name"];
210                         $Typo3_htmlmail->replyto_email = $tipData["email"];
211                         $Typo3_htmlmail->replyto_name = $tipData["name"];
212                         $Typo3_htmlmail->organisation = "";
213                         $Typo3_htmlmail->priority = 3;
214
215         //              debug($url);
216                 
217                                 // this will fail if the url is password protected!
218                         $Typo3_htmlmail->addHTML($url);
219                         $Typo3_htmlmail->addPlain($plain_message);
220         
221                         $Typo3_htmlmail->setHeaders();
222                         $Typo3_htmlmail->setContent();
223                         $Typo3_htmlmail->setRecipient($tipData["recipient"]);
224
225 //                      debug($Typo3_htmlmail->theParts);
226                         $Typo3_htmlmail->sendtheMail();
227                 } else { // Plain mail:
228                                 // Sending mail:                
229                         mail ($tipData["recipient"], $subject, $plain_message, implode($headers,chr(10)));
230                 }
231         }
232         function tiplink()      {
233                 $url="http://".t3lib_div::getIndpEnv("HTTP_HOST").t3lib_div::getIndpEnv("REQUEST_URI");
234
235                 $subpart = $this->cObj->getSubpart($this->templateCode,"###TEMPLATE_TIPLINK###");
236                 
237                 $wrappedSubpartArray=array();
238                 $tConf = $this->typolink_conf;
239                 $tConf["additionalParams"].= "&tipUrl=".rawurlencode($url);
240 //              debug($tConf);
241                 $wrappedSubpartArray["###LINK###"]= $this->cObj->typolinkWrap($tConf);
242
243                 $markerArray=array();
244                 $markerArray["###URL###"]=$url;
245                 $markerArray["###URL_ENCODED###"]=rawurlencode($url);
246                 $markerArray["###URL_SPECIALCHARS###"]=htmlspecialchars($url);
247
248                         // Substitute
249                 $content= $this->cObj->substituteMarkerArrayCached($subpart,$markerArray,array(),$wrappedSubpartArray);
250                 return $content;
251         }
252 }
253
254
255
256 if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["media/scripts/tipafriendLib.inc"]) {
257         include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["media/scripts/tipafriendLib.inc"]);
258 }
259
260
261 ?>