/uc_client/model/app.php

https://github.com/shanchengren/darky · PHP · 139 lines · 125 code · 13 blank · 1 comment · 19 complexity · 425ad3b75f7b5404da1067589eafa754 MD5 · raw file

  1. <?php
  2. class appmodel {
  3. var $base;
  4. var $db;
  5. function __construct(&$base) {
  6. $this->appmodel($base);
  7. }
  8. function appmodel(&$base) {
  9. $this->base =& $base;
  10. $this->db =& $base->db;
  11. }
  12. function applist($appid = null) {
  13. static $list = null;
  14. if (!isset($list)) {
  15. $list = array();
  16. $query = $this->db->query("SELECT * FROM pw_ucapp");
  17. while ($rt = $this->db->fetch_array($query)) {
  18. $list[$rt['id']] = $rt;
  19. }
  20. }
  21. return isset($appid) ? $list[$appid] : $list;
  22. }
  23. function isUc($appid) {
  24. $app = $this->applist($appid);
  25. return ($app['uc'] == 1);
  26. }
  27. function checkColumns() {
  28. $apps = array();
  29. $ucid = 0;
  30. foreach ($this->applist() as $key => $app) {
  31. if (!$app['uc']) {
  32. $apps[] = $key;
  33. } else {
  34. $ucid = $key;
  35. }
  36. }
  37. $this->alterTable('pw_ucsyncredit', $apps);
  38. $this->alterTable('pw_ucnotify', $apps, $ucid);
  39. }
  40. function alterTable($table, $apps, $ucid = null) {
  41. if ($ucid) {
  42. $apps[] = $ucid;
  43. }
  44. $col = array();
  45. $query = $this->db->query("SHOW COLUMNS FROM $table LIKE 'app%'");
  46. while ($rt = $this->db->fetch_array($query)) {
  47. $col[] = substr($rt['Field'], 3);
  48. }
  49. if ($addcol = array_diff($apps, $col)) {
  50. $sql = '';
  51. foreach ($addcol as $v) {
  52. $sql .= "ADD app{$v} TINYINT(1) NOT NULL,";
  53. }
  54. $this->db->query("ALTER TABLE $table " . rtrim($sql, ','));
  55. }
  56. if ($delcol = array_diff($col, $apps)) {
  57. $sql = '';
  58. foreach ($delcol as $v) {
  59. $sql .= "DROP app{$v},";
  60. }
  61. $this->db->query("ALTER TABLE $table " . rtrim($sql, ','));
  62. }
  63. }
  64. function post_params($apikey, $mode, $method, $args = array()) {
  65. $url = '';
  66. $params = array();
  67. $params['mode'] = $mode;
  68. $params['method'] = $method;
  69. $params['format'] = 'PHP';
  70. $params['charset'] = 'gbk';
  71. $params['type'] = 'uc';
  72. $params['v'] = '1.0';
  73. $params['params'] = $args ? serialize($args) : '';
  74. ksort($params);
  75. $str = '';
  76. foreach ($params as $k => $v) {
  77. if ($v) {
  78. $str .= $k . '=' . $v . '&';
  79. $url .= $k . '=' . urlencode($v) . '&';
  80. }
  81. }
  82. $url .= 'sig=' . md5($str . $apikey);
  83. return $url;
  84. }
  85. function post_url($site, $interface) {
  86. !$interface && $interface = 'pw_api.php';
  87. return rtrim($site, '/') . "/{$interface}?";
  88. }
  89. function urlformat($url, $interface, $apikey, $mode, $method, $args = array()) {
  90. return $this->post_url($url, $interface) . $this->post_params($apikey, $mode, $method, $args);
  91. }
  92. function ucfopen($url, $interface, $apikey, $mode, $method, $args = array(), $limit = 5) {
  93. $url = $this->post_url($url, $interface);
  94. $parse = @parse_url($url);
  95. if (empty($parse)) return false;
  96. if (!$parse['port']) {
  97. $parse['port'] = '80';
  98. }
  99. $parse['host'] = str_replace(array('http://','https://'), array('','ssl://'), "$parse[scheme]://").$parse['host'];
  100. if (!$fp = @fsockopen($parse['host'],$parse['port'],$errnum,$errstr,$limit)) {
  101. return array('errCode' => -1, 'errMessage' => 'connect fail');
  102. //return false;
  103. }
  104. $gp = 'GET';
  105. $parse['path'] = str_replace(array('\\','//'),'/',$parse['path'])."?$parse[query]";
  106. $wlength = $wdata = '';
  107. if ($data = $this->post_params($apikey, $mode, $method, $args)) {
  108. $gp = 'POST';
  109. $wlength = "Content-length: ".strlen($data)."\r\n";
  110. $wdata = $data;
  111. }
  112. $write = "$gp $parse[path] HTTP/1.0\r\nHost: $parse[host]\r\nContent-type: application/x-www-form-urlencoded\r\n{$wlength}Connection: close\r\n\r\n$wdata";
  113. @fwrite($fp,$write);
  114. while ($data = @fread($fp, 4096)) {
  115. $responseText .= $data;
  116. }
  117. @fclose($fp);
  118. $responseText = trim(stristr($responseText,"\r\n\r\n"),"\r\n");
  119. if ($responseText && is_array($responseText = unserialize($responseText))) {
  120. return $responseText;
  121. } else {
  122. return array('errCode' => -1, 'errMessage' => 'connect fail');
  123. }
  124. }
  125. }
  126. ?>