/plugins/extensions/controllers/extensions_themes_controller.php

https://github.com/ShadowCross/croogo · PHP · 195 lines · 142 code · 24 blank · 29 comment · 36 complexity · 1054872d11fcb1259904c08caa7ce89b MD5 · raw file

  1. <?php
  2. /**
  3. * Extensions Themes Controller
  4. *
  5. * PHP version 5
  6. *
  7. * @category Controller
  8. * @package Croogo
  9. * @version 1.0
  10. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  11. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  12. * @link http://www.croogo.org
  13. */
  14. class ExtensionsThemesController extends AppController {
  15. /**
  16. * Controller name
  17. *
  18. * @var string
  19. * @access public
  20. */
  21. public $name = 'ExtensionsThemes';
  22. /**
  23. * Models used by the Controller
  24. *
  25. * @var array
  26. * @access public
  27. */
  28. public $uses = array('Setting', 'User');
  29. public function beforeFilter() {
  30. parent::beforeFilter();
  31. App::import('Core', 'File');
  32. App::import('Core', 'Folder');
  33. }
  34. public function admin_index() {
  35. $this->set('title_for_layout', __('Themes', true));
  36. $themes = $this->Croogo->getThemes();
  37. $themesData = array();
  38. $themesData[] = $this->Croogo->getThemeData();
  39. foreach ($themes AS $theme) {
  40. $themesData[$theme] = $this->Croogo->getThemeData($theme);
  41. }
  42. $currentTheme = $this->Croogo->getThemeData(Configure::read('Site.theme'));
  43. $this->set(compact('themes', 'themesData', 'currentTheme'));
  44. }
  45. public function admin_activate($alias = null) {
  46. if ($alias == 'default') {
  47. $alias = null;
  48. }
  49. if (!isset($this->params['named']['token']) || ($this->params['named']['token'] != $this->params['_Token']['key'])) {
  50. $blackHoleCallback = $this->Security->blackHoleCallback;
  51. $this->$blackHoleCallback();
  52. }
  53. $siteTheme = $this->Setting->findByKey('Site.theme');
  54. $siteTheme['Setting']['value'] = $alias;
  55. $this->Setting->save($siteTheme);
  56. $this->Session->setFlash(__('Theme activated.', true), 'default', array('class' => 'success'));
  57. $this->redirect(array('action' => 'index'));
  58. }
  59. public function admin_add() {
  60. $this->set('title_for_layout', __('Upload a new theme', true));
  61. if (!empty($this->data)) {
  62. $file = $this->data['Theme']['file'];
  63. unset($this->data['Theme']['file']);
  64. // get Theme YML and alias
  65. $ymlContent = '';
  66. $zip = zip_open($file['tmp_name']);
  67. if ($zip) {
  68. while ($zip_entry = zip_read($zip)) {
  69. $zipEntryName = zip_entry_name($zip_entry);
  70. if (strstr($zipEntryName, 'theme.yml') &&
  71. zip_entry_open($zip, $zip_entry, "r")) {
  72. $ymlContent = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
  73. $zipEntryNameE = explode('/', $zipEntryName);
  74. if (isset($zipEntryNameE['0'])) {
  75. $themeAlias = $zipEntryNameE[count($zipEntryNameE) - 3];
  76. }
  77. }
  78. }
  79. zip_close($zip);
  80. }
  81. if ($ymlContent == '') {
  82. $this->Session->setFlash(__('Invalid YML file', true), 'default', array('class' => 'error'));
  83. $this->redirect(array('action' => 'index'));
  84. }
  85. // check if alias already exists
  86. if (!isset($themeAlias)) {
  87. $this->Session->setFlash(__('Invalid zip archive', true), 'default', array('class' => 'error'));
  88. $this->redirect(array('action' => 'index'));
  89. }
  90. if (is_dir(APP.'views'.DS.'themed'.DS.$themeAlias) ||
  91. is_dir(APP.'webroot'.DS.'themed'.DS.$themeAlias)) {
  92. $this->Session->setFlash(__('Directory with theme alias already exists.', true), 'default', array('class' => 'error'));
  93. $this->redirect(array('action' => 'add'));
  94. }
  95. // extract it
  96. $zip = zip_open($file['tmp_name']);
  97. if ($zip) {
  98. while ($zip_entry = zip_read($zip)) {
  99. $zipEntryName = zip_entry_name($zip_entry);
  100. if (strstr($zipEntryName, $themeAlias . '/')) {
  101. $zipEntryNameE = explode($themeAlias . '/', $zipEntryName);
  102. if (isset($zipEntryNameE['1'])) {
  103. $path = APP . 'views' . DS . 'themed' . DS . $themeAlias . DS . str_replace('/', DS, $zipEntryNameE['1']);
  104. } else {
  105. $path = APP . 'views' . DS . 'themed' . DS . $themeAlias . DS;
  106. }
  107. if (substr($path, strlen($path) - 1) == DS) {
  108. // create directory
  109. mkdir($path);
  110. } else {
  111. // create file
  112. if (zip_entry_open($zip, $zip_entry, 'r')) {
  113. $fileContent = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
  114. touch($path);
  115. $fh = fopen($path, 'w');
  116. fwrite($fh, $fileContent);
  117. fclose($fh);
  118. zip_entry_close($zip_entry);
  119. }
  120. }
  121. }
  122. }
  123. zip_close($zip);
  124. $this->Session->setFlash(__('Theme uploaded successfully.', true), 'default', array('class' => 'success'));
  125. $this->redirect(array('action' => 'index'));
  126. }
  127. }
  128. }
  129. public function admin_editor() {
  130. $this->set('title_for_layout', __('Theme Editor', true));
  131. }
  132. public function admin_save() {
  133. }
  134. public function admin_delete($alias = null) {
  135. if ($alias == null) {
  136. $this->Session->setFlash(__('Invalid Theme.', true), 'default', array('class' => 'error'));
  137. $this->redirect(array('action' => 'index'));
  138. }
  139. if (!isset($this->params['named']['token']) || ($this->params['named']['token'] != $this->params['_Token']['key'])) {
  140. $blackHoleCallback = $this->Security->blackHoleCallback;
  141. $this->$blackHoleCallback();
  142. }
  143. if ($alias == 'default') {
  144. $this->Session->setFlash(__('Default theme cannot be deleted.', true), 'default', array('class' => 'error'));
  145. $this->redirect(array('action' => 'index'));
  146. } elseif ($alias == Configure::read('Site.theme')) {
  147. $this->Session->setFlash(__('You cannot delete a theme that is currently active.', true), 'default', array('class' => 'error'));
  148. $this->redirect(array('action' => 'index'));
  149. }
  150. $paths = array(
  151. APP . 'webroot' . DS . 'theme' . DS . $alias . DS,
  152. APP . 'views' . DS . 'themed' . DS . $alias . DS,
  153. );
  154. $error = 0;
  155. $folder =& new Folder;
  156. foreach ($paths AS $path) {
  157. if (is_dir($path)) {
  158. if (!$folder->delete($path)) {
  159. $error = 1;
  160. }
  161. }
  162. }
  163. if ($error == 1) {
  164. $this->Session->setFlash(__('An error occurred.', true), 'default', array('class' => 'error'));
  165. } else {
  166. $this->Session->setFlash(__('Theme deleted successfully.', true), 'default', array('class' => 'success'));
  167. }
  168. $this->redirect(array('action' => 'index'));
  169. }
  170. }
  171. ?>