Dotclear

source: plugins/sendToFriend/_public.php @ 2792

Revision 2792, 6.1 KB checked in by brol, 13 years ago (diff)

1.0.8

Line 
1<?php
2/* BEGIN LICENSE BLOCK
3This file is part of SendToFriend, a plugin for Dotclear.
4
5Julien Appert
6brol contact@brol.info
7
8Licensed under the GPL version 2.0 license.
9A copy of this license is available in LICENSE file or at
10http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
11END LICENSE BLOCK */
12
13if (!defined('DC_RC_PATH')) {return;}
14
15l10n::set(dirname(__FILE__).'/locales/'.$_lang.'/public');
16
17$core->url->register('envoyer','envoyer','^envoyer/([0-9]*)',array('tplEnvoyer','load'));
18class sdf{
19     public $id = null;
20     public $title = '';
21     public $url = '';
22     public $abstract = '';
23     public $senderName = '';
24     public $senderEmail = '';
25     public $receiverName = '';
26     public $receiverEmail = '';   
27     public $error = array();
28     public $message = array();
29}
30class tplEnvoyer extends dcUrlHandlers
31{
32     public static function load($args) {
33          global $core, $_ctx;
34
35          $settings = new dcSettings($core,$core->blog->id);
36          $settings->addNamespace('sendtofriend');
37         
38          if (empty($args) || $args == '') self::p404();
39         
40          $GLOBALS['envoyer'] = array();
41          $aParams = array();
42          $aParams['post_id'] = (int)$args;
43          $rs = $core->blog->getPosts($aParams);
44          if(!$rs->isEmpty()){
45               while ($rs->fetch()) {
46                         $_ctx->sdf = new sdf();                     
47                         $_ctx->sdf->id = (int)$args;
48                         $_ctx->sdf->title = $rs->post_title;
49                         $_ctx->sdf->url = $core->blog->url.'post/'.$rs->post_url;
50                         $_ctx->sdf->abstract = tplEnvoyer::getAbstract($rs->post_excerpt_xhtml,$rs->post_content_xhtml);
51                         if(isset($_POST['submit'])){
52                              try{                     
53                                        if(strlen(trim($_POST['yourName']))==0){
54                                             $_ctx->sdf->error[] =  __('Your name is required');
55                                        }
56                                        if(strlen(trim($_POST['yourEmail']))==0){
57                                             $_ctx->sdf->error[] =  __('Your email is required');
58                                        }   
59                                        if(strlen(trim($_POST['yourFriendName']))==0){
60                                             $_ctx->sdf->error[] =  __('Your friend name is required');
61                                        }
62                                        if(strlen(trim($_POST['yourFriendEmail']))==0){
63                                             $_ctx->sdf->error[] =  __('Your friend email is required');
64                                        }
65                                        if(count($_ctx->sdf->error)>0){
66                                             throw new Exception();
67                                        }
68                                        else{
69                                                  $_ctx->sdf->senderName = $_POST['yourName'];
70                                                  $_ctx->sdf->senderEmail = $_POST['yourEmail'];
71                                                  $_ctx->sdf->receiverName = $_POST['yourFriendName'];
72                                                  $_ctx->sdf->receiverEmail = $_POST['yourFriendEmail'];                                             
73
74                                                  $headers = array(
75                                                       'From: '.mail::B64Header($_POST['yourName']).' <'.$_POST['yourEmail'].'>',
76                                                       'Content-Type: text/plain; charset=UTF-8;',
77                                                       'X-Originating-IP: '.http::realIP(),
78                                                       'X-Mailer: Dotclear',
79                                                       'X-Blog-Id: '.mail::B64Header($core->blog->id),
80                                                       'X-Blog-Name: '.mail::B64Header($core->blog->name),
81                                                       'X-Blog-Url: '.mail::B64Header($core->blog->url)
82                                                  );
83                                                                                                   
84                                                  $subject = mail::B64Header(tplEnvoyer::EnvoyerSearchReplace($settings->sendtofriend->get('sendtofriend_subject')));
85                                                  $sBody = tplEnvoyer::EnvoyerSearchReplace($settings->sendtofriend->get('sendtofriend_content'));
86                                                  $aFriendEmails = explode(';', $_POST['yourFriendEmail']);   
87                                                  if(count($aFriendEmails)==1){
88                                                       $aFriendEmails = explode(',', $_POST['yourFriendEmail']);   
89                                                  }
90                                                  foreach ($aFriendEmails as $email) {
91                                                       mail::sendMail($email,$subject,$sBody,$headers);
92                                                  }                             
93                                                  $_ctx->sdf->message = array(__('email sent'));   
94                                        }
95                                   }
96                                   catch (Exception $e)
97                                   {
98                                        if(strlen($e->getMessage())>0){
99                                             $_ctx->sdf->message = array(__('email not sent'));
100                                        }
101                                        else{
102                                             $_ctx->sdf->message = $_ctx->sdf->error;
103                                        }
104                                   }                             
105                         }
106               }
107          }
108          else{
109               self::p404();
110          }         
111          $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates');
112          self::serveDocument('sendtofriend.html');
113          exit;                   
114     }
115     
116     public function getAbstract($sExcerpt, $sContent){
117          global $core, $_ctx;     
118          $settings = new dcSettings($core,$core->blog->id); 
119          $settings->addNamespace('sendtofriend');
120          $sContenuFinal = ''; 
121          if($settings->sendtofriend->get('sendtofriend_abstractType') == 'firstWords'){
122               $iNb = $settings->sendtofriend->get('sendtofriend_firstWords');
123               $sContenu = str_replace('\"','&quot;',strip_tags($sExcerpt.' '.$sContent));
124               $aContenu = explode(' ',$sContenu);
125               if(count($aContenu)>$iNb)
126               {
127                    $sContenuFinal = '';
128                    foreach($aContenu as $iKey=>$sContenu)
129                    {
130                         if($iKey==$iNb) break;
131                         $sContenuFinal .= $sContenu.' ';
132                    }
133                    $sContenuFinal .= '(...)';
134               }
135               else
136               {
137                    $sContenuFinal = $sContenu;
138               }
139          }
140          elseif($settings->sendtofriend->get('sendtofriend_abstractType') == 'abstract'){
141               $sContenuFinal = str_replace('\"','&quot;',strip_tags($sExcerpt));
142          }
143          return $sContenuFinal;   
144     }
145     
146     public function EnvoyerSearchReplace($str){
147          global $core,$_ctx;
148          $in = array('%post-title%','%post-url%','%post-abstract%','%sender-name%','%sender-email%','%receiver-name%','%receiver-email%');
149          $out = array(
150               $_ctx->sdf->title,
151               $_ctx->sdf->url,
152               $_ctx->sdf->abstract,
153               $_ctx->sdf->senderName,
154               $_ctx->sdf->senderEmail,
155               $_ctx->sdf->receiverName,
156               $_ctx->sdf->receiverEmail               
157          );
158          return str_replace($in,$out,$str);
159     
160     }
161     
162     public static function EnvoyerPostId() {     
163          return '<?php echo $_ctx->sdf->id; ?>';
164     }         
165     public static function EnvoyerPostTitle() {       
166          return '<?php echo $_ctx->sdf->title; ?>';
167     }   
168     public static function EnvoyerPostUrl() {         
169          return '<?php echo $_ctx->sdf->url; ?>';
170     }         
171     
172     public static function EnvoyerMessage() {
173          $retour = '<?php
174               $sMessages = "";
175               if(count($_ctx->sdf->message)>0){
176                    $aMessages = $_ctx->sdf->message;
177                    $sMessages = "<ul>";
178                    foreach($aMessages as $sMessage){
179                         $sMessages .="<li>".$sMessage."</li>";
180                    }
181                    $sMessages .= "</ul>";
182               }
183               echo $sMessages;
184          ?>'; 
185          return $retour;
186     }
187}         
188     
189$core->tpl->addValue('EnvoyerPostId',array('tplEnvoyer','EnvoyerPostId'));
190$core->tpl->addValue('EnvoyerPostTitle',array('tplEnvoyer','EnvoyerPostTitle'));
191$core->tpl->addValue('EnvoyerPostUrl',array('tplEnvoyer','EnvoyerPostUrl'));
192$core->tpl->addValue('EnvoyerMessage',array('tplEnvoyer','EnvoyerMessage'));
193?>
Note: See TracBrowser for help on using the repository browser.

Sites map