api.yalfcore.php 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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. $remember=false;
  691. $keepLoggedDefault=$this->getConfigOption('YALF_AUTH_KEEP_DEFAULT') ? true : false;
  692. $stayLogInFlagCB=$this->getConfigOption('YALF_AUTH_KEEP_CB') ? true : false;
  693. if ($keepLoggedDefault) {
  694. $remember=true;
  695. }
  696. if ($stayLogInFlagCB) {
  697. if (!empty($_POST['remember'])) {
  698. $remember=true;
  699. } else {
  700. $remember=false;
  701. }
  702. }
  703. $this->logInUser(@$_POST['username'], @$_POST['password'], $remember);
  704. if (!$this->getConfigOption('YALF_AUTH_NOREDIR')) {
  705. rcms_redirect('index.php', true);
  706. }
  707. }
  708. //default POST logout
  709. if (!empty($_POST['logout_form'])) {
  710. $this->logOutUser();
  711. rcms_redirect('index.php', true);
  712. }
  713. //additional get-request user auto logout sub
  714. if (!empty($_GET['idleTimerAutoLogout'])) {
  715. $this->logOutUser();
  716. rcms_redirect('index.php', true);
  717. }
  718. //normal get-request user logout
  719. if (!empty($_GET['forceLogout'])) {
  720. $this->logOutUser();
  721. rcms_redirect('index.php', true);
  722. }
  723. }
  724. }
  725. /**
  726. * Parses some rights string into protected rights property
  727. *
  728. * @param string $rights
  729. *
  730. * @return bool
  731. */
  732. protected function initialiseAccess($rights) {
  733. if ($rights !== '*') {
  734. preg_match_all('/\|(.*?)\|/', $rights, $rights_r);
  735. foreach ($rights_r[1] as $right) {
  736. $this->rights[$right] = (empty($this->rights_database[$right])) ? ' ' : $this->rights_database[$right];
  737. }
  738. } else {
  739. $this->root = true;
  740. }
  741. return (true);
  742. }
  743. /**
  744. * Returns rights database registered due modules preload
  745. *
  746. * @return array
  747. */
  748. public function getRightsDatabase() {
  749. return ($this->rights_database);
  750. }
  751. /**
  752. * This function log out user from system and destroys his cookie.
  753. *
  754. * @return bool
  755. */
  756. protected function logOutUser() {
  757. //normal user logout
  758. if (!@$_COOKIE[$this->cookie_ghost]) {
  759. setcookie($this->cookie_user, '', time() - 3600);
  760. $_COOKIE[$this->cookie_user] = '';
  761. $this->initializeUser(false);
  762. } else {
  763. //ghostmode logout
  764. $this->deinitGhostMode();
  765. }
  766. return (true);
  767. }
  768. /**
  769. * This function check user's data and logs in him.
  770. *
  771. * @param string $username
  772. * @param string $password
  773. * @param bool $remember
  774. *
  775. * @return bool
  776. */
  777. protected function logInUser($username, $password, $remember) {
  778. $username = basename($username);
  779. if ($username == 'guest') {
  780. return false;
  781. }
  782. if (!$this->loggedIn and $this->checkUserData($username, $password, 'user_login', false, $userdata)) {
  783. // OK... Let's allow user to log in :)
  784. setcookie($this->cookie_user, $username . ':' . $userdata['password'], ($remember) ? time() + 3600 * 24 * 365 : 0);
  785. $_COOKIE[$this->cookie_user] = $username . ':' . $userdata['password'];
  786. $this->initializeUser(true);
  787. return (true);
  788. } else {
  789. return (false);
  790. }
  791. }
  792. /**
  793. * Returns logged in state
  794. *
  795. * @return bool
  796. */
  797. public function getLoggedInState() {
  798. return ($this->loggedIn);
  799. }
  800. /**
  801. * Returns system athorization flag state
  802. *
  803. * @return bool
  804. */
  805. public function getAuthEnabled() {
  806. return ($this->authEnabled);
  807. }
  808. /**
  809. * This function check user's data and validate his profile file.
  810. *
  811. * @param string $username
  812. * @param string $password
  813. * @param string $report_to
  814. * @param boolean $hash
  815. * @param link $userdata
  816. *
  817. * @return bool
  818. */
  819. protected function checkUserData($username, $password, $report_to, $hash, &$userdata) {
  820. if (preg_replace("/[\d\w]+/i", "", $username) != "") {
  821. $this->results[$report_to] = __('Invalid username');
  822. return false;
  823. }
  824. // If login is not exists - we exiting with error
  825. if (!is_file(USERS_PATH . $username)) {
  826. $this->results[$report_to] = __('There are no user with this username');
  827. return false;
  828. }
  829. // So all is ok. Let's load userdata
  830. $result = $this->getUserData($username);
  831. // If userdata is invalid we must exit with error
  832. if (empty($result))
  833. return false;
  834. // If password is invalid - exit with error
  835. if ((!$hash && md5($password) !== $result['password']) || ($hash && $password !== $result['password'])) {
  836. $this->results[$report_to] = __('Invalid password');
  837. return false;
  838. }
  839. // If user is blocked - exit with error
  840. if (@$result['blocked']) {
  841. $this->results[$report_to] = __('This account has been blocked by administrator');
  842. return false;
  843. }
  844. $userdata = $result;
  845. return true;
  846. }
  847. /**
  848. * Public getter for currently logged in user login
  849. *
  850. * @return string
  851. */
  852. public function getLoggedInUsername() {
  853. return ($this->user['username']);
  854. }
  855. /**
  856. * Logs some data to system log
  857. *
  858. * @param string $event Event text to log
  859. *
  860. * @return void
  861. */
  862. public function logEvent($event) {
  863. $date = date("Y-m-d H:i:s");
  864. $myLogin = $this->getLoggedInUsername();
  865. $myIp = $_SERVER['REMOTE_ADDR'];
  866. switch ($this->logType) {
  867. case 'file':
  868. $logRecord = $date . ' ' . $myLogin . ' ' . $myIp . ': ' . $event . PHP_EOL;
  869. file_put_contents($this->logFilePath, $logRecord, FILE_APPEND);
  870. break;
  871. case 'mysql':
  872. $event = mysql_real_escape_string($event);
  873. $query = "INSERT INTO `" . $this->logTable . "` (`id`,`date`,`admin`,`ip`,`event`) VALUES";
  874. $query .= "(NULL,'" . $date . "','" . $myLogin . "','" . $myIp . "','" . $event . "');";
  875. nr_query($query);
  876. break;
  877. case 'fake':
  878. //just do nothing ^_^
  879. break;
  880. default:
  881. die('Wrong logging type');
  882. break;
  883. }
  884. }
  885. /**
  886. * TODO: This piece of shit must be reviewed and rewritten
  887. * to many existing code use $system->getRightsForUser and $system->checkForRight('ONLINE') or something like
  888. */
  889. /**
  890. * Check if user have specified right
  891. *
  892. * @param string $right
  893. * @param string $username
  894. *
  895. * @return bool
  896. */
  897. public function checkForRight($right = '-any-', $username = '') {
  898. if (empty($username)) {
  899. $rights = &$this->rights;
  900. $root = &$this->root;
  901. } else {
  902. if (!$this->getRightsForUser($username, $rights, $root)) {
  903. return false;
  904. }
  905. }
  906. return $root or ($right == '-any-' && !empty($rights)) or ! empty($rights[$right]);
  907. }
  908. /**
  909. *
  910. * @param string $username
  911. * @param pointer $rights
  912. * @param pointer $root
  913. *
  914. * @return bool
  915. */
  916. protected function getRightsForUser($username, &$rights, &$root) {
  917. if (!($userdata = $this->getUserData($username))) {
  918. return false;
  919. }
  920. $rights = array();
  921. $root = false;
  922. if ($userdata['admin'] !== '*') {
  923. preg_match_all('/\|(.*?)\|/', $userdata['admin'], $rights_r);
  924. foreach ($rights_r[1] as $right) {
  925. $rights[$right] = (empty($this->rights_database[$right])) ? ' ' : $this->rights_database[$right];
  926. }
  927. } else {
  928. $root = true;
  929. }
  930. return true;
  931. }
  932. /**
  933. * Returns some yalfConf option value or false if its not exists
  934. *
  935. * @param string $optionName
  936. *
  937. * @return mixed/bool on error
  938. */
  939. public function getConfigOption($optionName) {
  940. $result = false;
  941. if (isset($this->config[$optionName])) {
  942. $result = $this->config[$optionName];
  943. }
  944. return ($result);
  945. }
  946. /**
  947. * Inits ghost mode for some administrator login
  948. *
  949. * @param string $adminLogin
  950. *
  951. * @return void
  952. */
  953. public function initGhostMode($adminLogin) {
  954. if (file_exists(USERS_PATH . $adminLogin)) {
  955. $userData = $this->getUserData($adminLogin);
  956. if (!empty($userData)) {
  957. $myLogin = whoami();
  958. $myData = $this->getUserData($myLogin);
  959. //current login data is used for ghost mode identification
  960. setcookie($this->cookie_ghost, $myLogin . ':' . $myData['password'], 0);
  961. $_COOKIE[$this->cookie_ghost] = $myLogin . ':' . $myData['password'];
  962. //login of another admin
  963. log_register('GHOSTMODE {' . $myLogin . '} LOGIN AS {' . $adminLogin . '}');
  964. setcookie($this->cookie_user, $adminLogin . ':' . $userData['password'], 0);
  965. $_COOKIE[$this->cookie_user] = $adminLogin . ':' . $userData['password'];
  966. }
  967. }
  968. }
  969. /**
  970. * Deinits ghost mode for current ghost administrator
  971. *
  972. * @return void
  973. */
  974. public function deinitGhostMode() {
  975. global $system;
  976. if (@$_COOKIE[$this->cookie_ghost]) {
  977. $ghostData = explode(':', $_COOKIE[$this->cookie_ghost]);
  978. //cleanup ghostmode data
  979. setcookie($this->cookie_ghost, '', 0);
  980. $_COOKIE[$this->cookie_ghost] = '';
  981. //login of another admin
  982. setcookie($this->cookie_user, $ghostData[0] . ':' . $ghostData[1], 0);
  983. $_COOKIE[$this->cookie_user] = $ghostData[0] . ':' . $ghostData[1];
  984. }
  985. }
  986. }