dolibarr /htdocs/core/class/CSMSFile.class.php

Language PHP Lines 233
MD5 Hash 9a6ba63a9924f083f210e9ae59e99d4d
Repository https://github.com/asterix14/dolibarr.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/* Copyright (C) 2000-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
 * Copyright (C) 2003      Jean-Louis Bergamo   <jlb@j1b.org>
 * Copyright (C) 2004-2011 Laurent Destailleur  <eldy@users.sourceforge.net>
 * Copyright (C) 2005-2009 Regis Houssin        <regis@dolibarr.fr>
 *
 * This program 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.
 *
 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
 * or see http://www.gnu.org/
 *
 * Lots of code inspired from Dan Potter's CSMSFile class
 */

/**
 *      \file       htdocs/core/class/CSMSFile.class.php
 *      \brief      File of class to send sms
 *      \author	    Laurent Destailleur.
 */

/**
 *      \class      CSMSFile
 *      \brief      Class to send SMS
 *      \remarks    Usage: $smsfile = new CSMSFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to);
 *      \remarks           $smsfile->sendfile();
 */
class CSMSFile
{
    var $error='';

    var $message;
	var $addr_from;
	var $addr_to;
	var $deliveryreceipt;
	var $deferred;
	var $priority;
	var $class;


	/**
	 *	CSMSFile
	 *	@param 	to                  Recipients SMS
	 *	@param 	from                Sender SMS
	 *	@param 	msg                 Message
	 *	@param 	deliveryreceipt		Ask a delivery receipt
	 *	@param 	deferred			Deferred or not
	 *	@param 	priority			Priority
	 *	@param 	class				Class
	 */
	function CSMSFile($to,$from,$msg,$deliveryreceipt=0,$deferred=0,$priority=3,$class=1)
	{
		global $conf;

		// On definit fin de ligne
		$this->eol="\n";
		if (preg_match('/^win/i',PHP_OS)) $this->eol="\r\n";
		if (preg_match('/^mac/i',PHP_OS)) $this->eol="\r";

		// If ending method not defined
		if (empty($conf->global->MAIN_SMS_SENDMODE))
		{
		    $this->error='No SMS Engine defined';
		    return -1;
		}

		dol_syslog("CSMSFile::CSMSFile: MAIN_SMS_SENDMODE=".$conf->global->MAIN_SMS_SENDMODE." charset=".$conf->file->character_set_client." from=".$from.", to=".$to.", msg length=".count($msg), LOG_DEBUG);
		dol_syslog("CSMSFile::CSMSFile: deliveryreceipt=".$deliveryreceipt." deferred=".$deferred." priority=".$priority." class=".$class, LOG_DEBUG);

		// Action according to choosed sending method
	    $this->addr_from=$from;
	    $this->addr_to=$to;
        $this->message=$msg;
        $this->deliveryreceipt=$deliveryreceipt;
        $this->deferred=$deferred;
        $this->priority=$priority;
        $this->class=$class;
	}


	/**
	 * Send mail that was prepared by constructor
	 *
	 * @return    boolean     True if mail sent, false otherwise
	 */
	function sendfile()
	{
		global $conf;

		$errorlevel=error_reporting();
		error_reporting($errorlevel ^ E_WARNING);   // Desactive warnings

		$res=false;

        dol_syslog("CSMSFile::sendfile addr_to=".$this->addr_to, LOG_DEBUG);
        dol_syslog("CSMSFile::sendfile message=\n".$this->message);

        $this->message=stripslashes($this->message);

        if (! empty($conf->global->MAIN_SMS_DEBUG)) $this->dump_sms();

		if (empty($conf->global->MAIN_DISABLE_ALL_SMS))
		{
		    // Action according to choosed sending method
		    if ($conf->global->MAIN_SMS_SENDMODE == 'ovh')    // Backward compatibility    @deprecated
			{
				dol_include_once('/ovh/class/ovhsms.class.php');
				$sms=new OvhSms($this->db);
				$sms->expe=$this->addr_from;
				$sms->dest=$this->addr_to;
				$sms->message=$this->message;
				$sms->deferred=$this->deferred;
				$sms->priority=$this->priority;
                $sms->class=$this->class;

                $res=$sms->SmsSend();
				if ($res <= 0)
				{
					$this->error=$sms->error;
					dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR);
				}
				else
				{
					dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG);
					//var_dump($res);        // 1973128
					$this->dump_sms_result($res);
				}
			}
		    else if (! empty($conf->global->MAIN_SMS_SENDMODE))    // $conf->global->MAIN_SMS_SENDMODE looks like a value 'class@module'
		    {
		        $tmp=explode('@',$conf->global->MAIN_SMS_SENDMODE);
		        $classfile=$tmp[0]; $module=(empty($tmp[1])?$tmp[0]:$tmp[1]);
		        dol_include_once('/'.$module.'/class/'.$classfile.'.class.php');
		        try
		        {
		            $classname=ucfirst($classfile);
		            $sms = new $classname($this->db);
		            $sms->expe=$this->addr_from;
		            $sms->dest=$this->addr_to;
		            $sms->message=$this->message;
		            $sms->deferred=$this->deferred;
		            $sms->priority=$this->priority;
		            $sms->class=$this->class;

                    $res=$sms->SmsSend();
    				if ($res <= 0)
    				{
    					$this->error=$sms->error;
    					dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR);
    				}
    				else
    				{
    					dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG);
    					//var_dump($res);        // 1973128
    					$this->dump_sms_result($res);
    				}
		        }
		        catch(Exception $e)
		        {
		            dol_print_error('','Error to get list of senders: '.$e->getMessage());
		        }
		    }
			else
			{
				// Send mail method not correctly defined
				// --------------------------------------

				return 'Bad value for MAIN_SMS_SENDMODE constant';
			}
		}
		else
		{
			$this->error='No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_SMS';
			dol_syslog("CSMSFile::sendfile: ".$this->error, LOG_WARNING);
		}

		error_reporting($errorlevel);              // Reactive niveau erreur origine

		return $res;
	}


	/**
	 *  Write content of a SMTP request into a dump file (mode = all)
	 *  Used for debugging.
	 */
	function dump_sms()
	{
		global $conf,$dolibarr_main_data_root;

		if (@is_writeable($dolibarr_main_data_root))	// Avoid fatal error on fopen with open_basedir
		{
			$fp = fopen($dolibarr_main_data_root."/dolibarr_sms.log","w");

			fputs($fp, $this->message);

			fclose($fp);
			if (! empty($conf->global->MAIN_UMASK))
			@chmod($outputfile, octdec($conf->global->MAIN_UMASK));
		}
	}

    /**
     *  Write content of a SMTP request into a dump file (mode = all)
     *  Used for debugging.
     */
    function dump_sms_result($result)
    {
        global $conf,$dolibarr_main_data_root;

        if (@is_writeable($dolibarr_main_data_root))    // Avoid fatal error on fopen with open_basedir
        {
            $fp = fopen($dolibarr_main_data_root."/dolibarr_sms.log","a+");

            fputs($fp, "\nResult id=".$result);

            fclose($fp);
            if (! empty($conf->global->MAIN_UMASK))
            @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
        }
    }

}

?>
Back to Top