Asterisk Call Spying

Hi Gurus !
I am looking for a solution here and i guess its a challenging job to do. Ive been tryin to make up a spy service and m stuck somwhere. Can anyone of u guide me to the right path …

Scenario :
When party1 dial a number … the number is searched in the database… if the spy service is on, on the number then the call is recorded and if also there is a spy to destination number given then a specified number is called automatically and a 3 peer conference is created where party1 and party2 are talking and spy3 is listening to their call.

Can anyone do the spying part ??

[code]#!/usr/bin/php -q

<?php ob_implicit_flush(true); set_time_limit(5); require ('phpagi.php'); require_once('phpagi-asmanager.php'); $agi = new AGI(); $callernum=$agi->get_variable("CALLERID(number)"); $callernum=$callernum['data']; $agi->verbose($callernum); $agi->exec('Set','${CALLERID}=$CALLERID(number)-${time()}'); $con=mysql_connect("localhost","root",""); $db=mysql_select_db("asterisk",$con); $sql="select DISTINCT src,status,destination from cdr left join spy on spy.number=cdr.src WHERE status='on'and src='$callernum' and startdate <= now() and enddate >= now()"; $result=mysql_query($sql); if(mysql_num_rows($result)>0) { $agi->exec('monitor','wav||m'); $agi->exec('ChannelRedirect','100'|'100'|'${200}'|'1'); $destination=mysql_result($result,0,'destination'); if($destination!=''){ $agi->exec('Dial',"SIP/100"); } } else {echo "Spy Service is OFF on this Number";} ?>[/code]