PageRenderTime 57ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/hypervm/httpdocs/htmllib/phplib/lxlib.php

https://github.com/yourhty/hypervm
PHP | 2351 lines | 1975 code | 287 blank | 89 comment | 253 complexity | 63a5a338bb5182828c725c38794c60d3 MD5 | raw file
  1. <?php
  2. if (windowsOs()) {
  3. include_once "htmllib/lib/windowslib.php";
  4. include_once "lib/windowsproglib.php";
  5. } else {
  6. include_once "htmllib/lib/linuxlib.php";
  7. include_once "lib/linuxproglib.php";
  8. }
  9. // Don't remove this. This is used for slave upgrade.
  10. function remotetestfunc()
  11. {
  12. }
  13. define('S_IFDIR', 00040000);
  14. define('S_ISUID', 00004000);
  15. define('S_ISGID', 00002000);
  16. // This is the only function that exectues during the initialization... The rest of the whle library exists as functions that can be called... Nothing gets executed on their own... Execept this.. So it makes this sort of special... very special..
  17. init_global();
  18. function init_global()
  19. {
  20. global $gbl, $sgbl, $ghtml;
  21. global $g_demo;
  22. $sgbl = new Sgbl();
  23. $gbl = new Gbl();
  24. $gbl->get();
  25. //
  26. // Turn on demo version by putting a empty file called demo in the etc dir
  27. //
  28. if (lfile_exists("__path_program_etc/demo")) {
  29. $g_demo = 1;
  30. }
  31. //
  32. // ### LxCenter
  33. //
  34. // Check for Development/Debug version
  35. // If file not exists, Production mode (-1)
  36. // If file exists it can have the following numbers to enable
  37. // 1 = Debug mode 1
  38. // 2 = Debug mode 2
  39. // 3 = Debug mode 3
  40. // 4 = Debug mode 4
  41. // 5 = Debug mode 5
  42. // -1 = Turn Off and go to production mode
  43. check_for_debug("/commands.php");
  44. // Disabled by LxCenter, we are not at PHP version with number 1 at postition N ( x.N.x )
  45. // $v = explode(".", PHP_VERSION);
  46. // if ($v[1] == "1" && $sgbl->isDebug()) {
  47. // date_default_timezone_set("UTC");
  48. // }
  49. $sgbl->method = ($sgbl->dbg >= 1) ? "get" : "post";
  50. }
  51. function debug_for_backend()
  52. {
  53. global $gbl, $sgbl, $login, $ghtml;
  54. check_for_debug("/commands.php");
  55. if ($sgbl->isDebug()) {
  56. return;
  57. }
  58. check_for_debug("/backend.php");
  59. }
  60. function check_for_debug($file)
  61. {
  62. global $gbl, $sgbl, $login, $ghtml;
  63. if (file_exists(getreal($file))) {
  64. $sgbl->dbg = file_get_contents(getreal($file));
  65. if ($sgbl->dbg != "1" && $sgbl->dbg != "2" && $sgbl->dbg != "3" && $sgbl->dbg != "4" && $sgbl->dbg != "5") {
  66. $sgbl->dbg = -1;
  67. }
  68. } else {
  69. $sgbl->dbg = -1;
  70. }
  71. if ($sgbl->dbg > 0) {
  72. ini_set("error_reporting", E_ALL & ~E_STRICT);
  73. ini_set("display_errors", "On");
  74. ini_set("log_errors", "On");
  75. } else {
  76. ini_set("error_reporting", E_ERROR);
  77. ini_set("display_errors", "Off");
  78. ini_set("log_errors", "On");
  79. }
  80. }
  81. function isUpdating()
  82. {
  83. return lx_core_lock_check_only("update.php");
  84. }
  85. class lxException extends Exception
  86. {
  87. public $syncserver;
  88. public $class;
  89. public $variable;
  90. public $error;
  91. public $message;
  92. function getClass()
  93. {
  94. return lget_class($this);
  95. }
  96. function __construct($message, $variable = 'nname', $value = null)
  97. {
  98. $this->message = $message;
  99. $this->variable = $variable;
  100. $this->value = $value;
  101. $this->__full_message = "$message: $variable: $value";
  102. //log_log("exception", "$message: $variable: $value");
  103. }
  104. function getlMessage()
  105. {
  106. return "$this->message: $this->variable: $this->value";
  107. }
  108. }
  109. function getAllOperatingSystemDetails()
  110. {
  111. $ret = findOperatingSystem();
  112. $ret['loadavg'] = os_getLoadAvg();
  113. dprintr($ret);
  114. return $ret;
  115. }
  116. function findOperatingSystem($type = null)
  117. {
  118. if (windowsOs()) {
  119. $ret['os'] = 'windows';
  120. try {
  121. $obj = new COM("Winmgmts://./root/cimv2");
  122. } catch (exception $e) {
  123. //throw new lxException("com_failed", '');
  124. return null;
  125. }
  126. $list = $obj->execQuery("select Caption from Win32_OperatingSystem");
  127. foreach ($list as $l) {
  128. $ret['version'] = $l->Caption;
  129. $ret['pointversion'] = $l->Caption;
  130. }
  131. return $ret;
  132. }
  133. if (file_exists("/etc/fedora-release")) {
  134. $ret['os'] = 'fedora';
  135. $ret['version'] = file_get_contents("/etc/fedora-release");
  136. $ret['pointversion'] = find_os_pointversion();
  137. } else if (file_exists("/etc/redhat-release")) {
  138. $ret['os'] = 'rhel';
  139. $ret['version'] = file_get_contents("/etc/redhat-release");
  140. $ret['pointversion'] = find_os_pointversion();
  141. }
  142. if (lxfile_exists("__path_program_etc/install_xen") || lxfile_exists("/proc/xen")) {
  143. $ret['vpstype'] = "xen";
  144. $ret['xenlocation'] = vg_complete();
  145. }
  146. if ($type) {
  147. return $ret[$type];
  148. }
  149. return $ret;
  150. }
  151. function find_os_pointversion()
  152. {
  153. if (file_exists("/etc/fedora-release")) {
  154. $release = trim(file_get_contents("/etc/fedora-release"));
  155. $osv = explode(" ", $release);
  156. if (strtolower($osv[1]) === 'core') {
  157. $osversion = "fedora-" . $osv[3];
  158. } else {
  159. $osversion = "fedora-" . $osv[2];
  160. }
  161. return $osversion;
  162. }
  163. if (file_exists("/etc/redhat-release")) {
  164. $release = trim(file_get_contents("/etc/redhat-release"));
  165. $osv = explode(" ", $release);
  166. if (isset($osv[6])) {
  167. $osversion = "rhel-" . $osv[6];
  168. } else {
  169. $oss = explode(".", $osv[2]);
  170. $osversion = "centos-" . $oss[0];
  171. }
  172. return $osversion;
  173. }
  174. }
  175. function lscandir_without_dot($arg, $dotflag = false)
  176. {
  177. $list = lscandir($arg);
  178. if (!$list) {
  179. return $list;
  180. }
  181. foreach ($list as $k => $v) {
  182. if ($v === ".." || $v === "." || $v === '.svn') {
  183. unset($list[$k]);
  184. }
  185. if ($dotflag && csb($v, '.')) {
  186. unset($list[$k]);
  187. }
  188. }
  189. return $list;
  190. }
  191. function lscandir_without_dot_or_underscore($arg, $dotflag = false)
  192. {
  193. $list = lscandir($arg);
  194. if (!$list) {
  195. return $list;
  196. }
  197. foreach ($list as $k => $v) {
  198. if ($v === ".." || $v === "." || $v === '.svn') {
  199. unset($list[$k]);
  200. }
  201. if ($dotflag && csb($v, '.')) {
  202. unset($list[$k]);
  203. }
  204. if (csb($v, "__")) {
  205. unset($list[$k]);
  206. }
  207. }
  208. return $list;
  209. }
  210. function lscandir($arg)
  211. {
  212. return lx_redefine_func("scandir", $arg);
  213. }
  214. function lunlink($arg)
  215. {
  216. return lx_redefine_func("unlink", $arg);
  217. }
  218. function ltouch($arg)
  219. {
  220. return lx_redefine_func("touch", $arg);
  221. }
  222. function lchdir($arg)
  223. {
  224. return lx_redefine_func("chdir", $arg);
  225. }
  226. function takeToStartOfLine($fp)
  227. {
  228. while (fgetc($fp) == "\n" && ftell($fp) != 1 && ftell($fp) != 0) {
  229. fseek($fp, -2, SEEK_CUR);
  230. }
  231. while (($c = fgetc($fp)) != "\n" && ftell($fp) != 1 && ftell($fp) != 0) {
  232. fseek($fp, -2, SEEK_CUR);
  233. }
  234. }
  235. function tail_func($file, $lines)
  236. {
  237. $fp = fopen($file, "r");
  238. if (!$fp) {
  239. return null;
  240. }
  241. dprint("in Tail Func\n");
  242. fseek($fp, 0, SEEK_END);
  243. // Go back onece and read the line.
  244. takeToStartOfLine($fp);
  245. $arr[] = fgets($fp);
  246. $n = 0;
  247. while ($n < $lines && ftell($fp) !== 1) {
  248. $n++;
  249. //dprint($n . "\n");
  250. // You have to go back twice.
  251. print(' Before: ' . ftell($fp) . "\n");
  252. fseek($fp, -3, SEEK_CUR);
  253. takeToStartOfLine($fp);
  254. fseek($fp, -3, SEEK_CUR);
  255. print(' aaFter: ' . ftell($fp) . "\n");
  256. takeToStartOfLine($fp);
  257. print(' aaaaFter: ' . ftell($fp) . "\n");
  258. $arr[] = fgets($fp);
  259. }
  260. return implode("", array_reverse($arr));
  261. }
  262. function lfile_get_json_unserialize($file)
  263. {
  264. if (!lxfile_exists($file)) {
  265. return null;
  266. }
  267. return json_decode(lfile_get_contents($file), true);
  268. }
  269. function lfile_put_json_serialize($file, $var)
  270. {
  271. return lfile_put_contents($file, json_encode($var));
  272. }
  273. function lfile_get_unserialize($file)
  274. {
  275. if (!lxfile_exists($file)) {
  276. return null;
  277. }
  278. return unserialize(lfile_get_contents($file));
  279. }
  280. function lfile_put_serialize($file, $var)
  281. {
  282. return lfile_put_contents($file, serialize($var));
  283. }
  284. function lfile_get_contents($arg)
  285. {
  286. return lx_redefine_func("file_get_contents", $arg);
  287. }
  288. function lrename($arg1, $arg2)
  289. {
  290. return lx_redefine_func("rename", $arg1, $arg2);
  291. }
  292. function lfopen($arg1, $arg2)
  293. {
  294. return lx_redefine_func("fopen", $arg1, $arg2);
  295. }
  296. function lfilesize($arg)
  297. {
  298. return lx_redefine_func("filesize", $arg);
  299. }
  300. function ltempnam($arg1, $arg2)
  301. {
  302. return lx_redefine_func("tempnam", $arg1, $arg2);
  303. }
  304. function lfile_write_content($file, $data, $user)
  305. {
  306. if (csa($user, ":")) {
  307. $realuser = strtil($user, ":");
  308. } else {
  309. $realuser = $user;
  310. }
  311. if (!check_file_if_owned_by($file, $realuser)) {
  312. return;
  313. }
  314. lfile_put_contents($file, $data);
  315. lxfile_unix_chown($file, $user);
  316. }
  317. function check_file_if_owned_by_and_throw($filename, $username)
  318. {
  319. if (!check_file_if_owned_by($filename, $username)) {
  320. throw new lxexception('file_exists_not_owned', '', $filename);
  321. }
  322. }
  323. function lis_hardlink($file)
  324. {
  325. $file = expand_real_root($file);
  326. if (is_dir($file)) {
  327. return false;
  328. }
  329. $stat = stat($file);
  330. if ($stat['nlink'] >= 2) {
  331. return true;
  332. }
  333. return false;
  334. }
  335. function is_soft_or_hardlink($file)
  336. {
  337. if (!lxfile_exists($file)) {
  338. return false;
  339. }
  340. if (lis_link($file) || lis_hardlink($file)) {
  341. return true;
  342. }
  343. return false;
  344. }
  345. function new_process_mv_rec($user, $src, $dst)
  346. {
  347. $src = expand_real_root($src);
  348. $dst = expand_real_root($dst);
  349. new_process_cmd($user, null, "mv $src $dst");
  350. }
  351. function new_process_chmod_rec($user, $file, $perm)
  352. {
  353. $file = expand_real_root($file);
  354. $cmd = "chmod -R $perm '$file'";
  355. new_process_cmd($user, null, $cmd);
  356. }
  357. function new_process_cp_rec($user, $src, $dst)
  358. {
  359. $src = expand_real_root($src);
  360. $dst = expand_real_root($dst);
  361. $cmd = "cp -a '$src' '$dst'";
  362. new_process_cmd($user, null, $cmd);
  363. }
  364. function new_process_cmd($user, $dir, $cmd)
  365. {
  366. global $sgbl;
  367. if (csa($user, ':')) {
  368. list($user, $group) = explode(':', $user);
  369. } else {
  370. $group = $user;
  371. }
  372. if ($user === 'root') {
  373. $user = '__system__';
  374. }
  375. if ($dir) {
  376. $olddir = getcwd();
  377. chdir($dir);
  378. }
  379. if ($user !== '__system__') {
  380. $uid = is_numeric($user) ? (int) $user : os_get_uid_from_user($user);
  381. $gid = is_numeric($group) ? (int) $group : os_get_gid_from_user($user);
  382. exec("{$sgbl->__path_php_path} {$sgbl->__path_program_root}/bin/phpexec.php $uid $gid $cmd 2>&1", $output, $retval);
  383. } else {
  384. exec("$cmd 2>&1", $output, $retval);
  385. }
  386. if ($dir) {
  387. chdir($olddir);
  388. }
  389. $output = implode("\n", $output);
  390. log_log('user_cmd', "($dir) $user $cmd $output");
  391. return $retval;
  392. }
  393. function lfile_put_contents($file, $data, $flag = null)
  394. {
  395. $file = expand_real_root($file);
  396. if (is_soft_or_hardlink($file)) {
  397. log_log("link_error", "$file is hard or symlink. Not writing\n");
  398. return;
  399. }
  400. if (char_search_a($data, "__path_")) {
  401. dprint("<font color=red>Warning : Trying to write __path into a file $file: </font> $data <br> \n", 3);
  402. }
  403. lxfile_mkdir(dirname($file));
  404. if(file_exists($file)){
  405. if(is_readable($file)){
  406. if(is_writable($file)){
  407. return file_put_contents($file, $data, $flag);
  408. }
  409. else{
  410. $error_msg = 'Could not write the file \''.$file.'\' with permissions: '.substr(sprintf('%o', fileperms($file)), -4);
  411. dprint($error_msg);
  412. log_log('filesys', $error_msg);
  413. return false;
  414. }
  415. }
  416. else{
  417. $error_msg = 'Could not read the file \''.$file.'\' with permissions: '.substr(sprintf('%o', fileperms($file)), -4);
  418. dprint($error_msg);
  419. log_log('filesys', $error_msg);
  420. return false;
  421. }
  422. }
  423. else{
  424. if(file_put_contents($file, $data, $flag) === false){
  425. $error_msg = 'File \''.$file.'\' could not be created.';
  426. dprint($error_msg);
  427. log_log('filesys', $error_msg);
  428. return false;
  429. }
  430. return true;
  431. }
  432. }
  433. /**
  434. * @return void
  435. * @param unknown
  436. * @param unknown
  437. * @desc Redefining php functions ... sort of.. Stupid php doesn't allow that. So we do the next best thing.. We add an 'l' to all system functions and then use these functions instead of the php ones... In a way, is a better idea too, since, there might always be some cases where we might want to override this crap. :-)
  438. */
  439. function lmkdir($dir)
  440. {
  441. return lx_redefine_func("mkdir", $dir);
  442. }
  443. function lis_executable($file)
  444. {
  445. return lx_redefine_func("is_executable", $file);
  446. }
  447. function lis_readable($file)
  448. {
  449. return lx_redefine_func("is_readable", $file);
  450. }
  451. function lreadlink($file)
  452. {
  453. return lx_redefine_func("readlink", $file);
  454. }
  455. function lis_link($file)
  456. {
  457. return lx_redefine_func("is_link", $file);
  458. }
  459. function lis_dir($file)
  460. {
  461. return lx_redefine_func("is_dir", $file);
  462. }
  463. function cp_if_not_exists($src, $dst)
  464. {
  465. if (lxfile_exists($dst)) {
  466. return;
  467. }
  468. if (!lxfile_exists($src)) {
  469. return;
  470. }
  471. lxfile_cp($src, $dst);
  472. }
  473. function cp_rec_if_not_exists($src, $dst)
  474. {
  475. if (lxfile_exists($dst)) {
  476. return;
  477. }
  478. if (!lxfile_exists($src)) {
  479. return;
  480. }
  481. lxfile_cp_rec($src, $dst);
  482. }
  483. function mv_rec_if_not_exists($src, $dst)
  484. {
  485. if (lxfile_exists($dst)) {
  486. return;
  487. }
  488. if (!lxfile_exists($src)) {
  489. return;
  490. }
  491. lxfile_mv_rec($src, $dst);
  492. }
  493. function llstat($file)
  494. {
  495. return lx_redefine_func("lstat", $file);
  496. }
  497. function lx_merge_good($arg)
  498. {
  499. global $gbl, $sgbl, $login, $ghtml;
  500. $start = 0;
  501. $transforming_func = null;
  502. eval($sgbl->arg_getting_string);
  503. //dprintr($arglist);
  504. $list = $arglist;
  505. foreach ($list as &$l) {
  506. if (!$l) {
  507. $l = array();
  508. }
  509. }
  510. $ret = array();
  511. foreach ($list as $nl) {
  512. //dprintr($nl);
  513. if (is_array($nl)) {
  514. $ret = array_merge($ret, $nl);
  515. } else {
  516. $ret[] = $nl;
  517. }
  518. }
  519. return $ret;
  520. }
  521. function lx_array_merge($list)
  522. {
  523. foreach ($list as &$l) {
  524. if (!$l) {
  525. $l = array();
  526. }
  527. }
  528. $ret = array();
  529. foreach ($list as $nl) {
  530. //dprintr($nl);
  531. if (is_array($nl)) {
  532. $ret = array_merge($ret, $nl);
  533. } else {
  534. $ret[] = $nl;
  535. }
  536. }
  537. return $ret;
  538. }
  539. function log_switch($mess, $id = 1)
  540. {
  541. log_log('switch', $mess, $id);
  542. }
  543. function log_error($mess, $id = 1)
  544. {
  545. log_log('error', $mess, $id);
  546. }
  547. function log_bdatabase($mess, $id = 1)
  548. {
  549. log_log('bdatabase', $mess, $id);
  550. }
  551. function log_restore($mess, $id = 1)
  552. {
  553. log_log('restore', $mess, $id);
  554. }
  555. function log_database($mess, $id = 1)
  556. {
  557. log_log('database', $mess, $id);
  558. }
  559. function myPcntl_reaper()
  560. {
  561. pcntl_wait($status, WNOHANG);
  562. }
  563. function myPcntl_wait()
  564. {
  565. if (!WindowsOs()) {
  566. pcntl_wait($status);
  567. }
  568. }
  569. function myPcntl_fork()
  570. {
  571. global $gbl, $sgbl, $login, $ghtml;
  572. if (!WindowsOs()) {
  573. $pid = pcntl_fork();
  574. } else {
  575. // make it child
  576. $pid = 0;
  577. }
  578. return $pid;
  579. }
  580. function log_log($file, $mess, $id = null)
  581. {
  582. if (!is_string($mess)) {
  583. $mess = var_export($mess, true);
  584. }
  585. $mess = trim($mess);
  586. $rf = "__path_program_root/log/$file";
  587. lfile_put_contents($rf, @ date("H:i M/d/Y") . ": $mess" . PHP_EOL, FILE_APPEND);
  588. }
  589. function log_ajax($mess, $id = 1)
  590. {
  591. log_log('ajax', $mess, $id);
  592. }
  593. function log_redirect($mess, $id = 1)
  594. {
  595. log_log('redirect_error', $mess, $id);
  596. }
  597. function log_message($mess, $id = 1)
  598. {
  599. log_log('message', $mess, $id);
  600. }
  601. function log_security($mess, $id = 1)
  602. {
  603. // get IP
  604. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  605. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  606. }
  607. else {
  608. $ip = $_SERVER['REMOTE_ADDR'];
  609. }
  610. $user_agent = $_SERVER["HTTP_USER_AGENT"];
  611. if (empty($_SERVER["HTTP_USER_AGENT"])) {
  612. $user_agent = "Not a browser";
  613. }
  614. log_log('security', $mess . " IP: $ip; User agent: $user_agent", $id);
  615. }
  616. function log_filesys_err($mess, $id = 1)
  617. {
  618. log_log('filesyserr', $mess, $id);
  619. }
  620. function log_filesys($mess, $id = 1)
  621. {
  622. global $global_dontlogshell;
  623. if ($global_dontlogshell) {
  624. log_log('nonfilesys', $mess, $id);
  625. } else {
  626. log_log('filesys', $mess, $id);
  627. }
  628. }
  629. function log_shell($mess, $id = 1)
  630. {
  631. log_log('shell_exec', $mess, $id);
  632. }
  633. function log_shell_error($mess, $id = 1)
  634. {
  635. log_log('shell_error', $mess, $id);
  636. }
  637. function lfile_trim($arg)
  638. {
  639. $list = lfile($arg);
  640. foreach ($list as &$s) {
  641. $s = trim($s);
  642. }
  643. return $list;
  644. }
  645. function lcopy($src, $dst)
  646. {
  647. return lx_redefine_func("copy", $src, $dst);
  648. }
  649. function lreadfile($file)
  650. {
  651. return lx_redefine_func("readfile", $file);
  652. }
  653. function lmd5_file($file)
  654. {
  655. return lx_redefine_func("md5_file", $file);
  656. }
  657. function lfile_exists($file)
  658. {
  659. return lx_redefine_func("file_exists", $file);
  660. }
  661. function lsqlite_open($file)
  662. {
  663. return lx_redefine_func("sqlite_open", $file);
  664. }
  665. /**
  666. * @return void
  667. * @param unknown
  668. * @param unknown
  669. * @desc This function is the core of the the path abstraction. It converts the paths of the form '__path.../dir' to '$sgbl->__path.../dir'. This is used in all the redefined functions to convert their arguments.
  670. */
  671. function expand_real_root($root)
  672. {
  673. global $gbl, $sgbl, $login, $ghtml;
  674. if (char_search_beg($root, "__path")) {
  675. if (char_search_a($root, "/")) {
  676. $var = substr($root, 0, strpos($root, "/"));
  677. $root = $sgbl->$var . "/" . substr($root, strpos($root, "/") + 1);
  678. } else {
  679. $root = $sgbl->$root;
  680. }
  681. }
  682. $root = remove_extra_slash($root);
  683. return $root;
  684. }
  685. /**
  686. * @return void
  687. * @param unknown
  688. * @param unknown
  689. * @desc the function that does the redefining of fundamental file access functions. Note the double use of 'eval'. hey hey, nothing is impossible in php. Never ever repeat the code; Make it unreadable instead. :-)
  690. */
  691. function kill_and_save_pid($name)
  692. {
  693. kill_pid($name);
  694. usleep(100);
  695. save_pid($name);
  696. }
  697. function save_pid($name)
  698. {
  699. lfile_put_contents("__path_program_root/pid/$name.pid", os_getpid());
  700. }
  701. function kill_pid($name)
  702. {
  703. $pid = lfile_get_contents("__path_program_root/pid/$name.pid");
  704. os_killpid($pid);
  705. }
  706. /************************************
  707. * HOW-TO GENERATE KEYPAIRS
  708. * //Passphrase is helloworld
  709. * $ openssl req -x509 -newkey rsa:1024 -keyout mykey.key -out mycert.crt
  710. */
  711. function licenseDecrypt($license_content)
  712. {
  713. global $gbl, $sgbl, $login, $ghtml;
  714. $fp = lfopen("__path_program_root/file/lprogram.crt", "r");
  715. $public_key = fread($fp, 8192);
  716. fclose($fp);
  717. openssl_get_publickey($public_key);
  718. $list = explode("\n", $license_content);
  719. // decrypt
  720. print_time('decrypt');
  721. $fullstring = null;
  722. foreach ($list as $l) {
  723. $l = trim($l);
  724. if (!$l) {
  725. continue;
  726. }
  727. $decrypted_string = null;
  728. openssl_public_decrypt(base64_decode($l), $decrypted_string, $public_key);
  729. //dprintr($decrypted_string . " $l<br> \n");
  730. $fullstring .= $decrypted_string;
  731. }
  732. print_time('decrypt', "Time Taken For decrypt:", 3);
  733. return $fullstring;
  734. }
  735. function lx_redefine_func($func)
  736. {
  737. global $gbl, $sgbl, $login, $ghtml;
  738. $start = 1;
  739. $transforming_func = "expand_real_root";
  740. eval($sgbl->arg_getting_string);
  741. return call_user_func_array($func, $arglist);
  742. }
  743. function licenseEncrypt($string)
  744. {
  745. global $gbl, $sgbl, $login, $ghtml;
  746. $file = "$sgbl->__path_program_root/file/license_privatekey.key";
  747. // encrypt
  748. $pass = "helloworld";
  749. $result = openssl_get_privatekey(array("file://" . $file, $pass));
  750. $ar = str_split($string, 100);
  751. $fullstring = null;
  752. foreach ($ar as $s) {
  753. openssl_private_encrypt($s, $encrypted_string, $result);
  754. $fullstring .= base64_encode($encrypted_string) . "\n";
  755. }
  756. print(openssl_error_string());
  757. return $fullstring;
  758. }
  759. function remove_unnecessary_stat(&$stat)
  760. {
  761. foreach ($stat as $k => $v) {
  762. if (is_numeric($k)) {
  763. unset($stat[$k]);
  764. }
  765. }
  766. }
  767. function getShellCommand($cmd, $arglist)
  768. {
  769. global $gbl, $sgbl, $login, $ghtml;
  770. $args = null;
  771. $q = $sgbl->__var_quote_char;
  772. $cmd = expand_real_root($cmd);
  773. $cmd = str_replace(";", "", $cmd);
  774. $cmd = "{$q}$cmd{$q}";
  775. foreach ($arglist as $a) {
  776. if ($a === "") {
  777. continue;
  778. }
  779. if (is_array($a)) {
  780. foreach ($a as $aa) {
  781. $aa = str_replace(";", "", $aa);
  782. $args .= " $q" . expand_real_root($aa) . "$q";
  783. }
  784. } else {
  785. $a = str_replace(";", "", $a);
  786. $args .= " $q" . expand_real_root($a) . "$q";
  787. }
  788. }
  789. $cmd .= " " . $args;
  790. return $cmd;
  791. }
  792. class Remote
  793. {
  794. /*
  795. public $ddata;
  796. public $message;
  797. public $exception;
  798. */
  799. }
  800. function dprintoa($var, $type = 0)
  801. {
  802. global $sgbl, $login, $ghtml;
  803. if ($type > $sgbl->dbg) {
  804. return;
  805. }
  806. if (!is_array($var)) {
  807. return;
  808. }
  809. foreach ($var as $k => $v) {
  810. dprint("$k =>");
  811. dprinto($var, $type);
  812. dprint("\n");
  813. }
  814. }
  815. function dprinto($var, $type = 0)
  816. {
  817. global $sgbl;
  818. if ($type > $sgbl->dbg) {
  819. return;
  820. }
  821. if (!is_object($var)) {
  822. return;
  823. }
  824. $newob = clone($var);
  825. $newob->__parent_o = null;
  826. dprintr($newob);
  827. }
  828. function dprintr($var, $type = 0)
  829. {
  830. global $sgbl;
  831. if ($type > $sgbl->dbg) {
  832. return;
  833. }
  834. if (is_object($var) && method_exists($var, "clearChildrenAndParent")) {
  835. $newvar = myclone($var);
  836. lxclass::clearChildrenAndParent($newvar);
  837. $newvar->driverApp = 'unset for printing';
  838. $newvar->__parent_o = 'unset for printing';
  839. $class = $newvar->get__table();
  840. if (csb($class, "sp_")) {
  841. $bclass = strfrom($class, "sp_") . "_b";
  842. $newvar->$bclass->__parent_o = 'unset for printing';
  843. }
  844. } else {
  845. $newvar = $var;
  846. }
  847. if ($sgbl->isBlackBackground()) {
  848. print("<font color=gray>");
  849. }
  850. print_r($newvar);
  851. if ($sgbl->isBlackBackground()) {
  852. print("</font> ");
  853. }
  854. }
  855. function dprint($var, $type = 0)
  856. {
  857. global $sgbl;
  858. if ($type <= $sgbl->dbg) {
  859. if (is_string($var)) {
  860. if ($sgbl->isBlackBackground()) {
  861. print("<font color=gray>");
  862. }
  863. print($var);
  864. if ($sgbl->isBlackBackground()) {
  865. print("</font> ");
  866. }
  867. }
  868. }
  869. }
  870. function dprint_r($var, $type = 0)
  871. {
  872. global $sgbl;
  873. if ($type <= $sgbl->dbg) {
  874. print_r($var);
  875. }
  876. }
  877. function lx_local_socket_read($socket)
  878. {
  879. return @ socket_read($socket, 2048);
  880. //$res=socket_recv($MsgSock,$buffer,1024,0);
  881. }
  882. function csa($haystack, $needle, $insensitive = 0)
  883. {
  884. return char_search_a($haystack, $needle, $insensitive);
  885. }
  886. function char_search_a($haystack, $needle, $insensitive = 1)
  887. {
  888. if (is_array($haystack)) {
  889. //dprint("Got array in Char Search ");
  890. //dprintr($haystack);
  891. }
  892. if (is_object($haystack)) {
  893. $v = debugBacktrace(true);
  894. log_log("error", $v);
  895. }
  896. if ($insensitive) {
  897. return (false !== stristr($haystack, $needle)) ? true : false;
  898. } else {
  899. return (false !== strpos($haystack, $needle)) ? true : false;
  900. }
  901. }
  902. function strtil($string, $needle)
  903. {
  904. if (strrpos($string, $needle) !== false) {
  905. return substr($string, 0, strrpos($string, $needle));
  906. } else {
  907. return $string;
  908. }
  909. }
  910. function strtilfirst($string, $needle)
  911. {
  912. if (strpos($string, $needle)) {
  913. return substr($string, 0, strpos($string, $needle));
  914. } else {
  915. return $string;
  916. }
  917. }
  918. function strfrom($string, $needle)
  919. {
  920. if (!csa($string, $needle)) {
  921. return $string;
  922. }
  923. return substr($string, strpos($string, $needle) + strlen($needle));
  924. }
  925. function array_push_unique($array, $value)
  926. {
  927. if (!$array) {
  928. $array = array();
  929. }
  930. foreach ($array as $var) {
  931. if ($var === $value) {
  932. return $array;
  933. }
  934. }
  935. $array[] = $value;
  936. return $array;
  937. }
  938. function array_remove($array, $element)
  939. {
  940. $ret = null;
  941. foreach ($array as $value) {
  942. if ($value !== $element) {
  943. $ret[] = $value;
  944. }
  945. }
  946. return $ret;
  947. }
  948. function csb($haystack, $needle, $insensitive = 1)
  949. { # Char Search Begin
  950. return char_search_beg($haystack, $needle, $insensitive);
  951. }
  952. function char_search_beg($haystack, $needle)
  953. {
  954. if (is_array($haystack)) {
  955. //debugBacktrace();
  956. }
  957. if (strpos($haystack, $needle) === 0) {
  958. return true;
  959. }
  960. return false;
  961. }
  962. function cse($haystack, $needle, $insensitive = 1)
  963. { # Char Search End
  964. return char_search_end($haystack, $needle, $insensitive);
  965. }
  966. function char_search_end($haystack, $needle, $insensitive)
  967. {
  968. if (strpos($haystack, $needle) === false) {
  969. return false;
  970. }
  971. if ((strrpos($haystack, $needle) + strlen($needle)) === strlen($haystack)) {
  972. return true;
  973. } else {
  974. return false;
  975. }
  976. }
  977. function array_search_bool($needle, $haystack)
  978. {
  979. if (!$haystack) {
  980. return false;
  981. }
  982. if (array_search($needle, $haystack) !== false) {
  983. return true;
  984. }
  985. return false;
  986. }
  987. function isLicensed($var)
  988. {
  989. global $gbl, $sgbl, $login, $ghtml;
  990. if ($var == 'lic_client') {
  991. return true;
  992. }
  993. $lic = $login->getObject('license')->licensecom_b;
  994. if (!isset($lic->$var)) {
  995. return false;
  996. }
  997. return isOn($lic->$var);
  998. }
  999. function is_composite($class)
  1000. {
  1001. return false;
  1002. return csa($class, "__");
  1003. }
  1004. function get_composite($class)
  1005. {
  1006. return array(null, null, $class);
  1007. $list = explode("__", $class);
  1008. if (count($list) === 2) {
  1009. return array($list[0], null, $list[1]);
  1010. }
  1011. return array($list[0], $list[1], $list[2]);
  1012. }
  1013. function setLicenseTodefault()
  1014. {
  1015. global $gbl, $sgbl, $login, $ghtml;
  1016. $license = $login->getObject('license');
  1017. $license->parent_clname = $login->getClName();
  1018. $lic = $license->licensecom_b;
  1019. $def = array("maindomain_num" => "40", "vps_num" => 5, "pserver_num" => 10, "client_num" => "Unlimited");
  1020. $list = get_license_resource();
  1021. foreach ($list as $l) {
  1022. $licv = "lic_$l";
  1023. $lic->$licv = $def[$l];
  1024. }
  1025. $license->setUpdateSubaction();
  1026. $license->write();
  1027. }
  1028. function decodeAndStoreLicense($ip, $license_content)
  1029. {
  1030. global $gbl, $sgbl, $login, $ghtml;
  1031. $license = $login->getObject('license');
  1032. $license->parent_clname = $login->getClName();
  1033. $lic = $license->licensecom_b;
  1034. $get = licenseDecrypt($license_content);
  1035. $license->text_license_content = $license_content;
  1036. if (!$get) {
  1037. throw new lxException("could_not_decrypt_license");
  1038. }
  1039. $get = 'licence.php?' . $get;
  1040. $ghtml->get_post_from_get($get, $path, $post);
  1041. if (!isset($post['maindomain_num'])) {
  1042. $post['maindomain_num'] = $post['domain_num'];
  1043. }
  1044. if ($sgbl->isDebug()) {
  1045. $post['maindomain_num'] = '1000';
  1046. }
  1047. foreach ($post as $k => $v) {
  1048. $var = "lic_" . $k;
  1049. $lic->$var = $v;
  1050. }
  1051. $lic->lic_ipaddress .= " ($ip)";
  1052. $prilist = $login->getQuotaVariableList();
  1053. foreach ($prilist as $k => $v) {
  1054. if (cse($k, "_flag")) {
  1055. $login->priv->$k = 'On';
  1056. } else if (cse($k, "_usage")) {
  1057. $login->priv->$k = 'Unlimited';
  1058. } else if (cse($k, "_num")) {
  1059. $login->priv->$k = 'Unlimited';
  1060. }
  1061. }
  1062. $login->priv->client_num = $post['client_num'];
  1063. if (isset($post['maindomain_num'])) {
  1064. $login->priv->maindomain_num = $post['maindomain_num'];
  1065. }
  1066. if (isset($post['vps_num'])) {
  1067. $login->priv->vps_num = $post['vps_num'];
  1068. }
  1069. $login->priv->pserver_num = $post['pserver_num'];
  1070. $login->setUpdateSubaction();
  1071. $login->write();
  1072. $license->setUpdateSubaction();
  1073. $license->write();
  1074. }
  1075. function remove_dot_dot($list)
  1076. {
  1077. foreach ($list as $k => $v) {
  1078. if ($v === "." || $v === "..") {
  1079. unset($list[$k]);
  1080. }
  1081. }
  1082. return $list;
  1083. }
  1084. function lx_tmp_file($file)
  1085. {
  1086. global $gbl, $sgbl, $login, $ghtml;
  1087. $file = expand_real_root($file);
  1088. $n = preg_replace("+/+i", "_", $file);
  1089. return tempnam("$sgbl->__path_tmp/", "lxtmp_$n");
  1090. //return "/tmp/" . $n;
  1091. }
  1092. function lx_array_keys($list)
  1093. {
  1094. if (!$list) {
  1095. $list = array();
  1096. }
  1097. return array_keys($list);
  1098. }
  1099. function array_filter_key($full, $need)
  1100. {
  1101. if (!$need) {
  1102. return $full;
  1103. }
  1104. foreach ($full as $key => $value) {
  1105. if (array_search_bool($key, $need)) {
  1106. $ret[$key] = $value;
  1107. }
  1108. }
  1109. return $ret;
  1110. }
  1111. function gethtmllibversion()
  1112. {
  1113. }
  1114. function isOn($var)
  1115. {
  1116. return (($var === 'on') || ($var === 'On')) ? true : false;
  1117. }
  1118. // Function that is called to test whther the remote server is working fine or not. Used while adding.
  1119. function test_remote_func()
  1120. {
  1121. return true;
  1122. }
  1123. function log_clicks($mess, $id = 1)
  1124. {
  1125. global $gbl, $sgbl, $login, $ghtml;
  1126. if (!if_demo()) {
  1127. return;
  1128. }
  1129. $ip = $gbl->c_session->ip_address;
  1130. $mess = trim($mess);
  1131. $file = "__path_program_root/log/clicks";
  1132. lfile_put_contents($file, "$id: $ip: " . @date("H:i:s M/d/Y") . ": $mess\n", FILE_APPEND);
  1133. }
  1134. # Version Comparison Returns 1 if version1 is greater, and -1 if version2 is greater.
  1135. function version_cmp($version1, $version2)
  1136. {
  1137. $l1 = explode(".", $version1);
  1138. $l2 = explode(".", $version2);
  1139. for ($i = 0; $i < 3; $i++) {
  1140. if ($l2[$i] === $l1[$i]) {
  1141. continue;
  1142. }
  1143. if ($l1[$i] > $l2[$i]) {
  1144. return 1;
  1145. }
  1146. if ($l1[$i] < $l2[$i]) {
  1147. return -1;
  1148. }
  1149. }
  1150. }
  1151. function app_version_cmp($version1, $version2)
  1152. {
  1153. $l1 = explode(".", $version1);
  1154. $l2 = explode(".", $version2);
  1155. for ($i = 0; $i < 4; $i++) {
  1156. if (!isset($l2[$i])) {
  1157. $l2[$i] = 0;
  1158. }
  1159. if (!isset($l1[$i])) {
  1160. $l1[$i] = 0;
  1161. }
  1162. }
  1163. //dprintr($l1);
  1164. //dprintr($l2);
  1165. for ($i = 0; $i < 4; $i++) {
  1166. if ($l2[$i] === $l1[$i]) {
  1167. continue;
  1168. }
  1169. if ($l1[$i] > $l2[$i]) {
  1170. return 1;
  1171. }
  1172. if ($l1[$i] < $l2[$i]) {
  1173. return -1;
  1174. }
  1175. }
  1176. }
  1177. function fput_content_with_lock($file, $string)
  1178. {
  1179. lfile_put_contents($file, $string);
  1180. }
  1181. function filter_object_list($list, $rule)
  1182. {
  1183. $nlist = null;
  1184. foreach ((array) $list as $o) {
  1185. if ($o->eeval($rule)) {
  1186. $nlist[$o->nname] = $o;
  1187. }
  1188. }
  1189. return $nlist;
  1190. }
  1191. function is_assoc_array($var)
  1192. {
  1193. if (!is_array($var)) {
  1194. return false;
  1195. }
  1196. return array_keys($var) !== range(0, sizeof($var) - 1);
  1197. }
  1198. function get_namelist_from_objectlist($ol, $key = null, $val = null)
  1199. {
  1200. if (!$ol) {
  1201. return;
  1202. }
  1203. $name = array();
  1204. if (!$key) {
  1205. $key = "nname";
  1206. }
  1207. if ($val === null) {
  1208. $val = $key;
  1209. }
  1210. foreach ($ol as $o) {
  1211. if (!is_object($o)) {
  1212. debugBacktrace();
  1213. }
  1214. $name[$o->$key] = $o->display($val);
  1215. }
  1216. return $name;
  1217. }
  1218. function convert_to_associate($ar)
  1219. {
  1220. foreach ($ar as $k => $v) $ret[$v] = $v;
  1221. return $ret;
  1222. }
  1223. function get_namelist_from_arraylist($ol, $key = null, $val = null)
  1224. {
  1225. $name = array();
  1226. if (!$key) {
  1227. $key = "nname";
  1228. }
  1229. if ($val === null) {
  1230. $val = $key;
  1231. }
  1232. foreach ((array) $ol as $o) $name[$o[$key]] = $o[$val];
  1233. return $name;
  1234. }
  1235. function isQuotaGreaterThanOrEq($used, $priv)
  1236. {
  1237. if (is_unlimited($priv)) {
  1238. return false;
  1239. }
  1240. if (is_unlimited($used)) {
  1241. return true;
  1242. }
  1243. if (isOn($priv)) {
  1244. return false;
  1245. }
  1246. if (isOn($used)) {
  1247. return true;
  1248. }
  1249. return ($used >= $priv) ? true : false;
  1250. }
  1251. function isQuotaGreaterThan($used, $priv)
  1252. {
  1253. if (is_unlimited($priv)) {
  1254. return false;
  1255. }
  1256. if (is_unlimited($used)) {
  1257. return true;
  1258. }
  1259. if (isOn($priv)) {
  1260. return false;
  1261. }
  1262. if (isOn($used)) {
  1263. return true;
  1264. }
  1265. return ($used > $priv) ? true : false;
  1266. }
  1267. function is_unlimited($val)
  1268. {
  1269. if (strtolower($val) === 'unlimited' || strtolower($val) === 'na') {
  1270. return true;
  1271. }
  1272. return false;
  1273. }
  1274. function if_demo_throw()
  1275. {
  1276. if (if_demo()) {
  1277. throw new lxException ("demo", '');
  1278. }
  1279. }
  1280. function if_demo()
  1281. {
  1282. global $gbl, $sgbl, $g_demo;
  1283. return $g_demo;
  1284. }
  1285. function lx_phpdebug()
  1286. {
  1287. global $gbl, $sgbl;
  1288. if ($sgbl->dbg <= 0) {
  1289. return;
  1290. }
  1291. if (!lfile_exists("/tmp/.php_debug")) {
  1292. return;
  1293. }
  1294. $fp = lfopen("/tmp/.php_debug", "r");
  1295. $s = fgets($fp, 1024);
  1296. fclose($fp);
  1297. $s = preg_replace("/--noraise /i", "", $s);
  1298. $arr = parse_url($s);
  1299. parse_str($arr['query'], $out);
  1300. $ghtml->print_input("hidden", "start_debug", "1");
  1301. $ghtml->print_input("hidden", "debug_port", $out['debug_port']);
  1302. $ghtml->print_input("hidden", "debug_text_mode", "1");
  1303. $ghtml->print_input("hidden", "debug_no_cache", "1095197145");
  1304. }
  1305. function create_simpleObject($arglist)
  1306. {
  1307. $obj = new Remote();
  1308. foreach ($arglist as $k => $v) {
  1309. $obj->$k = $v;
  1310. }
  1311. return $obj;
  1312. }
  1313. function lx_sync()
  1314. {
  1315. global $gbl, $sgbl, $login;
  1316. $login->was();
  1317. }
  1318. function get_current_file()
  1319. {
  1320. $n = basename(dirname($_SERVER['PHP_SELF']));
  1321. return $n;
  1322. }
  1323. function array_flatten($a, $pref = null)
  1324. {
  1325. $ret = array();
  1326. foreach ($a as $i => $j) {
  1327. if (is_array($j)) {
  1328. $ret = array_merge($ret, array_flatten($j, "$pref$i"));
  1329. } else {
  1330. $ret["$pref$i"] = $j;
  1331. }
  1332. }
  1333. return $ret;
  1334. }
  1335. function get_general_image_path($v = null)
  1336. {
  1337. return add_http_host("/img/general/$v");
  1338. }
  1339. /**
  1340. * @return void
  1341. * @param
  1342. * @param
  1343. * @desc part of the getting-image-through-http (to enable caching) madness.
  1344. */
  1345. function add_http_host($elem)
  1346. {
  1347. global $gbl, $sgbl, $login, $ghtml;
  1348. return $elem;
  1349. $host = $_SERVER['SERVER_NAME'];
  1350. //$port = $sgbl->__var_prog_port;
  1351. //$host = "http://" . $host . ":" . "$port";
  1352. //$host = "https://" . $host . ":" . "$port";
  1353. return $host . $elem;
  1354. }
  1355. function get_image_path($path = null)
  1356. {
  1357. global $gbl, $sgbl;
  1358. global $gbl, $sgbl, $login;
  1359. //Return path of the encrypted images in the deployment version.
  1360. return "/img/image/{$login->getSpecialObject('sp_specialplay')->icon_name}/$path";
  1361. }
  1362. function randomString($length)
  1363. {
  1364. $randstr = '';
  1365. $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
  1366. for ($rand = 0; $rand <= $length; $rand++) {
  1367. $random = rand(0, count($chars) - 1);
  1368. $randstr .= $chars[$random];
  1369. }
  1370. return $randstr;
  1371. }
  1372. function DBG_GetBacktrace($traceArr)
  1373. {
  1374. if ($sgbl->dbg < 0) {
  1375. return;
  1376. }
  1377. $s = '';
  1378. $MAXSTRLEN = 64;
  1379. $s = '<pre align=left>';
  1380. array_shift($traceArr);
  1381. $tabs = sizeof($traceArr) - 1;
  1382. foreach ($traceArr as $arr) {
  1383. for ($i = 0; $i < $tabs; $i++) {
  1384. $s .= ' &nbsp; ';
  1385. }
  1386. $tabs -= 1;
  1387. $s .= '<font face="Courier New,Courier">';
  1388. if (isset($arr['class'])) {
  1389. $s .= $arr['class'] . '.';
  1390. }
  1391. $args = array();
  1392. foreach ((array) $arr['args'] as $v) {
  1393. if (is_null($v)) {
  1394. $args[] = 'null';
  1395. } else {
  1396. if (is_array($v)) {
  1397. $args[] = 'Array[' . sizeof($v) . ']';
  1398. } else {
  1399. if (is_object($v)) {
  1400. $args[] = 'Object:' . get_class($v);
  1401. } else {
  1402. if (is_bool($v)) {
  1403. $args[] = $v ? 'true' : 'false';
  1404. } else {
  1405. $v = (string) @$v;
  1406. $str = htmlspecialchars(substr($v, 0, $MAXSTRLEN));
  1407. if (strlen($v) > $MAXSTRLEN) $str .= '...';
  1408. $args[] = "\"" . $str . "\"";
  1409. }
  1410. }
  1411. }
  1412. }
  1413. }
  1414. $s .= $arr['function'] . '(' . implode(',
  1415. ', $args) . ')</font>';
  1416. $Line = (isset($arr['line']) ? $arr['line'] : "unknown");
  1417. $File = (isset($arr['file']) ? $arr['file'] : "unknown");
  1418. $s .= sprintf("<font color=#808080 size=-1> # line
  1419. %4d, file: <a href=\"file:/%s\">%s</a></font>", $Line, $File, $File);
  1420. $s .= "\n";
  1421. }
  1422. $s .= '</pre>';
  1423. return $s;
  1424. }
  1425. function getInfo($b)
  1426. {
  1427. if (is_object($b) && is_subclass_of($b, 'lxclass')) {
  1428. return $b->get__table() . ':' . $b->nname;
  1429. } else {
  1430. return $b;
  1431. }
  1432. }
  1433. function backtrace_once()
  1434. {
  1435. global $gbl, $sgbl, $login, $ghtml;
  1436. if ($sgbl->dbg < 2) {
  1437. return;
  1438. }
  1439. $v = debug_backtrace();
  1440. $count = 0;
  1441. $string = null;
  1442. foreach ($v as $q) {
  1443. $count++;
  1444. if ($count === 1) {
  1445. continue;
  1446. }
  1447. if ($count > 2) break;
  1448. if ($count === 2 && (basename($q['file']) === 'sqlite.php')) {
  1449. return null;
  1450. continue;
  1451. }
  1452. $string .= $q['file'] . ":" . $q['line'] . ": " . $q['function'] . '(';
  1453. if (isset($q['args'])) {
  1454. foreach ($q['args'] as $a) {
  1455. if (is_array($a)) {
  1456. foreach ($a as $b) {
  1457. if (is_array($b)) {
  1458. foreach ($b as $c) {
  1459. $string .= ', ' . getInfo($c);
  1460. }
  1461. } else {
  1462. $string .= ', ' . getInfo($b);
  1463. }
  1464. }
  1465. } else {
  1466. $string .= $a;
  1467. }
  1468. }
  1469. }
  1470. $string .= ")<br>\n";
  1471. }
  1472. return $string;
  1473. }
  1474. function debugBacktrace($flag = false)
  1475. {
  1476. global $gbl, $sgbl, $login, $ghtml;
  1477. $string = null;
  1478. if ($sgbl->dbg < 2) {
  1479. return;
  1480. }
  1481. $v = debug_backtrace();
  1482. foreach ($v as $q) {
  1483. $string .= $q['file'] . ":" . $q['line'] . ": " . $q['function'] . '(';
  1484. if (isset($q['args'])) {
  1485. foreach ($q['args'] as $a) {
  1486. if (is_array($a)) {
  1487. foreach ($a as $b) {
  1488. if (is_array($b)) {
  1489. foreach ($b as $c) {
  1490. $string .= ', ' . getInfo($c);
  1491. }
  1492. } else {
  1493. $string .= ', ' . getInfo($b);
  1494. }
  1495. }
  1496. } else {
  1497. if (is_string($a)) {
  1498. $string .= $a;
  1499. }
  1500. }
  1501. }
  1502. }
  1503. $string .= ")<br>\n";
  1504. }
  1505. if ($flag) {
  1506. return $string;
  1507. }
  1508. dprintr($string);
  1509. }
  1510. function lx_strip_tags($str)
  1511. {
  1512. $nstr = strip_tags($str);
  1513. $nstr = preg_replace("/\s+/", " ", $nstr);
  1514. return $nstr;
  1515. }
  1516. class Language_Mes
  1517. {
  1518. }
  1519. function get_language()
  1520. {
  1521. global $gbl, $sgbl, $login, $ghtml;
  1522. if (is_object($login) && isset($login->getSpecialObject('sp_specialplay')->language)) {
  1523. $lan = $login->getSpecialObject('sp_specialplay')->language;
  1524. } else {
  1525. $lan = 'en';
  1526. }
  1527. return $lan;
  1528. }
  1529. function get_charset()
  1530. {
  1531. $lang = get_language();
  1532. $charset = @ lfile_get_contents("lang/$lang/charset");
  1533. $charset = trim($charset);
  1534. return $charset;
  1535. }
  1536. function print_meta_lan()
  1537. {
  1538. global $gbl, $sgbl, $login, $ghtml;
  1539. $lan = get_language();
  1540. $charset = @ lfile_get_contents("lang/$lan/charset");
  1541. $charset = trim($charset);
  1542. print("<head>");
  1543. if ($charset) {
  1544. print("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=$charset\" />");
  1545. } else {
  1546. print("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
  1547. }
  1548. }
  1549. function init_language()
  1550. {
  1551. global $gbl, $sgbl, $login, $ghtml;
  1552. global $g_language_mes, $g_language_desc;
  1553. $language = get_language();
  1554. if (lxfile_exists("lang/$language/messagelib.php")) {
  1555. include_once("lang/$language/messagelib.php");
  1556. } else {
  1557. include_once("lang/en/messagelib.php");
  1558. }
  1559. if (lxfile_exists("lang/$language/langfunctionlib.php")) {
  1560. include_once("lang/$language/langfunctionlib.php");
  1561. } else {
  1562. include_once("lang/en/langfunctionlib.php");
  1563. }
  1564. if (lxfile_exists("lang/$language/langkeywordlib.php")) {
  1565. include_once("lang/$language/langkeywordlib.php");
  1566. } else {
  1567. include_once("lang/en/langkeywordlib.php");
  1568. }
  1569. if (lxfile_exists("lang/$language/desclib.php")) {
  1570. include_once("lang/$language/desclib.php");
  1571. } else {
  1572. include_once("lang/en/desclib.php");
  1573. }
  1574. include_once("htmllib/lib/commonmessagelib.php");
  1575. $g_language_mes = new Language_Mes();
  1576. $g_language_mes->__information = $__information;
  1577. $g_language_mes->__emessage = $__emessage;
  1578. $g_language_mes->__keyword = $__keyword;
  1579. $g_language_mes->__help = $__help;
  1580. $g_language_mes->__helpvar = $__helpvar;
  1581. $g_language_mes->__commonhelp = $g_commonhelp;
  1582. $g_language_desc = new Remote();
  1583. $g_language_desc->__description = $__description;
  1584. }
  1585. function lx_error_handler($errno, $errstr, $file, $line)
  1586. {
  1587. global $gbl, $sgbl, $login, $ghtml;
  1588. global $last_error;
  1589. $last_error = $errstr;
  1590. if ($sgbl->dbg < 0) {
  1591. return;
  1592. }
  1593. if (error_reporting() === 0) {
  1594. return;
  1595. }
  1596. static $error = "";
  1597. $pos = "$file:$line: $errstr";
  1598. $error .= $pos . "\n";
  1599. lfile_put_contents($sgbl->__var_error_file, $error);
  1600. dprint("\n### PHP Error detected\n");
  1601. dprint("### Notice: $errstr\n");
  1602. dprint("### File:$file\n");
  1603. dprint("### Line number: $line\n");
  1604. dprint("### End PHP Error information\n\n");
  1605. }
  1606. /**
  1607. * @return void
  1608. * @param
  1609. * @param
  1610. * @desc Truly random and insane way to encrypt the image/form names so that hackers won't easily understand teh program structure, (which is clearly visible in these names...)
  1611. */
  1612. function createEncName($name)
  1613. {
  1614. global $gbl;
  1615. return $name;
  1616. if ($sgbl->dbg > 0) {
  1617. return $name;
  1618. }
  1619. $name = str_replace("_", "", $name);
  1620. $name = str_replace("php", "", $name);
  1621. $name = str_replace("a", "r", $name);
  1622. $name = str_replace("e", "z", $name);
  1623. $name = str_replace("i", "q", $name);
  1624. $name = str_replace("o", "j", $name);
  1625. $name = str_replace("t", "y", $name);
  1626. $name = str_replace("s", "x", $name);
  1627. $name = str_replace("r", "p", $name);
  1628. return $name;
  1629. }
  1630. function check_password($unenc, $enc)
  1631. {
  1632. //Old Stuff Not reached
  1633. if (crypt($unenc, $enc) === $enc) {
  1634. return true;
  1635. }
  1636. return false;
  1637. }
  1638. function lx_exception_handler($e)
  1639. {
  1640. global $gbl, $sgbl, $login, $ghtml;
  1641. print("Notice : The resource you have requested doesn't exist. The server returned the error message: <br> ");
  1642. print(" {$e->getMessage()} $e->variable $e->value ");
  1643. print("<br>\n\n");
  1644. if ($sgbl->dbg <= 0) {
  1645. return;
  1646. }
  1647. $tr = $e->getTrace();
  1648. $trace = "";
  1649. foreach ($tr as $a) {
  1650. $trace .= $a["file"] . ":" . $a["line"] . ":";
  1651. if (isset($a["class"])) {
  1652. $trace .= $a["class"] . ":";
  1653. }
  1654. $trace .= $a["function"] . "(";
  1655. $trace .= ")\n";
  1656. }
  1657. lfile_put_contents($sgbl->__var_error_file, $trace);
  1658. }
  1659. function check_raw_password($class, $client, $pass)
  1660. {
  1661. //return true;
  1662. if (!$class || !$client || !$pass) {
  1663. return false;
  1664. }
  1665. $rawdb = new Sqlite(null, $class);
  1666. $password = $rawdb->rawquery("select password from $class where nname = '$client'");
  1667. $enp = $password[0]['password'];
  1668. if ($enp && check_password($pass, $enp)) {
  1669. return true;
  1670. }
  1671. return false;
  1672. //$rawdb->close();
  1673. }
  1674. /**
  1675. * @return void
  1676. * @param
  1677. * @param
  1678. * @desc Checks if the client is disabled and exits immedeiately showing a message.
  1679. */
  1680. function check_if_disabled_and_exit()
  1681. {
  1682. global $gbl, $sgbl, $login, $ghtml;
  1683. $contact = "administrator";
  1684. if (!$login->isOn('cpstatus')) {
  1685. Utmp::updateUtmp($gbl->c_session->nname, $login, 'disabled');
  1686. $ghtml->print_css_source("/htmllib/css/common.css");
  1687. if ($sgbl->isLxlabsClient()) {
  1688. $ghtml->__http_vars['frm_emessage'] = "This login has been Disabled due to non-payment. Please pay the invoice below, and your account will automatically get enabled.";
  1689. $ghtml->print_message();
  1690. $login->print_invoice();
  1691. } else {
  1692. $ghtml->__http_vars['frm_emessage'] = "This login has been Disabled. Please contact the $contact";
  1693. $ghtml->print_message();
  1694. }
  1695. $gbl->c_session->delete();
  1696. $gbl->c_session->was();
  1697. exit(0);
  1698. }
  1699. }
  1700. function delete_expired_ssessions()
  1701. {
  1702. global $gbl, $sgbl, $login, $ghtml;
  1703. $s_l = $login->getList("ssessionlist");
  1704. foreach ($s_l as $s) {
  1705. if (!is_object($s)) {
  1706. continue;
  1707. }
  1708. $timeout = $s->last_access + $login->getSpecialObject('sp_specialplay')->ssession_timeout;
  1709. dprint($s->nname);
  1710. if ($timeout < time()) {
  1711. $s->delete();
  1712. Utmp::updateUtmp($s->nname, $login, "Session Expired");
  1713. }
  1714. }
  1715. }
  1716. function createTreeObject($name, $img, $imgstr, $url, $open, $help, $alt)
  1717. {
  1718. static $val;
  1719. $imgstr = str_replace("'", "\'", $imgstr);
  1720. $help = str_replace("'", "\'", $help);
  1721. $alt = str_replace("'", "\'", $alt);
  1722. $img = str_replace("'", "\'", $img);
  1723. $val++;
  1724. $name = $name . $val;
  1725. $tobj = new Tree(null, null, $name);
  1726. $tobj->img = $img;
  1727. $tobj->imgstr = $imgstr;
  1728. $tobj->url = $url;
  1729. $tobj->open = $open;
  1730. $tobj->help = $help;
  1731. $tobj->alt = $alt;
  1732. return $tobj;
  1733. }
  1734. /**
  1735. * @return void
  1736. * @param
  1737. * @param
  1738. * @desc A generic function, that can be used by all programs. Does all the basic login stuff.
  1739. */
  1740. function initProgramlib($ctype = null)
  1741. {
  1742. global $gbl, $sgbl, $login, $ghtml;
  1743. if ($sgbl->is_this_slave()) {
  1744. print("This is a Slave Server. Operate it at the Master server.\n");
  1745. exit;
  1746. }
  1747. static $var = 0;
  1748. $var++;
  1749. $progname = $sgbl->__var_program_name;
  1750. lfile_put_contents($sgbl->__var_error_file, "");
  1751. set_exception_handler("lx_exception_handler");
  1752. //xdebug_disable();
  1753. set_error_handler("lx_error_handler");
  1754. //setcookie("XDEBUG_SESSION", "sess");
  1755. if ($var >= 2) {
  1756. dprint("initProgramlib called twice \n <br> ");
  1757. }
  1758. if ($ctype === 'superadmin') {
  1759. $sgbl->__var_dbf = $sgbl->__path_supernode_db;
  1760. $sgbl->__path_admin_pass = $sgbl->__path_super_pass;
  1761. $sgbl->__var_admin_user = $sgbl->__var_super_user;
  1762. $login = new SuperClient(null, null, 'superadmin', 'login', 'forced');
  1763. $login->get();
  1764. return;
  1765. } else if ($ctype === "guest") {
  1766. $login = new Client(null, null, "____________", "guest");
  1767. $login->get();
  1768. return;
  1769. } else if ($ctype != "") {
  1770. $login = new Client(null, null, $ctype, "login", "forced");
  1771. $login->get();
  1772. return;
  1773. }
  1774. $sessobj = null;
  1775. if ($ghtml->frm_consumedlogin === 'true') {
  1776. $clientname = $_COOKIE["$progname-consumed-clientname"];
  1777. $classname = $_COOKIE["$progname-consumed-classname"];
  1778. $session_id = $_COOKIE["$progname-consumed-session-id"];
  1779. get_login($classname, $clientname);
  1780. $login->__session_id = $session_id;
  1781. $sessobj = $login->getObject('ssession');
  1782. } else {
  1783. if (isset($_COOKIE["$progname-session-id"])) {
  1784. $clientname = $_COOKIE["$progname-clientname"];
  1785. $classname = $_COOKIE["$progname-classname"];
  1786. $session_id = $_COOKIE["$progname-session-id"];
  1787. if ($classname === 'superclient') {
  1788. $sgbl->__var_dbf = $sgbl->__path_supernode_db;
  1789. $sgbl->__path_admin_pass = $sgbl->__path_super_pass;
  1790. $sgbl->__var_admin_user = $sgbl->__var_super_user;
  1791. }
  1792. if ($classname === 'slave') {
  1793. $sgbl->__var_dbf = $sgbl->__path_slave_db;
  1794. }
  1795. if ($classname) {
  1796. get_login($classname, $clientname);
  1797. $login->__session_id = $session_id;
  1798. $sessobj = $login->getObject('ssession');
  1799. }
  1800. }
  1801. }
  1802. if (!$sessobj || $sessobj->dbaction === 'add') {
  1803. if ($ghtml->frm_ssl) {
  1804. $ssl = unserialize(base64_decode($ghtml->frm_ssl));
  1805. $string = $ssl['string'];
  1806. $ssl_param = $ssl['ssl_param'];
  1807. $encrypted_string = base64_decode($ssl['encrypted_string']);
  1808. if (!$string || !checkPublicKey($string, $encrypted_string)) {
  1809. print("SSL Connection Failed <br> \n");
  1810. exit;
  1811. }
  1812. $class = 'client';
  1813. $clientname = 'admin';
  1814. get_login($class, $clientname);
  1815. do_login($class, $clientname, $ssl_param);
  1816. $sessobj = $gbl->c_session;
  1817. $sessobj->write();
  1818. $sessobj->dbaction = 'clean';
  1819. }
  1820. }
  1821. //get_savedlogin($classname, $clientname);
  1822. //print_time('login_get', "Login Get");
  1823. //dprintr($login);
  1824. //avoid some php warnings
  1825. if (isset($login)) {
  1826. $gbl->client = $login->nname;
  1827. $gbl->client_ttype = $login->cttype;
  1828. }
  1829. //dprintr($login->hpfilter);
  1830. // This means the session object got created fresh.
  1831. if (!$sessobj || $sessobj->dbaction === 'add') {
  1832. dprint("no session id");
  1833. clear_all_cookie();
  1834. $ghtml->print_redirect_self("/login/");
  1835. }
  1836. $gbl->c_session = $sessobj;
  1837. if ($login->getClName() !== $sessobj->parent_clname) {
  1838. dprint_r($login->ssession_l);
  1839. dprint(" <br> $session_id <br> <br> <br> ");
  1840. print("Session error! Login again.");
  1841. clear_all_cookie();
  1842. $ghtml->print_redirect_self("/login/?frm_emessage=sessionname_not_client");
  1843. }
  1844. $gen = $login->getObject('general')->generalmisc_b;
  1845. if (!$gen->isOn('disableipcheck') && $_SERVER['REMOTE_ADDR'] != $sessobj->ip_address) {
  1846. $hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
  1847. log_message("An attempt to hack from $hostname (" . $_SERVER['REMOTE_ADDR'] . ") with sess $sessobj->nname, session ip: $sessobj->ip_address");
  1848. if ($gen->isOn('disableipcheck')) {
  1849. } else {
  1850. clear_all_cookie();
  1851. if ($gbl->c_session->ssl_param) {
  1852. $sessobj->delete();
  1853. $sessobj->write();
  1854. $ghtml->print_redirect("{$gbl->c_session->ssl_param['backurl']}&frm_emessage=ipaddress_changed_amidst_session");
  1855. } else {
  1856. $ghtml->print_redirect_self("/login/?frm_emessage=ipaddress_changed_amidst_session");
  1857. }
  1858. }
  1859. }
  1860. if (intval($login->getSpecialObject('sp_specialplay')->ssession_timeout) <= 100) {
  1861. $login->getSpecialObject('sp_specialplay')->ssession_timeout = 100;
  1862. $login->setUpdateSubaction();
  1863. }
  1864. $timeout = $sessobj->last_access + $login->getSpecialObject('sp_specialplay')->ssession_timeout;
  1865. $sessobj->timeout = $timeout;
  1866. //$timeout = $sessobj->last_access + 4;
  1867. $sessobj->last_access = time();
  1868. $sessobj->setUpdateSubaction();
  1869. if ($sessobj->auxiliary_id) {
  1870. $aux = new Auxiliary(null, null, $sessobj->auxiliary_id);
  1871. $aux->get();
  1872. $login->__auxiliary_object = $aux;
  1873. }
  1874. if (time() > $timeout) {
  1875. $sessobj->delete();
  1876. $sessobj->write();
  1877. //print("session error timeout");
  1878. if ($gbl->c_session->ssl_param) {
  1879. $ghtml->print_redirect("{$gbl->c_session->ssl_param['backurl']}&frm_emessage=session_timeout");
  1880. } else {
  1881. $ghtml->print_redirect_self("/login/?frm_emessage=session_timeout");
  1882. }
  1883. }
  1884. addToUtmp($sessobj, 'update');
  1885. }
  1886. function clear_all_cookie()
  1887. {
  1888. global $gbl, $sgbl, $login, $ghtml;
  1889. $progname = $sgbl->__var_program_name;
  1890. $search = $progname;
  1891. if ($ghtml->frm_consumedlogin === 'true') {
  1892. $search .= "-consumed";
  1893. }
  1894. foreach ($_COOKIE as $k => $v) {
  1895. if (csb($k, $search)) {
  1896. setcookie($k, "", time() - 360000000);
  1897. }
  1898. }
  1899. }
  1900. function checkPublicKey($string, $encrypted_string)
  1901. {
  1902. $res = lfile_get_contents("__path_program_root/etc/authorized_keys");
  1903. $public_key = $res;
  1904. $pubkey_res = openssl_get_publickey($public_key);
  1905. openssl_public_decrypt($encrypted_string, $decrypted_string, $public_key);
  1906. if ($decrypted_string === $string) {
  1907. return true;
  1908. }
  1909. return false;
  1910. }
  1911. function initSession($object, $ssl_param, $consuming_parent)
  1912. {
  1913. global $gbl, $sgbl, $login, $ghtml;
  1914. $progname = $sgbl->__var_program_name;
  1915. $session = randomString(50);
  1916. //clear_all_cookie();
  1917. // Making cookie persistent, otherwise IE will not pass it to new windows. Needed in the file selector. Set the expiration to 10 years in the future. Needed for brain damaged ie, which cannot recognize server time.
  1918. $cookietime = time() + 24 * 60 * 60 * 365 * 80;
  1919. //$cookietime = 0;
  1920. header('P3P: CP="CAO PSA OUR"');
  1921. $ckstart = $progname;
  1922. if ($consuming_parent) {
  1923. $ckstart .= "-consumed";
  1924. }
  1925. if ($object->isAuxiliary()) {
  1926. $name = $object->__auxiliary_object->nname;
  1927. $class = $object->__auxiliary_object->getClass();
  1928. } else {
  1929. $name = $object->nname;
  1930. $class = $object->getClass();
  1931. }
  1932. setcookie("$ckstart-clientname", $name, $cookietime, '/');
  1933. setcookie("$ckstart-classname", $class, $cookietime, '/');
  1934. setcookie("$ckstart-session-id", $session, $cookietime, '/');
  1935. dprint("Set cookies\n");
  1936. $hostname = $_SERVER['REMOTE_ADDR'];
  1937. $sessobj = new Ssession(null, null, $session);
  1938. $sessa['nname'] = $session;
  1939. $sessa['ip_address'] = $_SERVER['REMOTE_ADDR'];
  1940. $sessa['cttype'] = $object->getLoginType();
  1941. $sessa['hostname'] = $hostname;
  1942. $sessa['tsessionid'] = randomString(30);
  1943. // Login time is set to null. This is set to the correct time inside display.php. This allows us to figure out the first run of display.php.
  1944. $sessa['logintime'] = time();
  1945. $sessa['ssession_vars'] = array();
  1946. $sessa['http_vars'] = array();
  1947. $sessa['parent_clname'] = $object->getClName();
  1948. $sessa['consuming_parent'] = $consuming_parent;
  1949. $sessa['auxiliary_id'] = $object->getAuxiliaryId();
  1950. $sessa['ssl_param'] = $ssl_param;
  1951. if (intval($object->getSpecialObject('sp_specialplay')->ssession_timeout) <= 100) {
  1952. $timeout = 100;
  1953. } else {
  1954. $timeout = $object->getSpecialObject('sp_specialplay')->ssession_timeout;
  1955. }
  1956. $sessa['timeout'] = time() + $timeout;
  1957. $sessa['last_access'] = time();
  1958. $sessobj->create($sessa);
  1959. return $sessobj;
  1960. }
  1961. function do_login($classname, $cgi_clientname, $ssl_param = null)
  1962. {
  1963. global $gbl, $sgbl, $login, $ghtml;
  1964. $url = "/display.php?frm_action=show";
  1965. $progname = $sgbl->__var_program_name;
  1966. if (!$classname) {
  1967. $classname = 'client';
  1968. }
  1969. $sessobj = initSession($login, $ssl_param, null);
  1970. $gbl->c_session = $sessobj;
  1971. $login->addToList("ssession", $sessobj);
  1972. $login->createSessionProperties();
  1973. addToUtmp($sessobj, 'add');
  1974. if ($ghtml->frm_extra_var) {
  1975. $extra = unserialize(base64_decode($ghtml->frm_extra_var));
  1976. $gbl->setSessionV('extra_var', $extra);
  1977. }
  1978. //This is not the way. You have to periodically scan the utmp and delete eveyrthing that had expired.
  1979. delete_expired_ssessions();
  1980. }
  1981. function ifSplashScreen()
  1982. {
  1983. global $gbl, $sgbl, $login, $ghtml;
  1984. if (array_search_bool(strtolower($ghtml->frm_action), array('show', 'updateform', 'addform', 'continue'))) {
  1985. return true;
  1986. }
  1987. return false;
  1988. }
  1989. function isModifyAction()
  1990. {
  1991. global $gbl, $sgbl, $login, $ghtml;
  1992. if (array_search_bool(strtolower($ghtml->frm_action), array('show', 'updateform', 'addform', 'continue', 'list')) || array_search_bool($ghtml->frm_subaction, array("download"))) {
  1993. return false;
  1994. }
  1995. return true;
  1996. }
  1997. function lget_class($object)
  1998. {