api.deploy.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * One of se7en deadly sins
  4. */
  5. class Avarice {
  6. private $data = array();
  7. private $serial = '';
  8. private $raw = array();
  9. private $ident='HN';
  10. public function __construct() {
  11. $this->getSerial();
  12. $this->load();
  13. }
  14. /**
  15. * encodes data string by some key
  16. *
  17. * @param $data data to encode
  18. * @param $key encoding key
  19. *
  20. * @return string
  21. */
  22. protected function xoror($data, $key) {
  23. $result = '';
  24. for ($i = 0; $i < strlen($data);) {
  25. for ($j = 0; $j < strlen($key); $j++, $i++) {
  26. @$result .= $data[$i] ^ $key[$j];
  27. }
  28. }
  29. return ($result);
  30. }
  31. /**
  32. * packs xorored binary data into storable ascii data
  33. *
  34. * @param $data
  35. *
  36. *
  37. * @return string
  38. */
  39. protected function pack($data) {
  40. $data = base64_encode($data);
  41. return ($data);
  42. }
  43. /**
  44. * unpack packed ascii data into xorored binary
  45. *
  46. * @param $data
  47. *
  48. *
  49. * @return string
  50. */
  51. protected function unpack($data) {
  52. $data = base64_decode($data);
  53. return ($data);
  54. }
  55. /**
  56. * loads all stored licenses into private data prop
  57. *
  58. * @return void
  59. */
  60. protected function load() {
  61. if (!empty($this->serial)) {
  62. $query = "SELECT * from `ubstorage` WHERE `key` LIKE 'AVLICENSE_%'";
  63. $keys = simple_queryall($query);
  64. if (!empty($keys)) {
  65. foreach ($keys as $io => $each) {
  66. if (!empty($each['value'])) {
  67. $unpack = $this->unpack($each['value']);
  68. $unenc = $this->xoror($unpack, $this->serial);
  69. @$unenc = unserialize($unenc);
  70. if (!empty($unenc)) {
  71. if (isset($unenc['AVARICE'])) {
  72. if (isset($unenc['AVARICE']['SERIAL'])) {
  73. if ($this->serial == $unenc['AVARICE']['SERIAL']) {
  74. if (isset($unenc['AVARICE']['MODULE'])) {
  75. if (!empty($unenc['AVARICE']['MODULE'])) {
  76. $this->data[$unenc['AVARICE']['MODULE']] = $unenc[$unenc['AVARICE']['MODULE']];
  77. $this->raw[$unenc['AVARICE']['MODULE']]['LICENSE'] = $each['value'];
  78. $this->raw[$unenc['AVARICE']['MODULE']]['MODULE'] = $unenc['AVARICE']['MODULE'];
  79. $this->raw[$unenc['AVARICE']['MODULE']]['KEY'] = $each['key'];
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }
  91. /**
  92. * sets ubilling system key into private key prop
  93. *
  94. * @return void
  95. */
  96. protected function getSerial() {
  97. $hostid_q = "SELECT * from `ubstats` WHERE `key`='ubid'";
  98. $hostid = simple_query($hostid_q);
  99. if (!empty($hostid)) {
  100. if (isset($hostid['value'])) {
  101. $this->serial = $hostid['value'];
  102. }
  103. }
  104. }
  105. /**
  106. * checks module license availability
  107. *
  108. * @param $module module name to check
  109. *
  110. * @return bool
  111. */
  112. protected function check($module) {
  113. if (!empty($module)) {
  114. if (isset($this->data[$module])) {
  115. return (true);
  116. } else {
  117. return (false);
  118. }
  119. }
  120. }
  121. /**
  122. * returns module runtime
  123. *
  124. * @return array
  125. */
  126. public function runtime($module) {
  127. $result = array();
  128. if ($this->check($module)) {
  129. $result = $this->data[$module];
  130. }
  131. return ($result);
  132. }
  133. /**
  134. * returns list available license keys
  135. *
  136. * @return array
  137. */
  138. public function getLicenseKeys() {
  139. return ($this->raw);
  140. }
  141. /**
  142. * check license key before storing it
  143. *
  144. * @param string $key key to check 4 valid format
  145. *
  146. * @return bool
  147. */
  148. protected function checkLicenseValidity($key) {
  149. $result = false;
  150. if (@strpos($key, strrev($this->ident), 0) !== false) {
  151. @$key = $this->unpack($key);
  152. @$key = $this->xoror($key, $this->serial);
  153. @$key = unserialize($key);
  154. if (!empty($key)) {
  155. $result = true;
  156. }
  157. }
  158. return ($result);
  159. }
  160. /**
  161. * deletes key database record
  162. *
  163. * @param $keyname string identify key into database
  164. *
  165. * @return void
  166. */
  167. public function deleteKey($keyname) {
  168. $keyname = mysql_real_escape_string($keyname);
  169. $query = "DELETE from `ubstorage` WHERE `key` = '" . $keyname . "';";
  170. nr_query($query);
  171. log_register("AVARICE DELETE KEY `" . $keyname . '`');
  172. }
  173. /**
  174. * installs new license key
  175. *
  176. * @param $key string valid license key
  177. *
  178. * @return bool
  179. */
  180. public function createKey($key) {
  181. $key = mysql_real_escape_string($key);
  182. if ($this->checkLicenseValidity($key)) {
  183. $keyname = 'AVLICENSE_' . zb_rand_string('8');
  184. $query = "INSERT INTO `ubstorage` (`id`, `key`, `value`) VALUES (NULL, '" . $keyname . "', '" . $key . "');";
  185. nr_query($query);
  186. log_register("AVARICE INSTALL KEY `" . $keyname . '`');
  187. return (true);
  188. } else {
  189. log_register("AVARICE TRY INSTALL WRONG KEY");
  190. return (false);
  191. }
  192. }
  193. /**
  194. * updates existing license key
  195. */
  196. public function updateKey($index, $key) {
  197. if ($this->checkLicenseValidity($key)) {
  198. simple_update_field('ubstorage', 'value', $key, "WHERE `key`='" . $index . "'");
  199. log_register("AVARICE UPDATE KEY `" . $index . '`');
  200. return (true);
  201. } else {
  202. log_register("AVARICE TRY UPDATE WRONG KEY");
  203. return (false);
  204. }
  205. }
  206. }
  207. /**
  208. * Renders available license keys with all of required controls
  209. *
  210. * @return void
  211. */
  212. function zb_LicenseLister() {
  213. $result = '';
  214. $avarice = new Avarice();
  215. $all = $avarice->getLicenseKeys();
  216. $messages = new UbillingMessageHelper();
  217. if (!empty($all)) {
  218. $cells = wf_TableCell(__('License key'));
  219. $cells .= wf_TableCell(__('Actions'));
  220. $rows = wf_TableRow($cells, 'row1');
  221. foreach ($all as $io => $each) {
  222. //construct edit form
  223. $editinputs = wf_HiddenInput('editdbkey', $each['KEY']);
  224. $editinputs .= wf_TextArea('editlicense', '', $each['LICENSE'], true, '50x10');
  225. $editinputs .= wf_Submit(__('Save'));
  226. $editform = wf_Form("", 'POST', $editinputs, 'glamour');
  227. $editcontrol = wf_modalAuto(web_edit_icon(), __('Edit') . ' ' . $each['MODULE'], $editform, '');
  228. $deletionUrl = '?module=licensekeys&licensedelete=' . $each['KEY'];
  229. $cancelUrl = '?module=licensekeys';
  230. $delLabel = __('Delete') . ' ' . __('License key') . ' ' . $each['MODULE'] . '? ';
  231. $delLabel .= $messages->getDeleteAlert();
  232. $deletecontrol = wf_ConfirmDialog($deletionUrl, web_delete_icon(), $delLabel, '', $cancelUrl);
  233. $cells = wf_TableCell($each['MODULE']);
  234. $cells .= wf_TableCell($deletecontrol . ' ' . $editcontrol);
  235. $rows .= wf_TableRow($cells, 'row5');
  236. }
  237. $result .= wf_TableBody($rows, '100%', 0, '');
  238. } else {
  239. $result .= $messages->getStyledMessage(__('You do not have any license keys installed. So how are you going to live like this?'), 'warning');
  240. }
  241. //constructing license creation form
  242. $addinputs = wf_TextArea('createlicense', '', '', true, '50x10');
  243. $addinputs .= wf_Submit(__('Save'));
  244. $addform = wf_Form("", 'POST', $addinputs, 'glamour');
  245. $addcontrol = wf_modalAuto(web_icon_create() . ' ' . __('Install license key'), __('Install license key'), $addform, 'ubButton');
  246. $result .= wf_delimiter(0);
  247. $result .= $addcontrol;
  248. show_window(__('Installed license keys'), $result);
  249. }