api.yalfcore.php 31 KB

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