api.yalfcore.php 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. rcms_redirect('index.php');
  283. }
  284. }
  285. }
  286. //some custom locale already set
  287. if (@$_COOKIE[$this->cookie_locale]) {
  288. if (is_string($_COOKIE[$this->cookie_locale])) {
  289. $customLocale = preg_replace('/\0/s', '', $_COOKIE[$this->cookie_locale]);
  290. $customLocale = preg_replace("#[^a-z0-9A-Z]#Uis", '', $customLocale);
  291. if (!empty($customLocale)) {
  292. if (file_exists(self::LANG_PATH . $customLocale)) {
  293. $this->language = $customLocale;
  294. }
  295. }
  296. }
  297. }
  298. }
  299. }
  300. //page title setup
  301. if (isset($this->config['YALF_TITLE'])) {
  302. if (!empty($this->config['YALF_TITLE'])) {
  303. $this->pageTitle = $this->config['YALF_TITLE'];
  304. }
  305. }
  306. //global menu rendering flag setup
  307. if (isset($this->config['YALF_MENU_ENABLED'])) {
  308. if ($this->config['YALF_MENU_ENABLED']) {
  309. $this->globalMenuEnabled = true;
  310. }
  311. }
  312. //system auth enabled
  313. if (isset($this->config['YALF_AUTH_ENABLED'])) {
  314. if ($this->config['YALF_AUTH_ENABLED']) {
  315. $this->authEnabled = true;
  316. }
  317. }
  318. //system logging settings
  319. if (isset($this->config['YALF_LOGGING_TYPE'])) {
  320. $this->logType = $this->config['YALF_LOGGING_TYPE'];
  321. if (isset($this->config['YALF_LOG_TABLE'])) {
  322. $this->logTable = $this->config['YALF_LOG_TABLE'];
  323. }
  324. }
  325. //no auth public modules
  326. if (isset($this->config['YALF_NO_AUTH_MODULES'])) {
  327. if (!empty($this->config['YALF_NO_AUTH_MODULES'])) {
  328. $this->noAuthModules = explode(',', $this->config['YALF_NO_AUTH_MODULES']);
  329. $this->noAuthModules = array_flip($this->noAuthModules); //use module name as index
  330. }
  331. }
  332. //renderer type detection
  333. if (isset($this->config['LAYER_CLIRENDER'])) {
  334. $this->renderer = 'CLI';
  335. }
  336. if (isset($this->config['LAYER_WEBRENDER'])) {
  337. $this->renderer = 'WEB';
  338. }
  339. }
  340. /**
  341. * Switches index(current) module if its required
  342. *
  343. * @return void
  344. */
  345. protected function switchIndexModule() {
  346. $forceLoginForm = false; //show login form module instead any of called
  347. //user is not authorized now and auth engine enabled
  348. if (!$this->loggedIn) {
  349. $forceLoginForm = true;
  350. }
  351. //is module public and excluded from forced auth?
  352. if ($forceLoginForm) {
  353. if (isset($_GET[self::ROUTE_MODULE_LOAD])) {
  354. $moduleName = $_GET[self::ROUTE_MODULE_LOAD];
  355. $moduleName = preg_replace('/\0/s', '', $moduleName);
  356. if (isset($this->noAuthModules[$moduleName])) {
  357. $forceLoginForm = false;
  358. }
  359. } else {
  360. if (isset($this->noAuthModules[$this->indexModule])) {
  361. $forceLoginForm = false;
  362. }
  363. }
  364. }
  365. if (!$forceLoginForm) {
  366. //switching module if set some required route
  367. if (isset($_GET[self::ROUTE_MODULE_LOAD])) {
  368. $moduleName = $_GET[self::ROUTE_MODULE_LOAD];
  369. $moduleName = preg_replace('/\0/s', '', $moduleName);
  370. if ($this->isModuleValid($moduleName)) {
  371. $this->indexModule = $moduleName;
  372. } else {
  373. die('No module ' . $moduleName . ' exists');
  374. }
  375. }
  376. } else {
  377. //force login form switch
  378. if ($this->isModuleValid('loginform')) {
  379. $this->indexModule = 'loginform';
  380. } else {
  381. die('No module loginform exists');
  382. }
  383. }
  384. }
  385. /**
  386. * Loads some module by its name.
  387. * Not used at this moment, due fails on global objects like $ubillingConfig, $system, etc
  388. *
  389. * @return void
  390. */
  391. public function loadCurrentModule() {
  392. require_once($this->getIndexModulePath());
  393. }
  394. /**
  395. * Preloads all general modules from general modules directory
  396. *
  397. * @return void
  398. */
  399. protected function initializeModules() {
  400. $disabledModules = array();
  401. //some modules may be disabled
  402. if (isset($this->config['YALF_DISABLED_MODULES'])) {
  403. if (!empty($this->config['YALF_DISABLED_MODULES'])) {
  404. $disabledModules = explode(',', $this->config['YALF_DISABLED_MODULES']);
  405. $disabledModules = array_flip($disabledModules);
  406. }
  407. }
  408. $allModules = scandir(MODULES_PATH);
  409. foreach ($allModules as $module) {
  410. if (!isset($disabledModules[$module])) {
  411. if (file_exists(MODULES_PATH . $module . '/' . self::MODULE_DEFINITION)) {
  412. include_once(MODULES_PATH . $module . '/' . self::MODULE_DEFINITION);
  413. }
  414. }
  415. }
  416. // Register modules rights in main database
  417. foreach ($this->modules as $type => $modules) {
  418. foreach ($modules as $module => $moduledata) {
  419. //rights register
  420. foreach ($moduledata['rights'] as $right => $desc) {
  421. $this->rights_database[$right] = $desc;
  422. }
  423. //registering module as loadable
  424. $this->loadableModules[$module] = $moduledata['title'];
  425. }
  426. }
  427. }
  428. /**
  429. * Registers module as preloaded
  430. *
  431. * @param string $module
  432. * @param string $type
  433. * @param string $title
  434. * @param string $copyright
  435. * @param array $rights
  436. *
  437. * @return void
  438. */
  439. protected function registerModule($module, $type, $title, $copyright = '', $rights = array()) {
  440. $this->modules[$type][$module]['title'] = $title;
  441. $this->modules[$type][$module]['copyright'] = $copyright;
  442. $this->modules[$type][$module]['rights'] = $rights;
  443. }
  444. /**
  445. * Returns array of libs required for loading layers
  446. *
  447. * @return array
  448. */
  449. public function getLibs() {
  450. return ($this->loadLibs);
  451. }
  452. /**
  453. * Returns state of flag that allows live locale switching by clients
  454. *
  455. * @return bool
  456. */
  457. public function isLocaleSwitchable() {
  458. return ($this->langSwitchAllowed);
  459. }
  460. /**
  461. * Returns full path of index module aka main route
  462. *
  463. * @return string
  464. */
  465. public function getIndexModulePath() {
  466. return (MODULES_PATH . $this->indexModule . '/' . self::MODULE_CODE_NAME);
  467. }
  468. /**
  469. * Returns current module name
  470. *
  471. * @return string
  472. */
  473. public function getCurrentModuleName() {
  474. return ($this->indexModule);
  475. }
  476. /**
  477. * Returns current locale language full path
  478. *
  479. * @return string
  480. */
  481. public function getLangPath() {
  482. return (self::LANG_PATH . $this->language . '/');
  483. }
  484. /**
  485. * Returns current locale ID as two-letters code
  486. *
  487. * @return string
  488. */
  489. public function getCurLang() {
  490. return (substr($this->language, 0, '2'));
  491. }
  492. /**
  493. * Returns current locale name
  494. *
  495. * @return string
  496. */
  497. public function getCurLangName() {
  498. return ($this->language);
  499. }
  500. /**
  501. * Returns current skin path
  502. *
  503. * @return string
  504. */
  505. public function getSkinPath() {
  506. return (self::SKINS_PATH . $this->skin . '/');
  507. }
  508. /**
  509. * Returns current application renderer type
  510. *
  511. * @return string
  512. */
  513. public function getRenderer() {
  514. return ($this->renderer);
  515. }
  516. /**
  517. * Returns current application page title
  518. *
  519. * @return string
  520. */
  521. public function getPageTitle() {
  522. return ($this->pageTitle);
  523. }
  524. /**
  525. * Sets current page title text
  526. *
  527. * @param string $title
  528. *
  529. * @return void
  530. */
  531. public function setPageTitle($title = '') {
  532. $this->pageTitle = $title;
  533. }
  534. /**
  535. * Returns ISP logo image code
  536. *
  537. * @return string
  538. */
  539. public function renderLogo() {
  540. $result = '';
  541. if (isset($this->config['YALF_LOGO'])) {
  542. if ((!empty($this->config['YALF_APP'])) and (!empty($this->config['YALF_URL'])) and ((!empty($this->config['YALF_LOGO'])))) {
  543. $rawUrl = strtolower($this->config['YALF_URL']);
  544. if (stripos($rawUrl, 'http') === false) {
  545. $rawUrl = 'http://' . $rawUrl;
  546. } else {
  547. $rawUrl = $rawUrl;
  548. }
  549. $result = '<a href="' . $rawUrl . '" target="_BLANK"><img src="' . $this->config['YALF_LOGO'] . '" title="' . __($this->config['YALF_APP']) . '"></a>';
  550. }
  551. }
  552. return ($result);
  553. }
  554. /**
  555. * Renders application menu
  556. *
  557. * @return string
  558. */
  559. public function renderMenu() {
  560. $result = '';
  561. if ($this->globalMenuEnabled) {
  562. if (file_exists(self::YALF_MENU_PATH)) {
  563. $rawData = parse_ini_file(self::YALF_MENU_PATH, true);
  564. if (!empty($rawData)) {
  565. foreach ($rawData as $section => $each) {
  566. $renderMenuEntry = true;
  567. $icon = (!empty($each['ICON'])) ? $each['ICON'] : self::DEFAULT_ICON;
  568. $icon = self::MENU_ICONS_PATH . $icon;
  569. $name = __($each['NAME']);
  570. $actClass = ($this->getCurrentModuleName() == $section) ? 'active' : '';
  571. //right check
  572. if (isset($each['NEED_RIGHT'])) {
  573. //is auth engine enabled?
  574. if ($this->authEnabled) {
  575. if (!empty($each['NEED_RIGHT'])) {
  576. $renderMenuEntry = $this->checkForRight($each['NEED_RIGHT']);
  577. }
  578. }
  579. }
  580. //option check
  581. if ($renderMenuEntry) {
  582. //if not denied by rights now
  583. if (isset($each['NEED_OPTION'])) {
  584. if (!empty($each['NEED_OPTION'])) {
  585. if (isset($this->config[$each['NEED_OPTION']])) {
  586. if (empty($this->config[$each['NEED_OPTION']])) {
  587. $renderMenuEntry = false; //required option disabled
  588. }
  589. } else {
  590. $renderMenuEntry = false;
  591. }
  592. }
  593. }
  594. }
  595. if ($renderMenuEntry) {
  596. $result .= wf_tag('li', false, $actClass) . wf_Link($each['URL'], wf_img($icon) . ' ' . $name, false) . wf_tag('li', true);
  597. }
  598. }
  599. }
  600. }
  601. }
  602. return ($result);
  603. }
  604. /**
  605. * Returns global menu enable state flag
  606. *
  607. * @return bool
  608. */
  609. public function getGlobalMenuFlag() {
  610. return ($this->globalMenuEnabled);
  611. }
  612. /**
  613. * Returns some user data as array
  614. *
  615. * @param string $username
  616. *
  617. * @return array/bool
  618. */
  619. public function getUserData($username) {
  620. $result = @unserialize(@file_get_contents(USERS_PATH . basename($username)));
  621. if (empty($result)) {
  622. return (false);
  623. } else {
  624. return $result;
  625. }
  626. }
  627. /**
  628. * Inits user and sets some cookies if its ok
  629. *
  630. * @param bool $skipcheck Use this parameter to skip userdata checks
  631. *
  632. * @return bool
  633. */
  634. protected function initializeUser($skipcheck = false) {
  635. //Inits default guest user
  636. $this->user = array('nickname' => __('Guest'), 'username' => 'guest', 'admin' => '', 'tz' => (int) @$this->config['default_tz'], 'accesslevel' => 0);
  637. $this->initialiseAccess($this->user['admin']);
  638. if (@$this->config['YALF_AUTH_ENABLED']) {
  639. // If user cookie is not present we exiting without error
  640. if (empty($_COOKIE[$this->cookie_user])) {
  641. $this->loggedIn = false;
  642. return (true);
  643. }
  644. // So we have a cookie, let's extract data from it
  645. if (is_string($_COOKIE[$this->cookie_user])) {
  646. $cookie_data = explode(':', $_COOKIE[$this->cookie_user], 2);
  647. } else {
  648. $cookie_data = array();
  649. }
  650. if (!$skipcheck) {
  651. // If this cookie is invalid - we exiting destroying cookie and exiting with error
  652. if (sizeof($cookie_data) != 2) {
  653. setcookie($this->cookie_user, '', time() - 3600);
  654. return (false);
  655. }
  656. // Now we must validate user's data
  657. if (!$this->checkUserData($cookie_data[0], $cookie_data[1], 'user_init', true, $this->user)) {
  658. setcookie($this->cookie_user, '', time() - 3600);
  659. $this->loggedIn = false;
  660. return (false);
  661. }
  662. }
  663. $userdata = $this->getUserData($cookie_data[0]);
  664. //failed to load user profile
  665. if ($userdata == false) {
  666. setcookie($this->cookie_user, '', time() - 3600);
  667. $this->loggedIn = false;
  668. return (false);
  669. }
  670. $this->user = $userdata;
  671. $this->loggedIn = true;
  672. // Initialise access levels
  673. $this->initialiseAccess($this->user['admin']);
  674. // Secure the nickname
  675. $this->user['nickname'] = htmlspecialchars($this->user['nickname']);
  676. } else {
  677. //All users around is logged in and have root rights
  678. $this->loggedIn = true;
  679. $this->root = true;
  680. }
  681. }
  682. /**
  683. * Performs user ath/deauth if required
  684. *
  685. * @return void
  686. */
  687. protected function performUserAuth() {
  688. if ($this->config['YALF_AUTH_ENABLED']) {
  689. if (!empty($_POST['login_form'])) {
  690. $this->logInUser(@$_POST['username'], @$_POST['password'], !empty($_POST['remember']) ? true : false);
  691. }
  692. //default POST logout
  693. if (!empty($_POST['logout_form'])) {
  694. $this->logOutUser();
  695. rcms_redirect('index.php', true);
  696. }
  697. //additional get-request user auto logout sub
  698. if (!empty($_GET['idleTimerAutoLogout'])) {
  699. $this->logOutUser();
  700. rcms_redirect('index.php', true);
  701. }
  702. //normal get-request user logout
  703. if (!empty($_GET['forceLogout'])) {
  704. $this->logOutUser();
  705. rcms_redirect('index.php', true);
  706. }
  707. }
  708. }
  709. /**
  710. * Parses some rights string into protected rights property
  711. *
  712. * @param string $rights
  713. *
  714. * @return bool
  715. */
  716. protected function initialiseAccess($rights) {
  717. if ($rights !== '*') {
  718. preg_match_all('/\|(.*?)\|/', $rights, $rights_r);
  719. foreach ($rights_r[1] as $right) {
  720. $this->rights[$right] = (empty($this->rights_database[$right])) ? ' ' : $this->rights_database[$right];
  721. }
  722. } else {
  723. $this->root = true;
  724. }
  725. return (true);
  726. }
  727. /**
  728. * Returns rights database registered due modules preload
  729. *
  730. * @return array
  731. */
  732. public function getRightsDatabase() {
  733. return ($this->rights_database);
  734. }
  735. /**
  736. * This function log out user from system and destroys his cookie.
  737. *
  738. * @return bool
  739. */
  740. protected function logOutUser() {
  741. //normal user logout
  742. if (!@$_COOKIE[$this->cookie_ghost]) {
  743. setcookie($this->cookie_user, '', time() - 3600);
  744. $_COOKIE[$this->cookie_user] = '';
  745. $this->initializeUser(false);
  746. } else {
  747. //ghostmode logout
  748. $this->deinitGhostMode();
  749. }
  750. return (true);
  751. }
  752. /**
  753. * This function check user's data and logs in him.
  754. *
  755. * @param string $username
  756. * @param string $password
  757. * @param bool $remember
  758. *
  759. * @return bool
  760. */
  761. protected function logInUser($username, $password, $remember) {
  762. $username = basename($username);
  763. if ($username == 'guest') {
  764. return false;
  765. }
  766. if (!$this->loggedIn and $this->checkUserData($username, $password, 'user_login', false, $userdata)) {
  767. // OK... Let's allow user to log in :)
  768. setcookie($this->cookie_user, $username . ':' . $userdata['password'], ($remember) ? time() + 3600 * 24 * 365 : 0);
  769. $_COOKIE[$this->cookie_user] = $username . ':' . $userdata['password'];
  770. $this->initializeUser(true);
  771. return (true);
  772. } else {
  773. return (false);
  774. }
  775. }
  776. /**
  777. * Returns logged in state
  778. *
  779. * @return bool
  780. */
  781. public function getLoggedInState() {
  782. return ($this->loggedIn);
  783. }
  784. /**
  785. * Returns system athorization flag state
  786. *
  787. * @return bool
  788. */
  789. public function getAuthEnabled() {
  790. return ($this->authEnabled);
  791. }
  792. /**
  793. * This function check user's data and validate his profile file.
  794. *
  795. * @param string $username
  796. * @param string $password
  797. * @param string $report_to
  798. * @param boolean $hash
  799. * @param link $userdata
  800. *
  801. * @return bool
  802. */
  803. protected function checkUserData($username, $password, $report_to, $hash, &$userdata) {
  804. if (preg_replace("/[\d\w]+/i", "", $username) != "") {
  805. $this->results[$report_to] = __('Invalid username');
  806. return false;
  807. }
  808. // If login is not exists - we exiting with error
  809. if (!is_file(USERS_PATH . $username)) {
  810. $this->results[$report_to] = __('There are no user with this username');
  811. return false;
  812. }
  813. // So all is ok. Let's load userdata
  814. $result = $this->getUserData($username);
  815. // If userdata is invalid we must exit with error
  816. if (empty($result))
  817. return false;
  818. // If password is invalid - exit with error
  819. if ((!$hash && md5($password) !== $result['password']) || ($hash && $password !== $result['password'])) {
  820. $this->results[$report_to] = __('Invalid password');
  821. return false;
  822. }
  823. // If user is blocked - exit with error
  824. if (@$result['blocked']) {
  825. $this->results[$report_to] = __('This account has been blocked by administrator');
  826. return false;
  827. }
  828. $userdata = $result;
  829. return true;
  830. }
  831. /**
  832. * Public getter for currently logged in user login
  833. *
  834. * @return string
  835. */
  836. public function getLoggedInUsername() {
  837. return ($this->user['username']);
  838. }
  839. /**
  840. * Logs some data to system log
  841. *
  842. * @param string $event Event text to log
  843. *
  844. * @return void
  845. */
  846. public function logEvent($event) {
  847. $date = date("Y-m-d H:i:s");
  848. $myLogin = $this->getLoggedInUsername();
  849. $myIp = $_SERVER['REMOTE_ADDR'];
  850. switch ($this->logType) {
  851. case 'file':
  852. $logRecord = $date . ' ' . $myLogin . ' ' . $myIp . ': ' . $event . PHP_EOL;
  853. file_put_contents($this->logFilePath, $logRecord, FILE_APPEND);
  854. break;
  855. case 'mysql':
  856. $event = mysql_real_escape_string($event);
  857. $query = "INSERT INTO `" . $this->logTable . "` (`id`,`date`,`admin`,`ip`,`event`) VALUES";
  858. $query .= "(NULL,'" . $date . "','" . $myLogin . "','" . $myIp . "','" . $event . "');";
  859. nr_query($query);
  860. break;
  861. case 'fake':
  862. //just do nothing ^_^
  863. break;
  864. default:
  865. die('Wrong logging type');
  866. break;
  867. }
  868. }
  869. /**
  870. * TODO: This piece of shit must be reviewed and rewritten
  871. * to many existing code use $system->getRightsForUser and $system->checkForRight('ONLINE') or something like
  872. */
  873. /**
  874. * Check if user have specified right
  875. *
  876. * @param string $right
  877. * @param string $username
  878. *
  879. * @return bool
  880. */
  881. public function checkForRight($right = '-any-', $username = '') {
  882. if (empty($username)) {
  883. $rights = &$this->rights;
  884. $root = &$this->root;
  885. } else {
  886. if (!$this->getRightsForUser($username, $rights, $root)) {
  887. return false;
  888. }
  889. }
  890. return $root or ($right == '-any-' && !empty($rights)) or ! empty($rights[$right]);
  891. }
  892. /**
  893. *
  894. * @param string $username
  895. * @param pointer $rights
  896. * @param pointer $root
  897. *
  898. * @return bool
  899. */
  900. protected function getRightsForUser($username, &$rights, &$root) {
  901. if (!($userdata = $this->getUserData($username))) {
  902. return false;
  903. }
  904. $rights = array();
  905. $root = false;
  906. if ($userdata['admin'] !== '*') {
  907. preg_match_all('/\|(.*?)\|/', $userdata['admin'], $rights_r);
  908. foreach ($rights_r[1] as $right) {
  909. $rights[$right] = (empty($this->rights_database[$right])) ? ' ' : $this->rights_database[$right];
  910. }
  911. } else {
  912. $root = true;
  913. }
  914. return true;
  915. }
  916. /**
  917. * Returns some yalfConf option value or false if its not exists
  918. *
  919. * @param string $optionName
  920. *
  921. * @return mixed/bool on error
  922. */
  923. public function getConfigOption($optionName) {
  924. $result = false;
  925. if (isset($this->config[$optionName])) {
  926. $result = $this->config[$optionName];
  927. }
  928. return ($result);
  929. }
  930. /**
  931. * Inits ghost mode for some administrator login
  932. *
  933. * @param string $adminLogin
  934. *
  935. * @return void
  936. */
  937. public function initGhostMode($adminLogin) {
  938. if (file_exists(USERS_PATH . $adminLogin)) {
  939. $userData = $this->getUserData($adminLogin);
  940. if (!empty($userData)) {
  941. $myLogin = whoami();
  942. $myData = $this->getUserData($myLogin);
  943. //current login data is used for ghost mode identification
  944. setcookie($this->cookie_ghost, $myLogin . ':' . $myData['password'], 0);
  945. $_COOKIE[$this->cookie_ghost] = $myLogin . ':' . $myData['password'];
  946. //login of another admin
  947. log_register('GHOSTMODE {' . $myLogin . '} LOGIN AS {' . $adminLogin . '}');
  948. setcookie($this->cookie_user, $adminLogin . ':' . $userData['password'], 0);
  949. $_COOKIE[$this->cookie_user] = $adminLogin . ':' . $userData['password'];
  950. }
  951. }
  952. }
  953. /**
  954. * Deinits ghost mode for current ghost administrator
  955. *
  956. * @return void
  957. */
  958. public function deinitGhostMode() {
  959. global $system;
  960. if (@$_COOKIE[$this->cookie_ghost]) {
  961. $ghostData = explode(':', $_COOKIE[$this->cookie_ghost]);
  962. //cleanup ghostmode data
  963. setcookie($this->cookie_ghost, '', 0);
  964. $_COOKIE[$this->cookie_ghost] = '';
  965. //login of another admin
  966. setcookie($this->cookie_user, $ghostData[0] . ':' . $ghostData[1], 0);
  967. $_COOKIE[$this->cookie_user] = $ghostData[0] . ':' . $ghostData[1];
  968. }
  969. }
  970. }