api.yalfcore.php 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. <?php
  2. /**
  3. * Primary Y.A.L.F core class that implements core functionality
  4. */
  5. class YALFCore {
  6. /**
  7. * Contains raw YALF primary config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $config = array();
  12. /**
  13. * Contains names of libs to load as path=>layer
  14. *
  15. * @var array
  16. */
  17. protected $loadLibs = array();
  18. /**
  19. * Name of module which will be used as main route
  20. *
  21. * @var string
  22. */
  23. protected $indexModule = 'index';
  24. /**
  25. * Current skin name
  26. *
  27. * @var string
  28. */
  29. protected $skin = 'paper';
  30. /**
  31. * Default language name
  32. *
  33. * @var string
  34. */
  35. protected $language = 'english';
  36. /**
  37. * Application renderer type. Can be WEB/CLI at this moment
  38. *
  39. * @var string
  40. */
  41. protected $renderer = 'WEB';
  42. /**
  43. * Contains page title here
  44. *
  45. * @var string
  46. */
  47. protected $pageTitle = '';
  48. /**
  49. * Is global menu rendering enabled flag
  50. *
  51. * @var bool
  52. */
  53. protected $globalMenuEnabled = false;
  54. /**
  55. * Contains modules preloaded from general modules directory
  56. *
  57. * @var array
  58. */
  59. protected $modules = array();
  60. /**
  61. * Contains all rights injected with startup modules initialization
  62. *
  63. * @var array
  64. */
  65. protected $rights_database = array();
  66. /**
  67. * Is now some user logged in flag
  68. *
  69. * @var bool
  70. */
  71. protected $loggedIn = false;
  72. /**
  73. * Have current user root rights?
  74. *
  75. * @var bool
  76. */
  77. protected $root = false;
  78. /**
  79. * This array contain data from user's profile
  80. *
  81. * @var array
  82. */
  83. protected $user = array();
  84. /**
  85. * Contains current user rights
  86. *
  87. * @var array
  88. */
  89. protected $rights = array();
  90. /**
  91. * Some mystic output buffer. Used in i18n, users auth etc.
  92. *
  93. * @var array
  94. */
  95. protected $results = array();
  96. /**
  97. * Name of default auth cookie. May be configurable in future.
  98. *
  99. * @var string
  100. */
  101. protected $cookie_user = 'yalf_user';
  102. /**
  103. * Name of default auth ghost-mode cookie. May be configurable in future.
  104. *
  105. * @var string
  106. */
  107. protected $cookie_ghost = 'yalf_ghost';
  108. /**
  109. * Name of default user defined locale. May be configurable in future.
  110. *
  111. * @var string
  112. */
  113. protected $cookie_locale = 'yalf_lang';
  114. /**
  115. * System athorization enable flag
  116. *
  117. * @var bool
  118. */
  119. protected $authEnabled = false;
  120. /**
  121. * System logging engine
  122. *
  123. * @var string
  124. */
  125. protected $logType = 'fake';
  126. /**
  127. * Database logs table
  128. *
  129. * @var string
  130. */
  131. protected $logTable = 'weblogs';
  132. /**
  133. * Default system log file path. May be configurable in future.
  134. *
  135. * @var string
  136. */
  137. protected $logFilePath = 'content/logs/yalflog.log';
  138. /**
  139. * Is live-locale switching allowed flag
  140. *
  141. * @var bool
  142. */
  143. protected $langSwitchAllowed = false;
  144. /**
  145. * Contains names of pre-loaded modules as modulename=>title
  146. *
  147. * @var array
  148. */
  149. protected $loadableModules = array();
  150. /**
  151. * Contains list of modules which not require any authorization (public modules)
  152. *
  153. * @var array
  154. */
  155. protected $noAuthModules = array();
  156. /**
  157. * Some paths, routes etc
  158. */
  159. const YALF_CONF_PATH = 'config/yalf.ini';
  160. const YALF_MENU_PATH = 'config/globalmenu.ini';
  161. const LIBS_PATH = 'api/libs/';
  162. const LANG_PATH = 'languages/';
  163. const MODULE_CODE_NAME = 'index.php';
  164. const MODULE_DEFINITION = 'module.php';
  165. const ROUTE_MODULE_LOAD = 'module';
  166. const SKINS_PATH = 'skins/';
  167. const MENU_ICONS_PATH = 'skins/menuicons/';
  168. const DEFAULT_ICON = 'defaulticon.png';
  169. const SKIN_TEMPLATE_NAME = 'template.html';
  170. /**
  171. * Creates new system core instance
  172. */
  173. public function __construct() {
  174. $this->loadConfig();
  175. $this->performUserAuth();
  176. $this->initializeUser();
  177. $this->initializeModules();
  178. $this->setOptions();
  179. $this->switchIndexModule();
  180. }
  181. /**
  182. * Loads framework primary config into protected property for further usage
  183. *
  184. * @return void
  185. */
  186. protected function loadConfig() {
  187. $this->config = parse_ini_file(self::YALF_CONF_PATH);
  188. }
  189. /**
  190. * Checks is module path valid and loadable?
  191. *
  192. * @param string $moduleName
  193. *
  194. * @return bool
  195. */
  196. protected function isModuleValid($moduleName) {
  197. $result = false;
  198. $moduleName = preg_replace('/\0/s', '', $moduleName);
  199. if (!empty($moduleName)) {
  200. //already preloaded from filesystem
  201. if (isset($this->loadableModules[$moduleName])) {
  202. //check for module dir
  203. if (file_exists(MODULES_PATH . $moduleName)) {
  204. //check for module codepart
  205. if (file_exists(MODULES_PATH . $moduleName . '/' . self::MODULE_CODE_NAME)) {
  206. //check for module definition
  207. if (file_exists(MODULES_PATH . $moduleName . '/' . self::MODULE_DEFINITION)) {
  208. $result = true;
  209. }
  210. }
  211. }
  212. }
  213. }
  214. return ($result);
  215. }
  216. /**
  217. * Preprocess some options an sets internal props for further usage
  218. *
  219. * @return void
  220. */
  221. protected function setOptions() {
  222. //library layers preloading
  223. if (!empty($this->config)) {
  224. foreach ($this->config as $eachOption => $eachValue) {
  225. if (ispos($eachOption, 'LAYER_')) {
  226. if (!empty($eachValue)) {
  227. $requirements = explode(',', $eachValue);
  228. if (!empty($requirements)) {
  229. foreach ($requirements as $io => $eachLib) {
  230. $libPath = self::LIBS_PATH . 'api.' . $eachLib . '.php';
  231. if (!file_exists($libPath)) {
  232. die('Library ' . $libPath . ' required for loading of feature layer ' . $eachOption . ' is not exists!');
  233. } else {
  234. $this->loadLibs[$libPath] = $eachOption;
  235. }
  236. }
  237. }
  238. }
  239. }
  240. }
  241. }
  242. //initial index module setup
  243. if (isset($this->config['INDEX_MODULE'])) {
  244. if (!empty($this->config['INDEX_MODULE'])) {
  245. if ($this->isModuleValid($this->config['INDEX_MODULE'])) {
  246. $this->indexModule = $this->config['INDEX_MODULE'];
  247. } else {
  248. die('Module code ' . MODULES_PATH . $this->config['INDEX_MODULE'] . '/' . self::MODULE_CODE_NAME . ' set in INDEX_MODULE is not exists!');
  249. }
  250. }
  251. }
  252. //template selection
  253. if (isset($this->config['YALF_SKIN'])) {
  254. if (!empty($this->config['YALF_SKIN'])) {
  255. $this->skin = $this->config['YALF_SKIN'];
  256. if (!file_exists(self::SKINS_PATH . $this->skin . '/' . self::SKIN_TEMPLATE_NAME)) {
  257. die('Template code not found ' . self::SKINS_PATH . $this->skin . '/' . self::SKIN_TEMPLATE_NAME . ' set in YALF_SKIN');
  258. }
  259. }
  260. }
  261. //default locale selection
  262. if (isset($this->config['YALF_LANG'])) {
  263. //setting default locale
  264. if (!empty($this->config['YALF_LANG'])) {
  265. $this->language = $this->config['YALF_LANG'];
  266. }
  267. }
  268. //locale switching if allowed
  269. if (isset($this->config['YALF_LANG_SWITCHABLE'])) {
  270. if ($this->config['YALF_LANG_SWITCHABLE']) {
  271. //setup of flag
  272. $this->langSwitchAllowed = true;
  273. //setting new locale on GET request
  274. if (isset($_GET['yalfswitchlocale'])) {
  275. $rawLocale = $_GET['yalfswitchlocale'];
  276. $customLocale = preg_replace('/\0/s', '', $rawLocale);
  277. $customLocale = preg_replace("#[^a-z0-9A-Z]#Uis", '', $customLocale);
  278. if (!empty($customLocale)) {
  279. if (file_exists(self::LANG_PATH . $customLocale)) {
  280. $this->language = $customLocale;
  281. setcookie($this->cookie_locale, $customLocale, time() + 2592000);
  282. $currentUrlCallback = $_SERVER['REQUEST_URI'];
  283. $currentUrlCallback = str_replace('&yalfswitchlocale=' . $rawLocale, '', $currentUrlCallback);
  284. $currentUrlCallback = str_replace('?yalfswitchlocale=' . $rawLocale, '', $currentUrlCallback);
  285. rcms_redirect($currentUrlCallback, true); //back to the same URL witchout switch param
  286. }
  287. }
  288. }
  289. //some custom locale already set
  290. if (@$_COOKIE[$this->cookie_locale]) {
  291. if (is_string($_COOKIE[$this->cookie_locale])) {
  292. $customLocale = preg_replace('/\0/s', '', $_COOKIE[$this->cookie_locale]);
  293. $customLocale = preg_replace("#[^a-z0-9A-Z]#Uis", '', $customLocale);
  294. if (!empty($customLocale)) {
  295. if (file_exists(self::LANG_PATH . $customLocale)) {
  296. $this->language = $customLocale;
  297. }
  298. }
  299. }
  300. }
  301. }
  302. }
  303. //page title setup
  304. if (isset($this->config['YALF_TITLE'])) {
  305. if (!empty($this->config['YALF_TITLE'])) {
  306. $this->pageTitle = $this->config['YALF_TITLE'];
  307. }
  308. }
  309. //global menu rendering flag setup
  310. if (isset($this->config['YALF_MENU_ENABLED'])) {
  311. if ($this->config['YALF_MENU_ENABLED']) {
  312. $this->globalMenuEnabled = true;
  313. }
  314. }
  315. //system auth enabled
  316. if (isset($this->config['YALF_AUTH_ENABLED'])) {
  317. if ($this->config['YALF_AUTH_ENABLED']) {
  318. $this->authEnabled = true;
  319. }
  320. }
  321. //system logging settings
  322. if (isset($this->config['YALF_LOGGING_TYPE'])) {
  323. $this->logType = $this->config['YALF_LOGGING_TYPE'];
  324. if (isset($this->config['YALF_LOG_TABLE'])) {
  325. $this->logTable = $this->config['YALF_LOG_TABLE'];
  326. }
  327. }
  328. //no auth public modules
  329. if (isset($this->config['YALF_NO_AUTH_MODULES'])) {
  330. if (!empty($this->config['YALF_NO_AUTH_MODULES'])) {
  331. $this->noAuthModules = explode(',', $this->config['YALF_NO_AUTH_MODULES']);
  332. $this->noAuthModules = array_flip($this->noAuthModules); //use module name as index
  333. }
  334. }
  335. //renderer type detection
  336. if (isset($this->config['LAYER_CLIRENDER'])) {
  337. $this->renderer = 'CLI';
  338. }
  339. if (isset($this->config['LAYER_WEBRENDER'])) {
  340. $this->renderer = 'WEB';
  341. }
  342. }
  343. /**
  344. * Switches index(current) module if its required
  345. *
  346. * @return void
  347. */
  348. protected function switchIndexModule() {
  349. $forceLoginForm = false; //show login form module instead any of called
  350. //user is not authorized now and auth engine enabled
  351. if (!$this->loggedIn) {
  352. $forceLoginForm = true;
  353. }
  354. //is module public and excluded from forced auth?
  355. if ($forceLoginForm) {
  356. if (isset($_GET[self::ROUTE_MODULE_LOAD])) {
  357. $moduleName = $_GET[self::ROUTE_MODULE_LOAD];
  358. $moduleName = preg_replace('/\0/s', '', $moduleName);
  359. if (isset($this->noAuthModules[$moduleName])) {
  360. $forceLoginForm = false;
  361. }
  362. } else {
  363. if (isset($this->noAuthModules[$this->indexModule])) {
  364. $forceLoginForm = false;
  365. }
  366. }
  367. }
  368. if (!$forceLoginForm) {
  369. //switching module if set some required route
  370. if (isset($_GET[self::ROUTE_MODULE_LOAD])) {
  371. $moduleName = $_GET[self::ROUTE_MODULE_LOAD];
  372. $moduleName = preg_replace('/\0/s', '', $moduleName);
  373. if ($this->isModuleValid($moduleName)) {
  374. $this->indexModule = $moduleName;
  375. } else {
  376. die('No module ' . $moduleName . ' exists');
  377. }
  378. }
  379. } else {
  380. //force login form switch
  381. if ($this->isModuleValid('loginform')) {
  382. $this->indexModule = 'loginform';
  383. } else {
  384. die('No module loginform exists');
  385. }
  386. }
  387. }
  388. /**
  389. * Loads some module by its name.
  390. * Not used at this moment, due fails on global objects like $ubillingConfig, $system, etc
  391. *
  392. * @return void
  393. */
  394. public function loadCurrentModule() {
  395. require_once($this->getIndexModulePath());
  396. }
  397. /**
  398. * Preloads all general modules from general modules directory
  399. *
  400. * @return void
  401. */
  402. protected function initializeModules() {
  403. $disabledModules = array();
  404. //some modules may be disabled
  405. if (isset($this->config['YALF_DISABLED_MODULES'])) {
  406. if (!empty($this->config['YALF_DISABLED_MODULES'])) {
  407. $disabledModules = explode(',', $this->config['YALF_DISABLED_MODULES']);
  408. $disabledModules = array_flip($disabledModules);
  409. }
  410. }
  411. $allModules = scandir(MODULES_PATH);
  412. foreach ($allModules as $module) {
  413. if (!isset($disabledModules[$module])) {
  414. if (file_exists(MODULES_PATH . $module . '/' . self::MODULE_DEFINITION)) {
  415. include_once(MODULES_PATH . $module . '/' . self::MODULE_DEFINITION);
  416. }
  417. }
  418. }
  419. // Register modules rights in main database
  420. foreach ($this->modules as $type => $modules) {
  421. foreach ($modules as $module => $moduledata) {
  422. //rights register
  423. foreach ($moduledata['rights'] as $right => $desc) {
  424. $this->rights_database[$right] = $desc;
  425. }
  426. //registering module as loadable
  427. $this->loadableModules[$module] = $moduledata['title'];
  428. }
  429. }
  430. }
  431. /**
  432. * Registers module as preloaded
  433. *
  434. * @param string $module
  435. * @param string $type
  436. * @param string $title
  437. * @param string $copyright
  438. * @param array $rights
  439. *
  440. * @return void
  441. */
  442. protected function registerModule($module, $type, $title, $copyright = '', $rights = array()) {
  443. $this->modules[$type][$module]['title'] = $title;
  444. $this->modules[$type][$module]['copyright'] = $copyright;
  445. $this->modules[$type][$module]['rights'] = $rights;
  446. }
  447. /**
  448. * Returns array of libs required for loading layers
  449. *
  450. * @return array
  451. */
  452. public function getLibs() {
  453. return ($this->loadLibs);
  454. }
  455. /**
  456. * Returns state of flag that allows live locale switching by clients
  457. *
  458. * @return bool
  459. */
  460. public function isLocaleSwitchable() {
  461. return ($this->langSwitchAllowed);
  462. }
  463. /**
  464. * Returns full path of index module aka main route
  465. *
  466. * @return string
  467. */
  468. public function getIndexModulePath() {
  469. return (MODULES_PATH . $this->indexModule . '/' . self::MODULE_CODE_NAME);
  470. }
  471. /**
  472. * Returns current module name
  473. *
  474. * @return string
  475. */
  476. public function getCurrentModuleName() {
  477. return ($this->indexModule);
  478. }
  479. /**
  480. * Returns current locale language full path
  481. *
  482. * @return string
  483. */
  484. public function getLangPath() {
  485. return (self::LANG_PATH . $this->language . '/');
  486. }
  487. /**
  488. * Returns current locale ID as two-letters code
  489. *
  490. * @return string
  491. */
  492. public function getCurLang() {
  493. return (substr($this->language, 0, '2'));
  494. }
  495. /**
  496. * Returns current locale name
  497. *
  498. * @return string
  499. */
  500. public function getCurLangName() {
  501. return ($this->language);
  502. }
  503. /**
  504. * Returns current skin path
  505. *
  506. * @return string
  507. */
  508. public function getSkinPath() {
  509. return (self::SKINS_PATH . $this->skin . '/');
  510. }
  511. /**
  512. * Returns current application renderer type
  513. *
  514. * @return string
  515. */
  516. public function getRenderer() {
  517. return ($this->renderer);
  518. }
  519. /**
  520. * Returns current application page title
  521. *
  522. * @return string
  523. */
  524. public function getPageTitle() {
  525. return ($this->pageTitle);
  526. }
  527. /**
  528. * Sets current page title text
  529. *
  530. * @param string $title
  531. *
  532. * @return void
  533. */
  534. public function setPageTitle($title = '') {
  535. $this->pageTitle = $title;
  536. }
  537. /**
  538. * Returns ISP logo image code
  539. *
  540. * @return string
  541. */
  542. public function renderLogo() {
  543. $result = '';
  544. if (isset($this->config['YALF_LOGO'])) {
  545. if ((!empty($this->config['YALF_APP'])) and (!empty($this->config['YALF_URL'])) and ((!empty($this->config['YALF_LOGO'])))) {
  546. $rawUrl = strtolower($this->config['YALF_URL']);
  547. if (stripos($rawUrl, 'http') === false) {
  548. $rawUrl = 'http://' . $rawUrl;
  549. } else {
  550. $rawUrl = $rawUrl;
  551. }
  552. $result = '<a href="' . $rawUrl . '" target="_BLANK"><img src="' . $this->config['YALF_LOGO'] . '" title="' . __($this->config['YALF_APP']) . '"></a>';
  553. }
  554. }
  555. return ($result);
  556. }
  557. /**
  558. * Renders application menu
  559. *
  560. * @return string
  561. */
  562. public function renderMenu() {
  563. $result = '';
  564. if ($this->globalMenuEnabled) {
  565. if (file_exists(self::YALF_MENU_PATH)) {
  566. $rawData = parse_ini_file(self::YALF_MENU_PATH, true);
  567. if (!empty($rawData)) {
  568. foreach ($rawData as $section => $each) {
  569. $renderMenuEntry = true;
  570. $icon = (!empty($each['ICON'])) ? $each['ICON'] : self::DEFAULT_ICON;
  571. $icon = self::MENU_ICONS_PATH . $icon;
  572. $name = __($each['NAME']);
  573. $actClass = ($this->getCurrentModuleName() == $section) ? 'active' : '';
  574. //right check
  575. if (isset($each['NEED_RIGHT'])) {
  576. //is auth engine enabled?
  577. if ($this->authEnabled) {
  578. if (!empty($each['NEED_RIGHT'])) {
  579. $renderMenuEntry = $this->checkForRight($each['NEED_RIGHT']);
  580. }
  581. }
  582. }
  583. //option check
  584. if ($renderMenuEntry) {
  585. //if not denied by rights now
  586. if (isset($each['NEED_OPTION'])) {
  587. if (!empty($each['NEED_OPTION'])) {
  588. if (isset($this->config[$each['NEED_OPTION']])) {
  589. if (empty($this->config[$each['NEED_OPTION']])) {
  590. $renderMenuEntry = false; //required option disabled
  591. }
  592. } else {
  593. $renderMenuEntry = false;
  594. }
  595. }
  596. }
  597. }
  598. if ($renderMenuEntry) {
  599. $result .= wf_tag('li', false, $actClass) . wf_Link($each['URL'], wf_img($icon) . ' ' . $name, false) . wf_tag('li', true);
  600. }
  601. }
  602. }
  603. }
  604. }
  605. return ($result);
  606. }
  607. /**
  608. * Returns global menu enable state flag
  609. *
  610. * @return bool
  611. */
  612. public function getGlobalMenuFlag() {
  613. return ($this->globalMenuEnabled);
  614. }
  615. /**
  616. * Returns some user data as array
  617. *
  618. * @param string $username
  619. *
  620. * @return array/bool
  621. */
  622. public function getUserData($username) {
  623. $result = @unserialize(@file_get_contents(USERS_PATH . basename($username)));
  624. if (empty($result)) {
  625. return (false);
  626. } else {
  627. return $result;
  628. }
  629. }
  630. /**
  631. * Inits user and sets some cookies if its ok
  632. *
  633. * @param bool $skipcheck Use this parameter to skip userdata checks
  634. *
  635. * @return bool
  636. */
  637. protected function initializeUser($skipcheck = false) {
  638. //Inits default guest user
  639. $this->user = array('nickname' => __('Guest'), 'username' => 'guest', 'admin' => '', 'tz' => (int) @$this->config['default_tz'], 'accesslevel' => 0);
  640. $this->initialiseAccess($this->user['admin']);
  641. if (@$this->config['YALF_AUTH_ENABLED']) {
  642. // If user cookie is not present we exiting without error
  643. if (empty($_COOKIE[$this->cookie_user])) {
  644. $this->loggedIn = false;
  645. return (true);
  646. }
  647. // So we have a cookie, let's extract data from it
  648. if (is_string($_COOKIE[$this->cookie_user])) {
  649. $cookie_data = explode(':', $_COOKIE[$this->cookie_user], 2);
  650. } else {
  651. $cookie_data = array();
  652. }
  653. if (!$skipcheck) {
  654. // If this cookie is invalid - we exiting destroying cookie and exiting with error
  655. if (sizeof($cookie_data) != 2) {
  656. setcookie($this->cookie_user, '', time() - 3600);
  657. return (false);
  658. }
  659. // Now we must validate user's data
  660. if (!$this->checkUserData($cookie_data[0], $cookie_data[1], 'user_init', true, $this->user)) {
  661. setcookie($this->cookie_user, '', time() - 3600);
  662. $this->loggedIn = false;
  663. return (false);
  664. }
  665. }
  666. $userdata = $this->getUserData($cookie_data[0]);
  667. //failed to load user profile
  668. if ($userdata == false) {
  669. setcookie($this->cookie_user, '', time() - 3600);
  670. $this->loggedIn = false;
  671. return (false);
  672. }
  673. $this->user = $userdata;
  674. $this->loggedIn = true;
  675. // Initialise access levels
  676. $this->initialiseAccess($this->user['admin']);
  677. // Secure the nickname
  678. $this->user['nickname'] = htmlspecialchars($this->user['nickname']);
  679. } else {
  680. //All users around is logged in and have root rights
  681. $this->loggedIn = true;
  682. $this->root = true;
  683. }
  684. }
  685. /**
  686. * Performs user ath/deauth if required
  687. *
  688. * @return void
  689. */
  690. protected function performUserAuth() {
  691. if ($this->config['YALF_AUTH_ENABLED']) {
  692. if (!empty($_POST['login_form'])) {
  693. $this->logInUser(@$_POST['username'], @$_POST['password'], !empty($_POST['remember']) ? true : false);
  694. }
  695. //default POST logout
  696. if (!empty($_POST['logout_form'])) {
  697. $this->logOutUser();
  698. rcms_redirect('index.php', true);
  699. }
  700. //additional get-request user auto logout sub
  701. if (!empty($_GET['idleTimerAutoLogout'])) {
  702. $this->logOutUser();
  703. rcms_redirect('index.php', true);
  704. }
  705. //normal get-request user logout
  706. if (!empty($_GET['forceLogout'])) {
  707. $this->logOutUser();
  708. rcms_redirect('index.php', true);
  709. }
  710. }
  711. }
  712. /**
  713. * Parses some rights string into protected rights property
  714. *
  715. * @param string $rights
  716. *
  717. * @return bool
  718. */
  719. protected function initialiseAccess($rights) {
  720. if ($rights !== '*') {
  721. preg_match_all('/\|(.*?)\|/', $rights, $rights_r);
  722. foreach ($rights_r[1] as $right) {
  723. $this->rights[$right] = (empty($this->rights_database[$right])) ? ' ' : $this->rights_database[$right];
  724. }
  725. } else {
  726. $this->root = true;
  727. }
  728. return (true);
  729. }
  730. /**
  731. * Returns rights database registered due modules preload
  732. *
  733. * @return array
  734. */
  735. public function getRightsDatabase() {
  736. return ($this->rights_database);
  737. }
  738. /**
  739. * This function log out user from system and destroys his cookie.
  740. *
  741. * @return bool
  742. */
  743. protected function logOutUser() {
  744. //normal user logout
  745. if (!@$_COOKIE[$this->cookie_ghost]) {
  746. setcookie($this->cookie_user, '', time() - 3600);
  747. $_COOKIE[$this->cookie_user] = '';
  748. $this->initializeUser(false);
  749. } else {
  750. //ghostmode logout
  751. $this->deinitGhostMode();
  752. }
  753. return (true);
  754. }
  755. /**
  756. * This function check user's data and logs in him.
  757. *
  758. * @param string $username
  759. * @param string $password
  760. * @param bool $remember
  761. *
  762. * @return bool
  763. */
  764. protected function logInUser($username, $password, $remember) {
  765. $username = basename($username);
  766. if ($username == 'guest') {
  767. return false;
  768. }
  769. if (!$this->loggedIn and $this->checkUserData($username, $password, 'user_login', false, $userdata)) {
  770. // OK... Let's allow user to log in :)
  771. setcookie($this->cookie_user, $username . ':' . $userdata['password'], ($remember) ? time() + 3600 * 24 * 365 : 0);
  772. $_COOKIE[$this->cookie_user] = $username . ':' . $userdata['password'];
  773. $this->initializeUser(true);
  774. return (true);
  775. } else {
  776. return (false);
  777. }
  778. }
  779. /**
  780. * Returns logged in state
  781. *
  782. * @return bool
  783. */
  784. public function getLoggedInState() {
  785. return ($this->loggedIn);
  786. }
  787. /**
  788. * Returns system athorization flag state
  789. *
  790. * @return bool
  791. */
  792. public function getAuthEnabled() {
  793. return ($this->authEnabled);
  794. }
  795. /**
  796. * This function check user's data and validate his profile file.
  797. *
  798. * @param string $username
  799. * @param string $password
  800. * @param string $report_to
  801. * @param boolean $hash
  802. * @param link $userdata
  803. *
  804. * @return bool
  805. */
  806. protected function checkUserData($username, $password, $report_to, $hash, &$userdata) {
  807. if (preg_replace("/[\d\w]+/i", "", $username) != "") {
  808. $this->results[$report_to] = __('Invalid username');
  809. return false;
  810. }
  811. // If login is not exists - we exiting with error
  812. if (!is_file(USERS_PATH . $username)) {
  813. $this->results[$report_to] = __('There are no user with this username');
  814. return false;
  815. }
  816. // So all is ok. Let's load userdata
  817. $result = $this->getUserData($username);
  818. // If userdata is invalid we must exit with error
  819. if (empty($result))
  820. return false;
  821. // If password is invalid - exit with error
  822. if ((!$hash && md5($password) !== $result['password']) || ($hash && $password !== $result['password'])) {
  823. $this->results[$report_to] = __('Invalid password');
  824. return false;
  825. }
  826. // If user is blocked - exit with error
  827. if (@$result['blocked']) {
  828. $this->results[$report_to] = __('This account has been blocked by administrator');
  829. return false;
  830. }
  831. $userdata = $result;
  832. return true;
  833. }
  834. /**
  835. * Public getter for currently logged in user login
  836. *
  837. * @return string
  838. */
  839. public function getLoggedInUsername() {
  840. return ($this->user['username']);
  841. }
  842. /**
  843. * Logs some data to system log
  844. *
  845. * @param string $event Event text to log
  846. *
  847. * @return void
  848. */
  849. public function logEvent($event) {
  850. $date = date("Y-m-d H:i:s");
  851. $myLogin = $this->getLoggedInUsername();
  852. $myIp = $_SERVER['REMOTE_ADDR'];
  853. switch ($this->logType) {
  854. case 'file':
  855. $logRecord = $date . ' ' . $myLogin . ' ' . $myIp . ': ' . $event . PHP_EOL;
  856. file_put_contents($this->logFilePath, $logRecord, FILE_APPEND);
  857. break;
  858. case 'mysql':
  859. $event = mysql_real_escape_string($event);
  860. $query = "INSERT INTO `" . $this->logTable . "` (`id`,`date`,`admin`,`ip`,`event`) VALUES";
  861. $query .= "(NULL,'" . $date . "','" . $myLogin . "','" . $myIp . "','" . $event . "');";
  862. nr_query($query);
  863. break;
  864. case 'fake':
  865. //just do nothing ^_^
  866. break;
  867. default:
  868. die('Wrong logging type');
  869. break;
  870. }
  871. }
  872. /**
  873. * TODO: This piece of shit must be reviewed and rewritten
  874. * to many existing code use $system->getRightsForUser and $system->checkForRight('ONLINE') or something like
  875. */
  876. /**
  877. * Check if user have specified right
  878. *
  879. * @param string $right
  880. * @param string $username
  881. *
  882. * @return bool
  883. */
  884. public function checkForRight($right = '-any-', $username = '') {
  885. if (empty($username)) {
  886. $rights = &$this->rights;
  887. $root = &$this->root;
  888. } else {
  889. if (!$this->getRightsForUser($username, $rights, $root)) {
  890. return false;
  891. }
  892. }
  893. return $root or ($right == '-any-' && !empty($rights)) or ! empty($rights[$right]);
  894. }
  895. /**
  896. *
  897. * @param string $username
  898. * @param pointer $rights
  899. * @param pointer $root
  900. *
  901. * @return bool
  902. */
  903. protected function getRightsForUser($username, &$rights, &$root) {
  904. if (!($userdata = $this->getUserData($username))) {
  905. return false;
  906. }
  907. $rights = array();
  908. $root = false;
  909. if ($userdata['admin'] !== '*') {
  910. preg_match_all('/\|(.*?)\|/', $userdata['admin'], $rights_r);
  911. foreach ($rights_r[1] as $right) {
  912. $rights[$right] = (empty($this->rights_database[$right])) ? ' ' : $this->rights_database[$right];
  913. }
  914. } else {
  915. $root = true;
  916. }
  917. return true;
  918. }
  919. /**
  920. * Returns some yalfConf option value or false if its not exists
  921. *
  922. * @param string $optionName
  923. *
  924. * @return mixed/bool on error
  925. */
  926. public function getConfigOption($optionName) {
  927. $result = false;
  928. if (isset($this->config[$optionName])) {
  929. $result = $this->config[$optionName];
  930. }
  931. return ($result);
  932. }
  933. /**
  934. * Inits ghost mode for some administrator login
  935. *
  936. * @param string $adminLogin
  937. *
  938. * @return void
  939. */
  940. public function initGhostMode($adminLogin) {
  941. if (file_exists(USERS_PATH . $adminLogin)) {
  942. $userData = $this->getUserData($adminLogin);
  943. if (!empty($userData)) {
  944. $myLogin = whoami();
  945. $myData = $this->getUserData($myLogin);
  946. //current login data is used for ghost mode identification
  947. setcookie($this->cookie_ghost, $myLogin . ':' . $myData['password'], 0);
  948. $_COOKIE[$this->cookie_ghost] = $myLogin . ':' . $myData['password'];
  949. //login of another admin
  950. log_register('GHOSTMODE {' . $myLogin . '} LOGIN AS {' . $adminLogin . '}');
  951. setcookie($this->cookie_user, $adminLogin . ':' . $userData['password'], 0);
  952. $_COOKIE[$this->cookie_user] = $adminLogin . ':' . $userData['password'];
  953. }
  954. }
  955. }
  956. /**
  957. * Deinits ghost mode for current ghost administrator
  958. *
  959. * @return void
  960. */
  961. public function deinitGhostMode() {
  962. global $system;
  963. if (@$_COOKIE[$this->cookie_ghost]) {
  964. $ghostData = explode(':', $_COOKIE[$this->cookie_ghost]);
  965. //cleanup ghostmode data
  966. setcookie($this->cookie_ghost, '', 0);
  967. $_COOKIE[$this->cookie_ghost] = '';
  968. //login of another admin
  969. setcookie($this->cookie_user, $ghostData[0] . ':' . $ghostData[1], 0);
  970. $_COOKIE[$this->cookie_user] = $ghostData[0] . ':' . $ghostData[1];
  971. }
  972. }
  973. }