PageRenderTime 223ms CodeModel.GetById 61ms app.highlight 74ms RepoModel.GetById 77ms app.codeStats 1ms

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

https://github.com/asterix14/dolibarr
PHP | 233 lines | 137 code | 33 blank | 63 comment | 15 complexity | 9a6ba63a9924f083f210e9ae59e99d4d MD5 | raw file
  1<?php
  2/* Copyright (C) 2000-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3 * Copyright (C) 2003      Jean-Louis Bergamo   <jlb@j1b.org>
  4 * Copyright (C) 2004-2011 Laurent Destailleur  <eldy@users.sourceforge.net>
  5 * Copyright (C) 2005-2009 Regis Houssin        <regis@dolibarr.fr>
  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 2 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      CSMSFile
 32 *      \brief      Class to send SMS
 33 *      \remarks    Usage: $smsfile = new CSMSFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to);
 34 *      \remarks           $smsfile->sendfile();
 35 */
 36class CSMSFile
 37{
 38    var $error='';
 39
 40    var $message;
 41	var $addr_from;
 42	var $addr_to;
 43	var $deliveryreceipt;
 44	var $deferred;
 45	var $priority;
 46	var $class;
 47
 48
 49	/**
 50	 *	CSMSFile
 51	 *	@param 	to                  Recipients SMS
 52	 *	@param 	from                Sender SMS
 53	 *	@param 	msg                 Message
 54	 *	@param 	deliveryreceipt		Ask a delivery receipt
 55	 *	@param 	deferred			Deferred or not
 56	 *	@param 	priority			Priority
 57	 *	@param 	class				Class
 58	 */
 59	function CSMSFile($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;
 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: deliveryreceipt=".$deliveryreceipt." 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->message=$msg;
 82        $this->deliveryreceipt=$deliveryreceipt;
 83        $this->deferred=$deferred;
 84        $this->priority=$priority;
 85        $this->class=$class;
 86	}
 87
 88
 89	/**
 90	 * Send mail that was prepared by constructor
 91	 *
 92	 * @return    boolean     True if mail 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		    // Action according to choosed sending method
113		    if ($conf->global->MAIN_SMS_SENDMODE == 'ovh')    // Backward compatibility    @deprecated
114			{
115				dol_include_once('/ovh/class/ovhsms.class.php');
116				$sms=new OvhSms($this->db);
117				$sms->expe=$this->addr_from;
118				$sms->dest=$this->addr_to;
119				$sms->message=$this->message;
120				$sms->deferred=$this->deferred;
121				$sms->priority=$this->priority;
122                $sms->class=$this->class;
123
124                $res=$sms->SmsSend();
125				if ($res <= 0)
126				{
127					$this->error=$sms->error;
128					dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR);
129				}
130				else
131				{
132					dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG);
133					//var_dump($res);        // 1973128
134					$this->dump_sms_result($res);
135				}
136			}
137		    else if (! empty($conf->global->MAIN_SMS_SENDMODE))    // $conf->global->MAIN_SMS_SENDMODE looks like a value 'class@module'
138		    {
139		        $tmp=explode('@',$conf->global->MAIN_SMS_SENDMODE);
140		        $classfile=$tmp[0]; $module=(empty($tmp[1])?$tmp[0]:$tmp[1]);
141		        dol_include_once('/'.$module.'/class/'.$classfile.'.class.php');
142		        try
143		        {
144		            $classname=ucfirst($classfile);
145		            $sms = new $classname($this->db);
146		            $sms->expe=$this->addr_from;
147		            $sms->dest=$this->addr_to;
148		            $sms->message=$this->message;
149		            $sms->deferred=$this->deferred;
150		            $sms->priority=$this->priority;
151		            $sms->class=$this->class;
152
153                    $res=$sms->SmsSend();
154    				if ($res <= 0)
155    				{
156    					$this->error=$sms->error;
157    					dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR);
158    				}
159    				else
160    				{
161    					dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG);
162    					//var_dump($res);        // 1973128
163    					$this->dump_sms_result($res);
164    				}
165		        }
166		        catch(Exception $e)
167		        {
168		            dol_print_error('','Error to get list of senders: '.$e->getMessage());
169		        }
170		    }
171			else
172			{
173				// Send mail method not correctly defined
174				// --------------------------------------
175
176				return 'Bad value for MAIN_SMS_SENDMODE constant';
177			}
178		}
179		else
180		{
181			$this->error='No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_SMS';
182			dol_syslog("CSMSFile::sendfile: ".$this->error, LOG_WARNING);
183		}
184
185		error_reporting($errorlevel);              // Reactive niveau erreur origine
186
187		return $res;
188	}
189
190
191	/**
192	 *  Write content of a SMTP request into a dump file (mode = all)
193	 *  Used for debugging.
194	 */
195	function dump_sms()
196	{
197		global $conf,$dolibarr_main_data_root;
198
199		if (@is_writeable($dolibarr_main_data_root))	// Avoid fatal error on fopen with open_basedir
200		{
201			$fp = fopen($dolibarr_main_data_root."/dolibarr_sms.log","w");
202
203			fputs($fp, $this->message);
204
205			fclose($fp);
206			if (! empty($conf->global->MAIN_UMASK))
207			@chmod($outputfile, octdec($conf->global->MAIN_UMASK));
208		}
209	}
210
211    /**
212     *  Write content of a SMTP request into a dump file (mode = all)
213     *  Used for debugging.
214     */
215    function dump_sms_result($result)
216    {
217        global $conf,$dolibarr_main_data_root;
218
219        if (@is_writeable($dolibarr_main_data_root))    // Avoid fatal error on fopen with open_basedir
220        {
221            $fp = fopen($dolibarr_main_data_root."/dolibarr_sms.log","a+");
222
223            fputs($fp, "\nResult id=".$result);
224
225            fclose($fp);
226            if (! empty($conf->global->MAIN_UMASK))
227            @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
228        }
229    }
230
231}
232
233?>