PageRenderTime 23ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/web/system/ThemeModule/Api/UserApi.php

https://github.com/planetenkiller/core
PHP | 409 lines | 255 code | 65 blank | 89 comment | 77 complexity | c99658e2a6ef399d0c609e0696459cbf MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0
  1. <?php
  2. /**
  3. * Copyright Zikula Foundation 2009 - Zikula Application Framework
  4. *
  5. * This work is contributed to the Zikula Foundation under one or more
  6. * Contributor Agreements and licensed to You under the following license:
  7. *
  8. * @license GNU/LGPLv3 (or at your option, any later version).
  9. * @package Zikula
  10. *
  11. * Please see the NOTICE file distributed with this source code for further
  12. * information regarding copyright and licensing.
  13. */
  14. namespace ThemeModule\Api;
  15. use LogUtil, ModUtil, ThemeUtil, FileUtil, DataUtil, CacheUtil, SecurityUtil, System, UserUtil, ZLanguage;
  16. class UserApi extends \Zikula_AbstractApi
  17. {
  18. /**
  19. * Get all settings for a theme
  20. */
  21. public function getvariables($args)
  22. {
  23. // check our input
  24. if (!isset($args['theme']) || empty($args['theme'])) {
  25. throw new \InvalidArgumentException('Missing or invalid arguments');
  26. }
  27. $args['variables'] = $this->_readinifile(array('theme'=> $args['theme'], 'file' => 'themevariables.ini', 'sections' => true));
  28. if (isset($args['formatting']) && is_bool($args['formatting']) && $args['formatting']) {
  29. $args['variables'] = $this->formatvariables($args);
  30. }
  31. return $args['variables'];
  32. }
  33. /**
  34. * Format the variables of a theme or pageconfiguration
  35. * It uses the theme additional variables as a base which should be variables specifications
  36. */
  37. public function formatvariables($args)
  38. {
  39. // check our input
  40. if (!isset($args['theme']) || empty($args['theme']) || !isset($args['variables']) || empty($args['variables'])) {
  41. throw new \InvalidArgumentException('Missing or invalid arguments');
  42. }
  43. $dom = $this->_getthemedomain($args['theme']);
  44. // take any variables specification from the themevars
  45. $themevars = $this->_readinifile(array('theme'=> $args['theme'], 'file' => 'themevariables.ini', 'sections' => true));
  46. unset($themevars['variables']);
  47. $variables = array_merge($themevars, $args['variables']);
  48. foreach (array_keys($variables['variables']) as $var) {
  49. if (is_array($variables['variables'][$var])) {
  50. // process each array field and insert it in $variables.variables
  51. foreach ($variables['variables'][$var] as $k => $v) {
  52. if (!isset($variables["$var.$k"])) {
  53. $variables["{$var}[{$k}]"] = array('editable' => true, 'type' => 'text');
  54. } else {
  55. $variables["{$var}[{$k}]"] = $variables["$var.$k"];
  56. unset($variables["$var.$k"]);
  57. }
  58. $variables['variables']["{$var}[{$k}]"] = $v;
  59. $this->_variable_options($variables["{$var}[{$k}]"], $args, $dom);
  60. }
  61. unset($variables['variables'][$var]);
  62. } else {
  63. // process the options of the single value
  64. if (!isset($variables[$var])) {
  65. $variables[$var] = array('editable' => true, 'type' => 'text');
  66. }
  67. $this->_variable_options($variables[$var], $args, $dom);
  68. }
  69. }
  70. return $variables;
  71. }
  72. /**
  73. * Internal variable options processor
  74. */
  75. private function _variable_options(&$options, $args, $dom)
  76. {
  77. if (!isset($args['explode']) || $args['explode'] != false) {
  78. if (isset($options['type']) && $options['type'] == 'select') {
  79. $options['values'] = explode(',', $options['values']);
  80. $options['output'] = explode(',', __($options['output'], $dom));
  81. }
  82. }
  83. if (isset($options['language'])) {
  84. $options['language'] = __($options['language'], $dom);
  85. }
  86. }
  87. /**
  88. * Get all paletters for a theme
  89. */
  90. public function getpalettes($args)
  91. {
  92. // check our input
  93. if (!isset($args['theme']) || empty($args['theme'])) {
  94. throw new \InvalidArgumentException('Missing or invalid arguments');
  95. }
  96. return $this->_readinifile(array('theme'=> $args['theme'], 'file' => 'themepalettes.ini', 'sections' => true));
  97. }
  98. /**
  99. * Get one palette for a theme
  100. */
  101. public function getpalette($args)
  102. {
  103. // check our input
  104. if (!isset($args['theme']) || empty($args['theme']) || !isset($args['palette']) || empty($args['palette'])) {
  105. throw new \InvalidArgumentException('Missing or invalid arguments');
  106. }
  107. $allpalettes = ModUtil::apiFunc('ThemeModule', 'user', 'getpalettes', array('theme' => $args['theme']));
  108. return isset($allpalettes[$args['palette']]) ? $allpalettes[$args['palette']] : null;
  109. }
  110. /**
  111. * Get a list of palettes available for a theme
  112. */
  113. public function getpalettenames($args)
  114. {
  115. // check our input
  116. if (!isset($args['theme']) || empty($args['theme'])) {
  117. throw new \InvalidArgumentException('Missing or invalid arguments');
  118. }
  119. $allpalettes = ModUtil::apiFunc('ThemeModule', 'user', 'getpalettes', array('theme' => $args['theme']));
  120. $palettes = array();
  121. foreach (array_keys((array)$allpalettes) as $name) {
  122. $palettes[$name] = $name;
  123. }
  124. return $palettes;
  125. }
  126. /**
  127. * Get all page configurations for a theme
  128. */
  129. public function getpageconfigurations($args)
  130. {
  131. // check our input
  132. if (!isset($args['theme']) || empty($args['theme'])) {
  133. throw new \InvalidArgumentException('Missing or invalid arguments');
  134. }
  135. return $this->_readinifile(array('theme'=> $args['theme'], 'file' => 'pageconfigurations.ini', 'sections' => true));
  136. }
  137. /**
  138. * Get a page configuration for a theme
  139. */
  140. public function getpageconfiguration($args)
  141. {
  142. // check our input
  143. if (!isset($args['theme']) || empty($args['theme'])) {
  144. throw new \InvalidArgumentException('Missing or invalid arguments');
  145. }
  146. if (!isset($args['filename']) || empty($args['filename'])) {
  147. throw new \InvalidArgumentException('Missing or invalid arguments');
  148. }
  149. $config = $this->_readinifile(array('theme'=> $args['theme'], 'file' => $args['filename'], 'sections' => true));
  150. $default = array(
  151. 'page' => '',
  152. 'block' => '',
  153. 'palette' => '', // deprecated
  154. 'modulewrapper' => 1,
  155. 'blockwrapper' => 1,
  156. 'blockinstances' => array(),
  157. 'blocktypes' => array(),
  158. 'blockpositions' => array(),
  159. 'filters' => array(),
  160. 'variables' => array()
  161. );
  162. return array_merge($default, $config);
  163. }
  164. /**
  165. * Get all configurations available for a theme
  166. */
  167. public function getconfigurations($args)
  168. {
  169. // check our input
  170. if (!isset($args['theme']) || empty($args['theme'])) {
  171. throw new \InvalidArgumentException('Missing or invalid arguments');
  172. }
  173. $themeinfo = ThemeUtil::getInfo(ThemeUtil::getIDFromName($args['theme']));
  174. $templatedir = ZIKULA_ROOT.'/themes/'.DataUtil::formatForOS($themeinfo['directory']).'/Resources/config';
  175. // get the available .ini files and exclude the core ones
  176. $inifiles = FileUtil::getFiles($templatedir, false, true, '.ini', 'f');
  177. $inifiles = array_diff($inifiles, array('admin.ini', 'pageconfigurations.ini', 'themevariables.ini', 'themepalettes.ini'));
  178. sort($inifiles);
  179. return $inifiles;
  180. }
  181. /**
  182. * Get all templates for a theme
  183. */
  184. public function gettemplates($args)
  185. {
  186. // check our input
  187. if (!isset($args['theme']) || empty($args['theme'])) {
  188. throw new \InvalidArgumentException('Missing or invalid arguments');
  189. }
  190. $args['type'] = isset($args['type']) ? DataUtil::formatForOS($args['type']) : 'modules';
  191. $themeinfo = ThemeUtil::getInfo(ThemeUtil::getIDFromName($args['theme']));
  192. $templatedir = ZIKULA_ROOT.'/themes/'.DataUtil::formatForOS($themeinfo['directory']).'/Resources/view';
  193. if ($args['type'] == 'modules') {
  194. // for module templates also search on the theme/templates folder
  195. $templatelist = FileUtil::getFiles($templatedir, false, true, array('.tpl', '.htm'), 'f');
  196. } else {
  197. $templatelist = array();
  198. }
  199. $templatelist = array_merge($templatelist, FileUtil::getFiles($templatedir.'/'.$args['type'], false, $args['type'], array('.tpl', '.htm'), 'f'));
  200. return $templatelist;
  201. }
  202. /**
  203. * read an ini file from either the master theme config or running config
  204. *
  205. */
  206. function _readinifile($args)
  207. {
  208. // check our input
  209. if (!isset($args['file']) || empty($args['file'])) {
  210. throw new \InvalidArgumentException('Missing or invalid arguments');
  211. }
  212. if (!isset($args['theme']) || empty($args['theme'])) {
  213. throw new \InvalidArgumentException('Missing or invalid arguments');
  214. }
  215. // get the theme info
  216. $themeinfo = ThemeUtil::getInfo(ThemeUtil::getIDFromName($args['theme']));
  217. // set the section parse flag
  218. if (!isset($args['sections']) || !is_bool($args['sections'])) {
  219. $args['sections'] = false;
  220. }
  221. $ostemp = CacheUtil::getLocalDir();
  222. $ostheme = DataUtil::formatForOS($themeinfo['directory']);
  223. $osfile = DataUtil::formatForOS($args['file']);
  224. if (file_exists($ostemp.'/Theme_Config/'.$ostheme.'/'.$osfile)) {
  225. return parse_ini_file($ostemp.'/Theme_Config/'.$ostheme.'/'.$osfile, $args['sections']);
  226. } else if (file_exists(ZIKULA_ROOT.'/themes/'.$ostheme.'/Resources/config/'.$osfile)) {
  227. return parse_ini_file(ZIKULA_ROOT.'/themes/'.$ostheme.'/Resources/config/'.$osfile, $args['sections']);
  228. }
  229. }
  230. /**
  231. * write an ini file to the running configuration directory
  232. *
  233. * @return boolean
  234. */
  235. public function writeinifile($args)
  236. {
  237. // check our input
  238. if (!isset($args['file']) || empty($args['file'])) {
  239. throw new \InvalidArgumentException('Missing or invalid arguments');
  240. }
  241. if (!isset($args['theme']) || empty($args['theme'])) {
  242. throw new \InvalidArgumentException('Missing or invalid arguments');
  243. }
  244. // get the theme info
  245. $themeinfo = ThemeUtil::getInfo(ThemeUtil::getIDFromName($args['theme']));
  246. $content = ModUtil::apiFunc('themeModule', 'user', 'createinifile', array('has_sections' => $args['has_sections'], 'assoc_arr' => $args['assoc_arr']));
  247. $ostemp = CacheUtil::getLocalDir();
  248. $ostheme = DataUtil::formatForOS($themeinfo['directory']);
  249. $osfile = DataUtil::formatForOS($args['file']);
  250. // verify the writable paths
  251. $tpath = ZIKULA_ROOT.'/themes/'.$ostheme.'/Resources/config';
  252. if (is_writable($tpath.'/'.$osfile)) {
  253. $handle = fopen($tpath.'/'.$osfile, 'w+');
  254. } else {
  255. if (!file_exists($zpath = $ostemp.'/Theme_Config/'.$ostheme)) {
  256. mkdir($zpath, $this->container['system.chmod_dir'], true);
  257. }
  258. if (!file_exists($zpath.'/'.$osfile) || is_writable($zpath.'/'.$osfile)) {
  259. $handle = fopen($zpath.'/'.$osfile, 'w+');
  260. } else {
  261. return LogUtil::registerError($this->__f('Error! Cannot write in \'%1$s\' or \'%2$s\' to store the
  262. contents of \'%3$s\'.', array($tpath, $zpath, $osfile)));
  263. }
  264. }
  265. // validate the resulting handler and the write operation result
  266. if (!isset($handle) || !is_resource($handle)) {
  267. return LogUtil::registerError($this->__f('Error! Could not open file so that it could be written to: %s', $osfile));
  268. } else {
  269. if (fwrite($handle, $content) === false) {
  270. fclose($handle);
  271. return LogUtil::registerError($this->__f('Error! Could not write to file: %s', $osfile));
  272. }
  273. fclose($handle);
  274. return true;
  275. }
  276. }
  277. /**
  278. * create an ini file
  279. *
  280. * @return mixed string ini file contents if succesful, boolean false otherwise
  281. */
  282. public function createinifile($args)
  283. {
  284. if (!isset($args['assoc_arr']) || empty($args['assoc_arr'])) {
  285. return false;
  286. }
  287. if (!isset($args['has_sections'])) {
  288. $args['has_sections'] = true;
  289. }
  290. $content = '';
  291. if ($args['has_sections']) {
  292. foreach ($args['assoc_arr'] as $section => $sectionval) {
  293. // process and write each section value
  294. if (!is_array($sectionval)) {
  295. $content .= "$section = $sectionval\r\n";
  296. } else {
  297. $content .= "\r\n[$section]\r\n";
  298. foreach ($sectionval as $var => $value) {
  299. if (is_array($value)) {
  300. foreach ($value as $k => $v) {
  301. $content .= "{$var}[{$k}] = $v\r\n";
  302. }
  303. } else {
  304. $content .= "$var = $value\r\n";
  305. }
  306. }
  307. }
  308. }
  309. } else {
  310. foreach ($args['assoc_arr'] as $key => $value) {
  311. $content .= "$key = $value\r\n";
  312. }
  313. }
  314. return $content;
  315. }
  316. /**
  317. * Reset the current users theme to the site default
  318. */
  319. public function resettodefault($args)
  320. {
  321. // Security check
  322. if (!System::getVar('theme_change')) {
  323. return LogUtil::registerError($this->__('Notice: Theme switching is currently disabled.'));
  324. }
  325. if (!SecurityUtil::checkPermission('Theme::', '::', ACCESS_COMMENT)) {
  326. throw new \Zikula\Framework\Exception\ForbiddenException();
  327. }
  328. // update the users record to an empty string - if this user var is empty then the site default is used.
  329. UserUtil::setVar('theme', '');
  330. return true;
  331. }
  332. /**
  333. * Retrieves the theme domain
  334. *
  335. * @param string $themename Name of the theme to parse
  336. */
  337. function _getthemedomain($themename)
  338. {
  339. if (in_array($themename, array('Andreas08Theme', 'AtomTheme', 'PrinterTheme', 'RssTheme', 'SeaBreezeTheme'))) {
  340. return 'zikula';
  341. }
  342. return ZLanguage::getThemeDomain($themename);
  343. }
  344. }