Skip to content

Instantly share code, notes, and snippets.

@fastcodecoq
Last active December 23, 2015 15:29
Show Gist options
  • Select an option

  • Save fastcodecoq/6655646 to your computer and use it in GitHub Desktop.

Select an option

Save fastcodecoq/6655646 to your computer and use it in GitHub Desktop.
Simple class to mannage SMS sender [ Webservive ]
<?php
//include(dirname(__FILE__) . "path/to/storageEngine.php"); crea una clase para salvar cosas en base de datos, y así guardar registro de los sms
class sms
{
private $smsList;
private $storageEngine;
public function __construct($recipient = NULL, $content = NULL, $storageEngine = true) //cambia a false si no quieres salvar en DB
{
$this->smsList = array();
if($recipient != NULL && $content != NULL)
$this->create($recipient, $content);
}
public function create($recipient, $content) // create a new SMS
{
$sms = array(
"recipient" => $recipient,
"content" => $this->doHash($content),
"type" => "plain/text"
);
if(! array_push($this->smsList, $sms))
throw new smsException("Error creating new sms");
return true;
}
private function find($recipient){ // Find a SMS by recipiente number return array with index matches
$i = 0;
$matches = array();
for(; $i < count($this->smsList) ; $i++)
if( $recipient === $this->smsList[$i]->recipient)
$matches[] = $i;
if(count($matches) > 0)
return $matches;
else
return false;
}
public function copy($recipient){
if( $index = $this->find($recipient))
if(array_push($this->smsList, $this->smsList[$i]))
return true;
else
throw new smsException("Can't copy SMS, unkno error line:51");
else
throw new smsException("Can't copy SMS, recipient not found");
}
protected function doHash($content){
//coding message, this only will decode went the message is sended to recipient (execution time)
$bytes = strlen($content);
$chunks = $bytes/3;
$part1 = substr($content, 0, $chunks);
$part2 = substr($content, $chunks, $chunks * 2);
$part3 = substr($content, ($chunks*2) + 1, $chunks * 3);
$salt = hash("sha1", md5(time()));
$content = base64_encode("{$part1}:{$salt}:{$part2}:{$salt}:{$part3}");
return $content;
}
public function setContent($recipient, $content)
{
if( $index = $this->find($recipient))
if($this->smsList[$index[0]]["content"] === $content)
throw new smsException("Can't set duplicate content");
else{
$this->smsList[$i]["content"] = $this->doHash($content);
return true;
}
else
return false;
}
public function sendOne($recipient){
if( $index = $this->find($recipient))
$this->send($this->smsList[$index[0]]);
else
throw new smsException("Can't send SMS, recipient not found");
}
public function sendAll(){
foreach ($this->smsList as $sms)
$this->send($sms);
}
private function send( $sms ){ //recive an array { "recipient" => ?, "content" => ? }
//write a code to send SMS
}
}
$sms = new sms;
$sms->create("+57301707070", "Hola como vas");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment