00001 <?PHP
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00028 function sendMessage($username, $subject, $message, $from, $messagetype = "email") {
00029 switch( $messagetype )
00030 {
00031 default:
00032 if( mail( getUserInfo( $username, "email" ), $subject, $message, "From: " . getUserInfo( $from, "name") . " <" . getUserInfo( $from, "email" ) . ">" . "\r\n" . "X-Mailer: PHP/" . phpversion() ) )
00033 return true;
00034 }
00035
00036
00037 return false;
00038 }
00039
00054 class subscriptionsManager extends lockableClass
00055 {
00056 var $subscriptions;
00057 var $updates;
00058
00059 var $filename = "";
00060 var $updatesFilename = "";
00061
00065 function subscriptionsManager()
00066 {
00067 global $setup_folder;
00068 $this->lockfile = "$setup_folder/subscriptions.lock";
00069 $this->filename = "$setup_folder/subscriptions.php";
00070 $this->updatesFilename = "$setup_folder/updates.php";
00071
00072 $this->load();
00073
00074 if( $_REQUEST["globalID"] != "" )
00075 {
00076 if( $_REQUEST["subscribe"] == "yes" )
00077 $this->subscribe( currentUser(), $_REQUEST["globalID"] );
00078 else if( $_REQUEST["subscribe"] == "no" )
00079 $this->unsubscribe( currentUser(), $_REQUEST["globalID"] );
00080 }
00081 }
00082
00083 function load()
00084 {
00085 if( file_exists( $this->filename ) )
00086 include $this->filename;
00087 if( ! is_array( $subscriptions ) )
00088 $this->subscriptions = array();
00089 else
00090 $this->subscriptions = $subscriptions;
00091
00092 if( file_exists( $this->updatesFilename ) )
00093 include $this->updatesFilename;
00094 if( ! is_array( $updates ) )
00095 $this->updates = array();
00096 else
00097 $this->updates = $updates;
00098
00099 return true;
00100 }
00101
00107 function save()
00108 {
00109 if( file_exists( $this->filename ) )
00110 unlink( $this->filename );
00111
00112 $returnval = file_put_contents( $this->filename, array_export( $this->subscriptions, "subscriptions" ) );
00113
00114 if( file_exists( $this->updatesFilename ) )
00115 unlink( $this->updatesFilename );
00116
00117 $returnval = file_put_contents( $this->updatesFilename, array_export( $this->updates, "updates" ) );
00118
00119 return $returnval;
00120 }
00121
00130 function isSubscribed( $username, $globalID )
00131 {
00132 if( array_key_exists( $globalID, $this->subscriptions ) )
00133 if( array_key_exists( $username, $this->subscriptions[$globalID] ) )
00134 return true;
00135
00136 return false;
00137 }
00138
00147 function parentSubscribed( $username, $globalID )
00148 {
00149 if( $globalID == "" )
00150 return false;
00151
00152 $parentID = substr( $globalID, 0, strrpos( $globalID, "/" ) );
00153 if( $this->isSubscribed( $username, $parentID ) )
00154 return true;
00155
00156 return $this->parentSubscribed( $username, $parentID );
00157 }
00158
00168 function subscribe( $username, $globalID, $subscriptiontype = "email" )
00169 {
00170 $returnval = false;
00171 if( $this->lock() )
00172 {
00173 $this->load();
00174 $this->subscriptions[$globalID][$username][$subscriptiontype] = true;
00175 $returnval = $this->save();
00176 $this->unlock();
00177 }
00178 return $returnval;
00179 }
00180
00191 function unsubscribe( $username, $globalID, $subscriptiontype = "" )
00192 {
00193 $returnval = false;
00194 if( $this->lock() )
00195 {
00196 if( $subscriptiontype == "" )
00197 unset( $this->subscriptions[$globalID][$username] );
00198 else
00199 unset( $this->subscriptions[$globalID][$username][$subscriptiontype] );
00200
00201 foreach( $this->subscriptions as $globalID => $value )
00202 {
00203 if( count( $value ) < 1 )
00204 unset( $this->subscriptions[$globalID] );
00205 }
00206 $returnval = $this->save();
00207 $this->unlock();
00208 }
00209
00210 return $returnval;
00211 }
00212
00220 function unsubscribeAll( $username )
00221 {
00222 $returnval = false;
00223 if( $this->lock() )
00224 {
00225 foreach( $this->subscriptions as $key => $value )
00226 {
00227 if( array_key_exists( $username, $value ) )
00228 unset( $this->subscriptions[$key][$username] );
00229 }
00230 $returnval = $this->save();
00231 $this->unlock();
00232 }
00233 return $returnval;
00234 }
00235
00243 function listUserSubscriptions( $username )
00244 {
00245 $theSubscriptions = array();
00246
00247 foreach( $this->subscriptions as $key => $value )
00248 {
00249 if( array_key_exists( $username, $value ) )
00250 {
00251 foreach( $value[$username] as $subscriptiontype => $irrelevant )
00252 {
00253 $theSubscriptions[$globalID][] = $subscriptiontype;
00254 }
00255 }
00256 }
00257
00258 return $theSubscriptions;
00259 }
00260
00269 function renameGlobalID( $renameFrom, $renameTo )
00270 {
00271 $returnval = false;
00272 if( $this->lock() )
00273 {
00274 $this->load();
00275
00276 if( array_key_exists( $renameFrom, $this->subscriptions ) )
00277 {
00278 $tempSubscriptions = $this->subscriptions[$renameFrom];
00279 unset( $this->subscriptions[$renameFrom] );
00280 $this->subscriptions[$renameTo] = $tempSubscriptions;
00281 }
00282
00283 foreach( $this->updates as $key => $value )
00284 if( $value["globalID"] == $renameFrom )
00285 $this->updates[$key]["globalID"] = $renameTo;
00286
00287 $returnval = $this->save();
00288 $this->unlock();
00289 }
00290 return $returnval;
00291 }
00292
00301 function removeGlobalID( $globalID, $notify = false )
00302 {
00303 $returnval = false;
00304 if( $this->lock() )
00305 {
00306 if( is_string( $notify ) )
00307 {
00308 foreach( $this->subscriptions[$globalID] as $username => $user )
00309 {
00310 foreach( $user as $subscriptiontype => $irrelevant )
00311 sendMessage( $username, i18n("Subscription item removed"), i18n("The item ##0## has been removed, and your subscription removed. Message as follows: ##1##", array( $globalID, $notify ) ), "admin", $subscriptiontype );
00312 }
00313 }
00314
00315 unset( $this->subscriptions[$globalID] );
00316
00317 foreach( $this->updates as $key => $value )
00318 if( $value["globalID"] == $globalID )
00319 unset($this->updates[$key]);
00320
00321 $returnval = $this->save();
00322 $this->unlock();
00323 }
00324 return $returnval;
00325 }
00326
00334 function listSubscribers( $globalID )
00335 {
00336 $theSubscribers = array();
00337
00338 if( array_key_exists( $globalID, $this->subscriptions ) )
00339 {
00340 foreach( $this->subscriptions[$globalID] as $username => $irellevant )
00341 $theSubscribers[] = $username;
00342 }
00343
00344 return $theSubscribers;
00345 }
00346
00357 function notifySubscribers( $globalID, $subject, $message )
00358 {
00359 $success = true;
00360
00361 if( array_key_exists( $globalID, $this->subscriptions ) )
00362 {
00363 foreach( $this->subscriptions[$globalID] as $username => $user )
00364 {
00365 foreach( $user as $subscriptiontype => $irrelevant )
00366 if( ! $this->parentSubscribed( $username, $globalID ) && ! sendMessage( $username, $subject, $message, "admin", $subscriptiontype ) )
00367 $success = false;
00368 }
00369 }
00370
00371 $parentID = substr( $globalID, 0, strrpos( $globalID, "/" ) );
00372 if( $parentID != "" )
00373 $this->notifySubscribers( $parentID, $subject, $message );
00374
00375 return $success;
00376 }
00377
00389 function handle( $globalID, $owner, $viewLevel, $subject = "", $message = "" )
00390 {
00391 $timestamp = time();
00392
00393 if( $subject == "" )
00394 $subject = i18n( "Update on ##0##", array( $globalID ) );
00395
00396 if( $message == "" )
00397 $message = i18n( "The item ##0## has been updated on ##1##", array( $globalID, formatTime( $timestamp ) ) );
00398
00399 if( $this->lock() )
00400 {
00401 $this->load();
00402 $this->updates[] = array(
00403 "timestamp" => $timestamp,
00404 "globalID" => $globalID,
00405 "subject" => $subject,
00406 "message" => $message,
00407 "owner" => $owner,
00408 "viewLevel" => $viewLevel
00409 );
00410
00411 while( count( $this->updates ) > 10000 )
00412 array_shift( $this->updates );
00413
00414 $this->save();
00415 $this->unlock();
00416 }
00417
00418 return $this->notifySubscribers( $globalID, $subject, $message );
00419 }
00420
00431 function renderSubscribeControl( $globalID, $dataOnly = false, $extraClass = "" )
00432 {
00433 if( $this->parentSubscribed( currentUser(), $globalID ) )
00434 return "<span class=\"subscriptioncontrol command-disabled\">" . i18n("(Parent subscribed)") . "</span>";
00435 else
00436 {
00437 if( $this->isSubscribed( currentUser(), $globalID ) )
00438 {
00439 $subscribe = "no";
00440 $title = i18n("Unsubscribe");
00441 $description = i18n("Unsubscribe from ##0##", array($globalID) );
00442 }
00443 else
00444 {
00445 $subscribe = "yes";
00446 $title = i18n("Subscribe");
00447 $description = i18n("Subscribe to ##0##", array($globalID) );
00448 }
00449
00450 $url = thisPageURL( false, true, array( "subscribe" => $subscribe ) );
00451
00452 if( isAuth() )
00453 $renderedControl = drawCommand( $title, $description, $url, $extraClass );
00454
00455 return $dataOnly ? array( $title, $description, $url ) : "<span class=\"subscriptioncontrol\">" . $renderedControl . "</span>";
00456 }
00457 }
00458 }
00459 ?>