#!/usr/local/bin/php -q value pair) for all of the data returned if (sizeof($result) > 0) { $num_fields = mysql_num_fields($result); $row_cnt = 0; while ($row_data = mysql_fetch_array($result)) { for ($cnt = 0; $cnt < $num_fields; $cnt++) { $field_name = mysql_field_name($result, $cnt); $data[$row_cnt][$field_name] = $row_data[$cnt]; } $row_cnt++; } } return $data; } /* Table structure id - auto increment int phone - string amt - int chapter - int What I want to happen: 1. Person calls. Plays greeting. Caller ID blocked? Then prompt for number and store as $callerid variable. Caller ID not blocked? Then pass it into $callerid variable. 2. Check to see if caller's number already exists in table. Number not in table? Insert number and amt. Pass amt to variable $balance. Number in table? Get amt associated with number and pass it to $balance. 3. Announce number person is calling from. Announce balance. 4. Play first conversation. Increase chapter in table by 1. Subtract a "first minute connection charge" from $balance. 5. Plays second, third, etc conversations. For each conversation played chapter increases by 1. 6. After last conversation, chapter gets reset to 0. Caller is prompted to leave a recorded response to the whole experience. 7. Caller's response gets posted to a web site. 8. Use phone number to manage recorded responses on web site. (phone# + SMS code to authenticate?) ...... Need to figure out... 1. How do I decrease $balance for each minute connected? Possible solution: time the chapters and interstitial music, then after each is played deduct that amount from $balance. Messy, but it will work for the final. */ // Create an AGI Object $agi = new AGI(); // Database connection variables $hostname = "itp.nyu.edu"; $dbname = "mcnert01"; $username = "mcnert01"; $password = "2zX0vZ5"; // Connect to the database $mySql = mysql_connect($hostname, $username, $password) or die (mysql_error()); mysql_select_db($dbname, $mySql) or die(mysql_error()); // Caller ID check: // Get caller ID, pass it to variable $callerid $callerid = $agi->request["agi_callerid"]; if (!is_numeric($callerid)) { // Prompt to enter phone number, store into $callerid. $entered = $agi->get_data("/home/mcnert01/asterisk_sounds/ccs/pleaseenter", "10000", "10"); $callerid = $entered['result']; $agi->stream_file("/home/mcnert01/asterisk_sounds/ccs/youhaveentered"); $agi->say_digits($callerid); } else { // Read out caller ID digits $agi->stream_file("/home/mcnert01/asterisk_sounds/ccs/yourphoneis"); $agi->say_digits($callerid); } // Check to see if $callerid is already in DB. If it is, then get the balance and chapter location. // If not, then insert and set $balance to 20 and $chapnum to 0. $query = "SELECT * FROM redial_callingcard WHERE phone = '$callerid'"; $result = sqlQuery($query,$mySql); // Returns an array with the table results. if (sizeof($result) <= 0) { // Inserts caller ID into database and sets AMT to 10, CHAPTER to 1 $query = "INSERT INTO redial_callingcard VALUES ('','$callerid','20','1')"; $result = sqlQuery($query,$mySql); $balance = 20; $chapnum = 1; } else { $balance = $result[0]['amt']; $chapnum = $result[0]['chapter']; } /* Enter and store PIN, 11 digits max. PIN function doesn't do anything as of this version, just part of the interaction. IDEAS FOR FUTURE: - Generate random PINs, print them on cards. Then when caller enters pin, it will be unique and added to the database. No need for phone numbers this way. When unique pin gets added, it can get credited with minutes and associated with chapter location. This would work better with the "web version" of the calling card. It's easier to generate a random PIN and display it on the web site. */ $enterpin = $agi->get_data("/home/mcnert01/asterisk_sounds/ccs/enterpin", "10000", "9"); $pin = $enterpin['result']; // Announce available credit $agi->stream_file("/home/mcnert01/asterisk_sounds/ccs/availablecredit"); $agi->say_number($balance); $agi->stream_file("/home/mcnert01/asterisk_sounds/ccs/minutes"); // If credit lower than connection fee + first minute charge, // Announce insufficient credit, refill credit in DB, // Then disconnect. if ($balance < 4) { $agi->stream_file("/home/mcnert01/asterisk_sounds/ccs/insufficient"); $query = "UPDATE redial_callingcard SET amt = 20 WHERE phone = '$callerid'"; $result = sqlQuery($query,$mySql); $agi->hangup(); } else { // Else charge the connection fee. Update the database. $balance = $balance - 2; $query = "UPDATE redial_callingcard SET amt = '$balance' WHERE phone = '$callerid'"; $result = sqlQuery($query,$mySql); $agi->stream_file("/home/mcnert01/asterisk_sounds/ccs/onemoment"); } // Chapter looping. Chapter $balance get charged before they are played. Chapter $chapnum increments after being played. // This way you will be charged for a whole chapter if you disconnect mid-chapter, however you have to hear the whole chapter // before the database will update with your new chapter location. for($i = $chapnum; $i < 5; $i++) { if ($balance > 1) { $balance = $balance - 2; $agi->exec("Playback /home/mcnert01/asterisk_sounds/ccs/chapter0$chapnum"); // stream_file DOES NOT WORK FOR PLAYING LONG AUDIO FILES!!! // $agi->stream_file("/home/mcnert01/asterisk_sounds/ccs/chapter0$chapnum"); // For DEBUGGING: Read out the balance and chapter number after playing each chapter. // $agi->say_number($balance); // $agi->say_number($chapnum); $chapnum++; $query = "UPDATE redial_callingcard SET amt = '$balance', chapter = '$chapnum' WHERE phone = '$callerid'"; $result = sqlQuery($query,$mySql); } else { $agi->stream_file("/home/mcnert01/asterisk_sounds/ccs/insufficient"); $query = "UPDATE redial_callingcard SET amt = 20 WHERE phone = '$callerid'"; $result = sqlQuery($query,$mySql); $agi->hangup(); } } // $agi->stream_file("/home/mcnert01/asterisk_sounds/ccs/closing"); $query = "DELETE FROM redial_callingcard WHERE phone = '$callerid' LIMIT 1"; $result = sqlQuery($query,$mySql); ?>