PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/emclnx/lnx_pay.php

https://gitlab.com/NucleusStudios/emclnx
PHP | 179 lines | 115 code | 35 blank | 29 comment | 20 complexity | 09f653cfce2adcc2527d03407fd68cb1 MD5 | raw file
  1. <?php
  2. //--------------------------------------------------------------
  3. // EMCLNX - EmerCoin Link Exchange system
  4. // Distributed under BSD license
  5. // https://en.wikipedia.org/wiki/BSD_licenses
  6. // Designed by maxihatop, EmerCoin group
  7. // WEB: http://www.emercoin.com
  8. // Contact: team@emercoin.com
  9. require_once('emclnx.php');
  10. $lnx = new emcLNX();
  11. $conf = $lnx->GetCoinfig();
  12. $dbh = $lnx->GetDbh();
  13. $payment_tx = "NONE";
  14. $dest_url = $conf['dest_url']; // default destination URL
  15. //------------------------------------------------------------------------------
  16. // Invoice structure like:
  17. // HashCoins:EQ6DKLnBAbHxUvGTyjuAhQwVruZ7SvXHbR:0.91:7.6:-6.61
  18. // There is:
  19. // ContractName : PayTo address : Pay for this Click : Balance : Credit(allowance)
  20. // returns 0 or error text
  21. function emcLNX_lnx_pay($invoice, $ip, $quality) {
  22. global $lnx, $dbh, $conf, $payment_tx, $dest_url;
  23. $lnxRatingTAU = $conf['RatingTAU'];
  24. $lnxK4RatingSIGMA = $conf['K4RatingSIGMA'];
  25. $lnxCPCExtra = $conf['cpc_extra'];
  26. $lnxTempTreshold = $conf['max_temp'];
  27. $lnxQualityTreshold = $conf['min_quality'];
  28. $lnxIPTreshold = $conf['IPTreshold'];
  29. try {
  30. list($nvs_key, $pay_addr, $pay_this, $balance, $credit, $cpa_addr) = preg_split('/:/', $invoice, 6);
  31. if(!isset($credit))
  32. throw new Exception("Wrong invoice syntax\n");
  33. $iptemp = $lnx->emcLNX__IPTemperature($ip, ($pay_this > 0)? 1 : 0);
  34. $rc = 0;
  35. // Fetch CPC and increase visit_cnt
  36. $stmt = $dbh->prepare("Select cpc, dest_url from payer_contracts where nvs_key=?");
  37. $stmt->execute(array($nvs_key));
  38. $cpc_row = $stmt->fetchAll(PDO::FETCH_ASSOC);
  39. if(empty($cpc_row))
  40. return "Contract $nvs_key is missing"; // We do not pay this contract
  41. $cpc = $cpc_row[0]['cpc'];
  42. if(!empty($cpc_row[0]['dest_url']))
  43. $dest_url = $cpc_row[0]['dest_url'];
  44. // Attach CPA address, if needed and exist
  45. $dest_url = preg_replace('/\$CPA_ADDR/', $cpa_addr, $dest_url);
  46. $stmt = $dbh->prepare("Update payer_contracts Set visit_cnt = visit_cnt + 1 where nvs_key=?");
  47. $stmt->execute(array($nvs_key));
  48. // Insert default record, if not exist
  49. $stmt = $dbh->prepare("Insert ignore into payer_hosts (pay_addr) values(?)");
  50. $stmt->execute(array($pay_addr));
  51. $dbh->beginTransaction();
  52. $q = "Select rating, temperature, TIME_TO_SEC(TIMEDIFF(NOW(), last_event)) as dt, " .
  53. " pay_balance, quality, pay_sent" .
  54. " from payer_hosts where pay_addr=? for update";
  55. $stmt = $dbh->prepare($q);
  56. $stmt->execute(array($pay_addr));
  57. $hoster_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  58. // Update credentials by formulas
  59. $dt = $hoster_rows[0]['dt'];
  60. $temp = $hoster_rows[0]['temperature'] * exp(-$dt / $conf['RatingTAU']);
  61. $rating = $hoster_rows[0]['rating'];
  62. $actual_cpc = 0;
  63. if($pay_this > 0) {
  64. // this is real user, so invoice is proceed
  65. $temp += 1 + log(1 + $cpc);
  66. $rating += 1 / $temp;
  67. // Set Actual CPC - adjust with Rating and routemp
  68. $actual_cpc = $cpc * $rating / ($rating + $conf['K4RatingSIGMA']);
  69. }
  70. $quality = $hoster_rows[0]['quality'] * exp(-$dt / $lnxRatingTAU) + $quality;
  71. $routemp = $iptemp <= 1.0? 1.0 : sqrt($iptemp);
  72. $actual_cpc = sprintf("%.2f", $actual_cpc / $routemp);
  73. // Set up initial payment/ignored values
  74. $actual_pay = $actual_ignore = 0.0;
  75. $db_balance = $hoster_rows[0]['pay_balance'];
  76. // Check restrictions
  77. do {
  78. if($iptemp > $lnxIPTreshold) {
  79. $rc = "Too high IP temperature=$iptemp > $lnxIPTreshold";
  80. $actual_ignore = $pay_this;
  81. break; // Seems like fraudster activity
  82. }
  83. if($temp > $lnxTempTreshold) {
  84. $actual_ignore = $pay_this;
  85. $rc = "Too high hoster temperature=$temp > $lnxTempTreshold";
  86. break; // Hoster asks quick and many
  87. }
  88. if($quality < $lnxQualityTreshold) {
  89. $actual_ignore = $pay_this;
  90. $rc = "Too low quality=$quality < $lnxQualityTreshold";
  91. break; // Hoster asks quick and many
  92. }
  93. if($pay_this > $actual_cpc + $lnxCPCExtra) {
  94. $pay_this = $actual_cpc + $lnxCPCExtra;
  95. }
  96. $limbal = $db_balance + $lnxCPCExtra + $pay_this;
  97. if($balance > $limbal) {
  98. $balance = $limbal;
  99. }
  100. // Minimal trusted balance - min(request_bal, $limbal); Can be negative!
  101. $balance = sprintf("%.2f", $balance); // round to cents
  102. if($credit < $actual_cpc * 3 && $balance > 0) {
  103. $actual_pay = sprintf("%.2f", max($pay_this, $balance));
  104. }
  105. } while(0);
  106. if($actual_pay > 0) {
  107. $overpay = sprintf("%.2f", $lnx->emcLNX__compute_allowance($hoster_rows[0]['pay_sent']));
  108. $actual_pay += $overpay;
  109. // echo "B=$balance O=$overpay a=$actual_pay\n";
  110. // Send payment to hoster
  111. $payment_tx = $lnx->emcLNX__req('sendtoaddress', array($pay_addr, floatval($actual_pay), "emcLNX $nvs_key"));
  112. $db_balance = -$overpay; // payoff balance
  113. } else // If no pay - just accept the new balance
  114. if(!$rc && $balance > $db_balance)
  115. $db_balance = $balance; // we agreed with this new balance only if no err, and skip fale low req_bal
  116. $q = "Update payer_hosts Set rating=?, temperature=?," .
  117. " last_event=Now(), visit_cnt=visit_cnt + 1, " .
  118. " pay_balance=?, pay_sent=pay_sent+?, pay_ignored=pay_ignored+?, quality=?" .
  119. " where pay_addr=?";
  120. $stmt = $dbh->prepare($q);
  121. $stmt->execute(array($rating, $temp,
  122. $db_balance, $actual_pay, $actual_ignore, $quality,
  123. $pay_addr));
  124. $dbh->commit();
  125. // $dbh->rollBack();
  126. return $rc? "Contract $nvs_key hoster=$pay_addr: $rc" : 0;
  127. } catch(Exception $ex) {
  128. return "emcLNX_lnx_pay error: ". $ex->getMessage();
  129. }
  130. } // function rand_href
  131. //------------------------------------------------------------------------------
  132. // MAIN here
  133. // Request from NVS list of contracts
  134. $invoice = $lnx->GetQS();
  135. $ip = $lnx->GetIP();
  136. $rc = emcLNX_lnx_pay($invoice, $ip, 1);
  137. $lnx->Log("\tlnx_pay($invoice) OUT:\t$rc; TX=$payment_tx" );
  138. if($rc) echo $rc;
  139. else {
  140. if(!$ip) echo "Res=OK; TX=$payment_tx\n";
  141. else header("Location: " . $dest_url); /* Redirect browser */
  142. }
  143. //------------------------------------------------------------------------------
  144. ?>