PageRenderTime 35ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/kloxo/httpdocs/htmllib/phplib/lxlib.php

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