<?

function SavePayment($user, $type, $detail, $amount, $time) {
  global $dbConn;
  $query  = "insert into idx_payment_history
             (username, type, detail, amount, time)
             values
             ('$user', '$type', '$detail', $amount, now())";
  $result = $dbConn->Execute($query);
}

function UpgradeLink($id, $type, $day) {
  global $dbConn, $base_path;

  if ($type == 'PREMIUM') {
    $premium   = 1;
    $sponsored = 0;
  }

  if ($type == 'SPONSORED') {
    $premium   = 0;
    $sponsored = 1;
  }

  if ($type == 'PREMIUM' || $type == 'SPONSORED') {

    $days_extend = 3600 * 24 * $day;

    // check if exist

    $query  = "select link_id, premium, sponsored, expire
               from idx_paid_listing where link_id = '$id'";

    $result = $dbConn->Execute($query);

    if ($result->RecordCount() == 0) {

      // create new record
      $expire = date('Y-m-d H:i:s', strtotime("+$days_extend seconds"));
      $query  = "insert into idx_paid_listing
                 (link_id, premium, sponsored, expire, paid)
                 values ('$id', '$premium', '$sponsored', '$expire', 1)";
      $result = $dbConn->Execute($query);
    }
    else {

      // get expire time
      $expire = $result->Fields("expire");
      $curr_premium   = $result->Fields("premium");
      $curr_sponsored = $result->Fields("sponsored");

      if ($curr_premium != $premium || $curr_sponsored != $sponsored) {
        $expire = date('Y-m-d H:i:s');
      }

      if (time() < @strtotime($expire)) {
        $expire = date('Y-m-d H:i:s', strtotime("$expire +$days_extend seconds"));
      }
      else {
        $expire = date('Y-m-d H:i:s', strtotime("+$days_extend seconds"));
      }

      // update existing record
      $query  = "update idx_paid_listing
                 set expire = '$expire', premium = $premium, sponsored = $sponsored
                 where link_id = '$id'";
      $result = $dbConn->Execute($query);
    }
  }

  // activate link
  $query  = "update idx_link set suspended = 0 where link_id = '$id'";
  $result = $dbConn->Execute($query);

  // remove cache
  @RemoveDir($base_path . 'cache');
}

function MarkAsPaid($id) {
  global $dbConn;
  $query = "update idx_invoice set paid = '1' where invoice_id = '$id'";
  $result = $dbConn->Execute($query);
}

function GetPaymentSystems($model = 'radio') {
  global $base_path, $id, $gateway;

  $dir = $base_path . "payment/";
  $files = array ();
  if ($dir = @opendir($dir)) {
    while (($file = readdir($dir)) !== false) {
      $files[] = $path . $file;
    }
  }
  closedir ($dir);

  $i          = 0;
  $invoice_id = $id;
  if (!$gateway) {
    $checked = "checked=\"checked\"";
  }

  foreach ($files as $key => $value) {
    if (substr($value, -8) == '.inc.php') {
      include $base_path . "payment/" . $value;
      $ps_name = str_replace('.inc.php','',$value);

      if($i>0) {
        $checked = '';
      }

      if ($enable == '1' || $model == 'raw') {
        if ($model == 'radio') {
          $ps_html = strip_tags($invoice_html,'<img>');
          $ps_html = str_replace('<img ', '<img align="middle" ', $ps_html);
          if ($gateway == $ps_name) {
            $ps .= "<input type=\"radio\" name=\"gateway\" value=\"$ps_name\" checked=\"checked\" /> " . $ps_html;
          }
          else {
            $ps .= "<input type=\"radio\" name=\"gateway\" value=\"$ps_name\" $checked /> " . $ps_html;
          }
        }

        if ($model == 'image') {
          $ps .= $invoice_html;
        }

        if ($model == 'raw') {
          $ps[$i]['name'] = $ps_name;
          $ps[$i]['enable'] = $enable;
        }

        $i++;
      }
    }
  }

  return $ps;
}

?>
