api.openpayz.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. //include mysql interraction layer and useful debug functions
  3. include("api.mysql.php");
  4. include("api.compat.php");
  5. include_once("api.paysysproto.php");
  6. /**
  7. * Registers new non processed transaction
  8. *
  9. * @param string $hash
  10. * @param float $summ
  11. * @param int $customerid
  12. * @param string $paysys
  13. * @param string $note
  14. *
  15. * @return void
  16. */
  17. function op_TransactionAdd($hash, $summ, $customerid, $paysys, $note) {
  18. $date = curdatetime();
  19. $summ = vf($summ);
  20. $customerid = mysql_real_escape_string($customerid);
  21. $paysys = mysql_real_escape_string($paysys);
  22. $note = mysql_real_escape_string($note);
  23. $hash = mysql_real_escape_string($hash);
  24. $query = "INSERT INTO `op_transactions` (`id`,`hash`, `date` , `summ` , `customerid` ,`paysys` , `processed` ,`note`)
  25. VALUES (NULL ,'" . $hash . "' , '" . $date . "', '" . $summ . "', '" . $customerid . "', '" . $paysys . "', '0', '" . $note . "');";
  26. nr_query($query);
  27. }
  28. /**
  29. * Loads openpayz config as key=>value
  30. *
  31. * @return array
  32. */
  33. function op_LoadConfig() {
  34. $result = @parse_ini_file(dirname(__FILE__) . '/../config/openpayz.ini');
  35. return ($result);
  36. }
  37. /**
  38. * Mark transaction as processed
  39. *
  40. * @param int $transactionid
  41. *
  42. * @return void
  43. */
  44. function op_TransactionSetProcessed($transactionid) {
  45. $transactionid = vf($transactionid);
  46. $query = "UPDATE `op_transactions` SET `processed` = '1' WHERE `id`='" . $transactionid . "'";
  47. nr_query($query);
  48. }
  49. /**
  50. * Converts IP to integer value
  51. *
  52. * @param string $src
  53. *
  54. * @return int
  55. */
  56. function ip2int($src) {
  57. $t = explode('.', $src);
  58. return count($t) != 4 ? 0 : 256 * (256 * ((float) $t[0] * 256 + (float) $t[1]) + (float) $t[2]) + (float) $t[3];
  59. }
  60. /**
  61. * Converts integer into IP
  62. *
  63. * @param int $src
  64. *
  65. * @return string
  66. */
  67. function int2ip($src) {
  68. $s1 = (int) ($src / 256);
  69. $i1 = $src - 256 * $s1;
  70. $src = (int) ($s1 / 256);
  71. $i2 = $s1 - 256 * $src;
  72. $s1 = (int) ($src / 256);
  73. return sprintf('%d.%d.%d.%d', $s1, $src - 256 * $s1, $i2, $i1);
  74. }
  75. /**
  76. * Native XML parser function that returns XML structure as array
  77. *
  78. * @param string $contents
  79. * @param int $get_attributes
  80. * @param string $priority
  81. *
  82. * @return array
  83. */
  84. function xml2array($contents, $get_attributes = 1, $priority = 'tag') {
  85. if (!$contents) {
  86. return array();
  87. }
  88. if (!function_exists('xml_parser_create')) {
  89. print "'xml_parser_create()' function not found!";
  90. return array();
  91. }
  92. //Get the XML parser of PHP - PHP must have this module for the parser to work
  93. $parser = xml_parser_create('');
  94. xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
  95. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  96. xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  97. xml_parse_into_struct($parser, trim($contents), $xml_values);
  98. xml_parser_free($parser);
  99. if (!$xml_values) {
  100. return; //Hmm...
  101. }
  102. //Initializations
  103. $xml_array = array();
  104. $parents = array();
  105. $opened_tags = array();
  106. $arr = array();
  107. $current = &$xml_array; //Refference
  108. //Go through the tags.
  109. $repeated_tag_index = array(); //Multiple tags with same name will be turned into an array
  110. foreach ($xml_values as $data) {
  111. unset($attributes, $value); //Remove existing values, or there will be trouble
  112. //This command will extract these variables into the foreach scope
  113. // tag(string), type(string), level(int), attributes(array).
  114. extract($data); //We could use the array by itself, but this cooler.
  115. $result = array();
  116. $attributes_data = array();
  117. if (isset($value)) {
  118. if ($priority == 'tag')
  119. $result = $value;
  120. else
  121. $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
  122. }
  123. //Set the attributes too.
  124. if (isset($attributes) and $get_attributes) {
  125. foreach ($attributes as $attr => $val) {
  126. if ($priority == 'tag')
  127. $attributes_data[$attr] = $val;
  128. else
  129. $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
  130. }
  131. }
  132. //See tag status and do the needed.
  133. if ($type == "open") {//The starting of the tag '<tag>'
  134. $parent[$level - 1] = &$current;
  135. if (!is_array($current) or ( !in_array($tag, array_keys($current)))) { //Insert New tag
  136. $current[$tag] = $result;
  137. if ($attributes_data)
  138. $current[$tag . '_attr'] = $attributes_data;
  139. $repeated_tag_index[$tag . '_' . $level] = 1;
  140. $current = &$current[$tag];
  141. } else { //There was another element with the same tag name
  142. if (isset($current[$tag][0])) {//If there is a 0th element it is already an array
  143. $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
  144. $repeated_tag_index[$tag . '_' . $level] ++;
  145. } else {//This section will make the value an array if multiple tags with the same name appear together
  146. $current[$tag] = array($current[$tag], $result); //This will combine the existing item and the new item together to make an array
  147. $repeated_tag_index[$tag . '_' . $level] = 2;
  148. if (isset($current[$tag . '_attr'])) { //The attribute of the last(0th) tag must be moved as well
  149. $current[$tag]['0_attr'] = $current[$tag . '_attr'];
  150. unset($current[$tag . '_attr']);
  151. }
  152. }
  153. $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
  154. $current = &$current[$tag][$last_item_index];
  155. }
  156. } elseif ($type == "complete") { //Tags that ends in 1 line '<tag />'
  157. //See if the key is already taken.
  158. if (!isset($current[$tag])) { //New Key
  159. $current[$tag] = $result;
  160. $repeated_tag_index[$tag . '_' . $level] = 1;
  161. if ($priority == 'tag' and $attributes_data)
  162. $current[$tag . '_attr'] = $attributes_data;
  163. } else { //If taken, put all things inside a list(array)
  164. if (isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
  165. // ...push the new element into that array.
  166. $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
  167. if ($priority == 'tag' and $get_attributes and $attributes_data) {
  168. $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
  169. }
  170. $repeated_tag_index[$tag . '_' . $level] ++;
  171. } else { //If it is not an array...
  172. $current[$tag] = array($current[$tag], $result); //...Make it an array using using the existing value and the new value
  173. $repeated_tag_index[$tag . '_' . $level] = 1;
  174. if ($priority == 'tag' and $get_attributes) {
  175. if (isset($current[$tag . '_attr'])) { //The attribute of the last(0th) tag must be moved as well
  176. $current[$tag]['0_attr'] = $current[$tag . '_attr'];
  177. unset($current[$tag . '_attr']);
  178. }
  179. if ($attributes_data) {
  180. $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
  181. }
  182. }
  183. $repeated_tag_index[$tag . '_' . $level] ++; //0 and 1 index is already taken
  184. }
  185. }
  186. } elseif ($type == 'close') { //End of tag '</tag>'
  187. $current = &$parent[$level - 1];
  188. }
  189. }
  190. return($xml_array);
  191. }
  192. /**
  193. * Returns plain array of not processed transactions
  194. *
  195. * @return array
  196. */
  197. function op_TransactionGetNotProcessed() {
  198. $query = "SELECT * from `op_transactions` WHERE `processed`='0';";
  199. $result = simple_queryall($query);
  200. return ($result);
  201. }
  202. /**
  203. * Returns array of availavle virtualid=>realid(login) mappings
  204. *
  205. * @return array
  206. */
  207. function op_CustomersGetAll() {
  208. $query = "SELECT * from `op_customers`";
  209. $allcustomers = simple_queryall($query);
  210. $result = array();
  211. if (!empty($allcustomers)) {
  212. foreach ($allcustomers as $io => $eachcustomer) {
  213. $result[$eachcustomer['virtualid']] = $eachcustomer['realid'];
  214. }
  215. }
  216. return ($result);
  217. }
  218. /**
  219. * Stargazer direct handler
  220. *
  221. * @param int $virtualid
  222. * @param float $cash
  223. * @param string $paysys
  224. *
  225. * @return void
  226. */
  227. function op_HandleStg($virtualid, $cash, $paysys = '') {
  228. $opconfig = op_LoadConfig();
  229. $allcustomers = op_CustomersGetAll();
  230. $sgconf = $opconfig['SGCONF'];
  231. $stg_host = $opconfig['STG_HOST'];
  232. $stg_port = $opconfig['STG_PORT'];
  233. $stg_login = $opconfig['STG_LOGIN'];
  234. $stg_passwd = $opconfig['STG_PASSWD'];
  235. $customPrefix = 'CASHTYPEID_';
  236. if (isset($opconfig['UB_CASHTYPE'])) {
  237. //normal cashtypes handling
  238. $ub_cashtype = $opconfig['UB_CASHTYPE'];
  239. //custom per payment system cashtypes
  240. if (isset($opconfig[$customPrefix . $paysys])) {
  241. $ub_cashtype=$opconfig[$customPrefix . $paysys];
  242. }
  243. } else {
  244. //cash money by default
  245. $ub_cashtype = 1;
  246. }
  247. if ($paysys == '') {
  248. $note = 'OPENPAYZ';
  249. } else {
  250. $note = 'OP:' . $paysys;
  251. }
  252. if (isset($allcustomers[$virtualid])) {
  253. $login = $allcustomers[$virtualid];
  254. //adding cash if login exists
  255. $addcash_cmd = $sgconf . ' set -s ' . $stg_host . ' -p ' . $stg_port . ' -a' . $stg_login . ' -w' . $stg_passwd . ' -u' . $login . ' -c ' . $cash;
  256. shell_exec($addcash_cmd);
  257. //ubilling payment logging
  258. $curdate = date("Y-m-d H:i:s");
  259. $balance_q = "SELECT `Cash` from `users` WHERE `login`='" . $login . "'";
  260. $curbalance = simple_query($balance_q);
  261. $curbalance = $curbalance['Cash'] - $cash;
  262. $query_paymentlog = "INSERT INTO `payments` (`id` , `login` , `date` , `admin` , `balance` , `summ` , `cashtypeid` , `note` )
  263. VALUES (NULL , '" . $login . "', '" . $curdate . "', 'openpayz', '" . $curbalance . "', '" . $cash . "', '" . $ub_cashtype . "', '" . $note . "');";
  264. nr_query($query_paymentlog);
  265. }
  266. }
  267. /**
  268. * Mail notification handler
  269. *
  270. * @param string $body
  271. *
  272. * @return void
  273. */
  274. function op_HandleMail($body) {
  275. $opconfig = op_LoadConfig();
  276. $allcustomers = op_CustomersGetAll();
  277. $notify_mail = $opconfig['NOTIFY_MAIL'];
  278. $subject = "New OpenPayz transaction";
  279. mail($notify_mail, $subject, $body);
  280. }
  281. /**
  282. * Performs processing of all unprocessed transctions with required handlers
  283. *
  284. * @param bool $force - Use the force, Luke!
  285. *
  286. * @return void
  287. */
  288. function op_ProcessHandlers($force = false) {
  289. $opconfig = op_LoadConfig();
  290. if (($force == true) OR ( !@$opconfig['OP_HIGHLOAD_ENABLE'])) {
  291. $realProcessing = true; //do some real work?
  292. } else {
  293. $realProcessing = false;
  294. }
  295. if ($realProcessing) {
  296. $unprocessed = op_TransactionGetNotProcessed();
  297. if (!empty($unprocessed)) {
  298. $mailbody = '';
  299. foreach ($unprocessed as $io => $eachtransaction) {
  300. // mail notify
  301. if ($opconfig['SEND_MAIL']) {
  302. $mailbody.="
  303. ===\n
  304. id: " . $eachtransaction['id'] . " \n
  305. date: " . $eachtransaction['date'] . " \n
  306. customerid: " . $eachtransaction['customerid'] . " \n
  307. summ: " . $eachtransaction['summ'] . " \n
  308. paysys: " . $eachtransaction['paysys'] . " \n
  309. hash: " . $eachtransaction['hash'] . " \n
  310. ===\n\n
  311. ";
  312. }
  313. // stargazer direct payments
  314. if ($opconfig['STG_DIRECT']) {
  315. //$customerid=vf($eachtransaction['customerid'],3);
  316. $customerid = $eachtransaction['customerid'];
  317. op_HandleStg($customerid, $eachtransaction['summ'], $eachtransaction['paysys']);
  318. op_TransactionSetProcessed($eachtransaction['id']);
  319. }
  320. }
  321. //send mail notify
  322. if ($opconfig['SEND_MAIL']) {
  323. op_HandleMail($mailbody);
  324. }
  325. }
  326. }
  327. }
  328. ?>