ultimix

- Main Page
- Data Structures
- Files
- Examples

- File List
- Globals

subscription_algorithms.php
Go to the documentation of this file.
1 <?php
2
3 /*
4 * This source code is a part of the Ultimix Project.
5 * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6 * it's own license wich could be found from the corresponding files or sources.
7 * This source code is provided "as is" without any warranties or garanties.
8 *
9 * Have a nice day!
10 *
11 * @url http://ultimix.sorceforge.net
12 *
13 * @author Alexey "gdever" Dodonov
14 */
15
26 class subscription_algorithms_1_0_0{
27
38 var $AutoMarkup = false;
39 var $CachedMultyFS = false;
40 var $Link = false;
41 var $Security = false;
42 var $SecurityParser = false;
43 var $String = false;
44 var $UserAlgorithms = false;
45
56 function __construct()
57 {
58 try
59 {
60 $this->AutoMarkup = get_package( 'page::auto_markup' , 'last' , __FILE__ );
61 $this->CachedMultyFS = get_package( 'cached_multy_fs' , 'last' , __FILE__ );
62 $this->Link = get_package( 'link' , 'last' , __FILE__ );
63 $this->Security = get_package( 'security' , 'last' , __FILE__ );
64 $this->SecurityParser = get_package( 'security::security_parser' , 'last' , __FILE__ );
65 $this->String = get_package( 'string' , 'last' , __FILE__ );
66 $this->SubscriptionAccess = get_package( 'subscription::subscription_access' , 'last' , __FILE__ );
67 $this->UserAlgorithms = get_package( 'user::user_algorithms' , 'last' , __FILE__ );
68 }
69 catch( Exception $e )
70 {
71 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
72 }
73 }
74
101 function compile_subscription( $User , $Subscription )
102 {
103 try
104 {
105 $Code = $this->CachedMultyFS->get_template( __FILE__ , get_field( $Subscription , 'template' ).'.tpl' );
106
107 $User = set_field( $User , 'user_id' , get_field( $User , 'id' ) );
108 $Code = $this->String->print_record( $Code , $User );
109
110 $Subscription = set_field( $Subscription , 'subscription_id' , get_field( $Subscription , 'id' ) );
111 $Code = $this->String->print_record( $Code , $Subscription );
112
113 $Code = $this->AutoMarkup->compile_string( $Code );
114
115 return( $Code );
116 }
117 catch( Exception $e )
118 {
119 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
120 }
121 }
122
149 function calculate_hash( $UserId , $SubscriptionId )
150 {
151 try
152 {
153 $UserId = $this->Security->get( $UserId , 'integer' );
154 $SubscriptionId = $this->Security->get( $SubscriptionId , 'integer' );
155
156 $Link = $this->Link->get_links( $UserId , $SubscriptionId , 'user' , 'subscription' );
157
158 if( isset( $Link[ 0 ] ) )
159 {
160 $LinkId = get_field( $Link[ 0 ] , 'id' );
161 return( md5( "$UserId.$LinkId" ) );
162 }
163
164 return( false );
165 }
166 catch( Exception $e )
167 {
168 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
169 }
170 }
171
194 function unsubscribe_user( $UserId , $Hash )
195 {
196 try
197 {
198 $UserId = $this->Security->get( $UserId , 'integer' );
199 $Hash = $this->Security->get( $Hash , 'string' );
200
201 $Subscriptions = $this->SubscriptionAccess->get_subscriptions_for_user( $UserId );
202
203 foreach( $Subscriptions as $i => $Subscription )
204 {
205 $SubscriptionId = get_field( $Subscription , 'id' );
206
207 if( $this->calculate_hash( $UserId , $SubscriptionId ) == $Hash )
208 {
209 $this->SubscriptionAccess->unsubscribe_user( $UserId , $SubscriptionId );
210 return;
211 }
212 }
213 }
214 catch( Exception $e )
215 {
216 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
217 }
218 }
219
246 function is_subscribed( $UserId , $SubscriptionId )
247 {
248 try
249 {
250 $UserId = $this->Security->get( $UserId , 'integer' );
251 $SubscriptionId = $this->Security->get( $SubscriptionId , 'integer' );
252
253 $Links = $this->Link->get_links( $UserId , $SubscriptionId , 'user' , 'subscription' );
254
255 return( isset( $Links[ 0 ] ) );
256 }
257 catch( Exception $e )
258 {
259 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
260 }
261 }
262
285 function get_by_id( $id )
286 {
287 try
288 {
289 $id = $this->Security->get( $id , 'integer' );
290
291 $Subscriptions = $this->SubscriptionAccess->unsafe_select(
292 $this->SubscriptionAccess->NativeTable.".id = $id"
293 );
294
295 if( count( $Subscriptions ) === 0 || count( $Subscriptions ) > 1 )
296 {
297 throw( new Exception( 'Subscription with id '.$id.' was not found' ) );
298 }
299 else
300 {
301 return( $Subscriptions[ 0 ] );
302 }
303 }
304 catch( Exception $e )
305 {
306 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
307 }
308 }
309
340 private function get_subscription_parameters( $Subscription , $From , $Sender )
341 {
342 try
343 {
344 $Settings = get_package_object( 'settings::settings' , 'last' , __FILE__ );
345 $Settings->load_package_settings(
346 'subscription::subscription_algorithms' , 'last' , 'cf_'.get_field( $Subscription , 'template' )
347 );
348 $From = $Settings->get_setting( 'email' , $From );
349 $Subject = $Settings->get_setting( 'subject' , 'Subscription' );
350 $Sender = $Settings->get_setting( 'email_sender' , $Sender );
351
352 return( array( $From , $Subject , $Sender ) );
353 }
354 catch( Exception $e )
355 {
356 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
357 }
358 }
359
382 private function get_email_parameters( $Subscription )
383 {
384 try
385 {
386 $SiteSettings = get_package_object( 'settings::settings' , 'last' , __FILE__ );
387 $SiteSettings->load_package_settings( 'page::page_composer' , 'last' , 'cf_site' );
388 $Sender = $SiteSettings->get_setting( 'email_sender' , 'System' );
389 $From = $SiteSettings->get_setting( 'system_email' , 'ultimix@localhost' );
390
391 return( $this->get_subscription_parameters( $Subscription , $From , $Sender ) );
392 }
393 catch( Exception $e )
394 {
395 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
396 }
397 }
398
425 private function send_email( $EmailAddress , $Message , $Subscription )
426 {
427 try
428 {
429 $Email = get_package( 'mail' , 'last' , __FILE__ );
430
431 list( $From , $Subject , $Sender ) = $this->get_email_parameters( $Subscription );
432
433 $Email->send_email( $From , $EmailAddress , $Subject , $Message , $Sender );
434 }
435 catch( Exception $e )
436 {
437 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
438 }
439 }
440
463 function send_subscription_for_user( $UserId , $SubscriptionId )
464 {
465 try
466 {
467 $UserId = $this->Security->get( $UserId , 'integer' );
468 $SubscriptionId = $this->Security->get( $SubscriptionId , 'integer' );
469
470 if( $this->is_subscribed( $UserId , $SubscriptionId ) )
471 {
472 $User = $this->UserAlgorithms->get_by_id( $UserId );
473 $Subscription = $this->get_by_id( $SubscriptionId );
474
475 $Message = $this->compile_subscription( $User , $Subscription );
476
477 $this->send_email( get_field( $User , 'email' ) , $Message , $Subscription );
478 }
479 }
480 catch( Exception $e )
481 {
482 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
483 }
484 }
485 }
486 ?>