class PrepaidCards { var $whereResellerFilter= " (1=1) "; function PrepaidCards () { global $auth; global $CDRTool; $this->loginname = $auth->auth["uname"]; $this->CDRTool = $CDRTool; $this->db = new DB_CDRTool; $this->db1 = new DB_CDRTool; if ($this->CDRTool['filter']['reseller']) { $this->whereResellerFilter = sprintf ("reseller_id = %d",$this->CDRTool['filter']['reseller']); } } function showGenerateForm() { $this->PHP_SELF = $_SERVER['PHP_SELF']; print "
Error: No digits specified!"; return 0; } $card_len=$digits; if (!$nr_cards) $nr_cards = 10; if (!$value) $value = 10; $alf=array("0","1","2","3","4","5","6","7","8","9"); print "
"; printf ("Request generation of %d cards or %d digits in value of %s",$nr_cards,$card_len,$value); $now = date("Y-m-d_H:i:s"); if (!$batch) { $batch_name=$now."-cards_".$nr_cards."-value_".$value; } else { $batch_name=$now."-".$batch."-cards_".$nr_cards."-value_".$value; } $generated = 0; $failed = 0; $j = 0; $random_len = $card_len-strlen($start); $initial_len = strlen($start); while ($generated < $nr_cards) { $j++; if ($j > 5 * $nr_cards) { print "Error: could not generate $nr_cards unique random cards"; break; } $card=$start; $i=0; while($i < $random_len) { srand((double)microtime()*1000000); if ($i==0) { $randval = rand(1,9); } else { $randval = rand(0,9); } $card=$card.$alf[$randval]; $i++; } $query=sprintf("insert into prepaid_cards (number,value,date_batch,batch,service,reseller_id) values ('%s','%s',NOW(),'%s','sip',%d)", addslashes($card), addslashes($value), addslashes($batch_name), $this->CDRTool['filter']['reseller'] ); dprint($query); if ($this->db->query($query) && $this->db->affected_rows()) { $generated++; } else { $failed++; } } if ($generated) { print "
Generated $generated cards"; $log_query=sprintf("insert into log (date,login,ip,datasource,results,description) values (NOW(),'%s','%s','Prepaid generator','%d','Batch %s created')", addslashes($this->loginname), addslashes($_SERVER['REMOTE_ADDR']), addslashes($generated), addslashes($batch_name) ); dprint($log_query); $this->db->query($log_query); } } function export($batch) { if (!$batch) return false; $available = $_REQUEST['available']; $query = sprintf("select * from prepaid_cards where batch = '%s' and %s",addslashes($batch),$this->whereResellerFilter); if ($available == "yes") { $query .= " and value > 0"; $query .= " order by id ASC "; } elseif ($available == "no") { $query .= " and value = 0"; $query .= " order by date_active DESC "; } dprint($query); print "Id,Number,Batch,Value\n"; $this->db->query($query); $rows= $this->db->num_rows(); while ($this->db->next_record()) { $i++; $id = $this->db->f('id'); $batch = $this->db->f('batch'); $number = $this->db->f('number'); $value = $this->db->f('value'); $date_active = $this->db->f('date_active'); print "$id,$number,$batch,$value\n"; } $log_query=sprintf("insert into log (date,login,ip,datasource,results,description) values (NOW(),'%s','%s','Prepaid generator','%d','Batch %s exported')", addslashes($this->loginname), addslashes($_SERVER['REMOTE_ADDR']), addslashes($rows), addslashes($batch) ); dprint($log_query); $this->db->query($log_query); } function deleteBatch($batch, $confirm) { if (!$batch) return false; if (!$confirm) { $batch_enc=urlencode($batch); print "
PHP_SELF?batch=$batch_enc&action=delete&confirm=1>Confirm deletion of batch $batch
"; return; } $query=sprintf("delete from prepaid_cards where batch = '%s' and %s",addslashes($batch),$this->whereResellerFilter); $this->db->query($query); if ($this->db->affected_rows()) { $log_query=sprintf("insert into log (date,login,ip,datasource,results,description) values (NOW(),'%s','%s','Prepaid generator','%d','Batch %s deleted')", addslashes($this->loginname), addslashes($_SERVER['REMOTE_ADDR']), addslashes($this->db->affected_rows()), addslashes($batch) ); dprint($log_query); $this->db->query($log_query); return true; } return false; } function blockBatch($batch) { if (!$batch) return false; $query=sprintf("update prepaid_cards set blocked = '1' where batch = '%s' and %s",addslashes($batch),$this->whereResellerFilter); $this->db->query($query); } function deblockBatch($batch) { if (!$batch) return false; $query=sprintf("update prepaid_cards set blocked = '0' where batch = '%s' and %s",addslashes($batch),$this->whereResellerFilter); $this->db->query($query); } function showBatches () { $query=sprintf("select count(*) as c,batch,reseller_id,date_batch from prepaid_cards where %s group by batch order by date_batch DESC",$this->whereResellerFilter); dprint($query); $this->db->query($query); print "
Reseller | Cards | Date | Batch | Download | Actions | ||
$reseller_id | $c | $date | $batch | PHP_SELF?batch=$batch_enc&action=export target=_new>All cards | PHP_SELF?batch=$batch_enc&action=export&available=yes target=_new>Available cards ($unused) | PHP_SELF?batch=$batch_enc&action=export&available=no target=_new>Used cards ($used) | PHP_SELF?batch=$batch_enc&action=delete>Delete |