gforge-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gforge-commits] gforge/common/mail MailingList.class, NONE, 1.1 Mailing


From: gsmet
Subject: [Gforge-commits] gforge/common/mail MailingList.class, NONE, 1.1 MailingListFactory.class, NONE, 1.1
Date: Thu, 15 Jan 2004 04:30:45 -0600

Update of /cvsroot/gforge/gforge/common/mail
In directory db.perdue.net:/tmp/cvs-serv27422/common/mail

Added Files:
        MailingList.class MailingListFactory.class 
Log Message:
rewrote mailing list management (patch #643)

--- NEW FILE: MailingList.class ---
<?php
/**
 * GForge Mailing Lists Facility
 *
 * Copyright 2003 Guillaume Smet
 * http://gforge.org/
 *
 * @version   $Id: MailingList.class,v 1.1 2004/01/15 10:30:42 gsmet Exp $
 *
 * This file is part of GForge.
 *
 * GForge is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GForge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GForge; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


 /*
 
 This work is based on Tim Perdue's work on the forum stuff
 
 */

require_once('common/include/Error.class');

define('MAIL__MAILING_LIST_IS_PRIVATE', '0');
define('MAIL__MAILING_LIST_IS_PUBLIC', '1');
define('MAIL__MAILING_LIST_IS_DELETED', '9');

class MailingList extends Error {

        /**
         * Associative array of data from db.
         *
         * @var  array   $dataArray.
         */
        var $dataArray;

        /**
         * The Group object.
         *
         * @var  object  $Group.
         */
        var $Group;
        
        /**
         * The mailing list id
         *
         * @var int $groupMailingListId
         */
        var $groupMailingListId;

        /**
         *  Constructor.
         *
         * @param       object  The Group object to which this mailing list is 
associated.
         * @param       int             The group_list_id.
         * @param       array           The associative array of data.
         * @return      boolean success.
         */
        function MailingList(&$Group, $groupListId = false, $dataArray = false) 
{
                global $Language;
                $this->Error();
                if (!$Group || !is_object($Group)) {
                        $this->setError($Language->getText('general', 
'error_no_valid_group_object', array('MailingList')));
                        return false;
                }
                if ($Group->isError()) {
                        $this->setError('MailingList:: 
'.$Group->getErrorMessage());
                        return false;
                }
                $this->Group =& $Group;

                if ($groupListId) {
                        $this->groupMailingListId = $groupListId;
                        if (!$dataArray || !is_array($dataArray)) {
                                if (!$this->fetchData($groupListId)) {
                                        return false;
                                }
                        } else {
                                $this->dataArray =& $dataArray;
                                if ($this->dataArray['group_id'] != 
$this->Group->getID()) {
                                        
$this->setError($Language->getText('general', 'error_group_id'));
                                        $this->dataArray = null;
                                        return false;
                                }
                        }
                        if (!$this->isPublic()) {
                                $perm =& 
$this->Group->getPermission(session_get_user());

                                if (!$perm || !is_object($perm) || 
!$perm->isMember()) {
                                        $this->setPermissionDeniedError();
                                        $this->dataArray = null;
                                        return false;
                                }
                        }
                }

                return true;
        }

        /**
         *      create - use this function to create a new entry in the 
database.
         *
         *      @param  string  The name of the mailing list
         *      @param  string  The description of the mailing list
         *      @param  int     Pass (1) if it should be public (0) for private.
         *
         *      @return boolean success.
         */
        function create($listName, $description, $isPublic = 1) {
                global $Language;
                
                if(! 
MailingListFactory::userCanAdminMailingLists($this->Group)) {
                        $this->setPermissionDeniedError();
                        return false;
                }
                
                if(!$listName || strlen($listName) < 4) {
                        $this->setError($Language->getText('mail_common', 
'error_min_name_length'));
                        return false;
                }
                
                $realListName = 
strtolower($this->Group->getUnixName().'-'.$listName);
                
                
if(!validate_email($realListName.'@'.$GLOBALS['sys_lists_host'])) {
                        $this->setError($Language->getText('mail_common', 
'error_invalid_name'));
                        return false;
                }

                $result = db_query('SELECT 1 FROM mail_group_list WHERE 
lower(list_name)=\''.$realListName.'\'');

                if (db_numrows($result) > 0) {
                        $this->setError($Language->getText('mail_common', 
'error_list_already_exists'));
                        return false;
                }
                        
                $listPassword = substr(md5($GLOBALS['session_hash'] . time() . 
rand(0,40000)), 0, 16);
                
                $sql = 'INSERT INTO mail_group_list '
                        . '(group_id, list_name, is_public, password, 
list_admin, status, description) VALUES ('
                        . $this->Group->getID(). ', '
                        . "'".$realListName."',"
                        . "'".$isPublic."',"
                        . "'".$listPassword."',"
                        . "'".user_getid()."',"
                        . "1,"
                        . "'".$description."')";
                
                db_begin();
                $result = db_query($sql);
                
                if (!$result) {
                        db_rollback();
                        $this->setError($Language->getText('general', 
'error_creating', array($Language->getText('mail_common', 
'mailing_list'))).db_error());
                        return false;
                }
                        
                $this->groupMailingListId = db_insertid($result, 
'mail_group_list', 'group_list_id');
                $this->fetchData($this->groupMailingListId);
                
                $user = &session_get_user();
                $userEmail = $user->getEmail();
                if(empty($userEmail) || !validate_email($userEmail)) {
                        db_rollback();
                        $this->setInvalidEmailError();
                        return false;
                } else {
                        $mailBody = 
stripcslashes($Language->getText('mail_common', 'creation_mail_body', 
array($GLOBALS['sys_name'], $GLOBALS['sys_lists_host'], $realListName, 
$this->getExternalInfoUrl(), $this->getExternalAdminUrl(), $listPassword)));
                        $mailSubject = $Language->getText('mail_common', 
'creation_mail_subject', array($GLOBALS['sys_name']));
                        
                        util_send_message($userEmail, $mailSubject, $mailBody, 
'admin@'.$GLOBALS['sys_default_domain']);
                }
                
                db_commit();
                return true;
        }

        /**
         *  fetchData - re-fetch the data for this mailing list from the 
database.
         *
         *  @param  int  The list_id.
         *      @return boolean success.
         */
        function fetchData($groupListId) {
                global $Language;
                $res=db_query("SELECT * FROM mail_group_list "
                        . "WHERE group_list_id='".$groupListId."' "
                        . "AND group_id='". $this->Group->getID() ."'");
                if (!$res || db_numrows($res) < 1) {
                        $this->setError($Language->getText('general', 
'error_getting', array($Language->getText('mail_common', 'mailing_list'))));
                        return false;
                }
                $this->dataArray =& db_fetch_array($res);
                db_free_result($res);
                return true;
        }

        /**
         *      update - use this function to update an entry in the database.
         *
         *      @param  string  The description of the mailing list
         *      @param  int     Pass (1) if it should be public (0) for private 
(9) for deleted
         *      @return boolean success.
         */
        function update($description, $isPublic = MAIL__MAILING_LIST_IS_PUBLIC) 
{
                global $Language;
                
                if(! 
MailingListFactory::userCanAdminMailingLists($this->Group)) {
                        $this->setPermissionDeniedError();
                        return false;
                }
                
                $sql = "UPDATE mail_group_list ".
                        "SET is_public='".$isPublic."', ".
                                "description='". $description ."' ".
                        "WHERE group_list_id='".$this->groupMailingListId."' ".
                        "AND group_id='".$this->Group->getID()."'";
                        
                echo $sql;
                        
                $res = db_query($sql);

                if (!$res || db_affected_rows($res) < 1) {
                        $this->setError($Language->getText('general', 
'error_on_update').db_error());
                        return false;
                }
                return true;
        }

        /**
         *      getGroup - get the Group object this mailing list is associated 
with.
         *
         *      @return object  The Group object.
         */
        function &getGroup() {
                return $this->Group;
        }

        /**
         *      getID - The id of this mailing list
         *
         *      @return int     The group_list_id #.
         */
        function getID() {
                return $this->dataArray['group_list_id'];
        }


        /**
         *      isPublic - Is this mailing list open to the general public.
         *
         *      @return boolean is_public.
         */
        function isPublic() {
                return $this->dataArray['is_public'];
        }

        /**
         *      getName - get the name of this mailing list
         *
         *      @return string  The name of this mailing list
         */
        function getName() {
                return $this->dataArray['list_name'];
        }


        /**
         *      getDescription - get the description of this mailing list
         *
         *      @return string  The description.
         */
        function getDescription() {
                return $this->dataArray['description'];
        }
        
        /**
         * getPassword - get the password to administrate the mailing list
         *
         * @return string The password
         */
        function getPassword() {
                return $this->dataArray['password'];
        }
        
        /**
         * getListAdmin - get the user who is the admin of this mailing list
         *
         * @return User The admin user
         */
        function getListAdmin() {
                return user_get_object($this->dataArray['list_admin']);
        }
        
        /**
         * getStatus - get the status of this mailing list
         *
         * seems to be useless
         *
         * @return int The status
         */
        function getStatus() {
                return $this->dataArray['status'];
        }
        
        /**
         * getArchivesUrl - get the url to see the archives of the list
         *
         * @return string url of the archives
         */
        function getArchivesUrl() {
                return 
'http://'.$GLOBALS['sys_lists_host'].'/pipermail/'.$this->getName().'/';
        }
        
        /**
         * getExternalInfoUrl - get the url to subscribe/unsubscribe
         *
         * @return string url of the info page
         */
        function getExternalInfoUrl() {
                return 
'http://'.$GLOBALS['sys_lists_host'].'/mailman/listinfo/'.$this->getName();
        }
        
        /**
         * getExternalAdminUrl - get the url to admin the list with the 
external tools used
         *
         * @return string url of the admin
         */
        function getExternalAdminUrl() {
                return 
'http://'.$GLOBALS['sys_lists_host'].'/mailman/admin/'.$this->getName();
        }

}

?>
--- NEW FILE: MailingListFactory.class ---
<?php
/**
 * GForge Mailing Lists Facility
 *
 * Copyright 2003 Guillaume Smet
 * http://gforge.org/
 *
 * @version   $Id: MailingListFactory.class,v 1.1 2004/01/15 10:30:42 gsmet Exp 
$
 *
 * This file is part of GForge.
 *
 * GForge is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GForge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GForge; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 
 /*
 
 This work is based on Tim Perdue's work on the forum stuff
 
 */

require_once('common/include/Error.class');
require_once('common/mail/MailingList.class');

class MailingListFactory extends Error {

        /**
         * The Group object.
         *
         * @var  object  $Group.
         */
        var $Group;

        /**
         * The mailing lists array.
         *
         * @var  array  $mailingLists.
         */
        var $mailingLists;


        /**
         *  Constructor.
         *
         *      @param  object  The Group object to which these mailing lists 
are associated.
         */
        function MailingListFactory(& $Group) {
                global $Language;
                $this->Error();
                
                if (!$Group || !is_object($Group)) {
                        $this->setError($Language->getText('general', 
'error_no_valid_group_object', array('MailingListFactory')));
                        return false;
                }
                if ($Group->isError()) {
                        $this->setError('MailingListFactory:: 
'.$Group->getErrorMessage());
                        return false;
                }
                $this->Group =& $Group;

                return true;
        }

        /**
         *      getGroup - get the Group object this MailingListFactory is 
associated with.
         *
         *      @return object  The Group object.
         */
        function &getGroup() {
                return $this->Group;
        }

        /**
         *      getMailingLists - get an array of MailingList objects for this 
Group.
         *
         * @param boolean $admin if we are in admin mode (we want to see 
deleted lists)
         *      @return array   The array of MailingList objects.
         */
        function &getMailingLists($admin = false) {
                global $Language;
                if (isset($this->mailingLists) && 
is_array($this->mailingLists)) {
                        return $this->mailingLists;
                }
                
                $public_flag = MAIL__MAILING_LIST_IS_PUBLIC;
                
                $perm = & $this->Group->getPermission(session_get_user());
                if ($perm && is_object($perm) && $perm->isMember()) {
                        $public_flag = MAIL__MAILING_LIST_IS_PRIVATE.', 
'.MAIL__MAILING_LIST_IS_PUBLIC;
                        if($admin && 
$this->userCanAdminMailingLists($this->Group)) {
                                $public_flag .= ', 
'.MAIL__MAILING_LIST_IS_DELETED;
                        }
                }

                $sql = 'SELECT * '
                        . 'FROM mail_group_list '
                        . 'WHERE group_id=\''.$this->Group->getID().'\' '
                        . 'AND is_public IN ('.$public_flag.') '
                        . 'ORDER BY list_name;';
                

                $result = db_query($sql);

                if (!$result) {
                        $this->setError($Language->getText('general', 
'error_getting', array($Language->getText('mail_common', 
'mailing_list'))).db_error());
                        return false;
                } else {
                        $this->mailingLists = array();
                        while ($arr = db_fetch_array($result)) {
                                $this->mailingLists[] = new 
MailingList($this->Group, $arr['group_list_id'], $arr);
                        }
                }
                return $this->mailingLists;
        }

        /**
         * userCanAdminMailingList - use this function to know if the user can 
administrate mailing lists
         *
         * This is a static method. Currently the user must be a project or a 
sitewide admin to administrate the mailing lists
         *
         * @param object $Group group object
         * @return boolean true if the user can administrate mailing lists
         */
        function userCanAdminMailingLists(& $Group) {
                $perm = & $Group->getPermission(session_get_user());
                if ($perm && is_object($perm) && $perm->isAdmin()) {
                        return true;
                } else {
                        return false;
                }
        }

}

?>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]