PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/jsrewrite/jfx-private/modules/core/actions/modules/list.php

http://jfxcms.googlecode.com/
PHP | 417 lines | 252 code | 110 blank | 55 comment | 59 complexity | 3ac322f306c1f1357f8e1b0c5bd617df MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. ############### COPYLEFT GPLv3 LICENSE ###############
  3. ##
  4. ## JFX Version 0.2.7
  5. ## Website Management Software
  6. ## www.jfxcms.com
  7. ##
  8. ## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
  9. ##
  10. ## Anthony Gallon
  11. ## oi_antz@hotmail.com
  12. ##
  13. ## Permission is hereby granted to any person having a copy of this software
  14. ## to freely use and modify as required so long as the copyright notices
  15. ## and branding remain intact.
  16. ##
  17. ## Full license details available at http://www.jfxcms.com/license
  18. ##
  19. ############### COPYLEFT GPLv3 LICENSE ###############
  20. $THEME->addBreadcrumb($this->adminUrl.'/modules/list/', $this->lang('ap_title_modules_list'));
  21. JFX::checkAdminPermsRedirect('core', 'perm_manage_modules');
  22. if(Antz_IntelliForm::submitted('upload-module')){
  23. if(isset($_FILES['file']) && file_exists($_FILES['file']['tmp_name'])){
  24. $error = false;
  25. $fc = file_get_contents($_FILES['file']['tmp_name']);
  26. $moduleParams = unserialize($fc);
  27. if(!is_array($moduleParams) ||
  28. !array_key_exists('m', $moduleParams) ||
  29. $moduleParams['m'] != strtolower($moduleParams['m']) ||
  30. preg_replace('/[^a-zA-Z0-9]/', '', $moduleParams['m']) != $moduleParams['m']){
  31. JFX::addError('The file is not a valid module export');
  32. $error = true;
  33. }else{
  34. $moduleKey = $moduleParams['m'];
  35. }
  36. $isTheme = JFX_Theme::isValidTheme($moduleKey);
  37. $isModule = JFX_Module::isValidModule($moduleKey);
  38. if((!$error) && ($isTheme && JFX_Theme::isInstalled($moduleKey) || $isModule && JFX_Module::isInstalled($moduleKey)) && (post('overwrite')!=1)){
  39. $error = true;
  40. JFX::addError('Module "'.$moduleKey.'" is already installed');
  41. }
  42. if(!$error){
  43. // now we are going to process
  44. if($array_key_exists('is_theme', $moduleParams) && (bool) $moduleParams['is_theme']) $dirname = $CONFIG->themesDir.'/'.$moduleKey;
  45. else $dirname = $CONFIG->modulesDir.'/'.$moduleKey;
  46. $MM = JFX::registry('JFX_Module_Manager');
  47. $MM->import($_FILES['file']['tmp_name'], $dirname);
  48. JFX::addSuccess('Module imported!');
  49. }
  50. }
  51. }
  52. if(get('download')!=''){
  53. $module = get('download');
  54. $isModule = JFX_Module::isValidModule($module);
  55. $isTheme = JFX_Theme::isValidTheme($module);
  56. if($isTheme || $isModule){
  57. if($isTheme){
  58. $className = 'JFX_Theme_'.$module;
  59. $contentType = 'application/jfx-theme';
  60. $ext = 'jfxt';
  61. }else{
  62. $className = 'JFX_Module_'.$module;
  63. $contentType = 'application/jfx-module';
  64. $ext = 'jfxm';
  65. }
  66. $object = JFX::registry($className);
  67. $tmpName = $CONFIG->tmpDir.'/'.get('download').'.moduleexport.'.$ext;
  68. $object->saveExport($tmpName);
  69. while(ob_get_level()>0) ob_end_clean();
  70. header('Content-type: '.$contentType);
  71. header('Content-disposition: attachment; filename='.$object->keyname.'_'.$object->getVersion().'.'.$ext);
  72. readfile($tmpName);
  73. exit;
  74. }
  75. }
  76. if(get('install')!=''){
  77. $module = get('install');
  78. $isModule = JFX_Module::isValidModule($module);
  79. $isTheme = JFX_Theme::isValidTheme($module);
  80. if(!$isTheme && !$isModule){
  81. JFX::addError('Invalid module to install');
  82. }else{
  83. if(JFX_Module::isInstalled($module) || JFX_Theme::isInstalled($module)){
  84. JFX::addError('The module is already installed');
  85. }else{
  86. $className = ($isTheme)
  87. ? 'JFX_Theme_'.ucfirst(strtolower($module))
  88. : 'JFX_Module_'.ucfirst(strtolower($module))
  89. ;
  90. $object = JFX::registry($className);
  91. if(!is_object($object)){
  92. JFX::addError('Could not instantiate the module');
  93. $error = true;
  94. }else{
  95. $error = false;
  96. // check dependencies
  97. $deps = $object->getDependencies();
  98. foreach($deps as $dep){
  99. $depName = $dep['keyname'];
  100. $depVersion = explode('.', $dep['version']);
  101. if($DB->countRows($CONFIG->dbprefix.'modules', "keyname = '{$depName}'")==0){
  102. JFX::addError('Requires module "'.$depName.'" version '.implode('.', $depVersion).' to be installed');
  103. $error = true;
  104. continue;
  105. }else{
  106. $version = $DB->oneValue($CONFIG->dbprefix.'modules', 'version', "keyname = '{$depName}'");
  107. $version = explode('.', $version);
  108. foreach($version as $k=>$v){
  109. if((int) $depVersion[$k] > $version[$k]){
  110. JFX::addError('Module "'.$depName.'" is only verion '.implode('.', $version).', requires '.implode('.', $depVersion));
  111. $error = true;
  112. break;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. if(!$error){
  119. // lets install the module
  120. $dbParams = array(
  121. 'keyname' => $module,
  122. 'version' => $object->getVersion(),
  123. 'title' => $object->getTitle(),
  124. 'sorting' => (1+$DB->oneValue($CONFIG->dbprefix.'modules', 'sorting', "1 ORDER BY sorting DESC"))
  125. );
  126. $result = $object->install();
  127. if($isTheme){
  128. $tablename = $CONFIG->dbprefix.'themes';
  129. }else{
  130. $tablename = $CONFIG->dbprefix.'modules';
  131. }
  132. $DB->insert($tablename, $dbParams);
  133. $LANG->update('core', 'perm_module_admin_'.$module, 'Manage '.$object->getTitle());
  134. $dbParams = array(
  135. 'keyname' => 'perm_module_admin_'.$module,
  136. 'module_key' => 'core'
  137. );
  138. if($result !== false){
  139. $DB->insert($CONFIG->dbprefix.'admin_permissions', $dbParams);
  140. JFX::addSuccess('Module was installed');
  141. }else{
  142. JFX::addError('Error installing module');
  143. }
  144. }
  145. JFX::redirect($CONFIG->adminUrl.'/modules/list/');
  146. }
  147. }
  148. }
  149. if(get('uninstall')!=''){
  150. $module = get('uninstall');
  151. if($module == $this->keyname){
  152. JFX::addError($this->lang('cannot_uninstall_core_module'));
  153. JFX::redirect($this->adminUrl.'/modules/list/');
  154. }
  155. if(!JFX_Module::isValidModule($module)){
  156. JFX::addError('Invalid module to uninstall');
  157. }else{
  158. $moduleObj = JFX_Module::getInstance($module);
  159. if(strlen(post('confirm'))==0){
  160. $error = true;
  161. $formFields = array(
  162. array(
  163. 'type' => 'longtag',
  164. 'tag' => 'h2',
  165. 'content' => 'Uninstall '.$moduleObj->getTitle()
  166. ),
  167. array(
  168. 'type'=>'submit',
  169. 'name' => 'confirm',
  170. 'value' => $this->lang('confirm_uninstall'),
  171. 'label' => '&nbsp;'
  172. )
  173. );
  174. $form = JFX::makeRapidForm($formFields, 'Uninstall Module: '.$moduleObj->getTitle(), 'uninstall-module');
  175. echo (string) $form;
  176. }else{
  177. // form submitted and confirmed
  178. if(!JFX_Module::isInstalled($module)){
  179. JFX::addError('The module is not installed');
  180. }else{
  181. // lets install the module
  182. $safeModule = preg_replace('/[^a-zA-Z0-9]/', '', $module);
  183. $object = JFX::registry('JFX_Module_'.$safeModule);
  184. $object->uninstall();
  185. $DB->delete($CONFIG->dbprefix.'modules', "keyname = '{$safeModule}'");
  186. $DB->delete($CONFIG->dbprefix.'admin_permissions', "keyname = 'perm_module_admin_{$safeModule}' AND module_key = 'core'");
  187. $DB->delete($CONFIG->dbprefix.'admin_users_to_permissions', "permission_key = 'perm_module_admin_{$safeModule}' AND module_key = 'core'");
  188. JFX::addSuccess('Module was uninstalled');
  189. JFX::redirect($CONFIG->adminUrl.'/modules/list/');
  190. }
  191. }
  192. }
  193. }
  194. // form to upload
  195. $formFields = array(
  196. array(
  197. 'name' => 'file',
  198. 'value' => '',
  199. 'type' => 'file',
  200. 'label' => 'File'
  201. ),
  202. array(
  203. 'name' => 'overwrite',
  204. 'value' => 1,
  205. 'type' => 'checkbox',
  206. 'label' => 'Overwrite existsing files?'
  207. ),
  208. array(
  209. 'name' => 'submitBtn',
  210. 'value' => 'Upload',
  211. 'type' => 'submit',
  212. 'label' => '&nbsp;'
  213. )
  214. );
  215. $form = JFX::makeRapidForm($formFields, 'Upload a module', 'upload-module');
  216. $SMARTY->assign('moduleUploadForm', (string) $form);
  217. $installedModules = JFX_Module::getInstalledModules();
  218. $modules = array();
  219. $readModules = array();
  220. foreach($installedModules as $k=>$v){
  221. if(array_key_exists($v['keyname'], $modules)) continue;
  222. $modules[] = array(
  223. 'keyname' => $v['keyname'],
  224. 'installed' => true,
  225. 'canInstall' => ($this->checkPermission('perm_install_modules') && $v['keyname'] != $this->keyname),
  226. 'object' => $v['object'],
  227. 'title' => $v['object']->getTitle(),
  228. 'hasAdmin' => (bool) (method_exists($v['object'], 'admin') && $USER->checkAdminPermission('core', 'perm_module_admin_'.$v['keyname']))
  229. );
  230. $readModules[$v['keyname']] = $v['keyname'];
  231. }
  232. $dh = opendir($CONFIG->modulesDir);
  233. while($file = readdir($dh)){
  234. if($file == '..' || $file == '.' || !is_dir($CONFIG->modulesDir.'/'.$file) || !file_exists($CONFIG->modulesDir.'/'.$file.'/'.$file.'.module.php')) continue;
  235. if(strtolower($file) != $file){
  236. JFX::addError($file.' is an invalid module name: must be lowercase');
  237. continue;
  238. }
  239. if(preg_replace('/[^a-zA-Z0-9]/', '', $file) != $file){
  240. JFX::addError($file.' is an invalid module name: must be alphanumeric');
  241. continue;
  242. }
  243. if(array_key_exists($file, $readModules)) continue;
  244. $count = count($modules);
  245. $modules[$count] = array('keyname' => $file);
  246. $modules[$count]['installed'] = JFX_Module::isInstalled($file);
  247. $modules[$count]['canInstall'] = ($this->checkPermission('perm_install_modules') && $v['keyname'] != $this->keyname);
  248. $obj = JFX::registry('JFX_Module_'.ucfirst(strtolower($file)));
  249. $modules[$count]['object'] = $obj;
  250. $modules[$count]['title'] = $obj->getTitle();
  251. $modules[$count]['hasAdmin'] = (bool) (method_exists($obj, 'admin') && $USER->checkAdminPermission('core', 'perm_module_admin_'.$file));
  252. }
  253. // get our themes
  254. $themes = array();
  255. $installedThemes = JFX_Theme::getInstalledThemes();
  256. $readThemes = array();
  257. foreach($installedThemes as $k=>$v){
  258. if(array_key_exists($v['keyname'], $themes)) continue;
  259. $themes[] = array(
  260. 'keyname' => $v['keyname'],
  261. 'installed' => true,
  262. 'canInstall' => ($this->checkPermission('perm_install_modules') && $v['keyname'] != $SETTINGS->get('theme-admin')),
  263. 'object' => $v['object'],
  264. 'title' => $v['object']->getTitle(),
  265. 'hasAdmin' => (bool) (method_exists($v['object'], 'admin') && $USER->checkAdminPermission('core', 'perm_module_admin_'.$v['keyname']))
  266. );
  267. $readThemes[$v['keyname']] = $v['keyname'];
  268. }
  269. /*
  270. $dh = opendir($CONFIG->themesDir);
  271. while($file = readdir($dh)){
  272. if($file == '..' || $file == '.' || !is_dir($CONFIG->themesDir.'/'.$file) || !file_exists($CONFIG->themesDir.'/'.$file.'/'.$file.'.theme.php')) continue;
  273. if(strtolower($file) != $file){
  274. JFX::addError($file.' is an invalid theme name: must be lowercase');
  275. continue;
  276. }
  277. if(preg_replace('/[^a-zA-Z0-9]/', '', $file) != $file){
  278. JFX::addError($file.' is an invalid theme name: must be alphanumeric');
  279. continue;
  280. }
  281. if(array_key_exists($file, $readThemes)) continue;
  282. $count = count($themes);
  283. $themes[$count] = array('keyname' => $file);
  284. $themes[$count]['installed'] = JFX_Theme::isInstalled($file);
  285. $obj = JFX::registry('JFX_Theme_'.ucfirst(strtolower($file)));
  286. $themes[$count]['object'] = $obj;
  287. $themes[$count]['title'] = $obj->getTitle();
  288. $themes[$count]['hasAdmin'] = (bool) (method_exists($obj, 'admin') && $USER->checkAdminPermission('core', 'perm_module_admin_'.$file));
  289. }
  290. */
  291. $eParams = array(
  292. 'modules' => $modules,
  293. 'themes' => $themes
  294. );
  295. JFX::hook('JFX_Core.modulesList', $eParams);
  296. $SMARTY->assign('modules', $eParams['modules']);
  297. $SMARTY->assign('themes', $eParams['themes']);
  298. $content = $SMARTY->fetch($CONFIG->templatesDir.'/admin/modules/list.tpl');
  299. echo $content;