//Code is under active development.
//Feel free to use at your own risk.
//No copyright asserted.

<?php
$start
=microtime(); 

function 
valid_amt_units($amount,$units,$native_units) {
  if (
is_numeric($amount)) { return true;
    
/*if ($units == $native_units OR $units == $native_units ."s" OR $units == substr($native_units,0,-1)) { echo "valid amt_units";
      return true; 
    }
    else { echo "invalid amt units";
      return false;
    } */
  
}
  else {return 
false;}
}

function 
valid_tally($publ_amt,$audit_amt) {
  if (!
is_numeric($publ_amt) OR $publ_amt!=$audit_amt) {
    return 
"<font color=\"red\">"number_format($audit_amt,2) ."</font>";
  }
  else { 
    return 
"Calculated = "number_format($audit_amt,2);
  }


$create=0;
$revert=0;
$inflow=0;
$outflow=0;
$cr_bal=0;
$db_bal=0;
$net_bal=0;
$start_db=0;
$start_cr=0;
$record_list=array();

if (isset(
$_POST['src_content'])) {
  
$src_content $_POST['src_content'];
}
else if (
$url) {
  
$src_content=file_get_contents($url);
}

//echo $content ."<br /><br />";
$content=str_replace(array("<br />","<br>","<p>","\r"),"\n",$src_content);

//$content = strip_tags($content,"<a>"); //ok to leave anchor tags
//echo $content ."<br /><br />";

$content explode("Prowl/version ",$content);

if (
count($content)==1) { //echo "content: "; print_r($content);
  
echo "File is not formatted as a Prowl record.";
}
else {
  
$content explode("\n",$content[1]); 
  
$content array_filter($content);
  
$version_no current($content);
  list(
$model_indicator,$model_name) = explode(": ",next($content));
  list(
$main_icb,$native_units,$report_word,$for_word,$year,$status) = explode(" ",next($content));
  
$year rtrim($year,":"); //echo "model: ". $model_name;
  
  
if ($query_icb) { 
    if (
$query_icb!=$main_icb OR $query_year!=$year OR $query_units!=$native_units) {
      echo 
"The published Prowl report did not match the query parameters of: "$query_icb .", "$query_year .", "$query_units .".";
    }
  }
  
  
$count count($content);

  for (
$i=1;$i<$count;$i++) { //the intended combination of current and next precludes the use of foreach instead 
    
if (strpos(current($content),$year)!==false) { //only lines with the year are considered
      
$temp_content[] = trim(current($content));    
    }
    
    
next($content);
  }

  
//print_r($temp_content);
  
  
$content $temp_content;//print_r($content);
  
  
$i=0//to prevent infinite loop
  
$record current($content);
  
?>
<h4><a href="" onclick="toggle_disp('src_content'); toggle_disp('form_note'); return false;">Click here to View and Modify Audited Data</a></h4>
<table align="center">
<tr align="center">
<td>
<p id="form_note" style="display: none; text-align: center;">See what happens when period and amounts are changed, "ocaup" is changed to "cc", etc. </p><br /> 
<form method="POST" action="" align="center" id="src_content" style="display: none;" >
<textarea name="src_content" rows="20" cols="100">
<?php echo $src_content?>
</textarea><br />
<input type="submit" name="submit" value="Submit">
</form>
</td>
</tr>
</table>
  <table border="1" cellpadding="10" align="center">
  
  <?php 
  
while ($record!=false AND $i<2000) {
    
//echo $word ."<br />";
    //list($record,$bal_line)=explode($record,"</a>");
    
$record explode(" ",$record);
    
$word=current($record);
                    
    if (
substr($word,0,4) == $year) {
      
$date $word//echo "date=". $date ."<br />";
      
$keyword next($record); 
      
      if (
$keyword=="reverse") {
        
$sign = -1;
        
$rev_phrase $date ." reverse ";
        
$date next($record);
        
$keyword next($record);
      } else {
        
$sign 1;
      }
      
      if (
$keyword=="from") {      
        
$from_icb=next($record);
        
$to_wordnext($record);
               
        if (
$to_word=="to") {
          
$to_icb=next($record);
          
$amount=$sign*next($record);        
          
$units next($record);
          
          if (
valid_amt_units($amount,$units,$native_units)) {
            if (
substr($units,-1)!=".") {
              
$id next($record);
            }
            
            
$record_copy $date ." from "$from_icb ." to "$to_icb ." "number_format($sign*$amount,2) ." "$units ." "$id;
            
$record_copy trim($rev_phrase $record_copy);
            
            if (!
in_array($record_copy,$record_list)) {    
              if (
$from_icb==$main_icb) { //txn was an outflow            
                
$cr_bal=$cr_bal-$amount;
                
$outflow $outflow+$amount;
                
$net_bal=$net_bal-$amount;            
              }
              else if (
$to_icb==$main_icb) { //txn was an inflow
                
$db_bal=$db_bal-$amount;  
                
$inflow $inflow+$amount;
                
$net_bal=$net_bal+$amount;
              }
              else {
                
$record_copy "<font color='red'>"$record_copy ."</font>";                
              }
            } 
          }
        }      
      }
      else if (
$keyword=="add" OR $keyword=="cut") { //this might have reporter also      
        
$amount$sign*next($record);
        
$units next($record);
        
$other_keyword next($record);
        
        if (
valid_amt_units($amount,$units,$native_units)) { //echo "<br />adjust,sign,keyword ". $adjust .", ". $sign .", ". $keyword;
          
if (next($record)==$main_icb) {
            
$param trim(next($record));
            if (
substr($param,-1)!=".") { 
              
$id " "next($record);
            }
            
            
$record_copy $date ." "$keyword ." "number_format($sign*$amount,2) ." "$units ." to "$main_icb ." "$param $id;
            
$record_copy trim($rev_phrase $record_copy);
                        
            if (!
in_array($record_copy,$record_list)) {            
              if (
strpos($param,"unusedBudget")!==false) {      
                if (
$keyword=="add" AND $other_keyword=="to") {
                  
$cr_bal=$cr_bal+$amount;
                  
$db_bal=$db_bal+$amount;   
                  
$create $create+$amount;           
                } else if (
$other_keyword=="from") {
                  
$cr_bal=$cr_bal-$amount;
                  
$db_bal=$db_bal-$amount
                  
$revert $revert+$amount;
                } 
              }    
            }          
          }
        }      
      }    
      else if (
$keyword=="set") {
        include(
"audit_tally_3.php"); 
        if (!
$record_copy$record_copy trim(implode(" ",$record));
      }           
      else if (
$keyword=="tally") {
         
        
$param explode(".",next($record));
        
        if (
$param[0]=="unusedBudget") {
          
$sub_param $param[1];
          
$amount next($record);
          
$units next($record);
          
          if (
valid_amt_units($amount,$units,$native_units)) {
            switch (
$sub_param) {
              case 
"adds"$tally_amt valid_tally($amount,$create); break;
            
              case 
"cuts"$tally_amt valid_tally($amount,$revert); break;
                
              case 
"revenueBal"$tally_amt valid_tally($amount,$db_bal); break;
              
              case 
"expenseBal"$tally_amt valid_tally($amount,$cr_bal); break;          
            }
            
            
$record_copy $date ." tally "$param [0]."."$sub_param ." "number_format($amount,2) ." "$units
          }
        }       
        else {
          
$param $param[0];
          
$amount next($record);
          
$units next($record);
          
          if (
valid_amt_units($amount,$units,$native_units)) { //echo "<br />valid ". $amount ." ". $units;
            
switch ($param) {
              case 
"inflow"$tally_amt valid_tally($amount,$inflow); break;
                
              case 
"outflow"$tally_amt valid_tally($amount,$outflow); break;
                
              case 
"netBalance"$tally_amt valid_tally($amount,$net_bal); break;
            }
            
            
$record_copy $date ." tally "$param ." "number_format($amount,2) ." "$units;
          }
        }
      }
      
//echo "<br />units,amount,record_copy=". $units .", ". $amount .", ". $param .", ". $sub_param .", ". $record_copy ."end<br />";    
  
      
if ($record_copy) {
        
$record_copy trim($rev_phrase $record_copy);   
        if (!
in_array($record_copy,$record_list)) {
          
$record_list[] = $record_copy;
                   
          
$notary=next($record); //echo "<br />href= ". $notary;          
          
if (substr($notary,0,1)=="[" AND substr($notary,-1)=="]") {            
            
$notary_copy $notary;
            
$notary explode(",",trim($notary,"[..]"));                       
            
$timeout stream_context_create(array('http' => array('timeout'=>1)));
                                      
            while (
current($notary)) {
              
$get_url "http://"current($notary) ."/?prowl=get+"urlencode("~"substr($record_copy,11));          
              
$link_contents = @file_get_contents($get_url);          
              
              if (!
$link_contents) {
                
$link_note $link_note ." <a href=\""$get_url ."\">link not found</a> |";
              }
              else if (
strpos($link_contents,$record_copy)===false) {
                
$link_note $link_note ." <a href=\""$get_url ."\">record not found</a> |";
              }
              else {
                
$link_note $link_note ." <a href=\""$get_url ."\">notarized</a> |";
              }
                    
              
next($notary);
            }
            
            
$record_copy trim($record_copy ." "$notary_copy);
          }
          else {
            
$link_note="No links";
          }
        }
        else {
          
$addl_mssg " <font color='red'>(Duplicate record)</font> ";
        }            
                       
        if (!
$notary) {
          
$notary "no links";
        }      
              
        if (
$keyword=="tally") { 
          
?>
          <tr>
            <td width="600"><?php echo $record_copy ?></td>
            <td width="200"><?php echo $tally_amt $addl_mssg ?></td>
          </tr>
          <?php
        
}
        else if (
$keyword=="set") { 
          
?>
          <tr>
            <td width="600"><?php echo $record_copy ?></td>
            <td width="200"><?php echo $set_verify $addl_mssg /*."<br />". $db_bal ." +". $cr_bal*/ ?></td>
          </tr>
          <?php
        
}      
        else {
          
?>
          <tr>
            <td width="600"><?php echo $record_copy ?></td>
            <td width="200">
            <?php echo $link_note $addl_mssg ?></td>      
          </tr>
          <?php 
        
}                     
      }
    } 
     
    
$record next($content);
    
$i++;
    
$rev_phrase "";
    
$keyword"";
    
$opp_icb "";
    
$notary "";  
    
$date "";
    
$from_icb "";
    
$to_icb "";
    
$amount "";
    
$units "";  
    
$id ="";
    
$neg_bal "";
    
$pos_bal "";  
    
$addl_link"";
    
$url_list="";
    
$record_copy="";
    
$tally_amt "";
    
$set_verify "";  
    
$link_contents="";
    
$link_note="";
    
$addl_mssg ""
  }
}

if (
trim($status)=="Pending") {
  if (
trim($model_name)=="ocaup") {
    
?>
    <tr align="center">
      <td colspan="2">
        <?php
        
echo "
        <b>unusedBudget.revenueBal</b> = {set} + {adds} - {cuts} - {inflow}<br /><b>"
.         
        
number_format($db_bal,2) ."</b> "$native_units ." = {".  number_format($start_db,2) ."} + {"number_format($create,2) ."} - {"number_format($revert,2) ."} - {"number_format($inflow,2) ."}<br /><br />
        <b>unusedBudget.expenseBal</b> = {set} + {adds} - {cuts} - {outflow}<br /><b>"
.         
        
number_format($cr_bal,2) ."</b> "$native_units ." = {".  number_format($start_cr,2) ."} + {"number_format($create,2) ."} - {"number_format($revert,2) ."} - {"number_format($outflow,2) ."}<br />";        
                
        
?>      
      </td>
    </tr>
    <?php
  
}
  else if (
trim($model_name)=="cc") {
    
?>
    <tr align="center">
      <td colspan="2">
        <?php
        
echo "<b>netBalance</b> = {set} + {inflow} - {outflow} <br /><b>".         
        
number_format($net_bal,2) ."</b> "$native_units ." = {".  number_format($start_net_bal,2) ."} + {"number_format($inflow,2) ."} - {"number_format($outflow,2) ."}<br /><br />";        
        
?>      
      </td>
    </tr>
    
    
    <?php
  
}
}

?>

</table>

<br /><br />


<?php 
$stop 
microtime(); 
//echo "total time: ". ($stop-$start) ."s"; ?>
1