Completed
Branch develop (e6f0e7)
by
unknown
24:49
created

CSMSFile::dump_sms()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 0
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2000-2005 Rodolphe Quiedeville <[email protected]>
3
 * Copyright (C) 2003      Jean-Louis Bergamo   <[email protected]>
4
 * Copyright (C) 2004-2012 Laurent Destailleur  <[email protected]>
5
 * Copyright (C) 2005-2009 Regis Houssin        <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19
 * or see http://www.gnu.org/
20
 *
21
 * Lots of code inspired from Dan Potter's CSMSFile class
22
 */
23
24
/**
25
 *      \file       htdocs/core/class/CSMSFile.class.php
26
 *      \brief      File of class to send sms
27
 *      \author	    Laurent Destailleur.
28
 */
29
30
/**
31
 *		Class to send SMS
32
 *      Usage: $smsfile = new CSMSFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to);
33
 *             $smsfile->sendfile();
34
 */
35
class CSMSFile
36
{
37
    var $error='';
38
39
	var $addr_from;
40
	var $addr_to;
41
	var $deferred;
42
	var $priority;
43
	var $class;
44
	var $message;
45
	var $nostop;
46
47
48
	/**
49
	 *	CSMSFile
50
	 *
51
	 *	@param	string	$to                 Recipients SMS
52
	 *	@param 	string	$from               Sender SMS
53
	 *	@param 	string	$msg                Message
54
	 *	@param 	int		$deliveryreceipt	Not used
55
	 *	@param 	int		$deferred			Deferred or not
56
	 *	@param 	int		$priority			Priority
57
	 *	@param 	int		$class				Class
58
	 */
59
	function __construct($to,$from,$msg,$deliveryreceipt=0,$deferred=0,$priority=3,$class=1)
60
	{
61
		global $conf;
62
63
		// On definit fin de ligne
64
		$this->eol="\n";
65
		if (preg_match('/^win/i',PHP_OS)) $this->eol="\r\n";
66
		if (preg_match('/^mac/i',PHP_OS)) $this->eol="\r";
67
68
		// If ending method not defined
69
		if (empty($conf->global->MAIN_SMS_SENDMODE))
70
		{
71
		    $this->error='No SMS Engine defined';
72
		    return -1;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
73
		}
74
75
		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);
76
		dol_syslog("CSMSFile::CSMSFile: deferred=".$deferred." priority=".$priority." class=".$class, LOG_DEBUG);
77
78
		// Action according to choosed sending method
79
	    $this->addr_from=$from;
80
	    $this->addr_to=$to;
81
        $this->deferred=$deferred;
82
        $this->priority=$priority;
83
        $this->class=$class;
84
        $this->message=$msg;
85
        $this->nostop=false;
86
	}
87
88
89
	/**
90
	 * Send sms that was prepared by constructor
91
	 *
92
	 * @return    boolean     True if sms sent, false otherwise
93
	 */
94
	function sendfile()
95
	{
96
		global $conf;
97
98
		$errorlevel=error_reporting();
99
		error_reporting($errorlevel ^ E_WARNING);   // Desactive warnings
100
101
		$res=false;
102
103
        dol_syslog("CSMSFile::sendfile addr_to=".$this->addr_to, LOG_DEBUG);
104
        dol_syslog("CSMSFile::sendfile message=\n".$this->message);
105
106
        $this->message=stripslashes($this->message);
107
108
        if (! empty($conf->global->MAIN_SMS_DEBUG)) $this->dump_sms();
109
110
		if (empty($conf->global->MAIN_DISABLE_ALL_SMS))
111
		{
112
113
		    // Action according to choosed sending method
114
		    if ($conf->global->MAIN_SMS_SENDMODE == 'ovh')    // Backward compatibility    @deprecated
115
			{
116
				dol_include_once('/ovh/class/ovhsms.class.php');
117
				$sms=new OvhSms($this->db);
118
				$sms->expe=$this->addr_from;
119
				$sms->dest=$this->addr_to;
120
				$sms->message=$this->message;
121
				$sms->deferred=$this->deferred;
122
				$sms->priority=$this->priority;
123
                $sms->class=$this->class;
124
                $sms->nostop=$this->nostop;
125
126
                $res=$sms->SmsSend();
127
				if ($res <= 0)
128
				{
129
					$this->error=$sms->error;
130
					dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR);
131
				}
132
				else
133
				{
134
					dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG);
135
					//var_dump($res);        // 1973128
136
					if (! empty($conf->global->MAIN_SMS_DEBUG)) $this->dump_sms_result($res);
137
				}
138
			}
139
		    else if (! empty($conf->global->MAIN_SMS_SENDMODE))    // $conf->global->MAIN_SMS_SENDMODE looks like a value 'class@module'
140
		    {
141
		        $tmp=explode('@',$conf->global->MAIN_SMS_SENDMODE);
142
		        $classfile=$tmp[0]; $module=(empty($tmp[1])?$tmp[0]:$tmp[1]);
143
		        dol_include_once('/'.$module.'/class/'.$classfile.'.class.php');
144
		        try
145
		        {
146
		            $classname=ucfirst($classfile);
147
		            $sms = new $classname($this->db);
148
		            $sms->expe=$this->addr_from;
149
		            $sms->dest=$this->addr_to;
150
		            $sms->deferred=$this->deferred;
151
		            $sms->priority=$this->priority;
152
		            $sms->class=$this->class;
153
		            $sms->message=$this->message;
154
		            $sms->nostop=$this->nostop;
155
156
                    $res=$sms->SmsSend();
157
                    $this->error = $sms->error;
158
                    $this->errors = $sms->errors;
159
    				if ($res <= 0)
160
    				{
161
    					dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR);
162
    				}
163
    				else
164
    				{
165
    					dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG);
166
    					//var_dump($res);        // 1973128
167
    					if (! empty($conf->global->MAIN_SMS_DEBUG)) $this->dump_sms_result($res);
168
    				}
169
		        }
170
		        catch(Exception $e)
171
		        {
172
		            dol_print_error('','Error to get list of senders: '.$e->getMessage());
173
		        }
174
		    }
175
			else
176
			{
177
				// Send sms method not correctly defined
178
				// --------------------------------------
179
180
				return 'Bad value for MAIN_SMS_SENDMODE constant';
181
			}
182
		}
183
		else
184
		{
185
			$this->error='No sms sent. Feature is disabled by option MAIN_DISABLE_ALL_SMS';
186
			dol_syslog("CSMSFile::sendfile: ".$this->error, LOG_WARNING);
187
		}
188
189
		error_reporting($errorlevel);              // Reactive niveau erreur origine
190
191
		return $res;
192
	}
193
194
195
	/**
196
	 *  Write content of a SendSms request into a dump file (mode = all)
197
	 *  Used for debugging.
198
	 *
199
	 *  @return	void
200
	 */
201
	function dump_sms()
202
	{
203
		global $conf,$dolibarr_main_data_root;
204
205
		if (@is_writeable($dolibarr_main_data_root))	// Avoid fatal error on fopen with open_basedir
206
		{
207
			$outputfile=$dolibarr_main_data_root."/dolibarr_sms.log";
208
			$fp = fopen($outputfile,"w");
209
210
			fputs($fp, "From: ".$this->addr_from."\n");
211
			fputs($fp, "To: ".$this->addr_to."\n");
212
			fputs($fp, "Priority: ".$this->priority."\n");
213
			fputs($fp, "Class: ".$this->class."\n");
214
			fputs($fp, "Deferred: ".$this->deferred."\n");
215
			fputs($fp, "DisableStop: ".$this->nostop."\n");
216
			fputs($fp, "Message:\n".$this->message);
217
218
			fclose($fp);
219
			if (! empty($conf->global->MAIN_UMASK))
220
			@chmod($outputfile, octdec($conf->global->MAIN_UMASK));
221
		}
222
	}
223
224
    /**
225
     *  Write content of a SendSms result into a dump file (mode = all)
226
     *  Used for debugging.
227
     *
228
     *  @param	int		$result		Result of sms sending
229
     *  @return	void
230
     */
231
    function dump_sms_result($result)
232
    {
233
        global $conf,$dolibarr_main_data_root;
234
235
        if (@is_writeable($dolibarr_main_data_root))    // Avoid fatal error on fopen with open_basedir
236
        {
237
        	$outputfile=$dolibarr_main_data_root."/dolibarr_sms.log";
238
            $fp = fopen($outputfile,"a+");
239
240
            fputs($fp, "\nResult id=".$result);
241
242
            fclose($fp);
243
            if (! empty($conf->global->MAIN_UMASK))
244
            @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
245
        }
246
    }
247
248
}
249
250