set_time_limit(0); $lockFile=sprintf("/var/lock/CDRTool_import_rates.lock"); $abort_text="Another import operation is in progress. Try again later.\n"; $f=fopen($lockFile,"w"); if (flock($f, LOCK_EX + LOCK_NB, $w)) { if ($w) { print $abort_text; syslog(LOG_NOTICE,$abort_text); exit(2); } } else { print $abort_text; syslog(LOG_NOTICE,$abort_text); exit(1); } $RatingTables= new RatingTables(); $RatingTables->ImportCSVFiles(); if ($RatingTables->mustReload) { if (!reloadRatingEngineTables()) { print "Error: cannot connect to network rating engine\n"; } } ?>
function showTable() { $PHP_SELF=$_SERVER['PHP_SELF']; foreach ($this->web_elements as $_el) { ${$_el}= $_REQUEST[$_el]; } if ($this->table == 'prepaid_cards') { print "<p> <a href=prepaid_cards.phtml>Prepaid card generator</a>"; } // Init table structure if (!is_array($this->tables[$this->table]['exceptions'])) $this->tables[$this->table]['exceptions']=array(); if (!is_array($this->tables[$this->table]['keys'])) $this->tables[$this->table]['keys']=array(); if (!is_array($this->tables[$this->table]['fields'])) $this->tables[$this->table]['fields']=array(); if ($this->table=='prepaid' && strlen($_REQUEST['search_session_counter'])) { $this->readonly=true; } if ($this->readonly) { $this->tables[$this->table]['readonly']=1; } $metadata = $this->db->metadata($this->table); $cc = count($metadata); // end init table structure // delimiter for exporting records if ($this->settings['csv_delimiter']) { $delimiter=$this->settings['csv_delimiter']; } else { $delimiter=","; } $query=sprintf("select count(*) as c from %s where %s", addslashes($this->table), $this->whereResellerFilter); $t=0; $j=0; while ($j < $cc ) { $Fname=$metadata[$j]['name']; $size=$metadata[$j]['len']; $class=$metadata[$j]['class']; if (!in_array($Fname,$this->tables[$this->table]['exceptions'])) { $f_name="search_".$Fname; $value=$_REQUEST[$f_name]; if (preg_match("/^([<|>]+)(.*)$/",$value,$likes)) { $like=$likes[1]; $likewhat=$likes[2]; $quotes=""; } else { $like="like"; $likewhat=$value; $quotes="'"; } if (strlen($value)) { $where.=sprintf(" and %s %s %s%s%s ", addslashes($Fname),$like, $quotes, addslashes($likewhat),$quotes); $t++; } } $j++; } $query .= $where; $this->db->query($query); $this->db->next_record(); $rows=$this->db->Record['c']; if (!$export) { print " <table border=0 align=center> <tr><td colspan=\"2\"> "; if ($rows == 0) { print "No records found. "; } else { print "$selectie $rows records found. "; } if ($this->settings['socketIPforClients'] && $this->settings['socketPort']) { $engineAddress=$this->settings['socketIPforClients'].":".$this->settings['socketPort']; if ($this->checkRatingEngineConnection()) { print " | <span class=\"label label-success\">Rating engine running at $engineAddress</span>"; } else { print " | <span class=\"label label-important\">Cannot connect to rating engine $engineAddress</span>"; } } print " | <a href=doc/RATING.txt target=rating_help>Rating documentation</a>"; print " </td> </tr> "; if ($this->csv_import[$this->table]) { print "<td style='text-align: center; padding-top:5px'> <form class='form-inline' action=$PHP_SELF method='post' enctype='multipart/form-data'> <input type=hidden name=import value=1> "; printf (" <input type='hidden' name=table value=%s> <input type='hidden' name='MAX_FILE_SIZE' value=1024000> <div class='fileupload fileupload-new' style='display: inline-block; margin-bottom:0px' data-provides='fileupload'> <div class='input-append'> <div class='uneditable-input input-small'> <span class='fileupload-preview'></span> </div> <span class='btn btn-file'> <span class='fileupload-new'>Select file</span> <span class='fileupload-exists'>Change</span> <input type='file' name='%s'/></span> <a href='#' class='btn fileupload-exists' data-dismiss='fileupload'>Remove</a> <button type='submit' class='btn fileupload-exists' value=\"Import\"><i class='icon-upload'></i> Import</button> </div> </div> ",$this->table,$this->table ); print "</form><td style='padding-top:5px'> "; } else { print "<td style='padding-top:5px; text-align: center' colspan=\"2\">"; } print "<form class='form-inline' action=$PHP_SELF method=post target=export> <input type=hidden name=export value=1> "; $j=0; while ($j < $cc ) { $Fname=$metadata[$j]['name']; $size=$metadata[$j]['len']; if (!in_array($Fname,$this->tables[$this->table]['exceptions'])) { $SEARCH_NAME="search_".$Fname; $value=$_REQUEST[$SEARCH_NAME]; print "<input type=hidden name=search_$Fname value=\"$value\">"; } $j++; } if ($this->table!=='prepaid_cards' ) { printf (" <input type=hidden name=table value=%s> <button class=btn type=submit value=\"Export %s\"><i class=icon-file></i> Export %s</button> ",$this->table,$this->csv_export[$this->table],$this->csv_export[$this->table]); } if ($this->settings['socketIPforClients'] && $this->settings['socketPort']) { if ($ReloadRatingTables) { reloadRatingEngineTables(); } else { $this->db->query("select var_value from settings where var_name = 'reloadRating' and var_value='1'"); if ($this->db->num_rows()) { print "<a class='btn btn-danger' href=rating_tables.phtml?ReloadRatingTables=1&table=$this->table>Reload rating tables</a>"; } } } print "</form></td> </tr> </table> "; } else { $this->maxrowsperpage=10000000; } if (!$next) { $i=0; $next=0; } else { $i=intval($next); } $j=0; $z=0; if ($rows > $this->maxrowsperpage) { $maxrows=$this->maxrowsperpage+$next; if ($maxrows > $rows) { $maxrows=$rows; $prev_rows=$maxrows; } } else { $maxrows=$rows; } if (!$order && $this->tables[$this->table]['order']) { $order=sprintf(" order by %s ",addslashes($this->tables[$this->table]['order'])); } $query=sprintf("select * from %s where (1=1) %s and %s %s limit %d, %d", addslashes($this->table), $where, $this->whereResellerFilter, $order, intval($i), intval($this->maxrowsperpage) ); $this->db->query($query); $num_fields=$this->db->num_fields(); $k=0; if (!$export) { if ($this->table=='prepaid') { print " <table class='table-hover table table-condensed' id='rates_table' align=center width=100%> <thead> <tr> <th></th>"; } else { print " <table class='table-hover table table-condensed table-striped' id='rates_table' align=center width=100%> <thead> <tr> <th></th> "; } } while ($k < $cc) { $th=$metadata[$k]['name']; if (!in_array($th,$this->tables[$this->table]['exceptions']) ) { if ($this->tables[$this->table]['fields'][$th]['name']) { $th=$this->tables[$this->table]['fields'][$th]['name']; } else { $th=ucfirst($th); } if (!$export) { print "<th>$th</th>"; } else { if ($k) { printf ("%s%s",$delimiter,$th); } else { print "Ops"; } } $t_columns++; } $k++; } if ($export) { print "\n"; } if (!$export) { print " <th>Action</th> </tr>"; $t_columns=$t_columns+2; // SEARCH FORM print " <tr> <td colspan=$t_columns> Use _ to match one character and % to match any. Use > or < to find greater or smaller values.</td> </tr> "; // Search form print " <form class='form-inline' style='display:none' action=$PHP_SELF method=post name=rating> <input type=hidden name=web_task value=Search> <tr> <td> </td>"; $j=0; while ($j < $cc ) { $Fname=$metadata[$j]['name']; $size=$metadata[$j]['len']; if (!in_array($Fname,$this->tables[$this->table]['exceptions'])) { $SEARCH_NAME="search_".$Fname; $value=$_REQUEST[$SEARCH_NAME]; if ($value != "") { $selection_made=1; } $maxlength=$size; if ($this->tables[$this->table]['fields'][$Fname]['size']) { $field_size=$this->tables[$this->table]['fields'][$Fname]['size']; } else { $field_size=$el_size; } $class=$this->tables[$this->table]['fields'][$Fname]['class']; if (!in_array($Fname,$this->tables[$this->table]['keys']) ) { if (!$class) { $class="span1"; } print "<td><input class=$class type=text size=$field_size maxlength=$maxlength name=search_$Fname value=\"$value\"></td>"; } else { print "<td></td>"; } } $j++; } printf(" <script type=\"text/JavaScript\"> function jumpMenu(){ location.href=\"%s?table=\" + document.rating.table.options[document.rating.table.selectedIndex].value; } </script>", $PHP_SELF ); print " <td> "; printf("<div class='input-append'><select class='span3' name='table' onChange=\"jumpMenu('this.form')\">\n"); $selected_table[$this->table]="selected"; foreach (array_keys($this->tables) as $tb) { $sel_name=$this->tables[$tb]['name']; print "<option value=$tb $selected_table[$tb]>$sel_name"; } print " </select><input class='btn btn-primary' type=submit name=subweb_task value=Search></div> </form>"; print " </td> </tr></thead> "; //print " //<tr> //<td colspan=$t_columns><hr noshade size=2></td> //</tr> //"; if ($selection_made && !$this->tables[$this->table]['readonly']) { // Update all form print " <tr><td colspan=$t_columns> Use + or - to add/substract from curent values. Use * or / to multiply/divide curent values.</td> </tr>"; $j=0; print " <form class='form-inline' action=$PHP_SELF method=post> <input type=hidden name=web_task value=update> <input type=hidden name=next value=$next> <tr> <td> </td>"; while ($j < $cc ) { $Fname=$metadata[$j]['name']; $size=$metadata[$j]['len']; if ($this->tables[$this->table]['fields'][$Fname]['size']) { $field_size=$this->tables[$this->table]['fields'][$Fname]['size']; } else { $field_size=$el_size; } $class=$this->tables[$this->table]['fields'][$Fname]['class']; if (!in_array($Fname,$this->tables[$this->table]['exceptions'])) { if (!in_array($Fname,$this->tables[$this->table]['keys']) ) { if (!$class) { $class="span1"; } print "<td><input class='$class' type=text size=$field_size maxlength=$size name=$Fname></td>"; } else { print "<td></td>"; } } $j++; } $j=0; while ($j < $cc ) { $Fname=$metadata[$j]['name']; $size=$metadata[$j]['len']; if (!in_array($Fname,$this->tables[$this->table]['exceptions'])) { $SEARCH_NAME="search_".$Fname; $value=$_REQUEST[$SEARCH_NAME]; print "<input type=hidden name=search_$Fname value=\"$value\">"; } $j++; } if ($subweb_task=="Delete selection" && !$confirmDelete) { print "<td>"; print "<input type=hidden name=confirmDelete value=1>"; print "<input class='btn btn-danger' type=submit name=subweb_task value=\"Delete selection\">"; print " ($rows records)"; } else if (!$this->tables[$this->table]['readonly']){ if ($this->table == "billing_rates" && strlen($_REQUEST['search_name'])) { if ($subweb_task=="Copy rate" && !$confirmCopy) { print "<td>"; print "<input type=hidden name=confirmCopy value=1>"; } else { print "<td>"; print "<div class=\"btn-group\"> <input class='btn' type=submit name=subweb_task value=\"Update selection\"> <input class='btn btn-danger' type=submit name=subweb_task value=\"Delete selection\"> </div> "; } print " <input type=submit name=subweb_task value=\"Copy rate\">"; printf (" id %s to",$_REQUEST['search_name']); $query=sprintf("select distinct(name) as name from billing_rates where name like '%s' order by name DESC limit 1",addslashes($_REQUEST['search_name'])); $this->db1->query($query); $this->db1->next_record(); $_rateName=$this->db1->f('name'); $_rateName=preg_replace("/%/","",$_rateName); if (preg_match("/^(.*)_(\d+)$/",$_rateName,$m)) { $_idx=$m[2]+1; $newRateName=$m[1]."_".$_idx; } else { $newRateName=$_rateName."_1"; } printf ("<input type=hidden name=fromRate value=\"%s\">",$_REQUEST['search_name']); $selected_newtable[$toRate]='selected'; printf ("<select name=toRate> <option value=\"%s\" %s>Rate id %s <option value=history %s>Rate history table </select>", $newRateName, $selected_newtable[$newRateName], $newRateName, $selected_newtable['history'] ); } else { print "<td>"; print "<div class=\"btn-group\"> <input class='btn' type=submit name=subweb_task value=\"Update selection\"> <input class='btn btn-danger' type=submit name=subweb_task value=\"Delete selection\"> </div>"; } } print " <td> <input type=hidden name=table value=$this->table> <input type=hidden name=search_text value=\"$search_text\"> </td> </tr></thead> </form> "; } else if (!$this->tables[$this->table]['readonly']){ // Insert form $j=0; print " <form style='display:none' action=$PHP_SELF method=post> <input type=hidden name=web_task value=update> <input type=hidden name=next value=$next> <tr> <td> </td> "; while ($j < $cc ) { $Fname=$metadata[$j]['name']; $size=$metadata[$j]['len']; if ($this->tables[$this->table]['fields'][$Fname]['size']) { $field_size=$this->tables[$this->table]['fields'][$Fname]['size']; } else { $field_size=$el_size; } $class=$this->tables[$this->table]['fields'][$Fname]['class']; if (!in_array($Fname,$this->tables[$this->table]['exceptions'])) { if (!in_array($Fname,$this->tables[$this->table]['keys']) ) { if (!$class){ $class='span1'; } print "<td><input class='$class' type=text size=$field_size maxlength=$size name=$Fname></td>"; } else { print "<td></td>"; } } $j++; } $j=0; while ($j < $cc ) { $Fname=$metadata[$j]['name']; $size=$metadata[$j]['len']; if (!in_array($Fname,$this->tables[$this->table]['exceptions'])) { $SEARCH_NAME="search_".$Fname; $value=$_REQUEST[$SEARCH_NAME]; print "<input type=hidden name=search_$Fname value=\"$value\">"; } $j++; } print " <td> <input type=hidden name=table value=\"$this->table\"> <input type=hidden name=search_text value=\"$search_text\"> <input class='btn btn-warning' type=submit name=subweb_task value=Insert> </td> </tr></thead> </form> "; //print " //<tr> //<td colspan=$t_columns><hr noshade size=2></td> //</tr> //"; } } while ($i<$maxrows) { $this->db->next_record(); $id = $this->db->f('id'); $status = $this->db->f('status'); $found = $i+1; if (!$export) { print " <form style='display:none' action=$PHP_SELF method=post> <input type=hidden name=web_task value=update> <input type=hidden name=next value=$next> <input type=hidden name=id value=$id> <tr> "; if ($this->table == 'prepaid') { $active_sessions = json_decode($this->db->f('active_sessions'),true); $account=$this->db->f('account'); $extraInfo=" <table border=0 bgcolor=#CCDDFF cellpadding=0 cellspacing=0> <form action=$PHP_SELF method=post> <input type=hidden name=web_task value=update> <input type=hidden name=next value=$next> <input type=hidden name=id value=$id> <tr> <td valign=top> <table border=0> "; $t=0; foreach (array_keys($active_sessions) as $_session) { $t++; $maxsessiontime=$active_sessions[$_session]['MaxSessionTime']; $extraInfo.=sprintf ("<tr bgcolor=lightgrey><td class=border>%d. Session id</td><td>%s</td></tr>",$t,$_session); $duration=time()-$active_sessions[$_session]['timestamp']; foreach (array_keys($active_sessions[$_session]) as $key) { if ($key=='timestamp') { $extraInfo.= sprintf ("<tr><td class=border><b>StartTime</b></td><td>%s</td></tr>",Date("Y-m-d H:i",$active_sessions[$_session]['timestamp'])); $extraInfo.= sprintf ("<tr><td class=border><b>Progress</b></td><td>%s (%s s)</td></tr>",sec2hms($duration),$duration); } else { $extraInfo.= sprintf ("<tr><td class=border><b>%s</b></td><td>%s</td></tr>",ucfirst($key),$active_sessions[$_session][$key]); } } if ($maxsessiontime < $duration ) { $extraInfo.= sprintf ("<tr><td class=border colspan=2><font color=red><b>Session expired since %d s</b></font></td></tr>",$duration-$maxsessiontime); $extraInfo.= sprintf("<tr><td colspan=2><input type=submit name=subweb_task value='Delete session'></td></tr>"); } //if (!$this->readonly) { //} } $extraInfo.=sprintf(" <input type=hidden name=table value='%s'> <input type=hidden name=next value='%s'> <input type=hidden name=sessionId value='%s'> <input type=hidden name=search_text value='%s'> </form> </table> </td> </tr> </table>", $this->table,$next,$_session,$search_text ); } print " <td>$found. </td> "; } $j=0; while ($j < $this->db->num_fields()) { $value=$this->db->Record[$metadata[$j]['name']]; $Fname=$metadata[$j]['name']; $size=$metadata[$j]['len']; $class=$metadata[$j]['class']; if ($this->tables[$this->table]['fields'][$Fname]['size']) { $field_size=$this->tables[$this->table]['fields'][$Fname]['size']; } else { $field_size=$el_size; } $class=$this->tables[$this->table]['fields'][$Fname]['class']; if ($this->tables[$this->table]['fields'][$Fname]['readonly']=="1") { $extra_form_els="disabled=true"; } else { $extra_form_els=""; } $class=$this->tables[$this->table]['fields'][$Fname]['class']; if (!in_array($Fname,$this->tables[$this->table]['exceptions'])) { if (!$export) { if (!in_array($Fname,$this->tables[$this->table]['keys']) && !$this->readonly) { if ($this->table == 'prepaid' && $Fname == 'session_counter' && $value) { if (count($active_sessions) > 1) { $session_counter_txt=sprintf("%d sessions",$value); } else { $session_counter_txt=sprintf("%d session",$value); } printf("<td onClick=\"return toggleVisibility('row%s')\"><a href=#>%s</td>",$found,$session_counter_txt); } else { if (!$class) { $class="span1"; } print "<td> <input class='$class' type=text bgcolor=grey size=$field_size maxlength=$size name=$Fname value=\"$value\" $extra_form_els> </td>"; } } else { if ($this->table == 'prepaid' && $Fname == 'session_counter' && $value) { if (count($active_sessions) > 1) { $session_counter_txt=sprintf("%d sessions",$value); } else { $session_counter_txt=sprintf("%d session",$value); } printf("<td onClick=\"return toggleVisibility('row%s')\"><a href=#>%s</td>",$found,$session_counter_txt); } else { print "<td>$value</td>"; } } } else { if ($j) { printf ("%s%s",$delimiter,$value); } else { print "2"; } } } $j++; } $j=0; while ($j < $cc ) { $Fname=$metadata[$j]['name']; $size=$metadata[$j]['len']; if (!in_array($Fname,$this->tables[$this->table]['exceptions'])) { $SEARCH_NAME="search_".$Fname; $value=$_REQUEST[$SEARCH_NAME]; if (!$export) { print "<input type=hidden name=search_$Fname value=\"$value\">"; } } $j++; } if ($export) { print "\n"; } if (!$export) { if (!$this->tables[$this->table]['readonly']) { if ($subweb_task=="Delete" && $idForDeletion == $id && !$confirmDelete) { print "<td class=border bgcolor=lightgrey>"; print "<input type=hidden name=confirmDelete value=1>"; print "<input type=submit name=subweb_task value=Delete>"; } else { print " <td class=border> <div class=\"btn-group\"> <input class='btn' type=submit name=subweb_task value=Update> <input class='btn btn-danger' type=submit name=subweb_task value=Delete></div> "; print "<input type=hidden name=confirmDelete value=1>"; } print " <input type=hidden name=table value=$this->table> <input type=hidden name=next value=$next> <input type=hidden name=search_text value=\"$search_text\"> </td> </tr> </form>"; if ($extraInfo!='') { print " <tr style='display:none' id='row$found'> <td></td> <td colspan=$t_columns>$extraInfo</td> </tr> "; } } else { if ($this->table=='prepaid') { print " <tr style='display:none' id='row$found'> <td></td> <td colspan=$t_columns>$extraInfo</td> </tr> "; } } } $i++; } if (!$export) { print "</form> </table> "; print " <form class=form-inline id=prev method=post> "; if ($next!= 0 ) { $show_next=$this->maxrowsperpage-$next; if ($show_next<0) { $mod_show_next = $show_next-2*$show_next; } print " <input style=\"display:none\" type=hidden name=maxrowsperpage value=$this->maxrowsperpage> <input type=hidden name=next value=$mod_show_next> <input type=hidden name=web_task value=Search> <input type=hidden name=table value=$this->table> <input type=hidden name=search_text value=\"$search_text\"> "; $j=0; while ($j < $cc ) { $Fname=$metadata[$j]['name']; $size=$metadata[$j]['len']; if (!in_array($Fname,$this->tables[$this->table]['exceptions'])) { $SEARCH_NAME="search_".$Fname; $value=$_REQUEST[$SEARCH_NAME]; print "<input type=hidden name=search_$Fname value=\"$value\"> "; } $j++; } } print " </form> <form class=form-inline id=next method=post> "; if ($rows>$this->maxrowsperpage && $rows!=$maxrows) { $show_next=$this->maxrowsperpage+$next; print " <input type=hidden name=maxrowsperpage value=$this->maxrowsperpage> <input type=hidden name=next value=$show_next> <input type=hidden name=table value=$this->table> <input type=hidden name=web_task value=Search> "; $j=0; while ($j < $cc ) { $Fname=$metadata[$j]['name']; $size=$metadata[$j]['len']; if (!in_array($Fname,$this->tables[$this->table]['exceptions'])) { $SEARCH_NAME="search_".$Fname; $value=$_REQUEST[$SEARCH_NAME]; print "<input type=hidden name=search_$Fname value=\"$value\">"; } $j++; } print " <input type=hidden name=search_text value=\"$search_text\"> "; } print " </form> <ul class=\"pager\">"; if ($next!= 0 ) { print " <li><a href=\"javascript:document.forms['prev'].submit()\">← Previous</a></li>"; } if ($rows>$this->maxrowsperpage && $rows!=$maxrows) { print " <li><a href=\"javascript:document.forms['next'].submit()\">Next →</a></li>"; } print "</ul>"; print " </body> </html> "; } }