File: 0.01.1a/core/Email.php (View as HTML)

  1: <?php 
  2: /* -------------------------------------------------------------
  3: This file is part of FreeDESK
  4: 
  5: FreeDESK is (C) Copyright 2012 David Cutting
  6: 
  7: FreeDESK 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: FreeDESK 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 FreeDESK.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freedesk/
 21: -------------------------------------------------------------- */
 22: 
 23: /**
 24:  * Email Class - provides email sending functionality for FreeDESK
 25: **/
 26: class Email
 27: {
 28: 	/**
 29: 	 * FreeDESK instance
 30: 	**/
 31: 	private $DESK = null;
 32: 	
 33: 	/**
 34: 	 * Account Data loaded flag
 35: 	**/
 36: 	private $loaded = false;
 37: 	
 38: 	/**
 39: 	 * Email account information
 40: 	**/
 41: 	private $accounts = array();
 42: 	
 43: 	/**
 44: 	 * Email account count
 45: 	**/
 46: 	private $accountCount = 0;
 47: 	
 48: 	/**
 49: 	 * Constructor
 50: 	 * @param mixed &$freeDESK FreeDESK instance
 51: 	**/
 52: 	function Email(&$freeDESK)
 53: 	{
 54: 		$this->DESK = &$freeDESK;
 55: 		$this->DESK->PluginManager->Register(new Plugin(
 56: 			"Email System", "0.01", "Core" ));
 57: 		$this->DESK->Include->IncludeFile("core/phpmailer/class.phpmailer.php");
 58: 		$this->DESK->Include->IncludeFile("core/phpmailer/class.smtp.php");
 59: 		$this->DESK->PluginManager->Register(new Plugin(
 60: 			"phpMailer", "5.2.1", "Email" ));
 61: 		$this->DESK->PermissionManager->Register("email_accounts",false);
 62: 		$this->DESK->PermissionManager->Register("email_templates",false);
 63: 	}
 64: 	
 65: 	/**
 66: 	 * Load account information
 67: 	**/
 68: 	private function LoadAccounts()
 69: 	{
 70: 		if (!$this->loaded)
 71: 		{
 72: 			$q="SELECT * FROM ".$this->DESK->Database->Table("email");
 73: 			$r=$this->DESK->Database->Query($q);
 74: 			$this->accountCount = 0;
 75: 			$this->accounts = array();
 76: 			while ($row=$this->DESK->Database->FetchAssoc($r))
 77: 			{
 78: 				$this->accountCount++;
 79: 				$this->accounts[$row['accountid']] = $row;
 80: 			}
 81: 			$this->DESK->Database->Free($r);
 82: 			$this->loaded = true;
 83: 		}
 84: 	}
 85: 	
 86: 	/**
 87: 	 * Check if there are email accounts
 88: 	 * @return bool Account indicator flag
 89: 	**/
 90: 	function hasAccounts()
 91: 	{
 92: 		if (!$this->loaded)
 93: 			$this->LoadAccounts();
 94: 		if ($this->accountCount > 0)
 95: 			return true;
 96: 		return false;
 97: 	}
 98: 	
 99: 	/**
100: 	 * Get email accounts
101: 	 * @return array Email account data
102: 	**/
103: 	function GetAccounts()
104: 	{
105: 		if (!$this->loaded)
106: 			$this->LoadAccounts();
107: 		return $this->accounts;
108: 	}
109: 	
110: 	/**
111: 	 * Save/Create Email account
112: 	 * @param string $name Account name
113: 	 * @param string $host SMTP host
114: 	 * @param string $from From email
115: 	 * @param string $fromName From Name
116: 	 * @param int $wordwrap Word Wrap
117: 	 * @param int $auth Use Authentication
118: 	 * @param string $username Username
119: 	 * @param string $password Password
120: 	 * @param string $smtpsec SMTP Security Mode
121: 	 * @param int $id Account ID (optional, default 0=create new)
122: 	**/
123: 	function SaveAccount($name, $host, $from, $fromName, $wordwrap, $auth, $username,
124: 		$password, $smtpsec, $id=0)
125: 	{
126: 		if ($id == 0)
127: 		{
128: 			$q="INSERT INTO ".$this->DESK->Database->Table("email")."(";
129: 			$q.=$this->DESK->Database->Field("name").",";
130: 			$q.=$this->DESK->Database->Field("host").",";
131: 			$q.=$this->DESK->Database->Field("from").",";
132: 			$q.=$this->DESK->Database->Field("fromname").",";
133: 			$q.=$this->DESK->Database->Field("wordwrap").",";
134: 			$q.=$this->DESK->Database->Field("auth").",";
135: 			$q.=$this->DESK->Database->Field("username").",";
136: 			$q.=$this->DESK->Database->Field("password").",";
137: 			$q.=$this->DESK->Database->Field("smtpsec").") ";
138: 			$q.="VALUES(";
139: 			$q.=$this->DESK->Database->SafeQuote($name).",";
140: 			$q.=$this->DESK->Database->SafeQuote($host).",";
141: 			$q.=$this->DESK->Database->SafeQuote($from).",";
142: 			$q.=$this->DESK->Database->SafeQuote($fromName).",";
143: 			$q.=$this->DESK->Database->Safe($wordwrap).",";
144: 			$q.=$this->DESK->Database->Safe($auth).",";
145: 			$q.=$this->DESK->Database->SafeQuote($username).",";
146: 			$q.=$this->DESK->Database->SafeQuote($password).",";
147: 			$q.=$this->DESK->Database->SafeQuote($smtpsec).")";
148: 			
149: 			$this->DESK->Database->Query($q);
150: 		}
151: 		else
152: 		{
153: 			$q="UPDATE ".$this->DESK->Database->Table("email")." SET ";
154: 			$q.=$this->DESK->Database->Field("name")."=".$this->DESK->Database->SafeQuote($name).",";
155: 			$q.=$this->DESK->Database->Field("host")."=".$this->DESK->Database->SafeQuote($host).",";
156: 			$q.=$this->DESK->Database->Field("from")."=".$this->DESK->Database->SafeQuote($from).",";
157: 			$q.=$this->DESK->Database->Field("fromname")."=".$this->DESK->Database->SafeQuote($fromName).",";
158: 			$q.=$this->DESK->Database->Field("wordwrap")."=".$this->DESK->Database->Safe($wordwrap).",";
159: 			$q.=$this->DESK->Database->Field("auth")."=".$this->DESK->Database->Safe($auth).",";
160: 			$q.=$this->DESK->Database->Field("username")."=".$this->DESK->Database->SafeQuote($username).",";
161: 			$q.=$this->DESK->Database->Field("password")."=".$this->DESK->Database->SafeQuote($password).",";
162: 			$q.=$this->DESK->Database->Field("smtpsec")."=".$this->DESK->Database->SafeQuote($smtpsec)." ";
163: 			$q.="WHERE ";
164: 			$q.=$this->DESK->Database->Field("accountid")."=".$this->DESK->Database->Safe($id);
165: 			
166: 			$this->DESK->Database->Query($q);
167: 		}
168: 	}
169: 	
170: 	/**
171: 	 * Delete Email account
172: 	 * @param int $id Account ID
173: 	**/
174: 	function DeleteAccount($id)
175: 	{
176: 		$q="DELETE FROM ".$this->DESK->Database->Table("email")." WHERE ".$this->DESK->Database->Field("accountid")."=".$this->DESK->Database->Safe($id);
177: 		$this->DESK->Database->Query($q);
178: 	}
179: 	
180: 	/**
181: 	 * Send an email
182: 	 * @param int $id Account ID
183: 	 * @param string $to To Address
184: 	 * @param string $subject Subject
185: 	 * @param string $body Body
186: 	 * @return bool Indicates successfully sent
187: 	**/
188: 	function Send($id, $to, $subject, $body)
189: 	{
190: 		if (!$this->loaded)
191: 			$this->LoadAccounts();
192: 			
193: 		if (!isset($this->accounts[$id]))
194: 			return false;
195: 		$acc = $this->accounts[$id];
196: 		
197: 		$mail = new PHPMailer();
198: 		
199: 		//$mail->SMTPDebug=2;
200: 		
201: 		$mail->Subject = $subject;
202: 		$mail->Body = $body;
203: 		$mail->AddAddress($to);
204: 		$mail->SetFrom($acc['from'], $acc['fromname']);
205: 		$mail->WordWrap = $acc['wordwrap'];
206: 		
207: 		if ($acc['host'] != "")
208: 		{
209: 			$mail->IsSMTP();
210: 			if (strpos($acc['host'],":") !== false)
211: 			{
212: 				$parts = explode(":", $acc['host']);
213: 				$mail->Host=$parts[0];
214: 				$mail->Port=$parts[1];
215: 			}
216: 			else
217: 				$mail->Host=$acc['host'];
218: 			if ($acc['auth'] == 1)
219: 			{
220: 				$mail->SMTPAuth = true;
221: 				$mail->Username = $acc['username'];
222: 				$mail->Password = $acc['password'];
223: 			}
224: 			if ($acc['smtpsec'] != "")
225: 			{
226: 				$mail->SMTPSecure = $acc['smtpsec'];
227: 			}
228: 		}
229: 		
230: 		//print_r($mail);
231: 		
232: 		if (!$mail->Send()) // Failed
233: 		{
234: 			$this->DESK->LoggingEngine->Log("Email Failed to ".$to, "Email", "Fail", 2);
235: 			$this->DESK->LoggingEngine->Log("phpMailer: ".$mail->ErrorInfo,"Email","Error", 2);
236: 			return false;
237: 		}
238: 		else
239: 		{
240: 			$this->DESK->LoggingEngine->Log("Email Sent to ".$to, "Email", "Send", 8);
241: 			return true;
242: 		}
243: 	}
244: 	
245: 	/**
246: 	 * Load Templates
247: 	 * @param bool $force Force reload (optional, default false)
248: 	**/
249: 	private function LoadTemplates($force=false)
250: 	{
251: 		if (!$this->loadedTemplates || $force)
252: 		{
253: 			$this->templates = array();
254: 			$q="SELECT * FROM ".$this->DESK->Database->Table("templates");
255: 			$r=$this->DESK->Database->Query($q);
256: 			while ($row=$this->DESK->Database->FetchAssoc($r))
257: 			{
258: 				$this->templates[$row['templateid']] = $row;
259: 			}
260: 			$this->loadedTemplates = true;
261: 		}
262: 	}
263: 	
264: 	/**
265: 	 * Save (or create) a template
266: 	 * @param string $id Template ID
267: 	 * @param string $subject Subject
268: 	 * @param string $body Body
269: 	**/
270: 	function SaveTemplate($id, $subject, $body)
271: 	{
272: 		$this->LoadTemplates();
273: 		// Check if it exists
274: 		if (isset($this->templates[$id]))
275: 		{
276: 			$q="UPDATE ".$this->DESK->Database->Table("templates")." SET ";
277: 			$q.=$this->DESK->Database->Field("subject")."=".$this->DESK->Database->SafeQuote($subject).",";
278: 			$q.=$this->DESK->Database->Field("body")."=".$this->DESK->Database->SafeQuote($body)." ";
279: 			$q.="WHERE ".$this->DESK->Database->Field("templateid")."=".$this->DESK->Database->SafeQuote($id);
280: 			$this->DESK->Database->Query($q);
281: 		}
282: 		else
283: 		{
284: 			$q="INSERT INTO ".$this->DESK->Database->Table("templates")."(";
285: 			$q.=$this->DESK->Database->Field("templateid").",";
286: 			$q.=$this->DESK->Database->Field("subject").",";
287: 			$q.=$this->DESK->Database->Field("body").")";
288: 			$q.=" VALUES(";
289: 			$q.=$this->DESK->Database->SafeQuote($id).",";
290: 			$q.=$this->DESK->Database->SafeQuote($subject).",";
291: 			$q.=$this->DESK->Database->SafeQuote($body).")";
292: 			$this->DESK->Database->Query($q);
293: 		}
294: 		$this->LoadTemplates(true);
295: 	}
296: 	
297: 	/**
298: 	 * Get a template by ID
299: 	 * @param string $id ID
300: 	 * @return mixed String of template on success or bool false on failure
301: 	**/
302: 	function GetTemplate($id)
303: 	{
304: 		$this->LoadTemplates();
305: 		
306: 		if (isset($this->templates[$id]))
307: 			return $this->templates[$id];
308: 		return false;
309: 	}
310: 	
311: 	/**
312: 	 * Get a substituted template by ID
313: 	 * @param string $id ID
314: 	 * @param array $data Data array (macro => value)
315: 	 * @return mixed String of sub'd template on success or bool false on failure
316: 	**/
317: 	function GetSubTemplate($id, $data)
318: 	{
319: 		$temp = $this->GetTemplate($id);
320: 		if ($temp === false)
321: 			return false;
322: 		foreach($data as $key => $val)
323: 		{
324: 			$macro = "%".$key."%";
325: 			$temp['subject'] = str_replace($macro, $val, $temp['subject']);
326: 			$temp['body'] = str_replace($macro, $val, $temp['body']);
327: 		}
328: 		return $temp;
329: 	}
330: }
331: ?>
332: