api.taskbar.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. /**
  3. * Taskbar loading and rendering class
  4. */
  5. class Taskbar {
  6. /**
  7. * Contains system alter config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Message helper object placeholder
  14. *
  15. * @var object
  16. */
  17. protected $messages = '';
  18. /**
  19. * Contains currently loaded categories as dir=>name
  20. *
  21. * @var array
  22. */
  23. protected $categories = array();
  24. /**
  25. * Contains current run alerts if available
  26. *
  27. * @var string
  28. */
  29. protected $currentAlerts = '';
  30. /**
  31. * Contains full list of loaded taskbar elements
  32. *
  33. * @var array
  34. */
  35. protected $loadedElements = array();
  36. /**
  37. * Taskbar elements rendered content
  38. *
  39. * @var string
  40. */
  41. protected $taskbarContent = '';
  42. /**
  43. * Contains default icon size
  44. *
  45. * @var int
  46. */
  47. protected $iconSize = 128;
  48. /**
  49. * Contains default taskbar elements path
  50. */
  51. const BASE_PATH = 'config/taskbar.d/';
  52. /**
  53. * Contains path to widgets code
  54. */
  55. const WIDGETS_CODEPATH = 'config/taskbar.d/widgets/';
  56. /**
  57. * Contains default module URL
  58. */
  59. const URL_ME = '?module=taskbar';
  60. /**
  61. * Creates new taskbar instance
  62. */
  63. public function __construct() {
  64. $this->loadConfigs();
  65. $this->initMessages();
  66. $this->setCategories();
  67. }
  68. /**
  69. * Loads system alter config into protected property
  70. *
  71. * @global object $ubillingConfig
  72. *
  73. * @return void
  74. */
  75. protected function loadConfigs() {
  76. global $ubillingConfig;
  77. $this->altCfg = $ubillingConfig->getAlter();
  78. }
  79. /**
  80. * Inits system message helper object
  81. *
  82. * @return void
  83. */
  84. protected function initMessages() {
  85. $this->messages = new UbillingMessageHelper();
  86. }
  87. /**
  88. * Sets available taskbar element categories
  89. *
  90. * @return void
  91. */
  92. protected function setCategories() {
  93. $this->categories['widgets'] = '';
  94. $this->categories['playback'] = __('Playback');
  95. $this->categories['settings'] = __('Settings');
  96. $this->categories['system'] = __('System');
  97. }
  98. /**
  99. * Sets current administrators login into protected prof for further usage
  100. *
  101. * @return void
  102. */
  103. protected function setLogin() {
  104. $this->myLogin = whoami();
  105. }
  106. /**
  107. * Renders taskbar icon element
  108. *
  109. * @param string $url
  110. * @param string $name
  111. * @param string $icon
  112. *
  113. * @return string
  114. */
  115. protected function renderIconElement($url, $elementName, $elementIcon) {
  116. $result = '';
  117. $name = __($elementName);
  118. $icon = 'skins/taskbar/' . $elementIcon;
  119. $result = '<div class="dashtask" style="height:' . ($this->iconSize + 38) . 'px; width:' . ($this->iconSize + 38) . 'px;"> <a href="' . $url . '"><img src="' . $icon . '" border="0" width="' . $this->iconSize . '" height="' . $this->iconSize . '" alt="' . $name . '" title="' . $name . '"></a> <br><br>' . $name . ' </div>';
  120. return ($result);
  121. }
  122. /**
  123. * Checks element required rights, options and returns element content
  124. *
  125. * @param array $elementData
  126. *
  127. * @return string
  128. */
  129. protected function buildElement($elementData) {
  130. $result = '';
  131. $elementId = (isset($elementData['ID'])) ? $elementData['ID'] : '';
  132. $elementType = (!empty($elementData['TYPE'])) ? $elementData['TYPE'] : '';
  133. //basic taskbar icon
  134. if ($elementType == 'icon') {
  135. $accesCheck = false;
  136. $elementRight = (!empty($elementData['NEED_RIGHT'])) ? $elementData['NEED_RIGHT'] : '';
  137. if (!empty($elementRight)) {
  138. if (cfr($elementRight)) {
  139. $accesCheck = true;
  140. }
  141. } else {
  142. $accesCheck = true;
  143. }
  144. //basic rights check
  145. if ($accesCheck) {
  146. $elementOption = (!empty($elementData['NEED_OPTION'])) ? $elementData['NEED_OPTION'] : '';
  147. $optionCheck = false;
  148. if (!empty($elementOption)) {
  149. if (isset($this->altCfg[$elementOption])) {
  150. if ($this->altCfg[$elementOption]) {
  151. $optionCheck = true;
  152. }
  153. } else {
  154. if (!isset($elementData['UNIMPORTANT'])) {
  155. $this->currentAlerts .= $this->messages->getStyledMessage(__('Missed config option') . ': ' . $elementOption . ' ' . __('required by') . ' ' . $elementId, 'error');
  156. }
  157. }
  158. } else {
  159. $optionCheck = true;
  160. }
  161. if ($optionCheck) {
  162. $elementName = (!empty($elementData['NAME'])) ? $elementData['NAME'] : '';
  163. $elementUrl = (!empty($elementData['URL'])) ? $elementData['URL'] : '';
  164. $elementIcon = (!empty($elementData['ICON'])) ? $elementData['ICON'] : '';
  165. $result .= $this->renderIconElement($elementUrl, $elementName, $elementIcon);
  166. }
  167. }
  168. }
  169. //widgets loading
  170. if ($elementType == 'widget') {
  171. $accesCheck = false;
  172. $elementRight = (!empty($elementData['NEED_RIGHT'])) ? $elementData['NEED_RIGHT'] : '';
  173. if (!empty($elementRight)) {
  174. if (cfr($elementRight)) {
  175. $accesCheck = true;
  176. }
  177. } else {
  178. $accesCheck = true;
  179. }
  180. //basic rights check
  181. if ($accesCheck) {
  182. $elementOption = (!empty($elementData['NEED_OPTION'])) ? $elementData['NEED_OPTION'] : '';
  183. $optionCheck = false;
  184. if (!empty($elementOption)) {
  185. if (isset($this->altCfg[$elementOption])) {
  186. if ($this->altCfg[$elementOption]) {
  187. $optionCheck = true;
  188. }
  189. } else {
  190. if (!isset($elementData['UNIMPORTANT'])) {
  191. $this->currentAlerts .= $this->messages->getStyledMessage(__('Missed config option') . ': ' . $elementOption . ' ' . __('required by') . ' ' . $elementId, 'error');
  192. }
  193. }
  194. } else {
  195. $optionCheck = true;
  196. }
  197. if ($optionCheck) {
  198. //run widget code
  199. if (isset($elementData['CODEFILE'])) {
  200. if (file_exists(self::WIDGETS_CODEPATH . $elementData['CODEFILE'])) {
  201. require_once(self::WIDGETS_CODEPATH . $elementData['CODEFILE']);
  202. if (class_exists($elementData['ID'])) {
  203. $widget = new $elementData['ID']();
  204. $result .= $widget->render();
  205. } else {
  206. $this->currentAlerts .= $this->messages->getStyledMessage(__('Widget class not exists') . ': ' . $elementData['ID'], 'error');
  207. }
  208. } else {
  209. $this->currentAlerts .= $this->messages->getStyledMessage(__('File not exist') . ': ' . self::WIDGETS_CODEPATH . $elementData['CODEFILE'], 'warning');
  210. }
  211. } else {
  212. $this->currentAlerts .= $this->messages->getStyledMessage(__('Wrong element format') . ': ' . $elementData['ID'], 'warning');
  213. }
  214. }
  215. }
  216. }
  217. return ($result);
  218. }
  219. /**
  220. * Loads and returns category taskbar elements
  221. *
  222. * @param string $category
  223. *
  224. * @return string
  225. */
  226. protected function loadCategoryElements($category) {
  227. $result = '';
  228. $elementsPath = self::BASE_PATH . $category . '/';
  229. $allElements = rcms_scandir($elementsPath, '*.ini');
  230. $categoryContent = '';
  231. if (!empty($allElements)) {
  232. $categoryName = (isset($this->categories[$category])) ? $this->categories[$category] : '';
  233. foreach ($allElements as $io => $eachfilename) {
  234. $elementData = parse_ini_file($elementsPath . $eachfilename);
  235. if ((isset($elementData['TYPE'])) and (isset($elementData['ID']))) {
  236. if (!isset($this->loadedElements[$elementData['ID']])) {
  237. $this->loadedElements[$elementData['ID']] = $elementData;
  238. $categoryContent .= $this->buildElement($elementData);
  239. } else {
  240. $this->currentAlerts .= $this->messages->getStyledMessage(__('Duplicate element ID') . ': ' . $elementData['ID'] . ' -> ' . $eachfilename, 'warning');
  241. }
  242. } else {
  243. $this->currentAlerts .= $this->messages->getStyledMessage(__('Wrong element format') . ': ' . $eachfilename, 'warning');
  244. }
  245. }
  246. //injecting optional ReportMaster reports here
  247. if ($category == 'reports') {
  248. if (@$this->altCfg['TB_REPORTMASTER']) {
  249. $reportMaster = new ReportMaster();
  250. $availableReports = $reportMaster->getTaskBarReports();
  251. if (!empty($availableReports)) {
  252. foreach ($availableReports as $eachReportId => $eachReportElement) {
  253. $categoryContent .= $this->buildElement($eachReportElement);
  254. }
  255. }
  256. }
  257. }
  258. if (!empty($categoryContent)) {
  259. $result .= wf_tag('p') . wf_tag('h3') . wf_tag('u') . $categoryName . wf_tag('u', true) . wf_tag('h3', true) . wf_tag('p', true);
  260. $result .= wf_tag('div', false, 'dashboard');
  261. $result .= $categoryContent;
  262. $result .= wf_tag('div', true);
  263. $result .= wf_CleanDiv();
  264. }
  265. }
  266. return ($result);
  267. }
  268. /**
  269. * Loads and try to render all of available taskbar categories
  270. *
  271. * @return string
  272. */
  273. protected function loadAllCategories() {
  274. $result = '';
  275. if (!empty($this->categories)) {
  276. foreach ($this->categories as $category => $categoryname) {
  277. $result .= $this->loadCategoryElements($category);
  278. }
  279. }
  280. return ($result);
  281. }
  282. /**
  283. * Checks for default password usage, etc.
  284. *
  285. * @return void
  286. */
  287. protected function checkSecurity() {
  288. global $system;
  289. $controlLogin = 'admin';
  290. $badPasswords = file_get_contents(DATA_PATH . 'shitpass.dat');
  291. $badPasswords = trim($badPasswords);
  292. $badPasswords = explodeRows($badPasswords);
  293. $defaultPassOffset = 0;
  294. if (isset($_COOKIE['yalf_user'])) {
  295. if (!file_exists('DEMO_MODE')) {
  296. //am i using default account?
  297. if ($_COOKIE['yalf_user'] == $controlLogin . ':' . $badPasswords[$defaultPassOffset]) {
  298. $notice = __('You are using the default login and password') . '. ' . __('Dont do this') . '.';
  299. // ugly hack to prevent elements autofocusing
  300. $label = wf_TextInput('dontfocusonlinks', '', '', false, '', '', '', '', 'style="width: 0; height: 0; top: -100px; position: absolute;"');
  301. $label .= wf_tag('div', false, '', 'style="min-width:550px;"') . $this->messages->getStyledMessage($notice, 'error') . wf_tag('div', true);
  302. $label .= wf_tag('br');
  303. $label = wf_TextInput('dontfocusonlinks', '', '', false, '', '', '', '', 'style="width: 0; height: 0; top: -100px; position: absolute;"');
  304. $label .= wf_tag('div', false, '', 'style="min-width:600px;"') . $this->messages->getStyledMessage($notice, 'error') . wf_tag('div', true);
  305. $label .= wf_tag('br');
  306. $label .= wf_tag('center') . wf_img_sized('skins/securitywolf.png', '', '', '300') . wf_tag('center' . true);
  307. $label .= wf_delimiter(1);
  308. $label .= wf_Link('?module=usermanager&edituserdata=admin', __('Change admin user password'), true, 'confirmagree');
  309. $this->currentAlerts .= wf_modalOpenedAuto(__('Oh no') . '!' . ' ' . __('Danger') . '!', $label);
  310. } else {
  311. //ony administrators(?) should see that
  312. if (cfr('OPERATOR')) {
  313. //fast check for few shitty passwords
  314. if (file_exists(USERS_PATH . $controlLogin)) {
  315. $adminData = $system->getUserData($controlLogin);
  316. if (!empty($adminData)) {
  317. $adminHash = trim($adminData['password']);
  318. foreach ($badPasswords as $passIdx => $eachHash) {
  319. if (!empty($eachHash)) {
  320. $eachHash = trim($eachHash);
  321. if (strpos($adminHash, $eachHash) !== false) {
  322. $this->currentAlerts .= $this->messages->getStyledMessage(__('For administrator') . ' «' . $controlLogin . '» ' . __('a very fucked up password is used') . '. ' . __('Dont do this') . '.', 'error');
  323. }
  324. }
  325. }
  326. }
  327. }
  328. }
  329. }
  330. }
  331. }
  332. }
  333. /**
  334. * Returns rendered taskbar elements and services content
  335. *
  336. * @return string
  337. */
  338. public function renderTaskbar() {
  339. $result = '';
  340. $this->taskbarContent = $this->loadAllCategories();
  341. $this->checkSecurity();
  342. if (!empty($this->currentAlerts)) {
  343. $result .= $this->currentAlerts;
  344. }
  345. $result .= $this->taskbarContent;
  346. return ($result);
  347. }
  348. }
  349. /**
  350. * Basic taskbar widgets class.
  351. */
  352. class TaskbarWidget {
  353. /**
  354. * Creates new instance of taskbar widget
  355. */
  356. public function __construct() {
  357. }
  358. /**
  359. * Returns content in default taskbar dashtask coontainer
  360. *
  361. * @param string $content
  362. * @param string $options
  363. *
  364. * @return string
  365. */
  366. protected function widgetContainer($content, $options = '') {
  367. $result = wf_tag('div', false, 'dashtask', $options);
  368. $result .= $content;
  369. $result .= wf_tag('div', true);
  370. return ($result);
  371. }
  372. /**
  373. * Returns result that directly embeds into taskbar
  374. *
  375. * @return string
  376. */
  377. public function render() {
  378. $result = 'EMPTY_WIDGET';
  379. return ($result);
  380. }
  381. }